8 Prolog Potpourri (or Fun with Prolog)
Solutions
8.1 Crypto-arithmetic Puzzle
Solve the puzzle:
S E N D + M O R E ------------ M O N E Y
Different letters represent different decimal digits.
Implement a general method for solving crypto-arithmetic puzzles:
puzzle( X, Y, Z).
X, Y, Z are lists of letters (Prolog variables)
or the discovered digits and "Z = X + Y" (elementwise).Example:
?- puzzle(
[0, S, E, N, D],
[0, M, O, R, E],
[M, O, N, E, Y]).
⇒ S = 9, E = 5, N = 6, D = 7, M = 1, O = 0, R = 8, N = 6, Y = 2
Hint: The following predicates could be useful:
add1( X, Y, Z, Cin, Cout)
/* 10 * Cout + Z = X + Y + Cin. */
delete( X, Y, Z)
/* The list Z results from the list Y by nondeterministic deletion of the element X.*/
Solution 8.1
8.2 Three Friends
The three friends Bill, Daniel und Michael live in different cities and have different jobs. Each year they meet in Zurich, where one of them lives, and take part in the Silvester run. This time Michael was faster than his friend from Bern and Daniel was faster than the officer. The fastest was the sport teacher as always. Daniel lives in Basel, and Michael is a doctor.
Who is who ?
Solution 8.2
8.3 An Intelligence Competion
A Prolog program won an intelligence competion. This was a final proof that the Artificial Intelligence can be realized. In the competition sequences of numbers were presented and the competitors had to guess the next following numbers. It looked like:
?- iq( [ 2, 4, 6], X) ⇒ X = 8
?- iq( [ 4, 7, 12, 19, 28], X) ⇒ X = 39
?- iq( [ 1, 1, 2, 3], X) ⇒ X = 5
?- iq( [ 1, 2, 4, 7], X) ⇒ X = 11
Implement such an intelligent programm.
Solution 8.3
8.4 Language Translation
The students of the lecture Applied Artificial Intelligence achieved a breakthrough in the automatic language translation! They wrote a simple Prolog program (only several lines) that can translate from German into English. The program can even learn and so improve the performance over time. The program uses the following strategy: :
- Sentences (or partial sentences) are translated if possible (if the translation is known).
- Individual words are translated if the translation is known.
- Unknown words are displayed.
The user can add new translations that are stored and used in the future. Examples:
?- consult( 'dictionary.pdb').
/* consults the file with the known translations, z.B.:
word( er, he).
word( geht, goes).
sequence( [ nach, hause], [ home]).
sequence( [ zu, hause], [ at, home]).
*/
?- translate( [ er, geht, nach, hause], E) ⇒ E = [ he, goes, home]
?- translate( [ er, ist, zu, hause], E) ⇒ E = [ he, ?, at, home]
?- learn( ist, is). ⇒ OK, teacher!
?- translate( [ das, haus, ist, gross], E) ⇒ E = [ ?, ?, is, ?]
?- translate( [ wie, geht, es, ihnen], E) ⇒ E = [ ?, goes, ?, ?]
?- learn( [ wie, geht, es, ihnen], [ how, are, you])
⇒ OK, teacher!
?- translate( [ wie, geht, es, ihnen], E) ⇒ E = [ how, are, you]
?- save( 'dictionary.pdb')
/* The internal translations are saved in a file. */