Home
Flora-2: User`s Manual
Contents
1. character list 13 _iri 98 class 10 _list 111 _ boolean 107 _long 108 _date 102 string 109 dateTime 101 symbol 97 decimal 109 _time 104 double 107 debugger 140 duration 105 debugging 112 integer 109 delete iri 99 bulk 76 _list 111 deleterule 86 _long 108 deleterule_a 86 string 109 deleterule_z 86 symbol 97 derived part of predicate 73 _time 104 directive see compiler directive expression 18 do until 92 instance 10 don t care variable 5 subclass 10 dynamic module 62 clause 87 dynamic rule 84 closure axioms 148 code inheritance 58 encapsulation 38 comment 14 equality 61 compiler directive 123 equality maintenance level 61 index 124 escaped character 12 ope Hi Laa F molecule 11 setsemantics 124 falsele E2 compile time 123 FLIP 4 equality 61 executable 123 constant symbol 9 floating number 13 FLORA_ABORT 95 flora_compiler_options directive 134
2. Constraint solving 23 FLORA UNDEFINED EXCEPTION 95 cut FLORID 1 across tables 72 and local scheduling 72 generated oid 24 in FLORA 2 72 HiLog 42 data atom 10 translation 42 datatype unification 43 _boolean 107 HiLog to Prolog conversion 49 _date 102 _dateTime 101 1 0 decimal 09 port based 125 _ double 107 stream based 125 duration 105 Tambar 9 integer 109 if then else 91 ignore_depchk 82 120 INDEX inheritance behavioral 53 non monotonic 54 of code 58 of value 58 structural 53 inline program 30 insert bulk 74 insertrule 85 insertrule_a 85 insertrule_z 85 integer 13 loading files 29 local scheduling in XSB 3 83 84 137 local_override optimizer option 123 logical expressions 16 loop until 92 meta decomposition operator 47 meta programming 44 meta unification operator 44 method 10 boolean 23 inheritable 23 procedural 70 self 20 value returning 10 Method class in module _syste
3. 30 1 Facts are Unordered o o oaa a 131 30 2 Testing for Class Membership oaa a 000 eee ee eee 131 30 3 Complex F molecules in the Rule Heads 00a aa 0 200 000 004 132 31 Miscellaneous Features 134 31 1 Suppression of Banners aoao a 134 31 2 Passing Compiler Options to XSB aoaaa aaa es 134 32 Bugs in Prolog and FLORA 2 How to Report 134 Appendices 138 A A BNF style Grammar for FLORA 2 138 B The FLORA 2 Debugger 140 C Emacs Support 143 G1 Tnst ll tioni s ranen a e E OS a ee OS 4 End e apie ui a r a ee eS 143 C2 Functionality es s a aoe et ck A eR a RA A re Soe we Be eee ba Ste 8 144 D Inside FLORA 2 146 D 1 How FLORA 2 Works s pe aeoe k a aie ee 146 D 2 System Architecture oaoa a a a a a 152 1 INTRODUCTION 1 1 Introduction FLORA 2 is a sophisticated object oriented knowledge base language and application development platform It is implemented as a set of run time libraries and a compiler that translates a unified language of F logic 8 HiLog 4 and Transaction Logic 2 1 into tabled Prolog code Applications of FLORA 2 include intelligent agents Semantic Web ontology management in tegration of information and others The programming language supported by FLORA 2 is a dialect of F logic with numerous ex tensions which include a natural way to do meta programming in the style o
4. 116 export directive flora_query 4 predicate index compiler directive op compiler directive 38 35 124 124 setsemantics compiler directive abolish_all_tables aggregates avg 89 collectbag 89 collectset 89 count 89 max 89 min 89 set valued methods 90 sum 89 aggregation aggregate operator 89 grouping 89 anonymous oid 23 anonymous variable 5 arithmetic expression 16 atom data isa 10 10 Signature 69 81 atomic formula in F logic attribute inheritable non inheritable 53 53 backtracking over updates in XSB base part of predicate batched scheduling in XSB boolean method inheritable bulk delete 76 bulk insert 74 canonic term 73 124 83 84 23 152 cardinality constraint 94 114 115 catch INDEX 157
5. 11 8 3 Passing Arbitrary Queries to FLORA 2 The method of calling FLORA 2 from Prolog which we just described assumes that the user knows which predicates and methods to call in the FLORA 2 module Sometimes it is useful to be able to pass arbitrary queries to FLORA 2 This is particularly useful when FLORA 2 runs under the control of a Java or C program To enable such unrestricted queries FLORA 2 provides a special predicate flora_query 4 which is called from Prolog and takes the following arguments e String A string that contains a FLORA 2 query It can be an atom e g foo bar j X or a list of character codes e g foo bar j X e Vars A list of the form Name1 Vari Name2 Var2 or ofthe form Name1 Vari Name2 Var2 Name is a name of a variable mentioned in String for instance X note the name must be quoted since it is an atom Var is a Prolog not FLORA 2 vari able where you want the binding for the variable Name in String to be returned For instance if String is p X Y then Vars can be X Xyz Y Qpr In this case Xyz will be bound to the value of X in p X Y after the execution and Qpr will be bound to the value of Y in p X 7Y e Status Indicates the status of compilation of the command in String It is a list which contains various indicators The most important ones are success and failure e Exception If the execution of t
6. head John age gt 31 children gt Bob Mary children gt John 66 99 Note that conjunction binds stronger than disjunction so the parentheses in the above example are essential e Programs and queries A program is a set of rules A query is a rule without the head In FLORA 2 such headless rules use instead of e g John age gt X The symbol in headless FLORA 2 expressions is used for various directives which are plenty and will be introduced in due course 5 BASIC FLORA 2 SYNTAX 12 Example 5 1 Publications Database Figure l depicts a fragment of a FLORA 2 program that represents a database of scientific publications Schema conf_p paper journal_p paper paper authors gt person title gt string journal_p in_vol gt volume conf_p at_conf gt conf_proc journal_vollof gt journal volume gt integer number gt integer year gt integer journal name gt string publisher gt string editors gt person conf_proc of_conf gt conf_series year gt integer editors gt person conf_series name gt string publisher name gt string person name gt string affil integer gt institution institution name gt string address gt string Objects oj journal_p title gt Records Relations Sets Entities and Things authors gt Omes in_vol gt o 11 Oqi conf p title gt DIAM II and L
7. will produce the same result where a b c and done alternate in the output 17 5 Cuts No discussion of a logic programming language is complete without a few words about the infamous Prolog cut Although Prolog cut has been mostly rightfully excommunicated as far as Database Query Languages are concerned it is sometimes indispensable when doing real work like pretty printing FLORA 2 programs or implementing a pattern matching algorithm To facilitate this kind of tasks FLORA 2 lets the programmer use cuts However the current implementation of XSB has a limitation that Prolog cuts cannot cut across tabled predicates If you get an error message telling something about cutting across the tables you know that you have cut too much The basic rule that can keep you out of trouble is do not put a cut in the body of a rule after any F molecule or tabled predicate However it is OK to put a cut before any F molecule It is even OK to have a cut in the body of a rule that defines an F molecule again provided that the body has no F molecule to the left of that cut If you need to use cuts plan on using procedural methods or non tabled predicates Also when XSB is configured for local scheduling cuts across tables are much less likely because under this strategy XSB tries to compute the entire clique of interrelated predicates before it proceeds to the next body literal which could be the dreadful cut Thus something
8. X attri gt Y attr2 gt 7Z f P attr3 gt Q is translated as X attri gt Y Y attr2 gt 7Z P PLattr3 gt 7Q i e the nested literals follow their hosts in the translation Thus writing terms in this way is con sidered a hint to the compiler which indicates that bindings are propagated from X attr1 gt Y to YLattr2 gt Z etc If on the other hand Y attr2 gt Z has only one solution then per haps writing Y attr2 gt Z X attr1 gt Y might produce a more efficient code The same considerations apply to f P attr3 gt 7Q Similarly to nested molecules the FLORA 2 compiler assumes that path expressions represent a hint that bindings are propagated left to right In other words in X Y Z X will be bound 27 OPTIMIZATIONS 122 first Based on this the oids of the objects X Y are computed and then the attribute Z is applied In other words the translation will be X Y gt Newvari Newvar1i Z gt Newvar2 Unfortunately unlike in databases statistical information is not available to the FLORA 2 compiler and only a few heuristics such as variable binding analysis which the compiler does not perform can be used to optimize such queries If the order chosen by the compiler is not right the programmer can always unnest the literals and place them in the right order in the rule body Open calls vs bound calls In Prolog it is much more efficient space and time wise to m
9. Xn there are the following basic types of formulas 1 O M gt V O M gt V 5 BASIC FLORA 2 SYNTAX 10 2 O M gt Vi Vn O M gt Vi Vn 3 C M gt T C M gt T In all of the above cases 0 C M Viz and T are HiLog terms i e expressions of the form a X X s Y X Y X g k etc where X and Y are variables and lowercase letters f s etc are constants Expressions 1 and 2 above are data atoms for value returning methods They specify that a method expression M applied to an object O returns the result object V in case 1 or a set of objects Vi Va in case 2 In all cases methods are assumed to be set valued However later we will see that cardinality constraints can be imposed on methods so it would be possible to state that a particular method is functional or has some other cardinality property The formula 2 says that the result consists of several objects which includes V1 Vo Vn Note that we emphasized includes to make it plain that other facts and rules in the knowledge base can specify additional objects that must be included among the method result When n 1 in 2 the curly braces can be omitted For instance O M gt V4 In fact the single expression 2 is equivalent to a the following set of expressions where the result set is split into singletons O M gt Vi O M gt V3 oM gt Val When M is a constant e g abc then
10. add hook find file hooks turn on font lock in emacs or xemacs whichever is used by your XEmacs C 2 Functionality Menubar menu Once FLORA 2 editing mode is installed it provides a number of functions First whenever you edit a FLORA 2 program you will see the Flora menu in the menubar This menu provides commands for controlling the Flora process i e the FLORA 2 shell You can start and stop this process type queries to it and you can tell it to consult regions of the buffer you are editing the entire buffer or some other file Because Emacs provides automatic file completion and allows you to edit what you typed per forming these functions right out of the buffer takes much less effort than typing the corresponding commands to the FLORA 2 shell Keyboard functions In addition to the menu flora mode lets you execute most of the menu commands using the keyboard Once you get the hang of it keyboard commands are much faster to invoke Load file Ctl c Ctl f Load file dynamically Ctl u Ctl c Ctl f Load buffer Ctl c Ctl b Load buffer dynamically Ctl u Ctl c Ctl b Load region Ctl c Ctl r Load region dynamically Ctl u Ctl c Ctl r When you invoke any of the above commands a FLORA 2 process is started unless it is already running However if you want to invoke this process explicitly type ESC x run flora You can control the FLORA 2 process using the following commands Interrupt Flora Pro
11. alb gt c Yes The reason for this behavior is as before that the stale positive answer to the query a b gt c has been recorded in Prolog tables In order to invalidate this answer it would be necessary to keep track of the dependencies among different facts which Prolog currently does not do and it would be very hard and inefficient to keep this information at the FLORA 2 level Nevertheless FLORA 2 provides partial solution to this problem in the form of the refresh operator which lets the programmer to explicitly remove stale answers from tables For instance in the above case we could do the following flora2 refresh a b gt c alb gt c No In general refresh can take a comma separated list of facts to be purged from the tables and the facts can even contain unbound variables In the latter case any stale call that unifies with the given facts will be refreshed For instance flora2 refresh alb gt X c Y p z V foo Yes will refresh the tables for a b gt X and c Y in module main and for p z V in module foo Sometimes it is desirable to completely get rid of all the information stored in tables for instance when it is hard to track down all the facts that might depend on the changed base facts In such a case the command flora2 abolish_all_tables can be used However this command is unsafe If it is executed during the computation of a subquery that involves an F logic mole
12. 2001 11 23 _date _toString gt 2001 11 23 00 00 _basetype e 2001 11 23 02 30 _date _add P2Y2M10D _duration gt 1979 09 13 02 30 _dt _basetype Note that when adding duration to a date the time part of the duration constant must be empty 25 5 The Primitive Type time This primitive type corresponds to the XML Schema time data type Constants of this type have the form HH MM SS sZHH MM _time The symbols and are part of the syntax The part s is optional It represents fractions of a second Here s can be any positive integer The sign Z represents the sign of the timezone or The following HH represents time zone hours and MM time zone minutes The time zone part is optional The name of this type has the following alternative versions _t and http www w3 org 2001 XMLSchema time All constants of that type are also assumed to be members of the built in class _time The following methods are available for the class _time and are provided by the module _basetype Their signatures are given below Class methods e _time _toType _integer _integer decimal _integer integer integer gt _time The arguments represent hour minute second time zone sign time zone hour and time zone minute e time _toType _symbol gt time e time gt _isType0f _object Tells if object belongs to the primitive type time Component methods e time hour gt _integer e time minute
13. _typecheck The Atom variable must be bound to an atomic F logic molecule as described below Result gets bound to the evidence of type violation one or two atoms that violate the typing constraint e If Atom is of the form Meth gt Mod then all type constraints for 7Meth are checked in module Mod Missing types semistructured data are flagged If Mod is a variable then 26 DEBUGGING USER PROGRAMS 117 the constraints are checked in all modules Meth can also be a variable In this case all non procedural methods will be checked e If Atom is of the form Meth gt Mod then the type constraints for Meth are checked in module Mod but missing types semistructured data are ignored As before Mod and 7Meth can be variables e If Atom is of the form 7Meth gt Mod then only the consistency between gt and gt is checked The gt style molecules are ignored Missing types semistructured data are flagged e If Atom is of the form 7Meth gt Mod then again only the consistency between gt and gt is checked The gt style molecules are ignored Missing types semistructured data are ignored For example if our knowledge base consists of a lb gt c a lb gt d Cid then the query will fail as the typing is correct Typel _check Meth gt Result _typecheck But if in addition we had a b gt e a foo gt e then the above query would yield multiple
14. buddhist is_vegetarian John buddhist The above says that all Buddhists are vegetarian and John the object with oid John is a Buddhist Since is_vegetarian is inheritable it follows that John is also a vegetarian i e John is_vegetarian 9 1 Boolean Signatures Boolean methods can have signatures like value returning methods For noninheritable Boolean methods signatures are specified as follows Class gt Meth For inheritable Boolean methods signatures are declared similarly C gt Meth 10 Anonymous and Generated Oids For applications where oids are not important FLORA 2 provides the compiler directive _ to automatically generate a new oid _ can be used wherever an Id term is allowed except in the 11 MULTIFILE PROGRAMS 24 rule body where such oids make no sense Like the anonymous variable _ each occurrence of _ represents an anonymous oid The difference is that such an oid is not only unique in each rule but in the source program as well Of course uniqueness is achieved through the use of special weird naming schema for such oids which internally prefixes them with several _ s However as long as the user does not use a similar naming convention who on earth would give names that begin with lots of _ s uniqueness is guaranteed For example in the following program _ ssn gt 123 father gt _ name gt John spouse gt _ name gt Mary foo _ X
15. c1 c2 i e c1 c2 is the least upper bound of c1 and c2 in the class hierarchy 2 If cl c and c2 c then c1 c2 c i e c1 c2 is the greatest lower bound of c1 and c2 in the class hierarchy 3 Any class c is considered a superclass of c _ and _ c In particular c c c At present FLORA 2 does not enforce the equality c c c 4 Any class c is considered a subtype of c _ and _ c In particular c c c At present FLORA 2 does not enforce the equality c c c 5 Any class c is considered a superclass of c d for any class d Unfortunately these subclass relationships can adversely affect certain user programs and FLORA 2 provides an optimization option that allows the user to disable these relationships for programs that do not need them See Section 27 2 Note Type expressions introduce a potential for infinite answers for seemingly innocuous queries For instance suppose that a c is true Then also a c c a c3c a c c c a c c c etc So the query a X will not terminate To mitigate this problem when class expressions are involved FLORA 2 guarantees to provide sound answers to queries about class membership and subclasses only when the arguments are ground it does not guarantee that all class expressions will be returned to queries that involve open calls to and 7 Path Expressions In addition to the basic F logic syntax the FLORA 2 system also supports
16. gt m gt gt abc 11 MULTIFILE PROGRAMS 40 Observe that the method g and the boolean method h have been exported in the updatable mode This means that the modules foo and bar can insert and delete the facts of the form a g gt b and a h b c using the statements like assuming that moo is the name of the module that includes the above directives insert alg gt b moo delete a h b c moo Parenthesizing rules Note that in the last three export statements above we used parentheses to disambiguate the syntax Without the parentheses these statements would be understood differently export e gt 7 7 gt gt foo bar export updatable g gt 7 h gt gt foo bar export updatable g gt 7 h 7 gt gt foo bar k gt m gt gt abc We should also note that updatable binds stronger than the comma or gt gt which means that an export statement such as the one bellow export updatable g gt 7 h gt gt foo is actually interpreted as export updatable g gt h 7 gt gt foo Exporting molecules other than gt In order to export any kind of call to a non Boolean method one should use only gt This will allow other modules to make calls such as a d c e gt X a b gt gt Z and cle gt t to the exported methods The export directive does not allow the user to separately
17. http www w3 org 2001 XMLSchema long 25 PRIMITIVE DATA TYPES 109 Class methods e _long _toType _symbol gt _long Converts strings to long integers if the string represents an integer in textual form If it does not then this method fails e long _toType _integer gt _long Converts long integers to arbitrary big integers e _integer gt _isTypeOf _object Other methods e long _toString gt _symbol e _long gt _equals _object e long gt _lessThan _object e long _typeName gt _symbol e _long _rawValue gt _number Extract the number part of the _long data type Examples 123 55 55 _long Caveat The long form and the short form must really be the same i e 123 _long and 123 must denote the same constant However this has not been implemented yet In fact it is not even possible to do arithmetics with the long representation of long integers To extract the number part from a _long data type one should use the method _rawValue For instance 12 long _rawValue gt X _basetype 25 10 The Primitive Types decimal and _integer At present FLORA 2 does not implement the decimal and the _integer types which correspond to XML Schema arbitrary precision types decimal and integer Instead decimal is a synonym for double and _integer for long As usual there are corresponding classes integer and decimal 25 11 The Primitive Type string This corresponds to th
18. lt Body UnlessDo unless Body do Body WhileDo while Body do Body WhileLoop while Body loop Body DoUntil do Body until Body LoopUntil loop Body until Body A A BNF STYLE GRAMMAR FOR FLORA 2 139 BodyLiteral BinaryRelationship ObjectSpecification Term DBUpdate Refresh NewoidOp Builtin Loading CatchExpr ThrowExpr TruthTest Builtin ArithmeticComparison Unification MetaUnification etc Loading LoadingCommand LoadingCommand LoadingCommand filename gt gt atom BinaryRelationship PathExpression PathExpression BinaryRelationship PathExpression PathExpression ObjectSpecification PathExpression SpecBody SpecBody not MethodSpecification SpecBody SpecBody SpecBody SpecBody SpecBody SpecBody MethodSpecification Term MethodSpecification PathExpression ValueReferenceConnective PathExpression ValueReferenceConnective gt gt gt gt 2 gt gt P gt gt 2 gt gt t PathExpression atom number string variable special0idToken PathExpression Term List ReifiedFormula PathExpression PathExpression PathExpressionConnective PathExpression PathExpression BinaryRelationship PathExpression ObjectSpecification PathExpression Aggregate PathE
19. 18 1 for details of these operations To illustrate the difference between transactional and non transactional updates consider the following execution trace immediately after the FLORA 2 system starts flora2 insert p a fail No flora2 p a flora2 t_insert q a fail flora2 q a No In the above example when the first fail executes the system backtracks to insert p a and does nothing Thus the insertion of p a persists and the following query p a returns with Yes However when the second fail executes the system backtracks to t_insert q a and removes q a that was previously inserted into the database Thus the next query q a returns with No This behavior is similar to database transactions whence the name transactional update 18 UPDATING THE KNOWLEDGE BASE 79 Notes on working with transactional updates Keep in mind that some things that Prolog programmers routinely do with assert and retract goes against the very concept of transactional updates e fail loops are not going to work will leave the database unchanged for obvious reasons The while and until loops should be used in such situations e Tabled predicates or methods must never depend on transactional updates First as explained on page 82 tabled predicates should not depend on any predicates that have side effects because this rarely makes sense Second when evaluating tabled predicates XSB performs backtrackin
20. All constants of this type belong to the built in class date The type name _date has the following synonyms _d http www w3 org 2001 XMLSchema date The following methods are defined for this type and are available through the system module basetype 25 PRIMITIVE DATA TYPES 103 Class methods e _date _toType _integer _integer _integer _integer _integer _integer _integer gt date The meaning of the arguments is as follows in that order date sign year month day zone sign zone hour zone minute All arguments except date sign and zone sign are assumed to be positive integers while date sign and zone sign can be either 1 or 1 e _date _toType _symbol gt _date e date gt _isType0f _object Tells if object belongs to the primitive type _date Component methods e date _dateSign gt _integer e date _year gt _integer e _date month gt _integer e date _day gt _integer e _date _zoneSign gt _integer e _date _zoneHour gt _integer e _date _zoneMinute gt _integer Other methods e date toString gt _symbol e date gt _equals _object e date gt _lessThan _object e date _typeName gt _symbol e date _add _duration gt _date Examples e 2001 11 23 2 30 date e 2001 11 23 _date e 237 11 23 _date Note that this date refers to year 238 BCE 25 PRIMITIVE DATA TYPES 104 e 2001 11 23 date day gt 23 basetype e
21. File isabsolute F True if F is an absolute path name 29 FLORA 2 SYSTEM MODULES 128 o File rename F To Renames file F to file To e File basename F Base Binds Base to the base name of file path F For instance File basename a b cde Base would bind Base to cde e File extension F Ext Binds Ext to the extension of the file F For instance File extension a b cde exe Ext would bind Ext to exe e File dirname F Dir Binds Dir to the directory name of file F e File expand F Expanded Expands the file F by attaching the directory name if the file is not absolute and binds 7Expanded to that expansion o File Y onewerthan F F2 True if F is a newer file than F2 e File copy F To Copies the contents of the file F to To 29 2 Storage Control FLORA 2 keeps the facts that are part of the program or those that are inserted by the program in special data structures called storage tries The system module db accessible through the module reference _db provides primitives for controlling this storage This module also has a longer synonym _storage e commit commits all changes made by transactional updates If this statement is executed in the middle of an update transaction changes made by transactional updates prior to this will be committed and will not be undone even if a subsequent subgoal fails e commit 7Module commits all
22. In any case the explicit equality option overrides the default Note that even if the module might have path expression in the head the default equality level is still none unless is used The reason for this is that such path expressions do not always require equality maintenance so the user has to request it explicitly For instance if in the above example we never insert John mother gt Sally then no equality maintenance will be required even if the program defines the fact John mother father gt Bob as above However if this fact is inserted then the equality maintenance level appropriate for this case is flogic basic will not be sufficient Locality of equality Equality in FLORA 2 is always local to the module in which it is derived For example if a b is derived by the rules in module foo then the query flora2 a b foo will succeed but the query 15 CUSTOM MODULE SEMANTICS 62 flora2 a b bar will fail unless of course a b is also derived by the rules in module bar Since equality information is local to each module the directives for setting the equality level affect only the particular user modules in which they are included Thus equality can be treated differently in different modules which allows the programmer to compartmentalize the perfor mance problem associated with equality and if used judiciously can lead to significant gains in performance Run time changes to the eq
23. Nixon Diamond illustrates the problem Let us assume the following knowledge base republican policy gt nonpacifist quaker policy gt pacifist nixon quaker Since Nixon is a Quaker we can derive nixon policy gt pacifist by inheritance from the second clause Let us now assume that the following information is added nixon republican Now we have a conflict There are two conflicting inheritance candidates policy gt pacifist and policy gt nonpacifist In FLORA 2 such conflicts cause previously established inheri tance to be withdrawn and the value of the attribute policy becomes undefined for object nixon Behavioral inheritance in F logic is discussed at length in 13 The above non monotonic behavior is just the tip of an iceberg Much more difficult problems arise when inheritance interacts with regular deduction To illustrate consider the following program b m gt c a b a m gt d a m gt c This behavior can be altered by adding additional rules For instance one could define a predicate hasPriority and then define 0bj policy gt P 0bj Class Class policy gt P not hasPriority AnotherClass Class 14 INHERITANCE 56 In the beginning it seems that alm gt c should be derived by inheritance and so we can derive alm gt d Now however we can reason in two different ways 1 alm gt c was derived based on the belief that attribute m is not defi
24. The problem here is that the interpretation of F logic expressions as objects is not always what we want In our example we need to indicate to the compiler that the first argument of foobar 1 ought to be translated into Prolog as follows foobar P X where P is the object that represents the formula a b gt X itself rather than just the oid a This can be accomplished using the reification feature of FLORA 2 a formula is compiled into an object that represents that formula if that formula is wrapped with the construct as in foobar a lb gt X X Reification is further discussed in Section 12 2 9 BOOLEAN METHODS 23 9 Boolean Methods As a syntactic sugar FLORA 2 provides boolean methods which can be considered as value returning methods that return some fixed value e g void For example the following facts John is_tall gt void John loves tennis gt void can be simplified as boolean methods as follows John is_tall John loves tennis Conceptually boolean methods are statements about objects whose truth value is the only con cern Boolean methods do not return any value not even the value void Therefore boolean meth ods cannot appear in path expressions For instance John is_vegetarian where is vegetarian is a binary method is illegal Like other methods boolean methods can be inheritable To make a boolean method inheritable the sign is prepended to the method name
25. X Y depends on ins X Y close and insert Notice that ignore depchk ins _ tells the compiler to ignore not only dependencies on 4ins 1 but also all dependencies that have 4ins 1 in the path The ignore_depchk directive can also be used to ignore direct dependencies on updates For example ignore depchk insert _ _ _ 18 UPDATING THE KNOWLEDGE BASE 83 ignores dependencies on conditional insertions which insert two literals such as insert a b c d e And ignore depchk insert ignores dependencies on unconditional insertions which insert exactly one literal such as insert p a but not insert p a p b 18 4 Updates and Meta programming The update operators can take variables in place of literals to be inserted For instance flora2 X alb gt c insert Xx One use for this facility is when one module foo provides methods that allow other modules to perform update operations on objects in foo For instance foo can have a rule wupdate X Y delete X insert Y Other modules can then issue queries like John salary gt X foo Y is X 1000 update John salary gt X John salary gt Y foo 18 5 Updates and Negation Negation applied to methods that have side effects is typically a rich source of trouble and confusion First of all applying negation to FLORA 2 molecules that involve non transactional updates does not have logical semantics and thus the programmer must hav
26. X lt 5 p X Intuitively one would expect 2 0 and 3 0 as answers However if you actually try to run this program you will be disappointed an error message will be reported Error XSB Runtime P Type Error Uninstantiated Arithmetic Expression Aborting 24 EXCEPTION HANDLING 94 This happens because ordinarily Prolog views gt 2 and lt 2 as predicates with infinite number of facts Since there are infinite number of values for X that make X gt O true it reasons the query does not make sense Constraint logic programming takes a different view it considers 7X gt 0 X lt 5 to be a constraint on the set of solutions of the query p X This approach allows Prolog to return meaningful solutions to the above query However the user must explicitly tell the system which view to take the dumb view that treats arithmetic built ins are infinite predicates or a smart view which treats them as constraints The smart view is indicated by enclosing constraints in curly braces Thus the above program becomes clpr must be loaded prior to the use of constraint solver insert p 1 0 p 2 0 p 3 0 X gt 0 X lt 5 p X X 2 000000e 00 X 3 000000e 00 2 solution s in 0 0000 seconds Note that the package clpr must be loaded in advance It should be kept in mind that the constraint solver is very picky about the type of values it is willing to work with It insists on floats an
27. canonic term list which represents the entire parsed program The canonic term is taken up by the intermediate code generator which generates abstract code This code is represented in a form that is convenient for manipulation and is not yet Prolog code The compiler might add additional rules such as patch rules and Prolog instructions The compiled program is converted into almost Prolog syntax by the coder As mentioned previously the code produced by the compiler is full of preprocessor macros so before passing it to Prolog it must be preprocessed by GPP GPP pipes the result to Prolog which finally produces the byte code program that can run under the control of the Prolog emulator The following is a list of the key files of the system e flrshell P The top level module that implements the FLORA 2 shell a subsystem for accepting user commands and queries and passing them to the compiler See Section 2 for a full description of shell commands e flrlexer P The FLORA 2 tokenizer e flrcomposer P The FLORA 2 composer which parses tokens according to the operator grammar and does other magic e flrparser P The FLORA 2 parser e flrcompiler P The generator of the intermediate code e flrcoder P The FLORA 2 coder which generates Prolog code D INSIDE FLORA 2 153 Flora Program t t Parser Coder flrparser P flrcoder P Tokenizer flrlexer P Ma
28. gt Y bar Y gt X the compiler will generate unique oids for each occurrence of _ Note that in the second clause only one oid is generated and it serves as a method name In some situations it is needed to be able to create a new oid and use it within the same rule head or a fact Since such an oid needs to be referenced inside the same program clause it is no longer possible to use _ because each occurrence of _ causes the compiler to generate a new oid To solve this problem FLORA 2 allows numbered anonymous oids which are of the form _ 132 i e _ with a number attached to it For instance _ 1 ssn gt 123 father gt f _ 1 name gt John spouse gt _ name gt Mary _ 1 self gt _ 1 The first time the compiler finds _ 1 in the first clause above it will generate a new oid However the second occurrence of _ 1 in the same clause i e f _ 1 will use the oid previously generated for the first occurrence On the other hand occurrences of _ 1 in different clauses are substituted with different oids Thus the occurrences of _ 1 in the first and second clauses above refer to different objects Anonymous oids are generated at compile time without regard for the oids that might exist at run time Sometimes it is necessary to generate a completely new oid at run time This can be accomplished with the newoid builtin For instance flora2 newoid X X _ _ _flora dyn_newoid308 1 solution s in 0 000
29. is a potential pitfall The problem is that these loops will continue as long as there is a way to satisfy Condition If condition stays true the loop continues forever Therefore the way to use these loops is to make sure that Condition is modified by Action If Action has non transactional updates the user must ensure that if Action fails then Condition is modified appropriately anyway for otherwise the loop will never end If Action is fully transactional and it fails then using the Action true idiom in the loop body will definitely make the loop infinite so the use of this idiom in the body of while loop and loop until is dangerous if there is a possibility that Action will fail and it is useless if the action is expected to always succeed 23 Constraint Solving The following feature temporarily does not work since beginning with XSB 2 6 constraint solving is being revamped and is not supported FLORA 2 provides an interface to constraint solving capabilities of the underlying Prolog en gine Currently XSB supports linear constraint solving over the domain of real numbers CLPR However we must warn that the XSB implementation of CLPR has many rough spots do not say that we did not warn To pass a constraint to a constraint solver in the body of a FLORA 2 rule or query simply include it inside curly braces Here is a 2 minute introduction to CLPR Try the following program insert p 1 0 p 2 0 p 3 0 X gt 0
30. 2 provides a utility that makes the compiled Prolog program more readable The _dump 1 predicate can be used to strip the macros from the code making it much easier to understand If you issue the following command flora2 _dump foo the program foo f1r will be compiled without the macros and dump the result in the file foo_dump P This file is pretty printed to make it easier to read Similarly flora2 _dump foo bar will compile foo flr for module bar and will dump the result to the file foo_dump P Unfortunately this more readable version of the translated FLORA 2 program might still not be executable on its own because it might contain calls to FLORA 2 libraries or other modules The set of guidelines below can help cope with these problems Reporting FLORA 2 related Prolog bugs If you find a Prolog bug triggered by a FLORA 2 program here is a set of guidelines that can simplify the job of the XSB developers and increase the chances that the bug will be fixed promptly 1 Reduce the size of your FLORA 2 program as much as possible while still being able to reproduce the bug 2 Eliminate all calls to the system modules that use the _1ib syntax Prolog modules that are accessible through the _prolog modname syntax are OK but the more you can eliminate the better 3 If the program has several user modules try to put them into one file and use just one module 4 Use _dump 1 to strip FLORA 2 macros from the output o
31. Component methods _iril_ scheme gt _symbol e iri user gt _symbol iri _ host gt _symbol e _iri _ port gt _symbol e _iri _ path gt _symbol e _iri _ query gt _symbol e _iri _ fragment gt _symbol Note that the exact meaning of the above components depends on the URI scheme For http ftp file etc the meaning the first five components is clear The query is an optional part of the IRI that follows the sign and fragment is the last part that follows Some components might be optional for some URI schemes For instance for the urn and file schemata only the path component is defined For mailto scheme port path query and fragment are not defined If a scheme is not recognized then the part of the URI that follows the scheme goes into the path component unparsed Other methods e _iri toString gt _symbol1 e iri gt _equals _object e _iri _typeName gt _symbol1 Examples e _ http foo bar com abc e http foo bar com abc _iri e _iril _toType http foo bar com abc gt http foo bar com abc _iri _basetype e _ http foo bar com abc _iri host gt foo bar com _basetype 25 PRIMITIVE DATA TYPES 101 25 3 The Primitive Type dateTime This data type corresponds to the XML Schema dateTime type The constants of this data type have the form ZYYYY MM DDTHH MM SS sZHH MM _dateTime The symbols T and are part of the syn
32. HILOG AND METAPROGRAMMING 47 reified rule In general conjunctions of rules are allowed inside the reification operator e g rule1 rule2 where each rule is enclosed in a pair of parentheses Such a conjunction can then be inserted or deleted into the rulebase using the insertrule orimitivell Reification and meta unification Reification should not be confused with meta unification although they are close concepts A reified formula reflects the exact structure that is used to encode it so structurally similar but syntactically different formulas might meta unify but their internal representations could be very different For instance flora2 a b gt X 0 7M Y b gt d foo will return true because the two molecules are structurally similar and thus meta unify On the other hand flora2 a b gt 7X O7M Y b gt d foo will be false because a b gt Y X and Z b gt d foo have different internal representations even though their conceptual structures are similar so they do not unify using i e in the usual first order sense Note however that the queries alb gt 7Y foo Z b gt d foo M foo a b gt 7Y 7M Z b gt d 7M a b gt Y foo Z b gt d foo M foo alb gt Y 7M Z b gt d M will all return true because a b gt Y foo and Z b gt d foo are structurally similar both conceptually and as far as their inter
33. X in the body of a rule For instance p X caller X write I was called by module writeln X _prolog When a call to predicate p X is made from any module say foobar and the above rule is invoked as a result then the message I was called by module foobar will be printed 11 5 Loading Files into User Modules FLORA 2 provides several commands for compiling and loading program files into specified user modules Compilation The command flora2 _compile file gt gt module generates the byte code for the program to be loaded into the user module named module The name of the byte code for the program in file flr which can later be loaded into the specified module In practice this means that the compiler generates files named file module P and file module cwam with symbols appropriately renamed to avoid clashes If no module is specified the command flora2 _compile file 11 MULTIFILE PROGRAMS 29 compiles file flr for the default module main Loading The above commands compile files without actually loading their contents into the in memory knowledge base To load a file the following commands can be used flora2 myprog flora2 _load myprog This loads the program in the file myprog flr into the default user module main If myprog flr is newer than the compiled code the source file is recompiled An optional module name can be given to tell FLORA 2 to load the program into
34. Y e X 7Y flora2 insertrule_a tc X Y tc X Z e Z 7Y flora2 newmodule foo flora2 insertrule_a tc X 7Y e X Y _ foo flora2 insertrule_a tc X Y tc X Z e Z Y _ foo 20 QUERYING THE RULE BASE 88 Then the query flora2 clause X 7Y will list all the inserted rules In this case four rules will be returned To query specific rules in a specific module for example rules defined for the predicate tc 2 in the module foo we can use flora2 clause tc X Y foo Z We can also query rules by providing patterns for their bodies For example the query flora2 clause X e _ _ will return the first and the third rules Querying the rules with composite involves the following subtlety Recall from Section 19 3 that a rule with a composite head such as o b gt V1 c gt V2 something V1 V2 is treated as a pair of rules o b gt V1 something V1 V2 o c gt V2 something V1 V2 Therefore if we delete one of these rules for instance flora2 deleterule o b gt V1 something V1 7V2 then a query with a composite head that involves the head of the deleted rule will fail unless there is another matching rule Thus the following query will fail flora2 clausefo b gt V1 d gt V2 Body The clause primitive can be used to query static rules just as it can be used to query dynamic rules The normal tw
35. aggregates return the empty list 11 See http www informatik uni freiburg de dbis florid for more details 21 AGGREGATE OPERATIONS 90 In general aggregates can appear wherever a number or a list is allowed Therefore aggregates can be nested The following examples illustrate the use of aggregates some borrowed from the FLORID manual flora2 Z min S John salary Year gt S flora2 Z count Year John salary Year lt max S John salary Y gt S Y lt Year flora2 avg S Who Who employee salary Year gt S gt 20000 If an aggregate contains grouping variables that are not bound by a preceding subgoal then this aggregate would backtrack over such grouping variables In other words grouping variables are considered to be existentially quantified For instance in the last query above the aggregate will backtrack over the variable Who Thus if John s and Mary s average salary is greater than 20000 this query will backtrack and return both John and Mary The following query returns for each employee a list of years when this employee had salary less than 60 This illustrates the use of the collectset aggregate flora2 Z collectset Year Who Who salary Year gt X X lt 60 Z 1990 1991 Who Mary Z 1990 1991 1997 Who John 21 1 Aggregation and Set Valued Methods Aggregation is often used in conjunction with set valued methods and FLORA 2 provides
36. an example setsemantics equality basic custom a b c The order of the options in the directive does not matter Changing module semantics precautions Changing module semantics on the fly at run time is a rather drastic operation It is therefore not recommended to do this in the body of a rule especially if the rule defines a tabled HiLog predicate or an F logic molecule The only safe way to execute setsemantics is in a query at the top level For instance setsemantics 15 1 Equality Maintenance User defined equality FLORA 2 users can define equality explicitly in the source program using the predicate e g John Batman X Y X similar gt 7Y 15 CUSTOM MODULE SEMANTICS 61 Once two oids are established to be equal with respect to whatever is true of one object is also true of the other Note that is different from the built in The latter is a predefined primitive which cannot occur in the facts or in the rule head Since is understood as unification ground terms can be equal only if they are identical Thus a a is always true and a b is always false In contrast the user can assert a fact such as a b and from then on the object a and the object b are considered the same modulo the equality maintenance level which is described below Equality maintenance levels Once an equality between terms is derived this information may need to be propagated to all F logic
37. and FLORA 2 distinguishes between attributes and methods that can inherit values from superclasses and those that do not The syntax that we have seen so far in this manual applies to non inheritable attributes only Inheritable attributes are declared using the gt style arrow and defined using the gt style arrow Note that while gt typically occurs in facts and rules that define the properties of individual objects gt normally occurs in definitions of classes Non inheritable attributes sometimes correspond to what is known as class variables in tra ditional object oriented languages For example the attribute average age would be such an attribute for class person It does not make sense to propagate this attribute and its value to the instances of that class because the concept of an average age does not apply to individual people Similarly it does not make sense to propagate this attribute to subclasses such as student because the average age of students is likely to be different from that of persons and therefore the inherited value would be of no use In FLORA 2 we would specify such a fact as follows 14 INHERITANCE 54 person avg_age gt 40 Similarly attributes that typically refer to individuals are better specified as non inheritable be cause normally there is nothing to inherit these attributes to John age gt 30 Inheritable attributes typically define default properties of the objects in a clas
38. attribute m is indeed r Although previously we discussed only the object interpretation for path expressions it is easy to see that they have truth values as well because a path expression corresponds to a conjunction of F logic atoms Consequently all F molecules of the form 1 through 7 have dual reading As logical formulas the deductive perspective and as expressions that represent one or more objects the object oriented perspective Given an intended model Z of an F logic program an expression has e An object value which yields the Id s of the object s that are reachable in Z by the corre sponding expression and e A truth value like any other literal or molecule of the language An important property that relates the above interpretations is a molecule r evaluates to false if T has no object corresponding to r Consider the following path expression and an equivalent decomposed expression a blc gt d e lt alb gt Xap A dle gt Xge A Xap c gt Xae 2 Such decomposition is used to determine the truth value of arbitrarily complex path expressions in the body of a rule Let obj path denote the Ids of all objects represented by the path expression Then for 2 above we have obj d e Xae Z H dle gt xae 8 TRUTH VALUES AND OBJECT VALUES 22 where Z means that y holds in Z Observe two formulas can be equivalent but their object values
39. be constructed 31 MISCELLANEOUS FEATURES 134 31 Miscellaneous Features 31 1 Suppression of Banners When FLORA 2 initializes itself it produces a lot of chatter the XSB banner a great number of messages about the various XSB and FLORA 2 modules that are loaded and the FLORA 2 welcome message However the user might not want to see all these and when the program runs in a batch mode or interacts with other programs then all these messages are either not needed or they can complicate this interaction To suppress these messages use the following switches when invoking the system nobanner suppress the XSB banner and the FLORA 2 welcome message quietload when loading a module do not print feedback to the terminal Thus runflora nobanner quietload will get you directly to the FLORA 2 prompt without cluttering the terminal with chatter Sometimes even the prompt stands in the way For instance when FLORA 2 interacts with other programs e g with a GUI then sending the prompt to the other program just complicates things as the receiving program needs to remember to ignore the prompt To avoid this complication the invocation flag noprompt is provided Thus runflora nobanner quietload noprompt will print nothing on startup and will be just waiting for user input When the input occurs FLORA 2 will evaluate the query and return the result After this it will return to wait for the input without issuing any
40. classical negation none of the above formulas is actually equivalent to V Vars Goal if is understood as classical negation A more precise meaning is that not Goal is true if and only if for every ground instance Goal of Goal the literal not Goal is true in the well founded semantics Similarly Goal evaluates to true if and only if for every ground instance Goal of Goal the query Goal succeeds according to the negation as failure semantics To illustrate this consider the following example p a b q X 7Y not p X Y flora q X Y When not p X Y is called in the query evaluation process the variables are unbound so for the query to return a positive answer the literal p t s should be false for every possible terms t and s Since p a b is true our query q X 7Y fails In contrast the query flora2 q b Y will succeed because this will cause the query not p b Y to be evaluated But this query will return positive answer because p b Y is false for all Y Note that even when the query succeeds the unbound variable that occurs in the scope of the negation operator remains unbound flora2 not p b Y Y _hi747 1 solution s in 0 0000 seconds on speedy foo org 14 Inheritance In general inheritance means that attribute and method specifications for a class are propagated to the subclasses of that class and to the objects that are instances of that class F logic
41. control calls to the molecules that involve the method specifiers such as gt gt gt gt etc The export directive has an executable counterpart For instance at run time a module can execute an export instruction such as export e gt f gt gt foo bar and export the corresponding methods If the module was not encapsulated before it will become now Likewise it is possible to execute export directives in another module For instance executing export e gt 7 7 gt gt foo bar foo will cause the module foo to export the specified methods and to encapsulate it if it was not encapsulated before 11 MULTIFILE PROGRAMS 41 11 13 Importing Modules Referring to methods and predicates defined in other modules is one way to invoke knowledge defined separately in another program Sometimes however it is convenient to import the entire module into another module This practice is particularly common when it comes to reusing ontologies FLORA 2 supports import of modules through the importmodule compile time directive Its syntax is as follows importmodule module1 module2 module k Once a module is imported its methods and predicates can be referenced without the need to use the module idiom Importing a module is not the same as including another module as a file with the include statement First only exported methods and predicates can be referenced by the i
42. developers how to reproduce the bug Make sure you include all the steps including such gory details as whether it is necessary to call bootstrap_flora 0 Finally remember to include the details of your OS and other relevant information Some bugs might be architecture dependent Reporting FLORA 2 bugs If you believe that the bug is in the FLORA 2 system rather than in the underlying Prolog engine the algorithm is much simpler 1 Reduce the size of the program as much as possible by deleting unrelated program clauses and squeezing a multi module program into just one file 2 Remove all the calls to system modules unless such a call that is the essence of the bug 3 Tell FLORA 2 developers how to reproduce the bug The current version contains the following known bugs which are due to the fact that certain features are yet to be implemented 1 Certain programs might cause the following XSB error message Error XSB Compiler after table dependent symbol 32 BUGS IN PROLOG AND FLORA 2 HOW TO REPORT 137 This is due to certain limitations in the implementation of tabled predicates in the XSB system This problem will be eliminated in a future release of XSB Meanwhile as explained in the Introduction configuring XSB for SLG WAM and local scheduling will avoid many of such errors 2 Error messages when FLORA 2 update predicates contain arithmetic expressions in the query part This problem will be fixed in the fu
43. e abc e a string n _ string e a string n e a tstring b e string with a quoted substring Caveat The long form and the short form must really be the same i e abc _string and abc must denote the same constant However this has not been implemented yet To extract the atom part from a _long data type one should use the method _rawValue For instance abc _string _rawValue gt X _basetype 25 12 The Primitive Type list This is the usual Prolog list type The members of this type have the form elt1 eltn _list short form elt1 eltn and belong to class list The following methods are available from the standard module _basetype Class methods e _list gt _isTypeDf _object e list _toType _list gt _list Other methods e list gt _contains _list Tells if a list object contains the method s argument as a sublist e list gt _member _object The method s argument and the list object may not be fully ground In this case the method succeeds if the argument to the method unifies with a member of the list e list _append _list gt _list e list length gt _long Computes the length of the list e list _ reverse gt _list 26 DEBUGGING USER PROGRAMS 112 e list _sort gt _list e list gt _startsWith _list e list gt _endsWith _list e list gt _subset _list True if the list object contains the argument li
44. e trace _notrace Turn on off FLORA 2 trace e chatter _nochatter Turn on off the display of the number of solutions at the end of query evaluation e end Say Ciao to FLORA 2 stay in Prolog e halt Quit both FLORA 2 and Prolog 3 F LOGIC AND FLORA 2 BY EXAMPLE 7 Of course many other executable directives and queries can be executed at FLORA 2 shell These are described further in this manual In general FLORA 2 built in predicates whose name is of the form 1 A Z are either the FLORA 2 shell commands or predicates that can be used in Prolog to control the execution of FLORA 2 modules We will discuss the latter in Section 11 8 Some of these commands mostly dealing with loading and compilation of FLORA 2 modules can also be useful within FLORA 2 applications All commands with a FILE argument passed to them use the Prolog library_directory pred icate to search for the file except that the command _demo FILE first looks for FILE in the FLORA 2 demo directory The search path typically includes the standard system s directories used by Prolog followed by the current directory All Prolog commands can be executed from FLORA 2 shell if the corresponding Prolog library has already been loaded After a parsing or compilation error FLORA 2 shell will discard tokens read from the current input stream until the end of file or a rule delimiter is encountered If FLORA 2 shell seems to be hanging after the
45. evidences of type inconsistency Result alb gt e a b gt d Result alfoo gt e The first means that the atom a b gt e violates the type constraint specified by the signature a b gt d The second means that the specified atom does not have a corresponding signature On the other hand Typel _check Meth gt Result _typecheck will yield only the first evidence because a foo gt e does not violate any typing constraints for semistructured data If the object position in the first argument of check is bound then this object is treated as a class and only the objects in that class will be type checked For instance if we also had 26 DEBUGGING USER PROGRAMS 118 q foo gt bar q aqq in our knowledge base then the query Typel _check qq Meth gt 7 Result _typecheck will return one evidence of type inconsistency Result q foo gt bar because q is the only object in class qq that has type violations An easy way to remember which type of atoms represent what kind of type checking is to think that gt represents typing and therefore the gt style atoms mean that only the methods that have typing information will be type checked The gt style atoms on the other hand mean that all methods will be checked whether they have signatures or not Similarly gt and gt means that only the default values represented by gt will be checked and gt in
46. gt c foo Moreover in the second query the variable N is not bound to anything because as noted before the literal X N becomes a lb gt c foo N at run time and due to the scoping rules is the same as a b gt c foo 12 2 Reification It is sometimes useful to be able to treat FLORA 2 molecules and predicates as objects For instance consider the following statement Tom believes gt Alice thinks gt floraProgramming coolThing The intended meaning here is that one of Tom s beliefs is that Alice thinks that programming in FLORA 2 is a cool thing Unfortunately this is incorrect because as stated the above formula has a different meaning 12 HILOG AND METAPROGRAMMING 46 Tom believes gt Alice Alice thinks gt floraProgramming coolThing That is Tom believes in Alice and Alice thinks that FLORA 2 programming is cool This is different from what we originally intended For instance we did not want to say that Alice likes FLORA 2 she probably does but she did not tell us All we said was what Tom has certain beliefs about what Alice thinks In other words to achieve the desired effect we must turn the formula Alice thinks gt floraProgramming coolThing into an object i e reify it Reification is done using the operator For instance to say that Tom believes that Alice thinks that programming in FLORA 2 is a cool thing one should write Tom believes gt Alice thinks gt floraProgra
47. gt val1i o att2 gt val2 of meth gt res 5 5 Arithmetic Expressions In FLORA 2 arithmetic expressions are not always evaluated As in Prolog the arithmetic operators such as and are defined as normal binary functors To evaluate an arithmetic expression FLORA 2 provides another operator is For example X is 3 4 will bind X to the value 7 When dealing with arithmetic expressions the order of literals is important In particular all variables appearing in an arithmetic expression must be instantiated at the time of evaluation Otherwise a runtime error will occur For instance X gt 1 X is 1 1 In brief represents negation as failure and can be applied only to non tabled Prolog FLORA 2 or HiLog predicates not on the other hand is negation that implements the well founded semantics Refer to Section 13 for more information on the difference between negation operators 5 BASIC FLORA 2 SYNTAX 17 will produce an error while 7X is 1 1 X gt 1 will evaluate to true As in Prolog the operands of an arithmetic expression can be any variable or a constant However in FLORA 2 an operand can also be a path expression For the purpose of this discussion a path expression of the form p q should be understood as a shortcut for p g gt X where X is a new variable and p q r is a shortcut for p q gt X X r gt Y More detailed discussion of path expressio
48. import _load 1 _compile 1 from flora2 _load flora file gt gt flora module load flora file compile flora file gt gt flora module compile flora file The first query loads the file flora file into the given user module and compiles it if necessary The second query loads the program into the default module main The last two queries compile the file for loading into the module flora module and main respectively but do not load it 11 MULTIFILE PROGRAMS 35 Finally a Prolog program can check if a certain FLORA 2 user module has been loaded using the following call import _isloaded 1 from flora2 _isloaded flora module name Note that when used inside Prolog the _isloaded predicate must be quoted Quoting is not necessary when it is used in FLORA 2 Note If you are using a release of FLORA 2 that is installed outside of the XSB directory tree you must make sure that Prolog will find this installation and use it One way of doing this was described earlier by executing an appropriate asserta 1 This method works best if your application consists of both FLORA 2 and Prolog modules but the initial module of your application 7 e the one that bootstraps everything is a Prolog program If the initial module is a FLORA 2 program then the best way is to start XSB and FLORA 2 using the runflora script page 3 located in the distribution of FLORA 2
49. instance Str X b gt Y flora_query Str X YYY Y PPP _Status Exception writeln X YYY writeln Y PPP fail Note that the Prolog variables in the variable list like YYY and PPP above can be bound and in this way input to the FLORA 2 query can be provided For instance YYY abc flora_query X b gt Y X YYY Y PPP _Status Exception yields the same result as flora_query abc b gt Y Y PPP _Status _Exception However the user should be aware of the fact that if a query is going to be used many times with different parameters then the first form is much faster That is 11 MULTIFILE PROGRAMS 37 YYY abci flora_query X b gt Y X YYY Y PPP _Status Exception YYY abc2 flora_query X b gt Y X YYY Y PPP _Status _ Exception is noticeably faster than flora_query abci b gt Y Y PPP _Status _Exception flora_query abc2 b gt Y Y PPP _Status _Exception if the above queries are executed thousands of times with different parameters abci abc2 etc 11 9 FLORA 2 System Modules FLORA 2 provides a special set of modules that are preloaded with useful utilities such as pret typrinting or I O These modules have special syntax modname and cannot be loaded by the user For this reason these modules are called FLORA 2 system modul
50. is not based on a model theory FLORA 2 uses a different more cautious semantics for inheritance which favors the first interpretation above Details of this semantics are formally described in 13 Under this semantics clyde will still inherit color white but in the other two examples a m gt c is not inherited The basic intuition can be summarized as follows 1 Method definitions in subclasses override the definitions that appear in the superclasses 2 Incase of a multiple inheritance conflict the result of inheritance is undefined More precisely FLORA 2 is based on a three valued logic and in this case the truth value is unknown 3 Inheritance from the same source through different paths is not considered a multiple inher itance conflict For instance in aic ciie e m gt f a d d e 14 INHERITANCE 57 Even though we derive c m gt f and d m gt f by inheritance these two facts can be further inherited to the object a since they came from a single source e On the other hand in a similar program a c c m gt f a d d m gt f inheritance does not take place the truth value of a m gt f is undefined because the two inheritance candidates c m gt f and d m gt f are considered to be in conflict Note that in the last example one might argue that even if we did inherit both facts to a there would be no discrepancy because in both cases the values of the attribute m ag
51. is the take no prisoners version of erase Just like deleteall it first computes query and for each binding of variables it deletes the corresponding instance of literal For each deleted object it then finds all objects it references through its methods and deletes those This continues recursively until nothing reachable is left This primitive always succeeds and leaves its free variables unbound 18 UPDATING THE KNOWLEDGE BASE 78 18 2 Backtrackable Logical Updates The effects of transactional updates are undone upon backtracking i e if some post condition fails and the system backtracks a previously inserted item will be removed from the database and a previously deleted item will be put back The syntax of transactional update primitives is similar to that of non transactional ones and the names are similar too The syntax for transactional insertion is t_insop literals formula while the syntax of a transactional deletion is t_delop literals query where t_insop stands for either t_insert or t_insertall and t_delop stands for either of the fol lowing four deletion operations t_delete t_deleteall t_erase and t_eraseall The meanings of literals and query is the same as in Section 18 1 t_insert t_insertall t_delete t_deleteall t_erase and t_eraseall work similarly to insert delete deleteall erase and eraseall respectively except that the new operations are transactional Please refer to Section
52. is user defined can be a molecule as well which will be called when an undefinedness exception occurs The variable Cal1 will be bound to an internal representation of the method or predicate call that caused the exception For instance if we define handler _ then the undefinedness exception that occurs while executing FOO will be ignored and the call to FOO will succeed Undefinedness check and meta programming We should note one subtle interaction be tween these checks and meta programming Suppose your program does not have any class mem bership facts and the undefinedness checks are turned on Then the meta query flora2 a X would cause the following error Error FLORA Undefined class in user module main Likewise if the program does not have any method definitions the query X Y gt Z would cause an error This might not be what one expects because the program in question might be exploring the schema or the available data and the intention in the above cases might be to fail rather than to get an error One way of circumventing this problem is to insert some unusual facts into the database and special case them in the program For instance you could put the following facts into the program to silence the above errors ads_asd_fsffdfd ads_asd_fsffdfd ads_asd_fsffdfd ads_asd_fsffdfd gt ads_asd_fsffdfd You can then arrange the logic of your program so that anything that contains ads_asd_f
53. library provides just a few common ones See the XSB manual volume 2 for a complete list of these primitives The methods and predicates accessible through the io library are listed below Note that some operations are defined as procedural methods and others as predicates This is because we use the object oriented representation only where it makes sense we avoid introducing additional classes and objects that require more typing just for the sake of keeping the syntax object oriented 12 See Section 12 for a discussion of the problems associated with this representation mismatch 29 FLORA 2 SYSTEM MODULES 126 Stream based I O e see Filename Filename see open Filename and make it into the current input stream e seeing 7Stream binds Stream to the current input stream e seen closes the current input stream e tell Filename Filename tell opens Filename as the current output stream e telling Stream binds Stream to the current output stream e told closes the current output stream e write 0bj writes 0bj to the current output stream e Stream write 0bj writes 70bj to the stream Stream e writeln 0bj Stream writeln 70bj same as above except that the newline character is output after 0bj e nl writes the newline character to the current output stream e read Result binds Result to the next term in the current input
54. like X foo 7Y Z moo gt W W X rest will not cause problems under the local scheduling but X foo gt Y Z moo gt W W 7X rest will likely result in a runtime error The reason is that in the first case the molecule X f00 Y is implemented as a non tabled predicate so by the time the evaluation reaches the cut both Z moo gt W and W X will be evaluated completely and their tables will be marked as complete In contrast in the second example X foo gt Y is implemented as a tabled predicate which is interrelated with the predicates that are used to implement Z moo gt W and W X Thus the cut would occur in the middle of the computation of the table for X foo gt Y and an error will result 18 UPDATING THE KNOWLEDGE BASE 73 In a future release XSB will implement a different tabling schema While cutting across tables will still be prohibited it will provide an alternative mechanism that achieves many of the goals a cut is used for 18 Updating the Knowledge Base FLORA 2 provides primitives to update the runtime database Unlike Prolog FLORA 2 does not require the user to define a predicate as dynamic in order to update it Instead every predicate and object has a base part and a derived part Updates directly change only the base parts and only indirectly the derived parts Note that the base part of a predicate or an object contains both the facts that were inserted explic
55. main Low to 2 and High to ok Indeed only the lower bound of the cardinality constraint c foo 2 3 gt int which was inherited from c1 is violated by the class c Cardinality _check 0 foo Low High gt _typecheck will return the following results 70 o1 Low ok High 3 70 02 Low 2 High ok 26 4 Logical Assertions that Depend on Procedural and Non logical Features On page 82 we mentioned the potential problems when tabled predicates or F logic molecules depend on updates A similar problem arises when such statements depend on non logical features such as var or on statements that have side effects such as I O operations e g write foo bar _prolog Since tabled statements in FLORA 2 are considered purely logical one cannot assume that the evaluation happens in the same way as in Prolog For instance consider the following program 0 bar 0 foo 70 foo writeln executed _prolog abcl bar Despite what one might expect the above query will cause executed to be printed twice once when abc bar will be proved for the first time and once when the system will attempt some other way of proving abc bar The system may not realize that the second proof is not necessary In general procedural and side effectful statements might be executed even if the attempt to prove the statement in the rule head ultimately fails 27 OPTIMIZATIONS 121 F
56. of the Prolog module not FLORA 2 module in which the predicate PredName Arity was called If you call a Prolog predicate in a Prolog module that does not exist then Prolog will throw the exception error existence_error module Module _ Note that the thrown exception will contain a module name but not the predicate Although Prolog obviously throws Prolog terms as exceptions there is no need to worry about making sure that the terms caught by the FLORA 2catch primitive are also specified as Prolog terms he primitive takes care of the Prolog to HiLog conversion automatically 25 Primitive Data Types An extensive data type support is being planned for FLORA 2 in the future At present FLORA 2 supports the built in data types _long _integer double _decimal _string symbol _object _iri international resource identifier time date dateTime and _duration Following the now accepted practice on the Semantic Web FLORA 2 denotes the constants that belong to a particular primitive data type using the idiom Literal type The literal part rep resents the value of the constant and the type part is the type For instance 2004 12 24 date 2004 12 24T15 33 44 dateTime A type name must be an atom Some data types like time dateTime etc are exact analogues of the corresponding XML Schema types In this case their names will be denoted using symbols that have the form of a URI For instance http www w3
57. order to be able to capture undefined methods i e methods those definition was not supplied by the user see Section 26 1 All these predicates are connected through an elaborate set of rules which appear in closure f1h files and also in genincludes flrpreddef f1lh these flh files are generated from the corresponding fli files at FLORA 2 configuration time The following diagram shows the main predicates involved in the plumbing system that connects the D INSIDE FLORA 2 148 derived_ and d_ the arrow lt indicates the immediate dependency relationship i e that the predicate on the right appears in the body of a rule that defines the predicate on the left In program rules derived_mvd lt d_mvd In auxiliary runtime libraries d_mvd lt mvd d_imvd lt imvd mvd lt inferred_mvd mvd lt not inferred_mvd lt derived_mvd mvd lt not conflict_obj_imvd lt imvddef lt mvd mvd lt imvd mvd lt immediate_isa imvd lt immediate_sub imvd lt inferred_imvd lt derived_imvd imvd lt not inferred_imvd imvd lt not conflict_imvd lt imvddef imvddef lt imvd derived_mvd lt storage_find_fact trie_name mvd Here we listed only the predicates that are used to model value returning inheritable imvd and non inheritable mvd methods A similar diagram exists for method signatures There is additional machinery for IS A and sub
58. path expressions to simplify object navigation along value returning method applications and to avoid explicit join conditions 6 The basic idea is to allow the following path expressions wherever Id terms are allowed 7 OM Path expressions are allowed only in rule bodies The path expression in 7 refers to the unique object Ro for which O M gt Ro holds The symbols 0 and M stand for an Id term or path a expression Moreover M can be a method that takes arguments in which case 0 M P4 P x is a valid path expressions In order to disambiguate the syntax and to specify the desired order of method applications parentheses can be used By default path expressions associate to the left so a b c is equivalent 7 PATH EXPRESSIONS 20 to a b c which specifies the object o such that a b gt x A x c gt o holds note that x a b In contrast a b c is the object o1 such that b c gt x1 A a x1 gt 01 holds note that in this case x1 b c In general o and o1 can be different objects Note also that in a b c b is a method name whereas in a b c it is used as an object name and b c as a method Observe that function symbols can also be applied to path expressions since path expressions like Id terms represent objects Thus f a b is a valid expression Note A path expression can appear wherever an oid can but not in place of a truth valued expression e g a subquery even though path expressions can be vi
59. programmer does not need to worry about tabling the right predicates Second there is no need to worry that a predicate must be declared as dynamic in order to be updatable Third and most important the facts specified in the program are considered to be part of the database In particular their order does not matter and duplicates are eliminated automatically 30 1 Facts are Unordered The fact that FLORA 2 does not assume any particular order for facts has a far reaching implication on the programming style and is one of the pitfalls that a programmer should avoid In Prolog it is a common practice to put the catch all facts at the end of a program block in order to capture subgoals that do not match the rest of the program clauses For instance p X 0 p g X 25 24 hh If all else fails simply succeed p _ This will not work in FLORA 2 because p _ will be treated as a database fact which is placed in no particular order with respect to the program If you want the desired effect represent the catch all facts as rules p X p g X ws 4h If all else fails simply succeed Use rule notation for p 1 p _ true 30 2 Testing for Class Membership In imperative programming users specify objects properties together with the statements about the class membership of those objects The same is true in FLORA 2 For instance we would 30 NOTES ON THE PROGRAMMING STYLE AND COMMON PITFALLS 132 s
60. stream e Stream read Result same as above but use Stream as the input stream Port based I O e Filename Zopen Mode Port opens Filename with mode Mode which can be r w or a and binds Port to the file handle e Port close closes the file handle to which Port is bound e Port Zread Result bind Result to the next term in the previously open input Port e stdread Result same but use the standard input as the port e Port Zwrite Result write Result out to the previously open output Port e stdwrite Result same but use standard output as the port e fmt_write Format Term C style formatted output to the standard output See the XSB manual volume 2 for the description of all the options e Port Zfmt_write Format 0 same but use Port for the output e imt_write_string String Format 0bj same as above but bind String to the result See the XSB manual for the details 29 FLORA 2 SYSTEM MODULES 127 Zfmt_read Format Result Status C style formatted read from standard input See the XSB manual Port Zfmt_read Format Result Status same but use Port for input Zwrite_canonical Term write Term to standard output in canonic Prolog form Port Zwrite_canonical Term same but use Port for output read_canonical Term read standard input and bind Term to the next term in the input The term mus
61. system to treat X b as a Prolog term Clearly this might be too much writing in some cases and it is also error prone Moreover bindings returned by Prolog predicates are Prolog terms and they somehow need to be converted into HiLog To simplify calls to Prolog FLORA 2 provides another more powerful primitive _prologall In the above case one can call foo f X b _prologall without having to worry about the differences between the HiLog representation of terms in FLORA 2 and the representation used in Prolog One might wonder why is there the _prolog module call in the first place The reason is efficiency The _prologall call does automatic conversion between Prolog and HiLog which is not always necessary For instance to check whether a term f a is a member of a list f b f a one does not need to do any conversion because the answer is the same whether these terms are HiLog terms or Prolog terms Thus 11 MULTIFILE PROGRAMS 33 member f a f b f a _prolog basics is perfectly acceptable and is more efficient than member f a f b f a _prologall basics FLORA 2 provides a special primitive p2h which converts terms to and from the HiLog representation and the programmer can use it in conjunction with _prolog to achieve a greater degree of control over argument conversion This issue is further discussed in Section 12 4 11 8 Calling FLORA 2 from Prolog Since Prolog d
62. the extension xwam If there is no program file f1r file the file is assumed to contain a Prolog program and the sys tem will look for the file named program file P This file then is compiled into program file xwam and loaded Note that in this case the program is loaded into a Prolog module of FLORA 2 and therefore calls to the predicates defined in that program must use the appropriate module attri bution see Section 11 1 for the details about the module system in FLORA 2 By default all FLORA 2 programs are loaded into the module called main but you can also load programs into other modules using the following command flora2 file gt gt modulename Understanding FLORA 2 modules is very important in order to be able to take full advantage of the system we will discuss the module system of FLORA 2 in Section 11 1 Once the program is loaded you can pose queries and invoke methods for the objects defined in the program There is an important special case of the _load and command when the file name is _ underscore In that case instead of looking for the program file _ flr FLORA 2 starts reading user input At this point the user can start typing in program clauses which the system saves in a temporary file When the user is done and types the end of file character Control D Unix or Control Z Windows the file is compiled and loaded It is also possible to load such a program into a designated m
63. the system first asks the query G If the query succeeds then G is said to be satisfied Unfortunately this semantics is problematic It cannot be characterized model theoretically and in certain simple cases the procedure for testing whether G holds may send the system into an infinite loop For instance in the presence of the rule Zp p the query p will not terminate Negation as failure is the recommended kind of negation for non tabled predicates but caution needs to be exercised The well founded negation not has a model theoretic semantics and is much more satisfactory from the logical point of view Formally this semantics uses three valued models where formulas can be true false or undefined For instance if our program has the rule p not p then the truth value of p is undefined Although the details of this semantics are somewhat involved 10 it is usually not necessary to know them because this type of negation yields the results that the user normally expects The implementation of the well founded negation in XSB requires that it is applied to goals that consist entirely of tabled predicates or molecules Although FLORA 2 allows not to be applied to non tabled goals this may lead to unexpected results For instance Section 18 discusses what might happen if the negated formula is defined in terms of an update primitive For more information on the implementation of the negation operators in X
64. to any type or if it does the type would normally cover only certain portions of the object structure In this situation one might want to limit type checking only to certain methods and classes because other parts of the data might not be expected to have regular structure Note that in a multi module program the module information should be added to the various parts of the above type checker It is reasonable to assume that the schema information and the definition for the same object resides in the same module a well designed program is likely to satisfy this requirement In this case a type checker that take the module information into account can be written as follows type_error 0 M R 7D hh Values that violate typing O M gt R O M gt D 7Mod1 not R D Mod1 hh Defined methods that do not have type information O 7M gt R not 0 7M gt _D Mod1 type_error 0bj Meth Result Class We should note that type checking queries in FLORA 2 are likely to work only for pure queries 7 e ones that do not involve built ins like arithmetic expressions Built ins pose a problem because they typically expect certain variable binding patterns when these built ins are called This assumption may not hold when one asks queries as general as type_error To facilitate all these checks FLORA 2 provides a method _check in class Type of module _typecheck Its syntax is Typel _check Atom Result
65. to start tracing the program To stop tracing type _notrace During tracing the user is normally prompted at the four ports of subgoal execution Call when a subgoal is first called Exit when the call exits Redo when the subgoal is tried with a different binding on backtracking and Fail when a subgoal fails At each of the prompts the user can issue a number of commands The most common ones are listed below See the XSB manual for more e carriage return creep to go to the next step e s skip execute this subgoal non interactively prompt again when the call exits or fails e S verbose skip like s but also show the trace generated by this execution e 1 leap stop tracing and execute the remainder of the program The behavior of the debugger is controlled by the predicate debug_ctl For instance executing debug _ctl profile on at the FLORA 2 prompt tells XSB to measure the CPU time it takes to execute each call This is useful for tuning your program for performance Other useful controls are debug_ctl prompt off which causes the trace to be generated without user intervention and debug_ctl redirect foobar which redirects debugger output to the file named foobar The latter feature is usually useful only in conjunction with the aforesaid prompt off mode See the XSB manual for additional information on debugger control FLORA 2 provides a convenient shortcut that capture some of the most common uses of the afor
66. true false A synonym for this type name is http www w3 org 2001 XMLSchema boolean All constants in this type belong to the built in class _boolean The following methods are available in module _basetype Class methods e boolean toString gt _symbol e _boolean gt _isTypeOf _object Other methods e boolean _toString gt _symbol e _boolean gt _equals _object e _boolean gt _lessThan _object Note _false _lessThan _true e boolean _typeName gt _symbol e _boolean _rawValue gt _symbol Extract the short representation value from the boolean data type Caveat The long form and the short form must really be the same i e true _boolean and true must denote the same constant However this has not been implemented yet To extract the short representation part from a _boolean data type one should use the method _rawValue For instance 7 true _boolean _rawValue gt X _basetype 25 8 The Primitive Type double This corresponds to the XML Schema type double The constants in this type all belong to the class double and have the form value _double where value is a floating point number that uses the regular decimal point representation with an optional exponent Doubles have a short form where the _double wrapper is removed This type name has a synonym http www w3 org 2001 XMLSchema double The follow ing methods are available for type double
67. very hard to fix and which can corrupt its internal state irreparably One such thing is the issue of backtracking over updates especially over deletions Here we are not talking about FLORA 2 s transactional updates which are implemented prop erly and are safe Instead a problem arises when backtracking over updates happens in the XSB program into which FLORA 2 programs are compiled If this happens the program might crash or produce random unexplainable results Unfortunately it is not easy to spot such cases by just looking at a source code of your FLORA 2 program because much is done by the runtime system behind the scene Here we provide high level tips that can help avoid the problem First make sure that when you use non transactional updates and your program backtracks over them deleted facts are never queried again This situation is analogous to backtracking over updates in XSB Second FLORA 2 may backtrack over updates if your top level query calls an update directly or indirectly This may happen when the interpreter is in the all answers mode _all even if the logic of your program does not call for backtracking Indeed in the venerable Prolog way all answers can be obtained only by backtracking and this is what FLORA 2 does behind the scene In this situation transactional updates are as vulnerable as non transactional ones because after each iteration that returns an answer to the top level all updates are committed and c
68. while Alternative succeeds Note that the if statement is friendly to transactional updates in the sense that transactional updates executed as part of an if statement would be undone on backtracking unless the changes done by such updates are explicitly committed using the commit method of the system module _db see Section 29 2 22 CONTROL FLOW STATEMENTS 92 22 2 Loops unless do This construct is an abbreviation of if Cond then true else Action If Cond is true it succeeds without executing the action Otherwise it executes Action and succeeds or fails depending on whether Action succeeds or fails while do and do until These loops are similar to those in C Java and the like In while Condition do Action Condition is evaluated before each iteration If it is true Action is exe cuted This statement succeeds even if Condition fails at the very beginning The only case when this loop fails is when Condition succeeds but Action fails for all possible instantiations The loop do Action until Condition is similar except that Condition is evaluated after each iteration Thus Action is guaranteed to execute at least once These loops work by backtracking through Condition and terminate when all ways to satisfy it have been exhausted or when Action fails The loop condition should not be modified inside the loop body If it is modified e g new facts are inserted in a predicate that Condition uses XSB does not gua
69. 0 seconds Yes 11 Multifile Programs FLORA 2 supports many ways in which a program can be modularized First an F logic program 11 MULTIFILE PROGRAMS 25 can be split into many files with separate namespaces Each such file can be considered an in dependent library and the different libraries can call each other In particular the same method name or a predicate can be used in different files and the definitions will not clash Second a program file can be split of several files and these files can be included by the preprocessor prior to the compilation In this case all files share the same namespace in the sense that the different rules that define the same method name or a predicate in different files are assumed to be part of one definition Third FLORA 2 programs can call Prolog modules and vice versa In this way a large system can be built partly in Prolog and partly in FLORA 2 We discuss each of the aforesaid modularization methods in turn 11 1 FLORA 2 Modules A FLORA 2 module is a programming abstraction that allows a large program to be split into separate libraries that can be reused in multiple ways in the same program Formally a module is a pair that consists of a name and a contents The name must be an alphanumeric symbol the underscore _ is also allowed and the contents consists of the program code that is typically loaded from some file but it can also be constructed dynamically by inserting facts into
70. 000000 0b eee 28 11 4 Finding the Module That Invoked A Rule 28 11 5 Loading Files into User Modules 0 0 0 0000000 eee eee 28 11 6 Adding Rule Bases to Existing Modules 2 02000000 30 11 7 Calling Prolog from FLORA 2 2 0 0 00 a 31 11 8 Calling FLORA 2 from Prolog aoaaa ee 33 CONTENTS 11 8 1 Importing FLORA 2 Predicates into Prolog Shell 0 11 8 2 Calling FLORA 2 from a Prolog Module 11 8 3 Passing Arbitrary Queries to FLORA 2 2 2 ee ee 11 9 FLORA 2 System Modules aaa ee 11 10Including Files into FLORA 2 Programs 0 0000 ee ee eee 11 11 More on Variables as Module Specifications 0 02 2 0004 11 12Module Encapsulation ooa a 11 13Importing Modules oaa a ee 11 14Persistent Modules 0 0 200000 000 2 12 HiLog and Metaprogramming 12 1 Meta programming Meta unification 2 20 20 02 ee ee 19 2 ReimGationl lt lt Soke e Bete sie ghee Be ee Bee pe Bd a ei ee a ee 12 3 Meta decomposition ooo a 12 4 Passing Parameters between FLORA 2 and Prolog bao gaa Seb ea Be ee ae a 13 Negation 13 1 Two Operators for Negation ooa a a 13 2 True vs Undefined Formulas 0 0 0 000000 0 eee eee 13 3 Unbound Variables in
71. 1 e F molecules provide a convenient way to shortcut specifications related to the same object For instance the conjunction of the atoms John person John age gt 31 John children gt Bob Mary and John children gt John is equivalent to the following single F molecule John person age gt 31 children gt Bob Mary John Note the use of the that separates the expression for the age attribute from the expression for the children attribute This is a departure from the original F logic syntax in 8 which uses to separate such expressions e Rules are as usual the constructs of the form head body where head is an F molecule and body is a conjunction of F molecules or negated F molecules Negation is specified using or not the difference will be explained later Each rule must be terminated with a Conjunction is specified as in Prolog using the symbol Like in Prolog FLORA 2 also allows disjunction in the rule body which is denoted using As usual in logic languages a single rule of the form head Johniage gt 31 1 John children gt Bob Mary John children gt John is equivalent to the following pair of rules head John age gt 31 John children gt Bob Mary head John age gt 31 John children gt John Disjunction is also allowed inside F molecules For instance the rule 1 can be equivalently rewritten as
72. 2 X _symbol No We now describe each data type separately 25 1 FLORA 2 Symbols Before describing the actual data types we would like to remind that in Section 5 1 we introduced alphanumeric constants such as abc12 and sequences of symbols enclosed in single quotes such as aaa 2 and called them constant symbols These are not the only constants in FLORA 2 In the following subsections we will introduce typed literals that represent time URIs and more Constant symbols belong to FLORA 2 builtin class _symbol In addition this class contains IRIs which are described next An IRI of the form some string _iri is assumed to be identical to a constant symbol some string However this feature has not been fully implemented Namely a query 25 PRIMITIVE DATA TYPES 98 foobar foobar _iri fails However the methods defined for the IRIs are applicable For instance http foo bar com _scheme gt X _basetype binds X to http as is expected of URIs Such methods will give syntax error if applied to symbols that are not IRIs foo _host gt X _basetype Abort FLORA gt invalid IRI literal 25 2 The iri Data Type The canonical representation of the constants of type IRI international resource identifiers a generalization of IRIs universal resource identifiers is some iri _ iri where literal must have a lexical form corresponding to IRIs on the WWW I
73. 2 compiler the macro FLORA_USER_WORKSPACE gets replaced with the module name and out comes a unique hard to replicate triehandle Unfortunately putting something in a trie does not mean that Prolog will find it there automat ically That is if you insert p a in a trie it does not mean that the query p a will evaluate to true and this is another major source of complexity that the FLORA 2 compiler has to deal with To find out if a term exists in a trie we must use the primitive storage find fact Triehandle Term If the term exists in the trie identified by its triechandle then the predicate succeeds if the term does not exist then it fails The above primitive can be used to query tries in a more general way with the second variable unbound In this case we can backtrack through all the terms that exist in the trie Suppose we insert a fact alm gt v represented by the formula mvd a m v Since this formula is inserted in the trie and Prolog knows nothing about it we need to connect the trie to Prolog through a rule like this mvd 0 M V storage _find_fact triehandle f 0 M V Of course the name of the triehandle and the predicate names must be generated using the macros as described above so that they could be used for any module In FLORA 2 such rules are called patch rules D INSIDE FLORA 2 152 Since F logic uses only about the predicates that represent F molecules we can create such rules statically and
74. 25 9 The Primitive Type _ 25 10 The Primitive Types decimal and integer o o ooa a 25 11 The Primitive Type string ene Gad tbe yee Ble we de wee Go es Ba bee aa i 25 12 The Primitive Type list aoaaa ee 26 Debugging User Programs 26 1 Checking for Undefined Methods and Predicates 00 26 2 Type Checking 26 3 Checking Cardinality of Methods 0 0 0000 00000000004 26 4 Logical Assertions that Depend on Procedural and Non logical Features 27 Optimizations 27 1 Manual Optimizations ooa a a 27 2 Invoking the FLORA 2 Runtime Optimizer oaoa 28 Compiler Directives 29 FLORA 2 System Modules 29 1 Input and Output 2 ee 29 2 Storage Control s s sos srid aoee mod aop aim PE ane E ALE a araia pi i a E E a g 29 3 System Control ooa a a a a a a 29 4 Cardinality Constraint Checking m oe ean eee a e a ae a ae ae es a 29 5 Data Types 29 6 Reading and Compiling Input Terms oaoa a a 30 Notes on the Programming Style and Common Pitfalls iv 97 98 101 102 104 105 107 107 108 109 109 111 112 112 115 118 120 121 121 122 123 125 125 128 128 129 129 129 131 CONTENTS v
75. 8 FLORA 2 predicates and molecules can have both static and dynamic parts and no special declaration is required to 19 INSERTION AND DELETION OF RULES 85 make a predicate dynamic The same molecule or a predicate can be defined by a mixture of static or dynamic rules In this section we will first look at the syntax of creating new modules Then we will describe how to insert rules and delete rules Finally we address other related issues including tabling indexing and the cut 19 1 Creation of a New Module and Module Erasure at Run time The syntax for creating a new module is as follows newmodule modulename This creates a blank module with the given name and default semantics If a module by that name already exists an error results A module created using newmodule can be used just as any module that was created by loading a user program A dual operation to module creation is erasemodule with the following syntax erasemodule modulename 19 2 Insertion of Rules Dynamic rules can be inserted before all static rules using the primitive insertrule_a or after all static rules using the primitive insertrule_z or just insertrule Several rules can be inserted in the same command The syntax of inserting a list of rules is as follows insruleop rulelist where insruleop is either insertrule_a insertrule_z or insertrule rulelist is a comma separated list of rules The rules being inserted should not terminate
76. A 2 compilation The basic idea behind the implementation of F logic by translating it into predicate calculus is described in 8 It consists of two parts translation of F molecules into various kinds of Prolog predicates and addition of appropriate closure rules that implement the object oriented semantics of the logic Consider for instance the following complex F molecule which represents some facts about the object Mary Mary employee age gt 29 kids gt Tim Leo salary 1998 gt 100000 As described in 8 any complex F molecule can be decomposed into a conjunction of simpler F logic atomic formulas These latter atoms can be directly represented using Prolog syntax For D INSIDE FLORA 2 147 different kinds of F logic atoms we use different Prolog predicates For instance the result of translating the above F molecule might be isa Mary employee Mary employee fd Mary age 29 Mary lage gt 29 mvd Mary kids Tim Mary kids gt Tim mvd Mary kids Leo Mary kids gt Leo fd Mary salary 1998 100000 Mary salary 1998 gt 100000 The mvd predicate is used to encode methods that return values as opposed to Boolean meth ods The predicates isa ans sub encode the IS A and subclass relationships respectively We call these predicates wrapper predicates Of course FLORA 2 has much more signatures inheritable and non inheritable methods directives and all kinds of auxiliary pre
77. A 2 compiler detects that a tabled literal depends on an update statement a warning is issued because such a dependency is most likely a mistake This warning is issued also for procedural methods i e Boolean methods of the form foo when a tabled literal depends on them Moreover because non tabled HiLog predicates are regarded as having procedural side effect by default this warning is also issued when a tabled literal depends on non tabled HiLog predicates There are situations however when dependency on an update makes perfect sense For instance we might be computing a histogram of some function by computing its values at every point and then adding it to the histogram When a value f a is computed first the histogram is updated However subsequent calls to f a which might be made during the computation of other values for f should not update the histogram In this case it makes sense to make f 1 into a tabled predicate whose definition will include an update operator For this reason a compiler directive ignore_depchk is provided to exempt certain predicates and methods from such dependency checks The example below shows the usage of the ignore _depchk directive ignore_depchk ins 7 t X Y ins xX Y close _io fins X insert XxX No dependency warning is issued for this program However without the ignore_depchk direc tive three warnings would be issued saying that tabled literal t
78. F molecules in the Rule Heads Another common mistake is the inappropriate use of complex F molecules in the rule heads When using a complex molecule such as a b gt c d gt e one must always keep in mind that its meaning is alb gt c and al d gt e whether the molecule occurs in the rule head or in its body Therefore if a b gt c d gt e occurs in the head of a rule like alb gt c d gt e body then the rule can be broken up in two using the usual logical tautology X AY Z X lt Z A 2 30 NOTES ON THE PROGRAMMING STYLE AND COMMON PITFALLS 133 alb gt c body a d gt e body Forgetting this tautology sometimes causes logical mistakes For instance suppose flight is a binary relation that represents direct flights between cities Then a rule like this flightobj from gt F to gt T flight F T is likely to be a mistake if the user simply wanted to convert the relational representation into an object oriented one Indeed in the head flight is a single object and therefore both from and to will get multiple values and it will not be possible to find out by querying that object which cities have direct flights between them The easiest way to see this is through the use of the aforesaid tautology flightobj from gt F flight F T flightobj to gt T flight F T Therefore if the flight relation has the following facts flight newyork boston flight seattle to
79. FLORA 2 SYNTAX 8 led us to introduce some changes to the syntax and to some degree also to the semantics The main differences are enumerated below 1 FLORA 2 uses to separate methods in F molecules The version of the logic in 8 used In FLORA 2 represents disjunction instead It is also possible to use and instead of and or instead of 2 FLORA 2 does not use the sign to separate method names from their arguments With HiLog extensions the Q sign is redundant 3 p p is not a tautology in FLORA 2 i e is not reflexive This is because our experience showed that the non reflexive use of is amore common idiom in knowledge representation 4 In 8 types are always inheritable but values are not For instance the property a b gt c is not inheritable to the subclasses of a but the property alb gt c is In FLORA 2 the notation for types is brought in line with the notation for values In particular FLORA 2 uses gt for inheritable types and gt for non inheritable ones The original F logic in 8 used only gt and gt gt because it distinguished between functional and set valued methods and both were inheritable The semantics of gt are characterized by the following inference rules X M gt T Y X YIM gt T X M gt T Y X YIM gt T which is analogous to the behavior of gt 5 The typ
80. FULORA 2 User s Manual Version 0 95 Androcymbium Guizhen Yang Michael Kifer Hui Wan Chang Zhao SRI International 333 Ravenswood Avenue Menlo Park CA 94025 U S A Department of Computer Science State University of New York at Stony Brook Stony Brook NY 11794 4400 U S A October 2 2007 CONTENTS i Contents 1 Introduction 1 2 FLORA 2 Shell Commands 4 3 F logic and FLORA 2 by Example 7 4 Differences Between FLORA 2 Syntax and F logic Syntax 7 5 Basic FLORA 2 Syntax 8 5 1 F logic Vocabulary 2 a a 9 5 2 Symbols Strings and Comments 0 0000 eee ee eee 12 5 3 Operator ogos de ce A Ree ee We ee See AO ee fe re ee ee ee ek 14 5 4 Logical Expressions lt 4 6 saa ee we See Ra a Ee be ee oe oe O 16 5 5 Arithmetic Expressions s osa w p sore wia ponce W a ea a a bonde is p pa aa a a 16 6 Class Expressions 18 7 Path Expressions 19 8 Truth Values and Object Values 21 9 Boolean Methods 23 9 1 Boolean Signatures o oaa 23 10 Anonymous and Generated Oids 23 11 Multifile Programs 24 HFAA Mod le yere 44d ao Se ChG4 4 Hoa pee oh OK OEE a 25 11 2 Calling Methods and Predicates Defined in User Modules 26 11 3 Finding the Current Module Name 0 0 0
81. However if the argument is reified i e is an object that represents a formula see Section 12 2 as in flora2 foo goo a goomod foomod then foo 1 is assumed to be a meta predicate that receives the query goo a in module goomod as a parameter Moreover module specification propagates to any reified formula appearing in the argument of a predicate for which the module is specified For example flora2 foo goo a foomod is equivalent to flora2 foo goo a foomod foomod 11 MULTIFILE PROGRAMS 28 11 3 Finding the Current Module Name Since a FLORA 2 program can be loaded into any module the program does not have a priori knowledge of the module it will be executing in However the program can determine its module at runtime using the special token _ which is replaced with the current module name when the module is loaded More precisely if occurs anywhere as an oid method name value etc in file foo flr then when foo flr is loaded into a module say bar then all such occurrences of _ are replaced with bar For instance a b gt _ a lb gt X X main Yes 11 4 Finding the Module That Invoked A Rule Sometimes it is useful to find out which module called any particular rule at run time This can be used for example when the rule performs different services for different modules The name of the caller module can be obtained by calling the primitive caller
82. IRECTIVES 124 a fff b X fff 7Y then fff would be known to the shell but unfortunately we will not get that far to find out The compiler will issue an error since fff will not be known as an operator during the compilation of the program Summary of directives The following is a summary of all supported compiler directives setsemantics Option1 Option2 Sets the semantic options in the current user module The currently allowed options are equality none default equality basic inheritance none inheritance flogic custom none default and custom filename With equality none equality is not maintained and the symbol works like an ordinary predicate With equality basic the predicate is treated as equality setsemantics Option1 Option2 This is an executable version of the setsemantics directive setsemantics Option1 Option2 module Same as above except that equality maintenance is set for the specified user module index Arity Argument Says that all tabled HiLog predicates of arity Arity should be indexed on argument number Argument the count starts at 1 This directive should appear at the beginning of a module to have any effect Normally predicates in FLORA 2 are indexed on predicate name only The above directive changes this so that indexing is done on the given argument number instead Note that the index directive is not very useful for predicates that mos
83. LORA 2 issues warnings when it finds that a tabled predicate depends on non logical or side effectful statements but it does not warn about all Prolog predicates of this kind Therefore caution needs t be exercised in specifying purely logical statements and warnings should not be ignored If you are certain that a particular suspicious dependency is harmless use the ignore_depchk directive to suppress the warning 27 Optimizations 27 1 Manual Optimizations Left to right processing The first rule in improving the performance of FLORA 2 programs is to remember that query evaluation proceeds from left to right Therefore it is generally advisable to place subgoals with smaller answer sets as close to the left of the rule body as possible And like in databases Cartesian products should be avoided at all costs Nested molecules and path expressions FLORA 2 compiler makes decisions about where to place the various parts of complex F logic molecules and the programmer can affect this placement by writing molecules in various ways For instance X attri gt Y attr2 gt Y is translated as X attri gt Y X attr2 gt Y so the first attribute will be computed first If the second attribute has a smaller answer set the attributes in the molecule should be written in the opposite order The other consideration has to do with literals that have nested molecules in them For instance the following query
84. Negated Goals 2 0 a 14 Inheritance 14 1 Structural vs Behavioral Inheritance 00 2 2 00000008 ee 14 2 Code Inheritance 0 ee 15 Custom Module Semantics 15 1 Equality Maintenance 2 2 a 15 2 Choosing an Inheritance Semantics o oo e a 15 3 Ad Hoc Custom Semantics 0 0 0 0 000 a a a 15 4 Querying Module Semantics 2 ee 16 Cardinality Constraints 17 FLORA 2 and Tabling ii 42 44 45 47 49 50 5l 52 52 53 54 58 59 60 63 63 63 64 66 CONTENTS 17 1 Tabling ina Nutshell 2 0 0 0 0 17 2 Discarding Information Stored in Prolog Tables 17 3 Procedural Methods 20 17 3 1 Procedural Signatures 2 2 08 17 4 Operational Semantics of FLORA 2 TR RERNE eee Me Ge ee ee ee ae is 18 Updating the Knowledge Base 18 1 Non transactional Non logical Updates 18 2 Backtrackable Logical Updates 18 3 Updates and Tabling ee Bln PP Ske p i ad Oe S 18 4 Updates and Meta programming 18 5 Updates and Negation 0004 18 6 Words of Extreme Caution 0 19 Insertion and Deletio
85. ORA 2 program and is compiled as such If file flr is not found but file P or file O is the file is passed to Prolog for compilation Sometimes it is useful to know which user modules are loaded or if a particular user module is loaded say because your might want to load it if not To find out which modules are loaded at 11 MULTIFILE PROGRAMS 30 the present time use the predicate _isloaded 1 For instance the first query below succeeds if the module foo is loaded The second query succeeds and binds L to the list of all user modules that are loaded at the present time aggregate operators including collectset are discussed in Section 21 flora2 _isloaded foo flora2 L collectset X _isloaded X Inline programs In some cases primarily for testing it is convenient to be able to type up and load small programs into a running FLORA 2 session To this end the system provides spe cial idioms _ _ gt gt module _ and _ gt gt module This causes FLORA 2 to start reading program clauses from the standard input and load them into the default module or the specified module To indicate the end of the input the user can type Control D in Unix like systems or Control Z in Windows For instance flora2 _ gt gt foo aaa bbb gt ccc X foo gt Y Y X gt bar Control D A word of caution It is dangerous to place the Load command in the body of a rule if load loads a file into the same modul
86. RIs have shorthand notation _ some iri as mentioned before The full IRI name of this type is http www w3 org 2007 rif iri IRIs can come in the usual full form or in an abbreviated form known as the ciri form for compact IRI An a compact form of an IRI ciri consists of a prefix and a local name as follows PREFIX NAME LOCALNAME Here PREFIXNAME is an alphanumeric identifier that must be defined as a shortcut for an IRI elsewhere see below LOCALNAME can be a string an alphanumeric identifier or a quoted atom If LOCALNAME contains non alphanumeric symbols it must be enclosed in double quotes as in ab 20 A compact IRI is treated as a macro that expands into a full IRI by concatenating the expansion of PREFIXNAME with LOCALNAME The prefix of a compact IRI must be defined as follows iriprefix PREFIXNAME PREFIXIRI Here PREFIXIRI can be an alphanumeric identifier a quoted atom or a character list Prefixes can also be defined on command line at run time iriprefix PREFIXNAME PREFIXIRI Such a prefix becomes defined only after the command is executed If a prefix is used before it is defined an error will result For example iriprefix w3c http www w3c org AAAWEB http www AAA com Defines two prefixes which can be used in subsequent commands like this X w3ct a 25 PRIMITIVE DATA TYPES 99 This will bind X to _ http www w3c org a Likewise Y AAAWEB ab 20 bind
87. SB we refer the reader to the XSB manual Both and not can be used as operators inside and outside FLORA 2 molecules For instance flora2 not pla flora2 p a flora2 not X foo gt bar bar gt foo flora2 X not foo gt bar bar gt foo p Y are all legal queries Note that applies only to non tabled constructs such as non tabled FLORA 2 predicates and procedural methods We should warn against one pitfall however Sometimes it is necessary to apply negation to several separate literals and write something like flora2 hp a q X flora2 not p a q X flora2 not X foo gt bar X bar gt foo This is incorrect however since in this context FLORA 2 and Prolog as well will interpret not and as predicates with two arguments which are likely to be undefined The correct syntax is 13 NEGATION 52 flora2 Chip a q X flora2 not p a q X flora2 not X foo gt bar X bar gt foo i e an additional pair of parentheses is needed to indicate that the sequence of literals form a single argument 13 2 True vs Undefined Formulas The fact that the well founded semantics for negation is three valued brings up the question of what exactly does the success or failure of a call means Is undefinedness covered by a success or by failure The way this is implemented in XSB is such that a call to a literal P succeeds if and only if P is t
88. The directive import _flimport 1 from flora2 must appear near the top of test P prior to any call to FLORA 2 predicates The first form for _flimport above is used to both import the predicate and also to load the program file defining it into a given FLORA 2 user module The second syntax is used when the FLORA 2 program is already loaded into a module and we only need to import the corresponding predicate In _flimport flora predicate is the name of the imported predicate as it is known in the FLORA 2 module For non tabled predicates whose name starts with in FLORA 2 flora predicate should have the following syntax predicate name For instance to import a FLORA 2 non tabled predicate foobar of arity 3 one can use the following statement _flimport foobar 3 as foobar _ _ _ from mymodule The imported predicate must be given a name by which the imported predicate will be known in Prolog This name can be the same as the name used in FLORA 2 It is important however that the Prolog name be specified as shown i e as a predicate skeleton with the same number of arguments as in the FLORA 2 predicate For instance foo _ _ _ will do but foo 3 will not Once the predicate is imported it can be used under its Prolog name as a regular predicate Prolog programs can also load and compile FLORA 2 programs using the following queries again bootstrap_flora must be executed in advance
89. The question is what is the color of clyde clyde s color has not been defined in the above program However since clyde is an elephant and the default color for elephants is gray clyde must be gray Thus we can derive clyde color gt gray Observe that when inheritable methods are inherited from a class by its members the attribute becomes non inheritable On the other hand when such a method is inherited by a subclass from its superclass then the method is still inheritable so it can be further inherited by the members of that subclass or by its subclasses For instance if we have 14 INHERITANCE 59 circus_elephant elephant then we can derive circus_elephant color gt gray Non monotonicity of behavioral inheritance becomes apparent when certain new information gets added to the knowledge base For instance suppose we learn that royal_elephant color gt white Although we have previously established that clyde is gray this new information renders our earlier conclusion invalid Indeed Since clyde is a royal elephant it must be white while being an elephant it must be gray The conventional wisdom in object oriented languages however is that inheritance from more specific classes must take precedence Thus we must withdraw our earlier conclusion that clyde is gray and infer that he is white clyde color gt white Nonmonotonicity also arises due to multiple inheritance The following example known as the
90. _WORKSPACE and FLORA_THIS_FDB_STORAGE in includes flrheader f1lh to see what is involved D INSIDE FLORA 2 151 The updatable part of the database All objects and facts that are explicitly inserted by the program are kept in the special storage trie associated with the user module where the program is loaded A tries is a special data structure which is well suited for indexing tree structured objects like Prolog terms This workhorse does much of the grudge work in the Prolog engine To manipu late the storage tries FLORA 2 uses the XSB package called storage P which is described in the XSB manual This package was originally created to support FLORA 2 but it has independent uses as well All primitives in this package take a Prolog symbol called a triehandle a Prolog term and some also return status in the third argument Here are some of the most relevant predicates storage_insert_fact Triehandle Term Status storage_delete_fact Triehandle Term Status storage_insert_fact_bt Triehandle Term Status storage delete fact_bt Triehandle Term Status The first two methods insert and delete in a non transactional manner while the last two are transactional FLORA 2 associates a separate triehandle and thus a separate trie with each module The mechanism is similar to that used for predicate names FLORA_THIS_FDB_STORAGE FLORA_USER_WORKSPACE As explained earlier when Prolog compiles the file generated by the FLORA
91. addition restricts the checks to the methods that have type information 26 3 Checking Cardinality of Methods FLORA 2 does not automatically enforce the cardinality constraint specified in method signatures However the type system module in FLORA 2 provides methods for checking cardinality con straints for methods that have such constraints declared in their signatures In practice as well as in theory things are more complicated however First it is theoretically impossible to have a query that will flag a violation of a cardinality constrain if and only if one exists and will terminate In practice the constraint checking methods in the type system library may trigger run time errors if there are rules that use non logical features or certain builtins in their bodies Therefore in practice the user should use the constraint checking methods only for purely logical methods Cardinality constraints declared for methods that are defined with the help of non logical features should be used for documentation only The above problems aside it is easy to verify that a particular satisfies a cardinality constraint For instance if method foo is declared as someclass foo 2 3 gt sometype then to check that the cardinality constraint is not violated one can ask the following query flora2 Cardinality _check 0bj foo gt _typecheck If no violations are found the above query will fail If there are violations of this constra
92. ake one unbound call than multiple bound ones For instance suppose we have a class cl that has hundreds of members and consider the following query flora2 X cl attr gt 7Y Here Prolog would first evaluate the open call X cl and then for each answer x for X it will evaluate x attr gt Y If the cost of computing x attr gt Y is higher than the cost of x cl and the number of answers to X attr gt Y is not significantly higher than the number of answers to X cl then the following query might be evaluated much faster flora2 X attr gt Y X cl In this query a single call X attr gt Y is evaluated first and then x cl is computed for each an swer for X Since as we remarked the cost of this call can be much smaller than the combined cost of multiple calls to x attr gt Y for different x If the number of bindings for X in X attr gt Y that are not members of class cl is small the second query might take significantly less space and time 27 2 Invoking the FLORA 2 Runtime Optimizer FLORA 2 has a rudimentary runtime optimizer which can be invoked by executing the following commands flora2 _optimize 0ptimizerO0ption flora2 _optimize 0ptimizer0ption 7Module The first command invokes the optimizer with the option OptimizerOption in all modules and the second command does the same in a given module The commands flora2 _resetoptimization 0ptimizerOption flora2 _resetoptimiza
93. an no longer be backtracked over due to the aforesaid XSB bug The only more or less sure remedy in this situation is to ensure that backtracking does not occur at the top level For instance if the top level query is a procedural method then semantically it would be correct to put a cut at the end of the query X Z some_method SomeArgs something This will work if XSB is configured for the local scheduling strategy which is currently the default If it is configured for the batched strategy see XSB manual for how to do this then the above trick will work only is something does not have tabled predicates or those predicates are fully evaluated by the time something succeeds In some cases cuts can be also inserted in the rules that define procedural methods if the logic of the program ensures that backtracking over those methods should not occur 19 Insertion and Deletion of Rules FLORA 2 supports non transactional insertion of rules into modules as well as deletion of inserted rules A FLORA 2 module gets created when a program is loaded into it as described in Section 2 or it can be created using the primitive newmodule Subsequently rules can be added to an existing module Rules that are inserted via the insertrule and add commands are called dynamic and the rules loaded using the _load or commands are called static or compiled Dynamic rules can be deleted via the deleterule command As mentioned in Section 1
94. and any element in the date and the time parts can be omitted The constants of this data type all belong to the class _duration The type name has the following synonyms http www w3 org 2001 XMLSchema duration du The following classes are available in module basetype Their signatures are shown below 25 PRIMITIVE DATA TYPES 106 Class methods e _duration _toType _integer _integer _integer _integer _integer gt _duration The meaning of the arguments in that order is year month day hour minute second e duration _toType _symbol gt _duration e _duration gt _isTypeOf _object Tells if am object belongs to the primitive type duration Component methods e duration _year gt _integer e duration _month gt _integer e duration _day gt _integer e duration hour gt _integer e duration minute gt _integer e _duration _second gt _integer Other methods e duration toString gt _symbol e _duration gt _equals _object e duration gt _lessThan _object e duration _typeName gt _symbol e duration _add _duration gt _duration Examples e P5Y5M10DT11H24M22S duration e P2Y05M10DT11H24M22 _duration minute gt 24 _basetype 25 PRIMITIVE DATA TYPES 107 25 7 The Primitive Type boolean This corresponds to the XML Schema Boolean type Constants of this type have the form true _ boolean false _ boolean or the shorter form
95. and returning answers interactively The meaning of the argu ments is the same Under one at a time solution _one will wait for input return compiled code then wait for input again if the user types If the user types RET then this predicate succeeds and exits Under all solutions semantics _al1 will wait for inputs and process them but will not return answers unless the file is closed e g Ctl D at standard input 30 NOTES ON THE PROGRAMMING STYLE AND COMMON PITFALLS 131 e Source readAll Module CodeList _parse This collects all answers from a source which can be either a file or a string If the source is a string it should be specified as string Str where Str is either an atom or a variable that is bound to an atom If the source is a file then it should be specified as file FileName where FileName is an atom that specifies a file name or a variable bound to it The mean ing of Module is the same as before CodeList contains the result It is bound to a list of the form code TermCode1 Status1 code TermCode2 Status2 where TermCode is the compiled code of a term in the source and Status is the status of the compilation for this term It has the form OQutcomeFlag EOF_flag ErrorList as explained before 30 Notes on the Programming Style and Common Pitfalls Programming in FLORA 2 is similar to programming in Prolog but is more declarative For one thing F molecules are always tabled so the
96. andard input and compile it The resulting term is bound to the variable Code The term can also be a reified formula and even a reified rule Such a formula rule can be used in a query or inserted into the knowledge base as appropriate If the input term is not reified the 7Mod parameter has no effect If the formula is reified them it will be built for the module specified in Mod If Mod is unbound then the default module main is assumed Status is bound to the status code returned by the predicate and has the form OutcomeFlag EOF_flag ErrorList where OutcomeFlag null or error null a blank line was read no code generated Code null ok good code was generated no errors error parsing compilation errors EOF_flag eof not_eof not_eof end of file has not been reached eof if it has been reached ErrorList if OutcomeFlag null ok then this list would be empty if OutcomeFlag error then this would be a list of the form error N1 N2 Message where N1 N2 encode the line and character number which is largely irrelevant in this context Message is an error message Error messages are displayed Example read Code foobar Stat _parse f a lt user input Code f a Stat ok not_eof read Code foobar Stat _parse a b gt c lt user input Code a b gt c foobar Stat ok not_eof e f readAll Code Mod Stat _parse Used for reading terms one by one
97. another module The basic idea behind FLORA 2 modularization is that reusable code libraries are to be placed in separate files To use a library it must be loaded into a module Other parts of the program can then invoke this library s methods by providing the name of the module and the method predicate names of course There is no need to export anything from a library any public method or predicate can be called by other parts of the program A module can have non public methods if the module is encapsulated see Section 11 12 In this way the library loaded into a module becomes that module s content Note that there is no a priori association between files and modules Any file can be loaded into any module and one program file can even be loaded into two different modules at the same time The same module can be reused during the same program run by loading another file into that module In this case the old contents is erased and the module gets new contents from the second file In FLORA 2 modules are completely decoupled from file names A FLORA 2 program knows only the module names it needs to call but not the file names Specific files can be loaded into modules by another unrelated bootstrapping program Moreover a program can be written in such a way that it calls a method of some module without knowing that module s name The name of the module can be passed as a parameter or in some other way and the concre
98. ardinality constraints by putting variables in the appropriate places For instance consider the following knowledge base loaded into module foo 16 CARDINALITY CONSTRAINTS C m 3 gt B C m x 1 gt B x 0 v C C22 2C v2 C2 C m gt 1 2 v m gt 2 C2 m gt 1 2 3 The query CL M 7L 7H gt 7 foo will yield three solutions 7C C M m 7L 0 H 1 C v M m L 3 H C v2 7M m L 3 H 65 Note that the objects v and v2 are in the answer to the query because they inherited the cardinality constraint for non inheritable version of m from the first clause C m3 gt B ha C M L H On the other hand the query 7C M L H gt fo0 s two solutions W WBA 17 FLORA 2 AND TABLING 66 C C2 7M m 7L 3 7H Class C is in the result because the constraint is specified explicitly and C2 is in the result because it inherited the constraint from C 17 FLORA 2 and Tabling 17 1 Tabling in a Nutshell Tabling is a technique that enhances top down query evaluation with a mechanism that remembers the calls made previously in the process This technique is known to be essentially equivalent to the Magic Sets method for bottom up evaluation However tabling combined with top down evaluation has the advantage of being able to utilize highly optimized compilation techniques developed for Prolog The result is a very efficient deduct
99. are different terms However in programming it is convenient to identify these terms when they are treated as predicates Prologs often disallow the use of the foo syntax altogether The same distinction holds in HiLog foo foo and foo O are all different In terms of the HiLog to Prolog translation this means that foo is different from apply foo is different from apply apply foo However just like in Prolog we treat p as syntactic sugar for p when both occur as predicates Thus the following queries are the same flora2 p flora2 p In the following program Pp qQ pO XQ q X r r the first two queries will succeed with X bound to p or q but the last one will fail Identification of p with p does not extend to p which is distinct from both p and p not only as a term but also as a formula Thus in the following program all queries fail p q0 p00 q0 0 p p00 q0 q00 T This is allowed in sorted HiLog 3 12 HILOG AND METAPROGRAMMING 44 12 1 Meta programming Meta unification F logic together with HiLog is powerful stuff In particular it lends itself naturally to meta programming For instance it is easy to examine the methods and types defined for the various classes Here are some simple examples all unary methods defined for John John M gt 7 all unary methods that apply to John for which a signature w
100. as declared John M gt all method signatures that apply to John which are either declared explicitly or inherited John M gt 7 all method invocations defined for John John M gt However a number of meta programming primitives are still needed since they cannot be di rectly expressed in F logic Many such features are provided by the underlying Prolog system and FLORA 2 simply takes advantage of them functor X f 3 _prolog X _h455 _h456 _h457 _prolog Yes compound f X _prolog X _h472 Yes Note that these primitives are used for Prolog terms only and are described in the XSB manual These primitives have not been ported to work with HiLog terms yet Meta unification In FLORA 2 variables can be bound to both formulas and terms For in stance in X p a p a is viewed as a term and X is bound to it Likewise in X alm gt v the F molecule is evaluated to its object value which is a and then unified with X To bind variables to formulas instead FLORA 2 provides a meta unification operator This operator treats its arguments as formulas and unifies them as such For instance 7X alm gt v k gt V binds X to the F molecule alm gt v k gt V and alm gt v k gt V X M gt v k gt p unifies the two molecules by binding X to a M to m and V to p Meta unification is very useful when it is necessary to find out the module in which a particular f
101. ator and x and y stand for the arguments The symbol x in an operator expression means that the precedence level of the corresponding argument should be strictly less than that of the operator while y means that the precedence level of the corresponding argument should be less or equal than that of the operator The precedence level and the type together determine the way the operators are parsed The general rule is that precedence of a constant or a functor symbol that has not been defined as an 5 BASIC FLORA 2 SYNTAX 15 operator is zero Precedence of a Prolog term is the same as the precedence of its main functor An expression that contains several operators is parsed in such a way that the operator with the highest precedence level becomes the main functor of the parsed term the operator with the next highest precedence level becomes the main functor of one of the arguments and so on If an expression cannot be parsed according to this rule a parse error is reported It is not our goal to cover the use of operators in any detail since this information can be found in any book on Prolog Here we just give an example that illustrates the main points For example in FLORA 2 has precedence level 800 and type yfx has precedence level 700 and type yfx gt has precedence level 1100 and type xfx Therefore 8 2 3 4 is the same as 8 2 3 4 in prefix notation and a gt b gt c will generate a parsing error Any symbol can
102. ator grammar is inadequate as for example in parsing if then else 5 1 F logic Vocabulary e Symbols The F logic alphabet of object constructors consists of the sets C and V vari ables Variables are symbols that begin with a questionmark followed by a letter or an underscore and then followed by zero or more letters and or digits and or underscores e g X name _ v5 All other symbols including the constants which are 0 ary object con structors are symbols that start with a letter followed by zero or more letters and or digits and or underscores e g a John v_10 They are called constant symbols Constant sym bols can also be any string of symbols enclosed in single quotes e g AB c Later in Section 25 we introduce additional constants called typed literals In addition to the usual first order connectives and symbols there is a number of special symbols L Cr s A E E gt gt i n MP Moor Rt ete Anonymous and don t care variables Variables of the form _ or are called anonymous variable It is used whenever a unique new variable is needed In particular two different occurrences of _ or in the same clause are treated as different variables Named variables that start with an underscore e g _fo00 are called don t care variables Unlike anonymous variables two different occurrences of such a variable in the same clause refer to the same variable Neverthe
103. aught as follows Mn In this case all the catch Goal FLORA_ABORT FLORA_USER_ABORT Message _ Handler In order to be able to use the predefined constants FLORA_ABORT and FLORA_USER_ABORT the program must contain the include statement include flora_exceptions e warning Message prints a warning header then message 7Message and continues 9 Output goes to standard error stream Message can be of the form 7M1 M2 Mn e message Message Like warning 1 but does not print the warning header 7Message can be of the form 7M1 M2 Mn 29 4 Cardinality Constraint Checking This system module of FLORA 2 provides methods for testing cardinality constraints of the methods defined in the FLORA 2 knowledge base The module defines the transactional method _check in class Cardinality of module _typecheck This method is described in Section 26 3 29 5 Data Types This system module of FLORA 2 provides methods for accessing the as _dateTime _iri and so on Data types are described in Section 29 6 Reading and Compiling Input Terms components of data types such 25 Sometimes it may be necessary for an application to read and compile FLORA 2 statements from an input source To this end the _parse system library provides the following predicates and methods 29 FLORA 2 SYSTEM MODULES 130 e f read Code Mod Stat parse Read the next term from the st
104. be defined as an operator The general syntax is _op Precedence Type Name For instance _op 800 xfx foo As a notational convenience the argument Name can also be a list of operator names of the same type and precedence level for instance _op 800 yfx It is possible to have more than one operator with the same name provided they have different use e g one infix and the other postfix However the FLORA 2 built in operators are not allowed to be redefined In particular any symbol that is part of F logic syntax such as ete as well as any name that begins with flora or f1 followed by a capital letter should be considered as reserved for internal use Although this simple rule is sufficient in most cases to keep you out of trouble you should be aware of the fact that symbols such as gt 2 7 and many other parts of FLORA 2 syntax are operators Therefore there is a chance that precedence levels chosen for the user defined operators conflict with those of FLORA 2 and as a result your program might not parse If in doubt check the declarations in the file flroperator P in the FLORA 2 source code The fact that some symbols are operators can sometimes lead to surprises For instance a b c a b will be interpreted as terms a b c and a b rather than a query and a directive re
105. bstituted for any occurrence of a in any term For instance f x a f x p In FLORA 2 however the query a b g a gb 15 CUSTOM MODULE SEMANTICS 63 will fail However equal terms can be substituted for the arguments of F logic molecules and HiLog predicates For instance the queries a b alf gt c p a c b f gt c p b c will succeed 15 2 Choosing an Inheritance Semantics As mentioned earlier the setsemantics directive accepts two options inheritance none and inheritance flogic The default is flogic this type of inheritance is described in Section 14 With inheritance none behavioral inheritance is turned off in the corresponding module This can significantly improve performance in cases when inheritance is not needed Note that inheritance none does not turn off inheritance of signatures Inheritance of signa tures can be used for run time type checking and it makes no good sense to disable it Preserving inheritance of signatures does not affect the performance either 15 3 Ad Hoc Custom Semantics The setsemantics directive allows the user to include additional axioms that define the semantics of a particular module These axioms should be stored in a file and included into the module using the compiler or executable directive setsemantics custom filename However the default is custom none 4 To take advantage of this feature the user must write the axioms usi
106. ceptional situation The term thrown as an exception is then unified with the term myError Reason X that was specified in catch If the two terms do not unify e g if the error specified in catch was something like myError foo X then the exception is propagated upwards and if the user program does not catch it the exception will eventually be caught by the FLORA 2 command loop In out concrete case however the thrown term and the exception specified in catch unify and thus ZhandleException 2 is called with Reason and X bound by this unification The queries Goal and Handler in the catch primitive can be F logic molecules not just predicates However 7Error both in catch and in throw must be HiLog or Prolog terms No molecules are allowed inside these terms unless they are reified That is myError problem found alb gt c will result in a parser error but an exception of the form myError problem found a lb gt c is correct because the molecule is reified Some exceptions are thrown by FLORA 2 itself and applications might want to catch them e _ flora_undefined MethodSpec ErrMsg thrown when undefinedness checking is in effect see Section 26 1 and an attempt is made to execute an undefined method or predicate The first argument in the thrown exception is a specification of the undefined predicate or the method that caused the exception The second argument is the error messa
107. cess Ctl c Ctl c Quit Flora Process Ctl c Ctl d Restart Flora Process Ctl c Ctl s Interrupting FLORA 2 is equivalent to typing Ctl c at the FLORA 2 prompt Quitting the process stops the Prolog engine and restarting the process shuts down the old Prolog process and starts a new one with FLORA 2 shell running Indentation Flora editing mode understands some aspects of the FLORA 2 syntax which en ables it to provide correct indentation of program lines in many cases In the future flora mode will know more about the syntax which will let it provide even better support for indentation C EMACS SUPPORT 145 The most common use of FLORA 2 indentation facility is by typing the TAB key If flora mode manages to understand where the cursor is it will indent the line accordingly Another way is to put the following in your emacs startup file emacs or xemacs setq flora electric t In this case whenever you type the return key the next line will be indented automatically D INSIDE FLORA 2 146 D Inside FLORA 2 D 1 How FLoRA 2 Works As an F logic to Prolog compiler FLORA 2 first parses its source file compiles it into Prolog syntax and then outputs the resulting code For instance the command flora2 _notrace myprog compiles the program found in the file myprog flr and generates the following files myprog P myprog_main xwam and myprog fdb if myprog flr contains facts in addition to rules By default _load
108. changes made by transactional updates to facts in the user module Module Backtrackable updates to other modules are unaffected e purgedb Module deletes all facts previously inserted into the storage associated with module Module 29 3 System Control The system module sys provides primitives that affect the global behavior of the system It is accessible though the system module reference _sys or through its synonym _system e Libpath add Path adds Path to the library search path This works similarly to the PATH environment variable in that when the compiler or the loader are trying to locate a file specified by its name only without directory etc then they examine the files stored in the directories on the library search path e Libpath Zremove Path removes Path from the library search path 29 FLORA 2 SYSTEM MODULES 129 e Libpath query Path queries the library search path If Path is bound checks if the specified directory is on the library search path Otherwise binds through backtracking Path to each directory on the library search path e Tables abolish discards all tabled data in Prolog This module also provides the following amenities e abort 7Message puts Message on standard error stream and terminates the current execution Message can also be in the form M1 M2 component strings are concatenated before printing them out User aborts can be c
109. class relationships and for equality maintenance The closure axioms tie all these predicates together to implement the semantics of F logic In particular they take care of the following features e Computing the transitive closure of the subclass relationship A runtime check warns about cycles in the subclass hierarchy e Computing the closure of with respect to i e if X C C D then X D e Performing monotonic and non monotonic inheritance The predicates conflict_obj_imvd conflict_imvd immediate_sub immediate_isa are used for this purpose e Making sure that when the equality maintenance mode changes as a result of the executable instruction equality basic flogic none program clauses are not overwritten by the rules specified in FLORA 2 runtime libraries This is the reason for having the wrappers derived_mvd and inferred_mvd The former appear only in the rule heads of the clauses generated by the compiler from the clauses in the user s program while the latter appear only in the rule heads in runtime libraries D INSIDE FLORA 2 149 e Providing the infrastructure for capturing undefined methods The purpose of the d_mvd mvd dichotomy is to provide a gap into which we inject rules which are enabled only if runtime debugging is turned on using Method _mustDefine on that can capture calls to undefined methods at run time Files that implement these axioms reside in the subdirectory clo
110. crete module name before the call is made If it is a variable then X will get successively bound to the user modules where alm gt b is true However these bindings will not include _prolog _prolog module or module Dynamic module bindings can be used to implement adaptive methods which are used in many types of applications e g agent programming Consider the following example Module foo Module moo something la something else 2 2 2 2 2 2 2 2 ee alsomeservice _ Arg gt Res moo _ eae alsomeservice Module Arg gt Res niga aus ie something Module Here the method someservice in user module moo performs different operation depending on who is calling it because something can be defined differently for different callers When something else is called in module foo it invokes the method someservice on object a in mod ule moo The current module name foo is passed as a parameter with the token When someservice is executed in module moo it therefore calls the predicate something in module foo If someservice is called from a different module say bar it will invoke something defined in that module and the result might be different since something in module bar may have a different definition than in module foo An example of the use of the above idea is the pretty printing module of FLORA 2 A pretty printing method is called on an object in some user module and to do its job the pretty p
111. cro program Canonic terms Token list GPP Intermediate Code Generator preprocessor flrcompiler P E Composer flrcomposer P XSB Program 3 XSB Figure 2 The architecture of the FLORA 2 system e flrutils P Miscellaneous utility predicates for loading programs checking if files exist whether they need to be recompiled etc Additional system libraries are located in the syslib subdirectory These include the various printing utilities implementation for aggregates update primitives and some others The compiler determines which of these libraries are needed while parsing the program When a library is needed the compiler generates an include statement to include an appropriate file in the syslibinc directory For instance to include support for the avg aggregate function the compiler copies the file syslibinc flraggavg_inc flh to the output P file Since syslibinc flraggavg_inc flh contains the code to load the library syslib flraggavg P this library will be loaded together with that output file The association between the libraries and the files that need to be included to provide the appropriate functionality is implemented in the file flrlibman P which also implements the utility used to load the libraries While syslib directory contains the libraries implemented in Prolog the 1ib directory con tains libraries implemented i
112. cule or a tabled predicate then the system might crash This is an unsafe low level XSB builtin The only safe way to execute abolish_all_tables is as a separate query 18 UPDATING THE KNOWLEDGE BASE 82 Note Neither the update operators such as insert delete nor the refresh operator nor abolish_all_tables can occur under the scope of the negation operator not either directly or indirectly If they do XSB will likely crash Typically it will first issue an error message telling you that an update has been issued under the scope of the negation operator Tabled predicates that depend on update operations A related issue is that a tabled predicate or an F logic molecule might occur in the head of a rule that has an update operation in its body or it may be transitively dependent on such an update Note that this is different from the previous issue where tabled predicates did not necessarily depend on update operations but rather on other predicates that were modified by these update operations In this case the update operation will be executed the first time the tabled predicate is evalu ated Subsequent calls will return the predicate truth value from the tables without invoking the predicate definition Moreover if the update statement is non logical i e non transactional then it is hard to predict how many times it will be executed due to backtracking before it will start being ignored due to tabling If FLOR
113. d 18 UPDATING THE KNOWLEDGE BASE 74 note that the query may affect the variable binding and thus the particular instance of literals that will be inserted For instance in flora2 insert p a Mary spouse gt Smith children gt Frank flora2 insert P spouse gt S S spouse gt P the first statement inserts a particular molecule In the second case the query S spouse gt P is posed and one answer a binding for P and S is obtained If there is no such binding nothing is inserted and the statement fails Otherwise the instance of P spouse gt S is inserted for that binding and the statement succeeds The insert statement has two forms insert and insertall The difference between insert and insertall is that insert inserts only one instance of literals that satisfies the formula while insertall inserts all instances of the literals that satisfy the formula In other words query is posed first and all answers are obtained Each answer is a tuple of bindings for some or all of the variables that occur in literals To illustrate the difference between insert and insertall consider the following queries flora2 p X 7Y insert q X Y Z r Y Z flora2 p X 7Y insertall q X Y Z r Y Z In the first case if p x y and r y z are true then the fact q x y z is inserted In the second case if p x y is true then the update means the following For each z such that r y z holds insert
114. d will refuse to convert integers to floats For instance if the insert statement were as follows flora2 insert p 1 p 2 p 3 then the user would have been rewarded with the following obscure message type_error _h5356 3 2 a real number 3 It is trying to tell the user that a floating number is expected and the integer 3 will not do 24 Exception Handling FLORA 2 supports the common catch throw paradigm through the primitives catch Goal Error Handler and throw Error Here Goal can be any FLORA 2 query Error is a HiLog or Prolog term and Handler is a FLORA 2 query that will be called if an exception that unifies with Error is thrown during the execution of Goal For instance AsomeQuery Y Y value gt X X gt 0 doSomethingUseful X someQuery Y Y value gt X X lt 0 throw myError X non positive X 24 EXCEPTION HANDLING 95 pC Y catch someQuery Y myError Reason X handleException Reason X handleException Reason X format w X w n Reason X _prolog format fail The catch construct first calls the query someQuery 1 If X is positive then nothing special happens the query executes normally and catch has no effect However if X turns out to be non positive then the query throws an exception myError X non positive X where X is bound to the non positive value that was deemed by the logic of the program to be an ex
115. de if then else and a number of looping constructs 22 1 IfThen Else This is the usual conditional control flow construct supported by most programming languages For instance flora2 if foo a foo2 b then abc X cde Y else qpr X rts Y Here the system first evaluates foo a f002 b and if true evaluates abc X cde Y Other wise it evaluates qpr X rts Y Note that if then and else bind stronger than the conjunc un 6699 tion the disjunction etc This is why the parentheses are needed in the above example The abbreviated if then construct is also supported However it should be mentioned that FLORA 2 gives a different semantics to if then than Prolog does In Prolog Cond gt Action Statement fails if Cond fails and Statement is not executed If the programmer wants such a conditional succeed even if Cond fails then Cond gt Action true must be used Our experience shows however that it is the latter form that is used in most cases in Prolog programming so in FLORA 2 the conditional if Cond then Action Statement succeeds even if Cond fails and Statement is executed next To fail when Cond fails one should explicitly use else if Cond then Action else fail More precisely e if Cond then Action fails if and only if Cond succeeds but Action fails e if Cond then Action else Alternative succeeds if and only if Cond and Action both succeed or Cond fails
116. dence level 14 type 14 optimizer 122 p2h 33 49 patch rules 151 path expression 19 persistence 42 predicate base part of 73 derived part of 73 Prolog module 25 31 INDEX 159 Prolog to HiLog conversion 49 updates and tabling 79 user module 25 refresh 69 81 reification 22 reification operator 45 value inheritance 58 rule variable 9 dynamic 84 anonymous 5 static 84 don t care 5 rule deletion 84 rule insertion 84 runflora script 3 well founded semantics 57 well founded semantics for negation 50 while do 92 semantics primitive 63 while loop 92 set valued methods wrapper predicates 147 aggregation 90 setsemantics directive 59 signature in F logic 10 spying 140 static rule 84 string 13 subclass 10 symbol 12 system module 25 37 125 tabling 66 throw 94 tracing 140 transactional update 78 t_delete 78 t_deleteall 78 t_erase 78
117. dicates needed to improve efficiency FLORA 2 facts such as above are then stored in a special data structure called trie and are retrieved using patch rules which have the form fd 0bj Meth Val storage_find_fact TrieName fd Obj Meth Val where storage find fact 2is an XSB predicate that retrieves facts from tries TrieName is the storage trie that is specifically dedicated to storing facts There is one such trie per module Since programs are loaded into modules dynamically the name of the storage trie is determined at program load time Also as we shall discuss later fd mvd etc are not the actual names of the predicates used in the encoding The actual names have the module name prepended to them and thus are different for different modules Moreover since module names are determined at program load time the names of the wrapper predicates are also generated at that time from predefined templates The way FLORA 2 rules are encoded is more complex Consider the following rule Mary parent gt X Mary father gt X This is translated as follows derived_fd Mary parent X d_fd Mary father X This is done for a number of reasons The prefix derived_ is used to separate the head predicates from the body It is necessary in order to be able to implement inheritance rules correctly using the XSB well founded semantics for negation not see Section 13 The prefix used in the body of a rule d_ is introduced in
118. e XML Schema type string The constants in this class belong to type string and the type name has the synonym http www w3 org 2001 XMLSchema string The values of this class have the form value _string Alphanumeric strings that start with a letter do not need to be quoted In the full representation with the _string wrapper the 25 PRIMITIVE DATA TYPES 110 double quote symbol and the backslash must be escaped with a backslash In short representation the single quote symbol and the backslash must be escaped with a backslash The following methods are available in module _basetype Class methods e string gt _isTypeOf _object e _string _toType _symbol gt _string Instance methods e string gt _contains _string e string _concat _string gt _string e string _reverse gt _string e string _length gt _integer e _string _toUpper gt _string e string _toLower gt _string e _string gt _startsWith _string e string gt _endsWith _string e string _substring _integer integer gt _string Returns a substring of the object string where the starting and the ending position of the substring are given by the arguments of the method 1 in argument 2 means the end of the string Other methods e _string gt _equals _object e _string gt _lessThan _object e string _typeName gt _symbol 25 PRIMITIVE DATA TYPES 111 Examples e abc _string
119. e earlier versions of F logic made a distinction between functional and set valued attributes and methods The former were allowed to have only one value for any particular object and the latter could have any In FLORA 2 this dichotomy was replaced with the much more general mechanism of cardinality constraints These constraints can be specified in signature expressions which we have earlier used only to define types of attributes and methods The extended syntax is as follows C1 Meth LowerBound UpperBound gt C12 C1 Meth LowerBound UpperBound gt C12 The first signature applies to object C1 and to its noninheritable method Meth The second expres sion applies to the inheritable method Meth of C1 of the subclasses of C1 and to noninheritable method Meth of the objects that belong to class Cl Recall that inheritable methods are inherited as inheritable methods to subclasses but they become non inheritable once they are inherited to class members The lower and upper bounds in cardinality constraints can be non negative integers variables or the symbol Variables can occur in signatures in rule bodies when one wants to query the bounds of the cardinality constraints and means infinity For example c1 m 2 X gt c2 X 3 means that the method m of class c1 must have at least 2 at most 3 values Similarly c1 m 2 gt c2 means that m has at least 2 values there is no upper bound We can query the specified c
120. e good understanding of the procedural semantics of FLORA 2 Section 17 4 In this case negation applied to methods or pred icates that produce side effects through updates or I O is that of negation as failure Section 13 1 When only transactional updates are used the semantics is well defined and is provided by Transaction Logic 2 1 In particular negation is also well defined However simply negating an update A is useless in programming since it simply means to jump to a random state that is not reachable via execution of A As explained in 2 1 negation is typically useful only in conjunction with A where it acts as a constraint or with the hypothetical operator of possibility In most cases when the programmer wants to apply negation to a method that performs logical transactional updates he has Omethod in mind i e a test to verify that execution of method is not possible At present this operator is not implemented in FLORA 2 and neither nor not will not produce correct results The difference with the logical formula Omethod is that the latter does not change the current state of the underlying database while method will try to execute method If it succeeds then method will fail but the changes made by method will not be undone 19 INSERTION AND DELETION OF RULES 84 18 6 Words of Extreme Caution XSB has certain well known bugs which are
121. e inference rules for input restriction and output relaxation introduced in 8 are not implemented in FLORA 2 6 The syntax a b gt c d of F logic which states that the type returned by the attribute b is the intersection of the classes c and d is not allowed Use a b gt c d instead FLORA 2 also allows a b gt c d alb gt c d and combinations of these operators on types 7 Instead of class method gt one should use class method gt 8 Equality the predicate is implemented only partially in FLORA 2 The main limitation is that the congruence axiom for equality substitution by equals works only at the top level and the first level of nesting For deeper levels of nesting substitution by equals has not been implemented This is discussed in more detail in Section 15 1 9 Behavioral inheritance has a different and better semantics in FLORA 2 compared to 8 This is discussed in Section 14 5 Basic FLORA 2 Syntax In this section we describe the basic syntactic structures used to build FLORA 2 programs Subse quent sections describe the various advanced features that are needed to build practical applications 5 BASIC FLORA 2 SYNTAX The complete syntax is given in Appendix the syntax of FLORA precisely because LA However it should be noted that BNF cannot describe it is based on operator grammar like in Prolog mixed with context free grammars in places where oper
122. e where the rule belongs For instance if the following rule is in module bar p X foo gt gt bar then execution of such a rule is likely to crash Prolog This is because this very rule will be wiped out before it finishes execution something that XSB is not ready for FLORA 2 tries to forewarn the user about such dangerous occurrences of _load but it cannot intercept all such cases reliably 11 6 Adding Rule Bases to Existing Modules Loading a file into a module causes the knowledge base contained in that module to be erased before the new information is loaded Sometimes however it is desirable to add knowledge rules and facts contained in a file to an existing module This operation does not erase the old contents of the module For instance each of the following commands flora2 myprog gt gt foomod flora2 _add myprog gt gt foomod will add the rules and facts contained in t file myprog flr into the module foomod without erasing the old contents The following commands 11 MULTIFILE PROGRAMS 31 flora2 myprog flora2 _load myprog will do the same for module main Note that in the form loading and adding can be freely mixed For instance flora2 foo1 fo002 will first load the file foo1 flr into the default module main and then add the contents of foo2 flr to that same module Like the loading commands the addition statements first compile the files they load if neces
123. e with the problem FLORA 2 provides a primitive p2h Plg H1g which does the translation If the first argument Plg is bound the primitive binds the second argument to the Hilog representation of the term If Plg is already bound to a Hilog term then Hlg is bound to the same term without conversion Similarly if Hlg is bound to a HiLog term then Plg gets bound to the Prolog representation of that term If Hlg is bound to a non HiLog term then Plg gets bound to the same term without conversion In all these cases the call to p2h succeeds If both arguments are bound then the call succeeds if and only if e Plg is a Prolog term and H1g is its HiLog representation e Both Plg and H1g are identical Prolog terms Note that if both Plg and H1lg are bound to the same HiLog term then the predicate fails Thus if you type the following queries into the FLORA 2 shell they both succeed flora2 p2h X f a p2h X X but the following will fail 13 NEGATION 50 flora2 p2h f a X p2h X X flora2 p2h f a f a The first query succeeds because X is bound to a prolog term and by the above rules p2h X X is supposed to succeed The second query fails because X is bound to a HiLog term and again by the above rules p2h X X is supposed to fail The reason why the last query fails is less obvious In that query both occurrences of f a are HiLog terms as are all terms that appear in a FLORA 2 prog
124. ead of hilog For instance flora2 p a b foo L L hilog p foo a b For F logic molecules the head of the list has the form flogic MoleculeSymbol Module The MoleculeSymbol argument represents the type of the molecule and can be one of the following gt gt gt gt gt gt gt gt gt gt gt gt boolean tabled Boolean methods boolean inheritable tabled Boolean methods boolean procedural nontabled Boolean methods empty molecules such as a Here is a number of examples that illustrate the use of for decomposition of F logic molecules alb gt c foo flogic gt foo a b c a b gt c foo flogic gt foo a b c a b gt c foo flogic gt foo a b c a b gt c foo flogic gt foo a b c a b gt gt c foo flogic gt gt foo a b c a b gt gt c foo flogic gt gt foo a b c a b gt gt c foo flogic gt gt foo a b c a b gt gt c foo flogic gt gt foo a b c a b foo flogic foo a b a b foo flogic foo a b a bO foo flogic foo a b a foo flogic foo a a p foo flogic boolean foo p al p foo flogic boolean foo a p al p foo flogic boolean foo a p 12 HILOG AND METAPROGRAMMING 49 The operator is bi directional which means that either one or both of i
125. ed assert p a yes pCa yes retract p a p a yes 18 UPDATING THE KNOWLEDGE BASE 80 What is going on here The last positive answer is a consequence of the fact that Prolog tables remember that the fact p a is true from the evaluation of the second query So when the same query is asked after retract a stale answer is returned from the tables Similarly tabling might interact poorly with assert in the following case p b no assert p b p b no The reason for the bad answer is again that Prolog remembers that p b was false the last time it looked up this fact even though this answer has become stale after the insertion Fortunately FLORA 2 is much more update friendly than plain Prolog and in situations similar to the above it will behave correctly flora2 insert olm gt v Yes flora2 o m gt v Yes flora2 deletefo m gt v o m gt v No flora2 insert o m gt v o m gt v Yes Nevertheless there still are problems with facts that depend through rules on facts that were inserted or deleted This problem is illustrated by the following program alb gt c d e gt f d e gt f Consider the following query flora2 alb gt c Yes 18 UPDATING THE KNOWLEDGE BASE 81 Suppose that next we delete the fact d e gt f Then we will get the following counterintuitive results flora2 delete d e gt f Yes flora2
126. er inside a charlist a single quote character does not need to be escaped A double quote char acter however needs to be escaped by another double quote e g foo or by a backslash Numbers Normal FLORA 2 integers are decimals represented by a sequence of digits e g 892 12 FLORA 2 also recognizes integers in other bases 2 through 36 The base is specified by a decimal integer followed by a single quote The digit string immediately follows the single quote The letters A Z or a z are used to represent digits greater than 9 Table 2 lists a few example integers Underscore _ can be put inside any sequence of digits as delimiters It is used to partition some long numbers For instance 2 11_1111_1111 is the same as 2 1111111111 However _ 5 BASIC FLORA 2 SYNTAX 14 Integer Base decimal Value decimal 1023 10 1023 271111111111 2 1023 871777 8 1023 16 3FF 16 1023 32 vv 32 1023 Table 2 Representation of Integers cannot be the first symbol of an integer since variables can start with an underscore For example 1 2 3 represents the number 123 whereas _12_3 represents a variable named _12_3 Floating numbers normally look like 24 38 The decimal point must be preceded by an integral part even if it is 0 e g 0 3 must be entered as 0 3 but not as 3 Each floating number may also have an optional exponent It begins with a lowercase e o
127. er divide the derived part into three sub parts the dyna sub part the part that precedes all other facts in the predicate the static sub part and the dynz sub part All rules inserted using insertrule_a go into the dyna sub part all the rules in the program file go into the static sub part and all the rules inserted using insertrule_z go into the dynz sub part This works well when there are no cuts in rules inserted by insertrule_a With the cuts the program might not behave as expected For example if we have the following program p X r X r a q b insertrule_a p X q X p X we normally expect the answer to be b only However FLORA 2 will return two answers a and b This is because the cut affects only the dynamic part of p X instead of all the rules for p 1 20 Querying the Rule Base The rule base can be queried using the primitive clause The syntax of clause is as follows clause head body where head can be anything that is allowed to appear in a rule head and body can be anything that can appear in a rule body In addition explicit module specifications are allowed in the rule heads in the clause primitive Both head and body represent templates that unify with the actual rules and those rules that unify with the templates are returned The following example illustrates the use of the clause primitive Suppose we have previously inserted several rules flora2 insertrule_a tc X
128. erent modules of the user program The directory headerinc is another place where the template files are located Each of these files contains just a few include statements mostly for the files in the closure directory which if you recall contains pieces of the trailer All meaningful combinations of these pieces of the trailer are represented in the file includes flrtrailer f1lh Recall that trailers implement the closure axioms The directory p2h contains the only C program in the system It implements conversion of Prolog terms to HiLog and back Finally the pkgs directory is empty Some day it will contain add on programs such as Internet access etc REFERENCES 155 References 1 2 10 11 12 14 A J Bonner and M Kifer An overview of transaction logic Theoretical Computer Science 133 205 265 October 1994 A J Bonner and M Kifer A logic for programming database transactions In J Chomicki and G Saake editors Logics for Databases and Information Systems chapter 5 pages 117 166 Kluwer Academic Publishers March 1998 W Chen and M Kifer Sorted HiLog Sorts in higher order logic programming In Inti Conference on Database Theory number 893 in Lecture Notes in Computer Science January 1995 W Chen M Kifer and D S Warren HiLog A foundation for higher order logic programming Journal of Logic Programming 15 3 187 230 February 1993 K Clark Negation as failure Lo
129. es For instance to prettyprint all the attributes and methods of an object the following method defined in the system module pp can be used flora2 obj pp_self _pp Here the method pp_self is applied to the object obj and will pretty print the state of that object For more details on the existing FLORA 2 system modules see Section 29 11 10 Including Files into FLORA 2 Programs The last and the simplest way to construct multi file FLORA 2 programs is by using the include preprocessing directive For instance if file foo flr contains the following instructions include filei include file2 include file3 the effect is the same as if the above three files were concatenated together and stored in foo flr Note however that when compiling foo flr the compiler has no way of knowing if any of the included files have changed because file inclusion is done by the preprocessor So it is recommended to compile such multi file programs using a Makefile like in C and C Note that the include instruction requires that the file name is enclosed in double quotes Also under Windows backslashes in file names must be doubled For instance include foo bar flr 11 MULTIFILE PROGRAMS 38 11 11 More on Variables as Module Specifications Earlier we mentioned that a user module specification can be a variable e g alm gt b X which ranges over module names This variable does not need to be bound to a con
130. es apply e If the idiom X module is used X can be bound only to a tabled predicate a tabled molecular formula or a Hilog term not a predicate Otherwise an error is issued If X is already bound to a tabled predicate or molecular formula then the explicit module specification module is discarded When X is bound to a HiLog term e g p a Z X module represents the tabled predicate p a Z module e If the idiom 4 X module is used X can be bound to only a non tabled predicate a non tabled molecular formula or a Hilog term If X is already bound to a non tabled predicate or molecular formula the explicit module specification is discarded as before If X is bound to a HiLog term then X module represents the non tabled predicate p a 7Z module Due to these rules the first query below succeeds while the second fails and the third causes an error 17 FLORA 2 AND TABLING 68 flora2 X p a X 7M p a X O N p a foo flora2 X p a X 7M p a foo flora2 X pla P X M Z p a foo The first query succeeds because X is bound to the term p a which X M promotes to a non tabled predicate with yet to be determined module The meta unification that follows then binds M to main Similarly X N promotes the term p a to a tabled predicate with a yet to be determined module and meta unification binds N to foo The second query fails because X is already bound to a tabled
131. esaid debug_ct1 interface Executing flora2 _trace filename will switch FLORA 2 to non interactive trace mode and the entire trace will be dumped to file filename Note that you have to execute notrace or exit Prolog in order for the entire file to be flushed on disk Low level tracing FLORA 2 debugger also supports low level tracing via the shell command _tracelow With normal tracing the debugger converts low level subgoals to subgoals that are found in the user program and are thus meaningful to the programmer With low level tracing the debugger displays the actual Prolog subgoals of the compiled P program that are being executed This facility is useful for debugging FLORA 2 runtime libraries B THE FLORA 2 DEBUGGER 142 As with trace FLORA 2 provides a convenient shortcut that allows the entire execution trace to be dumped into a file flora2 _tracelow filename C EMACS SUPPORT 143 C Emacs Support Editing and debugging FLORA 2 programs can be greatly simplified with the help of flora mode a special Emacs editing mode designed specifically for FLORA 2 programs Flora mode provides support for syntactic highlighting automatic indentation and the ability to run FLORA 2 programs right out of the Emacs buffer C 1 Installation To install flora mode you must perform the following steps Put the file flora2 emacs flora el found in your FLORA 2 distribution on the load path of Emacs or XEmacs whichever you a
132. eth the method whose value inheritance will simulate the inheritance of code of foo and bar ci dispatch meth gt bar c2 dispatch meth gt foo Clearly the object bb will inherit dispatch meth gt foo from c2 while the object aa will inherit dispatch meth gt bar from c1 inheritance from c2 is overwritten Next we define how methods are to be invoked in a way that resembles code inheritance X M Y gt Z X dispatch M gt RealMeth X RealMeth 7Y gt Z When M is bound to a particular method say meth and this method is invoked in the context of a class instance X the invocation X meth Y gt Z first computes the value of the attribute dispatch meth which gives the name of the actual method to be invoked The value of the dispatch meth attribute represented by the variable 7RealMeth is obtained by value inheri tance As explained above this value is foo when X is bound to bb and bar when X aa Finally the real method whose name is obtained by value inheritance is invoked in the context of the class instance X One can easily verify the following results flora2 aalmeth 4 gt Z 7Z 8 flora2 bb meth 4 gt Z 7Z 9 This is exactly what would have happened in Java if aa inherited the instance method whose code is equivalent to the definition of bar 1 and if bb inherited the code of foo 1 15 Custom Module Semantics FLORA 2 enables the user to choose the appro
133. ethods Such a method would be either declared as inheritable in a class object or it would be inherited by a subclass of that class Whenever an evidence of type violation is required as an answer the corresponding component of the signature should be specified as an unbound variable For instance e Cardinality check 0bject Method gt 7 typecheck Checks cardinality constraints for Method of type gt in the current module e Cardinality _check 0bj Method gt Module _typecheck Checks cardinality constraints for 7Method of type gt in module Module If Module is unbound and a cardinality constraint violation is detected in some module then Module is bound to that module e Cardinality _check 0bj Method LoBound HiBound gt Mod _typecheck Like the previous query but the variables 7LoBound and HiBound which must be unbound 26 DEBUGGING USER PROGRAMS 120 variables can be used to indicate which bounds are violated If the lower bound is violated then LoBound will be bound to the violated lower bound otherwise it is bound to ok If the higher bound is violated then 7HiBound is bound to the violated higher bound otherwise it is bound to ok If Mod is unbound then it will be bound to the module s in which the cardinality constraint is violated For instance for the above program the query Cardinality _check 0 foo Low High gt 7 Module _typecheck will bind 70 to c Mod to
134. evaluated Unfortunately the default notion of similarity used by XSB is fairly weak and many unnecessary recomputations might result Recently a new technique called subsumptive tabling has been implemented in XSB It is known that subsumptive tabling can speed up certain queries by an order of magnitude A future version of FLORA 2 might take advantage of this technique 17 2 Discarding Information Stored in Prolog Tables When Prolog and FLORA 2 evaluate a program all tabled predicates are partially materialized and all the computed tuples are stored in Prolog tables Thus if you change the underlying set of facts via insert and delete operations the existing tables must be discarded in order to allow Prolog to recompute the results We discuss updates and the problems caused by tabling Section 18 There are two ways to discard tabled information in FLORA 2 One and the safest way is to use the operator refresh Inside the braces you list the calls for which you want to discard table information For instance flora2 refresh p a X X meth _ gt b will discard any tabling information that is related to p a X and X meth _ gt b To affect the tables in another module attach the module name to the corresponding literals For instance flora2 refresh p a X X meth _ gt b foo Sometimes it may be desirable to discard all table information in the current run of the program This can be done by is
135. evels of Abstraction authors gt Omes Oeba at conf gt 0y76 oi11 journal _vollof gt o number gt 1 volume gt 1 year gt 1975 Ois journal name gt Information Systems editors gt 0m Oy76 conf_proclof gt vldb year gt 1976 editors gt 0pcl Oejn Ovid conf_series name gt Very Large Databases Omes person name gt Michael E Senko Omj person name gt Matthias Jarke affil 1976 gt opwe Orwt institution name gt RWTH_Aachen Figure 1 A Publications Object Base and its Schema in FLORA 2 5 2 Symbols Strings and Comments Symbols FLORA 2 symbols that are used for the names of constants predicates and object constructors begin with a letter followed by zero or more letters A Z a z digits 0 9 or underscores _ e g student apple pie Symbols can also be any sequence of characters enclosed in a pair of single quotes e g JOHN SMITH default flr Internally FLORA 2 symbols are represented as Prolog eyb which are used there as names of predicates and function symbols All FLORA 2 symbols belong to the class symbol FLORA 2 also recognizes escaped characters inside single quotes An escaped character normally begins with a backslash Table I lists the special escaped character strings and their 3 Symbols are called atoms in Prolog which contravenes the use of this term fo
136. ewed as formulas Thus P authors is illegal and will cause a compiler error To use a path expression as a query square brackets must be attached For instance the following are legal queries P authors P authors name gt N As path expressions and F molecules can be arbitrarily nested this leads to a concise and flexible specification language for object properties as illustrated in the following example Example 7 1 Path Expressions Consider again the schema given in Figure 1 If n is the name of a person the following path expression is a query that returns all editors of conferences in which n had a paper flora2 P authors gt name gt n at_conf editors Likewise the answer to the query flora2 P authors gt name gt n at_conf editors gt E is the set of all pairs P E such that P is the logical oid of a paper written by n and E is the corresponding proceedings editor If we also want to see the affiliations of the above editors we only need to modify our query slightly flora2 P authors gt name gt n at conf year gt Y editors affil Y gt 7A Thus FLORA 2 path expressions support navigation along the method application dimension using the operator In addition intermediate objects through which such navigation takes place can be selected by specifying the properties of such objects inside square brackets To access i
137. f HiLog and logical updates in the style of Transaction Logic FLORA 2 was designed with extensibility and flexibility in mind and it provides strong support for modular software design through its unique feature of dynamic modules Other extensions such as the versatile syntax of FLORID path expressions are borrowed from FLORID a C based F logic system developed at Freiburg Universitylil Extensions aside the syntax of FLORA 2 differs in many important ways from FLORID from the original ver sion of F logic as described in 8 and from an earlier implementation of FLORA These syntactic changes were needed in order to bring the syntax of FLORA 2 closer to that of Prolog and make it possible to include simple Prolog programs into FLORA 2 programs without choking the compiler Other syntactic deviations from the original F logic syntax are a direct consequence of the added support for HiLog which obviates the need for the sign in method invocations this sign is now used to denote calls to FLORA 2 modules FLORA 2 is available on FLORA 2 s Web site at http flora sourceforge net Installing FLORA 2 in UNIX To install the latest release of FLORA 2 or its current develop ment version download it from http flora sourceforge net into a separate directory outside the XSB installation tree After unpacking or checking out from CVS the FLORA 2 sources will be placed in the flora2 subdirectory of the current directory To config
138. f the FLORA 2 compiler 5 See if the resulting program runs under plain XSB system without the FLORA 2 shell If it does not it means that the program contains calls to FLORA 2 runtime libraries Try to eliminate such calls One common library call is used to collect all query answers in a list and then print them out You can get rid of this library call by finding the predicate fllibprogramans 2 in 32 BUGS IN PROLOG AND FLORA 2 HOW TO REPORT 136 the compiled P program and removing it while preserving the subgoal the first argument and renaming the variables as indicated by the second argument Make sure the resulting program is still syntactically correct Other calls that are often no longer needed in the dumped code are those that load FLORA 2 runtime libraries which we are trying to eliminate These calls have the form flora_load_library If there are other calls to FLORA 2 runtime libraries try to delete them but make sure that the bug is still reproducible 6 If the program still does not run because of the hard to get rid of calls to FLORA 2 runtime libraries then see if it runs after you execute the command bootstrap_flora in the Prolog shell If the program runs after this and reproduces the bug it is better than nothing If it does not then something went wrong during the above process start anew 7 Try to reduce the size of the resulting program as much as possible 8 Tell the XSB
139. g unbeknownst to the programmer Therefore if a tabled predicate depends on a transactional update backtracking will happen invisibly and the updates will be undone Therefore in such situations transactional updates will have no effect e As before t_insertall t_deleteall and t_eraseall primitives always succeed and leave the free variables unbound Likewise in the all answers mode the primitives t_insert t_delete and t_erase behave similarly to the bt all versions in other respects i e they will insert or delete facts for every answer to the associated query This can be prevented with the use of the cut or the _one directive Unimplemented In the current release arithmetic expressions in the query part of an update must have their variables be bound by the subgoals that precede the update primitive except that the literal part does not currently bind For instance flora2 delete X salary gt Y Y lt 20000 is going to cause a run time error This limitation will be removed in a future release 18 3 Updates and Tabling Changing tabled predicates or predicates on which tabled predicates depend We have earlier remarked in Section 17 3 that tabling and database updates do not mix well One problem is that the results from previous queries are stored in Prolog tables and database updates do not modify those tables Thus in Prolog the user might get the following counterintuitive result if the predicate p 1 is tabl
140. ge e flora_abort or _ flora_abort Message thrown when FLORA 2 encounters other kinds of errors This exception comes in two flavors with an error message and without A user program can also throw this exception when immediate exit to the top level is required The safest way to do so is by calling abort Message _sys as explained in Section 29 3 These exceptions are defined by FLORA 2 under the symbolic names FLORA_UNDEFINED_EXCEPTION and FLORA_ABORT When a user application needs to catch these errors we recommend to include the file flora_exceptions f1lh in the program and use the above symbolic names For instance include flora_exceptions flh catch myQuery 7Y FLORA_ABORT FLORA_UNDEFINED_EXCEPTION MethSpec Message _ myHandler 7MethSpec catch yourQuery Y FLORA_ABORT Message _ yourHandler 7Message The catch primitive can also catch exceptions thrown by the underlying Prolog system For this to happen you need to know the format i e the exact terms of the exceptions thrown 25 PRIMITIVE DATA TYPES 96 by Prolog which can be found in the manual One exception that some sophisticated FLORA 2 program might need to catch is thrown by Prolog when a FLORA 2 application calls an undefined Prolog predicate The format of the corresponding exception term is error undef ined_predicate PredName Arity Module Message _ where Module is the name
141. gic and Databases pages 293 322 1978 J Frohn G Lausen and H Uphoff Access to objects by path expressions and rules In VLDB pages 273 284 1994 M Kifer W Kim and Y Sagiv Querying object oriented databases In Proceedings of the ACM SIGMOD International Conference on the Management of Data pages 393 402 New York June 1992 ACM M Kifer G Lausen and J Wu Logical foundations of object oriented and frame based languages Journal of the ACM 42 741 843 July 1995 A Van Gelder The alternating fixpoint of logic programs with negation In ACM Principles of Database Systems pages 1 10 New York 1989 ACM A Van Gelder K A Ross and J S Schlipf The well founded semantics for general logic programs Journal of the ACM 38 3 620 650 1991 G Yang and M Kifer Implementing an efficient DOOD system using a tabling logic engine In First International Conference on Computational Logic DOOD 2000 Stream July 2000 G Yang and M Kifer On the semantics of anonymous identity and reification In Intl Con ference on Ontologies DataBases and Applications of Semantics for Large Scale Information Systems ODBASE October 2002 G Yang and M Kifer Well founded optimism Inheritance in frame based knowledge bases In Intl Conference on Ontologies DataBases and Applications of Semantics for Large Scale Information Systems ODBASE October 2002 G Yang and M Kifer Inheritance and rules in object oriented semant
142. gt _integer e dateTime _hour gt _integer e dateTime minute gt _integer e dateTime _second gt _integer e dateTime _zoneSign gt integer e dateTime _zoneHour gt _integer e dateTime _zoneMinute gt _integer 25 PRIMITIVE DATA TYPES 102 Other methods e dateTime _toString gt _symbol e dateTime gt _equals _object e dateTime gt _lessThan _object e dateTime _typeName gt _symbol e dateTime _add _duration gt _dateTime Examples e 2001 11 23T12 33 55 123 02 30 _dateTime e 2001 11 23T12 33 55 123 02 30 http www w3 org 2001 XMLSchema dateTime e 2001 11 23 _dateTime e 0237 11 23T12 33 55 _dateTime Note that this date refers to year 238 BCE e 2001 11 23 _dateTime _day gt 23 _basetype e 2001 11 23 _dateTime _toString gt 2001 11 23T00 00 00 00 00 _basetype e 2001 11 23T18 33 44 02 30 _dateTime add P22Y2M10DT1H2M3S _ duration gt 1979 09 13T17 31 41 02 30 _dateTime _basetype 25 4 The Primitive Type date This type corresponds to the XML Schema date type Constants of this type have the form ZYYYY MM DDSHH MM _date The symbols and are part of the syntax The symbol S represents the timezone sign or The timezone part beginning with S is optional The leftmost Z is the optional sign Note that unlike dateTime which represents a single time point date represents duration of a single day
143. gt _integer e time _second gt _integer e time _zoneSign gt _integer e time _zoneHour gt _integer e time _zoneMinute gt _integer 25 PRIMITIVE DATA TYPES 105 Other methods e time _toString gt _symbol e time gt _equals _object e time gt _lessThan _object e time _typeName gt _symbol e time _add _duration gt _time Examples e 11 24 22 time e 11 24 22 http www w3 org 2001 XMLSchema time e time _toType 12 44 55 gt 12 44 55 time _basetype e 12 44 55 time minute gt 44 basetype e 12 44 55 time toString gt 12 44 55 basetype e 12 44 55 _time _add PT2M3S _duration gt 12 46 58 _time _basetype Note that when adding duration to time the date part of the duration constant must not be present 25 6 The Primitive Type duration The primitive type duration corresponds to the XML Schema duration data type The constants that belong to this type have the form sPnYnMnDTnHnMnS _ duration Here s is optional sign Pindicates that this is a duration data type and Y M D H M S denote year month date hour minutes and seconds T separates date from time The symbols P Y M D H M and S are part of the syntax The symbol n stands for any positive number for instance the number of hours can be more than 12 and the number of minutes and seconds can exceed 60 The part that starts with T is optional
144. he above statement will not do anything useful due to certain idiosyncrasies in the XSB module system In this case we have to tell the system that foo 3 was defined in Prolog module bar Thus f00 3 was defined as a dynamic predicate in the module bar we have to write flora2 assert foo a b c _prolog bar _prolog Note that if we want to assert a more complex fact such as foo f a b c we would have to use either flora2 assert foo f a _prolog bar b c _prolog bar _prolog or _prologall 18 UPDATING THE KNOWLEDGE BASE 76 flora2 assert foo f a b c prologall bar prolog We should also mention one important difference between insertion of facts in FLORA 2 and Prolog Prolog treats facts as members of a list so duplicates are allowed and the order matters In contrast FLORA 2 treats the database as a set of facts with no duplicates Thus insertion of a fact that is already in the database has no effect Deletion The syntax of a deletion primitive is as follows delop literals query where delop can be delete deleteall erase and eraseall The literals part is a comma separated list of F molecules and predicates The optional part query represents an additional constraint or a restricted quantifier similarly to the one used in the insertion primitive For instance the following predicate flora2 deleteall John Year Semester gt Course Year lt 2000 will delete John s cour
145. he query is successful this variable is bound to normal Otherwise this variable will contain an exception term returned by XSB see the XSB manual if you need to process exceptions in sophisticated ways In order to use the the flora_query 4 predicate from within Prolog the following steps are necessary 11 MULTIFILE PROGRAMS 36 1 If FLORA 2 is installed as a standalone application rather than an XSB package then the FLORA 2 installation directory must be added to the XSB search path asserta library_directory home myHomeDir flora2 2 The query flora2 bootstrap_flora must be executed before compiling or loading the Prolog file 3 flora_query 4 must be imported from flora2 Here is an example of a Prolog file test P which loads and then queries a FLORA 2 file flrtest flr import bootstrap_flora O from flora2 asserta library_directory home myHomeDir flora2 flora2 bootstrap_flora import flora_query 4 from flora2 import _load 1 from flora2 _load flrtest Str X b gt Y flora_query Str X YYY Y PPP _Status Exception After the query to flrtest flr is successfully executed the bindings for the variable X in the FLORA 2 query will be returned in the Prolog variable YYY The binding for Y in the query will be returned in the Prolog variable PPP If there are several answers you can get them all in a failure loop as usual in Prolog For
146. home directory in the subdirectory xsb flora As mentioned earlier FLORA 2 uses different names for the wrapper predicates that appear in the rule heads in user programs and those that appear in the rule heads in trailers This makes it possible to load the trailers by executing the equality instruction at any time without overriding the user program The above is a much simplified picture of the inner workings of FLORA 2 The actual translation into Prolog and the form of the closure rules is very complex Some of this complexity exists to ensure good performance Other complications come from the need to provide a module system and integrate it with the underlying Prolog engine The module system serves two purposes First it promotes modular design for FLORA 2 programs making it possible to split the code into separate files and import objects defined in other modules Second it allows FLORA 2 programs to communicate with Prolog by using the predicates defined in Prolog programs and letting Prolog programs use FLORA 2 objects Some of these implementation issues are described in 11 The module system The module system is implemented by providing separate namespaces for the various predicates used to encode F logic formulas First all predicates have a weird prefix to make clashes with other Prolog programs unlikely The prefix is defined in includes header flh and currently is _ _ _flora The user of course does not need to worry abo
147. ic web languages In Rules and Rule Markup Languages for the Semantic Web RuleMLO03 number 2876 in Springer Verlag October 2003 Index meta unification operator flimport operator 33 file 30 45 reification operator check method class Cardinality module _typecheck class Type module _typecheck ol 4 not ol _add 30 check method class Cardinality module _typecheck class Type module _typecheck dump 1 mustDefine 1 in class Method module _system 112 _mustDefine 2 in class Method module _system 112 _notrace 141 _optimize 122 _resetoptimization _trace 141 _tracelow 28 gt gt gt gt gt gt 90 nobanner noprompt quietload gt gt 90 file 5 _add 5 _compile 28 _isloaded 1 _load 29 caller 122 141 90 90 134 134 134 29 29 28 compile operator 34 isloaded 1 predicate 35 _load operator 34 bootstrap_flora command 33 118 118 meta decomposition operator 116
148. in module _basetype 25 PRIMITIVE DATA TYPES 108 Class methods e double _toType _decimal gt _double Converts decimals to doubles Error if overflow e double _toType _long gt _double Converts long integers to doubles e _double gt _isTypeOf _object Instance methods e double floor gt _integer e double _ceiling gt _integer e double _round gt _integer Other methods e _double _toString gt _symbol e double gt _equals _object e _double gt _lessThan _object e double _typeName gt _symbol e double _rawValue gt _number Extract the number part of the double data type Examples 2 50 double 2 50 25E 1 Caveat The long form and the short form must really be the same i e 2 50 _double and 2 50 must denote the same constant However this has not been implemented yet In fact it is not even possible to do arithmetics with the long representation of doubles To extract the number part from a _double data type one should use the method _rawValue For instance 1 2 double _rawValue gt X _basetype 25 9 The Primitive Type long This data type corresponds to XML Schema s long integers The constants in this data type belong to class Long and have the form value _long where value is an integer in its regular representation in the decimal system A shorter form without the _long wrapper is also allowed This type name has a synonym
149. int then 0bj will get bound to the objects for which the violation was detected For instance consider the following knowledge base 26 DEBUGGING USER PROGRAMS 119 cl foo 2 3 gt int Creel ol c o2 c O3 c ol foo gt 1 2 3 4 o03 foo gt 3 4 c foo gt 2 cl foo gt 3 4 5 Then the query Cardinality _check 0 foo gt 7 _typecheck will return 70 o1 and 70 o2 because o1 has a non inheritable method foo with four values while at most 3 are allowed according to the signature The object o2 is returned because foo has no values for that object while at east 2 are required The object 03 is not returned because it does not violate the constraint Similarly the query Cardinality _check 0 foo gt _typecheck will return 0 c because the inheritable version of method foo has only 1 value for that class while at least two are required by the signature The class cl is not returned because it does not violate the constraint In general the allowed forms of the method check in class Cardinality are as follows The argument is always a signature atom no need to specify reification The method type of the signature can be either gt or gt The gt version checks non inheritable methods Such a method would normally be declared as a class property for a particular class or it would be inherited by an object from its superclass The gt version checks inheritable m
150. itly into the database and the facts that you specified in the program For instance in p a alm gt b the fact p a will be placed in the base part of the predicate p 1 tt and it can be deleted by the delete primitive Likewise the fact a m gt b is updatable If you do not want some facts to be updatable use the following syntax p a true a m gt b true FLORA 2 updates can be non transactional as in Prolog or transactional as in Transaction Logic 2 1 We first describe non transactional updates 18 1 Non transactional Non logical Updates The effects of non transactional updates persist even if a subsequent failure causes the system to backtrack FLORA 2 supports the following non transactional update primitives insert insertall delete deleteall erase eraseall These primitives use special syntax the curly braces and are not predicates Thus it is allowed to have a user defined predicate such as insert Insertion The syntax of an insertion is as follows note the s insop literals query where insop stands for either insert or insertall The literals part represents a comma separated list of literals which can include predicates and F molecules The optional part query is an additional condition that must be satisfied in order for literals to be inserted or deleted depending on what insop is The semantics is that query is posed first and if it is satisfied literals is inserte
151. ive engine XSB lets the user specify which predicates must be tabled The FLORA 2 compiler automati cally tables F molecules and HiLog predicates If the user wants to use a non tabled predicate she must use a predicate name that begins with the sign For instance in the following rules tc 2 is tabled but Zedge 2 is not tabled tc X Y Z edge X Y tc X Y hedge X 7Y tc Y Z A predicate with the prefix is logically unrelated to the predicate without the prefix Thus p a b being true does not imply anything about p a b and vice versa Identifiers and variables that are prefixed with the sign can appear only as predicate for mulas predicate names or Boolean method names However a variable prefixed with can not be a stand alone formula unless it is associated with a module specification The following occurrences of are legal insert p a 7 X is a variable ranging over non tabled predicate names a b c a Y hb and Y are procedural Boolean methods LPX M p a hp a non tabled predicate but the following are not pha fa appears as a term not formula X ha fa appears as a term not formula 17 FLORA 2 AND TABLING 67 ZeX a X appears as a term not formula al b c gt d b is not a Boolean method h X hpla 4 X as a stand alone formula The first formula is illegal because a occ
152. le time and this distinction is very important Executable directives are treated as queries and they begin with Compile time directives begin with Executable directives are mostly used to control how the FLORA 2 shell interprets the expres sions that the user types in These directives have no effect during the compilation of program files Instead when they are executed as queries they affect the shell In contrast compile time directives affect the compilation of the files they occur in Also if a module is loaded into the main module in the shell then all compile time directives in that module are executed in the shell as well so there is no need to explicitly execute these directives in the shell FLORA 2 requires that all compile time directives appear at the top of the program prior to the first appearance of a rule or a fact because such a directive has effect only after it is found and processed To better understand the issue consider the following simple program say in file test flr _op 400 xfx fff a fff b X fff Y If you load this program then it will execute correctly and return the bindings a and b for X and Y respectively If you execute the same query X fff Y in the FLORA 2 shell the result will still be correct because FLORA 2 made sure that the directive _op 400 xfx fff in test flr was executed in the shell as well On the other hand if the program was _op 400 xfx fff 28 COMPILER D
153. les appearing in query but not in X or Gs are considered to be existentially quantified Furthermore the syntax of an aggregate must satisfy the following conditions 1 All names of variables in both X and Gs must appear in query 2 Gs should not contain X Aggregates are evaluated as follows First the query condition specified in query is evaluated to obtain all the bindings for the template of the form lt X Gs gt Then these tuples are grouped according to each distinct binding for lt Gs gt Finally for each group the aggregate operator is applied to the list of bindings for the aggregate variable X The following aggregate operators are supported in FLORA 2 min max count sum avg collectset and collectbag The operators min and max can apply to any list of terms The order among terms is defined by the Prolog operator lt In contrast the operators sum and avg can take numbers only If the aggregate variable is instantiated to something other than a number sum and avg will discard it and generate a runtime warning message For each group the operator collectbag collects all the bindings of the aggregation variable into a list The operator collectset works similarly to collectbag except that all the duplicates are removed from the result list The aggregates min max sum count and avg fail if query fails In contrast collectbag and collectset succeed even if query returns no binding In this case these
154. less don t variables have special status when it comes to error checking and returning answers The practice of logic programming shows that a singleton occurrence of a variable in a clause is often a mistake due to misspelling Therefore FLORA 2 issues a warning when it finds that some variable is mentioned only once in a clause If such an occurrence is truly intended it must be replaced by an anonymous variable or a don t care variable to avoid the warning message from FLORA 2 Also bindings for anonymous and don t care variables are not returned as answers Id Terms Oids Instead of the regular first order terms used in Prolog FLORA 2 uses HiLog terms HiLog terms 4 generalize first order terms by allowing variables in the position of function symbols and even other terms can serve as functors For instance p a X b is a legal HiLog term Formally a HiLog term is a constant a variable or an expression of the form t t1 tn where t t1 tn is a HiLog term HiLog terms over C and V are called Id terms and are used to name objects methods and classes Ground Id terms i e terms with no variables correspond to logical object identifiers oids also called object names Numbers including integers and floats can also be used as Id terms but such use might be confusing and is not recommended Atomic formulas Let 0 M Ri Xi C D T be Id terms In addition to the usual first order atomic formulas like p X1
155. let gpp wrap them with the appropriate prefixes on the fly The problem arises with predicates since although they are represented using HiLog encoding using a single predicate this predicate can have any arity At present we statically create patch rules for such predicates up to a certain large arity The static patch rules are located in genincludes flrpatch fli from which flrpatch flh is generated by the FLORA 2 installation script For compiled programs the patch rules are included into the compiled code by the FLORA 2 compiler For the FLORA 2 shell however these rules are loaded when the corresponding shell module is created either the default main module or any module that was created by the newmodule command This patch file is loaded exactly once per shell module and is kept in the file xsb flora patch P in the user s home directory D 2 System Architecture The overall architecture of FLORA 2 is depicted in Figure 2 The program is first tokenized and then the composer combines the disparate tokens into terms Since due to the existence of operators not everything looks like a term in the source program the composer consults the operator definitions in the file flroperator P to get the directives on how to turn the operator expressions into terms Next the parser checks the syntax of the rules and of the various other primitives e g the aggregates updates module specifications etc The output of the parser is a
156. ll generate a parsing error John father gt Smith foomod foo X foomod goo X because defining a literal that belongs to another module does not make sense 6 In fact any symbol is allowed However it cannot contain the quote symbol 11 MULTIFILE PROGRAMS 27 2 Module specification is distributive over logical connectives including the conjunction opera ow tor the disjunction and the negation operators and not For example the formula below John father gt Smith not Smith spouse gt Mary foomod is equivalent to the following formula John father gt Smith foomod not Smith spouse gt Mary foomod 3 Module specifications can be nested The one closest to a literal takes effect For example foo a goo b goomod hoo c foomod is equivalent to foo a foomod goo b goomod hoo c foomod 4 The module specification propagates to any F molecule appearing in the argument of a pred icate for which the module is specified For example foo a b c gt d foomod is equivalent to a lb gt X foomod X c gt d foomod foo X foomod 5 Module specifications do not affect function terms that are not predicates or method names unless such a specification is explicitly attached to such a term For instance in flora2 foo goo a foomod goo 1 refers to the same functor both in module foomod and in the calling module
157. log predicates i e 11 MULTIFILE PROGRAMS 32 flora2 M usermod foo X _prolog M will cause a compilation error Some Prolog predicates are considered well known and even though they are defined in vari ous Prolog modules the user can just use those predicates without remembering the corresponding Prolog module names These predicates that are listed in the XSB manual can be called from FLORA 2 with particular ease flora2 writeln Hello prolog i e we can simply omit the Prolog module name but parentheses must be preserved _prologall and _plgall The Prolog module specification _prolog has one subtlety it does not affect the arguments of a call For instance flora2 foo f X b _prolog will call the Prolog predicate foo 1 Recall that FLORA 2 uses HiLog terms to represent objects while Prolog uses Prolog terms Thus the argument f X b above will be treated as a HiLog term Although it looks like a Prolog term and in fact HiLog terms generalize Prolog terms the internal representation of HiLog and Prolog terms is different Therefore if the fact foo f a b is defined somewhere in the Prolog program then the above query will fail since a Prolog term f X b and a HiLog term f X b are different even though their textual representation in FLORA 2 is the same A correct call to foo 1 in this case would be as follows foo f X b _prolog _prolog Here we explicitly tell the
158. m 112 module 25 _ 28 _ isloaded 1 29 prolog 31 prolog modulename 31 prologall modulename 32 prologall 32 prologall modulename 32 _ modulename 37 contents 25 name 25 Prolog 25 31 rules for 26 system 25 37 125 user 25 158 compilation of 28 reference to 26 molecule logic expressions 16 object value 21 truth value 21 negation as failure 50 newmodule 1 85 newoid 24 non tabled predicate in FLORA 2 66 non tabled predicates importing into Prolog 34 non transactional update 73 delete 76 deleteall 76 erase 76 eraseall 76 insert 73 insertall 73 number 13 numbered anonymous oid 24 object base part of 73 derived part of 73 object constructor 9 object identifier 9 oid 9 anonymous 23 numbered 24 generated 24 operators 14 prece
159. message FLORA Warning discarding tokens rule delimiter or EOF expected 66 99 hit the Enter key once type and then Enter again This should reset the current input buffer and you should see the FLORA 2 command prompt flora2 3 F logic and FLORA 2 by Example In the future this section will contain a number of small introductory examples illustrating the use of F logic and FLORA 2 Meanwhile the reader can read the FLORA 2Z tutorial which is available on the FLORA 2 Web site http flora sourceforge net tutorial php Other tutorials exist for systems that use F logic as their knowledge representation language A tutorial for the FLORID project is at http www informatik uni freiburg de dbis florid A tutorial for Ontobroker a commercial system from Ontoprise de can be found at http www ontoprise de documents tutorial_flogic pdf FLORA 2 shares much of the syn tax with those other systems with the following notable differences FLORA 2 uses as the sepa 66099 rator between methods in object formulas while these other systems use In addition FLORA 2 does not use the sign between method names and arguments 4 Differences Between FLORA 2 Syntax and F logic Syntax FLORA 2 was developed several years after the publication of the initial works on F logic 8 and so it benefits from the experience gained in the use and implementation of the logic This experience 5 BASIC
160. might be different For instance die gt f is equivalent to d e as a formula However obj d e is f while obj d e gt f is d In general for an F logic database Z the object values of ground path expressions are given by the following mapping obj from ground molecules to sets of ground oids t o c d m can be oids or path expressions obj t t Zt for a ground Id term t obj o ol ol obj 0 T ol obj o c ol ol obj 0 Z ol c obj c d cl cl obj c T H cl d obj o m rl rl obj r Z H olm gt r Observe that if t does not occur in Z then obj t is Conversely a ground molecule r is called active if obj r is not empty Dual representation and meta predicates Since path expressions can appear wherever Id terms are allowed the question arises whether a path expression is intended to indicate a truth value or an object value For instance we may want to call a predicate foobar 1 which expects as an argument a formula because the predicate calls this formula as part of the definition For instance the predicate may take a formula and a variable that occurs in that formula and joins this formula with some predicate using that variable foobar Form Var Form mypred Var foobar a b gt X X If all arguments are treated as objects then the above query would mean alb gt X foobar a X and an unintended result will be obtained
161. mming coolThing When reification appears in facts or rule heads then the module specification and the predicate part of the reified formula must be bound For instance the following statements are illegal p X foo q X p q a 7M insert p XO 7M The semantics of reification in FLORA 2 is described in 12 Reification of complex formulas In FLORA 2 one can reify not only simple facts but also anything that can occur in arule body Even a set of rules can be reified The corresponding objects can then be manipulated in ways that are semantically permissible for them For instance reified conjunctions of facts can be inserted into the database using the insert primitive Reified conjunctions of rules can be inserted into the rulebase using the insertrule primitive Reified rule bodies which can include disjunctions negation and even things like aggregate functions and update operators can be called as queries request input gt Ticket from gt From to gt To not international inputAxioms gt Ticket international Ticket from gt From Country1 to gt To Country2 Country1 Country2 Request input gt Input inputAxioms gt Rules insertrule Rules Input In the above example the object request has two attributes which return reified formulas The input attribute returns a Boolean combination of molecules while inputAxioms returns a 12
162. mporting module The non exported elements of an imported module are encapsulated Second even when everything is exported as in the case when no explicit export directive is provided import is still different from inclusion To see why consider one module main that looks like this myprog gt gt foo importmodule foo p abc q X This module loads a program from the file myprog flr into a module foo and then imports that module The importing module itself contains a fact and a query Suppose myprog flr is as follows q X p X p 123 It is easy to see that the query q X in the importing module main will return the answer X 123 In contrast if the module main included myprog flr instead of importing it i e if it looked like this include myprog flr p abc q X then the same query would have returned two answers X 123 and X abc This is because the latter program is simply 12 HILOG AND METAPROGRAMMING 42 q X p X p 123 p abc q X In other words in the first case the query q X still queries module foo even though the query does not use the foo idiom The module foo has only one answer to the query so only one answer is returned In contrast when myprog flr is included then the resulting program has two p facts and two answers are returned 11 14 Persistent Modules Normally the data in a FLORA 2 module is transient it is lost as soon as the sy
163. myprog loads the program into the default user module named main If myprog flr contains F logic facts all these facts will be compiled separately into the file myprog fdb that is dynamically loaded at runtime Next the file myprog P is generated take a look at myprog_main P to see what has become of your FLORA 2 program and passed to the Prolog compiler yielding Prolog byte code myprog xwam which is then renamed to myprog main xwam This file is then loaded and executed If myprog flr contains queries they are immediately executed by Prolog provided there are no errors In the module system of FLORA 2 the same program can be loaded into any user module The same program can even be loaded into two different modules at the same time in which case there will be two distinct copies of the same program running at the same time For each user module a different byte code is generated this is why myprog xwam was renamed into an object file that contains the module name as part of the file name The main purpose of the FLORA 2 shell is to allow the evaluation of ad hoc F logic queries For example after consulting and loading the the file default flr from the demo directory by launching the command _demo default pose the following query and see what happens flora2 X kids Whose kids self gt K list them by name hobbies gt have hobbies H dangerous_hobby that are dangerous Ws FLOR
164. n FLORA 2 itself Apart from that the two types of libraries differ in functionality The libraries in syslib implement the primitives that are part of the syntax of the FLORA 2 language itself In contrast the libraries in 1ib are utilities that are part of the system but not part of the syntax An example is the pretty printing library Methods and predicates defined in the libraries in 1ib are accessible through the _libname system module and unlike D INSIDE FLORA 2 154 user modules they are loaded automatically at startup There are several subdirectories that hold the various files that contain definitions included at compile time These will be described in a technical document A number of other important directories contain the various included files many of which include other files The directory flrincludes contains the all important flora_terms f1lh file which defines all the names used in the system These names are defined as preprocessor macros so that it would be easy to change them if necessary The directory genincludes currently contains the already mentioned patch rules The file flrpatch f1i is a template and flrpatch flh which contains the actual patch rules is generated from flrpatch f1i during the installation The directory includes contains among others the header file which defines the a number of important macros e g FLORA_THIS_WORKSPACE that wrap all the names with prefixes to separate the diff
165. n of Rules 19 1 Creation of a New Module and Module Erasure at Run time 19 2 Insertion of Rules 0 0 0 0 0 0004 19 3 Deletion of Rules 0 0 0 0 0 0000004 20 Querying the Rule Base 21 Aggregate Operations 21 1 Aggregation and Set Valued Methods 22 Control Flow Statements 22 1 Tt Then Ese s ras vo ans a are a we Be ww aw 22 2 Loops Bute teria Vo ors amp te US a ieee ee oe ee GS St Bo 22 3 Subtleties Related to the Semantics of the Loop Statements 23 Constraint Solving 24 Exception Handling 25 Primitive Data Types iii 66 69 70 70 71 72 73 73 78 79 83 83 84 84 85 85 86 87 89 90 91 91 92 93 93 94 96 CONTENTS 25 1 FLORA 2 Symbols 25 2 The iri Data Type 25 3 GateTime 6 6 4 ea ae e a dw a k pa RE a ake Hee a The Primitive Type 25 4 date as eae E aeur ue e A a pe Dock a a a es a The Primitive Type 25 5 The Primitive Type PUNO i sey E a a ed dome age a EA A a do e ee A 25 6 The Primitive Type duration e s sra aa gra d adoa eae g a Doa E r a a ea ao 25 7 The Primitive Type boolean sas ae sw Ge a d aa we pan a Ge eae 25 8 The Primitive Type doubled o sssr sodiy eeu dadoa eae g a da fo aG eee
166. nal encoding is concerned and likewise are a b gt Y foo and Z b gt d foo 12 3 Meta decomposition oe FLORA 2 supports an extended version of the Prolog meta decomposition operator On Prolog terms it behaves the same way as one would expect in Prolog For instance flora2 X p a Z _prolog X Y X p a _h4094 _prolog Z _h4094 Y p a _h4094 8 In fact Boolean combinations of rules are also allowed inside the reification operator However such combinations cannot be inserted into the rulebase FLORA 2 does not impose limitations here since is impossible to rule out that a knowledge base designer might use such a feature in creative ways 12 HILOG AND METAPROGRAMMING 48 The main use of the operator in FLORA 2 is however for decomposing HiLog terms or of reifications of HiLog predicates and F logic atomic molecules The meta decomposition operator uses special conventions for these new cases For HiLog terms the head of the list on the fight hand side of has the form hilog HiLogPredicateName For instance flora2 p a b L L hilog p a b For HiLog predicates the head of the list has the form hilog HiLogPredicateName Module For instance flora2 p a b foo L L hilog p foo a b For non tabled HiLog predicates which represent actions with side effects the head of the list is similar except that hilog quoted is used inst
167. ned for the object a However once inherited we must necessarily have a m gt c d So the value of attribute m is not really the one produced by inheritance In other words inheritance of a m gt c negates the very premise on which the original inheritance was based so we must undo the operation and the ensuing rule application 2 We did derive a m gt d as a result of inheritance but that s OK we should not really be looking back and undo previously made inheritance inferences Thus the result must be alm gt c d A similar situation with similarly conflicting conclusions arises when the class hierarchy is not static For instance d m gt e d b b m gt c a b a d alm gt c If we inherit afm gt c from b which seems to be OK in the beginning because nothing overrides this inheritance then we derive a d i e we get the following a d b This means that now d seems to be negating the reason why alm gt c was inherited in the first place Again we can either undo the inheritance or adopt the principle that inheritance is never undone A semantics that favors the second interpretation was proposed in 8 This approach is based on a fixpoint computation of non monotonic behavioral inheritance However this semantics is very hard to implement efficiently especially using a top down deductive engine provided by the underlying Prolog engine It is also unsatisfactory in many respects because it
168. ng the same API that is used for FLORA 2 trailers which are located in the closure directory of the distribution This API will be described at a later date 15 4 Querying Module Semantics In addition to the ability to change the semantics of a module FLORA 2 also lets the user query the semantics used by any given module through the semantics primitive The syntax is similar to the setsemanticsdirective semantics Option1 Option2 semantics Option1 Option2 modulename 10 Which implies that if the file has the name none then a full path name should be specified just none implies no custom file 16 CARDINALITY CONSTRAINTS 64 The options are the same as in the case of the setsemantics directive but variables are allowed in place of the specific semantic choices e g equality X The options unify with the current semantic settings in the module so queries such as semantics equality X custom none semantics inheritance flogic equality X custom 7 Y foo are allowed The order of the options in a semantics query does not matter The module part in the semantics primitive must be bound to a module name at the time the query is executed However it is still possible to find out which modules have any given combination of semantic options by examining every loaded module via the _isloaded 1 builtin and then posing the desired semantics query 16 Cardinality Constraints Th
169. nguage it provides primitives such as input output certain types of updates cuts etc which have no logical meaning In such cases it is important to have an idea of the procedural semantics of FLORA 2 This procedural semantics is essentially the same as in XSB and when no tabled predicates or F logic molecules are involved the behavior is the same as in Prolog However when tabled HiLog predicates or F logic molecules other than procedural methods are used the programmer must have some understanding of the way XSB evaluates tabled predicates XSB has two configuration modes that affect tabled predicates batched and local the default These modes affect scheduling i e the order in which answers to the literals in a rule body are computed The current release does not work under batched scheduling so reading on is even more important to understand the flow of control under local evaluation Under the batched scheduling the behavior is similar to that of Prolog Under the local schedul ing answers to the entire clique of inter dependent predicates is computed before the computation proceeds to the next literal in a rule body The following little program illustrates the difference a b d b c b X foo Y X Y writeln X _prolog q X Y X 7Y writeln X _prolog X foo Y writeln done _prolog 4q X Y writeln done _prolog The two queries are essentially the same the first is an F logic m
170. nheritance The type of behavioral inheritance defined in the previous subsection is called value inheritance It originates in Artificial Intelligence but is also found in modern main stream object oriented languages For instance it is related to inheritance of static methods in Java With this inheritance one would define a method for a class e g cl attr gt 14 cl foo Y gt Z cl attr gt V Z is V 7Y Every member of this class will then inherit exactly the same definition of foo which refers to the class property attr Since the method definition has no way to refer to the instances on which it is invoked this method yields the same result for all class instances One way to look at this is that class instances do not really inherit the definition of the method Instead the method is invoked in the context of the class where it is defined and then the computed value is inherited down to all instances provided that they do not override the inheritance So if a cl and b cl then a foo 4 and b foo 4 will return exactly the same value 18 A more common kind of methods is called instance methods in Java In this case the method definition refers to instances of the class in whose context the method is supposed to be invoked The invocation takes place as follows First a class member inherits the code of the method Then the code is executed in the context of that class member In F logic this kind of inheritance i
171. ns appears in Section 7 In arithmetic expressions and all variables are considered to be existentially quantified For example the following query flora2 John bonus Mary bonus gt 1000 should be understood as flora2 John bonus gt _V1 Mary bonus gt _V2 V1 _V2 gt 1000 Note that in first query does not have any variables so after the evaluation the system would print either yes or no To achieve the same behavior we use don t care variables V1 and _V2 If we used V1 and V2 instead the values of these variables would have been printed out FLORA 2 recognizes numbers as oids and thus it is perfectly normal to have allows arithmetic expressions inside path expressions such as this 1 2 3 4 2 7 When parentheses are omitted this might lead to ambiguity For instance is the meaning of 1i mt2 n k represented by the arithmetic expression 1 m 2 n k or by the path expressions 1 m 2 n k by 1 m 2 n k or by 1 m 2 n k To disambiguate such expressions we must remember that the operator used in path expressions binds stronger than the arithmetic operators etc Even more interesting is the following example 2 3 4 Does it represent the path expression 2 3 4 or 2 3 4 or 2 3 4 where in the latter two cases 2 3 and 3 4 are interpreted as decimal numbers The answer to this puzzle according to FLORA 2 conventions is 2 3 4 when tokenizing FLORA 2 first tries t
172. ntermediate objects that arise implicitly in the middle of a path expression one can define the method self as X self gt X 5 A similar feature is used in other languages e g XSQL 7 8 TRUTH VALUES AND OBJECT VALUES 21 and then simply write self gt 0 anywhere in a complex path expression This would bind the Id of the current object to the variable 0 Example 7 2 Path Expressions with self To illustrate convenience afforded by the use of the self attribute in path expressions consider the second query in Example 7 1 If in addition we want to obtain the names of the conferences where the respective papers were published that query can be reformulated as follows X self gt X P authors gt name gt n at_conf self gt C year gt Y editors affil Y gt A 8 Truth Values and Object Values Id terms F logic atoms and path expressions can all be used as objects This is obvious for Id terms and the object interpretation of path expressions of the form 7 and 8 on page 19 was discussed through 10 are typically viewed as formulas and thus they are assumed to have a truth value only However there also is a natural way to give them object interpretation For example o c m gt r has object value o and some truth value However unlike the object value the truth value depends on the database on whether o belongs to class c in the database and whether the value of the
173. o argument primitive queries all rules If one wants to query only the static compiled rules or only dynamic inserted rules then the three argument primitive can be used For example flora2 clause static X Y flora2 clause dynamic X Y Withing the dynamic rules one can separately query just the dynamic rules that precede all the static rules using the flag dyna or just those dynamic rules that follow all the static ones with the dynz flag 21 AGGREGATE OPERATIONS 89 flora2 clause dyna X Y flora2 clause dynz X Y Due to a limitation of the underlying Prolog system the clause primitive cannot query rules whose size exceeds the limit imposed by the Prolog system A warning message is issued when a rule exceeds this limit and thus cannot be retrieved by clause The only way to remedy this problem is to split the long rule into smaller rules by introducing intermediate predicates 21 Aggregate Operations The syntax for aggregates is similar to the syntax used in the FLORID system A FLORA 2 aggregate query has the following form age X Gs query where agg represents the aggregate operator X is called the aggregation variable Gs is a list of comma separated grouping variables and query is a logical formula that specifies the query conditions The grouping variables Gs are optional The query part can be any combination of conjunction disjunction and negation of literals All the variab
174. o classify tokens into meaningful categories Thus when 2 3 is first found it is identified as a decimal Thus the parser receives the expression 2 3 4 which it identifies as a path expression that consists of two components the oids 2 3 and 4 Another ambiguous situation arises when the symbols and are used as minus and plus signs respectively FLORA 2 follows the common arithmetic interpretation of such expressions where the signs bind stronger than the infix operators and thus 4 7 and 4 7 are interpreted as 4 7 and 4 7 respectively Table 3 lists various operators in decreasing precedence order their associativity and arity When in doubt use parentheses Here are some more examples of valid arithmetic expressions 6 CLASS EXPRESSIONS Precedence Operator Use Associativity Arity not applicable O parentheses not applicable not applicable not applicable decimal point not applicable not applicable object reference left binary ISA specification left binary 33 subclass specification left binary 600 minus sign right unary plus sign right unary 700 multiplication left binary division left binary 800 subtraction left binary addition left binary lt less than or equals to not applicable binary gt greater than or equals to not applicable binary 1000 equals to not applicable binary unequal to not applicable binary i
175. o export This list is optional If it is not given then the predicates and modules are exported to all modules e Whether the above are exported as updatable or not If a method or a predicate is exported as updatable then the external modules can add or delete the corresponding facts Otherwise these modules can only query these methods and predicates If updatable is not specified the calls are exported for querying only The exact syntax of a MethodOrPredExportSpec is as follows updatable ExportList gt gt ModuleList The square brackets here denote optional parts The module list is simply a comma separated list of modules and ExportList is a comma separated list of predicate method ISA templates Method templates have the form termTemplate gt or termTemplate and predicate templates are the same as term templates A term template is a HiLog term that has no constants or function symbols in it For instance p and q are term templates while p a and q f are not ISA templates have the form or Of course _ can also be used instead of Examples Here are some examples of export directives export a gt 7 export b gt c d 7 gt export e gt f gt gt foo bar export updatable g gt h gt gt foo bar export updatable g gt h gt gt foo bar k
176. o loops while loop and loop until succeed even if Condition fails right from the outset The only case when these loops fail is when Action fails see Sec tion 22 3 for ways to avoid this i e to continue executing the loop even when Action fails and the possible pitfalls The statements while loop and loop until are more expensive both time and space wise than while do and do until Therefore they should be used only when truly transactional updates are required In particular such loops are rarely used with non transactional updates 23 CONSTRAINT SOLVING 93 22 3 Subtleties Related to the Semantics of the Loop Statements Observe that while loop and loop until assume that the condition in the loop is being updated inside the loop body Therefore the condition must not contain tabled predicates If such predicates are involved in the loop condition the loop is likely to execute infinitely many times Also keep in mind that in any of the four loop statements if Action fails before Condition does the loops terminate and fail Therefore if the intention is that the loop should continue even if Action fails use the Action true idiom in the loop body In case of while do and do until continuing execution of the loop is not a problem because these loops work by backtracking through Condition and the loop will terminate when there is no more ways to backtrack However in case of while loop and loop until there
177. odule rather than the default one using one of the following commands 2 FLORA 2 SHELL COMMANDS 5 flora2 file gt gt module flora2 _load file gt gt module Adding rule bases to modules When the load command loads a rule base into a module it first wipes out all the rules and facts that previously formed the knowledge base of that module Sometimes it is desirable to add the facts and rules contained in a certain file to the already existing knowledge base of a module This operation called _add does not erase the old knowledge base in the module in question It is also possible to use the syntax by prefixing the file name with a t sign Here are some examples of adding a rule base contained in files to existing modules flora2 foo flora2 foo gt gt bar flora2 _add foo flora2 _add foo gt gt bar When using the syntax adding and loading can be intermixed For instance flora2 foo gt gt bar foo02 gt gt bar This first loads the file foo flr into the module bar and then adds the rule base contained in foo2 flr to the same module Reporting answers to queries When the user types in a query to the shell the query is evaluated and the results are returned A result is a tuple of values for each variable mentioned in the query except for the anonymous variables represented as _ or and named don t care variables which are preceded with the underscore e g _abc B
178. oes not understand FLORA 2 syntax it can call only predicates defined in FLORA 2 programs To call predicates defined in FLORA 2 programs they must be imported by the Prolog program 11 8 1 Importing FLORA 2 Predicates into Prolog Shell To import a FLORA 2 predicate into Prolog shell the following must be done e The query flora2 bootstrap flora must be executed first e One of the following _flimport queries must be executed in the shell _flimport flora predicate arity as xsb name _ _ from filename gt gt flora module name _flimport flora predicate arity as rsb name _ _ from flora module name We will explain shortly which _flimport query should be used in what situation Note If FLORA 2 is installed outside of the XSB directory structure then you must let Prolog know the location of your installation of FLORA 2 This is done by executing the prolog instruction asserta library_directory path to flora For instance asserta library_directory home me flora2 before calling any of the FLORA 2 modules Observe that asserta and not assert must be used 11 MULTIFILE PROGRAMS 34 11 8 2 Calling FLORA 2 from a Prolog Module To call FLORA 2 from within a Prolog program say test P the following must be done 1 The query flora2 bootstrap_flora must be executed before compiling or loading test P otherwise the program will not compile or load 2
179. olecule and so it is implemented internally as a tabled XSB predicate The second query is implemented as a non tabled predicates Thus despite the fact that the two queries are logically equivalent they are not operationally equiv alent under local scheduling Indeed a simple experiment shows that the answers to the above two queries are produced in different orders as seen by the order of execution of the print statement In the first query X foo Y is evaluated completely before proceeding to writeln done _prolog and thus the executions of writeln X _prolog are grouped together In the second case exe cutions of writeln X _prolog and writeln done _prolog alternate because q 2is not tabled and thus its evaluation follows the usual Prolog semantics On the other hand if we have X foo Y X Y writeln X _prolog q X Y X Y writeln X _prolog X foo Y writeln done _prolog q X 7Y writeln done _prolog 17 FLORA 2 AND TABLING 72 then the two queries will behave the same as both q 2 and X foo Y would then be imple mented internally as tabled predicates Likewise if we replace foo with foo then the corresponding molecule would be represented internally as a non tabled predicate Thus the two queries in the program X foo Y X Y writeln X _prolog q X Y X 7Y writeln X _prolog X foo Y writeln done _prolog q X Y writeln done _prolog
180. olog bytecode exists If none of these tries are successful an error is reported e end Show the help info e compile file Compile FILE flr for the default module main e compile file gt gt module Compile FILE flr for the module module e _load file gt gt module Load file flr into the module module If you specify file P or file xwam then will load these files e _load file Load file flr into the default module main If you specify file P or file xwam then will load these files e ccompile file Compile FILE fir for adding to the default module main e compileadd file gt gt module Compile FILE flr for adding the module module e _add file gt gt module Add file flr to the module module e add file Add file flr to the default module main e file P xwam flr gt gt module Load the files in the specified list into the module module The files can optionally be prefixed with a which means that the file should be added to the module rather than loaded into it e _demo demofilename Consult a demo from FLORA 2 demos directory e abolish_all tables Flush all tabled data This is sometimes needed when Prolog s tabling gets in the way We describe tabling as it pertains to FLORA 2 in Section 17 e op Precedence Associativity Operator Define an operator in shell mode e all Show all solutions default Affects subsequent queries only e one Show solutions to subsequent queries one by one
181. om UpdateList UpdateList and UpdateList Refresh refresh UpdateList RuleUpdate RuleOp RuleList RuleOp insertrule insertrule_a insertrule_z deleterule deleterule_a deleterule_z RuleList Rule Rule and Rule NewoidOp newoid Variable CatchExpr catch Body Term Body ThrowExpr throw Term TruthTest true Body unknown Body false Body B The FLORA 2 Debugger FLORA 2 debugger is implemented as a presentation layer on top of the Prolog debugger so familiarity with the latter is highly recommended XSB Manual Part I Here we sketch only a few basics B THE FLORA 2 DEBUGGER 141 The debugger has two facilities tracing and spying Tracing allows the user to watch the program being executed step by step and spying allows one to tell FLORA 2 that it must pose when execution reaches certain predicates or object methods The user can trace the execution from then on At present only the tracing facility has been implemented Tracing To start tracing you must issue the command trace at the FLORA 2 prompt It is also possible to put the subgoal _trace in the middle of the program In tat case tracing will start after this subgoal gets executed This is useful when you know where exactly you want
182. omod The above statement will insert Mary children gt Frank and John father gt Smith into module foomod Note that module specifications are also allowed in the condition part of an update operator to the right of the mark flora2 insert Mary children gt X foobar adult X infomod Updates to Prolog modules is accomplished using the usual Prolog s assert retract flora2 assert foo a b c _prolog The following subtleties related to updates of Prolog modules are worth noting Recall Section 12 4 on the issues concerning the difference between the HiLog representation of terms in FLORA 2 and the one used in Prolog The problem is that foo a b c is a HiLog term that Prolog does not understand and will not associate it with the predicate foo 3 that it might have To do it right use explicit conversion flora2 p2h PrologRepr foo a b c assert PrologRepr _prolog This will insert foo a b c into the default XSB module called usermod If all this looks too complicated FLORA 2 provides a higher level primitive _prologall equivalently plgal1 as described in Section 11 7 This module specifier does automatic con version of terms to and from Prolog representation so the above example can be written much more simply flora2 assert foo a b c _prologall Another possible complication might be that If foo 3 is defined in another Prolog module bar and is imported by usermod then t
183. org 2001 XMLSchema time How ever for convenience all type names will have one or more FLORA 2 specific abbreviated forms such as time or t These abbreviated forms are case insensitive So time and _TiMe are as sumed to be equivalent In addition when the type names have the form of an IRI the compact prefix representation is supported see Section 25 2 below For instance if xsd is a prefix name for http www w3 org 2001 XMLSchema then the constant 12 33 55 http www w3 org 2001 XMLSchema time can be written as 12 33 55 xsd time Taking into the account the various abbreviations for this data type we can also write it as 12 33 55 _time or even 12 33 55 _t Variables can be also typed i e restricted to be bound only to objects of a particular primitive data type The notation is variablename typename For instance the variable X _time can be bound only to constants that have the primitive types _time The methods that are applicable to each particular primitive type vary from type to type However certain methods are more or less common 25 PRIMITIVE DATA TYPES 97 e toString which applies to a data type constant and returns its printable representation For instance if Y is bound to 12 44 23 time then Y _toString gt 12 44 237 will be true e toType parameters which applies to the any class corresponding to a primitive data type for instance _time Mo
184. ormula lives For instance 12 HILOG AND METAPROGRAMMING 45 flora2 X M a b gt c foo would bind X to the formula a b gt c 7M to the module of X Note that in meta unification the variable X in the idiom X M or X foo is viewed as a meta variable that is bound to a formula More subtle examples are flora2 X f a X Y M flora2 f a foo YOQ M M is bound to the current module in the first query and foo in the second one Y is bound to the internal representation of the HiLog formula f a _ in the first query and f a foo in the second not to the HiLog term f a Another subtlety has to do with the scope of the module specification In FLORA 2 module specifications have scope and inner specifications override the outer ones For instance in abc foo cde bar the term abc is in module foo while cde in module bar This is because the inner module specification foo overrides the outer specification bar for the literal in which it occurs i e abc These scoping rules have subtle impact on literals that are computed dynamically at run time For instance consider flora2 X M a b gt c foo X bar Because X gets bound to a b gt c foo the literal 7X bar becomes the same as a b gt c fo00 bar i e aLb gt c foo Thus both of the following queries succeed flora2 X M a b gt c foo X bar al b gt c foo flora2 X M a b gt c foo X N al b
185. pecify an object John as follows which is conceptually similar to say Java John person name gt John Doe address gt 123 Main St hobby gt chess hiking l However in FLORA 2 attributes can also be specified using rules For instance we can say that in our particular enterprise an employee works in the same building where the employee s department is located X building gt B X employee department gt _ building gt B Our experience in teaching F logic programming to users indicates that initially there is a tendency to confuse premises with consequents when it comes to class membership So a common mistake is to write the above as X employee building gt B X department gt building gt B A minute reflection should convince the reader that his is incorrect since the above rule is equivalent to two statements X building gt B X department gt _ building gt B X employee X department gt _ building gt B It is the second statement which is problematic Certainly we did not intend to say that any object with a department attribute pointing to an object with a building attribute is an employee It is interesting to note that such a confusion between premises and consequences is common only when it comes to class membership Therefore the user should be carefully check the validity of placing class membership molecules in the rule heads 30 3 Complex
186. pply foo a This guarantees that HiLog unification will work correctly at runtime For instance foo a will unify with F a and bind the variable F to foo There is one important difference between HiLog as described in 4 and its implementation in FLORA 2 In HiLog functor terms that appear as arguments to predicates and the atomic formulas i e predicates that are applied to some arguments belong to the same domain In contrast in 12 HILOG AND METAPROGRAMMING 43 FLORA 2 they are in different domains For instance suppose p a b is true and consider the following query X a b p X Here is a meta unification operator to be discussed shortly in Section 12 1 it binds X to the atomic formula a b in the current module The answer to this query is No because X is bound to the atomic formula a b while a b in p a b is a HiLog term Our earlier query 3 will also not work unlike in the original HiLog because X is bound to a term and not a formula if we execute the query 3 we will get an error stating that X is bound to a HiLog term not a predicate and therefore the query X is meaningless To correct the problem X must be promoted to a predicate and relativized to a concrete module in our case to the current module So the following query will work and produce a binding a b for X flora2 p X X p X _ Like in classical logic foo and foo
187. predicate and therefore X M represents p a main which does not meta unify with p a foo The third query gives an error because X is bound to a tabled predicate while X M expects a non tabled predicate or a HiLog term When X and X occur with explicit module specifications and are unbound then the occur rences of 4 X indicate that X is expected to be bound to predicate names Boolean method names or predicate molecular formulas that correspond only to non tabled methods or predicates Like wise an occurrence of an unbound X indicates that X is expected to be bound to predicate names or predicate molecular formulas that correspond to tabled methods or predicates prefixed variables and meta programming In meta unifications update operations and the clause construct variables that are prefixed with a 4 to indicate non tabled occurrences must have explicit module specifications An unprefixed variable without a module specification such as X can meta unify with both tabled and non tabled predicates However when an explicit module specification is given such as in X main unprefixed variables can be bound only to tabled predicates For example all of the following queries succeed without errors 7 Tota X p a X alb gt c foo X alib M X M p a X foo al b 7M In the context of update operations FLORA 2 uses the same rules for variables of the form X and X The
188. priate semantics for any user module This is done with the help of the following directive setsemantics Option1 Option2 15 CUSTOM MODULE SEMANTICS 60 Three kinds of options are allowed Equality equality none equality basic equality none is the default Inheritance inheritance none inheritance flogic inheritance flogic is the default Custom custom none custom filename custom none is the default These options are described in more detail in the following subsections Within each group only one choice can be present or else an error will result It is not required that all options be present defaults are substituted for the missing options The compiler directive described above determines the initial semantics used by the module in which the instruction occurs However it is also possible to change the semantics at run time using the executable directive setsemantics Option1 Option2 Note the use of here the symbol in the first directive designates the directives that are used at compile time only Executable directives on the other hand can occur in any query or rule body It is also possible for one module to change the semantics in another module Typically this is needed when one module creates another In this case the new module is created with the default semantics and the setsemantics executable directive makes it possible to change the semantics of such a module Here is
189. prompts 31 2 Passing Compiler Options to XSB Sometimes it is desirable to pass compiler options to the underlying Prolog compiler To do this FLORA 2 provides the directive flora_compiler_options 1 It takes one argument a list of options that is understood by the underlying Prolog compiler For instance the directive flora_compiler_options spec_repr will cause the module that contains this directive to be compiled with the XSB specialization optimization 32 Bugs in Prolog and FLORA 2 How to Report The FLORA 2 system includes a compiler and runtime libraries but for execution it relies on Prolog Thus some bugs that you might encounter are the fault of FLORA 2 while others are 32 BUGS IN PROLOG AND FLORA 2 HOW TO REPORT 135 Prolog bugs For instance a memory violation that occurs during the execution is in all likelihood an internal Prolog bug FLORA 2 is a stress test all bugs come to the surface An incorrect result during the execution can be equally blamed on Prolog or on FLORA 2 it requires a close look at the program A compiler or a runtime error for a perfectly valid program is probably a bug in the FLORA 2 system Bugs that are the fault of the underlying Prolog engine are particularly hard to fix because FLORA 2 programs are translated into mangled unreadable to humans Prolog code To make things worse this code might contain calls to FLORA 2 system libraries To simplify bug reporting FLORA
190. q x y z The primitive insertall1 is also known as a bulk insert operator Unlike insert the operator insertall always succeeds and it always leaves its free variables unbound The difference between insert and insertall is more subtle than it may appear from the above discussion In the all answers mode the above two queries will actually behave the same because FLORA 2 will try to find all answers to the query p X 7Y r Y Z and will do the insertion for each answer The difference becomes apparent if FLORA 2 is in one answer at a time mode because _one was executed in a preceding query or when the all answers mode is suppressed by a cut as in flora2 p X 7Y insert q X Y Z Ir Y Z flora2 p X Y insertall q X 7Y Z Ir Y Z In such cases the first query will indeed insert only one fact while the second will insert all Note that literals appearing inside an insert primitive to the left of the symbol if it is present are treated as facts and should follow the syntactic rules for facts and literals in the rule head In particular path expressions are not allowed Similarly module specifications inside update operators are illegal However it is allowed to insert facts into a different module so module specifications are permitted in the literals that appear in the insert primitive 18 UPDATING THE KNOWLEDGE BASE 75 flora2 insert Mary children gt Frank John father gt Smith fo
191. r an uppercase E followed by an optional minus sign or plus sign and an integer This exponent is recognized as in base 10 For example 2 43E2 is 243 whereas 2 43e 2 is 0 0243 Other datatypes FLORA 2 supports an array of primitive data types including string Boolean dateTime iri and more Primitive data types are described in Section 25 Comments FLORA 2 supports two kinds of comments 1 all characters following until the end of the line 2 all characters inside a pair of and Note that only 2 can span multiple lines Comments are recognized like whitespaces by the compiler Therefore tokens can also be delimited by comments 5 3 Operators As in Prolog FLORA 2 allows the user to define operators to liven up the syntax There are three kinds of operators infix prefix and postfix An infix operator appears between its two arguments while a prefix operator before its single argument and a postfix operator after its single argument For instance if foo is defined as an infix operator then X foo a will be parsed as foo X a and if bar is a postfix operator then X bar is parsed as bar X Each operator has a precedence level which is a positive integer Each operator also has a type The possible types for infix operators are xfx xfy yfx the possible types for prefix operators are fx fy and the possible types for postfix operators are xf yf In each of these expressions f stands for the oper
192. r atomic formulas in classical logic and F logic We avoid the use of the term atom in reference to symbols 5 BASIC FLORA 2 SYNTAX 13 Escaped String ASCII decimal Symbol 92 n 10 NewLine N 10 NewLine t 9 Tab T 9 Tab r 13 Return R 13 Return v 11 Vertical Tab V 11 Vertical Tab b 8 Backspace B 8 Backspace f 12 Form Feed F 12 Form Feed e 27 Escape E 27 Escape d 127 Delete D 127 Delete s 32 Whitespace s 32 Whitespace Table 1 Escaped Character Strings and Their Corresponding Symbols corresponding special symbols An escaped character may also be any ASCII character Such a character is preceded with a backslash together with a lowercase x or an uppercase X followed by one or two hexadecimal symbols representing its ASCII value For example xd is the ASCII character Carriage Return whereas x3A represents the semicolon In other cases a backslash is recognized as itself If it is necessary to include a single quote inside a quoted symbol that single quote must be escaped by another single quote e g isn t or by a backslash e g isn t Character lists Like Prolog character lists FLORA 2 character lists charlists are enclosed in a pair of double quotes For instance 102 111 111 is the same as foo Escape characters are recognized inside FLORA 2 charlists similarly to FLORA 2 symbols How ev
193. ram unless they are marked with _prolog or _prologall module designations Therefore again by the rules above the query should fail One should not try to convert certain Prolog terms to HiLog and expect them to be the same as similarly looking FLORA 2 terms In particular this applies to reified statements For instance if X a b gt c then p2h X Y Y a b gt c is not expected to succeed This is because p2h does not attempt to mimic the FLORA 2 compiler in cases where conversion to HiLog such as in case of reified statements makes no sense Doing so would have substantially increased the run time overhead Not all arguments passed back and forth to Prolog need conversion For instance sort 2 ground 1 compound 1 and many others do not need conversion because they work the same for Prolog and HiLog representations On the other hand most I O predicates require conversion FLORA 2 provides the io library described in Section 29 which provides the needed conversions for the I O predicates Another mechanism for calling Prolog modules described in Section 11 7 is to use of the _prologall and _prologall module specifiers _plgall also works These specifiers cause the compiler to include code for automatic conversion of arguments to and from Prolog represen tation However as mentioned above such conversion is sometimes not necessary and the use of prologall might incur unnecessary overhead 13 Nega
194. rantee that the changes will be seen during backtracking and thus the result of such a loop is indeterminate If you need to modify Condition use the statements while loop and loop until described below The above loop statements have special semantics for transactional updates Namely changes done by these types of updates are committed at the end of each iteration Thus if Condition fails the changes done by transactional updates that occur in Cond are undone Likewise if Action fails backtracking occurs and the corresponding updates are undone However changes made by transactional update statements during the previous iteration remain committed If the current iteration finishes then its changes will also remain committed regardless of what happens during the next iteration while loop and loop until This pair of loop statements is similar to while do and do until except that transactional updates are not committed after each iteration Thus failure of a state ment following such a loop can cause all changes made by the execution of the loop to be undone In addition while loop and loop until do not work through backtracking Instead they execute as long as Condition stays true Therefore the intended use of these loop statements is that Action in the loop body must modify Condition and eventually make it false for instance by deleting objects or tuples from some predicates mentioned in Condition As in the case of the previous tw
195. re using The best way to work with Emacs is to make a separate directory for Emacs libraries if you do not have one and put flora el there Such a directory can be added to emacs search path by putting the following command in the file emacs or xemacs if you are running one of the newer versions of XEmacs setq load path cons your directory load path It is also a good idea to compile emacs libraries To compile flora el use this emacs batch f batch byte compile flora el This will produce the file flora elc a compiled byte code If you are using XEmacs use xemacs instead of emacs above the two emacsen use incompatible byte code and you cannot use flora elc compiled under one system for editing files under another Finally you must tell X Emacs how to recognize FLORA 2 program files so Emacs will be able to invoke the Flora major mode automatically when you are editing such files setq auto mode alist cons flr flora mode auto mode alist autoload flora mode flora Major mode for editing Flora programs t To enable syntactic highlighting of Emacs buffers not just for FLORA 2 programs you can do the following e In Emacs select Help Options Global Font Lock on the menubar To enable highlighting permanently put global font lock mode t in emacs e In XEmacs select Options Syntax Highlighting Automatic in the menubar To enable this permanently put C EMACS SUPPORT 144
196. ree with each other However FLORA 2 views this agreement accidental as it depends on the data currently stored in the database Had one of the values changed to say d m gt g there would be a conflict 4 At the level of methods of arity gt 1 a conflict is considered to have taken place if there are two non overwritten definitions of the same method attached to two different superclasses When deciding whether a conflict has taken place we disregard the arguments of the method For instance in a c c m k gt f a d d m u gt f a multiple inheritance conflict has taken place even though in one case the method m is applied to object k while in the other it is applied to object u On the other hand a c c m k gt f a d d m k k gt f do not conflict because m 1 in the first case is a different method than m 2 in the second Similarly a c c m k gt f a d d m u gt f are not considered in conflict because here it is assumed that the method names are m k and m u which are distinct names In the examples that we have seen so far path expressions used only non inheritable attributes Clearly there is no reason to disallow inheritable attributes in such expressions To distinguish inheritable attributes from non inheritable ones FLORA 2 uses the symbol in its path expressions For instance clyde color means some X such that clyde color gt X 14 INHERITANCE 58 14 2 Code I
197. refore the following operations will succeed insert p a 4 q b Yes delete X _ Yes with X is bound p a delete X _ Yes with X is bound q b insert p a q b Yes delete X Yes X is bound to p a or q b These rules also apply to queries issued against rule bases using the clause primitive see Section 20 for the discussion of this primitive or to deletion of rules with the deleterule primitive insertrule p X q X 17 FLORA 2 AND TABLING 69 insertrule t X r xX insertrule pp X q X amp r X clause X 7Y all three inserted rules above would be retrieved clause 4 7X _0 7Y X tC _var and Y r _var clause X _ 7Y _ X p _var and Y q _var clause X _ 7Y the first and the third rules would be retrieved It is important to keep in mind that Prolog does not reorder F logic molecules and predicates during joins Instead all joins are performed left to right Thus program clauses must be written in such a way as to ensure that smaller predicates and classes appear early on in the join Also even though XSB tables the results obtained from previous queries the current tabling engine has several limitations In particular when a new query comes in XSB tries to determine if this query is similar to one that already has been answered or is in the process of being
198. rgument Module must be a valid loaded FLORA 2 module name or it can be a variable When the flag argument is on the first method turns on the checks for undefinedness in all modules The second method does it in a specific module When the flag argument is off the above methods turn the undefinedness checks off globally or in a specific module respectively When either Flag or Module or both is a variable the above methods do not change the way undefined calls are treated Instead they query the state of the system For instance in Method _mustDefine Flag _sys Method _mustDefine Flag foo _sys Method _mustDefine on Module _sys the first query binds Flag to on or off depending on whether the checks are turned on or off globally The second query reports on the state of the undefinedness checks in FLORA 2 module foo while the third query tells in which modules these checks are turned on In addition to turning on off the checks for undefinedness on the per module basis FLORA 2 provides a way to turn off such checks for individual predicates and methods Method _mustDefine off Predicate Method spec _sys For example Method _mustDefine off 7 foo _sys specifies that all undefinedness errors of predicates that unify with foo are ignored provided that foo is a loaded module Note that the module must always be specified For instance to ignore undefinedness checking in the curren
199. rinting method needs to query the object in the context of the calling module to find the methods that the object has It is also possible to view adaptive methods as a declarative counterpart of the callback functions in C C which allows the callee to behave differently for different clients 11 12 Module Encapsulation So far in multi module programs any module could call any method or predicate in any other module That is modules were not encapsulated However FLORA 2 lets the user to encapsulate any module and export the methods and predicates that other modules are allowed to call Making an unexported call will result in a runtime error A module is encapsulated by placing an export directive in it or by executing an export directive at run time Modules that do not have export directives in them are not encapsulated which means that any method or predicate defined inside such a module can be called from the outside 11 MULTIFILE PROGRAMS 39 Syntax The export directive has the form export MethodOrPredExportSpeci1 MethodOrPredExportSpec2 There can be one or more export specifications MethodOrPredExportSpec in each export state ment and there can be any number of different export statements in a module The effect of all these statements is cumulative Each MethodOrPredExportSpec specifies three things two of which are optional e The list of methods or predicates to export e The list of modules to which t
200. ronto then the following molecules will be derived where the last two are unintended flightobj from gt newyork to gt boston flightobj from gt seattle to gt toronto flightobj from gt newyork to gt toronto flightobj from gt seattle to gt boston To rectify this problem one must realize that each tuple in the flight relation must correspond to a separate object in the rule head The error in the above program is in that all tuples in flight correspond to the same object flightobj There are two general ways to achieve our goal Both try to make sure that a new object is used in the head for each flight tuple The first method is to use a new function symbol say f to construct the oids in the rule head F T flight from gt F to gt T flight F T As an added bonus we also created a class flight and made the flight objects into the members of that class While it solves the problem this approach might not always be acceptable since the oid essentially explicitly encodes all the information in the tuple An alternative approach is to use the newoid primitive from Section 10 Here we are using the fact that each time flight F T is satisfied newoid X generates a new value for X 0 flight from gt F to gt T newoid 0 flight F T This approach is not as declarative as the first one but it saves the user from the need to figure out how exactly the oids in the rule head should
201. rue or undefined Therefore it is sometimes necessary to be able to separate true from undefined facts In FLORA 2 this separation is accomplished with the FLORA 2 primitives true Goal and undefined Goal For good measure the primitive false Goal is also thrown in For instance alb gt c e f gt g not elg gt g truefa b gt c Yes unknown e f gt g Yes false k 1 gt m Yes It should be noted that the primitives true and unknown can be used only in the top level queries Otherwise correctness of the result is not guaranteed The expression false Goal is equivalent to not Goal and can be used anywhere 13 3 Unbound Variables in Negated Goals When negation either or not is applied to a non ground goal one should be aware of the following peculiarity Consider Goal where Goal has variables that are not bound As men tioned before Goal is evaluated by posing Goal as a query If for some values of for the variables in Goal the query succeeds then Goal is false it is true only if for all possible sub stitutions for the variables in Goal the query is false fails Therefore Goal intuitively means V Vars Goal where Vars represents all the nonbound variables in Goal The well founded negation has the same flavor if Goal is non ground then not Goal means V Vars Goal 14 INHERITANCE 53 Of course one should keep in mind that since neither nor not is a
202. s Rules in the list mast be enclosed in parentheses and should not terminate with a period To delete the rules inserted in the second example of Section 19 2 we can use flora2 deleterule_a X student enroll X _T X student enroll X _T mod1i or flora2 deleterule X student enroll X _T X student enroll X _T mod1 FLORA 2 provides a flexible way to express rules to be deleted by allowing variable rule head variable rule body and variable module specification For example rule deletions below are all valid flora2 deleterule H q X foo flora2 deleterule p X q X M flora2 deleterule H B The last query attempts delete every dynamically inserted rule So it should be used with great caution We should note that a rule with a composite head such as o b gt V1 c gt V2 something V1 V2 is treated as a pair of separate rules o b gt V1 something V1 V2 o c gt V2 something V1 V2 Therefore 20 QUERYING THE RULE BASE 87 flora2 deleterule o b gt V1 something V1 7V2 will succeed and will delete the first of the above rules Therefore the following action will fail afterwards flora2 deleterule o b gt V1 d gt V2 Body A Problem with Cuts What is behind rule insertion is pretty simple As we know from Section 18 every predicate and object has a base part and a derived part Now we furth
203. s such as british nativeLanguage gt English If John british is true then without evidence to the contrary we can derive John nativeLanguage gt English If we are also told that scottish british i e Scottish people are also British then we can derive again in the absence of a counter evidence that scottish nativeLanguage gt English Note that the form of the arrow gt mutates when an attribute or a method is inherited to a member of a class In general an inheritable attribute is inherited to a subclass as an inheritable attribute and to a member of a class as a non inheritable attribute In other words gt and gt do not change when inherited to subclasses but they change to gt and gt respectively when inherited to class members 14 1 Structural vs Behavioral Inheritance F logic supports two types of inheritance structural and behavioral Structural inheritance ap plies to signatures only For instance if student person and a program defines the signature person name gt string then the query student name gt X succeeds with X string Behavioral inheritance is much more complicated It is non monotonic in the sense that addition of new facts might falsify previously true facts The following is a FLORA 2 program for the classical Royal Elephant example elephant color gt color royal_elephant elephant clyde royal_elephant elephant color gt gray
204. s 7Y to http www AAA com ab 20 Note that prefix definitions are local to the module where they are defined If we define the following prefixes in module foo iriprefix W3 http www w3 org W4 w4 and then load the following file into module main iriprefix W3 http w3 org Cla gt _ http www w3 org abc D a gt _ http w3 org cde xr X X a gt W3 abc foo s X X a gt W3 cde then the different occurrences of W3 will have different expansions Thus the answer to r X will be C and the answer to s X will be D Note that a reference to W3 in a module where the prefix W3 is not defined will result in an error For convenience some IRI prefixes are predefined xsd http www w3 org 2001 XMLSchema rdf http www w3 org 1999 02 22 rdf syntax ns rdfs http www w3 org 2000 01 rdf schema owl http www w3 org 2002 07 owl rif 7 http www w3 org 2007 rif However one can always override these builtin definitions using either a compile time directive iriprefix or a runtime query iriprefix All constants of the primitive type IRI are members of the built in class _iri The IRI data type supports the following methods which are available in FLORA 2 module _basetype They are described here by their signatures 25 PRIMITIVE DATA TYPES 100 Class methods e iri _toType _symbol gt _iri e iril gt _isTypeDf _object
205. s assignment not applicable binary Table 3 Operators in Non Increasing Precedence Order and Their Associativity and Arity o1 mi o2 m2 m3 same as o1 m1 02 m2 2 3 4 the value of the attribute 3 4 on object 2 e Sad same as 3 2 5 6 same as 5 6 5 6 the value of the attribute 6 on object 5 e Note that the parentheses in 5 6 are needed because otherwise would be recognized as a single token Similarly the whitespace around and are also needed in these examples to avoid and being interpreted as distinct token 6 Class Expressions FLORA 2 defines a number of set theoretic operations on classes For instance a b represents intersection a b represents the union and a b represents the difference between the extensions of class a and b Suppose the following information is given a b c in classi c in class2 e in class3 Then class1 class2 class3 has the extension of a b e We call the above combinations of types class expressions Type expressions can occur in signature expressions as shown below c3 c3 gt c1 c2 gt c1 c2 cl attr cl attr 7 PATH EXPRESSIONS 19 Note that the old F logic syntax a b gt c d for type intersection of c and d is no longer permitted FLORA 2 also defines a number of subclass relationships among class expressions as follows 1 Ifc cl and c c2 then c
206. s called again but this time it sets FLORA_USER_WORKSPACE to foo When Prolog finally compiles the program into the object file the file is renames to myprog foo xwam It is important to keep in mind that only the predicate names are wrapped with the FLORA_PREFIX macro and a module name Predicate arguments are not wrapped and thus the space of object Ids is shared among modules However this is not a problem and actually is very convenient we can easily refer to objects defined in other modules and yet the same object can have completely different sets of properties in each separate module This does not preclude the possibility of en capsulating objects because only the methods need to be encapsulated oids do not carry any meaning by themselves To provide encapsulation for HiLog predicates they are also prepended with the module name In particular this implies that HiLog atomic formulas have different representation than HiLog terms a formula p a f b would be encoded as FLORA_THIS WORKSPACE FLORA USER WORKSPACE apply p a FLORA PREFIX apply f b The same term would be encoded differently if it occurs as an argument of a predicate of another functor FLORA_PREFIX apply p a FLORA_PREFIX apply f b Thus FLORA 2 implements a 2 sorted version of HiLog 3 13 Tt is necessary to ensure that the resulting predicate names are symbol strings acceptable to the Prolog compiler Look at the macros FLORA_THIS
207. s called code inheritance and was studied in 14 Code inheritance is not yet supported by FLORA 2 However with some loss of elegance and extra work code inheritance can often be simulated using value inheritance The method consists of three steps 1 Define desired methods for all appropriate objects irrespective of classes Definitions of these methods are the ones to be inherited using simulated code inheritance 2 Define attributes whose values are the names of the methods defined in 1 These attributes will be subject to value inheritance 3 Specify how the real methods in 1 represented by the fake methods in 2 are to be invoked on class instances We illustrate this process with the following example First assume the following database aa cl bb c2 cl c2 aa lattri gt 7 attr2 gt 2 bb attri gt 5 attr2 gt 4 We are going to show how code is inherited from c2 to bb In an attempt to inherit the same code from c2 to aa it will be overwritten by code from c1 and the latter will be inherited by aa 15 CUSTOM MODULE SEMANTICS 59 method foo 1 defined for every instance X foo Y gt Z X attri gt V Z is V Y method bar 1 defined for every instance X bar Y gt Z X attr2 gt V Z is V Y Unlike Java the above code is not really local to any class and this is one aspect in which simulation of code inheritance by value inheritance is inelegant Next we define m
208. s implemented in those modules The syntactic conventions for calling each of these types of modules are similar but distinct 11 2 Calling Methods and Predicates Defined in User Modules If literal is an F molecule or a predicate defined in another user module it can be called using the following syntax literal module The name of the module can be any alphanumeric symbol 6 For instance foo a foomod tests whether foo a is true in the user module named foomod and Mary children gt X genealogy queries the information on Mary s children available in the module genealogy More interestingly the module specifier can be a variable that gets bound to a module name at run time For instance Agent zagat newyork dinner italian gt X Agent A call to a literal with an unbound module specification or one that is not bound to a symbol will result in a runtime error When calling the literals defined in the same module the module notation is not needed of course In fact since a program does not know where it will be loaded using the notation to call a literal in the same module is hard However it is possible with the help of the special token _ which is described later and is left as an exercise The following rules apply when calling a literal defined in another module 1 Literal reference cannot appear in a rule head or be specified as a fact For example the following program wi
209. sary It is also possible to compile files for later addition without actually adding them Since files are compiled for addition a little differently from files compiled for loading we use a different command flora2 _compileadd foo flora2 _compileadd foo gt gt bar 11 7 Calling Prolog from FLORA 2 Prolog predicates can be called from FLORA 2 through the FLORA 2 module system FLORA 2 models Prolog programs as collections of static Prolog modules i e from FLORA 2 s point of view Prolog modules are always available and do not need to be loaded explicitly because the association between Prolog programs and modules is fixed _prolog and _plg The syntax to call Prolog predicates is one of the following flora2 predicate _prolog module For instance since the predicate member 2 is defined in the Prolog module basics we can call it as follows flora2 member abc cde abc pqr _prolog basics plg instead of prolog also works To use this mechanism you must know which Prolog module the particular predicate is defined in Some predicates are defined by programs that do not belong to any module When such an Prolog program is loaded the corresponding predicates become available in the default Prolog module In XSB the default module is called usermod and FLORA 2 can call such predicates as follows flora2 foo X _prolog usermod Note that variables are not allowed in the module specifications of Pro
210. se selection history before the year 2000 Note that the semantics of a delete literal query statement is that first the query literal A query should be asked If it succeeds then deletion is performed For instance if the database is p a p b p c q a q c then the query below deleteall p X q X will succeed with the variable X bound to a and c and p a p c will be deleted However if the database contains only the facts p b and q c then the above predicate will succeed deleteallalways succeeds and the database will stay unchanged FLORA 2 provides four deletion primitives delete deleteall erase and eraseall The primitive delete removes at most one fact at a time from the database The primitives deleteall and eraseall are bulk delete operations erase is kind of a hybrid it starts slowly by deleting one fact but may go on a joy ride and end up deleting much of your data These primitives are described below 1 If there are several bindings or matches for the literals to be deleted then delete will choose only one of them nondeterministically and delete it For instance suppose the database contains the following facts p a p b q a q b UPDATING THE KNOWLEDGE BASE 77 then delete p X q X will succeed with X bound to either a or b depending on the ordering of facts in the database at runtime However as with insertion in the all answers mode the above deletion will
211. several shortcuts to facilitate this use In particular the operators gt gt and gt gt for non inheritable and inheritable multivalued methods collects all the values of the given method for a given object in a set The semantics of these operators is as follows O M gt gt L L collectset V 0O M gt V O M gt gt L L collectset V 0O M gt V Note that in O M gt gt L and O M gt gt L L is a list of oids Having special meaning for gt gt and gt gt means that these constructs cannot appear in the head of a rule One other caveat recursion through aggregation is not supported and can produce incorrect results Sets collected in the above manner often need to be compared to other sets For this FLORA 2 provides another pair of primitives gt gt and gt gt for non inheritable and inheritable methods respectively The atom of the form o m gt gt s is true if the set of all values of the non inheritable attribute m for object o contains every element in the list s For instance the following query tests whether all Mary s children are also John s children 22 CONTROL FLOW STATEMENTS 91 flora2 Mary children gt gt L John children gt gt L As with gt gt and gt gt the use of gt gt and gt gt is limited to rule bodies 22 Control Flow Statements FLORA 2 supports a number of control statements that are commonly used in procedural languages These inclu
212. sffdfd is discarded 26 DEBUGGING USER PROGRAMS 115 Another way to circumvent the problem is to turn the undefinedness checks off temporarily For instance suppose the query X a causes unintended undefinedness error in module foo Then we can avoid the problem by posing the following query instead flora2 Method _mustDefine off foo _sys X a Method _mustDefine on foo _sys A more selective way to circumvent this problem is to turn off undefinedness checking just for the offending classes For instance Method _mustDefine off a _ _sys The fourth way is to deal with the exception is to use FLORA 2 s catch builtin note the curly braces include flora_exception f1lh catch X a FLORA_UNDEFINED_EXCEPTION _prolog true Undefinedness check and update operators Although undefinedness checking can be turned on and off at will it cannot always capture all cases correctly Namely if an insert or delete statement is executed while undefinedness checking is off the corresponding methods will not be properly captured an spurious undefinedness errors might result For instance if insert a meth gt b deletefalmeth gt b Method _mustDefine on _sys are executed then the query flora2 al meth gt b will cause the undefinedness error However insert a meth gt b deletefalmeth gt b Method _mustDefine on _sys almeth gt b will not flag the method me
213. spectively The reason for this is that first such terms are allowed in Prolog and there is no good reason to ban them in FLORA 2 and second the above syntax is ambiguous and the parser makes the choice that is consistent with the choice made in Prolog Typically users do not put parentheses around subgoals in such cases and would instead write 5 BASIC FLORA 2 SYNTAX 16 a b c i ais Dis Note that things like a b c a b c will be interpreted as queries so there are plenty of ways to satisfy one s fondness for redundant parentheses 5 4 Logical Expressions In a FLORA 2 program any combination of conjunction disjunction and negation of literals can appear wherever a logical formula is allowed e g in a rule body Conjunction is represented through the infix operator and disjunction is made using the infix operator Negation is made through the prefix operators and not 4 When parentheses are omitted conjunction binds stronger than disjunction and the negation operators bind their arguments stronger than the other logical operators For example in FLORA 2 the following expression a b c not d is equivalent to the the logical formula a A b V c A d Logical formulas can also appear inside the specification of an object For instance the following F molecule o not atti gt val1 att2 gt val2 meth gt res is equivalent to the following formula not of atti
214. st Other methods e list toString gt _symbol e list gt _equals _object e list _typeName gt _symbol Examples e a b c e a b X e a b cl d e 26 Debugging User Programs FLORA 2 comes with an interactive Prolog style debugger which is described in Appendix B The compiler makes many useful checks such as the occurrence of singleton variables which is often an error see Section 5 1 More checks will be provided in the future In addition it is possible to tell FLORA 2 to perform various run time checks as described below 26 1 Checking for Undefined Methods and Predicates FLORA 2 has support for checking the invocation of undefined methods and predicates at run time This feature can be of great help because a trivial typo can cause a method predicate call to fail sending the programmer on a wild goose chase after a hard to find bug It should be noted however that enabling these checks can slow the runtime by up to 2 times typically about 50 though so we recommend this to be done during debugging only To enable runtime checks for undefined invocations FLORA 2 provides two methods which can be called at any time during program execution and thus enable and disable the checks dynamically 26 DEBUGGING USER PROGRAMS 113 Method _mustDefine Flag _sys Method _mustDefine Flag Module _sys The argument Flag can be on off or it can be a variable The a
215. st be tabled 29 FLORA 2 System Modules FLORA 2 provides a number of useful libraries that other programs can use These libraries are statically preloaded into modules that are accessible through the special modname syntax and they are called system modules We describe the functionality of these modules below Some of these modules also have longer synonyms These synonyms are mentioned below if they exist 29 1 Input and Output This library simplifies access to the most common Prolog I O facilities This library is preloaded into the system module io and can be accessed using the _io syntax The purpose of the I O library is not to replace the standard I O predicates with FLORA 2 methods but rather to relieve the user from the need to do explicit conversion of arguments between the HiLog representation of terms used in FLORA 2 and the standard Prolog representation of the underlying engine 2 However for uniformity the io library also provides certain methods that do not suffer from the conversion problem The library contains two types of I O operations stream based I O and port based Stream based I O is based on the standard Prolog I O primitives It uses symbols as file handles Port based I O is specific to XSB Its file handles are internally represented as numbers Although stream based I O is often easier to use there are many more port based primitives that can ac complish various low level I O operations This FLORA 2
216. st types will have two versions of this method One will apply to ar guments that represent the components of a data type For instance time _toType 12 23 45 gt 12 23 45 time The other will apply to the constant symbol representation of the data type For instance time _toType 12 23 45 gt 12 23 45 time e _isTypeOf constant which applies to every data type class e g time and determines whether constant has the given primitive type _time in this example e _equal constant which tells when the given datatype constant equals some other term e _lessThan constant which tells when one constant is less than some other terms For integers floats time dates durations and strings this method corresponds to the natural order on these types For other types this method returns false e _typeName which tells the type name and thus also class of the given data type All these methods are available in FLORA 2 system module _basetype In addition each primitive data type has a builtin class associated with it For instance the primitive data type integer has an associated class named _integer and the data type _dateTime has an associated class under the same name Note Since builtin classes have infinite extensions you can only have ground membership tests with respect to these classes Non ground tests are permitted but are evaluated to false For instance the following query fails flora
217. stem terminates The FLORA 2 package persistentmodules allows one to make FLORA 2 modules persistent This package is described in the document A Guide to FLORA 2 Packages 12 HiLog and Metaprogramming HiLog 4 is the default syntax that FLORA 2 uses to represent functor terms including object Ids and predicates In HiLog complex terms can appear wherever a function symbol is allowed For example group X Y Z is a HiLog term where the functor is no longer a symbol but rather a complex term group X Variables in HiLog can range over terms predicate and function symbols and even over atomic formulas For instance p X X p and p X X p X 3 are perfectly legal queries If p a b a b p and a b are all true in the database then X a b is one of the answers to the query in HiLog Although HiLog has a higher order syntax its semantics is first order 4 Any HiLog term can be consistently translated into a Prolog term For instance group X Y Z can be represented by the Prolog term apply apply group X Y Z The translation scheme is pretty straightforward and is described in 4 Any Id term in FLORA 2 including function symbols and predicate symbols are considered to be HiLog terms and therefore are subject to translation That is even a normal Prolog term will by default be represented using the HiLog translation e g foo a will be represented as a
218. structures including the subclass hierarchy the ISA hierarchy etc For instance if x and y are equal then so must be f x and f y If x a has been previously derived then we should now be able to derive y a etc Although equality is a powerful feature its maintenance can slow the program down quite significantly In order to be able to eat the cake and have it at the same time FLORA 2 allows the user to control how equality is handled by providing the following three compiler directives setsemantics equality none default setsemantics equality basic The first directive setsemantics equality none does not maintain any equality and behaves similarly to the regular unification operator but additional facts and rules can be inserted to augment the definition of this predicate Under this semantics is not transitive and the special congruence properties of equality are not supported for instance p a and a b do not imply p b The directive setsemantics equality basic guarantees that obeys the usual rules for equality i e transitivity reflexivity symmetry and limited substitution If a FLORA 2 module does not define facts of the form a b which involve the equality predicate then the default equality maintenance level is none If the program does define such facts then the default equality maintenance level is basic because it is assumed that the use of in the program is not accidental
219. suing the query abolish all_tables O described in the XSB manual However this should be done with great caution because abolish_all_tables O is not a safe query it can crush XSB If you really need to use abolish_all_tables 0 it cannot be used in the following cases 1 in the body of a rule that defines an object attribute or method 2 in the body of a rule that defines a tabled HiLog predicate 17 FLORA 2 AND TABLING 70 This is because internally the above entities are represented using tabled predicates Execution of abolish_all_tables O in the body of such a rule would destroy the table for the predicate being computed by that rule More generally no tabled predicate or object molecule of the above sort can depend via rules directly or indirectly on abolish_all_tables 0 However it is safe to use this predicate in the body of a rule that defines a procedural method defined next Note Neither refresh nor abolish_all_tables can occur under the scope of the negation operator not either directly or indirectly 17 3 Procedural Methods Because tabling is not integrated with the update mechanism in Prolog it can have undesirable effect on predicates with non logical side effects e g writing or reading a file and predicates that change the state of the database If a tabled predicate has a side effect the first time the predicate is called the side effect is performed but the second time the call simpl
220. sure and have the suffix f1i These files are used as components from which FLORA 2 trailers are created Trailers are called so because they are typically included at the end of the compiled program The template for all trailers is found in includes flrtrailer flh Several kinds of trailers can be generated from this file the no equality trailer whose main component is closure flrnoeqltrailer fli which maintains no equality and the basic trailer closure flreqltrailer f1li which maintains only the standard equality axioms There are variations of these trailers that also support F logic inheritance f lreqltrailer_inh fli and flrnoeqltrailer_inh f1li When a FLORA 2 program is compiled the compiler includes the trailers into the P file However there also is a need to be able to load the trailers dynamically First this is needed in the system shell because the shell is not represented by any particular user program and so there is no place where we can include the trailer Second the user might enter the executable instruction setsemantics equality at the shell prompt and user programs can contain these instructions as part of their code When an equality maintenance instruction is executed for a particular module the trailer for that module must be compiled and loaded dynamically The need for this compilation will become clear after we explain the implementation of the module system These trailers are stored in the user
221. t be in canonical Prolog form or else an error will result This method is much faster than the usual read operation but it is not as versatile as it assumes that input is in canonical form Port 4read_canonical Term same but use Port for input readline Type String read the standard input and bind String to the next line Type is either atom or charlist The former means that String is to be bound to a Prolog atom and the latter binds it to a list of characters Port 4readline Type String same but use Port for input Common file operations The io module also provides a class File which has methods for the most common file operations These include File exists F True if file F exists File readable F True if file F is readable File writable F True if the file is writable File executable F True if the file is executable File modtime F T Binds T to the last modification time of F File mkdir F Makes a directory named after the value of F File rmdir F Removes the directory F File chdir F Changes the current directory to F File cwd F Binds F to the current working directory in the shell File link F Dest _ Creates a link named after F to the existing file 7Dest File unlink F Removes the link F File remove F Removes the file F File tmpfilename F _ Binds F to a temporary file with a completely new name
222. t module use Method _mustDefine off 7 _ _sys Note that the use of the current module symbol _ is essential in this example Omitting it is probably not what you want because the module specification _sys propagates inward and so the above statement without the _ would turn off undefinedness checks in module sys instead of the current module One can also turn undefinedness checks is all modules by putting a variable in the module position Method _mustDefine off 7 Mod _sys However this must not be an anonymous variable like _ or a don t care variable like _Something If one uses an anonymous or a don t care variable then undefinedness checks will be ignored only in some randomly picked module A pair of parenthesis is needed when multiple predicates methods are listed in one call 26 DEBUGGING USER PROGRAMS 114 Method _mustDefine off class foo 7 7 _ _sys The undefinedness exception in FLORA 2 can be caught using FLORA 2 s catch builtin For instance suppose FOO is a predicate or an F molecule whose execution might trigger the unde finedness exception Then we can catch this exception as follows include flora_exceptions f1lh catch F00 FLORA_UNDEFINED_EXCEPTION Call ErrorMessage handler Call Here FLORA_UNDEFINED_EXCEPTION is the exception name defined in the FLORA 2 system file flora_exceptions flh which must be included as shown The predicate handler 1
223. t_eraseall 78 t_insert 78 t_insertall 78 triehandle 151 true 52 type checking 115 type constraint 10 unknown 52 unless do 92 until 92 update 73 non transactional 73 transactional 78
224. take place for each binding that makes the query true To avoid this use one answer at a time mode or the cut 2 In contrast to the plain delete primitive deleteal1 will try to delete all bindings or matches Namely for each binding of variables produced by query it deletes the corresponding instance of literal If query A literal is false the deleteall primitive fails To illustrate consider the following flora2 p X 7Y deleteall q X Y Z r Y Z and suppose p x y is true Then the above statement will for each z such that r y z is true delete q x y z For another example suppose the database contains the following facts p a q b q c and the query is deleteall p a q X The effect will be the deletion of p a and of all the facts in q If you wanted to delete just one fact in q delete should have been used Unlike the delete predicate deleteall always succeeds Also deleteall leaves all variables unbound 3 erase works like delete but with an object oriented twist For each F logic fact f that it deletes erase will traverse the object tree by following f s methods and delete all objects reachable in this way It is a power tool that can cause maiming and injury Safety glasses and protective gear are recommended Note that only the base part of the objects can be erased If the object has a part that is derived from the facts that still exist this part will not be erased 4 eraseall
225. tax The leftmost Z is an optional sign The part that starts with the second Z is optional and represents the time zone the second Z is a sign which can be either or note that the first Z can be only the minus sign or nothing The part that starts with T is also optional it represents the time of the specified day The part of the time component of the form s represents fractions of the second Here s can be any positive integer The constants of this primitive type all belong to the class dateTime The name of this type has the following synonyms _dt http www w3 org 2001 XMLSchema dateTime The following methods are available in the FLORA 2 system module _basetype they are de scribed by their signatures below Class methods e dateTime _toType _integer integer integer integer integer integer decimal _integer integer integer gt _dateTime The meaning of the arguments is as follows in that order date sign year month day hour minute second zone sign zone hour zone minute All arguments except date sign and zone sign are assumed to be positive integers while date sign and zone sign can be either 1 or 1 e dateTime _toType _symbol gt _dateTime e dateTime gt _isTypeDf _object Tells if object belongs to the primitive type _dateTime Component methods e dateTime _dateSign gt _integer e _dateTime _year gt _integer e dateTime _month gt _integer e dateTime _day
226. te binding of the method to the module will be done at runtime This dynamic nature of FLORA 2 modules stands in sharp contrast to the module system of Prolog which is static and associates modules with files at compile time Moreover to call a predicate from another module that predicate must be imported explicitly and referred to by the same name As a pragmatic measure FLORA 2 defines three kinds of modules rather than just one The kind described above is actually just one of the three the user module As explained these modules 11 MULTIFILE PROGRAMS 26 are decoupled from the actual code and so they can contain different code at different times The next kind is a Prolog module This is an abstraction in FLORA 2 which is used to call Prolog predicates Prolog modules are static and are assumed to be closely associated with their code We describe these modules in Section 11 7 Do not confuse FLORA 2 Prolog modules an abstraction used in the language of FLORA 2 with Prolog modules which is an abstraction used in Prolog The third type of modules are the FLORA 2 system modules These modules are preloaded with FLORA 2 programs that provide useful methods and predicates e g I O and thus are also static These modules are described in Section 11 9 and 29 The abstraction of system modules is a convenience provided by FLORA 2 which enables user programs to perform common actions using standard names of predicates and method
227. th as undefined 26 2 Type Checking Although FLORA 2 allows specification of object types through signatures type correctness is not checked automatically A future versions of FLORA 2 might support some form of run time type checking Nevertheless run time type checking is possible even now although you should not expect any speed here and this should be done during debugging only Run time type checking is possible because F logic naturally supports powerful meta programming although currently the programmer has to do some work to make type checking happen For in stance a programmer can write simple queries to check the types of methods that might look suspicious Here is one way to construct such a type checking query 26 DEBUGGING USER PROGRAMS 116 type_error 0 M R 7D hh Values that violate typing O0L M gt R OL M gt D not R D hh Defined methods that do not have type information 0 M gt R not 0 M gt _D type_error 0bj Meth Result Class Here we define what it means to violate type checking using the usual F logic semantics The corresponding predicate can then be queried A no answer means that the corresponding attribute does not violate the typing rules In this way one can easily construct special purpose type checkers This feature is particularly important when dealing with semistructured data Semistructured data has object like structure but normally does not need to conform
228. th to prolog executable If XSB can be found through the PATH environment variable then you can simply type makeflora Running FLORA 2 FLORA 2 is fully integrated into the underlying Prolog engine including its module system In particular FLORA 2 modules can invoke predicates defined in other Prolog modules and Prolog modules can query the objects defined in FLORA 2 modules At present XSB is the only Prolog platform where FLORA 2 can run because it heavily relies on tabling and the well founded semantics for negation both of which are available only in XSB Due to certain problems with XSB FLORA 2 runs best when XSB is configured with local scheduling which is the default XSB configuration However with this type of scheduling many Prolog intuitions that relate to the operational semantics do not work Thus the programmer must think more declaratively and in particular to not rely on the order in which answers are returned The easiest way to get a feel of the system is to start FLORA 2 shell and begin to enter queries interactively The simplest way to do this is to use the shell script flora2 runflora 4 where is the directory where FLORA 2 is downloaded For instance FLORA flora2 runflora At this point FLORA 2 takes over and F logic syntax becomes the norm To get back to the Prolog command loop type Control D Unix or Control Z Windows or flora2 _end If you are using FLORA 2 shell frequen
229. the specified module flora2 myprog gt gt foomod flora2 _load myprog gt gt foomod This loads the FLORA 2 program myprog flr into the user module named foomod compiling it if necessary The user can compile and load several program files at the same time If the file was not compiled before or if the program file is newer the program is compiled before being loaded For instance the following command flora2 myprog1 myprog2 will load both myprogi and myprog2 into the default module main However loading several programs into the same module is not very useful the code of the last program will wipe out the code of the previous ones This is a general rule in FLORA 2 Thus loading multiple files is normally used in conjunction with the module targets flora2 myprogi flr myprog2 gt gt foomod which loads myprog1 flr into the module main and myprog2 flr into the module foomod Adding to already loaded modules Files can also be added to an existing module as ex plained in the following subsection Note that the command can also load and compile Prolog programs The overall algo rithm is as follows If the file suffix is specified explicitly the corresponding file is assumed to be a FLORA 2 file a Prolog file or a byte code depending on the suffix flr P or xwam If the suffix is not given explicitly the compiler first checks if file flr exists If so the file assumed to be a FL
230. tion FLORA 2 supports two kinds of negation the usual Prolog s negation as failure 5 and negation based on well founded semantics 9 10 Both types of negation are compiled into clauses that invoke the corresponding operators in Prolog We should remark that originally the term negation as failure was used to denote the treat ment of negation in Prolog This style of negation is unsatisfactory in many respects because it does not have a model theoretic characterization and because operationally it often leads to infi nite loops To overcome this problem several different semantics were introduced including the aforesaid well founded semantics At some point later the meaning of the term negation as failure was broadened to refer to the class of all these forms of negation When ambiguity may arise we will be referring to Prolog style negation as failure 13 NEGATION 5l 13 1 Two Operators for Negation Prolog style negation as failure is specified using the operator Negation based on the well founded semantics is specified using the operator not The well founded negation not applies to predicates that are tabled i e predicates that do not have the prefix which will be discussed in detail in Section 17 or to F molecules that do not contain procedural methods i e methods that are prefixed with a The semantics for negation as failure is simple To find out whether G is true
231. tion 0ptimizerOption 7Module disables the corresponding optimizer options It should be noted that different queries require different optimizations and any given option might improve the performance of one set of queries while degrading the performance of others 28 COMPILER DIRECTIVES 123 Some queries may not even work with certain optimizations and produce runtime errors Therefore FLORA 2 gives the user the means to turn the optimizations on and off depending on the situation At present FLORA 2 has only two optimization option local_override class_expressions Invoking _optimize local_override somemodule can in some cases speed up query processing by the factor of 10 This optimization typically helps programs that use inheritance but inheritance is overwritten by locally defined methods in most of the cases Invoking _optimize class_expressions somemodule always improves performance some times significantly and sometimes negligibly However this is done at the expense of disabling the subtype relationships that involve class expressions see Section 6 So while class_expressions optimization is on the usual subclass relationships among these expressions does not hold For instance c c d and c d c are false 28 Compiler Directives Executable vs compile time directives Like Prolog compiler FLORA 2 compiler can take compiler directives Like in Prolog these directives can be executable or compi
232. tly it pays to define an alias say in Bash alias flora2 xsb e flora2 flora_shell alias runflora FLORA flora2 runflora FLORA 2 can then be invoked directly from the shell prompt by typing flora2 or runflora It is even possible to tell FLORA 2 to execute commands on start up For instance foo gt flora2 e _end foo gt runflora e _end will cause the system to execute the help command right after after the initialization Then the usual FLORA 2 shell prompt is displayed FLORA 2 comes with a number of demo programs that live in 2 FLORA 2 SHELL COMMANDS 4 flora2 demos The demos can be run issuing the command _demo demo filename at the FLORA 2 prompt e g flora2 _demo flogic_basics There is no need to change to the demo directory as demo knows where to find these programs 2 FLORA 2 Shell Commands Loading programs from files The most common shell command you probably need are the commands for loading and compiling a program flora2 programfile or flora2 _load programfile Here program file can contain a FLORA 2 program or a Prolog program If program file flr exists it is assumed to be a FLORA 2 program The system will compile the program if necessary and then load it The compilation process is two stage first the program is compiled into a Prolog program one or more files with extensions P and fdb and then into an executable byte code which has
233. tly contain facts because these are trie indexed anyway regardless of what you say Thus this instruction is useful only for predicates with partially instantiated arguments that appear in the rule heads This is an executable version of the index directive The module of the predicates can be specified index ZArity Argument The index directive for non tabled HiLog predicates index 7Arity Argument Qmodule The executable index for non tabled HiLog predicates _op precedence type operator Defines operator as a FLORA 2 operator with the given precedence and type The type is the same as in Prolog operators i e fx xf xfy etc Note that the op directive is confined to the module in which it is executed or defined For instance if example flr has a call a 29 FLORA 2 SYSTEM MODULES 125 foo b bar the symbol foo is declared as an operator in the program loaded to module bar but not in example flr then a syntax error will result because example flr does not know about the operator declaration for foo _op precedence type Loperator operator Same as above except that this directive defines a list of operators with the same precedence and type _op precedence type operator G module Same as above except that a module is also given However unless the module is main this directive acts as a no op table functor arity functor arity Requests that the specified first order predicates mu
234. ts arguments can be bound For instance flora2 X flogic boolean foo a pl 7X al p foo At present the operator does not do reified terms that represent aggregate operators update predicates and other FLORA 2 statements that are not part of the extended F logic or HiLog syntax In such cases the query simply fails In the future might be extended to some of these additional classes of terms The original Prolog s is also available using the idiom _prolog This is rarely used however One might use this when the term to be decomposed is known to be a Prolog terms in this case the Prolog s operator will run slightly faster or if one wants to process the Prolog terms into which FLORA 2 literals are encoded internally which is probably hardly ever necessary 12 4 Passing Parameters between FLORA 2 and Prolog The native HiLog support in FLORA 2 causes some tension when crossing the border from one system to another The reason is that FLORA 2 terms and Prolog terms have different internal representation Even though XSB supports HiLog according to the manual anyway this support is incomplete and is not integrated well into the system most notably into the XSB module system As a result XSB does not recognize terms passed to it from FLORA 2 as HiLog terms and thus many useful primitives will not work correctly Try writeln foo abc prolog and see what happens To cop
235. ture 3 Inheritance of procedural methods is not supported al p X A A BNF STYLE GRAMMAR FOR FLORA 2 138 Appendices A A BNEF style Grammar for FLORA 2 hh To avoid confusion between some language elements and meta syntax hh e g parentheses and brackets are part of BNF and also of the language hh being described we enclose some symbols in single quotes to make it hh Clear that they are part of the language syntax not of the grammar hh However in FLORA these symbols can be used with or without the quotes Rule Head Body Query Body Directive ExportDirective OperatorDirective SetSemanticsDirective IgnoreDependencyCheckDirective PrologDirective Head HeadLiteral Head Head and Head HeadLiteral BinaryRelationship ObjectSpecification Term Body BodyLiteral Body BodyConjunct BodyDisjunct BodyNegative ControlFlowStatement Body Body ModuleName Body BodyConstraint ModuleName atom atom atom atom thisModuleName BodyConjuct Body and Body BodyDisjunct Body gt or Body BodyNegative not Body false Body BodyConstraint CLPR style constraint ControlFlowStatement IfThenElse UnlessDo WhileDo WhileLoop DoUntil LoopUntil IfThenElse if Body then Body else Body Body
236. uality maintenance level In FLORA 2 the desired level of equality maintenance can also be changed at run time by executing a goal such as setsemantics equality basic Furthermore FLORA 2 allows one user module to set at run time the level of equality maintenance in another user module setsemantics equality basic foobar This might be useful for dynamic modules i e modules that are not associated with any files and whose content is generated completely dynamically See Section 18 Using the preprocessor to avoid the need for equality maintenance One final advice regarding equality In many cases programmers tend to use equality as an aliasing technique for long messages numbers etc In this case we recommend to use the preprocessor commands which achieve the same result without loss of performance For instance define YAHOO 7 http yahoo com YAHOO fetch gt X Assuming that fetch is a method that applies to strings that represent WWW sites and that fetches the corresponding Web pages the above program will fetch the page at the Yahoo site because FLORA 2 compiler will replace YAHOO with the corresponding string that represents a URL Limitations of equality maintenance in FLORA 2 The implementation of equality in FLORA 2 supports only a limited version of the congruence axiom due to the overhead associated with such an implementation A congruence axiom states that if a then 8 can be su
237. unlike Unix there should be no all after makeflora in Windows It is also recommended that you set the environment variable HOME on your Windows system if it is not already defined Environment variables are usually set by opening the System folder which is located inside the Settings folder Typically the HOME variable is set to the directory C Documents and Settings your user name Es y This can prevent problems with upgrading to the latest version of FLORA 2 If you are a developer and wish to recompile the C part of FLORA 2 and provided you have a Microsoft C compiler then you can type makeflora c path to prolog ezecutable Normally however there is no need to do so Installing FLORA 2 in Windows under Cygwin Although FLORA 2 runs under native Win dows it runs faster under Cygwin because the underlying Prolog engine has special optimizations for GCC To install FLORA 2 under Cygwin configure XSB as in Unix and use the default options cd XSB build configure makexsb Then change to the FLORA 2 directory and configure FLORA 2 A version of this compiler which is all you need in order to compile XSB or FLORA 2 can be downloaded free of charge from http msdn microsoft com vstudio express visualC default aspx It is also necessary to install Windows Platform SDK accessible from the above page 1 INTRODUCTION 3 cd directory where you unpacked floraz make clean makeflora all pa
238. ure FLORA 2 do the following cd flora2 make clean makeflora assuming that XSB has been already installed and configured If an XSB executable is not on your program search PATH then in the third command above you need to provide the XSB installation directory to makeflora as an argument e g makeflora all XSB if XSB is installed in the directory XSB Installing FLORA 2 in Windows First you need Microsoft s nmake which can be downloaded from http download microsoft com download vc15 Patch 1 52 W95 EN US Nmake15 exe The 1 See http www informatik uni freiburg de dbis florid for more details 1 INTRODUCTION 2 file Nmake15 exe is a self extracting archive when it is run it extracts the program files for nmake exe into the same directory where Nmake15 exe resides Let s say this directory is C Nmake To unpack the FLORA 2 file archive you also need WinZIP Once nmake exe is installed and the FLORA 2 archive is unpacked use the following commands to configure FLORA 2 assuming that XSB is already installed and configured PATH C Nmake PATH assuming that nmake exe is in C Nmake cd directory where you unpacked flora2 makeflora clean makeflora path to prolog executable Here directory where you unpacked flora2 is the directory where you unpacked FLORA 2 it should have the form something flora2 The path to prolog executable must be the full path name of the XSB executable Note that
239. urs as a term and not as a predicate it can be made legal by reifying the argument p a In the second and third formulas a and X also appear as unreified arguments The fourth formula is illegal because b c is not a Boolean method The last one is illegal because X can not be a stand alone formula it can be made legal by associating a module with it Occurrences of variables that are prefixed with are treated specially First it should be kept in mind that 4 X and X represent the same variable If X is already bound to something then all both of them mean the same thing However X itself can range not only over predicates but also terms conjunctions disjunctions of predicates and even rules In contrast 4 X with module specification can be bound only to non tabled formulas and X with module specification can be bound only to tabled formulas Thus error messages will be issued for the following two queries X pla X 7M pla X a b X 7M al b The following query fails because X and X represent the same variable the first conjunct deter mines the binding for X and this binding does not match the expression on the right side of in the second conjunct XOMM p a X pCa In the query X is bound to the non tabled formula p a and this does not meta unify with the tabled formula p a When a bound variable occurs with an explicit module specification then the following rul
240. ut it unless she runs FLORA 2 programs in a very unfriendly Prolog environment in which other programs also use this D INSIDE FLORA 2 150 prefix In this case the prefix can be made even harder to match Apart from the general prefix each predicate name s prefix contains the module name where this predicate is defined Since the same F logic program can be loaded into different modules the FLORA 2 compiler does not actually know the real names of the predicates it is producing Instead it dumps code where each predicate is wrapped with a preprocessor macro For instance the predicate mvd would be dumped as FLORA_THIS_ WORKSPACE FLORA_USER WORKSPACE mvd where FLORA_THIS_WORKSPACE and FLORA USER WORKSPACE are preprocessor macros When a pro gram myprog P which is compiled by the FLORA 2 compiler needs to be loaded into a user module say main the preprocessor gpp is called with the macro FLORA_USER_WORKSPACE set to main Gpp replaces all macros with the actual values For instance the above macro expression will be replaced with something like _ _ _flora usermod main mvd Gpp then includes all the necessary files and then pipes the result to the Prolog compiler The latter produces the object myprog xwam file where all the predicate names are wrapped with the user module name as described above This object file is renamed to myprog main xwam If later we need to compile myprog P for another user module foo gpp i
241. we say that it is an attribute for example John name gt John When M has the form f X Y Z then we refer to it as a method f with arguments X Y and Z for example John salary 1998 gt 50000 However as we saw earlier method expressions can be much more general than these two possibilities as they can be arbitrary HiLog terms The expression 3 above is a signature atom It specifies a type constraint which says that the method expression M when applied to objects that belong to class C must yield objects that belong to class T Note FLORA 2 does not automatically enforce type constraints However run time type checking is possible see Section 26 2 Objects are grouped into classes using S A atoms 4 0 C 5 C D The expression 4 states that 0 is an instance of class C while 5 states that C is a subclass of D User defined equality 5 BASIC FLORA 2 SYNTAX 11 6 01 02 enables the user to state that two syntactically different and typically non unifiable terms represent the same object For instance one can assert that a b and from then on everything that is true about a will be true about b and vice versa Note that this is different and more powerful than the unification based equality builtin which exists both in FLORA 2 and Prolog For instance based formulas can never occur as a fact or in the rule head and a b is always false More on user defined equality in Section 15
242. with a period unlike the static program rules insertrule_a X student enroll X _T The above inserts the rule X student enroll X _T in front of the current module If a rule is meant to be inserted into a module other than the current one then the rule needs to be parenthesized and the module name must be attached using the usual module operator If several rules need to be inserted using the same command each rule must be parenthesized For example the following statement inserts the same rule into two different modules the current one and into module mod1 insertrule_a X student enroll X _T X student enroll X _T mod1 As a result the rule X student 4 enroll X _T will be inserted in front of each of these two modules For this to be executed successfully the module mod1 must already exist 19 INSERTION AND DELETION OF RULES 86 19 3 Deletion of Rules Rules inserted dynamically using insertrule_a can be deleted using the primitive deleterule_a and rules inserted using insertrule_z can be deleted using the primitive deleterule_z If the user wishes to delete a rule that was previously inserted using either insertrule_a or insertrule_z then the primitive deleterule can be used Similarly to rule insertion several rules can be deleted in the same command delruleop rulelist where delruleop is either deleterule_a or deleterule_z and rulelist is a comma separated list of rule
243. xpressionConnective special0idToken anonymousOid numberedOid thisModuleName ReifiedFormula Body Rule hh No quotes are allowed in the following special tokens hh No space allowed between _ and integer hh anonymousOid amp numberedOid can occur only in rule head hh or in reified formulas anonymous0id 7 _ hh No space between _ and integer numberedOid _ integer B THE FLORA 2 DEBUGGER 140 thisModuleName _ List PathExpression PathExpression PathExpression Term Functor Arguments Term Functor Arguments Functor PathExpression Arguments PathExpression PathExpression Aggregate AggregateOperator TargetVariable GroupingVariables Body AggregateOperator max min avg sum collectset collectbag hh Note only one TargetVariable is permitted hh It must be a variable not a term If you need to aggregate over terms hh as for example in collectset collectbag use the following idiom hh S collectset V V Term TargetVariable variable GroupingVariables variable variable DBUpdate DBOp UpdateList Body DBOp insert insertall delete deleteall erase eraseall UpdateList HeadLiteral at
244. y default FLORA 2 prints out all answers If only one at a time is desired type in the following command _one You can revert back to the all answers mode by typing all Note one and _all affect only the subsequent queries That is in flora2 _one goallist1 flora2 goallist2 the one directive will affect goallist2 but not goallisti This is because goallist1 executes in the same query as _one and thus is not affected by this directive FUORA 2 shell includes many more commands beyond those mentioned above These com mands are listed below However at this point the purpose of some of these commands might seem a bit cryptic so it is a good idea to come back here after you become more familiar with the various concepts underlying the system Summary of shell commands In the following command list the suffixes flr P xwam are optional If the file suffix is specified explicitly the system uses the file with the given name without any modification The flr suffix denotes a FLORA 2 program the P suffix indicates that it is a 2 FLORA 2 SHELL COMMANDS 6 Prolog program and xwam means that it is a bytecode file which can be executed by Prolog If no suffix is given the system assumes it is dealing with a FLORA 2 program and adds the suffix flr If the file with such a name does not exist it assumes that the file contains a Prolog program and tries the suffix P Otherwise it tries xwam in the hope that an executable Pr
245. y returns with success or failure depending on the outcome of the first call because Prolog will simply look it up in a table Thus if the predicate is intended to perform the side effect each time it is called it will not operate correctly Object oriented programs often rely on methods that produce side effects or make updates In FLORA 2 we call such methods procedural Because by default FLORA 2 tables everything that looks like an F molecule these procedural methods are potentially subject to the aforesaid problem To sidestep this problem FLORA 2 introduces a new syntax to identify procedural methods by allowing the 4 sign in front of a procedural method For instance the following rule defines an output method that for every object writes out its oid 70 Zoutput write 0 _prolog Like boolean methods procedural methods can take arguments but do not return any values The only difference is that procedural methods are not tabled while boolean methods are 17 3 1 Procedural Signatures Procedural methods can have signatures like other kinds of methods For noninheritable Boolean methods signatures are specified as follows Class gt Meth FLORA 2 does not support inheritable procedural methods at present but the syntax permits signatures for such methods which are just ignored C gt Meth 17 FLORA 2 AND TABLING 71 17 4 Operational Semantics of FLORA 2 Although FLORA 2 is a declarative la
Download Pdf Manuals
Related Search
Related Contents
User`s manual FLIR VP50/VP52 マルチ動画横断検索 サイト運営システム - bestprice NVR User Manual go! control_user_v1.9_spa Istruzioni d`uso Manuel d'utilisation カタログダウンロード(PDF:1.3MB) Peavey X-VCA Music Mixer User Manual Copyright © All rights reserved.
Failed to retrieve file