DEFINITION OF RELATIONAL PROGRAMMING LANGUAGE 3 kinds of programming languages: 1. imperative (e.g. C++, Java, any assembler, etc.) 2. functional (e.g. Lisp) 3. relational (e.g. Prolog) APE is a relational language. In an imperative language, the basic building blocks are: 1. assignment: a = b means put value of b in a (imperative = do something!) 2. evaluation of functions/expressions (2 + 2, f(x), etc.) In a functional language, the basic building block is: 1. evaluation of functions So you can think of every program as a hierarchical set of functions. In a relational language, the basic building blocks are: 1. determination of truth/falsity 2. determining consistency of expressions (= matching = unification) So instead of thinking of "what you want the program to do", you can think of "how are the input & output related". ---------------------------------------------------------- SEMANTICS OF RELATIONAL EXPRESSIONS Relations without variables are facts about the world. They can be stored in a DB: (is-red apple) Two different ways to say the same thing: (my-counter-is 0) (has-value my-counter 0) Relation names (and no. of arguments) are arbitrary, just like variable names. Some more facts: (+ 2 2 4) (+ 2 3 5) ---------------------------------------------------------- HOW ARE RELATIONS WITH VARIABLES USED? They are used in preconditions (if-statements) and recipe items (then-statements). All of the preconditions must be true for a precondition to be true. In addition, the goal must match the top of the agenda. If more than one rule applies, the last matching rule is chosen (so default rules must come first). If a rule is chosen, all of its recipe items will be executed, in order. Example: how to update a counter: If (count == 0) then count = 1; :precond ( ... (count-is 0) ...) :recipe (... (retract (no-error (count-is ?x))) (assert (count-is 1)) ...) assert = adds fact to DB retract = removes _all_ matching facts from DB What does the no-error term above do? It prevents an error from being signaled if there are no matching facts to retract. Its use is optional. ---------------------------------------------------------- A RELATION SERVES THE PURPOSE OF MANY DATA STRUCTURES A relation is a way of declaring a variable. Example: (count-is 3) is an easy way to declare a counter and give it the value 3. A relation is also a way of declaring an unordered list: Example: unordered list of 3 red items: (is-red apple) (is-red strawberry) (is-red raspberry) How can you declare an ordered list (i.e., a queue)? You could assign them sequence numbers: (is-red 1 apple) (is-red 2 strawberry) (is-red 3 raspberry) Or you could build an internal list inside a relation: (is-red-list (apple strawberry raspberry)) A relation is also a useful way of declaring a table: (has-feet foot 1) (has-feet yard 3) (has-feet mile 5280) ---------------------------------------------------------- WHY ARE MULTIPLE FACTS WITH THE SAME RELATION PERMITTED? IMPORTANT FACT: 'Assert' _adds_ a fact to the DB. It _does not_ replace any existing facts. Why not? Because multiple facts might be acceptable. In other words, the system doesn't know whether you are thinking of a variable or a list. Example: This doesn't make sense: (is-count 0) (is-count 1) But this does: (is-red apple) (is-red strawberry) (is-red raspberry) A more general way to update that counter: :precond ( ... (count-is ?x) (+ ?x 1 ?y) ...) :recipe (... (retract (count-is ?z)) (assert (count-is ?y)) ...) ---------------------------------------------------------- HOW DO WE KNOW IF SOMETHING (A RELATIONAL EXPRESSION) IS TRUE? Short answer: It matches something (a relational expression) in the DB. Long answer: 1. It genuinely matches something (a relational expression) in the DB. or 2. A module in the DB interface evaluates the expression and returns true or false using the same layout the DB would have used. Why do we need the second category? a) We would like to be able to do arithmetic: (+ 2 2 4) -> true (+ 2 3 5) -> true (+ 2 3 9) -> false But we can't put all the possible cases in the DB. (Why not?) b) Perhaps we would like to look up facts in an external DB, e.g. Oracle, DB/2, etc. This is similar to using an external DB from a web page. c) Or call a function from an external library, etc. ---------------------------------------------------------- CAN WE BUILD MORE COMPLEX RELATIONAL EXPRESSIONS? NOT: Yes, example: (not (= ?x 3)) AND: Default when there are more than one precondition OR: Write 2 rules, one for each case So we can have any kind of desired if-condition without getting involved in complex syntax. ---------------------------------------------------------- HOW DO WE DECIDE WHICH RULE TO USE NEXT? 1. Goal of rule must match top of agenda 2. Each precondition must be true. 3. Then choose last matching rule (so default must come first). The initial goal on the agenda is supplied by the user. ---------------------------------------------------------- WHAT DOES IT MEAN FOR TWO FORMULAS TO MATCH? The system uses the unification algorithm.