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

What's wrong with my Haskell type synonym?

我有两个控制循环的函数,continue and 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’

我不明白为什么会出现此错误。也许你能启发我。

必须始终完全应用类型同义词。您不能部分应用它们。

如果您打算这样做,您可能需要重新键入它。

正如 Fraser 所说,这种东西通常无法工作,因为类型部分应用了类型同义词 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