論理型言語 Prolog

作成日 : 2008-03-16
最終更新日 :

Prolog とは

論理型言語として、かなり昔にもてはやされた。 現在は昔ほどは流行していないが、そのパターンマッチ (ユニフィケーション)機能は魅力的だ。

また、Prolog に並列処理機能を付加した処理系から出発して、 独自の進化を遂げた言語に Erlang がある。 現在はこちらの人気が高い。

Prolog の本

Prolog の処理系

3 種類の処理系を使ってみた。

gprolog は、Vine Linux のパッケージで apt-install できる唯一の処理系である(Vine 4.1 から Vine 5.1 まで)。 一方、swi-prolog は、無料で取得できる処理系の中で最もよく知られている。 N-Prolog については後述する。

N-Prolog

N-Prolog は笹川賢一氏による Arity/Prolog32 互換の処理系である。下記から入手できる:
https://github.com/sasagawa888/nprolog
(2022-12-24 追記)

N-Prolog のインストール

WSL2 (Windows Subsystem for Linux 2 ) の Ubuntu 22.04 に問題なくインストールできた。(2022-12-24)

tests ディレクトリ

今年(2022 年)は Prolog 生誕 50 年にあたる( Prolog が誕生したのが 1972 ごろだという)。そこで、 N-Prolog の tests ディレクトリにあるプログラムを解読することにした。以下、下記で読み込んだプログラムに対して行った結果を示す。 $ npl -c xxx.pl

example ディレクトリ

tests ディレクトリに引き続き、N-Prolog の example ディレクトリについても調べてみた。tests ディレクトリと重複しているのもある。

プログラムの説明

ack.pl

アッカーマン関数である。再帰のテストによく用いられる。私の環境では、

?- ack(4,1,X).

で 30 秒経ってもプロンプトが返ってこなかった。

animal.pl

命題論理を示すためのデータベースである。規則が animal.pl に書かれている。参考:
https://qiita.com/sym_num/items/3ce8e53d81a32c53b17b

?- 動物(X).
X = 人間 ;
X = ジョー ;
X = 鷲 ;
X = すずめ ;
no
?- 動物(ジョー).
yes
?- ほ乳類(熊).
yes
?- ほ乳類(鷲).
no

bagof.pl

setof/3, bagof/3, findall/3 の使い方である。コメントに書かれているような入力をすればいい。

bignum.pl

大きな数が扱えるかどうかのテストである。

?- foo(1,X).
X = 111111111111111111111111111111111111111111111111111111111111111111112

diff.pl

記号微分である。

?- dif(x^3+3*x,x,Ans).
Ans = 3*x^(3-1)+3 .
yes
?- dif(cos(x)+sin(x),x,Ans).
Ans = -sin(x)+cos(x).
yes

tests ディレクトリは diff.pl の形で、example ディレクトリには dif.pl として収められている。 両者は 2 行異なるが、どちらも tests/diff.pl のほうが正しい。

hiroi.pl

広井誠(M.Hiroi)氏の Prolog プログラムのライツアウトを解く問題。
http://www.nct9.ne.jp/m_hiroi/prolog/prolog11.html

?- solve(0x1ffffff).

11000
11011
00111
01110
01101

10110
01110
11100
11011
00011

01101
01110
00111
11011
11000

00011
11011
11100
01110
10110
no

iidaka.pl

飯高茂氏の著書「Prolog で作る数学の世界」にあるプログラム(なお飯高氏のよみは「いいたか」である)。

math.pl

完全数かどうかを判定する。ある数が完全数であるとは、その数の約数すべて(1を含み、その数自身を含まない)の和がその数と等しいことをいう。 6 や 28 は完全数である。ここでは perfect(N) が N が完全数のとき yes を、そうでないとき no を返す。 perfect(N) を求めるために divisor(N, A) が定義されている。これは N の約数をリストにして A に返す関数である。

?- perfect(6).
yes
?- perfect(28).
yes
?- perfect(27).
no
?- divisor(6,N).
N = [6,3,2,1] .
yes
?- divisor(28,N).
N = [28,14,7,4,2,1]

queens.pl

いわゆる N-クイーン問題の N = 9 の場合を解く。

?- test.
[1,3,6,8,2,4,9,7,5]
[1,3,7,2,8,5,9,4,6]
[1,3,8,6,9,2,5,7,4]
(中略)
[9,7,2,4,1,8,5,3,6]
[9,7,3,8,2,5,1,6,4]
[9,7,4,2,8,6,1,3,5]
no

SWI-Prolog のインストール(Vine 4.1)の場合

世の中では、gprolog より、swi-prolog のほうが、評判が高い。 まず、swi-prolog のダウンロードサイトにある rpm パッケージを入れてみようとしたが、 下記のメッセージが出てエラーとなった。

$ rpm -ivh ~/rpm/pl-5.6.52-310.i586.rpm
エラー: 依存性の欠如:
				/usr/bin/../pl.sh は pl-5.6.52-310.i586 に必要とされています
				libexpat.so.1 は pl-5.6.52-310.i586 に必要とされています
				libpthread.so.0(GLIBC_2.3.3) は pl-5.6.52-310.i586 に必要とされています

従って、ソースをコンパイルした。 特に問題なく、次の手順で使える。

$ ./configure
$ make
$ sudo make install

swi-prolog のインストール(Vine 5.1)の場合

