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 #if defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ <= 201
44 IMPORT_DELOOPER(TyLoop) ( SYN_IE(Type), GenType,
45 SYN_IE(Class), GenClass,
47 splitSigmaTy, splitFunTy,
48 tupleCon, isNullaryDataCon, idType
49 --LATER: specMaybeTysSuffix
52 import {-# SOURCE #-} Type ( Type, splitSigmaTy, splitFunTy )
53 import {-# SOURCE #-} Class ( Class )
54 import {-# SOURCE #-} Id ( Id, isNullaryDataCon, idType )
55 import {-# SOURCE #-} TysWiredIn ( tupleCon )
58 import BasicTypes ( SYN_IE(Arity), NewOrData(..) )
59 import TyVar ( GenTyVar, alphaTyVars, alphaTyVar, betaTyVar, SYN_IE(TyVar) )
60 import Usage ( GenUsage, SYN_IE(Usage) )
61 import Kind ( Kind, mkBoxedTypeKind, mkArrowKind, resultKind, argKind )
64 import Name ( Name, nameUnique, mkWiredInTyConName, NamedThing(getName) )
65 import Unique ( Unique, funTyConKey, Uniquable(..) )
67 import PrimRep ( PrimRep(..) )
68 import PrelMods ( gHC__, pREL_TUP, pREL_BASE )
69 import Lex ( mkTupNameStr )
70 import SrcLoc ( SrcLoc, mkBuiltinSrcLoc )
71 import Util ( nOfThem, isIn, Ord3(..), panic, panic#, assertPanic )
76 = FunTyCon -- Kind = Type -> Type -> Type
78 | DataTyCon Unique{-TyConKey-}
82 [(Class,Type)] -- Its context
83 [Id{-DataCon-}] -- Its data constructors, with fully polymorphic types
84 -- This list can be empty, when we import a data type abstractly,
85 -- either (a) the interface is hand-written and doesn't give
86 -- the constructors, or
87 -- (b) in a quest for fast compilation we don't import
89 [Class] -- Classes which have derived instances
92 | TupleTyCon Unique -- cached
93 Name -- again, we could do without this, but
94 -- it makes life somewhat easier
95 Arity -- just a special case of DataTyCon
96 -- Kind = BoxedTypeKind
97 -- -> ... (n times) ...
101 | PrimTyCon -- Primitive types; cannot be defined in Haskell
102 Unique -- Always unboxed; hence never represented by a closure
103 Name -- Often represented by a bit-pattern for the thing
104 Kind -- itself (eg Int#), but sometimes by a pointer to
107 | SpecTyCon -- A specialised TyCon; eg (Arr# Int#), or (List Int#)
109 [Maybe Type] -- Specialising types
111 -- OLD STUFF ABOUT Array types. Use SpecTyCon instead
112 -- ([PrimRep] -> PrimRep) -- a heap-allocated object (eg ArrInt#).
113 -- The primitive types Arr# and StablePtr# have
114 -- parameters (hence arity /= 0); but the rest don't.
115 -- Only arrays use the list in a non-trivial way.
116 -- Length of that list must == arity.
123 [TyVar] -- Argument type variables
124 Type -- Right-hand side, mentioning these type vars.
125 -- Acts as a template for the expansion when
126 -- the tycon is applied to some types.
130 mkFunTyCon = FunTyCon
131 mkFunTyConName = mkWiredInTyConName funTyConKey gHC__ SLIT("->") FunTyCon
133 mkSpecTyCon = SpecTyCon
134 mkTupleTyCon = TupleTyCon
136 mkDataTyCon name = DataTyCon (nameUnique name) name
137 mkPrimTyCon name = PrimTyCon (nameUnique name) name
138 mkSynTyCon name = SynTyCon (nameUnique name) name
140 isFunTyCon FunTyCon = True
143 isPrimTyCon (PrimTyCon _ _ _ _) = True
144 isPrimTyCon _ = False
146 -- At present there are no unboxed non-primitive types, so
147 -- isBoxedTyCon is just the negation of isPrimTyCon.
148 isBoxedTyCon = not . isPrimTyCon
150 -- isAlgTyCon returns True for both @data@ and @newtype@
151 isAlgTyCon (DataTyCon _ _ _ _ _ _ _ _) = True
152 isAlgTyCon (TupleTyCon _ _ _) = True
153 isAlgTyCon other = False
155 -- isDataTyCon returns False for @newtype@.
156 isDataTyCon (DataTyCon _ _ _ _ _ _ _ DataType) = True
157 isDataTyCon (TupleTyCon _ _ _) = True
158 isDataTyCon other = False
160 maybeNewTyCon :: TyCon -> Maybe ([TyVar], Type) -- Returns representation type info
161 maybeNewTyCon (DataTyCon _ _ _ _ _ (con:null_cons) _ NewType)
162 = ASSERT( null null_cons && null null_tys)
163 Just (tyvars, rep_ty)
165 (tyvars, theta, tau) = splitSigmaTy (idType con)
166 (rep_ty:null_tys, res_ty) = splitFunTy tau
168 maybeNewTyCon other = Nothing
170 isNewTyCon (DataTyCon _ _ _ _ _ _ _ NewType) = True
171 isNewTyCon other = False
173 isSynTyCon (SynTyCon _ _ _ _ _ _) = True
178 -- Special cases to avoid reconstructing lots of kinds
179 kind1 = mkBoxedTypeKind `mkArrowKind` mkBoxedTypeKind
180 kind2 = mkBoxedTypeKind `mkArrowKind` kind1
182 tyConKind :: TyCon -> Kind
183 tyConKind FunTyCon = kind2
184 tyConKind (DataTyCon _ _ kind _ _ _ _ _) = kind
185 tyConKind (PrimTyCon _ _ kind _) = kind
186 tyConKind (SynTyCon _ _ k _ _ _) = k
188 tyConKind (TupleTyCon _ _ n)
191 mkArrow 0 = mkBoxedTypeKind
194 mkArrow n = mkBoxedTypeKind `mkArrowKind` mkArrow (n-1)
196 tyConKind (SpecTyCon tc tys)
197 = spec (tyConKind tc) tys
200 spec kind (Just _ : tys) = spec (resultKind kind) tys
201 spec kind (Nothing : tys) =
202 argKind kind `mkArrowKind` spec (resultKind kind) tys
206 tyConUnique :: TyCon -> Unique
207 tyConUnique FunTyCon = funTyConKey
208 tyConUnique (DataTyCon uniq _ _ _ _ _ _ _) = uniq
209 tyConUnique (TupleTyCon uniq _ _) = uniq
210 tyConUnique (PrimTyCon uniq _ _ _) = uniq
211 tyConUnique (SynTyCon uniq _ _ _ _ _) = uniq
212 tyConUnique (SpecTyCon _ _ ) = panic "tyConUnique:SpecTyCon"
214 synTyConArity :: TyCon -> Maybe Arity -- Nothing <=> not a syn tycon
215 synTyConArity (SynTyCon _ _ _ arity _ _) = Just arity
216 synTyConArity _ = Nothing
220 tyConTyVars :: TyCon -> [TyVar]
221 tyConTyVars FunTyCon = [alphaTyVar,betaTyVar]
222 tyConTyVars (DataTyCon _ _ _ tvs _ _ _ _) = tvs
223 tyConTyVars (TupleTyCon _ _ arity) = take arity alphaTyVars
224 tyConTyVars (SynTyCon _ _ _ _ tvs _) = tvs
226 tyConTyVars (PrimTyCon _ _ _ _) = panic "tyConTyVars:PrimTyCon"
227 tyConTyVars (SpecTyCon _ _ ) = panic "tyConTyVars:SpecTyCon"
232 tyConDataCons :: TyCon -> [Id]
233 tyConFamilySize :: TyCon -> Int
235 tyConDataCons (DataTyCon _ _ _ _ _ data_cons _ _) = data_cons
236 tyConDataCons (TupleTyCon _ _ a) = [tupleCon a]
237 tyConDataCons other = []
238 -- You may think this last equation should fail,
239 -- but it's quite convenient to return no constructors for
240 -- a synonym; see for example the call in TcTyClsDecls.
242 tyConFamilySize (DataTyCon _ _ _ _ _ data_cons _ _) = length data_cons
243 tyConFamilySize (TupleTyCon _ _ _) = 1
245 --tyConFamilySize other = pprPanic "tyConFamilySize:" (pprTyCon PprDebug other)
248 tyConPrimRep :: TyCon -> PrimRep
249 tyConPrimRep (PrimTyCon _ _ _ rep) = rep
250 tyConPrimRep _ = PtrRep
254 tyConDerivings :: TyCon -> [Class]
255 tyConDerivings (DataTyCon _ _ _ _ _ _ derivs _) = derivs
256 tyConDerivings other = []
260 tyConTheta :: TyCon -> [(Class,Type)]
261 tyConTheta (DataTyCon _ _ _ _ theta _ _ _) = theta
262 tyConTheta (TupleTyCon _ _ _) = []
263 -- should ask about anything else
267 getSynTyConDefn :: TyCon -> ([TyVar], Type)
268 getSynTyConDefn (SynTyCon _ _ _ _ tyvars ty) = (tyvars,ty)
272 maybeTyConSingleCon :: TyCon -> Maybe Id
274 maybeTyConSingleCon (TupleTyCon _ _ arity) = Just (tupleCon arity)
275 maybeTyConSingleCon (DataTyCon _ _ _ _ _ [c] _ _) = Just c
276 maybeTyConSingleCon (DataTyCon _ _ _ _ _ _ _ _) = Nothing
277 maybeTyConSingleCon (PrimTyCon _ _ _ _) = Nothing
278 maybeTyConSingleCon (SpecTyCon tc tys) = panic "maybeTyConSingleCon:SpecTyCon"
279 -- requires DataCons of TyCon
281 isEnumerationTyCon (TupleTyCon _ _ arity)
283 isEnumerationTyCon (DataTyCon _ _ _ _ _ data_cons _ _)
284 = not (null data_cons) && all isNullaryDataCon data_cons
287 isTupleTyCon (TupleTyCon _ _ arity) = arity >= 2 -- treat "0-tuple" specially
288 isTupleTyCon (SpecTyCon tc tys) = isTupleTyCon tc
289 isTupleTyCon other = False
294 @derivedFor@ reports if we have an {\em obviously}-derived instance
295 for the given class/tycon. Of course, you might be deriving something
296 because it a superclass of some other obviously-derived class --- this
297 function doesn't deal with that.
299 ToDo: what about derivings for specialised tycons !!!
302 derivedClasses :: TyCon -> [Class]
303 derivedClasses (DataTyCon _ _ _ _ _ _ derivs _) = derivs
304 derivedClasses something_weird = []
307 %************************************************************************
309 \subsection[TyCon-instances]{Instance declarations for @TyCon@}
311 %************************************************************************
313 @TyCon@s are compared by comparing their @Unique@s.
315 The strictness analyser needs @Ord@. It is a lexicographic order with
316 the property @(a<=b) || (b<=a)@.
319 instance Ord3 TyCon where
320 cmp tc1 tc2 = uniqueOf tc1 `cmp` uniqueOf tc2
322 instance Eq TyCon where
323 a == b = case (a `cmp` b) of { EQ_ -> True; _ -> False }
324 a /= b = case (a `cmp` b) of { EQ_ -> False; _ -> True }
326 instance Ord TyCon where
327 a <= b = case (a `cmp` b) of { LT_ -> True; EQ_ -> True; GT__ -> False }
328 a < b = case (a `cmp` b) of { LT_ -> True; EQ_ -> False; GT__ -> False }
329 a >= b = case (a `cmp` b) of { LT_ -> False; EQ_ -> True; GT__ -> True }
330 a > b = case (a `cmp` b) of { LT_ -> False; EQ_ -> False; GT__ -> True }
331 _tagCmp a b = case (a `cmp` b) of { LT_ -> _LT; EQ_ -> _EQ; GT__ -> _GT }
333 instance Uniquable TyCon where
334 uniqueOf (DataTyCon u _ _ _ _ _ _ _) = u
335 uniqueOf (TupleTyCon u _ _) = u
336 uniqueOf (PrimTyCon u _ _ _) = u
337 uniqueOf (SynTyCon u _ _ _ _ _) = u
338 uniqueOf tc@(SpecTyCon _ _) = panic "uniqueOf:SpecTyCon"
339 uniqueOf tc = uniqueOf (getName tc)
343 instance NamedThing TyCon where
344 getName (DataTyCon _ n _ _ _ _ _ _) = n
345 getName (PrimTyCon _ n _ _) = n
346 getName (SpecTyCon tc _) = getName tc
347 getName (SynTyCon _ n _ _ _ _) = n
348 getName FunTyCon = mkFunTyConName
349 getName (TupleTyCon _ n _) = n
350 getName tc = panic "TyCon.getName"
353 getName (SpecTyCon tc tys) = let (OrigName m n) = origName "????" tc in
354 (m, n _APPEND_ specMaybeTysSuffix tys)
355 getName other_tc = moduleNamePair (expectJust "tycon1" (getName other_tc))
356 getName other = Nothing