Skip to content

Commit

Permalink
Add speculation examples
Browse files Browse the repository at this point in the history
  • Loading branch information
sampsyo committed Nov 15, 2020
1 parent 62db209 commit 8027d54
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions docs/lang/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,36 @@ It is an error to commit or abort outside of speculation.
It is not an error to perform side effects like `print` during speculation, but it is probably a bad idea.


Examples
--------

Committing a speculative update makes it behave like normal:

v: int = const 4;
speculate;
v: int = const 2;
commit;
print v;

So this example prints `2`.
However, when a guard fails, it rolls back any modifications that happened since the last `speculate` instruction:

b: bool = const false;

v: int = const 4;
speculate;
v: int = const 2;
guard b .failed;
commit;

.failed:
print v;

The guard here fails because `b` is false, then `v` gets restored to its pre-speculation value, and then control transfers to the `.failed` label.
So this example prints `4`.
You can think of the code at `.failed` as the "recovery routine" that handles exceptional conditions.


Interpreter
-----------

Expand Down

0 comments on commit 8027d54

Please sign in to comment.