2 The Facts2.1 Simple factsIn Prolog we can make some statements by using facts. Facts either consist of a particular item or a relation between items. For example we can represent the fact that it is sunny by writing the program :sunny.We can now ask a query of Prolog by asking ?- sunny.?- is the Prolog prompt. To this query, Prolog will answer yes. sunny is true because (from above) Prolog matches it in its database of facts. Facts have some simple rules of syntax. Facts should always begin with a lowercase letter and end with a full stop. The facts themselves can consist of any letter or number combination, as well as the underscore _
character. However, names containing the characters -,+,*,/, or other mathematical operators should be
avoided.2.2 Facts with argumentsMore complicated facts consist of a relation and the items that this refers to. These items are called arguments. Facts can have arbitrary number of arguments from zero upwards. A general model is shown below:relation(<argument1>,<argument2>,....,<argumentN> ).The arguments can be any legal Prolog term. The basic Prolog terms are an integer, an atom, a variable or a structure. Various Prolog implementation enhance this basic list with other data types, such as floating point numbers, or strings. Exemple : likes(john,mary).In the above fact john and mary are two atomes. Atoms are usally made from letters and digits with lowercase characters. The underscore (_) can also be used to separe 2 words but is not allowed as the first charactere. Atoms can also be legally made from symbols. The followings are legal atoms :atoms hello zz42 two_words ====>The followings are not legal atoms : Hello 4hello _Hello two words two-wordsYou can use single quotes to make any character combination a legal atom. 'two words' 'UpperCase' '12444' 'etc...'The fact likes(john,mary). say that there is a relation between john and mary. It can be read as either john likes mary or mary likes john. This reversibility can be very useful to the programmer, however it can also be a source of mistakes. You have to be clear on how you intend to interpret the relation. The number of arguments is the arity of the predicate. A predicate with 2 arguments will be called by predicate_name/2. You can have differents predicats with the same name if they have a different arity. 2.3 How to queryOnce you have entered the facts in a program you can ask prolog about it. An exemple programm can be :eats(fred,oranges). /* 'Fred eats oranges' */ eats(tony,apple). /* 'Tony eats apple' */ eats(john,apple). /* 'John eats apple' */If we now ask some queries we would get the followings things : ?- eats(fred,oranges). /* does this match anything in the database? */ yes /* yes, that matchs the first clause */ ?- eats(john,apple). yes ?- eats(mike,apple). no /* there is no relation between mike and apple */ |
|