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
231 -- This printer is used for both interface files and
232 -- printing user types in error messages; and alas the
233 -- two use slightly different syntax. Ah well.
234 = getPprStyle $ \ sty ->
235 if userStyle sty then
236 ptext SLIT("forall") <+> interppSP tvs <> dot <+>
237 -- **! ToDo: want to hide uvars from user, but not enough info
238 -- in a HsTyVarBndr name (see PprType). KSW 2000-10.
240 else -- Used in interfaces
241 ptext SLIT("__forall") <+> interppSP tvs <+>
242 ppr_hs_context cxt <+> ptext SLIT("=>")
244 pprHsContext :: (Outputable name) => HsContext name -> SDoc
245 pprHsContext [] = empty
246 pprHsContext cxt = ppr_hs_context cxt <+> ptext SLIT("=>")
248 ppr_hs_context [] = empty
249 ppr_hs_context cxt = parens (interpp'SP cxt)
253 pREC_TOP = (0 :: Int) -- type in ParseIface.y
254 pREC_FUN = (1 :: Int) -- btype in ParseIface.y
255 -- Used for LH arg of (->)
256 pREC_OP = (2 :: Int) -- Used for arg of any infix operator
257 -- (we don't keep their fixities around)
258 pREC_CON = (3 :: Int) -- Used for arg of type applicn:
259 -- always parenthesise unless atomic
261 maybeParen :: Int -- Precedence of context
262 -> Int -- Precedence of top-level operator
263 -> SDoc -> SDoc -- Wrap in parens if (ctxt >= op)
264 maybeParen ctxt_prec op_prec p | ctxt_prec >= op_prec = parens p
267 -- printing works more-or-less as for Types
269 pprHsType, pprParendHsType :: (Outputable name) => HsType name -> SDoc
271 pprHsType ty = ppr_mono_ty pREC_TOP (de_paren ty)
272 pprParendHsType ty = ppr_mono_ty pREC_CON ty
274 -- Remove outermost HsParTy parens before printing a type
275 de_paren (HsParTy ty) = de_paren ty
278 ppr_mono_ty ctxt_prec (HsForAllTy maybe_tvs ctxt ty)
279 = maybeParen ctxt_prec pREC_FUN $
280 sep [pp_header, pprHsType ty]
282 pp_header = case maybe_tvs of
283 Just tvs -> pprHsForAll tvs ctxt
284 Nothing -> pprHsContext ctxt
286 ppr_mono_ty ctxt_prec (HsTyVar name) = ppr name
287 ppr_mono_ty ctxt_prec (HsFunTy ty1 ty2) = ppr_fun_ty ctxt_prec ty1 ty2
288 ppr_mono_ty ctxt_prec (HsTupleTy con tys) = tupleParens con (interpp'SP tys)
289 ppr_mono_ty ctxt_prec (HsKindSig ty kind) = parens (ppr_mono_ty pREC_TOP ty <+> dcolon <+> pprKind kind)
290 ppr_mono_ty ctxt_prec (HsListTy ty) = brackets (ppr_mono_ty pREC_TOP ty)
291 ppr_mono_ty ctxt_prec (HsPArrTy ty) = pabrackets (ppr_mono_ty pREC_TOP ty)
292 ppr_mono_ty ctxt_prec (HsPredTy pred) = braces (ppr pred)
293 ppr_mono_ty ctxt_prec (HsNumTy n) = integer n -- generics only
295 ppr_mono_ty ctxt_prec (HsAppTy fun_ty arg_ty)
296 = maybeParen ctxt_prec pREC_CON $
297 hsep [ppr_mono_ty pREC_FUN fun_ty, ppr_mono_ty pREC_CON arg_ty]
299 ppr_mono_ty ctxt_prec (HsOpTy ty1 op ty2)
300 = maybeParen ctxt_prec pREC_OP $
301 ppr_mono_ty pREC_OP ty1 <+> ppr op <+> ppr_mono_ty pREC_OP ty2
303 ppr_mono_ty ctxt_prec (HsParTy ty)
304 = parens (ppr_mono_ty pREC_TOP ty)
305 -- Put the parens in where the user did
306 -- But we still use the precedence stuff to add parens because
307 -- toHsType doesn't put in any HsParTys, so we may still need them
309 --------------------------
310 ppr_fun_ty ctxt_prec ty1 ty2
311 = let p1 = ppr_mono_ty pREC_FUN ty1
312 p2 = ppr_mono_ty pREC_TOP ty2
314 maybeParen ctxt_prec pREC_FUN $
315 sep [p1, ptext SLIT("->") <+> p2]
317 --------------------------
318 pabrackets p = ptext SLIT("[:") <> p <> ptext SLIT(":]")