<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 <<a href="mailto:hilco.wijbenga@gmail.com">hilco.wijbenga@gmail.com</a>> 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 <<a href="mailto:hilco.wijbenga@gmail.com" target="_blank">hilco.wijbenga@gmail.com</a>> wrote:<br>
><br>
> Hi all,<br>
><br>
> I'm trying to create a "HasToList" type class. It starts off really simple:<br>
><br>
> class HasToList collection where<br>
>         toList ∷ collection element → [element]<br>
><br>
> instance HasToList Set where<br>
>         toList = Data.Set.toList<br>
><br>
> Unfortunately, this breaks down with Data.EnumSet.EnumSet. So<br>
><br>
> instance HasToList EnumSet where<br>
>         toList = Data.EnumSet.toList<br>
><br>
> doesn't compile and I could not find a way to add "Enum a =>" to<br>
> either the instance or "toList". (Well, not without making the<br>
> compiler upset.)<br>
><br>
> With the aid of ConstraintKinds and TypeFamilies (and not much<br>
> understanding on my end, obviously) I created<br>
><br>
> class HasToList collection where<br>
>         type Requirement collection element :: Constraint<br>
>         toList ∷ Requirement collection element => collection element<br>
> → [element]<br>
><br>
> instance HasToList Set where<br>
>         type Requirement Set a = ()<br>
>         toList = Data.Set.toList<br>
><br>
> instance HasToList EnumSet where<br>
>         type Requirement EnumSet a = Enum a<br>
>         toList = Data.EnumSet.toList<br>
><br>
> This works but now I have to add the constraint to every instance even<br>
> though only a tiny minority actually need it. Is there a way to add a<br>
> default "type implementation"?<br>
><br>
> I tried the obvious<br>
><br>
> type Requirement collection element :: Constraint = ()<br>
><br>
> and<br>
><br>
> type Requirement collection element :: Constraint<br>
> type Requirement collection element :: Constraint = ()<br>
><br>
> but neither compiles.<br>
><br>
> Another problem is that the compiler can't compile something like this:<br>
><br>
> isteps ∷ HasToList collection ⇒ collection String → [(Int, String)]<br>
> isteps steps = Data.List.Index.indexed (toList steps)<br>
><br>
> because it doesn't know which constraint to apply ("Could not deduce:<br>
> Requirement hasToList String"). That seems fair but also irrelevant at<br>
> this point.<br>
><br>
> I feel like this is all far too complicated for what I'm trying to<br>
> accomplish. :-) While I would like to have an answer to the questions<br>
> above, I guess what I really want is a simpler way to implement<br>
> HasToList.<br>
><br>
> Cheers,<br>
> Hilco<br>
<br>
This:<br>
<br>
class HasToList collection element where<br>
        toList ∷ collection element -> [element]<br>
<br>
instance HasToList Set element where<br>
        toList = Data.Set.toList<br>
<br>
instance Enum element => 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>