Chapter 04 - Logic Programming (1)
Page 1
Title
Programming Languages Third Edition Chapter 4: Logic Programming
Page 2
Objectives
Understand the nature of logic programming
Understand Horn clauses
Understand resolution and unification
Become familiar with the Prolog language
Explore problems with logic programming
Become familiar with the Curry language
Page 3
Introduction
Logic: the science of reasoning and proof, existing since ancient Greek philosophers.
Mathematical or symbolic logic: developed with George Boole and Augustus De Morgan in the mid-1800s.
Logic is crucial for computers and programming languages, often using:
Circuits designed with Boolean algebra.
Logical statements describe axiomatic semantics.
Page 4
Introduction (cont’d.)
Logical statements serve as formal specifications for programs, aiding in proving correctness mathematically.
Computers implement mathematical logic principles through:
Automatic deduction systems/theorem provers:
Transform proofs into computation, leading to the creation of Prolog.
Page 5
Logic and Logic Programs
Understanding mathematical logic is essential.
First-order predicate calculus allows formal expression of logical statements.
Logical statements: statements that are either true or false.
Axioms: logical statements assumed true from which other true statements are derived.
Page 6
Logic and Logic Programs (cont’d.)
Parts of first-order predicate calculus statements:
Constants: numbers or names.
Predicates: names for functions that are true or false.
Functions: return non-Boolean values.
Variables: unspecified quantities.
Connectives: operations like and, or, not, implication (→), equivalence (↔).
Quantifiers: introduce variables.
Punctuation: parentheses, commas, periods.
Page 7
Logic and Logic Programs (cont’d.)
Example 1: Logical statement translations from English to predicate calculus.
Page 8
Logic and Logic Programs (cont’d.)
In Example 1:
First and third statements are axioms.
The second statement can be proved.
The fourth statement cannot be proved from the axioms and is assumed false.
Variables: e.g., x in the third statement denotes an unspecified quantity.
Page 9
Logic and Logic Programs (cont’d.)
Quantifiers:
Universal quantifier: true for all things (e.g., ∀x).
Existential quantifier: true for at least one thing (e.g., ∃x).
Bound variables: those introduced by quantifiers.
Page 10
Logic and Logic Programs (cont’d.)
A free variable is not bound by a quantifier.
Arguments to predicates/functions must only be terms: combinations of variables, constants, and functions (cannot include predicates, quantifiers, or connectives).
Page 11
Logic and Logic Programs (cont'd.)
Example 2:
Statements about horses and mammals, translated into predicates.
Page 12
Logic and Logic Programs (cont’d.)
Inference rules in first-order calculus derive new statements based on existing ones.
Example: if statements a→b and b→c are given, then a→c follows.
Page 13
Logic and Logic Programs (cont’d.)
Theorems are statements derived from axioms.
Logic programming: uses assumptions as axioms and derives facts through inference rules.
Page 14
Logic and Logic Programs (cont’d.)
Logic programming language: notation for logical statements with algorithms for inference.
Logic program: the set of statements considered axioms. Queries are inputs to initiate computation.
Page 15
Logic and Logic Programs (cont’d.)
Logic programming systems function as deductive databases to answer queries about facts and implications.
Control problem: the specific pathway used to derive statements.
Page 16
Logic and Logic Programs (cont’d.)
Kowalski's logic programming paradigm: combines algorithm, logic, and control vs. Wirth's imperative programming (data structures + programs).
Operations can theoretically be conducted in any order, making logic programs suitable for parallelism.
Page 17
Logic and Logic Programs (cont’d.)
Challenges in logic programming systems:
Difficulty with first-order predicate calculus due to too many expressions and inference rules.
Most systems restrict themselves to Horn clauses.
Page 18
Horn Clauses
Horn clause: statements of the form where a1,...,an implies b,
ai: simple statements (no connectives or quantifiers).
b: head of the clause; if no ai, it becomes →b (b is a fact).
Page 19
Horn Clauses (cont’d.)
Horn clauses can express most logical statements but have limitations.
Example 4: translating statements into Horn clauses.
Page 20
Horn Clauses (cont’d.)
Logical description of the Euclidian algorithm for GCD expressed in predicate calculus.
Page 21
Horn Clauses (cont’d.)
Example: GCD function and its translation into Horn clauses.
Page 22
Horn Clauses (cont’d.)
Example 6: logical statement translations into Horn clauses.
Page 23
Horn Clauses (cont’d.)
Example 7: approximating logical statements into Horn clauses.
Page 24
Horn Clauses (cont’d.)
Procedural interpretation: Horn clauses can be reversed to resemble procedures.
Similar to context-free grammar rules in recursive descent parsing.
Page 25
Horn Clauses (cont’d.)
Parsing of natural language motivated Prolog's development.
Definite clause grammars: specific grammar rules in Prolog.
Page 26
Horn Clauses (cont’d.)
Horn clauses define the properties of results but not the algorithms.
Logic programming systems typically write Horn clauses backward.
Page 27
Horn Clauses (cont’d.)
Variable scope:
Variables in the head are parameters.
Variables in the body are local.
Queries: Horn clauses without a head.
Page 28
Resolution and Unification
Resolution: inference rule for Horn clauses.
If the head of one clause matches with the body of another, replace the head with the body's content.
Page 29
Resolution and Unification (cont’d.)
Example: using resolution to derive relationships between two Horn clauses.
Page 30
Resolution and Unification (cont’d.)
The logic processing system matches goals with heads to create subgoals.
If all goals are resolved to the empty Horn clause, the statement is proved.
Unification: setting variables equal to match terms.
Page 31
Resolution and Unification (cont’d.)
Example 10: resolving GCD with unification demonstrating the process.
Page 32
Resolution and Unification (cont’d.)
Continuing with Example 10, unification leads to deriving correct results from GCD statements.
Page 33
Resolution and Unification (cont’d.)
A logic programming system specifies:
Order for resolving goals.
Order of clause usage.
Order matters significantly in deriving answers.
Page 34
The Language Prolog
Prolog: a widely used logic programming language employing Horn clauses.
Implements resolution using a depth-first strategy; ISO standard adopted.
Page 35
Notation and Data Structures
Prolog notation resembles Horn clauses with added specifics:
Implication as :-; uppercase for variables, lowercase for constants; list notation using brackets.
Page 36
Notation and Data Structures (cont’d.)
Example of specifying head and tail of lists via a vertical bar.
Built-in predicates include arithmetic and I/O.
Page 37
Execution in Prolog
Prolog programs consist of Horn clauses and goal sets.
Queries initiated at runtime prompt user for input.
Page 38
Execution in Prolog (cont'd.)
Example 11: ancestor and parent functions in Prolog with queries and results.
Page 39
Execution in Prolog (cont’d.)
Use semicolon for queries to indicate multiple potential results.
Page 40
Arithmetic
Prolog includes built-in arithmetic with both infix and prefix notations.
It requires using predicates to distinguish between arithmetic and data.
Page 41
Arithmetic (cont’d.)
Example illustrating GCD algorithm implemented in both Horn clauses and Prolog.
Page 42
Unification
Unification is a process where variables match during resolution, leading to equalities.
Prolog’s unification rules described.
Page 43
Unification (cont'd.)
Examples of various unification scenarios in Prolog.
Page 44
Unification (cont’d.)
Explanation of pattern-directed invocation in unification.
Page 45
Unification (cont’d.)
Append procedure explanation and examples in Prolog syntax.
Page 46
Unification (cont’d.)
Concise representation of the append procedure capable of running backward.
Page 47
Unification (cont'd.)
Reverse procedure example in Prolog.
Page 48
Unification (cont'd.)
Prolog clauses for GCD, append, and reverse outlined.
Page 49
Prolog’s Search Strategy
Prolog uses linear resolution leading to depth-first searches across decision trees.
Page 50
Prolog's Search Strategy (cont'd.)
Example of a Prolog search tree illustrating resolution and variable instantiations.
Page 51
Prolog’s Search Strategy (cont’d.)
Leaf nodes occur upon failing to match clauses or achieving success. Depth-first search's efficiency and potential for infinite loops highlighted.
Page 52
Prolog’s Search Strategy (cont’d.)
Illustrates how ordering clauses differently can lead to infinite looping.
Page 53
Loops and Control Structures
Prolog utilizes backtracking for loops and repetitive actions.
Example demonstrating this backtracking in action.
Page 54
Loops and Control Structures (cont’d.)
Generate integers as solutions using recursive search trees in Prolog.
Page 55
Loops and Control Structures (cont'd.)
Infinite branches of integer generation illustrated in search trees.
Page 56
Loops and Control Structures (cont’d.)
Challenges of controlling loop implementations causing infinite loops when conditions are not valid.
Page 57
Loops and Control Structures (cont’d.)
Discusses the cut operator (!), how it prunes search trees, and its implications for solutions.
Page 58
Loops and Control Structures (cont'd.)
Consequences of cut usage in Prolog illustrated in a search tree example.
Page 59
Loops and Control Structures (cont’d.)
Rewriting examples and challenges of finding solutions due to structuring of cuts.
Page 60
Loops and Control Structures (cont'd.)
Further exploration of cut operation impact on solution discovery in Prolog.
Page 61
Loops and Control Structures (cont’d.)
Solutions management when utilizing cuts to control flow in looping structures.
Page 62
Loops and Control Structures (cont’d.)
Strategies to control infinite loops using cuts for bounded expressions.
Page 63
Loops and Control Structures (cont’d.)
Using cuts to resemble if-else constructs from imperative languages demonstrated.
Page 64
Loops and Control Structures (cont'd.)
Prolog implementation of the Sieve of Eratosthenes for prime generation demonstrated.
Page 65
Problems with Logic Programming
Logic programming's goal to enable specification focused on properties rather than computation.
Declarative programming explains solutions rather than procedures, with limitations in Prolog's outcomes.
Page 66
Problems with Logic Programming (cont’d.)
Programmers must recognize and navigate algorithms in logic systems and their low-level specifics.
Page 67
The Occur-Check Problem in Unification
Occur-check problem: potential infinite loops due to unification issues when a variable reoccurs in terms.
Page 68
The Occur-Check Problem in Unification (cont’d.)
Illustrates an infinite loop stemming from unification producing circular structures.
Page 69
Negation as Failure
Closed-world assumption: anything unproven is assumed false leading to challenges with the negation as failure principle.
Page 70
Negation as Failure (cont’d.)
Nonmonotonic reasoning: discovered when updating knowledge leads to decreased provable statements.
Page 71
Negation as Failure (cont’d.)
Illustrates variable instantiation being lost post-failure in negation scenarios.
Page 72
Negation as Failure (cont’d.)
Examples illustrating failed goals and variable instantiation consequences.
Page 73
Horn Clauses Do Not Express All of Logic
Challenges arise in representational limitations of Horn clauses concerning quantifiers.
Page 74
Horn Clauses Do Not Express All of Logic (cont’d.)
Better approximations illustrated but recognized as not equivalent to original logical statements.
Page 75
Control Information in Logic Programming
Prolog's search strategy implies control limitations causing potential failures.
Page 76
Control Information in Logic Programming (cont'd.)
Definitions for sorted and permutation properties stressing computational inefficiencies.
Page 77
Control Information in Logic Programming (cont’d.)
Stresses the gap between mathematical definitions and computational efficiency of Prolog systems.
Page 78
Control Information in Logic Programming (cont'd.)
Example of efficient sorting with a quicksort algorithm in Prolog syntax.
Page 79
Curry: A Functional Logic Language
Curry merges functional and logic programming advantages into one language.
Page 80
Functional Programming in Curry
Curry is based on Haskell, maintaining and adding both functional and logic programming capabilities.
Page 81
Adding Nondeterminism, Conditions, and Backtracking
Curry introduces nondeterminism and conditions to support backtracking.
Page 82
Adding Nondeterminism, Conditions, and Backtracking (cont’d.)
Example showcasing nondeterministic functions and implications for sorting.
Page 83
Adding Nondeterminism, Conditions, and Backtracking (cont’d.)
Explains function permutations and how it operates within Curry's design.
Page 84
Adding Logical Variables and Unification
Incorporates logical variables enabling solutions to be formed with unknown data through equations.