Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write-option max_depth/1 #250

Open
UWN opened this issue Jul 10, 2023 · 10 comments
Open

Write-option max_depth/1 #250

UWN opened this issue Jul 10, 2023 · 10 comments

Comments

@UWN
Copy link

UWN commented Jul 10, 2023

This is currently an implementation specific extension, but it is handled incorrectly:

?- write_term(t,[max_depth(-1)]).
t   true, unexpected.
   domain_error(write_option,max_depth(-1)). % expected
?- write_term(t,[max_depth(a)]).
t   true, unexpected.
   domain_error(write_option,max_depth(a)). % expected
?- write_term(t,[max_depth(1+1)]).
t   true, unexpected.
   domain_error(write_option,max_depth(1+1)). % expected

p(1).
p(X+1):-p(X).

?- p(L), write_term(L,[max_depth(3)]).
?-  p(L), write_term(L,[max_depth(3)]).
1   L = 1
; 1+1 L = 1+1
; 1+1+1 L = 1+1+1
; 1+1+1+1 L = 1+1+1+1 % unexpected. Expected ... + ... +1+1
;  ... .
@UWN
Copy link
Author

UWN commented Jul 10, 2023

And here it is perfect:

?- length(L,N), write_term(L,[max_depth(3)]).
[]   L = [], N = 0
; [_13] L = [_A], N = 1
; [_13,_18] L = [_A,_B], N = 2
; [_13,_18,_23] L = [_A,_B,_C], N = 3
; [_13,_18,_23|...] L = [_A,_B,_C,_D], N = 4
;  ... .

@UWN
Copy link
Author

UWN commented Jul 10, 2023

Trealla Prolog (c) Infradig 2020-2023, v2.21.31
?- write_term(t,[max_depth(-1)]).
   error(domain_error(write_option,max_depth(-1)),write_term/2).
t?- 
^^ unexpected t

@infradig
Copy link
Contributor

infradig commented Jul 10, 2023 via email

@UWN UWN closed this as completed Jul 10, 2023
@UWN UWN reopened this Jul 10, 2023
@pmoura
Copy link

pmoura commented Jul 10, 2023

Pushed tests for this de facto standard option (of the Prolog systems supported by Logtalk, only B-Prolog, CxProlog, and JIProlog fail to support it):

$ logtalk_tester -p trealla -s Documents/Logtalk/
% Batch testing started @ 2023-07-10 10:20:44
%         Logtalk version: 3.68.0-b01
%         Trealla Prolog version: 2.21.32
%
% /Users/pmoura/Documents/Logtalk/logtalk3/tests/prolog/predicates/write_term_3
%         130 tests: 0 skipped, 130 passed, 0 failed (0 flaky)
%         completed tests from object tests in 2 seconds
%         clause coverage n/a
%
% 1 test sets: 1 completed, 0 skipped, 0 broken, 0 timedout, 0 crashed
% 130 tests: 0 skipped, 130 passed, 0 failed (0 flaky)
%
% Batch testing ended @ 2023-07-10 10:20:47

The main difference I found in error-checking is with negative depth values. Ciao Prolog, LVM, SICStus Prolog, Trealla Prolog, and XSB throw a domain error. But others like ECLiPSe, GNU Prolog, and SWI-Prolog accept them. YAP doesn't try to type-check the depth.

@UWN
Copy link
Author

UWN commented Jul 10, 2023

?- D=3,write_term([1,2,3,4],[max_depth(3)]).
[1,2,3|...]   D = 3. % OK
?- D=3,write_term([1,2,3,4],[max_depth(D)]).
[1,2,3,4]   D = 3, unexpected.

@infradig
Copy link
Contributor

infradig commented Jul 10, 2023 via email

@UWN
Copy link
Author

UWN commented Jul 11, 2023

Taking the observations from didoudiaz/gprolog#56

  1. The printed term is exactly like the original term with some non-variable subterms replaced by the atom ...

  2. When replacing the atom ... by fresh new variables, the resulting term has the original term as an instance.

The 2nd point now works, the first not.

Here is a comparison with SICStus which seems the way to go except for 4-1:

?- nth1(I,[1+2+3+4,_^_^_^_^_,1^2^3^4^nil,[1,2,3,4],[_,_,_,_]],T),
   between(0,5,D), write(I-D), write(' '), write_term(T,[max_depth(D)]), nl, false.
