17. 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:
- Fact: spouse (john, jane)
- Fact: spouse (david, mary)
- Fact: spouse(george, susan)
- Fact: female (jane)
- Fact: female (mary)
- Fact: female (susan)
- Fact: male (john)
- Fact: male (david)
- Fact: male (george)
- Rule: husband(A,B) IF spouse(A,B) AND male (A)
- 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:
- Prolog will first of all scan the Rules looking for a match.
- Rule 11 fits, where A and B can be any two names.
wife (A,B)
- 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)
- Next it looks to see if the second condition female(B) has a match and yes Fact 5 is a match.
Fact: female (mary)
- 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