2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[HsTypes]{Abstract syntax: user-defined types}
9 HsTyVarBndr(..), LHsTyVarBndr,
11 HsContext, LHsContext,
14 LBangType, BangType, HsBang(..),
15 getBangType, getBangStrictness,
17 mkExplicitHsForAllTy, mkImplicitHsForAllTy,
18 hsTyVarName, hsTyVarNames, replaceTyVarName,
19 hsLTyVarName, hsLTyVarNames, hsLTyVarLocName, hsLTyVarLocNames,
20 splitHsInstDeclTy, splitHsFunType,
23 PostTcType, placeHolderType,
26 pprParendHsType, pprHsForAll, pprHsContext, ppr_hs_context, pprHsTyVarBndr
29 #include "HsVersions.h"
31 import {-# SOURCE #-} HsExpr ( HsSplice, pprSplice )
34 import Kind ( {- instance Outputable Kind -} Kind,
35 pprParendKind, pprKind, isLiftedTypeKind )
36 import BasicTypes ( IPName, Boxity, tupleParens )
37 import SrcLoc ( Located(..), unLoc, noSrcSpan )
38 import StaticFlags ( opt_PprStyle_Debug )
43 %************************************************************************
45 \subsection{Annotating the syntax}
47 %************************************************************************
50 type PostTcType = Type -- Used for slots in the abstract syntax
51 -- where we want to keep slot for a type
52 -- to be added by the type checker...but
53 -- before typechecking it's just bogus
55 placeHolderType :: PostTcType -- Used before typechecking
56 placeHolderType = panic "Evaluated the place holder for a PostTcType"
59 %************************************************************************
61 \subsection{Bang annotations}
63 %************************************************************************
66 type LBangType name = Located (BangType name)
67 type BangType name = HsType name -- Bangs are in the HsType data type
69 data HsBang = HsNoBang -- Only used as a return value for getBangStrictness,
70 -- never appears on a HsBangTy
72 | HsUnbox -- {-# UNPACK #-} ! (GHC extension, meaning "unbox")
74 instance Outputable HsBang where
75 ppr (HsNoBang) = empty
76 ppr (HsStrict) = char '!'
77 ppr (HsUnbox) = ptext SLIT("!!")
79 getBangType :: LHsType a -> LHsType a
80 getBangType (L _ (HsBangTy _ ty)) = ty
83 getBangStrictness :: LHsType a -> HsBang
84 getBangStrictness (L _ (HsBangTy s _)) = s
85 getBangStrictness _ = HsNoBang
89 %************************************************************************
91 \subsection{Data types}
93 %************************************************************************
95 This is the syntax for types as seen in type signatures.
98 type LHsContext name = Located (HsContext name)
100 type HsContext name = [LHsPred name]
102 type LHsPred name = Located (HsPred name)
104 data HsPred name = HsClassP name [LHsType name]
105 | HsIParam (IPName name) (LHsType name)
107 type LHsType name = Located (HsType name)
110 = HsForAllTy HsExplicitForAll -- Renamer leaves this flag unchanged, to record the way
111 -- the user wrote it originally, so that the printer can
112 -- print it as the user wrote it
113 [LHsTyVarBndr name] -- With ImplicitForAll, this is the empty list
114 -- until the renamer fills in the variables
118 | HsTyVar name -- Type variable or type constructor
120 | HsBangTy HsBang (LHsType name) -- Bang-style type annotations
122 | HsAppTy (LHsType name)
125 | HsFunTy (LHsType name) -- function type
128 | HsListTy (LHsType name) -- Element type
130 | HsPArrTy (LHsType name) -- Elem. type of parallel array: [:t:]
133 [LHsType name] -- Element types (length gives arity)
135 | HsOpTy (LHsType name) (Located name) (LHsType name)
137 | HsParTy (LHsType name)
138 -- Parenthesis preserved for the precedence re-arrangement in RnTypes
139 -- It's important that a * (b + c) doesn't get rearranged to (a*b) + c!
141 -- However, NB that toHsType doesn't add HsParTys (in an effort to keep
142 -- interface files smaller), so when printing a HsType we may need to
145 | HsNumTy Integer -- Generics only
147 | HsPredTy (HsPred name) -- Only used in the type of an instance
148 -- declaration, eg. Eq [a] -> Eq a
151 -- Note no need for location info on the
152 -- enclosed HsPred; the one on the type will do
154 | HsKindSig (LHsType name) -- (ty :: kind)
155 Kind -- A type with a kind signature
157 | HsSpliceTy (HsSplice name)
159 data HsExplicitForAll = Explicit | Implicit
161 -----------------------
162 -- Combine adjacent for-alls.
163 -- The following awkward situation can happen otherwise:
164 -- f :: forall a. ((Num a) => Int)
165 -- might generate HsForAll (Just [a]) [] (HsForAll Nothing [Num a] t)
166 -- Then a isn't discovered as ambiguous, and we abstract the AbsBinds wrt []
167 -- but the export list abstracts f wrt [a]. Disaster.
169 -- A valid type must have one for-all at the top of the type, or of the fn arg types
171 mkImplicitHsForAllTy ctxt ty = mkHsForAllTy Implicit [] ctxt ty
172 mkExplicitHsForAllTy tvs ctxt ty = mkHsForAllTy Explicit tvs ctxt ty
174 mkHsForAllTy :: HsExplicitForAll -> [LHsTyVarBndr name] -> LHsContext name -> LHsType name -> HsType name
175 -- Smart constructor for HsForAllTy
176 mkHsForAllTy exp tvs (L _ []) ty = mk_forall_ty exp tvs ty
177 mkHsForAllTy exp tvs ctxt ty = HsForAllTy exp tvs ctxt ty
179 -- mk_forall_ty makes a pure for-all type (no context)
180 mk_forall_ty exp tvs (L _ (HsParTy ty)) = mk_forall_ty exp tvs ty
181 mk_forall_ty exp1 tvs1 (L _ (HsForAllTy exp2 tvs2 ctxt ty)) = mkHsForAllTy (exp1 `plus` exp2) (tvs1 ++ tvs2) ctxt ty
182 mk_forall_ty exp tvs ty = HsForAllTy exp tvs (L noSrcSpan []) ty
183 -- Even if tvs is empty, we still make a HsForAll!
184 -- In the Implicit case, this signals the place to do implicit quantification
185 -- In the Explicit case, it prevents implicit quantification
186 -- (see the sigtype production in Parser.y.pp)
187 -- so that (forall. ty) isn't implicitly quantified
189 Implicit `plus` Implicit = Implicit
190 exp1 `plus` exp2 = Explicit
192 type LHsTyVarBndr name = Located (HsTyVarBndr name)
194 data HsTyVarBndr name
196 | KindedTyVar name Kind
197 -- *** NOTA BENE *** A "monotype" in a pragma can have
198 -- for-alls in it, (mostly to do with dictionaries). These
199 -- must be explicitly Kinded.
201 hsTyVarName :: HsTyVarBndr name -> name
202 hsTyVarName (UserTyVar n) = n
203 hsTyVarName (KindedTyVar n _) = n
205 hsLTyVarName :: LHsTyVarBndr name -> name
206 hsLTyVarName = hsTyVarName . unLoc
208 hsTyVarNames :: [HsTyVarBndr name] -> [name]
209 hsTyVarNames tvs = map hsTyVarName tvs
211 hsLTyVarNames :: [LHsTyVarBndr name] -> [name]
212 hsLTyVarNames = map hsLTyVarName
214 hsLTyVarLocName :: LHsTyVarBndr name -> Located name
215 hsLTyVarLocName = fmap hsTyVarName
217 hsLTyVarLocNames :: [LHsTyVarBndr name] -> [Located name]
218 hsLTyVarLocNames = map hsLTyVarLocName
220 replaceTyVarName :: HsTyVarBndr name1 -> name2 -> HsTyVarBndr name2
221 replaceTyVarName (UserTyVar n) n' = UserTyVar n'
222 replaceTyVarName (KindedTyVar n k) n' = KindedTyVar n' k
228 :: OutputableBndr name
230 -> ([LHsTyVarBndr name], HsContext name, name, [LHsType name])
231 -- Split up an instance decl type, returning the pieces
233 splitHsInstDeclTy inst_ty
235 HsParTy (L _ ty) -> splitHsInstDeclTy ty
236 HsForAllTy _ tvs cxt (L _ ty) -> split_tau tvs (unLoc cxt) ty
237 other -> split_tau [] [] other
238 -- The type vars should have been computed by now, even if they were implicit
240 split_tau tvs cxt (HsPredTy (HsClassP cls tys)) = (tvs, cxt, cls, tys)
241 split_tau tvs cxt (HsParTy (L _ ty)) = split_tau tvs cxt ty
243 -- Splits HsType into the (init, last) parts
244 -- Breaks up any parens in the result type:
245 -- splitHsFunType (a -> (b -> c)) = ([a,b], c)
246 splitHsFunType :: LHsType name -> ([LHsType name], LHsType name)
247 splitHsFunType (L l (HsFunTy x y)) = (x:args, res)
249 (args, res) = splitHsFunType y
250 splitHsFunType (L _ (HsParTy ty)) = splitHsFunType ty
251 splitHsFunType other = ([], other)
255 %************************************************************************
257 \subsection{Pretty printing}
259 %************************************************************************
261 NB: these types get printed into interface files, so
262 don't change the printing format lightly
265 instance (OutputableBndr name) => Outputable (HsType name) where
266 ppr ty = pprHsType ty
268 instance (Outputable name) => Outputable (HsTyVarBndr name) where
269 ppr (UserTyVar name) = ppr name
270 ppr (KindedTyVar name kind) = pprHsTyVarBndr name kind
272 instance OutputableBndr name => Outputable (HsPred name) where
273 ppr (HsClassP clas tys) = ppr clas <+> hsep (map (pprParendHsType.unLoc) tys)
274 ppr (HsIParam n ty) = hsep [ppr n, dcolon, ppr ty]
276 pprHsTyVarBndr :: Outputable name => name -> Kind -> SDoc
277 pprHsTyVarBndr name kind | isLiftedTypeKind kind = ppr name
278 | otherwise = hsep [ppr name, dcolon, pprParendKind kind]
280 pprHsForAll exp tvs cxt
281 | show_forall = forall_part <+> pprHsContext (unLoc cxt)
282 | otherwise = pprHsContext (unLoc cxt)
284 show_forall = opt_PprStyle_Debug
285 || (not (null tvs) && is_explicit)
286 is_explicit = case exp of {Explicit -> True; Implicit -> False}
287 forall_part = ptext SLIT("forall") <+> interppSP tvs <> dot
289 pprHsContext :: (OutputableBndr name) => HsContext name -> SDoc
290 pprHsContext [] = empty
291 pprHsContext cxt = ppr_hs_context cxt <+> ptext SLIT("=>")
293 ppr_hs_context [] = empty
294 ppr_hs_context cxt = parens (interpp'SP cxt)
298 pREC_TOP = (0 :: Int) -- type in ParseIface.y
299 pREC_FUN = (1 :: Int) -- btype in ParseIface.y
300 -- Used for LH arg of (->)
301 pREC_OP = (2 :: Int) -- Used for arg of any infix operator
302 -- (we don't keep their fixities around)
303 pREC_CON = (3 :: Int) -- Used for arg of type applicn:
304 -- always parenthesise unless atomic
306 maybeParen :: Int -- Precedence of context
307 -> Int -- Precedence of top-level operator
308 -> SDoc -> SDoc -- Wrap in parens if (ctxt >= op)
309 maybeParen ctxt_prec op_prec p | ctxt_prec >= op_prec = parens p
312 -- printing works more-or-less as for Types
314 pprHsType, pprParendHsType :: (OutputableBndr name) => HsType name -> SDoc
316 pprHsType ty = getPprStyle $ \sty -> ppr_mono_ty pREC_TOP (prepare sty ty)
317 pprParendHsType ty = ppr_mono_ty pREC_CON ty
319 -- Before printing a type
320 -- (a) Remove outermost HsParTy parens
321 -- (b) Drop top-level for-all type variables in user style
322 -- since they are implicit in Haskell
323 prepare sty (HsParTy ty) = prepare sty (unLoc ty)
326 ppr_mono_lty ctxt_prec ty = ppr_mono_ty ctxt_prec (unLoc ty)
328 ppr_mono_ty ctxt_prec (HsForAllTy exp tvs ctxt ty)
329 = maybeParen ctxt_prec pREC_FUN $
330 sep [pprHsForAll exp tvs ctxt, ppr_mono_lty pREC_TOP ty]
333 ppr_mono_ty ctxt_prec (HsBangTy b ty) = ppr b <> ppr ty
334 ppr_mono_ty ctxt_prec (HsTyVar name) = ppr name
335 ppr_mono_ty ctxt_prec (HsFunTy ty1 ty2) = ppr_fun_ty ctxt_prec ty1 ty2
336 ppr_mono_ty ctxt_prec (HsTupleTy con tys) = tupleParens con (interpp'SP tys)
337 ppr_mono_ty ctxt_prec (HsKindSig ty kind) = parens (ppr_mono_lty pREC_TOP ty <+> dcolon <+> pprKind kind)
338 ppr_mono_ty ctxt_prec (HsListTy ty) = brackets (ppr_mono_lty pREC_TOP ty)
339 ppr_mono_ty ctxt_prec (HsPArrTy ty) = pabrackets (ppr_mono_lty pREC_TOP ty)
340 ppr_mono_ty ctxt_prec (HsPredTy pred) = braces (ppr pred)
341 ppr_mono_ty ctxt_prec (HsNumTy n) = integer n -- generics only
342 ppr_mono_ty ctxt_prec (HsSpliceTy s) = pprSplice s
344 ppr_mono_ty ctxt_prec (HsAppTy fun_ty arg_ty)
345 = maybeParen ctxt_prec pREC_CON $
346 hsep [ppr_mono_lty pREC_FUN fun_ty, ppr_mono_lty pREC_CON arg_ty]
348 ppr_mono_ty ctxt_prec (HsOpTy ty1 op ty2)
349 = maybeParen ctxt_prec pREC_OP $
350 ppr_mono_lty pREC_OP ty1 <+> ppr op <+> ppr_mono_lty pREC_OP ty2
352 ppr_mono_ty ctxt_prec (HsParTy ty)
353 = parens (ppr_mono_lty pREC_TOP ty)
354 -- Put the parens in where the user did
355 -- But we still use the precedence stuff to add parens because
356 -- toHsType doesn't put in any HsParTys, so we may still need them
358 --------------------------
359 ppr_fun_ty ctxt_prec ty1 ty2
360 = let p1 = ppr_mono_lty pREC_FUN ty1
361 p2 = ppr_mono_lty pREC_TOP ty2
363 maybeParen ctxt_prec pREC_FUN $
364 sep [p1, ptext SLIT("->") <+> p2]
366 --------------------------
367 pabrackets p = ptext SLIT("[:") <> p <> ptext SLIT(":]")