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

Method Chain Syntax Implementation Summary

Problem Statement

Implement all combinations and configurations of nested predicate calls also being able to be called as method chains, e.g., d(a(b(1,C))) can be called b(1,C).a.d

Solution Implemented

Due to SWI-Prolog's built-in dictionary syntax using `.` for dict access, we implemented method chains using the >> operator instead. This provides the same functionality with a syntax commonly used in functional programming for piping/chaining operations.

Syntax

% Original nested calls
X is d(a(b(1,c)))

% Method chain syntax (implemented)
X is b(1,c) >> a >> d

% Both produce: X = d(a(b(1,c)))

Implementation Details

Files Modified

  1. starlog.pl
    • Added >> operator definition with precedence 650 (between is and other Starlog operators)
    • Operator: :- op(650, yfx, user:(>>))
  2. starlog_expand.pl
    • Added >> to operator definitions for pattern matching
    • Added is_starlog_expr clause to recognize method chains
    • Added contains_starlog_operator clause for >>
    • Added compile_starlog_expr clause to handle method chain compilation
    • Added helper predicates:
      • collect_method_chain/2: Collects methods from chain into a list
      • build_nested_method_calls/4: Builds sequential goals from method list
      • create_method_call/4: Creates individual method call goals
  3. README.md
    • Added new "Method Chain Syntax" section
    • Added examples to Quick Start
    • Documented all features and use cases

Features Implemented

  • ✅ Basic method chains: b(1,c) >> a >> d
  • ✅ Chains with built-in functions: reverse([1,2,3]) >> length
  • ✅ Chains with Starlog operators: `([1,2]&[3,4]) >> reverse`
  • ✅ Complex multi-step chains: sort([3,1,2]) >> reverse >> length
  • ✅ User-defined predicates: Works with any binary predicate
  • ✅ Full equivalence with nested calls
  • ✅ Backward compatibility: All existing tests pass

Examples

Basic Usage

% Simple chain
?- starlog_call(R is b(1,c) >> wrap_a).
R = a(b(1,c)).

% Multiple steps
?- starlog_call(R is b(1,c) >> wrap_a >> wrap_d).
R = d(a(b(1,c))).

With Built-ins

?- starlog_call(R is reverse([1,2,3]) >> length).
R = 3.

?- starlog_call(R is sort([3,1,2]) >> reverse).
R = [3,2,1].

With Starlog Operators

?- starlog_call(R is ([1,2]&[3,4]) >> reverse).
R = [4,3,2,1].

?- starlog_call(R is ("hello":"world") >> string_length).
R = 10.

Complex Combinations

?- starlog_call(R is ([1,2]&[3,4]) >> reverse >> length).
R = 4.

?- starlog_call(R is (reverse([1,2])&reverse([3,4])) >> flatten).
R = [2,1,4,3].

Testing

Tests Created

  1. test_method_chain_basic.pl - Basic functionality tests (7 tests, all passing)
  2. demo_method_chain_simple.pl - Interactive demonstration
  3. test_verify_chains.pl - Verification of core functionality

Test Results

  • ✅ All new tests pass
  • ✅ All existing tests pass (backward compatibility verified)
  • ✅ Problem statement requirement met: b(1,c) >> a >> d works correctly

Technical Notes

Why >> instead of `.`?

SWI-Prolog version 7+ uses `.` as a built-in operator for dictionary access (e.g., Dict.key). This syntax is deeply integrated into the parser and takes precedence over custom operator definitions. Using >>:

  • Avoids conflicts with dict syntax
  • Is a common notation for piping in functional programming
  • Provides clear, unambiguous semantics
  • Works reliably across all contexts

How It Works

  1. The >> operator is parsed as a right-associative binary operator
  2. a >> b >> c is parsed as a >> (b >> c)
  3. The expansion collects all methods into a list [a, b, c]
  4. Goals are built sequentially: apply a, then apply b to result, then apply c to that result
  5. This creates the same structure as nested calls: c(b(a(...)))

Integration with Existing Features

Method chains work seamlessly with:

  • All value-returning builtins registered in starlog_registry.pl
  • User-defined predicates following the value-returning pattern
  • All Starlog operators (:, &, •)
  • Nested expressions and complex combinations

Conclusion

The method chain syntax implementation successfully addresses the problem statement by providing a fluent, left-to-right chaining syntax that is equivalent to nested predicate calls. The use of >> instead of `.` provides a practical solution that works reliably within SWI-Prolog's constraints while maintaining full backward compatibility.