Skip to content

Allow recursive bindings with _#486

Closed
OlivierNicole wants to merge 3 commits intoocaml:trunkfrom
OlivierNicole:underscore-in-letrec
Closed

Allow recursive bindings with _#486
OlivierNicole wants to merge 3 commits intoocaml:trunkfrom
OlivierNicole:underscore-in-letrec

Conversation

@OlivierNicole
Copy link
Copy Markdown
Contributor

Description of the problem from the Compiler hacking wiki:

Currently, you cannot use _ in recursive bindings:

let rec _ = () and x = 3;;
Characters 8-9:
  let rec _ = () and x = 3;;
          ^
Error: Only variables are allowed as left-hand side of `let rec'

This is because patterns aren't allowed there, only variables. Really _ should be considered a variable in this context.

What has been done

Just like with let bindings, _ is now allowed as a variable name in let rec bindings. As with let bindings, this construction is translated into a sequence.

A test has also been added to the test suite to make sure that _ bindings work and can produce effects. Evaluation order is not tested as it may be changed by flambda.

(* the GNU Lesser General Public License version 2.1, with the *)
(* special exception on linking described in the file LICENSE. *)
(* *)
(**************************************************************************)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please don't use headers in testsuite files. Especially headers with the wrong names...

@damiendoligez damiendoligez added this to the 4.04-or-later milestone Mar 2, 2016
* effect to avoid testing evaluation order. *)
let test =
let rec _ = p "1"; p "2"
and x = p "1\n2"; 3
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It'd be good to have the bindings used recursively in the test -- i.e. use x in the rhs of y and vice versa, and also use them both in the rhs of the _ bindings.

@yallop
Copy link
Copy Markdown
Member

yallop commented Mar 11, 2016

Could you please add an entry to Changes?

@OlivierNicole
Copy link
Copy Markdown
Contributor Author

After modifying the test as you suggested, it makes my code crash because of an unknown identifier, because of the way I translated "_"-bindings into a sequence. I am working on the problem. So this PR is faulty, sorry about that.

lukemaurer added a commit to lukemaurer/ocaml that referenced this pull request Jul 7, 2021
mshinwell pushed a commit to mshinwell/ocaml that referenced this pull request Jul 13, 2021
The decision logic is intended to be unchanged from the ancien régime,
with the exception that unrolling state is preserved between the
simplification of a recursive function's body and any re-simplification
that occurs due to inlining.
stedolan pushed a commit to stedolan/ocaml that referenced this pull request May 24, 2022
EmileTrotignon pushed a commit to EmileTrotignon/ocaml that referenced this pull request Jan 12, 2024
* add a carousel-style books component

* allow books to be navigated using a keyboard
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.

3 participants