2011-10-07 46 views
6

在“仿函数,应用型函子幺”了解你的Haskell的章,米朗执行以下操作:GHC 7.0.4似乎已经忘记了如何申请函子

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) 
+1

适合我... – Landei

+0

什么版本的ghci是那个? –

+1

适用于使用“The Glorious Glasgow Haskell编译系统,版本7.0.3”,从'Control.Applicative'输入'pure'' –

回答

9

你错过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])尚未在您的环境中定义。

+0

工作!谢谢! –

+4

在一个真正的Haskell程序中,你不需要导入Control.Monad.Instances,这个实例必须在Control.Applicative中可见,所以它也应该在你的程序中可见。但是,由于ghci导入语法最近(相当)最近被扩展了,所以有一些bug。我认为他们现在已经被压扁了。 所以:这*不应该是必要的,但是是一个很好的解决方法。 –

相关问题