CS 420: Advanced Programming Languages - Lecture 23: Logic Programming Languages

Introduction to Logic Programming

  • Declarative Programming: Logic programming is declarative, focusing on what problem to solve rather than how to solve it. Solutions are inferred from facts, rules (propositions), and relationships between propositions.

  • Applications: Suited for knowledge processing, artificial intelligence, natural language processing, computational linguistics, expert systems, theorem proving, machine learning, and robotics.

Topics Covered

  • Introduction to Logic Programming

  • Brief Introduction to Predicate Calculus

  • Predicate Calculus and Proving Theorems

  • Overview of Logic Programming

  • Origins of Prolog

  • Basic Elements of Prolog

  • Deficiencies of Prolog

  • Applications of Logic Programming

Introduction

  • Logic languages use symbolic logic to express programs.

  • They employ logical inferencing to produce results.

  • Declarative vs. Procedural: Logic programming is declarative. Only the specification of results is stated and not detailed procedures for producing them.

CProposition

  • A logical statement that may or may not be true.

  • Consists of objects and relationships of objects to each other.

Symbolic Logic

  • Symbolic logic is used for:

    • Expressing propositions.

    • Expressing relationships between propositions.

    • Describing how new propositions can be inferred from other propositions.

  • Predicate Calculus: A particular form of symbolic logic used for logic programming.

Object Representation

  • Objects in propositions are represented by simple terms:

    • Constants: Symbols representing objects.

    • Variables: Symbols that can represent different objects at different times. They differ from variables in imperative languages.

compound Terms

  • Atomic propositions consist of compound terms.

  • Compound Term: An element of a mathematical relation, written like a mathematical function.

    • A mathematical function represents a mapping.

    • Can be written as a table.

Parts of a Compound Term

  • A compound term is composed of two parts:

    • Functor: A function symbol that names the relationship.

    • Ordered List of Parameters (Tuple)

  • Examples:

    • student(jon)

    • like(seth, OSX)

    • like(nick, windows)

    • like(jim, linux)

Forms of a Proposition

  • Propositions can be stated in two forms:

    • Fact: A proposition assumed to be true.

    • Query: A proposition whose truth is to be determined.

Compound Proposition

  • Consists of two or more atomic propositions.

  • Propositions are connected by logical operators.

Logical Operators

Name

Symbol

Example

Meaning

Negation

¬

¬ a

not a

Conjunction

∩

a ∩ b

a and b

Disjunction

∪

a ∪ b

a or b

Equivalence

≡

a ≡ b

a is equivalent to b

Implication

⊃ ⊂

a ⊃ b

a implies b



a ⊂ b

b implies a

Quantifiers

Name

Example

Meaning

Universal

∀X.P

For all X, P is true

Existential

∃X.P

There exists a value of X such that P is true.

Clausal Form

  • To standardize propositions, clausal form is used.

  • Clausal Form: B<em>1∪B</em>2∪…∪B<em>n⊂A</em>1∩A<em>2∩…∩A</em>mB<em>1 ∪ B</em>2 ∪ … ∪ B<em>n ⊂ A</em>1 ∩ A<em>2 ∩ … ∩ A</em>m

    • This means if all the AAs are true, then at least one BB is true.

    • Antecedent: Right side (A<em>1∩A</em>2∩…∩AmA<em>1 ∩ A</em>2 ∩ … ∩ A_m).

    • Consequent: Left side (B<em>1∪B</em>2∪…∪BnB<em>1 ∪ B</em>2 ∪ … ∪ B_n).

Clausal Form Examples

  • likes(bob, trout) C likes(bob, fish) fish(trout)

  • father(louis, al) U father(louis, violet) C father(al, bob) mother(violet, bob)

  • grandfather(louis, bob)

Predicate Calculus and Proving Theorems

  • Propositions can be used to discover new theorems inferred from known axioms and propositions.

  • Resolution: An inference principle that allows inferred propositions to be computed from given propositions.

Resolution Examples

  • Given:

    • P<em>1CP</em>2P<em>1 C P</em>2

    • Q<em>1CQ</em>2Q<em>1 C Q</em>2

    • TT

  • Inference:

    • Q<em>1CP</em>2Q<em>1 C P</em>2

  • Example:

    • older(joanne, jake) C mother(joanne, jake)

    • wiser(joanne, jake) C older(joanne, jake)

    • Therefore: wiser(joanne, jake) C mother(joanne, jake)

  • Example:

    • father(bob, jake) U mother(bob, jake) C parent(bob, jake)

    • grandfather(bob, fred) C father(bob, jake) father(jake, fred)

    • Therefore:

      • mother(bob, jake) U grandfather(bob, fred) C parent(bob, jake) father(jake, fred)

Resolution: Unification

  • Unification: Finding values for variables in propositions that allows the matching process to succeed.

  • Goal: man(bob).

  • Database facts and rules:

    • father(bob).

    • man(X) :- father(X).

  • Instantiation: Assigning temporary values to variables to allow unification to succeed.

  • If matching fails after instantiating a variable, it may be necessary to backtrack and instantiate with a different value.

Proof by Contradiction

  • Hypotheses: A set of pertinent propositions.

  • Goal: Negation of the theorem stated as a proposition.

  • The theorem is proved by finding an inconsistency.

Theorem Proving

  • Basis for logic programming.

  • When propositions are used for resolution, only a restricted form can be used.

  • Horn Clause: Can have only two forms:

    • Headed: Single atomic proposition on the left side.

    • Headless: Empty left side (used to state facts).

      • Example: father(bob, jake).

  • Most propositions can be stated as Horn clauses.

Overview of Logic Programming

  • Declarative Semantics

    • There is a simple way to determine the meaning of each statement.

    • Simpler than the semantics of imperative languages.

  • Programming is nonprocedural

    • Programs do not state how a result is to be computed but rather the form of the result.

Example: Sorting a List

  • Describe the characteristics of a sorted list, not the process of rearranging a list.

  • sort(old_list, new_list) C permute(old_list, new_list) ∩ sorted(new_list)

  • sorted(list) C ∀j such that 1≤ j < n, list(j) ≤ list(j+1)