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