[project @ 1999-07-15 14:08:03 by keithw]
[ghc-hetmet.git] / ghc / compiler / deSugar / DsUtils.lhs
index 177b183..7cb082f 100644 (file)
@@ -10,13 +10,15 @@ module DsUtils (
        CanItFail(..), EquationInfo(..), MatchResult(..),
         EqnNo, EqnSet,
 
+       mkDsLet, mkDsLets,
+
        cantFailMatchResult, extractMatchResult,
        combineMatchResults, 
        adjustMatchResult, adjustMatchResultDs,
        mkCoLetsMatchResult, mkGuardedMatchResult, 
        mkCoPrimCaseMatchResult, mkCoAlgCaseMatchResult,
 
-       mkErrorAppDs,
+       mkErrorAppDs, mkNilExpr, mkConsExpr,
 
        mkSelectorBinds, mkTupleExpr, mkTupleSelector,
 
@@ -35,16 +37,19 @@ import CoreSyn
 import DsMonad
 
 import CoreUtils       ( coreExprType )
-import PrelVals                ( iRREFUT_PAT_ERROR_ID )
+import PrelInfo                ( iRREFUT_PAT_ERROR_ID )
 import Id              ( idType, Id, mkWildId )
 import Const           ( Literal(..), Con(..) )
 import TyCon           ( isNewTyCon, tyConDataCons )
-import DataCon         ( DataCon, dataConStrictMarks, dataConArgTys )
-import BasicTypes      ( StrictnessMark(..) )
+import DataCon         ( DataCon, StrictnessMark, maybeMarkedUnboxed, dataConStrictMarks, 
+                         dataConId, splitProductType_maybe
+                       )
 import Type            ( mkFunTy, isUnLiftedType, splitAlgTyConApp, unUsgTy,
                          Type
                        )
-import TysWiredIn      ( unitDataCon, tupleCon, stringTy, unitTy, unitDataCon )
+import TysWiredIn      ( unitDataCon, tupleCon, stringTy, unitTy, unitDataCon,
+                         nilDataCon, consDataCon
+                       )
 import UniqSet         ( mkUniqSet, minusUniqSet, isEmptyUniqSet, UniqSet )
 import Outputable
 \end{code}
@@ -52,7 +57,28 @@ import Outputable
 
 %************************************************************************
 %*                                                                     *
-%* Selecting match variables
+\subsection{ Building lets}
+%*                                                                     *
+%************************************************************************
+
+Use case, not let for unlifted types.  The simplifier will turn some
+back again.
+
+\begin{code}
+mkDsLet :: CoreBind -> CoreExpr -> CoreExpr
+mkDsLet (NonRec bndr rhs) body
+  | isUnLiftedType (idType bndr) = Case rhs bndr [(DEFAULT,[],body)]
+mkDsLet bind body
+  = Let bind body
+
+mkDsLets :: [CoreBind] -> CoreExpr -> CoreExpr
+mkDsLets binds body = foldr mkDsLet body binds
+\end{code}
+
+
+%************************************************************************
+%*                                                                     *
+\subsection{ Selecting match variables}
 %*                                                                     *
 %************************************************************************
 
@@ -127,7 +153,7 @@ extractMatchResult (MatchResult CantFail match_fn) fail_expr
 extractMatchResult (MatchResult CanFail match_fn) fail_expr
   = mkFailurePair fail_expr            `thenDs` \ (fail_bind, if_it_fails) ->
     match_fn if_it_fails               `thenDs` \ body ->
-    returnDs (Let fail_bind body)
+    returnDs (mkDsLet fail_bind body)
 
 
 combineMatchResults :: MatchResult -> MatchResult -> MatchResult
@@ -157,7 +183,7 @@ adjustMatchResultDs encl_fn (MatchResult can_it_fail body_fn)
 
 mkCoLetsMatchResult :: [CoreBind] -> MatchResult -> MatchResult
 mkCoLetsMatchResult binds match_result
-  = adjustMatchResult (mkLets binds) match_result
+  = adjustMatchResult (mkDsLets binds) match_result
 
 
 mkGuardedMatchResult :: CoreExpr -> MatchResult -> MatchResult
@@ -198,7 +224,8 @@ mkCoAlgCaseMatchResult var match_alts
        -- Stuff for newtype
     (con_id, arg_ids, match_result) = head match_alts
     arg_id                         = head arg_ids
-    coercion_bind                  = NonRec arg_id (Note (Coerce (idType arg_id) scrut_ty) (Var var))
+    coercion_bind                  = NonRec arg_id
+                       (Note (Coerce (unUsgTy (idType arg_id)) (unUsgTy scrut_ty)) (Var var))
     newtype_sanity                 = null (tail match_alts) && null (tail arg_ids)
 
        -- Stuff for data types
@@ -227,10 +254,12 @@ mkCoAlgCaseMatchResult var match_alts
     un_mentioned_constructors
         = mkUniqSet data_cons `minusUniqSet` mkUniqSet [ con | (con, _, _) <- match_alts]
     exhaustive_case = isEmptyUniqSet un_mentioned_constructors
-
--- for each constructor we match on, we might need to re-pack some
--- of the strict fields if they are unpacked in the constructor.
-
+\end{code}
+%
+For each constructor we match on, we might need to re-pack some
+of the strict fields if they are unpacked in the constructor.
+%
+\begin{code}
 rebuildConArgs
   :: DataCon                           -- the con we're matching on
   -> [Id]                              -- the source-level args
@@ -244,19 +273,20 @@ rebuildConArgs con (arg:args) stricts body | isTyVar arg
     returnDs (body',arg:args')
 rebuildConArgs con (arg:args) (str:stricts) body
   = rebuildConArgs con args stricts body `thenDs` \ (body', real_args) ->
-    case str of
-       MarkedUnboxed pack_con tys -> 
-           let id_tys  = dataConArgTys pack_con ty_args in
-           newSysLocalsDs id_tys `thenDs` \ unpacked_args ->
-           returnDs (
-                Let (NonRec arg (Con (DataCon pack_con) 
-                                     (map Type ty_args ++
-                                      map Var  unpacked_args))) body', 
-                unpacked_args ++ real_args
-           )
+    case maybeMarkedUnboxed str of
+       Just (pack_con1, _) -> 
+           case splitProductType_maybe (idType arg) of
+               Just (_, tycon_args, pack_con, con_arg_tys) ->
+                   ASSERT( pack_con == pack_con1 )
+                   newSysLocalsDs con_arg_tys          `thenDs` \ unpacked_args ->
+                   returnDs (
+                        mkDsLet (NonRec arg (Con (DataCon pack_con) 
+                                                 (map Type tycon_args ++
+                                                  map Var  unpacked_args))) body', 
+                        unpacked_args ++ real_args
+                   )
+               
        _ -> returnDs (body', arg:real_args)
-
-  where ty_args = case splitAlgTyConApp (idType arg) of { (_,args,_) -> args }
 \end{code}
 
 %************************************************************************
@@ -288,10 +318,10 @@ mkErrorAppDs err_id ty msg
 
 This is used in various places to do with lazy patterns.
 For each binder $b$ in the pattern, we create a binding:
-
+\begin{verbatim}
     b = case v of pat' -> b'
-
-where pat' is pat with each binder b cloned into b'.
+\end{verbatim}
+where @pat'@ is @pat@ with each binder @b@ cloned into @b'@.
 
 ToDo: making these bindings should really depend on whether there's
 much work to be done per binding.  If the pattern is complex, it
@@ -328,11 +358,15 @@ mkSelectorBinds pat val_expr
 
 
   | otherwise
-  = mkErrorAppDs iRREFUT_PAT_ERROR_ID tuple_ty (showSDoc (ppr pat))    `thenDs` \ error_expr ->
-    matchSimply val_expr LetMatch pat local_tuple error_expr   `thenDs` \ tuple_expr ->
-    newSysLocalDs tuple_ty                                     `thenDs` \ tuple_var ->
+  = mkErrorAppDs iRREFUT_PAT_ERROR_ID tuple_ty (showSDoc (ppr pat))
+    `thenDs` \ error_expr ->
+    matchSimply val_expr LetMatch pat local_tuple error_expr
+    `thenDs` \ tuple_expr ->
+    newSysLocalDs tuple_ty
+    `thenDs` \ tuple_var ->
     let
-       mk_tup_bind binder = (binder, mkTupleSelector binders binder tuple_var (Var tuple_var))
+       mk_tup_bind binder =
+         (binder, mkTupleSelector binders binder tuple_var (Var tuple_var))
     in
     returnDs ( (tuple_var, tuple_expr) : map mk_tup_bind binders )
   where
@@ -387,10 +421,10 @@ If there is just one id in the ``tuple'', then the selector is
 just the identity.
 
 \begin{code}
-mkTupleSelector :: [Id]                        -- The tuple args
-               -> Id                   -- The selected one
-               -> Id                   -- A variable of the same type as the scrutinee
-               -> CoreExpr             -- Scrutinee
+mkTupleSelector :: [Id]                -- The tuple args
+               -> Id           -- The selected one
+               -> Id           -- A variable of the same type as the scrutinee
+               -> CoreExpr     -- Scrutinee
                -> CoreExpr
 
 mkTupleSelector [var] should_be_the_same_var scrut_var scrut
@@ -409,6 +443,24 @@ mkTupleSelector vars the_var scrut_var scrut
 %*                                                                     *
 %************************************************************************
 
+Call the constructor Ids when building explicit lists, so that they
+interact well with rules.
+
+\begin{code}
+mkNilExpr :: Type -> CoreExpr
+mkNilExpr ty = App (Var (dataConId nilDataCon)) (Type ty)
+
+mkConsExpr :: Type -> CoreExpr -> CoreExpr -> CoreExpr
+mkConsExpr ty hd tl = mkApps (Var (dataConId consDataCon)) [Type ty, hd, tl]
+\end{code}
+
+
+%************************************************************************
+%*                                                                     *
+\subsection[mkFailurePair]{Code for pattern-matching and other failures}
+%*                                                                     *
+%************************************************************************
+
 Generally, we handle pattern matching failure like this: let-bind a
 fail-variable, and use that variable if the thing fails:
 \begin{verbatim}
@@ -423,7 +475,7 @@ fail-variable, and use that variable if the thing fails:
 Then
 \begin{itemize}
 \item
-If the case can't fail, then there'll be no mention of fail.33, and the
+If the case can't fail, then there'll be no mention of @fail.33@, and the
 simplifier will later discard it.
 
 \item
@@ -434,7 +486,7 @@ Only if it is used more than once will the let-binding remain.
 \end{itemize}
 
 There's a problem when the result of the case expression is of
-unboxed type.  Then the type of fail.33 is unboxed too, and
+unboxed type.  Then the type of @fail.33@ is unboxed too, and
 there is every chance that someone will change the let into a case:
 \begin{verbatim}
        case error "Help" of
@@ -455,7 +507,7 @@ for the primitive case:
                p4 -> ...
 \end{verbatim}
 
-Now fail.33 is a function, so it can be let-bound.
+Now @fail.33@ is a function, so it can be let-bound.
 
 \begin{code}
 mkFailurePair :: CoreExpr      -- Result type of the whole case expression