Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.2k views
in Technique[技术] by (71.8m points)

haskell - Is this a good monoid action?

I think I might have stumbled across a general, albeit somewhat degenerate, monoid action. Pseudo-Haskell:

instance (Monoid m, Monoid n) => Act m n where
    act mempty x = x  -- let's pretend that we can use `mempty` as a pattern
    act _ _ = mempty

m's action on n is to set n to mempty unless m itself is empty.

Is this a law-abiding monoid action? Has it been invented before by someone other than me? If so, what is its name?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It does not look as a monoid action, at least in the general case. If it were, we should have the following properties:

-- law 1
act mempty            x = x
-- law 2
act (m1 <> m2) x = act m1 (act m2 x)

and, because of the way act was defined in the pseudo-instance:

-- property 1
act x y = mempty
   when x /= mempty

Take m and n to be Sum Int, which is a monoid.

We have

act (Sum 0) (Sum 1)
= { definition of mempty }
act mempty (Sum 1)
= { law 1 }
Sum 1

we also have

act (Sum 0) (Sum 1)
= { definition of <> on Sum }
act (Sum (-2) <> Sum 2) (Sum 1)
= { law 2 }
act (Sum (-2)) (act (Sum 2) (Sum 1))
= { property 1, given Sum (-2) /= mempty }
mempty
= { definition of mempty }
Sum 0

leading to two incompatible results.

On the other side, when m is a monoid where no (nontrivial) element has an inverse, e.g. [a], then your act looks like a proper action.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...