Seven Languages in Seven Weeks: Prolog Day 1

Last time I presented you some of the answers I came up with for the Ruby problems in Seven Seven Languages in Seven Weeks. Today we are going to be looking at Prolog. Prolog is a very different beast, the programming paradigm is logic based.

In Prolog, rather than defining some instructions on how to solve a problem (like in imperative programming languages), you declare a set of facts and rules. You declare what is true, rather than what to do.

You can then use the Prolog inference engine to run queries over those rules and facts. While the sample answers for day one may seem more like running queries on a database than programming, Prolog is indeed a full fledged programming language.

Here are my solutions to day one’s problems:

Make a simple knowledge base containing books and their authors.

book_author(agilePatterns, uncleBob).

book_author(cleanCode, uncleBob).

book_author(sevenLanguages, bruceTate).

book_author(lordOfTheRings, tolkien).

Find all books written by a specific author (for fun I also checked to see if uncleBob was the author of agilePatterns and lordOfTheRings, also who was the author of sevenLanguages).

| ?- book_author(agilePatterns, uncleBob).

yes
| ?- book_author(lordOfTheRings, uncleBob).

no
| ?- book_author(X, uncleBob).             

X = agilePatterns ? ;

X = cleanCode ? ;

no
| ?- book_author(sevenLanguages, X).

X = bruceTate

yes

Make a knowledge base containing musicians, their instruments and which genre of music they play.

musician(clapton, guitar).

musician(hendrix, guitar).

musician(collins, drums).

musician(mangini, drums).

musician(wilson, vocals).

musician(watts, drums).



genre(clapton, blues).

genre(hendrix, rock).

genre(collins, rock).

genre(mangini, metal).

genre(wilson, prog).

genre(watts, rock).



genre_instrument(X, Y) :- genre(Z, X), musician(Z, Y). 

Find all guitarists.

| ?- musician(X, guitar).

X = clapton ? ;

X = hendrix ? ;

no

I also made a genre_instrument rule to find all instruments used for a particular genre. You can use it likewise:

| ?- genre_instrument(rock, X).

X = guitar ? ;

X = drums ? ;

X = drums

yes

Because of unification all rules can be used to query for different information by changing the variables. For example, I could also use the same rule to query all genres that use drums:

| ?- genre_instrument(X, drums).

X = rock ? ;

X = metal ? ;

X = rock

yes

Or to get all possible combinations:

| ?- genre_instrument(X, Y).

X = blues
Y = guitar ? ;

X = rock
Y = guitar ? ;

X = rock
Y = drums ? ;

X = metal
Y = drums ? ;

X = prog
Y = vocals ? ;

X = rock
Y = drums

yes

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s