Challenge 3: The Real Scope Difference — Backtracking vs. the [] Monad — Possible Solution ==================================================================== // Concrete example of ORDINARY Prolog code, with nothing special // wrapped around it, that is automatically backtracking-capable: // // color(red). // color(green). // color(blue). // // ?- color(X). // X = red ; // X = green ; // X = blue. // // color/1 is a completely ordinary predicate, defined with plain // facts -- there's no special type, no import, no annotation marking // it as "this one supports multiple results." EVERY predicate in // Prolog works this way by default; multiple matching clauses simply // ARE multiple possible answers, explored automatically via // backtracking whenever a query needs more than the first one. There // is no separate, non-backtracking "default" mode a predicate has to // opt out of. // // Haskell's [] monad, by contrast, is a specific, deliberately-chosen // type. A function has to actually RETURN a list -- e.g. // possibleColors :: [String] -- and CHAIN it through >>= for // nondeterminism to apply at all. An ordinary Haskell function // returning a plain Int, or a Maybe Int, or an IO Int, gets none of // this "explore every combination" behavior automatically -- it has // to be deliberately built using the [] monad specifically, one // function at a time, by a programmer choosing that type on purpose. // // So the real scope difference: in Prolog, EVERY predicate call is // implicitly backtracking-capable, all the time, by default, with // nothing to opt into. In Haskell, nondeterminism-via-[] is one // specific, deliberately-selected tool among several (Maybe, Either, // IO, [] all being separate, explicitly-chosen options) -- exactly // the same "one opt-in type vs. the whole language's own default" // distinction haskell1-5 already drew between Java's own opt-in // Stream laziness and Haskell's language-wide laziness. WHY THIS WORKS AS AN ANSWER ------------------------------ This provides a genuinely ordinary Prolog predicate with no special wrapping to demonstrate automatic backtracking capability, and correctly contrasts it with Haskell's [] monad requiring deliberate, per-function opt-in, matching the chapter's own stated echo of haskell1-5's Java-Streams comparison.