Contents:
QUESTION: "What is a computer program?"
ANSWER: "It is an executable representation of some algorithm designed to solve some real world problem."
There are thus two elements to a computer program:
Consider the following definition (logic) for factorial n:
if n = 0 then n! = 1 if n > then n! = 1x2x3 ... xn
with CS_IO ; use CS_IO ; procedure FACTORIAL is N: integer; T: integer:= 1; begin put("Input ");get(N); for K in 2..N loop T:= T*K; end loop; put("Factorial ");put(N); put("is ");put(T);newline; end FACTORIAL;
The above comprises a series of steps which, if executed, will calculate the factorial of a given number. When writing in an imperative language the programmer needs to concentrate on both the logic (what) and the control (how). The principal idea behind declarative languages is to separate out these two elements so that the programmer is free to concentrate on the logic of a problem without being distracted by control considerations.
factorial(0,1):- !. factorial(N1,T2):- N2 is N1-1, factorial(N2,T1), T2 is N1*T1.
Here we define the logic (the desired goal or result) and not the control (how we achieve the desired goal). We say that declarative languages are goal directed or goal driven. Note the closeness of the code to the initial definition, both specify what is to be achieved (i.e. the logic).
To study computer programming languages it is convenient to devide such languages into categories according to some classification. Many classifications have been suggested. The most popular are listed below:
Classification according to levels of abstraction:
Classification according to generations of programming languages:
Classification according to application area. Most current programming languages are categorised as 3rd generation. This category is therefore sometimes divided into two sub categories:
Declarative languages are considered to be specialised in that they were initially intended for use in Artificial Intelligence (AI) and expert systems programming. Example decalarative languages include: PROLOG, LISP, Miranda, Haskell and SML.
From the above we have established that the principal distinction between declarative languages and imperative languages is that declartaive languages allow the programmer to concentrate on the logic of an algorithm (declarative languages are goal driven, control is not the concern of the programmer), while imperative languages require the programmer to focus on both the logic and control of an algorithm. To distinguish more succinctly between declarative and imperative programming languages we will consider the models of computation on which they are based in terms of the following features:
IMPERATIVE LANGUAGES: Imperative languages describe computable relationships in terms of sequences of operations, e.g. Ada factorial program.
DECLARATIVE LANGUAGES: Declarative programs, in turn, are made up of sets of definitions or equations describing relations which specify what is to be computed (not how it is to be computed), e.g. Prolog factorial program.
IMPERATIVE LANGUAGES: Imperative languages are based on the concept of variables names which can be associated with changeable values through expressions. Typically each expression will consist of an assignment which will in some way change a value associated with a variable name. The process whereby different values are continually associated with a particular variable name is referred to as destructive assignment - each fresh assignment obliterates the existing value.
DECLARATIVE LANGUAGES: In declarative programs variables can only ever have one value "assigned" to them and this value can not be altered during a program's execution. We refer to this as non-destructive assignment. In declarative languages to achieve results such as that illustrated by the factorial Ada program, because the same name cannot be reused with different values, we must either:
IMPERATIVE LANGUAGES: Imperative language make use of data structures such as arrays, records etc. Array elements and record fields are changed by successive destructive assignments.
DECLARATIVE LANGUAGES In declarative languages, where there is no destructive assignment, explicit representations for data structures must be used. The most fundamental data structure used by declarative programming languages is the list.
IMPERATIVE LANGUAGES Because relationships in imperative programs languages are expressed using sequences of expressions the order of execution is crucial. Values are passed from one expression to another by reference to common variables. Further one expression may change a variable's value before that variable is used in the next expression, i.e. imperative expressions have side effects. Thus if the order in which expressions are executed is changed then the behaviour of the entire program will be affected. In imperative programs, we say that commands can only be understood in the context of the previous computation.
DECLARATIVE LANGUAGES In declarative programs the values associated with variable names cannot be changed. Thus the order in which definitions or equations are called does not matter, i.e. they are order independent. Further, declarative definitions do not permit side effects, i.e. the computation of one value will not effect some other value. Of course declarative programs must be executed in some order, but the order should not affect the final result. Thus we can say that, in declarative programs, program statements are true independent of the computational context.
IMPERATIVE LANGUAGES In most imperative languages sub-programs cannot be passed as actual parameters to other sub-programs, or be passed back as results (there are exceptions).
DECLARATIVE LANGUAGES In declarative languages, relationships may construct new relationships and pass then on to other relationships as arguments. Thus declarative languages allow definitions or expressions to be treated in the same manner as more standard parameter data items.
IMPERATIVE LANGUAGES The control of programs in imperative languages is the responsibility of the programmer, i.e. the programmer is concerned with assignments and the order of assignments.
DECLARATIVE LANGUAGES In declarative languages control is transferred to the language and hence is no longer the programmers responsibility.
Characteristics of imperative languages:
Characteristics of declarative languages:
Declarative languages make extensive use of relationships. There are two principal styles of defining relationships available to such languages:
These form the two sub-branches of the declarative family of programming languages.
Functional declarative languages tend to be inspired by the Lambda Calculus ( Calculus), a computational model proposed by Alonzo Church in the 1930's. As the name suggests, using the functional style, relationships are expressed using functions. Example:
(square (n) (* n n))
This is a function, square, that express the relationship between the input n and the output value n*n.
Logic declarative languages, in turn, tend to be inspired by Predicate Calculus and, more particularly, the Horn Clause subset of Predicate calculus. In this style of declarative programming relationships are declared using expressions known as clauses. Thus:
square(N, M):- M is N*N.
Clauses can be used to express both facts and rules. A logic programme can therefore be treated as a relational database to which we can pose queries of various forms.
Created and maintained by Frans Coenen. Last updated 11 October 1999