gpt4 book ai didi

haskell - 我的 Haskell 类型同义词有什么问题?

转载 作者:行者123 更新时间:2023-12-02 02:50:07 24 4
gpt4 key购买 nike

我有两个控制循环的函数, continue break :

type Control a = (a -> a) -> a -> a

continue :: Control a
continue = id

break :: Control a
break = const id

然后,我想简化 Control类型同义词。因此,我写道:

type Endo a = a -> a

type Control a = Endo (Endo a)

continue :: Control a
continue = id

break :: Control a
break = const id

然而,当我试图进一步简化它时,我得到了一个错误:

GHCi, version 7.10.2: http://www.haskell.org/ghc/  :? for help
Prelude> type Endo a = a -> a
Prelude> type Duplicate w a = w (w a)
Prelude> type Control a = Duplicate Endo a

<interactive>:4:1:
Type synonym ‘Endo’ should have 1 argument, but has been given none
In the type declaration for ‘Control’

我不明白为什么我会收到这个错误。也许你可以启发我。

最佳答案

正如弗雷泽所说,这种东西通常不能工作,因为类型部分应用类型同义词make everything undecidable .

但是,如果您使用 -XLiberalTypeSynonyms扩展,GHC 将内联同义词,直到它可以推断出:

Prelude> type Endo a = a -> a
Prelude> type Duplicate w a = w (w a)
Prelude> type Control a = Duplicate Endo a

<‌interactive>:4:1:
    Type synonym ‘Endo’ should have 1 argument, but has been given none
    In the type declaration for ‘Control’
Prelude> :set -XLiberalTypeSynonyms
Prelude> type Control a = Duplicate Endo a

关于haskell - 我的 Haskell 类型同义词有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31728077/

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