skip to main content
research-article
Open access

Answer Refinement Modification: Refinement Type System for Algebraic Effects and Handlers

Published: 05 January 2024 Publication History

Abstract

Algebraic effects and handlers are a mechanism to structure programs with computational effects in a modular way. They are recently gaining popularity and being adopted in practical languages, such as OCaml. Meanwhile, there has been substantial progress in program verification via refinement type systems. While a variety of refinement type systems have been proposed, thus far there has not been a satisfactory refinement type system for algebraic effects and handlers. In this paper, we fill the void by proposing a novel refinement type system for languages with algebraic effects and handlers. The expressivity and usefulness of algebraic effects and handlers come from their ability to manipulate delimited continuations, but delimited continuations also complicate programs’ control flow and make their verification harder. To address the complexity, we introduce a novel concept that we call answer refinement modification (ARM for short), which allows the refinement type system to precisely track what effects occur and in what order when a program is executed, and reflect such information as modifications to the refinements in the types of delimited continuations. We formalize our type system that supports ARM (as well as answer type modification, or ATM) and prove its soundness. Additionally, as a proof of concept, we have extended the refinement type system to a subset of OCaml 5 which comes with a built-in support for effect handlers, implemented a type checking and inference algorithm for the extension, and evaluated it on a number of benchmark programs that use algebraic effects and handlers. The evaluation demonstrates that ARM is conceptually simple and practically useful. Finally, a natural alternative to directly reasoning about a program with delimited continuations is to apply a continuation passing style (CPS) transformation that transforms the program to a pure program without delimited continuations. We investigate this alternative in the paper, and show that the approach is indeed possible by proposing a novel CPS transformation for algebraic effects and handlers that enjoys bidirectional (refinement-)type-preservation. We show that there are pros and cons with this approach, namely, while one can use an existing refinement type checking and inference algorithm that can only (directly) handle pure programs, there are issues such as needing type annotations in source programs and making the inferred types less informative to a user.

References

