Search code examples
typessemantics

The most general type of a given function


I am studying for a test from operational semantics and type systems and I came across a task I am not sure how to approach.

The task is as follows: Determine the most general type for a function f. f(a,b,c,d) = g(c,d), where g = a(b).


I guess that function f(a,b,c,d) returns the output of a function g(c,d). So lets suppose that c,d are some variables of a basic type and a is a function with one argument b of some basic type. But I do not know what g = a(b) means when used without arguments when there is a call of this function in a form of g(c,d)... or if anything I just said is correct :-/

I am not sure what to do here. Can you give me a hint or redirect me to some article concerning this topic (ideally with an example like this one). The only thing I have found so far are general texts about type systems and semantics. Thanks a lot!


Solution

  • You have to ask yourself "what kind of function is a?"

    Since a(b) is later used as g(c , d) (which is the same as (a(b))(c, d)), we know that the result of a(b) is a function. so "a" is a function that returns a function. Specifically it is a function that returns a function which takes two arguments c and d.

    If the above is confusing (I'm not sure how familiar you are with functional programming, etc) it might help to consider a concrete example. Imagine you have two functions, foo and bar. foo multiplies two numbers (c and d) and bar adds two numbers.

    a could be a function which chooses whether we want to use foo or bar.

    So f(a , "bar", 1, 2) would be (a("bar"))(1, 2) = 1 + 2 = 3. and f(a , "foo", 1, 2) would be (a("foo"))(1, 2) = 1 * 2 = 2.