I have a foo function defined as follows
fun foo x y = x (x (x y));
How to deduce the function type?
The answer is:
val foo = fn : ('a -> 'a) -> 'a -> 'a
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This process is called type inference which is done in SML by Hindley–Milner algorithm.
First let’s start with a generic type signature for
foo:where
xhas type'candyhas type'b.We have the following steps:
x yis function application,xshould have signature'b -> 'dand we have an equation'c = 'b -> 'dandx yhas type'd.x (x y)means we apply a function type'b -> 'don an argument type'dso'd = 'band'c = 'b -> 'b.x (x (x y))means we apply a function type'b -> 'bon an argument type'band return'a(type offoo); it suffices'b = 'a.'c = 'a -> 'a and 'b = 'aandfoohas a generic type:val foo = fn : ('a -> 'a) -> 'a -> 'a