VectUtils.hs 15.7 KB
Newer Older
1
module VectUtils (
2
  collectAnnTypeBinders, collectAnnTypeArgs, isAnnTypeArg,
3
  collectAnnValBinders,
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
4
5
6
  dataConTagZ, mkDataConTag, mkDataConTagLit,

  newLocalVVar,
7

8
  mkBuiltinCo, voidType, mkWrapType,
9
  mkPADictType, mkPArrayType, mkPDataType, mkPReprType, mkPArray,
10

11
  pdataReprTyCon, pdataReprDataCon, mkVScrut,
12
  prDictOfType, prDFunOfTyCon,
13
  paDictArgType, paDictOfType, paDFunType,
14
  paMethod, wrapPR, replicatePD, emptyPD, packPD, packByTagPD,
15
16
  combinePD,
  liftPD,
17
  zipScalars, scalarClosure,
18
19
  polyAbstract, polyApply, polyVApply, polyArity,
  Inline(..), addInlineArity, inlineMe,
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
20
  hoistBinding, hoistExpr, hoistPolyVExpr, takeHoisted,
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
21
22
  buildClosure, buildClosures,
  mkClosureApp
23
24
) where

25
import VectCore
26
27
import VectMonad

28
import MkCore ( mkCoreTup, mkCoreTupTy, mkWildCase )
29
import CoreSyn
30
import CoreUtils
31
import CoreUnfold         ( mkInlineRule )
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
32
import Coercion
33
34
import Type
import TypeRep
35
import TyCon
36
import DataCon
37
import Var
38
import MkId               ( unwrapFamInstScrut )
39
import Id                 ( setIdUnfolding )
40
41
import TysWiredIn
import BasicTypes         ( Boxity(..) )
42
import Literal            ( Literal, mkMachInt )
43

44
import Outputable
45
import FastString
46

twanvl's avatar
twanvl committed
47
48
import Control.Monad

49
50
51
52
53
54
55
56
57
58
59
60
collectAnnTypeArgs :: AnnExpr b ann -> (AnnExpr b ann, [Type])
collectAnnTypeArgs expr = go expr []
  where
    go (_, AnnApp f (_, AnnType ty)) tys = go f (ty : tys)
    go e                             tys = (e, tys)

collectAnnTypeBinders :: AnnExpr Var ann -> ([Var], AnnExpr Var ann)
collectAnnTypeBinders expr = go [] expr
  where
    go bs (_, AnnLam b e) | isTyVar b = go (b:bs) e
    go bs e                           = (reverse bs, e)

61
62
63
collectAnnValBinders :: AnnExpr Var ann -> ([Var], AnnExpr Var ann)
collectAnnValBinders expr = go [] expr
  where
64
65
    go bs (_, AnnLam b e) | isId b = go (b:bs) e
    go bs e                        = (reverse bs, e)
66

67
isAnnTypeArg :: AnnExpr b ann -> Bool
twanvl's avatar
twanvl committed
68
isAnnTypeArg (_, AnnType _) = True
69
70
isAnnTypeArg _              = False

rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
71
72
73
dataConTagZ :: DataCon -> Int
dataConTagZ con = dataConTag con - fIRST_TAG

74
mkDataConTagLit :: DataCon -> Literal
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
75
mkDataConTagLit = mkMachInt . toInteger . dataConTagZ
76

77
mkDataConTag :: DataCon -> CoreExpr
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
78
mkDataConTag = mkIntLitInt . dataConTagZ
79

80
81
82
83
84
85
86
87
splitPrimTyCon :: Type -> Maybe TyCon
splitPrimTyCon ty
  | Just (tycon, []) <- splitTyConApp_maybe ty
  , isPrimTyCon tycon
  = Just tycon

  | otherwise = Nothing

rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
88
89
mkBuiltinTyConApp :: (Builtins -> TyCon) -> [Type] -> VM Type
mkBuiltinTyConApp get_tc tys
90
  = do
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
91
92
      tc <- builtin get_tc
      return $ mkTyConApp tc tys
93

rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
94
95
mkBuiltinTyConApps :: (Builtins -> TyCon) -> [Type] -> Type -> VM Type
mkBuiltinTyConApps get_tc tys ty
96
  = do
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
97
98
      tc <- builtin get_tc
      return $ foldr (mk tc) ty tys
99
  where
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
100
    mk tc ty1 ty2 = mkTyConApp tc [ty1,ty2]
101

102
voidType :: VM Type
103
voidType = mkBuiltinTyConApp VectMonad.voidTyCon []
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
104

105
106
107
mkWrapType :: Type -> VM Type
mkWrapType ty = mkBuiltinTyConApp wrapTyCon [ty]

rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
108
109
110
mkClosureTypes :: [Type] -> Type -> VM Type
mkClosureTypes = mkBuiltinTyConApps closureTyCon

111
112
113
mkPReprType :: Type -> VM Type
mkPReprType ty = mkBuiltinTyConApp preprTyCon [ty]

rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
114
115
mkPADictType :: Type -> VM Type
mkPADictType ty = mkBuiltinTyConApp paTyCon [ty]
116
117

mkPArrayType :: Type -> VM Type
118
119
120
mkPArrayType ty
  | Just tycon <- splitPrimTyCon ty
  = do
121
122
123
124
      r <- lookupPrimPArray tycon
      case r of
        Just arr -> return $ mkTyConApp arr []
        Nothing  -> cantVectorise "Primitive tycon not vectorised" (ppr tycon)
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
125
mkPArrayType ty = mkBuiltinTyConApp parrayTyCon [ty]
126

127
128
129
130
131
132
133
134
135
mkPDataType :: Type -> VM Type
mkPDataType ty = mkBuiltinTyConApp pdataTyCon [ty]

mkPArray :: Type -> CoreExpr -> CoreExpr -> VM CoreExpr
mkPArray ty len dat = do
                        tc <- builtin parrayTyCon
                        let [dc] = tyConDataCons tc
                        return $ mkConApp dc [Type ty, len, dat]

136
137
mkBuiltinCo :: (Builtins -> TyCon) -> VM Coercion
mkBuiltinCo get_tc
138
  = do
139
140
      tc <- builtin get_tc
      return $ mkTyConApp tc []
141

142
143
pdataReprTyCon :: Type -> VM (TyCon, [Type])
pdataReprTyCon ty = builtin pdataTyCon >>= (`lookupFamInst` [ty])
144

145
146
pdataReprDataCon :: Type -> VM (DataCon, [Type])
pdataReprDataCon ty
147
  = do
148
      (tc, arg_tys) <- pdataReprTyCon ty
149
150
151
      let [dc] = tyConDataCons tc
      return (dc, arg_tys)

152
mkVScrut :: VExpr -> VM (CoreExpr, CoreExpr, TyCon, [Type])
153
154
mkVScrut (ve, le)
  = do
155
156
157
158
      (tc, arg_tys) <- pdataReprTyCon ty
      return (ve, unwrapFamInstScrut tc arg_tys le, tc, arg_tys)
  where
    ty = exprType ve
159

160
161
prDFunOfTyCon :: TyCon -> VM CoreExpr
prDFunOfTyCon tycon
162
163
164
  = liftM Var
  . maybeCantVectoriseM "No PR dictionary for tycon" (ppr tycon)
  $ lookupTyConPR tycon
165

166
167
168
169
170
171
paDictArgType :: TyVar -> VM (Maybe Type)
paDictArgType tv = go (TyVarTy tv) (tyVarKind tv)
  where
    go ty k | Just k' <- kindView k = go ty k'
    go ty (FunTy k1 k2)
      = do
Ian Lynagh's avatar
Ian Lynagh committed
172
          tv   <- newTyVar (fsLit "a") k1
173
174
175
176
177
178
179
180
181
          mty1 <- go (TyVarTy tv) k1
          case mty1 of
            Just ty1 -> do
                          mty2 <- go (AppTy ty (TyVarTy tv)) k2
                          return $ fmap (ForAllTy tv . FunTy ty1) mty2
            Nothing  -> go ty k2

    go ty k
      | isLiftedTypeKind k
