[700] | 1 | include "utilities/extralib.ma". |
---|
| 2 | include "common/Errors.ma". |
---|
[24] | 3 | |
---|
| 4 | (* IO monad *) |
---|
| 5 | |
---|
[487] | 6 | inductive IO (output:Type[0]) (input:output → Type[0]) (T:Type[0]) : Type[0] ≝ |
---|
[366] | 7 | | Interact : ∀o:output. (input o → IO output input T) → IO output input T |
---|
| 8 | | Value : T → IO output input T |
---|
[797] | 9 | | Wrong : errmsg → IO output input T. |
---|
[24] | 10 | |
---|
[487] | 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' ≝ |
---|
[24] | 12 | match v with |
---|
[366] | 13 | [ Interact out k ⇒ (Interact ??? out (λres. bindIO O I T T' (k res) f)) |
---|
[24] | 14 | | Value v' ⇒ (f v') |
---|
[797] | 15 | | Wrong m ⇒ Wrong O I T' m |
---|
[24] | 16 | ]. |
---|
| 17 | |
---|
[487] | 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' ≝ |
---|
[24] | 19 | match v with |
---|
[25] | 20 | [ Interact out k ⇒ (Interact ??? out (λres. bindIO2 ?? T1 T2 T' (k res) f)) |
---|
[487] | 21 | | Value v' ⇒ match v' with [ pair v1 v2 ⇒ f v1 v2 ] |
---|
[797] | 22 | | Wrong m ⇒ Wrong ?? T' m |
---|
[24] | 23 | ]. |
---|
| 24 | |
---|
[487] | 25 | definition err_to_io : ∀O,I,T. res T → IO O I T ≝ |
---|
[797] | 26 | λO,I,T,v. match v with [ OK v' ⇒ Value O I T v' | Error m ⇒ Wrong O I T m ]. |
---|
[487] | 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) ≝ |
---|
[797] | 29 | λO,I,T,P,v. match v with [ OK v' ⇒ Value O I (Sig T P) v' | Error m ⇒ Wrong O I (Sig T P) m ]. |
---|
[487] | 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 ??).*) |
---|
[24] | 31 | |
---|
| 32 | |
---|
| 33 | (* If the original definitions are vague enough, do I need to do this? *) |
---|
[208] | 34 | notation > "! ident v ← e; e'" with precedence 40 for @{'bindIO ${e} (λ${ident v}.${e'})}. |
---|
| 35 | notation > "! ident v : ty ← e; e'" with precedence 40 for @{'bindIO ${e} (λ${ident v} : ${ty}.${e'})}. |
---|
| 36 | notation < "vbox(! \nbsp ident v ← e; break e')" with precedence 40 for @{'bindIO ${e} (λ${ident v}.${e'})}. |
---|
| 37 | notation < "vbox(! \nbsp ident v : ty ← e; break e')" with precedence 40 for @{'bindIO ${e} (λ${ident v} : ${ty}.${e'})}. |
---|
| 38 | notation > "! 〈ident v1, ident v2〉 ← e; e'" with precedence 40 for @{'bindIO2 ${e} (λ${ident v1}.λ${ident v2}.${e'})}. |
---|
| 39 | notation > "! 〈ident v1 : ty1, ident v2 : ty2〉 ← e; e'" with precedence 40 for @{'bindIO2 ${e} (λ${ident v1} : ${ty1}.λ${ident v2} : ${ty2}.${e'})}. |
---|
| 40 | notation < "vbox(! \nbsp 〈ident v1, ident v2〉 ← e; break e')" with precedence 40 for @{'bindIO2 ${e} (λ${ident v1}.λ${ident v2}.${e'})}. |
---|
| 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'})}. |
---|
[25] | 42 | interpretation "IO monad bind" 'bindIO e f = (bindIO ???? e f). |
---|
[487] | 43 | interpretation "IO monad Prod bind" 'bindIO2 e f = (bindIO2 ????? e f). |
---|
[24] | 44 | (**) |
---|
[487] | 45 | let rec P_io O I (A:Type[0]) (P:A → Prop) (v:IO O I A) on v : Prop ≝ |
---|
[24] | 46 | match v return λ_.Prop with |
---|
[797] | 47 | [ Wrong _ ⇒ True |
---|
[24] | 48 | | Value z ⇒ P z |
---|
[366] | 49 | | Interact out k ⇒ ∀v'.P_io O I A P (k v') |
---|
[24] | 50 | ]. |
---|
| 51 | |
---|
[487] | 52 | let rec P_io' O I (A:Type[0]) (P:A → Prop) (v:IO O I A) on v : Prop ≝ |
---|
[24] | 53 | match v return λ_.Prop with |
---|
[797] | 54 | [ Wrong _ ⇒ False |
---|
[24] | 55 | | Value z ⇒ P z |
---|
[366] | 56 | | Interact out k ⇒ ∀v'.P_io' O I A P (k v') |
---|
[24] | 57 | ]. |
---|
| 58 | |
---|
[487] | 59 | definition P_to_P_option_io : ∀O,I,A.∀P:A → Prop.option (IO O I A) → Prop ≝ |
---|
[366] | 60 | λO,I,A,P,a.match a with |
---|
[24] | 61 | [ None ⇒ False |
---|
[366] | 62 | | Some y ⇒ P_io O I A P y |
---|
[24] | 63 | ]. |
---|
| 64 | |
---|
[487] | 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) ≝ |
---|
[366] | 66 | (match a return λa'.P_io O I A P a' → ? with |
---|
[797] | 67 | [ Wrong m ⇒ λ_. Wrong O I ? m |
---|
[487] | 68 | | Value c ⇒ λp'. Value ??? (dp A P c p') |
---|
[366] | 69 | | Interact out k ⇒ λp'. Interact ??? out (λv. io_inject_0 O I A P (k v) (p' v)) |
---|
[211] | 70 | ]) p. |
---|
[24] | 71 | |
---|
[487] | 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) ≝ |
---|
[366] | 73 | λO,I,A.λP:A → Prop.λa:option (IO O I A).λp:P_to_P_option_io O I A P a. |
---|
[487] | 74 | (match a return λa'.P_to_P_option_io O I A P a' → IO O I (Sig A P) with |
---|
[211] | 75 | [ None ⇒ λp'.? |
---|
[366] | 76 | | Some b ⇒ λp'. io_inject_0 O I A P b p' |
---|
[211] | 77 | ]) p. |
---|
[487] | 78 | elim p'; qed. |
---|
[24] | 79 | |
---|
[487] | 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 ≝ |
---|
[24] | 81 | match a with |
---|
[797] | 82 | [ Wrong m ⇒ Wrong ??? m |
---|
[487] | 83 | | Value b ⇒ match b with [ dp w p ⇒ Value ??? w] |
---|
[25] | 84 | | Interact out k ⇒ Interact ??? out (λv. io_eject ?? A P (k v)) |
---|
[24] | 85 | ]. |
---|
| 86 | |
---|
[487] | 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 ???. |
---|
[24] | 92 | |
---|
[797] | 93 | definition opt_to_io : ∀O,I,T.errmsg → option T → IO O I T ≝ |
---|
| 94 | λO,I,T,m,v. match v with [ None ⇒ Wrong ?? T m | Some v' ⇒ Value ??? v' ]. |
---|
[24] | 95 | |
---|
[487] | 96 | 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. |
---|
| 97 | (∀v:A. ∀p:P v. P_io O I ? P' (f (dp A P v p))) → |
---|
| 98 | P_io O I ? P' (bindIO O I (Sig A P) B e f). |
---|
| 99 | #O #I #A #B #P #P' #e #f elim e; |
---|
| 100 | [ #out #k #IH #IH' whd; #res @IH //; |
---|
| 101 | | #v0 elim v0; #v #Hv #IH whd; @IH |
---|
| 102 | | //; |
---|
| 103 | ] qed. |
---|
[24] | 104 | |
---|
[487] | 105 | 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. |
---|
[366] | 106 | (∀vA:A.∀vB:B. ∀p:P 〈vA,vB〉. P_io O I ? P' (f vA vB)) → |
---|
| 107 | P_io O I ? P' (bindIO2 O I A B C e f). |
---|
[487] | 108 | #I #O #A #B #C #P #P' #e #f elim e; |
---|
| 109 | [ #out #k #IH #IH' whd; #res @IH @IH' |
---|
| 110 | | #v0 elim v0; #v elim v; #vA #vB #Hv #IH @IH //; |
---|
| 111 | | //; |
---|
| 112 | ] qed. |
---|
[24] | 113 | |
---|
[797] | 114 | lemma opt_bindIO_OK: ∀O,I,A,B,m. ∀P:B → Prop. ∀e:option A. ∀f: A → IO O I B. |
---|
[366] | 115 | (∀v:A. e = Some A v → P_io O I ? P (f v)) → |
---|
[797] | 116 | P_io O I ? P (bindIO O I A B (opt_to_io ??? m e) f). |
---|
| 117 | #I #O #A #B #m #P #e elim e; //; #v #f #H @H //; |
---|
[487] | 118 | qed. |
---|
[24] | 119 | |
---|
[797] | 120 | lemma opt_bindIO2_OK: ∀O,I,A,B,C,m. ∀P:C → Prop. ∀e:option (A×B). ∀f: A → B → IO O I C. |
---|
[366] | 121 | (∀vA:A.∀vB:B. e = Some (A×B) 〈vA,vB〉 → P_io O I ? P (f vA vB)) → |
---|
[797] | 122 | P_io O I ? P (bindIO2 O I A B C (opt_to_io ??? m e) f). |
---|
| 123 | #I #O #A #B #C #m #P #e elim e; //; #v cases v; #vA #vB #f #H @H //; |
---|
[487] | 124 | qed. |
---|
[125] | 125 | |
---|
[487] | 126 | lemma res_bindIO_OK: ∀O,I,A,B. ∀P:B → Prop. ∀e:res A. ∀f: A → IO O I B. |
---|
[366] | 127 | (∀v:A. e = OK A v → P_io O I ? P (f v)) → |
---|
| 128 | P_io O I ? P (bindIO O I A B e f). |
---|
[487] | 129 | #I #O #A #B #P #e elim e; //; #v #f #H @H //; |
---|
| 130 | qed. |
---|
[251] | 131 | |
---|
[487] | 132 | lemma res_bindIO2_OK: ∀O,I,A,B,C. ∀P:C → Prop. ∀e:res (A×B). ∀f: A → B → IO O I C. |
---|
[366] | 133 | (∀vA:A.∀vB:B. e = OK (A×B) 〈vA,vB〉 → P_io O I ? P (f vA vB)) → |
---|
| 134 | P_io O I ? P (bindIO2 O I A B C e f). |
---|
[487] | 135 | #I #O #A #B #C #P #e elim e; //; #v cases v; #vA #vB #f #H @H //; |
---|
| 136 | qed. |
---|
[251] | 137 | |
---|
[487] | 138 | lemma bindIO_OK: ∀O,I,A,B. ∀P:B → Prop. ∀e:IO O I A. ∀f: A → IO O I B. |
---|
[366] | 139 | (∀v:A. P_io O I ? P (f v)) → |
---|
| 140 | P_io O I ? P (bindIO O I A B e f). |
---|
[487] | 141 | #I #O #A #B #P #e elim e; |
---|
| 142 | [ #out #k #IH #f #H whd; #res @IH //; |
---|
| 143 | | #v #f #H @H |
---|
| 144 | | //; |
---|
| 145 | ] qed. |
---|
[24] | 146 | |
---|
[487] | 147 | lemma bindIO2_OK: ∀O,I,A,B,C. ∀P:C → Prop. ∀e:IO O I (A×B). ∀f: A → B → IO O I C. |
---|
[366] | 148 | (∀v1:A.∀v2:B. P_io O I ? P (f v1 v2)) → |
---|
| 149 | P_io O I ? P (bindIO2 O I A B C e f). |
---|
[487] | 150 | #I #O #A #B #C #P #e elim e; |
---|
| 151 | [ #out #k #IH #f #H whd; #res @IH //; |
---|
| 152 | | #v cases v; #v1 #v2 #f #H @H |
---|
| 153 | | //; |
---|
| 154 | ] qed. |
---|
[252] | 155 | |
---|
[487] | 156 | 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. |
---|
[252] | 157 | P_io … P' e → |
---|
[366] | 158 | (∀v:A. P' v → P_io O I ? P (f v)) → |
---|
| 159 | P_io O I ? P (bindIO O I A B e f). |
---|
[487] | 160 | #I #O #A #B #P' #P #e elim e; |
---|
| 161 | [ #out #k #IH #f #He #H whd in He ⊢ %; #res @IH /2/; |
---|
| 162 | | #v #f #He #H @H @He |
---|
| 163 | | //; |
---|
| 164 | ] qed. |
---|
[252] | 165 | |
---|
[487] | 166 | 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. |
---|
[252] | 167 | P_io … P' e → |
---|
[366] | 168 | (∀v1:A.∀v2:B. P' 〈v1,v2〉 → P_io O I ? P (f v1 v2)) → |
---|
| 169 | P_io O I ? P (bindIO2 O I A B C e f). |
---|
[487] | 170 | #I #O #A #B #C #P' #P #e elim e; |
---|
| 171 | [ #out #k #IH #f #He #H whd in He ⊢ %; #res @IH /2/; |
---|
| 172 | | #v cases v; #v1 #v2 #f #He #H @H @He |
---|
| 173 | | //; |
---|
| 174 | ] qed. |
---|
[252] | 175 | |
---|
| 176 | |
---|
[411] | 177 | (* Is there a way to prove this without extensionality? *) |
---|
[24] | 178 | |
---|
[487] | 179 | lemma bind_assoc_r: ∀O,I,A,B,C,e,f,g. |
---|
| 180 | ∀ext:(∀T1,T2:Type[0].∀f,f':T1 → T2.(∀x.f x = f' x) → f = f'). |
---|
[411] | 181 | 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). |
---|
[487] | 182 | #O #I #A #B #C #e #f #g #ext elim e; |
---|
| 183 | [ #o #k #IH whd in ⊢ (??%%); @eq_f |
---|
| 184 | @ext @IH |
---|
| 185 | | #v @refl |
---|
[797] | 186 | | #m @refl |
---|
[487] | 187 | ] qed. |
---|
[24] | 188 | |
---|
[487] | 189 | (* |
---|
| 190 | 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. |
---|
[366] | 191 | (∀a,b. eject ?? e = 〈a,b〉 → P 〈a,b〉 → P_io O I ? R (Q a b)) → |
---|
[487] | 192 | P_io O I ? R (match eject ?? e with [ pair a b ⇒ Q a b ]). |
---|
| 193 | #I #O #A #B #C #P #e #Q #R cases e; #e' cases e'; normalize; |
---|
| 194 | [ *; |
---|
| 195 | | #e'' cases e''; #a #b #Pab #H normalize; /2/; |
---|
| 196 | ] qed. |
---|
| 197 | *) |
---|