[英]GHC do not resolve correctly constraint in typeclass methods (ghc bug / limitation ?)
GHC在某些類成員的默認實現中不解析/傳播約束。 這種行為真的很奇怪,在我看來,這是一個錯誤。
有人可以幫我/向我解釋什么地方嗎?
a
從類聲明和a
從類方法,這樣的定義是沒有歧義 碼:
module Foo where
import Data.Proxy
data Stuff a = Stuff
{content :: String}
class HasStuff a where
stuff :: Stuff a
-- This works
useStuffOK :: Proxy a -> (Stuff a)
useStuffOK _ = (stuff)
-- those don't work,
-- (but I think ghc has all the information necessary to figure it out)
useStuffBAD :: Proxy a -> (Stuff a, String)
useStuffBAD _ = (stuff, content (stuff :: Stuff a))
-- Could not deduce (HasStuff a1) arising from a use of ‘stuff’
-- from the context (HasStuff a)
-- bound by the class declaration for ‘HasStuff’
-- at Test.hs:(7,1)-(17,45)
useStuffBAD2 :: Proxy a -> String
useStuffBAD2 _ = content (stuff :: Stuff a)
-- Could not deduce (HasStuff a1) arising from a use of ‘stuff’
-- from the context (HasStuff a)
-- bound by the class declaration for ‘HasStuff’
-- at Test.hs:(7,1)-(17,45)
instance HasStuff Int where
stuff = Stuff "ok"
-- inference works here
x :: Stuff Int
x = stuff
-- works here too
x :: String
x = content (stuff :: Stuff Int)
謝謝
正如@epsilonhalbe和@ user2407038指出的那樣,我缺少ScopedTypeVariables擴展
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.