[prev in list] [next in list] [prev in thread] [next in thread] 

List:       haskell-cafe
Subject:    Re: [Haskell-cafe] Get number of fields for non-record ADTs using Data type class?
From:       Artem Pelenitsyn <a.pelenitsyn () gmail ! com>
Date:       2018-10-29 22:06:48
Message-ID: CAPKAvQUMV7dzgRQOf3z-bVZ0H_ThWQc=rKisYO48+sMKzWcfhQ () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


Oh, didn't know about `one-liner`. This looks v. nice. Thank you!

-- Artem

On Mon, 29 Oct 2018 at 17:52 Li-yao Xia <lysxia@gmail.com> wrote:

> On 10/29/18 4:42 PM, Artem Pelenitsyn wrote:
> > I don't think there is a point in looking for
> > GHC.Generics-based solution, as Data.Data is the exact match for this
> > kind of problem.
>
> Although GHC.Generics has its shortcomings (usage complexity and compile
> times), I still find it worthwhile to advocate as a statically-typed
> alternative to the dynamically-typed Data.Data for many problems,
> including this one.
>
> Using the one-liner library (which is built around GHC.Generics), the
> equivalent line of code is:
>
>      getSum . gfoldMap @AnyType (const 1) :: T -> Int
>
> Data.Data is more visible mainly because it comes with a lot of
> functionality baked into the standard library, whereas GHC.Generics
> provides only a minimal interface and we have to find everything else in
> separate packages. However, there is no fundamental reason why one is a
> better fit than the other for the task of counting constructor fields.
>
> Li-yao
>
> >
> > On Mon, 29 Oct 2018 at 16:35 Li-yao Xia <lysxia@gmail.com
> > <mailto:lysxia@gmail.com>> wrote:
> >
> >     This maps every field to 1, and folds them together using (+):
> >
> >           Data.Data.gmapQl (+) 0 (const 1) :: T -> Int
> >
> >     (There has to be a similarly easy solution using GHC.Generics instead
> >     but I can't think of one...)
> >
> >     Li-yao
> >
> >     On 10/29/18 2:56 PM, Markus Läll wrote:
> >      > Dear list,
> >      >
> >      > Is it possible te get the number of fields for data constructors
> >     for a
> >      > plain ADT, i.e something with no record fields? E.g for
> >      >
> >      > data T = A Int Double | B String (Maybe String)
> >      >
> >      > it would give 2 for both `A` and `B`.
> >      >
> >      > For a record it's possible using the `constrFields` function from
> >     Data.Data.
> >      >
> >      > I was trying to follow this tutorial by Christopher Done
> >      > https://chrisdone.com/posts/data-typeable, and I feel that it
> >     must be
> >      > possible somehow to get these numbers with the gmap*/gfold*
> >     functions,
> >      > but the use of them is over my head at the moment.
> >      >
> >      >
> >      > Best,
> >      >
> >      >
> >      >
> >      > --
> >      > Markus Läll
> >      >
> >      > _______________________________________________
> >      > 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.
> >
>

[Attachment #5 (text/html)]

<div dir="ltr"><div>Oh, didn&#39;t know about `one-liner`. This looks v. nice. Thank \
you!</div><div><br></div><div>-- Artem<br></div></div><br><div \
class="gmail_quote"><div dir="ltr">On Mon, 29 Oct 2018 at 17:52 Li-yao Xia &lt;<a \
href="mailto:lysxia@gmail.com">lysxia@gmail.com</a>&gt; wrote:<br></div><blockquote \
class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc \
solid;padding-left:1ex">On 10/29/18 4:42 PM, Artem Pelenitsyn wrote:<br> &gt; I \
don&#39;t think there is a point in looking for <br> &gt; GHC.Generics-based  \
solution, as Data.Data is the exact match for this <br> &gt; kind of problem.<br>
<br>
Although GHC.Generics has its shortcomings (usage complexity and compile <br>
times), I still find it worthwhile to advocate as a statically-typed <br>
alternative to the dynamically-typed Data.Data for many problems, <br>
including this one.<br>
<br>
Using the one-liner library (which is built around GHC.Generics), the <br>
equivalent line of code is:<br>
<br>
        getSum . gfoldMap @AnyType (const 1) :: T -&gt; Int<br>
<br>
Data.Data is more visible mainly because it comes with a lot of <br>
functionality baked into the standard library, whereas GHC.Generics <br>
provides only a minimal interface and we have to find everything else in <br>
separate packages. However, there is no fundamental reason why one is a <br>
better fit than the other for the task of counting constructor fields.<br>
<br>
Li-yao<br>
<br>
&gt; <br>
&gt; On Mon, 29 Oct 2018 at 16:35 Li-yao Xia &lt;<a href="mailto:lysxia@gmail.com" \
target="_blank">lysxia@gmail.com</a> <br> &gt; &lt;mailto:<a \
href="mailto:lysxia@gmail.com" target="_blank">lysxia@gmail.com</a>&gt;&gt; \
wrote:<br> &gt; <br>
&gt;        This maps every field to 1, and folds them together using (+):<br>
&gt; <br>
&gt;                 Data.Data.gmapQl (+) 0 (const 1) :: T -&gt; Int<br>
&gt; <br>
&gt;        (There has to be a similarly easy solution using GHC.Generics instead<br>
&gt;        but I can&#39;t think of one...)<br>
&gt; <br>
&gt;        Li-yao<br>
&gt; <br>
&gt;        On 10/29/18 2:56 PM, Markus Läll wrote:<br>
&gt;         &gt; Dear list,<br>
&gt;         &gt;<br>
&gt;         &gt; Is it possible te get the number of fields for data \
constructors<br> &gt;        for a<br>
&gt;         &gt; plain ADT, i.e something with no record fields? E.g for<br>
&gt;         &gt;<br>
&gt;         &gt; data T = A Int Double | B String (Maybe String)<br>
&gt;         &gt;<br>
&gt;         &gt; it would give 2 for both `A` and `B`.<br>
&gt;         &gt;<br>
&gt;         &gt; For a record it&#39;s possible using the `constrFields` function \
from<br> &gt;        Data.Data.<br>
&gt;         &gt;<br>
&gt;         &gt; I was trying to follow this tutorial by Christopher Done<br>
&gt;         &gt; <a href="https://chrisdone.com/posts/data-typeable" \
rel="noreferrer" target="_blank">https://chrisdone.com/posts/data-typeable</a>, and I \
feel that it<br> &gt;        must be<br>
&gt;         &gt; possible somehow to get these numbers with the gmap*/gfold*<br>
&gt;        functions,<br>
&gt;         &gt; but the use of them is over my head at the moment.<br>
&gt;         &gt;<br>
&gt;         &gt;<br>
&gt;         &gt; Best,<br>
&gt;         &gt;<br>
&gt;         &gt;<br>
&gt;         &gt;<br>
&gt;         &gt; --<br>
&gt;         &gt; Markus Läll<br>
&gt;         &gt;<br>
&gt;         &gt; _______________________________________________<br>
&gt;         &gt; Haskell-Cafe mailing list<br>
&gt;         &gt; To (un)subscribe, modify options or view archives go to:<br>
&gt;         &gt; <a \
href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" rel="noreferrer" \
target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe</a><br> \
&gt;         &gt; Only members subscribed via the mailman list are allowed to \
post.<br> &gt;         &gt;<br>
&gt;        _______________________________________________<br>
&gt;        Haskell-Cafe mailing list<br>
&gt;        To (un)subscribe, modify options or view archives go to:<br>
&gt;        <a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" \
rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe</a><br>
 &gt;        Only members subscribed via the mailman list are allowed to post.<br>
&gt; <br>
</blockquote></div>


[Attachment #6 (text/plain)]

_______________________________________________
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.

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic