问题
I am experimenting with Core's List.fold_left
.
# List.fold_left;;
- : 'a Core.Std.List.t -> init:'b -> f:('b -> 'a -> 'b) -> 'b = <fun>
It works fine when I specify the labels:
# List.fold_left [1;2;3] ~init:0 ~f:(+);;
- : int = 6
But I get a different result when I do not specify the labels:
# List.fold_left [1;2;3] 0 (+);;
- : init:(int -> (int -> int -> int) -> '_a) ->
f:((int -> (int -> int -> int) -> '_a) ->
int -> int -> (int -> int -> int) -> '_a) ->
'_a
= <fun>
And other partial applications also yield nonintuitive types. Why can I add an arbitrary amount of 0's after the list argument?
# List.fold_left [1;2;3] 0;;
- : init:(int -> '_a) -> f:((int -> '_a) -> int -> int -> '_a) -> '_a = <fun>
# List.fold_left [1;2;3] 0 0;;
- : init:(int -> int -> '_a) ->
f:((int -> int -> '_a) -> int -> int -> int -> '_a) -> '_a
= <fun>
Some other functions exhibit the same behavior:
# let app ~f ~x = f x;;
val app : f:('a -> 'b) -> x:'a -> 'b = <fun>
# app (fun x -> x + 1) 1;;
- : f:('a -> (int -> int) -> int -> 'b) -> x:'a -> 'b = <fun>
But some labeled functions are able to return the expected result when applied without labels given. For example:
# List.map;;
- : 'a Core.Std.List.t -> f:('a -> 'b) -> 'b Core.Std.List.t = <fun>
# List.map [1;2;3] (fun x -> x + 1);;
- : int Core.Std.List.t = [2; 3; 4]
Why does some functions return nonintuitive values when applied without labels given, while the others work as expected?
回答1:
According to the manual you may only omit labels "if an application is total (omitting all optional arguments)" with the important caveat "that functions like ListLabels.fold_left
whose result type is a type variable will never be considered as totally applied."
So since core's fold_left
's result type is, of course, also a type variable, you can not call it without labels. What happens when you do is that the positional arguments are interpreted as arguments to the function produced by fold_left
and it still waits for you to provide the labeled arguments.
回答2:
@sepp2k's answer clearly explains what is happening here. Let me add one more thing.
Simply, do NOT omit labels of labeled functions. They are there for purposes. I believe no one can handle label omittion always correctly. You should always turn the warning 6 on and make it an error:
$ ocamlc -w +6 -warn-error +6 file.ml
File "file.ml", line 2, characters 17-18:
Warning 6: labels were omitted in the application of this function.
File "file.ml", line 1:
Error: Some fatal warnings were triggered (1 occurrences)
来源:https://stackoverflow.com/questions/26703974/using-core-std-list-fold-left-without-label