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

List Dual Expressions Implementation

Problem Statement

Complete `[A:...a] is [b:...B] and [A•a:c] is [b•B:c]` with any combination of :, •, [], arithmetic expression or &, and any configuration.

What Was Implemented

This implementation adds support for list dual expressions without the append operator (&). This enables powerful bidirectional constraint solving for patterns where both sides of is are lists containing concatenation expressions.

Key Features

1. Direct List-to-List Dual Expressions

Unlike existing patterns that use the append operator &, this feature works with direct list comparisons:

?- [A:a] is [b:B].
A = b, B = a.

2. Mixed Operators Within Elements

Elements can contain nested concatenation expressions mixing different operators:

?- [(A•a):c] is [(b•B):c].
A = b, B = a.

3. Multiple Elements

Lists can have multiple elements, each with their own concat expressions:

?- [A•q, x] is [p•q, x].
A = p.

?- [A•a, B•b] is [p•a, r•b].
A = p, B = r.

4. Longer Concatenation Chains

Nested concatenations are fully supported:

?- [A:x:y] is [p:x:y].
A = p.

?- [((A•a):b):c] is [((p•a):b):c].
A = p.

Supported Patterns

Basic String Concatenation

[A:a] is [b:B]          % Basic dual
[a:A] is [B:b]          % Reversed
[A:x] is [p:x]          % Matching suffix
[x:A] is [x:p]          % Matching prefix

Basic Atom Concatenation

[A•a] is [b•B]          % Basic dual
[a•A] is [B•b]          % Reversed
[A•x] is [p•x]          % Matching suffix
[x•A] is [x•p]          % Matching prefix

Mixed Operators

[(A•a):c] is [(b•B):c]          % Atom then string
[(A:a)•c] is [(b:B)•c]          % String then atom
[((A•a):b):c] is [((p•a):b):c]  % Deeply nested

Multiple Elements

[A•q, x] is [p•q, x]            % One concat, one literal
[A•a, B•b] is [p•a, r•b]        % Multiple concats
[A•a, x, y] is [p•a, x, y]      % Concat with literals

Longer Chains

[A:x:y] is [p:x:y]              % String chain
[A•x•y] is [p•x•y]              % Atom chain

Implementation Details

Detection

The feature adds a new detection predicate is_list_dual_expr_with_concat/2 that checks:

  1. Both LHS and RHS are lists (not with &)
  2. Both lists contain concat operations (: or •)

Solver Algorithm

The solver solve_list_dual_expr/3 works as follows:

  1. Process Lists: Extract concat constraints from both lists
    • For each concat expression, create a result variable
    • For each non-concat element, keep as-is
  2. Create Bidirectional Constraints: Pair up constraints from LHS and RHS
    • Position-wise pairing: first with first, second with second, etc.
    • Use bidirectional solvers (string_concat_dual or atom_concat_dual)
  3. Execute Goals: Run bidirectional constraints, then unify processed lists
    • Bidirectional constraints execute first to bind variables
    • List unification happens after to verify structure

Bidirectional Constraint Solving

The existing concat_dual/5 predicate handles various patterns:

  • Same suffix: (A + b) = (p + b) → A = p
  • Same prefix: (a + B) = (a + q) → B = q
  • All bound: Verify equality
  • One variable: Solve for the unknown A new case was added to handle when only the first operand is variable:
    (var(A), nonvar(B), nonvar(C), nonvar(D)) ->
        (call(ConcatPred, C, D, Result),
         atom_concat(A, B, Result))

    This enables solving patterns like (A + a) = (p + q) by computing p + q = pq and then solving A + a = pq.

Use Cases

1. Template Matching

Extract components from strings/atoms with known patterns:

extract_prefix(Input, Prefix) :-
    [Prefix:"_suffix"] is [Input].

2. Dual Constraints

Solve multiple concatenation equations simultaneously:

?- [A:"_x", B:"_y"] is ["prefix_x", "other_y"].
A = "prefix",
B = "other".

3. Pattern Verification

Verify that complex nested expressions match:

?- [((a•b):c)] is [((a•b):c)].
true.

4. Mixed Operator Parsing

Combine atom and string operations in a single expression:

?- [(Name•"@"):Domain] is [(user•"@"):example_com].
Name = user,
Domain = example_com.

Test Coverage

Comprehensive test suite with 23 tests covering:

  • Basic string and atom concatenation
  • Mixed operators within elements
  • Multiple elements in lists
  • Longer concatenation chains
  • Edge cases and compatibility All tests pass successfully.

Compatibility

This feature integrates seamlessly with existing Starlog functionality:

  • Simple concat dual expressions still work: (A:a) is (b:B)
  • List append dual expressions still work: `([A•b] & [d]) is [a•B, d]`
  • Standard list append still works: `([1] & [2]) is [1,2]`

Examples

See demo_list_dual_expressions.pl for comprehensive demonstrations of all patterns.

Benefits

  1. More Expressive: Write complex constraints as list patterns
  2. Bidirectional: Solves for unknowns on either side
  3. Composable: Mix different operators and nesting levels
  4. Pattern Matching: Elegant syntax for template matching
  5. Type Flexible: Works with both atoms and strings

Technical Notes

Constraint Pairing

Constraints are paired position-wise. If lists have different lengths or incompatible patterns, the system gracefully degrades to standard compilation.

Performance

The implementation maintains linear complexity with respect to:

  • Number of list elements
  • Number of concat operations
  • Nesting depth

Limitations

  • Suffixes/prefixes must match for solutions to exist
    • `[A•a] is [p•q]` has no solution if `a ≠ q`
    • Use matching suffixes: `[A•q] is [p•q] → A = p`
  • Both operands being variables requires at least one side to be bound
    • [x:A] is [x:B] needs one of A or B to be known

Future Enhancements

Potential extensions:

  1. Support for regex-like patterns
  2. Arithmetic expressions in lists
  3. More complex constraint propagation
  4. Integration with CLP(FD) for additional constraints

Conclusion

This implementation completes the requirement for list dual expressions, enabling powerful pattern matching and constraint solving capabilities while maintaining full backward compatibility with existing Starlog features.