Library QedkConcrete.vmmachinewords

Word ranges through VM state operations

The invariant includes the program counter, stack and memory chunks, alongside a Host invariant chosen by the caller. Successful pops return range facts for their operands. Errors preserve the invariant too: a failed child call can return control to its parent.
From Stdlib Require Import List ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Vm QedkConcrete.wordrange QedkConcrete.memorywords.
Set Default Goal Selector "!".
Local Open Scope Z_scope.

Module V := Vm.Make.
Module MS := V.MachineState.

Section Machine.
Context {Host_t : Set} {F : @V.FArgs Host_t}.
Context (hostInvariant : Host_t → Prop).

Definition machineWords (state : @MS.t Host_t F) : Prop :=
  Forall wordRange (MS.t.stack state) ∧ wordRange (MS.t.pc state) ∧
    hostInvariant (MS.t.host state) ∧ memoryWordChunks (MS.t.memory state).

Definition machineSafe {A : Set} (returned : A → Prop) (action : @V.M.t Host_t F A) : Prop :=
  ∀ state, machineWords state →
    machineWords (snd (action state)) ∧
    match fst (action state) with inl value ⇒ returned value | inr _ ⇒ True end.

Lemma initial_machine_words host gas capacity :
  hostInvariant host →
  machineWords (@MS.initial Host_t F host gas capacity).
Proof.
  intro Hhost. split; [constructor|]. split; [apply word_zero|].
  split; [exact Hhost|apply empty_word_chunks].
Qed.

Lemma safe_bind {A B : Set} (P : A → Prop) (Q : B → Prop)
    (action : @V.M.t Host_t F A) (next : A → @V.M.t Host_t F B) :
  machineSafe P action → (∀ value, P value → machineSafe Q (next value)) →
  machineSafe Q (@V.M.op_letdollar Host_t F A B action next).
Proof.
  intros Haction Hnext state Hinitial. specialize (Haction state Hinitial).
  change (machineWords (snd
    (match action state with (inl v, st) ⇒ next v st | (inr e, st) ⇒ (inr e, st) end)) ∧
    match fst
      (match action state with (inl v, st) ⇒ next v st | (inr e, st) ⇒ (inr e, st) end)
    with inl v ⇒ Q v | inr _ ⇒ True end).
  destruct (action state) as [[value|error] middle]; cbn [snd fst] in ×.
  - apply Hnext; [exact (proj2 Haction)|exact (proj1 Haction)].
  - exact Haction.
Qed.

Lemma safe_return {A : Set} (P : A → Prop) value :
  P value → machineSafe P (@V.M._return Host_t F A value).
Proof. intros HP state Hinitial. split; assumption. Qed.

Lemma safe_fail {A : Set} (P : A → Prop) error :
  machineSafe P (@V.M.fail Host_t F A error).
Proof. intros state Hinitial. split; [exact Hinitial|exact I]. Qed.

Lemma safe_fmap {A B : Set} (P : A → Prop) (f : A → B) action :
  machineSafe P action →
  machineSafe (fun result ⇒ ∃ value, P value ∧ result = f value)
    (@V.M.op_ltdollargt Host_t F A B f action).
Proof.
  intros Haction state Hinitial. specialize (Haction state Hinitial).
  change (machineWords (snd
    (match action state with (inl v, st) ⇒ (inl (f v), st) | (inr e, st) ⇒ (inr e, st) end)) ∧
    match fst
      (match action state with (inl v, st) ⇒ (inl (f v), st) | (inr e, st) ⇒ (inr e, st) end)
    with inl v ⇒ ∃ value, P value ∧ v = f value | inr _ ⇒ True end).
  destruct (action state) as [[value|error] middle]; cbn [snd fst] in ×.
  - split; [exact (proj1 Haction)|∃ value; split; [exact (proj2 Haction)|reflexivity]].
  - exact Haction.
Qed.

Lemma safe_push value : wordRange value →
  machineSafe (fun _ ⇒ True) (@V.push Host_t F value).
Proof.
  intros Hvalue state Hinitial. pose proof Hinitial as [Hstack [Hpc Hhost]]. unfold V.push.
  destruct (Z.geb (MS.t.stack_depth state) V.max_stack_depth).
  - split; [exact Hinitial|exact I].
  - split; [|exact I]. split; [constructor; assumption|]. split; assumption.
Qed.

Lemma safe_pop : machineSafe wordRange (@V.pop Host_t F).
Proof.
  intros state Hinitial. pose proof Hinitial as [Hstack [Hpc Hhost]]. unfold V.pop.
  destruct (MS.t.stack state) as [|value tail] eqn:Hvalues.
  - split; [exact Hinitial|exact I].
  - inversion Hstack; subst. split; [|assumption]. split; [assumption|]. split; assumption.
Qed.

