我试图拼凑下面的类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.'。我发现'@'可以在类方法上正常工作,并且可以按类类型参数的顺序使用。 –