Oz
Introduction to Programming Concepts
Variables
The declare statement creates a new store variable and makes the variable identifier refer to it. Previous calculations using the same identifier are not changed because the identifier refers to another store variable.
declare V=9999*9999
Functions
The declare statement creates the new variable Fact.The fun statement defines a function. The variable Fact is bound to the function.The function has one argument N, which is a local variable, i.e., it is known only inside the function body. Each time we call the function a new local variable is created.
declare fun {Fact N} if N==0 then 1 else N*{Fact N-1} end end
Recursion
Combinations
declare fun {Comb N K} {Fact N} div ({Fact K}*{Fact N-K}) end
Lists
Pattern Matching
declare L=[5 6 7 8] case L of H|T then {Browse H} {Browse T} end
Functions over Lists
Correctness
To prove correctness in general, we have to reason about the program. This means three things:
- We need a mathematical model of the operations of the programming language, defining what they should do.This model is called the language’s semantics.
- We need to define what we would like the program to do.Usually, this is a mathematical definition of the inputs that the program needs and the output that it calculates. This is called the program’s specification.
- We use mathematical techniques to reason about the program, using the semantics. We would like to demonstrate that the program satisfies the specification.
A program that is proved correct can still give incorrect results, if the system on which it runs is incorrectly implemented.
Complexity
Lazy Evaluation
fun lazy {Ints N} N|{Ints N+1} end
Higher-Order Programming
Concurrency
thread P in P={Pascal 30} {Browse P} end {Browse 99*99}
Dataflow
What happens if an operation tries to use a variable that is not yet bound? From a purely aesthetic point of view, it would be nice if the operation would simply wait. Perhaps some other thread will bind the variable, and then the operation can continue.This civilized behavior is known as dataflow.
(…)
Adding threads and delays to a program can radically change a program’s appearance. But as long as the same operations are invoked with the same arguments, it does not change the program’s results at all. This is the key property of dataflow concurrency.This is why dataflow concurrency gives most of the advantages of concurrency without the complexities that are usually associated with it.
declare X in thread {Delay 10000} X=99 end {Browse start} {Browse X*X}
Explicit state
Memory Cell
There are lots of ways to define explicit state.The simplest way is to define a single memory cell. This is a kind of box in which you can put any content. Many programming languages call this a "variable". We call it a "cell" to avoid confusion with the variables we used before, which are more like mathematical variables, i.e., just shortcuts for values.
declare C={NewCell 0} C:=@C+1 {Browse @C}
Object
declare local C in C={NewCell 0} fun {Bump} C:=@C+1 @C end fun {Read} @C end end
Classes
declare fun {NewCounter} C Bump Read in C={NewCell 0} fun {Bump} C:=@C+1 @C end fun {Read} @C end counter(bump:Bump read:Read) end
Atomicity
declare C={NewCell 0} L={NewLock} thread lock L then I in I=@C C:=I+1 end end thread lock L then J in J=@C C:=J+1 end end