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

Slides:



Advertisements
Similar presentations
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Advertisements

Cs7120 (Prasad)L22-MetaPgm1 Meta-Programming
Semantics Static semantics Dynamic semantics attribute grammars
Computational Models The exam. Models of computation. –The Turing machine. –The Von Neumann machine. –The calculus. –The predicate calculus. Turing.
ICE1341 Programming Languages Spring 2005 Lecture #6 Lecture #6 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Introduction to Prolog, cont’d Lecturer: Xinming (Simon) Ou CIS 505: Programming Languages Fall 2010 Kansas State University 1.
Cs7120 (Prasad)L21-DCG1 Definite Clause Grammars
Control Structures Any mechanism that departs from straight-line execution: –Selection: if-statements –Multiway-selection: case statements –Unbounded iteration:
Rigorous Software Development CSCI-GA Instructor: Thomas Wies Spring 2012 Lecture 11.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
CSI 3120, Prolog recap, page 1 Prolog recap Prolog was invented in 1970, first implemented in 1972, first implemented efficiently in Excellent commercial-
ISBN Chapter 3 Describing Syntax and Semantics.
CS 355 – Programming Languages
Chapter 12 - Logic Programming
Comp 205: Comparative Programming Languages Semantics of Imperative Programming Languages denotational semantics operational semantics logical semantics.
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
Functional Design and Programming Lecture 1: Functional modeling, design and programming.
Chapter 8 . Sequence 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
Computer Science School of Computing Clemson University Discrete Math and Reasoning about Software Correctness Murali Sitaraman
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
Formal Models of Computation Part II The Logic Model
(1.1) COEN 171 Programming Languages Winter 2000 Ron Danielson.
Notes for Chapter 12 Logic Programming The AI War Basic Concepts of Logic Programming Prolog Review questions.
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
Mathematical Modeling and Formal Specification Languages CIS 376 Bruce R. Maxim UM-Dearborn.
CSCI-383 Object-Oriented Programming & Design Lecture 1.
Programming Paradigms Procedural Functional Logic Object-Oriented.
1 Lecture 6 Logic Programming introduction to Prolog, facts, rules Ras Bodik Shaon Barman Thibaud Hottelier Hack Your Language! CS164: Introduction to.
Declarative vs Procedural Programming  Procedural programming requires that – the programmer tell the computer what to do. That is, how to get the output.
ISBN Chapter 3 Describing Semantics -Attribute Grammars -Dynamic Semantics.
CS 363 Comparative Programming Languages Semantics.
Ceg860 (Prasad)L1SQ1 Software Quality Object-Oriented Programming Paradigm.
Computer Science School of Computing Clemson University Discrete Math and Reasoning about Software Correctness Joseph E. Hollingsworth
Semantics. Semantics is a precise definition of the meaning of a syntactically and type-wise correct program. Ideas of meaning: –Operational Semantics.
CS4026 Formal Models of Computation Part II The Logic Model Lecture 2 – Prolog: History and Introduction.
3.2 Semantics. 2 Semantics Attribute Grammars The Meanings of Programs: Semantics Sebesta Chapter 3.
ISBN Chapter 3 Describing Semantics.
Chapter 3 Part II Describing Syntax and Semantics.
Programming Languages and Design Lecture 3 Semantic Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
Semantics In Text: Chapter 3.
Theory of Programming Languages Introduction. What is a Programming Language? John von Neumann (1940’s) –Stored program concept –CPU actions determined.
Languages and Compilers
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
© Kenneth C. Louden, Chapter 12 - Logic Programming Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Programming Languages Third Edition Chapter 4 Logic Programming.
© Copyright 2008 STI INNSBRUCK Intelligent Systems Propositional Logic.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 1 Overview A good programming language is.
Programming Language Concepts (CIS 635) Elsa L Gunter 4303 GITC NJIT,
CSC3315 (Spring 2009)1 CSC 3315 Languages & Compilers Hamid Harroud School of Science and Engineering, Akhawayn University
Cs7120 (Prasad)L1-FP-HOF1 Functional Programming Basics Correctness > Clarity > Efficiency.
Artificial Intelligence Knowledge Representation.
Interface specifications At the core of each Larch interface language is a model of the state manipulated by the associated programming language. Each.
Advanced programming language theory. week 2. Attribute grammars and semantics.
COMP 412, FALL Type Systems C OMP 412 Rice University Houston, Texas Fall 2000 Copyright 2000, Robert Cartwright, all rights reserved. Students.
Functional Programming
Type Checking and Type Inference
Programming Paradigms
A Simple Syntax-Directed Translator
Scheme : variant of LISP
Imperative languages Most familiar: computation modifies store
Programming Paradigms
Prolog syntax + Unification
Procedural vs Functional Style
Semantics In Text: Chapter 3.
Predicate Transformers
Some Programming Paradigms
Programming Paradigms
Presentation transcript:

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

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

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.

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;

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

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.

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.

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

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

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;

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

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

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

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.

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

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

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

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

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

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

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

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

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

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

cs774 (Prasad)L1LP26

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

cs774 (Prasad)L1LP28

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

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