behavior explanation for higher order functions and labeled argument in OCaml

孤者浪人 提交于 2019-12-24 09:15:20

问题


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:


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.)



来源:https://stackoverflow.com/questions/53132410/behavior-explanation-for-higher-order-functions-and-labeled-argument-in-ocaml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!