Search code examples
f#monomorphism

Why does F# function return a signature of "val FunctionName: int -> int"?


I was following some examples on F# Wikibook on High Order Functions.

Second code snippet under title, Composition Function has following code snippet.

#light
open System

let compose f g x = f (g x)

let xSquared x = x*x
let negXPlusFive x = -x/2.0 + 5.0

let fog = compose xSquared negXPlusFive

// ... Console.WriteLine statements....

The one I am having problem understanding is

let xSquared x = x*x

When I run it with F# interactive shell (fsi.exe) by itself, I get the following signature.

> let xSquared x = x*x;;

val xSquared : int -> int

But when I run the whole code snippet, xSquared returns the following.

val compose : ('a -> 'b) -> ('c -> 'a) -> 'c -> 'b
val xSquared : float -> float
val negXPlusFive : float -> float
val fog : (float -> float)

Why does xSquared takes float and returns float?


Solution

  • With more information, F# can determine that xSquared is called with float arguments. If you change negXPlusFive to something like "let negXPlusFive x = -x + 5" you would find that it, fog and xSquared would be "int -> int".