Declarative languages

Declarative languages are often used by intelligent systems. Declarative languages are very different to the other types of language because of the way that the language is structured. Programmers will create a knowledge base. A knowledge base contains a series of related facts and rules.


An expert system exists to provide information on train routes throughout Scotland. The programmer used a declarative language to create the system.

There are three forms of train service: the direct train, which does not stop at all, the limited stop service which will stop at a few stations on the route, and the local service which will stop at every station on the route.

There are a series of facts held within the knowledge base. A sample is shown below:

limited_stop_service (glasgow, edinburgh)There is a limited stop service from Glasgow to Edinburgh
direct_service (dundee, perth)There is a direct service from Dundee to Perth
local_service (glasgow, east kilbride)There is a local service from Glasgow to East Kilbride
local_service (edinburgh, bathgate)There is a local service from Edinburgh to Bathgate

This example and related theory presumes that Prolog has been used to create this expert system.

An example of a rule within the knowledge base could be:

travel_direct (X, Y):- direct_service (X, Y)

The :- symbol represents the term ‘IF’, so this rule could also be written as:

travel_direct (X, Y) IF direct_service (X, Y)

This means you can travel directly from location X to location Y if there is a fact that states there is a direct service between location X and location Y. This is a simple rule for the purpose of exemplification.

X and Y represent variables; when using Prolog, variables should always be written as capital letters but declarative languages do not require the programmer to declare a data type. It is necessary to be able to read, understand and create simple facts and rules in the format shown.

Queries can be written that will attempt to return a result after interrogating the knowledge base. For example:

? local_service (X, bathgate)

This query is trying to determine the value for X where X is a local service that runs to Bathgate. The result would be "X = edinburgh."

Queries depend upon in-built algorithms that will carry out pattern matching to return a result. Heuristic, breadth-first and depth-first searches are often used to perform pattern matching when querying a knowledge base.

Declarative languages like Prolog allow for the use of self-modifying code. This means that during execution the program can modify the facts and rules that it holds if necessary.

Another key feature of declarative languages is the use of recursion. Rules are normally created to call other facts or rules. Recursion takes place when a rule calls itself. A commonly used example of recursion is that of a rule about ancestry.

ancestor_of(X,Y):- parent_of(X,Z), ancestor_of(Z,Y)

This reads as 'X is an ancestor of Y if X is a parent of Z and Z is an ancestor of Y'. Recursion is evident here as the rule ancestor_of(X,Y) calls itself within the rule definition – ancestor_of(Z,Y).