ocaml-effects-tutorial icon indicating copy to clipboard operation
ocaml-effects-tutorial copied to clipboard

wrong type in README.md

Open Wenke-D opened this issue 2 years ago • 3 comments

in the section 1.2. Basics

The parameter k, is the delimited continuation between the point of performing the effect and the effect handler. The delimited continuation is like a dynamically defined function, that can be called and returns a value. The type of k in this case is (int, int) continuation, which says that the continuation expects an integer to continue (the first type parameter), and returns with an integer (the second type parameter).

I believe the type of k should be (int, unit) continuation instead of (int, int) continuation as the function sum_up does not return value.

Wenke-D avatar Nov 22 '23 15:11 Wenke-D

Thanks! Yes, the given type is incorrect. But your reasoning is not correct. The second type argument of the continuation type constructor is the return type of the handler, not the handled computation, meaning the return type of sum_up is irrelevant. We can deduce that the handler returns unit from looking at the exnc clause.

dhil avatar Nov 22 '23 16:11 dhil

Thanks for the clarification!

I am still new to the effect Module, could you please explain why the second type argument is the return type of the handler instead of the one of the computation?

If I understand correctly, the ('a, 'b) continuation represents the computation left to finish when we perform an effect. We need a data of type 'a to resume the left computation, because we sort of took a break when we perform the effect. It feels more nature that 'b is the return type of the computation instead of the handler.

Thanks in advance

Wenke-D avatar Nov 22 '23 18:11 Wenke-D

The correct reading of ('a, 'b) continuation depends on the origin of the continuation. If the continuation was captured "shallowly" then you are correct that 'b would be instantiated to the same type as the remainder of the handled computation. If it is captured "deeply" then the handler itself is embedded in the continuation, in this case 'b is instantiated to the return type of the handler (because the handler is run as part of the continuation).

The example uses a deep handler, hence 'b should be the return type of the handler.

(This paper may interest you, it discusses the differences between shallow and deep handlers.)

dhil avatar Nov 27 '23 09:11 dhil

Thanks for the information

Wenke-D avatar May 07 '24 08:05 Wenke-D