source: src/common/IOMonad.ma @ 1709

Last change on this file since 1709 was 1647, checked in by tranquil, 9 years ago
  • corrected some notation problems
  • adapted Cligth with slight modifications to new monad framework
File size: 7.6 KB
RevLine 
[700]1include "utilities/extralib.ma".
2include "common/Errors.ma".
[24]3
4(* IO monad *)
5
[487]6inductive 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
[1640]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
[487]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' ≝
[24]17match v with
[366]18[ Interact out k ⇒ (Interact ??? out (λres. bindIO O I T T' (k res) f))
[24]19| Value v' ⇒ (f v')
[797]20| Wrong m ⇒ Wrong O I T' m
[24]21].
22
[1640]23include "utilities/monad.ma".
[24]24
[1640]25definition IOMonad ≝ λO,I.
[1647]26  MakeMonadProps
[1640]27  (* the monad *)
28  (IO O I)
29  (* return *)
30  (λX.Value … X)
31  (* bind *)
32  (bindIO O I)
[1647]33  ???. / by /
34[(* bind_ret *)
35 #X#m elim m normalize // #o#f#Hi @interact_proper //
36|(* bind_bind *)
[1640]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;
[1647]48  N ≟ IOMonad O I, M ≟ max_def N, M' ≟ m_def M
[1640]49(*******************************************) ⊢
[1647]50  IO O I T ≡ monad M' T
[1640]51.
52
53
[487]54definition err_to_io : ∀O,I,T. res T → IO O I T ≝
[797]55λO,I,T,v. match v with [ OK v' ⇒ Value O I T v' | Error m ⇒ Wrong O I T m ].
[1640]56
[487]57coercion err_to_io : ∀O,I,A.∀c:res A.IO O I A ≝ err_to_io on _c:res ? to IO ???.
[1640]58
[487]59definition err_to_io_sig : ∀O,I,T.∀P:T → Prop. res (Sig T P) → IO O I (Sig T P) ≝
[797]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 ].
[487]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 ??).*)
[24]62
[487]63let rec P_io O I (A:Type[0]) (P:A → Prop) (v:IO O I A) on v : Prop ≝
[24]64match v return λ_.Prop with
[797]65[ Wrong _ ⇒ True
[24]66| Value z ⇒ P z
[366]67| Interact out k ⇒ ∀v'.P_io O I A P (k v')
[24]68].
69
[487]70let rec P_io' O I (A:Type[0]) (P:A → Prop) (v:IO O I A) on v : Prop ≝
[24]71match v return λ_.Prop with
[797]72[ Wrong _ ⇒ False
[24]73| Value z ⇒ P z
[366]74| Interact out k ⇒ ∀v'.P_io' O I A P (k v')
[24]75].
76
[487]77definition P_to_P_option_io : ∀O,I,A.∀P:A → Prop.option (IO O I A) → Prop ≝
[366]78  λO,I,A,P,a.match a with
[24]79   [ None ⇒ False
[366]80   | Some y ⇒ P_io O I A P y
[24]81   ].
82
[487]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) ≝
[366]84(match a return λa'.P_io O I A P a' → ? with
[797]85 [ Wrong m ⇒ λ_. Wrong O I ? m
[1601]86 | Value c ⇒ λp'. Value ??? (mk_Sig A P c p')
[366]87 | Interact out k ⇒ λp'. Interact ??? out (λv. io_inject_0 O I A P (k v) (p' v))
[211]88 ]) p.
[24]89
[487]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) ≝
[366]91  λO,I,A.λP:A → Prop.λa:option (IO O I A).λp:P_to_P_option_io O I A P a.
[487]92  (match a return λa'.P_to_P_option_io O I A P a' → IO O I (Sig A P) with
[211]93   [ None ⇒ λp'.?
[366]94   | Some b ⇒ λp'. io_inject_0 O I A P b p'
[211]95   ]) p.
[487]96elim p'; qed.
[24]97
[487]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 ≝
[24]99match a with
[797]100[ Wrong m ⇒ Wrong ??? m
[1601]101| Value b ⇒ match b with [ mk_Sig w p ⇒ Value ??? w]
[25]102| Interact out k ⇒ Interact ??? out (λv. io_eject ?? A P (k v))
[24]103].
104
[487]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 ???.
[24]110
[797]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' ].
[24]113
[487]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.
[1601]115  (∀v:A. ∀p:P v. P_io O I ? P' (f (mk_Sig A P v p))) →
[487]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.
[24]122
[487]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.
[366]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).
[487]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.
[24]131
[797]132lemma opt_bindIO_OK: ∀O,I,A,B,m. ∀P:B → Prop. ∀e:option A. ∀f: A → IO O I B.
[366]133  (∀v:A. e = Some A v → P_io O I ? P (f v)) →
[797]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 //;
[487]136qed.
[24]137
[797]138lemma opt_bindIO2_OK: ∀O,I,A,B,C,m. ∀P:C → Prop. ∀e:option (A×B). ∀f: A → B → IO O I C.
[366]139  (∀vA:A.∀vB:B. e = Some (A×B) 〈vA,vB〉 → P_io O I ? P (f vA vB)) →
[797]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 //;
[487]142qed.
[125]143
[487]144lemma res_bindIO_OK: ∀O,I,A,B. ∀P:B → Prop. ∀e:res A. ∀f: A → IO O I B.
[366]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).
[487]147#I #O #A #B #P #e elim e; //; #v #f #H @H //;
148qed.
[251]149
[487]150lemma res_bindIO2_OK: ∀O,I,A,B,C. ∀P:C → Prop. ∀e:res (A×B). ∀f: A → B → IO O I C.
[366]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).
[487]153#I #O #A #B #C #P #e elim e; //; #v cases v; #vA #vB #f #H @H //;
154qed.
[251]155
[487]156lemma bindIO_OK: ∀O,I,A,B. ∀P:B → Prop. ∀e:IO O I A. ∀f: A → IO O I B.
[366]157  (∀v:A. P_io O I ? P (f v)) →
158  P_io O I ? P (bindIO O I A B e f).
[487]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.
[24]164
[487]165lemma bindIO2_OK: ∀O,I,A,B,C. ∀P:C → Prop. ∀e:IO O I (A×B). ∀f: A → B → IO O I C.
[366]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).
[487]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.
[252]173
[487]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.
[252]175  P_io … P' e →
[366]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).
[487]178#I #O #A #B #P' #P #e elim e;
[1647]179[ #out #k #IH #f #He #H whd in He ⊢ %; #res @IH /2 by /;
[487]180| #v #f #He #H @H @He
181| //;
182] qed.
[252]183
[487]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.
[252]185  P_io … P' e →
[366]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).
[487]188#I #O #A #B #C #P' #P #e elim e;
[1647]189[ #out #k #IH #f #He #H whd in He ⊢ %; #res @IH /2 by /;
[487]190| #v cases v; #v1 #v2 #f #He #H @H @He
191| //;
192] qed.
[252]193
194
[411]195(* Is there a way to prove this without extensionality? *)
[1640]196(*
[487]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').
[411]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).
[487]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
[797]204| #m @refl
[487]205] qed.
[1640]206*)
207definition bind_assoc_r ≝ λO,I.m_bind_bind (IOMonad O I).
[24]208
[487]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.
[366]211  (∀a,b. eject ?? e = 〈a,b〉 → P 〈a,b〉 → P_io O I ? R (Q a b)) →
[487]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[ *;
[1647]215| #e'' cases e''; #a #b #Pab #H normalize; /2 by _/;
[487]216] qed.
217*)
Note: See TracBrowser for help on using the repository browser.