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 )
57 import Name ( Name, nameUnique, NamedThing(getName) )
58 import Unique ( Unique, Uniquable(..), anyBoxConKey )
59 import PrimRep ( PrimRep(..), isFollowableRep )
63 %************************************************************************
65 \subsection{The data type}
67 %************************************************************************
71 type SuperKindCon = TyCon
75 tyConUnique :: Unique,
82 | AlgTyCon { -- Tuples, data type, and newtype decls.
83 -- All lifted, all boxed
84 tyConUnique :: Unique,
89 tyConTyVars :: [TyVar],
90 tyConArgVrcs :: ArgVrcs,
91 algTyConTheta :: ClassContext,
93 dataCons :: [DataCon],
94 -- Its data constructors, with fully polymorphic types
95 -- This list can be empty, when we import a data type abstractly,
96 -- either (a) the interface is hand-written and doesn't give
97 -- the constructors, or
98 -- (b) in a quest for fast compilation we don't import
101 noOfDataCons :: Int, -- Number of data constructors
102 -- Usually this is the same as the length of the
103 -- dataCons field, but the latter may be empty if
104 -- we imported the type abstractly. But even if we import
105 -- abstractly we still need to know the number of constructors
106 -- so we can get the return convention right. Tiresome!
108 algTyConDerivings :: [Class], -- Classes which have derived instances
110 algTyConFlavour :: AlgTyConFlavour,
111 algTyConRec :: RecFlag, -- Tells whether the data type is part of
112 -- a mutually-recursive group or not
114 algTyConClass_maybe :: Maybe Class -- Nothing for ordinary types;
115 -- Just c for the type constructor
116 -- for dictionaries of class c.
120 | PrimTyCon { -- Primitive types; cannot be defined in Haskell
121 -- NB: All of these guys are *unlifted*, but not all are *unboxed*
122 tyConUnique :: Unique,
126 tyConArgVrcs :: ArgVrcs,
127 primTyConRep :: PrimRep
132 tyConUnique :: Unique,
136 tyConBoxed :: Boxity,
137 tyConTyVars :: [TyVar],
142 tyConUnique :: Unique,
147 tyConTyVars :: [TyVar], -- Bound tyvars
148 synTyConDefn :: Type, -- Right-hand side, mentioning these type vars.
149 -- Acts as a template for the expansion when
150 -- the tycon is applied to some types.
151 tyConArgVrcs :: ArgVrcs
154 | KindCon { -- Type constructor at the kind level
155 tyConUnique :: Unique,
157 tyConKind :: SuperKind,
161 | SuperKindCon { -- The type of kind variables or boxity variables,
162 tyConUnique :: Unique,
166 type ArgVrcs = [(Bool,Bool)] -- Tyvar variance info: [(occPos,occNeg)]
167 -- *NB*: this is tyvar variance info, *not*
168 -- termvar usage info.
171 = DataTyCon -- Data type
172 | EnumTyCon -- Special sort of enumeration type
173 | NewTyCon Type -- Newtype, with its *ultimate* representation type
174 -- By 'ultimate' I mean that the rep type is not itself
175 -- a newtype or type synonym.
177 -- The rep type has explicit for-alls for the tyvars of
179 -- newtype T a = MkT [(a,Int)]
180 -- The rep type is forall a. [(a,Int)]
182 -- The rep type isn't entirely simple:
183 -- for a recursive newtype we pick () as the rep type
187 %************************************************************************
189 \subsection{TyCon Construction}
191 %************************************************************************
193 Note: the TyCon constructors all take a Kind as one argument, even though
194 they could, in principle, work out their Kind from their other arguments.
195 But to do so they need functions from Types, and that makes a nasty
196 module mutual-recursion. And they aren't called from many places.
197 So we compromise, and move their Kind calculation to the call site.
200 mkSuperKindCon :: Name -> SuperKindCon
201 mkSuperKindCon name = SuperKindCon {
202 tyConUnique = nameUnique name,
206 mkKindCon :: Name -> SuperKind -> KindCon
209 tyConUnique = nameUnique name,
215 mkFunTyCon :: Name -> Kind -> TyCon
218 tyConUnique = nameUnique name,
224 mkAlgTyCon name kind tyvars theta argvrcs cons ncons derivs flavour rec
227 tyConUnique = nameUnique name,
229 tyConArity = length tyvars,
230 tyConTyVars = tyvars,
231 tyConArgVrcs = argvrcs,
232 algTyConTheta = theta,
234 noOfDataCons = ncons,
235 algTyConDerivings = derivs,
236 algTyConClass_maybe = Nothing,
237 algTyConFlavour = flavour,
241 mkClassTyCon name kind tyvars argvrcs con clas flavour
244 tyConUnique = nameUnique name,
246 tyConArity = length tyvars,
247 tyConTyVars = tyvars,
248 tyConArgVrcs = argvrcs,
252 algTyConDerivings = [],
253 algTyConClass_maybe = Just clas,
254 algTyConFlavour = flavour,
255 algTyConRec = NonRecursive
259 mkTupleTyCon name kind arity tyvars con boxed
261 tyConUnique = nameUnique name,
266 tyConTyVars = tyvars,
270 mkPrimTyCon name kind arity arg_vrcs rep
273 tyConUnique = nameUnique name,
276 tyConArgVrcs = arg_vrcs,
280 mkSynTyCon name kind arity tyvars rhs argvrcs
283 tyConUnique = nameUnique name,
286 tyConTyVars = tyvars,
288 tyConArgVrcs = argvrcs
291 setTyConName tc name = tc {tyConName = name, tyConUnique = nameUnique name}
295 isFunTyCon (FunTyCon {}) = True
298 isPrimTyCon (PrimTyCon {}) = True
299 isPrimTyCon _ = False
301 isUnLiftedTyCon (PrimTyCon {}) = True
302 isUnLiftedTyCon (TupleTyCon { tyConBoxed = boxity}) = not (isBoxed boxity)
303 isUnLiftedTyCon _ = False
305 -- isBoxedTyCon should not be applied to SynTyCon, nor KindCon
306 isBoxedTyCon (AlgTyCon {}) = True
307 isBoxedTyCon (FunTyCon {}) = True
308 isBoxedTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
309 isBoxedTyCon (PrimTyCon {primTyConRep = rep}) = isFollowableRep rep
311 -- isAlgTyCon returns True for both @data@ and @newtype@
312 isAlgTyCon (AlgTyCon {}) = True
313 isAlgTyCon (TupleTyCon {}) = True
314 isAlgTyCon other = False
316 -- isDataTyCon returns False for @newtype@ and for unboxed tuples
317 isDataTyCon (AlgTyCon {algTyConFlavour = new_or_data}) = case new_or_data of
320 isDataTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
321 isDataTyCon other = False
323 isNewTyCon (AlgTyCon {algTyConFlavour = NewTyCon _}) = True
324 isNewTyCon other = False
326 newTyConRep (AlgTyCon {algTyConFlavour = NewTyCon rep}) = Just rep
327 newTyConRep other = Nothing
330 -- has *one* constructor,
331 -- is *not* existential
333 -- may be DataType or NewType,
334 -- may be unboxed or not,
335 -- may be recursive or not
336 isProductTyCon (AlgTyCon {dataCons = [data_con]}) = not (isExistentialDataCon data_con)
337 isProductTyCon (TupleTyCon {}) = True
338 isProductTyCon other = False
340 isSynTyCon (SynTyCon {}) = True
343 isEnumerationTyCon (AlgTyCon {algTyConFlavour = EnumTyCon}) = True
344 isEnumerationTyCon other = False
346 -- The unit tycon didn't used to be classed as a tuple tycon
347 -- but I thought that was silly so I've undone it
348 -- If it can't be for some reason, it should be a AlgTyCon
349 isTupleTyCon (TupleTyCon {}) = True
350 isTupleTyCon other = False
352 isUnboxedTupleTyCon (TupleTyCon {tyConBoxed = boxity}) = not (isBoxed boxity)
353 isUnboxedTupleTyCon other = False
355 isBoxedTupleTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
356 isBoxedTupleTyCon other = False
358 tupleTyConBoxity tc = tyConBoxed tc
360 isRecursiveTyCon (AlgTyCon {algTyConRec = Recursive}) = True
361 isRecursiveTyCon other = False
365 tyConDataCons :: TyCon -> [DataCon]
366 tyConDataCons tycon = ASSERT2( not (null cons), ppr tycon ) cons
368 cons = tyConDataConsIfAvailable tycon
370 tyConDataConsIfAvailable (AlgTyCon {dataCons = cons}) = cons -- Empty for abstract types
371 tyConDataConsIfAvailable (TupleTyCon {dataCon = con}) = [con]
372 tyConDataConsIfAvailable other = []
373 -- You may think this last equation should fail,
374 -- but it's quite convenient to return no constructors for
375 -- a synonym; see for example the call in TcTyClsDecls.
377 tyConFamilySize :: TyCon -> Int
378 tyConFamilySize (AlgTyCon {noOfDataCons = n}) = n
379 tyConFamilySize (TupleTyCon {}) = 1
381 tyConFamilySize other = pprPanic "tyConFamilySize:" (ppr other)
384 tyConPrimRep :: TyCon -> PrimRep
385 tyConPrimRep (PrimTyCon {primTyConRep = rep}) = rep
386 tyConPrimRep _ = PtrRep
390 tyConDerivings :: TyCon -> [Class]
391 tyConDerivings (AlgTyCon {algTyConDerivings = derivs}) = derivs
392 tyConDerivings other = []
396 tyConTheta :: TyCon -> ClassContext
397 tyConTheta (AlgTyCon {algTyConTheta = theta}) = theta
398 -- should ask about anything else
401 @tyConArgVrcs_maybe@ gives a list of (occPos,occNeg) flags, one for
402 each tyvar, if available. See @calcAlgTyConArgVrcs@ for how this is
403 actually computed (in another file).
406 tyConArgVrcs_maybe :: TyCon -> Maybe ArgVrcs
408 tyConArgVrcs_maybe (FunTyCon {} ) = Just [(False,True),(True,False)]
409 tyConArgVrcs_maybe (AlgTyCon {tyConArgVrcs = oi}) = Just oi
410 tyConArgVrcs_maybe (PrimTyCon {tyConArgVrcs = oi}) = Just oi
411 tyConArgVrcs_maybe (TupleTyCon {tyConArity = arity }) = Just (replicate arity (True,False))
412 tyConArgVrcs_maybe (SynTyCon {tyConArgVrcs = oi }) = Just oi
413 tyConArgVrcs_maybe _ = Nothing
417 getSynTyConDefn :: TyCon -> ([TyVar], Type)
418 getSynTyConDefn (SynTyCon {tyConTyVars = tyvars, synTyConDefn = ty}) = (tyvars,ty)
422 maybeTyConSingleCon :: TyCon -> Maybe DataCon
423 maybeTyConSingleCon (AlgTyCon {dataCons = [c]}) = Just c
424 maybeTyConSingleCon (AlgTyCon {}) = Nothing
425 maybeTyConSingleCon (TupleTyCon {dataCon = con}) = Just con
426 maybeTyConSingleCon (PrimTyCon {}) = Nothing
427 maybeTyConSingleCon (FunTyCon {}) = Nothing -- case at funty
428 maybeTyConSingleCon tc = pprPanic "maybeTyConSingleCon: unexpected tycon " $
433 tyConClass_maybe :: TyCon -> Maybe Class
434 tyConClass_maybe (AlgTyCon {algTyConClass_maybe = maybe_cls}) = maybe_cls
435 tyConClass_maybe other_tycon = Nothing
439 %************************************************************************
441 \subsection[TyCon-instances]{Instance declarations for @TyCon@}
443 %************************************************************************
445 @TyCon@s are compared by comparing their @Unique@s.
447 The strictness analyser needs @Ord@. It is a lexicographic order with
448 the property @(a<=b) || (b<=a)@.
451 instance Eq TyCon where
452 a == b = case (a `compare` b) of { EQ -> True; _ -> False }
453 a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
455 instance Ord TyCon where
456 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
457 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
458 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
459 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
460 compare a b = getUnique a `compare` getUnique b
462 instance Uniquable TyCon where
463 getUnique tc = tyConUnique tc
465 instance Outputable TyCon where
466 ppr tc = ppr (getName tc)
468 instance NamedThing TyCon where
473 %************************************************************************
475 \subsection{Kind constructors}
477 %************************************************************************
479 @matchesTyCon tc1 tc2@ checks whether an appliation
480 (tc1 t1..tn) matches (tc2 t1..tn). By "matches" we basically mean "equals",
481 except that at the kind level tc2 might have more boxity info than tc1.
484 matchesTyCon :: TyCon -- Expected (e.g. arg type of function)
485 -> TyCon -- Inferred (e.g. type of actual arg to function)
488 matchesTyCon tc1 tc2 = uniq1 == uniq2 || uniq1 == anyBoxConKey
490 uniq1 = tyConUnique tc1
491 uniq2 = tyConUnique tc2