Prolog Coding Horror
8 hours ago
- #Prolog
- #coding-horror
- #declarative-programming
- Prolog programs can be defective by giving wrong answers or missing solutions; missing solutions is worse if irrecoverable.
- Avoid impure/non-monotonic constructs like !/0, (->)/2, var/1 to prevent losing solutions; use clean data, constraints (dif/2), meta-predicates.
- Avoid global state via assertz/1, retract/1 to prevent implicit dependencies; use predicate arguments or semicontext notation.
- Avoid impure output (e.g., format/2) that prints directly; describe solutions as Prolog relations for testability and generality.
- Avoid low-level constructs like (is)/2, (=:=)/2, (>)/2; use higher-level constraints (e.g., CLP(FD)) for easier teaching and declarative code.
- Example horror_factorial shows issues: cut loses solutions, low-level arithmetic causes errors; pure version with constraints works for general queries.
- Recommend staying in pure monotonic Prolog subset, using declarative constructs for generality and performance.