340 likes | 510 Vues
Declarative Programming. How to use PROLOG environment. Autumn 2012. Getting a PROLOG interpreter - SWI-Prolog. Freely available from http://www.swi-prolog.org/. Getting a PROLOG interpreter - SWI-Prolog. Freely available from http://www.swi-prolog.org/ under GNU license
E N D
Declarative Programming How to use PROLOG environment Autumn 2012
Getting a PROLOG interpreter - SWI-Prolog Freely available from http://www.swi-prolog.org/
Getting a PROLOG interpreter - SWI-Prolog • Freely available from http://www.swi-prolog.org/ under GNU license • 5.* versions already does not seem to have apparent problems • This will be the "official version" used for this course • Comes as an installation, although just copying the directories seems to be ok • Includes graphical components - if you wish, you may use them • Includes a number of nice predicates not required by Prolog standard, but the use of them in home assignments may be limited
Getting a PROLOG interpreter - SWI-Prolog • A SWI-Prolog-Editor for Windows is also available • Not necessary, but might be useful
Getting a PROLOG interpreter - Sicstus Prolog Available from http://www.swi-prolog.org/
Getting a PROLOG interpreter - Sicstus Prolog • Available from http://www.swi-prolog.org/ • Unfortunately not free any more (even educational licenses) • You can get a 30-day evaluation version • Might be better that SWI-Prolog (this was true some time ago, but SWI-Prolog has improved a lot) • Installation is required • With few exceptions programs are compatible with SWI-Prolog (if you don't use non-stadard predicates)
Getting a PROLOG interpreter - other Other interpreters are available. You may use them, but compatibility with SWI-Prolog is your problem. http://www.binnetcorp.com/BinProlog/ http://www.amzi.com/download/
The beginning When starting the interpreter you will see something like this: You can load an existing prolog program (assuming its name is 'your_program.pl') by typing: [your_program]. or ['your_program.pl']. or consult(your_program). or consult('your_program'). and pressing <Enter>
Loading a prolog program • The dot ('.') at the end of input command is mandatory • You must use apostrophes around file name, if it contains special symbols (e.g. '.') • '.pl' is considered a native extension for prolog files and may be omitted (some interpreters use other native extensions, e.g. '.pro') • you can load several programs simultaneously, e.g. [your_program_1,your_program_2]. • pay attention to warning and error messages • consult in SWI-Prolog seems to correspond to the standard predicate reconsult, which here is missing
Writing a prolog program • You can write a new prolog program by using command [user]. or consult(user). • This means you should not use names 'user' and 'user.pl' for your program files :) • Saving and reusing of a program composed in this way might be difficult or impossible (depending from interpreter you are using) • To terminate the writing and to return to prolog prompt use <Ctrl>+D (although just typing of single '.' works in some versions)
Executing queries • When the program is loaded you may give queries to prolog interpreter, e.g. rule1(a). • Queries also must terminate with dot ('.') • Depending from the query you will receiver either answer yes or no (or get a 'stack owerflow' message or never any result at all :)
Executing queries • Basically yes/no are the sole results a logic programming language is expected to produce (but later you will find out that Prolog actually has some basic input/output facilities) • Still, if your query includes a variable (e.g. rule1(X).), in the 'yes' case prolog will produce a value for this variable that makes the query true • Now, if you continue with '.' the query will terminate • If you continue with ';' the prolog will look for other possibilities to satisfy the same query
Terminating the session • Somehow this is tricky. The correct way to do this is to use command halt. (somehow not the most intuitive name for this...) • And <Ctrl>+Z will do as nicely...