This issue is for architectural refinement and planning a series of actionable, stepwise issues to bring generics (template-based polymorphism) to Fortrun, targeting compatibility with LFortran generics syntax first, and then extending to the concise trait/generic style proposed in https://github.com/difference-scheme/Traits-for-Fortran/.
Overall Goal:
- Enable client code to use generics elegantly, with boilerplate encapsulated in generated code.
- Allow the frontend to parse both LFortran-compatible and future short-form trait/generic syntax.
- Analyzer should support type inference, type constraints, and generate specialized code automatically for standard Fortran.
- Support for interfaces, abstract types, unlimited polymorphism, and select type constructs.
- Architecturally remain open to multiple dispatch in the future.
- More immediately, extend type inference to handle abstract types, inheritance, and possibly add a backward pass supplementing the Hindley-Milner system (similar to TypeScript's approach).
Stepwise Action Plan:
- Syntax & Parser: Add/extend parser to support LFortran generics syntax (template/require/instantiate) and plan for future concise syntax (
function foo{T} etc.).
- Analyzer: Extend type inference to handle generics, abstract types, and inheritance. Consider backward type propagation for cases where Hindley-Milner is insufficient.
- Code Generation: Automatically generate specialized versions for each instantiation, encapsulating all boilerplate. Output standard Fortran code using interfaces, abstract types, select type, etc.
- Client Experience: Ensure client code can call generic procedures with minimal ceremony and maximal clarity.
- Trait and Interface Integration: Plan for seamless use of generics with interfaces, abstract types, and unlimited polymorphism.
- Extensibility: Keep architecture open for future multiple dispatch, extensible traits, and structural/nominal constraint checking.
Initial Sub-Issue Extraction Candidates:
- Parser: Grammar extensions for
requirement, template, instantiate, and brace-parameterized function form.
- AST/IR: Nodes for generic parameters, constraints, instantiation sites.
- Type System: Backward constraint propagation pass (post-inference refinement).
- Abstract Type Handling: Enrich inference with inheritance lattice and unlimited polymorphic (
class(*)) narrowing via select type.
- Constraint Representation: Unified internal model for requirements now; future trait/implements compatibility layer.
- Monomorphization Pipeline: Collection, dependency analysis, instantiation ordering, caching / de-duplication.
- Codegen: Emission of specialized standard Fortran modules/procedures; name mangling scheme.
- Boilerplate Suppression: Strategy for generating interface blocks / wrapper modules.
- Diagnostics: Friendly messages for unsatisfied requirements / ambiguous instantiation.
- Testing Harness: Golden tests for generic expansion and specialization equivalence.
- Performance Considerations: Caching strategy; avoidance of code bloat.
- Roadmap for Short-Form Syntax: Mapping from concise trait syntax (Traits-for-Fortran) to internal representation.
- Multiple Dispatch Feasibility Study: Architectural hooks (argument type vector signature table).
- Migration Tools: Refactoring assistance to lift ad-hoc overload sets into generics.
Vision:
- Lazy Fortran standardized via fortfront should enable idiomatic, elegant, and safe generic programming—first matching LFortran's capabilities, then extending to the more expressive and concise patterns discussed in Traits-for-Fortran.
- All complexity and boilerplate should be hidden from the user and handled by the codegen/standardizer.
This is an umbrella issue: please extract smaller actionable issues from each numbered item above, as well as from the design/vision notes.
This issue is for architectural refinement and planning a series of actionable, stepwise issues to bring generics (template-based polymorphism) to Fortrun, targeting compatibility with LFortran generics syntax first, and then extending to the concise trait/generic style proposed in https://github.com/difference-scheme/Traits-for-Fortran/.
Overall Goal:
Stepwise Action Plan:
function foo{T}etc.).Initial Sub-Issue Extraction Candidates:
requirement,template,instantiate, and brace-parameterized function form.class(*)) narrowing viaselect type.Vision:
This is an umbrella issue: please extract smaller actionable issues from each numbered item above, as well as from the design/vision notes.