[project @ 2000-04-14 08:58:00 by simonmar]
[ghc-hetmet.git] / ghc / compiler / deSugar / MatchLit.lhs
index c9b6bb1..f3e10ff 100644 (file)
@@ -1,5 +1,5 @@
 %
-% (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
+% (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
 %
 \section[MatchLit]{Pattern-matching literal patterns}
 
@@ -11,19 +11,19 @@ module MatchLit ( matchLiterals ) where
 import {-# SOURCE #-} Match  ( match )
 import {-# SOURCE #-} DsExpr ( dsExpr )
 
-import HsSyn           ( HsLit(..), OutPat(..), HsExpr(..), Fixity,
-                         Match, HsBinds, DoOrListComp, HsType, ArithSeqInfo )
+import HsSyn           ( HsLit(..), OutPat(..), HsExpr(..) )
 import TcHsSyn         ( TypecheckedHsExpr, TypecheckedPat )
-import CoreSyn         ( CoreExpr, CoreBinding, GenCoreExpr(..), GenCoreBinding(..) )
-import Id              ( GenId {- instance Eq -}, Id )
+import CoreSyn         ( Expr(..), Bind(..) )
+import Id              ( Id )
 
 import DsMonad
 import DsUtils
 
 import Literal         ( mkMachInt, Literal(..) )
+import PrimRep          ( PrimRep(IntRep) )
 import Maybes          ( catMaybes )
-import Type            ( Type )
-import Util            ( panic, assertPanic )
+import Type            ( Type, isUnLiftedType )
+import Panic           ( panic, assertPanic )
 \end{code}
 
 \begin{code}
@@ -45,7 +45,7 @@ matchLiterals all_vars@(var:vars) eqns_info@(EqnInfo n ctx (LitPat literal lit_t
     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 []
 
@@ -72,27 +72,29 @@ matchLiterals all_vars@(var:vars) eqns_info@(EqnInfo n ctx (LitPat literal lit_t
        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(isUnpointedType 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 n ctx ((NPat literal lit_ty eq_chk):ps1) _ : eqns)
+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 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 ->
-    mkGuardedMatchResult pred_expr inner_match_result    `thenDs` \ match_result1 ->
-
+    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
     if (null eqns_not_for_this_lit)
     then
        returnDs match_result1
     else
         matchLiterals all_vars eqns_not_for_this_lit     `thenDs` \ match_result2 ->
-       combineMatchResults match_result1 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.
@@ -117,22 +119,22 @@ matchLiterals all_vars@(var:vars) eqns_info@(EqnInfo n ctx ((NPlusKPat master_n
     dsExpr (HsApp ge (HsVar var))              `thenDs` \ ge_expr ->
     dsExpr (HsApp sub (HsVar var))             `thenDs` \ nminusk_expr ->
 
-    mkGuardedMatchResult
-       ge_expr
-       (mkCoLetsMatchResult [NonRec master_n nminusk_expr] inner_match_result)
-                                       `thenDs` \ match_result1 ->
-
+    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 ->
-       combineMatchResults match_result1 match_result2
+       returnDs (combineMatchResults match_result1 match_result2)
 \end{code}
 
-Given a blob of LitPats/NPats, we want to split them into those
+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 :: Maybe Id         -- (Just v) for N-plus-K patterns, where v
@@ -162,15 +164,19 @@ partitionEqnsByLit nPlusK lit eqns
       | 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)
+    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
+                        | 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 nPlusK lit eqn@(EqnInfo n ctx (WildPat _ : remaining_pats) match_result)
+       -- 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