Lemma safe_pop2 : machineSafe (fun pair ⇒ wordRange (fst pair) ∧ wordRange (snd pair))
    (@V.pop2 Host_t F).
Proof.
  intros state Hinitial. pose proof Hinitial as [Hstack [Hpc Hhost]]. unfold V.pop2.
  destruct (MS.t.stack state) as [|a tail] eqn:Hvalues.
  - split; [exact Hinitial|exact I].
  - destruct tail as [|b tail].
    + split; [exact Hinitial|exact I].
    + inversion Hstack; subst. match goal with H : Forall _ (_ :: _) |- _ ⇒ inversion H; subst end.
      split; [split; [assumption|split; assumption]|split; assumption].
Qed.

Lemma safe_pop3 : machineSafe
    (fun triple ⇒ wordRange (fst (fst triple)) ∧ wordRange (snd (fst triple)) ∧ wordRange (snd triple))
    (@V.pop3 Host_t F).
Proof.
  intros state Hinitial. pose proof Hinitial as [Hstack [Hpc Hhost]]. unfold V.pop3.
  destruct (MS.t.stack state) as [|a tail] eqn:Hvalues.
  - split; [exact Hinitial|exact I].
  - destruct tail as [|b tail].
    + split; [exact Hinitial|exact I].
    + destruct tail as [|c tail].
      × split; [exact Hinitial|exact I].
      × inversion Hstack; subst.
        repeat match goal with H : Forall _ (_ :: _) |- _ ⇒ inversion H; clear H; subst end.
        split; [split; [assumption|split; assumption]|split; [assumption|split; assumption]].
Qed.

Lemma safe_spend amount :
  machineSafe (fun _ ⇒ True) (@V.spend Host_t F amount).
Proof.
  intros state Hinitial. unfold V.spend.
  destruct (Gas.op_lt (MS.t.gas state) amount); split; [exact Hinitial|exact I|exact Hinitial|exact I].
Qed.

Lemma safe_update_pc f : (∀ pc, wordRange pc → wordRange (f pc)) →
  machineSafe (fun _ ⇒ True) (@V.update_pc_and_continue Host_t F f).
Proof.
  intros Hf state [Hstack [Hpc Hhost]]. split; [|exact I].
  split; [exact Hstack|]. split; [apply Hf; exact Hpc|exact Hhost].
Qed.

Lemma safe_increase_pc :
  machineSafe (fun _ ⇒ True) (@V.increase_pc_and_continue Host_t F).
Proof. apply safe_update_pc. intros pc Hpc. apply word_add. Qed.

Lemma safe_read {A : Set} (P : A → Prop) (lens : Lens.t (@MS.t Host_t F) A) :
  (∀ state, machineWords state → P (Lens.t.get lens state)) →
  machineSafe P (@V.M.op_exclamation Host_t F A lens).
Proof. intros Hread state Hinitial. split; [exact Hinitial|apply Hread; exact Hinitial]. Qed.

Lemma safe_write {A : Set} (lens : Lens.t (@MS.t Host_t F) A) value :
  (∀ state, machineWords state → machineWords (Lens.t.set lens value state)) →
  machineSafe (fun _ ⇒ True) (@V.M.op_coloneq Host_t F A lens value).
Proof. intros Hwrite state Hinitial. split; [apply Hwrite; exact Hinitial|exact I]. Qed.

Lemma safe_when condition action :
  machineSafe (fun _ ⇒ True) action →
  machineSafe (fun _ ⇒ True) (@V.M.when_ Host_t F condition action).
Proof. intro Haction. destruct condition; [exact Haction|apply safe_return; exact I]. Qed.

Lemma safe_option {A : Set} (P : A → Prop) error (value : option A) :
  (∀ v, value = Some v → P v) →
  machineSafe P (@V.M.Option.or_fail Host_t F A error value).
Proof.
  intro Hvalue. destruct value; [apply safe_return, Hvalue; reflexivity|apply safe_fail].
Qed.

Lemma safe_finish returnOutput :
  machineSafe (fun _ ⇒ True) (@V.finish_execution Host_t F returnOutput).
Proof.
  unfold V.finish_execution. eapply safe_bind.
  - apply safe_when, safe_write. intros state Hinitial. exact Hinitial.
  - intros [] _. apply safe_return. exact I.
Qed.

Lemma safe_memory_read : machineSafe memoryWordChunks
  (@V.M.op_exclamation Host_t F _ MS.memory).
Proof. apply safe_read. intros state [_ [_ [_ Hmemory]]]. exact Hmemory. Qed.

Lemma safe_memory_write memory : memoryWordChunks memory →
  machineSafe (fun _ ⇒ True) (@V.M.op_coloneq Host_t F _ MS.memory memory).
Proof.
  intro Hmemory. apply safe_write. intros state [Hstack [Hpc [Hhost _]]].
  split; [exact Hstack|]. split; [exact Hpc|]. split; assumption.
Qed.
End Machine.