%    SICStus                       % Trealla
1-0 1+2+3+4                        1-0 1+2+3+4
1-1 ... + ...                      1-1 ... + ...
1-2 ... + ... +4                   1-2 ... + ... +4
1-3 ... + ... +3+4                 1-3 ... + ... +3+4
1-4 1+2+3+4                        1-4 1+2+3+4
1-5 1+2+3+4                        1-5 1+2+3+4
2-0 _1143^_1179^_1215^_1251^_1287  2-0 _1^_2^_3^_4^_5
2-1 _1143^ ...                     2-1 ... ^ ...
2-2 _1143^_1179^ ...               2-2 _1^ ... ^ ...
2-3 _1143^_1179^_1215^ ...         2-3 _1^_2^ ... ^ ...
2-4 _1143^_1179^_1215^_1251^_1287  2-4 _1^_2^_3^ ... ^ ...
2-5 _1143^_1179^_1215^_1251^_1287  2-5 _1^_2^_3^_4^_5
3-0 1^2^3^4^nil                    3-0 1^2^3^4^nil
3-1 ... ^ ...                      3-1 ... ^ ...
3-2 1^ ... ^ ...                   3-2 1^ ... ^ ...
3-3 1^2^ ... ^ ...                 3-3 1^2^ ... ^ ...
3-4 1^2^3^ ... ^ ...               3-4 1^2^3^ ... ^ ...
3-5 1^2^3^4^nil                    3-5 1^2^3^4^nil
4-0 [1,2,3,4]                      4-0 [1,2,3,4]
4-1 [...|...]                      4-1 [...|...]                            Suggested: [1|...]
4-2 [1,2|...]                      4-2 [...,...|...]
4-3 [1,2,3|...]                    4-3 [1,2,3|...]
4-4 [1,2,3,4]                      4-4 [1,2,3,4]
4-5 [1,2,3,4]                      4-5 [1,2,3,4]
5-0 [_1581,_1613,_1645,_1677]      5-0 [_6,_7,_8,_9]
5-1 [_1581|...]                    5-1 [...|...]
5-2 [_1581,_1613|...]              5-2 [...,...|...]
5-3 [_1581,_1613,_1645|...]        5-3 [_6,_7,_8|...]
5-4 [_1581,_1613,_1645,_1677]      5-4 [_6,_7,_8,_9]
5-5 [_1581,_1613,_1645,_1677]      5-5 [_6,_7,_8,_9]

@pmoura
Copy link

pmoura commented Jul 11, 2023

Trealla Prolog (c) Infradig 2020-2023, v2.21.31
?- write_term(t,[max_depth(-1)]).
error(domain_error(write_option,max_depth(-1)),write_term/2).
t?-
^^ unexpected t

Added a test for this unexpected output. With v2.22.0 (916748d), this and other new tests all pass:

$ logtalk_tester -p trealla -s ~/Documents/Logtalk/
% Batch testing started @ 2023-07-11 11:02:59
%         Logtalk version: 3.68.0-b01
%         Trealla Prolog version: 2.22.0
%
% logtalk3/tests/prolog/predicates/write_term_3
%         134 tests: 0 skipped, 134 passed, 0 failed (0 flaky)
%         completed tests from object tests in 1 second
%         clause coverage n/a
%
% 1 test sets: 1 completed, 0 skipped, 0 broken, 0 timedout, 0 crashed
% 134 tests: 0 skipped, 134 passed, 0 failed (0 flaky)
%
% Batch testing ended @ 2023-07-11 11:03:02
``

@UWN
Copy link
Author

UWN commented Jul 13, 2023

Now:

?-  nth0(I,[A+B+C+D,1+2+3+4,A^B^C^D^E,1^2^3^4^nil,[1,2,3,4],[A,B,C,D],[- -A,- -B,- -C,- -D],[[A,B,C,D]],[[[[[A,B,C,D]]]]]],T),
     between(0,5,M), write(I-M), write(' '),
     write_term(T,[variable_names(['A'=A,'B'=B,'C'=C,'D'=D,'E'=E]),max_depth(M)]), nl, false.

SICStus                     Trealla
0-0 A+B+C+D                 0-0 A+B+C+D                             
0-1 ... +D                  0-1 ... + ...                           
0-2 ... +C+D                0-2 ... + ... +D                        
0-3 A+B+C+D                 0-3 ... + ... +C+D                      
0-4 A+B+C+D                 0-4 A+B+C+D                             
0-5 A+B+C+D                 0-5 A+B+C+D                             
1-0 1+2+3+4                 1-0 1+2+3+4                             
1-1 ... + ...               1-1 ... + ...                           
1-2 ... + ... +4            1-2 ... + ... +4                        
1-3 ... + ... +3+4          1-3 ... + ... +3+4                      
1-4 1+2+3+4                 1-4 1+2+3+4                             
1-5 1+2+3+4                 1-5 1+2+3+4                             
2-0 A^B^C^D^E               2-0 A^B^C^D^E                           
2-1 A^ ...                  2-1 ... ^ ...                           
2-2 A^B^ ...                2-2 A^ ... ^ ...                        
2-3 A^B^C^ ...              2-3 A^B^ ... ^ ...                      
2-4 A^B^C^D^E               2-4 A^B^C^ ... ^ ...                    
2-5 A^B^C^D^E               2-5 A^B^C^D^E                           
3-0 1^2^3^4^nil             3-0 1^2^3^4^nil                         
3-1 ... ^ ...               3-1 ... ^ ...                           
3-2 1^ ... ^ ...            3-2 1^ ... ^ ...                        
3-3 1^2^ ... ^ ...          3-3 1^2^ ... ^ ...                      
3-4 1^2^3^ ... ^ ...        3-4 1^2^3^ ... ^ ...                    
3-5 1^2^3^4^nil             3-5 1^2^3^4^nil                         
4-0 [1,2,3,4]               4-0 [1,2,3,4]                           
4-1 [...|...]               4-1 [1|...]                             
4-2 [1,2|...]               4-2 [1,2|...]                           
4-3 [1,2,3|...]             4-3 [1,2,3|...]                         
4-4 [1,2,3,4]               4-4 [1,2,3,4]                           
4-5 [1,2,3,4]               4-5 [1,2,3,4]                           
5-0 [A,B,C,D]               5-0 [A,B,C,D]                           
5-1 [A|...]                 5-1 [A|...]                             
5-2 [A,B|...]               5-2 [A,B|...]                           
5-3 [A,B,C|...]             5-3 [A,B,C|...]                         
5-4 [A,B,C,D]               5-4 [A,B,C,D]                           
5-5 [A,B,C,D]               5-5 [A,B,C,D]                           
6-0 [- -A,- -B,- -C,- -D]   6-0 [- -A,- -B,- -C,- -D]               
6-1 [...|...]               6-1 [- ...|...]                         
6-2 [- ...,- ...|...]       6-2 [- ...,- ...|...]                   
6-3 [- -A,- -B,- ...|...]   6-3 [- ...,- ...,- ...|...]             
6-4 [- -A,- -B,- -C,- ...]  6-4 [- -...,- -...,- -...,- -...]       
6-5 [- -A,- -B,- -C,- -D]   6-5 [- -A,- -B,- -C,- -D]               
7-0 [[A,B,C,D]]             7-0 [[A,B,C,D]]                         
7-1 [...]                   7-1 [[...|...]]                         
7-2 [[A|...]]               7-2 [[...,...|...]]                     
7-3 [[A,B|...]]             7-3 [[A,B,C|...]]                       
7-4 [[A,B,C|...]]           7-4 [[A,B,C,D]]                         
7-5 [[A,B,C,D]]             7-5 [[A,B,C,D]]                         
8-0 [[[[[A,B,C,D]]]]]       8-0 [[[[[A,B,C,D]]]]]
8-1 [...]                   8-1 [[...]]          
8-2 [[...]]                 8-2 [[...]]          
8-3 [[[...]]]               8-3 [[[...]]]        
8-4 [[[[...]]]]             8-4 [[[...]]]        
8-5 [[[[[A|...]]]]]         8-5 [[[[...]]]]

Do you really intend to replace variables by ...? It starts with 0-1... 4-1 is definitely an improvement. But what happens in 8 is not clear to me at all, with increasing depth, also the lists should resurface, one by one.

@infradig
Copy link
Contributor

infradig commented Jul 13, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants