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