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, isExistentialDataCon )
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
281 -- has one constructor,
282 -- is *not* existential
283 -- is *not* an unboxed tuple
284 -- whether DataType or NewType
285 isProductTyCon (AlgTyCon {dataCons = [data_con], algTyConRec = NonRecursive})
286 = not (isExistentialDataCon data_con)
287 isProductTyCon (TupleTyCon { tyConBoxed = boxed })
289 isProductTyCon other = False
291 isSynTyCon (SynTyCon {}) = True
294 isEnumerationTyCon (AlgTyCon {algTyConFlavour = EnumType}) = True
295 isEnumerationTyCon other = False
297 -- The unit tycon isn't classed as a tuple tycon
298 isTupleTyCon (TupleTyCon {tyConArity = arity, tyConBoxed = True}) = arity >= 2
299 isTupleTyCon other = False
301 isUnboxedTupleTyCon (TupleTyCon {tyConBoxed = False}) = True
302 isUnboxedTupleTyCon other = False
306 tyConDataCons :: TyCon -> [DataCon]
307 tyConDataCons (AlgTyCon {dataCons = cons}) = cons
308 tyConDataCons (TupleTyCon {dataCon = con}) = [con]
309 tyConDataCons other = []
310 -- You may think this last equation should fail,
311 -- but it's quite convenient to return no constructors for
312 -- a synonym; see for example the call in TcTyClsDecls.
314 tyConFamilySize :: TyCon -> Int
315 tyConFamilySize (AlgTyCon {dataCons = cons}) = length cons
316 tyConFamilySize (TupleTyCon {}) = 1
318 tyConFamilySize other = pprPanic "tyConFamilySize:" (ppr other)
321 tyConPrimRep :: TyCon -> PrimRep
322 tyConPrimRep (PrimTyCon {primTyConRep = rep}) = rep
323 tyConPrimRep _ = PtrRep
327 tyConDerivings :: TyCon -> [Class]
328 tyConDerivings (AlgTyCon {dataTyConDerivings = derivs}) = derivs
329 tyConDerivings other = []
333 tyConTheta :: TyCon -> [(Class, [Type])]
334 tyConTheta (AlgTyCon {dataTyConTheta = theta}) = theta
335 -- should ask about anything else
338 @tyConArgVrcs_maybe@ gives a list of (occPos,occNeg) flags, one for
339 each tyvar, if available. See @calcAlgTyConArgVrcs@ for how this is
340 actually computed (in another file).
343 tyConArgVrcs_maybe :: TyCon -> Maybe ArgVrcs
345 tyConArgVrcs_maybe (FunTyCon {} ) = Just [(False,True),(True,False)]
346 tyConArgVrcs_maybe (AlgTyCon {dataTyConArgVrcs = oi}) = Just oi
347 tyConArgVrcs_maybe (PrimTyCon {primTyConArgVrcs = oi}) = Just oi
348 tyConArgVrcs_maybe (TupleTyCon {tyConArity = arity }) = Just (replicate arity (True,False))
349 tyConArgVrcs_maybe (SynTyCon {synTyConArgVrcs = oi }) = Just oi
350 tyConArgVrcs_maybe _ = Nothing
354 getSynTyConDefn :: TyCon -> ([TyVar], Type)
355 getSynTyConDefn (SynTyCon {tyConTyVars = tyvars, synTyConDefn = ty}) = (tyvars,ty)
359 maybeTyConSingleCon :: TyCon -> Maybe DataCon
360 maybeTyConSingleCon (AlgTyCon {dataCons = [c]}) = Just c
361 maybeTyConSingleCon (AlgTyCon {}) = Nothing
362 maybeTyConSingleCon (TupleTyCon {dataCon = con}) = Just con
363 maybeTyConSingleCon (PrimTyCon {}) = Nothing
364 maybeTyConSingleCon (FunTyCon {}) = Nothing -- case at funty
365 maybeTyConSingleCon tc = pprPanic "maybeTyConSingleCon: unexpected tycon " $
370 tyConClass_maybe :: TyCon -> Maybe Class
371 tyConClass_maybe (AlgTyCon {dataTyConClass_maybe = maybe_cls}) = maybe_cls
372 tyConClass_maybe other_tycon = Nothing
376 %************************************************************************
378 \subsection[TyCon-instances]{Instance declarations for @TyCon@}
380 %************************************************************************
382 @TyCon@s are compared by comparing their @Unique@s.
384 The strictness analyser needs @Ord@. It is a lexicographic order with
385 the property @(a<=b) || (b<=a)@.
388 instance Eq TyCon where
389 a == b = case (a `compare` b) of { EQ -> True; _ -> False }
390 a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
392 instance Ord TyCon where
393 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
394 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
395 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
396 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
397 compare a b = getUnique a `compare` getUnique b
399 instance Uniquable TyCon where
400 getUnique tc = tyConUnique tc
402 instance Outputable TyCon where
403 ppr tc = ppr (getName tc)
405 instance NamedThing TyCon where
410 %************************************************************************
412 \subsection{Kind constructors}
414 %************************************************************************
416 @matchesTyCon tc1 tc2@ checks whether an appliation
417 (tc1 t1..tn) matches (tc2 t1..tn). By "matches" we basically mean "equals",
418 except that at the kind level tc2 might have more boxity info than tc1.
421 matchesTyCon :: TyCon -- Expected (e.g. arg type of function)
422 -> TyCon -- Inferred (e.g. type of actual arg to function)
425 matchesTyCon tc1 tc2 = uniq1 == uniq2 || uniq1 == anyBoxConKey
427 uniq1 = tyConUnique tc1
428 uniq2 = tyConUnique tc2