(Stream s Identity t)
下面的类型声明中的约束是什么意思?
parse :: (Stream s Identity t)
=> Parsec s () a -> SourceName -> s -> Either ParseError a
下面的类声明是什么Stream
,是什么意思。我完全迷路了。
class Monad m => Stream s m t | s -> t where
当我使用 Parsec 时,我总是被类型签名 ( xxx :: yyy
) 所困扰。我总是跳过签名,将 src 加载到 ghci 中,然后将类型签名复制回我的 .hs 文件。它有效,但我仍然不明白所有这些签名是什么。
编辑:更多关于我的问题的观点。
我仍然对类型签名的“上下文”感到困惑:
(Show a) =>
手段a
必须是类的一个实例Show
。
(Stream s Identity t) =>
这个“上下文”是什么意思,因为t
在=>
我有很多不同的解析器要运行,所以我编写了一个 warp 函数来运行任何带有真实文件的解析器。但问题来了:
这是我的代码,它无法加载,我怎样才能让它工作?
module RunParse where
import System.IO
import Data.Functor.Identity (Identity)
import Text.Parsec.Prim (Parsec, parse, Stream)
--what should I write "runIOParse :: ..."
--runIOParse :: (Stream s Identity t, Show a) => Parsec s () a -> String -> IO ()
runIOParse pa filename =
do
inh <- openFile filename ReadMode
outh <- openFile (filename ++ ".parseout") WriteMode
instr <- hGetContents inh
let result = show $ parse pa filename instr
hPutStr outh result
hClose inh
hClose outh