Skip to content

effect syntax: effect patterns as computation patterns#13135

Open
Octachron wants to merge 1 commit into
ocaml:trunkfrom
Octachron:effect-pattern-as-computation-pattern
Open

effect syntax: effect patterns as computation patterns#13135
Octachron wants to merge 1 commit into
ocaml:trunkfrom
Octachron:effect-pattern-as-computation-pattern

Conversation

@Octachron

Copy link
Copy Markdown
Member

This PR is meant to be read at the top of the effect-syntax PR(#12309). In particular, only the last commit is new.

This commit aims at making the typechecker part of the effect syntax changes smaller.
This is done by merging back effect patterns into the computation pattern umbrella in the typechecker part of the compiler. Splitting patterns by kind is then done when compiling to the lambda IR.

This change also has the advantage of making the error messages for non-toplevel computation patterns more uniform:

let f [effect _, _] = ()

Error: Effect patterns are not allowed in this position.

let g [exception _ ] = ()

Error: Exception patterns are not allowed in this position.

Incidentally, it makes the compiler support cases containing mixed effect, exception and value cases:

let catch f = match f () with _  | exception _ | effect _, _ -> ( )
type _ Effect.t += A: unit Effect.t
type _ Effect.t += B: unit Effect.t

let () = catch ignore; catch (fun () -> raise Not_found); catch (fun () -> Effect.perform A)

However, the feature is mostly useless, since effect types require type equations to be useful, and we discard type equations introduced under or-patterns. In other words, it is impossible to bind the continuation in the pattern above:

let test f = match f () with k | effect A, k -> ()
Error: The variable k on the left-hand side of this or-pattern has type 'a
      but on the right-hand side it has type (%eff, 'b) continuation
      The type constructor %eff would escape its scope

@Octachron Octachron mentioned this pull request Apr 29, 2024
9 tasks
@gasche gasche added the typing label May 15, 2024
At the typechecker level, effect cases are similar to the other
computation patterns. This commit refactorize the parsing and
typechecking of effect cases to fully fall in the computation pattern
category
@Octachron Octachron force-pushed the effect-pattern-as-computation-pattern branch from e50ee90 to aa28263 Compare May 15, 2024 15:43

@voodoos voodoos left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made a round of review with a few comments. I did not enter deeply into the typecore changes but it looks all-rigth from afar.

(I don't know what is the correct etiquette regarding formatting nitpicking, they are quite a few in the translcore module...)

Comment thread lambda/translcore.ml
and transl_cont (type a) cont pat_cont body =
match pat_cont with
| Some (id2,_) -> Llet(Alias, Pgenval, id2, Lvar cont, body)
| None -> body

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
| None -> body
| None -> body

Comment thread lambda/translcore.ml
| None, None -> assert false
let sp = split_pattern c_lhs in
match sp.value, sp.exn with
| None, None -> assert false

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
| None, None -> assert false
| None, None -> assert false

Comment thread lambda/translcore.ml
(lbl, ids_kinds, rhs) :: static_handlers
in
let val_cases, exn_cases, static_handlers =
let val_cases, exn_cases, static_handlers =

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let val_cases, exn_cases, static_handlers =
let val_cases, exn_cases, static_handlers =

^^^
Line 3, characters 4-19:
3 | | effect A _, [k] -> ()
^^^^^^^^^^^^^^^

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this position is less precise than before ?

Comment thread typing/parmatch.ml
add_row vpss vp, add_row epss ep
) patl ([], [])
let val_pss, exn_pss, eff_pss =
List.fold_right (fun pat (vpss, epss,effss)->

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
List.fold_right (fun pat (vpss, epss,effss)->
List.fold_right (fun pat (vpss, epss, effss)->

Comment thread typing/predef.ml
ident_create "Undefined_recursive_module"
and ident_continuation_already_taken = ident_create "Continuation_already_taken"


Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the compiler policy about unrelated formatting changes ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are generally not accepted. If this case, it looks like this is reverting the addition of a newline in the effect syntax PR.

Comment thread typing/typedtree.mli
type _ pattern_category =
| Value : value pattern_category
| Computation : computation pattern_category
| Computation : {exn:bool} -> computation pattern_category

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think commenting about the role of exn would be useful here.

Comment thread typing/typecore.ml
| Ppat_any -> None
| Ppat_var name ->
let ty = instance cont_ty in
let id, _uid =

@voodoos voodoos Jul 29, 2024

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be useful to store the uid of the continuation in the Tpat_effect node in the same way as we do for Tpat_var and Tpat_alias. (Merlin relies on this information when querying local occurrences from an identifier's declaration.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants