Commit 8f377ccd authored by ian@well-typed.com's avatar ian@well-typed.com

Whitespace only in deSugar/Match.lhs

parent ebaa3321
......@@ -6,13 +6,6 @@
The @match@ function
\begin{code}
{-# OPTIONS -fno-warn-tabs #-}
-- The above warning supression flag is a temporary kludge.
-- While working on this module you are encouraged to remove it and
-- detab the module (please do the detabbing in a separate patch). See
-- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#TabsvsSpaces
-- for details
module Match ( match, matchEquations, matchWrapper, matchSimply, matchSinglePat ) where
#include "HsVersions.h"
......@@ -20,7 +13,7 @@ module Match ( match, matchEquations, matchWrapper, matchSimply, matchSinglePat
import {-#SOURCE#-} DsExpr (dsLExpr, dsExpr)
import DynFlags
import HsSyn
import HsSyn
import TcHsSyn
import TcEvidence
import TcRnMonad
......@@ -52,7 +45,7 @@ import Control.Monad( when )
import qualified Data.Map as Map
\end{code}
This function is a wrapper of @match@, it must be called from all the parts where
This function is a wrapper of @match@, it must be called from all the parts where
it was called match, but only substitutes the first call, ....
if the associated flags are declared, warnings will be issued.
It can not be called matchWrapper because this name already exists :-(
......@@ -61,7 +54,7 @@ JJCQ 30-Nov-1997
\begin{code}
matchCheck :: DsMatchContext
-> [Id] -- Vars rep'ing the exprs we're matching with
-> [Id] -- Vars rep'ing the exprs we're matching with
-> Type -- Type of the case expression
-> [EquationInfo] -- Info about patterns, etc. (type synonym below)
-> DsM MatchResult -- Desugared result!
......@@ -80,11 +73,11 @@ 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
where
(pats, eqns_shadow) = check qs
incomplete = incomplete_flag hs_ctx && (notNull pats)
shadow = wopt Opt_WarnOverlappingPatterns dflags
&& notNull eqns_shadow
&& notNull eqns_shadow
incomplete_flag :: HsMatchContext id -> Bool
incomplete_flag (FunRhs {}) = wopt Opt_WarnIncompletePatterns dflags
......@@ -99,9 +92,9 @@ matchCheck_really dflags ctx@(DsMatchContext hs_ctx _) vars ty qs
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
-- in list comprehensions, pattern guards
-- etc. They are often *supposed* to be
-- incomplete
\end{code}
This variable shows the maximum number of lines of output generated for warnings.
......@@ -123,34 +116,34 @@ dsShadowWarn ctx@(DsMatchContext kind loc) qs
where
warn | qs `lengthExceeds` maximum_output
= pp_context ctx (ptext (sLit "are overlapped"))
(\ f -> vcat (map (ppr_eqn f kind) (take maximum_output qs)) $$
ptext (sLit "..."))
| otherwise
(\ f -> vcat (map (ppr_eqn f kind) (take maximum_output qs)) $$
ptext (sLit "..."))
| otherwise
= pp_context ctx (ptext (sLit "are overlapped"))
(\ f -> vcat $ map (ppr_eqn f kind) qs)
(\ f -> vcat $ map (ppr_eqn f kind) qs)
dsIncompleteWarn :: DsMatchContext -> [ExhaustivePat] -> DsM ()
dsIncompleteWarn ctx@(DsMatchContext kind loc) pats
dsIncompleteWarn ctx@(DsMatchContext kind loc) pats
= putSrcSpanDs loc (warnDs warn)
where
warn = pp_context ctx (ptext (sLit "are non-exhaustive"))
where
warn = pp_context ctx (ptext (sLit "are non-exhaustive"))
(\_ -> hang (ptext (sLit "Patterns not matched:"))
4 ((vcat $ map (ppr_incomplete_pats kind)
(take maximum_output pats))
$$ dots))
4 ((vcat $ map (ppr_incomplete_pats kind)
(take maximum_output pats))
$$ dots))
dots | pats `lengthExceeds` maximum_output = ptext (sLit "...")
| otherwise = empty
dots | pats `lengthExceeds` maximum_output = ptext (sLit "...")
| otherwise = empty
pp_context :: DsMatchContext -> SDoc -> ((SDoc -> SDoc) -> SDoc) -> SDoc
pp_context (DsMatchContext kind _loc) msg rest_of_msg_fun
= vcat [ptext (sLit "Pattern match(es)") <+> msg,
sep [ptext (sLit "In") <+> ppr_match <> char ':', nest 4 (rest_of_msg_fun pref)]]
sep [ptext (sLit "In") <+> ppr_match <> char ':', nest 4 (rest_of_msg_fun pref)]]
where
(ppr_match, pref)
= case kind of
FunRhs fun _ -> (pprMatchContext kind, \ pp -> ppr fun <+> pp)
= case kind of
FunRhs fun _ -> (pprMatchContext kind, \ pp -> ppr fun <+> pp)
_ -> (pprMatchContext kind, \ pp -> pp)
ppr_pats :: Outputable a => [a] -> SDoc
......@@ -163,8 +156,8 @@ ppr_shadow_pats kind pats
ppr_incomplete_pats :: HsMatchContext Name -> ExhaustivePat -> SDoc
ppr_incomplete_pats _ (pats,[]) = ppr_pats pats
ppr_incomplete_pats _ (pats,constraints) =
sep [ppr_pats pats, ptext (sLit "with"),
sep (map ppr_constraint constraints)]
sep [ppr_pats pats, ptext (sLit "with"),
sep (map ppr_constraint constraints)]
ppr_constraint :: (Name,[HsLit]) -> SDoc
ppr_constraint (var,pats) = sep [ppr var, ptext (sLit "`notElem`"), ppr pats]
......@@ -175,9 +168,9 @@ ppr_eqn prefixF kind eqn = prefixF (ppr_shadow_pats kind (eqn_pats eqn))
%************************************************************************
%* *
The main matching function
%* *
%* *
The main matching function
%* *
%************************************************************************
The function @match@ is basically the same as in the Wadler chapter,
......@@ -278,34 +271,34 @@ Wadler-chapter @match@ (p.~93, last clause), and @match_unmixed_blk@
corresponds roughly to @matchVarCon@.
\begin{code}
match :: [Id] -- Variables rep\'ing the exprs we\'re matching with
match :: [Id] -- Variables rep\'ing the exprs we\'re matching with
-> Type -- Type of the case expression
-> [EquationInfo] -- Info about patterns, etc. (type synonym below)
-> [EquationInfo] -- Info about patterns, etc. (type synonym below)
-> DsM MatchResult -- Desugared result!
match [] ty eqns
= ASSERT2( not (null eqns), ppr ty )
return (foldr1 combineMatchResults match_results)
where
match_results = [ ASSERT( null (eqn_pats eqn) )
eqn_rhs eqn
| eqn <- eqns ]
match_results = [ ASSERT( null (eqn_pats eqn) )
eqn_rhs eqn
| eqn <- eqns ]
match vars@(v:_) ty eqns -- Eqns *can* be empty
= do { dflags <- getDynFlags
; -- Tidy the first pattern, generating
-- auxiliary bindings if necessary
= do { dflags <- getDynFlags
; -- Tidy the first pattern, generating
-- auxiliary bindings if necessary
(aux_binds, tidy_eqns) <- mapAndUnzipM (tidyEqnInfo v) eqns
-- Group the equations and match each group in turn
-- Group the equations and match each group in turn
; let grouped = groupEquations dflags tidy_eqns
-- print the view patterns that are commoned up to help debug
; whenDOptM Opt_D_dump_view_pattern_commoning (debug grouped)
; match_results <- match_groups grouped
; return (adjustMatchResult (foldr (.) id aux_binds) $
foldr1 combineMatchResults match_results) }
; match_results <- match_groups grouped
; return (adjustMatchResult (foldr (.) id aux_binds) $
foldr1 combineMatchResults match_results) }
where
dropGroup :: [(PatGroup,EquationInfo)] -> [EquationInfo]
dropGroup = map snd
......@@ -328,19 +321,19 @@ match vars@(v:_) ty eqns -- Eqns *can* be empty
PgCo _ -> matchCoercion vars ty (dropGroup eqns)
PgView _ _ -> matchView vars ty (dropGroup eqns)
PgOverloadedList -> matchOverloadedList 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 literals
debug eqns =
let gs = map (\group -> foldr (\ (p,_) -> \acc ->
case p of PgView e _ -> e:acc
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
in
maybeWarn $ (map (\g -> text "Putting these view expressions into the same case:" <+> (ppr g))
(filter (not . null) gs))
......@@ -349,7 +342,7 @@ matchEmpty :: Id -> Type -> DsM [MatchResult]
matchEmpty var res_ty
= return [MatchResult CanFail mk_seq]
where
mk_seq fail = return $ mkWildCase (Var var) (idType var) res_ty
mk_seq fail = return $ mkWildCase (Var var) (idType var) res_ty
[(DEFAULT, [], fail)]
matchVariables :: [Id] -> Type -> [EquationInfo] -> DsM MatchResult
......@@ -360,45 +353,45 @@ matchVariables [] _ _ = panic "matchVariables"
matchBangs :: [Id] -> Type -> [EquationInfo] -> DsM MatchResult
matchBangs (var:vars) ty eqns
= do { match_result <- match (var:vars) ty $
= do { match_result <- match (var:vars) ty $
map (decomposeFirstPat getBangPat) eqns
; return (mkEvalMatchResult var ty match_result) }
; return (mkEvalMatchResult var ty match_result) }
matchBangs [] _ _ = panic "matchBangs"
matchCoercion :: [Id] -> Type -> [EquationInfo] -> DsM MatchResult
-- Apply the coercion to the match variable and then match that
matchCoercion (var:vars) ty (eqns@(eqn1:_))
= do { let CoPat co pat _ = firstPat eqn1
; var' <- newUniqueId var (hsPatType pat)
; match_result <- match (var':vars) ty $
= do { let CoPat co pat _ = firstPat eqn1
; var' <- newUniqueId var (hsPatType pat)
; match_result <- match (var':vars) ty $
map (decomposeFirstPat getCoPat) eqns
; rhs' <- dsHsWrapper co (Var var)
; return (mkCoLetMatchResult (NonRec var' rhs') match_result) }
; return (mkCoLetMatchResult (NonRec var' rhs') match_result) }
matchCoercion _ _ _ = panic "matchCoercion"
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
= 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
; var' <- newUniqueId var (hsPatType pat)
; match_result <- match (var':vars) ty $
-- do the rest of the compilation
; var' <- newUniqueId var (hsPatType pat)
; match_result <- match (var':vars) ty $
map (decomposeFirstPat getViewPat) eqns
-- compile the view expressions
; viewExpr' <- dsLExpr viewExpr
; return (mkViewMatchResult var' viewExpr' var match_result) }
; return (mkViewMatchResult var' viewExpr' var match_result) }
matchView _ _ _ = panic "matchView"
matchOverloadedList :: [Id] -> Type -> [EquationInfo] -> DsM MatchResult
matchOverloadedList (var:vars) ty (eqns@(eqn1:_))
-- Since overloaded list patterns are treated as view patterns,
-- Since overloaded list patterns are treated as view patterns,
-- the code is roughly the same as for matchView
= do { let ListPat _ elt_ty (Just (_,e)) = firstPat eqn1
= do { let ListPat _ elt_ty (Just (_,e)) = firstPat eqn1
; var' <- newUniqueId var (mkListTy elt_ty) -- we construct the overall type by hand
; match_result <- match (var':vars) ty $
; match_result <- match (var':vars) ty $
map (decomposeFirstPat getOLPat) eqns -- getOLPat builds the pattern inside as a non-overloaded version of the overloaded list pattern
; e' <- dsExpr e
; return (mkViewMatchResult var' e' var match_result) }
......@@ -407,7 +400,7 @@ matchOverloadedList _ _ _ = panic "matchOverloadedList"
-- decompose the first pattern and leave the rest alone
decomposeFirstPat :: (Pat Id -> Pat Id) -> EquationInfo -> EquationInfo
decomposeFirstPat extractpat (eqn@(EqnInfo { eqn_pats = pat : pats }))
= eqn { eqn_pats = extractpat pat : pats}
= eqn { eqn_pats = extractpat pat : pats}
decomposeFirstPat _ _ = panic "decomposeFirstPat"
getCoPat, getBangPat, getViewPat, getOLPat :: Pat Id -> Pat Id
......@@ -433,16 +426,16 @@ pass may remove it if it's inaccessible. (See also Note [Empty case
alternatives] in CoreSyn.)
We do *not* desugar simply to
error "empty case"
error "empty case"
or some such, because 'x' might be bound to (error "hello"), in which
case we want to see that "hello" exception, not (error "empty case").
See also Note [Case elimination: lifted case] in Simplify.
%************************************************************************
%* *
Tidying patterns
%* *
%* *
Tidying patterns
%* *
%************************************************************************
Tidy up the leftmost pattern in an @EquationInfo@, given the variable @v@
......@@ -457,7 +450,7 @@ Replace the `as' pattern @x@@p@ with the pattern p and a binding @x = v@.
Removing lazy (irrefutable) patterns (you don't want to know...).
\item
Converting explicit tuple-, list-, and parallel-array-pats into ordinary
@ConPats@.
@ConPats@.
\item
Convert the literal pat "" to [].
\end{itemize}
......@@ -473,7 +466,7 @@ The @VarPat@ information isn't needed any more after this.
\item[@LitPats@ and @NPats@:]
@LitPats@/@NPats@ of ``known friendly types'' (Int, Char,
Float, Double, at least) are converted to unboxed form; e.g.,
Float, Double, at least) are converted to unboxed form; e.g.,
\tr{(NPat (HsInt i) _ _)} is converted to:
\begin{verbatim}
(ConPat I# _ _ [LitPat (HsIntPrim i)])
......@@ -482,62 +475,62 @@ Float, Double, at least) are converted to unboxed form; e.g.,
\begin{code}
tidyEqnInfo :: Id -> EquationInfo
-> DsM (DsWrapper, EquationInfo)
-- DsM'd because of internal call to dsLHsBinds
-- and mkSelectorBinds.
-- "tidy1" does the interesting stuff, looking at
-- one pattern and fiddling the list of bindings.
--
-- POST CONDITION: head pattern in the EqnInfo is
-- WildPat
-- ConPat
-- NPat
-- LitPat
-- NPlusKPat
-- but no other
tidyEqnInfo _ (EqnInfo { eqn_pats = [] })
-> DsM (DsWrapper, EquationInfo)
-- DsM'd because of internal call to dsLHsBinds
-- and mkSelectorBinds.
-- "tidy1" does the interesting stuff, looking at
-- one pattern and fiddling the list of bindings.
--
-- POST CONDITION: head pattern in the EqnInfo is
-- WildPat
-- ConPat
-- NPat
-- LitPat
-- NPlusKPat
-- but no other
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 }) }
tidy1 :: Id -- The Id being scrutinised
-> Pat Id -- The pattern against which it is to be matched
-> DsM (DsWrapper, -- Extra bindings to do before the match
Pat Id) -- Equivalent pattern
tidy1 :: Id -- The Id being scrutinised
-> Pat Id -- The pattern against which it is to be matched
-> DsM (DsWrapper, -- Extra bindings to do before the match
Pat Id) -- Equivalent pattern
-------------------------------------------------------
-- (pat', mr') = tidy1 v pat mr
-- (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
-- ConPatOut
-- LitPat
-- NPat
-- NPlusKPat
tidy1 v (ParPat pat) = tidy1 v (unLoc pat)
tidy1 v (SigPatOut pat _) = tidy1 v (unLoc pat)
-- WildPat
-- ConPatOut
-- LitPat
-- NPat
-- NPlusKPat
tidy1 v (ParPat pat) = tidy1 v (unLoc pat)
tidy1 v (SigPatOut pat _) = tidy1 v (unLoc pat)
tidy1 _ (WildPat ty) = return (idDsWrapper, WildPat ty)
tidy1 v (BangPat (L l p)) = tidy_bang_pat v l p
-- case v of { x -> mr[] }
-- = case v of { _ -> let x=v in mr[] }
-- case v of { x -> mr[] }
-- = case v of { _ -> let x=v in mr[] }
tidy1 v (VarPat var)
= return (wrapBind var v, WildPat (idType var))
= return (wrapBind var v, WildPat (idType var))
-- case v of { x@p -> mr[] }
-- = case v of { p -> let x=v in mr[] }
-- case v of { x@p -> mr[] }
-- = case v of { p -> let x=v in mr[] }
tidy1 v (AsPat (L _ var) pat)
= do { (wrap, pat') <- tidy1 v (unLoc pat)
; return (wrapBind var v . wrap, pat') }
= do { (wrap, pat') <- tidy1 v (unLoc pat)
; return (wrapBind var v . wrap, pat') }
{- now, here we handle lazy patterns:
tidy1 v ~p bs = (v, v1 = case v of p -> v1 :
v2 = case v of p -> v2 : ... : bs )
v2 = case v of p -> v2 : ... : bs )
where the v_i's are the binders in the pattern.
......@@ -548,16 +541,16 @@ tidy1 v (AsPat (L _ var) pat)
tidy1 v (LazyPat pat)
= do { sel_prs <- mkSelectorBinds [] pat (Var v)
; let sel_binds = [NonRec b rhs | (b,rhs) <- sel_prs]
; return (mkCoreLets sel_binds, WildPat (idType v)) }
; let sel_binds = [NonRec b rhs | (b,rhs) <- sel_prs]
; return (mkCoreLets sel_binds, WildPat (idType v)) }
tidy1 _ (ListPat pats ty Nothing)
= return (idDsWrapper, unLoc list_ConPat)
where
list_ty = mkListTy ty
list_ConPat = foldr (\ x y -> mkPrefixConPat consDataCon [x, y] list_ty)
(mkNilPat list_ty)
pats
(mkNilPat list_ty)
pats
-- Introduce fake parallel array constructors to be able to handle parallel
-- arrays with the existing machinery for constructor pattern
......@@ -604,7 +597,7 @@ tidy_bang_pat v _ (ParPat (L l p)) = tidy_bang_pat v l p
tidy_bang_pat v _ (SigPatOut (L l p) _) = tidy_bang_pat v l p
-- Push the bang-pattern inwards, in the hope that
-- it may disappear next time
-- it may disappear next time
tidy_bang_pat v l (AsPat v' p) = tidy1 v (AsPat v' (L l (BangPat p)))
tidy_bang_pat v l (CoPat w p t) = tidy1 v (CoPat w (BangPat (L l p)) t)
......@@ -640,9 +633,9 @@ evaluation of \tr{e}. An alternative translation (No.~2):
\end{verbatim}
%************************************************************************
%* *
%* *
\subsubsection[improved-unmixing]{UNIMPLEMENTED idea for improved unmixing}
%* *
%* *
%************************************************************************
We might be able to optimise unmixing when confronted by
......@@ -680,9 +673,9 @@ Presumably just a variant on the constructor case (as it is now).
\end{description}
%************************************************************************
%* *
%* matchWrapper: a convenient way to call @match@ *
%* *
%* *
%* matchWrapper: a convenient way to call @match@ *
%* *
%************************************************************************
\subsection[matchWrapper]{@matchWrapper@: a convenient interface to @match@}
......@@ -724,9 +717,9 @@ Call @match@ with all of this information!
\end{enumerate}
\begin{code}
matchWrapper :: HsMatchContext Name -- For shadowing warning messages
-> MatchGroup Id (LHsExpr Id) -- Matches being desugared
-> DsM ([Id], CoreExpr) -- Results
matchWrapper :: HsMatchContext Name -- For shadowing warning messages
-> MatchGroup Id (LHsExpr Id) -- Matches being desugared
-> DsM ([Id], CoreExpr) -- Results
\end{code}
There is one small problem with the Lambda Patterns, when somebody
......@@ -734,7 +727,7 @@ matchWrapper :: HsMatchContext Name -- For shadowing warning message
\begin{verbatim}
(\ (x:xs) -> ...)
\end{verbatim}
he/she don't want a warning about incomplete patterns, that is done with
he/she don't want a warning about incomplete patterns, that is done with
the flag @opt_WarnSimplePatterns@.
This problem also appears in the:
\begin{itemize}
......@@ -755,37 +748,37 @@ JJQC 30-Nov-1997
matchWrapper ctxt (MG { mg_alts = matches
, mg_arg_tys = arg_tys
, mg_res_ty = rhs_ty })
= do { eqns_info <- mapM mk_eqn_info matches
; new_vars <- case matches of
= do { eqns_info <- mapM mk_eqn_info matches
; new_vars <- case matches of
[] -> mapM newSysLocalDs arg_tys
(m:_) -> selectMatchVars (map unLoc (hsLMatchPats m))
; result_expr <- matchEquations ctxt new_vars eqns_info rhs_ty
; return (new_vars, result_expr) }
; result_expr <- matchEquations ctxt new_vars eqns_info rhs_ty
; return (new_vars, result_expr) }
where
mk_eqn_info (L _ (Match pats _ grhss))
= do { let upats = map unLoc pats
; match_result <- dsGRHSs ctxt upats grhss rhs_ty
; return (EqnInfo { eqn_pats = upats, eqn_rhs = match_result}) }
; match_result <- dsGRHSs ctxt upats grhss rhs_ty
; return (EqnInfo { eqn_pats = upats, eqn_rhs = match_result}) }
matchEquations :: HsMatchContext Name
-> [Id] -> [EquationInfo] -> Type
-> DsM CoreExpr
-> [Id] -> [EquationInfo] -> Type
-> DsM CoreExpr
matchEquations ctxt vars eqns_info rhs_ty
= do { locn <- getSrcSpanDs
; let ds_ctxt = DsMatchContext ctxt locn
error_doc = matchContextErrString ctxt
= do { locn <- getSrcSpanDs
; let ds_ctxt = DsMatchContext ctxt locn
error_doc = matchContextErrString ctxt
; match_result <- matchCheck ds_ctxt vars rhs_ty eqns_info
; match_result <- matchCheck ds_ctxt vars rhs_ty eqns_info
; fail_expr <- mkErrorAppDs pAT_ERROR_ID rhs_ty error_doc
; extractMatchResult match_result fail_expr }
; fail_expr <- mkErrorAppDs pAT_ERROR_ID rhs_ty error_doc
; extractMatchResult match_result fail_expr }
\end{code}
%************************************************************************
%* *
%* *
\subsection[matchSimply]{@matchSimply@: match a single expression against a single pattern}
%* *
%* *
%************************************************************************
@mkSimpleMatch@ is a wrapper for @match@ which deals with the
......@@ -793,12 +786,12 @@ situation where we want to match a single expression against a single
pattern. It returns an expression.
\begin{code}
matchSimply :: CoreExpr -- Scrutinee
-> HsMatchContext Name -- Match kind
-> LPat Id -- Pattern it should match
-> CoreExpr -- Return this if it matches
-> CoreExpr -- Return this if it doesn't
-> DsM CoreExpr
matchSimply :: CoreExpr -- Scrutinee
-> HsMatchContext Name -- Match kind
-> LPat Id -- Pattern it should match
-> CoreExpr -- Return this if it matches
-> CoreExpr -- Return this if it doesn't
-> DsM CoreExpr
-- Do not warn about incomplete patterns; see matchSinglePat comments
matchSimply scrut hs_ctx pat result_expr fail_expr = do
let
......@@ -809,14 +802,14 @@ matchSimply scrut hs_ctx pat result_expr fail_expr = do
extractMatchResult match_result' fail_expr
matchSinglePat :: CoreExpr -> HsMatchContext Name -> LPat Id
-> Type -> MatchResult -> DsM MatchResult
-> Type -> MatchResult -> DsM MatchResult
-- Do not warn about incomplete patterns
-- Used for things like [ e | pat <- stuff ], where
-- Used for things like [ e | pat <- stuff ], where
-- incomplete patterns are just fine
matchSinglePat (Var var) ctx (L _ pat) ty match_result
matchSinglePat (Var var) ctx (L _ pat) ty match_result
= do { locn <- getSrcSpanDs
; matchCheck (DsMatchContext ctx locn)
[var] ty
[var] ty
[EqnInfo { eqn_pats = [pat], eqn_rhs = match_result }] }
matchSinglePat scrut hs_ctx pat ty match_result
......@@ -827,29 +820,29 @@ matchSinglePat scrut hs_ctx pat ty match_result
%************************************************************************
%* *
Pattern classification
%* *
%* *
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*
= 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*
| 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)
| PgOverloadedList
groupEquations :: DynFlags -> [EquationInfo] -> [[(PatGroup, EquationInfo)]]
-- If the result is of form [g1, g2, g3],
-- 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
-- The ordering of equations is unchanged
......@@ -860,11 +853,11 @@ groupEquations dflags eqns
(pg1,_) `same_gp` (pg2,_) = pg1 `sameGroup` pg2
subGroup :: Ord a => [(a, EquationInfo)] -> [[EquationInfo]]
-- Input is a particular group. The result sub-groups the
-- Input is a particular group. The result sub-groups the
-- equations by with particular constructor, literal etc they match.
-- Each sub-list in the result has the same PatGroup
-- See Note [Take care with pattern order]
subGroup group
subGroup group
= map reverse $ Map.elems $ foldl accumulate Map.empty group
where
accumulate pg_map (pg, eqn)
......@@ -880,27 +873,27 @@ 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
Then in bringing together the patterns for True, we must not
swap the Nothing and y!
\begin{code}
sameGroup :: PatGroup -> PatGroup -> Bool