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,
43 #include "HsVersions.h"
45 import {-# SOURCE #-} TypeRep ( Type, Kind, SuperKind )
46 -- Should just be Type(Type), but this fails due to bug present up to
47 -- and including 4.02 involving slurping of hi-boot files. Bug is now fixed.
49 import {-# SOURCE #-} DataCon ( DataCon, isExistentialDataCon )
51 import Class ( Class )
53 import BasicTypes ( Arity, NewOrData(..), RecFlag(..) )
55 import Name ( Name, nameUnique, NamedThing(getName) )
56 import Unique ( Unique, Uniquable(..), anyBoxConKey )
57 import PrimRep ( PrimRep(..), isFollowableRep )
61 %************************************************************************
63 \subsection{The data type}
65 %************************************************************************
69 type SuperKindCon = TyCon
73 tyConUnique :: Unique,
80 | AlgTyCon { -- Tuples, data type, and newtype decls.
81 -- All lifted, all boxed
82 tyConUnique :: Unique,
87 tyConTyVars :: [TyVar],
88 dataTyConTheta :: [(Class,[Type])],
89 dataTyConArgVrcs :: ArgVrcs,
91 dataCons :: [DataCon],
92 -- Its data constructors, with fully polymorphic types
93 -- This list can be empty, when we import a data type abstractly,
94 -- either (a) the interface is hand-written and doesn't give
95 -- the constructors, or
96 -- (b) in a quest for fast compilation we don't import
99 dataTyConDerivings :: [Class], -- Classes which have derived instances
101 dataTyConClass_maybe :: (Maybe Class), -- Nothing for ordinary types;
102 -- Just c for the type constructor
103 -- for dictionaries of class c.
104 algTyConFlavour :: NewOrData,
105 algTyConRec :: RecFlag -- Tells whether the data type is part of
106 -- a mutually-recursive group or not
109 | PrimTyCon { -- Primitive types; cannot be defined in Haskell
110 -- NB: All of these guys are *unlifted*, but not all are *unboxed*
111 tyConUnique :: Unique,
115 primTyConArgVrcs :: ArgVrcs,
116 primTyConRep :: PrimRep
121 tyConUnique :: Unique,
125 tyConBoxed :: Bool, -- True for boxed; False for unboxed
126 tyConTyVars :: [TyVar],
131 tyConUnique :: Unique,
136 tyConTyVars :: [TyVar], -- Bound tyvars
137 synTyConDefn :: Type, -- Right-hand side, mentioning these type vars.
138 -- Acts as a template for the expansion when
139 -- the tycon is applied to some types.
140 synTyConArgVrcs :: ArgVrcs
143 | KindCon { -- Type constructor at the kind level
144 tyConUnique :: Unique,
146 tyConKind :: SuperKind,
150 | SuperKindCon { -- The type of kind variables or boxity variables,
151 tyConUnique :: Unique,
155 type ArgVrcs = [(Bool,Bool)] -- Tyvar variance info: [(occPos,occNeg)]
156 -- *NB*: this is tyvar variance info, *not*
157 -- termvar usage info.
160 %************************************************************************
162 \subsection{TyCon Construction}
164 %************************************************************************
166 Note: the TyCon constructors all take a Kind as one argument, even though
167 they could, in principle, work out their Kind from their other arguments.
168 But to do so they need functions from Types, and that makes a nasty
169 module mutual-recursion. And they aren't called from many places.
170 So we compromise, and move their Kind calculation to the call site.
173 mkSuperKindCon :: Name -> SuperKindCon
174 mkSuperKindCon name = SuperKindCon {
175 tyConUnique = nameUnique name,
179 mkKindCon :: Name -> SuperKind -> KindCon
182 tyConUnique = nameUnique name,
188 mkFunTyCon :: Name -> Kind -> TyCon
191 tyConUnique = nameUnique name,
197 mkAlgTyCon name kind tyvars theta argvrcs cons derivs maybe_clas flavour rec
200 tyConUnique = nameUnique name,
202 tyConArity = length tyvars,
203 tyConTyVars = tyvars,
204 dataTyConTheta = theta,
205 dataTyConArgVrcs = argvrcs,
207 dataTyConDerivings = derivs,
208 dataTyConClass_maybe = maybe_clas,
209 algTyConFlavour = flavour,
213 mkTupleTyCon name kind arity tyvars con boxed
215 tyConUnique = nameUnique name,
220 tyConTyVars = tyvars,
224 mkPrimTyCon name kind arity arg_vrcs rep
227 tyConUnique = nameUnique name,
230 primTyConArgVrcs = arg_vrcs,
234 mkSynTyCon name kind arity tyvars rhs argvrcs
237 tyConUnique = nameUnique name,
240 tyConTyVars = tyvars,
242 synTyConArgVrcs = argvrcs
245 setTyConName tc name = tc {tyConName = name, tyConUnique = nameUnique name}
249 isFunTyCon (FunTyCon {}) = True
252 isPrimTyCon (PrimTyCon {}) = True
253 isPrimTyCon _ = False
255 isUnLiftedTyCon (PrimTyCon {}) = True
256 isUnLiftedTyCon (TupleTyCon { tyConBoxed = False }) = True
257 isUnLiftedTyCon _ = False
259 -- isBoxedTyCon should not be applied to SynTyCon, nor KindCon
260 isBoxedTyCon (AlgTyCon {}) = True
261 isBoxedTyCon (FunTyCon {}) = True
262 isBoxedTyCon (TupleTyCon {tyConBoxed = boxed}) = boxed
263 isBoxedTyCon (PrimTyCon {primTyConRep = rep}) = isFollowableRep rep
265 -- isAlgTyCon returns True for both @data@ and @newtype@
266 isAlgTyCon (AlgTyCon {}) = True
267 isAlgTyCon (TupleTyCon {}) = True
268 isAlgTyCon other = False
270 -- isDataTyCon returns False for @newtype@ and for unboxed tuples
271 isDataTyCon (AlgTyCon {algTyConFlavour = new_or_data}) = case new_or_data of
274 isDataTyCon (TupleTyCon {tyConBoxed = True}) = True
275 isDataTyCon other = False
277 isNewTyCon (AlgTyCon {algTyConFlavour = NewType}) = True
278 isNewTyCon other = False
281 -- has *one* constructor,
282 -- is *not* existential
284 -- may be DataType or NewType,
285 -- may be unboxed or not,
286 -- may be recursive or not
287 isProductTyCon (AlgTyCon {dataCons = [data_con]}) = not (isExistentialDataCon data_con)
288 isProductTyCon (TupleTyCon {}) = True
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
304 isRecursiveTyCon (AlgTyCon {algTyConRec = Recursive}) = True
305 isRecursiveTyCon other = False
309 tyConDataCons :: TyCon -> [DataCon]
310 tyConDataCons (AlgTyCon {dataCons = cons}) = cons
311 tyConDataCons (TupleTyCon {dataCon = con}) = [con]
312 tyConDataCons other = []
313 -- You may think this last equation should fail,
314 -- but it's quite convenient to return no constructors for
315 -- a synonym; see for example the call in TcTyClsDecls.
317 tyConFamilySize :: TyCon -> Int
318 tyConFamilySize (AlgTyCon {dataCons = cons}) = length cons
319 tyConFamilySize (TupleTyCon {}) = 1
321 tyConFamilySize other = pprPanic "tyConFamilySize:" (ppr other)
324 tyConPrimRep :: TyCon -> PrimRep
325 tyConPrimRep (PrimTyCon {primTyConRep = rep}) = rep
326 tyConPrimRep _ = PtrRep
330 tyConDerivings :: TyCon -> [Class]
331 tyConDerivings (AlgTyCon {dataTyConDerivings = derivs}) = derivs
332 tyConDerivings other = []
336 tyConTheta :: TyCon -> [(Class, [Type])]
337 tyConTheta (AlgTyCon {dataTyConTheta = theta}) = theta
338 -- should ask about anything else
341 @tyConArgVrcs_maybe@ gives a list of (occPos,occNeg) flags, one for
342 each tyvar, if available. See @calcAlgTyConArgVrcs@ for how this is
343 actually computed (in another file).
346 tyConArgVrcs_maybe :: TyCon -> Maybe ArgVrcs
348 tyConArgVrcs_maybe (FunTyCon {} ) = Just [(False,True),(True,False)]
349 tyConArgVrcs_maybe (AlgTyCon {dataTyConArgVrcs = oi}) = Just oi
350 tyConArgVrcs_maybe (PrimTyCon {primTyConArgVrcs = oi}) = Just oi
351 tyConArgVrcs_maybe (TupleTyCon {tyConArity = arity }) = Just (replicate arity (True,False))
352 tyConArgVrcs_maybe (SynTyCon {synTyConArgVrcs = oi }) = Just oi
353 tyConArgVrcs_maybe _ = Nothing
357 getSynTyConDefn :: TyCon -> ([TyVar], Type)
358 getSynTyConDefn (SynTyCon {tyConTyVars = tyvars, synTyConDefn = ty}) = (tyvars,ty)
362 maybeTyConSingleCon :: TyCon -> Maybe DataCon
363 maybeTyConSingleCon (AlgTyCon {dataCons = [c]}) = Just c
364 maybeTyConSingleCon (AlgTyCon {}) = Nothing
365 maybeTyConSingleCon (TupleTyCon {dataCon = con}) = Just con
366 maybeTyConSingleCon (PrimTyCon {}) = Nothing
367 maybeTyConSingleCon (FunTyCon {}) = Nothing -- case at funty
368 maybeTyConSingleCon tc = pprPanic "maybeTyConSingleCon: unexpected tycon " $
373 tyConClass_maybe :: TyCon -> Maybe Class
374 tyConClass_maybe (AlgTyCon {dataTyConClass_maybe = maybe_cls}) = maybe_cls
375 tyConClass_maybe other_tycon = Nothing
379 %************************************************************************
381 \subsection[TyCon-instances]{Instance declarations for @TyCon@}
383 %************************************************************************
385 @TyCon@s are compared by comparing their @Unique@s.
387 The strictness analyser needs @Ord@. It is a lexicographic order with
388 the property @(a<=b) || (b<=a)@.
391 instance Eq TyCon where
392 a == b = case (a `compare` b) of { EQ -> True; _ -> False }
393 a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
395 instance Ord TyCon where
396 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
397 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
398 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
399 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
400 compare a b = getUnique a `compare` getUnique b
402 instance Uniquable TyCon where
403 getUnique tc = tyConUnique tc
405 instance Outputable TyCon where
406 ppr tc = ppr (getName tc)
408 instance NamedThing TyCon where
413 %************************************************************************
415 \subsection{Kind constructors}
417 %************************************************************************
419 @matchesTyCon tc1 tc2@ checks whether an appliation
420 (tc1 t1..tn) matches (tc2 t1..tn). By "matches" we basically mean "equals",
421 except that at the kind level tc2 might have more boxity info than tc1.
424 matchesTyCon :: TyCon -- Expected (e.g. arg type of function)
425 -> TyCon -- Inferred (e.g. type of actual arg to function)
428 matchesTyCon tc1 tc2 = uniq1 == uniq2 || uniq1 == anyBoxConKey
430 uniq1 = tyConUnique tc1
431 uniq2 = tyConUnique tc2