Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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 :: ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading