scryer-prolog icon indicating copy to clipboard operation
scryer-prolog copied to clipboard

Wrong output of write_term/2, max_depth/1

Open flexoron opened this issue 1 year ago • 8 comments

?- A = [A|c], write_term(A,[max_depth(0)]),nl.
[...|c]
   A = [A|c].
?- A = [A|c], write_term(A,[max_depth(1)]),nl.
[...|...]
   A = [A|c].
?- A = [A|c], write_term(A,[max_depth(2)]),nl.
[c,...|...] % unexpected
   A = [A|c].
?- A = [A|c], write_term(A,[max_depth(3)]),nl.
[c,c,...|...] % unexpected
   A = [A|c].
?-

For example: [c,c,..|...] should be displayed like so [c,c,...|c] ?
EDIT: ... like so [[[...]|c]|c] ?

flexoron avatar Jan 10 '24 23:01 flexoron

Let's first unfold the term for a few levels:

?- A = [A|c], A == [[A|c]|c], A == [[[A|c]|c]|c], A == [[[[A|c]|c]|c]|c].
   A = [A|c].

So, A in this example is not of the shape [c|...], and therefore the cases where the output starts with [c are wrong output.

?- A = [A|c], A = [c|_].
   false.

The output must truthfully capture the shape of the term.

triska avatar Jan 10 '24 23:01 triska

Like SWI does:
?- A = [A|c], write_term(A,[max_depth(6)]),nl.
[[[[[[...|...]|...]|c]|c]|c]|c]

So output should not start with [c and should end with |c] (after a certain depth)

flexoron avatar Jan 10 '24 23:01 flexoron

A much cleaner approach would be to first transform the term to a term that contains the atom ... at certain places and then to display it. There is a lot of code in Rust which does not belong there.

UWN avatar Jan 11 '24 05:01 UWN

Like GNU then: | ?- A = [A|c], write_term(A,[max_depth(3)]),nl. [[[...]|c]|c]

flexoron avatar Jan 11 '24 08:01 flexoron

Is it a good idea that [...] stands here for a non-list? The term A = [A|c] does not contain any list. [[...|c]|c] might be a better abbreviation. (These abbreviations are everywhere a chaos)

UWN avatar Jan 11 '24 08:01 UWN

So, someone can say the term contains list notation.

flexoron avatar Jan 11 '24 09:01 flexoron

It means that one cannot transform a term to an abbreviated term just by replacing in adequate positions some terms by the atom .... In stead, much more is happening. And every system does it differently.

UWN avatar Jan 11 '24 09:01 UWN

I think, following the pattern of how Scryer does things like the toplevel, that the thing we should do here is the one that is more close to actual Prolog and transformations of it. So I support the idea @UWN gave of doing some term substitution in Prolog for the implementation.

bakaq avatar Jan 11 '24 09:01 bakaq