[project @ 2002-12-05 23:49:43 by mthomas]
[ghc-hetmet.git] / ghc / compiler / utils / Util.lhs
index 119ae82..4949515 100644 (file)
@@ -13,9 +13,8 @@ module Util (
        nOfThem, 
        lengthExceeds, lengthIs, lengthAtLeast, listLengthCmp, atLength,
        isSingleton, only,
-       notNull,
+       notNull, snocView,
 
-       snocView,
        isIn, isn'tIn,
 
        -- for-loop
@@ -44,6 +43,9 @@ module Util (
        unzipWith,
 
        global,
+
+       -- module names
+       looksLikeModuleName,
     ) where
 
 #include "../includes/config.h"
@@ -64,6 +66,8 @@ import qualified List ( elem, notElem )
 import List            ( zipWith4 )
 #endif
 
+import Char            ( isUpper, isAlphaNum )
+
 infixr 9 `thenCmp`
 \end{code}
 
@@ -258,6 +262,15 @@ notNull :: [a] -> Bool
 notNull [] = False
 notNull _  = True
 
+snocView :: [a] -> Maybe ([a],a)
+       -- Split off the last element
+snocView [] = Nothing
+snocView xs = go [] xs
+           where
+               -- Invariant: second arg is non-empty
+             go acc [x]    = Just (reverse acc, x)
+             go acc (x:xs) = go (x:acc) xs
+
 only :: [a] -> a
 #ifdef DEBUG
 only [a] = a
@@ -266,14 +279,6 @@ only (a:_) = a
 #endif
 \end{code}
 
-\begin{code}
-snocView :: [a] -> ([a], a)    -- Split off the last element
-snocView xs = go xs []
-           where
-             go [x]    acc = (reverse acc, x)
-             go (x:xs) acc = go xs (x:acc)
-\end{code}
-
 Debugging/specialising versions of \tr{elem} and \tr{notElem}
 
 \begin{code}
@@ -782,3 +787,12 @@ Global variables:
 global :: a -> IORef a
 global a = unsafePerformIO (newIORef a)
 \end{code}
+
+Module names:
+
+\begin{code}
+looksLikeModuleName [] = False
+looksLikeModuleName (c:cs) = isUpper c && all isAlphaNumEx cs
+
+isAlphaNumEx c = isAlphaNum c || c == '_' || c == '.'
+\end{code}