设计一元类型
我需要一些帮助来设计一个单子数据类型,我似乎很难理解这个想法,但我非常清楚我想要什么。在这方面,只有类型检查器似乎需要一些说服力。
我想要的是一些可以是 monad 的数据类型Primitive
。其功能基本上是绘制基元,我希望能够将它们组合起来,并且组合也是基元,就像库 parsec
中的 Parser
类型一样。但实际的绘制必须使用IO
monad 来完成。
顺便说一句,我还有一个类 Drawable a
,如下所示:
class Drawable a where
draw :: (MonadIO m) => Pos -> a -> m ()
并且我当然希望每个 Primitive 都是可绘制的。感谢您的任何意见。
I'd need some help to design a monadic datatype, I seem to have trouble wrapping my head around the idea, but I pretty definitely know what I want. Only the type checker seems to require some persuading in that matter.
What I want is some datatype Primitive
that can be a monad. Functions of that are basically drawing primitives, and I want to be able to combine them and the combinations being primitives as well, much like the Parser
type in the library parsec
. But the actual drawing has to be done with an IO
monad.
Incidentally, I also have a class Drawable a
like this:
class Drawable a where
draw :: (MonadIO m) => Pos -> a -> m ()
And I want every Primitive of course to be drawable. Am thankful for any input.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不明白为什么你认为这应该是一个单子。它有什么一元的?
你应该先设计你想要的API,然后逐步思考如何抽象它。
顺便说一句,Brent 的图表库有一个非常好的组合 API,可以提供一些好主意: http://byorgey.wordpress.com/2008/04/30/new-haskell-diagrams-library/
I don't understand why you think this should be a monad. What's monadic about it?
You should design the API you want first, and incrementally think about how to abstract it.
Incidentally, Brent's diagrams library has a very nice compositional API that could provide some good ideas: http://byorgey.wordpress.com/2008/04/30/new-haskell-diagrams-library/