LoginSignup
10
9

More than 5 years have passed since last update.

Mozart2 - Oz言語 を利用する (OSX,非emacs,CUI)

Last updated at Posted at 2015-01-08

動機

僕は emacs が使えないので、普段の環境で「コンピュータプログラミングの概念・技法・モデル」(通称ガウディ本)の例題を実行できるようにしたい。この例題がちゃんと解けないと会社で居場所がなくなってしまう。恐怖と戦いながらこれを書いている。

普段の環境とは、Mac OSX と bash と vi だ。

Mozart2 のインストール

こちらを参考に実施した。

パスの特定

上の手順通りやっていれば、ozcは以下の場所にいる。

/Applications/Mozart2.app/Contents/Resources/bin/ozc

これから毎日使うので、パスを通しておく。

echo 'export PATH=$PATH:/Applications/Mozart2.app/Contents/Resources/bin' >> ~/.bash_profile

実行

とりあえず、以下のようなファイルは実行できる。

functor _
require
   System
prepare
    % システムの関数を使い、文字列を表示するプロシージャ
    proc {Show T}
       {System.showInfo {GetMsg T}}
    end

    % 文字列を返す関数
    fun {GetMsg N}
       % 文字列連結は "#"
       'Hello ' # N # '!!'
    end

    {Show 'Oz'}
end

実行する。

# -e でコンパイルして実行する(デフォルト)
$ ozc -e test.oz

Hello Oz!!

動いた。

階乗を求める

「階乗はHello World」とは上司の言である。

$ cat fact.oz
functor _
require
  System
prepare
  fun { Fact N }
    if N == 0 then 1
    else
      N * {Fact N -1 }
    end
  end
  { System.showInfo {Fact 10} }
end

$ ozc fact.oz
3628800

ozc の help

--help で見れる。 -eが Compile and execute である。

$ ozc --help
Usage: x-oz://system/Compile.ozf { [option] | [file] }
You have to choose one of the following modes of operation:
-h, -?, --help                Output usage information and exit.
-e, --execute, --mode=execute Compile and execute a statement.
                              This is the default mode.
-c, --dump, --mode=dump       Compile and evaluate an expression,
                              pickling the result
                              (file extension: .ozf).
-x, --executable, --mode=executable
                              Compile and evaluate an expression,
                              making result executable
                              (file extension: none).
-E, --core, --mode=core       Compile a statement to core language
                              (file extension: .ozi).
-S, --scode, --mode=scode     Compile a statement to assembly code
                              (file extension: .ozm).
-s, --ecode, --mode=ecode     Compile an expression to assembly code
                              (file extension: .ozm).

.. 略 ..

続く

日本語の wikipedia は寂しいもんだけど、英語 wikipedia はかなり充実してる。

公式プロジェクトのwikiにも軽く目は通しておきたい。後で。。
https://github.com/mozart/mozart2/wiki

ozc については本にはほとんど触れられていない(付録の「コマンドラインインターフェイス」に半ページほど記載がある)。なかなか動作させることができず、ファイルを実行するトリックについては、以下のやりとりを参考にした。

System.showInfoだと、本で使われている Browse とか Show のような、inspect 的な出力が出来ない。リストの中身の出力とか、いろいろ足りない部分がある。こっから先は標準ライブラリの中身とか調べていくしかなさそうだ。

というか、そもそも、コンピュータプログラミングの概念・技法・モデルの内容は 「Mozart1.3に近い」と明記されているわけであるから、そっちを使うべきなんじゃないかと思ってきた。

考えるべきではないことを考えそうになったので、ここで一旦終わることにする。>>続きがこちら

10
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
9