[1]
Danel Ahman. 2017. Handling Fibred Algebraic Effects. Proc. ACM Program. Lang., 2, POPL (2017), Article 7, dec, 29 pages. https://doi.org/10.1145/3158095
[2]
Danel Ahman and Gordon Plotkin. 2015. Refinement types for algebraic effects. In Abstracts of the 21st Meeting ‘Types for Proofs and Programs’ (TYPES). Institute of Cybernetics, Tallinn University of Technology, 10–11.
[3]
Andrew W. Appel. 1992. Compiling with Continuations. Cambridge University Press. https://doi.org/10.1017/CBO9780511609619
[4]
Kenichi Asai. 2009. On typing delimited continuations: three new solutions to the printf problem. Higher-Order and Symbolic Computation, 22, 3 (2009), 01 Sep, 275–291. issn:1573-0557 https://doi.org/10.1007/s10990-009-9049-5
[5]
Andrej Bauer. 2018. What is algebraic about algebraic effects and handlers? CoRR, abs/1807.05923 (2018), arXiv:1807.05923. arxiv:1807.05923
[6]
Andrej Bauer and Matija Pretnar. 2013. An Effect System for Algebraic Effects and Handlers. In Algebra and Coalgebra in Computer Science, Reiko Heckel and Stefan Milius (Eds.). Springer Berlin Heidelberg, Berlin, Heidelberg. 1–16. isbn:978-3-642-40206-7
[7]
Andrej Bauer and Matija Pretnar. 2015. Programming with algebraic effects and handlers. Journal of Logical and Algebraic Methods in Programming, 84, 1 (2015), 108–123. issn:2352-2208 https://doi.org/10.1016/j.jlamp.2014.02.001 Special Issue: The 23rd Nordic Workshop on Programming Theory (NWPT 2011) Special Issue: Domains X, International workshop on Domain Theory and applications, Swansea, 5-7 September, 2011
[8]
Jesper Bengtson, Karthikeyan Bhargavan, Cédric Fournet, Andrew D. Gordon, and Sergio Maffeis. 2011. Refinement Types for Secure Implementations. ACM Trans. Program. Lang. Syst., 33, 2 (2011), Article 8, feb, 45 pages. issn:0164-0925 https://doi.org/10.1145/1890028.1890031
[9]
Dariusz Biernacki, Maciej Piróg, Piotr Polesiuk, and Filip Sieczkowski. 2020. Binders by day, labels by night: effect instances via lexically scoped handlers. Proc. ACM Program. Lang., 4, POPL (2020), 48:1–48:29. https://doi.org/10.1145/3371116
[10]
Nikolaj Bjørner, Arie Gurfinkel, Kenneth L. McMillan, and Andrey Rybalchenko. 2015. Horn Clause Solvers for Program Verification. In Fields of Logic and Computation II: Essays Dedicated to Yuri Gurevich on the Occasion of His 75th Birthday. 9300, 24–51.
[11]
Aaron R. Bradley. 2011. SAT-Based Model Checking without Unrolling. In Proceedings of the 12th International Conference on Verification, Model Checking, and Abstract Interpretation (VMCAI’11). Springer-Verlag, Berlin, Heidelberg. 70–87. isbn:9783642182747
[12]
Edwin Brady. 2013. Programming and Reasoning with Algebraic Effects and Dependent Types. In Proceedings of the 18th ACM SIGPLAN International Conference on Functional Programming (ICFP ’13). Association for Computing Machinery, New York, NY, USA. 133–144. isbn:9781450323260 https://doi.org/10.1145/2500365.2500581
[13]
Youyou Cong and Kenichi Asai. 2018. Handling Delimited Continuations with Dependent Types. Proc. ACM Program. Lang., 2, ICFP (2018), Article 69, jul, 31 pages. https://doi.org/10.1145/3236764
[14]
Youyou Cong and Kenichi Asai. 2022. Understanding Algebraic Effect Handlers via Delimited Control Operators. In Trends in Functional Programming - 23rd International Symposium, TFP 2022, Virtual Event, March 17-18, 2022, Revised Selected Papers, Wouter Swierstra and Nicolas Wu (Eds.) (Lecture Notes in Computer Science, Vol. 13401). Springer, 59–79. https://doi.org/10.1007/978-3-031-21314-4_4
[15]
Olivier Danvy and Andrzej Filinski. 1990. Abstracting Control. In Proceedings of the 1990 ACM Conference on LISP and Functional Programming (LFP ’90). Association for Computing Machinery, New York, NY, USA. 151–160. isbn:089791368X https://doi.org/10.1145/91556.91622
[16]
Leonardo de Moura and Nikolaj Bjørner. 2008. Z3: An Efficient SMT Solver. In TACAS’08. 4963, Springer Berlin Heidelberg, 337–340.
[17]
Niklas Een, Alan Mishchenko, and Robert Brayton. 2011. Efficient Implementation of Property Directed Reachability. In Proceedings of the International Conference on Formal Methods in Computer-Aided Design (FMCAD ’11). FMCAD Inc, Austin, Texas. 125–134. isbn:9780983567813
[18]
Yannick Forster, Ohad Kammar, Sam Lindley, and Matija Pretnar. 2017. On the Expressive Power of User-Defined Effects: Effect Handlers, Monadic Reflection, Delimited Control. Proc. ACM Program. Lang., 1, ICFP (2017), Article 13, aug, 29 pages. https://doi.org/10.1145/3110257
[19]
Jeffrey S. Foster, Tachio Terauchi, and Alexander Aiken. 2002. Flow-Sensitive Type Qualifiers. In Proceedings of the 2002 ACM SIGPLAN Conference on Programming Language Design and Implementation (PLDI), Berlin, Germany, June 17-19, 2002, Jens Knoop and Laurie J. Hendren (Eds.). ACM, 1–12. https://doi.org/10.1145/512529.512531
[20]
Timothy S. Freeman and Frank Pfenning. 1991. Refinement Types for ML. In Proceedings of the ACM SIGPLAN’91 Conference on Programming Language Design and Implementation (PLDI), Toronto, Ontario, Canada, June 26-28, 1991, David S. Wise (Ed.). ACM, 268–277. https://doi.org/10.1145/113445.113468
[21]
Daniel Hillerström and Sam Lindley. 2018. Shallow Effect Handlers. In Programming Languages and Systems, Sukyoung Ryu (Ed.). Springer International Publishing, Cham. 415–435. isbn:978-3-030-02768-1
[22]
Daniel Hillerström, Sam Lindley, Robert Atkey, and K. C. Sivaramakrishnan. 2017. Continuation Passing Style for Effect Handlers. In 2nd International Conference on Formal Structures for Computation and Deduction (FSCD 2017), Dale Miller (Ed.) (Leibniz International Proceedings in Informatics (LIPIcs), Vol. 84). Schloss Dagstuhl–Leibniz-Zentrum fuer Informatik, Dagstuhl, Germany. 18:1–18:19. isbn:978-3-95977-047-7 issn:1868-8969 https://doi.org/10.4230/LIPIcs.FSCD.2017.18
[23]
Chiaki Ishio and Kenichi Asai. 2022. Type System for Four Delimited Control Operators. In Proceedings of the 21st ACM SIGPLAN International Conference on Generative Programming: Concepts and Experiences (GPCE 2022). Association for Computing Machinery, New York, NY, USA. 45–58. isbn:9781450399203 https://doi.org/10.1145/3564719.3568691
[24]
Ohad Kammar, Sam Lindley, and Nicolas Oury. 2013. Handlers in Action. In Proceedings of the 18th ACM SIGPLAN International Conference on Functional Programming (ICFP ’13). Association for Computing Machinery, New York, NY, USA. 145–158. isbn:9781450323260 https://doi.org/10.1145/2500365.2500590
[25]
Ohad Kammar and Matija Pretnar. 2017. No value restriction is needed for algebraic effects and handlers. Journal of Functional Programming, 27 (2017), e7. https://doi.org/10.1017/S0956796816000320
[26]
Anvesh Komuravelli, Arie Gurfinkel, Sagar Chaki, and Edmund M. Clarke. 2013. Automatic Abstraction in SMT-Based Unbounded Software Model Checking. In Proceedings of the 25th International Conference on Computer Aided Verification - Volume 8044 (CAV 2013). Springer-Verlag, Berlin, Heidelberg. 846–862. isbn:9783642397981
[27]
Daan Leijen. 2014. Koka: Programming with Row Polymorphic Effect Types. In Proceedings 5th Workshop on Mathematically Structured Functional Programming, MSFP@ETAPS 2014, Grenoble, France, 12 April 2014, Paul Blain Levy and Neel Krishnaswami (Eds.) (EPTCS, Vol. 153). 100–126. https://doi.org/10.4204/EPTCS.153.8
[28]
Daan Leijen. 2017. Type Directed Compilation of Row-Typed Algebraic Effects. In Proceedings of the 44th ACM SIGPLAN Symposium on Principles of Programming Languages (POPL ’17). Association for Computing Machinery, New York, NY, USA. 486–499. isbn:9781450346603 https://doi.org/10.1145/3009837.3009872
[29]
PaulBlain Levy, John Power, and Hayo Thielecke. 2003. Modelling environments in call-by-value programming languages. Information and Computation, 185, 2 (2003), 182–210. issn:0890-5401 https://doi.org/10.1016/S0890-5401(03)00088-9
[30]
Sam Lindley, Conor McBride, and Craig McLaughlin. 2017. Do Be Do Be Do. In Proceedings of the 44th ACM SIGPLAN Symposium on Principles of Programming Languages (POPL ’17). Association for Computing Machinery, New York, NY, USA. 500–514. isbn:9781450346603 https://doi.org/10.1145/3009837.3009897
[31]
Marek Materzok and Dariusz Biernacki. 2011. Subtyping Delimited Continuations. In Proceedings of the 16th ACM SIGPLAN International Conference on Functional Programming (ICFP ’11). Association for Computing Machinery, New York, NY, USA. 81–93. isbn:9781450308656 https://doi.org/10.1145/2034773.2034786
[32]
John C. Mitchell. 1988. Polymorphic type inference and containment. Information and Computation, 76, 2 (1988), 211–249. issn:0890-5401 https://doi.org/10.1016/0890-5401(88)90009-0
[33]
Multicore OCaml. 2022. OCaml effects examples. https://github.com/ocaml-multicore/effects-examples
[34]
Yoji Nanjo, Hiroshi Unno, Eric Koskinen, and Tachio Terauchi. 2018. A Fixpoint Logic and Dependent Effects for Temporal Property Verification. In Proceedings of the 33rd Annual ACM/IEEE Symposium on Logic in Computer Science, LICS 2018, Oxford, UK, July 09-12, 2018, Anuj Dawar and Erich Grädel (Eds.). ACM, 759–768. https://doi.org/10.1145/3209108.3209204
[35]
Maciej Piróg, Piotr Polesiuk, and Filip Sieczkowski. 2019. Typed Equivalence of Effect Handlers and Delimited Control. In 4th International Conference on Formal Structures for Computation and Deduction (FSCD 2019), Herman Geuvers (Ed.) (Leibniz International Proceedings in Informatics (LIPIcs), Vol. 131). Schloss Dagstuhl–Leibniz-Zentrum fuer Informatik, Dagstuhl, Germany. 30:1–30:16. isbn:978-3-95977-107-8 issn:1868-8969 https://doi.org/10.4230/LIPIcs.FSCD.2019.30
[36]
Gordon Plotkin. 1975. Call-by-name, call-by-value and the λ -calculus. Theoretical Computer Science, 1, 2 (1975), 125–159. issn:0304-3975 https://doi.org/10.1016/0304-3975(75)90017-1
[37]
Gordon Plotkin and John Power. 2003. Algebraic Operations and Generic Effects. Applied Categorical Structures, 11, 1 (2003), 01 Feb, 69–94. issn:1572-9095 https://doi.org/10.1023/A:1023064908962
[38]
Gordon Plotkin and Matija Pretnar. 2009. Handlers of Algebraic Effects. In Programming Languages and Systems, Giuseppe Castagna (Ed.). Springer Berlin Heidelberg, Berlin, Heidelberg. 80–94. isbn:978-3-642-00590-9
[39]
Gordon D. Plotkin and John Power. 2001. Adequacy for Algebraic Effects. In Foundations of Software Science and Computation Structures, 4th International Conference, FOSSACS 2001 Held as Part of the Joint European Conferences on Theory and Practice of Software, ETAPS 2001 Genova, Italy, April 2-6, 2001, Proceedings, Furio Honsell and Marino Miculan (Eds.) (Lecture Notes in Computer Science, Vol. 2030). Springer, 1–24. https://doi.org/10.1007/3-540-45315-6_1
[40]
Gordon D Plotkin and Matija Pretnar. 2013. Handling Algebraic Effects. Logical Methods in Computer Science, Volume 9, Issue 4 (2013), Dec., https://doi.org/10.2168/LMCS-9(4:23)2013
[41]
Matija Pretnar. 2015. An Introduction to Algebraic Effects and Handlers. Invited tutorial paper. Electronic Notes in Theoretical Computer Science, 319 (2015), 19–35. issn:1571-0661 https://doi.org/10.1016/j.entcs.2015.12.003 The 31st Conference on the Mathematical Foundations of Programming Semantics (MFPS XXXI)
[42]
Matija Pretnar. 2022. Eff. https://github.com/matijapretnar/eff
[43]
Patrick M. Rondon, Ming Kawaguci, and Ranjit Jhala. 2008. Liquid Types. In Proceedings of the 29th ACM SIGPLAN Conference on Programming Language Design and Implementation (PLDI ’08). Association for Computing Machinery, New York, NY, USA. 159–169. isbn:9781595938602 https://doi.org/10.1145/1375581.1375602
[44]
Taro Sekiyama and Hiroshi Unno. 2023. Temporal Verification with Answer-Effect Modification: Dependent Temporal Type-and-Effect System with Delimited Continuations. Proc. ACM Program. Lang., 7, POPL (2023), Article 71, jan, 32 pages. https://doi.org/10.1145/3571264
[45]
KC Sivaramakrishnan, Stephen Dolan, Leo White, Tom Kelly, Sadiq Jaffer, and Anil Madhavapeddy. 2021. Retrofitting Effect Handlers onto OCaml. In Proceedings of the 42nd ACM SIGPLAN International Conference on Programming Language Design and Implementation (PLDI 2021). Association for Computing Machinery, New York, NY, USA. 206–221. isbn:9781450383912 https://doi.org/10.1145/3453483.3454039
[46]
Armando Solar-Lezama, Liviu Tancau, Rastislav Bodik, Sanjit Seshia, and Vijay Saraswat. 2006. Combinatorial Sketching for Finite Programs. In Proceedings of the 12th International Conference on Architectural Support for Programming Languages and Operating Systems (ASPLOS XII). Association for Computing Machinery, New York, NY, USA. 404–415. isbn:1595934510 https://doi.org/10.1145/1168857.1168907
[47]
Nikhil Swamy, Catalin Hritcu, Chantal Keller, Aseem Rastogi, Antoine Delignat-Lavaud, Simon Forest, Karthikeyan Bhargavan, Cédric Fournet, Pierre-Yves Strub, Markulf Kohlweiss, Jean Karim Zinzindohoue, and Santiago Zanella Béguelin. 2016. Dependent types and multi-monadic effects in F. In Proceedings of the 43rd Annual ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, POPL 2016, St. Petersburg, FL, USA, January 20 - 22, 2016, Rastislav Bodík and Rupak Majumdar (Eds.). ACM, 256–270. https://doi.org/10.1145/2837614.2837655
[48]
Tachio Terauchi. 2010. Dependent types from counterexamples. In Proceedings of the 37th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, POPL 2010, Madrid, Spain, January 17-23, 2010, Manuel V. Hermenegildo and Jens Palsberg (Eds.). ACM, 119–130. https://doi.org/10.1145/1706299.1706315
[49]
Hayo Thielecke. 2003. From control effects to typed continuation passing. In Conference Record of POPL 2003: The 30th SIGPLAN-SIGACT Symposium on Principles of Programming Languages, New Orleans, Louisisana, USA, January 15-17, 2003, Alex Aiken and Greg Morrisett (Eds.). ACM, 139–149. https://doi.org/10.1145/604131.604144
[50]
Hiroshi Unno and Naoki Kobayashi. 2009. Dependent Type Inference with Interpolants. In Proceedings of the 11th ACM SIGPLAN Conference on Principles and Practice of Declarative Programming (PPDP ’09). Association for Computing Machinery, New York, NY, USA. 277–288. isbn:9781605585680 https://doi.org/10.1145/1599410.1599445
[51]
Hiroshi Unno, Yuki Satake, and Tachio Terauchi. 2018. Relatively complete refinement type system for verification of higher-order non-deterministic programs. Proc. ACM Program. Lang., 2, POPL (2018), 12:1–12:29. https://doi.org/10.1145/3158100
[52]
Hiroshi Unno, Tachio Terauchi, and Naoki Kobayashi. 2013. Automating Relatively Complete Verification of Higher-Order Functional Programs. SIGPLAN Not., 48, 1 (2013), jan, 75–86. issn:0362-1340 https://doi.org/10.1145/2480359.2429081
[53]
Hiroshi Unno, Tachio Terauchi, and Eric Koskinen. 2021. Constraint-Based Relational Verification. In CAV’21. Springer Berlin Heidelberg, 742–766.
[54]
Niki Vazou, Eric L. Seidel, Ranjit Jhala, Dimitrios Vytiniotis, and Simon L. Peyton Jones. 2014. Refinement types for Haskell. In Proceedings of the 19th ACM SIGPLAN international conference on Functional programming, Gothenburg, Sweden, September 1-3, 2014, Johan Jeuring and Manuel M. T. Chakravarty (Eds.). ACM, 269–282. https://doi.org/10.1145/2628136.2628161
[55]
Panagiotis Vekris, Benjamin Cosman, and Ranjit Jhala. 2016. Refinement types for TypeScript. In Proceedings of the 37th ACM SIGPLAN Conference on Programming Language Design and Implementation, PLDI 2016, Santa Barbara, CA, USA, June 13-17, 2016, Chandra Krintz and Emery D. Berger (Eds.). ACM, 310–325. https://doi.org/10.1145/2908080.2908110
[56]
Hongwei Xi and Frank Pfenning. 1999. Dependent Types in Practical Programming. In POPL ’99, Proceedings of the 26th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, San Antonio, TX, USA, January 20-22, 1999, Andrew W. Appel and Alex Aiken (Eds.). ACM, 214–227. https://doi.org/10.1145/292540.292560
[57]
Yizhou Zhang and Andrew C. Myers. 2019. Abstraction-safe effect handlers via tunneling. Proc. ACM Program. Lang., 3, POPL (2019), 5:1–5:29. https://doi.org/10.1145/3290318
[58]
He Zhu and Suresh Jagannathan. 2013. Compositional and Lightweight Dependent Type Inference for ML. In Verification, Model Checking, and Abstract Interpretation, 14th International Conference, VMCAI 2013, Rome, Italy, January 20-22, 2013. Proceedings, Roberto Giacobazzi, Josh Berdine, and Isabella Mastroeni (Eds.) (Lecture Notes in Computer Science, Vol. 7737). Springer, 295–314. https://doi.org/10.1007/978-3-642-35873-9_19

