Skip to content

Commit 5c3ee42

Browse files
committed
IN PROGRESS
1 parent 2c6f53d commit 5c3ee42

File tree

4 files changed

+77
-59
lines changed

4 files changed

+77
-59
lines changed

extra/Dijkstra/DelaySpecMonad.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ Delimit Scope delayspec_scope with delayspec.
5858
Notation "a ∈ b" := (proj1_sig (A := _ -> Prop) b a) (at level 70) : delayspec_scope.
5959
Notation "a ∋ b" := (proj1_sig (A := _ -> Prop) a b) (at level 70, only parsing) : delayspec_scope.
6060

61-
Definition Delay (A : Type) := itree void1 A.
61+
Definition Delay := itree void1.
6262

6363
#[global] Instance EqMDelay : Eq1 Delay := @ITreeMonad.Eq1_ITree void1.
6464
#[global] Instance MonadDelay : Monad Delay := @Monad_itree void1.

hoare_example/Imp.v

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ Module ImpNotations.
107107
if b then BTrue else BFalse.
108108
Coercion bool_to_bexp : bool >-> bexp.
109109

110+
Declare Scope imp_scope.
110111
Bind Scope imp_scope with aexp.
111112
Bind Scope imp_scope with bexp.
112113
Delimit Scope imp_scope with imp.
@@ -361,7 +362,8 @@ From ITree Require Import
361362
From ExtLib Require Import
362363
Core.RelDec
363364
Structures.Maps
364-
Data.Map.FMapAList.
365+
Data.Map.FMapAList
366+
Data.Monads.StateMonad.
365367
(* end hide *)
366368

