X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=ghc%2Fcompiler%2FdeSugar%2FMatchLit.lhs;h=287d730f7d5283178f1e50f850600aa87f3a6d38;hb=f84bb9a2e577c9c1be83a806a4cf70fccfb45b54;hp=15c5519dbc43ee270d8b820065ef9b7f1eda7d84;hpb=26741ec416bae2c502ef00a2ba0e79050a32cb67;p=ghc-hetmet.git diff --git a/ghc/compiler/deSugar/MatchLit.lhs b/ghc/compiler/deSugar/MatchLit.lhs index 15c5519..287d730 100644 --- a/ghc/compiler/deSugar/MatchLit.lhs +++ b/ghc/compiler/deSugar/MatchLit.lhs @@ -1,36 +1,96 @@ % -% (c) The GRASP/AQUA Project, Glasgow University, 1992-1996 +% (c) The GRASP/AQUA Project, Glasgow University, 1992-1998 % \section[MatchLit]{Pattern-matching literal patterns} \begin{code} -#include "HsVersions.h" - -module MatchLit ( matchLiterals ) where +module MatchLit ( dsLit, matchLiterals ) where -IMP_Ubiq() -IMPORT_DELOOPER(DsLoop) -- break match-ish and dsExpr-ish loops +#include "HsVersions.h" -import HsSyn ( HsLit(..), OutPat(..), HsExpr(..), - Match, HsBinds, Stmt, Qualifier, PolyType, ArithSeqInfo ) -import TcHsSyn ( TypecheckedHsExpr(..), TypecheckedHsBinds(..), - TypecheckedPat(..) - ) -import CoreSyn ( SYN_IE(CoreExpr), SYN_IE(CoreBinding) ) +import {-# SOURCE #-} Match ( match ) +import {-# SOURCE #-} DsExpr ( dsExpr ) import DsMonad +import DsCCall ( resultWrapper ) import DsUtils +import HsSyn ( HsLit(..), Pat(..), HsExpr(..) ) +import TcHsSyn ( TypecheckedPat ) +import Id ( Id ) +import CoreSyn +import TyCon ( tyConDataCons ) +import TcType ( tcSplitTyConApp, isIntegerTy ) + +import PrelNames ( ratioTyConKey ) +import Unique ( hasKey ) import Literal ( mkMachInt, Literal(..) ) import Maybes ( catMaybes ) -import Type ( isPrimType ) -import Util ( panic, assertPanic ) +import Type ( isUnLiftedType ) +import Panic ( panic, assertPanic ) +import Maybe ( isJust ) +import Ratio ( numerator, denominator ) \end{code} +%************************************************************************ +%* * + Desugaring literals + [used to be in DsExpr, but DsMeta needs it, + and it's nice to avoid a loop] +%* * +%************************************************************************ + +We give int/float literals type @Integer@ and @Rational@, respectively. +The typechecker will (presumably) have put \tr{from{Integer,Rational}s} +around them. + +ToDo: put in range checks for when converting ``@i@'' +(or should that be in the typechecker?) + +For numeric literals, we try to detect there use at a standard type +(@Int@, @Float@, etc.) are directly put in the right constructor. +[NB: down with the @App@ conversion.] + +See also below where we look for @DictApps@ for \tr{plusInt}, etc. + +\begin{code} +dsLit :: HsLit -> DsM CoreExpr +dsLit (HsChar c) = returnDs (mkCharExpr c) +dsLit (HsCharPrim c) = returnDs (mkLit (MachChar c)) +dsLit (HsString str) = mkStringLitFS str +dsLit (HsStringPrim s) = returnDs (mkLit (MachStr s)) +dsLit (HsInteger i) = mkIntegerExpr i +dsLit (HsInt i) = returnDs (mkIntExpr i) +dsLit (HsIntPrim i) = returnDs (mkIntLit i) +dsLit (HsFloatPrim f) = returnDs (mkLit (MachFloat f)) +dsLit (HsDoublePrim d) = returnDs (mkLit (MachDouble d)) +dsLit (HsLitLit str ty) + = ASSERT( isJust maybe_ty ) + returnDs (wrap_fn (mkLit (MachLitLit str rep_ty))) + where + (maybe_ty, wrap_fn) = resultWrapper ty + Just rep_ty = maybe_ty + +dsLit (HsRat r ty) + = mkIntegerExpr (numerator r) `thenDs` \ num -> + mkIntegerExpr (denominator r) `thenDs` \ denom -> + returnDs (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) +\end{code} + +%************************************************************************ +%* * + Pattern matching on literals +%* * +%************************************************************************ + \begin{code} matchLiterals :: [Id] -> [EquationInfo] - -> [EquationInfo] -- Shadows -> DsM MatchResult \end{code} @@ -42,63 +102,61 @@ is much like @matchConFamily@, which uses @match_cons_used@ to create the alts---here we use @match_prims_used@. \begin{code} -matchLiterals all_vars@(var:vars) eqns_info@(EqnInfo (LitPat literal lit_ty : ps1) _ : eqns) shadows +matchLiterals all_vars@(var:vars) eqns_info@(EqnInfo n ctx (LitPat literal : ps1) _ : eqns) = -- GENERATE THE ALTS - match_prims_used vars eqns_info shadows `thenDs` \ prim_alts -> + match_prims_used vars eqns_info `thenDs` \ prim_alts -> -- MAKE THE PRIMITIVE CASE - mkCoPrimCaseMatchResult var prim_alts + returnDs (mkCoPrimCaseMatchResult var prim_alts) where - match_prims_used _ [{-no more eqns-}] _ = returnDs [] + match_prims_used _ [{-no more eqns-}] = returnDs [] - match_prims_used vars eqns_info@(EqnInfo ((LitPat literal lit_ty):ps1) _ : eqns) shadows + match_prims_used vars eqns_info@(EqnInfo n ctx (pat@(LitPat literal):ps1) _ : eqns) = let (shifted_eqns_for_this_lit, eqns_not_for_this_lit) - = partitionEqnsByLit literal eqns_info - (shifted_shadows_for_this_lit, shadows_not_for_this_lit) - = partitionEqnsByLit literal shadows + = partitionEqnsByLit pat eqns_info in -- recursive call to make other alts... - match_prims_used vars eqns_not_for_this_lit shadows_not_for_this_lit `thenDs` \ rest_of_alts -> + match_prims_used vars eqns_not_for_this_lit `thenDs` \ rest_of_alts -> -- (prim pats have no args; no selectMatchVars as in match_cons_used) -- now do the business to make the alt for _this_ LitPat ... - match vars shifted_eqns_for_this_lit shifted_shadows_for_this_lit `thenDs` \ match_result -> + match vars shifted_eqns_for_this_lit `thenDs` \ match_result -> returnDs ( - (mk_core_lit lit_ty literal, match_result) + (mk_core_lit literal, match_result) : rest_of_alts ) where - mk_core_lit :: Type -> HsLit -> Literal - - mk_core_lit ty (HsIntPrim i) = mkMachInt i - mk_core_lit ty (HsCharPrim c) = MachChar c - mk_core_lit ty (HsStringPrim s) = MachStr s - mk_core_lit ty (HsFloatPrim f) = MachFloat f - mk_core_lit ty (HsDoublePrim d) = MachDouble d - mk_core_lit ty (HsLitLit s) = ASSERT(isPrimType ty) - MachLitLit s (panic "MatchLit.matchLiterals:mk_core_lit:HsLitLit; typePrimRep???") - mk_core_lit ty other = panic "matchLiterals:mk_core_lit:unhandled" + mk_core_lit :: HsLit -> Literal + + mk_core_lit (HsIntPrim i) = mkMachInt i + mk_core_lit (HsCharPrim c) = MachChar c + mk_core_lit (HsStringPrim s) = MachStr s + mk_core_lit (HsFloatPrim f) = MachFloat f + mk_core_lit (HsDoublePrim d) = MachDouble d + mk_core_lit (HsLitLit s ty) = ASSERT(isUnLiftedType ty) + MachLitLit s ty + mk_core_lit other = panic "matchLiterals:mk_core_lit:unhandled" \end{code} \begin{code} -matchLiterals all_vars@(var:vars) eqns_info@(EqnInfo ((NPat literal lit_ty eq_chk):ps1) _ : eqns) shadows +matchLiterals all_vars@(var:vars) + eqns_info@(EqnInfo n ctx (pat@(NPatOut literal lit_ty eq_chk):ps1) _ : eqns) = let (shifted_eqns_for_this_lit, eqns_not_for_this_lit) - = partitionEqnsByLit literal eqns_info - (shifted_shadows_for_this_lit, shadows_not_for_this_lit) - = partitionEqnsByLit literal shadows + = partitionEqnsByLit pat eqns_info + in + dsExpr (HsApp eq_chk (HsVar var)) `thenDs` \ pred_expr -> + match vars shifted_eqns_for_this_lit `thenDs` \ inner_match_result -> + let + match_result1 = mkGuardedMatchResult pred_expr inner_match_result in - dsExpr (HsApp eq_chk (HsVar var)) `thenDs` \ pred_expr -> - match vars shifted_eqns_for_this_lit shifted_shadows_for_this_lit `thenDs` \ inner_match_result -> - mkGuardedMatchResult pred_expr inner_match_result `thenDs` \ match_result1 -> - if (null eqns_not_for_this_lit) then returnDs match_result1 else - matchLiterals all_vars eqns_not_for_this_lit shadows_not_for_this_lit `thenDs` \ match_result2 -> - combineMatchResults match_result1 match_result2 + matchLiterals all_vars eqns_not_for_this_lit `thenDs` \ match_result2 -> + returnDs (combineMatchResults match_result1 match_result2) \end{code} For an n+k pattern, we use the various magic expressions we've been given. @@ -111,12 +169,37 @@ We generate: \end{verbatim} -Given a blob of LitPats/NPats, we want to split them into those + +\begin{code} +matchLiterals all_vars@(var:vars) eqns_info@(EqnInfo n ctx (pat@(NPlusKPatOut master_n k ge sub):ps1) _ : eqns) + = let + (shifted_eqns_for_this_lit, eqns_not_for_this_lit) + = partitionEqnsByLit pat eqns_info + in + match vars shifted_eqns_for_this_lit `thenDs` \ inner_match_result -> + + dsExpr (HsApp ge (HsVar var)) `thenDs` \ ge_expr -> + dsExpr (HsApp sub (HsVar var)) `thenDs` \ nminusk_expr -> + + let + match_result1 = mkGuardedMatchResult ge_expr $ + mkCoLetsMatchResult [NonRec master_n nminusk_expr] $ + inner_match_result + in + if (null eqns_not_for_this_lit) + then + returnDs match_result1 + else + matchLiterals all_vars eqns_not_for_this_lit `thenDs` \ match_result2 -> + returnDs (combineMatchResults match_result1 match_result2) +\end{code} + +Given a blob of @LitPat@s/@NPat@s, we want to split them into those that are ``same''/different as one we are looking at. We need to know -whether we're looking at a LitPat/NPat, and what literal we're after. +whether we're looking at a @LitPat@/@NPat@, and what literal we're after. \begin{code} -partitionEqnsByLit :: HsLit +partitionEqnsByLit :: TypecheckedPat -> [EquationInfo] -> ([EquationInfo], -- These ones are for this lit, AND -- they've been "shifted" by stripping @@ -125,40 +208,35 @@ partitionEqnsByLit :: HsLit -- are exactly as fed in. ) -partitionEqnsByLit lit eqns +partitionEqnsByLit master_pat eqns = ( \ (xs,ys) -> (catMaybes xs, catMaybes ys)) - (unzip (map (partition_eqn lit) eqns)) + (unzip (map (partition_eqn master_pat) eqns)) where - partition_eqn :: HsLit -> EquationInfo -> - (Maybe EquationInfo, Maybe EquationInfo) + partition_eqn :: TypecheckedPat -> EquationInfo -> (Maybe EquationInfo, Maybe EquationInfo) + + partition_eqn (LitPat k1) (EqnInfo n ctx (LitPat k2 : remaining_pats) match_result) + | k1 == k2 = (Just (EqnInfo n ctx remaining_pats match_result), Nothing) + -- NB the pattern is stripped off the EquationInfo - partition_eqn lit (EqnInfo (LitPat k _ : remaining_pats) match_result) - | lit `eq_lit` k = (Just (EqnInfo remaining_pats match_result), Nothing) - -- NB the pattern is stripped off thhe EquationInfo + partition_eqn (NPatOut k1 _ _) (EqnInfo n ctx (NPatOut k2 _ _ : remaining_pats) match_result) + | k1 == k2 = (Just (EqnInfo n ctx remaining_pats match_result), Nothing) + -- NB the pattern is stripped off the EquationInfo - partition_eqn lit (EqnInfo (NPat k _ _ : remaining_pats) match_result) - | lit `eq_lit` k = (Just (EqnInfo remaining_pats match_result), Nothing) - -- NB the pattern is stripped off thhe EquationInfo + partition_eqn (NPlusKPatOut master_n k1 _ _) + (EqnInfo n ctx (NPlusKPatOut n' k2 _ _ : remaining_pats) match_result) + | k1 == k2 = (Just (EqnInfo n ctx remaining_pats new_match_result), Nothing) + -- NB the pattern is stripped off the EquationInfo + where + new_match_result | master_n == n' = match_result + | otherwise = mkCoLetsMatchResult + [NonRec n' (Var master_n)] match_result - -- Wild-card patterns, which will only show up in the shadows, go into both groups - partition_eqn lit eqn@(EqnInfo (WildPat _ : remaining_pats) match_result) - = (Just (EqnInfo remaining_pats match_result), Just eqn) + -- Wild-card patterns, which will only show up in the shadows, + -- go into both groups + partition_eqn master_pat eqn@(EqnInfo n ctx (WildPat _ : remaining_pats) match_result) + = (Just (EqnInfo n ctx remaining_pats match_result), Just eqn) -- Default case; not for this pattern - partition_eqn lit eqn = (Nothing, Just eqn) - --- ToDo: meditate about this equality business... - -eq_lit (HsInt i1) (HsInt i2) = i1 == i2 -eq_lit (HsFrac f1) (HsFrac f2) = f1 == f2 - -eq_lit (HsIntPrim i1) (HsIntPrim i2) = i1 == i2 -eq_lit (HsFloatPrim f1) (HsFloatPrim f2) = f1 == f2 -eq_lit (HsDoublePrim d1) (HsDoublePrim d2) = d1 == d2 -eq_lit (HsChar c1) (HsChar c2) = c1 == c2 -eq_lit (HsCharPrim c1) (HsCharPrim c2) = c1 == c2 -eq_lit (HsString s1) (HsString s2) = s1 == s2 -eq_lit (HsStringPrim s1) (HsStringPrim s2) = s1 == s2 -eq_lit (HsLitLit s1) (HsLitLit s2) = s1 == s2 -- ToDo: ??? (dubious) -eq_lit other1 other2 = panic "matchLiterals:eq_lit" + partition_eqn master_pat eqn = (Nothing, Just eqn) \end{code} +