[Haskell-cafe] Ambiguous type variable with subclass instance
(also: is there a better way to do this?)
José Pedro Magalhães
jpm at cs.uu.nl
Thu Sep 17 10:21:34 EDT 2009
Hello,
On Thu, Sep 17, 2009 at 16:05, Daniel Fischer <daniel.is.fischer at web.de>wrote:
> Am Donnerstag 17 September 2009 15:56:03 schrieb José Pedro Magalhães:
> > Hey Andy,
> >
> > On Thu, Sep 17, 2009 at 15:40, Andy Gimblett <haskell at gimbo.org.uk>
> wrote:
> > > Now, some of those algebraic data type types happen to be
> > > enumerations; in this case, my idea is to list the constructors, with
> > > the rule that each constructor's position in the list is the Int which
> > > gets converted into that constructor.
> > >
> > > > class Enumerated a where
> > > > constructors :: [a]
> > >
> > > E.g. here's a type Bar with three constructors:
> > > > data Bar = X | Y | Z deriving (Show)
> > > > instance Enumerated Bar where
> > > > constructors = [X, Y, Z]
> > >
> > > (This is certainly ugly. Any suggestions?)
> > >
> > |constructors| is expressible in SYB:
> Wow.
>
> What about
>
> data Bar = X | Y | Z deriving (Show, Eq, Ord, Enum, Bounded)
>
> instance Enumerated Bar where
> constructors = [minBound .. maxBound]
>
> ?
>
Oh yes, that will certainly work for this very simple datatype. However, one
cannot automatically derive instances of |Bounded| for datatypes with
non-nullary constructors.
Cheers,
Pedro
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090917/c859e2e5/attachment.html
More information about the Haskell-Cafe
mailing list