2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
4 \section[TyCon]{The @TyCon@ datatype}
12 isFunTyCon, isPrimTyCon, isBoxedTyCon, isProductTyCon,
13 isAlgTyCon, isDataTyCon, isSynTyCon, isNewTyCon,
14 isEnumerationTyCon, isTupleTyCon,
39 #include "HsVersions.h"
41 import {-# SOURCE #-} Type ( Type )
42 import {-# SOURCE #-} Class ( Class )
43 import {-# SOURCE #-} Id ( Id, isNullaryDataCon, idType )
44 import {-# SOURCE #-} TysWiredIn ( tupleCon )
47 import BasicTypes ( Arity, NewOrData(..), RecFlag(..) )
48 import TyVar ( GenTyVar, alphaTyVars, alphaTyVar, betaTyVar, TyVar )
49 import Kind ( Kind, mkBoxedTypeKind, mkTypeKind, mkUnboxedTypeKind,
50 mkArrowKind, resultKind, argKind
53 import Name ( Name, nameUnique, mkWiredInTyConName, NamedThing(getName) )
54 import Unique ( Unique, funTyConKey, Uniquable(..) )
55 import PrimRep ( PrimRep(..), isFollowableRep )
56 import PrelMods ( gHC__, pREL_TUP, pREL_BASE )
57 import Lex ( mkTupNameStr )
58 import SrcLoc ( SrcLoc, mkBuiltinSrcLoc )
59 import Util ( nOfThem, isIn )
65 = FunTyCon -- Kind = Type -> Type -> Type
71 [(Class,[Type])] -- Its context
72 [Id{-DataCon-}] -- Its data constructors, with fully polymorphic types
73 -- This list can be empty, when we import a data type abstractly,
74 -- either (a) the interface is hand-written and doesn't give
75 -- the constructors, or
76 -- (b) in a quest for fast compilation we don't import
78 [Class] -- Classes which have derived instances
79 (Maybe Class) -- Nothing for ordinary types; Just c for the type constructor
80 -- for dictionaries of class c.
82 RecFlag -- Tells whether the data type is part of
83 -- a mutually-recursive group or not
85 | TupleTyCon Unique -- cached
86 Name -- again, we could do without this, but
87 -- it makes life somewhat easier
88 Arity -- just a special case of DataTyCon
89 -- Kind = BoxedTypeKind
90 -- -> ... (n times) ...
94 | PrimTyCon -- Primitive types; cannot be defined in Haskell
95 Unique -- Always unpointed; hence never represented by a closure
96 Name -- Often represented by a bit-pattern for the thing
97 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
132 mkPrimTyCon name arity rep
133 = PrimTyCon (nameUnique name) name (mk_kind arity) arity rep
135 mk_kind 0 | isFollowableRep rep = mkBoxedTypeKind -- Represented by a GC-ish ptr
136 | otherwise = mkUnboxedTypeKind -- Represented by a non-ptr
137 mk_kind n = mkTypeKind `mkArrowKind` mk_kind (n-1)
139 mkSynTyCon name = SynTyCon (nameUnique name) name
141 isFunTyCon FunTyCon = True
144 isPrimTyCon (PrimTyCon _ _ _ _ _) = True
145 isPrimTyCon _ = False
147 -- At present there are no unboxed non-primitive types, so
148 -- isBoxedTyCon is just the negation of isPrimTyCon.
149 isBoxedTyCon = not . isPrimTyCon
151 -- isAlgTyCon returns True for both @data@ and @newtype@
152 isAlgTyCon (DataTyCon _ _ _ _ _ _ _ _ _ _) = True
153 isAlgTyCon (TupleTyCon _ _ _) = True
154 isAlgTyCon other = False
156 -- isDataTyCon returns False for @newtype@.
157 isDataTyCon (DataTyCon _ _ _ _ _ _ _ _ DataType _) = True
158 isDataTyCon (TupleTyCon _ _ _) = True
159 isDataTyCon other = False
161 isNewTyCon (DataTyCon _ _ _ _ _ _ _ _ NewType _) = True
162 isNewTyCon other = False
164 -- A "product" tycon is non-recursive and has one constructor,
165 -- whether DataType or NewType
166 isProductTyCon (TupleTyCon _ _ _) = True
167 isProductTyCon (DataTyCon _ _ _ _ _ [c] _ _ _ NonRecursive) = True
168 isProductTyCon other = False
170 isSynTyCon (SynTyCon _ _ _ _ _ _) = True
173 isEnumerationTyCon (TupleTyCon _ _ arity)
175 isEnumerationTyCon (DataTyCon _ _ _ _ _ data_cons _ _ DataType _)
176 = not (null data_cons) && all isNullaryDataCon data_cons
177 isEnumerationTyCon other = False
179 isTupleTyCon (TupleTyCon _ _ arity) = arity >= 2 -- treat "0-tuple" specially
180 isTupleTyCon (SpecTyCon tc tys) = isTupleTyCon tc
181 isTupleTyCon other = False
185 -- Special cases to avoid reconstructing lots of kinds
186 kind1 = mkBoxedTypeKind `mkArrowKind` mkBoxedTypeKind
187 kind2 = mkBoxedTypeKind `mkArrowKind` kind1
189 tyConKind :: TyCon -> Kind
190 tyConKind FunTyCon = kind2
191 tyConKind (DataTyCon _ _ kind _ _ _ _ _ _ _) = kind
192 tyConKind (PrimTyCon _ _ kind _ _) = kind
193 tyConKind (SynTyCon _ _ k _ _ _) = k
195 tyConKind (TupleTyCon _ _ n)
198 mkArrow 0 = mkBoxedTypeKind
201 mkArrow n = mkBoxedTypeKind `mkArrowKind` mkArrow (n-1)
203 tyConKind (SpecTyCon tc tys)
204 = spec (tyConKind tc) tys
207 spec kind (Just _ : tys) = spec (resultKind kind) tys
208 spec kind (Nothing : tys) =
209 argKind kind `mkArrowKind` spec (resultKind kind) tys
213 tyConUnique :: TyCon -> Unique
214 tyConUnique FunTyCon = funTyConKey
215 tyConUnique (DataTyCon uniq _ _ _ _ _ _ _ _ _) = uniq
216 tyConUnique (TupleTyCon uniq _ _) = uniq
217 tyConUnique (PrimTyCon uniq _ _ _ _) = uniq
218 tyConUnique (SynTyCon uniq _ _ _ _ _) = uniq
219 tyConUnique (SpecTyCon _ _ ) = panic "tyConUnique:SpecTyCon"
221 tyConArity :: TyCon -> Arity
222 tyConArity FunTyCon = 2
223 tyConArity (DataTyCon _ _ _ tyvars _ _ _ _ _ _) = length tyvars
224 tyConArity (TupleTyCon _ _ arity) = arity
225 tyConArity (PrimTyCon _ _ _ arity _) = arity
226 tyConArity (SynTyCon _ _ _ arity _ _) = arity
227 tyConArity (SpecTyCon _ _ ) = panic "tyConArity:SpecTyCon"
231 tyConTyVars :: TyCon -> [TyVar]
232 tyConTyVars FunTyCon = [alphaTyVar,betaTyVar]
233 tyConTyVars (DataTyCon _ _ _ tvs _ _ _ _ _ _) = tvs
234 tyConTyVars (TupleTyCon _ _ arity) = take arity alphaTyVars
235 tyConTyVars (SynTyCon _ _ _ _ tvs _) = tvs
237 tyConTyVars (PrimTyCon _ _ _ _ _) = panic "tyConTyVars:PrimTyCon"
238 tyConTyVars (SpecTyCon _ _ ) = panic "tyConTyVars:SpecTyCon"
243 tyConDataCons :: TyCon -> [Id]
244 tyConFamilySize :: TyCon -> Int
246 tyConDataCons (DataTyCon _ _ _ _ _ data_cons _ _ _ _) = data_cons
247 tyConDataCons (TupleTyCon _ _ a) = [tupleCon a]
248 tyConDataCons other = []
249 -- You may think this last equation should fail,
250 -- but it's quite convenient to return no constructors for
251 -- a synonym; see for example the call in TcTyClsDecls.
253 tyConFamilySize (DataTyCon _ _ _ _ _ data_cons _ _ _ _) = length data_cons
254 tyConFamilySize (TupleTyCon _ _ _) = 1
256 --tyConFamilySize other = pprPanic "tyConFamilySize:" (pprTyCon other)
259 tyConPrimRep :: TyCon -> PrimRep
260 tyConPrimRep (PrimTyCon _ __ _ rep) = rep
261 tyConPrimRep _ = PtrRep
265 tyConDerivings :: TyCon -> [Class]
266 tyConDerivings (DataTyCon _ _ _ _ _ _ derivs _ _ _) = derivs
267 tyConDerivings other = []
271 tyConTheta :: TyCon -> [(Class, [Type])]
272 tyConTheta (DataTyCon _ _ _ _ theta _ _ _ _ _) = theta
273 tyConTheta (TupleTyCon _ _ _) = []
274 -- should ask about anything else
278 getSynTyConDefn :: TyCon -> ([TyVar], Type)
279 getSynTyConDefn (SynTyCon _ _ _ _ tyvars ty) = (tyvars,ty)
283 maybeTyConSingleCon :: TyCon -> Maybe Id
285 maybeTyConSingleCon (TupleTyCon _ _ arity) = Just (tupleCon arity)
286 maybeTyConSingleCon (DataTyCon _ _ _ _ _ [c] _ _ _ _) = Just c
287 maybeTyConSingleCon (DataTyCon _ _ _ _ _ _ _ _ _ _) = Nothing
288 maybeTyConSingleCon (PrimTyCon _ _ _ _ _) = Nothing
289 maybeTyConSingleCon (SpecTyCon tc tys) = panic "maybeTyConSingleCon:SpecTyCon"
290 -- requires DataCons of TyCon
294 tyConClass_maybe :: TyCon -> Maybe Class
295 tyConClass_maybe (DataTyCon _ _ _ _ _ _ _ maybe_cls _ _) = maybe_cls
296 tyConClass_maybe other_tycon = Nothing
299 @derivedFor@ reports if we have an {\em obviously}-derived instance
300 for the given class/tycon. Of course, you might be deriving something
301 because it a superclass of some other obviously-derived class --- this
302 function doesn't deal with that.
304 ToDo: what about derivings for specialised tycons !!!
306 %************************************************************************
308 \subsection[TyCon-instances]{Instance declarations for @TyCon@}
310 %************************************************************************
312 @TyCon@s are compared by comparing their @Unique@s.
314 The strictness analyser needs @Ord@. It is a lexicographic order with
315 the property @(a<=b) || (b<=a)@.
318 instance Eq TyCon where
319 a == b = case (a `compare` b) of { EQ -> True; _ -> False }
320 a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
322 instance Ord TyCon where
323 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
324 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
325 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
326 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
327 compare a b = uniqueOf a `compare` uniqueOf b
329 instance Uniquable TyCon where
330 uniqueOf tc = tyConUnique tc
334 instance NamedThing TyCon where
335 getName (DataTyCon _ n _ _ _ _ _ _ _ _) = n
336 getName (PrimTyCon _ n _ _ _) = n
337 getName (SpecTyCon tc _) = getName tc
338 getName (SynTyCon _ n _ _ _ _) = n
339 getName FunTyCon = mkFunTyConName
340 getName (TupleTyCon _ n _) = n
343 getName (SpecTyCon tc tys) = let (OrigName m n) = origName "????" tc in
344 (m, n _APPEND_ specMaybeTysSuffix tys)
345 getName other_tc = moduleNamePair (expectJust "tycon1" (getName other_tc))
346 getName other = Nothing