gpt4 book ai didi

haskell - 用元组伪造的多参数新类型?

转载 作者:行者123 更新时间:2023-12-02 08:58:35 27 4
gpt4 key购买 nike

这是一个愚蠢的问题,困扰了我一段时间。为什么我不能编写具有多个参数的新类型,

newtype A = A Int Int

元组版本就可以了吗?

newtype A = A (Int, Int)

前者在模式匹配等方面要好得多。

最佳答案

newtype A = A Int 创建与 Int 同构的类型。也就是说,它的行为与 Int 完全相同。例如bottom,但名称不同。

这与 data A = A Int 形成对比,后者创建了一个行为与 Int 不同的提升类型。还有另一个 Int 中没有的附加值:A undefined(与 undefined::A 不同)。

现在,newtype A = A (Int, Int) 创建一个与 (Int, Int) 同构的类型。顺便说一句,这正是 data A = A Int Int 所做的。

如果我们承认 newtype A = A Int Int 等同于 newtype A = A (Int, Int),我们得到了什么? newtype A = A Int Int 相当于 newtype A = A (Int, Int),相当于 data A = A Int Int

newtype A = A Int Int 相当于 data A = A Int Int(因此在这种情况下 newtype 是多余的),<强>但是

newtype A = A Int 等于 data A = A Int (这就是 newtype 的全部意义所在 首先)。

因此我们必须得出结论,newtype A = A Int Int 等同于 newtype A = A (Int, Int) 会造成冗余和不一致,我们更好关闭不允许。

可能没有办法赋予 newtype A = A Int Int 一些不受这些不一致影响的其他含义(否则我想它会被发现并使用;)

关于haskell - 用元组伪造的多参数新类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6970662/

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