Untitled

Chapter 19: A First Look at Prolog

19.1: Introduction

  • This chapter introduces Prolog, a logic programming language.

  • Key learning goal: Write simple Prolog code and manipulate lists.

  • Prolog reflects an explicit use of logic for programming, acting more like a collection of facts and rules than traditional imperative programming.

  • Users pose questions, and Prolog’s logic system consists of facts and rules that help answer these.

  • Prolog is especially powerful for search problems that can be defined logically.

  • It is one of the top languages for artificial intelligence, along with Lisp.

19.2: The Building Blocks— Prolog Terms

19.2.1 Overview of Prolog Terms
  • Prolog syntax differs significantly from other programming languages like Java and ML but is simple.

  • All entities in Prolog comprise Prolog terms, which can be of three types:

    • Constants: Basic, unchangeable values such as integers (123, 1.23) or atoms (e.g., fred, [], nil).

    • Variables: Names that start with uppercase letters or underscores (e.g., X, Child, Fred_123). Variables act as placeholders.

    • Compound Terms: Made up of an atom followed by a list of terms in parentheses, separated by commas (e.g., parent(adam, seth)).

19.2.2 Types of Terms
  • Constants:

    • Include integers, real numbers, and atoms.

    • Atoms: Any name starting with a lowercase letter or specific symbols.

  • Variables: Any name starting with an uppercase letter or underscore.

  • Compound Terms: Structured data typically resembling function calls in other programming languages, but should be treated as constructions rather than function calls.

19.2.3 Simplified Grammar for Terms
  • The grammar for terms can be represented as:

    • <term> ::= <constant> | <variable> | <compound-term>

    • <constant> ::= <integer> | <real-number> | <atom>

    • <compound-term> ::= <atom>(<termlist>)

    • <termlist> ::= <term> | <term>, <termlist>

19.3: Getting Started with a Prolog Language System

19.3.1 Setting up Prolog
  • To experiment with Prolog, a Prolog system (like SWI-Prolog) is required.

  • SWI-Prolog can be installed on Unix or Windows and is recommended for exercises and examples in this chapter.

  • The system operates interactively, prompting for queries and returning responses.

19.3.2 Example Queries
  • A Prolog program consists of a database of facts, e.g., parent(kim, holly), and queries can check if these facts are true, e.g., ?- parent(margaret, kent)..

  • Queries can yield true, false, or retrieve variable bindings.

  • Important note: The period at the end of queries indicates completion; omitting it prompts for further input.

19.3.3 Query Variability
  • Queries can utilize variable placements: ?- parent(Parent, Child). retrieves all parent-child pairs.

  • The semicolon allows Prolog to search for additional solutions instead of stopping at the first found.

19.4: Rules

19.4.1 Defining Relationships with Rules
  • Rules express logical statements and relationships between terms.

  • A rule format: <head> :- <conditions>., i.e., to prove the head, prove the conditions.

  • Introduction of rules increases program capability without redefining facts.

19.4.2 Example Rule Use
  • An example: greatgrandparent(GGP, GGC) :- parent(GGP, GP), parent(GP, P), parent(P, GGC). defines a grandparent relationship.

  • In use, Prolog attempts to unify the head of the rule against queries, leading to a process of condition proving.

19.5: The Two Faces of Prolog

19.5.1 Procedural vs. Declarative
  • Prolog's semantics can be viewed both procedurally (how Prolog executes) and declaratively (what Prolog asserts).

  • An understanding of declarative structure and procedural execution aids in mastering Prolog's usage.

19.5.2 Representation and Execution
  • Each rule can be thought of as a procedure with a logical assertion built-in to provide a flexible execution environment.

19.5.3 Practical Representation
  • Programmers can effectively map the rules to practical problem-solving domains.

19.6: Operators

  • Operators in Prolog facilitate operator notation acceptance by the system for improved readability.

  • Operators can use standard mathematical principles, and some have specific syntactical meanings within Prolog, e.g., :- for rules.

19.7: Lists

19.7.1 Understanding Lists
  • Representation of lists in Prolog mirrors that of lists in ML but with notable distinctions in behavior and syntax.

  • The empty list is represented by the atom [], and lists utilize the | operator for elements, e.g., [Head | Tail].

19.7.2 Operations on Lists
  • Lists can be appended, queried, and processed flexibly with predefined predicates such as append, member, nth, etc.

19.7.3 Flexibility and Use Case
  • Prolog’s list predicates are designed for maximum utility with variables allowed flexibly, important for numerous logical reasoning tasks.

19.8: Computation and Recursion

  • Prolog supports recursion which is critical for many logic-based solutions but must be designed carefully with optimization in mind.

19.8.1 Using Recursion
  • Recursive rules in Prolog require careful structuring to prevent infinite loops and ensure termination; tail-recursion optimization can help.

19.9: Negation and Failure Handling

  • Definition of negation within Prolog is more procedural than declarative; not predicate might produce non-standard behavior under certain conditions.

19.10: Prolog Applications

  • Discussion on how Prolog handles and optimizes complex problem-solving scenarios, such as the classic river crossing riddle.

Conclusion of Chapter 19

  • The chapter equipped readers with fundamental Prolog syntax, constructs, and computational logic needed to begin using Prolog effectively, setting the stage for deeper dives into its unique capabilities.

Exercises

  • Various exercises targeting aspects like relation definitions, list handling, and recursive structures are designed to extend learning and application of Prolog concepts when applied to practical coding scenarios.