280 likes | 433 Views
ICS 313: Programming Language Theory. Module 14: Logic Overview. Logic 101 Review. A proposition is a logical statement that may or may not be true. Formal logic is a method for describing propositions and checking their validity. Symbolic logic is a kind of formal logic with:
E N D
ICS 313:Programming Language Theory Module 14: Logic Overview
Logic 101 Review • A proposition is a logical statement that may or may not be true. • Formal logic is a method for describing propositions and checking their validity. • Symbolic logic is a kind of formal logic with: • A syntax for describing propositions • A syntax for expressing relationships between propositions • A method for inferring new propositions based upon other propositions that are assumed to be true. • Predicate calculus is a form of symbolic logic used for logic programming.
Propositions in predicate calculus • Represented by terms, which are either constants or variables. • A constant is a symbol that represents an object. • A variable is a symbol that can represent different objects at different times. • A compound term is one element of a relation, and has two parts: • functor: the function symbol that names the relation • parameters: an ordered list of terms • Examples: man(jake), like(bob, redheads)
Propositions (cont.) • Propositions can be stated in two modes: • Assertion: the proposition is defined to be true • Query: the truth of the proposition is something to be determined.
Compound propositions • Atomic propositions connected by logical connectives. • Examples of compound propositions: • a b c • a b d
Quantified variables • Variables in propositions must be quantified: • Universal quantifier: X.P • for all X, P is true • Existential quantifier: X.P • there exists an X such that P is true • Examples: • X . (woman (X) human(X)) • X . (mother (mary, X) male(X))
Clausal form • Predicate calculus allows multiple representations for the same proposition. • Clausal form is one standard representation for all propositions. • Clausal form has the following structure: • B1 B2 ... Bn A1 A2 ... An • If all of the A’s are true, then at least one B is true. • Example: • likes(bob, mary) likes(bob, redhead) redhead(mary)
Properties of clausal form • Existential quantifiers are not required. • Variables are implicitly universally quantified. • No operators other than and needed. • Any predicate calculus proposition can be converted to clausal form algorithmically.
Theorem Proving • One important use of predicate calculus is theorem proving: inferring new facts from collections of propositions. • Resolution is an inference rule that allows inferred propositions to be automatically computed from given propositions, allowing automated theorem proving. • Resolution applies to propositions in clausal form.
Resolution • Suppose: • P1 P2 • Q1 Q2 • Now suppose that P1 and Q2 are identical (match), so they could be renamed T. • Then: • T P2 • Q1 T • and therefore: Q1 P2
Resolution example • If: • father(bob, jake) mother(bob, jake) parent(bob, jake) • grandfather(bob, fred) father(bob, jake) father(jake, fred) • Then: • mother(bob, jake) grandfather(bob, fred) parent(bob, jake) father(jake, fred)
Variables • Use of variables in propositions: • Implies that the resolution process must find values for variables to make propositions identical (match). • Finding variable values to make two propositions match is called unification. • Unification requires backtracking, since initial choice of a variable substitution may need to be undone.
Resolution in automated theorem proving • Resolution detects any inconsistencies in the set of propositions • Allows use in automated theorem proving (logic programming): • Supply a set of hypotheses • Propositions defined as true • Supply the goal • The negation of the theorem to be proven • If resolution finds an inconsistency, then the theorem is true • This is proof by contradiction.
Horn clauses for ATP • ATP requires a restricted kind of clausal form called ‘Horn Clauses’: • A single atomic proposition on the left side • ‘Headed Horn Clauses’ • likes(bob, mary) likes(bob, redhead) redhead(mary) • An empty left side • ‘Headless Horn Clauses’ • father(bob, jake)
Pros and cons of resolution-based inference • Strengths of resolution/theorem proving • ‘Programmer’ supplies what to do, not how to do it. • ‘What’ is expressed in predicate calculus • ‘How’ is accomplished through resolution • Analogous to high-level program synthesis from specifications.
Pros and cons (cont.) • Weaknesses of resolution/theorem proving • ‘Programmer’ supplies what to do, not how to do it. • Time required to find an inconsistency in a large set of hypotheses may be huge • Minimizing the number of unsuccessful matches, unifications, and backtracking • Automatic determination of ‘intermediate’ theorems
Prolog: one flavor of logic programming • Syntax is a modified version of predicate calculus • Inferencing method is a modified version of resolution. • Terms (headless Horn clauses): • Constant, variable, structure • Analogous to simple propositions in predicate calculus
Prolog (cont.) • Variables: • Not bound to types by declarations • Can be instantiated to arbitrary terms during resolution. • Take on a type dynamically (when instantiated) • Instantiations last only as long as it takes to satisfy one complete goal. • Structures: • Syntax similar to function invocation, but use more analogous to Pascal records
Prolog rules • Prolog rules are headed Horn clauses • Right hand side is the if part • contains only conjunctions • Left hand side is the then part • restricted to only a single disjunction • Example: • grandparent (X, Z) :- parent (X, Y), parent (Y, Z)
Prolog inferencing • Prolog queries are called goals. • When a goal is a compound proposition, each term is called a subgoal. • To prove a goal is true, Prolog must find a chain of inference rules and/or facts that connect the goal to the facts in the database.
Prolog inference example • If Q is the goal, then either • Q must be found as a fact in the database, or • The inferencing process must find a sequence of propositions P1... Pn such that: • P1 :- P2 • P1 :- P2 • : : • Pn :- Q • and P1 is a fact.
Forward chaining • Forward chaining • Use database to systematically generate new theorems until one matching query is found • Example: • father(bob). • man(X) :- father(X) • Given the goal: man(bob) • Under forward chaining, father(bob) is matched against father(X) to derive the new fact man(bob) in the database. • This new fact satisfies the goal.
Backward chaining • Use goal to work backward to a fact • Example: • Given man(bob) as goal • Match against man(X) to create new goal: father(bob). • father(bob) goal matches pre-existing fact, thus the query is satisfied.
Breadth vs. depth-first searching • How to order search when goal has multiple subgoals? • Breadth-first • Work on all subgoals in parallel • Depth-first • Find a complete sequence of propositions (a proof) for the first subgoal before working on the others.
Prolog Example: Syntax of lists • Enumerated lists • [a, b, c] • [] • Lists as a head and tail • [a, b, c | []] • [a, b | [c]] • [a | [b, c]]
Variables in Lists • Variables can be used to destructure lists: • ?- [H | T] = [a, b, c] • H = a • T = [b, c] • ?- [a | T] = [H, b, c] • T = [b,c] • H = a
Prolog examples: append • append([],Y,Y). • append([H|X],Y,[H|Z]) :- append(X,Y,Z). • Rule 2: In order to show that A appended to B equals C, show that X appended to B equals Z, where X is the tail of A and Z is the tail of C. • ?- append([a,b],[c,d],Z). • Z = [a,b,c,d] • ?- append ([a,b],Y,[a,b,c,d]). • Y = [c,d] • ?- append (X,[d,c],[a,b,c,d]). • no