Teach me the básics of swi prolog
- User
Teach me the básics of swi prolog
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 = bobUnification
Prolog matches terms through unification. Variables can be bound to values:
?- parent(X, Y). % Returns all parent-child relationshipsLists
Lists use square brackets:
member(X, [X|_]). member(X, [_|T]) :- member(X, T). ?- member(a, [a, b, c]). % Returns: trueBasic Operations
% Arithmetic ?- X is 2 + 3. % X = 5 ?- 5 > 3. % true % Comparison ?- tom = tom. % true ?- X = mary. % X = maryGetting Started
- Install SWI-Prolog
- Save your facts and rules in a
.plfile - Load it with
?- [filename]. - 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.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.