在下面的代码(ideone link),为什么线(1)当行(4)编译没有问题(ideone link with line (1) commented)失败:不寻常的暧昧错误
data DataOrError a b = Error a | Data b deriving Show
apply f (Data x) (Data y) = Data (f x y)
apply f (Data x) [email protected](Error _) = y
apply f [email protected](Error _) _ = x
main = do
print (apply (+) x1 x2) -- (1)
print (apply (+) x1 e2) -- (2)
print (apply (+) e1 x2) -- (3)
print (apply (+) e1 e2) -- (4)
where
x1 = Data (2 :: Int)
x2 = Data (3 :: Int)
e1 = Error ("First thing failed")
e2 = Error ("Second thing failed")
我知道DataOrError
基本上是Either
,这只是为了说明。
的错误是:
prog.hs:8:3:
No instance for (Show a0) arising from a use of `print'
The type variable `a0' is ambiguous
Possible fix: add a type signature that fixes these type variable(s)
Note: there are several potential instances:
instance (Show a, Show b) => Show (DataOrError a b)
-- Defined at prog.hs:1:50
instance Show Double -- Defined in `GHC.Float'
instance Show Float -- Defined in `GHC.Float'
...plus 24 others
In a stmt of a 'do' block: print (apply (+) x1 x2)
In the expression:
do { print (apply (+) x1 x2);
print (apply (+) x1 e2);
print (apply (+) e1 x2);
print (apply (+) e1 e2) }
In an equation for `main':
main
= do { print (apply (+) x1 x2);
print (apply (+) x1 e2);
print (apply (+) e1 x2);
.... }
where
x1 = Data (2 :: Int)
x2 = Data (3 :: Int)
e1 = Error ("First thing failed")
e2 = Error ("Second thing failed")
什么是错误? – bheklilr 2014-11-20 21:26:13
现在包含错误和ideone链接 – Clinton 2014-11-20 21:31:54