Prolog Quick Guide in Prolog Language - PiEmbSysTech Embedded VLSI Research Hub

Quick Guide in Prolog Language

Prolog (Programming in Logic) is a high-level programming language rooted in formal logic, primarily used for artif

icial intelligence and computational linguistics. Unlike imperative languages, Prolog is declarative, meaning you specify what you want to achieve rather than how to achieve it.

This quick guide provides a concise overview of Prolog’s fundamental concepts:

  • Facts: Statements that are unconditionally true.
parent(tom, bob).
parent(bob, alice).
  • Rules: Conditional statements defining relationships between facts.
grandparent(X, Y) :- parent(X, Z), parent(Z, Y).
  • Queries: Questions posed to the Prolog system to infer information from facts and rules.
?- parent(tom, bob).
?- grandparent(tom, alice).
  • Variables: Begin with an uppercase letter or an underscore and are used to generalize queries and rules.
  • Lists: Essential data structures in Prolog, represented as [Head|Tail].
?- [H|T] = [1, 2, 3].
% H = 1, T = [2, 3]
  • Recursion: Commonly used for defining rules and processing lists.
ancestor(X, Y) :- parent(X, Y).
ancestor(X, Y) :- parent(X, Z), ancestor(Z, Y).
  • Arithmetic: Requires explicit evaluation using the `is` operator.
?- member(X, [1, 2, 3]).
% X = 1 ; X = 2 ; X = 3.
  • Built-in Predicates: Prolog includes various predicates for list processing, arithmetic, and more.

`member/2`: Checks if an element is in a list.

?- member(X, [1, 2, 3]).
% X = 1 ; X = 2 ; X = 3.

    `append/3`: Concatenates two lists.

    ?- append([1, 2], [3, 4], Result).
    % Result = [1, 2, 3, 4].

    Example Knowledge Base

    To display how Prolog works, let’s look at a simple example involving family relationships. This example will demonstrate how we can use facts and rules together to discover new information.

    % Facts
    parent(tom, bob).
    parent(bob, alice).
    parent(alice, john).
    
    % Rule
    grandparent(X, Y) :- parent(X, Z), parent(Z, Y).
    
    % Queries
    % ?- grandparent(tom, john).
    % Expected output: Yes, because Tom is the grandparent of John.
    • Explanation: This knowledge base includes three facts that establish parent-child relationships and one rule that defines a grandparent relationship based on these facts. When querying if Tom is the grandparent of John, Prolog deduces this through the rule and existing facts.

    Purpose of Lists in Prolog

    Lists in Prolog are essential for storing and managing collections of elements, simplifying the handling of data sequences. As a fundamental data structure, they facilitate various operations such as traversal, modification, and pattern matching.

    • Purpose: Lists are used to store and manipulate collections of elements, making it easier to handle sequences of data.
    • List Representation:
    [Head|Tail].
    • Explanation: A list is split into its head (the first element) and tail (the remaining elements).
    • Example:
    ?- [H|T] = [1, 2, 3].
    % H = 1, T = [2, 3]
    • Explanation: This query deconstructs the list [1, 2, 3] into its head (H = 1) and tail (T = [2, 3]).

    Recursion

    Recursion is like breaking down a big task into smaller, similar tasks, then solving each smaller task until you reach a simple enough task that you know the answer to directly. It’s like solving a puzzle by solving smaller puzzles inside it.

    In Prolog, recursion often involves defining a rule or function that refers to itself. For example, imagine you want to calculate the factorial of a number. Instead of trying to compute it directly, you can break it down: the factorial of a number N is N times the factorial of N-1, until you reach 1. This step-by-step breakdown is what recursion is all about.

    • Example:
    ancestor(X, Y) :- parent(X, Y).
    ancestor(X, Y) :- parent(X, Z), ancestor(Z, Y).
    • Explanation: The first rule states that X is an ancestor of Y if X is a parent of Y. The second rule states that X is an ancestor of Y if X is a parent of Z and Z is an ancestor of Y. This recursive definition allows tracing ancestry through multiple generations.

    Arithmetic

    Arithmetic in programming involves performing mathematical operations like addition, subtraction, multiplication, and division. In Prolog, you can use arithmetic operations to calculate values, compare numbers, or manipulate data.

    Basic Arithmetic Operations

    • Addition (+): Combining two or more numbers to find their total sum.
    • Subtraction (-): Finding the difference between two numbers.
    • Multiplication (*): Repeated addition of a number.
    • Division (/): Splitting a number into equal parts.
    • Example:

    % Example
    ?- X is 2 + 3.
    % X = 5.

    • Explanation: The `is` operator evaluates the arithmetic expression ‘2 + 3‘ and assigns the result to X.

    Built-in Predicates

    Prolog comes with a set of built-in predicates, which are pre-defined functions that can be used directly in programs without having to implement them from scratch

    These predicates cover a wide range of tasks, such as:

    • Term unification: Predicates like (=)/2 for Prolog unification and unify_with_occurs_check/2
    • Type testing: Predicates like var/1, atom/1, integer/1, float/1, number/1, atomic/1, compound/1, nonvar/1, ground/1
    • Term comparison: Predicates like (==)/2 for term identity, (@<)/2 for less than, (@>)/2 for greater than, etc.
    • Term creation and decomposition: Predicates like functor/3, arg/3, (=..)/2, copy_term/2
    • Arithmetic evaluation: Predicates like (is)/2 for evaluating arithmetic expressions, (=:=)/2 for arithmetic equality, etc.
    • List processing: Predicates like append/3, member/2, reverse/2, sort/2
    • Input/output: Predicates like write/1, read/1, open/3, close/1

    Example Program: List Length

    % Base case
    list_length([], 0).
    
    % Recursive case
    list_length([_|T], Length) :-
        list_length(T, N),
        Length is N + 1.
    
    % Query
    % ?- list_length([a, b, c, d], Length).
    % Expected output: Length = 4.
    • Explanation: `The list_length/2` predicate calculates the length of a list by recursively counting each element until the list is empty.

    Practical Tips

    • Use meaningful names: Helps in understanding the rules and facts.
    • Comment your code: `Use %` for single-line comments to explain logic.
    • Test with different queries: Ensure your rules and facts behave as expected by trying various scenarios.
    • Explore built-in predicates: Familiarize yourself with common predicates like ‘findall/3‘, ‘sort/2‘, etc., to leverage Prolog’s full capabilities.

    Discover more from PiEmbSysTech

    Subscribe to get the latest posts sent to your email.

    Leave a Reply

    Scroll to Top

    Discover more from PiEmbSysTech

    Subscribe now to keep reading and get access to the full archive.

    Continue reading