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

时间:2015-07-30 15:33:09

标签: haskell ocaml type-inference hindley-milner type-synonyms

我有两个控制循环的函数,continuebreak

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’

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

2 个答案:

答案 0 :(得分:9)

正如弗雷泽所说,这种东西通常不起作用,因为类型部分应用了类型同义词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

答案 1 :(得分:8)

类型同义词必须始终完全应用。你无法部分应用它们。

如果您打算这样做,您可能需要对其进行新打字。