haskell - 重构 where 子句

标签 haskell monads parsec

我正在学习 Haskell,所以这可能是一些非常微不足道的事情,但我希望得到一些关于如何重写它以及它如何工作的指示。

我有以下工作代码(使用的包: HTFParsecFlow ):

{-# OPTIONS_GHC -F -pgmF htfpp #-}
{-# LANGUAGE FlexibleContexts #-}

module Main where

import Test.Framework -- assertEqual, assertBool, htfMain, htf_thisModulesTests
import Text.ParserCombinators.Parsec (eof, spaces, parse)
import Flow ((|>))
import Data.Either (isLeft)

whiteSpaces = spaces

test_parse_whitespace = do
  mapM_ positive [
      "", " ", "\t", "\n", "\r\n", "  \r\n  ",
      "   \t   \r\n  \t  \n   \r  \t "
    ]
  mapM_ negative ["x",  " x",  "x ",  " x ",  "\t_\t"]
  where
    parser = whiteSpaces >> eof
    parseIt = parse parser ""
    positive str = assertEqual (parseIt str) (Right ())
    negative str = assertBool (parseIt str |> isLeft)

main :: IO ()
main = htfMain htf_thisModulesTests

我正在添加一个新的测试,它的 where 部分几乎相同,所以我尝试像这样重构它:

pos_neg_case parser = do
  return [positive, negative]
  where
    fullParser = parser >> eof
    parseIt = parse fullParser ""
    positive str = assertEqual (parseIt str) (Right ())
    negative str = assertBool (parseIt str |> isLeft)

test_parse_whitespace' = do
  mapM_ positive [
      "", " ", "\t", "\n", "\r\n", "  \r\n  ",
      "   \t   \r\n  \t  \n   \r  \t "
    ]
  mapM_ negative ["x",  " x",  "x ",  " x ",  "\t_\t"]
  where
    [positive, negative] = pos_neg_case whiteSpaces

这不起作用(即使我按照编译器的建议打开 lang. 功能)。

Couldn't match expected type ‘[Char] -> m b0’
            with actual type ‘[String -> IO ()]’
Relevant bindings include
  test_parse_whitespace' :: m () (bound at test/Spec.hs:21:1)
In the first argument of ‘mapM_’, namely ‘positive’
In a stmt of a 'do' block:
  mapM_ positive ["", " ", "\t", "\n", ....]

Couldn't match expected type ‘[Char] -> m b1’
            with actual type ‘[String -> IO ()]’
Relevant bindings include
  test_parse_whitespace' :: m () (bound at test/Spec.hs:21:1)
In the first argument of ‘mapM_’, namely ‘negative’
In a stmt of a 'do' block:
  mapM_ negative ["x", " x", "x ", " x ", ....]

最佳答案

正如您所注意到的,问题在于您添加到的return:

pos_neg_case parser = do
  return [positive, negative]
  where -- etc.

mapM_的类型是:

GHCi> :t mapM_
mapM_ :: (Foldable t, Monad m) => (a -> m b) -> t a -> m ()

正数负数是已经具有要传递给mapM的适当类型的函数,因此如果您想要pos_neg_case 要将它们作为列表返回,您只需将它们包装在列表中即可。 return 不是关键字;它只是一个将值注入(inject)到单子(monad)上下文中的函数。如果您不需要进行任何此类注入(inject),则不需要return

PS:引用你的答案:

But I had to guess the Parser type, hole was giving me very complex thingy Text.Parsec.Prim.ParsecT s () Data.Functor.Identity.Identity a -> [s -> IO ()].

这是一个非常常见的模式的示例。 ParsecT 是一个具有相当多类型变量的类型构造函数,而 Parser 是其中一些变量的一组通用选择的类型同义词,它允许更整洁的类型签名其中没有明确提及它们。如果您在 documentation 中查找它(索引在这种情况下很有帮助)或者在 GHCi 中使用 :info,你会发现 Parser 只是意味着...

type Parser = Parsec String ()

...和秒差距,依次是...

type Parsec s u = ParsecT s u Identity

...这样扩展Parser同义词就可以得到ParsecT String () Identity,这就是 GHC 在引入类型漏洞时告诉您的。

关于haskell - 重构 where 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40450864/

相关文章:

haskell - 如何使用 QuickCheck2 预测异常

haskell - 我可以让 GHC 推断出超过 GADT 模式匹配的约束吗?

haskell - 优化计算 200 万以下所有素数总和的 Haskell 代码

haskell - Haskell的Lens库中 `ix`和 `at`有什么区别

f# - FParsec:如何在 fparsec 中解析日期(新手)

scala - 使用 scalaz Monad 的示例

haskell - 在 Haskell 中编写汇编程序 - 带状态的 mapM?

haskell - Monad 的应用

parsing - 解析器返回值的自定义 ADT 与树

haskell - parsec:意外字符解析嵌套注释