Ignore:
Timestamp:
Feb 9, 2011, 11:49:17 AM (9 years ago)
Author:
campbell
Message:

Port Clight semantics to the new-new matita syntax.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • Deliverables/D3.1/C-semantics/IOMonad.ma

    r411 r487  
    1 include "Plogic/russell_support.ma".
    21include "extralib.ma".
    32include "Errors.ma".
     
    54(* IO monad *)
    65
    7 ninductive IO (output:Type) (input:output → Type) (T:Type) : Type
     6inductive IO (output:Type[0]) (input:output → Type[0]) (T:Type[0]) : Type[0]
    87| Interact : ∀o:output. (input o → IO output input T) → IO output input T
    98| Value : T → IO output input T
    109| Wrong : IO output input T.
    1110
    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' ≝
     11let 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' ≝
    1312match v with
    1413[ Interact out k ⇒ (Interact ??? out (λres. bindIO O I T T' (k res) f))
     
    1716].
    1817
    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' ≝
     18let 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' ≝
    2019match v with
    2120[ 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 ]
    2322| Wrong ⇒ Wrong ?? T'
    2423].
    2524
    26 ndefinition err_to_io : ∀O,I,T. res T → IO O I T ≝
     25definition err_to_io : ∀O,I,T. res T → IO O I T ≝
    2726λ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 (sigma T P) ≝
    30 λO,I,T,P,v. match v with [ OK v' ⇒ Value O I (sigma T P) v' | Error ⇒ Wrong O I (sigma T 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 ??).*)
     27coercion err_to_io : ∀O,I,A.∀c:res A.IO O I A ≝ err_to_io on _c:res ? to IO ???.
     28definition 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 ??).*)
    3231
    3332
     
    4241notation < "vbox(! \nbsp 〈ident v1 : ty1, ident v2 : ty2〉 ← e; break e')" with precedence 40 for @{'bindIO2 ${e} (λ${ident v1} : ${ty1}.λ${ident v2} : ${ty2}.${e'})}.
    4342interpretation "IO monad bind" 'bindIO e f = (bindIO ???? e f).
    44 interpretation "IO monad pair bind" 'bindIO2 e f = (bindIO2 ????? e f).
     43interpretation "IO monad Prod bind" 'bindIO2 e f = (bindIO2 ????? e f).
    4544(**)
    46 nlet rec P_io O I (A:Type) (P:A → Prop) (v:IO O I A) on v : Prop ≝
     45let rec P_io O I (A:Type[0]) (P:A → Prop) (v:IO O I A) on v : Prop ≝
    4746match v return λ_.Prop with
    4847[ Wrong ⇒ True
     
    5150].
    5251
    53 nlet rec P_io' O I (A:Type) (P:A → Prop) (v:IO O I A) on v : Prop ≝
     52let rec P_io' O I (A:Type[0]) (P:A → Prop) (v:IO O I A) on v : Prop ≝
    5453match v return λ_.Prop with
    5554[ Wrong ⇒ False
     
    5857].
    5958
    60 ndefinition P_to_P_option_io : ∀O,I,A.∀P:A → Prop.option (IO O I A) → Prop ≝
     59definition P_to_P_option_io : ∀O,I,A.∀P:A → Prop.option (IO O I A) → Prop ≝
    6160  λO,I,A,P,a.match a with
    6261   [ None ⇒ False
     
    6463   ].
    6564
    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 (sigma A P) ≝
     65let 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) ≝
    6766(match a return λa'.P_io O I A P a' → ? with
    6867 [ Wrong ⇒ λ_. Wrong O I ?
    69  | Value c ⇒ λp'. Value ??? (sig_intro A P c p')
     68 | Value c ⇒ λp'. Value ??? (dp A P c p')
    7069 | Interact out k ⇒ λp'. Interact ??? out (λv. io_inject_0 O I A P (k v) (p' v))
    7170 ]) p.
    7271
    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 (sigma A P) ≝
     72definition 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) ≝
    7473  λ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 (sigma A P) with
     74  (match a return λa'.P_to_P_option_io O I A P a' → IO O I (Sig A P) with
    7675   [ None ⇒ λp'.?
    7776   | Some b ⇒ λp'. io_inject_0 O I A P b p'
    7877   ]) p.
    79 nelim p'; nqed.
     78elim p'; qed.
    8079
    81 nlet rec io_eject O I (A:Type) (P: A → Prop) (a:IO O I (sigma A P)) on a : IO O I A ≝
     80let rec io_eject O I (A:Type[0]) (P: A → Prop) (a:IO O I (Sig A P)) on a : IO O I A ≝
    8281match a with
    8382[ Wrong ⇒ Wrong ???
    84 | Value b ⇒ match b with [ sig_intro w p ⇒ Value ??? w]
     83| Value b ⇒ match b with [ dp w p ⇒ Value ??? w]
    8584| Interact out k ⇒ Interact ??? out (λv. io_eject ?? A P (k v))
    8685].
    8786
    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 (sigma A P) ≝ io_inject
    90   on a:option (IO ???) to IO ?? (sigma ? ?).
    91 ncoercion io_eject : ∀O,I,A.∀P:A → Prop.∀c:IO O I (sigma A P).IO O I A ≝ io_eject
    92   on _c:IO ?? (sigma ? ?) to IO ???.
     87coercion 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 ? ?).
     90coercion 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 ???.
    9392
    94 ndefinition opt_to_io : ∀O,I,T.option T → IO O I T ≝
     93definition opt_to_io : ∀O,I,T.option T → IO O I T ≝
    9594λ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 ???.
     95coercion opt_to_io : ∀O,I,T.∀v:option T. IO O I T ≝ opt_to_io on _v:option ? to IO ???.
    9796
    98 nlemma sig_bindIO_OK: ∀O,I,A,B. ∀P:A → Prop. ∀P':B → Prop. ∀e:IO O I (sigma A P). ∀f:sigma A P → IO O I B.
    99   (∀v:A. ∀p:P v. P_io O I ? P' (f (sig_intro A P v p))) →
    100   P_io O I ? P' (bindIO O I (sigma A 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.
     97lemma 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.
    106105
    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.
     106lemma 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.
    108107  (∀vA:A.∀vB:B. ∀p:P 〈vA,vB〉. P_io O I ? P' (f vA vB)) →
    109108  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.
    115114
    116 nlemma opt_bindIO_OK: ∀O,I,A,B. ∀P:B → Prop. ∀e:option A. ∀f: A → IO O I B.
     115lemma opt_bindIO_OK: ∀O,I,A,B. ∀P:B → Prop. ∀e:option A. ∀f: A → IO O I B.
    117116  (∀v:A. e = Some A v → P_io O I ? P (f v)) →
    118117  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 //;
     119qed.
    121120
    122 nlemma opt_bindIO2_OK: ∀O,I,A,B,C. ∀P:C → Prop. ∀e:option (A×B). ∀f: A → B → IO O I C.
     121lemma opt_bindIO2_OK: ∀O,I,A,B,C. ∀P:C → Prop. ∀e:option (A×B). ∀f: A → B → IO O I C.
    123122  (∀vA:A.∀vB:B. e = Some (A×B) 〈vA,vB〉 → P_io O I ? P (f vA vB)) →
    124123  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 //;
     125qed.
    127126
    128 nlemma res_bindIO_OK: ∀O,I,A,B. ∀P:B → Prop. ∀e:res A. ∀f: A → IO O I B.
     127lemma res_bindIO_OK: ∀O,I,A,B. ∀P:B → Prop. ∀e:res A. ∀f: A → IO O I B.
    129128  (∀v:A. e = OK A v → P_io O I ? P (f v)) →
    130129  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 //;
     131qed.
    133132
    134 nlemma res_bindIO2_OK: ∀O,I,A,B,C. ∀P:C → Prop. ∀e:res (A×B). ∀f: A → B → IO O I C.
     133lemma res_bindIO2_OK: ∀O,I,A,B,C. ∀P:C → Prop. ∀e:res (A×B). ∀f: A → B → IO O I C.
    135134  (∀vA:A.∀vB:B. e = OK (A×B) 〈vA,vB〉 → P_io O I ? P (f vA vB)) →
    136135  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 //;
     137qed.
    139138
    140 nlemma bindIO_OK: ∀O,I,A,B. ∀P:B → Prop. ∀e:IO O I A. ∀f: A → IO O I B.
     139lemma bindIO_OK: ∀O,I,A,B. ∀P:B → Prop. ∀e:IO O I A. ∀f: A → IO O I B.
    141140  (∀v:A. P_io O I ? P (f v)) →
    142141  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.
    148147
    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.
     148lemma bindIO2_OK: ∀O,I,A,B,C. ∀P:C → Prop. ∀e:IO O I (A×B). ∀f: A → B → IO O I C.
    150149  (∀v1:A.∀v2:B. P_io O I ? P (f v1 v2)) →
    151150  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.
    157156
    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.
     157lemma P_bindIO_OK: ∀O,I,A,B. ∀P':A → Prop. ∀P:B → Prop. ∀e:IO O I A. ∀f: A → IO O I B.
    159158  P_io … P' e →
    160159  (∀v:A. P' v → P_io O I ? P (f v)) →
    161160  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.
    167166
    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.
     167lemma 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.
    169168  P_io … P' e →
    170169  (∀v1:A.∀v2:B. P' 〈v1,v2〉 → P_io O I ? P (f v1 v2)) →
    171170  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.
    177176
    178177
    179178(* Is there a way to prove this without extensionality? *)
    180179
    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').
     180lemma 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').
    183182  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.
    190189
    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(*
     191lemma 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.
    193192  (∀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.