Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REPL grouping works if there is no error #90

Merged
merged 4 commits into from
May 7, 2024

Conversation

Bohun9
Copy link
Collaborator

@Bohun9 Bohun9 commented May 6, 2024

This commit should resolve #82.

I have made minimal changes to make it work when there are no type errors. There is a problem with infinite loop:

> let x = () ()
  let y = () ;;
<stdin>:1:9-10: fatal error: This expression has type Unit. It is not a function and cannot be applied
<stdin>:1:9-10: fatal error: This expression has type Unit. It is not a function and cannot be applied
<stdin>:1:9-10: fatal error: This expression has type Unit. It is not a function and cannot be applied
<stdin>:1:9-10: fatal error: This expression has type Unit. It is not a function and cannot be applied
...

But the other order works:

> let x = ()
let y = () () ;;
<stdin>:2:9-10: fatal error: This expression has type Unit. It is not a function and cannot be applied
> x ;;
<stdin>:1:1: fatal error: Unbound variable x

Changes in Parser/Main.ml have effect of inserting semicolons between lets and I guess that lazy handling of REPL definitions takes all those lets in one go. But I don't understand the codebase enough to tell why the loop error occurs.

@ppolesiuk ppolesiuk self-requested a review May 6, 2024 16:27
Copy link
Member

@ppolesiuk ppolesiuk left a comment

Choose a reason for hiding this comment

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

The order of definitions is reversed (see detailed comments).

Moreover, the problem of looping REPL (bug mentioned in the PR comment, and #83) probably comes from the fact, that single user input is not treated as a monolith somewhere in the pipeline. If some definition is split into two definitions fun () -> Cons(def1, fun () -> Conf(def2, ...)) and checking of def1 succeeds, but def2 fails, then the evaluator (see Eval.eval_repl) calls the failing thunk again, which in this scenario deterministically returns def2 instead of asking the user for another input. Therefore, thunks describing user input in REPL must be never split into smaller ones. In order to achieve this, the group of definitions should be propagated to type-checker and checked as a monolith. Fortunately, we already have the infrastructure for that (we can use TypeInference.Def.check_defs instead of TypeInferece.Def.check_def).

| Raw.REPL_Def def ->
Seq.Cons(Desugar.tr_def def, repl_seq imported)
| Raw.REPL_Defs defs ->
(List.fold_left (fun ac def () -> Seq.Cons(Desugar.tr_def def, ac)) (repl_seq imported) defs) ()
Copy link
Member

Choose a reason for hiding this comment

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

Using fold_left for appending lists naturally gives us rev_append instead of append. The same problem occurs here: definitions are reversed during parsing. This can be exhibited by the following input to REPL.

> let x = ()
let y = x ;;
<stdin>:2:9: fatal error: Unbound variable x
>

On the other hand, the following input work, but it shouldn't.

> let y = x
let x = () ;;
> 

BTW, we try to keep source in good old 80-column style. This line is too long.

@Bohun9
Copy link
Collaborator Author

Bohun9 commented May 6, 2024

I made suggested change and it seems to work.

Copy link
Member

@ppolesiuk ppolesiuk left a comment

Choose a reason for hiding this comment

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

Adding new constructor, just to slightly modify existing functionality is a bit overkill. Please modify existing Surface.ERepl constructor.

@@ -301,6 +301,9 @@ and def_data =
(** Print type, evaluate, and print the expression, provided by a user in
REPL. *)

| DReplDefs of def list
(** Group of REPL definitions *)
Copy link
Member

Choose a reason for hiding this comment

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

Why do you add a new constructor? It is easier to modify ERepl to accept list of definitions.

@Bohun9
Copy link
Collaborator Author

Bohun9 commented May 6, 2024

New changes were added.

Copy link
Member

@ppolesiuk ppolesiuk left a comment

Choose a reason for hiding this comment

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

I approve the changes, but documentation comments need to be updated. Since there are merge conflicts with the upstream, that I'm going to resolve, I will fix these comments myself.

@@ -232,7 +232,7 @@ and expr_data =
| EAnnot of expr * type_expr
(** Type annotation *)

| ERepl of def Seq.t
| ERepl of def list Seq.t
(** REPL. It is a lazy sequence of definitions provided by a user. *)
Copy link
Member

Choose a reason for hiding this comment

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

A documentation comment could be updated here.


| Raw.REPL_Import import ->
let imported, defs = Import.import_one imported import in
Seq.append (List.to_seq defs) (repl_seq imported) ()
Seq.append (List.to_seq (List.map (fun x -> [x]) defs)) (repl_seq imported) ()
Copy link
Member

Choose a reason for hiding this comment

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

Getting rid of Seq.append and changing it to single monolithic Seq.Cons hopefully fix #83. If it's a change of single line (that should be modified anyway) we should try.

@ppolesiuk
Copy link
Member

Updated version fixes #83

@ppolesiuk ppolesiuk linked an issue May 7, 2024 that may be closed by this pull request
@ppolesiuk ppolesiuk merged commit ce90ab1 into fram-lang:master May 7, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Type-errors in modules imported in REPL could lead to infinite loop Grouping definitions in REPL
2 participants