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,
39 #include "HsVersions.h"
41 import {-# SOURCE #-} Type ( Type, Kind, SuperKind )
42 import {-# SOURCE #-} DataCon ( DataCon )
44 import Class ( Class )
46 import BasicTypes ( Arity, NewOrData(..), RecFlag(..) )
48 import Name ( Name, nameUnique, NamedThing(getName) )
49 import Unique ( Unique, Uniquable(..), anyBoxConKey )
50 import PrimRep ( PrimRep(..), isFollowableRep )
54 %************************************************************************
56 \subsection{The data type}
58 %************************************************************************
62 type SuperKindCon = TyCon
66 tyConUnique :: Unique,
73 | AlgTyCon { -- Tuples, data type, and newtype decls.
74 -- All lifted, all boxed
75 tyConUnique :: Unique,
80 tyConTyVars :: [TyVar],
81 dataTyConTheta :: [(Class,[Type])],
83 dataCons :: [DataCon],
84 -- Its data constructors, with fully polymorphic types
85 -- This list can be empty, when we import a data type abstractly,
86 -- either (a) the interface is hand-written and doesn't give
87 -- the constructors, or
88 -- (b) in a quest for fast compilation we don't import
91 dataTyConDerivings :: [Class], -- Classes which have derived instances
93 dataTyConClass_maybe :: (Maybe Class), -- Nothing for ordinary types;
94 -- Just c for the type constructor
95 -- for dictionaries of class c.
96 algTyConFlavour :: NewOrData,
97 algTyConRec :: RecFlag -- Tells whether the data type is part of
98 -- a mutually-recursive group or not
101 | PrimTyCon { -- Primitive types; cannot be defined in Haskell
102 -- NB: All of these guys are *unlifted*, but not all are *unboxed*
103 tyConUnique :: Unique,
107 primTyConRep :: PrimRep
112 tyConUnique :: Unique,
116 tyConBoxed :: Bool, -- True for boxed; False for unboxed
117 tyConTyVars :: [TyVar],
122 tyConUnique :: Unique,
127 tyConTyVars :: [TyVar], -- Bound tyvars
128 synTyConDefn :: Type -- Right-hand side, mentioning these type vars.
129 -- Acts as a template for the expansion when
130 -- the tycon is applied to some types.
133 | KindCon { -- Type constructor at the kind level
134 tyConUnique :: Unique,
136 tyConKind :: SuperKind,
140 | SuperKindCon { -- The type of kind variables or boxity variables,
141 tyConUnique :: Unique,
146 %************************************************************************
148 \subsection{TyCon Construction}
150 %************************************************************************
152 Note: the TyCon constructors all take a Kind as one argument, even though
153 they could, in principle, work out their Kind from their other arguments.
154 But to do so they need functions from Types, and that makes a nasty
155 module mutual-recursion. And they aren't called from many places.
156 So we compromise, and move their Kind calculation to the call site.
159 mkSuperKindCon :: Name -> SuperKindCon
160 mkSuperKindCon name = SuperKindCon {
161 tyConUnique = nameUnique name,
165 mkKindCon :: Name -> SuperKind -> KindCon
168 tyConUnique = nameUnique name,
174 mkFunTyCon :: Name -> Kind -> TyCon
177 tyConUnique = nameUnique name,
183 mkAlgTyCon name kind tyvars theta cons derivs maybe_clas flavour rec
186 tyConUnique = nameUnique name,
188 tyConArity = length tyvars,
189 tyConTyVars = tyvars,
190 dataTyConTheta = theta,
192 dataTyConDerivings = derivs,
193 dataTyConClass_maybe = maybe_clas,
194 algTyConFlavour = flavour,
198 mkTupleTyCon name kind arity tyvars con boxed
200 tyConUnique = nameUnique name,
205 tyConTyVars = tyvars,
209 mkPrimTyCon name kind arity rep
212 tyConUnique = nameUnique name,
218 mkSynTyCon name kind arity tyvars rhs
221 tyConUnique = nameUnique name,
224 tyConTyVars = tyvars,
230 isFunTyCon (FunTyCon {}) = True
233 isPrimTyCon (PrimTyCon {}) = True
234 isPrimTyCon _ = False
236 isUnLiftedTyCon (PrimTyCon {}) = True
237 isUnLiftedTyCon (TupleTyCon { tyConBoxed = False }) = True
238 isUnLiftedTyCon _ = False
240 -- isBoxedTyCon should not be applied to SynTyCon, nor KindCon
241 isBoxedTyCon (AlgTyCon {}) = True
242 isBoxedTyCon (FunTyCon {}) = True
243 isBoxedTyCon (TupleTyCon {tyConBoxed = boxed}) = boxed
244 isBoxedTyCon (PrimTyCon {primTyConRep = rep}) = isFollowableRep rep
246 -- isAlgTyCon returns True for both @data@ and @newtype@
247 isAlgTyCon (AlgTyCon {}) = True
248 isAlgTyCon (TupleTyCon {}) = True
249 isAlgTyCon other = False
251 -- isDataTyCon returns False for @newtype@.
252 isDataTyCon (AlgTyCon {algTyConFlavour = new_or_data}) = case new_or_data of
255 isDataTyCon (TupleTyCon {}) = True -- is an unboxed tuple a datatype?
256 isDataTyCon other = False
258 isNewTyCon (AlgTyCon {algTyConFlavour = NewType}) = True
259 isNewTyCon other = False
261 -- A "product" tycon is non-recursive and has one constructor,
262 -- whether DataType or NewType
263 isProductTyCon (AlgTyCon {dataCons = [c], algTyConRec = NonRecursive}) = True
264 isProductTyCon (TupleTyCon {}) = True
265 isProductTyCon other = False
267 isSynTyCon (SynTyCon {}) = True
270 isEnumerationTyCon (AlgTyCon {algTyConFlavour = EnumType}) = True
271 isEnumerationTyCon other = False
273 -- The unit tycon isn't classed as a tuple tycon
274 isTupleTyCon (TupleTyCon {tyConArity = arity, tyConBoxed = True}) = arity >= 2
275 isTupleTyCon other = False
277 isUnboxedTupleTyCon (TupleTyCon {tyConBoxed = False}) = True
278 isUnboxedTupleTyCon other = False
282 tyConDataCons :: TyCon -> [DataCon]
283 tyConDataCons (AlgTyCon {dataCons = cons}) = cons
284 tyConDataCons (TupleTyCon {dataCon = con}) = [con]
285 tyConDataCons other = []
286 -- You may think this last equation should fail,
287 -- but it's quite convenient to return no constructors for
288 -- a synonym; see for example the call in TcTyClsDecls.
290 tyConFamilySize :: TyCon -> Int
291 tyConFamilySize (AlgTyCon {dataCons = cons}) = length cons
292 tyConFamilySize (TupleTyCon {}) = 1
294 tyConFamilySize other = pprPanic "tyConFamilySize:" (ppr other)
297 tyConPrimRep :: TyCon -> PrimRep
298 tyConPrimRep (PrimTyCon {primTyConRep = rep}) = rep
299 tyConPrimRep _ = PtrRep
303 tyConDerivings :: TyCon -> [Class]
304 tyConDerivings (AlgTyCon {dataTyConDerivings = derivs}) = derivs
305 tyConDerivings other = []
309 tyConTheta :: TyCon -> [(Class, [Type])]
310 tyConTheta (AlgTyCon {dataTyConTheta = theta}) = theta
311 -- should ask about anything else
315 getSynTyConDefn :: TyCon -> ([TyVar], Type)
316 getSynTyConDefn (SynTyCon {tyConTyVars = tyvars, synTyConDefn = ty}) = (tyvars,ty)
320 maybeTyConSingleCon :: TyCon -> Maybe DataCon
321 maybeTyConSingleCon (AlgTyCon {dataCons = [c]}) = Just c
322 maybeTyConSingleCon (AlgTyCon {}) = Nothing
323 maybeTyConSingleCon (TupleTyCon {dataCon = con}) = Just con
324 maybeTyConSingleCon (PrimTyCon {}) = Nothing
328 tyConClass_maybe :: TyCon -> Maybe Class
329 tyConClass_maybe (AlgTyCon {dataTyConClass_maybe = maybe_cls}) = maybe_cls
330 tyConClass_maybe other_tycon = Nothing
334 %************************************************************************
336 \subsection[TyCon-instances]{Instance declarations for @TyCon@}
338 %************************************************************************
340 @TyCon@s are compared by comparing their @Unique@s.
342 The strictness analyser needs @Ord@. It is a lexicographic order with
343 the property @(a<=b) || (b<=a)@.
346 instance Eq TyCon where
347 a == b = case (a `compare` b) of { EQ -> True; _ -> False }
348 a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
350 instance Ord TyCon where
351 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
352 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
353 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
354 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
355 compare a b = getUnique a `compare` getUnique b
357 instance Uniquable TyCon where
358 getUnique tc = tyConUnique tc
360 instance Outputable TyCon where
361 ppr tc = ppr (getName tc)
363 instance NamedThing TyCon where
368 %************************************************************************
370 \subsection{Kind constructors}
372 %************************************************************************
374 @matchesTyCon tc1 tc2@ checks whether an appliation
375 (tc1 t1..tn) matches (tc2 t1..tn). By "matches" we basically mean "equals",
376 except that at the kind level tc2 might have more boxity info than tc1.
379 matchesTyCon :: TyCon -- Expected (e.g. arg type of function)
380 -> TyCon -- Inferred (e.g. type of actual arg to function)
383 matchesTyCon tc1 tc2 = uniq1 == uniq2 || uniq1 == anyBoxConKey
385 uniq1 = tyConUnique tc1
386 uniq2 = tyConUnique tc2