2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[HsTypes]{Abstract syntax: user-defined types}
8 HsType(..), HsTyVarBndr(..),
9 , HsContext, HsPred(..)
11 , mkHsForAllTy, mkHsDictTy, mkHsIParamTy
12 , hsTyVarName, hsTyVarNames, replaceTyVarName
16 , PostTcType, placeHolderType,
19 , SyntaxName, placeHolderName,
22 , pprParendHsType, pprHsForAll, pprHsContext, ppr_hs_context, pprHsTyVarBndr
25 #include "HsVersions.h"
27 import TcType ( Type, Kind, liftedTypeKind, eqKind )
28 import TypeRep ( Type )
29 import Name ( Name, mkInternalName )
30 import OccName ( mkVarOcc )
31 import PprType ( {- instance Outputable Kind -}, pprParendKind, pprKind )
32 import BasicTypes ( IPName, Boxity, tupleParens )
33 import PrelNames ( unboundKey )
34 import SrcLoc ( noSrcLoc )
39 %************************************************************************
41 \subsection{Annotating the syntax}
43 %************************************************************************
46 type PostTcType = Type -- Used for slots in the abstract syntax
47 -- where we want to keep slot for a type
48 -- to be added by the type checker...but
49 -- before typechecking it's just bogus
51 placeHolderType :: PostTcType -- Used before typechecking
52 placeHolderType = panic "Evaluated the place holder for a PostTcType"
55 type SyntaxName = Name -- These names are filled in by the renamer
56 -- Before then they are a placeHolderName (so that
57 -- we can still print the HsSyn)
58 -- They correspond to "rebindable syntax";
59 -- See RnEnv.lookupSyntaxName
61 placeHolderName :: SyntaxName
62 placeHolderName = mkInternalName unboundKey
63 (mkVarOcc FSLIT("syntaxPlaceHolder"))
68 %************************************************************************
70 \subsection{Data types}
72 %************************************************************************
74 This is the syntax for types as seen in type signatures.
77 type HsContext name = [HsPred name]
79 data HsPred name = HsClassP name [HsType name]
80 | HsIParam (IPName name) (HsType name)
83 = HsForAllTy (Maybe [HsTyVarBndr name]) -- Nothing for implicitly quantified signatures
87 | HsTyVar name -- Type variable or type constructor
89 | HsAppTy (HsType name)
92 | HsFunTy (HsType name) -- function type
95 | HsListTy (HsType name) -- Element type
97 | HsPArrTy (HsType name) -- Elem. type of parallel array: [:t:]
100 [HsType name] -- Element types (length gives arity)
102 | HsOpTy (HsType name) name (HsType name)
104 | HsParTy (HsType name)
105 -- Parenthesis preserved for the precedence re-arrangement in RnTypes
106 -- It's important that a * (b + c) doesn't get rearranged to (a*b) + c!
108 -- However, NB that toHsType doesn't add HsParTys (in an effort to keep
109 -- interface files smaller), so when printing a HsType we may need to
112 | HsNumTy Integer -- Generics only
114 -- these next two are only used in interfaces
115 | HsPredTy (HsPred name)
117 | HsKindSig (HsType name) -- (ty :: kind)
118 Kind -- A type with a kind signature
121 -----------------------
122 -- Combine adjacent for-alls.
123 -- The following awkward situation can happen otherwise:
124 -- f :: forall a. ((Num a) => Int)
125 -- might generate HsForAll (Just [a]) [] (HsForAll Nothing [Num a] t)
126 -- Then a isn't discovered as ambiguous, and we abstract the AbsBinds wrt []
127 -- but the export list abstracts f wrt [a]. Disaster.
129 -- A valid type must have one for-all at the top of the type, or of the fn arg types
131 mkHsForAllTy mtvs [] ty = mk_forall_ty mtvs ty
132 mkHsForAllTy mtvs ctxt ty = HsForAllTy mtvs ctxt ty
134 -- mk_forall_ty makes a pure for-all type (no context)
135 mk_forall_ty (Just []) ty = ty -- Explicit for-all with no tyvars
136 mk_forall_ty mtvs1 (HsParTy ty) = mk_forall_ty mtvs1 ty
137 mk_forall_ty mtvs1 (HsForAllTy mtvs2 ctxt ty) = mkHsForAllTy (mtvs1 `plus` mtvs2) ctxt ty
138 mk_forall_ty mtvs1 ty = HsForAllTy mtvs1 [] ty
140 mtvs1 `plus` Nothing = mtvs1
141 Nothing `plus` mtvs2 = mtvs2
142 (Just tvs1) `plus` (Just tvs2) = Just (tvs1 ++ tvs2)
144 mkHsDictTy cls tys = HsPredTy (HsClassP cls tys)
145 mkHsIParamTy v ty = HsPredTy (HsIParam v ty)
147 data HsTyVarBndr name
149 | KindedTyVar name Kind
150 -- *** NOTA BENE *** A "monotype" in a pragma can have
151 -- for-alls in it, (mostly to do with dictionaries). These
152 -- must be explicitly Kinded.
154 hsTyVarName (UserTyVar n) = n
155 hsTyVarName (KindedTyVar n _) = n
157 hsTyVarNames tvs = map hsTyVarName tvs
159 replaceTyVarName :: HsTyVarBndr name1 -> name2 -> HsTyVarBndr name2
160 replaceTyVarName (UserTyVar n) n' = UserTyVar n'
161 replaceTyVarName (KindedTyVar n k) n' = KindedTyVar n' k
169 -> ([HsTyVarBndr name], HsContext name, name, [HsType name])
170 -- Split up an instance decl type, returning the pieces
172 -- In interface files, the instance declaration head is created
173 -- by HsTypes.toHsType, which does not guarantee to produce a
174 -- HsForAllTy. For example, if we had the weird decl
175 -- instance Foo T => Foo [T]
176 -- then we'd get the instance type
178 -- So when colleting the instance context, to be on the safe side
179 -- we gather predicate arguments
181 -- For source code, the parser ensures the type will have the right shape.
182 -- (e.g. see ParseUtil.checkInstType)
184 splitHsInstDeclTy inst_ty
186 HsForAllTy (Just tvs) cxt1 tau
187 -> (tvs, cxt1++cxt2, cls, tys)
189 (cxt2, cls, tys) = split_tau tau
191 other -> ([], cxt2, cls, tys)
193 (cxt2, cls, tys) = split_tau inst_ty
196 split_tau (HsFunTy (HsPredTy p) ty) = (p:ps, cls, tys)
198 (ps, cls, tys) = split_tau ty
199 split_tau (HsPredTy (HsClassP cls tys)) = ([], cls,tys)
200 split_tau other = pprPanic "splitHsInstDeclTy" (ppr inst_ty)
204 %************************************************************************
206 \subsection{Pretty printing}
208 %************************************************************************
210 NB: these types get printed into interface files, so
211 don't change the printing format lightly
214 instance (Outputable name) => Outputable (HsType name) where
215 ppr ty = pprHsType ty
217 instance (Outputable name) => Outputable (HsTyVarBndr name) where
218 ppr (UserTyVar name) = ppr name
219 ppr (KindedTyVar name kind) = pprHsTyVarBndr name kind
221 instance Outputable name => Outputable (HsPred name) where
222 ppr (HsClassP clas tys) = ppr clas <+> hsep (map pprParendHsType tys)
223 ppr (HsIParam n ty) = hsep [ppr n, dcolon, ppr ty]
225 pprHsTyVarBndr :: Outputable name => name -> Kind -> SDoc
226 pprHsTyVarBndr name kind | kind `eqKind` liftedTypeKind = ppr name
227 | otherwise = hsep [ppr name, dcolon, pprParendKind kind]
229 pprHsForAll [] [] = empty
230 pprHsForAll tvs cxt = ptext SLIT("forall") <+> interppSP tvs <+> pprHsContext cxt
232 pprHsContext :: (Outputable name) => HsContext name -> SDoc
233 pprHsContext [] = empty
234 pprHsContext cxt = ppr_hs_context cxt <+> ptext SLIT("=>")
236 ppr_hs_context [] = empty
237 ppr_hs_context cxt = parens (interpp'SP cxt)
241 pREC_TOP = (0 :: Int) -- type in ParseIface.y
242 pREC_FUN = (1 :: Int) -- btype in ParseIface.y
243 -- Used for LH arg of (->)
244 pREC_OP = (2 :: Int) -- Used for arg of any infix operator
245 -- (we don't keep their fixities around)
246 pREC_CON = (3 :: Int) -- Used for arg of type applicn:
247 -- always parenthesise unless atomic
249 maybeParen :: Int -- Precedence of context
250 -> Int -- Precedence of top-level operator
251 -> SDoc -> SDoc -- Wrap in parens if (ctxt >= op)
252 maybeParen ctxt_prec op_prec p | ctxt_prec >= op_prec = parens p
255 -- printing works more-or-less as for Types
257 pprHsType, pprParendHsType :: (Outputable name) => HsType name -> SDoc
259 pprHsType ty = getPprStyle $ \sty -> ppr_mono_ty pREC_TOP (prepare sty ty)
260 pprParendHsType ty = ppr_mono_ty pREC_CON ty
262 -- Before printing a type
263 -- (a) Remove outermost HsParTy parens
264 -- (b) Drop top-level for-all type variables in user style
265 -- since they are implicit in Haskell
266 prepare sty (HsParTy ty) = prepare sty ty
267 prepare sty (HsForAllTy _ cxt ty) | userStyle sty = (HsForAllTy Nothing cxt ty)
270 ppr_mono_ty ctxt_prec (HsForAllTy maybe_tvs ctxt ty)
271 = maybeParen ctxt_prec pREC_FUN $
272 sep [pp_header, ppr_mono_ty pREC_TOP ty]
274 pp_header = case maybe_tvs of
275 Just tvs -> pprHsForAll tvs ctxt
276 Nothing -> pprHsContext ctxt
278 ppr_mono_ty ctxt_prec (HsTyVar name) = ppr name
279 ppr_mono_ty ctxt_prec (HsFunTy ty1 ty2) = ppr_fun_ty ctxt_prec ty1 ty2
280 ppr_mono_ty ctxt_prec (HsTupleTy con tys) = tupleParens con (interpp'SP tys)
281 ppr_mono_ty ctxt_prec (HsKindSig ty kind) = parens (ppr_mono_ty pREC_TOP ty <+> dcolon <+> pprKind kind)
282 ppr_mono_ty ctxt_prec (HsListTy ty) = brackets (ppr_mono_ty pREC_TOP ty)
283 ppr_mono_ty ctxt_prec (HsPArrTy ty) = pabrackets (ppr_mono_ty pREC_TOP ty)
284 ppr_mono_ty ctxt_prec (HsPredTy pred) = braces (ppr pred)
285 ppr_mono_ty ctxt_prec (HsNumTy n) = integer n -- generics only
287 ppr_mono_ty ctxt_prec (HsAppTy fun_ty arg_ty)
288 = maybeParen ctxt_prec pREC_CON $
289 hsep [ppr_mono_ty pREC_FUN fun_ty, ppr_mono_ty pREC_CON arg_ty]
291 ppr_mono_ty ctxt_prec (HsOpTy ty1 op ty2)
292 = maybeParen ctxt_prec pREC_OP $
293 ppr_mono_ty pREC_OP ty1 <+> ppr op <+> ppr_mono_ty pREC_OP ty2
295 ppr_mono_ty ctxt_prec (HsParTy ty)
296 = parens (ppr_mono_ty pREC_TOP ty)
297 -- Put the parens in where the user did
298 -- But we still use the precedence stuff to add parens because
299 -- toHsType doesn't put in any HsParTys, so we may still need them
301 --------------------------
302 ppr_fun_ty ctxt_prec ty1 ty2
303 = let p1 = ppr_mono_ty pREC_FUN ty1
304 p2 = ppr_mono_ty pREC_TOP ty2
306 maybeParen ctxt_prec pREC_FUN $
307 sep [p1, ptext SLIT("->") <+> p2]
309 --------------------------
310 pabrackets p = ptext SLIT("[:") <> p <> ptext SLIT(":]")