11

A short search didn't help me to find the answer, so I started to doubt in its existance. The question is simple. I want to create a polymorphic function, something like this:

f :: String -> String
f s = show (length s)

f :: Int -> String
f i = show i

A function defined differently for different data types is meant. Is it possible? If yes, how?

2

2 Answers 2

24

There are two flavors of polymorphism in Haskell:

  • parameteric polymorphism; and
  • bounded polymorphism

The first is the most general -- a function is parametrically polymorphic if it behaves uniformly for all types, in at least one of its type parameters.

For example, the function length is polymorphic -- it returns the length of a list, no matter what type is stored in its list.

length :: [a] -> Int

The polymorphism is indicated by a lower case type variable.

Now, if you have custom behavior that you want to have for a certain set of types, then you have bounded polymorphism (also known as "ad hoc"). In Haskell we use type classes for this.

The class declares which function will be available across a set of types:

class FunnyShow a where
    funnyshow :: a -> String

and then you can define instances for each type you care about:

instance FunnyShow Int where
    funnyshow i = show (i+1)

and maybe:

instance FunnyShow [Char] where
   funnyshow s = show (show s)
6
  • And is the funnyshow just a regular function, or it has some specific things to have in mind when using it?
    – aplavin
    Commented Sep 14, 2012 at 19:25
  • 1
    It is a regular function. You will have a compile error if you try to use it on a type for which you don't have an instance. Commented Sep 14, 2012 at 19:26
  • Thanks, this really seems to be the correct solution. However, it's strange that I haven't noticed usage of instance in several haskell programs I looked at.
    – aplavin
    Commented Sep 14, 2012 at 19:33
  • 2
    @chersanya: Have you used show? ==//=? </<=/>/>=? Or +/-/*? Those are all (some of the) functions from type classes in the Prelude: Show, Eq, Ord, and Num, respectively. So you don't see many instances because much of that has been done for you. Commented Sep 14, 2012 at 20:51
  • 2
    @chersanya: It isn't that surprising. An instance for some type only needs to be declared once to be used everywhere, and most of the standard types have standard instances already declared. This is more the type of code you expect to see in libraries defining new types than in normal programs. Also, common classes like Show and Eq can be specified with deriving where the compiler writes the instance for you, so if you've seen something like deriving (Show, Eq), you've just seen a different way to declare an instance. Commented Sep 15, 2012 at 6:54
14

Here is how you can achieve something similar using type families.

Well if you have same return types then you can achieve the behaviour without using type families and just using type classes alone as suggested by Don.

But it is better to use type families when you want to support more complex adhoc polymorphism, like different return types for each instance.

{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE TypeFamilies #-}

class F a where
    type Out a :: * 
    f :: a -> Out a

instance F String where  
    type Out String = String
    f = show . length

instance F Int where 
    type Out Int = String 
    f = show 

instance F Float where 
    type Out Float = Float
    f = id  

In ghci

*Main> f (2::Int)
"2"

*Main> f "Hello"
"5"

*Main> f (2::Float)
2.0
4
  • How can length return a String? :)
    – is7s
    Commented Sep 14, 2012 at 20:33
  • @is7s Sorry, I didn't changed it completely after author modified the question.. Thanks .. corrected ..
    – Satvik
    Commented Sep 15, 2012 at 2:57
  • I didn't see type Out... before. I think without it will work as well. Is it used to disambiguate???
    – Jaider
    Commented Sep 28, 2012 at 18:13
  • @jaider see the type of f. it depends on type of Out a. You can read more about type families.
    – Satvik
    Commented Sep 29, 2012 at 5:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.