Oz (programming language): Difference between revisions

Content deleted Content added
m State and objects: Adding wikilinks
 
(41 intermediate revisions by 31 users not shown)
Line 1:
{{Short description|Multiparadigm programming language}}
{{Use dmy dates|date=May 20122022}}
{{Infobox programming language
|name = Oz
Line 4 ⟶ 6:
|paradigm = [[multi-paradigm programming language|multi-paradigm]]: [[logic programming|logic]], [[functional programming|functional]], [[imperative programming|imperative]], [[object-oriented programming|object-oriented]], [[constraint programming|constraint]], [[distributed programming|distributed]], [[concurrent programming language|concurrent]]
|year = 1991
|designer = [[Gert Smolka]], his students
|developer = [[Mozart Consortium]]
|released = {{Start date and age|1991}}
|latest release version = 1.4.0
|latest release dateversion = Oz 1.4.0 =(final), {{startMozart date and age|df=yes|2008|07|03}}2.0.1
|latest release date = {{Start date and age|2018|09|05|df=yes}}
|typing = [[dynamic typing|dynamic]]
|implementations = Mozart Programming System
|license = [[MIT X11 License|MIT X11]] <ref>{{cite web | url=httphttps://mozart.github.io/license-info/ | title=Mozart Oz License Info | date=16 January 2014 |access-01-date=16 |January accessdate=2014-01-16}}</ref>
|dialects = Oz, Mozart
|influenced by = [[Erlang (programming language)|Erlang]], [[Lisp (programming language)|Lisp]], [[Prolog]]
|influenced = [[Alice (programming language)|Alice]], [[Scala (programming language)|Scala]]
|website = [http://{{URL|mozart.github.io/ http://mozart.github.io]}}
}}
{{Use dmy dates|date=May 2012}}
 
'''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 the, 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 different flavors of [[Unix]], [[FreeBSD]], [[Linux]], [[Microsoft Windows|Windows]], and [[Mac OS XmacOS]].
 
==Language features==
Oz<ref name="Oz programming model">
{{cite paperbook
| author = [[Gert Smolka]]
| title = TheComputer OzScience Programming ModelToday
| chapter = The Oz Programming Model
|journal series = Lecture Notes in Computer Science
| volume = 1000
| year= 1995
| pages = 324–343
| doi = 10.1007/BFb0015252
| url= https://www.ps.uni-saarland.de/Publications/documents/Vol1000.pdf}}
| isbn = 978-3-540-60105-0
</ref> contains most of the concepts of the major [[programming paradigm]]s, including logic, functional (both [[lazy evaluation|lazy]] and [[eager evaluation|eager]]), imperative, object-oriented, constraint, distributed, and concurrent programming. Oz has both a simple formal semantics (see chapter 13 of the book mentioned below) and an efficient implementation{{Citation needed|date=June 2007}}. Oz is a [[concurrency (computer science)|concurrency]]-oriented language, as the term was introduced by [[Joe Armstrong (programmer)|Joe Armstrong]], the main designer of the [[Erlang (programming language)|Erlang language]]. A concurrency-oriented language makes concurrency both easy to use and efficient. Oz supports a canonical GUI language [http://www.mozart-oz.org/home/doc/mozart-stdlib/wp/qtk/html/ QTk].
| chapter-url= https://www.ps.uni-saarland.de/Publications/documents/Vol1000.pdf}}
</ref> contains most of the concepts of the major [[programming paradigm]]s, including logic, functional (both [[lazy evaluation|lazy]] and [[eager evaluation|eager]]), imperative, object-oriented, constraint, distributed, and concurrent programming. Oz has both a simple formal semantics (see chapter 13 of the book mentioned below) and an efficient implementation{{Citation needed-span|date=June 2007|text=an efficient implementation.}}. Oz is a [[concurrencyConcurrency (computer science)|concurrency]]-oriented language, as the term was introduced by [[Joe Armstrong (programmer)|Joe Armstrong]], the main designer of the [[Erlang (programming language)|Erlang language]]. A concurrency-oriented language makes concurrency both easy to use and efficient. Oz supports a canonical [[graphical user interface]] (GUI) language [QTk.<ref>{{Cite web |url=http://www.mozart-oz.org/home/doc/mozart-stdlib/wp/qtk/html/ |title=QTk] |access-date=6 April 2009 |archive-url=https://web.archive.org/web/20130520060646/http://www.mozart-oz.org/home/doc/mozart-stdlib/wp/qtk/html/ |archive-date=20 May 2013 |url-status=usurped }}</ref>
 
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, [[faultFault toleranttolerance|fault-tolerant]] applications within the language. For constraint programming, Oz introduces the idea of "''computation spaces";'', thesewhich allow user-defined search and distribution strategies [[Orthogonal#Computer science|orthogonal]] to the constraint ___domain.
 
==Language overview==
Line 48 ⟶ 53:
* Tuples: Records with integer features in ascending order: <code> circle(1:0 2:1 3:3 4:blue 5:dots) </code>.
* Lists: a simple linear structure
<sourcesyntaxhighlight lang="erlang">
'|'(2 '|'(4 '|'(6 '|'(8 nil)))) % as a record.
2|(4|(6|(8|nil))) % with some syntactic sugar
2|4|6|8|nil % more syntactic sugar
[2 4 6 8] % even more syntactic sugar
</syntaxhighlight>
</source>
Those data structures are values (constant), [[first-class object|first class]] and [[dynamic typing|dynamically type checked]]. Variable names in ozOz start with an uppercase letter to distinguish them from [[Literal (computer programming)|literals]]<ref>{{Cite web|url=https://mozart.github.io/mozart-v1/doc-1.4.0/tutorial/node3.html#label18|title=3 Basics}}</ref> which always begin with a lowercase letter.
 
===Functions===
Functions<ref name="Advanced Functional Programming in Oz">{{cite book
{{cite paper
| author = Leif Grönqvist
| title = Advanced Functional Programming in Oz
| chapter = Higher Order Functions
| url = http://www2.gslt.hum.gu.se/~leifg/gslt/doc/ozfunpaper.ps
| access-date = 3 November 2014
}}
| archive-url = https://web.archive.org/web/20160303171453/http://www2.gslt.hum.gu.se/~leifg/gslt/doc/ozfunpaper.ps
</ref> are first class values, allowing [[Higher-order programming|higher order functional]] programming:
| archive-date = 3 March 2016
<source lang="erlang">
| url-status = dead
}}</ref> are first class values, allowing [[Higher-order programming|higher order functional]] programming:
<sourcesyntaxhighlight lang="erlang">
fun {Fact N}
if N =< 0 then 1 else N*{Fact N-1} end
end
</sourcesyntaxhighlight><sourcesyntaxhighlight lang="erlang">
fun {Comb N K}
{Fact N} div ({Fact K} * {Fact N-K}) % integers can't overflow in Oz (unless no memory is left)
Line 79 ⟶ 86:
end
end
</syntaxhighlight>
</source>
Functions may be used with both free and bound variables. Free variable values isare found using static [[Scope (computer science)|lexical scoping]].<ref name="Scoping">
{{cite bookjournal
| author = Robert Gentleman|author2=Ross Ihaka
| title = Lexical Scope in Statistical Computing
| url= https://www.stat.auckland.ac.nz/~ihaka/downloads/lexical.pdf
|journal=Journal of Computational and Graphical Statistics
|volume= 9|issue= 3, Systems and Languages |date=Sep 2000|pages=491–508}}|doi=10.1080/10618600.2000.10474895
}}
</ref>
 
====Higher-order programming====
Functions are like other Oz objects. A function can be passed as an attribute to other functions or can be returned in a function.
<sourcesyntaxhighlight lang="erlang">
fun {Square N} % A general function
N*N
Line 105 ⟶ 113:
%usage
{Browse {Map Square [1 2 3]}} %browses [1 4 9]
</syntaxhighlight>
</source>
 
====Anonymous functions====
Like many other functional languages, Oz supports use of [[anonymous functionsfunction]]s (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 <code>[1 4 9]</code> to be browsed.
<sourcesyntaxhighlight lang="erlang">
{Browse {Map fun {$ N} N*N end [1 2 3]}}
</syntaxhighlight>
</source>
 
Since, anonymous functions don't possesshave a namenames, it is not possible to define recursive anonymous functions.
 
====Procedures====
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.
<sourcesyntaxhighlight lang="erlang">
declare
fun {Ret X}
if X > 0 then 5 else ~5 end
end
</syntaxhighlight>
</source>
But Oz also provides a facility in case wea don'tfunction want the functionmust tonot return values. Such functions are called procedures.<ref>{{Cite web|url=https://mozart.github.io/mozart-v1/doc-1.4.0/tutorial/node5.html#control.procedure|title = 5 Basic Control Structures}}</ref> Procedures are defined using the construct "proc" as follows
<sourcesyntaxhighlight lang="erlang">
declare
proc {Ret X}
if X > 0 then {Browse 5} else {Browse ~5} end
end
</syntaxhighlight>
</source>
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 ===
When the program encounters an unbound variable it waits for a value. For example, in the followingbelow, the thread will wait until both X and Y are bound to a value before showing the value of Z.
<sourcesyntaxhighlight lang="erlang">
thread
Z = X+Y
Line 143 ⟶ 151:
thread X = 40 end
thread Y = 2 end
</syntaxhighlight>
</source>
 
It is not possible to change theThe value of a dataflow variable cannot be changed once it is bound:
<sourcesyntaxhighlight lang="erlang">
X = 1
X = 2 % error
</syntaxhighlight>
</source>
 
Dataflow variables make it easy to create concurrent stream agents:
 
<sourcesyntaxhighlight lang="erlang">
fun {Ints N Max}
if N == Max then nil
Line 163 ⟶ 171:
 
fun {Sum S Stream}
case Stream
of nil then S
[] H|T then S|{Sum H+S T}
end
end
 
Line 172 ⟶ 182:
{Browse Y}
end
</syntaxhighlight>
</source>
 
Because of the way dataflow variables work, it is possible to put threads anywhere in thea program and it is guaranteed that it will have the same result. This makes concurrent programming very easy. Threads are very cheap: it is possible to have a hundred thousand100,000 threads running at once.<ref>{{Cite web |url=http://www.mozart-oz.org/documentation/tutorial/node8.html#chapter.concurrency |title=Archived copy |access-date=29 November 2008 |archive-url=https://web.archive.org/web/20150224185115/http://www.mozart-oz.org/documentation/tutorial/node8.html#chapter.concurrency |archive-date=24 February 2015 |url-status=usurped }}</ref>
 
===Example: Trial division sieve===
This example computes a stream of prime numbers using the [[Trialtrial division]] algorithm by recursively creating concurrent stream agents that filter out non-prime numbers:
<sourcesyntaxhighlight lang="erlang">
fun {Sieve Xs}
case Xs of nil then nil
Line 186 ⟶ 196:
end
end
</syntaxhighlight>
</source>
 
=== Laziness ===
Oz uses [[eager evaluation]] by default, but [[lazy evaluation]]<ref name="Lazy Programming">
{{cite paperjournal
| author = [[Paul Hudak]]
| author-link = Paul Hudak
| title = Conception, evolution, and application of functional programming languages
| journal = ACM Computing Surveys (CSUR)
| year = 1989
| volume = 21
| number = 3
| pages = 359–411 }}
| doi=10.1145/72551.72554| s2cid = 207637854
</ref> is possible. In the following, the fact is only computed when value of X is needed to compute the value of Y.
}}
<source lang="erlang">
</ref> is possible. In the followingBelow, the fact is only computed when value of X is needed to compute the value of Y.
<sourcesyntaxhighlight lang="erlang">
fun lazy {Fact N}
if N =< 0 then 1 else N*{Fact N-1} end
Line 206 ⟶ 220:
Y = X + 1
end
</syntaxhighlight>
</source>
 
[[lazyLazy evaluation]] gives the possibility of storing truly infinite data structures in Oz. The power of [[lazy evaluation]] can be seen from the following fragmentcode of codesample:
<sourcesyntaxhighlight lang="erlang">
declare
fun lazy {Merge Xs Ys}
Line 231 ⟶ 245:
H = 1 | {Merge {Times 2 H} {Merge {Times 3 H} {Times 5 H}}}
{Browse {List.take H 6}}
</syntaxhighlight>
</source>
The code above elegantly computes all the [[Regular Number]]s<ref name="Hamming Numbers">
{{cite paperjournal
| author author1= Rao, AC and |author2=Varada Raju, D
|name-list-style=amp | title = Application of the Hamming number technique to detect isomorphism among kinematic chains and inversions
| journal = Mechanism and Machine theoryTheory
| volume = 26
| number = 1
| pages = 55–75
| year = 1991 }}
| doi=10.1016/0094-114x(91)90022-v}}
</ref> in an infinite list. The actual numbers are computed only when they are needed.
 
=== Message passing concurrency ===
The declarative concurrent model can be extended with [[message passing]] throughvia simple semantics:
<sourcesyntaxhighlight lang="erlang">
declare
local Stream Port in
Line 254 ⟶ 269:
{Send Port n} % Stream is now 1|2| .. |n|_
end
</syntaxhighlight>
</source>
 
With a port and a thread, theasynchronous programmeragents can definebe asynchronous agentsdefined:
<sourcesyntaxhighlight lang="erlang">
fun {NewAgent Init Fun}
Msg Out in
Line 263 ⟶ 278:
{NewPort Msg}
end
</syntaxhighlight>
</source>
 
=== State and objects ===
 
It is again possible to extend the declarative model to support state and [[object-oriented programming]] with very simple semantics;. weTo create a new mutable data structure called Cells:
<sourcesyntaxhighlight lang="erlang">
local A X in
A = {NewCell 0}
Line 274 ⟶ 289:
X = @A % @ is used to access the value of A
end
</syntaxhighlight>
</source>
 
With these simple semantic changes we can support, the whole object-oriented paradigm can be supported. With a little [[syntactic sugar]], OOP becomes well integrated in Oz.
<syntaxhighlight lang="visualprolog">
With a little syntactic sugar OOP becomes well integrated in Oz.
<source lang="erlang">
class Counter
attr val
Line 297 ⟶ 311:
{C browse}
end
</syntaxhighlight>
</source>
 
==Execution speed==
The execution speed of a program produced by the Mozart Compilercompiler (version 1.4.0 implementing Oz 3) is very slow. On a 2012 set of benchmarks[[The itComputer isLanguage ''onBenchmarks Game|benchmarks]] it average''averaged about 50 times slower than that of the gcc[[GNU compilerCompiler Collection]] (GCC) for the C language, solving the benchmarks-tasks.<ref>[https://archive.today/20120713231944/http://shootout.alioth.debian.org/u32/which-programming-languages-are-fastest.php?calc=chart&gcc=on&oz=on The Computer Language Benchmarks Game]</ref>{{failed verification|date=February 2014}}
 
==See also==
* [[Alice (programming language)|Alice]], thea concurrent functional constraint programming language from Saarland University
* [[Dataflow programming]]
* [[:Category:Functional logic programming languages|Functional logic programming languages]]
** [[Curry (programming language)|Curry]],
** a functional logic[[Mercury (programming language)]]
** [[MercuryVisual (programming language)|MercuryProlog]], aan object-oriented, functional, logic programming language
** [[Visual Prolog]], an object-oriented, functional, logic programming language
 
==References==
* Peter Van Roy and Seif Haridi (2004). ''[[Concepts, Techniques, and Models of Computer Programming]]''. MIT Press. There is [http://www.info.ucl.ac.be/~pvr/book.html online supporting material] for this book. The book, an introduction to the principles of programming languages, uses Oz as its preferred idiom for examples.
{{Reflist|30em}}
 
{{Reflist}}
 
==External links==
* {{officialOfficial website|http://mozart.github.io/}}
* [httphttps://mozart.github.io/mozart-v1/doc-1.4.0/tutorial/index.html Tutorial of Oz]
* [http://www.info.ucl.ac.be/people/PVR/distribution.html Programming Language Research at UCL]: One of the core developers of Mozart/Oz, this group does research using Mozart/Oz as the vehicle
* [http://www.informatik.uni-trier.de/~ley/db/conf/moz/moz2004.html ''Multiparadigm Programming in Mozart/Oz: Proceedings of MOZ 2004'']: Conference which gives a snapshot of the work being done with Mozart/Oz
* [http://people.cis.ksu.edu/~xou/505f10/slides/oz.pdf Programming in Oz]
* [httphttps://strasheela.sourceforge.net/strasheela/doc/01-Basics.html Oz Basics]
 
{{DEFAULTSORT:Oz (Programmingprogramming Languagelanguage)}}
[[Category:Multi-paradigm programming languages]]
[[Category:Functional logic programming languages]]
Line 332 ⟶ 345:
[[Category:Educational programming languages]]
[[Category:Programming languages created in 1991]]
[[Category:Academic programming languages]]