Experimental support for linear types #192
Merged
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.
This commit adds the possibility to mark a class (passive or active) as
linear
. This means that all references to an object of that type mustbe treated linearly, i.e. cannot be copied. Reference transfer is done
using the
consume
keyword. Here is an example program that shows mostof the current functionality:
The checking that linearity is preserved is done using an additional
pass, implemented in the file
src/typechecker/Capturechecker.hs
. Thiskeeps the changes made to
Typechecker.hs
to a minimum, which is nice.Basically, the capturechecker figures out where references are captured
and when a linear reference is safe to capture.
The linear references should be interoperable with all the features
currently in Encore, but testing is very welcome! I will keep this in a
feature branch (and try to keep it rebased on
master
) until theexclusive capabilities find their way into the type system.
It should be noted that consumes are currently not atomic (the value is
read and nullified sequentially), but this shouldn't matter in a
race-free program. Linear streams are possible but not very useful,
since both
get
andgetNext
require you to consume the stream. Forany useful patterns we would need a way to
get
and move the streamforward in one go (maybe
consume s
should be the way to do this?).