Cs7120 (Prasad)L26-ProgTech1 Programming Techniques

Slides:



Advertisements
Similar presentations
Symbol Table.
Advertisements

1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Cs7120 (Prasad)L22-MetaPgm1 Meta-Programming
CS CS4432: Database Systems II Logical Plan Rewriting.
Answer Set Programming Overview Dr. Rogelio Dávila Pérez Profesor-Investigador División de Posgrado Universidad Autónoma de Guadalajara
1 Knowledge Based Systems (CM0377) Lecture 8 (Last modified 5th March 2001)
Dynamic Programming.
Cs774 (Prasad)L7Negation1 Negation by Failure
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 52 Database Systems I Relational Algebra.
Introduction Combining two frameworks
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
CS263 Lecture 19 Query Optimisation.  Motivation for Query Optimisation  Phases of Query Processing  Query Trees  RA Transformation Rules  Heuristic.
A First Attempt towards a Logical Model for the PBMS PANDA Meeting, Milano, 18 April 2002 National Technical University of Athens Patterns for Next-Generation.
CMSC724: Database Management Systems Instructor: Amol Deshpande
CS 286, UC Berkeley, Spring 2007, R. Ramakrishnan 1 Evaluation of Recursive Queries Part 1: Efficient fixpoint evaluation “Seminaïve Evaluation”
Chapter 9: Subprogram Control
Programming Paradigms cs784(Prasad)L5Pdm1. Programming Paradigm A way of conceptualizing what it means to perform computation and how tasks to be carried.
Describing Syntax and Semantics
1 ES 314 Advanced Programming Lec 2 Sept 3 Goals: Complete the discussion of problem Review of C++ Object-oriented design Arrays and pointers.
1 Relational Algebra and Calculus Yanlei Diao UMass Amherst Feb 1, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
ASP vs. Prolog like programming ASP is adequate for: –NP-complete problems –situation where the whole program is relevant for the problem at hands èIf.
Introduction to Logic for Artificial Intelligence Lecture 2 Erik Sandewall 2010.
1 First order theories. 2 Satisfiability The classic SAT problem: given a propositional formula , is  satisfiable ? Example:  Let x 1,x 2 be propositional.
Query Processing Presented by Aung S. Win.
The design of j-DREW: a deductive reasoning engine for the web Bruce Spencer National Research Council Canada and University of New Brunswick Fredericton,
DEDUCTIVE DATABASE.
22-Sep-06 CS6795 Semantic Web Techniques 0 Horn Logic Markup Languages.
Semantic Analysis Legality checks –Check that program obey all rules of the language that are not described by a context-free grammar Disambiguation –Name.
CS848: Topics in Databases: Foundations of Query Optimization Topics Covered  Databases  QL  Query containment  More on QL.
Access Path Selection in a Relational Database Management System Selinger et al.
Database Management 9. course. Execution of queries.
Query Optimization (CB Chapter ) CPSC 356 Database Ellen Walker Hiram College (Includes figures from Database Systems: An Application Oriented.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages.
Cs7120 (Prasad)L16-Meaning1 Procedural and Declarative Meaning of Prolog
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Datalog Inspired by the impedance mismatch in relational databases. Main expressive advantage: recursive queries. More convenient for analysis: papers.
1 Relational Algebra & Calculus Chapter 4, Part A (Relational Algebra)
1 Relational Algebra and Calculas Chapter 4, Part A.
O A procedure: a set of axioms (rules and facts) with identical signature (predicate symbol and arity). o A logic program: a set of procedures defining.
The Volcano Optimizer Generator Extensibility and Efficient Search.
Introduction to Code Generation and Intermediate Representations
Prolog Program Style (ch. 8) Many style issues are applicable to any program in any language. Many style issues are applicable to any program in any language.
For Monday Finish chapter 19 No homework. Program 4 Any questions?
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
16.7 Completing the Physical- Query-Plan By Aniket Mulye CS257 Prof: Dr. T. Y. Lin.
Cs774 (Prasad)L6Backtracking1 Controlling Backtracking : Cuts
© Copyright 2008 STI INNSBRUCK Intelligent Systems Propositional Logic.
CS4710 Algorithms. What is an Algorithm? An algorithm is a procedure to perform some task. 1.General - applicable in a variety of situations 2.Step-by-step.
1 First order theories (Chapter 1, Sections 1.4 – 1.5) From the slides for the book “Decision procedures” by D.Kroening and O.Strichman.
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
1 Reasoning with Infinite stable models Piero A. Bonatti presented by Axel Polleres (IJCAI 2001,
CSC5101 Advanced Algorithms Analysis
CMPB454 ARTIFICIAL INTELLIGENCE (AI) CHAPTER 1.1 Background Information CHAPTER 1.1 Background Information Instructor: Alicia Tang Y. C.
COMP 3438 – Part II-Lecture 5 Syntax Analysis II Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
CSC 8505 Compiler Construction
Chapter 18 Query Processing and Optimization. Chapter Outline u Introduction. u Using Heuristics in Query Optimization –Query Trees and Query Graphs –Transformation.
O A procedure: a set of axioms (rules and facts) with identical signature (predicate symbol and arity). o A logic program: a set of procedures (predicates),
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
CHAPTER 19 Query Optimization. CHAPTER 19 Query Optimization.
Introduction Chapter : Introduction.
Introduction to Logic for Artificial Intelligence Lecture 2
Chapter 15 Lists Objectives
Knowledge Representation
Relational Algebra 1.
Query Optimization.
Introduction Chapter : Introduction.
Chapter 5 Recursion.
Controlling Backtracking : Cuts
Relational Calculus Chapter 4, Part B
Presentation transcript:

cs7120 (Prasad)L26-ProgTech1 Programming Techniques

Generalization/Abstraction Analogy: [a,b,c]  [f(a),f(b),f(c)] maplist(_,[],[]). maplist(P,[X|T],[NX|NT]) :- G =.. [P,X,NX], call(G), maplist(P,T,NT). (G  p(N,NX)) cs7120 (Prasad)L26-ProgTech2

Application transpose([],[]). transpose([[]|_],[]) :- !. transpose([R|Rs],[C|Cs]) :- maplist(first,[R|Rs],C), maplist(rest,[R|Rs],RC), transpose(RC,Cs). first([H|T],H). rest([H|T],T). /* Built-in maplist exists*/ cs7120 (Prasad)L26-ProgTech3

Enhancing Efficiency Interpreted vs Compiled code (order of magnitude improvement observed) Improving data structures and algorithm 8-Queens problem, Heuristic Search, Quicksort, etc Tail-recursive optimization Memoization storing partial results / caching intermediate results Difference lists DCGs cs7120 (Prasad)L26-ProgTech4

(cont’d) Prolog implementations that index on the first argument of a predicate improve determinism. Cuts and other meta-programming primitives can be used to program in new search strategies for controlled backtracking. cs7120 (Prasad)L26-ProgTech5

Optimizing Fibonacci Number Computation fib(0,0) :- !. fib(1,1) :- !. fib(N,F) :- N1 is N - 1, N2 is N1 -1, fib(N1,F1), fib(N2,F2), F is F1 + F2. ?-fib(5,F). Complexity: Exponential time algorithm cs7120 (Prasad)L26-ProgTech6

Fibonacci Call Tree with Parameter Value cs7120 (Prasad)L26-ProgTech7

(cont’d) f(0,F,_,F). f(1,_,F,F). f(N,Fpp,Fp,F) :- N >= 2, N1 is N – 1, F0 is Fp + Fpp, f(N1,Fp,F0,F). fib(N,F) :- f(N,0,1,F). ?-fib(5,F). Complexity: Linear time algorithm (tail-recursive version) cs7120 (Prasad)L26-ProgTech8

Last call optimization Activation record normally stores a continuation and a backtrack point, to be used when the goal succeeds or fails respectively. p :- q, r. p :- s. –LCO avoids allocating a new activation record for s, but rather reuses one for p. cs7120 (Prasad)L26-ProgTech9

Caching intermediate results Instead of explicitly modifying the code to improve performance, XSB uses tabling to store intermediate results and avoids recomputing earlier goals. Ironically, double-recursive (exponential- time) Fibonacci Number definition serves as a benchmark for testing efficiency of implementation of recursion! cs7120 (Prasad)L26-ProgTech10

Different Lists : Motivation cs7120 (Prasad)L26-ProgTech11 STACKpushpop Array-Impl.O(1) Linked-list Impl.O(1) QUEUEenqueuedequeue Circular Buffer or Linked List Impl. (front& rear pointer) O(1) Linked-List Impl. (front but no rear pointer) O(1) O(n)

(cont’d) In Prolog, pointers implementing list structures are not available for inspection/manipulation. Hence, complexity of enqueue (resp. dequeue) is O(1) and that of dequeue (resp. enqueue) is O(n). enqueue(Q,E,[E|Q]). dequeue([E],E). dequeue([_|F|T],E) :- dequeue([F|T],E). Difference list is a techqniue to get O(1) complexity for both the operations. cs7120 (Prasad)L26-ProgTech12

Difference Lists : Details Represent list L as a difference of two lists L1 and L2 –E.g., consider L = [a,b,c] and various L1-L2 combinations given below. cs7120 (Prasad)L26-ProgTech13 L1L2 [a,b,c][] [a,b,c,d,e][d,e] [a,b,c|T]T [a,b,c,d|T][d|T]

Benefit L = L1 – L2 Both enqueue and dequeue are O(1) operations obtained by cons-ing an element to L1 and L2 respectively. enqueue(L1-L2, E, [E|L1] – L2). dequeue(L1-L2, E, L1 – [E|L2]). E.g., enqueue([a]-[], b, [b,a] – []). dequeue([a]-[], a, [a]–[a]). cs7120 (Prasad)L26-ProgTech14

Append using Difference Lists append(X-Y, Y-Z, X-Z). Ordinary append complexity = O(length of first list) Difference list append complexity = O(1) cs7120 (Prasad)L26-ProgTech15 X Y Z X-Y Y-Z Y Z Z X-Z

(cont’d) append(X-Y, Y-Z, X-Z). ?-append([a,b,c|L]-L, [1,2|M]-M, N). X=[a,b,c|L] Y = L Y = [1,2|M] Z = M X – Z = N N= [a,b,c|[1,2|Z]]-Z N= [a,b,c,1,2|Z]]-Z cs7120 (Prasad)L26-ProgTech16

Restriction append(X-Y, Y-Z, X-Z). ?-append([a,b,c|[d]]-[d], [1,2]-[], N). Fails because the second lists must be a variable. Incomplete data structure is a necessity. cs7120 (Prasad)L26-ProgTech17

Interpreter-based Semantics vs Declarative Semantics IS is an over-specification but may provide an efficient implementation. DS specifies correctness criteria and may permit further optimization. Overall research goal: Characterize classes of programs for which the declarative and the procedural semantics coincide. cs7120 (Prasad)L26-ProgTech18

Relational Algebra (Operations on Relations) Select, Project, Join, Union, Intersection, difference –Transitive closure cannot be expressed in terms of these operations. A query language is relationally complete if it can perform the above operations. cs7120 (Prasad)L26-ProgTech19

Deductive Databases : Datalog (Function-free/Finite Domain Prolog) Datalog + Negation is relationally complete. What effects query evaluation efficiency? –Characteristics of data (cyclic vs acyclic) –Ordering of rules and body literals –Search strategy (top-down vs bottom-up) Tuple-at-a-time vs Set-at-a-time cs7120 (Prasad)L26-ProgTech20

Middle Ground: Top-down vs Bottom-up Improve efficiency by caching. (cf. tabling) Remove Incompleteness by loop detection. Focused search. Propagate bindings in the query. (cf. Magic sets) cs7120 (Prasad)L26-ProgTech21 In general, the efficiency of query evaluation can be improved by sequencing goals on the basis of their bindings and dependencies among rule literals.

Heuristics for rearranging rules and body literals for efficiency Order body literals by decreasing values of failure probability Order rules by decreasing values of success probability Order body literals to maximize dependencies among adjacent literals. Metric for comparison – e.g., extent of base relation graphs inspected cs7120 (Prasad)L26-ProgTech22

Backtracking Chronological Dependency directed –focus on the reason for backtracking ans(X,Y) :- p(X), q(Y), r(X). p(1). p(2). p(3). q(1). q(2). q(3). r(3). cs7120 (Prasad)L26-ProgTech23

Data Dependency Graph cs7120 (Prasad)L26-ProgTech24 ans(X,Y) :- p(X), r(X), q(Y), If r(X) fails, then backtrack to p(X) rather than q(Y).

Indexing Prolog indexes on –predicate symbol and arity –principal functor of first argument (cf. constant -> hash) Randomly accessed rule groups p(a) :- … p(22) :- … p(f(X)) :- … p([]) :- …, p([a]) :- …, … cs7120 (Prasad)L26-ProgTech25

Robert Kowalski Algorithm = Logic + Control Niklaus Wirth Programs = Data Structures + Algorithms cs7120 (Prasad)L26-ProgTech26