367369
(** We provide an _ITree event handler_ to interpret away [ImpState] events. We
@@ -402,8 +404,8 @@ Definition interp_imp {E A} (t : itree (ImpState +' E) A) : stateT env (itree E
402404
interp_map t'.
403405

404406

405-
Definition eval_imp (s: com) : itree void1 (env * unit) :=
406-
interp_imp (denote_com s) empty.
407+
Definition eval_imp (s: com) : itree void1 (unit * env) :=
408+
runStateT (interp_imp (denote_com s)) empty.
407409

408410
(** Equipped with this evaluator, we can now compute.
409411
Naturally since Coq is total, we cannot do it directly inside of it.
@@ -433,20 +435,19 @@ Section InterpImpProperties.
433435

434436
(** This interpreter is compatible with the equivalence-up-to-tau. *)
435437
Global Instance eutt_interp_imp {R}:
436-
Proper (@eutt E R R eq ==> eq ==> @eutt E' (prod (env) R) (prod _ R) eq)
438+
Proper (@eutt E R R eq ==> eq_stateT (@eutt E' (prod R (env)) (prod R _) eq))
437439
interp_imp.
438440
Proof.
439441
repeat intro.
440442
unfold interp_imp.
441-
unfold interp_map.
442-
rewrite H0. eapply eutt_interp_state_eq; auto.
443-
rewrite H. reflexivity.
443+
rewrite H.
444+
reflexivity.
444445
Qed.
445446

446447
(** [interp_imp] commutes with [bind]. *)
447448
Lemma interp_imp_bind: forall {R S} (t: itree E R) (k: R -> itree E S) (g : env),
448-
(interp_imp (ITree.bind t k) g)
449-
(ITree.bind (interp_imp t g) (fun '(g', x) => interp_imp (k x) g')).
449+
runStateT (interp_imp (ITree.bind t k)) g
450+
≅ ITree.bind (runStateT (interp_imp t) g) (fun '(x, g') => runStateT (interp_imp (k x)) g').
450451
Proof.
451452
intros.
452453
unfold interp_imp.

hoare_example/ImpHoare.v

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
From Coq Require Import
22
Arith Lia (* nia *)
33
Morphisms
4+
Classes.RelationClasses
45
.
56

67
From ExtLib Require Import
78
Data.String
9+
Data.Monads.StateMonad
810
.
911

1012
From ITree Require Import
@@ -40,15 +42,15 @@ Definition denote_imp (c : com) : stateT env Delay unit :=
4042
interp_imp (denote_com c).
4143

4244
Definition hoare_triple (P Q : env -> Prop) (c : com) : Prop :=
43-
forall (s s' :env), P s -> (denote_imp c s ≈ ret (s',tt)) -> Q s'.
45+
forall (s s' :env), P s -> (runStateT (denote_imp c) s ≈ runStateT (ret tt) s') -> Q s'.
4446

45-
Definition lift_imp_post (P : env -> Prop) : Delay (env * unit) -> Prop :=
46-
fun (t : Delay (env * unit) ) => (exists (s : env), ret (s, tt) ≈ t /\ P s).
47+
Definition lift_imp_post (P : env -> Prop) : Delay (unit * env) -> Prop :=
48+
fun (t : Delay (unit * env) ) => (exists (s : env), ret (tt,s) ≈ t /\ P s).
4749

4850
Notation "{{ P }} c {{ Q }}" := (hoare_triple P Q c) (at level 70).
4951

5052
Definition is_bool (E : Type -> Type) (bc : bool) (be : bexp) (s : env) : Prop :=
51-
@interp_imp E bool (denote_bexp be) s ≈ ret (s, bc).
53+
runStateT (@interp_imp E bool (denote_bexp be)) s ≈ ret (bc, s).
5254

5355
Definition is_true (b : bexp) (s : env) : Prop :=
5456
is_bool void1 true b s.
@@ -61,12 +63,13 @@ Ltac unf_intep := unfold interp_imp, interp_map, interp_state, interp, Basics.it
6163
*)
6264

6365
Lemma aexp_term : forall (E : Type -> Type) (ae : aexp) (s : env),
64-
exists (n : nat), @interp_imp void1 _ (denote_aexp ae) s ≈ Ret (s,n).
66+
exists (n : nat), runStateT (@interp_imp void1 _ (denote_aexp ae)) s ≈ Ret (n,s).
6567
Proof.
6668
intros. induction ae.
6769
- exists n. cbn. tau_steps. reflexivity.
6870
(*getvar case, extract to a lemma*)
69-
- cbn. exists (lookup_default x 0 s).
71+
- unfold interp_imp, interp_map, interp_state.
72+
cbn. exists (lookup_default x 0 s).
7073
tau_steps. reflexivity.
7174
- basic_solve. exists (n0 + n)%nat.
7275
cbn. setoid_rewrite interp_imp_bind. rewrite IHae1.
@@ -83,7 +86,7 @@ Proof.
8386
Qed.
8487

8588
Lemma bools_term : forall (be : bexp) (s : env),
86-
exists (bc : bool), @interp_imp void1 _ (denote_bexp be) s ≈ Ret (s,bc).
89+
exists (bc : bool), runStateT (@interp_imp void1 _ (denote_bexp be)) s ≈ Ret (bc,s).
8790
Proof.
8891
intros. induction be.
8992
- exists true. cbn. unfold interp_imp, interp_map, interp_state. repeat rewrite interp_ret.
@@ -120,11 +123,12 @@ Lemma hoare_seq : forall (c1 c2 : com) (P Q R : env -> Prop), {{P}} c1 {{Q}} ->
120123
{{P}} c1 ;;; c2 {{R}}.
121124
Proof.
122125
unfold hoare_triple. intros c1 c2 P Q R Hc1 Hc2 s s' Hs Hs'.
123-
unfold denote_imp in Hs'. cbn in Hs'. rewrite interp_imp_bind in Hs'.
126+
unfold denote_imp in Hs'. cbn in Hs'. unfold Delay in Hs'. rewrite interp_imp_bind in Hs'.
124127
fold (denote_imp c1) in Hs'. fold (denote_imp c2) in Hs'.
125-
destruct (eutt_reta_or_div (denote_imp c1 s) ); basic_solve.
126-
- destruct a as [s'' [] ]. rewrite <- H in Hs'. setoid_rewrite bind_ret_l in Hs'. symmetry in H.
128+
destruct (eutt_reta_or_div (runStateT (denote_imp c1) s) ); basic_solve.
129+
- destruct a as [[] s'']. rewrite <- H in Hs'. setoid_rewrite bind_ret_l in Hs'. symmetry in H.
127130
eapply Hc2; eauto.
131+
eapply Hc1; eauto.
128132
- apply div_spin_eutt in H. rewrite H in Hs'. rewrite <- spin_bind in Hs'.
129133
symmetry in Hs'. exfalso. eapply not_ret_eutt_spin. eauto.
130134
Qed.
@@ -137,27 +141,28 @@ Proof.
137141
unfold hoare_triple. intros c1 c2 b P Q Hc1 Hc2 s s' Hs.
138142
unfold denote_imp. cbn.
139143
destruct (classic_bool b s).
140-
- unfold is_true, is_bool in H. rewrite interp_imp_bind.
144+
- unfold is_true, is_bool in H. unfold Delay. rewrite interp_imp_bind.
141145
rewrite H. setoid_rewrite bind_ret_l. apply Hc1. auto.
142-
- unfold is_false, is_bool in H. rewrite interp_imp_bind.
146+
- unfold is_false, is_bool in H. unfold Delay. rewrite interp_imp_bind.
143147
rewrite H. setoid_rewrite bind_ret_l. apply Hc2. auto.
144148
Qed.
145149

146150
Definition app {A B : Type} (f : A -> B) (a : A) := f a.
147151

148-
Definition run_state_itree {A S : Type} {E : Type -> Type} (s : S) (m : stateT S (itree E) A ) : itree E (S * A) :=
149-
m s.
152+
Definition run_state_itree {A S : Type} {E : Type -> Type} (s : S) (m : stateT S (itree E) A ) : itree E (A * S) :=
153+
runStateT m s.
150154

151-
Global Instance EqStateEq {S R: Type} {E : Type -> Type} : Equivalence (@state_eq E R S).
155+
Global Instance EqStateEq {S R: Type} {E : Type -> Type} : Equivalence (@eq_stateT S (itree E) R (eq_itree eq)).
152156
Proof.
153157
constructor; repeat intro.
154158
- reflexivity.
155-
- unfold state_eq in H. symmetry. auto.
156-
- unfold state_eq in *. rewrite H. auto.
159+
- unfold eq_stateT in H. symmetry. auto.
160+
- unfold eq_stateT in *. rewrite H. auto.
157161
Qed.
158162

163+
(*
159164
Global Instance run_state_proper_eq_itree {E : Type -> Type} {S R : Type} {s : S} :
160-
Proper (@state_eq E S R ==> eq_itree eq) (@run_state_itree R S E s).
165+
Proper (@eq_stateT S (itree E) R (eq_itree eq)) (@run_state_itree R S E s).
161166
Proof.
162167
repeat intro. unfold run_state_itree. unfold state_eq in H. rewrite H. reflexivity.
163168
Qed.
@@ -174,7 +179,7 @@ Global Instance eutt_proper_under_interp_state
174179
Proof.
175180
repeat intro. unfold interp_state. rewrite H. reflexivity.
176181
Qed.
177-
182+
*)
178183
(*
179184
Check (case_ (handle_map (V := value) pure_state ) ).
180185
@@ -191,8 +196,8 @@ Section interp_state_eq_iter.
191196
Context (a : A).
192197

193198

194-
Lemma interp_state_eq_iter : state_eq (interp_state f (ITree.iter g a) )
195-
(MonadIter_stateT0 _ _ (fun a0 => interp_state f (g a0)) a).
199+
Lemma interp_state_eq_iter : eq_stateT (eq_itree eq) (interp_state f (ITree.iter g a))
200+
(MonadIter_stateT _ _ (fun a0 => interp_state f (g a0)) a).
196201
Proof.
197202
unfold ITree.iter, Iter_Kleisli, Basics.iter, MonadIter_itree.
198203
eapply interp_state_iter; reflexivity.
@@ -202,54 +207,49 @@ End interp_state_eq_iter.
202207
Set Default Timeout 15.
203208

204209
Global Instance proper_state_eq_iter {S: Type} :
205-
Proper (@state_eq void1 S (unit + unit) ==> @state_eq void1 S (unit) ) (fun body => @MonadIter_stateT0 Delay S _ _ unit unit (fun _ : unit => body) tt ).
210+
Proper (@eq_stateT S (itree void1) (unit + unit) (eq_itree eq) ==> @eq_stateT S (itree void1) unit (eq_itree eq))
211+
(fun body => @MonadIter_stateT Delay S _ _ unit unit (fun _ : unit => body) tt ).
206212
Proof.
207213
repeat intro.
208-
unfold MonadIter_stateT0, Basics.iter, MonadIterDelay. eapply eq_itree_iter.
209-
repeat intro. subst. destruct y0 as [s' [] ].
214+
unfold MonadIter_stateT, Basics.iter, MonadIterDelay. eapply eq_itree_iter.
215+
repeat intro. subst. destruct y0 as [[] s'].
210216
simpl. specialize (H s'). rewrite H. reflexivity.
211217
Qed.
212218

213219
Lemma interp_state_bind_state : forall (E F : Type -> Type) (A B S : Type)
214-
(h : forall T : Type, E T -> S -> itree F (S * T) ) (t : itree E A)
220+
(h : forall T : Type, E T -> stateT S (itree F) T) (t : itree E A)
215221
(k : A -> itree E B),
216-
state_eq (interp_state h (ITree.bind t k))
217-
(bind (interp_state h t) (fun a => interp_state h (k a) ) ).
222+
eq_stateT (eq_itree eq)
223+
(interp_state h (ITree.bind t k))
224+
(bind (interp_state h t) (fun a => interp_state h (k a))).
218225

219226
Proof.
220-
unfold state_eq. intros. eapply interp_state_bind.
227+
Locate interp_state_bind'.
228+
unfold eq_stateT. intros. eapply interp_state_bind'.
221229
Qed.
222230

223231
Definition state_eq2 {E : Type -> Type} {A B S : Type} (k1 k2 : A -> stateT S (itree E) B ) : Prop :=
224-
forall a, state_eq (k1 a) (k2 a).
225-
226-
Lemma eq_itree_clo_bind {E : Type -> Type} {R1 R2 : Type} :
227-
forall (RR : R1 -> R2 -> Prop) (U1 U2 : Type) (UU : U1 -> U2 -> Prop)
228-
(t1 : itree E U1) (t2 : itree E U2)
229-
(k1 : U1 -> itree E R1) (k2 : U2 -> itree E R2),
230-
eq_itree UU t1 t2 ->
231-
(forall (u1 : U1) (u2 : U2), UU u1 u2 -> eq_itree RR (k1 u1) (k2 u2) ) ->
232-
eq_itree RR (ITree.bind t1 k1) (ITree.bind t2 k2).
233-
Proof.
234-
intros. unfold eq_itree in *. eapply eqit_bind'; eauto.
235-
Qed.
236-
232+
forall a, eq_stateT (eq_itree eq) (k1 a) (k2 a).
237233

238234
Global Instance bind_state_eq2 {E : Type -> Type} {A B S : Type} {m : stateT S (itree E) A} :
239-
Proper (@state_eq2 E A B S ==> @state_eq E S B) (bind m).
235+
Proper (@state_eq2 E A B S ==> @eq_stateT S (itree E) B (eq_itree eq)) (bind m).
240236
Proof.
241-
repeat intro. unfold state_eq2, state_eq in H. cbn.
237+
repeat intro. unfold state_eq2, eq_stateT in H. cbn.
242238
eapply eq_itree_clo_bind; try reflexivity. intros. subst.
243239
destruct u2 as [s' a]. simpl. rewrite H. reflexivity.
244240
Qed.
245241

246242
(*can actually make this nicer*)
247-
Lemma compile_while : forall (b : bexp) (c : com),
248-
((denote_imp ( WHILE b DO c END )) ≈ MonadIter_stateT0 unit unit
249-
(fun _ : unit => bind (interp_imp (denote_bexp b))
250-
(fun b : bool => if b
251-
then bind (denote_imp c) (fun _ : unit => interp_imp (Ret (inl tt)) )
252-
else interp_imp (Ret (inr tt))) ) tt)%monad.
243+
Lemma compile_while :
244+
forall (b : bexp) (c : com),
245+
(denote_imp ( WHILE b DO c END ) ≈
246+
MonadIter_stateT unit unit
247+
(fun _ : unit =>
248+
bind (interp_imp (denote_bexp b))
249+
(fun b : bool =>
250+
if b
251+
then denote_imp c ;; interp_imp (Ret (inl tt))
252+
else interp_imp (Ret (inr tt)))) tt)%monad.
253253
Proof.
254254
intros. simpl. unfold denote_imp. simpl. unfold while. unfold interp_imp at 1, interp_map at 1.
255255
cbn. red. red. intros. symmetry.

theories/Events/StateFacts.v

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,23 @@ Proof.
172172
auto with paco.
173173
Qed.
174174

175+
Lemma interp_state_bind' {E F : Type -> Type} {A B S : Type}
176+
(f : forall T, E T -> stateT S (itree F) T)
177+
(t : itree E A) (k : A -> itree E B)
178+
(s : S) :
179+
(runStateT (interp_state f (t >>= k)) s)
180+
181+
(runStateT (interp_state f t) s >>= fun '(t, s) => runStateT (interp_state f (k t)) s).
182+
Proof.
183+
rewrite interp_state_bind.
184+
apply eq_itree_clo_bind with (UU := eq).
185+
- reflexivity.
186+
- intros.
187+
subst.
188+
destruct u2.
189+
reflexivity.
190+
Qed.
191+
175192
#[global]
176193
Instance eutt_interp_state {E F: Type -> Type} {S : Type}
177194
(h : E ~> stateT S (itree F)) R RR :

0 commit comments

Comments
 (0)