我有兩個我認為是等效型別類和實體定義的實作。PureScript 版本構建沒有錯誤,但 Haskell 版本失敗并顯示錯誤Un-determined variables: e, f。
我可以在 Haskell 中完成這項作業嗎?
哈斯克爾:
class Foo a b c | a b -> c where
newtype Bar a b = Bar (a, b)
instance (Foo a c e, Foo b d f) => Foo (Bar a b) (Bar c d) (Bar e f) where
純腳本:
class Foo a b c | a b -> c
newtype Bar a b = Bar (Tuple a b)
instance (Foo a c e, Foo b d f) => Foo (Bar a b) (Bar c d) (Bar e f)
uj5u.com熱心網友回復:
您必須啟用UndecidableInstances;那么您的宣告將起作用。為什么需要這樣做有點微妙,即如何不啟用它會導致型別檢查器中的回圈。但是假設在您的程式中,您以某種方式導致需要解決以下約束:
Foo (Bar a b) (Bar c d) a
約束求解器會很高興地觀察到我們必須選擇您撰寫的實體,并因此決定a ~ Bar a0 a1對于 somea0和a1,這意味著我們要解決約束:
Foo (Bar (Bar a0 a1) b) (Bar c d) (Bar a0 a1)
現在我們可以分派到背景關系,這意味著我們現在需要解決以下兩個約束:
( Foo (Bar a0 a1) c a0
, Foo b d a1
)
可是等等!第一個約束與我們開始的約束形式相同,但具有不同的型別變數。一個回圈!
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/347700.html
上一篇:Haskell中的引數化型別
