[Haskell-cafe] Store type-class polymorphic values generically

Heinrich Apfelmus apfelmus
Fri Oct 4 10:41:33 UTC 2013

Christopher Done wrote:
> On 4 October 2013 10:56, Heinrich Apfelmus <apfelmus at quantentunnel.de> wrote:
>> In particular, the  Locker  stores arbitrary values like  Dynamic , except
>> that values are extracted and removed with the help of a  Key . This gets
>> rid of the  Typeable  constraint.
> lock :: Key a -> a -> Locker
> I can't pass anything with class constraints to that.

I don't know what "something with a class constraint" means. But I guess 
you want to pass a value with a *polymorphic* type? This is no problem, 
but requires impredicative polymorphism:

     a = (forall b. Show b => b -> IO ())

     lock :: Key (forall b. Show b => b -> IO ())
          -> (forall b. Show b => b -> IO ())
          -> Locker

Unfortunately, GHC's support for that is a little shaky, but a solution 
that always works is to put it in a new data type.

     data Dummy = Dummy { unDummy :: forall b. Show b => b -> IO () }

     lock :: Key Dummy -> Dummy -> Locker

It seems to me that your problem decomposes into two problems:

1. A heterogenous store for values of different types.
2. Values with polymorphic instead of monomorphic types.

Solution for problem 1 are usually restricted to monomorphic types, but 
you can work around it.

Best regards,
Heinrich Apfelmus


More information about the Haskell-Cafe mailing list