Restricting a monad to a type class

前端 未结 3 1915
醉梦人生
醉梦人生 2020-12-09 18:18

In Haskell, is there a way to restrict a monad M a so that a satisfy a type class constraint?

I am translating the probabilistic modeling e

3条回答
  •  南方客
    南方客 (楼主)
    2020-12-09 18:46

    My understanding of this is that you simply cannot, because a monad is meant to be generalized over all types, not some restricted subset of types such as (Ord a).

    Instead of restricting the monadic type M a, you can simply restrict functions which use that monadic type, e.g.,

    foo :: Ord a => Int -> M a

    In fact, it is preferable to keep types as general as possible and use type classes only to restrict functions.

    etc.

提交回复
热议问题