GHCとは
https://ja.wikipedia.org/wiki/Guarded_Horn_Clauses/
インストール方法
今回は、SWI-Prolog上に実装されたGHCの処理系をインストールする。
SWI-Prologは SWI-Prolog からインストールできる。
次に、 Software from UEDA Lab. から GHC system running on top of SWI-Prolog をダウンロード、展開する。
展開したディレクトリに移動し、
$ swipl Welcome to ~... ?- ['ghcswi.pl']. Warning ~... true. ?-
これでGHCプログラムを実行できる様になった。
GHCプログラムのコンパイルはghccompile/1
で行う
?- ghccompile('fib.ghc'). go/1', 'fibonacci/2', 'fib/4', 'outterms/2', ''END.' true.
実行は
?- ghc go(10). 1 1 2 3 5 8 false.
fib.ghcはこんな感じ
go(Max) :- true | fibonacci(Max, Fs), outterms(Fs, Os), outstream(Os). fibonacci(Max, Ns) :- true | fib(Max, 0, 1, Ns). fib(Max, N1, N2, Ns0) :- N2 =< Max | Ns0 = [N2 | Ns1], N3 := N1 + N2, fib(Max, N2, N3, Ns1). fib(Max, N1, N2, Ns0) :- N2 > Max | Ns0 = 0. outterms([X|Xs1], Os0) :- true | Os0 = [write(X), nl | Os1], outterms(Xs1, Os1). outterms([], Os0) :- true | Os0 = [].