2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
4 \section[TyCon]{The @TyCon@ datatype}
7 #include "HsVersions.h"
10 TyCon(..), -- NB: some pals need to see representation
12 SYN_IE(Arity), NewOrData(..),
14 isFunTyCon, isPrimTyCon, isBoxedTyCon,
15 isAlgTyCon, isDataTyCon, isSynTyCon, isNewTyCon, maybeNewTyCon,
37 isEnumerationTyCon, isTupleTyCon,
41 CHK_Ubiq() -- debugging consistency check
43 IMPORT_DELOOPER(TyLoop) ( SYN_IE(Type), GenType,
44 SYN_IE(Class), GenClass,
46 splitSigmaTy, splitFunTy,
47 tupleCon, isNullaryDataCon, idType
48 --LATER: specMaybeTysSuffix
51 import BasicTypes ( SYN_IE(Arity), NewOrData(..) )
52 import TyVar ( GenTyVar, alphaTyVars, alphaTyVar, betaTyVar, SYN_IE(TyVar) )
53 import Usage ( GenUsage, SYN_IE(Usage) )
54 import Kind ( Kind, mkBoxedTypeKind, mkArrowKind, resultKind, argKind )
57 import Name ( Name, nameUnique, mkWiredInTyConName, NamedThing(getName) )
58 import Unique ( Unique, funTyConKey )
59 import UniqFM ( Uniquable(..) )
61 import PrimRep ( PrimRep(..) )
62 import PrelMods ( gHC__, pREL_TUP, pREL_BASE )
63 import Lex ( mkTupNameStr )
64 import SrcLoc ( SrcLoc, mkBuiltinSrcLoc )
65 import Util ( nOfThem, isIn, Ord3(..), panic, panic#, assertPanic )
70 = FunTyCon -- Kind = Type -> Type -> Type
72 | DataTyCon Unique{-TyConKey-}
76 [(Class,Type)] -- Its context
77 [Id{-DataCon-}] -- Its data constructors, with fully polymorphic types
78 -- This list can be empty, when we import a data type abstractly,
79 -- either (a) the interface is hand-written and doesn't give
80 -- the constructors, or
81 -- (b) in a quest for fast compilation we don't import
83 [Class] -- Classes which have derived instances
86 | TupleTyCon Unique -- cached
87 Name -- again, we could do without this, but
88 -- it makes life somewhat easier
89 Arity -- just a special case of DataTyCon
90 -- Kind = BoxedTypeKind
91 -- -> ... (n times) ...
95 | PrimTyCon -- Primitive types; cannot be defined in Haskell
96 Unique -- Always unboxed; hence never represented by a closure
97 Name -- Often represented by a bit-pattern for the thing
98 Kind -- itself (eg Int#), but sometimes by a pointer to
101 | SpecTyCon -- A specialised TyCon; eg (Arr# Int#), or (List Int#)
103 [Maybe Type] -- Specialising types
105 -- OLD STUFF ABOUT Array types. Use SpecTyCon instead
106 -- ([PrimRep] -> PrimRep) -- a heap-allocated object (eg ArrInt#).
107 -- The primitive types Arr# and StablePtr# have
108 -- parameters (hence arity /= 0); but the rest don't.
109 -- Only arrays use the list in a non-trivial way.
110 -- Length of that list must == arity.
117 [TyVar] -- Argument type variables
118 Type -- Right-hand side, mentioning these type vars.
119 -- Acts as a template for the expansion when
120 -- the tycon is applied to some types.
124 mkFunTyCon = FunTyCon
125 mkFunTyConName = mkWiredInTyConName funTyConKey gHC__ SLIT("->") FunTyCon
127 mkSpecTyCon = SpecTyCon
128 mkTupleTyCon = TupleTyCon
130 mkDataTyCon name = DataTyCon (nameUnique name) name
131 mkPrimTyCon name = PrimTyCon (nameUnique name) name
132 mkSynTyCon name = SynTyCon (nameUnique name) name
134 isFunTyCon FunTyCon = True
137 isPrimTyCon (PrimTyCon _ _ _ _) = True
138 isPrimTyCon _ = False
140 -- At present there are no unboxed non-primitive types, so
141 -- isBoxedTyCon is just the negation of isPrimTyCon.
142 isBoxedTyCon = not . isPrimTyCon
144 -- isAlgTyCon returns True for both @data@ and @newtype@
145 isAlgTyCon (DataTyCon _ _ _ _ _ _ _ _) = True
146 isAlgTyCon (TupleTyCon _ _ _) = True
147 isAlgTyCon other = False
149 -- isDataTyCon returns False for @newtype@.
150 isDataTyCon (DataTyCon _ _ _ _ _ _ _ DataType) = True
151 isDataTyCon (TupleTyCon _ _ _) = True
152 isDataTyCon other = False
154 maybeNewTyCon :: TyCon -> Maybe ([TyVar], Type) -- Returns representation type info
155 maybeNewTyCon (DataTyCon _ _ _ _ _ (con:null_cons) _ NewType)
156 = ASSERT( null null_cons && null null_tys)
157 Just (tyvars, rep_ty)
159 (tyvars, theta, tau) = splitSigmaTy (idType con)
160 (rep_ty:null_tys, res_ty) = splitFunTy tau
162 maybeNewTyCon other = Nothing
164 isNewTyCon (DataTyCon _ _ _ _ _ _ _ NewType) = True
165 isNewTyCon other = False
167 isSynTyCon (SynTyCon _ _ _ _ _ _) = True
172 -- Special cases to avoid reconstructing lots of kinds
173 kind1 = mkBoxedTypeKind `mkArrowKind` mkBoxedTypeKind
174 kind2 = mkBoxedTypeKind `mkArrowKind` kind1
176 tyConKind :: TyCon -> Kind
177 tyConKind FunTyCon = kind2
178 tyConKind (DataTyCon _ _ kind _ _ _ _ _) = kind
179 tyConKind (PrimTyCon _ _ kind _) = kind
180 tyConKind (SynTyCon _ _ k _ _ _) = k
182 tyConKind (TupleTyCon _ _ n)
185 mkArrow 0 = mkBoxedTypeKind
188 mkArrow n = mkBoxedTypeKind `mkArrowKind` mkArrow (n-1)
190 tyConKind (SpecTyCon tc tys)
191 = spec (tyConKind tc) tys
194 spec kind (Just _ : tys) = spec (resultKind kind) tys
195 spec kind (Nothing : tys) =
196 argKind kind `mkArrowKind` spec (resultKind kind) tys
200 tyConUnique :: TyCon -> Unique
201 tyConUnique FunTyCon = funTyConKey
202 tyConUnique (DataTyCon uniq _ _ _ _ _ _ _) = uniq
203 tyConUnique (TupleTyCon uniq _ _) = uniq
204 tyConUnique (PrimTyCon uniq _ _ _) = uniq
205 tyConUnique (SynTyCon uniq _ _ _ _ _) = uniq
206 tyConUnique (SpecTyCon _ _ ) = panic "tyConUnique:SpecTyCon"
208 synTyConArity :: TyCon -> Maybe Arity -- Nothing <=> not a syn tycon
209 synTyConArity (SynTyCon _ _ _ arity _ _) = Just arity
210 synTyConArity _ = Nothing
214 tyConTyVars :: TyCon -> [TyVar]
215 tyConTyVars FunTyCon = [alphaTyVar,betaTyVar]
216 tyConTyVars (DataTyCon _ _ _ tvs _ _ _ _) = tvs
217 tyConTyVars (TupleTyCon _ _ arity) = take arity alphaTyVars
218 tyConTyVars (SynTyCon _ _ _ _ tvs _) = tvs
220 tyConTyVars (PrimTyCon _ _ _ _) = panic "tyConTyVars:PrimTyCon"
221 tyConTyVars (SpecTyCon _ _ ) = panic "tyConTyVars:SpecTyCon"
226 tyConDataCons :: TyCon -> [Id]
227 tyConFamilySize :: TyCon -> Int
229 tyConDataCons (DataTyCon _ _ _ _ _ data_cons _ _) = data_cons
230 tyConDataCons (TupleTyCon _ _ a) = [tupleCon a]
231 tyConDataCons other = []
232 -- You may think this last equation should fail,
233 -- but it's quite convenient to return no constructors for
234 -- a synonym; see for example the call in TcTyClsDecls.
236 tyConFamilySize (DataTyCon _ _ _ _ _ data_cons _ _) = length data_cons
237 tyConFamilySize (TupleTyCon _ _ _) = 1
239 --tyConFamilySize other = pprPanic "tyConFamilySize:" (pprTyCon PprDebug other)
242 tyConPrimRep :: TyCon -> PrimRep
243 tyConPrimRep (PrimTyCon _ _ _ rep) = rep
244 tyConPrimRep _ = PtrRep
248 tyConDerivings :: TyCon -> [Class]
249 tyConDerivings (DataTyCon _ _ _ _ _ _ derivs _) = derivs
250 tyConDerivings other = []
254 tyConTheta :: TyCon -> [(Class,Type)]
255 tyConTheta (DataTyCon _ _ _ _ theta _ _ _) = theta
256 tyConTheta (TupleTyCon _ _ _) = []
257 -- should ask about anything else
261 getSynTyConDefn :: TyCon -> ([TyVar], Type)
262 getSynTyConDefn (SynTyCon _ _ _ _ tyvars ty) = (tyvars,ty)
266 maybeTyConSingleCon :: TyCon -> Maybe Id
268 maybeTyConSingleCon (TupleTyCon _ _ arity) = Just (tupleCon arity)
269 maybeTyConSingleCon (DataTyCon _ _ _ _ _ [c] _ _) = Just c
270 maybeTyConSingleCon (DataTyCon _ _ _ _ _ _ _ _) = Nothing
271 maybeTyConSingleCon (PrimTyCon _ _ _ _) = Nothing
272 maybeTyConSingleCon (SpecTyCon tc tys) = panic "maybeTyConSingleCon:SpecTyCon"
273 -- requires DataCons of TyCon
275 isEnumerationTyCon (TupleTyCon _ _ arity)
277 isEnumerationTyCon (DataTyCon _ _ _ _ _ data_cons _ _)
278 = not (null data_cons) && all isNullaryDataCon data_cons
281 isTupleTyCon (TupleTyCon _ _ arity) = arity >= 2 -- treat "0-tuple" specially
282 isTupleTyCon (SpecTyCon tc tys) = isTupleTyCon tc
283 isTupleTyCon other = False
288 @derivedFor@ reports if we have an {\em obviously}-derived instance
289 for the given class/tycon. Of course, you might be deriving something
290 because it a superclass of some other obviously-derived class --- this
291 function doesn't deal with that.
293 ToDo: what about derivings for specialised tycons !!!
296 derivedClasses :: TyCon -> [Class]
297 derivedClasses (DataTyCon _ _ _ _ _ _ derivs _) = derivs
298 derivedClasses something_weird = []
301 %************************************************************************
303 \subsection[TyCon-instances]{Instance declarations for @TyCon@}
305 %************************************************************************
307 @TyCon@s are compared by comparing their @Unique@s.
309 The strictness analyser needs @Ord@. It is a lexicographic order with
310 the property @(a<=b) || (b<=a)@.
313 instance Ord3 TyCon where
314 cmp tc1 tc2 = uniqueOf tc1 `cmp` uniqueOf tc2
316 instance Eq TyCon where
317 a == b = case (a `cmp` b) of { EQ_ -> True; _ -> False }
318 a /= b = case (a `cmp` b) of { EQ_ -> False; _ -> True }
320 instance Ord TyCon where
321 a <= b = case (a `cmp` b) of { LT_ -> True; EQ_ -> True; GT__ -> False }
322 a < b = case (a `cmp` b) of { LT_ -> True; EQ_ -> False; GT__ -> False }
323 a >= b = case (a `cmp` b) of { LT_ -> False; EQ_ -> True; GT__ -> True }
324 a > b = case (a `cmp` b) of { LT_ -> False; EQ_ -> False; GT__ -> True }
325 _tagCmp a b = case (a `cmp` b) of { LT_ -> _LT; EQ_ -> _EQ; GT__ -> _GT }
327 instance Uniquable TyCon where
328 uniqueOf (DataTyCon u _ _ _ _ _ _ _) = u
329 uniqueOf (TupleTyCon u _ _) = u
330 uniqueOf (PrimTyCon u _ _ _) = u
331 uniqueOf (SynTyCon u _ _ _ _ _) = u
332 uniqueOf tc@(SpecTyCon _ _) = panic "uniqueOf:SpecTyCon"
333 uniqueOf tc = uniqueOf (getName tc)
337 instance NamedThing TyCon where
338 getName (DataTyCon _ n _ _ _ _ _ _) = n
339 getName (PrimTyCon _ n _ _) = n
340 getName (SpecTyCon tc _) = getName tc
341 getName (SynTyCon _ n _ _ _ _) = n
342 getName FunTyCon = mkFunTyConName
343 getName (TupleTyCon _ n _) = n
344 getName tc = panic "TyCon.getName"
347 getName (SpecTyCon tc tys) = let (OrigName m n) = origName "????" tc in
348 (m, n _APPEND_ specMaybeTysSuffix tys)
349 getName other_tc = moduleNamePair (expectJust "tycon1" (getName other_tc))
350 getName other = Nothing