DsUtils.lhs 28.1 KB
Newer Older
1
%
Simon Marlow's avatar
Simon Marlow committed
2
% (c) The University of Glasgow 2006
3
% (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4
%
Simon Marlow's avatar
Simon Marlow committed
5 6

Utilities for desugaring
7 8 9 10

This module exports some utility functions of no great interest.

\begin{code}
11
-- | Utility functions for constructing Core syntax, principally for desugaring
12
module DsUtils (
13 14
	EquationInfo(..), 
	firstPat, shiftEqns,
15

16 17 18 19
	MatchResult(..), CanItFail(..), 
	cantFailMatchResult, alwaysFailMatchResult,
	extractMatchResult, combineMatchResults, 
	adjustMatchResult,  adjustMatchResultDs,
20
	mkCoLetMatchResult, mkViewMatchResult, mkGuardedMatchResult, 
21
	matchCanFail, mkEvalMatchResult,
22
	mkCoPrimCaseMatchResult, mkCoAlgCaseMatchResult,
23
	wrapBind, wrapBinds,
24

25
	mkErrorAppDs, mkCoreAppDs, mkCoreAppsDs,
26 27 28 29

        seqVar,

        -- LHs tuples
30
        mkLHsVarPatTup, mkLHsPatTup, mkVanillaTuplePat,
31 32 33 34 35
        mkBigLHsVarTup, mkBigLHsTup, mkBigLHsVarPatTup, mkBigLHsPatTup,

        mkSelectorBinds,

        dsSyntaxTable, lookupEvidence,
36

andy@galois.com's avatar
andy@galois.com committed
37 38
	selectSimpleMatchVarL, selectMatchVars, selectMatchVar,
	mkTickBox, mkOptTickBox, mkBinaryTickBox
39 40
    ) where

41 42
#include "HsVersions.h"

43
import {-# SOURCE #-}	Match ( matchSimply )
44
import {-# SOURCE #-}	DsExpr( dsExpr )
45

46
import HsSyn
Simon Marlow's avatar
Simon Marlow committed
47
import TcHsSyn
48
import TcType( tcSplitTyConApp )
49 50 51
import CoreSyn
import DsMonad

Simon Marlow's avatar
Simon Marlow committed
52
import CoreUtils
53
import MkCore
Simon Marlow's avatar
Simon Marlow committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
import MkId
import Id
import Var
import Name
import Literal
import TyCon
import DataCon
import Type
import Coercion
import TysPrim
import TysWiredIn
import BasicTypes
import UniqSet
import UniqSupply
import PrelNames
sof's avatar
sof committed
69
import Outputable
Simon Marlow's avatar
Simon Marlow committed
70 71 72
import SrcLoc
import Util
import ListSetOps
73
import FastString
74
import StaticFlags
75 76
\end{code}

sof's avatar
sof committed
77

78

79 80 81 82 83 84 85
%************************************************************************
%*									*
		Rebindable syntax
%*									*
%************************************************************************

\begin{code}
86
dsSyntaxTable :: SyntaxTable Id 
87 88 89
	       -> DsM ([CoreBind], 	-- Auxiliary bindings
		       [(Name,Id)])	-- Maps the standard name to its value

90 91
dsSyntaxTable rebound_ids = do
    (binds_s, prs) <- mapAndUnzipM mk_bind rebound_ids
92 93
    return (concat binds_s, prs)
  where
94 95
        -- The cheapo special case can happen when we 
        -- make an intermediate HsDo when desugaring a RecStmt
96
    mk_bind (std_name, HsVar id) = return ([], (std_name, id))
97 98 99 100
    mk_bind (std_name, expr) = do
           rhs <- dsExpr expr
           id <- newSysLocalDs (exprType rhs)
           return ([NonRec id rhs], (std_name, id))
101

102 103 104
lookupEvidence :: [(Name, Id)] -> Name -> Id
lookupEvidence prs std_name
  = assocDefault (mk_panic std_name) prs std_name
105
  where
Ian Lynagh's avatar
Ian Lynagh committed
106
    mk_panic std_name = pprPanic "dsSyntaxTable" (ptext (sLit "Not found:") <+> ppr std_name)
107 108
\end{code}

sof's avatar
sof committed
109 110
%************************************************************************
%*									*
111
\subsection{ Selecting match variables}
sof's avatar
sof committed
112 113 114 115 116 117 118 119 120
%*									*
%************************************************************************

We're about to match against some patterns.  We want to make some
@Ids@ to use as match variables.  If a pattern has an @Id@ readily at
hand, which should indeed be bound to the pattern as a whole, then use it;
otherwise, make one up.

\begin{code}
121
selectSimpleMatchVarL :: LPat Id -> DsM Id
122
selectSimpleMatchVarL pat = selectMatchVar (unLoc pat)
123 124 125 126

-- (selectMatchVars ps tys) chooses variables of type tys
-- to use for matching ps against.  If the pattern is a variable,
-- we try to use that, to save inventing lots of fresh variables.
127 128 129
--
-- OLD, but interesting note:
--    But even if it is a variable, its type might not match.  Consider
130 131 132 133 134 135 136
--	data T a where
--	  T1 :: Int -> T Int
--	  T2 :: a   -> T a
--
--	f :: T a -> a -> Int
--	f (T1 i) (x::Int) = x
--	f (T2 i) (y::a)   = 0
137 138 139 140 141 142
--    Then we must not choose (x::Int) as the matching variable!
-- And nowadays we won't, because the (x::Int) will be wrapped in a CoPat

selectMatchVars :: [Pat Id] -> DsM [Id]
selectMatchVars ps = mapM selectMatchVar ps

143
selectMatchVar :: Pat Id -> DsM Id
144 145 146
selectMatchVar (BangPat pat) = selectMatchVar (unLoc pat)
selectMatchVar (LazyPat pat) = selectMatchVar (unLoc pat)
selectMatchVar (ParPat pat)  = selectMatchVar (unLoc pat)
147
selectMatchVar (VarPat var)  = return (localiseId var)  -- Note [Localise pattern binders]
148
selectMatchVar (AsPat var _) = return (unLoc var)
149
selectMatchVar other_pat     = newSysLocalDs (hsPatType other_pat)
150
				  -- OK, better make up one...
sof's avatar
sof committed
151 152
\end{code}

153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
Note [Localise pattern binders]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Consider     module M where
               [Just a] = e
After renaming it looks like
             module M where
               [Just M.a] = e

We don't generalise, since it's a pattern binding, monomorphic, etc,
so after desugaring we may get something like
             M.a = case e of (v:_) ->
                   case v of Just M.a -> M.a
Notice the "M.a" in the pattern; after all, it was in the original
pattern.  However, after optimisation those pattern binders can become
let-binders, and then end up floated to top level.  They have a
different *unique* by then (the simplifier is good about maintaining
proper scoping), but it's BAD to have two top-level bindings with the
External Name M.a, because that turns into two linker symbols for M.a.
It's quite rare for this to actually *happen* -- the only case I know
of is tc003 compiled with the 'hpc' way -- but that only makes it 
all the more annoying.

To avoid this, we craftily call 'localiseId' in the desugarer, which
simply turns the External Name for the Id into an Internal one, but
doesn't change the unique.  So the desugarer produces this:
             M.a{r8} = case e of (v:_) ->
                       case v of Just a{r8} -> M.a{r8}
The unique is still 'r8', but the binding site in the pattern
is now an Internal Name.  Now the simplifier's usual mechanisms
will propagate that Name to all the occurrence sites, as well as
un-shadowing it, so we'll get
             M.a{r8} = case e of (v:_) ->
                       case v of Just a{s77} -> a{s77}
In fact, even CoreSubst.simplOptExpr will do this, and simpleOptExpr
runs on the output of the desugarer, so all is well by the end of
the desugaring pass.

sof's avatar
sof committed
190

191 192 193 194 195 196 197 198 199 200 201
%************************************************************************
%*									*
%* type synonym EquationInfo and access functions for its pieces	*
%*									*
%************************************************************************
\subsection[EquationInfo-synonym]{@EquationInfo@: a useful synonym}

The ``equation info'' used by @match@ is relatively complicated and
worthy of a type synonym and a few handy functions.

\begin{code}
202
firstPat :: EquationInfo -> Pat Id
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
203
firstPat eqn = ASSERT( notNull (eqn_pats eqn) ) head (eqn_pats eqn)
204

205
shiftEqns :: [EquationInfo] -> [EquationInfo]
206 207
-- Drop the first pattern in each equation
shiftEqns eqns = [ eqn { eqn_pats = tail (eqn_pats eqn) } | eqn <- eqns ]
208 209
\end{code}

210 211 212
Functions on MatchResults

\begin{code}
213 214 215 216
matchCanFail :: MatchResult -> Bool
matchCanFail (MatchResult CanFail _)  = True
matchCanFail (MatchResult CantFail _) = False

217
alwaysFailMatchResult :: MatchResult
218
alwaysFailMatchResult = MatchResult CanFail (\fail -> return fail)
219

220
cantFailMatchResult :: CoreExpr -> MatchResult
221
cantFailMatchResult expr = MatchResult CantFail (\_ -> return expr)
222

223
extractMatchResult :: MatchResult -> CoreExpr -> DsM CoreExpr
224
extractMatchResult (MatchResult CantFail match_fn) _
225
  = match_fn (error "It can't fail!")
226

227 228 229
extractMatchResult (MatchResult CanFail match_fn) fail_expr = do
    (fail_bind, if_it_fails) <- mkFailurePair fail_expr
    body <- match_fn if_it_fails
230
    return (mkCoreLet fail_bind body)
231

232 233 234

combineMatchResults :: MatchResult -> MatchResult -> MatchResult
combineMatchResults (MatchResult CanFail      body_fn1)
235
                    (MatchResult can_it_fail2 body_fn2)
236 237
  = MatchResult can_it_fail2 body_fn
  where
238 239 240 241
    body_fn fail = do body2 <- body_fn2 fail
                      (fail_bind, duplicatable_expr) <- mkFailurePair body2
                      body1 <- body_fn1 duplicatable_expr
                      return (Let fail_bind body1)
242

243
combineMatchResults match_result1@(MatchResult CantFail _) _
244 245
  = match_result1

246
adjustMatchResult :: DsWrapper -> MatchResult -> MatchResult
247
adjustMatchResult encl_fn (MatchResult can_it_fail body_fn)
248
  = MatchResult can_it_fail (\fail -> encl_fn <$> body_fn fail)
249 250 251

adjustMatchResultDs :: (CoreExpr -> DsM CoreExpr) -> MatchResult -> MatchResult
adjustMatchResultDs encl_fn (MatchResult can_it_fail body_fn)
252
  = MatchResult can_it_fail (\fail -> encl_fn =<< body_fn fail)
253

254 255 256
wrapBinds :: [(Var,Var)] -> CoreExpr -> CoreExpr
wrapBinds [] e = e
wrapBinds ((new,old):prs) e = wrapBind new old (wrapBinds prs e)
257

258
wrapBind :: Var -> Var -> CoreExpr -> CoreExpr
259
wrapBind new old body	-- Can deal with term variables *or* type variables
260
  | new==old    = body
261
  | isTyCoVar new = Let (mkTyBind new (mkTyVarTy old)) body
262
  | otherwise   = Let (NonRec new (Var old))         body
263

simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
264 265 266 267
seqVar :: Var -> CoreExpr -> CoreExpr
seqVar var body = Case (Var var) var (exprType body)
			[(DEFAULT, [], body)]

268
mkCoLetMatchResult :: CoreBind -> MatchResult -> MatchResult
269
mkCoLetMatchResult bind = adjustMatchResult (mkCoreLet bind)
270

271 272 273 274
-- (mkViewMatchResult var' viewExpr var mr) makes the expression
-- let var' = viewExpr var in mr
mkViewMatchResult :: Id -> CoreExpr -> Id -> MatchResult -> MatchResult
mkViewMatchResult var' viewExpr var = 
275
    adjustMatchResult (mkCoreLet (NonRec var' (mkCoreAppDs viewExpr (Var var))))
276

277 278 279
mkEvalMatchResult :: Id -> Type -> MatchResult -> MatchResult
mkEvalMatchResult var ty
  = adjustMatchResult (\e -> Case (Var var) var ty [(DEFAULT, [], e)]) 
280 281

mkGuardedMatchResult :: CoreExpr -> MatchResult -> MatchResult
282
mkGuardedMatchResult pred_expr (MatchResult _ body_fn)
283 284
  = MatchResult CanFail (\fail -> do body <- body_fn fail
                                     return (mkIfThenElse pred_expr body fail))
285 286

mkCoPrimCaseMatchResult :: Id				-- Scrutinee
287
                    -> Type                             -- Type of the case
288 289
		    -> [(Literal, MatchResult)]		-- Alternatives
		    -> MatchResult
290
mkCoPrimCaseMatchResult var ty match_alts
291
  = MatchResult CanFail mk_case
292
  where
293 294 295
    mk_case fail = do
        alts <- mapM (mk_alt fail) sorted_alts
        return (Case (Var var) var ty ((DEFAULT, [], fail) : alts))
296

297
    sorted_alts = sortWith fst match_alts	-- Right order for a Case
298 299
    mk_alt fail (lit, MatchResult _ body_fn) = do body <- body_fn fail
                                                  return (LitAlt lit, [], body)
300 301


302
mkCoAlgCaseMatchResult :: Id					-- Scrutinee
303
                    -> Type                                     -- Type of exp
304 305
		    -> [(DataCon, [CoreBndr], MatchResult)]	-- Alternatives
		    -> MatchResult
306
mkCoAlgCaseMatchResult var ty match_alts 
307
  | isNewTyCon tycon		-- Newtype case; use a let
308
  = ASSERT( null (tail match_alts) && null (tail arg_ids1) )
309
    mkCoLetMatchResult (NonRec arg_id1 newtype_rhs) match_result1
310

chak's avatar
chak committed
311 312 313
  | isPArrFakeAlts match_alts	-- Sugared parallel array; use a literal case 
  = MatchResult CanFail mk_parrCase

314 315
  | otherwise			-- Datatype case; use a case
  = MatchResult fail_flag mk_case
316
  where
317 318 319
    tycon = dataConTyCon con1
	-- [Interesting: becuase of GADTs, we can't rely on the type of 
	--  the scrutinised Id to be sufficiently refined to have a TyCon in it]
320 321

	-- Stuff for newtype
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
322 323
    (con1, arg_ids1, match_result1) = ASSERT( notNull match_alts ) head match_alts
    arg_id1 	= ASSERT( notNull arg_ids1 ) head arg_ids1
324
    var_ty      = idType var
325 326
    (tc, ty_args) = tcSplitTyConApp var_ty	-- Don't look through newtypes
    	 	    		    		-- (not that splitTyConApp does, these days)
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
327
    newtype_rhs = unwrapNewTypeBody tc ty_args (Var var)
328
		
329
	-- Stuff for data types
330 331
    data_cons      = tyConDataCons tycon
    match_results  = [match_result | (_,_,match_result) <- match_alts]
332

333 334 335 336
    fail_flag | exhaustive_case
	      = foldr1 orFail [can_it_fail | MatchResult can_it_fail _ <- match_results]
	      | otherwise
	      = CanFail
337

338 339
    sorted_alts  = sortWith get_tag match_alts
    get_tag (con, _, _) = dataConTag con
340
    mk_case fail = do alts <- mapM (mk_alt fail) sorted_alts
341
                      return (mkWildCase (Var var) (idType var) ty (mk_default fail ++ alts))
342

343 344 345 346
    mk_alt fail (con, args, MatchResult _ body_fn) = do
          body <- body_fn fail
          us <- newUniqueSupply
          return (mkReboxingAlt (uniqsFromSupply us) con args body)
347

348 349
    mk_default fail | exhaustive_case = []
		    | otherwise       = [(DEFAULT, [], fail)]
350

351 352 353
    un_mentioned_constructors
        = mkUniqSet data_cons `minusUniqSet` mkUniqSet [ con | (con, _, _) <- match_alts]
    exhaustive_case = isEmptyUniqSet un_mentioned_constructors
chak's avatar
chak committed
354 355 356

	-- Stuff for parallel arrays
	-- 
357
	--  * the following is to desugar cases over fake constructors for
chak's avatar
chak committed
358 359 360 361 362
	--   parallel arrays, which are introduced by `tidy1' in the `PArrPat'
	--   case
	--
	-- Concerning `isPArrFakeAlts':
	--
363
	--  * it is *not* sufficient to just check the type of the type
chak's avatar
chak committed
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
	--   constructor, as we have to be careful not to confuse the real
	--   representation of parallel arrays with the fake constructors;
	--   moreover, a list of alternatives must not mix fake and real
	--   constructors (this is checked earlier on)
	--
	-- FIXME: We actually go through the whole list and make sure that
	--	  either all or none of the constructors are fake parallel
	--	  array constructors.  This is to spot equations that mix fake
	--	  constructors with the real representation defined in
	--	  `PrelPArr'.  It would be nicer to spot this situation
	--	  earlier and raise a proper error message, but it can really
	--	  only happen in `PrelPArr' anyway.
	--
    isPArrFakeAlts [(dcon, _, _)]      = isPArrFakeCon dcon
    isPArrFakeAlts ((dcon, _, _):alts) = 
      case (isPArrFakeCon dcon, isPArrFakeAlts alts) of
        (True , True ) -> True
        (False, False) -> False
382 383
        _              -> panic "DsUtils: you may not mix `[:...:]' with `PArr' patterns"
    isPArrFakeAlts [] = panic "DsUtils: unexpectedly found an empty list of PArr fake alternatives"
chak's avatar
chak committed
384
    --
385
    mk_parrCase fail = do
386
      lengthP <- dsLookupDPHId lengthPName
387
      alt <- unboxAlt
388
      return (mkWildCase (len lengthP) intTy ty [alt])
chak's avatar
chak committed
389 390 391 392 393 394 395
      where
	elemTy      = case splitTyConApp (idType var) of
		        (_, [elemTy]) -> elemTy
		        _	        -> panic panicMsg
        panicMsg    = "DsUtils.mkCoAlgCaseMatchResult: not a parallel array?"
	len lengthP = mkApps (Var lengthP) [Type elemTy, Var var]
	--
396 397
	unboxAlt = do
	  l      <- newSysLocalDs intPrimTy
398
	  indexP <- dsLookupDPHId indexPName
399
	  alts   <- mapM (mkAlt indexP) sorted_alts
400
	  return (DataAlt intDataCon, [l], mkWildCase (Var l) intPrimTy ty (dft : alts))
chak's avatar
chak committed
401 402 403 404 405 406 407 408 409
          where
	    dft  = (DEFAULT, [], fail)
	--
	-- each alternative matches one array length (corresponding to one
	-- fake array constructor), so the match is on a literal; each
	-- alternative's body is extended by a local binding for each
	-- constructor argument, which are bound to array elements starting
	-- with the first
	--
410 411
	mkAlt indexP (con, args, MatchResult _ bodyFun) = do
	  body <- bodyFun fail
412
	  return (LitAlt lit, [], mkCoreLets binds body)
chak's avatar
chak committed
413 414 415 416
	  where
	    lit   = MachInt $ toInteger (dataConSourceArity con)
	    binds = [NonRec arg (indexExpr i) | (i, arg) <- zip [1..] args]
	    --
417
	    indexExpr i = mkApps (Var indexP) [Type elemTy, Var var, mkIntExpr i]
418
\end{code}
419

420 421
%************************************************************************
%*									*
422
\subsection{Desugarer's versions of some Core functions}
423 424 425 426
%*									*
%************************************************************************

\begin{code}
427 428
mkErrorAppDs :: Id 		-- The error function
	     -> Type		-- Type to which it should be applied
429
	     -> SDoc		-- The error message string to pass
430 431
	     -> DsM CoreExpr

432 433
mkErrorAppDs err_id ty msg = do
    src_loc <- getSrcSpanDs
434
    let
435
        full_msg = showSDoc (hcat [ppr src_loc, text "|", msg])
436 437
        core_msg = Lit (mkMachString full_msg)
        -- mkMachString returns a result of type String#
438
    return (mkApps (Var err_id) [Type ty, core_msg])
439 440
\end{code}

441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
'mkCoreAppDs' and 'mkCoreAppsDs' hand the special-case desugaring of 'seq'.

Note [Desugaring seq (1)]  cf Trac #1031
~~~~~~~~~~~~~~~~~~~~~~~~~
   f x y = x `seq` (y `seq` (# x,y #))

The [CoreSyn let/app invariant] means that, other things being equal, because 
the argument to the outer 'seq' has an unlifted type, we'll use call-by-value thus:

   f x y = case (y `seq` (# x,y #)) of v -> x `seq` v

But that is bad for two reasons: 
  (a) we now evaluate y before x, and 
  (b) we can't bind v to an unboxed pair

Seq is very, very special!  So we recognise it right here, and desugar to
        case x of _ -> case y of _ -> (# x,y #)

simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
459
Note [Desugaring seq (2)]  cf Trac #2273
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
~~~~~~~~~~~~~~~~~~~~~~~~~
Consider
   let chp = case b of { True -> fst x; False -> 0 }
   in chp `seq` ...chp...
Here the seq is designed to plug the space leak of retaining (snd x)
for too long.

If we rely on the ordinary inlining of seq, we'll get
   let chp = case b of { True -> fst x; False -> 0 }
   case chp of _ { I# -> ...chp... }

But since chp is cheap, and the case is an alluring contet, we'll
inline chp into the case scrutinee.  Now there is only one use of chp,
so we'll inline a second copy.  Alas, we've now ruined the purpose of
the seq, by re-introducing the space leak:
    case (case b of {True -> fst x; False -> 0}) of
      I# _ -> ...case b of {True -> fst x; False -> 0}...

We can try to avoid doing this by ensuring that the binder-swap in the
case happens, so we get his at an early stage:
   case chp of chp2 { I# -> ...chp2... }
But this is fragile.  The real culprit is the source program.  Perhaps we
should have said explicitly
   let !chp2 = chp in ...chp2...

But that's painful.  So the code here does a little hack to make seq
more robust: a saturated application of 'seq' is turned *directly* into
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
487 488 489 490 491
the case expression, thus:
   x  `seq` e2 ==> case x of x -> e2    -- Note shadowing!
   e1 `seq` e2 ==> case x of _ -> e2

So we desugar our example to:
492 493
   let chp = case b of { True -> fst x; False -> 0 }
   case chp of chp { I# -> ...chp... }
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
494
And now all is well.
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514

The reason it's a hack is because if you define mySeq=seq, the hack
won't work on mySeq.  

Note [Desugaring seq (3)] cf Trac #2409
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The isLocalId ensures that we don't turn 
        True `seq` e
into
        case True of True { ... }
which stupidly tries to bind the datacon 'True'. 

\begin{code}
mkCoreAppDs  :: CoreExpr -> CoreExpr -> CoreExpr
mkCoreAppDs (Var f `App` Type ty1 `App` Type ty2 `App` arg1) arg2
  | f `hasKey` seqIdKey            -- Note [Desugaring seq (1), (2)]
  = Case arg1 case_bndr ty2 [(DEFAULT,[],arg2)]
  where
    case_bndr = case arg1 of
                   Var v1 | isLocalId v1 -> v1        -- Note [Desugaring seq (2) and (3)]
515
                   _                     -> mkWildValBinder ty1
516 517 518 519 520 521 522 523

mkCoreAppDs fun arg = mkCoreApp fun arg	 -- The rest is done in MkCore

mkCoreAppsDs :: CoreExpr -> [CoreExpr] -> CoreExpr
mkCoreAppsDs fun args = foldl mkCoreAppDs fun args
\end{code}


524 525 526 527 528 529 530 531
%************************************************************************
%*									*
\subsection[mkSelectorBind]{Make a selector bind}
%*									*
%************************************************************************

This is used in various places to do with lazy patterns.
For each binder $b$ in the pattern, we create a binding:
532
\begin{verbatim}
533
    b = case v of pat' -> b'
534 535
\end{verbatim}
where @pat'@ is @pat@ with each binder @b@ cloned into @b'@.
536 537 538 539 540 541 542 543 544 545 546

ToDo: making these bindings should really depend on whether there's
much work to be done per binding.  If the pattern is complex, it
should be de-mangled once, into a tuple (and then selected from).
Otherwise the demangling can be in-line in the bindings (as here).

Boring!  Boring!  One error message per binder.  The above ToDo is
even more helpful.  Something very similar happens for pattern-bound
expressions.

\begin{code}
547 548
mkSelectorBinds :: LPat Id	-- The pattern
		-> CoreExpr	-- Expression to which the pattern is bound
549
		-> DsM [(Id,CoreExpr)]
550

551
mkSelectorBinds (L _ (VarPat v)) val_expr
552
  = return [(v, val_expr)]
sof's avatar
sof committed
553 554

mkSelectorBinds pat val_expr
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
  | isSingleton binders || is_simple_lpat pat = do
        -- Given   p = e, where p binds x,y
        -- we are going to make
        --      v = p   (where v is fresh)
        --      x = case v of p -> x
        --      y = case v of p -> x

        -- Make up 'v'
        -- NB: give it the type of *pattern* p, not the type of the *rhs* e.
        -- This does not matter after desugaring, but there's a subtle 
        -- issue with implicit parameters. Consider
        --      (x,y) = ?i
        -- Then, ?i is given type {?i :: Int}, a PredType, which is opaque
        -- to the desugarer.  (Why opaque?  Because newtypes have to be.  Why
        -- does it get that type?  So that when we abstract over it we get the
        -- right top-level type  (?i::Int) => ...)
        --
        -- So to get the type of 'v', use the pattern not the rhs.  Often more
        -- efficient too.
      val_var <- newSysLocalDs (hsLPatType pat)

        -- For the error message we make one error-app, to avoid duplication.
        -- But we need it at different types... so we use coerce for that
578
      err_expr <- mkErrorAppDs iRREFUT_PAT_ERROR_ID  unitTy (ppr pat)
579 580 581 582 583 584 585 586
      err_var <- newSysLocalDs unitTy
      binds <- mapM (mk_bind val_var err_var) binders
      return ( (val_var, val_expr) : 
               (err_var, err_expr) :
               binds )


  | otherwise = do
587
      error_expr <- mkErrorAppDs iRREFUT_PAT_ERROR_ID   tuple_ty (ppr pat)
588 589
      tuple_expr <- matchSimply val_expr PatBindRhs pat local_tuple error_expr
      tuple_var <- newSysLocalDs tuple_ty
590
      let mk_tup_bind binder
591
            = (binder, mkTupleSelector local_binders binder tuple_var (Var tuple_var))
592
      return ( (tuple_var, tuple_expr) : map mk_tup_bind binders )
593
  where
594 595 596 597
    binders       = collectPatBinders pat
    local_binders = map localiseId binders	-- See Note [Localise pattern binders]
    local_tuple   = mkBigCoreVarTup binders
    tuple_ty      = exprType local_tuple
598

599
    mk_bind scrut_var err_var bndr_var = do
600
    -- (mk_bind sv err_var) generates
601
    --          bv = case sv of { pat -> bv; other -> coerce (type-of-bv) err_var }
602
    -- Remember, pat binds bv
603 604 605
        rhs_expr <- matchSimply (Var scrut_var) PatBindRhs pat
                                (Var bndr_var) error_expr
        return (bndr_var, rhs_expr)
606
      where
607 608
        error_expr = mkCoerce co (Var err_var)
        co         = mkUnsafeCoercion (exprType (Var err_var)) (idType bndr_var)
609

610 611
    is_simple_lpat p = is_simple_pat (unLoc p)

612
    is_simple_pat (TuplePat ps Boxed _)        = all is_triv_lpat ps
613
    is_simple_pat (ConPatOut{ pat_args = ps }) = all is_triv_lpat (hsConPatArgs ps)
614 615 616
    is_simple_pat (VarPat _)                   = True
    is_simple_pat (ParPat p)                   = is_simple_lpat p
    is_simple_pat _                                    = False
617

618 619
    is_triv_lpat p = is_triv_pat (unLoc p)

620
    is_triv_pat (VarPat _)  = True
621
    is_triv_pat (WildPat _) = True
622
    is_triv_pat (ParPat p)  = is_triv_lpat p
623 624 625 626
    is_triv_pat _           = False

\end{code}

627 628 629
Creating big tuples and their types for full Haskell expressions.
They work over *Ids*, and create tuples replete with their types,
which is whey they are not in HsUtils.
630 631 632

\begin{code}
mkLHsPatTup :: [LPat Id] -> LPat Id
633
mkLHsPatTup []     = noLoc $ mkVanillaTuplePat [] Boxed
634
mkLHsPatTup [lpat] = lpat
635 636
mkLHsPatTup lpats  = L (getLoc (head lpats)) $ 
		     mkVanillaTuplePat lpats Boxed
637

638 639 640 641 642 643
mkLHsVarPatTup :: [Id] -> LPat Id
mkLHsVarPatTup bs  = mkLHsPatTup (map nlVarPat bs)

mkVanillaTuplePat :: [OutPat Id] -> Boxity -> Pat Id
-- A vanilla tuple pattern simply gets its type from its sub-patterns
mkVanillaTuplePat pats box 
644
  = TuplePat pats box (mkTupleTy box (map hsLPatType pats))
645

646 647 648 649 650
-- The Big equivalents for the source tuple expressions
mkBigLHsVarTup :: [Id] -> LHsExpr Id
mkBigLHsVarTup ids = mkBigLHsTup (map nlHsVar ids)

mkBigLHsTup :: [LHsExpr Id] -> LHsExpr Id
651
mkBigLHsTup = mkChunkified mkLHsTupleExpr
652 653 654 655 656 657

-- The Big equivalents for the source tuple patterns
mkBigLHsVarPatTup :: [Id] -> LPat Id
mkBigLHsVarPatTup bs = mkBigLHsPatTup (map nlVarPat bs)

mkBigLHsPatTup :: [LPat Id] -> LPat Id
658
mkBigLHsPatTup = mkChunkified mkLHsPatTup
659 660
\end{code}

661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
%************************************************************************
%*									*
\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}
	let fail.33 = error "Help"
	in
	case x of
		p1 -> ...
		p2 -> fail.33
		p3 -> fail.33
		p4 -> ...
\end{verbatim}
Then
\begin{itemize}
\item
681
If the case can't fail, then there'll be no mention of @fail.33@, and the
682 683 684 685 686 687 688 689 690 691
simplifier will later discard it.

\item
If it can fail in only one way, then the simplifier will inline it.

\item
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
692
unboxed type.  Then the type of @fail.33@ is unboxed too, and
693 694 695 696 697 698 699
there is every chance that someone will change the let into a case:
\begin{verbatim}
	case error "Help" of
	  fail.33 -> case ....
\end{verbatim}

which is of course utterly wrong.  Rather than drop the condition that
700
only boxed types can be let-bound, we just turn the fail into a function
701 702
for the primitive case:
\begin{verbatim}
703
	let fail.33 :: Void -> Int#
704 705 706 707
	    fail.33 = \_ -> error "Help"
	in
	case x of
		p1 -> ...
708 709
		p2 -> fail.33 void
		p3 -> fail.33 void
710 711 712
		p4 -> ...
\end{verbatim}

713
Now @fail.33@ is a function, so it can be let-bound.
714 715

\begin{code}
716 717
mkFailurePair :: CoreExpr	-- Result type of the whole case expression
	      -> DsM (CoreBind,	-- Binds the newly-created fail variable
718 719 720
				-- to \ _ -> expression
		      CoreExpr)	-- Fail variable applied to realWorld#
-- See Note [Failure thunks and CPR]
721
mkFailurePair expr
722 723 724 725
  = do { fail_fun_var <- newFailLocalDs (realWorldStatePrimTy `mkFunTy` ty)
       ; fail_fun_arg <- newSysLocalDs realWorldStatePrimTy
       ; return (NonRec fail_fun_var (Lam fail_fun_arg expr),
                 App (Var fail_fun_var) (Var realWorldPrimId)) }
726
  where
727
    ty = exprType expr
728 729
\end{code}

730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
Note [Failure thunks and CPR]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
When we make a failure point we ensure that it
does not look like a thunk. Example:

   let fail = \rw -> error "urk"
   in case x of 
        [] -> fail realWorld#
        (y:ys) -> case ys of
                    [] -> fail realWorld#  
                    (z:zs) -> (y,z)

Reason: we know that a failure point is always a "join point" and is
entered at most once.  Adding a dummy 'realWorld' token argument makes
it clear that sharing is not an issue.  And that in turn makes it more
CPR-friendly.  This matters a lot: if you don't get it right, you lose
the tail call property.  For example, see Trac #3403.

andy@galois.com's avatar
andy@galois.com committed
748
\begin{code}
749
mkOptTickBox :: Maybe (Int,[Id]) -> CoreExpr -> DsM CoreExpr
andy@galois.com's avatar
andy@galois.com committed
750
mkOptTickBox Nothing e   = return e
751
mkOptTickBox (Just (ix,ids)) e = mkTickBox ix ids e
andy@galois.com's avatar
andy@galois.com committed
752

753 754
mkTickBox :: Int -> [Id] -> CoreExpr -> DsM CoreExpr
mkTickBox ix vars e = do
755
       uq <- newUnique 	
andy@galois.com's avatar
andy@galois.com committed
756
       mod <- getModuleDs
757 758
       let tick | opt_Hpc   = mkTickBoxOpId uq mod ix
                | otherwise = mkBreakPointOpId uq mod ix
759 760
       uq2 <- newUnique 	
       let occName = mkVarOcc "tick"
761
       let name = mkInternalName uq2 occName noSrcSpan   -- use mkSysLocal?
762
       let var  = Id.mkLocalId name realWorldStatePrimTy
763 764 765 766 767
       scrut <- 
          if opt_Hpc 
            then return (Var tick)
            else do
              let tickVar = Var tick
768
              let tickType = mkFunTys (map idType vars) realWorldStatePrimTy 
769
              let scrutApTy = App tickVar (Type tickType)
770
              return (mkApps scrutApTy (map Var vars) :: Expr Id)
771
       return $ Case scrut var ty [(DEFAULT,[],e)]
772 773
  where
     ty = exprType e
andy@galois.com's avatar
andy@galois.com committed
774 775 776

mkBinaryTickBox :: Int -> Int -> CoreExpr -> DsM CoreExpr
mkBinaryTickBox ixT ixF e = do
777
       uq <- newUnique 	
Ian Lynagh's avatar
Ian Lynagh committed
778
       let bndr1 = mkSysLocal (fsLit "t1") uq boolTy 
779 780
       falseBox <- mkTickBox ixF [] $ Var falseDataConId
       trueBox  <- mkTickBox ixT [] $ Var trueDataConId
781 782 783 784
       return $ Case e bndr1 boolTy
                       [ (DataAlt falseDataCon, [], falseBox)
                       , (DataAlt trueDataCon,  [], trueBox)
                       ]
785
\end{code}