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 data HsBang = HsNoBang -- Only used as a return value for getBangStrictness,
106 -- never appears on a HsBangTy
108 | HsUnbox -- {-# UNPACK #-} ! (GHC extension, meaning "unbox")
109 deriving (Data, Typeable)
111 instance Outputable HsBang where
112 ppr (HsNoBang) = empty
113 ppr (HsStrict) = char '!'
114 ppr (HsUnbox) = ptext (sLit "!!")
116 getBangType :: LHsType a -> LHsType a
117 getBangType (L _ (HsBangTy _ ty)) = ty
120 getBangStrictness :: LHsType a -> HsBang
121 getBangStrictness (L _ (HsBangTy s _)) = s
122 getBangStrictness _ = HsNoBang
126 %************************************************************************
128 \subsection{Data types}
130 %************************************************************************
132 This is the syntax for types as seen in type signatures.
135 type LHsContext name = Located (HsContext name)
137 type HsContext name = [LHsPred name]
139 type LHsPred name = Located (HsPred name)
141 data HsPred name = HsClassP name [LHsType name] -- class constraint
142 | HsEqualP (LHsType name) (LHsType name)-- equality constraint
143 | HsIParam (IPName name) (LHsType name)
144 deriving (Data, Typeable)
146 type LHsType name = Located (HsType name)
149 = HsForAllTy HsExplicitFlag -- Renamer leaves this flag unchanged, to record the way
150 -- the user wrote it originally, so that the printer can
151 -- print it as the user wrote it
152 [LHsTyVarBndr name] -- With ImplicitForAll, this is the empty list
153 -- until the renamer fills in the variables
157 | HsTyVar name -- Type variable or type constructor
159 | HsAppTy (LHsType name)
162 | HsFunTy (LHsType name) -- function type
165 | HsListTy (LHsType name) -- Element type
167 | HsPArrTy (LHsType name) -- Elem. type of parallel array: [:t:]
170 [LHsType name] -- Element types (length gives arity)
172 | HsOpTy (LHsType name) (Located name) (LHsType name)
174 | HsParTy (LHsType name)
175 -- Parenthesis preserved for the precedence re-arrangement in RnTypes
176 -- It's important that a * (b + c) doesn't get rearranged to (a*b) + c!
178 -- However, NB that toHsType doesn't add HsParTys (in an effort to keep
179 -- interface files smaller), so when printing a HsType we may need to
182 | HsNumTy Integer -- Generics only
184 | HsPredTy (HsPred name) -- Only used in the type of an instance
185 -- declaration, eg. Eq [a] -> Eq a
188 -- Note no need for location info on the
189 -- enclosed HsPred; the one on the type will do
191 | HsKindSig (LHsType name) -- (ty :: kind)
192 Kind -- A type with a kind signature
194 | HsQuasiQuoteTy (HsQuasiQuote name)
196 | HsSpliceTy (HsSplice name)
197 FreeVars -- Variables free in the splice (filled in by renamer)
200 | HsDocTy (LHsType name) LHsDocString -- A documented type
202 | HsBangTy HsBang (LHsType name) -- Bang-style type annotations
203 | HsRecTy [ConDeclField name] -- Only in data type declarations
204 deriving (Data, Typeable)
206 data HsExplicitFlag = Explicit | Implicit deriving (Data, Typeable)
208 data ConDeclField name -- Record fields have Haddoc docs on them
209 = ConDeclField { cd_fld_name :: Located name,
210 cd_fld_type :: LBangType name,
211 cd_fld_doc :: Maybe LHsDocString }
212 deriving (Data, Typeable)
214 -----------------------
215 -- Combine adjacent for-alls.
216 -- The following awkward situation can happen otherwise:
217 -- f :: forall a. ((Num a) => Int)
218 -- might generate HsForAll (Just [a]) [] (HsForAll Nothing [Num a] t)
219 -- Then a isn't discovered as ambiguous, and we abstract the AbsBinds wrt []
220 -- but the export list abstracts f wrt [a]. Disaster.
222 -- A valid type must have one for-all at the top of the type, or of the fn arg types
224 mkImplicitHsForAllTy :: LHsContext name -> LHsType name -> HsType name
225 mkExplicitHsForAllTy :: [LHsTyVarBndr name] -> LHsContext name -> LHsType name -> HsType name
226 mkImplicitHsForAllTy ctxt ty = mkHsForAllTy Implicit [] ctxt ty
227 mkExplicitHsForAllTy tvs ctxt ty = mkHsForAllTy Explicit tvs ctxt ty
229 mkHsForAllTy :: HsExplicitFlag -> [LHsTyVarBndr name] -> LHsContext name -> LHsType name -> HsType name
230 -- Smart constructor for HsForAllTy
231 mkHsForAllTy exp tvs (L _ []) ty = mk_forall_ty exp tvs ty
232 mkHsForAllTy exp tvs ctxt ty = HsForAllTy exp tvs ctxt ty
234 -- mk_forall_ty makes a pure for-all type (no context)
235 mk_forall_ty :: HsExplicitFlag -> [LHsTyVarBndr name] -> LHsType name -> HsType name
236 mk_forall_ty exp tvs (L _ (HsParTy ty)) = mk_forall_ty exp tvs ty
237 mk_forall_ty exp1 tvs1 (L _ (HsForAllTy exp2 tvs2 ctxt ty)) = mkHsForAllTy (exp1 `plus` exp2) (tvs1 ++ tvs2) ctxt ty
238 mk_forall_ty exp tvs ty = HsForAllTy exp tvs (L noSrcSpan []) ty
239 -- Even if tvs is empty, we still make a HsForAll!
240 -- In the Implicit case, this signals the place to do implicit quantification
241 -- In the Explicit case, it prevents implicit quantification
242 -- (see the sigtype production in Parser.y.pp)
243 -- so that (forall. ty) isn't implicitly quantified
245 plus :: HsExplicitFlag -> HsExplicitFlag -> HsExplicitFlag
246 Implicit `plus` Implicit = Implicit
247 _ `plus` _ = Explicit
249 hsExplicitTvs :: LHsType name -> [name]
250 -- The explicitly-given forall'd type variables of a HsType
251 hsExplicitTvs (L _ (HsForAllTy Explicit tvs _ _)) = hsLTyVarNames tvs
254 ---------------------
255 type LHsTyVarBndr name = Located (HsTyVarBndr name)
257 data HsTyVarBndr name
258 = UserTyVar -- No explicit kinding
259 name -- See Note [Printing KindedTyVars]
265 -- *** NOTA BENE *** A "monotype" in a pragma can have
266 -- for-alls in it, (mostly to do with dictionaries). These
267 -- must be explicitly Kinded.
268 deriving (Data, Typeable)
270 hsTyVarName :: HsTyVarBndr name -> name
271 hsTyVarName (UserTyVar n _) = n
272 hsTyVarName (KindedTyVar n _) = n
274 hsTyVarKind :: HsTyVarBndr name -> Kind
275 hsTyVarKind (UserTyVar _ k) = k
276 hsTyVarKind (KindedTyVar _ k) = k
278 hsTyVarNameKind :: HsTyVarBndr name -> (name, Kind)
279 hsTyVarNameKind (UserTyVar n k) = (n,k)
280 hsTyVarNameKind (KindedTyVar n k) = (n,k)
282 hsLTyVarName :: LHsTyVarBndr name -> name
283 hsLTyVarName = hsTyVarName . unLoc
285 hsTyVarNames :: [HsTyVarBndr name] -> [name]
286 hsTyVarNames tvs = map hsTyVarName tvs
288 hsLTyVarNames :: [LHsTyVarBndr name] -> [name]
289 hsLTyVarNames = map hsLTyVarName
291 hsLTyVarLocName :: LHsTyVarBndr name -> Located name
292 hsLTyVarLocName = fmap hsTyVarName
294 hsLTyVarLocNames :: [LHsTyVarBndr name] -> [Located name]
295 hsLTyVarLocNames = map hsLTyVarLocName
297 replaceTyVarName :: HsTyVarBndr name1 -> name2 -> HsTyVarBndr name2
298 replaceTyVarName (UserTyVar _ k) n' = UserTyVar n' k
299 replaceTyVarName (KindedTyVar _ k) n' = KindedTyVar n' k
305 :: OutputableBndr name
307 -> ([LHsTyVarBndr name], HsContext name, name, [LHsType name])
308 -- Split up an instance decl type, returning the pieces
310 splitHsInstDeclTy inst_ty
312 HsParTy (L _ ty) -> splitHsInstDeclTy ty
313 HsForAllTy _ tvs cxt (L _ ty) -> split_tau tvs (unLoc cxt) ty
314 other -> split_tau [] [] other
315 -- The type vars should have been computed by now, even if they were implicit
317 split_tau tvs cxt (HsPredTy (HsClassP cls tys)) = (tvs, cxt, cls, tys)
318 split_tau tvs cxt (HsParTy (L _ ty)) = split_tau tvs cxt ty
319 split_tau _ _ _ = pprPanic "splitHsInstDeclTy" (ppr inst_ty)
321 -- Splits HsType into the (init, last) parts
322 -- Breaks up any parens in the result type:
323 -- splitHsFunType (a -> (b -> c)) = ([a,b], c)
324 splitHsFunType :: LHsType name -> ([LHsType name], LHsType name)
325 splitHsFunType (L _ (HsFunTy x y)) = (x:args, res)
327 (args, res) = splitHsFunType y
328 splitHsFunType (L _ (HsParTy ty)) = splitHsFunType ty
329 splitHsFunType other = ([], other)
333 %************************************************************************
335 \subsection{Pretty printing}
337 %************************************************************************
340 instance (OutputableBndr name) => Outputable (HsType name) where
341 ppr ty = pprHsType ty
343 instance (Outputable name) => Outputable (HsTyVarBndr name) where
344 ppr (UserTyVar name _) = ppr name
345 ppr (KindedTyVar name kind) = hsep [ppr name, dcolon, pprParendKind kind]
347 instance OutputableBndr name => Outputable (HsPred name) where
348 ppr (HsClassP clas tys) = ppr clas <+> hsep (map pprLHsType tys)
349 ppr (HsEqualP t1 t2) = hsep [pprLHsType t1, ptext (sLit "~"),
351 ppr (HsIParam n ty) = hsep [ppr n, dcolon, ppr ty]
353 pprLHsType :: OutputableBndr name => LHsType name -> SDoc
354 pprLHsType = pprParendHsType . unLoc
356 pprHsForAll :: OutputableBndr name => HsExplicitFlag -> [LHsTyVarBndr name] -> LHsContext name -> SDoc
357 pprHsForAll exp tvs cxt
358 | show_forall = forall_part <+> pprHsContext (unLoc cxt)
359 | otherwise = pprHsContext (unLoc cxt)
361 show_forall = opt_PprStyle_Debug
362 || (not (null tvs) && is_explicit)
363 is_explicit = case exp of {Explicit -> True; Implicit -> False}
364 forall_part = ptext (sLit "forall") <+> interppSP tvs <> dot
366 pprHsContext :: (OutputableBndr name) => HsContext name -> SDoc
367 pprHsContext [] = empty
368 pprHsContext cxt = ppr_hs_context cxt <+> ptext (sLit "=>")
370 ppr_hs_context :: (OutputableBndr name) => HsContext name -> SDoc
371 ppr_hs_context [] = empty
372 ppr_hs_context cxt = parens (interpp'SP cxt)
374 pprConDeclFields :: OutputableBndr name => [ConDeclField name] -> SDoc
375 pprConDeclFields fields = braces (sep (punctuate comma (map ppr_fld fields)))
377 ppr_fld (ConDeclField { cd_fld_name = n, cd_fld_type = ty,
379 = ppr n <+> dcolon <+> ppr ty <+> ppr_mbDoc doc
382 Note [Printing KindedTyVars]
383 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
384 Trac #3830 reminded me that we should really only print the kind
385 signature on a KindedTyVar if the kind signature was put there by the
386 programmer. During kind inference GHC now adds a PostTcKind to UserTyVars,
387 rather than converting to KindedTyVars as before.
389 (As it happens, the message in #3830 comes out a different way now,
390 and the problem doesn't show up; but having the flag on a KindedTyVar
391 seems like the Right Thing anyway.)
394 pREC_TOP, pREC_FUN, pREC_OP, pREC_CON :: Int
395 pREC_TOP = 0 -- type in ParseIface.y
396 pREC_FUN = 1 -- btype in ParseIface.y
397 -- Used for LH arg of (->)
398 pREC_OP = 2 -- Used for arg of any infix operator
399 -- (we don't keep their fixities around)
400 pREC_CON = 3 -- Used for arg of type applicn:
401 -- always parenthesise unless atomic
403 maybeParen :: Int -- Precedence of context
404 -> Int -- Precedence of top-level operator
405 -> SDoc -> SDoc -- Wrap in parens if (ctxt >= op)
406 maybeParen ctxt_prec op_prec p | ctxt_prec >= op_prec = parens p
409 -- printing works more-or-less as for Types
411 pprHsType, pprParendHsType :: (OutputableBndr name) => HsType name -> SDoc
413 pprHsType ty = getPprStyle $ \sty -> ppr_mono_ty pREC_TOP (prepare sty ty)
414 pprParendHsType ty = ppr_mono_ty pREC_CON ty
416 -- Before printing a type
417 -- (a) Remove outermost HsParTy parens
418 -- (b) Drop top-level for-all type variables in user style
419 -- since they are implicit in Haskell
420 prepare :: PprStyle -> HsType name -> HsType name
421 prepare sty (HsParTy ty) = prepare sty (unLoc ty)
424 ppr_mono_lty :: (OutputableBndr name) => Int -> LHsType name -> SDoc
425 ppr_mono_lty ctxt_prec ty = ppr_mono_ty ctxt_prec (unLoc ty)
427 ppr_mono_ty :: (OutputableBndr name) => Int -> HsType name -> SDoc
428 ppr_mono_ty ctxt_prec (HsForAllTy exp tvs ctxt ty)
429 = maybeParen ctxt_prec pREC_FUN $
430 sep [pprHsForAll exp tvs ctxt, ppr_mono_lty pREC_TOP ty]
432 ppr_mono_ty _ (HsBangTy b ty) = ppr b <> ppr ty
433 ppr_mono_ty _ (HsQuasiQuoteTy qq) = ppr qq
434 ppr_mono_ty _ (HsRecTy flds) = pprConDeclFields flds
435 ppr_mono_ty _ (HsTyVar name) = ppr name
436 ppr_mono_ty prec (HsFunTy ty1 ty2) = ppr_fun_ty prec ty1 ty2
437 ppr_mono_ty _ (HsTupleTy con tys) = tupleParens con (interpp'SP tys)
438 ppr_mono_ty _ (HsKindSig ty kind) = parens (ppr_mono_lty pREC_TOP ty <+> dcolon <+> pprKind kind)
439 ppr_mono_ty _ (HsListTy ty) = brackets (ppr_mono_lty pREC_TOP ty)
440 ppr_mono_ty _ (HsPArrTy ty) = pabrackets (ppr_mono_lty pREC_TOP ty)
441 ppr_mono_ty _ (HsPredTy pred) = ppr pred
442 ppr_mono_ty _ (HsNumTy n) = integer n -- generics only
443 ppr_mono_ty _ (HsSpliceTy s _ _) = pprSplice s
445 ppr_mono_ty ctxt_prec (HsAppTy fun_ty arg_ty)
446 = maybeParen ctxt_prec pREC_CON $
447 hsep [ppr_mono_lty pREC_FUN fun_ty, ppr_mono_lty pREC_CON arg_ty]
449 ppr_mono_ty ctxt_prec (HsOpTy ty1 op ty2)
450 = maybeParen ctxt_prec pREC_OP $
451 ppr_mono_lty pREC_OP ty1 <+> ppr op <+> ppr_mono_lty pREC_OP ty2
453 ppr_mono_ty _ (HsParTy ty)
454 = parens (ppr_mono_lty pREC_TOP ty)
455 -- Put the parens in where the user did
456 -- But we still use the precedence stuff to add parens because
457 -- toHsType doesn't put in any HsParTys, so we may still need them
459 ppr_mono_ty ctxt_prec (HsDocTy ty doc)
460 = maybeParen ctxt_prec pREC_OP $
461 ppr_mono_lty pREC_OP ty <+> ppr (unLoc doc)
462 -- we pretty print Haddock comments on types as if they were
465 --------------------------
466 ppr_fun_ty :: (OutputableBndr name) => Int -> LHsType name -> LHsType name -> SDoc
467 ppr_fun_ty ctxt_prec ty1 ty2
468 = let p1 = ppr_mono_lty pREC_FUN ty1
469 p2 = ppr_mono_lty pREC_TOP ty2
471 maybeParen ctxt_prec pREC_FUN $
472 sep [p1, ptext (sLit "->") <+> p2]
474 --------------------------
475 pabrackets :: SDoc -> SDoc
476 pabrackets p = ptext (sLit "[:") <> p <> ptext (sLit ":]")