我发现 Haskell 的一个奇怪的特性让我相信我的思考方式是错误的。我认为在Haskell中,应该有一些“不存在的”单子。这是因为以下原因。
Prelude> return 1
1
Prelude> return 1 >>= \x -> if even x then return True else return False
False
>>=
属于类型m a -> (a -> m b) -> m b
where m
可以是任何单子。我的理论是这样的:因为return 1
评估为仅仅1
, return 1
可以被认为是1
提升到包裹在 a 内的值“不存在的”单子。 Haskell 捕获了这个事实并评估了整个表达式
return 1 >>= \x -> if even x then return True else return False
to False
因为它必须产生一个“不存在的False
"这只是一个False
.
不过,我从来没有听说过这样的事情“不存在的”之前的单子。
我确信我的理论是错误的,因为如果“不存在的1
"只是1
,下面的表达式必须是类型一致的,但事实并非如此。它是上述版本的修改版本。
1 >>= \x -> if even x then return True else return False
<interactive>:57:1: error:
• Could not deduce (Integral a0)
from the context: (Monad m, Integral a, Num (m a))
bound by the inferred type for ‘it’:
forall (m :: * -> *) a. (Monad m, Integral a, Num (m a)) => m Bool
at <interactive>:57:1-56
The type variable ‘a0’ is ambiguous
• In the ambiguity check for the inferred type for ‘it’
To defer the ambiguity check to use sites, enable AllowAmbiguousTypes
When checking the inferred type
it :: forall (m :: * -> *) a.
(Monad m, Integral a, Num (m a)) =>
m Bool
所以,我的理论一定是有矛盾的。我缺少什么?