-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Delayed effects in partially applied functions #7531
Description
Original bug ID: 7531
Reporter: jmi
Assigned to: @mshinwell
Status: resolved (set by @mshinwell on 2017-05-08T11:17:10Z)
Resolution: duplicate
Priority: normal
Severity: minor
Version: 4.04.0
Category: middle end (typedtree to clambda)
Related to: #7533
Bug description
The native code compiler erroneously delays the effect of an operator until (if ever) it is fully applied.
The following program outputs a newline when compiled with the bytecode compiler but not with the native code compiler:
(let _i = print_newline ()
in fun q -> fun i -> "") ()
I believe the issue is described by the following comment on line 828 of asmcomp/closure.ml:
(* We convert [f a] to [let a' = a in fun b c -> f a' b c]
when fun_arity > nargs *)
which instead should use an approach along the lines of:
(* We convert [f a] to [let f’ = f in
let a’ = a in fun b c -> f’ a’ b c]
when fun_arity > nargs *)
The above example illustrates the situation when the effect is delayed indefinitely. For an example, where the effect is delayed until the time of full application, consider:
let k =
(let _i = print_int 1
in fun q -> fun i -> "") ()
in k (print_int 0)
which prints 10 when compiled with the bytecode backend and 01 with the native code backend.
The behaviour is the same for version 4.02.3.
Steps to reproduce
$ ocamlc -o func.byte func.ml
$ ./func.byte
$ ocamlopt -o func.native func.ml
$ ./func.native
$