Skip to content

Add lambdas with statements / local functions in imp syntax #562

@developedby

Description

@developedby

Currently, in imp syntax there is no way of writing lambdas that have statements in them.
We can raise them to proper top-level functions, but then any captured variables must be passed through additional arguments, which increases the overhead.
But the main problem is that it's very inconvenient to do so. Instead we'd like a way to write lambdas with statements like we can do in Fun syntax.

I'll propose two possible syntaxes here.

def foo:
  lambda x:
    ...
    return y
  return z

Here lambda x defines a block for a local anonymous function bound to variable x.
I thought of this as just a simple lambda/closure, that can freely capture variables and doesn't get lifted into a separate definition.

Another option is

def foo:
  def foo/local(a, b):
    ...
    return c
  return foo/local(x, y)

This defines a function that is raised as a top-level. Captured variables are passed as arguments implicitly through a use term, like this

def foo:
  use foo/local = foo__local_foo/local(captured1, captured2)
  ...
  return foo/local(x, y)

This allow local functions to be recursive, but adds an overhead compared to just a simple lambda. And if the lambda is affine, it will get floated anyway.

I don't know which is better, need to think more about it.
I think we don't need both syntaxes, but that could also be an option.

Also, note that either syntax could have either of the described behaviours, I just think that they sort of imply intuitively the behaviours i described.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or requestsyntaxAbout Bend's syntax

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions