Home

User's Guide - bei IF Computer

image

Contents

1. Settings GE Add to Source Control IV Docking View Hide Properties 5 Select Your_project_name not loaded item 6 Press right mouse button again 7 Select Load Project in pop up menu Gas Workspace MyApp 1 project Load Project Y Docking View Hide Properties Your project is now reloaded 11 10 2 How to create IF Prolog Console Application Prolog sources only 1 Start IF Prolog Application Wizard selecting it as a new project type in Visual C 2 In Step 1 select IF Prolog Console Project 3 In Step 2 select Pure Prolog source 4 Select Finish button 5 In New Project Information dialog box select OK button User s Guide 280 IF Prolog V5 3 Graphical User Interface MS Windows 11 10 3 How to create IF Prolog Console Application Prolog and C C sources 1 Start IF Prolog Application Wizard selecting it as a new project type in Visual C 2 In Step 1 select IF Prolog Console Project 3 In Step 2 select Prolog amp C C source 4 Select Finish button 5 In New Project Information dialog box select OK button 11 10 4 How to create IF Prolog GUI Application Prolog sources only 1 Start IF Prolog Application Wizard selecting it as a new project type in Visual C 2 In Step 1 select IF Prolog GUI Project 3 In Step 2 select Pure Prolog source 4 Select Finish button 5 In
2. IF Prolog lt version gt lt operating system gt Copyright C Siemens Nixdorf Informationssysteme AG 1996 Serial lt serial gt lt license gt user user is the Prolog prompt It indicates that you are at the Prolog top level and that Prolog is waiting for your input First Prolog query Sample session Exiting Prolog You can now start experimenting with Prolog But first you should know how to get out of Prolog so that you can exit whenever you need to You may make either a temporary exit or a permanent exit When you exit Prolog temporar ily you can have commands executed in a subshell and then return to the Prolog system This is particularly useful if you have already done some work with Prolog but have not yet saved your database Quitting Prolog completely at such a stage would result in the loss of all your dynamic data To exit temporarily from Prolog type user system i You must terminate every input in a Prolog dialog with a period and the lt gt key The command interpreter prompt now appears on the screen indicating that you can con tinue working in the operating system s soon as you type exit the Prolog system again issues its prompt exit yes user To quit Prolog completely type user halt You can also quit the Prolog system by pressing the key combination errr b The s
3. write screen on the screen n on the screen or by making the screen the current output stream and then addressing it implicitly set_output screen lt The following settings are now effective Designation Opened stream Standard input Keyboard Standard output File proout Standard error output Screen Current input Keyboard Current output Screen Current error output Screen You can address the standard output with the name user_output write on current output n e on current output write user_output on standard output n User s Guide 86 IF Prolog V5 3 Input output interface File processing If you now terminate Prolog the file proout will contain the following IF Prolog Copyright C Siemens Nixdorf Informationssysteme AG 1996 A B yes on current output yes no yes yes yes on standard output yes Ml a b c d 5 3 Working with files In order to work with Prolog you must make all the necessary information available in the Prolog database in the form of predicates When Prolog is called the system automatically provides a default database containing the built in predicates Maintaining and modifying data in the database alone would not only be laborious but also rather inefficient since all the data would be lost at the end of the Prolog session You should therefore work with files You ca
4. father_son 0 Send_Pipe Receive_Pipe Server Args I open Send Pipe read Send stream copy Send user input open Receive Pipe write Receive stream copy Receive user output exec Server Args father_son _Pid Send_Pipe Receive_Pipe _ _ open Send_Pipe write Send open Receive_Pipe read Receive task Send Receive DRO OC OO GO kkk kkk k kkk kkk k I k kkk k 2k KK KK a task 2 requests an input reads it from the current input stream and passes it to evaluate 3 This continues until end_of_file is entered DOR OC OO k kk kkk k kkk kkk k kkk OK kkk k kkk k 2k KK KK IF Prolog V5 3 93 User s Guide Process management Input output interface skip header Stream repeat get_until Stream n Line _ Line ready task Send Receive skip_header Receive repeat write gt get_until n Command Stop evaluate Command Send Receive Stop end_of_file stop_server Send Receive PROC ESE stop_server 2 closes the pipes evaluate 3 passes the input to the Send_Pipe and writes the result from the Receive_Pipe to the current output stream K K kk OO OO 2K K K FK 2K K FK 2K 2K K FK 2K K KK KK KK K KK KK KK KK KK KK KK KK KK KK 2k 2k stop_server Send Receive close Send close Receive write quit nl evaluate _ _ evaluate Command Send Receive write Send Command nl Send flush_out
5. IF Prolog V5 3 265 User s Guide MS Windows Graphical User Interface 11 7 4 Configuration Options This is the Configuration property page IF Prolog Y5 2 Options Figure Configuration property page IF Prolog configuration You can modify stack sizes memory parameters Option Description nobuf The standard input streams are not buffered by Prolog nosignal This option prevents the Prolog system from managing signals itself i e it defines no user defined signal handling routines The predicate signal_control 2 has no effect mi Defines minimum size of the memory blocks requested by the Prolog system Default 8K ms Defines initial size of the total memory for the Prolog system Default 8K msc Defines initial value of the constraint data stack Default 8K msg Defines initial value of the global data stack Default 30K msl Defines initial value of the local data stack Default 30K mst Defines initial value of the variable instantions stack trail Default 8K msx Defines initial value of the extended term stack Default 8K User s Guide 266 IF Prolog V5 3 Graphical User Interface MS Windows 11 7 5 Cross Reference Options This is the Cross Reference Options property page IF Prolog Y5 2 Options Figure Cross Reference Options property page The Cross Reference tool displays predicate information for particular file The information includ
6. Since the variable names which describe the input sequence and its tail in the difference lists are assigned by the Prolog system itself they cannot be accessed directly for explicit Prolog goals However if appropriate information is required it can be made available by means of additional arguments By inserting explicit goals and using additional arguments a context free grammar can be turned into an attributed grammar This not only performs syntactic analysis but may also contain semantic elements User s Guide 110 IF Prolog V5 3 Prolog grammar rules Explicit Prolog goals Example The sample grammar given above will accept meaningless sentences This can be avoided by replacing the production rule for sentence by sentence sentence N_ph V_ph gt nominal_phrase N_ph verbal_phrase V_ph not N_ph n_ph X noun bird V_ph v_ph verb barks In a more comprehensive grammar however we would like find a more systematic way to exclude semantically incorrect sentences Example Explicit Prolog goals are used here to calculate the numeric values of verbally specified inte gers in addition to performing a syntactical analysis The restriction to three digit integers can easily be removed by means of additional grammar rules number 0 gt zero number N gt digit H hundred and_tens T N is H 100 T number N gt sub_tens N and_tens 0 gt and_tens N gt a
7. User s Guide 252 IF Prolog V5 3 Graphical User Interface MS Windows 11 4 Prolog Console with Scrollbars This is the IF Prolog Main Window Console with Scrollbars eaea za ict fd fd 525 54 a Pat Pt De Dec E EAA E Js user 141 142 shift _left 5 C prologi Demos Standard Hanoi pro user 134 135 shift _stone 4 C prologi Demos Standard Hanoi pro user 118 126 132 stone 3 C prologi Demos Standard Hanoi pro user 162 169 text 3 C prolog Demos Standard Hanoi pro user 57 threetimes 0 C prologi Demos Standard Hanoi pro user 154 up_stone 4 C prologi Demos Standard Hanoi pro user 110 111 user bd re Eg ra Sh Ss Figure IF Prolog Main Window Console with Scrollbars Console Window is standard input output device for IF Prolog It allows you also to ex change text between the Clipboard and Console window If you click on the right mouse button the pop up edit menu appears Cut Ctrli X Copy Ctrl C Paste Ctrl V Figure Console Window Pop Up menu You can use left or bottom scrollbars to move displayed text and to show previous out put Console window has also several key sequences that can be used to launch IF Prolog commands IF Prolog V5 3 253 User s Guide MS Windows Graphical User Interface 11 4 1 Console Key Sequences Key Sequence Description Note Ctrl D Ctrl N Ctrl O Ctrl Break F10 Shift F10 Shift F11 F5 Shi
8. descendant Name asserta child Name you can compile the file the predicate descendant 1 is compiled child 1 is excluded from compilation This makes it possible to modify child 1 dynamically 7 5 2 Loading compiled files The result of a compilation is not linked into a main program before the program is started as you might expect if you have worked with other languages such as C Pascal FORTRAN etc In Prolog the compiled modules are read into the database after the Prolog system has been started This operation corresponds approximately to the use of reconsult 1 for non compiled predicates and is called loading All clauses and directives of a file that are not assigned to a module interface are assigned to the body of the built in module user User s Guide 144 IF Prolog V5 3 Module concept Loading files e The initialization directives are executed in the context of the relevant module e If the file contains a predicate that is already defined with the same functor and arity in the module user it is replaced by the new definition as in the case of reconsult 1 e A module that has already been loaded into the database is replaced in its entirety by the module definition in which a module with the same name is defined in the file After the file has been loaded you can use the predicates in the same way as interpreted predicates However they can be neither modified nor analyzed with the debugger There
9. New Project Information dialog box select OK button 11 10 5 How to create IF Prolog GUI Application Prolog and C C sources dialog based 1 Start IF Prolog Application Wizard selecting it as a new project type in Visual C 2 In Step 1 select IF Prolog GUI Project 3 In Step 2 select Prolog amp C C source Custom dialog based GUT 4 Select Finish button 5 In New Project Information dialog box select OK button 11 10 6 How to create IF Prolog GUI Application Prolog and C C sources MFC window based 1 Start IF Prolog Application Wizard selecting it as a new project type in Visual C 2 In Step 1 select IF Prolog GUI Project IF Prolog V5 3 281 User s Guide Prolog applications Appendix 3 In Step 2 select Prolog amp C C source Custom window based GUT 4 Select Finish button 5 In New Project Information dialog box select OK button User s Guide 282 IF Prolog V5 3 Appendix A Preparing Prolog applications for marketing An application which you deliver to a customer is referred to in the following as a Prolog application Such Prolog applications comprise two parts e the Prolog kernel system e the runtime environment To ensure that a Prolog application is executable at your customer s site the customer must have purchased and installed the Prolog runtime system license information Prolog kernel system You gener
10. Program abort on serious initialization error Procedure e Preparatory steps Each source file in which the passive C interface is to be used must contain the following User s Guide 222 IF Prolog V5 3 Passive C interface Application include statement include cpro h e Initializing the Prolog system When the Prolog system is initialized you can specify various options on the operating system command line These options can be taken from the parameters of the main function by means of argc and argv The call InitProlog argc argv in out err initializes the Prolog system the parameters in out and err define the standard streams for the Prolog system e Parsing a Prolog goal A goal must exist as a C character string goalstring A structure context of type t_parse_context must be defined in the source text t_parse_context context This structure is passed as the second parameter when the parser is called ParseProlog goalstring context ParseProlog checks whether context is a structure of type t_parse_context and stores the new information about goalstring in the components of this structure thereby overwriting the old information If a goal belonging to goalstring is to be used repeatedly it need only be parsed once provided you have not in the meantime used the variable context when parsing another goal e Creating Prolog goals You can now create an active or passive Prolog goal using
11. The predicate readatom D Z is to have the following properties When called it opens file D reads the first line from the file forms a Prolog atom out of it and unifies it with Z During backtracking it reads the next line each time and passes it to Z At end of file the next call fails and the file is closed include lt stdio h gt include cpred h define LINELEN 100 User s Guide 216 IF Prolog V5 3 Active C interface Linking a coroutine static BOOLEAN readline FILE fp char line int c int i 0 c getc fp while c EOF amp amp c n amp amp i lt LINELEN line i c c getc fp line i 0 return c EOF typedef struct FILE fp ReadState COROUTINE Read ReadState state RESUME 1 Label1 HASEPILOG BEGIN char line LINELEN 1 STRING name PrologEnableEpilog state gt fp NULL if TermIsAtom PrologArg 1 amp name FAIL gt if state gt fp fopen name r NULL ErrorContextSet EXISTENCE ERROR cannot open file name 1 NULL FAIL gt while readline state gt fp line if TermUnifyAtom PrologArg 2 line DETACH 1 Label1 else PrologUndo gt IF Prolog V5 3 217 User s Guide Overview Passive C interface EPILOG if state gt fp NULL fclose state gt fp printf file closed by epilog n END void Cboot void CPRED readatom 2 Read
12. 9 1 Debugger settings Before starting debugging activities you can use built in predicates to set certain controls which are to apply to the tracer or the interactive debugger e debug_mode 3 defines the debug mode of a predicate e debug_config 3 configures the debugger e trace_config 3 configures the trace The predicates are described at the end of this chapter 161 Debugger settings Debugging 9 1 1 Activating deactivating box ports The debugging tools enable you to follow the progress of goal execution with the box model Each of the thirteen ports of the extended box model serve as a possible control point Before starting debugging you can select which box ports are to be activated by default for your debugging session The port settings apply to both the tracer and the interactive debugger To select the ports you use the built in predicates debug_config 3 or trace_config 3 The following table shows the default settings in boldface for the debugger Parameter Value Meaning call on Debugger Breakpoint at the call port off Trace Activate deactivate listing for the call port error on Debugger Breakpoint at the error port off Trace Activate deactivate listing for the error port exit on Debugger Breakpoint at the exit port off Trace Activate deactivate listing for the exit port fail on Debugger Breakpoint at the fail port off Trace Activate deactivate listing for the fail port redo on Debugger B
13. Module body i You must not overlay reexported predicates with private predicates You must not reexport predicates which have the same indicator but have been declared in different modules 7 3 5 Changes to the module interface Changes to the module interface are ignored by other modules that already exist In other words the redefinition of a module interface does not affect the visibility of predicates in other modules For reasons of program transparency any changes to a module interface should be made only during the module development or debugging phase 7 4 The module body The module body is introduced by the begin_module directive and constitutes the private part of a module It contains the implementation details of a module The module body is divided syntactically into two parts the declaration part and the implementation part The module body ends with the next module begin_module or end_module directive or at the end of the logical file begin module Module Declaration part Implementation part end module end module Module import Module import PredicateListfromM odule discontiguous PredicateList Declaration part multifile PredicateList private PredicateList dynamic PredicateList meta PredicateList Literal Literal Goal Implementation part Grammar Rule initialization Goal init Goal Module is an atom which speci
14. This is the Main IF Prolog toolbar D sasea saelas FEE ele E elo l Figure Main IF Prolog toolbar Main IF Prolog toolbar contains buttons for general commands such as file handling com pile consult clipboard functions cut copy and paste execution handling interrupt break and tools database browser options This is a list of toolbar buttons with descriptions Button Command Description EE Bl ll EE ee ee Create New Consult File Reconsult File Load File Compile File Cross Reference tool on File Edit File Consult Active File Reconsult Active File Compile Active File Cross Reference tool on Ac tive File Edit Active File View IF Prolog Database Create new file end open it in the default editor file is named untitled pro Consult a file and set it as the IF Prolog active file Reconsult a file and set it as the IF Prolog active file Load a compiled file Compile a file and set it as the IF Prolog active file Run Cross Reference Tool on a file Open a file in the default editor and set it as the IF Prolog active file Consult IF Prolog active file Reconsult IF Prolog active file Compile IF Prolog active file Run Cross Reference Tool on IF Prolog ac tive file Open IF Prolog active file in default editor Activate IF Prolog Database Browser IF Prolog V5 3 249 User s Guide MS Windows Graphical User Interface Butt
15. e frequency gives the number of passes through a breakpoint The number is incre mented by 1 whenever the breakpoint is reached By formulating a frequency condi tion in the commands stop 4 and stop_interactive 1 2 4 you can specify that a port is to become a breakpoint only after a certain number of passes stop_interactive p 1 call frequency gt 5 e depth gives the current tree depth in the search tree By formulating a depth condition on setting an explicit breakpoint you can specify that a port is to become a breakpoint only at a certain tree depth stop_interactive p 1 call depth gt 3 e recursion gives the current recursion depth during execution of a recursive predicate The recursion depth is incremented by 1 whenever the predicate is called or is called again recursively call redo The recursion depth is decremented by 1 whenever a recursive call returns fail exit By formulating a recursion condition on setting an explicit breakpoint you can specify that a port is to become a breakpoint only at a certain recursion depth stop_interactive p 1 call recursion 3 e match Head checks at the time of setting the breakpoint whether the Head of the specified clause can be unified If this check fails the breakpoint is not set If unification User s Guide 172 IF Prolog V5 3 Debugging Branches is possible formulating a match Head condition will cause the debugger to stop at the breakpoint when Head is u
16. member X L 7 2 Module objects IF Prolog manages two classes of objects in modules e predicates e global variables IF Prolog V5 3 119 User s Guide Metapredicates Module objects Objects of these types are known as module objects Each module object is assigned to precisely one module known as the original module There are no module objects which are assigned globally i e assigned to all modules or to none at all This applies also to all built in module objects By default module objects are visible only in their original module This means that it is possible to define objects with the same name in different modules without name conflicts You can declare predicates publicly export You can then make them visible in other modules import The indicator name and arity of a predicate is significant for its identification Each module object in a module must be uniquely identified This means that it is impossible to make predicates with the same indicator from several modules visible in any one module or to define a predicate as private and at the same time import it Built in system predicates are made visible in all modules as standard provided they are not overlaid by local definitions or by explicitly imported predicates with the same name and the same arity Global variables are only visible in their original module The module system has no language constructs for making them visible in other
17. parentS cronus rhea zeus parentS cronus rhea hera parentS cronus rhea demeter parentS zeus leto artemis parentS zeus leto apollo parentS zeus demeter persephone user female 1 xxx female artemis female persephone female X mother X _ user male 1 xxx male apollo male X father X _ user mother 2 xxx mother gaia uranus mother X Y parentS _ X Y xxx user father 2 xxx father X Y parentS X _ Y user sibling 2 xxx sibling X Y parentS A B X parentS A B Y x Y user sister 2 xxx sister X Y female X sibling X Y User s Guide 22 IF Prolog V5 3 Sample session Modifying database in Prolog dialog xxx user brother 2 xxx brother X Y male X sibling X Y yes You can now ask a question for example user incest zeus Goddess or search for all incestuous relationships user incest X Y Note the positions of the atom zeus and the variable Goddess in the question incest zeus Goddess As the rules for incest apply directly to the parentS facts the sequence incest Father Mother must be observed If you were to ask incest Goddess zeus Prolog would answer no 2 6 Modifying the database in a Prolog dialog Prolog also provides built in predicates for deleting clauses and predicates Deleting single clauses To delete a clause from the da
18. stream_copy Stream1 Stream2 unix_make_pipe Pipename Redefine streams Create a pipe User s Guide 92 IF Prolog V5 3 Input output interface Process management Example for process management and communication using pipes The following predicate activate 2 first creates two pipes and then spawns a child process In the child process these two pipes are used as the standard input and standard output streams Now the child is overlaid by a program Prog with the arguments Args This program reads an integer from standard input calculates the square of the integer and writes the result to standard output The parent process uses the predicate task 2 to communicate with the child process KKR KKKKKKKKKKKKKKKKK KK KK KK KK K KK KK KK K KK KK KK KK KK KK KK KK KK KK Create pipes and generate child process The child process is overlaid by the program Server with the parameters Args KKR KKKKKKKKKKKKKKKKKK KK KK KK kkk kkk k KK IR KK KK KK kk KK KK KK x activate Server Args unix make pipe Send Pipe unix make pipe Receive Pipe unix_fork Pid father_son Pid Send_Pipe Receive_Pipe Server Args DRC COO OOO RI KK KK A KK KK KK KK 2k k 2k KK KK The parent process opens the pipes The child process opens the pipes and makes them the standard input and standard output and is finally overlaid by the program Server PRR OO CO E E E E OR I kkk KK KK KK KK KK KK KK 2k a6 2k
19. 264 ba ee ee we ee ws 9 13 Further parameters for the debugger Oa Nie Be ek Boe oe eS ES Se EG Sok ee 6 Gree ee eek ee 9 2 1 Predicates for the trace 64 24 4 bee ra Ow Dee Blant tr de er a e eS ek RR G 9 23 Trace Operation s e ea ne BY RR ER AE Oe od oo wD a san ee eee eee Poe BUS Se bLESH ROE SY 93 Interactive debugger 2c eu dae pa oe ew oe ole He ls a we ees 9 3 1 Debugger Operation e ss tae o Se teo a Dkk KEE ee ER A 2a os bs eee be 6 ES Soke ed ee ee ee ee RS 9 3 3 Controlling debugger execution 0000004 O24 Branching in the proof tree 4 4446 sera a ORG Ree 2S 9 4 Debugging with the alphanumeric user interface 9 5 Debugging under the OSF Motif user interface 95 1 Controlling xecuttion ccoo ara Bde 0 FE DELNE POO RN 9 5 3 Setting explicit breakpoints 0 0000 eee A AE 9 5 5 Displaying ancestors cocer criada G4 Hom Deemer l g ck ak RR a Me ap BRAS e b SR ode e 9 6 Summary tables of debugger COMMANdS 10 The C interface JUL Active interface oia a A DEERE BA 101 1 Linking simple C Cons s s scs sesca dee da Oo n 101 2 Complex MOOD ba n e ee a A e Hers 10 1 3 Built in macros and functions for connecting complex C functions 10 2 Passive C interface aooaa a a a a a 161 161 162 163 163 164 164 165 165 167 167 168 170 173 173 174 183 184 185 186 189 190 190 191 195 196 201 205
20. 5 4 2 Predicates for process communication with signals 92 5 4 3 Predicates for process communication using pipes 92 5 4 4 Predicates for process communication using sockets 95 5 4 5 Predicates for computer network information 97 Prolog grammar rules 101 Context free grammar aoaaa eee eee eee een 101 Simple pram mar rules scs egan e ewe hewn bbw en FEES Rs SY 103 6 2 1 Non terminals in a production rule 104 O22 Terminals ma production rile scs sss ts sse 54464 a5 44 105 6 2 3 Deriving a start symbol ened ee eee ee ee rao do 106 624 Syntaetie analysis s ie sos ee BEES ROSS RE Ow HRS Se 107 Additional arguments 22 a ROR OR A RO a eR ED 107 Explicit Prolog goals sce 4 42448 44448 Ohba ee naw ewe e de 4 110 Syntax of the production rules 1 a ee 112 7 The Prolog module concept 115 Tol Wiggles s e cla pa ero hoe A AR AE A E A 117 AA II 117 7 12 Logicaland physical Bles s s dee ra 118 T a Mod le objecis sss ss aace nres a a EES 119 Ral IPS si rn e rat a a ad Br oe 120 Tig Metaprediceates s ro a boi e is eie a kon atio EA 121 T23 Global variabl s is sce aci eane aoi eane ara e aa RED e a 125 Tae Module interface AMI 126 7 3 1 Exporting predicates 24 242 5 64 a st es e 127 7 9 2 Exporting metapredicates 4 44 44 be bbw Rw e oO 128 7 3 3 Reexporting all public predicates of a module 130 7 3 4 Reexporting specific public pred
21. View trace Display explicit breakpoints Breakpoints dialog Display constraints View variable constraints View module View predicate View ancestors View first screen View previous screen View next screen View last screen View history Line forward Page forward Line back Page back Help See Also Debugger Commands Logging passes through ports Display the trace field in the window Display all explicit brakpoints Invoke breakpoints dialog it displays the list of breakpoints and allows removing activat ing deactivating or setting the breakpoints Displays a pending constraint subgoal in the context of ist definition Displays all suspended constraints at the cur rent breakpoint Variables that do not occur in the current clause are displayed using their internal name Displays the predicates of the current module from the Prolog database Displays the current predicate from the Pro log database Enables to user to show the current dynamic call chain Displays the first goal or clause box stored Displays the previous goal or clause box stored Displays the next goal or clause box stored Displays the last goal or clause box stored Displays the goal or clause box in the history list Scrolls line by line forward Scrolls page by page forward Scrolls line by line backward Scrolls page by page backward Displays the on line help page on debugger commands and debugger support
22. ah a a a er SSES SE BEGIN indicator debug CALL gt call ancestor X pepsi user A Output window Command fa Command line QS Message lines The topmost two screen lines are used for status indication The bottom four lines are re served for command input error messages and outputs to the standard output and standard error output The lines in between form the output window the length of which is variable You can have different information fields displayed in the output window and also scroll them if the information field is larger than the output window Status indicator The status indicator displays the fully qualified designation of the current predicate and field related notes indicating for example which information field you are located in AND BOX In the second line the debugger displays information on explicit breakpoints or on pending constraints BEGIN Command line You enter debugger commands in the command line Command The command line is the only screen line which you can write to If the command which you enter is longer than IF Prolog V5 3 175 User s Guide Alpha user interface Debugging the command line the debugger automatically continues writing in the message area The admissibility of a command does not depend on the currently displayed field The only exception is the command If the debugger is displaying the current box
23. cpred h IF Prolog V5 3 209 User s Guide Linking a coroutine Active C interface typedef struct i int x NameState int NameFunc int PP void state NameState StatePtr NameState state switch PP case 0 break case 1 goto Labell case 2 goto Label2 case 3 goto Label3 default return 0 StatePtr gt x 1 pred X Y if TermUnifyInteger PrologArg 1 StatePtr gt x X 1 amp amp TermUnifyAtom PrologArg 2 Start Y Start return 1 PrologUndo Label1 StatePtr gt x pred X Y if TermUnifyInteger PrologArg 1 StatePtr gt x X 2 amp amp TermUnifyAtom PrologArg 2 Redo Y Redo return 2 PrologUndo Label2 StatePtr gt x pred X Y if TermUnifyInteger PrologArg 1 StatePtr gt x X 3 amp amp TermUnifyAtom PrologArg 2 Last visit Y Last visit return 3 PrologUndo Label3 return 0 pred X Y J fail void Cboot void CPRED pred 2 NameFunc sizeof NameState If you write the source code into a file file c and then generate an extended Prolog system the new predicate pred 2 is made available to you This predicate now has the same properties as if you had defined it with the clauses given on the right in the above diagram Before a complex C function can be linked to Prolog it has to be fitted as in the example into a framework with the following structure User s Guide 210 IF Prolog V5
24. for the entire proof tree searched by a given goal which may be a part of the entire proof tree of a Prolog application Within a context certain context global actions may be executed In setting up a context information may be stored which will control or influence such context global actions The information is not destroyed until the context is exited upon completion of the goals execution From the point of view of the entire application the information is only local for the duration of the context There are no restrictions on the level of nesting of contexts When context global actions are executed nested contexts are searched from inside to out for information which matches the context global action There are various reasons why normal program execution is not possible not relevant or not desirable These include errors during goal execution external events such as signals and possibly algorithmic requirements global jumps for example You can control program execution within a local program context You can use highly differentiated exception and signal handling procedures with a high degree of reliability and independency within a local program context thus excluding the reciprocal effects of different program sections which make use of global exception handling IF Prolog supports three classes of context global action e Context jumps e Exceptions e Signals The predicate context Goal ContextInfo is used to cre
25. gt is the command for step by step operation In any other field lt is the command which causes the debugger to return to and display the current box Message lines In the first message line the debugger displays error messages relating to the commands which have been issued The other two lines display messages which are not generated by the debugger If these outputs are longer than the two lines the lines are scrolled up into the output window At the next breakpoint the structure of the debugger screen is restored and only the last lines of the output are retained Output window You can have the following information fields displayed in the output window e Help field e Goal box e Clause box e Field for settings e Field for breakpoints e Field for old goal and clause boxes e Field for ancestor goals e Field for database e Constraint field for variables e Constraint field for subgoals e Trace field The information field presently visible in the output window is the current field You can fetch the information fields for help goal box clause box old goal and clause boxes and trace into the output window by means of commands The other information fields appear only as a side effect of commands relating to the current field The following commands enable you to fetch the information fields into the output window User s Guide 176 IF Prolog V5 3 Debugging Alpha user inter
26. q e g Lp e e p X Y EXIT lt p LJ lt p Li FAIL calling e g l p and e p X Y 3 5 8 Activating backtracking If a subgoal of a clause cannot be proven with the specified variable instantiations the variable instantiations specified during the execution of this subgoal will be undone and backtracking will be activated The system returns to the most recent choice point There are two ways that this can happen e The system goes back to a preceding subgoal of the same clause where there are still choice points The variable instantiations performed since this subgoal was proved are undone e Otherwise the clause itself fails Representation in the box model From the fail exit of a subgoal box Prolog proceeds e to the redo entrance of the preceding subgoal box e or to the failbody exit of the surrounding clause box Once the clause box is left through the failbody port it is destroyed User s Guide 46 IF Prolog V5 3 Database and procedure execution Procedure execution Activate backtracking 3 5 9 Redoing a goal When backtracking the system returns to the last choice point set during execution of the goal This can also be a choice point in a subgoal If no such choice point exists the system returns to the last choice point set when executing the precedin
27. sizeof ReadState 10 2 Passive C interface The following diagram gives a general overview of the operating principle of the passive C interface Link Prolog with C function gt Initialize Prolog Y Generate Prolog query Y Prolog Activate Prolog goal C funcher Y Call Prolog goal Initiate backtracking i Release resources sg lt Terminate Prolog Control flow Information flow Functions of the active C interface User s Guide 218 IF Prolog V5 3 Passive C interface Overview You initialize the Prolog system by calling the function InitProlog At this point you can add to the database predicates which you will need later for executing goals A Prolog query is initially presented as a C string You must use the function ParseProlog to convert this string into a goal term and the function PrologOpen to convert it into an active Prolog goal The information about an active goal and its processing are kept in a data structure of type t_cursor During goal execution the function PrologFetch accesses this information and places the results in the same structures When processing of the current query is completed you use the function PrologClose to release the resources associated with it When you no longer require the passive C interface you release the memory space occupied by the Prolog system
28. to be covered during backtracking thereby speeding the search up and reducing memory requirements These cuts limit the set of alternative proofs of a goal by a pro gram The sequence of clauses in the database is relevant for the set of successful proofs of a goal In the first example the following clauses are contained in the database tree Branch Twig Leaf branch Branch Twig leaf Leaf Clause 1 branch Branch Twig branch Branch twig Twig h Clause 2 branch branch1 true Clause 3 branch branch2 true Clause 4 twig twigl true h Clause 5 twig twig2 true h Clause 6 leaf leaf1 true Clause 7 leaf leaf2 true Clause 8 You then enter the following query user tree Branch Twig Leaf Branch branchi Twig twigl Leaf leafi Branch branchi Twig twigl Leaf leaf2 Branch branch2 Twig twig2 Leaf leaf2 no User s Guide 52 IF Prolog V5 3 Database and procedure execution Procedural predicates In response you are given all paths in the tree from the trunk to any leaf Originally then the tree has the form branch 1 branch 2 leaf 1 leaf 2 leaf 1 leaf 2 leaf 1 leaf 2 leaf 1 leaf 2 Original form of the tree Using a cut the tree should now be pruned First add it to the end of clause 1 i e tree Branch Twig Leaf branch Branch Twig leaf Leaf Clause 1 new The cut removes all alternatives for the subgoals locate
29. with the function EndProlog 10 2 1 Overview of the built in data types and functions Central data types Information is exchanged between a C program and the Prolog system using four data structures The data types on which these are based are defined externally or in the include file cpro h which resides in the directory PROROOT c e MODULE contains information relating to the module whose predicates are accessed by the Prolog query You cannot access the individual components of the structure typedef struct _t_module MODULE e t query is a structure containing a Prolog goal which is ready to execute but inactive You cannot access the individual components of the structure typedef struct _t_query t_query e t_cursor is a structure containing an active Prolog goal You cannot access the individual components of the structure typedef struct _t_cursor t_cursor e t_parse_context contains information which is placed there after analysis of the C string typedef struct TERM pc_term TERM pc_varlist long pc_position int pc_errno t parse context IF Prolog V5 3 219 User s Guide Overview Passive C interface You can access the components of the structure t parse context where the individual components contain the following after initialization Component Contents pc term The Prolog term which is read pc varlist The list of Prolog variables as a Prolog term in the form Variablename V
30. you have declared predicates as public with an export directive you may not declare them locally as metapredicates Implementation part The implementation part of the module contains the predicate definitions In addition you can define goals there which will be executed in its module context once the module has been loaded You can define any number of module bodies in a logical file the declaration part must be completely defined within a single logical file The implementation part of a module body may be spread over any number of logical files This enables it to be partially loaded or redefined with consult 1 or reconsult 1 7 4 1 Importing all public predicates of a module The import directive is used in the declaration part of the module body It enables you to import all the predicates which were exported in Module The import directive requires that the module interface of Module be known Module is an atom which defines the name of the module whose module interface is to be imported If you declare a predicate as private with an export meta dynamic or private directive it is not imported If you have already imported a predicate from a different module it is not imported again by the general import directive In this case a warning appears IF Prolog V5 3 133 User s Guide Importing predicates Module body i In this respect the general import directive differs from the selective import directive Wit
31. 211 10 2 1 Overview of the built in data types and functions 219 10 2 2 Working with the passive C interface 221 103 LAUSANA A 228 10 31 tolvas SHTUCNES kan ee css cnt a A ede 8 228 10 3 2 Control functions ova a a ARA 232 10 3 3 Functions for implementing device drivers 234 10 4 Linking Prolog and Ce so c cce arar ca 238 11 Graphical User Interface for MS Windows 243 11 1 The IF Prolog Graphical User Interface oaoa aaa a 243 11 2 167 Prolos Main Window Menu Bar ee cas V r ad kii sadro riis 244 MAL File Men sorna SSS a e Pers 245 OS Ede MEL asus kebus r keena a dad a NE eS 246 11 2 3 Active File Menu 1444 44 boo ee we aa aaa ara 246 24A EI 246 125 Debug Meni seen m be ok Sk eek ee eee OE EE See mel Y 247 11 2 6 Options Meng 224 224 445 44 4 oo bw Ea be ee ee aS 247 Moy Help Ment 6 siei ee ee Ge ee E 248 11 3 Main Window Toolbars soe e e s occa 2462 ao oe moren ee EOS 248 MAL Mam Toolbar s s s se sysco selenu a E a E o e es 249 11 32 Debug Main Toolbar gt e se p2 entrando ed wa dead 250 1133 Debug Information Toolbar s sess e sore pai eaa D ea s 251 11 4 Prolog Console with Scrollbars 2 0242 wee Bee eRe Eee Se ed 253 11 4 1 Console Key Sequences eee eee ee ke 254 VILA Status Bar da ee es as AA a a 255 11 6 Database Browser lt lt ic ergab ie de ek NOE kode en pa e sk e e ma 256 11 6 1 Database Browser Window 2244 22 46 essere e eee 257 11
32. 3 Active C interface Linking a coroutine ttinclude cpred h typedef struct int x int y State int Func_name int PP void state State StatePtr State state switch PP case 0 break case 1 goto Labell case 2 goto Label2 default return 0 StatePtr gt y StatePtr gt x return 1 PrologUndo Label1 return 2 PrologUndo Label2 return 0 void Cboot void CPRED Pred_name Arity Func name sizeof State 10 1 3 Built in macros and functions for connecting complex C functions The macros and functions described below facilitate the development and linking of complex C functions The macros and functions are listed here in the same logical order in which IF Prolog V5 3 211 User s Guide Linking a coroutine Active C interface they should be used in the program text In the case of macros the definition in the include file cpred h is also given at the end of the macro description COROUTINE c function State StatePtr This macro call constitutes the beginning of the function c function Name of the new C function this must be the same as the name specified in CPRED or MCPRED State Type of a state structure defined in a typedef statement StatePtr Pointer to a structure of type State bf Source code define COROUTINE n t s int n int _p void _s t s t _s switch _p case 0 break RESUME ProgPtr Label s
33. 3 Prolog grammar rules Simple grammar rules verb gt runs verb gt barks Start symbol sentence generates the language consisting of the two sentences the dog runs the dog barks Here consecutive lists were combined to form a single list 6 2 Simple grammar rules Prolog enables you to enter production rules directly in infix operator notation To create a grammar we shall begin with the rules on which the above example was based namely e Terminals Prolog uses single element lists as terminal symbols The elements in these lists are the tokens of the processed token sequence e Non terminals Prolog atoms e Production rules made up of a head consisting of a non terminal symbol a production operator gt followed by the body of the production rule This consists of at least one or a series of gt terminal or non terminal symbols separated by The end of a production rule is indicated by a period e Start symbol A start symbol does not have to be specially identified as such For derivation purposes any non terminal can be used as a start symbol Extensions to these rules are outlined in sections Additional arguments page 107 and Explicit Prolog goals page 110 IF Prolog V5 3 103 User s Guide Simple grammar rules Prolog grammar rules 6 2 1 Non terminals in a production rule As already mentioned production rules of this s
34. 30 2 looped Prolo e se sest es 44 46 68 44484084 a a A 56 Prolog system 61 Organization of the Prolog system e c scorer eko be et 2 0 a 61 ALI Calling Prolog s4 2009 sk a a ea ek ode a EEE eA eS 62 el Eang Prol ase ee oe ew e al a as cae SS Sw 68 Querying information about the system 69 4 2 1 Querying system size parameters 69 4 2 2 Querying system properties oc e 70 42 3 Qu erying other information lt lt r beo be Re Ba kode ERED DS 72 4 2 4 Querying memory management ee 72 Environment variables ccoo aer Eee ER RRR HS 74 Interfaces of the Prolog system o e ees 76 5 The El 9 2 9 3 5 4 6 The 6 1 6 2 6 3 6 4 6 5 input output interface 79 Input output in the Prolog top level dialog 79 Elementary input output in Prolog s ses s seor semo aeei na 80 5 2 1 Input output using current streams ooo a 82 5 2 2 Explicitly specifying streams for input output 83 5 2 3 Querying information about input output streams 85 Workings silt flee gt sed ea ria rra at aa A 87 5 3 1 Editing files with an editor 87 5 3 2 Reading a file into the database 90 5 3 3 Writing the database to a file 91 Process management and communication 91 5 4 1 Predicates for process management o 91
35. Bar When you move the Status Bar displays contains a text area and keyboard indicators mouse cursor over a toolbar button or select a menu item Status Bar displays short help information to corresponding button or menu command Keyboard indicators shows the current keyboard mode Num Lock Caps Lock or Scroll Lock IF Prolog V5 3 255 User s Guide MS Windows Graphical User Interface 11 6 Database Browser IF Prolog Database Browser helps to maintain large Prolog applications It can show each loaded or consulted module its predicates predicates visible in this module module variables and operators When available Database Browser displays listing for the module or particular predicates and operators only available when module or predicate is consulted in debug mode For system predicates the manual page is shown This is the IF Prolog Database Browser Window E shift stone 4 stone 23 E text 3 E threetimes 0 E tower 73 E up stone 4 W wa Predicates Visible in Modu kag Module Variables H I ifprolog41 LJ os EL system E IF Prolog Database Browser OL ES View Sort p IF Prolog Database a ll 6 xxx hanoi initialise 1 xxx E 14 Module Predicates initialise N E E dlear_it 3 debug_mode inform 2 _ off E clear pole 3 clear screen pii abolish tower 3 E daw base 0 abolish max 1 E gotoxy 2 N1 is N 2 SEN meee ball set_it N 1 lise 7 1 eae flush output
36. Exceptions fail eval end of file halt eval Goal call Goal exception message Error Message write formatted xxx EXCEPTION s s Yn Error write_formatted q in q n Message 1 There is a special predicate which creates a context which handles only exceptions The pre dicate exception handler Goal Exception Handler creates a context for Goal If while Goal is being proved an exception is raised whose description can be unified with Excep tion as the term error Error Info the goal which raised the exception is replaced by Han dler and execution is resumed The call exception_handler Goal error Error Info Handler is therefore identical to context Goal error Error Info gt Handler The predicate context 2 has the advantage that different types of events can be handled in one context i Do not attempt to handle all exceptions in a single context because then there is a danger of endless loops You should always handle possible exceptions specifically Example The following Prolog text implements a simple autoload mechanism for predicates which are defined in a collection of facts private pred1 3 pred2 2 pred3 1 auto_load pred1 3 hello pro auto_load pred2 2 preds pro auto_load pred3 1 hello pro mybreak context break error existence_error procedure Pred Info gt try_autoload Pred Info try_autolo
37. Generation User s Guide 16 IF Prolog V5 3 Sample session Modifying database with editor parentS cronus rhea zeus parentS cronus rhea hera parentS cronus rhea demeter Third Generation parentS zeus leto artemis parentS zeus leto apollo parentS zeus demeter persephone Facts female name male name female artemis female persephone male apollo X is mother of Y if X is the second parent of Y mother X Y parentS _ X Y X is father of Y if X is the first parent of Y father X Y parentS X _ Y X is female iff X is mother female X mother X _ X is male iff X is father male X father X _ X and Y are siblings iff F and M are parentS of X and F and M are parentS of Y and X and Y are not identical sibling X Y parentS F M X parentS F M Y X X Y IF Prolog V5 3 17 User s Guide Modifying database with editor Sample session X is sister of Y iff X is female and X and Y are siblings sister X Y female X sibling X Y X is brother of Y iff X is male and X and Y are siblings brother X Y male X sibling X Y X is parent of Y iff X is father of Y OR iff X is mother of Y parent X Y father X Y parent X Y mother X Y X is ancestor of Y iff X is parent of Y OR iff X is parent of Z and Z is ancestor of Y ancestor X Y parent X Y ancestor
38. Interactive debugger The various windows and the input and output of the interactive debugger are described in detail in chapter 9 5 2 Elementary input output in Prolog Two groups of input output predicates are available for input and output namely those which refer implicitly to a current stream and those which require the stream to be explicitly given Current streams There is one current input stream one current output stream and one current error stream at any given time At system startup these are the standard streams If required you can change this default setting You should redefine a current stream only if you wish to subsequently use it frequently for input or output The input output predicates in this group implicitly address the current streams since a stream is not explicitly specified Input and output using current streams is described in section 5 2 1 Explicitly specified stream The specified stream must already exist when the cor responding predicate is called If the stream is a file it must be open for reading or writing Input and output using explicitly specified streams is described in section 5 2 2 In Prolog streams for input and output are referred to by alias names or by means of a system defined terms which the Prolog system supplies with the predicate open 3 4 The User s Guide 80 IF Prolog V5 3 Input output interface Elementary input output alias names are atoms The following t
39. Prolog is exited The built in predicate break 0 the Prolog input loop defines a context which matches all the context jumps initiated in the course of standard error handling in the Prolog execution Therefore in the Prolog development system an error will seldom be seen ever apart from a very few exceptions which leads to the Prolog system aborting You initiate a context jump with the predicate throw Q Term A call of throw 1 is never evaluated with true or fail instead it always aborts the execution of current goals The Prolog system first searches from inside to out for an active context whose Contex tInfo list contains an entry in the form catch CatchMask gt SubstituteGoal and in which CatchMask can be unified with a copy of Term If such a context is found the proof tree is discarded in this context unification of CatchMask with a copy of Term is executed the call of context 2 relating to this context is replaced with SubstituteGoal and the execution is resumed All the variable instantiations made in the course of execution of the goal within this context are undone as is with fail Only the instantiations created by the unification of CatchMask with a copy of Term are retained The ContextInfo list of the context may also contain multiple entries for each context global action When on the occurrence of a corresponding event the first appropriate information entry is used If no matching context can be found Prolog abo
40. System information Predicates for terminating a Prolog session Predicate Purpose halt Terminate Prolog halt ExitStatus Set exit status and terminate Prolog halt 0 and halt 1 always return you to the operating system level end of file 0 or the key combination crrL p return you to operating system level only if you are on the highest Prolog top level see predicate break 0 4 2 Querying information about the system Information about the current system configuration can be queried while a Prolog session is in progress 4 2 1 Querying system size parameters The predicate is 2 allows you to query the system size parameters user X is ssize gt System time Meaning cputime Seconds of CPU time used time System time in seconds System size Meaning csize Total size of local stack bytes cused Local stack in use dsize Total size of database bytes dused Database in use esize Total size of extended term stack bytes eused Extended term stack in use gsize Total size of global stack bytes gused Global stack in use tsize Total trail stack size bytes tused Trail stack in use ssize Total stack size bytes sused Stack in use The following zero arity functions show the value range available for integers IF Prolog V5 3 69 User s Guide System information Prolog system System size Meaning minint Smallest representable int
41. Type Socket get_socket_option Socket Option Value net_service Service Protocol Port select Streams Timeout ReadyStreams set_socket_option Socket Option Value socket Domain Type Socket socket_accept Socket Addr NewSocket socket _bind Socket Addr socket _connect Socket Addr socket _listen Socket socket _listen Socket Quelen socket _receive Socket List socket_receive Socket Addr List socket_send Socket List socket_send Socket Addr List socket_shutdown Socket socket_shutdown Socket How Query communication sockets Get socket option Query communication service Synchronous input multiplexing Set socket option Create communication socket Accept a connection Bind a name to a socket Connect a socket Listen for connections Listen for connections Receive data from socket Receive data from socket Send data to socket Send data to socket Close a socket Close a socket A socket is the basic object for interprocess communication It is an endpoint of communi cation represented in Prolog by an opaque term The type of a socket specifies the communication properties The following socket types are supported stream sockets provide bidirectional reliable sequenced and unduplicated flow of message data datagram sockets support bidirectional data flow but don t guarantee that the message is sequenced reliable or unduplicated The doma
42. User Interface MS Windows After the two step selection the Application Wizard will show a description of the project created You can see what files that will be generated with a short note After the confir mation the whole project is generated and a read me file for this project is opened Special Microsoft Developer Studio x MN IF Prolog Application Wizard made changes to the project MyApp Please reload the project MyApp firstto keep the changes IF Prolog Application Wizard makes some changes to the project that are not automatically loaded into Visual C You must reload the project by hand This process is described in the chapter How to finish IF Prolog project creation 11 10 1 How to finish 1F Prolog project creation IF Prolog project created by Application Wizard must be reloaded in Visual C environ ment to include all changes made by IF Prolog Application Wizard To reload the project you have to 1 Open the FileView in workspace window o I as 8 Fie a 2 Select Your project name files item 3 Press right mouse button on right handed mouse 4 Select Unload Project in pop up menu IF Prolog V5 3 279 User s Guide MS Windows Graphical User Interface Hr workspace My pp 1 project Smet MyApp files Build Build selection only Clean selection only New Folder Add Files to Project Set as Active Project Unload Project
43. V5 3 Database and procedure execution Procedure execution The clause g X Y contains three subgoals which must also be proved To prove the first subgoal SG1 the clauses of predicate e 3 located in the database are searched for the first clause head which is unifiable with SG1 e X M V SG1 is unified with the fact e1 which is then marked internally as a choice point CP11 for the first subgoal When executing the second subgoal SG2 the system proceeds in exactly the same way SG2 is likewise unified with the fact el and el is marked as a choice point CP21 for the second subgoal The third subgoal cannot be proved with the previously defined variable instantiations and causes a fail This fail triggers the backtracking process internally The unification performed at the last choice point CP21 is undone and the system searches for alternatives for executing the second subgoal The search for alternatives to SG2 again causes a fail which in turn triggers backtracking The unification performed at the last choice point in this case CP11 is undone and the system searches for alternatives for executing the first subgoal and so on When the system finds a solution it displays the corresponding instantiations in the Prolog input loop You can interactively activate backtracking by entering a semicolon to search for further solutions IF Prolog V5 3 49 User s Guide Procedural predicates Database and pro
44. X mother X _ male apollo father X Y parentS X _ Y i In this example the functor parents plural is written with an upper case S to distinguish it more clearly from the functor parent one parent Atoms such as gaia are written with a lowercase initial letter Char acter strings which begin with an uppercase letter are variables Comments are introduced by and terminated by End of line comments are introduced by the percent sign Comments have no effect on program execution The rules for each predicate should always follow one another in your file otherwise the Prolog system will issue warnings If you do not write clauses for a predicate consecutively you should inform the Prolog system by using the discontiguous directive Save the file which was created as gods pro in the current directory and quit the editor The Prolog system now asks whether the file is to be read into the database Should the file gods pro be reconsulted y n Answer yes by typing y and hitting the key The Prolog system cannot access the information until the file has been reconsulted i e read in If your gods pro file includes clauses containing syntax errors the Prolog system now reports these errors The incorrect clauses are not read into the database The Prolog system confirms completion of the consult process xxx reconsult gods pro loaded in 0 02 sec If your
45. X Y parent X Z ancestor Z Y Terminate the editor to store the changes in the gods pro file Answer the Prolog system s reconsult question by confirming that the gods pro file is to be read back into the database User s Guide 18 IF Prolog V5 3 Sample session Extending database in dialog Special cases You can delete individual clauses of a predicate from the database either by marking them as comment lines in your gods pro file i e by enclosing them in and or by deleting them from the file However it is not possible to delete all clauses of a predicate for example all parents facts from the database in this way If you delete all clauses of a predicate from the file even though nothing will be read in for this predicate the old clauses will remain unchanged in the database If on the other hand at least one new or modified clause exists in the file the change will be read in and all the old clauses will be deleted For how to delete all clauses of a predicate from the database turn to page 23 Possible errors Clauses containing syntax errors will not be read into the database A syntax error message will be displayed on the screen Asking questions Now try asking questions about the information you have added to the database for example user ancestor X persephone Prolog outputs the ancestors of persephone Again the same names occur more than o
46. and Prolog sources dialog based e GUI Application C C and Prolog sources MFC window based You can choose the IF Prolog Application Wizard as one of the Visual C project Wizards User s Guide 276 IF Prolog V5 3 Graphical User Interface MS Windows GR SAPI Extension Wizard f Makefile e MFC ActiveX Controlwizard MFC AppWizard dll pata MFC Appwizard exe A Win32 Application Win32 Console Application Win32 Dynamic Link Library Win32 Static Library The selection process includes two steps e Step 1 Choosing the user interface type Console or GUI e Step 2 Choosing the project source type Prolog Prolog amp C C Step 1 IF Prolog AppWizard Step 1 of 2 Prog am FIS UF Prolog 520 MES errar ve Tie nwa pager eu a daera KLUS CCT er TEE SET re iby arn tg FAM sample awg amd beet kM LL en werd ss You can select your desired user interface type Console interface or GUI interface IF Prolog V5 3 277 User s Guide MS Windows Graphical User Interface Step 2 IF Prolog AppWizard Step 2 of 2 Then you can select application subtype if your desired Visual C will include Prolog sources only or C C and Prolog sources GUI application type offers also Dialog based or MFC window based interfaces Confirmation New Project Information User s Guide 278 IF Prolog V5 3 Graphical
47. any module are executed in the context of the module user i Non compiled predicates in the database will be overwritten by any compiled predicates with the same functor and arity when the latter are loaded You cannot load a compiled file into the database with consult 1 or reconsult 1 Possible exceptions Exceptions which may occur during loading include e The file Filename does not exist e Access to the file is not permitted e There is insufficient space in the database e The file was compiled using the compiler of an earlier version of IF Prolog The file does not therefore have the correct format for a compiled Prolog file An exception during loading causes the load operation to be aborted and an exception message to be generated 7 5 3 Removing a module from the database The predicate unload 1 removes all information associated with the specified module from the database Subsequently the module has neither an interface nor a body but its name remains known user unload Module If the removed module was the current module the built in module user becomes the current module If you remove the module user however Prolog immediately creates an empty module interface and an empty body again i You cannot remove built in modules with unload 1 User s Guide 146 IF Prolog V5 3 Chapter 8 Contexts A context is a logical environment for the execution of a goal i e
48. app module app begin module app app write I am a Prolog application nl halt end module app As the next step you must load the Prolog programs of the application with the newly gener ated Prolog kernel system and the supplied runtime system PROROOT prolib runtime pst and store it as a saved system state in a file In the following program gen pro the init directive is used to generate a system state in the file app pst The predicate program 0 is the entry point to the application in the module user init save_runtime app pst halt program app app Now call the new Prolog kernel system and the saved system state with the Prolog runtime system in order to generate the application saved system state With the previously men tioned files and the new Prolog kernel system named app_prolog the call would look like this app_prolog r PROROOT prolib runtime pst c app pro c gen pro Using the files app pro and gen pro you generate the file app pst which as a saved system state contains all the required information and can be delivered to your customers i You may deliver only the Prolog kernel system and the generated saved system state to your customers You may use only PRO ROOT prolib runtime pst to generate the saved system state User s Guide 284 IF Prolog V5 3 Appendix Prolog applications Finally you must also develop a distribution and installati
49. are several ways of loading a compiled file into the database e by specifying an option when calling Prolog see chapter 4 1 1 1 Filename e by using the predicate load 1 during a Prolog session user load Filename Filename is the name of the compiled Prolog file Normally Filename has the suffix cmp The system searches automatically in the following sequence e for a file called Filename e if this is not found for a file named Filename cmp and loads the file found Internal load operation The file generated as a result of the compilation contains two types of code e Prolog source code for predicates excluded from compilation by means of dynamic 1 e compiled code for all other predicates Accordingly loading a compiled file is internally divided up into e Loading all complete modules or module interfaces from the file If the modules already exist in the database they are replaced e Loading all clauses that are not assigned to a module from the file into the body of the module user If predicates with the same name and arity are defined in the module user these are replaced as in the case of reconsult 1 The file contents do not however replace the entire module user IF Prolog V5 3 145 User s Guide Unloading modules Module concept e Execution of all initialization directives in the file in the context of the relevant modules initialization directives that are not assigned to
50. be created and if need be subsequently destroyed The steps taken from port to port and the creation and destruction of boxes are described in detail in the following Goal execution is divided into individual steps Related steps have been combined in the description and are represented first in the order in which they are passed when a goal is being proven In the course of backtracking we return to boxes which were created but have not been destroyed again These are precisely those boxes in which choice points are located Taking into account the individual circumstances involved the entire procedure execution can then be reconstructed from small steps of this sort The Prolog system debugger is derived from this model 3 5 1 Queries and init directives Program execution in conventional programming languages such as C or FORTRAN corre sponds in Prolog to the execution of a goal using the predicates of the database There are three different ways to activate the execution of a goal e Query Every correct input following the prompt user in the Prolog input loop is understood by Prolog to be a goal to be proved in the current database The specified goal term should end with a period E e initialization or init directive If the Prolog system consults or loads a file which contains one or more initialization or init directives the goal terms they contain are proved after the load operation The predicates defined in
51. char_conversion CharacterIn Define a character conversion CharacterOut discontiguous PredicateList Declare discontiguous predicate definition dynamic PredicateList Declare predicates as modifiable end module Conclude the definition of a module end module Module Conclude the definition of a module ensure loaded Filename Insert a Prolog file in the source text once export PredicateList Export predicates import Module Import exported predicates of a module import PredicateList from Module Import certain exported predicates of a module include FileName Include a Prolog file in the source text init Goal Initialization goal initialization Goal Initialization goal meta PredicateList Declare metapredicates module Module Define a module multifile PredicateList Declare predicates as being scattered over several files op Priority Assoc Names Define and delete operators private PredicateList Declare predicates as private reexport Module Reexport predicates reexport PredicateList from Module Reexport certain predicates Any module specific declaration is performed using directives 7 1 2 Logical and physical files IF Prolog makes a distinction between physical and logical files A logical file may comprise a number of physical files You use the include Filename or the ensure_loaded Filename directive to incorporate physical files into logical files
52. concerning objects a fact or a rule see structure Combination of statements A statement A B is true when both statements A and then B are true An atom or a number Restriction for the value of one or more variables To read in analyse and store in the database a Prolog text executing any directives Logical environment for the entire proof tree covered by the spec ified goal A built in predicate that removes choice points Set of predicates and all the objects managed by Prolog exclud ing Prolog stacks 291 Glossary Appendix C declaration part depth first search directive disjunction dynamic predicate error context exception export expression extended term fact flag functor global variable goal ground head of a module Is defined by the directives which are possible in it Search strategy in proof trees which continues to pursue only one path until that path is successful or ends in a blind alley In the latter case for example backtracking can be initiated Structure having the functor and arity 1 A directive is eval uated when Prolog text is read in ORing of statements A statement A Bis true when at least one of the statements A or Bis true Predicate whose clauses may be be modified during execution Context in which an error is handled Programmable event In the event of user errors the system branches to the part of the program defined to
53. consult 1 and enter the following clauses in order to define these incestuous relationships user consult user User s Guide 20 IF Prolog V5 3 Sample session Extending database in dialog incest X Y parentS X Y _ sibling X Y incest X Y parentS X Y _ father X Y incest X Y parentS X Y _ mother Y X end_of_file xxx consult user loaded in 0 03 sec Y y vyv vv yes The prompt means that the input of a term has not yet been completed If you make a syntax error when typing your input after the gt prompt an error message will immediately appear on the screen You should then re enter the clause in the correct form Next request a listing of your database user listing The output produced by the predicate listing 0 differs in respect of the order of the predicates and facts from that in your file h user parent 2 xxx parent X Y father X Y parent X Y mother X Y xxx user ancestor 2 xxx ancestor X Y parent X Y ancestor X Y parent X Z ancestor Z Y Y user incest 2 xxx incest X Y parentS X Y _ sibling X Y incest X Y parentS X Y _ father X Y IF Prolog V5 3 21 User s Guide Extending database in dialog Sample session incest X Y parentS X Y _ mother X Y h x user parentS 3 xxx parentS uranus gaia rhea parentS uranus gaia cronus
54. database system Using the passive C interface you can call Prolog predicates from a C program just like C functions and if necessary initiate backtracking It is thus a flexible and easy to use facility for combining C functions and Prolog predicates and might be used in contexts where e C programs can use the Prolog database like a relational database e C programs can access Prolog programs such as expert systems existing C programs can be extended by means of rapid prototyping with Prolog pred icates applications which are difficult to program in C such as those requiring backtracking can be written in Prolog and then integrated into a C program 195 Overview Active C interface You can also call functions of the passive C interface recursively and mix them with functions of the active C interface The first section of this chapter provides an overview of the active C interface It explains how you can write simple C functions i e functions which do not allow backtracking Predicates allowing backtracking can be implemented by means of complex C functions which are described in section 10 1 2 The section 10 2 gives you a short overview of the passive C interface with tables summa rizing the data types macros and functions of this interface Then you will find a detailed description of how to call a Prolog predicate by means of a C function Examples are also provided The IF Prolog Reference Manual 1 contains des
55. fact or a rule in Prolog is a clause For a detailed explanation of how to form syntactically correct Prolog clauses refer to chapter 3 In this first sample session with Prolog you will learn how to read valid Prolog clauses into the database and how to delete them from the database In other words you will know how to modify the database to suit your own requirements This can be done with or without an editor e You can work with an editor by using the built in predicate edit 1 Under UNIX edit 1 calls the vi editor by default and opens the file which you name The following User s Guide 10 IF Prolog V5 3 Sample session Prolog example sections refer specifically to the use of this editor If you want to use some other editor e g emacs then you can select it by calling the predicate user default_editor _ emacs Your preferred editor must of course already be installed on your system Any subse quent calls of edit 0 1 will then execute this editor as long as Prolog is active You can use the environment variable EDITOR to preselect a different editor on operating system level When you exit the editor the predicate edit 0 1 returns you to Prolog level and after asking for confirmation reads your new or modified file into the database e You can modify the database directly on Prolog level without using an editor The predicate assertz 1 2 inserts an individual clause into the database and the
56. fail The debugger stops at the branch destination regardless of whether the port is activated If a branch destination does not exist an error message is issued Branch commands allow you to e check execution steps retrospectively by branching back to a preceding execution point e determine the behavior of subgoals for test purposes using cross branches to specify how a box is to be exited The debugger command view_ancestors is of interest in connection with branch commands as it allows you to display the dynamic ancestors 9 4 Debugging with the alphanumeric user interface The alphanumeric user interface is a simple interface which enables you to debug Prolog programs even if you have a terminal without graphics capability If you have a OSF Motif interface for your Prolog system you can also debug your Prolog programs under the OSF Motif graphical user interface For further information see the next section Unless you have redefined the input output streams the debugger reads the inputs from the keyboard keyboard and sends outputs to the screen screen For information on redefining streams refer to chapter 5 User s Guide 174 IF Prolog V5 3 Debugging Alpha user interface If you call the debugger for the predicate ancestor 2 the following screen is displayed Current output field A A ENE a A ER A a ES NERE LEE FO fe ee NE SE RE ESS ERE ER DS V call ancestor X pepsi user AND BOX Status
57. failbody exit is reached during backtracking Once the trymatch entrance has been reached the mark is set to the clause box just entered and the previous box is deleted Searching for the next clause 3 5 5 Proving subgoals The ANDed conditions of the clause body are understood to be subgoals which in turn must be proved in the order of their appearance Any previously performed variable instantiations are taken into account Representation in the box model During execution subgoal boxes are entered and left by the following paths e From the enterbody port the first subgoal box is created and entered through its call entrance e At the exit port of a subgoal box the next subgoal box is created provided another subgoal follows in the clause Prolog then goes to the call port of the new box IF Prolog V5 3 43 User s Guide Procedure execution Database and procedure execution e Prolog goes from the exit port of the final subgoal box in a clause box to the exitbody exit of the clause box If the clause box happens to be empty i e all subgoal boxes were destroyed in the course of this path see section Proving a goal then it too is destroyed Subgoals 3 5 6 Proving a goal Once all ANDed subgoals of a goal have been proved the goal itself is considered to be
58. field for AND BOX alphanumeric user interface 177 field for breakpoints alphanumeric user interface 178 field for database alphanumeric user interface 179 field for old goal and clause boxes alphanumeric user interface 178 field for OR BOX alphanumeric user interface 178 field for settings alphanumeric user interface 178 file compile 141 consult 90 device 79 edit 87 load 90 144 load into database 141 load operation 146 logical 118 modify 13 physical 118 read into database 90 filename 88 flag 70 framework with epilog 215 framework without epilog 214 functor 15 functor of a predicate 30 generate a new Prolog system 238 context for signal handling 158 generation of data objects 200 generation of Prolog objects 200 global control of signal handling 156 memory area 73 global data stack 63 global stack size 69 used 69 global variable module object 120 visibility 120 goal 44 execute 223 parse 223 redo 47 store 224 goal box debugger 177 grammar context free 101 grammar rules compilation 104 graphical development environment start 65 gsize 69 gused 69 help field alphanumeric user interface 177 IF THEN operation 28 implementation part module body 133 import IF Prolog V5 3 301 User s Guide Index module 115 predicate 115 directive 133 module constraints 78 module informix 78 module motif 77 predicate 133 incl
59. file contains syntactically invalid clauses you will have to re edit it see page 16 to correct the errors Interpreting clauses You can interpret the Prolog clauses you have entered as follows User s Guide 12 IF Prolog V5 3 Sample session Prolog example Prolog fact Our interpretation mother gaia uranus Gaia is the mother of Uranus parentS uranus gaia rhea Uranus and Gaia are the parents of Rhea female artemis Artemis is female Rules allow you to derive further information from the information already in the database If you wish to represent a mother child relationship using the existing information there are two possibilities 1 As the example family tree is so small you could write all mother child relationships represented in it into the database as facts 2 You could deduce the mother child relationship from the parent facts The second method is both neater and more efficient because your database will not always be so small The following rule formulates the mother child relationship mother X Y parentS _ X Y This rule states that X is the mother of Y if there is a clause parentS _ X Y The first argument in the parentS clause in this interpretation the father is of no significance for the rule This is indicated by the inclusion of the anonymous variable _ at this position Possible errors If a clause in your gods pro file contains a syntax error the clause will not be read i
60. functions TermIs set the error context im plicitly in the event of an error 10 1 1 Linking simple C functions Simple C functions are those C functions which when used as a predicate do not initiate backtracking First the general procedure for including a simple C function in Prolog is explained step by step The procedure is then applied to an example The various built in C functions used in the example are described in detail in the IF Prolog Reference Manual 1 e Programming a function First write the C function c_function and store the source text for the function in a file file c e Write the include statement include cpred h as the first line of file file c The include file cpred h contains macros and type defini tions required for incorporating the C function e After this include statement insert a Cboot function to your file file c based on the following model void Cboot void CPRIM functor arity c_function In this way you set up a link between Prolog and the c_function and assign the predicate with functor and arity to the C function IF Prolog V5 3 201 User s Guide Simple C functions Active C interface i You may define only one Cboot function For each simple C func tion which you wish to incorporate Cboot must include precisely one CPRIM or MCPRIM call with appropriate arguments e Passing parameters between C and Prolog The func
61. goal or clause box You select the field for old goal or clause boxes with the commands lt lt lt gt gt gt which simultaneously also enable you to scroll in this field User s Guide 178 IF Prolog V5 3 Debugging Alpha user interface Field for ancestor goals In this field the debugger displays the dynamic ancestor goals for the current goal You select the field for ancestor goals with the command view ancestors or va Field for database This field allows you to view a section in the database The clauses for each predicate and the subgoals for each clause are consecutively numbered so as to enable you to specify the numbers for setting explicit breakpoints You select the field for the database with the command view predicate or view module or their short forms v and vm Constraint field for variables The command view variable or vv enables you to display all the defined constraints for a variable in the constraints field Constraint field for subgoals If the next subgoal in the proof is a constraint goal this is announced with CONSTRAINT PENDING in the status indicator You can use the command view constraints or vc to display in this field the position in the program at which processing is to continue If no constraint goal is pending processing an error message will be issued following the command view constraints Trace field If you have not defined a separate output predicate trace output 4 for
62. gt pra A E A A 281 11 10 4 How to create IF Prolog GUI Application Prolog sources only 281 11 10 5 How to create IF Prolog GUI Application Prolog and C C sources dialog based scc ek ne corra ie A Aa 281 11 10 6 How to create IF Prolog GUI Application Prolog and C C sources MFC window based cccas lt icrrirnss rta ara 281 A Preparing Prolog applications for marketing 283 B ASCII table 287 C Glossary 291 Bibliography 295 Index 296 Chapter 1 Preface The IF Prolog system from Siemens AG Austria is an implementation of the ISO Prolog standard ISO International Standardization Organization This standard was prepared by ISO Working Group 17 comprising representatives from various national standardization bodies IF Prolog also contains interfaces and predicates which extend the language and ensure compatibility with earlier versions of the product The Standard dictates us to supply a strictly conforming mode where the Prolog system only accepts and supplies conforming language predciates To invoke this mode see the section on invoking IF Prolog in the User s Guide Target group This IF Prolog User s Guide is intended for anyone wishing to develop Prolog programs for a system running under the UNIX Windows and OS 2 operating systems As the manuals for IF Prolog are not tutorials you should have some basic knowledge of the Prolog language In addition you should be familiar with the basics of the op
63. handle this ex ception If the exception is not caught the system issues an error message Making a predicate visible to and thus usable by other modules A term consisting of numbers and compound terms with evalu able functors Combination of terms using operators Fact concerning the properties and names of objects A fact describes a clause whose body is true see Prolog flag The name of a predicate or structure Variable whose scope is not local to a clause and whose value is not lost during backtracking Term whose proof is attempted with depth first searching back tracking and unification A ground term contains no variables Conclusion lefthand part of a clause implementation part of a module Contains predicate and operator definitions import infix notation Making a predicate from another module visible and thus usable Structure notation where the operator is located between the operands e g A B instantiated variable Variable which is instantiated with a constant or a compound instantiation list literal load local variable logical variable metapredicate term Substitution of a variable by a non variable A Prolog term either the empty list or a compound term whose functor is and which has two arguments the second of which is a list Atom or structure Reading compiled modules into the database Variable which is visible in only one clause A term which can
64. however that to do so you must call the Prolog system with the debug option 3 2 1 Compiled predicates IF Prolog offers a number of ways of compiling user defined predicates and including them in the database This can be done by the system s incremental compiler by using the assert and consult predicates In addition predicate definitions stored in files can be compiled by the predicate compile 1 or the external compiler procmp command Predicates compiled with consult 1 reconsult 1 compile 1 or procmp are normally no longer modifiable However you can inform the compiler with the dynamic 1 directive that certain predicates are to remain modifiable Predicates which you have generated with assert predicates or which you have declared as modifiable with other compilation methods are compiled only to the extent where the compilation is reversible i e where decompilation is possible Whenever references are made in this manual to compiled predicates they should be taken to mean predicates which cannot be decompiled These can no longer be modified by simply adding or removing clauses Further information about compiling Prolog source text and the related advantages and restrictions can be found in chapter 7 5 on Compiling and loading files IF Prolog V5 3 31 User s Guide Predicates Database and procedure execution C interface IF Prolog also gives you the option of using C functions without backtracking simple C functio
65. in the list are declared as private The private declaration of a predicate prevents the private definition of a predicate from being overlaid by an import directive The private directive thus also allows you to redefine built in system predicates The directives export dynamic and meta implicitly declare a predicate as private If you reexport a predicate in the module interface then you may not declare it as private Example In this example the system predicates assertz 1 and retract 1 are redefined locally This enables important actions of this module to be logged without having to change the actual implementation of the module The system predicates assertz 1 and retract 1 in the module database are made invisible by the private declaration You can however continue to call them by means of an explicit qualification module database export db_assert 1 db_retract 1 db_access 1 end_module database begin module database dynamic database 1 private lassertz 1 retract 1 assertz Tuple User s Guide 136 IF Prolog V5 3 Module body Declaring metapredicates writeq insert Tuple nl system assertz Tuple retract Tuple system retract Tuple writeq delete Tuple nl db assert Tuple assertz database Tuple db_retract Tuple retract database Tuple db_access Tuple database Tuple end module database 7 4 5 Declaring predicates as dyn
66. interface which govern how a complex C function is implemented as a predicate with backtracking A presentation of various macros which make it easier for you to incorporate certain coroutine operations correctly into your C function is also included These macros enable frameworks to be built for two types of complex C functions namely e complex C functions without epilog e complex C functions with epilog In an epilog you can define actions to be taken when an exit from a predicate is made through the fail port A description of these frameworks and how they are used to program complex C functions is given on page 215 in each case their use is illustrated by means of an example The box model and coroutines A Prolog predicate can be represented as a box with four ports see also chapter 3 The following scenario is typical for a predicate with backtracking IF Prolog V5 3 205 User s Guide Complex C functions Active C interface e The box is exited via the exit port and is not removed All internal information within the box remains intact e The box is reentered via the redo port The search for a solution is resumed from the point at which it was previously interrupted This sequence may be repeated several times The box model itself can be simulated by a coroutine coroutine is a subprogram with the following special characteristics e it can be exited at a specific point without its internal information being los
67. is addressed by means of the predicate 2 user f a b f X no The two terms are not unifiable because their arities do not match user X a Y X lt X a Y a yes 3 5 Procedure execution Effective programming in Prolog requires not only that the problem has been logically cor rectly structured but also that you take into account the procedure execution defined by the Prolog search technique This is essential whenever predicates with a clearly procedural character are used To allow you to maintain an overview of the actual state of goal execution even during backtracking or when unpredicted results occur it is helpful to illustrate the situation by means of a model Below we introduce an extended box model as one means of providing IF Prolog V5 3 37 User s Guide Procedure execution Database and procedure execution an especially detailed simulation of goal execution This model contains in addition to the ports call exit redo and fail of the standard box model nine additional ports as a refinement thereby permitting a more accurate modeling of goal execution This model is referred to below as the box model TRYMATCH ENTERBODY CALL EXIT EXITBODY FAIL REDO FAILMATCH REDOBODY ERROR FAILBODY Box Model The execution of goal can be simulated on this box model as a path through the ports of boxes during which further boxes can
68. item is text you can select a part of it and copy it to the clipboard 11 7 Options Dialog Options Dialog is a tool that allows to modify IF Prolog configuration or set properties of IF Prolog system It includes several panes property pages General General property page includes general settings default editor console font and console sizes startup parameters and current configuration of the startup parameters Prolog Flags Prolog flags configuration Preprocessor Options Prolog preprocessor configuration Prolog preprocessor can be configured to preprocess files before consulting or compiling Configuration IF Prolog configuration Modify stack sizes memory parameters Cross Reference Options Cross Reference tool configuration User s Guide 258 IF Prolog V5 3 Graphical User Interface MS Windows 11 7 1 General Options This is the General property page IF Prolog 5 2 Options Fre pe CCAA OEA Figure General property page General property page includes general settings default editor console font and console sizes startup parameters and current configuration of the startup parameters Option Description Default editor Default editor is used by predicate edit 0 1 or by menu button commands that edit a file It can be set by typing a new filename or by selecting a new file in file dialog available on Browse button click Start in debug mode Starts IF Prolog
69. main built in predicates concerned It then explains how to work with files pipes and sockets How to add device drivers to the Prolog system is explained in section 10 3 5 1 Input output in the Prolog top level dialog Top level dialog Input output in the top level dialog is handled by the Prolog system using the two standard streams The Prolog system expects queries to be entered on standard input usually the keyboard Ifthe goal is successfully proved the system outputs the variable instantiations 79 Elementary input output Input output interface and after entry of the response yes on standard output usually the screen If the goal cannot be proved the system outputs the response no In the case of backtracking the Prolog system waits for a request from standard input Error messages Error messages issued by the Prolog system are directed to the current error output stream In trace mode the Prolog system also writes the trace listing to the current error output The predicate set_error 1 can be used to make a stream the current error output stream see page 82 Any error messages from the Prolog system will then be output to this stream This can be useful for example for detailed study of a trace listing or when a large number of error messages are to be processed such as when a newly created file is consulted It also speeds up the time considerably as standard error output is character buffered
70. manipulated with built in predicates The built in predicates for event handling allow flexible configuration of interactive user interfaces For a description of this interface refer to 3 If you wish to use the predicates of the OSF Motif interface in modules you must first import the module motif begin_module surface import motif If you wish to work with the predicates of the OSF Motif interface in the top level dialog module user you can import the module motif as follows IF Prolog V5 3 77 User s Guide Interfaces Prolog system user user gt import motif gt end of file xxx consult user loaded in 0 01 sec yes Interface to Informix If the Prolog system has been configured with the Informix interface an interface to the Informix relational database system is available For an explanation of how to work with this interface refer to 4 If you wish to use the predicates of the Informix interface in modules you must first import the module informix see also chapter 7 begin_module surface import informix If you wish to work with the predicates of the Informix interface in the top level dialog module user you can import the module informix as follows user user gt import informix gt end of file xxx consult user loaded in 0 01 sec
71. naturally comply with the naming conventions of the operating system You can compile any syntactically correct Prolog clause i All clauses of a predicate to be compiled must be stored in a single logical file unless it was declared with the multifile directive You can only perform compilation with reference to a module any predicates which are compiled without specifying a module will automatically be assigned to the module user You can also compile a file containing for example only module bodies However the corresponding module interfaces must then be known to the Prolog system for example as a result of a previous compilation Prolog then places the relevant complete module in the file which has been created A Prolog text file can also contain all the syntax elements directives required for the definition and declaration of modules Compiling a Prolog source file There are two ways of compiling a Prolog source file Filename e Compilation at the operating system shell level by calling the compiler procmp Filename Following a successful compilation the Prolog compiler automatically activates the optimizer see also proopt in the F Prolog Reference Manual 1 At the time of compilation the following module interfaces should be known to the Prolog compiler interfaces of the system modules from which you import predicates into the mod ules that are defined in Filename interfaces of other modules from
72. paren thesized expression in single quotes Prolog regards the first period as the end of the query and responds with an error message such as user write hello User s Guide 8 IF Prolog V5 3 Sample session Prolog example xxx syntax error operator expected gt gt gt line column In this example and the ones below the question mark stands for the line and the column number in which the error occurred in the sequence of the inputs The correct input with single quotes is user write hello hello yes user 4 The Prolog system could also respond with the following error message if for example you attempt to modify your input using the cursor keys xxx syntax error illegal character gt gt gt line column user You can correct input only in the current line by using the Backspace key 5 If all else fails for example if you get no response with any of the usual keys try hitting the Interrupt key per key or the key combination crrx c This causes processing of the current query to be interrupted and a message to be displayed followed by the Prolog dialog prompt user user repeat fail DEL xxx EXCEPTION system error gt gt gt error interrupt gt gt gt signal interrupt gt gt gt goal fail no user 2 3 A short Prol
73. predicate retract 1 2 deletes an individual clause from the database This allows you to modify and test a program without switching between Prolog and the editor program and having to store the data in a file 2 3 1 Entering data with an editor After your first experiments with a Prolog query you are still on the top level of Prolog and will see the Prolog prompt user on your screen If you have quit Prolog in the meantime and do not get the prompt call Prolog again Using the built in predicate edit 1 you can now open a file with the name gods pro and type in a few facts and rules user edit gods You do not need to explicitly specify the suffix pro for the file name but you can if you wish If you do not supply it it is added automatically by the Prolog system If you specify pro it must be enclosed in single quotes as it contains a period If the file does not yet exist it is created automatically in the current directory The selected editor is now called and you can enter the following facts and rules FACTS mother gaia uranus mother X Y parentS _ X Y parentS uranus gaia rhea parentS cronus rhea zeus parentS cronus rhea hera parentS cronus rhea demeter IF Prolog V5 3 11 User s Guide Prolog example Sample session parentS zeus leto artemis parentS zeus leto apollo parentS demeter zeus persephone female artemis female persephone female
74. predicate call Query the module argument of meta predicates Query current module Query modules Store current module in a file Set current module Remove module from the database User s Guide 116 IF Prolog V5 3 Module concept Modules This chapter describes the syntax and structure of modules and objects which you can manage in modules You will learn how to import and export predicates compile a module and process the module in the database 7 1 Modules A module comprises two parts the module interface and the module body which may be both logically and physically separate The module interface and module body are interlinked by their common module name The module concept is static i e the module interface is not created until the module is loaded You can dynamically modify only the module body 7 1 1 Directives Modules are defined by syntactic constructs known as directives A directive is a single arity structure and has the functor where is a built in operator The directives are evaluated by the following built in predicates which are used for loading Prolog programs consult 1 reconsult 1 compile 1 2 Outside the context of these predicates directives have no effect The following table provides an overview of the directives available IF Prolog V5 3 117 User s Guide Modules Module concept Directive Effect begin module Module Define the body of a module
75. predicate which when called displays the integers between Min and Max on the current output When global variables are used the predicates are specified as alternatives following the comment character count Min Max Min gt Max IF Prolog V5 3 57 User s Guide Procedural predicates Database and procedure execution count Min Max asserta number Min set global number Min repeat retract number Number get global number Number write Number nl Y is Number 1 asserta number Y set global number Y Number Max e retract number Y unset_global number The first clause prevents the program from going into an endless loop You can also create loop like execution structures using recursive predicates Repeat fail loops require less memory than recursions unless you are able to put the recursive predicate into tail recursive form Tail recursion i e the recursive call is located at the end of the clause is helpful because the Prolog system generates particularly efficient code for these clauses With recursive constructions too you must pay attention to the way you formulate the tail condition so that you do not produce an endless recursion Example The function of the predicate count 2 given above can also be performed by an tail recursive predicate count_rec 2 with the following clauses count rec Min Max Min gt Max count rec Min Max wri
76. print 1 2 write_term 2 3 Metapredicates which manage global variables IF Prolog V5 3 121 User s Guide Metapredicates Module objects current global 1 get_global 2 pop_global 2 push_global 2 set_global 2 unset_global 1 The language construct call 1 has an important role to play in implementing metapredi cates It has a module argument which is handled by Prolog in the same way as in metapred icates If the module argument is not set explicitly it is assigned the name of the module in which the language construct is used As its argument cal1 1 has a goal which can be statically or even dynamically known The module argument determines the module in which this goal is to be executed Example This example shows the implementation of a small debugger module test help meta 1 begin module test help op 900 fx Goal calling_context Module writeq call Goal nl call Goal Module writeq exit Goal nl Goal writeq fail Goal nl fail end module test help You can trace goal by placing the operator in front of the goal is defined as an operator at load time using the op directive It can thus also be used by other modules import test help predicate goall goal2 goal3 goaln User s Guide 122 IF Prolog V5 3 Module objects Metapredicates The predicate 1 has been declared as a metapredicate so its module argument is im
77. proved Representation in the box model Prolog can only reach the exit exit of a goal box from the exitbody exit of one of its clause boxes If a goal box is left through the exit port and it contains no further clause boxes 1t is destroyed Success User s Guide 44 IF Prolog V5 3 Database and procedure execution Procedure execution 3 5 7 Failing to prove a goal A goal fails to be proven when there is only one more clause available for the attempt to prove it and e the clause head cannot be unified with the goal term or e one or more of the corresponding subgoals cannot be proved All variable instantiations made while executing this goal are undone Representation in the box model Each of the two paths to the fail exit of a goal box originate from its final clause box e the first stems from the failmatch exit of the clause box e the second stems from the failbody exit Any goal box left by its fail exit is destroyed Failure Example The goal e g 1 p cannot be proven in the database containing the predicate e 3 with the clauses e a l p true e p e h true e g e h true IF Prolog V5 3 45 User s Guide Procedure execution Database and procedure execution because there is no unifiable clause head The goal e p X Y can be proven with the second clause because the associated clause body is always true
78. qualify implicitly 123 reexport 130 reexport selectively 131 selective import 134 trace 164 visibility 120 predicate indicator 120 predicates of a module import all visible 133 private directive 136 procedure execution 37 process management 91 User s Guide 304 IF Prolog V5 3 Index production rule 101 production rules syntax 112 program executable 144 program pointer 207 program 0 38 Prolog exit 6 terminate 224 Prolog application distribution 283 Prolog dialog 19 Prolog kernel system 283 Prolog objects generate 200 Prolog prompt 11 Prolog query 6 Prolog runtime stack size 63 Prolog source code 145 Prolog source file consult 65 Prolog system call 62 environment variables 74 generate an extended 238 initialize 223 interfaces 76 organization 61 Prolog text 90 compile 141 prompt request 64 PRORC environment variable 75 PROROOT 61 environment variable 75 qualify predicate explicitly 123 predicate implicitly 123 query 38 current module 116 current stream 82 modules in database 115 Prolog flag 70 signal mode 155 query environment variable 75 query processing 25 quit signal 155 raise exception 152 read in a file during a Prolog session 90 read in a file when calling Prolog 90 read into the database 12 reconsult a file 16 recursion 58 redo goal 47 reexport all predicates 130 specific predicates 131 reexport 1 directiv
79. syntactic structure of the phrase start If there is the goal succeeds and Tail is unified with that part of InputList which no longer belongs to this token sequence A similar process takes place internally when the subgoals belonging to this goal are proved The list tail remaining in a subgoal then forms the input list for the following subgoal or in the case of the final subgoal it forms the list tail for the entire goal Example The Prolog system has compiled the grammar given in the first example and read it into the database We now ask user sentence the dog barks the dog S_Tail S Tail the dog yes The answer shows that in the input list the sublist the dog barks is made up of the phrase sentence and the tail the dog is left over When this goal is proved the first thing that happens is that the subgoal nominal_phrase the dog barks the dog N_Tail is activated Since the dog is a valid nominal_phrase the goal succeeds and N_Tail is unified with barks the dog This forms the input list for the second subgoal verbal_phrase barks the dog S_Tail Thus for the entire goal S_Tail the dog remains as the list tail 6 3 Additional arguments In addition to the above Prolog structures of any arity are permitted for non terminal symbols in a rule of grammar This does not however apply to lists since these are already used for the terminal symbols During compilation t
80. the client this should be specified Since several servers for several displays can be started on one host the display number should be specified Numbering starts with 0 If you are unsure check with your system adminis trator for displaynumber or screennumber For further information refer to the IF Prolog OSF Motif Interface manual 3 Environment variable EDITOR defines which editor is used for editing files Default operating system dependent Environment variable PROHELP defmes the path under which the help files for Prolog are stored Default S PROROOT prolib Environment variable PRORC defines the path of the file which is opened when the Prolog system is called and the st option is not set If you have not set PRORC and have not specified the st option when calling Prolog the Prolog system attempts to open the file prorc in your HOME directory Environment variable PROROOT defines the path under which the system files are stored by Prolog The pathnames may differ on different operating systems Should the path specified here not be applicable to your system check in the release notice Default operating system dependent e g usr lib prolog or opt lib prolog Environment variable SHELL defines the command interpreter which is called if you temporarily exit the Prolog system In UNIX this environment variable is normally already preset to the value contained in the file etc passwd 4 4 Interfaces of the
81. the components context pc_term and context pc_varlist The function QueryCreate creates a passive goal which is executable but should not be called at the moment You can execute only active goals You create an active goal by calling the function PrologOpen cursor PrologOpen SystemModule context pc_term context pc_varlist You make an active goal from a passive goal by using the function QueryOpen A current goal is the most recently created active goal The functions PrologOpen and QueryOpen both store information about the active goal in the structure cursor of type t_cursor The function PrologFetch accesses this structure e Proving a Prolog goal backtracking Calling PrologFetch cursor starts or restarts the proof for a goal The first time that PrologFetch is called a search is made for the first solution for the goal each subsequent call searches for the next solution If after successful proof of a goal the first solution is to be found again and if further solutions exist then the structure t_cursor must be recreated by calling PrologOpen or QueryOpen again before calling PrologFetch IF Prolog V5 3 223 User s Guide Application Passive C interface Proof of a goal also restarts with the search for the first solution if since the last proof ParseProlog has been called or PrologFetch has been called with a different cursor If the search for a solution is successful the retur
82. the context of the calling module If a predicate with the indicator portray 1 is visible in the calling module it will be called in order to output the term If portray 1 cannot be successfully executed the term will be output with writeq 1 in the context of the calling module User s Guide 128 IF Prolog V5 3 Module interface module output export portray 1 meta output 1 begin module output output Term calling_context Module call portray Term Module output Term writeq Term portray _ fail end module output In the following the module persons is defined this module exports the predicate collects 2 In addition a predicate portray 1 is defined which outputs the structure of collects 2 in a special way module persons export collects 2 begin_module persons collects bill stamps pictures collects jane butterflies portray collects Person write_formatted q collects nothing n Person portray collects Person Things write_formatted q collects Person show_collection Things show_collection Thing 2 write_formatted q n Thing show_collection Thing Tail write_formatted q and Thing show_collection Tail end_module persons The body of the module operation is also defined It imports the modules output and IF Prolog V5 3 129 User s Guide Module i
83. the relevant file are all available for proof purposes Note that execution always takes place after the file has been loaded irrespective of the position of the initialization directives in the original file User s Guide 38 IF Prolog V5 3 Database and procedure execution Procedure execution e program 0 When the Prolog system is started a check is carried out after the command line arguments have been processed i e in particular after the files specified in the com mand line have been loaded or consulted to see whether there is a definition of the predicate program 0 in the database in the module user If so instead of starting the normal Prolog input loop break 0 the goal user program is executed After this operation the Prolog system terminates Thus it is possible to implement independent applications i Output of variable instantiations interactive backtracking capability and output of the proof result in the form yes or no is a property of the Prolog input loop break 0 and therefore takes place only in the event of queries 3 5 2 Calling a goal When you call a goal with several subgoals Subgoal Subgoal the Prolog system first tries to prove the first subgoal To do this it searches the database for the first clause of the matching predicate i e the first clause which has the same clause name and arity as the term Subgoal_1 If more than one clause belongs to this predicate the system wil
84. this dialog Cancel Cancels the dialog and no breakpoint is set Help Displays this help page See Also Debugger Commands Debugger Toolbar Debugger Information Toolbar Console Window User s Guide 272 IF Prolog V5 3 Graphical User Interface MS Windows 11 9 Linking Custom IF Prolog Applications IF Prolog custom applications can be linked also with GUI There are two versions of GUI e Runtime User Interface e Development User Interface Each GUI version consists of several files They include main application loop resources menus toolbars main console window To create a custom linked application you do not need to call InitProlog to iniitialize Prolog You only define your C predicates with your code and declare them in Cboot function You can define also Cshutdown function to call custom functions during the Prolog shutdown 11 9 1 Runtime User Interface This version of the graphical user interface is intended to be used for end user IF Prolog applications It includes simple console window simple toolbar and menu This is the Runtime User Interface main window FE IF Prolog V5 2 Console Window Of x File Edit View Options Help e Es IF PROLOG U5 2 WIN32 Copyright C Siemens AG Austria 1996 98 Serial Xb 526666661 licensed for Daniel Banas PC5156 1018 SWH Bratislava user For Help press F1 Figure Runtime User Interface main window IF Prolog V5 3 273 User s Gu
85. trace or have not redirected the output stream the trace outputs are displayed in a separate trace field You can change the size of this field with the command set trace length IF Prolog V5 3 179 User s Guide Alpha user interface Debugging You can select the trace field with the command view trace or its short form vt Example If you call the debugger for the goal ancestor X pepsi from the example on page 166 you will get the following screen user debug ancestor X pepsi debug CALL gt call ancestor X pepsi user Command v ancestor 2 lt ENTER gt User s Guide 180 IF Prolog V5 3 Debugging Alpha user interface The AND BOX is displayed in the current output field BEGIN signals that the debugger is situated at the first outer port of the box model Then enter the command v ancestor 2 in the command line 1 ancestor X Y 1 father X Y 2 ancestor X Y 1 mother X Y 3 ancestor X Y 1 father X Z 2 ancestor Z Y 4 ancestor X Y 1 mother X Z 2 ancestor Z Y IF Prolog V5 3 181 User s Guide Alpha user interface Debugging The field for the database is output in the output window which displays that section of the database which defines the predicate ancestor 2 You now use the following command to define an interactive breakpoint and then have the field for breakpoints displayed in the output window stop interactive user
86. we have a query consisting of three subgoals These are processed sequentially from left to right tell 1 redefines the current output stream Subsequent outputs will now be written to the specified file This is why the output from the listing 0 predicate does not appear on the screen but is written directly to the file det pro The predicate told 0 closes the current output file again so that any further output will be directed to the screen The suffix pro is in this case not automatically added to the file name However you should identify all files which contain Prolog clauses with this suffix IF Prolog V5 3 25 User s Guide Storing clauses in file Sample session User s Guide 26 IF Prolog V5 3 Chapter 3 Database and procedure execution This chapter opens with a brief overview of the elements and structure of a Prolog database and continues with a step by step description of the principles underlying procedure execu tion in Prolog The basic features of an extended box model for Prolog predicates are also introduced and explained This extended box model forms the basis of the Prolog debugger described in greater detail in the chapter 9 on Debugging in this manual The following description is not intended as a tutorial on the Prolog language but because of the descriptive nature of Prolog even a Prolog novice will be able to understand the simpler examples and thereby the underlying syntax and methodology of th
87. which you import predicates to the modules that are defined in Filename and which are not defined in Filename e Compilation during a Prolog session with the predicate compile 1 user compile Filename User s Guide 142 IF Prolog V5 3 Module concept Compiling files Filename is the name of the Prolog source file The result of the compilation is con tained in Filename cmp If any suffix pro is present in Filename it will be removed In contrast to compilation using procmp the optimizer is not automatically activated However you can optimize the file manually with the proopt command At the time of compilation the following module interfaces must already be loaded interfaces of the system modules from which you import predicates into the mod ules that are defined in Filename interfaces of the modules from which you import predicates to the modules that are defined in Filename and which are not defined in Filename Possible errors The system reports the following errors or exceptions e syntax errors in the Prolog source file e read errors the file to be compiled does not exist the result file cannot be created no access permission e insufficient space available Restrictions on compiled predicates If a predicate is to be modified or is still in the development or debugging phase it is inadvisable to perform a compilation for the following reasons e Compiled predicates cann
88. window Prolog Breakpoints will contain the following entries Prolog Breakpoints am MEA ancestor 2 call ancestor 2 redo 1 ancestor 2 failmatch 1 1 ancestor X Y 3 1 father X 2 ancestor X 3 1 mother X Y 3 ancestor X Y 3 1 father x 2 2 ancestor Z Y 4 ancestor X 3 mother X Z 2 ancestor Z Y Predicate ancestor 2 with 3 interactive breakpoints User s Guide 188 IF Prolog V5 3 Debugging OSF Motif interface 9 5 4 The Tracer In the Trace menu you can globally turn on or off the trace or activate it for an individual subgoal If you wish to take a look at the trace output click on the menu item open This opens the window Prolog Trace In the Command menu for this window you can use the menu command configure to define the ports to be logged when they are passed through Trace Configuration call exit redo fail error suspend wakeup O trymatch O failmatch O redobody O failbody enterbody exitbody Configuring trace The menu command clear allows you to clear all the outputs displayed in the output field of the trace window The menu command quit closes the trace window Example The following diagram shows the trace outputs for the predicate ancestor X pepsi from the example on page 166 IF Prolog V5 3 189 User s Guide OSF Motif interface Debugging Command call mother gregor p
89. with debug flag set to on This means that all predicates which you read into the database with consult 1 reconsult 1 or assert 1 2 are implicitly de clared as dynamic thereby supporting the debugging of a program Start with ISO Starts IF Prolog with ISO flag Only the facilities re quired to ISO Prolog are available Start without banner Starts IF Prolog without a banner window showing a license information IF Prolog V5 3 259 User s Guide MS Windows Graphical User Interface Option Description Start without copyright Start with consult flag set to nonotify Start with warnings flag set to off Start with package informa tion Current startup configuration Console font Y Console size X Console size Scrolling area Starts IF Prolog without a copyright information Starts IF Prolog with consult flag set to nonotify No messages are displayed on the screen when a file is loaded Starts IF Prolog with warnings flag set to off When consult 1 is called no warnings will be displayed on the screen Shows current IF Prolog package configuration Displays the current IF Prolog configuration IF Prolog will be started with this configuration next time This configuration contains also parameters set in the Config uration property page Configuration is saved into the Registry Configuration parameters are appended at the end of the parameters set by the user at the comma
90. you should always call the Prolog system with the debug option 2 3 3 Asking questions You can now ask Prolog questions about the portion of the Greek gods family tree rep resented in the database Your first question or query is whether gaia is the mother of uranus In Prolog you ask this question in the following form user mother gaia uranus Prolog finds the fact in the database and answers yes user However if you ask whether gaia is the mother of zeus Prolog will answer no user mother gaia zeus no user Prolog can do more than answer questions with yes or no You can also ask a question such as Which members of the divine family are female user female Goddess User s Guide 14 IF Prolog V5 3 Sample session Modifying database with editor Goddess is a variable because the string starts with an uppercase letter The name you give to the variable is not important you could for example equally well write female X Prolog now searches the database for the first clause with the functor female and arity 1 in order to find an instantiation of the variable Goddess The first matching clause in the database is the fact female artemis You therefore receive the following answer to your question Goddess artemis You can now choose to continue the search for further names or end the search if you are satisfied wit
91. 3 271 User s Guide MS Windows Graphical User Interface Set Breakpoint dialog allows to set a new breakpoint This dialog can be invoked from the Breakpoint dialog When Breakpoint dialog contains at least on breakpoint Set Breakpoint dialog fields are initialized with parameters of the selected breakpoint You can modify it and set similar breakpoint or delete all fields and create new one The table describes fields and buttons of the dialog Field or Button Description Regular Variable radio buttons Regular sets a regular breakpoint To minitor a variable set Variable breakpoint type Interactivity check box Sets interactivity of the breakpoint Predicate field Predicate name and arity or also its module in the format name arity or module name arity Clause Number field Number of clause to set breakpoint for Conditions field A list of conditions on which an action is invoked Actions field A list of actions that are invoked on conditions Subgoal field Number of subgoal to set breakpoint for Only for regular breakpoints Ports field Port to set breakpoint for Only for regular breakpoints Monitored Variable field Name of the monitored variable to set breakpoint for Only for variable breakpoints Binding Type field Binding type to set breakpoint for bind modify Only for variable breakpoints Set as New Stop button Creates a new breakpoint with parameters specified in the fields of
92. 5 2a Release 2 01 This is the list of all IF Prolog C Interface VC 6 0 specific files e Resource DLL GUlres6 d11 GUlres6d d11 e Runtime GUI rtmGUI6 1ib rtmGUI6d lib rtmGUImain6 obj rtmGUImain6d obj e Development GUI devGUI6 1ib devGUI6d lib devGUImain6 obj devGUImain6d obj 11 9 4 Special GUI startup parameters These startup parameters are new to IF Prolog V5 2 They are included in Windows port only and apply to GUI IF Prolog applications only e wshow Causes the main IF Prolog is showed e g when it was minimalised on every write operation into this window The default behavior is that the IF Prolog main window is showed when waiting for the keyboard input IF Prolog V5 3 275 User s Guide MS Windows Graphical User Interface e wmin Minimizes main window on start up e nobanner Prevents from showing banner window on start up GUI startup parameters apply to both GUIs to Runtime and Development GUI 11 95 Demos You can get more information from demos These demos are linked with GUI libraries e Visual C WinApp e Visual C WinPred e Visual C EventDemo e Visual C Queens 11 10 Using the IF Prolog Application Wizard IF Prolog Application Wizard can create a skeleton and Visual C project for several IF Prolog application types e Console Application Prolog sources only e Console Application C C and Prolog sources e GUI Application Prolog sources only e GUI Application C C
93. 6 2 Database Tree Pane 2 c gt 24 bei acidi med demt Amiki 257 1163 Database Item Pang o 20 emassa asi 257 ILGA Menu s sa eer rare ee a o a A a Bee 258 11 7 Options Dialog s sosse ts sm eo Ow AA A 258 ILL General Options s c ser ee A A AA DS 259 lig Prolog Flags Options 1614444646444 ai i e ir s 261 117 3 Preprocessor Options seso de beca dis kee Re ee Dawes 264 11 7 4 Configuration Options 44 see doed paa pupae e a OS 266 11 7 5 Cross Reference Options oaoa a ee be eee 24 267 11 8 Debugger Support s sacas emoe sng iaoea i ew ee g 268 11 8 1 Debugger Toolbars s s s ere 24 ea dbeadb EP Oe ORE ee 269 11 8 2 Accelerator Keys roda a Ew bie o aa da 269 11 83 Breakpoint Dialog sera iras arta BH ered Bae ee we aot 270 11 84 Set Breakp int Dialog st e k sn a Ra e ads 271 11 9 Linking Custom IF Prolog Applications 2y ea cacc ss An 65 273 Ti Runtime User Interface oco si e doe ee RR Ree e 273 11 9 2 Development User Interface oaoa aaa eee eee eee 274 11 9 3 Release and Debug configurations o 274 11 9 4 Special GUI startup parameters o 275 ELA DONOS y a e a a a rs a Ge 276 11 10 Using the IF Prolog Application Wizard o 276 11 10 1 How to finish IF Prolog project creation 279 11 10 2 How to create IF Prolog Console Application Prolog sources only 280 11 10 3 How to create IF Prolog Console Application Prolog and C C MIE
94. 64 change current module 116 editor 89 character string device 79 charset language specific 62 Chinese language specific charset 62 choice point 38 classification of Prolog terms 198 clause 10 13 27 body 28 append 20 enter 19 clause box debugger 178 clauses delete 19 delete several 24 delete single 23 display on screen 14 enter individual 20 enter several 20 modify several 24 store 25 client 96 command line alphanumeric user interface 175 comment 12 comment line 19 compilation 141 advantages of 141 IF Prolog V5 3 297 User s Guide Index at operating system level 142 disadvantages of 141 during a Prolog session 142 excluding predicates 144 file 141 grammar rules 104 module 141 problems 143 Prolog text 141 source file 142 undefined predicates 144 compiled file 90 load 65 144 compiled predicate debug 143 compiled predicates 31 modifiability 143 compiled Prolog code 145 complex C functions 32 196 205 example 209 214 216 framework 210 macros 211 conjunction AND 28 constraint data stack 63 constraints import 78 memory area 73 constraints for subgoals alphanumeric user interface 179 constraints for variables alphanumeric user interface 179 consult file 90 context 147 actions 147 exception handling 150 153 for signal handling 158 nesting 147 context action context jump 147 exception 147 signal 147 context jump 148
95. 68 h 105 151 69 i 106 152 6A j 107 153 6B k 108 154 6C l 109 155 6D m 110 156 6E n 111 157 6F O 112 160 70 p 113 161 71 q 114 162 T2 r 115 163 73 S 116 164 74 t 117 165 75 u 118 166 76 v 119 167 77 w 120 170 78 X 121 171 79 y 122 172 7A Z 123 173 7B 124 174 7C 125 175 7D 126 176 TE 127 177 TF DEL Delete Interrupt Signal User s Guide 290 IF Prolog V5 3 Appendix C Glossary alias name argument arity atom backtracking body box model Atom associated with an open stream An object in a Prolog structure In the term mother gaia uranus for example gaia and uranus are the arguments A non negative integer representing the number of arguments in a structure A basic object incapable of further subdivision denoted by an identifier Proving method which in the event of a failed route in a blind alley returns to the last choice point in the proof tree and checks alternative paths Condition righthand part of a rule Model illustrating how Prolog proves a goal breadth first search Search strategy in proof trees which considers several paths built in predicate choice point clause compound term conjunction constant constraint consult context cut database simultaneously A predicate defined by the Prolog system Node in the proof tree at which multiple possibilities to satisfy or refute the goal exist Statement about objects and relations
96. 70 start file 66 example 68 start symbol 101 state structure 207 status indicator alphanumeric user interface 175 stream 79 80 alias name 79 current 80 82 default 82 explicitly specified 80 information 85 standard 79 STRING data type 198 string device 79 subgoal 25 43 subquery 25 suffix for file name 11 suppress messages 64 warnings 64 synonym alias name 82 syntax module body 132 notational conventions 2 syntax error 13 syntax of production rules 112 syntax_error User s Guide 306 IF Prolog V5 3 Index error type 151 system argument 62 system information 69 system modules 115 system predicate visibility 120 system size 69 system status load 66 system time 69 system error error type 151 t_cursor data type 219 t_module data type 219 t_parse_context data type 219 t_query data type 219 tail recursion 58 Taiwanese language specific charset 62 TERM data type 197 term access to components 199 analyze 199 classify 198 unification 200 terminal device 79 terminal symbol 101 terminate session 68 termination signal 155 test functions active C interface 201 time 69 token 101 top level 5 top level dialog error messages 80 input output 79 trace stop 167 turn on 165 trace field alphanumeric user interface 180 trace listing 165 trace 1 165 trail memory area 73 size 64 trail stack size 69 used 69 ts
97. Cross Reference Options property page in IF Prolog Options dialog IF Prolog V5 3 247 User s Guide MS Windows Graphical User Interface 11 2 7 Help Menu e User Interface Displays the information on user interface e IF Prolog Database Browser Displays the information about IF Prolog Database Browser Debugger Displays the information on debugger user interface Prolog Reference Displays on line predicate reference About Displays a short information about the IF Prolog application 11 3 Main Window Toolbars These toolbars are the IF Prolog Main Window Toolbars Me cee mw Ler oy Spee o sasea aaea soles eje aj elol JLA ETENE a a rr eee HEE HA aa eaa E a user 141 142 Figure IF Prolog Main Window Toolbars Toolbar buttons are shortcuts for most frequent commands You can invoke various IF Prolog actions just by button click You do not need to remember each button function ality If you move mouse cursor over any toolbar button a short button help text appears over the button Also the main window status bar displays additional information on button command There are three toolbars contained in the IF Prolog main window e Main Toolbar e Debug Main Toolbar e Debug Information Toolbar You can hide or show each of these toolbars by menu commands from the View Menu User s Guide 248 IF Prolog V5 3 Graphical User Interface MS Windows 11 3 1 Main Toolbar
98. DIR lib netlib a lgen los These library definitions are only necessary if the application uses the Constraints Package of PROLOG Uncomment the following lines if necessary PROLIB_CONS PROROOT c libCprolog a PROROOT c libCDprolog a PROROOT c libCFprolog a PROROOT c libCLprolog a PROLIBS_CONFIG PROLIB_MOT PROLIB_INF PROLIB_CONS PROLIB_RAT SYSLIBS_CONFIG SYSLIBS_MOT SYSLIBS_INF SYSLIBS This defines the objects and or libraries of your application USEROBJS USERLIBS This defines the PROLOG Part of your application PROSOURCE SAVESTATE This is the name of your application kernel TARGET application This is the main module of the application Change this definition for your own main module MAIN PROROOT c main o User s Guide 240 IF Prolog V5 3 C interfaces Generating an extended Prolog system IF cc c CFLAGS lt This makefile entry generates the application kernel according to the above definitions TARGET MAIN USEROBJS USERLIBS SAVESTATE IF cc LDFLAGS MAIN V USEROBJS USERLIBS V PROLIB_KERNEL PROLIB_CHARS PROLIBS_CONFIG PROLIB_DUMMY SYSLIBS_CONFIG SYSLIBS o This makefile entry may be used to generate the PROLOG Part of the application SAVESTATE PROSOURCE PROROOT bin prolog root PROROOT r runtime pst st c PROSOURCE clear rm
99. El move 4 clear_it N 1 centre E move tower 2 flush output 3 a clear_it N 1 right E shift_down 4 flush_output shift left 5 draw_base flush_output Figure IF Prolog Database Browser Window User s Guide 256 IF Prolog V5 3 Graphical User Interface MS Windows 11 6 1 Database Browser Window Database Browser window contains several items e Database Tree Pane Left subwindow of the Database Browser window e Database Item Pane Right subwindow of the Database Browser window e Menu Menu at the window top e Toolbar Toolbar at the window top 11 6 2 Database Tree Pane The root item of the tree is the database itself Database Tree Pane shows all the modules sorted alphabetically as subtrees of the root One additional module is created for operators It is situated at the tree bottom Each module subtree contains three subtrees Predicates contained in the module Predicates visible in the module and Module variables 11 6 3 Database Item Pane Database Item Pane displays the information for every item that is selected in the left pane Database Tree Pane The Table shows a correspondence between a selected item in the Database Tree Pane and information displayed in the Database Item Pane Item Database Tree Pane Item information Database Item Pane Item Database Tree Pane Item information Database Item Pane Database root IF Prolog statistics Module Module listing Module Predicates L
100. Escape 28 34 1C FS File Separator 29 35 1D GS Group Separator 30 36 1E RS Record Separator 31 37 1F US Unit Separator 287 ASCII table Appendix B Decimal Octal Hexadec Meaning 32 40 20 SP SPACE 33 41 21 34 42 22 35 43 23 Number symbol 36 44 24 or national currency symbol 37 45 25 38 46 26 amp 39 47 27 i 40 50 28 41 51 29 42 52 2A x Asterisk multiplication sign 43 53 2B ES 44 54 2C 45 55 2D 46 56 2E 47 57 2F Division sign 48 60 30 0 49 61 31 1 50 62 32 2 51 63 33 3 52 64 34 4 53 65 35 5 54 66 36 6 55 67 37 7 56 70 38 8 57 71 39 9 58 72 3A E 59 73 3B i 60 74 3C lt 61 75 3D 62 76 3E gt 63 77 3F User s Guide 288 IF Prolog V5 3 Appendix B ASCII table Decimal Octal Hexadec Meaning 64 100 40 commercial at or 65 101 41 66 102 42 67 103 43 68 104 44 69 105 45 70 106 46 71 107 47 72 110 48 73 111 49 74 112 4A 75 113 4B 76 114 4C 77 115 4D 718 116 4E 79 117 4F 80 120 50 81 121 51 8 amp 2 122 52 83 123 53 84 124 54 85 125 55 86 126 56 87 127 57 88 130 58 89 131 59 90 132 5A 91 133 5B EN KKM SS GCHPWAOTVOSZEZMACTEOAAIAWS OO 92 134 5C Backslash 93 135 5D 94 136 5E 95 137 5F Underscore IF Prolog V5 3 289 User s Guide ASCII table Appendix B Decimal Octal Hexadec Meaning 96 140 60 97 141 61 a 98 142 62 b 9 143 63 c 100 144 64 d 101 145 65 e 102 146 66 f 103 147 67 g 104 150
101. For each 1024 byte unit you may also use the abbreviation 1k or 1K e g 10k instead of the value 10240 If required for program execution the memory area is automatically in creased Default 8K The standard input streams are not buffered by Prolog Sets the Prolog consult flag to nonotify This means that no messages are displayed on the screen when a file is loaded Prevents the Prolog system from managing signals itself i e it defines no user defined signal handling routines The predicate signal_control 2 has no effect The standard streams screen and keyboard are not opened but are mapped to the standard input and standard output In this case it is not possible to use the keyboard and the screen as the input output devices when the standard input output has been redirected Sets the Prolog warnings flag to off This means that when consult 1 is called no warnings will be displayed on the screen program Goal If Goal is a syntactically correct goal it will be invoked automatically inside the module user after the system start After successful execu tion of that goal Prolog is terminated If Goal is incorrect Prolog is terminated immediately It is possible to qualify Goal with the name of a loaded module where it is to run User s Guide 64 IF Prolog V5 3 Prolog system Calling Prolog prompt Sets the Prolog prompt flag to on This means that Prolog will out put a prompt or that backtracking can b
102. IF Prolog V5 3 User s Guide Siemens AG Austria Is there anything you would like to tell us about this manual Please send us your comments Siemens AG Austria PSE KB B3 Gudrunstrasse 11 A 1100 Vienna Austria Fax 43 1 1707 56992 email prologU siemens at The information in this document is subject to change and does not represent a commitment on the part of Siemens AG Austria The software described in this document is furnished under a license agreement The software may be used or copied only in accordance with the terms of the agreement UNIX is a registered trademark in the United States and other countries licensed exclusively through X Open Company Limited X Open and the X device are trademarks of X Open Company Ltd Copyright Siemens AG Austria 1999 All rights reserved The reproduction transmission translation or exploitation of this document or its contents is not permitted without express written authority Offenders will be liable for damages Delivery subject to availability right of technical modifications reserved Contents Contents ili 1 Preface 1 Target OU cioe et eee eee Oe BEY Ree ee eee eS 1 Summary of ONIONS J ek e eS eR eB eR Le we Re a aoh 2 2 A first Prolog session 5 2 1 Calling and exiting Prolog ascos eee edo O45 a 5 o A irot Prolog GUE a e ee eo eS 6 Ze A short Prolog example o cessc oncekan a a WR 9 2 3 1 Entering data with an editor 2 4 6444 be ebb aw as a 11 Zoe L
103. Prolog V5 3 7 User s Guide First Prolog query Sample session Possible errors and how to correct them 1 Maybe you forgot to terminate your input with a period If Prolog did not respond as described to write hello then you may have forgotten to type the period at the end of the line If so simply type the period and then hit the lt key Your input sequence would then appear as follows user write hello gt hello yes user The prompt character signifies that input of the term has not yet been completed 2 Maybe you wrote Hello with an uppercase H but forgot to enclose it in single quotes Prolog s response will be something like this user write Hello 656 Hello 656 If you now hit the gt key again the Prolog prompt user will appear once more yes user Hello is a Prolog variable because it begins with an uppercase H The variable is not instantiated i e no value has been assigned to it For uninstantiated variables the system outputs an underscore character followed by a number This is how variables are represented internally in the Prolog system You must enclose the correct input in single quotes It then appears as follows user write Hello Hello yes user 3 Maybe you entered a period within the parentheses without enclosing the entire
104. Prolog system The Prolog system offers the following interfaces User s Guide 76 IF Prolog V5 3 Prolog system Interfaces Input output interface The input output interface of the Prolog system is structured on the basis of a driver model enabling you to manage different input output streams with a specific set of predicates You can program your own device drivers using the C language and link them into the Prolog system For a detailed description of the input output interface refer to chapter 5 Active interface to the C language This interface enables Prolog predicates to be programmed as C functions and then to be used like normal predicates The active interface to the C language is described in detail in chapter 10 1 Passive interface to the C language The passive interface to the C language enables Prolog predicates to be called in a C program in a similar way to C functions and also to initiate backtracking where appropriate For further information on this interface refer to chapter 10 2 C device drivers You can use the C device driver interface to implement your own device drivers in C and link them into the Prolog system For a description of this interface refer to chapter 10 3 Interface to OSF Motif If the Prolog system has been configured with OSF Motif and an X server is available the interface to OSF Motif is automatically available OSF Motif objects such as menus and windows can be created and
105. Prolog system In the latter case the Prolog system may round the specified values to useful values The maximum values depend on the size of the available main memory Ini tialization is not normally required however because the Prolog system automatically matches its memory usage to the system requirements The built in predicate system_parameters 1 al lows you to query the system options and their associated argu ments which you have specified in the call including the system name prolog The meanings of the various system arguments are explained in the following debug Sets the Prolog flag debug to on This means that all the predicates which you read into the database with consult 1 reconsult 1 or assert 1 2 are implicitly declared as dynamic thereby supporting the debugging of a program iso Only the facilities required by the ISO standard for Prolog are available to you You cannot use the extensions specific to IF Prolog language charset Sets the character set which will be supported by the input output built in predicates of IF Prolog See also the IF Prolog Reference Man ual 1 predicate current_language 2 The parameter charset may have one of the following values User s Guide 62 IF Prolog V5 3 Prolog system Calling Prolog mi number ms number msc number msg number msl number e ascii This is the default value It specifies that I O predicates accept and deliver ASCII code text e e
106. Ti T2 uni T1 T2 R R1 T1 T2 var T1 stack_replace T1 T2 R R1 The original term pair is put in the stack Start unification End if stack is empty Take first pair from stack Find first substitutions Subl and create new stack R1 Find remaining substitutions there and combine them all to form unifier Constants are unifiable if they are identical No changes made to stack Identical terms can be unified without further substitution Ti is a variable replace T1 with T2 in stack and record the IF Prolog V5 3 35 User s Guide Unification Database and procedure execution substitution T1 T2 uni T1 T2 R R1 T2 T1 T2 is a variable replace T2 var T2 stack_replace T2 T1 R R1 with T1 in stack and record the substitution T2 T1 uni T1 T2 R R1 compound T1 compound T2 Ti and T2 are structures with functor T1 Functor Arity the same functor and the same functor T2 Functor Arity arity push all argument push T1 T2 Arity R R1 pairs onto the stack push _ _ 0 R R End because there are no more arguments push T1 T2 N R pair X Y R1 Take the two Nth arguments arg N T1 X arg N T2 Y put the pair on the top of the Ni is N 1 stack push down the remaining push T1 T2 N1 R R1 pairs beneath it stack replace T1 T2 yA End because there is nothing more to replace stack_replace T1 T2 pair X_old Y_o
107. When Prolog texts are consulted Prolog replaces these directives with the contents of the physical file Filename Filename is an atom which represents the path and the name of a file in accordance with the operating system used If Filename is not an absolute pathname the Prolog system inter prets the pathname relative to the current file For example if the file usr joey prolog test pro contains the directive include example frame pro on loading the include di rective is replaced by the file usr joey example frame pro User s Guide 118 IF Prolog V5 3 Module objects If the file cannot be opened or if Filename is not an absolute pathname Prolog searches for the file in the directories defined under the Prolog flag search path see predicate current prolog_flag 2 Example physical file append pro append L L append XIL1 L2 X L3 append L1 L2 L3 physical file member pro include append pro member X XIL member X _IL member X L physical file list pro is list Cl is_list _IL is_list L include member pro When you consult the physical file list pro the physical files member pro and append pro will be read in too Your database will then contain the following logical file is list is list L is list L append L L append X L1 L2 XIL3 append L1 L2 L3 member X XIL member X _ L
108. able you are not allowed to examine the box for that predicate Consequently only the call error exit fail redo suspend and wakeup ports may be used as breakpoints If you try to specify other ports the debugger ignores them Provided a port is active by the debug mode set for the predicate the debugger stops when User s Guide 170 IF Prolog V5 3 Debugging Breakpoints e the port is activated e an explicit breakpoint is set at this port and its call is successful at this port e the port is the destination of a branch command see page 173 You use breakpoints to interrupt the execution of a goal and to perform certain actions three types of breakpoint may be differentiated e implicit breakpoints e explicit breakpoints e temporary breakpoints Breakpoints which are skipped by commands are also referred to as deactivated breakpoints in the following Implicit breakpoints Implicit breakpoints are points at which the debugger switches to interactive mode You define implicit breakpoints before calling the debugger with the predicate debug config 3 or during the debugger session with the command port 2 or the menu Ports Debugger commands such as nonstop or fast skip skip the implicit breakpoints Explicit breakpoints You set explicit breakpoints yourself during a debugger session In doing so you can set simple explicit breakpoints or make stopping in the debugger dependent on conditions From the static viewpo
109. able provides an overview of the streams which can be addressed For addressing either the device name or the alias name can be used The device name or driver name is used only on opening a stream The alias name represents a logical reference to a stream and establishes a link between a stream and a name The name is freely selectable Stream Alias name Standard input user_input Standard output user_output Standard error output user_error Keyboard keyboard Screen screen The following defaults are set for the three standard streams Stream Default Standard input Keyboard Standard output Screen or window in which Prolog was started Standard error output Screen or window in which Prolog was started and error message window You can also redefine the standard streams when calling Prolog Example The file goals pro is assumed to contain the following goals Y is 3 4 append A B a b c d atom A If you then call Prolog and specify the file as the input stream these goals are processed sequentially The following output appears on the screen prolog lt goals pro Y 7 yes w Il a b c d yes no IF Prolog V5 3 81 User s Guide Elementary input output Input output interface When the end of the file is reached Prolog is terminated You can use the predicate stream copy 2 to change the standard stream assignments during a Prolog session 5 2 1 Input output using current
110. ad Pred Info auto_load Pred File member goal Goal Info IF Prolog V5 3 153 User s Guide Signals Contexts consult File predicate type Pred Type Type undefined l Goal try_autoload Pred Info throw error existence_error procedure Pred Info The handling of exceptions takes place within the context which defines this handling Con sequently there is a danger of cycles if exceptions occur during handling itself and are then handled in the same context The following rules may help you to avoid such situations 1 You should specify exceptions to be handled as precisely as possible so that the context handles only the exceptions it is intended to handle There will be very few cases where it is appropriate to specify that all exceptions are to be handled 2 When an exception is handled you should ensure that the cause of the exception is eliminated so that the exception does not immediately re occur In the previous example this is achieved by checking before calling Goal again whether the predicate is defined after the load operation 3 You should pay special attention to planning what is to happen when during handling it is established that the handling cannot be successfully performed In particular the original exception should not be generated again In the previous example this is covered by the second clause of try_autoload 2 No exception is generated instead a context jump is exec
111. amic The dynamic directive is used in the declaration part of the module body It has the argument PredicateList which corresponds to a list of predicate indicators The predicates in the list are declared as dynamic At runtime you can inspect clause 2 listing 0 1 and modify retract 1 2 dynamic predicates Activation of dynamic predicates which do not have any clauses does not trigger the exception existence_error procedure instead the predicate is evaluated with fail All predicates listed in a dynamic directive are also implicitly declared as private This prevents the private definition of a predicate from being overlaid by an import directive This also allows you to redefine built in system predicates within a module If you wish to reexport a predicate in the module interface then you should not declare it as dynamic using the dynamic directive i All the predicates defined in the module body which are not declared as dynamic are by default static unless you specify the system parameter debug when invoking Prolog You can neither inspect nor modify static predicates but may delete them with abolish 1 You cannot declare predicates which you have implemented with the C interface as dynamic Example This example defines the module storage_administration It exports the predicates receipt 2 outlet 2 and stock 2 The predicate stock 2 shows the current stock level since it is subject to change thi
112. ancestor 2 1 1 call 0 stop interactive user ancestor 2 1 2 redo 01 1 i user ancestor 2 1 1 call D 2 i user ancestor 2 1 2 redo Command lt ENTER gt User s Guide 182 IF Prolog V5 3 Debugging OSF Motif interface The command Continue or gt then produces the following output from the debugger lancestor gregor pepsi CALL gt father gregor pepsi Command 9 5 Debugging under the OSF Motif user interface The Prolog system provides a window oriented debugger under OSF Motif The following section describes how you call the Prolog debugger under the OSF Motif user interface and use the menus windows and buttons If the Prolog system has been configured with OSF Motif the OSF Motif user interface is loaded when the debugger is called If you call the debugger for the predicate ancestor 2 you will get the following screen IF Prolog V5 3 183 User s Guide OSF Motif interface Debugging Prolog Debugger e ml Command Ports Breakpoints Trace Ancestors History Help Goal call ancestor X pepsi user Breakpoint BEGIN Ex debug CALL gt calllancestor X pepsi user fast skip continue nonstop abort back_subgoal back_clause back_parent success failure error Debugging under the OSF Motif user interface OSF Motif opens a separate window for the Prolog debugger In the title bar of
113. ansforma tion are those predicates which are parenthesized by ca11 1 explicitly or implicitly if the goal is a variable A term is transformed into a goal whenever it is loaded into the Prolog database with consult 1 assertz 1 2 etc or when a term parenthesized by call 1 has to be made available for execution Transformation is subject to the following rules e If a Prolog goal is a variable the variable is parenthesized by the control structure call 1 and the module argument of ca11 1 is set to the name of the current module e If a Prolog goal is an explicitly qualified metapredicate without an explicit module argument the module argument is set to the name of the current module e Ifa Prolog goal is an unqualified predicate it is qualified with the name of the module from which it was imported If the predicate was not imported it is qualified with the name of the current module IF Prolog V5 3 123 User s Guide Global variables Module objects e Ifa Prolog goal is an unqualified visible predicate whose module argument is explicitly set although it is not defined as a metapredicate the module argument is ignored e If a Prolog goal is a metapredicate whose module argument is not explicitly set the module argument is set to the name of the current module The same applies to the language construct call 1 e If a Prolog goal is a language construct except for ca11 1 which itself has subgoals as arguments the a
114. ariable pc_position Position at which reading from the string was terminated pc_errno 0 or the error number according to syntax_error 2 if an error has occurred i If an error occurs during initialization the components pc_term and pc varlist remain undefined In addition to the data types described here you may also use the data types TERM ARITY and STRING described under the active C interface section 10 1 Central functions of the passive C interface Function Purpose InitProlog Initialize the Prolog system and define the standard media ParseProlog Parse the goal string and store the information ParseError Determine error description during the analysis PrologFetch Prove a goal PrologModule Determine module EndProlog Release memory space occupied by the Prolog system Built in module variables All built in predicates are assigned to the system or user module as a result of the modular organization of the Prolog system If you want to prove goals within the built in modules you can do this by specifying the global variables defined by Prolog system Variable Module SystemModule system User Module user Passive Prolog goals The passive C interface distinguishes between passive active and current Prolog goals A passive Prolog goal is one which although executable is not being executed at present it is so to speak on hold You should create a passive Prolog goal if you subsequen
115. at finfo if fstat fileno FILE unit amp finfo 1 return FALSE if finfo st_mode S_IFMT S_IFREG return TermUnify CTLARG 1 long finfo st_size return FALSE static const t_control 1 file control stream_size 1 file_size isatty 0 file isatty NULL Fi static const t device 1 file device my_device file_open file_close NULL file_read file_write IF Prolog V5 3 237 User s Guide Generating an extended Prolog system C interfaces file flush file is unix 1 file control DeviceCreate amp 1 file device NULL void Cboot void 10 4 Linking Prolog and C If you wish to use one of the C interfaces and have performed the steps described in the previous sections you must generate a new extended Prolog system which will then contain the C functions e To do this you must first write a Cboot function containing the following calls a call to the function CPRIM for each simple function which is to be linked or for a Prolog call in a C program a call to the function CPRED for each complex C function a call to the function MCPRIM for each simple function which is to be assigned to a particular Prolog module a call to the function MCPRED for each complex C function which is to be assigned to a particular Prolog module and a call to the function DeviceCreate for each C device driver which is to be connected
116. atabase The database is the set of predicates available at the moment a goal is being processed It includes e User defined predicates Predicates which the user defines by means of clauses The relevant clauses for a predicate are those which were read in at system startup with consult 1 reconsult 1 or an assert predicate but were not subsequently removed Compiled predicates loaded into the database by the user Predicates defined by the user with the C interface e Built in predicates The built in predicates are made available automatically Note however that the contents of the database are not entirely visible Visibility is con trolled by the module concept see section 7 The Prolog module concept 3 4 Unification The key concepts governing procedure execution are unification and depth first search Unification is an operation which e checks whether two terms are equal or can be made equal by suitably instantiating their uninstantiated variables e if necessary performs an instantiation of the most general kind possible substitution Otherwise unification fails The unification operator the equal sign is declared with op 700 xfx as a non associative infix operator see 2 Unification is governed by the following rules User s Guide 34 IF Prolog V5 3 Database and procedure execution Unification e an uninstantiated variable can be unified with any term by being instantiated to
117. ate a context for the execution of Goal The information for context global actions specified in the list ContextInfo is stored in this context How the information is structured and what the underlying semantics are is defined by the individual context global actions Other predicates for context global actions are 147 Context jumps Contexts Predicate Purpose catch Goal CatchMask SubstituteGoal Intercept a context jump current signal Signal Mode Query signals and modes exception Error Info Raise an exception exception_handler Goal Exception Handler Define local exception handling signal control Signal Command Control signal processing signal handler Goal Signal Handler Define local signal handling throw Q Term Initiate a context jump The predicates for the context global actions are all based on context 2 All predicates are described in the following sections 8 1 Context jumps In a context jump similar to a longjmp 2 in C see 10 a context is exited at any point in the proof tree and the entire proof tree is discarded at a stroke When the context is exited the actions defined at creation time can be executed You can explicitly initiate context jumps or they may be executed automatically in the course of standard error handling in the system If several contexts are nested the innermost context matching the corresponding context jump is used If no matching context is found
118. ate a new Prolog kernel system by means of the makefile model make If you have used one of the C interfaces active passive or device driver in the application or if your customer is using further system variants such as OSF Motif or Informix you must enter this in model_make before you generate a new kernel system The new Prolog system is part of the application and is delivered to your customer Runtime environment The runtime environment is that part of your application which is written in Prolog You make this execution environment available to your customer in the form of a saved system state with the predicate save_runtime 1 Those parts of IF Prolog system which are written in Prolog are also available in the form of a saved system state The runtime version of IF Prolog is located in the file runtime pst in the directory PROROOT prolib You must create such a runtime environment for the Prolog application that it contains all the Prolog programs and the Prolog runtime system This has the following advantages 283 Prolog applications Appendix e Starting the Prolog system with the application is very fast e The file containing the saved system state is largely unreadable e You deliver only a limited number of files Example The following example is an illustration of how to create a Prolog application A prerequisite here is the part written in Prolog of the application app pro which is stored in the module
119. ate is called whose arguments are the evaluated subexpressions and a variable for the result The name of the predicate is determined by the name of the function module expression meta 2 begin module expression op 700 xfx Value Expression number Expression Value Expression Value Expression functor Expression Name Arity Expression _lArgs evaluate Args Params calling context Module evaluate function Name Arity Params Value Module evaluate function Name Arity Params Result _ IF Prolog V5 3 139 User s Guide Compiling and loading files Module concept system function Name Arity Function Name Params Result is Function evaluate_function Name _ Params Result Module Function Name Result Params once Function Module evaluate evaluate ArglArgs Param Params Param Arg evaluate Args Params system_function 2 system function 2 system_function 2 system function 2 end module expression The module demo uses the predicates of the module expression and the operator defined there module demo export demo 0 begin module demo import expression r opt 1007 xf demo N 2 3 writeq result N nl 1 R N gt N gt 0 R N N 1 end module demo User s Guide 140 IF Prolog V5 3 Module
120. ates The Prolog predicate concept can be compared to the subprogram concept in conventional programming languages we therefore also speak of calling a predicate in Prolog The pre dicate concept ensures a high degree of modularity in Prolog programs and thereby supports structured programming Example The two clauses below define the predicate fac 2 for calculating the factorial of a natural number fac 0 1 O is 1 User s Guide 30 IF Prolog V5 3 Database and procedure execution Predicates fac N Nfac N gt 0 for integers N gt 0 M is N 1 defines NFac as N N 1 fac M Mfac thus MFac is calculated Nfac is N Mfac recursively as N 1 You can call the predicate fac 2 in the Prolog input loop by entering for example user fac 5 X lt The Prolog system searches the clauses belonging to the predicate fac 2 and displays the answer X 120 The precise sequence in which Prolog performs this search operation is described in the section Procedure execution Only predicates whose clauses have been included in the database with consult 1 reconsult 1 or with an assert predicate can be modified i e clauses can be added assert predicates or removed retract predicates individual predicates can be deleted abolish 1 or re placed reconsult 1 The clauses of these predicates can be output with listing 0 1 and analyzed with clause predicates Note
121. ava Interface IF Prolog V5 3 BDD Package X Open CAE Common Applications Environment Specification System Interfaces and Headers Issue 4 Prentice Hall 1994 International Standard ISO IEC IS 13211 1 International Organization for Standard ization 1995 William F Clocksin Chris S Mellish Programming in PROLOG Standard Edition Berlin et al Springer 1995 Ivan Bratko PROLOG Programming for Artificial Intelligence Second Edition Addison Wesley 1990 Leon Sterling Ehud Shapiro The Art of PROLOG Advanced Programming Tech niques Cambridge Massachusetts MIT Press 1986 295 Index OSF Motif import 77 OSF Motif user interface 183 debugger 183 error message window 185 operation window 184 abort signal 155 action context global 147 active signal handling 158 active C interface 195 functions 197 active goal 221 create 223 additional information exception 151 alarm signal 155 alias current_error 82 current_input 82 current_output 82 alias name 79 for stream 80 keyboard 81 screen 81 user_error 81 user input 81 user output 81 alphanumeric user interface command line 175 constraints for subgoals 179 constraints for variables 179 debugger 174 field for ancestor goals 179 296 field for AND BOX 177 field for breakpoints 178 field for database 179 field for old goal and clause boxes 178 field for OR BOX 178 field for settings 178 help field 177 informa
122. ay in which a value is assigned to a C variable User s Guide 196 IF Prolog V5 3 Active C interface Overview To resolve these differences the active C interface provides a set of built in functions The following diagram presents an overview of the areas covered by these functions In each case the arrow indicates the direction of information flow lt Link C function to Prolog gt Classify Prolog terms gt lt Generate Prolog terms Convert data objects Prolog o IE C function Convert Prolog terms to C objects gt lt Convert C objects to Prolog terms Access components of a Prolog term gt lt Unify Prolog terms Type test functions Prolog exception handling Functions of the active C interface Overview of the built in data objects and functions This section summarizes the library functions of the active C interface grouped according to the tasks which they perform All built in data objects and functions are declared in the include file cpred h which you will find in the directory PROROOT c Data objects of the C interface All data objects in Prolog are terms Internally every Prolog term is represented by a machine word containing an appropriate identifier for the class to which the term belongs The data type name TERM is defined for this data type in the include file cpred h This data type name is used
123. base now has clauses of the form verbal_phrase v_ph V Inp Tail verb V Inp Tail and the Prolog system responds to the query user sentence Syntaxtree Sentence Syntaxtree sentence n_ph article the noun dog v_ph verb barks Sentence the dog barks Syntaxtree sentence n_ph article the noun dog v_ph verb barks Sentence the dog runs Syntaxtree sentence n_ph article the noun bird v_ph verb barks Sentence the bird barks Syntaxtree sentence n_ph article the noun bird v_ph verb runs Sentence the bird runs no User s Guide 108 IF Prolog V5 3 Prolog grammar rules Additional arguments The first solution generated the sentence the dog barks and the following syntax tree sentence EX nn_ph v_ph article noun verb the dog barks Admittedly however this grammar also recognizes the token sequence the bird barks as a correct sentence and rightly so since a syntactical analysis cannot provide any infor mation on meaning i e on the semantic content Example An option line of the form pl p2 p3 is to be read and checked for syntactical correctness and a list of the input parameter values generated If a given option is not specified the associated default value
124. ber of characters read at the end of the data it returns 0 otherwise 1 int dev_write t_iounit deviceunit void buffer int number This function writes a maximum of number characters from a buffer to the in put output unit deviceunit If successful dev_write returns the number of char acters written otherwise 1 BOOLEAN dev_flush t_iounit deviceunit dev_flush flushes any buffers of the device driver i e any data buffered for the stream is output In place of this function you may also enter the NULL pointer in the User s Guide 230 IF Prolog V5 3 C interface C device drivers structure t device If successful dev flush returns TRUE otherwise FALSE BOOLEAN dev_is_unix_file t_iounit deviceunit int no This function checks whether the input output unit deviceunit is a UNIX file If so it returns TRUE and writes the UNIX file number of the unit in no If no corresponding file exists the function returns FALSE If dev_is_unix_file is always FALSE you may also enter the NULL pointer in the structure t_device instead const t control dev_control dev_control points to an array of type t_control in which you can place an arbitrary set of stream functions control functions These control functions enable you to use built in predicates to access the device driver which you have programmed yourself When the predicate stream_control 2 and certain other predicates are called their functions are mapp
125. bgoals of a clause called prior to repeat 0 will therefore never be reached when backtracking within this clause This predicate is often used in conjunction with the following fail 0 to create loops fail 0 Proof of fail 0 will invariably fail Example The clause endless repeat fail creates an endless loop You can implement conversion to a repeat until loop with a defined termination by means of non logical predicates Possible ways are to e use input output predicates e manipulate the database e use a global variable User s Guide 56 IF Prolog V5 3 Database and procedure execution Procedural predicates Example One example of input output predicates is the incomplete simulation of a Prolog input loop by means of the following program dialog repeat prompt read Goal work Goal Goal end F prompt write work end true work Goal Goal I 3 write yes nl work Goal write no nl Calling dialog starts the loop and entering end terminates it The cut in the dialog clause prevents the repeat loop from being run through again in the course of backtracking after the end criterion Goal end has been reached The cut in the second work clause replaces the condition not Goal in the final clause and thus prevents double execution of Goal Both cuts are red cuts Example To illustrate manipulation of the database we will take a count
126. ble in the operation module with the exception of sort 2 In addition the sort 2 predicate from the quicksort module and the permute 2 predicate from the utilities module are visible If there are any private predicates defined in the module then these will of course also be visible within the module module utilities export sort 2 permute 2 end module utilities module quicksort export sort 2 end module quicksort module operation begin module operation import quicksort import utilities permute 2 end module operation IF Prolog V5 3 135 User s Guide Declaring private predicates Module body 7 4 3 Declaring predicates as distributed You can split up the clauses of a predicate with the directives discontiguous 1 and multifile 1 If the definition of a predicate is split without using an appropriate directive the Prolog sys tem issues a warning By using the discontiguous 1 directive you can distribute the definition clauses of a predicate within a file The multifile 1 directive allows the clauses of a predicate to be defined in several files Both directives have the argument PredicateList which corresponds to a list of predicate indicators 7 4 4 Declaring predicates as private The private directive is used in the declaration part of the module body It has the argument PredicateList which corresponds to a list of predicate indicators The predicates
127. bove rules are applied to the subgoals Example module expert meta minimize 2 module timetable export connection 3 duration 3 module plan begin_module plan import expert import timetable journey From To Holidays minimize Price connection From To Price duration From To Days Days lt Holidays end module The body of rules journey 3 is transformed as follows journey From To Holidays expert minimize Price connection From To Price plan timetable duration From To Days system Days lt Holidays Language constructs The following indicators denote language constructs and can therefore not be used as indi cators for user predicates User s Guide 124 IF Prolog V5 3 Module objects Global variables Indicator Language construct Je 2 gt 2 1 not 1 call 1 true 0 fail 0 1 0 Ea a 2 1 2 gt 2 Conjunction of goals Disjunction of goals Conditional execution of goals Negation of a goal Negation of a goal Indirect activation of goals Always true Always false Cut Explicit qualification Setting of the module argument Directive Rule Grammar rule 7 2 3 Global variables Global variables are managed only in modules They are visible only within their original module You can however access global variables in other modules All predicates which access global variables are implemented as me
128. breakpoint parameters Field Description Number Breakpoint identifier Mode Mode of the breakpoint active suspended Interactivity Interactivity of the predicate batch interactive Predicate Indicator Clause Subgoal or Variable Port or Binding Conditions Actions Variables Predicate name arity and its module in the format module name arity Clause number Subgoal regular stop number or monitored variable Port regular stop or name of the monitored variable A list of conditions on which an action is invoked A list of actions that are invoked on conditions List of variables of the breakpoint User s Guide 270 IF Prolog V5 3 Graphical User Interface MS Windows The table describes functionality of the dialog buttons Button Description Set Invokes Set Breakpoint dialog When there is at least one breakpoint Set Breakpoint dialog displays parameters of this breakpoint Remove Removes current selected breakpoint Remove All Removes all breakpoints Activate Activates current selected breakpoint Activate All Activates all breakpoints Deactivate Deactivates current selected breakpoint Deactivate All Close Help Deactivates all breakpoints Closes the Breakpoint dialog Invokes this help page 11 8 4 Set Breakpoint Dialog Set Breakpoint 6 hanoi clear_stone 3 Figure Set Breakpoint dialog IF Prolog V5
129. burger hamburger 40 1 50 supplier burger supplier burger supplier burger ice cream 30 1 25 init save_system supplier halt A C program accesses this data and calculates the total price for each article from a given supplier The supplier is the call argument for the function The source text for such a function could look something like this include lt stdio h gt include lt stdlib h gt include cpro h static BOOLEAN initialized FALSE static void if defined __STDC__ leave_application int status else leave_application status int status endif if initialized EndProlog exit status void IF Prolog V5 3 User s Guide Application Passive C interface if defined __STDC__ main int argc char argv else main argc argv int argc char argvl endif STRING prolog_args 4 char query 100 t_parse_context context t_cursor cursor TERMCONTEXT tmcontext if argc 2 fprintf stderr Usage s supplier n argv 0 leave_application 1 The save state supplier pst is read into the database automatically at startup prolog_args 0 argv 0 prolog_args 1 r prolog_args 2 supplier pst prolog_args 3 NULL initialized InitProlog 3 prolog_args stdin stdout stderr if initialized fprintf stderr PROLOG could not be initialized n leave_application 1 argv 1 sup
130. byte unit you may also use the abbreviation 1k or 1K e g 10k instead of the value 10240 If required for program execution the memory area is automatically in creased Default 30K number bytes are made available as the initial value of the local data stack number is an integer specifying the size in bytes of the stack made avail able For each 1024 byte unit you may also use the abbreviation 1k or 1K e g 10k instead of the value 10240 IF Prolog V5 3 63 User s Guide Calling Prolog Prolog system mst number msx number nobuf nonotify nosignal notty nowarnings If required for program execution the memory area is automatically in creased Default 30K number bytes are made available as the initial value of the variable in stantiations stack trail While the system is attempting to prove a goal the stack is used to store information about current variable instantiations which will have to be cleared again undone during backtracking number is an integer specifying the size in bytes of the stack made avail able For each 1024 byte unit you may also use the abbreviation 1k or 1K e g 10k instead of the value 10240 If required for program execution the memory area is automatically in creased Default 8K number bytes are made available as the initial value of the extended terms stack number is an integer specifying the size in bytes of the stack made avail able
131. cal data stack 63 local stack size 69 used 69 logic update view 42 loops 56 macros for including complex C functions 211 makefile model_make 239 map data objects 198 maxint 70 memory garbage collection 72 query usage 72 memory area constraints 73 database 73 extension 73 global 73 local 73 trail 73 memory block size define 63 memory management 72 memory parameter 73 message lines alphanumeric user interface 176 messages suppress 64 meta directive 128 138 metapredicate 121 declare 138 export 128 minint 70 mode query for a signal 155 model_make 239 modifiable syn dynamic 143 modifiable predicate 137 modification dynamic 144 file 13 module acces using variables 220 body 117 compile 141 constraints 78 current 116 declare metapredicate 138 define operator 139 directive 126 directives 118 dynamic predicate 137 file 118 import 133 informix 78 interface 117 load 144 146 local predicates 136 motif 77 reexport predicate 130 reexport predicates selectively 131 remove from database 146 selective import 134 module argument 121 module body 132 declaration part 133 implementation part 133 operator 139 syntax 132 module concept 115 module interface of different module impor 133 module object global variable 120 predicate 120 modules built in 115 directive export 127 global variables 125 interface 126 language constructs 124 meta a
132. can reexport only predicates which you have already exported in the module interface of Module i You must not overlay reexported predicates with private predicates You must not reexport predicates which have the same indicator but have been declared in different modules Example In this example the module utility exports all predicates which are exported in the modules permute order and sort_nev module permutation export permute 2 end module permutation module order export ordered 2 end module order module sort new export sort 2 end module sort new module utility reexport permutation reexport order reexport sort new end module utility 7 3 4 Reexporting specific public predicates of a module The selective directive reexport PredicateList from Module makes a subset of the public predicates of the module Module available to other modules The reexport directive requires that the module interface of Module be known Module is an atom which defines the name of the module whose module interface is to be reexported PredicateList is a list of predicate indicators The predicates contained in the list are to be reexported You must have exported all predicates of the PredicateList in the module interface of Module You can reexport only predicates which you have already exported in the module interface of Module IF Prolog V5 3 131 User s Guide
133. cate open 3 4 then none of the flags S_IO_EA_ERROR S_IO_EA_RESET and S_IO_EA_EOF_CODE is set How ever opening of a stream is rejected when repositioning is requested but is not possible or when the requested input output direction cannot be supported t_iounit deviceunit The pointer deviceunit contains the address of the unique identification of the input output stream This unique identification is the reference for all subsequent operations on the stream BOOLEAN dev_close t_iounit deviceunit BOOLEAN force Closes the stream which you have opened with dev_open If you do not need dev_close you may also enter the NULL pointer here If successful the function must return TRUE otherwise FALSE t_iounit deviceunit Identification of the input output stream BOOLEAN force To be set according to the force option of the predicate open 3 4 but not currently evaluated by the standard device drivers void dev_stop void Closes the driver The function is called before exiting from the Prolog system It enables you for example to eliminate any side effects which installation of your driver has caused e g closing all open windows in a windowing system or releasing memory You may also enter a NULL pointer here int dev_read t_iounit deviceunit void buffer int number This function reads a maximum of number characters from the input output unit deviceunit into a buffer If successful dev_read returns the num
134. cates of the constraints package const_linear const_domain The predicate current_module 1 allows you to query all modules which have been loaded or imported into the database 115 Modules Module concept Example user system user system system system system system system system system system system system system current_module M write M nl fail debug debug debug debug debug debug debug debug debug debug debug break informix motif const linear const domain const_delay no breakpoint command alpha surface_sh com_sh surface_handler configuration error global kernel As soon as you call the Prolog system the built in module user becomes the current module All predicates which you enter on the standard input loop break 0 are proved in the context of this module see also chapter 8 The predicate current_default_module 1 allows you to query the current module and set_default_module 1 allows you to define a module of your own as the current module The following predicates are available to you when programming with modules Predicate Purpose Module Predicate Predicate Module calling context Module current_default_module Module current_module Module save_module Module set_default_module Module unload Module Specify the module of the called predicate Specify module context for
135. cedure execution Y a A X X a true B Y M 1 e a l p V p true e a l p e p e h X a e g e h g e h gt M 1 X Y V p e p e h pa e a 1 p Y p X Y true e p e h Y g e g e h true acy legen a calling g A B 3 6 Procedural predicates The concept of a purely logical programming language may look eminently desirable in theory but in practice it would be too limited Even the Prolog input output predicates are difficult to fit into this purely logical scheme because of their side effects Moreover Prolog provides predicates which lead to an irrevocable departure from the purely logical level of programming The programs can then be understood only procedurally The most important members of this category are the predicates 0 cut fail 0 repeat 0 and for 3 These are described in greater detail in the following sections User s Guide 50 IF Prolog V5 3 Database and procedure execution Procedural predicates 3 6 1 The cut 0 The predicate 0 succeeds only once If a cut 0 is called as a subgoal when executing a goal Goal all choice points which were set while executing Goal will be destroyed This has the following effects e When executing Goal Prolog will now be committed to the clause in wh
136. ceptions Contexts goal execution is terminated and the call of catch 3 is replaced by SubstituteGoal The call catch Goal CatchMask SubstituteGoal is therefore identical to context Goal catch CatchMask gt SubstituteGoal 1 You can use the predicate catch 3 or the corresponding context 2 call to resume program execution at a defined point after errors Use is made of the fact that the default exception handling procedure of the Prolog system consists actually of executing a context jump Example If for example a test is to be carried out to determine whether an argument in a predicate is an arithmetic expression and depending on the result of this test a different action is to be executed this may be implemented with catch 3 as follows test Arg catch Result is Arg _ fail first_action Result test Arg other_action Arg i When SubstituteGoal is proved after execution of a context jump the context for the original goal execution is already destroyed since Substi tuteGoal replaces the corresponding context 2 call This means that program cycles cannot occur when context jumps are intercepted by catch 3 or context 2 so this is a very save method of error handling 8 2 Exceptions Exceptions are raised by the Prolog system if errors have occurred However you can also raise exceptions explicitly with the predicate exception Error Clnfo They are handled exactly as if they were exceptions rais
137. clause body is true At the same time the clause head can be seen as the activation point of a procedure which activates further procedures in the clause body The most important operations between subgoals conditions in the clause body are the following operations notation conjunction AND comma disjunction OR semicolon IF THEN arrow gt As an alternative to operator notation using or gt it is also possible but rather inconvenient to specify the clause body in the equivalent normal structure When specified explicitly parentheses effect the precedence and hence the evaluation of the subgoals involved The clause body is true if e for ANDed conjoined conditions comma all conditions have the truth value TRUE e for ORed disjoined conditions semicolon at least one of the conditions has the truth value TRUE e for IF THEN conditions arrow both conditions have the truth value TRUE e If ANDs ORs and IF THENs are mixed the whole evaluation is governed by paren theses and operator precedences The principal functor of the clause body will be determined then both the associated arguments are evaluated and finally the truth value of the whole expression is determined on the basis of one of the three rules described above The exception to this is a combination of an OR operation and an IF THEN operation in the lefthand part of the OR operation This special combinat
138. concept Compiling files 7 5 Compiling and loading files In addition to reading Prolog source texts it is possible to compile the source texts into runtime efficient code The Prolog text which is contained in Filename is there stored in the internal form Filename cmp You may then load this into the database Shell level Prolog level edit lt editor gt Filename user edit Filename compile procmp Filename user compile Filename load prolog 1 Filename user load Filename DATABASE 7 5 1 Compiling Prolog texts and modules Compilation has several advantages over the interpretation of Prolog source texts e smaller runtime stacks e protection against unauthorized copying of readable Prolog sources e capability to restrict access permissions to a predicate e reduced space requirement in the database But there are also some disadvantages e certain restrictions apply to compiled predicates see below in particular the compiled code cannot be modified while a program is running IF Prolog V5 3 141 User s Guide Compiling files Module concept e compiled code is not readable and there are no trace or debug facilities You may mix compiled and interpreted predicates in the database as you wish Contents of a Prolog source file To compile a Prolog source text you should first have to write the text into a file with an editor The file name must
139. containing the test data by entering for example the following predi cate read data user parameters L append _ data File _ L see File read data seen 3 Calling with a start file The start file offers a convenient means of changing default values or defining useful fre quently used predicates For example if the file prorc has the following contents init default editor emacs writeln Term write Term nl then emacs is immediately selected as the file editor for the predicates edit 0 and edit 1 when the Prolog system is started Also the predicate writeln 1 is automatically available it outputs a term and a line feed each time a call is made 4 1 2 Exiting Prolog You can either exit Prolog temporarily or quit Prolog by ending the session Predicates for making a temporary exit Predicate Purpose system Switch to operating system level system Command system Command ExitStatus Execute operating system command system Command Input Output edit Module Edit a file edit Filename Module Edit a file With the predicate system 0 you remain at the operating system level until you press the key combination crrt p or enter the exit command to bring up the Prolog system prompt again With all other predicates control returns automatically to Prolog when execution of the command is completed User s Guide 68 IF Prolog V5 3 Prolog system
140. context action 147 explicitly initiate 149 initiate 148 context free grammar 101 context global action 147 catch 3 147 current signal 3 147 exception 2 147 exception handler 3 147 predicates 147 signal control 2 147 signal_handler 3 148 throw 1 148 control signal handling 156 convert passive goal to active goal 223 coroutine 206 example 207 exit 206 exit and remove 206 generate and start 206 implementation in C 206 link to Prolog 208 operations 206 restart 206 cpred h include file 198 cpro h include file 222 CPU time 69 cputime 69 create active goal 223 context for exception handling 153 passive goal 223 csize 69 current error output 80 current goal 221 current module change 116 query 116 current stream 80 default 82 query 82 set 82 User s Guide 298 IF Prolog V5 3 Index current error alias 82 current input alias 82 current output alias 82 cused 69 cut 91 grammar rules 110 green 52 red 52 data object C 197 convert 197 generate 200 map 198 Prolog 197 data type MODULE 219 t_cursor 219 t_module 219 t_parse_context 219 t_query 219 data types passive C interface 219 database 13 34 87 example 9 extend in a dialog 19 modify 10 query known modules 115 read in a file 12 remove module 146 size 69 used 69 write to file 91 debug 161 compiled predicate 143 debug mode of a predicate 163 query 163 swi
141. criptions in alphabetical order of the built in C functions of the C interface The section 10 3 of this chapter describes how to implement a C device driver The function Cboot provides you the method of introducing your code to all three C interfaces to the Prolog system You can use the skeleton makefile model make to compile and link your code with the Prolog system For information on the steps you must perform to link C and Prolog see page 238 10 1 Active C interface There are two classes of C functions which can be used in Prolog using the active C interface e simple C functions are functions which can be used in Prolog as predicates without backtracking You will find a description of how to write such functions on beginning page 202 e complex C functions are incorporated as Prolog predicates with backtracking The backtracking concept has to be programmed into the execution sequence of the C function The manner in which this is done is defined by the active C interface The conventions which apply are explained in section 10 1 2 The active C interface is designed to reconcile the following differences between the language concepts of C and Prolog e Distinct data objects The data objects in Prolog terms are different from the data types in C int double char e Unification and value assignment The unification of terms and the associated instantiation of variables in Prolog differs significantly from the w
142. ctioning of the concept e generate and start The function is called with the program pointer PP 0 a pointer to the state structure is passed as the second argument The function uses the components of this state structure as local variables e exit without removal The function returns a positive value This is the new program pointer it is stored outside of the function The state structure remains intact e restart The function is called with the most recently stored value of the program pointer PP gt 0 It branches to the corresponding label The local variables from the preceding call are still present in the state structure e exit and remove The function returns a value of 0 The memory space used for the state structure and program pointer is released The first time the above function is called as a coroutine it produces the output 1 Start A count of the number of calls is now recorded in component x of the state structure NameS tate The first restart of the function produces the output 2 Redo The next restart produces 3 Last visit On the next call the instance of the coroutine is removed Linking a coroutine to Prolog The active C interface for Prolog governs the final details for linking a coroutine The link is set up by a call of the function CPRED or MCPREDO in the Cboot function The call arguments inform the Prolog system of the functor of the new predicate its arity the name of the C fu
143. ctor and arity Structure notation where the operator precedes the operands e g A B A compound term Functor Arity Outermost functor in term notation An empty or non empty sequence of clauses in a database defin ing a predicate System state which can be queried and in some cases set by the user A sequence of terms representing clauses and directives Sequence of logical conclusions which produce a statement from facts The paths taken by the system during proving of a goal can be mapped onto a tree structure both the successful paths and also the blind alleys are included in the tree Notation specifying the module in which it was defined Request to the Prolog system to prove one or more goals Relation or object which is based on itself The proof method of Prolog Statement in the form If A then B term in the database in the form B A where A is the rule body and B the rule head A non logical effect of a built in predicate such as input output arithmetic evaluation or database modification Type of memory usage the values are pushed onto the stack and the last value stored is the first value processed again Description of a situation which can be either exactly true or IF Prolog V5 3 293 User s Guide Glossary Appendix 3 static predicate stream structure subgoal term trail stack user predicate unification exactly false Predicate which may not be modified Unique de
144. d to its left Now you are only given the first of the solutions created above the tree therefore has the structure IF Prolog V5 3 53 User s Guide Procedural predicates Database and procedure execution branch 1 twig 1 leaf 1 Tree after rigorous pruning Prune the tree very carefully by applying the cut only to the first clause of the predicate leaf 1 i e to clause 7 leaf leaf1 true Clause 7 new This removes the solutions for leaf 1 which result from clause 8 thus yielding the following form branch 1 branch 2 twig 2 leaf 1 leaf 1 leaf 1 leaf 1 Tree after careful pruning User s Guide 54 IF Prolog V5 3 Database and procedure execution Procedural predicates All branches and twigs are still there Example Red cuts can be used to replace subgoals which may under certain circumstances be difficult to formulate This is illustrated below by a program for the predicate remove Xs X Ys which removes all list elements X from an input list Xs and unifies the remainder with Ys Without using a cut you can create this predicate as follows Program 1 remove X Xs1 X Ys Clause 1 remove Xs1 X Ys remove Y Xs1 X Y Ys1 Clause 1 Y X remove Xs1 X Ys1 remove X Clause 1 3 The condition Y X is essential as a subgoal in clause 1 because otherwise during back tracking or when interchanging the clauses result lists will be created wh
145. debugger User s Guide 268 IF Prolog V5 3 Graphical User Interface MS Windows 11 8 1 Debugger Toolbars Debugger toolbars contain buttons that are shortcuts for many Prolog debugger commands It is easier to use debugger with toolbars You do not need to remember command syntax There are two debugger toolbars The first is the main debugger toolbar It contains commands for debug process control The second toolbar contains command that provide information on debug process See Also Main Debugger Toolbar Debugger Information Toolbar 11 8 2 Accelerator Keys Development user interface defines several key sequences as shortcuts for debugger com mands Key Sequence Description F10 Debugger step Shift F10 Debugger skip Shift F11 Debugger fast skip F5 Debugger continue Shift F5 Debugger abort IF Prolog V5 3 269 User s Guide MS Windows Graphical User Interface 11 8 3 Breakpoint Dialog Breakpoint dialog displays list of all current breakpoints They can be activated deacti vated removed or a new breakpoint can be set RE i Remove Remove All Activate Activate Al Deactivate Deactivate All Close Help 000001 active 000002 active Interacti Predicate Indicator Clause Subgoal or Y interactive hanoi clear stone 3 1 2 interactive hanoi gotoxy 2 none none call li Figure Breakpoint dialog The breakpoint list has several fields that describe
146. dual clauses or predicates can be deleted with retract 1 2 or abolish 1 2 User s Guide 90 IF Prolog V5 3 Input output interface Process management 5 3 3 Writing the database to a file You can store clauses read into the database by a user during a Prolog session with the predicates listing 0 1 Note that listing 0 1 cause the output to be written on the current output stream see page 83 If the output is to be directed to a file the current output stream must be first redefined accordingly see page 85 Example user open file clausefile write Z set_output Z listing set_output screen close Z yes The predicate listing 1 allows you to selectively write clauses with a specific functor and arity 5 4 Process management and communication The Prolog system offers a set of predicates for process management and communication with signals pipes and sockets This section provides a tabular overview of these predicates and an example of process communication using pipes and sockets The predicates are described in detail in the F Prolog Reference Manual 1 There you will also find further short examples 5 4 1 Predicates for process management The Prolog system provides the following predicates for process management IF Prolog V5 3 91 User s Guide Process management Input output interface Predicate Purpose system Start the operating system command interpreter sys
147. e 130 reexport 2 directive 131 remove module from database 146 representation_error error type 151 reset environment variable 75 resource_error error type 151 return to Prolog 68 rule 10 13 28 rules for exception handling 154 runtime environment 283 sample session 5 screen 80 alias name 81 search path set 65 selective import directive 134 server 96 set current stream 82 Prolog flag 70 IF Prolog V5 3 305 User s Guide Index set environment variable 75 settings for debugger 169 SHELL environment variable 75 signal abort 155 alarm 155 context action 147 interrupt 155 pipe 155 query mode 155 quit 155 termination 155 user 1 155 user 2 155 signal handling 154 active 158 global control 156 in special context 158 passive 157 signal management disable 64 signals 154 simple C functions 32 196 201 example 202 size of dynamic memory blocks 63 total memory 63 SJIS language specific charset 62 socket 95 generic input output 97 99 input output 97 bind 97 client 96 connect 97 create 96 device 79 domain 96 listen 97 server 96 type 96 specified stream 80 stack for constraint data 63 for extended terms 64 for global data 63 for local data 63 size 69 used 69 standard device 79 ISO 62 standard error output 80 standard input 20 79 standard output 20 80 standard stream 79 81 start and end processing debugger 1
148. e Prolog language For a sample session illustrating the various ways of creating a database and explaining the predicates used for database manipulation see chapter 2 3 1 Clauses In Prolog a clause represents a statement about objects and relations between objects Clauses represent the knowledge which the Prolog system can draw upon when processing a query The precise syntax of clauses is defined in the section on Syntax in the IF Prolog Reference Manual 1 If clauses are to be read in from a file or from the keyboard using consult 1 or reconsult 1 each clause must end with a period and be separated from the next clause by a delimiter This period does not itself form part of the clause syntax but serves the Prolog system as an additional delimiter during syntactical analysis When the clauses in the database are output with listing 0 or listing 1 a period is again added to the end of each clause This improves readability and has the added advantage that clauses output to a file in this way can be read back in again without the need for further manipulations see also the section 5 3 3 Writing the database to a file There are two kinds of clauses rules and facts 27 Rules Database and procedure execution 3 1 1 Rules A rule is a Prolog clause containing a clause body A rule represents conditional knowledge i e it is an implication in the form clause head lt clause body the clause head is true if the
149. e error context and generates an exception line 9 Generating and testing an extended Prolog system Once you have generated and called the extended Prolog system as described in section 10 4 you can test the new predicate user len_pred hello L L 5 yes User s Guide 204 IF Prolog V5 3 Active C interface Complex C functions 10 1 2 Complex C functions Basically the following differences exist between predicates with backtracking and C func tions Predicate with backtracking C function Yields different solutions in the course of back Always yields the same solution tracking when the same call arguments are used Stores information regarding the point reached Following termination of the func during the last activation of the predicate tion all local information is lost Because of these differences simple C functions are not suitable for implementation of pred icates with backtracking Predicates with backtracking may be implemented in a C context using the concept of coroutines In order to clarify how this works the following sections adopt a step by step approach to show how e predicates with backtracking can be mapped onto the box model the box model can be simulated using coroutines e coroutines can be implemented in C e a coroutine is linked to Prolog as a predicate with backtracking This completes the description of the conventions of the active C
150. e initiated interactively with a semicolon even if terminals are not used for standard input output root pathname Sets the pathname under which the files of the Prolog runtime environ sp path stream xenv ment can be accessed This option takes precedence over the environment variable PROROOT Sets the initial value of the Prolog search path flag to the list specified in path Pathnames are separated by means of a colon UNIX or semicolon DOS Prolog is started on an alpha screen no link to OSF Motif The de bugger is started without a graphical user interface see also debug_ config 3 Starts the graphical development environment under OSF Motif Identifies the end of the system arguments All following arguments are considered to be user arguments even if the name is the same as that of a system argument The end of the system arguments is also considered to have been reached if the command line contains an argument which is not a system argu ment The following options are always evaluated last regardless of their position in the call line c filename 1 filename The Prolog source file filename is read into the database with consult 1 after the start file if there is one see st option The system searches first the current directory and then the directories specified under search_path and proceeds in the following order e for a file filename e then if none is found for a file
151. e module body can be terminated by means of an optional end_module directive The end_module 1 directive must contain the name of the current module as its argument The module interface is introduced by the module directive and constitutes the public part of a module Predicates defined in the module body can be exported and thus imported into other modules module Module export PredicateList meta PredicateList reexport Module reexport PredicateList from Other Module end module end module Module User s Guide 126 IF Prolog V5 3 Module interface Module is an atom which specifies the name of the module The name must be unique system wide i e there must not be more than one module with the same name If you have already defined a module with the name Module it is redefined and all module objects and declarations which belonged to the previous module are deleted Redefining the mod ule interface automatically deletes an existing module body You cannot redefine system modules The module directive creates a new module The module interface ends with the following module begin module or end module directive or at the end of the logical file A logical file may contain more than one module interface A module interface however must be completely defined within a single logical file i If you have implemented C predicates for a module they cannot be accessed until after
152. e module interface of the module Module by means of export reexport or meta directives If you have declared a predicate as private with an export meta dynamic or private directive or reexported a predicate you can no longer import it from a different module Any attempt to import it will not be treated as an error instead the system will issue a warning If you have already imported a predicate from a different module it will be imported again This means that previous import declarations may be partially overlaid by subsequent ones However the Prolog system issues a warning to draw your attention to the redeclaration of a predicate i In this respect the general import directive differs from the selective import directive With the selective import directive predicates which have already been imported can be overlaid The general import direc tive on the other hand imports only predicates with indicators which have been neither declared as local nor imported from a different module Example In this example two module interfaces are defined the predicate sort 2 is exported in both interfaces In the module body of the module operation the module quicksort is first imported Consequently the built in standard predicate sort 2 is implicitly overlaid by the definition from the module quicksort The predicate permute 2 is then imported from the module utilities As a result all the built in system predicates are visi
153. e of goals that has led to the exception The elements of the list are predicate in dicators Functor Arity Goals that have been re moved due to tail recursion optimization normally the last subgoal in a clause body are not included in the list double_quotes codes Characters inside double quotes are to be inter preted as a list of character codes when reading a term e g with read 1 2 chars Characters inside double quotes are to be inter preted as a list of characters when reading a term e g with read 1 2 atom Characters inside double quotes are to be inter preted as an atom when reading a term e g with read 1 2 extended_syntax on Cyclic terms can be entered using the notation Variable Term When reading such a cyclic term Variable is unified with Term The above notation is also used by output of cyclic terms see 2 off Cyclic terms can be entered in the above notation but Variable is not unified with Term IF Prolog V5 3 263 User s Guide MS Windows Graphical User Interface nested_comments off Nested comments are not allowed in a Prolog text Begin of a comment inside a comment is ignored which normally leads to a syntax error on Nested comments are accepted in a Prolog text write_depth 10 Maximal depth of terms output in the Prolog toplevel see break 0 This applies to variable instantiations of a query and to exception messages The predicate write_formatted 2 3
154. e of the passive C interface This is followed by a description of the individual steps involved when using it Reference is made to this framework and possible extensions to it are shown Examples are provided to illustrate the description IF Prolog V5 3 221 User s Guide Application Passive C interface Simple framework for the passive C interface The file PROROOT c main c contains a framework program for using the passive C in terface You can copy this and modify it to suit your own application include lt stdio h gt include lt stdlib h gt include cpro h int main int argc const char argv t_parse_context context If several goals are to be t_cursor cursor stored several variables of x this type must be defined if InitProloglargc argv stdin stdout stderr if PrologInitError NULL fprintf stderr sYn PrologInitError Error message on initialization error gt else if ParseProlog program amp context amp amp cursor PrologOpen SystemModule context pc_term context pc_varlist NULL 1 void PrologFetch cursor The values of the variables in the goal can now be used elsewhere in the program Errors detected during call of the goal can be reported at PrologClose cursor this point in the program EndProlog Release memory space return 0 fprintf stderr could not initialize Prolog n return 1
155. eate this structure with the property static const and name it In this structure you must specify everything that the new device driver is to do You link the new device driver into a new Prolog system by means of the function Cboot See page 238 for information on creating a new Prolog system Example include device h static const t_device file_device F void Cboot void DeviceCreate amp file device User s Guide 234 IF Prolog V5 3 Device driver functions Flushing Prolog buffers The function StreamFlush flushes the buffers provided for the C interface by the Prolog system If the buffers have been flushed the function returns the value TRUE otherwise FALSE include device h extern BOOLEAN StreamFlush t_stream stream long number t_stream stream is a pointer to the stream which is passed to a control function see ctl_handler as the first argument long number This variable contains the number of characters which were in the buffer prior to flushing Querying stream mode The function StreamMode returns as a flag array the mode of a stream see also open 3 4 include device h extern t_iomode StreamMode t_stream stream t_stream stream is the stream whose mode is to be queried Example This example implements a simple device driver which can support UNIX files and which in addition to the functions for opening closing reading and writing contains two contr
156. ed by the Prolog system If one of the active contexts provides information on how to handle the exception exception handling is executed without destroying the current proof tree in the context Exception handling replaces the goal which initiated the exception At the end of the exception han dling the goal execution is continued normally unless a context jump is executed during the exception handling If no active context is found which can handle the exception standard exception handling is executed by the Prolog system Standard exception handling performs a context jump Each exception is described by the type of the error and possibly additional information User s Guide 150 IF Prolog V5 3 Contexts Exceptions Error types The error type Error is an atom which describes an error more precisely The following error types are used by the Prolog system Error type Meaning domain_error The argument does not lie in the required value range evaluation error An arithmetic expression causes an error during evaluation existence_error An object to which an operation is to be applied is not present instantiation error An argument or subargument is a variable permission error The execution of an operation for an object is not permitted representation error The implementation dependent restriction has been exceeded resource_error The resources are inadequate for further processing syntax_error A character strin
157. ed to the functions in dev_control The predicates for which you may define control functions are given in the table in section 10 3 2 t_control has the following components typedef struct STRING ctl_name ARITY ctl_arity BOOLEAN ctl_handler t_stream t_iounit TERM t_control STRING ctl_name Functor of the control information as a string You mark the end of the array containing control functions with a NULL pointer in this component ARITY ctl_arity Arity of the control information BOOLEAN xctl handler t stream stream t_iounit deviceunit TERM term Control function which is to perform a particular function on a stream or on an input output unit It takes as its arguments a stream and its associated input output unit The control information is passed by Prolog as an array of terms You can access the terms using the macro CTLARG n If successful the function ctl_handler returns the value TRUE otherwise FALSE If you do not wish to provide any control functions you may also enter the NULL pointer in the structure t_device const t control dev_class_control dev_class_control points to an array of type t control in which you can place an arbitrary set of class specific control functions These class specific control functions which are not dependent on any concrete stream enable you to use the built in predicates device_control 2 and current device control 3 to access the device driver which you ha
158. eger maxint Largest representable integer If the IF Prolog system has been configured with multiple precision integer support a much wider range of integers is available However the functions above return always the smallest or largest integer that can be represented as one word on the underlying hardware 4 2 2 Querying system properties The predicate current_prolog_flag 2 allows you to query the flags defined in the Prolog system user current_prolog_flag Flag Value The following Prolog flags may be queried and modified Flag Meaning Default value backtrace Maximal length of backtrace list on exception 10 char_conversion Table controlled character conversion on consult Output message on loading Prolog text notify debug Implicitly declare predicate as dynamic off double_quotes Handling of double quotes codes extended_syntax Input of cyclic terms off nested_comments Allow nested comments in Prolog text off prompt Input prompt and interactive backtracking even off if standard input output is not terminal read_error Handling of syntax error during analysis with error read_term 2 3 etc report_read_error Handling of syntax error during analysis with on parse_atom 6 search_path Input files are sought in the list of O specified directories unknown Handling of undefined predicates error warnings Output warnings on possible errors with on consult 1 etc write_depth Maximal depth of terms output in Prolog top
159. elect 3 5 4 5 Predicates for computer network information The Prolog system provides the following predicates for accessing information on computer networks IF Prolog V5 3 97 User s Guide Process management Input output interface Predicate Purpose current host Host Query names of current host host addr Host Addr Query host address host_addr Host Addr Query host name net service Service Protocol Port Query communication service The information provided is extracted either from Network Information Service NIS Do main Name Service DNS or local operating system databases More information can be found in the operating system documentation in sections dealing with networking and communication Example for client communication using sockets The following Prolog program shows simple client requesting the time service from a server this is a standard service on networked computers The Prolog system must be configured with Multiple Precision Integer Support for correct arithmetics without overflow gettime Host gettime Host GMT1970 localtime GMT1970 Year Month Day _DoW _DoY Hrs Min Sec write_formatted 02d 02d 4d 02d 02d 02d Day Month Year Hrs Min Sec gettime Host GMT1970 net_service time tcp TimePort Well known number 37 host_addr Host _ h only to check Host socket internet stream Socket socket_connect S
160. ell call Call Prolog goal unify Instantiate variable interrupt Interrupt current goal execution kill Terminate Prolog session command Enter and execute debugger command quit Exit debugger When you click on a menu item in the Command menu the associated debugger command is executed Some commands are followed by three periods e g error which indicate that you must enter further parameters for the command in a separate window If an error occurs here for example if you enter an incorrect command you will receive an error message in the form of the following dialog box You should acknowledge this by clicking on the OK field In addition the exception is also output in the bottom field of the main window Prolog Error wee EXCEPTION existence_error clause back_clause gt gt gt command back_clause Error message window 9 5 2 Setting ports The Ports menu allows you to change the default settings for the debugger and to set implicit breakpoints at the ports of the box model The following diagram illustrates the default settings for the activated ports IF Prolog V5 3 185 User s Guide OSF Motif interface Debugging Port Configuration call exit redo fail suspend wakeup error O trymatch O failmatch O enterbody HO exitbody HO redobody CO failbody Setting implicit breakpoints When you click on a button with the mouse pointer you activate or deactivate an implicit breakp
161. emoved The section of the program to be executed as an epilog following the instance s removal must be placed directly between EPILOG and END Source code define EPILOG FAIL _epilog END Must be called as the last macro in the complex C function The END call ensures that the function returns FALSE the predicate fails if it was not previously exited using DETACH Source code define END FAIL IF Prolog V5 3 213 User s Guide Linking a coroutine Active C interface Example The macros and functions described above can be used to construct the following framework for facilitating error free production of the source text for a complex C function without epilog tinclude cpred h typedef struct 1 State COROUTINE Funcname State StatePtr RESUME ProgPtr1 Label1 RESUME ProgPtr2 Label2 RESUME ProgPtrN LabelN BEGIN Start of actual program code of the function including FAIL DETACH ProgPtr1 Label1 DETACH ProgPtr2 Label2 DETACH ProgPtrN LabelN End of the actual program code END void Cboot void CPRED Predname Arity Funcname sizeof State Example As an example the count predicate for_pred Minimum Counter Maximum is to be implemented its purpose is to unify Counter with the value of Minimum on the first User s Guide 214 IF Prolog V5 3 Active C interface Linking a coroutine call of the function and then unify Counter with the next hig
162. ent variable 75 editor 10 call 88 call during a Prolog session 88 change 89 errors 89 exception 89 end_module directive 126 enter clause in a dialog 19 environment variable DISPLAY 75 EDITOR 75 PRORC 75 PROROOT 75 query 75 reset 75 set 75 SHELL 75 environment variables 74 epilog 205 error context 201 error message window 185 error messages 80 error term exception 151 error type 151 domain_error 151 evaluation_error 151 existence_error 151 instantiation error 151 permission error 151 representation error 151 resource_error 151 syntax_error 151 system_error 151 type error 151 esize 69 EUC language specific charset 62 eused 69 evaluation error error type 151 exception 150 additional information 151 context action 147 error term 151 error type 151 raise 152 exception handling 150 active C interface 201 202 create context 153 rules 154 exceptions during loading 146 execute a goal 44 execute subgoals 43 existence_error User s Guide 300 IF Prolog V5 3 Index error type 151 exit debugger temporarily 170 exit Prolog 6 export predicate 115 directive 127 metapredicate 128 predicate 127 extended Prolog system generate 202 extended term stack size 69 used 69 extended terms stack 64 extension memory area 73 fact 10 13 29 failing to prove a goal 45 field for ancestor goals alphanumeric user interface 179
163. epsi fail mother gregor pepsi enterbody ancestorigregor pepsi call father gregor Z enterbody father lt gregor hans call true exit true exitbody father gregor hans gt exit father gregor hans call ancestor hans pepsi enterbody ancestor hans pepsi call father hans pepsi fail father hans pepsi enterbody ancestor hans pepsi call mother lt hans pepsi fail mother hans pepsi enterbody ancestor hans pepsi call father lt hans Z enterbody father hans olaf call true exit true exitbody fatherihans olaf exit fatherihans olaf call ancestor olaf pepsi rr e ee ee ee AMOMOOMHAAHAAnnninseaninnnsFrFrA Pals NRORRORORRORRONRORRORORER Trace outputs 9 5 5 Displaying ancestors The menu Ancestors activates a window in which the dynamic chain of Prolog goals is displayed The contents of this window are not automatically refreshed but only updated with new information whenever the menu item is activated Prolog Ancestors m O Command calllancestor max pepsi user ancestor imax pepsi ancestor gregor pepsi ancestor hans pepsi ancestor olaf pepsi father olaf Z gt Dynamic call chain 9 5 6 Debugger log The contents of the text fields in the Prolog Debugger window are stored internally by the debugger The History menu allows you to take another look at the most recent outputs User s Guide 190 IF Prolog V5 3 Debugging Deb
164. er of ANDed conditions in a clause body These subboxes are themselves goal boxes with corresponding ports and an internal structure They are created in the course of goal execution and may be destroyed later User s Guide 40 IF Prolog V5 3 Database and procedure execution Procedure execution Subgoal boxes In the box model a fact is treated as a special case of a rule The rightmost part of the clause box contains the box belonging to true 0 as its only subbox The creation of a goal box belonging to the matching predicate corresponds to the first call of a sub goal This goal box contains all clause boxes belonging to the predicate The path leads from the call port of the goal box to the trymatch entrance of its first clause box This clause box is marked with the character Calling a goal IF Prolog V5 3 41 User s Guide Procedure execution Database and procedure execution i The Prolog system enters all the available clauses in the goal box at the time of the first call call port Modifications to the clause set of the predicate after the creation of the goal box have no effect on this goal box Therefore if you delete retract predicates or add assert predicates clauses after the goal box was created this has no effect on existing goal boxes This is referred to as the logic update viewof the database 3 5 3 Uni
165. erating system and know how to use one of the editors installed on your system If you wish to work with the C language interface you should also have experience of the C programming language For a description of the elements of the Prolog language and the built in predicates refer to the IF Prolog Reference Manual 1 The interfaces of the Prolog system to the OSF Motif window management system and to the Informix database system including their associated built in predicates are dealt with in the manuals F Prolog OSF Motif Interface 3 and IF Prolog Informix Interface 4 The IF Prolog Constraints Package manual 5 explains how to work with constraints for variables in IF Prolog The IF Prolog Quick Reference 6 contains all the built in predicates arranged into groups by function and accompanied by a brief description Preface Summary of contents The IF Prolog User s Guide describes how to work with IF Prolog The F Prolog User s Guide describes all the concepts on which the Prolog system is based The IF Prolog Reference Manual 1 is intended purely as a source of reference in addition to the syntax elements it also contains a description of all the built in predicates and C functions and the debugger commands The second and third chapter in this manual are an introduction for absolute novices to Prolog If you are already familiar with Prolog feel free to skip these chapters The next chapters describe the organiza
166. erating system is activated for all signals If the Prolog input loop break 0 is started handling of the interrupt signal is set to on User s Guide 156 IF Prolog V5 3 Contexts Passive signal handling 8 3 2 Passive signal handling If you activate passive signal handling for a signal with signal control 2 and this sig nal arrives then an internal counter for the arrival of this signal is incremented by 1 and processing continues You may query or clear this counter with signal control 2 Example The following short example implements a reaction test reaction for 1 _ 10000 fail reaction signal_control interrupt off signal_control interrupt clear write Press interrupt key flush_output for 1 Cnt 30000 signal_control interrupt received nl write Result write Cnt nl reaction nl write too slow nl The first of the two clauses is used as a header At the end of this loop Press interrupt key is displayed and you now have to hit the interrupt key e g per as quickly as possible The smaller the result value displayed the faster your reaction user reaction Press interrupt key DEL Result 1269 yes IF Prolog V5 3 157 User s Guide Active signal handling Contexts i In certain cases the arrival of signals can lead to irreparable damage There are a number of system calls which can be i
167. es the actual tasks involved The central component of this function is a switch statement which branches as necessary so that the correct entry point is reached at a start or restart e definition of a state structure The local variables of a coroutine instance are stored outside the function in the state structure so that they are not lost when the coroutine is exited e an int variable PP program pointer The program pointer contains information about the entry point for the start or restart Example typedef struct int x NameState int NameFunc int PP void state NameState StatePtr NameState state switch PP case 0 break case 1 goto Labell case 2 goto Label2 case 3 goto Label3 default return 0 StatePtr gt x 1 printf d StatePtr gt x printf Start n return 1 Label1 StatePtr gt x printf d StatePtr gt x printf Redo n return 2 Label2 StatePtr gt x printf d StatePtr gt x printf Last visit n return 3 Label3 return 0 start PP 0 lt start gt exit lt restart PP 1 gt exit lt restart PP 2 gt exit lt restart PP 3 IF Prolog V5 3 207 User s Guide Linking a coroutine Active C interface exit and remove x This example offers an easy to follow guide to the general fun
168. es which predicates are defined in target file where it is implemented in which lines Options set in this property page are used only when Cross Reference tool is invoked by menu or toolbar command They are not used when the Cross Reference tool is run outside the IF Prolog development GUI Option Description a Cross Reference output is in the alphabetic order predicates are ordered alphabetically u Cross Reference output is in the order of occurrence in files predicates are not ordered e Cross Reference output contains only exported predicates f Cross Reference output is not limited to 80 characters IF Prolog V5 3 267 User s Guide MS Windows Graphical User Interface 11 8 Debugger Support Graphical user interface supports debugging by several features e Debugger Toolbars e Accelerator Keys e Breakpoint Dialog This is the IF Prolog window with activated debugger FE IF Prolog Y5 2 C prolog Demos Standard Hanoi_pro File Edit Activefile View Debug Options Help 050 87 220 37 aa e el eh TEE Ou o alerlolala AAA elo pajaja jo meesal 13 8118 pj initialise 5 CALL gt debug mode inform 2 Off clear_screen abolish tower 3 abolish max 1 Ni is 5 2 asserta max N1 set_it 5 1 flush_output clear it 5 1 centre flush output clear it 5 1 right flush_output draw_base flush_output For Help press Fl Figure IF Prolog window with activated
169. evice driver for interprocess communication sockets User s Guide 228 IF Prolog V5 3 C interface C device drivers BOOLEAN xdev open STRING Name t_iomode mode t_iounit deviceunit dev_open is a pointer to a function of the driver which opens a stream If a corresponding stream can be opened dev_open must return TRUE otherwise it returns FALSE If no stream is to be opened you may also specify the NULL pointer here You call the function later implicitly with the predicates open 3 4 open Device Name Mode Stream open Device Name Mode Stream Options The arguments of the predicates open 3 4 are then mapped to the parameters of the function dev_open where Name is passed as a string and the arguments in Mode and Options are passed as flags STRING Name Name is a the name of an input output stream t_iomode mode This parameter determines the mode in which the stream is to be opened and also the options if defined see also the F Prolog Reference Manual 1 open 4 predicate The flags in mode can be modified by the device driver The following flags are available S IO READ The stream is opened for reading S IO WRITE The stream is opened for writing the file pointer is set to the start of the file S_IO_APPEND The stream is opened for writing the file pointer is set to the end of the file S_IO_BINARY The stream is opened for binary data S_IO_REPOSITION You can query the position in the inpu
170. f double_quotes codes float_max 1 79769e 308 float_min 4 94066e 324 integer_rounding function toward zero iso off max arity 127 max integer 2147483647 min integer 2147483648 nested comments notty off off IF Prolog V5 3 71 User s Guide System information Prolog system prompt off read error error report read error on signal on search path unknown error warnings on write depth 10 no 4 2 3 Querying other information The following predicates also provide information about the system Predicate Purpose system_name SystemName Query operating system system_hostid Hostid Query machine identification prolog_version Version Query Prolog version prolog_serial Seria Query Prolog serial number proroot Path Query path of Prolog system 4 2 4 Querying memory management You can control or check memory management of the Prolog system by using the following predicates Predicate Purpose current memory management Area Parameters Value garbage_collection Area set_memory_management Area Parameters Value statistics statistics Type ResultList For a detailed description of the predicates Query memory management parameters Activate garbage collection Set memory management parameters Output system statistics Query system statistics refer to the IF Prolog Reference Manual 1 IF Prolog manage
171. f TARGET USEROBJS SAVESTATE A new Prolog system application has been defined in this makefile e With the following system call from the shell level you generate a new Prolog system make f model make e You can then call the extended Prolog system from the shell level as follows using the name defined in the makefile application application If you have set the necessary options you can also specify parameters for the Prolog system in the call see section 4 1 1 On starting the Prolog system calls the Cboot function specified by you and connects the C functions as predicates to Prolog A newly incorporated predicate now behaves like a built in predicate of the corresponding type Should you wish to connect further C functions at some later time you must extend the function Cboot and your makefile appropriately and generate a new extended Prolog system IF Prolog V5 3 241 User s Guide Generating an extended Prolog system C interfaces User s Guide 242 IF Prolog V5 3 Chapter 11 Graphical User Interface for MS Windows 11 1 The IF Prolog Graphical User Interface IF Prolog provides a graphical user interface under MS Windows 795 798 NT and future releases of MS Windows operating systems The following section describes the user interface features and how to use it for developing IF Prolog applications After starting the IF Prolog application the main windo
172. f_file User s Guide 100 IF Prolog V5 3 Chapter 6 The Prolog grammar rules This section deals with the syntax of the Prolog grammar rules At the same time it explains how the Prolog system converts these rules into Prolog clauses during consultation and how these clauses can then be used for the syntactical analysis of sentences in the corresponding language Finally the complete syntax of the production rules is defined by reference to the associated grammar 6 1 Context free grammar An important step in the automatic processing of natural or formal languages consists in breaking down the input stream of words or characters into sentences in the relevant language and then analyzing those sentences syntactically This can be done for each language on the basis of a grammar which defines e The set of words tokens in the language These are identified in the grammar by means of terminal symbols terminals e The various categories of token sequences i e phrases which occur when sentences are formed and which are part of the input stream These are indicated by non terminal symbols non terminals e The internal structure of phrases A set of rules called production rules is provided for this purpose e A start symbol from the set of non terminals Whereas a grammar of this sort must be constructed inductively for natural languages this is usually only partly successful a formal language command language prog
173. face Command Short Meaning form help Display help field in output window view_and_box amp Display goal box in output window view_or_box HE Display clause box in output window view_previous_screen lt Display previous goal or clause box view_last_screen lt lt Display last goal or clause box view_next_screen gt Display next goal or clause box view_first_screen gt gt Display first goal or clause box view_history vh Display current goal or clause box view_stops vs Display explicit breakpoints view _trace vt Display trace field Scroll forward line by line in the information field Scroll forward page by page in the information field Scroll back line by line in the information field s Scroll back page by page in the information field snapshot sn Output current information field to a file The information fields may be longer but not wider than the output window In the infor mation area the debugger displays if the field extends beyond the lower window border and if it extends beyond the upper window border The commands enable you to scroll in the information fields You do not need to switch to a special mode to do so The fields showing parts of the proof tree AND BOX OR BOX are stored in a history list the length of which you can change The commands lt lt lt gt gt gt enable you to scroll in these information fields You do not need to switch to a special mode to do so Help f
174. fication attempt The Prolog system now checks whether the head of the current clause can be unified with the goal If it can the subgoals of the clause may now be processed If not Prolog must continue the search for a unifiable clause Representation in the box model The starting point is the trymatch port e If unification is successful Prolog proceeds to the enterbody port e If not Prolog leaves the clause box through the failmatch exit and destroys the clause box Ti II Unification attempt 3 5 4 Searching for the next clause If a goal cannot be successfully proved with the currently selected clause of a predicate the system attempts to prove it with the next clause of the same predicate It locates this clause using the previously set choice point This will take place in two cases e when the clause head of the currently selected clause cannot be unified with the goal term or e if any of the subgoals of the currently selected clause cannot be proved User s Guide 42 IF Prolog V5 3 Database and procedure execution Procedure execution If the selected clause is not the final clause in the predicate a choice point is set Representation in the box model Prolog can proceed along two paths from the currently selected clause box marked with to the trymatch entrance of the next clause box e from the failmatch exit of the marked box e from the failbody exit of the marked box The
175. fies the name of the module whose body is to be defined For a module body to be defined the associated module interface must exist i e you must always load the module interface of a module before its body If the body of a module has already been declared all the module objects and private declarations of the module User s Guide 132 IF Prolog V5 3 Module body Importing predicates are deleted before the body is reloaded The module interface itself is not affected by the begin module directive The begin module directive implicitly causes all the system predicates from the system module and all the built in operators to be made visible If in the module interface you have declared a predicate whose indicator is identical to a system predicate the corresponding system predicate is not made visible These predicates can be overlaid with a local definition i You cannot redefine the body of a system module The prefix system is also reserved for system module names and should not be used for module names see also page 115 Declaration part The declaration part of the module body contains only directives which make predicates from different modules visible define operators or declare private i e local predicates and their properties The individual directives and their application are described in detail below Declarations in the module body must not contradict the module interface in any way For example if
176. filename pro The contents of filename must be a syntactically correct Prolog source text The compiled Prolog file filename is loaded into the database after the start file if there is one see st option The system searches first the current directory and then the directories specified under search_path and proceeds in the following order e If the file filename contains the suffix cmp it is loaded into the database e Otherwise the system searches for the file filename cmp and loads it IF Prolog V5 3 65 User s Guide Calling Prolog Prolog system i You may also specify the options c filename and 1 filename more than once with different files If you do they are evaluated in the specified order r file The system status is loaded from file which was saved with save_ system 1 If Prolog cannot open file or if file does not have the suffix pst the suffix is appended and the name thus produced is used st startfile If a hyphen is specified for startfile then no start file is loaded or con sulted Otherwise the specified start file will be the first file to be loaded or consulted when the Prolog system is called The system searches the current directory for the file in the following order e If startfile has the suffix cmp the system loads this file e If startfile has the suffix pro the system consults this file e If the name has no suffix the system first searches for and load
177. fined them They are identified by the text Body after the argument list The call mode specifies the instantiation of an argument at the time of the call In front of each argument there is a sign or to indicate the call type of the argument as follows The argument is a pure input parameter The current parameter specified in the call must be of the prescribed type and any uninstantiated variables contained in this parameter are not instantiated in the call The argument is an input parameter The current parameter specified in the call must be of the prescribed type Any uninstantiated variables contained in this parameter may be instantiated in the call When the argument is an atomic term there is no difference between the modes and The mode is therefore used only when the argument may be a compound term User s Guide 32 IF Prolog V5 3 Database and procedure execution Predicates The argument is an input output parameter The current parameter must be either a variable or a term of the prescribed type In the course of the execution of the predicate this parameter is unified If this unification is not successful then the entire predicate call will fail Any uninstantiated variables contained in this parameter may be instantiated in the call The argument is a pure output parameter The current parameter must be an uninstan tiated variable If the predicate succeed
178. ft F5 Ctrl Insert Ctrl C Shift Ins Ctrl V Shift Delete Ctrl X Ctrl Up Ctrl Down Home Ctrl Home End Ctrl End Left Right Up Down Esc EOF end_of_file 0 New file Open file in default editor Interrupt goal execution Debugger step Debugger skip Debugger fast skip Debugger continue Debugger abort Edit copy Edit copy Edit paste Edit paste Edit cut Edit cut Move back in the command his tory Move forward in the command history Go to the beginning of the cur rent input line Go to the beginning of the cur rent input Go to the end of the current in put line Go to the end of the current in put Go left in the current input Go right in the current input Go up in the current input Go down in the current input Erase current input development GUI only development GUI only development GUI only development GUI only debugger command development GUI only debugger command development GUI only debugger command development GUI only debugger command development GUI only debugger command development GUI only User s Guide 254 IF Prolog V5 3 Graphical User Interface MS Windows Key Sequence Description Note Del Delete character Backspace Delete left character F1 Help development GUI only 11 5 Status Bar This is the IF Prolog Main Window Status Bar l For Help press Fl Figure IF Prolog Main Window Status
179. g goals Representation in the box model Three paths begin at the redo entrance of a goal box e If the goal box still contains a marked clause box the clause box is entered through the redobody entrance e If the uppermost clause box is not marked Prolog enters through its trymatch en trance e If the goal box is empty Prolog leaves through the fail exit IF Prolog V5 3 47 User s Guide Procedure execution Database and procedure execution Redoing a goal 3 5 10 Redoing a clause If during backtracking there are still choice points in the subgoals belonging to a clause Prolog returns to these choice points Representation in the box model The only path from the redobody entrance of a clause box leads to the redo entrance of the rightmost goal box contained therein Redoing a clause Example Extend the above database with the clauses e a l p true e p e h true e g e h true by adding the clause g X Y e X M V e Y M V X Y To prove the goal g A B Prolog now searches in the database for the first clause head with arity 2 and functor g which is unifiable with the query The search sequence thus corresponds to the sequence of the clauses in the database The located clause has the head g X Y unification causes the following variable instantia tions to take place X A Y B User s Guide 48 IF Prolog
180. g which is to be read in as a term does not conform to the Prolog syntax system_error An operating system error has occurred type_error An argument or subargument does not conform to the expected type With most of the error types the Prolog system also supplies information which specify the error cause more precisely and the culprit error term which caused the error Additional information Info is a list whose elements have the form InfoName Value and which supplies additional error related information InfoName is an atom which determines the type of the additional information The Prolog system can supply the following additional information Additional in Meaning formation argno Argument number relating to the current Prolog goal which caused the error column Column where a syntax error has occurred culprit Term which caused the error error Atom indicating the type of a system error e g fork_failed goal Goal which initiated the error interrupted If a predicate implemented in C was interrupted by a signal this infor mation is contained here line Line where a syntax error has occurred signal Name of the signal syntax_error Type of a syntax error valid_type Expected type of a term IF Prolog V5 3 151 User s Guide Exceptions Contexts The order of the additional information is not defined You can also expand the Info list with additional information of your own However you should only
181. generally atoms non_terminal stands for a particular category of token sequences non_terminal may be Prolog atoms Prolog normal structures and expressions but not lists Rules 8 and 10 imply that instead of the operator notation given in 7 or 9 for conjunction or disjunction it is also possible to use normal structure notation with the functors and The bracketing option described in rule 13 is employed with operators and to influence the precedence of the terms accordingly In this case the same rules apply as with normal Prolog terms see IF Prolog Reference Manual 1 chapter on syntax As with Prolog clauses disjunction should be avoided wherever possible for the sake of clarity Example sentence gt sub_sentence sub_sentence and sentence This grammar rule is compiled into the following Prolog clause which is added to the database IF Prolog V5 3 113 User s Guide Production rule syntax Prolog grammar rules sentence A B sub sentence A B sub_sentence A C C and D sentence D B If the rule is split up as follows sentence gt sub_sentence sentence gt sub_sentence and sentence the clauses are more easily read sentence A B sub_sentence A B sentence A B sub_sentence A C C and D sentence D B User s Guide 114 IF Prolog V5 3 Chapter 7 The Prolog module concept IF Prolog featu
182. goal but does not allow you to intervene in the execution of the search 9 2 1 Predicates for the trace The following table summarizes all predicates which are relevant to tracing User s Guide 164 IF Prolog V5 3 Debugging Tracing Predicate Purpose debug_mode Predicate Before After trace Goal trace_begin trace_config Parameter Old New trace end trace_output CallNumber Port Goal Query and set the debug mode of a predicate Activate tracing for a goal User defined tracer initialization User defined tracer initialization User defined termination of the tracer User defined output predicate for the tracer A VariableNames The predicates are described in full in the IF Prolog Reference Manual 1 However the main features of the tracer are covered in the sections below 9 2 2 Start trace You can turn on the trace alone or activate it under the debugger e In the top level dialog you turn on the trace with the command trace Goal Before the trace logs the proof of Goal the predicate trace_begin O is called After proof of the goal the predicate trace_end 0 is executed You can define both predicates yourself e The debugger and the trace can be on simultaneously You call a goal whose processing you wish to trace by means of the debugger command trace on In this case the two predicates trace_begin 0 and trace_end 0 are not executed 9 2 3 Trace operation W
183. h the selective import directive predicates which have already been imported can be overlaid The general import direc tive on the other hand imports only predicates with indicators which have been neither declared as local nor imported from a different module Example The module circular_note imports the interface of the modules letterbox and persons This makes the exported predicates visible in the module circular_note module letterbox export send 2 end module letterbox module persons export person 1 end module persons module circular note export circular 1 begin module circular note import letterbox import persons circular Message person Person send Person Message fail circular _ end module circular note 7 4 2 Importing specific predicates of a module The selective directive import PredicateList from Module allows you to make visible a subset of the exported predicates of the module Module The import directive requires that the module interface of the module Module be known Module is an atom which defines the name of the module from which the predicates are to be imported PredicateList is a list of predicate indicators The predicates in the list are to be made visible You should have made all the predicates in PredicateList publicly User s Guide 134 IF Prolog V5 3 Module body Declaring distributed predicates visible in th
184. h this answer 1 If you want to see more names type a semicolon and press the key The semicolon initiates backtracking Prolog now searches for a further instantiation of Goddess With the query female Goddess some names will appear more than once For example rhea will be repeated three times because rhea occurs in three different parentS facts There are of course ways of avoiding such repetitions If no more instantiation is possible Prolog responds with no user 2 If you don t want to see any further instances then simply hit the key user female Goddess Goddess artemis yes user Now ask a question about mothers and their children user mother Mother Child Mother gaia Child uranus By typing and hitting the key after each answer you will be shown one by one all mother child relationships in the database Trap If you entered all the rules and facts exactly as given above then a small error has crept in This becomes apparent when you are given the answer IF Prolog V5 3 15 User s Guide Modifying database with editor Sample session Mother zeus The error can be traced to the fact parentS demeter zeus persephone This should have been specified as parentS zeus demeter persephone so that the correct name will be found at the second argume
185. he Prolog source file test if it exists is then read into the database If it is not found the file test pro is read in instead prolog ms 1000k mst 200k 1 userpred Prolog is called with new values for database size and trail size If available the start file is read in Next the file userpred cmp if found is loaded into the database 2 Calling with user arguments Suppose that you want to read test data from a file in a test run For this purpose you can define your own option to enable you to specify the name of the file containing the test data In the example we shall call the option data prolog ms 10000k mst 200k c students data test IF Prolog Copyright C Siemens Nixdorf Informationssysteme AG 1996 xxx consult students pro loaded in 0 03 sec In a Prolog session you can query the arguments in several different ways user system_parameters L L opt lib prolog bin prostd root opt lib prolog ms 10000k mst 200k c students yes user program_parameters L L opt lib prolog bin prostd root opt lib prolog ms 10000k mst 200k c students data test yes IF Prolog V5 3 67 User s Guide Exiting Prolog Prolog system user user_parameters L L data test yes You can read the file
186. he explicitly specified arguments in a structure of this type are incorporated unchanged as arguments in the corresponding subgoal of the Prolog clause Behind these arguments the compiler inserts two further variables or lists which as described are intended for the difference list for the purpose of handling the input list The production rule so T01 ste T0i9 gt si Th oar Tla nya Sn Tn4 SAR Thin is thus compiled into so T01 SEER Lio Ro Rn A 81 714 rae Tla Ro Ri Dany Sn Tni eng Thin Rn 1 Ra IF Prolog V5 3 107 User s Guide Additional arguments Prolog grammar rules Example One example of how to use these additional arguments is the construction of a syntax tree in parallel to the syntactical analysis A grammar extended in this manner not only confirms whether an input phrase is a valid sentence in a language but also generates additional information on its syntactic structure The rules of our original grammar are now extended for each non terminal symbol by an argument containing the associated node in the syntax tree sentence sentence N_ph V_ph gt nominal_phrase N_ph verbal_phrase V_ph verbal phrase v ph V gt verb V nominal_phrase n_ph A N gt article A noun N article article the gt the noun noun dog gt dog noun noun bird gt bird verb verb barks gt barks verb verb runs gt runs The data
187. he grammar database of the first example the production rule verb gt barks has the corresponding clause verb In Out In barks Out Example A grammatical rule in the form phrasel word gt phrase2 phrase3 recurs in the database as the clause phrase1 Ini word Out3 phrase2 In1 Out2 phrase3 Out2 Out3 As soon as phrase2 and phrase3 are recognized in the input stream In1 the terminal word is added to the front of the list Out3 of the remaining terminals word is then the first terminal symbol which is available for further syntactical analysis 6 2 3 Deriving a start symbol Once all the production rules of a grammar have been compiled and read into the database you may form the derivations of a start symbol start by calling the Prolog goal start Head Head is instantiated successively on all lists of tokens corresponding to the syntactical struc ture of the non terminal start Example Using the grammar from the first example the following result is output user sentence Head Head the dog runs J Head the dog barks no User s Guide 106 IF Prolog V5 3 Prolog grammar rules Additional arguments 6 2 4 Syntactic analysis If conversely a list of tokens InputList is given then by calling the goal start InputList Tail you may check whether there is a token sequence at the beginning of InputList which has the
188. hell prompt character will now appear If you want to work with Prolog again at a later time you will have to reinvoke Prolog with the command prolog 2 2 A first Prolog query Enter as your first query or question user write hello User s Guide 6 IF Prolog V5 3 Sample session First Prolog query The built in predicate write 1 generates the following output on your screen hello yes user The number following a Prolog predicate e g 1 indicates how many arguments the predicate requires This number is called the arity of the predicate Special cases Character strings containing a blank a period or some other special character must be enclosed in single quotes Example If you want the character string hello to be displayed with a terminating period you must type the following user write hello hello yes user If you want Hello to be displayed with an uppercase H you have to enclose it in single quotes because Prolog treats strings beginning with an uppercase letter as variables user write Hello Hello yes user i Any character string enclosed in single quotes is an atom Complete and detailed information about which character strings are regarded as atoms in Prolog can be found in the section Prolog syntax in the IF Prolog Reference Manual 1 IF
189. hen the trace is on a step by step execution listing is generated and output An execution step is represented by the appropriate box port All ports permitted by the debug mode of the predicate can be monitored The trace listing is written to the current error output stream You can redefine this stream if necessary All activated ports default call fail exit and redo are traced An output listing contains the following columns for each execu tion step Output format ebox port which represents the execution step esubgoal with which the execution step is associated The column entries are listed side by side with each execution step on a separate line User control facilities You can use trace_output 4 to structure a listing of your own Standard format IF Prolog V5 3 165 User s Guide Tracing Debugging Example In order to demonstrate the debugging facilities we shall use the following predicates in most of the examples father max gregor father max sabine father max rudi father gregor hans father gregor pepsi father hans olaf mother sabine eva mother sabine anita mother sabine ludwig mother eva suzi mother pepsi miki mother miki ulf ancestor X Y father X Y ancestor X Y mother X Y ancestor X Y father X Z ancestor Z Y ancestor X Y mother X Z ancestor Z Y user trace ancestor X hans 1 1 call call ances
190. her integer each time during backtracking After Maximum has been reached the predicate should fail include cpred h typedef struct long curr long maxim FORSTATE COROUTINE Forpred FORSTATE state RESUME 1 next BEGIN ifo TermIsInteger PrologArg 1 amp state gt curr amp amp TermIsInteger PrologArg 3 amp state gt maxim while state gt curr lt state gt maxim if TermUnifyInteger PrologArg 2 state gt curr DETACH 1 next gt else PrologUndo state gt currtt gt END void Cboot void CPRED for_pred 3 Forpred sizeof FORSTATE Framework for complex C functions with epilog Following the final exit from the coroutine in Prolog fail there may still be certain actions to be performed e g closing files These actions can be executed in an epilog include cpred h typedef struct State COROUTINE Funcname State StatePtr RESUME ProgPtr1 Label1 IF Prolog V5 3 215 User s Guide Linking a coroutine Active C interface RESUME ProgPtr2 Label2 RESUME ProgPtrN LabelN HASEPILOG BEGIN PrologEnableEpilog Start of the actual program code of the function including FAIL DETACH ProgPtri Label1 DETACH ProgPtr2 Label2 DETACH ProgPtrN LabelN End of the actual program code EPILOG Program code for the epilog END void Cboot void CPRED Predname Arity Funcname sizeof State Example
191. hether a breakpoint is set When it encounters a breakpoint or an error the debugger stops outputs the current status in the appropriate input output areas and is ready to execute commands You may then enter debugger commands in order to request information change debugger settings or continue execution Schematically a debugger session proceeds as follows User s Guide 168 IF Prolog V5 3 Debugging Interactive debugger Prolog dialog Debugger user debug Goal call goal debugger starts debugger stops at breakpoint enter debugger commands user debugger terminates after execution of goal You can only call the debugger for a particular goal user debug ancestor X pepsi After the call of debug 1 the debugger starts and first calls the predicate debug_begin 0O if defined by the user Debugger settings The debugger provides default settings You can change settings by means of predicates and debugger commands in order to define your own debugging strategy IF Prolog V5 3 169 User s Guide Breakpoints Debugging Predicate or command Meaning debug_config 3 trace_config 3 port trace_port set_debug_depth set_trace_depth set_trace_length set_history stop stop interactive Configure debugger Configure trace Activate deactivate port
192. i e this predicate is evaluated with fail The warning is not output for predicates for which the exception mode has been set to on see predicate_mode 3 For consult 1 and reconsult 1 all predicates are implicitly declared as dynamic see dynamic 1 to support program testing For consult 1 and reconsult 1 all the predicates which were not explicitly declared with dynamic 1 are declared as static These cannot then be modi fied tested or displayed User s Guide 262 IF Prolog V5 3 Graphical User Interface MS Windows prompt on Output of the prompt in the Prolog input loop and interaction for backtracking for goals which have been successfully executed in the Prolog input loop and for which variables have been instantiated always take place i e even if the standard input and the standard output are not terminals off Output of the prompt in the Prolog input loop and interaction for backtracking for goals which have been successfully executed in the Prolog input loop and for which variables have been instantiated take place only if the standard input and the standard output are terminals char_conversion on The character conversions defined with the predicate char_conversion 2 are performed when terms are read off No character conversion is performed when terms are read backtrace 10 The maximal length of the backtrace list The list is created when an exception has been raised and con tains the calling sequenc
193. icates of amodule 131 7 3 5 Changes to the module interface 132 TA Wie ol Dody y Se missas oe ee AR eR AAA 132 7 4 1 Importing all public predicates of a module 133 7 4 2 Importing specific predicates of a module 134 7 4 3 Declaring predicates as distributed lt 136 7 4 4 Declaring predicates as private o o 136 TAS Declaring predicates as dynamic s se s e eos oo e be poe eoe G 137 7 4 6 Declaring metapredicates ao oa a eee deed ed ees 138 TAT Defining operators 2 42444 25 244 oe e ca eoh arh 139 o Compiling and loading files 4 acca eb Rw ee See Bee A 141 7 5 1 Compiling Prolog texts and modules 141 7u2 Loading compiled files 4225254 edb meee ke weed pases 144 7 5 3 Removing a module from the database 146 8 Contexts 147 amp 1 Context jumps gt z er se hw See BAe Bie ew Bele ed BEA ws 148 8 amp 2 OO i e pacos Bad we ee eS YE ee ee eH Pe Swe Bree Se 150 Go ee 6 ee oe Re ee RS Se Hh eee ee eee ee Ge 154 6 3 1 Global control of signal handling s se s weere sisao tt ess 156 8 3 2 Passive signal handling lt 0 0 lt lt 157 8 3 3 Active signal handling o aaa a 158 9 Debugging 9 1 Debugger settings e tori sanet BERS ea SEE RE OS ERY 9 1 1 Activating deactivating box ports lt lt lt 9 1 2 Debug mode of a predicate lt lt
194. ich still contain the list element X The fact that the only promising clause in the form X Xs1 of the input list is clause 1 of program 1 can be highlighted by inserting a cut at the start of its body remove X Xs1 X Ys Clause 1 gt remove Xs1 X Ys This prevents an attempted proof with the final two clauses from taking place during back tracking since it is bound to fail anyway The logical behavior of the predicate is not changed externally but in certain cases processing will be expedited In other words we are dealing with a green cut Admittedly following this modification you can delete the condition Y X in the second clause This then gives you Program 2 remove X Xs1 X Ys Clause 2 2 remove Xs1 X Ys IF Prolog V5 3 55 User s Guide Procedural predicates Database and procedure execution remove Y Xs1 X Y Ys1 Clause 2 remove Xs1 X Ys1 remove X Clause 2 3 In Program 2 you can no longer remove the cut without changing the meaning Likewise exchanging the first two clauses will cause a change of meaning In other words we are now dealing with a red cut A further example of this can be found on page 93 3 6 2 Loops in Prolog repeat 0 The predicate repeat 0 can be defined as follows repeat true repeat repeat repeat 0 is always successful and thus creates an unlimited number of choice points for backtracking Su
195. ich the cut was called as a subgoal e All instantiations made up to this point while executing Goal will be made permanent If the cut is encountered again from the right in the course of backtracking the proof of the calling Goal will fail altogether since no new variable instantiations or clause selections can be made Representation in the box model Once a box belonging to the cut is entered through the call port as a subgoal box within the current clause box the following happens e The cut box is left through exit and destroyed e All subgoal boxes which precede the cut box in the current clause box are destroyed e All clause boxes which follow the current clause box inside the parent goal box are destroyed The cut The cut A distinction is made between two types of cut IF Prolog V5 3 51 User s Guide Procedural predicates Database and procedure execution green cuts red cuts Example These cuts do not limit the set of alternative proofs of a goal by a program On the one hand they enable the programmer to remove from the search tree of a goal those branches which would end with fail anyway On the other hand if two or more branches in the search tree lead to a proof of a goal with identical variable instantiations some of these branches can be pruned with green cuts This reduces the size of the search area
196. ide MS Windows Graphical User Interface 11 9 2 Development User Interface Development version of the graphical user interface is intended to be used for debugging custom IF Prolog applications It includes the same interface as the IF Prolog application This is the Development User Interface main window IF IF Prolog V5 2 C iprologiDemosiCOMiado pro ME x File Edit Active file View Debug Options Help O 3369 47 saelas esla ee Bl elol OL Fic ee a ES FEE H7 al ejes visite Bl IF PROLOG U5 2 WIN32 Copyright C Siemens AG Austria 1996 98 Serial Xb 526666661 licensed for Daniel Banas PC5156 1018 SWH Bratislava user For Help press F1 Figure Development User Interface main window 11 9 3 Release and Debug configurations User interface libraries come with two versions Release and Debug You can use Debug configuration to debug the application code The Release configuration does not contain debug information and is optimized the resulting code is faster and smaller Release configuration can be linked with runtime version of IF Prolog GUI where main IF Prolog window is simpler to create end user version of the custom application Debug configuration can be linked with development version of IF Prolog GUI where main IF Prolog window contains more toolbar buttons and tools that help the development to create test version of the application User s Guide 274 IF Prolog V5 3 Graphical User I
197. ield In the help field you will find information on the debugger commands You select the help field with the help command Field for AND BOX The current outermost port and the current subgoal appear in this field If the clause does not fit into the output window the debugger displays only part of it But the commands allow you to scroll in the output window The debugger truncates overlong lines at the window border The command snapshot allows you to store the screen contents in a file without restriction of line length and to examine them further there The goal box becomes the current field when the debugger reaches a breakpoint at an outermost port If this is an explicit breakpoint the message BREAKPOINT will appear in the status indicator where stands for the number of the breakpoint IF Prolog V5 3 177 User s Guide Alpha user interface Debugging You select the AND BOX with the command view and box or amp Field for OR BOX The field for the OR BOX shows the clause and its environment in the database If the clause does not fit into the output window the debugger displays only part of it But the commands allow you to scroll in the output window The debugger truncates overlong lines at the window border The command snapshot allows you to store the screen contents in a file without restriction of line length and to examine them further there The clause box becomes the current field when
198. in of a socket specifies the address family The following socket domains are supported stream domain address is a combination of an Internet address and port delivery slot The Internet address can be given symbolically as a host name or explicitly as an address in dotted notation The ports are managed by the system unix domain address is a unique filename used only for rendezvous The corresponding file is created by the server process and used by client process es The processes wishing to communicate with each other create sockets with the predicate socket 3 Messages can be sent only after the communication has been established The communicating processes are classified conceptually into server s and client s A server is basically a process providing services for other processes and a client is a process utilizing these services User s Guide 96 IF Prolog V5 3 Input output interface Process management Communication between connected processes In order to establish reliable communication between processes stream sockets are used and the processes are connected This is semantically comparable with communication using pipes The server process must first bind the socket to an address before being able to provide the service predicate socket bind 2 The server can bind the socket to a specific port or let the system choose the port It then listens for incoming communication requests from clients predicates soc
199. ing a Prolog session with edit Filename edit the file Filename edit edit the last file processed with edit 0 1 during the Prolog session Filename must be a syntactically valid file name for the operating system The Prolog system searches in the order below e first for a file called Filename e then if this cannot be found for a file called Filename pro If the search is unsuccessful a file named Filename pro is created automatically The suffix pro is added only if it is not already included in Filename The predicate edit 0 or edit 1 now handles the following automatically e temporary exit from Prolog call to the editor with the specified file name as a parameter e return to Prolog e after user confirmation reading in the updated file again with reconsult 1 You can thus modify files containing Prolog source text during a Prolog session without quit ting Prolog completely The changes take effect immediately provided you have saved them with the editor and then reconsulted the file You can also edit files containing no Prolog clauses but any input In this case you should not let Prolog reconsult the file Otherwise it can have unintended results when it is read into the database User s Guide 88 IF Prolog V5 3 Input output interface File processing Changing the editor The default setting for the predicates edit 0 1 is operating system dependent You can change this default setting with default_editor 2 fo
200. inimum number of bytes which must be free in the memory used after garbage collection before further memory is allocated maximal_gap Maximum number of bytes which must be free in the memory used after garbage collection before further memory is allocated trace Enable disable output of a message to standard error output in the case of garbage collection IF Prolog V5 3 73 User s Guide Environment variables Prolog system By using the predicate statistics 2 you can query the memory size and the data for garbage collection for all areas by means of backtracking In the following example the Prolog system has just been called and consequently the list which holds the data for garbage collection does not yet contain any significant values for garbage collection Example user statistics X Y X time Y cputime 1 41 X memory Y system 1048560 575824 database 962544 489808 global 30720 716 constraints 8192 0 local 30720 760 extension 8192 32 trail 8192 0 X garbage_collection Y database 0 0 0 global 0 0 0 constraints 0 0 0 local 0 0 0 extension 0 0 0 trail 0 0 0 no 4 3 Environment variables You can modify the environment with which the Prolog system works This is done using variables whose contents you may change individually and which are then evaluated accord ingly by the Prolog system The following table summari
201. installation time The default setting for the root directory is operating system dependent If you copy an existing Prolog system to a different position in the file system you must assign the pathname of the new root directory to the environment variable PROROOT The various components of the Prolog system are stored under the root as follows Root of the Prolog system bin c DEMOS prolib Executable C interface Examples Compilers files Boot files Built in predicates 61 Calling Prolog Prolog system 4 1 1 Calling Prolog When calling Prolog you can specify two different types of options system arguments which have a Prolog specific meaning and user arguments which you have defined yourself You must enter system arguments first they can be given in any order You can specify user arguments after the system arguments If system arguments are included among the user arguments they will no longer be recognized as such and will be treated as user arguments You call the Prolog system with the command prolog prolog option systemargument userargument option systemargument Prolog offers a range of built in options which allow you to define set values for system arguments With these system argu ments you can e g specify the files which are to be loaded into the database when the system is started or specify values for initial ization of the
202. int a Prolog program consists of predicates and these in turn of clauses clause is a fact or a rule comprising of a clause head and subgoals You can define explicit breakpoints for e the entire predicate e the individual clauses e the subgoals of the clauses e a variable in a clause IF Prolog V5 3 171 User s Guide Breakpoints Debugging You can have the instantiation of a variable output at an explicit breakpoint If this mon itored variable is part of a structure which also contains variables you can also extend monitoring to these variables Explicit breakpoints are further differentiated into interactive and non interactive break points depending on whether or not you want the debugger to switch to interactive mode at the breakpoint You use the commands stop 4 or stop_interactive 1 2 4 to set an explicit breakpoint The following table lists all the commands with which you can process explicit breakpoints Command Short Meaning form activate_stop as Activate explicit breakpoint deactivate_stop ds Deactivate explicit breakpoint remove_stop rs Delete explicit breakpoint stop S Set explicit breakpoint stop interactive si Set explicit interactive breakpoint view_stops vs Display breakpoints The commands are described in the reference section in the IF Prolog Reference Manual 1 When you set a breakpoint you can use conditions to define whether or not the debugger is to stop at an explicit breakpoint
203. ion forms an IF THEN ELSE operation on conditions This operation is true if either the IF condition and the THEN condition have the truth value TRUE or the IF condition has the truth value FALSE and the ELSE condition has the truth value TRUE For reasons of clarity you should avoid using disjunctions in clauses and instead write two clauses clauses with the same clause head have an implicit OR relation clause_head subgoali subgoal2 User s Guide 28 IF Prolog V5 3 Database and procedure execution Facts is equivalent to clause head subgoall clause head subgoal2 Example sibling X Y The sibling rule has a clause body parentS F M X consisting of three subgoals joined parentS F M Y by AND XAS Y e half_sibling X Y The half_sibling rule has a clause body parentS F1 M X consisting of two conditions joined by parentS F2 M Y OR F1 F2 X X Y The two conditions each have the form parentS F M1 X clause body with each clause body we parentS F M2 Y consisting of four conditions joined by M1 M2 AND X X Y It would be better to reformulate this rule as half_sibling X Y parentS F1 M X parentS F2 M Y Fi F2 LAS Y half_sibling X Y parentS F M1 X parentS F M2 Y M1 M2 EAS 3 1 2 Facts A fact is a Prolog clause without a clause body IF Prolog V5 3 29 User s Guide Predicates Database and procedure executi
204. is to be assumed default1 default2 and default3 supplement options missing from the input stream input P1 P2 P3 gt blanks default1 parameter1 P1 blanks default2 parameter2 P2 blanks default3 parameter3 P3 blanks blanks gt 7 blanks blanks gt parameter1 P1 gt p 717 blanks par P1 parameter2 P2 gt IF Prolog V5 3 109 User s Guide Explicit Prolog goals Prolog grammar rules p 727 blanks par P2 parameter3 P3 gt p 3 blanks par P3 default1 p 717 gt 1 default1 gt default2 p 222 gt 1 default2 gt default3 p 737 gt default3 gt par gt par gt If no option is entered the default values are set user input List 1 List Parameter 1 and parameter 3 are set explicitly and the blanks are skipped user atom chars pl p3 Name input List Name 2 List 6 4 Explicit Prolog goals Prolog also allows explicit Prolog goals as body elements in a grammar rule In order for these elements to be recognizable as such by the compiler they must be enclosed in braces They will then be inserted unchanged at the corresponding position in a resultant Prolog clause One exception to this is the cut A call of the predicate 0 can be inserted without braces as a body element in a grammar rule
205. ist of module predicates Visible Predicates List of predicates visible in the module Module Variables List of module variables Predicate Listing of the predicate or its manual page Variable Variable description and value Operators List of all operators Operator Operator description type precedence IF Prolog V5 3 257 User s Guide MS Windows Graphical User Interface 11 6 4 Menu Database Browser Menu contains several commands useful when using Database Browser View is a pop up sub menu contains items Refresh Updates the database browser directories and displays the root item Exit Exits the database browser main IF Prolog window is activated Sort is a pop up sub menu contains items Sort ascending When the right pane item is list predicates variables operators you can sort it in ascending order Sort descending When the right pane item is list predicates variables operators you can sort it in descending order Help menu item displays this help page on database browser Toolbar Database Browser Menu contains several buttons useful when using Database Browser Refresh Updates the database browser directories and displays the root item Sort ascending When the right pane item is list predicates variables operators you can sort it in ascending order Sort descending When the right pane item is list predicates variables operators you can sort it in descending order Copy When the right pane
206. isung Clee e se teere daeta sa Be ee Hoes 13 Dave Pee questions ra a Rw Oe Bw 14 2 4 Modifyin a database with an editor eras 4k ack ee ake Swe eee x 16 2 5 Extending the database in a Prolog dialog 19 2 6 Modifying the database in a Prolog dialog 23 27 were clauses Ima Mee so oe a ee A e eE sa RD RA 25 3 Database and procedure execution 27 A NIE 27 Glo Atlee yoo an a ed RE HOSE ER ESE wD 28 ub US ee ae Be ae Be eS ER See bee GS 29 de Predicates esi e So eh ok ke BOE Oe BO BD eee ee on EES 30 3 2 1 Compiled predicates 2 6 56542 84 64 ERR ee eR Ee eS 31 111 3 3 3 4 3 5 3 6 The 4 1 4 2 4 3 4 4 3 2 2 Built in pr dicat s lt A AE a 32 De IA E 34 MIO cars aa a wee a e a a e a 34 Procedure SGU a a e A A 37 35 1 Queries and Init directives e acma ce we ee a a a 38 oe a NIE 39 205 9 Unikeation attempt eds a ia ade 42 3 5 4 Searching for the next clause 1 646 44 84 ee 884 8 eee ea 42 3 5 Proving subgoals lt ia i sdra t a sie a Re ee OR ee eS 43 ow Proving o goles ee aee Rhen e ewe e a Bod 44 3 5 7 Failing fo prove a goal 2 424 creme cp pe ee RRA eR pasi 45 208 Activating backtracking sos i See gee a eR YS i e a ES 46 3 00 Redoing agoal lt sa Bes eu ee gona eee a a ee bw 5 47 Sol Redoms gcse s i taoma ase a a ia ide a ak EERE a eaea 48 Procedural predicates cso e Keans eoa krans o a et we a Da a SS a 50 al Thee VO oe ose hee da aE ME p AE e pe G 51
207. ize 69 tused 69 type error error type 151 unification 34 active C interface 200 user argument 62 66 67 user_1 signal 155 user_2 signal 155 user_error alias name 81 user_input alias name 81 user_output alias name 81 value range integers 69 variable 12 14 anonymous 13 built in for modules 220 global 125 instantiation 15 uninstantiated 8 version number query 72 visibility global variable 120 of predicates 120 of system predicates 120 IF Prolog V5 3 307 User s Guide Index visibility operator 123 visible predicate from different module 134 warnings suppressing 64 working with OSF Motif 77 working with constraints 78 working with Informix 78 write the database to file 91 User s Guide 308 IF Prolog V5 3
208. ket listen 1 2 A client process must know which kind of service it needs and at which address the service is provided It uses the same address filename or port as the server for establishing a connection predicate socket connect 2 The server accepts the incoming requests predicate socket_accept 3 and the data transfer can begin predicates socket send 2 and socket receive 2 Once the communication has been completed the processes can be disconnected by closing the sockets predicate socket shutdown 1 2 Communication between processes without connection Connectionless interactions are typically realized using datagram sockets Processes are still conceptually divided into a server and client s There is no need for the server to bind a socket to an address neither for the client to connect to this address Instead each message sent includes a destination address that can be queried by the receiving process predicates socket send 3 and socket receive 3 Generic stream input output using sockets After a connection has been established a socket is both readable and writable Prolog streams however are either readable or writable but not both Therefore to be able to use the generic stream input output predicates two streams must be opened Input output multiplexing Several clients can be simultaneously connected to one server In order to be able to handle incoming messages input output multiplexing can be used predicate s
209. l is received the actions provided by the operating sys tem are performed normally program abort if abort and quit occur a memory dump can also written to disk ignore Signal handling by the operating system is disabled for the signal on Active signal handling is enabled for a signal i e when a signal arrives either user handling is activated or an exception is initiated off When the signal is received it is handled passively by the Prolog system i e the number of incoming signals is counted but no action is taken If signal handling is reactivated with on signal handling is also performed for registered signals received Queries whether the signal has been received at least once i e whether the counter for this signal is greater than 0 In this case the counter for the arrival of the signal is decremented by 1 received Name Queries whether the signal has been received at least once The counter for the arrival of the signal is decremented by 1 here if it was not 0 Name is unified with the name of the signal received Immediately before a handler predicate for the active signal handling of Signal is called the command off is set for the signal The signal handling routine is itself responsible for the renewed activation of the signal command on This is done to prevent the signal handler routine from being interrupted by the re occurrence of the signal After Prolog is called default signal handling default of the op
210. l set a choice point through which it may if necessary reach the next clause of the predicate when backtracking A goal or subgoal is represented by a goal box with the five outermost ports call exit redo fail and error CALL EXIT FAIL REDO ERROR Goal box The error port has a special status which you can activate or deactivate yourself by means of your own exception handling You cannot temporarily disable the error port by means of the debugger s execution control commands skip next You will find debugger commands described in the IF Prolog Reference Manual 1 The clause boxes inside the goal box correspond to the sequence of ORed alternative clauses in the matching predicate IF Prolog V5 3 39 User s Guide Procedure execution Database and procedure execution CALL gt EXIT FAIL REDO ERROR Clause boxes in the goal box In turn each clause box contains a left hand and a right hand subbox belonging to the clause head and the clause body respectively A clause box has the following ports trymatch failmatch enterbody exitbody failbody and redobody EXITBODY TRYMATCH ENTERBODY REDOBODY FAILMATCH t FAILBODY Clause box A series of subboxes extending from left to right in the rightmost part of the clause box corresponds to an equal numb
211. ld RR pair X_new Y_new RR_new replace T1 T2 X_old X_new Perform substitutions in the replace T1 T2 Y_old Y_new first pair then in the rest stack replace T1 T2 RR RR new of the stack replace Old New Term New If Term is identical to Old Term Old replace it with New replace Old _New Term Term If Term is not a structure not compound Term Term Old nor identical to Old there is nothing to replace replace Old New Term Term1 If Term is a structure functor Term Functor Arity form a second Termi with functor Term1 Functor Arity the same functor and arity str_rep Arity 0ld New Term Terml1 and substitute New for Old in the arguments there str_rep 0 _01d _New _Term _Term1 End because there are no more h arguments str_rep N Old New Term Term1 User s Guide 36 IF Prolog V5 3 Database and procedure execution Procedure execution arg N Term Arg Take the Nth argument from replace 0ld New Arg Arg1 Term and substitute New for Old arg N Term1 Arg1 The result Argi is the new Nth Ni is N 1 argument of Terml str_rep N1 01d New Term Term1 Do same for remaining arguments user unification a Y b a YY C U Y _1288 YY 1319 C _1316 U 1316 b 1288 _1312 yes The system displays the substitution which causes the terms to be equal The built in unification mechanism of Prolog
212. lear the output buffers with flush_output 0 1 You can control buffering with open 3 4 You can use the predicate close 1 to close any stream apart from the standard streams Should you attempt to close a standard stream with close 1 it will have no effect The predicate reset_streams 0 restores the defaults for the standard streams User s Guide 84 IF Prolog V5 3 Input output interface Elementary input output 5 2 3 Querying information about input output streams The following predicates allow you to query which streams are open for input and output and to request further information about the stream type and device Predicate Purpose current_error Stream current _input Stream current _output Stream current_device_control Device Command Arity device_control Device Command current _stream_control Q Stream Command Arity stream_control Stream A Command stream _device O Stream Device stream_property Stream Info stream_type Stream StreamType file test Pathname AccessMode Example Query current error stream Query current input stream Query current output stream Query information on device drivers Control device driver Query information on device drivers Control input output stream Determine device name of an in put output stream Query streams information on input output Query type of an input output stream Check access permissions f
213. led and then the trace is turned off When the debugger is active the debugger command trace off stops the trace 9 3 Interactive debugger Simply tracing the execution of your programs does not usually give you enough information for pinpointing specific errors or for thorough debugging Prolog therefore provides you with an interactive debugger which allows you to do the following e Execution monitoring IF Prolog V5 3 167 User s Guide Interactive debugger Debugging You can select various types of breakpoints which enable you to monitor the execution of a goal in small or large steps e Execution control You can return to previous execution points skip execution steps or select normal execution e Execution intervention You can intervene to perform other actions during the processing of a predicate e Querying information Before you debug a predicate you should check whether you are allowed to do so If you call the Prolog system without parameters all predicates are compiled by the incremental compiler and thus held as static predicates in the database You are not permitted to debug static predicates You define all predicates as dynamic if you call the Prolog system with the debug parameter You can declare individual predicates as dynamic with the dynamic directive 9 3 1 Debugger operation Depending on its settings the debugger executes a proof for a goal from one port to the next checking at each step w
214. level 10 The predicate set_prolog_flag 2 allows you to change these default values User s Guide 70 IF Prolog V5 3 Prolog system System information The following table gives an overview of the flags which you are not able to modify The flags themselves provide information about how the Prolog system is implemented or configured Since the default values differ for different system variants they are not listed below Flag Meaning bounded integer rounding function iso float_max float_min max_arity max_integer min_integer notty signal Example Correct results with integer arithmetic Rounding up and down for functions and the division remain der rem Restriction to predicates which conform to the ISO draft stan dard you can set this flag only when calling the Prolog system Maximum representable floating point number Minimum representable floating point number Maximum arity of predicates and structures Maximum representable integer Minimum representable integer Input output devices are not terminals Enable disable signal handling You can set this flag only when calling the Prolog system The following example shows the default values of all flags on a MIPS based system with IRIX 5 3 user current prolog flag X Y write X Y nl fail backtrace 10 bounded true char_conversion on consult notify extended_syntax off debug of
215. log a PROLIB_MOCTET PROROOT c libKprolog a PROLIB_CHARS PROLIB_MOCTET Multibyte character support PROLIB_KERNEL PROROOT c libprolog a PROLIB_DUMMY PROROOT c libdummy a Comment the following library inclusion if you explicitly wish to exclude multiple precision integer and rational number support The library must be included if you include the constraint libraries PROLIB_RAT PROROOT c libRprolog a These library definitions are only necessary if the application uses the Motif Interface of PROLOG Uncomment the appropriate lines Motif 1 1 PROLIB_MOT PROROOT c promotif11 o PROLIB_MOT PROROOT c 1libMotiiprolog a SYSLIBS_MOT 1Mrm 1Xmu 1Xm 1Xt 1X11 Motif 1 2 PROLIB_MOT PROROOT c promotif12 o PROLIB_MOT PROROOT c 1libMot12prolog a SYSLIBS_MOT 1Mrm 1Xmu 1Xm 1Xt 1X11 IF Prolog V5 3 239 User s Guide Generating an extended Prolog system C interfaces These library definitions are only necessary if the application uses the Informix Interface of PROLOG Uncomment the appropriate lines INFORMIXDIR opt 1lib informix Informix 4 1 PROLIB_INF PROROOT c libInf41prolog a SYSLIBS_INF L INFORMIXDIR lib 1sq14 Informix 5 0 PROLIB_INF PROROOT c libInf50prolog a SYSLIBS_INF L INFORMIXDIR 1lib esql 1sql lgen los Informix 7 1 PROLIB_INF PROROOT c libInf71prolog a SYSLIBS_INF L INFORMIXDIR 1lib esql 1sql INFORMIX
216. m 1 Predicates are specified in the form Name Arity nl 0 1 Several predicates with the same name and different arities are denoted in this form 1 A number in square brackets indicates a reference to another man ual or a textbook The number identifies the publication in the Bibliography at the end of the manual The following pictograms are also used User s Guide 2 IF Prolog V5 3 Preface me for important advice and related information for warnings IF Prolog V5 3 3 User s Guide Preface User s Guide 4 IF Prolog V5 3 Chapter 2 A first Prolog session Is Prolog already installed on your system If not please have the Prolog software installed The following examples are intended for users who have never worked with Prolog before 2 1 Calling and exiting Prolog Calling Prolog When you see the shell prompt e g type in the following prolog debug Terminate your input with the key After typing the name of the executable program prolog one may specify one or more options e g debug see also chapter 4 section entitled Calling Prolog These options set the Prolog system in a specific initial state The debug option ensures that all user defined predicates are implicitly declared as dynamic see page 137 thereby enabling debugging of programs The Prolog system is then loaded into main memory When ready it displays the welcome screen
217. modules They can however be accessed by using an explicit module argument for the corresponding built in predicates Only the name of a global variable is significant in its identification 7 2 1 Predicates IF Prolog manages both user defined predicates and built in system predicates in modules Initially all predicates are visible only in their original module This applies both to pred icates defined in Prolog and to C predicates You can access C predicates only while the body of their original module exists Built in predicates automatically become visible when the module body is defined provided you do not redefine them in the module interface and thereby declare them as private The following directives declare predicates as private export PredicateList meta PredicateList dynamic PredicateList private PredicateList If you declare an imported predicate as private in the module interface using one of the above directives you overlay the predicate i e the imported predicate becomes invisible again If you have reexported a predicate in the module interface you may not overlay it with a private predicate in the module body mie User s Guide 120 IF Prolog V5 3 Module objects Metapredicates 7 2 2 Metapredicates In contrast to all other predicates metapredicates have an additional argument the module argument or meta argument The module argument is handled separately by Prol
218. motif motif Display 9 2 The Tracer Maximum structure depth in which terms are output If you specify the value O here terms are displayed down to the lowest level Number of goal and clause boxes which are to be stored This parameter applies only to the debugger Number of trace outputs which are to be stored This parameter applies only to the trace Option for setting the user interface This parameter applies only to the debugger The user interface is set automatically by the debugger according to the system environment The user interface is set for alphanumeric screens The input output device must be a terminal and the device driver must recognize the terminal control functions If this is not the case the debugger switches over auto matically to a stream user interface The debugger switches on the command line stream user interface This user interface requires no terminal functions and is consequently also suitable for redirec tion of input output The debugger attempts to start the OSF Motif user interface on the preset display If the attempt is un successful the debugger switches back automatically to the alphanumeric user interface The debugger attempts to start the OSF Motif user interface on the specified Display If the attempt is unsuccessful the debugger switches back automatically to the alphanumeric user interface The tracer can provide you with more detailed information about the search for a
219. n conveniently write and modify files using the editor e retain files as Prolog sources independently of Prolog sessions When a Prolog session is terminated the entire database is erased from main memory Anything you wish to keep must be stored in files e compile Prolog source files Prolog modules which you wish to compile must be held in a file e structure large programs in files Files allow you to give your programs a clear structure For example all predicates which logically belong together can be written to one file 5 3 1 Editing files with an editor Files are most conveniently created and manipulated with an editor provided by most operating systems An editor is a programm designed to support creating and manipulating IF Prolog V5 3 87 User s Guide File processing Input output interface text files An explanation of the use and facilities of editors is beyond the scope of this manual Various editors are available under the UNIX operating system as tools for processing The vi editor belongs to the basic configuration of a UNIX system and is thus almost certainly installed on your system Under other operating systems other text processing tools are available Calling an editor from the operating system You call the desired editor from the operating system shell Loading editing and writing to a file are done in the usual manner Calling an editor in a Prolog session You can call the editor dur
220. n TermlsList Check and convert list TermIsNil Check empty list TermlsF loat Check and convert floating point number TermIsFloatExpression Check and evaluate arithmetic expression TermIsUniversal Check and convert structure TermlIsVar Check variable Analyzing Prolog terms You can also classify a term by using the functions TermType and TermDecompose C function Purpose TermDecompose Analyze term and query additional information TermType Analyze term Accessing components of a term C function Purpose PrologArg Access an argument of the active C predicate TermArg Access an argument of a Prolog structure IF Prolog V5 3 199 User s Guide Overview Active C interface Generating Prolog lists in C Lists are terms which are used very often in Prolog The active C interface therefore supports the processing of lists with special functions C function Purpose TermOpenList TermAddList TermCloseList Generate list Add element to list Close list Generating Prolog objects The following functions allow you to convert C values into Prolog objects C function Purpose TermMakeAtom TermMakeCompound TermMakeFunctor Term Makelnteger TermMakeList Term MakeNil TermMakeF loat TermMakeUniversal Term MakeVar Unification Generate Prolog atom Generate Prolog structure from functor arity and arguments Generate Prolog structure from funct
221. n value from PrologFetch is TRUE If no further solution can be found the return value is FALSE e Checking variable instantiation within a goal The value with which a variable of a goal is instantiated after execution is stored as a data object of the type long double char or TERM Prolog term depending on the selected default Using the variable name you can check in the current goal cursor whether the expected variable is present and query its value Depending on the values you can initiate further actions in the program In contrast to previous Prolog versions a type conversion is no longer required PrologIsTerm cursor term amp term If you check for a term you can use the functions TermType or TermDecompose for further analysis of the term TermDecompose term info e Outputting results The three functions fprintt printt and sprintt are available for formatted output of Prolog terms and C data objects They correspond to the C functions fprintf printf and sprintf respectively The function outputlen is used to determine the number of characters in a term for formatted output e Reusing Prolog goals As soon as a character string goalstring has been parsed successfully the information about the goal is stored in the structure t_parse_context If several goals are to be stored and reused your best course of action is to create passive goals You need to parse a passive goal only once but yo
222. nce with those higher up the family tree appearing more frequently than those lower down You could ask many more questions based on the patterns which you are now familiar with 2 5 Extending the database in a Prolog dialog To extend the database directly in a Prolog dialog you use the built in predicates assertz 1 or consult 1 Entering single clauses To add a clause to the database type the following when the Prolog prompt is displayed user assertz male ares yes user IF Prolog V5 3 19 User s Guide Extending database in dialog Sample session assertz 1 stores the specified clause into the database placing it after all existing clauses with the same functor and the same arity In our example the fact male ares is added after the existing male clauses There are other forms of the assert predicate for example asserta Clause which stores the clause as the first clause of the appropriate predicate For a detailed description of all the available options refer to the IF Prolog Reference Manual 1 If you make a syntax error when typing your input omitting a parenthesis or period for ex ample the clause containing the error is not written into the database Prolog immediately issues an error message xxx syntax error type of error gt gt gt line column You must repeat your last input with the clause specified correctly The assert predicates store individual clause
223. nction and the size of the state structure Further actions by the system when the new predicate is called are shown in the following diagram The arrows indicate the direction of execution flow User s Guide 208 IF Prolog V5 3 Active C interface Linking a coroutine Predicate Actions of the Prolog system Coroutine call The Prolog system reserves memory space for a program pointer and a state structure generate start exit without exit removal The Prolog system stores the program pointer redo restart exit remove The function returns the result value 0 The Prolog system recalls the function with the argument PP 1 and the state structure pointer of the instance The statements which are now processed are the epilog of the predicate The Prolog system ensures that when this function call terminates the memory space occupied by the program pointer and state structure of this instance is released fail Example The simple coroutine is now to be expanded so that it can be linked to Prolog as a predicate with backtracking During backtracking the predicate pred 2 is to unify its first argument successively with the integers 1 2 and 3 and the second with the atoms Start Redo and Last visit Source code for the complex Corresponding C function Prolog predicate include with clauses
224. nd sub_tens N sub_tens N gt digit N sub_tens N gt teen N sub_tens N gt tens T and_digit D N is T D and_digit 0 gt and_digit N gt digit NW digit 1 gt one teen 10 gt ten digit 2 gt two teen 11 gt eleven digit 3 gt three teen 12 gt twelve digit 4 gt four teen 13 gt thirteen digit 5 gt five teen 14 gt fourteen digit 6 gt six teen 15 gt fifteen digit 7 gt seven teen 16 gt sixteen IF Prolog V5 3 111 User s Guide Production rule syntax Prolog grammar rules digit 8 digit 9 tens 20 tens 30 tens 40 tens 50 tens 60 tens 70 tens 80 tens 90 gt gt eight nine twenty thirty forty fifty sixty seventy eighty ninety teen 17 gt seventeen teen 18 gt eighteen teen 19 gt nineteen The associated program now responds to queries of the following type user number Value two hundred and twenty three lt Value yes 223 Besides performing this syntactical analysis and calculating the value the program is also capable of providing an appropriate verbal expression for a given value synthesis user number 101 X 1 X one hundred and one 6 5 Syntax of the production rules The most importan
225. nd line You can disable all this parameters by adding the parameter at the end of the command line parame ters You can copy current configuration from this edit box and paste it to a file or command line Note Con figuration from the Registry is used only for the GUI Invokes font dialog You can modify console font You can change Y size of the console window number of lines You can change X size of the console window number of columns You can change scrolling size of the console window to tal number of lines that are used for buffered screen output User s Guide 260 IF Prolog V5 3 Graphical User Interface MS Windows 11 7 2 Prolog Flags Options This is the Prolog Flags property page IF Prolog Y5 2 Options Figure Prolog Flags property page Prolog flags are used to make global settings for IF Prolog Property page contains dialog box items radio buttons edit boxes buttons by which Prolog flags can be set easily The following table contains the available Prolog flags and their permitted values The following Prolog flags may be queried and modified Flag Value Description consult notify A message is output when a Prolog text is loaded nonotify No message is output when a Prolog text is loaded search_path Paths If input files are not specified using a complete path name they are searched for in the directories speci fied in the Paths list The default value of thi
226. ng but not for both The following predicates require the input output stream to be specified explicitly The stream is addressed by means of a name alias name or term of open 3 4 You can also address the standard streams by means of their synonyms Predicate Purpose get_byte Stream ByteCode get_char Q Stream Char get_code Q Stream CharCode Stream SearchChar Text EndChar nl Stream print Stream Term put_byte Stream ByteCode put_char Stream Char put_code Stream CharCode read Stream Term read_term Stream Term Options skip_line Stream write QStream Term write_canonical Stream Term get_until write_formatted Stream Format TermList write_term Stream Term Options writeq Stream Term to the Prolog system when Read a byte Read a character Read a character Read up to a specific character Write new line Write a term Write a byte Write a character Write a character Read a term Read a term with control options Skip input line Write a term Write a term in normal structure repre sentation and in single quotes Write terms with formatting Write a term using operator notation Write a term in single quotes using oper ator notation i Keyboard inputs are normally buffered one line at a time and are passed is pressed Output to files and pipes is normally also buffered If required you can c
227. nifiable with the current subgoal stop interactive p 1 call match p 3 Temporary breakpoints You cannot explicitly create temporary breakpoints You create a temporary breakpoint indirectly for example by means of a branch command or if goal execution is exited through an exit fail or error port Temporary breakpoints are always interactive breakpoints For all these ports the debugger creates a set of temporary breakpoints these are created jointly and are also deleted jointly If you issue a command which creates a temporary breakpoint before one of the most recently created breakpoints has been encountered the old temporary breakpoints are deleted before the new ones are created 9 3 3 Controlling debugger execution Starting from the current breakpoint you can control subsequent execution of the debugger The first breakpoint for a goal is the call port Command Short Meaning form continue c Execute to next explicit breakpoint fast_skip fs Execute to exit of current subgoal nonstop no Execute to end skip S Execute to exit of current subgoal step gt Execute to next breakpoint s soon as the debugger reaches a port of the box model it checks whether breakpoints are set at this port and if so whether they are activated If no breakpoint is present the debugger continues the execution in accordance with the current execution command If breakpoints are present at the current port as its first step
228. ns and with backtracking complex C functions as Prolog predicates using the built in C interface The C interface is described in detail in chapter 10 3 2 2 Built in predicates IF Prolog provides a large number of built in predicates which you can use to formulate your own predicates Built in predicates cannot be extended by additional clauses or entirely replaced The module system does however give you the option to define your own predicates with the same clause names and the same arity as built in predicates since all the built in predicates are defined in special system modules An overview and an alphabetically arranged description of the built in predicates can be found in the IF Prolog Reference Manual 1 Predicates which permit backtracking are indicated by a hash character in front of the functor This character does not appear with predicates not having this capability For metapredicates the argument list is followed by the text Q Module The module qualification can be specified by 2 Metapredicates are supplied with information on the calling module Directives are special syntactic structures which can be specified in IF Prolog texts and which are processed when these texts are read in e g with consult 1 They are indicated by in front of the functor as they would be written in a Prolog text User definable predicates are automatically called at particular points by IF Prolog if the user has de
229. nt position when the mother rule is applied 2 4 Modifying a database with an editor If you have already called Prolog and do not want to quit again completely there are various ways of working temporarily with an editor The simplest option is to use the predicate edit 0 1 which you have used already when creating the gods pro file After exiting from the editor and returning to Prolog level confirm in answer to the question from the Prolog system that you want the file to be read in again When the file is read in reconsult 1 overwrites all clauses in the database which have the same functor and arity as clauses in the modified file This ensures that there is only one occurrence of each clause in the database even if you re edit and read in the same file several times Type user edit gods This calls the preselected editor and reads in the gods pro file which you have just stored You can now amend the erroneous line to parentS zeus demeter persephone Add a few more rules to the file and if you like you can also include comment lines This example is included in the IF Prolog distribution as file PROROOT DEMOS standard gods pro Database Greek Gods discontiguous mother 2 male 1 female 1 Facts mother Mother Child mother gaia uranus Facts parentS Father Mother Child First Generation parentS uranus gaia rhea parentS uranus gaia cronus Second
230. nterface persons making the predicates output 1 portray 1 and collects 2 visible The defini tion of portray 1 from the persons module is not visible module operation begin module operation import output import persons program collects Person Things output collects Person Things output collects Person Things persons end module operation Calling the predicate program 0 generates the following output collects bill stamps pictures bill collects stamps and pictures This output is the result of the following individual steps For output Term Current module Goal operation output operation output output output Term operation call portray Term operation portray Term fail writeq Term operation For output Term Q persons Current module Goal operation output persons output Term persons call portray Term persons portray Term write_formatted q collects bill persons show_collection Things 7 3 3 Reexporting all public predicates of a module The directive reexport Module makes all public predicates of the module Module available to other modules The reexport directive requires that the module interface of Module be User s Guide 130 IF Prolog V5 3 Module interface known Module is an atom which defines the name of the module whose module interface is to be reexported You
231. nterface MS Windows The table describes which files must be linked with which GUI version Files Runtime GUI Development GUI Resource DLL for Release config GUIres dll GUles dll uration Resource DLL for Debug config GUlresd dll GUlresd dll uration User Interface static library for rtmGUI lib devGULlib Release configuration User Interface static library for rtmGUId lib devGUld lib Debug configuration Main object file for Release con rtmGUlmain obj devGUlmain obj figuration Main object file for Debug con rtmGUlmaind obj devGUImaind obj figuration Important note All IF Prolog custom GUI applications have to be delivered with an appropriate MFC dynamicaly linked library with file mfc42 dll that is delivered with IF Prolog package and installed in the system VC 6 0 note Due to changes of MFC libraries in Visual C 6 0 different libraries for IF Prolog GUI application must be linked when using Visual C 6 0 VC 6 0 compliant libraries contain the number 6 in the filenames e g the devGUI6d 1ib is a user interface static library for Debug configuration containing Development GUI Compiling custom GUI applications with VC 6 0 is supported from the version 5 2a Release 2 01 Custom applications copmiled with Visual C 6 0 have to be delivered with an appropriate versiob of the resource DLL GUTres6 dll IF Prolog Application Wizard is can create Visual C 6 0 compatible projects from the version
232. nterrupted by the arrival of signals This may occur in particular with operating system services which lead to a program wait state for example read operations which may have to wait for user input from the keyboard e g the predicate read 1 2 In such cases passive signal handling also leads to changes in program execution 8 3 3 Active signal handling If a signal arrives for which you have activated active signal handling with signal control 2 Prolog searches from the inside out for an active context whose ContextInfo list contains an entry in the form signal Signal gt Handler where Signal is unified with the name of the received signal If Prolog finds such a context it proves Handler without destroying the cur rent proof tree If Handler is successful true the interrupted goal is resumed as if the Signal had never arrived Otherwise or if Prolog finds no matching context it generates the ex ception error system_error error interrupt signal Signal goal Subgoal i e initiates a context jump where Subgoal is the goal which would have been proved next if the signal had not arrived There is a special predicate which creates a context which handles only signals The pre dicate signal_handler Goal Signal Handler creates a context for executing Goal If while Goal is being proved a signal arrives for which active handling is defined and whose name can be unified with Signal the goal Handler is proved a
233. nto the database Instead an appropriate syntax error message will be displayed on your screen In order to correct the error you can e modify the clause in your file with the predicate edit 0 1 The change will subse quently be stored and will be present in the database see also page 19 e modify the clause directly in the database with the predicate assertz 1 The clause will then be present in the database but is not stored in the file It will be lost when you quit Prolog 2 3 2 Listing clauses First request an output of a list of the clauses which you have just read in from the gods pro file on your screen You do this by typing user listing IF Prolog V5 3 13 User s Guide Prolog example Sample session The predicate listing 0 causes all rules and facts stored in the database to be output on the screen In order to pause the output press the key combination crrr s To resume press orrL o To stop the clauses being listed press the per key If the predicate listing 0 displays only yes on your screen the reason may be that you have not declared your predicates as dynamic Only dynamic predicates can be listed By default Prolog regards all user defined predicates as static If you call Prolog with the debug option all user defined predicates are implicitly declared as dynamic i If you are still debugging a Prolog program
234. o term vari ables The active C interface itself takes care of any memory expansion or garbage collection It retains all term variables until the C predicate creating them has been terminated or until the function itself explicitly release the memory C function Purpose TermCollect Release memory for specific term variables TermCollectSequence Release memory for specific term variables TermContext Determine current term context Classifying Prolog terms The basic data objects of Prolog are terms see the chapter on Syntax in the IF Prolog Reference Manual 1 The following classification hierarchy applies User s Guide 198 IF Prolog V5 3 Active C interface Overview Term Constant Variable Structure Extended term Atom Number List Normal structure Expression Integer Floating point number The active C interface offers various C functions which allow you to classify a term more precisely and if this is successful to convert it into a C data type In the event of an error these functions generate certain exceptions C function Purpose TermlsAtom Check atom and convert to string TermIsCompound Check and convert structure TermIsFunctor Check and convert structure TermlIsInteger Check and convert integer TermIsIntegerExpression Check and evaluate integer expressio
235. ocket Host TimePort open Socket read Stream select Stream off _ wait for data socket_receive Socket TimeData close Stream socket shutdown Socket TimeData Bi B2 B3 B4 1 GMT1900 is B1 256 256 256 B2 256 256 B3 256 B4 Magic is Ox83AA7E80 GMT1970 is GMT1900 Magic Example for client server communication using sockets The following Prolog text implements a simple server reading Prolog queries from a socket and writing all answers to a socket It also shows how to use sockets with generic in User s Guide 98 IF Prolog V5 3 Input output interface Process management put output predicates server Port number will be written use the same number for client server socket internet stream Socket socket_bind Socket Port write port Port nl socket_listen Socket socket accept Socket Host Port2 NewSocket write accepted Host Port2 nl open NewSocket read Input open NewSocket write Output repeat read Input Message write Server received write Message nl not handle_server Message Output I close Input close Output socket_shutdown Socket socket_shutdown NewSocket handle server end of file _ fail handle_server halt _ fail handle_server help Output I write canonical Output Enter Prolog queries End with halt write Output flush output O
236. og user reconsult user gt or in abbreviated form user user gt As in the case of consult user you can now write all the clauses which you want to include in the new database Your final input is end of file or the key combination crrr D The Prolog system then displays the prompt user again i All old entries with the same functor and the same arity as the new clauses will be overwritten in the database and thus deleted In other words the database is modified by reconsult rather than merely ex tended as in the case of assertz or consult As there are now no further errors in the example database you can immediately terminate the modification process by typing end of file or pressing the key combination errr p User s Guide 24 IF Prolog V5 3 Sample session Storing clauses in file 2 7 Storing clauses in a file So far all the clauses you have entered or modified in the Prolog dialog exist only in the database If you want to quit Prolog without losing your facts and rules then you will first have to store them in a file The file name should be a new one so that no existing file will be overwritten Type the following user tell dei pro listing told yes If you name an existing file in the te11 1 predicate it will be overwritten Here
237. og If the module argument is not specified during goal execution for a metapredicate it is implicitly assigned the name of the module in which the predicate was called This enables metapred icates to determine the name of the calling module You can use metapredicates to perform certain actions within the calling module This is particularly desirable if a predicate has Prolog goals as arguments which are to be executed in the calling module A metapredicate can access the module argument using the built in predicate calling context 1 calling_context Module Module is unified with the implicitly or explicitly specified module name of the metapredicate call In Prolog built in predicates are referred to as metapredicates if they have goals as arguments allow the database to be modified or manage global variables Metapredicates which modify the database PD abolish 1 asserta 1 2 asserta with names 2 3 assertz 1 2 assertz with names 2 3 consult 1 edit 0 1 reconsult 1 retract 1 2 retract_with_names 2 3 Metapredicates which inspect the database clause 2 clause_with_names 3 current_predicate 1 current_visible 2 debug_mode 3 list_mode 3 listing 0 1 modify_mode 3 predicate_mode 3 predicate_type 2 save_module 1 Metapredicates which have goals as parameters bagof 3 catch 3 context 2 debug 1 exception_handler 3 findall 3 once 1 setof 3 signal_handler 3 trace 1 Metapredicates which execute user predicates in the calling module
238. og example As the next step you should now write a small example database This will contain facts and rules which describe the subject of the program The subject of the example is Greek mythological deities and the family relationships between various gods and goddesses IF Prolog V5 3 9 User s Guide Prolog example Sample session gala lt uranus uranus rhea lt gt cronus leto lt zeus hera demeter zeus artemis apollo ares persephone signifies a relationship between a goddess and a god signifies a parent child relationship The relationships shown in the family tree are now described using Prolog The simplest relationship is a fact for example Ares is male In Prolog you can formulate the fact as follows male ares The parent child relationship or the siblings relationship requires a more detailed description known as a rule The rule for the siblings relationship could be formulated as follows X and Y are siblings if both have the same parents and if X and Y are not identical Converted into Prolog the rule looks like this sibling X Y parentS F M X parentS F M Y X X Y The comma between the individual subgoals indicates a conjunction The rule is satisfied only when all three subgoals are true The generic term for a
239. oint At the bottom of the window you will see four buttons which allow you to control operation of the dialog window OK The entered data is accepted the command exe cuted and the dialog window closed Apply The entered data is accepted and the command ex ecuted The dialog window remains on the screen Cancel The command is not executed and the dialog win dow is closed Help Help information for the dialog window is displayed in a separate window 9 5 3 Setting explicit breakpoints The Breakpoints menu allows you to set delete activate and deactivate explicit breakpoints see also page 171 by clicking on the associated button The window for explicit breakpoints contains two text windows The upper text window contains the breakpoints which you have set up the lower one contains a section of the database with the program to be debugged With the button Database you activate a dialog window in which you can specify the section of the database If you click on the button set the Set Breakpoint window is opened on the screen in which you can define the breakpoint User s Guide 186 IF Prolog V5 3 Debugging OSF Motif interface ie E Prolog Breakpoints_____ c Stop Regular interactive Predicate Ports Conditions Actions E DK Apply Cancel Help l 3 Setting explicit breakpoints You can use the to
240. ol functions include lt string h gt include lt stdio h gt include lt unistd h gt include lt sys stat h gt include device h static int file_read t_iounit unit void buffer int nbytes return fread buffer 1 nbytes FILE unit static int IF Prolog V5 3 235 User s Guide Device driver functions file_write t_iounit unit void buffer int nbytes return fwrite buffer 1 nbytes FILE unit static BOOLEAN file_flush t_iounit unit fflush FILE unit return TRUE static BOOLEAN file_close t_iounit unit BOOLEAN force fclose FILE unit return TRUE static BOOLEAN file_is_unix t_iounit unit int no xno fileno FILE unit return TRUE static BOOLEAN file_open const char filename t_iomode iomode t_iounit unit FILE fd char mode 3 mode 1 mode 2 0 switch iomode amp S_IO_READ S_IO_WRITE S_IO_APPEND case S_IO_READ mode 0 r break case S_IO_WRITE mode 0 w break case S_IO_WRITE S_IO_APPEND mode 0 a break User s Guide 236 IF Prolog V5 3 Device driver functions default return FALSE if iomode S_IO_BINARY 0 mode 1 b if fd fopen filename mode NULL unit t_iounit fd return TRUE return FALSE CONTROL file_isatty stream unit return isatty fileno FILE unit CONTROL file_size stream unit struct st
241. on A fact represents unconditional knowledge i e it describes a relation between objects which always holds true regardless of any other relations Thus provided their clause heads are identical a fact is equivalent to a rule whose clause body always has the truth value TRUE Example john has parents eve and paul anita is female parentS paul eve john female anita faculty 0 1 0 is 1 anita likes troll likes defined as infix operator father son father paul son john paul is the father of john append L L list concatenation appending any list L to the empty list yields the list L everything has Buddha nature XXX XXX buddha_nature _ 3 2 Predicates Clauses with the same clause name the same number of arguments and defined in the same module are combined in the database and form the definition of a predicate The common clause name is called the predicate name or functor of the predicate The number of arguments is the arity For example the predicate fac 2 is defined by the collection of all clauses with the clause head fac Argl Arg2 where Argl and Arg2 may be any terms The separate clauses of a predicate are connected by disjunction i e by inclusive OR Clauses with the same clause name but a different number of arguments belong to different predicates Likewise clauses which have the same clause name and the same arity but are associated with different modules belong to different predic
242. on Command Description General Options Activate IF Prolog Options dialog with Gen eral property page iva Working Directory Set working directory S Cut Cut current selection and copy it to the Clipboard Copy Copy current selection to the Clipboard El Paste Paste text from the Clipboard ES Fit Console Size Resize main window to fit console size parameters amp Interrupt Interrupt goal execution re Break Break IF Prolog input loop pep End Of File End IF Prolog input loop 11 3 2 Debug Main Toolbar This is the Main IF Prolog debugger toolbar 5 Hees opel ajaja e Figure Main IF Prolog debugger toolbar Main IF Prolog debugger toolbar contains buttons for debugger commands such as de bugger tracer activation debugger control abort step skip continue branching back parent and debug trace options This is a list of toolbar buttons with descriptions Button Command Description BI Debug on off Activate deactivate debug mode TJ Trace on off Activate deactivate trace mode Debug goal Activate debugger with a goal and set goal as an active debugger goal Debug last goal Activate debugger with an active debugger i goa User s Guide 250 IF Prolog V5 3 Graphical User Interface MS Windows Button Command Description El Abort Terminate interactive debugger ay Break inside debugger Call new input loop l Continue Continue to next breakpoint H Step Execute u
243. on procedure which stores the Prolog kernel system the saved system state and any further application files at a suitable location in the file tree of the target system The customer will usually call the application with a shell script which could contain the following command for the above example path app_prolog r path app pst x where path is the installation path of the application on your customer s system IF Prolog V5 3 285 User s Guide Prolog applications Appendix User s Guide 286 IF Prolog V5 3 Appendix B ASCII table Decimal Octal Hexadec Meaning Control 0 00 00 NUL Null no operation 1 01 01 SOH Start of Heading A 2 02 02 STX Start of Text B 3 03 03 ETX End of Text C 4 04 04 EOT End of Transmission END key D 5 05 05 ENQ Enquiry E 6 06 06 ACK Acknowledge F i 07 07 BEL Bell G 8 10 08 BS Backspace H 9 11 09 HT Horizontal Tabulation I 10 12 DA LF Line Feed J 11 13 OB VT Vertical Tabulation K 12 14 0C FF Form Feed L 13 15 0D CR Carriage Return M 14 16 OE SO Shift Out N 15 17 OF SI Shift In O 16 20 10 DLE Data Link Escape P 17 21 11 DC1 Device Control 1 Continue output Q 18 22 12 DC2 Device Control 2 R 19 23 13 DC3 Device Control 3 Stop output S 20 24 14 DC4 Device Control 4 T 21 25 15 NAK Negative Acknowledge U 22 26 16 SYN Synchronous Idle V 23 27 17 ETB End of Transmission Block W 24 30 18 CAN Cancel X 25 31 19 EM End of Medium Y 26 32 1A SUB Substitute Character Z QT 33 1B ESC
244. op ds Deactivate breakpoint activate_stop as Reactivate breakpoint Manipulating the database Command WM hort form Meaning unify Step by step processing Instantiate variable Command Short Meaning form step gt Process to next monitored port skip S Skip monitoring of current subgoal fast_skip fs Fast skip monitoring of current subgoal continue c Execute to next breakpoint nonstop no Execute to end of proof IF Prolog V5 3 193 User s Guide Debugger commands Debugging Branching in the proof tree Command Short Meaning form back_subgoal b Branch back to call port of subgoal back_parent bp Branch back to call port of parent goal back_clause be Branch back to clause head fail f Force failure of current subgoal exit x Force success of current subgoal error err Generate exception User s Guide 194 IF Prolog V5 3 Chapter 10 The C interface IF Prolog provides two paths to the programming language C Using the active C interface you can implement predicates as C functions and then use them like normal predicates in cases where e a problem can be solved only in C e large application packages are already available for you to use e the solution in C is more efficient than the one in Prolog Possible applications include e access to the operating system e interfaces to other programming languages such as FORTRAN Pascal etc e access to an existing
245. or a file Prolog is started with the following redirection of the output prolog gt proout All outputs from the Prolog system are thus written to the file proout Should the file not yet exist it will be created by the operating system The following settings are now in effect for the standard and current streams Designation Opened stream Standard input Keyboard Standard output File proout Standard error output Screen Keyboard File proout Current input Current output Current error output Screen The results of executing the following goal are written to the standard output i e to the file proout Note also that since the standard output is redirected there is no prompt user Po IF Prolog V5 3 85 User s Guide Elementary input output Input output interface append A B a b c d The variable instantiations are output on standard output You have to enter again in order to return to toplevel dialog The output resulting from the predicate write 1 is also written to this file write on current output n e Error messages such as the following will still appear on the screen however because you have not redefined this stream asom a x EX C EP TION existence_error procedure asom 1 gt gt gt goal user asom a You can have outputs made to the screen either by explicitly addressing the screen
246. or and arity Generate Prolog integer Generate Prolog list Generate empty Prolog list Generate Prolog floating point number Generate Prolog structure with uninstantiated variables Generate uninstantiated Prolog variable The unification functions allow you after converting C values into terms to return them to Prolog C function Purpose TermUnify Atom TermUnifyCompound TermUnifyFunctor TermUnifyInteger TermUnifyList TermUnifyNil Term UnifyFloat TermUnify Universal TermUnify Var nify Prolog atom nify Prolog structure nify Prolog structure nify Prolog integer nify empty Prolog list nify Prolog floating point number nify Prolog structure U U U U Unify Prolog list U U U U nify uninstantiated Prolog variable User s Guide 200 IF Prolog V5 3 Active C interface Simple C functions Check functions and exception handling An error context is a particular set of data which the Prolog system requires in order to generate an exception see also chapter 8 Before a C predicate is called Prolog clears the error context If a C predicate fails Prolog checks the error context If this is set Prolog initiates exception handling exception 2 C function Purpose ErrorContextClear Clear error context ErrorContextIsSet Check whether an error context is set ErrorContext Get Query error context ErrorContextSet Set error context i The term classification
247. ort are automatically compiled into syntacti cally correct clauses by the Prolog system when reading text with consult 1 reconsult 1 or compile 1 Each non terminal symbol is converted into a structure with an arity of 2 A grammar rule So 77 gt 81 82 3 5 5 Sne with non terminals sg Sn is compiled to form the clause sol Ro Rn a s Ro Ry s R Ra i lt Sn Rn 1 Rn Example sentence gt nominal_phrase verbal_phrase is compiled to form the clause sentence S_Input S_Tail nominal_phrase S_Input N_Tail verbal_phrase N_Tail S_Tail The variable names in the database have been replaced by more meaningful names in the text The two arguments in the structures so Ro Rn 5 Sn Rn 1 Rn each form a differ ence list the first argument is a list and the second argument is a list tail of the first list The elements in the difference list are obtained by subtracting the two lists i e by removing the elements of the list tail from the end of the first list Example The difference lists with the pairs List the dog barks Tail or List the dog barks the Tail the User s Guide 104 IF Prolog V5 3 Prolog grammar rules Simple grammar rules or List Tail the dog barks dog dog dog dog dog dog are all equivalent to the list Diff the dog barks On the other hand the list pair L1 the dog barks L2 runs does not form a diffe
248. ot be modified Following compilation their definitions cannot be modified by assert consult 1 retract 1 2 predicates If you attempt to make the predicate modifiable by calling the predicate modify_mode 3 the call will result in the exception static_ procedure e You cannot inspect compiled predicates with the predicates listing 0 1 or clause 1 2 because the Prolog source text required for this is no longer available e Tracing or debugging of the compiled predicates is not possible IF Prolog V5 3 143 User s Guide Loading files Module concept What gets compiled Basically the following applies Only predicates defined in the specified text file are compiled Undefined predicates occurring for example as subgoals or arguments are not compiled In a file containing the definition p X g X asserta remember X fail p 1 is compiled The predicates g 1 asserta 1 remember 1 are not compiled This example also illustrates a good use of an assert call within a compiled predicate Excluding predicates from compilation By using the directive dynamic PredicateList you may explicitly exclude predicates from the compilation All predicates specified after dynamic are not compiled Instead they are merely inserted into the database as with reconsult 1 when the file is loaded If your file contains the following Prolog source text dynamic child 1 child anita child sonja child julia
249. ows 11 2 5 Debug Menu Abort Terminates interactive debugger Continue Executes debugger to next breakpoint Step Processes to next monitored port Skip Skips monitoring of a current subgoal Fast Skip Skips fast fast skip monitoring of current subgoal Nonstop Executes to the end of proof Force to Succeed Forces success of current subgoal Force to Fail Forces failure of current subgoal Debug Ports Turns port monitoring on off in the debug Trace Ports Turns port monitoring on off in the trace View is a pop up submenu with the items Goal Box Displays goal box AND box in output window Clause Box Displays clause box OR box in output window Constraints Displays all constraints in output window Breakpoints Stops Displays list of all explicit breakpoints in output window Trace Displays trace field in output window See Also Debugger commands Debugger Toolbar Debugger Information Toolbar Console Window Debugger Support 11 2 6 Options Menu IF Prolog Options dialog window allows to set various parameters It has several subdialogs property pages General Opens the General property page in IF Prolog Options dialog Prolog Flags Opens the Prolog Flags property page in IF Prolog Options dialog Preprocessor Options Opens the Preprocessor Options property page in IF Prolog Options dialog Configuration Opens the Configuration property page in IF Prolog Options dialog Cross Reference Options Opens the
250. p to next monitored port P Skip Execute up to exit of a subsequent subgoal l Fast skip Accelerate execution to exit of the subse quent subgoal o Nonstop Continue without stopping T Force to succeed Force current subgoal to succeed T Force to fail Force current subgoal to fail A Back clause Branch back to clause head l Back parent Branch back to CALL port of parent goal Al Back subgoal Branch back to CALL port of subgoal DER Set debugger ports Configure the debugger Set tracer ports Configure the tracer ES gt See Also Debugger Commands 11 33 Debug Information Toolbar The IF Prolog debugger toolbar with additional information on debugging gynae ojo lees ol 1 3 13 dj Figure IF Prolog debugger toolbar with additional information on debugging Second IF Prolog debugger toolbar contains buttons for debugger commands that provides additional information on debugging such as different views goal clause structure lists breakpoints modules constraints screen history debugger help This is a list of toolbar buttons with descriptions Button Command Description amp Display the goal box Display the goal box in the window Display the clause box Display the clause box in the window IF Prolog V5 3 251 User s Guide MS Windows Graphical User Interface Y O 5 Command Description la la 18 la CA le a a jeje ole la Bla la la E Trace current port
251. p two buttons of this window to determine firstly whether a variable is to be monitored during goal execution or whether goal execution is to be regularly debugged Stop Secondly you can specify whether you wish to set an interactive or non interactive breakpoint interactive The button Predicate Selection allows you to specify the predicate of the program for which you wish to set a breakpoint With the button Ports Selection you determine the port at which the breakpoint is to be set With Conditions Selection you can define the conditions see also page 172 which must be satisfied before goal execution is interrupted at the breakpoint Finally Actions Selections allows you to define all the actions which are to be per formed after stopping at the breakpoint All Selections buttons open a separate window in which you make relevant entries IF Prolog V5 3 187 User s Guide OSF Motif interface Debugging SEES A Set Stop Action mi O help O set trace depth O back parent O activate stop Ol set trace length Ol back_subgoal O break O stop O continue O call O stop interactive O error O deactivate_stop O system O exit O halt O trace O fail O port O trace_port O fast_skip E remove stop O unify O nonstop O set_debug_depth O abort O skip O set history Ol back_clause O step Possible actions at an explicit breakpoint If you debug the example from page 166 under the OSF Motif user interface the
252. part of the module body The meta directive has the argument PredicateList corresponding to a list of predicate indicators Each predicate in the list is declared as a metapredicate Each predicate is also User s Guide 138 IF Prolog V5 3 Module body Defining operators implicitly declared as private This prevents the private definition of a predicate from being overlaid by an import directive Local definition of metapredicates is seldom required since a local metapredicate is usually called only within the module If the module argument is implicitly assigned by the module system it will by default bear the name of its own module i Each metapredicate must be declared before it can be defined You may not declare a predicate which you have exported or reexported locally as a metapredicate 7 4 7 Defining operators The op directive allows you to declare and define operators in the module body and module interface The op directive differs from the predicate op 3 only in as much as the directive is evaluated at load time and thus takes effect immediately The operators which you define with the predicate or directive have global validity How to define and delete operators is described in the section on Syntax in the IF Prolog Reference Manual 1 Example In the module expression expressions of the type Result Expression are evaluated as follows First all subexpressions are evaluated and then a predic
253. pecifies the point of restart following a re entry The parameters must match those given in the DETACH call ProgPtr Number 4 1 belonging to Label at which restart is to occur Label Name of the point of restart Source code define RESUME no label case no goto label HASEPILOG announces that the function has an epilog which can be activated if necessary Source code define HASEPILOG case 1 goto _epilog BEGIN comes immediately before the actual program code Source code define BEGIN default FAIL User s Guide 212 IF Prolog V5 3 Active C interface Linking a coroutine void PrologEnableEpilog void This function must be called immediately after BEGIN if the complex C function has an epilog HASEPILOG DETACH ProgPtr Label The function is exited ProgPtr is the return value A new entry point is defined The parameters must match those given in the RESUME call ProgPtr Number 4 1 belonging to Label at which restart is to occur Label Name of the point of exit from the function Source code define DETACH no label return no label void PrologUndo void Unifications in the current call are undone FAIL The coroutine instance is removed It may be necessary for you to include an epilog to ensure that possible side effects are correctly handled Source code define FAIL return 0 EPILOG Defines a label to which a branch will be made if the coroutine instance is r
254. plicitly set to the name of the calling module 1 uses this to execute Goal in the calling module i It is possible to simulate a predicate call from any module by explicitly specifying the module argument when using metapredicates This is important particularly in the implementation of metainterpreters if they are to operate on programs spread over a number of modules Explicit qualification of predicate activators Irrespective of its visibility any predicate can be called system wide by means of explicit qualification A predicate is explicitly associated with a module in which it is visible using the visibility operator 2 You can use the visibility operator as follows Module Activator It is not necessary for the Predicate called by Activator to have been exported from the specified Module or for the Module to have been imported into the current module You only need to know the module in which the predicate you wish to qualify is visible If you use the visibility operator both the Module and the Predicate must be instantiated at load time However the predicate need not be defined at load time i You can explicitly qualify predicates only you may not qualify language constructs Implicit qualification of predicates When a term is transformed into a goal all the predicates not explicitly qualified are implicitly qualified i e identified with their original module The only exceptions to this tr
255. plier sprintf query supplier s Article Amount Price Amount gt 0 argv 1 tmcontext TermContext if ParseProlog query amp context Output information for syntax error fprintf stderr SYNTAX s n ParseError context pc_errno leave_application 1 User s Guide 226 IF Prolog V5 3 C interface C device drivers cursor PrologOpen UserModule context pc term context pc_varlist if cursor NULL gt TERM error printf Delivery from s n n argv 1 printf 15s s 9s n Article Amount Price while PrologFetch cursor const char article long amount double price if PrologIsString cursor Article amp article amp amp PrologIsInteger cursor Amount amp amount amp amp PrologIsFloat cursor Price price 1 printf 15s 31d 10 21f n article amount amount price if PrologError cursor amp error printt ERROR t n error PrologClose cursor gt free terms created by ParseProlog TermCollect tmcontext NULL leave_application 0 The program is compiled and linked It can now be called for example as follows supply bakery Delivery from bakery Article Amount Price bread 3 7 50 cake 0 38 00 cookies 600 1 25 IF Prolog V5 3 227 User s Guide C device drivers C interface 10 3 C device drivers In IF Prolog you can write your own device drivers in C and link
256. put Send get_until Receive n Result _ write Result nl The program Server which overlays the child process may be implemented in any language It could for example be the following Prolog program file server pro program write ready n flush_output repeat get_until n Command Stop evaluate Command Stop end_of_file evaluate Command atom_chars Command CharList exception_handler number_chars Number CharList User s Guide 94 IF Prolog V5 3 Input output interface Process management error syntax_error illegal number _ fail integer Number calculate square Number evaluate Command write Command write nl flush_output calculate_square Number Limit is sqrt maxint Number lt Limit Square is Number Number write Square nl flush_output calculate_square _ write overflow nl flush_output A dialog such as the following could take place user activate prolog c server pro gt 45 2025 gt 56 3136 gt 5678 32239684 gt 99999 overflow gt stop stop gt quit 5 4 4 Predicates for process communication using sockets The Prolog system provides the following predicates for process communication using sockets IF Prolog V5 3 95 User s Guide Process management Input output interface Predicate Purpose current socket Domain
257. r 2 set_stream_position 2 seek 1 stream_type 2 stream_type 1 stream_property 2 tell 1 tty_size 2 3 tty_size 2 The following predicates allow you to access device drivers Predicate Meaning current_device_control Device Command Arity current_stream_control Q Stream Command Arity device control Device Command stream control AStream A Command stream_device Q Stream Device stream _property Stream Info stream_type Stream StreamType Query information about device drivers Query information on device drivers Control an input output device Control an input output stream Determine device name of an input output stream Query information on input output streams Query type of an input output stream IF Prolog V5 3 233 User s Guide Device driver functions 10 3 3 Functions for implementing device drivers The C interface supports the implementation of device drivers with the following functions Linking a device driver into Prolog The function DeviceCreate declares the new device driver to the Prolog system The function has as its argument a pointer to a structure of type t device in which you have defined all the properties of the new device driver include device h extern void DeviceCreate const t device device const t device device is a pointer to the structure of type t device which contains all the properties of the new device driver You should cr
258. r example user default_editor _ emacs gt This selects the editor emacs From now on edit 0 1 will cause emacs to be invoked Possible errors Three types of error or exception can occur e Exceptions when the call is made such as Filename is not a valid operating system file name the file cannot be created or modified because the user does not have the proper access permissions e Errors during editing such as typing errors incorrect use of editor commands see relevant editor description e Errors and exceptions when the modified file is reconsulted such as the file does not exist because you exited the editor with a command which did not save the text in a new file the file contains invalid Prolog clauses These clauses are not included in the database when reconsult 1 is called The Prolog system issues appropriate syntax error messages the file contains clauses associated with a predicate which cannot be changed see modify_mode 3 These clauses are not included in the database The Prolog system generates an appropriate error message i Deleting a predicate from the file does not automatically delete it from the database see the description of reconsult 1 in the IF Prolog Ref erence Manual 1 retract 1 2 or abolish 1 2 remove only non compiled predicates from the database IF Prolog V5 3 89 User s Guide File processing Input output in
259. ramming language is often a set of sentences generated deductively with a predefined grammar 101 Context free grammar Prolog grammar rules A grammar will be particularly amenable to automatic processing if all of its production rules have the following structure So may consist of S followed by S2 followed by 53 followed by S followed by Sj Or expressed concisely in operator notation So S 52 53 Si See Ope Nil In this case the start symbol Sy is a non terminal symbol and the body elements Sj Sn are non terminal or terminal symbols This type of grammar is known as a context free grammar In a context free grammar the start symbol is used to generate a sentence in the following way deriving the start symbol e Begin with the start symbol e Replace each non terminal S by the right hand side of a production rule S e Repeat this until a sequence of terminal symbols is left over This sequence is then a sentence in the language The set of sentences derived in this manner is called the language generated by the grammar Example A grammar consisting of Terminals the dog barks runs Non terminals sentence nominal_phrase verbal_phrase article noun verb Production rules sentence gt nominal_phrase verbal phrase verbal_phrase gt verb nominal phrase gt article noun article gt the noun gt dog User s Guide 102 IF Prolog V5
260. re arriving signals The Prolog system does not receive these signals at all e Passive handling by the Prolog system this means that the arrival of the signal is registered but it does not affect program execution Active handling by the application or the Prolog system If the application provides signal handling the routines are executed otherwise the Prolog system takes control and initiates a context jump throw 1 As with exceptions active handling of a signal by the application is possible only within a local context With the predicate current_signal 2 you can query the signal modes which were active on the first call even if they have changed in the meantime logic update view Example user current_signal S M write S M nl fail user_2 default user 1 default pipe default termination default IF Prolog V5 3 155 User s Guide Passive signal handling Contexts quit default alarm default abort default interrupt on no 8 3 1 Global control of signal handling You can selectively activate disable or delay signal handling for individual signals The predicate signal control 2 allows you to globally control signal handling you may specify the following commands for a signal Mode Meaning clear Handling of signals which have been received but not yet processed is suppressed The number of signals still to be processed is set to 0 default When the signa
261. reakpoint at the redo port off Trace Activate deactivate listing for the redo port wakeup on Debugger Breakpoint at the wakeup port off Trace Activate deactivate listing for the wakeup port suspend on Debugger Breakpoint at the suspend port off Trace Activate deactivate listing for the suspend port trymatch on Debugger Breakpoint at the trymatch port off Trace Activate deactivate listing for the trymatch port failmatch on Debugger Breakpoint at the failmatch port off Trace Activate deactivate listing for the failmatch port enterbody on Debugger Breakpoint at the enterbody port default off off Trace Activate deactivate listing for the enterbody port default on exitbody on Debugger Breakpoint at the exitbody port default off off Trace Activate deactivate listing for the exitbody port default on redobody on Debugger Breakpoint at the redobody port off Trace Activate deactivate listing for the redobody port failbody on Debugger Breakpoint at the failbody port off Trace Activate deactivate listing for the failbody port User s Guide 162 IF Prolog V5 3 Debugging Tracing Example user debug_config exitbody _ on lt activates the exitbody port in addition to the external ports All other internal ports remain deactivated 9 1 2 Debug mode of a predicate The debug mode of a predicate allows you to define whether a predicate can be debugged The debug mode of a predicate applies during both
262. rence list The first list in a difference list is frequently referred to as an input list 6 2 2 Terminals in a production rule Unlike the non terminal symbols of a production rule the terminal symbols do not have equivalents in the associated subgoals of the compiled rule They should correspond directly to tokens from the input list and are therefore taken into account in both parts of the difference list which implement the processing of this input list Example The grammar from the first example is extended by adding the production rule sentence gt nominal phrase and verbal phrase After consulting the database contains the clause sentence SE SR nominal_phrase SE SO SO and S1R verbal_phrase S1R SR The format and SIR of the list tail implies that it must begin with the terminal sym bol and and that the remaining tail unified with SIR is available for further syntactical analysis In general a terminal symbol may be located e at any position in the body of the production rule e after the non terminal symbol in the head of the production rule Terminal symbols at the beginning of the production rule body are inserted by the compiler at the beginning of the input list In all other cases the terminal symbols are added at the beginning of the list tail of the preceding non terminal symbol IF Prolog V5 3 105 User s Guide Simple grammar rules Prolog grammar rules Example In t
263. rent error stream error_tell Stream Set and if required open current error stream error_telling Stream Query current error stream error_told Reset current error stream to user_error and if required close the first stream When you declare a file as the current input output stream you should first open it with the predicate open 3 4 and then define it as the current input output stream with one of User s Guide 82 IF Prolog V5 3 Input output interface Elementary input output the above predicates If you wish to declare a pipe as the current input output stream you should first create it with unix_make_pipe 1 and open it with open 3 4 Then you define the pipe as the current input output stream With the following predicates input is using the current input stream and output is using the current output stream Predicate Purpose get_byte ByteCode Read a byte get_char Char Read a character get_code CharCode Read a character get_until SearchChar Text EndChar Read up to a specified character nl Write new line print Term Write a term put_byte ByteCode Write a byte put_char Char Write a character put_code CharCode Write a character read Term Read a term read_term Term Options Read a term with control options skip_line Skip input line write Term Write a term write_canonical Q Term Write a term in normal structure repre sentation and in single quotes write_forma
264. represent any term A metapredicate has an additional argument which specifies the module in whose context it is to be executed User s Guide 292 IF Prolog V5 3 Appendix C Glossary module module body module interface operator overflow parser postfix notation predicate prefix notation predicate indicator principal functor procedure Prolog flag Prolog text proof proof tree qualification query recursion resolution rule side effect stack statement Logical collection of predicates a module has a module interface and a module body which are interlinked by a common module name Private portion of the module introduced by the begin module directive and containing a declaration part and an implementa tion part Public portion of the module introduced by the module directive Atom which may occur as a functor in prefix infix or postfix notation The notation of the operator is determined by the predicate or the directive op 3 An operator has an precedence and an associativity which make it possible to determine which arguments and terms of different or matching precedence can be assigned to one another A calculated value is greater than the storage location provided for it Component of the Prolog system which converts character strings into Prolog terms Structure notation where the operator follows the operands e g A B A relation identified by fun
265. res a powerful module concept designed to provide you with support for creating large program packages and program libraries In a module a set of predicates and clauses are combined to form a logical unit The predicates are visible only within the module but you can also make one or more predicates visible for other modules export Modules thus allow you to conceal implementation details information hiding and avoid naming conflicts The predicates which you have exported can be made visible in other modules import The set of exported module objects therefore constitutes the interface to other modules The built in predicates of IF Prolog are also stored in modules When Prolog is called the modules of the standard system are visible The extensions to the Prolog system e g for the OSF Motif interface or the constraint package are stored in separate modules which you must import Once you have imported a module all the predicates exported there are visible in your own module You may not redefine the modules of the Prolog system The built in predicates are stored in the following modules Module Meaning system Contains the built in predicates of the standard system system Further system modules user Contains user defined predicates which are not assigned to any other module motif Contains the predicates of the OSF Motif interface informix Contains the predicates of the Informix interface const_delay Contain the predi
266. rgument 121 metapredicate 121 module argument 121 module body 132 IF Prolog V5 3 303 User s Guide Index objects 119 original module 120 predicates 120 query known in database 115 network host 97 service 97 NIS 98 non terminal symbol 101 notational conventions 2 null device 79 objects module 119 occurs check 35 op directive 139 operating commands alphanumeric user interface 177 operation window OSF Motif user interface 184 operator define 139 option 62 call Prolog 62 organization Prolog system 61 original module 120 output passive C interface 221 output window alphanumeric user interface 176 parameter passing between C and Prolog 202 parse a goal 223 passive signal handling 157 passive C interface 195 application 222 central functions 220 cpro h 222 formatted output 221 framework 222 passive goal convert to active goal 223 create 223 passive Prolog goal 220 pathname specify 65 permission_error error type 151 phrase 101 pipe device 79 signal 155 predicate 30 arity 30 built in 32 87 call types 32 compiled 31 context global action 147 declare as dynamic 137 declare as metapredicate 138 declare as modifiable 137 declare as private 136 dynamic through call 62 exclude from compilation 144 export 127 functor 30 mix compiled and interpreted 142 module object 120 monitorable 163 name 30 overwrite 146 qualify 123
267. rt 2 predicate and declares it public Initially it remains visible only in the sort module Exported predicates from other modules are made visible with the import directive Thus in the above example the import directives make the following predicates visible in the sort module e permute 2 from the original module permutation e ordered 1 from the original module order 7 3 2 Exporting metapredicates Using the meta directive you can export metapredicates in the module interface and define them in the declaration part of the module body This section describes the semantics of the meta directive in the module interface The meta directive has the argument PredicateList which corresponds to a list of predicate indicators The predicates in the list are declared publicly as metapredicates thus you can make them visible in other modules using the import directive You can export only metapredicates with the meta directive You must use the export directive to export all other predicates All the predicates which you have made public with the meta directive in the interface are implicitly declared as private This prevents an import directive in the module body from overlaying the private definition of a predicate i You may not export metapredicates with the export directive Example This example defines the module output It exports the predicate portray 1 and the metapredicate output 1 output 1 will output a term in
268. rts User s Guide 148 IF Prolog V5 3 Contexts Context jumps Example Let us assume that the following predicate is defined test H H test throw hello When it is called the argument H is first unified with the atom test next a context jump is initiated If a context is defined and the predicate proved in this context the instantiation of the argument of test 1 is undone when the context jump is executed user context test Z catch Y gt true Z _90 Y hello yes The following predicate reads Prolog terms from a file and outputs the number of terms count_file File open File read Stream context count_terms Stream 0 catch end_of_file Count gt count_write Count close Stream count_terms Stream Count read Stream Term check_term Term Count NewCount is Count 1 count_terms Stream NewCount check term end of file Count throw end_of_file Count check_term _ _ count_write Count write Count write terms counted n There is a special predicate for context jumps which creates a context which only han dles context jumps as context global actions The predicate catch Goal CatchMask SubstituteGoal creates a context for the execution Goal If in the course of execution Goal the goal throw Term is executed where Term may be unified with CatchMask IF Prolog V5 3 149 User s Guide Ex
269. s this variable is instantiated with the result of the predicate call The type of result from the predicate call is indicated in the section on Arguments in the full description Several call patterns are possible for some predicates In some predicates where an integer is expected as an input parameter it is also possible to input an arithmetic expression with an integer value an expression will be evaluated implicitly as with is 2 Examples If uninstantiated variables are permitted in the mode they will not be instantiated The argument of atom 1 has the call mode It is therefore possible to specify a variable in order for example to test whether it is instantiated to an atom This variable will not be instantiated however The three arguments of current_op 3 have the call mode If all three arguments are uninstantiated variables you can obtain all defined operators by means of backtracking user current_op Precedence Type Name Precedence 50 Type xfx Name tog Precedence 100 Type xfx Name etc until you terminate backtracking The instantiated arguments are unified the uninstantiated variables are instantiated The following queries operators with precedence 400 user current_op 400 Type Name IF Prolog V5 3 33 User s Guide Unification Database and procedure execution Type yfx Name gt gt Type yfx Name lt lt yes 3 3 D
270. s Pro log flag is the value of the system parameter sp if specified otherwise it is the value of the environment variable PROPATH or it is empty IF Prolog V5 3 261 User s Guide MS Windows Graphical User Interface read_error error fail quiet report_read_error on off warnings on off unknown error fail warning debug on off Syntax errors found by analysis with read_term 2 3 etc raise an exception Syntax errors found by analysis with read_term 2 3 etc are reported directly by the parser in a message and the predicate fails Syntax errors found by analysis with read_term 2 3 etc are not reported and the predicate fails Syntax errors found by analysis with parse_atom 6 are reported directly by the parser in a message Syntax errors found by analysis with parse_atom 6 are not reported by the parser but can be queried with the read_error 2 3 predicates For consult 1 etc warnings are output for possible errors For consult 1 etc no warnings are output If a predicate is to be activated which is not defined an exception is raised This does not apply to pred icates for which the exception mode has been set to on see predicate_mode 3 If a predicate is to be activated which is not defined backtracking is initiated i e the predicate is evalu ated with fail If a predicate is to be activated which is not defined a warning is output and backtracking is initiated
271. s a file called startfile cmp if no such file is found it attempts to consult a file called start file then if this file also does not exist it consults the file start file pro Default option st not specified Prolog checks whether the environment variable PRORC is set and it it is tries to load the file with this name Otherwise the system searches the HOME directory x for a file called prorc cmp if the file exists it is loaded x for a file called prorc if the file exists it is consulted x for a file called prorc pro if the file exists it is consulted i If one of the files which you specified in the options st c or 1 could not be found an appropriate exception is generated and the Prolog process is terminated userargument You can define your own arguments and specify them in accor dance with your own syntax rules when calling an extended Pro log system These arguments are called user arguments i e you determine the form in which the arguments are specified e g with or without options and what effect the arguments have in your Prolog application In Prolog you can query the user argu ments specified in the call by means of user_parameters 1 User s Guide 66 IF Prolog V5 3 Prolog system Calling Prolog Example 1 Calling without user arguments prolog c test Prolog is called with the default values and the start file if one is available is read in T
272. s for debugger Activate deactivate ports for trace Define structure depth for debugger Define structure depth for trace Set length of trace field Set number of clause and goal boxes to be stored Set explicit breakpoints Set explicit interactive breakpoints The settings remain in effect until you explicitly change them By defining the predicates debug_begin 0 and debug_end 0 you can specify start and end processing operations which are to be performed automatically whenever the debugger starts and exits You can temporarily exit the debugger e by using the debugger command B or break to start a new Prolog loop You can then work in the Prolog dialog until you enter end_of_file The Prolog system then returns automatically to the debugger e by using the command call or its short form ca to call a goal outside the control of the debugger e by using the command system or its short form S to start an operating system subshell outside the Prolog system When the debugger has proved the test goal it terminates itself automatically and you are returned to the Prolog top level dialog The top level dialog prompt appears on the screen user 9 3 2 Breakpoints Breakpoints are always associated with the ports of the box model The debug mode of a predicate determines which ports are permitted as breakpoints Where a predicate is moni torable all thirteen box ports are possible breakpoints Where a predicate is not monitor
273. s into the database If you want to enter a number of rules and facts it is better to write these clauses directly into a file or to work in a Prolog dialog with consult 1 Entering several clauses Using the built in predicate consult 1 you can enter a number of clauses in succession in the database This saves you having to call assertz 1 separately for each clause Type user consult user gt With consult 1 the atom user is used to address standard input and standard output After this call several clauses can be entered sequentially from standard input usually the keyboard Prolog indicates this by displaying a different prompt on standard output usually the screen gt You can now enter line by line all the facts and rules you need for your program Remember to terminate each clause with a period followed by the gt key The Prolog system then immediately examines each clause for correct syntax A syntactically correct clause is stored into the database immediately Syntax errors are reported You must then enter the erroneous clauses correctly Your final input is end of file or the key combination crrr p The Prolog system then displays the prompt user again If you take a closer look at the family tree for the Greek deities you will come across some incestuous relationships The following rules will enable such relationships to be deduced First call
274. s predicate is declared as dynamic IF Prolog V5 3 137 User s Guide Declaring metapredicates Module body module storage administration export receipt 2 outlet 2 stock 2 begin module storage administration dynamic stock 2 stock screws 50 stock nails 100 stock fillets 20 receipt Product Quantity retract stock Product So_far lis NewQuantity is So_far Quantity assertz stock Product NewQuantity receipt Product Quantity assertz stock Product Quantity outlet Product Quantity stock Product So_far So_far gt Quantity NewQuantity is So_far Quantity retract stock Product So_far assertz stock Product NewQuantity end module storage administration i If you export a dynamic predicate it cannot be modified in a different module You may modify a dynamic predicate in the original module only This restriction is lifted by explicitly specifying the original module of a dynamic predicate retract stock Product Quantity storage administration The following call of assertz 1 in a different module would not affect the module storage_administration assertz stock P Q 7 4 6 Declaring metapredicates The meta directive allows you to export metapredicates in the module interface and to declare metapredicates in the declaration part of a module body This section describes the semantics of the meta directive in the declaration
275. s six memory areas and also performs garbage collection in those ar eas as required You may query the parameters for the memory areas with current_ memory_management 3 and set new parameters with set_memory_management 3 You can use garbage_collection 1 to initiate garbage collection for a specific memory area IF Prolog manages the following memory areas User s Guide 72 IF Prolog V5 3 Prolog system System information Area Meaning database Entire database constraints Stack for constraint processing extension Stack for term extensions local Stack for local variables and choice points global Stack for structures trail Stack for term instantiations which are to be undone during backtracking Example user statistics memory statistic size used gc collected gc time database 962544 490560 0 0 0 00 global 30720 496 0 0 0 00 constraints 8192 0 local 30720 700 0 0 0 00 extension 8192 32 0 0 0 00 trail 8192 0 0 0 0 00 total 1048560 576576 0 0 0 00 date Fri Sep 9 14 32 43 1996 cputime 1 40 yes You can query or set the following parameters for any of these memory areas Parameter Meaning minimal size Minimum size of memory area in bytes maximal size Maximum size of memory area in bytes default_gap Percentage in relation to memory used indicates the minimum percent age of memory used which must remain free before further memory is allocated minimal_gap M
276. screen lt Display previous goal or clause box view_last_screen lt lt Display last goal or clause box view_next_screen gt Display next goal or clause box view _first_screen gt gt Display first goal or clause box view history vh Display current goal or clause box view stops vs Display explicit breakpoints view_trace vt Display trace field Scroll forward line by line in the information field Scroll forward page by page in the information field Scroll back line by line in the information field Scroll back page by page in the information field snapshot sn Output current information field to a file Setting the configuration Command Short Meaning form port p Turn port monitoring on off trace port tp Turn port monitoring on off in the trace set debug depth sdd Set structure depth set trace depth std Set structure depth set trace length stl Set length of trace field set history sh Set length of history field trace t Turn trace on off User s Guide 192 IF Prolog V5 3 Debugging Debugger commands Controlling the debugger Command Short Meaning form abort A Terminate interactive debugger break B Initialize new input loop halt H Terminate Prolog session system S Start shell call ca Call Prolog goal Working with breakpoints Command Short Meaning form remove_stop rs Delete explicit breakpoint stop st Set explicit breakpoint stop_interactive si Set interactive breakpoint deactivate_st
277. signation for an open input output channel to a file terminal or printer for example This designation is created by the Prolog system and can also be referenced by means of a user defined alias name Term consisting of a functor and a sequence of arguments The number of arguments is the arity of the term A structure is written in functor notation as follows functor followed with out intervening space by an opening left parenthesis followed by comma separated arguments and a closing right parenthesis If the arity is zero the term is also referred to as an atom and is written without parentheses For structures with an arity of one or two an alternative notation may be used if the functor has been defined as a prefix infix or postfix operator Term whose proof must succeed in order that the given goal may be proved One of the objects variable number atom or structure Memory area in the Prolog system for temporary storage of vari able instantiations Predicate which must be defined by the user Method for making two terms identical through the substitution of variables User s Guide 294 IF Prolog V5 3 Bibliography 10 11 12 13 14 IF Prolog V5 3 Reference Manual IF Prolog V5 3 User s Guide IF Prolog V5 3 OSF Motif Interface IF Prolog V5 3 Informix Interface IF Prolog V5 3 Constraints Package IF Prolog V5 3 Quick Reference IF Prolog V5 3 Windows Interfaces IF Prolog V5 3 J
278. standard stream_type Arg see stream type 2 stream size N Unifies N with the size of the file isatty stream control 2 succeeds if Stream is a terminal fileno N Unifies N with the operating system dependent file number of the file clear eos see clear eos 0 1 clear eol see clear_eo1 0 1 clear screen see clear screen 0 1 tty size Z S see tty size 2 3 move_cursor Z S see move cursor 2 3 pipe stream_type Arg see stream_type 2 fileno N Unifies N with the operating system dependent file number of the file socket stream_type Arg see stream type 2 fileno N Unifies N with the operating system dependent file number of the file User s Guide 232 IF Prolog V5 3 Device driver functions DeviceCreate string stream_type Arg see stream type 2 stream_size N Unifies N with the size of the string string Arg Unifies Arg with the contents of the string seek P see set_stream_position 2 tell P see stream_property 2 null stream_type Arg see stream_type 2 get_last Z S see get_last 2 3 There are a number of predicates which are mapped to special control functions If you wish to support these predicates with the device driver you must specify the corresponding control functions for the input output unit Predicate Control function Arity clear_eol 0 1 clear_eol 0 clear_eos 0 1 clear_eos 0 clear_screen0 1 clear_screen 0 stream copy 2 dup 0 stream copy 2 fileno 1 get_last 2 3 get_last 2 move_cursor 2 3 move_curso
279. streams Input output predicates in which no stream is specified address the current streams The predicate listing 0 1 also sends its outputs to the current output stream The Prolog system issues error messages on the current error stream When the Prolog system is started the corresponding standard streams are first opened i e Stream Alias Default Current input current_input user input standard input Current output current_output user output standard output Current error output current_error user_error standard error output You can refer to the current streams by their alias names in input output predicates which require a stream to be specified You can query or change the settings for the current streams with the following predicates Predicate Purpose current input Stream Query current input stream set_input Q Stream Set current input stream see Q Stream Set and if required open current input stream seeing Stream Query current input stream seen Reset current input stream to user_input and if required close the first stream current_output Stream Query current output stream set_output Stream Set current output stream tell Stream Set and if required open current output stream telling Stream Query current output stream told Reset current output stream to user_output and if required close the first stream current _error Stream Query current error stream set_error Stream Set cur
280. t and e it can be reentered as though it had never been exited all variables and the control structures are exactly as they were when the exit was made This process can be repeated more than once at different points These similarities with the characteristics of the box model already suggest how the simula tion will look Coroutine operations are assigned to the box ports as follows call generate exit start exit Coroutine exit restart fail remove redo call gt generate and start A coroutine instance is generated initialized and started i e the search for the first solution is initiated exit lt exit The coroutine instance is exited but not removed i e local vari ables processing position etc are stored so that the same in stance can be executed again later redo restart The variable instantiations made during the previous execution of the instance are undone The instance is executed again the processing position and local variables are taken from the previ ously stored values fail gt exit and remove The coroutine instance is exited and removed Simulating a coroutine in C In C you must simulate the coroutine concept by means of C functions You require the following for this purpose User s Guide 206 IF Prolog V5 3 Active C interface Complex C functions e a function with result type int This function handl
281. t options offered by the syntax of the grammar rules are explained above Below you will find the entire syntax of this form of grammar described with these very same production rules Then we will deal with one of these rules which has not yet been discussed grammar gt g_rule grammar gt g_rule grammar 2 g_rule gt g rule head 1 gt g_rule_body 3 g_rule_head gt non terminal h4 g_rule_head gt non terminal terminal 5 User s Guide 112 IF Prolog V5 3 Prolog grammar rules Production rule syntax g rule body gt body element 6 g_rule_body gt g rule body 7 7 g_rule_body ht g_rule_body gt body element body_element 18 g_rule_body gt g rule body g_rule_body 9 g_rule_body gt P C body element body element 10 body element gt 11 body element gt prolog goal P 12 body element gt P g rule body 13 body element gt non terminal 14 body_element gt terminal LS terminal gt 16 terminal gt token_sequence 417 token_sequence gt token 18 token_sequence gt token token_sequence 19 Here token stands for the smallest syntactical unit of the language described by the gram mar Any Prolog structure is permitted as a token
282. t output stream and set it at a later time S_IO_NBUF The Prolog system is not to buffer the data S_IO_LBUF The Prolog system is to buffer the data line by line S_IO_FBUF The Prolog system is to buffer all data S_IO_BUFFER This flag is equivalent to logically ORing the buffer flags S_IO_NBUF S_IO_LBUF and S_IO_FBUF and can be used as a mask S_IO_EA_ERROR If a further read operation is attempted after the end of the file has been reached an exception is to be generated S_IO_EA_RESET If a further read operation is attempted after the end of the file has been reached the input output stream is to be reset S_IO_EA_EOF_CODE IF Prolog V5 3 229 User s Guide C device drivers C interface All predicates which read from the stream behave as though the end of the input has been reached for the first time and will return end of file S I0 EOF ACTION This flag is equivalent to logically ORing the input output flags S 10 EA ERROR S IO EA RESET and S I0 EA EOF CODE and can be used as a mask With open 3 you cannot set any options consequently none of the flags S_IO_NBUF S_IO_LBUF and S_IO_FBUF is set when dev_open is called If your device driver does not set any of these three flags then the flag S_IO_FBUF will be set automatically when dev_open is exited Even when there are technical reasons why the device driver itself provides the buffering you should set mode correctly If no option is set for eof action in the predi
283. t the interrupt point The call signal_handler Goal Signal Handler is therefore identical to context Goal signal Signal gt Handler The predicate context 2 has the advantage that different types of events can be handled in one context Example The following predicate defines a daemon which becomes active when the signal user_1 arrives and waits for further signals at the end of the activity The daemon terminates when the signal user_2 arrives daemon WorkGoal signal_control user_1 on signal_control user_2 on context sleep catch end_of_work gt true signal user_1 gt work WorkGoal signal user_2 gt throw end_of_work User s Guide 158 IF Prolog V5 3 Contexts Active signal handling sleep repeat fail work WorkGoal call WorkGoal IF Prolog V5 3 159 User s Guide Active signal handling Contexts User s Guide 160 IF Prolog V5 3 Chapter 9 Debugging This chapter describes the debugging facilities which consist of a tracer and an interac tive debugger These tools assist the programmer when debugging and correcting Prolog programs The programmer can e monitor the sequence of execution e halt execution e intervene in the processing e query information This has the advantage of allowing clause selection to be monitored in addition to subgoal calling and exiting The debugging tools are based on the extended box model which is described in chapter 3
284. tabase you use the predicate retract 1 user retract mother gaia uranus yes Now use the predicate listing 0 to obtain a listing of the database you will see that the fact has been deleted To insert the fact in the database again enter user assertz mother gaia uranus yes IF Prolog V5 3 23 User s Guide Storing clauses in file Sample session Deleting all clauses of a predicate To delete all clauses of a predicate from the database you use the built in predicate abolish 1 The following abolish 1 call deletes all clauses of the predicate with functor ancestor and arity 2 user abolish ancestor 2 yes The ancestor rules are now deleted from the database but still exist in the gods pro file i Prolog answers yes even if the clause you want to delete cannot be found in the database You should therefore be careful when deleting clauses If for example you mistype one of your argument entries it could occur that the intended predicate is not deleted because it could not be found In spite of this no error message will be issued Modifying several clauses If you want to modify a number of clauses in the database the most convenient method is to use edit 0 1 This ensures that the changes are applied both to the file and to the database at the same time But if you prefer you can also modify the database directly in a Prolog dial
285. takes this value in account if a term is to be output with the format w 11 7 3 Preprocessor Options This is the Preprocessor Options property page IF Prolog Y5 2 Options aaga Figure Preprocessor Options property page Prolog files can be preprocessed before consulting or compiling You can set this option in Preprocessor Options property page Preprocessor is invoked only by menu or button commands It is not invoked by predicates consult 1 reconsult 1 or compile 1 User s Guide 264 IF Prolog V5 3 Graphical User Interface MS Windows Option Description Use preprocessor before consulting Use preprocessor before compiling Start compiler with ISO Start compiler with warnings flag set to off Use IF Prolog search path for compiler Preprocessor definitions Preprocessor parameters Preprocessor is activated before consulting or reconsulting a file invoked by menu or button command Preprocessor is activated before compiling a file invoked by menu or button command Compiler is started with iso flag Compiler is started with nowarnings flag Compiler is started with sp flag List box with preprocessor definitions Def initions e g define SYSTEM are ap pended to the preprocessor parameters e g DSYSTEM and you can use ifdef or if defined blocks in your code Preprocessor parameters can be displayed as they are used for compiling or for consulting
286. tapredicates Predicates for accessing global variables Predicate Purpose current_global Name Query global variables get_global Name Value pop_global Name Value push_global Name Value set_global Name Value unset _global Name Query the value of a global variable Remove value of a global variable Set value of a global variable Set value of a global variable Delete a global variable If you explicitly specify the meta argument these predicates are executed in the context of the specified module In this way you can access the global variables of these modules Example The following example shows how you can modify global variables in other modules module generator IF Prolog V5 3 125 User s Guide Module interface export reset 0 gensym 1 begin module generator reset set_global index 0 gensym Symbol get_global index Index NewIndex is Index 1 set_global index NewIndex concat_atom symbol_ Index Symbol init set_global index 0 module objects begin module objects import generator create_object Name gensym Name reset N set_global index N generator 7 3 Module interface A module consists of two parts the module interface and the module body which may be both physically and logically separate The link between the two parts is established by their common module name The module interface and th
287. tch off 163 switch on 163 debug 1 169 debug_mode 3 163 debugger OSF Motif user interface 183 alphanumeric user interface 174 branch commands 173 clause box 178 debug mode 163 exit temporarily 170 goal box 177 operation 168 session 168 settings 161 169 declaration part module body 133 default for current streams 82 input output stream 81 default database 87 define memory block size 63 delivery Prolog application 283 depth first search 34 derivation 13 device 79 character string 79 file 79 null 79 pipe 79 socket 79 standard 79 string 79 terminal 79 device driver 79 device h include file 228 dialog enter clause 19 extend database 19 difference list 104 differences C functions and Prolog 205 directive 117 begin module 132 discontiguous 136 dynamic 137 end_module 126 IF Prolog V5 3 299 User s Guide Index export 127 import 133 include 118 init 38 145 initialization 38 meta 128 138 module 126 multifile 136 op 139 private 136 reexport 130 selective import 134 disable input buffering 64 signal management 64 disjunction OR 28 disjunction of clauses 30 DISPLAY environment variable 75 distribution Prolog application 283 DNS 98 domain error error type 151 driver model 79 dsize 69 dused 69 dynamic declare predicate 62 directive 137 dynamic syn modifiable 143 EDITOR environm
288. te Min nl Min New is Min 1 count rec Min New Max for 3 Loops in which basically a series of integers is created and utilized can be conveniently created using the built in predicate for 3 see description in the Reference section on built in predicates in the IF Prolog Reference Manual 1 User s Guide 58 IF Prolog V5 3 Database and procedure execution Procedural predicates Example for 3 provides the easiest way to create the count predicate count for Min Max Min gt Max count for Min Max for Min Number Max write Number nl Number Max IF Prolog V5 3 59 User s Guide Procedural predicates Database and procedure execution User s Guide 60 IF Prolog V5 3 Chapter 4 The Prolog system This chapter describes the Prolog system and its environment Topics dealt with include the organization of the Prolog system calling and exiting Prolog and the different system modes It describes how you can query diverse information about the Prolog system and provides an overview of the interfaces of the Prolog system As IF Prolog is available on different platforms with different operating systems input key or path specifications may be different for your system System specific settings and special considerations are described in the release notice for your Prolog system 4 1 Organization of the Prolog system The root directory of the Prolog system is defined at
289. tem Command Execute an operating system command system Command ExitStatus system Command Input Output system Command Input Output Error Pid chdir Dir getewd Dir exec Command exec Prog QArgs unix fork Pid unix_getpid Pid unix_wait Pid unix_wait Pid ExitStatus Execute an operating system command and query the exit status Execute an operating system command with input and output streams specified Execute an operating system command with input output and error streams specified Change current directory Query current working directory Overlay the process and execute a command Call a program and overlay the current process Spawn process Query process ID of current process Wait for termination of a child process Wait for termination of a child process and query the exit status 5 4 2 Predicates for process communication with signals The Prolog system provides the following predicates for process communication with signals Predicate Purpose current_signal Signal Mode signal_control Signal Command signal_handler Goal Signal Handler unix_kill Pid Signal Query signals and modes Control signal processing Define local signal handling Send signal to a process 5 4 3 Predicates for process communication using pipes The Prolog system provides the following predicates for process communication using pipes Predicate Purpose
290. terface 5 3 2 Reading a file into the database You can read a file into the database either when Prolog is first called or during a Prolog session In each case a distinction is made according to whether the file contains Prolog source text or is a compiled file In the case of Prolog source text the procedure is called consulting with compiled files it is called loading Reading in a file when calling Prolog When you initiate Prolog there are various ways of reading a file into the database e The option st startfile allows you to read in a special start file st causes this operation to be suppressed If no st option is specified a default start file if present is read in see page 66 e You can also read a file into the database by specifying the option c and or 1 see chapter 4 1 1 File contents Option Prolog source text c filename Compiled Prolog code l filename Reading in a file during a Prolog session File contents Predicate Prolog source text consult Filename File1 File2 reconsult Filename Filel File2 Compiled Prolog code load Filename For a description of the various predicates refer to the IF Prolog Reference Manual 1 i The association of the predicates to the original file is not stored in the database i You can remove only a module as a complete entity from the database see chapter 7 5 3 Removing a module from the database Indivi
291. th 11 I void Cboot void 12 CPRIM len_pred 2 len call 13 i This achieves the following Preparation for integration into Prolog The main function has been removed and the include statement inserted The functor len_pred and the arity 2 are declared for the new predicate A link is established between Prolog and the function len by means of the function len_call line 6 This function handles the tasks arising from the succeeding steps Parameter passing between C and Prolog TermIsAtom checks whether the first argument is an atom if so it converts the atom into a C string This is passed as the first argument to the function len line 10 TermUnifyInteger converts the value of length in C representation into Prolog representation and unifies the result with the second argument of the predicate line 11 Returning TRUE or FALSE as result All goals are proved only if the three functions TermisAtom len and TermUnifyInteger O all yield the result TRUE non zero i e if the first argument when the predicate was called was really an atom the len call was successful and the length of the atom could be unified with the second argument of the predicate In all other cases the goal fails because at least one of the three functions yields the result 0 or FALSE Exception handling If the first argument of the goal is not an atom the function TermIsAtom automat ically sets th
292. the debugger executes all the commands which you have associated with this breakpoint If one of these commands causes the debugger to continue the execution it will be immediately executed The debugger thus leaves the breakpoint and any other commands present at this breakpoint will have no affect After execution of all commands the debugger switches to interactive mode if at least one of the breakpoints was interactive However the debugger switches only once to interactive mode regardless of the number of interactive breakpoints 9 3 4 Branching in the proof tree Return branches allow you to branch back in the proof tree within the debugger You can thus place the debugger in a state which has already been reached without the necessity for a breakpoint at the port to which the debugger is to branch IF Prolog V5 3 173 User s Guide Alpha user interface Debugging Cross branches are box related manipulative branches which place the execution in a state which it would not generally attain in the course of normal program execution You can use cross branches to simulate success failure or the occurrence of an error during the proof of a subgoal or a clause Command Short Meaning form back_clause be Branch back to clause head back parent bp Branch back to call port of calling goal back subgoal bs Branch back to call port of subgoal error err Generate exception exit x Force current goal to succeed fail f Force current goal to
293. the debugger reaches a breakpoint at an internal port If this is an explicit breakpoint the message BREAKPOINT will appear in the status indicator where stands for the number of the breakpoint You select the OR BOX with the command view_or_box or Field for settings This field displays the current settings for the debugger and the interface for example the current structure depth or how many clause or goal boxes are to be stored You select the field for settings with the command view_configuration or vco Field for breakpoints In this field the debugger displays the breakpoints which you have defined explicitly The breakpoints are displayed as follows Number i Predicate Clause Subgoal Port Condition Actions Number i Predicate Clause Variable Instantiation Condition Actions The asterisk at the beginning of the line identifies an active breakpoint i an interactive breakpoint The number allows you to address those breakpoints which you wish to activate deactivate or delete Each breakpoint has such a Number which is automatically assigned to it by the debugger You select the field for breakpoints with the command view_stops or vs Field for old goal and clause boxes You can view old goal and clause boxes in this field You can set the number of boxes to be stored with the command set history The message PREVIOUS BOX appears in the information area where specifies the chronological offset relative to the current
294. the module body has been defined If a module body is deleted the associated C predicates remain hidden until the body has been redefined 7 3 1 Exporting predicates The export directive is used in the module interface It has the argument PredicateList which corresponds to a list of predicate indicators The predicates in the list are made publicly visible thus they can be made visible in other modules by means of the import directive All predicates which you list in an export directive are implicitly declared as private This prevents the local definition of a predicate being overlaid by an import directive in the module body In this way you can redefine built in system predicates within a module If you have implemented predicates with the C interface in a module you can make them vis ible with the export directive But you must ensure that the predicate definitions themselves are not made available until after the module body has been defined i Predicates which you have implemented in C and have not exported are visible only in their original module Example module permutation export permute 2 module order export ordered 1 module sort export sort 2 begin_module sort IF Prolog V5 3 127 User s Guide Module interface import permutation import order sort List SortedList permute List SortedList ordered SortedList The sort module exports the so
295. the window you will find the typical OSF Motif buttons for the window menu minimize and maximize and also the name of the application running in this window i e Prolog Debugger Then you will see the menu bar with the entries Command Ports Breakpoints Trace Ancestors and History Beneath the menu bar is the information bar which displays the current subgoal being debugged and the breakpoint where current goal execution has halted The Constraints button allows you to have pending constraints if any displayed in a separate window The Prolog output field which displays the current section of the proof tree occupies the major part of the screen To the left of the output field is a row of buttons which execute the associated debugger command as soon as you click on a button The Prolog system displays any exceptions in the bottom field of Prolog Debugger 9 5 1 Controlling execution Both the command buttons to the left of the output field and also the menu bar contain debugger commands You will use the commands on the command buttons most frequently those in the Command menu less often In the Command menu you will find the entries break halt system call unify interrupt kill command and quit these activate the corresponding debugger commands User s Guide 184 IF Prolog V5 3 Debugging OSF Motif interface Menu entry Meaning break New input loop halt Terminate Prolog session system Start operating system sh
296. them into the Prolog system To do this you require the predefined C functions and C data structures which are contained in the include file device h in the directory PROROOT c 10 3 1 t device structure You define an input output device driver in a t_device structure which is defined in the file device h typedef struct STRING BOOLEAN BOOLEAN void int int BOOLEAN BOOLEAN dev_name xdev open STRING t_iomode t_iounit dev_close t_iounit BOOLEAN dev_stop void dev_read t_iounit void int dev_write t_iounit void int dev_flush t_iounit dev_is_unix_file t_iounit int const t_control dev_control const t_control dev_class_control t_device The components of the structure determine which facilities the driver is to have The indi vidual components of the t_device structure are described below STRING dev_name The first component of t_device is a pointer to a string giving the name of the device driver You can freely select the name of the driver but it must be unique i e you must not define any other device driver with the same name The following device drivers are already built in in the Prolog system file Device driver for regular files null Device driver for bit bucket pipe Device driver for operating system pipes standard Device driver for standard input output and error string Device driver for character strings socket D
297. this term e constants are unifiable if they are identical e structures are unifiable if they have the same functor and the same arity and if corre sponding arguments are also unifiable When a variable is instantiated to a term by unification the instantiation applies to the particular clause involved It is not possible to perform a new instantiation for this variable within the clause and any subsequent unification with a different term will fail Variable instantiation can only be undone in the course of backtracking The system does not check whether a variable to be replaced occurs a second time in the substitution term This occurs check as it is called is not performed because it takes a great deal of computer time Consequently when formulating your clauses you should ensure that loops of this sort e g X X do not occur Unification with an occurs check is implemented by the built in predicate unify with occurs check 2 Example The Prolog program below tests whether the two terms Term1 and Term2 are unifiable where applicable it also defines a list of necessary substitutions as the unifier unification Term1 Term2 Substitution Stack pair Term1 Term2 unify Stack Substitution unify unify pair T1 T2 R Unificator uni T1 72 R R1 Sub1 unify R1 Sub2 append Sub1 Sub2 Unificator uni T1 T2 R R atomic T1 atomic T2 T1 T2 uni T1 T2 R R O
298. tion PrologArg n gives you the n th argument of the newly defined predicate By using the functions of the function class TermUnify you can unify the results of the C function with the arguments of the predicate and thus pass them to Prolog To perform type checking and conversion to C variables you use the functions of the function class TermIs e Returning TRUE or FALSE as result You should ensure that your function yields a result using the return statement as follows Return value Interpretation in C Goal executing TRUE 1 Successful FALSE 0 Unsuccessful If the return value is FALSE the Prolog system automatically undoes any variable instantiations produced by TermUnify e Generating exceptions If you want to set an exception so that Prolog exception handling will be invoked in the event of fail you must use the C function ErrorContextSet However you should note that the functions of the function class TermIs set the error context implicitly e Generating an extended Prolog system In order to include your C function definitively in Prolog as a new predicate you need to generate an extended Prolog system see section 10 4 In our case the extended Prolog system has been given the name myprolog but you are free to choose any other name e Debugging in Prolog To call the extended system you have just generated you enter the command myprolog As soon as your new Prolog system displa
299. tion fields 176 message lines 176 operating commands 177 output window 176 status indicator 175 trace field 180 analyze term 199 anonymous variable 13 ARITY data type 198 arity 7 15 30 ASCII language specific charset 62 ask questions 14 atom 12 attributed grammar 110 backtracking 15 46 223 begin module directive 132 box model 38 205 box port activate default 162 branch commands debugger 173 branches debugger 174 breakpoint 170 buffering input stream Index disable 64 built in modules 115 predicates 87 visibility 120 built in predicates 32 C functions built in 197 classes 196 complex 196 205 link 238 simple 196 201 C interface 32 active 195 196 205 complex functions 205 simple functions 201 device drivers 228 link with Prolog 238 passive 195 218 data types 219 call define block size 63 define character set 62 define memory areas 63 dynamic predicates 62 graphical development environment 65 without input buffering 64 without messages 64 without signal management 64 call parameter 62 c 65 debug 62 1 65 language 62 mi 63 ms 63 msc 63 msg 63 msl 63 mst 64 msx 64 nobuf 64 nonotify 64 nosignal 64 notty 64 nowarnings 64 program 64 prompt 64 r 66 root 65 sp 65 st 66 stream 65 xenv 65 call Prolog 5 62 call types of predicates 32 call with input output redirection
300. tion in the Console window and copies it into the Clipboard Note Selection is cut only if it is a part of an editable section in the console window e g command line Selection is always copied into the Clipboard e Copy Copies the current selection in the Console window into the Clipboard e Paste Pastes the Clipboard text only to the Console window Note Selection is pasted only if it is a part of an editable section in the console window e g command line 11 23 Active File Menu e Set Active file Opens a file dialog When the file is selected it is set as the IF Prolog active file e Edit Opens the IF Prolog active file in the default editor e Consult Consults the IF Prolog active file into the Prolog database e Reconsult Re consults the IF Prolog active file into the Prolog database e Compile Compiles the IF Prolog active file into the filename with the extension CMP e Cross Reference Starts Cross Reference tool on the IF Prolog active file 11 24 View Menu e Main Toolbar Activates or deactivates IF Prolog main toolbar Debug Main Toolbar Activates or deactivates IF Prolog main debugger toolbar e Debug Information Toolbar Activates or deactivates IF Prolog second debugger toolbar Status Bar Activates or deactivates IF Prolog status bar IF Prolog Database Browser Activates or deactivates IF Prolog database browser User s Guide 246 IF Prolog V5 3 Graphical User Interface MS Wind
301. tion of the Prolog system and the Prolog language The description covers the concepts of the input output interface and a driver model along with the organization of the predicates in modules and their contexts The interactive debugger which you can operate with three different user interfaces is based on the concept of the extended box model which is presented in chapter 3 5 The last chapter explains both how to link C functions in Prolog and how to call Prolog in a C program and also how to implement device drivers in C The following notational conventions are used throughout this manual XXX Syntax definitions are enclosed within a frame bind Prolog language elements operating system commands and outputs from the system are printed in teletype font Name Italics are used to represent variable parts in inputs and outputs where you should substitute them with your own values Square brackets denote optional entries in the syntax notation the brackets are not part of the Prolog text Square brackets in bold type are elements of the Prolog list notation and are part of the Prolog text Braces denote alternatives in the syntax notation the brackets are not part of the Prolog text A bar denotes alternatives in the syntax notation Parentheses are required parts of the Prolog predicate notation and is part of the Prolog text Ellipsis indicate that the preceding syntax element may be re peated ato
302. tly intend to initiate the goal on more than one occasion User s Guide 220 IF Prolog V5 3 Passive C interface Framework Function Purpose QueryCreate Create passive Prolog goal QueryDispose Release passive Prolog goal QueryOpen Activate passive Prolog goal Active Prolog goals An active Prolog goal is ready for execution A current goal is also an an active goal but simply created most recently in the time frame Only active goals can be proved using the function PrologFetch You must first make a passive Prolog goal into an active and current goal by means of the function QueryOpen Function Purpose PrologClose Deactivate current Prolog goal PrologError Query error on last call of PrologFetch PrologGoal Determine term representation of current Prolog goal PrologOpen Create active Prolog goal Variable instantiations You can use the following functions to determine the variable instantiation in the current Prolog goal Function Purpose ProloglsInteger Check for integer ProloglsFloat Check for floating point number ProloglsString Check for string ProloglsTerm Check for term Functions for formatted output Function Purpose fprintt Formatted output of printt Prolog terms and sprintt C data objects outputlen Count the characters for formatted output 10 2 2 Working with the passive C interface This section starts with a simple framework for the us
303. to Prolog You thus have the following functions at your disposal with which you can link C functions in Prolog and Prolog calls in C programs C function Purpose CPRIM Add simple C function in Prolog CPRED Add complex C function in Prolog MCPRIM Assign simple C function to a Prolog module MCPRED Assign complex C function to a Prolog module DeviceCreate Link C device driver into Prolog i You may define only one Cboot function User s Guide 238 IF Prolog V5 3 C interfaces Generating an extended Prolog system e You should then copy the model makefile model make from the directory PRO ROOT c and modify it to suit your application With this makefile you can create a new Prolog application The libraries and objects which you select and the order in which you specify them are significant and can vary according to the system Example In the following you will see the contents of the makefile model_make for a UNIX system The contents of the makefile may differ for different machines This Makefile is a model for making your own application kernel using the C Language Interface of PROLOG and possibly using Motif or database interfaces or Constraint Package Change the definitions if appropriate COMMANDS SE HH H H H The PROROOT is the directory where your PROLOG is installed PROROOT DEFAULTROOT These library definitions are always necessary PROLIB_ASCII PROROOT c libApro
304. to declare C variables which can accommodate Prolog terms and are referred to as term variables in the following Term variables are objects which are administered by the C interface They should never be manipulated directly IF Prolog V5 3 197 User s Guide Overview Active C interface Prolog objects Representation in C Data type name Term Implicit type conversion TERM The following facilities of C are used for mapping Prolog terms onto C objects C objects Representation in C Data type name Integer long long Floating point number double double String C character string i e an array of STRING characters terminated by a null byte NO The data type names ARITY for the arity of structures and STRING for strings or names are also declared in the include file cpred h The use of these names is recommended in order to avoid inconsistent data types If you use data types other than signed long for integers and double for floating point numbers in a C function you must insert appropriate cast operators before a type conversion to a Prolog term is performed You should use only the built in C functions of the active C interface for the conversion as inconsistencies in the internal representation may otherwise result Memory for term variables The C interface manages all the terms which you can access using term variables You must declare the term variables before calling built in functions which write terms t
305. tor X hans user 2 1 call ancestor X hans 3 0 enterbody ancestor X hans 3 1 call father X hans 4 0 enterbody father gregor hans 4 1 call true 4 1 exit true 4 0 exitbody father gregor hans 3 1 exit father gregor hans 3 0 exitbody ancestor gregor hans 2 1 exit ancestor gregor hans 1 1 exit call ancestor gregor hans user X gregor 1 1 redo call ancestor gregor hans user 2 1 redo ancestor gregor hans 3 1 redo father gregor hans User s Guide 166 IF Prolog V5 3 Debugging Interactive debugger 3 1 fail father X hans 3 0 enterbody ancestor X hans 3 1 call mother X hans 3 1 fail mother X hans 3 0 enterbody ancestor X hans 3 1 call father X Z 4 0 enterbody father max gregor 4 1 call true 4 1 exit true 4 0 exitbody father max gregor 3 1 exit father max gregor 3 2 call ancestor gregor hans 4 0 enterbody ancestor gregor hans 4 1 call father gregor hans 5 0 enterbody father gregor hans 5 1 call true 5 1 exit true 5 0 exitbody father gregor hans 4 1 exit father gregor hans 4 0 exitbody ancestor gregor hans 3 2 exit ancestor gregor hans 3 0 exitbody ancestor max hans 2 1 exit ancestor max hans list exit call ancestor max hans user X max yes 9 2 4 Stop trace If you call trace 1 when the debugger is not active tracing will terminate of its own accord after the specified goal has been proved The user defined predicate trace_end 0 is cal
306. tracing and interactive debugging e Predicates with debug mode turned on can be monitored i e the sequence of execution within the predicate box clause unifications and subgoals can be debugged e Predicates with debug mode turned off cannot be monitored i e execution sequences within the predicate box cannot be debugged You should also note the following e C predicates and compiled predicates of a module cannot be monitored e Non compiled user defined predicates can be monitored by default With this type of predicate you can use debug_mode 3 to switch off debug mode to switch it back on again and to query it user debug_mode Predicate Old Mode off user debug_mode Predicate Old Mode on user debug_mode Predicate Mode Mode i You should note that the Prolog system regards predicates as dynamic or modifiable only if either you have called Prolog with the debug pa rameter or have declared the predicates as dynamic with the dynamic directive 9 1 3 Further parameters for the debugger The predicates debug_config 3 and trace_config 3 allow you not only to define break points for the debugger and tracer but also to control the output IF Prolog V5 3 163 User s Guide Tracing Debugging Parameter Value Meaning display_depth history length trace length display 100 automatic alpha stream
307. tted Format TermList Write terms with formatting write_term Term Options Write a term using operator notation writeq Term Write a term in single quotes using oper ator notation 5 2 2 Explicitly specifying streams for input output Input and output can be performed also from and to an explicitly specified stream It is not necessary to redefine current streams only for a single input or output operation Instead a predicate with an extra argument for the stream can be used If you wish to execute the input output from or to a stream you must first open this stream prior to the first read or write operation If the stream is to be a pipe you should first create it with the predicate unix_make_pipe Pipename Pipename is generated by the Prolog system when the pipe is created Open for file by calling pipe by calling Reading open file Filename read X open pipe Pipename read Y Writing open file Filename write X open pipe Pipename write Y Appending open file Filename append X IF Prolog V5 3 83 User s Guide Elementary input output Input output interface error tell 1 simultaneously i You can also open a stream usually a file by calling see 1 tel1 1 or i File names which contain special or Prolog specific characters or which begin with an uppercase letter must be enclosed in single quotes i You can open a stream either for reading or for writi
308. u can activate it as often as required with the function QueryOpen e Terminating Prolog When you no longer need the passive C interface you should release the memory space reserved for the Prolog system by calling EndProlog e Compiling and linking In order to compile and link the program you should copy the makefile model_make from the directory PROROOT c into your working directory Then proceed as de scribed in section 10 4 Set the variable MAIN to the absolute pathname of your ex tended file main c and choose a new name for the extended Prolog system After you have copied the makefile you can call the program from the shell level using the name defined in the makefile If you have set the necessary options see section on initializing the Prolog system above you can also specify call parameters for the Prolog system User s Guide 224 IF Prolog V5 3 Passive C interface Application Example In the first example the Prolog database is to be used as a data base The file supplier pro contains the following dispatch list as a Prolog predicate banana 500 7 50 french fries 40 1 00 soft drink 20 2 00 supplier grocer supplier grocer apple 600 6 50 supplier grocer orange 20 5 60 supplier grocer cabbage 100 3 45 supplier grocer carrot 100 4 50 supplier bakery bread 3 7 50 supplier bakery cake 0 38 00 supplier bakery cookies 600 1 25 supplier
309. uc The EUC character set is supported e sjis The SJIS character set is supported e chinese The multi octet set of Chinese characters is supported e korean The multi octet set of Korean characters is supported e taiwanese The multi octet set of Taiwanese characters is supported Sets the minimum size of the dynamic memory blocks requested by the Prolog system number is an integer specifying the size in bytes of the memory made available For each 1024 byte unit you may also use the abbreviation 1k or 1K e g 10k instead of the value 10240 Default 8K number bytes are made available as the initial size of the total memory for the Prolog system Prolog then partitions the individual memory areas itself number is an integer specifying the size in bytes of the memory made available For each 1024 byte unit you may also use the abbreviation 1k or 1K e g 10k instead of the value 10240 Default 8K number bytes are made available as the initial value of the constraint data stack number is an integer specifying the size in bytes of the stack made avail able For each 1024 byte unit you may also use the abbreviation 1k or 1K e g 10k instead of the value 10240 If required for program execution the memory area is automatically in creased Default 8K number bytes are made available as the initial value of the global data stack number is an integer specifying the size in bytes of the stack made avail able For each 1024
310. ude directive 118 include file cpred h 198 cpro h 222 device h 228 information derive 13 information fields alphanumeric user interface 176 Informix import 78 init directive 38 execute 145 initialization directive 38 initiate context jump 148 149 initiate backtracking 15 input stream buffering disabled 64 input output default stream 81 generic 79 interface 79 redirection 64 stream 79 80 top level dialog 79 instantiation 35 variable 15 instantiation error error type 151 integer largest representable 70 smallest representable 70 integers value range 69 interactive debugger 167 interface input output 79 module 126 interfaces of the Prolog system 76 interpretable 144 interprocess communication pipe 92 signal 92 socket 95 interrupt signal 155 invocation automatic of an application 64 ISO standard 62 kanji language specific charset 62 keyboard 79 alias name 81 Korean language specific charset 62 language specific character set 62 language constructs 124 language interface active 195 applications 195 passive 195 link C functions 238 list process in C 200 load compiled Prolog file 144 exceptions 146 file 90 module 144 suppress messages 64 without input buffering 64 without signal management 64 load operation 146 internal 145 local memory area 73 User s Guide 302 IF Prolog V5 3 Index lo
311. ugger commands ancestor max pepsi 3 father max gregor REDO gt ancestor gregor pepsi Configuring the debugger log In the Command menu of the History window the menu item configure lets you configure the debugger The number you specify determines how many sections of the proof tree are stored The buttons lt lt lt gt gt gt allow you to display the first the previous the next or the last section of the proof tree Index indicates which section in the series of stored section is currently being displayed 9 6 Summary tables of debugger commands The debugger commands ordered by function are summarized in the following tables The commands are described individually in the F Prolog Reference Manual 1 IF Prolog V5 3 191 User s Guide Debugger commands Debugging Requesting information Command Short Meaning form help h Display help information trace t Log passage through ports view_ancestors a Display dynamic call chain with all ancestors view_stops vs Display list of all explicit breakpoints view_variable vv Display all constraints for a variable view_constraints vc Display all constraints view_configuration vco Display all debugger settings view_predicate v Display predicate view module vm Display all predicates for a module view and box amp Display goal box in output window view_or_box Display clause box in output window view_previous_
312. use the predefined designators in a consistent manner Example If you attempt to open a non existent file with the name hello for reading user open hello read Stream the Prolog system raises the following exception EX CEP TION existence_error source_sink hello gt gt gt error existence gt gt gt goal system open hello read 225 no When an exception is raised a search is carried out from inside to out for an active context whose ContextInfo list contains an entry in the form error Error Info gt Handler where Error and Info can be unified with the corresponding information of the initiated exception If such a context is found the goal which initiated the exception is replaced by Handler Goal execution is then resumed as normal If no matching context is found the goal initiating the exception is replaced with the goal throw error Error Info and executed in other words a context jump is performed You can intercept this with catch 3 or a corresponding context 2 call i The Prolog input loop break 0 defines a context which catches all exceptions and generates appropriate error outputs from the information supplied Example The following Prolog text implements a simple read evaluate print loop similar to break 0 mybreak repeat catch read Goal eval Goal error E A exception_message E A User s Guide 152 IF Prolog V5 3 Contexts
313. uted as in standard exception handling 8 3 Signals Signals are asynchronous events arriving from the outside world One of the ways in which signals can be dealt with is active handling in the application If there is provision for signal handling by the application and if information is provided for in one of the active contexts on how to handle the signal handling is performed without destroying the current proof tree If signal handling is successful the goal interrupted by the signal is resumed as if the signal had not arrived Otherwise or if no context is found which can handle the signal the exception error system_error error interrupt signal Signall is generated and a context jump is executed In IF Prolog the way in which the system reacts to the signals it receives can be controlled selectively The following signals can be handled in some operating systems some signals are not supported User s Guide 154 IF Prolog V5 3 Contexts Signals Prolog signal C signal name see 10 abort SIGABRT alarm SIGALRM interrupt SIGINT pipe SIGPIPE quit SIGQUIT termination SIGTERM user 1 SIGUSR1 user_2 SIGUSR2 For each of these signals it is possible to define the general handling mode The following handling modes are provided e Default handling is the standard handling provided by the operating system for the signal program abort in most cases e Ignore The operating system is instructed to igno
314. utput handle_server Query Output exception_handler findall Query Query Answer Exception Answer Exception writeq Output Answer write Qutput flush output Output The following Prolog text implements a client which connects to the above server reads queries from the user and sends them to the server IF Prolog V5 3 99 User s Guide Process management Input output interface client Host Port Port number written by server Server outputs accepted Internet Number Port Client now waits for queries sends them to the server which answers etc End with halt client Host Port open_client Host Port Socket work_client Socket open_client Host Port Socket integer Port socket internet stream Socket socket connect Socket Host Port work_client Socket open Socket read Input open Socket write Output repeat write Client flush_output read Query not handle_client Query Input Output close Input close Output socket_shutdown Socket handle client end of file _ _ fail handle_client halt _Input Output write Output halt write Qutput flush_output Output fail handle_client Query Input Output writeq Output Query write Qutput flush_output Output read Input Answer write Server answered write Answer nl ang Answer end_o
315. ve file e View Menu View commands enable disable toolbars database browser or status bar e Debug Menu Debugger commands step skip continue e Options Menu Options commands general options prolog flags e Help Menu Help commands GUI reference Prolog reference 11 2 1 File Menu e Edit new Opens a file named untitled pro in the IF Prolog default editor e Edit Opens a file dialog When the file is selected it is open in the IF Prolog default editor e Consult Opens a file dialog When the file is selected it is consulted into the Prolog database e Reconsult Opens a file dialog When the file is selected it is re consulted into the Prolog database e Load Opens a file dialog When the file is selected it is loaded into the Prolog database e Compile Opens a file dialog When the file is selected it is compiled to the filename with extension CMP e Cross Reference Opens a file dialog When the file is selected Cross Reference tool is started on this file The Cross Reference tool output is displayed in the console window e Working directory Opens a directory dialog Sets the current working directory e Set Active file Opens a file dialog When the file is selected it is set as the IF Prolog active file see Active File Menu e Exit Exits IF Prolog application IF Prolog V5 3 245 User s Guide MS Windows Graphical User Interface 11 2 2 Edit Menu e Cut Cuts the current selec
316. ve programmed yourself When the predicates are called their functions are mapped to the functions in dev_class_control where the component stream is always initialized with the NULL pointer You can use the same control function for handling both class specific and stream specific functions Through the component stream the function can check the purpose IF Prolog V5 3 231 User s Guide C device drivers C interface for which it has been activated 10 3 2 Control functions A device driver provides a set of commands for performing special operations on input output streams If you define the required control commands the built in predicates which use the control interface of device drivers can also support the input output streams for a device driver which you have programmed yourself The built in device drivers implement the following control commands The following control commands are implemented in the built in device drivers of the Prolog system Device Command Meaning file stream_type Arg see stream_type 2 stream_size N Unifies N with the size of the file isatty stream_control 2 succeeds if Stream is a terminal fileno N Unifies N with the operating system dependent file number of the file clear_eos see clear_eos 0 1 clear_eol see clear_eo1 0 1 clear_screen see clear screen 0 1 tty size Z S see tty size 2 3 move_cursor Z S see move cursor 2 3 seek P see set_stream_position 2 tell P see stream property 2
317. w appears on the screen It contains e Menu Bar e Toolbars e Prolog Console with Scrollbars e Status Bar Other topics on User Interface features and Application Development includes e Database Browser e Options Dialog e Debugger Support e Linking Custom IF Prolog Applications with GUI 243 MS Windows Graphical User Interface This is the IF Prolog Main Window fs IF Prolog Y5 2 CAprologiDemosi5StandardiHanoi pro 141 142 shift_left 5 C prologi Demos Standard Hanoi pro user 134 135 shift_stone 4 prolog Demos Standard Hanoi pro user 118 126 132 prolog Demos Standard Hanoi pro user 162 169 prolog Demos Standard Hanoi pro user 57 threetimes 0 prologi Demos Standard Hanoi pro user 154 up_stone 4 prologi Demos Standard Hanoi pro user 110 111 yes user Figure IF Prolog Main Window 11 2 IF Prolog Main Window Menu Bar This is the IF Prolog Main Window Menu Bar E IF Prolog Y5 2 CAprologiDemosiStandardiHanoi pro Dl SRA ma al E e Figure IF Prolog Main Window Menu Bar User s Guide 244 IF Prolog V5 3 Graphical User Interface MS Windows The Main window menu bar contains several menus e File Menu File commands open consult reconsult load e Edit Menu Clipboard commands cut copy amp paste e Active File Menu File commands open consult reconsult load but only for IF Prolog acti
318. yes Constraints package IF Prolog system features a constraints package which allows you to organize goal execution more efficiently The package offers several different constraints classes which are available as modules For information on how to work with constraints and use the built in predicates refer to 5 User s Guide 78 IF Prolog V5 3 Chapter 5 The input output interface Input and output in IF Prolog take place using device drivers which are managed on the basis of a driver model A device driver is responsible for operations for a particular stream type e g files character strings or pipes Prolog can manage different streams with the same set of generic built in predicates Prolog provides the following built in devices e file is the device for operating system files e null is the bit bucket device e pipe is the device for operating system pipes e standard is the device for the standard streams of the Prolog system e string is the device for input output using character strings e socket is the device for interprocess communication In this context Stream is the unique designation of an opened input or output channel e g to a file terminal or pipe The designation is an opaque term created by the Prolog system However you may also use alias names which you assign to a stream This chapter describes both input output in the top level dialog and also elementary in put output in Prolog and the
319. ys the prompt you can test your new predi cate Example In this example we want to program a C function which will accept a character string as a parameter and yield the length of the character string as its result The function is then to be incorporated in Prolog as a new predicate User s Guide 202 IF Prolog V5 3 Active C interface Simple C functions e Programming the function The core component here is the function len int len const char text long length length strlen text return 1 e Testing the function The following main function allows you to test the function len include lt stdio h gt include lt string h gt int main int argc char argv long length if argc 0 fprintf stderr At least one argument expected n return 1 len argv 1 amp length printf Length ld n length return 0 The completed source file has the following contents include lt stdio h gt 1 x include lt string h gt include cpred h 2 static int len const char text long length 3 length strlen text 4 return 1 5 static BOOLEAN len_call void 6 STRING text 7 long length 8 x return TermIsAtom PrologArg 1 amp text 9 IF Prolog V5 3 203 User s Guide Complex C functions Active C interface amp amp len text amp length 10 amp amp TermUnifyInteger PrologArg 2 leng
320. zes all environment variables used by the Prolog system User s Guide 74 IF Prolog V5 3 Prolog system Environment variables Environment variable Meaning DISPLAY OSF Motif server name EDITOR Editor started with edit 0 1 HOME Directory of user login LANG Set language environment PROHELP Path for help files PROPATH Path for directories in which files to be opened should be looked for PROROOT Path for Prolog system files PRORC Start file for the Prolog call SHELL Command interpreter started with system 0 TERM Name of terminal TMPDIR Directory for temporary files Set environment variables You set environment variables in the shell Note that you must specify environment variables in uppercase letters Example Bourne shell EDITOR emacs export EDITOR C shell setenv EDITOR emacs If you do not wish to make these inputs again after every login then you should store them in the start file profile Bourne shell or login C shell You use unset or unsetenv to reset the variables Environment variable DISPLAY determines the OSF Motif server A display is defined using the name of the host to which the display is connected the display number and the screen number in the following format DISPLAY hostname displaynumber screennumber IF Prolog V5 3 75 User s Guide Interfaces Prolog system If the server is located on a different host than

Download Pdf Manuals

image

Related Search

Related Contents

instrucciones para el uso  EZPhone Cam USB User Manual    instrucciones de montaje gran diámetro y relining esp.  

Copyright © All rights reserved.
Failed to retrieve file