[Haskell-beginners] Functor instance for ordered lists

Benjamin Edwards edwards.benj at gmail.com
Mon Jan 4 14:46:20 UTC 2016


It is impossible.

You can make a new functor class using contraint kinds that allows what you
want. There is probably a package out there already that does!

http://www.cl.cam.ac.uk/~dao29/publ/constraint-families.pdf

Sections 2.2 and 5.1 have the relevant stuff. I realise this is a bit err,
dense for the beginners list. There are probably better references out
there.

Ben

On Mon, 4 Jan 2016 at 14:30 martin <martin.drautzburg at web.de> wrote:

> Am 01/04/2016 um 11:45 AM schrieb Imants Cekusins:
> >> a newtype for ordered lists
> >
> > why not:
> > newtype Ordlist a = Ordlist [a]
>
> All nice and dandy, but at first you already need an Ord constraint for
> your smart constructor
>
> -- and a ctor:
> ordList::(Ord a) => [a]-> OrdList a
> ordList = OrdList . sort
>
> but this is still not the main problem. When you try to define a Functor
> instance, you'd be tempted to do this (at least
> I was):
>
> instance Functor OrdList where
>         fmap f (OrdList xs) = OrdList $ sort $ map f xs
>
> but you can't do this, because of: "No instance for (Ord b) arising from a
> use of ‘sort’", where b is the return type of
> f :: (a->b). This does make sense, the function has to return something
> which can be sorted.
>
> So my question is: is it impossible to write a functor instance for
> ordered lists? It appears so, because a Functor does
> not impose any constraints of f. But my knowledge is quite limited and
> maybe a well-set class constraint can fix things.
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20160104/eeb19714/attachment.html>


More information about the Beginners mailing list