Prolog Programming

incoggeek
3 min readDec 31, 2022

--

Photo by Possessed Photography on Unsplash

The Basics

Prolog is one of the most widely used programming languages in artificial intelligence research. Prolog means PROgramming in LOGic.

Features

  • Prolog is the major example of a fourth generation programming language supporting the declarative programming paradigm.
  • Prolog is a logical and a declarative programming language.
  • In declarative language the programmer specifies a goal to be achieved the Prolog system works out how to achieve it.
  • Relational databases owe something to Prolog.

Prolog is available in different versions. So, Prolog program written in one version may or may not work in other version.

  • Traditional programming languages are said to be procedural programmer must specify in detail how to solve a problem :
  • For instance, General guideline to make tea.
  1. Add 1 cup/200 ml of freshly boiled water to your tea bag (in a mug)
  2. Allow the tea bag to brew for 2 minutes
  3. Remove the tea bag
  4. Add 10 ml of milk
  5. Wait 6 minutes before consumption for the cup to reach its optimum temperature of 140 F/60 C
  • In purely declarative languages, the programmer only states what the problem is and leaves the rest to the language system.
  • Declarative Programming is like asking your friend to fix your car. You don’t care how to fix it, that’s up to her.
  • Imperative Programming is like your friend calling your father that tells her how to fix your car step by step.

Application of Prolog

  • Prolog’s fast incremental development cycle and rapid prototyping capabilities have encouraged the use of the language as a tool for solving A.I problems.
  • Prolog is a powerful tool for building robust commercial applications.
  • Prolog features include interfaces to other languages and database products, stand-alone application generators, and more recently, support for techniques such object-orientated and constraint based programming.
  • Constraints are a powerful way to reduce the size of the search space and thus increase the efficiency of the scheduler.
  • Intelligent data base retrieval
  • Expert systems
  • Natural language understanding
  • Machine learning

Relations in Prolog

Relationship is one of the main features that we have to properly mention in Prolog. These relationships can be expressed as facts and rules.

  • We will create the knowledge base by creating facts and rules, and play query on them.
  • In Prolog programs, it specifies relationship between objects and properties of the objects.

Suppose, there’s a statement, “Amit has a bike”, then we are actually declaring the ownership relationship between two objects — one is Amit and the other is bike.

If we ask a question, “Does Amit own a bike?”, we are actually trying to find out about one relationship.

There are various kinds of relationships, of which some can be rules as well. A rule can find out about a relationship even if the relationship is not defined explicitly as a fact.

We can define a brother relationship as follows −

Two person are brothers, if,

  • They both are male.
  • They have the same parent.

Now consider we have the below phrases −

  • parent(sudip, piyus).
  • parent(sudip, raj).
  • male(piyus).
  • male(raj).
  • brother(X,Y) :- parent(Z,X), parent(Z,Y),male(X), male(Y)

These clauses can give us the answer that piyus and raj are brothers, but we will get three pairs of output here. They are: (piyus, piyus), (piyus, raj), (raj, raj). For these pairs, given conditions are true, but for the pairs (piyus, piyus), (raj, raj), they are not actually brothers, they are the same persons. So we have to create the clauses properly to form a relationship.

The revised relationship can be as follows −

A and B are brothers if −

  • A and B, both are male
  • They have same father
  • They have same mother
  • A and B are not same

--

--