[project @ 1996-04-25 16:31:20 by partain]
[ghc-hetmet.git] / ghc / compiler / coreSyn / CoreUtils.lhs
index 1a993e6..d3afc57 100644 (file)
@@ -9,19 +9,18 @@
 module CoreUtils (
        coreExprType, coreAltsType,
 
-       substCoreExpr
+       substCoreExpr, substCoreBindings
 
        , mkCoreIfThenElse
-       , mkErrorApp, escErrorMsg
        , argToExpr
        , unTagBinders, unTagBindersAlts
+       , manifestlyWHNF, manifestlyBottom
+       , maybeErrorApp
+       , nonErrorRHSs
+       , squashableDictishCcExpr
 {-     exprSmallEnoughToDup,
-       manifestlyWHNF, manifestlyBottom,
        coreExprArity,
        isWrapperFor,
-       maybeErrorApp,
-       nonErrorRHSs,
-       squashableDictishCcExpr,
 
 -}  ) where
 
@@ -31,38 +30,38 @@ import IdLoop       -- for pananoia-checking purposes
 import CoreSyn
 
 import CostCentre      ( isDictCC )
-import Id              ( idType, mkSysLocal,
+import Id              ( idType, mkSysLocal, getIdArity, isBottomingId,
                          addOneToIdEnv, growIdEnvList, lookupIdEnv,
                          isNullIdEnv, IdEnv(..),
                          GenId{-instances-}
                        )
+import IdInfo          ( arityMaybe )
 import Literal         ( literalType, isNoRepLit, Literal(..) )
-import Maybes          ( catMaybes )
+import Maybes          ( catMaybes, maybeToBool )
 import PprCore         ( GenCoreExpr{-instances-}, GenCoreArg{-instances-} )
 import PprStyle                ( PprStyle(..) )
-import PprType         ( GenType{-instances-}, GenTyVar{-instance-} )
+import PprType         ( GenType{-instances-} )
 import Pretty          ( ppAboves )
 import PrelInfo                ( trueDataCon, falseDataCon,
-                         augmentId, buildId,
-                         pAT_ERROR_ID
+                         augmentId, buildId
                        )
 import PrimOp          ( primOpType, PrimOp(..) )
 import SrcLoc          ( mkUnknownSrcLoc )
-import TyVar           ( isNullTyVarEnv, TyVarEnv(..), GenTyVar{-instances-} )
-import Type            ( mkFunTys, mkForAllTy, mkForAllUsageTy,
-                         getFunTy_maybe, applyTy, splitSigmaTy
+import TyVar           ( isNullTyVarEnv, TyVarEnv(..) )
+import Type            ( mkFunTys, mkForAllTy, mkForAllUsageTy, mkTyVarTy,
+                         getFunTy_maybe, applyTy, isPrimType,
+                         splitSigmaTy, splitFunTy, eqTy, applyTypeEnvToTy
                        )
-import Unique          ( Unique{-instances-} )
 import UniqSupply      ( initUs, returnUs, thenUs,
-                         mapUs, mapAndUnzipUs,
+                         mapUs, mapAndUnzipUs, getUnique,
                          UniqSM(..), UniqSupply
                        )
+import Usage           ( UVar(..) )
 import Util            ( zipEqual, panic, pprPanic, assertPanic )
 
 type TypeEnv = TyVarEnv Type
 applyUsage = panic "CoreUtils.applyUsage:ToDo"
 dup_binder = panic "CoreUtils.dup_binder"
-applyTypeEnvToTy = panic "CoreUtils.applyTypeEnvToTy"
 \end{code}
 
 %************************************************************************
@@ -130,7 +129,8 @@ default_ty (BindDefault _ rhs) = coreExprType rhs
 \end{code}
 
 \begin{code}
-applyTypeToArgs = panic "applyTypeToArgs"
+applyTypeToArgs op_ty args
+  = foldl applyTy op_ty [ ty | TyArg ty <- args ]
 \end{code}
 
 %************************************************************************
@@ -151,76 +151,49 @@ mkCoreIfThenElse guard then_expr else_expr
        NoDefault )
 \end{code}
 
-\begin{code}
-mkErrorApp :: Type -> Id -> String -> CoreExpr
-
-mkErrorApp ty str_var error_msg
-  = Let (NonRec str_var (Lit (NoRepStr (_PK_ error_msg)))) (
-    mkApp (Var pAT_ERROR_ID) [] [ty] [VarArg str_var])
-
-escErrorMsg [] = []
-escErrorMsg ('%':xs) = '%' : '%' : escErrorMsg xs
-escErrorMsg (x:xs)   = x : escErrorMsg xs
-\end{code}
-
 For making @Apps@ and @Lets@, we must take appropriate evasive
 action if the thing being bound has unboxed type.  @mkCoApp@ requires
-a name supply to do its work.  Other-monad code will call @mkCoApp@
-through its own interface function (e.g., the desugarer uses
-@mkCoAppDs@).
+a name supply to do its work.
 
-@mkCoApp@, @mkCoCon@ and @mkCoPrim@ also handle the
+@mkCoApps@, @mkCoCon@ and @mkCoPrim@ also handle the
 arguments-must-be-atoms constraint.
 
 \begin{code}
-{- LATER:
---mkCoApp :: CoreExpr -> CoreExpr -> UniqSM CoreExpr
+data CoreArgOrExpr
+  = AnArg   CoreArg
+  | AnExpr  CoreExpr
 
-mkCoApp e1 (Var v) = returnUs (App e1 (VarArg v))
-mkCoApp e1 (Lit l) = returnUs (App e1 (LitArg l))
-mkCoApp e1 e2
-  = let
-       e2_ty = coreExprType e2
-    in
-    panic "getUnique"  `thenUs` \ uniq ->
-    let
-       new_var = mkSysLocal SLIT("a") uniq e2_ty mkUnknownSrcLoc
-    in
-    returnUs (
-       mkCoLetUnboxedToCase (NonRec new_var e2)
-                            (App e1 (VarArg new_var))
-    )
--}
-\end{code}
+mkCoApps :: CoreExpr -> [CoreArgOrExpr] -> UniqSM CoreExpr
+mkCoCon  :: Id       -> [CoreArgOrExpr] -> UniqSM CoreExpr
+mkCoPrim :: PrimOp   -> [CoreArgOrExpr] -> UniqSM CoreExpr
 
-\begin{code}
-{-LATER
-mkCoCon  :: Id     -> [CoreExpr] -> UniqSM CoreExpr
-mkCoPrim :: PrimOp -> [CoreExpr] -> UniqSM CoreExpr
+mkCoApps fun args = co_thing (mkGenApp fun) args
+mkCoCon  con args = co_thing (Con  con)     args
+mkCoPrim  op args = co_thing (Prim op)      args 
 
-mkCoCon con args = mkCoThing (Con con) args
-mkCoPrim op args = mkCoThing (Prim op) args
+co_thing :: ([CoreArg] -> CoreExpr)
+        -> [CoreArgOrExpr]
+        -> UniqSM CoreExpr
 
-mkCoThing thing arg_exprs
+co_thing thing arg_exprs
   = mapAndUnzipUs expr_to_arg arg_exprs `thenUs` \ (args, maybe_binds) ->
     returnUs (mkCoLetsUnboxedToCase (catMaybes maybe_binds) (thing args))
   where
-    expr_to_arg :: CoreExpr
-              -> UniqSM (CoreArg, Maybe CoreBinding)
+    expr_to_arg :: CoreArgOrExpr
+               -> UniqSM (CoreArg, Maybe CoreBinding)
 
-    expr_to_arg (Var v) = returnUs (VarArg v, Nothing)
-    expr_to_arg (Lit l) = returnUs (LitArg l, Nothing)
-    expr_to_arg other_expr
+    expr_to_arg (AnArg  arg)     = returnUs (arg,      Nothing)
+    expr_to_arg (AnExpr (Var v)) = returnUs (VarArg v, Nothing)
+    expr_to_arg (AnExpr (Lit l)) = returnUs (LitArg l, Nothing)
+    expr_to_arg (AnExpr other_expr)
       = let
            e_ty = coreExprType other_expr
        in
-       panic "getUnique" `thenUs` \ uniq ->
+       getUnique `thenUs` \ uniq ->
        let
            new_var  = mkSysLocal SLIT("a") uniq e_ty mkUnknownSrcLoc
-           new_atom = VarArg new_var
        in
-       returnUs (new_atom, Just (NonRec new_var other_expr))
--}
+       returnUs (VarArg new_var, Just (NonRec new_var other_expr))
 \end{code}
 
 \begin{code}
@@ -233,18 +206,6 @@ argToExpr (LitArg lit) = Lit lit
 
 \begin{code}
 {- LATER:
---mkCoApps ::
---  GenCoreExpr val_bdr val_occ tyvar uvar ->
---  [GenCoreExpr val_bdr val_occ tyvar uvar] ->
---  UniqSM(GenCoreExpr val_bdr val_occ tyvar uvar)
-
-mkCoApps fun []  = returnUs fun
-mkCoApps fun (arg:args)
-  = mkCoApp fun arg `thenUs` \ new_fun ->
-    mkCoApps new_fun args
-\end{code}
-
-\begin{code}
 exprSmallEnoughToDup :: GenCoreExpr binder Id -> Bool
 
 exprSmallEnoughToDup (Con _ _ _)   = True      -- Could check # of args
@@ -252,13 +213,14 @@ exprSmallEnoughToDup (Prim op _ _) = not (fragilePrimOp op)       -- Could check # of
 exprSmallEnoughToDup (Lit lit) = not (isNoRepLit lit)
 
 exprSmallEnoughToDup expr  -- for now, just: <var> applied to <args>
-  = case (collectArgs expr) of { (fun, args) ->
+  = case (collectArgs expr) of { (fun, _, _, vargs) ->
     case fun of
       Var v -> v /= buildId
                 && v /= augmentId
-                && length args <= 6 -- or 10 or 1 or 4 or anything smallish.
+                && length vargs <= 6 -- or 10 or 1 or 4 or anything smallish.
       _       -> False
     }
+-}
 \end{code}
 Question (ADR): What is the above used for?  Is a _ccall_ really small
 enough?
@@ -269,29 +231,30 @@ errs on the conservative side (returning \tr{False})---I've probably
 left something out... [WDP]
 
 \begin{code}
-manifestlyWHNF :: GenCoreExpr bndr Id -> Bool
+manifestlyWHNF :: GenCoreExpr bndr Id tyvar uvar -> Bool
 
-manifestlyWHNF (Var _)     = True
-manifestlyWHNF (Lit _)     = True
-manifestlyWHNF (Con _ _ _) = True  -- ToDo: anything for Prim?
-manifestlyWHNF (Lam _ _)   = True
-manifestlyWHNF (CoTyLam _ e) = manifestlyWHNF e
-manifestlyWHNF (SCC _ e)   = manifestlyWHNF e
-manifestlyWHNF (Let _ e)   = False
-manifestlyWHNF (Case _ _)  = False
+manifestlyWHNF (Var _)   = True
+manifestlyWHNF (Lit _)   = True
+manifestlyWHNF (Con _ _)  = True
+manifestlyWHNF (SCC _ e)  = manifestlyWHNF e
+manifestlyWHNF (Let _ e)  = False
+manifestlyWHNF (Case _ _) = False
+
+manifestlyWHNF (Lam x e)  = if isValBinder x then True else manifestlyWHNF e
 
 manifestlyWHNF other_expr   -- look for manifest partial application
-  = case (collectArgs other_expr) of { (fun, args) ->
+  = case (collectArgs other_expr) of { (fun, _, _, vargs) ->
     case fun of
-      Var f -> let
-                   num_val_args = length [ a | (ValArg a) <- args ]
-                in
-                num_val_args == 0 ||           -- Just a type application of
-                                               -- a variable (f t1 t2 t3)
-                                               -- counts as WHNF
-                case (arityMaybe (getIdArity f)) of
-                  Nothing     -> False
-                  Just arity  -> num_val_args < arity
+      Var f ->  let
+                   num_val_args = length vargs
+               in
+               num_val_args == 0 -- Just a type application of
+                                 -- a variable (f t1 t2 t3);
+                                 -- counts as WHNF.
+               ||
+               case (arityMaybe (getIdArity f)) of
+                 Nothing     -> False
+                 Just arity  -> num_val_args < arity
 
       _ -> False
     }
@@ -303,17 +266,18 @@ some point.  It isn't a disaster if it errs on the conservative side
 (returning \tr{False}).
 
 \begin{code}
-manifestlyBottom :: GenCoreExpr bndr Id -> Bool
+manifestlyBottom :: GenCoreExpr bndr Id tyvar uvar -> Bool
 
 manifestlyBottom (Var v)     = isBottomingId v
 manifestlyBottom (Lit _)     = False
-manifestlyBottom (Con _ _ _) = False
-manifestlyBottom (Prim _ _ _)= False
-manifestlyBottom (Lam _ _)   = False  -- we do not assume \x.bottom == bottom. should we? ToDo
-manifestlyBottom (CoTyLam _ e) = manifestlyBottom e
+manifestlyBottom (Con  _ _)  = False
+manifestlyBottom (Prim _ _)  = False
 manifestlyBottom (SCC _ e)   = manifestlyBottom e
 manifestlyBottom (Let _ e)   = manifestlyBottom e
 
+  -- We do not assume \x.bottom == bottom:
+manifestlyBottom (Lam x e) = if isValBinder x then False else manifestlyBottom e
+
 manifestlyBottom (Case e a)
   = manifestlyBottom e
   || (case a of
@@ -329,17 +293,18 @@ manifestlyBottom (Case e a)
     mbdef (BindDefault _ e') = manifestlyBottom e'
 
 manifestlyBottom other_expr   -- look for manifest partial application
-  = case (collectArgs other_expr) of { (fun, args) ->
+  = case (collectArgs other_expr) of { (fun, _, _, _) ->
     case fun of
-      Var f | isBottomingId f -> True          -- Application of a function which
-                                               -- always gives bottom; we treat this as
-                                               -- a WHNF, because it certainly doesn't
-                                               -- need to be shared!
+      Var f | isBottomingId f -> True
+               -- Application of a function which always gives
+               -- bottom; we treat this as a WHNF, because it
+               -- certainly doesn't need to be shared!
       _ -> False
     }
 \end{code}
 
 \begin{code}
+{-LATER:
 coreExprArity
        :: (Id -> Maybe (GenCoreExpr bndr Id))
        -> GenCoreExpr bndr Id
@@ -371,7 +336,7 @@ Probably a little too HACKY [WDP].
 isWrapperFor :: CoreExpr -> Id -> Bool
 
 expr `isWrapperFor` var
-  = case (digForLambdas  expr) of { (_, _, args, body) -> -- lambdas off the front
+  = case (collectBinders  expr) of { (_, _, args, body) -> -- lambdas off the front
     unravel_casing args body
     --NO, THANKS: && not (null args)
     }
@@ -382,11 +347,11 @@ expr `isWrapperFor` var
 
     --------------
     unravel_casing case_ables (Case scrut alts)
-      = case (collectArgs scrut) of { (fun, args) ->
+      = case (collectArgs scrut) of { (fun, _, _, vargs) ->
        case fun of
          Var scrut_var -> let
                                answer =
-                                    scrut_var /= var && all (doesn't_mention var) args
+                                    scrut_var /= var && all (doesn't_mention var) vargs
                                  && scrut_var `is_elem` case_ables
                                  && unravel_alts case_ables alts
                             in
@@ -396,15 +361,15 @@ expr `isWrapperFor` var
        }
 
     unravel_casing case_ables other_expr
-      = case (collectArgs other_expr) of { (fun, args) ->
+      = case (collectArgs other_expr) of { (fun, _, _, vargs) ->
        case fun of
          Var wrkr -> let
                            answer =
                                -- DOESN'T WORK: wrkr == var's_worker
                                wrkr /= var
                             && isWorkerId wrkr
-                            && all (doesn't_mention var)  args
-                            && all (only_from case_ables) args
+                            && all (doesn't_mention var)  vargs
+                            && all (only_from case_ables) vargs
                        in
                        answer
 
@@ -501,23 +466,24 @@ Example:
 Notice that the \tr{<alts>} don't get duplicated.
 
 \begin{code}
-{- LATER:
-nonErrorRHSs :: GenCoreCaseAlts binder Id -> [GenCoreExpr binder Id]
+nonErrorRHSs :: GenCoreCaseAlts a Id TyVar UVar -> [GenCoreExpr a Id TyVar UVar]
 
-nonErrorRHSs alts = filter not_error_app (find_rhss alts)
+nonErrorRHSs alts
+  = filter not_error_app (find_rhss alts)
   where
-    find_rhss (AlgAlts  alts deflt) = [rhs | (_,_,rhs) <- alts] ++ deflt_rhs deflt
-    find_rhss (PrimAlts alts deflt) = [rhs | (_,rhs)   <- alts] ++ deflt_rhs deflt
+    find_rhss (AlgAlts  as deflt) = [rhs | (_,_,rhs) <- as] ++ deflt_rhs deflt
+    find_rhss (PrimAlts as deflt) = [rhs | (_,rhs)   <- as] ++ deflt_rhs deflt
 
     deflt_rhs NoDefault           = []
     deflt_rhs (BindDefault _ rhs) = [rhs]
 
-    not_error_app rhs = case maybeErrorApp rhs Nothing of
-                        Just _  -> False
-                        Nothing -> True
+    not_error_app rhs
+      = case (maybeErrorApp rhs Nothing) of
+         Just _  -> False
+         Nothing -> True
 \end{code}
 
-maybeErrorApp checkes whether an expression is of the form
+maybeErrorApp checks whether an expression is of the form
 
        error ty args
 
@@ -533,24 +499,24 @@ Here's where it is useful:
  ===>
                error ty' "Foo"
 
-where ty' is the type of any of the alternatives.
-You might think this never occurs, but see the comments on
-the definition of @singleAlt@.
+where ty' is the type of any of the alternatives.  You might think
+this never occurs, but see the comments on the definition of
+@singleAlt@.
 
-Note: we *avoid* the case where ty' might end up as a
-primitive type: this is very uncool (totally wrong).
+Note: we *avoid* the case where ty' might end up as a primitive type:
+this is very uncool (totally wrong).
 
-NOTICE: in the example above we threw away e1 and e2, but
-not the string "Foo".  How did we know to do that?
+NOTICE: in the example above we threw away e1 and e2, but not the
+string "Foo".  How did we know to do that?
 
-Answer: for now anyway, we only handle the case of a function
-whose type is of form
+Answer: for now anyway, we only handle the case of a function whose
+type is of form
 
        bottomingFn :: forall a. t1 -> ... -> tn -> a
                              ^---------------------^ NB!
 
-Furthermore, we only count a bottomingApp if the function is
-applied to more than n args.  If so, we transform:
+Furthermore, we only count a bottomingApp if the function is applied
+to more than n args.  If so, we transform:
 
        bottomingFn ty e1 ... en en+1 ... em
 to
@@ -559,47 +525,47 @@ to
 That is, we discard en+1 .. em
 
 \begin{code}
-maybeErrorApp :: GenCoreExpr bndr Id   -- Expr to look at
-             -> Maybe Type         -- Just ty => a result type *already cloned*;
-                                   -- Nothing => don't know result ty; we
-                                   -- *pretend* that the result ty won't be
-                                   -- primitive -- somebody later must
-                                   -- ensure this.
-              -> Maybe (GenCoreExpr bndr Id)
+maybeErrorApp
+       :: GenCoreExpr a Id TyVar UVar  -- Expr to look at
+       -> Maybe Type                   -- Just ty => a result type *already cloned*;
+                                       -- Nothing => don't know result ty; we
+                                       -- *pretend* that the result ty won't be
+                                       -- primitive -- somebody later must
+                                       -- ensure this.
+       -> Maybe (GenCoreExpr a Id TyVar UVar)
 
 maybeErrorApp expr result_ty_maybe
-  = case collectArgs expr of
-      (Var fun, (TypeArg ty : other_args))
+  = case (collectArgs expr) of
+      (Var fun, [{-no usage???-}], [ty], other_args)
        | isBottomingId fun
        && maybeToBool result_ty_maybe -- we *know* the result type
                                       -- (otherwise: live a fairy-tale existence...)
        && not (isPrimType result_ty) ->
-       case splitSigmaTy (idType fun) of
-         ([tyvar_tmpl], [], tau_ty) ->
-             case (splitTyArgs tau_ty) of { (arg_tys, res_ty) ->
+
+       case (splitSigmaTy (idType fun)) of
+         ([tyvar], [], tau_ty) ->
+             case (splitFunTy tau_ty) of { (arg_tys, res_ty) ->
              let
                  n_args_to_keep = length arg_tys
                  args_to_keep   = take n_args_to_keep other_args
              in
-             if  res_ty == mkTyVarTemplateTy tyvar_tmpl &&
-                 n_args_to_keep <= length other_args
+             if  (res_ty `eqTy` mkTyVarTy tyvar)
+              && n_args_to_keep <= length other_args
              then
                    -- Phew!  We're in business
-                 Just (mkGenApp (Var fun)
-                             (TypeArg result_ty : args_to_keep))
+                 Just (mkGenApp (Var fun) (TyArg result_ty : args_to_keep))
              else
                  Nothing
              }
 
-         other ->      -- Function type wrong shape
-                   Nothing
+         other -> Nothing  -- Function type wrong shape
       other -> Nothing
   where
     Just result_ty = result_ty_maybe
 \end{code}
 
 \begin{code}
-squashableDictishCcExpr :: CostCentre -> GenCoreExpr a b -> Bool
+squashableDictishCcExpr :: CostCentre -> GenCoreExpr a b c d -> Bool
 
 squashableDictishCcExpr cc expr
   = if not (isDictCC cc) then
@@ -608,11 +574,11 @@ squashableDictishCcExpr cc expr
        squashable expr -- note: quite like the "atomic_rhs" stuff in simplifier
   where
     squashable (Var _)      = True
-    squashable (CoTyApp f _)  = squashable f
-    squashable (Con _ _ _)  = True -- I think so... WDP 94/09
-    squashable (Prim _ _ _) = True -- ditto
-    squashable other         = False
--}
+    squashable (Con  _ _)   = True -- I think so... WDP 94/09
+    squashable (Prim _ _)   = True -- ditto
+    squashable (App f a)
+      | notValArg a        = squashable f
+    squashable other       = False
 \end{code}
 
 %************************************************************************
@@ -622,14 +588,25 @@ squashableDictishCcExpr cc expr
 %************************************************************************
 
 \begin{code}
+substCoreBindings :: ValEnv
+               -> TypeEnv -- TyVar=>Type
+               -> [CoreBinding]
+               -> UniqSM [CoreBinding]
+
 substCoreExpr  :: ValEnv
                -> TypeEnv -- TyVar=>Type
                -> CoreExpr
                -> UniqSM CoreExpr
 
-substCoreExpr venv tenv expr
+substCoreBindings venv tenv binds
   -- if the envs are empty, then avoid doing anything
   = if (isNullIdEnv venv && isNullTyVarEnv tenv) then
+       returnUs binds
+    else
+       do_CoreBindings venv tenv binds
+
+substCoreExpr venv tenv expr
+  = if (isNullIdEnv venv && isNullTyVarEnv tenv) then
        returnUs expr
     else
        do_CoreExpr venv tenv expr
@@ -688,18 +665,19 @@ do_CoreBinding venv tenv (Rec binds)
 do_CoreArg :: ValEnv
            -> TypeEnv
            -> CoreArg
-           -> UniqSM CoreExpr
+           -> UniqSM CoreArgOrExpr
 
-do_CoreArg venv tenv (LitArg lit)     = returnUs (Lit lit)
-do_CoreArg venv tenv (TyArg ty)              = panic "do_CoreArg: TyArg"
-do_CoreArg venv tenv (UsageArg usage) = panic "do_CoreArg: UsageArg"
-do_CoreArg venv tenv (VarArg v)
+do_CoreArg venv tenv a@(VarArg v)
   = returnUs (
       case (lookupIdEnv venv v) of
-       Nothing   -> --false:ASSERT(toplevelishId v)
-                    Var v
-       Just expr -> expr
+       Nothing   -> AnArg  a
+       Just expr -> AnExpr expr
     )
+
+do_CoreArg venv tenv (TyArg ty)
+  = returnUs (AnArg (TyArg (applyTypeEnvToTy tenv ty)))
+
+do_CoreArg venv tenv other_arg = returnUs (AnArg other_arg)
 \end{code}
 
 \begin{code}
@@ -719,15 +697,10 @@ do_CoreExpr venv tenv orig_expr@(Var var)
 do_CoreExpr venv tenv e@(Lit _) = returnUs e
 
 do_CoreExpr venv tenv (Con con as)
-  = panic "CoreUtils.do_CoreExpr:Con"
-{- LATER:
   = mapUs  (do_CoreArg venv tenv) as `thenUs`  \ new_as ->
     mkCoCon con new_as
--}
 
 do_CoreExpr venv tenv (Prim op as)
-  = panic "CoreUtils.do_CoreExpr:Prim"
-{- LATER:
   = mapUs  (do_CoreArg venv tenv) as   `thenUs`  \ new_as ->
     do_PrimOp op                       `thenUs`  \ new_op ->
     mkCoPrim new_op new_as
@@ -740,7 +713,6 @@ do_CoreExpr venv tenv (Prim op as)
        returnUs (CCallOp label is_asm may_gc new_arg_tys new_result_ty)
 
     do_PrimOp other_op = returnUs other_op
--}
 
 do_CoreExpr venv tenv (Lam binder expr)
   = dup_binder tenv binder `thenUs` \(new_binder, (old,new)) ->
@@ -749,12 +721,9 @@ do_CoreExpr venv tenv (Lam binder expr)
     returnUs (Lam new_binder new_expr)
 
 do_CoreExpr venv tenv (App expr arg)
-  = panic "CoreUtils.do_CoreExpr:App"
-{-
   = do_CoreExpr venv tenv expr `thenUs` \ new_expr ->
     do_CoreArg  venv tenv arg   `thenUs` \ new_arg  ->
-    mkCoApp new_expr new_arg
--}
+    mkCoApps new_expr [new_arg] -- ToDo: more efficiently?
 
 do_CoreExpr venv tenv (Case expr alts)
   = do_CoreExpr venv tenv expr     `thenUs` \ new_expr ->