Pipe-calculus: Difference between revisions

Line 16: Line 16:
Development of pipe-calculus started with the idea that syntax should be a first class element of programming languages as well as formation rules are integral part of logical systems. A language like that is able to interpret complex input without using a parser library. Pipe-calculus is a minimal language with this capability.
Development of pipe-calculus started with the idea that syntax should be a first class element of programming languages as well as formation rules are integral part of logical systems. A language like that is able to interpret complex input without using a parser library. Pipe-calculus is a minimal language with this capability.


On the practical side pipe-calculus is inspired by shell programming, which is largely based on text streams and stream processing tools. Indeed the idea of the ''pipe'' combinator comes from shell scripting. Shell programming is a proved and efficient way of problem solving, although it is not known about clarity and simplicity.  
On the practical side pipe-calculus is inspired by shell programming, which is largely based on text streams and stream processing tools. Indeed the idea of the ''pipe'' combinator comes from shell scripting.  


How would a small and well established stream processing language look like? To get a taste, consider a program that adds two natural numbers encoded in [[wikipedia:Unary_numeral_system|unary numeral system]], a popular example in theoretical computer science. Suppose that we send both numbers over a stream as a series of <code>S</code> symbols terminated by a symbol <code>Z</code>. Our example language consists of the following expressions.
=== An informal example ===


* <code>match s</code> checks if the next symbol of the input stream is <code>s</code>. On failure the current branch of the program is aborted.
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>S</code> symbols terminated by a <code>Z</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 branch of the program is aborted.
* <code>write s</code> writes the symbol <code>s</code> to the output stream.
* <code>write s</code> writes 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 p 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 branch runs <code>p</code>, another branch 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.


First we write a routine that copies a natural number from the input to the output.
Start writing a routine that copies a natural number from the input to the output.


  passNat = match S; write S; run passNat | match Z; write Z
  passNat = match S; write S; run passNat | match Z; write Z


Now we can write a program that writes the sum of two numbers to the output.
Now we can write the program that writes the sum of two numbers to the output.


  addNat = match S; write S; run addNat | match Z; run passNat
  addNat = match S; write S; run addNat | match Z; run passNat


The program terminates after matching and writing out the second symbol <code>Z</code> it encounters. It skips the first <code>Z</code> that terminates the first number and copies everything else.
The program terminates after matching and writing out the second <code>Z</code> symbol. It skips the first <code>Z</code> that terminates the first number and copies everything else.
Effectively it concatenates two <code>Z</code>-terminated sequences of <code>S</code>s. If any other symbol occurs in the input stream, the program fails.
Effectively it concatenates two <code>Z</code>-terminated sequences of <code>S</code>s. If any other symbol occurs in the input stream, the program fails.


283

edits