Prolog Is Magic

Jul 16, 2019   #Prolog 

It’s hard these days to impress kids with stuff computers can do that felt like magic back in the early days. Turtle graphics, even the venerable Scratch don’t have the wow factor from the get go because .. the kids have seen too much already. Here is how I (re)discovered something magical in a moment of resistant engagement with my 6-yr old when he persistently asked me to “teach him how to work on a computer”.

Well, the title is all revealing, but I’m so excited to tell the story of how this happened! So I’m writing this more for myself than for you, dear reader :)

I had 30 minutes before I had to attend a business call when my son begged “you promised to teach me how to work on computer”. I did promise him, so I couldn’t weasel out of it.

The real reason I was so reluctant was that I have faced repeated failures in my attempts to elicit a single “wow!” from a child - any child. Adolescents and above are a bit more amenable, but I’m talking 5-6 year old types who’ve already touched iPads, watched videos and played games with hi-fi graphics.

As I tend to do, I let all the options swim in my head until the very last second when I absolutely had to commit to something. Just before leaving work, I happened to have been making some tweaks to a “bot” whose sole purpose is to lookup a name in an address book … and I’d written the bot in Prolog (SWI Prolog) … just for fun. So Prolog was somewhat dominant in my head.

I decided to take the plunge. Prolog it is.

So I fired up a SWI Prolog console, with a little apprehension about what might follow. I must’ve been channeling Seymour Papert’s spirit or something, for things just flowed from that point on.

Me (to my son Rud): Let’s teach the computer how old you are.
Rud: Yes!

?- assert(age(rud,6)).
true.

Me: The assert - “ah,suh,suh,eh,ruh,tuh” - “assert” tells the computer to learn things. I’m then saying “age of rud is 6”. 1
Rud: Okay.
Me: Now let’s ask the computer what your age is.

?- age(rud, A).
A = 6.

Me: See? It now knows your age is 6. I typed age(rud,A) where I wrote A because I don’t know your age, but the computer figured it out and told us “A is 6”.
Rud: Yay!

(I’m encouraged by the yay already! .. and I continue.)

Me: So let’s now teach it your brother’s age.

?- assert(age(uth,11)).
true.

Me: It has now learnt Uth’s age. Let’s ask it and see whether it really knows.
Rud: Yes let’s ask.

?- age(uth,A).
A = 11.

Rud: It said 11!
Me: Yes it did.
Rud: Can I teach it your age now?
Me: Of course. You have to type it out like I did with the assert.
Rud: (Shoves me aside and starts finger typing. I help him with the “shift-9” for the parentheses.)

I realize that part of the fun is simply just pushing a button and seeing the character appear on the screen … and nothing better than a shell to give that experience. It probably also helped a bit that I’d enlarged the shell font for ease of reading.

?- assert(age(shicumar,119)).
true.

Note: No, I’m not 119 years old, but just obfuscating the ages for the purpose of this post ;)

Rud: Now let’s teach it amma’s age. (Proceeds to type)

?- assert(age(sho,91)).
true.

Rud: Now Ari’s age. (… continues typing).

?- assert(age(ari,5)).
true.

By this point, he was already a little happy .. and my time was running out, but I wanted more.

Me: So let’s now teach it how to tell who’s older than who.

?- assert((older(A,B) :- age(A,AgeA), age(B,AgeB), AgeA > AgeB)).
true.

Me: So I just taught the computer that if one person’s age is more years than another person, then that person is older. (I glossed over the “magic line”, yes).

Me: Now let’s ask it whether uth is older than rud. What do you think?
Rud: Yes he’s older.

?- older(uth,rud).
true.

Me: It says true. Now let’s ask it if rud is older than uth.
Rud: No I’m not.

?- older(rud,uth).
false.

Rud: Told you!
Me: Now let’s ask it whether amma is older than me.
Rud: No she’s younger.

?- older(sho,shicumar).
false.

Rud: It says correctly!
Me: Now let’s ask it to tell us who are all younger than uth.
Rud: Yes. Me and Ari.

?- older(uth,Who).
Who = rud ;
Who = ari ;
false.

Rud: (Eyes wide open!) HOOOOW DID IT KNOW THAAAAT!

… and that dear men and gentle-ladies, is computing magic for a kid!

Reflection

This unexpected interaction maybe points to some room for improvement in a “prolog for kids”. Even the following minimal change would be an improvement -

?- learn(age(rud,6)).
?- learn(age(uth,11)).
?- learn(age(shicumar,119)).
?- age(rud,What?).
What? = 6.
?- learn(older(A,B) if AgeA > AgeB where age(A,AgeA) and age(B,AgeB)).
?- older(uth,rud).
yes.
?- older(rud,uth).
no.
?- older(uth,Who?).
Who? = rud ;
Who? = ari ;
finished.

Perhaps automatic support for functional notation like how Mozart/Oz does would make this interaction better?

?- learn(age(rud,6)).
?- learn(age(uth,11)).
?- learn(age(shicumar,119)).
?- age(rud).
... is 6.
?- learn(older(A,B) if age(A) > age(B)).
?- older(uth,rud).
yes.
?- older(rud,uth).
no.
?- older(uth).
... is rud ;
... is ari ;
finished.

But I’m not too happy with this in this context, though it adds brevity. For example, the expression older(uth) seems to suggest the question “who is older than uth?“, whereas older(uth,Who?) which is ok to read as “uth is older than who?” since we introduced the teaching as “A is older than B if …” when we wrote older(A,B) :- ....

========


  1. My son can read a bit the phonetic way so this much is doable for him. [return]