Changeset 487 for Deliverables/D3.1/Csemantics/IOMonad.ma
 Timestamp:
 Feb 9, 2011, 11:49:17 AM (9 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

Deliverables/D3.1/Csemantics/IOMonad.ma
r411 r487 1 include "Plogic/russell_support.ma".2 1 include "extralib.ma". 3 2 include "Errors.ma". … … 5 4 (* IO monad *) 6 5 7 ninductive IO (output:Type) (input:output → Type) (T:Type) : Type≝6 inductive IO (output:Type[0]) (input:output → Type[0]) (T:Type[0]) : Type[0] ≝ 8 7  Interact : ∀o:output. (input o → IO output input T) → IO output input T 9 8  Value : T → IO output input T 10 9  Wrong : IO output input T. 11 10 12 nlet rec bindIO (O:Type) (I:O → Type) (T,T':Type) (v:IO O I T) (f:T → IO O I T') on v : IO O I T' ≝11 let rec bindIO (O:Type[0]) (I:O → Type[0]) (T,T':Type[0]) (v:IO O I T) (f:T → IO O I T') on v : IO O I T' ≝ 13 12 match v with 14 13 [ Interact out k ⇒ (Interact ??? out (λres. bindIO O I T T' (k res) f)) … … 17 16 ]. 18 17 19 nlet rec bindIO2 (O:Type) (I:O → Type) (T1,T2,T':Type) (v:IO O I (T1×T2)) (f:T1 → T2 → IO O I T') on v : IO O I T' ≝18 let rec bindIO2 (O:Type[0]) (I:O → Type[0]) (T1,T2,T':Type[0]) (v:IO O I (T1×T2)) (f:T1 → T2 → IO O I T') on v : IO O I T' ≝ 20 19 match v with 21 20 [ Interact out k ⇒ (Interact ??? out (λres. bindIO2 ?? T1 T2 T' (k res) f)) 22  Value v' ⇒ match v' with [ mk_pair v1 v2 ⇒ f v1 v2 ]21  Value v' ⇒ match v' with [ pair v1 v2 ⇒ f v1 v2 ] 23 22  Wrong ⇒ Wrong ?? T' 24 23 ]. 25 24 26 ndefinition err_to_io : ∀O,I,T. res T → IO O I T ≝25 definition err_to_io : ∀O,I,T. res T → IO O I T ≝ 27 26 λO,I,T,v. match v with [ OK v' ⇒ Value O I T v'  Error ⇒ Wrong O I T ]. 28 ncoercion err_to_io : ∀O,I,A.∀c:res A.IO O I A ≝ err_to_io on _c:res ? to IO ???.29 ndefinition err_to_io_sig : ∀O,I,T.∀P:T → Prop. res (sigma T P) → IO O I (sigmaT P) ≝30 λO,I,T,P,v. match v with [ OK v' ⇒ Value O I ( sigma T P) v'  Error ⇒ Wrong O I (sigmaT P) ].31 (* ncoercion err_to_io_sig : ∀O,I,A.∀P:A → Prop.∀c:res (sigma A P).IO O I (sigma A P) ≝ err_to_io_sig on _c:res (sigma ??) to IO ?? (sigma??).*)27 coercion err_to_io : ∀O,I,A.∀c:res A.IO O I A ≝ err_to_io on _c:res ? to IO ???. 28 definition err_to_io_sig : ∀O,I,T.∀P:T → Prop. res (Sig T P) → IO O I (Sig T P) ≝ 29 λO,I,T,P,v. match v with [ OK v' ⇒ Value O I (Sig T P) v'  Error ⇒ Wrong O I (Sig T P) ]. 30 (*coercion err_to_io_sig : ∀O,I,A.∀P:A → Prop.∀c:res (Sig A P).IO O I (Sig A P) ≝ err_to_io_sig on _c:res (Sig ??) to IO ?? (Sig ??).*) 32 31 33 32 … … 42 41 notation < "vbox(! \nbsp 〈ident v1 : ty1, ident v2 : ty2〉 ← e; break e')" with precedence 40 for @{'bindIO2 ${e} (λ${ident v1} : ${ty1}.λ${ident v2} : ${ty2}.${e'})}. 43 42 interpretation "IO monad bind" 'bindIO e f = (bindIO ???? e f). 44 interpretation "IO monad pairbind" 'bindIO2 e f = (bindIO2 ????? e f).43 interpretation "IO monad Prod bind" 'bindIO2 e f = (bindIO2 ????? e f). 45 44 (**) 46 nlet rec P_io O I (A:Type) (P:A → Prop) (v:IO O I A) on v : Prop ≝45 let rec P_io O I (A:Type[0]) (P:A → Prop) (v:IO O I A) on v : Prop ≝ 47 46 match v return λ_.Prop with 48 47 [ Wrong ⇒ True … … 51 50 ]. 52 51 53 nlet rec P_io' O I (A:Type) (P:A → Prop) (v:IO O I A) on v : Prop ≝52 let rec P_io' O I (A:Type[0]) (P:A → Prop) (v:IO O I A) on v : Prop ≝ 54 53 match v return λ_.Prop with 55 54 [ Wrong ⇒ False … … 58 57 ]. 59 58 60 ndefinition P_to_P_option_io : ∀O,I,A.∀P:A → Prop.option (IO O I A) → Prop ≝59 definition P_to_P_option_io : ∀O,I,A.∀P:A → Prop.option (IO O I A) → Prop ≝ 61 60 λO,I,A,P,a.match a with 62 61 [ None ⇒ False … … 64 63 ]. 65 64 66 nlet rec io_inject_0 O I (A:Type) (P:A → Prop) (a:IO O I A) (p:P_io O I A P a) on a : IO O I (sigmaA P) ≝65 let rec io_inject_0 O I (A:Type[0]) (P:A → Prop) (a:IO O I A) (p:P_io O I A P a) on a : IO O I (Sig A P) ≝ 67 66 (match a return λa'.P_io O I A P a' → ? with 68 67 [ Wrong ⇒ λ_. Wrong O I ? 69  Value c ⇒ λp'. Value ??? ( sig_introA P c p')68  Value c ⇒ λp'. Value ??? (dp A P c p') 70 69  Interact out k ⇒ λp'. Interact ??? out (λv. io_inject_0 O I A P (k v) (p' v)) 71 70 ]) p. 72 71 73 ndefinition io_inject : ∀O,I,A.∀P:A → Prop.∀a:option (IO O I A).∀p:P_to_P_option_io O I A P a.IO O I (sigmaA P) ≝72 definition io_inject : ∀O,I,A.∀P:A → Prop.∀a:option (IO O I A).∀p:P_to_P_option_io O I A P a.IO O I (Sig A P) ≝ 74 73 λO,I,A.λP:A → Prop.λa:option (IO O I A).λp:P_to_P_option_io O I A P a. 75 (match a return λa'.P_to_P_option_io O I A P a' → IO O I ( sigmaA P) with74 (match a return λa'.P_to_P_option_io O I A P a' → IO O I (Sig A P) with 76 75 [ None ⇒ λp'.? 77 76  Some b ⇒ λp'. io_inject_0 O I A P b p' 78 77 ]) p. 79 nelim p'; nqed.78 elim p'; qed. 80 79 81 nlet rec io_eject O I (A:Type) (P: A → Prop) (a:IO O I (sigmaA P)) on a : IO O I A ≝80 let rec io_eject O I (A:Type[0]) (P: A → Prop) (a:IO O I (Sig A P)) on a : IO O I A ≝ 82 81 match a with 83 82 [ Wrong ⇒ Wrong ??? 84  Value b ⇒ match b with [ sig_introw p ⇒ Value ??? w]83  Value b ⇒ match b with [ dp w p ⇒ Value ??? w] 85 84  Interact out k ⇒ Interact ??? out (λv. io_eject ?? A P (k v)) 86 85 ]. 87 86 88 ncoercion io_inject :89 ∀O,I,A.∀P:A → Prop.∀a.∀p:P_to_P_option_io O I ? P a.IO O I ( sigmaA P) ≝ io_inject90 on a:option (IO ???) to IO ?? ( sigma? ?).91 ncoercion io_eject : ∀O,I,A.∀P:A → Prop.∀c:IO O I (sigmaA P).IO O I A ≝ io_eject92 on _c:IO ?? ( sigma? ?) to IO ???.87 coercion io_inject : 88 ∀O,I,A.∀P:A → Prop.∀a.∀p:P_to_P_option_io O I ? P a.IO O I (Sig A P) ≝ io_inject 89 on a:option (IO ???) to IO ?? (Sig ? ?). 90 coercion io_eject : ∀O,I,A.∀P:A → Prop.∀c:IO O I (Sig A P).IO O I A ≝ io_eject 91 on _c:IO ?? (Sig ? ?) to IO ???. 93 92 94 ndefinition opt_to_io : ∀O,I,T.option T → IO O I T ≝93 definition opt_to_io : ∀O,I,T.option T → IO O I T ≝ 95 94 λO,I,T,v. match v with [ None ⇒ Wrong ?? T  Some v' ⇒ Value ??? v' ]. 96 ncoercion opt_to_io : ∀O,I,T.∀v:option T. IO O I T ≝ opt_to_io on _v:option ? to IO ???.95 coercion opt_to_io : ∀O,I,T.∀v:option T. IO O I T ≝ opt_to_io on _v:option ? to IO ???. 97 96 98 nlemma sig_bindIO_OK: ∀O,I,A,B. ∀P:A → Prop. ∀P':B → Prop. ∀e:IO O I (sigma A P). ∀f:sigmaA P → IO O I B.99 (∀v:A. ∀p:P v. P_io O I ? P' (f ( sig_introA P v p))) →100 P_io O I ? P' (bindIO O I ( sigmaA P) B e f).101 #O I A B P P' e f; nelim e;102 ##[ #out k IH; #IH'; nwhd; #res; napply IH;//;103 ## #v0; nelim v0; #v Hv IH; nwhd; napply IH; 104 ## //;105 ##] nqed.97 lemma sig_bindIO_OK: ∀O,I,A,B. ∀P:A → Prop. ∀P':B → Prop. ∀e:IO O I (Sig A P). ∀f:Sig A P → IO O I B. 98 (∀v:A. ∀p:P v. P_io O I ? P' (f (dp A P v p))) → 99 P_io O I ? P' (bindIO O I (Sig A P) B e f). 100 #O #I #A #B #P #P' #e #f elim e; 101 [ #out #k #IH #IH' whd; #res @IH //; 102  #v0 elim v0; #v #Hv #IH whd; @IH 103  //; 104 ] qed. 106 105 107 nlemma sig_bindIO2_OK: ∀O,I,A,B,C. ∀P:(A×B) → Prop. ∀P':C → Prop. ∀e:IO O I (sigma(A×B) P). ∀f: A → B → IO O I C.106 lemma sig_bindIO2_OK: ∀O,I,A,B,C. ∀P:(A×B) → Prop. ∀P':C → Prop. ∀e:IO O I (Sig (A×B) P). ∀f: A → B → IO O I C. 108 107 (∀vA:A.∀vB:B. ∀p:P 〈vA,vB〉. P_io O I ? P' (f vA vB)) → 109 108 P_io O I ? P' (bindIO2 O I A B C e f). 110 #I O A B C P P' e f; nelim e;111 ##[ #out k IH; #IH'; nwhd; #res; napply IH; napply IH'; 112 ## #v0; nelim v0; #v; nelim v; #vA vB Hv IH; napply IH;//;113 ## //;114 ##] nqed.109 #I #O #A #B #C #P #P' #e #f elim e; 110 [ #out #k #IH #IH' whd; #res @IH @IH' 111  #v0 elim v0; #v elim v; #vA #vB #Hv #IH @IH //; 112  //; 113 ] qed. 115 114 116 nlemma opt_bindIO_OK: ∀O,I,A,B. ∀P:B → Prop. ∀e:option A. ∀f: A → IO O I B.115 lemma opt_bindIO_OK: ∀O,I,A,B. ∀P:B → Prop. ∀e:option A. ∀f: A → IO O I B. 117 116 (∀v:A. e = Some A v → P_io O I ? P (f v)) → 118 117 P_io O I ? P (bindIO O I A B e f). 119 #I O A B P e; nelim e; //; #v f H; napply H;//;120 nqed.118 #I #O #A #B #P #e elim e; //; #v #f #H @H //; 119 qed. 121 120 122 nlemma opt_bindIO2_OK: ∀O,I,A,B,C. ∀P:C → Prop. ∀e:option (A×B). ∀f: A → B → IO O I C.121 lemma opt_bindIO2_OK: ∀O,I,A,B,C. ∀P:C → Prop. ∀e:option (A×B). ∀f: A → B → IO O I C. 123 122 (∀vA:A.∀vB:B. e = Some (A×B) 〈vA,vB〉 → P_io O I ? P (f vA vB)) → 124 123 P_io O I ? P (bindIO2 O I A B C e f). 125 #I O A B C P e; nelim e; //; #v; ncases v; #vA vB f H; napply H;//;126 nqed.124 #I #O #A #B #C #P #e elim e; //; #v cases v; #vA #vB #f #H @H //; 125 qed. 127 126 128 nlemma res_bindIO_OK: ∀O,I,A,B. ∀P:B → Prop. ∀e:res A. ∀f: A → IO O I B.127 lemma res_bindIO_OK: ∀O,I,A,B. ∀P:B → Prop. ∀e:res A. ∀f: A → IO O I B. 129 128 (∀v:A. e = OK A v → P_io O I ? P (f v)) → 130 129 P_io O I ? P (bindIO O I A B e f). 131 #I O A B P e; nelim e; //; #v f H; napply H;//;132 nqed.130 #I #O #A #B #P #e elim e; //; #v #f #H @H //; 131 qed. 133 132 134 nlemma res_bindIO2_OK: ∀O,I,A,B,C. ∀P:C → Prop. ∀e:res (A×B). ∀f: A → B → IO O I C.133 lemma res_bindIO2_OK: ∀O,I,A,B,C. ∀P:C → Prop. ∀e:res (A×B). ∀f: A → B → IO O I C. 135 134 (∀vA:A.∀vB:B. e = OK (A×B) 〈vA,vB〉 → P_io O I ? P (f vA vB)) → 136 135 P_io O I ? P (bindIO2 O I A B C e f). 137 #I O A B C P e; nelim e; //; #v; ncases v; #vA vB f H; napply H;//;138 nqed.136 #I #O #A #B #C #P #e elim e; //; #v cases v; #vA #vB #f #H @H //; 137 qed. 139 138 140 nlemma bindIO_OK: ∀O,I,A,B. ∀P:B → Prop. ∀e:IO O I A. ∀f: A → IO O I B.139 lemma bindIO_OK: ∀O,I,A,B. ∀P:B → Prop. ∀e:IO O I A. ∀f: A → IO O I B. 141 140 (∀v:A. P_io O I ? P (f v)) → 142 141 P_io O I ? P (bindIO O I A B e f). 143 #I O A B P e; nelim e;144 ##[ #out k IH; #f H; nwhd; #res; napply IH;//;145 ## #v f H; napply H; 146 ## //;147 ##] nqed.142 #I #O #A #B #P #e elim e; 143 [ #out #k #IH #f #H whd; #res @IH //; 144  #v #f #H @H 145  //; 146 ] qed. 148 147 149 nlemma bindIO2_OK: ∀O,I,A,B,C. ∀P:C → Prop. ∀e:IO O I (A×B). ∀f: A → B → IO O I C.148 lemma bindIO2_OK: ∀O,I,A,B,C. ∀P:C → Prop. ∀e:IO O I (A×B). ∀f: A → B → IO O I C. 150 149 (∀v1:A.∀v2:B. P_io O I ? P (f v1 v2)) → 151 150 P_io O I ? P (bindIO2 O I A B C e f). 152 #I O A B C P e; nelim e;153 ##[ #out k IH; #f H; nwhd; #res; napply IH;//;154 ## #v; ncases v; #v1 v2 f H; napply H; 155 ## //;156 ##] nqed.151 #I #O #A #B #C #P #e elim e; 152 [ #out #k #IH #f #H whd; #res @IH //; 153  #v cases v; #v1 #v2 #f #H @H 154  //; 155 ] qed. 157 156 158 nlemma P_bindIO_OK: ∀O,I,A,B. ∀P':A → Prop. ∀P:B → Prop. ∀e:IO O I A. ∀f: A → IO O I B.157 lemma P_bindIO_OK: ∀O,I,A,B. ∀P':A → Prop. ∀P:B → Prop. ∀e:IO O I A. ∀f: A → IO O I B. 159 158 P_io … P' e → 160 159 (∀v:A. P' v → P_io O I ? P (f v)) → 161 160 P_io O I ? P (bindIO O I A B e f). 162 #I O A B P' P e; nelim e;163 ##[ #out k IH f He H; nwhd in He ⊢ %; #res; napply IH;/2/;164 ## #v f He H; napply H; napply He; 165 ## //;166 ##] nqed.161 #I #O #A #B #P' #P #e elim e; 162 [ #out #k #IH #f #He #H whd in He ⊢ %; #res @IH /2/; 163  #v #f #He #H @H @He 164  //; 165 ] qed. 167 166 168 nlemma P_bindIO2_OK: ∀O,I,A,B,C. ∀P':A×B → Prop. ∀P:C → Prop. ∀e:IO O I (A×B). ∀f: A → B → IO O I C.167 lemma P_bindIO2_OK: ∀O,I,A,B,C. ∀P':A×B → Prop. ∀P:C → Prop. ∀e:IO O I (A×B). ∀f: A → B → IO O I C. 169 168 P_io … P' e → 170 169 (∀v1:A.∀v2:B. P' 〈v1,v2〉 → P_io O I ? P (f v1 v2)) → 171 170 P_io O I ? P (bindIO2 O I A B C e f). 172 #I O A B C P' P e; nelim e;173 ##[ #out k IH f He H; nwhd in He ⊢ %; #res; napply IH;/2/;174 ## #v; ncases v; #v1 v2 f He H; napply H; napply He; 175 ## //;176 ##] nqed.171 #I #O #A #B #C #P' #P #e elim e; 172 [ #out #k #IH #f #He #H whd in He ⊢ %; #res @IH /2/; 173  #v cases v; #v1 #v2 #f #He #H @H @He 174  //; 175 ] qed. 177 176 178 177 179 178 (* Is there a way to prove this without extensionality? *) 180 179 181 nlemma bind_assoc_r: ∀O,I,A,B,C,e,f,g.182 ∀ext:(∀T1,T2:Type .∀f,f':T1 → T2.(∀x.f x = f' x) → f = f').180 lemma bind_assoc_r: ∀O,I,A,B,C,e,f,g. 181 ∀ext:(∀T1,T2:Type[0].∀f,f':T1 → T2.(∀x.f x = f' x) → f = f'). 183 182 bindIO O I B C (bindIO O I A B e f) g = bindIO O I A C e (λx.bindIO O I B C (f x) g). 184 #O I A B C e f g ext; nelim e;185 ##[ #o k IH; nwhd in ⊢ (??%%); napply eq_f; 186 napply ext; napply IH;187 ## #v; napply refl; 188 ## napply refl; 189 ##] nqed.183 #O #I #A #B #C #e #f #g #ext elim e; 184 [ #o #k #IH whd in ⊢ (??%%); @eq_f 185 @ext @IH 186  #v @refl 187  @refl 188 ] qed. 190 189 191 192 nlemma extract_subset_pair_io: ∀O,I,A,B,C,P. ∀e:{e:A×B  P e}. ∀Q:A→B→IO O I C. ∀R:C→Prop.190 (* 191 lemma extract_subset_pair_io: ∀O,I,A,B,C,P. ∀e:{e:A×B  P e}. ∀Q:A→B→IO O I C. ∀R:C→Prop. 193 192 (∀a,b. eject ?? e = 〈a,b〉 → P 〈a,b〉 → P_io O I ? R (Q a b)) → 194 P_io O I ? R (match eject ?? e with [ mk_pair a b ⇒ Q a b ]).195 #I O A B C P e Q R; ncases e; #e'; ncases e'; nnormalize;196 ##[ *;197 ## #e''; ncases e''; #a b Pab H; nnormalize; /2/;198 ##] nqed.199 193 P_io O I ? R (match eject ?? e with [ pair a b ⇒ Q a b ]). 194 #I #O #A #B #C #P #e #Q #R cases e; #e' cases e'; normalize; 195 [ *; 196  #e'' cases e''; #a #b #Pab #H normalize; /2/; 197 ] qed. 198 *)
Note: See TracChangeset
for help on using the changeset viewer.