[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"doc-detail-85524-en":3,"doc-seo-85524-105":29,"detail-sidebar-cat-0-en-105":91},{"code":4,"msg":5,"data":6},0,"success",{"doc_id":7,"user_id":8,"nickname":9,"user_avatar":10,"doc_module":4,"category_id":11,"category_name":12,"doc_title":13,"doc_description":14,"doc_content":15,"file_id":16,"file_url":17,"file_type":18,"file_size":19,"view_count":20,"is_deleted":4,"is_public":20,"is_downloadable":20,"audit_status":20,"page_count":21,"language":22,"language_code":23,"site_id":24,"html_lang":23,"table_of_contents":25,"faqs":26,"seo_title":13,"seo_description":14,"update_tm":27,"read_time":28},85524,8796095360427,"Lucas Martin","https://ap-avatar.wpscdn.com/davatar_994ba38a5ba835b3df7d355c54d3ed8d",8,"Research & Report","Taming Scope Extrusion in Gradual Imperative Metaprogramming","Metaprogramming can generate efficient code, and gradual typing supports migration from untyped scripts to statically typed programs. When combined with imperative state via mutable references, the interaction can cause scope extrusion, letting fragments with free variables escape their lexical context. The paper introduces Re★f, the first gradual metaprogramming language that allows mutable references while guaranteeing scope safety. It builds a statically typed sister Ref, mechanizes safety proofs in Agda, and enforces environment-classifier discipline dynamically using a new cast calculus, with space-efficient runtime scope checks.","Taming Scope Extrusion in  \nGradual Imperative Metaprogramming  \nTIANYU CHEN, Indiana University, USADARSHAL SHETTY, Indiana University, USA JEREMY G. SIEK, Indiana University, USA  \nCHAO-HONG CHEN, Meta, USA WEIXI MA, Meta, USA ARNAUD VENET, Meta, USA ROCKY LIU, Meta, USA  \nMetaprogramming enables the generation of performant code, while gradual typing facilitates the smooth migration from untyped scripts to robust statically typed programs. However, combining these features with imperative state—specifically mutable references—introduces the classic peril of scope extrusion, where code fragments containing free variables escape their defining lexical context. While static type systems have employed environment classifiers to successfully tame this interaction, enforcing these invariants in a gradual language remains an open challenge.  \nThis paper presents 􀁟􀁕,Re★f, the first gradual metaprogramming language that supports mutable references while guaranteeing scope safety. To put 􀁟􀁕,Re★f on a firm foundation, we also develop its statically typed sister language, 􀁟􀁕Ref, which introduces unrestricted subtyping for environment classifiers. We mechanize the proof of scope safety for 􀁟􀁕Ref, contributing the first mechanized proof of scope safety for a statically typed language. Our key innovation, however, is the dynamic enforcement of the environment classifier discipline in 􀁟􀁕,Re★f , enabling the language to mediate between statically verified scopes and dynamically verified scopes. The dynamic enforcement is carried out in a novel cast calculus CC􀁕,Re★f that uses an extension of Henglein’s Coercion Calculus to handle code types, classifier polymorphism, and subtype constraints. We prove that 􀁟􀁕,Re★f satisfies type safety and scope safety. Finally, we provide a space-efficient implementation strategy for the dynamic scope checks, ensuring that the runtime overhead remains practical. All of our results are fully mechanized in Agda.  \nCCS Concepts: • Theory of computation → Program semantics; • Software and its engineering → Software development techniques.  \nAdditional Key Words and Phrases: gradual typing, metaprogramming, multi-stage programming, code generation, quote, splice, environment classifier, mutable reference, imperative, scope extrusion  \nACM Reference Format:  \nTianyu Chen, Darshal Shetty, Jeremy G. Siek, Chao-Hong Chen, Weixi Ma, Arnaud Venet, and Rocky Liu.  \n2018. Taming Scope Extrusion in Gradual Imperative Metaprogramming. In Proceedings of Make sure to enter the correct conference title from your rights confirmation email (Conference acronym ’XX). ACM, New York, NY, USA, 52 pages. [https://doi.org/XXXXXXX.XXXXXXX](https://doi.org/XXXXXXX.XXXXXXX)  \nAuthors’ Contact Information: Tianyu Chen, Indiana University, Bloomington, USA, [chen512@iu.edu](chen512@iu.edu); Darshal Shetty, Indiana University, Bloomington, USA, [dcshetty@iu.edu](dcshetty@iu.edu); Jeremy G. Siek, Indiana University, Bloomington, USA, jsiek@iu.  \nedu; Chao-Hong Chen, Meta, Menlo Park, USA, [chaohong@meta.com](chaohong@meta.com); Weixi Ma, Meta, Menlo Park, USA, [mavc@meta.com](mavc@meta.com);  \nArnaud Venet, Meta, Menlo Park, USA, [ajv@meta.com](ajv@meta.com); Rocky Liu, Meta, Menlo Park, USA, [rockyliu4@meta.com](rockyliu4@meta.com).  \nConference acronym ’XX, Woodstock, NY  \n2018. ACM ISBN 978-1-4503-XXXX-X/2018/06  \n[https://doi.org/XXXXXXX.XXXXXXX](https://doi.org/XXXXXXX.XXXXXXX)  \n, Vol. 1, No. 1, Article . Publication date: July 2018 .  \n1 Introduction  \nThe impetus of our work is a production problem at Meta: helping machine-learning engineers build and debug feature-engineering pipelines. These engineers use F3 [Ma et al. 2024], a DSL embedded in Python that generates statically typed data-pipeline descriptions. Although Python is dynamically typed, engineers add type annotations and check them with gradual type checkers such as Pyrefly (https://pyrefly.org/) . Thus F3 combines metaprogramming with gradual typing ina production settin","cbCailhvP4hEbSO0","https://ap.wps.com/l/cbCailhvP4hEbSO0","pdf",884605,1,52,"English","en",105,"# Introduction\n## Motivation and production setting\n## Scope safety problem in metaprogramming\n## Prior techniques for scope safety","[{\"question\":\"What problem does the paper address in gradual imperative metaprogramming?\",\"answer\":\"It addresses scope extrusion that occurs when metaprogramming and gradual typing are combined with imperative mutable references, allowing code fragments with free variables to escape their lexical scope.\"},{\"question\":\"How does Re★f ensure scope safety while supporting mutable references?\",\"answer\":\"Re★f enforces the environment-classifier discipline dynamically. It uses a novel cast calculus (CC★Re★f) to mediate between statically and dynamically verified scopes.\"},{\"question\":\"What role does the statically typed sister language Ref play?\",\"answer\":\"Ref provides a foundation for Re★f by introducing unrestricted subtyping for environment classifiers, and the paper mechanizes the proof of scope safety for Ref in Agda.\"}]",1784204168,131,{"code":4,"msg":30,"data":31},"ok",{"site_id":24,"language":23,"slug":32,"title":13,"keywords":33,"description":14,"schema_data":34,"social_meta":86,"head_meta":88,"extra_data":90,"updated_unix":27},"taming-scope-extrusion-in-gradual-imperative-metaprogramming","",{"@graph":35,"@context":85},[36,53,68],{"@type":37,"itemListElement":38},"BreadcrumbList",[39,43,47,50],{"item":40,"name":41,"@type":42,"position":20},"https://docshare.wps.com","Home","ListItem",{"item":44,"name":45,"@type":42,"position":46},"https://docshare.wps.com/document/","Document",2,{"item":48,"name":12,"@type":42,"position":49},"https://docshare.wps.com/document/research-report/",3,{"item":51,"name":13,"@type":42,"position":52},"https://docshare.wps.com/document/taming-scope-extrusion-in-gradual-imperative-metaprogramming/85524/",4,{"url":51,"name":13,"@type":54,"author":55,"headline":13,"publisher":57,"fileFormat":60,"inLanguage":23,"description":14,"dateModified":61,"datePublished":62,"encodingFormat":60,"isAccessibleForFree":63,"interactionStatistic":64},"DigitalDocument",{"name":9,"@type":56},"Person",{"url":40,"name":58,"@type":59},"DocShare","Organization","application/pdf","2026-07-17","2026-07-16",true,{"@type":65,"interactionType":66,"userInteractionCount":20},"InteractionCounter",{"@type":67},"ViewAction",{"@type":69,"mainEntity":70},"FAQPage",[71,77,81],{"name":72,"@type":73,"acceptedAnswer":74},"What problem does the paper address in gradual imperative metaprogramming?","Question",{"text":75,"@type":76},"It addresses scope extrusion that occurs when metaprogramming and gradual typing are combined with imperative mutable references, allowing code fragments with free variables to escape their lexical scope.","Answer",{"name":78,"@type":73,"acceptedAnswer":79},"How does Re★f ensure scope safety while supporting mutable references?",{"text":80,"@type":76},"Re★f enforces the environment-classifier discipline dynamically. It uses a novel cast calculus (CC★Re★f) to mediate between statically and dynamically verified scopes.",{"name":82,"@type":73,"acceptedAnswer":83},"What role does the statically typed sister language Ref play?",{"text":84,"@type":76},"Ref provides a foundation for Re★f by introducing unrestricted subtyping for environment classifiers, and the paper mechanizes the proof of scope safety for Ref in Agda.","https://schema.org",{"og:url":51,"og:type":87,"og:title":13,"og:site_name":58,"og:description":14},"article",{"robots":89,"canonical":51},"index,follow",{"doc_id":7,"site_id":24},{"code":4,"msg":5,"data":92},[93,97,101,105,110,115,120,123,128,131,135],{"id":20,"doc_module":4,"doc_module_name":45,"category_name":94,"show_sort_weight":95,"slug":96},"Story & Novel",90,"story-novel",{"id":46,"doc_module":4,"doc_module_name":45,"category_name":98,"show_sort_weight":99,"slug":100},"Literature",80,"literature",{"id":52,"doc_module":4,"doc_module_name":45,"category_name":102,"show_sort_weight":103,"slug":104},"Exam",70,"exam",{"id":106,"doc_module":4,"doc_module_name":45,"category_name":107,"show_sort_weight":108,"slug":109},5,"Comic",60,"comic",{"id":111,"doc_module":4,"doc_module_name":45,"category_name":112,"show_sort_weight":113,"slug":114},6,"Technology",50,"technology",{"id":116,"doc_module":4,"doc_module_name":45,"category_name":117,"show_sort_weight":118,"slug":119},7,"Healthcare",40,"healthcare",{"id":11,"doc_module":4,"doc_module_name":45,"category_name":12,"show_sort_weight":121,"slug":122},30,"research-report",{"id":124,"doc_module":4,"doc_module_name":45,"category_name":125,"show_sort_weight":126,"slug":127},9,"Religion & Spirituality",20,"religion-spirituality",{"id":126,"doc_module":4,"doc_module_name":45,"category_name":129,"show_sort_weight":126,"slug":130},"World Cup","world-cup",{"id":132,"doc_module":4,"doc_module_name":45,"category_name":133,"show_sort_weight":132,"slug":134},10,"Lifestyle","lifestyle",{"id":136,"doc_module":4,"doc_module_name":45,"category_name":137,"show_sort_weight":106,"slug":138},19,"General","general"]