2 ms·
>> That this process of computation is difficult to grok is especially noticable when you try to debug a Prolog program. Computations get undone when attempts a
by YeGoblynQueenne 8y ago
>> That this process of computation is difficult to grok is especially noticable when you try to debug a Prolog program. Computations get undone when attempts at satisfying a goal fail; other computations get retried down different branches resulting in different unifications and worse of all, the order in which you wrote your clauses in the program makes a difference to how it gets executed and, indeed, whether any part of the program is reachable.
Actually, Prolog's clause selection rule that relies on clause ordering in the database is a boon when it comes to understanding backtracking during debugging ("tracing", please). You know that if you have two clauses of the predicate p/2:
p(a).
p(b).
And you make the query:
?- p(A).
The interpreter will first find the result p(a) and then backtrack to p(b). You know the order in which choice points will be created, that is. This makes it infinitely easier to trace a Prolog program than in a hypothetical (and very impractical) "purely" declarative langauge where clause order wouldn't matter.
Now, tracing a complex program with lots of recursive calls- that can be difficult. But that's not because of backtracking. It's because of the way Prolog "unfolds" recursion, which is something I'd have trouble explaining even after ten ish years of coding in Prolog. It's something you have to develop a feeling for, after tracing a sufficient number of recursive programs. Now _that_ I'd agree is a difficulty that may keep programmers from using the language. But- backtracking? I don't agree.
- segmondy 8y agomost people don't know how to debug prolog because they never got taught her. it has debugging facilities, even GUI debuggers. http://www.swi-prolog.org/pldoc/man?section=debugger http://www.swi-prolog.org/pldoc/man?section=debugger here's an example of a trace from a CLI section. [trace] ?- animal(X). Call: (7) animal(_G1588) ? creep Call: (8) is_true('has fur') ? creep ^ Call: (9) format("~w?\n", ['has fur']) ? creep has fur? ^ Exit: (9) format("~w?\n", ['has fur']) ? creep Call: (9) read(yes) ? creep |: Most don't understand the standard 4-port prolog (call, exit, redo, and fail.) making it hard to grok what's going on. http://www.swi-prolog.org/pldoc/doc_for?object=section(2,%272.9%27,swi(%27/doc/Manual/debugoverview.html%27) http://www.swi-prolog.org/pldoc/doc_for?object=section(2,%27...)
- YeGoblynQueenne 8y agoThat's not the fault of the language though. If you get taught, say, C# and you 're never taught how to debug it you'd have the same problem. The four-port debugger takes some explaining, but it's not the end of the world. It's actually a very conceptually simple way to understand Prolog's execution model. It's a shame that it's not taught more often.