cs774 (prasad)l1lp1 programming paradigms logic programming paradigm correctness > efficiency...

Post on 19-Jan-2016

222 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

cs774 (Prasad) L1LP 1

Programming Paradigms

Logic Programming Paradigm

Correctness > Efficiency

t.k.prasad@wright.edu

http://www.knoesis.org/tkprasad/

cs774 (Prasad) L1LP 2

Programming Paradigm

A way of conceptualizing what it means to perform computation and how tasks to be carried out on the computer should be structured and organized.

• Imperative : Machine-model based

• Functional : Equations; Expression Evaluation

• Logical : First-order Logic Deduction

• Object-Oriented : Programming with Data Types

cs774 (Prasad) L1LP 3

Imperative Style vs Declarative Style

• Imperative programs– Description of WHAT is to be computed is

inter-twined with HOW it is to be computed.– The latter involves organization of data and the

sequencing of instructions.

• Declarative Programs– Separates WHAT from HOW.– The former is programmer’s responsibility; the

latter is interpreter’s/compiler’s responsibility.

cs774 (Prasad) L1LP 4

• What : Intent

– Value to be computed: a + b + c• How : Details

– Recipe for computing the value• Intermediate Code

– T := a + b; T := T + c;

– T := b + c; T := a + T;

• Accumulator Machine– Load a; Add b; Add c;

• Stack Machine– Push a; Push b; Add; Push c; Add;

cs774 (Prasad) L1LP 5

Role of variable• In declarative style, a

variable stands for an arbitrary value , and is used to abbreviate an infinite collection of equations.

0 + 0 = 0

0 + 1 = 1

for all x : 0 + x = x

• In imperative style, a variable is a location that can hold a value, and can be changed through an assignment.

x := x + 1;

� Declarative variable can be viewed as assign-only- once imperative variable.

cs774 (Prasad) L1LP 6

Logic Programming Paradigm

• Integrates Data and Control Structures

edge(a,b).

edge(a,c).

edge(c,a).

path(X,X).

path(X,Y) :- edge(X,Y).

path(X,Y) :- edge(X,Z), path(Z,Y).

cs774 (Prasad) L1LP 7

Logic Programming

• A logic program defines a set of relations.

This “knowledge” can be used in various ways by the interpreter to solve different queries.

• In contrast, the programs in other languagesIn contrast, the programs in other languages

also make explicit also make explicit HOWHOW the “declarative the “declarative knowledge” is used to solve the query.knowledge” is used to solve the query.

cs774 (Prasad) L1LP 8

AppendAppend in Prolog

append([], L, L).append([], L, L).

append([ H | T ], L, [ H | R ]) :-append([ H | T ], L, [ H | R ]) :-

append(T, L, R).append(T, L, R).

• True statements about appendappend relation.• “.” and “:-” are logical connectives that stand for

“and” and “if” respectively.

• Uses pattern matching.• “[]” and “|” stand for empty list and cons operation.

cs774 (Prasad) L1LP 9

Different Kinds of Queries

• Verification– sig: list x list x list -> boolean

• append([1], [2,3], [1,2,3]).

• Concatenation– sig: list x list -> list

• append([1], [2,3], R).

cs774 (Prasad) L1LP 10

More Queries

• Constraint solving– sig: list x list -> list

• append( R, [2,3], [1,2,3]).

– sig: list -> list x list• append(A, B, [1,2,3]).

• Generation– sig: -> list x list x list

• append(X, Y, Z).

cs774 (Prasad) L1LP 11

GCD : functional vs imperativePrecondition: n > m >= 0

fun gcd(m,n) = if m=0 then n else gcd(n mod m, m);

function gcd(m,n: int) : int; var pm:int; begin while m<>0 do begin pm := m; m := n mod m; n := pm end; return n end;

GCD: logicPrecondition: n > m >= 0gcd(M, N, N):- M = 0.

gcd(M, N, G):- M \= 0, M =< N,

T is N mod M, gcd(T, M, G).

?-gcd(3,4,G)

G = 1;

false

cs774 (Prasad) L1LP 12

Recursion + Logic Variables• Convenient way of defining functions over

inductively defined sets (e.g., numbers, lists, trees, etc)

• Implicit Stack

• No aliasing problems • Same location cannot be accessed and modified

using two different names

• Use semantics preserving transformations for efficiency

• Role of the interpreter

cs774 (Prasad) L1LP 13

Progression of values bound to a variable as computation progresses

• Imperative language– Essentially independent (subject to typing

constraints)

• Logic language– Values are in instance-of/sub-structure relation

(general -> specific)

cs774 (Prasad) L1LP 14

cs774 (Prasad) L1LP 15

OOPL: Expressive Power vs Naturalness

Object-oriented techniques do not provide any new computational power that permits problems to be solved that cannot, in theory, be solved by other means (Church-Turing Hypothesis).

But object-oriented techniques do make it easier and more natural to address problems in a fashion that tends to favor the management of large software projects.

cs774 (Prasad) L1LP 16

Other Benefits of Programming in a Declarative Language

• Abstraction – Convenient to code symbolic computations and

list processing applications.• Meta-programming (which exploits uniform syntax)

• Automatic storage management • Improves program reliability.• Enhances programmer productivity.

• Ease of prototyping using interactive development environments.

• Executable Specification

Logic Programming Paradigm(cont’d)

cs774 (Prasad) L1LP 17

Logic Program

• Integrates data structures with programs

• Involves asserting properties satisfied by relationships among individuals in a logic language

• Computation is logical deduction – make explicit facts that are implicit, that is, are

logical consequence of input

cs774 (Prasad) L1LP 18

Example

• Prolog Facts (asserting relationships among objects) (cf. ABox)– child(c,p) holds if c is a child of p.

child(tom, john).

child(tom, mary).• Prolog Rules (formalizing relationships) (cf.

TBox)– parent(p,c) holds if p is a parent of c.

parent(P,C) :- child(C,P).cs774 (Prasad) L1LP 19

Querying as Deduction

?- child(tom, john).– Is Tom a child of John?

?- child(X, john).– List children of John, one by one

?- child(X, Y).– List all child-parent pairs, one by one

?- parent(tom, X).– List children of Tom, one by one

cs774 (Prasad) L1LP 20

Two definitions of ancestor relation

• Ancestor-relation is the reflexive transitive closure of the parent-relation.

ancestor(X,X).

ancestor(X,Y) :- parent(X,Y).

ancestor(X,Y) :- parent(X,T), ancestor(T,Y).

ancestor(X,X).

ancestor(X,Y) :- parent(X,Y).

ancestor(X,Y) :- ancestor(X,T), ancestor(T,Y).cs774 (Prasad) L1LP 21

Prolog is not an ideal logic programming language

• In traditional Prolog implementations (e.g., SWI-Prolog), the query ancestor(tom,X) terminates (with correct answers), while the query ancestor(tom,X) does not terminate.

• In tabled Prolog (e.g., XSB), both queries terminate.

• Left-recursion causes a depth-first search strategy to loop for ever, while both breadth-first search and tabling strategy terminate.

cs774 (Prasad) L1LP 22

cs774 (Prasad) L1LP 23

expressivenessmechanization

Logic Programming Paradigm

Knowledge Representation

Knowledge Representation

Theorem Proving

Theorem Proving

Attribute Grammars / Compilers (DCGs)Attribute Grammars / Compilers (DCGs)

Relational DatabasesRelational Databases

Programming Languages

Programming Languages

Problem Solving in AI(i)Search

(ii)Divide and Conquer

Problem Solving in AI(i)Search

(ii)Divide and Conquer

unification

declarativeness

efficiency

Trading expressiveness for efficiency :Executable specification

• Knowledge Representation– LP provides a sufficiently rich subset of first-

order logic that is computationally tractable.– Supports non-monotonic reasoning via

negation-by-failure.

• Deductive Databases– LP adds expressive power by extending

relational query language (to support recursion) without sacrificing computational efficiency

• E.g., expression of transitive closure

cs774 (Prasad) L1LP 24

Divide and Conquer Strategy

• AND-OR Graphs

Goal_k :-

subgoal_1, subgoal_2, …, subgoal_n

subgoal_i :- Alt_i1.

subgoal_i :- Alt_i2.

subgoal_i :- Alt_im.cs774 (Prasad) L1LP 25

cs774 (Prasad) L1LP 26

State-Space SearchinitialState(_).

finalStates(_). …

finalStates(_).

transitions(_,_). …

transitions(_,_).

solved :-

finalStates(Y), reachable(Y).

reachable(Y) :-

initialState(Y).

reachable(Y) :-

transitions(X,Y), reachable(X).

cs774 (Prasad) L1LP 27

cs774 (Prasad) L1LP 28

Declarative Programming

permute(Lst,[Hd|RstPerm]):-split(Hd,Lst,Rst),

permute(Rst,RstPerm).

permute([],[]).

split(Hd,[Hd|Tl],Tl).

split(Hd,[NHd|Tl],[NHd|NTl]):-split(Hd,Tl,NTl).

?- findall(X,permute([1,2,3],X),Xall).Xall = [[1, 2, 3], [1, 3, 2], [2, 1, 3],

[2, 3, 1], [3, 1, 2], [3, 2, 1]].

cs774 (Prasad) L1LP 29

Definite Clause Grammars

Program

abcLst -->

abc, abcLst.

abcLst -->

[].

abc --> [a].

abc --> [b].

abc --> [c].

Queries

?-abcLst([b,c],[]).

true

?-abcLst([b,c,d],[]).

false

?-abcLst([b,c,d],[d]).

true

cs774 (Prasad) L1LP 30

top related