slides01-8 - Review of Logic as a Query Language Datalog...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Review of Logic as a Query Language Datalog programs are collections of rules, which are Horn clauses or if-then expressions. Example The following rules express what is needed to make" a le. It assumes these relations or EDB extensional database predicates are available: 1. sourceF : F is a source le, i.e., stored in the le system. 2. includesF; G: le F includes le G. 3. createF; P; G: we create le F by applying process P to le G. reqF,F reqF,G reqF,G reqF,G Rules ::::- sourceF includesF,G createF,P,G reqF,H & reqH,G Head :- Body is read if" Atom = predicate applied to arguments. Head is atom. Body is logical AND of zero or more atoms. Atoms of body are called subgoals. Head predicate is IDB intensional database = predicate de ned by rules. Body subgoals may have IDB or EDB predicates. Datalog program = collection of rules. One IDB predicate is distinguished and represents result of program. :- Meaning of Rules The head is true for its arguments whenever there exist values for any local variables those that appear in the body, but not the head that make all the subgoals true. Extensions 1. Negated subgoals. Example: cycleF :- reqF,F & NOT sourceF 2. Constants as arguments. Example: 1 reqF,"stdio.h" :- typeF,"cCode" 3. Arithmetic subgoals. Example: compositeA :- dividesB,A & B 1&B A 3 Opposite of an arithmetic atom is a relational atom. Applying Rules  Naive Evaluation" Given an EDB: 1. Start with all IDB relations empty. 2. Instantiate with constants variables of all rules in all possible ways. If all subgoals become true, then infer that the head is true. 3. Repeat 2 in rounds," as long as new IDB facts can be inferred. 2 makes sense and is nite, as long as rules are safe = each variable that appears anywhere in the rule appears in some nonnegated, nonarithmetic subgoal of the body. Limit of 1 3 = Least xed point of the rules and EDB. Seminaive Evaluation More e cient approach to evaluating rules. Based on principle that if at round i a fact is inferred for the rst time, then we must have used a rule in which one or more subgoals were instantiated to facts that were inferred on round i , 1. Thus, for each IDB predicate p, keep both relation P and relation P ; the latter represents the new facts for p inferred on the most recent round. Outline of SNE Algorithm 1. Initialize IDB relations by using only those rules without IDB subgoals. 2. Initialize the -IDB relations to be equal to the corresponding IDB relations. 3. In one round, for each IDB predicate p: 2 a Compute new P by applying each rule for p, but with one subgoal treated as a -IDB relation and the others treated as the correct IDB or EDB relation. Do for all possible choices of the -subgoal. b Remove from new P all facts that are already in P . c P := P P . 4. Repeat 3 until no changes to any IDB relation. Example 1 2 3 4 reqF,F reqF,G reqF,G reqF,G ::::- sourceF includesF,G createF,P,G reqF,H & reqH,G Assume EDB relations S , I , C and IDB relation R, with obvious correspondence to predicates. Initialize: R := R := 1=2 S  S  I 1 3C . Iterate until R = ;: 1. R := 1 3R . R R . R 2. R := R , R 3. R := R R ; ; Models Model of rules + EDB facts = set of ground atoms selected to be true such that 1. An EDB fact is selected true i it is in the given EDB relation. 2. All rules become true under any instantiation of the variables. 3 Facts not stated true in the model are assumed false. 3 Only way to falsify a rule is to make each subgoal true and the head false. Minimal model = model + no proper subset is a model. 3 For a Datalog program with only nonnegated, relational atoms in the bodies, the unique minimal model is what naive or seminaive evaluation produces, i.e., the IDB facts we are forced to deduce. Moreover, this LFP is reached after a nite number of rounds, if the EDB is nite. Function Symbols Terms built from 1. Constants. 2. Variables. 3. Function symbols applied to terms as arguments. 3 Example: ,  addr streetmaple; number101 Example Binary trees de ned by isTreenull isTreenodeL,T1,T2 :labelL & isTreeT1 & isTreeT2 If labela and labelb are true, infers facts like ,  isTree nodea; null; null ,  isTree node b; null; nodea; null; null Application of rules as for Datalog: make all possible instantiations of variables and infer head if all subgoals are true. LFP is still unique minimal model, as long as subgoals are relational, nonnegated. But LFP may be reached only after an in nite number of rounds. Problems for Datalog With Negation Recall extra safety condition: variables in a negated subgoal must appear also in a nonnegated subgoal. 4 Apply rule as without negation: search for substitutions that make all subgoals true. Resulting head is true. 3 But | a subgoal NOT S is true i S is false. Example Failed attempt to express X is a bachelor i there does not exists a person Y such that X is married to Y ": Neither safe, nor correct. bachelorX :- personX & NOT marriedX,Y Suppose fa; b; cg are persons, and marrieda; b. Substitution X ! a, Y ! c makes both subgoals true and lets us infer bachelora incorrectly." The following is a safe" version of the incorrect program, which makes it clearer why the above interpretation is right: bachelorX :- personX & personY & NOT marriedX,Y Correct version: spouseX :- marriedX,Y bachelorX :- personX & NOT spouseX Multiple Minimal Models EDB = redX; Y , greenX; Y . IDB = greenPathX; Y , monopolyX; Y . 1 greenPathX,Y :- greenX,Y 2 greenPathX,Y :greenPathX,Z & greenPathZ,Y 3 monopolyX,Y :- redX,Y & NOT greenPathX,Y EDB data: red1; 2, red2; 3, green1; 2. green 1 red 2 5 red 3 Model1: greenPath1; 2 + monopoly2; 3 + EDB. Model2: greenPath1; 2 + greenPath2; 3 + greenPath1; 3 + EDB. Both are minimal. Dependency Graph Nodes = predicates. Arc p ! q if there is a rule with predicate p in the head and predicate q in some subgoal. Arc p ! q with label |" if there is a rule with p in the head and a negated subgoal with q. Example Arcs for monopoly" program: monopoly greenPath green red Strati ed Logic Models Stratum of a predicate p = largest number of | arcs on a path in dependency graph originating at p. Thus, If p depends negatively on q, then stratump stratumq. If there are no cycles involving negation i.e., no recursive negation, then all strata are nite. If a logic program has no recursive negation, it is strati ed. Example: The Win" Program This Datalog program represents winning positions in a board game, e.g., Nim, where you win by 6 giving your opponent a position with no legal move. winX :- moveX,Y & NOT winY Win" is not strati ed. win move Strati ed Models For strati ed programs, the strati ed model is computed bottom-up." 3 Work from lowest strata to highest. 3 Compute the LFP for a stratum assuming subgoal NOT pX1 ; : : :; X  is true i pX1 ; : : :; X  is false in the LFP for the stratum of p. For monopoly," Model 1 is the strati ed model. Win" has no strati ed model. n n 7 ...
View Full Document

This document was uploaded on 01/06/2012.

Ask a homework question - tutors are online