GHC 7.0.4 似乎忘记了如何应用函子

发布于 2024-12-08 10:30:57 字数 2110 浏览 1 评论 0原文

在《Learn You A Haskell》的“函子、应用函子和幺半群”一章中,Miran 做了以下事情:

ghci> (pure 3) "blah"
3

然而我明白了:

ghci> (pure 3) "blah"
<interactive>:1:2:
    No instance for (Functor ((->) [Char]))
      arising from a use of `pure'
    Possible fix:
      add an instance declaration for (Functor ((->) [Char]))
    In the expression: (pure 3) "blah"
    In an equation for `it': it = (pure 3) "blah"

我不确定会发生什么。到目前为止,所有示例都运行正常。我肯定没有进口什么东西,但我不知道是什么。

这是我的版本信息:

$ ghci -v
GHCi, version 7.0.4: http://www.haskell.org/ghc/  :? for help
Glasgow Haskell Compiler, Version 7.0.4, for Haskell 98, stage 2 booted by GHC version 6.12.3

我的 ~/.ghc/ghci.conf 如下所示:

{-# LANGUAGE Arrows #-}

:set prompt "[32;1m%s[0m\n> "

import Control.Arrow
import Control.Monad (when, forever, forM, liftM)
import Control.Applicative
-- import Control.Applicative (ZipList (..), (<$>), (<*>), pure)
import Control.Exception (IOException (..), catch)
import qualified Data.ByteString as ByteString (pack, unpack)
import qualified Data.ByteString.Lazy as LazyByteString (cons, cons', empty, fromChunks, pack, readFile, unpack, writeFile)
import Data.Char (chr, ord, toUpper, toLower, isDigit)
import Data.Function (fix, on)
import Data.Functor
import Data.List (find, genericLength, intercalate, intersperse, nub, tails)
import Data.Map (Map (..))
import qualified Data.Map as Map (fromList, lookup)
import Data.Monoid (mempty, mappend, mconcat)
import Data.Tuple (fst, snd, curry, uncurry, swap)
import System.Console.ANSI (setCursorPosition, clearScreen)
import System.Directory (renameFile, doesFileExist)
import System.Environment (getArgs, getProgName)
import System.IO (IOMode (..), stdout, openFile, withFile, hGetContents, hClose, openTempFile, hPutStr, hFlush)
import System.IO.Error (isDoesNotExistError)
import System.Random (StdGen (..), RandomGen (..), Random (..), getStdGen, mkStdGen, newStdGen, random, randomR, randomRIO, randoms)
import Text.Printf (PrintfArg (..), printf)

In the "Functors, Applicative Functors and Monoids" chapter of Learn You A Haskell, Miran does the following:

ghci> (pure 3) "blah"
3

I however get this:

ghci> (pure 3) "blah"
<interactive>:1:2:
    No instance for (Functor ((->) [Char]))
      arising from a use of `pure'
    Possible fix:
      add an instance declaration for (Functor ((->) [Char]))
    In the expression: (pure 3) "blah"
    In an equation for `it': it = (pure 3) "blah"

I am not sure what happen. All the examples have worked correctly until now. I must not have imported something, but I don't know what.

Here is my version information:

$ ghci -v
GHCi, version 7.0.4: http://www.haskell.org/ghc/  :? for help
Glasgow Haskell Compiler, Version 7.0.4, for Haskell 98, stage 2 booted by GHC version 6.12.3

My ~/.ghc/ghci.conf looks like this:

{-# LANGUAGE Arrows #-}

:set prompt "[32;1m%s[0m\n> "

import Control.Arrow
import Control.Monad (when, forever, forM, liftM)
import Control.Applicative
-- import Control.Applicative (ZipList (..), (<
gt;), (<*>), pure)
import Control.Exception (IOException (..), catch)
import qualified Data.ByteString as ByteString (pack, unpack)
import qualified Data.ByteString.Lazy as LazyByteString (cons, cons', empty, fromChunks, pack, readFile, unpack, writeFile)
import Data.Char (chr, ord, toUpper, toLower, isDigit)
import Data.Function (fix, on)
import Data.Functor
import Data.List (find, genericLength, intercalate, intersperse, nub, tails)
import Data.Map (Map (..))
import qualified Data.Map as Map (fromList, lookup)
import Data.Monoid (mempty, mappend, mconcat)
import Data.Tuple (fst, snd, curry, uncurry, swap)
import System.Console.ANSI (setCursorPosition, clearScreen)
import System.Directory (renameFile, doesFileExist)
import System.Environment (getArgs, getProgName)
import System.IO (IOMode (..), stdout, openFile, withFile, hGetContents, hClose, openTempFile, hPutStr, hFlush)
import System.IO.Error (isDoesNotExistError)
import System.Random (StdGen (..), RandomGen (..), Random (..), getStdGen, mkStdGen, newStdGen, random, randomR, randomRIO, randoms)
import Text.Printf (PrintfArg (..), printf)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瑾兮 2024-12-15 10:30:57

您缺少 Control.Monad.Instances。不知道为什么它被定义在那里,但我以前也遇到过这个。

GHCi, version 7.0.4: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.

Prelude Control.Applicative> (pure 3) "blah"

<interactive>:1:2:
    No instance for (Functor ((->) [Char]))
      arising from a use of `pure'
    Possible fix:
      add an instance declaration for (Functor ((->) [Char]))
    In the expression: (pure 3) "blah"
    In an equation for `it': it = (pure 3) "blah"
Prelude Control.Applicative> import Control.Monad.Instances 
Prelude Control.Applicative Control.Monad.Instances> (pure 3) "blah"
3

另外,在查看 LYAH 中的章节后,作者确实定义了示例上方的实例,但不明显这已经在其他地方定义了。

更新

错误并不是因为 ghci 忘记了如何应用函子,而是函子实例 Functor ((->) [Char]) 尚未在您的环境中定义。

You're missing Control.Monad.Instances. Not sure why it's defined there, but I've run into this before as well.

GHCi, version 7.0.4: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.

Prelude Control.Applicative> (pure 3) "blah"

<interactive>:1:2:
    No instance for (Functor ((->) [Char]))
      arising from a use of `pure'
    Possible fix:
      add an instance declaration for (Functor ((->) [Char]))
    In the expression: (pure 3) "blah"
    In an equation for `it': it = (pure 3) "blah"
Prelude Control.Applicative> import Control.Monad.Instances 
Prelude Control.Applicative Control.Monad.Instances> (pure 3) "blah"
3

Also, after looking at the chapter in LYAH, the author does define the instance above the example, but it's not obvious this is already defined elsewhere.

Update

The error wasn't because ghci forgot how to apply functors, rather the Functor instance Functor ((->) [Char]) was not yet defined in your environment.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文