1 (*********************************************************************************************************************************)
2 (* HaskWeakToCore: convert HaskWeak to HaskCore *)
3 (*********************************************************************************************************************************)
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.
21 Variable mkCoreLet : @CoreBind CoreVar -> @CoreExpr CoreVar -> @CoreExpr CoreVar.
22 Extract Inlined Constant mkCoreLet => "MkCore.mkCoreLet".
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]].
28 Variable mkUnsafeCoercion : CoreType -> CoreType -> CoreCoercion.
29 Extract Inlined Constant mkUnsafeCoercion => "Coercion.mkUnsafeCoercion".
31 (* Coercion and Type are actually the same thing in GHC, but we don't tell Coq about that. This lets us get around it. *)
32 Variable coreCoercionsAreReallyTypes : CoreCoercion -> CoreType.
33 Extract Inlined Constant coreCoercionsAreReallyTypes => "(\x -> x)".
35 Definition weakAltConToCoreAltCon (wa:WeakAltCon) : CoreAltCon :=
37 | WeakDataAlt cdc => DataAlt cdc
38 | WeakLitAlt lit => LitAlt lit
39 | WeakDEFAULT => DEFAULT
42 Fixpoint weakTypeToCoreType (wt:WeakType) : CoreType :=
44 | WTyVarTy (weakTypeVar v _) => TyVarTy v
45 | WAppTy (WAppTy WFunTyCon t1) t2 => FunTy (weakTypeToCoreType t1) (weakTypeToCoreType t2)
46 | WAppTy t1 t2 => match (weakTypeToCoreType t1) with
47 | TyConApp tc tys => TyConApp tc (app tys ((weakTypeToCoreType t2)::nil))
48 | t1' => AppTy t1' (weakTypeToCoreType t2)
50 | WTyCon tc => TyConApp tc nil
51 | WTyFunApp tf lt => TyConApp tf (map weakTypeToCoreType lt)
52 | WClassP c lt => PredTy (ClassP c (map weakTypeToCoreType lt))
53 | WIParam n ty => PredTy (IParam n (weakTypeToCoreType ty))
54 | WForAllTy (weakTypeVar wtv _) t => ForAllTy wtv (weakTypeToCoreType t)
55 | WFunTyCon => TyConApp ArrowTyCon nil
56 | WCodeTy (weakTypeVar ec _) t => TyConApp ModalBoxTyCon ((TyVarTy ec)::(weakTypeToCoreType t)::nil)
57 | WCoFunTy t1 t2 t3 => FunTy (PredTy (EqPred (weakTypeToCoreType t1) (weakTypeToCoreType t2)))
58 (weakTypeToCoreType t3)
61 Definition weakCoercionToCoreCoercion (wc:WeakCoercion) : CoreCoercion :=
62 mkUnsafeCoercion (weakTypeToCoreType (fst (weakCoercionTypes wc))) (weakTypeToCoreType (snd (weakCoercionTypes wc))).
64 Fixpoint weakExprToCoreExpr (me:WeakExpr) : @CoreExpr CoreVar :=
66 | WEVar (weakExprVar v _) => CoreEVar v
67 | WELit lit => CoreELit lit
68 | WEApp e1 e2 => CoreEApp (weakExprToCoreExpr e1) (weakExprToCoreExpr e2)
69 | WETyApp e t => CoreEApp (weakExprToCoreExpr e ) (CoreEType (weakTypeToCoreType t))
70 | WECoApp e co => CoreEApp (weakExprToCoreExpr e )
71 (CoreEType (coreCoercionsAreReallyTypes (weakCoercionToCoreCoercion co)))
72 | WENote n e => CoreENote n (weakExprToCoreExpr e )
73 | WELam (weakExprVar ev _ ) e => CoreELam ev (weakExprToCoreExpr e )
74 | WETyLam (weakTypeVar tv _ ) e => CoreELam tv (weakExprToCoreExpr e )
75 | WECoLam (weakCoerVar cv _ _) e => CoreELam cv (weakExprToCoreExpr e )
76 | WECast e co => CoreECast (weakExprToCoreExpr e ) (weakCoercionToCoreCoercion co)
77 | WEBrak v (weakTypeVar ec _) e t => fold_left CoreEApp
78 ((CoreEType (TyVarTy ec))::
79 (CoreEType (weakTypeToCoreType t))::
80 (weakExprToCoreExpr e)::
83 | WEEsc v (weakTypeVar ec _) e t => fold_left CoreEApp
84 ((CoreEType (TyVarTy ec))::
85 (CoreEType (weakTypeToCoreType t))::
86 (weakExprToCoreExpr e)::
89 | WECSP v (weakTypeVar ec _) e t => fold_left CoreEApp
90 ((CoreEType (TyVarTy ec))::
91 (CoreEType (weakTypeToCoreType t))::
92 (weakExprToCoreExpr e)::
95 | WELet (weakExprVar v _) ve e => mkCoreLet (CoreNonRec v (weakExprToCoreExpr ve)) (weakExprToCoreExpr e)
96 | WECase vscrut escrut tbranches tc types alts =>
97 CoreECase (weakExprToCoreExpr escrut) vscrut (weakTypeToCoreType tbranches)
99 fix mkCaseBranches (alts:Tree
100 ??(WeakAltCon*list WeakTypeVar*list WeakCoerVar*list WeakExprVar*WeakExpr)) :=
103 | T_Branch b1 b2 => app (mkCaseBranches b1) (mkCaseBranches b2)
104 | T_Leaf (Some (ac,tvars,cvars,evars,e)) =>
105 (mkTriple (weakAltConToCoreAltCon ac)
107 (map (fun v:WeakTypeVar => weakVarToCoreVar v) tvars)
108 (map (fun v:WeakCoerVar => weakVarToCoreVar v) cvars))
109 (map (fun v:WeakExprVar => weakVarToCoreVar v) evars))
110 (weakExprToCoreExpr e))::nil
113 | WELetRec mlr e => CoreELet (CoreRec
114 ((fix mkLetBindings (mlr:Tree ??(WeakExprVar * WeakExpr)) :=
117 | T_Leaf (Some (weakExprVar cv _,e)) => (cv,(weakExprToCoreExpr e))::nil
118 | T_Branch b1 b2 => app (mkLetBindings b1) (mkLetBindings b2)
120 (weakExprToCoreExpr e)
123 Definition weakTypeOfWeakExpr (we:WeakExpr) : ???WeakType :=
124 coreTypeToWeakType (coreTypeOfCoreExpr (weakExprToCoreExpr we)).
126 Instance weakExprToString : ToString WeakExpr :=
127 { toString := fun we => toString (weakExprToCoreExpr we) }.