Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Oz (programming language)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Language overview== ===Data structures=== 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: * Numbers: floating point or integer (real integer) * Records: for grouping data : <code> circle(x:0 y:1 radius:3 color:blue style:dots)</code>. 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: <code> circle(1:0 2:1 3:3 4:blue 5:dots) </code>. * Lists: a simple linear structure <syntaxhighlight 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> Those data structures are values (constant), [[first-class object|first class]] and [[dynamic typing|dynamically type checked]]. Variable names in Oz 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 | 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 | archive-date = 3 March 2016 | url-status = dead }}</ref> are first class values, allowing [[Higher-order programming|higher order functional]] programming: <syntaxhighlight lang="erlang"> fun {Fact N} if N =< 0 then 1 else N*{Fact N-1} end end </syntaxhighlight><syntaxhighlight 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) end fun {SumList List} case List of nil then 0 [] H|T then H+{SumList T} % pattern matching on lists end end </syntaxhighlight> Functions may be used with both free and bound variables. Free variable values are found using static [[Scope (computer science)|lexical scoping]].<ref name="Scoping"> {{cite journal | 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. <syntaxhighlight lang="erlang"> fun {Square N} % A general function N*N end fun {Map F Xs} % F is a function here - higher order programming case Xs of nil then nil [] X|Xr then {F X}|{Map F Xr} end end %usage {Browse {Map Square [1 2 3]}} %browses [1 4 9] </syntaxhighlight> ====Anonymous functions==== Like many other functional languages, Oz supports use of [[anonymous function]]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. <syntaxhighlight lang="erlang"> {Browse {Map fun {$ N} N*N end [1 2 3]}} </syntaxhighlight> Since anonymous functions don't have names, 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. <syntaxhighlight lang="erlang"> declare fun {Ret X} if X > 0 then 5 else ~5 end end </syntaxhighlight> But Oz also provides a facility in case a function must not 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 <syntaxhighlight lang="erlang"> declare proc {Ret X} if X > 0 then {Browse 5} else {Browse ~5} end end </syntaxhighlight> 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, below, the thread will wait until both X and Y are bound to a value before showing the value of Z. <syntaxhighlight lang="erlang"> thread Z = X+Y {Browse Z} end thread X = 40 end thread Y = 2 end </syntaxhighlight> The value of a dataflow variable cannot be changed once it is bound: <syntaxhighlight lang="erlang"> X = 1 X = 2 % error </syntaxhighlight> Dataflow variables make it easy to create concurrent stream agents: <syntaxhighlight lang="erlang"> fun {Ints N Max} if N == Max then nil else {Delay 1000} N|{Ints N+1 Max} end end fun {Sum S Stream} case Stream of nil then S [] H|T then S|{Sum H+S T} end end local X Y in thread X = {Ints 0 1000} end thread Y = {Sum 0 X} end {Browse Y} end </syntaxhighlight> 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.<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 [[trial division]] algorithm by recursively creating concurrent stream agents that filter out non-prime numbers: <syntaxhighlight lang="erlang"> fun {Sieve Xs} case Xs of nil then nil [] X|Xr then Ys in thread Ys = {Filter Xr fun {$ Y} Y mod X \= 0 end} end X|{Sieve Ys} end end </syntaxhighlight> === Laziness === Oz uses [[eager evaluation]] by default, but [[lazy evaluation]]<ref name="Lazy Programming"> {{cite journal | author = Paul Hudak | author-link = Paul Hudak | title = Conception, evolution, and application of functional programming languages | journal = ACM Computing Surveys | year = 1989 | volume = 21 | number = 3 | pages = 359–411 | doi=10.1145/72551.72554| s2cid = 207637854 }} </ref> is possible. Below, the fact is only computed when value of X is needed to compute the value of Y. <syntaxhighlight lang="erlang"> fun lazy {Fact N} if N =< 0 then 1 else N*{Fact N-1} end end local X Y in X = {Fact 100} Y = X + 1 end </syntaxhighlight> [[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: <syntaxhighlight lang="erlang"> declare fun lazy {Merge Xs Ys} case Xs#Ys of (X|Xr)#(Y|Yr) then if X < Y then X|{Merge Xr Ys} elseif X>Y then Y|{Merge Xs Yr} else X|{Merge Xr Yr} end end end fun lazy {Times N Xs} case Xs of nil then nil [] X|Xr then N*X|{Times N Xr} end end declare H H = 1 | {Merge {Times 2 H} {Merge {Times 3 H} {Times 5 H}}} {Browse {List.take H 6}} </syntaxhighlight> The code above elegantly computes all the [[Regular Number]]s<ref name="Hamming Numbers"> {{cite journal |author1=Rao, AC |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 Theory | 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]] via simple semantics: <syntaxhighlight lang="erlang"> declare local Stream Port in Port = {NewPort Stream} {Send Port 1} % Stream is now 1|_ ('_' indicates an unbound and unnamed variable) {Send Port 2} % Stream is now 1|2|_ ... {Send Port n} % Stream is now 1|2| .. |n|_ end </syntaxhighlight> With a port and a thread, asynchronous agents can be defined: <syntaxhighlight lang="erlang"> fun {NewAgent Init Fun} Msg Out in thread {FoldL Msg Fun Init Out} end {NewPort Msg} end </syntaxhighlight> === State and objects === 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: <syntaxhighlight lang="erlang"> local A X in A = {NewCell 0} A := 1 % changes the value of A to 1 X = @A % @ is used to access the value of A end </syntaxhighlight> With these simple semantic changes, the whole object-oriented paradigm can be supported. With a little syntactic sugar, OOP becomes well integrated in Oz. <syntaxhighlight lang="erlang"> class Counter attr val meth init(Value) val:=Value end meth browse {Browse @val} end meth inc(Value) val :=@val+Value end end local C in C = {New Counter init(0)} {C inc(6)} {C browse} end </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)