Skip to content

Monadic Endo#110

Open
crumbtoo wants to merge 5 commits into
ndmitchell:masterfrom
crumbtoo:master
Open

Monadic Endo#110
crumbtoo wants to merge 5 commits into
ndmitchell:masterfrom
crumbtoo:master

Conversation

@crumbtoo
Copy link
Copy Markdown

Hello! This adds a very simple semigroup/monoid wrapper for functions a -> m a I find myself using often. Hoping someone has a better name than KEndo...

@ndmitchell
Copy link
Copy Markdown
Owner

Yeah, I don't love KEndo. Why the K? Why not MEndo (which I still don't love)?

@crumbtoo
Copy link
Copy Markdown
Author

Why the K?

For the resemblance to Kleisli!

I've consulted the more creative; Any particularly strong feelings towards {EndoM, EndoKleisli, KleisliEndo}? EndoM is the most sensible in my opinion, following existing conventions.

@crumbtoo
Copy link
Copy Markdown
Author

(Those more creative people have suggested matching Endo's composition order)

Comment thread src/Data/Monoid/Extra.hs
-- | The intersection of 'Data.Monoid.Endo' and 'Control.Arrow.Kleisli'. This
-- type provides a 'Monoid' instance for composition of monadic actions
-- @a -> m a@.
newtype KEndo m a = KEndo { appKEndo :: a -> m a }
Copy link
Copy Markdown
Contributor

@Bodigrim Bodigrim Jul 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be generalized to

newtype Endomorphism k a = Endomorphism (k a a)

instance Category k => Semigroup (Endomorphism k a) where 
  (<>) = coerce (Control.Category..)

instance Category k => Monoid (Endomorphism k a) where 
  mempty = coerce Control.Category.id

Then Endomorphism k a would cover both usual Endo (when k ~ (->)) and monadic endo (when k ~ Kleisli).

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a great idea. That said, once we have gone this far, I wonder if extra is the right place for it? It seems to be more categorical and less compose a few obvious things?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants