Re: Prolog Programming for AI, problem 7.1




Lash Rambo wrote:
I'm soldiering through "Prolog Programming for Artifical Intelligence" by
Bratko, but have run into a nasty exercise. It's 7.1, and reads:

"
Write a procedure 'simplify' to symbolically simplify summation
expressions with numbers and symbols (lower-case letters). Let the
procedure rearrange the expressions so that all the symbols precede
numbers. These are examples of its use:

?- simplify(1 + 1 + a, E).
E = a + 2

?- simplify(1 + a + 4 + 2 + b + c, E).
E = a + b + c + 7

?- simplify(3 + x + x, E).
E = 2*x + 3
"

I hit the same exercise today, and eventually came up with the
following. It doesn't use anything that Bratko hasn't covered in his
book up to that point. The only outside functions it uses are the count
procedure from his book (which appears in the same section as the
exercise so I suspect you're supposed to use it) and the delete
procedure that comes with SWI Prolog (but Bratko does a delete earlier
in the book on page 69).

The only thing I couldn't get rid of were the parentheses in the answer
(maybe this is an artifact of SWI?). The output it gives for the
examples in Bratko's question are:

134 ?- simplify(1+1+a,E).

E = a+2

Yes
135 ?- simplify(1+a+4+2+b+c,E).

E = a+ (b+ (c+7))

Yes
136 ?- simplify(3+x+x,E).

E = 2*x+3
=====================
% Ex 7.1. simplify additions
% X is compound expression, A is simplied expression
% The idea is to calculate the numeric sum, and build
% a list of the atoms in the compound expression, then
% convert the list of atoms into the correct format
simplify(X, A) :-
simplify(X, Sum, Atoms),
atomSums(A, Sum, Atoms).

% If X is an atom, there are no numbers so Sum is 0
% Atom list contains the atom
simplify(X, Sum, [X]) :-
atom(X),
Sum is 0, !.

% If X is a number, then Sum is X, and the atom list
% is empty
simplify(X, Sum, []) :-
integer(X),
Sum is X, !.

% Recursive case
simplify(X, Sum, Atoms) :-
X = Y + Z, % Split compound expression
simplify(Z, SumZ, AtomsZ), % Simplify each part
simplify(Y, SumY, AtomsY),
Sum is SumY + SumZ, % Add the subsums together
append(AtomsY, AtomsZ, Atoms),!. % Join the sub-lists of atoms

% Convert the list of atoms to the simplified format
atomSums(A, A, []). % If no atoms, expression is just
the sum

% Recursive case
atomSums(A, Sum, [T|Atoms]) :- % Get first atom T in list
count(T, [T|Atoms], CountT), % Count number of times it occurs
in list
delete(Atoms, T, NewAtoms), % Delete it from list so you don't
count it again
atomSums(B, Sum, NewAtoms), % Convert remainder of list
buildExp(A, B, T, CountT), !. % Build the expression

% Only print out the count for an atom if it's > 1
buildExp(A, B, T, CountT) :-
CountT > 1,
A = CountT * T + B, !
;
A = T + B, !.

% count procedure from Bratko's book, page 150.
count(_,[],0).
count(A, [B|L],N) :-
atom(A), A = B, !,
count(A,L,N1),
N is N1 + 1
;
count(A,L,N).

.



Relevant Pages

  • Re: NOVA on Emergence
    ... always precisely the sum of its constituent parts. ... lbs of plastic ... # of carbon atoms ... walking water, that's what you are. ...
    (talk.origins)
  • Re: Problem using solve... keeps busy
    ... However, if you then simplify() the result, you get a *large* ... Then at the end of the sum, there are a few lines of other terms ... and you simplifyafter substitution (applying the ... a=x into the original equation ...
    (comp.soft-sys.matlab)
  • Re: Simplify formula for iterative programming
    ... character set is getting munged. ... >I wondered I anybody knew about analog simplifications to simplify H: ... >This simplied formula is much easier in iterative programming, ... If, as I guessing, your H is the sum over i,j of point distances from ...
    (comp.programming)
  • Re: Help with equation
    ... how do you simplify that by taking the log of both sides? ... to me like you have a sum there on the right side, ... minus 30th root of ... take the positive root, because then you get a number that makes the ...
    (comp.sys.hp48)
  • Re: Simplify formulas
    ... Cutting your range down (to make testing easier): ... will sum every value from the 5th largest to the largest. ... >I would like to simplify the following formulas ...
    (microsoft.public.excel)