Catalog / Prolog Cheat Sheet
Prolog Cheat Sheet
A concise reference for Prolog syntax, predicates, and common programming patterns.
Basic Syntax and Data Types
Facts and Rules
Facts: |
Declare relationships between objects. |
Rules: |
Define conditional relationships. |
Queries: |
Ask questions about the relationships. |
Data Types
Atoms: |
Constants, starting with a lowercase letter. |
Numbers: |
Integers and floating-point numbers. |
Variables: |
Start with an uppercase letter or underscore. |
Structures: |
Complex terms, combining a functor (name) and arguments. |
Lists: |
Ordered collections of terms. |
Operators
|
Rule definition (if). |
|
Conjunction (and). |
|
Disjunction (or). |
= |
Unification (attempt to make terms identical). |
|
Not unifiable. |
List Manipulation
Basic List Operations
Lists are a fundamental data structure in Prolog. |
|
Predicates for List Manipulation
|
Succeeds if
|
|
Succeeds if
|
|
Succeeds if
|
|
Succeeds if
|
Example: Defining `member`
|
Arithmetic Operations
Basic Arithmetic
|
Used to evaluate arithmetic expressions. |
+, -, *, / |
Standard arithmetic operators. |
|
Modulo operator (remainder of division). |
Comparison Operators
|
Arithmetic equality (values are equal). |
|
Arithmetic inequality (values are not equal). |
|
Less than, greater than, less than or equal to, greater than or equal to. |
Example: Factorial
|
Control Flow and Logic
Cut (`!`)
The cut ( |
Use with caution, as it can make programs harder to understand and debug. |
Negation as Failure
|
Succeeds if |
Example: |
|
Conditional Predicates
Prolog doesn’t have explicit |
Example:
If |