b868c2a8ebd672b74f64d367987f39b05ede6cd4
[ghc-hetmet.git] / ghc / compiler / types / Generics.lhs
1 \begin{code}
2 module Generics ( mkTyConGenInfo, mkGenericRhs, 
3                   validGenericInstanceType, validGenericMethodType
4     ) where
5
6
7 import RnHsSyn          ( RenamedHsExpr )
8 import HsSyn            ( HsExpr(..), InPat(..), mkSimpleMatch, placeHolderType )
9
10 import Type             ( Type, isUnLiftedType, tyVarsOfType, tyVarsOfTypes,
11                           mkTyVarTys, mkForAllTys, mkTyConApp, 
12                           mkFunTy, isTyVarTy, getTyVar_maybe,
13                           funTyCon
14                         )
15 import TcType           ( tcSplitTyConApp_maybe, tcSplitSigmaTy, tcSplitSigmaTy )
16 import DataCon          ( DataCon, dataConOrigArgTys, dataConWrapId, isExistentialDataCon )
17
18 import TyCon            ( TyCon, tyConTyVars, tyConDataCons_maybe, 
19                           tyConGenInfo, isNewTyCon, newTyConRep, isBoxedTupleTyCon
20                         )
21 import Name             ( Name, mkSystemName )
22 import CoreSyn          ( mkLams, Expr(..), CoreExpr, AltCon(..), 
23                           mkConApp, Alt, mkTyApps, mkVarApps )
24 import CoreUtils        ( exprArity )
25 import BasicTypes       ( EP(..), Boxity(..) )
26 import Var              ( TyVar )
27 import VarSet           ( varSetElems )
28 import Id               ( Id, mkVanillaGlobal, idType, idName, mkSysLocal )
29 import MkId             ( mkReboxingAlt, mkNewTypeBody )
30 import TysWiredIn       ( genericTyCons,
31                           genUnitTyCon, genUnitDataCon, plusTyCon, inrDataCon,
32                           inlDataCon, crossTyCon, crossDataCon
33                         )
34 import IdInfo           ( noCafNoTyGenIdInfo, setUnfoldingInfo, setArityInfo )
35 import CoreUnfold       ( mkTopUnfolding ) 
36
37 import Maybe            ( isNothing )
38 import SrcLoc           ( builtinSrcLoc )
39 import Unique           ( Unique, builtinUniques, mkBuiltinUnique )
40 import Util             ( takeList, dropList )
41 import Outputable 
42 import FastString
43
44 #include "HsVersions.h"
45 \end{code}
46
47 Roadmap of what's where in the Generics work.
48 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
49
50 Parser
51 No real checks.
52
53 RnSource.rnHsType
54   Checks that HsNumTy has a "1" in it.
55
56 TcInstDcls.mkGenericInstance:
57   Checks for invalid type patterns, such as f {| Int |}
58
59 TcClassDcl.tcClassSig
60   Checks for a method type that is too complicated;
61         e.g. has for-alls or lists in it
62   We could lift this restriction
63
64 TcClassDecl.mkDefMethRhs
65   Checks that the instance type is simple, in an instance decl 
66   where we let the compiler fill in a generic method.
67         e.g.  instance C (T Int)
68         is not valid if C has generic methods.
69
70 TcClassDecl.checkGenericClassIsUnary
71   Checks that we don't have generic methods in a multi-parameter class
72
73 TcClassDecl.checkDefaultBinds
74   Checks that all the equations for a method in a class decl
75   are generic, or all are non-generic
76
77
78                         
79 Checking that the type constructors which are present in Generic
80 patterns (not Unit, this is done differently) is done in mk_inst_info
81 (TcInstDecls) in a call to tcHsType (TcMonoBinds). This means that
82 HsOpTy is tied to Generic definitions which is not a very good design
83 feature, indeed a bug. However, the check is easy to move from
84 tcHsType back to mk_inst_info and everything will be fine. Also see
85 bug #5.
86
87 Generics.lhs
88
89 Making generic information to put into a tycon. Constructs the
90 representation type, which, I think, are not used later. Perhaps it is
91 worth removing them from the GI datatype. Although it does get used in
92 the construction of conversion functions (internally).
93
94 TyCon.lhs
95
96 Just stores generic information, accessible by tyConGenInfo or tyConGenIds.
97
98 TysWiredIn.lhs
99
100 Defines generic and other type and data constructors.
101
102 This is sadly incomplete, but will be added to.
103
104
105 Bugs & shortcomings of existing implementation:
106 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
107
108 2. Another pretty big bug I dscovered at the last minute when I was
109 testing the code is that at the moment the type variable of the class
110 is scoped over the entire declaration, including the patterns. For
111 instance, if I have the following code,
112
113 class Er a where
114  ...
115   er {| Plus a b |} (Inl x) (Inl y) = er x y 
116   er {| Plus a b |} (Inr x) (Inr y) = er x y 
117   er {| Plus a b |} _ _ = False
118  
119 and I print out the types of the generic patterns, I get the
120 following.  Note that all the variable names for "a" are the same,
121 while for "b" they are all different.
122
123 check_ty
124     [std.Generics.Plus{-33u,i-} a{-r6Z-} b{-r7g-},
125      std.Generics.Plus{-33u,i-} a{-r6Z-} b{-r7m-},
126      std.Generics.Plus{-33u,i-} a{-r6Z-} b{-r7p-}]
127
128 This is a bug as if I change the code to
129
130  er {| Plus c b |} (Inl x)  (Inl y) = er x y 
131
132 all the names come out to be different.
133
134 Thus, all the types (Plus a b) come out to be different, so I cannot
135 compare them and test whether they are all the same and thus cannot
136 return an error if the type variables are different.
137
138 Temporary fix/hack. I am not checking for this, I just assume they are
139 the same, see line "check_ty = True" in TcInstDecls. When we resolve
140 the issue with variables, though - I assume that we will make them to
141 be the same in all the type patterns, jus uncomment the check and
142 everything should work smoothly.
143
144 Hence, I have also left the rather silly construction of:
145 * extracting all the type variables from all the types
146 * putting them *all* into the environment
147 * typechecking all the types
148 * selecting one of them and using it as the instance_ty.
149
150 (the alternative is to make sure that all the types are the same,
151 taking one, extracting its variables, putting them into the environment,
152 type checking it, using it as the instance_ty)
153  
154 6. What happens if we do not supply all of the generic patterns? At
155 the moment, the compiler crashes with an error message "Non-exhaustive
156 patterns in a generic declaration" 
157
158
159 What has not been addressed:
160 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
161
162 Contexts. In the generated instance declarations for the 3 primitive
163 type constructors, we need contexts. It is unclear what those should
164 be. At the moment, we always say eg. (Eq a, Eq b) => Eq (Plus a b)
165
166 Type application. We have type application in expressions
167 (essentially) on the lhs of an equation. Do we want to allow it on the
168 RHS?
169
170 Scoping of type variables in a generic definition. At the moment, (see
171 TcInstDecls) we extract the type variables inside the type patterns
172 and add them to the environment. See my bug #2 above. This seems pretty
173 important.
174
175
176
177 %************************************************************************
178 %*                                                                      *
179 \subsection{Getting the representation type out}
180 %*                                                                      *
181 %************************************************************************
182
183 \begin{code}
184 validGenericInstanceType :: Type -> Bool
185   -- Checks for validity of the type pattern in a generic
186   -- declaration.  It's ok to have  
187   --    f {| a + b |} ...
188   -- but it's not OK to have
189   --    f {| a + Int |}
190
191 validGenericInstanceType inst_ty
192   = case tcSplitTyConApp_maybe inst_ty of
193         Just (tycon, tys) ->  all isTyVarTy tys && tycon `elem` genericTyCons
194         Nothing           ->  False
195
196 validGenericMethodType :: Type -> Bool
197   -- At the moment we only allow method types built from
198   --    * type variables
199   --    * function arrow
200   --    * boxed tuples
201   --    * an arbitrary type not involving the class type variables
202   --            e.g. this is ok:        forall b. Ord b => [b] -> a
203   --                 where a is the class variable
204 validGenericMethodType ty 
205   = valid tau
206   where
207     (local_tvs, _, tau) = tcSplitSigmaTy ty
208
209     valid ty
210       | isTyVarTy ty    = True
211       | no_tyvars_in_ty = True
212       | otherwise       = case tcSplitTyConApp_maybe ty of
213                                 Just (tc,tys) -> valid_tycon tc && all valid tys
214                                 Nothing       -> False
215       where
216         no_tyvars_in_ty = all (`elem` local_tvs) (varSetElems (tyVarsOfType ty))
217
218     valid_tycon tc = tc == funTyCon || isBoxedTupleTyCon tc 
219         -- Compare bimapApp, below
220 \end{code}
221
222
223 %************************************************************************
224 %*                                                                      *
225 \subsection{Generating representation types}
226 %*                                                                      *
227 %************************************************************************
228
229 \begin{code}
230 mkTyConGenInfo :: TyCon -> [Name] -> Maybe (EP Id)
231 -- mkTyConGenInfo is called twice
232 --      once from TysWiredIn for Tuples
233 --      once the typechecker TcTyDecls 
234 -- to generate generic types and conversion functions for all datatypes.
235 -- 
236 -- Must only be called with an algebraic type.
237 -- 
238 -- The two names are the names constructed by the renamer
239 -- for the fromT and toT conversion functions.
240
241 mkTyConGenInfo tycon []
242   = Nothing     -- This happens when we deal with the interface-file type
243                 -- decl for a module compiled without -fgenerics
244
245 mkTyConGenInfo tycon [from_name, to_name]
246   | isNothing maybe_datacons    -- Abstractly imported types don't have
247   = Nothing                     -- to/from operations, (and should not need them)
248
249         -- If any of the constructor has an unboxed type as argument,
250         -- then we can't build the embedding-projection pair, because
251         -- it relies on instantiating *polymorphic* sum and product types
252         -- at the argument types of the constructors
253         -- Nor can we do the job if it's an existential data constructor,
254   | or [ any isUnLiftedType (dataConOrigArgTys dc) || isExistentialDataCon dc
255        | dc <- datacons ]
256   = Nothing
257
258   | null datacons       -- There are no constructors; 
259   = Nothing             -- there are no values of this type
260
261   | otherwise
262   = ASSERT( not (null datacons) )       -- mk_sum_stuff loops if no datacons
263     Just (EP { fromEP = mkVanillaGlobal from_name from_ty from_id_info,
264                toEP   = mkVanillaGlobal to_name   to_ty   to_id_info })
265   where
266     maybe_datacons = tyConDataCons_maybe tycon
267     Just datacons  = maybe_datacons             -- [C, D]
268
269     tyvars         = tyConTyVars tycon          -- [a, b, c]
270     tycon_ty       = mkTyConApp tycon tyvar_tys -- T a b c
271     tyvar_tys      = mkTyVarTys tyvars
272
273     from_id_info = noCafNoTyGenIdInfo `setUnfoldingInfo` mkTopUnfolding from_fn
274                                       `setArityInfo`     exprArity from_fn
275     to_id_info   = noCafNoTyGenIdInfo `setUnfoldingInfo` mkTopUnfolding to_fn
276                                       `setArityInfo`     exprArity to_fn
277         -- It's important to set the arity info, so that
278         -- the calling convention (gotten from arity) 
279         -- matches reality.
280
281     from_ty = mkForAllTys tyvars (mkFunTy tycon_ty rep_ty)
282     to_ty   = mkForAllTys tyvars (mkFunTy rep_ty tycon_ty)
283
284     (from_fn, to_fn, rep_ty) 
285         | isNewTyCon tycon
286         = ( mkLams tyvars $ Lam x  $ mkNewTypeBody tycon newrep_ty (Var x),
287             Var (dataConWrapId the_datacon),
288             newrep_ty )
289
290         | otherwise
291         = ( mkLams tyvars $ Lam x     $ Case (Var x) x from_alts,
292             mkLams tyvars $ Lam rep_var to_inner,
293             idType rep_var )
294
295     -- x :: T a b c
296     x = mkGenericLocal u1 tycon_ty
297     (u1 : uniqs) = builtinUniques
298
299             ----------------------
300             --  Newtypes only
301     [the_datacon]  = datacons
302     (_, newrep_ty) = newTyConRep tycon
303        
304             ----------------------
305             --  Non-newtypes only
306     -- Recurse over the sum first
307     -- The "2" is the first free unique
308     (from_alts, to_inner, rep_var) = mk_sum_stuff uniqs tyvars datacons
309     
310 mkTyConGenInfo tycon names = pprPanic "mkTyConGenInfo" (ppr tycon <+> ppr names)
311     
312
313 ----------------------------------------------------
314 --      Dealing with sums
315 ----------------------------------------------------
316 mk_sum_stuff :: [Unique]        -- Base for generating unique names
317              -> [TyVar]         -- Type variables over which the tycon is abstracted
318              -> [DataCon]       -- The data constructors
319              -> ([Alt Id], CoreExpr, Id)
320
321 -- For example, given
322 --      data T = C | D Int Int Int
323 -- 
324 -- mk_sum_stuff v [C,D] = ([C -> Inl Unit, D a b c -> Inr (a :*: (b :*: c))],
325 --                         case cd of { Inl u -> C; 
326 --                                      Inr abc -> case abc of { a :*: bc ->
327 --                                                 case bc  of { b :*: c ->
328 --                                                 D a b c }} },
329 --                         cd)
330
331 mk_sum_stuff us tyvars [datacon]
332    = ([from_alt], to_body_fn app_exp, rep_var)
333    where
334      types        = dataConOrigArgTys datacon   -- Existentials already excluded
335      datacon_vars = zipWith mkGenericLocal us types
336      us'          = dropList types us
337
338      app_exp      = mkVarApps (Var (dataConWrapId datacon)) (tyvars ++ datacon_vars)
339      from_alt     = mkReboxingAlt us' datacon datacon_vars from_alt_rhs
340                         -- We are talking about *user* datacons here; hence
341                         --      dataConWrapId
342                         --      mkReboxingAlt
343
344      (_,args',_)  = from_alt
345      us''         = dropList args' us'  -- Conservative, but safe
346      
347      (_, from_alt_rhs, to_body_fn, rep_var) = mk_prod_stuff us'' datacon_vars
348
349 mk_sum_stuff (u:us) tyvars datacons
350   = (wrap inlDataCon l_from_alts ++ wrap inrDataCon r_from_alts,
351      Case (Var rep_var) rep_var [(DataAlt inlDataCon, [l_rep_var], l_to_body),
352                                  (DataAlt inrDataCon, [r_rep_var], r_to_body)],
353      rep_var)
354   where
355     (l_datacons, r_datacons)            = splitInHalf datacons
356     (l_from_alts, l_to_body, l_rep_var) = mk_sum_stuff us tyvars l_datacons
357     (r_from_alts, r_to_body, r_rep_var) = mk_sum_stuff us tyvars r_datacons
358     rep_tys                             = [idType l_rep_var, idType r_rep_var]
359     rep_ty                              = mkTyConApp plusTyCon rep_tys
360     rep_var                             = mkGenericLocal u rep_ty
361
362     wrap :: DataCon -> [Alt Id] -> [Alt Id] 
363         -- Wrap an application of the Inl or Inr constructor round each alternative
364     wrap datacon alts
365         = [(dc, args, App datacon_app rhs) | (dc,args,rhs) <- alts]
366         where
367           datacon_app = mkTyApps (Var (dataConWrapId datacon)) rep_tys
368
369 ----------------------------------------------------
370 --      Dealing with products
371 ----------------------------------------------------
372 mk_prod_stuff :: [Unique]               -- Base for unique names
373               -> [Id]                   -- arg-ids; args of the original user-defined constructor
374                                         --      They are bound enclosing from_rhs
375                                         --      Please bind these in the to_body_fn 
376               -> ([Unique],             -- Depleted unique-name supply
377                   CoreExpr,             -- from-rhs: puts together the representation from the arg_ids
378                   CoreExpr -> CoreExpr, -- to_body_fn: takes apart the representation
379                   Id)                   -- The rep-id; please bind this to the representation
380
381 -- For example:
382 -- mk_prod_stuff [a,b,c] = ( a :*: (b :*: c),
383 --                           \x -> case abc of { a :*: bc ->
384 --                                 case bc  of { b :*: c  -> 
385 --                                 x,
386 --                           abc )
387
388 -- We need to use different uqiques in the branches 
389 -- because the returned to_body_fns are nested.  
390 -- Hence the returned unqique-name supply
391
392 mk_prod_stuff (u:us) []         -- Unit case
393   = (us,
394      Var (dataConWrapId genUnitDataCon),
395      \x -> x, 
396      mkGenericLocal u (mkTyConApp genUnitTyCon []))
397
398 mk_prod_stuff us [arg_var]      -- Singleton case
399   = (us, Var arg_var, \x -> x, arg_var)
400
401 mk_prod_stuff (u:us) arg_vars   -- Two or more
402   = (us'', 
403      mkConApp crossDataCon (map Type rep_tys ++ [l_alt_rhs, r_alt_rhs]),
404      \x -> Case (Var rep_var) rep_var 
405                 [(DataAlt crossDataCon, [l_rep_var, r_rep_var], l_to_body_fn (r_to_body_fn x))],
406      rep_var)
407   where
408     (l_arg_vars, r_arg_vars)             = splitInHalf arg_vars
409     (us', l_alt_rhs, l_to_body_fn, l_rep_var) = mk_prod_stuff us  l_arg_vars
410     (us'', r_alt_rhs, r_to_body_fn, r_rep_var) = mk_prod_stuff us' r_arg_vars
411     rep_var = mkGenericLocal u (mkTyConApp crossTyCon rep_tys)
412     rep_tys = [idType l_rep_var, idType r_rep_var]
413 \end{code}
414
415 A little utility function
416
417 \begin{code}
418 splitInHalf :: [a] -> ([a],[a])
419 splitInHalf list = (left, right)
420                  where
421                    half  = length list `div` 2
422                    left  = take half list
423                    right = drop half list
424
425 mkGenericLocal :: Unique -> Type -> Id
426 mkGenericLocal uniq ty = mkSysLocal FSLIT("g") uniq ty
427 \end{code}
428
429 %************************************************************************
430 %*                                                                      *
431 \subsection{Generating the RHS of a generic default method}
432 %*                                                                      *
433 %************************************************************************
434
435 Generating the Generic default method.  Uses the bimaps to generate the
436 actual method. All of this is rather incomplete, but it would be nice
437 to make even this work.  Example
438
439         class Foo a where
440           op :: Op a
441
442         instance Foo T
443
444 Then we fill in the RHS for op, RenamedHsExpr, by calling mkGenericRhs:
445
446         instance Foo T where
447            op = <mkGenericRhs op a T>
448
449 To do this, we generate a pair of RenamedHsExprs (EP toOp fromOp), where
450
451         toOp   :: Op Trep -> Op T
452         fromOp :: Op T    -> Op Trep
453
454 (the bimap) and then fill in the RHS with
455
456         instance Foo T where
457            op = toOp op
458
459 Remember, we're generating a RenamedHsExpr, so the result of all this
460 will be fed to the type checker.  So the 'op' on the RHS will be 
461 at the representation type for T, Trep.
462
463
464 A note about polymorphism.  Suppose the class op is polymorphic:
465
466         class Baz a where
467           op :: forall b. Ord b => a -> b -> b
468
469 Then we can still generate a bimap with
470
471         toOP :: forall b. (Trep -> b -> b) -> (T -> b -> b)
472
473 and fill in the instance decl thus
474
475         instance Foo T where
476            op = toOp op
477
478 By the time the type checker has done its stuff we'll get
479
480         instance Foo T where
481            op = \b. \dict::Ord b. toOp b (op Trep b dict)
482
483 \begin{code}
484 mkGenericRhs :: Id -> TyVar -> TyCon -> RenamedHsExpr
485 mkGenericRhs sel_id tyvar tycon
486   = HsApp (toEP bimap) (HsVar (idName sel_id))
487   where 
488         -- Initialising the "Environment" with the from/to functions
489         -- on the datatype (actually tycon) in question
490         Just (EP from to) = tyConGenInfo tycon  -- Caller checked this will succeed
491         ep                = EP (HsVar (idName from)) (HsVar (idName to)) 
492
493         -- Takes out the ForAll and the Class restrictions 
494         -- in front of the type of the method.
495         (_,_,op_ty) = tcSplitSigmaTy (idType sel_id)
496
497         -- Do it again!  This deals with the case where the method type 
498         -- is polymorphic -- see notes above
499         (local_tvs,_,final_ty) = tcSplitSigmaTy op_ty
500
501         -- Now we probably have a tycon in front
502         -- of us, quite probably a FunTyCon.
503         bimap = generate_bimap (tyvar, ep, local_tvs) final_ty
504
505 type EPEnv = (TyVar,            -- The class type variable
506               EP RenamedHsExpr, -- The EP it maps to
507               [TyVar]           -- Other in-scope tyvars; they have an identity EP
508              )
509
510 -------------------
511 generate_bimap :: EPEnv
512                -> Type
513                -> EP RenamedHsExpr
514 -- Top level case - splitting the TyCon.
515 generate_bimap env@(tv,ep,local_tvs) ty 
516   = case getTyVar_maybe ty of
517         Just tv1 |  tv == tv1 -> ep                             -- The class tyvar
518                  |  otherwise -> ASSERT( tv1 `elem` local_tvs)  -- One of the polymorphic tyvars of the method
519                                  idEP   
520         Nothing  -> bimapApp env (tcSplitTyConApp_maybe ty)
521
522 -------------------
523 bimapApp :: EPEnv -> Maybe (TyCon, [Type]) -> EP RenamedHsExpr
524 bimapApp env Nothing                = panic "TcClassDecl: Type Application!"
525 bimapApp env (Just (tycon, ty_args)) 
526   | tycon == funTyCon       = bimapArrow arg_eps
527   | isBoxedTupleTyCon tycon = bimapTuple arg_eps
528   | otherwise               =   -- Otherwise validGenericMethodType will 
529                                 -- have checked that the type is a constant type
530                               ASSERT( all (`elem` local_tvs) (varSetElems (tyVarsOfTypes ty_args)) )
531                               idEP
532     where
533       arg_eps = map (generate_bimap env) ty_args
534       (_,_,local_tvs) = env
535
536 -------------------
537 -- bimapArrow :: [EP a a', EP b b'] -> EP (a->b) (a'->b')
538 bimapArrow [ep1, ep2]
539   = EP { fromEP = mk_hs_lam [VarPatIn g1, VarPatIn g2] from_body, 
540          toEP   = mk_hs_lam [VarPatIn g1, VarPatIn g2] to_body }
541   where
542     from_body = fromEP ep2 `HsApp` (HsPar $ HsVar g1 `HsApp` (HsPar $ toEP   ep1 `HsApp` HsVar g2))
543     to_body   = toEP   ep2 `HsApp` (HsPar $ HsVar g1 `HsApp` (HsPar $ fromEP ep1 `HsApp` HsVar g2))
544
545 -------------------
546 bimapTuple eps 
547   = EP { fromEP = mk_hs_lam [tuple_pat] from_body,
548          toEP   = mk_hs_lam [tuple_pat] to_body }
549   where
550     names       = takeList eps genericNames
551     tuple_pat   = TuplePatIn (map VarPatIn names) Boxed
552     eps_w_names = eps `zip` names
553     to_body     = ExplicitTuple [toEP   ep `HsApp` HsVar g | (ep,g) <- eps_w_names] Boxed
554     from_body   = ExplicitTuple [fromEP ep `HsApp` HsVar g | (ep,g) <- eps_w_names] Boxed
555
556 -------------------
557 genericNames :: [Name]
558 genericNames = [mkSystemName (mkBuiltinUnique i) (mkFastString ('g' : show i)) | i <- [1..]]
559 (g1:g2:g3:_) = genericNames
560
561 mk_hs_lam pats body = HsPar (HsLam (mkSimpleMatch pats body placeHolderType builtinSrcLoc))
562
563 idEP :: EP RenamedHsExpr
564 idEP = EP idexpr idexpr
565      where
566        idexpr = mk_hs_lam [VarPatIn g3] (HsVar g3)
567 \end{code}