Haskellでは、型クラスの制約を満たすM aようにモナドを制限する方法はありますか?a
確率的モデリングの例をF#からHaskellに翻訳しています。ただし、Haskellではに変更されるので省略しましsupportた。この変更により、次のエラーが発生します。data Distribution adata (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です。モナドを持つことができる方法はありますが、このモナドDistributionに制約(Ord a)を強制しますか?私は試した:
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.
したがって、モナドを持つ方法はありますが、 ?などの制約でM a制限します。aOrd a
ありがとう。