开发者

How do you read OCaml function typing output in your mental dialog?

For [1]:

# let make pair int (x:int) (y:int) = x,y ;;
val make_pair_int : int -> int -> int * int = <fun>

How do you read the typing information output? In other languages, I find being able to construct a cogent, English sentence out of the syntax or the printouts to be critical in understanding what's going on. What's the sentence for 开发者_开发百科the above OCaml statement?

Could someone diagram out the printout so as to correlate the typing information back into English? Like:

val make_pair_int : int -> int -> int * int = <fun>
      |              |   |  |   |  \_____/  \__/
      |              |   |  |   |     |       ` is a function
      |              |   |  |   |     ` a tuple of integers
      |              |   |  |   `
      |              |   |  `
      |              |   `
      |              `
      ` symbol make_pair_int is bound to


This is just the curried version of your function. The reading of such a function depends on how comfortable you are with curried notation for functions.

The gist is to consider that taking two arguments is a completely different notion from taking a pair of arguments in the sense that the pair makes a reference to a specific type constructor (what is hidden under the * and , symbols in your ocaml snippet).

What do you do if you do not want to make a reference to a pairing (or tupling, if you want an arbitrary array of arguments) construct ? The curried answer to that question is the one that makes use of higher-order functions:

val make_pair_int : int -> int -> int * int = <fun>
      |              |   |  |   |  \_____/  \__/
      |              |   |  |   |     |       ` and all that is a higher-order function
      |              |   |  |   |     ` a pair of integers
      |              |   |  |   `and returns
      |              |   |  `a function that takes an integer as an argument
      |              |   `and returns
      |              ` something that takes an integer as an argument 
      ` symbol make_pair_int is bound to

With the implicit convention that this curried interpretation (and the notation that comes with it) is the admitted way of thinking of a function taking several arguments, you can just count the number of arrows in the type, and consider those before the last one as "the arguments" of your function.


I guess it is highly subjective and depends on the use of the function. Usually I try to read this as the uncurried version of the function and then read this in my mind. However if I actually use this function to construct closures I might read it as something different.

So in this case, depending on the context I use this function i would read it as:

"function from int and int to tuple of integers"

In other cases the same might become:

"function from int to function from int to tuple of integers"

I guess the ability to switch between the two representations of the same function offers a lot of flexibility in the use of the function later.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