A Quick Introduction to the Popr Programming Language
Modified on November 5, 2013
Popr is a pure functional lazy post-fix concatenative programming language. I will briefly explain some of the features of Popr through some examples. You can try these examples online.
1 2 + --> [ 3 ]
+
is a function that takes two integers and returns one. There are several similar functions, with the same meaning as in C: -
, *
, <
, <=
, ==
, >
, >=
. Booleans are currently represented as integers, non-zero for true, zero for false.
1 2 | 3 + --> [ { 4 | 5 } ]
2 5 | dup 2 - ! --> [ 5 ]
|
takes two values and creates two alternatives, where each value is returned. !
takes two values; if the second argument is 0, the it fails, otherwise the first value is returned. Alternatives and failure provide a more general branching mechanism than if/else, similar to speculative execution, or Prolog inference.
[ 1 2 + 3 4 + ] popr --> [ [ 1 2 + ] 7 ]
1 [ 2 + ] pushl --> [ [ 1 2 + ] ]
1 [ 2 + ] pushl popr --> [ [] 3 ]
[ ... ]
denotes a quotation, which can be used for constructing functions or for aggregating values. popr
pops the rightmost element from a quotation and forces evaluation. pushl
takes its first argument and pushes it onto the left of the quotation. The two primitives can be combined to evaluate a quotation. Notice that Popr is lazy, and only reduces functions in quotations when required.
1 2 | dup --> { [ 1 1 ] | [ 2 2 ] }
1 2 swap --> [ 2 1 ]
1 2 drop --> [ 1 ]
1 2 | cut --> [ 1 ]
Some other primitives. dup
duplicates a value; notice how it respects the constraints introduced by alternatives. swap
swaps two values. drop
drops a value. cut
prunes alternatives after the first successful one. This should only be used to hint that only one unique alternative would succeed, rather than to prune otherwise successful execution paths. The type checker might eventually check this.