1

consider the follow f# code

let vl x= let tem y =(x,y) in tem in let a= vl 5 in Console.WriteLine((a "4",a 3))

where vl has the signature 'a -> 'b -> 'a * 'b so tem is 'b -> 'a * 'b but compiling the code produce the error message:

Error FS0001 This expression was expected to have type 'string'
but here has type 'int'

what is going on here?

1 Answers1

3

This is because of the eternally dreaded and marvelously confusing value restriction.

The signature of vl is indeed what you expect it to be. It's the signature of a that can't be generic.

It can't be generic because it's syntactically a value, not a function, which is the essence of the value restriction. For a bit more info on it see this answer.

If a was a top-level binding, this could be fixed by explicitly giving it a generic parameter like let a<'t> = ..., but since it's a local binding, this doesn't work, and the only way to fix this is to give it a parameter, so it's no longer a value, but a function:

let vl x = let tem y = (x,y) in tem in let a z = vl 5 z in (a "4", a 3)
Asti
  • 12,447
  • 29
  • 38
Fyodor Soikin
  • 78,590
  • 9
  • 125
  • 172