Did you know ... Search Documentation:
Pack starlog -- PRETTY_PRINTING_VERIFICATION.md

Pretty Printing Verification Report

Summary

The Starlog-in-Prolog library already has comprehensive pretty printing functionality for both Prolog and Starlog code. This report documents the existing implementation and verifies that it works correctly.

Pretty Printing Features

1. Starlog Code Output (starlog_output_code, starlog_output_file)

  • ✅ Proper indentation for nested structures
  • ✅ Formatted output for findall with nested goals
  • ✅ Proper formatting for if-then-else structures
  • ✅ Disjunction and conjunction formatting
  • ✅ Negation formatting
  • ✅ Human-friendly variable names (A, B, C, etc.)
  • ✅ Optional compression for nested expressions

2. Prolog Code Output (starlog_to_prolog_code, starlog_to_prolog_file)

  • ✅ Proper indentation for clause bodies
  • ✅ Sequential goal formatting with proper line breaks
  • ✅ Human-friendly variable names (A, B, C, etc.)
  • ✅ Decompression of nested Starlog expressions
  • ✅ Proper formatting for complex structures

3. Key Implementation Details

Indentation System

  • Uses write_indent/2 to write consistent 2-space indentation
  • Tracks indent level through recursive calls
  • Increases indent for nested structures (findall, if-then-else, etc.)

Pretty Printing Functions

  1. pretty_write_body/3 - Formats clause bodies with proper indentation
  2. pretty_write_goal/3 - Formats individual goals
  3. pretty_write_term_at_level/3 - Formats complete terms (clauses, facts)

Special Handling

  • If-then-else: Multi-line format with indented branches
  • Findall: Nested format with template, goal, and result on separate lines
  • Conjunctions: Each goal on its own line with proper indentation
  • Disjunctions: Bracketed format with indented alternatives

Test Results

All tests pass successfully:

Test 1: Starlog Output

?- starlog_output_code(findall(X, (member(X,[1,2,3]), X > 1), R)).
A is
  findall(
    B,
    (
      member(B,[1,2,3]),
      B>1
    )
  )

Test 2: Prolog Output

?- starlog_to_prolog_code(A is "a":" ":"b").
string_concat(" ","b",A),
string_concat("a",A,B)

Test 3: File Output

Both starlog_output_file/1-3 and starlog_to_prolog_file/1-3 produce properly formatted output with:

  • Header comments indicating the source file
  • Properly indented clause bodies
  • Human-friendly variable names
  • Clean, readable structure

Conclusion

The pretty printing functionality is fully implemented and working correctly. The implementation provides:

  1. ✅ Pretty printed Prolog code calls
  2. ✅ Pretty printed Prolog files
  3. ✅ Pretty printed Starlog code calls
  4. ✅ Pretty printed Starlog files
  5. ✅ Proper indentation for all control structures
  6. ✅ Human-friendly variable naming
  7. ✅ Optional compression/decompression All requirements for "pretty print Prolog code calls and files" are satisfied by the existing implementation.