%
% (c) The University of Glasgow 2006
% (c) The GRASP/AQUA Project, Glasgow University, 19921998
%
\section[PatSyntax]{Abstract Haskell syntax---patterns}
\begin{code}
module HsPat (
Pat(..), InPat, OutPat, LPat,
HsConDetails(..),
HsConPatDetails, hsConPatArgs,
HsRecFields(..), HsRecField(..), hsRecFields,
HsQuasiQuote(..),
mkPrefixConPat, mkCharLitPat, mkNilPat, mkCoPat, mkCoPatCoI,
isBangHsBind, hsPatNeedsParens,
patsAreAllCons, isConPat, isSigPat, isWildPat,
patsAreAllLits, isLitPat, isIrrefutableHsPat
) where
import HsExpr (SyntaxExpr, LHsExpr, pprLExpr)
import HsBinds
import HsLit
import HsTypes
import BasicTypes
import Coercion
import PprCore ( )
import TysWiredIn
import Var
import DataCon
import TyCon
import Outputable
import Type
import SrcLoc
import FastString
\end{code}
\begin{code}
type InPat id = LPat id
type OutPat id = LPat id
type LPat id = Located (Pat id)
data Pat id
=
WildPat PostTcType
| VarPat id
| VarPatOut id (DictBinds id)
| LazyPat (LPat id)
| AsPat (Located id) (LPat id)
| ParPat (LPat id)
| BangPat (LPat id)
| ListPat [LPat id]
PostTcType
| TuplePat [LPat id]
Boxity
PostTcType
| PArrPat [LPat id]
PostTcType
| ConPatIn (Located id)
(HsConPatDetails id)
| ConPatOut {
pat_con :: Located DataCon,
pat_tvs :: [TyVar],
pat_dicts :: [id],
pat_binds :: DictBinds id,
pat_args :: HsConPatDetails id,
pat_ty :: Type
}
| ViewPat (LHsExpr id)
(LPat id)
PostTcType
| QuasiQuotePat (HsQuasiQuote id)
| LitPat HsLit
| NPat (HsOverLit id)
(Maybe (SyntaxExpr id))
(SyntaxExpr id)
| NPlusKPat (Located id)
(HsOverLit id)
(SyntaxExpr id)
(SyntaxExpr id)
| TypePat (LHsType id)
| SigPatIn (LPat id)
(LHsType id)
| SigPatOut (LPat id)
Type
| CoPat HsWrapper
(Pat id)
Type
\end{code}
HsConDetails is use for patterns/expressions *and* for data type declarations
\begin{code}
data HsConDetails arg rec
= PrefixCon [arg]
| RecCon rec
| InfixCon arg arg
type HsConPatDetails id = HsConDetails (LPat id) (HsRecFields id (LPat id))
hsConPatArgs :: HsConPatDetails id -> [LPat id]
hsConPatArgs (PrefixCon ps) = ps
hsConPatArgs (RecCon fs) = map hsRecFieldArg (rec_flds fs)
hsConPatArgs (InfixCon p1 p2) = [p1,p2]
\end{code}
However HsRecFields is used only for patterns and expressions
(not data type declarations)
\begin{code}
data HsRecFields id arg
= HsRecFields { rec_flds :: [HsRecField id arg],
rec_dotdot :: Maybe Int }
data HsRecField id arg = HsRecField {
hsRecFieldId :: Located id,
hsRecFieldArg :: arg,
hsRecPun :: Bool
}
hsRecFields :: HsRecFields id arg -> [id]
hsRecFields rbinds = map (unLoc . hsRecFieldId) (rec_flds rbinds)
\end{code}
\begin{code}
data HsQuasiQuote id = HsQuasiQuote
id
id
SrcSpan
FastString
instance OutputableBndr id => Outputable (HsQuasiQuote id) where
ppr = ppr_qq
ppr_qq :: OutputableBndr id => HsQuasiQuote id -> SDoc
ppr_qq (HsQuasiQuote name quoter _ quote) =
char '$' <> brackets (ppr name) <>
ptext (sLit "[:") <> ppr quoter <> ptext (sLit "|") <>
ppr quote <> ptext (sLit "|]")
\end{code}
%************************************************************************
%* *
%* Printing patterns
%* *
%************************************************************************
\begin{code}
instance (OutputableBndr name) => Outputable (Pat name) where
ppr = pprPat
pprPatBndr :: OutputableBndr name => name -> SDoc
pprPatBndr var
= getPprStyle $ \ sty ->
if debugStyle sty then
parens (pprBndr LambdaBind var)
else
ppr var
pprPat :: (OutputableBndr name) => Pat name -> SDoc
pprPat (VarPat var) = pprPatBndr var
pprPat (VarPatOut var bs) = parens (pprPatBndr var <+> braces (ppr bs))
pprPat (WildPat _) = char '_'
pprPat (LazyPat pat) = char '~' <> ppr pat
pprPat (BangPat pat) = char '!' <> ppr pat
pprPat (AsPat name pat) = parens (hcat [ppr name, char '@', ppr pat])
pprPat (ViewPat expr pat _) = parens (hcat [pprLExpr expr, text " -> ", ppr pat])
pprPat (ParPat pat) = parens (ppr pat)
pprPat (ListPat pats _) = brackets (interpp'SP pats)
pprPat (PArrPat pats _) = pabrackets (interpp'SP pats)
pprPat (TuplePat pats bx _) = tupleParens bx (interpp'SP pats)
pprPat (ConPatIn con details) = pprUserCon con details
pprPat (ConPatOut { pat_con = con, pat_tvs = tvs, pat_dicts = dicts,
pat_binds = binds, pat_args = details })
= getPprStyle $ \ sty ->
if debugStyle sty then
ppr con <+> sep [ hsep (map pprPatBndr tvs) <+> hsep (map pprPatBndr dicts),
pprLHsBinds binds, pprConArgs details]
else pprUserCon con details
pprPat (LitPat s) = ppr s
pprPat (NPat l Nothing _) = ppr l
pprPat (NPat l (Just _) _) = char '-' <> ppr l
pprPat (NPlusKPat n k _ _) = hcat [ppr n, char '+', ppr k]
pprPat (QuasiQuotePat (HsQuasiQuote name quoter _ quote))
= char '$' <> brackets (ppr name) <>
ptext (sLit "[:") <> ppr quoter <> ptext (sLit "|") <>
ppr quote <> ptext (sLit "|]")
pprPat (TypePat ty) = ptext (sLit "{|") <> ppr ty <> ptext (sLit "|}")
pprPat (CoPat co pat _) = parens (pprHsWrapper (ppr pat) co)
pprPat (SigPatIn pat ty) = ppr pat <+> dcolon <+> ppr ty
pprPat (SigPatOut pat ty) = ppr pat <+> dcolon <+> ppr ty
pprUserCon :: (Outputable con, OutputableBndr id) => con -> HsConPatDetails id -> SDoc
pprUserCon c (InfixCon p1 p2) = ppr p1 <+> ppr c <+> ppr p2
pprUserCon c details = ppr c <+> pprConArgs details
pprConArgs :: OutputableBndr id => HsConPatDetails id -> SDoc
pprConArgs (PrefixCon pats) = interppSP pats
pprConArgs (InfixCon p1 p2) = interppSP [p1,p2]
pprConArgs (RecCon rpats) = ppr rpats
instance (OutputableBndr id, Outputable arg)
=> Outputable (HsRecFields id arg) where
ppr (HsRecFields { rec_flds = flds, rec_dotdot = Nothing })
= braces (fsep (punctuate comma (map ppr flds)))
ppr (HsRecFields { rec_flds = flds, rec_dotdot = Just n })
= braces (fsep (punctuate comma (map ppr (take n flds) ++ [dotdot])))
where
dotdot = ptext (sLit "..") <+> ifPprDebug (ppr (drop n flds))
instance (OutputableBndr id, Outputable arg)
=> Outputable (HsRecField id arg) where
ppr (HsRecField { hsRecFieldId = f, hsRecFieldArg = arg,
hsRecPun = pun })
= ppr f <+> (if pun then empty else equals <+> ppr arg)
pabrackets :: SDoc -> SDoc
pabrackets p = ptext (sLit "[:") <> p <> ptext (sLit ":]")
\end{code}
%************************************************************************
%* *
%* Building patterns
%* *
%************************************************************************
\begin{code}
mkPrefixConPat :: DataCon -> [OutPat id] -> Type -> OutPat id
mkPrefixConPat dc pats ty
= noLoc $ ConPatOut { pat_con = noLoc dc, pat_tvs = [], pat_dicts = [],
pat_binds = emptyLHsBinds, pat_args = PrefixCon pats,
pat_ty = ty }
mkNilPat :: Type -> OutPat id
mkNilPat ty = mkPrefixConPat nilDataCon [] ty
mkCharLitPat :: Char -> OutPat id
mkCharLitPat c = mkPrefixConPat charDataCon [noLoc $ LitPat (HsCharPrim c)] charTy
mkCoPat :: HsWrapper -> Pat id -> Type -> Pat id
mkCoPat co pat ty
| isIdHsWrapper co = pat
| otherwise = CoPat co pat ty
mkCoPatCoI :: CoercionI -> Pat id -> Type -> Pat id
mkCoPatCoI IdCo pat _ = pat
mkCoPatCoI (ACo co) pat ty = mkCoPat (WpCast co) pat ty
\end{code}
%************************************************************************
%* *
%* Predicates for checking things about patternlists in EquationInfo *
%* *
%************************************************************************
\subsection[Patlistpredicates]{Look for interesting things in patterns}
Unlike in the Wadler chapter, where patterns are either ``variables''
or ``constructors,'' here we distinguish between:
\begin{description}
\item[unfailable:]
Patterns that cannot fail to match: variables, wildcards, and lazy
patterns.
These are the irrefutable patterns; the two other categories
are refutable patterns.
\item[constructor:]
A nonliteral constructor pattern (see next category).
\item[literal patterns:]
At least the numeric ones may be overloaded.
\end{description}
A pattern is in {\em exactly one} of the above three categories; `as'
patterns are treated specially, of course.
The 1.3 report defines what ``irrefutable'' and ``failurefree'' patterns are.
\begin{code}
isWildPat :: Pat id -> Bool
isWildPat (WildPat _) = True
isWildPat _ = False
patsAreAllCons :: [Pat id] -> Bool
patsAreAllCons pat_list = all isConPat pat_list
isConPat :: Pat id -> Bool
isConPat (AsPat _ pat) = isConPat (unLoc pat)
isConPat (ConPatIn {}) = True
isConPat (ConPatOut {}) = True
isConPat (ListPat {}) = True
isConPat (PArrPat {}) = True
isConPat (TuplePat {}) = True
isConPat _ = False
isSigPat :: Pat id -> Bool
isSigPat (SigPatIn _ _) = True
isSigPat (SigPatOut _ _) = True
isSigPat _ = False
patsAreAllLits :: [Pat id] -> Bool
patsAreAllLits pat_list = all isLitPat pat_list
isLitPat :: Pat id -> Bool
isLitPat (AsPat _ pat) = isLitPat (unLoc pat)
isLitPat (LitPat _) = True
isLitPat (NPat _ _ _) = True
isLitPat (NPlusKPat _ _ _ _) = True
isLitPat _ = False
isBangHsBind :: HsBind id -> Bool
isBangHsBind (PatBind { pat_lhs = L _ (BangPat _) }) = True
isBangHsBind _ = False
isIrrefutableHsPat :: OutputableBndr id => LPat id -> Bool
isIrrefutableHsPat pat
= go pat
where
go (L _ pat) = go1 pat
go1 (WildPat {}) = True
go1 (VarPat {}) = True
go1 (VarPatOut {}) = True
go1 (LazyPat {}) = True
go1 (BangPat pat) = go pat
go1 (CoPat _ pat _) = go1 pat
go1 (ParPat pat) = go pat
go1 (AsPat _ pat) = go pat
go1 (ViewPat _ pat _) = go pat
go1 (SigPatIn pat _) = go pat
go1 (SigPatOut pat _) = go pat
go1 (TuplePat pats _ _) = all go pats
go1 (ListPat {}) = False
go1 (PArrPat {}) = False
go1 (ConPatIn {}) = False
go1 (ConPatOut{ pat_con = L _ con, pat_args = details })
= isProductTyCon (dataConTyCon con)
&& all go (hsConPatArgs details)
go1 (LitPat {}) = False
go1 (NPat {}) = False
go1 (NPlusKPat {}) = False
go1 (QuasiQuotePat {}) = urk pat
go1 (TypePat {}) = urk pat
urk pat = pprPanic "isIrrefutableHsPat:" (ppr pat)
hsPatNeedsParens :: Pat a -> Bool
hsPatNeedsParens (WildPat {}) = False
hsPatNeedsParens (VarPat {}) = False
hsPatNeedsParens (VarPatOut {}) = True
hsPatNeedsParens (LazyPat {}) = False
hsPatNeedsParens (BangPat {}) = False
hsPatNeedsParens (CoPat {}) = True
hsPatNeedsParens (ParPat {}) = False
hsPatNeedsParens (AsPat {}) = False
hsPatNeedsParens (ViewPat {}) = True
hsPatNeedsParens (SigPatIn {}) = True
hsPatNeedsParens (SigPatOut {}) = True
hsPatNeedsParens (TuplePat {}) = False
hsPatNeedsParens (ListPat {}) = False
hsPatNeedsParens (PArrPat {}) = False
hsPatNeedsParens (ConPatIn _ ds) = conPatNeedsParens ds
hsPatNeedsParens (ConPatOut {}) = True
hsPatNeedsParens (LitPat {}) = False
hsPatNeedsParens (NPat {}) = False
hsPatNeedsParens (NPlusKPat {}) = True
hsPatNeedsParens (QuasiQuotePat {}) = True
hsPatNeedsParens (TypePat {}) = False
conPatNeedsParens :: HsConDetails a b -> Bool
conPatNeedsParens (PrefixCon args) = not (null args)
conPatNeedsParens (InfixCon {}) = False
conPatNeedsParens (RecCon {}) = False
\end{code}