This can be done with existensial types as implemented in hbc. See

  http://www.cs.chalmers.se/~augustss/hbc/decls.html

for the example and 

  http://www.cs.chalmers.se/~augustss/hbc/hbc.html

for the compiler.

Below is a part of the example from the webpage above:

Patrik Jansson

--------------------------------------------------------------------

Existentially quantified variables can also be constrained by giving a
context before a constructor. The following defines a type where the
argument to the constructor has to have a type belonging to class Show. 

  data TShow = (Show ?a) => T ?a

This can be used as follows: 

  l :: [TShow]
  l = [T True, T 1, T (atan 1), T [True]] 
  s = map show l

and the value of s will be ["True","1","7.8539816339744828e-1","[True]"]. 




Reply via email to