Fork me on GitHub

Project Notes

#384 About Prolog

A quick introduction to Prolog and a test drive with GNU Prolog on macOS.

Notes

Prolog is a logic programming language that has was created around 1972 by Alain Colmerauer with Philippe Roussel, from the Artificial Intelligence Group of the Faculty of Sciences of Luminy of Aix-Marseille II University of France.

It perhaps garnered some fresh attentino when it earned a chapter in Bruce Tate’s Seven Languages in Seven Weeks.

Prolog In a Nutshell

Prolog is…

  • one of the first logic programming languages
  • a declarative programming language

Prolog has..

  • a single data type: the term.
    • Terms are either atoms, numbers, variables or compound terms (lists, strings)
  • clauses: Prolog programs describe relations, defined by means of clauses
    • Pure Prolog is restricted to Horn clauses
    • Two types of Horn clauses are used to define Prolog programs: rules and facts
  • A rule is of the form: Head :- Body.
    • Negation can be used in the rule
  • A fact is a clauses with an empty body.
  • Predicates and programs
    • A predicate (or procedure definition) is a collection of clauses whose heads have the same name and arity.
    • A logic program is a set of predicates
  • Loops and recursion
  • Execution
    • Execution of a Prolog program is initiated by the user’s posting of a single goal, called the query.

Prolog applications..

  • mainly:
    • theorem proving
    • expert systems
    • term rewriting
    • type systems
    • automated planning
    • question answering
    • natural language processing (its original intended use)
  • most applications are small by industrial standards
  • widely used in research and education, though more common in Europe (Americans favor Lisp).
  • however, Prolog and other logic programming languages have not had a significant impact on the computer industry in general.

Prolog is governed by..

  • The International Organization for Standardization (ISO) Prolog technical standard

prolog-systems

Prolog implementations include..

Test drive: GNU Prolog on macOS

As I’m on macOS right now, I’ll use the Homebrew Formulae for gnu-prolog to install:

brew install gnu-prolog

Let’s test it with an example from Seven Languages in Seven Weeks. See friend.pl:

likes(wallace, cheese).
likes(grommit, cheese).
likes(wendolene, sheep).

friend(X, Y) :- \+(X = Y), likes(X, Z), likes(Y, Z).

We have three facts about who likes cheese. The friend rule with three clauses:

  • \+(X = Y) means you can’t friend yourself (\+ indicates negation)
  • likes(X, Z), likes(Y, Z) means X and Y both have to share a like for the same thing

Testing it out, querying for likes and friend conditions:

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

compiling ./friend.pl for byte code...
./friend.pl compiled, 5 lines read - 1029 bytes written, 6 ms
| ?- likes(wallace, cheese).

yes
| ?- likes(grommit, cheese).

yes
| ?- likes(wendolene, cheese).

no
| ?- friend(wallace, grommit).

yes
| ?- friend(grommit, wallace).

yes
| ?- friend(wallace, wendolene).

no
| ?- likes(wallace, wallace).

no

Credits and References

About LCK#384 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