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

feat(compiler)!: Partial function application #2091

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

alex-snezhko
Copy link
Member

Partial function application

let add = (x, y) => x + y
let add1 = partial add(1, _)
assert add1(2) == 3

let printValues = (x, y, z=3) => {
  print(x)
  print(y)
  print(z)
}
let printSome = partial printValues(y=2, _, z=_)
printSome(1) // prints 1, 2, 3

Closes #402

@alex-snezhko
Copy link
Member Author

alex-snezhko commented Apr 8, 2024

TODO:

  • Make precedence of partial tighter to allow x |> partial add(_, y) without parentheses around the partial expression
  • Decide if this feature should be supported for infix functions - doesn't seem particularly useful so will not do in this PR

raise(
SyntaxError(
loc,
"To use partial application, prefix the function call with `partial`.",
Copy link
Member Author

@alex-snezhko alex-snezhko Apr 8, 2024

Choose a reason for hiding this comment

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

This was kind of a bandaid fix around me being a noob with LR(1) parsers and not being able to figure out how to create separate rules for partial and total applications without reduce/reduce conflicts. If anyone has insight on how to do this please let me know

@ospencer ospencer changed the title feat(compiler): Partial function application feat(compiler)!: Partial function application Apr 19, 2024
@ospencer
Copy link
Member

Marked this as breaking because of the new keywords.

@alex-snezhko alex-snezhko marked this pull request as draft May 9, 2024 14:08
@alex-snezhko alex-snezhko marked this pull request as ready for review July 20, 2024 04:33
Copy link
Member

@spotandjake spotandjake left a comment

Choose a reason for hiding this comment

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

I had a few questions mostly on the dummy locations, looks great overall though.

| ArgumentHole(_) =>
raise(
SyntaxError(
loc,
Copy link
Member

@spotandjake spotandjake Nov 26, 2024

Choose a reason for hiding this comment

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

Should we point to the arg.ppaa_loc? I feel like this error might be a little annoying as it isn't immediately clear that the _ hole is specific to partial application, at least that way the position would be telling you hey this argument is the issue specifically, an alternate here would be Hole patterns are only allowed under partial application, prefix the function call with partial. which is a bit more specific and searchable.

};

(
Expression.ident(
Copy link
Member

Choose a reason for hiding this comment

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

Can we give all this stuff the location from the arg.


let body =
Expression.apply(
~loc=Location.dummy_loc,
Copy link
Member

Choose a reason for hiding this comment

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

Should this take the location from the func?


let new_func_params_pat =
Pattern.tuple(
~loc=Location.dummy_loc,
Copy link
Member

Choose a reason for hiding this comment

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

Same as above.

switch (l) {
| Default(_) =>
// omitted optional argument
(l, option_none(env, instance(env, ty), Location.dummy_loc))
Copy link
Member

Choose a reason for hiding this comment

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

Should this take the location from ty?

let pf = partial f(1, _, y=_)
pf(3, 2)
|},
"1\n2\n3\n",
Copy link
Member

Choose a reason for hiding this comment

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

as a sanity check can we just add a print("0") between the partial application and the actual application of the function i think it's obvious that it will only call the function afterwards but I feel like we should still double check that.

@@ -385,4 +385,173 @@ truc()|},
|},
"which has a default value, but the matching argument does not.",
);

assertRun(
Copy link
Member

Choose a reason for hiding this comment

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

Should we have a test where a module provides a partially applied function, also maybe a clear test where a function returns a partially applied function. something like:

let f = (x, y) => {
  let t  = (a, b) => x + a + b
  let y = partial (_, y)
  y
}
print(f(1, 2)(3)) // 6

this ensures that we don't break closures and ensures that we are not losing the partial information when returning from a scope (The way this is implemented I don't think those are major concerns but they seem like good sanity checks. I think your test with the pipeline operator somewhat tests this but doesn't test closures.

switch (label) {
| Labeled(name)
| Default(name) => name
| Unlabeled => mknoloc("$arg" ++ string_of_int(i))
Copy link
Member

Choose a reason for hiding this comment

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

I was having a little trouble visualizing what is actually happening here are we building a tuple from the arguments and then passing the tuple around and at the end passing the tuple values to the function when its fully applied? or did I misunderstand the abstraction here?

assertRun(
"partial13",
{|
let (|>) = (x, f) => f(x)
Copy link
Member

Choose a reason for hiding this comment

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

Should we add this function to pervasives? I feel like the moment we add partial application we are going to be asked for a pipeline operator soon after we add this.

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.

Lang: partial operator
3 participants