Skip to content

v1.1.1 - Memoization Bug Fix

Choose a tag to compare

@Procionegobbo Procionegobbo released this 15 Mar 22:41
· 2 commits to master since this release
06c22c3

πŸ› Bug Fix Release

This patch release fixes a critical bug in the memoization system where assignment rules (:=) were unintentionally sharing cache state across multiple rules.

What's Fixed

Memoization Cache Bug - Assignment rules that called the same non-terminal would generate identical values instead of different ones:

Before Fix:
S ::= first A and second B;
A := pick X;
B := pick X;
X ::= a | b | c;

Output: "first a and second a" (both same) ❌

After Fix:
Output: "first a and second b" (different values) βœ…

Root Cause

All assignment closures were unintentionally capturing a reference to the same $cache variable due to PHP's loop variable scope behavior. This was fixed by creating a factory method that ensures each assignment gets its own isolated cache variable.

Changes

  • Fixed: Cache variable isolation in Generator.php
  • Added: createAssignmentClosure() factory method
  • Added: Comprehensive test suite in MemoizationTest.php
  • Added: Reference test grammar demonstrating the fix
  • Tested: All 160 existing tests pass with no regressions

Test Results

βœ… All 160 tests passing
βœ… 2 new memoization tests confirm proper behavior
βœ… Counter-based weighted selection works correctly

Installation

composer require procionegobbo/polygen-php:^1.1.1

Files Changed

  • src/Generator/Generator.php - Fixed closure cache sharing
  • tests/Feature/MemoizationTest.php - New comprehensive tests
  • grm/other/memoization_test.grm - Reference grammar
  • MEMOIZATION_FIX_PLAN.md - Detailed analysis

Impact

This fix ensures that any grammar using multiple assignment rules with shared dependencies will now generate the expected diverse values instead of predictable identical output.


Thank you for using Polygen PHP! πŸŽ‰