Untitled

Chapter 19: A First Look at Prolog

19.1 Introduction

  • This chapter introduces the logic programming language Prolog.

    • Prolog focuses on logic more explicitly than many other programming languages.

    • A Prolog program is a collection of facts and rules.

    • Unlike traditional programming, you do not "run" a Prolog program; instead, you pose questions to it.

    • Prolog is flexible and can perform all tasks that can be done in ML or Java, making it suitable for domains that require logical problem-solving.

    • Prolog is among the most popular languages used for artificial intelligence programming, alongside Lisp.

19.2 The Building Blocks—Prolog Terms

19.2.1 Overview of Prolog Terms
  • Prolog programs consist of different types of constructs called terms.

  • There are three types of Prolog terms:

    1. Constants

    2. Variables

    3. Compound terms

19.2.2 Constants
  • Constants:

    • Types of constants include integers, real numbers, and atoms.

    • Examples of numeric constants:

    • Decimal: 123, -123, 1.23, -123e-2

    • Atoms start with a lowercase letter, followed by letters, digits, or underscores.

    • Example of atoms:

    • n, fred, [] (the empty list)

    • Atoms use a specific syntax, while special sequences like ; denote additional functions.

19.2.3 Variables
  • Variables begin with an uppercase letter or an underscore, followed by additional characters.

    • Examples: X, Child, Fred_123

    • Variables can have special treatment when starting with an underscore.

19.2.4 Compound Terms
  • Compound terms consist of an atom followed by a parenthesized list of terms, e.g., parent(adam, seth).

    • These terms are different from function calls in many programming languages and should be considered as structured data rather than executed functions.

19.2.5 Grammar for Prolog Terms
  • Simplified grammar for terms:

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

    • <compound> ::= <atom>(<term list>)

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

19.3 Getting Started with a Prolog Language System

  • A Prolog language system is required to try examples and solve exercises from this chapter.

  • Examples were demonstrated using SWI-Prolog:

    • SWI-Prolog is a free and straightforward Prolog language system available for various platforms (Unix, Windows).

  • The Prolog interaction is prompt-based, allowing for easy input and output of queries.

    • Example of loading a file using the consult predicate:
      ?- consult(relations).

19.3.1 Querying Prolog
  • Queries are formulated to assess if something can be proven from the database.

    • Example queries demonstrated include parent(margaret, kent) and parent(fred, pebbles), with responses being true or false based on database facts.

    • The period at the end of queries is crucial; it signifies the end of a query.

  • Queries can utilize variables to uncover relationships; e.g., parent(P, jean).

19.3.2 Finding Solutions
  • Variables can be positioned to check for parent-child relationships in both ways, thus enabling the system to explore to generate multiple solutions.

    • Example shows exploring bindings using parent(Parent, Child).

19.4 Rules

  • Rules are used to derive facts from established relationships logically.

  • A rule’s formulation:

  predicate(Head) :- Conditions.
  • Example rule illustrates deriving great-grandparent relationships from existing parent facts.

19.5 The Two Faces of Prolog

  • Prolog rules possess both procedural and logical aspects:

    1. Procedural: Rules indicate how to prove something.

    2. Declarative: Rules represent logical assertions connecting facts.

  • Example of a rule that showcases logical assertions:

  greatgrandparent(GGP, GGC) :- parent(GGP, GP), parent(GP, P), parent(P, GGC).
  • In mathematical language:
    orallGGP,GGC.extifparent(GGP,GP)extandparent(GP,P)extandparent(P,GGC)extthengreatgrandparent(GGP,GGC)orall GGP, GGC. ext{ if } parent(GGP, GP) ext{ and } parent(GP, P) ext{ and } parent(P, GGC) ext{ then } greatgrandparent(GGP, GGC)