[project @ 2003-11-03 10:22:21 by simonmar]
[ghc-hetmet.git] / ghc / compiler / hsSyn / HsBinds.lhs
index eb836a3..34ebac6 100644 (file)
@@ -15,21 +15,16 @@ import {-# SOURCE #-} HsExpr ( HsExpr, pprExpr,
                               GRHSs,  pprPatBind )
 
 -- friends:
-import HsImpExp                ( pprHsVar )
 import HsPat           ( Pat )
 import HsTypes         ( HsType )
-import CoreSyn         ( CoreExpr )
-import PprCore         ( {- instance Outputable (Expr a) -} )
 
 --others:
 import Name            ( Name )
-import PrelNames       ( isUnboundName )
 import NameSet         ( NameSet, elemNameSet, nameSetToList )
-import BasicTypes      ( RecFlag(..), FixitySig(..), Activation(..) )
+import BasicTypes      ( RecFlag(..), Activation(..), Fixity, IPName )
 import Outputable      
 import SrcLoc          ( SrcLoc )
 import Var             ( TyVar )
-import Class            ( DefMeth (..) )
 \end{code}
 
 %************************************************************************
@@ -49,13 +44,16 @@ Collections of bindings, created by dependency analysis and translation:
 \begin{code}
 data HsBinds id                -- binders and bindees
   = EmptyBinds
+  | ThenBinds  (HsBinds id) (HsBinds id)
 
-  | ThenBinds  (HsBinds id)
-               (HsBinds id)
+  | MonoBind                   -- A mutually recursive group
+       (MonoBinds id)
+       [Sig id]                -- Empty on typechecker output, Type Signatures
+       RecFlag
 
-  | MonoBind   (MonoBinds id)
-               [Sig id]                -- Empty on typechecker output, Type Signatures
-               RecFlag
+  | IPBinds                    -- Implcit parameters
+                               -- Not allowed at top level
+       [(IPName id, HsExpr id)]
 \end{code}
 
 \begin{code}
@@ -64,10 +62,11 @@ nullBinds :: HsBinds id -> Bool
 nullBinds EmptyBinds           = True
 nullBinds (ThenBinds b1 b2)    = nullBinds b1 && nullBinds b2
 nullBinds (MonoBind b _ _)     = nullMonoBinds b
+nullBinds (IPBinds b)          = null b
 
-mkMonoBind :: MonoBinds id -> [Sig id] -> RecFlag -> HsBinds id
-mkMonoBind EmptyMonoBinds _ _ = EmptyBinds
-mkMonoBind mbinds sigs is_rec = MonoBind mbinds sigs is_rec
+mkMonoBind :: RecFlag -> MonoBinds id -> HsBinds id
+mkMonoBind _      EmptyMonoBinds  = EmptyBinds
+mkMonoBind is_rec mbinds         = MonoBind mbinds [] is_rec
 \end{code}
 
 \begin{code}
@@ -77,6 +76,12 @@ instance (OutputableBndr id) => Outputable (HsBinds id) where
 ppr_binds EmptyBinds = empty
 ppr_binds (ThenBinds binds1 binds2)
     = ppr_binds binds1 $$ ppr_binds binds2
+
+ppr_binds (IPBinds binds)
+  = sep (punctuate semi (map pp_item binds))
+  where
+    pp_item (id,rhs) = pprBndr LetBind id <+> equals <+> pprExpr rhs
+
 ppr_binds (MonoBind bind sigs is_rec)
      = vcat [ppr_isrec,
             vcat (map ppr sigs),
@@ -125,9 +130,6 @@ data MonoBinds id
   | VarMonoBind            id                  -- TRANSLATION
                    (HsExpr id)
 
-  | CoreMonoBind    id                 -- TRANSLATION
-                   CoreExpr            -- No zonking; this is a final CoreExpr with Ids and Types!
-
   | AbsBinds                           -- Binds abstraction; TRANSLATION
                [TyVar]                 -- Type variables
                [id]                    -- Dicts
@@ -212,9 +214,6 @@ ppr_monobind (FunMonoBind fun inf matches locn) = pprFunBind fun matches
 ppr_monobind (VarMonoBind name expr)
       = sep [pprBndr LetBind name <+> equals, nest 4 (pprExpr expr)]
 
-ppr_monobind (CoreMonoBind name expr)
-      = sep [pprBndr LetBind name <+> equals, nest 4 (ppr expr)]
-
 ppr_monobind (AbsBinds tyvars dictvars exports inlines val_binds)
      = sep [ptext SLIT("AbsBinds"),
            brackets (interpp'SP tyvars),
@@ -245,12 +244,6 @@ data Sig name
                (HsType name)
                SrcLoc
 
-  | ClassOpSig name            -- Selector name
-                (DefMeth name) -- Default-method info
-                               -- See "THE NAMING STORY" in HsDecls
-               (HsType name)
-               SrcLoc
-
   | SpecSig    name            -- specialise a function or datatype ...
                (HsType name)   -- ... to these types
                SrcLoc
@@ -265,16 +258,17 @@ data Sig name
                SrcLoc
 
   | FixSig     (FixitySig name)        -- Fixity declaration
+
+data FixitySig name = FixitySig name Fixity SrcLoc 
 \end{code}
 
 \begin{code}
 okBindSig :: NameSet -> Sig Name -> Bool
-okBindSig ns (ClassOpSig _ _ _ _) = False
 okBindSig ns sig                 = sigForThisGroup ns sig
 
-okClsDclSig :: NameSet -> Sig Name -> Bool
-okClsDclSig ns (Sig _ _ _) = False
-okClsDclSig ns sig        = sigForThisGroup ns sig
+okClsDclSig :: Sig Name -> Bool
+okClsDclSig (SpecInstSig _ _) = False
+okClsDclSig sig              = True    -- All others OK
 
 okInstDclSig :: NameSet -> Sig Name -> Bool
 okInstDclSig ns (Sig _ _ _)      = False
@@ -282,39 +276,38 @@ okInstDclSig ns (FixSig _)          = False
 okInstDclSig ns (SpecInstSig _ _) = True
 okInstDclSig ns sig              = sigForThisGroup ns sig
 
+sigForThisGroup :: NameSet -> Sig Name -> Bool
 sigForThisGroup ns sig 
   = case sigName sig of
-       Nothing                  -> False
-       Just n | isUnboundName n -> True        -- Don't complain about an unbound name again
-              | otherwise       -> n `elemNameSet` ns
+       Nothing -> False
+       Just n  -> n `elemNameSet` ns
 
 sigName :: Sig name -> Maybe name
 sigName (Sig         n _ _)        = Just n
-sigName (ClassOpSig  n _ _ _)      = Just n
 sigName (SpecSig     n _ _)        = Just n
 sigName (InlineSig _ n _ _)        = Just n
 sigName (FixSig (FixitySig n _ _)) = Just n
 sigName other                     = Nothing
 
+sigLoc :: Sig name -> SrcLoc
+sigLoc (Sig         _ _ loc)        = loc
+sigLoc (SpecSig     _ _ loc)        = loc
+sigLoc (InlineSig _ _ _ loc)        = loc
+sigLoc (FixSig (FixitySig n _ loc)) = loc
+sigLoc (SpecInstSig _ loc)         = loc
+
 isFixitySig :: Sig name -> Bool
 isFixitySig (FixSig _) = True
 isFixitySig _         = False
 
-isClassOpSig :: Sig name -> Bool
-isClassOpSig (ClassOpSig _ _ _ _) = True
-isClassOpSig _                   = False
-
 isPragSig :: Sig name -> Bool
        -- Identifies pragmas 
 isPragSig (SpecSig _ _ _)     = True
 isPragSig (InlineSig _ _ _ _) = True
 isPragSig (SpecInstSig _ _)   = True
 isPragSig other                      = False
-\end{code}
 
-\begin{code}
 hsSigDoc (Sig        _ _ loc)        = (ptext SLIT("type signature"),loc)
-hsSigDoc (ClassOpSig _ _ _ loc)       = (ptext SLIT("class-method type signature"), loc)
 hsSigDoc (SpecSig    _ _ loc)        = (ptext SLIT("SPECIALISE pragma"),loc)
 hsSigDoc (InlineSig True  _ _ loc)    = (ptext SLIT("INLINE pragma"),loc)
 hsSigDoc (InlineSig False _ _ loc)    = (ptext SLIT("NOINLINE pragma"),loc)
@@ -322,6 +315,19 @@ hsSigDoc (SpecInstSig _ loc)             = (ptext SLIT("SPECIALISE instance pragma"),l
 hsSigDoc (FixSig (FixitySig _ _ loc)) = (ptext SLIT("fixity declaration"), loc)
 \end{code}
 
+Signature equality is used when checking for duplicate signatures
+
+\begin{code}
+eqHsSig :: Sig Name -> Sig Name -> Bool
+eqHsSig (FixSig (FixitySig n1 _ _)) (FixSig (FixitySig n2 _ _)) = n1 == n2
+eqHsSig (Sig n1 _ _)               (Sig n2 _ _)                = n1 == n2
+eqHsSig (InlineSig b1 n1 _ _)      (InlineSig b2 n2 _ _)       = b1 == b2 && n1 == n2
+       -- For specialisations, we don't have equality over
+       -- HsType, so it's not convenient to spot duplicate 
+       -- specialisations here.  Check for this later, when we're in Type land
+eqHsSig _other1 _other2 = False
+\end{code}
+
 \begin{code}
 instance (Outputable name) => Outputable (Sig name) where
     ppr sig = ppr_sig sig
@@ -330,20 +336,6 @@ ppr_sig :: Outputable name => Sig name -> SDoc
 ppr_sig (Sig var ty _)
       = sep [ppr var <+> dcolon, nest 4 (ppr ty)]
 
-ppr_sig (ClassOpSig var dm ty _)
-      = sep [ pprHsVar var <+> dcolon, 
-             nest 4 (ppr ty),
-             nest 4 (pp_dm_comment) ]
-      where
-       pp_dm = case dm of 
-                 DefMeth _  -> equals  -- Default method indicator
-                 GenDefMeth -> semi    -- Generic method indicator
-                 NoDefMeth  -> empty   -- No Method at all
-       pp_dm_comment = case dm of 
-                 DefMeth _  -> text "{- has default method -}"
-                 GenDefMeth -> text "{- has generic method -}"
-                 NoDefMeth  -> empty   -- No Method at all
-
 ppr_sig (SpecSig var ty _)
       = sep [ hsep [text "{-# SPECIALIZE", ppr var, dcolon],
              nest 4 (ppr ty <+> text "#-}")
@@ -359,21 +351,7 @@ ppr_sig (SpecInstSig ty _)
       = hsep [text "{-# SPECIALIZE instance", ppr ty, text "#-}"]
 
 ppr_sig (FixSig fix_sig) = ppr fix_sig
-\end{code}
 
-Checking for distinct signatures; oh, so boring
-
-
-\begin{code}
-eqHsSig :: Sig Name -> Sig Name -> Bool
-eqHsSig (Sig n1 _ _)         (Sig n2 _ _)          = n1 == n2
-eqHsSig (InlineSig b1 n1 _ _)(InlineSig b2 n2 _ _) = b1 == b2 && n1 == n2
-
-eqHsSig (SpecInstSig ty1 _)  (SpecInstSig ty2 _)  = ty1 == ty2
-eqHsSig (SpecSig n1 ty1 _)   (SpecSig n2 ty2 _)   =
-    -- may have many specialisations for one value;
-    -- but not ones that are exactly the same...
-    (n1 == n2) && (ty1 == ty2)
-
-eqHsSig _other1 _other2 = False
+instance Outputable name => Outputable (FixitySig name) where
+  ppr (FixitySig name fixity loc) = sep [ppr fixity, ppr name]
 \end{code}