plus

swi-prologでplusという述語発見。isと違って逆演算が出来るのがいい。

?- R is 2+3.
R = 5
?- 5 is 2+R.
ERROR: Arguments are not sufficiently instantiated
?- plus(2,3,R).
R = 5
?- plus(2,R,5).
R = 3
?- plus(R,3,5).
R = 2
?- plus(X,Y,5).
ERROR: succ/2: Arguments are not sufficiently instantiated
?- succ(1,R).
R = 2
?- succ(R,2).
R = 1
?- succ(X,Y).
ERROR: succ/2: Arguments are not sufficiently instantiated

かけ算、累乗とかはないのかな?あれば以下のようなことがやれるのに。

足し算 2+3 → plus(2,3,R)
引き算 5-2 → plus(2,R,5)
かけ算 2*3 → mul(2,3,R)
割り算 6/2 → mul(2,R,6)
累乗 2^3 → pow(2,3,R)
対数 log2 8 → pow(2,R,8)

mul(X,1,X).
mul(X,Y,Z) :- succ(A,Y),mul(X,A,B),plus(X,B,Z).

試しにsuccとplusを使ってかけ算を作ってみた。

?- mul(2,3,R).
R = 6
Yes
?- mul(2,R,6).
ERROR: succ/2: Arguments are not sufficiently instantiated
?- mul(R,3,6).
ERROR: succ/2: Arguments are not sufficiently instantiated
   Exception: (9) plus(_G157, _G157, _L169) ? creep

が、やっぱりそううまくはいかないか…。