1 / 16

Predicate Calculus

Predicate Calculus. Formal language True/False statements Supports reasoning Usage Integrity constraints Non-procedural query languages “what” rather than “how” SQL (helps with formulation of some harder queries) Prolog Model theory (basis for design theory, techniques & tools).

ouida
Download Presentation

Predicate Calculus

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Predicate Calculus • Formal language • True/False statements • Supports reasoning • Usage • Integrity constraints • Non-procedural query languages • “what” rather than “how” • SQL (helps with formulation of some harder queries) • Prolog • Model theory (basis for design theory, techniques & tools)

  2. Syntax – Symbols • Truth Values: T F • Constants • Variables • Functions: return a value • Predicates: return T or F • Logical Connectors • Quantifiers:   • xP(x) = P(x1)  P(x2)  … • xP(x) = P(x1)  P(x2)  ... • Parentheses P Q P PQ PQ PQ PQ F F T F F T T F T T F T T F T F F F T F F T T F T T T T

  3. Syntax – Terms & Atoms • Terms: yield a value • Each variable and constant is a term. • Each function is a term (e.g., f(t1, t2) where t1 and t2 are terms). • Atoms: yield T or F • T • F • Each predicate is an atom (e.g., p(t1, t2) where t1 and t2 are terms).

  4. Syntax – Formulas • Formulas • An atom is a formula. • If P & Q are formulas, so are (P), (PQ), (PQ), (PQ), (PQ) • If P is a formula and x is a variable, x(P) and x(P) are formulas • Example: x(x+y = 10  zx(x > z)) • bound and free variables • scope of a variable • closed and open formulas

  5. Semantics • Interpretation • Specify domain D • Assign values in D to: • Constants (unique name assumption: literals denote themselves & constant symbols like  have only one value) • n-ary functions: f:Dn D (e.g., +, -, …) • n-ary predicates: p:Dn {T, F} (e.g., <, =, …) • Evaluation • Closed formulas: evaluation yields T or F • Open formulas: evaluation yields set of domain elements • Examples • D = {0, …, 99} • x(2x mod 100 > 95) evaluates to T • x(2x mod 100 > 95) evaluates to F • 2x mod 100 > 95 yields {48, 49, 98, 99}

  6. Counting Quantifiers • Notational shorthand • 1x(P(x)) T if there is exactly 1 value for which P is true • 2x(P(x)) … exactly 2 … • 5x(P(x)) … 5 or more … • <3x(P(x)) … less than 3 … • Equivalent expressions • 0x(P(x))  x(P(x))  x(P(x)) • 1x(P(x))  x(P(x))  xy(P(x)  P(y)  x = y)

  7. Model Theory & Relational DBs • Let relation names be predicates with a place for each attribute. • Write integrity constraints as closed formulas. • Example: Interpretation: Domain = {1,2,3} r(1, 2, 3) = T r(2, 2, 2) = T r(x, y, z) = F (for all others) x1<y, z>(r(x, y, z)) r(A, B, C) 1 2 3 2 2 2 A is a Key.

  8. Model Theory, OSM, & Rel. DBs Room(x) has Cost(y) Cost(x) is equivalent to Amount(y) in Currency(z) R1 90 90 150 Mark R2 80 90 225,000 Drachma R3 80 80 133 Mark 80 200,000 Drachma • Relations: • Integrity Constraints: • Key Constraints • x1y(Room(x) has Cost(y)) • xy1z(Cost(x) is equivalent to Amount(z) in Currency(y)) • xy1z(Cost(z) is equivalent to Amount(x) in Currency(y)) • Referential-Integrity Constraints • xyz(Cost(x) is equivalent to Amount(y) in Currency(z)  w(Room(w) has Cost(x))) • Attribute-Domain Constraints • x(yz(Cost(y) is equivalent to Amount(z) in Currency(x)  x  {Mark, Drachma, …}) • ...

  9. Valid Interpretations • Interpretation • Domain • all values in the current DB • domain-closure assumption: these are the only elements that can be substituted for variables • Value Assignments • assign constants to themselves • use the closed-world assumption (tuple substitutions for predicates yield T, and all other substitutions yield F) • Valid Interpretation • All closed formulas evaluate to T • DB integrity: the constraints of the DB are satisfied • Examples: the previous two examples show valid interpretations

  10. Relational Calculus • Queries are open formulas, • General form: { <x1, …, xn> | F(x1, …, xn) } • Examples: List $80 rooms. Room R2 R3 { <x> | Room(x) has Cost(80) } List room costs in Marks. { <x, y> | z(Room(x) has Cost(z)  Cost(z) is equivalent to Amount(y) in Currency(Mark)) } Room Amount R1 150 R2 133 R3 133

  11. Relational CalculusBasic Project-Select-Join Examples Get room information. { <x, y, z, w> | r(x, y ,z, w) } Get room number and room name of rooms that cost less than $85 and have 2 beds. { <x, y> | z(r(x, y, 2, z)  z < 85) } Get name and address of guests arriving on 10 May. { <x, y, z> | uvw(g(u, x, y, z)  s(u, v, 10 May, w)) }

  12. Relational CalculusJoin, Renaming & Union Examples Get name and address of guests who have a reservation for a room whose name is the same as the guest’s name. { <x, y, z> | abcdef(g(a, x, y, z)  r(b, x, c, d)  s(a, b, e, f)) } Get name and address of guests who have reservations for more than two days or reservations for two-bed rooms. { <x, y:StreetAddr, z:Location> | abcdef( g(a, x, y, z)  s(a, b, c, d)  (d > 2  r(b, e, 2, f))) }

  13. Relational CalculusNegation Get guest number and name of guests not from Boston. { <x, y> | zw(g(x, y, z, w)  w  Boston) } Get guest number of guests who do not have a reservation for room 1. The following is not correct. { <x> | yzw(s(x, y, z, w)  y  1) }

  14. Relational CalculusNegation and Universal Quantification Get guest number of guests who do not have a reservation for room 1. (continuation of example) Find those who do and negate. { <x> | yzw(s(x, y, z, w)  y = 1) } = { <x> | yzw(s(x, y, z, w)  y = 1) } = { <x> | yzw(s(x, y, z, w)  y  1) } Almost correct, but yields universal complement. Restrict by using relative complement. { <x> | tuv(g(x, t, u, v)  yzw(s(x, y, z ,w)  y = 1)) }

  15. Relational CalculusUniversal Quantification Get name and address of guests who have reservations for all presidential suites (rooms with two beds). { <x, y, z> | w(g(w, x, y, z)  abc(r(a, b, 2, c)  de(s(w, a, d, e)))) }

  16. Universal Quantificationand SQL Queries { <x, y, z> | w(g(w, x, y, z)  abc(r(a, b, 2, c)  de(s(w, a, d, e)))) } = { <x, y, z> | w(g(w, x, y, z)  abc(r(a, b, 2, c)  de(s(w, a, d, e)))) } = { <x, y, z> | w(g(w, x, y, z)  abc(r(a, b, 2, c)  de(s(w, a, d, e)))) } = select Name, StreetNr, City from Guest g where not exists (select * from Room r where NrBeds = 2 and not exists (select * from Reservation s where g.GuestNr = s.GuestNr and r.RoomNr = s.RoomNr))

More Related