Changeset 1976 for src/utilities


Ignore:
Timestamp:
May 21, 2012, 7:04:21 PM (8 years ago)
Author:
tranquil
Message:
  • monads: just changed some defs, which had to be propagated in some files
  • ASM/CostProof.ma: linked cost as defined there to the one in StructuredTraces? that uses fold
  • added a library for permutations of lists (useful with fold AC ops on lists)
  • first draft of abstract_status implementation for joint languages (file joint/as_semantics.ma)
Location:
src/utilities
Files:
2 added
3 edited

Legend:

Unmodified
Added
Removed
  • src/utilities/monad.ma

    r1949 r1976  
    2323 
    2424record Monad : Type[1] ≝ {
    25   monad : Type[0] → Type[0] ;
     25  monad :1> Type[0] → Type[0] ;
    2626  m_return : ∀X. X → monad X ;
    2727  m_bind : ∀X,Y. monad X → (X → monad Y) → monad Y
     
    124124record MonadProps : Type[1] ≝
    125125  { max_def :> Monad
    126   ; m_return_bind : ∀X,Y.∀x : X.∀f : X → monad max_def Y. ! y ← return x ; f y = f x
    127   ; m_bind_return : ∀X.∀m : monad max_def X.! x ← m ; return x = m
    128   ; m_bind_bind : ∀X,Y,Z. ∀m : monad max_def X.∀f : X → monad max_def Y.
    129       ∀g : Y → monad max_def Z.! y ← (! x ← m ; f x) ; g y = ! x ← m; ! y ← f x ; g y
    130   ; m_bind_ext_eq : ∀X,Y.∀m : monad max_def X.∀f,g : X → monad max_def Y.
     126  ; m_return_bind : ∀X,Y.∀x : X.∀f : X → max_def Y. ! y ← return x ; f y = f x
     127  ; m_bind_return : ∀X.∀m : max_def X.! x ← m ; return x = m
     128  ; m_bind_bind : ∀X,Y,Z. ∀m : max_def X.∀f : X → max_def Y.
     129      ∀g : Y → max_def Z.! y ← (! x ← m ; f x) ; g y = ! x ← m; ! y ← f x ; g y
     130  ; m_bind_ext_eq : ∀X,Y.∀m : max_def X.∀f,g : X → max_def Y.
    131131      (∀x.f x = g x) → ! x ← m ; f x = ! x ← m ; g x
    132132  }.
     
    134134record SetoidMonadProps : Type[1] ≝
    135135  { smax_def :> Monad
    136   ; sm_eq : ∀X.relation (monad smax_def X)
     136  ; sm_eq : ∀X.relation (smax_def X)
    137137  ; sm_eq_refl : ∀X.reflexive ? (sm_eq X)
    138138  ; sm_eq_trans : ∀X.transitive ? (sm_eq X)
     
    140140  ; sm_return_proper : ∀X,x.sm_eq X (return x) (return x)
    141141  ; sm_bind_proper : ∀X,Y,x,y,f,g.sm_eq X x y → (∀x.sm_eq Y (f x) (g x)) → sm_eq Y (x »= f) (y »= g)
    142   ; sm_return_bind : ∀X,Y.∀x : X.∀f : X → monad smax_def Y.
     142  ; sm_return_bind : ∀X,Y.∀x : X.∀f : X → smax_def Y.
    143143      sm_eq Y (! y ← return x ; f y) (f x)
    144   ; sm_bind_return : ∀X.∀m : monad smax_def X.sm_eq X (! x ← m ; return x) m
    145   ; sm_bind_bind : ∀X,Y,Z. ∀m : monad smax_def X.∀f : X → monad smax_def Y.
    146       ∀g : Y → monad smax_def Z.sm_eq Z (! y ← (! x ← m ; f x) ; g y) (! x ← m; ! y ← f x ; g y)
     144  ; sm_bind_return : ∀X.∀m : smax_def X.sm_eq X (! x ← m ; return x) m
     145  ; sm_bind_bind : ∀X,Y,Z. ∀m : smax_def X.∀f : X → smax_def Y.
     146      ∀g : Y → smax_def Z.sm_eq Z (! y ← (! x ← m ; f x) ; g y) (! x ← m; ! y ← f x ; g y)
    147147  }.
    148148
    149149definition setoid_of_monad : ∀M : SetoidMonadProps.∀X : Type[0].
    150150  Setoid ≝
    151   λM,X.mk_Setoid (monad M X) (sm_eq M X) (sm_eq_refl M X) (sm_eq_trans M X) (sm_eq_sym M X).
     151  λM,X.mk_Setoid (M X) (sm_eq M X) (sm_eq_refl M X) (sm_eq_trans M X) (sm_eq_sym M X).
    152152
    153153include "hints_declaration.ma".
     
    160160include "basics/lists/list.ma".
    161161
    162 definition m_map ≝ λM,X,Y.λf : X → Y.λm : monad M X.
     162definition m_map ≝ λM : Monad.λX,Y.λf : X → Y.λm : M X.
    163163  ! x ← m ; return f x.
    164164
    165 definition m_map2 ≝ λM,X,Y,Z.λf : X → Y → Z.λm : monad M X.λn : monad M Y.
     165definition m_map2 ≝ λM : Monad.λX,Y,Z.λf : X → Y → Z.λm : M X.λn : M Y.
    166166  ! x ← m ; ! y ← n ; return f x y.
    167167 
    168 definition m_bind2 ≝ λM,X,Y,Z.λm : monad M (X × Y).λf : X → Y → monad M Z.
     168definition m_bind2 ≝ λM : Monad.λX,Y,Z.λm : M (X × Y).λf : X → Y → M Z.
    169169  ! p ← m ; f (\fst p) (\snd p).
    170170
     
    172172
    173173definition m_bind3 :
    174   ∀M,X,Y,Z,W.monad M (X×Y×Z) → (X → Y → Z → monad M W) → monad M W ≝
     174  ∀M : Monad.∀X,Y,Z,W.M (X×Y×Z) → (X → Y → Z → M W) → M W ≝
    175175  λM,X,Y,Z,W,m,f.
    176176  ! p ← m ; f (\fst (\fst p)) (\snd (\fst p)) (\snd p).
     
    178178interpretation "monad bind3" 'm_bind3 m f = (m_bind3 ? ? ? ? ? m f).
    179179
    180 definition m_join : ∀M,X.monad M (monad M X) → monad M X ≝
     180definition m_join : ∀M : Monad.∀X.M (M X) → M X ≝
    181181  λM,X,m.! x ← m ; x.
    182182
    183183definition m_sigbind2 :
    184   ∀M,A,B,C.∀P:A×B → Prop. monad M (Σx:A×B.P x) →
    185       (∀a,b. P 〈a,b〉 → monad M C) → monad M C ≝
     184  ∀M : Monad.∀A,B,C.∀P:A×B → Prop. M (Σx:A×B.P x) →
     185      (∀a,b. P 〈a,b〉 → M C) → M C ≝
    186186λM,A,B,C,P,e,f.
    187187    ! e_sig ← e ;
     
    197197
    198198definition m_list_map :
    199   ∀M,X,Y.(X → monad M Y) → list X → monad M (list Y) ≝
     199  ∀M : Monad.∀X,Y.(X → M Y) → list X → M (list Y) ≝
    200200  λM,X,Y,f,l.foldr … (λel,macc.! r ← f el; !acc ← macc; return (r :: acc)) (return [ ]) l.
    201201
    202202definition m_list_map_sigma :
    203   ∀M,X,Y,P.(X → monad M (Σy : Y.P y)) → list X → monad M (Σl : list Y.All ? P l) ≝
     203  ∀M : Monad.∀X,Y,P.(X → M (Σy : Y.P y)) → list X → M (Σl : list Y.All ? P l) ≝
    204204  λM,X,Y,P,f,l.foldr … (λel,macc.
    205205    ! «r, r_prf» ← f el ;
     
    209209
    210210definition m_bin_op :
    211   ∀M,X,Y,Z.(X → Y → Z) → monad M X → monad M Y → monad M Z ≝
     211  ∀M : Monad.∀X,Y,Z.(X → Y → Z) → M X → M Y → M Z ≝
    212212  λM,X,Y,Z,op,m,n. ! x ← m ; ! y ← n ; return op x y.
    213213
    214 unification hint 0 ≔ M, X, Y, Z, m, f ⊢
    215 m_bind M (X×Y) Z m (λp.f (\fst p) (\snd p)) ≡ m_bind2 M X Y Z m f.
    216 
    217 unification hint 0 ≔ M, X, Y, Z, W, m, f ⊢
    218 m_bind M (X×Y×Z) W m (λp.f (\fst (\fst p)) (\snd (\fst p)) (\snd p)) ≡
    219 m_bind3 M X Y Z W m f.
     214unification hint 0 ≔ M, X, Y, Z, m, f ;
     215  P ≟ Prod X Y, F ≟ λp.f (\fst p) (\snd p) ⊢
     216m_bind M P Z m F ≡ m_bind2 M X Y Z m f.
     217
     218unification hint 0 ≔ M, X, Y, Z, W, m, f ;
     219  P' ≟ Prod X Y, P ≟ Prod P' Z, F ≟ λp.f (\fst (\fst p)) (\snd (\fst p)) (\snd p) ⊢
     220m_bind M P W m F ≡ m_bind3 M X Y Z W m f.
    220221
    221222definition MakeMonadProps : ?→?→?→?→?→?→?→MonadProps ≝ λmonad,m_return,m_bind.
     
    229230 
    230231record MonadPred (M : Monad) : Type[1] ≝
    231   { m_pred :> ∀X.pred_transformer X (monad M X)
    232   ; mp_inject : ∀X,P.(Σm.m_pred X P m) → monad M (Σx.P x)
     232  { m_pred :2> ∀X.(X → Prop) → (M X → Prop)
    233233  ; mp_return : ∀X,P,x.P x → m_pred X P (return x)
    234234  ; mp_bind : ∀X,Y,Pin,Pout,m.m_pred X Pin m →
     
    236236                  m_pred ? Pout (m_bind … m f)
    237237  ; m_pred_mp : ∀X.modus_ponens ?? (m_pred X)
     238  }.
     239
     240record InjMonadPred (M : Monad) : Type[1] ≝
     241  { im_pred :> MonadPred M
     242  ; mp_inject : ∀X.∀P : X → Prop.(Σm.im_pred P m) → M (Σx.P x)
    238243  ; mp_inject_eq : ∀X,P,m.pi1 ?? m = ! x ← mp_inject X P m ; return pi1 … x
    239244  }.
    240245
    241 coercion coerc_mp_inject : ∀M.∀MP:MonadPred M.
    242   ∀X,P.∀m : Σm.m_pred ? MP X P m.monad M (Σx.P x) ≝
    243   mp_inject on _m : Sig (monad ??) (λm.m_pred ???? m) to monad ? (Sig ? (λx.? x)).
    244 
    245 lemma mp_inject_bind : ∀M : MonadProps.∀MP,X,P,Y.∀m.∀f : X → monad M Y.
     246coercion coerc_mp_inject : ∀M.∀MP:InjMonadPred M.
     247  ∀X.∀P : X → Prop.∀m : Σm.MP P m.M (Σx.P x) ≝
     248  mp_inject on _m : Sig (monad ??) (λm.im_pred ??? m) to monad ? (Sig ? (λx.? x)).
     249
     250lemma mp_inject_bind : ∀M : MonadProps.∀MP,X,P,Y.∀m.∀f : X → M Y.
    246251  ! x ← mp_inject M MP X P m ; f (pi1 … x) = ! x ← pi1 … m ; f x.
    247252#M#MP#X#P#Y#m#f >mp_inject_eq >m_bind_bind @m_bind_ext_eq #x >m_return_bind % qed.
    248253
    249254record MonadRel (M1 : Monad) (M2 : Monad) : Type[1] ≝
    250   { m_rel :> ∀X,Y.rel_transformer X Y (monad M1 X) (monad M2 Y)
     255  { m_rel :3> ∀X,Y.(X → Y → Prop) → (M1 X → M2 Y → Prop)
    251256  ; mr_return : ∀X,Y,rel,x,y.rel x y → m_rel X Y rel (return x) (return y)
    252257  ; mr_bind : ∀X,Y,Z,W,relin,relout,m,n.m_rel X Y relin m n → ∀f,g.(∀x,y.relin x y → m_rel Z W relout (f x) (g y)) →
     
    254259  ; m_rel_mp : ∀X,Y.rel_modus_ponens ???? (m_rel X Y)
    255260  }.
    256 
    257 
  • src/utilities/option.ma

    r1949 r1976  
    6262interpretation "option try and catch" 'trycatch m f = (opt_try_catch ? m f).
    6363
    64 definition OptPred ≝ λPdef : Prop.mk_MonadPred Option
    65   (λX,P,x.Try ! y ← x ; return P y catch ⇒ Pdef)
     64definition OptPred ≝ λPdef : Prop.mk_InjMonadPred Option
     65  (mk_MonadPred ?
     66    (λX,P,x.Try ! y ← x ; return P y catch ⇒ Pdef)
     67    ???)
    6668  (λX,P,m_sig.match m_sig with [ mk_Sig m prf ⇒ match m return λx.Try ! y ← x ; return P y catch ⇒ Pdef → option (Σy.?) with [ Some x ⇒ λprf.Some ? x | None ⇒ λ_.None ? ] prf ])
    67   ????.
    68 [ @prf
     69  ?.
     70[4: @prf
    6971|*:
    70 #X[2:#Y]#P1[1,3:#P2[2:#H]]
     72#X[2:#Y]#P1[1,2:#P2[2:#H]]
    7173[1,2,4: * [5: *]] normalize /2/
    7274qed.
  • src/utilities/state.ma

    r1882 r1976  
    11include "utilities/monad.ma".
    22
    3 definition State
     3definition state_monad
    44  λS : Type[0].MakeSetoidMonadProps
    55  (* the monad *)
     
    2121qed.
    2222
    23 definition state_monad ≝ λS.monad (State S).
    24 
    2523definition state_get : ∀S.state_monad S S ≝ λS,s.〈s,s〉.
    2624definition state_set : ∀S.S → state_monad S unit ≝ λS,s.λ_.〈s,it〉.
    2725definition state_run : ∀S,X. S → (state_monad S X) → X ≝ λS,X,s,c.'pi2 (c s).
    2826
    29 include "hints_declaration.ma".
    30 unification hint 0 ≔ S,X;
    31 N ≟ State S, M ≟ smax_def N
    32 (*---------------*) ⊢
    33 state_monad S X ≡ monad M X
    34 .
    35 
    36 definition StatePred ≝ λS.λPs : S → Prop.mk_MonadPred (State S)
     27definition state_pred ≝ λS.λPs : S → Prop.mk_MonadPred (state_monad S)
    3728  (λX,P,m.∀s.Ps s → let m' ≝ m s in Ps (\fst m') ∧ P (\snd m')) ???.
    38 [ normalize /2/
     29[ normalize /2 by conj/
    3930| normalize #X#Y#P1#P2 #m #H #f #G #s #Ps lapply (H … Ps)
    4031  elim (m s) #s' #x normalize * /2/
     
    4334qed.
    4435
    45 definition StateRel ≝ λS,T.λPs : S → T → Prop.mk_MonadRel (State S) (State T)
     36definition StateRel ≝ λS,T.λPs : S → T → Prop.mk_MonadRel (state_monad S) (state_monad T)
    4637  (λX,Y,P,m,n.∀s,t.Ps s t → let m' ≝ m s in let n' ≝ n t in
    4738    Ps (\fst m') (\fst n') ∧ P (\snd m') (\snd n')) ???.
Note: See TracChangeset for help on using the changeset viewer.