Implementation Summary: eval/no_eval Output Control
Overview
This implementation adds options to control whether eval() and no_eval() wrappers are shown in the output when converting code to Starlog notation.
Problem Statement
The original requirement was:
> "Make the default not to output eval() and no_eval() but not necessarily the contents of (), and have options to output eval(), no_eval() or both."
Solution
Changes Made
- Modified starlog_output_code/3 - Added support for new options:
output_eval(true/false) - Controls whether eval() wrappers are shown
output_no_eval(true/false) - Controls whether no_eval() wrappers are shown
- Default behavior: Both are set to
false (strip both wrappers)
- Modified starlog_output_file/3 - Added the same options for file output
- Added new predicates:
- Updated documentation in README.md with:
- New section explaining the eval/no_eval output control
- Usage examples showing all option combinations
- Summary table of available options
- Added tests in
tests/test_output_eval_options.pl:
- Test default behavior (strips both)
- Test keeping eval only
- Test keeping no_eval only
- Test keeping both
- Test nested eval/no_eval combinations
- Test multiple expressions
- Test complex nested structures
- Test return value verification
- Added demo in
demo_output_eval_options.pl:
- Demonstrates all option combinations
- Shows comparison of different settings
- Provides clear examples for users
Default Behavior
By default (when no options are specified):
eval() wrappers are stripped from the output
no_eval() wrappers are stripped from the output
- The contents of these wrappers are preserved
This makes sense because:
- Evaluation is the default behavior in Starlog, so
eval() is redundant
- Most users want cleaner output without unnecessary wrappers
- The wrappers can be re-added if needed via options
Options
| Option | Effect |
output_eval(false) | Strip eval() wrappers (default) |
output_eval(true) | Keep eval() wrappers in output |
output_no_eval(false) | Strip no_eval() wrappers (default) |
output_no_eval(true) | Keep no_eval() wrappers in output |
Examples
% Default - strips both
?- starlog_output_code(A is no_eval(1+1)).
A is 1+1
% Keep no_eval
?- starlog_output_code(A is no_eval(1+1), _, [output_no_eval(true)]).
A is no_eval(1+1)
% Keep eval
?- starlog_output_code(B is eval("x":"y"), _, [output_eval(true)]).
B is eval("x":"y")
% Keep both
?- starlog_output_code(C is no_eval(eval(1+1)), _, [output_eval(true), output_no_eval(true)]).
C is no_eval(eval(1+1))
Implementation Details
Recursive Stripping Algorithm
The stripping algorithm recursively processes the entire term structure:
- Handles all control structures: conjunctions, disjunctions, if-then-else, negation
- Processes
is expressions: Strips from the right-hand side
- Strips wrappers conditionally: Based on the StripEval and StripNoEval flags
- Recursively processes nested structures: Handles compound terms and lists
- Preserves atomic terms: Numbers, atoms, strings are returned as-is
Integration Points
The stripping is applied at two points in the output process:
- After variable renaming: When outputting Starlog code that's already in Starlog form
- After conversion and compression: When converting from Prolog to Starlog form
This ensures that the stripping works correctly regardless of the input format.
Compatibility
- All existing functionality is preserved
- Existing code will see no difference (default behavior strips wrappers)
- Options are backward-compatible (ignored if not provided)
- Can be combined with existing options like
compress(true)
Testing
Comprehensive tests cover:
- Default behavior verification
- Each option individually
- Combined options
- Nested structures
- Return value verification
- Edge cases
Files Modified
starlog.pl - Core implementation
README.md - Documentation
tests/test_output_eval_options.pl - Tests (new)
demo_output_eval_options.pl - Demo (new)
Future Enhancements
Potential improvements for the future:
- Add option to strip only at top level
- Add option to strip based on depth
- Add custom filtering predicates for more control