我試圖拼湊下面的類Domain
及其實例TrivialDomain
型歧義
{-# LANGUAGE TypeFamilies #-}
data Transition = Transition
class Domain d where
type Set d
type Engine d :: * -> *
top :: Engine d (Set d)
-- ...
complement :: Set d -> Engine d (Set d)
exclude :: Set d -> Set d -> Engine d (Set d)
-- ...
data TrivialDomain = TrivialDomain
instance Domain TrivialDomain where
type Set TrivialDomain = [Int]
type Engine TrivialDomain = IO
top = return [0..10]
-- ...
complement a = top >>= (flip exclude) a
exclude a b = return $ filter (not . (`elem` b)) a
-- ...
,但我不斷收到以下錯誤,我不明白
test3.hs:25:21:
Couldn't match type ‘Engine d0’ with ‘IO’
The type variable ‘d0’ is ambiguous
Expected type: IO (Set d0)
Actual type: Engine d0 (Set d0)
In the first argument of ‘(>>=)’, namely ‘top’
In the expression: top >>= (flip exclude) a
test3.hs:25:35:
Couldn't match type ‘Set d1’ with ‘[Int]’
The type variable ‘d1’ is ambiguous
Expected type: Set d0 -> [Int] -> IO [Int]
Actual type: Set d1 -> Set d1 -> Engine d1 (Set d1)
In the first argument of ‘flip’, namely ‘exclude’
In the second argument of ‘(>>=)’, namely ‘(flip exclude) a’
我會期望Engine d (Set d)
在實例聲明中解析爲IO [Int]
,但似乎並非如此。至少GHC不這麼認爲。我錯過了什麼?
關於最後一個例子,你能不能寫在GHC8類級別'頂部@ D'? – jakubdaniel
我認爲我們不能。默認情況下,'forall'綁定的類型變量可以是'@'應用的,但是顯然,我們不能在'Domain'方法類型中寫入'forall d.'。我發現'@'可以在類方法上正常工作,並且可以按類類型參數的順序使用。 –