0

I started learning functional programming (OCaml), but I don't understand one important topic about functional programming: types.

Can anyone explain me this solution please?

Have a test this week and can't get reach the resolution..

let f a b c = a (a b c) 0;;
f: ('a -> int -> 'a) -> 'a -> int -> 'a
3
  • 1
    You seem to be asking about the type of this function, but unfortunately I don't see a question to be answered. When you enter an expression, the OCaml toplevel tells you its type. That's what's happening here. Types are explained in Chapter 6.4 of the OCaml manual. But a tutorial might be a better place to start (I couldn't find one in the time I have, sorry). Commented Jun 18, 2014 at 13:54
  • Yes.. the type of function is what i cant determine but thanks! Commented Jun 18, 2014 at 13:55
  • A tutorial that Jeffrey could have pointed out had he had the time is mauny.net/data/papers/mauny-1995b.pdf Commented Jun 18, 2014 at 18:21

1 Answer 1

4
let f a b c = a (a b c) 0;; 

Your confusion involves types and type inference, i.e., when you define a function or binding, you don't need to give explicit types for its parameters, nor the function/binding itself, OCaml will figure it out if your definition of function/binding is correct.

So, let's do some manual inferences ourselves. If a human can do, then the compiler can also do.


1.

let x = 1

1 is integer, so x must be an integer. So you don't need to do int x = 1 as in other languages, right?

2.

let f x = 1

If there are multiple variable names between let and =, then it must be a function definition, right? Otherwise, it won't make sense. In Java like language, it also makes no sense to say int x y = 1, right?

So f is a function and x is must be a parameter. Since the righthand side of = is an integer, then we know f will return an integer. For x, we don't know, so x will be thought as a polymorphic type 'a.

So f: 'a -> int = <fun>.

3.

let f a b c = a (a b c) 0;;
  • f is a function, with parameters a, b, c.

  • a must be a function, because on the righthand side of =, it is a function application.

  • a takes two arguments: (a b c) and 0. 0 is an integer, so the 2nd parameter of a must be an integer type.

  • Look inside (a b c), c is the 2nd arguement, so c must be integer.

  • We can't infer on b. So b is 'a.

  • Since (a b c) can be the 1st argument of a, and (a b c) itself is an application on function a, the return type of a will have the same type of b which is 'a.

Combine information above together, you get f: ('a -> int -> 'a) -> 'a -> int -> 'a.


If you want to learn it formally, https://realworldocaml.org/ is your friend.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.