182
      = liftM Just (mkPADictType ty)
183

twanvl's avatar
twanvl committed
184
    go _ _ = return Nothing
185

186
187
188
189
190
191
192
193
194
195
196
197
198
199
paDictOfType :: Type -> VM CoreExpr
paDictOfType ty = paDictOfTyApp ty_fn ty_args
  where
    (ty_fn, ty_args) = splitAppTys ty

paDictOfTyApp :: Type -> [Type] -> VM CoreExpr
paDictOfTyApp ty_fn ty_args
  | Just ty_fn' <- coreView ty_fn = paDictOfTyApp ty_fn' ty_args
paDictOfTyApp (TyVarTy tv) ty_args
  = do
      dfun <- maybeV (lookupTyVarPA tv)
      paDFunApply dfun ty_args
paDictOfTyApp (TyConApp tc _) ty_args
  = do
200
201
      dfun <- maybeCantVectoriseM "No PA dictionary for tycon" (ppr tc)
            $ lookupTyConPA tc
202
      paDFunApply (Var dfun) ty_args
203
204
paDictOfTyApp ty _
  = cantVectorise "Can't construct PA dictionary for type" (ppr ty)
205

206
207
208
209
210
211
212
213
214
215
216
paDFunType :: TyCon -> VM Type
paDFunType tc
  = do
      margs <- mapM paDictArgType tvs
      res   <- mkPADictType (mkTyConApp tc arg_tys)
      return . mkForAllTys tvs
             $ mkFunTys [arg | Just arg <- margs] res
  where
    tvs = tyConTyVars tc
    arg_tys = mkTyVarTys tvs

217
218
219
220
221
222
paDFunApply :: CoreExpr -> [Type] -> VM CoreExpr
paDFunApply dfun tys
  = do
      dicts <- mapM paDictOfType tys
      return $ mkApps (mkTyApps dfun tys) dicts

223
224
paMethod :: (Builtins -> Var) -> String -> Type -> VM CoreExpr
paMethod _ name ty
225
  | Just tycon <- splitPrimTyCon ty
226
227
228
  = liftM Var
  . maybeCantVectoriseM "No PA method" (text name <+> text "for" <+> ppr tycon)
  $ lookupPrimMethod tycon name
229

230
paMethod method _ ty
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
231
232
233
234
235
  = do
      fn   <- builtin method
      dict <- paDictOfType ty
      return $ mkApps (Var fn) [Type ty, dict]

236
237
238
239
240
241
242
243
244
prDictOfType :: Type -> VM CoreExpr
prDictOfType ty = prDictOfTyApp ty_fn ty_args
  where
    (ty_fn, ty_args) = splitAppTys ty

prDictOfTyApp :: Type -> [Type] -> VM CoreExpr
prDictOfTyApp ty_fn ty_args
  | Just ty_fn' <- coreView ty_fn = prDictOfTyApp ty_fn' ty_args
prDictOfTyApp (TyConApp tc _) ty_args
245
  = do
246
      dfun <- liftM Var $ maybeV (lookupTyConPR tc)
247
      prDFunApply dfun ty_args
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
248
prDictOfTyApp _ _ = noV
249
250
251
252
253
254
255
256
257
258
259
260
261

prDFunApply :: CoreExpr -> [Type] -> VM CoreExpr
prDFunApply dfun tys
  = do
      dicts <- mapM prDictOfType tys
      return $ mkApps (mkTyApps dfun tys) dicts

wrapPR :: Type -> VM CoreExpr
wrapPR ty
  = do
      pa_dict <- paDictOfType ty
      pr_dfun <- prDFunOfTyCon =<< builtin wrapTyCon
      return $ mkApps pr_dfun [Type ty, pa_dict]
262

263
264
265
replicatePD :: CoreExpr -> CoreExpr -> VM CoreExpr
replicatePD len x = liftM (`mkApps` [len,x])
                          (paMethod replicatePDVar "replicatePD" (exprType x))
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
266

267
268
emptyPD :: Type -> VM CoreExpr
emptyPD = paMethod emptyPDVar "emptyPD"
269

270
271
272
packPD :: Type -> CoreExpr -> CoreExpr -> CoreExpr -> VM CoreExpr
packPD ty xs len sel = liftM (`mkApps` [xs, len, sel])
                             (paMethod packPDVar "packPD" ty)
273

274
275
276
277
278
279
packByTagPD :: Type -> CoreExpr -> CoreExpr -> CoreExpr -> CoreExpr
                 -> VM CoreExpr
packByTagPD ty xs len tags t
  = liftM (`mkApps` [xs, len, tags, t])
          (paMethod packByTagPDVar "packByTagPD" ty)

280
combinePD :: Type -> CoreExpr -> CoreExpr -> [CoreExpr]
281
          -> VM CoreExpr
282
283
284
combinePD ty len sel xs
  = liftM (`mkApps` (len : sel : xs))
          (paMethod (combinePDVar n) ("combine" ++ show n ++ "PD") ty)
285
286
287
  where
    n = length xs

288
289
liftPD :: CoreExpr -> VM CoreExpr
liftPD x
290
291
  = do
      lc <- builtin liftingContext
292
      replicatePD (Var lc) x
293

294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
zipScalars :: [Type] -> Type -> VM CoreExpr
zipScalars arg_tys res_ty
  = do
      scalar <- builtin scalarClass
      (dfuns, _) <- mapAndUnzipM (\ty -> lookupInst scalar [ty]) ty_args
      zipf <- builtin (scalarZip $ length arg_tys)
      return $ Var zipf `mkTyApps` ty_args `mkApps` map Var dfuns
    where
      ty_args = arg_tys ++ [res_ty]

scalarClosure :: [Type] -> Type -> CoreExpr -> CoreExpr -> VM CoreExpr
scalarClosure arg_tys res_ty scalar_fun array_fun
  = do
      ctr <- builtin (closureCtrFun $ length arg_tys)
      pas <- mapM paDictOfType (init arg_tys)
      return $ Var ctr `mkTyApps` (arg_tys ++ [res_ty])
                       `mkApps`   (pas ++ [scalar_fun, array_fun])

312
313
314
newLocalVVar :: FastString -> Type -> VM VVar
newLocalVVar fs vty
  = do
315
      lty <- mkPDataType vty
316
317
318
319
      vv  <- newLocalVar fs vty
      lv  <- newLocalVar fs lty
      return (vv,lv)

320
polyAbstract :: [TyVar] -> ([Var] -> VM a) -> VM a
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
321
322
323
polyAbstract tvs p
  = localV
  $ do
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
324
      mdicts <- mapM mk_dict_var tvs
325
326
327
      zipWithM_ (\tv -> maybe (defLocalTyVar tv)
                              (defLocalTyVarWithPA tv . Var)) tvs mdicts
      p (mk_args mdicts)
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
328
329
330
331
  where
    mk_dict_var tv = do
                       r <- paDictArgType tv
                       case r of
Ian Lynagh's avatar
Ian Lynagh committed
332
                         Just ty -> liftM Just (newLocalVar (fsLit "dPA") ty)
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
333
334
                         Nothing -> return Nothing

335
336
337
338
339
340
    mk_args mdicts = [dict | Just dict <- mdicts]

polyArity :: [TyVar] -> VM Int
polyArity tvs = do
                  tys <- mapM paDictArgType tvs
                  return $ length [() | Just _ <- tys]
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
341

rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
342
343
polyApply :: CoreExpr -> [Type] -> VM CoreExpr
polyApply expr tys
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
344
345
346
347
  = do
      dicts <- mapM paDictOfType tys
      return $ expr `mkTyApps` tys `mkApps` dicts

348
349
350
351
352
353
polyVApply :: VExpr -> [Type] -> VM VExpr
polyVApply expr tys
  = do
      dicts <- mapM paDictOfType tys
      return $ mapVect (\e -> e `mkTyApps` tys `mkApps` dicts) expr

