2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
6 HsTypes: Abstract syntax: user-defined types
9 {-# LANGUAGE DeriveDataTypeable #-}
13 HsTyVarBndr(..), LHsTyVarBndr,
15 HsContext, LHsContext,
19 LBangType, BangType, HsBang(..),
20 getBangType, getBangStrictness,
22 ConDeclField(..), pprConDeclFields,
24 mkExplicitHsForAllTy, mkImplicitHsForAllTy, hsExplicitTvs,
25 hsTyVarName, hsTyVarNames, replaceTyVarName,
26 hsTyVarKind, hsTyVarNameKind,
27 hsLTyVarName, hsLTyVarNames, hsLTyVarLocName, hsLTyVarLocNames,
28 splitHsInstDeclTy, splitHsFunType,
31 PostTcType, placeHolderType, PostTcKind, placeHolderKind,
34 pprParendHsType, pprHsForAll, pprHsContext, ppr_hs_context,
37 import {-# SOURCE #-} HsExpr ( HsSplice, pprSplice )
39 import NameSet( FreeVars )
52 %************************************************************************
54 \subsection{Annotating the syntax}
56 %************************************************************************
59 type PostTcKind = Kind
60 type PostTcType = Type -- Used for slots in the abstract syntax
61 -- where we want to keep slot for a type
62 -- to be added by the type checker...but
63 -- before typechecking it's just bogus
65 placeHolderType :: PostTcType -- Used before typechecking
66 placeHolderType = panic "Evaluated the place holder for a PostTcType"
68 placeHolderKind :: PostTcKind -- Used before typechecking
69 placeHolderKind = panic "Evaluated the place holder for a PostTcKind"
72 %************************************************************************
74 Quasi quotes; used in types and elsewhere
76 %************************************************************************
79 data HsQuasiQuote id = HsQuasiQuote
80 id -- The quasi-quoter
81 SrcSpan -- The span of the enclosed string
82 FastString -- The enclosed string
83 deriving (Data, Typeable)
85 instance OutputableBndr id => Outputable (HsQuasiQuote id) where
88 ppr_qq :: OutputableBndr id => HsQuasiQuote id -> SDoc
89 ppr_qq (HsQuasiQuote quoter _ quote) =
90 char '[' <> ppr quoter <> ptext (sLit "|") <>
91 ppr quote <> ptext (sLit "|]")
95 %************************************************************************
97 \subsection{Bang annotations}
99 %************************************************************************
102 type LBangType name = Located (BangType name)
103 type BangType name = HsType name -- Bangs are in the HsType data type
105 getBangType :: LHsType a -> LHsType a
106 getBangType (L _ (HsBangTy _ ty)) = ty
109 getBangStrictness :: LHsType a -> HsBang
110 getBangStrictness (L _ (HsBangTy s _)) = s
111 getBangStrictness _ = HsNoBang
115 %************************************************************************
117 \subsection{Data types}
119 %************************************************************************
121 This is the syntax for types as seen in type signatures.
124 type LHsContext name = Located (HsContext name)
126 type HsContext name = [LHsPred name]
128 type LHsPred name = Located (HsPred name)
130 data HsPred name = HsClassP name [LHsType name] -- class constraint
131 | HsEqualP (LHsType name) (LHsType name)-- equality constraint
132 | HsIParam (IPName name) (LHsType name)
133 deriving (Data, Typeable)
135 type LHsType name = Located (HsType name)
138 = HsForAllTy HsExplicitFlag -- Renamer leaves this flag unchanged, to record the way
139 -- the user wrote it originally, so that the printer can
140 -- print it as the user wrote it
141 [LHsTyVarBndr name] -- With ImplicitForAll, this is the empty list
142 -- until the renamer fills in the variables
146 | HsTyVar name -- Type variable or type constructor
148 | HsAppTy (LHsType name)
151 | HsFunTy (LHsType name) -- function type
154 | HsListTy (LHsType name) -- Element type
156 | HsPArrTy (LHsType name) -- Elem. type of parallel array: [:t:]
158 | HsModalBoxType name (LHsType name) -- modal types; first argument is the environment classifier
161 [LHsType name] -- Element types (length gives arity)
163 | HsOpTy (LHsType name) (Located name) (LHsType name)
165 | HsParTy (LHsType name)
166 -- Parenthesis preserved for the precedence re-arrangement in RnTypes
167 -- It's important that a * (b + c) doesn't get rearranged to (a*b) + c!
169 -- However, NB that toHsType doesn't add HsParTys (in an effort to keep
170 -- interface files smaller), so when printing a HsType we may need to
173 | HsNumTy Integer -- Generics only
175 | HsPredTy (HsPred name) -- Only used in the type of an instance
176 -- declaration, eg. Eq [a] -> Eq a
179 -- Note no need for location info on the
180 -- Enclosed HsPred; the one on the type will do
182 | HsKindSig (LHsType name) -- (ty :: kind)
183 Kind -- A type with a kind signature
185 | HsQuasiQuoteTy (HsQuasiQuote name)
187 | HsSpliceTy (HsSplice name)
188 FreeVars -- Variables free in the splice (filled in by renamer)
191 | HsDocTy (LHsType name) LHsDocString -- A documented type
193 | HsBangTy HsBang (LHsType name) -- Bang-style type annotations
194 | HsRecTy [ConDeclField name] -- Only in data type declarations
196 | HsCoreTy Type -- An escape hatch for tunnelling a *closed*
197 -- Core Type through HsSyn.
199 deriving (Data, Typeable)
201 data HsExplicitFlag = Explicit | Implicit deriving (Data, Typeable)
203 data ConDeclField name -- Record fields have Haddoc docs on them
204 = ConDeclField { cd_fld_name :: Located name,
205 cd_fld_type :: LBangType name,
206 cd_fld_doc :: Maybe LHsDocString }
207 deriving (Data, Typeable)
209 -----------------------
210 -- Combine adjacent for-alls.
211 -- The following awkward situation can happen otherwise:
212 -- f :: forall a. ((Num a) => Int)
213 -- might generate HsForAll (Just [a]) [] (HsForAll Nothing [Num a] t)
214 -- Then a isn't discovered as ambiguous, and we abstract the AbsBinds wrt []
215 -- but the export list abstracts f wrt [a]. Disaster.
217 -- A valid type must have one for-all at the top of the type, or of the fn arg types
219 mkImplicitHsForAllTy :: LHsContext name -> LHsType name -> HsType name
220 mkExplicitHsForAllTy :: [LHsTyVarBndr name] -> LHsContext name -> LHsType name -> HsType name
221 mkImplicitHsForAllTy ctxt ty = mkHsForAllTy Implicit [] ctxt ty
222 mkExplicitHsForAllTy tvs ctxt ty = mkHsForAllTy Explicit tvs ctxt ty
224 mkHsForAllTy :: HsExplicitFlag -> [LHsTyVarBndr name] -> LHsContext name -> LHsType name -> HsType name
225 -- Smart constructor for HsForAllTy
226 mkHsForAllTy exp tvs (L _ []) ty = mk_forall_ty exp tvs ty
227 mkHsForAllTy exp tvs ctxt ty = HsForAllTy exp tvs ctxt ty
229 -- mk_forall_ty makes a pure for-all type (no context)
230 mk_forall_ty :: HsExplicitFlag -> [LHsTyVarBndr name] -> LHsType name -> HsType name
231 mk_forall_ty exp tvs (L _ (HsParTy ty)) = mk_forall_ty exp tvs ty
232 mk_forall_ty exp1 tvs1 (L _ (HsForAllTy exp2 tvs2 ctxt ty)) = mkHsForAllTy (exp1 `plus` exp2) (tvs1 ++ tvs2) ctxt ty
233 mk_forall_ty exp tvs ty = HsForAllTy exp tvs (L noSrcSpan []) ty
234 -- Even if tvs is empty, we still make a HsForAll!
235 -- In the Implicit case, this signals the place to do implicit quantification
236 -- In the Explicit case, it prevents implicit quantification
237 -- (see the sigtype production in Parser.y.pp)
238 -- so that (forall. ty) isn't implicitly quantified
240 plus :: HsExplicitFlag -> HsExplicitFlag -> HsExplicitFlag
241 Implicit `plus` Implicit = Implicit
242 _ `plus` _ = Explicit
244 hsExplicitTvs :: LHsType name -> [name]
245 -- The explicitly-given forall'd type variables of a HsType
246 hsExplicitTvs (L _ (HsForAllTy Explicit tvs _ _)) = hsLTyVarNames tvs
249 ---------------------
250 type LHsTyVarBndr name = Located (HsTyVarBndr name)
252 data HsTyVarBndr name
253 = UserTyVar -- No explicit kinding
254 name -- See Note [Printing KindedTyVars]
260 -- *** NOTA BENE *** A "monotype" in a pragma can have
261 -- for-alls in it, (mostly to do with dictionaries). These
262 -- must be explicitly Kinded.
263 deriving (Data, Typeable)
265 hsTyVarName :: HsTyVarBndr name -> name
266 hsTyVarName (UserTyVar n _) = n
267 hsTyVarName (KindedTyVar n _) = n
269 hsTyVarKind :: HsTyVarBndr name -> Kind
270 hsTyVarKind (UserTyVar _ k) = k
271 hsTyVarKind (KindedTyVar _ k) = k
273 hsTyVarNameKind :: HsTyVarBndr name -> (name, Kind)
274 hsTyVarNameKind (UserTyVar n k) = (n,k)
275 hsTyVarNameKind (KindedTyVar n k) = (n,k)
277 hsLTyVarName :: LHsTyVarBndr name -> name
278 hsLTyVarName = hsTyVarName . unLoc
280 hsTyVarNames :: [HsTyVarBndr name] -> [name]
281 hsTyVarNames tvs = map hsTyVarName tvs
283 hsLTyVarNames :: [LHsTyVarBndr name] -> [name]
284 hsLTyVarNames = map hsLTyVarName
286 hsLTyVarLocName :: LHsTyVarBndr name -> Located name
287 hsLTyVarLocName = fmap hsTyVarName
289 hsLTyVarLocNames :: [LHsTyVarBndr name] -> [Located name]
290 hsLTyVarLocNames = map hsLTyVarLocName
292 replaceTyVarName :: HsTyVarBndr name1 -> name2 -> HsTyVarBndr name2
293 replaceTyVarName (UserTyVar _ k) n' = UserTyVar n' k
294 replaceTyVarName (KindedTyVar _ k) n' = KindedTyVar n' k
300 :: OutputableBndr name
302 -> ([LHsTyVarBndr name], HsContext name, name, [LHsType name])
303 -- Split up an instance decl type, returning the pieces
305 splitHsInstDeclTy inst_ty
307 HsParTy (L _ ty) -> splitHsInstDeclTy ty
308 HsForAllTy _ tvs cxt (L _ ty) -> split_tau tvs (unLoc cxt) ty
309 other -> split_tau [] [] other
310 -- The type vars should have been computed by now, even if they were implicit
312 split_tau tvs cxt (HsPredTy (HsClassP cls tys)) = (tvs, cxt, cls, tys)
313 split_tau tvs cxt (HsParTy (L _ ty)) = split_tau tvs cxt ty
314 split_tau _ _ _ = pprPanic "splitHsInstDeclTy" (ppr inst_ty)
316 -- Splits HsType into the (init, last) parts
317 -- Breaks up any parens in the result type:
318 -- splitHsFunType (a -> (b -> c)) = ([a,b], c)
319 splitHsFunType :: LHsType name -> ([LHsType name], LHsType name)
320 splitHsFunType (L _ (HsFunTy x y)) = (x:args, res)
322 (args, res) = splitHsFunType y
323 splitHsFunType (L _ (HsParTy ty)) = splitHsFunType ty
324 splitHsFunType other = ([], other)
328 %************************************************************************
330 \subsection{Pretty printing}
332 %************************************************************************
335 instance (OutputableBndr name) => Outputable (HsType name) where
336 ppr ty = pprHsType ty
338 instance (Outputable name) => Outputable (HsTyVarBndr name) where
339 ppr (UserTyVar name _) = ppr name
340 ppr (KindedTyVar name kind) = hsep [ppr name, dcolon, pprParendKind kind]
342 instance OutputableBndr name => Outputable (HsPred name) where
343 ppr (HsClassP clas tys) = ppr clas <+> hsep (map pprLHsType tys)
344 ppr (HsEqualP t1 t2) = hsep [pprLHsType t1, ptext (sLit "~"),
346 ppr (HsIParam n ty) = hsep [ppr n, dcolon, ppr ty]
348 pprLHsType :: OutputableBndr name => LHsType name -> SDoc
349 pprLHsType = pprParendHsType . unLoc
351 pprHsForAll :: OutputableBndr name => HsExplicitFlag -> [LHsTyVarBndr name] -> LHsContext name -> SDoc
352 pprHsForAll exp tvs cxt
353 | show_forall = forall_part <+> pprHsContext (unLoc cxt)
354 | otherwise = pprHsContext (unLoc cxt)
356 show_forall = opt_PprStyle_Debug
357 || (not (null tvs) && is_explicit)
358 is_explicit = case exp of {Explicit -> True; Implicit -> False}
359 forall_part = ptext (sLit "forall") <+> interppSP tvs <> dot
361 pprHsContext :: (OutputableBndr name) => HsContext name -> SDoc
362 pprHsContext [] = empty
363 pprHsContext [L _ pred]
364 | noParenHsPred pred = ppr pred <+> darrow
365 pprHsContext cxt = ppr_hs_context cxt <+> darrow
367 noParenHsPred :: HsPred name -> Bool
368 -- c.f. TypeRep.noParenPred
369 noParenHsPred (HsClassP {}) = True
370 noParenHsPred (HsEqualP {}) = True
371 noParenHsPred (HsIParam {}) = False
373 ppr_hs_context :: (OutputableBndr name) => HsContext name -> SDoc
374 ppr_hs_context [] = empty
375 ppr_hs_context cxt = parens (interpp'SP cxt)
377 pprConDeclFields :: OutputableBndr name => [ConDeclField name] -> SDoc
378 pprConDeclFields fields = braces (sep (punctuate comma (map ppr_fld fields)))
380 ppr_fld (ConDeclField { cd_fld_name = n, cd_fld_type = ty,
382 = ppr n <+> dcolon <+> ppr ty <+> ppr_mbDoc doc
385 Note [Printing KindedTyVars]
386 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
387 Trac #3830 reminded me that we should really only print the kind
388 signature on a KindedTyVar if the kind signature was put there by the
389 programmer. During kind inference GHC now adds a PostTcKind to UserTyVars,
390 rather than converting to KindedTyVars as before.
392 (As it happens, the message in #3830 comes out a different way now,
393 and the problem doesn't show up; but having the flag on a KindedTyVar
394 seems like the Right Thing anyway.)
397 pREC_TOP, pREC_FUN, pREC_OP, pREC_CON :: Int
398 pREC_TOP = 0 -- type in ParseIface.y
399 pREC_FUN = 1 -- btype in ParseIface.y
400 -- Used for LH arg of (->)
401 pREC_OP = 2 -- Used for arg of any infix operator
402 -- (we don't keep their fixities around)
403 pREC_CON = 3 -- Used for arg of type applicn:
404 -- always parenthesise unless atomic
406 maybeParen :: Int -- Precedence of context
407 -> Int -- Precedence of top-level operator
408 -> SDoc -> SDoc -- Wrap in parens if (ctxt >= op)
409 maybeParen ctxt_prec op_prec p | ctxt_prec >= op_prec = parens p
412 -- printing works more-or-less as for Types
414 pprHsType, pprParendHsType :: (OutputableBndr name) => HsType name -> SDoc
416 pprHsType ty = getPprStyle $ \sty -> ppr_mono_ty pREC_TOP (prepare sty ty)
417 pprParendHsType ty = ppr_mono_ty pREC_CON ty
419 -- Before printing a type
420 -- (a) Remove outermost HsParTy parens
421 -- (b) Drop top-level for-all type variables in user style
422 -- since they are implicit in Haskell
423 prepare :: PprStyle -> HsType name -> HsType name
424 prepare sty (HsParTy ty) = prepare sty (unLoc ty)
427 ppr_mono_lty :: (OutputableBndr name) => Int -> LHsType name -> SDoc
428 ppr_mono_lty ctxt_prec ty = ppr_mono_ty ctxt_prec (unLoc ty)
430 ppr_mono_ty :: (OutputableBndr name) => Int -> HsType name -> SDoc
431 ppr_mono_ty ctxt_prec (HsForAllTy exp tvs ctxt ty)
432 = maybeParen ctxt_prec pREC_FUN $
433 sep [pprHsForAll exp tvs ctxt, ppr_mono_lty pREC_TOP ty]
435 ppr_mono_ty _ (HsBangTy b ty) = ppr b <> ppr ty
436 ppr_mono_ty _ (HsQuasiQuoteTy qq) = ppr qq
437 ppr_mono_ty _ (HsRecTy flds) = pprConDeclFields flds
438 ppr_mono_ty _ (HsTyVar name) = ppr name
439 ppr_mono_ty prec (HsFunTy ty1 ty2) = ppr_fun_ty prec ty1 ty2
440 ppr_mono_ty _ (HsTupleTy con tys) = tupleParens con (interpp'SP tys)
441 ppr_mono_ty _ (HsKindSig ty kind) = parens (ppr_mono_lty pREC_TOP ty <+> dcolon <+> pprKind kind)
442 ppr_mono_ty _ (HsListTy ty) = brackets (ppr_mono_lty pREC_TOP ty)
443 ppr_mono_ty _ (HsPArrTy ty) = pabrackets (ppr_mono_lty pREC_TOP ty)
444 ppr_mono_ty _ (HsModalBoxType ecn ty) = ppr_modalBoxType (ppr ecn) (ppr_mono_lty pREC_TOP ty)
445 ppr_mono_ty _ (HsPredTy pred) = ppr pred
446 ppr_mono_ty _ (HsNumTy n) = integer n -- generics only
447 ppr_mono_ty _ (HsSpliceTy s _ _) = pprSplice s
448 ppr_mono_ty _ (HsCoreTy ty) = ppr ty
450 ppr_mono_ty ctxt_prec (HsAppTy fun_ty arg_ty)
451 = maybeParen ctxt_prec pREC_CON $
452 hsep [ppr_mono_lty pREC_FUN fun_ty, ppr_mono_lty pREC_CON arg_ty]
454 ppr_mono_ty ctxt_prec (HsOpTy ty1 op ty2)
455 = maybeParen ctxt_prec pREC_OP $
456 ppr_mono_lty pREC_OP ty1 <+> ppr op <+> ppr_mono_lty pREC_OP ty2
458 ppr_mono_ty _ (HsParTy ty)
459 = parens (ppr_mono_lty pREC_TOP ty)
460 -- Put the parens in where the user did
461 -- But we still use the precedence stuff to add parens because
462 -- toHsType doesn't put in any HsParTys, so we may still need them
464 ppr_mono_ty ctxt_prec (HsDocTy ty doc)
465 = maybeParen ctxt_prec pREC_OP $
466 ppr_mono_lty pREC_OP ty <+> ppr (unLoc doc)
467 -- we pretty print Haddock comments on types as if they were
470 --------------------------
471 ppr_fun_ty :: (OutputableBndr name) => Int -> LHsType name -> LHsType name -> SDoc
472 ppr_fun_ty ctxt_prec ty1 ty2
473 = let p1 = ppr_mono_lty pREC_FUN ty1
474 p2 = ppr_mono_lty pREC_TOP ty2
476 maybeParen ctxt_prec pREC_FUN $
477 sep [p1, ptext (sLit "->") <+> p2]
479 --------------------------
480 pabrackets :: SDoc -> SDoc
481 pabrackets p = ptext (sLit "[:") <> p <> ptext (sLit ":]")
483 ppr_modalBoxType :: SDoc -> SDoc -> SDoc
484 ppr_modalBoxType ecn p = ptext (sLit "<[") <> p <> ptext (sLit "]>@") <> ecn