[Haskell-fr] a function which return the type of element

Dan Popa popavdan at yahoo.com
Fri Nov 16 13:02:25 EST 2007


--- BOUNEB ZINE EL ABIDINE <bzine19 at yahoo.fr> wrote:
> I look for the function which return the type of un
> element in Haskell :

> For example 
> 
>    If  type(a) = Int then f else g 
> 

Hello,I am Dan P. and I try to answer:

If you want to get different effects, let's say
different expression being evaluated according to the
TYPE of the entity involved you may try to use:
- a class
- some instances of that class, every one having it's
own definition of 'type'

Let's start:
Let's suppose f and g are two functions.
First of all you can try to define your own class:

class MyClass a b c where
  type :: a -> (b->c)

So, for every element of type a, the value of type(a)
will be a function.
Let's suppose we know the signatures of f and g:
something like (the same for both f and g)

 f:: Float -> Char
 g:: Float -> Char
-----------------------------------------------------
| TO define the behaviour of type for a being an Int|
| YOU HAVE TO INSTANTIATE the class MyClass and tell|
| how can  type will work !                         |
-----------------------------------------------------

instance MyClass Int Float Char where
  type _ = f

For the other types (you did not specify them,sorry)
for example for String
other instances can be defined:

instance MyClass String Float Char where
  type _ = g

In this way the function 'type' became a
polimorphically overrloaded function.

--------------------------------------------
Simplified version. f::X and g :: X , X being a
specific type.

class SimpleClass a  where
  type :: a -> X

instance SimpleClass Int where
  type _ = f

instance SimpleClass Char where
  type _ = g 

and so one for other types which can be the type 
of a.

-----------------------------------------------
May be more solutions.
Let's hope this will help.


Dan






      ____________________________________________________________________________________
Be a better pen pal. 
Text or chat with friends inside Yahoo! Mail. See how.  http://overview.mail.yahoo.com/


More information about the Haskell-fr mailing list