如果推斷的型別包含在代碼中,則編譯cabal repl甚至指示特定型別應該是什么的函式 ( rel) 會產生錯誤。
coords2faces :: (MorphsHQ rel, Eq rel, Eq obj, Ord obj) => State (Store obj rel) [(obj, obj)]
coords2faces = do
f <- rel3 (hqFace :: rel)
return f
在這種情況下如何在代碼中包含型別?
錯誤資訊是
app/TheMain.hs:107:16: error:
? Could not deduce (MorphsHQ rel1) arising from a use of ‘hqFace’
from the context: (MorphsHQ rel, Eq rel, Ord obj)
bound by the type signature for:
coords2faces :: forall rel obj.
(MorphsHQ rel, Eq rel, Eq obj, Ord obj) =>
State (Store obj rel) [(obj, obj)]
at app/TheMain.hs:105:1-98
Possible fix:
add (MorphsHQ rel1) to the context of
an expression type signature:
forall rel1. rel1
? In the first argument of ‘rel3’, namely ‘(hqFace :: rel)’
In a stmt of a 'do' block: f <- rel3 (hqFace :: rel)
In the expression:
do f <- rel3 (hqFace :: rel)
return f
|
107 | f <- rel3 (hqFace :: rel)
uj5u.com熱心網友回復:
這是因為rel宣告的型別中coords2faces的 與rel宣告的型別中的不同hqFace。為了使這一點更明顯,您可以看到編譯器rel1在錯誤訊息中將型別變數重命名為。默認情況下,單獨簽名中的型別變數始終是獨立的。
為了使代碼編譯,您可以洗掉型別簽名hqFace,
coords2faces :: (MorphsHQ rel, Eq rel, Eq obj, Ord obj) => State (Store obj rel) [(obj, obj)]
coords2faces = do
f <- rel3 hqFace
return f
或者您可以啟用ScopedTypeVariables語言擴展并rel在 a 中宣告型別變數forall以指定范圍。
{-# LANGUAGE ScopedTypeVariables #-}
coords2faces :: forall rel obj. (MorphsHQ rel, Eq rel, Eq obj, Ord obj) => State (Store obj rel) [(obj, obj)]
coords2faces = do
f <- rel3 (hqFace :: rel)
return f
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/506262.html
標籤:哈斯克尔
下一篇:Winui3,無法切換字體
