Skip to main content

Sound and Complete Type Inference for Closed Effect Rows

  • Conference paper
  • First Online:
  • 205 Accesses

Part of the book series: Lecture Notes in Computer Science ((LNCS,volume 13401))

Abstract

Koka is a functional programming language that has algebraic effect handlers and a row-based effect system. The row-based effect system infers types by naively applying the Hindley-Milner type inference. However, it infers effect-polymorphic types for many functions, which are hard to read by the programmers and have a negative runtime performance impact to the evidence-passing translation. In order to improve readability and runtime efficiency, we aim to infer closed effect rows when possible, and open those closed effect rows automatically at instantiation to avoid loss of typability. This paper gives a type inference algorithm with the \({\mathsf {{open}}}\) and \({\mathsf {{close}}}\) mechanisms. In this paper, we define a type inference algorithm with the open and close constructs.

This is a preview of subscription content, log in via an institution.

Buying options

Chapter
USD   29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
eBook
USD   39.99
Price excludes VAT (USA)
  • Available as EPUB and PDF
  • Read on any device
  • Instant download
  • Own it forever
Softcover Book
USD   49.99
Price excludes VAT (USA)
  • Compact, lightweight edition
  • Dispatched in 3 to 5 business days
  • Free shipping worldwide - see info

Tax calculation will be finalised at checkout

Purchases are for personal use only

Learn about institutional subscriptions

Notes

  1. 1.

    Koka is the first practical language that tracks the (side) effects of functions in their type. .

