I'll keep this as light and informal as possible. To stay on point, I'll ignore most of the ES features-- no mutation (so we don't have to model the heap / object graph), and not even scope chains. For a faithful and precise model, we'd need all of these things. But for design sketches, I can afford to be sloppier.
Stacks and activations
I'll model the stack as a non-empty sequence of function activations:
S ::= A | S(A)
For the curious, what I just did was -- informally -- define evaluation contexts for ES.
If that was a little too opaque, here's an example. Let's say we have a function
If we call foo(42), then at first, our current code is f(42); and our activation isfunction foo(x) { f(x); g(x, 2 * x, null); return x + 4; }
When we're about to evaluate the second argument to g, then the current code is 2 * 42 and the current activation is{ {()} g(42, 2 * 42, null); return 42 + 4; }
When we get to the return statement, current code is return 42 + 4; and the current activation is{ g(42, (), null); return 42 + 4; }
Notice how after we execute some code, it's gone from the activation; the activation just needs to keep track of the code we have left to run.{ {()} }
For talking about the activation and the current code together, I'll write A(expr) or A{stmt}. This is what people call "plugging" code into the hole of a context. Essentially, it's just a modeling trick that makes it convenient to call out where the current code pointer is, but it also turns out to be really natural for talking about first-class control operators-- because it explicitly models the very things we want to manipulate.
Operational semantics
The above is more or less enough to start writing simple transition rules that describe how programs run, e.g.:
S(A{return val;}) → S(val)In English, this rule says: "in a stack starting with S and with activation A on top, when the next code to execute is the statement return val; (for some value val), then remove the current activation and place val in the expression hole of the stack."
One thing to make clear: this transition arrow describes a runtime state transition of the program, not a compile-time transformation.
Starting with shift and reset
Here's how a design based on shift and reset might work, specified in one line:
S(A(val->(val1, ..., valn))) → S(val(val1, ..., valn, function(x) A(x)))The special syntax of a capturing function call comes is from NarrativeJS. The transition rule describes several things:
- After evaluating the callee and its arguments, the operator immediately aborts the current activation and saves it in a function.
- Next, it calls the callee with its arguments along with the captured continuation value as an additional argument.
- If the captured continuation is called, it executes the captured function activation, with its new argument plugged into the hole of that activation.
A pretty clear violation of principle of least astonishment.function mightFail(k) {
// ...
throw "boo!";
}
function main() {
try {
mightFail->()
}
catch (x) {
return 42
}
}
main() // uncaught exception: "boo!"
When to abort
Having the semantics in such a concise format makes it easy to tweak and experiment with. Let me modify the above semantics in the following way: instead of aborting before we call the callee, let's abort the activation after we return from the call. That way, if it throws exceptions, they'll be caught by any handlers in A.
We'll have to invent an expression form that aborts the current function activation after evaluating an expression. This is pretty much just like return, exception it's an expression form.
S(A(val->(val1, ..., valn)))Actually, we could do this without positing a magic RETURN operator if we have something like the newly-proposed let expressions, which allow you to execute statements inside an expression. Then we'd have:
→ S(A(RETURN val(val1, ..., valn, function(x) A(x))))
S(A(val->(val1, ..., valn)))So that's a brief demo of using evaluation contexts and operational transition rules to sketch the semantics of continuation operators.
→ S(A(let (x = val(val1,..., valn, function(x) A(x))) {return x;}))
Edit: I forgot to mention, it's easy enough to specify the behavior of RETURN:
S(A(RETURN val)) → S(val)As I say, just like return statements, but as an expression form. To be clear, this is just a specification mechanism, not something I'd suggest as a language feature.
13 comments:
S(A(RETURN val)) → S(val)
It seems like that would skip finally blocks.
I found this post pretty hard to follow, but I think I got through it all...
It seems like that would skip finally blocks.
In my efforts to keep things simple, I left try, catch, and finally out of the model. Which probably wasn't the best move, since I was also talking about exception handling. Sorry 'bout that.
I found this post pretty hard to follow, but I think I got through it all...
Yeah, I debated for a while, and decided to go ahead and write it, but it's pretty hard to get the basics of evaluation contexts and operational semantics into a blog post or two. At the same time, I've found these techniques so useful for my own thinking, I wanted to try and convey them. Sounds like I wasn't too successful, though.
Nice blog..! I really loved reading through this article. Thanks for sharing such a
amazing post with us and keep blogging... iot training in chennai | iot training in chennai quora | iot training and placement in chennai | iot training center in chennai | best iot training centre in chennai
Outstanding blog thanks for sharing such wonderful blog with us ,after long time came across such knowlegeble blog. keep sharing such informative blog with us.
machine learning training in chennai
best training institute for machine learning
machine learning tution in chennai
artificial intelligence and machine learning course in chennai
Really useful information. Thank you so much for sharing.It will help everyone.Keep Post. RPA training in chennai | RPA training in Chennai with placement
Thanks for such a great article here. I was searching for something like this for quite a long time and at last, I’ve found it on your blog. It was definitely interesting for me to read about their market situation nowadays.angularjs best training center in chennai | angularjs training in velachery | angularjs training in chennai | angularjs training in omr
Get the most advanced UiPath Course by Professional expert. Just attend a FREE Demo session.
call us @ 9884412301 | 9600112302
RPA training in chennai | UiPath training in velachery
Do you require HP printer setup for your mac operating system? Is your printer driver not suitable for macOS? Then visit the 123.hp.com/setup to get the software and driver for better functioning of your printer. You can also call our expert HP support team for services.
It’s not a tough task to activate Roku. Understand the Roku activation steps. Connect the necessary hardware. It's the power and HDMI, USB cable that is required. Sign in to Roku account and move to the page Roku.com/link. To begin streaming, visit the channel store and start the channel search. Select and then add the channel to proceed. Want to know How to Activate Roku? Use the customer support number available on our webpage.
If you're looking to burn fat then you absolutely have to jump on this brand new custom keto plan.
To create this service, licensed nutritionists, fitness couches, and top chefs united to develop keto meal plans that are efficient, decent, money-efficient, and delicious.
Since their first launch in 2019, thousands of people have already transformed their figure and well-being with the benefits a good keto plan can give.
Speaking of benefits; clicking this link, you'll discover eight scientifically-certified ones offered by the keto plan.
Read your blog, Excellent content written on "Thinking about continuations"
If you are looking for RPA related job with unexpected Pay, then visit below link
RPA Training in Chennai
RPA course in Chennai
RPA course
RPA Training in Velachery
RPA Training
Robotic Process Automation Training
Robotic Process Automation Training in Chennai
Robotic Process Automation Courses
RPA Classes in Chennai
Robotic Process Automation Certification
Thanks for the informative post. Gathered lots of information here and do share more.
AngularJS Training in Chennai
AngularJS Online course
AngularJS Online Training
AngularJS course in Chennai
Best Hacking Books
Ethical Hacking Books
Ethical Hacking course in Chennai
Salesforce Training in Chennai
RPA Training in Chennai
An instruction manual for Printer 123 hp printer setup, driver download, software install, wireless setup, USB connection, and troubleshooting issues.
Post a Comment