gpt4 book ai didi

haskell - 为什么所有递归模式同义词都被拒绝?

转载 作者:行者123 更新时间:2023-12-02 18:36:28 25 4
gpt4 key购买 nike

{-# LANGUAGE PatternSynonyms, ViewPatterns #-}

data Quun = Foo | Bar | Oink Quun

fooey :: Quun -> Bool
fooey Foo = True
fooey (Oink Yum) = True
fooey _ = False

pattern Yum <- (fooey -> True)

这无法编译(至少在 GHC-7.10.2 中)

/tmp/wtmpf-file10227.hs:1:1:
Recursive pattern synonym definition with following bindings:
foo (defined at /tmp/wtmpf-file10227.hs:(6,1)-(8,13))
Yum (defined at /tmp/wtmpf-file10227.hs:10:1-28)

当然,对于简单的直接自引用模式来说,这是有意义的。但是,是否有一些根本原因导致上述 View 模式介导的布局也不可能实现?我觉得这没有说服力;毕竟,可以内联 View 模式并获得完全无害的(嗯......至少,允许)定义:

fooey :: Quun -> Bool
fooey Foo = True
fooey (Oink (fooey -> True)) = True
fooey _ = False

pattern Yum <- (fooey -> True)

那么,这些同义词是否由于技术原因尚不可用?我们将来会得到它们吗?

最佳答案

一些递归模式是有问题的,例如

f :: [()] -> Bool
f L = True
f _ = False

pattern L <- () : L

这应该脱糖做什么?

模式不是一流的值。它们只是在出现的地方被它们的定义所替换。对于这样的语言,递归定义通常是不明智的。

关于haskell - 为什么所有递归模式同义词都被拒绝?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35439671/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com