[project @ 2000-09-07 09:10:07 by simonpj]
authorsimonpj <unknown>
Thu, 7 Sep 2000 09:10:07 +0000 (09:10 +0000)
committersimonpj <unknown>
Thu, 7 Sep 2000 09:10:07 +0000 (09:10 +0000)
* Make the desugarer use string equality for string literal
  patterns longer than 1 character.  And put a specialised
  eqString into PrelBase, with a suitable specialisation rule.
  This makes a huge difference to the size of the code generated
  by deriving(Read) notably in Time.lhs

ghc/compiler/deSugar/DsUtils.lhs
ghc/lib/std/PrelBase.lhs

index bf63c5f..2221c26 100644 (file)
@@ -92,7 +92,7 @@ tidyLitPat lit lit_ty default_pat
   | lit_ty == floatTy            = ConPat floatDataCon  lit_ty [] [] [LitPat (mk_float lit)  floatPrimTy]
   | lit_ty == doubleTy           = ConPat doubleDataCon lit_ty [] [] [LitPat (mk_double lit) doublePrimTy]
 
-               -- Convert literal patterns like "foo" to 'f':'o':'o':[]
+       -- Convert short string-literal patterns like "f" to 'f':[]
   | str_lit lit           = mk_list lit
 
   | otherwise = default_pat
@@ -116,10 +116,7 @@ tidyLitPat lit lit_ty default_pat
     mk_double (HsFrac f)     = HsDoublePrim f
     mk_double l@(HsLitLit s) = l
 
-    null_str_lit (HsString s) = _NULL_ s
-    null_str_lit other_lit    = False
-
-    str_lit (HsString s)     = True
+    str_lit (HsString s)     = _LENGTH_ s <= 1 -- Short string literals only
     str_lit _                = False
 
     mk_list (HsString s)     = foldr
index cebd110..315469d 100644 (file)
@@ -1,5 +1,5 @@
 % -----------------------------------------------------------------------------
-% $Id: PrelBase.lhs,v 1.36 2000/08/29 17:42:17 qrczak Exp $
+% $Id: PrelBase.lhs,v 1.37 2000/09/07 09:10:07 simonpj Exp $
 %
 % (c) The University of Glasgow, 1992-2000
 %
@@ -108,14 +108,14 @@ default ()                -- Double isn't available yet
 %*********************************************************
 
 \begin{code}
-{-             
+{-
 data  Bool  =  False | True
 data Ordering = LT | EQ | GT 
 data Char = C# Char#
 type  String = [Char]
 data Int = I# Int#
 data  ()  =  ()
--- data [] a = MkNil
+data [] a = MkNil
 
 not True = False
 (&&) True True = True
@@ -195,7 +195,6 @@ class  Monad m  where
 
     m >> k      =  m >>= \_ -> k
     fail s      = error s
-
 \end{code}
 
 
@@ -454,6 +453,20 @@ ord :: Char -> Int
 ord (C# c) =  I# (ord# c)
 \end{code}
 
+String equality is used when desugaring pattern-matches against strings.
+It's worth making it fast, and providing a rule to use the fast version
+where possible.
+
+\begin{code}
+eqString :: String -> String -> Bool
+eqString []            []            = True
+eqString (C# c1 : cs1) (C# c2 : cs2) = c1 `eqChar#` c2 && cs1 `eqString` cs2
+eqString _            _             = False
+
+{-# RULES
+"eqString"  (==) = eqString
+  #-}  
+\end{code}
 
 %*********************************************************
 %*                                                     *
@@ -715,5 +728,4 @@ unpackNBytes#  addr len# = unpack [] (len# -# 1#)
 --     unpackFoldr "foo" c (unpackFoldr "baz" c n)  =  unpackFoldr "foobaz" c n
 
   #-}
-
 \end{code}