| Did you know ... | Search Documentation: |
| Pack starlog -- IMPLEMENTATION_SUMMARY_DUAL_EXPR.md |
The problem statement requested completion of patterns like:
[1]&A is B&[2] - Dual list append expressions3 is 1+(A is 1;2;3) - Value selection with disjunctionA is (B is 1)+B - Nested is expressions
The main issue was that pattern #1 ([1]&A is B&[2]) caused an infinite loop because the Starlog library didn't handle cases where both the left-hand side (LHS) and right-hand side (RHS) of is/2 were Starlog expressions.
The existing code in starlog_expand.pl only handled the pattern Out is Expr where:
([1] & A) is (B & [2])), the code would attempt to compile the LHS as if it were an output variable, leading to infinite recursion.
Added a new pattern matcher in starlog_expand.pl that:
File: starlog_expand.pl (lines 103-113)
% Starlog is-expression with both sides being expressions: (Expr1 is Expr2)
% This handles cases like ([1] & A) is (B & [2])
expand_goal_internal((LHS is RHS), Expanded) :-
is_starlog_expr(LHS),
is_starlog_expr(RHS),
!,
% Compile both sides and unify the results
compile_starlog_expr(LHS, LHSResult, LHSGoals),
compile_starlog_expr(RHS, RHSResult, RHSGoals),
append(LHSGoals, [LHSResult = RHSResult|RHSGoals], FinalGoals),
list_to_conjunction(FinalGoals, Expanded).
This clause is placed before the existing Out is Expr pattern to ensure dual expressions are caught first.
Comprehensive tests were added to verify the implementation:
All tests pass:
?- ([1] & A) is (B & [2]). A = [2], B = [1].
?- (([1] & [2]) & A) is (B & [3]). A = [3], B = [1, 2].
?- ("hello" : "world") is ("hello" : "world").
true.
?- member(Op, [+, -, *, /]), Expr =.. [Op, 1, 2], 3 is Expr. Op = (+).
?- member(A, [1, 2, 3]), 3 is 1+A. A = 2.
?- B is 1, A is B+B. B = 1, A = 2.
The README.md was updated with:
This change is minimal and surgical:
starlog_expand.plThe implementation has expected limitations:
("x" : "y") is (Z : "world") requires constraint solving)