我想使用一個typeclass將字典傳遞到一個函數,其中該字典的值由函數的兩個類型參數的類型唯一指定。我該如何改變這種說法,體型應該等於參數類型?
當我編譯此代碼時,我在評論中收到錯誤消息。
如何修改此代碼以避免此歧義?我希望身體中的a和b類型與形式參數中的a和b類型相同。
{-# LANGUAGE MultiParamTypeClasses#-}
class C a b where
a :: a
b :: b
f :: C a b => a -> b
f a = b
-- $ runhaskell toy2.hs
-- toy2.hs:8:7:
-- Could not deduce (C a0 b) arising from a use of ‘b’
-- from the context (C a b)
-- bound by the type signature for f :: C a b => a -> b
-- at toy2.hs:7:6-20
-- The type variable ‘a0’ is ambiguous
-- Relevant bindings include f :: a -> b (bound at toy2.hs:8:1)
-- In the expression: b
-- In an equation for ‘f’: f a = b
然後使用會像
instance C String Double where
a = "foo"
b = 42.0
和
f "bar" = 42.0
他們已經是你想要的類型。麻煩的是,沒有辦法使用'b',因爲它的類型沒有提及'a'。 – melpomene
理想情況下,如果使用了足夠的擴展名,比如'AllowAmbiguousTypes,TypeArguments,ScopedTypeVariables','b @ a @ b'可以工作。不過,我現在無法測試。 – chi
你能澄清你想完成什麼嗎?假設你有'instance C String Double where a =「foo」; b = 42.0'。你期望'f'bar''產生什麼? – user2297560