c7e88be9e5b397d752937a59d22fedf65f010158
[ghc-hetmet.git] / compiler / coreSyn / MkCore.lhs
1 \begin{code}
2 -- | Handy functions for creating much Core syntax
3 module MkCore (
4         -- * Constructing normal syntax
5         mkCoreLet, mkCoreLets,
6         mkCoreApp, mkCoreApps, mkCoreConApps,
7         mkCoreLams, mkWildCase, mkWildBinder, mkIfThenElse,
8         
9         -- * Constructing boxed literals
10         mkWordExpr, mkWordExprWord,
11         mkIntExpr, mkIntExprInt,
12         mkIntegerExpr,
13         mkFloatExpr, mkDoubleExpr,
14         mkCharExpr, mkStringExpr, mkStringExprFS,
15         
16         -- * Constructing general big tuples
17         -- $big_tuples
18         mkChunkified,
19         
20         -- * Constructing small tuples
21         mkCoreVarTup, mkCoreVarTupTy,
22         mkCoreTup, mkCoreTupTy,
23         
24         -- * Constructing big tuples
25         mkBigCoreVarTup, mkBigCoreVarTupTy,
26         mkBigCoreTup, mkBigCoreTupTy,
27         
28         -- * Deconstructing small tuples
29         mkSmallTupleSelector, mkSmallTupleCase,
30         
31         -- * Deconstructing big tuples
32         mkTupleSelector, mkTupleCase,
33         
34         -- * Constructing list expressions
35         mkNilExpr, mkConsExpr, mkListExpr, 
36         mkFoldrExpr, mkBuildExpr
37     ) where
38
39 #include "HsVersions.h"
40
41 import Id
42 import Var      ( setTyVarUnique )
43
44 import CoreSyn
45 import CoreUtils        ( exprType, needsCaseBinding, bindNonRec )
46 import Literal
47 import HscTypes
48
49 import TysWiredIn
50 import PrelNames
51
52 import Type
53 import TypeRep
54 import TysPrim          ( alphaTyVar )
55 import DataCon          ( DataCon, dataConWorkId )
56
57 import FastString
58 import UniqSupply
59 import Unique           ( mkBuiltinUnique )
60 import BasicTypes
61 import Util             ( notNull, zipEqual )
62 import Panic
63 import Constants
64
65 import Data.Char        ( ord )
66 import Data.Word
67
68 infixl 4 `mkCoreApp`, `mkCoreApps`
69 \end{code}
70
71 %************************************************************************
72 %*                                                                      *
73 \subsection{Basic CoreSyn construction}
74 %*                                                                      *
75 %************************************************************************
76
77 \begin{code}
78 -- | Bind a binding group over an expression, using a @let@ or @case@ as
79 -- appropriate (see "CoreSyn#let_app_invariant")
80 mkCoreLet :: CoreBind -> CoreExpr -> CoreExpr
81 mkCoreLet (NonRec bndr rhs) body        -- See Note [CoreSyn let/app invariant]
82   | needsCaseBinding (idType bndr) rhs
83   = Case rhs bndr (exprType body) [(DEFAULT,[],body)]
84 mkCoreLet bind body
85   = Let bind body
86
87 -- | Bind a list of binding groups over an expression. The leftmost binding
88 -- group becomes the outermost group in the resulting expression
89 mkCoreLets :: [CoreBind] -> CoreExpr -> CoreExpr
90 mkCoreLets binds body = foldr mkCoreLet body binds
91
92 -- | Construct an expression which represents the application of one expression
93 -- to the other
94 mkCoreApp :: CoreExpr -> CoreExpr -> CoreExpr
95 -- Check the invariant that the arg of an App is ok-for-speculation if unlifted
96 -- See CoreSyn Note [CoreSyn let/app invariant]
97 mkCoreApp fun (Type ty) = App fun (Type ty)
98 mkCoreApp fun arg       = mk_val_app fun arg arg_ty res_ty
99                       where
100                         (arg_ty, res_ty) = splitFunTy (exprType fun)
101
102 -- | Construct an expression which represents the application of a number of
103 -- expressions to another. The leftmost expression in the list is applied first
104 mkCoreApps :: CoreExpr -> [CoreExpr] -> CoreExpr
105 -- Slightly more efficient version of (foldl mkCoreApp)
106 mkCoreApps fun args
107   = go fun (exprType fun) args
108   where
109     go fun _      []               = fun
110     go fun fun_ty (Type ty : args) = go (App fun (Type ty)) (applyTy fun_ty ty) args
111     go fun fun_ty (arg     : args) = go (mk_val_app fun arg arg_ty res_ty) res_ty args
112                                    where
113                                      (arg_ty, res_ty) = splitFunTy fun_ty
114
115 -- | Construct an expression which represents the application of a number of
116 -- expressions to that of a data constructor expression. The leftmost expression
117 -- in the list is applied first
118 mkCoreConApps :: DataCon -> [CoreExpr] -> CoreExpr
119 mkCoreConApps con args = mkCoreApps (Var (dataConWorkId con)) args
120
121 -----------
122 mk_val_app :: CoreExpr -> CoreExpr -> Type -> Type -> CoreExpr
123 mk_val_app fun arg arg_ty _        -- See Note [CoreSyn let/app invariant]
124   | not (needsCaseBinding arg_ty arg)
125   = App fun arg                -- The vastly common case
126
127 mk_val_app fun arg arg_ty res_ty
128   = Case arg arg_id res_ty [(DEFAULT,[],App fun (Var arg_id))]
129   where
130     arg_id = mkWildBinder arg_ty    
131         -- Lots of shadowing, but it doesn't matter,
132         -- because 'fun ' should not have a free wild-id
133         --
134         -- This is Dangerous.  But this is the only place we play this 
135         -- game, mk_val_app returns an expression that does not have
136         -- have a free wild-id.  So the only thing that can go wrong
137         -- is if you take apart this case expression, and pass a 
138         -- fragmet of it as the fun part of a 'mk_val_app'.
139
140
141 -- | Make a /wildcard binder/. This is typically used when you need a binder 
142 -- that you expect to use only at a *binding* site.  Do not use it at
143 -- occurrence sites because it has a single, fixed unique, and it's very
144 -- easy to get into difficulties with shadowing.  That's why it is used so little.
145 mkWildBinder :: Type -> Id
146 mkWildBinder ty = mkSysLocal (fsLit "wild") (mkBuiltinUnique 1) ty
147
148 mkWildCase :: CoreExpr -> Type -> Type -> [CoreAlt] -> CoreExpr
149 -- Make a case expression whose case binder is unused
150 -- The alts should not have any occurrences of WildId
151 mkWildCase scrut scrut_ty res_ty alts 
152   = Case scrut (mkWildBinder scrut_ty) res_ty alts
153
154 mkIfThenElse :: CoreExpr -> CoreExpr -> CoreExpr -> CoreExpr
155 mkIfThenElse guard then_expr else_expr
156 -- Not going to be refining, so okay to take the type of the "then" clause
157   = mkWildCase guard boolTy (exprType then_expr) 
158          [ (DataAlt falseDataCon, [], else_expr),       -- Increasing order of tag!
159            (DataAlt trueDataCon,  [], then_expr) ]
160 \end{code}
161
162 The functions from this point don't really do anything cleverer than
163 their counterparts in CoreSyn, but they are here for consistency
164
165 \begin{code}
166 -- | Create a lambda where the given expression has a number of variables
167 -- bound over it. The leftmost binder is that bound by the outermost
168 -- lambda in the result
169 mkCoreLams :: [CoreBndr] -> CoreExpr -> CoreExpr
170 mkCoreLams = mkLams
171 \end{code}
172
173 %************************************************************************
174 %*                                                                      *
175 \subsection{Making literals}
176 %*                                                                      *
177 %************************************************************************
178
179 \begin{code}
180 -- | Create a 'CoreExpr' which will evaluate to the given @Int@
181 mkIntExpr      :: Integer    -> CoreExpr            -- Result = I# i :: Int
182 mkIntExpr  i = mkConApp intDataCon  [mkIntLit i]
183
184 -- | Create a 'CoreExpr' which will evaluate to the given @Int@
185 mkIntExprInt   :: Int        -> CoreExpr            -- Result = I# i :: Int
186 mkIntExprInt  i = mkConApp intDataCon  [mkIntLitInt i]
187
188 -- | Create a 'CoreExpr' which will evaluate to the a @Word@ with the given value
189 mkWordExpr     :: Integer    -> CoreExpr
190 mkWordExpr w = mkConApp wordDataCon [mkWordLit w]
191
192 -- | Create a 'CoreExpr' which will evaluate to the given @Word@
193 mkWordExprWord :: Word       -> CoreExpr
194 mkWordExprWord w = mkConApp wordDataCon [mkWordLitWord w]
195
196 -- | Create a 'CoreExpr' which will evaluate to the given @Integer@
197 mkIntegerExpr  :: MonadThings m => Integer    -> m CoreExpr  -- Result :: Integer
198 mkIntegerExpr i
199   | inIntRange i        -- Small enough, so start from an Int
200     = do integer_id <- lookupId smallIntegerName
201          return (mkSmallIntegerLit integer_id i)
202
203 -- Special case for integral literals with a large magnitude:
204 -- They are transformed into an expression involving only smaller
205 -- integral literals. This improves constant folding.
206
207   | otherwise = do       -- Big, so start from a string
208       plus_id <- lookupId plusIntegerName
209       times_id <- lookupId timesIntegerName
210       integer_id <- lookupId smallIntegerName
211       let
212            lit i = mkSmallIntegerLit integer_id i
213            plus a b  = Var plus_id  `App` a `App` b
214            times a b = Var times_id `App` a `App` b
215
216            -- Transform i into (x1 + (x2 + (x3 + (...) * b) * b) * b) with abs xi <= b
217            horner :: Integer -> Integer -> CoreExpr
218            horner b i | abs q <= 1 = if r == 0 || r == i 
219                                      then lit i 
220                                      else lit r `plus` lit (i-r)
221                       | r == 0     =               horner b q `times` lit b
222                       | otherwise  = lit r `plus` (horner b q `times` lit b)
223                       where
224                         (q,r) = i `quotRem` b
225
226       return (horner tARGET_MAX_INT i)
227   where
228     mkSmallIntegerLit :: Id -> Integer -> CoreExpr
229     mkSmallIntegerLit small_integer i = mkApps (Var small_integer) [mkIntLit i]
230
231
232 -- | Create a 'CoreExpr' which will evaluate to the given @Float@
233 mkFloatExpr :: Float -> CoreExpr
234 mkFloatExpr f = mkConApp floatDataCon [mkFloatLitFloat f]
235
236 -- | Create a 'CoreExpr' which will evaluate to the given @Double@
237 mkDoubleExpr :: Double -> CoreExpr
238 mkDoubleExpr d = mkConApp doubleDataCon [mkDoubleLitDouble d]
239
240
241 -- | Create a 'CoreExpr' which will evaluate to the given @Char@
242 mkCharExpr     :: Char             -> CoreExpr      -- Result = C# c :: Int
243 mkCharExpr c = mkConApp charDataCon [mkCharLit c]
244
245 -- | Create a 'CoreExpr' which will evaluate to the given @String@
246 mkStringExpr   :: MonadThings m => String     -> m CoreExpr  -- Result :: String
247 -- | Create a 'CoreExpr' which will evaluate to a string morally equivalent to the given @FastString@
248 mkStringExprFS :: MonadThings m => FastString -> m CoreExpr  -- Result :: String
249
250 mkStringExpr str = mkStringExprFS (mkFastString str)
251
252 mkStringExprFS str
253   | nullFS str
254   = return (mkNilExpr charTy)
255
256   | lengthFS str == 1
257   = do let the_char = mkCharExpr (headFS str)
258        return (mkConsExpr charTy the_char (mkNilExpr charTy))
259
260   | all safeChar chars
261   = do unpack_id <- lookupId unpackCStringName
262        return (App (Var unpack_id) (Lit (MachStr str)))
263
264   | otherwise
265   = do unpack_id <- lookupId unpackCStringUtf8Name
266        return (App (Var unpack_id) (Lit (MachStr str)))
267
268   where
269     chars = unpackFS str
270     safeChar c = ord c >= 1 && ord c <= 0x7F
271 \end{code}
272
273 %************************************************************************
274 %*                                                                      *
275 \subsection{Tuple constructors}
276 %*                                                                      *
277 %************************************************************************
278
279 \begin{code}
280
281 -- $big_tuples
282 -- #big_tuples#
283 --
284 -- GHCs built in tuples can only go up to 'mAX_TUPLE_SIZE' in arity, but
285 -- we might concievably want to build such a massive tuple as part of the
286 -- output of a desugaring stage (notably that for list comprehensions).
287 --
288 -- We call tuples above this size \"big tuples\", and emulate them by
289 -- creating and pattern matching on >nested< tuples that are expressible
290 -- by GHC.
291 --
292 -- Nesting policy: it's better to have a 2-tuple of 10-tuples (3 objects)
293 -- than a 10-tuple of 2-tuples (11 objects), so we want the leaves of any
294 -- construction to be big.
295 --
296 -- If you just use the 'mkBigCoreTup', 'mkBigCoreVarTupTy', 'mkTupleSelector'
297 -- and 'mkTupleCase' functions to do all your work with tuples you should be
298 -- fine, and not have to worry about the arity limitation at all.
299
300 -- | Lifts a \"small\" constructor into a \"big\" constructor by recursive decompositon
301 mkChunkified :: ([a] -> a)      -- ^ \"Small\" constructor function, of maximum input arity 'mAX_TUPLE_SIZE'
302              -> [a]             -- ^ Possible \"big\" list of things to construct from
303              -> a               -- ^ Constructed thing made possible by recursive decomposition
304 mkChunkified small_tuple as = mk_big_tuple (chunkify as)
305   where
306         -- Each sub-list is short enough to fit in a tuple
307     mk_big_tuple [as] = small_tuple as
308     mk_big_tuple as_s = mk_big_tuple (chunkify (map small_tuple as_s))
309
310 chunkify :: [a] -> [[a]]
311 -- ^ Split a list into lists that are small enough to have a corresponding
312 -- tuple arity. The sub-lists of the result all have length <= 'mAX_TUPLE_SIZE'
313 -- But there may be more than 'mAX_TUPLE_SIZE' sub-lists
314 chunkify xs
315   | n_xs <= mAX_TUPLE_SIZE = [xs] 
316   | otherwise              = split xs
317   where
318     n_xs     = length xs
319     split [] = []
320     split xs = take mAX_TUPLE_SIZE xs : split (drop mAX_TUPLE_SIZE xs)
321     
322 \end{code}
323
324 Creating tuples and their types for Core expressions 
325
326 @mkBigCoreVarTup@ builds a tuple; the inverse to @mkTupleSelector@.  
327
328 * If it has only one element, it is the identity function.
329
330 * If there are more elements than a big tuple can have, it nests 
331   the tuples.  
332
333 \begin{code}
334
335 -- | Build a small tuple holding the specified variables
336 mkCoreVarTup :: [Id] -> CoreExpr
337 mkCoreVarTup ids = mkCoreTup (map Var ids)
338
339 -- | Bulid the type of a small tuple that holds the specified variables
340 mkCoreVarTupTy :: [Id] -> Type
341 mkCoreVarTupTy ids = mkCoreTupTy (map idType ids)
342
343 -- | Build a small tuple holding the specified expressions
344 mkCoreTup :: [CoreExpr] -> CoreExpr
345 mkCoreTup []  = Var unitDataConId
346 mkCoreTup [c] = c
347 mkCoreTup cs  = mkConApp (tupleCon Boxed (length cs))
348                          (map (Type . exprType) cs ++ cs)
349
350 -- | Build the type of a small tuple that holds the specified type of thing
351 mkCoreTupTy :: [Type] -> Type
352 mkCoreTupTy [ty] = ty
353 mkCoreTupTy tys  = mkTupleTy Boxed (length tys) tys
354
355
356 -- | Build a big tuple holding the specified variables
357 mkBigCoreVarTup :: [Id] -> CoreExpr
358 mkBigCoreVarTup ids = mkBigCoreTup (map Var ids)
359
360 -- | Build the type of a big tuple that holds the specified variables
361 mkBigCoreVarTupTy :: [Id] -> Type
362 mkBigCoreVarTupTy ids = mkBigCoreTupTy (map idType ids)
363
364 -- | Build a big tuple holding the specified expressions
365 mkBigCoreTup :: [CoreExpr] -> CoreExpr
366 mkBigCoreTup = mkChunkified mkCoreTup
367
368 -- | Build the type of a big tuple that holds the specified type of thing
369 mkBigCoreTupTy :: [Type] -> Type
370 mkBigCoreTupTy = mkChunkified mkCoreTupTy
371 \end{code}
372
373 %************************************************************************
374 %*                                                                      *
375 \subsection{Tuple destructors}
376 %*                                                                      *
377 %************************************************************************
378
379 \begin{code}
380 -- | Builds a selector which scrutises the given
381 -- expression and extracts the one name from the list given.
382 -- If you want the no-shadowing rule to apply, the caller
383 -- is responsible for making sure that none of these names
384 -- are in scope.
385 --
386 -- If there is just one 'Id' in the tuple, then the selector is
387 -- just the identity.
388 --
389 -- If necessary, we pattern match on a \"big\" tuple.
390 mkTupleSelector :: [Id]         -- ^ The 'Id's to pattern match the tuple against
391                 -> Id           -- ^ The 'Id' to select
392                 -> Id           -- ^ A variable of the same type as the scrutinee
393                 -> CoreExpr     -- ^ Scrutinee
394                 -> CoreExpr     -- ^ Selector expression
395
396 -- mkTupleSelector [a,b,c,d] b v e
397 --          = case e of v { 
398 --                (p,q) -> case p of p {
399 --                           (a,b) -> b }}
400 -- We use 'tpl' vars for the p,q, since shadowing does not matter.
401 --
402 -- In fact, it's more convenient to generate it innermost first, getting
403 --
404 --        case (case e of v 
405 --                (p,q) -> p) of p
406 --          (a,b) -> b
407 mkTupleSelector vars the_var scrut_var scrut
408   = mk_tup_sel (chunkify vars) the_var
409   where
410     mk_tup_sel [vars] the_var = mkSmallTupleSelector vars the_var scrut_var scrut
411     mk_tup_sel vars_s the_var = mkSmallTupleSelector group the_var tpl_v $
412                                 mk_tup_sel (chunkify tpl_vs) tpl_v
413         where
414           tpl_tys = [mkCoreTupTy (map idType gp) | gp <- vars_s]
415           tpl_vs  = mkTemplateLocals tpl_tys
416           [(tpl_v, group)] = [(tpl,gp) | (tpl,gp) <- zipEqual "mkTupleSelector" tpl_vs vars_s,
417                                          the_var `elem` gp ]
418 \end{code}
419
420 \begin{code}
421 -- | Like 'mkTupleSelector' but for tuples that are guaranteed
422 -- never to be \"big\".
423 --
424 -- > mkSmallTupleSelector [x] x v e = [| e |]
425 -- > mkSmallTupleSelector [x,y,z] x v e = [| case e of v { (x,y,z) -> x } |]
426 mkSmallTupleSelector :: [Id]        -- The tuple args
427           -> Id         -- The selected one
428           -> Id         -- A variable of the same type as the scrutinee
429           -> CoreExpr        -- Scrutinee
430           -> CoreExpr
431 mkSmallTupleSelector [var] should_be_the_same_var _ scrut
432   = ASSERT(var == should_be_the_same_var)
433     scrut
434 mkSmallTupleSelector vars the_var scrut_var scrut
435   = ASSERT( notNull vars )
436     Case scrut scrut_var (idType the_var)
437          [(DataAlt (tupleCon Boxed (length vars)), vars, Var the_var)]
438 \end{code}
439
440 \begin{code}
441 -- | A generalization of 'mkTupleSelector', allowing the body
442 -- of the case to be an arbitrary expression.
443 --
444 -- To avoid shadowing, we use uniques to invent new variables.
445 --
446 -- If necessary we pattern match on a \"big\" tuple.
447 mkTupleCase :: UniqSupply       -- ^ For inventing names of intermediate variables
448             -> [Id]             -- ^ The tuple identifiers to pattern match on
449             -> CoreExpr         -- ^ Body of the case
450             -> Id               -- ^ A variable of the same type as the scrutinee
451             -> CoreExpr         -- ^ Scrutinee
452             -> CoreExpr
453 -- ToDo: eliminate cases where none of the variables are needed.
454 --
455 --         mkTupleCase uniqs [a,b,c,d] body v e
456 --           = case e of v { (p,q) ->
457 --             case p of p { (a,b) ->
458 --             case q of q { (c,d) ->
459 --             body }}}
460 mkTupleCase uniqs vars body scrut_var scrut
461   = mk_tuple_case uniqs (chunkify vars) body
462   where
463     -- This is the case where don't need any nesting
464     mk_tuple_case _ [vars] body
465       = mkSmallTupleCase vars body scrut_var scrut
466       
467     -- This is the case where we must make nest tuples at least once
468     mk_tuple_case us vars_s body
469       = let (us', vars', body') = foldr one_tuple_case (us, [], body) vars_s
470             in mk_tuple_case us' (chunkify vars') body'
471     
472     one_tuple_case chunk_vars (us, vs, body)
473       = let (us1, us2) = splitUniqSupply us
474             scrut_var = mkSysLocal (fsLit "ds") (uniqFromSupply us1)
475               (mkCoreTupTy (map idType chunk_vars))
476             body' = mkSmallTupleCase chunk_vars body scrut_var (Var scrut_var)
477         in (us2, scrut_var:vs, body')
478 \end{code}
479
480 \begin{code}
481 -- | As 'mkTupleCase', but for a tuple that is small enough to be guaranteed
482 -- not to need nesting.
483 mkSmallTupleCase
484         :: [Id]         -- ^ The tuple args
485         -> CoreExpr     -- ^ Body of the case
486         -> Id           -- ^ A variable of the same type as the scrutinee
487         -> CoreExpr     -- ^ Scrutinee
488         -> CoreExpr
489
490 mkSmallTupleCase [var] body _scrut_var scrut
491   = bindNonRec var scrut body
492 mkSmallTupleCase vars body scrut_var scrut
493 -- One branch no refinement?
494   = Case scrut scrut_var (exprType body) [(DataAlt (tupleCon Boxed (length vars)), vars, body)]
495 \end{code}
496
497 %************************************************************************
498 %*                                                                      *
499 \subsection{Common list manipulation expressions}
500 %*                                                                      *
501 %************************************************************************
502
503 Call the constructor Ids when building explicit lists, so that they
504 interact well with rules.
505
506 \begin{code}
507 -- | Makes a list @[]@ for lists of the specified type
508 mkNilExpr :: Type -> CoreExpr
509 mkNilExpr ty = mkConApp nilDataCon [Type ty]
510
511 -- | Makes a list @(:)@ for lists of the specified type
512 mkConsExpr :: Type -> CoreExpr -> CoreExpr -> CoreExpr
513 mkConsExpr ty hd tl = mkConApp consDataCon [Type ty, hd, tl]
514
515 -- | Make a list containing the given expressions, where the list has the given type
516 mkListExpr :: Type -> [CoreExpr] -> CoreExpr
517 mkListExpr ty xs = foldr (mkConsExpr ty) (mkNilExpr ty) xs
518
519 -- | Make a fully applied 'foldr' expression
520 mkFoldrExpr :: MonadThings m
521             => Type             -- ^ Element type of the list
522             -> Type             -- ^ Fold result type
523             -> CoreExpr         -- ^ "Cons" function expression for the fold
524             -> CoreExpr         -- ^ "Nil" expression for the fold
525             -> CoreExpr         -- ^ List expression being folded acress
526             -> m CoreExpr
527 mkFoldrExpr elt_ty result_ty c n list = do
528     foldr_id <- lookupId foldrName
529     return (Var foldr_id `App` Type elt_ty 
530            `App` Type result_ty
531            `App` c
532            `App` n
533            `App` list)
534
535 -- | Make a 'build' expression applied to a locally-bound worker function
536 mkBuildExpr :: (MonadThings m, MonadUnique m)
537             => Type                                     -- ^ Type of list elements to be built
538             -> ((Id, Type) -> (Id, Type) -> m CoreExpr) -- ^ Function that, given information about the 'Id's
539                                                         -- of the binders for the build worker function, returns
540                                                         -- the body of that worker
541             -> m CoreExpr
542 mkBuildExpr elt_ty mk_build_inside = do
543     [n_tyvar] <- newTyVars [alphaTyVar]
544     let n_ty = mkTyVarTy n_tyvar
545         c_ty = mkFunTys [elt_ty, n_ty] n_ty
546     [c, n] <- sequence [mkSysLocalM (fsLit "c") c_ty, mkSysLocalM (fsLit "n") n_ty]
547     
548     build_inside <- mk_build_inside (c, c_ty) (n, n_ty)
549     
550     build_id <- lookupId buildName
551     return $ Var build_id `App` Type elt_ty `App` mkLams [n_tyvar, c, n] build_inside
552   where
553     newTyVars tyvar_tmpls = do
554       uniqs <- getUniquesM
555       return (zipWith setTyVarUnique tyvar_tmpls uniqs)
556 \end{code}