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