-
Notifications
You must be signed in to change notification settings - Fork 6
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
#57: cage recursion #59
Open
levBagryansky
wants to merge
6
commits into
objectionary:master
Choose a base branch
from
levBagryansky:57_cage-recursion
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3ea0d8e
#57: cage recursion
levBagryansky b5c2fb8
#57: Use the valid link
levBagryansky b4b857c
#57: Edited `cage-contains-int` example
levBagryansky ac9721a
#57: `cage`
levBagryansky 9e9eaeb
#57: typo with case instead of cage
levBagryansky e403067
#57: Refactored describing of `PhTracedEnclosure` class
levBagryansky File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
--- | ||
layout: post | ||
date: 2024-03-18 | ||
title: "Recursion in cage" | ||
author: levBagryansky | ||
--- | ||
|
||
The main difference between `cage` and `memory` is how the object is written. | ||
When dataizing, `memory.write` datazies its argument as well. Whereas | ||
`cage.write` just saves a reference to this object. As a result, the dataization | ||
of the `cage` may turn out to be recursive. Unfortunately, this error is often not obvious. | ||
|
||
In this post we will tell you how we solved this problem. | ||
|
||
<!--more--> | ||
|
||
# A little about `cage` | ||
|
||
In [this](https://news.eolang.org/2023-08-04-storing-objects-formed-differently-into-cage.html) post we | ||
raised the topic of `cage`. There we told how to write into `cage`, and what limitations there are - | ||
objects of the same `forma` can be written into one cage. | ||
Here is a simple example of using a `cage`: | ||
|
||
```eo | ||
# Cage conatains int. | ||
[] > cage-contains-int | ||
cage 0 > my-int | ||
eq. > @ | ||
seq | ||
* | ||
my-int.write 42 | ||
my-int | ||
42 | ||
``` | ||
|
||
But you might also accidentally write `cage` inside `cage`, leading to infinite recursion. | ||
```eo | ||
[] > catches-stack-overflow | ||
cage > cge | ||
0.plus | ||
seq > @ | ||
* | ||
cge.write | ||
0.plus cge | ||
cge | ||
``` | ||
In order to provide everywhere the same `forma` we use `0.plus`. | ||
When `cge` is dataizing it dataizes `0.plus cge`. It takes attribute `plus` from object `0`. `int.plus` | ||
tries to dataize its argument `cge` inside. We have achieved our goal: infinite recursion is obtained. | ||
The first attempt is to detect such cases even in compilation. But generally speaking, the presence | ||
of `cage` inside `cage` is not a mistake, because such a program may well be correct. | ||
Dataization of `cage` can several times pas through the `cage` and only then return some value. | ||
This eo illustrates such scenario (it does not work with latest version but idea of deep `cage` is clear): | ||
```eo | ||
# correct-cage-recursion. | ||
[] > correct-cage-recursion | ||
cage > cge | ||
if. | ||
i.as-bool | ||
TRUE | ||
seq | ||
* | ||
i.write | ||
TRUE | ||
$.cge | ||
memory > i | ||
FALSE | ||
cge > @ | ||
|
||
``` | ||
Therefore, it is necessary to detect recursion in runtime somehow. Until recently `EOcage::lambda` | ||
was the following: | ||
```java | ||
@Override | ||
public Phi lambda() { | ||
return this.attr("enclosure").get(); | ||
} | ||
``` | ||
Don't worry about this piece of code from our source, it's easy to understand: During `cage` dataization, | ||
it returns a reference to the object stored in it- `enclosure`. In order to trace how many times | ||
`cage` dataization uses itself we wrap `enclosure` in the `PhTracedEnclosure`: | ||
```java | ||
@Override | ||
public Phi lambda() { | ||
return new PhTracedEnclosure(this.attr("enclosure").get(), this); | ||
} | ||
``` | ||
`PhTracedEnclosure` keeps `enclosure` and `cage` that it traces. It also has something like **static** | ||
`Map<Phi, Integer>`, where it counts how many times `enclosure` was used: It increments the counter of `cage` | ||
before retrieving its attributes and decrements it after successful retrieval. Therefore, in the case of recursion, | ||
the counter doesn't decrease, but only increases. When the counter reaches a certain threshold, an exception is thrown. | ||
|
||
The value of such a threshold is called `"EO_MAX_CAGE_RECURSION_DEPTH"`, and it is a java property | ||
which you can set by yourself. We chose `100` to be default value. Then `cage` can be | ||
recursive enough, but it will detect problem before throwing of `StackOverflowError`. You can | ||
set environment variable via `-D` flag. | ||
|
||
Keep exploring EO, and remember, there's always more to discover. Stay connected for future updates, | ||
and We look forward to catching up with you soon! |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@maxonfjvipon this is more illustrating example, not working code,
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@levBagryansky why doesn't it work? Everything seems fine
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@maxonfjvipon I get an error EOcorrect_cage_recursionTest.works:124->PhDefault.attr:250->PhDefault.attr:248->PhDefault.attr:250 » Ex D="Error at "EOorg.EOeolang.EObytes$EOas_bool#φ" attribute; caused by Can't #copy() package object 'bytes'" here and don't know why