<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Dalibor Novak &#187; Prolog</title>
	<atom:link href="http://dalibornovak.co.uk/blog/tag/prolog/feed/" rel="self" type="application/rss+xml" />
	<link>http://dalibornovak.co.uk/blog</link>
	<description>Tales from a grumpy developer...</description>
	<lastBuildDate>Sun, 27 Mar 2011 21:02:13 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.2</generator>
		<item>
		<title>Seven Languages in Seven Weeks &#8211; Prolog &#8211; Day 1</title>
		<link>http://dalibornovak.co.uk/blog/seven-languages-in-seven-weeks-prolog-day-1/</link>
		<comments>http://dalibornovak.co.uk/blog/seven-languages-in-seven-weeks-prolog-day-1/#comments</comments>
		<pubDate>Sun, 27 Mar 2011 20:50:19 +0000</pubDate>
		<dc:creator>Dalibor "Bore" Novak</dc:creator>
				<category><![CDATA[books]]></category>
		<category><![CDATA[Books]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Prolog]]></category>

		<guid isPermaLink="false">http://dalibornovak.co.uk/blog/?p=154</guid>
		<description><![CDATA[I am tackling a third language from the book today. This is Prolog and we are not complete strangers. I remember using it at uni for some lab practices and I can remember we didn&#8217;t get along like a house on fire. I found Prolog strange and at odds with imperative mindset I held back...<a href="http://dalibornovak.co.uk/blog/seven-languages-in-seven-weeks-prolog-day-1/">&#187;</a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://dalibornovak.co.uk/blog/wp-content/uploads/2011/03/prologS.png"><img class="alignleft size-full wp-image-167" title="Prolog" src="http://dalibornovak.co.uk/blog/wp-content/uploads/2011/03/prologS.png" alt="Prolog prompt" width="200" height="185" /></a>I am tackling a third language from <a href="http://dalibornovak.co.uk/blog/seven-languages-in-seven-weeks/">the book</a> today. This is Prolog and we are not complete strangers. I remember using it at uni for some lab practices and I can remember we didn&#8217;t get along like a house on fire. I found Prolog strange and at odds with imperative mindset I held back then. Now that I am a bit older, I find the fact that language is a bit different appealing and intriguing and I look forward to see why was it designed in such manner and what benefits it&#8217;s approach brings.</p>
<p>Day 1 explains the very basic concepts behind the language. It explains its syntax which is very simple. Main constructs are atoms, variables, facts and rules.</p>
<pre class="brush: plain; title: ; notranslate">
%atoms
me.
brother.
mother.
granddad.

%facts
parent(me, mother).
parent(brother, mother).
parent(mother, granddad).

%rules
grandparent(X, Y) :- parent(X, Z), parent(Z, Y).
sibling(X, Y) :- \+(X = Y), parent(X, Z), parent(Y, Z).
% X, Y and Z are variables
</pre>
<p>No syntax highlighting is available &#8211; I guess that the &#8220;hip crowd&#8221; does not grok Prolog.<span id="more-154"></span></p>
<p>Atom is a general purpose name without an inherent meaning. Atoms are written as words starting with a lower case letter, if spaces, special characters or capital letter are required atoms have to be quoted using single quotes.</p>
<p>Variables are place holders for arbitrary terms. They are written as words starting with capital letter or an underscore. A single underscore stands for an anonymous variable matching any term.</p>
<p>Facts are expressed as expressions consisting a name (functor) and a comma separated list of arguments in parenthesis. Functor must following naming convention for atoms.</p>
<p>Rules are expressed as expression consisting of head and body that are separated by :- operator. Head is expressed as a functor followed by arguments. Body consists of sub goals that need to be met in order for the rule to be true.</p>
<p>We use these constructs to express our knowledge about our domain at hand. The idea is that we define a knowledge base about our domain and then question (query) Prolog for possible solutions to our problems within the scope of the defined domain.</p>
<p>So in the example above I&#8217;ve defined (somewhat abstract) knowledge base about my family. Facts define that mother is parent to both me and brother and that granddad is parent of mother.<br />
Then I&#8217;ve defined two rules grandparent and sibling. If I try to read the grandparent rule it reads like this: X is grandparent of Y if there are such X, Y and Z that Z is parent of X and Y is parent of Z. If you look at the rule definition it shouldn&#8217;t be too difficult to map between the previous sentence and rule itself.</p>
<p>Now that I have this knowledge base defined I can query Prolog about it like so:</p>
<pre class="brush: plain; title: ; notranslate">
%is mother my parent?
| ?- parent(me, mother).

yes
%is granddad brother's parent?
| ?- parent(brother, granddad).

no
</pre>
<p>These were very simple queries and Prolog did not have much work as I was just asking it to return the facts I stated. But even with only facts we can still have some fun. When we use a query with a variable Prolog will try to find if there is such combination of facts and rules which can satisfy the query. If it finds it it will print it out.</p>
<pre class="brush: plain; title: ; notranslate">
%Who is parent of brother?
| ?- parent(brother, Who).

Who = mother

yes

%Who's parent is mother?
| ?- parent(Who, mother).

Who = me ? ;

Who = brother ? ;

no
</pre>
<p>In first query we have used Who variable and Prolog was trying to find the value of the variable that would satisfy the query. It finds that mother value fits and prints it out. In second query Prolog finds more than one match: me and brother. When Prolog finds more than one match it will print out the first one it finds and wait. You can then type ; to get next solution, a to get all solutions or &lt;Enter&gt; to stop looking for more solutions. no at the last line of my interaction means that Prolog could not find any more solutions.</p>
<p>And we can have just as much fun with rules that build on top of facts. I will just provide some queries but will not go into detailed explanation of them as you should &#8220;get the drill&#8221; by now:</p>
<pre class="brush: plain; title: ; notranslate">
%is granddad brother's grandparent.
| ?- grandparent(brother, granddad).

yes

%is brother my sibling.
| ?- sibling(me, brother).

yes

%Who is granddad grandparent of?
| ?- grandparent(Who, granddad).

Who = me ? a

Who = brother

no

%Who is Grandparent of a Grandchild?
%(Find all Grandparent, Grandchild pairs.)
| ?- grandparent(Grandchild, Grandparent).

Grandchild = me
Grandparent = granddad ? a

Grandchild = brother
Grandparent = granddad

no
</pre>
<p>At first all of this building of knowledgebase and then querying of it seems a bit unnatural to most programmers (including me when I first saw it). To begin there is no explanation about how Prolog came to the solutions it offers. It doesn&#8217;t print any algorithms and it can be quite frustrating when you are expecting a certain result and Prolog just <a href="http://www.youtube.com/watch?v=7TYAQ0JWBzE">says: no</a>. But if you think of it it&#8217;s not that much different from creating a database and and then using SQL statements to query it. When using SQL you also don&#8217;t specify search algorithms and and the DBMS merely presents you with result.</p>
<p>First day with Prolog ends with two homework assignments:</p>
<p>HW1: Make a simple knowledge base. Represent some of your favorite books and authors. Find all books in your knowledge base written by one author.</p>
<pre class="brush: plain; title: ; notranslate">
%books.pl
book('The Metamorphosis', 'Franz Kafka').
book('The Trial', 'Franz Kafka').
book('1984', 'George Orwell').
book('Catch 22', 'Joseph Heller').
book('Breakfast of Champions', 'Kurt Vonnegut').
book('Slaughterhouse-Five', 'Kurt Vonnegut').
</pre>
<pre class="brush: plain; title: ; notranslate">
| ?- book(Which, 'Franz Kafka').

Which = 'The Metamorphosis' ? a

Which = 'The Trial'

no
</pre>
<p>This was the simplest way of tackling this task that I could come up with.</p>
<p>HW2: Make a knowledge base representing musicians and instruments. Also represent musicians and their genre of music. Find all musicians who play the guitar.</p>
<pre class="brush: plain; title: ; notranslate">
%musicians.pl
musician('Jimi Hendrix').
musician('Bob Marley').
musician('Ringo Starr').
musician('Terminator X').
musician('Peter Tosh').
musician('Fela Kuti').
musician('Johnny Stulic').

instrument(guitar).
instrument(drums).
instrument(keyboard).
instrument(turntables).
instrument(saxophone).

genre(rock).
genre(reggae).
genre(hip-hop).
genre(afrobeat).

plays('Jimi Hendrix', guitar).
plays('Bob Marley', guitar).
plays('Ringo Starr', drums).
plays('Ringo Starr', keyboard).
plays('Terminator X', turntables).
plays('Peter Tosh', guitar).
plays('Peter Tosh', keyboard).
plays('Fela Kuti', saxophone).
plays('Fela Kuti', keyboard).
plays('Johnny Stulic', guitar).

plays('Jimi Hendrix', rock).
plays('Bob Marley', reggae).
plays('Ringo Starr', rock).
plays('Terminator X', hip-hop).
plays('Peter Tosh', reggae).
plays('Fela Kuti', afrobeat).
plays('Johnny Stulic', rock).

plays_instrument(Musician, Instrument) :-
    musician(Musician),
    instrument(Instrument),
    plays(Musician, Instrument).

plays_genre(Musician, Genre) :-
    musician(Musician),
    genre(Genre),
    plays(Musician, Genre).

genre_instrumentalist(Musician, Instrument, Genre) :-
    plays_instrument(Musician, Instrument),
    plays_genre(Musician, Genre).

rock_guitarist(Musician) :-
    genre_instrumentalist(Musician, guitar, rock).

genre_instrument(Genre, Instrument) :-
    genre_instrumentalist(_, Instrument, Genre).
</pre>
<pre class="brush: plain; title: ; notranslate">
%Who plays the guitar?
| ?- plays_instrument(Who, guitar).

Who = 'Jimi Hendrix' ? ;

Who = 'Bob Marley' ? ;

Who = 'Peter Tosh' ? ;

Who = 'Johnny Stulic' ? ;

no

%Does Jimi Hendrix play the guitar?
| ?- plays_instrument('Jimi Hendrix', guitar).

true ?

yes

%Which instruments does Ringo Starr play?
| ?- plays_instrument('Ringo Starr', Which).

Which = drums ? ;

Which = keyboard ? ;

no

%Who is a rock guitarist?
| ?- rock_guitarist(Who).

Who = 'Jimi Hendrix' ? ;

Who = 'Johnny Stulic' ? ;

no

%Is Bob Marley a rock guitarist?
| ?- rock_guitarist('Bob Marley').

no

%Which instruments are used to play rock?
| ?- genre_instrument(rock, Which).

Which = guitar ? ;

Which = drums ? ;

Which = keyboard ? ;

Which = guitar ? ;

no
</pre>
<p>In the second task I have expanded a bit on requirements as it was otherwise almost completely the same as the first one. I have also used the same rule (plays) for instruments and genres to make the task more interesting and challenging.</p>
<p>After the first day with Prolog I must say I like it quite a lot. The language is very simple and logical (PROgramming in LOGic) and has low barrier to entry even for non programmers. I have explained basic structure of last program to my girlfriend who understood it quite quickly and was happy making queries on her own. When she once asked me to show her what I was working on at the moment (in Java), she gave up on it quite quickly as there was so much &#8220;noise&#8221; to explain before I could explain what am I actually trying to do. I think the barriers that exist around Prolog are result of it tackling problems from a different (even though as shown with the SQL analogy not completely unusual) angle and the lack of willingness amongst programmers to accept different approach to problems. But it&#8217;s only the end of the first day so my opinions are still only forming.</p>
]]></content:encoded>
			<wfw:commentRss>http://dalibornovak.co.uk/blog/seven-languages-in-seven-weeks-prolog-day-1/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Seven Languages in Seven Weeks</title>
		<link>http://dalibornovak.co.uk/blog/seven-languages-in-seven-weeks/</link>
		<comments>http://dalibornovak.co.uk/blog/seven-languages-in-seven-weeks/#comments</comments>
		<pubDate>Mon, 29 Nov 2010 22:34:39 +0000</pubDate>
		<dc:creator>Dalibor "Bore" Novak</dc:creator>
				<category><![CDATA[books]]></category>
		<category><![CDATA[Books]]></category>
		<category><![CDATA[Clojure]]></category>
		<category><![CDATA[Erlang]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Io]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Prolog]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://boreplusplus.co.uk/blog/?p=17</guid>
		<description><![CDATA[Lately I have a feeling that Java ecosystem is getting a bit stale. To add to that grass seemed greener on the other side of the fence. Several of my friends that I started my coding career with in J2EE trenches have deserted and moved on to Ruby. None of them ever mentioned regretting the...<a href="http://dalibornovak.co.uk/blog/seven-languages-in-seven-weeks/">&#187;</a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.pragprog.com/titles/btlang/seven-languages-in-seven-weeks"><img class="alignleft size-full wp-image-18" title="Seven Languages in Seven Weeks" src="http://dalibornovak.co.uk/blog/wp-content/uploads/2010/11/sevenLanguages.gif" alt="Book Cover: Seven Languages in Seven Weeks: A Pragmatic Guide to Learning Programming Languages by Bruce A. Tate" /></a>Lately I have a feeling that Java ecosystem is getting a bit stale. To add to that grass seemed greener on the other side of the fence. Several of my friends that I started my coding career with in J2EE trenches have deserted and moved on to Ruby. None of them ever mentioned regretting the move.</p>
<p>Then new languages that run on JVM started gaining traction and I had a little play with Groovy couple of years ago. I liked the language very much and I grokked why my friends were so happy with Ruby path they&#8217;ve chosen. Sheer expressiveness and productivity provided were impressive. I tried to push for Groovy to be used at work but it never got beyond utilities and scripts. I once read that someone said that the problem Groovy has, is that it is not called Business Logic Expression Language. It seems that sentiment rings quite true.</p>
<p>Anyway as I mentioned earlier Java no longer feels quite like a nice new comfy shirt. I am feeling that it&#8217;s constraining me and due to my brushes with alternative, there is always that little question in the back of my head: &#8220;Am I using the proverbial golden hammer?&#8221;</p>
<p>Recently I started looking over the fence again and I am seeing that there seems to be more choice than ever. One of the languages that sparked my interest was Scala. I checked out some of the websites devoted to it and found that community seems quite vibrant and that the language itself is very interesting indeed. Also the fact that it allows and encourages functional programming sparked my interest even further. I am quite familiar with object orientation and have merely brushed with functional programming at uni. The prospect of getting my head around something new and the promises that functional programming paradigm brings (especially regarding concurrency) got me quite excited.</p>
<p>As I almost decided to devote more time to Scala I have attended <a href="http://skillsmatter.com/event/agile-scrum/agile-testing-bdd-exchange-2010">Agile Testing &amp; BDD eXchange</a>. In one of the presentations Erik Stenman talked about Erlang amongst other things and caught my attention.<span id="more-17"></span></p>
<p>Now I knew I have a problem. Too many languages and too little time. I realised that before I commit to a single language I want to have a bit of a nose around the programming languages landscape. So I thought, wouldn&#8217;t it be great if there was a book that would give you a crash course in several languages. Nothing too deep &#8211; only enough to present you the language&#8217;s philosophy and give you a feel of the language. Preferably these languages should cover different paradigms so that I could evaluate them as well and try to understand their strengths and weaknesses as well.</p>
<p>Well guess what a bit less than a month ago The Pragmatic Bookshelf have published <a href="http://pragprog.com/titles/btlang/seven-languages-in-seven-weeks">Seven Languages in Seven Weeks: A Pragmatic Guide to Learning Programming Languages by Bruce A. Tate</a>. From a bit of scan reading it seems that book is almost a perfect fit to my requirements. It promises to take you on a journey through seven languages in seven days (I&#8217;ll see if I can keep up) and languages are chosen to give you a flavour of object orientated programming, function programming, prototype based OO, logic programming and hybrids.</p>
<p>I will try to keep up with the program and tackle a new language each week. For those interested in my progress and findings I intend to blog about them as I go along. Wish me luck on my journey of discovery.</p>
]]></content:encoded>
			<wfw:commentRss>http://dalibornovak.co.uk/blog/seven-languages-in-seven-weeks/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
