| Did you know ... | Search Documentation: |
| Pack starlog -- STARLOG_TO_PROLOG.md |
The Starlog to Prolog conversion feature allows you to convert Starlog syntax back to standard Prolog with maximal decompression. This is the inverse of the Prolog to Starlog converter, and together they provide bidirectional conversion between the two notations.
:, &, •) to standard Prolog predicatesUse starlog_to_prolog_code/1 to convert a single Starlog goal:
?- starlog_to_prolog_code(A is "hello":"world").
string_concat("hello","world",A)
?- starlog_to_prolog_code(A is [1,2]&[3,4]).
append([1,2],[3,4],A)
?- starlog_to_prolog_code(A is hello•world).
atom_concat(hello,world,A)
The converter automatically decompresses nested expressions:
% Simple nesting
?- starlog_to_prolog_code(A is reverse([1,2]&[3,4])).
append([1,2],[3,4],A),reverse(A,B)
% Multiple levels of nesting
?- starlog_to_prolog_code(A is "hello":" ":"world").
string_concat("hello"," ",A),string_concat(A,"world",B)
% Complex nested expression
?- starlog_to_prolog_code(Result is reverse([1]&[2]&[3])).
append([1],[2],A),append(A,[3],B),reverse(B,C)
Use starlog_to_prolog_file/1 to convert an entire file:
?- starlog_to_prolog_file('my_starlog_code.pl').
% Prolog code output for file: my_starlog_code.pl
greet(A,B):-string_concat("Hello, ",A,B).
combine_and_reverse(A,B,C):-append(A,B,D),reverse(D,C).
process_text(A,B,C):-string_concat(A," ",D),string_concat(D,B,C).
...
Write to a file using starlog_to_prolog_file/2:
?- open('output.pl', write, Stream),
starlog_to_prolog_file('input_starlog.pl', Stream),
close(Stream).
starlog_to_prolog_code(+StarlogGoal)
Convert a Starlog goal to Prolog and output it.
Parameters:
?- starlog_to_prolog_code(A is "x":"y").
string_concat("x","y",A)
starlog_to_prolog_code(+StarlogGoal, -PrologCode)
Convert a Starlog goal to Prolog and return it as a term.
Parameters:
?- starlog_to_prolog_code(A is "x":"y", Code).
Code = string_concat("x","y",A).
starlog_to_prolog_code(+StarlogGoal, -PrologCode, +Options)
Convert a Starlog goal to Prolog with options.
Parameters:
decompress(true/false), default is true)
Example:
?- starlog_to_prolog_code(A is "x":"y", Code, [decompress(true)]).
Code = string_concat("x","y",A).
starlog_to_prolog_file(+FilePath)
Convert a Starlog file to Prolog and output to stdout.
Parameters:
starlog_to_prolog_file(+FilePath, +OutputStream)
Convert a Starlog file to Prolog and write to a stream.
Parameters:
starlog_to_prolog_file(+FilePath, +OutputStream, +Options)
Convert a Starlog file to Prolog with options.
Parameters:
decompress(true/false), default is true)The decompression algorithm works as follows:
starlog_expand.pl to expand Starlog expressions into Prolog goalsappend([1,2],[3,4],Temp), reverse(Temp,A)A is "hello":" ":"world" expands to two goals: string_concat("hello"," ",Temp1), string_concat(Temp1,"world",A)The converter handles the following Starlog operators:
| Starlog Operator | Prolog Predicate | Description |
|---|---|---|
: | string_concat/3 | String concatenation |
& | append/3 | List append |
• | atom_concat/3 | Atom concatenation |
Additionally, it converts value-returning builtins:
% Starlog: A is reverse([1,2,3])
% Prolog: reverse([1,2,3],A)
% Starlog: A is string_length("hello")
% Prolog: string_length("hello",A)
The converter uses human-friendly variable names following this pattern:
% Starlog input
greet(Name, Greeting) :-
Greeting is "Hello, ":Name.
% Prolog output
greet(A,B):-string_concat("Hello, ",A,B).
% Starlog input
combine_and_reverse(A, B, Result) :-
Result is reverse(A&B).
% Prolog output (decompressed)
combine_and_reverse(A,B,C):-append(A,B,D),reverse(D,C).
% Starlog input
process_text(First, Last, Full) :-
Full is First:" ":Last.
% Prolog output (decompressed)
process_text(A,B,C):-string_concat(A," ",D),string_concat(D,B,C).
% Starlog input
complex_concat(A, B, C, Result) :-
Result is (A:B):C.
% Prolog output (decompressed)
complex_concat(A,B,C,D):-string_concat(A,B,E),string_concat(E,C,D).
The library supports bidirectional conversion:
% Start with Prolog
Prolog = (string_concat("hello"," ",T1), string_concat(T1,"world",T2))
% Convert to compressed Starlog
?- starlog_output_code(Prolog, Starlog, [compress(true)]).
Starlog = (A is "hello":" ":"world")
% Convert back to decompressed Prolog
?- starlog_to_prolog_code(Starlog, BackToProlog).
BackToProlog = (string_concat("hello"," ",A), string_concat(A,"world",B))
Run the test suite to verify the conversion:
cd tests swipl -s test_starlog_to_prolog.pl swipl -s test_starlog_to_prolog_file.pl
Run the comprehensive demo:
swipl -s demo_starlog_to_prolog.pl
This will show various examples of Starlog to Prolog conversion with maximal decompression.
The conversion is implemented in starlog.pl using: