3

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?

Guy Coder
  • 24,501
  • 8
  • 71
  • 136
  • Of interest: [Prolog DCG set_prolog_flag double_quotes source code directive location matters; documentation?](https://stackoverflow.com/q/45390050/1243762) – Guy Coder Jan 17 '19 at 16:46
  • See [this answer](https://stackoverflow.com/questions/8264699/what-is-the-difference-between-and-in-prolog/8269897#8269897). – false Jan 17 '19 at 17:26

1 Answers1

4

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.

Guy Coder
  • 24,501
  • 8
  • 71
  • 136
gusbro
  • 22,357
  • 35
  • 46
  • I was writing an answer after I figured it out. Your answer is correct, but I don't think it has enough detail for others that read this in the future. Care if I accept this answer and then modify it with details? – Guy Coder Jan 17 '19 at 16:45
  • @GuyCoder: I'd say you do whatever benefits others the most in the future, including adding and accepting your own answer if you think that would be better (I've seen you writing very detailed posts here ;) – gusbro Jan 17 '19 at 17:14