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

Iterator syntax #2126

Merged
merged 10 commits into from
May 30, 2023
Merged

Iterator syntax #2126

merged 10 commits into from
May 30, 2023

Conversation

lukaszcz
Copy link
Collaborator

@lukaszcz lukaszcz commented May 24, 2023

A function identifier fun can be declared as an iterator with

syntax iterator fun;

For example:

syntax iterator for;
for : {A B : Type} -> (A -> B -> A) -> A -> List B -> List A;
for f acc nil := acc;
for f acc (x :: xs) := for (f acc x) xs;

Iterator application syntax allows for a finite number of initializers acc := a followed by a finite number of ranges x in xs. For example:

for (acc := 0) (x in lst) acc + x

The number of initializers plus the number of ranges must be non-zero.

An iterator application

fun (acc1 := a1; ..; accn := an) (x1 in b1; ..; xk in bk) body

gets desugared to

fun \{acc1 .. accn x1 .. xk := body} a1 .. an b1 .. bk

The acc1, ..., accn, x1, ..., xk can be patterns.

The desugaring works on a purely syntactic level. Without further restrictions, it is not checked if the number of initializers/ranges matches the type of the identifier. The restrictions on the number of initializers/ranges can be specified in iterator declaration:

syntax iterator fun {init: n, range: k};
syntax iterator for {init: 1, range: 1};
syntax iterator map {init: 0, range: 1};

The attributes (init, range) in between braces are parsed as YAML to avoid inventing and parsing a new attribute language. Both attributes are optional.

@lukaszcz lukaszcz added this to the 0.4 - Prague milestone May 24, 2023
@lukaszcz lukaszcz self-assigned this May 24, 2023
@lukaszcz lukaszcz force-pushed the syntax-iterators-mappers branch 2 times, most recently from 293c0d9 to 4d5a9aa Compare May 24, 2023 14:59
@lukaszcz lukaszcz force-pushed the syntax-iterators-mappers branch from e26f8fc to e72a670 Compare May 25, 2023 09:55
@lukaszcz lukaszcz marked this pull request as ready for review May 25, 2023 11:30
Copy link
Collaborator

@janmasrovira janmasrovira left a comment

Choose a reason for hiding this comment

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

We should have a test that shows the syntax for multiple initializers/ranges.
I propose that the syntax for that follows this schema:

fun (acc1 := a1; .. ; accn := an) (x1 in b1; .. ; xk in bk) body

@janmasrovira
Copy link
Collaborator

@lukaszcz
Copy link
Collaborator Author

Yes, there is a possibility of ambiguity, I realise this.

My proposal is for the iterators to take priority.

If one really wants to have a function application with named non-implicit arguments at the beginning, then one can put it in parentheses

(fun) (arg := a)

This would not be parsed as an iterator because an iterator requires an identifier at the front.

This would perhaps be inconvenient if used often, e.g., for the record syntax. But we need something like the with notation anyway that I proposed in the comments (for record update and/or construction), because otherwise record update is already ambiguous with named implicit arguments.

@lukaszcz
Copy link
Collaborator Author

@lukaszcz
Copy link
Collaborator Author

With a bit more effort in the parser/scoper, one could also recognise when an identifier is an iterator and prioritise iterator syntax only then. But this would require reworking the current iterator parsing concept (to have separate "initializer" and "range" atoms and build (or not) an iterator application only during scoping).

@lukaszcz lukaszcz marked this pull request as draft May 29, 2023 17:19
@lukaszcz lukaszcz force-pushed the syntax-iterators-mappers branch from 124b1c9 to de86305 Compare May 30, 2023 09:50
@lukaszcz lukaszcz marked this pull request as ready for review May 30, 2023 09:50
@lukaszcz lukaszcz requested a review from janmasrovira May 30, 2023 09:50
@lukaszcz lukaszcz force-pushed the syntax-iterators-mappers branch from de86305 to d90c862 Compare May 30, 2023 09:51
@lukaszcz lukaszcz merged commit e9284b3 into main May 30, 2023
@lukaszcz lukaszcz deleted the syntax-iterators-mappers branch May 30, 2023 13:30
@jonaprieto jonaprieto modified the milestones: 0.4 - Prague, 0.3.5 Jun 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Special syntax for folds, maps, etc
3 participants