CS589 Principles of DB Systems Fall 2008 Lecture 4a: Introduction to Datalog Lois Delcambre lmd@cs.pdx.edu 503 725-2405.

Slides:



Advertisements
Similar presentations
1 Datalog: Logic Instead of Algebra. 2 Datalog: Logic instead of Algebra Each relational-algebra operator can be mimicked by one or several Database Logic.
Advertisements

First Order Logic Logic is a mathematical attempt to formalize the way we think. First-order predicate calculus was created in an attempt to mechanize.
Relational Calculus and Datalog
Lecture 11: Datalog Tuesday, February 6, Outline Datalog syntax Examples Semantics: –Minimal model –Least fixpoint –They are equivalent Naive evaluation.
CPSC 504: Data Management Discussion on Chandra&Merlin 1977 Laks V.S. Lakshmanan Dept. of CS UBC.
1 Logic Programming. 2 A little bit of Prolog Objects and relations between objects Facts and rules. Upper case are variables. parent(pam, bob).parent(tom,bob).
Answer Set Programming Overview Dr. Rogelio Dávila Pérez Profesor-Investigador División de Posgrado Universidad Autónoma de Guadalajara
CS 330 Programming Languages 12 / 02 / 2008 Instructor: Michael Eckmann.
Chapter 12 - Logic Programming
Outline Recap Knowledge Representation I Textbook: Chapters 6, 7, 9 and 10.
Computability and Complexity 8-1 Computability and Complexity Andrei Bulatov Logic Reminder.
Lecture 3 [Self Study] Relational Calculus
The Relational Model: Relational Calculus
CSE314 Database Systems The Relational Algebra and Relational Calculus Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
CMU SCS Carnegie Mellon Univ. Dept. of Computer Science Database Applications Lecture#6: Relational calculus.
CS344: Introduction to Artificial Intelligence Lecture: Herbrand’s Theorem Proving satisfiability of logic formulae using semantic trees (from Symbolic.
CS621: Artificial Intelligence Pushpak Bhattacharyya CSE Dept., IIT Bombay Lecture 28– Interpretation; Herbrand Interpertation 30 th Sept, 2010.
Relational Calculus R&G, Chapter 4. Relational Calculus Comes in two flavors: Tuple relational calculus (TRC) and Domain relational calculus (DRC). Calculus.
CS Introduction to AI Tutorial 8 Resolution Tutorial 8 Resolution.
Dr. Muhammed Al-Mulhem ICS An Introduction to Logical Programming.
Logic Programming Languages Session 13 Course : T Programming Language Concept Year : February 2011.
COMP 170 L2 Part 3 of Course Chapter 3 of Textbook.
CS6133 Software Specification and Verification
CMU SCS Carnegie Mellon Univ. Dept. of Computer Science /615 – DB Applications C. Faloutsos & A. Pavlo Lecture#5: Relational calculus.
Copyright © Curt Hill The Relational Calculus Another way to do queries.
1-1 An Introduction to Logical Programming Sept
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 26 A First Course in Database Systems.
6/11/2016 Linear Resolution and Introduction to First Order Logic Michael Leuschel Softwaretechnik und Programmiersprachen Lecture 5.
CS589 Principles of DB Systems Fall 2008 Lecture 4d: Recursive Datalog with Negation – What is the query answer defined to be? Lois Delcambre
CS589 Principles of DB Systems Fall 2008 Lecture 4c: Query Language Equivalence Lois Delcambre
CS589 Principles of DB Systems Fall 2008 Lecture 4b: Domain Independence and Safety Lois Delcambre
CENG 424-Logic for CS Introduction Based on the Lecture Notes of Konstantin Korovin, Valentin Goranko, Russel and Norvig, and Michael Genesereth.
Relational Calculus Chapter 4, Section 4.3.
Relational Algebra & Calculus
CSE202 Database Management Systems
Propositional Logic (a.k.a. Sentential Logic)
By P. S. Suryateja Asst. Professor, CSE Vishnu Institute of Technology
CS589 Principles of DB Systems Spring 2014 Unit 2: Recursive Query Processing Lecture 2-1 – Naïve algorithm for recursive queries Lois Delcambre (slides.
Lois Delcambre (slides from Dave Maier)
An Algebraic Query Language
Knowledge and reasoning – second part
Relational Calculus Chapter 4, Part B
Goal for this lecture Demonstrate how we can prove that one query language is more expressive than (i.e., “contained in” as described in the book) another.
Logics for Data and Knowledge Representation
Database Applications (15-415) Relational Calculus Lecture 6, September 6, 2016 Mohammad Hammoud.
Lecture#5: Relational calculus
Relational Calculus.
Relational Calculus and QBE
Carlos Varela Rensselaer Polytechnic Institute November 10, 2017
Horn Clauses and Unification
Knowledge and reasoning – second part
Back to “Serious” Topics…
CS 186, Fall 2002, Lecture 8 R&G, Chapter 4
Logic Based Query Languages
Relational Calculus and QBE
Chapter 1: Propositional and First-Order Logic
Propositional Logic CMSC 471 Chapter , 7.7 and Chuck Dyer
Predicate Logic: Syntax.
Relational Algebra & Calculus
Predicate Logic.
Chapter 27: Formal-Relational Query Languages
Relational Calculus Chapter 4, Part B 7/1/2019.
Representations & Reasoning Systems (RRS) (2.2)
CS589 Principles of DB Systems Fall 2008 Lecture 4a: Introduction to Datalog Lois Delcambre
CS589 Principles of DB Systems Fall 2008 Lecture 4e: Logic (Model-theoretic view of a DB) Lois Delcambre
CS589 Principles of DB Systems Fall 2008 Lecture 4b: Domain Independence and Safety Lois Delcambre
CS589 Principles of DB Systems Fall 2008 Lecture 4d: Recursive Datalog with Negation – What is the query answer defined to be? Lois Delcambre
Relational Calculus Chapter 4 – Part II.
Relational Calculus Chapter 4, Part B
Presentation transcript:

CS589 Principles of DB Systems Fall 2008 Lecture 4a: Introduction to Datalog Lois Delcambre lmd@cs.pdx.edu 503 725-2405

Goals for today Introduce you to Datalog queries Briefly introduce the various versions of Datalog Explain how Datalog queries are evaluated

Datalog – a query language based on definite Horn logic clauses Datalog is a query language A Datalog program consists of one or more clauses (also called rules) Datalog syntax is the same as Prolog but without functions and without the extralogical features such as Cut and Fail. The order of clauses does not matter in Datalog; the order of literals in the body of a rule does not matter; the query answer will be the same.

Example Datalog Query Student(s-id, s-name, major, f-id, age) Faculty(f-id, f-name, rank, dept) Query 1: Result (x, y) :- Student(s,x,m,f, a),Faculty(f,y,r,d). Write an equivalent query in domain calculus: {x, y | s(m(f(a(r(d( Student(s,x,m,f, a)  Faculty(f,y,r,d)))))))} Write an equivalent query in relational algebra: s-name,f-name(Student |X| Faculty) Literals separated by commas are “ANDed” together Choosing variables for answer (project) join

Example Datalog Query 2 Student(s-id, s-name, major, f-id, age) Faculty(f-id, f-name, rank, dept) Query 2: Result (x,y) :- Student(x,y,”CS”,f,a). Write an equivalent query in domain calculus: {x, y | f(a(Student(x, y,”CS”,f, a))) } Write an equivalent query in relational algebra: s-id, sname(major=“CS”Student) Constant of “CS” selects students with major = “CS” Choosing variables for answer (project)

Exercise Student(s-id, s-name, major, f-id, age) Faculty(f-id, f-name, rank, dept) Query 3: Answer(x) :- Student(a,x,b,c,21),Faculty(c,d,e,”CS”). Write an equivalent query in domain calculus: Write an equivalent query in relational algebra:

Comparing domain calculus & Datalog Student(s-id, s-name, major, f-id, age) { a, b, c | x(y(Student(a,b,c,x,y))) } This expression defines a set (query answer). The tuple <a,b,c> is in this set provided there exists an x and a y where the tuple <a,b,c,x,y> is in relation student (with relation schema Student). Result (a,b,c) :- Student(a,b,c,x,y). This is a definite Horn clause that says “If Student(a,b,c,x,y) is true, then Result(a,b,c) is true.” In a Horn clause, every variable is universally quantified. This clause is the same as: (a)(b)(c)(x)(y)(Student(a,b,c,x,y) → Result(a,b,c)) So … are these expressions defining the same query?

Implication (quick reminder) Any logical connector can be defined using a truth table. Here we show the truth table for ^ (and) and for  (implication). p q p^q true false p q pq true false (a)(b)(c)(x)(y)(Student(a,b,c,x,y) → Result(a,b,c) ) If ever the left part is true, then the right part must be true. Result (a,b,c) :- Student(a,b,c,x,y). If the body (the right hand side) is true, then the head (left hand side) must be true. Evaluation of Datalog actively looks for tuples that satisfy the body.

Exercise Student(s-id, s-name, major, f-id, age) Faculty(f-id, f-name, rank, dept) Write a Datalog query that is equivalent to the following Relational Algebra query: s-name(s-age=21Student)  f-name(rank=“Professor”Faculty)

Datalog Example – for Union Student(s-id, name, major, f-id, age) Faculty(f-id, name, rank, dept) Ans(x,y) :- Student(x,y,a,b,c). Ans(x,y) :- Faculty(x,y,d,e). This is a Datalog program consisting of two rules. They both fire and produce Ans tuples. This query is equivalent to the following: (s-id, nameStudent) U (f-id, nameFaculty)

Exercise Student(s-id, s-name, major, f-id, age) Faculty(f-id, f-name, rank, dept) Write a Datalog query that is equivalent to: SELECT * FROM Student, Faculty;

Exercise Grad-course (c-num, title, credits) Undergrad-course (c-num, title, credits) Write a Datalog query that is equivalent to: (Grad-course)  (Undergrad-course)

Exercise Grad-course (c-num, title, credits) Undergrad-course (c-num, title, credits) Write a Datalog query that is equivalent to: Grad-course – Undergrad-course

Datalog with negation Student(s-id, name, major, f-id, age) Faculty(f-id, name, rank, dept) No-advisees(x,y) :- Faculty(x,y,a,b),Student(c,d,e,x,f). Show the f-id and name for any faculty tuple for which there does not exist a Student tuple advised by this faculty member. What is an equivalent relational algebra query?

Exercise Write each of these queries in Datalog. Student(s-id, s-name, major, f-id, age) Faculty(f-id, f-name, rank, dept) (name(sage=21Student))  nameFaculty Student |X| Faculty (name(sage=21Student)) – (name(smajor=“CS”Student))

Datalog syntax Atomic formulas: Literal: R(y1, y2, …, yk) – a predicate formula x = y (Note: this is syntatic sugar for equal(x,y).) R(v1, v2, …, vk) – a ground atomic formula, where vi are values Literal: an atomic formula (positive literal) or the negation of an atomic formula (negative literal) Clause (Datalog rule): L :- L1, L2, …, Ln. where L is a predicate formula and Li, i = 1, …, n, is a literal. (Some versions of Datalog require all literals to be positive literals.) The comma means “and”.

Datalog with recursion (more about this in a future lecture) Parent(p-id, ch-id) Ancestor(x,y) :- Parent-child(x, y). Ancestor(x,z) :- Ancestor(x,y), Parent-child(y,z). How does this Datalog program get evaluated? Fire all rules (from right to left) until you don’t produce any new tuples in Academic-descendant. The book describes the meaning of a program using the “immediate consequence” of a program. Note each Datalog rule is independent. The variable names in separate rules have no connection.

Several Datalog languages Datalog – one rule, no negation, no recursion. Datalog – multiple rules, no negation, no recursion. Datalog – multiple rules, no negation, with recursion. Datalog – multiple rules, with negation, no recursion. Datalog – multiple rules, with negation, with recursion.

Expressive power of Datalog languages (compared to relational algebra) Datalog – one rule, no negation, no recursion. Datalog – multiple rules, no negation, no recursion. Datalog – multiple rules, no negation, with recursion. Datalog – multiple rules, with negation, no recursion. Datalog – multiple rules, with negation, with recursion. Conjunctive queries SPJ SPJU SPJU + recursion but NOT relationally complete SPJU- relationally complete but no recursion Relationally complete with recursion but some queries are ambiguous!

How is Datalog evaluated? Single rule (non-recursive) Datalog without negation (Non-recursive) Datalog without negation (Non-recursive) Datalog with negation Recursive Datalog without negation (see next slide) Recursive Datalog with negation … a whole different topic ….