symbol (empty) → 0.1
raw patch · 4 files changed
+157/−0 lines, 4 filesdep +basedep +containersdep +sybsetup-changed
Dependencies added: base, containers, syb
Files
- Data/Symbol.hs +98/−0
- LICENSE +26/−0
- Setup.hs +3/−0
- symbol.cabal +30/−0
+ Data/Symbol.hs view
@@ -0,0 +1,98 @@+-- Copyright (c) 2009-2010+-- The President and Fellows of Harvard College.+--+-- Redistribution and use in source and binary forms, with or without+-- modification, are permitted provided that the following conditions+-- are met:+-- 1. Redistributions of source code must retain the above copyright+-- notice, this list of conditions and the following disclaimer.+-- 2. Redistributions in binary form must reproduce the above copyright+-- notice, this list of conditions and the following disclaimer in the+-- documentation and/or other materials provided with the distribution.+-- 3. Neither the name of the University nor the names of its contributors+-- may be used to endorse or promote products derived from this software+-- without specific prior written permission.++-- THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND+-- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE+-- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE+-- ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE+-- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL+-- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS+-- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)+-- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT+-- LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY+-- OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF+-- SUCH DAMAGE.++--------------------------------------------------------------------------------+-- |+-- Module : Data.Symbol+-- Copyright : (c) Harvard University 2009-2010+-- License : BSD-style+-- Maintainer : mainland@eecs.harvard.edu+--+--------------------------------------------------------------------------------++{-# LANGUAGE CPP #-}+{-# LANGUAGE DeriveDataTypeable #-}++module Data.Symbol (+ Symbol(..),+ intern,+ unintern+ ) where++import Control.Concurrent.MVar+import Data.Generics (Data, Typeable)+#if __GLASGOW_HASKELL__ >= 608+import Data.String+#endif /* __GLASGOW_HASKELL__ >= 608 */+import qualified Data.Map as Map+import System.IO.Unsafe (unsafePerformIO)++data Symbol = -- | Unique identifier and the string itself+ Symbol {-# UNPACK #-} !Int !String+#if defined(__GLASGOW_HASKELL__)+ deriving (Data, Typeable)+#endif /* defined(__GLASGOW_HASKELL__) */++instance Eq Symbol where+ (Symbol i1 _) == (Symbol i2 _) = i1 == i2++instance Ord Symbol where+ compare (Symbol i1 _) (Symbol i2 _) = compare i1 i2++#if __GLASGOW_HASKELL__ >= 608+instance IsString Symbol where+ fromString = intern+#endif /* __GLASGOW_HASKELL__ >= 608 */++data SymbolEnv = SymbolEnv+ { uniq :: {-# UNPACK #-} !Int+ , symbols :: !(Map.Map String Symbol)+ }++symbolEnv :: MVar SymbolEnv+symbolEnv = unsafePerformIO $ newMVar $ SymbolEnv 1 Map.empty++-- We @seq@ @s@ so that we can guarantee that when we perform the lookup we+-- won't potentially have to evaluate a thunk that might itself call @intern@,+-- leading to a deadlock.++-- |Intern a string to produce a 'Symbol'.+{-# NOINLINE intern #-}+intern :: String -> Symbol+intern s = s `seq` unsafePerformIO $ modifyMVar symbolEnv $ \env -> do+ case Map.lookup s (symbols env) of+ Nothing -> do let sym = Symbol (uniq env) s+ let env' = env { uniq = uniq env + 1,+ symbols = Map.insert s sym+ (symbols env)+ }+ env' `seq` return (env', sym)+ Just sym -> return (env, sym)++-- |Return the 'String' associated with a 'Symbol'.+unintern :: Symbol -> String+unintern (Symbol _ s) = s
+ LICENSE view
@@ -0,0 +1,26 @@+Copyright (c) 2006-2010+ The President and Fellows of Harvard College.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions+are met:+1. Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.+2. Redistributions in binary form must reproduce the above copyright+ notice, this list of conditions and the following disclaimer in the+ documentation and/or other materials provided with the distribution.+3. Neither the name of the University nor the names of its contributors+ may be used to endorse or promote products derived from this software+ without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE+ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS+OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)+HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY+OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF+SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,3 @@+import Distribution.Simple++main = defaultMain
+ symbol.cabal view
@@ -0,0 +1,30 @@+name: symbol+version: 0.1+cabal-version: >= 1.6+license: BSD3+license-file: LICENSE+copyright: (c) 2006-2010 Harvard University+author: Geoffrey Mainland <mainland@eecs.harvard.edu>+maintainer: mainland@eecs.harvard.edu+stability: alpha+homepage: http://www.eecs.harvard.edu/~mainland/+category: Data+synopsis: A 'Symbol' type for fast symbol comparison.+description: Provides a 'Symbol' data type allowing fast symbol comparisons+ and functions for interning symbols and recovering their+ 'String' representation.++build-type: Simple++library+ exposed-modules:+ Data.Symbol++ build-depends:+ base >= 4 && < 5,+ containers >= 0.2 && < 0.4,+ syb >= 0.1 && < 0.2++source-repository head+ type: svn+ location: http://senseless.eecs.harvard.edu/repos/mainland-projects/symbol/trunk/