Skip to content

Architectural Roadmap: Stepwise Integration of Generics and Trait-Based Syntax (with LFortran Compatibility) #33

@krystophny

Description

@krystophny

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:

  1. Syntax & Parser: Add/extend parser to support LFortran generics syntax (template/require/instantiate) and plan for future concise syntax (function foo{T} etc.).
  2. Analyzer: Extend type inference to handle generics, abstract types, and inheritance. Consider backward type propagation for cases where Hindley-Milner is insufficient.
  3. Code Generation: Automatically generate specialized versions for each instantiation, encapsulating all boilerplate. Output standard Fortran code using interfaces, abstract types, select type, etc.
  4. Client Experience: Ensure client code can call generic procedures with minimal ceremony and maximal clarity.
  5. Trait and Interface Integration: Plan for seamless use of generics with interfaces, abstract types, and unlimited polymorphism.
  6. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions