{{short description|Programming paradigm}}
In computer science, '''choreographic programming''' is a programming paradigm for distributed systems, where programs are written as compositions of interactions among multiple concurrent participants.<ref name="itc">{{cite book |last1=Montesi |first1=Fabrizio |title=Introduction to Choreographies |date=2023 |publisher=Cambridge University Press |doi=10.1017/9781108981491 |isbn=978-1-108-83376-9 |s2cid=102335067 |url=https://doi.org/10.1017/9781108981491}}</ref><ref name="bt-survey">{{Cite journal|url=https://doi.org/10.1561/2500000031|doi = 10.1561/2500000031|title = Behavioral Types in Programming Languages|year = 2016|last1 = Yoshida|first1 = Nobuko|last2 = Vasconcelos|first2 = Vasco T.|last3 = Padovani|first3 = Luca|last4 = Bono|first4 = Nicholas Ng|last5 = Neykova|first5 = Rumyana|last6 = Montesi|first6 = Fabrizio|last7 = Mascardi|first7 = Viviana|last8 = Martins|first8 = Francisco|last9 = Johnsen|first9 = Einar Broch|last10 = Hu|first10 = Raymond|last11 = Giachino|first11 = Elena|last12 = Gesbert|first12 = Nils|last13 = Gay|first13 = Simon J.|last14 = Deniélou|first14 = Pierre-Malo|last15 = Castagna|first15 = Giuseppe|last16 = Campos|first16 = Joana|last17 = Bravetti|first17 = Mario|last18 = Bono|first18 = Viviana|last19 = Ancona|first19 = Davide|journal = Foundations and Trends in Programming Languages|volume = 3|issue = 2–3|pages = 95–230| hdl=10044/1/44282 |hdl-access = free}}</ref><ref name="mp-langs">{{Cite book|doi = 10.4230/LIPIcs.ECOOP.2021.22|year = 2021|last1 = Giallorenzo|first1 = Saverio|last2 = Montesi|first2 = Fabrizio|last3 = Peressotti|first3 = Marco|last4 = Richter|first4 = David|last5 = Salvaneschi|first5 = Guido|last6 = Weisenburger|first6 = Pascal|title = Multiparty Languages: The Choreographic and Multitier Cases (Pearl)|series = Leibniz International Proceedings in Informatics (LIPIcs)|volume = 194|pages = 22:1–22:27| doi-access=free |isbn = 9783959771900}} [https://2021.ecoop.org/details/ecoop-2021-ecoop-research-papers/9/Multiparty-Languages-the-Choreographic-and-Multitier-Cases (ECOOP 2021 Distinguished Paper)]</ref>
Deadlock is a common error that can occur in distributed systems. Choreographic programming ensures that deadlock ''cannot occur'' in the scope of the choreography by ensuring that every time a message is sent from one party, there is a corresponding receive on the other end.
== Overview ==
=== Choreographies === In choreographic programming, developers use a '''choreographic programming language''' to define the intended communication behaviour of concurrent participants. Programs in this paradigm are called '''choreographies'''.<ref name="itc"/> Choreographic languages are inspired by security protocol notation (also known as "Alice and Bob" notation). The key to these languages is the communication primitive, for example <syntaxhighlight lang="text"> Alice.expr -> Bob.x </syntaxhighlight> reads "<code>Alice</code> communicates the result of evaluating the expression <code>expr</code> to <code>Bob</code>, which stores it in its local variable <code>x</code>".<ref name="mp-langs"/> Alice, Bob, etc. are typically called '''roles''' or '''processes'''.<ref name="bt-survey"/>
The example below shows a choreography for a simplified single sign-on (SSO) protocol based on a Central Authentication Service (CAS) that involves three roles: * <code>Client</code>, which wishes to obtain an access token from <code>CAS</code> to interact with <code>Service</code>. * <code>Service</code>, which needs to know from <code>CAS</code> if the <code>Client</code> should be given access. * <code>CAS</code>, which is the Central Authentication Service responsible for checking the <code>Client</code>'s credentials.
The choreography is: <syntaxhighlight lang="text" line> Client.(credentials, serviceID) -> CAS.authRequest if CAS.check(authRequest) then CAS.token = genToken(authRequest) CAS.Success(token) -> Client.result CAS.Success(token) -> Service.result else CAS.Failure -> Client.result CAS.Failure -> Service.result </syntaxhighlight>
The choreography starts in Line 1, where <code>Client</code> communicates a pair consisting of some credentials and the identifier of the service it wishes to access to <code>CAS</code>. <code>CAS</code> stores this pair in its local variable <code>authRequest</code> (for authentication request). In Line 2, the <code>CAS</code> checks if the request is valid for obtaining an authentication token. If so, it generates a token and communicates a <code>Success</code> message containing the token to both <code>Client</code> and <code>Service</code> (Lines 3–5). Otherwise, the <code>CAS</code> informs <code>Client</code> and <code>Service</code> that authentication failed, by sending a <code>Failure</code> message (Lines 7–8). We refer to this choreography as the "SSO choreography" in the remainder.
=== Endpoint Projection === A key feature of choreographic programming is the capability of compiling choreographies to distributed implementations. These implementations can be libraries for software that needs to participate in a computer network by following a protocol,<ref name="itc"/><ref name="mp-langs" /><ref>[https://www.choral-lang.org/ Choral programming language]</ref> or standalone distributed programs.<ref name=":0" /><ref name="aiocj-paper" />
The translation of a choreography into distributed programs is called '''endpoint projection''' (EPP for short).<ref name="bt-survey" /><ref name="mp-langs" />
Endpoint projection returns a program for each role described in the source choreography.<ref name="mp-langs" /> For example, given the choreography above, endpoint projection would return three programs: one for <code>Client</code>, one for <code>Service</code>, and one for <code>CAS</code>. They are shown below in pseudocode form, where <code>send</code> and <code>recv</code> are primitives for sending and receiving messages to/from other roles. {{aligned table|class=wikitable|cols=2|col1header=y|rowstyle=vertical-align:middle |title=Endpoint Projection (EPP) of the SSO choreography |1=Client |3=Service |5=CAS |2=<syntaxhighlight lang="text" line> send (credentials, serviceID) to CAS recv result from CAS </syntaxhighlight> |4=<syntaxhighlight lang="text" line> recv result from CAS </syntaxhighlight> |6=<syntaxhighlight lang="text" line> recv authRequest from Client if check(authRequest) then token = genToken(authRequest) send Success(token) to Client send Success(token) to Service else send Failure to Client send Failure to Service </syntaxhighlight> }} For each role, its code contains the actions that the role should execute to implement the choreography correctly together with the others.
== Development == The paradigm of choreographic programming originates from its titular PhD thesis.<ref name="M13:phd">{{cite thesis |type=PhD |last=Montesi |first=Fabrizio |date=2013 |title=Choreographic Programming |url=https://www.fabriziomontesi.com/files/choreographic_programming.pdf |location= |publisher=IT University of Copenhagen |isbn=978-87-7949-299-8}} [https://eapls.org/items/1855/ (EAPLS Best PhD Dissertation Award)]</ref><ref name="GH21">{{cite journal |last1=Hirsch |first1=Andrew K. |last2=Garg |first2=Deepak |title=Pirouette: higher-order typed functional choreographies |journal=Proceedings of the ACM on Programming Languages |date=16 January 2022 |volume=6 |issue=POPL |pages=1–27 |doi=10.1145/3498684|s2cid=243833095 |doi-access=free |arxiv=2111.03484 }} [https://popl22.sigplan.org/details/POPL-2022-popl-research-papers/23/Pirouette-Higher-Order-Typed-Functional-Choreographies (POPL 2022 Distinguished Paper)]</ref><ref>{{cite web |url=https://eapls.org/items/1855/ |title=Fabrizio Montesi wins the EAPLS Best PhD Dissertation Award 2014 |author=Arend Rensink |date=2015-08-30 |publisher=European Association for Programming Languages and Systems}}</ref> The inspiration for the syntax of choreographic programming languages can be traced back to security protocol notation, also known as "Alice and Bob" notation.<ref name="itc"/> Choreographic programming has also been heavily influenced by standards for service choreography and interaction diagrams, as well as developments of the theory of process calculi.<ref name="itc"/><ref name="mp-langs"/><ref>{{Cite journal|doi=10.1145/2220365.2220367|title=Structured Communication-Centered Programming for Web Services|year=2012|last1=Carbone|first1=Marco|last2=Honda|first2=Kohei|last3=Yoshida|first3=Nobuko|journal=ACM Transactions on Programming Languages and Systems|volume=34|issue=2|pages=1–78|s2cid=15737118|doi-access=free}}</ref>
Choreographic programming is an active area of research. The paradigm has been used in the study of information flow,<ref>{{Cite book|chapter-url=https://doi.org/10.1007/978-3-319-23165-5_20|doi = 10.1007/978-3-319-23165-5_20|chapter = Discretionary Information Flow Control for Interaction-Oriented Specifications|title = Logic, Rewriting, and Concurrency|series = Lecture Notes in Computer Science|year = 2015|last1 = Lluch Lafuente|first1 = Alberto|last2 = Nielson|first2 = Flemming|last3 = Nielson|first3 = Hanne Riis|author3-link=Hanne Riis Nielson|volume = 9200|pages = 427–450|isbn = 978-3-319-23164-8| s2cid=32617923 |url = https://backend.orbit.dtu.dk/ws/files/119987994/Discretionary_Information_Flow_Control_for_Interaction_Oriented_Specifications.pdf}}</ref> parallel computing,<ref>{{Cite book|chapter-url=https://doi.org/10.1007/978-3-319-39570-8_8|doi = 10.1007/978-3-319-39570-8_8|chapter = Choreographies in Practice|title = Formal Techniques for Distributed Objects, Components, and Systems|series = Lecture Notes in Computer Science|year = 2016|last1 = Cruz-Filipe|first1 = Luís|last2 = Montesi|first2 = Fabrizio|volume = 9688|pages = 114–123|arxiv = 1602.08863|isbn = 978-3-319-39569-2|s2cid = 18067252}}</ref> cyber-physical systems,<ref>{{Cite book|chapter-url=https://doi.org/10.1145/3019612.3019656|doi=10.1145/3019612.3019656|chapter=Choreographing cyber-physical distributed control systems for the energy sector|title=Proceedings of the Symposium on Applied Computing|year=2017|last1=López|first1=Hugo A.|last2=Heussen|first2=Kai|pages=437–443|isbn=9781450344869|s2cid=39112346}}</ref><ref>{{Cite book|chapter-url=https://inria.hal.science/hal-01432918|doi = 10.1007/978-3-319-39570-8_13|chapter = Enforcing Availability in Failure-Aware Communicating Systems|title = Formal Techniques for Distributed Objects, Components, and Systems|series = Lecture Notes in Computer Science|year = 2016|last1 = López|first1 = Hugo A.|last2 = Nielson|first2 = Flemming|last3 = Nielson|first3 = Hanne Riis|author3-link=Hanne Riis Nielson|volume = 9688|pages = 195–211|isbn = 978-3-319-39569-2| s2cid=12872876 }}</ref> runtime adaptation,<ref name="aiocj-paper">{{Cite journal|url=https://doi.org/10.23638/LMCS-13(2:1)2017|doi = 10.23638/LMCS-13(2:1)2017|year = 2017|last1 = Preda|first1 = Mila Dalla|last2 = Gabbrielli|first2 = Maurizio|last3 = Giallorenzo|first3 = Saverio|last4 = Lanese|first4 = Ivan|last5 = Mauro|first5 = Jacopo|title = Dynamic Choreographies: Theory and Implementation|journal = Logical Methods in Computer Science |volume = 13|issue = 2| article-number=3263 |s2cid = 5555662|arxiv = 1611.09067}}</ref> and system integration.<ref>{{Cite book|chapter-url=https://doi.org/10.1007/978-3-030-02671-4_2|doi = 10.1007/978-3-030-02671-4_2|chapter = ChIP: A Choreographic Integration Process|title = On the Move to Meaningful Internet Systems. OTM 2018 Conferences|series = Lecture Notes in Computer Science|year = 2018|last1 = Giallorenzo|first1 = Saverio|last2 = Lanese|first2 = Ivan|last3 = Russo|first3 = Daniel|volume = 11230|pages = 22–40|isbn = 978-3-030-02670-7| s2cid=53015580 |url = https://findresearcher.sdu.dk:8443/ws/files/153104697/ChIP.pdf}}</ref>
== Languages == * AIOCJ ([https://www.cs.unibo.it/projects/jolie/aiocj.html website]).<ref name="aiocj-paper"/> A choreographic programming language for adaptable systems that produces code in Jolie. * Chor.<ref name=":0">{{Cite book|chapter-url=https://doi.org/10.1145/2429069.2429101|doi=10.1145/2429069.2429101|chapter=Deadlock-freedom-by-design|title=Proceedings of the 40th annual ACM SIGPLAN-SIGACT symposium on Principles of programming languages - POPL '13|year=2013|last1=Carbone|first1=Marco|last2=Montesi|first2=Fabrizio|page=263|isbn=9781450318327|s2cid=15627190}}</ref> A session-typed choreographic programming language that compiled to microservices in Jolie. In the meantime superseded by Choral. * Choral ([https://www.choral-lang.org/ website]).<ref>{{Cite journal |last1=Giallorenzo |first1=Saverio |last2=Montesi |first2=Fabrizio |last3=Peressotti |first3=Marco |date=2024-01-16 |title=Choral: Object-oriented Choreographic Programming |url=https://dl.acm.org/doi/10.1145/3632398 |journal=ACM Trans. Program. Lang. Syst. |volume=46 |issue=1 |pages=1:1–1:59 |doi=10.1145/3632398 |issn=0164-0925|arxiv=2005.09520 }}</ref><ref>{{cite arXiv |last1=Giallorenzo |first1=Saverio |title=Choral: Object-Oriented Choreographic Programming |date=2023-10-19 |eprint=2005.09520 |last2=Montesi |first2=Fabrizio |last3=Peressotti |first3=Marco|class=cs.PL }}</ref> An object-oriented choreographic programming language that compiles to libraries in Java. Choral is the first choreographic programming language with decentralised data structures and higher-order parameters. * Chorex ([https://github.com/utahplt/chorex website]). An Elixir library providing an embedded choreographic programming language through Elixir's macro system. * ChoRus ([https://lsd-ucsc.github.io/ChoRus/ website]). Library-level choreographic programming in Rust. * Core Choreographies.<ref>{{Cite journal|url=https://doi.org/10.1016/j.tcs.2019.07.005|doi = 10.1016/j.tcs.2019.07.005|title = A core model for choreographic programming|year = 2020|last1 = Cruz-Filipe|first1 = Luís|last2 = Montesi|first2 = Fabrizio|journal = Theoretical Computer Science|volume = 802|pages = 38–66|s2cid = 199122777|arxiv = 1510.03271}}</ref> A core theoretical model for choreographic programming. A mechanised implementation is available in Rocq.<ref>{{Cite book|doi=10.4230/LIPIcs.ITP.2021.15|isbn=9783959771887|title=Formalising a Turing-Complete Choreographic Language in Coq|series=Leibniz International Proceedings in Informatics (LIPIcs)|year=2021|volume=193|pages=15:1–15:18|last1=Cohen|first1=Liron|last2=Kaliszyk|first2=Cezary|doi-access=free |s2cid=231802115}}</ref><ref>{{Cite journal |last1=Cruz-Filipe |first1=Luís |last2=Montesi |first2=Fabrizio |last3=Peressotti |first3=Marco |date=2023-05-27 |title=A Formal Theory of Choreographic Programming |journal=Journal of Automated Reasoning |language=en |volume=67 |issue=2 |pages=21 |doi=10.1007/s10817-023-09665-3 |s2cid=252090305 |issn=1573-0670|doi-access=free |arxiv=2209.01886 }}</ref><ref>{{cite book |last1=Cruz-Filipe |first1=Luís |title=Certifying Choreography Compilation |date=2021 |url=https://link.springer.com/10.1007/978-3-030-85315-0_8 |work=Theoretical Aspects of Computing – ICTAC 2021 |volume=12819 |pages=115–133 |editor-last=Cerone |editor-first=Antonio |place=Cham |publisher=Springer International Publishing |language=en |doi=10.1007/978-3-030-85315-0_8 |isbn=978-3-030-85314-3 |access-date=2022-03-07 |last2=Montesi |first2=Fabrizio |last3=Peressotti |first3=Marco |series=Lecture Notes in Computer Science |arxiv=2102.10698 |s2cid=231985665 |editor2-last=Ölveczky |editor2-first=Peter Csaba}}</ref> * HasChor ([https://github.com/gshen42/HasChor website]).<ref>{{Cite journal |last1=Shen |first1=Gan |last2=Kashiwa |first2=Shun |last3=Kuper |first3=Lindsey |title=HasChor: Functional Choreographic Programming for All (Functional Pearl) |journal=Proceedings of the ACM on Programming Languages |date=2023-08-31 |volume=7 |url=https://doi.org/10.1145/3607849 |pages= 541–565|doi=10.1145/3607849|arxiv=2303.00924 }}</ref> A library for choreographic programming in Haskell. * Kalas.<ref>{{Cite journal |last1=Pohjola |first1=Johannes Åman |last2=Gómez-Londoño |first2=Alejandro |last3=Shaker |first3=James |last4=Norrish |first4=Michael |date=2022 |editor-last=Andronick |editor-first=June |editor2-last=de Moura |editor2-first=Leonardo |title=Kalas: A Verified, End-To-End Compiler for a Choreographic Language |url=https://drops.dagstuhl.de/opus/volltexte/2022/16736 |journal=13th International Conference on Interactive Theorem Proving (ITP 2022) |series=Leibniz International Proceedings in Informatics (LIPIcs) |location=Dagstuhl, Germany |publisher=Schloss Dagstuhl – Leibniz-Zentrum für Informatik |volume=237 |pages=27:1–27:18 |doi=10.4230/LIPIcs.ITP.2022.27 |doi-access=free |isbn=978-3-95977-252-5|s2cid=251322644 }}</ref> A choreographic programming language with a verified compiler to CakeML. * Pirouette.<ref name="GH21"/> A mechanised choreographic programming language theory with higher-order procedures. * Klor ([https://github.com/lovrosdu/klor website]). Library-level choreographic programming in Clojure. * Tempo ([https://github.com/tempo-lang/tempo website]). A practical choreographic programming language that compiles to library source code for multiple target languages.
== See also == * Security protocol notation * Sequence diagram * Service choreography * Structured concurrency * Multitier programming
== References == {{Reflist}}
== External links == * [https://www.choral-lang.org/ www.choral-lang.org]
{{Programming paradigms navbox}}
Category:Concurrent computing Category:Programming paradigms