Marcin 'Qrczak' Kowalczyk
1 Jun 2001 18:34:46 GMT
Fri, 1 Jun 2001 15:25:24 -0700, Saswat Anand <email@example.com> pisze:
> I can't understand how the arithmatic operators like (+),(-) are =
> defined and fromInteger works in that context.
They are all in class Num. fromInteger is automatically
inserted before all integer literals, e.g. '42' is equivalent to
'fromInteger (42::Integer)' and has type 'Num a => a', i.e. the
context can provide any numeric type and 42 will have that type
by using the appropriate fromInteger.
> Type Fun a = Int -> a
> fun:: Fun Int
> fun = (+1)
> instance (Num a) => Num (Fun a) where
> (fun + 2) 10 and (2 + fun) 10 works fine. But how does it know it
> has to use fromInteger?
(+):: Num a => a -> a -> a
>From this type it follows that both arguments of '+' must have the
same type (and that this type must be Num).
The first argument of '+' in 'fun + 2', i.e. 'fun', has a fully
determined type: 'Fun Int'. It follows that the second argument of
'+', i.e. '2', must be 'Fun Int' too.
It's an integral literal, so it can have any Num type. Unifying
'Num a => a' with 'Fun Int' succeeds and gives an answer what 'a'
is here: it's 'Fun Int', and it's ok because it's Num.
> But If I want these,
> (Just 2 + fun) 10 and (fun + Just 2) 10
> to work, then is there a way to tell which conversion function
> to use?
No: 'Just 2' has type 'Num a => Maybe a', 'fun' has type 'Fun Int',
i.e. 'Int -> Int'. In no way 'Maybe something' can be the same type as
'arg -> result'.
__("< Marcin Kowalczyk * firstname.lastname@example.org http://qrczak.ids.net.pl/
^^ SYGNATURA ZASTĘPCZA