11From Coq Require Import
22 Arith Lia (* nia *)
33 Morphisms
4+ Classes .RelationClasses
45.
56
67From ExtLib Require Import
78 Data.String
9+ Data.Monads.StateMonad
810.
911
1012From ITree Require Import
@@ -40,15 +42,15 @@ Definition denote_imp (c : com) : stateT env Delay unit :=
4042 interp_imp (denote_com c).
4143
4244Definition 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
4850Notation "{{ P }} c {{ Q }}" := (hoare_triple P Q c) (at level 70).
4951
5052Definition 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
5355Definition 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
6365Lemma 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 ).
6567Proof .
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.
8386Qed .
8487
8588Lemma 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 ).
8790Proof .
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}}.
121124Proof .
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.
130134Qed .
@@ -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.
144148Qed .
145149
146150Definition 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) ).
152156Proof .
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.
157161Qed .
158162
163+ (*
159164Global 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).
161166Proof.
162167 repeat intro. unfold run_state_itree. unfold state_eq in H. rewrite H. reflexivity.
163168Qed.
@@ -174,7 +179,7 @@ Global Instance eutt_proper_under_interp_state
174179Proof.
175180 repeat intro. unfold interp_state. rewrite H. reflexivity.
176181Qed.
177-
182+ *)
178183(*
179184Check (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.
202207Set Default Timeout 15.
203208
204209Global 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 ).
206212Proof .
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.
211217Qed .
212218
213219Lemma 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
219226Proof .
220- unfold state_eq. intros. eapply interp_state_bind.
227+ Locate interp_state_bind'.
228+ unfold eq_stateT. intros. eapply interp_state_bind'.
221229Qed .
222230
223231Definition 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
238234Global 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).
240236Proof .
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.
244240Qed .
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.
253253Proof .
254254 intros. simpl. unfold denote_imp. simpl. unfold while. unfold interp_imp at 1, interp_map at 1.
255255 cbn. red. red. intros. symmetry.
0 commit comments