是否可以创建一个计算指令数量的Monad? [英] Is it possible to create a Monad that count the number of instructions?
问题描述
考虑到monad,我想到了monad作为打破冯·诺依曼(Von Neumann)体系结构的方式的想法.冯·诺依曼体系结构使用一组指令(称为程序)来更改内存中的数据,并且该程序的每条指令的执行都会更新程序计数器,以了解下一条指令将要执行.
Thinking about monad, it came to me the idea of a monad as the way to break with the Von Neumann architecture. The Von Neumann architecture uses a set of instructions (called program) to change the data in memory and the execution of each instruction of the program updates a program counter to know whom instruction is the next to execute.
如果我们将冯·诺依曼(Von Neumann)架构视为monad,则绑定运算符(>> =)将更新程序计数器.我们可以制造出打破冯·诺依曼(Von Neumann)架构的Monad,从而在绑定中做更多的事情.例如,我们可以有一个Monad来计数程序中执行的指令数.
If we think about the Von Neumann architecture as a monad, the bind operator (>>=) update the program counter. We can make a Monad that break Von Neumann architecture to do more in the bind. As an example, we can have a Monad that count the number of instructions executed in our programs.
但是,当我尝试在haskell中将Monad实现为:
But, when I tried to implement that Monad in haskell as:
data Counter a = Counter Integer a
deriving( Show )
instance Monad Counter where
(Counter n1 a) >>= f = let Counter _ b = f a
in (Counter (n1+1) b)
return a = Counter 1 a
我注意到这将违反Monads法律,例如:
I notice it'll break de Monads laws, e.g:
return x >>= f /= f x
do
a <- return 3
return a
do
return 3
由于monad定律,这两个块是相同的,但是由于它们具有不同数量的指令(句子),它们将返回不同的内容
The two blocks are the same because the monad laws, but they'll return something different because they have different number of instructions (sentences)
我做错什么了吗?还是不可能有这种单子?
Do I made something wrong? or Is it not possible to have such Monad?
推荐答案
严格来说,任何此类单子"都违反了单子法则,因此...不是单子.有关详细信息,请参见此上一个问题.换句话说-您的猜测是正确的,不可能有这样的单子.
Strictly speaking, any such "monad" breaks the monad laws and is thus... not a monad. See this previous question for details. In other words - your guess is correct, it is not possible to have such a monad.
这篇关于是否可以创建一个计算指令数量的Monad?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!