Prolog is a logic programming language associated with artificial intelligence and computational linguistics. It is based on formal logic and is particularly well-suited for problems involving complex relationships and pattern matching.
Prolog Anti-Patterns Overview
Using Cut (!) Incorrectly
Relying on Specific Clause Order
Not Using Tail Recursion
Using assert/retract for State
assert/1
and retract/1
for managing state. These predicates modify the global database, making your code harder to reason about, test, and parallelize. Instead, pass state explicitly through arguments or use techniques like DCGs (Definite Clause Grammars) for state threading.Ignoring Determinism
Inefficient Use of Built-in Predicates
Not Using Indexing Effectively
Using Cuts to Prevent Exploration
Not Using Higher-Order Predicates
maplist/2-5
, foldl/4
, and include/3
to abstract common patterns and reduce code duplication. Many Prolog implementations provide these predicates in their standard libraries.Not Using Modules
Not Using Proper Error Handling
catch/3
to catch and handle exceptions, and ensure resources are properly cleaned up using call_cleanup/2
or similar mechanisms.Not Using Declarative Programming
Not Using Database Abstraction
Not Using Proper Documentation
Not Writing Tests
plunit
to structure and run your tests.Not Using Type Checking
must_be/2
, integer/1
, number/1
, etc., to validate inputs before processing them.Not Using Constraint Logic Programming
clpfd
(Constraint Logic Programming over Finite Domains) allow you to express constraints declaratively and let the solver find solutions efficiently.Not Using DCGs for Parsing