From 47db5d763f4fbfef6540352eb6b84279cc51e26e Mon Sep 17 00:00:00 2001 From: crumbtoo Date: Fri, 5 Jan 2024 10:15:40 -0700 Subject: [PATCH 1/4] KEndo monoid --- src/Data/Monoid/Extra.hs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Data/Monoid/Extra.hs b/src/Data/Monoid/Extra.hs index 8f1a763..e3eada4 100644 --- a/src/Data/Monoid/Extra.hs +++ b/src/Data/Monoid/Extra.hs @@ -3,10 +3,14 @@ module Data.Monoid.Extra( module Data.Monoid, -- * Extra operations - mwhen + mwhen, + + -- * Kleisli endomorphisms + KEndo(KEndo, appKEndo) ) where import Data.Monoid +import Control.Monad -- | Like 'Control.Monad.when', but operating on a 'Monoid'. If the first argument -- is 'True' returns the second, otherwise returns 'mempty'. @@ -15,3 +19,16 @@ import Data.Monoid -- > mwhen False "test" == "" mwhen :: Monoid a => Bool -> a -> a mwhen b x = if b then x else mempty + +-- | 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 } + +instance (Monad m) => Semigroup (KEndo m a) where + -- | Left-to-right composition + KEndo f <> KEndo g = KEndo (f >=> g) + +instance (Monad m) => Monoid (KEndo m a) where + mempty = KEndo pure + From d0c7be5eabed346b47273c3f89afd4da02bc112d Mon Sep 17 00:00:00 2001 From: crumbtoo Date: Mon, 15 Jan 2024 21:55:11 -0700 Subject: [PATCH 2/4] comment --- src/Data/Monoid/Extra.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Data/Monoid/Extra.hs b/src/Data/Monoid/Extra.hs index e3eada4..8f42069 100644 --- a/src/Data/Monoid/Extra.hs +++ b/src/Data/Monoid/Extra.hs @@ -21,7 +21,7 @@ mwhen :: Monoid a => Bool -> a -> a mwhen b x = if b then x else mempty -- | The intersection of 'Data.Monoid.Endo' and 'Control.Arrow.Kleisli'. This --- type provides a monoid instance for composition of monadic actions +-- type provides a 'Monoid' instance for composition of monadic actions -- @a -> m a@. newtype KEndo m a = KEndo { appKEndo :: a -> m a } From 772051a6a988a77f0ab06b8a87aae5a5ac992873 Mon Sep 17 00:00:00 2001 From: Sadie Sorceress Date: Wed, 15 May 2024 07:31:59 -0600 Subject: [PATCH 3/4] s/<==> --- src/Data/Monoid/Extra.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Data/Monoid/Extra.hs b/src/Data/Monoid/Extra.hs index 8f42069..5b758e4 100644 --- a/src/Data/Monoid/Extra.hs +++ b/src/Data/Monoid/Extra.hs @@ -27,7 +27,7 @@ newtype KEndo m a = KEndo { appKEndo :: a -> m a } instance (Monad m) => Semigroup (KEndo m a) where -- | Left-to-right composition - KEndo f <> KEndo g = KEndo (f >=> g) + KEndo f <> KEndo g = KEndo (f <=< g) instance (Monad m) => Monoid (KEndo m a) where mempty = KEndo pure From aa1669a41cf57e5635cbb19d3893a4368d558693 Mon Sep 17 00:00:00 2001 From: Sadie Sorceress Date: Wed, 15 May 2024 07:32:53 -0600 Subject: [PATCH 4/4] comment --- src/Data/Monoid/Extra.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Data/Monoid/Extra.hs b/src/Data/Monoid/Extra.hs index 5b758e4..d04e1b3 100644 --- a/src/Data/Monoid/Extra.hs +++ b/src/Data/Monoid/Extra.hs @@ -26,7 +26,7 @@ mwhen b x = if b then x else mempty newtype KEndo m a = KEndo { appKEndo :: a -> m a } instance (Monad m) => Semigroup (KEndo m a) where - -- | Left-to-right composition + -- | Right-to-left composition KEndo f <> KEndo g = KEndo (f <=< g) instance (Monad m) => Monoid (KEndo m a) where