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 Arity(..), NewOrData(..),
14 isFunTyCon, isPrimTyCon, isBoxedTyCon,
15 isDataTyCon, isSynTyCon, isNewTyCon,
41 CHK_Ubiq() -- debugging consistency check
43 IMPORT_DELOOPER(TyLoop) ( Type(..), GenType,
46 mkTupleCon, isNullaryDataCon,
50 import TyVar ( GenTyVar, alphaTyVars, alphaTyVar, betaTyVar )
51 import Usage ( GenUsage, Usage(..) )
52 import Kind ( Kind, mkBoxedTypeKind, mkArrowKind, resultKind, argKind )
55 import Name ( Name, RdrName(..), appendRdr, nameUnique,
56 mkTupleTyConName, mkFunTyConName
58 import Unique ( Unique, funTyConKey, mkTupleTyConUnique )
59 import Pretty ( Pretty(..), PrettyRep )
60 import PrimRep ( PrimRep(..) )
61 import SrcLoc ( SrcLoc, mkBuiltinSrcLoc )
62 import Util ( panic, panic#, pprPanic{-ToDo:rm-}, nOfThem, isIn, Ord3(..) )
73 = FunTyCon -- Kind = Type -> Type -> Type
75 | DataTyCon Unique{-TyConKey-}
79 [(Class,Type)] -- Its context
80 [Id] -- Its data constructors, with fully polymorphic types
81 [Class] -- Classes which have derived instances
84 | TupleTyCon Unique -- cached
85 Name -- again, we could do without this, but
86 -- it makes life somewhat easier
87 Arity -- just a special case of DataTyCon
88 -- Kind = BoxedTypeKind
89 -- -> ... (n times) ...
93 | PrimTyCon -- Primitive types; cannot be defined in Haskell
94 Unique -- Always unboxed; hence never represented by a closure
95 Name -- Often represented by a bit-pattern for the thing
96 Kind -- itself (eg Int#), but sometimes by a pointer to
99 | SpecTyCon -- A specialised TyCon; eg (Arr# Int#), or (List Int#)
101 [Maybe Type] -- Specialising types
103 -- OLD STUFF ABOUT Array types. Use SpecTyCon instead
104 -- ([PrimRep] -> PrimRep) -- a heap-allocated object (eg ArrInt#).
105 -- The primitive types Arr# and StablePtr# have
106 -- parameters (hence arity /= 0); but the rest don't.
107 -- Only arrays use the list in a non-trivial way.
108 -- Length of that list must == arity.
115 [TyVar] -- Argument type variables
116 Type -- Right-hand side, mentioning these type vars.
117 -- Acts as a template for the expansion when
118 -- the tycon is applied to some types.
121 = NewType -- "newtype Blah ..."
122 | DataType -- "data Blah ..."
126 mkFunTyCon = FunTyCon
127 mkSpecTyCon = SpecTyCon
130 = TupleTyCon u n arity
132 n = mkTupleTyConName arity
136 = DataTyCon (nameUnique name) name
138 = PrimTyCon (nameUnique name) name
140 = 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 -- isDataTyCon returns False for @newtype@.
153 -- Not sure about this decision yet.
154 isDataTyCon (DataTyCon _ _ _ _ _ _ _ DataType) = True
155 isDataTyCon (TupleTyCon _ _ _) = True
156 isDataTyCon other = False
158 isNewTyCon (DataTyCon _ _ _ _ _ _ _ NewType) = True
159 isNewTyCon other = False
161 isSynTyCon (SynTyCon _ _ _ _ _ _) = True
166 -- Special cases to avoid reconstructing lots of kinds
167 kind1 = mkBoxedTypeKind `mkArrowKind` mkBoxedTypeKind
168 kind2 = mkBoxedTypeKind `mkArrowKind` kind1
170 tyConKind :: TyCon -> Kind
171 tyConKind FunTyCon = kind2
172 tyConKind (DataTyCon _ _ kind _ _ _ _ _) = kind
173 tyConKind (PrimTyCon _ _ kind _) = kind
174 tyConKind (SynTyCon _ _ k _ _ _) = k
176 tyConKind (TupleTyCon _ _ n)
179 mkArrow 0 = mkBoxedTypeKind
182 mkArrow n = mkBoxedTypeKind `mkArrowKind` mkArrow (n-1)
184 tyConKind (SpecTyCon tc tys)
185 = spec (tyConKind tc) tys
188 spec kind (Just _ : tys) = spec (resultKind kind) tys
189 spec kind (Nothing : tys) =
190 argKind kind `mkArrowKind` spec (resultKind kind) tys
194 tyConUnique :: TyCon -> Unique
195 tyConUnique FunTyCon = funTyConKey
196 tyConUnique (DataTyCon uniq _ _ _ _ _ _ _) = uniq
197 tyConUnique (TupleTyCon uniq _ _) = uniq
198 tyConUnique (PrimTyCon uniq _ _ _) = uniq
199 tyConUnique (SynTyCon uniq _ _ _ _ _) = uniq
200 tyConUnique (SpecTyCon _ _ ) = panic "tyConUnique:SpecTyCon"
202 synTyConArity :: TyCon -> Maybe Arity -- Nothing <=> not a syn tycon
203 synTyConArity (SynTyCon _ _ _ arity _ _) = Just arity
204 synTyConArity _ = Nothing
208 tyConTyVars :: TyCon -> [TyVar]
209 tyConTyVars FunTyCon = [alphaTyVar,betaTyVar]
210 tyConTyVars (DataTyCon _ _ _ tvs _ _ _ _) = tvs
211 tyConTyVars (TupleTyCon _ _ arity) = take arity alphaTyVars
212 tyConTyVars (SynTyCon _ _ _ _ tvs _) = tvs
214 tyConTyVars (PrimTyCon _ _ _ _) = panic "tyConTyVars:PrimTyCon"
215 tyConTyVars (SpecTyCon _ _ ) = panic "tyConTyVars:SpecTyCon"
220 tyConDataCons :: TyCon -> [Id]
221 tyConFamilySize :: TyCon -> Int
223 tyConDataCons (DataTyCon _ _ _ _ _ data_cons _ _) = data_cons
224 tyConDataCons (TupleTyCon _ _ a) = [mkTupleCon a]
225 tyConDataCons other = []
226 -- You may think this last equation should fail,
227 -- but it's quite convenient to return no constructors for
228 -- a synonym; see for example the call in TcTyClsDecls.
230 tyConFamilySize (DataTyCon _ _ _ _ _ data_cons _ _) = length data_cons
231 tyConFamilySize (TupleTyCon _ _ _) = 1
233 tyConFamilySize other = pprPanic "tyConFamilySize:" (pprTyCon PprDebug other)
236 tyConPrimRep :: TyCon -> PrimRep
237 tyConPrimRep (PrimTyCon _ _ _ rep) = rep
238 tyConPrimRep _ = PtrRep
242 tyConDerivings :: TyCon -> [Class]
243 tyConDerivings (DataTyCon _ _ _ _ _ _ derivs _) = derivs
244 tyConDerivings other = []
248 tyConTheta :: TyCon -> [(Class,Type)]
249 tyConTheta (DataTyCon _ _ _ _ theta _ _ _) = theta
250 tyConTheta (TupleTyCon _ _ _) = []
251 -- should ask about anything else
255 getSynTyConDefn :: TyCon -> ([TyVar], Type)
256 getSynTyConDefn (SynTyCon _ _ _ _ tyvars ty) = (tyvars,ty)
260 maybeTyConSingleCon :: TyCon -> Maybe Id
262 maybeTyConSingleCon (TupleTyCon _ _ arity) = Just (mkTupleCon arity)
263 maybeTyConSingleCon (DataTyCon _ _ _ _ _ [c] _ _) = Just c
264 maybeTyConSingleCon (DataTyCon _ _ _ _ _ _ _ _) = Nothing
265 maybeTyConSingleCon (PrimTyCon _ _ _ _) = Nothing
266 maybeTyConSingleCon (SpecTyCon tc tys) = panic "maybeTyConSingleCon:SpecTyCon"
267 -- requires DataCons of TyCon
269 isEnumerationTyCon (TupleTyCon _ _ arity)
271 isEnumerationTyCon (DataTyCon _ _ _ _ _ data_cons _ _)
272 = not (null data_cons) && all isNullaryDataCon data_cons
275 @derivedFor@ reports if we have an {\em obviously}-derived instance
276 for the given class/tycon. Of course, you might be deriving something
277 because it a superclass of some other obviously-derived class --- this
278 function doesn't deal with that.
280 ToDo: what about derivings for specialised tycons !!!
283 derivedFor :: Class -> TyCon -> Bool
284 derivedFor clas (DataTyCon _ _ _ _ _ _ derivs _) = isIn "derivedFor" clas derivs
285 derivedFor clas something_weird = False
288 %************************************************************************
290 \subsection[TyCon-instances]{Instance declarations for @TyCon@}
292 %************************************************************************
294 @TyCon@s are compared by comparing their @Unique@s.
296 The strictness analyser needs @Ord@. It is a lexicographic order with
297 the property @(a<=b) || (b<=a)@.
300 instance Ord3 TyCon where
301 cmp tc1 tc2 = uniqueOf tc1 `cmp` uniqueOf tc2
303 instance Eq TyCon where
304 a == b = case (a `cmp` b) of { EQ_ -> True; _ -> False }
305 a /= b = case (a `cmp` b) of { EQ_ -> False; _ -> True }
307 instance Ord TyCon where
308 a <= b = case (a `cmp` b) of { LT_ -> True; EQ_ -> True; GT__ -> False }
309 a < b = case (a `cmp` b) of { LT_ -> True; EQ_ -> False; GT__ -> False }
310 a >= b = case (a `cmp` b) of { LT_ -> False; EQ_ -> True; GT__ -> True }
311 a > b = case (a `cmp` b) of { LT_ -> False; EQ_ -> False; GT__ -> True }
312 _tagCmp a b = case (a `cmp` b) of { LT_ -> _LT; EQ_ -> _EQ; GT__ -> _GT }
314 instance Uniquable TyCon where
315 uniqueOf (DataTyCon u _ _ _ _ _ _ _) = u
316 uniqueOf (TupleTyCon u _ _) = u
317 uniqueOf (PrimTyCon u _ _ _) = u
318 uniqueOf (SynTyCon u _ _ _ _ _) = u
319 uniqueOf tc@(SpecTyCon _ _) = panic "uniqueOf:SpecTyCon"
320 uniqueOf tc = uniqueOf (getName tc)
324 instance NamedThing TyCon where
325 getName (DataTyCon _ n _ _ _ _ _ _) = n
326 getName (PrimTyCon _ n _ _) = n
327 getName (SpecTyCon tc _) = getName tc
328 getName (SynTyCon _ n _ _ _ _) = n
329 getName FunTyCon = mkFunTyConName
330 getName (TupleTyCon _ n _) = n
331 getName tc = panic "TyCon.getName"
334 getName (SpecTyCon tc tys) = let (m,n) = moduleNamePair tc in
335 (m, n _APPEND_ specMaybeTysSuffix tys)
336 getName other_tc = moduleNamePair (expectJust "tycon1" (getName other_tc))
337 getName other = Nothing