Newyo
Newyo

Reputation: 187

How to run prolog on swipl and return all the traces in one go

1: If one query ie. a(X). returns multiple answers, how can I let the program return all the answers and the trace in one go.

2: How can I run a program auto matically i.e  print all answers of a(X). when I load the .pl file to the swipl i.e. after the comman

% swipl -f foo.pl

then it return all the answer of X

Thank you

Upvotes: 1

Views: 2004

Answers (2)

CapelliC
CapelliC

Reputation: 60034

you need leash as well as trace

swipl -s f.pl -g "leash(-all),trace,numbers(X),print(X),nl,fail." -t halt
% /home/carlo/.plrc compiled 0.04 sec, 1,439 clauses
% /home/carlo/prolog/f.pl compiled 0.00 sec, 2 clauses
   Call: (6) numbers(_G1453)
   Call: (7) between(1, 10, _G1453)
   Exit: (7) between(1, 10, 1)
   Exit: (6) numbers(1)
   Call: (6) print(1)
1
   Exit: (6) print(1)
   Call: (6) nl

   Exit: (6) nl
   Call: (6) fail
   Fail: (6) fail
   Redo: (7) between(1, 10, _G1453)
   Exit: (7) between(1, 10, 2)
   Exit: (6) numbers(2)
   Call: (6) print(2)
2
   Exit: (6) print(2)
...

Upvotes: 4

Here is a simple file with simple clause:

$> cat f.pl
numbers(X) :-
  between(1,10,X).

You can use -g option for setting goal:

$> swipl -s f.pl -g "numbers(X),print(X),nl,fail." -t halt.
% .../f.pl compiled 0.00 sec, 2 clauses
1
2
3
4
5
6
7
8
9
10

Upvotes: 3

Related Questions