Skip to content

feat: Implement unified generator mode with common_generator abstraction#98

Open
s243a wants to merge 7 commits intomainfrom
feature/generator-mode-unified-api
Open

feat: Implement unified generator mode with common_generator abstraction#98
s243a wants to merge 7 commits intomainfrom
feature/generator-mode-unified-api

Conversation

@s243a
Copy link
Copy Markdown
Owner

@s243a s243a commented Nov 28, 2025

Summary

This PR implements a unified generator mode architecture across C#, Python, and Bash targets by introducing a shared common_generator.pl module. It refactors the C# target to support both query and generator modes, fixes critical bugs identified in code reviews, and establishes a foundation for consistent cross-target behavior.

Type of Change

  • New feature (non-breaking change which adds functionality)
  • Bug fix (non-breaking change which fixes an issue)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Refactoring (code restructure without changing functionality)

Key Changes

1. Architecture Improvements

  • Created common_generator.pl: Centralized variable mapping, expression translation, and negation handling
  • Refactored C# target: Renamed csharp_query_target.plcsharp_target.pl with dual-mode support
  • Added backward compatibility: Created csharp_query_target.pl shim to prevent breaking existing code

2. Critical Bug Fixes (from Code Reviews)

Codex Review #1 Fixes (Commit: b4429eb)

  1. Accessor format bug: Fixed fact["arg0"] generation (was generating fact["0"])
  2. N-way join support: Implemented recursive join handling for 3+ relational goals
  3. Negation support: Added \+ and not/1 translation to !total.Contains(...)
  4. API compatibility: Created shim module for smooth migration
  5. Enhanced testing: Generator tests now compile and execute C# code

Codex Review #2 Fixes (Commits: 2633a2b, 173e022)

  1. VarMap threading: Fixed critical bug where variables from later join goals resolved to null
  2. Accumulator logic: Rewrote compile_nway_join to properly thread Goal-Access pairs
  3. Test file corruption: Removed 184 lines of duplicate/corrupted code
  4. Test runner: Updated run_all_tests.pl to use renamed modules

3. New Modules

Module Purpose Lines
common_generator.pl Shared generator logic (variable mapping, expression translation, negation) ~85
test_common_generator.pl Unit tests for common generator helpers ~150
csharp_target.pl Unified C# target (query + generator modes) ~1845
csharp_query_target.pl Backward compatibility shim ~20

4. Modified Files

Core:

  • src/unifyweaver/core/recursive_compiler.pl - Updated imports to use empty lists (avoid conflicts)

Tests:

  • tests/core/test_csharp_target.pl - Renamed, added generator mode execution tests
  • tests/core/test_csharp_janus.pl - Updated imports
  • tests/core/test_csharp_constraint_operand.pl - Updated imports
  • run_all_tests.pl - Updated to new module names, added test_common_generator

Removed:

  • tests/core/test_csharp_query_target.pl - Replaced by test_csharp_target.pl

Breaking Changes

Module Rename

  • csharp_query_targetcsharp_target
  • Mitigation: Backward compatibility shim provided

Commits in This PR

  1. b4429eb - fix: Address CodeX review - implement generator mode fixes
  2. 2633a2b - fix: Address Codex Review 2 - fix N-way join VarMap threading
  3. 173e022 - fix: Address Codex Review 2 remaining issues

Testing

swipl -g "consult('run_all_tests.pl'), main, halt" -t halt

Status: Ready for Review

Fixed 5 critical issues from code review:
1. Accessor format bug - now generates fact[arg0] instead of fact[0]
2. N-way joins - support for 3+ goal joins with nested loops
3. Negation support - implements not/1 and backslash-plus
4. API compatibility - added csharp_query_target shim
5. Enhanced tests - generator test now compiles and runs C# code

Also renamed csharp_query_target to csharp_target and updated all references.
Critical fixes for multi-way joins:
- Thread accumulated Goal-Access pairs through compile_nway_join recursion
- Base case now uses ALL accumulated pairs for VarMap, not just FirstGoal
- Join conditions now properly reference correct source variables
- Removed broken collect_previous_goals and get_source_for_index helpers
- Fixed VarMap bug that caused variables from later goals to resolve to null

This fixes the core correctness issue where rules with 3+ relational goals
would generate incorrect C# code with broken variable lookups.
Fixed all remaining blocking issues from second review:

Issue 3 - Test File Syntax:
- Removed duplicate/corrupted code in test_csharp_target.pl
- File now loads without syntax errors
- Eliminated raw C# code appearing outside Prolog terms

Issue 4 - Test Suite Import:
- Updated run_all_tests.pl to use test_csharp_target
- Removed references to old test_csharp_query_target module

Additional:
- Wired test_common_generator.pl into test suite
- Added module import and invocation to run_all_tests.pl

All blocking issues from Codex Review 2 now resolved.
# Conflicts:
#	src/unifyweaver/core/recursive_compiler.pl
#	src/unifyweaver/targets/csharp_query_target.pl
#	src/unifyweaver/targets/python_target.pl
…e-unified-api

# Please enter a commit message to explain why this merge is necessary,
# especially if it merges an updated upstream into a topic branch.
#
# Lines starting with '#' will be ignored, and an empty message aborts
# the commit.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants