2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[TyCon]{The @TyCon@ datatype}
8 TyCon, KindCon, Boxity(..),
10 isFunTyCon, isUnLiftedTyCon, isBoxedTyCon, isProductTyCon,
11 isAlgTyCon, isDataTyCon, isSynTyCon, isNewTyCon, isPrimTyCon,
12 isEnumerationTyCon, isTupleTyCon, isUnboxedTupleTyCon,
39 #include "HsVersions.h"
41 import {-# SOURCE #-} Type ( Type, Kind )
42 import {-# SOURCE #-} DataCon ( DataCon )
44 import Class ( Class )
46 import BasicTypes ( Arity, NewOrData(..), RecFlag(..) )
48 import Name ( Name, nameUnique, NamedThing(getName) )
49 import Unique ( Unique, Uniquable(..), superKindConKey )
50 import PrimRep ( PrimRep(..), isFollowableRep )
54 %************************************************************************
56 \subsection{The data type}
58 %************************************************************************
65 tyConUnique :: Unique,
72 | AlgTyCon { -- Tuples, data type, and newtype decls.
73 -- All lifted, all boxed
74 tyConUnique :: Unique,
79 tyConTyVars :: [TyVar],
80 dataTyConTheta :: [(Class,[Type])],
82 dataCons :: [DataCon],
83 -- 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
90 dataTyConDerivings :: [Class], -- Classes which have derived instances
92 dataTyConClass_maybe :: (Maybe Class), -- Nothing for ordinary types;
93 -- Just c for the type constructor
94 -- for dictionaries of class c.
95 algTyConFlavour :: NewOrData,
96 algTyConRec :: RecFlag -- Tells whether the data type is part of
97 -- a mutually-recursive group or not
100 | PrimTyCon { -- Primitive types; cannot be defined in Haskell
101 -- NB: All of these guys are *unlifted*, but not all are *unboxed*
102 tyConUnique :: Unique,
106 primTyConRep :: PrimRep
111 tyConUnique :: Unique,
116 tyConTyVars :: [TyVar],
121 tyConUnique :: Unique,
126 tyConTyVars :: [TyVar], -- Bound tyvars
127 synTyConDefn :: Type -- Right-hand side, mentioning these type vars.
128 -- Acts as a template for the expansion when
129 -- the tycon is applied to some types.
132 | KindCon { -- Type constructor at the kind level
133 tyConUnique :: Unique,
138 kindConBoxity :: Boxity
141 | SuperKindCon { -- The type of kind variables,
142 tyConUnique :: Unique -- sometimes written as a box
145 data Boxity = Boxed | Unboxed | Open
148 %************************************************************************
150 \subsection{TyCon Construction}
152 %************************************************************************
154 Note: the TyCon constructors all take a Kind as one argument, even though
155 they could, in principle, work out their Kind from their other arguments.
156 But to do so they need functions from Types, and that makes a nasty
157 module mutual-recursion. And they aren't called from many places.
158 So we compromise, and move their Kind calculation to the call site.
161 superKindCon = SuperKindCon superKindConKey
163 mkKindCon name kind boxity
165 tyConUnique = nameUnique name,
169 kindConBoxity = boxity
174 tyConUnique = nameUnique name,
180 mkAlgTyCon name kind tyvars theta cons derivs maybe_clas flavour rec
183 tyConUnique = nameUnique name,
185 tyConArity = length tyvars,
186 tyConTyVars = tyvars,
187 dataTyConTheta = theta,
189 dataTyConDerivings = derivs,
190 dataTyConClass_maybe = maybe_clas,
191 algTyConFlavour = flavour,
195 mkTupleTyCon name kind arity tyvars con boxed
197 tyConUnique = nameUnique name,
202 tyConTyVars = tyvars,
206 mkPrimTyCon name kind arity rep
209 tyConUnique = nameUnique name,
215 mkSynTyCon name kind arity tyvars rhs
218 tyConUnique = nameUnique name,
221 tyConTyVars = tyvars,
227 isFunTyCon (FunTyCon {}) = True
230 isPrimTyCon (PrimTyCon {}) = True
231 isPrimTyCon _ = False
233 isUnLiftedTyCon (PrimTyCon {}) = True
234 isUnLiftedTyCon (TupleTyCon { tyConBoxed = False }) = True
235 isUnLiftedTyCon _ = False
237 -- isBoxedTyCon should not be applied to SynTyCon, nor KindCon
238 isBoxedTyCon (AlgTyCon {}) = True
239 isBoxedTyCon (FunTyCon {}) = True
240 isBoxedTyCon (TupleTyCon {tyConBoxed = boxed}) = boxed
241 isBoxedTyCon (PrimTyCon {primTyConRep = rep}) = isFollowableRep rep
243 -- isAlgTyCon returns True for both @data@ and @newtype@
244 isAlgTyCon (AlgTyCon {}) = True
245 isAlgTyCon (TupleTyCon {}) = True
246 isAlgTyCon other = False
248 -- isDataTyCon returns False for @newtype@.
249 isDataTyCon (AlgTyCon {algTyConFlavour = new_or_data}) = case new_or_data of
252 isDataTyCon (TupleTyCon {}) = True -- is an unboxed tuple a datatype?
253 isDataTyCon other = False
255 isNewTyCon (AlgTyCon {algTyConFlavour = NewType}) = True
256 isNewTyCon other = False
258 -- A "product" tycon is non-recursive and has one constructor,
259 -- whether DataType or NewType
260 isProductTyCon (AlgTyCon {dataCons = [c], algTyConRec = NonRecursive}) = True
261 isProductTyCon (TupleTyCon {}) = True
262 isProductTyCon other = False
264 isSynTyCon (SynTyCon {}) = True
267 isEnumerationTyCon (AlgTyCon {algTyConFlavour = EnumType}) = True
268 isEnumerationTyCon other = False
270 -- The unit tycon isn't classed as a tuple tycon
271 isTupleTyCon (TupleTyCon {tyConArity = arity, tyConBoxed = True}) = arity >= 2
272 isTupleTyCon other = False
274 isUnboxedTupleTyCon (TupleTyCon {tyConBoxed = False}) = True
275 isUnboxedTupleTyCon other = False
279 tyConDataCons :: TyCon -> [DataCon]
280 tyConDataCons (AlgTyCon {dataCons = cons}) = cons
281 tyConDataCons (TupleTyCon {dataCon = con}) = [con]
282 tyConDataCons other = []
283 -- You may think this last equation should fail,
284 -- but it's quite convenient to return no constructors for
285 -- a synonym; see for example the call in TcTyClsDecls.
287 tyConFamilySize :: TyCon -> Int
288 tyConFamilySize (AlgTyCon {dataCons = cons}) = length cons
289 tyConFamilySize (TupleTyCon {}) = 1
291 tyConFamilySize other = pprPanic "tyConFamilySize:" (ppr other)
294 tyConPrimRep :: TyCon -> PrimRep
295 tyConPrimRep (PrimTyCon {primTyConRep = rep}) = rep
296 tyConPrimRep _ = PtrRep
300 tyConDerivings :: TyCon -> [Class]
301 tyConDerivings (AlgTyCon {dataTyConDerivings = derivs}) = derivs
302 tyConDerivings other = []
306 tyConTheta :: TyCon -> [(Class, [Type])]
307 tyConTheta (AlgTyCon {dataTyConTheta = theta}) = theta
308 -- should ask about anything else
312 getSynTyConDefn :: TyCon -> ([TyVar], Type)
313 getSynTyConDefn (SynTyCon {tyConTyVars = tyvars, synTyConDefn = ty}) = (tyvars,ty)
317 maybeTyConSingleCon :: TyCon -> Maybe DataCon
318 maybeTyConSingleCon (AlgTyCon {dataCons = [c]}) = Just c
319 maybeTyConSingleCon (AlgTyCon {}) = Nothing
320 maybeTyConSingleCon (TupleTyCon {dataCon = con}) = Just con
321 maybeTyConSingleCon (PrimTyCon {}) = Nothing
325 tyConClass_maybe :: TyCon -> Maybe Class
326 tyConClass_maybe (AlgTyCon {dataTyConClass_maybe = maybe_cls}) = maybe_cls
327 tyConClass_maybe other_tycon = Nothing
331 %************************************************************************
333 \subsection[TyCon-instances]{Instance declarations for @TyCon@}
335 %************************************************************************
337 @TyCon@s are compared by comparing their @Unique@s.
339 The strictness analyser needs @Ord@. It is a lexicographic order with
340 the property @(a<=b) || (b<=a)@.
343 instance Eq TyCon where
344 a == b = case (a `compare` b) of { EQ -> True; _ -> False }
345 a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
347 instance Ord TyCon where
348 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
349 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
350 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
351 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
352 compare a b = getUnique a `compare` getUnique b
354 instance Uniquable TyCon where
355 getUnique tc = tyConUnique tc
357 instance Outputable TyCon where
358 ppr tc = ppr (getName tc)
360 instance NamedThing TyCon where
365 %************************************************************************
367 \subsection{Kind constructors}
369 %************************************************************************
371 @matchesTyCon tc1 tc2@ checks whether an appliation
372 (tc1 t1..tn) matches (tc2 t1..tn). By "matches" we basically mean "equals",
373 except that at the kind level tc2 might have more boxity info that tc1.
375 E.g. It's ok to bind a type variable
381 matchesTyCon :: TyCon -- Expected (e.g. arg type of function)
382 -> TyCon -- Inferred (e.g. type of actual arg to function)
385 matchesTyCon (KindCon {kindConBoxity = k1}) (KindCon {kindConBoxity = k2})
388 -- "has_more" means has more boxity info
389 Boxed `has_more` Open = True
390 Boxed `has_more` Boxed = True
391 Unboxed `has_more` Open = True
392 Unboxed `has_more` Unboxed = True
393 Open `has_more` Open = True
394 k1 `has_more` k2 = False
396 matchesTyCon tc1 tc2 = tyConUnique tc1 == tyConUnique tc2