Most novices try to write a single rule using a long and complicated control structure that produces the desired answer. The rule typically doesn't work, so they start adding additional control structures and conditions to fix the corner cases. Suprisingly quickly, it gets very difficult to read the rule. Recognise this? You need to write monolitic queries in query languages such as SQL or SPARQL. Prolog allows for composing rules from simpler ones. Use composition!
First of all, make sure your program consists of small predicates, where
the body (the part after the
:- operator) is typically a
simple sequence (conjunction) of sub-queries (goals). In
( If -> Then ; Else ), and in particular nested
Following the above guidelines (1) makes your program easier to read, (2) makes it easier to formulate new queries (code reuse) and (3) makes the program easier to debug.
If the answer is wrong, you may consider tracing the execution of the
query. To start debugging at a particular place in the source, set a
breakpoint by clicking a line number in the gutter of
the editor. To trace from the beginning, use the menu Solutions
-> Debug (trace). Note that you can add a (conditional) call to
trace/0 anywhere in the program to start debugging at that
In trace mode, you single-step through the execution of the program. The SWISH debugger only shows calls appearing in the current source files; details for called predicates are hidden. The following buttons are displayed below the port
If your query contains an error, such as dividing by zero or adding a number to an atom, Prolog will raise an exception. SWISH tries to start the tracer as soon as possible. Unfortunately, Prolog optimization sometimes causes that it cannot stop right there or that much of the execution context is not available. In that case, run the query in debug mode as illustrated below.
?- debug, goal.