Skip to content
This repository has been archived by the owner on Jan 8, 2024. It is now read-only.

20230110 modern lambda #109

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

20230110 modern lambda #109

wants to merge 79 commits into from

Conversation

prozacchiwawa
Copy link

@prozacchiwawa prozacchiwawa commented Jan 11, 2023

Create a new form

(lambda ((& captures ...) arguments) (code))

Which captures the items in captures from the environment and creates runnable code taking (arguments) as arguments.

In this code:

(mod (add-number L)

  (include *standard-cl-21*)

  (defun map (F L)
    (if L
      (c (a F (list (f L))) (map F (r L)))
      ()
      )
    )

  (map
    (lambda ((& add-number) number) (+ add-number number))
    L
    )
  )

A runnable (with the 'a' operator) lambda is emitted by the lambda form, which captures add-number from the environment. When run by 'map' with another argument (here filling 'number'), it can use both the captured arguments and the arguments given when it is run.

Run with (5 (1 2 3 4)) gives (6 7 8 9)

You can introduce bindings for capture using let:

(mod (add-number L)

  (include *standard-cl-21*)

  (defun map (F L)
    (if L
      (c (a F (list (f L))) (map F (r L)))
      ()
      )
    )

  (map
    (let ((A (* add-number 2)))
      (lambda ((& A) number) (+ A number))
      )
    L
    )
  )

Run with (5 (1 2 3 4)) gives (11 12 13 14)

@emlowe emlowe requested a review from arvidn February 6, 2023 17:10
@prozacchiwawa
Copy link
Author

based on use, a bug fix might be needed. looking at a test case.

@prozacchiwawa
Copy link
Author

not a bug, added some test cases.

src/compiler/clvm.rs Outdated Show resolved Hide resolved
src/compiler/frontend.rs Outdated Show resolved Hide resolved
src/compiler/frontend.rs Show resolved Hide resolved
src/compiler/lambda.rs Outdated Show resolved Hide resolved
// M = (mod ((captures) arguments) (body))
// (list 2
// (c 1 (mod ((captures) arguments) body))
// (list 4 (c 1 (@ 2)) (list 4 (c 1 compose_captures) @))
Copy link

Choose a reason for hiding this comment

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

the 1s and 2s here refer to the quote- and apply- opcodes, right?
if so, it might be easier to understand by using #q and #a instead

Copy link
Author

Choose a reason for hiding this comment

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

Admittedly, I find the mixing more confusing, but will change that.

// Test that leaving off the lambda captures causes bare words for the
// requested values to find their way into the output and that having
// the capture catches it. This shows that uses of uncaptured words
// are unencumbered.
Copy link

Choose a reason for hiding this comment

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

perhaps I misunderstand what this means, but I would hope that the compiler would catch my mistake if I misspell a variable name in the lambda body. having an explicit capture seems like an opportunity for us to be stricter about this

Copy link
Author

Choose a reason for hiding this comment

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

As we discussed earlier, that wasn't a change I felt i could make at this point. Since it's been very contentious I will speed through the changes needed to be fully strict.

Copy link
Author

Choose a reason for hiding this comment

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

This upcoming pr allows us to get to strictness in macro output since it allows precise control of the form the output takes, as well as other improvements.

https://github.com/Chia-Network/clvm_tools_rs/pull/134

It's a precondition for being fully strict.

Copy link
Author

Choose a reason for hiding this comment

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

.to_string();
let res = run_string(&prog, &"(((2 3) (4 9)))".to_string()).unwrap();
assert_eq!(res.to_string(), "242");
}
Copy link

Choose a reason for hiding this comment

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

I appreciate these tests. I think it would make sense to have negative tests as well. it's important compilation fails when it should

Copy link
Author

Choose a reason for hiding this comment

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

It's been a bit hard because not much actually is meant to fail in chialisp and I often discover things people want that are questionable or should fail to me, but I will set those bounds as well as I can.

@prozacchiwawa
Copy link
Author

now that other required features have been merged, the test suite for chia-gaming is now in here as a fairly comprehensive regression test.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants