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

List:       haskell-cafe
Subject:    Re: [Haskell-cafe] Default "type" in type class
From:       Erik Hesselink <hesselink () gmail ! com>
Date:       2019-09-16 9:05:36
Message-ID: CAPeieQHzDEfuMgOdWKk_EJ1QPPqb-hWiWcth0QpVc1rEQe2WNg () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


You can also define a default type family instance to the class declaration:

class HasToList collection where
  type Requirement collection element :: Constraint
  type Requirement collection element = ()

Regards,

Erik

On Mon, 16 Sep 2019 at 04:02, Hilco Wijbenga <hilco.wijbenga@gmail.com>
wrote:

> On Sun, Sep 15, 2019 at 5:28 PM Hilco Wijbenga <hilco.wijbenga@gmail.com>
> wrote:
> >
> > Hi all,
> >
> > I'm trying to create a "HasToList" type class. It starts off really
> simple:
> >
> > class HasToList collection where
> >         toList ∷ collection element → [element]
> >
> > instance HasToList Set where
> >         toList = Data.Set.toList
> >
> > Unfortunately, this breaks down with Data.EnumSet.EnumSet. So
> >
> > instance HasToList EnumSet where
> >         toList = Data.EnumSet.toList
> >
> > doesn't compile and I could not find a way to add "Enum a =>" to
> > either the instance or "toList". (Well, not without making the
> > compiler upset.)
> >
> > With the aid of ConstraintKinds and TypeFamilies (and not much
> > understanding on my end, obviously) I created
> >
> > class HasToList collection where
> >         type Requirement collection element :: Constraint
> >         toList ∷ Requirement collection element => collection element
> > → [element]
> >
> > instance HasToList Set where
> >         type Requirement Set a = ()
> >         toList = Data.Set.toList
> >
> > instance HasToList EnumSet where
> >         type Requirement EnumSet a = Enum a
> >         toList = Data.EnumSet.toList
> >
> > This works but now I have to add the constraint to every instance even
> > though only a tiny minority actually need it. Is there a way to add a
> > default "type implementation"?
> >
> > I tried the obvious
> >
> > type Requirement collection element :: Constraint = ()
> >
> > and
> >
> > type Requirement collection element :: Constraint
> > type Requirement collection element :: Constraint = ()
> >
> > but neither compiles.
> >
> > Another problem is that the compiler can't compile something like this:
> >
> > isteps ∷ HasToList collection ⇒ collection String → [(Int, String)]
> > isteps steps = Data.List.Index.indexed (toList steps)
> >
> > because it doesn't know which constraint to apply ("Could not deduce:
> > Requirement hasToList String"). That seems fair but also irrelevant at
> > this point.
> >
> > I feel like this is all far too complicated for what I'm trying to
> > accomplish. :-) While I would like to have an answer to the questions
> > above, I guess what I really want is a simpler way to implement
> > HasToList.
> >
> > Cheers,
> > Hilco
>
> This:
>
> class HasToList collection element where
>         toList ∷ collection element -> [element]
>
> instance HasToList Set element where
>         toList = Data.Set.toList
>
> instance Enum element => HasToList EnumSet where
>         toList = Data.EnumSet.toList
>
> compiles fine, of course.
>
> Strange, I thought I had tried that.
> _______________________________________________
> 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>You can also define a default type family instance to the class \
declaration:</div><div><br></div><div>class HasToList collection where<br>   type \
Requirement collection element :: Constraint<br>   type Requirement collection \
element = ()</div><div><br></div><div>Regards,</div><div><br></div><div>Erik<br></div></div><br><div \
class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, 16 Sep 2019 at 04:02, \
Hilco Wijbenga &lt;<a \
href="mailto:hilco.wijbenga@gmail.com">hilco.wijbenga@gmail.com</a>&gt; \
wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px \
0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On Sun, Sep 15, 2019 \
at 5:28 PM Hilco Wijbenga &lt;<a href="mailto:hilco.wijbenga@gmail.com" \
target="_blank">hilco.wijbenga@gmail.com</a>&gt; wrote:<br> &gt;<br>
&gt; Hi all,<br>
&gt;<br>
&gt; I&#39;m trying to create a &quot;HasToList&quot; type class. It starts off \
really simple:<br> &gt;<br>
&gt; class HasToList collection where<br>
&gt;              toList ∷ collection element → [element]<br>
&gt;<br>
&gt; instance HasToList Set where<br>
&gt;              toList = Data.Set.toList<br>
&gt;<br>
&gt; Unfortunately, this breaks down with Data.EnumSet.EnumSet. So<br>
&gt;<br>
&gt; instance HasToList EnumSet where<br>
&gt;              toList = Data.EnumSet.toList<br>
&gt;<br>
&gt; doesn&#39;t compile and I could not find a way to add &quot;Enum a =&gt;&quot; \
to<br> &gt; either the instance or &quot;toList&quot;. (Well, not without making \
the<br> &gt; compiler upset.)<br>
&gt;<br>
&gt; With the aid of ConstraintKinds and TypeFamilies (and not much<br>
&gt; understanding on my end, obviously) I created<br>
&gt;<br>
&gt; class HasToList collection where<br>
&gt;              type Requirement collection element :: Constraint<br>
&gt;              toList ∷ Requirement collection element =&gt; collection \
element<br> &gt; → [element]<br>
&gt;<br>
&gt; instance HasToList Set where<br>
&gt;              type Requirement Set a = ()<br>
&gt;              toList = Data.Set.toList<br>
&gt;<br>
&gt; instance HasToList EnumSet where<br>
&gt;              type Requirement EnumSet a = Enum a<br>
&gt;              toList = Data.EnumSet.toList<br>
&gt;<br>
&gt; This works but now I have to add the constraint to every instance even<br>
&gt; though only a tiny minority actually need it. Is there a way to add a<br>
&gt; default &quot;type implementation&quot;?<br>
&gt;<br>
&gt; I tried the obvious<br>
&gt;<br>
&gt; type Requirement collection element :: Constraint = ()<br>
&gt;<br>
&gt; and<br>
&gt;<br>
&gt; type Requirement collection element :: Constraint<br>
&gt; type Requirement collection element :: Constraint = ()<br>
&gt;<br>
&gt; but neither compiles.<br>
&gt;<br>
&gt; Another problem is that the compiler can&#39;t compile something like this:<br>
&gt;<br>
&gt; isteps ∷ HasToList collection ⇒ collection String → [(Int, String)]<br>
&gt; isteps steps = Data.List.Index.indexed (toList steps)<br>
&gt;<br>
&gt; because it doesn&#39;t know which constraint to apply (&quot;Could not \
deduce:<br> &gt; Requirement hasToList String&quot;). That seems fair but also \
irrelevant at<br> &gt; this point.<br>
&gt;<br>
&gt; I feel like this is all far too complicated for what I&#39;m trying to<br>
&gt; accomplish. :-) While I would like to have an answer to the questions<br>
&gt; above, I guess what I really want is a simpler way to implement<br>
&gt; HasToList.<br>
&gt;<br>
&gt; Cheers,<br>
&gt; Hilco<br>
<br>
This:<br>
<br>
class HasToList collection element where<br>
            toList ∷ collection element -&gt; [element]<br>
<br>
instance HasToList Set element where<br>
            toList = Data.Set.toList<br>
<br>
instance Enum element =&gt; HasToList EnumSet where<br>
            toList = Data.EnumSet.toList<br>
<br>
compiles fine, of course.<br>
<br>
Strange, I thought I had tried that.<br>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
To (un)subscribe, modify options or view archives go to:<br>
<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>
 Only members subscribed via the mailman list are allowed to post.</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