Fix CodingStyle#Warnings URLs
[ghc-hetmet.git] / compiler / hsSyn / HsTypes.lhs
1 %
2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 %
5
6 HsTypes: Abstract syntax: user-defined types
7
8 \begin{code}
9 {-# OPTIONS -w #-}
10 -- The above warning supression flag is a temporary kludge.
11 -- While working on this module you are encouraged to remove it and fix
12 -- any warnings in the module. See
13 --     http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
14 -- for details
15
16 module HsTypes (
17         HsType(..), LHsType, 
18         HsTyVarBndr(..), LHsTyVarBndr,
19         HsExplicitForAll(..),
20         HsContext, LHsContext,
21         HsPred(..), LHsPred,
22
23         LBangType, BangType, HsBang(..), 
24         getBangType, getBangStrictness, 
25         
26         mkExplicitHsForAllTy, mkImplicitHsForAllTy, hsExplicitTvs,
27         hsTyVarName, hsTyVarNames, replaceTyVarName,
28         hsLTyVarName, hsLTyVarNames, hsLTyVarLocName, hsLTyVarLocNames,
29         splitHsInstDeclTy, splitHsFunType,
30         
31         -- Type place holder
32         PostTcType, placeHolderType,
33
34         -- Printing
35         pprParendHsType, pprHsForAll, pprHsContext, ppr_hs_context, pprHsTyVarBndr
36     ) where
37
38 #include "HsVersions.h"
39
40 import {-# SOURCE #-} HsExpr ( HsSplice, pprSplice )
41
42 import Type
43 import HsDoc
44 import BasicTypes
45 import SrcLoc
46 import StaticFlags
47 import Outputable
48 \end{code}
49
50
51 %************************************************************************
52 %*                                                                      *
53 \subsection{Annotating the syntax}
54 %*                                                                      *
55 %************************************************************************
56
57 \begin{code}
58 type PostTcType = Type          -- Used for slots in the abstract syntax
59                                 -- where we want to keep slot for a type
60                                 -- to be added by the type checker...but
61                                 -- before typechecking it's just bogus
62
63 placeHolderType :: PostTcType   -- Used before typechecking
64 placeHolderType  = panic "Evaluated the place holder for a PostTcType"
65 \end{code}
66
67 %************************************************************************
68 %*                                                                      *
69 \subsection{Bang annotations}
70 %*                                                                      *
71 %************************************************************************
72
73 \begin{code}
74 type LBangType name = Located (BangType name)
75 type BangType name  = HsType name       -- Bangs are in the HsType data type
76
77 data HsBang = HsNoBang  -- Only used as a return value for getBangStrictness,
78                         -- never appears on a HsBangTy
79             | HsStrict  -- ! 
80             | HsUnbox   -- {-# UNPACK #-} ! (GHC extension, meaning "unbox")
81
82 instance Outputable HsBang where
83     ppr (HsNoBang) = empty
84     ppr (HsStrict) = char '!'
85     ppr (HsUnbox)  = ptext SLIT("!!")
86
87 getBangType :: LHsType a -> LHsType a
88 getBangType (L _ (HsBangTy _ ty)) = ty
89 getBangType ty                    = ty
90
91 getBangStrictness :: LHsType a -> HsBang
92 getBangStrictness (L _ (HsBangTy s _)) = s
93 getBangStrictness _                    = HsNoBang
94 \end{code}
95
96
97 %************************************************************************
98 %*                                                                      *
99 \subsection{Data types}
100 %*                                                                      *
101 %************************************************************************
102
103 This is the syntax for types as seen in type signatures.
104
105 \begin{code}
106 type LHsContext name = Located (HsContext name)
107
108 type HsContext name = [LHsPred name]
109
110 type LHsPred name = Located (HsPred name)
111
112 data HsPred name = HsClassP name [LHsType name]          -- class constraint
113                  | HsEqualP (LHsType name) (LHsType name)-- equality constraint
114                  | HsIParam (IPName name) (LHsType name)
115
116 type LHsType name = Located (HsType name)
117
118 data HsType name
119   = HsForAllTy  HsExplicitForAll        -- Renamer leaves this flag unchanged, to record the way
120                                         -- the user wrote it originally, so that the printer can
121                                         -- print it as the user wrote it
122                 [LHsTyVarBndr name]     -- With ImplicitForAll, this is the empty list
123                                         -- until the renamer fills in the variables
124                 (LHsContext name)
125                 (LHsType name)
126
127   | HsTyVar             name            -- Type variable or type constructor
128
129   | HsBangTy    HsBang (LHsType name)   -- Bang-style type annotations 
130
131   | HsAppTy             (LHsType name)
132                         (LHsType name)
133
134   | HsFunTy             (LHsType name)   -- function type
135                         (LHsType name)
136
137   | HsListTy            (LHsType name)  -- Element type
138
139   | HsPArrTy            (LHsType name)  -- Elem. type of parallel array: [:t:]
140
141   | HsTupleTy           Boxity
142                         [LHsType name]  -- Element types (length gives arity)
143
144   | HsOpTy              (LHsType name) (Located name) (LHsType name)
145
146   | HsParTy             (LHsType name)   
147         -- Parenthesis preserved for the precedence re-arrangement in RnTypes
148         -- It's important that a * (b + c) doesn't get rearranged to (a*b) + c!
149         -- 
150         -- However, NB that toHsType doesn't add HsParTys (in an effort to keep
151         -- interface files smaller), so when printing a HsType we may need to
152         -- add parens.  
153
154   | HsNumTy             Integer         -- Generics only
155
156   | HsPredTy            (HsPred name)   -- Only used in the type of an instance
157                                         -- declaration, eg.  Eq [a] -> Eq a
158                                         --                             ^^^^
159                                         --                            HsPredTy
160                                         -- Note no need for location info on the
161                                         -- enclosed HsPred; the one on the type will do
162
163   | HsKindSig           (LHsType name)  -- (ty :: kind)
164                         Kind            -- A type with a kind signature
165
166   | HsSpliceTy          (HsSplice name)
167
168   | HsDocTy             (LHsType name) (LHsDoc name) -- A documented type
169
170 data HsExplicitForAll = Explicit | Implicit
171
172 -----------------------
173 -- Combine adjacent for-alls. 
174 -- The following awkward situation can happen otherwise:
175 --      f :: forall a. ((Num a) => Int)
176 -- might generate HsForAll (Just [a]) [] (HsForAll Nothing [Num a] t)
177 -- Then a isn't discovered as ambiguous, and we abstract the AbsBinds wrt []
178 -- but the export list abstracts f wrt [a].  Disaster.
179 --
180 -- A valid type must have one for-all at the top of the type, or of the fn arg types
181
182 mkImplicitHsForAllTy     ctxt ty = mkHsForAllTy Implicit [] ctxt ty
183 mkExplicitHsForAllTy tvs ctxt ty = mkHsForAllTy Explicit tvs ctxt ty
184
185 mkHsForAllTy :: HsExplicitForAll -> [LHsTyVarBndr name] -> LHsContext name -> LHsType name -> HsType name
186 -- Smart constructor for HsForAllTy
187 mkHsForAllTy exp tvs (L _ []) ty = mk_forall_ty exp tvs ty
188 mkHsForAllTy exp tvs ctxt ty = HsForAllTy exp tvs ctxt ty
189
190 -- mk_forall_ty makes a pure for-all type (no context)
191 mk_forall_ty exp  tvs  (L _ (HsParTy ty))                   = mk_forall_ty exp tvs ty
192 mk_forall_ty exp1 tvs1 (L _ (HsForAllTy exp2 tvs2 ctxt ty)) = mkHsForAllTy (exp1 `plus` exp2) (tvs1 ++ tvs2) ctxt ty
193 mk_forall_ty exp  tvs  ty                                   = HsForAllTy exp tvs (L noSrcSpan []) ty
194         -- Even if tvs is empty, we still make a HsForAll!
195         -- In the Implicit case, this signals the place to do implicit quantification
196         -- In the Explicit case, it prevents implicit quantification    
197         --      (see the sigtype production in Parser.y.pp)
198         --      so that (forall. ty) isn't implicitly quantified
199
200 Implicit `plus` Implicit = Implicit
201 exp1     `plus` exp2     = Explicit
202
203 hsExplicitTvs :: LHsType name -> [name]
204 -- The explicitly-given forall'd type variables of a HsType
205 hsExplicitTvs (L _ (HsForAllTy Explicit tvs _ _)) = hsLTyVarNames tvs
206 hsExplicitTvs other                               = []
207
208 ---------------------
209 type LHsTyVarBndr name = Located (HsTyVarBndr name)
210
211 data HsTyVarBndr name
212   = UserTyVar name
213   | KindedTyVar name Kind
214         --  *** NOTA BENE *** A "monotype" in a pragma can have
215         -- for-alls in it, (mostly to do with dictionaries).  These
216         -- must be explicitly Kinded.
217
218 hsTyVarName :: HsTyVarBndr name -> name
219 hsTyVarName (UserTyVar n)     = n
220 hsTyVarName (KindedTyVar n _) = n
221
222 hsLTyVarName :: LHsTyVarBndr name -> name
223 hsLTyVarName = hsTyVarName . unLoc
224
225 hsTyVarNames :: [HsTyVarBndr name] -> [name]
226 hsTyVarNames tvs = map hsTyVarName tvs
227
228 hsLTyVarNames :: [LHsTyVarBndr name] -> [name]
229 hsLTyVarNames = map hsLTyVarName
230
231 hsLTyVarLocName :: LHsTyVarBndr name -> Located name
232 hsLTyVarLocName = fmap hsTyVarName
233
234 hsLTyVarLocNames :: [LHsTyVarBndr name] -> [Located name]
235 hsLTyVarLocNames = map hsLTyVarLocName
236
237 replaceTyVarName :: HsTyVarBndr name1 -> name2 -> HsTyVarBndr name2
238 replaceTyVarName (UserTyVar n)     n' = UserTyVar n'
239 replaceTyVarName (KindedTyVar n k) n' = KindedTyVar n' k
240 \end{code}
241
242
243 \begin{code}
244 splitHsInstDeclTy 
245     :: OutputableBndr name
246     => HsType name 
247     -> ([LHsTyVarBndr name], HsContext name, name, [LHsType name])
248         -- Split up an instance decl type, returning the pieces
249
250 splitHsInstDeclTy inst_ty
251   = case inst_ty of
252         HsParTy (L _ ty)              -> splitHsInstDeclTy ty
253         HsForAllTy _ tvs cxt (L _ ty) -> split_tau tvs (unLoc cxt) ty
254         other                         -> split_tau []  []          other
255     -- The type vars should have been computed by now, even if they were implicit
256   where
257     split_tau tvs cxt (HsPredTy (HsClassP cls tys)) = (tvs, cxt, cls, tys)
258     split_tau tvs cxt (HsParTy (L _ ty))            = split_tau tvs cxt ty
259     split_tau _ _ other = pprPanic "splitHsInstDeclTy" (ppr inst_ty)
260
261 -- Splits HsType into the (init, last) parts
262 -- Breaks up any parens in the result type: 
263 --      splitHsFunType (a -> (b -> c)) = ([a,b], c)
264 splitHsFunType :: LHsType name -> ([LHsType name], LHsType name)
265 splitHsFunType (L l (HsFunTy x y)) = (x:args, res)
266   where
267   (args, res) = splitHsFunType y
268 splitHsFunType (L _ (HsParTy ty))  = splitHsFunType ty
269 splitHsFunType other               = ([], other)
270 \end{code}
271
272
273 %************************************************************************
274 %*                                                                      *
275 \subsection{Pretty printing}
276 %*                                                                      *
277 %************************************************************************
278
279 \begin{code}
280 instance (OutputableBndr name) => Outputable (HsType name) where
281     ppr ty = pprHsType ty
282
283 instance (Outputable name) => Outputable (HsTyVarBndr name) where
284     ppr (UserTyVar name)        = ppr name
285     ppr (KindedTyVar name kind) = pprHsTyVarBndr name kind
286
287 instance OutputableBndr name => Outputable (HsPred name) where
288     ppr (HsClassP clas tys) = ppr clas <+> hsep (map pprLHsType tys)
289     ppr (HsEqualP t1 t2)    = hsep [pprLHsType t1, ptext SLIT("~"), 
290                                     pprLHsType t2]
291     ppr (HsIParam n ty)     = hsep [ppr n, dcolon, ppr ty]
292
293 pprLHsType :: OutputableBndr name => LHsType name -> SDoc
294 pprLHsType = pprParendHsType . unLoc
295
296 pprHsTyVarBndr :: Outputable name => name -> Kind -> SDoc
297 pprHsTyVarBndr name kind | isLiftedTypeKind kind = ppr name
298                          | otherwise             = hsep [ppr name, dcolon, pprParendKind kind]
299
300 pprHsForAll exp tvs cxt 
301   | show_forall = forall_part <+> pprHsContext (unLoc cxt)
302   | otherwise   = pprHsContext (unLoc cxt)
303   where
304     show_forall =  opt_PprStyle_Debug
305                 || (not (null tvs) && is_explicit)
306     is_explicit = case exp of {Explicit -> True; Implicit -> False}
307     forall_part = ptext SLIT("forall") <+> interppSP tvs <> dot
308
309 pprHsContext :: (OutputableBndr name) => HsContext name -> SDoc
310 pprHsContext []  = empty
311 pprHsContext cxt = ppr_hs_context cxt <+> ptext SLIT("=>")
312
313 ppr_hs_context []  = empty
314 ppr_hs_context cxt = parens (interpp'SP cxt)
315 \end{code}
316
317 \begin{code}
318 pREC_TOP = (0 :: Int)  -- type   in ParseIface.y
319 pREC_FUN = (1 :: Int)  -- btype  in ParseIface.y
320                         -- Used for LH arg of (->)
321 pREC_OP  = (2 :: Int)   -- Used for arg of any infix operator
322                         -- (we don't keep their fixities around)
323 pREC_CON = (3 :: Int)   -- Used for arg of type applicn: 
324                         -- always parenthesise unless atomic
325
326 maybeParen :: Int       -- Precedence of context
327            -> Int       -- Precedence of top-level operator
328            -> SDoc -> SDoc      -- Wrap in parens if (ctxt >= op)
329 maybeParen ctxt_prec op_prec p | ctxt_prec >= op_prec = parens p
330                                | otherwise            = p
331         
332 -- printing works more-or-less as for Types
333
334 pprHsType, pprParendHsType :: (OutputableBndr name) => HsType name -> SDoc
335
336 pprHsType ty       = getPprStyle $ \sty -> ppr_mono_ty pREC_TOP (prepare sty ty)
337 pprParendHsType ty = ppr_mono_ty pREC_CON ty
338
339 -- Before printing a type
340 -- (a) Remove outermost HsParTy parens
341 -- (b) Drop top-level for-all type variables in user style
342 --     since they are implicit in Haskell
343 prepare sty (HsParTy ty)          = prepare sty (unLoc ty)
344 prepare sty ty                    = ty
345
346 ppr_mono_lty ctxt_prec ty = ppr_mono_ty ctxt_prec (unLoc ty)
347
348 ppr_mono_ty ctxt_prec (HsForAllTy exp tvs ctxt ty)
349   = maybeParen ctxt_prec pREC_FUN $
350     sep [pprHsForAll exp tvs ctxt, ppr_mono_lty pREC_TOP ty]
351
352 ppr_mono_ty ctxt_prec (HsBangTy b ty)     = ppr b <> ppr ty
353 ppr_mono_ty ctxt_prec (HsTyVar name)      = ppr name
354 ppr_mono_ty ctxt_prec (HsFunTy ty1 ty2)   = ppr_fun_ty ctxt_prec ty1 ty2
355 ppr_mono_ty ctxt_prec (HsTupleTy con tys) = tupleParens con (interpp'SP tys)
356 ppr_mono_ty ctxt_prec (HsKindSig ty kind) = parens (ppr_mono_lty pREC_TOP ty <+> dcolon <+> pprKind kind)
357 ppr_mono_ty ctxt_prec (HsListTy ty)       = brackets (ppr_mono_lty pREC_TOP ty)
358 ppr_mono_ty ctxt_prec (HsPArrTy ty)       = pabrackets (ppr_mono_lty pREC_TOP ty)
359 ppr_mono_ty ctxt_prec (HsPredTy pred)     = ppr pred
360 ppr_mono_ty ctxt_prec (HsNumTy n)         = integer n  -- generics only
361 ppr_mono_ty ctxt_prec (HsSpliceTy s)      = pprSplice s
362
363 ppr_mono_ty ctxt_prec (HsAppTy fun_ty arg_ty)
364   = maybeParen ctxt_prec pREC_CON $
365     hsep [ppr_mono_lty pREC_FUN fun_ty, ppr_mono_lty pREC_CON arg_ty]
366
367 ppr_mono_ty ctxt_prec (HsOpTy ty1 op ty2)  
368   = maybeParen ctxt_prec pREC_OP $
369     ppr_mono_lty pREC_OP ty1 <+> ppr op <+> ppr_mono_lty pREC_OP ty2
370
371 ppr_mono_ty ctxt_prec (HsParTy ty)
372   = parens (ppr_mono_lty pREC_TOP ty)
373   -- Put the parens in where the user did
374   -- But we still use the precedence stuff to add parens because
375   --    toHsType doesn't put in any HsParTys, so we may still need them
376
377 ppr_mono_ty ctxt_prec (HsDocTy ty doc)
378   = ppr ty <+> ppr (unLoc doc)
379
380 --------------------------
381 ppr_fun_ty ctxt_prec ty1 ty2
382   = let p1 = ppr_mono_lty pREC_FUN ty1
383         p2 = ppr_mono_lty pREC_TOP ty2
384     in
385     maybeParen ctxt_prec pREC_FUN $
386     sep [p1, ptext SLIT("->") <+> p2]
387
388 --------------------------
389 pabrackets p = ptext SLIT("[:") <> p <> ptext SLIT(":]")
390 \end{code}
391
392