5

我有两个用于控制循环的功能,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’

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

4

2 回答 2

13

正如弗雷泽所说,这种东西通常不能工作,因为类型部分应用的类型同义词使一切都无法确定。

但是,如果您插入-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
于 2015-07-30T15:53:23.310 回答
9

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

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

于 2015-07-30T15:34:35.477 回答