简体   繁体   中英

Prolog: Export the result of listing/1 to a file

I would like to export/save the result which listing/1 prints out in the prolog "window" to a text file. Is there any way to do this?

my code:

parent(a,b).
parent(c,d).
parent(f,g).

list_items(G) :-
    current_predicate(G),
    listing(G).

and the output in prolog which i would like to save to a text file:

| ?- list_items(X).

% file: C:/GNU-Prolog/file.pl

parent(a, b).
parent(c, d).
parent(f, g).

X = parent/2 ? ;

% file: C:/GNU-Prolog/file.pl

list_items(A) :-
     current_predicate(A),
     listing(A).

 X = list_items/1 ? ;

 no

(I am using GNU prolog if that helps :D ) Thank you for your help!!

You can change the stream output to a file by calling tell/1 , list what you want and then call told/0 .

eg:

  tell('MyListing.txt'), 
  listing(A), 
  told.

Edit after comments:

If you intend to let listing/1 backtrack through all matching procedures then you may need to split the tell/listing/told across two clauses.

ie:

list_items(A) :-
     tell('MyListing.txt'), 
     current_predicate(A),
     listing(A),
     fail.
list_items(_) :- told.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM