目的是使类型类约束代码更清晰。
type CanThrowDice = (Monad m, MonadIO m, Random a)
throwDice :: CanThrowDice m a => (a, a) -> m a
throwDice (r1, r2) = ...
代替
throwDice :: (Monad m, MonadIO m, Random a) => (a, a) -> m a
throwDice (r1, r2) = ...
我记得在什么地方见过这个用法,但想不起来了。上面的代码警告我添加一些额外的pragma,如ConstraintKinds
,我不能使它工作。
唯一缺少的是类型参数m
和a
以及ConstraintKinds
语言pragma [ghc-doc]:
{-# LANGUAGEConstraintKinds#-}
-- ↓ ↓ type parameters
type CanThrowDicema= (Monad m, MonadIO m, Random a)
throwDice :: CanThrowDice m a => (a, a) -> m a
throwDice (r1, r2) = …