CSE 452: Programming Languages Logical Programming Languages Part 1

download CSE 452: Programming Languages Logical Programming Languages Part 1

of 45

  • date post

    19-Dec-2015
  • Category

    Documents

  • view

    221
  • download

    2

Embed Size (px)

Transcript of CSE 452: Programming Languages Logical Programming Languages Part 1

  • Slide 1
  • CSE 452: Programming Languages Logical Programming Languages Part 1
  • Slide 2
  • 2 Organization of Programming Languages-Cheng (Fall 2004) Outline u Another programming paradigm: Logic Programming u Prolog Well be using GNU prolog (http://www.gnu.org/software/gprolog/gprolog.ht ml)http://www.gnu.org/software/gprolog/gprolog.ht ml
  • Slide 3
  • 3 Organization of Programming Languages-Cheng (Fall 2004) Another Paradigm QUESTION: "What is a computer program?" ANSWER: "It is an executable representation of some algorithm designed to solve some real world problem." u Kowalski (CACM, 1979): There are two key elements to a computer program Logic what we want the program to achieve Control how we are going to achieve it ALGORITHM = LOGIC + CONTROL u Difference between imperative and declarative programming
  • Slide 4
  • 4 Organization of Programming Languages-Cheng (Fall 2004) Example: Computing Factorial Imperative (Ada) implementation with CS_IO ; use CS_IO ; procedure FACTORIAL is N: integer; T: integer:= 1; begin put("Input ");get(N); for K in 2..N loop T:= T*K; end loop; put("Factorial "); put(N); put("is "); put(T); newline; end FACTORIAL; u For imperative language programmer needs to concentrate on both the logic and control Declarative (PROLOG) implementation factorial(0,1):- !. factorial(N1,T2):- N2 is N1-1, factorial(N2,T1), T2 is N1*T1. u For declarative language, we define the logic (the desired goal or result) but not the control (how we achieve the desired goal)
  • Slide 5
  • 5 Organization of Programming Languages-Cheng (Fall 2004) Declarative Languages u Declarative languages make extensive use of relationships between objects u There are two principal styles of defining relationships: 1. Functional Programming Relationships are expressed using functions. (define (square n) (* n n)) The square function expresses the relationship between the input n and the output value n*n 2. Logic programming Relationships are declared using expressions known as clauses. square(N, M):- M is N*N. Clauses can be used to express both facts and rules
  • Slide 6
  • 6 Organization of Programming Languages-Cheng (Fall 2004) What is logic? u Encyclopedia Brittanica: Logic is the study of propositions and their use in argumentation u Encarta Encyclopedia: Logic is the science dealing with the principles of valid reasoning and argument u Factasia Logic: Logic is the study of necessary truths and of systematic methods for expressing and demonstrating such truths
  • Slide 7
  • 7 Organization of Programming Languages-Cheng (Fall 2004) Logic Programming u Logic programming expresses programs in the form of symbolic logic uses a logical inferencing process for reasoning u Logic programs are declarative rather than procedural Programs do not state exactly how a result is to be computed but rather describe the form of the result It is assumed that the computer can determine how the result is to be obtained One needs to provide the computer with the relevant information and a method of inference for computing desirable results u Programming languages based on symbolic logic are called logic programming languages Prolog is the most widely used logic programming language
  • Slide 8
  • 8 Organization of Programming Languages-Cheng (Fall 2004) Terminology u Proposition a logical statement that may be true or false u Symbolic logic is used for three purposes: express propositions express the relationships between propositions describe how new propositions may be inferred from others u Two primary forms of symbolic logic Propositional calculus Predicate calculus Predicate calculus is the form of symbolic logic used for logic programming
  • Slide 9
  • 9 Organization of Programming Languages-Cheng (Fall 2004) Propositions u Objects in logic programming propositions are Constants symbols that represent an object Example: man, jake, like bob, and steak Variables symbols that can represent different objects at different times u Atomic propositions are the simplest propositions and consist of compound terms
  • Slide 10
  • 10 Organization of Programming Languages-Cheng (Fall 2004) Atomic Propositions u Compound term has two parts functor: symbol that names the relation an ordered list of parameters u Examples: man (jake) like (bob, steak) Compound term with single parameter called a 1-tuple; Compound term with two params is called a 2-tuple, etc. u These propositions have no intrinsic semantics father (john, jake) could mean several things u Propositions are stated in two modes fact: one in which the proposition is defined to be true query: one in which the truth of the proposition is to be determined
  • Slide 11
  • 11 Organization of Programming Languages-Cheng (Fall 2004) Compound Propositions u Compound propositions have two or more atomic propositions connected by logical operators NameSymbolExampleMeaning negation anot a conjunction a ba and b disjunction a ba or b equivalence a ba is eqv to b implication a ba implies b a b b implies a (in prolog a b is written as a :- b)
  • Slide 12
  • 12 Organization of Programming Languages-Cheng (Fall 2004) Compound Propositions Compound proposition examples: a b c a b d equivalent to (a ( b)) d Precedence of logical connectors: highest precedence , , next , lowest precedence
  • Slide 13
  • 13 Organization of Programming Languages-Cheng (Fall 2004) Compound Proposition u Implication: p q Meaning: if p then q p implies q p is the premise or antecedent q is the conclusion or consequent u Can write p q in disjunctive normal form p OR q u Truth table shows equivalence
  • Slide 14
  • 14 Organization of Programming Languages-Cheng (Fall 2004) p q Equivalent to p OR q
  • Slide 15
  • 15 Organization of Programming Languages-Cheng (Fall 2004) Propositions with Quantifiers Variables may appear in propositions - only when introduced by symbols called quantifiers NameExampleMeaning universal X.PFor all X, P is true existential X.PThere exists a value of X such that P is true Note: the period separates the variable from the proposition
  • Slide 16
  • 16 Organization of Programming Languages-Cheng (Fall 2004) Propositions with Quantifiers u Examples of propositions with quantifiers X.(woman(X) human(X)) For any value of X, if X is a woman, then X is human X.(mother(mary, X) male (X)) There exists a value of X, such that mary is the mother of X and X is a male Note: quantifiers have a higher precedence than any of the logical operators
  • Slide 17
  • 17 Organization of Programming Languages-Cheng (Fall 2004) First Order Predicate Calculus u Provides a method of expressing collections of propositions Collection of propositions can be used to determine whether any interesting or useful facts can be inferred from them 0 is a natural number. 2 is a natural number. For all X, if X is a natural number, then so is the successor of X. -1 is a natural number u Predicate calculus: natural (0) natural (2) X.natural (X) natural (successor (X)) natural (-1)
  • Slide 18
  • 18 Organization of Programming Languages-Cheng (Fall 2004) First-Order Predicate Calculus A horse is a mammal A human is a mammal Mammals have four legs and no arms, or two legs and two arms A horse has no arms A human has no legs mammal (horse) mammal (human) X. mammal (X) legs (X,4) arms (X,0) legs (X,2) arms (X,2) arms (horse, 0) legs (human, 0)
  • Slide 19
  • 19 Organization of Programming Languages-Cheng (Fall 2004) Clausal Form u Redundancy is a problem with predicate calculus there are many different ways of stating propositions that have the same meaning Example: p q p OR q p AND q) not a problem for logicians but for computerized system, redundancy is a problem u Clausal form is one standard form of propositions used for simplification and has the syntax: B1 B2 ... Bn A1 A2 ... Am Meaning: If all As are true, then at least one B is true
  • Slide 20
  • 20 Organization of Programming Languages-Cheng (Fall 2004) Clausal Form u Characteristics Existential quantifiers are not required Universal quantifiers are implicit in the use of variable in the atomic propositions Only the conjunction and disjunction operators are required Disjunction appears on the left side of the clausal form and conjunction on the right side The left side is called the consequent The right side is called the antecedent
  • Slide 21
  • 21 Organization of Programming Languages-Cheng (Fall 2004) Clausal Form Examples likes (bob, trout) likes (bob, fish) fish (trout) Meaning: if bob likes fish and a trout is a fish, then bob likes trout father(louis, al) father(louis, violet) father(al, bob) mother(violet, bob) grandfather(louis, bob) Meaning: if al is bobs father and violet is bobs mother and louis is bobs grandfather, then louis is either als father or violets father
  • Slide 22
  • 22 Organization of Programming Languages-Cheng (Fall 2004) Predicate Calculus - Proving Theorems u One of the most significant breakthroughs in automatic theorem-proving was the discovery of the resolution principle by Robinson in 1965 u Resolution is an inference rule that allows inferred propositions to be computed from given propositions Resolution was devised to be applied to propositions in clausal form
  • Slide 23
  • 23 Organization of Programming Languages-Cheng (Fa