Cited By

View all

Recommendations

Comments

Information & Contributors

Information

Published In

cover image Proceedings of the ACM on Programming Languages
Proceedings of the ACM on Programming Languages  Volume 8, Issue POPL
January 2024
2820 pages
EISSN:2475-1421
DOI:10.1145/3554315
Issue’s Table of Contents
This work is licensed under a Creative Commons Attribution 4.0 International License.

Publisher

Association for Computing Machinery

New York, NY, United States

Publication History

Published: 05 January 2024
Published in PACMPL Volume 8, Issue POPL

Permissions

Request permissions for this article.

Check for updates

Badges

Author Tags

  1. CPS transformation
  2. algebraic effects and handlers
  3. answer refinement modification
  4. answer type modification
  5. refinement type system
  6. type-and-effect system

Qualifiers

  • Research-article

Funding Sources

Contributors

Other Metrics

Bibliometrics & Citations

Bibliometrics

Article Metrics

  • Downloads (Last 12 months)378
  • Downloads (Last 6 weeks)69
Reflects downloads up to 17 Oct 2024

Other Metrics

Citations

Cited By

View all

View Options

View options

PDF

View or Download as a PDF file.

PDF

eReader

View online with eReader.

eReader

Get Access

Login options

Full Access

Media

Figures

Other

Tables

Share

Share

Share this Publication link

Share on social media