-
Notifications
You must be signed in to change notification settings - Fork 1
Pseudo imperative programming
The aim of our following constructs is naturally to allow the user to program in a style that's natural in procedural programming, although in a self-contained manner. The procedural interpretation of our while statement thus clearly mimics the while statement of procedural languages. This allows the user to try a variety of programming styles even while keeping the code's logic or functional properties.
Imperative programming: code is a recipe of instructions. The interpreter will execute these instructions in order. Variables are cells that can be set to different values.
Procedural programming: in addition to the above, code may contain control structures such as 'if' or 'while' that change the flow of execution.
Here is an example of a while construct.
init i=1
while(i<6)
print(i)//1,2,3,4,5
next i=1+i
print(i)//6
The code will successively write every integer from 1 to 6.
Logical interpretation
while(A) B: If the condition A is true, then B holds, and remains so while A holds. Conversely, B is true until A stops holding.
Procedural interpretation
The condition A is tried; if it holds, execution proceeds to the code in B, if not, leave the while statement. This is repeated until the condition doesn't hold.
We'd like to call our indexing variable i a temporal variable, for lack of a better term, so as to give it a better declarative reading. In this reading, a temporal variable is a variable for which the value changes over time.
These expressions are relevant for a temporal variable.
init i: the initial value of the variable.
next i: the value it'll hold in the next loop or iteration.
When inside the loop, the term i by itself is the current value. Outside, it is the final value of the variable.
In our example, the variable is continually increased, meaning that the condition eventually stops holding.
The classic for-statement is simply an extension of the while statement. It's perhaps not necessary, but we thought best to include it.
for(init i=1; i<6; next i=1+i;)
print(i)//1,2,3,4,5
print(i)//6
The only difference is that we made the parts of code concerned with iteration clear by placing them separate from the body; our variable iterates from 1 to 5, increasing by 1 each time, and it is written.
A simple way to do so is also by using a generic mechanism.
range=math.range
for(x in range(1,6))
print(x)//1,2,3,4,5
The relation range is used to iterate through 1 to 6.
for(x in [1,2,3])
print(x)//1,2,3
We may also incidentally move through any data structure in such a way, as long as an iteration mechanism is provided for them.
It's curious that we started by mimicking procedural languages, and arrived at a way of expressing what are essentially logic idioms. Consider,
for(x in l)
human(x)
some(x in l)
human(x)
This expresses that all x are human or some x are human, respectively.
Operator | Idiom |
---|---|
for | "For all x, x is y" (All X are Y.) |
some | "There is at least one x, such that x is y" (Some X are Y.) |
Some examples may illustrate the idea.
>for(x in [1,2,3]) odd(x)
| false
>some(x in [1,2,3]) odd(x)
| true
>for(x in {1,1,1}) x=1
| true
>some(x in {2,1,1}) x=1
| true