%
+% (c) The University of Glasgow 2006
% (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
%
-\section[MatchLit]{Pattern-matching literal patterns}
+
+Pattern-matching literal patterns
\begin{code}
module MatchLit ( dsLit, dsOverLit, hsLitKey, hsOverLitKey,
import DsUtils
import HsSyn
-import Id ( Id, idType )
+
+import Id
import CoreSyn
-import TyCon ( tyConDataCons )
-import DataCon ( DataCon )
-import TcType ( tcSplitTyConApp, isIntegerTy, isIntTy,
- isFloatTy, isDoubleTy, isStringTy )
-import Type ( Type )
-import PrelNames ( ratioTyConKey )
-import TysWiredIn ( stringTy, consDataCon, intDataCon, floatDataCon, doubleDataCon )
-import PrelNames ( eqStringName )
-import Unique ( hasKey )
-import Literal ( mkMachInt, Literal(..) )
-import SrcLoc ( noLoc )
-import Ratio ( numerator, denominator )
-import SrcLoc ( Located(..), unLoc )
+import MkCore
+import TyCon
+import DataCon
+import TcHsSyn ( shortCutLit )
+import TcType
+import PrelNames
+import TysWiredIn
+import Literal
+import SrcLoc
+import Data.Ratio
import Outputable
-import Util ( mapAndUnzip )
-import FastString ( lengthFS, unpackFS )
+import Util
+import FastString
\end{code}
%************************************************************************
\begin{code}
dsLit :: HsLit -> DsM CoreExpr
-dsLit (HsStringPrim s) = returnDs (mkLit (MachStr s))
-dsLit (HsCharPrim c) = returnDs (mkLit (MachChar c))
-dsLit (HsIntPrim i) = returnDs (mkLit (MachInt i))
-dsLit (HsFloatPrim f) = returnDs (mkLit (MachFloat f))
-dsLit (HsDoublePrim d) = returnDs (mkLit (MachDouble d))
-
-dsLit (HsChar c) = returnDs (mkCharExpr c)
+dsLit (HsStringPrim s) = return (Lit (MachStr s))
+dsLit (HsCharPrim c) = return (Lit (MachChar c))
+dsLit (HsIntPrim i) = return (Lit (MachInt i))
+dsLit (HsWordPrim w) = return (Lit (MachWord w))
+dsLit (HsFloatPrim f) = return (Lit (MachFloat f))
+dsLit (HsDoublePrim d) = return (Lit (MachDouble d))
+
+dsLit (HsChar c) = return (mkCharExpr c)
dsLit (HsString str) = mkStringExprFS str
dsLit (HsInteger i _) = mkIntegerExpr i
-dsLit (HsInt i) = returnDs (mkIntExpr i)
+dsLit (HsInt i) = return (mkIntExpr i)
-dsLit (HsRat r ty)
- = mkIntegerExpr (numerator r) `thenDs` \ num ->
- mkIntegerExpr (denominator r) `thenDs` \ denom ->
- returnDs (mkConApp ratio_data_con [Type integer_ty, num, denom])
+dsLit (HsRat r ty) = do
+ num <- mkIntegerExpr (numerator r)
+ denom <- mkIntegerExpr (denominator r)
+ return (mkConApp ratio_data_con [Type integer_ty, num, denom])
where
(ratio_data_con, integer_ty)
- = case tcSplitTyConApp ty of
- (tycon, [i_ty]) -> ASSERT(isIntegerTy i_ty && tycon `hasKey` ratioTyConKey)
- (head (tyConDataCons tycon), i_ty)
+ = case tcSplitTyConApp ty of
+ (tycon, [i_ty]) -> ASSERT(isIntegerTy i_ty && tycon `hasKey` ratioTyConKey)
+ (head (tyConDataCons tycon), i_ty)
+ x -> pprPanic "dsLit" (ppr x)
dsOverLit :: HsOverLit Id -> DsM CoreExpr
-- Post-typechecker, the SyntaxExpr field of an OverLit contains
-- (an expression for) the literal value itself
-dsOverLit (HsIntegral _ lit) = dsExpr lit
-dsOverLit (HsFractional _ lit) = dsExpr lit
+dsOverLit (OverLit { ol_val = val, ol_rebindable = rebindable
+ , ol_witness = witness, ol_type = ty })
+ | not rebindable
+ , Just expr <- shortCutLit val ty = dsExpr expr -- Note [Literal short cut]
+ | otherwise = dsExpr witness
\end{code}
+Note [Literal short cut]
+~~~~~~~~~~~~~~~~~~~~~~~~
+The type checker tries to do this short-cutting as early as possible, but
+becuase of unification etc, more information is available to the desugarer.
+And where it's possible to generate the correct literal right away, it's
+much better do do so.
+
+
\begin{code}
hsLitKey :: HsLit -> Literal
-- Get a Core literal to use (only) a grouping key
-- It only works for primitive types and strings;
-- others have been removed by tidy
hsLitKey (HsIntPrim i) = mkMachInt i
+hsLitKey (HsWordPrim w) = mkMachWord w
hsLitKey (HsCharPrim c) = MachChar c
hsLitKey (HsStringPrim s) = MachStr s
hsLitKey (HsFloatPrim f) = MachFloat f
hsLitKey (HsDoublePrim d) = MachDouble d
hsLitKey (HsString s) = MachStr s
+hsLitKey l = pprPanic "hsLitKey" (ppr l)
-hsOverLitKey :: HsOverLit a -> Bool -> Literal
+hsOverLitKey :: OutputableBndr a => HsOverLit a -> Bool -> Literal
-- Ditto for HsOverLit; the boolean indicates to negate
-hsOverLitKey (HsIntegral i _) False = MachInt i
-hsOverLitKey (HsIntegral i _) True = MachInt (-i)
-hsOverLitKey (HsFractional r _) False = MachFloat r
-hsOverLitKey (HsFractional r _) True = MachFloat (-r)
+hsOverLitKey (OverLit { ol_val = l }) neg = litValKey l neg
+
+litValKey :: OverLitVal -> Bool -> Literal
+litValKey (HsIntegral i) False = MachInt i
+litValKey (HsIntegral i) True = MachInt (-i)
+litValKey (HsFractional r) False = MachFloat r
+litValKey (HsFractional r) True = MachFloat (-r)
+litValKey (HsIsString s) neg = ASSERT( not neg) MachStr s
\end{code}
%************************************************************************
\begin{code}
tidyLitPat :: HsLit -> Pat Id
-- Result has only the following HsLits:
--- HsIntPrim, HsCharPrim, HsFloatPrim
+-- HsIntPrim, HsWordPrim, HsCharPrim, HsFloatPrim
-- HsDoublePrim, HsStringPrim, HsString
-- * HsInteger, HsRat, HsInt can't show up in LitPats
-- * We get rid of HsChar right here
tidyLitPat lit = LitPat lit
----------------
-tidyNPat :: HsOverLit Id -> Maybe (SyntaxExpr Id) -> SyntaxExpr Id
- -> Type -> Pat Id
-tidyNPat over_lit mb_neg eq lit_ty
- | isIntTy lit_ty = mk_con_pat intDataCon (HsIntPrim int_val)
- | isFloatTy lit_ty = mk_con_pat floatDataCon (HsFloatPrim rat_val)
- | isDoubleTy lit_ty = mk_con_pat doubleDataCon (HsDoublePrim rat_val)
- | otherwise = NPat over_lit mb_neg eq lit_ty
+tidyNPat :: HsOverLit Id -> Maybe (SyntaxExpr Id) -> SyntaxExpr Id -> Pat Id
+tidyNPat (OverLit val False _ ty) mb_neg _
+ -- False: Take short cuts only if the literal is not using rebindable syntax
+ --
+ -- Once that is settled, look for cases where the type of the
+ -- entire overloaded literal matches the type of the underlying literal,
+ -- and in that case take the short cut
+ -- NB: Watch out for wierd cases like Trac #3382
+ -- f :: Int -> Int
+ -- f "blah" = 4
+ -- which might be ok if we hvae 'instance IsString Int'
+ --
+
+ | isIntTy ty, Just int_lit <- mb_int_lit = mk_con_pat intDataCon (HsIntPrim int_lit)
+ | isWordTy ty, Just int_lit <- mb_int_lit = mk_con_pat wordDataCon (HsWordPrim int_lit)
+ | isFloatTy ty, Just rat_lit <- mb_rat_lit = mk_con_pat floatDataCon (HsFloatPrim rat_lit)
+ | isDoubleTy ty, Just rat_lit <- mb_rat_lit = mk_con_pat doubleDataCon (HsDoublePrim rat_lit)
+ | isStringTy ty, Just str_lit <- mb_str_lit = tidyLitPat (HsString str_lit)
where
mk_con_pat :: DataCon -> HsLit -> Pat Id
- mk_con_pat con lit = unLoc (mkPrefixConPat con [noLoc $ LitPat lit] lit_ty)
- neg_lit = case (mb_neg, over_lit) of
- (Nothing, _) -> over_lit
- (Just _, HsIntegral i s) -> HsIntegral (-i) s
- (Just _, HsFractional f s) -> HsFractional (-f) s
-
- int_val :: Integer
- int_val = case neg_lit of
- HsIntegral i _ -> i
- HsFractional f _ -> panic "tidyNPat"
+ mk_con_pat con lit = unLoc (mkPrefixConPat con [noLoc $ LitPat lit] ty)
+
+ mb_int_lit :: Maybe Integer
+ mb_int_lit = case (mb_neg, val) of
+ (Nothing, HsIntegral i) -> Just i
+ (Just _, HsIntegral i) -> Just (-i)
+ _ -> Nothing
+
+ mb_rat_lit :: Maybe Rational
+ mb_rat_lit = case (mb_neg, val) of
+ (Nothing, HsIntegral i) -> Just (fromInteger i)
+ (Just _, HsIntegral i) -> Just (fromInteger (-i))
+ (Nothing, HsFractional f) -> Just f
+ (Just _, HsFractional f) -> Just (-f)
+ _ -> Nothing
- rat_val :: Rational
- rat_val = case neg_lit of
- HsIntegral i _ -> fromInteger i
- HsFractional f _ -> f
+ mb_str_lit :: Maybe FastString
+ mb_str_lit = case (mb_neg, val) of
+ (Nothing, HsIsString s) -> Just s
+ _ -> Nothing
+
+tidyNPat over_lit mb_neg eq
+ = NPat over_lit mb_neg eq
\end{code}
-> DsM MatchResult
matchLiterals (var:vars) ty sub_groups
- = do { -- Deal with each group
+ = ASSERT( all notNull sub_groups )
+ do { -- Deal with each group
; alts <- mapM match_group sub_groups
-- Combine results. For everything except String
= do { lit <- mkStringExprFS s
; let pred = mkApps (Var eq_str) [Var var, lit]
; return (mkGuardedMatchResult pred mr) }
+ wrap_str_guard _ (l, _) = pprPanic "matchLiterals/wrap_str_guard" (ppr l)
+
+matchLiterals [] _ _ = panic "matchLiterals []"
\end{code}
%************************************************************************
\begin{code}
-matchNPats :: [Id] -> Type -> [[EquationInfo]] -> DsM MatchResult
- -- All NPats, but perhaps for different literals
-matchNPats vars ty groups
- = do { match_results <- mapM (matchOneNPat vars ty) groups
- ; return (foldr1 combineMatchResults match_results) }
-
-matchOneNPat (var:vars) ty (eqn1:eqns) -- All for the same literal
- = do { let NPat lit mb_neg eq_chk _ = firstPat eqn1
+matchNPats :: [Id] -> Type -> [EquationInfo] -> DsM MatchResult
+matchNPats (var:vars) ty (eqn1:eqns) -- All for the same literal
+ = do { let NPat lit mb_neg eq_chk = firstPat eqn1
; lit_expr <- dsOverLit lit
; neg_lit <- case mb_neg of
Nothing -> return lit_expr
; let pred_expr = mkApps eq_expr [Var var, neg_lit]
; match_result <- match vars ty (shiftEqns (eqn1:eqns))
; return (mkGuardedMatchResult pred_expr match_result) }
+matchNPats vars _ eqns = pprPanic "matchOneNPat" (ppr (vars, eqns))
\end{code}
\begin{code}
matchNPlusKPats :: [Id] -> Type -> [EquationInfo] -> DsM MatchResult
- -- All NPlusKPats, for the *same* literal k
-matchNPlusKPats all_vars@(var:vars) ty (eqn1:eqns)
+-- All NPlusKPats, for the *same* literal k
+matchNPlusKPats (var:vars) ty (eqn1:eqns)
= do { let NPlusKPat (L _ n1) lit ge minus = firstPat eqn1
; ge_expr <- dsExpr ge
; minus_expr <- dsExpr minus
shift n1 eqn@(EqnInfo { eqn_pats = NPlusKPat (L _ n) _ _ _ : pats })
= (wrapBind n n1, eqn { eqn_pats = pats })
-- The wrapBind is a no-op for the first equation
+ shift _ e = pprPanic "matchNPlusKPats/shift" (ppr e)
+
+matchNPlusKPats vars _ eqns = pprPanic "matchNPlusKPats" (ppr (vars, eqns))
\end{code}