-
Notifications
You must be signed in to change notification settings - Fork 42
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
Tracking control-flow linearity for effect handlers #1173
Merged
Merged
Changes from 57 commits
Commits
Show all changes
76 commits
Select commit
Hold shift + click to select a range
0adf01d
Some attempts on fixing issue 1161:
thwfhk 421c2a8
some tests for the usage of linear variables in handlers
thwfhk 769e047
clean up comments
thwfhk d204c5a
Fix a bug that params of parameterised handlers can escape the scope …
thwfhk 3207347
clean up comments
thwfhk 2ba1870
clean up comments
thwfhk cb11dea
deal with linear variables in the return clauses of handlers; add mor…
thwfhk 1d5a151
initial attempt to track control-flow linearity:
thwfhk 2f69150
more proof of concept implementation
thwfhk b38ed56
Fix and improve the naive implementation of tracking control-flow
thwfhk 8445815
Improve syntax and fix bugs:
thwfhk 68821d4
some minor updates
thwfhk 9e7b867
comments
thwfhk e99585b
merge with polymorphic operations
thwfhk b95faa7
Merge branch 'master' into control-flow-linearity
thwfhk 0d9599a
pass all the linear handler tests
thwfhk 24bb4c7
fix the unification of rows with subkinds
thwfhk 834f8e0
improve the choose_and_state examples
thwfhk aa5d797
syntax for lincase
thwfhk ecc51eb
fix the linearity checking of unbound continuation
thwfhk 003d517
a potential bug
thwfhk e087edc
remove some test files
thwfhk d45d20a
comment some unused functions
thwfhk bbac798
nullary operations & some lib function signatures
thwfhk 65e4b6d
fix typo in core/typeSugar.ml
thwfhk 712e229
remove dead code
thwfhk b6e19f9
remove dead code
thwfhk 25b5ec4
remove dead code in core/typeSugar.ml
thwfhk b5dfc75
Update core/types.mli
thwfhk f9885e4
use DeclaredLinearity.t instead of ad hoc boolean in the definition o…
thwfhk f8de147
wrap the linear continuation things in typeSugar.ml into a module
thwfhk b1a361d
add a flag track-control-flow-linearity
thwfhk 909c599
something
thwfhk 682a5c4
modify the pretty printing for effect rows
thwfhk 19d67dd
modify the pretty printing for function types
thwfhk a85105b
modify the parser and printer; try to keep the round-tripping property
thwfhk 445ae32
something
thwfhk c902696
a mess
thwfhk 626cbd2
some attempts to make default Any
thwfhk 20ced3e
add a test example
thwfhk e67293a
successfully make Any as the default of effect row vars
thwfhk 1e801bc
clean up some tests
thwfhk df3e053
fix a bug of the effect vars of curried functions
thwfhk 1b57295
fix some bugs (including the parsing of effect presence types)
thwfhk cc632ab
need to explicitly say Presence(Any)
thwfhk 91ae333
compatibility with old handler tests
thwfhk e9e3bf8
Merge branch 'cfl-linear-default' into control-flow-linearity
thwfhk c525c04
postpone something, but equivalent
thwfhk c9eba50
more printing compatibility
thwfhk b98378a
more tests
thwfhk 5796f26
Merge remote-tracking branch 'upstream/control-flow-linearity' into c…
thwfhk 2615823
add a readme
thwfhk 7b9a8c2
update the syntax of presence variables
thwfhk ed2c26c
some backward compatibility
thwfhk 6086c36
update the documentation
thwfhk 0235d3e
typo
thwfhk dd43b18
Merge remote-tracking branch 'upstream/master' into control-flow-line…
thwfhk 366aaaf
update document
thwfhk 0d3c5b9
Fixed a stupid and hard-to-find bug about flattening effect types, wh…
thwfhk 6b741ee
update the original handlers test
thwfhk 5d5fa5e
update the original typecheck_examples test
thwfhk 6d9c0fa
update tests and compatibility document
thwfhk 6ba486d
update tests and compatibility document
thwfhk 436bc16
trim trailing whitespaces
thwfhk f8e5218
remove some test comments
thwfhk 29a0605
add more test cases on usage of linear resources in handlers
thwfhk 55ab6c5
try to enable automatic xlin, but it seems to be not very useful
thwfhk 3728cee
fix a testcase
thwfhk 7202caa
add more CFL tests
thwfhk ee943ba
modify the type signatures of some library functions
thwfhk fc00331
fix a test case because APs are unlimited
thwfhk 0f10124
give a more flexible type to the prelude function fork
thwfhk 8336bf3
more tests
thwfhk 29e7eec
remove a redundant line
thwfhk 5413bca
more tests from the paper
thwfhk f17866c
remove a doc file
thwfhk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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,114 @@ | ||
# Control-Flow Linearity | ||
|
||
Enable the feature of tracking control-flow linearity by passing the | ||
flag `--track-control-flow-linearity`. It is usually used with | ||
`--enabled-handlers`. | ||
|
||
## New constructs | ||
|
||
New types and kinds: | ||
|
||
* `A =@ B` is the signature for linear operations (`A => B` for unlimited operations) | ||
* `Lin` is the kind for linear effect variables (`Any` for effect | ||
variables with no linear restriction) | ||
|
||
New terms: | ||
|
||
* `lindo L` invokes the linear operation `L` | ||
* `case <L =@ r> -> M` handles the linear operation `L` | ||
* `xlin` switches the current control flow to linear | ||
|
||
|
||
## Control flow | ||
|
||
We use the concept *control flow* to mean terms that are sequencing | ||
evaluated. Control flows carry effects that are invoked by the terms. | ||
In other words, the terms on the same control flow share effects. | ||
Since Links' effect system is based on row polymorphism, the effect | ||
types of all terms on the same control flow are unified. | ||
|
||
Function bodies introduce their own control flows. The computations | ||
being handled (the `M` in `handle M {...}`) also have their own | ||
control flows, which will be merged with the control flow outside | ||
after being handled. | ||
|
||
Control flows are unlimited by default. We are allowed use both | ||
unlimited and linear operations, but only unlimited variables. We can | ||
switch the current control flow to linear by invoking the keyword | ||
`xlin`. In a linear control flow, we are allowed to use both unlimited | ||
and linear variables, but only linear operations. Once the control | ||
flow is switched to linear, all invocations of unlimited operations in | ||
it (even before `xlin`) would cause type errors. | ||
|
||
## Examples | ||
|
||
1. We can mix linear and unlimited operations in an unlimited control | ||
flow. The anonymous effect variable `_` has kind `Any` by default | ||
which can be unified with any operations. | ||
|
||
``` | ||
links> fun() {do U; lindo L}; | ||
fun : () {L:() =@ a,U:() => ()|_}-> a | ||
``` | ||
|
||
2. We can only invoke linear operations in a linear control flow. A | ||
linear control flow allows the usage of the linear channel `ch`. | ||
Now the effect variable `_` has kind `Lin` explicitly, which can | ||
only be unified with linear operations of signatures `=@`. | ||
|
||
``` | ||
links> fun(ch:End) {xlin; lindo L; close(ch)}; | ||
fun : (End) {L:() =@ ()|_::Lin}~> () | ||
``` | ||
|
||
3. We can only handle linear operations using a linear handler | ||
(indicated by `=@` in the clause) which guarantees the continuation | ||
is used exactly once. | ||
|
||
``` | ||
links> handle ({xlin; lindo L + 40}) { case <L =@ r> -> xlin; r(2) }; | ||
42 : Int | ||
``` | ||
|
||
4. We can handle unlimited operations before merging with a linear | ||
control flow as long as we guarantee that they are all handled. | ||
Note that after handling, the presence variable of `Choose` and row | ||
variable are both linear. | ||
|
||
``` | ||
links> fun(ch:End) { xlin; close(ch); handle ({if (do Choose) 40 else 2}) {case <Choose => r> -> r(true) + r(false)} }; | ||
fun : (End) {Choose{_::Lin}|_::Lin}~> Int | ||
``` | ||
|
||
5. When writing explicit quantifiers, we must explicitly annotate the | ||
kinds of row variables using `e::Row(Lin)` or `e::Row(Any)`. If the | ||
subkind is not specified, it means `Lin` in order to be compatible | ||
with variants and records in Links. | ||
|
||
``` | ||
links> sig f:forall e::Row(Any). () {Get:() => Int|e}-> Int fun f() {do Get}; f(); | ||
f = fun : () {Get:() => Int|_}-> Int | ||
``` | ||
|
||
## Design choices | ||
|
||
It is necessary to have `xlin` for the same reason of having `linfun` | ||
in Links. For example, neither of the following functions has a more | ||
general type than the other one. | ||
|
||
``` | ||
links> fun(x) {lindo L; x}; | ||
fun : (a) {L:() =@ ()|_}-> a | ||
links> fun(x) {xlin; lindo L; x}; | ||
fun : (a::Any) {L:() =@ ()|_::Lin}-> a::Any | ||
``` | ||
|
||
## Compatibility | ||
|
||
* Compatible with all previous handler tests (except part of | ||
polymorphic operations and effect sugar). | ||
* Not entirely compatible with FreezeML, SessionFail, etc. | ||
* Passes all tests with the flag disabled, except | ||
* `!FAILURE: Operation polymorphism (2)` | ||
* `!FAILURE: Operation polymorphism (3)` | ||
* `!FAILURE: Typecheck example file examples/handlers/monadic_reflection.links` |
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
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
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Lets leave as-is for now.
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.
Though, I see that you have changed the signature of
SessionFail
below... I would be inclined not to change any of the session exceptions stuff.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.
I have to change the
SessionFail
operation to be linear to make the functions inprelude.links
well-typed. Otherwise we would get a type error because we use the unlimited operationSessionFail
with linear channels. I also need to change some signatures of functions inlib.ml
, because by defaulte::Row
ise::Row(Unl,Any)
, which can only be unified with linear operations.This is also a challenge for hiding this feature behind a new flag; it is not entirely compatible with the current Links syntax.
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.
I see... The settings logic does allow you specify transitive dependencies, so if session exceptions are toggled, then you can forcibly toggle linear continuations too.
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.
Actually it is fine to not toggle control_flow_linearity, but we still need to make sure that the existing library code is compatible with it. FOr example, using unlimited row variable
e::Row(Any, Any)
by default and use the linear operation signature=@
forSessionFail
.