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 )
53 import {-# SOURCE #-} Class ( Class )
54 import {-# SOURCE #-} Id ( Id, isNullaryDataCon, idType )
55 import {-# SOURCE #-} Type ( splitSigmaTy, splitFunTy )
56 import {-# SOURCE #-} TysWiredIn ( tupleCon )
59 import BasicTypes ( SYN_IE(Arity), NewOrData(..) )
60 import TyVar ( GenTyVar, alphaTyVars, alphaTyVar, betaTyVar, SYN_IE(TyVar) )
61 import Usage ( GenUsage, SYN_IE(Usage) )
62 import Kind ( Kind, mkBoxedTypeKind, mkArrowKind, resultKind, argKind )
65 import Name ( Name, nameUnique, mkWiredInTyConName, NamedThing(getName) )
66 import Unique ( Unique, funTyConKey )
67 import UniqFM ( Uniquable(..) )
69 import PrimRep ( PrimRep(..) )
70 import PrelMods ( gHC__, pREL_TUP, pREL_BASE )
71 import Lex ( mkTupNameStr )
72 import SrcLoc ( SrcLoc, mkBuiltinSrcLoc )
73 import Util ( nOfThem, isIn, Ord3(..), panic, panic#, assertPanic )
78 = FunTyCon -- Kind = Type -> Type -> Type
80 | DataTyCon Unique{-TyConKey-}
84 [(Class,Type)] -- Its context
85 [Id{-DataCon-}] -- Its data constructors, with fully polymorphic types
86 -- This list can be empty, when we import a data type abstractly,
87 -- either (a) the interface is hand-written and doesn't give
88 -- the constructors, or
89 -- (b) in a quest for fast compilation we don't import
91 [Class] -- Classes which have derived instances
94 | TupleTyCon Unique -- cached
95 Name -- again, we could do without this, but
96 -- it makes life somewhat easier
97 Arity -- just a special case of DataTyCon
98 -- Kind = BoxedTypeKind
99 -- -> ... (n times) ...
103 | PrimTyCon -- Primitive types; cannot be defined in Haskell
104 Unique -- Always unboxed; hence never represented by a closure
105 Name -- Often represented by a bit-pattern for the thing
106 Kind -- itself (eg Int#), but sometimes by a pointer to
109 | SpecTyCon -- A specialised TyCon; eg (Arr# Int#), or (List Int#)
111 [Maybe Type] -- Specialising types
113 -- OLD STUFF ABOUT Array types. Use SpecTyCon instead
114 -- ([PrimRep] -> PrimRep) -- a heap-allocated object (eg ArrInt#).
115 -- The primitive types Arr# and StablePtr# have
116 -- parameters (hence arity /= 0); but the rest don't.
117 -- Only arrays use the list in a non-trivial way.
118 -- Length of that list must == arity.
125 [TyVar] -- Argument type variables
126 Type -- Right-hand side, mentioning these type vars.
127 -- Acts as a template for the expansion when
128 -- the tycon is applied to some types.
132 mkFunTyCon = FunTyCon
133 mkFunTyConName = mkWiredInTyConName funTyConKey gHC__ SLIT("->") FunTyCon
135 mkSpecTyCon = SpecTyCon
136 mkTupleTyCon = TupleTyCon
138 mkDataTyCon name = DataTyCon (nameUnique name) name
139 mkPrimTyCon name = PrimTyCon (nameUnique name) name
140 mkSynTyCon name = SynTyCon (nameUnique name) name
142 isFunTyCon FunTyCon = True
145 isPrimTyCon (PrimTyCon _ _ _ _) = True
146 isPrimTyCon _ = False
148 -- At present there are no unboxed non-primitive types, so
149 -- isBoxedTyCon is just the negation of isPrimTyCon.
150 isBoxedTyCon = not . isPrimTyCon
152 -- isAlgTyCon returns True for both @data@ and @newtype@
153 isAlgTyCon (DataTyCon _ _ _ _ _ _ _ _) = True
154 isAlgTyCon (TupleTyCon _ _ _) = True
155 isAlgTyCon other = False
157 -- isDataTyCon returns False for @newtype@.
158 isDataTyCon (DataTyCon _ _ _ _ _ _ _ DataType) = True
159 isDataTyCon (TupleTyCon _ _ _) = True
160 isDataTyCon other = False
162 maybeNewTyCon :: TyCon -> Maybe ([TyVar], Type) -- Returns representation type info
163 maybeNewTyCon (DataTyCon _ _ _ _ _ (con:null_cons) _ NewType)
164 = ASSERT( null null_cons && null null_tys)
165 Just (tyvars, rep_ty)
167 (tyvars, theta, tau) = splitSigmaTy (idType con)
168 (rep_ty:null_tys, res_ty) = splitFunTy tau
170 maybeNewTyCon other = Nothing
172 isNewTyCon (DataTyCon _ _ _ _ _ _ _ NewType) = True
173 isNewTyCon other = False
175 isSynTyCon (SynTyCon _ _ _ _ _ _) = True
180 -- Special cases to avoid reconstructing lots of kinds
181 kind1 = mkBoxedTypeKind `mkArrowKind` mkBoxedTypeKind
182 kind2 = mkBoxedTypeKind `mkArrowKind` kind1
184 tyConKind :: TyCon -> Kind
185 tyConKind FunTyCon = kind2
186 tyConKind (DataTyCon _ _ kind _ _ _ _ _) = kind
187 tyConKind (PrimTyCon _ _ kind _) = kind
188 tyConKind (SynTyCon _ _ k _ _ _) = k
190 tyConKind (TupleTyCon _ _ n)
193 mkArrow 0 = mkBoxedTypeKind
196 mkArrow n = mkBoxedTypeKind `mkArrowKind` mkArrow (n-1)
198 tyConKind (SpecTyCon tc tys)
199 = spec (tyConKind tc) tys
202 spec kind (Just _ : tys) = spec (resultKind kind) tys
203 spec kind (Nothing : tys) =
204 argKind kind `mkArrowKind` spec (resultKind kind) tys
208 tyConUnique :: TyCon -> Unique
209 tyConUnique FunTyCon = funTyConKey
210 tyConUnique (DataTyCon uniq _ _ _ _ _ _ _) = uniq
211 tyConUnique (TupleTyCon uniq _ _) = uniq
212 tyConUnique (PrimTyCon uniq _ _ _) = uniq
213 tyConUnique (SynTyCon uniq _ _ _ _ _) = uniq
214 tyConUnique (SpecTyCon _ _ ) = panic "tyConUnique:SpecTyCon"
216 synTyConArity :: TyCon -> Maybe Arity -- Nothing <=> not a syn tycon
217 synTyConArity (SynTyCon _ _ _ arity _ _) = Just arity
218 synTyConArity _ = Nothing
222 tyConTyVars :: TyCon -> [TyVar]
223 tyConTyVars FunTyCon = [alphaTyVar,betaTyVar]
224 tyConTyVars (DataTyCon _ _ _ tvs _ _ _ _) = tvs
225 tyConTyVars (TupleTyCon _ _ arity) = take arity alphaTyVars
226 tyConTyVars (SynTyCon _ _ _ _ tvs _) = tvs
228 tyConTyVars (PrimTyCon _ _ _ _) = panic "tyConTyVars:PrimTyCon"
229 tyConTyVars (SpecTyCon _ _ ) = panic "tyConTyVars:SpecTyCon"
234 tyConDataCons :: TyCon -> [Id]
235 tyConFamilySize :: TyCon -> Int
237 tyConDataCons (DataTyCon _ _ _ _ _ data_cons _ _) = data_cons
238 tyConDataCons (TupleTyCon _ _ a) = [tupleCon a]
239 tyConDataCons other = []
240 -- You may think this last equation should fail,
241 -- but it's quite convenient to return no constructors for
242 -- a synonym; see for example the call in TcTyClsDecls.
244 tyConFamilySize (DataTyCon _ _ _ _ _ data_cons _ _) = length data_cons
245 tyConFamilySize (TupleTyCon _ _ _) = 1
247 --tyConFamilySize other = pprPanic "tyConFamilySize:" (pprTyCon PprDebug other)
250 tyConPrimRep :: TyCon -> PrimRep
251 tyConPrimRep (PrimTyCon _ _ _ rep) = rep
252 tyConPrimRep _ = PtrRep
256 tyConDerivings :: TyCon -> [Class]
257 tyConDerivings (DataTyCon _ _ _ _ _ _ derivs _) = derivs
258 tyConDerivings other = []
262 tyConTheta :: TyCon -> [(Class,Type)]
263 tyConTheta (DataTyCon _ _ _ _ theta _ _ _) = theta
264 tyConTheta (TupleTyCon _ _ _) = []
265 -- should ask about anything else
269 getSynTyConDefn :: TyCon -> ([TyVar], Type)
270 getSynTyConDefn (SynTyCon _ _ _ _ tyvars ty) = (tyvars,ty)
274 maybeTyConSingleCon :: TyCon -> Maybe Id
276 maybeTyConSingleCon (TupleTyCon _ _ arity) = Just (tupleCon arity)
277 maybeTyConSingleCon (DataTyCon _ _ _ _ _ [c] _ _) = Just c
278 maybeTyConSingleCon (DataTyCon _ _ _ _ _ _ _ _) = Nothing
279 maybeTyConSingleCon (PrimTyCon _ _ _ _) = Nothing
280 maybeTyConSingleCon (SpecTyCon tc tys) = panic "maybeTyConSingleCon:SpecTyCon"
281 -- requires DataCons of TyCon
283 isEnumerationTyCon (TupleTyCon _ _ arity)
285 isEnumerationTyCon (DataTyCon _ _ _ _ _ data_cons _ _)
286 = not (null data_cons) && all isNullaryDataCon data_cons
289 isTupleTyCon (TupleTyCon _ _ arity) = arity >= 2 -- treat "0-tuple" specially
290 isTupleTyCon (SpecTyCon tc tys) = isTupleTyCon tc
291 isTupleTyCon other = False
296 @derivedFor@ reports if we have an {\em obviously}-derived instance
297 for the given class/tycon. Of course, you might be deriving something
298 because it a superclass of some other obviously-derived class --- this
299 function doesn't deal with that.
301 ToDo: what about derivings for specialised tycons !!!
304 derivedClasses :: TyCon -> [Class]
305 derivedClasses (DataTyCon _ _ _ _ _ _ derivs _) = derivs
306 derivedClasses something_weird = []
309 %************************************************************************
311 \subsection[TyCon-instances]{Instance declarations for @TyCon@}
313 %************************************************************************
315 @TyCon@s are compared by comparing their @Unique@s.
317 The strictness analyser needs @Ord@. It is a lexicographic order with
318 the property @(a<=b) || (b<=a)@.
321 instance Ord3 TyCon where
322 cmp tc1 tc2 = uniqueOf tc1 `cmp` uniqueOf tc2
324 instance Eq TyCon where
325 a == b = case (a `cmp` b) of { EQ_ -> True; _ -> False }
326 a /= b = case (a `cmp` b) of { EQ_ -> False; _ -> True }
328 instance Ord TyCon where
329 a <= b = case (a `cmp` b) of { LT_ -> True; EQ_ -> True; GT__ -> False }
330 a < b = case (a `cmp` b) of { LT_ -> True; EQ_ -> False; GT__ -> False }
331 a >= b = case (a `cmp` b) of { LT_ -> False; EQ_ -> True; GT__ -> True }
332 a > b = case (a `cmp` b) of { LT_ -> False; EQ_ -> False; GT__ -> True }
333 _tagCmp a b = case (a `cmp` b) of { LT_ -> _LT; EQ_ -> _EQ; GT__ -> _GT }
335 instance Uniquable TyCon where
336 uniqueOf (DataTyCon u _ _ _ _ _ _ _) = u
337 uniqueOf (TupleTyCon u _ _) = u
338 uniqueOf (PrimTyCon u _ _ _) = u
339 uniqueOf (SynTyCon u _ _ _ _ _) = u
340 uniqueOf tc@(SpecTyCon _ _) = panic "uniqueOf:SpecTyCon"
341 uniqueOf tc = uniqueOf (getName tc)
345 instance NamedThing TyCon where
346 getName (DataTyCon _ n _ _ _ _ _ _) = n
347 getName (PrimTyCon _ n _ _) = n
348 getName (SpecTyCon tc _) = getName tc
349 getName (SynTyCon _ n _ _ _ _) = n
350 getName FunTyCon = mkFunTyConName
351 getName (TupleTyCon _ n _) = n
352 getName tc = panic "TyCon.getName"
355 getName (SpecTyCon tc tys) = let (OrigName m n) = origName "????" tc in
356 (m, n _APPEND_ specMaybeTysSuffix tys)
357 getName other_tc = moduleNamePair (expectJust "tycon1" (getName other_tc))
358 getName other = Nothing