SkillAgentSearch skills...

OzCheatSheet

Basics of the Oz/Mozart language ^_^

Install / Use

/learn @alhassy/OzCheatSheet
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

Created 2019-09-24 Tue 13:24

#+OPTIONS: toc:nil d:nil #+OPTIONS: toc:nil d:nil #+TITLE: Oz CheatSheet #+AUTHOR: [[http://www.cas.mcmaster.ca/~alhassm/][Musa Al-hassy]] #+export_file_name: README.org

Basics of the Oz language.

The listing sheet, as PDF, can be found [[file:CheatSheet.pdf][here]], or as a [[file:CheatSheet_Portrait.pdf][single column portrait]], while below is an unruly html rendition.

This reference sheet is built from a [[https://github.com/alhassy/CheatSheet][CheatSheets with Org-mode]] system.

#+toc: headlines 2 #+macro: blurb Basics of the Oz language.

#+latex_header: \usepackage{titling,parskip} #+latex_header: \usepackage{eufrak} % for mathfrak fonts #+latex_header: \usepackage{multicol,xparse,newunicodechar}

#+latex_header: \usepackage{etoolbox}

#+latex_header: \newif\iflandscape #+latex_header: \landscapetrue

#+latex_header_extra: \iflandscape \usepackage[landscape, margin=0.5in]{geometry} \else \usepackage[margin=0.5in]{geometry} \fi

#+latex_header: \def\cheatsheetcols{2} #+latex_header: \AfterEndPreamble{\begin{multicols}{\cheatsheetcols}} #+latex_header: \AtEndDocument{ \end{multicols} }

#+latex_header: \let\multicolmulticols\multicols #+latex_header: \let\endmulticolmulticols\endmulticols #+latex_header: \RenewDocumentEnvironment{multicols}{mO{}}{\ifnum#1=1 #2 \def\columnbreak{} \else \multicolmulticols{#1}[#2] \fi}{\ifnum#1=1 \else \endmulticolmulticols\fi}

#+latex_header: \def\maketitle{} #+latex: \fontsize{9}{10}\selectfont

#+latex_header: \def\cheatsheeturl{}

#+latex_header: \usepackage[dvipsnames]{xcolor} % named colours #+latex: \definecolor{grey}{rgb}{0.5,0.5,0.5}

#+latex_header: \usepackage{color} #+latex_header: \definecolor{darkgreen}{rgb}{0.0, 0.3, 0.1} #+latex_header: \definecolor{darkblue}{rgb}{0.0, 0.1, 0.3} #+latex_header: \hypersetup{colorlinks,linkcolor=darkblue,citecolor=darkblue,urlcolor=darkgreen}

#+latex_header: \setlength{\parindent}{0pt}

#+latex_header: \def\cheatsheetitemsep{-0.5em} #+latex_header: \let\olditem\item #+latex_header_extra: \def\item{\vspace{\cheatsheetitemsep}\olditem}

#+latex_header: \usepackage{CheatSheet/UnicodeSymbols}

#+latex_header: \makeatletter #+latex_header: \AtBeginEnvironment{minted}{\dontdofcolorbox} #+latex_header: \def\dontdofcolorbox{\renewcommand\fcolorbox[4][]{##4}} #+latex_header: \makeatother

#+latex_header: \RequirePackage{fancyvrb} #+latex_header: \DefineVerbatimEnvironment{verbatim}{Verbatim}{fontsize=\scriptsize}

#+latex_header: \usepackage{listings} #+latex: \lstset{language=Oz}

Oz provides the /harmonious/ support for many paradigms; e.g., OOP, FP, Logic, concurrent and networked. Moreover, every entity in Oz is first-class; e.g., classes, threads, and methods.

  • Oz is a dynamically typed language, but strongly so: No coversions are performed; e.g., condition ~5.0 = 5~ raises an exception.
  • It is strong in that

#+latex_header: \def\cheatsheeturl{https://github.com/alhassy/OzCheatSheet}

#+latex_header: \def\cheatsheetcols{2} #+latex_header: \landscapetrue #+latex_header: \def\cheatsheetitemsep{-0.5em}

#+latex_header: \newunicodechar{𝑻}{\ensuremath{T}} #+latex_header: \newunicodechar{⊕}{\ensuremath{\oplus}} #+latex_header: \newunicodechar{≈}{\ensuremath{\approx}}

#+begin_quote

  • [[#intro][Intro]]
  • [[#extra-local-setup][Extra, Local, Setup]]
  • [[#setup][Setup]]
  • [[#variables][Variables]]
  • [[#functions][Functions]]
  • [[#literals][Literals]]
  • [[#records----hashes--tuples][Records ---Hashes & Tuples]]
  • [[#pattern-matching][Pattern Matching]]
  • [[#break][break]]
  • [[#lists][Lists]]
  • [[#lazy-evaluation][Lazy Evaluation]]
  • [[#-is-unification-or-incremental-tell][‘=’ is Unification, or ‘incremental tell’]]
  • [[#control-flow][Control Flow]]
  • [[#break][break]]
  • [[#mutable-state][Mutable State]]
  • [[#classes--objects][Classes & Objects]]
  • [[#reads][Reads]] #+end_quote
  • Setup

Download & install prebuilt binary. #+begin_src shell

Ubuntu:

wget https://github.com/mozart/mozart2/releases/download/v2.0.1/mozart2-2.0.1-x86_64-linux.deb sudo apt install ./mozart2-2.0.1-x86_64-linux.deb oz

Mac OS:

brew tap dskecse/tap brew cask install mozart2 mozart2 #+end_src

Emacs setup ---trying to accommodate Ubuntu and Mac OS. #+begin_src emacs-lisp ;; C-h o system-type ⇒ See possible values. ;; darwin ⇒ Mac OS (setq my-mozart-elisp (pcase system-type ('gnu/linux "/usr/share/mozart/elisp") ('darwin "/Applications/Mozart2.app/Contents/Resources/share/mozart/elisp")))

;; Mac OS needs to know the location. (add-to-list 'exec-path "/Applications/Mozart2.app/Contents/Resources/")

(when (file-directory-p my-mozart-elisp) (add-to-list 'load-path my-mozart-elisp) (load "mozart") (add-to-list 'auto-mode-alist '("\.oz\'" . oz-mode)) (add-to-list 'auto-mode-alist '("\.ozg\'" . oz-gump-mode)) (autoload 'run-oz "oz" "" t) (autoload 'oz-mode "oz" "" t) (autoload 'oz-gump-mode "oz" "" t) (autoload 'oz-new-buffer "oz" "" t))

;; oz-mode annoyingly remaps C-x SPC, so we must undo that. (eval-after-load "oz-mode" '(define-key oz-mode-map (kbd "C-x SPC") 'rectangle-mark-mode))

;; Org-mode setup for Oz; the Oz browser needs output. (require 'ob-oz) (setq org-babel-default-header-args:oz '((:results . "output"))) #+end_src

In an Emacs org-mode source block, executing the following brings up an Oz window ---as desired. #+begin_src oz declare X = 12 {Browse X} #+end_src

All subsequent calls to ~Browse~ will output to the same window, unless it's closed.

Instead, we may use ~Show~ and have output rendered in the Emacs buffer ~Oz Emulator~. #+begin_src oz {Show 'Hello World'} #+end_src

Jargon:

  • Oz :: The programming language at hand.
  • Mozart :: The implementation of Oz.
  • OPI :: The Oz Programming Interface, “OPI”, which is built-around Emacs.
  • Variables

Names that begin with a capital letter; a ~declare~ close affects all following occurrences and so is ‘global’. #+begin_src oz declare V = 1 {Show V} % ⇒ 1 declare V = 2 {Show V} % ⇒ 2 #+end_src One may also make local declarations; e.g., ~local X Y Z in S end~.

  • Functions

Function application is written ~{F X₁ … Xₙ}~ ---without parenthesis!

  • This approach is inherited from [[https://github.com/alhassy/ElispCheatSheet][Lisp]].
  • The last expression in the function body is its “return value”, unless declared otherwise.
  • If you write ~{F(X)}~ you will obtain a ~illegal record label~ error since ~F~ is a function name, not a literal.
  • Use parenthesis only on compound expressions, which is seldom needed since /infix operators bind strongest/.

#+begin_src oz declare fun {Fact Bop N} if N == 0 then 1 else {Bop N {Fact Bop N - 1}} end end

declare fun {Mult X Y} X * Y end {Show {Fact Mult 5}} % ⇒ 120

% Using an anonymous function. {Show {Fact fun {$ X Y} X + Y end 5}} % ⇒ 6

% Two ways to invoke a function. {Show {Mult 5 6}} % ⇒ 30 local X in {Mult 2 3 X} {Show X} end % ⇒ 6

% Erroenous calls: {Mult 5 (99)} {Mult (5) 99} % The following are eqiuvalent: Infix operators bind strongest! {Show {Mult 5 99}} {Show {Mult 2 + 3 9 * 11}} #+end_src

| ~F = fun {$ X₁ … Xₙ} S end ≈ fun {F X₁ … Xₙ} S end~ |

  • Procedure equality is based on names.
  • Mutually declared functions are declared like normal functions.

“Procedure invocation style”: | ~R = {F X₁ … Xₙ} ≈ {F X₁ … Xₙ R}~ |

  • Literals

Literals are symbolic entities that have no internal structure; e.g., ~hello~.

  • There are also ‘names’, which are guaranteed to be worldwide unique.
  • ~{NewName X}~ is the only way to create a name and assign it to ~X~.
  • Names cannot be printed. #+begin_src oz local X Y B in X = foo {NewName Y} B = true {Show [X Y B]} % ⇒ [foo <OptName> true] end #+end_src
  • Records ---Hashes & Tuples

A /tuple/ is a literal that has data with it ---the literal is then referred to as the “label”. If ~T~ is a tuple of $n$ items, then ~T.i~ is item $i ∈ 1..n$. #+begin_src oz declare J

J = jasim('Farm' 12 neato) % Tuple of three values

{Show J} % ⇒ jasim('Farm' 12 neato) {Show J.2} % ⇒ 12 #+end_src

A /record/ is a tuple where the projections ~T.i~ are not numbers but are stated explicitly ---and called “features”. This is also known as a “hash”, where the projections are called “keys”. #+begin_src oz declare J = jasim(work: 'Farm' family:12 title: myman) {Show J} % ⇒ jasim(family:12 title:myman work:'Farm') {Show J.family} % ⇒ 12 #+end_src

This approach is inherited from [[https://github.com/alhassy/PrologCheatSheet][Prolog]].

Tuples are also known as /terms/; everything can be thought of as a term. E.g., we can make trees using terms: #+begin_src oz declare G = grandparent(dad(child1 child2) uncle(onlycousin) scar)

{Show G.1.1} % ⇒ child1 {Show {Value.'.' G 1}} % ⇒ dad(child1 child2)

% {Show G.nope} % ⇒ Crashes since “G” has no “nope” feature % {CondSelect R f d X} ⇒ X = if R has feature f then R.f else d end local X in {CondSelect G nope 144 X} {Show X} end % ⇒ 144

% {AdjoinAt R f v R′} ⇒ R′ is a copy of R additionally with R′.f = v % This is an “update” if R.f exists, and otherwise is a new feature. local H in {AdjoinAt G nope 169 H} {Show H.nope} end % ⇒ 169 #+end_src

| Remember: Commas are useless! |

  • Since everything in Oz is first-class, we have ~r.p ≈ {Value.'.' r p}~.
  • [[https://mozart.github.io/mozart-v1/doc-1.4.0/base/record.html#section.records.records][Here]] is the library of methods for working with records.
    • Which includes folds on records!
  • ~{Arity R X}~ assigns ~X~ the list of features that ~R~ has.

A standard tuple former name is ~'#'~, and it may be used infix by dropping the quotes. #+begin_src oz {Show 1#2#3} % ⇒ 1#2#3 {Show '#'(1 2 3)} % ⇒ 1#2#3 {Show '#'()} % ⇒ '#', empty tuple {Show '#'(1)} % ⇒ '#'(1), singleton tuple #+end_src

Likewise, lists are just tuples, which are just records having label ~'|'~.

  • Pattern Matching

Besides projections, ~record.feature~, we may decompose a record along its “pattern”.

Below, ta

View on GitHub
GitHub Stars20
CategoryDevelopment
Updated2mo ago
Forks4

Security Score

80/100

Audited on Jan 19, 2026

No findings