Skip to content
TheChatArchiveThe Chat Archive
Claudeclaude-sonnet-4-20250514-thinking-32k· AnthropicHugging Face LMArena Human Preference 140K

Teach me the básics of swi prolog

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Claudeclaude-3-5-haiku-20241022’s answer.
  1. User

    Teach me the básics of swi prolog

  2. Claude Assistant

    SWI-Prolog is a popular implementation of Prolog, a logic programming language based on formal logic. Here are the fundamentals:

    Core Concepts

    Prolog programs consist of facts, rules, and queries. The system uses logical inference to answer questions based on what you've told it.

    Facts

    Facts state relationships that are always true:

    likes(mary, food).
    likes(john, wine).
    parent(tom, bob).
    parent(pam, bob).
    

    Rules

    Rules define relationships based on conditions:

    happy(X) :- likes(X, wine).
    grandparent(X, Z) :- parent(X, Y), parent(Y, Z).
    

    The :- means "if" - so the first rule says "X is happy if X likes wine."

    Variables and Queries

    Variables start with uppercase letters or underscores. You query the system by asking questions:

    ?- likes(mary, food).     % Returns: true
    ?- likes(X, wine).        % Returns: X = john
    ?- parent(tom, X).        % Returns: X = bob
    

    Unification

    Prolog matches terms through unification. Variables can be bound to values:

    ?- parent(X, Y).
    % Returns all parent-child relationships
    

    Lists

    Lists use square brackets:

    member(X, [X|_]).
    member(X, [_|T]) :- member(X, T).
    
    ?- member(a, [a, b, c]).  % Returns: true
    

    Basic Operations

    % Arithmetic
    ?- X is 2 + 3.           % X = 5
    ?- 5 > 3.                % true
    
    % Comparison
    ?- tom = tom.            % true
    ?- X = mary.             % X = mary
    

    Getting Started

    1. Install SWI-Prolog
    2. Save your facts and rules in a .pl file
    3. Load it with ?- [filename].
    4. Start querying!

    The key insight is that Prolog works backwards from your questions, using the facts and rules you've provided to find answers through logical deduction.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.