SWI-Prolog how to show entire answer (list)?
Asked Answered
L

5

8

I'm trying to convert a string to a list of ascii-codes like so:

7 ?- string_to_list("I'm a big blue banana in space!", C).
C = [73, 39, 109, 32, 97, 32, 98, 105, 103|...].

8 ?- 

This doesn't give me the entire list as you can see, but I need it.

This solution does not work: I can't press w since it gives me the answer and does a full stop. Neither does this: I can call the function alright, and it returns true, but the list still isn't fully displayed.

11 ?- set_prolog_flag(toplevel_print_options,[quoted(true), portray(true), max_depth(0), spacing(next_argument)]).
true.

12 ?- string_to_list("I'm a big blue banana in space!", C).
C = [73, 39, 109, 32, 97, 32, 98, 105, 103|...].

13 ?- 

Any help appreciated!

Likelihood answered 2/10, 2015 at 15:22 Comment(2)
You can use the following trick that puts you into a situation where you can press "w": Add an artificial choice point by appending ; 0=1. to your query, for example: ?- your_goal ; 0=1. or a bit longer: ?- your_goal ; false.. On backtracking, press "w" to print everything.Mccomb
@Mccomb right now, with the latest SWI-Prolog development release, doing this once causes all subsequent queries on the top level to show the full terms of all variable bindings. Is that a feature?Zola
G
11
?- set_prolog_flag(answer_write_options,[max_depth(0)]).
true.

?- string_to_list("I'm a big blue banana in space!", C).
C = [73,39,109,32,97,32,98,105,103,32,98,108,117,101,32,98,97,110,97,110,97,32,105,110,32,115,112,97,99,101,33].

there should be somewhere here on SO the same answer... I'm using the last SWI-prolog release, just compiled...

Gagne answered 2/10, 2015 at 15:42 Comment(1)
Any way to use some config file to make this the default upon start? (Edited: answered bellow by @sljTech )Sender
R
4

If you are finding yourself using string_codes/2 or atom_codes/2 a lot, reconsider your approach. You can use chars in place of codes and avoid the SWI-specific string datatype altogether. All this, by setting a Prolog flag:

?- set_prolog_flag(double_quotes, chars).
true.

?- Chs = "Codes are unreadable!".
Chs = ['C', o, d, e, s, ' ', a, r, e|...].

This is more readable than [67, 111, 100, 101, 115, 32, 97, 114, 101|...], but it still does not solve your problem. However, you can now display such answers compactly using library(double_quotes).

?- use_module(double_quotes).
true.

?- Chs = "Codes are unreadable!".
Chs = "Codes are unreadable!".

?- Chs = "Codes are unreadable", Chs = [C|Chs2].
Chs = "Codes are unreadable",
C = 'C',
Chs2 = "odes are unreadable".

With this setting you can process text much more conveniently. It also fits nicely with DCGs.

Robeson answered 7/5, 2016 at 11:59 Comment(0)
Z
2

Just to put the comment by @mat in an answer:

?- string_codes("string_to_list/2 is deprecated; use string_codes/2!", Codes)
   ;
   false.
Codes = [115, 116, 114, 105, 110, 103, 95, 116, 111|...] /* press w */ [write]
Codes = [115, 116, 114, 105, 110, 103, 95, 116, 111, 95, 108, 105, 115, 116, 47, 50, 32, 105, 115, 32, 100, 101, 112, 114, 101, 99, 97, 116, 101, 100, 59, 32, 117, 115, 101, 32, 115, 116, 114, 105, 110, 103, 95, 99, 111, 100, 101, 115, 47, 50, 33] /* press enter */.

However, from here on all terms will be shown completely. This might get annoying.

What you can also do is just use one of the printing predicates:

?- ..., writeln(Codes).

but this is frowned upon for some reason. It is definitely useful if you have several bindings reported in an answer, but you only want to look at the full value of one of the variables:

?- numlist(1,1000,L),
   Codes = `This is a code list in SWI-Prolog V7`.
L = [1, 2, 3, 4, 5, 6, 7, 8, 9|...],
Codes = [84, 104, 105, 115, 32, 105, 115, 32, 97|...].

?- numlist(1,1000,L),
   Codes = `This is a code list in SWI-Prolog V7`,
   writeln(Codes).
[84,104,105,115,32,105,115,32,97,32,99,111,100,101,32,108,105,115,116,32,105,110,32,83,87,73,45,80,114,111,108,111,103,32,86,55]
L = [1, 2, 3, 4, 5, 6, 7, 8, 9|...],
Codes = [84, 104, 105, 115, 32, 105, 115, 32, 97|...].
Zola answered 3/10, 2015 at 8:13 Comment(0)
I
1

You can solve your question with this,

?- set_prolog_flag(answer_write_options,[max_depth(0)]).
true.

However, if you plan to always show all results and not being shortlisted, then you can add this line in the "init".

Navigation: SWI-Prolog >Settings > User init File

You will be asked to create a swipl.ini file if you have never created one.

At the end of the day, you will not need to key in the above line of code when you start prolog again in the future.

Ignominy answered 12/4, 2017 at 3:21 Comment(1)
For linux systems add this line :- set_prolog_flag(answer_write_options,[max_depth(0)]). to a file named .swiplrc in your current directory.Sender
W
0

I've found two ways.


1.

?- set_prolog_flag(answer_write_options,[max_depth(0)]).
true.

Then do your command that is printing a truncated list.

(set_prolog_flag documentation)


2.

?- atom_chars(goodbye_prolog, X) ; true.

(AllOutput documentation)

Put ; true. at the end of the call that results in a long list. Then push the w key on your keyboard. The result is:

?- sudoku([_,_,2,3,_,_,_,_,_,_,_,_,3,4,_,_], Solution); true.
Solution = [4, 1, 2, 3, 2, 3, 4, 1, 1|...] [write]
Solution = [4, 1, 2, 3, 2, 3, 4, 1, 1, 2, 3, 4, 3, 4, 1, 2] ;
true.
Wilhite answered 29/4, 2016 at 23:20 Comment(2)
IMO this answer repeats the older ones by CapelliC and Boris... or did I miss something?Hydrazine
I think you might be right. I didn't notice they were both here already.Wilhite

© 2022 - 2024 — McMap. All rights reserved.