[project @ 2002-11-20 15:39:47 by simonpj]
[ghc-hetmet.git] / ghc / compiler / basicTypes / BasicTypes.lhs
index 35522d3..1f74e7f 100644 (file)
@@ -20,8 +20,12 @@ module BasicTypes(
 
        Unused, unused,
 
-       Fixity(..), FixityDirection(..),
-       defaultFixity, maxPrecedence, negateFixity, negatePrecedence,
+       FixitySig(..), Fixity(..), FixityDirection(..),
+       defaultFixity, maxPrecedence, 
+       arrowFixity, negateFixity, negatePrecedence,
+       compareFixity,
+
+       IPName(..), ipNameName, mapIPName,
 
        NewOrData(..), 
 
@@ -31,7 +35,8 @@ module BasicTypes(
 
        Boxity(..), isBoxed, tupleParens,
 
-       OccInfo(..), seqOccInfo, isFragileOcc, isDeadOcc, isLoopBreaker,
+       OccInfo(..), seqOccInfo, isFragileOcc, isOneOcc, 
+       isDeadOcc, isLoopBreaker,
 
        InsideLam, insideLam, notInsideLam,
        OneBranch, oneBranch, notOneBranch,
@@ -40,13 +45,16 @@ module BasicTypes(
 
        StrictnessMark(..), isMarkedUnboxed, isMarkedStrict,
 
-       CompilerPhase, pprPhase, 
-       Activation(..), isActive, isNeverActive, isAlwaysActive
+       CompilerPhase, 
+       Activation(..), isActive, isNeverActive, isAlwaysActive,
+
+       SuccessFlag(..), succeeded, failed, successIf
    ) where
 
 #include "HsVersions.h"
 
 import Outputable
+import SrcLoc
 \end{code}
 
 %************************************************************************
@@ -100,36 +108,105 @@ initialVersion = 1
 
 %************************************************************************
 %*                                                                     *
+\subsection{Implicit parameter identity}
+%*                                                                     *
+%************************************************************************
+
+The @IPName@ type is here because it is used in TypeRep (i.e. very
+early in the hierarchy), but also in HsSyn.
+
+\begin{code}
+data IPName name
+  = Dupable   name     -- ?x: you can freely duplicate this implicit parameter
+  | Linear name                -- %x: you must use the splitting function to duplicate it
+  deriving( Eq, Ord )  -- Ord is used in the IP name cache finite map
+                       --      (used in HscTypes.OrigIParamCache)
+
+
+ipNameName :: IPName name -> name
+ipNameName (Dupable n) = n
+ipNameName (Linear  n) = n
+
+mapIPName :: (a->b) -> IPName a -> IPName b
+mapIPName f (Dupable n) = Dupable (f n)
+mapIPName f (Linear  n) = Linear  (f n)
+\end{code}
+
+               
+%************************************************************************
+%*                                                                     *
 \subsection[Fixity]{Fixity info}
 %*                                                                     *
 %************************************************************************
 
 \begin{code}
+------------------------
+data FixitySig name = FixitySig name Fixity SrcLoc 
+
+instance Eq name => Eq (FixitySig name) where
+   (FixitySig n1 f1 _) == (FixitySig n2 f2 _) = n1==n2 && f1==f2
+
+instance Outputable name => Outputable (FixitySig name) where
+  ppr (FixitySig name fixity loc) = sep [ppr fixity, ppr name]
+
+------------------------
 data Fixity = Fixity Int FixityDirection
-data FixityDirection = InfixL | InfixR | InfixN 
-                    deriving(Eq)
 
 instance Outputable Fixity where
     ppr (Fixity prec dir) = hcat [ppr dir, space, int prec]
 
+instance Eq Fixity where               -- Used to determine if two fixities conflict
+  (Fixity p1 dir1) == (Fixity p2 dir2) = p1==p2 && dir1 == dir2
+
+------------------------
+data FixityDirection = InfixL | InfixR | InfixN 
+                    deriving(Eq)
+
 instance Outputable FixityDirection where
     ppr InfixL = ptext SLIT("infixl")
     ppr InfixR = ptext SLIT("infixr")
     ppr InfixN = ptext SLIT("infix")
 
-instance Eq Fixity where               -- Used to determine if two fixities conflict
-  (Fixity p1 dir1) == (Fixity p2 dir2) = p1==p2 && dir1 == dir2
-
+------------------------
 maxPrecedence = (9::Int)
 defaultFixity = Fixity maxPrecedence InfixL
 
 negateFixity :: Fixity
 negateFixity     = Fixity negatePrecedence InfixL      -- Precedence of unary negate is wired in as infixl 6!
 
+arrowFixity :: Fixity  -- Fixity of '->' in types
+arrowFixity = Fixity 0 InfixR
+
 negatePrecedence :: Int
 negatePrecedence = 6
 \end{code}
 
+Consider
+
+\begin{verbatim}
+       a `op1` b `op2` c
+\end{verbatim}
+@(compareFixity op1 op2)@ tells which way to arrange appication, or
+whether there's an error.
+
+\begin{code}
+compareFixity :: Fixity -> Fixity
+             -> (Bool,         -- Error please
+                 Bool)         -- Associate to the right: a op1 (b op2 c)
+compareFixity (Fixity prec1 dir1) (Fixity prec2 dir2)
+  = case prec1 `compare` prec2 of
+       GT -> left
+       LT -> right
+       EQ -> case (dir1, dir2) of
+                       (InfixR, InfixR) -> right
+                       (InfixL, InfixL) -> left
+                       _                -> error_please
+  where
+    right       = (False, True)
+    left         = (False, False)
+    error_please = (True,  False)
+\end{code}
+
 
 %************************************************************************
 %*                                                                     *
@@ -291,6 +368,9 @@ isDeadOcc :: OccInfo -> Bool
 isDeadOcc IAmDead = True
 isDeadOcc other          = False
 
+isOneOcc (OneOcc _ _) = True
+isOneOcc other       = False
+
 isFragileOcc :: OccInfo -> Bool
 isFragileOcc (OneOcc _ _) = True
 isFragileOcc other       = False
@@ -343,6 +423,28 @@ instance Outputable StrictnessMark where
 
 %************************************************************************
 %*                                                                     *
+\subsection{Success flag}
+%*                                                                     *
+%************************************************************************
+
+\begin{code}
+data SuccessFlag = Succeeded | Failed
+
+successIf :: Bool -> SuccessFlag
+successIf True  = Succeeded
+successIf False = Failed
+
+succeeded, failed :: SuccessFlag -> Bool
+succeeded Succeeded = True
+succeeded Failed    = False
+
+failed Succeeded = False
+failed Failed    = True
+\end{code}
+
+
+%************************************************************************
+%*                                                                     *
 \subsection{Activation}
 %*                                                                     *
 %************************************************************************
@@ -354,23 +456,23 @@ type CompilerPhase = Int  -- Compilation phase
                                -- Phases decrease towards zero
                                -- Zero is the last phase
 
-pprPhase :: CompilerPhase -> SDoc
-pprPhase n = brackets (int n)
-
 data Activation = NeverActive
                | AlwaysActive
+               | ActiveBefore CompilerPhase    -- Active only *before* this phase
                | ActiveAfter CompilerPhase     -- Active in this phase and later
                deriving( Eq )                  -- Eq used in comparing rules in HsDecls
 
 instance Outputable Activation where
-   ppr AlwaysActive    = empty         -- The default
-   ppr (ActiveAfter n) = pprPhase n
-   ppr NeverActive     = ptext SLIT("NEVER")
+   ppr AlwaysActive     = empty                -- The default
+   ppr (ActiveBefore n) = brackets (char '~' <> int n)
+   ppr (ActiveAfter n)  = brackets (int n)
+   ppr NeverActive      = ptext SLIT("NEVER")
     
 isActive :: CompilerPhase -> Activation -> Bool
-isActive p NeverActive     = False
-isActive p AlwaysActive    = True
-isActive p (ActiveAfter n) = p <= n
+isActive p NeverActive      = False
+isActive p AlwaysActive     = True
+isActive p (ActiveAfter n)  = p <= n
+isActive p (ActiveBefore n) = p >  n
 
 isNeverActive, isAlwaysActive :: Activation -> Bool
 isNeverActive NeverActive = True
@@ -379,3 +481,4 @@ isNeverActive act     = False
 isAlwaysActive AlwaysActive = True
 isAlwaysActive other       = False
 \end{code}
+