Unable to compile Writer Monad example from “Learn you a Haskell”

眉间皱痕 提交于 2019-11-30 23:23:39

问题


The following code, which is verbatim from LYAH, doesn't compile. Code and compile-time error are included below. On the LYAH page, the code is ~15% down the page, yay emacs browser :)

Any ideas why? Am I overlooking something totally obvious?

(Despite the similarity in post-titles, I think my question is different from this one.)


Here's the code (in a file which I named testcopy.hs)

import Control.Monad.Writer

logNumber :: Int -> Writer [String] Int
logNumber x = Writer (x, ["Got number: " ++ show x])

multWithLog :: Writer [String] Int
multWithLog = do
    a <- logNumber 3
    b <- logNumber 5
    return (a*b)

And here's the compile-time error:

Prelude> :l testcopy.hs
[1 of 1] Compiling Main             ( testcopy.hs, interpreted )
testcopy.hs:4:15:
    Not in scope: data constructor `Writer'
    Perhaps you meant `WriterT' (imported from Control.Monad.Writer)
Failed, modules loaded: none.

回答1:


LYAH is outdated in this example. You should use the writer smart constructor method instead of the (now non-existent) Writer data constructor.

To expand a bit, these data types were updated to be more compatible with monad transformers. As a result, there is a general WriterT, for use in a monad transformer stack, and a Writer type synonym that composes WriterT with Identity. Because of this, there is no longer a data constructor associated specifically with the Writer type (since Writer is a type synonym).

Luckily, despite this complication, the solution is pretty simple: replace Writer with writer.




回答2:


The correct version in GHC 7.10.3 should be like this

import Control.Monad.Writer

logNumber :: Int -> Writer [String] Int
logNumber x = writer (x, ["Got number: " ++ show x])

multWithLog :: Writer [String] Int
multWithLog = do
    a <- logNumber 3
    b <- logNumber 5
    return (a*b)


来源:https://stackoverflow.com/questions/26415122/unable-to-compile-writer-monad-example-from-learn-you-a-haskell

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!