[Haskell-cafe] Re: Newbie question

Jon Fairbairn jon.fairbairn at cl.cam.ac.uk
Mon Jan 21 13:28:09 EST 2008


"Alexander Seliverstov" <seliverstov.a at gmail.com> writes:

> So, the function type "(Num a)=>Integer->a" means that return value of
> this function can be cast to any particular instance of class Num.

For some meanings of the word "cast" yes. I'd rather say
"f:: Num a=> Integer -> a" means that for any type a that is
an instance of Num, given an integer f will return a member
of that type.

> Ok. I have a my own class "class A a" and want to write function like
> this  "f:: (A a)=>Integer->a". Can I do it?

You need to be a bit more specific about what f is supposed
to do without that, I can answer unequivocally yes, and give
you

f:: (A a) => Integer -> a
f n = undefined

:-P

But in general you are going to want something a bit more
useful, which means that you have to have a path from
Integer to a -- what the path can be is depends on what
"methods" you give class A. For example:

class A a where
  first_a :: a
  second_a :: a

f :: A t => Integer -> t
f n | odd n = first_a
    | otherwise = second_a

-- 
Jón Fairbairn                                 Jon.Fairbairn at cl.cam.ac.uk




More information about the Haskell-Cafe mailing list