久しぶりに Vine Linux を使うことにした。バージョンは 5.1 である。 また、swi-prolog のバージョンは 5.8.3-376であった。

$ rpm -ivh pl-5.8.3-376.i586.rpm 
エラー: 依存性の欠如:
	rpmlib(PayloadIsLzma) <= 4.4.2-1 は pl-5.8.3-376.i586 に必要とされています

この原因を追求するのもよいが、ダウンロード先では「バイナリーは移植性に乏しいのでソースからコンパイルするように」 と書かれていた。そこで、ソースコードからコンパイルした。

% tar zxvf pl-5.8.3.tar.gz
% cd pl-5.8.3/src
% ./configure
% make
% su
# make install

途中でウォーニングは出たが、取りあえずコンパイルは完了した。 うまく動くだろうか。

$ pl
Welcome to SWI-Prolog (Multi-threaded, 32 bits, Version 5.8.3)
Copyright (c) 1990-2009 University of Amsterdam.
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions.
Please visit http://www.swi-prolog.org for details.

For help, use ?- help(Topic). or ?- apropos(Word).

?- halt.
$

無事起動し、無事終了した。

swi-prolog のインストール(Vine 6.3)の場合

2016 年 2 月 14 日である。久しぶりに Vine Linux を使うことにした。バージョンは 6.3 である。

git をインストールするのがよいと swi-prolog のページにあったので、 まずは git をインストールした。

$ sudo apt-get install git

さて、swi-prolog のインストールだ。

参考 http://www.swi-prolog.org/git.html

$ git clone https://github.com/SWI-Prolog/swipl
$ cd swipl
$ ./prepare
(中略)
Do you want me to run git submodule update --init? yes
(中略)
All submodules are up-to-date

Could not find documentation. What do you want to do?

1) Download and unpack documentation from http://www.swi-prolog.org
and do this again automatically next time
2) Download and unpack documentation from http://www.swi-prolog.org
and ask next time
3) Warn only

Option? 1
(中略)
$ cp -p build.templ build
$ ./build
All submodules are up-to-date
Generating configure in ./src ... configure.in:4: error: Autoconf version 2.66 or higher is required
configure.in:4: the top level
autom4te: /usr/bin/m4 failed with exit status: 63
autoheader: '/usr/bin/autom4te' failed with exit status: 63
configure.in:4: error: Autoconf version 2.66 or higher is required
configure.in:4: the top level
autom4te: /usr/bin/m4 failed with exit status: 63
done
Your kit is prepared.
Please consult INSTALL for further instructions.
./configure: 行 77: ../src/configure: そのようなファイルやディレクトリはありません

さあどうしよう。上で、

Generating configure in ./src ... configure.in:4: error: Autoconf version 2.66 or higher is required

と出ているところが問題なのだろう。調べた。

$ autoconf --version
autoconf (GNU Autoconf) 2.65
Copyright (C) 2009 Free Software Foundation, Inc.

ガーン!バージョンがコンマ01足りない。

今度は最新版の autoconf を入れることにした。まったく Vine Linux はバージョンが古いなあ。

$ cd ../autoconf-2.69
$ ./configure
checking for a BSD-compatible install... /usr/bin/install -c
(中略)
checking for GNU M4 that supports accurate traces... configure: error: no acceptable m4 could be found in $PATH.
GNU M4 1.4.6 or later is required; 1.4.16 or newer is recommended.
GNU M4 1.4.15 uses a buggy replacement strstr on some systems.
Glibc 2.9 - 2.12 and GNU M4 1.4.11 - 1.4.15 have another strstr bug.
$

おかしいな。m4 は普通入っているはずだが。

$ m4 --version
m4 (GNU M4) 1.4.14
Copyright (C) 2010 Free Software Foundation, Inc.
(後略)
$

これもバージョンが古いのか。困ったな。m4 も最新バージョンを入れるか。

$ cd ../m4-1.4.17
$ ./configure
$ make
$ sudo make install
$ m4 --version
m4 (GNU M4) 1.4.17
Copyright (C) 2013 Free Software Foundation, Inc.
(後略)
$

まずはほっとする。次は autoconf だ。

$ ./configure
$ make
$ sudo make install
$ autoconf --version
autoconf (GNU Autoconf) 2.69
Copyright (C) 2012 Free Software Foundation, Inc.
$

autoconf まではうまくいったようだ。もとに戻ろう。

$ ./build
All submodules are up-to-date
(中略)
pl-gmp.c: In function ‘cmpFloatNumbers’:
pl-gmp.c:983: error: ‘V_MPZ’ undeclared (first use in this function)
pl-gmp.c:983: error: (Each undeclared identifier is reported only once
pl-gmp.c:983: error: for each function it appears in.)
(中略)
make: *** [lite] エラー 2
$

どうやら、gmp (GNU の多倍長パッケージ)の読み込みの問題らしい。

$ sudo apt-get install gmp-devel
$ make clean
$ ./configure
$ make
$ sudo make install
$

これで懸案の問題は解決した。

SWI Prolog のインストール(Windows 版)

2022-12-24 に SWI Prolog の Windows 版をインストールした。コンソールから、 swipl で起動できる。

C:> swipl
Welcome to SWI-Prolog (threaded, 64 bits, version 9.0.3)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.

For online help and background, visit https://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).

1 ?-

まりんきょ学問所関数型言語手習い > 論理型言語 Prolog


MARUYAMA Satosi