Presentation is loading. Please wait.

Presentation is loading. Please wait.

Cs774 (Prasad)L1LP1 Programming Paradigms Logic Programming Paradigm Correctness > Efficiency

Similar presentations


Presentation on theme: "Cs774 (Prasad)L1LP1 Programming Paradigms Logic Programming Paradigm Correctness > Efficiency"— Presentation transcript:

1 cs774 (Prasad)L1LP1 Programming Paradigms Logic Programming Paradigm Correctness > Efficiency t.k.prasad@wright.edu http://www.knoesis.org/tkprasad/

2 cs774 (Prasad)L1LP2 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

3 cs774 (Prasad)L1LP3 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.

4 cs774 (Prasad)L1LP4 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;

5 cs774 (Prasad)L1LP5 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.

6 cs774 (Prasad)L1LP6 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).

7 cs774 (Prasad)L1LP7 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 HOW the “declarative knowledge” is used to solve the query. also make explicit HOW the “declarative knowledge” is used to solve the query.

8 cs774 (Prasad)L1LP8 Append Append in Prolog append([], L, L). append([ H | T ], L, [ H | R ]) :- append([ H | T ], L, [ H | R ]) :- append(T, L, R). append(T, L, R). append True statements about append relation. “.” and “:-” are logical connectives that stand for “and” and “if” respectively. Uses pattern matching. “[]” and “|” stand for empty list and cons operation.

9 cs774 (Prasad)L1LP9 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).

10 cs774 (Prasad)L1LP10 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).

11 cs774 (Prasad)L1LP11 GCD : functional vs imperative Precondition: 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;

12 GCD: logic Precondition: n > m >= 0 gcd(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)L1LP12

13 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)L1LP13

14 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)L1LP14

15 cs774 (Prasad)L1LP15 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.

16 cs774 (Prasad)L1LP16 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

17 Logic Programming Paradigm (cont’d) cs774 (Prasad)L1LP17

18 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)L1LP18

19 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)L1LP19

20 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)L1LP20

21 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)L1LP21

22 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)L1LP22

23 cs774 (Prasad)L1LP23 expressiveness mechanization Logic Programming Paradigm Knowledge Representation Knowledge Representation Theorem Proving Theorem Proving Attribute Grammars / Compilers (DCGs) Attribute Grammars / Compilers (DCGs) Relational Databases Relational 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

24 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)L1LP24

25 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)L1LP25

26 cs774 (Prasad)L1LP26

27 State-Space Search initialState(_). finalStates(_). … finalStates(_). transitions(_,_). … transitions(_,_). solved :- finalStates(Y), reachable(Y). reachable(Y) :- initialState(Y). reachable(Y) :- transitions(X,Y), reachable(X). cs774 (Prasad)L1LP27

28 cs774 (Prasad)L1LP28

29 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)L1LP29

30 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)L1LP30


Download ppt "Cs774 (Prasad)L1LP1 Programming Paradigms Logic Programming Paradigm Correctness > Efficiency"

Similar presentations


Ads by Google