module DsExpr ( dsExpr ) where
-import Ubiq
-import DsLoop -- partly to get dsBinds, partly to chk dsExpr
-
-import HsSyn ( HsExpr(..), HsLit(..), ArithSeqInfo(..),
- Match, Qual, HsBinds, Stmt, PolyType )
-import TcHsSyn ( TypecheckedHsExpr(..), TypecheckedHsBinds(..) )
+IMP_Ubiq()
+IMPORT_DELOOPER(DsLoop) -- partly to get dsBinds, partly to chk dsExpr
+
+import HsSyn ( failureFreePat,
+ HsExpr(..), OutPat(..), HsLit(..), ArithSeqInfo(..),
+ Stmt(..), DoOrListComp(..), Match(..), HsBinds, HsType, Fixity,
+ GRHSsAndBinds
+ )
+import TcHsSyn ( SYN_IE(TypecheckedHsExpr), SYN_IE(TypecheckedHsBinds),
+ SYN_IE(TypecheckedRecordBinds), SYN_IE(TypecheckedPat),
+ SYN_IE(TypecheckedStmt)
+ )
import CoreSyn
import DsMonad
import DsCCall ( dsCCall )
+import DsHsSyn ( outPatType )
import DsListComp ( dsListComp )
-import DsUtils ( mkAppDs, mkConDs, mkPrimDs, dsExprToAtom )
+import DsUtils ( mkAppDs, mkConDs, mkPrimDs, dsExprToAtom, mkTupleExpr,
+ mkErrorAppDs, showForErr, EquationInfo,
+ MatchResult, SYN_IE(DsCoreArg)
+ )
import Match ( matchWrapper )
-import CoreUnfold ( UnfoldingDetails(..), UnfoldingGuidance(..),
- FormSummary )
import CoreUtils ( coreExprType, substCoreExpr, argToExpr,
mkCoreIfThenElse, unTagBinders )
import CostCentre ( mkUserCC )
-import Id ( mkTupleCon, idType, nullIdEnv, addOneToIdEnv,
- getIdUnfolding )
+import FieldLabel ( fieldLabelType, FieldLabel )
+import Id ( idType, nullIdEnv, addOneToIdEnv,
+ dataConArgTys, dataConFieldLabels,
+ recordSelectorFieldLabel, SYN_IE(Id)
+ )
import Literal ( mkMachInt, Literal(..) )
-import MagicUFs ( MagicUnfoldingFun )
+import Name ( Name{--O only-} )
import PprStyle ( PprStyle(..) )
-import PprType ( GenType, GenTyVar )
-import PrelInfo ( mkTupleTy, unitTy, nilDataCon, consDataCon,
- charDataCon, charTy )
-import Pretty ( ppShow )
-import Type ( splitSigmaTy )
-import TyVar ( nullTyVarEnv, addOneToTyVarEnv, GenTyVar )
-import Unique ( Unique )
-import Usage ( UVar(..) )
-import Util ( panic )
-
-primRepFromType = panic "DsExpr.primRepFromType"
-maybeBoxedPrimType = panic "DsExpr.maybeBoxedPrimType"
-splitTyArgs = panic "DsExpr.splitTyArgs"
+import PprType ( GenType )
+import PrelVals ( rEC_CON_ERROR_ID, rEC_UPD_ERROR_ID, voidId )
+import Pretty ( Doc, hcat, ptext, text )
+import TyCon ( isDataTyCon, isNewTyCon )
+import Type ( splitSigmaTy, splitFunTy, typePrimRep,
+ getAppDataTyConExpandingDicts, maybeAppTyCon, getAppTyCon, applyTy,
+ maybeBoxedPrimType, splitAppTy, SYN_IE(Type)
+ )
+import TysPrim ( voidTy )
+import TysWiredIn ( mkTupleTy, tupleCon, nilDataCon, consDataCon, listTyCon,
+ charDataCon, charTy
+ )
+import TyVar ( nullTyVarEnv, addOneToTyVarEnv, GenTyVar{-instance Eq-} )
+import Usage ( SYN_IE(UVar) )
+import Maybes ( maybeToBool )
+import Util ( zipEqual, pprError, panic, assertPanic )
+
+#if __GLASGOW_HASKELL__ >= 202
+import Outputable
+#endif
mk_nil_con ty = mkCon nilDataCon [] [ty] [] -- micro utility...
\end{code}
\begin{code}
dsExpr :: TypecheckedHsExpr -> DsM CoreExpr
-dsExpr (HsVar var) = dsApp (HsVar var) []
+dsExpr e@(HsVar var) = dsId var
\end{code}
%************************************************************************
the_char = mkCon charDataCon [] [] [LitArg (MachChar (_HEAD_ s))]
the_nil = mk_nil_con charTy
in
- mkConDs consDataCon [charTy] [the_char, the_nil]
+ mkConDs consDataCon [TyArg charTy, VarArg the_char, VarArg the_nil]
-- "_" => build (\ c n -> c 'c' n) -- LATER
-- "str" ==> build (\ c n -> foldr charTy T c n "str")
{- LATER:
-dsExpr (HsLitOut (HsString str) _) =
- newTyVarsDs [alphaTyVar] `thenDs` \ [new_tyvar] ->
+dsExpr (HsLitOut (HsString str) _)
+ = newTyVarsDs [alphaTyVar] `thenDs` \ [new_tyvar] ->
let
new_ty = mkTyVarTy new_tyvar
in
where
(data_con, kind)
= case (maybeBoxedPrimType ty) of
- Nothing
- -> error ("ERROR: ``literal-literal'' not a single-constructor type: "++ _UNPK_ s ++"; type: "++(ppShow 80 (ppr PprDebug ty)))
Just (boxing_data_con, prim_ty)
- -> (boxing_data_con, primRepFromType prim_ty)
+ -> (boxing_data_con, typePrimRep prim_ty)
+ Nothing
+ -> pprError "ERROR: ``literal-literal'' not a single-constructor type: "
+ (hcat [ptext s, text "; type: ", ppr PprDebug ty])
-dsExpr (HsLitOut (HsInt i) _)
- = returnDs (Lit (NoRepInteger i))
+dsExpr (HsLitOut (HsInt i) ty)
+ = returnDs (Lit (NoRepInteger i ty))
-dsExpr (HsLitOut (HsFrac r) _)
- = returnDs (Lit (NoRepRational r))
+dsExpr (HsLitOut (HsFrac r) ty)
+ = returnDs (Lit (NoRepRational r ty))
-- others where we know what to do:
-- end of literals magic. --
dsExpr expr@(HsLam a_Match)
- = let
- error_msg = "%L" --> "pattern-matching failed in lambda"
- in
- matchWrapper LambdaMatch [a_Match] error_msg `thenDs` \ (binders, matching_code) ->
+ = matchWrapper LambdaMatch [a_Match] "lambda" `thenDs` \ (binders, matching_code) ->
returnDs ( mkValLam binders matching_code )
-dsExpr expr@(HsApp e1 e2) = dsApp expr []
-dsExpr expr@(OpApp e1 op e2) = dsApp expr []
+dsExpr expr@(HsApp e1 e2) = dsApp expr []
+dsExpr expr@(OpApp e1 op _ e2) = dsApp expr []
\end{code}
Operator sections. At first it looks as if we can convert
dsExpr (SectionL expr op)
= dsExpr op `thenDs` \ core_op ->
dsExpr expr `thenDs` \ core_expr ->
- dsExprToAtom core_expr $ \ y_atom ->
+ dsExprToAtom (VarArg core_expr) $ \ y_atom ->
-- for the type of x, we need the type of op's 2nd argument
let
x_ty = case (splitSigmaTy (coreExprType core_op)) of { (_, _, tau_ty) ->
- case (splitTyArgs tau_ty) of {
+ case (splitFunTy tau_ty) of {
((_:arg2_ty:_), _) -> arg2_ty;
- _ -> panic "dsExpr:SectionL:arg 2 ty"
- }}
+ _ -> panic "dsExpr:SectionL:arg 2 ty" }}
in
newSysLocalDs x_ty `thenDs` \ x_id ->
returnDs (mkValLam [x_id] (core_op `App` y_atom `App` VarArg x_id))
dsExpr (SectionR op expr)
= dsExpr op `thenDs` \ core_op ->
dsExpr expr `thenDs` \ core_expr ->
- dsExprToAtom core_expr $ \ y_atom ->
+ dsExprToAtom (VarArg core_expr) $ \ y_atom ->
-- for the type of x, we need the type of op's 1st argument
let
x_ty = case (splitSigmaTy (coreExprType core_op)) of { (_, _, tau_ty) ->
- case (splitTyArgs tau_ty) of {
+ case (splitFunTy tau_ty) of {
((arg1_ty:_), _) -> arg1_ty;
- _ -> panic "dsExpr:SectionR:arg 1 ty"
- }}
+ _ -> panic "dsExpr:SectionR:arg 1 ty" }}
in
newSysLocalDs x_ty `thenDs` \ x_id ->
returnDs (mkValLam [x_id] (core_op `App` VarArg x_id `App` y_atom))
dsExpr expr@(HsCase discrim matches src_loc)
= putSrcLocDs src_loc $
- dsExpr discrim `thenDs` \ core_discrim ->
- let
- error_msg = "%C" --> "pattern-matching failed in case"
- in
- matchWrapper CaseMatch matches error_msg `thenDs` \ ([discrim_var], matching_code) ->
+ dsExpr discrim `thenDs` \ core_discrim ->
+ matchWrapper CaseMatch matches "case" `thenDs` \ ([discrim_var], matching_code) ->
returnDs ( mkCoLetAny (NonRec discrim_var core_discrim) matching_code )
-dsExpr (ListComp expr quals)
- = dsExpr expr `thenDs` \ core_expr ->
- dsListComp core_expr quals
-
dsExpr (HsLet binds expr)
= dsBinds binds `thenDs` \ core_binds ->
dsExpr expr `thenDs` \ core_expr ->
returnDs ( mkCoLetsAny core_binds core_expr )
-dsExpr (HsDoOut stmts m_id mz_id src_loc)
+dsExpr (HsDoOut do_or_lc stmts return_id then_id zero_id result_ty src_loc)
+ | maybeToBool maybe_list_comp
+ = -- Special case for list comprehensions
+ putSrcLocDs src_loc $
+ dsListComp stmts elt_ty
+
+ | otherwise
+ = putSrcLocDs src_loc $
+ dsDo do_or_lc stmts return_id then_id zero_id result_ty
+ where
+ maybe_list_comp
+ = case (do_or_lc, maybeAppTyCon result_ty) of
+ (ListComp, Just (tycon, [elt_ty]))
+ | tycon == listTyCon
+ -> Just elt_ty
+ other -> Nothing
+ -- We need the ListComp form to use deListComp (rather than the "do" form)
+ -- because the "return" in a do block is a call to "PrelBase.return", and
+ -- not a ReturnStmt. Only the ListComp form has ReturnStmts
+
+ Just elt_ty = maybe_list_comp
+
+dsExpr (HsIf guard_expr then_expr else_expr src_loc)
= putSrcLocDs src_loc $
- panic "dsExpr:HsDoOut"
+ dsExpr guard_expr `thenDs` \ core_guard ->
+ dsExpr then_expr `thenDs` \ core_then ->
+ dsExpr else_expr `thenDs` \ core_else ->
+ returnDs (mkCoreIfThenElse core_guard core_then core_else)
+\end{code}
+
+Type lambda and application
+~~~~~~~~~~~~~~~~~~~~~~~~~~~
+\begin{code}
+dsExpr (TyLam tyvars expr)
+ = dsExpr expr `thenDs` \ core_expr ->
+ returnDs (mkTyLam tyvars core_expr)
+
+dsExpr expr@(TyApp e tys) = dsApp expr []
+\end{code}
+
+
+Various data construction things
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+\begin{code}
dsExpr (ExplicitListOut ty xs)
= case xs of
[] -> returnDs (mk_nil_con ty)
(y:ys) ->
dsExpr y `thenDs` \ core_hd ->
dsExpr (ExplicitListOut ty ys) `thenDs` \ core_tl ->
- mkConDs consDataCon [ty] [core_hd, core_tl]
+ mkConDs consDataCon [TyArg ty, VarArg core_hd, VarArg core_tl]
dsExpr (ExplicitTuple expr_list)
= mapDs dsExpr expr_list `thenDs` \ core_exprs ->
- mkConDs (mkTupleCon (length expr_list))
- (map coreExprType core_exprs)
- core_exprs
-
-dsExpr (RecordCon con rbinds) = panic "dsExpr:RecordCon"
-dsExpr (RecordUpd aexp rbinds) = panic "dsExpr:RecordUpd"
-
-dsExpr (HsIf guard_expr then_expr else_expr src_loc)
- = putSrcLocDs src_loc $
- dsExpr guard_expr `thenDs` \ core_guard ->
- dsExpr then_expr `thenDs` \ core_then ->
- dsExpr else_expr `thenDs` \ core_else ->
- returnDs (mkCoreIfThenElse core_guard core_then core_else)
+ mkConDs (tupleCon (length expr_list))
+ (map (TyArg . coreExprType) core_exprs ++ map VarArg core_exprs)
dsExpr (ArithSeqOut expr (From from))
= dsExpr expr `thenDs` \ expr2 ->
dsExpr from `thenDs` \ from2 ->
- mkAppDs expr2 [] [from2]
+ mkAppDs expr2 [VarArg from2]
dsExpr (ArithSeqOut expr (FromTo from two))
= dsExpr expr `thenDs` \ expr2 ->
dsExpr from `thenDs` \ from2 ->
dsExpr two `thenDs` \ two2 ->
- mkAppDs expr2 [] [from2, two2]
+ mkAppDs expr2 [VarArg from2, VarArg two2]
dsExpr (ArithSeqOut expr (FromThen from thn))
= dsExpr expr `thenDs` \ expr2 ->
dsExpr from `thenDs` \ from2 ->
dsExpr thn `thenDs` \ thn2 ->
- mkAppDs expr2 [] [from2, thn2]
+ mkAppDs expr2 [VarArg from2, VarArg thn2]
dsExpr (ArithSeqOut expr (FromThenTo from thn two))
= dsExpr expr `thenDs` \ expr2 ->
dsExpr from `thenDs` \ from2 ->
dsExpr thn `thenDs` \ thn2 ->
dsExpr two `thenDs` \ two2 ->
- mkAppDs expr2 [] [from2, thn2, two2]
+ mkAppDs expr2 [VarArg from2, VarArg thn2, VarArg two2]
\end{code}
+Record construction and update
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+For record construction we do this (assuming T has three arguments)
+
+ T { op2 = e }
+==>
+ let err = /\a -> recConErr a
+ T (recConErr t1 "M.lhs/230/op1")
+ e
+ (recConErr t1 "M.lhs/230/op3")
+
+recConErr then converts its arugment string into a proper message
+before printing it as
+
+ M.lhs, line 230: missing field op1 was evaluated
+
+
\begin{code}
-dsExpr (TyLam tyvars expr)
- = dsExpr expr `thenDs` \ core_expr ->
- returnDs (mkTyLam tyvars core_expr)
+dsExpr (RecordCon con_expr rbinds)
+ = dsExpr con_expr `thenDs` \ con_expr' ->
+ let
+ con_id = get_con con_expr'
+ (arg_tys, _) = splitFunTy (coreExprType con_expr')
+
+ mk_arg (arg_ty, lbl)
+ = case [rhs | (sel_id,rhs,_) <- rbinds,
+ lbl == recordSelectorFieldLabel sel_id] of
+ (rhs:rhss) -> ASSERT( null rhss )
+ dsExpr rhs
+ [] -> mkErrorAppDs rEC_CON_ERROR_ID arg_ty (showForErr lbl)
+ in
+ mapDs mk_arg (zipEqual "dsExpr:RecordCon" arg_tys (dataConFieldLabels con_id)) `thenDs` \ con_args ->
+ mkAppDs con_expr' (map VarArg con_args)
+ where
+ -- "con_expr'" is simply an application of the constructor Id
+ -- to types and (perhaps) dictionaries. This gets the constructor...
+ get_con (Var con) = con
+ get_con (App fun _) = get_con fun
+\end{code}
-dsExpr expr@(TyApp e tys) = dsApp expr []
+Record update is a little harder. Suppose we have the decl:
+
+ data T = T1 {op1, op2, op3 :: Int}
+ | T2 {op4, op2 :: Int}
+ | T3
+
+Then we translate as follows:
+
+ r { op2 = e }
+===>
+ let op2 = e in
+ case r of
+ T1 op1 _ op3 -> T1 op1 op2 op3
+ T2 op4 _ -> T2 op4 op2
+ other -> recUpdError "M.lhs/230"
+
+It's important that we use the constructor Ids for T1, T2 etc on the
+RHSs, and do not generate a Core Con directly, because the constructor
+might do some argument-evaluation first; and may have to throw away some
+dictionaries.
+
+\begin{code}
+dsExpr (RecordUpdOut record_expr record_out_ty dicts rbinds)
+ = dsExpr record_expr `thenDs` \ record_expr' ->
+
+ -- Desugar the rbinds, and generate let-bindings if
+ -- necessary so that we don't lose sharing
+ dsRbinds rbinds $ \ rbinds' ->
+ let
+ record_in_ty = coreExprType record_expr'
+ (tycon, in_inst_tys, cons) = getAppDataTyConExpandingDicts record_in_ty
+ (_, out_inst_tys, _) = getAppDataTyConExpandingDicts record_out_ty
+ cons_to_upd = filter has_all_fields cons
+
+ -- initial_args are passed to every constructor
+ initial_args = map TyArg out_inst_tys ++ map VarArg dicts
+
+ mk_val_arg (field, arg_id)
+ = case [arg | (f, arg) <- rbinds',
+ field == recordSelectorFieldLabel f] of
+ (arg:args) -> ASSERT(null args)
+ arg
+ [] -> VarArg arg_id
+
+ mk_alt con
+ = newSysLocalsDs (dataConArgTys con in_inst_tys) `thenDs` \ arg_ids ->
+ let
+ val_args = map mk_val_arg (zipEqual "dsExpr:RecordUpd" (dataConFieldLabels con) arg_ids)
+ in
+ returnDs (con, arg_ids, mkGenApp (mkGenApp (Var con) initial_args) val_args)
+
+ mk_default
+ | length cons_to_upd == length cons
+ = returnDs NoDefault
+ | otherwise
+ = newSysLocalDs record_in_ty `thenDs` \ deflt_id ->
+ mkErrorAppDs rEC_UPD_ERROR_ID record_out_ty "" `thenDs` \ err ->
+ returnDs (BindDefault deflt_id err)
+ in
+ mapDs mk_alt cons_to_upd `thenDs` \ alts ->
+ mk_default `thenDs` \ deflt ->
+
+ returnDs (Case record_expr' (AlgAlts alts deflt))
+
+ where
+ has_all_fields :: Id -> Bool
+ has_all_fields con_id
+ = all ok rbinds
+ where
+ con_fields = dataConFieldLabels con_id
+ ok (sel_id, _, _) = recordSelectorFieldLabel sel_id `elem` con_fields
\end{code}
+Dictionary lambda and application
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@DictLam@ and @DictApp@ turn into the regular old things.
(OLD:) @DictFunApp@ also becomes a curried application, albeit slightly more
complicated; reminiscent of fully-applied constructors.
\end{verbatim}
\begin{code}
dsExpr (SingleDict dict) -- just a local
- = lookupEnvWithDefaultDs dict (Var dict)
+ = lookupEnvDs dict `thenDs` \ dict' ->
+ returnDs (Var dict')
-dsExpr (Dictionary dicts methods)
- = -- hey, these things may have been substituted away...
- zipWithDs lookupEnvWithDefaultDs
- dicts_and_methods dicts_and_methods_exprs
- `thenDs` \ core_d_and_ms ->
+dsExpr (Dictionary [] []) -- Empty dictionary represented by void,
+ = returnDs (Var voidId) -- (not, as would happen if we took the next case, by ())
- (case num_of_d_and_ms of
- 0 -> returnDs cocon_unit -- unit
-
- 1 -> returnDs (head core_d_and_ms) -- just a single Id
-
- _ -> -- tuple 'em up
- mkConDs (mkTupleCon num_of_d_and_ms)
- (map coreExprType core_d_and_ms)
- core_d_and_ms
- )
- where
- dicts_and_methods = dicts ++ methods
- dicts_and_methods_exprs = map Var dicts_and_methods
- num_of_d_and_ms = length dicts_and_methods
+dsExpr (Dictionary dicts methods)
+ = mapDs lookupEnvDs (dicts ++ methods) `thenDs` \ d_and_ms' ->
+ returnDs (mkTupleExpr d_and_ms')
dsExpr (ClassDictLam dicts methods expr)
= dsExpr expr `thenDs` \ core_expr ->
case num_of_d_and_ms of
- 0 -> newSysLocalDs unitTy `thenDs` \ new_x ->
+ 0 -> newSysLocalDs voidTy `thenDs` \ new_x ->
returnDs (mkValLam [new_x] core_expr)
1 -> -- no untupling
where
num_of_d_and_ms = length dicts + length methods
dicts_and_methods = dicts ++ methods
- tuple_ty = mkTupleTy num_of_d_and_ms (map idType dicts_and_methods)
- tuple_con = mkTupleCon num_of_d_and_ms
+ tuple_ty = mkTupleTy num_of_d_and_ms (map idType dicts_and_methods)
+ tuple_con = tupleCon num_of_d_and_ms
#ifdef DEBUG
-- HsSyn constructs that just shouldn't be here:
-dsExpr (HsDo _ _) = panic "dsExpr:HsDo"
+dsExpr (HsDo _ _ _) = panic "dsExpr:HsDo"
dsExpr (ExplicitList _) = panic "dsExpr:ExplicitList"
dsExpr (ExprWithTySig _ _) = panic "dsExpr:ExprWithTySig"
dsExpr (ArithSeqIn _) = panic "dsExpr:ArithSeqIn"
#endif
-cocon_unit = mkCon (mkTupleCon 0) [] [] [] -- out here to avoid CAF (sigh)
out_of_range_msg -- ditto
= " out of range: [" ++ show minInt ++ ", " ++ show maxInt ++ "]\n"
\end{code}
possible).
\begin{code}
-type DsCoreArg = GenCoreArg CoreExpr{-NB!-} TyVar UVar
-
dsApp :: TypecheckedHsExpr -- expr to desugar
-> [DsCoreArg] -- accumulated ty/val args: NB:
-> DsM CoreExpr -- final result
= dsExpr e2 `thenDs` \ core_e2 ->
dsApp e1 (VarArg core_e2 : args)
-dsApp (OpApp e1 op e2) args
+dsApp (OpApp e1 op _ e2) args
= dsExpr e1 `thenDs` \ core_e1 ->
dsExpr e2 `thenDs` \ core_e2 ->
dsApp op (VarArg core_e1 : VarArg core_e2 : args)
dsApp (DictApp expr dicts) args
- = -- now, those dicts may have been substituted away...
- zipWithDs lookupEnvWithDefaultDs dicts (map Var dicts)
- `thenDs` \ core_dicts ->
- dsApp expr (map VarArg core_dicts ++ args)
+ = mapDs lookupEnvDs dicts `thenDs` \ core_dicts ->
+ dsApp expr (map (VarArg . Var) core_dicts ++ args)
dsApp (TyApp expr tys) args
= dsApp expr (map TyArg tys ++ args)
-- we might should look out for SectionLs, etc., here, but we don't
-dsApp (HsVar v) args
- = lookupEnvDs v `thenDs` \ maybe_expr ->
- case maybe_expr of
- Just expr -> apply_to_args expr args
-
- Nothing -> -- we're only saturating constructors and PrimOps
- case getIdUnfolding v of
- GenForm _ _ the_unfolding EssentialUnfolding
- -> do_unfold nullTyVarEnv nullIdEnv (unTagBinders the_unfolding) args
-
- _ -> apply_to_args (Var v) args
-
-
dsApp anything_else args
= dsExpr anything_else `thenDs` \ core_expr ->
- apply_to_args core_expr args
-
--- a DsM version of mkGenApp:
-apply_to_args :: CoreExpr -> [DsCoreArg] -> DsM CoreExpr
+ mkAppDs core_expr args
-apply_to_args fun args
- = let
- (ty_args, val_args) = foldr sep ([],[]) args
- in
- mkAppDs fun ty_args val_args
- where
- sep a@(LitArg l) (tys,vals) = (tys, (Lit l):vals)
- sep a@(VarArg e) (tys,vals) = (tys, e:vals)
- sep a@(TyArg ty) (tys,vals) = (ty:tys, vals)
- sep a@(UsageArg _) _ = panic "DsExpr:apply_to_args:UsageArg"
+dsId v
+ = lookupEnvDs v `thenDs` \ v' ->
+ returnDs (Var v')
\end{code}
\begin{code}
-do_unfold ty_env val_env (Lam (TyBinder tyvar) body) (TyArg ty : args)
- = do_unfold (addOneToTyVarEnv ty_env tyvar ty) val_env body args
+dsRbinds :: TypecheckedRecordBinds -- The field bindings supplied
+ -> ([(Id, CoreArg)] -> DsM CoreExpr) -- A continuation taking the field
+ -- bindings with atomic rhss
+ -> DsM CoreExpr -- The result of the continuation,
+ -- wrapped in suitable Lets
+
+dsRbinds [] continue_with
+ = continue_with []
+
+dsRbinds ((sel_id, rhs, pun_flag) : rbinds) continue_with
+ = dsExpr rhs `thenDs` \ rhs' ->
+ dsExprToAtom (VarArg rhs') $ \ rhs_atom ->
+ dsRbinds rbinds $ \ rbinds' ->
+ continue_with ((sel_id, rhs_atom) : rbinds')
+\end{code}
+
+\begin{code}
+-- do_unfold ty_env val_env (Lam (TyBinder tyvar) body) (TyArg ty : args)
+-- = do_unfold (addOneToTyVarEnv ty_env tyvar ty) val_env body args
+--
+-- do_unfold ty_env val_env (Lam (ValBinder binder) body) (arg@(VarArg expr) : args)
+-- = dsExprToAtom arg $ \ arg_atom ->
+-- do_unfold ty_env
+-- (addOneToIdEnv val_env binder (argToExpr arg_atom))
+-- body args
+--
+-- do_unfold ty_env val_env body args
+-- = -- Clone the remaining part of the template
+-- uniqSMtoDsM (substCoreExpr val_env ty_env body) `thenDs` \ body' ->
+--
+-- -- Apply result to remaining arguments
+-- mkAppDs body' args
+\end{code}
-do_unfold ty_env val_env (Lam (ValBinder binder) body) (VarArg expr : args)
- = dsExprToAtom expr $ \ arg_atom ->
- do_unfold ty_env
- (addOneToIdEnv val_env binder (argToExpr arg_atom))
- body args
+Basically does the translation given in the Haskell~1.3 report:
+\begin{code}
+dsDo :: DoOrListComp
+ -> [TypecheckedStmt]
+ -> Id -- id for: return m
+ -> Id -- id for: (>>=) m
+ -> Id -- id for: zero m
+ -> Type -- Element type; the whole expression has type (m t)
+ -> DsM CoreExpr
+
+dsDo do_or_lc stmts return_id then_id zero_id result_ty
+ = dsId return_id `thenDs` \ return_ds ->
+ dsId then_id `thenDs` \ then_ds ->
+ dsId zero_id `thenDs` \ zero_ds ->
+ let
+ (_, b_ty) = splitAppTy result_ty -- result_ty must be of the form (m b)
+
+ go [ReturnStmt expr]
+ = dsExpr expr `thenDs` \ expr2 ->
+ mkAppDs return_ds [TyArg b_ty, VarArg expr2]
+
+ go (GuardStmt expr locn : stmts)
+ = do_expr expr locn `thenDs` \ expr2 ->
+ go stmts `thenDs` \ rest ->
+ mkAppDs zero_ds [TyArg b_ty] `thenDs` \ zero_expr ->
+ returnDs (mkCoreIfThenElse expr2 rest zero_expr)
+
+ go (ExprStmt expr locn : stmts)
+ = do_expr expr locn `thenDs` \ expr2 ->
+ let
+ (_, a_ty) = splitAppTy (coreExprType expr2) -- Must be of form (m a)
+ in
+ if null stmts then
+ returnDs expr2
+ else
+ go stmts `thenDs` \ rest ->
+ newSysLocalDs a_ty `thenDs` \ ignored_result_id ->
+ mkAppDs then_ds [TyArg a_ty, TyArg b_ty, VarArg expr2,
+ VarArg (mkValLam [ignored_result_id] rest)]
+
+ go (LetStmt binds : stmts )
+ = dsBinds binds `thenDs` \ binds2 ->
+ go stmts `thenDs` \ rest ->
+ returnDs (mkCoLetsAny binds2 rest)
+
+ go (BindStmt pat expr locn : stmts)
+ = putSrcLocDs locn $
+ dsExpr expr `thenDs` \ expr2 ->
+ let
+ (_, a_ty) = splitAppTy (coreExprType expr2) -- Must be of form (m a)
+ zero_expr = TyApp (HsVar zero_id) [b_ty]
+ main_match = PatMatch pat (SimpleMatch (
+ HsDoOut do_or_lc stmts return_id then_id zero_id result_ty locn))
+ the_matches
+ = if failureFreePat pat
+ then [main_match]
+ else [main_match, PatMatch (WildPat a_ty) (SimpleMatch zero_expr)]
+ in
+ matchWrapper DoBindMatch the_matches match_msg
+ `thenDs` \ (binders, matching_code) ->
+ mkAppDs then_ds [TyArg a_ty, TyArg b_ty,
+ VarArg expr2, VarArg (mkValLam binders matching_code)]
+ in
+ go stmts
-do_unfold ty_env val_env body args
- = -- Clone the remaining part of the template
- uniqSMtoDsM (substCoreExpr val_env ty_env body) `thenDs` \ body' ->
+ where
+ do_expr expr locn = putSrcLocDs locn (dsExpr expr)
- -- Apply result to remaining arguments
- apply_to_args body' args
+ match_msg = case do_or_lc of
+ DoStmt -> "`do' statement"
+ ListComp -> "comprehension"
\end{code}