[project @ 2002-04-02 13:21:36 by simonpj]
[ghc-hetmet.git] / ghc / compiler / typecheck / TcGenDeriv.lhs
index 273572b..4f20887 100644 (file)
@@ -34,7 +34,7 @@ import HsSyn          ( InPat(..), HsExpr(..), MonoBinds(..),
 import RdrHsSyn                ( mkHsOpApp, RdrNameMonoBinds, RdrNameHsExpr, RdrNamePat )
 import RdrName         ( RdrName, mkUnqual )
 import BasicTypes      ( RecFlag(..), Fixity(..), FixityDirection(..)
-                       , maxPrecedence, defaultFixity
+                       , maxPrecedence
                        , Boxity(..)
                        )
 import FieldLabel       ( fieldLabelName )
@@ -48,6 +48,7 @@ import Name           ( getOccString, getOccName, getSrcLoc, occNameString,
                          isDataSymOcc, isSymOcc
                        )
 
+import HscTypes                ( FixityEnv, lookupFixity )
 import PrelInfo                -- Lots of RdrNames
 import SrcLoc          ( generatedSrcLoc, SrcLoc )
 import TyCon           ( TyCon, isNewTyCon, tyConDataCons, isEnumerationTyCon,
@@ -57,16 +58,12 @@ import TcType               ( isUnLiftedType, tcEqType, Type )
 import TysPrim         ( charPrimTy, intPrimTy, wordPrimTy, addrPrimTy,
                          floatPrimTy, doublePrimTy
                        )
-import Util            ( mapAccumL, zipEqual, zipWithEqual,
+import Util            ( mapAccumL, zipEqual, zipWithEqual, isSingleton,
                          zipWith3Equal, nOfThem )
 import Panic           ( panic, assertPanic )
-import Maybes          ( maybeToBool, orElse )
+import Maybes          ( maybeToBool )
 import Constants
 import List            ( partition, intersperse )
-
-#if __GLASGOW_HASKELL__ >= 404
-import GlaExts         ( fromInt )
-#endif
 \end{code}
 
 %************************************************************************
@@ -184,7 +181,7 @@ gen_Eq_binds tycon
            else -- calc. and compare the tags
                 [([a_Pat, b_Pat],
                    untag_Expr tycon [(a_RDR,ah_RDR), (b_RDR,bh_RDR)]
-                     (cmp_tags_Expr eqH_Int_RDR ah_RDR bh_RDR true_Expr false_Expr))]
+                              (genOpApp (HsVar ah_RDR) eqH_Int_RDR (HsVar bh_RDR)))]
     in
     mk_FunMonoBind tycon_loc eq_RDR ((map pats_etc nonnullary_cons) ++ rest)
            `AndMonoBinds`
@@ -351,7 +348,7 @@ gen_Ord_binds tycon
     cmp_eq =
        mk_FunMonoBind tycon_loc 
                       cmp_eq_RDR 
-                      (if null nonnullary_cons && (length nullary_cons == 1) then
+                      (if null nonnullary_cons && isSingleton nullary_cons then
                           -- catch this specially to avoid warnings
                           -- about overlapping patterns from the desugarer.
                          let 
@@ -363,7 +360,7 @@ gen_Ord_binds tycon
                       else
                          map pats_etc nonnullary_cons ++
                          -- leave out wildcards to silence desugarer.
-                         (if length tycon_data_cons == 1 then
+                         (if isSingleton tycon_data_cons then
                              []
                           else
                               [([WildPatIn, WildPatIn], default_rhs)]))
@@ -527,7 +524,7 @@ gen_Bounded_binds tycon
   = if isEnumerationTyCon tycon then
        min_bound_enum `AndMonoBinds` max_bound_enum
     else
-       ASSERT(length data_cons == 1)
+       ASSERT(isSingleton data_cons)
        min_bound_1con `AndMonoBinds` max_bound_1con
   where
     data_cons = tyConDataCons tycon
@@ -751,7 +748,7 @@ gen_Ix_binds tycon
 %************************************************************************
 
 \begin{code}
-gen_Read_binds :: (Name -> Maybe Fixity) -> TyCon -> RdrNameMonoBinds
+gen_Read_binds :: FixityEnv -> TyCon -> RdrNameMonoBinds
 
 gen_Read_binds get_fixity tycon
   = reads_prec `AndMonoBinds` read_list
@@ -908,7 +905,7 @@ gen_Read_binds get_fixity tycon
 %************************************************************************
 
 \begin{code}
-gen_Show_binds :: (Name -> Maybe Fixity) -> TyCon -> RdrNameMonoBinds
+gen_Show_binds :: FixityEnv -> TyCon -> RdrNameMonoBinds
 
 gen_Show_binds get_fixity tycon
   = shows_prec `AndMonoBinds` show_list
@@ -1012,7 +1009,7 @@ gen_Show_binds get_fixity tycon
 \end{code}
 
 \begin{code}
-getLRPrecs :: Bool -> (Name -> Maybe Fixity) -> Name -> [Integer]
+getLRPrecs :: Bool -> FixityEnv -> Name -> [Integer]
 getLRPrecs is_infix get_fixity nm = [lp, rp]
     where
      {-
@@ -1033,17 +1030,16 @@ getLRPrecs is_infix get_fixity nm = [lp, rp]
       | otherwise       = paren_con_prec + 1
                  
 defaultPrecedence :: Integer
-defaultPrecedence = fromInt maxPrecedence
+defaultPrecedence = fromIntegral maxPrecedence
 
-getPrecedence :: (Name -> Maybe Fixity) -> Name -> Integer
+getPrecedence :: FixityEnv -> Name -> Integer
 getPrecedence get_fixity nm 
-   = case get_fixity nm of
-        Just (Fixity x _) -> fromInt x
-        other            -> defaultPrecedence
+   = case lookupFixity get_fixity nm of
+        Fixity x _ -> fromIntegral x
 
-isLRAssoc :: (Name -> Maybe Fixity) -> Name -> (Bool, Bool)
+isLRAssoc :: FixityEnv -> Name -> (Bool, Bool)
 isLRAssoc get_fixity nm =
-     case get_fixity nm `orElse` defaultFixity of
+     case lookupFixity get_fixity nm of
        Fixity _ InfixN -> (False, False)
        Fixity _ InfixR -> (False, True)
        Fixity _ InfixL -> (True,  False)
@@ -1155,7 +1151,7 @@ mk_FunMonoBind loc fun pats_and_exprs
                loc
 
 mk_match loc pats expr binds
-  = Match [] (map paren pats) Nothing 
+  = Match (map paren pats) Nothing 
          (GRHSs (unguardedRHS expr loc) binds placeHolderType)
   where
     paren p@(VarPatIn _) = p
@@ -1191,10 +1187,10 @@ compare_gen_Case fun lt eq gt a b
       generatedSrcLoc
 
 careful_compare_Case ty lt eq gt a b
-  = if not (isUnLiftedType ty) then
+  | not (isUnLiftedType ty) =
        compare_gen_Case compare_RDR lt eq gt a b
-
-    else -- we have to do something special for primitive things...
+  | otherwise               =
+         -- we have to do something special for primitive things...
        HsIf (genOpApp a relevant_eq_op b)
            eq
            (HsIf (genOpApp a relevant_lt_op b) lt gt generatedSrcLoc)
@@ -1237,13 +1233,14 @@ append_Expr a b = genOpApp a append_RDR b
 -----------------------------------------------------------------------
 
 eq_Expr :: Type -> RdrNameHsExpr -> RdrNameHsExpr -> RdrNameHsExpr
-eq_Expr ty a b
-  = if not (isUnLiftedType ty) then
-       genOpApp a eq_RDR  b
-    else -- we have to do something special for primitive things...
-       genOpApp a relevant_eq_op b
-  where
-    relevant_eq_op = assoc_ty_id eq_op_tbl ty
+eq_Expr ty a b = genOpApp a eq_op b
+ where
+   eq_op
+    | not (isUnLiftedType ty) = eq_RDR
+    | otherwise               =
+         -- we have to do something special for primitive things...
+       assoc_ty_id eq_op_tbl ty
+
 \end{code}
 
 \begin{code}
@@ -1329,17 +1326,17 @@ genOpApp e1 op e2 = mkHsOpApp e1 op e2
 qual_orig_name n = nameRdrName (getName n)
 varUnqual n      = mkUnqual varName n
 
-zz_a_RDR       = varUnqual SLIT("_a")
-a_RDR          = varUnqual SLIT("a")
-b_RDR          = varUnqual SLIT("b")
-c_RDR          = varUnqual SLIT("c")
-d_RDR          = varUnqual SLIT("d")
-ah_RDR         = varUnqual SLIT("a#")
-bh_RDR         = varUnqual SLIT("b#")
-ch_RDR         = varUnqual SLIT("c#")
-dh_RDR         = varUnqual SLIT("d#")
-cmp_eq_RDR     = varUnqual SLIT("cmp_eq")
-rangeSize_RDR  = varUnqual SLIT("rangeSize")
+zz_a_RDR       = varUnqual FSLIT("_a")
+a_RDR          = varUnqual FSLIT("a")
+b_RDR          = varUnqual FSLIT("b")
+c_RDR          = varUnqual FSLIT("c")
+d_RDR          = varUnqual FSLIT("d")
+ah_RDR         = varUnqual FSLIT("a#")
+bh_RDR         = varUnqual FSLIT("b#")
+ch_RDR         = varUnqual FSLIT("c#")
+dh_RDR         = varUnqual FSLIT("d#")
+cmp_eq_RDR     = varUnqual FSLIT("cmp_eq")
+rangeSize_RDR  = varUnqual FSLIT("rangeSize")
 
 as_RDRs                = [ varUnqual (_PK_ ("a"++show i)) | i <- [(1::Int) .. ] ]
 bs_RDRs                = [ varUnqual (_PK_ ("b"++show i)) | i <- [(1::Int) .. ] ]