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

FOLDR Implementation Summary

Overview

This implementation adds support for the foldr (fold right) higher-order predicate to the Starlog-in-Prolog library, enabling functional-style list processing operations.

Problem Statement

Implement:

foldr(string_concat, reverse(string_chars("abc")), "", B). B= "cba"

Given all combinations and configurations.

Implementation Details

1. Core Implementation (starlog.pl)

Added the foldr/4 predicate to the user module:

  • Signature: foldr(Operation, List, Accumulator, Result)
  • Behavior: Folds a list from right to left using a binary operation
  • Supports: Both atom operation names and compound terms
  • Location: Lines 50-73 in starlog.pl

2. Registry Integration (starlog_registry.pl)

Registered foldr as a value builtin with arity 3:

  • This enables Starlog syntax: Result is foldr(operation, list, accumulator)
  • Added at line 83: default_value_builtin(foldr, 3).

3. How foldr Works

For a list [x1, x2, x3] with operation f and accumulator acc:

foldr(f, [x1, x2, x3], acc) = f(x1, f(x2, f(x3, acc)))

Example with the problem statement:

string_chars("abc")           → [a, b, c]
reverse([a, b, c])            → [c, b, a]
foldr(string_concat, [c,b,a], "", Result)
  = string_concat(c, string_concat(b, string_concat(a, "")))
  = string_concat(c, string_concat(b, "a"))
  = string_concat(c, "ba")
  = "cba"  ✓

Supported Configurations

The implementation has been tested with all the following configurations:

Configuration 1: Pure Prolog

string_chars("abc", Chars),
reverse(Chars, RevChars),
foldr(string_concat, RevChars, "", Result).
% Result = "cba"

Configuration 2: Starlog Syntax

starlog_call(B is foldr(string_concat, reverse(string_chars("abc")), "")).
% B = "cba"

Configuration 3: Step-by-Step Evaluation

Chars is string_chars("abc"),
RevChars is reverse(Chars),
Result is foldr(string_concat, RevChars, "").
% Result = "cba"

Configuration 4: Different Operations

  • String concatenation: foldr(string_concat, [c,b,a], "", Result)
  • List append: foldr(append, [[1],[2],[3]], [], Result)
  • Atom concatenation: foldr(atom_concat, [c,b,a], '', Result)

Configuration 5: Edge Cases

  • Empty list: foldr(string_concat, [], "init", Result) → Result = "init"
  • Single element: foldr(string_concat, [x], "", Result) → Result = "x"
  • Different strings: Works with "hello" → "olleh", "world" → "dlrow", etc.

Test Coverage

Unit Tests (tests/test_foldr.pl)

  • 7 comprehensive test cases covering all configurations
  • All tests pass successfully
  • Tests use refactored helper predicates to reduce code duplication

Demo (demo_foldr.pl)

  • 7 different configurations demonstrated
  • Interactive output showing step-by-step results
  • Validates correct behavior across all use cases

Verification Script (verify_foldr.sh)

  • Automated verification of the problem statement
  • 5 independent test scenarios
  • All scenarios pass with ✓ markers

Files Modified/Added

Modified Files

  1. starlog.pl - Added foldr/4 predicate implementation
  2. starlog_registry.pl - Registered foldr as value builtin

New Files

  1. tests/test_foldr.pl - Comprehensive unit tests
  2. demo_foldr.pl - Interactive demonstration
  3. verify_foldr.sh - Verification script

Performance Notes

  • The implementation uses structural recursion, which is appropriate for foldr semantics
  • Suitable for lists up to ~10,000 elements
  • For very large lists where order doesn't matter, foldl would be more efficient
  • The recursive implementation matches standard functional programming patterns

Verification Results

All tests pass successfully:

✓ Pure Prolog syntax
✓ Starlog syntax with nested functions
✓ Different input strings (hello, world, x, empty)
✓ foldr with list append
✓ Empty list edge case
✓ Step-by-step evaluation
✓ Nested Starlog expressions

Usage Examples

Example 1: Reverse a string (the problem statement)

?- starlog_call(B is foldr(string_concat, reverse(string_chars("abc")), "")).
B = "cba".

Example 2: Concatenate lists

?- foldr(append, [[1], [2], [3]], [], Result).
Result = [1, 2, 3].

Example 3: Build a string from characters

?- foldr(string_concat, [h, e, l, l, o], "", Result).
Result = "hello".

Conclusion

The foldr predicate has been successfully implemented with full support for:

  • Pure Prolog syntax
  • Starlog notation with nested function calls
  • Multiple operation types (string_concat, append, atom_concat, etc.)
  • Edge cases (empty lists, single elements)
  • All combinations and configurations as requested The implementation integrates seamlessly with the existing Starlog-in-Prolog library and maintains backward compatibility with all existing tests.