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

Aad*_*hah 4 ocaml haskell type-inference hindley-milner type-synonyms

我有两个用于控制循环的函数,continue并且break:

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

continue :: Control a
continue = id

break :: Control a
break = const id
Run Code Online (Sandbox Code Playgroud)

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

type Endo a = a -> a

type Control a = Endo (Endo a)

continue :: Control a
continue = id

break :: Control a
break = const id
Run Code Online (Sandbox Code Playgroud)

但是,当我试图进一步简化它时,我收到一个错误:

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’
Run Code Online (Sandbox Code Playgroud)

我不明白为什么我会收到这个错误.也许你可以开导我.

lef*_*out 10

正如弗雷泽所说,这种东西通常不能起作用,因为类型部分应用的类型同义词使一切都不可判定.

但是,如果您放入-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
Run Code Online (Sandbox Code Playgroud)


Fra*_*ser 8

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

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

  • 这个。如果`Endo` 是`data` 或`newtype`,`Duplicate Endo a` 是有效的,但如果它只是一个`type`,则*不*。 (2认同)