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 isDataTyCon, isSynTyCon, isNewTyCon, maybeNewTyCon,
41 CHK_Ubiq() -- debugging consistency check
43 IMPORT_DELOOPER(TyLoop) ( SYN_IE(Type), GenType,
44 SYN_IE(Class), GenClass,
46 splitSigmaTy, splitFunTy,
47 mkTupleCon, isNullaryDataCon, idType
48 --LATER: specMaybeTysSuffix
51 import TyVar ( GenTyVar, alphaTyVars, alphaTyVar, betaTyVar, SYN_IE(TyVar) )
52 import Usage ( GenUsage, SYN_IE(Usage) )
53 import Kind ( Kind, mkBoxedTypeKind, mkArrowKind, resultKind, argKind )
56 import Name ( Name, RdrName(..), appendRdr, nameUnique,
57 mkTupleTyConName, mkFunTyConName
59 import Unique ( Unique, funTyConKey, mkTupleTyConUnique )
60 import Pretty ( SYN_IE(Pretty), PrettyRep )
61 import PrimRep ( PrimRep(..) )
62 import SrcLoc ( SrcLoc, mkBuiltinSrcLoc )
63 import Util ( nOfThem, isIn, Ord3(..), panic, panic#, assertPanic, pprPanic{-ToDo:rm-} )
74 = FunTyCon -- Kind = Type -> Type -> Type
76 | DataTyCon Unique{-TyConKey-}
80 [(Class,Type)] -- Its context
81 [Id] -- Its data constructors, with fully polymorphic types
82 [Class] -- Classes which have derived instances
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 unboxed; 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
100 | SpecTyCon -- A specialised TyCon; eg (Arr# Int#), or (List Int#)
102 [Maybe Type] -- Specialising types
104 -- OLD STUFF ABOUT Array types. Use SpecTyCon instead
105 -- ([PrimRep] -> PrimRep) -- a heap-allocated object (eg ArrInt#).
106 -- The primitive types Arr# and StablePtr# have
107 -- parameters (hence arity /= 0); but the rest don't.
108 -- Only arrays use the list in a non-trivial way.
109 -- Length of that list must == arity.
116 [TyVar] -- Argument type variables
117 Type -- Right-hand side, mentioning these type vars.
118 -- Acts as a template for the expansion when
119 -- the tycon is applied to some types.
122 = NewType -- "newtype Blah ..."
123 | DataType -- "data Blah ..."
127 mkFunTyCon = FunTyCon
128 mkSpecTyCon = SpecTyCon
131 = TupleTyCon u n arity
133 n = mkTupleTyConName arity
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 -- isDataTyCon returns False for @newtype@.
151 -- Not sure about this decision yet.
152 isDataTyCon (DataTyCon _ _ _ _ _ _ _ DataType) = True
153 isDataTyCon (TupleTyCon _ _ _) = True
154 isDataTyCon other = False
156 maybeNewTyCon :: TyCon -> Maybe ([TyVar], Type) -- Returns representation type info
157 maybeNewTyCon (DataTyCon _ _ _ _ _ (con:null_cons) _ NewType)
158 = ASSERT( null null_cons && null null_tys)
159 Just (tyvars, rep_ty)
161 (tyvars, theta, tau) = splitSigmaTy (idType con)
162 (rep_ty:null_tys, res_ty) = splitFunTy tau
164 maybeNewTyCon other = Nothing
166 isNewTyCon (DataTyCon _ _ _ _ _ _ _ NewType) = True
167 isNewTyCon other = False
169 isSynTyCon (SynTyCon _ _ _ _ _ _) = True
174 -- Special cases to avoid reconstructing lots of kinds
175 kind1 = mkBoxedTypeKind `mkArrowKind` mkBoxedTypeKind
176 kind2 = mkBoxedTypeKind `mkArrowKind` kind1
178 tyConKind :: TyCon -> Kind
179 tyConKind FunTyCon = kind2
180 tyConKind (DataTyCon _ _ kind _ _ _ _ _) = kind
181 tyConKind (PrimTyCon _ _ kind _) = kind
182 tyConKind (SynTyCon _ _ k _ _ _) = k
184 tyConKind (TupleTyCon _ _ n)
187 mkArrow 0 = mkBoxedTypeKind
190 mkArrow n = mkBoxedTypeKind `mkArrowKind` mkArrow (n-1)
192 tyConKind (SpecTyCon tc tys)
193 = spec (tyConKind tc) tys
196 spec kind (Just _ : tys) = spec (resultKind kind) tys
197 spec kind (Nothing : tys) =
198 argKind kind `mkArrowKind` spec (resultKind kind) tys
202 tyConUnique :: TyCon -> Unique
203 tyConUnique FunTyCon = funTyConKey
204 tyConUnique (DataTyCon uniq _ _ _ _ _ _ _) = uniq
205 tyConUnique (TupleTyCon uniq _ _) = uniq
206 tyConUnique (PrimTyCon uniq _ _ _) = uniq
207 tyConUnique (SynTyCon uniq _ _ _ _ _) = uniq
208 tyConUnique (SpecTyCon _ _ ) = panic "tyConUnique:SpecTyCon"
210 synTyConArity :: TyCon -> Maybe Arity -- Nothing <=> not a syn tycon
211 synTyConArity (SynTyCon _ _ _ arity _ _) = Just arity
212 synTyConArity _ = Nothing
216 tyConTyVars :: TyCon -> [TyVar]
217 tyConTyVars FunTyCon = [alphaTyVar,betaTyVar]
218 tyConTyVars (DataTyCon _ _ _ tvs _ _ _ _) = tvs
219 tyConTyVars (TupleTyCon _ _ arity) = take arity alphaTyVars
220 tyConTyVars (SynTyCon _ _ _ _ tvs _) = tvs
222 tyConTyVars (PrimTyCon _ _ _ _) = panic "tyConTyVars:PrimTyCon"
223 tyConTyVars (SpecTyCon _ _ ) = panic "tyConTyVars:SpecTyCon"
228 tyConDataCons :: TyCon -> [Id]
229 tyConFamilySize :: TyCon -> Int
231 tyConDataCons (DataTyCon _ _ _ _ _ data_cons _ _) = data_cons
232 tyConDataCons (TupleTyCon _ _ a) = [mkTupleCon a]
233 tyConDataCons other = []
234 -- You may think this last equation should fail,
235 -- but it's quite convenient to return no constructors for
236 -- a synonym; see for example the call in TcTyClsDecls.
238 tyConFamilySize (DataTyCon _ _ _ _ _ data_cons _ _) = length data_cons
239 tyConFamilySize (TupleTyCon _ _ _) = 1
241 tyConFamilySize other = pprPanic "tyConFamilySize:" (pprTyCon PprDebug other)
244 tyConPrimRep :: TyCon -> PrimRep
245 tyConPrimRep (PrimTyCon _ _ _ rep) = rep
246 tyConPrimRep _ = PtrRep
250 tyConDerivings :: TyCon -> [Class]
251 tyConDerivings (DataTyCon _ _ _ _ _ _ derivs _) = derivs
252 tyConDerivings other = []
256 tyConTheta :: TyCon -> [(Class,Type)]
257 tyConTheta (DataTyCon _ _ _ _ theta _ _ _) = theta
258 tyConTheta (TupleTyCon _ _ _) = []
259 -- should ask about anything else
263 getSynTyConDefn :: TyCon -> ([TyVar], Type)
264 getSynTyConDefn (SynTyCon _ _ _ _ tyvars ty) = (tyvars,ty)
268 maybeTyConSingleCon :: TyCon -> Maybe Id
270 maybeTyConSingleCon (TupleTyCon _ _ arity) = Just (mkTupleCon arity)
271 maybeTyConSingleCon (DataTyCon _ _ _ _ _ [c] _ _) = Just c
272 maybeTyConSingleCon (DataTyCon _ _ _ _ _ _ _ _) = Nothing
273 maybeTyConSingleCon (PrimTyCon _ _ _ _) = Nothing
274 maybeTyConSingleCon (SpecTyCon tc tys) = panic "maybeTyConSingleCon:SpecTyCon"
275 -- requires DataCons of TyCon
277 isEnumerationTyCon (TupleTyCon _ _ arity)
279 isEnumerationTyCon (DataTyCon _ _ _ _ _ data_cons _ _)
280 = not (null data_cons) && all isNullaryDataCon data_cons
283 @derivedFor@ reports if we have an {\em obviously}-derived instance
284 for the given class/tycon. Of course, you might be deriving something
285 because it a superclass of some other obviously-derived class --- this
286 function doesn't deal with that.
288 ToDo: what about derivings for specialised tycons !!!
291 derivedFor :: Class -> TyCon -> Bool
292 derivedFor clas (DataTyCon _ _ _ _ _ _ derivs _) = isIn "derivedFor" clas derivs
293 derivedFor clas something_weird = False
296 %************************************************************************
298 \subsection[TyCon-instances]{Instance declarations for @TyCon@}
300 %************************************************************************
302 @TyCon@s are compared by comparing their @Unique@s.
304 The strictness analyser needs @Ord@. It is a lexicographic order with
305 the property @(a<=b) || (b<=a)@.
308 instance Ord3 TyCon where
309 cmp tc1 tc2 = uniqueOf tc1 `cmp` uniqueOf tc2
311 instance Eq TyCon where
312 a == b = case (a `cmp` b) of { EQ_ -> True; _ -> False }
313 a /= b = case (a `cmp` b) of { EQ_ -> False; _ -> True }
315 instance Ord TyCon where
316 a <= b = case (a `cmp` b) of { LT_ -> True; EQ_ -> True; GT__ -> False }
317 a < b = case (a `cmp` b) of { LT_ -> True; EQ_ -> False; GT__ -> False }
318 a >= b = case (a `cmp` b) of { LT_ -> False; EQ_ -> True; GT__ -> True }
319 a > b = case (a `cmp` b) of { LT_ -> False; EQ_ -> False; GT__ -> True }
320 _tagCmp a b = case (a `cmp` b) of { LT_ -> _LT; EQ_ -> _EQ; GT__ -> _GT }
322 instance Uniquable TyCon where
323 uniqueOf (DataTyCon u _ _ _ _ _ _ _) = u
324 uniqueOf (TupleTyCon u _ _) = u
325 uniqueOf (PrimTyCon u _ _ _) = u
326 uniqueOf (SynTyCon u _ _ _ _ _) = u
327 uniqueOf tc@(SpecTyCon _ _) = panic "uniqueOf:SpecTyCon"
328 uniqueOf tc = uniqueOf (getName tc)
332 instance NamedThing TyCon where
333 getName (DataTyCon _ n _ _ _ _ _ _) = n
334 getName (PrimTyCon _ n _ _) = n
335 getName (SpecTyCon tc _) = getName tc
336 getName (SynTyCon _ n _ _ _ _) = n
337 getName FunTyCon = mkFunTyConName
338 getName (TupleTyCon _ n _) = n
339 getName tc = panic "TyCon.getName"
342 getName (SpecTyCon tc tys) = let (OrigName m n) = origName "????" tc in
343 (m, n _APPEND_ specMaybeTysSuffix tys)
344 getName other_tc = moduleNamePair (expectJust "tycon1" (getName other_tc))
345 getName other = Nothing