| Did you know ... | Search Documentation: |
| Pack starlog -- COMPRESSION_FEATURE.md |
The maximal compression feature adds the ability to nest Starlog expressions when converting from Prolog to Starlog notation. This creates more concise, readable code by eliminating unnecessary intermediate variables.
% With compression
?- starlog_output_code((string_concat("hello"," ",T1), string_concat(T1,"world",T2)), _, [compress(true)]).
A is "hello":" ":"world"
% Without compression (default)
?- starlog_output_code((string_concat("hello"," ",T1), string_concat(T1,"world",T2)), _).
A is "hello":" ",B is A:"world"
% Compress entire file
?- starlog_output_file('input.pl', user_output, [compress(true)]).
% Write compressed output to file
?- open('output.pl', write, Stream),
starlog_output_file('input.pl', Stream, [compress(true)]),
close(Stream).
The compression algorithm:
% Prolog input
greet(First, Last, Greeting) :-
string_concat("Hello, ", First, Temp),
string_concat(Temp, " ", Temp2),
string_concat(Temp2, Last, Greeting).
% Compressed Starlog output
greet(A,B,C):-C is "Hello, ":A:" ":B.
% Prolog input
combine_and_reverse(A, B, Result) :-
append(A, B, Combined),
reverse(Combined, Result).
% Compressed Starlog output
combine_and_reverse(A,B,C):-C is reverse(A&B).
% Prolog input
process(X, Y, Z) :-
string_concat("Hello", X, Temp),
string_concat(Temp, "world", Y),
string_concat(Temp, "friend", Z).
% Compressed Starlog output (Temp used twice, so not nested)
process(A,B,C):-D is "Hello":A,B is D:"world",C is D:"friend".
% Prolog input
complex(A, B, C, Result) :-
string_concat(A, B, AB),
string_concat(AB, C, ABC),
string_length(ABC, Len),
atom_concat(result, Len, Result).
% Compressed Starlog output
complex(A,B,C,D):-D is result•string_length(A:B:C).
starlog_output_code(+Goal, -StarlogCode, +Options)
Options:
compress(true) - Apply maximal compressioncompress(false) - No compression (default)starlog_output_file(+FilePath, +OutputStream, +Options)
Options:
compress(true) - Apply maximal compressioncompress(false) - No compression (default)The compression algorithm excludes:
(Cond -> Then)(Cond -> Then ; Else)(A ; B)\+ Goal, not(Goal)!Comprehensive tests are included in:
tests/test_compression.pl - Goal-level compression teststests/test_file_compression.pl - File-level compression tests
Run tests:
cd tests swipl -s test_compression.pl swipl -s test_file_compression.pl
Potential improvements: