Rules:
In order to work, people must be full and delighted.
work(X) :- delighted(X), full(X).
In order to be full, people must eat something.
full(X) :- ate(X).
In order to be delighted, people must sleep and have fun.
delighted(X) :- slept(X), fun(X).
Facts:
Mustafa ate.
ate(mustafa).
Mustafa slept.
slept(mustafa).
Mustafa had fun.
fun(mustafa).
Query:
Can Mustafa work?
Facts:
eats(fred,oranges).
eats(tony,apple).
eats(john,apple).
Queries:
?- eats(fred,oranges). /* yes
?- eats(john,apple). /*yes
?- eats(mike,apple). /*no
Showing posts with label query. Show all posts
Showing posts with label query. Show all posts
Sunday, 18 January 2009
Simple Facts
In 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.
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.
SWI-Prolog
SWI-Prolog is an open source implementation of the programming language Prolog, commonly used for teaching and semantic web applications, which is designed by Jan Wielemaker in 1987
SWI-Prolog runs on Unix, Windows, and Macintosh platforms.
SWI-Prolog can be downloaded from “http://www.swi-prolog.org”.
The program, sometimes called database is a text file (*.pl or *.pro) that contain the facts and rules that will be used by the user of the program.
First you have to create a facts and rules file which can be done by using notepad.
It contains all the relations that make this program.
When you launch a program you are in query modequery mode.
This mode is represented by the sign ? - at the begining of the line.
In query mode you ask questions about relations described in the program.
You can run the facts and rules file by double clicking, after running SWI-Prolog, by using “Consult” menu via “File” menu or typing the file name after ?- sign.
After running the file, all rules and facts are stored in prolog.
You can now use queries.
SWI-Prolog runs on Unix, Windows, and Macintosh platforms.
SWI-Prolog can be downloaded from “http://www.swi-prolog.org”.
The program, sometimes called database is a text file (*.pl or *.pro) that contain the facts and rules that will be used by the user of the program.
First you have to create a facts and rules file which can be done by using notepad.
It contains all the relations that make this program.
When you launch a program you are in query modequery mode.
This mode is represented by the sign ? - at the begining of the line.
In query mode you ask questions about relations described in the program.
You can run the facts and rules file by double clicking, after running SWI-Prolog, by using “Consult” menu via “File” menu or typing the file name after ?- sign.
After running the file, all rules and facts are stored in prolog.
You can now use queries.
Subscribe to:
Posts (Atom)