Simulate global variable

前端 未结 2 928
闹比i
闹比i 2020-12-19 06:11

I\'m working on a project in Haskell and I need a global variable. Currently I\'m doing this:

 funcs :: Map.Map String Double
 funcs = Map.empty

 eliminate          


        
相关标签:
2条回答
  • 2020-12-19 06:35

    With let funcs = aux you're only giving funcs a new binding in the scope of the f function, which means that the funcs you're referring to in g is the one in the global scope - the one that's defined as Map.empty. It is not possible to change pure values, global or otherwise, at runtime. It is, however, possible to use mutable references. Preferrably locally, but also globally with a bit of unsafe hackery.

    Is it really necessary to use a global variable though? If you're not using your global throughout your entire program, you may want to wrap all the computations that use it in a State monad instead:

    import Control.Monad.State
    import qualified Data.Map as Map
    
    funcs :: Map.Map String Double
    funcs = Map.empty
    
    f :: String -> Double -> State (Map.Map String Double) ()
    f str d = do
      funcs <- get
      put (Map.insert str d funcs)
    
    g :: State (Map.Map String Double) String
    g = do
      funcs <- get
      if (Map.lookup "aaa" funcs) == Nothing then return "not defined" else return "ok"
    
    main = putStrLn $ flip evalState funcs $ do {f "aaa" 1; g}
    

    Keeping your state constrained in this way makes it easier to keep track of your program as it grows; you always know which computations may alter your state as it's clearly indicated by its type.

    If, on the other hand, you for some reason absolutely need global variables, there is a well-known but fairly ugly trick using IORefs and unsafePerformIO:

    import Data.IORef
    import System.IO.Unsafe
    import qualified Data.Map as Map
    
    {-# NOINLINE funcs #-}
    funcs :: IORef (Map.Map String Double)
    funcs = unsafePerformIO $ newIORef Map.empty
    
    f :: String -> Double -> IO ()
    f str d = atomicModifyIORef funcs (\m -> (Map.insert str d m, ()))
    
    g :: IO ()
    g = do
      fs <- readIORef funcs
      if (Map.lookup "aaa" fs) == Nothing then error "not defined" else putStrLn "ok"
    
    main = do
      f "aaa" 1
      g
    

    This trick creates a global IORef, which can be read and updated inside the IO monad. This means that any computation that performs IO may change the value of your global, which gives you all the wonderful headaches of global state. Aside from that, this trick is also extremely hacky and only works because of implementation details in GHC (see the {-# NOINLINE funcs #-} part, for instance).

    If you decide to use this hack (which I really recommend against), keep in mind that you can absolutely not use it with polymorphic values. To illustrate why:

    import Data.IORef
    import System.IO.Unsafe
    
    {-# NOINLINE danger #-}
    danger :: IORef a
    danger = unsafePerformIO $ newIORef undefined
    
    coerce :: a -> IO b
    coerce x = do
      writeIORef danger x
      readIORef danger
    
    main = do
      x <- coerce (0 :: Integer) :: IO (Double, String) -- boom!
      print x
    

    As you can see, this trick can be used together with polymorphism to write a function that reinterprets any type as any other type, which obviously breaks type safety and so may cause your programs to segfault (at best).

    In summary, do consider using the State monad instead of global variables; do not turn to global variables lightly.

    0 讨论(0)
  • 2020-12-19 06:45

    Functional programming doesn't have states or global variables, every function should be able to run separetely from everyone else. There are some tricks:

    1. Write and read state to file

    http://hackage.haskell.org/packages/archive/base/latest/doc/html/Prelude.html#v:readFile

    1. Use state Monads ( http://en.wikibooks.org/wiki/Haskell/Understanding_monads/State )

      import Data.IORef
      
      type Counter = Int -> IO Int
      
      makeCounter :: IO Counter
      makeCounter = do
          r <- newIORef 0
         return (\i -> do modifyIORef r (+i)
                          readIORef r)
      
      testCounter :: Counter -> IO ()
      testCounter counter = do
        b <- counter 1
        c <- counter 1
        d <- counter 1
        print [b,c,d]
      
      main = do
        counter <- makeCounter
        testCounter counter
        testCounter counter
      
    0 讨论(0)
提交回复
热议问题