Skip to content

Instantly share code, notes, and snippets.

@ColmBhandal
Created April 8, 2022 09:13
Show Gist options
  • Save ColmBhandal/c0fe1485b1ffa6e98ebb60de5f3f7c91 to your computer and use it in GitHub Desktop.
Save ColmBhandal/c0fe1485b1ffa6e98ebb60de5f3f7c91 to your computer and use it in GitHub Desktop.
Slightly alternative fork/join semantics allowing a forked thread's return value to be brought back to the main thread
module LESSON-17-B
imports INT
imports BOOL
imports ID-SYNTAX
syntax Stmt ::= Id "=" Exp ";" [strict(2)]
| "return" Exp ";" [strict]
syntax Stmts ::= List{Stmt,""}
syntax Exp ::= Id
| Int
| Exp "+" Exp [seqstrict]
| "spawn" "{" Stmts "}"
| "join" Exp ";"[strict]
configuration <threads>
<thread multiplicity="*" type="Map">
<id> 0 </id>
<k> $PGM:K </k>
</thread>
</threads>
<state> .Map </state>
<next-id> 1 </next-id>
rule <k> X:Id => I:Int ...</k>
<state>... X |-> I ...</state>
rule <k> X:Id = I:Int ; => . ...</k>
<state> STATE => STATE [ X <- I ] </state>
rule <k> S:Stmt Ss:Stmts => S ~> Ss ...</k>
rule <k> I1:Int + I2:Int => I1 +Int I2 ...</k>
rule <thread>...
<k> spawn { Ss } => NEXTID ...</k>
...</thread>
<next-id> NEXTID => NEXTID +Int 1 </next-id>
(.Bag =>
<thread>
<id> NEXTID </id>
<k> Ss </k>
</thread>)
rule <thread>...
<k> join ID:Int ; => I ...</k>
...</thread>
(<thread>
<id> ID </id>
<k> return I:Int ; ...</k>
</thread> => .Bag)
syntax Bool ::= isKResult(K) [function, symbol]
rule isKResult(_:Int) => true
rule isKResult(_) => false [owise]
endmodule
@ColmBhandal
Copy link
Author

Notable changes are as follows:

  1. Join now includes strict heat/cool evaluation: | "join" Exp ";"[strict]
  2. The spawn rule now matches a k-sequence, rather than only matching the full contents of the k-cell, as specified by the three dots at the end: <k> spawn { Ss } => NEXTID ...</k>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment