[squeak-dev] [ANN] Prolog update

St├ęphane Rollandin lecteur at zogotounga.net
Tue Oct 4 22:13:58 UTC 2022


> Could this be easily done using Prolog?

Very easily. Prolog is the king of pattern-matching!

For example, the append/2 predicate is implemented as

	logicAppend

	append([], X, X).
	append([H | X], Y, [H | Z]) :- append(X, Y, Z).

and this is the *actual code* for the method #logicAppend in class Prolog.

Now equipped with this, you can do

	append([a, b], [c, d], List)

and you will get the unification (Prolog's uber-notion for pattern-matching)

	List = [a,b,c,d].

So far, so good. But you can also do

	append(A, B, [1, 2])

which will give you the three ways two lists A and B can be appended to 
get [1,2]. The answer will be

	A = []. B = [1,2].
	A = [1]. B = [2].
	A = [1 2]. B= [].

But there is more. You can in fact use any pattern in the arguments, and 
unification will tell you what works.

For example,

	append([X | T], [a], [Z, Z, Z])

will give you the only way it is possible to append [a] to some [X | T] 
(that's a cons cell with car X and cdr T) and get some [Z, Z, Z] as 
result (note that only the capitalized terms are variables). It is:

	X = a. T = [a]. Z = a

The code snippets above can be directly tested in the REPL you get by 
evaluating

	Prolog new openGui

Best,

Stef


More information about the Squeak-dev mailing list