remove stale import from ExtractionMain
[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 HaskLiteralsAndTyCons.
12 Require Import HaskCoreVars.
13 Require Import HaskCoreTypes.
14 Require Import HaskCore.
15 Require Import HaskWeakVars.
16 Require Import HaskWeakTypes.
17 Require Import HaskWeak.
18 Require Import HaskCoreToWeak.
19
20 Variable mkCoreLet : @CoreBind CoreVar -> @CoreExpr CoreVar -> @CoreExpr CoreVar.
21   Extract Inlined Constant mkCoreLet => "MkCore.mkCoreLet".
22
23 Variable sortAlts  : forall {a}{b}, list (@triple CoreAltCon a b) -> list (@triple CoreAltCon a b).
24   Extract Inlined Constant sortAlts => "sortAlts".
25   Implicit Arguments sortAlts [[a][b]].
26
27 Variable mkUnsafeCoercion : CoreType -> CoreType -> CoreCoercion.
28     Extract Inlined Constant mkUnsafeCoercion => "Coercion.mkUnsafeCoercion".
29
30 (* Coercion and Type are actually the same thing in GHC, but we don't tell Coq about that.  This lets us get around it. *)
31 Variable coreCoercionsAreReallyTypes : CoreCoercion -> CoreType.
32   Extract Inlined Constant coreCoercionsAreReallyTypes => "(\x -> x)".
33
34 Definition weakAltConToCoreAltCon (wa:WeakAltCon) : CoreAltCon :=
35   match wa with
36   | WeakDataAlt cdc => DataAlt cdc
37   | WeakLitAlt  lit => LitAlt lit
38   | WeakDEFAULT     => DEFAULT
39   end.
40
41 Fixpoint weakTypeToCoreType (wt:WeakType) : CoreType :=
42   match wt with
43     | WTyVarTy  (weakTypeVar v _)     => TyVarTy v
44     | WAppTy (WAppTy WFunTyCon t1) t2 => FunTy (weakTypeToCoreType t1) (weakTypeToCoreType t2)
45     | WAppTy  t1 t2                   => match (weakTypeToCoreType t1) with
46                                            | TyConApp tc tys => TyConApp tc (app tys ((weakTypeToCoreType t2)::nil))
47                                            | t1'             => AppTy t1' (weakTypeToCoreType t2)
48                                          end
49     | WTyCon    tc                    => TyConApp tc nil
50     | WTyFunApp tf lt                 => TyConApp tf (map weakTypeToCoreType lt)
51     | WClassP c lt                    => PredTy (ClassP c (map weakTypeToCoreType lt))
52     | WIParam n ty                    => PredTy (IParam n (weakTypeToCoreType ty))
53     | WForAllTy (weakTypeVar wtv _) t => ForAllTy wtv (weakTypeToCoreType t)
54     | WFunTyCon                       => TyConApp ArrowTyCon nil
55     | WCodeTy  (weakTypeVar ec _) t   => TyConApp ModalBoxTyCon ((TyVarTy ec)::(weakTypeToCoreType t)::nil)
56     | WCoFunTy t1 t2 t3               => FunTy (PredTy (EqPred (weakTypeToCoreType t1) (weakTypeToCoreType t2)))
57                                             (weakTypeToCoreType t3)
58   end.
59
60 Definition weakCoercionToCoreCoercion (wc:WeakCoercion) : CoreCoercion :=
61   mkUnsafeCoercion (weakTypeToCoreType (fst (weakCoercionTypes wc))) (weakTypeToCoreType (snd (weakCoercionTypes wc))).
62
63 Fixpoint weakExprToCoreExpr (me:WeakExpr) : @CoreExpr CoreVar :=
64   match me with
65   | WEVar   (weakExprVar v _)            => CoreEVar  v
66   | WELit   lit                          => CoreELit  lit
67   | WEApp   e1 e2                        => CoreEApp     (weakExprToCoreExpr e1) (weakExprToCoreExpr e2)
68   | WETyApp e t                          => CoreEApp     (weakExprToCoreExpr e ) (CoreEType (weakTypeToCoreType t))
69   | WECoApp e co                         => CoreEApp     (weakExprToCoreExpr e )
70                                                            (CoreEType (coreCoercionsAreReallyTypes (weakCoercionToCoreCoercion co)))
71   | WENote  n e                          => CoreENote n  (weakExprToCoreExpr e )
72   | WELam   (weakExprVar ev _  ) e       => CoreELam  ev (weakExprToCoreExpr e )
73   | WETyLam (weakTypeVar tv _  ) e       => CoreELam  tv (weakExprToCoreExpr e )
74   | WECoLam (weakCoerVar cv _ _ _) e     => CoreELam  cv (weakExprToCoreExpr e )
75   | WECast  e co                         => CoreECast    (weakExprToCoreExpr e ) (weakCoercionToCoreCoercion co)
76   | WEBrak  v (weakTypeVar ec _) e t     => fold_left CoreEApp
77                                                    ((CoreEType (TyVarTy ec))::
78                                                      (CoreEType (weakTypeToCoreType t))::
79                                                      (weakExprToCoreExpr e)::
80                                                      nil)
81                                                    (CoreEVar v)
82   | WEEsc   v (weakTypeVar ec _) e t     => fold_left CoreEApp
83                                                    ((CoreEType (TyVarTy ec))::
84                                                      (CoreEType (weakTypeToCoreType t))::
85                                                      (weakExprToCoreExpr e)::
86                                                      nil)
87                                                    (CoreEVar v)
88   | WECSP   v (weakTypeVar ec _) e t     => fold_left CoreEApp
89                                                    ((CoreEType (TyVarTy ec))::
90                                                      (CoreEType (weakTypeToCoreType t))::
91                                                      (weakExprToCoreExpr e)::
92                                                      nil)
93                                                    (CoreEVar v)
94   | WELet   (weakExprVar v _) ve e       => mkCoreLet      (CoreNonRec v (weakExprToCoreExpr ve))  (weakExprToCoreExpr e)
95   | WECase  vscrut escrut tbranches tc types alts  =>
96                                             CoreECase (weakExprToCoreExpr escrut) vscrut (weakTypeToCoreType tbranches)
97                                               (sortAlts ((
98                                                 fix mkCaseBranches (alts:Tree 
99                                                   ??(WeakAltCon*list WeakTypeVar*list WeakCoerVar*list WeakExprVar*WeakExpr)) :=
100                                                 match alts with
101                                                   | T_Leaf None              => nil
102                                                   | T_Branch b1 b2           => app (mkCaseBranches b1) (mkCaseBranches b2)
103                                                   | T_Leaf (Some (ac,tvars,cvars,evars,e)) =>
104                                                     (mkTriple (weakAltConToCoreAltCon ac)
105                                                       (app (app 
106                                                         (map (fun v:WeakTypeVar => weakVarToCoreVar v) tvars)
107                                                         (map (fun v:WeakCoerVar => weakVarToCoreVar v) cvars))
108                                                       (map (fun v:WeakExprVar => weakVarToCoreVar v) evars))
109                                                       (weakExprToCoreExpr e))::nil
110                                                 end
111                                               ) alts))
112   | WELetRec mlr e                       => CoreELet (CoreRec
113                                                ((fix mkLetBindings (mlr:Tree ??(WeakExprVar * WeakExpr)) :=
114                                                  match mlr with
115                                                    | T_Leaf None                        => nil
116                                                    | T_Leaf (Some (weakExprVar cv _,e)) => (cv,(weakExprToCoreExpr e))::nil
117                                                    | T_Branch b1 b2                     => app (mkLetBindings b1) (mkLetBindings b2)
118                                                  end) mlr))
119                                                (weakExprToCoreExpr e)
120   end.
121
122 Definition weakTypeOfWeakExpr (we:WeakExpr) : ???WeakType :=
123   coreTypeToWeakType (coreTypeOfCoreExpr (weakExprToCoreExpr we)).
124
125 Instance weakExprToString : ToString WeakExpr  :=
126   { toString := fun we => toString (weakExprToCoreExpr we) }.
127
128