[Haskell-cafe] DatatypeContexts / alternative
Tom Ellis
tom-lists-haskell-cafe-2017 at jaguarpaw.co.uk
Tue Feb 23 18:29:47 UTC 2021
On Tue, Feb 23, 2021 at 06:14:59PM +0000, CASANOVA Juan wrote:
> module DatatypeContextsExample where
>
> import Data.Map
> import Data.Bifunctor
>
> data Foo t = Foo (Map t t)
>
> instance Functor Foo where
> fmap f (Foo m) = Foo (fromList (fmap (bimap f f) (toList m)))
>
> This does not compile, because I am using toList and fromList, which
> require (Ord t). But I cannot really have Foo be a functor in this
> way without it. The thing is, every time I am going to use it, t is
> actually going to be Ord. But how do I tell Haskell to have this
> constraint?
You say that every time you are going to use it t is actually going to
be Ord, but how does the compiler know that when it comes to type
check fmap? After all, somewhere else you could write
fmap (const getLine) :: Foo t -> Foo (IO String)
and IO String is certainly not Ord.
Tom
More information about the Haskell-Cafe
mailing list