In an F# signature file, what is the significance of parentheses around a function signature?

Viewed 118

As the consumer of an .fsi signature file what difference, if any, is there between:

val sum : int -> int -> int

and

val sum : (int -> int -> int)

It seems that the second can be implemented by a function definition or by a function 'alias', but the first can only be implemented by a function definition.

With the following:

// .fsi
val add : int -> int -> int
val sum : int -> int -> int
// .fs
let add a b = a + b
let sum = add

an error is generated

error FS0034: Module 'Butter' contains
    val sum : (int -> int -> int)
but its signature specifies
    val sum : int -> int -> int    

however if the signature file is:

// .fsi
val add : (int -> int -> int)
val sum : (int -> int -> int)

it compiles ok.

Is this behaviour intentional, and if so how are the parentheses changing the interface?

2 Answers

What's Tuple here? If it's .NET's System.Tuple, then this doesn't make sense.

val x : Tuple -> float

Would be a function that takes the static class Tuple and returns a float. If you want a tuple instance, then you'll need to specify exactly which n-tuple type you want (Tuple<'a> and Tuple<'a, 'b> are two different types).

The following should work:

var x : Tuple<float, _, _, _> -> float

Yes it is intentional behaviour.

In creating a simpler example in response to Tom's answer I actually read the error message properly it says:

The arities in the signature and implementation differ. The signature specifies that 'sum' is function definition or lambda expression accepting at least 2 argument(s), but the implementation is a computed function value. To declare that a computed function value is a permitted implementation simply parenthesize its type in the signature, e.g.↔ val sum: int -> (int -> int)↔instead of↔ val sum: int -> int -> int.

So the parenthesis are used to indicated a computed expression is an allowed implementation as well lambdas and function definitions. (I won't pretend I properly understand the difference.)

Related