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(..), ConsVisible(..), NewOrData(..),
14 isFunTyCon, isPrimTyCon, isVisibleDataTyCon,
36 CHK_Ubiq() -- debugging consistency check
37 import NameLoop -- for paranoia checking
39 import TyLoop ( Type(..), GenType,
42 mkTupleCon, getDataConSig,
46 import TyVar ( GenTyVar, alphaTyVars, alphaTyVar, betaTyVar )
47 import Usage ( GenUsage, Usage(..) )
48 import Kind ( Kind, mkBoxedTypeKind, mkArrowKind, resultKind, argKind )
49 import PrelMods ( pRELUDE_BUILTIN )
52 import NameTypes ( FullName )
53 import Unique ( Unique, funTyConKey, mkTupleTyConUnique )
55 import Pretty ( Pretty(..), PrettyRep )
56 import PprStyle ( PprStyle )
57 import SrcLoc ( SrcLoc, mkBuiltinSrcLoc )
58 import Util ( panic, panic#, nOfThem, isIn, Ord3(..) )
65 = FunTyCon -- Kind = Type -> Type -> Type
67 | DataTyCon Unique{-TyConKey-}
71 [(Class,Type)] -- Its context
72 [Id] -- Its data constructors, with fully polymorphic types
73 [Class] -- Classes which have derived instances
77 | TupleTyCon Arity -- just a special case of DataTyCon
78 -- Kind = BoxedTypeKind
79 -- -> ... (n times) ...
83 | PrimTyCon -- Primitive types; cannot be defined in Haskell
84 Unique -- Always unboxed; hence never represented by a closure
85 FullName -- Often represented by a bit-pattern for the thing
86 Kind -- itself (eg Int#), but sometimes by a pointer to
88 | SpecTyCon -- A specialised TyCon; eg (Arr# Int#), or (List Int#)
90 [Maybe Type] -- Specialising types
92 -- OLD STUFF ABOUT Array types. Use SpecTyCon instead
93 -- ([PrimRep] -> PrimRep) -- a heap-allocated object (eg ArrInt#).
94 -- The primitive types Arr# and StablePtr# have
95 -- parameters (hence arity /= 0); but the rest don't.
96 -- Only arrays use the list in a non-trivial way.
97 -- Length of that list must == arity.
104 [TyVar] -- Argument type variables
105 Type -- Right-hand side, mentioning these type vars.
106 -- Acts as a template for the expansion when
107 -- the tycon is applied to some types.
110 = ConsVisible -- whether or not data constructors are visible
111 | ConsInvisible -- outside their TyCon's defining module.
114 = NewType -- "newtype Blah ..."
115 | DataType -- "data Blah ..."
119 mkFunTyCon = FunTyCon
120 mkDataTyCon = DataTyCon
121 mkTupleTyCon = TupleTyCon
122 mkPrimTyCon = PrimTyCon
123 mkSpecTyCon = SpecTyCon
124 mkSynTyCon = SynTyCon
126 isFunTyCon FunTyCon = True
129 isPrimTyCon (PrimTyCon _ _ _) = True
130 isPrimTyCon _ = False
132 isVisibleDataTyCon (DataTyCon _ _ _ _ _ _ _ ConsVisible _) = True
133 isVisibleDataTyCon _ = False
137 -- Special cases to avoid reconstructing lots of kinds
138 kind1 = mkBoxedTypeKind `mkArrowKind` mkBoxedTypeKind
139 kind2 = mkBoxedTypeKind `mkArrowKind` kind1
141 getTyConKind :: TyCon -> Kind
142 getTyConKind FunTyCon = kind2
143 getTyConKind (DataTyCon _ kind _ _ _ _ _ _ _) = kind
144 getTyConKind (PrimTyCon _ _ kind) = kind
146 getTyConKind (SpecTyCon tc tys)
147 = spec (getTyConKind tc) tys
150 spec kind (Just _ : tys) = spec (resultKind kind) tys
151 spec kind (Nothing : tys) =
152 argKind kind `mkArrowKind` spec (resultKind kind) tys
154 getTyConKind (TupleTyCon n)
157 mkArrow 0 = mkBoxedTypeKind
160 mkArrow n = mkBoxedTypeKind `mkArrowKind` mkArrow (n-1)
164 getTyConUnique :: TyCon -> Unique
165 getTyConUnique FunTyCon = funTyConKey
166 getTyConUnique (DataTyCon uniq _ _ _ _ _ _ _ _) = uniq
167 getTyConUnique (TupleTyCon a) = mkTupleTyConUnique a
168 getTyConUnique (PrimTyCon uniq _ _) = uniq
169 getTyConUnique (SynTyCon uniq _ _ _ _ _) = uniq
170 getTyConUnique (SpecTyCon _ _ ) = panic "getTyConUnique:SpecTyCon"
174 getTyConTyVars :: TyCon -> [TyVar]
175 getTyConTyVars FunTyCon = [alphaTyVar,betaTyVar]
176 getTyConTyVars (DataTyCon _ _ _ tvs _ _ _ _ _) = tvs
177 getTyConTyVars (TupleTyCon arity) = take arity alphaTyVars
178 getTyConTyVars (SynTyCon _ _ _ _ tvs _) = tvs
179 getTyConTyVars (PrimTyCon _ _ _) = panic "getTyConTyVars:PrimTyCon"
180 getTyConTyVars (SpecTyCon _ _ ) = panic "getTyConTyVars:SpecTyCon"
184 getTyConDataCons :: TyCon -> [Id]
185 getTyConDataCons (DataTyCon _ _ _ _ _ data_cons _ _ _) = data_cons
186 getTyConDataCons (TupleTyCon a) = [mkTupleCon a]
190 getTyConDerivings :: TyCon -> [Class]
191 getTyConDerivings (DataTyCon _ _ _ _ _ _ derivs _ _) = derivs
195 getSynTyConArity :: TyCon -> Maybe Arity
196 getSynTyConArity (SynTyCon _ _ _ arity _ _) = Just arity
197 getSynTyConArity other = Nothing
201 maybeTyConSingleCon :: TyCon -> Maybe Id
202 maybeTyConSingleCon (TupleTyCon arity) = Just (mkTupleCon arity)
203 maybeTyConSingleCon (DataTyCon _ _ _ _ _ [c] _ _ _) = Just c
204 maybeTyConSingleCon (DataTyCon _ _ _ _ _ _ _ _ _) = Nothing
205 maybeTyConSingleCon (PrimTyCon _ _ _) = Nothing
206 maybeTyConSingleCon (SpecTyCon tc tys) = panic "maybeTyConSingleCon:SpecTyCon"
207 -- requires DataCons of TyCon
209 isEnumerationTyCon (TupleTyCon arity)
211 isEnumerationTyCon (DataTyCon _ _ _ _ _ data_cons _ _ _)
212 = not (null data_cons) && all is_nullary data_cons
214 is_nullary con = case (getDataConSig con) of { (_,_, arg_tys, _) ->
218 @derivedFor@ reports if we have an {\em obviously}-derived instance
219 for the given class/tycon. Of course, you might be deriving something
220 because it a superclass of some other obviously-derived class --- this
221 function doesn't deal with that.
223 ToDo: what about derivings for specialised tycons !!!
226 derivedFor :: Class -> TyCon -> Bool
227 derivedFor clas (DataTyCon _ _ _ _ _ _ derivs _ _) = isIn "derivedFor" clas derivs
228 derivedFor clas something_weird = False
231 %************************************************************************
233 \subsection[TyCon-instances]{Instance declarations for @TyCon@}
235 %************************************************************************
237 @TyCon@s are compared by comparing their @Unique@s.
239 The strictness analyser needs @Ord@. It is a lexicographic order with
240 the property @(a<=b) || (b<=a)@.
243 instance Ord3 TyCon where
244 cmp FunTyCon FunTyCon = EQ_
245 cmp (DataTyCon a _ _ _ _ _ _ _ _) (DataTyCon b _ _ _ _ _ _ _ _) = a `cmp` b
246 cmp (SynTyCon a _ _ _ _ _) (SynTyCon b _ _ _ _ _) = a `cmp` b
247 cmp (TupleTyCon a) (TupleTyCon b) = a `cmp` b
248 cmp (PrimTyCon a _ _) (PrimTyCon b _ _) = a `cmp` b
249 cmp (SpecTyCon tc1 mtys1) (SpecTyCon tc2 mtys2)
250 = panic# "cmp on SpecTyCons" -- case (tc1 `cmp` tc2) of { EQ_ -> mtys1 `cmp` mtys2; xxx -> xxx }
252 -- now we *know* the tags are different, so...
254 | tag1 _LT_ tag2 = LT_
257 tag1 = tag_TyCon other_1
258 tag2 = tag_TyCon other_2
259 tag_TyCon FunTyCon = ILIT(1)
260 tag_TyCon (DataTyCon _ _ _ _ _ _ _ _ _) = ILIT(2)
261 tag_TyCon (TupleTyCon _) = ILIT(3)
262 tag_TyCon (PrimTyCon _ _ _) = ILIT(4)
263 tag_TyCon (SpecTyCon _ _) = ILIT(5)
265 instance Eq TyCon where
266 a == b = case (a `cmp` b) of { EQ_ -> True; _ -> False }
267 a /= b = case (a `cmp` b) of { EQ_ -> False; _ -> True }
269 instance Ord TyCon where
270 a <= b = case (a `cmp` b) of { LT_ -> True; EQ_ -> True; GT__ -> False }
271 a < b = case (a `cmp` b) of { LT_ -> True; EQ_ -> False; GT__ -> False }
272 a >= b = case (a `cmp` b) of { LT_ -> False; EQ_ -> True; GT__ -> True }
273 a > b = case (a `cmp` b) of { LT_ -> False; EQ_ -> False; GT__ -> True }
274 _tagCmp a b = case (a `cmp` b) of { LT_ -> _LT; EQ_ -> _EQ; GT__ -> _GT }
278 instance NamedThing TyCon where
279 getExportFlag tc = case get_name tc of
280 Nothing -> NotExported
281 Just name -> getExportFlag name
284 isLocallyDefined tc = case get_name tc of
286 Just name -> isLocallyDefined name
288 getOrigName FunTyCon = (pRELUDE_BUILTIN, SLIT("(->)"))
289 getOrigName (TupleTyCon a) = (pRELUDE_BUILTIN, _PK_ ("Tuple" ++ show a))
290 getOrigName (SpecTyCon tc tys) = let (m,n) = getOrigName tc in
291 (m, n _APPEND_ specMaybeTysSuffix tys)
292 getOrigName other_tc = getOrigName (expectJust "tycon1" (get_name other_tc))
294 getOccurrenceName FunTyCon = SLIT("(->)")
295 getOccurrenceName (TupleTyCon 0) = SLIT("()")
296 getOccurrenceName (TupleTyCon a) = _PK_ ( "(" ++ nOfThem (a-1) ',' ++ ")" )
297 getOccurrenceName (SpecTyCon tc tys)= getOccurrenceName tc _APPEND_ specMaybeTysSuffix tys
298 getOccurrenceName other_tc = getOccurrenceName (expectJust "tycon2" (get_name other_tc))
300 getInformingModules tc = case get_name tc of
301 Nothing -> panic "getInformingModule:TyCon"
302 Just name -> getInformingModules name
304 getSrcLoc tc = case get_name tc of
305 Nothing -> mkBuiltinSrcLoc
306 Just name -> getSrcLoc name
308 getItsUnique tycon = getTyConUnique tycon
310 fromPreludeCore tc = case get_name tc of
312 Just name -> fromPreludeCore name
315 Emphatically un-exported:
318 get_name (DataTyCon _ _ n _ _ _ _ _ _) = Just n
319 get_name (PrimTyCon _ n _) = Just n
320 get_name (SpecTyCon tc _) = get_name tc
321 get_name (SynTyCon _ n _ _ _ _) = Just n
322 get_name other = Nothing