never executed always true always false
1
2 {-# LANGUAGE ConstraintKinds #-}
3 {-# LANGUAGE DeriveTraversable #-}
4 {-# LANGUAGE FlexibleContexts #-}
5 {-# LANGUAGE FlexibleInstances #-}
6 {-# LANGUAGE ScopedTypeVariables #-}
7 {-# LANGUAGE TypeApplications #-}
8 {-# LANGUAGE TypeFamilies #-}
9 {-# LANGUAGE UndecidableInstances #-} -- Wrinkle in Note [Trees That Grow]
10 -- in module Language.Haskell.Syntax.Extension
11 {-# LANGUAGE ViewPatterns #-}
12 {-# LANGUAGE DataKinds #-}
13 {-
14 (c) The University of Glasgow 2006
15 (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
16
17 \section[PatSyntax]{Abstract Haskell syntax---patterns}
18 -}
19
20 -- See Note [Language.Haskell.Syntax.* Hierarchy] for why not GHC.Hs.*
21 module Language.Haskell.Syntax.Pat (
22 Pat(..), LPat,
23 ConLikeP,
24
25 HsConPatDetails, hsConPatArgs,
26 HsRecFields(..), HsFieldBind(..), LHsFieldBind,
27 HsRecField, LHsRecField,
28 HsRecUpdField, LHsRecUpdField,
29 hsRecFields, hsRecFieldSel, hsRecFieldsArgs,
30 ) where
31
32 import GHC.Prelude
33
34 import {-# SOURCE #-} Language.Haskell.Syntax.Expr (SyntaxExpr, LHsExpr, HsSplice)
35
36 -- friends:
37 import Language.Haskell.Syntax.Lit
38 import Language.Haskell.Syntax.Extension
39 import Language.Haskell.Syntax.Type
40 import GHC.Types.Basic
41 -- others:
42 import GHC.Core.Ppr ( {- instance OutputableBndr TyVar -} )
43 import GHC.Utils.Outputable
44 import GHC.Types.SrcLoc
45 -- libraries:
46
47 type LPat p = XRec p (Pat p)
48
49 -- | Pattern
50 --
51 -- - 'GHC.Parser.Annotation.AnnKeywordId' : 'GHC.Parser.Annotation.AnnBang'
52
53 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation
54 data Pat p
55 = ------------ Simple patterns ---------------
56 WildPat (XWildPat p) -- ^ Wildcard Pattern
57 -- The sole reason for a type on a WildPat is to
58 -- support hsPatType :: Pat Id -> Type
59
60 -- AZ:TODO above comment needs to be updated
61 | VarPat (XVarPat p)
62 (LIdP p) -- ^ Variable Pattern
63
64 -- See Note [Located RdrNames] in GHC.Hs.Expr
65 | LazyPat (XLazyPat p)
66 (LPat p) -- ^ Lazy Pattern
67 -- ^ - 'GHC.Parser.Annotation.AnnKeywordId' : 'GHC.Parser.Annotation.AnnTilde'
68
69 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation
70
71 | AsPat (XAsPat p)
72 (LIdP p) (LPat p) -- ^ As pattern
73 -- ^ - 'GHC.Parser.Annotation.AnnKeywordId' : 'GHC.Parser.Annotation.AnnAt'
74
75 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation
76
77 | ParPat (XParPat p)
78 !(LHsToken "(" p)
79 (LPat p) -- ^ Parenthesised pattern
80 !(LHsToken ")" p)
81 -- See Note [Parens in HsSyn] in GHC.Hs.Expr
82 -- ^ - 'GHC.Parser.Annotation.AnnKeywordId' : 'GHC.Parser.Annotation.AnnOpen' @'('@,
83 -- 'GHC.Parser.Annotation.AnnClose' @')'@
84
85 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation
86 | BangPat (XBangPat p)
87 (LPat p) -- ^ Bang pattern
88 -- ^ - 'GHC.Parser.Annotation.AnnKeywordId' : 'GHC.Parser.Annotation.AnnBang'
89
90 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation
91
92 ------------ Lists, tuples, arrays ---------------
93 | ListPat (XListPat p)
94 [LPat p]
95
96 -- ^ Syntactic List
97 --
98 -- - 'GHC.Parser.Annotation.AnnKeywordId' : 'GHC.Parser.Annotation.AnnOpen' @'['@,
99 -- 'GHC.Parser.Annotation.AnnClose' @']'@
100
101 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation
102
103 | TuplePat (XTuplePat p)
104 -- after typechecking, holds the types of the tuple components
105 [LPat p] -- Tuple sub-patterns
106 Boxity -- UnitPat is TuplePat []
107 -- You might think that the post typechecking Type was redundant,
108 -- because we can get the pattern type by getting the types of the
109 -- sub-patterns.
110 -- But it's essential
111 -- data T a where
112 -- T1 :: Int -> T Int
113 -- f :: (T a, a) -> Int
114 -- f (T1 x, z) = z
115 -- When desugaring, we must generate
116 -- f = /\a. \v::a. case v of (t::T a, w::a) ->
117 -- case t of (T1 (x::Int)) ->
118 -- Note the (w::a), NOT (w::Int), because we have not yet
119 -- refined 'a' to Int. So we must know that the second component
120 -- of the tuple is of type 'a' not Int. See selectMatchVar
121 -- (June 14: I'm not sure this comment is right; the sub-patterns
122 -- will be wrapped in CoPats, no?)
123 -- ^ Tuple sub-patterns
124 --
125 -- - 'GHC.Parser.Annotation.AnnKeywordId' :
126 -- 'GHC.Parser.Annotation.AnnOpen' @'('@ or @'(#'@,
127 -- 'GHC.Parser.Annotation.AnnClose' @')'@ or @'#)'@
128
129 | SumPat (XSumPat p) -- after typechecker, types of the alternative
130 (LPat p) -- Sum sub-pattern
131 ConTag -- Alternative (one-based)
132 Arity -- Arity (INVARIANT: ≥ 2)
133 -- ^ Anonymous sum pattern
134 --
135 -- - 'GHC.Parser.Annotation.AnnKeywordId' :
136 -- 'GHC.Parser.Annotation.AnnOpen' @'(#'@,
137 -- 'GHC.Parser.Annotation.AnnClose' @'#)'@
138
139 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation
140
141 ------------ Constructor patterns ---------------
142 | ConPat {
143 pat_con_ext :: XConPat p,
144 pat_con :: XRec p (ConLikeP p),
145 pat_args :: HsConPatDetails p
146 }
147 -- ^ Constructor Pattern
148
149 ------------ View patterns ---------------
150 -- | - 'GHC.Parser.Annotation.AnnKeywordId' : 'GHC.Parser.Annotation.AnnRarrow'
151
152 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation
153 | ViewPat (XViewPat p)
154 (LHsExpr p)
155 (LPat p)
156 -- ^ View Pattern
157
158 ------------ Pattern splices ---------------
159 -- | - 'GHC.Parser.Annotation.AnnKeywordId' : 'GHC.Parser.Annotation.AnnOpen' @'$('@
160 -- 'GHC.Parser.Annotation.AnnClose' @')'@
161
162 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation
163 | SplicePat (XSplicePat p)
164 (HsSplice p) -- ^ Splice Pattern (Includes quasi-quotes)
165
166 ------------ Literal and n+k patterns ---------------
167 | LitPat (XLitPat p)
168 (HsLit p) -- ^ Literal Pattern
169 -- Used for *non-overloaded* literal patterns:
170 -- Int#, Char#, Int, Char, String, etc.
171
172 | NPat -- Natural Pattern
173 -- Used for all overloaded literals,
174 -- including overloaded strings with -XOverloadedStrings
175 (XNPat p) -- Overall type of pattern. Might be
176 -- different than the literal's type
177 -- if (==) or negate changes the type
178 (XRec p (HsOverLit p)) -- ALWAYS positive
179 (Maybe (SyntaxExpr p)) -- Just (Name of 'negate') for
180 -- negative patterns, Nothing
181 -- otherwise
182 (SyntaxExpr p) -- Equality checker, of type t->t->Bool
183
184 -- ^ Natural Pattern
185 --
186 -- - 'GHC.Parser.Annotation.AnnKeywordId' : 'GHC.Parser.Annotation.AnnVal' @'+'@
187
188 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation
189 | NPlusKPat (XNPlusKPat p) -- Type of overall pattern
190 (LIdP p) -- n+k pattern
191 (XRec p (HsOverLit p)) -- It'll always be an HsIntegral
192 (HsOverLit p) -- See Note [NPlusK patterns] in GHC.Tc.Gen.Pat
193 -- NB: This could be (PostTc ...), but that induced a
194 -- a new hs-boot file. Not worth it.
195
196 (SyntaxExpr p) -- (>=) function, of type t1->t2->Bool
197 (SyntaxExpr p) -- Name of '-' (see GHC.Rename.Env.lookupSyntax)
198 -- ^ n+k pattern
199
200 ------------ Pattern type signatures ---------------
201 -- | - 'GHC.Parser.Annotation.AnnKeywordId' : 'GHC.Parser.Annotation.AnnDcolon'
202
203 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation
204 | SigPat (XSigPat p) -- After typechecker: Type
205 (LPat p) -- Pattern with a type signature
206 (HsPatSigType (NoGhcTc p)) -- Signature can bind both
207 -- kind and type vars
208
209 -- ^ Pattern with a type signature
210
211 -- Extension point; see Note [Trees That Grow] in Language.Haskell.Syntax.Extension
212 | XPat
213 !(XXPat p)
214
215 type family ConLikeP x
216
217
218 -- ---------------------------------------------------------------------
219
220
221 -- | Haskell Constructor Pattern Details
222 type HsConPatDetails p = HsConDetails (HsPatSigType (NoGhcTc p)) (LPat p) (HsRecFields p (LPat p))
223
224 hsConPatArgs :: forall p . (UnXRec p) => HsConPatDetails p -> [LPat p]
225 hsConPatArgs (PrefixCon _ ps) = ps
226 hsConPatArgs (RecCon fs) = map (hfbRHS . unXRec @p) (rec_flds fs)
227 hsConPatArgs (InfixCon p1 p2) = [p1,p2]
228
229 -- | Haskell Record Fields
230 --
231 -- HsRecFields is used only for patterns and expressions (not data type
232 -- declarations)
233 data HsRecFields p arg -- A bunch of record fields
234 -- { x = 3, y = True }
235 -- Used for both expressions and patterns
236 = HsRecFields { rec_flds :: [LHsRecField p arg],
237 rec_dotdot :: Maybe (Located Int) } -- Note [DotDot fields]
238 -- AZ:The XRec for LHsRecField makes the derivings fail.
239 -- deriving (Functor, Foldable, Traversable)
240
241
242 -- Note [DotDot fields]
243 -- ~~~~~~~~~~~~~~~~~~~~
244 -- The rec_dotdot field means this:
245 -- Nothing => the normal case
246 -- Just n => the group uses ".." notation,
247 --
248 -- In the latter case:
249 --
250 -- *before* renamer: rec_flds are exactly the n user-written fields
251 --
252 -- *after* renamer: rec_flds includes *all* fields, with
253 -- the first 'n' being the user-written ones
254 -- and the remainder being 'filled in' implicitly
255
256 -- | Located Haskell Record Field
257 type LHsFieldBind p id arg = XRec p (HsFieldBind id arg)
258
259 -- | Located Haskell Record Field
260 type LHsRecField p arg = XRec p (HsRecField p arg)
261
262 -- | Located Haskell Record Update Field
263 type LHsRecUpdField p = XRec p (HsRecUpdField p)
264
265 -- | Haskell Record Field
266 type HsRecField p arg = HsFieldBind (LFieldOcc p) arg
267
268 -- | Haskell Record Update Field
269 type HsRecUpdField p = HsFieldBind (LAmbiguousFieldOcc p) (LHsExpr p)
270
271 -- | Haskell Field Binding
272 --
273 -- - 'GHC.Parser.Annotation.AnnKeywordId' : 'GHC.Parser.Annotation.AnnEqual',
274 --
275 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation
276 data HsFieldBind lhs rhs = HsFieldBind {
277 hfbAnn :: XHsFieldBind lhs,
278 hfbLHS :: lhs,
279 hfbRHS :: rhs, -- ^ Filled in by renamer when punning
280 hfbPun :: Bool -- ^ Note [Punning]
281 } deriving (Functor, Foldable, Traversable)
282
283
284 -- Note [Punning]
285 -- ~~~~~~~~~~~~~~
286 -- If you write T { x, y = v+1 }, the HsRecFields will be
287 -- HsRecField x x True ...
288 -- HsRecField y (v+1) False ...
289 -- That is, for "punned" field x is expanded (in the renamer)
290 -- to x=x; but with a punning flag so we can detect it later
291 -- (e.g. when pretty printing)
292 --
293 -- If the original field was qualified, we un-qualify it, thus
294 -- T { A.x } means T { A.x = x }
295
296
297 -- Note [HsRecField and HsRecUpdField]
298 -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
299
300 -- A HsRecField (used for record construction and pattern matching)
301 -- contains an unambiguous occurrence of a field (i.e. a FieldOcc).
302 -- We can't just store the Name, because thanks to
303 -- DuplicateRecordFields this may not correspond to the label the user
304 -- wrote.
305 --
306 -- A HsRecUpdField (used for record update) contains a potentially
307 -- ambiguous occurrence of a field (an AmbiguousFieldOcc). The
308 -- renamer will fill in the selector function if it can, but if the
309 -- selector is ambiguous the renamer will defer to the typechecker.
310 -- After the typechecker, a unique selector will have been determined.
311 --
312 -- The renamer produces an Unambiguous result if it can, rather than
313 -- just doing the lookup in the typechecker, so that completely
314 -- unambiguous updates can be represented by 'GHC.HsToCore.Quote.repUpdFields'.
315 --
316 -- For example, suppose we have:
317 --
318 -- data S = MkS { x :: Int }
319 -- data T = MkT { x :: Int }
320 --
321 -- f z = (z { x = 3 }) :: S
322 --
323 -- The parsed HsRecUpdField corresponding to the record update will have:
324 --
325 -- hfbLHS = Unambiguous "x" noExtField :: AmbiguousFieldOcc RdrName
326 --
327 -- After the renamer, this will become:
328 --
329 -- hfbLHS = Ambiguous "x" noExtField :: AmbiguousFieldOcc Name
330 --
331 -- (note that the Unambiguous constructor is not type-correct here).
332 -- The typechecker will determine the particular selector:
333 --
334 -- hfbLHS = Unambiguous "x" $sel:x:MkS :: AmbiguousFieldOcc Id
335 --
336 -- See also Note [Disambiguating record fields] in GHC.Tc.Gen.Head.
337
338 hsRecFields :: forall p arg.UnXRec p => HsRecFields p arg -> [XCFieldOcc p]
339 hsRecFields rbinds = map (hsRecFieldSel . unXRec @p) (rec_flds rbinds)
340
341 hsRecFieldsArgs :: forall p arg. UnXRec p => HsRecFields p arg -> [arg]
342 hsRecFieldsArgs rbinds = map (hfbRHS . unXRec @p) (rec_flds rbinds)
343
344 hsRecFieldSel :: forall p arg. UnXRec p => HsRecField p arg -> XCFieldOcc p
345 hsRecFieldSel = foExt . unXRec @p . hfbLHS
346
347
348 {-
349 ************************************************************************
350 * *
351 * Printing patterns
352 * *
353 ************************************************************************
354 -}
355
356 instance (Outputable arg, Outputable (XRec p (HsRecField p arg)))
357 => Outputable (HsRecFields p arg) where
358 ppr (HsRecFields { rec_flds = flds, rec_dotdot = Nothing })
359 = braces (fsep (punctuate comma (map ppr flds)))
360 ppr (HsRecFields { rec_flds = flds, rec_dotdot = Just (unLoc -> n) })
361 = braces (fsep (punctuate comma (map ppr (take n flds) ++ [dotdot])))
362 where
363 dotdot = text ".." <+> whenPprDebug (ppr (drop n flds))
364
365 instance (Outputable p, OutputableBndr p, Outputable arg)
366 => Outputable (HsFieldBind p arg) where
367 ppr (HsFieldBind { hfbLHS = f, hfbRHS = arg,
368 hfbPun = pun })
369 = pprPrefixOcc f <+> (ppUnless pun $ equals <+> ppr arg)