Repository navigation
v1.1.1 - Memoization Bug Fix
π 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.1Files Changed
src/Generator/Generator.php- Fixed closure cache sharingtests/Feature/MemoizationTest.php- New comprehensive testsgrm/other/memoization_test.grm- Reference grammarMEMOIZATION_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! π