skip to main content
research-article
Open access

Contextual Typing

Published: 15 August 2024 Publication History

Abstract

Bidirectional typing is a simple, lightweight approach to type inference that propagates known type information during typing, and can scale up to many different type systems and features. It typically only requires a reasonable amount of annotations and eliminates the need for many obvious annotations. Nonetheless the power of inference is still limited, and complications arise in the presence of more complex features. In this paper we present a generalization of bidirectional typing called contextual typing. In contextual typing not only known type information is propagated during typing, but also other known information about the surrounding context of a term. This information can be of various forms, such as other terms or record labels. Due to this richer notion of contextual information, less annotations are needed, while the approach remains lightweight and scalable. For type systems with subtyping, contextual typing subsumption is also more expressive than subsumption with bidirectional typing, since partially known contextual information can be exploited. To aid specifying type systems with contextual typing, we introduce Quantitative Type Assignment Systems (QTASs). A QTAS quantifies the amount of type information that a term needs in order to type check using counters. Thus, a counter in a QTAS generalizes modes in traditional bidirectional typing, which can only model an all (checking mode) or nothing (inference mode) approach. QTASs enable precise guidelines for annotatability of contextual type systems formalized as a theorem. We illustrate contextual typing first on a simply typed lambda calculus, and then on a richer calculus with subtyping, intersection types, records and overloading. All the metatheory is formalized in the Agda theorem prover.

References

