Fork me on GitHub

Project Notes

#387 Musicians with Prolog

Using Prolog to represent and query a knowledge base representing musicians and instruments.

Notes

This is an exercise from Seven Languages in Seven Weeks.

The task:

  • Make a knowledge base representing musicians and instruments. Also represent musicians and their genre of music.
  • Find all musicians who play the guitar.

Thinking about the Problem

This is a fairly straight-forward problem:

  • we just need to record facts directly about each musician: the instrument(s) and genre(s)
  • we are only making basic and direct queries about the facts

Implementation

See musicians.pl. It defines the instrument(s) that a musicians plays, and the genre. For example, a small extract:

genre(ella_fitzgerald, jazz).
genre(elton_john, pop_rock).
plays(ella_fitzgerald, vocals).
plays(elton_john, piano).
plays(elton_john, vocals).

Some Queries

Loading the program:

$ gprolog --consult-file musicians.pl
GNU Prolog 1.5.0 (64 bits)
Compiled Jul  8 2021, 09:35:47 with clang
Copyright (C) 1999-2024 Daniel Diaz

compiling ./musicians.pl for byte code...
./musicians.pl compiled, 34 lines read - 3980 bytes written, 4 ms

Query to find all musicians who play the guitar:

| ?- plays(Musician, guitar).

Musician = bb_king ? ;

Musician = eric_clapton ? ;

Musician = jimi_hendrix ? ;

Musician = ritchie_blackmore

yes

Query to find all jazz musicians:

| ?- genre(Musician, jazz).

Musician = billie_holiday ? ;

Musician = charlie_parker ? ;

Musician = dave_brubeck ? ;

Musician = ella_fitzgerald ? ;

Musician = herbie_hancock ? ;

Musician = john_coltrane ? ;

Musician = louis_armstrong ? ;

Musician = miles_davis ? ;

(1 ms) no

Credits and References

About LCK#387 Prolog

This page is a web-friendly rendering of my project notes shared in the LittleCodingKata GitHub repository.

Project Source on GitHub Return to the LittleCodingKata Catalog
About LittleCodingKata

LittleCodingKata is my collection of programming exercises, research and code toys broadly spanning things that relate to programming and software development (languages, frameworks and tools).

These range from the trivial to the complex and serious. Many are inspired by existing work and I'll note credits and references where applicable. The focus is quite scattered, as I variously work on things new and important in the moment, or go back to revisit things from the past.

This is primarily a personal collection for my own edification and learning, but anyone who stumbles by is welcome to borrow, steal or reference the work here. And if you spot errors or issues I'd really appreciate some feedback - create an issue, send me an email or even send a pull-request.

Follow the Blog follow projects and notes as they are published in your favourite feed reader