2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[DsUtils]{Utilities for desugaring}
6 This module exports some utility functions of no great interest.
10 CanItFail(..), EquationInfo(..), MatchResult(..),
17 cantFailMatchResult, extractMatchResult,
19 adjustMatchResult, adjustMatchResultDs,
20 mkCoLetsMatchResult, mkGuardedMatchResult,
21 mkCoPrimCaseMatchResult, mkCoAlgCaseMatchResult,
23 mkErrorAppDs, mkNilExpr, mkConsExpr, mkListExpr,
24 mkIntExpr, mkCharExpr,
25 mkStringLit, mkStringLitFS, mkIntegerExpr,
27 mkSelectorBinds, mkTupleExpr, mkTupleSelector, mkCoreTup,
32 #include "HsVersions.h"
34 import {-# SOURCE #-} Match ( matchSimply )
37 import TcHsSyn ( TypecheckedPat, hsPatType )
42 import CoreUtils ( exprType, mkIfThenElse, mkCoerce )
43 import PrelInfo ( iRREFUT_PAT_ERROR_ID )
44 import MkId ( mkReboxingAlt, mkNewTypeBody )
45 import Id ( idType, Id, mkWildId )
46 import Literal ( Literal(..), inIntRange, tARGET_MAX_INT )
47 import TyCon ( isNewTyCon, tyConDataCons )
48 import DataCon ( DataCon, dataConSourceArity )
49 import Type ( mkFunTy, isUnLiftedType, Type, splitTyConApp )
50 import TcType ( tcTyConAppTyCon, isIntTy, isFloatTy, isDoubleTy )
51 import TysPrim ( intPrimTy )
52 import TysWiredIn ( nilDataCon, consDataCon,
54 unitDataConId, unitTy,
56 intTy, intDataCon, smallIntegerDataCon,
59 stringTy, isPArrFakeCon )
60 import BasicTypes ( Boxity(..) )
61 import UniqSet ( mkUniqSet, minusUniqSet, isEmptyUniqSet, UniqSet )
62 import PrelNames ( unpackCStringName, unpackCStringUtf8Name,
63 plusIntegerName, timesIntegerName,
64 lengthPName, indexPName )
66 import UnicodeUtil ( intsToUtf8, stringToUtf8 )
67 import Util ( isSingleton, notNull )
73 %************************************************************************
75 \subsection{Tidying lit pats}
77 %************************************************************************
80 tidyLitPat :: HsLit -> TypecheckedPat -> TypecheckedPat
81 tidyLitPat (HsChar c) pat = mkCharLitPat c
82 tidyLitPat lit pat = pat
84 tidyNPat :: HsLit -> Type -> TypecheckedPat -> TypecheckedPat
85 tidyNPat (HsString s) _ pat
86 | lengthFS s <= 1 -- Short string literals only
87 = foldr (\c pat -> mkPrefixConPat consDataCon [mkCharLitPat c,pat] stringTy)
88 (mkNilPat stringTy) (unpackIntFS s)
89 -- The stringTy is the type of the whole pattern, not
90 -- the type to instantiate (:) or [] with!
93 tidyNPat lit lit_ty default_pat
94 | isIntTy lit_ty = mkPrefixConPat intDataCon [LitPat (mk_int lit)] lit_ty
95 | isFloatTy lit_ty = mkPrefixConPat floatDataCon [LitPat (mk_float lit)] lit_ty
96 | isDoubleTy lit_ty = mkPrefixConPat doubleDataCon [LitPat (mk_double lit)] lit_ty
97 | otherwise = default_pat
100 mk_int (HsInteger i) = HsIntPrim i
102 mk_float (HsInteger i) = HsFloatPrim (fromInteger i)
103 mk_float (HsRat f _) = HsFloatPrim f
105 mk_double (HsInteger i) = HsDoublePrim (fromInteger i)
106 mk_double (HsRat f _) = HsDoublePrim f
110 %************************************************************************
112 \subsection{Building lets}
114 %************************************************************************
116 Use case, not let for unlifted types. The simplifier will turn some
120 mkDsLet :: CoreBind -> CoreExpr -> CoreExpr
121 mkDsLet (NonRec bndr rhs) body
122 | isUnLiftedType (idType bndr) = Case rhs bndr [(DEFAULT,[],body)]
126 mkDsLets :: [CoreBind] -> CoreExpr -> CoreExpr
127 mkDsLets binds body = foldr mkDsLet body binds
131 %************************************************************************
133 \subsection{ Selecting match variables}
135 %************************************************************************
137 We're about to match against some patterns. We want to make some
138 @Ids@ to use as match variables. If a pattern has an @Id@ readily at
139 hand, which should indeed be bound to the pattern as a whole, then use it;
140 otherwise, make one up.
143 selectMatchVar :: TypecheckedPat -> DsM Id
144 selectMatchVar (VarPat var) = returnDs var
145 selectMatchVar (AsPat var pat) = returnDs var
146 selectMatchVar (LazyPat pat) = selectMatchVar pat
147 selectMatchVar other_pat = newSysLocalDs (hsPatType other_pat) -- OK, better make up one...
151 %************************************************************************
153 %* type synonym EquationInfo and access functions for its pieces *
155 %************************************************************************
156 \subsection[EquationInfo-synonym]{@EquationInfo@: a useful synonym}
158 The ``equation info'' used by @match@ is relatively complicated and
159 worthy of a type synonym and a few handy functions.
164 type EqnSet = UniqSet EqnNo
168 EqnNo -- The number of the equation
170 DsMatchContext -- The context info is used when producing warnings
171 -- about shadowed patterns. It's the context
172 -- of the *first* thing matched in this group.
173 -- Should perhaps be a list of them all!
175 [TypecheckedPat] -- The patterns for an eqn
177 MatchResult -- Encapsulates the guards and bindings
183 CanItFail -- Tells whether the failure expression is used
184 (CoreExpr -> DsM CoreExpr)
185 -- Takes a expression to plug in at the
186 -- failure point(s). The expression should
189 data CanItFail = CanFail | CantFail
191 orFail CantFail CantFail = CantFail
195 Functions on MatchResults
198 cantFailMatchResult :: CoreExpr -> MatchResult
199 cantFailMatchResult expr = MatchResult CantFail (\ ignore -> returnDs expr)
201 extractMatchResult :: MatchResult -> CoreExpr -> DsM CoreExpr
202 extractMatchResult (MatchResult CantFail match_fn) fail_expr
203 = match_fn (error "It can't fail!")
205 extractMatchResult (MatchResult CanFail match_fn) fail_expr
206 = mkFailurePair fail_expr `thenDs` \ (fail_bind, if_it_fails) ->
207 match_fn if_it_fails `thenDs` \ body ->
208 returnDs (mkDsLet fail_bind body)
211 combineMatchResults :: MatchResult -> MatchResult -> MatchResult
212 combineMatchResults (MatchResult CanFail body_fn1)
213 (MatchResult can_it_fail2 body_fn2)
214 = MatchResult can_it_fail2 body_fn
216 body_fn fail = body_fn2 fail `thenDs` \ body2 ->
217 mkFailurePair body2 `thenDs` \ (fail_bind, duplicatable_expr) ->
218 body_fn1 duplicatable_expr `thenDs` \ body1 ->
219 returnDs (Let fail_bind body1)
221 combineMatchResults match_result1@(MatchResult CantFail body_fn1) match_result2
225 adjustMatchResult :: (CoreExpr -> CoreExpr) -> MatchResult -> MatchResult
226 adjustMatchResult encl_fn (MatchResult can_it_fail body_fn)
227 = MatchResult can_it_fail (\fail -> body_fn fail `thenDs` \ body ->
228 returnDs (encl_fn body))
230 adjustMatchResultDs :: (CoreExpr -> DsM CoreExpr) -> MatchResult -> MatchResult
231 adjustMatchResultDs encl_fn (MatchResult can_it_fail body_fn)
232 = MatchResult can_it_fail (\fail -> body_fn fail `thenDs` \ body ->
236 mkCoLetsMatchResult :: [CoreBind] -> MatchResult -> MatchResult
237 mkCoLetsMatchResult binds match_result
238 = adjustMatchResult (mkDsLets binds) match_result
241 mkGuardedMatchResult :: CoreExpr -> MatchResult -> MatchResult
242 mkGuardedMatchResult pred_expr (MatchResult can_it_fail body_fn)
243 = MatchResult CanFail (\fail -> body_fn fail `thenDs` \ body ->
244 returnDs (mkIfThenElse pred_expr body fail))
246 mkCoPrimCaseMatchResult :: Id -- Scrutinee
247 -> [(Literal, MatchResult)] -- Alternatives
249 mkCoPrimCaseMatchResult var match_alts
250 = MatchResult CanFail mk_case
253 = mapDs (mk_alt fail) match_alts `thenDs` \ alts ->
254 returnDs (Case (Var var) var ((DEFAULT, [], fail) : alts))
256 mk_alt fail (lit, MatchResult _ body_fn) = body_fn fail `thenDs` \ body ->
257 returnDs (LitAlt lit, [], body)
260 mkCoAlgCaseMatchResult :: Id -- Scrutinee
261 -> [(DataCon, [CoreBndr], MatchResult)] -- Alternatives
264 mkCoAlgCaseMatchResult var match_alts
265 | isNewTyCon tycon -- Newtype case; use a let
266 = ASSERT( null (tail match_alts) && null (tail arg_ids) )
267 mkCoLetsMatchResult [NonRec arg_id newtype_rhs] match_result
269 | isPArrFakeAlts match_alts -- Sugared parallel array; use a literal case
270 = MatchResult CanFail mk_parrCase
272 | otherwise -- Datatype case; use a case
273 = MatchResult fail_flag mk_case
276 scrut_ty = idType var
277 tycon = tcTyConAppTyCon scrut_ty -- Newtypes must be opaque here
280 (_, arg_ids, match_result) = head match_alts
281 arg_id = head arg_ids
282 newtype_rhs = mkNewTypeBody tycon (idType arg_id) (Var var)
284 -- Stuff for data types
285 data_cons = tyConDataCons tycon
286 match_results = [match_result | (_,_,match_result) <- match_alts]
288 fail_flag | exhaustive_case
289 = foldr1 orFail [can_it_fail | MatchResult can_it_fail _ <- match_results]
293 wild_var = mkWildId (idType var)
294 mk_case fail = mapDs (mk_alt fail) match_alts `thenDs` \ alts ->
295 returnDs (Case (Var var) wild_var (mk_default fail ++ alts))
297 mk_alt fail (con, args, MatchResult _ body_fn)
298 = body_fn fail `thenDs` \ body ->
299 getUniquesDs `thenDs` \ us ->
300 returnDs (mkReboxingAlt us con args body)
302 mk_default fail | exhaustive_case = []
303 | otherwise = [(DEFAULT, [], fail)]
305 un_mentioned_constructors
306 = mkUniqSet data_cons `minusUniqSet` mkUniqSet [ con | (con, _, _) <- match_alts]
307 exhaustive_case = isEmptyUniqSet un_mentioned_constructors
309 -- Stuff for parallel arrays
311 -- * the following is to desugar cases over fake constructors for
312 -- parallel arrays, which are introduced by `tidy1' in the `PArrPat'
315 -- Concerning `isPArrFakeAlts':
317 -- * it is *not* sufficient to just check the type of the type
318 -- constructor, as we have to be careful not to confuse the real
319 -- representation of parallel arrays with the fake constructors;
320 -- moreover, a list of alternatives must not mix fake and real
321 -- constructors (this is checked earlier on)
323 -- FIXME: We actually go through the whole list and make sure that
324 -- either all or none of the constructors are fake parallel
325 -- array constructors. This is to spot equations that mix fake
326 -- constructors with the real representation defined in
327 -- `PrelPArr'. It would be nicer to spot this situation
328 -- earlier and raise a proper error message, but it can really
329 -- only happen in `PrelPArr' anyway.
331 isPArrFakeAlts [(dcon, _, _)] = isPArrFakeCon dcon
332 isPArrFakeAlts ((dcon, _, _):alts) =
333 case (isPArrFakeCon dcon, isPArrFakeAlts alts) of
334 (True , True ) -> True
335 (False, False) -> False
337 panic "DsUtils: You may not mix `[:...:]' with `PArr' patterns"
340 dsLookupGlobalId lengthPName `thenDs` \lengthP ->
341 unboxAlt `thenDs` \alt ->
342 returnDs (Case (len lengthP) (mkWildId intTy) [alt])
344 elemTy = case splitTyConApp (idType var) of
345 (_, [elemTy]) -> elemTy
347 panicMsg = "DsUtils.mkCoAlgCaseMatchResult: not a parallel array?"
348 len lengthP = mkApps (Var lengthP) [Type elemTy, Var var]
351 newSysLocalDs intPrimTy `thenDs` \l ->
352 dsLookupGlobalId indexPName `thenDs` \indexP ->
353 mapDs (mkAlt indexP) match_alts `thenDs` \alts ->
354 returnDs (DataAlt intDataCon, [l], (Case (Var l) wild (dft : alts)))
356 wild = mkWildId intPrimTy
357 dft = (DEFAULT, [], fail)
359 -- each alternative matches one array length (corresponding to one
360 -- fake array constructor), so the match is on a literal; each
361 -- alternative's body is extended by a local binding for each
362 -- constructor argument, which are bound to array elements starting
365 mkAlt indexP (con, args, MatchResult _ bodyFun) =
366 bodyFun fail `thenDs` \body ->
367 returnDs (LitAlt lit, [], mkDsLets binds body)
369 lit = MachInt $ toInteger (dataConSourceArity con)
370 binds = [NonRec arg (indexExpr i) | (i, arg) <- zip [1..] args]
372 indexExpr i = mkApps (Var indexP) [Type elemTy, Var var, mkIntExpr i]
376 %************************************************************************
378 \subsection{Desugarer's versions of some Core functions}
380 %************************************************************************
383 mkErrorAppDs :: Id -- The error function
384 -> Type -- Type to which it should be applied
385 -> String -- The error message string to pass
388 mkErrorAppDs err_id ty msg
389 = getSrcLocDs `thenDs` \ src_loc ->
391 full_msg = showSDoc (hcat [ppr src_loc, text "|", text msg])
392 core_msg = Lit (MachStr (mkFastString (stringToUtf8 full_msg)))
394 returnDs (mkApps (Var err_id) [Type ty, core_msg])
398 *************************************************************
400 \subsection{Making literals}
402 %************************************************************************
405 mkCharExpr :: Int -> CoreExpr -- Returns C# c :: Int
406 mkIntExpr :: Integer -> CoreExpr -- Returns I# i :: Int
407 mkIntegerExpr :: Integer -> DsM CoreExpr -- Result :: Integer
409 mkIntExpr i = mkConApp intDataCon [mkIntLit i]
410 mkCharExpr c = mkConApp charDataCon [mkLit (MachChar c)]
413 | inIntRange i -- Small enough, so start from an Int
414 = returnDs (mkSmallIntegerLit i)
416 -- Special case for integral literals with a large magnitude:
417 -- They are transformed into an expression involving only smaller
418 -- integral literals. This improves constant folding.
420 | otherwise -- Big, so start from a string
421 = dsLookupGlobalId plusIntegerName `thenDs` \ plus_id ->
422 dsLookupGlobalId timesIntegerName `thenDs` \ times_id ->
424 plus a b = Var plus_id `App` a `App` b
425 times a b = Var times_id `App` a `App` b
427 -- Transform i into (x1 + (x2 + (x3 + (...) * b) * b) * b) with abs xi <= b
428 horner :: Integer -> Integer -> CoreExpr
429 horner b i | abs q <= 1 = if r == 0 || r == i
430 then mkSmallIntegerLit i
431 else mkSmallIntegerLit r `plus` mkSmallIntegerLit (i-r)
432 | r == 0 = horner b q `times` mkSmallIntegerLit b
433 | otherwise = mkSmallIntegerLit r `plus` (horner b q `times` mkSmallIntegerLit b)
435 (q,r) = i `quotRem` b
438 returnDs (horner tARGET_MAX_INT i)
440 mkSmallIntegerLit i = mkConApp smallIntegerDataCon [mkIntLit i]
442 mkStringLit :: String -> DsM CoreExpr
443 mkStringLit str = mkStringLitFS (mkFastString str)
445 mkStringLitFS :: FastString -> DsM CoreExpr
448 = returnDs (mkNilExpr charTy)
452 the_char = mkCharExpr (headIntFS str)
454 returnDs (mkConsExpr charTy the_char (mkNilExpr charTy))
456 | all safeChar int_chars
457 = dsLookupGlobalId unpackCStringName `thenDs` \ unpack_id ->
458 returnDs (App (Var unpack_id) (Lit (MachStr str)))
461 = dsLookupGlobalId unpackCStringUtf8Name `thenDs` \ unpack_id ->
462 returnDs (App (Var unpack_id) (Lit (MachStr (mkFastString (intsToUtf8 int_chars)))))
465 int_chars = unpackIntFS str
466 safeChar c = c >= 1 && c <= 0xFF
470 %************************************************************************
472 \subsection[mkSelectorBind]{Make a selector bind}
474 %************************************************************************
476 This is used in various places to do with lazy patterns.
477 For each binder $b$ in the pattern, we create a binding:
479 b = case v of pat' -> b'
481 where @pat'@ is @pat@ with each binder @b@ cloned into @b'@.
483 ToDo: making these bindings should really depend on whether there's
484 much work to be done per binding. If the pattern is complex, it
485 should be de-mangled once, into a tuple (and then selected from).
486 Otherwise the demangling can be in-line in the bindings (as here).
488 Boring! Boring! One error message per binder. The above ToDo is
489 even more helpful. Something very similar happens for pattern-bound
493 mkSelectorBinds :: TypecheckedPat -- The pattern
494 -> CoreExpr -- Expression to which the pattern is bound
495 -> DsM [(Id,CoreExpr)]
497 mkSelectorBinds (VarPat v) val_expr
498 = returnDs [(v, val_expr)]
500 mkSelectorBinds pat val_expr
501 | isSingleton binders || is_simple_pat pat
502 = newSysLocalDs (exprType val_expr) `thenDs` \ val_var ->
504 -- For the error message we make one error-app, to avoid duplication.
505 -- But we need it at different types... so we use coerce for that
506 mkErrorAppDs iRREFUT_PAT_ERROR_ID
507 unitTy (showSDoc (ppr pat)) `thenDs` \ err_expr ->
508 newSysLocalDs unitTy `thenDs` \ err_var ->
509 mapDs (mk_bind val_var err_var) binders `thenDs` \ binds ->
510 returnDs ( (val_var, val_expr) :
511 (err_var, err_expr) :
516 = mkErrorAppDs iRREFUT_PAT_ERROR_ID
517 tuple_ty (showSDoc (ppr pat)) `thenDs` \ error_expr ->
518 matchSimply val_expr PatBindRhs pat local_tuple error_expr `thenDs` \ tuple_expr ->
519 newSysLocalDs tuple_ty `thenDs` \ tuple_var ->
522 = (binder, mkTupleSelector binders binder tuple_var (Var tuple_var))
524 returnDs ( (tuple_var, tuple_expr) : map mk_tup_bind binders )
526 binders = collectPatBinders pat
527 local_tuple = mkTupleExpr binders
528 tuple_ty = exprType local_tuple
530 mk_bind scrut_var err_var bndr_var
531 -- (mk_bind sv err_var) generates
532 -- bv = case sv of { pat -> bv; other -> coerce (type-of-bv) err_var }
533 -- Remember, pat binds bv
534 = matchSimply (Var scrut_var) PatBindRhs pat
535 (Var bndr_var) error_expr `thenDs` \ rhs_expr ->
536 returnDs (bndr_var, rhs_expr)
538 error_expr = mkCoerce (idType bndr_var) (Var err_var)
540 is_simple_pat (TuplePat ps Boxed) = all is_triv_pat ps
541 is_simple_pat (ConPatOut _ ps _ _ _) = all is_triv_pat (hsConArgs ps)
542 is_simple_pat (VarPat _) = True
543 is_simple_pat (ParPat p) = is_simple_pat p
544 is_simple_pat other = False
546 is_triv_pat (VarPat v) = True
547 is_triv_pat (WildPat _) = True
548 is_triv_pat (ParPat p) = is_triv_pat p
549 is_triv_pat other = False
553 @mkTupleExpr@ builds a tuple; the inverse to @mkTupleSelector@. If it
554 has only one element, it is the identity function.
557 mkTupleExpr :: [Id] -> CoreExpr
559 {- This code has been replaced by mkCoreTup below
560 mkTupleExpr [] = Var unitDataConId
561 mkTupleExpr [id] = Var id
562 mkTupleExpr ids = mkConApp (tupleCon Boxed (length ids))
563 (map (Type . idType) ids ++ [ Var i | i <-ids])
566 mkTupleExpr ids = mkCoreTup(map Var ids)
568 mkCoreTup :: [CoreExpr] -> CoreExpr
569 mkCoreTup [] = Var unitDataConId
571 mkCoreTup cs = mkConApp (tupleCon Boxed (length cs))
572 (map (Type . exprType) cs ++ cs)
577 @mkTupleSelector@ builds a selector which scrutises the given
578 expression and extracts the one name from the list given.
579 If you want the no-shadowing rule to apply, the caller
580 is responsible for making sure that none of these names
583 If there is just one id in the ``tuple'', then the selector is
587 mkTupleSelector :: [Id] -- The tuple args
588 -> Id -- The selected one
589 -> Id -- A variable of the same type as the scrutinee
590 -> CoreExpr -- Scrutinee
593 mkTupleSelector [var] should_be_the_same_var scrut_var scrut
594 = ASSERT(var == should_be_the_same_var)
597 mkTupleSelector vars the_var scrut_var scrut
598 = ASSERT( notNull vars )
599 Case scrut scrut_var [(DataAlt (tupleCon Boxed (length vars)), vars, Var the_var)]
603 %************************************************************************
605 \subsection[mkFailurePair]{Code for pattern-matching and other failures}
607 %************************************************************************
609 Call the constructor Ids when building explicit lists, so that they
610 interact well with rules.
613 mkNilExpr :: Type -> CoreExpr
614 mkNilExpr ty = mkConApp nilDataCon [Type ty]
616 mkConsExpr :: Type -> CoreExpr -> CoreExpr -> CoreExpr
617 mkConsExpr ty hd tl = mkConApp consDataCon [Type ty, hd, tl]
619 mkListExpr :: Type -> [CoreExpr] -> CoreExpr
620 mkListExpr ty xs = foldr (mkConsExpr ty) (mkNilExpr ty) xs
625 %************************************************************************
627 \subsection[mkFailurePair]{Code for pattern-matching and other failures}
629 %************************************************************************
631 Generally, we handle pattern matching failure like this: let-bind a
632 fail-variable, and use that variable if the thing fails:
634 let fail.33 = error "Help"
645 If the case can't fail, then there'll be no mention of @fail.33@, and the
646 simplifier will later discard it.
649 If it can fail in only one way, then the simplifier will inline it.
652 Only if it is used more than once will the let-binding remain.
655 There's a problem when the result of the case expression is of
656 unboxed type. Then the type of @fail.33@ is unboxed too, and
657 there is every chance that someone will change the let into a case:
663 which is of course utterly wrong. Rather than drop the condition that
664 only boxed types can be let-bound, we just turn the fail into a function
665 for the primitive case:
667 let fail.33 :: Void -> Int#
668 fail.33 = \_ -> error "Help"
677 Now @fail.33@ is a function, so it can be let-bound.
680 mkFailurePair :: CoreExpr -- Result type of the whole case expression
681 -> DsM (CoreBind, -- Binds the newly-created fail variable
682 -- to either the expression or \ _ -> expression
683 CoreExpr) -- Either the fail variable, or fail variable
684 -- applied to unit tuple
687 = newFailLocalDs (unitTy `mkFunTy` ty) `thenDs` \ fail_fun_var ->
688 newSysLocalDs unitTy `thenDs` \ fail_fun_arg ->
689 returnDs (NonRec fail_fun_var (Lam fail_fun_arg expr),
690 App (Var fail_fun_var) (Var unitDataConId))
693 = newFailLocalDs ty `thenDs` \ fail_var ->
694 returnDs (NonRec fail_var expr, Var fail_var)