2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[TyCon]{The @TyCon@ datatype}
8 TyCon, KindCon, SuperKindCon,
10 isFunTyCon, isUnLiftedTyCon, isBoxedTyCon, isProductTyCon,
11 isAlgTyCon, isDataTyCon, isSynTyCon, isNewTyCon, isPrimTyCon,
12 isEnumerationTyCon, isTupleTyCon, isUnboxedTupleTyCon,
41 #include "HsVersions.h"
43 import {-# SOURCE #-} Type ( Type, Kind, SuperKind )
44 import {-# SOURCE #-} DataCon ( DataCon )
46 import Class ( Class )
48 import BasicTypes ( Arity, NewOrData(..), RecFlag(..) )
50 import Name ( Name, nameUnique, NamedThing(getName) )
51 import Unique ( Unique, Uniquable(..), anyBoxConKey )
52 import PrimRep ( PrimRep(..), isFollowableRep )
56 %************************************************************************
58 \subsection{The data type}
60 %************************************************************************
64 type SuperKindCon = TyCon
68 tyConUnique :: Unique,
75 | AlgTyCon { -- Tuples, data type, and newtype decls.
76 -- All lifted, all boxed
77 tyConUnique :: Unique,
82 tyConTyVars :: [TyVar],
83 dataTyConTheta :: [(Class,[Type])],
85 dataCons :: [DataCon],
86 -- Its data constructors, with fully polymorphic types
87 -- This list can be empty, when we import a data type abstractly,
88 -- either (a) the interface is hand-written and doesn't give
89 -- the constructors, or
90 -- (b) in a quest for fast compilation we don't import
93 dataTyConDerivings :: [Class], -- Classes which have derived instances
95 dataTyConClass_maybe :: (Maybe Class), -- Nothing for ordinary types;
96 -- Just c for the type constructor
97 -- for dictionaries of class c.
98 algTyConFlavour :: NewOrData,
99 algTyConRec :: RecFlag -- Tells whether the data type is part of
100 -- a mutually-recursive group or not
103 | PrimTyCon { -- Primitive types; cannot be defined in Haskell
104 -- NB: All of these guys are *unlifted*, but not all are *unboxed*
105 tyConUnique :: Unique,
109 primTyConRep :: PrimRep
114 tyConUnique :: Unique,
118 tyConBoxed :: Bool, -- True for boxed; False for unboxed
119 tyConTyVars :: [TyVar],
124 tyConUnique :: Unique,
129 tyConTyVars :: [TyVar], -- Bound tyvars
130 synTyConDefn :: Type -- Right-hand side, mentioning these type vars.
131 -- Acts as a template for the expansion when
132 -- the tycon is applied to some types.
135 | KindCon { -- Type constructor at the kind level
136 tyConUnique :: Unique,
138 tyConKind :: SuperKind,
142 | SuperKindCon { -- The type of kind variables or boxity variables,
143 tyConUnique :: Unique,
148 %************************************************************************
150 \subsection{TyCon Construction}
152 %************************************************************************
154 Note: the TyCon constructors all take a Kind as one argument, even though
155 they could, in principle, work out their Kind from their other arguments.
156 But to do so they need functions from Types, and that makes a nasty
157 module mutual-recursion. And they aren't called from many places.
158 So we compromise, and move their Kind calculation to the call site.
161 mkSuperKindCon :: Name -> SuperKindCon
162 mkSuperKindCon name = SuperKindCon {
163 tyConUnique = nameUnique name,
167 mkKindCon :: Name -> SuperKind -> KindCon
170 tyConUnique = nameUnique name,
176 mkFunTyCon :: Name -> Kind -> TyCon
179 tyConUnique = nameUnique name,
185 mkAlgTyCon name kind tyvars theta cons derivs maybe_clas flavour rec
188 tyConUnique = nameUnique name,
190 tyConArity = length tyvars,
191 tyConTyVars = tyvars,
192 dataTyConTheta = theta,
194 dataTyConDerivings = derivs,
195 dataTyConClass_maybe = maybe_clas,
196 algTyConFlavour = flavour,
200 mkTupleTyCon name kind arity tyvars con boxed
202 tyConUnique = nameUnique name,
207 tyConTyVars = tyvars,
211 mkPrimTyCon name kind arity rep
214 tyConUnique = nameUnique name,
220 mkSynTyCon name kind arity tyvars rhs
223 tyConUnique = nameUnique name,
226 tyConTyVars = tyvars,
230 setTyConName tc name = tc {tyConName = name, tyConUnique = nameUnique name}
234 isFunTyCon (FunTyCon {}) = True
237 isPrimTyCon (PrimTyCon {}) = True
238 isPrimTyCon _ = False
240 isUnLiftedTyCon (PrimTyCon {}) = True
241 isUnLiftedTyCon (TupleTyCon { tyConBoxed = False }) = True
242 isUnLiftedTyCon _ = False
244 -- isBoxedTyCon should not be applied to SynTyCon, nor KindCon
245 isBoxedTyCon (AlgTyCon {}) = True
246 isBoxedTyCon (FunTyCon {}) = True
247 isBoxedTyCon (TupleTyCon {tyConBoxed = boxed}) = boxed
248 isBoxedTyCon (PrimTyCon {primTyConRep = rep}) = isFollowableRep rep
250 -- isAlgTyCon returns True for both @data@ and @newtype@
251 isAlgTyCon (AlgTyCon {}) = True
252 isAlgTyCon (TupleTyCon {}) = True
253 isAlgTyCon other = False
255 -- isDataTyCon returns False for @newtype@ and for unboxed tuples
256 isDataTyCon (AlgTyCon {algTyConFlavour = new_or_data}) = case new_or_data of
259 isDataTyCon (TupleTyCon {tyConBoxed = True}) = True
260 isDataTyCon other = False
262 isNewTyCon (AlgTyCon {algTyConFlavour = NewType}) = True
263 isNewTyCon other = False
265 -- A "product" tycon is non-recursive and has one constructor,
266 -- whether DataType or NewType
267 isProductTyCon (AlgTyCon {dataCons = [c], algTyConRec = NonRecursive}) = True
268 isProductTyCon (TupleTyCon {}) = True
269 isProductTyCon other = False
271 isSynTyCon (SynTyCon {}) = True
274 isEnumerationTyCon (AlgTyCon {algTyConFlavour = EnumType}) = True
275 isEnumerationTyCon other = False
277 -- The unit tycon isn't classed as a tuple tycon
278 isTupleTyCon (TupleTyCon {tyConArity = arity, tyConBoxed = True}) = arity >= 2
279 isTupleTyCon other = False
281 isUnboxedTupleTyCon (TupleTyCon {tyConBoxed = False}) = True
282 isUnboxedTupleTyCon other = False
286 tyConDataCons :: TyCon -> [DataCon]
287 tyConDataCons (AlgTyCon {dataCons = cons}) = cons
288 tyConDataCons (TupleTyCon {dataCon = con}) = [con]
289 tyConDataCons other = []
290 -- You may think this last equation should fail,
291 -- but it's quite convenient to return no constructors for
292 -- a synonym; see for example the call in TcTyClsDecls.
294 tyConFamilySize :: TyCon -> Int
295 tyConFamilySize (AlgTyCon {dataCons = cons}) = length cons
296 tyConFamilySize (TupleTyCon {}) = 1
298 tyConFamilySize other = pprPanic "tyConFamilySize:" (ppr other)
301 tyConPrimRep :: TyCon -> PrimRep
302 tyConPrimRep (PrimTyCon {primTyConRep = rep}) = rep
303 tyConPrimRep _ = PtrRep
307 tyConDerivings :: TyCon -> [Class]
308 tyConDerivings (AlgTyCon {dataTyConDerivings = derivs}) = derivs
309 tyConDerivings other = []
313 tyConTheta :: TyCon -> [(Class, [Type])]
314 tyConTheta (AlgTyCon {dataTyConTheta = theta}) = theta
315 -- should ask about anything else
319 getSynTyConDefn :: TyCon -> ([TyVar], Type)
320 getSynTyConDefn (SynTyCon {tyConTyVars = tyvars, synTyConDefn = ty}) = (tyvars,ty)
324 maybeTyConSingleCon :: TyCon -> Maybe DataCon
325 maybeTyConSingleCon (AlgTyCon {dataCons = [c]}) = Just c
326 maybeTyConSingleCon (AlgTyCon {}) = Nothing
327 maybeTyConSingleCon (TupleTyCon {dataCon = con}) = Just con
328 maybeTyConSingleCon (PrimTyCon {}) = Nothing
329 maybeTyConSingleCon other = panic (showSDoc (ppr other))
333 tyConClass_maybe :: TyCon -> Maybe Class
334 tyConClass_maybe (AlgTyCon {dataTyConClass_maybe = maybe_cls}) = maybe_cls
335 tyConClass_maybe other_tycon = Nothing
339 %************************************************************************
341 \subsection[TyCon-instances]{Instance declarations for @TyCon@}
343 %************************************************************************
345 @TyCon@s are compared by comparing their @Unique@s.
347 The strictness analyser needs @Ord@. It is a lexicographic order with
348 the property @(a<=b) || (b<=a)@.
351 instance Eq TyCon where
352 a == b = case (a `compare` b) of { EQ -> True; _ -> False }
353 a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
355 instance Ord TyCon where
356 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
357 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
358 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
359 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
360 compare a b = getUnique a `compare` getUnique b
362 instance Uniquable TyCon where
363 getUnique tc = tyConUnique tc
365 instance Outputable TyCon where
366 ppr tc = ppr (getName tc)
368 instance NamedThing TyCon where
373 %************************************************************************
375 \subsection{Kind constructors}
377 %************************************************************************
379 @matchesTyCon tc1 tc2@ checks whether an appliation
380 (tc1 t1..tn) matches (tc2 t1..tn). By "matches" we basically mean "equals",
381 except that at the kind level tc2 might have more boxity info than tc1.
384 matchesTyCon :: TyCon -- Expected (e.g. arg type of function)
385 -> TyCon -- Inferred (e.g. type of actual arg to function)
388 matchesTyCon tc1 tc2 = uniq1 == uniq2 || uniq1 == anyBoxConKey
390 uniq1 = tyConUnique tc1
391 uniq2 = tyConUnique tc2