Challenge 2: Calling increment Five Times — Possible Solution ==================================================================== counter_demo.pl: :- dynamic(counter/1). counter(0). increment :- retract(counter(N)), N1 is N + 1, assertz(counter(N1)). Query: ?- increment, increment, increment, increment, increment. true. ?- counter(X). X = 5. -- Why would this have been impossible using only Course 1's tools? -- -- -- Every predicate available through the end of Course 1 -- facts, -- rules, unification, backtracking, even cut -- could only ever -- QUERY a fixed, unchanging set of relations; none of them could -- change what the database itself contains. counter(0) was written -- once and, without assert/retract, would stay counter(0) forever -- -- there is no way for a plain Prolog rule to "overwrite" a fact -- using Course 1's vocabulary alone. increment only works because -- retract and assertz let it physically remove the old counter(N) -- fact and replace it with a new counter(N1) fact, five separate -- times, with each call seeing the result the previous call left -- behind. That's genuine runtime state persisting across five -- separate top-level calls -- not a fixed relation being queried -- five different ways, but the same relation actually changing. WHY THIS WORKS AS AN ANSWER ------------------------------ This runs the exact chapter example the specified five times, confirms the correct final count, and directly explains why Course 1's purely declarative toolkit -- facts, rules, backtracking, cut -- had no mechanism capable of producing this same call-count-persisting behavior.