更多 rmonad 库?
我想用 RMonad
做一些基本的事情。有没有办法使用“as monad”功能来
- 拥有身份 rmonad,以应用 monad 转换器?
- 有诸如 StateT 变压器之类的常见东西吗?
- 向现有的 monad 添加约束? (例如,如果有人想要一个具有附加约束的
StateT
)
不幸的是,我还没有掌握诸如数据族等使其工作的东西......否则我可能会很乐意编写代码我。
编辑
我从库源中将 StateT
一起破解,看看它是否有效...
I want to do some rudimentary things with RMonad
. Are there ways of using the "as monad" functionality to
- have an identity rmonad, to apply monad transformers to?
- have common things like
StateT
transformers? - add a constraint to an existing monad? (e.g. if one wanted a
StateT
with additional constraints)
unfortunately I haven't yet grasped things like data families, etc. that make it work ... otherwise I'd probably be happy to write the code myself.
edit
I hacked StateT
together from library sources, will see if it works ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
快速浏览后,您的 StateT 版本看起来是正确的。不幸的是,使用 RMonad 等。等人。 确实需要复制几乎所有内容;我开始使用 Suit 编写一些代码,但放弃了,因为它涉及太多重复(最终我并不真正需要它)。
编辑:
根据我对
Suitable
工作方式的记忆,它是这样的:考虑
Functor
类:Set
可以不是它的实例,因为它需要额外的Ord
约束。一个简单的方法是这样的:
但这会返回以下错误:
这是为什么?这是因为约束没有在实例级别保留或找到,因此 GHC 没有意识到使用
myfmap
时,应该对有一个
。Ord
约束b.Suitable
类型用于显式创建一个约束字典,它允许您指定这些类型的约束:(其中
SetConstraints
已在Data.Suitable
中定义code>)这个实现是有效的,但它确实使类型签名更加复杂,并且方法实现更加复杂(请注意,我们需要引入
SetConstraints
两次:一次为一个为Set a
,另一个为Set b
)。请注意,对于没有任何约束的类型,不需要任何Suitable
约束函数。我已经开始尝试在 Typeclassopedia 中创建类的
合适
版本,但由于实例变得相当复杂而放弃了。Your version of StateT looks correct after a quick glance. Unfortunately, using RMonad et. al. does require duplicating just about everything; I started doing writing some code using Suitable and gave up because it involved too much duplication (and in the end I didn't really need it).
Edit:
From my memory of how
Suitable
works, it's something like this:Consider the
Functor
class:Set
can't be an instance of it because it needs the extraOrd
constraint.A naive approach would be something like:
But this then returns the following error:
Why is this? It's because the constraint isn't kept or found at the instance level, so GHC doesn't realise that when using
myfmap
, there should be anOrd
constraint onb
.The
Suitable
type is used to explicitly create a constraint dictionary which allows you to specify these kinds of constraints:(where
SetConstraints
is already defined inData.Suitable
)This implementation works, but it does make type signatures a bit more convoluted, and method implementations a bit hairier (note here that we need to bring in
SetConstraints
twice: once for theSet a
, the other for theSet b
). Note that for types without any constraints, none of theSuitable
constraint functions are needed.I had started to try and create
Suitable
versions of the classes in the Typeclassopedia but gave up because the instances were getting rather hairy.