Did you know ... Search Documentation:
Pack starlog -- ARITHMETIC_IN_BUILTINS_SUMMARY.md

Implementation Summary: Arithmetic in Builtin Arguments

Problem Statement

Complete support for: A is number_string(2^2):2

Solution

Added automatic evaluation of arithmetic expressions when used as arguments to builtin functions in Starlog.

Changes Made

1. Modified starlog_expand.pl

Added a new clause to compile_value/3 (lines 390-392):

% Arithmetic expressions should be evaluated when used as builtin arguments
compile_value(Expr, Value, [Value is Expr]) :-
    is_arithmetic(Expr),
    !.

This ensures that when an arithmetic expression (like 2^2, 10+5, etc.) is used as an argument to a builtin function, it gets evaluated first before being passed to the function.

2. Added Tests (tests/test_arithmetic_in_builtins.pl)

Comprehensive test suite covering:

  • Basic arithmetic in number_string: number_string(2^2) → "4"
  • String concatenation with arithmetic: number_string(2^2) : 2 → "42"
  • Complex arithmetic expressions: number_string(3*4+5) → "17"
  • Nested builtins: string_length(number_string(10+5)) → 2
  • Multiple concatenations: number_string(1+1) : number_string(2*2) → "24"
  • Various operators: ^, *, /, +, -
  • Backward compatibility verification

3. Added Demo (demo_arithmetic_in_builtins.pl)

Demonstration file showcasing:

  • Problem statement example
  • Basic arithmetic operations
  • Nested operations
  • Complex concatenation
  • Practical use cases

How It Works

Before

?- starlog_call(A is number_string(2^2):2).
ERROR: number_string/2: Type error: `number' expected, found `2^2' (a compound)

After

?- starlog_call(A is number_string(2^2):2).
A = "42".

The expression is evaluated step by step:

  1. 2^2 is evaluated to 4 (arithmetic evaluation)
  2. number_string(4) converts to "4" (builtin call)
  3. "4" : 2 concatenates to "42" (string concatenation with auto-conversion)

Design Rationale

The implementation distinguishes between two use cases:

  1. Arithmetic as main expression: A is 2^2
    • Handled by compile_starlog_expr/3 at lines 367-369
    • Keeps arithmetic evaluation as standard Prolog is/2
  2. Arithmetic as builtin argument: A is number_string(2^2)
    • Handled by new compile_value/3 clause at lines 390-392
    • Evaluates arithmetic before passing to builtin

    This design:

  • Maintains backward compatibility
  • Provides intuitive behavior (arithmetic is automatically evaluated)
  • Works seamlessly with all builtin functions
  • Supports complex nested expressions

Testing

All tests pass:

  • ✓ New tests: test_arithmetic_in_builtins.pl (10 tests)
  • ✓ Existing tests: test_basic.pl, test_nested.pl, test_arithmetic_is.pl, etc.
  • ✓ Related tests: test_number_string_number.pl
  • ✓ No regressions detected

Examples

% Basic
?- starlog_call(A is number_string(2^2)).
A = "4".

% With concatenation
?- starlog_call(A is number_string(2^2):2).
A = "42".

% Complex arithmetic
?- starlog_call(A is number_string((2+3)*(4+1))).
A = "25".

% Multiple concatenations
?- starlog_call(A is number_string(1+1):"-":number_string(2*2)).
A = "2-4".

% Nested builtins
?- starlog_call(A is string_length(number_string(10^2))).
A = 3.

Compatibility

  • ✓ Backward compatible with all existing code
  • ✓ Works with all arithmetic operators: +, -, *, /, //, mod, **, ^
  • ✓ Works with all builtin functions that accept numeric arguments
  • ✓ Preserves existing behavior for non-arithmetic expressions
  • ✓ No performance impact on non-arithmetic code paths

Security

No security issues detected by CodeQL analysis.