Showing posts with label prolog. Show all posts
Showing posts with label prolog. Show all posts

Thursday, 29 September 2011

Seven in Seven - Prolog is doing my head in.

It's day 2 of the Prolog section and my brain is suffering, I had forgotten how much unlike other languages Prolog is, and we haven't even got to backtracking yet. Things got so bad that I dug out my original copy of Clocksin and Mellish -It is so old the pages have actually yellowed and the reference implementations are for a DEC-10 and PDP-11! It seems to be helping to think of Prolog as a goal-seeking database, i.e. a collection of facts that you can ask questions of, it's quite cute that when you run a program Prolog answers 'yes' or 'no'.

Anyway the exercises, trivially they can be answered thus :


| ?- reverse([12,4,5,7],SL).

SL = [7,5,4,12]

| ?- min_list([12,4,5,7],SL).

SL = 4

| ?- sort([12,4,5,7],SL).  

SL = [4,5,7,12]

(1 ms) yes

But we ought to try a bit harder, so :

revappend([], Ys, Ys).
revappend([X|Xs], Ys, Zs) :- revappend(Xs, [X|Ys], Zs).

rev(Xs,Ys) :- revappend(Xs,[],Ys).

I pinched this in the end, after repeated attempts to use 'is' on something that isn't an arithmetic expression.

smallest(A,[A|[]]).
smallest(S,[H|T]) :- smallest(Ss,T), H >= Ss, S is Ss.
smallest(S,[H|T]) :- smallest(Ss,T), H < Ss, S is H.
This one is all mine, alas. It's not tail-recursive so it would be inefficient. Here I have added a rule for each case -but it might be possible to use an 'if' statement.

Finally the sort -I can't imagine that this is very efficient either, it works by taking the smallest member off the original list and putting it add the head of a new list, this happens repeatedly (through recursion) until the original list is empty :-


jsort([], Ys, Ys).
jsort(Xs, Ys, Zs) :- min_list(Xs,S), delete(Xs,S,X1s), jsort(X1s, [S|Ys], Zs).

sortj(Xs,Ys) :- jsort(Xs,[],Ys).

Monday, 26 September 2011

Seven in Seven - Prolog Day 1

I know it's out of sequence, but Io isn't making on the work Mac (it's fine on Fedora) so I'll jump ahead to Prolog for now. Prolog, it's nearly 30 years since I last had a play with Prolog and I eventually developed a soft spot for it -let's see if I still feel this way nowadays, or whether the soft spot is the bottom of the compost heap.

From memory Prolog stands for 'Programming with Logic' and it was popular with the Japanese AI community back in the 1980s, when they were taking over the world.  It is based around 'first order predicate calculus', and if I had my notes I'd tell you what it meant.  One of the main things to take from it though, is that Prolog is a formal language based on mathematics, and thus (in some sense) provable. Another formal language, that you may have heard of? SQL is based on set theory and in an ideal world is provable -E & OE, YMMV, not all implementations are equal, shares may go down as well as up!

First black mark, likes(X, Y) isn't the same as likes (X, Y) -the first is correct- in GNU Prolog.

Exercise : Create a knowledge base representing musicians and instruments. Also represent musicians and their genre of music.

I did this using tuples :
musician('Richard Thompson','Guitar','Folk') .
musician('Kathryn Tickell','Uillean Pipes','Folk') .
musician('John Lee Hooker','Guitar','Blues') .
 Which allows me to write queries like :

| ?- musician(Who,'Guitar',Genre).

Genre = 'Folk'
Who = 'Richard Thompson' ? a

Genre = 'Blues'
Who = 'John Lee Hooker'

yes

Very pretty, but at the end of the day it's just a database query.

linkedin