3

Graham Hutton の Programming in Haskell book ( http://www.cs.nott.ac.uk/~gmh/book.html ) の例を実行しようとしています。例はリテラシーな Haskell で書かれていますが、ghci を起動して例を読み込むことができました。例ghci cipher.lhs( http://www.cs.nott.ac.uk/~gmh/cipher.lhs ):

GHCi, version 7.10.2: http://www.haskell.org/ghc/  :? for help
[1 of 1] Compiling Main             ( cipher.lhs, interpreted )
Ok, modules loaded: Main.
*Main> let2int 'a'
0

ただし、いくつかの例では、ghci の変更により、いくつかの問題があります。たとえば、第 8 章の Parsing.ls にNo instance for (Applicative ...)エラーがあります。

https://ghc.haskell.org/trac/ghc/wiki/Migration/7.10から、コードを追加してエラーの一部を削除するヒントを得ました。

> instance Applicative Parser where
>    pure  = return
>    (<*>) = ap  -- defined in Control.Monad
>
> instance Functor Parser where
>    fmap  = liftM
>
> instance Alternative Parser where
>     (<|>) = mplus
>     empty = mzero 

ただし、次のエラー メッセージを解決できませんでした。

Not in scope: type constructor or class ‘Alternative’

これの何が問題で、この問題を解決するにはどうすればよいですか? 問題を引き起こす元のコードは、http ://www.cs.nott.ac.uk/~gmh/Parsing.lhs にあります。

解決

このコードを追加するとうまくいきます:

import qualified Control.Applicative as CA
instance CA.Alternative Parser where ...
4

2 に答える 2

1

私もこの問題に遭遇しました。モジュールの適合バージョンが必要な場合はparsing.lhs、インポートセクションに配置するだけです:

> import qualified Control.Applicative as CA

型パーサー定義の後、このコード:

> instance Applicative Parser where
>    pure  = return
>    (<*>) = ap  -- defined in Control.Monad
> 
> instance Functor Parser where
>    fmap  = liftM
> 
> instance CA.Alternative Parser where
>    (<|>) = mplus
>    empty = mzero

または、ここでモジュールの適応バージョンを取得しますhttps://gist.github.com/myshov/85badeb087c51631aee3

于 2016-01-31T17:35:56.497 に答える