Haskell/GHC:用相同的模式匹配多个一元构造函数 [英] Haskell/GHC: Matching multiple unary constructors with the same pattern
本文介绍了Haskell/GHC:用相同的模式匹配多个一元构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
因此,我一直在尝试定义TrieSet数据类型(即使我知道我不需要):
So I was playing around with defining a TrieSet datatype (even though I know I don't need to):
module Temp where
import Data.Map
data TrieSet a = Nonterminal (Data.Map a (TrieSet a)) | Terminal (Data.Map a (TrieSet a))
insert :: Ord a => [a] -> TrieSet a -> TrieSet a
insert [] (_ m) = Terminal m
insert (a:as) (c m) = c $ insertWith (insert as . flip const) a (insert as $ Nonterminal empty) m
当我遇到错误时,我从未见过:
When I got an error I've never seen before:
% ghc -c Temp.hs
Temp.hs:8:11: Parse error in pattern
因此,GHC似乎不喜欢以相同的模式匹配多个一元构造函数.我进行了另一项测试,以确保这是问题所在:
So it seemed like GHC doesn't like matching multiple unary constructors with the same pattern. I did another test to make sure that was the problem:
module Temp2 where
extract :: Either String String -> String
extract (_ s) = s
似乎证实了我的怀疑:
% ghc -c Temp2.hs
Temp2.hs:4:9: Parse error in pattern
所以我的问题是(分为多个部分):
So my question is (in multiple parts):
- 我对GHC为什么不喜欢这些功能吗?
- 为什么这不是Haskell标准的一部分?毕竟,我们可以用相同的模式匹配多个null构造函数.
- 我可以给GHC使其接受这些语言吗?
推荐答案
- 是的.从来没有支持过这种通配符.
- 在我看来,如果您不知道匹配的数据构造函数,则很难推断出函数的类型.只需考虑一个函数
f(_ n)= n
.它的类型应该是什么?Haskell的类型系统无法描述类型构造函数的多样性,因此不存在像f
这样的函数. - 我不这样认为.
- Yes. That kind of wildcards was never supported.
- In my opinion, it would be much more difficult to infer a function's type if you don't know the data-constructor that was matched on. Just think about a function
f (_ n) = n
. What should be its type? The type-system of Haskell has no way to describe the arity of a type's constructors, so a function likef
could not exist. - I don't think so.
这篇关于Haskell/GHC:用相同的模式匹配多个一元构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文