Pipe-calculus: Difference between revisions

Line 22: Line 22:
To get a taste of pipe-calculus, consider a program that adds two natural numbers encoded in [[wikipedia:Unary_numeral_system|unary numeral system]]. Suppose that we send both numbers over a stream as a series of <code>1</code>s terminated by an <code>end</code> symbol. Our example language consists of the following expressions.
To get a taste of pipe-calculus, consider a program that adds two natural numbers encoded in [[wikipedia:Unary_numeral_system|unary numeral system]]. Suppose that we send both numbers over a stream as a series of <code>1</code>s terminated by an <code>end</code> symbol. Our example language consists of the following expressions.


* <code>match s</code> expects that the next symbol of the input stream is <code>s</code>. On failure the current fork of the program is aborted.
* <code>match s</code> expects that the next symbol of the input stream is <code>s</code>. On failure the current branch of the program is aborted.
* <code>write s</code> appends the symbol <code>s</code> to the output stream.
* <code>write s</code> appends the symbol <code>s</code> to the output stream.
* <code>run x</code> runs a program given its name.
* <code>run x</code> runs a program given its name.
* <code>p ; q</code> runs <code>p</code> then runs <code>q</code> unless <code>p</code> fails.
* <code>p ; q</code> runs <code>p</code> then runs <code>q</code> unless <code>p</code> fails.
* <code>p | q</code> forks the execution of the program so that one fork runs <code>p</code>, another fork runs <code>q</code>, sharing the same input and output stream.
* <code>p | q</code> forks the execution of the program so that one branch runs <code>p</code>, another branch runs <code>q</code>, sharing the same input and output stream.


Start by writing a routine that copies a natural number from the input to the output.
Start by writing a routine that copies a natural number from the input to the output.
283

edits