| Did you know ... | Search Documentation: |
| Pack starlog -- VARIABLE_EXPRESSION_EVAL.md |
"Complete A=([a]&[c]),B is A. B=[a,c] with all combinations and configurations, please."
This implementation enables automatic evaluation of Starlog expressions bound to variables when used with the is operator. The pattern:
A = (StarlogExpr), B is A
is automatically transformed to:
A = (StarlogExpr), starlog_eval(A, B)
This allows for more natural and intuitive code where Starlog expressions can be stored in variables and evaluated later.
% List append
A = ([a] & [c]),
B is A.
% B = [a,c]
% String concatenation
X = ("hello" : "world"),
Y is X.
% Y = "helloworld"
% Atom concatenation
P = (foo • bar),
Q is P.
% Q = foobar
% Nested expressions
L = (([1] & [2]) & [3]),
M is L.
% M = [1,2,3]
% Sequential operations
A = ([1] & [2]),
B is A,
C = (B & [3]),
D is C.
% B = [1,2], D = [1,2,3]
% Multiple bindings
X = ([a] & [b]),
Y is X,
Z = ("p" : "q"),
W is Z.
% Y = [a,b], W = "pq"
All three main Starlog operators are supported:
&): `A = ([1] & [2]), B is A → B = [1,2]`:): A = ("x" : "y"), B is A → B = "xy"•): `A = (a • b), B is A → B = ab`Added a new pattern detection clause in expand_goal_internal/2 (lines 73-95) that:
Var1 = (StarlogExpr), (Var2 is Var1, ...)starlog_eval(Var1, Var2)A comprehensive demonstration file with 12 examples showing:
A complete test suite with 25 tests covering:
Var1 = (StarlogExpr), (Var2 is Var1, ...):, &, •)(X is Expr))Var2 is Var1 references the same variableVar1 = (StarlogExpr), starlog_eval(Var1, Var2)starlog_eval evaluates the expression and binds the result to Var2This feature complements the existing variable-bound goal pattern:
A = (C is Expr), A → automatically executes the goalA = (Expr), B is A → automatically evaluates the expression
Both patterns make Starlog expressions more flexible as first-class values that can be stored and evaluated later.starlog_call or starlog_evalAll 25 tests pass successfully:
cd tests swipl -g "run_tests, halt" -s test_variable_expression_evaluation.pl
Output:
% PL-Unit: variable_expression_evaluation ......................... passed % All 25 tests passed
This implementation completes the requirement to support `A = ([a] & [c]), B is A` with the result B = [a,c], and extends it to work with all Starlog operator combinations and configurations. The feature integrates seamlessly with existing Starlog functionality and provides a more natural way to work with Starlog expressions as values.