| Did you know ... | Search Documentation: |
| Pack starlog -- OUTPUT_STYLE_IMPLEMENTATION.md |
This document describes the implementation of the output_style option for the Starlog library, which allows users to convert between nested function call syntax and method chaining syntax when outputting Starlog code.
Add output_style(nested_calls) or output_style(method_chaining) options to starlog_to_prolog_file() and starlog_output_code().
Implemented the output_style option for starlog_output_code/3 and starlog_output_file/3 with two transformation modes:
output_style(nested_calls)
Converts method chains (using >> operator) to nested function calls.
Example:
Input: X is reverse([1,2,3]) >> length Output: X is length(reverse([1,2,3]))
Usage:
?- starlog_output_code(
(X is reverse([1,2,3]) >> length),
Code,
[output_style(nested_calls), print(true)]
).
% Outputs: A is length(reverse([1,2,3]))
output_style(method_chaining)Converts nested function calls to method chains.
Example:
Input: X is length(reverse([1,2,3])) Output: X is reverse([1,2,3]) >> length
Usage:
?- starlog_output_code(
(X is length(reverse([1,2,3]))),
Code,
[output_style(method_chaining), print(true)]
).
% Outputs: A is reverse([1,2,3])>>length
The implementation handles multiple levels of nesting/chaining:
% Chain to nested Input: X is sort([3,1,2]) >> reverse >> length Output: X is length(reverse(sort([3,1,2]))) % Nested to chain Input: X is length(reverse(sort([3,1,2]))) Output: X is sort([3,1,2]) >> reverse >> length
Works with Starlog operators (:, &, •):
% List append as chain base Input: X is ([1,2]&[3,4]) >> reverse >> length Output: X is length(reverse([1,2]&[3,4])) % Extract operator base when converting to chain Input: X is length(reverse([1,2]&[3,4])) Output: X is [1,2]&[3,4] >> reverse >> length
Transformations are reversible:
Original: X is reverse([1,2,3]) >> length → Nested: X is length(reverse([1,2,3])) → Chain: X is reverse([1,2,3]) >> length
The output_style option works seamlessly with other options:
% With compression
?- starlog_output_code(
(string_concat("a", "b", T1), string_concat(T1, "c", T2)),
Code,
[compress(true), output_style(method_chaining), print(true)]
).
% Outputs: A is "a":"b":"c"
% With variable renaming
?- starlog_output_code(
(X is reverse([1,2,3]) >> length),
Code,
[output_style(nested_calls), rename(true), print(true)]
).
% Outputs: A is length(reverse([1,2,3]))
apply_output_style(+Code, +Style, -TransformedCode)
transform_chains_to_nested(+Code, -NestedCode)
transform_nested_to_chains(+Code, -ChainCode)
Base >> Method1 >> Method2 >> ...)Base >> Method1 >> Method2 >> ...starlog_output_code(+Goal, -StarlogCode, +Options)
Options:
output_style(nested_calls) - Convert method chains to nested callsoutput_style(method_chaining) - Convert nested calls to method chainscompress(true/false), print(true/false), rename(true/false), etc.starlog_output_file(+FilePath, +OutputStream, +Options)
Options:
output_style(nested_calls) - Convert method chains to nested callsoutput_style(method_chaining) - Convert nested calls to method chainscompress(true/false), etc.
Created comprehensive test suite in tests/test_output_style.pl:
Created demonstration file demo_output_style.pl showing:
?- use_module(starlog).
% Convert chain to nested
?- starlog_output_code(
(X is reverse([1,2,3]) >> length),
Code,
[output_style(nested_calls)]
).
Code = (_ is length(reverse([1,2,3]))).
% Convert nested to chain
?- starlog_output_code(
(X is length(reverse([1,2,3]))),
Code,
[output_style(method_chaining)]
).
Code = (_ is reverse([1,2,3])>>length).
% Convert a Starlog file to show code in nested style
?- starlog_output_file(
'my_code.pl',
user_output,
[output_style(nested_calls)]
).
% Convert a Starlog file to show code in chaining style
?- starlog_output_file(
'my_code.pl',
user_output,
[output_style(method_chaining)]
).
% Compress and convert to chain style
?- starlog_output_code(
(string_concat("a", "b", T1), string_concat(T1, "c", T2)),
Code,
[compress(true), output_style(method_chaining), print(true)]
).
% Outputs: A is "a":"b":"c"
All changes are backward compatible:
output_style option is optionalPotential future improvements: