[project @ 2000-02-10 18:39:51 by lewie]
[ghc-hetmet.git] / ghc / compiler / deSugar / MatchLit.lhs
index 15c5519..af80397 100644 (file)
@@ -1,36 +1,34 @@
 %
-% (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
 
-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 HsSyn           ( HsLit(..), OutPat(..), HsExpr(..) )
+import TcHsSyn         ( TypecheckedHsExpr, TypecheckedPat )
+import CoreSyn         ( Expr(..), Bind(..) )
+import Id              ( Id )
 
 import DsMonad
 import DsUtils
 
-import Literal         ( mkMachInt, Literal(..) )
+import Const           ( mkMachInt, Literal(..) )
+import PrimRep          ( PrimRep(IntRep) )
 import Maybes          ( catMaybes )
-import Type            ( isPrimType )
-import Util            ( panic, assertPanic )
+import Type            ( Type, isUnLiftedType )
+import Panic           ( panic, assertPanic )
 \end{code}
 
 \begin{code}
 matchLiterals :: [Id]
              -> [EquationInfo]
-             -> [EquationInfo]         -- Shadows
              -> DsM MatchResult
 \end{code}
 
@@ -42,28 +40,26 @@ 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 lit_ty : 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 ((LitPat literal lit_ty):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 Nothing literal 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)
            : rest_of_alts
@@ -76,29 +72,29 @@ matchLiterals all_vars@(var:vars) eqns_info@(EqnInfo (LitPat literal lit_ty : ps
        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 (HsLitLit      s) = ASSERT(isUnLiftedType ty)
+                                          MachLitLit s ty
        mk_core_lit ty 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 ((NPat 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 Nothing literal 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 +107,40 @@ We generate:
        <try-next-pattern-or-whatever>
 \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 ((NPlusKPat master_n k ty ge sub):ps1) _ : eqns)
+  = let
+       (shifted_eqns_for_this_lit, eqns_not_for_this_lit)
+         = partitionEqnsByLit (Just master_n) k 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 :: Maybe Id         -- (Just v) for N-plus-K patterns, where v
+                               -- is the "master" variable;
+                               -- Nothing for NPats and LitPats
+                  -> HsLit
                   -> [EquationInfo]
                   -> ([EquationInfo],  -- These ones are for this lit, AND
                                        -- they've been "shifted" by stripping
@@ -125,27 +149,38 @@ partitionEqnsByLit :: HsLit
                                        -- are exactly as fed in.
                      )
 
-partitionEqnsByLit lit eqns
+partitionEqnsByLit nPlusK lit eqns
   = ( \ (xs,ys) -> (catMaybes xs, catMaybes ys))
-       (unzip (map (partition_eqn lit) eqns))
+       (unzip (map (partition_eqn nPlusK lit) eqns))
   where
-    partition_eqn :: HsLit -> EquationInfo ->
+    partition_eqn :: Maybe Id -> HsLit -> EquationInfo ->
                (Maybe EquationInfo, Maybe 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 Nothing lit (EqnInfo n ctx (LitPat k _ : remaining_pats) match_result)
+      | lit `eq_lit` k  = (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 Nothing lit (EqnInfo n ctx (NPat k _ _ : remaining_pats) match_result)
+      | lit `eq_lit` k  = (Just (EqnInfo n ctx remaining_pats match_result), Nothing)
+                         -- NB the pattern is stripped off the EquationInfo
+
+    partition_eqn (Just master_n) lit
+        (EqnInfo n ctx (NPlusKPat n' k _ _ _ : remaining_pats) match_result)
+      | lit `eq_lit` k  = (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 nPlusK lit
+                  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)
+    partition_eqn nPlusK lit eqn = (Nothing, Just eqn)
 
 -- ToDo: meditate about this equality business...