CanItFail(..), EquationInfo(..), MatchResult(..),
EqnNo, EqnSet,
+ mkDsLet, mkDsLets,
+
cantFailMatchResult, extractMatchResult,
combineMatchResults,
adjustMatchResult, adjustMatchResultDs,
mkCoLetsMatchResult, mkGuardedMatchResult,
mkCoPrimCaseMatchResult, mkCoAlgCaseMatchResult,
- mkErrorAppDs,
+ mkErrorAppDs, mkNilExpr, mkConsExpr,
mkSelectorBinds, mkTupleExpr, mkTupleSelector,
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}
%************************************************************************
%* *
-%* 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}
%* *
%************************************************************************
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
mkCoLetsMatchResult :: [CoreBind] -> MatchResult -> MatchResult
mkCoLetsMatchResult binds match_result
- = adjustMatchResult (mkLets binds) match_result
+ = adjustMatchResult (mkDsLets binds) match_result
mkGuardedMatchResult :: CoreExpr -> MatchResult -> MatchResult
-- 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
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
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}
%************************************************************************
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
| 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
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
%* *
%************************************************************************
+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}
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
\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
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