[Haskell-cafe] Is it possible to represent such polymorphism?

David Barbour dmbarbour at gmail.com
Sun Oct 2 17:55:05 CEST 2011


On Sun, Oct 2, 2011 at 8:45 AM, Du Xi <sdiyazg at sjtu.edu.cn> wrote:

> Then again , in typeclass definition how can I express the type "a->b"
> where "a" is the type parameter of the class and "b" is a type deduced from
> the rules defined in each instance of the class, which varies on a
> per-instance basis? e.g.
>
> instance ExampleClass a where
>    f :: a->**SomeTypeWhichIsDifferentInEach**Instance
>
> What I want is some thing like this in C++:
>
> float f(char x){ return 0.1f; }
> int f(double x){ return 1; }


Use TypeFamilies.


{-# LANGUAGE TypeFamilies #}
...
type family FType a :: *
type instance FType Char = Float
type instance FType Double = Int

class ExampleClass a where
  f :: a -> FType a
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20111002/c7399b86/attachment.htm>


More information about the Haskell-Cafe mailing list