Clauses: Prolog programs are built from clauses, which are either facts or rules.
gabi-cool85-gs / Prolog - Cheat Sheet
Prolog - Cheat Sheet
A quick reference guide to Prolog programming, covering syntax, data types, list manipulation, control flow, and built-in predicates.
Syntax Fundamentals
|
Facts: Declare a relationship. |
Rules: Define relationships based on other relationships. |
Queries: Ask questions about the defined relationships. |
Variables: Start with an uppercase letter or underscore. |
Atom: Constant values, starting with lowercase letter. |
Comments: |
Compound Terms: Structures built from a functor and arguments. |
Lists: Ordered collections of items. |
Operators: Symbols for arithmetic, comparison, and logical operations. |
Unification: The process of matching terms. |
Anonymous Variable: Represented by |
Data Types
Atoms: Symbolic constants. |
|
Numbers: Integers and floating-point numbers. |
|
Variables: Represent unknown values. Must start with an uppercase letter or underscore. |
|
Structures: Complex terms built from functors and arguments. |
|
Lists: Ordered collections of elements (see next page). |
|
Operators
Arithmetic operators. |
Addition.
Division. |
Integer division. |
Integer division of X by Y. |
Modulo operator. |
Remainder of X divided by Y. |
Arithmetic evaluation. |
Evaluates Y and unifies the result with X. |
Arithmetic equality. |
True if X and Y evaluate to the same number. |
Arithmetic inequality. |
True if X and Y evaluate to different numbers. |
Comparison operators. |
True if X is greater than Y.
True if X is less than or equal to Y. |
Unification operator. |
True if X and Y can be unified. |
Not unifiable. |
True if X and Y cannot be unified. |
Term equality (identical). |
True if X and Y are identical terms. |
Term inequality (not identical). |
True if X and Y are not identical terms. |
Rule definition. |
Defines a rule where |
List Representation
Empty List: |
List with elements: |
Head and Tail: |
Accessing elements: Prolog lists are typically accessed via unification and recursion, not direct indexing. |
List concatenation: Use Example:
|
Membership test: Use Example:
|
Anonymous variable in lists: Example:
|
Common List Operations
Membership (member/2): Checks if an element is in a list. |
|
Concatenation (append/3): Concatenates two lists. |
|
Prefix (prefix/2): Checks if a list is a prefix of another list. |
|
Suffix (suffix/2): Checks if a list is a suffix of another list. |
|
Sublist (sublist/2): Checks if a list is a sublist of another list. |
|
Length (length/2): Determines the length of a list. |
|
Reverse (reverse/2): Reverses the order of elements in a list. |
|
nth0/3: Access element at index (0-based). |
|
nth1/3: Access element at index (1-based). |
|
select/3: Select an element from a list, resulting in a new list without that element. |
|
last/2: Retrieves the last element of a list. |
|
delete/3: Deletes all occurrences of an element from a list. |
|
Example: List processing
|
|
|
|
|
|
Control Flow
Conjunction ( |
Disjunction ( |
Negation ( |
If-Then-Else ( |
Call: |
Once: |
Repeat: Example:
|
Fail: |
True: |
Useful Built-in Predicates
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Family Relationships
|
Queries: |
List Manipulation
|
Queries: |
Simple AI: Expert System
|
Queries: |