Skip to content

Semigroup and Monoid overhaul#381

Merged
tbagrel1 merged 1 commit intomasterfrom
tbagrel1/semigroup-monoid-linear-instances
Feb 23, 2022
Merged

Semigroup and Monoid overhaul#381
tbagrel1 merged 1 commit intomasterfrom
tbagrel1/semigroup-monoid-linear-instances

Conversation

@tbagrel1
Copy link
Copy Markdown
Member

@tbagrel1 tbagrel1 commented Feb 15, 2022

Closes #379. Closes #372.

@tbagrel1 tbagrel1 requested a review from aspiwack February 15, 2022 09:49
import qualified Control.Monad.Trans.Reader as NonLinear
import Data.Functor.Compose
import Data.Functor.Const
import Data.Functor.Const (Const (..))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

When there is a conflict, I'd rather us use hiding (conflicting thing). It's more robust.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

There seems to be no conflict after all

@tbagrel1 tbagrel1 requested a review from aspiwack February 23, 2022 08:51
@tbagrel1
Copy link
Copy Markdown
Member Author

Note for #377 #393 : Control.Optics.Linear.lengthOf signature changed (moving from Adding to Sum)

Comment on lines +132 to +140
-- 'transfer' was
-- > transfer (Pull.Array f n) =
-- > Push.Array (\k -> NonLinear.foldMap' (\x -> k (f x)) [0 .. (n - 1)])
-- but 'Linear.Monoid' no longer implies 'NonLinear.Monoid'. So we can have
-- @mempty :: a@ and @(<>) :: a -> a -> a@ (by degrading 'Linear.<>'), but we
-- no longer have the 'NonLinear.Monoid' instance required to use
-- 'NonLinear.foldMap\''. As a result, we just expand 'foldMap\'' to its
-- definition in terms of 'foldl\'', which doesn't require 'NonLinear.Monoid':
-- > foldMap' f' = foldl' (\acc a -> acc <> f' a) mempty
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

As a side note we still have that linear monoids have a corresponding classical monoid.

So we could have

newtype Linear a = Linear a

instance Semigroup a => Linear.Semigroup (Linear a) where
  x <> y = x <>  y
instance Monoid a => Linear.Monoid (Linear a) where
  mempty = mempty

Then you could in principle do

case NonLinear.Foldmap' (\x -> Linear (k (f x)) [0 .. (n - 1)] of
  Linear r -> r

It's not better than a simple foldl', but it's worth being aware of the possibility (in an ideal world you would be able to use coerce, but coerce isn't linear).


There is also the option of using a linear foldMap', but we don't have it available until we solve the Foldable issue (e.g. #190 ).

@tbagrel1 tbagrel1 force-pushed the tbagrel1/semigroup-monoid-linear-instances branch 3 times, most recently from 67033b9 to 925d96f Compare February 23, 2022 13:47
+ Break relationship between `Data.Monoid.Linear.{Semigroup,Monoid} and their
non-linear counterparts (see #379)
+ Add as many instances as possible for these classes (to replicate what is done
  in `base`
@tbagrel1 tbagrel1 force-pushed the tbagrel1/semigroup-monoid-linear-instances branch from 925d96f to d131277 Compare February 23, 2022 14:01
@tbagrel1 tbagrel1 merged commit 79db295 into master Feb 23, 2022
@tbagrel1 tbagrel1 deleted the tbagrel1/semigroup-monoid-linear-instances branch February 23, 2022 14:24
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.

Dependency between some linear classes and their non-linear counterpart (e.g. Semigroup) Semigroup and Monoid: clean-up and improvements

2 participants