在 Haskell 中,有没有办法限制 monadM a
以a
满足类型类约束?
我正在将概率建模示例从F#转换为Haskell。但是,在 Haskell 中,我省略了,support
因为它会更改data Distribution a
为data (Ord a) => Distribution a
. 通过此更改,我收到以下错误:
...probabilisticModeling.hs:42:13:
Could not deduce (Ord a) from the context ()
arising from a use of `always'
at ...probabilisticModeling.hs:42:13-18
Possible fix:
add (Ord a) to the context of the type signature for `return'
In the expression: always
In the definition of `return': return = always
In the instance declaration for `Monad Distribution'
实际上,always
/的类型return
是:(Ord a) => a -> Distribution a
。有没有办法让我拥有一个 monad Distribution
,但强制限制(Ord a)
这个 monad?我试过:
instance Monad Distribution where
(>>=) = bind
return :: (Ord a) => a -> Distribution a = always
但我得到了错误:
...probabilisticModeling2.hs:48:4:
Pattern bindings (except simple variables) not allowed in instance declarations
return :: (Ord a) => a -> Distribution a = always
Failed, modules loaded: none.
所以它有一种方法可以有一个 monad M a
,但是a
用一个约束来限制Ord a
?
谢谢。