2 ms·
Nice! >> As I understand it, Prolog itself is CLP(H), that is CLP over the Herbrand terms (that is, atoms and functors and stuff). That's an interesting way t
by YeGoblynQueenne 4y ago
Nice!
>> As I understand it, Prolog itself is CLP(H), that is CLP over the Herbrand terms (that is, atoms and functors and stuff).
That's an interesting way to see it. Quite an unorthodox one, mind. I learned recently that it was Alain Colmerauer, co-creator of Prolog, who also came up with the idea of Constraint Logic Programming, specifically to address Prolog's er difficulties with numbers and arithmetic. See:
https://youtu.be/74Ig_QKndvE?t=1046 https://youtu.be/74Ig_QKndvE?t=1046
I was recently asked a question about the use of Peano notation in some stuff I was working on. I didn't think of it that way at the time, but one big advantage of Peano notation compared to is/2 is that it is... well, in a nutshell it's declarative integer arithmetic, just like CLP(FD). Boy I hate saying jargon-y things like that. But Peano numbers as Prolog terms in predicate's heads can be backtracked-over, and unwound when recursion unwinds, just like you do in your rle2/2 above. The downside is that they get hard to read once they get too big.
Then again, you can easily do this sort of thing:
%! peano(?Arabic,?Peano) is nondet.
%
% Convert between Arabic and Peano number notations.
%
% Use this program to quickly convert between Peano notation and
% Arabic notation.
%
% Accepted modes are (+,-) and (-,+). In mode (?,?) this predicate
% returns only 0 deterministically.
%
% Examples:
% ==
% ?- peano(1,P).
% P = s(0).
%
% ?- peano(I,s(0)).
% I = 1.
%
% ?- peano(N,P).
% N = P, P = 0.
% ==
%
peano(N,P):-
peano(P,0,N).
peano(0,N,N):-
!.
peano(s(P),N,Acc):-
succ(N,N_)
,peano(P,N_,Acc).
Expects Peano numbers with the functor "s"!
Runs both ways :P