| Did you know ... | Search Documentation: |
| Pack starlog -- NESTED_FUNCTIONS_IMPLEMENTATION.md |
This implementation adds complete support for nested value-returning functions (f(A, b, ...)) on both the left-hand side (LHS) and right-hand side (RHS) of the is operator in Starlog, with all combinations and configurations.
"If not yet done, add support for nested f(A,b …) on LHS and RHS of ... is ... with all combinations and configurations."
X is reverse(reverse([1,2,3]))reverse(reverse([1,2,3])) is XX is reverse(reverse([1,2,3]))reverse(reverse([1,2,3])) is Xreverse(A) is reverse([1,2,3])reverse([1,2])&reverse([3,4])) is X`starlog_expand.plAdded a new clause to handle Starlog expressions on the LHS when RHS is not a Starlog expression:
% Starlog is-expression with LHS being a Starlog expression: Expr is Out
% This handles cases like reverse(reverse([1,2,3])) is X
expand_goal_internal((LHS is RHS), Expanded) :-
is_starlog_expr(LHS),
\+ is_starlog_expr(RHS),
!,
compile_starlog_expr(LHS, LHSResult, LHSGoals),
append(LHSGoals, [LHSResult = RHS], FinalGoals),
list_to_conjunction(FinalGoals, Expanded).
This clause:
The new clause is positioned strategically within the clause ordering:
!) prevents backtracking to less specific clauses and ensures deterministic behavior.% Nested on RHS ?- starlog_call(X is reverse(reverse([1,2,3]))). X = [1,2,3]. % Nested on LHS (NEW!) ?- starlog_call(reverse(reverse([1,2,3])) is X). X = [1,2,3]. % Nested on both sides (NEW!) ?- starlog_call(reverse(A) is reverse([1,2,3])). A = [1,2,3].
% Triple nesting ?- starlog_call(reverse(reverse(reverse([1,2,3]))) is X). X = [3,2,1]. % Different functions ?- starlog_call(length(reverse([1,2,3])) is X). X = 3.
% Function of concat expression
?- starlog_call(reverse([1,2]&[3,4]) is X).
X = [4,3,2,1].
% Concat of function results
?- starlog_call((reverse([1,2])&reverse([3,4])) is X).
X = [2,1,4,3].
% String concat with functions
?- starlog_call((string_length("ab"):string_length("cd")) is X).
X = "22".
% Multiple levels of nesting with concat ?- starlog_call(reverse(reverse([1]&[2])&reverse([3]&[4])) is X). X = [3,4,1,2]. % With variables ?- starlog_call(reverse(reverse(A)&[5]) is reverse([1,2,3,4]&[5])). A = [4,3,2,1].
Created comprehensive test suite in tests/test_nested_functions.pl with 28 tests covering:
reverse(reverse([1,2,3]))reverse(reverse(reverse([1,2,3])))length(reverse([1,2,3]))reverse(reverse([1,2,3])) is Xreverse(reverse(reverse([1,2,3]))) is Xlength(reverse([1,2,3])) is Xreverse(A) is reverse([1,2,3])reverse(reverse(A)) is reverse([1,2,3])reverse(A) is ([1,2]&[3])`reverse([1,2])&reverse([3,4])`reverse([1,2])&reverse([3,4])) is X`string_length("ab"):string_length("cd")atom_string(ab):atom_string(cd)reverse(A)&[5]) is reverse([1,2,3,4]&[5])`reverse([]) is XX is reverse([])reverse(reverse([])) is X
Result: All 28 tests pass ✓All existing test suites continue to pass:
test_basic.pl (6 tests)test_nested.pl (5 tests)test_dual_expr_is.pl (8 tests)test_arithmetic_is.pl (5 tests)test_comprehensive_combinations.pl (24 tests, 19 pass, 5 pre-existing failures unrelated to this change)is, matching natural mathematical notation:, •, &)This implementation builds on and integrates with:
starlog_registry.pl):, •, &)Potential future work could include:
This implementation completes the requirement to support nested f(A,b…) on both LHS and RHS of is with all combinations and configurations. The feature is fully tested, backward compatible, and integrates seamlessly with existing Starlog functionality.