Declarative language

From Higher Computing Science
Jump to: navigation, search

This article is unfinished. Please consider joining and adding to this article. Read about Page layout beforehand.

PROLOG, an example of a declarative language

Key Points

  • Declarative languages are designed to run tests on data
  • Instead of a sequence of commands, declarative languages have a set of facts and rules (the knowledge base)
  • A declarative program works by sending a query to the program. This is a test that is checked against the facts and rules in the knowledge base.
  • Declarative programs do not tend to use variables and control structures like procedural language, but depend on rules to control the behaviour of the program
  • Declarative programs can edit their own knowledge base (such as adding new facts) which means they can “learn” from user interaction.


Declarative languages are creating by declaring a set of facts and rules that can be tested out by a user with queries. This means that a declarative program can be used to create systems based on logic and knowledge, rather than sequence (as would be the case in a procedural language).

Example of a Declarative Language

This program lists a number of facts. At the bottom is a rule.

child_of(john, jane). 
child_of(john, fred). 
child_of(anne, john).

grand_child_of(X,Y) :- child_of(X,Z), child_of(Z,Y).

The program itself does not do anything. It requires a query, which is a request that will evaluate the program and produce a result, similar to searching a database.

The user interface, known as a shell, is used to enter a query. The queries below would all produce different sets of results from the same program.

? child_of(john,fred)

? child_of(john,anne)

? child_of(X, jane)

X = john
? grand_child_of(anne,jane)

? grand_child_of(X,fred)



Further information

Learn Prolog Now Tutorials

Test yourself

Teaching resources