1 / 30

Programming Paradigms

Programming Paradigms. Logic Programming Paradigm Correctness > Efficiency t.k.prasad@wright.edu http://www.knoesis.org/tkprasad/. Programming Paradigm.

sydney
Download Presentation

Programming Paradigms

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. Programming Paradigms Logic Programming Paradigm Correctness > Efficiency t.k.prasad@wright.edu http://www.knoesis.org/tkprasad/ 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 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. 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; L1LP

  5. 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. Role of variable 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). 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 languages also make explicit HOW the “declarative knowledge” is used to solve the query. L1LP

  8. Append in Prolog append([], L, L). append([ H | T ], L, [ H | R ]) :- append(T, L, R). • 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. 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). 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). L1LP

  11. 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; L1LP

  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 L1LP

  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 L1LP

  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) 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. 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 L1LP

  17. Logic Programming Paradigm(cont’d) L1LP

  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 L1LP

  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). L1LP

  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 L1LP

  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). L1LP

  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. L1LP

  23. Trading expressiveness for efficiency : Executable specification • Problem Solving in AI • Search • Divide and Conquer Knowledge Representation Theorem Proving efficiency unification Logic Programming Paradigm mechanization expressiveness declarativeness Programming Languages Attribute Grammars / Compilers (DCGs) Relational Databases L1LP

  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 L1LP

  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. L1LP

  26. L1LP

  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). L1LP

  28. L1LP

  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]]. L1LP

  30. Definite Clause Grammars Program Queries ?-abcLst([b,c],[]). true ?-abcLst([b,c,d],[]). false ?-abcLst([b,c,d],[d]). true abcLst --> abc, abcLst. abcLst --> []. abc --> [a]. abc --> [b]. abc --> [c]. L1LP

More Related