2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[TyCon]{The @TyCon@ datatype}
8 TyCon, KindCon, SuperKindCon, ArgVrcs, AlgTyConFlavour(..),
10 isFunTyCon, isUnLiftedTyCon, isBoxedTyCon, isProductTyCon,
11 isAlgTyCon, isDataTyCon, isSynTyCon, isNewTyCon, isPrimTyCon,
13 isTupleTyCon, isUnboxedTupleTyCon, isBoxedTupleTyCon, tupleTyConBoxity,
14 isRecursiveTyCon, newTyConRep,
31 tyConDataCons, tyConDataConsIfAvailable,
45 #include "HsVersions.h"
47 import {-# SOURCE #-} TypeRep ( Type, Kind, SuperKind )
48 -- Should just be Type(Type), but this fails due to bug present up to
49 -- and including 4.02 involving slurping of hi-boot files. Bug is now fixed.
51 import {-# SOURCE #-} DataCon ( DataCon, isExistentialDataCon )
53 import Class ( Class, ClassContext )
55 import BasicTypes ( Arity, NewOrData(..), RecFlag(..), Boxity(..), isBoxed )
56 import Name ( Name, nameUnique, NamedThing(getName) )
57 import Unique ( Unique, Uniquable(..), anyBoxConKey )
58 import PrimRep ( PrimRep(..), isFollowableRep )
62 %************************************************************************
64 \subsection{The data type}
66 %************************************************************************
70 type SuperKindCon = TyCon
74 tyConUnique :: Unique,
81 | AlgTyCon { -- Tuples, data type, and newtype decls.
82 -- All lifted, all boxed
83 tyConUnique :: Unique,
88 tyConTyVars :: [TyVar],
89 tyConArgVrcs :: ArgVrcs,
90 algTyConTheta :: ClassContext,
92 dataCons :: [DataCon],
93 -- Its data constructors, with fully polymorphic types
94 -- This list can be empty, when we import a data type abstractly,
95 -- either (a) the interface is hand-written and doesn't give
96 -- the constructors, or
97 -- (b) in a quest for fast compilation we don't import
100 noOfDataCons :: Int, -- Number of data constructors
101 -- Usually this is the same as the length of the
102 -- dataCons field, but the latter may be empty if
103 -- we imported the type abstractly. But even if we import
104 -- abstractly we still need to know the number of constructors
105 -- so we can get the return convention right. Tiresome!
107 algTyConDerivings :: [Class], -- Classes which have derived instances
109 algTyConFlavour :: AlgTyConFlavour,
110 algTyConRec :: RecFlag, -- Tells whether the data type is part of
111 -- a mutually-recursive group or not
113 algTyConClass_maybe :: Maybe Class -- Nothing for ordinary types;
114 -- Just c for the type constructor
115 -- for dictionaries of class c.
119 | PrimTyCon { -- Primitive types; cannot be defined in Haskell
120 -- NB: All of these guys are *unlifted*, but not all are *unboxed*
121 tyConUnique :: Unique,
125 tyConArgVrcs :: ArgVrcs,
126 primTyConRep :: PrimRep
131 tyConUnique :: Unique,
135 tyConBoxed :: Boxity,
136 tyConTyVars :: [TyVar],
141 tyConUnique :: Unique,
146 tyConTyVars :: [TyVar], -- Bound tyvars
147 synTyConDefn :: Type, -- Right-hand side, mentioning these type vars.
148 -- Acts as a template for the expansion when
149 -- the tycon is applied to some types.
150 tyConArgVrcs :: ArgVrcs
153 | KindCon { -- Type constructor at the kind level
154 tyConUnique :: Unique,
156 tyConKind :: SuperKind,
160 | SuperKindCon { -- The type of kind variables or boxity variables,
161 tyConUnique :: Unique,
165 type ArgVrcs = [(Bool,Bool)] -- Tyvar variance info: [(occPos,occNeg)]
166 -- *NB*: this is tyvar variance info, *not*
167 -- termvar usage info.
170 = DataTyCon -- Data type
171 | EnumTyCon -- Special sort of enumeration type
172 | NewTyCon Type -- Newtype, with its *ultimate* representation type
173 -- By 'ultimate' I mean that the rep type is not itself
174 -- a newtype or type synonym.
176 -- The rep type has explicit for-alls for the tyvars of
178 -- newtype T a = MkT [(a,Int)]
179 -- The rep type is forall a. [(a,Int)]
181 -- The rep type isn't entirely simple:
182 -- for a recursive newtype we pick () as the rep type
186 %************************************************************************
188 \subsection{TyCon Construction}
190 %************************************************************************
192 Note: the TyCon constructors all take a Kind as one argument, even though
193 they could, in principle, work out their Kind from their other arguments.
194 But to do so they need functions from Types, and that makes a nasty
195 module mutual-recursion. And they aren't called from many places.
196 So we compromise, and move their Kind calculation to the call site.
199 mkSuperKindCon :: Name -> SuperKindCon
200 mkSuperKindCon name = SuperKindCon {
201 tyConUnique = nameUnique name,
205 mkKindCon :: Name -> SuperKind -> KindCon
208 tyConUnique = nameUnique name,
214 mkFunTyCon :: Name -> Kind -> TyCon
217 tyConUnique = nameUnique name,
223 mkAlgTyCon name kind tyvars theta argvrcs cons ncons derivs flavour rec
226 tyConUnique = nameUnique name,
228 tyConArity = length tyvars,
229 tyConTyVars = tyvars,
230 tyConArgVrcs = argvrcs,
231 algTyConTheta = theta,
233 noOfDataCons = ncons,
234 algTyConDerivings = derivs,
235 algTyConClass_maybe = Nothing,
236 algTyConFlavour = flavour,
240 mkClassTyCon name kind tyvars argvrcs con clas flavour
243 tyConUnique = nameUnique name,
245 tyConArity = length tyvars,
246 tyConTyVars = tyvars,
247 tyConArgVrcs = argvrcs,
251 algTyConDerivings = [],
252 algTyConClass_maybe = Just clas,
253 algTyConFlavour = flavour,
254 algTyConRec = NonRecursive
258 mkTupleTyCon name kind arity tyvars con boxed
260 tyConUnique = nameUnique name,
265 tyConTyVars = tyvars,
269 mkPrimTyCon name kind arity arg_vrcs rep
272 tyConUnique = nameUnique name,
275 tyConArgVrcs = arg_vrcs,
279 mkSynTyCon name kind arity tyvars rhs argvrcs
282 tyConUnique = nameUnique name,
285 tyConTyVars = tyvars,
287 tyConArgVrcs = argvrcs
290 setTyConName tc name = tc {tyConName = name, tyConUnique = nameUnique name}
294 isFunTyCon (FunTyCon {}) = True
297 isPrimTyCon (PrimTyCon {}) = True
298 isPrimTyCon _ = False
300 isUnLiftedTyCon (PrimTyCon {}) = True
301 isUnLiftedTyCon (TupleTyCon { tyConBoxed = boxity}) = not (isBoxed boxity)
302 isUnLiftedTyCon _ = False
304 -- isBoxedTyCon should not be applied to SynTyCon, nor KindCon
305 isBoxedTyCon (AlgTyCon {}) = True
306 isBoxedTyCon (FunTyCon {}) = True
307 isBoxedTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
308 isBoxedTyCon (PrimTyCon {primTyConRep = rep}) = isFollowableRep rep
310 -- isAlgTyCon returns True for both @data@ and @newtype@
311 isAlgTyCon (AlgTyCon {}) = True
312 isAlgTyCon (TupleTyCon {}) = True
313 isAlgTyCon other = False
315 -- isDataTyCon returns False for @newtype@ and for unboxed tuples
316 isDataTyCon (AlgTyCon {algTyConFlavour = new_or_data}) = case new_or_data of
319 isDataTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
320 isDataTyCon other = False
322 isNewTyCon (AlgTyCon {algTyConFlavour = NewTyCon _}) = True
323 isNewTyCon other = False
325 newTyConRep (AlgTyCon {algTyConFlavour = NewTyCon rep}) = Just rep
326 newTyConRep other = Nothing
329 -- has *one* constructor,
330 -- is *not* existential
332 -- may be DataType or NewType,
333 -- may be unboxed or not,
334 -- may be recursive or not
335 isProductTyCon (AlgTyCon {dataCons = [data_con]}) = not (isExistentialDataCon data_con)
336 isProductTyCon (TupleTyCon {}) = True
337 isProductTyCon other = False
339 isSynTyCon (SynTyCon {}) = True
342 isEnumerationTyCon (AlgTyCon {algTyConFlavour = EnumTyCon}) = True
343 isEnumerationTyCon other = False
345 -- The unit tycon didn't used to be classed as a tuple tycon
346 -- but I thought that was silly so I've undone it
347 -- If it can't be for some reason, it should be a AlgTyCon
348 isTupleTyCon (TupleTyCon {}) = True
349 isTupleTyCon other = False
351 isUnboxedTupleTyCon (TupleTyCon {tyConBoxed = boxity}) = not (isBoxed boxity)
352 isUnboxedTupleTyCon other = False
354 isBoxedTupleTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
355 isBoxedTupleTyCon other = False
357 tupleTyConBoxity tc = tyConBoxed tc
359 isRecursiveTyCon (AlgTyCon {algTyConRec = Recursive}) = True
360 isRecursiveTyCon other = False
364 tyConDataCons :: TyCon -> [DataCon]
365 tyConDataCons tycon = ASSERT2( not (null cons), ppr tycon ) cons
367 cons = tyConDataConsIfAvailable tycon
369 tyConDataConsIfAvailable (AlgTyCon {dataCons = cons}) = cons -- Empty for abstract types
370 tyConDataConsIfAvailable (TupleTyCon {dataCon = con}) = [con]
371 tyConDataConsIfAvailable other = []
372 -- You may think this last equation should fail,
373 -- but it's quite convenient to return no constructors for
374 -- a synonym; see for example the call in TcTyClsDecls.
376 tyConFamilySize :: TyCon -> Int
377 tyConFamilySize (AlgTyCon {noOfDataCons = n}) = n
378 tyConFamilySize (TupleTyCon {}) = 1
380 tyConFamilySize other = pprPanic "tyConFamilySize:" (ppr other)
383 tyConPrimRep :: TyCon -> PrimRep
384 tyConPrimRep (PrimTyCon {primTyConRep = rep}) = rep
385 tyConPrimRep _ = PtrRep
389 tyConDerivings :: TyCon -> [Class]
390 tyConDerivings (AlgTyCon {algTyConDerivings = derivs}) = derivs
391 tyConDerivings other = []
395 tyConTheta :: TyCon -> ClassContext
396 tyConTheta (AlgTyCon {algTyConTheta = theta}) = theta
397 -- should ask about anything else
400 @tyConArgVrcs_maybe@ gives a list of (occPos,occNeg) flags, one for
401 each tyvar, if available. See @calcAlgTyConArgVrcs@ for how this is
402 actually computed (in another file).
405 tyConArgVrcs_maybe :: TyCon -> Maybe ArgVrcs
407 tyConArgVrcs_maybe (FunTyCon {} ) = Just [(False,True),(True,False)]
408 tyConArgVrcs_maybe (AlgTyCon {tyConArgVrcs = oi}) = Just oi
409 tyConArgVrcs_maybe (PrimTyCon {tyConArgVrcs = oi}) = Just oi
410 tyConArgVrcs_maybe (TupleTyCon {tyConArity = arity }) = Just (replicate arity (True,False))
411 tyConArgVrcs_maybe (SynTyCon {tyConArgVrcs = oi }) = Just oi
412 tyConArgVrcs_maybe _ = Nothing
416 getSynTyConDefn :: TyCon -> ([TyVar], Type)
417 getSynTyConDefn (SynTyCon {tyConTyVars = tyvars, synTyConDefn = ty}) = (tyvars,ty)
421 maybeTyConSingleCon :: TyCon -> Maybe DataCon
422 maybeTyConSingleCon (AlgTyCon {dataCons = [c]}) = Just c
423 maybeTyConSingleCon (AlgTyCon {}) = Nothing
424 maybeTyConSingleCon (TupleTyCon {dataCon = con}) = Just con
425 maybeTyConSingleCon (PrimTyCon {}) = Nothing
426 maybeTyConSingleCon (FunTyCon {}) = Nothing -- case at funty
427 maybeTyConSingleCon tc = pprPanic "maybeTyConSingleCon: unexpected tycon " $
432 tyConClass_maybe :: TyCon -> Maybe Class
433 tyConClass_maybe (AlgTyCon {algTyConClass_maybe = maybe_cls}) = maybe_cls
434 tyConClass_maybe other_tycon = Nothing
438 %************************************************************************
440 \subsection[TyCon-instances]{Instance declarations for @TyCon@}
442 %************************************************************************
444 @TyCon@s are compared by comparing their @Unique@s.
446 The strictness analyser needs @Ord@. It is a lexicographic order with
447 the property @(a<=b) || (b<=a)@.
450 instance Eq TyCon where
451 a == b = case (a `compare` b) of { EQ -> True; _ -> False }
452 a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
454 instance Ord TyCon where
455 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
456 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
457 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
458 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
459 compare a b = getUnique a `compare` getUnique b
461 instance Uniquable TyCon where
462 getUnique tc = tyConUnique tc
464 instance Outputable TyCon where
465 ppr tc = ppr (getName tc)
467 instance NamedThing TyCon where
472 %************************************************************************
474 \subsection{Kind constructors}
476 %************************************************************************
478 @matchesTyCon tc1 tc2@ checks whether an appliation
479 (tc1 t1..tn) matches (tc2 t1..tn). By "matches" we basically mean "equals",
480 except that at the kind level tc2 might have more boxity info than tc1.
483 matchesTyCon :: TyCon -- Expected (e.g. arg type of function)
484 -> TyCon -- Inferred (e.g. type of actual arg to function)
487 matchesTyCon tc1 tc2 = uniq1 == uniq2 || uniq1 == anyBoxConKey
489 uniq1 = tyConUnique tc1
490 uniq2 = tyConUnique tc2