f0a8300886a15eb82d1d717982d37e7dcb693d92
[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 HaskCoreLiterals.
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 AltCon a b) -> list (@triple AltCon a b).
24   Extract Inlined Constant mkCoreLet => "sortAlts".
25   Implicit Arguments sortAlts [[a][b]].
26
27 Variable trustMeCoercion           : CoreCoercion.
28   Extract Inlined Constant trustMeCoercion => "Coercion.unsafeCoerce".
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 (* a dummy variable which is never referenced but needed for a binder *)
35 Variable dummyVariable : CoreVar.
36   (* FIXME this doesn't actually work *)
37   Extract Inlined Constant dummyVariable => "(Prelude.error ""dummyVariable"")".
38
39 Section HaskWeakToCore.
40
41   (* the CoreVar for the "magic" bracket/escape identifiers must be created from within one of the monads *)
42   Context (hetmet_brak_var : CoreVar).
43   Context (hetmet_esc_var  : CoreVar).
44
45   (* FIXME: do something more intelligent here *)
46   Definition weakCoercionToCoreCoercion : WeakCoercion -> CoreCoercion :=
47     fun _ => trustMeCoercion.
48
49   Fixpoint weakExprToCoreExpr (me:WeakExpr) : @CoreExpr CoreVar :=
50   match me with
51   | WEVar   (weakExprVar v _)            => CoreEVar  v
52   | WELit   lit                          => CoreELit  lit
53   | WEApp   e1 e2                        => CoreEApp     (weakExprToCoreExpr e1) (weakExprToCoreExpr e2)
54   | WETyApp e t                          => CoreEApp     (weakExprToCoreExpr e ) (CoreEType (weakTypeToCoreType t))
55   | WECoApp e co                         => CoreEApp     (weakExprToCoreExpr e )
56                                                            (CoreEType (coreCoercionsAreReallyTypes (weakCoercionToCoreCoercion co)))
57   | WENote  n e                          => CoreENote n  (weakExprToCoreExpr e )
58   | WELam   (weakExprVar ev _  ) e       => CoreELam  ev (weakExprToCoreExpr e )
59   | WETyLam (weakTypeVar tv _  ) e       => CoreELam  tv (weakExprToCoreExpr e )
60   | WECoLam (weakCoerVar cv _ _) e       => CoreELam  cv (weakExprToCoreExpr e )
61   | WECast  e co                         => CoreECast    (weakExprToCoreExpr e ) (weakCoercionToCoreCoercion co)
62   | WEBrak  (weakTypeVar ec _) e t       => CoreEApp  (CoreEApp (CoreEVar hetmet_brak_var)
63                                                            (CoreEType (TyVarTy ec))) (CoreEType (weakTypeToCoreType t))
64   | WEEsc   (weakTypeVar ec _) e t       => CoreEApp  (CoreEApp (CoreEVar hetmet_esc_var)
65                                                            (CoreEType (TyVarTy ec))) (CoreEType (weakTypeToCoreType t))
66   | WELet   (weakExprVar v _) ve e       => mkCoreLet      (CoreNonRec v (weakExprToCoreExpr ve))  (weakExprToCoreExpr e)
67   | WECase  e tbranches tc types alts    => CoreECase (weakExprToCoreExpr e) dummyVariable (weakTypeToCoreType tbranches)
68                                               (sortAlts ((
69                                                 fix mkCaseBranches (alts:Tree 
70                                                   ??(AltCon*list WeakTypeVar*list WeakCoerVar*list WeakExprVar*WeakExpr)) :=
71                                                 match alts with
72                                                   | T_Leaf None              => nil
73                                                   | T_Branch b1 b2           => app (mkCaseBranches b1) (mkCaseBranches b2)
74                                                   | T_Leaf (Some (ac,tvars,cvars,evars,e)) =>
75                                                     (mkTriple (ac:AltCon)
76                                                       (app (app 
77                                                         (map (fun v:WeakTypeVar => weakVarToCoreVar v) tvars)
78                                                         (map (fun v:WeakCoerVar => weakVarToCoreVar v) cvars))
79                                                       (map (fun v:WeakExprVar => weakVarToCoreVar v) evars))
80                                                       (weakExprToCoreExpr e))::nil
81                                                 end
82                                               ) alts))
83   | WELetRec mlr e                       => CoreELet (CoreRec
84                                                ((fix mkLetBindings (mlr:Tree ??(WeakExprVar * WeakExpr)) :=
85                                                  match mlr with
86                                                    | T_Leaf None                        => nil
87                                                    | T_Leaf (Some (weakExprVar cv _,e)) => (cv,(weakExprToCoreExpr e))::nil
88                                                    | T_Branch b1 b2                     => app (mkLetBindings b1) (mkLetBindings b2)
89                                                  end) mlr))
90                                                (weakExprToCoreExpr e)
91   end.
92
93 End HaskWeakToCore.
94
95