Home > OS >  How can I manually calculate the type of functions with class constraints?
How can I manually calculate the type of functions with class constraints?

Time:06-21

The following typing rule for function application

    f :: A -> B
    e :: A
    -----------
    f e :: B

doesn't take into account an f with class constraints. How can I manually calculate the type of, for example:

( ) :: Num a => a -> a -> a
3 :: Int
---------------------------
( ) 3 :: ?

CodePudding user response:

For:

( ) :: Num a =>  a -> a -> a
3   ::          Int

We know that a (the first parameter) is the same type as Int (the type of 3), so that means that a ~ Int (a and Int are the same type), so that means that:

( )   :: Num a =>  a ->  a  ->  a
3     ::          Int
------------------------------------
( ) 3 :: Num Int =>     Int -> Int

Since Int is a member of the Num typeclass, we can remove Num Int =>, and thus obtain:

( )   :: Num a =>  a ->  a  ->  a
3     ::          Int
------------------------------------
( ) 3 ::                Int -> Int
  • Related