Changeset 797 for src/common/IOMonad.ma


Ignore:
Timestamp:
May 13, 2011, 1:10:23 PM (9 years ago)
Author:
campbell
Message:

Add error messages wherever the error monad is used.
Sticks to CompCert? style strings+identifiers for the moment.
Use axioms for strings as we currently have no representation or literals
for them - still *very* useful for animation in the proof assistant.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/common/IOMonad.ma

    r700 r797  
    77| Interact : ∀o:output. (input o → IO output input T) → IO output input T
    88| Value : T → IO output input T
    9 | Wrong : IO output input T.
     9| Wrong : errmsg → IO output input T.
    1010
    1111let 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' ≝
     
    1313[ Interact out k ⇒ (Interact ??? out (λres. bindIO O I T T' (k res) f))
    1414| Value v' ⇒ (f v')
    15 | Wrong ⇒ Wrong O I T'
     15| Wrong m ⇒ Wrong O I T' m
    1616].
    1717
     
    2020[ Interact out k ⇒ (Interact ??? out (λres. bindIO2 ?? T1 T2 T' (k res) f))
    2121| Value v' ⇒ match v' with [ pair v1 v2 ⇒ f v1 v2 ]
    22 | Wrong ⇒ Wrong ?? T'
     22| Wrong m ⇒ Wrong ?? T' m
    2323].
    2424
    2525definition err_to_io : ∀O,I,T. res T → IO O I T ≝
    26 λO,I,T,v. match v with [ OK v' ⇒ Value O I T v' | Error ⇒ Wrong O I T ].
     26λO,I,T,v. match v with [ OK v' ⇒ Value O I T v' | Error m ⇒ Wrong O I T m ].
    2727coercion err_to_io : ∀O,I,A.∀c:res A.IO O I A ≝ err_to_io on _c:res ? to IO ???.
    2828definition 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) ].
     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 ].
    3030(*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 ??).*)
    3131
     
    4545let rec P_io O I (A:Type[0]) (P:A → Prop) (v:IO O I A) on v : Prop ≝
    4646match v return λ_.Prop with
    47 [ Wrong ⇒ True
     47[ Wrong _ ⇒ True
    4848| Value z ⇒ P z
    4949| Interact out k ⇒ ∀v'.P_io O I A P (k v')
     
    5252let rec P_io' O I (A:Type[0]) (P:A → Prop) (v:IO O I A) on v : Prop ≝
    5353match v return λ_.Prop with
    54 [ Wrong ⇒ False
     54[ Wrong _ ⇒ False
    5555| Value z ⇒ P z
    5656| Interact out k ⇒ ∀v'.P_io' O I A P (k v')
     
    6565let 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) ≝
    6666(match a return λa'.P_io O I A P a' → ? with
    67  [ Wrong ⇒ λ_. Wrong O I ?
     67 [ Wrong m ⇒ λ_. Wrong O I ? m
    6868 | Value c ⇒ λp'. Value ??? (dp A P c p')
    6969 | Interact out k ⇒ λp'. Interact ??? out (λv. io_inject_0 O I A P (k v) (p' v))
     
    8080let rec io_eject O I (A:Type[0]) (P: A → Prop) (a:IO O I (Sig A P)) on a : IO O I A ≝
    8181match a with
    82 [ Wrong ⇒ Wrong ???
     82[ Wrong m ⇒ Wrong ??? m
    8383| Value b ⇒ match b with [ dp w p ⇒ Value ??? w]
    8484| Interact out k ⇒ Interact ??? out (λv. io_eject ?? A P (k v))
     
    9191  on _c:IO ?? (Sig ? ?) to IO ???.
    9292
    93 definition opt_to_io : ∀O,I,T.option T → IO O I T ≝
    94 λO,I,T,v. match v with [ None ⇒ Wrong ?? T | Some v' ⇒ Value ??? v' ].
    95 coercion opt_to_io : ∀O,I,T.∀v:option T. IO O I T ≝ opt_to_io on _v:option ? to IO ???.
     93definition 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' ].
    9695
    9796lemma 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.
     
    113112] qed.
    114113
    115 lemma opt_bindIO_OK: ∀O,I,A,B. ∀P:B → Prop. ∀e:option A. ∀f: A → IO O I B.
     114lemma opt_bindIO_OK: ∀O,I,A,B,m. ∀P:B → Prop. ∀e:option A. ∀f: A → IO O I B.
    116115  (∀v:A. e = Some A v → P_io O I ? P (f v)) →
    117   P_io O I ? P (bindIO O I A B e f).
    118 #I #O #A #B #P #e elim e; //; #v #f #H @H //;
     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 //;
    119118qed.
    120119
    121 lemma opt_bindIO2_OK: ∀O,I,A,B,C. ∀P:C → Prop. ∀e:option (A×B). ∀f: A → B → IO O I C.
     120lemma opt_bindIO2_OK: ∀O,I,A,B,C,m. ∀P:C → Prop. ∀e:option (A×B). ∀f: A → B → IO O I C.
    122121  (∀vA:A.∀vB:B. e = Some (A×B) 〈vA,vB〉 → P_io O I ? P (f vA vB)) →
    123   P_io O I ? P (bindIO2 O I A B C e f).
    124 #I #O #A #B #C #P #e elim e; //; #v cases v; #vA #vB #f #H @H //;
     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 //;
    125124qed.
    126125
     
    185184    @ext @IH
    186185| #v @refl
    187 | @refl
     186| #m @refl
    188187] qed.
    189188
Note: See TracChangeset for help on using the changeset viewer.