Declarative

Specification

    • Declarative programming

      • understanding of and ability to solve a problem by writing appropriate facts and rules based on supplied information

      • understanding of and ability to write code that can satisfy a goal using facts and rules

Declarative Programming

The summer term Prolog work can be found here. This site lists generic Prolog and declarative material. The Prolog tutorial can also be found under programming tutorials on this site.

Declarative programming, also less commonly referred to as functional programming, is where instructions are given by way of functionality, facts and basically specifying 'what' without necessarily giving the 'how'. There are many types of declarative languages such as Haskell, Prolog and HTML, while other languages incorporate declarative elements such as Lisp. SQL is also a declarative language, but makes use of a procedural style of programming.

Prolog, short for PROgramming LOGic has been chosen to fulfil the requirements of the specification, although CIE have not directly indicated any specific language. Tutorials can be found in the tutorials section here.

Taking an example from the examination. Let's imagine we have a set of cities a travel writer has visited but want to list the countries visited. The database has been specified as follows:

01 capital_city(amman).

02 capital_city(beijing).

03 capital_city(london).

04 city_in_country(amman, jordan).

05 city_in_country(london, uk).

06 city_in_country(manchester, uk).

07 country_in_continent(jordan, middle-east).

07 country_in_continent(uk, europe).

09 city_visited(amman).

10 city_visited(london).

We could specify a rule as follows:

countries_visited(ThisCountry) :- city_visited(ThisCity) AND city_in_country(ThisCity, ThisCountry)

Note, the exam seems to prefer IF over :- but as usual, look out for both.

Key Terms and Jargon Buster

It is very important that you understand that variables start with upper case letters and atoms with lower case. Also, predicates must also start with a lower case letter. The PowerPoint (at the bottom of the page) gives a very good introduction to the key terms used in Prolog.

a goal is solved by running a query. Therefore, the goal is the result of the query.

A clause is comprised of facts and rules. It always terminates with a full stop. E.g.

An atom is a general-purpose name with no inherent meaning and must start with a lower case letter (the examiner will mark you down if you do not). It is composed of a sequence of characters that is parsed by the Prolog reader as a single unit. Atoms are usually bare words in Prolog code, written with no special syntax. However, atoms containing spaces or certain other special characters must be surrounded by single quotes. Atoms beginning with a capital letter must also be quoted, to distinguish them from variables. The empty list, written [], is also an atom.

A variable is written with an upper case letter or underscore, These represent an unknown. E.g. mother(X) would return all values of X where we have defined a mother. E.g. X=Theresa, X=Samantha.

A Prolog program consists of predicate definitions (starting with a lower case letter). A predicate denotes a property or relationship between objects. Definitions consist of clauses. A clause has a head and a body (Rule) or just a head (Fact). A head consists of a predicate name and arguments

:- denotes IF and states the conditions that form a given rule. E.g. parent(X,Y) :- mother(X,Y). This states that “Person X is the parent of person Y if X is Y’s mother.”. Again, X and Y are variables used as placeholders for when the rule is called.

Both facts and rules are predicate definitions. A Predicate is the name given to the word occurring before the bracket in a fact or rule: parent(jane,alan) - here, parent is the predicate. By defining a predicate you are specifying which information needs to be known for the property denoted by the predicate to be true.

Arity refers to the number of arguments something has.

Links

Declarative vs Imperative

The difference can be summed up as:

Declarative programming tends towards:-

  • Sets of declarations, or declarative statements, each of which has meaning (often in the problem domain) and may be understood independently and in isolation.

Imperative programming tends towards:-

  • Sequences of commands, each of which perform some action; but which may or may not have meaning in the problem domain.

Effectively, declarative programming is saying what you want (the output), imperative is saying how you want something to work (the process).

For example, you can specify a webpage to have a certain font in a given colour, without having to write the programming statements to produce the outcomes. In case you are thinking that similar functionality can be obtained in languages such as VB.NET through the use of functions, you are correct. There is a little blur between the two paradigms and neither is superior, they both serve different purposes depending on the required outcome.

The Computerphile video below goes into some depth about HTML and also on the difference between imperative and declarative programming languages. The debate on whether something can be called a programming language often comes down to whether it is Turing complete.