Jump to content

Oz (programming language)

From Wikipedia, the free encyclopedia

Oz
Paradigmmulti-paradigm:logic,functional,imperative,object-oriented,constraint,distributed,concurrent
Designed byGert Smolka, his students
DeveloperMozart Consortium
First appeared1991;33 years ago(1991)
Stable release
Oz 1.4.0 (final), Mozart 2.0.1 / 5 September 2018;6 years ago(2018-09-05)
Typing disciplinedynamic
LicenseMIT X11[1]
Websitemozart.github.io
Majorimplementations
Mozart Programming System
Dialects
Oz, Mozart
Influenced by
Erlang,Lisp,Prolog
Influenced
Alice,Scala

Ozis amultiparadigm programming language,developed in the Programming Systems Lab atUniversité 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 theSwedish Institute of Computer Science.Since 1999, Oz has been continually developed by an international group, the Mozart Consortium, which originally consisted ofSaarland University,theSwedish Institute of Computer Science,and theUniversité 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 anopen source licenseby the Mozart Consortium. Mozart has been ported toUnix,FreeBSD,Linux,Windows,andmacOS.

Language features

[edit]

Oz[2]contains most of the concepts of the majorprogramming paradigms,including logic, functional (bothlazy evaluationandeager evaluation), imperative, object-oriented, constraint, distributed, and concurrent programming. Oz has both a simple formal semantics (see chapter 13 of the book mentioned below) andan efficient implementation.[citation needed]Oz is aconcurrency-oriented language, as the term was introduced by Joe Armstrong, the main designer of theErlang language.A concurrency-oriented language makes concurrency easy to use and efficient. Oz supports a canonicalgraphical user interface(GUI) language QTk.[3]

In addition to multi-paradigm programming, the major strengths of Oz are inconstraint programminganddistributed 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-tolerantapplications within the language. For constraint programming, Oz introduces the idea ofcomputation spaces,which allow user-defined search and distribution strategiesorthogonalto the constraint domain.

Language overview

[edit]

Data structures

[edit]

Oz is based on a core language with very few datatypes that can be extended into more practical ones throughsyntactic sugar.

Basic data structures:

  • Numbers: floating point or integer (real integer)
  • Records: for grouping data: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.
  • Tuples: Records with integer features in ascending order:circle(1:0 2:1 3:3 4:blue 5:dots).
  • Lists: a simple linear structure
'|'(2'|'(4'|'(6'|'(8nil))))% as a record.
2|(4|(6|(8|nil)))% with some syntactic sugar
2|4|6|8|nil% more syntactic sugar
[2468]% even more syntactic sugar

Those data structures are values (constant),first classanddynamically type checked.Variable names in Oz start with an uppercase letter to distinguish them fromliterals[4]which always begin with a lowercase letter.

Functions

[edit]

Functions[5]are first class values, allowinghigher order functionalprogramming:

fun{FactN}
ifN=<0then1elseN*{FactN-1}end
end
fun{CombNK}
{FactN}div({FactK}*{FactN-K})% integers can't overflow in Oz (unless no memory is left)
end

fun{SumListList}
caseListofnilthen0
[]H|TthenH+{SumListT}% pattern matching on lists
end
end

Functions may be used with both free and bound variables. Free variable values are found using staticlexical scoping.[6]

Higher-order programming

[edit]

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{SquareN}% A general function
N*N
end

fun{MapFXs}% F is a function here - higher order programming
caseXs
ofnilthennil
[]X|Xrthen{FX}|{MapFXr}
end
end

%usage
{Browse{MapSquare[123]}}%browses [1 4 9]

Anonymous functions

[edit]

Like many other functional languages, Oz supports use ofanonymous 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.

{Browse{Mapfun{$N}N*Nend[123]}}

Since anonymous functions don't have names, it is not possible to define recursive anonymous functions.

Procedures

[edit]

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.

declare
fun{RetX}
ifX>0then5else~5end
end

But Oz also provides a facility in case a function must not return values. Such functions are called procedures.[7]Procedures are defined using the construct "proc" as follows

declare
proc{RetX}
ifX>0then{Browse5}else{Browse~5}end
end

The above example doesn't return any value, it just prints 5 or -5 in the Oz browser depending on the sign of X.

Dataflow variables and declarative concurrency

[edit]

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.

thread
Z=X+Y
{BrowseZ}
end
threadX=40end
threadY=2end

The value of a dataflow variable cannot be changed once it is bound:

X=1
X=2% error

Dataflow variables make it easy to create concurrent stream agents:

fun{IntsNMax}
ifN==Maxthennil
else
{Delay1000}
N|{IntsN+1Max}
end
end

