1. Declarative language

A declarative language is non-procedural and very high-level (4th generation). This means the programmer specifies what needs to be done rather than how to do it (Like Captain Pickard's 'Make it So' command to his crew in Star Trek).

 

The software will seek an answer to the question (goal) by interrogating a database containing Facts and Rules. It does not matter what order the facts and rules are arranged within the database - unlike procedural languages - the computer will find the best path towards the answer. There will either be a matching answer - for example the question / goal might be 'who is David's wife?" or a 'False' is returned where there was no answer to be found..

This type of language is geared more towards applications such as artificial intelligence and expert systems where inexact data has to be handled or general decisions have to be made.

PROLOG is a declarative language that was developed for artificial intelligence. There are others such as 'D' also under development.

Example

Consider a Prolog database containing the following facts and rules:

  1. Fact: spouse (john, jane)
  2. Fact: spouse (david, mary)
  3. Fact: spouse(george, susan)
  4. Fact: female (jane)
  5. Fact: female (mary)
  6. Fact: female (susan)
  7. Fact: male (john)
  8. Fact: male (david)
  9. Fact: male (george)
  10. Rule: husband(A,B) IF spouse(A,B) AND male (A)
  11. Rule: wife (A,B) IF spouse(A,B) AND female(B)

A query could then be written:

		wife(david,mary)? 

The formal name for this statement is 'goal' . And the work of finding the answer is called 'satisfying the goal'.

Effectively this is asking if Mary is the wife of David. In order to provide an answer the following takes place:

  1. Prolog will first of all scan the Rules looking for a match.
  2. Rule 11 fits, where A and B can be any two names.
    wife (A,B)
  3. Then it applies the rule by looking to see if it can find a match to the spouse(A,B) condition and yes - Fact 2 fits.
    spouse(david,mary)
  4. Next it looks to see if the second condition female(B) has a match and yes Fact 5 is a match.
    Fact: female (mary)
  5. Both conditions have been met, so yes Mary is David's wife.

An important features of declarative languages is 'backtracking' where a search goes partially back on itself if it fails to find a complete match the first time around. This is discussed in more detail on the next page.

 

Challenge see if you can find out one extra fact on this topic that we haven't already told you

Click on this link: Declarative Languages

 

 

 

 

 

Copyright © www.teach-ict.com