Haskell IORef-答案与获得答案的功能

时间:2018-11-26 19:38:53

标签: haskell io closures do-notation ioref

我试图了解IORefs的实际用法,并且在遵循在https://www.seas.upenn.edu/~cis194/spring15/lectures/12-unsafe.html上找到的示例代码时遇到麻烦

newCounter :: IO (IO Int)
newCounter = do
  r <- newIORef 0
  return $ do
    v <- readIORef r
    writeIORef r (v + 1)
    return v

printCounts :: IO ()
printCounts = do
  c <- newCounter
  print =<< c
  print =<< c
  print =<< c

printCounts执行“ c <- newCounter”时,为什么cnewCounterreturn $ do”块中没有得到完成工作的结果,似乎应该在第一次调用它时将其分配给常量“ IO 0”,然后再不进行更改?相反,c似乎被分配了在该“ return $ do”块中定义的功能,然后每次printCounts到达另一个“ print =<< c”时重新执行。看来答案似乎在于newCounter具有双嵌套的“ IO (IO Int)”类型,但是我无法理解为什么这会使c成为被调用时要重新执行的函数常数的值仅评估一次。

1 个答案:

答案 0 :(得分:7)

您可以将IO视为一种程序。 newCounter :: IO (IO Int)是输出程序的程序。更准确地说,newCounter分配一个新的计数器,并返回一个程序,该程序在运行时递增该计数器并返回其旧值。 newCounter不执行返回的程序。如果您改为写:

newCounter :: IO (IO Int)
newCounter = do 
  r <- newIORef 0
  let p = do              -- name the counter program p
        v <- readIORef r
        writeIORef r (v + 1)
        return v
  p          -- run the counter program once
  return p   -- you can still return it to run again later

您还可以使用方程式推理将printCounts展开为一系列图元。以下printCounts的所有版本都是等效程序:

-- original definition
printCounts :: IO ()
printCounts = do
  c <- newCounter
  print =<< c
  print =<< c
  print =<< c

-- by definition of newCounter...

printCounts = do
  c <- do
    r <- newIORef 0
    return $ do
      v <- readIORef r
      writeIORef r (v + 1)
      return v
  print =<< c
  print =<< c
  print =<< c

-- by the monad laws (quite hand-wavy for brevity)
-- do
--   c <- do
--     X
--     Y
--   .....
-- =
-- do
--   X
--   c <- 
--     Y
--   .....
--
-- (more formally,
--  ((m >>= \x -> k x) >>= h) = (m >>= (\x -> k x >>= h)))

printCounts = do
  r <- newIORef 0
  c <-
    return $ do
      v <- readIORef r
      writeIORef r (v + 1)
      return v
  print =<< c
  print =<< c
  print =<< c

-- c <- return X
-- =
-- let c = X
--
-- (more formally, ((return X) >>= (\c -> k c)) = (k X)

printCounts = do
  r <- newIORef 0
  let c = do
        v <- readIORef r
        writeIORef r (v + 1)
        return v
  print =<< c
  print =<< c
  print =<< c

-- let-substitution

printCounts = do
  r <- newIORef 0
  print =<< do
        v <- readIORef r
        writeIORef r (v + 1)
        return v
  print =<< do
        v <- readIORef r
        writeIORef r (v + 1)
        return v
  print =<< do
        v <- readIORef r
        writeIORef r (v + 1)
        return v

-- after many more applications of monad laws and a bit of renaming to avoid shadowing
-- (in particular, one important step is ((return v >>= print) = (print v)))

printCounts = do
  r <- newIORef 0
  v1 <- readIORef r
  writeIORef r (v1 + 1)
  print v1
  v2 <- readIORef r
  writeIORef r (v2 + 1)
  print v2
  v3 <- readIORef r
  writeIORef r (v3 + 1)
  print v3

在最终版本中,您可以看到printCounts实际上是分配了一个计数器并将其递增3次,从而打印出每个中间值。

一个关键步骤是让替换步骤,计数器程序被复制,这就是为什么它要运行三次的原因。 let x = p; ...x <- p; ...不同,后者运行p,并将x绑定到结果而不是程序p本身。