fun{SumSStream}
caseStream
ofnilthenS
[]H|TthenS|{SumH+ST}
end
end

localXYin
threadX={Ints01000}end
threadY={Sum0X}end
{BrowseY}
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]

Example: Trial division sieve

[edit]

This example computes a stream of prime numbers using thetrial divisionalgorithm by recursively creating concurrent stream agents that filter out non-prime numbers:

fun{SieveXs}
caseXsofnilthennil
[]X|XrthenYsin
threadYs={FilterXrfun{$Y}YmodX\=0end}end
X|{SieveYs}
end
end

Laziness

[edit]

Oz useseager evaluationby default, butlazy evaluation[9]is possible. Below, the fact is only computed when value of X is needed to compute the value of Y.

funlazy{FactN}
ifN=<0then1elseN*{FactN-1}end
end
localXYin
X={Fact100}
Y=X+1
end

Lazy evaluationgives the possibility of storing truly infinite data structures in Oz. The power of lazy evaluation can be seen from the following code sample:

declare
funlazy{MergeXsYs}
caseXs#Ys
of(X|Xr)#(Y|Yr)then
ifX<YthenX|{MergeXrYs}
elseifX>YthenY|{MergeXsYr}
elseX|{MergeXrYr}
end
end
end

funlazy{TimesNXs}
caseXs
ofnilthennil
[]X|XrthenN*X|{TimesNXr}
end
end

declareH
H=1|{Merge{Times2H}{Merge{Times3H}{Times5H}}}
{Browse{List.takeH6}}

The code above elegantly computes all theRegular Numbers[10]in an infinite list. The actual numbers are computed only when they are needed.

Message passing concurrency

[edit]

The declarative concurrent model can be extended withmessage passingvia simple semantics:

declare
localStreamPortin
Port={NewPortStream}
{SendPort1}% Stream is now 1|_ ('_' indicates an unbound and unnamed variable)
{SendPort2}% Stream is now 1|2|_
...
{SendPortn}% Stream is now 1|2|.. |n|_
end

With a port and a thread, asynchronous agents can be defined:

fun{NewAgentInitFun}
MsgOutin
thread{FoldLMsgFunInitOut}end
{NewPortMsg}
end

State and objects

[edit]

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:

localAXin
A={NewCell0}
A:=1% changes the value of A to 1
X=@A% @ is used to access the value of A
end

With these simple semantic changes, the whole object-oriented paradigm can be supported. With a little syntactic sugar, OOP becomes well integrated in Oz.

classCounter
attrval
methinit(Value)
val:=Value
end
methbrowse
{Browse@val}
end
methinc(Value)
val:=@val+Value
end
end

localCin
C={NewCounterinit(0)}
{Cinc(6)}
{Cbrowse}
end

Execution speed

[edit]

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 ofbenchmarksit averaged about 50 times slower than that of theGNU Compiler Collection(GCC) for the C language.[11]

See also

[edit]

References

[edit]
  1. ^"Mozart Oz License Info".16 January 2014.Retrieved16 January2014.
  2. ^ Gert Smolka (1995)."The Oz Programming Model"(PDF).Computer Science Today.Lecture Notes in Computer Science. Vol. 1000. pp. 324–343.doi:10.1007/BFb0015252.ISBN978-3-540-60105-0.
  3. ^"QTk".Archived fromthe originalon 20 May 2013.Retrieved6 April2009.
  4. ^"3 Basics".
  5. ^Leif Grönqvist. "Higher Order Functions".Advanced Functional Programming in Oz.Archived fromthe originalon 3 March 2016.Retrieved3 November2014.
  6. ^ Robert Gentleman; Ross Ihaka (September 2000)."Lexical Scope in Statistical Computing"(PDF).Journal of Computational and Graphical Statistics.9(3, Systems and Languages): 491–508.doi:10.1080/10618600.2000.10474895.
  7. ^"5 Basic Control Structures".
  8. ^"Archived copy".Archived fromthe originalon 24 February 2015.Retrieved29 November2008.{{cite web}}:CS1 maint: archived copy as title (link)
  9. ^ Paul Hudak(1989). "Conception, evolution, and application of functional programming languages".ACM Computing Surveys.21(3): 359–411.doi:10.1145/72551.72554.S2CID207637854.
  10. ^ Rao, AC & Varada Raju, D (1991). "Application of the Hamming number technique to detect isomorphism among kinematic chains and inversions".Mechanism and Machine Theory.26(1): 55–75.doi:10.1016/0094-114x(91)90022-v.
  11. ^The Computer Language Benchmarks Game
[edit]