store the scrutinee CoreVar in WeakExpr Case to simplify WeakExprToCoreExpr
[coq-hetmet.git] / src / HaskWeakToCore.v
index 24eedc6..cb3d7a3 100644 (file)
@@ -31,43 +31,38 @@ Variable trustMeCoercion           : CoreCoercion.
 Variable coreCoercionsAreReallyTypes : CoreCoercion -> CoreType.
   Extract Inlined Constant coreCoercionsAreReallyTypes => "(\x -> x)".
 
-Section HaskWeakToCore.
+Definition weakCoercionToCoreCoercion : WeakCoercion -> CoreCoercion :=
+   fun _ => trustMeCoercion.
 
-  (* the CoreVar for the "magic" bracket/escape identifiers must be created from within one of the monads *)
-  Context (hetmet_brak_var : CoreVar).
-  Context (hetmet_esc_var  : CoreVar).
 
-  Definition weakCoercionToCoreCoercion : WeakCoercion -> CoreCoercion :=
-    fun _ => trustMeCoercion.
-
-  Fixpoint weakExprToCoreExpr (f:Fresh unit (fun _ => WeakExprVar)) (me:WeakExpr) : @CoreExpr CoreVar :=
+  Fixpoint weakExprToCoreExpr (me:WeakExpr) : @CoreExpr CoreVar :=
   match me with
   | WEVar   (weakExprVar v _)            => CoreEVar  v
   | WELit   lit                          => CoreELit  lit
-  | WEApp   e1 e2                        => CoreEApp     (weakExprToCoreExpr f e1) (weakExprToCoreExpr f e2)
-  | WETyApp e t                          => CoreEApp     (weakExprToCoreExpr f e ) (CoreEType (weakTypeToCoreType t))
-  | WECoApp e co                         => CoreEApp     (weakExprToCoreExpr f e )
+  | WEApp   e1 e2                        => CoreEApp     (weakExprToCoreExpr e1) (weakExprToCoreExpr e2)
+  | WETyApp e t                          => CoreEApp     (weakExprToCoreExpr e ) (CoreEType (weakTypeToCoreType t))
+  | WECoApp e co                         => CoreEApp     (weakExprToCoreExpr e )
                                                            (CoreEType (coreCoercionsAreReallyTypes (weakCoercionToCoreCoercion co)))
-  | WENote  n e                          => CoreENote n  (weakExprToCoreExpr f e )
-  | WELam   (weakExprVar ev _  ) e       => CoreELam  ev (weakExprToCoreExpr f e )
-  | WETyLam (weakTypeVar tv _  ) e       => CoreELam  tv (weakExprToCoreExpr f e )
-  | WECoLam (weakCoerVar cv _ _ _) e     => CoreELam  cv (weakExprToCoreExpr f e )
-  | WECast  e co                         => CoreECast    (weakExprToCoreExpr f e ) (weakCoercionToCoreCoercion co)
+  | WENote  n e                          => CoreENote n  (weakExprToCoreExpr e )
+  | WELam   (weakExprVar ev _  ) e       => CoreELam  ev (weakExprToCoreExpr e )
+  | WETyLam (weakTypeVar tv _  ) e       => CoreELam  tv (weakExprToCoreExpr e )
+  | WECoLam (weakCoerVar cv _ _ _) e     => CoreELam  cv (weakExprToCoreExpr e )
+  | WECast  e co                         => CoreECast    (weakExprToCoreExpr e ) (weakCoercionToCoreCoercion co)
   | WEBrak  v (weakTypeVar ec _) e t     => fold_left CoreEApp
                                                    ((CoreEType (TyVarTy ec))::
                                                      (CoreEType (weakTypeToCoreType t))::
-                                                     (weakExprToCoreExpr f e)::
+                                                     (weakExprToCoreExpr e)::
                                                      nil)
                                                    (CoreEVar v)
   | WEEsc   v (weakTypeVar ec _) e t     => fold_left CoreEApp
                                                    ((CoreEType (TyVarTy ec))::
                                                      (CoreEType (weakTypeToCoreType t))::
-                                                     (weakExprToCoreExpr f e)::
+                                                     (weakExprToCoreExpr e)::
                                                      nil)
                                                    (CoreEVar v)
-  | WELet   (weakExprVar v _) ve e       => mkCoreLet      (CoreNonRec v (weakExprToCoreExpr f ve))  (weakExprToCoreExpr f e)
-  | WECase  e tbranches tc types alts    => let (v,f') := next _ _ f tt  in
-                                            CoreECase (weakExprToCoreExpr f' e) v (weakTypeToCoreType tbranches)
+  | WELet   (weakExprVar v _) ve e       => mkCoreLet      (CoreNonRec v (weakExprToCoreExpr ve))  (weakExprToCoreExpr e)
+  | WECase  vscrut e tbranches tc types alts  =>
+                                            CoreECase (weakExprToCoreExpr e) vscrut (weakTypeToCoreType tbranches)
                                               (sortAlts ((
                                                 fix mkCaseBranches (alts:Tree 
                                                   ??(AltCon*list WeakTypeVar*list WeakCoerVar*list WeakExprVar*WeakExpr)) :=
@@ -80,19 +75,19 @@ Section HaskWeakToCore.
                                                         (map (fun v:WeakTypeVar => weakVarToCoreVar v) tvars)
                                                         (map (fun v:WeakCoerVar => weakVarToCoreVar v) cvars))
                                                       (map (fun v:WeakExprVar => weakVarToCoreVar v) evars))
-                                                      (weakExprToCoreExpr f' e))::nil
+                                                      (weakExprToCoreExpr e))::nil
                                                 end
                                               ) alts))
   | WELetRec mlr e                       => CoreELet (CoreRec
                                                ((fix mkLetBindings (mlr:Tree ??(WeakExprVar * WeakExpr)) :=
                                                  match mlr with
                                                    | T_Leaf None                        => nil
-                                                   | T_Leaf (Some (weakExprVar cv _,e)) => (cv,(weakExprToCoreExpr f e))::nil
+                                                   | T_Leaf (Some (weakExprVar cv _,e)) => (cv,(weakExprToCoreExpr e))::nil
                                                    | T_Branch b1 b2                     => app (mkLetBindings b1) (mkLetBindings b2)
                                                  end) mlr))
-                                               (weakExprToCoreExpr f e)
+                                               (weakExprToCoreExpr e)
   end.
 
-End HaskWeakToCore.
+