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 #-} TypeRep ( Type, Kind, SuperKind )
45 -- Should just be Type(Type), but this fails due to bug present up to
46 -- and including 4.02 involving slurping of hi-boot files. Bug is now fixed.
48 import {-# SOURCE #-} DataCon ( DataCon )
50 import Class ( Class )
52 import BasicTypes ( Arity, NewOrData(..), RecFlag(..) )
54 import Name ( Name, nameUnique, NamedThing(getName) )
55 import Unique ( Unique, Uniquable(..), anyBoxConKey )
56 import PrimRep ( PrimRep(..), isFollowableRep )
60 %************************************************************************
62 \subsection{The data type}
64 %************************************************************************
68 type SuperKindCon = TyCon
72 tyConUnique :: Unique,
79 | AlgTyCon { -- Tuples, data type, and newtype decls.
80 -- All lifted, all boxed
81 tyConUnique :: Unique,
86 tyConTyVars :: [TyVar],
87 dataTyConTheta :: [(Class,[Type])],
88 dataTyConArgVrcs :: ArgVrcs,
90 dataCons :: [DataCon],
91 -- Its data constructors, with fully polymorphic types
92 -- This list can be empty, when we import a data type abstractly,
93 -- either (a) the interface is hand-written and doesn't give
94 -- the constructors, or
95 -- (b) in a quest for fast compilation we don't import
98 dataTyConDerivings :: [Class], -- Classes which have derived instances
100 dataTyConClass_maybe :: (Maybe Class), -- Nothing for ordinary types;
101 -- Just c for the type constructor
102 -- for dictionaries of class c.
103 algTyConFlavour :: NewOrData,
104 algTyConRec :: RecFlag -- Tells whether the data type is part of
105 -- a mutually-recursive group or not
108 | PrimTyCon { -- Primitive types; cannot be defined in Haskell
109 -- NB: All of these guys are *unlifted*, but not all are *unboxed*
110 tyConUnique :: Unique,
114 primTyConArgVrcs :: ArgVrcs,
115 primTyConRep :: PrimRep
120 tyConUnique :: Unique,
124 tyConBoxed :: Bool, -- True for boxed; False for unboxed
125 tyConTyVars :: [TyVar],
130 tyConUnique :: Unique,
135 tyConTyVars :: [TyVar], -- Bound tyvars
136 synTyConDefn :: Type, -- Right-hand side, mentioning these type vars.
137 -- Acts as a template for the expansion when
138 -- the tycon is applied to some types.
139 synTyConArgVrcs :: ArgVrcs
142 | KindCon { -- Type constructor at the kind level
143 tyConUnique :: Unique,
145 tyConKind :: SuperKind,
149 | SuperKindCon { -- The type of kind variables or boxity variables,
150 tyConUnique :: Unique,
154 type ArgVrcs = [(Bool,Bool)] -- Tyvar variance info: [(occPos,occNeg)]
155 -- *NB*: this is tyvar variance info, *not*
156 -- termvar usage info.
159 %************************************************************************
161 \subsection{TyCon Construction}
163 %************************************************************************
165 Note: the TyCon constructors all take a Kind as one argument, even though
166 they could, in principle, work out their Kind from their other arguments.
167 But to do so they need functions from Types, and that makes a nasty
168 module mutual-recursion. And they aren't called from many places.
169 So we compromise, and move their Kind calculation to the call site.
172 mkSuperKindCon :: Name -> SuperKindCon
173 mkSuperKindCon name = SuperKindCon {
174 tyConUnique = nameUnique name,
178 mkKindCon :: Name -> SuperKind -> KindCon
181 tyConUnique = nameUnique name,
187 mkFunTyCon :: Name -> Kind -> TyCon
190 tyConUnique = nameUnique name,
196 mkAlgTyCon name kind tyvars theta argvrcs cons derivs maybe_clas flavour rec
199 tyConUnique = nameUnique name,
201 tyConArity = length tyvars,
202 tyConTyVars = tyvars,
203 dataTyConTheta = theta,
204 dataTyConArgVrcs = argvrcs,
206 dataTyConDerivings = derivs,
207 dataTyConClass_maybe = maybe_clas,
208 algTyConFlavour = flavour,
212 mkTupleTyCon name kind arity tyvars con boxed
214 tyConUnique = nameUnique name,
219 tyConTyVars = tyvars,
223 mkPrimTyCon name kind arity arg_vrcs rep
226 tyConUnique = nameUnique name,
229 primTyConArgVrcs = arg_vrcs,
233 mkSynTyCon name kind arity tyvars rhs argvrcs
236 tyConUnique = nameUnique name,
239 tyConTyVars = tyvars,
241 synTyConArgVrcs = argvrcs
244 setTyConName tc name = tc {tyConName = name, tyConUnique = nameUnique name}
248 isFunTyCon (FunTyCon {}) = True
251 isPrimTyCon (PrimTyCon {}) = True
252 isPrimTyCon _ = False
254 isUnLiftedTyCon (PrimTyCon {}) = True
255 isUnLiftedTyCon (TupleTyCon { tyConBoxed = False }) = True
256 isUnLiftedTyCon _ = False
258 -- isBoxedTyCon should not be applied to SynTyCon, nor KindCon
259 isBoxedTyCon (AlgTyCon {}) = True
260 isBoxedTyCon (FunTyCon {}) = True
261 isBoxedTyCon (TupleTyCon {tyConBoxed = boxed}) = boxed
262 isBoxedTyCon (PrimTyCon {primTyConRep = rep}) = isFollowableRep rep
264 -- isAlgTyCon returns True for both @data@ and @newtype@
265 isAlgTyCon (AlgTyCon {}) = True
266 isAlgTyCon (TupleTyCon {}) = True
267 isAlgTyCon other = False
269 -- isDataTyCon returns False for @newtype@ and for unboxed tuples
270 isDataTyCon (AlgTyCon {algTyConFlavour = new_or_data}) = case new_or_data of
273 isDataTyCon (TupleTyCon {tyConBoxed = True}) = True
274 isDataTyCon other = False
276 isNewTyCon (AlgTyCon {algTyConFlavour = NewType}) = True
277 isNewTyCon other = False
279 -- A "product" tycon is non-recursive and has one constructor, and is *not* an unboxed tuple
280 -- whether DataType or NewType
281 isProductTyCon (AlgTyCon {dataCons = [c], algTyConRec = NonRecursive}) = True
282 isProductTyCon (TupleTyCon { tyConBoxed = boxed }) = boxed
283 isProductTyCon other = False
285 isSynTyCon (SynTyCon {}) = True
288 isEnumerationTyCon (AlgTyCon {algTyConFlavour = EnumType}) = True
289 isEnumerationTyCon other = False
291 -- The unit tycon isn't classed as a tuple tycon
292 isTupleTyCon (TupleTyCon {tyConArity = arity, tyConBoxed = True}) = arity >= 2
293 isTupleTyCon other = False
295 isUnboxedTupleTyCon (TupleTyCon {tyConBoxed = False}) = True
296 isUnboxedTupleTyCon other = False
300 tyConDataCons :: TyCon -> [DataCon]
301 tyConDataCons (AlgTyCon {dataCons = cons}) = cons
302 tyConDataCons (TupleTyCon {dataCon = con}) = [con]
303 tyConDataCons other = []
304 -- You may think this last equation should fail,
305 -- but it's quite convenient to return no constructors for
306 -- a synonym; see for example the call in TcTyClsDecls.
308 tyConFamilySize :: TyCon -> Int
309 tyConFamilySize (AlgTyCon {dataCons = cons}) = length cons
310 tyConFamilySize (TupleTyCon {}) = 1
312 tyConFamilySize other = pprPanic "tyConFamilySize:" (ppr other)
315 tyConPrimRep :: TyCon -> PrimRep
316 tyConPrimRep (PrimTyCon {primTyConRep = rep}) = rep
317 tyConPrimRep _ = PtrRep
321 tyConDerivings :: TyCon -> [Class]
322 tyConDerivings (AlgTyCon {dataTyConDerivings = derivs}) = derivs
323 tyConDerivings other = []
327 tyConTheta :: TyCon -> [(Class, [Type])]
328 tyConTheta (AlgTyCon {dataTyConTheta = theta}) = theta
329 -- should ask about anything else
332 @tyConArgVrcs_maybe@ gives a list of (occPos,occNeg) flags, one for
333 each tyvar, if available. See @calcAlgTyConArgVrcs@ for how this is
334 actually computed (in another file).
337 tyConArgVrcs_maybe :: TyCon -> Maybe ArgVrcs
339 tyConArgVrcs_maybe (FunTyCon {} ) = Just [(False,True),(True,False)]
340 tyConArgVrcs_maybe (AlgTyCon {dataTyConArgVrcs = oi}) = Just oi
341 tyConArgVrcs_maybe (PrimTyCon {primTyConArgVrcs = oi}) = Just oi
342 tyConArgVrcs_maybe (TupleTyCon {tyConArity = arity }) = Just (replicate arity (True,False))
343 tyConArgVrcs_maybe (SynTyCon {synTyConArgVrcs = oi }) = Just oi
344 tyConArgVrcs_maybe _ = Nothing
348 getSynTyConDefn :: TyCon -> ([TyVar], Type)
349 getSynTyConDefn (SynTyCon {tyConTyVars = tyvars, synTyConDefn = ty}) = (tyvars,ty)
353 maybeTyConSingleCon :: TyCon -> Maybe DataCon
354 maybeTyConSingleCon (AlgTyCon {dataCons = [c]}) = Just c
355 maybeTyConSingleCon (AlgTyCon {}) = Nothing
356 maybeTyConSingleCon (TupleTyCon {dataCon = con}) = Just con
357 maybeTyConSingleCon (PrimTyCon {}) = Nothing
358 maybeTyConSingleCon (FunTyCon {}) = Nothing -- case at funty
359 maybeTyConSingleCon tc = pprPanic "maybeTyConSingleCon: unexpected tycon " $
364 tyConClass_maybe :: TyCon -> Maybe Class
365 tyConClass_maybe (AlgTyCon {dataTyConClass_maybe = maybe_cls}) = maybe_cls
366 tyConClass_maybe other_tycon = Nothing
370 %************************************************************************
372 \subsection[TyCon-instances]{Instance declarations for @TyCon@}
374 %************************************************************************
376 @TyCon@s are compared by comparing their @Unique@s.
378 The strictness analyser needs @Ord@. It is a lexicographic order with
379 the property @(a<=b) || (b<=a)@.
382 instance Eq TyCon where
383 a == b = case (a `compare` b) of { EQ -> True; _ -> False }
384 a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
386 instance Ord TyCon where
387 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
388 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
389 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
390 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
391 compare a b = getUnique a `compare` getUnique b
393 instance Uniquable TyCon where
394 getUnique tc = tyConUnique tc
396 instance Outputable TyCon where
397 ppr tc = ppr (getName tc)
399 instance NamedThing TyCon where
404 %************************************************************************
406 \subsection{Kind constructors}
408 %************************************************************************
410 @matchesTyCon tc1 tc2@ checks whether an appliation
411 (tc1 t1..tn) matches (tc2 t1..tn). By "matches" we basically mean "equals",
412 except that at the kind level tc2 might have more boxity info than tc1.
415 matchesTyCon :: TyCon -- Expected (e.g. arg type of function)
416 -> TyCon -- Inferred (e.g. type of actual arg to function)
419 matchesTyCon tc1 tc2 = uniq1 == uniq2 || uniq1 == anyBoxConKey
421 uniq1 = tyConUnique tc1
422 uniq2 = tyConUnique tc2