【问题标题】:What's wrong with my Haskell type synonym?我的 Haskell 类型同义词有什么问题?
【发布时间】:2015-07-30 15:33:09
【问题描述】:

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

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

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 这个。如果Endodatanewtype,则Duplicate Endo a 有效,但如果它只是type,则不是
      • 请参阅herehere 了解更多信息。要点是对部分应用的同义词的推断是不可判定的
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-16
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      • 2021-12-09
      相关资源
      最近更新 更多