2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[TyCon]{The @TyCon@ datatype}
8 TyCon, KindCon, SuperKindCon, ArgVrcs,
10 isFunTyCon, isUnLiftedTyCon, isBoxedTyCon, isProductTyCon,
11 isAlgTyCon, isDataTyCon, isSynTyCon, isNewTyCon, isPrimTyCon,
12 isEnumerationTyCon, isTupleTyCon, isUnboxedTupleTyCon,
42 #include "HsVersions.h"
44 import {-# SOURCE #-} Type ( Type, Kind, SuperKind )
45 import {-# SOURCE #-} DataCon ( DataCon )
47 import Class ( Class )
49 import BasicTypes ( Arity, NewOrData(..), RecFlag(..) )
51 import Name ( Name, nameUnique, NamedThing(getName) )
52 import Unique ( Unique, Uniquable(..), anyBoxConKey )
53 import PrimRep ( PrimRep(..), isFollowableRep )
57 %************************************************************************
59 \subsection{The data type}
61 %************************************************************************
65 type SuperKindCon = TyCon
69 tyConUnique :: Unique,
76 | AlgTyCon { -- Tuples, data type, and newtype decls.
77 -- All lifted, all boxed
78 tyConUnique :: Unique,
83 tyConTyVars :: [TyVar],
84 dataTyConTheta :: [(Class,[Type])],
85 dataTyConArgVrcs :: ArgVrcs,
87 dataCons :: [DataCon],
88 -- Its data constructors, with fully polymorphic types
89 -- This list can be empty, when we import a data type abstractly,
90 -- either (a) the interface is hand-written and doesn't give
91 -- the constructors, or
92 -- (b) in a quest for fast compilation we don't import
95 dataTyConDerivings :: [Class], -- Classes which have derived instances
97 dataTyConClass_maybe :: (Maybe Class), -- Nothing for ordinary types;
98 -- Just c for the type constructor
99 -- for dictionaries of class c.
100 algTyConFlavour :: NewOrData,
101 algTyConRec :: RecFlag -- Tells whether the data type is part of
102 -- a mutually-recursive group or not
105 | PrimTyCon { -- Primitive types; cannot be defined in Haskell
106 -- NB: All of these guys are *unlifted*, but not all are *unboxed*
107 tyConUnique :: Unique,
111 primTyConArgVrcs :: ArgVrcs,
112 primTyConRep :: PrimRep
117 tyConUnique :: Unique,
121 tyConBoxed :: Bool, -- True for boxed; False for unboxed
122 tyConTyVars :: [TyVar],
127 tyConUnique :: Unique,
132 tyConTyVars :: [TyVar], -- Bound tyvars
133 synTyConDefn :: Type, -- Right-hand side, mentioning these type vars.
134 -- Acts as a template for the expansion when
135 -- the tycon is applied to some types.
136 synTyConArgVrcs :: ArgVrcs
139 | KindCon { -- Type constructor at the kind level
140 tyConUnique :: Unique,
142 tyConKind :: SuperKind,
146 | SuperKindCon { -- The type of kind variables or boxity variables,
147 tyConUnique :: Unique,
151 type ArgVrcs = [(Bool,Bool)] -- Tyvar variance info: [(occPos,occNeg)]
152 -- *NB*: this is tyvar variance info, *not*
153 -- termvar usage info.
156 %************************************************************************
158 \subsection{TyCon Construction}
160 %************************************************************************
162 Note: the TyCon constructors all take a Kind as one argument, even though
163 they could, in principle, work out their Kind from their other arguments.
164 But to do so they need functions from Types, and that makes a nasty
165 module mutual-recursion. And they aren't called from many places.
166 So we compromise, and move their Kind calculation to the call site.
169 mkSuperKindCon :: Name -> SuperKindCon
170 mkSuperKindCon name = SuperKindCon {
171 tyConUnique = nameUnique name,
175 mkKindCon :: Name -> SuperKind -> KindCon
178 tyConUnique = nameUnique name,
184 mkFunTyCon :: Name -> Kind -> TyCon
187 tyConUnique = nameUnique name,
193 mkAlgTyCon name kind tyvars theta argvrcs cons derivs maybe_clas flavour rec
196 tyConUnique = nameUnique name,
198 tyConArity = length tyvars,
199 tyConTyVars = tyvars,
200 dataTyConTheta = theta,
201 dataTyConArgVrcs = argvrcs,
203 dataTyConDerivings = derivs,
204 dataTyConClass_maybe = maybe_clas,
205 algTyConFlavour = flavour,
209 mkTupleTyCon name kind arity tyvars con boxed
211 tyConUnique = nameUnique name,
216 tyConTyVars = tyvars,
220 mkPrimTyCon name kind arity arg_vrcs rep
223 tyConUnique = nameUnique name,
226 primTyConArgVrcs = arg_vrcs,
230 mkSynTyCon name kind arity tyvars rhs argvrcs
233 tyConUnique = nameUnique name,
236 tyConTyVars = tyvars,
238 synTyConArgVrcs = argvrcs
241 setTyConName tc name = tc {tyConName = name, tyConUnique = nameUnique name}
245 isFunTyCon (FunTyCon {}) = True
248 isPrimTyCon (PrimTyCon {}) = True
249 isPrimTyCon _ = False
251 isUnLiftedTyCon (PrimTyCon {}) = True
252 isUnLiftedTyCon (TupleTyCon { tyConBoxed = False }) = True
253 isUnLiftedTyCon _ = False
255 -- isBoxedTyCon should not be applied to SynTyCon, nor KindCon
256 isBoxedTyCon (AlgTyCon {}) = True
257 isBoxedTyCon (FunTyCon {}) = True
258 isBoxedTyCon (TupleTyCon {tyConBoxed = boxed}) = boxed
259 isBoxedTyCon (PrimTyCon {primTyConRep = rep}) = isFollowableRep rep
261 -- isAlgTyCon returns True for both @data@ and @newtype@
262 isAlgTyCon (AlgTyCon {}) = True
263 isAlgTyCon (TupleTyCon {}) = True
264 isAlgTyCon other = False
266 -- isDataTyCon returns False for @newtype@ and for unboxed tuples
267 isDataTyCon (AlgTyCon {algTyConFlavour = new_or_data}) = case new_or_data of
270 isDataTyCon (TupleTyCon {tyConBoxed = True}) = True
271 isDataTyCon other = False
273 isNewTyCon (AlgTyCon {algTyConFlavour = NewType}) = True
274 isNewTyCon other = False
276 -- A "product" tycon is non-recursive and has one constructor, and is *not* an unboxed tuple
277 -- whether DataType or NewType
278 isProductTyCon (AlgTyCon {dataCons = [c], algTyConRec = NonRecursive}) = True
279 isProductTyCon (TupleTyCon { tyConBoxed = boxed }) = boxed
280 isProductTyCon other = False
282 isSynTyCon (SynTyCon {}) = True
285 isEnumerationTyCon (AlgTyCon {algTyConFlavour = EnumType}) = True
286 isEnumerationTyCon other = False
288 -- The unit tycon isn't classed as a tuple tycon
289 isTupleTyCon (TupleTyCon {tyConArity = arity, tyConBoxed = True}) = arity >= 2
290 isTupleTyCon other = False
292 isUnboxedTupleTyCon (TupleTyCon {tyConBoxed = False}) = True
293 isUnboxedTupleTyCon other = False
297 tyConDataCons :: TyCon -> [DataCon]
298 tyConDataCons (AlgTyCon {dataCons = cons}) = cons
299 tyConDataCons (TupleTyCon {dataCon = con}) = [con]
300 tyConDataCons other = []
301 -- You may think this last equation should fail,
302 -- but it's quite convenient to return no constructors for
303 -- a synonym; see for example the call in TcTyClsDecls.
305 tyConFamilySize :: TyCon -> Int
306 tyConFamilySize (AlgTyCon {dataCons = cons}) = length cons
307 tyConFamilySize (TupleTyCon {}) = 1
309 tyConFamilySize other = pprPanic "tyConFamilySize:" (ppr other)
312 tyConPrimRep :: TyCon -> PrimRep
313 tyConPrimRep (PrimTyCon {primTyConRep = rep}) = rep
314 tyConPrimRep _ = PtrRep
318 tyConDerivings :: TyCon -> [Class]
319 tyConDerivings (AlgTyCon {dataTyConDerivings = derivs}) = derivs
320 tyConDerivings other = []
324 tyConTheta :: TyCon -> [(Class, [Type])]
325 tyConTheta (AlgTyCon {dataTyConTheta = theta}) = theta
326 -- should ask about anything else
329 @tyConArgVrcs_maybe@ gives a list of (occPos,occNeg) flags, one for
330 each tyvar, if available. See @calcAlgTyConArgVrcs@ for how this is
331 actually computed (in another file).
334 tyConArgVrcs_maybe :: TyCon -> Maybe ArgVrcs
336 tyConArgVrcs_maybe (FunTyCon {} ) = Just [(False,True),(True,False)]
337 tyConArgVrcs_maybe (AlgTyCon {dataTyConArgVrcs = oi}) = Just oi
338 tyConArgVrcs_maybe (PrimTyCon {primTyConArgVrcs = oi}) = Just oi
339 tyConArgVrcs_maybe (TupleTyCon {tyConArity = arity }) = Just (replicate arity (True,False))
340 tyConArgVrcs_maybe (SynTyCon {synTyConArgVrcs = oi }) = Just oi
341 tyConArgVrcs_maybe _ = Nothing
345 getSynTyConDefn :: TyCon -> ([TyVar], Type)
346 getSynTyConDefn (SynTyCon {tyConTyVars = tyvars, synTyConDefn = ty}) = (tyvars,ty)
350 maybeTyConSingleCon :: TyCon -> Maybe DataCon
351 maybeTyConSingleCon (AlgTyCon {dataCons = [c]}) = Just c
352 maybeTyConSingleCon (AlgTyCon {}) = Nothing
353 maybeTyConSingleCon (TupleTyCon {dataCon = con}) = Just con
354 maybeTyConSingleCon (PrimTyCon {}) = Nothing
355 maybeTyConSingleCon (FunTyCon {}) = Nothing -- case at funty
356 maybeTyConSingleCon tc = pprPanic "maybeTyConSingleCon: unexpected tycon " $
361 tyConClass_maybe :: TyCon -> Maybe Class
362 tyConClass_maybe (AlgTyCon {dataTyConClass_maybe = maybe_cls}) = maybe_cls
363 tyConClass_maybe other_tycon = Nothing
367 %************************************************************************
369 \subsection[TyCon-instances]{Instance declarations for @TyCon@}
371 %************************************************************************
373 @TyCon@s are compared by comparing their @Unique@s.
375 The strictness analyser needs @Ord@. It is a lexicographic order with
376 the property @(a<=b) || (b<=a)@.
379 instance Eq TyCon where
380 a == b = case (a `compare` b) of { EQ -> True; _ -> False }
381 a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
383 instance Ord TyCon where
384 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
385 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
386 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
387 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
388 compare a b = getUnique a `compare` getUnique b
390 instance Uniquable TyCon where
391 getUnique tc = tyConUnique tc
393 instance Outputable TyCon where
394 ppr tc = ppr (getName tc)
396 instance NamedThing TyCon where
401 %************************************************************************
403 \subsection{Kind constructors}
405 %************************************************************************
407 @matchesTyCon tc1 tc2@ checks whether an appliation
408 (tc1 t1..tn) matches (tc2 t1..tn). By "matches" we basically mean "equals",
409 except that at the kind level tc2 might have more boxity info than tc1.
412 matchesTyCon :: TyCon -- Expected (e.g. arg type of function)
413 -> TyCon -- Inferred (e.g. type of actual arg to function)
416 matchesTyCon tc1 tc2 = uniq1 == uniq2 || uniq1 == anyBoxConKey
418 uniq1 = tyConUnique tc1
419 uniq2 = tyConUnique tc2