Implementation Summary: Univ Operator Support
Overview
This implementation adds support for Prolog's univ operator (=..) in Starlog notation, allowing for bidirectional conversion between terms and lists.
Problem Statement
Implement two new Starlog operators:
A is ..=([f,0,1]) - Convert list to term (creates f(0,1))
A is =..(f(0,1)) - Convert term to list (creates [f,0,1])
Implementation Details
Files Modified
- starlog_expand.pl
- Added operator declarations:
..= and =.. as prefix operators with precedence 600
- Updated is_starlog_expr/1 to recognize the new operators
- Updated contains_starlog_operator/1 to include the new operators
- Added compilation rules:
compile_starlog_expr(..=(List), Out, Goals) - expands to Out =.. ListVal
compile_starlog_expr(=..(Term), Out, Goals) - expands to TermVal =.. Out
- starlog.pl
- Added operator declarations in the user module
- Updated contains_starlog_op/1 to recognize the new operators
- Added conversion rules for Prolog to Starlog:
(Term =.. Out) converts to (Out is =..(Term))
(Out =.. List) converts to (Out is ..=(List))
- README.md
- Added documentation for the new operators in the "Special Operators" section
- Added Example 6 demonstrating term manipulation with univ operators
- Includes usage examples for list-to-term, term-to-list, and roundtrip conversions
Files Created
- tests/test_univ.pl
- Comprehensive test suite for univ operators
- Tests include:
- List to term conversion
- Term to list conversion
- Different functors
- Nullary functors
- Roundtrip conversions
- Usage in clause bodies
- All tests pass ✓
- tests/test_univ_conversion.pl
- Tests for bidirectional conversion between Prolog and Starlog
- Verifies that Prolog
=.. expressions convert correctly to Starlog notation
- Verifies that Starlog univ expressions convert correctly to Prolog
- All tests pass ✓
- demo_univ.pl
- Interactive demonstration script
- Shows 5 examples of univ operator usage
- Demonstrates practical applications like term construction and deconstruction
Technical Details
Operator Definitions
:- op(600, fx, '..='). % List to term conversion (prefix)
:- op(600, fx, '=..'). % Term to list conversion (prefix)
Expansion Examples
List to Term:
% Starlog input:
A is ..=([f,0,1])
% Expands to:
A =.. [f,0,1]
% Result:
A = f(0,1)
Term to List:
% Starlog input:
A is =..(f(0,1))
% Expands to:
f(0,1) =.. A
% Result:
A = [f,0,1]
Compatibility
- Works seamlessly with existing Starlog operators (
:, &, •)
- Compatible with
no_eval and eval functions
- Preserves arithmetic expressions
- Works in both interactive queries and clause bodies
- Supports nested expressions
Testing Results
All existing tests continue to pass:
Usage Examples
Basic Usage
?- use_module(starlog).
% Create a term from a list
?- starlog_call((T is ..=([foo,a,b,c]))).
T = foo(a,b,c).
% Convert a term to a list
?- starlog_call((L is =..(bar(x,y,z)))).
L = [bar,x,y,z].
% Roundtrip conversion
?- starlog_call((T is ..=([baz,1,2]), L is =..(T))).
T = baz(1,2),
L = [baz,1,2].
In Clause Bodies
create_term(List, Term) :-
Term is ..=(List).
deconstruct_term(Term, List) :-
List is =..(Term).
Benefits
- Metaprogramming: Enables dynamic term construction and deconstruction
- Code Generation: Useful for generating Prolog terms programmatically
- Term Inspection: Easy examination of term structure
- Consistency: Maintains Starlog's functional notation style
- Bidirectional: Full support for Prolog ↔ Starlog conversion
Backward Compatibility
This implementation is fully backward compatible. All existing Starlog code continues to work without modification. The new operators are additive and do not conflict with existing functionality.