[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 <<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>
[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