The document discusses how to teach arithmetic concepts like addition and multiplication to a computer using PROLOG. It describes modeling the concepts after how children naturally learn them. Addition is defined recursively and grounded in the basic concept of counting numbers. With just two PROLOG rules, the computer can now solve addition problems. Multiplication can also be implemented in a similar recursive manner, grounded in previous knowledge.
Teach Arithmetic to Computers with PROLOG in 2 Lines
1. Understanding Symbolic Artificial Intelligence
How a computer can learn arithmetics with PROLOG
It’s child’s play !
( can be read and understood by non-programmers )
Jean Rohmer
Pôle Universitaire Léonard de Vinci, Paris La Défense
Can we teach arithmetics (numbers, addition, multiplication, ..) to a computer? You tell me that a
computer already knows arithmerics, its circuits are designed for that purpose ! This is not the point.
We want to express mathematical knowledge and embed it into a computer, then ask it to solve
arithmetic problems, like : give me all couples of integer numbers X and Y such that their product
equals 80.
If we succeed, we can contemplate to express other kinds of knowledge, transmit them to a
computer, and solve automatically a variety of problems. This is the purpose of symbolic artificial
intelligence.
N.B. You eventually will see that a comprehensive knowledge about addition takes just two (2) lines
of PROLOG.
What is the knowledge about arithmetics ? How to teach it ?
We will proceed the very way young children learn to « count», when between 2 and 5 years old.
First, we teach them numbers, using « nursery rythmes » (« comptines » in french, « cuento
infantil » in spanish) :
One, two, three, four …
What is after seven ? Eight. Good !
How far can you count ? Twenty. Excellent !
Using PROLOG, we can tell the computer :
next(one, two).
next(two ,three).
next (three, four).
next (four, five).
…
next( eleven, tvelve).
2. /* You stop when you want
/* Indeed you can make spelling mistakes
Now, you can ask questions to the computer :
next(three, X) ?
It gently answers : X = four
next (eleven, X) ? : X = tvelve
More difficult (for children)
next (X, ten) ? : X = nine
What are the two numbers before six ?
next(X,Y), next(Y,six) ?
X = four, Y = five
next( three, four) ? : « true »
next( three, seven) . ? : « false »
next( three, dad) ? : « false »
next(X,Y) ?
X = one, Y = two
X = two, Y = three
X = three, Y = four
…
X = eleven, Y = tvelve
Not so bad. Let us come to additions.
A first solution is to teach the addition table :
sum(one, one, two)
sum(one, two, three)
…
sum(one, eleven, tvelve)
…
3. sum(three, five, eight)
sum(three,six, nine)
…
Then you can ask :
sum(three, four,X) ? : X= seven
Great ! And more, the computer knows already enough to perform substraction :
sum(three, X,seven) ? X = four
sum(X,Y,four) ?
X = one, Y = three
X = two, Y = two
X = three, Y = one
Well, this is not very intelligent, just learning by heart, pure memorization, but we are so proud when
our little darlings achieve it.
We prefer however when they start reasoning by themsemves : « to add three to five, I start from
five and I proceed to the next number three times : six, then seven, then eight. They have discovered
an algorithm !
But to discover it, they first must have understood what « addition » really means in real life, what is
the « semantics » of « addition ».
Mom has some candies in her bag, Dad has other ones in his bag. An addition occurs when they pour
their bags in my bag. The number of candies in my bag is the sum of the numbers in their bags.
Very soon children have some intuitions about this operation : the more candies in Mom and Dad
bags, the more candies in my bag.
Then, as rationale and practical thinking agents, they identifiy the simplest –minimal- statement of
this property : one more candy in Mom or Dad bags means one more candy in My bag.
They just discovered one of the Peano axioms. (https://en.wikipedia.org/wiki/Peano_axioms):
« if a + b = c , then a + (b+1) = (c+1) »
Or :
« to ensure that a + (b+1)= (c+1), a sufficient condition is that a + b = c »
We can express this knowledge in PROLOG :
sum(Mom_old,Dad_new,My_new):-
next(Dad_old,Dad_new),next(My_old,My_new),sum(Mom_old,Dad_old,My_old).
4. The symbols :- in A :- B,C,D
can be read as : A is true if B and C and D are true
The knowledge above reads as :
If there is one more candy in Dad bag : next(Dad_old,Dad_new),
Then there is one more candy in My bag : next(My_old,My_new),
Provided that Mom bag remains unchanged : Mom_old is used in both additions.
Mathematics exist only because humans agree on this perception of the universe, around the age of
3.
Our definition of addition is recursive : one computes the addition of large numbers by increments
of one on the result of additions of smaller numbers. But this recursion cannot be infinite, it must
stop, since one has no smaller number than one.
We must ground the knowledge about sum on the only other knowledge we have, i.e. about the
succession of numbers, expressed by next(,).
The solution is –as young children find by themselves- to realize that adding one to a number is
equivalent to finding its next number, which is expressed by :
sum(X,one,Y):-next(X,Y).
Et voilà !
----------------------------------
With just these two statements -plus the next(,) declarations :
sum(X,one,Y) :- next (X,Y).
sum(Mom_old,Dad_new,My_new):-
next(Dad_old,Dad_new),next(My_old,My_new),sum(Mom_old,Dad_old,My_old)
Our computer has learned addition.
--------------------------------------------------
With Prolog, you can check :
sum(three,four,X) ?
X = seven
sum(two,X,six) ?
X = four
sum(X,Y,five)
5. X=one, Y= four
X=two, Y = three
X=three, Y = two
X=four, Y = one
To run this program, you can download SWI Prolog, developed by the University of Amsterdam :
http://www.swi-prolog.org/Download.html
and create a file containing :
next(one,two).
next(two,three).
next(three,four).
next(four,five).
next(five,six).
next(six,seven).
next(seven,eight).
next(eight,nine).
next(nine,ten).
next(ten,eleven).
next(eleven,tvelve).
sum(X,one,Y):-next(X,Y).
sum(Mom_old,Dad_new,My_new):-
next(Dad_old,Dad_new),next(My_old,My_new),sum(Mom_old,Dad_old,My_old).
To check if you have understood, try now to implement multiplication, writing « mult » so that
mult( two,three,X) answers X = six
Some help : it takes also two lines, and follows the same principles as sum : recursive definition of
mult, and grounding to previous knowledge …
You should also try to extend the bags story to multiplication.