354
355
356
357
358
359
360
361
362
363
364

data Inline = Inline Int -- arity
            | DontInline

addInlineArity :: Inline -> Int -> Inline
addInlineArity (Inline m) n = Inline (m+n)
addInlineArity DontInline _ = DontInline

inlineMe :: Inline
inlineMe = Inline 0

rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
365
366
367
368
hoistBinding :: Var -> CoreExpr -> VM ()
hoistBinding v e = updGEnv $ \env ->
  env { global_bindings = (v,e) : global_bindings env }

369
370
hoistExpr :: FastString -> CoreExpr -> Inline -> VM Var
hoistExpr fs expr inl
371
  = do
372
      var <- mk_inline `liftM` newLocalVar fs (exprType expr)
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
373
      hoistBinding var expr
374
      return var
375
376
377
378
379
  where
    mk_inline var = case inl of
                      Inline arity -> var `setIdUnfolding`
                                      mkInlineRule InlSat expr arity
                      DontInline   -> var
380

381
382
hoistVExpr :: VExpr -> Inline -> VM VVar
hoistVExpr (ve, le) inl
383
  = do
384
      fs <- getBindName
385
386
      vv <- hoistExpr ('v' `consFS` fs) ve inl
      lv <- hoistExpr ('l' `consFS` fs) le (addInlineArity inl 1)
387
      return (vv, lv)
388

389
390
hoistPolyVExpr :: [TyVar] -> Inline -> VM VExpr -> VM VExpr
hoistPolyVExpr tvs inline p
391
  = do
392
393
394
395
      inline' <- liftM (addInlineArity inline) (polyArity tvs)
      expr <- closedV . polyAbstract tvs $ \args ->
              liftM (mapVect (mkLams $ tvs ++ args)) p
      fn   <- hoistVExpr expr inline'
396
      polyVApply (vVar fn) (mkTyVarTys tvs)
397

398
399
400
401
402
403
404
takeHoisted :: VM [(Var, CoreExpr)]
takeHoisted
  = do
      env <- readGEnv id
      setGEnv $ env { global_bindings = [] }
      return $ global_bindings env

twanvl's avatar
twanvl committed
405
{-
406
407
408
409
410
411
412
413
414
415
416
boxExpr :: Type -> VExpr -> VM VExpr
boxExpr ty (vexpr, lexpr)
  | Just (tycon, []) <- splitTyConApp_maybe ty
  , isUnLiftedTyCon tycon
  = do
      r <- lookupBoxedTyCon tycon
      case r of
        Just tycon' -> let [dc] = tyConDataCons tycon'
                       in
                       return (mkConApp dc [vexpr], lexpr)
        Nothing     -> return (vexpr, lexpr)
twanvl's avatar
twanvl committed
417
-}
418

419
420
mkClosure :: Type -> Type -> Type -> VExpr -> VExpr -> VM VExpr
mkClosure arg_ty res_ty env_ty (vfn,lfn) (venv,lenv)
421
  = do
422
      dict <- paDictOfType env_ty
423
424
      mkv  <- builtin closureVar
      mkl  <- builtin liftedClosureVar
425
426
      return (Var mkv `mkTyApps` [arg_ty, res_ty, env_ty] `mkApps` [dict, vfn, lfn, venv],
              Var mkl `mkTyApps` [arg_ty, res_ty, env_ty] `mkApps` [dict, vfn, lfn, lenv])
427

428
429
mkClosureApp :: Type -> Type -> VExpr -> VExpr -> VM VExpr
mkClosureApp arg_ty res_ty (vclo, lclo) (varg, larg)
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
430
  = do
431
432
433
      vapply <- builtin applyVar
      lapply <- builtin liftedApplyVar
      lc     <- builtin liftingContext
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
434
      return (Var vapply `mkTyApps` [arg_ty, res_ty] `mkApps` [vclo, varg],
435
              Var lapply `mkTyApps` [arg_ty, res_ty] `mkApps` [Var lc, lclo, larg])
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
436

