Listing/1 with char codes displayed as characters and not codes?

Viewed 80

Using SWI-Prolog

Version:

?- current_prolog_flag(windows,Value).
Value = true.

?- current_prolog_flag(version,Value).
Value = 80000.

For a simple DCG

constant_value --> "ID".

listing/1 outputs

?- listing(constant_value).

constant_value([73, 68|A], A).

However in my notes I have it as

?- listing(constant_value).

constant_value(['I', 'D'|A], A).

but I don't know what I did to have the character codes appear as characters.

I tried the SWI-Prolog flag double_quotes with various values (chars, codes, string) but can't reproduce the desired output.

How are listing/1 of a DCG created where the character codes are displayed as characters?

1 Answers

I think that you probably had double_quotes flag in codes when constant_value was compiled. The value of this flag won't affect clauses already stored in the database.

constant_value1 --> "ID".
:-set_prolog_flag(double_quotes, chars).
constant_value2 --> "ID".

?- listing(constant_value1).
constant_value1([73, 68|A], A).
?- listing(constant_value2).
constant_value2(['I', 'D'|A], A).

Edit by Guy Coder

Since I use the Q&A at SO as a personal notebook with my own Q&A, I tend to look at the accepted answer for the details; I am adding them here for everyone's benefit.

As this answer correctly states, the problem was caused by the fact that originally in the source code, which was a module stored as a file, I had the statement

:- set_prolog_flag(double_quotes,chars). 

before the code

constant_value --> "ID".

then in the REPL

?- consult('C:/dcg_examples.pl').  

?- listing(constant_value).
constant_value(['I', 'D'|A], A).

true.  

and to use this

?- phrase(constant_value,"ID").
true.

Latter in the source code I changed the value for double_quotes

:- set_prolog_flag(double_quotes,codes). 

notice the change from chars to codes

Restarting the REPL and doing the same

?- consult('C:/dcg_examples.pl').  

?- listing(constant_value).
constant_value([73, 68|A], A).

true.

?- phrase(constant_value,"ID").
true.

gave the different result for listing/1.

However in the REPL using

set_prolog_flag(double_quotes,<VALUE>). 

had no effect on the result of listing/1.

<VALUE> can be one of string, chars, codes, traditional, or atom.

The key point to note here is that listing/1 uses consulted/compiled/stored code. So the value of double_quotes at the time of consulting/compiling/storing is what listing/1 uses. Any changes to double_quotes after that in the REPL will not change the consulted/compiled/stored code and thus the result of listing/1.

To effect a change you have to add

:- set_prolog_flag(double_quotes,<VALUE>). 

in your source code before the predicate then consult/1 to load it and then listing/1.

The meaning of consulted/compiled/stored does not imply that these are three separate actions performed in a sequence, here they define when the source code is put into the database for use by listing/1.

Related