Environment closures - #7
Merged
Merged
Conversation
The code of this commit is a bit messy, as explained in the comments. * Add 'is_used' member to 'Env', for storing whether or not it's being used somewhere else. * Unmark environments in 'gc_unmark_all'. * Mark environments in 'gc_mark_expr'. * Don't collect expressions if their associated environment is still in use.
This commit doesn't currently work, because we still work with copies.
From 'gc_mark_expr'.
Owner
Author
|
Even with this PR, at commit 00e2a2d, the Y combinator crashes the program by trying to access a freed environment. CodeY combinator: ;; For more information on the Y combinator, see my blog article:
;; https://8dcc.github.io/programming/understanding-y-combinator.html
(define Y
(lambda (f)
((lambda (x) (f (lambda (n) ((x x) n))))
(lambda (x) (f (lambda (n) ((x x) n)))))))
(define fact-generator
(lambda (self)
(lambda (n)
(if (equal? n 0)
1
(* n (self (- n 1)))))))
(define fact
(Y fact-generator))
(fact 5)Valgrind backtrace: |
Owner
Author
|
The issue mentioned in the previous comment was fixed in commit 5758c96. |
8dcc
added a commit
that referenced
this pull request
Feb 16, 2025
Work with references, instead of always returning copies. This change was possible thanks to #5 (which needed #4), #6 and #7. Some changes were merged into this 'copy-references' branch in #7. Major changes: * Remove most calls to 'expr_clone', for working with references instead of copies. * Return or store references in 'car', 'cdr', 'cons', 'nth', 'quote' and 'backquote' primitives. * Add proper environment closures, setting the parent environments whenever a lambda is created, not when it's called. See #7. * Change how GC handles lambdas, add 'is_used' member to 'Env'. Mostly included in #7, but not completely (see 5758c96). * Don't mark globals ('g_nil', 'g_tru' and 'g_debug_trace_list') for garbage collection, since their references are being stored in the environment. Minor changes: * Add 'gc_mark_env' and 'gc_mark_env_and_parents' static functions to 'garbage_collector.c'. * Add an 'Env*' parameter to 'lambdactx_init', remove it from 'lambdactx_eval_body'. * Don't make the 'Expr*' parameter of 'env_bind' and 'env_bind_global' constant. * Add closure test to 'test/lambdas.lisp'. See #7.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add support for environment closures. This pull request needs the changes of the
copy-referencesbranch in order to work.The environment of a lambda used to be created when the lambda was created, but its parent environment was not set until the lambda was called. This was a problem because, in the following code:
The parent environment of the inner lambda was set whenever it was called, so in that example
bwas not declared, and the last expression returned an error.After this PR, the parent environment is set when the lambda is created, so the new environment layout is:
Rather than:
The
applydiagram needs to be updated.Changes
is_usedmember toEnv, for storing whether or not it's being used somewhere else.gc_unmark_all(by settingis_used).gc_mark_expras used.pool_item_is_gcmarked, movepool_item_is_freetoexpr_pool.h, make arguments ofpool_item_flagsconstant.