References

  1. Bauer, A., Pretnar, M.: Programming with algebraic effects and handlers. J. Logic. Algebraic Methods Program. 84(1), 108–123 (2015)

    Article  MathSciNet  MATH  Google Scholar 

  2. Convent, L., Lindley, S., McBride, C., McLaughlin, C.: Doo bee doo bee doo. J. Funct. Program. 30, e9 (2020). To appear in the special issue on algebraic effects and handlers

    Google Scholar 

  3. Damas, L., Milner, R.: Principal type-schemes for functional programs. In: DeMillo, R.A. (ed.) Conference Record of the Ninth Annual ACM Symposium on Principles of Programming Languages, Albuquerque, New Mexico, USA, January 1982, pp. 207–212. ACM Press (1982). https://doi.org/10.1145/582153.582176

  4. Emrich, F., Lindley, S., Stolarek, J., Cheney, J., Coates, J.: FreezeML: complete and easy type inference for first-class polymorphism. In: Donaldson, A.F., Torlak, E. (eds.) Proceedings of the 41st ACM SIGPLAN International Conference on Programming Language Design and Implementation, PLDI 2020, London, UK, 15–20 June 2020, pp. 423–437. ACM (2020). https://doi.org/10.1145/3385412.3386003

  5. Hillerström, D., Lindley, S.: Liberating effects with rows and handlers. In: Chapman, J., Swierstra, W. (eds.) Proceedings of the 1st International Workshop on Type-Driven Development, TyDe@ICFP 2016, Nara, Japan, 18 September 2016, pp. 15–27. ACM (2016). https://doi.org/10.1145/2976022.2976033

  6. Jones, M.P.: A theory of qualified types. In: Krieg-Brückner, B. (ed.) ESOP 1992. LNCS, vol. 582, pp. 287–306. Springer, Heidelberg (1992). https://doi.org/10.1007/3-540-55253-7_17

    Chapter  Google Scholar 

  7. Kammar, O., Plotkin, G.D.: Algebraic foundations for effect-dependent optimisations. In: Proceedings of the 39th Annual ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, POPL 2012, New York, NY, USA, pp. 349–360. ACM (2012). https://doi.org/10.1145/2103656.2103698

  8. Karachalias, G., Pretnar, M., Saleh, A.H., Vanderhallen, S., Schrijvers, T.: Explicit effect subtyping. J. Funct. Program. 30, e15 (2020). https://doi.org/10.1017/S0956796820000131

    Article  MathSciNet  MATH  Google Scholar 

  9. Leijen, D.: Extensible records with scoped labels. In: Proceedings of the 2005 Symposium on Trends in Functional Programming, pp. 297–312 (2005)

    Google Scholar 

  10. Leijen, D.: HMF: simple type inference for first-class polymorphism. In: Proceedings of the 13th ACM Symposium of the International Conference on Functional Programming, ICFP 2008, Victoria, Canada, September 2008. https://doi.org/10.1145/1411204.1411245

  11. Leijen, D.: Koka: programming with row polymorphic effect types. In: 5th Workshop on Mathematically Structured Functional Programming, MSFP 2014 (2014). doi: 10.4204/EPTCS.153.8

    Google Scholar 

  12. Leijen, D.: Type directed compilation of row-typed algebraic effects. In: Proceedings of the 44th ACM SIGPLAN Symposium on Principles of Programming Languages (POPL 2017), Paris, France, pp. 486–499, January 2017. https://doi.org/10.1145/3009837.3009872

  13. Lindley, S., McBride, C., McLaughlin, C.: Do be do be do. In: Proceedings of the 44th ACM SIGPLAN Symposium on Principles of Programming Languages (POPL 2017), Paris, France, pp. 500–514, January 2017. https://doi.org/10.1145/3009837.3009897

  14. Jones, S.P., Washburn, G., Weirich, S.: Wobbly types: type inference for generalised algebraic data types. MS-CIS-05-26. Microsoft Research, July 2004

    Google Scholar 

  15. Plotkin, G.D., Power, J.: Algebraic operations and generic effects. Appl. Categ. Struct. 11(1), 69–94 (2003). https://doi.org/10.1023/A:1023064908962

    Article  MathSciNet  MATH  Google Scholar 

  16. Pretnar, M.: Inferring algebraic effects. Log. Methods Comput. Sci. 10(3) (2014). https://doi.org/10.2168/LMCS-10(3:21)2014

  17. Rémy, D.: Type inference for records in natural extension of ML. In: Theoretical Aspects of Object-Oriented Programming, pp. 67–95 (1994)

    Google Scholar 

  18. Robinson, J.A.: A machine-oriented logic based on the resolution principle. J. ACM 12(1), 23–41 (1965). https://doi.org/10.1145/321250.321253

    Article  MathSciNet  MATH  Google Scholar 

  19. Wright, A.K.: Simple imperative polymorphism. LISP Symb. Comput. 8(4), 343–355 (1995)

    Google Scholar 

  20. Xie, N., Brachthauser, J., Hillerstrom, D., Schuster, P., Leijen, D.: Effect handlers, evidently. In: The 25th ACM SIGPLAN International Conference on Functional Programming (ICFP). ACM SIGPLAN, August 2020. https://www.microsoft.com/en-us/research/publication/effect-handlers-evidently/

  21. Xie, N., Leijen, D.: Generized evidence passing for effect handlers - efficient compilation of effect handlers to C. In: Proceedings of the 26th ACM SIGPLAN International Conference on Functional Programming (ICFP 2021). Virtual, August 2021

    Google Scholar 

Download references

Acknowledgement

We would like to thank the anonymous reviewers for their valuable comments and the members of Masuhara laboratory for helpful advice. We are particularly grateful to Naoya Furudono for useful suggestions and discussions. This work was supported in part by JSPS KAKENHI under Grant No.  JP19K24339.

Author information

Authors and Affiliations

Authors

Corresponding author

Correspondence to Kazuki Ikemori .

Editor information

Editors and Affiliations

Appendices

Appendix

Fig. 11.
figure 11

Translation to system F\({^{\epsilon }}\)+\({\mathsf {{restrict}}}\)

Fig. 12.
figure 12

Types of system F\({^{\epsilon }}\)+\({\mathsf {{restrict}}}\)

Fig. 13.
figure 13

Expressions and operational semantics of F\({^{\epsilon }}\)+\({\mathsf {{restrict}}}\)

Fig. 14.
figure 14

Typing rules of F\({^{\epsilon }}\)+\({\mathsf {{restrict}}}\)

In this appendix, we present an extension of System F\({^{\epsilon }}\), which we call System F\({^{\epsilon }}\) + \({\mathsf {{restrict}}}\), and a type-directed translation from \({\mathsf {{ImpKoka}}}\) to System F\({^{\epsilon }}\) + \({\mathsf {{restrict}}}\). The translation allows us to prove the type soundness of \({\mathsf {{ImpKoka}}}\) without directly defining an operational semantics for \({\mathsf {{ImpKoka}}}\). This is a well-known technique, and is used in [10], for instance. The target calculus of the translation has a new construct \({\mathsf {{restrict}}}\), which is necessary for establishing soundness of the translation.

A System F\({^{\epsilon }}\) + \({\mathsf {{restrict}}}\)

In Figs. 12, 13 and 14, we define the syntax, operational semantics, and typing rules of System F \({^{\epsilon }}\) + \({\mathsf {{restrict}}}\). The typing rule \({{[\textsc {{restrict}}]}}\) extends the closed effect row \({\langle {} l _{1},.., l _ n \rangle {}}\) of the expression \({ e }\) to \({\langle {} l _1,{\dots } l _ n \mid {}{\epsilon }\rangle {}}\), where \({{\epsilon }}\) is an arbitrary effects.

We can prove the type soundness of System F\({^{\epsilon }}\) + \({\mathsf {{restrict}}}\) by showing the following theorems.

Theorem 13

(progress) \( \textrm{If }\, {\emptyset \vdash {}~ e _{1}~\,:\,{}~{\sigma }\mid {}\langle \rangle {}}\ \mathrm {then\ either}\ { e _{1}} \ \mathrm {is\ a\ value\ or}\ { e _{1}~{\longmapsto }~ e _{2}}.\)

Theorem 14

(preservation) \( \textrm{If }\, {\emptyset \vdash {}~ e _{1}~\,:\,{}~{\sigma }\mid {}\langle \rangle {}} \ \textrm{and}\ { e _{1}~{\longmapsto }~ e _{2}} \ \textrm{then}\ {\emptyset \vdash {}~ e _{2}~\,:\,{}~{\sigma }\mid {}\langle \rangle {}}.\)

B Type-Directed Translation to System F\({^{\epsilon }+\mathsf {{restrict}}}\)

In Fig. 11, we next define the type-directed translation from \({\mathsf {{ImpKoka}}}\) to System F\({^{\epsilon }}\) + \({\mathsf {{restrict}}}\). The judgment \({\varXi {}\mid {}\varGamma {}\mid {}\varDelta \vdash {}~ e ~\,:\,{}~{\sigma }\mid {}{\epsilon }~{\;\rightsquigarrow }\;~ e '}\) states that an expression \({ e }\) has type \({{\sigma }}\) and effect \({{\epsilon }}\) under the type variable context \({\varXi {}}\) and typing contexts \({\varGamma {}}\) and \({\varDelta }\), and translates to an expression \({ e '}\) of System F\({^{\epsilon }}\) + \({\mathsf {{restrict}}}\). We can easily prove the soundness of the type-directed translation.

Theorem 15

(soundness of type-directed translation)

\(\textrm{If}~{\varXi {}\mid {}\varGamma {}\mid {}\varDelta \vdash {}~ e ~\,:\,{}{\sigma }\mid {}}{{\epsilon }~{\;\rightsquigarrow }\;~ e '}\ \textrm{then}\ {\varGamma {},~\varDelta \vdash {}~ e '~\,:\,{}~{\sigma }\mid {}{\epsilon }}\).

Proof

By straightforward induction on the typing derivation.

Rights and permissions

Reprints and permissions

Copyright information

© 2022 Springer Nature Switzerland AG

About this paper

Check for updates. Verify currency and authenticity via CrossMark

Cite this paper

Ikemori, K., Cong, Y., Masuhara, H., Leijen, D. (2022). Sound and Complete Type Inference for Closed Effect Rows. In: Swierstra, W., Wu, N. (eds) Trends in Functional Programming. TFP 2022. Lecture Notes in Computer Science, vol 13401. Springer, Cham. https://doi.org/10.1007/978-3-031-21314-4_8

Download citation

  • DOI: https://doi.org/10.1007/978-3-031-21314-4_8

  • Published:

  • Publisher Name: Springer, Cham

  • Print ISBN: 978-3-031-21313-7

  • Online ISBN: 978-3-031-21314-4

  • eBook Packages: Computer ScienceComputer Science (R0)

Publish with us

Policies and ethics