[project @ 1998-04-14 13:59:59 by simonpj]
[ghc-hetmet.git] / ghc / compiler / specialise / Specialise.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1996
3 %
4 \section[Specialise]{Stamping out overloading, and (optionally) polymorphism}
5
6 \begin{code}
7 module Specialise (
8         specProgram, 
9         idSpecVars
10     ) where
11
12 #include "HsVersions.h"
13
14 import MkId             ( mkUserLocal )
15 import Id               ( Id, DictVar, idType, mkTemplateLocals,
16
17                           getIdSpecialisation, setIdSpecialisation, isSpecPragmaId,
18
19                           IdSet, mkIdSet, addOneToIdSet, intersectIdSets, isEmptyIdSet, 
20                                  emptyIdSet, unionIdSets, minusIdSet, unitIdSet, elementOfIdSet,
21
22                           IdEnv, mkIdEnv, lookupIdEnv, addOneToIdEnv, delOneFromIdEnv
23                         )
24
25 import Type             ( Type, mkTyVarTy, splitSigmaTy, instantiateTy, isDictTy,
26                           tyVarsOfType, tyVarsOfTypes, applyTys, mkForAllTys
27                         )
28 import TyCon            ( TyCon )
29 import TyVar            ( TyVar, mkTyVar,
30                           TyVarSet, mkTyVarSet, isEmptyTyVarSet, intersectTyVarSets,
31                                     elementOfTyVarSet, unionTyVarSets, emptyTyVarSet,
32                                     minusTyVarSet,
33                           TyVarEnv, mkTyVarEnv, delFromTyVarEnv
34                         )
35 import Kind             ( mkBoxedTypeKind )
36 import CoreSyn
37 import FreeVars         ( exprFreeVars )
38 import PprCore          ()      -- Instances 
39 import Name             ( NamedThing(..), getSrcLoc, mkSysLocalName, isLocallyDefined )
40 import SrcLoc           ( noSrcLoc )
41 import SpecEnv          ( addToSpecEnv, lookupSpecEnv, specEnvValues )
42
43 import UniqSupply       ( UniqSupply,
44                           UniqSM, initUs, thenUs, returnUs, getUnique, mapUs
45                         )
46 import Unique           ( mkAlphaTyVarUnique )
47 import FiniteMap
48 import Maybes           ( MaybeErr(..), maybeToBool )
49 import Bag
50 import List             ( partition )
51 import Util             ( zipEqual )
52 import Outputable
53
54
55 infixr 9 `thenSM`
56 \end{code}
57
58 %************************************************************************
59 %*                                                                      *
60 \subsection[notes-Specialise]{Implementation notes [SLPJ, Aug 18 1993]}
61 %*                                                                      *
62 %************************************************************************
63
64 These notes describe how we implement specialisation to eliminate
65 overloading.
66
67 The specialisation pass works on Core
68 syntax, complete with all the explicit dictionary application,
69 abstraction and construction as added by the type checker.  The
70 existing type checker remains largely as it is.
71
72 One important thought: the {\em types} passed to an overloaded
73 function, and the {\em dictionaries} passed are mutually redundant.
74 If the same function is applied to the same type(s) then it is sure to
75 be applied to the same dictionary(s)---or rather to the same {\em
76 values}.  (The arguments might look different but they will evaluate
77 to the same value.)
78
79 Second important thought: we know that we can make progress by
80 treating dictionary arguments as static and worth specialising on.  So
81 we can do without binding-time analysis, and instead specialise on
82 dictionary arguments and no others.
83
84 The basic idea
85 ~~~~~~~~~~~~~~
86 Suppose we have
87
88         let f = <f_rhs>
89         in <body>
90
91 and suppose f is overloaded.
92
93 STEP 1: CALL-INSTANCE COLLECTION
94
95 We traverse <body>, accumulating all applications of f to types and
96 dictionaries.
97
98 (Might there be partial applications, to just some of its types and
99 dictionaries?  In principle yes, but in practice the type checker only
100 builds applications of f to all its types and dictionaries, so partial
101 applications could only arise as a result of transformation, and even
102 then I think it's unlikely.  In any case, we simply don't accumulate such
103 partial applications.)
104
105 There's a choice of whether to collect details of all *polymorphic* functions
106 or simply all *overloaded* ones.  How to sort this out?
107   Pass in a predicate on the function to say if it is "interesting"?
108   This is dependent on the user flags: SpecialiseOverloaded
109                                        SpecialiseUnboxed
110                                        SpecialiseAll
111
112 STEP 2: EQUIVALENCES
113
114 So now we have a collection of calls to f:
115         f t1 t2 d1 d2
116         f t3 t4 d3 d4
117         ...
118 Notice that f may take several type arguments.  To avoid ambiguity, we
119 say that f is called at type t1/t2 and t3/t4.
120
121 We take equivalence classes using equality of the *types* (ignoring
122 the dictionary args, which as mentioned previously are redundant).
123
124 STEP 3: SPECIALISATION
125
126 For each equivalence class, choose a representative (f t1 t2 d1 d2),
127 and create a local instance of f, defined thus:
128
129         f@t1/t2 = <f_rhs> t1 t2 d1 d2
130
131 f_rhs presumably has some big lambdas and dictionary lambdas, so lots
132 of simplification will now result.  However we don't actually *do* that
133 simplification.  Rather, we leave it for the simplifier to do.  If we
134 *did* do it, though, we'd get more call instances from the specialised
135 RHS.  We can work out what they are by instantiating the call-instance
136 set from f's RHS with the types t1, t2.
137
138 Add this new id to f's IdInfo, to record that f has a specialised version.
139
140 Before doing any of this, check that f's IdInfo doesn't already
141 tell us about an existing instance of f at the required type/s.
142 (This might happen if specialisation was applied more than once, or
143 it might arise from user SPECIALIZE pragmas.)
144
145 Recursion
146 ~~~~~~~~~
147 Wait a minute!  What if f is recursive?  Then we can't just plug in
148 its right-hand side, can we?
149
150 But it's ok.  The type checker *always* creates non-recursive definitions
151 for overloaded recursive functions.  For example:
152
153         f x = f (x+x)           -- Yes I know its silly
154
155 becomes
156
157         f a (d::Num a) = let p = +.sel a d
158                          in
159                          letrec fl (y::a) = fl (p y y)
160                          in
161                          fl
162
163 We still have recusion for non-overloaded functions which we
164 speciailise, but the recursive call should get specialised to the
165 same recursive version.
166
167
168 Polymorphism 1
169 ~~~~~~~~~~~~~~
170
171 All this is crystal clear when the function is applied to *constant
172 types*; that is, types which have no type variables inside.  But what if
173 it is applied to non-constant types?  Suppose we find a call of f at type
174 t1/t2.  There are two possibilities:
175
176 (a) The free type variables of t1, t2 are in scope at the definition point
177 of f.  In this case there's no problem, we proceed just as before.  A common
178 example is as follows.  Here's the Haskell:
179
180         g y = let f x = x+x
181               in f y + f y
182
183 After typechecking we have
184
185         g a (d::Num a) (y::a) = let f b (d'::Num b) (x::b) = +.sel b d' x x
186                                 in +.sel a d (f a d y) (f a d y)
187
188 Notice that the call to f is at type type "a"; a non-constant type.
189 Both calls to f are at the same type, so we can specialise to give:
190
191         g a (d::Num a) (y::a) = let f@a (x::a) = +.sel a d x x
192                                 in +.sel a d (f@a y) (f@a y)
193
194
195 (b) The other case is when the type variables in the instance types
196 are *not* in scope at the definition point of f.  The example we are
197 working with above is a good case.  There are two instances of (+.sel a d),
198 but "a" is not in scope at the definition of +.sel.  Can we do anything?
199 Yes, we can "common them up", a sort of limited common sub-expression deal.
200 This would give:
201
202         g a (d::Num a) (y::a) = let +.sel@a = +.sel a d
203                                     f@a (x::a) = +.sel@a x x
204                                 in +.sel@a (f@a y) (f@a y)
205
206 This can save work, and can't be spotted by the type checker, because
207 the two instances of +.sel weren't originally at the same type.
208
209 Further notes on (b)
210
211 * There are quite a few variations here.  For example, the defn of
212   +.sel could be floated ouside the \y, to attempt to gain laziness.
213   It certainly mustn't be floated outside the \d because the d has to
214   be in scope too.
215
216 * We don't want to inline f_rhs in this case, because
217 that will duplicate code.  Just commoning up the call is the point.
218
219 * Nothing gets added to +.sel's IdInfo.
220
221 * Don't bother unless the equivalence class has more than one item!
222
223 Not clear whether this is all worth it.  It is of course OK to
224 simply discard call-instances when passing a big lambda.
225
226 Polymorphism 2 -- Overloading
227 ~~~~~~~~~~~~~~
228 Consider a function whose most general type is
229
230         f :: forall a b. Ord a => [a] -> b -> b
231
232 There is really no point in making a version of g at Int/Int and another
233 at Int/Bool, because it's only instancing the type variable "a" which
234 buys us any efficiency. Since g is completely polymorphic in b there
235 ain't much point in making separate versions of g for the different
236 b types.
237
238 That suggests that we should identify which of g's type variables
239 are constrained (like "a") and which are unconstrained (like "b").
240 Then when taking equivalence classes in STEP 2, we ignore the type args
241 corresponding to unconstrained type variable.  In STEP 3 we make
242 polymorphic versions.  Thus:
243
244         f@t1/ = /\b -> <f_rhs> t1 b d1 d2
245
246 We do this.
247
248
249 Dictionary floating
250 ~~~~~~~~~~~~~~~~~~~
251 Consider this
252
253         f a (d::Num a) = let g = ...
254                          in
255                          ...(let d1::Ord a = Num.Ord.sel a d in g a d1)...
256
257 Here, g is only called at one type, but the dictionary isn't in scope at the
258 definition point for g.  Usually the type checker would build a
259 definition for d1 which enclosed g, but the transformation system
260 might have moved d1's defn inward.  Solution: float dictionary bindings
261 outwards along with call instances.
262
263 Consider
264
265         f x = let g p q = p==q
266                   h r s = (r+s, g r s)
267               in
268               h x x
269
270
271 Before specialisation, leaving out type abstractions we have
272
273         f df x = let g :: Eq a => a -> a -> Bool
274                      g dg p q = == dg p q
275                      h :: Num a => a -> a -> (a, Bool)
276                      h dh r s = let deq = eqFromNum dh
277                                 in (+ dh r s, g deq r s)
278               in
279               h df x x
280
281 After specialising h we get a specialised version of h, like this:
282
283                     h' r s = let deq = eqFromNum df
284                              in (+ df r s, g deq r s)
285
286 But we can't naively make an instance for g from this, because deq is not in scope
287 at the defn of g.  Instead, we have to float out the (new) defn of deq
288 to widen its scope.  Notice that this floating can't be done in advance -- it only
289 shows up when specialisation is done.
290
291 User SPECIALIZE pragmas
292 ~~~~~~~~~~~~~~~~~~~~~~~
293 Specialisation pragmas can be digested by the type checker, and implemented
294 by adding extra definitions along with that of f, in the same way as before
295
296         f@t1/t2 = <f_rhs> t1 t2 d1 d2
297
298 Indeed the pragmas *have* to be dealt with by the type checker, because
299 only it knows how to build the dictionaries d1 and d2!  For example
300
301         g :: Ord a => [a] -> [a]
302         {-# SPECIALIZE f :: [Tree Int] -> [Tree Int] #-}
303
304 Here, the specialised version of g is an application of g's rhs to the
305 Ord dictionary for (Tree Int), which only the type checker can conjure
306 up.  There might not even *be* one, if (Tree Int) is not an instance of
307 Ord!  (All the other specialision has suitable dictionaries to hand
308 from actual calls.)
309
310 Problem.  The type checker doesn't have to hand a convenient <f_rhs>, because
311 it is buried in a complex (as-yet-un-desugared) binding group.
312 Maybe we should say
313
314         f@t1/t2 = f* t1 t2 d1 d2
315
316 where f* is the Id f with an IdInfo which says "inline me regardless!".
317 Indeed all the specialisation could be done in this way.
318 That in turn means that the simplifier has to be prepared to inline absolutely
319 any in-scope let-bound thing.
320
321
322 Again, the pragma should permit polymorphism in unconstrained variables:
323
324         h :: Ord a => [a] -> b -> b
325         {-# SPECIALIZE h :: [Int] -> b -> b #-}
326
327 We *insist* that all overloaded type variables are specialised to ground types,
328 (and hence there can be no context inside a SPECIALIZE pragma).
329 We *permit* unconstrained type variables to be specialised to
330         - a ground type
331         - or left as a polymorphic type variable
332 but nothing in between.  So
333
334         {-# SPECIALIZE h :: [Int] -> [c] -> [c] #-}
335
336 is *illegal*.  (It can be handled, but it adds complication, and gains the
337 programmer nothing.)
338
339
340 SPECIALISING INSTANCE DECLARATIONS
341 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
342 Consider
343
344         instance Foo a => Foo [a] where
345                 ...
346         {-# SPECIALIZE instance Foo [Int] #-}
347
348 The original instance decl creates a dictionary-function
349 definition:
350
351         dfun.Foo.List :: forall a. Foo a -> Foo [a]
352
353 The SPECIALIZE pragma just makes a specialised copy, just as for
354 ordinary function definitions:
355
356         dfun.Foo.List@Int :: Foo [Int]
357         dfun.Foo.List@Int = dfun.Foo.List Int dFooInt
358
359 The information about what instance of the dfun exist gets added to
360 the dfun's IdInfo in the same way as a user-defined function too.
361
362
363 Automatic instance decl specialisation?
364 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
365 Can instance decls be specialised automatically?  It's tricky.
366 We could collect call-instance information for each dfun, but
367 then when we specialised their bodies we'd get new call-instances
368 for ordinary functions; and when we specialised their bodies, we might get
369 new call-instances of the dfuns, and so on.  This all arises because of
370 the unrestricted mutual recursion between instance decls and value decls.
371
372 Still, there's no actual problem; it just means that we may not do all
373 the specialisation we could theoretically do.
374
375 Furthermore, instance decls are usually exported and used non-locally,
376 so we'll want to compile enough to get those specialisations done.
377
378 Lastly, there's no such thing as a local instance decl, so we can
379 survive solely by spitting out *usage* information, and then reading that
380 back in as a pragma when next compiling the file.  So for now,
381 we only specialise instance decls in response to pragmas.
382
383
384 SPITTING OUT USAGE INFORMATION
385 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
386
387 To spit out usage information we need to traverse the code collecting
388 call-instance information for all imported (non-prelude?) functions
389 and data types. Then we equivalence-class it and spit it out.
390
391 This is done at the top-level when all the call instances which escape
392 must be for imported functions and data types.
393
394 *** Not currently done ***
395
396
397 Partial specialisation by pragmas
398 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
399 What about partial specialisation:
400
401         k :: (Ord a, Eq b) => [a] -> b -> b -> [a]
402         {-# SPECIALIZE k :: Eq b => [Int] -> b -> b -> [a] #-}
403
404 or even
405
406         {-# SPECIALIZE k :: Eq b => [Int] -> [b] -> [b] -> [a] #-}
407
408 Seems quite reasonable.  Similar things could be done with instance decls:
409
410         instance (Foo a, Foo b) => Foo (a,b) where
411                 ...
412         {-# SPECIALIZE instance Foo a => Foo (a,Int) #-}
413         {-# SPECIALIZE instance Foo b => Foo (Int,b) #-}
414
415 Ho hum.  Things are complex enough without this.  I pass.
416
417
418 Requirements for the simplifer
419 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
420 The simplifier has to be able to take advantage of the specialisation.
421
422 * When the simplifier finds an application of a polymorphic f, it looks in
423 f's IdInfo in case there is a suitable instance to call instead.  This converts
424
425         f t1 t2 d1 d2   ===>   f_t1_t2
426
427 Note that the dictionaries get eaten up too!
428
429 * Dictionary selection operations on constant dictionaries must be
430   short-circuited:
431
432         +.sel Int d     ===>  +Int
433
434 The obvious way to do this is in the same way as other specialised
435 calls: +.sel has inside it some IdInfo which tells that if it's applied
436 to the type Int then it should eat a dictionary and transform to +Int.
437
438 In short, dictionary selectors need IdInfo inside them for constant
439 methods.
440
441 * Exactly the same applies if a superclass dictionary is being
442   extracted:
443
444         Eq.sel Int d   ===>   dEqInt
445
446 * Something similar applies to dictionary construction too.  Suppose
447 dfun.Eq.List is the function taking a dictionary for (Eq a) to
448 one for (Eq [a]).  Then we want
449
450         dfun.Eq.List Int d      ===> dEq.List_Int
451
452 Where does the Eq [Int] dictionary come from?  It is built in
453 response to a SPECIALIZE pragma on the Eq [a] instance decl.
454
455 In short, dfun Ids need IdInfo with a specialisation for each
456 constant instance of their instance declaration.
457
458 All this uses a single mechanism: the SpecEnv inside an Id
459
460
461 What does the specialisation IdInfo look like?
462 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
463
464 The SpecEnv of an Id maps a list of types (the template) to an expression
465
466         [Type]  |->  Expr
467
468 For example, if f has this SpecInfo:
469
470         [Int, a]  ->  \d:Ord Int. f' a
471
472 it means that we can replace the call
473
474         f Int t  ===>  (\d. f' t)
475
476 This chucks one dictionary away and proceeds with the
477 specialised version of f, namely f'.
478
479
480 What can't be done this way?
481 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
482 There is no way, post-typechecker, to get a dictionary for (say)
483 Eq a from a dictionary for Eq [a].  So if we find
484
485         ==.sel [t] d
486
487 we can't transform to
488
489         eqList (==.sel t d')
490
491 where
492         eqList :: (a->a->Bool) -> [a] -> [a] -> Bool
493
494 Of course, we currently have no way to automatically derive
495 eqList, nor to connect it to the Eq [a] instance decl, but you
496 can imagine that it might somehow be possible.  Taking advantage
497 of this is permanently ruled out.
498
499 Still, this is no great hardship, because we intend to eliminate
500 overloading altogether anyway!
501
502
503
504 A note about non-tyvar dictionaries
505 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
506 Some Ids have types like
507
508         forall a,b,c. Eq a -> Ord [a] -> tau
509
510 This seems curious at first, because we usually only have dictionary
511 args whose types are of the form (C a) where a is a type variable.
512 But this doesn't hold for the functions arising from instance decls,
513 which sometimes get arguements with types of form (C (T a)) for some
514 type constructor T.
515
516 Should we specialise wrt this compound-type dictionary?  We used to say
517 "no", saying:
518         "This is a heuristic judgement, as indeed is the fact that we 
519         specialise wrt only dictionaries.  We choose *not* to specialise
520         wrt compound dictionaries because at the moment the only place
521         they show up is in instance decls, where they are simply plugged
522         into a returned dictionary.  So nothing is gained by specialising
523         wrt them."
524
525 But it is simpler and more uniform to specialise wrt these dicts too;
526 and in future GHC is likely to support full fledged type signatures 
527 like
528         f ;: Eq [(a,b)] => ...
529
530
531 %************************************************************************
532 %*                                                                      *
533 \subsubsection{The new specialiser}
534 %*                                                                      *
535 %************************************************************************
536
537 Our basic game plan is this.  For let(rec) bound function
538         f :: (C a, D c) => (a,b,c,d) -> Bool
539
540 * Find any specialised calls of f, (f ts ds), where 
541   ts are the type arguments t1 .. t4, and
542   ds are the dictionary arguments d1 .. d2.
543
544 * Add a new definition for f1 (say):
545
546         f1 = /\ b d -> (..body of f..) t1 b t3 d d1 d2
547
548   Note that we abstract over the unconstrained type arguments.
549
550 * Add the mapping
551
552         [t1,b,t3,d]  |->  \d1 d2 -> f1 b d
553
554   to the specialisations of f.  This will be used by the
555   simplifier to replace calls 
556                 (f t1 t2 t3 t4) da db
557   by
558                 (\d1 d1 -> f1 t2 t4) da db
559
560   All the stuff about how many dictionaries to discard, and what types
561   to apply the specialised function to, are handled by the fact that the
562   SpecEnv contains a template for the result of the specialisation.
563
564 We don't build *partial* specialisations for f.  For example:
565
566   f :: Eq a => a -> a -> Bool
567   {-# SPECIALISE f :: (Eq b, Eq c) => (b,c) -> (b,c) -> Bool #-}
568
569 Here, little is gained by making a specialised copy of f.
570 There's a distinct danger that the specialised version would
571 first build a dictionary for (Eq b, Eq c), and then select the (==) 
572 method from it!  Even if it didn't, not a great deal is saved.
573
574 We do, however, generate polymorphic, but not overloaded, specialisations:
575
576   f :: Eq a => [a] -> b -> b -> b
577   {#- SPECIALISE f :: [Int] -> b -> b -> b #-}
578
579 Hence, the invariant is this: 
580
581         *** no specialised version is overloaded ***
582
583
584 %************************************************************************
585 %*                                                                      *
586 \subsubsection{The exported function}
587 %*                                                                      *
588 %************************************************************************
589
590 \begin{code}
591 specProgram :: UniqSupply -> [CoreBinding] -> [CoreBinding]
592 specProgram us binds
593   = initSM us (go binds         `thenSM` \ (binds', uds') ->
594                returnSM (dumpAllDictBinds uds' binds')
595               )
596   where
597     go []           = returnSM ([], emptyUDs)
598     go (bind:binds) = go binds          `thenSM` \ (binds', uds) ->
599                       specBind bind uds `thenSM` \ (bind', uds') ->
600                       returnSM (bind' ++ binds', uds')
601 \end{code}
602
603 %************************************************************************
604 %*                                                                      *
605 \subsubsection{@specExpr@: the main function}
606 %*                                                                      *
607 %************************************************************************
608
609 \begin{code}
610 specExpr :: CoreExpr -> SpecM (CoreExpr, UsageDetails)
611
612 ---------------- First the easy cases --------------------
613 specExpr e@(Var _)    = returnSM (e, emptyUDs)
614 specExpr e@(Lit _)    = returnSM (e, emptyUDs)
615 specExpr e@(Con _ _)  = returnSM (e, emptyUDs)
616 specExpr e@(Prim _ _) = returnSM (e, emptyUDs)
617
618 specExpr (Note note body)
619   = specExpr body       `thenSM` \ (body', uds) ->
620     returnSM (Note note body', uds)
621
622
623 ---------------- Applications might generate a call instance --------------------
624 specExpr e@(App fun arg)
625   = go fun [arg]
626   where
627     go (App fun arg) args = go fun (arg:args)
628     go (Var f)       args = returnSM (e, mkCallUDs f args)
629     go other         args = specExpr other      `thenSM` \ (e', uds) ->
630                             returnSM (foldl App e' args, uds)
631
632 ---------------- Lambda/case require dumping of usage details --------------------
633 specExpr e@(Lam _ _)
634   = specExpr body       `thenSM` \ (body', uds) ->
635     let
636         (filtered_uds, body'') = dumpUDs bndrs uds body'
637     in
638     returnSM (foldr Lam body'' bndrs, filtered_uds)
639   where
640     (bndrs, body) = go [] e
641
642         -- More efficient to collect a group of binders together all at once
643     go bndrs (Lam bndr e) = go (bndr:bndrs) e
644     go bndrs e            = (reverse bndrs, e)
645
646
647 specExpr (Case scrut alts)
648   = specExpr scrut      `thenSM` \ (scrut', uds_scrut) ->
649     spec_alts alts      `thenSM` \ (alts', uds_alts) ->
650     returnSM (Case scrut' alts', uds_scrut `plusUDs` uds_alts)
651   where
652     spec_alts (AlgAlts alts deflt)
653         = mapAndCombineSM spec_alg_alt alts     `thenSM` \ (alts', uds1) ->
654           spec_deflt deflt                      `thenSM` \ (deflt', uds2) ->
655           returnSM (AlgAlts alts' deflt', uds1 `plusUDs` uds2)
656
657     spec_alts (PrimAlts alts deflt)
658         = mapAndCombineSM spec_prim_alt alts    `thenSM` \ (alts', uds1) ->
659           spec_deflt deflt                      `thenSM` \ (deflt', uds2) ->
660           returnSM (PrimAlts alts' deflt', uds1 `plusUDs` uds2)
661
662     spec_alg_alt (con, args, rhs)
663         = specExpr rhs          `thenSM` \ (rhs', uds) ->
664           let
665              (uds', rhs'') = dumpUDs (map ValBinder args) uds rhs'
666           in
667           returnSM ((con, args, rhs''), uds')
668
669     spec_prim_alt (lit, rhs)
670         = specExpr rhs          `thenSM` \ (rhs', uds) ->
671           returnSM ((lit, rhs'), uds)
672
673     spec_deflt NoDefault = returnSM (NoDefault, emptyUDs)
674     spec_deflt (BindDefault arg rhs)
675         = specExpr rhs          `thenSM` \ (rhs', uds) ->
676           let
677              (uds', rhs'') = dumpUDs [ValBinder arg] uds rhs'
678           in
679           returnSM (BindDefault arg rhs'', uds')
680
681 ---------------- Finally, let is the interesting case --------------------
682 specExpr (Let bind body)
683   =     -- Deal with the body
684     specExpr body                               `thenSM` \ (body', body_uds) ->
685
686         -- Deal with the bindings
687     specBind bind body_uds                      `thenSM` \ (binds', uds) ->
688
689         -- All done
690     returnSM (foldr Let body' binds', uds)
691 \end{code}
692
693 %************************************************************************
694 %*                                                                      *
695 \subsubsection{Dealing with a binding}
696 %*                                                                      *
697 %************************************************************************
698
699 \begin{code}
700 specBind :: CoreBinding
701          -> UsageDetails                -- Info on how the scope of the binding
702          -> SpecM ([CoreBinding],       -- New bindings
703                    UsageDetails)        -- And info to pass upstream
704
705 specBind (NonRec bndr rhs) body_uds
706   | isDictTy (idType bndr)
707   =     -- It's a dictionary binding
708         -- Pick it up and float it outwards.
709     specExpr rhs                                `thenSM` \ (rhs', rhs_uds) ->
710     let
711         all_uds = rhs_uds `plusUDs` addDictBind body_uds bndr rhs'
712     in
713     returnSM ([], all_uds)
714
715   | isSpecPragmaId bndr
716   = specExpr rhs                                `thenSM` \ (rhs', rhs_uds) ->
717     returnSM ([], rhs_uds `plusUDs` body_uds)
718
719   | otherwise
720   =   -- Deal with the RHS, specialising it according
721       -- to the calls found in the body
722     specDefn (calls body_uds) (bndr,rhs)        `thenSM` \ ((bndr',rhs'), spec_defns, spec_uds) ->
723     let
724         (all_uds, (dict_binds, dump_calls)) 
725                 = splitUDs [ValBinder bndr]
726                            (body_uds `plusUDs` spec_uds)
727                         -- It's important that the `plusUDs` is this way round,
728                         -- because body_uds may bind dictionaries that are
729                         -- used in the calls passed to specDefn.  So the
730                         -- dictionary bindings in spec_uds may mention 
731                         -- dictionaries bound in body_uds.
732
733         -- If we make specialisations then we Rec the whole lot together
734         -- If not, leave it as a NonRec
735         new_bind | null spec_defns = NonRec bndr' rhs'
736                  | otherwise       = Rec ((bndr',rhs'):spec_defns)
737     in
738     returnSM ( new_bind : mkDictBinds dict_binds, all_uds )
739
740 specBind (Rec pairs) body_uds
741   = mapSM (specDefn (calls body_uds)) pairs     `thenSM` \ stuff ->
742     let
743         (pairs', spec_defns_s, spec_uds_s) = unzip3 stuff
744         spec_defns = concat spec_defns_s
745         spec_uds   = plusUDList spec_uds_s
746
747         (all_uds, (dict_binds, dump_calls)) 
748                 = splitUDs (map (ValBinder . fst) pairs)
749                            (body_uds `plusUDs` spec_uds)
750                         -- See notes for non-rec case
751
752         new_bind = Rec (spec_defns ++ pairs')
753     in
754     returnSM (  new_bind : mkDictBinds dict_binds, all_uds )
755     
756 specDefn :: CallDetails                 -- Info on how it is used in its scope
757          -> (Id, CoreExpr)              -- The thing being bound and its un-processed RHS
758          -> SpecM ((Id, CoreExpr),      -- The thing and its processed RHS
759                                         --      the Id may now have specialisations attached
760                    [(Id,CoreExpr)],     -- Extra, specialised bindings
761                    UsageDetails         -- Stuff to fling upwards from the RHS and its
762             )                           --      specialised versions
763
764 specDefn calls (fn, rhs)
765         -- The first case is the interesting one
766   |  n_tyvars == length rhs_tyvars      -- Rhs of fn's defn has right number of big lambdas
767   && n_dicts  <= length rhs_bndrs       -- and enough dict args
768   && not (null calls_for_me)            -- And there are some calls to specialise
769   =   -- Specialise the body of the function
770     specExpr body                                       `thenSM` \ (body', body_uds) ->
771     let
772         (float_uds, bound_uds@(dict_binds,_)) = splitUDs rhs_bndrs body_uds
773     in
774
775       -- Make a specialised version for each call in calls_for_me
776     mapSM (spec_call bound_uds) calls_for_me            `thenSM` \ stuff ->
777     let
778         (spec_defns, spec_uds, spec_env_stuff) = unzip3 stuff
779
780         fn'  = addIdSpecialisations fn spec_env_stuff
781         rhs' = foldr Lam (mkDictLets dict_binds body') rhs_bndrs 
782     in
783     returnSM ((fn',rhs'), 
784               spec_defns, 
785               float_uds `plusUDs` plusUDList spec_uds)
786
787   | otherwise   -- No calls or RHS doesn't fit our preconceptions
788   = specExpr rhs                        `thenSM` \ (rhs', rhs_uds) ->
789     returnSM ((fn, rhs'), [], rhs_uds)
790   
791   where
792     fn_type              = idType fn
793     (tyvars, theta, tau) = splitSigmaTy fn_type
794     n_tyvars             = length tyvars
795     n_dicts              = length theta
796     mk_spec_tys call_ts  = zipWith mk_spec_ty call_ts tyVarTemplates
797                          where
798                            mk_spec_ty (Just ty) _     = ty
799                            mk_spec_ty Nothing   tyvar = mkTyVarTy tyvar
800
801     (rhs_tyvars, rhs_ids, rhs_body) = collectBinders rhs
802     rhs_dicts = take n_dicts rhs_ids
803     rhs_bndrs = map TyBinder rhs_tyvars ++ map ValBinder rhs_dicts
804     body      = mkValLam (drop n_dicts rhs_ids) rhs_body
805                 -- Glue back on the non-dict lambdas
806
807     calls_for_me = case lookupFM calls fn of
808                         Nothing -> []
809                         Just cs -> fmToList cs
810
811     ----------------------------------------------------------
812         -- Specialise to one particular call pattern
813     spec_call :: ProtoUsageDetails          -- From the original body, captured by
814                                             -- the dictionary lambdas
815               -> ([Maybe Type], [DictVar])  -- Call instance
816               -> SpecM ((Id,CoreExpr),            -- Specialised definition
817                         UsageDetails,             -- Usage details from specialised body
818                         ([TyVar], [Type], CoreExpr))       -- Info for the Id's SpecEnv
819     spec_call bound_uds (call_ts, call_ds)
820       = ASSERT( length call_ts == n_tyvars && length call_ds == n_dicts )
821                 -- Calls are only recorded for properly-saturated applications
822         
823         -- Supppose the call is for f [Just t1, Nothing, Just t3, Nothing] [d1, d2]
824
825                 -- Construct the new binding
826                 --      f1 = /\ b d -> (..rhs of f..) t1 b t3 d d1 d2
827                 -- and the type of this binder
828         let
829            spec_tyvars = [tyvar | (tyvar, Nothing) <- tyVarTemplates `zip` call_ts]
830            spec_tys    = mk_spec_tys call_ts
831            spec_rhs    = mkTyLam spec_tyvars $
832                          mkGenApp rhs (map TyArg spec_tys ++ map VarArg call_ds)
833            spec_id_ty  = mkForAllTys spec_tyvars (instantiateTy ty_env tau)
834            ty_env      = mkTyVarEnv (zipEqual "spec_call" tyvars spec_tys)
835         in
836
837         newIdSM fn spec_id_ty           `thenSM` \ spec_f ->
838
839
840                 -- Construct the stuff for f's spec env
841                 --      [b,d] [t1,b,t3,d]  |->  \d1 d2 -> f1 b d
842                 -- The only awkward bit is that d1,d2 might well be global
843                 -- dictionaries, so it's tidier to make new local variables
844                 -- for the lambdas in the RHS, rather than lambda-bind the
845                 -- dictionaries themselves.
846                 --
847                 -- In fact we use the standard template locals, so that the
848                 -- they don't need to be "tidied" before putting in interface files
849         let
850            arg_ds        = mkTemplateLocals (map idType call_ds)
851            spec_env_rhs  = mkValLam arg_ds $
852                            mkTyApp (Var spec_f) $
853                            map mkTyVarTy spec_tyvars
854            spec_env_info = (spec_tyvars, spec_tys, spec_env_rhs)
855         in
856
857                 -- Specialise the UDs from f's RHS
858         let
859                 -- Only the overloaded tyvars should be free in the uds
860            ty_env   = [ (rhs_tyvar,ty) 
861                       | (rhs_tyvar, Just ty) <- zipEqual "specUDs1" rhs_tyvars call_ts
862                       ]
863            dict_env = zipEqual "specUDs2" rhs_dicts call_ds
864         in
865         specUDs ty_env dict_env bound_uds                       `thenSM` \ spec_uds ->
866
867         returnSM ((spec_f, spec_rhs),
868                   spec_uds,
869                   spec_env_info
870         )
871 \end{code}
872
873 %************************************************************************
874 %*                                                                      *
875 \subsubsection{UsageDetails and suchlike}
876 %*                                                                      *
877 %************************************************************************
878
879 \begin{code}
880 type FreeDicts = IdSet
881
882 data UsageDetails 
883   = MkUD {
884         dict_binds :: !(Bag DictBind),
885                         -- Floated dictionary bindings
886                         -- The order is important; 
887                         -- in ds1 `union` ds2, bindings in ds2 can depend on those in ds1
888                         -- (Remember, Bags preserve order in GHC.)
889                         -- The FreeDicts is the free vars of the RHS
890
891         calls     :: !CallDetails
892     }
893
894 type DictBind = (DictVar, CoreExpr, TyVarSet, FreeDicts)
895                         -- The FreeDicts are the free dictionaries (only)
896                         -- of the RHS of the dictionary bindings
897                         -- Similarly the TyVarSet
898
899 emptyUDs = MkUD { dict_binds = emptyBag, calls = emptyFM }
900
901 type ProtoUsageDetails = ([DictBind],
902                           [(Id, [Maybe Type], [DictVar])]
903                          )
904
905 ------------------------------------------------------------                    
906 type CallDetails  = FiniteMap Id CallInfo
907 type CallInfo     = FiniteMap [Maybe Type]      -- Nothing => unconstrained type argument
908                               [DictVar]         -- Dict args
909         -- The finite maps eliminate duplicates
910         -- The list of types and dictionaries is guaranteed to
911         -- match the type of f
912
913 callDetailsToList calls = [ (id,tys,dicts)
914                           | (id,fm) <- fmToList calls,
915                             (tys,dicts) <- fmToList fm
916                           ]
917
918 listToCallDetails calls  = foldr (unionCalls . singleCall) emptyFM calls
919
920 unionCalls :: CallDetails -> CallDetails -> CallDetails
921 unionCalls c1 c2 = plusFM_C plusFM c1 c2
922
923 singleCall (id, tys, dicts) = unitFM id (unitFM tys dicts)
924
925 mkCallUDs f args 
926   | null theta
927   || length spec_tys /= n_tyvars
928   || length dicts    /= n_dicts
929   = emptyUDs    -- Not overloaded
930
931   | otherwise
932   = MkUD {dict_binds = emptyBag, 
933           calls = singleCall (f, spec_tys, dicts)
934     }
935   where
936     (tyvars, theta, tau) = splitSigmaTy (idType f)
937     constrained_tyvars   = foldr (unionTyVarSets . tyVarsOfTypes . snd) emptyTyVarSet theta 
938     n_tyvars             = length tyvars
939     n_dicts              = length theta
940
941     spec_tys = [mk_spec_ty tv ty | (tv, TyArg ty) <- tyvars `zip` args]
942     dicts    = [d | (_, VarArg d) <- theta `zip` (drop n_tyvars args)]
943     
944     mk_spec_ty tyvar ty | tyvar `elementOfTyVarSet` constrained_tyvars
945                         = Just ty
946                         | otherwise
947                         = Nothing
948
949 ------------------------------------------------------------                    
950 plusUDs :: UsageDetails -> UsageDetails -> UsageDetails
951 plusUDs (MkUD {dict_binds = db1, calls = calls1})
952         (MkUD {dict_binds = db2, calls = calls2})
953   = MkUD {dict_binds, calls}
954   where
955     dict_binds = db1    `unionBags`   db2 
956     calls      = calls1 `unionCalls`  calls2
957
958 plusUDList = foldr plusUDs emptyUDs
959
960 mkDB dict rhs = (dict, rhs, db_ftvs, db_fvs)
961               where
962                 db_ftvs = tyVarsOfType (idType dict)    -- Superset of RHS fvs
963                 db_fvs  = dictRhsFVs rhs
964
965 addDictBind uds dict rhs = uds { dict_binds = mkDB dict rhs `consBag` dict_binds uds }
966
967 dumpAllDictBinds (MkUD {dict_binds = dbs}) binds
968   = foldrBag add binds dbs
969   where
970     add (dict,rhs,_,_) binds = NonRec dict rhs : binds
971
972 mkDictBinds :: [DictBind] -> [CoreBinding]
973 mkDictBinds = map (\(d,r,_,_) -> NonRec d r)
974
975 mkDictLets :: [DictBind] -> CoreExpr -> CoreExpr
976 mkDictLets dbs body = foldr mk body dbs
977                     where
978                       mk (d,r,_,_) e = Let (NonRec d r) e 
979
980 dumpUDs :: [CoreBinder]
981         -> UsageDetails -> CoreExpr
982         -> (UsageDetails, CoreExpr)
983 dumpUDs bndrs uds body
984   = (free_uds, mkDictLets dict_binds body)
985   where
986     (free_uds, (dict_binds, _)) = splitUDs bndrs uds
987
988 splitUDs :: [CoreBinder]
989          -> UsageDetails
990          -> (UsageDetails,              -- These don't mention the binders
991              ProtoUsageDetails)         -- These do
992              
993 splitUDs bndrs uds@(MkUD {dict_binds = orig_dbs, 
994                           calls      = orig_calls})
995
996   = if isEmptyBag dump_dbs && null dump_calls then
997         -- Common case: binder doesn't affect floats
998         (uds, ([],[]))  
999
1000     else
1001         -- Binders bind some of the fvs of the floats
1002         (MkUD {dict_binds = free_dbs, 
1003                calls      = listToCallDetails free_calls},
1004          (bagToList dump_dbs, dump_calls)
1005         )
1006
1007   where
1008     tyvar_set    = mkTyVarSet [tv | TyBinder tv <- bndrs]
1009     id_set       = mkIdSet    [id | ValBinder id <- bndrs]
1010
1011     (free_dbs, dump_dbs, dump_idset) 
1012           = foldlBag dump_db (emptyBag, emptyBag, id_set) orig_dbs
1013                 -- Important that it's foldl not foldr;
1014                 -- we're accumulating the set of dumped ids in dump_set
1015
1016         -- Filter out any calls that mention things that are being dumped
1017         -- Don't need to worry about the tyvars because the dicts will
1018         -- spot the captured ones; any fully polymorphic arguments will
1019         -- be Nothings in the call details
1020     orig_call_list = callDetailsToList orig_calls
1021     (dump_calls, free_calls) = partition captured orig_call_list
1022     captured (id,tys,dicts)  = any (`elementOfIdSet` dump_idset) (id:dicts)
1023
1024     dump_db (free_dbs, dump_dbs, dump_idset) db@(dict, rhs, ftvs, fvs)
1025         |  isEmptyIdSet    (dump_idset `intersectIdSets`    fvs)
1026         && isEmptyTyVarSet (tyvar_set  `intersectTyVarSets` ftvs)
1027         = (free_dbs `snocBag` db, dump_dbs, dump_idset)
1028
1029         | otherwise     -- Dump it
1030         = (free_dbs, dump_dbs `snocBag` db,
1031            dump_idset `addOneToIdSet` dict)
1032 \end{code}
1033
1034 Given a type and value substitution, specUDs creates a specialised copy of
1035 the given UDs
1036
1037 \begin{code}
1038 specUDs :: [(TyVar,Type)] -> [(DictVar,DictVar)] -> ProtoUsageDetails -> SpecM UsageDetails
1039 specUDs tv_env_list dict_env_list (dbs, calls)
1040   = specDBs dict_env_list dbs           `thenSM` \ (dict_env_list', dbs') ->
1041     let
1042         dict_env = mkIdEnv dict_env_list'
1043     in
1044     returnSM (MkUD { dict_binds = dbs',
1045                      calls      = listToCallDetails (map (inst_call dict_env) calls)
1046     })
1047   where
1048     bound_tyvars = mkTyVarSet (map fst tv_env_list)
1049     tv_env   = mkTyVarEnv tv_env_list   -- Doesn't change
1050
1051     inst_call dict_env (id, tys, dicts) = (id, map inst_maybe_ty tys, 
1052                                                map (lookupId dict_env) dicts)
1053
1054     inst_maybe_ty Nothing   = Nothing
1055     inst_maybe_ty (Just ty) = Just (instantiateTy tv_env ty)
1056
1057     specDBs dict_env []
1058         = returnSM (dict_env, emptyBag)
1059     specDBs dict_env ((dict, rhs, ftvs, fvs) : dbs)
1060         = newIdSM dict (instantiateTy tv_env (idType dict))     `thenSM` \ dict' ->
1061           let
1062             rhs'      = foldl App (foldr Lam rhs (t_bndrs ++ d_bndrs)) (t_args ++ d_args)
1063             (t_bndrs, t_args) = unzip [(TyBinder tv, TyArg ty)  | (tv,ty) <- tv_env_list,
1064                                                                    tv `elementOfTyVarSet` ftvs]
1065             (d_bndrs, d_args) = unzip [(ValBinder d, VarArg d') | (d,d')  <- dict_env,
1066                                                                    d `elementOfIdSet` fvs]
1067             dict_env' = (dict,dict') : dict_env
1068             ftvs' = tyVarsOfTypes [ty | TyArg ty <- t_args] `unionTyVarSets`
1069                     (ftvs `minusTyVarSet` bound_tyvars)
1070             fvs'  = mkIdSet [d | VarArg d <- d_args] `unionIdSets`
1071                     (fvs `minusIdSet` mkIdSet [d | ValBinder d <- d_bndrs])
1072           in
1073           specDBs dict_env' dbs         `thenSM` \ (dict_env'', dbs') ->
1074           returnSM ( dict_env'', (dict', rhs', ftvs', fvs') `consBag` dbs' )
1075 \end{code}
1076
1077 %************************************************************************
1078 %*                                                                      *
1079 \subsubsection{Boring helper functions}
1080 %*                                                                      *
1081 %************************************************************************
1082
1083 \begin{code}
1084 tyVarTemplates :: [TyVar]
1085 tyVarTemplates = map mk [1..]
1086   where
1087     mk i = mkTyVar (mkSysLocalName uniq occ noSrcLoc) mkBoxedTypeKind
1088          where
1089            uniq = mkAlphaTyVarUnique i
1090            occ  = _PK_ ("$t" ++ show i)
1091 \end{code}
1092
1093 \begin{code}
1094 lookupId:: IdEnv Id -> Id -> Id
1095 lookupId env id = case lookupIdEnv env id of
1096                         Nothing  -> id
1097                         Just id' -> id'
1098
1099 dictRhsFVs :: CoreExpr -> IdSet
1100 dictRhsFVs e = exprFreeVars isLocallyDefined e
1101
1102 addIdSpecialisations id spec_stuff
1103   = (if not (null errs) then
1104         pprTrace "Duplicate specialisations" (vcat (map ppr errs))
1105      else \x -> x
1106     )
1107     setIdSpecialisation id new_spec_env
1108   where
1109     (new_spec_env, errs) = foldr add (getIdSpecialisation id, []) spec_stuff
1110
1111     add (tyvars, tys, template) (spec_env, errs)
1112         = case addToSpecEnv True spec_env tyvars tys template of
1113                 Succeeded spec_env' -> (spec_env', errs)
1114                 Failed err          -> (spec_env, err:errs)
1115
1116 -- Given an Id, isSpecVars returns all its specialisations.
1117 -- We extract these from its SpecEnv.
1118 -- This is used by the occurrence analyser and free-var finder;
1119 -- we regard an Id's specialisations as free in the Id's definition.
1120
1121 idSpecVars :: Id -> [Id]
1122 idSpecVars id 
1123   = map get_spec (specEnvValues (getIdSpecialisation id))
1124   where
1125     -- get_spec is another cheapo function like dictRhsFVs
1126     -- It knows what these specialisation temlates look like,
1127     -- and just goes for the jugular
1128     get_spec (App f _) = get_spec f
1129     get_spec (Lam _ b) = get_spec b
1130     get_spec (Var v)   = v
1131
1132 ----------------------------------------
1133 type SpecM a = UniqSM a
1134
1135 thenSM    = thenUs
1136 returnSM  = returnUs
1137 getUniqSM = getUnique
1138 mapSM     = mapUs
1139 initSM    = initUs
1140
1141 mapAndCombineSM f []     = returnSM ([], emptyUDs)
1142 mapAndCombineSM f (x:xs) = f x  `thenSM` \ (y, uds1) ->
1143                            mapAndCombineSM f xs `thenSM` \ (ys, uds2) ->
1144                            returnSM (y:ys, uds1 `plusUDs` uds2)
1145
1146 newIdSM old_id new_ty
1147   = getUnique           `thenSM` \ uniq ->
1148     returnSM (mkUserLocal (getOccName old_id) 
1149                           uniq
1150                           new_ty
1151                           (getSrcLoc old_id)
1152     )
1153 \end{code}
1154
1155
1156                 Old (but interesting) stuff about unboxed bindings
1157                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1158
1159 What should we do when a value is specialised to a *strict* unboxed value?
1160
1161         map_*_* f (x:xs) = let h = f x
1162                                t = map f xs
1163                            in h:t
1164
1165 Could convert let to case:
1166
1167         map_*_Int# f (x:xs) = case f x of h# ->
1168                               let t = map f xs
1169                               in h#:t
1170
1171 This may be undesirable since it forces evaluation here, but the value
1172 may not be used in all branches of the body. In the general case this
1173 transformation is impossible since the mutual recursion in a letrec
1174 cannot be expressed as a case.
1175
1176 There is also a problem with top-level unboxed values, since our
1177 implementation cannot handle unboxed values at the top level.
1178
1179 Solution: Lift the binding of the unboxed value and extract it when it
1180 is used:
1181
1182         map_*_Int# f (x:xs) = let h = case (f x) of h# -> _Lift h#
1183                                   t = map f xs
1184                               in case h of
1185                                  _Lift h# -> h#:t
1186
1187 Now give it to the simplifier and the _Lifting will be optimised away.
1188
1189 The benfit is that we have given the specialised "unboxed" values a
1190 very simplep lifted semantics and then leave it up to the simplifier to
1191 optimise it --- knowing that the overheads will be removed in nearly
1192 all cases.
1193
1194 In particular, the value will only be evaluted in the branches of the
1195 program which use it, rather than being forced at the point where the
1196 value is bound. For example:
1197
1198         filtermap_*_* p f (x:xs)
1199           = let h = f x
1200                 t = ...
1201             in case p x of
1202                 True  -> h:t
1203                 False -> t
1204    ==>
1205         filtermap_*_Int# p f (x:xs)
1206           = let h = case (f x) of h# -> _Lift h#
1207                 t = ...
1208             in case p x of
1209                 True  -> case h of _Lift h#
1210                            -> h#:t
1211                 False -> t
1212
1213 The binding for h can still be inlined in the one branch and the
1214 _Lifting eliminated.
1215
1216
1217 Question: When won't the _Lifting be eliminated?
1218
1219 Answer: When they at the top-level (where it is necessary) or when
1220 inlining would duplicate work (or possibly code depending on
1221 options). However, the _Lifting will still be eliminated if the
1222 strictness analyser deems the lifted binding strict.
1223