Make Importing Order Independence#161
Conversation
…e compilation - Added pre_register_imports/1 to scan all import! statements in a file and pre-register function names and arities from imported files before compiling the main file. - Ensures that all function signatures are available regardless of import order, eliminating import order dependency issues. - Handles nested imports recursively and safely ignores missing or Python files. - Fixes cases where dependent functions were not visible due to import order.'
Added test files to ensure import order does not affect function resolution or evaluation. Ensured robust coverage for cross-file dependencies and type safety.
a97b88f to
445fe19
Compare
|
What if imports are done in an imported file? How can order-independence still be guaranteed? And in such case, will it still work if there are cyclic imports? Related to #157 |
- Add cycle detection to import pre-registration and runtime import logic - Prevent infinite recursion by tracking already imported files - Ensure function definitions from cyclically imported files are registered exactly once - Skip redundant runtime imports if a file is already processed
…est cases - Add file_a.metta and file_b.metta as simple modules with cross-calling functions - Add test_import_order.metta to demonstrate import order independence - Add test_nested_imports.metta to demonstrate nested import resolution - Add test_cyclic_imports.metta to demonstrate cyclic import handling
- Deleted import_order.metta, import_order_helper.metta, and import_order_test.metta from examples - These files are replaced by new, clearer import order, nested, and cyclic import test cases
What if imports are done in an imported file? How can order-independence still be guaranteed? And in such case, will it still work if there are cyclic imports? Test cases for all these scenarios—import order independence, nested imports, and cyclic imports—are included and verified in the examples directory. |
Summary
This PR implements order-independent importing in PeTTa. The import system and runtime have been enhanced so that all function definitions and dependencies are registered before evaluation, allowing files to be imported in any order without affecting program correctness. This eliminates a major source of user error.
Problem and reproduction
Previously, importing files in the wrong order could cause missing function errors or unevaluated calls, because function signatures were only registered as files were loaded. This made modular code fragile and error-prone.
Before and After Example: Import Order Independence
Example Files
utils.metta
dependent.metta
main.metta
Result Before the Fix (Order-Dependent)
mainimportsdependentbeforeutils, sois-evenis not defined whenprocess-numberis compiled.(* 30 3) = 90Result After the Fix (Order-Independent)
is-evenis available toprocess-numberregardless of import order.is-even 30is true, so the "then" branch:(+ 30 20) = 50Root cause
The import system registered function signatures only as files were loaded, so dependent functions were not visible if their defining file was imported later. This led to order-dependent errors and brittle code.
What I changed
Why this fix
Order independence is essential for modularity, maintainability, and user experience. This change allows users to organize and import their code in any order, without worrying about dependency errors or unevaluated calls. It brings PeTTa in line with modern language best practices.
Testing & validation
A comprehensive test suite is included to validate the new behavior:
Impact