Pointwise Monoid with DerivingVia on n-tuples

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Pointwise Monoid with DerivingVia on n-tuples

Georgi Lyubenov
Hi!

I would like to use `DerivingVia` to do something like this:
```
import Data.Semigroup

data Four = Four Bool (Maybe Int) Bool Bool
  deriving (Semigroup, Monoid) via (Any, Option (First Int), Any, Any)
```

This, however, doesn't work, because `(Any, Option (First Int), Any, Any)` is not coercible to `Four`.

Even if I do
```
data Four a b c d = Four a b c d

type Concrete = Four Bool (Maybe Int) Bool Bool
```
`Concrete` is still not coercible to `(Any, Option (First Int), Any, Any)`.

This seems like a very common thing you would want to do (for me at least it pops up often, to want to have a `Monoid` instance that is just a pointwise Monoid over it's arguments (exactly the way an n-tuple is a `Monoid`). But I don't seem to be finding an easy way to do this. I am aware of `generic-deriving` but `DerivingVia` seems like it should be able to handle something like this, with only `Coercible` holding it back (?).

Thanks in advance,

=======
Georgi

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: Pointwise Monoid with DerivingVia on n-tuples

Joachim Breitner-2
Hi,

Am Mittwoch, den 06.02.2019, 15:39 +0200 schrieb Georgi Lyubenov:
> Hi!
>
> I would like to use `DerivingVia` to do something like this:
> ```
> import Data.Semigroup
>
> data Four = Four Bool (Maybe Int) Bool Bool
>   deriving (Semigroup, Monoid) via (Any, Option (First Int), Any, Any)
> ```

you probably know that, but this works:

   newtype Four =  Four (Bool, Maybe Int, Bool, Bool)
        deriving (Semigroup, Monoid) via (Any, Option (First Int), Any, Any)

   and is operationally equivalent to your data type. You can define a
   pattern synonym to even the the other syntax.

   Cheers,
   Joachim
--
Joachim Breitner
  [hidden email]
  http://www.joachim-breitner.de/


_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Pointwise Monoid with DerivingVia on n-tuples

Matthew Pickering
In reply to this post by Georgi Lyubenov
This question is answered in section 4.3 of the paper.

https://www.kosmikus.org/DerivingVia/deriving-via-paper.pdf

Cheers,

Matt

On Wed, Feb 6, 2019 at 1:40 PM Georgi Lyubenov <[hidden email]> wrote:

>
> Hi!
>
> I would like to use `DerivingVia` to do something like this:
> ```
> import Data.Semigroup
>
> data Four = Four Bool (Maybe Int) Bool Bool
>   deriving (Semigroup, Monoid) via (Any, Option (First Int), Any, Any)
> ```
>
> This, however, doesn't work, because `(Any, Option (First Int), Any, Any)` is not coercible to `Four`.
>
> Even if I do
> ```
> data Four a b c d = Four a b c d
>
> type Concrete = Four Bool (Maybe Int) Bool Bool
> ```
> `Concrete` is still not coercible to `(Any, Option (First Int), Any, Any)`.
>
> This seems like a very common thing you would want to do (for me at least it pops up often, to want to have a `Monoid` instance that is just a pointwise Monoid over it's arguments (exactly the way an n-tuple is a `Monoid`). But I don't seem to be finding an easy way to do this. I am aware of `generic-deriving` but `DerivingVia` seems like it should be able to handle something like this, with only `Coercible` holding it back (?).
>
> Thanks in advance,
>
> =======
> Georgi
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.
_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: Pointwise Monoid with DerivingVia on n-tuples

Li-yao Xia-2

On 2/6/19 10:33 AM, Matthew Pickering wrote:
> This question is answered in section 4.3 of the paper.
>
> https://www.kosmikus.org/DerivingVia/deriving-via-paper.pdf
>

Is there a package containing SameRepAs and other such convenient
newtypes on Hackage or Github?

Cheers,
Li-yao
_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.