Oz | |
Paradigm: | multi-paradigm logic, functional, imperative, object-oriented, constraint, distributed, concurrent |
Year: | 1991 |
Designer: | Gert Smolka, his students |
Developer: | Mozart Consortium |
Latest Release Version: | Oz 1.4.0 (final), Mozart 2.0.1 |
Typing: | dynamic |
Implementations: | Mozart Programming System |
License: | MIT X11[1] |
Dialects: | Oz, Mozart |
Influenced By: | Erlang, Lisp, Prolog |
Influenced: | Alice, Scala |
Oz is a multiparadigm programming language, developed in the Programming Systems Lab at Université catholique de Louvain, for programming-language education. It has a canonical textbook: Concepts, Techniques, and Models of Computer Programming.
Oz was first designed by Gert Smolka and his students in 1991. In 1996, development of Oz continued in cooperation with the research group of Seif Haridi and Peter Van Roy at the Swedish Institute of Computer Science. Since 1999, Oz has been continually developed by an international group, the Mozart Consortium, which originally consisted of Saarland University, the Swedish Institute of Computer Science, and the Université catholique de Louvain. In 2005, the responsibility for managing Mozart development was transferred to a core group, the Mozart Board, with the express purpose of opening Mozart development to a larger community.
The Mozart Programming System is the primary implementation of Oz. It is released with an open source license by the Mozart Consortium. Mozart has been ported to Unix, FreeBSD, Linux, Windows, and macOS.
Oz[2] contains most of the concepts of the major programming paradigms, including logic, functional (both lazy evaluation and eager evaluation), imperative, object-oriented, constraint, distributed, and concurrent programming. Oz has both a simple formal semantics (see chapter 13 of the book mentioned below) and Oz is a concurrency-oriented language, as the term was introduced by Joe Armstrong, the main designer of the Erlang language. A concurrency-oriented language makes concurrency easy to use and efficient. Oz supports a canonical graphical user interface (GUI) language QTk.[3]
In addition to multi-paradigm programming, the major strengths of Oz are in constraint programming and distributed programming. Due to its factored design, Oz is able to successfully implement a network-transparent distributed programming model. This model makes it easy to program open, fault-tolerant applications within the language. For constraint programming, Oz introduces the idea of computation spaces, which allow user-defined search and distribution strategies orthogonal to the constraint domain.
Oz is based on a core language with very few datatypes that can be extended into more practical ones through syntactic sugar.
Basic data structures:
circle(x:0 y:1 radius:3 color:blue style:dots)
. Here the terms x,y, radius etc. are called features and the data associated with the features (in this case 0,1,3 etc.) are the values. circle(1:0 2:1 3:3 4:blue 5:dots)
.Functions[5] are first class values, allowing higher order functional programming:
fun case List of nil then 0 [] H|T then H+ % pattern matching on lists endendFunctions may be used with both free and bound variables. Free variable values are found using static lexical scoping.[6]
Functions are like other Oz objects. A function can be passed as an attribute to other functions or can be returned in a function.
fun % F is a function here - higher order programming case Xs of nil then nil [] X|Xr then | endend
%usage %browses [1 4 9]
Like many other functional languages, Oz supports use of anonymous functions (i.e. functions which do not have a name) with higher order programming. The symbol $ is used to denote these.
In the following, the square function is defined anonymously and passed, causing [1 4 9]
to be browsed.
Since anonymous functions don't have names, it is not possible to define recursive anonymous functions.
Functions in Oz are supposed to return a value at the last statement encountered in the body of the function during its execution. In the example below, the function Ret returns 5 if X > 0 and -5 otherwise.
When the program encounters an unbound variable it waits for a value. For example, below, the thread will wait until both X and Y are bound to a value before showing the value of Z.
The value of a dataflow variable cannot be changed once it is bound:
Dataflow variables make it easy to create concurrent stream agents:
fun case Stream of nil then S [] H|T then S| endend
local X Y in thread X = end thread Y = end end
Because of the way dataflow variables work, it is possible to put threads anywhere in a program and guaranteed that it will have the same result. This makes concurrent programming very easy. Threads are very cheap: it is possible to have 100,000 threads running at once.[8]
This example computes a stream of prime numbers using the trial division algorithm by recursively creating concurrent stream agents that filter out non-prime numbers:
Oz uses eager evaluation by default, but lazy evaluation[9] is possible. Below, the fact is only computed when value of X is needed to compute the value of Y.
Lazy evaluation gives the possibility of storing truly infinite data structures in Oz. The power of lazy evaluation can be seen from the following code sample:
fun lazy case Xs of nil then nil [] X|Xr then N*X| endend
declare HH = 1 | The code above elegantly computes all the Regular Numbers[10] in an infinite list. The actual numbers are computed only when they are needed.
The declarative concurrent model can be extended with message passing via simple semantics:
With a port and a thread, asynchronous agents can be defined:
It is again possible to extend the declarative model to support state and object-oriented programming with very simple semantics. To create a new mutable data structure called Cells:
With these simple semantic changes, the whole object-oriented paradigm can be supported. With a little syntactic sugar, OOP becomes well integrated in Oz.
local C in C = end
The execution speed of a program produced by the Mozart compiler (version 1.4.0 implementing Oz 3) is very slow. On a 2012 set of benchmarks it averaged about 50 times slower than that of the GNU Compiler Collection (GCC) for the C language.[11]