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

Nested Concatenation in Dual Expressions - Implementation Summary

Problem Statement

Complete all combinations and configurations of:

starlog_call(Result is find([A,C], starlog_call([A:a:C] is [a:a:c]))).

Solution

Enhanced the Starlog library to support nested concatenation in dual expressions by improving the concat_dual predicate in starlog_expand.pl.

Implementation Details

Core Change

Added two new cases to concat_dual (lines 642-656 in starlog_expand.pl):

  1. Nested concatenation on the right side: When both B and D are concat expressions
    • Example: (A:(a:C)) is (b:(a:c))
    • Recursively solves (a:C) is (a:c) to constrain C
    • Then unifies A = b
  2. Nested concatenation on the left side: When both A and C are concat expressions
    • Example: ((a:A):b) is ((a:C):b)
    • Recursively solves (a:A) is (a:C) to constrain A and C
    • Then unifies B = D

How It Works

The solution leverages the existing solve_nested_concat_dual predicate which recursively handles nested patterns. The key insight is that when we have:

  • (A:a:C) is (b:a:c)
  • This is parsed as (A:(a:C)) is (b:(a:c))
  • We detect that both (a:C) and (a:c) are concat expressions
  • We recursively solve them to find C
  • Then solve for A

Test Coverage

New Tests (tests/test_find_nested_concat.pl)

  1. Basic three-way: [A:a:C] is [a:a:c] → Result = [a,c]
  2. Different values: [A:x:C] is [y:x:z] → Result = [y,z]
  3. Four-way: [A:b:C:d] is [a:b:c:d] → Result = [a,c]
  4. Variables at end: [a:B:C] is [a:b:c] → Result = [b,c]
  5. Direct concat: (A:a:C) is (b:a:c) → A=b, C=c
  6. Atom concat: `[A•x•C] is [y•x•z]` → Result = [y,z]
  7. Reverse pattern: [a:b:c] is [A:b:C] → A=a, C=c
  8. Five-way: [A:b:C:d:E] is [a:b:c:d:e] → Result = [a,c,e]
  9. String values: [A:"_":C] is ["hello":"_":"world"] → Result = ["hello","world"] 10. Nested atom: `[A•m•C] is [x•m•y]` → Result = [x,y]

    All 10 tests pass ✓

Existing Tests

All 10 existing tests in tests/test_find.pl continue to pass ✓

Documentation

Demo (demo_find_nested_concat.pl)

Created comprehensive demo showing:

  • The exact problem statement solution
  • Various configurations (3-way, 4-way, 5-way concatenation)
  • Both string (:) and atom (•) operators
  • Direct use without find
  • Reverse patterns

README Updates

Enhanced documentation in two sections:

  1. Dual Starlog Expressions: Added nested concatenation examples
  2. find/2 Helper Predicate: Added section on solving for multiple variables

Supported Patterns

✓ [A:a:C] is [a:a:c] - Multiple variables with fixed middle values ✓ [A:b:C:d] is [a:b:c:d] - Four-way concatenation ✓ [A:"-":C:"-":E] is ["x":"-":"y":"-":"z"] - Five-way with separators ✓ `[A•x•C] is [y•x•z]` - Atom concatenation ✓ (A:a:C) is (b:a:c) - Direct (not in list) ✓ [a:b:c] is [A:b:C] - Variables on right side

Limitations

✗ [A:B:c] is [a:b:c] - Two consecutive variables (instantiation error) ✗ [A:B:C] is [a:b:c] - All variables (instantiation error)

These fail because there's insufficient information to determine how to split the concatenated result. At least one bound value is needed in each concat operation to constrain the solution.

Files Changed

  1. starlog_expand.pl - Enhanced concat_dual predicate (26 lines added)
  2. tests/test_find_nested_concat.pl - New test suite (125 lines)
  3. demo_find_nested_concat.pl - New demo (116 lines)
  4. README.md - Documentation updates (34 lines) Total: ~300 lines of new code and documentation

Impact

  • Backward compatible: All existing tests pass
  • Enables solving for multiple variables in concatenation patterns
  • Supports arbitrary nesting levels (tested up to 5-way)
  • Works with both string and atom concatenation operators
  • Provides clear examples and comprehensive test coverage
  • Updated to use find/2 Starlog syntax (Result is find(Template, Goal))