1 Require Import Computation.Monad.
2 Require Import Coq.Logic.JMeq.
6 (* an inductive predicate proving that a given computation terminates with a particular value *)
7 Reserved Notation "c ! y" (at level 30).
8 Inductive TerminatesWith (A:Set) : #A -> A -> Prop :=
9 | TerminatesReturnWith :
13 | TerminatesBindWith :
14 forall (B:Set) (b:B) (a:A) (f:B->#A) (c:#B),
18 where "c ! y" := (TerminatesWith _ c y)
21 (* an inductive predicate proving that a given computation terminates with /some/ value *)
22 Reserved Notation "c !?" (at level 30).
23 Inductive Terminates (A:Set)(c:#A) : Prop :=
24 | Terminates_intro : (exists a:A, c!a) -> c!?
25 where "c !?" := (Terminates _ c).
27 Inductive InvokedBy (A B C:Set) : #A -> #B -> #C -> Prop :=
28 | invokesPrev : forall
32 InvokedBy A B C (@Bind A B f c) c z
33 | invokesFunc : forall
39 (_:JMeq (pf (f b)) (f b))
41 InvokedBy A B C (@Bind A C f c) (pf (f b)) c.
43 Inductive Safe : forall (A:Set) (c:#A), Prop :=
45 forall (A:Set) (c:#A),
46 (forall (B C:Set) (c':#B)(z:#C), InvokedBy A B C c c' z -> Safe B c')
50 : forall (A B C:Set)(c:#A)(_:Safe A c)(c':#B)(z:#C)(_:InvokedBy A B C c c' z), Safe B c'.
55 Notation "{ c }!" := {a:_|TerminatesWith _ c a} (at level 5).
56 Notation "'!Let' x := y 'in' z" := ((fun x => z)y)(at level 100).
57 Definition eval' CC cc (Z:Set) (z:#Z) (s:Safe CC cc) : {cc}!.
61 match c return (forall PRED pred Z z, InvokedBy C PRED Z c pred z -> {pred}!) -> {c}! with
62 | Return x => fun _ => exist _ x _
65 match eval_pred CN cn Z z (invokesPrev C CN Z z cn f) with
67 match eval_pred C (f b) CN cn _ with
68 | exist a' pf' => exist _ a' _
73 fix eval_all C c Z z (s:Safe C c) {struct s} : {c}! :=
74 eval_one_step C c Z z (fun C' c' Z z icc => eval_all C' c' Z z (Safe_inv C C' Z c s c' z icc))
79 refine (invokesFunc C C CN cn b f (fun x:#C=>x) _ _ _).
84 apply (TerminatesBindWith C CN b a' f cn).
89 Theorem jmeq_lemma : forall (A1 A2 B:Set)(c1:#A1)(c2:#A2)(f1:A1->#B)(f2:A2->#B),
91 -> (JMeq c1 c2) /\ (JMeq f1 f2) /\ (A1=A2).
106 Lemma computation_is_deterministic :
107 forall (A:Set) (c:#A) (x y:A), c!x -> c!y -> x=y.
120 apply jmeq_lemma in H4.
128 apply IHTerminatesWith2.
129 apply IHTerminatesWith1 in H2.
134 Theorem termination_is_safe : forall (A:Set) (c:#A), c!? -> Safe A c.
146 apply jmeq_lemma in H2.
155 apply jmeq_lemma in H4.
168 apply (computation_is_deterministic B c b b0 H H3).
176 apply IHTerminatesWith2.
181 Implicit Arguments Terminates [A].
182 Implicit Arguments TerminatesReturnWith [A].
183 Implicit Arguments TerminatesBindWith [A].
184 Implicit Arguments eval' [CC].