Abstract
Fluent API is an object-oriented pattern for elegant APIs and embedded DSLs. A smart fluent API can enforce the API protocol or DSL syntax at compile time. Since fluent API implementations typically rely on overloading function names, they are hard to realize in functional programming languages. This work shows how functional fluent APIs can be implemented in the absence of name overloading, by relying on parametric polymorphism and Hindley-Milner type inference. The implementation supports fluent API protocols in the regular- and deterministic context-free language classes, and even beyond.
- Alfred V. Aho and Jeffrey D. Ullman. 1977. Principles of Compiler Design (Addison-Wesley Series in Computer Science and Information Processing). Addison-Wesley Longman Publishing Co., Inc., USA. isbn:0201000229
Google Scholar
- Jonathan Aldrich, Joshua Sunshine, Darpan Saini, and Zachary Sparks. 2009. Typestate-Oriented Programming. In Proceedings of the 24th ACM SIGPLAN Conference Companion on Object Oriented Programming Systems Languages and Applications (OOPSLA ’09). Association for Computing Machinery, New York, NY, USA. 1015–1022. isbn:9781605587684 https://doi.org/10.1145/1639950.1640073
Google Scholar
Digital Library
- Lennart Augustsson, Howard Mansell, and Ganesh Sittampalam. 2008. Paradise: A Two-Stage DSL Embedded in Haskell. In Proceedings of the 13th ACM SIGPLAN International Conference on Functional Programming (ICFP ’08). Association for Computing Machinery, New York, NY, USA. 225–228. isbn:9781595939197 https://doi.org/10.1145/1411204.1411236
Google Scholar
Digital Library
- Eric Bodden and Laurie Hendren. 2012. The Clara Framework for Hybrid Typestate Analysis. Int. J. Softw. Tools Technol. Transf., 14, 3 (2012), June, 307–326. issn:1433-2779
Google Scholar
Digital Library
- François Bourdoncle and Stephan Merz. 1997. Type Checking Higher-Order Polymorphic Multi-Methods. In Proceedings of the 24th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL ’97). Association for Computing Machinery, New York, NY, USA. 302–315. isbn:0897918533 https://doi.org/10.1145/263699.263743
Google Scholar
Digital Library
- Jan Bracker and Andy Gill. 2014. Sunroof: A Monadic DSL for Generating JavaScript. In Practical Aspects of Declarative Languages, Matthew Flatt and Hai-Feng Guo (Eds.). Springer International Publishing, Cham. 65–80. isbn:978-3-319-04132-2
Google Scholar
- Cristiano Calcagno, Walid Taha, Liwen Huang, and Xavier Leroy. 2003. Implementing Multi-stage Languages Using ASTs, Gensym, and Reflection. In Generative Programming and Component Engineering, Frank Pfenning and Yannis Smaragdakis (Eds.). Springer Berlin Heidelberg, Berlin, Heidelberg. 57–76. isbn:978-3-540-39815-8
Google Scholar
Digital Library
- Krzysztof Czarnecki, John T. O’Donnell, Jörg Striegnitz, and Walid Taha. 2004. DSL Implementation in MetaOCaml, Template Haskell, and C++. Springer Berlin Heidelberg, Berlin, Heidelberg. 51–72. isbn:978-3-540-25935-0 https://doi.org/10.1007/978-3-540-25935-0_4
Google Scholar
Cross Ref
- Markus Degen, Peter Thiemann, and Stefan Wehr. 2007. Tracking Linear and Affine Resources with Java(X). In ECOOP 2007 – Object-Oriented Programming, Erik Ernst (Ed.). Springer Berlin Heidelberg, Berlin, Heidelberg. 550–574. isbn:978-3-540-73589-2
Google Scholar
Cross Ref
- Robert DeLine and Manuel Fähndrich. 2001. Enforcing High-Level Protocols in Low-Level Software. In Proceedings of the ACM SIGPLAN 2001 Conference on Programming Language Design and Implementation (PLDI ’01). Association for Computing Machinery, New York, NY, USA. 59–69. isbn:1581134142 https://doi.org/10.1145/378795.378811
Google Scholar
Digital Library
- Sebastian Erdweg, Tillmann Rendel, Christian Kästner, and Klaus Ostermann. 2011. SugarJ: Library-Based Syntactic Language Extensibility. In Proceedings of the 2011 ACM International Conference on Object Oriented Programming Systems Languages and Applications (OOPSLA ’11). Association for Computing Machinery, New York, NY, USA. 391–406. isbn:9781450309400 https://doi.org/10.1145/2048066.2048099
Google Scholar
Digital Library
- Sebastian Erdweg, Tijs van der Storm, Markus Völter, Meinte Boersma, Remi Bosman, William R. Cook, Albert Gerritsen, Angelo Hulshout, Steven Kelly, Alex Loh, Gabriël D. P. Konat, Pedro J. Molina, Martin Palatnik, Risto Pohjonen, Eugen Schindler, Klemens Schindler, Riccardo Solmi, Vlad A. Vergu, Eelco Visser, Kevin van der Vlist, Guido H. Wachsmuth, and Jimi van der Woning. 2013. The State of the Art in Language Workbenches. In Software Language Engineering, Martin Erwig, Richard F. Paige, and Eric Van Wyk (Eds.). Springer International Publishing, Cham, Switzerland. 197–217. isbn:978-3-319-02654-1
Google Scholar
- Moritz Eysholdt and Heiko Behrens. 2010. Xtext: Implement Your Language Faster than the Quick and Dirty Way. In Proceedings of the ACM International Conference Companion on Object Oriented Programming Systems Languages and Applications Companion (OOPSLA ’10). Association for Computing Machinery, New York, NY, USA. 307–309. isbn:9781450302401 https://doi.org/10.1145/1869542.1869625
Google Scholar
Digital Library
- Matthias Felleisen, Robert Bruce Findler, Matthew Flatt, Shriram Krishnamurthi, Eli Barzilay, Jay McCarthy, and Sam Tobin-Hochstadt. 2015. The Racket Manifesto. In 1st Summit on Advances in Programming Languages (SNAPL 2015), Thomas Ball, Rastislav Bodik, Shriram Krishnamurthi, Benjamin S. Lerner, and Greg Morrisett (Eds.) (Leibniz International Proceedings in Informatics (LIPIcs), Vol. 32). Schloss Dagstuhl–Leibniz-Zentrum fuer Informatik, Dagstuhl, Germany. 113–128. isbn:978-3-939897-80-4 issn:1868-8969 https://doi.org/10.4230/LIPIcs.SNAPL.2015.113
Google Scholar
Cross Ref
- Kostas Ferles, Jon Stephens, and Isil Dillig. 2021. Verifying Correct Usage of Context-Free API Protocols. Proc. ACM Program. Lang., 5, POPL (2021), Article 17, Jan., 30 pages. https://doi.org/10.1145/3434298
Google Scholar
Digital Library
- John Field, Deepak Goyal, G. Ramalingam, and Eran Yahav. 2003. Typestate Verification: Abstraction Techniques and Complexity Results. In Static Analysis, Radhia Cousot (Ed.). Springer Berlin Heidelberg, Berlin, Heidelberg. 439–462. isbn:978-3-540-44898-3
Google Scholar
- Stephen J. Fink, Eran Yahav, Nurit Dor, G. Ramalingam, and Emmanuel Geay. 2008. Effective Typestate Verification in the Presence of Aliasing. ACM Trans. Softw. Eng. Methodol., 17, 2 (2008), Article 9, May, 34 pages. issn:1049-331X https://doi.org/10.1145/1348250.1348255
Google Scholar
Digital Library
- Gert Florijn. 1995. Object Protocols as Functional Parsers. In ECOOP’95 — Object-Oriented Programming, 9th European Conference, Åarhus, Denmark, August 7–11, 1995, Mario Tokoro and Remo Pareschi (Eds.). Springer Berlin Heidelberg, Berlin, Heidelberg. 351–373. isbn:978-3-540-49538-3
Google Scholar
- The Apache Software Foundation. 2023. Domain-Specific Languages. http://docs.groovy-lang.org/docs/latest/html/documentation/core-domain-specific-languages.html
Google Scholar
- Martin Fowler. 2005. FluentInterface. https://www.martinfowler.com/bliki/FluentInterface.html
Google Scholar
- Martin Fowler. 2005. Language Workbenches: The Killer-App for Domain Specific Languages? https://martinfowler.com/articles/languageWorkbench.html
Google Scholar
- Ronald Garcia, Éric Tanter, Roger Wolff, and Jonathan Aldrich. 2014. Foundations of Typestate-Oriented Programming. ACM Trans. Program. Lang. Syst., 36, 4 (2014), Article 12, Oct., 44 pages. issn:0164-0925 https://doi.org/10.1145/2629609
Google Scholar
Digital Library
- Yossi Gil and Tomer Levy. 2016. Formal language recognition with the Java type checker. In 30 Europ. Conf OO Prog. (ECOOP 2016), Shriram Krishnamurthi and Benjamin S. Lerner (Eds.) (Leibniz International Proceedings in Inf. (LIPIcs), Vol. 56). Schloss Dagstuhl–Leibniz-Zentrum fuer Informatik, Dagstuhl, Germany. 10:1–10:27. isbn:978-3-95977-014-9 issn:1868-8969 https://doi.org/10.4230/LIPIcs.ECOOP.2016.10
Google Scholar
Cross Ref
- Yossi Gil and Ori Roth. 2019. Fling—a fluent API generator. In 33 Europ. Conf OO Prog. (ECOOP 2019), Alastair F. Donaldson (Ed.) (Leibniz International Proceedings in Inf. (LIPIcs), Vol. 134). Schloss Dagstuhl–Leibniz-Zentrum fuer Informatik, Dagstuhl, Germany. 13:1–13:25. isbn:978-3-95977-111-5 issn:1868-8969 https://doi.org/10.4230/LIPIcs.ECOOP.2019.13
Google Scholar
Cross Ref
- Andy Gill. 2014. Domain-Specific Languages and Code Synthesis Using Haskell. Commun. ACM, 57, 6 (2014), June, 42–49. issn:0001-0782 https://doi.org/10.1145/2605205
Google Scholar
Digital Library
- Robert Harper, David MacQueen, and Robin Milner. 1986. Standard ML. Department of Computer Science, University of Edinburgh, The King’s Buildings, Edinburgh.
Google Scholar
- John E. Hopcroft, Rajeev Motwani, and Jeffrey D. Ullman. 2007. Introduction to automata theory, languages, and computation (3 ed.). Pearson Addison Wesley, Boston, MA. isbn:0321455371
Google Scholar
- Paul Hudak. 1998. Modular domain specific languages and tools. In Proceedings. Fifth International Conference on Software Reuse (Cat. No.98TB100203). IEEE Computer Society, USA. 134–142. https://doi.org/10.1109/ICSR.1998.685738
Google Scholar
Cross Ref
- Graham Hutton and Erik Meijer. 1998. Monadic parsing in Haskell. Journal of Functional Programming, 8, 4 (1998), 437–444. https://doi.org/10.1017/S0956796898003050
Google Scholar
Digital Library
- Samuel N. Kamin and David Hyatt. 1997. A Special-Purpose Language for Picture-Drawing. In Proceedings of the Conference on Domain-Specific Languages on Conference on Domain-Specific Languages (DSL), 1997 (DSL’97). USENIX Association, USA. 23.
Google Scholar
- Lennart C.L. Kats and Eelco Visser. 2010. The Spoofax Language Workbench: Rules for Declarative Specification of Languages and IDEs. In Proceedings of the ACM International Conference on Object Oriented Programming Systems Languages and Applications (OOPSLA ’10). Association for Computing Machinery, New York, NY, USA. 444–463. isbn:9781450302036 https://doi.org/10.1145/1869459.1869497
Google Scholar
Digital Library
- Steven Kelly, Kalle Lyytinen, and Matti Rossi. 1996. MetaEdit+ A fully configurable multi-user and multi-tool CASE and CAME environment. In Advanced Information Systems Engineering, Panos Constantopoulos, John Mylopoulos, and Yannis Vassiliou (Eds.). Springer Berlin Heidelberg, Berlin, Heidelberg. 1–21. isbn:978-3-540-68451-0
Google Scholar
- Andrew Kennedy. 1994. Dimension Types. In Programming Languages and Systems — ESOP ’94, Donald Sannella (Ed.). Springer Berlin Heidelberg, Berlin, Heidelberg. 348–362. isbn:978-3-540-48376-2
Google Scholar
- Oleg Kiselyov. 2014. The Design and Implementation of BER MetaOCaml. In Functional and Logic Programming, Michael Codish and Eijiro Sumii (Eds.). Springer International Publishing, Cham. 86–102. isbn:978-3-319-07151-0
Google Scholar
- Viktor Kuncak, Patrick Lam, and Martin Rinard. 2002. Role Analysis. In Proceedings of the 29th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL ’02). Association for Computing Machinery, New York, NY, USA. 17–32. isbn:1581134509 https://doi.org/10.1145/503272.503276
Google Scholar
Digital Library
- Daan Leijen and Erik Meijer. 2000. Domain Specific Embedded Compilers. In Proceedings of the 2nd Conference on Domain-Specific Languages (DSL ’99). Association for Computing Machinery, New York, NY, USA. 109–122. isbn:1581132557 https://doi.org/10.1145/331960.331977
Google Scholar
Digital Library
- Daan Leijen and Erik Meijer. 2001. Parsec: Direct Style Monadic Parser Combinators for the Real World. Dept. of Computer Science, Universiteit Utrecht.
Google Scholar
- David MacQueen. 1984. Modules for Standard ML. In Proceedings of the 1984 ACM Symposium on LISP and Functional Programming (LFP ’84). Association for Computing Machinery, New York, NY, USA. 198–207. isbn:0897911423 https://doi.org/10.1145/800055.802036
Google Scholar
Digital Library
- Harry G. Mairson. 2004. Linear lambda calculus and PTIME-completeness. Journal of Functional Programming, 14, 6 (2004), 623–633. https://doi.org/10.1017/S0956796804005131
Google Scholar
Digital Library
- Tomoki Nakamaru, Kazuhiro Ichikawa, Tetsuro Yamazaki, and Shigeru Chiba. 2017. Silverchain: a fluent API generator. In Proc. 16 ACM SIGPLAN Int. Conf Generative Prog. (GPCE’17). ACM, Vancouver, BC, Canada. 199–211. isbn:978-1-4503-5524-7
Google Scholar
Digital Library
- Matthias Neubauer, Peter Thiemann, Martin Gasbichler, and Michael Sperber. 2002. Functional Logic Overloading. In Proceedings of the 29th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL ’02). Association for Computing Machinery, New York, NY, USA. 233–244. isbn:1581134509 https://doi.org/10.1145/503272.503294
Google Scholar
Digital Library
- Nathaniel Nystrom, Michael R. Clarkson, and Andrew C. Myers. 2003. Polyglot: An Extensible Compiler Framework for Java. In Compiler Construction, Görel Hedin (Ed.). Springer Berlin Heidelberg, Berlin, Heidelberg. 138–152. isbn:978-3-540-36579-2
Google Scholar
- M. Pradel, C. Jaspan, J. Aldrich, and T. R. Gross. 2012. Statically checking API protocol conformance with mined multi-object specifications. In 2012 34th International Conference on Software Engineering (ICSE 2012). IEEE Computer Society, Los Alamitos, CA, USA. 925–935. issn:0270-5257 https://doi.org/10.1109/ICSE.2012.6227127
Google Scholar
Cross Ref
- Ori Roth. 2023. Flunct: Functional Fluent API Generator. https://doi.org/10.5281/zenodo.7723110
Google Scholar
Digital Library
- Ori Roth and Yossi Gil. 2019. Fling - A Fluent API Generator (Artifact). Dagstuhl Artifacts Series, 5, 2 (2019), 12:1–12:9. issn:2509-8195 https://doi.org/10.4230/DARTS.5.2.12
Google Scholar
Cross Ref
- Ori Roth and Yossi Gil. 2022. Fluent APIs in Functional Languages (full version). https://doi.org/10.48550/ARXIV.2211.01473
Google Scholar
- Tim Sheard. 1999. Using MetaML: A Staged Programming Language. In Advanced Functional Programming, S. Doaitse Swierstra, José N. Oliveira, and Pedro R. Henriques (Eds.). Springer Berlin Heidelberg, Berlin, Heidelberg. 207–239. isbn:978-3-540-48506-3
Google Scholar
- Tim Sheard and Simon Peyton Jones. 2002. Template Meta-Programming for Haskell. In Proceedings of the 2002 ACM SIGPLAN Workshop on Haskell (Haskell ’02). Association for Computing Machinery, New York, NY, USA. 1–16. isbn:1581136056 https://doi.org/10.1145/581690.581691
Google Scholar
Digital Library
- Mark Shields and Simon Peyton Jones. 2001. Object-Oriented Style Overloading for Haskell (Extended Abstract). Electronic Notes in Theoretical Computer Science, 59, 1 (2001), 89–108. issn:1571-0661 https://doi.org/10.1016/S1571-0661(05)80455-4 BABEL’01, First International Workshop on Multi-Language Infrastructure and Interoperability (Satellite Event of PLI 2001)
Google Scholar
Cross Ref
- Robert E. Strom and Shaula Yemini. 1986. Typestate: A programming language concept for enhancing software reliability. IEEE Transactions on Software Engineering, SE-12, 1 (1986), 157–171. https://doi.org/10.1109/TSE.1986.6312929
Google Scholar
Digital Library
- Joshua Sunshine, Karl Naden, Sven Stork, Jonathan Aldrich, and Éric Tanter. 2011. First-Class State Change in Plaid. In Proceedings of the 2011 ACM International Conference on Object Oriented Programming Systems Languages and Applications (OOPSLA ’11). Association for Computing Machinery, New York, NY, USA. 713–732. isbn:9781450309400 https://doi.org/10.1145/2048066.2048122
Google Scholar
Digital Library
- Markus Voelter and Vaclav Pech. 2012. Language Modularity with the MPS Language Workbench. In Proceedings of the 34th International Conference on Software Engineering (ICSE ’12). IEEE Press, Los Alamitos, CA, USA. 1449–1450. isbn:9781467310673
Google Scholar
Digital Library
- Philip Wadler and Stephen Blott. 1989. How to Make Ad-Hoc Polymorphism Less Ad Hoc. In Proceedings of the 16th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL ’89). Association for Computing Machinery, New York, NY, USA. 60–76. isbn:0897912942 https://doi.org/10.1145/75277.75283
Google Scholar
Digital Library
- Hao Xu. 2010. EriLex: an embedded domain specific language generator. In Objects, Models, Components, Patterns, Jan Vitek (Ed.). Springer, Berlin, Heidelberg. 192–212. isbn:978-3-642-13953-6
Google Scholar
- Tetsuro Yamazaki, Tomoki Nakamaru, and Shigeru Chiba. 2022. Yet Another Generating Method of Fluent Interfaces Supporting Flat- and Sub-Chaining Styles. In Proceedings of the 15th ACM SIGPLAN International Conference on Software Language Engineering (SLE 2022). Association for Computing Machinery, New York, NY, USA. 249–259. isbn:9781450399197 https://doi.org/10.1145/3567512.3567533
Google Scholar
Digital Library
- Tetsuro Yamazaki, Tomoki Nakamaru, Kazuhiro Ichikawa, and Shigeru Chiba. 2019. Generating a Fluent API with Syntax Checking from an LR Grammar. Proc. ACM Program. Lang., 3, OOPSLA (2019), Article 134, Oct., 24 pages. https://doi.org/10.1145/3360560
Google Scholar
Digital Library
Index Terms
Fluent APIs in Functional Languages
Recommendations
Almost first-class language embedding: taming staged embedded DSLs
GPCE 2015: Proceedings of the 2015 ACM SIGPLAN International Conference on Generative Programming: Concepts and ExperiencesEmbedded domain-specific languages (EDSLs), inheriting a general-purpose language's features as well as look-and-feel, have traditionally been second-class or rather non-citizens in terms of host-language design. This makes sense when one regards them ...
Silverchain: a fluent API generator
GPCE '17This paper presents a tool named Silverchain, which generates class definitions for a fluent API from the grammar of the API. A fluent API is an API that is used by method chaining and its grammar is a BNF-like set of rules that defines method chains ...
Silverchain: a fluent API generator
GPCE 2017: Proceedings of the 16th ACM SIGPLAN International Conference on Generative Programming: Concepts and ExperiencesThis paper presents a tool named Silverchain, which generates class definitions for a fluent API from the grammar of the API. A fluent API is an API that is used by method chaining and its grammar is a BNF-like set of rules that defines method chains ...






Comments