source: src/common/IOMonad.ma @ 1651

Last change on this file since 1651 was 1647, checked in by tranquil, 8 years ago
  • corrected some notation problems
  • adapted Cligth with slight modifications to new monad framework
File size: 7.6 KB
Line 
1include "utilities/extralib.ma".
2include "common/Errors.ma".
3
4(* IO monad *)
5
6inductive IO (output:Type[0]) (input:output → Type[0]) (T:Type[0]) : Type[0] ≝
7| Interact : ∀o:output. (input o → IO output input T) → IO output input T
8| Value : T → IO output input T
9| Wrong : errmsg → IO output input T.
10
11include "utilities/proper.ma".
12(* a weak form of extensionality *)
13axiom interact_proper :
14  ∀O,I,T,o.∀f,g : I o → IO O I T.(∀i. f i = g i) → Interact … o f = Interact … o g.
15
16let 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' ≝
17match v with
18[ Interact out k ⇒ (Interact ??? out (λres. bindIO O I T T' (k res) f))
19| Value v' ⇒ (f v')
20| Wrong m ⇒ Wrong O I T' m
21].
22
23include "utilities/monad.ma".
24
25definition IOMonad ≝ λO,I.
26  MakeMonadProps
27  (* the monad *)
28  (IO O I)
29  (* return *)
30  (λX.Value … X)
31  (* bind *)
32  (bindIO O I)
33  ???. / by /
34[(* bind_ret *)
35 #X#m elim m normalize // #o#f#Hi @interact_proper //
36|(* bind_bind *)
37 #X#Y#Z#m#f#g elim m normalize [2,3://]
38 (* Interact *)
39  #o#f #Hi @interact_proper //
40]
41qed.
42
43definition bindIO2 ≝ λO,I. m_bind2 (IOMonad O I).
44
45include "hints_declaration.ma".
46
47unification hint 0 ≔ O, I, T;
48  N ≟ IOMonad O I, M ≟ max_def N, M' ≟ m_def M
49(*******************************************) ⊢
50  IO O I T ≡ monad M' T
51.
52
53
54definition err_to_io : ∀O,I,T. res T → IO O I T ≝
55λO,I,T,v. match v with [ OK v' ⇒ Value O I T v' | Error m ⇒ Wrong O I T m ].
56
57coercion err_to_io : ∀O,I,A.∀c:res A.IO O I A ≝ err_to_io on _c:res ? to IO ???.
58
59definition err_to_io_sig : ∀O,I,T.∀P:T → Prop. res (Sig T P) → IO O I (Sig T P) ≝
60λ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 ].
61(*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 ??).*)
62
63let rec P_io O I (A:Type[0]) (P:A → Prop) (v:IO O I A) on v : Prop ≝
64match v return λ_.Prop with
65[ Wrong _ ⇒ True
66| Value z ⇒ P z
67| Interact out k ⇒ ∀v'.P_io O I A P (k v')
68].
69
70let rec P_io' O I (A:Type[0]) (P:A → Prop) (v:IO O I A) on v : Prop ≝
71match v return λ_.Prop with
72[ Wrong _ ⇒ False
73| Value z ⇒ P z
74| Interact out k ⇒ ∀v'.P_io' O I A P (k v')
75].
76
77definition P_to_P_option_io : ∀O,I,A.∀P:A → Prop.option (IO O I A) → Prop ≝
78  λO,I,A,P,a.match a with
79   [ None ⇒ False
80   | Some y ⇒ P_io O I A P y
81   ].
82
83let 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) ≝
84(match a return λa'.P_io O I A P a' → ? with
85 [ Wrong m ⇒ λ_. Wrong O I ? m
86 | Value c ⇒ λp'. Value ??? (mk_Sig A P c p')
87 | Interact out k ⇒ λp'. Interact ??? out (λv. io_inject_0 O I A P (k v) (p' v))
88 ]) p.
89
90definition 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) ≝
91  λO,I,A.λP:A → Prop.λa:option (IO O I A).λp:P_to_P_option_io O I A P a.
92  (match a return λa'.P_to_P_option_io O I A P a' → IO O I (Sig A P) with
93   [ None ⇒ λp'.?
94   | Some b ⇒ λp'. io_inject_0 O I A P b p'
95   ]) p.
96elim p'; qed.
97
98let rec io_eject O I (A:Type[0]) (P: A → Prop) (a:IO O I (Sig A P)) on a : IO O I A ≝
99match a with
100[ Wrong m ⇒ Wrong ??? m
101| Value b ⇒ match b with [ mk_Sig w p ⇒ Value ??? w]
102| Interact out k ⇒ Interact ??? out (λv. io_eject ?? A P (k v))
103].
104
105coercion io_inject :
106  ∀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
107  on a:option (IO ???) to IO ?? (Sig ? ?).
108coercion io_eject : ∀O,I,A.∀P:A → Prop.∀c:IO O I (Sig A P).IO O I A ≝ io_eject
109  on _c:IO ?? (Sig ? ?) to IO ???.
110
111definition opt_to_io : ∀O,I,T.errmsg → option T → IO O I T ≝
112λO,I,T,m,v. match v with [ None ⇒ Wrong ?? T m | Some v' ⇒ Value ??? v' ].
113
114lemma 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.
115  (∀v:A. ∀p:P v. P_io O I ? P' (f (mk_Sig A P v p))) →
116  P_io O I ? P' (bindIO O I (Sig A P) B e f).
117#O #I #A #B #P #P' #e #f elim e;
118[ #out #k #IH #IH' whd; #res @IH //;
119| #v0 elim v0; #v #Hv #IH whd; @IH
120| //;
121] qed.
122
123lemma 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.
124  (∀vA:A.∀vB:B. ∀p:P 〈vA,vB〉. P_io O I ? P' (f vA vB)) →
125  P_io O I ? P' (bindIO2 O I A B C e f).
126#I #O #A #B #C #P #P' #e #f elim e;
127[ #out #k #IH #IH' whd; #res @IH @IH'
128| #v0 elim v0; #v elim v; #vA #vB #Hv #IH @IH //;
129| //;
130] qed.
131
132lemma opt_bindIO_OK: ∀O,I,A,B,m. ∀P:B → Prop. ∀e:option A. ∀f: A → IO O I B.
133  (∀v:A. e = Some A v → P_io O I ? P (f v)) →
134  P_io O I ? P (bindIO O I A B (opt_to_io ??? m e) f).
135#I #O #A #B #m #P #e elim e; //; #v #f #H @H //;
136qed.
137
138lemma opt_bindIO2_OK: ∀O,I,A,B,C,m. ∀P:C → Prop. ∀e:option (A×B). ∀f: A → B → IO O I C.
139  (∀vA:A.∀vB:B. e = Some (A×B) 〈vA,vB〉 → P_io O I ? P (f vA vB)) →
140  P_io O I ? P (bindIO2 O I A B C (opt_to_io ??? m e) f).
141#I #O #A #B #C #m #P #e elim e; //; #v cases v; #vA #vB #f #H @H //;
142qed.
143
144lemma res_bindIO_OK: ∀O,I,A,B. ∀P:B → Prop. ∀e:res A. ∀f: A → IO O I B.
145  (∀v:A. e = OK A v → P_io O I ? P (f v)) →
146  P_io O I ? P (bindIO O I A B e f).
147#I #O #A #B #P #e elim e; //; #v #f #H @H //;
148qed.
149
150lemma res_bindIO2_OK: ∀O,I,A,B,C. ∀P:C → Prop. ∀e:res (A×B). ∀f: A → B → IO O I C.
151  (∀vA:A.∀vB:B. e = OK (A×B) 〈vA,vB〉 → P_io O I ? P (f vA vB)) →
152  P_io O I ? P (bindIO2 O I A B C e f).
153#I #O #A #B #C #P #e elim e; //; #v cases v; #vA #vB #f #H @H //;
154qed.
155
156lemma bindIO_OK: ∀O,I,A,B. ∀P:B → Prop. ∀e:IO O I A. ∀f: A → IO O I B.
157  (∀v:A. P_io O I ? P (f v)) →
158  P_io O I ? P (bindIO O I A B e f).
159#I #O #A #B #P #e elim e;
160[ #out #k #IH #f #H whd; #res @IH //;
161| #v #f #H @H
162| //;
163] qed.
164
165lemma bindIO2_OK: ∀O,I,A,B,C. ∀P:C → Prop. ∀e:IO O I (A×B). ∀f: A → B → IO O I C.
166  (∀v1:A.∀v2:B. P_io O I ? P (f v1 v2)) →
167  P_io O I ? P (bindIO2 O I A B C e f).
168#I #O #A #B #C #P #e elim e;
169[ #out #k #IH #f #H whd; #res @IH //;
170| #v cases v; #v1 #v2 #f #H @H
171| //;
172] qed.
173
174lemma P_bindIO_OK: ∀O,I,A,B. ∀P':A → Prop. ∀P:B → Prop. ∀e:IO O I A. ∀f: A → IO O I B.
175  P_io … P' e →
176  (∀v:A. P' v → P_io O I ? P (f v)) →
177  P_io O I ? P (bindIO O I A B e f).
178#I #O #A #B #P' #P #e elim e;
179[ #out #k #IH #f #He #H whd in He ⊢ %; #res @IH /2 by /;
180| #v #f #He #H @H @He
181| //;
182] qed.
183
184lemma 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.
185  P_io … P' e →
186  (∀v1:A.∀v2:B. P' 〈v1,v2〉 → P_io O I ? P (f v1 v2)) →
187  P_io O I ? P (bindIO2 O I A B C e f).
188#I #O #A #B #C #P' #P #e elim e;
189[ #out #k #IH #f #He #H whd in He ⊢ %; #res @IH /2 by /;
190| #v cases v; #v1 #v2 #f #He #H @H @He
191| //;
192] qed.
193
194
195(* Is there a way to prove this without extensionality? *)
196(*
197lemma bind_assoc_r: ∀O,I,A,B,C,e,f,g.
198  ∀ext:(∀T1,T2:Type[0].∀f,f':T1 → T2.(∀x.f x = f' x) → f = f').
199  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).
200#O #I #A #B #C #e #f #g #ext elim e;
201[ #o #k #IH whd in ⊢ (??%%); @eq_f
202    @ext @IH
203| #v @refl
204| #m @refl
205] qed.
206*)
207definition bind_assoc_r ≝ λO,I.m_bind_bind (IOMonad O I).
208
209(*
210lemma 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.
211  (∀a,b. eject ?? e = 〈a,b〉 → P 〈a,b〉 → P_io O I ? R (Q a b)) →
212  P_io O I ? R (match eject ?? e with [ pair a b ⇒ Q a b ]).
213#I #O #A #B #C #P #e #Q #R cases e; #e' cases e'; normalize;
214[ *;
215| #e'' cases e''; #a #b #Pab #H normalize; /2 by _/;
216] qed.
217*)
Note: See TracBrowser for help on using the repository browser.