NaturalDeductionContext: more permutation proofs
[coq-hetmet.git] / src / HaskWeakToCore.v
1 (*********************************************************************************************************************************)
2 (* HaskWeakToCore: convert HaskWeak to HaskCore                                                                                  *)
3 (*********************************************************************************************************************************)
4
5 Generalizable All Variables.
6 Require Import Preamble.
7 Require Import General.
8 Require Import Coq.Strings.String.
9 Require Import Coq.Lists.List.
10 Require Import HaskKinds.
11 Require Import HaskLiterals.
12 Require Import HaskTyCons.
13 Require Import HaskCoreVars.
14 Require Import HaskCoreTypes.
15 Require Import HaskCore.
16 Require Import HaskWeakVars.
17 Require Import HaskWeakTypes.
18 Require Import HaskWeak.
19 Require Import HaskCoreToWeak.
20
21 Variable mkCoreLet : @CoreBind CoreVar -> @CoreExpr CoreVar -> @CoreExpr CoreVar.
22   Extract Inlined Constant mkCoreLet => "MkCore.mkCoreLet".
23
24 Variable sortAlts  : forall {a}{b}, list (@triple CoreAltCon a b) -> list (@triple CoreAltCon a b).
25   Extract Inlined Constant sortAlts => "sortAlts".
26   Implicit Arguments sortAlts [[a][b]].
27
28 Definition weakAltConToCoreAltCon (wa:WeakAltCon) : CoreAltCon :=
29   match wa with
30   | WeakDataAlt cdc => DataAlt cdc
31   | WeakLitAlt  lit => LitAlt lit
32   | WeakDEFAULT     => DEFAULT
33   end.
34
35 Fixpoint weakTypeToCoreType (wt:WeakType) : CoreType :=
36   match wt with
37     | WTyVarTy  (weakTypeVar v _)     => TyVarTy v
38     | WAppTy (WAppTy WFunTyCon t1) t2 => FunTy (weakTypeToCoreType t1) (weakTypeToCoreType t2)
39     | WAppTy  t1 t2                   => match (weakTypeToCoreType t1) with
40                                            | TyConApp tc tys => TyConApp tc (app tys ((weakTypeToCoreType t2)::nil))
41                                            | t1'             => AppTy t1' (weakTypeToCoreType t2)
42                                          end
43     | WTyCon    tc                    => TyConApp tc nil
44     | WTyFunApp tf lt                 => TyConApp tf (map weakTypeToCoreType lt)
45     | WClassP c lt                    => PredTy (ClassP c (map weakTypeToCoreType lt))
46     | WIParam n ty                    => PredTy (IParam n (weakTypeToCoreType ty))
47     | WForAllTy (weakTypeVar wtv _) t => ForAllTy wtv (weakTypeToCoreType t)
48     | WFunTyCon                       => TyConApp ArrowTyCon nil
49     | WCodeTy  (weakTypeVar ec _) t   => TyConApp ModalBoxTyCon ((TyVarTy ec)::(weakTypeToCoreType t)::nil)
50     | WCoFunTy t1 t2 t3               => FunTy (PredTy (EqPred (weakTypeToCoreType t1) (weakTypeToCoreType t2)))
51                                             (weakTypeToCoreType t3)
52   end.
53
54 Definition weakCoercionToCoreCoercion (wc:WeakCoercion) : CoreCoercion :=
55   CoreCoercionUnsafeCo (weakTypeToCoreType (fst (weakCoercionTypes wc))) (weakTypeToCoreType (snd (weakCoercionTypes wc))).
56
57 Fixpoint weakExprToCoreExpr (me:WeakExpr) : @CoreExpr CoreVar :=
58   match me with
59   | WEVar   (weakExprVar v _)            => CoreEVar  v
60   | WELit   lit                          => CoreELit  lit
61   | WEApp   e1 e2                        => CoreEApp     (weakExprToCoreExpr e1) (weakExprToCoreExpr e2)
62   | WETyApp e t                          => CoreEApp     (weakExprToCoreExpr e ) (CoreEType (weakTypeToCoreType t))
63   | WECoApp e co                         => CoreEApp     (weakExprToCoreExpr e ) (CoreECoercion (weakCoercionToCoreCoercion co))
64   | WENote  n e                          => CoreENote n  (weakExprToCoreExpr e )
65   | WELam   (weakExprVar ev _  ) e       => CoreELam  ev (weakExprToCoreExpr e )
66   | WETyLam (weakTypeVar tv _  ) e       => CoreELam  tv (weakExprToCoreExpr e )
67   | WECoLam (weakCoerVar cv   _ _) e     => CoreELam  cv (weakExprToCoreExpr e )
68   | WECast  e co                         => CoreECast    (weakExprToCoreExpr e ) (weakCoercionToCoreCoercion co)
69   | WEBrak  v (weakTypeVar ec _) e t     => fold_left CoreEApp
70                                                    ((CoreEType (TyVarTy ec))::
71                                                      (CoreEType (weakTypeToCoreType t))::
72                                                      (weakExprToCoreExpr e)::
73                                                      nil)
74                                                    (CoreEVar v)
75   | WEEsc   v (weakTypeVar ec _) e t     => fold_left CoreEApp
76                                                    ((CoreEType (TyVarTy ec))::
77                                                      (CoreEType (weakTypeToCoreType t))::
78                                                      (weakExprToCoreExpr e)::
79                                                      nil)
80                                                    (CoreEVar v)
81   | WECSP   v (weakTypeVar ec _) e t     => fold_left CoreEApp
82                                                    ((CoreEType (TyVarTy ec))::
83                                                      (CoreEType (weakTypeToCoreType t))::
84                                                      (weakExprToCoreExpr e)::
85                                                      nil)
86                                                    (CoreEVar v)
87   | WELet   (weakExprVar v _) ve e       => mkCoreLet      (CoreNonRec v (weakExprToCoreExpr ve))  (weakExprToCoreExpr e)
88   | WECase  vscrut escrut tbranches tc types alts  =>
89                                             CoreECase (weakExprToCoreExpr escrut) vscrut (weakTypeToCoreType tbranches)
90                                               (sortAlts ((
91                                                 fix mkCaseBranches (alts:Tree 
92                                                   ??(WeakAltCon*list WeakTypeVar*list WeakCoerVar*list WeakExprVar*WeakExpr)) :=
93                                                 match alts with
94                                                   | T_Leaf None              => nil
95                                                   | T_Branch b1 b2           => app (mkCaseBranches b1) (mkCaseBranches b2)
96                                                   | T_Leaf (Some (ac,tvars,cvars,evars,e)) =>
97                                                     (mkTriple (weakAltConToCoreAltCon ac)
98                                                       (app (app 
99                                                         (map (fun v:WeakTypeVar => weakVarToCoreVar v) tvars)
100                                                         (map (fun v:WeakCoerVar => weakVarToCoreVar v) cvars))
101                                                       (map (fun v:WeakExprVar => weakVarToCoreVar v) evars))
102                                                       (weakExprToCoreExpr e))::nil
103                                                 end
104                                               ) alts))
105   | WELetRec mlr e                       => CoreELet (CoreRec
106                                                ((fix mkLetBindings (mlr:Tree ??(WeakExprVar * WeakExpr)) :=
107                                                  match mlr with
108                                                    | T_Leaf None                        => nil
109                                                    | T_Leaf (Some (weakExprVar cv _,e)) => (cv,(weakExprToCoreExpr e))::nil
110                                                    | T_Branch b1 b2                     => app (mkLetBindings b1) (mkLetBindings b2)
111                                                  end) mlr))
112                                                (weakExprToCoreExpr e)
113   end.
114
115 Definition weakTypeOfWeakExpr (we:WeakExpr) : ???WeakType :=
116   coreTypeToWeakType (coreTypeOfCoreExpr (weakExprToCoreExpr we)).
117
118 Instance weakExprToString : ToString WeakExpr  :=
119   { toString := fun we => toString (weakExprToCoreExpr we) }.
120
121