behavior explanation for higher order functions an

2019-08-04 10:13发布

Taking an exemple derived from RWOCaml :

utop # let divide ~first ~second = first / second;;
val divide : first:int -> second:int -> int = <fun>

utop # let apply_to_tuple_3 f (first,second) = f second first;;
val apply_to_tuple_3 : ('a -> 'b -> 'c) -> 'b * 'a -> 'c = <fun>

utop # apply_to_tuple_3 divide;;
Error: This expression has type first:int -> second:int -> int
       but an expression was expected of type 'a -> 'b -> 'c

Does it make sense to not match the types here ? apply_to_tuple_3 only makes use of the positional arguments, which certainly divide possesses.

Upon removing the names, the application is accepted

utop # let divide_an x y = divide x y;;
val divide_an : int -> int -> int = <fun>
utop # apply_to_tuple_3 divide_an;;
- : int * int -> int = <fun>

Is there any reason to reject the first call ?

1条回答
家丑人穷心不美
2楼-- · 2019-08-04 10:36

Functions with labeled parameters have a type that depends on the labels and on the order that they appear. When calling such functions, there is flexibility in the order of arguments that you supply. And in fact you can omit the labels if you supply all of the arguments.

However, when passing such functions as values themselves, there is no such flexibility. You have only the one labeled type to work with.

This is covered on page 42 of Real World OCaml: Higher-order functions and labels.

(If you're asking why this is the case, I can only assume that type checking becomes difficult or impossible if you allow such flexibility.)

查看更多
登录 后发表回答