Last active
August 25, 2024 09:09
-
-
Save aswinsekar/cf41e2104613178db25b1385ddaa7eb6 to your computer and use it in GitHub Desktop.
Routing code for Flourish book - https://flourishbooks.app/credits/ credits page
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
## Redex - Reducible Expressions | |
![[Pasted image 20231230121928.png]] | |
## Continuation | |
- The Context in which the `redex` is evaluated | |
- An expression within a whole | |
- The place is which a redex's value is `returned to` | |
- The rest of the program | |
### Why care about continuation? | |
Evaluation is extremely regular | |
1) Split the redex and continuation | |
2) Reduce the redex | |
3) Substitute the redex into the continuation | |
4) Repeat | |
Why is the continuation itself interesting? | |
Compiler writers care about the continuation! | |
Most programmers don't have much reason to, most of the time! | |
... but what about operators that use different rules? | |
Example: the below image | |
![[Pasted image 20231230122123.png]] | |
## Delimited Continuations | |
![[Pasted image 20231230122441.png]] | |
## Notation - #ref | |
#### Reduces | |
> $A \longrightarrow B$ | |
The above notation is read as A reduces to B | |
Example: | |
- $not(false) \longrightarrow true$ | |
- $not(true) \longrightarrow false$ | |
- if $true$ then $e_1$ else $e_2 \longrightarrow e_1$ | |
- if $false$ then $e_1$ else $e_2 \longrightarrow e_2$ | |
What happens with | |
if $not(false)$ then 1 else 2 ? | |
![[Pasted image 20231230123834.png]] | |
#### Some arbitrary Continuation | |
Instead of | |
$not(false) \longrightarrow true$ | |
We will write | |
> $E[not(false)] \longrightarrow E[true]$ | |
$E[x]$ denotes `plugging the hole` in $E$ with $x$ | |
$E = if \bullet then\;1\;else\;2$ | |
$x = not(false)$ | |
$E[x] = if\;not(false)\;then \; 1\;else\;2$ | |
### Why the notation is needed? | |
Now, the catch expression can be rewritten as follows | |
$E_1[catch\{E_2[throw(v)],f\}] \longrightarrow E_1[f(v)]$ | |
Lots of operations can be described in a similar way! | |
Coroutines in Python -> Generators in python | |
## First class Continuations | |
First class x means, if x is treated as variables like any other values. For example, functions in JS are first class functions | |
First class continuation is a continuation reified as a function. | |
Oldest way of doing first class continuation is `call_cc` - Call with current continuation | |
> $E[call\_cc(f)] \longrightarrow E[f((x) \to E[x])]$ | |
Call_cc is more complex, when used in real world continuation cases | |
Delimited versions of call_cc is **prompt/control** | |
> $E_1[prompt\{E_2[Control(f)] \}] \longrightarrow E_1[f((x) \to E_2[x])]$ | |
![[Pasted image 20231231101547.png]] | |
![[Pasted image 20231231101638.png]] | |
## Further Reading | |
- https://cs.ru.nl/~dfrumin/notes/delim.html#:~:text=Prompt%2Fcontrol&text=where%20the%20evaluation%20context%20E,a%20free%20variable%20in%20E.&text=In%20this%20case%20the%20continuation,%2B2*3%3D7. | |
- https://www.khoury.northeastern.edu/home/amal/course/7480-s12/delim-control-notes.pdf | |
- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment