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

Implementation Summary: All Combinations and Configurations

Problem Statement

Implement all combinations and configurations of: A is (1:1 >> string_number) * (+(1,1))

Solution

Successfully implemented comprehensive support for combining:

  1. String concatenation using the : operator
  2. Method chaining using the >> operator
  3. Arithmetic operations in both infix and function notation The key challenge was handling arithmetic expressions that contain Starlog sub-expressions, which required enhancing the expansion mechanism to detect and pre-evaluate Starlog parts before performing arithmetic.

Implementation Changes

Modified starlog_expand.pl

1. Added Detection for Starlog in Arithmetic (line 436-444)

% contains_starlog_in_arithmetic(+Expr)
% Check if an arithmetic expression contains Starlog sub-expressions
contains_starlog_in_arithmetic(Expr) :-
    compound(Expr),
    functor(Expr, _Op, Arity),
    Arity > 0,
    Expr =.. [_|Args],
    member(Arg, Args),
    (is_starlog_expr(Arg) ; contains_starlog_in_arithmetic(Arg)),
    !.

2. Added Compilation for Arithmetic with Starlog (line 446-472)

% compile_arithmetic_with_starlog(+ArithExpr, -CompiledArithExpr, -PreGoals)
% Compile an arithmetic expression that contains Starlog sub-expressions
% Pre-evaluates the Starlog parts and returns a pure arithmetic expression

This recursively processes arithmetic expressions, detecting Starlog sub-expressions and compiling them first.

3. Enhanced Goal Expansion (line 216-232)

% Arithmetic expression with Starlog sub-expressions: Out is ArithExpr
% Handle cases like: A is (1:1 >> string_number) * 2
expand_goal_internal((Out is Expr), Expanded) :-
    is_arithmetic(Expr),
    contains_starlog_in_arithmetic(Expr),
    !,
    compile_starlog_expr(Expr, Out, Goals),
    list_to_conjunction(Goals, Expanded).

This ensures arithmetic expressions with Starlog parts are properly expanded.

4. Updated Arithmetic Expression Handling (line 920-932)

% Arithmetic expression - check if it contains Starlog sub-expressions
compile_starlog_expr(Expr, Out, Goals) :-
    is_arithmetic(Expr),
    !,
    (contains_starlog_in_arithmetic(Expr) ->
        compile_arithmetic_with_starlog(Expr, ArithExpr, PreGoals),
        append(PreGoals, [Out is ArithExpr], Goals)
    ;
        Goals = [Out is Expr]
    ).

How It Works

Example: A is (1:1 >> string_number) * (+(1,1))

  1. Detection: The expression is identified as arithmetic (* operator)
  2. Sub-expression Analysis: Detects (1:1 >> string_number) as a Starlog sub-expression
  3. Pre-compilation:
    • 1:1 → string_concat(1, 1, _T1) → _T1 = "11"
    • "11" >> string_number → string_number("11", _T2) → _T2 = 11
  4. Arithmetic Compilation: +(1,1) → 2
  5. Final Arithmetic: _T2 * 2 → 11 * 2 → 22 The expansion produces:
    string_concat(1, 1, _T1),
    string_number(_T1, _T2),
    A is _T2 * 2

Test Coverage

Created comprehensive test suite tests/test_all_combinations_configurations.pl with 29 tests covering:

1. Core Pattern (6 tests)

  • Exact problem statement pattern
  • Intermediate step verification

2. Number Variations (4 tests)

  • Different number combinations
  • Multi-digit numbers

3. Arithmetic Operators (7 tests)

  • Addition: +
  • Subtraction: -
  • Multiplication: *
  • Division: /
  • Integer Division: //
  • Modulo: mod
  • Power: ** and ^

4. Method Chain Variations (3 tests)

  • Multiple method chains
  • Different method operations

5. Nested Combinations (3 tests)

  • Nested string concatenation
  • Nested arithmetic
  • Complex multi-level nesting

6. Mixed Operators (2 tests)

  • Multiple different operators
  • Atom concatenation variant

7. Edge Cases (4 tests)

  • Zero values
  • Negative numbers
  • Roundtrip conversions

Test Results

========================================
Test Summary
========================================
Total:  29
Passed: 29
Failed: 0
✓ All tests passed!

Usage Examples

Basic Usage

?- use_module(starlog).
?- starlog_call(A is (1:1 >> string_number) * (+(1,1))).
A = 22.

Different Numbers

?- starlog_call(A is (2:3 >> string_number) * (+(1,1))).
A = 46.  % "23" -> 23 * 2

Different Operators

?- starlog_call(A is (1:1 >> string_number) + (+(1,1))).
A = 13.  % 11 + 2

?- starlog_call(A is (2:0 >> string_number) ** (+(1,1))).
A = 400.  % 20 ** 2

Nested Combinations

?- starlog_call(A is ((1:1) : (2:2) >> string_number) * 2).
A = 2244.  % "1122" -> 1122 * 2

?- starlog_call(A is (1:1 >> string_number) * (+(1,1) + +(1,1))).
A = 44.  % 11 * (2 + 2)

Files Created/Modified

Created:

  1. tests/test_all_combinations_configurations.pl - Comprehensive test suite
  2. demo_all_combinations_configurations.pl - Interactive demonstration
  3. ALL_COMBINATIONS_CONFIGURATIONS.md - User documentation
  4. IMPLEMENTATION_ALL_COMBINATIONS.md - This file

Modified:

  1. starlog_expand.pl - Added arithmetic with Starlog sub-expression support

Benefits

This implementation enables:

  1. Natural arithmetic with Starlog expressions as operands
  2. Method chaining within arithmetic contexts
  3. Complex nesting of Starlog and arithmetic operations
  4. All operator combinations working seamlessly together
  5. Backwards compatibility - all existing code continues to work

Compatibility

✓ Fully compatible with all existing Starlog features ✓ All existing tests continue to pass ✓ No breaking changes ✓ Works with all arithmetic operators ✓ Works with all Starlog operators ✓ Supports arbitrary nesting depth

Conclusion

The pattern A is (1:1 >> string_number) * (+(1,1)) and all its combinations and configurations are now fully supported and tested. The implementation:

  • ✅ Handles string concatenation in arithmetic
  • ✅ Handles method chaining in arithmetic
  • ✅ Handles all arithmetic operators
  • ✅ Handles nested combinations
  • ✅ Handles edge cases
  • ✅ Has comprehensive test coverage (29/29 passing)
  • ✅ Maintains backwards compatibility
  • ✅ Is well-documented The feature is complete and ready for use.