360 likes | 527 Views
CS421 – Logic Programming (Unification). Based on slides by Mattox Beckman, updated by Vikram Adve, Gul Agha, Elsa Gunter. Contents. Logics Logic Programming Unification. Propositional Logic. Propositions P, Q, R Operations AND, OR, NOT, IMPLIES, EQUIV Models Propositions → { T, F }.
E N D
CS421 – Logic Programming(Unification) Based on slides by Mattox Beckman, updated by Vikram Adve, Gul Agha, Elsa Gunter
Contents • Logics • Logic Programming • Unification
Propositional Logic • Propositions • P, Q, R • Operations • AND, OR, NOT, IMPLIES, EQUIV • Models • Propositions → { T, F }
Predicate Logic • Terms • Objects – O = { o1, o2, … } • Functions – F : On→ O • Predicates – P : O → { T, F } • Operations • + Quantifiers • Models
Horn Clauses • H ← T1 & T2 & T3 & … • Each element a predicate • Inference • Backwards Chaining • Forwards Chaining
Example • Graduated(s) ← Quals(s) & Thesis(s) • Quals(s) ← LangR(s) & SystemR(s) & AreaR(s) & TheoryR(s) • LangR(s) ← CS421(s) • TheoryR(s) ← CS476(s) • CS421(P), CS476(P), CS423(P), …
Logic Programming • Inference on Horn Clauses • Facts: H ← • Rules: H ← T1 & T2 & … • Unification • Back Tracking
Example • (append empty x x) ← • (append (cons w x) y (cons w z)) ← (append x y z) • (append q r (cons 1 (cons 2 empty)))
Example • (append empty x x) ← • (append (cons w x) y (cons w z)) ← (append x y z) • (append q r (cons 1 (cons 2 empty))) • q = empty • r = (cons 1 (cons 2 empty)) • x = (cons 1 (cons 2 empty))
Example • (append empty x x) ← • (append (cons w x) y (cons w z)) ← (append x y z) • (append q r (cons 1 (cons 2 empty))) • q = (cons 1 x), w = 1, y = r • z = (cons 2 empty) • (append x r (cons 2 empty))
Unification • Terms • Constructors/Constants • Variables • Substitution • Variable Assignment: Variables → Terms Term → Constant → Variable → Constructor(Term1, Term2, …)
Unification Problem • { s1 = t1, s2 = t2, …, sn = tn } • Find unification solution σ • σ(si) = (ti), for all i = 1, …, n
Unification Algorithm • S = { s1 = t1, s2 = t2, …, sn = tn } • S = { } → Unif(S) = Identity • S = { s = t S’ } • Delete: if s = t • Orient: if s = x (x is a variable, s is not a variable) Unif(S) = Unif({ x = s S’ }) • Decompose: if c(q1, q2, …, qn) = c(r1, r2, …, rn) Unif(S) = Unif({ q1=r1 q2=r2 … qn=rn S’ }) • Eliminate: if x = t (x does not occur in t)
Unification • Eliminate: if x = t (x does not occur in t) • f = x | t • g = Unif(f(S’)) • Unif(S) = { x | g(t) } o g • Bad example: x = (cons 1 x)
Unification Example • x, y, z variables, f, g constructors • S = { g(y, f(y)) = x, f(x) = f(g(y, z)) }
Unification Example • x, y, z variables, f, g constructors • Pick: g(y, f(y)) = x • S = { g(y, f(y)) = x, f(x) = f(g(y, z)) }
Unification Example • x, y, z variables, f, g constructors • Pick: g(y, f(y)) = x • Orient: x = g(y, f(y) • S = { g(y, f(y)) = x, f(x) = f(g(y, z)) }
Unification Example • x, y, z variables, f, g constructors • S = { x = g(y, f(y)), f(x) = f(g(y, z)) }
Unification Example • x, y, z variables, f, g constructors • Pick: x = g(y, f(y)) • S = { x = g(y, f(y)), f(x) = f(g(y, z)) }
Unification Example • x, y, z variables, f, g constructors • Pick: x = g(y, f(y)) • Eliminate • S = { x = g(y, f(y)), f(x) = f(g(y, z)) }
Unification Example • x, y, z variables, f, g constructors • S = { f(g(y, f(y))) = f(g(y, z)) } • x |→ g(y, f(y))
Unification Example • x, y, z variables, f, g constructors • Pick: f(g(y, f(y))) = f(g(y, z)) • S = { f(g(y, f(y))) = f(g(y, z)) } • x |→ g(y, f(y))
Unification Example • x, y, z variables, f, g constructors • Pick: f(g(y, f(y))) = f(g(y, z)) • Decompose: g(y, f(y)) = g(y, z) • S = { f(g(y, f(y))) = f(g(y, z)) } • x |→ g(y, f(y))
Unification Example • x, y, z variables, f, g constructors • S = { g(y, f(y)) = g(y, z) } • x |→ g(y, f(y))
Unification Example • x, y, z variables, f, g constructors • Pick: g(y, f(y)) = g(y, z) • S = { g(y, f(y)) = g(y, z) } • x |→ g(y, f(y))
Unification Example • x, y, z variables, f, g constructors • Pick: g(y, f(y)) = g(y, z) • Decompose: y = y, f(y) = z • S = { g(y, f(y)) = g(y, z) } • x |→ g(y, f(y))
Unification Example • x, y, z variables, f, g constructors • S = { y = y, f(y) = z } • x |→ g(y, f(y))
Unification Example • x, y, z variables, f, g constructors • Pick: y = y • S = { y = y, f(y) = z } • x |→ g(y, f(y))
Unification Example • x, y, z variables, f, g constructors • Pick: y = y • Delete • S = { y = y, f(y) = z } • x |→ g(y, f(y))
Unification Example • x, y, z variables, f, g constructors • S = { f(y) = z } • x |→ g(y, f(y))
Unification Example • x, y, z variables, f, g constructors • Pick: f(y) = z • S = { f(y) = z } • x |→ g(y, f(y))
Unification Example • x, y, z variables, f, g constructors • Pick: f(y) = z • Orient: z = f(y) • S = { f(y) = z } • x |→ g(y, f(y))
Unification Example • x, y, z variables, f, g constructors • S = { z = f(y) } • x |→ g(y, f(y))
Unification Example • x, y, z variables, f, g constructors • Pick: z = f(y) • S = { z = f(y) } • x |→ g(y, f(y))
Unification Example • x, y, z variables, f, g constructors • Pick: z = f(y) • Elimination • S = { z = f(y) } • x |→ g(y, f(y))
Unification Example • x, y, z variables, f, g constructors • S = { } • x |→ g(y, f(y)) • z |→ f(y)