Logic Programming and Declarative Paradigm Declarative Programming Declare

  • Slides: 40
Download presentation
Logic Programming and Declarative Paradigm

Logic Programming and Declarative Paradigm

Declarative Programming • • Declare goals, not actions Query by Example HTML Prolog –

Declarative Programming • • Declare goals, not actions Query by Example HTML Prolog – AI work – Natural Language Community – Japanese 5’th Generation

Logic Programming • Resolution Principle – Alan Robinson 1965 • Restriction to Horn Clauses

Logic Programming • Resolution Principle – Alan Robinson 1965 • Restriction to Horn Clauses • Kowalski of U. of Edinburgh and Colmerauer of U. Aix-Marseille developed Prolog

Horn Clauses • If x 1& x 2 & x 3 Then r 1

Horn Clauses • If x 1& x 2 & x 3 Then r 1 • r 1 : - x 1 , x 2 , x 3.

Prolog Syntax • parent(X, Y) : - mother(X , Y). • mother(jane, bill). •

Prolog Syntax • parent(X, Y) : - mother(X , Y). • mother(jane, bill). • Note Prolog does not “understand” parent or mother!

Why do we like Prolog?

Why do we like Prolog?

Lists • [a, b, c, d] – a list • [X 1 | X

Lists • [a, b, c, d] – a list • [X 1 | X 2] - concatenations

Append • Append([], Y, Y). • Append([X 1|X 2], Y, [X 1|Z]) : Append(X

Append • Append([], Y, Y). • Append([X 1|X 2], Y, [X 1|Z]) : Append(X 2, Y, Z).

B-Prolog Version 4. 0 #3 (C) Neng-Fa Zhou 1994 -2000. | ? - consult(app).

B-Prolog Version 4. 0 #3 (C) Neng-Fa Zhou 1994 -2000. | ? - consult(app). consulting. . app. pl yes | ? - trace yes {Debug mode} | ? - app([a, b], [c, d], Ans). Call: app([a, b], [c, d], _860124) ? Call: app([b], [c, d], _860250) ? Call: app([], [c, d], _8602 c 4) ? Exit: app([], [c, d]) ? Exit: app([b], [c, d], [b, c, d]) ? Exit: app([a, b], [c, d], [a, b, c, d]) ? Ans = [a, b, c, d]?

| ? - app([a, b, c], Ans, [a, b, c, d, f]). Call: app([a,

| ? - app([a, b, c], Ans, [a, b, c, d, f]). Call: app([a, b, c], _8600 fc, [a, b, c, d, f]) ? Call: app([b, c], _8600 fc, [b, c, d, f]) ? Call: app([c], _8600 fc, [c, d, f]) ? Call: app([], _8600 fc, [d, f]) ? Exit: app([], [d, f]) ? Exit: app([c], [d, f], [c, d, f]) ? Exit: app([b, c], [d, f], [b, c, d, f]) ? Exit: app([a, b, c], [d, f], [a, b, c, d, f]) ? Ans = [d, f]? yes

Symbolic Differentiation dc/dx = 0 dx/dx = 1 d/dx(u+v) = du/dx + dv/dx d/dx(u-v)

Symbolic Differentiation dc/dx = 0 dx/dx = 1 d/dx(u+v) = du/dx + dv/dx d/dx(u-v) = du/dx – dv/dx d/dx(uv) = u dv/dx + v du/dx d/dx(u/v) = (v du/dx – u dv/dx) / v*v d(X, U+V, DU+DV) : - d(X, U, DU), d(X, V, DV). d(X, U-V, DU-DV) : - d(X, U, DU), d(X, V, DV). d(X, U*V, U*DV + V*DU) : - d(X, U, DU) , d(X, V, DV). d(X, U/V, (V*DU - U*DU) / ( V*V)) : - d(X, U, DU), d(X, V, DV). d(X, C, 0) : - atomic(C), C=X. d(X, X, 1).

| ? - d(x, 2*x+1, Ans). Call: d(x, 2*x+1, _860114) ? Call: d(x, 2*x,

| ? - d(x, 2*x+1, Ans). Call: d(x, 2*x+1, _860114) ? Call: d(x, 2*x, _86029 c) ? Call: d(x, 2, _860350) ? Call: atomic(2) ? Exit: atomic(2) ? Call: 2=x ? Exit: d(x, 2, 0) ? Call: d(x, x, _860340) ? Call: atomic(x) ? Exit: atomic(x) ?

Call: x=x ? Fail: x=x ? Redo: atomic(x) ? Fail: atomic(x) ? Exit: d(x,

Call: x=x ? Fail: x=x ? Redo: atomic(x) ? Fail: atomic(x) ? Exit: d(x, x, 1) ? Exit: d(x, 2*1+x*0) ? Call: d(x, 1, _8602 a 0) ? Call: atomic(1) ? Exit: atomic(1) ? Call: 1=x ? Exit: d(x, 1, 0) ? Exit: d(x, 2*x+1, 2*1+x*0+0) ? Ans = 2*1+x*0+0?

Search • Bottom-up resolution – forward search – Rules, facts goal • Top-down resolution

Search • Bottom-up resolution – forward search – Rules, facts goal • Top-down resolution – backward search – Goal -> rules, facts

Prolog Issues • Resolution Order Control • Closed World Assumption • Negation Problem

Prolog Issues • Resolution Order Control • Closed World Assumption • Negation Problem

Expert Systems • In the knowledge is the Power • CLIPS • Prolog based

Expert Systems • In the knowledge is the Power • CLIPS • Prolog based

Implementation of Prolog • Warren’s Abstract Machine • A Tutorial Reconstruction by Hassan Ait.

Implementation of Prolog • Warren’s Abstract Machine • A Tutorial Reconstruction by Hassan Ait. Kaci

Unify Constant c 2 Variable x 2 Structure g(r 1, …rk) Constant c 1

Unify Constant c 2 Variable x 2 Structure g(r 1, …rk) Constant c 1 {} If c 1 == c 2, else fail {x 2/c 1} fail Variable x 1 {x 1/c 2} {x 1/x 2} if (x 1 != x 2, else fail {x 2/g(r 1, . . rk)} if x 2 does not occur in g(…) else fail Structure f(s 1, …sn) fail {x 2/f(s 1, . . sn)} if x 2 does not occur in f(…) else fail Unify ((s 1, . . sk)(r 1. . rn)) if f==g and n==k else fail

Execution • WAM!

Execution • WAM!