Facts Roles and Queries There are only three basic constructs in Prolog: facts, rules, and queries. Prolog programming is all about writing knowledge bases. A collection of facts and rules is called a knowledge base (or a database)
Knowledge Base 1 Knowledge Base 1 (KB1) is simply a collection of facts. Facts are used to state things that are unconditionally true of the domain of interest. Ex: woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). state that Mia, Jody, and Yolanda are women, and Jody plays air guitar.
Knowledge Base 1 KB1 can be used by posing queries. Ex: We can ask Prolog whether Mia is a woman by posing the query: ?- woman(mia). Yes we can ask whether Jody plays air guitar by posing the following query: ?- playsAirGuitar(jody). Yes
items are rules. Rules state information that is conditionally true of the domain of interest.
Knowledge Base 2 :- should be read as ``if'', or``is implied by''. The part on the left hand side of the :- is called the head of the rule. The part on the right hand side is called the body. Ex: We can ask Prolog whether Mia plays air guitar: ?- playsAirGuitar(mia) Yes
Knowledge Base 3 KB3 consists of five clauses: happy(vincent). listensToMusic(butch). playsAirGuitar(vincent):- listensToMusic(vincent), happy(vincent). playsAirGuitar(butch):- happy(butch). playsAirGuitar(butch):- listensToMusic(butch). There are two facts, namely happy(vincent) and listensToMusic(butch), and three rules.
Knowledge Base 4 KB4 contains no rules, only a collection of facts. woman(mia). woman(jody). woman(yolanda). loves(vincent,mia). loves(marcellus,mia). loves(pumpkin,honey_bunny). loves(honey_bunny,pumpkin).
Knowledge Base 4 Ex: if we query ?- woman(X). Prolog reports back to us as follows: X = mia The first item in the knowledge base is woman(mia). So, Prolog matches X to mia, thus making the query agree perfectly with this first item.
KB5 contains four facts about the loves relation and one rule.
Knowledge Base 5 In the previous example, It says that an individual X will be jealous of an individual Y if there is some individual Z that X loves, and Y loves that same individual Z too. Ex: ?- jealous(marcellus,W).
This query asks: can you find an individual W such that Marcellus is jealous of W?
Prolog will return the value W = vincent
Prolog syntax There are four kinds of terms in Prolog: atoms, numbers, variables, and complex terms (or structures). Atoms and numbers are lumped together under the heading constants, and constants and variables together make up the simple terms of Prolog. The upper-case letters are A, B, ..., Z; The lower-case letters are a, b, ..., z; The digits are 1, 2, ..., 9; The special characters are +, -, *, /, <, >,=, :, ., &, ~, and _. The _ character is called underscore. A string is an unbroken sequence of characters.
A string of characters made up of upper-case letters, lower-case letters, digits, and the underscore character, that begins with a lower-case letter.
An arbitrary sequence of character enclosed in single quotes.
Ex: 'Vincent','The Gimp', ' A string of special characters. Five_Dollar_Shake', '&^%&#@$ &*', and ' '.
For example: @= and ====> and ; and :- are all atoms.
NUMBERS Prolog implementations support
Floating point numbers or floats (that is, representations of real numbers such as 1657.3087 or )
Integers (that is: ... -2, -1, 0, 1, 2, 3, ...) are useful for such tasks as counting the elements of a list.
VARIABLES A variable is a string of : upper-case letters lower-case letters digits underscore characters that starts either with an upper-case letter or with underscore.
COMPLEX TERMS Complex terms are build out of a functor followed by a sequence of arguments. The arguments are put in ordinary brackets, separated by commas, and placed after the functor. The functor must be an atom. That is, variables cannot be used as functors. Arguments can be any kind of term. Ex: in the previous examples playsAirGuitar(jody) is a complex term: its functor is playsAirGuitar and its argument is jody.
Visit more self help tutorials Pick a tutorial of your choice and browse through it at your own pace. The tutorials section is free, self-guiding and will not involve any additional support. Visit us at www.dataminingtools.net