437
buildClosures :: [TyVar] -> [VVar] -> [Type] -> Type -> VM VExpr -> VM VExpr
twanvl's avatar
twanvl committed
438
buildClosures _   _    [] _ mk_body
439
  = mk_body
440
buildClosures tvs vars [arg_ty] res_ty mk_body
441
442
  = -- liftM vInlineMe $
      buildClosure tvs vars arg_ty res_ty mk_body
443
buildClosures tvs vars (arg_ty : arg_tys) res_ty mk_body
444
445
  = do
      res_ty' <- mkClosureTypes arg_tys res_ty
Ian Lynagh's avatar
Ian Lynagh committed
446
      arg <- newLocalVVar (fsLit "x") arg_ty
447
448
449
      -- liftM vInlineMe
      buildClosure tvs vars arg_ty res_ty'
        . hoistPolyVExpr tvs (Inline (length vars + 1))
450
        $ do
451
452
            lc <- builtin liftingContext
            clo <- buildClosures tvs (vars ++ [arg]) arg_tys res_ty mk_body
453
454
            return $ vLams lc (vars ++ [arg]) clo

455
456
457
458
-- (clo <x1,...,xn> <f,f^>, aclo (Arr lc xs1 ... xsn) <f,f^>)
--   where
--     f  = \env v -> case env of <x1,...,xn> -> e x1 ... xn v
--     f^ = \env v -> case env of Arr l xs1 ... xsn -> e^ l x1 ... xn v
459
--
460
461
buildClosure :: [TyVar] -> [VVar] -> Type -> Type -> VM VExpr -> VM VExpr
buildClosure tvs vars arg_ty res_ty mk_body
462
  = do
463
      (env_ty, env, bind) <- buildEnv vars
Ian Lynagh's avatar
Ian Lynagh committed
464
465
      env_bndr <- newLocalVVar (fsLit "env") env_ty
      arg_bndr <- newLocalVVar (fsLit "arg") arg_ty
466

467
      fn <- hoistPolyVExpr tvs (Inline 2)
468
          $ do
469
              lc    <- builtin liftingContext
470
              body  <- mk_body
471
              return -- . vInlineMe
472
473
                     . vLams lc [env_bndr, arg_bndr]
                     $ bind (vVar env_bndr)
474
                            (vVarApps lc body (vars ++ [arg_bndr]))
475

476
      mkClosure arg_ty res_ty env_ty fn env
477

478
479
480
481
482
483
484
485
486
487
488
buildEnv :: [VVar] -> VM (Type, VExpr, VExpr -> VExpr -> VExpr)
buildEnv [] = do
             ty    <- voidType
             void  <- builtin voidVar
             pvoid <- builtin pvoidVar
             return (ty, vVar (void, pvoid), \_ body -> body)

buildEnv [v] = return (vVarType v, vVar v,
                    \env body -> vLet (vNonRec v env) body)

buildEnv vs
489
  = do
490
491
      
      (lenv_tc, lenv_tyargs) <- pdataReprTyCon ty
492

493
494
495
496
497
498
499
500
      let venv_con   = tupleCon Boxed (length vs) 
          [lenv_con] = tyConDataCons lenv_tc

          venv       = mkCoreTup (map Var vvs)
          lenv       = Var (dataConWrapId lenv_con)
                       `mkTyApps` lenv_tyargs
                       `mkApps`   map Var lvs

rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
501
502
          vbind env body = mkWildCase env ty (exprType body)
                           [(DataAlt venv_con, vvs, body)]
503
504

          lbind env body =
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
505
            let scrut = unwrapFamInstScrut lenv_tc lenv_tyargs env
506
507
508
509
510
511
512
513
514
515
516
517
            in
            mkWildCase scrut (exprType scrut) (exprType body)
              [(DataAlt lenv_con, lvs, body)]

          bind (venv, lenv) (vbody, lbody) = (vbind venv vbody,
                                              lbind lenv lbody)

      return (ty, (venv, lenv), bind)
  where
    (vvs, lvs) = unzip vs
    tys        = map vVarType vs
    ty         = mkCoreTupTy tys
518