1 (*********************************************************************************************************************************)
2 (* HaskCoreToWeak: convert HaskCore to HaskWeak *)
3 (*********************************************************************************************************************************)
5 Generalizable All Variables.
6 Require Import Preamble.
7 Require Import Coq.Lists.List.
8 Require Import General.
9 Require Import HaskKinds.
10 Require Import HaskLiteralsAndTyCons.
11 Require Import HaskCoreVars.
12 Require Import HaskCoreTypes.
13 Require Import HaskCore.
14 Require Import HaskWeakVars.
15 Require Import HaskWeakTypes.
16 Require Import HaskWeak.
18 Variable tyConOrTyFun : CoreTyCon -> sum TyCon TyFun. Extract Inlined Constant tyConOrTyFun => "tyConOrTyFun".
19 Variable coreViewDeep : CoreType -> CoreType. Extract Inlined Constant coreViewDeep => "coreViewDeep".
20 Variable coreCoercionToWeakCoercion : CoreCoercion -> WeakCoercion.
21 Extract Inlined Constant coreCoercionToWeakCoercion => "coreCoercionToWeakCoercion".
23 (* extracts the Name from a CoreVar *)
24 Variable coreVarCoreName : CoreVar -> CoreName. Extract Inlined Constant coreVarCoreName => "Var.varName".
26 (* the magic wired-in name for the modal type introduction/elimination forms *)
27 Variable hetmet_brak_name : CoreName. Extract Inlined Constant hetmet_brak_name => "PrelNames.hetmet_brak_name".
28 Variable hetmet_esc_name : CoreName. Extract Inlined Constant hetmet_esc_name => "PrelNames.hetmet_esc_name".
29 Variable hetmet_csp_name : CoreName. Extract Inlined Constant hetmet_csp_name => "PrelNames.hetmet_csp_name".
31 Fixpoint coreTypeToWeakType' (ct:CoreType) : ???WeakType :=
33 | TyVarTy cv => match coreVarToWeakVar cv with
34 | WExprVar _ => Error "encountered expression variable in a type"
35 | WTypeVar tv => OK (WTyVarTy tv)
36 | WCoerVar _ => Error "encountered coercion variable in a type"
39 | AppTy t1 t2 => coreTypeToWeakType' t2 >>= fun t2' => coreTypeToWeakType' t1 >>= fun t1' => OK (WAppTy t1' t2')
42 let recurse := ((fix rec tl := match tl with
44 | a::b => coreTypeToWeakType' a >>= fun a' => rec b >>= fun b' => OK (a'::b')
46 in match tyConOrTyFun tc_ with
47 | inr tf => recurse >>= fun recurse' => OK (WTyFunApp tf recurse')
48 | inl tc => if eqd_dec tc ModalBoxTyCon
50 | ((TyVarTy ec)::tbody::nil) =>
51 match coreVarToWeakVar ec with
52 | WTypeVar ec' => coreTypeToWeakType' tbody >>= fun tbody' => OK (WCodeTy ec' tbody')
53 | WExprVar _ => Error "encountered expression variable in a modal box type"
54 | WCoerVar _ => Error "encountered coercion variable in a modal box type"
56 | _ => Error ("mis-applied modal box tycon: " +++ ct)
58 else let tc' := if eqd_dec tc ArrowTyCon
61 in recurse >>= fun recurse' => OK (fold_left (fun x y => WAppTy x y) recurse' tc')
63 | FunTy (PredTy (EqPred t1 t2)) t3 => coreTypeToWeakType' t1 >>= fun t1' =>
64 coreTypeToWeakType' t2 >>= fun t2' =>
65 coreTypeToWeakType' t3 >>= fun t3' =>
66 OK (WCoFunTy t1' t2' t3')
67 | FunTy t1 t2 => coreTypeToWeakType' t1 >>= fun t1' =>
68 coreTypeToWeakType' t2 >>= fun t2' =>
69 OK (WAppTy (WAppTy WFunTyCon t1') t2')
70 | ForAllTy cv t => match coreVarToWeakVar cv with
71 | WExprVar _ => Error "encountered expression variable in a type"
72 | WTypeVar tv => coreTypeToWeakType' t >>= fun t' => OK (WForAllTy tv t')
73 | WCoerVar _ => Error "encountered coercion variable in a type"
75 | PredTy (ClassP cl lct) => ((fix rec tl := match tl with
77 | a::b => coreTypeToWeakType' a >>= fun a' =>
78 rec b >>= fun b' => OK (a'::b')
79 end) lct) >>= fun lct' => OK (WClassP cl lct')
80 | PredTy (IParam ipn ct) => coreTypeToWeakType' ct >>= fun ct' => OK (WIParam ipn ct')
81 | PredTy (EqPred _ _) => Error "hit a bare EqPred"
84 Fixpoint coreTypeToWeakType t := addErrorMessage "coreTypeToWeakType" (coreTypeToWeakType' (coreViewDeep t)).
86 (* detects our crude Core-encoding of modal type introduction/elimination forms *)
87 Definition isBrak (ce:@CoreExpr CoreVar) : ??(WeakExprVar * WeakTypeVar * CoreType) :=
89 | (CoreEApp (CoreEApp (CoreEVar v) (CoreEType (TyVarTy ec))) (CoreEType tbody))
90 => if coreName_eq hetmet_brak_name (coreVarCoreName v) then
91 match coreVarToWeakVar ec with
94 | WTypeVar tv => match coreVarToWeakVar v with
95 | WExprVar v' => Some (v',tv,tbody)
102 Definition isEsc (ce:@CoreExpr CoreVar) : ??(WeakExprVar * WeakTypeVar * CoreType) :=
104 | (CoreEApp (CoreEApp (CoreEVar v) (CoreEType (TyVarTy ec))) (CoreEType tbody))
105 => if coreName_eq hetmet_esc_name (coreVarCoreName v) then
106 match coreVarToWeakVar ec with
108 | WTypeVar tv => match coreVarToWeakVar v with
109 | WExprVar v' => Some (v',tv,tbody)
117 Definition isCSP (ce:@CoreExpr CoreVar) : ??(WeakExprVar * WeakTypeVar * CoreType) :=
119 | (CoreEApp (CoreEApp (CoreEVar v) (CoreEType (TyVarTy ec))) (CoreEType tbody))
120 => if coreName_eq hetmet_csp_name (coreVarCoreName v) then
121 match coreVarToWeakVar ec with
123 | WTypeVar tv => match coreVarToWeakVar v with
124 | WExprVar v' => Some (v',tv,tbody)
132 (* if this is a (WAppTy (WAppTy ... (WTyCon tc) .. ) .. ), return (tc,[...]) *)
133 Fixpoint expectTyConApp (wt:WeakType)(acc:list WeakType) : ???(TyCon * list WeakType) :=
135 | WTyCon tc => OK (tc,acc)
136 | WAppTy t1 t2 => expectTyConApp t1 (t2::acc)
137 | WTyFunApp tc tys => Error ("expectTyConApp encountered TyFunApp: " +++ wt)
138 | _ => Error ("expectTyConApp encountered " +++ wt)
141 Fixpoint coreExprToWeakExpr (ce:@CoreExpr CoreVar) : ???WeakExpr :=
143 | CoreELit lit => OK (WELit lit)
144 | CoreENote n e => coreExprToWeakExpr e >>= fun e' => OK (WENote n e')
145 | CoreEType t => Error "encountered CoreEType in a position where an Expr should have been"
146 | CoreECast e co => coreExprToWeakExpr e >>= fun e' =>
147 OK (WECast e' (coreCoercionToWeakCoercion co))
149 | CoreEVar v => match coreVarToWeakVar v with
150 | WExprVar ev => OK (WEVar ev)
151 | WTypeVar _ => Error "found a type variable inside an EVar!"
152 | WCoerVar _ => Error "found a coercion variable inside an EVar!"
155 | CoreEApp e1 e2 => match isBrak e1 with
157 coreExprToWeakExpr e2 >>= fun e' =>
158 coreTypeToWeakType t >>= fun t' =>
159 OK (WEBrak v tv e' t')
160 | None => match isEsc e1 with
162 coreExprToWeakExpr e2 >>= fun e' =>
163 coreTypeToWeakType t >>= fun t' =>
164 OK (WEEsc v tv e' t')
165 | None => match isCSP e1 with
167 coreExprToWeakExpr e2 >>= fun e' =>
168 coreTypeToWeakType t >>= fun t' =>
169 OK (WECSP v tv e' t')
170 | None => coreExprToWeakExpr e1 >>= fun e1' =>
173 coreTypeToWeakType t >>= fun t' =>
175 | _ => coreExprToWeakExpr e2
176 >>= fun e2' => OK (WEApp e1' e2')
182 | CoreELam v e => match coreVarToWeakVar v with
183 | WExprVar ev => coreExprToWeakExpr e >>= fun e' => OK (WELam ev e')
184 | WTypeVar tv => coreExprToWeakExpr e >>= fun e' => OK (WETyLam tv e')
185 | WCoerVar cv => coreExprToWeakExpr e >>= fun e' => OK (WECoLam cv e')
188 | CoreELet (CoreNonRec v ve) e => match coreVarToWeakVar v with
189 | WExprVar ev => coreExprToWeakExpr ve >>= fun ve' =>
190 coreExprToWeakExpr e >>= fun e' => OK (WELet ev ve' e')
191 | WTypeVar _ => match e with
192 | CoreEType t => Error "saw a type-let"
193 | _ => Error "saw (ELet <tyvar> e) where e!=EType"
195 | WCoerVar _ => Error "saw an (ELet <coercionVar>)"
198 | CoreELet (CoreRec rb) e =>
199 ((fix coreExprToWeakExprList (cel:list (CoreVar * (@CoreExpr CoreVar))) : ???(list (WeakExprVar * WeakExpr)) :=
202 | (v',e')::t => coreExprToWeakExprList t >>= fun t' =>
203 match coreVarToWeakVar v' with
204 | WExprVar ev => coreExprToWeakExpr e' >>= fun e' => OK ((ev,e')::t')
205 | WTypeVar _ => Error "found a type variable in a recursive let"
206 | WCoerVar _ => Error "found a coercion variable in a recursive let"
208 end) rb) >>= fun rb' =>
209 coreExprToWeakExpr e >>= fun e' =>
210 OK (WELetRec (unleaves' rb') e')
212 | CoreECase e v tbranches alts =>
213 match coreVarToWeakVar v with
214 | WTypeVar _ => Error "found a type variable in a case"
215 | WCoerVar _ => Error "found a coercion variable in a case"
217 coreTypeToWeakType (coreTypeOfCoreExpr e) >>= fun te' =>
218 expectTyConApp te' nil >>= fun tca =>
219 let (tc,lt) := tca:(TyCon * list WeakType) in
220 ((fix mkBranches (branches: list (@triple CoreAltCon (list CoreVar) (@CoreExpr CoreVar)))
221 : ???(list (WeakAltCon*list WeakTypeVar*list WeakCoerVar*list WeakExprVar*WeakExpr)) :=
224 | (mkTriple alt vars e)::rest =>
225 mkBranches rest >>= fun rest' =>
226 coreExprToWeakExpr e >>= fun e' =>
228 | DEFAULT => OK ((WeakDEFAULT,nil,nil,nil,e')::rest')
229 | LitAlt lit => OK ((WeakLitAlt lit,nil,nil,nil,e')::rest')
230 | DataAlt dc => let vars := map coreVarToWeakVar vars in
231 OK (((WeakDataAlt dc),
232 (filter (map (fun x => match x with WTypeVar v => Some v | _ => None end) vars)),
233 (filter (map (fun x => match x with WCoerVar v => Some v | _ => None end) vars)),
234 (filter (map (fun x => match x with WExprVar v => Some v | _ => None end) vars)),
239 coreExprToWeakExpr e >>= fun scrutinee =>
240 coreTypeToWeakType tbranches >>= fun tbranches' =>
241 OK (WECase ev scrutinee tbranches' tc lt (unleaves branches))