{"title":"An ML-style module system for cross-stage type abstraction in multi-stage programming","authors":"Takashi Suwa , Atsushi Igarashi","doi":"10.1016/j.scico.2025.103379","DOIUrl":null,"url":null,"abstract":"<div><div>We propose <em>MetaFM</em>, a novel ML-style module system that enables users to decompose <em>multi-stage programs</em> (i.e., programs written in a typed <em>multi-stage programming</em> language) into loosely coupled components in a manner natural with respect to type abstraction. The distinctive aspect of MetaFM is that it allows values at different stages to be bound in a single structure (i.e., <span><math><mrow><mi>struct</mi></mrow><mspace></mspace><mo>⋯</mo><mspace></mspace><mrow><mi>end</mi></mrow></math></span>). This feature is crucial, for example, for defining a function and a macro that use one abstract type in common without revealing the implementation detail of that type. MetaFM also accommodates staging with full-fledged module-related features such as <em>functors</em>, <em>higher-kinded types</em>, and the <strong>with type</strong><em>-construct</em>. We give two separate formalizations of MetaFM's semantics by employing the technique of <em>elaborations</em>, i.e., type-directed translations to target languages. Specifically, we first define <em>F-ing Modules</em>-based semantics as a set of elaboration rules that convert MetaFM programs into <em>System F</em><span><math><msup><mrow><mi>ω</mi></mrow><mrow><mo>〈</mo><mo>〉</mo></mrow></msup></math></span>, a multi-stage extension of System F<em>ω</em>, and prove that the elaboration preserves typing. The existential quantification offered by System F<span><math><msup><mrow><mi>ω</mi></mrow><mrow><mo>〈</mo><mo>〉</mo></mrow></msup></math></span> demonstrates that a type abstraction mechanism is properly formalized in our language. Then, because our F-ing Modules-based semantics of staging has some issues as to the evaluation order, we give another elaboration by utilizing a method called <em>static interpretation</em>, which flattens nested structures into arrays of bindings and inlines functor applications through type-checking. While our F-ing Modules-based semantics cannot be naturally extended with effectful computations, the static interpretation-based one can easily accommodate effectful features such as mutable references, though this is achieved with the limitation that functors must be first-order for the moment. As a sideline, we develop a technique that simplifies the correctness proof of the static interpretation for first-order functors. Additionally, our language supports <em>cross-stage persistence</em> (<em>CSP</em>), a feature for code reuse spanning more than one stage, without breaking type safety. We also implemented a module system for a language of real-world use based on the latter semantics to demonstrate the utility of our formalization.</div></div>","PeriodicalId":49561,"journal":{"name":"Science of Computer Programming","volume":"248 ","pages":"Article 103379"},"PeriodicalIF":1.4000,"publicationDate":"2025-08-11","publicationTypes":"Journal Article","fieldsOfStudy":null,"isOpenAccess":false,"openAccessPdf":"","citationCount":"0","resultStr":null,"platform":"Semanticscholar","paperid":null,"PeriodicalName":"Science of Computer Programming","FirstCategoryId":"94","ListUrlMain":"https://www.sciencedirect.com/science/article/pii/S0167642325001182","RegionNum":4,"RegionCategory":"计算机科学","ArticlePicture":[],"TitleCN":null,"AbstractTextCN":null,"PMCID":null,"EPubDate":"","PubModel":"","JCR":"Q3","JCRName":"COMPUTER SCIENCE, SOFTWARE ENGINEERING","Score":null,"Total":0}
引用次数: 0
Abstract
We propose MetaFM, a novel ML-style module system that enables users to decompose multi-stage programs (i.e., programs written in a typed multi-stage programming language) into loosely coupled components in a manner natural with respect to type abstraction. The distinctive aspect of MetaFM is that it allows values at different stages to be bound in a single structure (i.e., ). This feature is crucial, for example, for defining a function and a macro that use one abstract type in common without revealing the implementation detail of that type. MetaFM also accommodates staging with full-fledged module-related features such as functors, higher-kinded types, and the with type-construct. We give two separate formalizations of MetaFM's semantics by employing the technique of elaborations, i.e., type-directed translations to target languages. Specifically, we first define F-ing Modules-based semantics as a set of elaboration rules that convert MetaFM programs into System F, a multi-stage extension of System Fω, and prove that the elaboration preserves typing. The existential quantification offered by System F demonstrates that a type abstraction mechanism is properly formalized in our language. Then, because our F-ing Modules-based semantics of staging has some issues as to the evaluation order, we give another elaboration by utilizing a method called static interpretation, which flattens nested structures into arrays of bindings and inlines functor applications through type-checking. While our F-ing Modules-based semantics cannot be naturally extended with effectful computations, the static interpretation-based one can easily accommodate effectful features such as mutable references, though this is achieved with the limitation that functors must be first-order for the moment. As a sideline, we develop a technique that simplifies the correctness proof of the static interpretation for first-order functors. Additionally, our language supports cross-stage persistence (CSP), a feature for code reuse spanning more than one stage, without breaking type safety. We also implemented a module system for a language of real-world use based on the latter semantics to demonstrate the utility of our formalization.
期刊介绍:
Science of Computer Programming is dedicated to the distribution of research results in the areas of software systems development, use and maintenance, including the software aspects of hardware design.
The journal has a wide scope ranging from the many facets of methodological foundations to the details of technical issues andthe aspects of industrial practice.
The subjects of interest to SCP cover the entire spectrum of methods for the entire life cycle of software systems, including
• Requirements, specification, design, validation, verification, coding, testing, maintenance, metrics and renovation of software;
• Design, implementation and evaluation of programming languages;
• Programming environments, development tools, visualisation and animation;
• Management of the development process;
• Human factors in software, software for social interaction, software for social computing;
• Cyber physical systems, and software for the interaction between the physical and the machine;
• Software aspects of infrastructure services, system administration, and network management.