如何在Haskell中与Data.Text进行模式匹配?
我目前正在用 Haskell 编写解析器。我有以下代码。
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Data.Text
newtype Parser a = Parser { runParser :: Text -> Either Text (Text, a) }
char1 :: Char -> Parser Char
char1 c = Parser $ case
(x:xs) | x == c -> Right (xs, x)
_ -> Left "Unexpected character"
它无法编译这两个错误。
test.hs:12:6: error:
• Couldn't match expected type ‘Text’ with actual type ‘[Char]’
• In the pattern: x : xs
In a case alternative: (x : xs) | x == c -> Right (xs, x)
In the second argument of ‘($)’, namely
‘case
(x : xs) | x == c -> Right (xs, x)
_ -> Left "Unexpected character"’
|
12 | (x:xs) | x == c -> Right (xs, x)
| ^^^^
test.hs:12:24: error:
• Couldn't match type ‘[Char]’ with ‘Text’
Expected type: Either Text (Text, Char)
Actual type: Either Text ([Char], Char)
• In the expression: Right (xs, x)
In a case alternative: (x : xs) | x == c -> Right (xs, x)
In the second argument of ‘($)’, namely
‘case
(x : xs) | x == c -> Right (xs, x)
_ -> Left "Unexpected character"’
|
12 | (x:xs) | x == c -> Right (xs, x)
| ^^^^^^^^^^^^^
我可以通过替换Text数据类型来修复错误,String但我更喜欢使用Text数据类型。
有没有办法在Data.Text不首先将其显式转换为字符串的情况下与类型进行模式匹配?也许有一个 GHC 扩展可以让我这样做?
提前致谢。
回答
对@DanielWagner 的回答的改进,您可以结合视图模式和模式同义词来执行此操作。您需要一个新的构造函数来代替:,但它可能看起来像:
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE ViewPatterns #-}
import Data.Text
pattern x :> xs <- (uncons -> Just (x, xs))
pattern Empty <- (uncons -> Nothing)
findInText :: (Char -> Bool) -> Text -> Maybe Char
findInText _ Empty = Nothing
findInText p (x :> xs) | p x = Just x
| otherwise = findInText p xs
这里的想法是模式x :> xs是模式的同义词,模式uncons -> Just (x, xs)是一种视图模式,它通过应用于uncons被审查者并将结果与Just (x, xs)人口x和xs父模式进行模式匹配来进行操作。
根据评论,可能有人担心这种用法是否会uncons不止一次调用。优化完全关闭 ( -O0) 后,生成的核心确实有多个uncons调用:
-- unoptimized -O0
findInText
= ds ds1 ->
case uncons ds1 of {
Nothing -> Nothing;
Just ipv ->
case uncons ds1 of {
Nothing -> ...
通过 (-O或-O2)上的优化,所有内容都被内联,并且由于 Unicode 处理正在进行,生成的核心非常复杂。但是,如果您还定义了:
findInText' :: (Char -> Bool) -> Text -> Maybe Char
findInText' p txt = case uncons txt of
Nothing -> Nothing
Just (x, xs) | p x -> Just x
| otherwise -> findInText' p xs
事实证明,GHC 编译findInText'为:
findInText' = findInText
所以看起来至少在这种情况下,由于视图模式,GHC 不会做任何额外的工作。