Match.lhs 34.8 KB
 partain committed Jan 08, 1996 1 %  Simon Marlow committed Oct 11, 2006 2 % (c) The University of Glasgow 2006  simonm committed Dec 02, 1998 3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998  partain committed Jan 08, 1996 4 %  Simon Marlow committed Oct 11, 2006 5 6  The @match@ function  partain committed Jan 08, 1996 7 8  \begin{code}  simonpj@microsoft.com committed Feb 09, 2006 9 module Match ( match, matchEquations, matchWrapper, matchSimply, matchSinglePat ) where  partain committed Jan 08, 1996 10   simonm committed Jan 08, 1998 11 #include "HsVersions.h"  partain committed Jan 08, 1996 12   Dan Licata committed Oct 10, 2007 13 14 import {-#SOURCE#-} DsExpr (dsLExpr)  Simon Marlow committed Oct 11, 2006 15 import DynFlags  sof committed May 19, 1997 16 import HsSyn  Simon Marlow committed Oct 11, 2006 17 18 import TcHsSyn import Check  partain committed Mar 19, 1996 19 import CoreSyn  Simon Marlow committed Oct 11, 2006 20 21 import Literal import CoreUtils  batterseapower committed Jul 31, 2008 22 import MkCore  partain committed Mar 19, 1996 23 import DsMonad  Simon Marlow committed Oct 11, 2006 24 25 import DsBinds import DsGRHSs  partain committed Jan 08, 1996 26 import DsUtils  Simon Marlow committed Oct 11, 2006 27 28 29 30 31 import Id import DataCon import MatchCon import MatchLit import Type  32 import Coercion  Simon Marlow committed Oct 11, 2006 33 34 35 36 37 38 import TysWiredIn import ListSetOps import SrcLoc import Maybes import Util import Name  simonm committed Jan 08, 1998 39 import Outputable  Ian Lynagh committed Mar 29, 2008 40 import FastString  Ian Lynagh committed Sep 14, 2010 41   simonpj@microsoft.com committed Jan 27, 2011 42 import Control.Monad( when )  Ian Lynagh committed Sep 14, 2010 43 import qualified Data.Map as Map  partain committed Jan 08, 1996 44 45 \end{code}  quintela committed Dec 02, 1997 46 47 48 49 50 51 52 53 This function is a wrapper of @match@, it must be called from all the parts where it was called match, but only substitutes the firs call, .... if the associated flags are declared, warnings will be issued. It can not be called matchWrapper because this name already exists :-( JJCQ 30-Nov-1997 \begin{code}  simonpj committed Sep 30, 2004 54 55 56 matchCheck :: DsMatchContext -> [Id] -- Vars rep'ing the exprs we're matching with -> Type -- Type of the case expression  quintela committed Dec 02, 1997 57 58 59  -> [EquationInfo] -- Info about patterns, etc. (type synonym below) -> DsM MatchResult -- Desugared result!  simonpj@microsoft.com committed Jan 27, 2011 60 61 62 matchCheck ctx vars ty qs = do { dflags <- getDOptsDs ; matchCheck_really dflags ctx vars ty qs }  sewardj committed Oct 18, 2000 63   twanvl committed Feb 03, 2008 64 65 66 67 68 69 matchCheck_really :: DynFlags -> DsMatchContext -> [Id] -> Type -> [EquationInfo] -> DsM MatchResult  simonpj@microsoft.com committed Jan 27, 2011 70 71 72 73 74 75 76 matchCheck_really dflags ctx@(DsMatchContext hs_ctx _) vars ty qs = do { when shadow (dsShadowWarn ctx eqns_shadow) ; when incomplete (dsIncompleteWarn ctx pats) ; match vars ty qs } where (pats, eqns_shadow) = check qs incomplete = incomplete_flag hs_ctx && (notNull pats)  Ian Lynagh committed Jul 14, 2011 77  shadow = wopt Opt_WarnOverlappingPatterns dflags  simonpj@microsoft.com committed Jan 27, 2011 78 79 80  && notNull eqns_shadow incomplete_flag :: HsMatchContext id -> Bool  Ian Lynagh committed Jul 14, 2011 81 82  incomplete_flag (FunRhs {}) = wopt Opt_WarnIncompletePatterns dflags incomplete_flag CaseAlt = wopt Opt_WarnIncompletePatterns dflags  simonpj@microsoft.com committed Jan 27, 2011 83   Ian Lynagh committed Jul 14, 2011 84 85 86  incomplete_flag LambdaExpr = wopt Opt_WarnIncompleteUniPatterns dflags incomplete_flag PatBindRhs = wopt Opt_WarnIncompleteUniPatterns dflags incomplete_flag ProcExpr = wopt Opt_WarnIncompleteUniPatterns dflags  simonpj@microsoft.com committed Jan 27, 2011 87   Ian Lynagh committed Jul 14, 2011 88  incomplete_flag RecUpd = wopt Opt_WarnIncompletePatternsRecUpd dflags  simonpj@microsoft.com committed Jan 27, 2011 89 90 91 92 93 94  incomplete_flag ThPatQuote = False incomplete_flag (StmtCtxt {}) = False -- Don't warn about incomplete patterns -- in list comprehensions, pattern guards -- etc. They are often *supposed* to be -- incomplete  quintela committed Dec 02, 1997 95 96 \end{code}  simonmar committed Jun 17, 1999 97 98 This variable shows the maximum number of lines of output generated for warnings. It will limit the number of patterns/equations displayed to@ maximum_output@.  quintela committed Dec 02, 1997 99   sof committed Dec 17, 1997 100 101 (ToDo: add command-line option?)  quintela committed Dec 02, 1997 102 \begin{code}  twanvl committed Feb 03, 2008 103 maximum_output :: Int  quintela committed Dec 02, 1997 104 105 106 maximum_output = 4 \end{code}  simonmar committed Jun 17, 1999 107 The next two functions create the warning message.  quintela committed Dec 02, 1997 108 109 110  \begin{code} dsShadowWarn :: DsMatchContext -> [EquationInfo] -> DsM ()  simonpj@microsoft.com committed Feb 09, 2006 111 dsShadowWarn ctx@(DsMatchContext kind loc) qs  simonpj@microsoft.com committed Aug 18, 2006 112  = putSrcSpanDs loc (warnDs warn)  simonpj committed Jul 19, 2005 113 114  where warn | qs lengthExceeds maximum_output  Ian Lynagh committed Apr 12, 2008 115  = pp_context ctx (ptext (sLit "are overlapped"))  simonpj committed Jul 19, 2005 116  (\ f -> vcat (map (ppr_eqn f kind) (take maximum_output qs)) $$ Ian Lynagh committed Apr 12, 2008 117  ptext (sLit "..."))  simonpj committed Jul 19, 2005 118  | otherwise  Ian Lynagh committed Apr 12, 2008 119  = pp_context ctx (ptext (sLit "are overlapped"))  simonpj committed Jul 19, 2005 120  (\ f -> vcat  map (ppr_eqn f kind) qs)  sof committed Jan 18, 1999 121   quintela committed Dec 02, 1997 122 123  dsIncompleteWarn :: DsMatchContext -> [ExhaustivePat] -> DsM ()  simonpj@microsoft.com committed Feb 09, 2006 124 dsIncompleteWarn ctx@(DsMatchContext kind loc) pats  simonpj@microsoft.com committed Aug 18, 2006 125  = putSrcSpanDs loc (warnDs warn)  quintela committed Dec 02, 1997 126  where  Ian Lynagh committed Apr 12, 2008 127 128  warn = pp_context ctx (ptext (sLit "are non-exhaustive")) (\_ -> hang (ptext (sLit "Patterns not matched:"))  simonpj committed Jan 27, 1999 129 130 131 132  4 ((vcat  map (ppr_incomplete_pats kind) (take maximum_output pats))$$ dots))  Ian Lynagh committed Apr 12, 2008 133  dots | pats lengthExceeds maximum_output = ptext (sLit "...")  sof committed Oct 25, 2001 134  | otherwise = empty  quintela committed Dec 02, 1997 135   twanvl committed Feb 03, 2008 136 pp_context :: DsMatchContext -> SDoc -> ((SDoc -> SDoc) -> SDoc) -> SDoc  simonpj@microsoft.com committed Feb 09, 2006 137 pp_context (DsMatchContext kind _loc) msg rest_of_msg_fun  Ian Lynagh committed Apr 12, 2008 138 139  = vcat [ptext (sLit "Pattern match(es)") <+> msg, sep [ptext (sLit "In") <+> ppr_match <> char ':', nest 4 (rest_of_msg_fun pref)]]  simonmar committed Feb 26, 2001 140 141 142  where (ppr_match, pref) = case kind of  simonpj@microsoft.com committed Aug 22, 2007 143  FunRhs fun _ -> (pprMatchContext kind, \ pp -> ppr fun <+> pp)  twanvl committed Feb 03, 2008 144  _ -> (pprMatchContext kind, \ pp -> pp)  quintela committed Dec 02, 1997 145   twanvl committed Feb 03, 2008 146 ppr_pats :: Outputable a => [a] -> SDoc  simonm committed Jan 08, 1998 147 ppr_pats pats = sep (map ppr pats)  quintela committed Dec 02, 1997 148   twanvl committed Feb 03, 2008 149 ppr_shadow_pats :: HsMatchContext Name -> [Pat Id] -> SDoc  sof committed Jan 24, 1999 150 ppr_shadow_pats kind pats  Ian Lynagh committed Apr 12, 2008 151  = sep [ppr_pats pats, matchSeparator kind, ptext (sLit "...")]  twanvl committed Feb 03, 2008 152 153 154 155  ppr_incomplete_pats :: HsMatchContext Name -> ExhaustivePat -> SDoc ppr_incomplete_pats _ (pats,[]) = ppr_pats pats ppr_incomplete_pats _ (pats,constraints) =  Ian Lynagh committed Apr 12, 2008 156  sep [ppr_pats pats, ptext (sLit "with"),  simonm committed Jan 08, 1998 157  sep (map ppr_constraint constraints)]  quintela committed Dec 02, 1997 158   twanvl committed Feb 03, 2008 159 ppr_constraint :: (Name,[HsLit]) -> SDoc  Ian Lynagh committed Apr 12, 2008 160 ppr_constraint (var,pats) = sep [ppr var, ptext (sLit "notElem"), ppr pats]  quintela committed Dec 02, 1997 161   twanvl committed Feb 03, 2008 162 ppr_eqn :: (SDoc -> SDoc) -> HsMatchContext Name -> EquationInfo -> SDoc  simonpj committed Sep 30, 2004 163 ppr_eqn prefixF kind eqn = prefixF (ppr_shadow_pats kind (eqn_pats eqn))  quintela committed Dec 02, 1997 164 165 166 \end{code}  chak@cse.unsw.edu.au. committed Aug 04, 2006 167 168 169 170 171 172 %************************************************************************ %* * The main matching function %* * %************************************************************************  partain committed Jan 08, 1996 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 The function @match@ is basically the same as in the Wadler chapter, except it is monadised, to carry around the name supply, info about annotations, etc. Notes on @match@'s arguments, assuming $m$ equations and $n$ patterns: \begin{enumerate} \item A list of $n$ variable names, those variables presumably bound to the $n$ expressions being matched against the $n$ patterns. Using the list of $n$ expressions as the first argument showed no benefit and some inelegance. \item The second argument, a list giving the equation info'' for each of the $m$ equations: \begin{itemize} \item the $n$ patterns for that equation, and \item  partain committed Mar 19, 1996 192 a list of Core bindings [@(Id, CoreExpr)@ pairs] to be stuck on  partain committed Jan 08, 1996 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 the front'' of the matching code, as in: \begin{verbatim} let in \end{verbatim} \item and finally: (ToDo: fill in) The right way to think about the after-match function'' is that it is an embryonic @CoreExpr@ with a hole'' at the end for the final else expression''. \end{itemize} There is a type synonym, @EquationInfo@, defined in module @DsUtils@. An experiment with re-ordering this information about equations (in particular, having the patterns available in column-major order) showed no benefit. \item A default expression---what to evaluate if the overall pattern-match fails. This expression will (almost?) always be  partain committed Mar 19, 1996 215 a measly expression @Var@, unless we know it will only be used once  partain committed Jan 08, 1996 216 217 218 (as we do in @glue_success_exprs@). Leaving out this third argument to @match@ (and slamming in lots of  partain committed Mar 19, 1996 219 @Var "fail"@s) is a positively {\em bad} idea, because it makes it  partain committed Jan 08, 1996 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 impossible to share the default expressions. (Also, it stands no chance of working in our post-upheaval world of @Locals@.) \end{enumerate} Note: @match@ is often called via @matchWrapper@ (end of this module), a function that does much of the house-keeping that goes with a call to @match@. It is also worth mentioning the {\em typical} way a block of equations is desugared with @match@. At each stage, it is the first column of patterns that is examined. The steps carried out are roughly: \begin{enumerate} \item Tidy the patterns in column~1 with @tidyEqnInfo@ (this may add bindings to the second component of the equation-info): \begin{itemize} \item Remove the as' patterns from column~1. \item Make all constructor patterns in column~1 into @ConPats@, notably @ListPats@ and @TuplePats@. \item Handle any irrefutable (or twiddle'') @LazyPats@. \end{itemize} \item  Ian Lynagh committed Jul 21, 2008 245 Now {\em unmix} the equations into {\em blocks} [w\/ local function  partain committed Jan 08, 1996 246 247 248 249 @unmix_eqns@], in which the equations in a block all have variable patterns in column~1, or they all have constructor patterns in ... (see the mixture rule'' in SLPJ). \item  simonpj committed Sep 09, 2002 250 Call @matchEqnBlock@ on each block of equations; it will do the  partain committed Jan 08, 1996 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 appropriate thing for each kind of column-1 pattern, usually ending up in a recursive call to @match@. \end{enumerate} We are a little more paranoid about the empty rule'' (SLPJ, p.~87) than the Wadler-chapter code for @match@ (p.~93, first @match@ clause). And gluing the success expressions'' together isn't quite so pretty. This (more interesting) clause of @match@ uses @tidy_and_unmix_eqns@ (a)~to get as'- and twiddle'-patterns out of the way (tidying), and (b)~to do the mixture rule'' (SLPJ, p.~88) [which really {\em un}mixes the equations], producing a list of equation-info blocks, each block having as its first column of patterns either all constructors, or all variables (or similar beasts), etc. @match_unmixed_eqn_blks@ simply takes the place of the @foldr@ in the Wadler-chapter @match@ (p.~93, last clause), and @match_unmixed_blk@ corresponds roughly to @matchVarCon@. \begin{code}  Ian Lynagh committed Jul 21, 2008 271 match :: [Id] -- Variables rep\'ing the exprs we\'re matching with  chak@cse.unsw.edu.au. committed Aug 04, 2006 272 273 274 275 276  -> Type -- Type of the case expression -> [EquationInfo] -- Info about patterns, etc. (type synonym below) -> DsM MatchResult -- Desugared result! match [] ty eqns  chak@cse.unsw.edu.au. committed Sep 20, 2006 277  = ASSERT2( not (null eqns), ppr ty )  twanvl committed Jan 17, 2008 278  return (foldr1 combineMatchResults match_results)  partain committed Jan 08, 1996 279  where  chak@cse.unsw.edu.au. committed Aug 04, 2006 280 281 282 283 284 285 286 287  match_results = [ ASSERT( null (eqn_pats eqn) ) eqn_rhs eqn | eqn <- eqns ] match vars@(v:_) ty eqns = ASSERT( not (null eqns ) ) do { -- Tidy the first pattern, generating -- auxiliary bindings if necessary  simonpj@microsoft.com committed Oct 21, 2010 288  (aux_binds, tidy_eqns) <- mapAndUnzipM (tidyEqnInfo v) eqns  chak@cse.unsw.edu.au. committed Aug 04, 2006 289 290  -- Group the equations and match each group in turn  simonpj@microsoft.com committed Oct 21, 2010 291  ; let grouped = groupEquations tidy_eqns  Dan Licata committed Oct 10, 2007 292 293  -- print the view patterns that are commoned up to help debug  simonpj@microsoft.com committed Oct 21, 2010 294  ; ifDOptM Opt_D_dump_view_pattern_commoning (debug grouped)  Dan Licata committed Oct 10, 2007 295 296  ; match_results <- mapM match_group grouped  chak@cse.unsw.edu.au. committed Aug 04, 2006 297 298 299 300 301 302 303  ; return (adjustMatchResult (foldr1 (.) aux_binds) $foldr1 combineMatchResults match_results) } where dropGroup :: [(PatGroup,EquationInfo)] -> [EquationInfo] dropGroup = map snd match_group :: [(PatGroup,EquationInfo)] -> DsM MatchResult  simonpj@microsoft.com committed Oct 06, 2010 304  match_group [] = panic "match_group"  chak@cse.unsw.edu.au. committed Aug 04, 2006 305  match_group eqns@((group,_) : _)  Dan Licata committed Oct 10, 2007 306  = case group of  simonpj@microsoft.com committed Mar 30, 2009 307 308  PgCon _ -> matchConFamily vars ty (subGroup [(c,e) | (PgCon c, e) <- eqns]) PgLit _ -> matchLiterals vars ty (subGroup [(l,e) | (PgLit l, e) <- eqns])  Dan Licata committed Oct 10, 2007 309  PgAny -> matchVariables vars ty (dropGroup eqns)  simonpj@microsoft.com committed Mar 30, 2009 310  PgN _ -> matchNPats vars ty (dropGroup eqns)  twanvl committed Feb 03, 2008 311  PgNpK _ -> matchNPlusKPats vars ty (dropGroup eqns)  Dan Licata committed Oct 10, 2007 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329  PgBang -> matchBangs vars ty (dropGroup eqns) PgCo _ -> matchCoercion vars ty (dropGroup eqns) PgView _ _ -> matchView vars ty (dropGroup eqns) -- FIXME: we should also warn about view patterns that should be -- commoned up but are not -- print some stuff to see what's getting grouped -- use -dppr-debug to see the resolution of overloaded lits debug eqns = let gs = map (\group -> foldr (\ (p,_) -> \acc -> case p of PgView e _ -> e:acc _ -> acc) [] group) eqns maybeWarn [] = return () maybeWarn l = warnDs (vcat l) in maybeWarn$ (map (\g -> text "Putting these view expressions into the same case:" <+> (ppr g)) (filter (not . null) gs))  simonpj committed Sep 30, 2004 330 331 332 333  matchVariables :: [Id] -> Type -> [EquationInfo] -> DsM MatchResult -- Real true variables, just like in matchVar, SLPJ p 94 -- No binding to do: they'll all be wildcards by now (done in tidy)  twanvl committed Feb 03, 2008 334 matchVariables (_:vars) ty eqns = match vars ty (shiftEqns eqns)  simonpj@microsoft.com committed Oct 06, 2010 335 matchVariables [] _ _ = panic "matchVariables"  simonpj committed Sep 30, 2004 336   chak@cse.unsw.edu.au. committed Aug 04, 2006 337 338 matchBangs :: [Id] -> Type -> [EquationInfo] -> DsM MatchResult matchBangs (var:vars) ty eqns  simonpj@microsoft.com committed Oct 06, 2010 339 340  = do { match_result <- match (var:vars) ty $map (decomposeFirstPat getBangPat) eqns  chak@cse.unsw.edu.au. committed Aug 04, 2006 341  ; return (mkEvalMatchResult var ty match_result) }  simonpj@microsoft.com committed Oct 06, 2010 342 matchBangs [] _ _ = panic "matchBangs"  chak@cse.unsw.edu.au. committed Aug 04, 2006 343 344 345  matchCoercion :: [Id] -> Type -> [EquationInfo] -> DsM MatchResult -- Apply the coercion to the match variable and then match that  Dan Licata committed Oct 10, 2007 346 matchCoercion (var:vars) ty (eqns@(eqn1:_))  chak@cse.unsw.edu.au. committed Aug 04, 2006 347  = do { let CoPat co pat _ = firstPat eqn1  simonpj@microsoft.com committed Oct 29, 2009 348  ; var' <- newUniqueId var (hsPatType pat)  simonpj@microsoft.com committed Oct 06, 2010 349 350  ; match_result <- match (var':vars) ty$ map (decomposeFirstPat getCoPat) eqns  simonpj@microsoft.com committed Sep 13, 2010 351  ; co' <- dsHsWrapper co  simonpj@microsoft.com committed Oct 29, 2009 352 353  ; let rhs' = co' (Var var) ; return (mkCoLetMatchResult (NonRec var' rhs') match_result) }  simonpj@microsoft.com committed Oct 06, 2010 354 matchCoercion _ _ _ = panic "matchCoercion"  Dan Licata committed Oct 10, 2007 355 356 357 358 359 360 361 362 363  matchView :: [Id] -> Type -> [EquationInfo] -> DsM MatchResult -- Apply the view function to the match variable and then match that matchView (var:vars) ty (eqns@(eqn1:_)) = do { -- we could pass in the expr from the PgView, -- but this needs to extract the pat anyway -- to figure out the type of the fresh variable let ViewPat viewExpr (L _ pat) _ = firstPat eqn1 -- do the rest of the compilation  simonpj@microsoft.com committed Oct 29, 2009 364  ; var' <- newUniqueId var (hsPatType pat)  simonpj@microsoft.com committed Oct 06, 2010 365 366  ; match_result <- match (var':vars) ty $map (decomposeFirstPat getViewPat) eqns  Dan Licata committed Oct 10, 2007 367  -- compile the view expressions  simonpj@microsoft.com committed Oct 06, 2010 368  ; viewExpr' <- dsLExpr viewExpr  Dan Licata committed Oct 10, 2007 369  ; return (mkViewMatchResult var' viewExpr' var match_result) }  simonpj@microsoft.com committed Oct 06, 2010 370 matchView _ _ _ = panic "matchView"  Dan Licata committed Oct 10, 2007 371 372  -- decompose the first pattern and leave the rest alone  twanvl committed Feb 03, 2008 373 decomposeFirstPat :: (Pat Id -> Pat Id) -> EquationInfo -> EquationInfo  Dan Licata committed Oct 10, 2007 374 375 decomposeFirstPat extractpat (eqn@(EqnInfo { eqn_pats = pat : pats })) = eqn { eqn_pats = extractpat pat : pats}  simonpj@microsoft.com committed Oct 06, 2010 376 decomposeFirstPat _ _ = panic "decomposeFirstPat"  Dan Licata committed Oct 10, 2007 377   simonpj@microsoft.com committed Oct 06, 2010 378 379 380 381 382 383 384 getCoPat, getBangPat, getViewPat :: Pat Id -> Pat Id getCoPat (CoPat _ pat _) = pat getCoPat _ = panic "getCoPat" getBangPat (BangPat pat ) = unLoc pat getBangPat _ = panic "getBangPat" getViewPat (ViewPat _ pat _) = unLoc pat getViewPat _ = panic "getBangPat"  partain committed Jan 08, 1996 385 386 \end{code}  chak@cse.unsw.edu.au. committed Aug 04, 2006 387 388 389 390 391 392 %************************************************************************ %* * Tidying patterns %* * %************************************************************************  partain committed Jan 08, 1996 393 394 395 396 397 398 399 Tidy up the leftmost pattern in an @EquationInfo@, given the variable @v@ which will be scrutinised. This means: \begin{itemize} \item Replace variable patterns @x@ (@x /= v@) with the pattern @_@, together with the binding @x = v@. \item  Ian Lynagh committed Feb 15, 2008 400 Replace the as' pattern @x@@p@ with the pattern p and a binding @x = v@.  partain committed Jan 08, 1996 401 402 403 \item Removing lazy (irrefutable) patterns (you don't want to know...). \item  chak committed Feb 11, 2002 404 405 Converting explicit tuple-, list-, and parallel-array-pats into ordinary @ConPats@.  simonpj committed Mar 14, 1997 406 407 \item Convert the literal pat "" to [].  partain committed Jan 08, 1996 408 409 410 411 412 413 414 415 416 417 418 \end{itemize} The result of this tidying is that the column of patterns will include {\em only}: \begin{description} \item[@WildPats@:] The @VarPat@ information isn't needed any more after this. \item[@ConPats@:] @ListPats@, @TuplePats@, etc., are all converted into @ConPats@.  partain committed Mar 19, 1996 419 420 \item[@LitPats@ and @NPats@:] @LitPats@/@NPats@ of known friendly types'' (Int, Char,  partain committed Jan 08, 1996 421 Float, Double, at least) are converted to unboxed form; e.g.,  partain committed Mar 19, 1996 422 \tr{(NPat (HsInt i) _ _)} is converted to:  partain committed Jan 08, 1996 423 \begin{verbatim}  simonpj committed Sep 13, 2002 424 (ConPat I# _ _ [LitPat (HsIntPrim i)])  partain committed Jan 08, 1996 425 426 427 428 \end{verbatim} \end{description} \begin{code}  chak@cse.unsw.edu.au. committed Aug 04, 2006 429 430 tidyEqnInfo :: Id -> EquationInfo -> DsM (DsWrapper, EquationInfo)  simonpj committed Jul 19, 2005 431  -- DsM'd because of internal call to dsLHsBinds  simonpj committed Sep 30, 2004 432  -- and mkSelectorBinds.  partain committed Jan 08, 1996 433 434  -- "tidy1" does the interesting stuff, looking at -- one pattern and fiddling the list of bindings.  simonpj committed Dec 18, 1998 435 436 437 438 439 440 441 442 443  -- -- POST CONDITION: head pattern in the EqnInfo is -- WildPat -- ConPat -- NPat -- LitPat -- NPlusKPat -- but no other  simonpj@microsoft.com committed Oct 06, 2010 444 445 446 447 448 449 tidyEqnInfo _ (EqnInfo { eqn_pats = [] }) = panic "tidyEqnInfo" tidyEqnInfo v eqn@(EqnInfo { eqn_pats = pat : pats }) = do { (wrap, pat') <- tidy1 v pat ; return (wrap, eqn { eqn_pats = do pat' : pats }) }  simonpj committed Sep 13, 2002 450 451  tidy1 :: Id -- The Id being scrutinised  simonmar committed Dec 10, 2003 452  -> Pat Id -- The pattern against which it is to be matched  chak@cse.unsw.edu.au. committed Aug 04, 2006 453  -> DsM (DsWrapper, -- Extra bindings to do before the match  simonpj committed Mar 01, 2005 454  Pat Id) -- Equivalent pattern  simonpj committed Sep 30, 2004 455   simonpj committed Sep 09, 2002 456 457 458 459 460 461 ------------------------------------------------------- -- (pat', mr') = tidy1 v pat mr -- tidies the *outer level only* of pat, giving pat' -- It eliminates many pattern forms (as-patterns, variable patterns, -- list patterns, etc) yielding one of: -- WildPat  simonpj committed Sep 30, 2004 462 -- ConPatOut  simonpj committed Sep 09, 2002 463 464 465 466 -- LitPat -- NPat -- NPlusKPat  chak@cse.unsw.edu.au. committed Aug 04, 2006 467 468 tidy1 v (ParPat pat) = tidy1 v (unLoc pat) tidy1 v (SigPatOut pat _) = tidy1 v (unLoc pat)  twanvl committed Feb 03, 2008 469 tidy1 _ (WildPat ty) = return (idDsWrapper, WildPat ty)  partain committed Jan 08, 1996 470   simonpj committed Sep 09, 2002 471 472  -- case v of { x -> mr[] } -- = case v of { _ -> let x=v in mr[] }  chak@cse.unsw.edu.au. committed Aug 04, 2006 473 tidy1 v (VarPat var)  twanvl committed Jan 17, 2008 474  = return (wrapBind var v, WildPat (idType var))  simonpj committed Sep 30, 2004 475   simonpj committed Sep 09, 2002 476 477  -- case v of { x@p -> mr[] } -- = case v of { p -> let x=v in mr[] }  chak@cse.unsw.edu.au. committed Aug 04, 2006 478 479 480 tidy1 v (AsPat (L _ var) pat) = do { (wrap, pat') <- tidy1 v (unLoc pat) ; return (wrapBind var v . wrap, pat') }  partain committed Jan 08, 1996 481 482 483 484 485 486 487 488 489  {- now, here we handle lazy patterns: tidy1 v ~p bs = (v, v1 = case v of p -> v1 : v2 = case v of p -> v2 : ... : bs ) where the v_i's are the binders in the pattern. ToDo: in "v_i = ... -> v_i", are the v_i's really the same thing?  partain committed Mar 19, 1996 490  The case expr for v_i is just: match [v] [(p, [], \ x -> Var v_i)] any_expr  partain committed Jan 08, 1996 491 492 -}  chak@cse.unsw.edu.au. committed Aug 04, 2006 493 494 tidy1 v (LazyPat pat) = do { sel_prs <- mkSelectorBinds pat (Var v)  simonpj committed Sep 30, 2004 495  ; let sel_binds = [NonRec b rhs | (b,rhs) <- sel_prs]  batterseapower committed Jul 31, 2008 496  ; return (mkCoreLets sel_binds, WildPat (idType v)) }  partain committed Jan 08, 1996 497   twanvl committed Feb 03, 2008 498 tidy1 _ (ListPat pats ty)  twanvl committed Jan 17, 2008 499  = return (idDsWrapper, unLoc list_ConPat)  partain committed Jan 08, 1996 500  where  simonpj committed Sep 13, 2002 501 502 503 504  list_ty = mkListTy ty list_ConPat = foldr (\ x y -> mkPrefixConPat consDataCon [x, y] list_ty) (mkNilPat list_ty) pats  partain committed Jan 08, 1996 505   simonpj committed Sep 30, 2004 506 -- Introduce fake parallel array constructors to be able to handle parallel  chak committed Feb 11, 2002 507 -- arrays with the existing machinery for constructor pattern  twanvl committed Feb 03, 2008 508 tidy1 _ (PArrPat pats ty)  twanvl committed Jan 17, 2008 509  = return (idDsWrapper, unLoc parrConPat)  chak committed Feb 11, 2002 510 511  where arity = length pats  simonpj committed Sep 13, 2002 512  parrConPat = mkPrefixConPat (parrFakeCon arity) pats (mkPArrTy ty)  chak committed Feb 11, 2002 513   twanvl committed Feb 03, 2008 514 tidy1 _ (TuplePat pats boxity ty)  twanvl committed Jan 17, 2008 515  = return (idDsWrapper, unLoc tuple_ConPat)  partain committed Jan 08, 1996 516 517  where arity = length pats  simonpj@microsoft.com committed Feb 02, 2006 518  tuple_ConPat = mkPrefixConPat (tupleCon boxity arity) pats ty  partain committed Jan 08, 1996 519   simonpj committed Sep 22, 2000 520 -- LitPats: we *might* be able to replace these w/ a simpler form  twanvl committed Feb 03, 2008 521 tidy1 _ (LitPat lit)  twanvl committed Jan 17, 2008 522  = return (idDsWrapper, tidyLitPat lit)  partain committed Jan 08, 1996 523 524  -- NPats: we *might* be able to replace these w/ a simpler form  twanvl committed Feb 03, 2008 525 tidy1 _ (NPat lit mb_neg eq)  Simon Peyton Jones committed May 04, 2011 526  = return (idDsWrapper, tidyNPat tidyLitPat lit mb_neg eq)  simonpj committed Sep 13, 2002 527   Ian Lynagh committed Apr 24, 2009 528 529 530 531 532 533 534 535 536 537 538 539 540 541 -- BangPatterns: Pattern matching is already strict in constructors, -- tuples etc, so the last case strips off the bang for thoses patterns. tidy1 v (BangPat (L _ (LazyPat p))) = tidy1 v (BangPat p) tidy1 v (BangPat (L _ (ParPat p))) = tidy1 v (BangPat p) tidy1 _ p@(BangPat (L _(VarPat _))) = return (idDsWrapper, p) tidy1 _ p@(BangPat (L _ (WildPat _))) = return (idDsWrapper, p) tidy1 _ p@(BangPat (L _ (CoPat _ _ _))) = return (idDsWrapper, p) tidy1 _ p@(BangPat (L _ (SigPatIn _ _))) = return (idDsWrapper, p) tidy1 _ p@(BangPat (L _ (SigPatOut _ _))) = return (idDsWrapper, p) tidy1 v (BangPat (L _ (AsPat (L _ var) pat))) = do { (wrap, pat') <- tidy1 v (BangPat pat) ; return (wrapBind var v . wrap, pat') } tidy1 v (BangPat (L _ p)) = tidy1 v p  chak@cse.unsw.edu.au. committed Aug 04, 2006 542 -- Everything else goes through unchanged...  simonpj committed Sep 13, 2002 543   twanvl committed Feb 03, 2008 544 tidy1 _ non_interesting_pat  twanvl committed Jan 17, 2008 545  = return (idDsWrapper, non_interesting_pat)  partain committed Jan 08, 1996 546 547 \end{code}  simonmar committed Jun 17, 1999 548 549 \noindent {\bf Previous @matchTwiddled@ stuff:}  partain committed Jan 08, 1996 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637  Now we get to the only interesting part; note: there are choices for translation [from Simon's notes]; translation~1: \begin{verbatim} deTwiddle [s,t] e \end{verbatim} returns \begin{verbatim} [ w = e, s = case w of [s,t] -> s t = case w of [s,t] -> t ] \end{verbatim} Here \tr{w} is a fresh variable, and the \tr{w}-binding prevents multiple evaluation of \tr{e}. An alternative translation (No.~2): \begin{verbatim} [ w = case e of [s,t] -> (s,t) s = case w of (s,t) -> s t = case w of (s,t) -> t ] \end{verbatim} %************************************************************************ %* * \subsubsection[improved-unmixing]{UNIMPLEMENTED idea for improved unmixing} %* * %************************************************************************ We might be able to optimise unmixing when confronted by only-one-constructor-possible, of which tuples are the most notable examples. Consider: \begin{verbatim} f (a,b,c) ... = ... f d ... (e:f) = ... f (g,h,i) ... = ... f j ... = ... \end{verbatim} This definition would normally be unmixed into four equation blocks, one per equation. But it could be unmixed into just one equation block, because if the one equation matches (on the first column), the others certainly will. You have to be careful, though; the example \begin{verbatim} f j ... = ... ------------------- f (a,b,c) ... = ... f d ... (e:f) = ... f (g,h,i) ... = ... \end{verbatim} {\em must} be broken into two blocks at the line shown; otherwise, you are forcing unnecessary evaluation. In any case, the top-left pattern always gives the cue. You could then unmix blocks into groups of... \begin{description} \item[all variables:] As it is now. \item[constructors or variables (mixed):] Need to make sure the right names get bound for the variable patterns. \item[literals or variables (mixed):] Presumably just a variant on the constructor case (as it is now). \end{description} %************************************************************************ %* * %* matchWrapper: a convenient way to call @match@ * %* * %************************************************************************ \subsection[matchWrapper]{@matchWrapper@: a convenient interface to @match@} Calls to @match@ often involve similar (non-trivial) work; that work is collected here, in @matchWrapper@. This function takes as arguments: \begin{itemize} \item Typchecked @Matches@ (of a function definition, or a case or lambda expression)---the main input; \item An error message to be inserted into any (runtime) pattern-matching failure messages. \end{itemize} As results, @matchWrapper@ produces: \begin{itemize} \item A list of variables (@Locals@) that the caller must promise'' to bind to appropriate values; and \item  partain committed Mar 19, 1996 638 a @CoreExpr@, the desugared output (main result).  partain committed Jan 08, 1996 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 \end{itemize} The main actions of @matchWrapper@ include: \begin{enumerate} \item Flatten the @[TypecheckedMatch]@ into a suitable list of @EquationInfo@s. \item Create as many new variables as there are patterns in a pattern-list (in any one of the @EquationInfo@s). \item Create a suitable if it fails'' expression---a call to @error@ using the error-string input; the {\em type} of this fail value can be found by examining one of the RHS expressions in one of the @EquationInfo@s. \item Call @match@ with all of this information! \end{enumerate} \begin{code}  simonmar committed Dec 10, 2003 658 matchWrapper :: HsMatchContext Name -- For shadowing warning messages  simonpj committed Sep 30, 2004 659  -> MatchGroup Id -- Matches being desugared  partain committed Mar 19, 1996 660  -> DsM ([Id], CoreExpr) -- Results  quintela committed Dec 02, 1997 661 \end{code}  partain committed Jan 08, 1996 662   quintela committed Dec 02, 1997 663 664  There is one small problem with the Lambda Patterns, when somebody writes something similar to:  simonmar committed Jun 17, 1999 665 \begin{verbatim}  quintela committed Dec 02, 1997 666  (\ (x:xs) -> ...)  simonmar committed Jun 17, 1999 667 \end{verbatim}  quintela committed Dec 02, 1997 668  he/she don't want a warning about incomplete patterns, that is done with  simonmar committed Jun 17, 1999 669 670 671 672 673 674 675 676 677 678 679 680  the flag @opt_WarnSimplePatterns@. This problem also appears in the: \begin{itemize} \item @do@ patterns, but if the @do@ can fail it creates another equation if the match can fail (see @DsExpr.doDo@ function) \item @let@ patterns, are treated by @matchSimply@ List Comprension Patterns, are treated by @matchSimply@ also \end{itemize} We can't call @matchSimply@ with Lambda patterns, due to the fact that lambda patterns can have more than  quintela committed Dec 02, 1997 681 682 683 one pattern, and match simply only accepts one pattern. JJQC 30-Nov-1997  simonmar committed Jun 17, 1999 684   quintela committed Dec 02, 1997 685 \begin{code}  simonpj committed Sep 30, 2004 686 matchWrapper ctxt (MatchGroup matches match_ty)  simonpj@microsoft.com committed May 02, 2007 687 688  = ASSERT( notNull matches ) do { eqns_info <- mapM mk_eqn_info matches  chak@cse.unsw.edu.au. committed Aug 04, 2006 689  ; new_vars <- selectMatchVars arg_pats  simonpj@microsoft.com committed Feb 09, 2006 690  ; result_expr <- matchEquations ctxt new_vars eqns_info rhs_ty  simonpj committed Sep 30, 2004 691  ; return (new_vars, result_expr) }  simonpj@microsoft.com committed Feb 09, 2006 692  where  chak@cse.unsw.edu.au. committed Aug 04, 2006 693 694 695  arg_pats = map unLoc (hsLMatchPats (head matches)) n_pats = length arg_pats (_, rhs_ty) = splitFunTysN n_pats match_ty  simonpj committed Sep 30, 2004 696 697 698 699  mk_eqn_info (L _ (Match pats _ grhss)) = do { let upats = map unLoc pats ; match_result <- dsGRHSs ctxt upats grhss rhs_ty  chak@cse.unsw.edu.au. committed Aug 04, 2006 700  ; return (EqnInfo { eqn_pats = upats, eqn_rhs = match_result}) }  simonpj committed Sep 30, 2004 701   simonpj@microsoft.com committed Feb 09, 2006 702 703 704 705 706  matchEquations :: HsMatchContext Name -> [Id] -> [EquationInfo] -> Type -> DsM CoreExpr matchEquations ctxt vars eqns_info rhs_ty  707 708  = do { locn <- getSrcSpanDs ; let ds_ctxt = DsMatchContext ctxt locn  Ian Lynagh committed Mar 31, 2009 709  error_doc = matchContextErrString ctxt  simonpj@microsoft.com committed Feb 09, 2006 710   711  ; match_result <- matchCheck ds_ctxt vars rhs_ty eqns_info  simonpj@microsoft.com committed Feb 09, 2006 712   Ian Lynagh committed Mar 31, 2009 713  ; fail_expr <- mkErrorAppDs pAT_ERROR_ID rhs_ty error_doc  simonpj@microsoft.com committed Feb 09, 2006 714  ; extractMatchResult match_result fail_expr }  partain committed Jan 08, 1996 715 716 717 718 719 720 721 722 723 724 725 726 727 \end{code} %************************************************************************ %* * \subsection[matchSimply]{@matchSimply@: match a single expression against a single pattern} %* * %************************************************************************ @mkSimpleMatch@ is a wrapper for @match@ which deals with the situation where we want to match a single expression against a single pattern. It returns an expression. \begin{code}  simonpj committed Jun 11, 2001 728 matchSimply :: CoreExpr -- Scrutinee  simonmar committed Dec 10, 2003 729 730  -> HsMatchContext Name -- Match kind -> LPat Id -- Pattern it should match  simonpj committed Jun 11, 2001 731 732  -> CoreExpr -- Return this if it matches -> CoreExpr -- Return this if it doesn't  partain committed Mar 19, 1996 733  -> DsM CoreExpr  734 -- Do not warn about incomplete patterns; see matchSinglePat comments  twanvl committed Jan 17, 2008 735 736 matchSimply scrut hs_ctx pat result_expr fail_expr = do let  simonm committed Dec 02, 1998 737  match_result = cantFailMatchResult result_expr  twanvl committed Jan 17, 2008 738 739 740  rhs_ty = exprType fail_expr -- Use exprType of fail_expr, because won't refine in the case of failure! match_result' <- matchSinglePat scrut hs_ctx pat rhs_ty match_result  simonm committed Dec 02, 1998 741  extractMatchResult match_result' fail_expr  quintela committed Dec 02, 1997 742   simonpj committed Apr 04, 2005 743 matchSinglePat :: CoreExpr -> HsMatchContext Name -> LPat Id  simonpj committed Sep 30, 2004 744  -> Type -> MatchResult -> DsM MatchResult  745 746 747 -- Do not warn about incomplete patterns -- Used for things like [ e | pat <- stuff ], where -- incomplete patterns are just fine  simonpj@microsoft.com committed Jan 27, 2011 748 749 750 751 752 753 754 755 756 757 matchSinglePat (Var var) ctx (L _ pat) ty match_result = do { locn <- getSrcSpanDs ; matchCheck (DsMatchContext ctx locn) [var] ty [EqnInfo { eqn_pats = [pat], eqn_rhs = match_result }] } matchSinglePat scrut hs_ctx pat ty match_result = do { var <- selectSimpleMatchVarL pat ; match_result' <- matchSinglePat (Var var) hs_ctx pat ty match_result ; return (adjustMatchResult (bindNonRec var scrut) match_result') }  partain committed Jan 08, 1996 758 759 \end{code}  chak@cse.unsw.edu.au. committed Aug 04, 2006 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777  %************************************************************************ %* * Pattern classification %* * %************************************************************************ \begin{code} data PatGroup = PgAny -- Immediate match: variables, wildcards, -- lazy patterns | PgCon DataCon -- Constructor patterns (incl list, tuple) | PgLit Literal -- Literal patterns | PgN Literal -- Overloaded literals | PgNpK Literal -- n+k patterns | PgBang -- Bang patterns | PgCo Type -- Coercion patterns; the type is the type -- of the pattern *inside*  Dan Licata committed Oct 10, 2007 778 779 780  | PgView (LHsExpr Id) -- view pattern (e -> p): -- the LHsExpr is the expression e Type -- the Type is the type of p (equivalently, the result type of e)  chak@cse.unsw.edu.au. committed Aug 04, 2006 781 782  groupEquations :: [EquationInfo] -> [[(PatGroup, EquationInfo)]]  chak@cse.unsw.edu.au. committed Sep 20, 2006 783 784 785 -- If the result is of form [g1, g2, g3], -- (a) all the (pg,eq) pairs in g1 have the same pg -- (b) none of the gi are empty  simonpj@microsoft.com committed Mar 30, 2009 786 -- The ordering of equations is unchanged  chak@cse.unsw.edu.au. committed Aug 04, 2006 787 788 789 790 791 792 groupEquations eqns = runs same_gp [(patGroup (firstPat eqn), eqn) | eqn <- eqns] where same_gp :: (PatGroup,EquationInfo) -> (PatGroup,EquationInfo) -> Bool (pg1,_) same_gp (pg2,_) = pg1 sameGroup pg2  simonpj@microsoft.com committed Mar 30, 2009 793 subGroup :: Ord a => [(a, EquationInfo)] -> [[EquationInfo]]  chak@cse.unsw.edu.au. committed Aug 04, 2006 794 795 -- Input is a particular group. The result sub-groups the -- equations by with particular constructor, literal etc they match.  simonpj@microsoft.com committed Mar 30, 2009 796 797 798 -- Each sub-list in the result has the same PatGroup -- See Note [Take care with pattern order] subGroup group  Ian Lynagh committed Sep 14, 2010 799  = map reverse$ Map.elems \$ foldl accumulate Map.empty group  chak@cse.unsw.edu.au. committed Aug 04, 2006 800  where  simonpj@microsoft.com committed Mar 30, 2009 801  accumulate pg_map (pg, eqn)  Ian Lynagh committed Sep 14, 2010 802 803 804  = case Map.lookup pg pg_map of Just eqns -> Map.insert pg (eqn:eqns) pg_map Nothing -> Map.insert pg [eqn] pg_map  simonpj@microsoft.com committed Mar 30, 2009 805   Ian Lynagh committed Sep 14, 2010 806  -- pg_map :: Map a [EquationInfo]  simonpj@microsoft.com committed Mar 30, 2009 807 808  -- Equations seen so far in reverse order of appearance \end{code}  chak@cse.unsw.edu.au. committed Aug 04, 2006 809   simonpj@microsoft.com committed Mar 30, 2009 810 811 812 813 814 815 816 817 818 Note [Take care with pattern order] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In the subGroup function we must be very careful about pattern re-ordering, Consider the patterns [ (True, Nothing), (False, x), (True, y) ] Then in bringing together the patterns for True, we must not swap the Nothing and y! \begin{code}  chak@cse.unsw.edu.au. committed Aug 04, 2006 819 820 821 822 823 824 825 826 sameGroup :: PatGroup -> PatGroup -> Bool -- Same group means that a single case expression -- or test will suffice to match both, *and* the order -- of testing within the group is insignificant. sameGroup PgAny PgAny = True sameGroup PgBang PgBang = True sameGroup (PgCon _) (PgCon _) = True -- One case expression sameGroup (PgLit _) (PgLit _) = True -- One case expression  simonpj@microsoft.com committed Mar 30, 2009 827 828 sameGroup (PgN l1) (PgN l2) = l1==l2 -- Order is significant sameGroup (PgNpK l1) (PgNpK l2) = l1==l2 -- See Note [Grouping overloaded literal patterns]  829 sameGroup (PgCo t1) (PgCo t2) = t1 eqType t2  simonpj@microsoft.com committed Sep 06, 2006 830 831 832 833  -- CoPats are in the same goup only if the type of the -- enclosed pattern is the same. The patterns outside the CoPat -- always have the same type, so this boils down to saying that -- the two coercions are identical.  Dan Licata committed Oct 10, 2007 834 835 836 sameGroup (PgView e1 t1) (PgView e2 t2) = viewLExprEq (e1,t1) (e2,t2) -- ViewPats are in the same gorup iff the expressions -- are "equal"---conservatively, we use syntactic equality  chak@cse.unsw.edu.au. committed Aug 04, 2006 837 sameGroup _ _ = False  Dan Licata committed Oct 10, 2007 838   simonpj@microsoft.com committed Jul 23, 2009 839 -- An approximation of syntactic equality used for determining when view  Dan Licata committed Oct 10, 2007 840 -- exprs are in the same group.  simonpj@microsoft.com committed Jul 23, 2009 841 -- This function can always safely return false;  Dan Licata committed Oct 10, 2007 842 843 -- but doing so will result in the application of the view function being repeated. --  simonpj@microsoft.com committed Jul 23, 2009 844 -- Currently: compare applications of literals and variables  Dan Licata committed Oct 10, 2007 845 846 847 848 849 850 851 -- and anything else that we can do without involving other -- HsSyn types in the recursion -- -- NB we can't assume that the two view expressions have the same type. Consider -- f (e1 -> True) = ... -- f (e2 -> "hi") = ... viewLExprEq :: (LHsExpr Id,Type) -> (LHsExpr Id,Type) -> Bool  simonpj@microsoft.com committed Oct 06, 2010 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 viewLExprEq (e1,_) (e2,_) = lexp e1 e2 where lexp :: LHsExpr Id -> LHsExpr Id -> Bool lexp e e' = exp (unLoc e) (unLoc e') --------- exp :: HsExpr Id -> HsExpr Id -> Bool -- real comparison is on HsExpr's -- strip parens exp (HsPar (L _ e)) e' = exp e e' exp e (HsPar (L _ e')) = exp e e' -- because the expressions do not necessarily have the same type, -- we have to compare the wrappers exp (HsWrap h e) (HsWrap h' e') = wrap h h' && exp e e' exp (HsVar i) (HsVar i') = i == i' -- the instance for IPName derives using the id, so this works if the -- above does exp (HsIPVar i) (HsIPVar i') = i == i' exp (HsOverLit l) (HsOverLit l') = -- Overloaded lits are equal if they have the same type -- and the data is the same. -- this is coarser than comparing the SyntaxExpr's in l and l', -- which resolve the overloading (e.g., fromInteger 1), -- because these expressions get written as a bunch of different variables -- (presumably to improve sharing)  877  eqType (overLitType l) (overLitType l') && l == l'  simonpj@microsoft.com committed Oct 06, 2010 878 879 880 881 882 883 884 885 886 887 888 889  exp (HsApp e1 e2) (HsApp e1' e2') = lexp e1 e1' && lexp e2 e2' -- the fixities have been straightened out by now, so it's safe -- to ignore them? exp (OpApp l o _ ri) (OpApp l' o' _ ri') = lexp l l' && lexp o o' && lexp ri ri' exp (NegApp e n) (NegApp e' n') = lexp e e' && exp n n' exp (SectionL e1 e2) (SectionL e1' e2') = lexp e1 e1' && lexp e2 e2' exp (SectionR e1 e2) (SectionR e1' e2') = lexp e1 e1' && lexp e2 e2' exp (ExplicitTuple es1 _) (ExplicitTuple es2 _) = eq_list tup_arg es1 es2  simonpj@microsoft.com committed Oct 22, 2010 890  exp (HsIf _ e e1 e2) (HsIf _ e' e1' e2') =  simonpj@microsoft.com committed Oct 06, 2010 891 892 893 894 895 896 897 898 899 900  lexp e e' && lexp e1 e1' && lexp e2 e2' -- Enhancement: could implement equality for more expressions -- if it seems useful -- But no need for HsLit, ExplicitList, ExplicitTuple, -- because they cannot be functions exp _ _ = False --------- tup_arg (Present e1) (Present e2) = lexp e1 e2  901  tup_arg (Missing t1) (Missing t2) = eqType t1 t2  simonpj@microsoft.com committed Oct 06, 2010 902 903 904 905 906 907 908 909 910 911 912 913  tup_arg _ _ = False --------- wrap :: HsWrapper -> HsWrapper -> Bool -- Conservative, in that it demands that wrappers be -- syntactically identical and doesn't look under binders -- -- Coarser notions of equality are possible -- (e.g., reassociating compositions, -- equating different ways of writing a coercion) wrap WpHole WpHole = True wrap (WpCompose w1 w2) (WpCompose w1' w2') = wrap w1 w1' && wrap w2 w2'  914  wrap (WpCast c) (WpCast c') = coreEqCoercion c c'  simonpj@microsoft.com committed Oct 06, 2010 915  wrap (WpEvApp et1) (WpEvApp et2) = ev_term et1 et2  916  wrap (WpTyApp t) (WpTyApp t') = eqType t t'  simonpj@microsoft.com committed Oct 06, 2010 917 918 919 920 921 922 923  -- Enhancement: could implement equality for more wrappers -- if it seems useful (lams and lets) wrap _ _ = False --------- ev_term :: EvTerm -> EvTerm -> Bool ev_term (EvId a) (EvId b) = a==b  924  ev_term (EvCoercion a) (EvCoercion b) = coreEqCoercion a b  simonpj@microsoft.com committed Oct 06, 2010 925 926 927 928 929 930 931 932  ev_term _ _ = False --------- eq_list :: (a->a->Bool) -> [a] -> [a] -> Bool eq_list _ [] [] = True eq_list _ [] (_:_) = False eq_list _ (_:_) [] = False eq_list eq (x:xs) (y:ys) = eq x y && eq_list eq xs ys  Dan Licata committed Oct 10, 2007 933   chak@cse.unsw.edu.au. committed Aug 04, 2006 934 935 936 937 938 patGroup :: Pat Id -> PatGroup patGroup (WildPat {}) = PgAny patGroup (BangPat {}) = PgBang patGroup (ConPatOut { pat_con = dc }) = PgCon (unLoc dc) patGroup (LitPat lit) = PgLit (hsLitKey lit)  Dan Licata committed Oct 10, 2007 939 patGroup (NPat olit mb_neg _) = PgN (hsOverLitKey olit (isJust mb_neg))  chak@cse.unsw.edu.au. committed Aug 04, 2006 940 patGroup (NPlusKPat _ olit _ _) = PgNpK (hsOverLitKey olit False)  Dan Licata committed Oct 10, 2007 941 942 patGroup (CoPat _ p _) = PgCo (hsPatType p) -- Type of innelexp pattern patGroup (ViewPat expr p _) = PgView expr (hsPatType (unLoc p))  chak@cse.unsw.edu.au. committed Aug 04, 2006 943 944 945 patGroup pat = pprPanic "patGroup" (ppr pat) \end{code}  simonpj@microsoft.com committed Mar 30, 2009 946 947 Note [Grouping overloaded literal patterns] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  chak@cse.unsw.edu.au. committed Aug 04, 2006 948 949 950 951 952 953 954 WATCH OUT! Consider f (n+1) = ... f (n+2) = ... f (n+1) = ... We can't group the first and third together, because the second may match  simonpj@microsoft.com committed Mar 30, 2009 955 956 957 958 959 960 961 962 the same thing as the first. Same goes for *overloaded* literal patterns f 1 True = ... f 2 False = ... f 1 False = ... If the first arg matches '1' but the second does not match 'True', we cannot jump to the third equation! Because the same argument might match '2'! Hence we don't regard 1 and 2, or (n+1) and (n+2), as part of the same group.  963