[1]
Andrea Asperti, Wilmer Ricciotti, Claudio Sacerdoti Coen, and Enrico Tassi. 2012. A Bi-Directional Refinement Algorithm for the Calculus of (Co)Inductive Constructions. Log. Methods Comput. Sci., 8, 1 (2012), https://doi.org/10.2168/LMCS-8(1:18)2012
[2]
Henk Barendregt, Mario Coppo, and Mariangiola Dezani-Ciancaglini. 1983. A Filter Lambda Model and the Completeness of Type Assignment. J. Symb. Log., 48, 4 (1983), 931–940. https://doi.org/10.2307/2273659
[3]
Gavin M. Bierman, Martín Abadi, and Mads Torgersen. 2014. Understanding TypeScript. In ECOOP 2014 - Object-Oriented Programming - 28th European Conference, Uppsala, Sweden, July 28 - August 1, 2014. Proceedings, Richard E. Jones (Ed.) (Lecture Notes in Computer Science, Vol. 8586). Springer, 257–281. https://doi.org/10.1007/978-3-662-44202-9_11
[4]
Iliano Cervesato and Frank Pfenning. 2003. A Linear Spine Calculus. J. Log. Comput., 13, 5 (2003), 639–688. https://doi.org/10.1093/LOGCOM/13.5.639
[5]
Mario Coppo, Mariangiola Dezani-Ciancaglini, and Betti Venneri. 1981. Functional Characters of Solvable Terms. Math. Log. Q., 27, 2-6 (1981), 45–58. https://doi.org/10.1002/MALQ.19810270205
[6]
Thierry Coquand. 1996. An Algorithm for Type-Checking Dependent Types. Sci. Comput. Program., 26, 1-3 (1996), 167–177. https://doi.org/10.1016/0167-6423(95)00021-6
[7]
Rowan Davies and Frank Pfenning. 2000. Intersection types and computational effects. In Proceedings of the Fifth ACM SIGPLAN International Conference on Functional Programming (ICFP ’00), Montreal, Canada, September 18-21, 2000, Martin Odersky and Philip Wadler (Eds.). ACM, 198–208. https://doi.org/10.1145/351240.351259
[8]
Jana Dunfield and Neel Krishnaswami. 2022. Bidirectional Typing. ACM Comput. Surv., 54, 5 (2022), 98:1–98:38. https://doi.org/10.1145/3450952
[9]
Jana Dunfield and Neelakantan R. Krishnaswami. 2013. Complete and easy bidirectional typechecking for higher-rank polymorphism. In ACM SIGPLAN International Conference on Functional Programming, ICFP’13, Boston, MA, USA - September 25 - 27, 2013, Greg Morrisett and Tarmo Uustalu (Eds.). ACM, 429–442. https://doi.org/10.1145/2500365.2500582
[10]
Jana Dunfield and Frank Pfenning. 2004. Tridirectional typechecking. In Proceedings of the 31st ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, POPL 2004, Venice, Italy, January 14-16, 2004, Neil D. Jones and Xavier Leroy (Eds.). ACM, 281–292. https://doi.org/10.1145/964001.964025
[11]
Xuejing Huang, Jinxu Zhao, and Bruno C. d. S. Oliveira. 2021. Taming the Merge Operator. J. Funct. Program., 31 (2021), e28. https://doi.org/10.1017/S0956796821000186
[12]
Christopher Jenkins and Aaron Stump. 2018. Spine-local Type Inference. In Proceedings of the 30th Symposium on Implementation and Application of Functional Languages, IFL 2018, Lowell, MA, USA, September 5-7, 2018, Matteo Cimini and Jay McCarthy (Eds.). ACM, 37–48. https://doi.org/10.1145/3310232.3310233
[13]
Daan Leijen. 2008. HMF: simple type inference for first-class polymorphism. In Proceeding of the 13th ACM SIGPLAN international conference on Functional programming, ICFP 2008, Victoria, BC, Canada, September 20-28, 2008, James Hook and Peter Thiemann (Eds.). ACM, 283–294. https://doi.org/10.1145/1411204.1411245
[14]
Andres Löh, Conor McBride, and Wouter Swierstra. 2010. A Tutorial Implementation of a Dependently Typed Lambda Calculus. Fundam. Informaticae, 102, 2 (2010), 177–207. https://doi.org/10.3233/FI-2010-304
[15]
Henry Mercer, Cameron Ramsay, and Neel Krishnaswami. 2022. Implicit Polarized F: local type inference for impredicativity. CoRR, abs/2203.01835, https://doi.org/10.48550/ARXIV.2203.01835 arXiv:2203.01835.
[16]
Ulf Norell. 2007. Towards a practical programming language based on dependent type theory. 32, Chalmers University of Technology.
[17]
Martin Odersky, Christoph Zenger, and Matthias Zenger. 2001. Colored local type inference. In Conference Record of POPL 2001: The 28th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, London, UK, January 17-19, 2001, Chris Hankin and Dave Schmidt (Eds.). ACM, 41–53. https://doi.org/10.1145/360204.360207
[18]
Benjamin C. Pierce and David N. Turner. 2000. Local type inference. ACM Trans. Program. Lang. Syst., 22, 1 (2000), 1–44. https://doi.org/10.1145/345099.345100
[19]
Nadia Polikarpova, Ivan Kuraj, and Armando Solar-Lezama. 2016. Program synthesis from polymorphic refinement types. 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, 522–538. https://doi.org/10.1145/2908080.2908093
[20]
François Pottier and Yann Régis-Gianas. 2006. Stratified type inference for generalized algebraic data types. In Proceedings of the 33rd ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, POPL 2006, Charleston, South Carolina, USA, January 11-13, 2006, J. Gregory Morrisett and Simon L. Peyton Jones (Eds.). ACM, 232–244. https://doi.org/10.1145/1111037.1111058
[21]
Garrel Pottinger. 1980. A type assignment for the strongly normalizable λ -terms. To HB Curry: essays on combinatory logic, lambda calculus and formalism, 561–577.
[22]
John C. Reynolds. 1991. The Coherence of Languages with Intersection Types. In Theoretical Aspects of Computer Software, International Conference TACS ’91, Sendai, Japan, September 24-27, 1991, Proceedings, Takayasu Ito and Albert R. Meyer (Eds.) (Lecture Notes in Computer Science, Vol. 526). Springer, 675–700. https://doi.org/10.1007/3-540-54415-1_70
[23]
John C. Reynolds. 1997. Design of the Programming Language Forsythe. In Algol-like Languages, Peter W. O’Hearn and Robert D. Tennent (Eds.). Birkhäuser Boston, Boston, MA. 173–233. isbn:978-1-4612-4118-8 https://doi.org/10.1007/978-1-4612-4118-8_9
[24]
Nick Rioux, Xuejing Huang, Bruno C. d. S. Oliveira, and Steve Zdancewic. 2023. A Bowtie for a Beast: Overloading, Eta Expansion, and Extensible Data Types in F⋈. Proc. ACM Program. Lang., 7, POPL (2023), 515–543. https://doi.org/10.1145/3571211
[25]
Alejandro Serrano, Jurriaan Hage, Simon Peyton Jones, and Dimitrios Vytiniotis. 2020. A quick look at impredicativity. Proc. ACM Program. Lang., 4, ICFP (2020), 89:1–89:29. https://doi.org/10.1145/3408971
[26]
Alejandro Serrano, Jurriaan Hage, Dimitrios Vytiniotis, and Simon Peyton Jones. 2018. Guarded impredicative polymorphism. In Proceedings of the 39th ACM SIGPLAN Conference on Programming Language Design and Implementation, PLDI 2018, Philadelphia, PA, USA, June 18-22, 2018, Jeffrey S. Foster and Dan Grossman (Eds.). ACM, 783–796. https://doi.org/10.1145/3192366.3192389
[27]
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
[28]
Ningning Xie and Bruno C. d. S. Oliveira. 2018. Let Arguments Go First. In Programming Languages and Systems - 27th European Symposium on Programming, ESOP 2018, Held as Part of the European Joint Conferences on Theory and Practice of Software, ETAPS 2018, Thessaloniki, Greece, April 14-20, 2018, Proceedings, Amal Ahmed (Ed.) (Lecture Notes in Computer Science, Vol. 10801). Springer, 272–299. https://doi.org/10.1007/978-3-319-89884-1_10
[29]
Xu Xue and Bruno C. d. S. Oliveira. 2024. Contextual Typing (Artifact). Zenodo. https://doi.org/10.5281/zenodo.11429428
[30]
Xu Xue, Bruno C. d. S. Oliveira, and Ningning Xie. 2022. Applicative Intersection Types. In Programming Languages and Systems - 20th Asian Symposium, APLAS 2022, Auckland, New Zealand, December 5, 2022, Proceedings, Ilya Sergey (Ed.) (Lecture Notes in Computer Science, Vol. 13658). Springer, 155–174. https://doi.org/10.1007/978-3-031-21037-2_8
[31]
Jinxu Zhao, Bruno C. d. S. Oliveira, and Tom Schrijvers. 2019. A mechanical formalization of higher-ranked polymorphic type inference. Proc. ACM Program. Lang., 3, ICFP (2019), 112:1–112:29. https://doi.org/10.1145/3341716

Index Terms

  1. Contextual Typing

    Recommendations

    Comments

    Please enable JavaScript to view thecomments powered by Disqus.

    Information & Contributors

    Information

    Published In

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

    Publisher

    Association for Computing Machinery

    New York, NY, United States

    Publication History

    Published: 15 August 2024
    Published in PACMPL Volume 8, Issue ICFP

    Permissions

    Request permissions for this article.

    Check for updates

    Badges

    Author Tags

    1. Bidirectional Typing
    2. Type Inference

    Qualifiers

    • Research-article

    Funding Sources

    • Hong Kong Research Grant Council

    Contributors

    Other Metrics

    Bibliometrics & Citations

    Bibliometrics

    Article Metrics

    • 0
      Total Citations
    • 198
      Total Downloads
    • Downloads (Last 12 months)198
    • Downloads (Last 6 weeks)73
    Reflects downloads up to 10 Nov 2024

    Other Metrics

    Citations

    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