<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="9788793519817.xsl"?>
<book id="home" xmlns:xlink="http://www.w3.org/1999/xlink">
<bookinfo>
<title>Behavioural Types: from Theory to Tools</title>
<affiliation><emphasis role="strong">Editors</emphasis></affiliation>
<authorgroup>
<author><firstname>Simon</firstname>
<surname>Gay</surname>
</author>
</authorgroup>
<affiliation>University of Glasgow, UK</affiliation>
<authorgroup>
<author><firstname>Ant&#x00F3;nio</firstname>
<surname>Ravara</surname>
</author>
</authorgroup>
<affiliation>Universidade Nova de Lisboa, Portugal</affiliation>
<publisher>
<publishername>River Publishers</publishername>
</publisher>
<isbn>9788793519817</isbn>
</bookinfo>
<preface class="preface" id="preface01">
<title>River Publishers Series in Automation, Control and Robotics</title>
<para><emphasis>Series Editors</emphasis></para>
<para><emphasis role="strong">SRIKANTA PATNAIK</emphasis><?lb?><emphasis>SOA University</emphasis><?lb?><emphasis>Bhubaneswar</emphasis><?lb?><emphasis>India</emphasis></para>
<para><emphasis role="strong">ISHWAR K. SETHI</emphasis><?lb?><emphasis>Oakland University</emphasis><?lb?><emphasis>USA</emphasis></para>
<para><emphasis role="strong">QUAN MIN ZHU</emphasis><?lb?><emphasis>University of the West of England</emphasis><?lb?><emphasis>UK</emphasis></para>
<para>Indexing: All books published in this series are submitted to Thomson Reuters Book Citation Index (BkCI), CrossRef and to Google Scholar.</para>
<para>The &#x0201C;River Publishers Series in Automation, Control and Robotics&#x0201D; is a series of comprehensive academic and professional books which focus on the theory and applications of automation, control and robotics. The series focuses on topics ranging from the theory and use of control systems, automation engineering, robotics and intelligent machines.</para>
<para>Books published in the series include research monographs, edited volumes, handbooks and textbooks. The books provide professionals, researchers, educators, and advanced students in the field with an invaluable insight into the latest research and developments.</para>
<para>Topics covered in the series include, but are by no means restricted to the following:</para>
<itemizedlist mark="bullet" spacing="normal">
<listitem><para>Robots and Intelligent Machines</para></listitem>
<listitem><para>Robotics</para></listitem>
<listitem><para>Control Systems</para></listitem>
<listitem><para>Control Theory</para></listitem>
<listitem><para>Automation Engineering</para></listitem>
</itemizedlist>
<para>For a list of other books in this series, visit www.riverpublishers.com</para>
</preface>
<preface class="preface" id="preface02">
<title>Preface</title>
<para>This book presents research produced by members of COST Action IC1201: Behavioural Types for Reliable Large-Scale Software Systems (BETTY), a European research network that was funded from October 2012 to October 2016. The technical theme of BETTY was the use of behavioural type systems in programming languages, to specify and verify properties of programs beyond the traditional use of type systems to describe data processing. A significant area within behavioural types is session types, which concerns the use of type-theoretic techniques to describe communication protocols so that static typechecking or dynamic monitoring can verify that protocols are implemented correctly. This is closely related to the topic of choreography, in which system design starts from a description of the overall communication flows. Another area is behavioural contracts, which describe the obligations of interacting agents in a way that enables blame to be attributed to the agent responsible for failed interaction. Type-theoretic techniques can also be used to analyse potential deadlocks due to cyclic dependencies between inter-process interactions.</para>
<para>BETTY was organised into four Working Groups: (1) Foundations; (2) Security; (3) Programming Languages; (4) Tools and Applications. Working Groups 1&#x2013;3 produced &#x0201C;state-of-the-art reports&#x0201D;, which originally intended to take snapshots of the field at the time the network started, but grew into substantial survey articles including much research carried out during the network [1&#x2013;3]. The situation for Working Group 4 was different. When the network started, the community had produced relatively few implementations of programming languages or tools. One of the aims of the network was to encourage more implementation work, and this was a great success. The community as a whole has developed a greater interest in putting theoretical ideas into practice. The sixteen chapters in this book describe systems that were either completely developed, or substantially extended, during BETTY. The total of 41 co-authors represents a significant proportion of the active participants in the network (around 120 people who attended at least one meeting). The book is a report on the new state of the art created by BETTY in the area of Working Group 4, and the title &#x0201C;Behavioural Types: from Theory to Tools&#x0201D; summarises the trajectory of the community during the last four years.</para>
<para>The book begins with two tutorials by Atzei <emphasis>et al.</emphasis> on contract-oriented design of distributed systems. Chapter 1 introduces the <emphasis role="italicAlt">CO</emphasis><subscript>2</subscript> contract specifi- cation language and the Diogenes toolchain. Chapter 2 describes how timing constraints can be incorporated into the framework and checked with the <emphasis role="italicAlt">CO</emphasis><subscript>2</subscript> middleware.</para>
<para>Part of the <emphasis role="italicAlt">CO</emphasis><subscript>2</subscript> middleware is a monitoring system, and the theme of monitoring continues in the next two chapters. In Chapter 3, Attard <emphasis>et al.</emphasis> present <literal>detectEr</literal>, a runtime monitoring tool for Erlang programs that allows correctness properties to be expressed in Hennessy-Milner logic. In Chapter 4, which is the first chapter about session types, Neykova and Yoshida describe a runtime verification framework for Python programs. Communication protocols are specified in the Scribble language, which is based on multiparty session types.</para>
<para>The next three chapters deal with choreographic programming. In Chapter 5, Debois and Hildebrandt present a toolset for working with dynamic condition response (DCR) graphs, which are a graphical formalism for choreography. Chapter 6, by Lange <emphasis>et al.</emphasis>, continues the graphical theme with <literal>ChorGram</literal>, a tool for synthesising global graphical choreographies from collections of communicating finite-state automata. Giallorenzo <emphasis>et al.</emphasis>, in Chapter 7, consider runtime adaptation. They describe AIOCJ, a choreographic programming language in which runtime adaptation is supported with a guarantee that it doesn&#x2019;t introduce deadlocks or races.</para>
<para>Deadlock analysis is important in other settings too, and there are two more chapters about it. In Chapter 8, Padovani describes the <literal>Hypha</literal> tool, which uses a type-based approach to check deadlock-freedom and lock-freedom of systems modelled in a form of pi-calculus. In Chapter 9, Garcia and Laneve present a tool for analysing deadlocks in Java programs; this tool, called <literal>JaDA</literal>, is based on a behavioural type system.</para>
<para>The next three chapters report on projects that have added session types to functional programming languages in order to support typechecking of communication-based code. In Chapter 10, Orchard and Yoshida describe an implementation of session types in Haskell, and survey several approaches to typechecking the linearity conditions required for safe session implemen- tation. In Chapter 11, Melgratti and Padovani describe an implementation of session types in OCaml. Their system uses runtime linearity checking. In Chapter 12, Lindley and Morris describe an extension of the web programming language Links with session types; their work contrasts with the previous two chapters in being less constrained by an existing language design.</para>
<para>Continuing the theme of session types in programming languages, the next two chapters describe two approaches based on Java. Hu&#x2019;s work, presented in Chapter 13, starts with the Scribble description of a multiparty session type and generates an API in the form of a collection of Java classes, each class containing the communication methods that are available in a particular state of the protocol. Dardha <emphasis>et al.</emphasis>, in Chapter 14, also start with a Scribble specification. Their StMungo tool generates an API as a single class with an associated typestate specification to constrain sequences of method calls. Code that uses the API can be checked for correctness with the Mungo typechecker.</para>
<para>Finally, there are two chapters about programming with the MPI libraries. Chapter 15, by Ng and Yoshida, uses an extension of Scribble, called Pabble, to describe protocols that parametric in the number of runtime roles. From a Pabble specification they generate C code that uses MPI for communication and is guaranteed correct by construction. Chapter 16, by Ng <emphasis>et al.</emphasis>, describes the <literal>ParTypes</literal> framework for analysing existing C+MPI programs with respect to protocols defined in an extension of Scribble.</para>
<para>We hope that the book will serve a useful purpose as a report on the activities of COST Action IC1201 and as a survey of programming languages and tools based on behavioural types.</para>
<blockquote>
<para>Simon Gay<?lb?>
Chair, COST Action IC1201</para>
<para>Anto&#x00F3; nio Ravara<?lb?>
Vice-Chair, COST Action IC1201</para>
</blockquote>
</preface>
<preface class="preface" id="preface03">
<title>Acknowledgments</title>
<para>COST is an EU-funded programme that enables researchers to set up their interdisciplinary research networks in Europe and beyond. We provide funds for organising conferences, meetings, training schools, short scientific exchanges or other networking activities in a wide range of scientific topics. By creating open spaces where people and ideas can grow, we unlock the full potential of science. <ulink url="http://www.cost.eu">www.cost.eu</ulink></para>
<para>This publication is based upon work from COST Action IC1201, supported by COST (European Cooperation in Science and Technology).</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fm1.jpg"/></para>
</preface>
<preface class="preface" id="preface04">
<title>List of Contributors</title>
<para><emphasis role="strong">Abel Garcia</emphasis>, <emphasis>Department of Computer Science and Engineering, University of Bologna &#x2013; INRIA FOCUS, Mura Anteo Zamboni 7, 40127, Bologna, Italy</emphasis></para>
<para><emphasis role="strong">Adrian Francalanza</emphasis>, <emphasis>Department of Computer Science, Faculty of ICT, University of Malta, Malta</emphasis></para>
<para><emphasis role="strong">Alessandro Sebastian Podda</emphasis>, <emphasis>University of Cagliari, Italy</emphasis></para>
<para><emphasis role="strong">Anna Ingo&#x00F3; lfsdo&#x00F3; ttir</emphasis>, <emphasis>School of Computer Science, Reykjav&#x00ED;k University, Iceland</emphasis></para>
<para><emphasis role="strong">Cosimo Laneve</emphasis>, <emphasis>Department of Computer Science and Engineering, Univer- sity of Bologna &#x2013; INRIA FOCUS, Mura Anteo Zamboni 7, 40127, Bologna, Italy</emphasis></para>
<para><emphasis role="strong">Dimitrios Kouzapas</emphasis>, <emphasis>School of Computing Science, University of Glasgow, UK</emphasis></para>
<para><emphasis role="strong">Dominic Orchard</emphasis>, <emphasis>University of Kent, UK</emphasis></para>
<para><emphasis role="strong">Duncan Paul Attard</emphasis>, <emphasis>Department of Computer Science, Faculty of ICT, University of Malta, Malta</emphasis></para>
<para><emphasis role="strong">Eduardo R. B. Marques</emphasis>, <emphasis>CRACS/INESC-TEC, Faculty of Sciences, Univer- sity of Porto, PT</emphasis></para>
<para><emphasis role="strong">Emilio Tuosto</emphasis>, <emphasis>University of Leicester, UK</emphasis></para>
<para><emphasis role="strong">Florian Weber</emphasis>, <emphasis>School of Computing Science, University of Glasgow, UK</emphasis> </para>
<para><emphasis role="strong">Francisco Martins</emphasis>, <emphasis>LaSIGE, Faculty of Sciences, University of Lisbon, PT</emphasis> </para>
<para><emphasis role="strong">Garrett Morris</emphasis>, <emphasis>University of Edinburgh, Edinburgh, UK</emphasis></para>
<para><emphasis role="strong">Hern&#x00E0;n Melgratti</emphasis>, <emphasis>Departamento de Computacio&#x00F3;n, Universidad de Buenos Aires, Argentina</emphasis> and <emphasis>CONICET-Universidad de Buenos Aires, Instituto de Investigacio&#x00F3; n en Ciencias de la Computacio&#x00F3; n (ICC), Buenos Aires, Argentina</emphasis></para>
<para><emphasis role="strong">Ian Cassar</emphasis>, <emphasis>Department of Computer Science, Faculty of ICT, University of Malta, Malta</emphasis></para>
<para><emphasis role="strong">Ivan Lanese</emphasis>, <emphasis>Focus Team, University of Bologna/INRIA, Italy</emphasis></para>
<para><emphasis role="strong">Jacopo Mauro</emphasis>, <emphasis>Department of Informatics, University of Oslo, Norway</emphasis></para>
<para><emphasis role="strong">Julien Lange</emphasis>, <emphasis>Imperial College London, UK</emphasis></para>
<para><emphasis role="strong">Laura Voinea</emphasis>, <emphasis>School of Computing Science, University of Glasgow, UK</emphasis></para>
<para><emphasis role="strong">Livio Pompianu</emphasis>, <emphasis>University of Cagliari, Italy</emphasis></para>
<para><emphasis role="strong">Luca Aceto</emphasis>, <emphasis>School of Computer Science, Reykjav&#x00ED;k University, Iceland</emphasis> </para>
<para><emphasis role="strong">Luca Padovani</emphasis>, <emphasis>Dipartimento di Informatica, Universit&#x00E0; di Torino, Italy</emphasis> </para>
<para><emphasis role="strong">Massimo Bartoletti</emphasis>, <emphasis>Universit&#x00E0; degli Studi di Cagliari, Italy</emphasis></para>
<para><emphasis role="strong">Maurizio Gabbrielli</emphasis>, <emphasis>Focus Team, University of Bologna/INRIA, Italy</emphasis></para>
<para><emphasis role="strong">Maurizio Murgia</emphasis>, <emphasis>Universit&#x00E0; degli Studi di Cagliari, Italy</emphasis></para>
<para><emphasis role="strong">Nicholas Ng</emphasis>, <emphasis>Imperial College London, UK</emphasis></para>
<para><emphasis role="strong">Nicola Atzei</emphasis>, <emphasis>Universit&#x00E0; degli Studi di Cagliari, Italy</emphasis></para>
<para><emphasis role="strong">Nobuko Yoshida</emphasis>, <emphasis>Imperial College London, UK</emphasis></para>
<para><emphasis role="strong">Ornela Dardha</emphasis>, <emphasis>School of Computing Science, University of Glasgow, UK</emphasis></para>
<para><emphasis role="strong">Raymond Hu</emphasis>, <emphasis>Imperial College London, UK</emphasis></para>
<para><emphasis role="strong">Roberto Zunino</emphasis>, <emphasis>Universit&#x00E0; degli Studi di Trento, Italy</emphasis></para>
<para><emphasis role="strong">Roly Perera</emphasis>, <emphasis>School of Computing Science, University of Glasgow, UK</emphasis> and <emphasis>School of Informatics, University of Edinburgh, UK</emphasis></para>
<para><emphasis role="strong">Rumyana Neykova</emphasis>, <emphasis>Imperial College London, UK</emphasis></para>
<para><emphasis role="strong">Sam Lindley</emphasis>, <emphasis>University of Edinburgh, Edinburgh, UK</emphasis></para>
<para><emphasis role="strong">Saverio Giallorenzo</emphasis>, <emphasis>Focus Team, University of Bologna/INRIA, Italy</emphasis> </para>
<para><emphasis role="strong">Simon J. Gay</emphasis>, <emphasis>School of Computing Science, University of Glasgow, UK</emphasis> </para>
<para><emphasis role="strong">S&#x00F8;ren Debois</emphasis>, <emphasis>Department of Computer Science, IT University of Copenhagen, Rued Langgaards Vej 7, 2300 Copenhagen S, Denmark</emphasis></para>
<para><emphasis role="strong">Stefano Lande</emphasis>, <emphasis>University of Cagliari, Italy</emphasis></para>
<para><emphasis role="strong">Thomas T. Hildebrandt</emphasis>, <emphasis>Department of Computer Science, IT University of Copenhagen, Rued Langgaards Vej 7, 2300 Copenhagen S, Denmark</emphasis></para>
<para><emphasis role="strong">Tiziana Cimoli</emphasis>, <emphasis>University of Cagliari, Italy</emphasis></para>
<para><emphasis role="strong">Vasco T. Vasconcelos</emphasis>, <emphasis>LaSIGE, Faculty of Sciences, University of Lisbon, PT</emphasis></para>
</preface>
<preface class="preface" id="preface05">
<title>List of Figures</title>
<table-wrap position="float">
<table cellspacing="5" cellpadding="5" frame="none" rules="none">
<tbody>
<tr><td valign="top" width="15%"><emphasis role="strong"><link linkend="F2-1">Figure <xref linkned="F2-1" remap="2.1"/></link></emphasis></td><td>Contract-oriented interactions in the CO<subscript>2</subscript> middleware</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F2-2">Figure <xref linkned="F2-2" remap="2.2"/></link></emphasis></td><td>Reputation of the dishonest and honest stores as a function of the number of sessions with malicious distributors</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F3-1">Figure <xref linkned="F3-1" remap="3.1"/></link></emphasis></td><td>Runtime monitor synthesis and operational set-up</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F3-2">Figure <xref linkned="F3-2" remap="3.2"/></link></emphasis></td><td>The syntax and semantics of <emphasis role="italicAlt">&#x03BC;</emphasis>HML</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F3-3">Figure <xref linkned="F3-3" remap="3.3"/></link></emphasis></td><td>The LTSs depicting the behaviour of two servers <emphasis>p</emphasis> and <emphasis>q</emphasis></td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F3-4">Figure <xref linkned="F3-4" remap="3.4"/></link></emphasis></td><td>The syntax of <literal>m</literal>HML</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F3-5">Figure <xref linkned="F3-5" remap="3.5"/></link></emphasis></td><td>The anatomy of action patterns for the enriched <emphasis role="roman">m</emphasis>HML syntax</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F3-6">Figure <xref linkned="F3-6" remap="3.6"/></link></emphasis></td><td>Runtime verifying the correctness of a client-server system</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F3-7">Figure <xref linkned="F3-7" remap="3.7"/></link></emphasis></td><td>The monitor synthesis process and instrumentation pipeline</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F3-8">Figure <xref linkned="F3-8" remap="3.8"/></link></emphasis></td><td>Creating the Erlang project directory structure</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F4-1">Figure <xref linkned="F4-1" remap="4.1"/></link></emphasis></td><td>Scribble methodology from global specification to local runtime verification</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F4-2">Figure <xref linkned="F4-2" remap="4.2"/></link></emphasis></td><td>Global Protocol (left) and Local Protocol (right)</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F4-3">Figure <xref linkned="F4-3" remap="4.3"/></link></emphasis></td><td>The core Python Conversation API operations</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F4-4">Figure <xref linkned="F4-4" remap="4.4"/></link></emphasis></td><td>Python program for A: synchronous implementation (left) and event-driven implementation (right)</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F4-5">Figure <xref linkned="F4-5" remap="4.5"/></link></emphasis></td><td>Configuration of distributed session monitors for an AMQP-based network</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F4-6">Figure <xref linkned="F4-6" remap="4.6"/></link></emphasis></td><td>Monitor workflow for (1) invitation and (2) in-conversation messages</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F4-7">Figure <xref linkned="F4-7" remap="4.7"/></link></emphasis></td><td>Sequence diagram (left) and Scribble protocol (right) for the RAC use case</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F4-8">Figure <xref linkned="F4-8" remap="4.8"/></link></emphasis></td><td>Python implementation (left) and Scribble local protocol (right) for the User role for the global protocol from Figure 4.7</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F4-9">Figure <xref linkned="F4-9" remap="4.9"/></link></emphasis></td><td>Event-driven conversation implementation for the User role (left) and Nested FSM generated from the User local protocol (right)</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F4-10">Figure <xref linkned="F4-10" remap="4.10"/></link></emphasis></td><td>The core Python Conversation API operations and their session <emphasis role="italicAlt">&#x03C0;</emphasis>-calulus counterparts</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F5-1">Figure <xref linkned="F5-1" remap="5.1"/></link></emphasis></td><td>Events and initial states (marking) for the mortgage application process. Where nothing else is indicated, the initial state of an event is not executed, included, and not pending</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F5-2">Figure <xref linkned="F5-2" remap="5.2"/></link></emphasis></td><td>DCR graph modelling the mortgage application process</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F5-3">Figure <xref linkned="F5-3" remap="5.3"/></link></emphasis></td><td>The DCR Workbench</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F5-4">Figure <xref linkned="F5-4" remap="5.4"/></link></emphasis></td><td>EBNF definition of the language recognised by the Parser panel</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F5-5">Figure <xref linkned="F5-5" remap="5.5"/></link></emphasis></td><td>Source code for the core process</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F5-6">Figure <xref linkned="F5-6" remap="5.6"/></link></emphasis></td><td>Visualisation of core process of Figure 5.5</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F5-7">Figure <xref linkned="F5-7" remap="5.7"/></link></emphasis></td><td>Transition system of the full mortgage application process (top), with the red box expanded for readability (bottom)</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F5-8">Figure <xref linkned="F5-8" remap="5.8"/></link></emphasis></td><td>Budget process fragment</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F5-9">Figure <xref linkned="F5-9" remap="5.9"/></link></emphasis></td><td>Visualisation of the budget process fragment of Figure 5.8</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F5-10">Figure <xref linkned="F5-10" remap="5.10"/></link></emphasis></td><td>Visualisation of the core process (Figures 5.5 and 5.6) refined by the budget fragment (Figures 5.8 and 5.9)</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F5-11">Figure <xref linkned="F5-11" remap="5.11"/></link></emphasis></td><td>Appraisal process fragment</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F5-12">Figure <xref linkned="F5-12" remap="5.12"/></link></emphasis></td><td>Additional timing constraints for the mortgage application process in Figure 5.2</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F5-13">Figure <xref linkned="F5-13" remap="5.13"/></link></emphasis></td><td>Visualisation of additional timing constraints for the mortgage application process in Figure 5.2</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F5-14">Figure <xref linkned="F5-14" remap="5.14"/></link></emphasis></td><td>Additional subprocess constraints (credit limit extension) for the full mortgage application process of Figure 5.14</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F5-15">Figure <xref linkned="F5-15" remap="5.15"/></link></emphasis></td><td>Visualisation of additional subprocess constraints (credit limit extension) for the full mortgage application process of Figure 5.2; after one execution of Apply for limit extension</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F5-16">Figure <xref linkned="F5-16" remap="5.16"/></link></emphasis></td><td>Alternative subprocess-with-data constraints (credit limit extension) for the full mortgage application process of Figure 5.2</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F6-1">Figure <xref linkned="F6-1" remap="6.1"/></link></emphasis></td><td>Four player game &#x2013; CFSMs</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F6-2">Figure <xref linkned="F6-2" remap="6.2"/></link></emphasis></td><td>Four player game &#x2013; Global graph</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F6-3">Figure <xref linkned="F6-3" remap="6.3"/></link></emphasis></td><td>Four player game &#x2013; TS<subscript><emphasis role="romanAlt">0</emphasis></subscript></td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F6-4">Figure <xref linkned="F6-4" remap="6.4"/></link></emphasis></td><td>Architecture of <literal>ChorGram</literal></td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F6-5">Figure <xref linkned="F6-5" remap="6.5"/></link></emphasis></td><td>HelloWorld example &#x2013; fsa representation</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F6-6">Figure <xref linkned="F6-6" remap="6.6"/></link></emphasis></td><td>Global graph for ATM service v1</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F6-7">Figure <xref linkned="F6-7" remap="6.7"/></link></emphasis></td><td>Synchronous transition system of ATM service v2</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F6-8">Figure <xref linkned="F6-8" remap="6.8"/></link></emphasis></td><td>Global graph for ATM service v3</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F7-1">Figure <xref linkned="F7-1" remap="7.1"/></link></emphasis></td><td>The AIOCJ IDE</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F7-2">Figure <xref linkned="F7-2" remap="7.2"/></link></emphasis></td><td>The AIOCJ framework &#x2014; deployment and execution of a choreography</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F7-3">Figure <xref linkned="F7-3" remap="7.3"/></link></emphasis></td><td>Location dependency graph among AIOCJ microservices</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F8-1">Figure <xref linkned="F8-1" remap="8.1"/></link></emphasis></td><td>Cases of circular dependencies that may lead to deadlocks</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F8-2">Figure <xref linkned="F8-2" remap="8.2"/></link></emphasis></td><td><literal>Java Network</literal> program and corresponding bytecode of methods <literal>buildNetwork</literal> and <literal>takeLocks</literal>. Comments in the bytecode give information of the objects used and/or methods invoked in each instruction</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F8-3">Figure <xref linkned="F8-3" remap="8.3"/></link></emphasis></td><td><literal>BuildNetwork</literal>&#x2019;s lams</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F8-4">Figure <xref linkned="F8-4" remap="8.4"/></link></emphasis></td><td>Type inference of methods&#x2019; behaviors in <literal>JaDA</literal></td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F8-5">Figure <xref linkned="F8-5" remap="8.5"/></link></emphasis></td><td>Type inference of method&#x2019;s bodies in <literal>JaDA</literal></td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F8-6">Figure <xref linkned="F8-6" remap="8.6"/></link></emphasis></td><td><literal>JaDA</literal> analysis of behavioral types</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F8-7">Figure <xref linkned="F8-7" remap="8.7"/></link></emphasis></td><td><literal>JaDA</literal> analysis output for the <literal>Network</literal> program</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F8-8">Figure <xref linkned="F8-8" remap="8.8"/></link></emphasis></td><td><literal>JaDA</literal> architecture</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F8-9">Figure <xref linkned="F8-9" remap="8.9"/></link></emphasis></td><td><literal>Java while</literal> loop with nested synchronizations and the corresponding bytecode</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F8-10">Figure <xref linkned="F8-10" remap="8.10"/></link></emphasis></td><td><literal>JaDA</literal> Eclipse plug-in screenshot</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F9-1">Figure <xref linkned="F9-1" remap="9.1"/></link></emphasis></td><td>Graphical representation of the Fibonacci stream network</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F9-2">Figure <xref linkned="F9-2" remap="9.2"/></link></emphasis></td><td>Graphical representation of a 4 <emphasis>&#x00D7;</emphasis> 3 bi-dimensional stencil</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F9-3">Figure <xref linkned="F9-3" remap="9.3"/></link></emphasis></td><td>Master-worker (left) and producer-consumer (right)</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F9-4">Figure <xref linkned="F9-4" remap="9.4"/></link></emphasis></td><td>Graphical representation of an odd-even 6-input sorting network</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F9-5">Figure <xref linkned="F9-5" remap="9.5"/></link></emphasis></td><td>Stream network computing the Thue-Morse sequence</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F10-1">Figure <xref linkned="F10-1" remap="10.1"/></link></emphasis></td><td>Implementations of the communication-typed combinators where <literal>link :: Links =&gt; (Chan s, Chan (Dual s)) -&gt; IO ()</literal></td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F12-1">Figure <xref linkned="F12-1" remap="12.1"/></link></emphasis></td><td>Syntax of types and kinds</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F12-2">Figure <xref linkned="F12-2" remap="12.2"/></link></emphasis></td><td>Syntax of terms and values</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F12-3">Figure <xref linkned="F12-3" remap="12.3"/></link></emphasis></td><td>Kinding rules</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F12-4">Figure <xref linkned="F12-4" remap="12.4"/></link></emphasis></td><td>Linearity of contexts and context splitting</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F12-5">Figure <xref linkned="F12-5" remap="12.5"/></link></emphasis></td><td>Typing rules</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F12-6">Figure <xref linkned="F12-6" remap="12.6"/></link></emphasis></td><td>Type schemas for constants</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F13-1">Figure <xref linkned="F13-1" remap="13.1"/></link></emphasis></td><td>Sequence diagram for the Math Service protocol</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F13-2">Figure <xref linkned="F13-2" remap="13.2"/></link></emphasis></td><td>A factorial calculation using Math Service via the <literal>java.net.Socket</literal> API</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F13-3">Figure <xref linkned="F13-3" remap="13.3"/></link></emphasis></td><td>Factorial calculation as a client of the remote interface in Figure 13.4(a)</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F13-4">Figure <xref linkned="F13-4" remap="13.4"/></link></emphasis></td><td>A remote Math Service: (a) interface, and (b) implementation</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F13-5">Figure <xref linkned="F13-5" remap="13.5"/></link></emphasis></td><td>Scribble global protocol for Math Service in Figure 13.1</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F13-6">Figure <xref linkned="F13-6" remap="13.6"/></link></emphasis></td><td>Endpoint FSMs for <literal>MathSvc</literal> (Figure 13.5)</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F13-7">Figure <xref linkned="F13-7" remap="13.7"/></link></emphasis></td><td>Factorial calculation using the Endpoint API generated for <literal>C</literal></td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F13-8">Figure <xref linkned="F13-8" remap="13.8"/></link></emphasis></td><td>State Channel API generated for <literal>C</literal> in <literal>MathSvc</literal> (Figure 13.5)</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F13-9">Figure <xref linkned="F13-9" remap="13.9"/></link></emphasis></td><td>State Channel API generated for <literal>S</literal> in <literal>MathSvc</literal>; and an implementation of <literal>S</literal> using the generated API</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F13-10">Figure <xref linkned="F13-10" remap="13.10"/></link></emphasis></td><td>Extract from the revised specification of HTTP in Scribble</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F13-11">Figure <xref linkned="F13-11" remap="13.11"/></link></emphasis></td><td>Extract from an implementation of a HTTP client via API generation</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F13-12">Figure <xref linkned="F13-12" remap="13.12"/></link></emphasis></td><td>Additional branch callback interfaces generated for <literal>S</literal> in <literal>MathSvc</literal>; and a corresponding implementation of <literal>S</literal></td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F15-1">Figure <xref linkned="F15-1" remap="15.1"/></link></emphasis></td><td><literal>Pabble</literal>-based MPI program generation workflow (core flow highlighted)</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F15-2">Figure <xref linkned="F15-2" remap="15.2"/></link></emphasis></td><td>Global view of <literal>Label(T) from Sender to Receiver;</literal></td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F15-3">Figure <xref linkned="F15-3" remap="15.3"/></link></emphasis></td><td><literal>Pabble</literal> syntax</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F15-4">Figure <xref linkned="F15-4" remap="15.4"/></link></emphasis></td><td><literal>Pabble</literal> interaction statement and its MPI backbone</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F15-5">Figure <xref linkned="F15-5" remap="15.5"/></link></emphasis></td><td><literal>Pabble</literal> parallel interaction statement and its MPI backbone</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F15-6">Figure <xref linkned="F15-6" remap="15.6"/></link></emphasis></td><td><literal>Pabble</literal> internal interaction statement and its MPI backbone</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F15-7">Figure <xref linkned="F15-7" remap="15.7"/></link></emphasis></td><td>Control-flow: <literal>Pabble</literal> iteration statements and their corresponding MPI backbones</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F15-8">Figure <xref linkned="F15-8" remap="15.8"/></link></emphasis></td><td>Control-flow: <literal>Pabble</literal> choice and its corresponding MPI backbone</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F15-9">Figure <xref linkned="F15-9" remap="15.9"/></link></emphasis></td><td>Collective operations: <literal>Pabble</literal> collectives and their corresponding MPI backbones</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F16-1">Figure <xref linkned="F16-1" remap="16.1"/></link></emphasis></td><td>Communication pattern for the finite differences algorithm</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F16-2">Figure <xref linkned="F16-2" remap="16.2"/></link></emphasis></td><td>Excerpt of an MPI program for the finite differences problem</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F16-3">Figure <xref linkned="F16-3" remap="16.3"/></link></emphasis></td><td>Protocol for the finite differences algorithm</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F16-4">Figure <xref linkned="F16-4" remap="16.4"/></link></emphasis></td><td>Protocol compiler running under the Eclipse IDE</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F16-5">Figure <xref linkned="F16-5" remap="16.5"/></link></emphasis></td><td>Workflow of the verification procedure for C+MPI programs</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="F16-6">Figure <xref linkned="F16-6" remap="16.6"/></link></emphasis></td><td>The code of Figure 16.2 with verification marks inserted</td></tr>
</tbody>
</table>
</table-wrap>
</preface>
<preface class="preface" id="preface06">
<title>List of Tables</title>
<table-wrap position="float">
<table cellspacing="5" cellpadding="5" frame="none" rules="none">
<tbody>
<tr><td valign="top" width="15%"><emphasis role="strong"><link linkend="T8-1">Table <xref linkned="T8-1" remap="8.1"/></link></emphasis></td><td>Comparison with different deadlock detection tools. The inner cells show the number of deadlocks detected by each tool. The output labelled &#x0201C;(*)&#x0201D; are related to modified versions of the original programs: see the text</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="T9-1">Table <xref linkned="T9-1" remap="9.1"/></link></emphasis></td><td>Syntax of <literal>Hypha</literal> input language (partial)</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="T9-2">Table <xref linkned="T9-2" remap="9.2"/></link></emphasis></td><td>Operational semantics of processes</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="T11-1">Table <xref linkned="T11-1" remap="11.1"/></link></emphasis></td><td>Application programming interface for binary sessions</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="T11-2">Table <xref linkned="T11-2" remap="11.2"/></link></emphasis></td><td><literal>OCaml</literal> interface of the API for binary sessions</td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="T15-1">Table <xref linkned="T15-1" remap="15.1"/></link></emphasis></td><td>Annotations in backbone and kernel</td></tr>
</tbody>
</table>
</table-wrap>
</preface>
<preface class="preface" id="preface07">
<title>List of Abbreviations</title>
<table-wrap position="float">
<table cellspacing="5" cellpadding="5" frame="none" rules="none">
<tbody>
<tr><td valign="top">AIOC</td><td valign="top">Adaptive Interaction Oriented Choreographies</td></tr>
<tr><td valign="top">AIOCJ</td><td valign="top">Adaptive Interaction Oriented Choreographies in Jolie</td></tr>
<tr><td valign="top">AMQP</td><td valign="top">Advanced Message Queuing Protocol</td></tr>
<tr><td valign="top">AOP</td><td valign="top">Aspect-Oriented Programming</td></tr>
<tr><td valign="top">API</td><td valign="top">Application Programming Interface</td></tr>
<tr><td valign="top">ASCII</td><td valign="top">American Standard Code for Information Interchange</td></tr>
<tr><td valign="top">ATM</td><td valign="top">Automated teller machine</td></tr>
<tr><td valign="top">BCT</td><td valign="top">Behavioral Class Table</td></tr>
<tr><td valign="top">BPMN</td><td valign="top">Business Process Model and Notation</td></tr>
<tr><td valign="top">CCP</td><td valign="top">Concurrent constraint programming</td></tr>
<tr><td valign="top">CCS</td><td valign="top">calculus of communicating systems</td></tr>
<tr><td valign="top">CFSM</td><td valign="top">Communicating Finite State Machines</td></tr>
<tr><td valign="top">cHML</td><td valign="top">Co-Safety HML</td></tr>
<tr><td valign="top">CML</td><td valign="top">Concurrent ML</td></tr>
<tr><td valign="top">CMMN</td><td valign="top">Case Management Model and Notation</td></tr>
<tr><td valign="top">CTA</td><td valign="top">Communicating timed automata</td></tr>
<tr><td valign="top">DCR</td><td valign="top">Dynamic Condition Response</td></tr>
<tr><td valign="top">EFSM</td><td valign="top">Endpoint Finite State Machine</td></tr>
<tr><td valign="top">EVM</td><td valign="top">Erlang Virtual Machine</td></tr>
<tr><td valign="top">FIFO</td><td valign="top">First In, First Out</td></tr>
<tr><td valign="top">FSM</td><td valign="top">Finite State Machine</td></tr>
<tr><td valign="top">GHC</td><td valign="top">Glasgow Haskell Compiler</td></tr>
<tr><td valign="top">GMC</td><td valign="top">Generalised multiparty compatibility</td></tr>
<tr><td valign="top">GSM</td><td valign="top">Guard Stage Milestone</td></tr>
<tr><td valign="top">HML</td><td valign="top">Hennessy-Milner Logic</td></tr>
<tr><td valign="top">HTTP</td><td valign="top">Hypertext Transfer Protocol</td></tr>
<tr><td valign="top">I/O</td><td valign="top">Input/Output</td></tr>
<tr><td valign="top">IDE</td><td valign="top">Integrated Development Environment</td></tr>
<tr><td valign="top">IT</td><td valign="top">Information Technology</td></tr>
<tr><td valign="top">ITU</td><td valign="top">IT University of Copenhagen</td></tr>
<tr><td valign="top">JPF</td><td valign="top">Java PathFinder</td></tr>
<tr><td valign="top">JSON</td><td valign="top">JavaScript Object Notation</td></tr>
<tr><td valign="top">JVM</td><td valign="top">Java Virtual Machine</td></tr>
<tr><td valign="top">JVML</td><td valign="top">Java Virtual Machine Language</td></tr>
<tr><td valign="top">lam</td><td valign="top">deadLock Analysis Model</td></tr>
<tr><td valign="top">LLC</td><td valign="top">Linear lambda-calculus</td></tr>
<tr><td valign="top">MC</td><td valign="top">Model Checking</td></tr>
<tr><td valign="top">MFA</td><td valign="top">Module, Function, Arguments</td></tr>
<tr><td valign="top">mHML</td><td valign="top">Monitorable HML</td></tr>
<tr><td valign="top">MOM</td><td valign="top">Message oriented middleware</td></tr>
<tr><td valign="top">MPI</td><td valign="top">Message-Passing Interface</td></tr>
<tr><td valign="top">MPST</td><td valign="top">Multiparty Session Types</td></tr>
<tr><td valign="top">OOI</td><td valign="top">Ocean Observatories Initiative</td></tr>
<tr><td valign="top">PCF</td><td valign="top">Programmable Computable Functions</td></tr>
<tr><td valign="top">POP3</td><td valign="top">Post Office Protocol 3</td></tr>
<tr><td valign="top">QoS</td><td valign="top">Quality of service</td></tr>
<tr><td valign="top">RAC</td><td valign="top">Resource Access Control</td></tr>
<tr><td valign="top">RE</td><td valign="top">Runtime Environment</td></tr>
<tr><td valign="top">RFC</td><td valign="top">Request for Comments</td></tr>
<tr><td valign="top">RMI</td><td valign="top">Remote Method Invocation</td></tr>
<tr><td valign="top">RPC</td><td valign="top">Remote Procedure Call</td></tr>
<tr><td valign="top">RV</td><td valign="top">Runtime Verification</td></tr>
<tr><td valign="top">sHML</td><td valign="top">Safety HML</td></tr>
<tr><td valign="top">SMS</td><td valign="top">Short Message Service</td></tr>
<tr><td valign="top">SMTP</td><td valign="top">Simple Mail Transfer Protocol</td></tr>
<tr><td valign="top">SOAP</td><td valign="top">Simple Object Access Protocol</td></tr>
<tr><td valign="top">SODEP</td><td valign="top">Simple Operation Data Exchange Protocol</td></tr>
<tr><td valign="top">TCP</td><td valign="top">Transmission Control Protocol</td></tr>
<tr><td valign="top">TCP/IP</td><td valign="top">Transmission Control Protocol/Internet Protocol</td></tr>
<tr><td valign="top">TST</td><td valign="top">Timed session type</td></tr>
<tr><td valign="top">UML</td><td valign="top">Unified modeling language</td></tr>
<tr><td valign="top">URI</td><td valign="top">Uniform Resource Identifier</td></tr>
<tr><td valign="top">VM</td><td valign="top">Virtual Machine</td></tr>
<tr><td valign="top">WS-CDL</td><td valign="top">Web Services Choreography Description Language</td></tr>
<tr><td valign="top">XML</td><td valign="top">eXtensible Markup Language</td></tr>
<tr><td valign="top">&#x03BC;HML</td><td valign="top">&#x03BC; HML</td></tr>
</tbody>
</table>
</table-wrap>
</preface>

<preface class="preface" id="preface08">
<title>Contents</title>
<table-wrap position="float">
<table cellspacing="5" cellpadding="5" frame="none" rules="none">
<tbody>
<tr><td valign="top"><emphasis role="strong"><link linkend="ch01">1 Contract-Oriented Design of Distributed Applications: A Tutorial</link></emphasis><?lb?>Nicola Atzei, Massimo Bartoletti, Maurizio Murgia, Emilio Tuosto and Roberto Zunino</td><td valign="top" align="left" width="20%"><ulink url="http://riverpublishers.com/dissertations_xml/9788793519817/pdf/01_Chapter_01.pdf">Download As PDF</ulink></td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="ch02">2 Contract-Oriented Programming with Timed Session Types</link></emphasis><?lb?>Nicola Atzei, Massimo Bartoletti, Tiziana Cimoli, Stefano Lande, Maurizio Murgia, Alessandro Sebastian Podda and Livio Pompianu</td><td valign="top" align="left" width="20%"><ulink url="http://riverpublishers.com/dissertations_xml/9788793519817/pdf/02_Chapter_02.pdf">Download As PDF</ulink></td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="ch03">3 A Runtime Monitoring Tool for Actor-Based Systems</link></emphasis><?lb?>Duncan Paul Attard, Ian Cassar, Adrian Francalanza, Luca Aceto and Anna Ingo&#x00F3; lfsdo&#x00F3; ttir</td><td valign="top" align="left" width="20%"><ulink url="http://riverpublishers.com/dissertations_xml/9788793519817/pdf/03_Chapter_03.pdf">Download As PDF</ulink></td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="ch04">4 How to Verify Your Python Conversations</link></emphasis><?lb?>Rumyana Neykova and Nobuko Yoshida</td><td valign="top" align="left" width="20%"><ulink url="http://riverpublishers.com/dissertations_xml/9788793519817/pdf/04_Chapter_04.pdf">Download As PDF</ulink></td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="ch05">5 The DCR Workbench: Declarative Choreographies for Collaborative Processes</link></emphasis><?lb?>S&#x00F8;ren Debois and Thomas T. Hildebrandt</td><td valign="top" align="left" width="20%"><ulink url="http://riverpublishers.com/dissertations_xml/9788793519817/pdf/05_Chapter_05.pdf">Download As PDF</ulink></td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="ch06">6 A Tool for Choreography-Based Analysis of Message-Passing Software</link></emphasis><?lb?>Julien Lange, Emilio Tuosto and Nobuko Yoshida</td><td valign="top" align="left" width="20%"><ulink url="http://riverpublishers.com/dissertations_xml/9788793519817/pdf/06_Chapter_06.pdf">Download As PDF</ulink></td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="ch07">7 Programming Adaptive Microservice Applications: an AIOCJ Tutorial</link></emphasis><?lb?>Saverio Giallorenzo, Ivan Lanese, Jacopo Mauro and Maurizio Gabbrielli</td><td valign="top" align="left" width="20%"><ulink url="http://riverpublishers.com/dissertations_xml/9788793519817/pdf/07_Chapter_07.pdf">Download As PDF</ulink></td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="ch08">8 <literal>JaDA</literal> &#x2013; the Java Deadlock Analyzer</link></emphasis><?lb?>Abel Garcia and Cosimo Laneve</td><td valign="top" align="left" width="20%"><ulink url="http://riverpublishers.com/dissertations_xml/9788793519817/pdf/08_Chapter_08.pdf">Download As PDF</ulink></td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="ch09">9 Type-Based Analysis of Linear Communications</link></emphasis><?lb?>Luca Padovani</td><td valign="top" align="left" width="20%"><ulink url="http://riverpublishers.com/dissertations_xml/9788793519817/pdf/09_Chapter_09.pdf">Download As PDF</ulink></td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="ch10">10 Session Types with Linearity in Haskell</link></emphasis><?lb?>Dominic Orchard and Nobuko Yoshida</td><td valign="top" align="left" width="20%"><ulink url="http://riverpublishers.com/dissertations_xml/9788793519817/pdf/10_Chapter_10.pdf">Download As PDF</ulink></td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="ch11">11 An OCaml Implementation of Binary Sessions</link></emphasis><?lb?>Herna&#x00F3;n Melgratti and Luca Padovani</td><td valign="top" align="left" width="20%"><ulink url="http://riverpublishers.com/dissertations_xml/9788793519817/pdf/11_Chapter_11.pdf">Download As PDF</ulink></td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="ch12">12 Lightweight Functional Session Types</link></emphasis><?lb?>Sam Lindley and J. Garrett Morris</td><td valign="top" align="left" width="20%"><ulink url="http://riverpublishers.com/dissertations_xml/9788793519817/pdf/12_Chapter_12.pdf">Download As PDF</ulink></td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="ch13">13 Distributed Programming Using Java APIs Generated from Session Types</link></emphasis><?lb?>Raymond Hu</td><td valign="top" align="left" width="20%"><ulink url="http://riverpublishers.com/dissertations_xml/9788793519817/pdf/13_Chapter_13.pdf">Download As PDF</ulink></td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="ch14">14 Mungo and StMungo: Tools for Typechecking Protocols in Java</link></emphasis><?lb?>Ornela Dardha, Simon J. Gay, Dimitrios Kouzapas, Roly Perera, A. Laura Voinea and Florian Weber</td><td valign="top" align="left" width="20%"><ulink url="http://riverpublishers.com/dissertations_xml/9788793519817/pdf/14_Chapter_14.pdf">Download As PDF</ulink></td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="ch15">15 Protocol-Driven MPI Program Generation</link></emphasis><?lb?>Nicholas Ng and Nobuko Yoshida</td><td valign="top" align="left" width="20%"><ulink url="http://riverpublishers.com/dissertations_xml/9788793519817/pdf/15_Chapter_15.pdf">Download As PDF</ulink></td></tr>
<tr><td valign="top"><emphasis role="strong"><link linkend="ch16">16 Deductive Verification of MPI Protocols</link></emphasis><?lb?>Vasco T. Vasconcelos, Francisco Martins, Eduardo R. B. Marques, Nobuko Yoshida and Nicholas Ng</td><td valign="top" align="left" width="20%"><ulink url="http://riverpublishers.com/dissertations_xml/9788793519817/pdf/16_Chapter_16.pdf">Download As PDF</ulink></td></tr>
</tbody>
</table>
</table-wrap>
</preface>
<chapter class="chapter" id="ch01" label="1" xreflabel="1">
<title>Contract-Oriented Design of Distributed Applications: A Tutorial</title>
<para><emphasis role="strong">Nicola Atzei</emphasis><footnote id="fn1_1a" label="1"><para>Universit&#x00E0; degli Studi di Cagliari, Italy</para></footnote><emphasis role="strong">, Massimo Bartoletti</emphasis><footnoteref linkend="fn1_1" label="1"/><emphasis role="strong">, Maurizio Murgia</emphasis><footnoteref linkend="fn1_1a" label="1"/><emphasis role="strong">, Emilio Tuosto</emphasis><footnote id="fn1_2a" label="2"><para>University of Leicester, UK</para></footnote> <emphasis role="strong">and Roberto Zunino</emphasis><footnote id="fn1_3a" label="3"><para>Universit&#x00E0; degli Studi di Trento, Italy</para></footnote></para>
<section class="lev2">
<title>Abstract</title>
<para>Modern distributed applications typically blend new code with legacy (and possibly untrusted) third-party services. Behavioural contracts can be used to discipline the interaction among these services. Contract-oriented design advocates that composition is possible only among services with compliant contracts, and execution is monitored to detect (and possibly sanction) contract breaches.</para>
<para>In this tutorial we illustrate a contract-oriented design methodology consisting of five phases: specification writing, specification analysis, code generation, code refinement, and code analysis. Specifications are written in CO<subscript>2</subscript>, a process calculus whose primitives include contract advertisement, stipulation, and contractual actions. Our analysis verifies a property called honesty: intuitively, a process is honest if it always honors its contracts upon stipulation, so being guaranteed to never be sanctioned at run-time. We automatically translate a given honest specification into a skeletal Java program which renders the contract-oriented interactions, to be completed with the application logic. Then, programmers can refine this skeleton into the actual Java application: however, doing so they could accidentally break its honesty. The last phase is an automated code analysis to verify that honesty has not been compromised by the refinement.</para>
<para>All the phases of our methodology are supported by a toolchain, called Diogenes. We guide the reader through Diogenes to design small contract-oriented applications.</para>
</section>
<section class="lev1" id="sec1-1" label="1.1" xreflabel="1.1">
<title>Introduction</title>
<para>Developing service-oriented applications is a challenging task: programmers have to reliably compose loosely-coupled services which can dynamically discover and invoke other services through open networks, and may be subject to failures and attacks. Usually, services live in a world of mutually distrusting providers, possibly competing among each other. Typically, these providers offer little guarantees about the services they control, and in particular they might arbitrarily change the service code (if not the Service Level Agreement <emphasis>tout court</emphasis>) at any time.</para>
<para>Therefore, to guarantee the reliability and security of service-oriented applications, one must use suitable analysis techniques. Remarkably, most existing techniques to guarantee deadlock-freedom of service-oriented applications (e.g., compositional verification based on choreographies [<link linkend="ch01_b35">35</link>, <link linkend="ch01_b21">21</link>]) need to inspect the code of <emphasis>all</emphasis> its components. Instead, under the given assumptions of mutual distrust between services, one can only analyse those under their control.</para>
<section class="lev2" id="sec1-1-1" label="1.1.1" xreflabel="1.1.1">
<title>From Service-Oriented to Contract-Oriented Computing</title>
<para>A possible countermeasure to these issues is to use <emphasis>behavioural contracts</emphasis> to regulate the interaction between services. In this setting, a service infrastructure acts as a trusted third party, which collects all the contracts advertised by services, and establishes sessions between services with compliant contracts. Unlike the usual service-oriented paradigm, here services are responsible for respecting their contracts. To incentivize such honest behaviour, the service infrastructure monitors all the messages exchanged among services, and sanctions those which do not respect their contracts.</para>
<para>Sanctions can be of different nature: e.g., pecuniary compensations, adaptations of the service binding [<link linkend="ch01_b29">29</link>], or reputation penalties which marginalize dishonest services in the selection phase [<link linkend="ch01_b3">3</link>]. Experimental evidence [<link linkend="ch01_b3">3</link>] shows that contract-orientation can mitigate the effort of handling potential misbehaviour of external services, at the cost of a tolerable loss in efficiency due to the contract-based service selection and monitoring.</para>
</section>
<section class="lev2" id="sec1-1-2" label="1.1.2" xreflabel="1.1.2">
<title>Honesty Attacks</title>
<para>The sanctioning mechanism of contract-oriented infrastructures protects honest services against malicious behaviours of the other services: indeed, if a malevolent service attempts to break the protocol (e.g. by prematurely terminating the interaction), it is punished by the infrastructure. At the same time, a new kind of attack becomes possible: adversaries can try to exploit possible discrepancies between the promised and the actual behaviour of a service, in order to make it sanctioned. For instance, consider a na&#x00EF;ve online store with the following process:</para>
<orderedlist numeration="arabic" continuation="restarts" spacing="normal">
<listitem><para>Advertise a contract to &#8220;receive a <literal>request</literal> from a buyer, and then either send the <literal>price</literal> of the ordered item, or notify that the item is <literal>unavailable</literal>&#8221;;</para></listitem>
<listitem><para>Wait to receive a <literal>request</literal>;</para></listitem>
<listitem><para>Advertise a contract to &#8220;receive a <literal>quote</literal> from a package delivery service, and then either <literal>confirm</literal> or <literal>abort</literal>&#8221;;</para></listitem>
<listitem><para>Wait to receive a quote from the delivery service;</para></listitem>
<listitem><para>If the quote is below a certain threshold, then <literal>confirm</literal> the delivery and send the <literal>price</literal> to the buyer; otherwise, send <literal>abort</literal> to the delivery service, and notify as <literal>unavailable</literal> to the buyer.</para></listitem>
</orderedlist>
<para>Now, assume an adversary which plays the role of a delivery service, and never sends the <literal>quote</literal>. This makes the store violate its contract with the buyer: indeed, the store should either send <literal>price</literal> or <literal>unavailable</literal> to the buyer, but these actions can only be performed after the delivery service has sent a <literal>quote</literal>. Therefore, the store can be sanctioned.</para>
<para>Since these <emphasis>honesty attacks</emphasis> may compromise the service and cause economic damage to its provider, it is important to detect the underlying vulnerabilities <emphasis>before</emphasis> deployment. Intuitively, a service is vulnerable if, in <emphasis>some</emphasis> execution context, it does <emphasis>not</emphasis> respect some of the contracts it advertises. Therefore, to avoid sanctions a service must be able to respect <emphasis>all</emphasis> the contracts it advertises, in <emphasis>all</emphasis> possible contexts &#x2014; even in those populated by adversaries. We call this property <emphasis>honesty</emphasis>.</para>
<para>Some recent works have studied honesty at the specification level, using the process calculus CO<subscript>2</subscript> for modelling contract-oriented services [<link linkend="ch01_b6">6</link>&#x2013;<link linkend="ch01_b9">9</link>], whose primitives include contract advertisement, stipulation, and contractual actions. Practical experience has shown that writing honest specifications is not an easy task, especially when a service has to juggle with multiple sessions. The reason of this difficulty lies in the fact that, to devise an honest specification, a designer has to anticipate the possible behaviour of the context, but at design time he does not yet know in which context his service will be run. Tools to automate the verification of honesty may be of great help.</para>
</section>
<section class="lev2" id="sec1-1-3" label="1.1.3" xreflabel="1.1.3">
<title>Diogenes</title>
<para>In this paper we illustrate the Diogenes toolchain [<link linkend="ch01_b1">1</link>], which supports the correct design of contract-oriented services as follows:</para>
<itemizedlist mark="none" spacing="normal">
<listitem><para><emphasis role="strong">Specification</emphasis> Designers can specify services in the process calculus CO<subscript>2</subscript>. An Eclipse plugin supports writing such specifications, providing syntax highlighting, code auto-completion, syntactic and semantic checks, and basic static type checking.</para></listitem>
<listitem><para><emphasis role="strong">Honesty checking of specifications</emphasis> Our tool can statically verify the honesty of specifications. When the specification is dishonest, the tool provides a counter example, in the form of a reachable abstract state of the service which violates some contract.</para></listitem>
<listitem><para><emphasis role="strong">Translation into Java</emphasis> The tool automatically translates specifications into skeletal Java programs, implementing the required contract-oriented interactions (while leaving the actual application logic to be implemented in a subsequent step). The obtained skeleton is honest when the specification is such.</para></listitem>
<listitem><para><emphasis role="strong">Honesty checking of refined Java code</emphasis> Programmers can refine the skeleton by implementing the actual application logic. This is a potentially dangerous operation, since honesty can be accidentally lost in the manual refinement. The tool supports this step, by providing an honesty checker for refined Java code.</para></listitem>
</itemizedlist>
</section>
</section>
<section class="lev1" id="sec1-2" label="1.2" xreflabel="1.2">
<title>Specifying Contract-Oriented Services in CO<subscript>2</subscript></title>
<para>A service in our modelling language consists of a CO<subscript>2</subscript> process. CO<subscript>2</subscript> is a process algebra inspired from CCS [<link linkend="ch01_b28">28</link>], and equipped with contract-oriented primitives: contract advertisement, stipulation, and contractual actions. Contracts are meant to model the promised behaviour of services, and they are expressed as session types ([<link linkend="ch01_b34">34</link>]).</para>
<para>We show the main features of our language with the help of a small case study, an online store which receives orders from customers.</para>
<section class="lev2" id="sec1-2-1" label="1.2.1" xreflabel="1.2.1">
<title>Contracts</title>
<para>We first specify the contract <literal>C</literal> between the store and a customer, from the point of view of the store. The store declares that it will receive an <literal>order</literal>, and then send either the corresponding <literal>price</literal>, or declare that the item is <literal>unavailable</literal>. We formalise this contract as the following first-order binary session type [<link linkend="ch01_b19">19</link>]:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog001.jpg" mime-subtype="jpeg"/></para>
<para>Receive actions are marked with the symbol <literal>?</literal>, while send actions are marked with <literal>!</literal>. The sort of a message (<literal>int</literal>, <literal>string</literal>, or <literal>unit</literal>) is specified next to the action label; the sort <literal>unit</literal> is used for pure synchronizations, and it can be omitted. The symbol <literal>_._</literal> denotes prefixing. The symbol <literal>_(+)_</literal> is used to group send actions, and it denotes an <emphasis>internal</emphasis> choice made by the store.</para>
</section>
<section class="lev2" id="sec1-2-2" label="1.2.2" xreflabel="1.2.2">
<title>Processes</title>
<para>Note that contracts only formalise the interaction protocol between two services, while they do not specify <emphasis>how</emphasis> these services advertise and realise the contracts. This behaviour is formalised in CO<subscript>2</subscript> [<link linkend="ch01_b6">6</link>, <link linkend="ch01_b7">7</link>], a specification language for contract-oriented services. For instance, a possible CO<subscript>2</subscript> specification of our store is the following:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog002.jpg" mime-subtype="jpeg"/></para>
<para>At line <literal>2</literal>, the store <emphasis>advertises</emphasis> the contract <literal>C</literal>, waiting for the service infrastructure to find some other service with a <emphasis>compliant</emphasis> contract. Intuitively, two contracts are compliant if they fulfil each other expectations<footnote id="fn1_1" label="1"><para>More precisely, the notion of compliance we use here is <emphasis>progress</emphasis>, that relates two processes whenever their interaction never reaches a deadlock [<link linkend="ch01_b4">4</link>].</para></footnote>. When the infrastructure finds a contract compliant with <literal>C</literal>, a new session is created between the respective services, and the variable <literal>x</literal> is bound to the session name.</para>
<para>At line <literal>3</literal> of the snippet above the store waits to receive an <literal>order</literal>, binding it to the variable <literal>v</literal> of sort <literal>string</literal>. At line <literal>4</literal>, the store checks whether the ordered item is in stock (the actual condition is not given in the specification). If the item is in stock, then the store sends the <literal>price</literal> to the customer; otherwise it notifies that the item is <literal>unavailable</literal> (lines <literal>5</literal>-<literal>6</literal>). The sent price <literal>*:int</literal> is a placeholder, to be replaced with an actual price upon refinement of the specification into an actual implementation of the service.</para>
</section>
<section class="lev2" id="sec1-2-3" label="1.2.3" xreflabel="1.2.3">
<title>An Execution Context</title>
<para>We now show a possible context wherein to execute our store. Although the context is not needed for verifying the store specification, we use it to complete the presentation of the primitives of our modelling language.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog003.jpg" mime-subtype="jpeg"/></para>
<para>The contract advertised by <literal>BuyerA</literal> at line <literal>2</literal> is <emphasis>not</emphasis> compliant with the contract <literal>C</literal> advertised by the store: indeed, after sending the <literal>order</literal>, <literal>BuyerA</literal> only expects to receive the <literal>price</literal> &#x2014; while the store can also choose to send <literal>unavailable</literal>. Therefore, any service implementing <literal>BuyerA</literal> will never be put in a session with the <literal>Store</literal>. Instead, the contract advertised at line <literal>8</literal> by <literal>BuyerB</literal> is compliant with <literal>C</literal>. Note that this is true also if the two contracts are not one dual of each other: indeed, <literal>BuyerB</literal> accepts all the messages that the store may send (i.e., <literal>price</literal> and <literal>unavailable</literal>), and it also allows for a further message (<literal>availablefrom</literal>), to be used e.g. to notify when the item will be available. Although this message will never be used by the <literal>Store</literal>, it could allow <literal>BuyerB</literal> to establish sessions with more advanced stores. The symbol <literal>+</literal> is used to group receive actions, and it denotes an <emphasis>external choice</emphasis>, one which is not made by the buyer. At lines <literal>11-13</literal>, <literal>BuyerB</literal> waits to receive at session <literal>y</literal> one of the messages declared in the contract.</para>
</section>
<section class="lev2" id="sec1-2-4" label="1.2.4" xreflabel="1.2.4">
<title>Adding Recursion</title>
<para>Note that our <literal>Store</literal> can only manage the <literal>order</literal> of a single item: if some buyer wants to order two or more items, she has to use distinct instances of the store. We now extend the store so that it can receive several orders in the same session, adding all the items to a cart.</para>
<para>We start by refining our contract as follows:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog004.jpg" mime-subtype="jpeg"/></para>
<para>The contract <literal>Crec</literal> requires the store to accept from buyers two kinds of messages: <literal>addToCart</literal> and <literal>checkout</literal>. When a buyer chooses <literal>addToCart</literal>, the store must allow the buyer to order more items. This is done by recursively calling <literal>Crec</literal> in the <literal>addToCart</literal> branch. When a buyer stops adding items to the cart (by choosing <literal>checkout</literal>), the store must either send a <literal>price</literal> or state that the items are <literal>unavailable</literal>. In the first case, the store allows the buyer to <literal>accept</literal> the quotation and finalise the order, or to <literal>reject</literal> it and abort.</para>
<para>A possible specification of the store using the contract <literal>Crec</literal> is as follows:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog005.jpg" mime-subtype="jpeg"/></para>
<para>The store <literal>StoreRec</literal> advertises the contract <literal>Crec</literal>, and then continues as the process <literal>Loop(x)</literal>, where <literal>x</literal> is the handle to the new session. The process <literal>Loop(x)</literal> receives messages from buyers through session <literal>x</literal>. When it receives <literal>addToCart</literal>, it just calls itself recursively; instead, when it receives <literal>checkout</literal>, it calls the process <literal>Checkout</literal>. This process internally chooses whether to send the buyer a <literal>price</literal>, or to notify that the requested items are <literal>unavailable</literal>. In the first case, it receives from the client a confirmation, that can be either <literal>accept</literal> or <literal>reject</literal>.</para>
<para>A possible buyer interacting with <literal>StoreRec</literal> is the following:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog006.jpg" mime-subtype="jpeg"/></para>
<para>Note that the buyer&#x2019;s contract is compliant with <literal>Crec</literal>, even though the store contract is recursive, while the buyer&#x2019;s one is not.</para>
</section>
</section>
<section class="lev1" id="sec1-3" label="1.3" xreflabel="1.3">
<title>Honesty</title>
<para>In an ideal world, one would expect that services respect the contracts they advertise, in <emphasis>all</emphasis> execution contexts: we call <emphasis>honest</emphasis> such services. In this section we illustrate, through a series of examples, that writing honest services may be difficult and error-prone. Further, we show how our tools may help service designers in specifying and implementing honest services.</para>
<section class="lev2" id="sec1-3-1" label="1.3.1" xreflabel="1.3.1">
<title>A Simple Dishonest Store</title>
<para>Our first example is a na&#x00EF;ve CO<subscript>2</subscript> specification of the store advertising the contract <literal>C</literal> at page 5:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog007.jpg" mime-subtype="jpeg"/></para>
<para>The store above waits for an order of some item <literal>v</literal>. Then, it checks whether <literal>v</literal> is in stock (the actual test is abstracted by the <literal>*:boolean</literal> guard). If the item is in stock, the store sends a price quotation to the buyer (again, the price is abstracted in the specification).</para>
<para>Note that the store does nothing when the ordered item is not in stock. In this way, the store fails to respect its advertised contract <literal>C</literal>, which prescribes to always respond to the buyer by sending either <literal>price</literal> or <literal>unavailable</literal>. Therefore, we classify this specification of the store as <emphasis>dishonest</emphasis>.</para>
<para>In this paper we give an intuitive description of honesty, referring the reader to the literature [<link linkend="ch01_b6">6</link>, <link linkend="ch01_b7">7</link>] for a formal definition. A specification <literal>A</literal> is honest when, in all possible executions, if a contract at some session requires <literal>A</literal> to do some action, then <literal>A</literal> actually performs it. Basically, this boils down to say that when <literal>A</literal> is required to send a message, then it does so. Likewise, when <literal>A</literal> is required to receive a message, then <literal>A</literal> is ready to accept any message that its partner may be willing to send. More in detail:</para>
<itemizedlist mark="bullet" spacing="normal">
<listitem><para>if the contract is an <emphasis>internal</emphasis> choice <literal>a1!S1 (+) ... (+) an!Sn</literal>, then <literal>A</literal> must <literal>send</literal> a message having sort <literal>Si</literal>, and labelled <literal>ai</literal>, for some <literal>i</literal>;</para></listitem>
<listitem><para>if the contract is an <emphasis>external</emphasis> choice <literal>a1?S1 + ... + an?Sn</literal>, then <literal>A</literal> must be able to <literal>receive</literal> messages labelled with <emphasis>any</emphasis> labels <literal>ai</literal> in the choice (with the corresponding sorts <literal>Si</literal>).</para></listitem>
</itemizedlist>
<para>The honesty property discussed above can be automatically verified using the Diogenes honesty checker, which uses the verification technique described and implemented in [<link linkend="ch01_b6">6</link>]. This technique is built upon an abstract semantics of CO<subscript>2</subscript> which approximates both values (sent, received, and in conditional expressions) and the actual <emphasis>context</emphasis> wherein a specification is executed. Basically, the tool checks, through an exaustive exploration, that in every reachable state of the abstract semantics a participant is always able to perform some of the actions prescribed in each of her stipulated contracts. Since this is a branching-time property, a natural approach to verify it is by model checking. To this purpose we exploit a rewriting logic specification of the CO<subscript>2</subscript> abstract semantics and the Maude [<link linkend="ch01_b12">12</link>] search capabilities. This abstraction is a <emphasis>sound</emphasis> over-approximation of honesty: namely, if the abstraction of a specification is honest, then also the concrete one is honest. Further, the analysis is <emphasis>complete</emphasis> for specifications without conditional statements: i.e., if an abstracted specification is dishonest, then also its concrete counterpart is dishonest. If the abstractions are finite-state, we can verify their honesty by model checking a (finite) state space<footnote id="fn1_2" label="2"><para>Abstractions are finite-state in the fragment of CO<subscript>2</subscript> without delimitation/parallel under process definitions. For specifications outside this fragment the analysis is still correct, but it may diverge; indeed, a negative result [<link linkend="ch01_b9">9</link>] excludes the existence of algorithms for honesty that are at the same time sound, complete, and terminating in full CO<subscript>2</subscript>.</para></footnote>. Our implementation first translates a CO<subscript>2</subscript> specification into a Maude term [<link linkend="ch01_b12">12</link>], and then uses the Maude model checker to decide the honesty of its abstract semantics.</para>
<para>The honesty checker outputs the message below, that reports that the specification <literal>StoreDishonest1</literal> is <emphasis>dishonest</emphasis>. The reason for its dishonesty can be inferred from the following output:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog008.jpg" mime-subtype="jpeg"/></para>
<para>This shows a reachable (abstract) state of the specification, where <literal>$ 0</literal> denotes an open session between the store and a buyer.</para>
<para>The state consists of two parallel components: the state of the store</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog009.jpg" mime-subtype="jpeg"/></para>
<para>and the state of the contract at session <literal>$ 0</literal>, from the point of view of the store:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog010.jpg" mime-subtype="jpeg"/></para>
<para>Such contract requires the store to send either <literal>price</literal> or <literal>unavailable</literal> to the buyer. However, if the guard <literal>exp</literal> of the conditional (within the state of the store) evaluates to false, the store will not send any message to the buyer, so violating the contract <literal>C</literal>. Therefore, the honesty checker correctly classifies <literal>StoreDishonest1</literal> as dishonest.</para>
</section>
<section class="lev2" id="sec1-3-2" label="1.3.2" xreflabel="1.3.2">
<title>A More Complex Dishonest Store</title>
<para>We now consider a more evolved specification of the store, which relies on external distributors to retrieve items. The contract <literal>D</literal> specifies the interaction between the store and distributors:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog011.jpg" mime-subtype="jpeg"/></para>
<para>Namely, the store first sends a request to the distributor for some item, and then waits for an <literal>ok</literal> or <literal>no</literal> answer, according to whether the distributor is able to provide the requested item or not.</para>
<para>Our first attempt to specify a store interacting with customers and distributors is the following:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog012.jpg" mime-subtype="jpeg"/></para>
<para>At line <literal>2</literal>, the store advertises the contract <literal>C</literal>, and then waits until a session is established with some customer; when this happens, the variable <literal>x</literal> is bound to the session name. At line <literal>3</literal> the store waits to receive an <literal>order</literal>, binding it to the variable <literal>v</literal>. At line <literal>4</literal> the store advertises the contract <literal>D</literal> to establish a session <literal>y</literal> with a distributor; at line <literal>5</literal>, it sends a <literal>req</literal>uest with the value <literal>v</literal>. Finally, the store waits to receive a response <literal>ok</literal> or <literal>no</literal> from the distributor, and accordingly responds <literal>price</literal> or <literal>unavailable</literal> to the customer (lines <literal>6</literal>-<literal>9</literal>). The price <literal>*:int</literal> is a placeholder, to be replaced upon refinement.</para>
<para>The honesty checker classifies <literal>StoreDishonest2</literal> as <emphasis>dishonest</emphasis>. The reason for its dishonesty can be inferred from the following output:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog013.jpg" mime-subtype="jpeg"/></para>
<para>This output shows a possible (abstract) state which could be reached by <literal>StoreDishonest2</literal>. There, <literal>$ 0</literal> denotes an open session between the store and a buyer, while <literal>"y"</literal> indicates that no session between the store and a distributor is established, yet. The contract at session <literal>$ 0</literal> requires the store to send either a price or an unavailability message. However, in the given state there is no guarantee to find a distributor, hence the store might be stuck in the <literal>tell</literal>, never performing the required actions at session <literal>$ 0</literal>. Because of this, the store does not fulfil the contract <literal>C</literal>, hence it is correctly classified as dishonest.</para>
</section>
<section class="lev2" id="sec1-3-3" label="1.3.3" xreflabel="1.3.3">
<title>Handling Failures</title>
<para>We try to fix the specification <literal>StoreDishonest2</literal> by adapting it so to consider the case where the distributor is not available. Let us refine the specification <literal>StoreDishonest2</literal> as follows:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog014.jpg" mime-subtype="jpeg"/></para>
<para>Note that <literal>StoreDishonest3</literal> uses the construct <literal>tell</literal> <emphasis>&#x00B7;&#x00B7;&#x00B7;</emphasis> <literal>after</literal> <emphasis>&#x00B7;&#x00B7;&#x00B7;</emphasis> at lines <literal>4-10</literal>. This ensures that, if no session is established within a given deadline, then the contract is <emphasis>retracted</emphasis> (i.e., removed from the registry of available contracts), and the control passes to the <literal>after</literal> process. In particular, in our <literal>StoreDishonest3</literal>, if no distributor is found, then <literal>D</literal> is retracted, and the store performs its duties with the buyer by sending him <literal>unavailable</literal>. Since the actual deadline is immaterial in this specification, it is abstracted here as <literal>*</literal>.</para>
<para>By running the honesty checker on the amended specification, we obtain:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog015.jpg" mime-subtype="jpeg"/></para>
<para>Note that <literal>StoreDishonest3</literal> is still dishonest. The output above shows a reachable (abstract) state where the store has opened two sessions, <literal>$ 0</literal> and <literal>$ 1</literal>, with a buyer and a distributor, respectively. At session <literal>$ 0</literal> the store is expected to send either <literal>price</literal> or <literal>unavailable</literal> to the buyer. Now, the store can perform <literal>do $ 0 "price" ! int</literal> only <emphasis>after</emphasis> receiving the input from the distributor, i.e. after performing <literal>do $ 1 "ok" ? unit</literal>. Similarly, the store can only perform the action <literal>do $ 0 "unavailable" ! unit</literal> after the action <literal>do $ 1 "no" ? unit</literal>. Should the distributor fail to send either of these messages, then the store would fail to honour its contract <literal>C</literal> with the buyer. Therefore, the honesty checker correctly classifies <literal>StoreDishonest3</literal> as dishonest. Note that, even if in this case the distributor would be dishonest as well, (since it violates the contract <literal>D</literal> with the store), this does not excuse the store from violating the contract <literal>C</literal> with the buyer.</para>
</section>
<section class="lev2" id="sec1-3-4" label="1.3.4" xreflabel="1.3.4">
<title>An Honest Store, Finally</title>
<para>In order to address the dishonesty issues in the previous specification, we revise the store as follows:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog016.jpg" mime-subtype="jpeg"/></para>
<para>The main difference between this specification and the previous one is related to the <literal>receive</literal> at session <literal>y</literal>. At line <literal>9</literal>, <literal>after *</literal> represents the case in which no messages are received within a given timeout (immaterial in this specification). In such case, the store fulfils its contract at session <literal>x</literal>, by sending <literal>unavailable</literal> to the buyer. Further, the store also fulfils its contract at session <literal>y</literal>, by receiving any message that could still be sent from the distributor after the timeout.</para>
<para>Now the honesty checker correctly detects that the revised specification <literal>StoreHonest</literal> is honest.</para>
</section>
<section class="lev2" id="sec1-3-5" label="1.3.5" xreflabel="1.3.5">
<title>A Recursive Honest Store</title>
<para>We reprise the specification of <literal>StoreRec</literal> in Section 1.2, by providing a recursive store which interacts with buyers (via contract <literal>Crec</literal> at page 7) and with distributors (via contract <literal>D</literal>).</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog017.jpg" mime-subtype="jpeg"/></para>
<para>The specification <literal>StoreHonestRec</literal> handles the checkout of buyers in the process <literal>Checkout</literal>, which is identical to lines <literal>4-14</literal> in <literal>StoreHonest</literal>. The main difference with respect to <literal>StoreHonest</literal> is that <literal>StoreHonestRec</literal> can receive multiple requests from a buyer, via the recursive process <literal>Loop(x)</literal>. Despite this complication, the specification is still verified as honest by Diogenes.</para>
</section>
</section>
<section class="lev1" id="sec1-4" label="1.4" xreflabel="1.4">
<title>Refining CO<subscript>2</subscript> Specifications in Java Programs</title>
<para>Diogenes translates CO<subscript>2</subscript> specifications into Java skeletons, using the APIs of the contract-oriented middleware in [<link linkend="ch01_b3">3</link>]. This middleware collects the contracts advertised by services, establishes sessions between those with compliant contracts, and it allows services to send/receive messages through sessions, while monitoring this activity to detect and punish violations. More specifically, upon detection of a contract violation the middleware punishes the culprit, by suitably decreasing its <emphasis>reputation</emphasis>. This is a measure of the trustworthiness of a participant in its past interactions: the lower is the reputation, the lower is the probability of being able to establish new sessions with it.</para>
<section class="lev2" id="sec1-4-1" label="1.4.1" xreflabel="1.4.1">
<title>Compilation of CO<subscript>2</subscript> Specifications into Java Skeletons</title>
<para>We illustrate the translation of CO<subscript>2</subscript> specifications into Java through an example, the <literal>StoreHonest</literal> given in the previous section. From it, we obtain the following Java skeleton<footnote id="fn1_3" label="3"><para>Minor cosmetic changes are applied to improve readability.</para></footnote>:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog018.jpg" mime-subtype="jpeg"/></para>
<para>We comment below how the specification of <literal>StoreHonest</literal> at page 13 is rendered in Java.</para>
<itemizedlist mark="bullet" spacing="normal">
<listitem><para><literal>tell x C</literal> (at line <literal>2</literal>) is translated into the assignment</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog019.jpg" mime-subtype="jpeg"/></para>
<para>The API method <literal>tellAndWait</literal> advertises the contract <literal>C</literal> to the middle-ware, and blocks until a compliant buyer contract is found. Then, it returns a new object, representing the newly established session between the store and the buyer.</para></listitem>
<listitem><para><literal>receive @x order?[v:string]</literal> (at line <literal>3</literal>) is translated into</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog020.jpg" mime-subtype="jpeg"/></para>
<para>where the call to <literal>waitForReceive</literal> blocks until the store receives a message labelled <literal>order</literal> on session <literal>x</literal>.</para></listitem>
<listitem><para>The block <literal>tell y D ... after * ...</literal> (at lines <literal>4-17</literal>) is translated in Java as the try-catch statement:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog021.jpg" mime-subtype="jpeg"/></para>
<para>The call <literal>tellAndWait(D, timeoutP)</literal> advertises the contract <literal>D</literal>; the second parameter specifies a timeout (in milliseconds) to find a compliant contract. If the timeout expires, the contract <literal>D</literal> is retracted, and an exception is thrown. Then, the exception handler performs the recovery action specified in the <literal>after</literal> clause by sending <literal>unavailable</literal> to the client.</para></listitem>
<listitem><para><literal>send @y req![*:string]</literal> (at line <literal>5</literal>) is translated as</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog022.jpg" mime-subtype="jpeg"/></para>
<para>This method call sends a message labelled <literal>req</literal> at session <literal>y</literal>, blocking until this action is permitted by the contract.</para></listitem>
<listitem><para>The <literal>receive</literal> block at lines <literal>6-16</literal> is translated into a try-catch statement</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog023.jpg" mime-subtype="jpeg"/></para>
<para>The <literal>waitForReceive</literal> waits (until the given timeout) to receive on session <literal>y</literal> a message labelled either <literal>yes</literal> or <literal>no</literal>, throwing an exception in case the timeout expires. In such case, the <literal>catch</literal> block performs the recovery actions in the <literal>after</literal> clause of the specification. Namely, the service spawns two parallel processes, which send <literal>unavailable</literal> to the buyer, and receives late replies from the distributor.</para></listitem>
</itemizedlist>
<para>Note that the timeout values <literal>timeoutP</literal>, as well as the order price <literal>intP</literal>, are just placeholders. Further, in an actual implementation of the store service, we may want e.g. to read the order price from a file or a database. This can be done by refining the skeleton, introducing the needed code to make the service actually implement the desired functionality.</para>
</section>
<section class="lev2" id="sec1-4-2" label="1.4.2" xreflabel="1.4.2">
<title>Checking Honesty of Refined Java Programs</title>
<para>Note that when refining the skeleton into the actual Java application, programmers could accidentally break its honesty. In general, this happens when the refinement alters the interaction behaviour of the service. For instance, in an actual implementation of our store service, we may want to delegate the computation of <literal>price</literal> to a separated method, as follows:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog024.jpg" mime-subtype="jpeg"/></para>
<para>and change the placeholder <literal>intP</literal> at line <literal>15</literal> of the generated code with an invocation <literal>getOrderPrice(v)</literal>. The method could read the order price from a file or a database, and suppose that, in that method, each possible exception is either handled or re-thrown as <literal>MyException</literal>. If <literal>getOrderPrice</literal> throws an exception, then the <literal>sendIfAllowed()</literal> at line <literal>15</literal> is not performed. Unless the store performs it while handling <literal>MyException</literal>, the store violates the contract with the buyer, and so it becomes dishonest.</para>
<para>To address this issue, the Diogenes toolchain includes an <emphasis>honesty checker</emphasis> for Java programs, to be used after refinement. This honesty checker is built on top of <emphasis>Java PathFinder</emphasis> (JPF [<link linkend="ch01_b27">27</link>, <link linkend="ch01_b37">37</link>]). We define suitable <emphasis>listeners</emphasis> for JPF, to intercept the requests to the contract-oriented middleware, and to simulate <emphasis>all</emphasis> the possible responses that the application can receive from it. Through JPF we symbolically execute the program, in order to infer a CO<subscript>2</subscript> specification that abstracts its behaviour, preserving dishonesty. Once a specification is constructed in this way, we apply the CO<subscript>2</subscript> honesty checker discussed in Section 1.3 to establish the honesty of the Java program.</para>
<para>We can check the honesty of a Java program through the static method <literal>HonestyChecker.isHonest(StoreHonest.class)</literal>, which returns one of the following values:</para>
<itemizedlist mark="bullet" spacing="normal">
<listitem><para><literal>HONEST</literal>: the tool has inferred a CO<subscript>2</subscript> specification and verified its honesty;</para></listitem>
<listitem><para><literal>UNKNOWN</literal>: the tool has been unable to infer a CO<subscript>2</subscript> specification, e.g. because of unhandled exceptions within the class under test.</para></listitem>
</itemizedlist>
<para>In our example, we just provide the following stub implementation of the method <literal>getOrderPrice</literal>:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog025.jpg" mime-subtype="jpeg"/></para>
<para>where the annotation <literal>@SkipMethod</literal> is interpreted by the honesty checker as follows: assume that the method terminates (possibly throwing one of the declared exceptions), and it does not interact with the contract-oriented middleware. For our refined store, the honesty checker returns <literal>UNKNOWN</literal>, outputting:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog026.jpg" mime-subtype="jpeg"/></para>
<para>As anticipated above, this output remarks that if <literal>getOrderAmount</literal> throws an exception, then the store is dishonest.</para>
<para>Asafirst(na&#x00EF;ve) attempt to recover honesty, we further refine the store by catching <literal>MyException</literal>, and just logging the error in the exception handler:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog027.jpg" mime-subtype="jpeg"/></para>
<para>In this case, the honesty checker correctly classifies the store as <literal>DISHONEST</literal>, producing the following output:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog028.jpg" mime-subtype="jpeg"/></para>
<para>This output highlights the reason for dishonesty: <literal>StoreHonest[0]</literal> means that the store does nothing, while at session <literal>$ 0</literal>, it should send either <literal>price</literal> or <literal>unavailable</literal> to the buyer.</para>
<para>To recover honesty, rather than just logging the error, we also perform <literal>x.sendIfAllowed("unavailable")</literal> in the exception handler, in order to fulfil the contract with the buyer:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog029.jpg" mime-subtype="jpeg"/></para>
<para>With this modification, the Java honesty checker correctly outputs <literal>HONEST</literal>.</para>
</section>
</section>
<section class="lev1" id="sec1-5" label="1.5" xreflabel="1.5">
<title>Conclusions</title>
<para>We have presented Diogenes, a toolchain for the specification and verification of contract-oriented services. Diogenes fills a gap between foundational research on honesty [<link linkend="ch01_b6">6</link>&#x2013;<link linkend="ch01_b9">9</link>] and more practical research on contract-oriented programming [<link linkend="ch01_b3">3</link>]. Our tools can help service designers to write specifications, check their adherence to contracts (i.e., their honesty), generate Java skeletons, and refine them while preserving honesty. We have experimented Diogenes with a set of case studies (more complex than the ones presented in this tutorial); our case studies are available at <literal>co2.unica.it/diogenes</literal>.</para>
<para>The effectiveness of our tools could be improved in several ways, ranging from the precision of the analysis, to the informative quality of output messages provided by the honesty checkers.</para>
<para>The precision of the honesty analysis could be improved e.g., by implementing the type checking technique of [<link linkend="ch01_b7">7</link>], which extends the class of infinite-state processes for which honesty can be verified. More specifically, the type system in [<link linkend="ch01_b7">7</link>] can also handle some processes with delimitation and parallel composition under recursion.</para>
<para>Another form of improvement would be to extend the formalism and the analysis to deal with timing constraints. This could be done e.g. by exploiting the timed version of CO<subscript>2</subscript> [<link linkend="ch01_b3">3</link>] and timed session types [<link linkend="ch01_b2">2</link>]. Although the current analysis for honesty does not consider timing constraints (and therefore is unsound in such scenario), it can still give useful feedback when applied to timed specifications. For instance, it could detect that some prescribed actions cannot be performed because the actions they depend on may be blocked by an unresponsive context.</para>
<para>When a specification/program is found dishonest, it would be helpful for programmers to know which parts of it is responsible for contract violations. The error reporting facilities of Diogenes could be improved to this purpose: this would require e.g., to signal what are the contract obligations that are not fulfilled, and in what session, and in particular which part of the specification/program should be fixed. Further, it would be useful to suggest possible corrections to the designer.</para>
<para>Another direction for future work is to formally establish relations between the original CO<subscript>2</subscript> specification and the refined Java code. In fact, our tools can only check that the user-refined Java code obtained from an honest CO<subscript>2</subscript> specification is honest, but this does not imply that the refined Java code still &#8220;adheres&#8221; to the specification. Indeed, improper refinements could drastically modify the interaction behaviour of a service, e.g. by removing some contract advertisements &#x2014; while preserving honesty. An additional static analysis could establish that the CO<subscript>2</subscript> process inferred from the user-refined Java code is behaviourally related to the original specification. An alternative way to cope with this issue would be to enhance the generation of the skeletal Java program, by providing a more structured class hierarchy. More precisely, we could avoid accidental breaches of honesty by separating, in the generated skeleton, the part that handles the interactions from the parts to be refined. This could be done e.g. by inserting entry points to invoke classes/interfaces whose behaviour is defined apart, so separating the application logic and simplifying possible updates in the specifications.</para>
<section class="lev2" id="sec1-5-1" label="1.5.1" xreflabel="1.5.1">
<title>Related Work</title>
<para>In recent years many works have addressed the safe design of service-oriented applications. A notable approach is to specify the overall communication behaviour of an application through a <emphasis>choreography</emphasis>, which validates some global properties of the application (e.g. safety, deadlock-freedom, <emphasis>etc.</emphasis>). To ensure that the application enjoys such properties, all the components forming the application have to be verified; this can be done e.g. by projecting the choreography to end-point views, against which these components are verified [<link linkend="ch01_b35">35</link>, <link linkend="ch01_b21">21</link>]. Examples of how to embody such approach in existing programming languages and models are presented for <literal>C</literal> [<link linkend="ch01_b33">33</link>], for <literal>Python</literal> [<link linkend="ch01_b30">30</link>], and for the actor model [<link linkend="ch01_b31">31</link>]. All those approaches are based on <literal>Scribble</literal> [<link linkend="ch01_b38">38</link>], a protocol description language featuring multiparty session types [<link linkend="ch01_b21">21</link>]. The strict relations between multiparty session types and actor-based models such as communicating machines [<link linkend="ch01_b15">15</link>] has been used to develop a framework to monitor <literal>Erlang</literal> applications [<link linkend="ch01_b18">18</link>].</para>
<para>This top-down approach assumes that designers control the whole application, e.g., they develop all the needed components. However, in many real-world scenarios several components are developed independently, without knowing at design time which other components they will be integrated with. In these scenarios, the compositional verification pursued by the top-down approach is not immediately applicable, because the choreography is usually unknown, and even if it were known, only a subset of the needed components is available for verification. However, this issue can be mitigated when the communication pattern of each component is available. In fact, in such case if the set of components is compatible, it is possible to synthesise a faithful choreography [<link linkend="ch01_b26">26</link>] with a suitable tool [<link linkend="ch01_b24">24</link>]. Such choreography can then be used to distil monitors for the components that are not trusted (if any). The ideas pursued in this paper depart from the top-down approach, because designers can advertise contracts to discover the needed components (and so ours can be considered a <emphasis>bottom-up</emphasis> approach). Coherently, the main property we are interested in is <emphasis>honesty</emphasis>, which is a property of components, and not of global applications. Some works mixing top-down and bottom-up composition have been proposed in the past few years [<link linkend="ch01_b5">5</link>, <link linkend="ch01_b16">16</link>, <link linkend="ch01_b25">25</link>]. Recent works [<link linkend="ch01_b32">32</link>] have explored how to integrate the bottom-up approach with inference of multiparty session types from <literal>GO</literal> programs.</para>
<para>The problem of ensuring safe interactions in session-based systems has been addressed by many authors [<link linkend="ch01_b10">10</link>, <link linkend="ch01_b11">11</link>, <link linkend="ch01_b13">13</link>, <link linkend="ch01_b14">14</link>, <link linkend="ch01_b17">17</link>, <link linkend="ch01_b19">19</link>, <link linkend="ch01_b21">21</link>&#x2013;<link linkend="ch01_b23">23</link>, <link linkend="ch01_b36">36</link>]. When processes have a single session, our notion of honesty is close (yet different) to session typeability. A technical difference is that we admit processes to attempt interactions which are not mandated by the contract. E.g., the process:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog030.jpg" mime-subtype="jpeg"/></para>
<para>is honest, while it would <emphasis>not</emphasis> be typeable according to most works on session types, because the action <literal>b</literal> is not immediately mandated by the contract.</para>
<para>Other, more substantial, differences between honesty and session typing arise when processes have more than one session. More specifically, we consider a process to be honest when it enjoys progress in <emphasis>all</emphasis> possible contexts, while most works on session typing guarantee progress in a given context. For instance, consider the process:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog031.jpg" mime-subtype="jpeg"/></para>
<para>We have that <literal>Q</literal> is <emphasis>not</emphasis> honest, because the action at session <literal>x</literal> is not possible if the participant at the other endpoint of session <literal>y</literal> does not send <literal>b</literal>. Note instead that <literal>Q</literal> would be well-typed in [<link linkend="ch01_b20">20</link>], even if some contexts <literal>R</literal> can lead <literal>Q</literal> to a deadlock. The interaction type system in [<link linkend="ch01_b14">14</link>] would allow to check the progress of <literal>Q|R</literal>, given a context <literal>R</literal>.</para>
<para><emphasis role="strong">Acknowledgments</emphasis> This work has been partially supported by Aut. Reg. of Sardinia P.I.A. 2013 &#8220;NOMAD&#8221;, and by EU COST Action IC1201 &#8220;Behavioural Types for Reliable Large-Scale Software Systems&#8221; (BETTY).</para>
</section>
</section>
<section class="lev1" id="sec2a">
<title>References</title>
<orderedlist numeration="arabic" continuation="restarts" spacing="normal">
<listitem><para><anchor id="ch01_b1"/>Nicola Atzei and Massimo Bartoletti. Developing honest Java programs with Diogenes. In <emphasis>Formal Techniques for Distributed Objects, Components, and Systems (FORTE)</emphasis>, volume 9688 of <emphasis>LNCS</emphasis>, pages 52&#x2013;61. Springer, 2016. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Nicola+Atzei+and+Massimo+Bartoletti%2E+Developing+honest+Java+programs+with+Diogenes%2E+In+Formal+Techniques+for+Distributed+Objects%2C+Components%2C+and+Systems+%28FORTE%29%2C+volume+9688+of+LNCS%2C+pages+52-61%2E+Springer%2C+2016%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch01_b2"/>Massimo Bartoletti, Tiziana Cimoli, Maurizio Murgia, Alessandro Sebastian Podda, and Livio Pompianu. Compliance and subtyping in timed session types. In <emphasis>Formal Techniques for Distributed Objects, Components, and Systems (FORTE)</emphasis>, volume 9039 of <emphasis>LNCS</emphasis>, pages 161&#x2013;177. Springer, 2015. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Massimo+Bartoletti%2C+Tiziana+Cimoli%2C+Maurizio+Murgia%2C+Alessandro+Sebastian+Podda%2C+and+Livio+Pompianu%2E+Compliance+and+subtyping+in+timed+session+types%2E+In+Formal+Techniques+for+Distributed+Objects%2C+Components%2C+and+Systems+%28FORTE%29%2C+volume+9039+of+LNCS%2C+pages+161-177%2E+Springer%2C+2015%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch01_b3"/>Massimo Bartoletti, Tiziana Cimoli, Maurizio Murgia, Alessandro Sebastian Podda, and Livio Pompianu. A contract-oriented middleware. In <emphasis>Formal Aspects of Component Software (FACS)</emphasis>, volume 9539 of <emphasis>LNCS</emphasis>, pages 86&#x2013;104. Springer, 2015. http://co2.unica.it<literal>co2.unica.it</literal> <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Massimo+Bartoletti%2C+Tiziana+Cimoli%2C+Maurizio+Murgia%2C+Alessandro+Sebastian+Podda%2C+and+Livio+Pompianu%2E+A+contract-oriented+middleware%2E+In+Formal+Aspects+of+Component+Software+%28FACS%29%2C+volume+9539+of+LNCS%2C+pages+86-104%2E+Springer%2C+2015%2E+http%3A%2F%2Fco2%2Eunica%2Eitco2%2Eunica%2Eit" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch01_b4"/>Massimo Bartoletti, Tiziana Cimoli, and Roberto Zunino. Compliance in behavioural contracts: a brief survey. In <emphasis>Programming Languages with Applications to Biology and Security</emphasis>, volume 9465 of <emphasis>LNCS</emphasis>, pages 103&#x2013;121. Springer, 2015. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Massimo+Bartoletti%2C+Tiziana+Cimoli%2C+and+Roberto+Zunino%2E+Compliance+in+behavioural+contracts%3A+a+brief+survey%2E+In+Programming+Languages+with+Applications+to+Biology+and+Security%2C+volume+9465+of+LNCS%2C+pages+103-121%2E+Springer%2C+2015%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch01_b5"/>Massimo Bartoletti, Julien Lange, Alceste Scalas, and Roberto Zunino. Choreographies in the wild. <emphasis>Science of Computer Programming</emphasis>, 109: 36&#x2013;60, 2015. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Massimo+Bartoletti%2C+Julien+Lange%2C+Alceste+Scalas%2C+and+Roberto+Zunino%2E+Choreographies+in+the+wild%2E+Science+of+Computer+Programming%2C+109%3A+36-60%2C+2015%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch01_b6"/>Massimo Bartoletti, Maurizio Murgia, Alceste Scalas, and Roberto Zunino. Verifiable abstractions for contract-oriented systems. <emphasis>Journal of Logical and Algebraic Methods in Programming (JLAMP)</emphasis>, 86:159&#x2013;207, 2017. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Massimo+Bartoletti%2C+Maurizio+Murgia%2C+Alceste+Scalas%2C+and+Roberto+Zunino%2E+Verifiable+abstractions+for+contract-oriented+systems%2E+Journal+of+Logical+and+Algebraic+Methods+in+Programming+%28JLAMP%29%2C+86%3A159-207%2C+2017%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch01_b7"/>Massimo Bartoletti, Alceste Scalas, Emilio Tuosto, and Roberto Zunino. Honesty by typing. <emphasis>Logical Methods in Computer Science</emphasis>, 12(4), 2016. Pre-print available as: <literal>https://arxiv.org/abs/1211.2609</literal> <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Massimo+Bartoletti%2C+Alceste+Scalas%2C+Emilio+Tuosto%2C+and+Roberto+Zunino%2E+Honesty+by+typing%2E+Logical+Methods+in+Computer+Science%2C+12%284%29%2C+2016%2E+Pre-print+available+as%3A+https%3A%2F%2Farxiv%2Eorg%2Fabs%2F1211%2E2609" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch01_b8"/>Massimo Bartoletti, Emilio Tuosto, and Roberto Zunino. Contract-oriented computing in CO<subscript>2</subscript>. <emphasis>Sci. Ann. Comp. Sci.</emphasis>, 22(1):5&#x2013;60, 2012. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Massimo+Bartoletti%2C+Emilio+Tuosto%2C+and+Roberto+Zunino%2E+Contract-oriented+computing+in+CO2%2E+Sci%2E+Ann%2E+Comp%2E+Sci%2E%2C+22%281%29%3A5-60%2C+2012%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch01_b9"/>Massimo Bartoletti and Roberto Zunino. On the decidability of honesty and of its variants. In <emphasis>Web Services, Formal Methods, and Behavioral Types</emphasis>, volume 9421 of <emphasis>LNCS</emphasis>, pages 143&#x2013;166. Springer, 2015.</para></listitem>
<listitem><para><anchor id="ch01_b10"/>Lorenzo Bettini, Mario Coppo, Loris D&#x2019;Antoni, Marco De Luca, Mariangiola Dezani-Ciancaglini, and Nobuko Yoshida. Global progress in dynamically interleaved multiparty sessions. In <emphasis>CONCUR</emphasis>, volume 5201 of <emphasis>LNCS</emphasis>, pages 418&#x2013;433. Springer, 2008. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Lorenzo+Bettini%2C+Mario+Coppo%2C+Loris+D%27Antoni%2C+Marco+De+Luca%2C+Mariangiola+Dezani-Ciancaglini%2C+and+Nobuko+Yoshida%2E+Global+progress+in+dynamically+interleaved+multiparty+sessions%2E+In+CONCUR%2C+volume+5201+of+LNCS%2C+pages+418-433%2E+Springer%2C+2008%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch01_b11"/>Giuseppe Castagna, Mariangiola Dezani-Ciancaglini, Elena Giachino, and Luca Padovani. Foundations of session types. In <emphasis>ACM SIGPLAN Conference on Principles and Practice of Declarative Programming (PPDP)</emphasis>, pages 219&#x2013;230. ACM, 2009. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Giuseppe+Castagna%2C+Mariangiola+Dezani-Ciancaglini%2C+Elena+Giachino%2C+and+Luca+Padovani%2E+Foundations+of+session+types%2E+In+ACM+SIGPLAN+Conference+on+Principles+and+Practice+of+Declarative+Programming+%28PPDP%29%2C+pages+219-230%2E+ACM%2C+2009%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch01_b12"/>Manuel Clavel, Francisco Dur&#x00E1;n, Steven Eker, Patrick Lincoln, Narciso Mart&#x00ED;-Oliet, Jos&#x00E9; Meseguer, and Jos&#x00E9; F. Quesada. Maude: Specification and programming in rewriting logic. <emphasis>TCS</emphasis>, 2001. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Manuel+Clavel%2C+Francisco+Dur%E1n%2C+Steven+Eker%2C+Patrick+Lincoln%2C+Narciso+Mart%ED-Oliet%2C+Jos%E9+Meseguer%2C+and+Jos%E9+F%2E+Quesada%2E+Maude%3A+Specification+and+programming+in+rewriting+logic%2E+TCS%2C+2001%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch01_b13"/>Mario Coppo, Mariangiola Dezani-Ciancaglini, Luca Padovani, and Nobuko Yoshida. Inference of global progress properties for dynamically interleaved multiparty sessions. In <emphasis>COORDINATION</emphasis>, volume 7890 of <emphasis>LNCS</emphasis>, pages 45&#x2013;59. Springer, 2013. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Mario+Coppo%2C+Mariangiola+Dezani-Ciancaglini%2C+Luca+Padovani%2C+and+Nobuko+Yoshida%2E+Inference+of+global+progress+properties+for+dynamically+interleaved+multiparty+sessions%2E+In+COORDINATION%2C+volume+7890+of+LNCS%2C+pages+45-59%2E+Springer%2C+2013%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch01_b14"/>Mario Coppo, Mariangiola Dezani-Ciancaglini, Nobuko Yoshida, and Luca Padovani. Global progress for dynamically interleaved multiparty sessions. <emphasis>Mathematical Structures in Computer Science</emphasis>, 26(2):238&#x2013; 302, 2016.</para></listitem>
<listitem><para><anchor id="ch01_b15"/>Pierre-Malo Deni&#x00E9;lou and Nobuko Yoshida. Multiparty session types meet communicating automata. In <emphasis>European Symposium on Programming (ESOP)</emphasis>, volume 7211 of <emphasis>LNCS</emphasis>, pages 194&#x2013;213. Springer, 2012. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Pierre-Malo+Deni%E9lou+and+Nobuko+Yoshida%2E+Multiparty+session+types+meet+communicating+automata%2E+In+European+Symposium+on+Programming+%28ESOP%29%2C+volume+7211+of+LNCS%2C+pages+194-213%2E+Springer%2C+2012%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch01_b16"/>Pierre-Malo Deni&#x00E9;lou and Nobuko Yoshida. Multiparty compatibility in communicating automata: Characterisation and synthesis of global session types. In <emphasis>International Colloquium on Automata, Languages, and Programming (ICALP)</emphasis>, volume 7966 of <emphasis>LNCS</emphasis>, pages 174&#x2013;186. Springer, 2013. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Pierre-Malo+Deni%E9lou+and+Nobuko+Yoshida%2E+Multiparty+compatibility+in+communicating+automata%3A+Characterisation+and+synthesis+of+global+session+types%2E+In+International+Colloquium+on+Automata%2C+Languages%2C+and+Programming+%28ICALP%29%2C+volume+7966+of+LNCS%2C+pages+174-186%2E+Springer%2C+2013%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch01_b17"/>Mariangiola Dezani-Ciancaglini, Ugo de&#x2019;Liguoro, and Nobuko Yoshida. On progress for structured communications. In <emphasis>Trustworthy Global Computing (TGC)</emphasis>, volume 4912 of <emphasis>LNCS</emphasis>, pages 257&#x2013;275. Springer, 2007.</para></listitem>
<listitem><para><anchor id="ch01_b18"/>Simon Fowler. An Erlang implementation of multiparty session actors. In <emphasis>Interaction and Concurrency Experience</emphasis>, volume 223 of <emphasis>EPTCS</emphasis>, pages 36&#x2013;50, 2016. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Simon+Fowler%2E+An+Erlang+implementation+of+multiparty+session+actors%2E+In+Interaction+and+Concurrency+Experience%2C+volume+223+of+EPTCS%2C+pages+36-50%2C+2016%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch01_b19"/>Kohei Honda, Vasco T. Vasconcelos, and Makoto Kubo. Language primitives and type disciplines for structured communication-based programming. In <emphasis>European Symposium on Programming (ESOP)</emphasis>, volume 1381 of <emphasis>LNCS</emphasis>, pages 22&#x2013;138. Springer, 1998.</para></listitem>
<listitem><para><anchor id="ch01_b20"/>Kohei Honda, Nobuko Yoshida, and Marco Carbone. Multiparty asynchronous session types. In <emphasis>ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL)</emphasis>, pages 273&#x2013;284. ACM, 2008. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Kohei+Honda%2C+Nobuko+Yoshida%2C+and+Marco+Carbone%2E+Multiparty+asynchronous+session+types%2E+In+ACM+SIGPLAN-SIGACT+Symposium+on+Principles+of+Programming+Languages+%28POPL%29%2C+pages+273-284%2E+ACM%2C+2008%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch01_b21"/>Kohei Honda, Nobuko Yoshida, and Marco Carbone. Multiparty asynchronous session types. <emphasis>J. ACM</emphasis>, 63(1):9:1&#x2013;9:67, 2016. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Kohei+Honda%2C+Nobuko+Yoshida%2C+and+Marco+Carbone%2E+Multiparty+asynchronous+session+types%2E+J%2E+ACM%2C+63%281%29%3A9%3A1-9%3A67%2C+2016%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch01_b22"/>Hans H&#x00FC;ttel, Ivan Lanese, Vasco T. Vasconcelos, Lu&#x00ED;s Caires, Marco Carbone, Pierre-Malo Deni&#x00E9;lou, Dimitris Mostrous, Luca Padovani, Ant&#x00F3;nio Ravara, Emilio Tuosto, Hugo Torres Vieira, and Gianluigi Zavattaro. Foundations of session types and behavioural contracts. <emphasis>ACM Comput. Surv.</emphasis>, 49(1):3:1&#x2013;3:36, 2016.</para></listitem>
<listitem><para><anchor id="ch01_b23"/>Naoki Kobayashi. A new type system for deadlock-free processes. In <emphasis>Proc. CONCUR</emphasis>, volume 4137 of <emphasis>LNCS</emphasis>, pages 233&#x2013;247. Springer, 2006. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Naoki+Kobayashi%2E+A+new+type+system+for+deadlock-free+processes%2E+In+Proc%2E+CONCUR%2C+volume+4137+of+LNCS%2C+pages+233-247%2E+Springer%2C+2006%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch01_b24"/>Julien Lange and Emilio Tuosto. A toolchain for choreography-based analysis of application level protocols. Available at <literal>https: //bitbucket.org/emlio_tuosto/gmc-synthesis-v0.2</literal> <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Julien+Lange+and+Emilio+Tuosto%2E+A+toolchain+for+choreography-based+analysis+of+application+level+protocols%2E+Available+at+https%3A+%2F%2Fbitbucket%2Eorg%2Femlio%5Ftuosto%2Fgmc-synthesis-v0%2E2" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch01_b25"/>Julien Lange and Emilio Tuosto. Synthesising choreographies from local session types. In <emphasis>CONCUR</emphasis>, volume 7454 of <emphasis>LNCS</emphasis>, pages 225&#x2013;239. Springer, 2012. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Julien+Lange+and+Emilio+Tuosto%2E+Synthesising+choreographies+from+local+session+types%2E+In+CONCUR%2C+volume+7454+of+LNCS%2C+pages+225-239%2E+Springer%2C+2012%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch01_b26"/>Julien Lange, Emilio Tuosto, and Nobuko Yoshida. From communicating machines to graphical choreographies. In <emphasis>ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL)</emphasis>, pages 221&#x2013;232, 2015. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Julien+Lange%2C+Emilio+Tuosto%2C+and+Nobuko+Yoshida%2E+From+communicating+machines+to+graphical+choreographies%2E+In+ACM+SIGPLAN-SIGACT+Symposium+on+Principles+of+Programming+Languages+%28POPL%29%2C+pages+221-232%2C+2015%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch01_b27"/>Flavio Lerda and Willem Visser. Addressing dynamic issues of program model checking. In <emphasis>SPIN workshop on Model checking of software</emphasis>, pages 80&#x2013;102, 2001. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Flavio+Lerda+and+Willem+Visser%2E+Addressing+dynamic+issues+of+program+model+checking%2E+In+SPIN+workshop+on+Model+checking+of+software%2C+pages+80-102%2C+2001%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch01_b28"/>Robin Milner. <emphasis>Communication and concurrency</emphasis>. Prentice-Hall, Inc., 1989.</para></listitem>
<listitem><para><anchor id="ch01_b29"/>A. Mukhija, Andrew Dingwall-Smith, and D.S. Rosenblum. QoS-aware service composition in Dino. In <emphasis>ECOWS</emphasis>, volume 5900 of <emphasis>LNCS</emphasis>, pages 3&#x2013;12. Springer, 2007. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=A%2E+Mukhija%2C+Andrew+Dingwall-Smith%2C+and+D%2ES%2E+Rosenblum%2E+QoS-aware+service+composition+in+Dino%2E+In+ECOWS%2C+volume+5900+of+LNCS%2C+pages+3-12%2E+Springer%2C+2007%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch01_b30"/>Rumyana Neykova. Session types go dynamic or how to verify your Python conversations. In <emphasis>Workshop on Programming Language Approaches to Concurrency and Communication-cEntric Software (PLACES)</emphasis>, volume 137 of <emphasis>EPTCS</emphasis>, pages 95&#x2013;102, 2013. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Rumyana+Neykova%2E+Session+types+go+dynamic+or+how+to+verify+your+Python+conversations%2E+In+Workshop+on+Programming+Language+Approaches+to+Concurrency+and+Communication-cEntric+Software+%28PLACES%29%2C+volume+137+of+EPTCS%2C+pages+95-102%2C+2013%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch01_b31"/>Rumyana Neykova and Nobuko Yoshida. Multiparty session actors. In <emphasis>COORDINATION</emphasis>, volume 8459 of <emphasis>LNCS</emphasis>, pages 131&#x2013;146. Springer, 2014. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Rumyana+Neykova+and+Nobuko+Yoshida%2E+Multiparty+session+actors%2E+In+COORDINATION%2C+volume+8459+of+LNCS%2C+pages+131-146%2E+Springer%2C+2014%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch01_b32"/>Nicholas Ng and Nobuko Yoshida. Static deadlock detection for concurrent go by global session graph synthesis. In <emphasis>International Conference on Compiler Construction (CC)</emphasis>, pages 174&#x2013;184. ACM, 2016. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Nicholas+Ng+and+Nobuko+Yoshida%2E+Static+deadlock+detection+for+concurrent+go+by+global+session+graph+synthesis%2E+In+International+Conference+on+Compiler+Construction+%28CC%29%2C+pages+174-184%2E+ACM%2C+2016%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch01_b33"/>Nicholas Ng, Nobuko Yoshida, and Kohei Honda. Multiparty session C: safe parallel programming with message optimisation. In <emphasis>Objects, Models, Components, Patterns (TOOLS)</emphasis>, pages 202&#x2013;218, 2012. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Nicholas+Ng%2C+Nobuko+Yoshida%2C+and+Kohei+Honda%2E+Multiparty+session+C%3A+safe+parallel+programming+with+message+optimisation%2E+In+Objects%2C+Models%2C+Components%2C+Patterns+%28TOOLS%29%2C+pages+202-218%2C+2012%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch01_b34"/>Kaku Takeuchi, Kohei Honda, and Makoto Kubo. An interaction-based language and its typing system. In <emphasis>PARLE</emphasis>, pages 398&#x2013;413, 1994. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Kaku+Takeuchi%2C+Kohei+Honda%2C+and+Makoto+Kubo%2E+An+interaction-based+language+and+its+typing+system%2E+In+PARLE%2C+pages+398-413%2C+1994%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch01_b35"/>Wil M. P. van der Aalst, Niels Lohmann, Peter Massuthe, Christian Stahl, and Karsten Wolf. Multiparty contracts: Agreeing and implementing interorganizational processes. <emphasis>Comput. J.</emphasis>, 53(1):90&#x2013;106, 2010. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Wil+M%2E+P%2E+van+der+Aalst%2C+Niels+Lohmann%2C+Peter+Massuthe%2C+Christian+Stahl%2C+and+Karsten+Wolf%2E+Multiparty+contracts%3A+Agreeing+and+implementing+interorganizational+processes%2E+Comput%2E+J%2E%2C+53%281%29%3A90-106%2C+2010%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch01_b36"/>V. T. Vasconcelos. Fundamentals of Session Types. <emphasis>Information and Computation</emphasis>, 217:52&#x2013;70, 2012. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=V%2E+T%2E+Vasconcelos%2E+Fundamentals+of+Session+Types%2E+Information+and+Computation%2C+217%3A52-70%2C+2012%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch01_b37"/>Willem Visser, Klaus Havelund, Guillaume Brat, SeungJoon Park, and Flavio Lerda. Model checking programs. <emphasis>Automated Software Engineering</emphasis>, 10(2):203&#x2013;232, 2003. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Willem+Visser%2C+Klaus+Havelund%2C+Guillaume+Brat%2C+SeungJoon+Park%2C+and+Flavio+Lerda%2E+Model+checking+programs%2E+Automated+Software+Engineering%2C+10%282%29%3A203-232%2C+2003%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch01_b38"/>Nobuko Yoshida, Raymond Hu, Rumyana Neykova, and Nicholas Ng. The Scribble protocol language. In <emphasis>Trustworthy Global Computing (TGC)</emphasis>, volume 8358 of <emphasis>LNCS</emphasis>, pages 22&#x2013;41. Springer, 2013. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Nobuko+Yoshida%2C+Raymond+Hu%2C+Rumyana+Neykova%2C+and+Nicholas+Ng%2E+The+Scribble+protocol+language%2E+In+Trustworthy+Global+Computing+%28TGC%29%2C+volume+8358+of+LNCS%2C+pages+22-41%2E+Springer%2C+2013%2E" target="_blank">Google Scholar</ulink></para></listitem>
</orderedlist>
</section>
</chapter>
<chapter class="chapter" id="ch02" label="2" xreflabel="2">
<title>Contract-Oriented Programming with Timed Session Types</title>
<para><emphasis role="strong">Nicola Atzei, Massimo Bartoletti, Tiziana Cimoli, Stefano Lande, Maurizio Murgia, Alessandro Sebastian Podda and Livio Pompianu</emphasis></para>
<para>University of Cagliari, Italy</para>
<section class="lev2">
<title>Abstract</title>
<para>Contract-oriented programming is a software engineering paradigm which proposes the use of behavioural contracts to discipline the interaction among software components. In a distributed setting, the various components of an application may be developed and run by untrustworthy parties, which could opportunistically diverge from the expected behaviour when they find it convenient. The use of contracts in this setting is essential: by binding the behaviour of each component to a contract, and by sanctioning contract violations, components are incentivized to behave in a correct and cooperative manner.</para>
<para>This chapter is a step-by-step tutorial on programming contract-oriented distributed applications. The glue between components is a middleware which establishes sessions between services with compliant contracts, and monitors sessions to detect and punish violations. Contracts are formalised as timed session types, which describe timed communication protocols between two components at the endpoints of a session. We illustrate some basic primitives of contract-oriented programming: advertising contracts, performing contractual actions, and dealing with violations. We then show how to exploit these primitives to develop some small distributed applications.</para>
</section>
<section class="lev1" id="sec2-1" label="2.1" xreflabel="2.1">
<title>Introduction</title>
<para>Developing trustworthy distributed applications can be a challenging task. A key issue is that the services that compose a distributed application may be under the governance of different providers, which may compete against each other. Furthermore, services interact through open networks, where competitors and adversaries can try to exploit their vulnerabilities.</para>
<para>A possible countermeasure to these issues is to use <emphasis>behavioural contracts</emphasis> to discipline the interaction among services. These are formal descriptions of service behaviour, which can be used at static or dynamic time to discover and bind services, and to guarantee that they interact in a protected manner: namely, when a service does not behave as prescribed by its contract, it can be blamed and sanctioned for a contract breach.</para>
<para>In previous work [<link linkend="ch02_b7">7</link>] we presented a middleware that uses behavioural contracts to discipline the interactions among distrusting services. Since it supports the COntract-Oriented paradigm, we called it &#x201C;CO<subscript>2</subscript> middleware&#x201D;.</para>
<para><link linkend="fig2_1">Figure <xref linkend="fig2_1" remap="2.1"/></link> illustrates the main features of the CO<subscript>2</subscript> middleware. In (1), the participant A advertises its contract to the middleware, making it available to other participants. In (2), the middleware determines that the contracts of A and B are <emphasis>compliant</emphasis>: this means that interactions which respect the contracts are deadlock-free. Upon compliance, the middleware establishes a session through which the two participants can interact. This interaction consists of sending and receiving messages, similarly to a standard message-oriented middleware (MOM): for instance, in (3) participant A delivers to the middleware a message for B, which can then collect it from the middleware.</para>
<fig id="fig2_1" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 2.1</label>
<caption><title>Contract-oriented interactions in the CO<subscript>2</subscript> middleware.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig001.jpg" mime-subtype="jpeg"/>
</fig>
<para>Unlike standard MOMs, the interaction happening in each session is monitored by the middleware, which checks whether contracts are respected or not. In particular, the execution monitor verifies that actions occur when prescribed by their contracts, and it detects when some expected action is missing. For instance, in (4) the execution monitor has detected an attempt of participant B to do some illegal action. Upon detection of a contract violation, the middleware punishes the culprit, by suitably decreasing its <emphasis>reputation</emphasis>. This is a measure of the trustworthiness of a participant in its past interactions: the lower its reputation is, the lower the probability of being able to establish new sessions with it.</para>
<para>Item (5) shows another mechanism for establishing sessions: here, the participant C advertises a contract, and D just <emphasis>accepts</emphasis> it. This means that the middleware associates D with the <emphasis>canonical compliant</emphasis> of the contract of C, and it establishes a session between C and D. The interaction happening in this session then proceeds as described previously.</para>
<para>In this chapter we illustrate how to program contract-oriented distributed applications which run on the CO<subscript>2</subscript> middleware. A public instance of the middleware is accessible from <literal>co2.unica.it</literal>, together with all examples and experiments we carried out.</para>
</section>
<section class="lev1" id="sec2-2" label="2.2" xreflabel="2.2">
<title>Timed Session Types</title>
<para>The CO<subscript>2</subscript> middleware currently supports two kinds of contracts:</para>
<itemizedlist mark="bullet" spacing="normal">
<listitem><para>first-order binary session types [<link linkend="ch02_b18">18</link>];</para></listitem>
<listitem><para>timed session types (TSTs) [<link linkend="ch02_b6">6</link>].</para></listitem></itemizedlist>
<para>In this section we illustrate TSTs with the help of a small case study, an online store which receives orders from customers. The use of <emphasis>un</emphasis>timed session types in contract-oriented applications is discussed in the literature [<link linkend="ch02_b3">3</link>, <link linkend="ch02_b4">4</link>, <link linkend="ch02_b8">8</link>].</para>
<section class="lev2" id="sec2-2-1" label="2.2.1" xreflabel="2.2.1">
<title>Specifying Contracts</title>
<para>Timed session types extend binary session types [<link linkend="ch02_b18">18</link>, <link linkend="ch02_b26">26</link>] with clocks and timing constraints, similarly to the way timed automata [<link linkend="ch02_b1">1</link>] extend (classic) finite state automata. We informally describe the syntax of TSTs below, and we refer to [<link linkend="ch02_b5">5</link>, <link linkend="ch02_b6">6</link>] for the full technical development.</para>
<para><emphasis role="strong">Guards.</emphasis> Guards describe timing constraints, and they are conjunctions of simple guards of the form <literal>t</literal> <emphasis>&#x25E6;</emphasis> <literal>d</literal>, where <literal>t</literal> is a <emphasis>clock</emphasis>, <literal>d</literal><emphasis><literal></literal> </emphasis>&#x2208; &#x2115;, and <emphasis>&#x25E6;</emphasis> is a relation in <literal>&lt;</literal>, <literal>&lt;=</literal>, <literal>=</literal>, <literal>&gt;=</literal>, <literal>&gt;</literal>. For instance, the guard <literal>t&lt;60,u&gt;10</literal> is true whenever the value of clock <literal>t</literal> is less than 60, <emphasis>and</emphasis> the value of clock <literal>u</literal> is greater than 10. The value of clocks is in &#x211D;<emphasis><subscript>&#x2265;</subscript></emphasis><subscript>0</subscript>, like for timed automata.</para>
<para><emphasis role="strong">Send and receive.</emphasis> A TST describes the behaviour of a single participant A at the end-point of a session. Participants can perform two kinds of actions:</para>
<itemizedlist mark="bullet" spacing="normal">
<listitem><para>a <emphasis>send action</emphasis> <literal>!a</literal><literal>{</literal><literal>g;t1,...,tk</literal><literal>}</literal> stipulates that A will output a message with label <literal>a</literal> in a time window where the guard <literal>g</literal> is true. The clocks <literal>t1,...,tk</literal> will be reset after the output is performed.</para></listitem>
<listitem><para>a <emphasis>receive action</emphasis> <literal>?a</literal><literal>{</literal><literal>g;t1,...,tk</literal><literal>}</literal> stipulates that A will be available to receive a message with label <literal>a</literal> at <emphasis>any instant</emphasis> within the time window where the guard <literal>g</literal> is true. The clocks <literal>t1,...,tk</literal> will be reset after the input is received.</para></listitem></itemizedlist>
<para>When <literal>g</literal> = <literal>true</literal>, the guard can be omitted.</para>
<para>For instance, consider the contract <literal>store1</literal> between the store and a customer, from the point of view of the store.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog032.jpg" mime-subtype="jpeg"/></para>
<para>The store declares that it will receive an order at any time. After it has been received, the store will send the corresponding price within 60 seconds.</para>
<para><emphasis role="strong">Internal and external choices.</emphasis> TSTs also feature two forms of choice:</para>
<itemizedlist mark="bullet" spacing="normal">
<listitem><para> <literal>!a1</literal><literal>{</literal><literal>g1;R1</literal><literal>}</literal> <literal>+ ... + !an</literal><literal>{</literal><literal>gn;Rn</literal><literal>}</literal></para>
<para>This is an <emphasis>internal choice</emphasis>, stipulating that A will decide at run-time which one of the output actions <literal>!ai</literal><literal>{</literal><literal>gi;Ri</literal><literal>}</literal> (with <literal>1</literal><emphasis> <literal>&#x2264;</literal></emphasis> <literal>i</literal><emphasis> <literal>&#x2264;</literal></emphasis> <literal>n</literal>) to perform, and at which time instant. After the action is performed, all clocks in the set <literal>Ri =</literal><emphasis> <literal>{</literal></emphasis><literal>t1,...,tk</literal><literal>}</literal> are reset.</para></listitem>
<listitem><para><literal>?a1</literal><literal>{</literal><literal>g1;R1</literal><literal>}</literal> <literal>&amp; ...&amp; ?an</literal><literal>{</literal><literal>gn;Rn</literal><literal>}</literal></para>
<para>This is an <emphasis>external choice</emphasis>, stipulating that A will be able to receive any of the inputs <literal>!ai</literal><literal>{</literal><literal>gi;Ri</literal><literal>}</literal>, in the declared time windows. The actual choice of the action, and of the instant when it is performed, will be made by the participant at the other endpoint of the session. After the action is performed, all clocks in the set <literal>Ri =</literal><emphasis> <literal>{</literal></emphasis><literal>t1,...,tk</literal><emphasis><literal>}</literal> </emphasis>are reset.</para></listitem>
</itemizedlist>
<para>With these ingredients, we can refine the contract of our store as follows:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog033.jpg" mime-subtype="jpeg"/></para>
<para>This version of the contract deals with the case where the store receives an unknown or invalid product code. In this case, the internal choice allows the store to inform the buyer that the requested item is <literal>unavailable</literal>.</para>
<para><emphasis role="strong">Recursion.</emphasis> The contracts shown so far can only handle a bounded (statically known) number of interactions. We can overcome this limitation by using recursive TSTs. For instance, the contract <literal>store3</literal> below models a store which handles an arbitrary number of orders from a buyer:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog034.jpg" mime-subtype="jpeg"/></para>
<para>The contract <literal>store3</literal> allows buyers to add some item to the cart, or checkout. When a buyer chooses <literal>addtocart</literal>, the store must allow him to add more items: this is done recursively. After a <literal>checkout</literal>, the store must send the overall <literal>price</literal>, or inform the buyer that the requested items are <literal>unavailable</literal>. If the store sends a price, it must expect a response from the buyer, who can either <literal>accept</literal> or <literal>reject</literal> the price.</para>
<para><emphasis role="strong">Context.</emphasis> Action labels are grouped into <emphasis>contexts</emphasis>, which can be created and made public through the middleware APIs. Each context defines the labels related to an application domain, and it associates each label with a <emphasis>type</emphasis> and a <emphasis>verification link</emphasis>. The type (e.g., <literal>int</literal>, <literal>string</literal>) is that of the messages exchanged with that label. The verification link is used by the runtime monitor (described later on in this section) to delegate the verification of messages to a trusted third party. For instance, the middleware supports Paypal as a verification link for online payments [<link linkend="ch02_b7">7</link>].</para>
</section>
<section class="lev2" id="sec2-2-2" label="2.2.2" xreflabel="2.2.2">
<title>Compliance</title>
<para>Besides being used to specify the interaction protocols between pairs of services, TSTs feature the following primitives:</para>
<itemizedlist mark="bullet" spacing="normal">
<listitem><para>a decidable notion of <emphasis>compliance</emphasis> between two TSTs;</para></listitem>
<listitem><para>an algorithm to detect if a TST admits a compliant one;</para></listitem>
<listitem><para>a computable <emphasis>canonical compliant</emphasis> construction.</para></listitem></itemizedlist>
<para>These primitives are exploited by the CO<subscript>2</subscript> middleware to establish sessions between services: more specifically, the middleware only allows interactions between services with compliant contracts. Intuitively, compliance guarantees that, if <emphasis>all</emphasis> services respect <emphasis>all</emphasis> their contracts, then the overall distributed application (obtained by composing the services) will not deadlock.</para>
<para>Below we illustrate the primitives of TSTs by examples; a comprehensive formal treatment is in [<link linkend="ch02_b5">5</link>].</para>
<para>Informally, two TSTs are <emphasis>compliant</emphasis> if, in the interactions where both participants respect their contract, the deadlock state is not reachable (see [<link linkend="ch02_b5">5</link>] for details). For instance, recall the simple version of the store contract:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog035.jpg" mime-subtype="jpeg"/></para>
<para>and consider the following buyer contracts:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog036.jpg" mime-subtype="jpeg"/></para>
<para>We have that:</para>
<itemizedlist mark="bullet" spacing="normal">
<listitem><para><literal>store1</literal> and <literal>buyer1</literal> are compliant: indeed, the time frame where <literal>buyer1</literal> is available to receive <literal>price</literal> is larger than the one where the store can send;</para></listitem>
<listitem><para> <literal>store1</literal> and <literal>buyer2</literal> are compliant: although the action <literal>?unavailable</literal> enables a further interaction, this is never chosen by the store <literal>store1</literal>.</para></listitem>
<listitem><para> <literal>store1</literal> and <literal>buyer3</literal> are <emphasis>not</emphasis> compliant, because the store may choose to send <literal>price</literal> 60 seconds after he got the order, while <literal>buyer2</literal> is only able to receive within 30 seconds.</para></listitem>
<listitem><para> <literal>store1</literal> and <literal>buyer4</literal> are <emphasis>not</emphasis> compliant. Here the reason is more subtle: assume that the buyer sends the order at time 19: at that point, the store receives the order and resets the clock <literal>t</literal>; after that, the store has 60 seconds more to send <literal>price</literal>. Now, assume that the store chooses to send <literal>price</literal> after 59 seconds (which fits within the declared time window of 60 seconds). The total elapsed time is 19+59=78 seconds, but the buyer is only able to receive before 70 seconds.</para></listitem></itemizedlist>
<para>We can check if two contracts are compliant through the middleware Java APIs<footnote id="fn2_1" label="1"><para><literal>co2.unica.it/downloads/co2api/</literal></para></footnote>. We show how to do this through the Groovy<footnote id="fn2_2" label="2"><para><literal>groovy-lang.org/download.html</literal></para></footnote> interactive shell<footnote id="fn2_3" label="3"><para>On Unix-like systems, copy the API&#x2019;s jar in <literal>$HOME/.groovy/lib/</literal>. Then, add <literal>import co2api.*</literal> to <literal>$HOME/.groovy/groovysh.rc</literal>, and run <literal>groovysh</literal>.</para></footnote>.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog037.jpg" mime-subtype="jpeg"/></para>
<para>Consider now the second version of the store contract:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog038.jpg" mime-subtype="jpeg"/></para>
<para>The contract <literal>store2</literal> is compliant with the buyer contract <literal>buyer2</literal> discussed before, while it is <emphasis>not</emphasis> compliant with:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog039.jpg" mime-subtype="jpeg"/></para>
<para>The problem with <literal>buyer5</literal> is that the buyer is only accepting a message labelled <literal>price</literal>, while <literal>store2</literal> can also choose to send <literal>unavailable</literal>. Although this option is present in <literal>buyer6</literal>, the latter contract is not compliant with <literal>store2</literal> as well. In this case the reason is that the time window for receiving <literal>unavailable</literal> does not include that for sending it (recall that the sender can choose any instant satisfying the guard in its output action). To illustrate some less obvious aspects of compliance, consider the following buyer contract:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog040.jpg" mime-subtype="jpeg"/></para>
<para>This contract stipulates that the buyer can wait up to 100 seconds for sending an order, and then she can wait until 60 seconds (from the <emphasis>start</emphasis> of the session), to receive the price from the store.</para>
<para>Now, assume that some store contract is compliant with <literal>buyer7</literal>. Then, the store must be able to receive the <literal>order</literal> at least until time 100. If the buyer chooses to send the <literal>order</literal> at time 90 (which is allowed by contract <literal>buyer7</literal>), then the store would never be able to send <literal>price</literal> before time 70. Therefore, no contract can be compliant with <literal>buyer7</literal>.</para>
<para>The issue highlighted by the previous example must be dealt with care: if one publishes a service whose contract does not admit a compliant one, then the middleware will never connect that service with others. To check whether a contract admits a compliant one, we can query the middleware APIs:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog041.jpg" mime-subtype="jpeg"/></para>
<para>Recall from Section 2.1 that the CO<subscript>2</subscript> middleware also allows a service to <emphasis>accept</emphasis> another service&#x2019;s contract, as per item (5) in <link linkend="fig2_1">Figure <xref linkend="fig2_1" remap="2.1"/></link>. E.g., assume that the store has advertised the contract <literal>store2</literal> above. When the buyer uses the primitive <literal>accept</literal>, the middleware associates the buyer with the <emphasis>canonical compliant</emphasis> of <literal>store2</literal>, constructed through the method <literal>dualOf</literal>, i.e.:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog042.jpg" mime-subtype="jpeg"/></para>
<para>Intuitively, if a TST admits a compliant one, then its canonical compliant is constructed as follows:</para>
<orderedlist numeration="arabic" continuation="restarts" spacing="normal">
<listitem><para>output labels <literal>!a</literal> are translated into input labels <literal>?a</literal>,and <emphasis>vice versa</emphasis>;</para></listitem>
<listitem><para>internal choices are translated into external choices, and <emphasis>vice versa</emphasis>;</para></listitem>
<listitem><para>prefixes and recursive calls are preserved;</para></listitem>
<listitem><para>guards are suitably adjusted in order to ensure compliance.</para></listitem></orderedlist>
<para>Consider now the following contract of a store which receives an order and a coupon, and then sends a discounted price to the buyer:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog043.jpg" mime-subtype="jpeg"/></para>
<para>In this case <literal>store4</literal> admits a compliant one, but this cannot be obtained by simply swapping input/output actions and internal/external choices.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog044.jpg" mime-subtype="jpeg"/></para>
<para>Indeed, the canonical compliant construction gives:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog045.jpg" mime-subtype="jpeg"/></para>
<para><emphasis role="strong">2.2.3 Run-Time Monitoring of Contracts</emphasis></para>
<para>In order to detect (and sanction) contract violations, the CO<subscript>2</subscript> middleware monitors all the interactions that happen through sessions. The monitor guarantees that, in each reachable configuration, only one participant can be &#x201C;on duty&#x201D; (i.e., she has to perform some actions); and if no one is on duty nor culpable, then both participants have reached success. Here we illustrate how runtime monitoring works, by making a store and a buyer interact.</para>
<para>To this purpose, we split the paper in two columns: in the left column we show the store behaviour, while in the right column we show the buyer. We assume that both participants call the middleware APIs through the Groovy shell, as shown before. Note that the interaction between the two participants is asynchronous: when needed, we will highlight the points where one of the participants performs a time delay.</para>
<para>Both participants start by creating a connection <literal>co2</literal> with the middleware:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog046.jpg" mime-subtype="jpeg"/></para>
<para>Then, the participants create their contracts, and advertise them to the middleware through the primitive <literal>tell</literal>. The variables <literal>pS</literal> and <literal>pB</literal> are the handles to the published contracts.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog047.jpg" mime-subtype="jpeg"/></para>
<para>Now the middleware has two compliant contracts in its collection, hence it can establish a session between the store and the buyer. To obtain a handle to the session, both participants use the blocking primitive <literal>waitForSession</literal>:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog048.jpg" mime-subtype="jpeg"/></para>
<para>At this point, participants can query the session to see who is &#x201C;on duty&#x201D; (namely, one is on duty if the contract prescribes her to perform the next action), and to check if they have violated the contract:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog049.jpg" mime-subtype="jpeg"/></para>
<para>Note that the first action must be performed by the buyer, who must send the <literal>order</literal>. This is accomplished by the <literal>send</literal> primitive. Dually, the store waits for the receipt of the message, using the <literal>waitForReceive</literal> primitive:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog050.jpg" mime-subtype="jpeg"/></para>
<para>Since there are no time constraints on sending <literal>order</literal>, this action can be successfully performed at any time; once this is done, the <literal>waitForReceive</literal> unlocks the store. The store is now on duty, and it must send <literal>price</literal> within 60 seconds, or <literal>unavailable</literal> within 10 seconds. Now, assume that the store tries to send <literal>unavailable</literal> after the deadline:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog051.jpg" mime-subtype="jpeg"/></para>
<para>On the store&#x2019;s side, the <literal>send</literal> throws a <literal>ContractException</literal>; on the buyer side, the <literal>waitForReceive</literal> throws an exception which reports the violation of the store. At this point, if the two participants check the state of the session, they find that none of them is still on duty, and that the store is culpable:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog052.jpg" mime-subtype="jpeg"/></para>
<para>At this point, the session is terminated, and the reputation of the store is suitably decreased.</para>
</section>
</section>
<section class="lev1" id="sec2-3" label="2.3" xreflabel="2.3">
<title>Contract-Oriented Programming</title>
<para>In this section we develop some simple contract-oriented services, using the middleware APIs via their Java binding<footnote id="fn2_4" label="4"><para>Full code listings are available at <literal>co2.unica.it</literal></para></footnote>.</para>
<section class="lev2" id="sec2-3-1" label="2.3.1" xreflabel="2.3.1">
<title>2.3.1 A Simple Store</title>
<para>We start with a basic store service, which advertises the contract <literal>store2</literal>:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog053.jpg" mime-subtype="jpeg"/></para>
<para>At lines <literal>1-2</literal>, the store constructs a TST <literal>c</literal> for contract <literal>store2</literal>. At lines <literal>4-5</literal>, the store connects to the middleware, providing its credentials. At line <literal>6</literal>, the <literal>Private</literal> object represents the contract in a state where it has not been advertised to the middleware yet. To advertise the contract, we invoke the <literal>tell</literal> method at line <literal>7</literal>. This call returns a <literal>Public</literal> object, modelling a latent contract that can be &#x201C;fused&#x201D; with a compliant one to establish a new session. At line <literal>9</literal>, the store waits for a session to be established; the returned <literal>Session</literal> object allows the store to interact with a buyer. At line <literal>10</literal>, the store waits for the receipt of a message, containing the code of the product requested by the buyer. At lines <literal>12-13</literal>, the store sends the message <literal>price</literal> (with the corresponding value) if the item is available, otherwise it sends <literal>unavailable</literal>.</para>
</section>
<section class="lev2" id="sec2-3-2" label="2.3.2" xreflabel="2.3.2">
<title>A Simple Buyer</title>
<para>We now show a buyer that can interact with the store. This buyer just accepts the already published contract <literal>store2</literal>. The contract is identified by its hash, which is obtained from <literal>Public.getContractID()</literal>.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog054.jpg" mime-subtype="jpeg"/></para>
<para>At line <literal>6</literal>, the buyer accepts the store&#x2019;s contract, identified by <literal>storeCID</literal>. The call to <literal>Public.accept</literal> returns a <literal>Public</literal> object. At this point a session with the store is already established, and <literal>waitForSession</literal> just returns the corresponding <literal>Session</literal> object (line <literal>7</literal>). Now, the buyer sends the item code (line <literal>9</literal>), waits for the store response (line <literal>12</literal>), and finally in the <literal>try</literal>-<literal>catch</literal> statement it handles the messages <literal>price</literal> and <literal>unavailable</literal>.</para>
<para>Note that the <literal>accept</literal> primitive allows a participant to establish sessions with a chosen counterpart; instead, this is not allowed by the <literal>tell</literal> primitive, which can establish a session whenever two contracts are compliant.</para>
</section>
<section class="lev2" id="sec2-3-3" label="2.3.3" xreflabel="2.3.3">
<title>A Dishonest Store</title>
<para>Consider now a more complex store, which relies on external distributors to retrieve items. As before, the store takes an order from the buyer; however, now it invokes an external distributor if the requested item is not in stock. If the distributor can provide the item, then the store confirms the order to the buyer; otherwise, it informs the buyer that the item is unavailable.</para>
<para>Our first attempt to implement this refined store is the following.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog055.jpg" mime-subtype="jpeg"/></para>
<para>At lines <literal>1-2</literal> we construct two TSTs: <literal>cB</literal> for interacting with buyers, and <literal>cD</literal> for interacting with distributors. In <literal>cD</literal>, the store first sends a request for some item to the distributor, and then waits for an <literal>ok</literal> or <literal>no</literal> answer, according to whether the distributor is able to provide the requested item or not. At lines <literal>4-6</literal>, the store advertises <literal>cB</literal>, and it waits for a buyer to join the session; then, it receives the order, and checks if the requested item is in stock (line <literal>8</literal>). If so, the store sends the price of the item to the buyer (line <literal>9</literal>).</para>
<para>If the item is not in stock, the store advertises <literal>cD</literal> to find a distributor (lines <literal>12-13</literal>). When a session <literal>sD</literal> is established, the store forwards the item identifier to the distributor (line <literal>15</literal>), and then it waits for a reply. If the reply is <literal>no</literal>, the store sends <literal>unavailable</literal> to the buyer, otherwise it sends a <literal>price</literal>.</para>
<para>Note that this implementation of the store is <emphasis>dishonest</emphasis>, namely it may violate contracts [<link linkend="ch02_b11">11</link>]. This happens in the following two cases:</para>
<orderedlist numeration="arabic" continuation="restarts" spacing="normal">
<listitem><para>Assume that the store has received the buyer&#x2019;s order, but the requested item is not in stock. Then, the store advertises the contract <literal>cD</literal> to find a distributor. Note that there is no guarantee that the session <literal>sD</literal> will be established within a given deadline, nor that it will be established at all. If more than 60 seconds pass on the <literal>waitForSession</literal> at line <literal>13</literal>, the store becomes culpable with respect to the contract <literal>cB</literal>. Indeed, such contract requires the store to perform an action before 60 seconds (10 seconds if the action is <literal>unavailable</literal>).</para></listitem>
<listitem><para>Moreover, if the session <literal>sD</literal> is established in timely fashion, a slow or unresponsive distributor could make the store violate the contract <literal>cB</literal>. For instance, assume that the distributor sends message <literal>no</literal> after nearly 10 seconds. In this case, the store may not have enough time to send <literal>unavailable</literal> to the buyer within 10 seconds, and so it becomes culpable at session <literal>sB</literal>.</para></listitem></orderedlist>
<para>We have simulated the scenario described in Item 1, by making the store interact with slow or unresponsive distributors (see <link linkend="fig2_2">Figure <xref linkend="fig2_2" remap="2.2"/></link>). The experimental results show that, although the store is not culpable in all the sessions, its reputation decreases over time. Recovering from such situation is not straightforward, since the reputation system of the CO<subscript>2</subscript> middleware features defensive techniques against self-promoting attacks [<link linkend="ch02_b25">25</link>].</para>
<fig id="fig2_2" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 2.2</label>
<caption><title>Reputation of the dishonest and honest stores as a function of the number of sessions with malicious distributors.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig002.jpg" mime-subtype="jpeg"/>
</fig>
</section>
<section class="lev2" id="sec2-3-4" label="2.3.4" xreflabel="2.3.4">
<title>An Honest Store</title>
<para>In order to implement an honest store, we must address the fact that, if the distributor delays its message to the maximum allowed time, the store may not have enough time to respond to the buyer. To cope with this scenario, we adjust the timing constraints in the contract between the store and the distributor, and we implement a revised version of the store as follows.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog056.jpg" mime-subtype="jpeg"/></para>
<para>The parameter in the <literal>tell</literal> at line <literal>12</literal> specifies a deadline of 3 seconds: if the session <literal>sD</literal> is not established within the deadline, the contract <literal>cD</literal> is retracted from the middleware, and a <literal>ContractExpiredException</literal> is thrown. The store catches the exception at line <literal>28</literal>, sending <literal>unavailable</literal> to the buyer.</para>
<para>Instead, if the session <literal>sD</literal> is established, the store forwards the item identifier to the distributor (line <literal>15</literal>), and then waits for the receipt of a response from it. If the distributor sends neither <literal>ok</literal> nor <literal>no</literal> within the deadline specified in <literal>cD</literal> (5 seconds), the middleware assigns the blame to the distributor for a contract breach, and unblocks the <literal>waitForReceive</literal> in the store with a <literal>ContractViolationException</literal>
(line <literal>24</literal>). In the exception handler, the store fulfils the contract <literal>cB</literal> by sending <literal>unavailable</literal> to the buyer.</para>
</section>
<section class="lev2" id="sec2-3-5" label="2.3.5" xreflabel="2.3.5">
<title>A Recursive Honest Store</title>
<para>We now present another version of the store, which uses the recursive contract <literal>store3</literal> on page 31. As in the previous version, if the buyer requests an item that is not in stock, the store resorts to an external distributor.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog057.jpg" mime-subtype="jpeg"/></para>
<para>After advertising the contract <literal>cB</literal>, the store waits for a session <literal>sB</literal> with the buyer (lines <literal>4-5</literal>). After the session is established, the store can receive <literal>addtocart</literal> multiple times: for each <literal>addtocart</literal>, it saves the corresponding item identifier in a list. The loop terminates when the buyer selects <literal>checkout</literal>. If all requested items are available, the store sends the total <literal>price</literal> to the buyer (line <literal>18</literal>). After that, the store expects either <literal>accept</literal> or <literal>reject</literal> from the buyer. If the buyer does not respect his deadlines, an exception is thrown, and it is caught at line <literal>57</literal>. If the buyer replies on time, the store advertises the contract <literal>cD</literal>, and waits for a session <literal>sD</literal> with the distributor (lines <literal>26-28</literal>). If the session is not established within 5 seconds, an exception is thrown. The store handles the exception at line <literal>52</literal>, by sending <literal>unavailable</literal> to the buyer. If a session with the distributor is established within the deadline, the store requests the unavailable items, and waits for a response (line <literal>31</literal>). If the distributor sends <literal>no</literal>, the store answers <literal>unavailable</literal> to the buyer (line <literal>32</literal>). If the distributor sends <literal>ok</literal>, then the interaction between store and buyer proceeds as if the items were in stock. If the distributor does not reply within the deadline, an exception is thrown. The store handles it at line <literal>47</literal>,by sending <literal>unavailable</literal> to the buyer. An untimed specification of this store is proved honest in [<link linkend="ch02_b4">4</link>]. We conjecture that also this timed version of the store respects contracts in all possible contexts.</para>
</section>
</section>
<section class="lev1" id="sec2-4" label="2.4" xreflabel="2.4">
<title>Conclusions</title>
<para>We have explored the use of behavioural contracts as service-level agreements among the components of a distributed application. In particular, we have considered a middleware where services can advertise contracts (in the form of timed session types, TSTs), and interact through sessions, which are only created between services with compliant contracts. The primitives of the middleware exploit the theory of TSTs: in particular, a decidable notion of compliance between TSTs, a decidable procedure to detect when a TST admits a compliant one, and a decidable runtime monitoring. The middleware has been validated in [<link linkend="ch02_b7">7</link>] through a series of experiments, which measure the scalability of the approach when the number of exchanged contracts grows, and the effectiveness of the reputation system.</para>
<para>Although the current version of the middleware only features binary (either timed or untimed) session types as contracts, the underlying idea can be extended to other contract models. Indeed, the middleware only makes mild assumptions about the nature of contracts, e.g., that they feature: (i) monitorable send and receive actions, (ii) some notion of accepting a contract or a role, or (iii) some notion of compliance with a sound (but not necessarily complete) verification algorithm. Other timed models of contracts would be ideal candidates for extensions of the middleware. For instance, communicating timed automata [<link linkend="ch02_b13">13</link>] (which are timed automata with unbounded communication channels) would allow for multi-party sessions.</para>
<para>Security issues should be seriously taken into account when developing contract-oriented applications. As we have shown for the dishonest online store in Section 2.3, adversaries could make a service sanctioned by exploiting discrepancies between its contracts and its actual behaviour. Since these mismatches are not always easy to spot, analysis techniques are needed in order to ensure that a service will not be susceptible to this kind of attacks. A starting point could be the analyses in [<link linkend="ch02_b8">8</link>, <link linkend="ch02_b9">9</link>], that can detect whether a contract-oriented specification is honest; the Diogenes toolchain [<link linkend="ch02_b3">3</link>] extends this check to Java code. Since these analyses do not take into account time constraints, further work is needed to extend these techniques to timed applications.</para>
<section class="lev2" id="sec2-4-1" label="2.4.1" xreflabel="2.4.1">
<title>Related Work</title>
<para>The theoretical foundations of our middleware are timed session types and CO<subscript>2</subscript> [<link linkend="ch02_b12">12</link>, <link linkend="ch02_b10">10</link>], a specification language for contract-oriented services. The middleware implements the main primitives of CO<subscript>2</subscript> (<literal>tell</literal>, <literal>send</literal>, <literal>receive</literal>), and it introduces new concepts, such as the <literal>accept</literal> primitive, time constraints, and reputation.</para>
<para>From the theoretical viewpoint, the idea of constraint-based interactions has been investigated in other process calculi, such as Concurrent Constraint Programming (CCP) [<link linkend="ch02_b24">24</link>], and <emphasis role="romanAlt">cc</emphasis>-pi [<link linkend="ch02_b16">16</link>]. The kind of interactions they induce is quite different from ours. In CCP, processes can interact by telling and asking for the validity of constraints on a global constraint store. In <emphasis role="romanAlt">cc</emphasis>-pi, interaction is a mix of name communication <emphasis>&#x00E0; la</emphasis><emphasis> <literal>&#x03C0;</literal></emphasis>-calculus [<link linkend="ch02_b21">21</link>] and <literal>tell</literal> <emphasis>&#x00E0; la</emphasis> CCP (which is used to put constraints on names). In <emphasis role="romanAlt">cc</emphasis>-pi consistency plays a crucial role: <literal>tell</literal>s <emphasis>restrict</emphasis> the future interactions with other processes, since adding constraints can lead to more inconsistencies; by contrast, in our middleware advertising a contract <emphasis>enables</emphasis> interaction with other services, so consistency is immaterial, but compliance is a key notion.</para>
<para>Several formalisms for expressing timed communication protocols have been proposed over the years. The work [<link linkend="ch02_b14">14</link>] addresses a timed extension of multi-party asynchronous session types [<link linkend="ch02_b19">19</link>]. Unlike ours, the approach pursued in [<link linkend="ch02_b14">14</link>] is top-down: a <emphasis>global type</emphasis>, specifying the overall communication protocol of a set of services, is projected onto a set of <emphasis>local types</emphasis>. Then, a composition of services preserves the properties of the global type (e.g., deadlock-freedom) if each service type-checks against the associated local type. The CO<subscript>2</subscript> middleware, instead, fosters a bottom-up approach to service composition. Both our approach and [<link linkend="ch02_b14">14</link>, <link linkend="ch02_b23">23</link>] use runtime monitoring to detect contract violations and assign the blame to the party that is responsible for a contract violation. The CO<subscript>2</subscript> middleware also exploits these data in its reputation system.</para>
<para>The work [<link linkend="ch02_b13">13</link>] studies <emphasis>communicating timed automata</emphasis>, a timed version of communicating finite-state machines [<link linkend="ch02_b15">15</link>]. In this model, participants in a network communicate asynchronously through bi-directional FIFO channels; similarly to [<link linkend="ch02_b14">14</link>], clocks, guards and resets are used to impose time constraints on when communications can happen. An approximate (sound, but not complete) decidable technique allows one to check when a system of automata enjoys progress. This technique is based on <emphasis>multiparty compatibility</emphasis>, a condition that guarantees deadlock-freedom of untimed systems [<link linkend="ch02_b20">20</link>].</para>
<para>From the application viewpoint, several works have investigated the problem of <emphasis>service selection</emphasis> in open dynamic environments [<link linkend="ch02_b2">2</link>, <link linkend="ch02_b22">22</link>, <link linkend="ch02_b27">27</link>, <link linkend="ch02_b28">28</link>]. This problem consists in matching client requests with service offers, in a way that, among the services respecting the given functional constraints, the one that maximises some <emphasis>non-functional</emphasis> constraints is selected. These nonfunctional constraints are often based on quality of service (QoS) metrics, e.g. cost, reputation, guaranteed throughput or availability, etc. The selection mechanism featured in our middleware does not search for the &#x201C;best&#x201D; contract that is compliant with a given one (actually, typical compliance relations in behavioural contracts are qualitative, rather than quantitative); the only QoS parameter we take into account is the reputation of services. In some approaches [<link linkend="ch02_b2">2</link>, <link linkend="ch02_b28">28</link>] clients can require a sequence of tasks together with a set of non-functional constraints, and the goal is to find an assignment of tasks to services that optimises all the given constraints. There are two main differences between these approaches and ours. First, unlike behavioural contracts, tasks are considered as atomic activities, not requiring any interaction between clients and services. Second, unlike ours, these approaches do not consider the possibility that a service may not fulfil the required task.</para>
<para>Some works have explored service selection mechanisms where functional constraints can be required in addition to QoS constraints [<link linkend="ch02_b22">22</link>]: the first are described by a web service ontology, while the others are defined as requested and offered ranges of basic QoS attributes. Runtime monitor and reputation systems are also implemented, which, similarly to ours, help to marginalise those services that do not respect the advertised QoS constraints. Some kinds of QoS constraints cannot be verified by the service broker, so their verification is delegated to clients. This can be easily exploited by malicious participants to carry on <emphasis>slandering attacks</emphasis> to the reputation system [<link linkend="ch02_b17">17</link>]: an attacker could destroy another participant&#x2019;s reputation by involving it in many sessions, and each time declare that the required QoS constraints have been violated. In our middleware there is no need to assume that participants are trusted, as the verification of contracts is delegated to the middleware itself and to trusted third parties.</para>
<para><emphasis role="strong">Acknowledgments</emphasis> This work is partially supported by Aut. Reg. of Sardinia grants L.R.7/2007 CRP-17285 (TRICS), P.I.A. 2013 (&#x201C;NOMAD&#x201D;), and by EU COST Action IC1201 &#x201C;Behavioural Types for Reliable Large-Scale Software Systems&#x201D; (BETTY). Alessandro Sebastian Podda gratefully acknowledges Sardinia Regional Government for the financial support of her PhD scholarship (P.O.R. Sardegna F.S.E. Operational Programme of the Autonomous Region of Sardinia, European Social Fund 2007&#x2013;2013 &#x2013; Axis IV Human Resources, Objective l.3, Line of Activity l.3.1).</para>
</section>
</section>
<section class="lev1" id="sec2-5">
<title>References</title>
<orderedlist numeration="arabic" continuation="restarts" spacing="normal">
<listitem><para><anchor id="ch02_b1"/>Rajeev Alur and David L. Dill. A theory of timed automata. <emphasis>Theor. Comput. Sci.</emphasis> 126(2):183&#x2013;235, 1994.</para></listitem>
<listitem><para><anchor id="ch02_b2"/>Danilo Ardagna and Barbara Pernici. Adaptive service composition in flexible processes. <emphasis>IEEE Trans. Software Eng.</emphasis> 33(6):369&#x2013;384, 2007. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Danilo+Ardagna+and+Barbara+Pernici%2E+Adaptive+service+composition+in+flexible+processes%2E+IEEE+Trans%2E+Software+Eng%2E+33%286%29%3A369-384%2C+2007%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch02_b3"/>Nicola Atzei and Massimo Bartoletti. Developing honest Java programs with Diogenes. In <emphasis>Formal Techniques for Distributed Objects, Components, and Systems (FORTE)</emphasis>, volume 9688 of <emphasis>LNCS</emphasis>, pages 52&#x2013;61. Springer, 2016. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Nicola+Atzei+and+Massimo+Bartoletti%2E+Developing+honest+Java+programs+with+Diogenes%2E+In+Formal+Techniques+for+Distributed+Objects%2C+Components%2C+and+Systems+%28FORTE%29%2C+volume+9688+of+LNCS%2C+pages+52-61%2E+Springer%2C+2016%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch02_b4"/>Nicola Atzei, Massimo Bartoletti, Maurizio Murgia, Emilio Tuosto, and Roberto Zunino. Contract-oriented design of distributed applications: a tutorial. <literal>tcs.unica.it/papers/diogenes-tutorial.pdf</literal>, 2016. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Nicola+Atzei%2C+Massimo+Bartoletti%2C+Maurizio+Murgia%2C+Emilio+Tuosto%2C+and+Roberto+Zunino%2E+Contract-oriented+design+of+distributed+applications%3A+a+tutorial%2E+tcs%2Eunica%2Eit%2Fpapers%2Fdiogenes-tutorial%2Epdf%2C+2016%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch02_b5"/>Massimo Bartoletti, Tiziana Cimoli, and Maurizio Murgia. Timed session types, 2015. Pre-print available at <literal>tcs.unica.it/papers/tst.pdf</literal> <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Massimo+Bartoletti%2C+Tiziana+Cimoli%2C+and+Maurizio+Murgia%2E+Timed+session+types%2C+2015%2E+Pre-print+available+at+tcs%2Eunica%2Eit%2Fpapers%2F+tst%2Epdf" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch02_b6"/>Massimo Bartoletti, Tiziana Cimoli, Maurizio Murgia, Alessandro Sebastian Podda, and Livio Pompianu. Compliance and subtyping in timed session types. In <emphasis>Formal Techniques for Distributed Objects, Components, and Systems (FORTE)</emphasis>, volume 9039 of <emphasis>LNCS</emphasis>, pages 161&#x2013;177. Springer, 2015. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Massimo+Bartoletti%2C+Tiziana+Cimoli%2C+Maurizio+Murgia%2C+Alessandro+Sebastian+Podda%2C+and+Livio+Pompianu%2E+Compliance+and+subtyping+in+timed+session+types%2E+In+Formal+Techniques+for+Distributed+Objects%2C+Components%2C+and+Systems+%28FORTE%29%2C+volume+9039+of+LNCS%2C+pages+161-177%2E+Springer%2C+2015%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch02_b7"/>Massimo Bartoletti, Tiziana Cimoli, Maurizio Murgia, Alessandro Sebastian Podda, and Livio Pompianu. A contract-oriented middleware. In <emphasis>Formal Aspects of Component Software (FACS)</emphasis>, volume 9539 of <emphasis>LNCS</emphasis>, pages 86&#x2013;104. Springer, 2015. <literal>co2.unica.it</literal> <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Massimo+Bartoletti%2C+Tiziana+Cimoli%2C+Maurizio+Murgia%2C+Alessandro+Sebastian+Podda%2C+and+Livio+Pompianu%2E+A+contract-oriented+middleware%2E+In+Formal+Aspects+of+Component+Software+%28FACS%29%2C+volume+9539+of+LNCS%2C+pages+86-104%2E+Springer%2C+2015%2E+co2%2Eunica%2Eit" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch02_b8"/>Massimo Bartoletti, Maurizio Murgia, Alceste Scalas, and Roberto Zunino. Verifiable abstractions for contract-oriented systems. <emphasis>Journal of Logical and Algebraic Methods in Programming (JLAMP)</emphasis>, 86:159&#x2013;207, 2017. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Massimo+Bartoletti%2C+Maurizio+Murgia%2C+Alceste+Scalas%2C+and+Roberto+Zunino%2E+Verifiable+abstractions+for+contract-oriented+systems%2E+Journal+of+Logical+and+Algebraic+Methods+in+Programming+%28JLAMP%29%2C+86%3A159-207%2C+2017%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch02_b9"/>Massimo Bartoletti, Alceste Scalas, Emilio Tuosto, and Roberto Zunino. Honesty by typing. <emphasis>Logical Methods in Computer Science</emphasis> 12(4), 2016. Pre-print available at: <literal>arxiv.org/abs/1211.2609</literal> <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Massimo+Bartoletti%2C+Alceste+Scalas%2C+Emilio+Tuosto%2C+and+Roberto+Zunino%2E+Honesty+by+typing%2E+Logical+Methods+in+Computer+Science+12%284%29%2C+2016%2E+Pre-print+available+at%3A+arxiv%2Eorg%2Fabs%2F1211%2E2609" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch02_b10"/>Massimo Bartoletti, Emilio Tuosto, and Roberto Zunino. Contract-oriented computing in CO<subscript>2</subscript>. <emphasis>Sci. Ann. Comp. Sci.</emphasis> 22(1):5&#x2013;60, 2012. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Massimo+Bartoletti%2C+Emilio+Tuosto%2C+and+Roberto+Zunino%2E+Contract-oriented+computing+in+CO2%2E+Sci%2E+Ann%2E+Comp%2E+Sci%2E+22%281%29%3A5-60%2C+2012%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch02_b11"/>Massimo Bartoletti, Emilio Tuosto, and Roberto Zunino. On the realizability of contracts in dishonest systems. In <emphasis>COORDINATION</emphasis>, volume 7274 of <emphasis>LNCS</emphasis>, pages 245&#x2013;260. Springer, 2012. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Massimo+Bartoletti%2C+Emilio+Tuosto%2C+and+Roberto+Zunino%2E+On+the+realizability+of+contracts+in+dishonest+systems%2E+In+COORDINATION%2C+volume+7274+of+LNCS%2C+pages+245-260%2E+Springer%2C+2012%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch02_b12"/>Massimo Bartoletti and Roberto Zunino. A calculus of contracting processes. In <emphasis>IEEE Symposium on Logic in Computer Science (LICS)</emphasis>, pages 332&#x2013;341. IEEE Computer Society, 2010. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Massimo+Bartoletti+and+Roberto+Zunino%2E+A+calculus+of+contracting+processes%2E+In+IEEE+Symposium+on+Logic+in+Computer+Science+%28LICS%29%2C+pages+332-341%2E+IEEE+Computer+Society%2C+2010%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch02_b13"/>Laura Bocchi, Julien Lange, and Nobuko Yoshida. Meeting deadlines together. In <emphasis>CONCUR</emphasis>, volume 42 of <emphasis>LIPIcs</emphasis>, pages 283&#x2013;296. Schloss Dagstuhl &#x2013; Leibniz-Zentrum fuer Informatik, 2015. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Laura+Bocchi%2C+Julien+Lange%2C+and+Nobuko+Yoshida%2E+Meeting+deadlines+together%2E+In+CONCUR%2C+volume+42+of+LIPIcs%2C+pages+283-296%2E+Schloss+Dagstuhl+-+Leibniz-Zentrum+fuer+Informatik%2C+2015%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch02_b14"/>Laura Bocchi, Weizhen Yang, and Nobuko Yoshida. Timed multiparty session types. In <emphasis>CONCUR</emphasis>, volume 8704 of <emphasis>LNCS</emphasis>, pages 419&#x2013;434. Springer, 2014. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Laura+Bocchi%2C+Weizhen+Yang%2C+and+Nobuko+Yoshida%2E+Timed+multiparty+session+types%2E+In+CONCUR%2C+volume+8704+of+LNCS%2C+pages+419-434%2E+Springer%2C+2014%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch02_b15"/>Daniel Brand and Pitro Zafiropulo. On communicating finite-state machines. <emphasis>J. ACM</emphasis> 30(2):323&#x2013;342, 1983. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Daniel+Brand+and+Pitro+Zafiropulo%2E+On+communicating+finite-state+machines%2E+J%2E+ACM+30%282%29%3A323-342%2C+1983%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch02_b16"/>Maria Grazia Buscemi and Ugo Montanari. CC-Pi: A constraint-based language for specifying service level agreements. In <emphasis>European Symposium on Programming (ESOP)</emphasis>, volume 4421 of <emphasis>LNCS</emphasis>, pages 18&#x2013;32. Springer, 2007. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Maria+Grazia+Buscemi+and+Ugo+Montanari%2E+CC-Pi%3A+A+constraint-based+language+for+specifying+service+level+agreements%2E+In+European+Symposium+on+Programming+%28ESOP%29%2C+volume+4421+of+LNCS%2C+pages+18-32%2E+Springer%2C+2007%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch02_b17"/>Kevin J. Hoffman, David Zage, and Cristina Nita-Rotaru. A survey of attack and defense techniques for reputation systems. <emphasis>ACM Comput. Surv.</emphasis> 42(1), 2009. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Kevin+J%2E+Hoffman%2C+David+Zage%2C+and+Cristina+Nita-Rotaru%2E+A+survey+of+attack+and+defense+techniques+for+reputation+systems%2E+ACM+Comput%2E+Surv%2E+42%281%29%2C+2009%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch02_b18"/>Kohei Honda, Vasco T. Vasconcelos, and Makoto Kubo. Language primitives and type disciplines for structured communication-based programming. In <emphasis>European Symposium on Programming (ESOP)</emphasis>, volume 1381 of <emphasis>LNCS</emphasis>, pages 22&#x2013;138. Springer, 1998.</para></listitem>
<listitem><para><anchor id="ch02_b19"/>Kohei Honda, Nobuko Yoshida, and Marco Carbone. Multiparty asynchronous session types. In <emphasis>ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL)</emphasis>, pages 273&#x2013;284. ACM, 2008. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Kohei+Honda%2C+Nobuko+Yoshida%2C+and+Marco+Carbone%2E+Multiparty+asynchronous+session+types%2E+In+ACM+SIGPLAN-SIGACT+Symposium+on+Principles+of+Programming+Languages+%28POPL%29%2C+pages+273-284%2E+ACM%2C+2008%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch02_b20"/>Julien Lange, Emilio Tuosto, and Nobuko Yoshida. From communicating machines to graphical choreographies. In <emphasis>ACM SIGPLANSIGACT Symposium on Principles of Programming Languages (POPL)</emphasis>, pages 221&#x2013;232. ACM, 2015. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Julien+Lange%2C+Emilio+Tuosto%2C+and+Nobuko+Yoshida%2E+From+communicating+machines+to+graphical+choreographies%2E+In+ACM+SIGPLANSIGACT+Symposium+on+Principles+of+Programming+Languages+%28POPL%29%2C+pages+221-232%2E+ACM%2C+2015%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch02_b21"/>Robin Milner, Joachim Parrow, and David Walker. A Calculus of Mobile Processes, I and II. <emphasis>Information and Computation</emphasis> 100(1):1&#x2013;40,41&#x2013;77, <month>September</month> 1992. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Robin+Milner%2C+Joachim+Parrow%2C+and+David+Walker%2E+A+Calculus+of+Mobile+Processes%2C+I+and+II%2E+Information+and+Computation+100%281%29%3A1-40%2C41-77%2C+September+1992%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch02_b22"/>A. Mukhija, Andrew Dingwall-Smith, and D.S. Rosenblum. QoS-aware service composition in Dino. In <emphasis>ECOWS</emphasis>, volume 5900 of <emphasis>LNCS</emphasis>, pages 3&#x2013;12. Springer, 2007. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=A%2E+Mukhija%2C+Andrew+Dingwall-Smith%2C+and+D%2ES%2E+Rosenblum%2E+QoS-aware+service+composition+in+Dino%2E+In+ECOWS%2C+volume+5900+of+LNCS%2C+pages+3-12%2E+Springer%2C+2007%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch02_b23"/>Rumyana Neykova, Laura Bocchi, and Nobuko Yoshida. Timed runtime monitoring for multiparty conversations. In <emphasis>BEAT</emphasis>, volume 162 of <emphasis>EPTCS</emphasis>, pages 19&#x2013;26, 2014. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Rumyana+Neykova%2C+Laura+Bocchi%2C+and+Nobuko+Yoshida%2E+Timed+runtime+monitoring+for+multiparty+conversations%2E+In+BEAT%2C+volume+162+of+EPTCS%2C+pages+19-26%2C+2014%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch02_b24"/>Vijay A. Saraswat and Martin C. Rinard. Concurrent constraint programming. In <emphasis>ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL)</emphasis>, pages 232&#x2013;245. ACM, 1990. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Vijay+A%2E+Saraswat+and+Martin+C%2E+Rinard%2E+Concurrent+constraint+programming%2E+In+ACM+SIGPLAN-SIGACT+Symposium+on+Principles+of+Programming+Languages+%28POPL%29%2C+pages+232-245%2E+ACM%2C+1990%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch02_b25"/>Mudhakar Srivatsa, Li Xiong, and Ling Liu. TrustGuard: countering vulnerabilities in reputation management for decentralized overlay networks. In <emphasis>International Conference on World Wide Web (WWW)</emphasis>, pages 422&#x2013;431. ACM, 2005. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Mudhakar+Srivatsa%2C+Li+Xiong%2C+and+Ling+Liu%2E+TrustGuard%3A+countering+vulnerabilities+in+reputation+management+for+decentralized+overlay+networks%2E+In+International+Conference+on+World+Wide+Web+%28WWW%29%2C+pages+422-431%2E+ACM%2C+2005%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch02_b26"/>Kaku Takeuchi, Kohei Honda, and Makoto Kubo. An interaction-based language and its typing system. In <emphasis>PARLE</emphasis>, pages 398&#x2013;413, 1994. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Kaku+Takeuchi%2C+Kohei+Honda%2C+and+Makoto+Kubo%2E+An+interaction-based+language+and+its+typing+system%2E+In+PARLE%2C+pages+398-413%2C+1994%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch02_b27"/>Tao Yu, Yue Zhang, and Kwei-Jay Lin. Efficient algorithms for Web services selection with end-to-end QoS constraints. <emphasis>ACM Transactions on the Web</emphasis> 1(1):6, 2007. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Tao+Yu%2C+Yue+Zhang%2C+and+Kwei-Jay+Lin%2E+Efficient+algorithms+for+Web+services+selection+with+end-to-end+QoS+constraints%2E+ACM+Transactions+on+the+Web+1%281%29%3A6%2C+2007%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch02_b28"/>Liangzhao Zeng, Boualem Benatallah, Anne HH Ngu, Marlon Dumas, Jayant Kalagnanam, and Henry Chang. QoS-aware middleware for Web services composition. <emphasis>IEEE Transactions on Software Engineering</emphasis> 30(5):311&#x2013;327, 2004. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Liangzhao+Zeng%2C+Boualem+Benatallah%2C+Anne+HH+Ngu%2C+Marlon+Dumas%2C+Jayant+Kalagnanam%2C+and+Henry+Chang%2E+QoS-aware+middleware+for+Web+services+composition%2E+IEEE+Transactions+on+Software+Engineering+30%285%29%3A311-327%2C+2004%2E" target="_blank">Google Scholar</ulink></para></listitem>
</orderedlist>
</section>
</chapter>
<chapter class="chapter" id="ch03" label="3" xreflabel="3">
<title>A Runtime Monitoring Tool for Actor-Based Systems</title>
<para><emphasis role="strong">Duncan Paul Attard</emphasis><footnote id="fn3_1a" label="1"><para>Department of Computer Science, Faculty of ICT, University of Malta, Malta</para></footnote><emphasis role="strong">, Ian Cassar</emphasis><footnoteref linkend="fn3_1a" label="1"/><emphasis role="strong">, Adrian Francalanza</emphasis><footnoteref linkend="fn3_1a" label="1"/><emphasis role="strong">, Luca Aceto</emphasis><footnote id="fn3_2a" label="2"><para>School of Computer Science, Reykjav&#x00ED;k University, Iceland</para></footnote> <emphasis role="strong">and Anna Ing&#x00F3;lfsd&#x00F3;ttir</emphasis><footnoteref linkend="fn3_2a" label="2"/></para>
<section class="lev2">
<title>Abstract</title>
<para>This chapter discusses <emphasis role="romanAlt">detectEr</emphasis>, an experimental runtime monitoring tool that can be used to formally verify concurrent systems developed in Erlang. Formal correctness properties in <emphasis role="romanAlt">detectEr</emphasis> are expressed using a monitorable subset of Hennessy-Milner Logic with recursion, and synthesised into actor-based runtime monitors. Our exposition focusses on how the specification logic is enriched and extended with pattern-matching and conditional constructs which allow monitors to be adept at processing the data obtained dynamically from the system&#x2019;s execution trace. The tool leverages the native tracing functionality provided by the Erlang language platform so as to produce asynchronous monitors that can be instrumented to run alongside the system with minimal effort. To demonstrate how <emphasis role="romanAlt">detectEr</emphasis> can be used in practice, this material also provides a hands-on guide that is especially aimed at users wishing to use our tool to monitor Erlang applications.</para>
</section>
<section class="lev1" id="sec3-1" label="3.1" xreflabel="3.1">
<title>Introduction</title>
<para><emphasis>Concurrency</emphasis> [<link linkend="ch03_b30">30</link>] refers to software systems whose functionality is expressed in terms of multiple <emphasis>components</emphasis> or processes that are specifically designed to work simultaneously with each other. In recent years, a <emphasis>concurrency-oriented</emphasis> [<link linkend="ch03_b3">3</link>] approach to software development has become increasingly commonplace, and is greatly favoured over monolithic-style approaches. This is, in part, owed to the rigidity that the latter types of architectures are synonymous with, where attempts at addressing scalability concerns usually lead to notoriously complex and often, inadequate solutions. Instead, concurrency recasts the notion of system design in a way that makes it possible to avail oneself of the multi-processor and multi-core platforms that are prevalent nowadays.</para>
<para>Formally ensuring the correctness of concurrent systems is an arduous, albeit necessary, task, especially since the interactions between fine-grained computational components can easily harbour subtle software bugs. Despite several success stories in their application to real-life applications, static verification techniques such as Model Checking (MC) scale poorly in concurrent scenarios, particularly because the system state space that needs to be <emphasis>exhaustively</emphasis> verified grows exponentially with respect to the size of the system [<link linkend="ch03_b13">13</link>, <link linkend="ch03_b14">14</link>] &#x2013; this is on account of the considerable number of possible execution paths that result from process interleaving. Moreover, situations often arise whereby verification cannot be performed statically (<emphasis>i.e., pre-deployment</emphasis>), as certain application components might not always be available for inspection before the system starts executing (<emphasis>e.g.</emphasis> in systems where functional components such as add-ons are downloaded and installed dynamically at runtime). There are also cases where the internal workings of a component (<emphasis>e.g.</emphasis> source code or execution graph) are not accessible and need to be treated as a black box. In these cases, Runtime Verification (RV) presents an appealing compromise towards ensuring the correctness of component-based applications. It is a <emphasis>lightweight</emphasis> verification technique that analyses the current runtime execution path of the system under scrutiny by considering partial executions incrementally, up to the current execution point [<link linkend="ch03_b17">17</link>, <link linkend="ch03_b26">26</link>]. Its nature inherently circumvents the scalability issues attributed to MC and provides a means for post-deployment verification. Despite these advantages, RV has limited expressiveness and cannot be used to verify arbitrary specifications such as (general) liveness properties [<link linkend="ch03_b27">27</link>].</para>
<para>This chapter discusses the implementation of a prototype RV tool called <emphasis role="romanAlt">detectEr</emphasis>, that targets concurrent, component-based applications written in Erlang. The presented material aspires to introduce this tool from a pragmatic standpoint, and thus omits technical details that may be abstruse to users of the tool. Interested readers should consult previous work [<link linkend="ch03_b4">4</link>, <link linkend="ch03_b19">19</link>, <link linkend="ch03_b21">21</link>] for details regarding the monitor synthesis and runtime behaviour of the monitoring tool.</para>
<para>The content that follows is organised into three sections. Section 3.2 gives a concise overview of the ideas behind RV and monitoring; this is followed by a review of <emphasis role="roman">m</emphasis>HML, the logic used for specifying correctness properties in our tool. Although this section helps to make the presentation self-contained, it may be safely skipped by readers familiar with the subject or merely interested in using the tool. Section 3.3 revisits the logic <emphasis role="roman">m</emphasis>HML from Section 3.2, and examines how it was adapted to address the practical requirements of users wishing to define correctness properties for Erlang concurrent programs. It also very briefly touches on the compilation process that transforms <emphasis role="roman">m</emphasis>HML specification scripts into executable runtime monitors. The final section takes the form of a hands-on tutorial that guides readers through the basic steps that need to be performed in order to instrument an Erlang application with runtime monitors using the tool.</para>
</section>
<section class="lev1" id="sec3-2" label="3.2" xreflabel="3.2">
<title>Background</title>
<para>An executing system results in the generation of a (possibly infinite) sequence of events known as a <emphasis>trace</emphasis>. These events are the upshot of internal or external system behaviours, such as message exchanges between processes or function invocations. An <emphasis>execution</emphasis>, <emphasis>i.e.,</emphasis> a <emphasis>finite prefix</emphasis> of an infinite trace, is consumed and processed by a software entity known as a <emphasis>monitor</emphasis>, tasked with the job of checking whether the execution provides enough evidence so as to determine whether a property is satisfied or violated. <emphasis>Correctness specifications (properties)</emphasis> serve to unambiguously describe the behaviour to which the executing system should adhere to. <emphasis>Verdicts</emphasis> denote monitoring outcomes and are assumed to be <emphasis>definite</emphasis> and non-retractable (<emphasis>i.e.,</emphasis> once given, cannot change). These typically consist of judgements relating to property violations and satisfactions, but may also include <emphasis>inconclusive</emphasis> verdicts for when the exhibited execution trace does not permit any definite judgement in relation to the property being monitored for [<link linkend="ch03_b4">4</link>, <link linkend="ch03_b6">6</link>, <link linkend="ch03_b17">17</link>, <link linkend="ch03_b19">19</link>, <link linkend="ch03_b26">26</link>]. A RV monitor for some correctness property is typically synthesised automatically from a high-level specification that finitely describes the property. Property specifications are given in terms of formal logics [<link linkend="ch03_b4">4</link>, <link linkend="ch03_b6">6</link>, <link linkend="ch03_b7">7</link>, <link linkend="ch03_b19">19</link>] or other formalisms such as regular expressions [<link linkend="ch03_b20">20</link>] or automata [<link linkend="ch03_b5">5</link>, <link linkend="ch03_b15">15</link>, <link linkend="ch03_b29">29</link>]. <link linkend="fig3_1">Figure <xref linkend="fig3_1" remap="3.1"/></link> depicts a correctness specification (denoted by &#x03C6;) that is translated into an executable monitor, Monitor<subscript>&#x03C6;</subscript>, and instrumented with the running system. Trace events are sequentially analysed by the monitor whenever these are generated by the system through the instrumentation mechanism. Once the monitor reaches a verdict, it typically stops executing.</para>
<fig id="fig3_1" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 3.1</label>
<caption><title>Runtime monitor synthesis and operational set-up.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig003.jpg" mime-subtype="jpeg"/>
</fig>
<section class="lev2" id="sec3-2-1" label="3.2.1" xreflabel="3.2.1">
<title>Runtime Monitoring Criteria</title>
<para>Monitor synthesis, <emphasis>i.e.,</emphasis> the translation procedure from specifications to monitors and the associated system instrumentation, should ideally provide some <emphasis>guarantees</emphasis> of correctness. This covers both aspects that relate to how monitor verdicts correspond to the semantics of the property being monitored for (<emphasis>e.g.</emphasis> a monitor trace <emphasis>rejection</emphasis> should correspond to the system <emphasis>violating</emphasis> the property being monitored for), as well as requirements that the monitors instrumented with the executing system under scrutiny do <emphasis>not</emphasis> introduce fresh bugs <emphasis>themselves</emphasis> (consult our previous work [<link linkend="ch03_b9">9</link>, <link linkend="ch03_b18">18</link>, <link linkend="ch03_b19">19</link>, <link linkend="ch03_b21">21</link>] for a detailed rendition on the subject). Equally important is the <emphasis>efficiency</emphasis> with which monitors execute, as this can adversely affect the monitored system or even alter its functional behaviour (<emphasis>e.g.</emphasis> slowdown due to inefficient monitors might cause the system to violate time-dependent properties that would not have been violated in the unmonitored system). A monitoring set-up that induces considerable levels of performance overhead may be deemed too costly to be feasibly used in practice.</para>
</section>
<section class="lev2" id="sec3-2-2" label="3.2.2" xreflabel="3.2.2">
<title>A Branching-Time Logic for Specifying Correctness Properties</title>
<para>Specification logics can be categorised into two classes. <emphasis>Linear-time</emphasis> logics [<link linkend="ch03_b6">6</link>, <link linkend="ch03_b13">13</link>, <link linkend="ch03_b26">26</link>] treat time as having one possible future, and regard the behaviour of a system under observation in terms of execution traces or paths. On the other hand, <emphasis>branching-time</emphasis> logics [<link linkend="ch03_b1">1</link>, <link linkend="ch03_b13">13</link>] make it possible to perceive time instances as potentially having more than one future, thereby giving rise to a <emphasis>tree</emphasis> of possible execution paths that may be (non-deterministically) taken by the executing system at runtime.</para>
<para><emphasis>&#x03BC;</emphasis>HML [<link linkend="ch03_b1">1</link>, <link linkend="ch03_b25">25</link>] is a branching-time logic that can be used to specify correctness properties over <emphasis>Labelled Transition Systems</emphasis> (LTSs) &#x2014; graphs modelling the possible behaviours that can be exhibited by executing processes (see <link linkend="fig3_3">Figure <xref linkend="fig3_3" remap="3.3"/></link> for a depiction of two LTSs). A LTS consists of a set of system states <emphasis>p, q</emphasis> &#x2208; S<emphasis role="smaller">YS</emphasis>, a set of actions <emphasis>&#x03B1;</emphasis> &#x2208; A<emphasis role="smaller">CT</emphasis>, and finally, a ternary transition relation between states labelled by actions, <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/equ053_1.jpg" mime-subtype="jpeg"/>. When <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/equ053_2.jpg" mime-subtype="jpeg"/> for no process <emphasis>q</emphasis>, the notation <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/equ053_3.jpg" mime-subtype="jpeg"/> is used. Additionally, <emphasis>p</emphasis> &#x21D2; <emphasis>q</emphasis> denotes <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/equ053_4.jpg" mime-subtype="jpeg"/>, whereas <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/equ053_5.jpg" mime-subtype="jpeg"/>, is written in place of <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/equ053_6.jpg" mime-subtype="jpeg"/>. Actions labelled by &#x03C4; are used to denote unobservable (silent) actions that are performed by the system internally.</para>
<para>The <emphasis>&#x03BC;</emphasis>HML syntax, given in <link linkend="fig3_2">Figure <xref linkend="fig3_2" remap="3.2"/></link>, assumes a countable set of logical variables <emphasis>X, Y</emphasis> &#x2208; LV<emphasis role="smaller">AR</emphasis>, thereby allowing formulae to recursively express largest and least fixpoints using <emphasis role="strongAlt">max</emphasis> <emphasis>X.&#x03C6;</emphasis> and <emphasis role="strongAlt">min</emphasis> <emphasis>X.&#x03C6;</emphasis> respectively; these constructs bind free instances of the variable <emphasis>X</emphasis> in <emphasis>&#x03C6;</emphasis>. In addition to the standard constructs for truth, falsity, conjunction and disjunction, the syntax also includes the necessity and possibility modalities.</para>
<para>The semantics of the logic is defined in terms of the function mapping <emphasis>&#x03BC;</emphasis>HML formulae <emphasis>&#x03C6;</emphasis> to the set of LTS states <emphasis>S</emphasis> &#x2286; S<emphasis role="smaller">YS</emphasis> satisfying them. <link linkend="fig3_2">Figure <xref linkend="fig3_2" remap="3.2"/></link> describes the semantics for both open and closed formulae, and uses a map <emphasis>&#x03C1;</emphasis> &#x2208; LV<emphasis role="smaller">AR</emphasis> &#x21C0; <emphasis role="roman">2</emphasis><sup>S<emphasis role="smaller">YS</emphasis></sup> from variables to sets of system states to enable an inductive definition on the structure of the formula <emphasis>&#x03C6;</emphasis>. The formula <emphasis role="strongAlt">tt</emphasis> is satisfied by all processes, while <emphasis role="strongAlt">ff</emphasis> is satisfied by none; conjunctions and disjunctions bear the standard set-theoretic meaning of intersection and union. Necessity formulae <emphasis role="roman">[</emphasis><emphasis>&#x03B1;</emphasis><emphasis role="roman">]</emphasis><emphasis>&#x03C6;</emphasis> state that <emphasis>for all</emphasis> system executions producing event <emphasis>&#x03B1;</emphasis> (possibly none), the subsequent system state must then satisfy <emphasis>&#x03C6;</emphasis> (<emphasis>i.e.,</emphasis> <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/equ053_7.jpg" mime-subtype="jpeg"/> implies <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/equ053_8.jpg" mime-subtype="jpeg"/> must hold). Possibility formulae &#x3008;<emphasis>&#x03B1;</emphasis>&#x3009;<emphasis>&#x03C6;</emphasis> require the existence of <emphasis>at least</emphasis> one system execution with event <emphasis>&#x03B1;</emphasis> whereby the subsequent state then satisfies <emphasis>&#x03C6;</emphasis> (<emphasis>i.e.,</emphasis> <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/equ053_9.jpg" mime-subtype="jpeg"/> and <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/equ053_10.jpg" mime-subtype="jpeg"/> must hold). The recursive formulae <emphasis role="strongAlt">max</emphasis> <emphasis>X.&#x03C6;</emphasis> and <emphasis role="strongAlt">min</emphasis> <emphasis>X.&#x03C6;</emphasis> are respectively satisfied by the largest and least set of system states satisfying <emphasis>&#x03C6;</emphasis>. The semantics of recursive variables <emphasis>X</emphasis> with respect to an environment instance <emphasis>&#x03C1;</emphasis> is given by the mapping of <emphasis>X</emphasis> in <emphasis>&#x03C1;</emphasis>, <emphasis>i.e.,</emphasis> the set of processes associated with <emphasis>X</emphasis>. <emphasis>Closed</emphasis> formulae (<emphasis>i.e.,</emphasis> formulae containing no free variables) are interpreted independently of the environment <emphasis>&#x03C1;</emphasis>, and the shorthand <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/equ055_1.jpg" mime-subtype="jpeg"/> is used to denote <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/equ055_2.jpg" mime-subtype="jpeg"/>, <emphasis>i.e.,</emphasis> the set of system states in SYS that satisfy <emphasis>&#x03C6;</emphasis>. In view of this, we say that a system (state) <emphasis>p</emphasis> satisfies some closed formula <emphasis>&#x03C6;</emphasis> whenever <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/equ055_3.jpg" mime-subtype="jpeg"/>, and conversely, that it violates <emphasis>&#x03C6;</emphasis> whenever <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/equ055_4.jpg" mime-subtype="jpeg"/>.</para>
<fig id="fig3_2" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 3.2</label>
<caption><title>The syntax and semantics of <emphasis>&#x03BC;</emphasis>HML.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig004.jpg" mime-subtype="jpeg"/>
</fig>
<para><emphasis role="strong">Example 3.2.1.</emphasis> The <emphasis>&#x03BC;</emphasis>HML formula &#x3008;<emphasis>&#x03B1;</emphasis>&#x3009;<emphasis role="strongAlt">tt</emphasis> describes systems that <emphasis>can</emphasis> produce action <emphasis>&#x03B1;</emphasis>, while <emphasis role="roman">[</emphasis><emphasis>&#x03B1;</emphasis><emphasis role="roman">]</emphasis><emphasis role="strongAlt">ff</emphasis> describes systems that <emphasis>cannot</emphasis> produce action <emphasis>&#x03B1;</emphasis>.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/equ055_5.jpg" mime-subtype="jpeg"/></para>
<para>Formula <emphasis>&#x03C6;</emphasis><subscript>1</subscript> describes a property that prohibits a system from producing duplicate responses in answer to client requests. System <emphasis>p</emphasis> whose LTS is depicted in <link linkend="fig3_3">Figure <xref linkend="fig3_3" remap="3.3"/></link>a <emphasis>violates</emphasis> <emphasis>&#x03C6;</emphasis><subscript>1</subscript> through any trace in the regular language <emphasis role="roman">(</emphasis><literal>req</literal><emphasis>.</emphasis><literal>resp</literal><emphasis role="roman">)</emphasis><sup>+</sup><emphasis>.</emphasis><literal>resp</literal>. Formula <emphasis>&#x03C6;</emphasis><subscript>2</subscript> describes systems that <emphasis>can</emphasis> reach a service limit after a number (possibly zero) of request and response interactions; system <emphasis>q</emphasis> depicted in <link linkend="fig3_3">Figure <xref linkend="fig3_3" remap="3.3"/></link>b <emphasis>satisfies</emphasis> <emphasis>&#x03C6;</emphasis><subscript>2</subscript> through any trace in the regular language <emphasis role="roman">(</emphasis><literal>req</literal><emphasis>.</emphasis><literal>resp</literal><emphasis role="roman">)</emphasis><sup>*</sup><emphasis>.</emphasis><literal>lim</literal>.</para>
</section>
<section class="lev2" id="sec3-2-3" label="3.2.3" xreflabel="3.2.3">
<title>Monitoring <emphasis>&#x03BC;</emphasis>HML</title>
<para>Despite its limitations (<emphasis>i.e.,</emphasis> monitors can only analyse single execution traces), RV can be still effectively applied in cases where correctness properties can be shown to be satisfied (or violated) by analysing a <emphasis>single</emphasis> finite execution. As explained previously, the formula <emphasis role="roman">[</emphasis><emphasis>&#x03B1;</emphasis><emphasis role="roman">]</emphasis><emphasis role="strongAlt">ff</emphasis> states that <emphasis>all</emphasis> <emphasis>&#x03B1;</emphasis>-actions performed by a satisfying system state should satisfy property <emphasis role="strongAlt">ff</emphasis> afterwards. Since no system state can satisfy <emphasis role="strongAlt">ff</emphasis>, the only way how to satisfy <emphasis role="roman">[</emphasis><emphasis>&#x03B1;</emphasis><emphasis role="roman">]</emphasis><emphasis role="strongAlt">ff</emphasis> is for a system <emphasis>not</emphasis> to perform <emphasis>&#x03B1;</emphasis>. From a RV perspective, for a monitor to detect a violation of this requirement, observing <emphasis>one negative witness</emphasis> execution trace that starts with action <emphasis>&#x03B1;</emphasis> suffices to show that property <emphasis>&#x03C6;</emphasis> is infringed. Dually, when monitoring for the formula &#x3008;<emphasis>&#x03B1;</emphasis>&#x3009;<emphasis role="strongAlt">tt</emphasis>, observing <emphasis>one positive witness</emphasis> that starts with action <emphasis>&#x03B1;</emphasis> suffices to show that property <emphasis>&#x03C6;</emphasis> is satisfied.</para>
<fig id="fig3_3" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 3.3</label>
<caption><title>The LTSs depicting the behaviour of two servers <emphasis>p</emphasis> and <emphasis>q</emphasis>.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig005.jpg" mime-subtype="jpeg"/>
</fig>
<para><emphasis role="strong">Example 3.2.2.</emphasis> The <emphasis>&#x03BC;</emphasis>HML formula <emphasis>&#x03C6;</emphasis><subscript>3</subscript> <emphasis role="roman">=</emphasis> &#x3008;<literal>lim</literal>&#x3009;<emphasis role="strongAlt">tt</emphasis> requires that <emphasis>&#8220;a process can perform action</emphasis> <emphasis><literal>lim</literal></emphasis><emphasis>&#8221;</emphasis>. System <emphasis>q</emphasis> in <link linkend="fig3_3">Figure <xref linkend="fig3_3" remap="3.3"/></link>b can exhibit the trace <literal>lim</literal><emphasis>.</emphasis><emphasis>&#x03B5;</emphasis> which suffices to show that system <emphasis>q</emphasis> satisfies <emphasis>&#x03C6;</emphasis><subscript>3</subscript>. Yet, <emphasis>q</emphasis> may also exhibit other traces, such as those matching <emphasis role="roman">(</emphasis><literal>req</literal><emphasis>.</emphasis><literal>resp</literal><emphasis role="roman">)</emphasis><sup>*</sup>, that all start with the event <literal>req</literal>. These traces do not provide enough evidence that system <emphasis>q</emphasis> satisfies <emphasis>&#x03C6;</emphasis><subscript>3</subscript>. Stated otherwise, the monitor for formula <emphasis>&#x03C6;</emphasis><subscript>3</subscript> can reach an <emphasis>acceptance</emphasis> verdict <emphasis>only</emphasis> when a trace starting with event <literal>lim</literal> is observed. Otherwise, no verdict relating to the satisfaction or violation of the formula can be reached; in our specific case, the monitors we consider will reach an inconclusive verdict.</para>
<para>The availability of a single finite runtime trace <emphasis>does</emphasis> however restrict the applicability of RV in cases such as those involving correctness properties describing <emphasis>infinite</emphasis> or <emphasis>branching</emphasis> executions. In view of this, certain properties expressed using the full expressive power of a branching-time logic such as <emphasis>&#x03BC;</emphasis>HML cannot be monitored for at runtime. The work by Francalanza <emphasis>et al.</emphasis> [<link linkend="ch03_b19">19</link>] explores the limits of monitorability for <emphasis>&#x03BC;</emphasis>HML, identifies a syntactic logical subset called <emphasis role="roman">m</emphasis>HML, and shows it to be monitorable and maximally expressive with respect to the constraints of runtime monitoring. The syntax of <emphasis role="roman">m</emphasis>HML, given in <link linkend="fig3_4">Figure <xref linkend="fig3_4" remap="3.4"/></link>, consists of two syntactic classes, <emphasis>Safety</emphasis> HML (<emphasis role="roman">s</emphasis>HML), describing <emphasis>invariant</emphasis> properties stipulating that bad things do <emphasis>not</emphasis> happen, and <emphasis>Co-Safety</emphasis> HML (<emphasis role="roman">c</emphasis>HML), describing properties that <emphasis>eventually</emphasis> hold after a <emphasis>finite</emphasis> number of events [<link linkend="ch03_b2">2</link>, <link linkend="ch03_b6">6</link>, <link linkend="ch03_b23">23</link>]. Formulae <emphasis>&#x03C6;</emphasis><subscript>1</subscript> and <emphasis>&#x03C6;</emphasis><subscript>2</subscript> from Example 3.2.1 are instances of <emphasis role="roman">s</emphasis>HML and <emphasis role="roman">c</emphasis>HML specifications respectively.</para>
<fig id="fig3_4" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 3.4</label>
<caption><title>The syntax of mHML.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig006.jpg" mime-subtype="jpeg"/>
</fig>
</section>
</section>
<section class="lev1" id="sec3-3" label="3.3" xreflabel="3.3">
<title>A Tool for Monitoring Erlang Applications</title>
<para>We briefly review the implementation of our RV tool <emphasis role="romanAlt">detectEr</emphasis> that analyses the correctness of concurrent programs developed in Erlang. It builds on the work by Francalanza <emphasis>et al.</emphasis> [<link linkend="ch03_b19">19</link>] which specifies a synthesis procedure that generates <emphasis>correct</emphasis> monitor descriptions from formulae written in <emphasis role="roman">m</emphasis>HML. We adapt this synthesis procedure so as to produce <emphasis>concurrent</emphasis> monitors in the form of Erlang actors that are instrumented with the running system via the tracing mechanism exposed by the VM of the host language. The synthesis procedure exploits the compositional semantics of <emphasis role="roman">m</emphasis>HML formulae to generate a choreography of monitor (actor) components that independently analyse the individual subformulae constituting a global formula, while still guaranteeing the correctness of the overall monitoring process.</para>
<para>In the sequel we refrain from delving into the specifics of how these concurrent monitors are synthesised; readers are encouraged to consult our previous work [<link linkend="ch03_b4">4</link>, <link linkend="ch03_b21">21</link>], where the synthesis procedure is discussed at length. Instead, we limit ourselves to a high-level description of the main concepts and technologies required by readers to be able to adequately use the monitoring tool. In particular, we discuss the mechanisms of the host language used by the tool, the adaptations to the specification logic that facilitate the handling of data, and finally, give an overview of the tool&#x2019;s compilation process.</para>
<section class="lev2" id="sec3-3-1" label="3.3.1" xreflabel="3.3.1">
<title>Concurrency-Oriented Development Using Erlang</title>
<para>Erlang is a general-purpose, concurrent programming language suitable for the development of fault-tolerant and distributed systems [<link linkend="ch03_b3">3</link>, <link linkend="ch03_b12">12</link>, <link linkend="ch03_b22">22</link>]. It adopts the actor model for concurrency as the primary means for structuring its applications. An <emphasis>actor</emphasis> is a concurrency unit of decomposition that represents a processing entity sharing no mutable memory with other actors. It interacts with other actors by sending (asynchronous) messages, and changes its internal state based on the messages received from other actors. In Erlang, actors are implemented as <emphasis>lightweight</emphasis> processes that are uniquely identified via their process PID (a number triple). Each process owns a message queue, known as a <emphasis>mailbox</emphasis>, to which messages from other processes can be sent in a non-blocking fashion; these can be consumed selectively at a later stage by the recipient process. Messages are comprised of elements of Erlang data types, including integers, floats, atoms, functions, binaries, <emphasis>etc.</emphasis>. Since process PIDs are allocated dynamically to newly spawned processes, Erlang provides a mechanism for registering a PID with a fixed alias name. This allows external entities to refer to a specific process statically via the registered name alias [<link linkend="ch03_b3">3</link>, <link linkend="ch03_b12">12</link>].</para>
<para>The Erlang Virtual Machine (EVM) offers a powerful and flexible <emphasis>tracing</emphasis> mechanism that makes it possible to observe process behaviour <emphasis>without</emphasis> modifying the system source code through commonly used instrumentation techniques such as Aspect Oriented Programming (AOP) [<link linkend="ch03_b3">3</link>, <link linkend="ch03_b12">12</link>]. Its flexibility stems from the fact that it can be <emphasis>selectively</emphasis> applied on specific processes as required, thereby fine tuning the tracing effort to the desired level of granularity. When traced, processes generate <emphasis>action</emphasis> messages that are directed by the Erlang runtime to a specially designated <emphasis>tracer</emphasis> process. Trace messages assume the form of Erlang <emphasis>tuples</emphasis> that describe the nature of trace events (<emphasis>e.g.</emphasis> function calls, message sends and receives, garbage collection triggers, <emphasis>etc.</emphasis>) and are deposited (like any other message) asynchronously inside the tracer&#x2019;s mailbox. Tracing serves as the basis for a number of utilities, including Erlang&#x2019;s text-based tracing facility <emphasis role="romanAlt">dbg</emphasis>, and trace tool builder <emphasis role="romanAlt">ttb</emphasis> [<link linkend="ch03_b3">3</link>]. Our tool, <emphasis role="romanAlt">detectEr</emphasis>, employs this tracing mechanism to achieve <emphasis>lightweight</emphasis> trace event extraction for monitoring purposes; refer to the work by Attard <emphasis>et al.</emphasis> [<link linkend="ch03_b4">4</link>] for further details.</para>
</section>
<section class="lev2" id="sec3-3-2" label="3.3.2" xreflabel="3.3.2">
<title>Reasoning about Data</title>
<para>Adapting <emphasis role="roman">m</emphasis>HML to be used for specifying the behaviour of Erlang programs adequately requires auxiliary functionality that describes system events carrying <emphasis>data</emphasis>; this involves mechanisms for generalising over specific data values and for expressing data dependencies. <emphasis role="romanAlt">detectEr</emphasis> assumes a richer set of system events that carry data. Our account focusses on two types of events, namely outputs <emphasis>i</emphasis> <emphasis role="strongAlt">!</emphasis> <emphasis>d</emphasis> and inputs <emphasis>i</emphasis> <emphasis role="strongAlt">?</emphasis> <emphasis>d</emphasis>, where <emphasis>i</emphasis> ranges over process PIDs, and <emphasis>d</emphasis> denotes the data payload associated with the action in the form of Erlang data values (<emphasis>e.g.</emphasis> PID, lists, tuples, atoms, <emphasis>etc.</emphasis>). In addition, our tool enriches the syntax of <link linkend="fig3_4">Figure <xref linkend="fig3_4" remap="3.4"/></link> by introducing <emphasis>pattern-matching</emphasis> extensions for event actions (see <link linkend="fig3_5">Figure <xref linkend="fig3_5" remap="3.5"/></link>). Necessity and possibility formulae may contain <emphasis>event patterns</emphasis> instead of specific events: these possess the <emphasis>same structure</emphasis> of the aforementioned data-carrying events, but may also employ <emphasis>variables</emphasis> (Erlang-style alphanumeric identifiers starting with an upper-case letter) in place of values. Variables denote quantifications over data and are dynamically bound to values when they are <emphasis>pattern-matched</emphasis> to specific system events at runtime. Event patterns also allow us to express data dependencies across multiple events. Intuitively, whenever a variable is used in a pattern inside a necessity or possibility formula and again in the ensuing guarded subformula, the <emphasis>first</emphasis> variable instance acts as a binder for subsequent variable uses. The next example illustrates this concept.</para>
<fig id="fig3_5" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 3.5</label>
<caption><title>The anatomy of action patterns for the enriched <emphasis role="roman">m</emphasis>HML syntax.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig007.jpg" mime-subtype="jpeg"/>
</fig>
<para><emphasis role="strong">Example 3.3.1.</emphasis> The client-server set-up shown in <link linkend="fig3_6">Figure <xref linkend="fig3_6" remap="3.6"/></link> consists of a successor server process (with PID <literal>&lt;0.33.0&gt;</literal>) that increments the numeric payloads it receives from requesting clients by 1. Client requests should adhere to the following protocol. A client sends a tuple of the form {<emphasis>tag, return_addr, value_to_increment</emphasis>} where the first element is a qualifier tag stating that it is a client request (<emphasis>tag</emphasis> <emphasis role="roman">=</emphasis> <literal>req</literal>). The client then awaits for an answer back from the server in the form of a message with format <literal>{resp,</literal><emphasis>incremented_value</emphasis><emphasis>}</emphasis>. The server obtains the identity of the client from the client request data <emphasis>return_addr</emphasis>, which should carry the PID of the client sending the request (<emphasis>e.g.</emphasis> <literal>&lt;0.38.0&gt;</literal> in the case of <link linkend="fig3_6">Figure <xref linkend="fig3_6" remap="3.6"/></link>). One attempt at verifying the correctness of the executing system is by specifying a safety property stating that</para>
<fig id="fig3_6" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 3.6</label>
<caption><title>Runtime verifying the correctness of a client-server system.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig008.jpg" mime-subtype="jpeg"/>
</fig>
<blockquote>
<para><emphasis>&#8220;the numeric payload contained in the server&#x2019;s response cannot equal the one sent in the original client request.&#8221;</emphasis></para>
</blockquote>
<para>This requirement can be expressed as follows:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/equ060_1.jpg" mime-subtype="jpeg"/></para>
<para>The two necessity constructs in the <emphasis role="roman">s</emphasis>HML formula <emphasis>&#x03C6;</emphasis><subscript>3</subscript> describe a request-response interaction between the client and server processes. The first necessity <emphasis role="roman">[</emphasis><emphasis>Srv</emphasis> <emphasis role="strongAlt">?</emphasis> {<literal>req,</literal> <emphasis>Clt</emphasis><literal>,</literal> <emphasis>Num</emphasis>}<emphasis role="roman">]</emphasis> specifies an <emphasis>input</emphasis> event data pattern that conforms to the structure of the data sent by the client when initiating its interaction with the server (<emphasis>i.e.,</emphasis> the action labelled by <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/equ060_2.jpg" mime-subtype="jpeg"/> in <link linkend="fig3_6">Figure <xref linkend="fig3_6" remap="3.6"/></link>); meanwhile, the second necessity <emphasis role="roman">[</emphasis><emphasis>Clt</emphasis> <emphasis role="strongAlt">!</emphasis> {<literal>resp,</literal> <emphasis>Num</emphasis>}<emphasis role="roman">]</emphasis> <emphasis role="strongAlt">ff</emphasis> specifies an <emphasis>output</emphasis> action data pattern that conforms to the structure of the data sent by the server in reply to the client&#x2019;s request (<emphasis>i.e.,</emphasis> action <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/equ060_3.jpg" mime-subtype="jpeg"/> in <link linkend="fig3_6">Figure <xref linkend="fig3_6" remap="3.6"/></link>). Formula <emphasis>&#x03C6;</emphasis><subscript>3</subscript> matches events in the execution trace whenever the server <emphasis>Srv</emphasis> receives a request with numeric payload <emphasis>Num</emphasis> from client <emphasis>Clt</emphasis>, and replies back to the same client <emphasis>Clt</emphasis> with an unchanged value <emphasis>Num</emphasis>. Note the dependency between the patterns in the two necessities: the values matched to the variable <emphasis>Clt</emphasis> and <emphasis>Num</emphasis> in first pattern are then instantiated in the subsequent necessity pattern.</para>
<para>To illustrate concretely how binding actually works, we can consider how the two different executions of client-server system depicted in <link linkend="fig3_6">Figures <xref linkend="fig3_6" remap="3.6"/>a</link> and <link linkend="fig3_6"><xref linkend="fig3_6" remap="3.6"/>b</link> are monitored at runtime. When the event pattern <emphasis>Srv</emphasis> <emphasis role="strongAlt">?</emphasis> {<literal>req,</literal> <emphasis>Clt</emphasis><literal>,</literal> <emphasis>Num</emphasis>} from the first necessity is matched to the first trace event <literal>&lt;0.33.0&gt;</literal> <emphasis role="strongAlt">?</emphasis> {<literal>req, &lt;0.38.0&gt;, 19</literal>} (resulting from the execution of action <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/equ060_4.jpg" mime-subtype="jpeg"/>), the free pattern variables <emphasis>Srv</emphasis>, <emphasis>Clt</emphasis> and <emphasis>Num</emphasis> become <emphasis>bound</emphasis> to the runtime values <literal>&lt;0.33.0&gt;</literal>, <literal>&lt;0.38.0&gt;</literal> and <literal>19</literal> respectively. The runtime binding of variables <emphasis>Srv</emphasis>, <emphasis>Clt</emphasis> and <emphasis>Num</emphasis> in turn, also instantiates subsequent (guarded) patterns in the second necessity &#x2014; this leaves us with the (continuation) residual formula <emphasis role="roman">[</emphasis><literal>&lt;0.38.0&gt;</literal> <emphasis role="strongAlt">!</emphasis> {<literal>resp, 19</literal>}<emphasis role="roman">]</emphasis> <emphasis role="strongAlt">ff</emphasis> to check for. This closed formula can now match the <emphasis>second</emphasis> trace event (due to action <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/equ060_5.jpg" mime-subtype="jpeg"/> ), <emphasis>only if</emphasis> an <emphasis>incorrectly</emphasis> implemented server responds to the initial client request with the <emphasis>same</emphasis> numeric payload sent to it, as is the case in <link linkend="fig3_6">Figure <xref linkend="fig3_6" remap="3.6"/></link>a. This leads to a <emphasis>violation</emphasis> detection. Contrastingly, <link linkend="fig3_6">Figure <xref linkend="fig3_6" remap="3.6"/></link>b shows the case where the server&#x2019;s reply sent back to the client contains the value &#x2018;20&#x2019; that does not match the runtime binding for the subformula <emphasis role="roman">[</emphasis><emphasis>Clt</emphasis> <emphasis role="strongAlt">!</emphasis> {<literal>resp,</literal> <emphasis>Num</emphasis>}<emphasis role="roman">]</emphasis> <emphasis role="strongAlt">ff</emphasis> of <emphasis>&#x03C6;</emphasis><subscript>3</subscript>. After the first pattern-match, <emphasis>Num</emphasis> is bound to &#x2018;19&#x2019;, and this does not match with event {<literal>resp, 20</literal>} of action <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/equ060_6.jpg" mime-subtype="jpeg"/> in <link linkend="fig3_6">Figure <xref linkend="fig3_6" remap="3.6"/></link>b (<emphasis>Clt</emphasis> is bound to <literal>&lt;0.38.0&gt;</literal> as before), thus leading to an <emphasis>inconclusive</emphasis> verdict.</para>
<section class="lev3" id="sec3-3-2-1" label="3.3.2.1" xreflabel="3.3.2.1">
<title>Properties with specific PIDs</title>
<para>Since process PIDs are allocated at runtime, there is no direct way for a correctness property to refer to a <emphasis>specific</emphasis> process. Nevertheless, the tool still provides an indirect method how to specify this via the process PID registering mechanism offered by the host language. For instance, in the case of formula <emphasis>&#x03C6;</emphasis><subscript>3</subscript> from Example 3.3.1, one could refer to a particular process (instead of <emphasis>any</emphasis> arbitrary process that is dynamically bound to variable <emphasis>Srv</emphasis> in the pattern <emphasis role="roman">[</emphasis><emphasis>Srv</emphasis> <emphasis role="strongAlt">?</emphasis> {<literal>req,</literal> <emphasis>Clt</emphasis><literal>,</literal> <emphasis>Num</emphasis>}<emphasis role="roman">]</emphasis>) using the notation <emphasis role="romanAlt">@</emphasis><emphasis>srv</emphasis> in place of <emphasis>Srv</emphasis>. This would then map to the process that is registered with the fixed (atom) name <emphasis>srv</emphasis> in the system and, subsequently, the respective event analysis would only match events sent specifically to the process whose PID is registered as <emphasis>srv</emphasis>.</para>
</section>
<section class="lev3" id="sec3-3-2-2" label="3.3.2.2" xreflabel="3.3.2.2">
<title>Further reasoning about data</title>
<para>Readers might have been wary of the fact that formula <emphasis>&#x03C6;</emphasis><subscript>3</subscript> in Example 3.3.1 only guards against cases where the server merely <emphasis>echoes</emphasis> back the same numeric payload sent to it by clients. This only partially addresses the ideal correctness requirements, because it does not capture the full behaviour expected of the successor server in <link linkend="fig3_6">Figure <xref linkend="fig3_6" remap="3.6"/></link>. Reformulating the property from Example 3.3.1 to read as</para>
<blockquote>
<para><emphasis>&#8220;the numeric payload contained in the server&#x2019;s response must be equal to the successor of the one sent in the original client request.&#8221;</emphasis></para>
</blockquote>
<para>while more specific, requires the monitor to check whether <emphasis>all</emphasis> responses issued by the server in reply to client requests do in fact contain the successor of the number enclosed in said requests.</para>
<para>Our logic handles this expressiveness requirement by extending the enriched <emphasis role="roman">m</emphasis>HML syntax from this section with conditional constructs and predicates, thus enabling it to perform complex reasoning on data values acquired dynamically through pattern matching. Data predicates<footnote id="fn3_1" label="1"><para>Data predicates are assumed to be decidable (<emphasis>i.e.,</emphasis> guaranteed to terminate). Our implementation makes use of a restricted subset of Erlang side effect-free functions employed in standard guard expressions (<emphasis>e.g.</emphasis> <literal>is_list/1</literal>, <literal>is_number/1</literal>, <literal>is_pid/1</literal>, <emphasis>etc.</emphasis>) [<link linkend="ch03_b12">12</link>].</para></footnote>, together with boolean expressions, are evaluated to values <emphasis>b</emphasis> &#x2208; {<emphasis role="romanAlt">false</emphasis><emphasis>,</emphasis> <emphasis role="romanAlt">true</emphasis>}. Conditionals, written as <emphasis role="strongAlt">if</emphasis> <emphasis>b</emphasis> <emphasis role="strongAlt">then</emphasis> <emphasis>&#x03B8;</emphasis> <emphasis role="strongAlt">else</emphasis> <emphasis>&#x03D1;</emphasis> for <emphasis role="roman">s</emphasis>HML formulae and <emphasis role="strongAlt">if</emphasis> <emphasis>b</emphasis> <emphasis role="strongAlt">then</emphasis> <emphasis>&#x03C0;</emphasis> <emphasis role="strongAlt">else</emphasis> <emphasis>&#x03D6;</emphasis> for <emphasis role="roman">c</emphasis>HML formulae, evaluate to <emphasis>&#x03B8;</emphasis> and <emphasis>&#x03C0;</emphasis> respectively when <emphasis>b</emphasis> evaluates to <emphasis role="romanAlt">true</emphasis>, and to <emphasis>&#x03D1;</emphasis> and <emphasis>&#x03D6;</emphasis> otherwise. The <emphasis role="strongAlt">else</emphasis> clause may be omitted if not required. Correctness formulae of the latter form are given an <emphasis>inconclusive</emphasis> interpretation whenever the boolean condition inside the <emphasis role="strongAlt">if</emphasis> clause evaluates to <emphasis role="romanAlt">false</emphasis>. Conditional constructs increase the expressiveness of <emphasis role="roman">m</emphasis>HML, because they make it possible to formalise properties that are otherwise hard to express using the basic form of the logic. When compiled, conditional formulae are translated into monitors whose runtime analysis branches depending on dynamic decisions made on data obtained at runtime.</para>
<para><emphasis role="strong">Example 3.3.2.</emphasis> The reformulated safety property <emphasis>&#8220;the numeric payload contained in the server&#x2019;s response must be equal to the successor of the one sent in the original client request&#8221;</emphasis> can be specified as follows using the extended <emphasis role="roman">s</emphasis>HML syntax:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/equ062_1.jpg" mime-subtype="jpeg"/></para>
<para>Formula <emphasis>&#x03C6;</emphasis><subscript>4</subscript> differs slightly from the one specified in Example 3.3.1. It introduces a new variable <emphasis>Succ</emphasis> that binds to the server&#x2019;s return value. This, in turn, enables the conditional construct to determine whether the successor operation is correctly implemented by the server, thus ensuring that <emphasis>&#x03C6;</emphasis><subscript>4</subscript> is violated <emphasis>only</emphasis> when the value bound to <emphasis>Succ</emphasis> is not the successor of <emphasis>Num</emphasis>. An inconclusive verdict is assumed by the formula whenever <emphasis role="roman">(</emphasis><emphasis>Succ</emphasis> <emphasis></emphasis> <emphasis role="roman">&#x2260;</emphasis> <emphasis>Num</emphasis> <emphasis role="roman">+1)</emphasis> does not hold, <emphasis>i.e., Succ is</emphasis> indeed the successor of <emphasis>Num</emphasis>, as in the case of <link linkend="fig3_6">Figure <xref linkend="fig3_6" remap="3.6"/></link>b.</para>
</section>
</section>
<section class="lev2" id="sec3-3-3" label="3.3.3" xreflabel="3.3.3">
<title>Monitor Compilation</title>
<para>Following closely the synthesis function of [<link linkend="ch03_b4">4</link>], our tool is able to parse <emphasis role="roman">m</emphasis>HML formulae and generate Erlang code that monitors for the input formulae. The inherent concurrency features offered by Erlang, together with the modular structure of the synthesis function are used to translate formulae into choreographed collections of (sub)monitors. These are expressed as concurrent <emphasis>processes</emphasis> that execute independently of one another and analyse different parts of the exhibited system trace (<emphasis>e.g.</emphasis> one submonitor may be analysing the second event in an execution trace of length five, whereas another may forge ahead and analyse the fourth event in the trace). In order to ensure that submonitors have access to the same trace events, they are organised as supervision trees [<link linkend="ch03_b3">3</link>, <link linkend="ch03_b12">12</link>]: the (parent) monitor to which the submonitors are attached <emphasis>forks</emphasis> (<emphasis>i.e.,</emphasis> replicates and forwards) individual trace events to its children. The moment a verdict is reached by any submonitor process, all monitoring processes are terminated, and said verdict is used to declare the final monitoring outcome. Interested readers are referred to our previous work [<link linkend="ch03_b4">4</link>, <link linkend="ch03_b21">21</link>] for details on how these monitor choreographies are organised.</para>
<para><link linkend="fig3_7">Figure <xref linkend="fig3_7" remap="3.7"/></link> outlines the compilation steps required to transform a formula script file (<emphasis>e.g.</emphasis> <literal>script.hml</literal>) into a corresponding Erlang source code implementing the monitor functionality (<emphasis>e.g.</emphasis> <literal>monitor.erl</literal>). The tool instruments the synthesised monitors to run asynchronously with the system to be analysed using the native tracing functionality provided by the EVM. Crucially, this type of instrumentation requires <emphasis>no changes to the monitor source code</emphasis> (or the target system binaries). In <link linkend="fig3_7">Figure <xref linkend="fig3_7" remap="3.7"/></link>, the file packaging component of the compiler leaves the system source files unchanged; this increases confidence in the correctness of the resulting monitoring set-up. In addition to the monitor source file, <link linkend="fig3_7">Figure <xref linkend="fig3_7" remap="3.7"/></link> shows also a second module, <literal>launcher.erl</literal>, that is generated automatically based on the specified system start up configuration. The launcher is tasked with the responsibility of starting the system and corresponding monitors in tandem. Said modules, together with other supporting tool-related source code files are afterwards compiled into executable modules (<literal>.beam</literal> files), which are then packaged and placed alongside other system binary files.</para>
</section>
</section>
<section class="lev1" id="sec3-4" label="3.4" xreflabel="3.4">
<title>detectEr in Practice</title>
<para>We revisit the runtime monitoring tool depicted in <link linkend="fig3_7">Figure <xref linkend="fig3_7" remap="3.7"/></link> from a user&#x2019;s perspective, and present a brief guide showcasing its main functionality. This guide, presented in the form of a tutorial, goes through the steps required to apply our tool to monitor an Erlang implementation of the client-server system seen earlier in Example 3.3.1. It shows how a simple (but useful) safety property can be scripted as a <emphasis role="roman">s</emphasis>HML formula, and compiled into a runtime monitor that is used to verify the incorrect and correct behaviour of the successor server illustrated in <link linkend="fig3_6">Figures <xref linkend="fig3_6" remap="3.6"/>a</link> and <link linkend="fig3_6"><xref linkend="fig3_6" remap="3.6"/>b</link>. <emphasis role="roman">c</emphasis>HML properties from <link linkend="fig3_4">Figure <xref linkend="fig3_4" remap="3.4"/></link> can also be monitored for using the same sequence of steps.</para>
<fig id="fig3_7" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 3.7</label>
<caption><title>The monitor synthesis process and instrumentation pipeline.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig009.jpg" mime-subtype="jpeg"/>
</fig>
<para>The current prototype tool implementation is capable of instrumenting only one monitor inside the target system. Nevertheless, the tool&#x2019;s compilation and instrumentation processes were developed with extensibility in mind, and the steps that are outlined in the following tutorial will remain valid once the tool is extended to support multiple monitors. Although the example presented in this guide is fairly basic, it conveys the essence of how the tool should be applied in practice; more complex properties [<link linkend="ch03_b8">8</link>, <link linkend="ch03_b10">10</link>] would be approached following the same instructions and procedures outlined in the coming sections.</para>
<section class="lev2" id="sec3-4-1" label="3.4.1" xreflabel="3.4.1">
<title>Creating the Target System</title>
<para>The initial distribution of the tool is available from <literal>https://bitbucket.org/duncanatt/detecter-lite</literal>, and requires a working installation of Erlang. This guide assumes that GNU <emphasis role="romanAlt">make</emphasis> is installed on the host system. OSX users can acquire <emphasis role="romanAlt">make</emphasis> by installing the <emphasis role="romanAlt">XCode</emphasis> Command Line Tools; Windows users can install the <emphasis role="romanAlt">MinGW</emphasis> suite of tools. Although Linux was used to create this tutorial, the steps below can be replicated on any other operating system.</para>
<section class="lev3" id="sec3-4-1-1" label="3.4.1.1" xreflabel="3.4.1.1">
<title>Setting up the Erlang project</title>
<para>To facilitate the development of Erlang applications, <emphasis role="romanAlt">detectEr</emphasis> includes a generic makefile which we use in this guide. The following <emphasis role="romanAlt">make</emphasis> targets are provided:</para>
<itemizedlist mark="bullet" spacing="normal">
<listitem><para><literal>init</literal>: Creates the standard Erlang project structure;</para></listitem>
<listitem><para><literal>clean</literal>: Removes Erlang <literal>.beam</literal> and other temporary files;</para></listitem>
<listitem><para><literal>all</literal>: Cleans and compiles the Erlang project;</para></listitem>
<listitem><para><literal>instrument</literal>: Synthesises and instruments monitors into the target system, given the HML script, target system binary directory, and application entry point configuration.</para></listitem>
</itemizedlist>
<para>We begin by creating a target directory called <literal>example</literal>. This contains the client-server system Erlang project and all its associated source code files. At the root of the <literal>example</literal> directory, we also place the aforementioned makefile, since this is used to manage the build process of our simple Erlang application. The latest version of the makefile can be downloaded directly from the project site using <emphasis role="romanAlt">wget</emphasis>:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog058.jpg" mime-subtype="jpeg"/></para>
<para>Once the makefile is downloaded, the standard Erlang directory structure is created using the <literal>init</literal> target:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog059.jpg" mime-subtype="jpeg"/></para>
<para>To avoid writing the Erlang server manually from scratch, the guide borrows a number of sample source code files that are included in the tool&#x2019;s distribution. For simplicity, we assume that the tool is set up in the same directory as our <literal>example</literal> project directory. The <literal>plus one</literal> module that forms part of the tool distribution, implements a version of the successor server as described in <link linkend="fig3_6">Figure <xref linkend="fig3_6" remap="3.6"/></link>. This file, together with its dependencies should be copied into the <literal>src</literal> and <literal>include</literal> directories as shown below; these commands result in the creation of a directory structure that corresponds to the one shown in <link linkend="fig3_8">Figure <xref linkend="fig3_8" remap="3.8"/></link>a.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog060.jpg" mime-subtype="jpeg"/></para>
<para>After the files have been copied successfully into their respective directories, the Erlang project can be built by invoking <emphasis role="romanAlt">make</emphasis>:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog061.jpg" mime-subtype="jpeg"/></para>
<fig id="fig3_8" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 3.8</label>
<caption><title>Creating the Erlang project directory structure.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig010.jpg" mime-subtype="jpeg"/>
</fig>
</section>
<section class="lev3" id="sec3-4-1-2" label="3.4.1.2" xreflabel="3.4.1.2">
<title>Running and testing the server</title>
<para>With the build now completed, the <literal>plus_one</literal> successor server can be launched and tested. Since we have not developed a complete application, but only the server part, testing is conducted using the Erlang shell in place of a full client implementation. For illustrative purposes, the <literal>plus_one</literal> server may exhibit different behaviours at runtime depending on the flag it is started up with. Concretely, the <literal>plus_one</literal> server and shell can be launched from the terminal as follows:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog062.jpg" mime-subtype="jpeg"/></para>
<para>The <literal>plus_one</literal> server is intentionally started using the startup flag <literal>bad</literal>, in order to simulate the <emphasis>incorrect</emphasis> server behaviour depicted in <link linkend="fig3_6">Figure <xref linkend="fig3_6" remap="3.6"/></link>a. This serves its purpose later when scripting the formula used to verify the server&#x2019;s behaviour. We can confirm that the server started up successfully by ensuring that the <literal>plus_one</literal> start up log (line 6) shows up in the terminal. Once loaded, the server can be tested by submitting requests to it using the Erlang <literal>!</literal> (send) operator (line 8 below). Following the protocol outlined in Example 3.3.1, the test request is sent to the process identified by the Erlang registered process name <literal>plus_one</literal>. This test request observes the tuple format {<literal>req,</literal> <emphasis>return_addr</emphasis><literal>,</literal><emphasis>value_to_increment</emphasis>}, where <emphasis>return_addr</emphasis> corresponds to the PID of the sender actor (in this case, the Erlang shell), and <emphasis>value_to_increment</emphasis> contains the actual numeric data payload, <emphasis>i.e.,</emphasis> the number the client wishes to increment. In Erlang, a process may obtain its own PID through the function call <literal>self()</literal>. Note that commands typed in the Erlang shell must terminate with a period symbol, otherwise these will not be processed.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog063.jpg" mime-subtype="jpeg"/></para>
<para>As can be gleaned from the logs above, the <literal>plus_one</literal> server receives the number &#x2018;19&#x2019; as payload, and echoes back that same value to the shell (lines 10&#x2013;11). A correct implementation of the server should have replied with a value of &#x2018;20&#x2019;, that corresponds to the client&#x2019;s request being incremented by &#x2018;1&#x2019;. The server&#x2019;s response can be extracted from the Erlang shell by invoking the <literal>flush()</literal> function to empty the shell&#x2019;s mailbox (line 14). After confirming that the server is working (incorrectly) as intended, the Erlang shell can be closed by typing &#8220;<literal>q().</literal>&#8221; at the terminal.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog064.jpg" mime-subtype="jpeg"/></para>
</section>
</section>
<section class="lev2" id="sec3-4-2" label="3.4.2" xreflabel="3.4.2">
<title>Instrumenting the Test System</title>
<para>We are now in a position to generate a monitor that verifies the safety property below, a generalisation of the property discussed earlier in Example 3.3.1:</para>
<blockquote>
<para><emphasis>&#8220;After any sequence of request-response interactions with arbitrary clients, the numeric payload contained in the server&#x2019;s response following a client request must never equal the one sent in the original client request.&#8221;</emphasis></para>
</blockquote>
<para>The monitor synthesised for this property should detect the violating behaviour exhibited by the <literal>plus_one</literal> server.</para>
<section class="lev3" id="sec3-4-2-1" label="3.4.2.1" xreflabel="3.4.2.1">
<title>Property specification</title>
<para>Properties using our tool are specified in plain text files that are processed to produce monitors in the form of Erlang code. These, together with other supporting source files, are compiled to executable Erlang <literal>.beam</literal> files and copied into the target system&#x2019;s binary directory, <literal>ebin</literal>. As explained in Section 3.3.3, the tool also creates a <literal>launcher</literal> module that is used to bootstrap the system together with the synthesised monitor. Once loaded, the system executes as it normally would, while concurrently, the monitor passively observes the system&#x2019;s behaviour expressed in terms of the messages exchanged between it and its environment. A violation will be promptly flagged when discovered by the monitor analysing the trace generated by our successor server. The aforestated safety property can be scripted by pasting the <emphasis role="roman">s</emphasis>HML formula given below into a plain text editor, and saving it as <literal>prop.hml</literal> <emphasis>in</emphasis> the <literal>example</literal> directory.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog065.jpg" mime-subtype="jpeg"/></para>
<para>This <emphasis>recursive</emphasis> <emphasis role="roman">s</emphasis>HML formula makes use of a conjunction (<literal>&#x0026;&#x0026;</literal>) construct to express the two possible behaviours expected of the system. The violating behaviour, specified using <literal>[</literal><literal><emphasis>Srv</emphasis></literal> <literal>?</literal> {<literal>req,</literal> <literal><emphasis>Clt</emphasis></literal><literal>,</literal> <literal><emphasis>Num</emphasis></literal> }<literal>][</literal><literal><emphasis>Clt</emphasis></literal> <literal>!</literal> {<literal>resp,</literal> <literal><emphasis>Num</emphasis></literal> }<literal>] ff</literal>, demands that a violation be flagged when the server <literal><emphasis>Srv</emphasis></literal> receives a request containing <literal><emphasis>Num</emphasis></literal> from client <literal><emphasis>Clt</emphasis></literal>, and returns to <literal><emphasis>Clt</emphasis></literal> the same value <literal><emphasis>Num</emphasis></literal>. The recursive (non-violating) behaviour, expressed by <literal>[</literal><literal><emphasis>Srv</emphasis></literal> <literal>?</literal> {<literal>req,</literal> <literal><emphasis>Clt</emphasis></literal><literal>,</literal> <literal><emphasis>Num</emphasis></literal> }<literal>][</literal><literal><emphasis>Clt</emphasis></literal> <literal>!</literal> {<literal>resp,</literal> <literal><emphasis>Other</emphasis></literal> }<literal>] &#x2018;X&#x2019;</literal>, requires the monitor to recurse whenever a request received from <literal><emphasis>Clt</emphasis></literal> is answered with some value <literal><emphasis>Other</emphasis></literal>, <emphasis>i.e.,</emphasis> not just the successor of <literal><emphasis>Num</emphasis></literal>. This is in line with the property above, as it requires the monitor to detect violations <emphasis>only</emphasis> when the same value of <literal><emphasis>Num</emphasis></literal> is returned by a server in reply to a client&#x2019;s request. Recursion, made possible by the maximal fixpoint construct <literal>max(&#x2018;X&#x2019;, ...)</literal> and the recursive variable <literal>&#x2018;X&#x2019;</literal>, allows the monitor to unfold repeatedly, thereby <emphasis>continuously</emphasis> analysing the system trace until the violating behaviour is detected. Note that the formula in <literal>prop.hml</literal> is an extension of the simpler property <emphasis>&#x03C6;</emphasis><subscript>3</subscript> from Example 3.3.1. In <emphasis>&#x03C6;</emphasis><subscript>3</subscript>, the absence of recursion restricts the corresponding monitor to analyse, at most, two trace events before terminating. Note also that a more comprehensive interpretation of the aforementioned correctness property would of course require the formula to check that each number in the server&#x2019;s response is actually the successor of the one sent in the client&#x2019;s request, as discussed earlier in Example 3.3.2. This can be expressed by modifying line 4 in the above script to</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog066.jpg" mime-subtype="jpeg"/></para>
<para>In what follows, we stick to the weaker variant of the property to simplify our presentation.</para>
</section>
<section class="lev3" id="sec3-4-2-2" label="3.4.2.2" xreflabel="3.4.2.2">
<title>Monitor synthesis and instrumentation</title>
<para>The monitor corresponding to the <emphasis role="roman">s</emphasis>HML script created above is synthesised using the <literal>instrument</literal> target from the application makefile:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog067.jpg" mime-subtype="jpeg"/></para>
<para>The <literal>instrument</literal> target requires the following command line arguments:</para>
<itemizedlist mark="bullet" spacing="normal">
<listitem><para><literal>hml</literal>: The relative or absolute path that leads to the formula script file;</para></listitem>
<listitem><para><literal>app-bin-dir</literal>: The target application&#x2019;s binary base directory;</para></listitem>
<listitem><para><literal>MFA</literal>: The target application&#x2019;s entry point function, encoded as a {<literal>Mod, Fun, [Args]</literal>} tuple, where we specify the <literal>plus_one</literal> module&#x2019;s <literal>start</literal> function passing <literal>bad</literal> as argument, like previously.</para></listitem>
</itemizedlist>
<para>Monitor synthesis and instrumentation (refer to <link linkend="fig3_7">Figure <xref linkend="fig3_7" remap="3.7"/></link>) results in the Erlang project directory structure shown in <link linkend="fig3_8">Figure <xref linkend="fig3_8" remap="3.8"/></link>b. All the original target system binaries remain untouched, and the <literal>plus_one</literal> server application can be still run without monitors, as before (see Section 3.4.1.2).</para>
</section>
<section class="lev3" id="sec3-4-2-3" label="3.4.2.3" xreflabel="3.4.2.3">
<title>Running the monitored system</title>
<para>The instrumented system can be started up by using the automatically generated <literal>launcher</literal> module as shown:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog068.jpg" mime-subtype="jpeg"/></para>
<para>As indicated by the above logs, the <literal>plus_one</literal> server and corresponding monitor are now executing in parallel with PIDs <literal>&lt;0.33.0&gt;</literal> and <literal>&lt;0.34.0&gt;</literal> that are dynamically assigned at runtime once the respective processes are spawned (lines 6&#x2013;7). The synthesised monitor corresponding to the recursion in the formula of Section 3.4.2.1 eagerly unfolds one iteration of the formula (lines 10&#x2013;11) exposing a conjunction construct at top level (see Francalanza <emphasis>et al.</emphasis> [<link linkend="ch03_b21">21</link>] for a detailed discussion of how recursion is handled in the synthesised monitors). The &#8220;conjunction monitor&#8221; <literal>mon_and</literal> spawns its two submonitor actors once it starts executing (line 12); these correspond to the violation submonitor created from subformula <literal>[</literal><literal><emphasis>Srv</emphasis></literal> <literal>?</literal> {<literal>req,</literal> <literal><emphasis>Clt</emphasis></literal><literal>,</literal> <literal><emphasis>Num</emphasis></literal> }<literal>][</literal><literal><emphasis>Clt</emphasis></literal> <literal>!</literal> {<literal>resp,</literal> <literal><emphasis>Num</emphasis></literal> }<literal>]ff</literal> and the recursive submonitor created from <literal>[</literal><literal><emphasis>Srv</emphasis></literal> <literal>?</literal> {<literal>req,</literal> <literal><emphasis>Clt</emphasis></literal><literal>,</literal> <literal><emphasis>Num</emphasis></literal> }<literal>][</literal><literal><emphasis>Clt</emphasis></literal> <literal>!</literal> {<literal>resp,</literal> <literal><emphasis>Other</emphasis></literal> }<literal>] &#x2018;X&#x2019;</literal>. As before, the server is tested using the same request, sent from the Erlang shell (line 15):</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog069.jpg" mime-subtype="jpeg"/></para>
<para>The violation (PID <literal>&lt;0.41.0&gt;</literal>) and recursive (PID <literal>&lt;0.42.0&gt;</literal>) submonitor processes acquire trace events from their parent &#8220;conjunction monitor&#8221; process <literal>mon_and</literal> as soon as new trace events are reported by the EVM. For instance, the trace event generated by the message {<literal>req, self(), 19</literal>} sent from the shell is forwarded by <literal>mon_and</literal> to its child submonitors (lines 18&#x2013;19). Next, the <literal>plus_one</literal> server computes the result and sends it back to the Erlang shell (line 20). This causes the second trace event to be generated by the system and reported by the EVM&#x2019;s tracing mechanism; once again this trace event is forwarded to, and processed by both submonitors (lines 23&#x2013;24). At this point, the recursive submonitor tries to unfold in preparation for the next computation (line 26), while the violation submonitor flags a violation verdict <emphasis role="strongAlt">ff</emphasis> (line 25), which is in turn sent to the main monitor. As a <emphasis>single</emphasis> detection suffices to ensure a global verdict, the main monitor terminates accordingly with <emphasis role="strongAlt">ff</emphasis> (line 29); consult the work by Attard <emphasis>et al.</emphasis> [<link linkend="ch03_b4">4</link>] for reasons on why this is the case.</para>
</section>
<section class="lev3" id="sec3-4-2-4" label="3.4.2.4" xreflabel="3.4.2.4">
<title>Running the correct server</title>
<para>So far, the <literal>plus_one</literal> successor server has been intentionally launched in <literal>bad</literal> mode in order to demonstrate how violations are handled by our monitor. We now re-instrument the system in order to emulate the correct successor server behaviour depicted in <link linkend="fig3_6">Figure <xref linkend="fig3_6" remap="3.6"/></link>b; invoking the <literal>instrument</literal> target differs only in the MFA tuple used to start the server, where instead of <literal>bad</literal>, the flag <literal>good</literal> is used:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog070.jpg" mime-subtype="jpeg"/></para>
<para>The server should now behave correctly, and return the successor value of any numeric payload that we choose to send to it from the Erlang shell.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog071.jpg" mime-subtype="jpeg"/></para>
<para>When the client request {<literal>req, self(), 19</literal>} is submitted to the server from the Erlang shell (line 15), this again generates a response from the server answering back with the tuple {<literal>resp,20</literal>}. Although the sequence of trace events is similar to the ones in Section 3.4.2.3, the data in these events is <emphasis>different</emphasis>: the server response now carries value &#x2018;20&#x2019; as opposed to &#x2018;19&#x2019;. This causes the violation submonitor to terminate with an inconclusive verdict (line 25) and the recursive submonitor to unfold (line 26) in preparation for the next trace events. Stated otherwise, no violation is detected by the monitor up to the current point of execution.</para>
</section>
</section>
</section>
<section class="lev1" id="sec3-5" label="3.5" xreflabel="3.5">
<title>Conclusion</title>
<para>We have presented an overview of <emphasis role="romanAlt">detectEr</emphasis> from the perspective of a user wishing to employ this tool to verify Erlang systems at runtime. The tool automatically synthesises monitoring code from specifications written in the monitorable subset of the Hennessy-Milner Logic with maximal and minimal fixpoints [<link linkend="ch03_b25">25</link>, <link linkend="ch03_b19">19</link>]. The monitoring code which is then instrumented to run alongside the system under scrutiny infers specification satisfactions or violations by analysing the runtime execution trace exhibited by the system. One salient aspect of the tool is that the instrumentation employs the tracing facility of the host language virtual machine. It therefore requires no access to system source code and relies only on the application&#x2019;s binary files. The execution of the monitor and the system being analysed is decoupled &#x2014; this may lead to late (satisfaction or violation) detections from the monitor. In spite of this, the lightweight instrumentation approach adopted by <emphasis role="romanAlt">detectEr</emphasis> leaves the target system binaries untouched, thus making it possible to employ our tool in cases where (commercial) software with licenses and/or support agreements explicitly forbid the modification of binary code.</para>
<section class="lev2" id="sec3-5-1" label="3.5.1" xreflabel="3.5.1">
<title>Related and Future Work</title>
<para>Apart from being a manifestation of the work due to Francalanza <emphasis>et al.</emphasis> [<link linkend="ch03_b19">19</link>], the tool <emphasis role="romanAlt">detectEr</emphasis> was also used as a starting point for a number of other investigations. Cassar <emphasis>et al.</emphasis> [<link linkend="ch03_b11">11</link>] explored choreographed reconfigurations for submonitors as means to lower the monitoring computational overhead, whereas in subsequent work [<link linkend="ch03_b8">8</link>], the authors also explored modifications to the tool to be able to synchronise more closely the executions of the system and the monitor, thereby avoiding problems associated with late detections. In other work by Cassar <emphasis>et al.</emphasis> [<link linkend="ch03_b10">10</link>], the investigators consider extensions to the tool that enable the runtime analysis to administer adaptation actions to the system once a violation is detected. Following this work, the authors also developed a type-based approach [<link linkend="ch03_b9">9</link>] to ensure that runtime adaptations are administered correctly by the tool. We are presently considering tool extensions that enable monitoring analysis to be distributed across sites and also alternative monitor synthesis procedures that guarantee a degree of property enforcement.</para>
<para>There has also been an extensive body of work [<link linkend="ch03_b16">16</link>, <link linkend="ch03_b28">28</link>] on the runtime checking of session types. Lange <emphasis>et al.</emphasis> [<link linkend="ch03_b24">24</link>] demonstrate the correspondence between session types and a fragment of the modal <emphasis>&#x03BC;</emphasis>-calculus, which has been previously shown by Larsen [<link linkend="ch03_b25">25</link>] to be a reformulation of the logic <emphasis>&#x03BC;</emphasis>HML. Crucially, the monitors we study consider the system from a global level. By contrast, the aforementioned works project global multiparty session types to local endpoint types, which are then synthesised into local monitors that analyse traffic at individual channel endpoints.</para>
<para><emphasis role="strong">Acknowledgments</emphasis> This work was partly supported by the project &#8220;TheoFoMon: Theoretical Foundations for Monitorability&#8221; (nr.163406-051) of the Icelandic Research Fund.</para>
</section>
</section>
<section class="lev1" id="sec2a">
<title>References</title>
<orderedlist numeration="arabic" continuation="restarts" spacing="normal">
<listitem><para><anchor id="ch03_b1"/>Luca Aceto, Anna Ing&#x00F3;lfsd&#x00F3;ttir, Kim Guldstrand Larsen, and Jiri Srba. <emphasis>Reactive Systems: Modelling, Specification and Verification</emphasis>. Cambridge Univ. Press, New York, NY, USA, first edition, 2007. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Luca+Aceto%2C+Anna+Ing%F3lfsd%F3ttir%2C+Kim+Guldstrand+Larsen%2C+and+Jiri+Srba%2E+Reactive+Systems%3A+Modelling%2C+Specification+and+Verification%2E+Cambridge+Univ%2E+Press%2C+New+York%2C+NY%2C+USA%2C+first+edition%2C+2007%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch03_b2"/>Bowen Alpern and Fred B. Schneider. Recognizing Safety and Liveness. <emphasis>Distributed Computing</emphasis>, 2(3):117&#x2013;126, 1987. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Bowen+Alpern+and+Fred+B%2E+Schneider%2E+Recognizing+Safety+and+Liveness%2E+Distributed+Computing%2C+2%283%29%3A117-126%2C+1987%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch03_b3"/>Joe Armstrong. <emphasis>Programming Erlang: Software for a Concurrent World</emphasis>. Pragmatic Bookshelf, first edition, 2007. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Joe+Armstrong%2E+Programming+Erlang%3A+Software+for+a+Concurrent+World%2E+Pragmatic+Bookshelf%2C+first+edition%2C+2007%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch03_b4"/>Duncan Paul Attard and Adrian Francalanza. A Monitoring Tool for a Branching-Time Logic. In <emphasis>RV</emphasis>, volume 10012 of <emphasis>LNCS</emphasis>, pages 473&#x2013;481. Springer, 2016. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Duncan+Paul+Attard+and+Adrian+Francalanza%2E+A+Monitoring+Tool+for+a+Branching-Time+Logic%2E+In+RV%2C+volume+10012+of+LNCS%2C+pages+473-481%2E+Springer%2C+2016%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch03_b5"/>Howard Barringer, Yli&#x00E8;s Falcone, Klaus Havelund, Giles Reger, and David E. Rydeheard. Quantified Event Automata: Towards Expressive and Efficient Runtime Monitors. In <emphasis>FM</emphasis>, volume 7436 of <emphasis>LNCS</emphasis>, pages 68&#x2013;84. Springer, 2012. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Howard+Barringer%2C+Yli%E8s+Falcone%2C+Klaus+Havelund%2C+Giles+Reger%2C+and+David+E%2E+Rydeheard%2E+Quantified+Event+Automata%3A+Towards+Expressive+and+Efficient+Runtime+Monitors%2E+In+FM%2C+volume+7436+of+LNCS%2C+pages+68-84%2E+Springer%2C+2012%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch03_b6"/>Andreas Bauer, Martin Leucker, and Christian Schallhart. Runtime Verification for LTL and TLTL. <emphasis>ACM Trans. Softw. Eng. Methodol.</emphasis>, 20(4):14, 2011. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Andreas+Bauer%2C+Martin+Leucker%2C+and+Christian+Schallhart%2E+Runtime+Verification+for+LTL+and+TLTL%2E+ACM+Trans%2E+Softw%2E+Eng%2E+Methodol%2E%2C+20%284%29%3A14%2C+2011%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch03_b7"/>Andreas Klaus Bauer and Yli&#x00E8;s Falcone. Decentralised LTL Monitoring. In <emphasis>FM</emphasis>, volume 7436 of <emphasis>LNCS</emphasis>, pages 85&#x2013;100. Springer, 2012. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Andreas+Klaus+Bauer+and+Yli%E8s+Falcone%2E+Decentralised+LTL+Monitoring%2E+In+FM%2C+volume+7436+of+LNCS%2C+pages+85-100%2E+Springer%2C+2012%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch03_b8"/>Ian Cassar and Adrian Francalanza. On Synchronous and Asynchronous Monitor Instrumentation for Actor-Based Systems. In <emphasis>FOCLASA</emphasis>, volume 175 of <emphasis>EPTCS</emphasis>, pages 54&#x2013;68, 2014. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Ian+Cassar+and+Adrian+Francalanza%2E+On+Synchronous+and+Asynchronous+Monitor+Instrumentation+for+Actor-Based+Systems%2E+In+FOCLASA%2C+volume+175+of+EPTCS%2C+pages+54-68%2C+2014%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch03_b9"/>Ian Cassar and Adrian Francalanza. Runtime Adaptation for Actor Systems. In <emphasis>RV</emphasis>, volume 9333 of <emphasis>LNCS</emphasis>, pages 38&#x2013;54. Springer, 2015. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Ian+Cassar+and+Adrian+Francalanza%2E+Runtime+Adaptation+for+Actor+Systems%2E+In+RV%2C+volume+9333+of+LNCS%2C+pages+38-54%2E+Springer%2C+2015%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch03_b10"/>Ian Cassar and Adrian Francalanza. On Implementing a Monitor-Oriented Programming Framework for Actor Systems. In <emphasis>IFM</emphasis>, volume 9681 of <emphasis>LNCS</emphasis>, pages 176&#x2013;192. Springer, 2016. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Ian+Cassar+and+Adrian+Francalanza%2E+On+Implementing+a+Monitor-Oriented+Programming+Framework+for+Actor+Systems%2E+In+IFM%2C+volume+9681+of+LNCS%2C+pages+176-192%2E+Springer%2C+2016%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch03_b11"/>Ian Cassar, Adrian Francalanza, and Simon Said. Improving Runtime Overheads for detectEr. In <emphasis>FESCA</emphasis>, volume 178 of <emphasis>EPTCS</emphasis>, pages 1&#x2013;8, 2015. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Ian+Cassar%2C+Adrian+Francalanza%2C+and+Simon+Said%2E+Improving+Runtime+Overheads+for+detectEr%2E+In+FESCA%2C+volume+178+of+EPTCS%2C+pages+1-8%2C+2015%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch03_b12"/>Francesco Cesarini and Simon Thompson. <emphasis>Erlang Programming</emphasis>. O&#x2019;Reilly Media, first edition, 2009. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Francesco+Cesarini+and+Simon+Thompson%2E+Erlang+Programming%2E+O%27Reilly+Media%2C+first+edition%2C+2009%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch03_b13"/>Edmund M. Clarke, Orna Grumberg, and Doron A. Peled. <emphasis>Model Checking</emphasis>. The MIT Press, first edition, 1999. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Edmund+M%2E+Clarke%2C+Orna+Grumberg%2C+and+Doron+A%2E+Peled%2E+Model+Checking%2E+The+MIT+Press%2C+first+edition%2C+1999%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch03_b14"/>Edmund M. Clarke, William Klieber, Milos Nov&#x00E1;cek, and Paolo Zuliani. Model Checking and the State Explosion Problem. In <emphasis>LASER</emphasis>, volume 7682 of <emphasis>LNCS</emphasis>, pages 1&#x2013;30. Springer, 2011. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Edmund+M%2E+Clarke%2C+William+Klieber%2C+Milos+Nov%E1cek%2C+and+Paolo+Zuliani%2E+Model+Checking+and+the+State+Explosion+Problem%2E+In+LASER%2C+volume+7682+of+LNCS%2C+pages+1-30%2E+Springer%2C+2011%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch03_b15"/>Christian Colombo, Adrian Francalanza, and Rudolph Gatt. Elarva: A Monitoring Tool for Erlang. In <emphasis>RV</emphasis>, volume 7186 of <emphasis>LNCS</emphasis>, pages 370&#x2013; 374. Springer, 2011. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Christian+Colombo%2C+Adrian+Francalanza%2C+and+Rudolph+Gatt%2E+Elarva%3A+A+Monitoring+Tool+for+Erlang%2E+In+RV%2C+volume+7186+of+LNCS%2C+pages+370-+374%2E+Springer%2C+2011%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch03_b16"/>Romain Demangeon, Kohei Honda, Raymond Hu, Rumyana Neykova, and Nobuko Yoshida. Practical interruptible conversations: Distributed dynamic verification with multiparty session types and Python. <emphasis>Formal Methods in System Design</emphasis>, 46(3):197&#x2013;225, 2015. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Romain+Demangeon%2C+Kohei+Honda%2C+Raymond+Hu%2C+Rumyana+Neykova%2C+and+Nobuko+Yoshida%2E+Practical+interruptible+conversations%3A+Distributed+dynamic+verification+with+multiparty+session+types+and+Python%2E+Formal+Methods+in+System+Design%2C+46%283%29%3A197-225%2C+2015%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch03_b17"/>Yli&#x00E8;s Falcone, Jean-Claude Fernandez, and Laurent Mounier. What can you verify and enforce at runtime? <emphasis>STTT</emphasis>, 14(3):349&#x2013;382, 2012. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Yli%E8s+Falcone%2C+Jean-Claude+Fernandez%2C+and+Laurent+Mounier%2E+What+can+you+verify+and+enforce+at+runtime%B4+STTT%2C+14%283%29%3A349-382%2C+2012%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch03_b18"/>Adrian Francalanza. A Theory of Monitors. In <emphasis>FoSSaCS</emphasis>, volume 9634 of <emphasis>LNCS</emphasis>, pages 145&#x2013;161. Springer, 2016. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Adrian+Francalanza%2E+A+Theory+of+Monitors%2E+In+FoSSaCS%2C+volume+9634+of+LNCS%2C+pages+145-161%2E+Springer%2C+2016%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch03_b19"/>Adrian Francalanza, Luca Aceto, and Anna Ing&#x00F3;lfsd&#x00F3;ttir. On Verifying Hennessy-Milner Logic with Recursion at Runtime. In <emphasis>RV</emphasis>, volume 9333 of <emphasis>LNCS</emphasis>, pages 71&#x2013;86. Springer, 2015. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Adrian+Francalanza%2C+Luca+Aceto%2C+and+Anna+Ing%F3lfsd%F3ttir%2E+On+Verifying+Hennessy-Milner+Logic+with+Recursion+at+Runtime%2E+In+RV%2C+volume+9333+of+LNCS%2C+pages+71-86%2E+Springer%2C+2015%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch03_b20"/>Adrian Francalanza, Andrew Gauci, and Gordon J. Pace. Distributed System Contract Monitoring. <emphasis>J. Log. Algebr. Program.</emphasis>, 82(5&#x2013;7):186&#x2013; 215, 2013. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Adrian+Francalanza%2C+Andrew+Gauci%2C+and+Gordon+J%2E+Pace%2E+Distributed+System+Contract+Monitoring%2E+J%2E+Log%2E+Algebr%2E+Program%2E%2C+82%285-7%29%3A186-+215%2C+2013%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch03_b21"/>Adrian Francalanza and Aldrin Seychell. Synthesising Correct Concurrent Runtime Monitors. <emphasis>Formal Methods in System Design</emphasis>, 46(3):226&#x2013; 261, 2015. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Adrian+Francalanza+and+Aldrin+Seychell%2E+Synthesising+Correct+Concurrent+Runtime+Monitors%2E+Formal+Methods+in+System+Design%2C+46%283%29%3A226-+261%2C+2015%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch03_b22"/>Fred Hebert. <emphasis>Learn You Some Erlang for Great Good!: A Beginner&#x2019;s Guide</emphasis>. No Starch Press, first edition, 2013. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Fred+Hebert%2E+Learn+You+Some+Erlang+for+Great+Good%21%3A+A+Beginner%27s+Guide%2E+No+Starch+Press%2C+first+edition%2C+2013%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch03_b23"/>Orna Kupferman. Variations on Safety. In <emphasis>TACAS</emphasis>, volume 8413 of <emphasis>LNCS</emphasis>, pages 1&#x2013;14. Springer, 2014. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Orna+Kupferman%2E+Variations+on+Safety%2E+In+TACAS%2C+volume+8413+of+LNCS%2C+pages+1-14%2E+Springer%2C+2014%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch03_b24"/>Julien Lange and Nobuko Yoshida. Characteristic Formulae for Session Types. In <emphasis>TACAS</emphasis>, volume 9636 of <emphasis>LNCS</emphasis>, pages 833&#x2013;850. Springer, 2016. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Julien+Lange+and+Nobuko+Yoshida%2E+Characteristic+Formulae+for+Session+Types%2E+In+TACAS%2C+volume+9636+of+LNCS%2C+pages+833-850%2E+Springer%2C+2016%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch03_b25"/>Kim Guldstrand Larsen. Proof Systems for Satisfiability in Hennessy-Milner Logic with Recursion. <emphasis>Theor. Comput. Sci.</emphasis>, 72(2&#x0026;3):265&#x2013;288, 1990. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Kim+Guldstrand+Larsen%2E+Proof+Systems+for+Satisfiability+in+Hennessy-Milner+Logic+with+Recursion%2E+Theor%2E+Comput%2E+Sci%2E%2C+72%282%263%29%3A265-288%2C+1990%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch03_b26"/>Martin Leucker and Christian Schallhart. A Brief Account of Runtime Verification. <emphasis>J. Log. Algebr. Program.</emphasis>, 78(5):293&#x2013;303, 2009. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Martin+Leucker+and+Christian+Schallhart%2E+A+Brief+Account+of+Runtime+Verification%2E+J%2E+Log%2E+Algebr%2E+Program%2E%2C+78%285%29%3A293-303%2C+2009%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch03_b27"/>Zohar Manna and Amir Pnueli. Completing the Temporal Picture. <emphasis>Theor. Comput. Sci.</emphasis>, 83(1):91&#x2013;130, 1991. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Zohar+Manna+and+Amir+Pnueli%2E+Completing+the+Temporal+Picture%2E+Theor%2E+Comput%2E+Sci%2E%2C+83%281%29%3A91-130%2C+1991%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch03_b28"/>Rumyana Neykova, Laura Bocchi, and Nobuko Yoshida. Timed Runtime Monitoring for Multiparty Conversations. In <emphasis>BEAT</emphasis>, volume 162 of <emphasis>EPTCS</emphasis>, pages 19&#x2013;26, 2014. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Rumyana+Neykova%2C+Laura+Bocchi%2C+and+Nobuko+Yoshida%2E+Timed+Runtime+Monitoring+for+Multiparty+Conversations%2E+In+BEAT%2C+volume+162+of+EPTCS%2C+pages+19-26%2C+2014%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch03_b29"/>Giles Reger, Helena Cuenca Cruz, and David E. Rydeheard. Marq: Monitoring at Runtime with QEA. In <emphasis>TACAS</emphasis>, volume 9035 of <emphasis>LNCS</emphasis>, pages 596&#x2013;610. Springer, 2015. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Giles+Reger%2C+Helena+Cuenca+Cruz%2C+and+David+E%2E+Rydeheard%2E+Marq%3A+Monitoring+at+Runtime+with+QEA%2E+In+TACAS%2C+volume+9035+of+LNCS%2C+pages+596-610%2E+Springer%2C+2015%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch03_b30"/>A. Roscoe. <emphasis>Theory and Practice of Concurrency</emphasis>. Prentice Hall, first edition, 1997. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=A%2E+Roscoe%2E+Theory+and+Practice+of+Concurrency%2E+Prentice+Hall%2C+first+edition%2C+1997%2E" target="_blank">Google Scholar</ulink></para></listitem>
</orderedlist>
</section>
</chapter>
<chapter class="chapter" id="ch04" label="4" xreflabel="4">
<title>How to Verify Your Python Conversations</title>
<para><emphasis role="strong">Rumyana Neykova and Nobuko Yoshida</emphasis></para>
<para>Imperial College London, UK</para>
<section class="lev2">
<title>Abstract</title>
<para>In large-scale distributed systems, each application is realised through interactions among distributed components. To guarantee safe communication (no deadlocks and communication mismatches) we need programming languages and tools that structure, manage, and policy-check these interactions. Multiparty session types (MSPT), a typing discipline for structured interactions between communicating processes, offers a promising approach. To date, however, session types applications have been limited to static verification, which is not always feasible and is often restrictive in terms of programming API and specifying policies. This chapter investigates the design and implementation of a runtime verification framework, ensuring conformance between programs and specifications. Specifications are written in Scribble, a protocol description language formally founded on MPST. The central idea of the approach is a runtime monitor, which takes a form of a communicating finite state machine, automatically generated from Scribble specifications, and a communication runtime stipulating a message format. We demonstrate Scribble-based runtime verification in manifold ways. First, we present a Python library, facilitated with session primitives and verification runtime. Second, we show examples from a large cyber-infrastructure project for oceanography, which uses the library as a communication medium. Third, we examine communication patterns, featuring advanced Scribble primitives for verification of exception handling behaviours.</para>
</section>
<section class="lev1" id="sec4-1" label="4.1" xreflabel="4.1">
<title>Framework Overview</title>
<para><link linkend="fig4_1">Figure <xref linkend="fig4_1" remap="4.1"/></link> illustrates the methodology of our framework. The development of a communication-oriented application starts with the specification of the intended interactions (the choreography) as a <emphasis>global protocol</emphasis> using the Scribble protocol description language [<link linkend="ch04_b9">9</link>]. From a global protocol, the toolchain mechanically generates (<emphasis>projects</emphasis>) a Scribble <emphasis>local protocol</emphasis>, represented as a finite state machine (FSM), for each participant (abstracted as a <emphasis>role</emphasis>). As a session is conducted at run-time, the monitor at each endpoint validates the communication actions performed by the local endpoint, and the messages that arrive from the other endpoints, against the transitions permitted by the monitor&#x2019;s FSM. Each monitor thus works to protect (1) the endpoint from invalid actions by the network environment, and (2) the network from incorrectly implemented endpoints. Our runtime multiparty session types (MPST) [<link linkend="ch04_b6">6</link>] framework is designed in this way to ensure, using the decentralised monitoring of each local endpoint, that the session as a whole conforms to the original global protocol [<link linkend="ch04_b1">1</link>], and that unsafe actions by a bad endpoint cannot corrupt the protocol state of other compliant endpoints.</para>
<fig id="fig4_1" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 4.1</label>
<caption><title>Scribble methodology from global specification to local runtime verification.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig011.jpg" mime-subtype="jpeg"/>
</fig>
<para><emphasis role="strong">Outline.</emphasis> We outline the structure of this chapter. Section 4.2 demonstrates our runtime MPST framework through an example. We present a global-to-local projection of Scribble protocols, endpoint implementations, and local FSM generation. Section 4.3 demonstrates an API for conversation programming in Python that supports standard socket-like operations, as well as event-driven interface. The API decorates conversation messages with meta information required by the monitors to perform runtime verification. Section 4.4 discusses the monitor implementation, focusing on key architectural requirements of our framework. Section 4.5 presents an extension of Scribble with asynchronous session interrupts. This is a feature of MPST, giving a general mechanism for handling nested, multiparty exception handling. We present the extension of the Python API with a new construct for scopes, endpoint implementations, and local FSM generation for monitoring. Section 4.6 explains the correspondence between a theoretical model and our implementation.</para>
</section>
<section class="lev1" id="sec4-2" label="4.2" xreflabel="4.2">
<title>Scribble-Based Runtime Verification</title>
<para>This section illustrates the stages of our framework and its implementation through a use case, emphasising the properties verified at each verification stage. The presented use case is obtained from our industrial partners Ocean Observatories Institute (OOI) [<link linkend="ch04_b7">7</link>] (use case UC.R2.13 &#8221;Acquire Data From Instrument&#8221;). The OOI is a project to establish a cyberinfrastructure for the delivery, management and analysis of scientific data from a large network of ocean sensor systems. Its architecture relies on the combination of high-level protocol specifications of network services (expressed as Scribble protocols [<link linkend="ch04_b8">8</link>]) and distributed runtime monitoring to regulate the behaviour of third-party applications within the system. We have integrated our framework into the Python-based runtime platform developed by OOI [<link linkend="ch04_b7">7</link>].</para>
<section class="lev2" id="sec4-2-1" label="4.2.1" xreflabel="4.2.1">
<title>Verification Steps</title>
<para><emphasis role="strong">Global Protocol Correctness.</emphasis> The first level of verification is when designing a global protocol. A Scribble global protocol for the use case is listed in <link linkend="fig4_2">Figure <xref linkend="fig4_2" remap="4.2"/></link> (left). Scribble describes interactions between session participants through message passing sequences, branches and recursion. Each message has a label (an operator) and a payload. The Scribble protocol in <link linkend="fig4_2">Figure <xref linkend="fig4_2" remap="4.2"/></link> starts by protocol declaration, which specifies the name of the protocol, <literal>Data</literal> <literal>Acquisition</literal>, and its participating roles &#x2013; a User (<literal>U</literal>), an Agent service (<literal>A</literal>) and an Instrument (<literal>I</literal>). The overall scenario is as follows: <literal>U</literal> requests through <literal>A</literal> to start streaming a list of resources from <literal>I</literal> (line 3&#x2013;4). At line 5 <literal>I</literal> makes a choice whether to continue the interaction or not. If <literal>I</literal> supports the requested resource, <literal>I</literal> sends a message <literal>Supported</literal> and the communication continues by <literal>A</literal> sending a <literal>Poll</literal> request to <literal>I</literal>. The raw resource data is sent from <literal>I</literal> to <literal>A</literal>, at <literal>A</literal> the data is formatted and forwarded to <literal>U</literal> (line 10&#x2013;12). Line 11 demonstrates an <emphasis>assertion</emphasis> construct specifying that <literal>I</literal> is allowed to send data packages that are less than 512MB.</para>
<fig id="fig4_2" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 4.2</label>
<caption><title>Global Protocol (left) and Local Protocol (right).</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig012.jpg" mime-subtype="jpeg"/>
</fig>
<para>The Scribble toolchain validates that a protocol is well-formed and thus projectable for each role. For example, in each case of a choice construct, the deciding party (e.g. at <literal>I</literal>) must correctly communicate the decision outcome unambiguously to all other roles involved; a choice is badly-formed if the actions of the deciding party would cause a race condition on the selected case between the other roles, or if it is ambiguous to another role whether the decision has already been made or is still pending.</para>
<para><emphasis role="strong">Local protocol conformance.</emphasis> The second level of verification is performed at runtime and ensures that each endpoint program conforms to the local protocol structure. Local protocols specify the communication behaviour for each conversation participant. Local protocols are mechanically projected from a global protocol. A local protocol is essentially a view of the global protocol from the perspective of one participant role. Projection works by identifying the message exchanges where the participant is involved, and disregarding the rest, while preserving the overall interaction structure of the global protocol.</para>
<para>From the local protocols, an FSM is generated. At runtime, the endpoint program is validated against the FSM states. There are two main checks that are performed. First, we verify that the type (a label and payloads) of each message matches its specification (labels can be mapped directly to message headers, or to method calls, class names or other relevant artefacts in the program). Second, we verify that the flow of interactions is correct, i.e. interaction sequences, branches and recursions proceed as expected, respecting the explicit dependencies (e.g. <literal>m1() from A to B; m2() from B to C</literal>; imposes a causality at <literal>B</literal>, which is obliged to receive the messages from <literal>A</literal> before sending a message to <literal>C</literal>).</para>
<para><emphasis role="strong">Policy validation.</emphasis> The final level of verification enables the elaboration of Scribble protocols using annotations (<emphasis role="roman">@</emphasis>{} in <link linkend="fig4_2">Figure <xref linkend="fig4_2" remap="4.2"/></link>). Annotations function as API hooks to the verification framework: they are not verified by the MPST monitor itself, but are, instead, delegated to a third-party library. Our current implementation uses a Python library for evaluating basic predicates (e.g. the size check in <link linkend="fig4_2">Figure <xref linkend="fig4_2" remap="4.2"/></link>). At runtime, the monitor passes the annotated information, along with the FSM state information, to the appropriate library to perform the additional checks or calculations. To plug in an external validation engine, our toolchain API requires modules for parsing and evaluating the annotation expressions specified in the protocol.</para>
</section>
<section class="lev2" id="sec4-2-2" label="4.2.2" xreflabel="4.2.2">
<title>Monitoring Requirements</title>
<para><emphasis role="strong">Positioning.</emphasis> In order to guarantee global safety, our monitoring framework imposes complete mediation of communications: communication actions should not have an effect unless the message is mediated by the monitor. The tool implements this principal for outline monitor configurations, i.e. the monitor is running as a separate application. Outline monitoring is realised by dynamically modifying the application-level network configuration to (asynchronously) route every message through a monitor. Our prototype is built over an Advanced Message Queuing Protocol (AMQP) [<link linkend="ch04_b1">1</link>] transport. An AMQP is a publish-subscribe middleware. An AMQP network consists of a federation of distributed virtual routers (called brokers) and queues. A monitor dispatcher is assigned to each network endpoint as a conversation gateway. The dispatcher can create new routes and spawn new monitor processes if needed, to ensure the scalability of this approach.</para>
<para><emphasis role="strong">Message format.</emphasis> To monitor Scribble conversations, our toolchain relies on a small amount of message meta data, referred to as Scribble header, and embedded into the message payload. Messages are processed depending on their kind, as recorded in the first field of the Scribble header. There are two kinds of conversation messages: initialisation (exchanged when a session is started, carrying information such as the protocol name and the role of the monitored process) and in-session (carrying the message operation and the sender/receiver roles). Initialisation messages are used for routing reconfiguration, while in-session messages are the ones checked for protocol conformance.</para>
<para><emphasis role="strong">Principals and Conversation runtime.</emphasis> A principal (an application) implements a protocol behaviour using the Conversation API. The API is built on top of a Conversation Runtime. The runtime provides a library for instantiating, managing and programming Scribble protocols and serialising and deserializing conversation messages. The library is implemented as a thin wrapper over an existing transport library. The API provides primitives for creating and joining a conversation, as well as primitives for sending and receiving messages.</para>
</section>
</section>
<section class="lev1" id="sec4-3" label="4.3" xreflabel="4.3">
<title>Conversation Programming in Python</title>
<para>The Python Conversation API is a message passing API, which offers a high-level interface for safe conversation programming, mapping the interaction primitives of session types to lower-level communication actions on concrete transports. The API primitives are displayed in <link linkend="fig4_3">Figure <xref linkend="fig4_3" remap="4.3"/></link>. In summary, the API provides functionality for (1) session initiation and joining, (2) basic send/receive.</para>
<para><emphasis role="strong">Conversation Initiation.</emphasis> A session is initiated using the <literal>create</literal> method. It creates a fresh conversation id and the required AMQP objects (principal exchange and queue), and sends an invitation for each role specified in the protocol. Invitations are sent to principals.</para>
<fig id="fig4_3" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 4.3</label>
<caption><title>The core Python Conversation API operations.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig013.jpg" mime-subtype="jpeg"/>
</fig>
<para>We use a configuration file to provide the mapping between roles and principals. We give on the right an example of the configuration file (invitation section) for the <literal>DataAcquisition</literal> protocol. Principal names direct the routing of invitation message to the right endpoint. Each invitation carries a role, a principal name and a name for a Scribble local specification file. An invitation is accepted using the <literal>Conversation.join</literal> method. It establishes an AMQP connection and, if one does not exist, creates an invitation queue for receiving invitations.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog072.jpg" mime-subtype="jpeg"/></para>
<para>We demonstrate the usage of the API in a Python implementation of the local protocol projected for the Agent role. The local protocol is given in <link linkend="fig4_2">Figure <xref linkend="fig4_2" remap="4.2"/></link> (right). <link linkend="fig4_4">Figure <xref linkend="fig4_4" remap="4.4"/></link> (left) gives the Agent role implementation. First, the <literal>create</literal> method of the Conversation API initiates a new conversation instance of the <literal>DataAcquisition</literal> protocol, and returns a token that is used to join the conversation locally. The <literal>config.yml</literal> file specifies which network principals will play which roles in this session and the runtime sends invitation messages to each principal. The <literal>join</literal> method confirms that the endpoint is joining the conversation as the principal <literal>alice</literal> playing role <literal>A</literal>. Once the invitations are sent and accepted (via <literal>Conversation.join</literal>), the conversation is established and the intended message exchange can proceed. As a result of the initiation procedure, the runtime at every participant has a mapping (conversation table) between each role and their AMQP addresses. <emphasis role="strong">Conversation Message Passing.</emphasis> The API provides standard send/receive primitives. Send is asynchronous, meaning that a basic send does not block on the corresponding receive; however, the basic receive does block until the message has been received. In addition, an asynchronous receive method, called <literal>recv_async</literal>, is provided to support event-driven usage of the conversation API. These asynchronous features map closely to those supported by Pika<footnote id="fn4_1" label="1"><para>http://pika.readthedocs.org/</para></footnote>, a Python transport library used as an underlying transport library in our implementations.</para>
<fig id="fig4_4" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 4.4</label>
<caption><title>Python program for <literal>A</literal>: synchronous implementation (left) and event-driven implementation (right).</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig014.jpg" mime-subtype="jpeg"/>
</fig>
<para>Each message signature in a Scribble specification contains an operation and payloads (message arguments). The API does not mandate how the operation field should be treated, allowing the flexibility to interpret the operation name in various ways, e.g. as a plain message label, an RMI method name, etc. We treat the operation name as a plain label.</para>
<para>Following its local protocol, the program for <literal>A</literal> receives a request from <literal>U</literal> and forwards the message to <literal>I</literal>. The <literal>recv</literal> returns a tuple, (label, payload) of the message. When the message does not have a payload, only the label is returned (<literal>req_result = c.recv(&#x2019;I&#x2019;)</literal>). The <literal>recv</literal> method can also take the source role as a single argument (<literal>c.recv(&#x2019;I&#x2019;)</literal>), or additionally the label of the desired message (<literal>c.recv(&#x2019;I&#x2019;, &#x2019;Request&#x2019;)</literal>). The <literal>send</literal> method called on the conversation channel <literal>c</literal> takes, in this order, the destination role, message operator and payload values as arguments. In our example, the received payload <literal>resource_request</literal> is forwarded without modifications to <literal>I</literal>. After <literal>A</literal> receives the reply from <literal>I</literal>, the program checks the label value <literal>req_result</literal> using conditional statements, <literal>if (req_result== &#x2019;Supported&#x2019;)</literal>. If <literal>I</literal> replies with <literal>&#x2019;Supported&#x2019;</literal>, <literal>A</literal> enters a loop, where it continuously sends a <literal>&#x2019;Poll&#x2019;</literal> requests to <literal>I</literal> and after receiving the result from <literal>I</literal>, formats the received data (<literal>format(data)</literal>) and resends the formatted result to <literal>U</literal>.</para>
<para><emphasis role="strong">Event-driven conversations.</emphasis> For asynchronous, non-blocking receives, the Conversation API provides <literal>recv_async</literal> to be used in an event-driven style. <link linkend="fig4_4">Figure <xref linkend="fig4_4" remap="4.4"/></link> (right) shows an alternative implementation of the <literal>user</literal> role using callbacks. The method <literal>recv_async</literal> accepts as arguments a callback to be invoked when a message is received.</para>
<para>We first create a conversation variable similar to the synchronous implementation. After joining the conversation, <literal>A</literal> registers a callback to be invoked when a message from <literal>U</literal> is received (<literal>on_request</literal>). The callback executions are linked to the flow of the protocol by taking the conversation id as an argument (e.g. <literal>conv</literal>). It also accepts as arguments the label for the message (<literal>op</literal>) and the payload (<literal>msg</literal>). In the message handler for <literal>Request</literal>, the role <literal>A</literal> forwards the received payload to <literal>I</literal> and registers a new message handler for the next message. Although the event-driven API promotes a notably different programming style, our framework monitors both implementations in <link linkend="fig4_4">Figure <xref linkend="fig4_4" remap="4.4"/></link> transparently without any modifications.</para>
</section>
<section class="lev1" id="sec4-4" label="4.4" xreflabel="4.4">
<title>Monitor Implementation</title>
<para><link linkend="fig4_5">Figure <xref linkend="fig4_5" remap="4.5"/></link> depicts our outline monitor configuration. The interception mechanism is based on message forwarding. A principal has at least one queue for consuming messages, although the number of queues can be tuned to use separate queues for invitations and roles. We outline a concrete scenario. Principal Alice is authenticated and connected to her local broker.</para>
<fig id="fig4_5" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 4.5</label>
<caption><title>Configuration of distributed session monitors for an AMQP-based network.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig015.jpg" mime-subtype="jpeg"/>
</fig>
<orderedlist numeration="arabic" continuation="restarts" spacing="normal">
<listitem><para>Authentication creates a network access point for Alice (the Monitor circle in <link linkend="fig4_5">Figure <xref linkend="fig4_5" remap="4.5"/></link>). The access point consists of a new conversation monitor instance, monitor queues (monitor as a consumer), and an exchange. Alice is only permitted to send messages to this exchange.</para></listitem>
<listitem><para>Alice initiates a new session (creates an exchange with id <emphasis role="roman">1234</emphasis> in <link linkend="fig4_5">Figure <xref linkend="fig4_5" remap="4.5"/></link>) and dispatches an invitation to principal Bob. The invitation is received and checked by Alice&#x2019;s monitor and then dispatched on the shared channel, from where it is rerouted to Bob&#x2019;s Monitor.</para></listitem>
<listitem><para>Bob&#x2019;s monitor checks the invitation, generates the local FSM and session context for Bob and Bob&#x2019;s role (for example client), and allocates a session channel (with exchange: <emphasis role="roman">1234</emphasis> and routing keys matching Bob&#x2019;s role (<emphasis role="roman">1234</emphasis><emphasis>.client.</emphasis><emphasis>*</emphasis> and <emphasis role="roman">1234</emphasis><emphasis>.</emphasis> <emphasis>*</emphasis> <emphasis>.client</emphasis>). The invitation is delivered to Bob&#x2019;s queue.</para></listitem>
<listitem><para>Any message sent by Alice (e.g. to Bob) in this session is similarly passed by the monitor and validated. If valid, the message is forwarded to the session channel to be routed. The receiver&#x2019;s monitor will similarly but independently validate the message.</para></listitem>
</orderedlist>
<para><link linkend="fig4_6">Figure <xref linkend="fig4_6" remap="4.6"/></link> depicts the main components and internal workflow of our prototype monitor. The lower part relates to conversation initiation. The <emphasis>invitation</emphasis> message carries (a reference to) the local protocol for the invitee and the conversation id. We use a parser generator (ANTLR<footnote id="fn4_2" label="2"><para>http://www.antlr.org/</para></footnote>) to produce, from a Scribble local protocol, an abstract syntax tree with MPST constructs as nodes. The tree is traversed to generate a finite state machine, represented in Python as a hash table, where each entry has the shape:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/equ086_1.jpg" mime-subtype="jpeg"/></para>
<para>where <emphasis>transition</emphasis> is a quadruple <emphasis role="roman">(</emphasis><emphasis>interaction type</emphasis><emphasis>,</emphasis> <emphasis>label</emphasis><emphasis>,</emphasis> <emphasis>sender</emphasis><emphasis>,</emphasis> <emphasis>receiver</emphasis><emphasis role="roman">)</emphasis>, <emphasis>interaction type</emphasis> is either <emphasis>send</emphasis> or <emphasis>receive</emphasis> and <emphasis>var</emphasis> is a variable binder for a message payload. We number the states using a generator of natural numbers. The FSM generation is based on the translation of local Scribble protocols to FSMs, presented in [<link linkend="ch04_b5">5</link>].</para>
<para>The upper part of <link linkend="fig4_6">Figure <xref linkend="fig4_6" remap="4.6"/></link> relates to <emphasis>in-conversation</emphasis> messages, which carry the conversation id (matching an entry in the FSM hash table), sender and receiver fields, and the message label and payload. This information allows the monitor to retrieve the corresponding FSM (by matching the message signature to the FSM&#x2019;s transition function). Assertions associated to communication actions are evaluated by invoking a library for Python predicate evaluation.</para>
<fig id="fig4_6" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 4.6</label>
<caption><title>Monitor workflow for (1) invitation and (2) in-conversation messages.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig016.jpg" mime-subtype="jpeg"/>
</fig>
</section>
<section class="lev1" id="sec4-5" label="4.5" xreflabel="4.5">
<title>Monitoring Interruptible Systems</title>
<para>This section presents the implementation of a new construct for verifying <emphasis>asynchronous multiparty session interrupts</emphasis>. Asynchronous session interrupts express communication patterns in which the behaviour of the roles following the default flow through a protocol segment may be overruled by one or more other roles concurrently raising asynchronous interrupt messages. Extending MPST with asynchronous interrupts is challenging because the inherent communication race conditions that may arise conflict with the MPST safety properties. Taking a continuous stream of messages from a producer to a consumer as a simple example: if the consumer sends an interrupt message to the producer to pause or end the stream, stream messages (those already in transit or subsequently dispatched before the interrupt arrives at the producer) may well continue arriving at the consumer for some time after the interrupt is dispatched. This scenario is in contrast to the patterns permitted by standard session types, where the safety properties guarantee that no message is ever lost or redundant by virtue of disallowing all protocols with potential races.</para>
<para>This section introduces a novel approach based on reifying the concept of <emphasis>scopes</emphasis> within a protocol at the runtime level when an instance of the protocol is executed. A scope designates a sub-region of the protocol, derived from its syntactic structure, on which certain communication actions, such as interrupts, may act on the region as a whole. At run-time, every message identifies the scope to which it belongs as part of its meta data. From this information and by tracking the local progress in the protocol, the runtime at each endpoint in the session is able to resolve discrepancies in a protocol state by discarding incoming messages that have become irrelevant due to an asynchronous interrupt. This mechanism is transparent to the user process, and although performed independently by each distributed endpoint, preserves global safety for the session.</para>
<para>We integrate the new interrupt construct in our framework for runtime monitoring. The FSM generation is extended to support interruptible protocol scopes. We treat interruptible scopes by generating nested FSM structures. In the case of scopes that may be entered multiple times by recursive protocols, we use dynamic FSM nesting (conceptually, a new sub-FSM is created each time the scope is entered, and the sub-FSM is terminated once it reaches its end state or when an interrupt message is received) corresponding to the generation of fresh scope names in the syntactic model.</para>
<section class="lev2" id="sec4-5-1" label="4.5.1" xreflabel="4.5.1">
<title>Use Case: Resource Access Control (RAC)</title>
<para>This section expands on how we extend Scribble to support the specification and verification of asynchronous session interrupts, henceforth referred to as just interrupts. Our running example is based on an OOI project use case, which we have distilled to focus on session interrupts.</para>
<para><link linkend="fig4_7">Figure <xref linkend="fig4_7" remap="4.7"/></link> (left) gives an abridged version of a sequence diagram given in the OOI documentation for the Resource Access Control (RAC) use case [<link linkend="ch04_b8">8</link>], regarding access control of users to sensor devices in the OOI cyberinfrastucture for data acquisition. In the OOI setting, a User interacts with a sensor device via its Agent proxy (which interacts with the device using a separate protocol outside of this example). OOI Controller agents manage concerns such as authentication of users and metering of service usage.</para>
<para>For brevity, we omit from the diagram some of the data types to be carried in the messages and focus on the <emphasis>structure</emphasis> of the protocol. The depicted interaction can be summarised as follows. The protocol starts at the top of the left-hand diagram. User sends Controller a <literal>request</literal> message to use a sensor for a certain amount of time (the <literal>int</literal> in parentheses), and Controller sends a <literal>start</literal> to Agent. The protocol then enters a phase (denoted by the horizontal line) that we label (1), in which Agent streams <literal>data</literal> messages (acquired from the sensor) to User. The vertical dots signify that Agent produces the stream of data freely under its own control, i.e. without application-level control from User. User and Controller, however, have the option at any point in phase (1) to move the protocol to the phase labelled (2), below.</para>
<fig id="fig4_7" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 4.7</label>
<caption><title>Sequence diagram (left) and Scribble protocol (right) for the RAC use case.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig017.jpg" mime-subtype="jpeg"/>
</fig>
<para>Phase (2) comprises three alternatives, separated by dashed lines. In the upper case, User <emphasis>interrupts</emphasis> the stream from Agent by sending Agent a <literal>pause</literal> message. At some subsequent point, User sends a <literal>resume</literal> and the protocol returns to phase (1). In the middle case, User interrupts the stream, sending both Agent and Controller a <literal>stop</literal> message. This is the case where User does not want any more sensor data, and ends the protocol for all three participants. Finally, in the lower case, Controller interrupts the stream by sending a <literal>timeout</literal> message to User and Agent. This is the case where, from Controller&#x2019;s view, the session has exceeded the requested duration, so Controller interrupts the other two participants to end the protocol. Note this diagram actually intends that <literal>stop</literal> (and <literal>timeout</literal>) can arise anytime after (1), e.g. between <literal>pause</literal> and <literal>resume</literal> (a notational ambiguity that is compensated by additional prose comments in the specification).</para>
</section>
<section class="lev2" id="sec4-5-2" label="4.5.2" xreflabel="4.5.2">
<title>Interruptible Multiparty Session Types</title>
<para><link linkend="fig4_7">Figure <xref linkend="fig4_7" remap="4.7"/></link> (right) shows a Scribble protocol that formally captures the structure of interaction in the Resource Access Control (RAC) use case and demonstrates the uses of our extension for asynchronous interrupts. Besides the formal foundations, we find the Scribble specification more explicit and precise, particularly regarding the combination of compound constructs such as choice and recursion, than the sequence diagram format, and provides firmer implementation guidelines for the programmer.</para>
<para>The protocol starts with a header declaring the protocol name (given as <literal>RACProtocol</literal> in <link linkend="fig4_7">Figure <xref linkend="fig4_7" remap="4.7"/></link>) and role names for the participants (three roles, aliased in the scope of this protocol definition as <literal>U</literal>, <literal>C</literal> and <literal>A</literal>). Lines 5 and 6 straightforwardly correspond to the first two communications in the sequence diagram, a User sends a request message, carrying an <literal>int</literal> <emphasis>payload</emphasis>, to the Controller and then the Controller replies with a <literal>start()</literal> message and an empty payload.</para>
<para>Then the intended communication in &#8220;phase&#8221; (1) and (2) in the diagram, is clarified in Scribble as two nested <literal>interruptible</literal> statements. The outer statement, on lines 7&#x2013;22, corresponds to the options for User and Controller to end the protocol by sending the <literal>stop</literal> and <literal>timeout</literal> interrupts. An <literal>interruptible</literal>consists of a main body of protocol actions, here lines 8&#x2013; 17, and a set of interrupt message signatures, lines 18&#x2013;22. The statement stipulates that each participant behaves by either (a) following the protocol specified in the body until finished for their role, or (b) raising or detecting a specified interrupt at any point during (a) and exiting the statement. Thus, the outer <literal>interruptible</literal> states that <literal>U</literal> can interrupt the body (and end the protocol) by a <literal>stop()</literal> message, and <literal>C</literal> by a <literal>timeout()</literal>.</para>
<para>The body of the outer <literal>interruptible</literal> is a labelled recursion statement with label <literal>X</literal>. The <literal>continue X;</literal> inside the recursion (line 16) causes the flow of the protocol to return to the top of the recursion (line 8). This recursion corresponds to the loop implied by the sequence diagram that allows User to pause and resume repeatedly. Since the recursion body always leads to the <literal>continue</literal>, Scribble protocols of this form state that the loop should be driven indefinitely by one role, until one of the interrupts is raised by <emphasis>another</emphasis> role. This communication pattern cannot be expressed in multiparty session types without <literal>interruptible</literal>.</para>
<para>The body of the <literal>X</literal>-recursion is the inner <literal>interruptible</literal>, which corresponds to the option for User to pause the stream. The stream itself is specified by the <literal>Y</literal>-recursion, in which <literal>A</literal> continuously sends <literal>data()</literal> messages to <literal>U</literal>. The inner <literal>interruptible</literal> specifies that <literal>U</literal> may interrupt the <literal>Y</literal>-recursion by a <literal>pause()</literal> message, which is followed by the <literal>resume()</literal> message from <literal>U</literal> before the protocol returns to the top of the <literal>X</literal>-recursion.</para>
</section>
<section class="lev2" id="sec4-5-3" label="4.5.3" xreflabel="4.5.3">
<title>Programming and Verification of Interruptible Systems</title>
<para>We extend the Python API, presented in Section 4.3, to provide functionality for <emphasis>scope</emphasis> management for handling interrupt messages. We demonstrate the usage of the construct through an implementation of the local protocol projected for the <literal>User</literal> role. <link linkend="fig4_8">Figure <xref linkend="fig4_8" remap="4.8"/></link> gives the local protocol and its implementation.</para>
<para>Similarly to the previous example from Section 4.3, the implementation starts by creating a conversation instance <literal>c</literal> of the Resource Access Control protocol (<link linkend="fig4_7">Figure <xref linkend="fig4_7" remap="4.7"/></link>) using method <literal>create</literal> (line 6, left) and <literal>join</literal>. The latter returns a conversation channel object for performing the subsequent communication operations.</para>
<para><emphasis role="strong">Interrupt handling.</emphasis> The implementation of the User program demonstrates a way of handling conversation interrupts by combining conversation scopes with the Python <literal>with</literal> statement (an enhanced try-finally construct). We use <literal>with</literal> to conveniently capture interruptible conversation flows and the nesting of interruptible scopes, as well as automatic <literal>close</literal> of interrupted channels in the standard manner, as follows. The API provides the <literal>c.scope()</literal> method, as in line 10, to create and enter the scope of an <literal>interruptible</literal> Scribble block (here, the outer interruptible of the RAC protocol). The <literal>timeout</literal> and <literal>stop</literal> arguments associate these message signatures as interrupts with this scope. The conversation channel <literal>c_x</literal> returned by <literal>scope</literal> is a wrapper of the parent channel <literal>c</literal> that (1) records the current scope of every message sent in its meta data, (2) ensures every send and receive operation is guarded by a check on the local interrupt queue, and (3) tracks the nesting of scope contexts through nested <literal>with</literal> statements. The interruptible scope of <literal>c x</literal> is given by the enclosing <literal>with</literal> (lines 10&#x2013;20); if, e.g., a <literal>timeout</literal> is received within this scope, the control flow will exit the <literal>with</literal> block to line 21. The inner <literal>with</literal> (lines 12&#x2013;16), corresponding to the inner interruptible block, is associated with the <literal>pause</literal> interrupt. When an interrupt, e.g. <literal>pause</literal> in line 16, is thrown (<literal>send_interrupt</literal>) to the other conversation participants, the local and receiver runtimes each raise an internal exception that is either handled or propagated up, depending on the interrupts declared at the current scope level, to direct the interrupted control flow accordingly. The delineation of interruptible scopes by the global protocol, and its projection to each local protocol, thus allows interrupted control flows to be coordinated between distributed participants in a structured manner.</para>
<fig id="fig4_8" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 4.8</label>
<caption><title>Python implementation (left) and Scribble local protocol (right) for the User role for the global protocol from <link linkend="fig4_7">Figure <xref linkend="fig4_7" remap="4.7"/></link>.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig018.jpg" mime-subtype="jpeg"/>
</fig>
<para>The scope wrapper channels are closed (using the Python construct <literal>with</literal>) after throwing or handling an interrupt message. Since we assume asynchronous communication, there is a delay from the time when an interrupt mesasage is sent untill the time when the interrupt message is received by all participants. Hence, the monitor reacts differently when checking message sending (a check driven by the monitored participant) and message receive (an action driven by a message arriving in the queue of the monitor); the monitor discards the message in the latter case and marks the message as wrong in the former case. More precisely, when a monitor receives a message from a closed scope, it discards it as to accommodate for the delay in receiving of an interrupt message. However, if a participant that is monitoried attepts to send a message on a scope that is already closed (after an interrupt message has been recieved or after the participant has thrown interrupt himself) then the monitor flagges the interaction as an error. For example, using <literal>c_x</literal> after a <literal>timeout</literal> is received (i.e. outside its parent scope) will be flagged as an error. However, receiving messages on that scope will be discarded and will not be dispatched to the application. In our example, the User runtime discards <literal>data</literal> messages that arrive after <literal>pause</literal> is thrown. The API can also make the discarded data available to the programmer through secondary (non-monitored) operations.</para>
<fig id="fig4_9" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 4.9</label>
<caption><title>Event-driven conversation implementation for the User role (left) and Nested FSM generated from the User local protocol (right).</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig019.jpg" mime-subtype="jpeg"/>
</fig>
<para><emphasis role="strong">Message handlers with scopes.</emphasis> As demonstrated in Section 4.3, our Python API supports asynchronous receive through the primitive <literal>recv_async</literal>. The construct is used to register a method that should be invoked on message receive. To support event-driven programming with interrupts, we extend the implementation presented in Section 4.3. The difference is in the semantics of event handlers. More precisely, each event handler is associated with a scope. Therefore, if an interrupt is received, but the protocol state is not in the same scope as the scope written in the conversation header of the interrupt message, the interrupt will be discarded.</para>
<para><link linkend="fig4_9">Figure <xref linkend="fig4_9" remap="4.9"/></link> (left) shows an alternative implementation of the <literal>User</literal> role using callbacks. We first enter the nested conversation scopes according to the potential interrupt messages (lines 9 and 10). The callback method is then registered using the <literal>recv_async</literal> operation (line 11). The callback executions are linked to the flow of the protocol by taking the scoped channel as an argument (e.g. <literal>c</literal> on line 13). Note that if the <literal>stop</literal> and <literal>pause</literal> interrupts were not declared for these scopes, line 16 and line 25 would be considered invalid by the monitor. When the buffer is full (line 17), the user sends the <literal>pause</literal> interrupt. After raising an interrupt, the current scope becomes obsolete and the channel object for the parent scope is returned. After the data is processed and the buffer is cleared, the <literal>resume</literal> message is sent (line 28) and a fresh scope is created and again registered for receiving data events (line 29). Our framework monitors both this implementation and that in <link linkend="fig4_8">Figure <xref linkend="fig4_8" remap="4.8"/></link> transparently without any modifications.</para>
</section>
<section class="lev2" id="sec4-5-4" label="4.5.4" xreflabel="4.5.4">
<title>Monitoring Interrupts</title>
<para>FSM generation for interruptible local protocols makes use of nested FSMs. Each <literal>interruptible</literal> induces a nested FSM given by the main interruptible block, as illustrated in <link linkend="fig4_9">Figure <xref linkend="fig4_9" remap="4.9"/></link> (right) for the User local protocol. The monitor internally augments the nested FSM with a scope id, derived from the signature of the interruptible block, and an interrupt table, which records the interrupt message signatures that may be thrown or received in this scope. Interrupt messages are marked via the same meta data field used to designate invitation and in-conversation messages, and are validated in a similar way except that they are checked against the interrupt table. However, if an interrupt arrives that does not have a match in the interrupt table of the immediate FSM(s), the check searches upwards through the parent FSMs; the interrupt is invalid if it cannot be matched after reaching the outermost FSM.</para>
</section>
</section>
<section class="lev1" id="sec4-6" label="4.6" xreflabel="4.6">
<title>Formal Foundations of MPST-Based Runtime Verification</title>
<para>In this section, we explain the correspondence between a theoretical model for MPST-based monitoring and the implementation, presented in this chapter. Our implementation is formalised in a theory for MPST-based verification of networks, first proposed in [<link linkend="ch04_b3">3</link>], and later extended in [<link linkend="ch04_b1">1</link>], and in [<link linkend="ch04_b2">2</link>]. The interrupt extension is formalised in [<link linkend="ch04_b4">4</link>]. [<link linkend="ch04_b3">3</link>] only gives an overview of the desired properties, and requires all local processes to be dynamically verified through the protections of system monitors, while [<link linkend="ch04_b1">1</link>] presents a framework for semantically precise decentralised run-time verification, supporting statically and dynamically verified components. In addition, the routing mechanism of AMPQ networks is explicitly presented in [<link linkend="ch04_b1">1</link>], while in [<link linkend="ch04_b3">3</link>] it is implicit.</para>
<para>A delicate technical difference between the theory and the implementation lies in handling of out-of-order delivery of messages when messages are sent from different senders to the same receiver. Asynchrony poses a challenge in the treatment of out-of-order asynchronous message monitoring, and thus, to prevent false positive results, in the theoretical model, a type-level permutations of actions is required, e.g a monitor checks messages up to permutations. The use of global queues and local permutations is inefficient in practice, and thus we have implemented the theoretical model of a global queue as different physical queues. Specifically, we introduce a queue per pair of roles, which ensures messages from the same receivers are delivered in order and are not mixed with messages from other roles. This model is semantically equivalent to a model of a global indexed queue, permitting permutation of messages.</para>
<para>Next we explain the correspondence between the asynchronous <emphasis>&#x03C0;-</emphasis>calculus with fine-grained primitives for session initiation and our Python API. Also in [<link linkend="ch04_b1">1</link>] specifications are given as local types. Instead of using local types, for efficient checking, we use communicating finite state machines (CFSMs) generated from local Scribble protocols, which are equivalent to local types, as has been shown in [<link linkend="ch04_b5">5</link>].</para>
<para><emphasis role="strong">Processes.</emphasis> Our Python API embodies the primitives of the asynchronous <emphasis>&#x03C0;</emphasis>-calculus with fine grained primitives for session initiation, presented in [<link linkend="ch04_b1">1</link>]. The correspondence is given in <link linkend="fig4_10">Figure <xref linkend="fig4_10" remap="4.10"/></link>. Note that the API does not stipulate the use of a recursion and a conditional, which appear in the syntax of session <emphasis>&#x03C0;</emphasis>-calculus, since these constructs are handled by native Python constructs. The <literal>create</literal> method, which, we remind, creates a fresh conversation id and the required AMQP objects (principal exchanges and queues), and sends an invitation for each role specified in the protocol, corresponds to the action <emphasis>&#x0101;</emphasis>&#x3008;<emphasis>s</emphasis><emphasis role="roman">[</emphasis><emphasis>r</emphasis><emphasis role="roman">] :</emphasis> <emphasis>T</emphasis>&#x3009;, which sends on the shared channel <emphasis>a</emphasis>, an invitation to join the fresh conversation <emphasis>s</emphasis> as the role of <emphasis>r</emphasis> with a specification <emphasis>T</emphasis> . In the implementation, this information is codified in the message header, which as we have explained contains the new session id (abstracted as <emphasis>s</emphasis>), the name of the local Scribble protocol (i.e. <emphasis>T</emphasis> ) and the role (i.e. <emphasis>r</emphasis>). The invitation action <emphasis>a</emphasis><emphasis role="roman">(</emphasis><emphasis>y</emphasis><emphasis role="roman">[</emphasis><emphasis>r</emphasis><emphasis role="roman">] :</emphasis> <emphasis>T</emphasis> <emphasis role="roman">)</emphasis><emphasis>.P</emphasis> models session <literal>join</literal>. As a result of <literal>join</literal> new queues and a routing bindings are created. For example, when Bob joins a conversation with id of 1234 as the role of <literal>client</literal>, as shown in <link linkend="fig4_5">Figure <xref linkend="fig4_5" remap="4.5"/></link>, an AMQP binding <literal>1234.client.</literal><emphasis>*</emphasis> is created, which ensures that all messages to the role of a <literal>client</literal> are delivered to Bob. The reduction rule for <emphasis>a</emphasis><emphasis role="roman">(</emphasis><emphasis>y</emphasis><emphasis role="roman">[</emphasis><emphasis>r</emphasis><emphasis role="roman">] :</emphasis> <emphasis>T</emphasis> <emphasis role="roman">)</emphasis><emphasis>.P</emphasis> , in the semantics in [<link linkend="ch04_b1">1</link>], reflects this behaviour by adding a record in the routing table. The primitive for sending a message <emphasis>k</emphasis><emphasis role="roman">[</emphasis><emphasis>r</emphasis><subscript>1</subscript><emphasis>, r</emphasis><subscript>2</subscript><emphasis role="roman">]!</emphasis><emphasis>l</emphasis>&#x3008;<emphasis>e</emphasis>&#x3009; corresponds to the API call <literal>send</literal>, and results in sending a message of type <emphasis>s</emphasis><emphasis role="roman">[</emphasis><emphasis>r</emphasis><subscript>1</subscript><emphasis>, t</emphasis><subscript>2</subscript><emphasis role="roman">]!</emphasis><emphasis>l</emphasis>&#x3008;<emphasis>e</emphasis>&#x3009;, which in the implementation is codified in the message header, consisting of session id <emphasis>s</emphasis>, sender <emphasis>r</emphasis><subscript>1</subscript>, receiver <emphasis>r</emphasis><subscript>2</subscript>, label <emphasis>l</emphasis> and a payload <emphasis>e</emphasis>.</para>
<fig id="fig4_10" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 4.10</label>
<caption><title>The core Python Conversation API operations and their session <emphasis>&#x03C0;</emphasis>-calulus counterparts.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig020.jpg" mime-subtype="jpeg"/>
</fig>
<para><emphasis role="strong">Properties of monitored networks.</emphasis> Finally, we give an overview of the properties of monitored networks as presented in [<link linkend="ch04_b1">1</link>]. Due to the correspondence explained above, these properties are preserved in the context of the monitor implementation, presented in this chapter.</para>
<itemizedlist mark="none" spacing="normal">
<listitem><para><emphasis role="strong">Local safety</emphasis> states that a monitored process respects its local protocol, i.e. that dynamic verification by monitoring is sound.</para></listitem>
<listitem><para><emphasis role="strong">Local transparency</emphasis> states that a monitored process has equivalent behaviour to an unmonitored but well-behaved process, e.g. statically verified against the same local protocol.</para></listitem>
<listitem><para><emphasis role="strong">Global safety</emphasis> states that a system satisfies the global protocol, provided that each participant behaves as if monitored.</para></listitem>
<listitem><para><emphasis role="strong">Global transparency</emphasis> states that a fully monitored network has equivalent behaviour to an unmonitored but well-behaved network, i.e. in which all local processes are well-behaved against the same local protocols.</para></listitem>
<listitem><para><emphasis role="strong">Session fidelity</emphasis> states that, as all message flows of a network satisfy global specifications, whenever the network changes because some local processes take actions, all message flows continue to satisfy global specifications.</para></listitem>
</itemizedlist>
</section>
<section class="lev1" id="sec4-7" label="4.7" xreflabel="4.7">
<title>Concluding Remarks</title>
<para>We have presented a runtime verification framework for Python programs based on Scribble protocols. We discuss the core design elements of the implemention of a conversation-based API in a dynamically typed language, Python. Through a runtime layer of protocol management Scribble protocols are loaded and translated to CFSMs such that during a program execution, messages emitted by the program are checked against a corresponding CFSM. We also introduce a construct for expressing exception-like patterns in Scribble, which syntactically splits the protocol into sub-regions, allowing certain messaging to act on the regions as a whole and thus permitting controllable races, traditionally disallowed by the theory of session types.</para>
<para><emphasis role="strong">Acknowledgements</emphasis> We thank the anonymous reviewers for their insightful comments, which helped us to improve the article. This work is partially supported by EPSRC projects EP/K034413/1, EP/K011715/1, EP/L00058X/1, EP/N027833/1 and EP/N028201/1; by EU FP7 612985 (UP-SCALE).</para>
</section>
<section class="lev1" id="sec2a">
<title>References</title>
<orderedlist numeration="arabic" continuation="restarts" spacing="normal">
<listitem><para><anchor id="ch04_b1"/>Laura Bocchi, Tzu-Chun Chen, Romain Demangeon, Kohei Honda, and Nobuko Yoshida. Monitoring networks through multiparty session types. In <emphasis>FMOODS</emphasis>, volume 7892 of <emphasis>LNCS</emphasis>, pages 50&#x2013;65, 2013. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Laura+Bocchi%2C+Tzu-Chun+Chen%2C+Romain+Demangeon%2C+Kohei+Honda%2C+and+Nobuko+Yoshida%2E+Monitoring+networks+through+multiparty+session+types%2E+In+FMOODS%2C+volume+7892+of+LNCS%2C+pages+50-65%2C+2013%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch04_b2"/>Tzu-Chun Chen. <emphasis>Theories for Session-based Governance for Large-scale Distributed Systems</emphasis>. PhD thesis, Queen Mary, University of London, 2013. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Tzu-Chun+Chen%2E+Theories+for+Session-based+Governance+for+Large-scale+Distributed+Systems%2E+PhD+thesis%2C+Queen+Mary%2C+University+of+London%2C+2013%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch04_b3"/>Tzu-Chun Chen, Laura Bocchi, Pierre-Malo Deni&#x00E9;lou, Kohei Honda, and Nobuko Yoshida. Asynchronous distributed monitoring for multiparty session enforcement. In <emphasis>TGC&#x2019;11</emphasis>, volume 7173 of <emphasis>LNCS</emphasis>, pages 25&#x2013;45, 2012.</para></listitem>
<listitem><para><anchor id="ch04_b4"/>Romain Demangeon, Kohei Honda, Raymond Hu, Rumyana Neykova, and Nobuko Yoshida. Practical interruptible conversations: Distributed dynamic verication with multiparty session types and python. <emphasis>FMSD</emphasis>, pages 1&#x2013;29, 2015. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Romain+Demangeon%2C+Kohei+Honda%2C+Raymond+Hu%2C+Rumyana+Neykova%2C+and+Nobuko+Yoshida%2E+Practical+interruptible+conversations%3A+Distributed+dynamic+verication+with+multiparty+session+types+and+python%2E+FMSD%2C+pages+1-29%2C+2015%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch04_b5"/>Pierre-Malo Deni&#x00E9;lou and Nobuko Yoshida. Multiparty session types meet communicating automata. In <emphasis>ESOP</emphasis>, volume 7211 of <emphasis>LNCS</emphasis>, pages 194&#x2013;213. Springer, 2012. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Pierre-Malo+Deni%E9lou+and+Nobuko+Yoshida%2E+Multiparty+session+types+meet+communicating+automata%2E+In+ESOP%2C+volume+7211+of+LNCS%2C+pages+194-213%2E+Springer%2C+2012%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch04_b6"/>Kohei Honda, Nobuko Yoshida, and Marco Carbone. Multiparty Asynchronous Session Types. <emphasis>Journal of the ACM</emphasis>, 63, 2016. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Kohei+Honda%2C+Nobuko+Yoshida%2C+and+Marco+Carbone%2E+Multiparty+Asynchronous+Session+Types%2E+Journal+of+the+ACM%2C+63%2C+2016%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch04_b7"/>Ocean Observatories Initiative. <literal>http://www.oceanobservatories.org/</literal></para></listitem>
<listitem><para><anchor id="ch04_b8"/>OOIExamples. <literal>http://confluence.oceanobservatories.org/display/CIDev/Identify+required+Scribble+extensions+ for+advanced+scenarios+of+R3+COI</literal></para></listitem>
<listitem><para><anchor id="ch04_b9"/>Scribble project home page. <literal>http://www.scribble.org</literal></para></listitem>
</orderedlist>
</section>
</chapter>
<chapter class="chapter" id="ch05" label="5" xreflabel="5">
<title>The DCR Workbench: Declarative Choreographies for Collaborative Processes</title>
<para><emphasis role="strong">S&#x00F8;ren Debois and Thomas T. Hildebrandt</emphasis></para>
<para>Department of Computer Science, IT University of Copenhagen, Rued Langgaards Vej 7, 2300 Copenhagen S, Denmark</para>
<section class="lev2">
<title>Abstract</title>
<para>The <emphasis>DCR Workbench</emphasis> is an online tool for simulation and analysis of collaborative distributed processes specified as <emphasis>DCR graphs</emphasis>. The Workbench is a robust and comprehensive implementation of DCR graphs, providing concrete syntax, specification by refinement, visualisation, simulation, static analysis, time analysis, enforcement, declarative subprocesses, data dependencies, translation to other declarative models, and more. This chapter introduces the Workbench and, through the features of the Workbench, surveys the DCR formalism. The Workbench is available on-line at <literal>http: //dcr.tools</literal>.</para>
</section>
<section class="lev1" id="sec5-1" label="5.1" xreflabel="5.1">
<title>Introduction</title>
<para>Citizens, businesses and public organisations increasingly rely on distributed business processes. Many such processes involve at the same time information systems, humans and mechanical artefacts, and are thus highly unpredictable. Moreover, such processes are constantly evolving due to advances in technology, improvement in business practices, and changes in legislation.</para>
<para>In this climate of distribution and continuous change, the traditional vision of verifying a system once and for all against a final formal description has little hope of realisation. Instead, we need tools and techniques for describing, building, and analysing systems of continuously changing distributed collaborative processes. Dynamic Condition Response graphs, DCR graphs, is a formal model developed in response to this need.</para>
<para>Developed through a series of research projects, DCR graphs today stands on three pillars: a substantial body of academic publications on both case studies [<link linkend="ch05_b8">8</link>, <link linkend="ch05_b10">10</link>, <link linkend="ch05_b15">15</link>, <link linkend="ch05_b17">17</link>, <link linkend="ch05_b20">20</link>, <link linkend="ch05_b38">38</link>] and formal aspects [<link linkend="ch05_b2">2</link>, <link linkend="ch05_b6">6</link>, <link linkend="ch05_b7">7</link>, <link linkend="ch05_b12">12</link>&#x2013;<link linkend="ch05_b14">14</link>, <link linkend="ch05_b16">16</link>, <link linkend="ch05_b18">18</link>, <link linkend="ch05_b21">21</link>&#x2013;<link linkend="ch05_b25">25</link>, <link linkend="ch05_b29">29</link>&#x2013;<link linkend="ch05_b31">31</link>, <link linkend="ch05_b34">34</link>, <link linkend="ch05_b37">37</link>]; the DCR Workbench, implementing most major advances of the formalism (the subject of this chapter); and a commercial adaptive case-management system developed by independent vendor Exformatics A/S [<link linkend="ch05_b9">9</link>, <link linkend="ch05_b11">11</link>, <link linkend="ch05_b15">15</link>, <link linkend="ch05_b19">19</link>, <link linkend="ch05_b23">23</link>, <link linkend="ch05_b28">28</link>].</para>
<para>Declarative process notations such as DCR graphs, DECLARE [<link linkend="ch05_b33">33</link>, <link linkend="ch05_b40">40</link>] and GSM [<link linkend="ch05_b26">26</link>] generally support <emphasis>specification and analysis</emphasis> of requirements, whereas imperative notations such as Workflow Nets [<link linkend="ch05_b1">1</link>] and BPMN [<link linkend="ch05_b32">32</link>] generally support <emphasis>implementation</emphasis> of requirements. DCR graphs have the advantage of serving as <emphasis>both</emphasis> the specification of requirements <emphasis>and</emphasis> the runtime representation of a process instance, which can be adapted dynamically if the requirements change.</para>
<para>The DCR Workbench is a comprehensive tool for modelling with DCR graphs and analysing DCR models. The Workbench serves the dual purposes of being a communication and teaching tool, used both in classroom settings and in discussions with industry, as well as a test-bed for experimentation with new analysis and variants.</para>
<para>This chapter gives an introduction to DCR graphs in general and the Workbench in particular. As we shall see, the Workbench implements a majority of published DCR graph variants and analysis methods, as well as some work-in-progress experimental additions and algorithms that have yet to be published. Through the features of the Workbench, the chapter also provides a survey of the state-of-the-art of DCR graphs variants, their technical properties, and their published analysis methods and algorithms.</para>
<section class="lev2" id="sec5-1-1" label="5.1.1" xreflabel="5.1.1">
<title>History of the DCR Workbench</title>
<para>DCR graphs were introduced in 2010 [<link linkend="ch05_b18">18</link>,<link linkend="ch05_b29">29</link>] by Thomas Hildebrandt and his group at the ITU. Soon after, Danish vendor of adaptive case-management solutions, Exformatics A/S, entered into a long-term collaboration with the ITU group; a collaboration which continues to this day. The continued financial support and interest of Exformatics A/S has been instrumental in the development of the formalism.</para>
<para>DCR graphs were implemented repeatedly as the formalism evolved. Notably, an early implementation created by industrial PhD Tijs Slaats in collaboration with Exformatics A/S [<link linkend="ch05_b37">37</link>] eventually grew into that company&#x2019;s current commercial DCR tool [<link linkend="ch05_b9">9</link>, <link linkend="ch05_b15">15</link>, <link linkend="ch05_b28">28</link>], available at <literal>dcrgraphs.net</literal>. In 2013 this tool was solidifying into a commercial offering. While the backing of commercial vendor was <emphasis>extremely</emphasis> helpful to DCR graph, the Exformatics tool was becoming too heavyweight for quick academic experiments. Accordingly, the ITU group in 2013 commenced development of a nimbler implementation. This effort was spearheaded by S&#x00F8;ren Debois and became the DCR Workbench of the present chapter.</para>
<para>The two tools have different goals: Exformatics&#x2019; offering is aimed at non-expert commercial users and emphasises stability and usability. Conversely, the DCR Workbench is aimed at academics and prioritises ease-of-experimentation overall. This division has so far been productive: sufficiently good ideas implemented in the Workbench has later been re-implemented by Exformatics in their commercial offering [<link linkend="ch05_b8">8</link>&#x2013;<link linkend="ch05_b10">10</link>, <link linkend="ch05_b28">28</link>].</para>
<para>The Workbench made its first appearance in a research paper in 2014 [<link linkend="ch05_b12">12</link>], and its first appearance in industry collaborations in 2015 [<link linkend="ch05_b9">9</link>]. Subsequently, the Workbench has provided implementation and examples for most major developments of the formalism [<link linkend="ch05_b2">2</link>, <link linkend="ch05_b6">6</link>, <link linkend="ch05_b12">12</link>&#x2013;<link linkend="ch05_b14">14</link>, <link linkend="ch05_b16">16</link>].</para>
<para>The DCR Workbench is implemented in F# [<link linkend="ch05_b39">39</link>], using the WebSharper library [<link linkend="ch05_b5">5</link>] to derive server- and client-side components from the same F# code base. The choice of implementation language and platform is no accident: On the one hand, F# is very well-suited to manipulating formal models; on the other, the web-based platform makes the Workbench <emphasis>immediately</emphasis> available to interested researchers: all it takes is a browser.</para>
</section>
<section class="lev2" id="sec5-1-2" label="5.1.2" xreflabel="5.1.2">
<title>The DCR Workbench</title>
<para>The DCR Workbench is available at</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog101a.jpg" mime-subtype="jpeg"/></para>
<para>This URL leads to a special page supporting this chapter with the collection of examples used on the following pages. We encourage the reader to visit this page and actively try out the examples presented in the remainder of this chapter as he progresses through the it.</para>
<para><emphasis role="strong">Overview</emphasis> In Section 5.2, we introduce a running example, and in Section 5.3, we recall DCR graphs. In Section 5.4 we introduce basic modelling, simulation and analysis of DCR graphs in the Workbench. In Section 5.5 we construct models by <emphasis>refinement</emphasis>; in Section 5.6 we discuss timed models; in Section 5.7 we talk about subprocesses; and in Section 5.8 data. In Section 5.9, we mention briefly other tools in the Workbench, before concluding in Section 5.10.</para>
</section>
</section>
<section class="lev1" id="sec5-2" label="5.2" xreflabel="5.2">
<title>Running Example</title>
<para>As a running example we consider a stylised mortgage loan application process distilled from real-life cases [<link linkend="ch05_b9">9</link>, <link linkend="ch05_b13">13</link>]. Mortgage application processes are in practice <emphasis>extremely</emphasis> varied, depending on the type of mortgage, the neigh-bourhood, the applicant, and the credit institution in question. The purpose of the process is to arrive at a point where the activity <emphasis role="romanAlt">Assess loan application</emphasis> can be carried out. This requires in turn:</para>
<orderedlist numeration="arabic" continuation="restarts" spacing="normal">
<listitem><para>collecting appropriate documentation,</para></listitem>
<listitem><para>collecting a budget from the applicant, and</para></listitem>
<listitem><para>appraising the property.</para></listitem>
</orderedlist>
<para>In practice, applicants&#x2019; budgets tend to be underspecified, so an intern will screen the budget and request a new one if the submitted one happens to be so. The case worker should not spend time assessing the application if the documentation has not been collected or the budget is underspecified. The caseworker decides if the appraisal can be entirely statistical, i.e., carried out without physical inspection, or if it requires an on-site appraisal. For reasons of cost efficiency, only one appraisal should be carried out.</para>
</section>
<section class="lev1" id="sec5-3" label="5.3" xreflabel="5.3">
<title>Dynamic Condition-Response Graphs</title>
<para>In this section, we recall DCR graphs [<link linkend="ch05_b6">6</link>, <link linkend="ch05_b9">9</link>, <link linkend="ch05_b14">14</link>, <link linkend="ch05_b15">15</link>, <link linkend="ch05_b18">18</link>, <link linkend="ch05_b29">29</link>, <link linkend="ch05_b37">37</link>]. We begin by an informal walkthrough, followed by a formal development in Section 5.3.4.</para>
<para>DCR graphs constitute a declarative modelling notation describing at the same time a process and its run-time state. The core notation comprises <emphasis>labelled events</emphasis>, <emphasis>event states</emphasis>, and five possible <emphasis>relations</emphasis> between events. The relations govern: (a) how executability of one event depend on the state of another, and (b) how execution of one event updates the states of another.</para>
<section class="lev2" id="sec5-3-1" label="5.3.1" xreflabel="5.3.1">
<title>Event States</title>
<para>The <emphasis>event state</emphasis> consists of three booleans: The <emphasis>executed, included,</emphasis> and <emphasis>pending</emphasis> states of the event.</para>
<itemizedlist mark="bullet" spacing="normal">
<listitem><para>The <emphasis>executed</emphasis> state simply registers whether the event has been previously executed (an event may execute more than once). It is updated to true whenever the event executes. It is never updated to false.</para></listitem>
<listitem><para>The <emphasis>included</emphasis> state indicates whether the event is included, i.e. relevant for the process. Being included is a prerequisite for an event to execute.</para></listitem>
<listitem><para>The <emphasis>pending</emphasis> state indicates whether the event is required to eventually execute (or become not included).</para></listitem>
</itemizedlist>
<para>We give events and initial states for the running example in <link linkend="fig5_1">Figure <xref linkend="fig5_1" remap="5.1"/></link>. Except <emphasis role="romanAlt">Request new budget</emphasis>, which becomes relevant only when a budget has been submitted, all events are included. The <emphasis role="romanAlt">Assess loan application</emphasis> and <emphasis role="romanAlt">Submit budget</emphasis> events are pending: they are required to complete the process.</para>
</section>
<section class="lev2" id="sec5-3-2" label="5.3.2" xreflabel="5.3.2">
<title>Relations</title>
<para>Each pair of events may be related by one of five different relations. Relations regulate (a) which events may execute in a given graph (condition, milestone) and (b) the effect of executing an event (inclusion, exclusion, response).</para>
<para>We give a full DCR model of the running example<footnote id="fn5_1" label="1"><para>This graph is in fact the output of the DCR Workbench visualiser; we describe in Section 5.4.2 exactly how the visualiser represents event state.</para></footnote> in <link linkend="fig5_2">Figure <xref linkend="fig5_2" remap="5.2"/></link>.</para>
<para><emphasis>Conditions.</emphasis> A condition <emphasis role="romanAlt">e</emphasis> <emphasis>&#x2192;&#x2022;</emphasis> <emphasis role="romanAlt">f</emphasis> causes the target activity <emphasis role="romanAlt">f</emphasis> to be not executable whenever the source activity <emphasis role="romanAlt">e</emphasis> is included (its &#8220;included&#8221; state is true) and has not been previously executed (its &#8220;executed&#8221; state is false). E.g., in <link linkend="fig5_2">Figure <xref linkend="fig5_2" remap="5.2"/></link>, we must execute <emphasis role="romanAlt">Collect documents</emphasis> before <emphasis role="romanAlt">Assess loan application</emphasis> can be executed.</para>
<para><emphasis>Milestones.</emphasis> A milestone <emphasis role="romanAlt">e</emphasis> <emphasis>&#x2192;</emphasis>&#x22C4; <emphasis role="romanAlt">f</emphasis> causes the target activity <emphasis role="romanAlt">f</emphasis> to be not executable whenever the source activity <emphasis role="romanAlt">e</emphasis> is included and pending (its &#8220;included&#8221; and &#8220;pending&#8221; states are true). In <link linkend="fig5_2">Figure <xref linkend="fig5_2" remap="5.2"/></link>, whenever <emphasis role="romanAlt">Submit budget</emphasis> is pending, <emphasis role="romanAlt">Assess loan application</emphasis> is prevented from executing.</para>
<fig id="fig5_1" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 5.1</label>
<caption><title>Events and initial states (marking) for the mortgage application process. Where nothing else is indicated, the initial state of an event is not executed, included, and not pending.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig021.jpg" mime-subtype="jpeg"/>
</fig>
<fig id="fig5_2" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 5.2</label>
<caption><title>DCR graph modelling the mortgage application process.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig022.jpg" mime-subtype="jpeg"/>
</fig>
<para><emphasis>Responses.</emphasis> A response <emphasis role="romanAlt">e</emphasis> <emphasis>&#x2022;&#x2192;</emphasis> <emphasis role="romanAlt">f</emphasis> causes the target activity <emphasis role="romanAlt">f</emphasis> to be pending (its &#8220;pending&#8221; state is true) whenever the source activity <emphasis role="romanAlt">e</emphasis> is executed. In <link linkend="fig5_2">Figure <xref linkend="fig5_2" remap="5.2"/></link>, when an applicant executes <emphasis role="romanAlt">Submit budget</emphasis>, we require a subsequent screening: there is a response from <emphasis role="romanAlt">Submit budget</emphasis> to <emphasis role="romanAlt">Budget screening approve</emphasis>.</para>
<para><emphasis>Inclusions and exclusions.</emphasis> An inclusion <emphasis role="romanAlt">e</emphasis> <emphasis>&#x2192;</emphasis><emphasis role="roman">+</emphasis> <emphasis role="romanAlt">f</emphasis> resp. an exclusion <emphasis role="romanAlt">e</emphasis> <emphasis>&#x2192;</emphasis>% <emphasis role="romanAlt">f</emphasis> causes the &#8220;included&#8221; state of the target activity <emphasis role="romanAlt">f</emphasis> to be true resp. false when the source activity <emphasis role="romanAlt">e</emphasis> is executed. In <link linkend="fig5_2">Figure <xref linkend="fig5_2" remap="5.2"/></link>, the activity <emphasis role="romanAlt">Submit budget</emphasis> includes the activity <emphasis role="romanAlt">Request new budget</emphasis>.</para>
</section>
<section class="lev2" id="sec5-3-3" label="5.3.3" xreflabel="5.3.3">
<title>Executing Events</title>
<para><emphasis>Enabled</emphasis> events are the ones which have their &#8220;included&#8221; state true, and which do not have their execution prohibited by a condition or milestone as indicated above. Conditions or milestones from excluded events <emphasis>do not</emphasis> disable their target events. E.g., in <link linkend="fig5_2">Figure <xref linkend="fig5_2" remap="5.2"/></link>, both <emphasis role="romanAlt">On-site appraisal</emphasis> and <emphasis role="romanAlt">Statistical appraisal</emphasis> are conditions for <emphasis role="romanAlt">Assess loan application</emphasis>, but once one executes, the other is excluded and thus no longer required for <emphasis role="romanAlt">Assess loan application</emphasis> to execute.</para>
<para>Executing an enabled event in a DCR graph updates event states as indicated by inclusion, exclusion, and response relations. Conceptually, we can think of the execution as producing a new DCR graph with the same relations but with updated event states.</para>
<para>The <emphasis>denotation</emphasis> of a DCR model is the set of (finite and infinite) sequences of event labels, corresponding to a sequence of such event executions, where at each step, every pending event is eventually executed or excluded at some later step. We refer to such sequences as <emphasis>accepting traces</emphasis>. It follows that for finite accepting traces, in the final state, no event is pending and included.</para>
<para>We consider potential traces for <link linkend="fig5_2">Figure <xref linkend="fig5_2" remap="5.2"/></link>.</para>
<itemizedlist mark="bullet" spacing="normal">
<listitem><para><emphasis>s</emphasis><subscript>0</subscript> <emphasis role="roman">=</emphasis> &#x3008;<emphasis role="romanAlt">Collect documents</emphasis><emphasis>,</emphasis> <emphasis role="romanAlt">Assess loan application</emphasis>&#x3009;. This sequence intuitively corresponds to assessing the loan application <emphasis>without</emphasis> getting a budget and appraising the property. After <emphasis role="romanAlt">Collect documents</emphasis>, the event <emphasis role="romanAlt">Assess loan application</emphasis> is not enabled, so <emphasis>s</emphasis><subscript>0</subscript> is not a trace.</para></listitem>
<listitem><para><emphasis>s</emphasis><subscript>1</subscript> <emphasis role="roman">=</emphasis> &#x3008;<emphasis role="romanAlt">Collect documents</emphasis><emphasis>,</emphasis> <emphasis role="romanAlt">Submit budget</emphasis>&#x3009;. This sequence is a trace, but not an accepting one, since <emphasis role="romanAlt">Assess loan application</emphasis> is pending and included in the final state. It follows that <emphasis>s</emphasis><subscript>1</subscript> sequence is not part of the denotation of <link linkend="fig5_2">Figure <xref linkend="fig5_2" remap="5.2"/></link>.</para></listitem>
<listitem><para><emphasis>s</emphasis><subscript>2</subscript> <emphasis role="roman">=</emphasis> &#x3008;<emphasis role="romanAlt">Collect documents</emphasis><emphasis>,</emphasis> <emphasis role="romanAlt">Submit budget</emphasis><emphasis>,</emphasis> <emphasis role="romanAlt">Budget screening approve</emphasis><emphasis>,</emphasis> <emphasis role="romanAlt">Statistical appraisal</emphasis><emphasis>,</emphasis> <emphasis role="romanAlt">Assess loan application</emphasis>&#x3009; is an accepting trace.</para></listitem>
</itemizedlist>
<para>Notice that between the notions of enabledness and accepting trace, DCR graphs express both permissions and obligations. We return to expressiveness of DCR graphs in Section 5.7 below.</para>
</section>
<section class="lev2" id="sec5-3-4" label="5.3.4" xreflabel="5.3.4">
<title>Formal Development</title>
<para><emphasis role="strong">Definition 1</emphasis> (DCR Graph [<link linkend="ch05_b18">18</link>])<emphasis role="strong">.</emphasis> A <emphasis>DCR graph</emphasis>, ranged over by <emphasis>G</emphasis>, is a tuple <emphasis role="roman">(</emphasis><emphasis role="roman1">E</emphasis><emphasis>,</emphasis><emphasis role="roman1">R</emphasis><emphasis>,</emphasis><emphasis role="roman1">M</emphasis><emphasis>,</emphasis> <emphasis>&#x2113;</emphasis>) where</para>
<itemizedlist mark="bullet" spacing="normal">
<listitem><para><emphasis role="roman1">E</emphasis> is a finite set of (labelled) <emphasis>events</emphasis>, the nodes of the graph.</para></listitem>
<listitem><para><emphasis role="roman1">R</emphasis> is the edges of the graph. Edges are partitioned into five kinds, named and drawn as follows: The <emphasis>conditions</emphasis> <emphasis role="roman">(</emphasis><emphasis>&#x2192;&#x2022;</emphasis><emphasis role="roman">)</emphasis>, <emphasis>responses</emphasis> <emphasis role="roman">(</emphasis><emphasis>&#x2022;&#x2192;</emphasis><emphasis role="roman">)</emphasis>, <emphasis>milestones</emphasis> <emphasis role="roman">(</emphasis><emphasis>&#x2192;</emphasis>&#x22C4; <emphasis role="roman">)</emphasis>, <emphasis>inclusions</emphasis> <emphasis role="roman">(</emphasis><emphasis>&#x2192;</emphasis><emphasis role="roman">+)</emphasis>, and <emphasis>exclusions</emphasis> <emphasis role="roman">(</emphasis><emphasis>&#x2192;</emphasis>%<emphasis role="roman">)</emphasis>.</para></listitem>
<listitem><para><emphasis role="roman1">M</emphasis> is the <emphasis>marking</emphasis> of the graph. This is a triple <emphasis role="roman">(</emphasis><emphasis role="roman1">Ex</emphasis><emphasis>,</emphasis><emphasis role="roman1">Re</emphasis><emphasis>,</emphasis><emphasis role="roman1">In</emphasis><emphasis role="roman">)</emphasis> of sets of events, respectively the previously executed <emphasis role="roman">(</emphasis><emphasis role="roman1">Ex</emphasis><emphasis role="roman">)</emphasis>, the currently pending <emphasis role="roman">(</emphasis><emphasis role="roman1">Re</emphasis><emphasis role="roman">)</emphasis>, and the currently included <emphasis role="roman">(</emphasis><emphasis role="roman1">In</emphasis><emphasis role="roman">)</emphasis> events.</para></listitem>
<listitem><para><emphasis>&#x2113;</emphasis> is a labelling function assigning to each <emphasis>e</emphasis> &#x2208; <emphasis role="roman1">E</emphasis> a label comprising an activity name and a set of roles.</para></listitem>
</itemizedlist>
<para>When <emphasis>G</emphasis> is a DCR graph, we write, e.g., <emphasis role="roman1">E</emphasis><emphasis role="roman">(</emphasis><emphasis>G</emphasis><emphasis role="roman">)</emphasis> for the set of events of <emphasis>G</emphasis>, <emphasis role="roman1">Ex</emphasis><emphasis role="roman">(</emphasis><emphasis>G</emphasis><emphasis role="roman">)</emphasis> for the executed events in the marking of <emphasis>G</emphasis>, etc.</para>
<para><emphasis>Notation.</emphasis> Let <emphasis>R</emphasis> &#x2286; <emphasis>X</emphasis> <emphasis>&#x00D7;</emphasis> <emphasis>Y</emphasis> be a relation. For <emphasis>y</emphasis> &#x2208; <emphasis>Y</emphasis> we take <emphasis>Ry</emphasis> <emphasis role="roman">=</emphasis> {<emphasis>x</emphasis> &#x2208; <emphasis>X</emphasis> <emphasis>|</emphasis> <emphasis role="roman">(</emphasis><emphasis>x</emphasis><emphasis>,</emphasis> <emphasis>y</emphasis><emphasis role="roman">)</emphasis> &#x2208; <emphasis>R</emphasis>}; dually for <emphasis>x</emphasis> &#x2208; <emphasis>X</emphasis> we take <emphasis>xR</emphasis> <emphasis role="roman">=</emphasis> {<emphasis>y</emphasis> &#x2208; <emphasis>Y</emphasis> <emphasis>|</emphasis> <emphasis role="roman">(</emphasis><emphasis>x</emphasis><emphasis>,</emphasis> <emphasis>y</emphasis><emphasis role="roman">)</emphasis> &#x2208; <emphasis>R</emphasis>}. We use this notation for relations, e.g.,, <emphasis role="roman">(</emphasis><emphasis>&#x2192;&#x2022;</emphasis> <emphasis>e</emphasis><emphasis role="roman">)</emphasis> is the set of events that are conditions for <emphasis>e</emphasis>.</para>
<para><emphasis role="strong">Definition 2</emphasis> (Enabled events)<emphasis role="strong">.</emphasis> Let <emphasis>G</emphasis> <emphasis role="roman">= (</emphasis><emphasis role="roman1">E</emphasis><emphasis>,</emphasis> <emphasis role="roman1">R</emphasis><emphasis>,</emphasis> <emphasis role="roman1">M</emphasis><emphasis>,</emphasis> <emphasis>&#x2113;</emphasis><emphasis role="roman">)</emphasis> be a DCR graph, with marking <emphasis role="roman1">M</emphasis> <emphasis role="roman">= (</emphasis><emphasis role="roman1">Ex</emphasis><emphasis>,</emphasis> <emphasis role="roman1">Re</emphasis><emphasis>,</emphasis> <emphasis role="roman1">In</emphasis><emphasis role="roman">)</emphasis>. An event <emphasis>e</emphasis> &#x2208; <emphasis role="roman1">E</emphasis> is <emphasis>enabled</emphasis>, written <emphasis>e</emphasis> &#x2208; <emphasis role="roman1">enabled</emphasis><emphasis role="roman">(</emphasis><emphasis>G</emphasis><emphasis role="roman">)</emphasis>, iff (a) <emphasis>e</emphasis> &#x2208; <emphasis role="roman1">In</emphasis>, (b) <emphasis role="roman1">In</emphasis> <emphasis>&#x2229;</emphasis> <emphasis role="roman">(</emphasis><emphasis>&#x2192;&#x2022;</emphasis><emphasis>e</emphasis><emphasis role="roman">)</emphasis> &#x2286; <emphasis role="roman1">Ex</emphasis>, and (c) <emphasis role="roman1">In</emphasis> <emphasis>&#x2229;</emphasis> <emphasis role="roman">(</emphasis><emphasis>&#x2192;</emphasis>&#x22C4; <emphasis>e</emphasis><emphasis role="roman">)</emphasis> &#x2286; <emphasis role="roman1">E</emphasis><emphasis>\</emphasis><emphasis role="roman1">Re</emphasis>.</para>
<para>That is, enabled events (a) are included, (b) have their included conditions executed, and (c) have no included milestone with an unfulfilled responses.</para>
<para><emphasis role="strong">Definition 3</emphasis> (Execution)<emphasis role="strong">.</emphasis> Let <emphasis>G</emphasis> <emphasis role="roman">= (</emphasis><emphasis role="roman1">E</emphasis><emphasis>,</emphasis> <emphasis role="roman1">R</emphasis><emphasis>,</emphasis> <emphasis role="roman1">M</emphasis><emphasis>,</emphasis> <emphasis>&#x2113;</emphasis><emphasis role="roman">)</emphasis> be a DCR graph with marking <emphasis role="roman1">M</emphasis> <emphasis role="roman">= (</emphasis><emphasis role="roman1">Ex</emphasis><emphasis>,</emphasis> <emphasis role="roman1">Re</emphasis><emphasis>,</emphasis> <emphasis role="roman1">In</emphasis><emphasis role="roman">)</emphasis>. Suppose <emphasis>e</emphasis> &#x2208; <emphasis role="roman1">enabled</emphasis><emphasis role="roman">(</emphasis><emphasis>G</emphasis><emphasis role="roman">)</emphasis>. We may <emphasis>execute e</emphasis> obtaining the DCR graph <emphasis>G</emphasis>&#x2032; <emphasis role="roman">= (</emphasis><emphasis role="roman1">E</emphasis><emphasis>,</emphasis> <emphasis role="roman1">R</emphasis><emphasis>,</emphasis> <emphasis role="roman1">M</emphasis>&#x2032;<emphasis>,</emphasis> <emphasis>&#x2113;</emphasis><emphasis role="roman">)</emphasis> with <emphasis role="roman1">M</emphasis>&#x2032; <emphasis role="roman">= (</emphasis><emphasis role="roman1">Ex</emphasis>&#x2032;<emphasis>,</emphasis> <emphasis role="roman1">Re</emphasis>&#x2032;<emphasis>,</emphasis> <emphasis role="roman1">In</emphasis>&#x2032;<emphasis role="roman">)</emphasis> defined as follows.</para>
<itemizedlist mark="arabic" spacing="normal">
<listitem><para><emphasis role="roman1">Ex</emphasis>&#x2032; <emphasis role="roman">=</emphasis> <emphasis role="roman1">Ex</emphasis> <emphasis>&#x222A;</emphasis> <emphasis>e</emphasis></para></listitem>
<listitem><para><emphasis role="roman1">Re</emphasis>&#x2032; <emphasis role="roman">= (</emphasis><emphasis role="roman1">Re</emphasis> <emphasis>\</emphasis> <emphasis>e</emphasis><emphasis role="roman">)</emphasis> <emphasis>&#x222A;</emphasis> <emphasis role="roman">(</emphasis><emphasis>e</emphasis><emphasis>&#x2022;&#x2192;</emphasis><emphasis role="roman">)</emphasis></para></listitem>
<listitem><para><emphasis role="roman1">In</emphasis>&#x2032; <emphasis role="roman">= (</emphasis><emphasis role="roman1">In</emphasis> <emphasis>\</emphasis> <emphasis role="roman">(</emphasis><emphasis>e</emphasis> <emphasis>&#x2192;</emphasis>%<emphasis role="roman">))</emphasis> <emphasis>&#x222A;</emphasis> <emphasis role="roman">(</emphasis><emphasis>e</emphasis><emphasis>&#x2192;</emphasis><emphasis role="roman">+)</emphasis></para></listitem>
</itemizedlist>
<para>That is, to execute an event <emphasis>e</emphasis> one must: (1) add <emphasis>e</emphasis> to the set <emphasis role="roman1">Ex</emphasis> of executed events; (2) update the currently required responses <emphasis role="roman1">Re</emphasis> by first removing <emphasis>e</emphasis>, then adding any responses required by <emphasis>e</emphasis>; and (3) remove from <emphasis role="roman1">In</emphasis> those events excluded by <emphasis>e</emphasis>, then adding those included by <emphasis>e</emphasis>.</para>
<para>Technically, the operational semantics of a DCR graph is the labelled transition system where states are graphs and transitions are executions.</para>
<para><emphasis role="strong">Definition 4</emphasis> (Transitions)<emphasis role="strong">.</emphasis> Let <emphasis>G</emphasis> be a DCR graph. If <emphasis>e</emphasis> &#x2208; <emphasis role="roman1">enabled</emphasis><emphasis role="roman">(</emphasis><emphasis>G</emphasis><emphasis role="roman">)</emphasis> and executing <emphasis>e</emphasis> in <emphasis>G</emphasis> yields <emphasis>H</emphasis>, we say that <emphasis>G</emphasis> has <emphasis>transition on e to H</emphasis> and write <emphasis>G</emphasis> <emphasis>&#x2192;</emphasis><emphasis><subscript>e</subscript></emphasis> <emphasis>H</emphasis>. A <emphasis>run</emphasis> of <emphasis>G</emphasis> is a (finite or infinite) sequence of DCR graphs <emphasis>G</emphasis><emphasis><subscript>i</subscript></emphasis> and events <emphasis>e</emphasis><emphasis><subscript>i</subscript></emphasis> such that: <emphasis>G</emphasis> <emphasis role="roman">=</emphasis> <emphasis>G</emphasis><subscript>0</subscript> <emphasis>&#x2192;</emphasis><emphasis><subscript>e</subscript></emphasis><subscript>0</subscript> <emphasis>G</emphasis><subscript>1</subscript> <emphasis>&#x2192;</emphasis><emphasis><subscript>e</subscript></emphasis><subscript>1</subscript> <emphasis>...</emphasis>. A <emphasis>trace</emphasis> of <emphasis>G</emphasis> is a sequence of labels of events <emphasis>e</emphasis><emphasis><subscript>i</subscript></emphasis> associated with a run of <emphasis>G</emphasis>. We write <emphasis role="roman1">runs</emphasis><emphasis role="roman">(</emphasis><emphasis>G</emphasis><emphasis role="roman">)</emphasis> and <emphasis role="roman1">traces</emphasis><emphasis role="roman">(</emphasis><emphasis>G</emphasis><emphasis role="roman">)</emphasis> for the set of runs and traces of <emphasis>G</emphasis>, respectively.</para>
<para>The denotation of a DCR graph is the set of <emphasis>accepting</emphasis> finite and infinite traces allowed by its operational semantics.</para>
<para><emphasis role="strong">Definition 5</emphasis> (Acceptance)<emphasis role="strong">.</emphasis> A run <emphasis>G</emphasis><subscript>0</subscript> <emphasis>&#x2192;</emphasis><emphasis><subscript>e</subscript></emphasis><subscript>0</subscript> <emphasis>G</emphasis><subscript>1</subscript> <emphasis>&#x2192;</emphasis><emphasis><subscript>e</subscript></emphasis><subscript>1</subscript> <emphasis>...</emphasis> is <emphasis>accepting</emphasis> iff for all <emphasis>n</emphasis> with <emphasis>e</emphasis> &#x2208; <emphasis role="roman1">In</emphasis><emphasis role="roman">(</emphasis><emphasis>G</emphasis><emphasis><subscript>n</subscript></emphasis><emphasis role="roman">)</emphasis> <emphasis>&#x2229;</emphasis> <emphasis role="roman1">Re</emphasis><emphasis role="roman">(</emphasis><emphasis>G</emphasis><emphasis><subscript>n</subscript></emphasis><emphasis role="roman">)</emphasis> there exists <emphasis>m</emphasis> <emphasis>&gt;</emphasis> <emphasis>n</emphasis> s.t. either <emphasis>e</emphasis><emphasis><subscript>m</subscript></emphasis> <emphasis role="roman">=</emphasis> <emphasis>e</emphasis>, or <emphasis>e</emphasis> <emphasis>&#x2209;</emphasis> <emphasis role="roman1">In</emphasis><emphasis role="roman">(</emphasis><emphasis>G</emphasis><emphasis><subscript>m</subscript></emphasis><emphasis role="roman">)</emphasis>. A <emphasis>trace is accepting</emphasis> iff it has an underlying run which is.</para>
<para>Acceptance tells us which workflows a DCR graph accepts, its <emphasis>language</emphasis>.</para>
<para><emphasis role="strong">Definition 6</emphasis> (Language)<emphasis role="strong">.</emphasis> The <emphasis>language</emphasis> of a DCR graph <emphasis>G</emphasis> is the set of its accepting traces. We write <emphasis role="roman1">lang</emphasis><emphasis role="roman">(</emphasis><emphasis>G</emphasis><emphasis role="roman">)</emphasis> for the language of <emphasis>G</emphasis>.</para>
<para>We conclude this Section by noting that by Definitions 2 and 3, because the set of events is finite, both the set of enabled events and the result of executing an event are computable in polynomial time.</para>
</section>
</section>
<section class="lev1" id="sec5-4" label="5.4" xreflabel="5.4">
<title>Modelling with the Workbench</title>
<para>A typical configuration of the Workbench can be seen in <link linkend="fig5_3">Figure <xref linkend="fig5_3" remap="5.3"/></link>. The Workbench is divided into panels. In the configuration in <link linkend="fig5_3">Figure <xref linkend="fig5_3" remap="5.3"/></link>, we see the Visualiser, Parser and Activity Log panels.</para>
<para>The Workbench maintains at all times a current DCR graph and a current trace. Each panel allow the user to interact with this current graph and current trace. A few panels also maintain a DCR graph of their own.</para>
<para>Panels are dynamic: The user is free to remove panels by clicking &#8220;close&#8221; in the lower-left corner of a panel; or to add panels by selecting a new panel in the &#8220;Add a new panel&#8221; section of the Workbench panel. At the time of writing, the Workbench implements 22 different panels.</para>
<para>When working with the Workbench, it is customary to have several panels open; e.g., a visualiser and one or more analysis panels. The Workbench panel contains a selection of seven pre-made such panel configurations called &#8220;presets&#8221;. These presets are accessible through the left-hand &#8220;Load a preset&#8221; section of the Workbench panel.</para>
<para>Finally, the Workbench can function as a process engine, making some of its functionality available programmatically as a REST interface; see the right-hand &#8220;REST API&#8221; section.</para>
<section class="lev2" id="sec5-4-1" label="5.4.1" xreflabel="5.4.1">
<title>Inputting a Model: The Parser Panel</title>
<para>The parser panel allows input of DCR graphs as plain text. The parser accepts programs written according to the grammar of <link linkend="fig5_4">Figure <xref linkend="fig5_4" remap="5.4"/></link>.</para>
<para>As an example program, consider the abridged variant of our running example given in <link linkend="fig5_6">Figure <xref linkend="fig5_6" remap="5.6"/></link>; the corresponding input program is listed in <link linkend="fig5_5">Figure <xref linkend="fig5_5" remap="5.5"/></link>. The Workbench accepts such source programs as input, producing visualisations automatically. (Visualisations in this chapter was so produced.)</para>
<fig id="fig5_3" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 5.3</label>
<caption><title>The DCR Workbench (<literal>http://dcr.tools</literal>).</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig023.jpg" mime-subtype="jpeg"/>
</fig>
<fig id="fig5_4" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 5.4</label>
<caption><title>EBNF definition of the language recognised by the Parser panel.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig024.jpg" mime-subtype="jpeg"/>
</fig>
<fig id="fig5_5" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 5.5</label>
<caption><title>Source code for the core process.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig025.jpg" mime-subtype="jpeg"/>
</fig>
<para>The concrete syntax specifies events and relations such as &#8220;condition from <emphasis role="romanAlt">A</emphasis> to <emphasis role="romanAlt">B</emphasis>&#8221; with expressions such as &#8220;<literal>A --&gt;* B</literal>&#8221;. An event state is specified by prefixing an event with modifiers such as <literal>!</literal> or <literal>%</literal>. We see this on line 6 in <link linkend="fig5_5">Figure <xref linkend="fig5_5" remap="5.5"/></link>. If the event occurs more than once in the program, it is sufficient to prefix the modifier only once. We specify roles by adding a role tag to the event. We see this on line 3 in <link linkend="fig5_5">Figure <xref linkend="fig5_5" remap="5.5"/></link>. More than one role may be added; in general, the same tag may be added multiple times.</para>
<para>It is occasionally convenient to relate more than one event at the same time. In the present case, <emphasis role="romanAlt">Assess loan application</emphasis> needs conditions on both <emphasis role="romanAlt">Collect documents</emphasis> and <emphasis role="romanAlt">Submit budget</emphasis>. We specify these conditions concisely by enclosing the latter two in parenthesis, as seen on line 3&#x2013;4.</para>
<fig id="fig5_6" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 5.6</label>
<caption><title>Visualisation of core process of <link linkend="fig5_5">Figure <xref linkend="fig5_5" remap="5.5"/></link>.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig026.jpg" mime-subtype="jpeg"/>
</fig>
<para>As the user types in the parser, a preview of the graph being input is presented on the right. The input graph substitutes the current graph and resets the current trace when the user clicks &#8220;Load&#8221;.</para>
<para>The parser also understands the XML format output by Exformatics A/S commercial <literal>http://dcrgraphs.net</literal> tool [<link linkend="ch05_b15">15</link>, <link linkend="ch05_b38">38</link>].</para>
</section>
<section class="lev2" id="sec5-4-2" label="5.4.2" xreflabel="5.4.2">
<title>Visualisation and Simulation: The Visualiser and Activity Log Panels</title>
<para>The Visualiser, top-most in <link linkend="fig5_3">Figure <xref linkend="fig5_3" remap="5.3"/></link>, displays a visualisation of the current DCR graph. In <link linkend="fig5_3">Figure <xref linkend="fig5_3" remap="5.3"/></link>, the chosen visualisation is simply the graph layout; alternatively, the underlying transition system may be shown (see below).</para>
<para>The visualisation of the core application process (<link linkend="fig5_5">Figure <xref linkend="fig5_5" remap="5.5"/></link>) is reproduced in <link linkend="fig5_6">Figure <xref linkend="fig5_6" remap="5.6"/></link>. The visualiser represent events as boxes, labelled by the activity of the event (centre) and the role or participant executing that activity (top). E.g., the top-left box represents an activity <emphasis role="romanAlt">Collect documents</emphasis> which is carried out by a Caseworker.</para>
<para>Activities are coloured according to their state: grey background is not currently executable (<emphasis role="romanAlt">Assess loan application</emphasis> in <link linkend="fig5_6">Figure <xref linkend="fig5_6" remap="5.6"/></link>), red label! with an exclamation mark is pending (ditto); &#8220;greyed out&#8221; boxes are excluded events (<emphasis role="romanAlt">Request new budget</emphasis> in the original <link linkend="fig5_2">Figure <xref linkend="fig5_2" remap="5.2"/></link>); and finally, executed events have a tick mark after their action label, (<emphasis role="romanAlt">Submit budget</emphasis> in <link linkend="fig5_3">Figure <xref linkend="fig5_3" remap="5.3"/></link>).</para>
<para><emphasis role="strong">Simulation</emphasis> The visualiser allows executing events by clicking. E.g., to execute <emphasis role="romanAlt">Submit budget</emphasis>, simply click it. This will extend the current trace with that execution, and replace the current graph with the one obtained by applying the updates to event state resulting from the execution of <emphasis role="romanAlt">Submit budget</emphasis> (in this case, setting &#8220;executed&#8221; of that event true). Use the browser&#x2019;s back buttons to revert to a previous state.</para>
<para>The Activity Log panel, third from the top in <link linkend="fig5_3">Figure <xref linkend="fig5_3" remap="5.3"/></link>, displays the current trace, analogous to the way the visualiser displays the current graph.</para>
<para><emphasis role="strong">State-space enumeration</emphasis> As mentioned in Definition 4, a DCR graph gives rise to a labelled transition system (LTS), where states are markings and transitions are labelled event executions. The visualiser can be configured to render a visualisation of the state space of the DCR graph rather than the DCR graph itself, through the drop-down button on the left of the panel. The visualiser highlights the current run in that LTS. The visualisation of the full LTS of the full mortgage application process of <link linkend="fig5_2">Figure <xref linkend="fig5_2" remap="5.2"/></link>.</para>
<para>The visualiser was originally reported in [<link linkend="ch05_b12">12</link>], with the transition system generator following in [<link linkend="ch05_b9">9</link>].</para>
</section>
</section>
<section class="lev1" id="sec5-5" label="5.5" xreflabel="5.5">
<title>Refinement</title>
<para>We proceed to construct step-wise the full mortgage process application by <emphasis>refinement</emphasis> [<link linkend="ch05_b6">6</link>, <link linkend="ch05_b14">14</link>]. We begin with the core process of <link linkend="fig5_5">Figures <xref linkend="fig5_5" remap="5.5"/></link> and <link linkend="fig5_6"><xref linkend="fig5_6" remap="5.6"/></link>. We first add the process fragments for budget submission and screening given in <link linkend="fig5_8">Figures <xref linkend="fig5_8" remap="5.8"/></link> and <link linkend="fig5_9"><xref linkend="fig5_9" remap="5.9"/></link>.</para>
<para>The Workbench supports step-wise refinement: by using in the parser the &#8220;Merge&#8221; button rather than the &#8220;Load&#8221; button. Whereas &#8220;Load&#8221; replaces the current graph and sets the current trace to empty, the &#8220;Merge&#8221; button preserves both, replacing the global current graph with (graph) union <emphasis>G</emphasis> &#x2295; <emphasis>H</emphasis> of the current graph <emphasis>G</emphasis> and the parser&#x2019;s current local graph <emphasis>H</emphasis>.</para>
<para>To refine the core process by the budget fragment, we make sure that the core process is the current graph, then enter the fragment (<link linkend="fig5_9">Figure <xref linkend="fig5_9" remap="5.9"/></link>) in the parser, and click &#8220;Merge&#8221;. The result is the graph in <link linkend="fig5_10">Figure <xref linkend="fig5_10" remap="5.10"/></link>. As can be seen, the resulting process is close to the full running example in <link linkend="fig5_2">Figure <xref linkend="fig5_2" remap="5.2"/></link>, except the process fragment for appraising the property is missing.</para>
<fig id="fig5_7" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 5.7</label>
<caption><title>Transition system of the full mortgage application process (top), with the red box expanded for readability (bottom).</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig027.jpg" mime-subtype="jpeg"/>
</fig>
<para>Repeating the Merge procedure with the process fragment in <link linkend="fig5_11">Figure <xref linkend="fig5_11" remap="5.11"/></link> adds the missing bits and leaves us with exactly <link linkend="fig5_2">Figure <xref linkend="fig5_2" remap="5.2"/></link>&#x2014;this is how the examples for the present chapter has been constructed.</para>
<fig id="fig5_8" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 5.8</label>
<caption><title>Budget process fragment.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig028.jpg" mime-subtype="jpeg"/>
</fig>
<fig id="fig5_9" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 5.9</label>
<caption><title>Visualisation of the budget process fragment of <link linkend="fig5_8">Figure <xref linkend="fig5_8" remap="5.8"/></link>.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig029.jpg" mime-subtype="jpeg"/>
</fig>
<para>Not every such merge preserves the language of the original graph. Exclusions may void conditions, giving the merged graph behaviour not present in the original graph, even when restricting attention to only the events of that graph. As a very simple example, consider the two graphs <emphasis>G</emphasis> <emphasis role="roman">=</emphasis> <emphasis role="romanAlt">a</emphasis> <emphasis>&#x2192;&#x2022;</emphasis> <emphasis role="romanAlt">b</emphasis> and <emphasis>H</emphasis> <emphasis role="roman">=</emphasis> <emphasis role="romanAlt">c</emphasis> <emphasis>&#x2192;</emphasis>% <emphasis role="romanAlt">a</emphasis>. The union <emphasis>G</emphasis> &#x2295; <emphasis>H</emphasis> <emphasis role="roman">=</emphasis> <emphasis role="romanAlt">a</emphasis> <emphasis>&#x2192;&#x2022;</emphasis> <emphasis role="romanAlt">b</emphasis><emphasis>,</emphasis> <emphasis role="romanAlt">c</emphasis> <emphasis>&#x2192;</emphasis>% <emphasis role="romanAlt">a</emphasis> has the trace &#x3008;<emphasis role="romanAlt">c</emphasis><emphasis>,</emphasis> <emphasis role="romanAlt">b</emphasis>&#x3009;; even if we dismiss the new event <emphasis role="romanAlt">c</emphasis>, <emphasis>G</emphasis> could not by itself exhibit the trace &#x3008;<emphasis role="romanAlt">b</emphasis>&#x3009;.</para>
<fig id="fig5_10" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 5.10</label>
<caption><title>Visualisation of the core process (<link linkend="fig5_5">Figures <xref linkend="fig5_5" remap="5.5"/></link> and <link linkend="fig5_6"><xref linkend="fig5_6" remap="5.6"/></link>) refined by the budget fragment (<link linkend="fig5_8">Figures <xref linkend="fig5_8" remap="5.8"/></link> and <link linkend="fig5_9"><xref linkend="fig5_9" remap="5.9"/></link>).</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig030.jpg" mime-subtype="jpeg"/>
</fig>
<fig id="fig5_11" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 5.11</label>
<caption><title>Appraisal process fragment.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig031.jpg" mime-subtype="jpeg"/>
</fig>
<para>This situation was investigated in detail in previous work [<link linkend="ch05_b6">6</link>, <link linkend="ch05_b14">14</link>], where a sufficient condition for a refinement to be language preserving in the above sense was established in a much richer setting. For the present notion of DCR graphs, it is sufficient to require that the refining graph does not exclude or include any events of the original graph.</para>
<para><emphasis>Notation.</emphasis> Given a sequence <emphasis>s</emphasis> and an alphabet &#x03A3;, write <emphasis>s</emphasis> &#x21C2;<subscript>&#x03A3;</subscript> for the largest sub-sequence <emphasis>s</emphasis>&#x2032; of <emphasis>s</emphasis> s.t. <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/equ115_1.jpg" mime-subtype="jpeg"/>; e.g, if <emphasis>s</emphasis> <emphasis role="roman">=</emphasis> <emphasis>AABC</emphasis> then <emphasis>s</emphasis>&#x21C2;<emphasis><subscript>A</subscript></emphasis><emphasis>,</emphasis><emphasis><subscript>C</subscript></emphasis> <emphasis role="roman">=</emphasis> <emphasis>AAC</emphasis>.</para>
<para><emphasis role="strong">Definition 7.</emphasis> Given DCR graphs <emphasis>G</emphasis> and <emphasis>H</emphasis>, we say that <emphasis>H is a refinement of G</emphasis> iff <emphasis role="roman1">lang</emphasis><emphasis role="roman">(</emphasis><emphasis>H</emphasis><emphasis role="roman">)</emphasis> &#x21C2;<emphasis><subscript>l</subscript></emphasis><subscript>(</subscript><subscript>E</subscript><subscript>(</subscript><emphasis><subscript>G</subscript></emphasis><subscript>))</subscript> &#x2286; <emphasis role="roman1">lang</emphasis><emphasis role="roman">(</emphasis><emphasis>G</emphasis><emphasis role="roman">)</emphasis>.</para>
<para>That is, the language of <emphasis>H</emphasis> restricted to the labels used by events in <emphasis>G</emphasis> must be a subset of the language of <emphasis>G</emphasis>. We can now state the following Proposition [<link linkend="ch05_b6">6</link>, Theorem 43]:</para>
<para><emphasis role="strong">Proposition 1.</emphasis> <emphasis>Let G and G</emphasis>&#x2032; <emphasis>be DCR processes such that for every e</emphasis> &#x2208; <emphasis role="roman1">E</emphasis><emphasis role="roman">(</emphasis><emphasis>G</emphasis><emphasis role="roman">)</emphasis><emphasis>, there is no relation x</emphasis> <emphasis>&#x2192;</emphasis>% <emphasis>e or x</emphasis> <emphasis>&#x2192;</emphasis><emphasis role="roman">+</emphasis> <emphasis>e in G</emphasis>&#x2032;<emphasis>. Then the graph union G</emphasis> &#x2295; <emphasis>G</emphasis>&#x2032; <emphasis>is a refinement of G.</emphasis></para>
<para>If the Workbench has current graph <emphasis>G</emphasis> and the parser has graph <emphasis>G</emphasis>&#x2032; <emphasis>not</emphasis> satisfying (a published [<link linkend="ch05_b6">6</link>,<link linkend="ch05_b14">14</link>] generalisation of) the conditions of Proposition 1, the Parser issues a warning and requires confirmation before merging.</para>
<para>DCR refinement was originally suggested in [<link linkend="ch05_b17">17</link>, <link linkend="ch05_b24">24</link>] and worked out comprehensively in [<link linkend="ch05_b6">6</link>, <link linkend="ch05_b14">14</link>]. The Workbench implements this latter mechanism.</para>
</section>
<section class="lev1" id="sec5-6" label="5.6" xreflabel="5.6">
<title>Time</title>
<para>The Workbench supports the extension of DCR graphs with time [<link linkend="ch05_b2">2</link>, <link linkend="ch05_b23">23</link>]. Time is modelled discretely by a special action <emphasis role="romanAlt">tick</emphasis> modelling the passage of time; conditions are augmented with an optional delay, <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/equ115_2.jpg" mime-subtype="jpeg"/>, and responses with an optional deadline <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/equ115_3.jpg" mime-subtype="jpeg"/>. Intuitively, the delay in the timed condition requires that at least <emphasis>k</emphasis> ticks have passed after the last execution of <emphasis role="romanAlt">e</emphasis> before <emphasis role="romanAlt">f</emphasis> may execute; dually, the deadline in the timed response requires that at most <emphasis>k</emphasis> ticks pass after the last execution of <emphasis role="romanAlt">e</emphasis> before <emphasis role="romanAlt">f</emphasis> must execute.</para> 
<para>The former requirement makes it possible to have <emphasis>timelocks</emphasis>, i.e., situations where, say, <emphasis role="romanAlt">f</emphasis> must execute, but is not allowed to. As a very simple example, consider the DCR graph <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/equ115_4.jpg" mime-subtype="jpeg"/>. In this graph, once <emphasis role="romanAlt">e</emphasis> executes, <emphasis>at least</emphasis> 3 ticks must pass before <emphasis role="romanAlt">f</emphasis> <emphasis>can</emphasis> execute because of the condition delay, but <emphasis>at most</emphasis> 2 ticks may pass before <emphasis role="romanAlt">f</emphasis> <emphasis>must</emphasis> execute because of the response deadline. After the sequence &#x3008;<emphasis role="romanAlt">e</emphasis><emphasis>,</emphasis><emphasis role="romanAlt">tick</emphasis><emphasis>,</emphasis><emphasis role="romanAlt">tick</emphasis>&#x3009;, the graph is said to be <emphasis>time-locked</emphasis>: Time cannot advance without a constraint being violated.</para>
<para>For our running example, suppose that (a) the initial screening of the customer&#x2019;s budget must be completed within 5 days, and (b) that the final assessment of the loan application must wait a 3-day &#8220;grace period&#8221; after a statistical appraisal (in order to prevent caseworkers from doing overly optimistic statistical appraisals). We model these constraints as a timed DCR graph directly using a timed response and condition in <link linkend="fig5_12">Figures <xref linkend="fig5_12" remap="5.12"/></link> and <link linkend="fig5_13"><xref linkend="fig5_13" remap="5.13"/></link>.</para>
<para>Timed DCR graphs are still in finite state [<link linkend="ch05_b23">23</link>], but deciding time-lock freedom naively by exploring in the state space is infeasible. Recent research [<link linkend="ch05_b2">2</link>] established a sufficient condition for a graph to be time-lock&#x2013; free and gave a generic &#8220;enforcement mechanism&#8221; for time-lock&#x2013;free graphs, that is, a device which monitors the progression of time and a DCR graph and proactively causes events to execute to avoid missing deadlines.</para>
<para>The Workbench implements time as defined in [<link linkend="ch05_b23">23</link>], and time-lock&#x2013;analysis and enforcement as defined in [<link linkend="ch05_b2">2</link>].</para>
<fig id="fig5_12" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 5.12</label>
<caption><title>Additional timing constraints for the mortgage application process in <link linkend="fig5_2">Figure <xref linkend="fig5_2" remap="5.2"/></link>.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig032.jpg" mime-subtype="jpeg"/>
</fig>
<fig id="fig5_13" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 5.13</label>
<caption><title>Visualisation of additional timing constraints for the mortgage application process in <link linkend="fig5_2">Figure <xref linkend="fig5_2" remap="5.2"/></link>.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig033.jpg" mime-subtype="jpeg"/>
</fig>
</section>
<section class="lev1" id="sec5-7" label="5.7" xreflabel="5.7">
<title>Subprocesses</title>
<para>It may happen that a customer <emphasis>during the application process</emphasis> applies for pre-approval of an expected increase in property value due to, e.g., ongoing kitchen remodellings. In this case, the caseworker must assess the limit extension before deciding on the mortgage application itself. At the caseworker&#x2019;s discretion, an intern may or may not collect bank statements from the customer for the limit extension assessment; however, collecting that statement requires the customer&#x2019;s explicit consent.</para>
<para>Such limit extensions in practice may happen several times during a mortgage application due to, e.g., expanded scope of a kitchen remodelling project. Thus, the limit extension fragment is a <emphasis>subprocess</emphasis>: A process that may be added to the main process when necessary, and possibly repeatedly.</para>
<para>Note that since subprocesses may be added repeatedly, each such addition must duplicate the events of the subprocess. This situation is akin to bound names under replication being duplicated in in the <emphasis>&#x03C0;</emphasis>-calculus [<link linkend="ch05_b35">35</link>]. The subprocess may contain both events local to the subprocess, <emphasis>bound</emphasis> events, and references to events of the containing graph. The former are indicated syntactically with a <literal>/</literal> prefix as seen in lines 6&#x2013;8.</para>
<para>The Workbench supports subprocesses; we add the above limit extension process in <link linkend="fig5_14">Figures <xref linkend="fig5_14" remap="5.14"/></link> and <link linkend="fig5_15"><xref linkend="fig5_15" remap="5.15"/></link>. Note that in visualisation, the subprocess is not visible until it has been expanded once.</para>
<para>The visualisation shows the triggering event <emphasis role="romanAlt">Apply for limit extension</emphasis>&#x2014; singled out as spawning a subprocess by the <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/equ117_1.jpg" mime-subtype="jpeg"/> following contemporary business process notations, e.g., BPMN [<link linkend="ch05_b32">32</link>]. The bound events in a subprocess are shown with round corners and inside a dashed box<footnote id="fn5_2" label="2"><para>If a subprocess adds new global events&#x2014;as opposed to bound ones&#x2014;these would appear with square corners inside the box.</para></footnote>.</para>
<fig id="fig5_14" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 5.14</label>
<caption><title>Additional subprocess constraints (credit limit extension) for the full mortgage application process of <link linkend="fig5_2">Figure <xref linkend="fig5_2" remap="5.2"/></link>.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig034.jpg" mime-subtype="jpeg"/>
</fig>
<fig id="fig5_15" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 5.15</label>
<caption><title>Visualisation of additional subprocess constraints (credit limit extension) for the full mortgage application process of <link linkend="fig5_2">Figure <xref linkend="fig5_2" remap="5.2"/></link>; after one execution of <emphasis role="romanAlt">Apply for limit extension</emphasis>.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig035.jpg" mime-subtype="jpeg"/>
</fig>
<para>Subprocess semantics is based on graph union; if <emphasis>G</emphasis> <emphasis role="roman">=</emphasis> <emphasis>...,</emphasis> <emphasis role="romanAlt">e</emphasis>{<emphasis>H</emphasis>} contains a subprocess-spawning event <emphasis role="romanAlt">e</emphasis>, then executing <emphasis role="romanAlt">e</emphasis> will form the graph <emphasis>G</emphasis> &#x2295; <emphasis>H</emphasis>, then apply the effects of <emphasis role="romanAlt">e</emphasis>. Note the importance of bound names here: If events in <emphasis>H</emphasis> were not bound, then repeated instantiation of the subprocess would not change the graph, i.e., <emphasis>G</emphasis> &#x2295; <emphasis>H</emphasis> &#x2295; <emphasis>H</emphasis> <emphasis role="roman">=</emphasis> <emphasis>G</emphasis> &#x2295; <emphasis>H</emphasis>. This equation emphatically does not hold under the current semantics, where events in <emphasis>H</emphasis> may be bound, and thus replicated. In the running example, executing <emphasis role="romanAlt">Apply for limit extension</emphasis> <emphasis>twice</emphasis> would result in all rounded-box event to be replicated <emphasis>twice</emphasis>&#x2014;we invite the reader to try this out in the Workbench.</para>
<para>Adding subprocesses and bound events significantly increase the expressive power of DCR graphs [<link linkend="ch05_b6">6</link>, Theorem 9]:</para>
<para><emphasis role="strong">Theorem 1.</emphasis> <emphasis>DCR graphs express the union of regular and</emphasis> &#x03C9; <emphasis>-regular languages. Graphs with subprocesses and bound events are Turing complete.</emphasis></para>
<para>In particular, while event-reachability and refinement is decidable for plain and timed DCR graphs, they are undecidable for DCR graphs with subprocesses and bound events.</para>
<para>DCR graphs was extended with a notion of subprocesses and bound events in [<link linkend="ch05_b12">12</link>], followed by an investigation of expressive power in [<link linkend="ch05_b6">6</link>, <link linkend="ch05_b14">14</link>]. The Workbench implements subprocesses in the sense of [<link linkend="ch05_b12">12</link>].</para>
</section>
<section class="lev1" id="sec5-8" label="5.8" xreflabel="5.8">
<title>Data</title>
<para>The Workbench augments DCR graphs with a notion of &#8220;input events&#8221; and relations conditional on data. Suppose for our running example that if the amount applied for in a credit limit extension exceeds EUR 10.000, then having a bank statement becomes a condition for evaluating the loan application.</para>
<para>Technically, this is accomplished by: (a) adding the option of inputting a value when a subprocess is spawned; and (b) adding data-guards on select relations. When the subprocess is instantiated, condition on the input value dictates whether, e.g., the relation <emphasis role="romanAlt">e</emphasis> <emphasis>&#x2192;&#x2022;</emphasis> <emphasis role="romanAlt">f</emphasis> takes effect or not.</para>
<para>We extend the running example with such a conditional condition in <link linkend="fig5_16">Figure <xref linkend="fig5_16" remap="5.16"/></link>. Note that since the &#8220;variable&#8221; associated with an event is simply the name of the event, it becomes convenient to specify separately the name and label of the event. This is done in line 3, where it is specified that the event <emphasis role="romanAlt">limit</emphasis> has label <emphasis role="romanAlt">Apply for a limit extension</emphasis> and role Customer. (Without an explicit specification, the Workbench identifies event and label.)</para>
<para>The visualiser does not show data-guarded relations, and the formal semantics of DCR graphs with data have yet to be published.</para>
<fig id="fig5_16" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 5.16</label>
<caption><title>Alternative subprocess-with-data constraints (credit limit extension) for the full mortgage application process of <link linkend="fig5_2">Figure <xref linkend="fig5_2" remap="5.2"/></link>.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig036.jpg" mime-subtype="jpeg"/>
</fig>
<para>An interesting application of data is that of specifying user-input &#8220;forms&#8221; (think Web forms) via DCR graph, associating with each event in a graph an input field in such a form. This idea was implemented in the Actions panel and later realised [<link linkend="ch05_b27">27</link>] in collaboration with Exformatics A/S.</para>
</section>
<section class="lev1" id="sec5-9" label="5.9" xreflabel="5.9">
<title>Other Panels</title>
<para>We mention here briefly a few panels of the Workbench not discussed so far.</para>
<orderedlist numeration="arabic" continuation="restarts" spacing="normal">
<listitem><para>An encoding from DCR to the GSM model [<link linkend="ch05_b26">26</link>] was defined in recent research [<link linkend="ch05_b16">16</link>]; the Workbench implements this encoding an outputs CMMN [<link linkend="ch05_b4">4</link>] XML.</para></listitem>
<listitem><para>Notions of concurrency and independence of DCR events, following the standard notion of labelled asynchronous transition systems [<link linkend="ch05_b3">3</link>, <link linkend="ch05_b36">36</link>], was recently investigated [<link linkend="ch05_b13">13</link>]. The Workbench&#x2019; Concurrency panel implements these notions, automatically identifying concurrent events.</para></listitem>
<listitem><para>Work on applications of DCR in practical settings suggested a need for simplifying process models when presented to end users [<link linkend="ch05_b9">9</link>]. The Workbench contains a number of such simplifying views, most notably a &#8220;swimlane&#8221; view of the current trace in the panel of the same name, and a mechanism for projecting a graph in various ways to sub-graphs of interest.</para></listitem>
</orderedlist>
</section>
<section class="lev1" id="sec5-10" label="5.10" xreflabel="5.10">
<title>Conclusion</title>
<para>We have given an introduction to DCR graphs, and an overview of the DCR Workbench. Since the Workbench implements most major variations of DCR graphs, this Chapter has also served as a survey of the state-of-the-art of DCR graphs as modelling and analysis tool for continuously changing distributed collaborative processes.</para>
<para>The Workbench has been instrumental for scientific research, providing a test-bed for quick experiments with new ideas; for teaching, providing students the opportunities for hands-on learning of abstract concepts; and for collaborations with industry and knowledge transfer to industry. In all these instances, providing a practical platform on which to demonstrate sometimes difficult-to-communicate abstract concepts helps to cement the reality and applicability of DCR as a modelling methodology. In particular, the Workbench has paved the way for academic results [<link linkend="ch05_b9">9</link>, <link linkend="ch05_b12">12</link>, <link linkend="ch05_b18">18</link>, <link linkend="ch05_b21">21</link>, <link linkend="ch05_b27">27</link>] to find their way to implementation in commercial tools [<link linkend="ch05_b8">8</link>, <link linkend="ch05_b28">28</link>].</para>
<para>We invite the reader to use the Workbench for research and teaching. It is available at <literal>http://dcr.tools</literal>.</para>
</section>
<section class="lev1" id="sec2a">
<title>References</title>
<orderedlist numeration="arabic" continuation="restarts" spacing="normal">
<listitem><para><anchor id="ch05_b1"/>Wil M. P. van der Aalst. Verification of Workflow Nets. In <emphasis>Proc. of the 18th Int. Conf. on Application and Theory of Petri Nets, ICATPN</emphasis>, pages 407&#x2013;426, 1997. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Wil+M%2E+P%2E+van+der+Aalst%2E+Verification+of+Workflow+Nets%2E+In+Proc%2E+of+the+18th+Int%2E+Conf%2E+on+Application+and+Theory+of+Petri+Nets%2C+ICATPN%2C+pages+407-426%2C+1997%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch05_b2"/>David A. Basin, S&#x00F8;ren Debois, and Thomas T. Hildebrandt. In the nick of time: Proactive prevention of obligation violations. In <emphasis>IEEE 29th Computer Security Foundations Symposium, CSF 2016</emphasis>, pages 120&#x2013;134. IEEE Computer Society, 2016. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=David+A%2E+Basin%2C+S%F8ren+Debois%2C+and+Thomas+T%2E+Hildebrandt%2E+In+the+nick+of+time%3A+Proactive+prevention+of+obligation+violations%2E+In+IEEE+29th+Computer+Security+Foundations+Symposium%2C+CSF+2016%2C+pages+120-134%2E+IEEE+Computer+Society%2C+2016%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch05_b3"/>Marek Bednarczyk. <emphasis>Categories of asynchronous systems</emphasis>. PhD thesis, U. Sussex, 1988.</para></listitem>
<listitem><para><anchor id="ch05_b4"/>BizAgi and others. Case Management Model and Notation (CMMN), v1, May 2014. OMG Document Number formal/2014-05-05, Object Management Group. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=BizAgi+and+others%2E+Case+Management+Model+and+Notation+%28CMMN%29%2C+v1%2C+May+2014%2E+OMG+Document+Number+formal%2F2014-05-05%2C+Object+Management+Group%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch05_b5"/>Joel Bjornson, Anton Tayanovskyy, and Adam Granicz. <emphasis>Composing Reactive GUIs in F# Using WebSharper</emphasis>, pages 203&#x2013;216. Springer, 2011. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Joel+Bjornson%2C+Anton+Tayanovskyy%2C+and+Adam+Granicz%2E+Composing+Reactive+GUIs+in+F%23+Using+WebSharper%2C+pages+203-216%2E+Springer%2C+2011%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch05_b6"/>S&#x00F8;ren Debois, Thomas Hildebrandt, and Tijs Slaats. Replication, refinement &#x0026; reachability: Complexity in Dynamic Condition-Response graphs. <emphasis>Acta Informatica</emphasis>, 2017. Accepted for publication.</para></listitem>
<listitem><para><anchor id="ch05_b7"/>S&#x00F8;ren Debois, Thomas T. Hildebrandt, Paw H&#x00F8;sgaard Larsen, and Kenneth Ry Ulrik. Declarative process mining for DCR graphs. In <emphasis>SAC &#x2019;17</emphasis>, 2017. Accepted for publication.</para></listitem>
<listitem><para><anchor id="ch05_b8"/>S&#x00F8;ren Debois, Thomas T. Hildebrandt, Morten Marquard, and Tijs Slaats. Bridging the valley of death: A success story on danish funding schemes paving a path from technology readiness level 1 to 9. In <emphasis>SER&#x0026;IP 2015</emphasis>, pages 54&#x2013;57. IEEE, 2015.</para></listitem>
<listitem><para><anchor id="ch05_b9"/>S&#x00F8;ren Debois, Thomas T. Hildebrandt, Morten Marquard, and Tijs Slaats. Hybrid process technologies in the financial sector. In <emphasis>BPM 2015, Industry track</emphasis>, volume 1439 of <emphasis>CEUR Workshop Proceedings</emphasis>, pages 107&#x2013;119. CEUR-WS.org, 2015. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=S%F8ren+Debois%2C+Thomas+T%2E+Hildebrandt%2C+Morten+Marquard%2C+and+Tijs+Slaats%2E+Hybrid+process+technologies+in+the+financial+sector%2E+In+BPM+2015%2C+Industry+track%2C+volume+1439+of+CEUR+Workshop+Proceedings%2C+pages+107-119%2E+CEUR-WS%2Eorg%2C+2015%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch05_b10"/>S&#x00F8;ren Debois, Thomas T. Hildebrandt, Morten Marquard, and Tijs Slaats. The DCR graphs process portal. In <emphasis>BPM 2016</emphasis>, volume 1789 of <emphasis>CEUR Workshop Proceedings</emphasis>, pages 7&#x2013;11. CEUR-WS.org, 2016. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=S%F8ren+Debois%2C+Thomas+T%2E+Hildebrandt%2C+Morten+Marquard%2C+and+Tijs+Slaats%2E+The+DCR+graphs+process+portal%2E+In+BPM+2016%2C+volume+1789+of+CEUR+Workshop+Proceedings%2C+pages+7-11%2E+CEUR-WS%2Eorg%2C+2016%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch05_b11"/>S&#x00F8;ren Debois, Thomas T. Hildebrandt, and Lene Sandberg. Experience report: Constraint-based modelling and simulation of railway emergency response plans. In <emphasis>ANT 2016 / SEIT-2016</emphasis>, volume 83 of <emphasis>Procedia Computer Science</emphasis>, pages 1295&#x2013;1300. Elsevier, 2016. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=S%F8ren+Debois%2C+Thomas+T%2E+Hildebrandt%2C+and+Lene+Sandberg%2E+Experience+report%3A+Constraint-based+modelling+and+simulation+of+railway+emergency+response+plans%2E+In+ANT+2016+%2F+SEIT-2016%2C+volume+83+of+Procedia+Computer+Science%2C+pages+1295-1300%2E+Elsevier%2C+2016%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch05_b12"/>S&#x00F8;ren Debois, Thomas T. Hildebrandt, and Tijs Slaats. Hierarchical declarative modelling with refinement and sub-processes. In <emphasis>BPM 2014</emphasis>, volume 8659 of <emphasis>LNCS</emphasis>, pages 18&#x2013;33. Springer, 2014. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=S%F8ren+Debois%2C+Thomas+T%2E+Hildebrandt%2C+and+Tijs+Slaats%2E+Hierarchical+declarative+modelling+with+refinement+and+sub-processes%2E+In+BPM+2014%2C+volume+8659+of+LNCS%2C+pages+18-33%2E+Springer%2C+2014%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch05_b13"/>S&#x00F8;ren Debois, Thomas T. Hildebrandt, and Tijs Slaats. Concurrency and asynchrony in declarative workflows. In <emphasis>BPM 2015</emphasis>, volume 9253 of LNCS, pages 72&#x2013;89. Springer, 2015. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=S%F8ren+Debois%2C+Thomas+T%2E+Hildebrandt%2C+and+Tijs+Slaats%2E+Concurrency+and+asynchrony+in+declarative+workflows%2E+In+BPM+2015%2C+volume+9253+of+LNCS%2C+pages+72-89%2E+Springer%2C+2015%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch05_b14"/>S&#x00F8;ren Debois, Thomas T. Hildebrandt, and Tijs Slaats. Safety, liveness and run-time refinement for modular process-aware information systems with dynamic sub processes. In <emphasis>FM 2015</emphasis>, pages 143&#x2013;160, 2015. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=S%F8ren+Debois%2C+Thomas+T%2E+Hildebrandt%2C+and+Tijs+Slaats%2E+Safety%2C+liveness+and+run-time+refinement+for+modular+process-aware+information+systems+with+dynamic+sub+processes%2E+In+FM+2015%2C+pages+143-160%2C+2015%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch05_b15"/>S&#x00F8;ren Debois, Thomas T. Hildebrandt, Tijs Slaats, and Morten Marquard. A case for declarative process modelling: Agile development of a grant application system. In <emphasis>EDOC Workshops &#x2019;14</emphasis>, pages 126&#x2013;133. IEEE Computer Society, 2014. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=S%F8ren+Debois%2C+Thomas+T%2E+Hildebrandt%2C+Tijs+Slaats%2C+and+Morten+Marquard%2E+A+case+for+declarative+process+modelling%3A+Agile+development+of+a+grant+application+system%2E+In+EDOC+Workshops+%2714%2C+pages+126-133%2E+IEEE+Computer+Society%2C+2014%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch05_b16"/>Rik Eshuis, S&#x00F8;ren Debois, Tijs Slaats, and Thomas T. Hildebrandt. Deriving consistent GSM schemas from DCR graphs. In <emphasis>ICSOC 2016</emphasis>, volume 9936 of <emphasis>Lecture Notes in Computer Science</emphasis>, pages 467&#x2013;482. Springer, 2016.</para></listitem>
<listitem><para><anchor id="ch05_b17"/>Thomas T. Hildebrandt, Morten Marquard, Raghava Rao Mukkamala, and Tijs Slaats. Dynamic condition response graphs for trustworthy adaptive case management. In <emphasis>OTM 2013 Workshops</emphasis>, volume 8186 of <emphasis>LNCS</emphasis>, pages 166&#x2013;171. Springer, 2013. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Thomas+T%2E+Hildebrandt%2C+Morten+Marquard%2C+Raghava+Rao+Mukkamala%2C+and+Tijs+Slaats%2E+Dynamic+condition+response+graphs+for+trustworthy+adaptive+case+management%2E+In+OTM+2013+Workshops%2C+volume+8186+of+LNCS%2C+pages+166-171%2E+Springer%2C+2013%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch05_b18"/>Thomas T. Hildebrandt and Raghava Rao Mukkamala. Declarative Event-Based Workflow as Distributed Dynamic Condition Response Graphs. In <emphasis>PLACES 2010</emphasis>, volume 69 of <emphasis>EPTCS</emphasis>, pages 59&#x2013;73, 2010. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Thomas+T%2E+Hildebrandt+and+Raghava+Rao+Mukkamala%2E+Declarative+Event-Based+Workflow+as+Distributed+Dynamic+Condition+Response+Graphs%2E+In+PLACES+2010%2C+volume+69+of+EPTCS%2C+pages+59-73%2C+2010%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch05_b19"/>Thomas T. Hildebrandt, Raghava Rao Mukkamala, and Tijs Slaats. Declarative modelling and safe distribution of healthcare workflows. In <emphasis>FHIES 2011</emphasis>, volume 7151 of <emphasis>LNCS</emphasis>, pages 39&#x2013;56. Springer, 2011. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Thomas+T%2E+Hildebrandt%2C+Raghava+Rao+Mukkamala%2C+and+Tijs+Slaats%2E+Declarative+modelling+and+safe+distribution+of+healthcare+workflows%2E+In+FHIES+2011%2C+volume+7151+of+LNCS%2C+pages+39-56%2E+Springer%2C+2011%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch05_b20"/>Thomas T. Hildebrandt, Raghava Rao Mukkamala, and Tijs Slaats. Designing a cross-organizational case management system using dynamic condition response graphs. In <emphasis>EDOC 2011</emphasis>, pages 161&#x2013;170. IEEE Computer Society, 2011. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Thomas+T%2E+Hildebrandt%2C+Raghava+Rao+Mukkamala%2C+and+Tijs+Slaats%2E+Designing+a+cross-organizational+case+management+system+using+dynamic+condition+response+graphs%2E+In+EDOC+2011%2C+pages+161-170%2E+IEEE+Computer+Society%2C+2011%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch05_b21"/>Thomas T. Hildebrandt, Raghava Rao Mukkamala, and Tijs Slaats. Nested dynamic condition response graphs. In <emphasis>FSEN 2011, Revised Selected Papers</emphasis>, volume 7141 of <emphasis>Lecture Notes in Computer Science</emphasis>, pages 343&#x2013;350. Springer, 2011. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Thomas+T%2E+Hildebrandt%2C+Raghava+Rao+Mukkamala%2C+and+Tijs+Slaats%2E+Nested+dynamic+condition+response+graphs%2E+In+FSEN+2011%2C+Revised+Selected+Papers%2C+volume+7141+of+Lecture+Notes+in+Computer+Science%2C+pages+343-350%2E+Springer%2C+2011%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch05_b22"/>Thomas T. Hildebrandt, Raghava Rao Mukkamala, and Tijs Slaats. Safe distribution of declarative processes. In <emphasis>SEFM 2011</emphasis>, volume 7041 of <emphasis>LNCS</emphasis>, pages 237&#x2013;252. Springer, 2011. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Thomas+T%2E+Hildebrandt%2C+Raghava+Rao+Mukkamala%2C+and+Tijs+Slaats%2E+Safe+distribution+of+declarative+processes%2E+In+SEFM+2011%2C+volume+7041+of+LNCS%2C+pages+237-252%2E+Springer%2C+2011%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch05_b23"/>Thomas T. Hildebrandt, Raghava Rao Mukkamala, Tijs Slaats, and Francesco Zanitti. Contracts for cross-organizational workflows as timed dynamic condition response graphs. <emphasis>J. Log. Algebr. Program.</emphasis>, 82(5&#x2013;7):164&#x2013;185, 2013. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Thomas+T%2E+Hildebrandt%2C+Raghava+Rao+Mukkamala%2C+Tijs+Slaats%2C+and+Francesco+Zanitti%2E+Contracts+for+cross-organizational+workflows+as+timed+dynamic+condition+response+graphs%2E+J%2E+Log%2E+Algebr%2E+Program%2E%2C+82%285-7%29%3A164-185%2C+2013%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch05_b24"/>Thomas T. Hildebrandt, Raghava Rao Mukkamala, Tijs Slaats, and Francesco Zanitti. Modular context-sensitive and aspect-oriented processes with dynamic condition response graphs. In <emphasis>FOAL 2013</emphasis>, pages 19&#x2013;24. ACM, 2013. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Thomas+T%2E+Hildebrandt%2C+Raghava+Rao+Mukkamala%2C+Tijs+Slaats%2C+and+Francesco+Zanitti%2E+Modular+context-sensitive+and+aspect-oriented+processes+with+dynamic+condition+response+graphs%2E+In+FOAL+2013%2C+pages+19-24%2E+ACM%2C+2013%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch05_b25"/>Thomas T. Hildebrandt and Francesco Zanitti. A process-oriented event-based programming language. In <emphasis>DEBS 2012</emphasis>, pages 377&#x2013;378. ACM, 2012. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Thomas+T%2E+Hildebrandt+and+Francesco+Zanitti%2E+A+process-oriented+event-based+programming+language%2E+In+DEBS+2012%2C+pages+377-378%2E+ACM%2C+2012%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch05_b26"/>R. Hull, E. Damaggio, F. Fournier, M. Gupta, F. Heath III, S. Hobson, M. Linehan, S. Maradugu, A. Nigam, P. Sukaviriya, and R. Vacul&#x00ED;n. Introducing the guard-stage-milestone approach for specifying business entity lifecycles. In <emphasis>WS-FM 2010</emphasis>, LNCS. Springer, 2010. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=R%2E+Hull%2C+E%2E+Damaggio%2C+F%2E+Fournier%2C+M%2E+Gupta%2C+F%2E+Heath+III%2C+S%2E+Hobson%2C+M%2E+Linehan%2C+S%2E+Maradugu%2C+A%2E+Nigam%2C+P%2E+Sukaviriya%2C+and+R%2E+Vacul%EDn%2E+Introducing+the+guard-stage-milestone+approach+for+specifying+business+entity+lifecycles%2E+In+WS-FM+2010%2C+LNCS%2E+Springer%2C+2010%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch05_b27"/>Morten Marquard, S&#x00F8;ren Debois, Tijs Slaats, and Thomas T. Hilde-brandt. Forms are declarative processes! In <emphasis>BPM 2016 Industry Track (to appear)</emphasis>, 2016. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Morten+Marquard%2C+S%F8ren+Debois%2C+Tijs+Slaats%2C+and+Thomas+T%2E+Hilde-brandt%2E+Forms+are+declarative+processes%21+In+BPM+2016+Industry+Track+%28to+appear%29%2C+2016%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch05_b28"/>Morten Marquard, Muhammad Shahzad, and Tijs Slaats. Web-based modelling and collaborative simulation of declarative processes. In <emphasis>BPM 2015</emphasis>, volume 9253 of LNCS, pages 209&#x2013;225. Springer, 2015. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Morten+Marquard%2C+Muhammad+Shahzad%2C+and+Tijs+Slaats%2E+Web-based+modelling+and+collaborative+simulation+of+declarative+processes%2E+In+BPM+2015%2C+volume+9253+of+LNCS%2C+pages+209-225%2E+Springer%2C+2015%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch05_b29"/>Raghava Rao Mukkamala. <emphasis>A Formal Model For Declarative Workflows: Dynamic Condition Response Graphs</emphasis>. PhD thesis, IT University of Copenhagen, 2012. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Raghava+Rao+Mukkamala%2E+A+Formal+Model+For+Declarative+Workflows%3A+Dynamic+Condition+Response+Graphs%2E+PhD+thesis%2C+IT+University+of+Copenhagen%2C+2012%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch05_b30"/>Raghava Rao Mukkamala and Thomas T. Hildebrandt. From dynamic condition response structures to b&#x00FC;chi automata. In <emphasis>TASE 2010</emphasis>, pages 187&#x2013;190. IEEE, 2010. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Raghava+Rao+Mukkamala+and+Thomas+T%2E+Hildebrandt%2E+From+dynamic+condition+response+structures+to+b%FCchi+automata%2E+In+TASE+2010%2C+pages+187-190%2E+IEEE%2C+2010%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch05_b31"/>Raghava Rao Mukkamala, Thomas T. Hildebrandt, and Tijs Slaats. Towards trustworthy adaptive case management with dynamic condition response graphs. In <emphasis>EDOC 2013</emphasis>, pages 127&#x2013;136. IEEE Computer Society, 2013. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Raghava+Rao+Mukkamala%2C+Thomas+T%2E+Hildebrandt%2C+and+Tijs+Slaats%2E+Towards+trustworthy+adaptive+case+management+with+dynamic+condition+response+graphs%2E+In+EDOC+2013%2C+pages+127-136%2E+IEEE+Computer+Society%2C+2013%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch05_b32"/>Object Management Group BPMN Technical Committee. Business Process Model and Notation, version 2.0, 2013. <literal>http://www.omg.org/spec/BPMN/2.0.2/PDF</literal> <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Object+Management+Group+BPMN+Technical+Committee%2E+Business+Process+Model+and+Notation%2C+version+2%2E0%2C+2013%2E+http%3A%2F%2Fwww%2Eomg%2Eorg%2F+spec%2FBPMN%2F2%2E0%2E2%2FPDF" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch05_b33"/>Maja Pesic, Helen Schonenberg, and Wil M. P. van der Aalst. DECLARE: full support for loosely-structured processes. In <emphasis>EDOC 2007</emphasis>, pages 287&#x2013;300, 2007. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Maja+Pesic%2C+Helen+Schonenberg%2C+and+Wil+M%2E+P%2E+van+der+Aalst%2E+DECLARE%3A+full+support+for+loosely-structured+processes%2E+In+EDOC+2007%2C+pages+287-300%2C+2007%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch05_b34"/>S&#x00F8;ren Debois and Tijs Slaats. The analysis of a real life declarative process. In <emphasis>CIDM 2015</emphasis>. IEEE, 2015. Accepted for publication. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=S%F8ren+Debois+and+Tijs+Slaats%2E+The+analysis+of+a+real+life+declarative+process%2E+In+CIDM+2015%2E+IEEE%2C+2015%2E+Accepted+for+publication%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch05_b35"/>Davide Sangiorgi and David Walker. <emphasis>The pi-calculus: a Theory of Mobile Processes</emphasis>. Cambridge university press, 2003. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Davide+Sangiorgi+and+David+Walker%2E+The+pi-calculus%3A+a+Theory+of+Mobile+Processes%2E+Cambridge+university+press%2C+2003%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch05_b36"/>M. W. Shields. Concurrent machines. <emphasis>Computer Journal</emphasis>, 28(5):449&#x2013; 465, 1985. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=M%2E+W%2E+Shields%2E+Concurrent+machines%2E+Computer+Journal%2C+28%285%29%3A449-+465%2C+1985%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch05_b37"/>Tijs Slaats. <emphasis>Flexible Process Notations for Cross-organizational Case Management Systems</emphasis>. PhD thesis, IT University of Copenhagen, January 2015. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Tijs+Slaats%2E+Flexible+Process+Notations+for+Cross-organizational+Case+Management+Systems%2E+PhD+thesis%2C+IT+University+of+Copenhagen%2C+January+2015%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch05_b38"/>Tijs Slaats, Raghava Rao Mukkamala, Thomas T. Hildebrandt, and Morten Marquard. Exformatics declarative case management workflows as DCR graphs. In <emphasis>BPM &#x2019;13</emphasis>, volume 8094 of <emphasis>LNCS</emphasis>, pages 339&#x2013;354. Springer, 2013. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Tijs+Slaats%2C+Raghava+Rao+Mukkamala%2C+Thomas+T%2E+Hildebrandt%2C+and+Morten+Marquard%2E+Exformatics+declarative+case+management+workflows+as+DCR+graphs%2E+In+BPM+%2713%2C+volume+8094+of+LNCS%2C+pages+339-354%2E+Springer%2C+2013%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch05_b39"/>Don Syme, Jack Hu, Luke Hoban, Tao Liu, Dmitry Lomov, James Margetson, Brian McNamara, Joe Pamer, Penny Orwick, Daniel Quirk, et al. The F# 4.0 language specification. Technical report, 2005. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Don+Syme%2C+Jack+Hu%2C+Luke+Hoban%2C+Tao+Liu%2C+Dmitry+Lomov%2C+James+Margetson%2C+Brian+McNamara%2C+Joe+Pamer%2C+Penny+Orwick%2C+Daniel+Quirk%2C+et+al%2E+The+F%23+4%2E0+language+specification%2E+Technical+report%2C+2005%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch05_b40"/>Wil M. P. van der Aalst and Maja Pesic. DecSerFlow: Towards a truly declarative service flow language. In <emphasis>WS-FM 2006</emphasis>, volume 4184 of <emphasis>LNCS</emphasis>, pages 1&#x2013;23. Springer, 2006. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Wil+M%2E+P%2E+van+der+Aalst+and+Maja+Pesic%2E+DecSerFlow%3A+Towards+a+truly+declarative+service+flow+language%2E+In+WS-FM+2006%2C+volume+4184+of+LNCS%2C+pages+1-23%2E+Springer%2C+2006%2E" target="_blank">Google Scholar</ulink></para></listitem>
</orderedlist>
</section>
</chapter>
<chapter class="chapter" id="ch06" label="6" xreflabel="6">
<title>A Tool for Choreography-Based Analysis of Message-Passing Software</title>
<para><emphasis role="strong">Julien Lange</emphasis><footnote id="fn6_1a" label="1"><para>Imperial College London, UK</para></footnote><emphasis role="strong">, Emilio Tuosto</emphasis><footnote id="fn6_2a" label="2"><para>University of Leicester, UK</para></footnote> <emphasis role="strong">and Nobuko Yoshida</emphasis><footnoteref linkend="fn6_1a" label="1"/></para>
<section class="lev2">
<title>Abstract</title>
<para>An appealing characteristic of choreographies is that they provide two complementary views of communicating software: the <emphasis>global</emphasis> and the <emphasis>local</emphasis> views. Communicating finite-state machines (CFSMs) have been proposed as an expressive formalism to specify local views. Global views have been represented with global graphs, that is graphical choreographies (akin to BPMN and UML) suitable to represent general multiparty session specifications. Global graphs feature expressive constructs such as forking, merging, and joining for representing application-level protocols.</para>
<para>An algorithm for the reconstruction of global graphs from CFSMs has been introduced in [<link linkend="ch06_b17">17</link>]; the algorithm ensures that the reconstructed global graph faithfully represents the original CFSMs provided that they satisfy a suitable condition, called generalised multiparty compatibility (GMC). The CFSMs enjoying GMC are guaranteed to interact without deadlocks and other communication errors. After reviewing the basic concepts underlying global graphs, communicating machines and safe communications, we highlight the main features of <emphasis role="strongAlt">ChorGram</emphasis>, a tool implementing the generalised multiparty compatibility and reconstruction of global graphs of [<link linkend="ch06_b17">17</link>]. We overview the architecture of <emphasis role="strongAlt">ChorGram</emphasis> and present a comprehensive example to illustrate how it is used directly to analyse message-passing software and programs.</para>
</section>
<section class="lev1" id="sec6-1" label="6.1" xreflabel="6.1">
<title>Introduction</title>
<para>Choreographic approaches are becoming popular in the &#8220;top-down&#8221; development of distributed software. In fact, a choreography-based model features two views of software: the <emphasis>global view</emphasis> and the <emphasis>local view</emphasis>. The former is a &#8220;holistic&#8221; specification of the behaviour of the composition of <emphasis>all</emphasis> components (and it abstracts away low level details such as asynchrony). The latter view specifies the behaviour of each component in isolation and should be obtained by <emphasis>projecting</emphasis> the global behaviour with respect to each component. In this framework, well-formedness of the global view and compliance of the realisation of software with respect to the corresponding projection should guarantee the soundness of the communication of the application.</para>
<para>The recent rise of services, cloud, and micro-services is changing the way software is produced. As a matter of fact, applications are being developed by composing (possibly distributed) independent components which coordinate their execution by exchanging messages. Modern applications offer and rely on public APIs to interact with each other, are deployed on different architectures and devices, and try to accommodate the needs of a vast number of users. The term &#8220;API economy&#8221; (see e.g., <literal>ibm.com/apieconomy</literal>) has been coined to refer to such applications. Existing and novel languages, as well as middlewares and programming models, foster this shift in software development. Languages such as Erlang, Elixir, Scala, and Go are paramount examples of this shift and start to be used in a wider range of application domains than the ones they were originally conceived for. For instance, Erlang plays a main role in well-known applications such as WhatsApp [<link linkend="ch06_b24">24</link>] and Facebook [<link linkend="ch06_b21">21</link>].</para>
<para>The trend described above is dictated by the compelling requirements of openness, scalability, and heterogeneity and caters to new challenges. Firstly, this shift pushes the applicability of top-down software development approaches to their limits. The composition mechanisms required to guarantee the interoperability of applications have to be of an order of magnitude more sophisticated than just the type signature of their APIs, as in traditional software engineering practice. More precisely, in order to attain a correct composition, it is crucial to expose (part of) the communication pattern of components. Hence, developers are responsible to guarantee the correct composition of their components. This is not an easy task. Subtle and hard to fix bugs can be introduced by inappropriate communications.</para>
<para>Our recent work [<link linkend="ch06_b17">17</link>] has shown that communication soundness is guaranteed when a set of communicating components enjoys the <emphasis>generalised multiparty compatibility</emphasis> property. Moreover, we have defined an algorithm that reconstructs a global view of a system from the composition of its local components. These results enable the realisation of an effective tool-supported approach to the design and analysis of communicating software. In fact, we have developed <emphasis role="strongAlt">ChorGram</emphasis> [<link linkend="ch06_b16">16</link>], a tool supporting the theory of multiparty compatibility and choreography construction, i.e., <emphasis role="strongAlt">ChorGram</emphasis> implements two fundamentals functionalities: it ensures that a system of CFSMs validates the GMC condition and if so, it returns a choreography which faithfully captures the interactions of the original system.</para>
<para>In this chapter, we introduce <emphasis role="strongAlt">ChorGram</emphasis> and show how it supports software architects in the design and analysis of software. We first review the theoretical results underlying the tool; Section 6.2 presents our theory only informally and with the help of a simple example. Section 6.3 presents the architecture of the tool, how it integrates with the auxiliary tools it relies upon, and its data flow. Section 6.4 shows an application to a non trivial example. We start from a multiparty compatible application and show how a naive evolution could break its multiparty compatibility. We then use <emphasis role="strongAlt">ChorGram</emphasis> to analyse and fix the problem. Section 6.5 gives our concluding remarks.</para>
</section>
<section class="lev1" id="sec6-2" label="6.2" xreflabel="6.2">
<title>Overview of the Theory</title>
<para>Here we introduce the key ingredients of our framework which constructs choreographies, i.e., global graphs such as the one in <link linkend="fig6_2">Figure <xref linkend="fig6_2" remap="6.2"/></link>, from local specifications, i.e., communicating finite-state machines, such as the ones in <link linkend="fig6_1">Figure <xref linkend="fig6_1" remap="6.1"/></link>.</para>
<para><emphasis role="strong">CFSMs</emphasis> In this framework, we use <emphasis>communicating finite-state machines</emphasis> [<link linkend="ch06_b7">7</link>] as behavioural specifications of distributed components (i.e., end-point specifications) from which a choreography can be built. CFSMs are a conceptually simple model and are well-established for analysing properties of distributed systems. A system of CFSMs consists of a finite number of automata which communicate with each other through unbounded FIFO channels. There are two channels for each pair of CFSMs in the system, one in each direction. We present the semantics of CFSMs informally through the example below.</para>
<fig id="fig6_1" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 6.1</label>
<caption><title>Four player game &#x2013; CFSMs.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig037.jpg" mime-subtype="jpeg"/>
</fig>
<para>Consider the system of four machines in <link linkend="fig6_1">Figure <xref linkend="fig6_1" remap="6.1"/></link>, whose initial states are highlighted in blue. Each machine has <emphasis>three</emphasis> input buffers to receive messages from the other three participants and has access to three output buffers to send messages to other participants. Each transition in a machine is either a send action, e.g., <emphasis role="romanAlt1">A</emphasis> &#183; <emphasis role="romanAlt1">B</emphasis>!<emphasis role="romanAlt1">bWin</emphasis> in machine <emphasis role="romanAlt1">A</emphasis> or a receive action, e.g., <emphasis role="romanAlt1">A</emphasis> &#183; <emphasis role="romanAlt1">B</emphasis>?<emphasis role="romanAlt1">bWin</emphasis> in machine <emphasis role="romanAlt1">B</emphasis>. The system realises a protocol of a fictive game where: <emphasis role="romanAlt1">A</emphasis><emphasis>lice</emphasis> (<emphasis role="romanAlt1">A</emphasis>) sends either <emphasis role="romanAlt1">bWin</emphasis> to <emphasis role="romanAlt1">B</emphasis><emphasis>ob</emphasis> (<emphasis role="romanAlt1">B</emphasis>)or <emphasis role="romanAlt1">cWin</emphasis> to <emphasis role="romanAlt1">C</emphasis><emphasis>arol</emphasis> (<emphasis role="romanAlt1">C</emphasis>) to decide who wins the game. In the former case, <emphasis role="romanAlt1">A</emphasis> fires the transition <emphasis role="romanAlt1">A</emphasis> &#183; <emphasis role="romanAlt1">B</emphasis>!<emphasis role="romanAlt1">bWin</emphasis> whereby the message <emphasis role="romanAlt1">bWin</emphasis> is put in the FIFO buffer <emphasis role="romanAlt1">AB</emphasis> from <emphasis role="romanAlt1">A</emphasis> to <emphasis role="romanAlt1">B</emphasis>, and likewise in the latter case. If <emphasis role="romanAlt1">B</emphasis> wins (that is the message <emphasis role="romanAlt1">bWin</emphasis> is on top of the queue <emphasis role="romanAlt1">AB</emphasis> and <emphasis role="romanAlt1">B</emphasis> consumes it by taking the transition <emphasis role="romanAlt1">A</emphasis> &#183; <emphasis role="romanAlt1">B</emphasis>?<emphasis role="romanAlt1">bWin</emphasis>), then he sends a notification (<emphasis role="romanAlt1">cLose</emphasis>)to <emphasis role="romanAlt1">C</emphasis> to notify her that she has lost. Symmetrically, <emphasis role="romanAlt1">C</emphasis> notifies <emphasis role="romanAlt1">B</emphasis> of her victory (<emphasis role="romanAlt1">bLose</emphasis>). During the game, <emphasis role="romanAlt1">C</emphasis> notifies <emphasis role="romanAlt1">D</emphasis><emphasis>ave</emphasis> (<emphasis role="romanAlt1">D</emphasis>)that she is <emphasis role="romanAlt1">busy</emphasis>.</para>
<para>After <emphasis role="romanAlt1">B</emphasis> and <emphasis role="romanAlt1">C</emphasis> have been notified of the outcome of the game, <emphasis role="romanAlt1">B</emphasis> sends a signal (<emphasis role="romanAlt1">sig</emphasis>)to <emphasis role="romanAlt1">A</emphasis>, while <emphasis role="romanAlt1">C</emphasis> sends a message (<emphasis role="romanAlt1">msg</emphasis>)to <emphasis role="romanAlt1">A</emphasis>. Once the result is sent, <emphasis role="romanAlt1">A</emphasis> notifies <emphasis role="romanAlt1">D</emphasis> that <emphasis role="romanAlt1">C</emphasis> is now <emphasis role="romanAlt1">free</emphasis> and a new round starts.</para>
<para><emphasis role="strong">Global graph</emphasis> The final product of our framework is the construction of a choreography which is equivalent to the original system of CFSMs. Global graphs [<link linkend="ch06_b17">17</link>] were inspired by the generalised global types [<link linkend="ch06_b10">10</link>] and BPMN choreography [<link linkend="ch06_b19">19</link>]. Given as input the CFSMs from <link linkend="fig6_1">Figure <xref linkend="fig6_1" remap="6.1"/></link>, our tool generates the global graph in <link linkend="fig6_2">Figure <xref linkend="fig6_2" remap="6.2"/></link>. The nodes of a global graph are labelled according to their function: a node labelled with <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/06_equ01.jpg" mime-subtype="jpeg"/> indicates the starting point of the interactions; a node labelled with <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/06_equ02.jpg" mime-subtype="jpeg"/> indicates the termination of the interactions (not used in <link linkend="fig6_2">Figure <xref linkend="fig6_2" remap="6.2"/></link>); a node labelled with an interaction <emphasis role="romanAlt1">A</emphasis> &#x2192; <emphasis role="romanAlt1">B</emphasis> : <emphasis role="romanAlt1">msg</emphasis> indicates that participant <emphasis role="romanAlt1">A</emphasis> sends a message of type <emphasis role="romanAlt1">msg</emphasis> to <emphasis role="romanAlt1">B</emphasis>; a node labelled <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/06_equ03.jpg" mime-subtype="jpeg"/> indicates either a choice, merge, or recursion; a node labelled with <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/06_equ04.jpg" mime-subtype="jpeg"/> indicates either the start or the end of concurrent interactions. The graphical notation for branch and merge is inspired by process-algebraic notations; the reader familiar with BPMN should note that our <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/06_equ05.jpg" mime-subtype="jpeg"/>-node corresponds to the <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/06_equ06.jpg" mime-subtype="jpeg"/> and <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/06_equ07.jpg" mime-subtype="jpeg"/> gateways in BPMN, while our <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/06_equ08.jpg" mime-subtype="jpeg"/>-node corresponds to the <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/06_equ09.jpg" mime-subtype="jpeg"/> gateway in BPMN.</para>
<para>In the global graph of <link linkend="fig6_2">Figure <xref linkend="fig6_2" remap="6.2"/></link>, the flow of the four player game becomes much clearer. In particular, one can clearly see that either <emphasis role="romanAlt1">B</emphasis> or <emphasis role="romanAlt1">C</emphasis> win the game and that, while the results of the game are being announced, <emphasis role="romanAlt1">C</emphasis> and <emphasis role="romanAlt1">D</emphasis> are interacting.</para>
<para><emphasis role="strong">Communication soundness properties</emphasis> A (runtime) configuration of a system of CFSMs, is a tuple consisting of the states in which each machine is and the content of each channel.</para>
<para>We say that a machine is in a sending (resp. receiving) state if all its outgoing transitions are send (resp. receive) actions. A state without any outgoing transition is said to be final. A state that is neither final, sending nor receiving is a mixed state.</para>
<para>We say that a configuration is a <emphasis>deadlock</emphasis> if all the buffers are empty, there is at least one machine in a receiving state, and all the other machines are either in a receiving state or a final state. A system has the <emphasis>eventual reception property</emphasis> [<link linkend="ch06_b5">5</link>] if whenever a message has been sent by a participant, that message will be eventually received. We say that a system of CFSMs is <emphasis>communication sound</emphasis> if none of its reachable configuration is a deadlock and it validates the eventual reception property.</para>
<fig id="fig6_2" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 6.2</label>
<caption><title>Four player game &#x2013; Global graph.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig038.jpg" mime-subtype="jpeg"/>
</fig>
<para><emphasis role="strong">Ensuring communication soundness</emphasis> Our tool checks that the CFSMs validate <emphasis>generalised multiparty compatibility</emphasis> (GMC) [<link linkend="ch06_b17">17</link>] which guarantees that (<emphasis>i</emphasis>) the projections of the generated global graph are equivalent to the original system and (<emphasis>ii</emphasis>) the system is <emphasis>communication sound</emphasis> (as defined above). The GMC condition consists of two parts: <emphasis>representability</emphasis> and <emphasis>branching property</emphasis>. Both parts are checked against the machines and their synchronous executions, i.e., the finite labelled transition system (dubbed <emphasis>TS</emphasis><subscript>0</subscript>) of the machines executing with the additional constraint that a message can be sent only if its partner is ready to receive it and no other messages are pending in other buffers. For instance, all the synchronous executions of our running example are modelled in the finite labelled transition system in <link linkend="fig6_3">Figure <xref linkend="fig6_3" remap="6.3"/></link>.</para>
<para>The representability condition essentially requires that for each participant, the projection of <emphasis>TS</emphasis><subscript>0</subscript> onto that participant yields an automaton that is bisimilar to the original machine. The branching property condition requires that whenever a branching occurs in <emphasis>TS</emphasis><subscript>0</subscript> then either (<emphasis>i</emphasis>) the branching commutes, i.e., it corresponds to two independent (concurrent) interactions, or (<emphasis>ii</emphasis>) it corresponds to a choice and the following constraints must be met:</para>
<fig id="fig6_3" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 6.3</label>
<caption><title>Four player game &#x2013; TS<subscript>0</subscript>.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig039.jpg" mime-subtype="jpeg"/>
</fig>
<orderedlist numeration="arabic" continuation="restarts" spacing="normal">
<listitem><para>The choice is made by a single participant (the selector).</para></listitem>
<listitem><para>If a participant is not the selector but behaves differently in two branches of the choice, then it must receive different messages in each branch (before the point where its behaviours differ).</para></listitem>
</orderedlist>
<para>Item (1) guarantees that every choice is located at exactly one participant (this is crucial since we are assuming <emphasis>asynchronous</emphasis> communications). Item (2) ensures that all the participants involved in the choice are made aware of which branch was chosen by the selector.</para>
<para>Besides guaranteeing communication soundness, our GMC condition ensures that if a system of CFSMs validates it, then we can construct a global graph which is equivalent to the original system, i.e., the global graph contains exactly the same information than the system of CFSMs.</para>
</section>
<section class="lev1" id="sec6-3" label="6.3" xreflabel="6.3">
<title>Architecture</title>
<para>The structure and the work-flow of our tool is illustrated in <link linkend="fig6_4">Figure <xref linkend="fig6_4" remap="6.4"/></link>. Before commenting on the diagram, we explain its graphical conventions. Dashed arrows represent files used to exchange data; the input files are provided by the user, those of <emphasis role="strongAlt">hkc</emphasis> are generated by the Haskell module <emphasis role="strongAlt">Representability</emphasis>. Solid arrows do not represent invocations but rather control/data flow. For instance, the arrow from <emphasis role="strongAlt">TS</emphasis> represents the fact that the check of the GMC property is made by concurrent threads on the results produced by <emphasis role="strongAlt">TS</emphasis>.</para>
<fig id="fig6_4" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 6.4</label>
<caption><title>Architecture of <emphasis role="strongAlt">ChorGram</emphasis>.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig040.jpg" mime-subtype="jpeg"/>
</fig>
<para><emphasis role="strong">Data- and control-flow</emphasis> The Python script <emphasis role="strongAlt">cfsm2gg</emphasis> provides a command line interface to the application and connects it with the external tools <emphasis role="strongAlt">hkc</emphasis> [<link linkend="ch06_b6">6</link>] and <emphasis role="strongAlt">petrify</emphasis> [<link linkend="ch06_b8">8</link>], respectively used to check language equivalence between projections and their corresponding CFSMs and to extract a Petri net from a transition system. The script takes a description of the system in (a file that is in) either of the two formats described in the following paragraph and triggers all the other activities.</para>
<para>The core functionalities are implemented in the Haskell modules (within the dotted box) and are described below.</para>
<itemizedlist mark="none" spacing="normal">
<listitem><para><emphasis role="strongAlt">gmc</emphasis> is the main program; it is invoked by <emphasis role="strongAlt">cfsm2gg</emphasis>, which passes over the input file (after having set some parameters according to the flags of the invocation). After invoking <emphasis role="strongAlt">SystemParser</emphasis>, the internal Haskell representation of the system of CFSMs is passed by <emphasis role="strongAlt">gmc</emphasis> to <emphasis role="strongAlt">TS</emphasis>, which computes the synchronous transition system &#x2013; TS<subscript>0</subscript> (and the bounded one if required with the <literal>-b</literal> flag of <emphasis role="strongAlt">cfsm2gg</emphasis>). The synchronous transition system is then checked for generalised multiparty compatibility [<link linkend="ch06_b17">17</link>, Definitions 3.4(ii) and 3.5] (but for the language equivalence part [<link linkend="ch06_b17">17</link>, Definition 3.4(i)] later checked by invoking <emphasis role="strongAlt">hkc</emphasis> from <emphasis role="strongAlt">cfsm2gg</emphasis>). This check is performed in parallel and has the side effect of producing the files to give in input to <emphasis role="strongAlt">hkc</emphasis>.</para></listitem>
<listitem><para><emphasis role="strongAlt">cfsm2gg</emphasis> invokes <emphasis role="strongAlt">hkc</emphasis>, once it has obtained the control back from <emphasis role="strongAlt">gmc</emphasis>, to check the language equivalence of the original CFSMs with respect to the corresponding projections of the synchronous transition system. Finally, <emphasis role="strongAlt">petrify</emphasis> is invoked and its output is then transformed by <emphasis role="strongAlt">Build-Global</emphasis> as described in [<link linkend="ch06_b17">17</link>] to obtain a global graph (in dot format) of the system. Besides, <emphasis role="strongAlt">cfsm2gg</emphasis> generates also graphical representation of the communicating machines and the transition systems (again in the <literal>dot</literal> format).</para></listitem>
</itemizedlist>
<para><emphasis role="strong">Input formats</emphasis> The syntax of the input files of <emphasis role="romanAlt">gmc</emphasis> can be specified either in the fsa (after finite state automata) or cms format, the latter being a simple process-algebraic syntax (described below). The format to be used depends on the extension of the file name (<emphasis role="romanAlt">.fsa</emphasis> or <emphasis role="romanAlt">.cms</emphasis> respectively, and for file names without extensions the default format is fsa).</para>
<para>A system consists of a list of automata, each described by specifying an (optional) identifier, its initial state, and its transitions. (Identifiers of CFSMs are strings starting with a letter.) We refer to the example in <link linkend="fig6_5">Figure <xref linkend="fig6_5" remap="6.5"/></link> to describe the fsa format. Consider the text on the left of <link linkend="fig6_5">Figure <xref linkend="fig6_5" remap="6.5"/></link> specifying the (system consisting of) two simple automata depicted on the right.</para>
<fig id="fig6_5" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 6.5</label>
<caption><title>HelloWorld example &#x2013; fsa representation.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig041.jpg" mime-subtype="jpeg"/>
</fig>
<para>It is sometimes more convenient to have a more concrete syntax to represent machines. Therefore we define the alternative cms format. The idea is that each CFSM of a system is described by a process in the syntax that we now describe.</para>
<para>The cms format of a system is a term of the following grammar:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog073a.jpg" mime-subtype="jpeg"/></para>
<para>where <emphasis role="romanAlt1">id</emphasis> is a string used to identify the system, <emphasis role="romanAlt1">A</emphasis><subscript>1</subscript><emphasis>,</emphasis>&#x2026; <emphasis>,</emphasis><emphasis role="romanAlt1">A</emphasis><emphasis><subscript>n</subscript></emphasis> are the names of the machines forming the system, and for each 1 &#x2264; <emphasis>i</emphasis> &#x2264; <emphasis>m</emphasis> (with <emphasis>m</emphasis> &#x2265; <emphasis>n</emphasis> &#x2265; 2) we have a unique defining equation assigning an expression that specifies the behaviour of <emphasis role="romanAlt1">A</emphasis><emphasis><subscript>i</subscript></emphasis>. We can now model the HelloWorld example of <link linkend="fig6_5">Figure <xref linkend="fig6_5" remap="6.5"/></link>, as follows:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog073b.jpg" mime-subtype="jpeg"/></para>
<para>(where the ellipsis will be defined in a moment). The list of defining equations specify the behaviour M<emphasis><subscript>i</subscript></emphasis> of each role <emphasis role="romanAlt1">A</emphasis><emphasis><subscript>i</subscript></emphasis>, with 1 &#x2264; <emphasis>i</emphasis> &#x2264; <emphasis>n</emphasis>, of the system and the behaviour of some auxiliary machines. For each 1 &#x2264; <emphasis>i</emphasis> &#x2264; <emphasis>m</emphasis>, the identity <emphasis role="romanAlt1">A</emphasis><emphasis><subscript>i</subscript></emphasis> cannot appear in the communication actions of the behaviour M<emphasis><subscript>i</subscript></emphasis> of the defining equation <emphasis role="romanAlt1">A</emphasis><emphasis><subscript>i</subscript></emphasis>= <emphasis>M</emphasis><emphasis><subscript>i</subscript></emphasis>.</para>
<para>Basically, the behaviour of a machine<footnote id="fn6_1" label="1"><para>The cms format provides a richer and more flexible syntax which we omit here because not used in the examples. The full syntax is described at <literal>https://bitbucket.org/emlio_ tuosto/chorgram/wiki/Home</literal></para></footnote>
 is specified as a regular expression on an alphabet of actions. We impose some syntactic restrictions to keep out some meaningless terms and define:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog073c.jpg" mime-subtype="jpeg"/></para>
<para>A machine <emphasis>M</emphasis> is a sum of branches <emphasis>B</emphasis>. A branch is a prefix-guarded behaviour (a machine or <emphasis role="romanAlt">end</emphasis>) or it is the invocation to the behaviour of a machine <emphasis role="romanAlt1">A</emphasis> specified in the set of defining equations of the system. Prefixes yield the possible actions of a participant: in <emphasis role="romanAlt1">A</emphasis>!<emphasis role="romanAlt1">m</emphasis> (resp. <emphasis role="romanAlt1">A</emphasis>?<emphasis role="romanAlt1">m</emphasis>), the message <emphasis role="romanAlt1">m</emphasis> is sent to (resp. received from) participant <emphasis role="romanAlt1">A</emphasis>. The equations for the participants of the <emphasis role="romanAlt1">helloWorld</emphasis> system are:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog073d.jpg" mime-subtype="jpeg"/></para>
<para>Trailing occurrences of <emphasis role="romanAlt">end</emphasis> can be omitted, e.g., writing <emphasis role="romanAlt1">A</emphasis> = <emphasis role="romanAlt1">B</emphasis>!<emphasis role="romanAlt1">hello</emphasis>;<emphasis role="romanAlt1">B</emphasis>!<emphasis role="romanAlt1">world</emphasis>. Finally, + is right-associative and gives precedence to all the other operators except <emphasis>[bardbl]</emphasis>, which has the lowest precedence.</para>
</section>
<section class="lev1" id="sec6-4" label="6.4" xreflabel="6.4">
<title>Modelling of an ATM Service</title>
<para>We use a simple scenario to showcase <emphasis role="strongAlt">ChorGram</emphasis>. We want to design the protocol of a service between an ATM (<emphasis role="romanAlt1">A</emphasis>), a bank (<emphasis role="romanAlt1">B</emphasis>), and a customer (<emphasis role="romanAlt1">C</emphasis>), where, after a successful authentication, the customer <emphasis role="romanAlt1">C</emphasis> can withdraw cash or check the balance of their account. Such services are enabled only after the ATM has successfully checked the credentials of <emphasis role="romanAlt1">C</emphasis>. We also require that bank <emphasis role="romanAlt1">B</emphasis> monitors the usage of the cards of its customers, so that unsuccessful attempts to use it are reported to <emphasis role="romanAlt1">C</emphasis> (e.g., via an SMS to the customers&#x2019; mobile).</para>
<section class="lev2" id="sec6-4-1" label="6.4.1" xreflabel="6.4.1">
<title>ATM Service &#x2013; Version 1</title>
<para>For the moment, we will assume that the protocol repeats only after a successful withdrawal. Let us start with the description of the bank <emphasis role="romanAlt1">B</emphasis>:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog074.jpg" mime-subtype="jpeg"/></para>
<para>The bank <emphasis role="romanAlt1">B</emphasis> is notified of the outcome of the authentication by the ATM <emphasis role="romanAlt1">A</emphasis>. If the access fails, <emphasis role="romanAlt1">B</emphasis> sends a message to the customer <emphasis role="romanAlt1">C</emphasis> (lines 1&#x2013;2); otherwise, the bank waits to be told which service has been requested by the customer and acts accordingly (lines 4&#x2013;14). (The symbol &#8220;..&#8221; is for single-line comments.)</para>
<para>The specification for the customer <emphasis role="romanAlt1">C</emphasis> is as follows:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog075.jpg" mime-subtype="jpeg"/></para>
<para>Firstly, <emphasis role="romanAlt1">C</emphasis> provides the ATM <emphasis role="romanAlt1">A</emphasis> with their credentials by sending the <emphasis role="romanAlt1">auth</emphasis> message (line 1). If the authentication fails, the ATM replies with the <emphasis role="romanAlt1">authFail</emphasis> message; in this case the customer also expects their card back and the message <emphasis role="romanAlt1">failedAttempt</emphasis> from the bank (line 14). On successful authentication, <emphasis role="romanAlt1">C</emphasis> can select one of the services offered by the ATM or quit the protocol (lines 3&#x2013;9). In the latter case, <emphasis role="romanAlt1">C</emphasis> receives their card and terminates (line 9). To check their balance, <emphasis role="romanAlt1">C</emphasis> sends the message <emphasis role="romanAlt1">checkBalance</emphasis> to <emphasis role="romanAlt1">A</emphasis> and waits for the result (line 3). If <emphasis role="romanAlt1">C</emphasis> sends <emphasis role="romanAlt1">A</emphasis> the message <emphasis role="romanAlt1">withdraw</emphasis>, then <emphasis role="romanAlt1">C</emphasis> continues to <literal>Cw</literal> (line 6), namely they expects to receive their cash (in which case the protocol restarts) or their card back.</para>
<para>The most complex participant is the ATM <emphasis role="romanAlt1">A</emphasis>. It can be specified as follows:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog076.jpg" mime-subtype="jpeg"/></para>
<para>The structure of participant <emphasis role="romanAlt1">A</emphasis> is very similar to the one of <emphasis role="romanAlt1">C</emphasis> with the addition of the interactions to liaise with the bank. In case <emphasis role="romanAlt1">A</emphasis> receives the request for a service from <emphasis role="romanAlt1">C</emphasis>, it will behave according to <literal>Ac</literal> (for checking the balance) or to <literal>Aw</literal> (for withdrawing money). These behaviours are specified below.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog077.jpg" mime-subtype="jpeg"/></para>
<para>Auxiliary machine <literal>Ac</literal> forwards the <emphasis role="romanAlt1">checkBalance</emphasis> message to <emphasis role="romanAlt1">B</emphasis>, waits for the balance, and returns it to the customer (line 1). Similarly, auxiliary machine <literal>Aw</literal> forwards the request for withdrawal to <emphasis role="romanAlt1">B</emphasis>, and waits for the outcome (lines 3&#x2013;8). If the withdrawal is denied (line 4), then the card is returned to the customer, otherwise the customer receives the money and the protocol restarts (line 8).</para>
<para>Executing <emphasis role="strongAlt">ChorGram</emphasis> on the system</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog078.jpg" mime-subtype="jpeg"/></para>
<para>we verify that the system is GMC and the resulting global graph is reported in <link linkend="fig6_6">Figure <xref linkend="fig6_6" remap="6.6"/></link>, where the overall protocol becomes apparent.</para>
</section>
<section class="lev2" id="sec6-4-2" label="6.4.2" xreflabel="6.4.2">
<title>ATM Service &#x2013; Version 2</title>
<para>The previous specification is GMC, but has several drawbacks, the most evident of which is the fact that when the protocol is repeated the customer has to re-authenticate. We therefore replace the previous participants <emphasis role="romanAlt1">C</emphasis> and <emphasis role="romanAlt1">A</emphasis> with the following ones:</para>
<fig id="fig6_6" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 6.6</label>
<caption><title>Global graph for ATM service v1.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig042.jpg" mime-subtype="jpeg"/>
</fig>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog079.jpg" mime-subtype="jpeg"/></para>
<para>Now, after successful authentication, the customer <emphasis role="romanAlt1">C</emphasis> decides which service to invoke, behaving as specified by <literal>Ca</literal> (lines 7&#x2013;12). Once the request has been served, the customer executes <literal>Cf</literal> deciding whether to quit or ask for a new service (lines 15&#x2013;17). Accordingly, <emphasis role="romanAlt1">A</emphasis> reacts to service requests as per <literal>Aa</literal> on lines 25&#x2013;27 of the above snippet, similarly to the previous version of ATM, but after the completion of each request, <emphasis role="romanAlt1">A</emphasis> behaves as per <literal>Af</literal> on line 37 and returns the card to <emphasis role="romanAlt1">C</emphasis> if a <emphasis role="romanAlt1">quit</emphasis> message is received or repeats from <literal>Aa</literal> when a new service is requested.</para>
<para>The verification of the new version of the system with <emphasis role="strongAlt">ChorGram</emphasis> now highlights some problems as shown by the following output message (slightly manipulated for readability):</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog080.jpg" mime-subtype="jpeg"/></para>
<para>The above message reports that a reachable configuration where participants <emphasis role="romanAlt1">C</emphasis>, <emphasis role="romanAlt1">A</emphasis>, and <emphasis role="romanAlt1">B</emphasis> respectively are in state <literal>qCf</literal>, <literal>qAf</literal>, and <literal>qBa</literal> is a &#x2018;No choice awareness&#x2019; configuration. This configuration is highlighted in yellow in the synchronous transition system, which is reported in <link linkend="fig6_7">Figure <xref linkend="fig6_7" remap="6.7"/></link>. Inspecting the synchronous transition system, we note that this configuration leads to deadlocks (the configurations highlighted in orange in <link linkend="fig6_7">Figure <xref linkend="fig6_7" remap="6.7"/></link>), due to the fact that the participant <emphasis role="romanAlt1">B</emphasis> is not notified when the <emphasis role="romanAlt1">quit</emphasis> branch is taken, i.e., <emphasis role="romanAlt1">B</emphasis> is <emphasis>not aware</emphasis> of which branch of the protocol was chosen by <emphasis role="romanAlt1">C</emphasis>.</para>
<para>Notice that <emphasis role="strongAlt">ChorGram</emphasis> builds a global graph also when the system violates GMC (not shown for space restrictions). Such a synthesised global graph reflects some of their possible communication sound executions while leaving out traces where communication misbehaviour happen. The global graph of our second version of the ATM system can also be used to understand what goes wrong in the overall choreography.</para>
<fig id="fig6_7" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 6.7</label>
<caption><title>Synchronous transition system of ATM service v2.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig043.jpg" mime-subtype="jpeg"/>
</fig>
</section>
<section class="lev2" id="sec6-4-3" label="6.4.3" xreflabel="6.4.3">
<title>ATM Service &#x2013; Version 3 (fixed)</title>
<para>Besides making the refined specification of Section 6.4.2 GMC, in the next version we also want to let the customer quit the protocol immediately after the authentication. This change makes <literal>Cf</literal> and <literal>Af</literal> unnecessary: so, we replace <literal>Ca</literal> and <literal>Cf</literal> with the following new versions:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog081.jpg" mime-subtype="jpeg"/></para>
<para>Note that now <emphasis role="romanAlt1">A</emphasis> notifies <emphasis role="romanAlt1">B</emphasis> when the protocol quits (line 15). This modification requires also to modify the bank, which is now:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog082.jpg" mime-subtype="jpeg"/></para>
<para>The above changes re-establish GMC, hence communication soundness of the system, as verified by <emphasis role="strongAlt">ChorGram</emphasis>, which returns the global graph of <link linkend="fig6_8">Figure <xref linkend="fig6_8" remap="6.8"/></link>.</para>
</section>
</section>
<section class="lev1" id="sec6-5" label="6.5" xreflabel="6.5">
<title>Conclusions and Related Work</title>
<para><emphasis role="strong">Conclusions &#x0026; future work</emphasis> We presented <emphasis role="strongAlt">ChorGram</emphasis>, a tool supporting the analysis and design of choreography-based development. We have discussed only part of the features of <emphasis role="strongAlt">ChorGram</emphasis>, those strictly related to the bottom-up development based on our theory [<link linkend="ch06_b17">17</link>], which is itself an extension of previous work on synthesising global types from local specifications [<link linkend="ch06_b9">9</link>, <link linkend="ch06_b13">13</link>, <link linkend="ch06_b15">15</link>]. Recently, <emphasis role="strongAlt">ChorGram</emphasis> has been extended with new functionalities for top-down development. These new functionalities rely on a new semantic framework [<link linkend="ch06_b11">11</link>]. We are also planning to plug the &#8220;bottomup&#8221; approach advocated here with the classical &#8220;top-down&#8221; approach [<link linkend="ch06_b12">12</link>] as advocated by, e.g., the Scribble specification language [<link linkend="ch06_b22">22</link>, <link linkend="ch06_b25">25</link>]. Such an integration would give the flexibility of designing protocols at the global level and obtain the local level automatically, and vice-versa.</para>
<fig id="fig6_8" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 6.8</label>
<caption><title>Global graph for ATM service v3.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig044.jpg" mime-subtype="jpeg"/>
</fig>
<para>As illustrated in Section 6.4, our approach can be used to give feedback to protocol designers. Hence, we are considering integrating <emphasis role="strongAlt">ChorGram</emphasis> with a framework [<link linkend="ch06_b20">20</link>] allowing programmers to obtain real-time feedback wrt. the multiparty compatibility of the system they are designing. Currently, the prototype highlights communication mismatches at the local level and it is sometimes difficult to identify the real cause of such errors [<link linkend="ch06_b20">20</link>]. However, it appears that a (possibly partial) global graph can help giving precise feedback to the developer so that they can fix the error(s) easily.</para>
<para><emphasis role="strong">Existing extensions and applications</emphasis> Recent work extends the theory underlying <emphasis role="strongAlt">ChorGram</emphasis> [<link linkend="ch06_b17">17</link>] to communicating timed automata (CTA) [<link linkend="ch06_b5">5</link>], i.e., CFSMs which use clocks to constrain when send and receive actions may take place. The authors show that if a system validates some conditions on communication soundness and deadlines, it is possible to construct a choreography with time constraints which is equivalent to the original system of CTAs.</para>
<para>The synthesis of global graphs from local specifications has been applied thus far in two programming languages. A tool to statically detect deadlocks in the Go programming language is available [<link linkend="ch06_b18">18</link>]. The tool first extracts CFSMs from each Go-routine in the source code, then feeds them into a slight variation of <emphasis role="strongAlt">ChorGram</emphasis> (for synchronous semantics) which checks whether the system is multiparty compatible and generates the corresponding global graph (which may be used to track down what may have caused deadlocks). Also, <emphasis role="strongAlt">ChorGram</emphasis> has been used to model and analyse <emphasis role="strongAlt">genserver</emphasis> [<link linkend="ch06_b23">23</link>], a part of the Erlang OTP standard library widely used in the Erlang community for the development of client/server applications. The analysis highlighted possible coordination errors and was conducted following the pattern showed in Section 6.4. The main difference was that the participants and the corresponding CFSMs had to be extracted from the API documentation of <emphasis role="strongAlt">genserver</emphasis>.</para>
<para>An interesting use [<link linkend="ch06_b2">2</link>, <link linkend="ch06_b14">14</link>] of multiparty compatibility is to support an orchestration mechanism based on the <emphasis>agreement</emphasis> of behavioural contracts [<link linkend="ch06_b4">4</link>]. Recently this theoretical framework has been used to develop <emphasis role="strongAlt">Diogenes</emphasis> [<link linkend="ch06_b1">1</link>], a middleware supporting designers (and developers) to write <emphasis>honest</emphasis> programs [<link linkend="ch06_b3">3</link>], namely programs that respect <emphasis>all</emphasis> their contracts in <emphasis>all</emphasis> their execution contexts. An interesting future work is to integrate <emphasis role="strongAlt">Diogenes</emphasis> and <emphasis role="strongAlt">ChorGram</emphasis> in order to adapt components when they are not multiparty compatible. In such cases, (as discussed at the end of Section 6.4.2) <emphasis role="strongAlt">Chor-Gram</emphasis> synthesises a choreography which, although not faithfully reflecting the behaviour of participants, represents some of their possible communication sound executions. Such a synthesised choreography could then be used to obtain projections that help to attain honesty.</para>
<para><emphasis role="strong">Acknowledgements</emphasis> This work is partially supported by EU FP7 612985 (UPSCALE) and by EPSRC EP/K034413/1, EP/K011715/1, EP/L00058X/1, EP/N027833/1 and EP/N028201/1.</para>
</section>
<section class="lev1" id="sec2a">
<title>References</title>
<orderedlist numeration="arabic" continuation="restarts" spacing="normal">
<listitem><para><anchor id="ch06_b1"/>Nicola Atzei and Massimo Bartoletti. Developing honest Java programs with Diogenes. In <emphasis>Formal Techniques for Distributed Objects, Components, and Systems (FORTE)</emphasis>, pages 52&#x2013;61, 2016. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Nicola+Atzei+and+Massimo+Bartoletti%2E+Developing+honest+Java+programs+with+Diogenes%2E+In+Formal+Techniques+for+Distributed+Objects%2C+Components%2C+and+Systems+%28FORTE%29%2C+pages+52-61%2C+2016%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch06_b2"/>Massimo Bartoletti, Julien Lange, Alceste Scalas, and Roberto Zunino. Choreographies in the wild. <emphasis>Sci. Comput. Program.</emphasis>, 109:36&#x2013;60, 2015. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Massimo+Bartoletti%2C+Julien+Lange%2C+Alceste+Scalas%2C+and+Roberto+Zunino%2E+Choreographies+in+the+wild%2E+Sci%2E+Comput%2E+Program%2E%2C+109%3A36-60%2C+2015%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch06_b3"/>Massimo Bartoletti, Alceste Scalas, Emilio Tuosto, and Roberto Zunino. Honesty by typing. In <emphasis>FMOODS/FORTE</emphasis>, volume 7892 of <emphasis>LNCS</emphasis>, pages 305&#x2013;320. Springer, 2013. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Massimo+Bartoletti%2C+Alceste+Scalas%2C+Emilio+Tuosto%2C+and+Roberto+Zunino%2E+Honesty+by+typing%2E+In+FMOODS%2FFORTE%2C+volume+7892+of+LNCS%2C+pages+305-320%2E+Springer%2C+2013%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch06_b4"/>Massimo Bartoletti, Emilio Tuosto, and Roberto Zunino. Contract-oriented computing in CO<subscript>2</subscript>. <emphasis>Scientific Annals in Comp. Sci.</emphasis>, 22(1):5&#x2013;60, 2012. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Massimo+Bartoletti%2C+Emilio+Tuosto%2C+and+Roberto+Zunino%2E+Contract-oriented+computing+in+CO2%2E+Scientific+Annals+in+Comp%2E+Sci%2E%2C+22%281%29%3A5-60%2C+2012%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch06_b5"/>Laura Bocchi, Julien Lange, and Nobuko Yoshida. Meeting deadlines together. In <emphasis>CONCUR 2015</emphasis>, pages 283&#x2013;296, 2015. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Laura+Bocchi%2C+Julien+Lange%2C+and+Nobuko+Yoshida%2E+Meeting+deadlines+together%2E+In+CONCUR+2015%2C+pages+283-296%2C+2015%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch06_b6"/>Filippo Bonchi and Damien Pous. HKC. <literal>http://perso.ens-lyon. fr/damien.pous/hknt/</literal> <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Filippo+Bonchi+and+Damien+Pous%2E+HKC%2E+http%3A%2F%2Fperso%2Eens-lyon%2E+fr%2Fdamien%2Epous%2Fhknt%2F+%5B7%5D+Daniel+Brand+and+Pitro+Zafiropulo%2E+On+communicating+finite-state+machines%2E+JACM%2C+30%282%29%3A323-342%2C+1983%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch06_b7"/>Daniel Brand and Pitro Zafiropulo. On communicating finite-state machines. <emphasis>JACM</emphasis>, 30(2):323&#x2013;342, 1983.</para></listitem>
<listitem><para><anchor id="ch06_b8"/>Jordi Cortadella, Michael Kishinevsky, Alex Kondratyev, Luciano Lavagno, Enric Pastor, and Alexandre Yakovlev. Petrify. <literal>http://www.lsi.upc.edu/jordicf/petrify/</literal></para></listitem>
<listitem><para><anchor id="ch06_b9"/>Pierre-Malo Deni&#x00E9;lou and Nobuko Yoshida. Multiparty compatibility in communicating automata: Characterisation and synthesis of global session types. In <emphasis>ICALP 2013</emphasis>. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Pierre-Malo+Deni%E9lou+and+Nobuko+Yoshida%2E+Multiparty+compatibility+in+communicating+automata%3A+Characterisation+and+synthesis+of+global+session+types%2E+In+ICALP+2013%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch06_b10"/>Pierre-Malo Deni&#x00E9;lou and Nobuko Yoshida. Multiparty session types meet communicating automata. In <emphasis>ESOP 2012</emphasis>, pages 194&#x2013;213, 2012. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Pierre-Malo+Deni%E9lou+and+Nobuko+Yoshida%2E+Multiparty+session+types+meet+communicating+automata%2E+In+ESOP+2012%2C+pages+194-213%2C+2012%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch06_b11"/>Roberto Guanciale and Emilio Tuosto. An Abstract Semantics of the Global View of Choreographies. In <emphasis>ICE 2016</emphasis>, pages 67&#x2013;82, 2016. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Roberto+Guanciale+and+Emilio+Tuosto%2E+An+Abstract+Semantics+of+the+Global+View+of+Choreographies%2E+In+ICE+2016%2C+pages+67-82%2C+2016%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch06_b12"/>Kohei Honda, Nobuko Yoshida, and Marco Carbone. Multiparty asynchronous session types. <emphasis>J. ACM</emphasis>, 63(1):9:1&#x2013;9:67, 2016. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Kohei+Honda%2C+Nobuko+Yoshida%2C+and+Marco+Carbone%2E+Multiparty+asynchronous+session+types%2E+J%2E+ACM%2C+63%281%29%3A9%3A1-9%3A67%2C+2016%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch06_b13"/>Julien Lange. <emphasis>On the Synthesis of Choreographies</emphasis>. PhD thesis, Department of Computer Science, University of Leicester, 2013. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Julien+Lange%2E+On+the+Synthesis+of+Choreographies%2E+PhD+thesis%2C+Department+of+Computer+Science%2C+University+of+Leicester%2C+2013%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch06_b14"/>Julien Lange and Alceste Scalas. Choreography synthesis as contract agreement. In <emphasis>ICE</emphasis>, volume 131 of <emphasis>EPTCS</emphasis>, pages 52&#x2013;67, 2013. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Julien+Lange+and+Alceste+Scalas%2E+Choreography+synthesis+as+contract+agreement%2E+In+ICE%2C+volume+131+of+EPTCS%2C+pages+52-67%2C+2013%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch06_b15"/>Julien Lange and Emilio Tuosto. Synthesising choreographies from local session types. In <emphasis>CONCUR 2012</emphasis>, pages 225&#x2013;239, 2012. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Julien+Lange+and+Emilio+Tuosto%2E+Synthesising+choreographies+from+local+session+types%2E+In+CONCUR+2012%2C+pages+225-239%2C+2012%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch06_b16"/>Julien Lange and Emilio Tuosto. <emphasis role="strongAlt">ChorGram</emphasis>: tool support for choreographic development. Available at <literal>https://bitbucket.org/emlio_ tuosto/chorgram/wiki/Home</literal>, 2015.</para></listitem>
<listitem><para><anchor id="ch06_b17"/>Julien Lange, Emilio Tuosto, and Nobuko Yoshida. From communicating machines to graphical choreographies. In <emphasis>POPL 2015</emphasis>, pages 221&#x2013;232, 2015. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Julien+Lange%2C+Emilio+Tuosto%2C+and+Nobuko+Yoshida%2E+From+communicating+machines+to+graphical+choreographies%2E+In+POPL+2015%2C+pages+221-232%2C+2015%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch06_b18"/>Nicholas Ng and Nobuko Yoshida. Static deadlock detection for concurrent Go by global session graph synthesis. In <emphasis>CC 2016</emphasis>, pages 174&#x2013;184, 2016.</para></listitem>
<listitem><para><anchor id="ch06_b19"/>Object Management Group. Business Process Model and Notation. <literal>http://www.bpmn.org</literal></para></listitem>
<listitem><para><anchor id="ch06_b20"/>Roly Perera, Julien Lange, and Simon J. Gay. Multiparty compatibility for concurrent objects. In <emphasis>PLACES 2016</emphasis>, pages 73&#x2013;82, 2016.</para></listitem>
<listitem><para><anchor id="ch06_b21"/>Chris Piro. Chat stability and scalability. <literal>https://goo.gl/Z1tpgA</literal></para></listitem>
<listitem><para><anchor id="ch06_b22"/>Scribble. <literal>http://www.scribble.org</literal></para></listitem>
<listitem><para><anchor id="ch06_b23"/>Ramsay Taylor, Emilio Tuosto, Neil Walkinshaw, and John Derrick. Choreography-based analysis of distributed message passing programs. In <emphasis>PDP 2016</emphasis>, pages 512&#x2013;519, 2016.</para></listitem>
<listitem><para><anchor id="ch06_b24"/>Paolo D&#x2019;Incau&#x2019;s blog. <literal>https://goo.gl/eXKng1</literal>, 2013.</para></listitem>
<listitem><para><anchor id="ch06_b25"/>Nobuko Yoshida, Raymond Hu, Rumyana Neykova, and Nicholas Ng. The Scribble protocol language. In <emphasis>TGC 2013</emphasis>, pages 22&#x2013;41, 2013. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Nobuko+Yoshida%2C+Raymond+Hu%2C+Rumyana+Neykova%2C+and+Nicholas+Ng%2E+The+Scribble+protocol+language%2E+In+TGC+2013%2C+pages+22-41%2C+2013%2E" target="_blank">Google Scholar</ulink></para></listitem>
</orderedlist>
</section>
</chapter>
<chapter class="chapter" id="ch07" label="7" xreflabel="7">
<title>Programming Adaptive Microservice Applications: An AIOCJ Tutorial<footnote id="fn7_1a" label="*"><para>Supported by the COST Action IC1201 BETTY, by the EU project FP7-644298 <emphasis>HyVar: Scalable Hybrid Variability for Distributed, Evolving Software Systems</emphasis>, by the GNCS group of INdAM via project <emphasis>Logica, Automi e Giochi per Sistemi Auto-adattivi</emphasis>, and by the EU EIT Digital project <emphasis>SMAll</emphasis>.</para></footnote></title>
<para><emphasis role="strong">Saverio Giallorenzo</emphasis><footnote id="fn7_1b" label="1"><para>Focus Team, University of Bologna/INRIA, Italy</para></footnote><emphasis role="strong">, Ivan Lanese</emphasis><footnote id="fn7_2b" label="2"><para>Department of Informatics, University of Oslo, Norway</para></footnote><emphasis role="strong">, Jacopo Mauro</emphasis><footnoteref linkend="fn7_2b" label="2"/> <emphasis role="strong">and Maurizio Gabbrielli</emphasis><footnoteref linkend="fn7_1b" label="1"/></para>
<section class="lev2">
<title>Abstract</title>
<para>This tutorial describes AIOCJ, which stands for <emphasis>Adaptive Interaction Oriented Choreographies in Jolie</emphasis>, a choreographic language for programming microservice-based applications which can be updated at runtime. The compilation of a single AIOCJ program generates the whole set of distributed microservices that compose the application. Adaptation is performed using adaptation rules. Abstractly, each rule replaces a pre-delimited part of the program with the new code contained in the rule itself. Concretely, at runtime, the application of a rule updates part of the microservices that compose the application so to match the behavior specified by the updated program. Thanks to the properties of choreographies, the adaptive application is free from communication deadlocks and message races even after adaptation.</para>
</section>
<section class="lev1" id="sec7-1" label="7.1" xreflabel="7.1">
<title>Introduction</title>
<para>Today, most applications are distributed, involving multiple participants scattered on the network and interacting by exchanging messages. While still widely used, the standard client-server topology has shown some of its limitations and peer-to-peer and other interaction patterns are raising in popularity in many contexts, from social networks to business-to-business, from gaming to public services. Programming the intended behavior of such applications requires to understand how the behavior of the single program of one of their nodes combines with the others, to produce the global behavior of the application. In brief, it requires to master the intricacies of concurrency and distribution. There is clearly a tension between the <emphasis>global</emphasis> desired behavior of a distributed application and the fact that it is programmed by developing <emphasis>local</emphasis> programs. Choreographies [<link linkend="ch07_b1">1</link>&#x2013;<link linkend="ch07_b5">5</link>], and more specifically choreographic programming [<link linkend="ch07_b6">6</link>], aim at solving this tension by providing to developers a programming language where they directly specify the global behavior. A sample choreography that describes the behavior of an application composed of one <literal>client</literal> and one <literal>seller</literal> is:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog083.jpg" mime-subtype="jpeg"/></para>
<para>The execution starts with an action performed by the <literal>client</literal>: an input request to the local user (line 1). The semicolon at the end of the line is a sequential composition operator, hence the user input should complete before execution proceeds to line 2. Then, a communication between the <literal>client</literal> and the <literal>seller</literal> takes place: the <literal>client</literal> sends a message and the <literal>seller</literal> receives it. A more detailed description of the choreographic language used in the example above is presented in Section 7.3.</para>
<para>Following the choreographic programming approach, given a choreography, the local programs that implement the global specification are automatically generated by the language compiler, ready for the deployment in the intended locations and machines. For instance, the compilation of the choreography in the example produces the local codes of both the <literal>client</literal> and the <literal>seller</literal>. The local code of the <literal>client</literal> starts with a user interaction, followed by the sending of a message to the <literal>seller</literal>. The local code of the <literal>seller</literal> has just one action: the reception of a message from the <literal>client</literal>.</para>
<para>The choice of a choreographic language also has the advantage of avoiding by construction common errors performed when developing concurrent and distributed applications [<link linkend="ch07_b7">7</link>]. Notably, these include communication deadlocks, which may cause the application to block, and message races, which may lead to unexpected behaviors in some executions.</para>
<para>Another advantage of the choreographic approach is that it eases the task of adapting a running distributed application. We recall that nowadays applications are often meant to run for a long time and should adapt to changes of the environment, to updates of requirements, and to the variability of business rules. Adapting distributed applications at runtime, that is without stopping and restarting them, and with limited degradation of the quality of service, is a relevant yet difficult to reach goal. In a choreographic setting, one can simply specify how the global behavior is expected to change. This approach leaves to the compiler and the runtime support the burden of concretely updating the code of each local program. This update should be done avoiding misbehaviors while the adaptation is carried out and ensuring a correct post-adaptation behavior.</para>
<para>This tutorial presents AIOCJ<footnote id="fn7_1" label="1"><para>The tutorial refers to version 1.3 of AIOCJ.</para></footnote>, which stands for <emphasis>Adaptive Interaction Oriented Choreographies in Jolie</emphasis>, a framework including <emphasis>i</emphasis>) a choreographic language, AIOC, for programming microservice-based applications which can be dynamically updated at runtime and <emphasis>ii</emphasis>) its runtime environment. The main features of the AIOCJ framework are:</para>
<itemizedlist mark="none" spacing="normal">
<listitem><para><emphasis role="strong">Choreographic approach:</emphasis> the AIOC language allows the programmer to write the behavior of a whole distributed application as a single program;</para></listitem>
<listitem><para><emphasis role="strong">Runtime adaptability:</emphasis> AIOCJ applications can be updated by writing new pieces of code embodied into AIOC <emphasis>adaptation rules</emphasis>. Adaptation rules are dynamically and automatically applied to running AIOCJ applications, providing new features, allowing for new behaviors, and updating underlying business rules.</para></listitem>
<listitem><para><emphasis role="strong">Microservice architecture:</emphasis> AIOCJ applications are implemented as systems of microservices [<link linkend="ch07_b8">8</link>]. Indeed, we found that the microservice architectural style supports the fine-grained distribution and flexibility required by our case. As a consequence, AIOCJ applications can interact using standard application-layer protocols (e.g., SOAP and HTTP) with existing (legacy) software thus also facilitating and supporting the integration of existing systems.</para></listitem>
</itemizedlist>
<para>A more technical account of the AIOCJ framework can be found in the literature, describing both the underlying theory [<link linkend="ch07_b9">9</link>] and the tool itself [<link linkend="ch07_b10">10</link>]. AIOCJ can be downloaded from its website [<link linkend="ch07_b11">11</link>], where additional documentation and examples are available.</para>
</section>
<section class="lev1" id="sec7-2" label="7.2" xreflabel="7.2">
<title>AIOCJ Outline</title>
<para>As described in the Introduction, AIOC is a choreographic language for programming microservice-based applications which can be dynamically updated at runtime. The AIOCJ framework is composed of two parts:</para>
<itemizedlist mark="bullet" spacing="normal">
<listitem><para>the AIOCJ Integrated Development Environment (IDE), provided as an Eclipse plugin, that lets developers write both AIOC programs and the adaptation rules that change the behavior of AIOCJ applications at runtime;</para></listitem>
<listitem><para>the AIOCJ Runtime Environment (RE), which is used to support the execution and the adaptation of AIOCJ applications.</para></listitem>
</itemizedlist>
<para>The AIOCJ IDE (see the screenshot in <link linkend="fig7_1">Figure <xref linkend="fig7_1" remap="7.1"/></link>) offers standard functionalities such as syntax highlighting and syntax checking. However, the most important functionality of the IDE is the support for code compilation. The target language of the AIOCJ compiler is Jolie [<link linkend="ch07_b12">12</link>, <link linkend="ch07_b13">13</link>], the first language natively supporting microservice architectures. A key feature of the Jolie language is its support for a wide range of communication technologies (TCP/IP sockets, Unix domain sockets, Bluetooth) and of protocols (e.g., HTTP, SOAP, JSON-RPC) that makes it extremely useful for system integration. AIOC inherits this ability since it makes the communication capabilities of Jolie available to the AIOC programmer.</para>
<para>Since AIOC is a choreographic language, each AIOC program defines a distributed application. The application is composed of different nodes, each taking a specific <emphasis>role</emphasis> in the choreography. Each role has its own local state, and the roles communicate by exchanging messages. The structure of AIOCJ applications makes the compilation process of AIOCJ peculiar for two main reasons:</para>
<fig id="fig7_1" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 7.1</label>
<caption><title>The AIOCJ IDE.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig045.jpg" mime-subtype="jpeg"/>
</fig>
<itemizedlist mark="bullet" spacing="normal">
<listitem><para>the compilation of a single AIOC program generates one Jolie microser-vice for each role involved in the choreography, instead of a unique executable for the whole application;</para></listitem>
<listitem><para>the compilation may involve either an AIOC program, or a set of AIOC adaptation rules. In particular, the latter may be compiled even after the compilation, deployment, and launch of the AIOC program. Thus AIOC adaptation rules can be devised and programmed while the application is running, and therefore applied to it at runtime.</para></listitem>
</itemizedlist>
<para>Adaptation rules target well-identified parts of AIOC programs. Indeed, an AIOC program may declare some part of its code as adaptable by enclosing it in a <literal>scope</literal> block. Abstractly, the effect of the application of an AIOC adaptation rule to a given <literal>scope</literal> is to replace the <literal>scope</literal> block with new code, contained in the adaptation rule itself. Concretely, when the distributed execution of an AIOC program reaches a <literal>scope</literal>, the AIOCJ RE checks whether there is any adaptation rule applicable to it. If this is the case, then the running system of microservices adapts so to match the behavior specified by the updated choreography. This adaptation involves coordinating the distribution and execution of the local codes corresponding to the global code in the adaptation rule. If instead no rule applies, the execution proceeds as specified by the code within the <literal>scope</literal>.</para>
<para>In the rest of this section we describe the architecture of AIOCJ and the workflow that developers, or better DevOps<footnote id="fn7_2" label="2"><para>DevOps is a portmanteau of &#8220;development&#8221; and &#8220;operations&#8221; used to indicate the professional figure involved in the development, deployment, and operation of the application.</para></footnote>, have to follow in order to compile, deploy, and adapt at runtime an AIOCJ application (a more detailed step-by-step description is in Section 7.6). We instead dedicate Sections 7.3 to 7.5 to the description of the AIOC language.</para>
<section class="lev2" id="sec7-2-1" label="7.2.1" xreflabel="7.2.1">
<title>AIOCJ Architecture and Workflow</title>
<para>The AIOCJ runtime environment comprises a few Jolie microservices that support the execution and adaptation of compiled programs. The main microservices of the AIOCJ runtime environment are:</para>
<itemizedlist mark="bullet" spacing="normal">
<listitem><para><emphasis role="romanAlt">Adaptation Manager</emphasis>, a microservice in charge of managing the adaptation protocol;</para></listitem>
<listitem><para><emphasis role="romanAlt">Adaptation Server</emphasis>, a microservice that contains a set of adaptation rules;</para></listitem>
<listitem><para><emphasis role="romanAlt">Environment</emphasis>, a microservice used to store values of global properties related to the execution environment. These properties may be used to check whether adaptation rules are applicable or not.</para></listitem>
</itemizedlist>
<para>More precisely, a runtime environment includes one <emphasis role="romanAlt">Adaptation Manager</emphasis>, zero or more <emphasis role="romanAlt">Adaptation Servers</emphasis>, each of them enabling a set of adaptation rules, and, if needed to evaluate the applicability conditions of the rules, one <emphasis role="romanAlt">Environment</emphasis> microservice. Adaptation Servers can be added or removed dynamically, thus enabling dynamic changes in the set of rules.</para>
<para>Microservices compiled from AIOC code interact both among themselves, as specified by the choreography, and with the <emphasis role="romanAlt">Adaptation Manager</emphasis>, to carry out adaptation. Indeed, when a <literal>scope</literal> is about to be executed, the <emphasis role="romanAlt">Adaptation Manager</emphasis> is invoked to check whether the <literal>scope</literal> can be executed as it is, or if it must be replaced by the code provided by some adaptation rule, made available by an active <emphasis role="romanAlt">Adaptation Server</emphasis>. In fact, when started, an <emphasis role="romanAlt">Adaptation Server</emphasis> registers itself at the <emphasis role="romanAlt">Adaptation Manager</emphasis>. The <emphasis role="romanAlt">Adaptation Manager</emphasis> invokes the registered <emphasis role="romanAlt">Adaptation Server</emphasis>s to check whether their adaptation rules are applicable. In order to check applicability, the corresponding <emphasis role="romanAlt">Adaptation Server</emphasis> evaluates the <emphasis>applicability condition</emphasis> of the rule, possibly interacting with the <emphasis role="romanAlt">Environment</emphasis> microservice. The first applicable adaptation rule, if any, is used to replace the code of the <literal>scope</literal>.</para>
<para>Let us consider an example. Take a simple choreography in AIOC involving two roles, <literal>client</literal> and <literal>seller</literal>. <link linkend="fig7_2">Figure <xref linkend="fig7_2" remap="7.2"/></link> depicts the process of compilation <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/07_equ01.jpg" mime-subtype="jpeg"/> and execution <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/07_equ02.jpg" mime-subtype="jpeg"/> of the AIOC. From left to right, we use the IDE to write the AIOC and to compile it into a set of executable Jolie microservices (<emphasis role="romanAlt">Client</emphasis> and <emphasis role="romanAlt">Seller</emphasis>). To execute the generated application, we first launch the <emphasis role="romanAlt">Adaptation Manager</emphasis> and then the two compiled microservices.</para>
<fig id="fig7_2" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 7.2</label>
<caption><title>The AIOCJ framework &#x2014; deployment and execution of a choreography.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig046.jpg" mime-subtype="jpeg"/>
</fig>
<para>Now, let us suppose that we want to adapt our application. Assuming that the choreography has at least one <literal>scope</literal>, we only need to write and introduce into the system a new set of adaptation rules. <link linkend="fig7_2">Figure <xref linkend="fig7_2" remap="7.2"/></link> depicts the needed steps. From right to left, we write the rules (outlined with dashes) and we compile them using the IDE <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/07_equ03.jpg" mime-subtype="jpeg"/>. The compilation of a set of adaptation rules in AIOCJ produces a single <emphasis role="romanAlt">Adaptation Server</emphasis> (also outlined with dashes). After the compilation, the generated <emphasis role="romanAlt">Adaptation Server</emphasis> is deployed and started, and it registers itself at the <emphasis role="romanAlt">Adaptation Manager</emphasis>. If environmental information is needed to evaluate the applicability condition of the rule, then the DevOps has also to deploy the <emphasis role="romanAlt">Environment</emphasis> microservice. From now on, until the <emphasis role="romanAlt">Adaptation Server</emphasis> is shut down, the rules it contains are active and can be applied to the application. Actual adaptation happens when a <literal>scope</literal> is about to execute, and the applicability condition of the rule for the current <literal>scope</literal> is satisfied. This adaptation is performed automatically and it is completely transparent to the user, except for possible differences in the visible behavior of the new code w.r.t. the original one.</para>
</section>
</section>
<section class="lev1" id="sec7-3" label="7.3" xreflabel="7.3">
<title>Choreographic Programming</title>
<para>The main idea of choreographic programming is that a unique program describes the behavior of a whole distributed application. The main construct of such a program are <emphasis>interactions</emphasis>, such as:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog084.jpg" mime-subtype="jpeg"/></para>
<para>This interaction specifies that role <literal>client</literal> sends a message to role <literal>seller</literal> on operation <literal>quote</literal>. The value of the message is given by the evaluation of expression <literal>product_name</literal> (here just a variable name) in the local state of role <literal>client</literal>. The message will be stored by the <literal>seller</literal> in its local variable <literal>sel_product</literal>. An interaction involves two roles of the choreography, but other choreography constructs involve just one role. For instance, an assignment like <literal>continue@client = "y"</literal>, means that the string <literal>"y"</literal> is assigned to the variable <literal>continue</literal> of role <literal>client</literal>, as specified by the <literal>@</literal> operator.</para>
<para>Let us now detail a simple AIOC program implementing a <literal>client</literal>/<literal>seller</literal> interaction featuring a payment via a <literal>bank</literal> (see <link linkend="list7_1">Listing <xref linkend="list7_1" remap="7.1"/></link>). We will use this program as running example throughout the tutorial. Lines 1&#x2013;6 form the <literal>preamble</literal>, which specifies some deployment information:</para>
<itemizedlist mark="bullet" spacing="normal">
<listitem><para>line 2 declares the <literal>starter</literal> of the choreography, i.e., the first role that needs to be started and the one that coordinates the start of the application by waiting for the other roles to join;</para>
<fig id="lis7_1">
<label>Listing 7.1</label>
<caption><title>Running example: basic choreography.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog085.jpg" mime-subtype="jpeg"/>
</fig></listitem>
<listitem><para>lines 3&#x2013;5 specify how the roles participating to the choreography can be reached. In this case, all the three roles communicate using TCP/IP sockets, as specified by the <literal>"socket://"</literal> prefix of the URI.</para></listitem>
</itemizedlist>
<para>The actual code is introduced by the keyword <literal>aioc</literal>. After the local assignment at line 9, line 10 introduces a while loop. The <literal>@client</literal> suffix specifies that the guard is evaluated by the <literal>client</literal> in its local state. Notice that the decision about whether to enter the loop or not is taken by the <literal>client</literal> but it impacts also other roles. These roles are notified of the choice by auxiliary communications which are automatically generated. The assignment at line 9 and the while loop starting at line 10 are composed using a semicolon, which represents sequential composition. Line 11 is again an assignment, where built-in function <literal>getInput</literal> is used to interact with the local user. The function creates a window showing the string in parameter and returns the input of the user. Line 12 is an interaction between the client and the seller. The next interesting construct is at line 14, featuring a conditional. As for while loops, the conditional specifies which role is in charge of evaluating the guard, and other roles are automatically notified of the outcome of the evaluation. Function <literal>show</literal> (line 26) is a built-in function like <literal>getInput</literal>, simply showing a message.</para>
<para>Abstracting from the technical details, the choreography specifies that the <literal>client</literal> asks the quote for a product (line 12), and then decides whether to buy it or not (line 19). In the first case, the <literal>client</literal> asks the <literal>bank</literal> to perform the payment (line 20). If the payment is authorized (line 23), then the money is sent to the <literal>seller</literal> (line 24), which delivers the product to the <literal>client</literal> (line 25). At the end of the interaction, the <literal>client</literal> may decide to buy a new product or to stop (line 34).</para>
<para>When writing AIOC programs, beyond the usual syntactic errors, one should pay attention to a semantic error peculiar of choreographic programming. Indeed, a semicolon specifies that the code before the semicolon should be executed before the code after the semicolon. However, since there is no central control, such a constraint can only be enforced if for each pair of statements <literal>S</literal> and <literal>T</literal> such that <literal>S</literal> is just before the semicolon and <literal>T</literal> is just after the semicolon, there is a role occurring in both <literal>S</literal> and <literal>T</literal>. This property is called connectedness [<link linkend="ch07_b9">9</link>] and it is needed to enforce the sequentiality of the actions. When connectedness does not hold, AIOCJ IDE alerts the user by showing the error &#8220;The sequence is not connected&#8221;. Instead of asking the programmer to satisfy connectedness, one could extend AIOCJ to automatically insert auxiliary communications to ensure connectedness, similarly to what is done for while loops and conditionals. Such an extension is left as future work.</para>
</section>
<section class="lev1" id="sec7-4" label="7.4" xreflabel="7.4">
<title>Integration with Legacy Software</title>
<para>The example in the previous section shows how one can program a distributed application in AIOCJ. However, such a distributed application is closed: there is no interaction between the application and the outside world, except for basic visual interactions with the users of the application. As we will see below, AIOCJ applications are not necessarily closed systems. Indeed, AIOCJ provides a strong support to integration with legacy software. We already cited that AIOCJ is based on the microservice technology. As such, it supports interaction with external services via standard application-layer protocols, such as SOAP and HTTP. Such services are seen as functions inside AIOC programs, and can be invoked and used inside expressions.</para>
<para>Let us see how this can be done by refining our running example from <link linkend="list7_1">Listing <xref linkend="list7_1" remap="7.1"/></link> into the one in <link linkend="list7_2">Listing <xref linkend="list7_2" remap="7.2"/></link>.</para>
<fig id="lis7_2">
<label>Listing 7.2</label>
<caption><title>Running example: integration with external services.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog086.jpg" mime-subtype="jpeg"/>
</fig>
<para>In <link linkend="list7_2">Listing <xref linkend="list7_2" remap="7.2"/></link>, lines 1 and 2 declare two external services, <literal>quoteProduct</literal> invoked using SOAP and <literal>makePayment</literal> invoked using HTTP (more precisely, a POST request carrying XML data). Both external services communicate with AIOCJ using TCP/IP sockets. The first service is invoked at line 16 by the <literal>seller</literal> and it is used to check the price of a given product. In principle, such a service can be either publicly available or a private service of the <literal>seller</literal>. Here, we assume that this service gives access to the <literal>seller</literal> IT system, e.g., to the database storing prices of the available products. The second service is invoked at line 24 by the <literal>bank</literal>, and gives access to the <literal>bank</literal> IT system. One can easily imagine to make the example more realistic by adding other external services taking care, e.g., of shipping the product.</para>
<para>We now discuss in more detail how function arguments are encoded for service invocation and how the result is sent back to the caller. In general AIOCJ functions can have an arbitrary number of parameters, separated by commas. The parameters are embedded in a tree structure which is then encoded according to the chosen application-layer data protocol. The tree structure has an empty root with a number of children all named <literal>p</literal> (for parameter) carrying the parameters of the invocation, in the order in which they are specified. The return value instead has basic type (such as string, integer, double) and it is contained in the root of the response message.</para>
<para>For instance, consider a sample function <literal>myFunction</literal>, with three parameters, a string, an integer, and a double. If the data protocol for <literal>myFunction</literal> is SOAP, then the AIOCJ application would send a SOAP message as reported in <link linkend="list7_3">Listing <xref linkend="list7_3" remap="7.3"/></link>. A possible reply to the message above is a SOAP message of the form reported in <link linkend="list7_4">Listing <xref linkend="list7_4" remap="7.4"/></link>.</para>
<fig id="lis7_3">
<label>Listing 7.3</label>
<caption><title>Function invocation: SOAP message request.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog087.jpg" mime-subtype="jpeg"/>
</fig>
<fig id="lis7_4">
<label>Listing 7.4</label>
<caption><title>Function invocation: SOAP message response.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog088.jpg" mime-subtype="jpeg"/>
</fig>
<para>Other application-layer data protocols would produce similar structures. Currently, AIOCJ supports SOAP, HTTP, SODEP (i.e., Jolie&#x2019;s binary data protocol), JSON/RPC, and XML/RPC. As far as the communication medium is concerned, AIOCJ supports other options beyond TCP/IP sockets, namely Bluetooth with URIs of the form <literal>"btl2cap://0050CD00321B:101"</literal> and Unix domain sockets with URIs of the form <literal>"localsocket://var/comm/socket"</literal>. The choice of the communication medium and the choice of the application-layer data protocols are orthogonal.</para>
</section>
<section class="lev1" id="sec7-5" label="7.5" xreflabel="7.5">
<title>Adaptation</title>
<para>We now come to the main feature of AIOCJ, namely the support for adaptation. Adaptation is performed in two stages:</para>
<orderedlist numeration="arabic" continuation="restarts" spacing="normal">
<listitem><para>when writing the original AIOC program, one should foresee which parts of the code could be adapted in the future, and enclose them into <literal>scope</literal>s;</para></listitem>
<listitem><para>while the AIOC program is running, one should write adaptation rules to introduce the desired new behavior.</para></listitem>
</orderedlist>
<para>We introduce in <link linkend="list7_5">Listing <xref linkend="list7_5" remap="7.5"/></link> three <literal>scope</literal>s to show how adaptation can be enabled in the running example in <link linkend="list7_2">Listing <xref linkend="list7_2" remap="7.2"/></link>.</para>
<para>Scope <literal>transaction-execution</literal> at lines 26&#x2013;29 encloses the body of the business transaction, with the idea that this can be changed to support integration with a shipper service, or more refined payment protocols. Then, we have two <literal>scope</literal>s, <literal>success-notification</literal> (lines 30&#x2013;32) and <literal>failure-notification</literal> (lines 34&#x2013;36), which are in charge of notifying the <literal>client</literal> of the outcome of the transaction, with the idea that different forms of notification, e.g., through e-mail or SMS, could be implemented in the future. Developers can equip <literal>scope</literal>s with properties describing their nature and characteristics. These properties can be used to decide whether a given rule should apply to a given <literal>scope</literal> or not. In the example, we just use a property <literal>scopename</literal> to describe each <literal>scope</literal>. In general, however, many properties can be used. For example, if some <literal>scope</literal> encloses a part of the code which is critical for security reasons, one of its properties could declare the security level of the current code. Such a declaration is under the responsibility of the programmer and it is in no way checked or enforced by the AIOCJ framework.</para>
<fig id="lis7_5">
<label>Listing 7.5</label>
<caption><title>Running example: enabling adaptation.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog089.jpg" mime-subtype="jpeg"/>
</fig>
<para>Note that each <literal>scope</literal> is followed by an annotation <literal><emphasis role="pd_pink">@</emphasis></literal><emphasis>role</emphasis> that declares the coordinator of the adaptation procedure of the <literal>scope</literal>. The coordinator is in charge of invoking the <emphasis role="romanAlt">Adaptation Manager</emphasis>, which handles the selection of an applicable adaptation rule. The <emphasis role="romanAlt">Adaptation Manager</emphasis> can access the internal state of the coordinator to check whether an adaptation rule is applicable or not. The coordinator is also in charge of fetching the local codes compiled from the selected adaptation rule and of distributing them to the other roles.</para>
<para><emphasis role="strong">Remark 1</emphasis> <emphasis>We highlight that there is no precise convention on how to place</emphasis> <literal>scope</literal><emphasis>s: one should try to foresee which parts of the AIOC program are likely to change. As a rule of thumb, parts which are critical for security or performance reasons may be updated to improve the security level or the performance of the application. Parts which instead implement business rules may need to be updated to change the business rules. Finally, parts that manage interactions with external services may need to be updated to match changes in the external services. There is also a trade-off involved in the definition of</emphasis> <literal>scope</literal><emphasis>s. On the one hand, large</emphasis> <literal>scope</literal><emphasis>s are rarely useful, since they could be updated only before the beginning of their execution, which can be quite early in the life of the application. On the other hand, small</emphasis> <literal>scope</literal><emphasis>s may be problematic, since a meaningful update may involve many of them and currently AIOCJ does not provide a way to synchronize when and how</emphasis> <literal>scope</literal><emphasis>s are updated.</emphasis></para>
<para>Now that the application in <link linkend="list7_5">Listing <xref linkend="list7_5" remap="7.5"/></link> is equipped with scopes, it is ready to be deployed, and offers built-in support for adaptation. While the application is running, a new need may emerge. Assume for instance that the application, meant for trading cheap products, needs to be used also for more expensive ones. In this previously unforeseen setting, the fact that the payment is performed in a single installment and before the shipping of the product may be undesirable for the <literal>Client</literal>. One can meet this new need by providing an adaptation rule (see <link linkend="list7_6">Listing <xref linkend="list7_6" remap="7.6"/></link>) where the payment is performed in two installments, each consisting in half of the original <literal>amount</literal>: one sent before and the other after the delivery of the product. This rule targets <literal>scope</literal>s with property <literal>scopename</literal> equal to <literal>transaction-execution</literal> and it applies only if the <literal>price</literal> of the product is above a <literal>split_payment_threshold</literal> available in the <emphasis role="romanAlt">Environment</emphasis> microservice. The idea is that such a threshold may be agreed upon by the <literal>client</literal> and the <literal>seller</literal> or established by some business regulation. We remark that properties of the <literal>scope</literal>, like <literal>N.scopename</literal>, are prefixed by <literal>N</literal> while values provided by the <emphasis role="romanAlt">Environment</emphasis> microservice, like <literal>E.split_payment_threshold</literal>,are prefixed by <literal>E</literal>. Names with no prefix refer to variables of the role that coordinates the adaptation of the <literal>scope</literal>, such as <literal>price</literal> in this example.</para>
<fig id="lis7_6">
<label>Listing 7.6</label>
<caption><title>Adaptation rule: split payment.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog090.jpg" mime-subtype="jpeg"/>
</fig>
<para>We note that the above adaptation rule changes the choreography and, as a consequence, the behavior of two of its roles. In general, an adaptation rule can impact an arbitrary number of roles. We also note that the need for adaptation is checked &#x2014; and adaptation is possibly performed &#x2014; every time the <literal>scope</literal> is executed. In this example, if the <literal>client</literal> buys many products, some with price above the threshold and some below, the need for adaptation is checked for each item and adaptation is performed only for the ones with a price above the threshold. In essence, purchases of cheap products follow the basic protocol whilst purchases of expensive ones follow the refined protocol introduced by the adaptation rule.</para>
<para>We now consider another need that may emerge. Assume that the <literal>seller</literal> decides to log all its sales, e.g., for tax payment reasons. Again, one may write an adaptation rule (see <link linkend="list7_7">Listing <xref linkend="list7_7" remap="7.7"/></link>) to answer this need. This rule targets the <literal>scope</literal> with property <literal>N.scopename = "success-notification"</literal> (lines 30&#x2013;32 in <link linkend="list7_5">Listing <xref linkend="list7_5" remap="7.5"/></link>), which was not exactly intended for logging, but can be adapted to do so by taking care of repeating in the new code also the original notification message (line 31 in <link linkend="list7_5">Listing <xref linkend="list7_5" remap="7.5"/></link>, repeated at line 8 in <link linkend="list7_7">Listing <xref linkend="list7_7" remap="7.7"/></link>). The rule exploits both a new role, <literal>logger</literal>, and two external services <literal>log</literal> and <literal>getTime</literal>. External services are declared exactly as in AIOC programs. Note that here we omit the application-layer protocol of both services, hence the default, SOAP, is used.</para>
<fig id="lis7_7">
<label>Listing 7.7</label>
<caption><title>Adaptation rule: logging.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog091.jpg" mime-subtype="jpeg"/>
</fig>
<para>The additional role is declared using keyword <literal>newRoles</literal> (line 4). New roles in AIOCJ rules should not be involved in the target AIOC program and take part to the choreography only while the body of the rule executes. As for normal roles, the URI of new roles is declared using the keyword <literal>location</literal>.</para>
</section>
<section class="lev1" id="sec7-6" label="7.6" xreflabel="7.6">
<title>Deployment and Adaptation Procedure</title>
<para>In this section we describe the steps that DevOps need to follow to deploy the AIOCJ application of <link linkend="list7_5">Listing <xref linkend="list7_5" remap="7.5"/></link> and to adapt it at runtime. When reporting paths, we use the Unix forward slash notation.</para>
<para><emphasis role="strong">Compiling and Running an AIOC.</emphasis> As already mentioned, AIOCJ IDE runs as an Eclipse plugin. Hence, to create a new AIOC program we create a new project and a new file inside it with <literal>.ioc</literal> extension. We write the code in <link linkend="list7_5">Listing <xref linkend="list7_5" remap="7.5"/></link> and we compile it by clicking on the button &#8220;Jolie Endpoint Projection&#8221; <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/07_equ04.jpg" mime-subtype="jpeg"/>. The compilation creates three folders in the Eclipse project: <literal>epp_aioc</literal>, <literal>adaptation_manager</literal>, and <literal>environment</literal>.</para>
<para>Within the folder <literal>epp_aioc</literal> we can find one subfolder for each role in the AIOC program containing all the related code. The main file is named after the role and has the standard Jolie extension <literal>.ol</literal>. The subfolder needs to be moved in the host corresponding to the location of the role declared in the <literal>preamble</literal> of the AIOC program. For example, the subfolder <literal>client</literal> should be moved into the host located at <literal>"client.com"</literal>.</para>
<para>Within the folders <literal>adaptation_manager</literal> and <literal>environment</literal> the main files are, respectively, <literal>main_adaptationManager.ol</literal> and <literal>environment.ol</literal>.</para>
<para>Before starting the compiled AIOC program, we make sure that the external services included in the choreography are running. To run the AIOC program, we first launch the <emphasis role="romanAlt">Adaptation Manager</emphasis> with</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/07_equ05.jpg" mime-subtype="jpeg"/></para>
<para>Then, we run the roles in the choreography, beginning from the <literal>client</literal>, which is declared as the <literal>starter</literal> of the choreography. For instance, the <literal>client</literal> &#x2014; previously deployed at <literal>"client.com"</literal> &#x2014; can be launched with</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/07_equ06.jpg" mime-subtype="jpeg"/></para>
<para>At the moment there is no need to run the <emphasis role="romanAlt">Environment</emphasis>. As soon as the last role is started, the execution of the AIOCJ application begins.</para>
<para><emphasis role="strong">Adapting a Running AIOC.</emphasis> Adaptation rules are defined using the same Eclipse plugin as AIOC programs. They need to be stored in a new <literal>.ioc</literal> file, either in the same project as the AIOC program or in a new one.</para>
<para>As for AIOC programs, the compilation of a set of adaptation rules is triggered by the &#8220;Jolie Endpoint Projection&#8221; button <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/07_equ04.jpg" mime-subtype="jpeg"/> and produces a folder named <literal>epp_rules</literal>, which corresponds to a unique <emphasis role="romanAlt">Adaptation Server</emphasis>. Inside the folder, the main file is <literal>AdaptationServer.ol</literal> within path</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/07_equ07.jpg" mime-subtype="jpeg"/></para>
<para>Also in this case, before starting the <emphasis role="romanAlt">Adaptation Server</emphasis>, we make sure that the external services included in the rules are running.</para>
<para>If some adaptation rule has an applicability condition that checks some <emphasis role="romanAlt">Environment</emphasis> variables (e.g., variable <literal>E.split_payment_threshold</literal> in <link linkend="list7_6">Listing <xref linkend="list7_6" remap="7.6"/></link>, line 3), the <emphasis role="romanAlt">Environment</emphasis> microservice needs to be launched, running the program <literal>environment.ol</literal>. <emphasis role="romanAlt">Environment</emphasis> variables can be added and removed both by console interaction or by editing the configuration file <literal>environmentVariables.xml</literal>.</para>
<para>If some adaptation rule needs a new role, the <literal>location</literal> declared for it should be able to interact with the <emphasis role="romanAlt">Adaptation Server</emphasis> that contains the rule. To this end, AIOCJ provides a dedicated microservice called <emphasis role="romanAlt">Role Supporter</emphasis>, which needs to be deployed in the host corresponding to the target <literal>location</literal>. This is done by moving to the corresponding host the folder</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/07_equ08.jpg" mime-subtype="jpeg"/></para>
<para>where <emphasis>N</emphasis> is the sequential number of the rule, from top to bottom, inside the file <literal>.ioc</literal>, and <emphasis>roleName</emphasis> is the name of the new role. The folder contains the code of the utility microservice, <literal>RoleSupporter.ol</literal>, and an automatically generated configuration file <literal>config/location.iol</literal>. For instance, the configuration file for the <emphasis role="romanAlt">RoleSupporter</emphasis> for role <literal>logger</literal> in the rule in <link linkend="list7_7">Listing <xref linkend="list7_7" remap="7.7"/></link> (assuming it is the only rule in the <literal>.ioc</literal> file) is</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/07_equ09.jpg" mime-subtype="jpeg"/></para>
<para>If the location of the new role is unspecified, then <literal>"localhost:9059"</literal> is used by default and the corresponding folder is <literal>default_role_supporter</literal>.</para>
<para>Once both the external services and the <emphasis role="romanAlt">Role Supporter</emphasis>s are running, we can launch the <emphasis role="romanAlt">Adaptation Server</emphasis>. When launched, the <emphasis role="romanAlt">Adaptation Server</emphasis> registers at the <emphasis role="romanAlt">Adaptation Manager</emphasis> and the compiled adaptation rules become enabled. From now on, when a <literal>scope</literal> is reached during execution, the rules in the <emphasis role="romanAlt">Adaptation Server</emphasis> are checked for applicability.</para>
<para>Both microservices implementing roles of AIOCJ applications and the ones in AIOCJ RE &#x2014; namely the <emphasis role="romanAlt">Adaptation Manager</emphasis>, the <emphasis role="romanAlt">Environment</emphasis>, the <emphasis role="romanAlt">Adaptation Server</emphasis>s, and the <emphasis role="romanAlt">Role Supporter</emphasis>s &#x2014; can be re-deployed on hosts different from the default ones. This requires to move the corresponding folder, but also to update the configuration files that contain their addresses, including their own configuration file. Notably, no recompilation is needed. We report in <link linkend="fig7_3">Figure <xref linkend="fig7_3" remap="7.3"/></link> the dependency graph among the locations of AIOCJ microservices. In the figure, the notation <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/07_equ10.jpg" mime-subtype="jpeg"/> means that microservice <emphasis role="strongAlt">A</emphasis> must know the deployment location of microservice <emphasis role="strongAlt">B</emphasis>. At the bottom of each box we report the path to the corresponding configuration file for locations, which is <literal>config/locations.iol</literal> except for the deployment location of <emphasis role="romanAlt">Adaptation Server</emphasis>s which is directly contained in their own main file.</para>
<fig id="fig7_3" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 7.3</label>
<caption><title>Location dependency graph among AIOCJ microservices.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig047.jpg" mime-subtype="jpeg"/>
</fig>
</section>
<section class="lev1" id="sec7-7" label="7.7" xreflabel="7.7">
<title>Conclusion</title>
<para>In this tutorial we have given a gentle introduction to the AIOCJ framework and to the AIOC language. While both adaptation and choreographies are thoroughly studied in the literature, their combination has not yet been explored in depth. As far as we know, AIOCJ is the only implemented framework in this setting. Theoretical investigations of the interplay between adaptation and multiparty session types [<link linkend="ch07_b14">14</link>&#x2013;<link linkend="ch07_b16">16</link>] (which use choreographies as types instead of as a language) have been undertaken. A relevant work considers self-adaptive systems [<link linkend="ch07_b14">14</link>]. It uses multiparty session types to monitor that the computation follows the expected patterns, and it performs adaptation by moving from one choreography to the other according to external conditions. However, all possible behaviors are present in the system since the very beginning. Another work studies how to update a system so to preserve the guarantees provided by multiparty session types [<link linkend="ch07_b15">15</link>]. Another study, still preliminary, describes multiparty session types that can be updated from both inside and outside the system [<link linkend="ch07_b16">16</link>]. None of the three proposals above has been implemented. On the other side, we find two implemented approaches for programming using choreographies, Scribble [<link linkend="ch07_b4">4</link>, <link linkend="ch07_b17">17</link>] and Chor [<link linkend="ch07_b2">2</link>], but they do not support adaptation. Chor is particularly related to AIOCJ, since they both produce Jolie code and they share part of the codebase. Finally, the main standard in the field of choreographic specifications, WS-CDL [<link linkend="ch07_b5">5</link>], does not support adaptation. Moreover, WS-CDL is just a specification language and not an executable one. Further information on choreographies can be found in two surveys. One presents a general description of the theory of choreographies and session types [<link linkend="ch07_b18">18</link>]. The other accounts for their use in programming languages [<link linkend="ch07_b19">19</link>].</para>
<para>As future work we would like to understand what is needed to make AIOCJ more usable in practice. To this end, we are experimenting by applying AIOCJ to case studies developed for other approaches to adaptation, such as Context-Oriented Programming [<link linkend="ch07_b20">20</link>] and distributed [<link linkend="ch07_b21">21</link>] and dynamic [<link linkend="ch07_b22">22</link>] Aspect-Oriented Programming. Initial results in this direction can be found on the AIOCJ website [<link linkend="ch07_b11">11</link>]. Another direction is to provide automated support for the deployment of AIOCJ applications using containerization technologies such as Docker [<link linkend="ch07_b23">23</link>].</para>
</section>
<section class="lev1" id="sec2a">
<title>References</title>
<orderedlist numeration="arabic" continuation="restarts" spacing="normal">
<listitem><para><anchor id="ch07_b1"/>M. Carbone, K. Honda, and N. Yoshida, &#8220;Structured communication-centered programming for web services,&#8221; <emphasis>ACM Trans. Program. Lang. Syst.</emphasis>, vol. 34, no. 2, 2012. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=M%2E+Carbone%2C+K%2E+Honda%2C+and+N%2E+Yoshida%2C+%22Structured+communication-centered+programming+for+web+services%2C%22+ACM+Trans%2E+Program%2E+Lang%2E+Syst%2E%2C+vol%2E+34%2C+no%2E+2%2C+2012%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch07_b2"/>M. Carbone and F. Montesi, &#8220;Deadlock-Freedom-by-Design: Multiparty Asynchronous Global Programming,&#8221; in <emphasis>POPL</emphasis>, pp. 263&#x2013;274, ACM, 2013. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=M%2E+Carbone+and+F%2E+Montesi%2C+%22Deadlock-Freedom-by-Design%3A+Multiparty+Asynchronous+Global+Programming%2C%22+in+POPL%2C+pp%2E+263-274%2C+ACM%2C+2013%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch07_b3"/>I. Lanese, C. Guidi, F. Montesi, and G. Zavattaro, &#8220;Bridging the Gap between Interaction- and Process-Oriented Choreographies,&#8221; in <emphasis>SEFM</emphasis>, pp. 323&#x2013;332, IEEE, 2008. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=I%2E+Lanese%2C+C%2E+Guidi%2C+F%2E+Montesi%2C+and+G%2E+Zavattaro%2C+%22Bridging+the+Gap+between+Interaction-+and+Process-Oriented+Choreographies%2C%22+in+SEFM%2C+pp%2E+323-332%2C+IEEE%2C+2008%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch07_b4"/>K. Honda, A. Mukhamedov, G. Brown, T. Chen, and N. Yoshida, &#8220;Scribbling interactions with a formal foundation,&#8221; in <emphasis>ICDCIT</emphasis>, vol. 6536 of <emphasis>LNCS</emphasis>, pp. 55&#x2013;75, Springer, 2011. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=K%2E+Honda%2C+A%2E+Mukhamedov%2C+G%2E+Brown%2C+T%2E+Chen%2C+and+N%2E+Yoshida%2C+%22Scribbling+interactions+with+a+formal+foundation%2C%22+in+ICDCIT%2C+vol%2E+6536+of+LNCS%2C+pp%2E+55-75%2C+Springer%2C+2011%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch07_b5"/>World Wide Web Consortium, <emphasis>Web Services Choreography Description Language Version 1.0</emphasis>, 2005. <literal>http://www.w3.org/TR/wscdl-10/</literal> <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=World+Wide+Web+Consortium%2C+Web+Services+Choreography+Description+Language+Version+1%2E0%2C+2005%2E+http%3A%2F%2Fwww%2Ew3%2Eorg%2FTR%2Fwscdl-10%2F" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch07_b6"/>F. Montesi, &#8220;Kickstarting choreographic programming,&#8221; in <emphasis>WSFM:FASOCC</emphasis>, vol. 9421 of <emphasis>LNCS</emphasis>, pp. 3&#x2013;10, Springer, 2014.</para></listitem>
<listitem><para><anchor id="ch07_b7"/>S. Lu, S. Park, E. Seo, and Y. Zhou, &#8220;Learning from mistakes: a comprehensive study on real world concurrency bug characteristics,&#8221; in <emphasis>ASPLOS</emphasis>, pp. 329&#x2013;339, ACM, 2008. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=S%2E+Lu%2C+S%2E+Park%2C+E%2E+Seo%2C+and+Y%2E+Zhou%2C+%22Learning+from+mistakes%3A+a+comprehensive+study+on+real+world+concurrency+bug+characteristics%2C%22+in+ASPLOS%2C+pp%2E+329-339%2C+ACM%2C+2008%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch07_b8"/>S. Newman, <emphasis>Building Microservices</emphasis>. " O&#x2019;Reilly Media, Inc.", 2015.</para></listitem>
<listitem><para><anchor id="ch07_b9"/>M. Dalla Preda, M. Gabbrielli, S. Giallorenzo, I. Lanese, and J. Mauro, &#8220;Dynamic choreographies - safe runtime updates of distributed applications,&#8221; in <emphasis>COORDINATION</emphasis>, vol. 9037 of <emphasis>LNCS</emphasis>, pp. 67&#x2013;82, Springer, 2015. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=M%2E+Dalla+Preda%2C+M%2E+Gabbrielli%2C+S%2E+Giallorenzo%2C+I%2E+Lanese%2C+and+J%2E+Mauro%2C+%22Dynamic+choreographies+-+safe+runtime+updates+of+distributed+applications%2C%22+in+COORDINATION%2C+vol%2E+9037+of+LNCS%2C+pp%2E+67-82%2C+Springer%2C+2015%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch07_b10"/>M. Dalla Preda, S. Giallorenzo, I. Lanese, J. Mauro, and M. Gabbrielli, &#8220;AIOCJ: A choreographic framework for safe adaptive distributed applications,&#8221; in <emphasis>SLE</emphasis>, vol. 8706 of <emphasis>LNCS</emphasis>, pp. 161&#x2013;170, Springer, 2014. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=M%2E+Dalla+Preda%2C+S%2E+Giallorenzo%2C+I%2E+Lanese%2C+J%2E+Mauro%2C+and+M%2E+Gabbrielli%2C+%22AIOCJ%3A+A+choreographic+framework+for+safe+adaptive+distributed+applications%2C%22+in+SLE%2C+vol%2E+8706+of+LNCS%2C+pp%2E+161-170%2C+Springer%2C+2014%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch07_b11"/>&#8220;AIOCJ website.&#8221; <literal>http://www.cs.unibo.it/projects/jolie/aiocj.html</literal> <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=%22AIOCJ+website%2E%22+http%3A%2F%2Fwww%2Ecs%2Eunibo%2Eit%2Fprojects%2F+jolie%2Faiocj%2Ehtml" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch07_b12"/>&#8220;Jolie website.&#8221; <literal>http://www.jolie-lang.org/</literal></para></listitem>
<listitem><para><anchor id="ch07_b13"/>F. Montesi, C. Guidi, and G. Zavattaro, &#8220;Composing services with JOLIE,&#8221; in <emphasis>Proc. of ECOWS&#x2019;07</emphasis>, pp. 13&#x2013;22, IEEE, 2007. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=F%2E+Montesi%2C+C%2E+Guidi%2C+and+G%2E+Zavattaro%2C+%22Composing+services+with+JOLIE%2C%22+in+Proc%2E+of+ECOWS%2707%2C+pp%2E+13-22%2C+IEEE%2C+2007%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch07_b14"/>M. Coppo, M. Dezani-Ciancaglini, and B. Venneri, &#8220;Self-adaptive multiparty sessions,&#8221; <emphasis>Service Oriented Computing and Applications</emphasis>, vol. 9, no. 3-4, pp. 249&#x2013;268, 2015. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=M%2E+Coppo%2C+M%2E+Dezani-Ciancaglini%2C+and+B%2E+Venneri%2C+%22Self-adaptive+multiparty+sessions%2C%22+Service+Oriented+Computing+and+Applications%2C+vol%2E+9%2C+no%2E+3-4%2C+pp%2E+249-268%2C+2015%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch07_b15"/>G. Anderson and J. Rathke, &#8220;Dynamic software update for message passing programs,&#8221; in <emphasis>APLAS</emphasis>, vol. 7705 of <emphasis>LNCS</emphasis>, pp. 207&#x2013;222, Springer, 2012. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=G%2E+Anderson+and+J%2E+Rathke%2C+%22Dynamic+software+update+for+message+passing+programs%2C%22+in+APLAS%2C+vol%2E+7705+of+LNCS%2C+pp%2E+207-222%2C+Springer%2C+2012%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch07_b16"/>M. Bravetti <emphasis>et al.</emphasis>, &#8220;Towards global and local types for adaptation,&#8221; in <emphasis>SEFM Workshops</emphasis>, vol. 8368 of <emphasis>LNCS</emphasis>, pp. 3&#x2013;14, Springer, 2013. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=M%2E+Bravetti+et+al%2E%2C+%22Towards+global+and+local+types+for+adaptation%2C%22+in+SEFM+Workshops%2C+vol%2E+8368+of+LNCS%2C+pp%2E+3-14%2C+Springer%2C+2013%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch07_b17"/>&#8220;Scribble website.&#8221; <literal>http://www.jboss.org/scribble</literal> <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=%22Scribble+website%2E%22+http%3A%2F%2Fwww%2Ejboss%2Eorg%2Fscribble" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch07_b18"/>H. H&#x00FC;ttel <emphasis>et al.</emphasis>, &#8220;Foundations of session types and behavioural contracts,&#8221; <emphasis>ACM Computing Surveys</emphasis>, vol. 49, no. 1, 2016. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=H%2E+H%FCttel+et+al%2E%2C+%22Foundations+of+session+types+and+behavioural+contracts%2C%22+ACM+Computing+Surveys%2C+vol%2E+49%2C+no%2E+1%2C+2016%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch07_b19"/>D. Ancona <emphasis>et al.</emphasis>, &#8220;Behavioral types in programming languages,&#8221; <emphasis>Foundations and Trends in Programming Languages</emphasis>, vol. 3, no. 2-3, pp. 95&#x2013; 230, 2016. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=D%2E+Ancona+et+al%2E%2C+%22Behavioral+types+in+programming+languages%2C%22+Foundations+and+Trends+in+Programming+Languages%2C+vol%2E+3%2C+no%2E+2-3%2C+pp%2E+95-+230%2C+2016%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch07_b20"/>R. Hirschfeld, P. Costanza, and O. Nierstrasz, &#8220;Context-oriented Programming,&#8221; <emphasis>Journal of Object Technology</emphasis>, vol. 7, no. 3, pp. 125&#x2013;151, 2008.</para></listitem>
<listitem><para><anchor id="ch07_b21"/>R. Pawlak <emphasis>et al.</emphasis>, &#8220;JAC: an aspect-based distributed dynamic framework,&#8221; <emphasis>Software: Practice and Experience</emphasis>, vol. 34, no. 12, pp. 1119&#x2013;1148, 2004. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=R%2E+Pawlak+et+al%2E%2C+%22JAC%3A+an+aspect-based+distributed+dynamic+framework%2C%22+Software%3A+Practice+and+Experience%2C+vol%2E+34%2C+no%2E+12%2C+pp%2E+1119-1148%2C+2004%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch07_b22"/>Z. Yang, B. H. C. Cheng, R. E. K. Stirewalt, J. Sowell, S. M. Sadjadi, and P. K. McKinley, &#8220;An aspect-oriented approach to dynamic adaptation,&#8221; in <emphasis>WOSS</emphasis>, pp. 85&#x2013;92, ACM, 2002.</para></listitem>
<listitem><para><anchor id="ch07_b23"/>D. Merkel, &#8220;Docker: Lightweight linux containers for consistent development and deployment,&#8221; <emphasis>Linux J.</emphasis>, vol. 2014, no. 239, 2014. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=D%2E+Merkel%2C+%22Docker%3A+Lightweight+linux+containers+for+consistent+development+and+deployment%2C%22+Linux+J%2E%2C+vol%2E+2014%2C+no%2E+239%2C+2014%2E" target="_blank">Google Scholar</ulink></para></listitem>
</orderedlist>
</section>
</chapter>
<chapter class="chapter" id="ch08" label="8" xreflabel="8">
<title><literal>JaDA</literal> &#x2013; the Java Deadlock Analyzer</title>
<para><emphasis role="strong">Abel Garcia and Cosimo Laneve</emphasis></para>
<para>Department of Computer Science and Engineering, University of Bologna &#x2013; INRIA FOCUS, Mura Anteo Zamboni 7, 40127, Bologna, Italy</para>
<section class="lev2">
<title>Abstract</title>
<para><literal>JaDA</literal> is a static deadlock analyzer that targets <literal>Java</literal> bytecode. The core of <literal>JaDA</literal> is a behavioral type system especially designed to record dependencies between concurrent code. These behavioral types are thereafter analyzed by means of a fixpoint algorithm that reports potential deadlocks in the original <literal>Java</literal> code. We give a practical presentation of <literal>JaDA</literal>, highlighting the main connections between the tool and the theory behind it and presenting some of the features for customising the analysis. We finally assess <literal>JaDA</literal> against the current state-of-the-art tools, including a commercial grade one.</para>
</section>
<section class="lev1" id="sec8-1" label="8.1" xreflabel="8.1">
<title>Introduction</title>
<para>In concurrent languages, a <emphasis>deadlock</emphasis> is a circular dependency between a set of threads, each one waiting for an event produced by another thread in the set. In the <literal>Java</literal> programming language, deadlocks are usually <emphasis>resource-related</emphasis>, namely they are caused by operations ensuring different threads the exclusive access to a set of resources. (<literal>Java</literal> has also so-called <emphasis>communication-related</emphasis> deadlocks, which are common in network based systems. These deadlocks, which are thoroughly studied in [<link linkend="ch08_b1">1</link>,<link linkend="ch08_b2">2</link>], are out of the scope of this work.) <literal>Java</literal> features threads by means of an ad-hoc class called <literal>Thread</literal>; this class has two methods <literal>Thread.start()</literal> and <literal>Thread.join()</literal> for spawning and joining threads. The consistency between threads that share objects is enforced by <emphasis>synchronized blocks</emphasis>, a linguistic construct that may be defined either for simple code blocks or for method bodies [<link linkend="ch08_b3">3</link>, Chapter 17]<footnote id="fn8_1" label="1"><para>There are also other mechanisms that remain out of the scope of this work, such as, the <literal>volatile</literal> variables and the higher-level synchronization API defined on package <literal>java.util.concurrent</literal>.</para></footnote>. It turns out that the dependencies defined by synchronized blocks may be circular. These problems are difficult to detect or anticipate, since they may not occur during every execution. <link linkend="fig8_1">Figure <xref linkend="fig8_1" remap="8.1"/></link> shows (a timeline representation of) some examples of deadlocked programs. At the time of writing this chapter, the <emphasis>Oracle Bug Database</emphasis><footnote id="fn8_2" label="2"><para>http://bugs.java.com/</para></footnote> reports more than 40 unresolved bugs due to deadlocks, while the <emphasis>Apache Issue Tracker</emphasis><footnote id="fn8_3" label="3"><para>https://issues.apache.org/jira</para></footnote> reports around 400 unresolved deadlock bugs. Clearly, a deadlock may have catastrophic effects for the overall functionality of a software system.</para>
<fig id="fig8_1" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 8.1</label>
<caption><title>Cases of circular dependencies that may lead to deadlocks. (Lock acquisitions are represented with squares, the corresponding release is marked with a circle).</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig048.jpg" mime-subtype="jpeg"/>
</fig>
<para>In this chapter, we present an end-to-end automatic analyzer for detecting potential deadlock bugs of <literal>Java</literal> programs <emphasis>at compilation time</emphasis> &#x2013; <literal>JaDA</literal>, the <literal>Ja</literal>va <literal>D</literal>eadlock <literal>A</literal>nalyzer tool. <literal>JaDA</literal> addresses the compilation target of every <literal>Java</literal> application &#x2013; the <literal>Java</literal> <emphasis>Virtual Machine Language</emphasis>, <literal>JVML</literal>, also called <literal>Java</literal> bytecode &#x2013; and extracts abstract models out of it by means of an inference system. These abstract models are successively analyzed.</para>
<para>The decision of addressing <literal>JVML</literal> instead of <literal>Java</literal> was motivated by two reasons: <literal>Java</literal> is too complex and it has no reference semantics. On the contrary, <literal>JVML</literal> is simple &#x2013; it has 198 instructions that can be sorted into 7 different groups of similar instructions &#x2013; and has a reference semantics that is defined by the behavior of the <literal>Java</literal> <emphasis>Virtual Machine</emphasis> (<literal>JVM</literal>) [<link linkend="ch08_b3">3</link>, Chapter 6]. Analyzing <literal>JVML</literal> has also other relevant advantages: addressing programming languages that are compiled to the same bytecode, such as <literal>Scala</literal> [<link linkend="ch08_b4">4</link>], and the possibility to analyze proprietary software whose sources are not available.</para>
<para>The inference system of <literal>JaDA</literal> consists of a number of rules that analyze the effects of the instructions on the synchronization process. The types inferred from the bytecode, called <emphasis>lams</emphasis> [<link linkend="ch08_b1">1</link>, <link linkend="ch08_b2">2</link>, <link linkend="ch08_b5">5</link>, <link linkend="ch08_b6">6</link>], are functional programs that define dependencies between threads. Then <literal>JaDA</literal> uses a variation of the algorithm defined in [<link linkend="ch08_b1">1</link>, <link linkend="ch08_b2">2</link>] for detecting circularities in lams, and reports potential threats as output of the analysis. The tool also exhibits the executions causing deadlocks, by linking the dependencies with the chunk of source code that originated them, thus easing the analysis of false positives.</para>
<para>The current release of <literal>JaDA</literal> covers most of the <literal>JVML</literal>, including threads and synchronizations, constructors, arrays, exceptions, static members, interfaces, inheritance, recursive data types. Few synchronization-related features are not covered by the current release, such as <literal>wait</literal>-<literal>notify</literal>-<literal>notifyAll</literal> operations, dynamic class loading and reflection.</para>
<para>The rest of the chapter is organized as follows. Section 8.2 presents a motivating example of a recursive <literal>Java</literal> program that creates a (statically) unbounded number of threads. This is one of the main achievements so far and the theory overviewed in Section 8.3 will be explained by means of it. Section 8.4 describes the tool in some detail, highlighting implementation issues. Section 8.5 analyses the current limitations of <literal>JaDA</literal> and Section 8.6 reports an assessment of <literal>JaDA</literal> with respect to state-of-the-art tools for <literal>Java</literal> deadlock analysis. Finally we conclude in Section 8.7.</para>
</section>
<section class="lev1" id="sec8-2" label="8.2" xreflabel="8.2">
<title>Example</title>
<para><link linkend="fig8_2">Figure <xref linkend="fig8_2" remap="8.2"/></link> reports the <literal>Java</literal> class <literal>Network</literal> and part of its <literal>JVML</literal>. The <literal>main</literal> method creates a network of <literal>n</literal> threads by invoking <literal>buildNetwork</literal> &#x2013; say <emphasis>t</emphasis><subscript>1</subscript><emphasis>,</emphasis> &#x2026; <emphasis>,t</emphasis><emphasis><subscript>n</subscript></emphasis> &#x2013; that are all potentially running in parallel with the caller &#x2013; say <emphasis>t</emphasis><subscript>0</subscript>. Every two adjacent threads share an object, which is also created by <literal>buildNetwork</literal>.</para>
<para>The <literal>buildNetwork</literal> method will produce a deadlock depending on its actual arguments: it is deadlock-free when it is invoked with two different objects, otherwise it may deadlock (if also <literal>n</literal> &#x003E; <emphasis role="roman">0</emphasis>). Therefore, in the case of <link linkend="fig8_2">Figure <xref linkend="fig8_2" remap="8.2"/></link>, the program is deadlocked, while it is deadlock free if we comment the instruction <literal>buildNetwork(n,x,x)</literal> and uncomment <literal>buildNetwork(n,x,y)</literal>.</para>
<fig id="fig8_2" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 8.2</label>
<caption><title><literal>Java Network</literal> program and corresponding bytecode of methods <literal>buildNetwork</literal> and <literal>takeLocks</literal>. Comments in the bytecode give information of the objects used and/or methods invoked in each instruction.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig049.jpg" mime-subtype="jpeg"/>
</fig>
<para>The problematic issue of <literal>Network</literal> is that the number of threads is not known statically &#x2013; <literal>n</literal> is an argument of <literal>main</literal>. This is displayed in the bytecode of <literal>buildNetwork</literal> in <link linkend="fig8_2">Figure <xref linkend="fig8_2" remap="8.2"/></link> by the instructions at addresses 30 and 37 that respectively created a new thread and start it, and by the recursive invocation at instruction 47.</para>
</section>
<section class="lev1" id="sec8-3" label="8.3" xreflabel="8.3">
<title>Overview of JaDA&#x2019;s Theory</title>
<para><literal>JaDA</literal>&#x2019;s theory relies on two main techniques: (<emphasis>i</emphasis>) an inference type system for extracting abstract models out of <literal>JVML</literal> instructions, and (<emphasis>ii</emphasis>) a fixpoint algorithm for analyzing the models. We overview the two techniques in the following subsections; in the last subsection we discuss the <literal>JaDA</literal> behavioral types for the <literal>buildNetwork</literal> example.</para>
<section class="lev2" id="sec8-3-1" label="8.3.1" xreflabel="8.3.1">
<title>The Abstract Behavior of the <literal>Network</literal> Class</title>
<para><link linkend="fig8_3">Figure <xref linkend="fig8_3" remap="8.3"/></link> details the output of <literal>JaDA</literal> for the <literal>Network</literal> class in <link linkend="fig8_2">Figure <xref linkend="fig8_2" remap="8.2"/></link>. The types have been simplified for readability: the actual <literal>JaDA</literal> types are more complex and verbose. Some comments (in gray) explain the side effects of invocations, other comments (in yellow) correspond to the lines that are commented in <link linkend="fig8_2">Figure <xref linkend="fig8_2" remap="8.2"/></link>. The behavior of <literal>main</literal> begins by calling the constructor of the class <literal>Object</literal>. Notice that, after such invocation, the structure of <literal>x</literal> and <literal>y</literal> is known. Then the type reports the invocation to <literal>buildNetwork</literal>.</para>
<para>The behavior of <literal>takeLocks</literal> is the parallel composition of two dependencies corresponding to the acquisition of the locks of <literal>x</literal> and <literal>y</literal>. Every dependency is formed by the last held lock and the current element. Notice that every method receives an extra argument corresponding to the last acquired lock at the moment of the invocation, in this case that argument is <literal>u</literal>.</para>
<para>The behavior of <literal>buildNetwork</literal> has five states: (<emphasis>i</emphasis>) the invocation to <literal>takeLocks</literal>,(<emphasis>ii</emphasis>) the creation and initialization of the object <literal>z</literal>,(<emphasis>iii</emphasis>) the creation and initialization of the thread <literal>thr</literal>, (<emphasis>iv</emphasis>) the spawn of <literal>thr</literal>, (<emphasis>v</emphasis>) and the recursive invocation (in parallel with the spawn of <literal>thr</literal>). The <literal>buildNetwork</literal> method also reports one spawned thread as side effect. This may appear contradictory (because <literal>buildNetwork</literal> spawns <emphasis>n</emphasis> threads). However, in this case <literal>JaDA</literal> is able to detect that <literal>thr</literal> is the only thread (from those created) that may be relevant (for the deadlock analysis) in an outer scope. This deduction is done by considering the objects in the record structure of <literal>thr</literal>.</para>
<para>The constructors of <literal>Object</literal> and <literal>Thread</literal> have an empty behavior. On the contrary, the constructor of the class <literal>Network$1</literal> is more complex (<literal>Network$1</literal> is the name the <literal>JVM</literal> automatically gives to the anonymous <literal>Thread</literal> child class<footnote id="fn8_4" label="4"><para>https://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html</para></footnote> instantiated inside the method <literal>buildNetwork</literal> of the class <literal>Network</literal>). Being defined as an inner class, <literal>Network$1</literal> has access to the local variables in the scope in which it has been created, namely the variables <literal>x</literal>, <literal>z</literal> and the <literal>this</literal> reference to the container instance. The <literal>JVM</literal> addresses this by passing these variables to the constructor of the class and assigning them to internal fields, in this case named <literal>val$x</literal>, <literal>val$z</literal> and <literal>this$0</literal>. Notice that the behavior of the constructor keeps track of two important things: the invocation to the constructor of the parent class <literal>Thread.init</literal> and the changes in the carrier object which goes from <literal>this</literal> to <literal>this[this$0:x1, val$x:x2, val$z:x3]</literal> where <literal>x</literal><emphasis><subscript>i</subscript></emphasis> are the formal arguments.</para>
<fig id="fig8_3" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 8.3</label>
<caption><title><literal>BuildNetwork&#x2019;</literal>s lams.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig050.jpg" mime-subtype="jpeg"/>
</fig>
<para>Finally, the behavior of the <literal>run</literal> method from the class <literal>Network$1</literal> contains only the invocation to the <literal>takeLocks</literal> method. Notice that <literal>run</literal> method assumes a certain structure from the carrier object.</para>
</section>
<section class="lev2" id="sec8-3-2" label="8.3.2" xreflabel="8.3.2">
<title>Behavioral Type Inference</title>
<para>The typing process is done bottom-up, in a compositional way. That is, a type is derived for every <literal>JVML</literal> instruction; the type of each method is the composition of the types of the instructions it contains. Similarly, the type of a program is the set of type of the methods therein. <literal>JaDA</literal> types are not standard types, such as integers, booleans, etc. They are models of the abstract behavior of a program, called <emphasis>behavioral types</emphasis>, that hold information about concurrency and synchronizations of every execution path.</para>
<para>In particular, the types of instructions retain two key pieces of information in <literal>JaDA</literal>:</para>
<itemizedlist mark="bullet" spacing="normal">
<listitem><para>the <emphasis>dependencies</emphasis>, written <literal>t:(a,b)</literal>, to be read as &#8220;thread <emphasis>t</emphasis> acquires the lock of object <emphasis>b</emphasis> while it is holding the lock of <emphasis>a</emphasis>&#8221;, and</para></listitem>
<listitem><para>the <emphasis>method invocations</emphasis>, written <literal>C.m(</literal><emphasis>args</emphasis><literal>| t, a)</literal>, which means that the method <emphasis role="roman">m</emphasis> of class <literal>C</literal> is invoked with arguments <emphasis>args</emphasis> (that include the carrier object) in the thread <emphasis>t</emphasis> and while holding the lock of <emphasis>a</emphasis>.</para></listitem>
</itemizedlist>
<para>In order to verify the consistency of parallel threads, behavioral types also take into account the (reading and writing) <emphasis>effects</emphasis> on objects. The types of the instructions can be composed either sequentially with the + operation, or in parallel with the &#x0026; operation.</para>
<para>In <literal>JaDA</literal>, the behaviors of methods are the sequential composition of instructions&#x2019; types in method&#x2019;s body plus the sum of their effects. The effects also include both threads spawns and thread joins in the method body. It is worth to remark that thread creations and synchronizations in <literal>JVML</literal> are defined by method invocations of the class <literal>Thread</literal>; therefore they are typed as method invocations with an ad-hoc management of spawns and joins.</para>
<para>The flow of the inference of behavioral types is described by the chart in <link linkend="fig8_4">Figure <xref linkend="fig8_4" remap="8.4"/></link>. The algorithm starts with an (empty) <emphasis>Behavioral Class Table</emphasis> (<literal>BCT</literal>), a structure where a behavioral description is associated to every method, and a sorted set of pending methods which initially contains all the methods of the program. The algorithm takes the first element of the set and types it (see below). The resulting effects are compared to the previous state of the <literal>BCT</literal>: if a change is found, the method is updated and every caller (every method depending on the current one) is added to the pending methods list. The algorithm terminates when the <literal>BCT</literal> reaches an stable state.</para>
<para>A similar technique is used to type the body of each method. The process is described in the chart shown in <link linkend="fig8_5">Figure <xref linkend="fig8_5" remap="8.5"/></link>. In this case, the inference process inside a method starts with a queue of pending instructions, which initially contains the first instruction. Each instruction is typed and the instruction <emphasis>state</emphasis> is updated (we have defined a set of typing rules in [<link linkend="ch08_b7">7</link>]). If the instruction type has been updated then the subsequent instruction(s) need to be typed (again). Notice that there may be several subsequent instructions, for example when the current instruction is a conditional. The state of an instruction contains an abstraction of the operand stack, the local variables, the local heap, the threads created upto that instruction and the chain of acquired locks (this information allows us to define the <emphasis>lam</emphasis> [<link linkend="ch08_b2">2</link>] of an instruction). Once no state is updated anymore, the type of the corresponding method is computed accordingly.</para>
<fig id="fig8_4" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 8.4</label>
<caption><title>Type inference of methods&#x2019; behaviors in <literal>JaDA</literal>.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig051.jpg" mime-subtype="jpeg"/>
</fig>
<fig id="fig8_5" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 8.5</label>
<caption><title>Type inference of method&#x2019;s bodies in <literal>JaDA</literal>.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig052.jpg" mime-subtype="jpeg"/>
</fig>
</section>
<section class="lev2" id="sec8-3-3" label="8.3.3" xreflabel="8.3.3">
<title>Analysis of Behavioral Types</title>
<para>The analysis of the inferred types is also performed iteratively. The overall approach is described by the chart in <link linkend="fig8_6">Figure <xref linkend="fig8_6" remap="8.6"/></link>. The initial step computes the <emphasis>abstract state</emphasis> of every method. This state is a sequence of parallel compositions of dependency pairs &#x2013; function invocations in lams are deleted. The algorithm proceeds instruction by instruction, by <emphasis>expanding</emphasis> and <emphasis>cleaning</emphasis> its current state. The expansion process unfolds every invocation, the cleaning process removes pairs containing a fresh name (names not belonging to the method arguments or effects). Removing such pairs is crucial for termination because it allows us to keep the set of dependencies finite. In particular, the cleaning is performed by computing the transitive closure of the dependency pairs (this way we recover dependencies that are not direct and involve fresh names) and keeping only those whose elements are not fresh. In case we find a circular dependency formed only by fresh names then a special dependency pair is inserted (and this will ensure the presence of a deadlock). The full details of this algorithm are described in [<link linkend="ch08_b1">1</link>, <link linkend="ch08_b2">2</link>].</para>
<para>Once all abstract states have been computed, the algorithm returns the circularities present in the <literal>main</literal> method.</para>
<fig id="fig8_6" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 8.6</label>
<caption><title><literal>JaDA</literal> Analysis of behavioral types.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig053.jpg" mime-subtype="jpeg"/>
</fig>
<para>As an example, we apply the algorithm of <link linkend="fig8_6">Figure <xref linkend="fig8_6" remap="8.6"/></link> to the <literal>Network</literal> behaviour in <link linkend="fig8_3">Figure <xref linkend="fig8_3" remap="8.3"/></link>. For simplicity we have excluded the methods with empty behaviour as well as their invocations.</para>
<para>Initially, an empty state is associated to every method. Using this model, we perform the first iteration and we get (we denote a set with <literal>[ e</literal><subscript>1</subscript><literal>, e</literal><subscript>2</subscript><literal>,</literal> &#x2026;] where elements <literal>e</literal><subscript><emphasis>i</emphasis></subscript> are dependencies <literal>t:(x,y)</literal>; sets of sets are denoted by <literal>[ [ e<subscrip>1</subscrip>, e<subscript>2</subscript>, &#x00B7;&#x00B7;&#x00B7; ], &#x00B7;&#x00B7;&#x00B7; ])</literal>:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog092.jpg" mime-subtype="jpeg"/></para>
<para>Since the states of methods is changed (all except <literal>main</literal>) we perform a second iteration, which gives:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog093.jpg" mime-subtype="jpeg"/></para>
<para>Since <literal>buildNetwork</literal> is changed, we need a third iteration. The computation of the dependencies of <literal>main</literal> gives</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog094.jpg" mime-subtype="jpeg"/></para>
<para>In particular, in the states of <literal>main</literal>, after the transitive closure, contain <literal>t_thr:(x,x)</literal>, which is a circular dependency involving two threads. Instead of writing the dependency in that way (using a fresh name <literal>x</literal>), we write it as <literal>t_thr:($,$)</literal>, where <literal>x</literal> is replaced by a special name <literal>$</literal>. It is worth to notice that <literal>t_thr:($,$)</literal> gives two informations: (<emphasis>i</emphasis>) the deadlock is created by threads <literal>t</literal> and <literal>thr</literal>, (<emphasis>ii</emphasis>) the object name is <literal>$</literal>, which indicates that the deadlock is produced regardless of the arguments of the invocation. Since <literal>t_thr:($,$)</literal> denotes a circularity, the algorithm might stop. Actually, we decided not to stop <literal>JaDA</literal> at this point, we let it continue in order to collect every circularity.</para>
<para><literal>JaDA</literal> output for the <literal>Network</literal> program is reported in <link linkend="fig8_7">Figure <xref linkend="fig8_7" remap="8.7"/></link>. In this case, <literal>JaDA</literal> has been set to analyze only the <literal>Network</literal> class (see <emphasis>analysis-extent</emphasis> in Section 8.4.4). Therefore, it warns about non-analyzed dependencies: the constructors from classes <literal>Thread</literal> and <literal>Object</literal> (whose types are considered empty &#x2013; the actual type of these methods is nevertheless empty). <literal>JaDA</literal> reports 1 deadlock after the analysis, and outputs its trace. In this case there are two threads involved in the deadlock: those with id 204 (the one running <literal>main</literal>) and 229. The deadlock is caused by two <literal>monitorenter</literal> instructions on objects 346 and 211, taken in different order by the two threads. The tool outputs the computational traces ending with the two <literal>monitorenter</literal> instructions; the numbers in the traces represent the lines in the source<footnote id="fn8_5" label="5"><para>The line numbers in the output may not accurately match the example in <link linkend="fig8_2">Figure <xref linkend="fig8_2" remap="8.2"/></link>, because the latter has been slightly reduced for presentation purposes.</para></footnote>.</para>
<fig id="fig8_7" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 8.7</label>
<caption><title><literal>JaDA</literal> analysis output for the <literal>Network</literal> program.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig054.jpg" mime-subtype="jpeg"/>
</fig>
</section>
</section>
<section class="lev1" id="sec8-4" label="8.4" xreflabel="8.4">
<title>The JaDA Tool</title>
<para>In this section we describe the main features of the <literal>JaDA</literal> tool, as well as, some key implementation details.</para>
<section class="lev2" id="sec8-4-1" label="8.4.1" xreflabel="8.4.1">
<title>Prerequisites</title>
<para><literal>JaDA</literal> has been designed to run on bytecode generated by the <literal>Java</literal> compiler<footnote id="fn8_6" label="6"><para>We have tested <literal>JaDA</literal> against the 1.6, 1.7 and 1.8 versions of the <literal>Java</literal> compiler, and against the 1.8 version of the Eclipse Java Compiler (ECJ).</para></footnote> and it assumes that the bytecode has been already checked by the <literal>Java</literal> Bytecode Verifier (therefore it does not contain either syntactic or semantic errors). <literal>JaDA</literal> also requires that every dependency is matched by a corresponding bytecode. Although the bytecode is not executed, <literal>JaDA</literal> computes every necessary information to solve key issues for the analysis, such as the informations about inheritance. The loading of the existing types is done dynamically in a sand-boxed class loader<footnote id="fn8_7" label="7"><para>https://docs.oracle.com/javase/7/docs/api/java/lang/ClassLoader.html</para></footnote> to avoid security risks. The full set of dependencies can be specified in <literal>JaDA</literal> through a <emphasis>classpath</emphasis>-like configuration (see property <literal>class-path</literal> in Section 8.4.4). Finally, <literal>JaDA</literal> also assumes that the code targeted by the analysis fits with the current limitations of the tool (see Section 8.5).</para>
</section>
<section class="lev2" id="sec8-4-2" label="8.4.2" xreflabel="8.4.2">
<title>The Architecture</title>
<para>The <literal>JaDA</literal> analysis starts by parsing of the bytecode of a program and its dependencies. This is a cumbersome task because of the length and verbosity of the <literal>JVML</literal> syntax. <literal>JaDA</literal> relies on the <literal>ASM</literal> framework [<link linkend="ch08_b8">8</link>] for the bytecode extraction and manipulation. (Other third party tools have been also designed for manipulating and analyzing the bytecode: the page <literal>https://java-source.net/open-source/bytecode-libraries</literal> contains a list of existing tools for this purpose. <literal>ASM</literal> provides a wide set of tools for interacting with the bytecode, including code generation and code analysis. It is also light-weight, open source, very well-documented and up todate with the latests versions of <literal>Java</literal>.</para>
<para><link linkend="fig8_8">Figure <xref linkend="fig8_8" remap="8.8"/></link> shows part of the <literal>JaDA</literal> architecture. In the figure, nodes are classes while arrows denote inheritance relationships. In the center of the image, there are the classes of the <literal>ASM</literal> framework; the other classes implement the technique so far described.</para>
<para><emphasis role="strong">Values.</emphasis> A basic element of the architecture are the <literal>Value</literal> objects. <literal>JaDA</literal> uses two types of values: <literal>RecordTree</literal> store the methods&#x2019; signature in the <literal>BCT</literal>, while <literal>RecordPtr</literal> store the state of local variables and the operand stack. Updating the <literal>Value</literal> elements amounts to upgrade every other element of the <literal>JaDA</literal> architecture. In the following paragraphs we discuss their functionality.</para>
<para><emphasis role="strong">Frames.</emphasis> The <literal>JDAFrame</literal> class extends the <literal>ASM Frame</literal> by defining two important methods: <literal>execute</literal> and <literal>merge</literal>. The method <literal>execute</literal> implements the typing rules used by <literal>JaDA</literal>. It relies on an abstract interpreter that executes symbolically the current instruction with the given stack and local variables state. The method <literal>merge</literal> is invoked when the analysis process reiterates over an already typed frame. This method implements the logics of the <emphasis>has changed</emphasis> condition in the type inference of method bodies, see Section 8.3.2.</para>
<fig id="fig8_8" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 8.8</label>
<caption><title><literal>JaDA</literal> architecture.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig055.jpg" mime-subtype="jpeg"/>
</fig>
<fig id="fig8_9" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 8.9</label>
<caption><title><literal>Java while</literal> loop with nested synchronizations and the corresponding bytecode.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig056.jpg" mime-subtype="jpeg"/>
</fig>
<para>The decision on whether the subsequent frames must be checked again is taken upon the result of this method. To illustrate this consider the example from <link linkend="fig8_9">Figure <xref linkend="fig8_9" remap="8.9"/></link>.</para>
<para>When the typing process arrives each instruction for the first time its current Frame changes from the empty frame to the frame containing information about the instruction. Namely this first frame will contain the local variable status, the invocations and the existing locks and threads at each instruction. This changes enforces every frame to calculate its continuation at least one time. The following sequence shows the frames calculation for this chunk of code (only the relevant instructions are included):</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog095.jpg" mime-subtype="jpeg"/></para>
<para>Notice that after calculating the frame 46, there are two possible continuations: 21 and 48. The second time Fr.21 is calculated it produces the same known result, therefore its continuation (Fr.22) is not calculated again. The calculation process continues then sequentially with Fr.48.</para>
<para><emphasis role="strong">Interpreter.</emphasis> The <literal>JVM</literal> is a <emphasis>stack machine</emphasis>, every operation pops a certain number of elements off the stack and pushes on its result. The <literal>JDAInterpreter</literal> class extends the <literal>ASM Interpreter</literal> in order to comply with the values representations in <literal>JaDA</literal>. In particular, <literal>JDAInterpreter</literal> implements an important feature of our tool, namely the output of the traces potentially causing deadlocks. In fact, it returns the variable names of the objects involved, the stack trace chain and the related line numbers in the original <literal>Java</literal> code<footnote id="fn8_8" label="8"><para>This is possible only when the bytecode has been compiled including debugging information.</para></footnote>.</para>
<para><emphasis role="strong">Analyzer.</emphasis> The <literal>ASM</literal> default analyzer supports very basic data-flow analysis limited to the scope of a single method. Similarly, <literal>JaDA</literal> analysis of a method does not go beyond its scope. However <literal>JDAAnalyzer</literal> extracts the necessary information &#x2013; the type &#x2013; that supports the compositional analysis. This part is implemented by the algorithm described in <link linkend="fig8_5">Figure <xref linkend="fig8_5" remap="8.5"/></link>, which is the building block of our tool. Consequently, <literal>JDAAnalyzer</literal> analyses the whole program by computing the final state of the <literal>BCT</literal> according to the algorithm in <link linkend="fig8_4">Figure <xref linkend="fig8_4" remap="8.4"/></link>. The final step of <literal>JDAAnalyzer</literal> is the computation of the models of the methods in the BCT as described in the algorithm in <link linkend="fig8_6">Figure <xref linkend="fig8_6" remap="8.6"/></link>.</para>
</section>
<section class="lev2" id="sec8-4-3" label="8.4.3" xreflabel="8.4.3">
<title>The Current <literal>JVML</literal> Coverage</title>
<para>The theory of <literal>JaDA</literal> has been studied in [<link linkend="ch08_b7">7</link>] where we have defined the typing rules for a number of complex features of <literal>JVML</literal>, in particular, threads, synchronizations, static constructors, recursive data structures, inheritance and polymorphism, reflection and native methods. For a subset of this language &#x2013; those featuring threads and synchronizations &#x2013; we also delivered a correctness proof [<link linkend="ch08_b9">9</link>]. In this section we briefly overview our solutions for the main features of <literal>JVML</literal> that are covered in the current release of <literal>JaDA</literal>.</para>
<para><emphasis role="strong">Static constructors.</emphasis> Static constructors are problematic because they are not explicitly invoked by the <literal>JVM</literal>. In fact, those are invoked on-the-fly by the <literal>JVM</literal> when the first (static) access to the containing class is performed. That is, the code of a static constructor can potentially precede any operation involving a static member of its class. In order to deal with this issue in a sound way, we model every static operation as a non-deterministic choice between the type of the operation <emphasis>per-se</emphasis> and the typing of the static constructor of the class followed by the original operation. As one can imagine this makes the analysis computationally complex because the number of possible behaviors exponentially increases. The alternative (and the default choice) in <literal>JaDA</literal> is to assume that static constructors are all executed <emphasis>before</emphasis> the <literal>main</literal> method (see the <literal>static-constructor</literal> option in Section 8.4.4). This is a safe choice provided that concurrent operations do not occur within static constructors (which is often the case).</para>
<para><emphasis role="strong">Recursive data structures.</emphasis> As discussed in Section 8.3.2, the analysis of <literal>JaDA</literal> relies on several iterative processes. The termination of these iterations strongly relies on the constraint that the number of object names is always finite. To ensure this finiteness constraint, the recursive objects are abstracted during the inference process. In particular, the inference replaces the field values whose class is already present in the object structure by a generic representative value. These representative values are treated in an ad-hoc way during the analysis of circular dependencies. Namely, they are considered equal to any other object of the same class (that is their identity is not guaranteed). Our assessments indicate that this over-approximation does not jeopardise <literal>JaDA</literal>&#x2019;s precision when the elements of the recursive structure are pairwise different and threads act in a uniform way on them. On the contrary, the tool may return a number of false positives that is proportional to the dimension of the structure.</para>
<para><emphasis role="strong">Arrays.</emphasis> Since <literal>JaDA</literal> does not process numerical expressions, it considers <literal>array[<link linkend="ch08_b2">2</link>]</literal> equal to <literal>array[<link linkend="ch08_b3">3</link>]</literal>. Therefore, <literal>JaDA</literal> manages arrays in a similar way it does for recursive data types. Every element in the array is represented by a unique object and, as for recursive data structures, this may be the cause of over-approximations. For example, <literal>JaDA</literal> returns a false positive when two different threads in parallel perform a lock operation on different objects of the array.</para>
<para><emphasis role="strong">Inheritance and polymorphism.</emphasis> Inheritance and, in particular, polymorphism are sources of non-determinism. In fact, since it is not possible to resolve the runtime type of an object at static time, we cannot determine in a precise way the instance method being invoked over it. To deal with this issue in a sound way, <literal>JaDA</literal> substitutes every invocation with the nondeterministic choice among the method implementations in the type hierarchy of the carrier. Enhancing this process to increase the precision of the analysis is currently an ongoing work. In the current release, whenever it is possible to derive the runtime type, we drop the wrong invocations.</para>
<para><emphasis role="strong">Reflection, native methods, alternative concurrency models.</emphasis> In <literal>Java</literal>, like in many modern programming languages, there is some support for meta-programming, namely the capacity of a program to modify itself during its execution. <literal>Java</literal> also admits (native) methods and concurrency models that have no bytecode implementation. These methods are treated in an ad-hoc manner by the <literal>JVM</literal>. In all these cases, since there is not an explicit bytecode implementation, there is no evidence of what will happen at static time. Because of this reason, <literal>JaDA</literal> by default assumes a void behavior in these situations. Although, users can manually provide the behavior descriptions for methods involving such operations (see <literal>custom-types</literal> option in Section 8.4.4). This is particularly useful in the case of native methods (which are implemented in <literal>C</literal>), where users provide a more accurate behavior by analyzing its actual implementation.</para>
</section>
<section class="lev2" id="sec8-4-4" label="8.4.4" xreflabel="8.4.4">
<title>Tool Configuration</title>
<para>In order to provide some flexibility, <literal>JaDA</literal> supports a set of settings to customize the analysis.</para>
<itemizedlist mark="none" spacing="normal">
<listitem><para><literal>&lt;target&gt;</literal><emphasis role="strong">:</emphasis> this setting specifies the target file or folder to analyze. It is mandatory. The type of files admitted are: <literal>Java</literal>class files (&#8220;.class&#8221;), <literal>Java</literal> jar files (&#8220;.jar&#8221;) and compressed zip files (&#8220;.zip&#8221;). In the case of folders, the content of the folder is analyzed recursively.</para></listitem>
<listitem><para><literal>verbose[=&lt;value&gt;]</literal><emphasis role="strong">:</emphasis> the value ranges from 1 to 5, the default and more verbose value is 5.</para></listitem>
<listitem><para><literal>class-path &lt;classpath&gt;</literal><emphasis role="strong">:</emphasis> Standard <literal>Java</literal> classpath description. If the target contains dependencies other than those in the standard library, they must be specified via this option.</para></listitem>
<listitem><para><literal>target-method &lt;methodName&gt;</literal><emphasis role="strong">:</emphasis> fully qualified target method (should be a void method without arguments). It compels <literal>JaDA</literal> to analyse the specified method. If this option is not set, the analysis chooses the first <literal>main</literal> method found.</para></listitem>
<listitem><para><literal>analysis-extent[=&lt;value&gt;]</literal><emphasis role="strong">:</emphasis> Indicates the extent of the analysis. Possible values are <literal>full</literal>: analyzes every dependency including the system and classpath-included libraries; <literal>classpath</literal>: analyzes every library in the classpath (this is the default value); <literal>custom</literal>: analyzes the classes specified through the property <literal>additional-targets</literal>;and <literal>self</literal>: does not analyze any class but the specified target.</para></listitem>
<listitem><para><literal>additional-targets &lt;classes&gt;</literal><emphasis role="strong">:</emphasis> if <literal>analysis-extent</literal> is set to <literal>custom</literal> this property must contain a comma separated list of the fully qualified names of a subset of classes in the <emphasis>classpath</emphasis> to include in the analysis. Such a feature is useful for avoiding typing known libraries.</para></listitem>
<listitem><para><literal>custom-types &lt;file&gt;</literal><emphasis role="strong">:</emphasis> a setting file to specify predefined behavioral types.</para></listitem>
<listitem><para><literal>static-constructors[=&lt;value&gt;]</literal><emphasis role="strong">:</emphasis> indicates when the static constructors should be processed, the possibilities are <literal>before-all</literal> and <literal>non-deterministically</literal>. The default option is <literal>before-all</literal>.</para></listitem>
</itemizedlist>
</section>
<section class="lev2" id="sec8-4-5" label="8.4.5" xreflabel="8.4.5">
<title>Deliverables</title>
<para><literal>JaDA</literal> is available in three forms: a demo website [<link linkend="ch08_b10">10</link>], a command line tool (see <link linkend="fig8_7">Figure <xref linkend="fig8_7" remap="8.7"/></link>) and an Eclipse plug-in. All of them share the same core: a prototype implementation of the technique discussed in [<link linkend="ch08_b7">7</link>]. At the moment of writing this chapter, the demo website only allows to analyze single-file programs and to use a subset of the options previously described. The command line tool and the Eclipse plug-in are available through direct requests. The Eclipse plug-in output also displays the execution graph causing the deadlock with links to the source code that originates it (see <link linkend="fig8_10">Figure <xref linkend="fig8_10" remap="8.10"/></link>).</para>
</section>
</section>
<section class="lev1" id="sec8-5" label="8.5" xreflabel="8.5">
<title>Current Limitations</title>
<para>The current version of <literal>JaDA</literal> does not cover a coordination mechanism between thread that is quite usual in <literal>Java</literal>: the <literal>wait</literal>-<literal>notify</literal>-<literal>notifyAll</literal> operations. There are also other less critical limitations, such as the analysis of native code and reflection operations. However, these features can be covered by manually specifying the behavior of the corresponding methods (see property <literal>custom-types</literal> in Section 8.4.4).</para>
<para>The methods <literal>wait</literal>-<literal>notify</literal>-<literal>notifyAll</literal> are public and final of the class <literal>Object</literal>; therefore they are inherited by all classes and cannot be modified. The invocations to <literal>wait</literal>, <literal>notify</literal> and <literal>notifyAll</literal> succeed for threads that already hold the lock of the object <emphasis>a</emphasis> on the stack. In this case, the <literal>wait</literal> instruction moves its own thread to the <emphasis>wait set</emphasis> of <emphasis>a</emphasis> and the object is relinquished by performing as many unlock operations as the integer stored in the lock field of <emphasis>a</emphasis>. The instructions <literal>notify</literal> and <literal>notifyAll</literal> respectively wake up one thread and all the threads in the wait set of <emphasis>a</emphasis>. The woken-up threads are re-enabled for thread scheduling, which means competing for acquiring the lock of <emphasis>a</emphasis> again. The winner will lock <emphasis>a</emphasis> as many times it did on <emphasis>a</emphasis> before the <literal>wait</literal>-operation.</para>
<fig id="fig8_10" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 8.10</label>
<caption><title><literal>JaDA</literal> Eclipse plug-in screenshot.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig057.jpg" mime-subtype="jpeg"/>
</fig>
<para>Below we briefly describe the solution we are currently investigating for extending <literal>JaDA</literal> to cover <literal>wait</literal>-<literal>notify</literal>-<literal>notifyAll</literal>.</para>
<para>We use two new type of dependency pairs: <emphasis>t</emphasis><subscript>1</subscript> <emphasis role="roman">:</emphasis> (<emphasis>a, a</emphasis><emphasis><sup>n</sup></emphasis>), which means &#8220;<emphasis>the thread</emphasis> <emphasis>t</emphasis><subscript>1</subscript> <emphasis>sends a notification on</emphasis> <emphasis>a</emphasis> <emphasis>while holding its lock</emphasis>, and <emphasis>t</emphasis><subscript>2</subscript> <emphasis role="roman">:</emphasis> (<emphasis>a, a</emphasis><emphasis><sup>w</sup></emphasis>), which means &#8220;<emphasis>the thread</emphasis> <emphasis>t</emphasis><subscript>2</subscript> <emphasis>awaits a notification on</emphasis> <emphasis>a</emphasis> <emphasis>while holding its lock</emphasis>&#8221;. These two pairs are respectively produced by <literal>notify</literal> and <literal>wait</literal> methods. The problem is that, even if the abstract model retains a term <emphasis>t</emphasis><subscript>1</subscript> <emphasis role="roman">:</emphasis> (<emphasis>a, a</emphasis><emphasis><sup>n</sup></emphasis>)&#x0026;<emphasis>t</emphasis><subscript>2</subscript> <emphasis role="roman">:</emphasis> (<emphasis>a, a</emphasis><emphasis><sup>w</sup></emphasis>) expressing that the <literal>wait</literal> and <literal>notify</literal> occur in parallel threads (<emphasis>notification-wait</emphasis> matching couple), we cannot conclude that the program is deadlock-free. This because the above term does not convey any information about what operation <emphasis>has been performed before</emphasis>. In fact, a wrong ordering might cause the thread <emphasis>t</emphasis><subscript>2</subscript> to wait indefinitely. To overcome this problem, we extend <literal>JaDA</literal> with an additional analysis that detects the <emphasis>wait</emphasis> pairs that can potentially remain unsatisfied. This solution is extensively discussed in [<link linkend="ch08_b7">7</link>].</para>
</section>
<section class="lev1" id="sec8-6" label="8.6" xreflabel="8.6">
<title>Related Tools and Assessment</title>
<para><literal>JaDA</literal> has been assessed with respect to a number of state-of-the-art tools. In particular, in <link linkend="tab8_1">Table <xref linkend="tab8_1" remap="8.1"/></link>, the tools have been classified according to the type of analysis they perform (see [<link linkend="ch08_b7">7</link>] for a discussion about analysis techniques for deadlock detection). We have chosen <literal>Chord</literal> for static analysis [<link linkend="ch08_b11">11</link>], <literal>Sherlock</literal> for dynamic analysis [<link linkend="ch08_b12">12</link>], and <literal>GoodLock</literal> for hybrid analysis [<link linkend="ch08_b13">13</link>]. We have also considered a commercial tool, <literal>ThreadSafe</literal><footnote id="fn8_9" label="9"><para>http://www.contemplateltd.com/threadsafe</para></footnote> [<link linkend="ch08_b14">14</link>].</para>
<para>We have analyzed a number of programs that exhibit a variety of sharing patterns. The source of all benchmarks in <link linkend="tab8_1">Table <xref linkend="tab8_1" remap="8.1"/></link> is available either at [<link linkend="ch08_b11">11</link>, <link linkend="ch08_b12">12</link>] or in the <literal>JaDA-deadlocks</literal> repository<footnote id="fn8_10" label="10"><para>https://github.com/abelunibo/Java-Deadlocks</para></footnote>.</para>
<para>Since the current release of <literal>JaDA</literal> does not completely cover <literal>JVM</literal>, in order to gain preliminary experience, we modified the Java libraries and the multithreaded server programs of RayTracer, MolDyn and MonteCarlo (labelled with &#8220;(*)&#8221; in the <link linkend="tab8_1">Table <xref linkend="tab8_1" remap="8.1"/></link>) and implemented them in our system. This required little programming overhead; in particular, we removed volatile variables, avoided the use of <literal>Runnable</literal> interfaces for creating threads, and reduced the invocations of native methods involved in I/O operations. Out of the four chosen tools, we were able to install and effectively test only two of them: <literal>Chord</literal> and <literal>ThreadSafe</literal>; the results corresponding to <literal>GoodLock</literal> and <literal>Sherlock</literal> come from [<link linkend="ch08_b12">12</link>] because we were not able to get the sources of the tools and run our new programs (*). We also had problems in testing <literal>Chord</literal> with some of the examples in the benchmarks, perhaps due to some misconfigurations, that we were not able to solve because <literal>Chord</literal> has been discontinued.</para>
<fig id="tab8_1" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Table 8.1</label>
<caption><title>Comparison with different deadlock detection tools. The inner cells show the number of deadlocks detected by each tool. The output labelled &#8220;(*)&#8221; are related to modified versions of the original programs: see the text</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/tab001.jpg" mime-subtype="jpeg"/>
</fig>
<para>The first block of programs belongs to a well-known group used as benchmarks for several <literal>Java</literal> analysis tools. In its current state <literal>JaDA</literal> only detects 1 deadlock in all of the four analyzed programs from this group. It gives responses that are similar to <literal>ThreadSafe</literal> and <literal>Chord</literal> (<literal>ThreadSafe</literal> appears a bit more imprecise on Sor). The programs in the second block corresponds to examples designed to test our tool against complex deadlock scenarios like the <literal>Network</literal> program. We notice that both <literal>Chord</literal> and <literal>ThreadSafe</literal> fail to detect those kinds of deadlocks. The third group reports the analysis of two examples of <literal>Scala</literal> programs [<link linkend="ch08_b4">4</link>]. These programs have been compiled with the <literal>Scala</literal> compiler <emphasis role="roman">2</emphasis><emphasis>.</emphasis><emphasis role="roman">11</emphasis> whose target is <literal>Java</literal> bytecode. We remark that, to the best of our knowledge, at the moment of writing this chapter, there is no static deadlock analysis tools for such language (for this reason the entries corresponding to the other tools are empty).</para>
<para>We think that the results in <link linkend="tab8_1">Table <xref linkend="tab8_1" remap="8.1"/></link> are encouraging and we hope to deliver more convincing ones as soon as <literal>JaDA</literal> overcomes its current limitations.</para>
</section>
<section class="lev1" id="sec8-7" label="8.7" xreflabel="8.7">
<title>Conclusions</title>
<para><literal>JaDA</literal> is a static deadlock analysis tool that targets <literal>JVML</literal>. Therefore it supports the analysis of every compiled <literal>Java</literal> program, as well as, every programs written in languages that are also compiled in <literal>JVML</literal>, like <literal>Scala</literal>. The technique underlying <literal>JaDA</literal> uses a behavioral type system that abstract the main features of the programs with respect to the concurrent operations.</para>
<para><literal>JaDA</literal> is designed to run in an automatic fashion, meaning that the inference of the program type and the subsequent analysis could be done unassisted. Nevertheless, user intervention is possible and may enhance the precision of the analysis, for example in presence of native methods.</para>
<para>Even though the tool is still under development, we have been able to asses it by analyzing a set of <literal>Java</literal> and <literal>Scala</literal> programs. This contribution also reports a comparison between <literal>JaDA</literal>&#x2019;s results and those of existing deadlock analysis tools, amongst which is a commercial grade one. The results obtained so far are very promising and we expect to gain more precision as the development continues.</para>
</section>
<section class="lev1" id="sec2a">
<title>References</title>
<orderedlist numeration="arabic" continuation="restarts" spacing="normal">
<listitem><para><anchor id="ch08_b1"/>N. Kobayashi and C. Laneve, &#8220;Deadlock analysis of unbounded process networks,&#8221; <emphasis>Information and Computation</emphasis>, vol. 252, pp. 48&#x2013;70, 2017. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=N%2E+Kobayashi+and+C%2E+Laneve%2C+%22Deadlock+analysis+of+unbounded+process+networks%2C%22+Information+and+Computation%2C+vol%2E+252%2C+pp%2E+48-70%2C+2017%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch08_b2"/>E. Giachino, N. Kobayashi, and C. Laneve, &#8220;Deadlock analysis of unbounded process networks,&#8221; in <emphasis>Proceedings of 25th International Conference on Concurrency Theory CONCUR 2014</emphasis>, vol. 8704 of <emphasis>Lecture Notes in Computer Science</emphasis>, pp. 63&#x2013;77, Springer, 2014. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=E%2E+Giachino%2C+N%2E+Kobayashi%2C+and+C%2E+Laneve%2C+%22Deadlock+analysis+of+unbounded+process+networks%2C%22+in+Proceedings+of+25th+International+Conference+on+Concurrency+Theory+CONCUR+2014%2C+vol%2E+8704+of+Lecture+Notes+in+Computer+Science%2C+pp%2E+63-77%2C+Springer%2C+2014%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch08_b3"/>J. Gosling, W. N. Joy, and G. L. S. Jr., <emphasis>The Java Language Specification</emphasis>. Addison-Wesley, 1996.</para></listitem>
<listitem><para><anchor id="ch08_b4"/>M. Odersky and al., &#8220;An Overview of the Scala Programming Language,&#8221; Tech. Rep. IC/2004/64, EPFL, Lausanne, Switzerland, 2004. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=M%2E+Odersky+and+al%2E%2C+%22An+Overview+of+the+Scala+Programming+Language%2C%22+Tech%2E+Rep%2E+IC%2F2004%2F64%2C+EPFL%2C+Lausanne%2C+Switzerland%2C+2004%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch08_b5"/>E. Giachino and C. Laneve, &#8220;Deadlock detection in linear recursive programs,&#8221; in <emphasis>14th Int. School on Formal Methods for the Design of Computer, Communication, and Software Systems, SFM 2014</emphasis>, vol. 8483 of <emphasis>Lecture Notes in Computer Science</emphasis>, pp. 26&#x2013;64, Springer, 2014. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=E%2E+Giachino+and+C%2E+Laneve%2C+%22Deadlock+detection+in+linear+recursive+programs%2C%22+in+14th+Int%2E+School+on+Formal+Methods+for+the+Design+of+Computer%2C+Communication%2C+and+Software+Systems%2C+SFM+2014%2C+vol%2E+8483+of+Lecture+Notes+in+Computer+Science%2C+pp%2E+26-64%2C+Springer%2C+2014%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch08_b6"/>E. Giachino, C. Laneve, and M. Lienhardt, &#8220;A framework for deadlock detection in core ABS,&#8221; <emphasis>Software and Systems Modeling</emphasis>, vol. 15, no. 4, pp. 1013&#x2013;1048, 2016. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=E%2E+Giachino%2C+C%2E+Laneve%2C+and+M%2E+Lienhardt%2C+%22A+framework+for+deadlock+detection+in+core+ABS%2C%22+Software+and+Systems+Modeling%2C+vol%2E+15%2C+no%2E+4%2C+pp%2E+1013-1048%2C+2016%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch08_b7"/>A. Garcia and C. Laneve, &#8220;Deadlock detection of Java Bytecode.&#8221; A preliminary version is available at <literal>http://jada.cs.unibo.it/data/Doc/jada-draft-lncs.pdf</literal>, 2016.</para></listitem>
<listitem><para><anchor id="ch08_b8"/>E. Bruneton, &#8220;Asm 4.0 a java bytecode engineering library.&#8221; <literal>http: //download.forge.objectweb.org/asm/asm4-guide.pdf</literal>. Last accessed: 2016-12-03. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=E%2E+Bruneton%2C+%22Asm+4%2E0+a+java+bytecode+engineering+library%2E%22+http%3A+%2F%2Fdownload%2Eforge%2Eobjectweb%2Eorg%2Fasm%2Fasm4-guide%2Epdf%2E+Last+accessed%3A+2016-12-03%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch08_b9"/>A. Garcia, &#8220;Static analysis of concurrent programs based on behavioral type systems.&#8221; Available at <literal>http://jada.cs.unibo.it/data/Doc/Abel-Garcia-PhD-Thesis-draft.pdf</literal>, 2017.</para></listitem>
<listitem><para><anchor id="ch08_b10"/>A. Garcia and C. Laneve, &#8220;<literal>JaDA</literal> &#x2013; the <literal>Ja</literal>va <literal>D</literal>eadlock <literal>A</literal>nalyzer.&#8221; Available at <literal>http://jada.cs.unibo.it</literal>, 2016.</para></listitem>
<listitem><para><anchor id="ch08_b11"/>M. Naik, C. Park, K. Sen, and D. Gay, &#8220;Effective static deadlock detection,&#8221; in <emphasis>31st International Conference on Software Engineering (ICSE 2009)</emphasis>, pp. 386&#x2013;396, ACM, 2009. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=M%2E+Naik%2C+C%2E+Park%2C+K%2E+Sen%2C+and+D%2E+Gay%2C+%22Effective+static+deadlock+detection%2C%22+in+31st+International+Conference+on+Software+Engineering+%28ICSE+2009%29%2C+pp%2E+386-396%2C+ACM%2C+2009%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch08_b12"/>M. Eslamimehr and J. Palsberg, &#8220;Sherlock: scalable deadlock detection for concurrent programs,&#8221; in <emphasis>Proceedings of the 22nd International Symposium on Foundations of Software Engineering (FSE-22)</emphasis>, pp. 353&#x2013;365, ACM, 2014. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=M%2E+Eslamimehr+and+J%2E+Palsberg%2C+%22Sherlock%3A+scalable+deadlock+detection+for+concurrent+programs%2C%22+in+Proceedings+of+the+22nd+International+Symposium+on+Foundations+of+Software+Engineering+%28FSE-22%29%2C+pp%2E+353-365%2C+ACM%2C+2014%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch08_b13"/>S. Bensalem and K. Havelund, &#8220;Dynamic deadlock analysis of multi-threaded programs,&#8221; in <emphasis>in Hardware and Software Verification and Testing</emphasis>, vol. 3875 of <emphasis>Lecture Notes in Computer Science</emphasis>, pp. 208&#x2013;223, Springer, 2005. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=S%2E+Bensalem+and+K%2E+Havelund%2C+%22Dynamic+deadlock+analysis+of+multi-threaded+programs%2C%22+in+in+Hardware+and+Software+Verification+and+Testing%2C+vol%2E+3875+of+Lecture+Notes+in+Computer+Science%2C+pp%2E+208-223%2C+Springer%2C+2005%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch08_b14"/>R. Atkey and D. Sannella, &#8220;Threadsafe: Static analysis for java concurrency,&#8221; <emphasis>ECEASST</emphasis>, vol. 72, 2015. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=R%2E+Atkey+and+D%2E+Sannella%2C+%22Threadsafe%3A+Static+analysis+for+java+concurrency%2C%22+ECEASST%2C+vol%2E+72%2C+2015%2E" target="_blank">Google Scholar</ulink></para></listitem>
</orderedlist>
</section>
</chapter>
<chapter class="chapter" id="ch09" label="9" xreflabel="9">
<title>Type-Based Analysis of Linear Communications</title>
<para><emphasis role="strong">Luca Padovani</emphasis></para>
<para>Dipartimento di Informatica, Universit&#x00E0; di Torino, Italy</para>
<section class="lev2">
<title>Abstract</title>
<para>This chapter presents a tool called <literal>Hypha</literal> for the type-based analysis of processes that communicate on linear channels. We describe the specification language used to model the systems under analysis (Section 9.1) followed by the typing rules on which the tool is based in order to verify two properties of systems, <emphasis>deadlock freedom</emphasis> and <emphasis>lock freedom</emphasis> (Section 9.2). In the final part of the chaper we illustrate the expressiveness and the limitations of the tool discussing a number of examples inspired by representative communication patterns using in parallel computing (Section 9.3) and then discuss closely related work (Section 9.4). The tool can be downloaded from the author&#x2019;s home page, the type system has been described by Padovani [<link linkend="ch09_b18">18</link>] and the corresponding reconstruction algorithms by Padovani <emphasis>et al.</emphasis> [<link linkend="ch09_b19">19</link>, <link linkend="ch09_b20">20</link>].</para>
</section>
<section class="lev1" id="sec9-1" label="9.1" xreflabel="9.1">
<title>Language</title>
<para>The <literal>Hypha</literal> specification language is a mildly sugared variant of the linear <emphasis>&#x03C0;</emphasis>-calculus [<link linkend="ch09_b16">16</link>] whose grammar is shown in <link linkend="tab9_1">Table <xref linkend="tab9_1" remap="9.1"/></link>. It makes use of booleans, integers, an infinite set <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e193_01.jpg" mime-subtype="jpeg"/> of names, and comprises <emphasis>expressions</emphasis> and <emphasis>processes</emphasis>. The syntax shown here is somewhat simplified and tailored to the modeling of the examples discussed in this chapter. <literal>Hypha</literal> supports other forms that may be useful in the description of protocols with branching points and provide convenient syntactic sugar on top of those given in <link linkend="tab9_1">Table <xref linkend="tab9_1" remap="9.1"/></link>. The <literal>Hypha</literal> specification language is appropriate for modeling concurrent processes that exchange messages on private (or <emphasis>session</emphasis>) channels [<link linkend="ch09_b10">10</link>].</para>
<fig id="tab9_1" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Table 9.1</label>
<caption><title>Syntax of <literal>Hypha</literal> input language (partial)</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/tab002.jpg" mime-subtype="jpeg"/>
</fig>
<para>For simplicity, in the provided syntax expressions are limited to values and comprise booleans, integers, and names. In the examples we will also make use of a few binary operators (such as <literal>+</literal>) and relations (such as <literal>&lt;</literal>). Processes comprise the usual terms of the <emphasis>&#x03C0;</emphasis>-calculus. The term <literal>{ }</literal> models the idle process that performs no actions. The term <emphasis>u</emphasis><literal>!(</literal><emphasis>e</emphasis><subscript>1</subscript>,..., <emphasis>e</emphasis><emphasis><subscript>n</subscript></emphasis><literal>)</literal> models a process that outputs the tuple <literal>(</literal><emphasis>e</emphasis><subscript>1</subscript>,..., <emphasis>e</emphasis><emphasis><subscript>n</subscript></emphasis><literal>)</literal> on the channel <emphasis>u</emphasis>. We omit the parentheses when <emphasis>n</emphasis> is 1 and write, for example, <emphasis>u</emphasis><literal>!</literal><emphasis>n</emphasis> in place of <emphasis>u</emphasis><literal>!(</literal><emphasis>n</emphasis><literal>)</literal>. We consider two forms of input processes. The term <emphasis>u</emphasis><literal>?(</literal><emphasis>x</emphasis><subscript>1</subscript>,..., <emphasis>x</emphasis><emphasis><subscript>n</subscript></emphasis><literal>).</literal><emphasis>P</emphasis> models an <emphasis>ephemeral input process</emphasis> that waits for <emphasis>one</emphasis> message from <emphasis>u</emphasis>, which is supposed to be an <emphasis>n</emphasis>-tuple, and then executes <emphasis>P</emphasis> where <emphasis>x</emphasis><emphasis><subscript>i</subscript></emphasis> is replaced by the <emphasis>i</emphasis>-th component of the tuple. The term <literal>*</literal><emphasis>u</emphasis><literal>?(</literal><emphasis>x</emphasis><subscript>1</subscript>,..., <emphasis>x</emphasis><emphasis><subscript>n</subscript></emphasis><literal>).</literal><emphasis>P</emphasis> models a <emphasis>persistent input process</emphasis> (also called <emphasis>service</emphasis>) that waits for an arbitrary number of messages. Each time a message is received, a new copy of <emphasis>P</emphasis> (with the variables <emphasis>x</emphasis><emphasis><subscript>i</subscript></emphasis> suitably instantiated) is spawned and the service makes itself available again for further receptions. The term <literal>new</literal> <emphasis>u</emphasis><subscript>1</subscript>,..., <emphasis>u</emphasis><emphasis><subscript>n</subscript></emphasis> <literal>in</literal> <emphasis>P</emphasis> models a process creating new channels <emphasis>u</emphasis><subscript>1</subscript>,..., <emphasis>u</emphasis><emphasis><subscript>n</subscript></emphasis> with scope <emphasis>P</emphasis>. As usual in the <emphasis>&#x03C0;</emphasis>-calculus, the scope of a channel may broaden as a result of communications (scope extrusion). The terms <literal>if</literal> <emphasis>e</emphasis> <literal>then</literal> <emphasis>P</emphasis> <literal>else</literal> <emphasis>Q</emphasis> and <emphasis>P</emphasis> <literal>|</literal> <emphasis>Q</emphasis> respectively model conditional and parallel processes <emphasis>P</emphasis> and <emphasis>Q</emphasis>. Finally, <literal>{</literal><emphasis>P</emphasis><literal>}</literal> represents the same process as <emphasis>P</emphasis> and is useful to disambiguate the way processes are grouped. The notions of <emphasis>free</emphasis> and <emphasis>bound names</emphasis> of a process <emphasis>P</emphasis>, respectively denoted by <emphasis role="romanAlt">fn</emphasis>(<emphasis>P</emphasis>)and <emphasis role="romanAlt">bn</emphasis>(<emphasis>P</emphasis>), are as expected.</para>
<fig id="lis9_1">
<label>Listing 9.1</label>
<caption><title>Modeling of the recursive Fibonacci function.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog096.jpg" mime-subtype="jpeg"/>
</fig>
<para><emphasis role="strong">Example 1</emphasis> (recursive Fibonacci function)<emphasis role="strong">.</emphasis> <link linkend="list9_1">Listing <xref linkend="list9_1" remap="9.1"/></link> shows the modeling of a service that computes the <emphasis>n</emphasis>-th number in the Fibonacci sequence. The service waits for invocations on channel <literal>fibo</literal>, each invocation consisting of the number <emphasis>n</emphasis> and a channel <emphasis>c</emphasis> on which the <emphasis>n</emphasis>-th Fibonacci number will be sent. The body of the service closely follows the familiar structure of the recursive definition of the Fibonacci sequence. When <emphasis>n</emphasis> &#x2264; 0 the answer is immediately sent over <emphasis>c</emphasis>. When <emphasis>n</emphasis> &gt; 0, two new channels <emphasis>a</emphasis> and <emphasis>b</emphasis> are created, the service invokes itself twice to compute the (<emphasis>n</emphasis>&#x2212;1)-th and (<emphasis>n</emphasis>&#x2212;2)-th numbers in the sequence, and then the sum of the two partial results is sent over <emphasis>c</emphasis>.</para>
<para>As usual, the operational semantics of the language is defined in terms of a <emphasis>structural congruence relation</emphasis>, which identifies terms that are meant to have the same semantics, and a <emphasis>reduction relation</emphasis> that defines the proper computation steps. We omit the formal definition of structural congruence, which is essentially the same as in the <emphasis>&#x03C0;</emphasis>-calculus and includes commutativity and associativity laws for parallel composition and the usual laws for shrinking and extending the scope of channels. The second one is the least relation defined by the rules in <link linkend="tab9_2">Table <xref linkend="tab9_2" remap="9.2"/></link> and closed by structural congruence and under the following <emphasis>reduction contexts</emphasis>:</para>
<para><emphasis role="strong">Reduction context</emphasis> <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e195_01.jpg" mime-subtype="jpeg"/></para>
<para>The fully-fledged formalization of the language also includes an evaluation relation for compound expressions [<link linkend="ch09_b18">18</link>].</para>
<para>To formulate the properties enforced by our typing discipline we introduce a few predicates that describe the pending communications of a process</para>
<fig id="tab9_2" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Table 9.2</label>
<caption><title>Operational semantics of processes</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/tab003.jpg" mime-subtype="jpeg"/>
</fig>
<para><emphasis>P</emphasis> with respect to some channel <emphasis>a</emphasis>. We use the obvious extension of bound names we have introduced for processes to reduction contexts:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e196_01.jpg" mime-subtype="jpeg"/></para>
<para>In words, <emphasis role="romanAlt">in</emphasis>(<emphasis>a</emphasis>, <emphasis>P</emphasis>) holds if there is a sub-process <emphasis>Q</emphasis> within <emphasis>P</emphasis> that is waiting for a message on channel <emphasis>a</emphasis>. Note that, by definition of reduction context, the input cannot be guarded by other actions. The condition <emphasis>a</emphasis> &#x2209; <emphasis role="romanAlt">bn</emphasis>(<literal>C</literal>) implies that <emphasis>a</emphasis> is not captured by a binder in <literal>C</literal>, <emphasis>i.e.</emphasis> it occurs free in <emphasis>P</emphasis>. The predicates <emphasis role="romanAlt">out</emphasis>(<emphasis>a</emphasis>, <emphasis>P</emphasis>) and <literal>*</literal><emphasis role="romanAlt">in</emphasis>(<emphasis>a</emphasis>, <emphasis>P</emphasis>) are similar, but they regard outputs and persistent inputs, respectively. Therefore, when <emphasis role="romanAlt">in</emphasis>(<emphasis>a</emphasis>, <emphasis>P</emphasis>) holds it means that there is a pending ephemeral input on <emphasis>a</emphasis> and when <emphasis role="romanAlt">out</emphasis>(<emphasis>a</emphasis>, <emphasis>P</emphasis>) holds it means that there is a pending output on <emphasis>a</emphasis>. Then, <emphasis role="romanAlt">sync</emphasis>(<emphasis>a</emphasis>, <emphasis>P</emphasis>) means that there are pending input/output operations on <emphasis>a</emphasis>, but a synchronization on <emphasis>a</emphasis> is <emphasis>immediately</emphasis> possible. On the contrary, <emphasis role="romanAlt">wait</emphasis>(<emphasis>a</emphasis>, <emphasis>P</emphasis>) means that there is a pending output or a pending ephemeral input on <emphasis>a</emphasis>, but no immediate synchronization on <emphasis>a</emphasis> is possible. Note the asymmetry in the way pending inputs and outputs trigger the <emphasis role="romanAlt">wait</emphasis> predicate. We do not interpret <literal>*</literal><emphasis role="romanAlt">in</emphasis>(<emphasis>a</emphasis>, <emphasis>P</emphasis>) as a pending input operation, meaning that we do not require a persistent input process to run infinitely often. At the same time, <emphasis>any</emphasis> pending output triggers the <emphasis role="romanAlt">wait</emphasis> predicate, even when the output represents a service invocation.</para>
<para>We say that a process <emphasis>P</emphasis> is deadlock free if every residual of <emphasis>P</emphasis> that cannot reduce further contains no pending communications. Formally:</para>
<para><emphasis role="strong">Definition 9.1.</emphasis> <emphasis>P</emphasis> is <emphasis>deadlock free</emphasis> if whenever <emphasis>P</emphasis> <literal>&#x2192;</literal><sup>*</sup> <literal>new</literal> <emphasis>c</emphasis><subscript>1</subscript>,..., <emphasis>c</emphasis><emphasis><subscript>n</subscript></emphasis> <literal>in</literal> <emphasis>Q</emphasis> &#x219B; we have &#x00AC;<emphasis role="romanAlt">wait</emphasis>(<emphasis>a</emphasis>, <emphasis>Q</emphasis>) for every <emphasis>a</emphasis>.</para>
<para>We say that a process <emphasis>P</emphasis> is lock free if every residual <emphasis>Q</emphasis> of <emphasis>P</emphasis> in which there are pending communications can reduce further to a state in which such operations complete. Formally:</para>
<para><emphasis role="strong">Definition 9.2.</emphasis> <emphasis>P</emphasis> is <emphasis>lock free</emphasis> if whenever <emphasis>P</emphasis> <literal>&#x2192;</literal><sup>*</sup> <literal>new</literal> <emphasis>c</emphasis><subscript>1</subscript>,..., <emphasis>c</emphasis><emphasis><subscript>n</subscript></emphasis> <literal>in</literal> <emphasis>Q</emphasis> and <emphasis role="romanAlt">wait</emphasis>(<emphasis>a</emphasis>, <emphasis>Q</emphasis>) there is <emphasis>R</emphasis> such that <emphasis>Q</emphasis> <literal>&#x2192;</literal><sup>*</sup> <emphasis>R</emphasis> and <emphasis role="romanAlt">sync</emphasis>(<emphasis>a</emphasis>, <emphasis>R</emphasis>).</para>
<para>In Definitions 9.1 and 9.2, it is important to universally quantifiy over the topmost channel restrictions in a residual of <emphasis>P</emphasis> so that the notion of (dead)lock freedom for <emphasis>P</emphasis> concerns both free and bound channels of <emphasis>P</emphasis>.</para>
<para>It is easy to prove that lock freedom implies deadlock freedom [<link linkend="ch09_b19">19</link>]. On the other hand, there exist deadlock-free processes that are not lock free, as shown in the example below.</para>
<para><emphasis role="strong">Example 2</emphasis> (deadlock-free, locked process)<emphasis role="strong">.</emphasis> The process</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e197_01.jpg" mime-subtype="jpeg"/></para>
<para>is deadlock free but not lock free. Indeed, the process reduces forever, but no input operation is ever performed on the <emphasis>c</emphasis> channel. As a result, the pending output on <emphasis>c</emphasis> cannot be completed.</para>
</section>
<section class="lev1" id="sec9-2" label="9.2" xreflabel="9.2">
<title>Type System</title>
<para>In this section we describe a type system that enforces the properties introduced in Section 9.1: well-typed processes are guaranteed to be (dead)lock free. The tool <literal>Hypha</literal> then implements a type reconstruction algorithm for this type system and finds a typing derivation for a given process, provided there is one. Note that, while the type reconstruction algorithm is complete with respect to the type system, the type system itself is not complete with respect to (dead)lock freedom: there exist (dead)lock free processes that are ill typed according to the type system. In fact, it is undecidable in general to establish whether a <emphasis>&#x03C0;</emphasis>-calculus process is (dead)lock free, hence the type system is necessarily conservative.</para>
<para>Polarities, qualifiers, and types are defined by the following grammar:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e197_02.jpg" mime-subtype="jpeg"/></para>
<para>Types comprise the base types <literal>bool</literal> and <literal>int</literal> of boolean and integer values, channel types <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e197_03.jpg" mime-subtype="jpeg"/>, and the usual forms &#x03B1; and <emphasis>&#x03BC;</emphasis>&#x03B1;<literal>.</literal><emphasis>t</emphasis> for representing recursive types. A channel type <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e197_03.jpg" mime-subtype="jpeg"/> consists of:</para>
<itemizedlist mark="bullet" spacing="normal">
<listitem><para>A <emphasis>polarity p</emphasis> specifying the operations allowed on the channel: &#x2205; means none, <literal>{</literal><literal>?</literal><literal>}</literal> means input, <literal>{</literal><literal>!</literal><literal>}</literal> means output, and <literal>{</literal><literal>?</literal>, <literal>!</literal><literal>}</literal> means both. We will abbreviate <literal>{</literal><literal>?</literal>, <literal>!</literal><literal>}</literal> with <literal>#</literal> and <literal>{</literal><literal>?</literal><literal>}</literal> and <literal>{</literal><literal>!</literal><literal>}</literal> with <literal>?</literal> and <literal>!</literal>, respectively.</para></listitem>
<listitem><para>A sequence <emphasis>t</emphasis><subscript>1</subscript>,..., <emphasis>t</emphasis><emphasis><subscript>n</subscript></emphasis> of types, abbreviated as <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e197_04.jpg" mime-subtype="jpeg"/>, specifying that each message exchanged over the channel is an <emphasis>n</emphasis>-tuple of values where the <emphasis>i</emphasis>-th value has type <emphasis>t</emphasis><emphasis><subscript>i</subscript></emphasis>.</para></listitem>
<listitem><para>A <emphasis>qualifier</emphasis> <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e197_05.jpg" mime-subtype="jpeg"/> specifying how many times the channel can or must be used according to its polarity. The qualifier <literal>*</literal> means that the channel can be used any number of times. A qualifier of the form <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e198_01.jpg" mime-subtype="jpeg"/> means that the channel can only be used once. In this case, <emphasis>h</emphasis> and <emphasis>k</emphasis> are respectively the <emphasis>level</emphasis> and the <emphasis>tickets</emphasis> associated with the channel: channels with smaller levels must be used <emphasis>before</emphasis> channels with greater levels; a channel with <emphasis>k</emphasis> tickets can be sent as a message on another channel at most <emphasis>k</emphasis> times.</para></listitem>
</itemizedlist>
<para>We require that, in a recursive type <emphasis>&#x03BC;</emphasis>&#x03B1;<literal>.</literal><emphasis>t</emphasis>, the type variable &#x03B1; can only occur guarded by a channel type constructor. For example, <emphasis>&#x03BC;</emphasis>&#x03B1;<literal>.</literal>&#x03B1; is illegal while <emphasis>&#x03BC;</emphasis>&#x03B1;<literal>.?[</literal>&#x03B1;<literal>]</literal><sup>*</sup> is allowed. We identify two types modulo renaming of bound type variables and if they have the same infinite unfolding, that is if they denote the same (regular) tree [<link linkend="ch09_b4">4</link>]. In particular, we have <emphasis>&#x03BC;</emphasis>&#x03B1;<literal>.</literal><emphasis>t</emphasis> = <emphasis>t</emphasis><literal>{</literal><emphasis>&#x03BC;</emphasis>&#x03B1;<literal>.</literal><emphasis>t</emphasis>/&#x03B1;<literal>}</literal>.</para>
<para>Qualifiers distinguish <emphasis>service channels</emphasis> (with qualifier <literal>*</literal>) from <emphasis>linear channels</emphasis> (with qualifiers of the form <emphasis>h</emphasis>, <emphasis>k</emphasis>). Service channels are used for modeling persistent services, such as <literal>fibo</literal> in <link linkend="list9_1">Listing <xref linkend="list9_1" remap="9.1"/></link> and <literal>forever</literal> in Example 2. Linear channels are used for modeling private communications between pairs of processes. Examples of linear channels are <emphasis>a</emphasis> and <emphasis>b</emphasis> in <link linkend="list9_1">Listing <xref linkend="list9_1" remap="9.1"/></link> and <emphasis>c</emphasis> in Example 2. The fact that a linear channel can be used for one communication only is not a limitation in practice. Structured private conversations made of arbitrarily many communications can be encoded using a continuation-passing style [<link linkend="ch09_b5">5</link>, <link linkend="ch09_b12">12</link>]. We will see several examples of this technique at work in the rest of the chapter. On the other hand, knowing that a channel is linear provides some guarantees on the fact that the channel will not be discarded without being used. This is a necessary (although not sufficient) condition for guaranteeing that communications on linear channels eventually occur.</para>
<para>The level of a linear channel measures the urgency with which the channel must be used: the lower the level is, the sooner the channel must be used. We extend this notion from linear channels to arbitrary types. To compute the level of a type, we define an auxiliary function <literal>|&#x00B7;|</literal> such that <literal>|</literal><emphasis>t</emphasis><literal>|</literal> is an element of <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e198_01a.jpg" mime-subtype="jpeg"/> where <literal>&#x22A5;</literal> &lt; <emphasis>n</emphasis> &lt; &#x22A4; for every <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e198_02.jpg" mime-subtype="jpeg"/>:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e198_03.jpg" mime-subtype="jpeg"/></para>
<para>According to this definition, service channels with input capability have the lowest level <literal>&#x22A5;</literal> (first equation). This way we guarantee input receptiveness of services, for the use of a service channel with input capability cannot be postponed by any means. Base values, service channels with output capability, and linear channels with no capabilities have the highest level &#x22A4; (last equation) because they do not affect (dead)lock freedom in any way. Linear channels with non-empty polarity must be used according to their level (second equation). We say that a (value with) type <emphasis>t</emphasis> is <emphasis>unlimited</emphasis> if <literal>|</literal><emphasis>t</emphasis><literal>|</literal> = &#x22A4;, that it is <emphasis>linear</emphasis> if <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e198_02a.jpg" mime-subtype="jpeg"/>, that itis <emphasis>relevant</emphasis> if <literal>|</literal><emphasis>t</emphasis><literal>|</literal> = <literal>&#x22A5;</literal>.</para>
<para>We define another auxiliary function <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e199_01.jpg" mime-subtype="jpeg"/> to <emphasis>shift</emphasis> levels and tickets: <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e199_01.jpg" mime-subtype="jpeg"/> is the same as <emphasis>t</emphasis> except when <emphasis>t</emphasis> is a linear channel. In this case, the level/tickets in <emphasis>t</emphasis> are transposed by <emphasis>h</emphasis> and <emphasis>k</emphasis> respectively. Formally:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e199_02.jpg" mime-subtype="jpeg"/></para>
<para>Note that positive/negative shifting of levels corresponds to decreasing/increasing the urgency with which a value of a given type must be used.</para>
<para>The type system makes use of <emphasis>type environments</emphasis> &#x0393; to keep track of the type of names occurring free in processes. A type environment is a finite map from names to types written <emphasis>u</emphasis><subscript>1</subscript> : <emphasis>t</emphasis><subscript>1</subscript>,..., <emphasis>u</emphasis><emphasis><subscript>n</subscript></emphasis> : <emphasis>t</emphasis><emphasis><subscript>n</subscript></emphasis>. We write <emphasis role="romanAlt">dom</emphasis>(&#x0393;) for the <emphasis>domain</emphasis> of &#x0393;, namely the set of names for which there is an association in &#x0393;, and &#x0393;, &#x0393;<sup>&#x2032;</sup> for the union of &#x0393; and &#x0393;<sup>&#x2032;</sup> when <emphasis role="romanAlt">dom</emphasis>(&#x0393;) <literal>&#x2229;</literal> <emphasis role="romanAlt">dom</emphasis>(&#x0393;<sup>&#x2032;</sup>) = &#x2205;. We also need a more general way of composing type environments that takes into account the level and tickets of linear channel types and the fact that we can split channel types by distributing different capabilities to different processes. Following [<link linkend="ch09_b16">16</link>], we define a partial operator + between types, thus:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e199_03.jpg" mime-subtype="jpeg"/></para>
<para>Informally, unlimited types combine with themselves without restrictions. The combination of two unlimited/relevant channel types has the union of their polarities. Two linear channel types can be combined only if they have the same level and disjoint polarities, and their combination has the union of their polarities and the sum of their tickets. We extend the partial operator + to type environments:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e199_04.jpg" mime-subtype="jpeg"/></para>
<para>Note that &#x0393; + &#x0393;<sup>&#x2032;</sup> is undefined if there is <emphasis>u</emphasis> <literal>&#x2208;</literal> <emphasis role="romanAlt">dom</emphasis>(&#x0393;) <literal>&#x2229;</literal> <emphasis role="romanAlt">dom</emphasis>(&#x0393;<sup>&#x2032;</sup>) such that &#x0393;(<emphasis>u</emphasis>) + &#x0393;<sup>&#x2032;</sup>(<emphasis>u</emphasis>) is undefined and that <emphasis role="romanAlt">dom</emphasis>(&#x0393; + &#x0393;<sup>&#x2032;</sup>) = <emphasis role="romanAlt">dom</emphasis>(&#x0393;) <literal>&#x222A;</literal> <emphasis role="romanAlt">dom</emphasis>(&#x0393;<sup>&#x2032;</sup>). We let |&#x0393;| denote the lowest level of the types in the range of &#x0393;, that is</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e200_01.jpg" mime-subtype="jpeg"/></para>
<para>We say that &#x0393; is unlimited if <literal>|</literal>&#x0393;<literal>|</literal> = &#x22A4;.</para>
<para>We now turn our attention to the typing rules, which are meant to enforce the following properties of channels:</para>
<orderedlist numeration="arabic" continuation="restarts" spacing="normal">
<listitem><para>a service channel with input capability must be used by a replicated input process (we refer to this condition as <emphasis>input receptiveness</emphasis>);</para></listitem>
<listitem><para>a linear channel cannot be discarded until both its input and output capabilities have been used (we refer to this condition as <emphasis>linearity</emphasis>);</para></listitem>
<listitem><para>an operation on a linear channel cannot block channels with lower or equal level (with linearity, this condition guarantees <emphasis>deadlock freedom</emphasis>);</para></listitem>
<listitem><para>the use of a linear channel cannot be postponed forever (with linearity and deadlock freedom, this condition guarantees <emphasis>lock freedom</emphasis>).</para></listitem>
</orderedlist>
<para>The typing rules allow us to derive judgments of the form &#x0393; &#x22A2; <emphasis>e</emphasis> : <emphasis>t</emphasis>, stating that <emphasis>e</emphasis> is well typed in the environment &#x0393; and has type <emphasis>t</emphasis>, and of the form &#x0393; &#x22A2;<emphasis><subscript>k</subscript></emphasis> <emphasis>P</emphasis>, stating that <emphasis>P</emphasis> is well typed in the environment &#x0393;. The parameter <emphasis>k</emphasis> <literal>&#x2208; {</literal>0, 1<literal>}</literal> intuitively represents the &#8220;cost&#8221; for sending a channel over another channel: each output operation consumes <emphasis>k</emphasis> tickets from the channels being sent. The type system is designed in such a way that a well typed, closed process <emphasis>P</emphasis> is guaranteed to be deadlock free if &#x00D8; <literal>&#x22A2;</literal><subscript>0</subscript> <emphasis>P</emphasis> and lock free if &#x00D8; <literal>&#x22A2;</literal><subscript>1</subscript> <emphasis>P</emphasis>.</para>
<para><emphasis role="strong">Expressions.</emphasis> Because the model has an extremely simple expression language, the corresponding typing rules, shown below, are fairly obvious and extend easily to more complex expressions:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e200_02.jpg" mime-subtype="jpeg"/></para>
<para>The important remark concerning these rules is that the type environment used for typing an expression <emphasis>e</emphasis> only contains associations for the free names occurring in <emphasis>e</emphasis>. This makes sure that no linear or relevant resource (namely, channels that must be used at least once) is left unused. Later on we will discuss a structural rule that allows us to discard resources whose use is not necessary in order to enforce (dead)lock freedom.</para>
<para><emphasis role="strong">Idle and grouped processes.</emphasis> Rule [<emphasis role="smaller">t</emphasis>-<emphasis role="smaller">idle</emphasis>] states that the idle process is well typed in the empty environment only:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e200_03.jpg" mime-subtype="jpeg"/></para>
<para>For example, the judgment <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e201_01.jpg" mime-subtype="jpeg"/> is not derivable, because the linear channel <emphasis>a</emphasis> is supposed to be used for one output operation, whereas the process <literal>{ }</literal> does nothing. This typing rule illustrates a key trait of the type system, making sure that linear channels with pending capabilities are not discarded. If this were not the case, one could write processes like</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e201_02.jpg" mime-subtype="jpeg"/></para>
<para>which are stuck waiting for messages that will never arrive.</para>
<para>The typing rule for a grouped process is simple and does not enforce any constraint other than typability of the process itself:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e201_03.jpg" mime-subtype="jpeg"/></para>
<para><emphasis role="strong">Outputs.</emphasis> Rule [<emphasis role="smaller">t</emphasis>-<emphasis role="smaller">out</emphasis>] is used for typing output operations on linear channels:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e201_04.jpg" mime-subtype="jpeg"/></para>
<para>First of all, the channel <emphasis>u</emphasis> being used for the output must indeed have capability <literal>!</literal>. The type of the message <emphasis>v</emphasis> must be <emphasis>t</emphasis> (as specified in the type of the channel <emphasis>u</emphasis>) except that its level is shifted by <emphasis>m</emphasis> and its tickets are shifted by <emphasis>k</emphasis>. The shifting of the level means that the level of <emphasis>t</emphasis> in ! <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e201_05.jpg" mime-subtype="jpeg"/> is relative to <emphasis>m</emphasis>. This, together with the side condition 0 &lt; <literal>|</literal><emphasis>t</emphasis><literal>|</literal>, makes sure that the level of <emphasis>v</emphasis> (the channel being sent as a message) is strictly greater than the level of <emphasis>u</emphasis>. The shifting of the tickets in <emphasis>t</emphasis> accounts for the fact that, by sending <emphasis>v</emphasis> as a message, one ticket from <emphasis>v</emphasis> is consumed. Note that this is necessary only in the judgments for lock freedom (<emphasis>k</emphasis> = 1). Below are a few examples:</para>
<itemizedlist mark="bullet" spacing="normal">
<listitem><para>The judgment <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e201_06.jpg" mime-subtype="jpeg"/> is derivable because <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e201_07.jpg" mime-subtype="jpeg"/>. Note in particular that the channel to be sent on <emphasis>a</emphasis> must have no tickets, which is in fact what happens to <emphasis>b</emphasis> after 1 ticket is consumed from its type before it travels on <emphasis>a</emphasis>.</para></listitem>
<listitem><para>The judgment <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e201_08.jpg" mime-subtype="jpeg"/> is not derivable because <emphasis>b</emphasis> has no tickets and so it cannot travel on <emphasis>a</emphasis>.</para></listitem>
<listitem><para>Let <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e201_09.jpg" mime-subtype="jpeg"/> and observe that <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e201_10.jpg" mime-subtype="jpeg"/>. The judgment <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e201_11.jpg" mime-subtype="jpeg"/> is not derivable, despite the message <emphasis>a</emphasis> has the &#8220;right&#8221; type <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e201_12.jpg" mime-subtype="jpeg"/>, because the condition 0 &lt; <literal>|</literal><emphasis>t</emphasis><literal>|</literal> = 0 is not satisfied. A process like <emphasis>a</emphasis><literal>!</literal><emphasis>a</emphasis> is deadlocked because the occurrence of <emphasis>a</emphasis> that is meant to be used for matching this output operation is the very message sent on <emphasis>a</emphasis> itself.</para></listitem>
<listitem><para>The judgment <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e202_01.jpg" mime-subtype="jpeg"/> is not derivable because <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e202_02.jpg" mime-subtype="jpeg"/>. A service channel with input capability such as <emphasis>b</emphasis> cannot be sent as a message to guarantee input receptiveness.</para></listitem>
</itemizedlist>
<para>Rule [<emphasis role="smaller">t</emphasis>-<emphasis role="smaller">out</emphasis>*] is used for typing outputs on unlimited channels.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e202_03.jpg" mime-subtype="jpeg"/></para>
<para>There are two key differences between [<emphasis role="smaller">t</emphasis>-<emphasis role="smaller">out</emphasis>] and [<emphasis role="smaller">t</emphasis>-<emphasis role="smaller">out</emphasis>*]. First, the condition <literal>&#x22A5;</literal> &lt; <literal>|</literal><emphasis>t</emphasis><literal>|</literal>,where <emphasis>t</emphasis> is the type of <emphasis>v</emphasis> means that only unlimited channels with input capability cannot be communicated. Second, the type of <emphasis>v</emphasis> does not need to match exactly the type <emphasis>t</emphasis> in the channel type of <emphasis>u</emphasis>, but its level can be shifted by an arbitrary amount <emphasis>n</emphasis>. This is the technical realization of polymorphism. In particular, each distinct output on <emphasis>u</emphasis> can shift the type of the message by a different amount, therefore allowing polymorphic recursion. We will often use this feature in the extended examples in the second half of this chapter.</para>
<para>Both [<emphasis role="smaller">t</emphasis>-<emphasis role="smaller">out</emphasis>] and [<emphasis role="smaller">t</emphasis>-<emphasis role="smaller">out</emphasis>*] generalize easily to an arbitrary number of message arguments. As an example, the former rule can be generalized as follows:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e202_04.jpg" mime-subtype="jpeg"/></para>
<para><emphasis role="strong">Inputs.</emphasis> Rule [<emphasis role="smaller">t</emphasis>-<emphasis role="smaller">in</emphasis>] is used for typing linear input operations:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e202_05.jpg" mime-subtype="jpeg"/></para>
<para>The channel <emphasis>u</emphasis> must have type <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e202_06.jpg" mime-subtype="jpeg"/> and the continuation <emphasis>P</emphasis> is typed in an environment where the input polarity of <emphasis>u</emphasis> has been removed and the received message <emphasis>x</emphasis> has been added. The level of the type of <emphasis>x</emphasis> is shifted by <emphasis>n</emphasis>, consistently with the relative interpretation of levels of message types that we have already discussed for [<emphasis role="smaller">t</emphasis>-<emphasis role="smaller">out</emphasis>]. The tickets of <emphasis>u</emphasis> are irrelevant since <emphasis>u</emphasis> is used for an input operation, not as the content of a message. The condition <emphasis>n</emphasis> &lt; <literal>|</literal>&#x0393;<literal>|</literal> ensures that the input on <emphasis>u</emphasis> does not block operations on other channels with equal or lower level. In particular, &#x0393; cannot contain service channels with input capability. Below are some typical examples of ill-typed processes that violate this condition:</para>
<itemizedlist mark="bullet" spacing="normal">
<listitem><para><inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e202_07.jpg" mime-subtype="jpeg"/> is not derivable because 1 &#x226E; 0: the input on <emphasis>a</emphasis> blocks the output on <emphasis>b</emphasis>, but <emphasis>b</emphasis> has lower level than <emphasis>a</emphasis>;</para></listitem>
<listitem><para><inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e203_01.jpg" mime-subtype="jpeg"/> is a degenerate case of the previous example, where the input on <emphasis>a</emphasis> blocks the very output that should synchronize with it. Note that this process is well-typed in the traditional linear <emphasis>&#x03C0;</emphasis>-calculus [<link linkend="ch09_b16">16</link>].</para></listitem>
<listitem><para><inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e203_02.jpg" mime-subtype="jpeg"/> is not derivable because <literal>|?[int]</literal><sup>*</sup><literal>|</literal> = <literal>&#x22A5;</literal>. To guarantee input receptiveness, we require that replicated inputs cannot be guarded by other operations.</para></listitem>
</itemizedlist>
<para>Rule [<emphasis role="smaller">t</emphasis>-<emphasis role="smaller">in</emphasis>*] is used for typing replicated input operations corresponding to persistent services:</para>
<inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e203_03.jpg" mime-subtype="jpeg"/>
<para>This rule differs from [<emphasis role="smaller">t</emphasis>-<emphasis role="smaller">in</emphasis>] in three important ways. First of all, <emphasis>u</emphasis> must be a service channel with input capability. Second, the side condition <literal>&#x22A4; &#x2264; |</literal>&#x0393;<literal>|</literal> makes sure that the environment &#x0393; used for typing the body of the service is unlimited. This is because the service can be invoked an arbitrary number of times, hence its body cannot contain linear resources. Third, it may be the case that <emphasis>u</emphasis> <literal>&#x2208;</literal> <emphasis role="romanAlt">dom</emphasis>(&#x0393;), because <literal>?[</literal><emphasis>t</emphasis><literal>]</literal><sup>*</sup> + <literal>![</literal><emphasis>t</emphasis><literal>]</literal><sup>*</sup> = <literal>#[</literal><emphasis>t</emphasis><literal>]</literal><sup>*</sup> according to (9.3) and <literal>![</literal><emphasis>t</emphasis><literal>]</literal><sup>*</sup> is unlimited. This means that services may recursively invoke themselves. We use this feature in several examples, including Example 1.</para>
<para>As for [<emphasis role="smaller">t</emphasis>-<emphasis role="smaller">out</emphasis>] and [<emphasis role="smaller">t</emphasis>-<emphasis role="smaller">out</emphasis>*], both [<emphasis role="smaller">t</emphasis>-<emphasis role="smaller">in</emphasis>] and [<emphasis role="smaller">t</emphasis>-<emphasis role="smaller">in</emphasis>*] can be easily generalized to handle arbitrary tuples of message arguments.</para>
<para><emphasis role="strong">Conditional and parallel processes.</emphasis> The typing rule for conditional processes is shown below:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e203_04.jpg" mime-subtype="jpeg"/></para>
<para>As usual, the condition must have type <literal>bool</literal> and + is used for combining the type environments used in different parts of the process. Note that both branches must be typable using the same type environment, meaning that the linear channels occurring in <emphasis>P</emphasis> and <emphasis>Q</emphasis> must be used in the same order. For example, the judgment</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e203_05.jpg" mime-subtype="jpeg"/></para>
<para>is not derivable because the <literal>else</literal> branch uses the two linear channels <emphasis>a</emphasis> and <emphasis>b</emphasis> in an order not allowed by their levels.</para>
<para>The typing rule for parallel compositions is shown below:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e204_01.jpg" mime-subtype="jpeg"/></para>
<para>Because of the definition of the + operator, which combines the types of linear channels only provided that such channels have the same level, the order in which linear channels are used in the branches of the parallel composition must be consistent. For example, the judgment</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e204_02.jpg" mime-subtype="jpeg"/></para>
<para>cannot be derived because the second branch violates the side condition of [<emphasis role="smaller">t</emphasis>-<emphasis role="smaller">in</emphasis>] requiring <emphasis>b</emphasis> to have a strictly smaller level than <emphasis>a</emphasis>. Indeed, the whole process is deadlocked.</para>
<para><emphasis role="strong">Channel creation.</emphasis> Restrictions can be used to introduce both linear and service channels. In the former case, the typing rule is</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e204_03.jpg" mime-subtype="jpeg"/></para>
<para>Note that the rule &#8220;guesses&#8221; the right level and number of tickets that are necessary for typing <emphasis>P</emphasis>. The polarity is either <literal>#</literal>, meaning that <emphasis>a</emphasis> must be used for both one input and one output operation in <emphasis>P</emphasis>, or <literal>0</literal>, meaning that <emphasis>a</emphasis> is a depleted channel that is not supposed to be used at all in <emphasis>P</emphasis>. The reason why the typing rule accounts for this possibility is purely technical and is necessary to prove that process reductions preserve typing [<link linkend="ch09_b18">18</link>].</para>
<para>The typing rule for introducing a service channel is essentially the same:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e204_04.jpg" mime-subtype="jpeg"/></para>
<para>Unlike linear channels, the capability of restricted unlimited channels is always <literal>#</literal>. Since an unlimited channel <emphasis>a</emphasis> with input capability must be used (<emphasis>cf.</emphasis>[<emphasis role="smaller">t</emphasis>-<emphasis role="smaller">in</emphasis>*]), this guarantees that there is always a service waiting for invocations on <emphasis>a</emphasis>. On the other hand, a service channel with output capability does not have to be used (<emphasis>cf.</emphasis> (9.1)), therefore imposing that the capability of <emphasis>a</emphasis> is <literal>#</literal> does not mandate invocations on <emphasis>a</emphasis>.</para>
<para><emphasis role="strong">Unused resources.</emphasis> The following structural rule provides a limited form of weakening whereby it is possible to add unused resources in a type environment, provided that these resources have an unlimited type:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e205_01.jpg" mime-subtype="jpeg"/></para>
<para>For example, both <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e205_02.jpg" mime-subtype="jpeg"/> and <emphasis>x</emphasis> : <literal>int</literal> <literal>&#x22A2;</literal><emphasis><subscript>k</subscript></emphasis> <literal>{ }</literal> are derivable, because the type environments only contain resources that impose no usage and therefore can be discarded using [<emphasis role="smaller">t</emphasis>-<emphasis role="smaller">weak</emphasis>]. On the contrary, neither <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e205_03.jpg" mime-subtype="jpeg"/> nor <emphasis>a</emphasis> : <literal>?[int]</literal><sup>*</sup> <literal>&#x22A2;</literal><emphasis><subscript>k</subscript></emphasis> are derivable.</para>
<para>The type system refines the one for the linear <emphasis>&#x03C0;</emphasis>-calculus [<link linkend="ch09_b16">16</link>], hence all the properties of the linear <emphasis>&#x03C0;</emphasis>-calculus (partial confluence, linear usage of channels, etc.) are still guaranteed. The added value is that the type system also guarantees deadlock/lock freedom.</para>
<para><emphasis role="strong">Theorem 9.3.</emphasis> <emphasis>The following properties hold:</emphasis></para>
<orderedlist numeration="arabic" continuation="restarts" spacing="normal">
<listitem><para><emphasis>If</emphasis> &#x2205; <literal>&#x22A2;</literal><subscript>0</subscript> <emphasis>P, then P is deadlock free.</emphasis></para></listitem>
<listitem><para><emphasis>If</emphasis> &#x2205; <literal>&#x22A2;</literal><subscript>1</subscript> <emphasis>P, then Pis lock free.</emphasis></para></listitem>
</orderedlist>
<para><emphasis role="strong">Example 3</emphasis> (recursive Fibonacci function)<emphasis role="strong">.</emphasis> Let <emphasis>P</emphasis> be the process shown in <link linkend="list9_1">Listing <xref linkend="list9_1" remap="9.1"/></link>. Then it is possible to derive</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e205_04.jpg" mime-subtype="jpeg"/></para>
<para>if and only if <emphasis>k</emphasis> = 0. It is not possible to find a derivation for <emphasis>k</emphasis> = 1 since the type system cannot establish an upper bound to the time after which the continuation channel <emphasis>c</emphasis> will be used in an invocation <literal>fibo!(</literal><emphasis>n</emphasis><literal>,</literal><emphasis>c</emphasis><literal>)</literal>. In fact, such upper bound depends on <emphasis>n</emphasis> and on the fact that the recursion of the <literal>fibo</literal> service is well-founded This latter property requires a kind of analysis that eludes the capabilities of the type system.</para>
</section>
<section class="lev1" id="sec9-3" label="9.3" xreflabel="9.3">
<title>Extended Examples</title>
<section class="lev2" id="sec9-3-1" label="9.3.1" xreflabel="9.3.1">
<title>Fibonacci Stream Network</title>
<para>In this section we discuss an alternative modeling of system that computes the sequence of Fibonacci numbers and that is an example of <emphasis>stream network</emphasis>, that is a network of communicating processes that exchange infinite sequences (streams) of messages. <link linkend="fig9_1">Figure <xref linkend="fig9_1" remap="9.1"/></link> depicts the Fibonacci stream network [<link linkend="ch09_b8">8</link>, <link linkend="ch09_b22">22</link>] where the boxes represent processing units and the arrows represent communication channels.</para>
<fig id="fig9_1" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 9.1</label>
<caption><title>Graphical representation of the Fibonacci stream network [<link linkend="ch09_b8">8</link>, <link linkend="ch09_b22">22</link>].</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig058.jpg" mime-subtype="jpeg"/>
</fig>
<para>Each <literal>source(</literal><emphasis>n</emphasis><literal>)</literal> process sends <emphasis>n</emphasis> on the outgoing channel followed by each message received from the incoming channel. The <literal>copy</literal> process forwards each received message on each of its two outgoing channels. Finally, <literal>add</literal> sends on the outgoing channel the sum of corresponding messages received from the two incoming channels. Overall, it is easy to see that the stream of messages flowing on channel <emphasis>a</emphasis> corresponds to the Fibonacci sequence 1, 1, 2, 3, 5,....</para>
<para>The modeling of the Fibonacci stream network in <literal>Hypha</literal>&#x2019;s input language is shown in <link linkend="list9_2">Listing <xref linkend="list9_2" remap="9.2"/></link>. There is a service for each of the boxes in <link linkend="fig9_1">Figure <xref linkend="fig9_1" remap="9.1"/></link>, with <literal>source</literal> that makes use of an auxiliary service <literal>link</literal> that acts as a persistent message forwarder. The network itself is created on line 5, where the services are invoked and connected by the channels <emphasis>a</emphasis> through <emphasis>e</emphasis>. The most distinctive aspect of the modeling is the use of continuation passing for the representation of message streams: each channel that connects two combinators is in fact a linear channel (a channel that is meant to be used for one communication only); whenever a message is exchanged on the channel, the payload is paired with a fresh (linear) channel on which the subsequent message will be exchanged. This pattern can be clearly observed in the definitions of <literal>link</literal>, <literal>add</literal>,and <literal>copy</literal>. To improve readability, hereafter we write x&#x0304; for a channel name that is meant to represent the continuation of <emphasis>x</emphasis>.</para>
<fig id="lis9_2">
<label>Listing 9.2</label>
<caption><title>Term representation of the Fibonacci stream network [<link linkend="ch09_b8">8</link>, <link linkend="ch09_b22">22</link>].</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog097.jpg" mime-subtype="jpeg"/>
</fig>
<para><literal>Hypha</literal> infers the following types for the channels used in the system</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog098.jpg" mime-subtype="jpeg"/></para>
<para>confirming that the system is well typed and therefore lock free. In particular, Theorem 9.3(2) allows us to deduce that every number in the sequence of Fibonacci is computed in finite time. We make some observations concerning the inferred channel types: first, <literal>Hypha</literal> correctly distinguishes between the channels representing services (such as <literal>copy</literal> and <literal>source</literal>) from the linear channels that connect them (such as <emphasis>a</emphasis> and <emphasis>b</emphasis>). Second, the levels associated with linear channels give hints concerning the order of synchronizations in the system. The synchronizations on <emphasis>a</emphasis> and <emphasis>d</emphasis> (with level 0) happen first, followed by that on <emphasis>c</emphasis> (level 1), and then by that on <emphasis>e</emphasis> (level 2). Note however, that the total order on levels does not necessarily reflect the partial order that represents dependencies between channels. For example, <emphasis>b</emphasis> has a strictly greater level than <emphasis>c</emphasis> and yet the synchronizations on these two channels may happen in any order. Concerning the ticket annotations, note that all linear channels require at least 2 tickets because they are used to connect 2 services. For example, <emphasis>a</emphasis> connects <literal>source(1)</literal> and <literal>copy</literal>. By contrast, <emphasis>b</emphasis> and <emphasis>e</emphasis> need one more ticket because they are also forwarded by <literal>source</literal> to <literal>link</literal>.</para>
</section>
<section class="lev2" id="sec9-3-2" label="9.3.2" xreflabel="9.3.2">
<title>Full-Duplex and Half-Duplex Communications</title>
<para>Many parallel algorithms use batteries of processes arranged in a grid that iteratively update array elements and communicate with processes assigned to neighbor elements (<link linkend="fig9_2">Figure <xref linkend="fig9_2" remap="9.2"/></link>). Processes may communicate according to one out of two modalities: when communication is <emphasis>full-duplex</emphasis>, processes simultaneously send messages to each other; when communication is <emphasis>half-duplex</emphasis>, only one message travels between two processes at any moment in time. Correspondingly, we can model the dotted grid fragment in <link linkend="fig9_2">Figure <xref linkend="fig9_2" remap="9.2"/></link> as</para>
<fig id="fig9_2" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 9.2</label>
<caption><title>Graphical representation of a 4 <literal>&#x00D7;</literal> 3 bi-dimensional stencil.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig059.jpg" mime-subtype="jpeg"/>
</fig>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e208_01.jpg" mime-subtype="jpeg"/></para>
<para>where <emphasis>e</emphasis> and <emphasis>f</emphasis> are service channels defined as either</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e208_02.jpg" mime-subtype="jpeg"/></para>
<para>in case of full-duplex communication or as</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e208_03.jpg" mime-subtype="jpeg"/></para>
<para>in case of half-duplex communication. In both cases, <emphasis>x</emphasis> is used for sending messages to, and <emphasis>y</emphasis> for receiving messages from, a neighbor process. Each message sent on <emphasis>x</emphasis> carries a payload <emphasis>n</emphasis> as well as a continuation channel x&#x0304; used for the communication at the next iteration. Symmetrically, each message received from <emphasis>y</emphasis> contains the neighbor&#x2019;s payload <emphasis>m</emphasis> and a continuation &#x0233;. The difference between <literal>full</literal> and <literal>half</literal> is that, in the latter case, the sender waits for the message from its neighbor before sending its own.</para>
<para>Overall there are 4 possible configurations of the system (9.6) obtained by instantiating <emphasis>e</emphasis> and <emphasis>f</emphasis> with either <literal>full</literal> or <literal>half</literal>. It is easy to see that a configuration is lock free as long as <emphasis>at least</emphasis> one of <emphasis>e</emphasis> or <emphasis>f</emphasis> is instantiated with <literal>full</literal>. Indeed, <literal>Hypha</literal> infers the types</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e208_04.jpg" mime-subtype="jpeg"/></para>
<para>when <emphasis>e</emphasis> = <emphasis>f</emphasis> = <literal>full</literal> and the types</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e208_05.jpg" mime-subtype="jpeg"/></para>
<para>when <emphasis>e</emphasis> = <literal>half</literal> and <emphasis>f</emphasis> = <literal>full</literal>. The case when <emphasis>e</emphasis> = <literal>full</literal> and <emphasis>f</emphasis> = <literal>half</literal> is symmetric, while the one when <emphasis>e</emphasis> = <emphasis>f</emphasis> = <literal>half</literal> is ill typed for deadlock freedom and, therefore, for lock freedom as well.</para>
<fig id="fig9_3" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 9.3</label>
<caption><title>Master-worker (left) and producer-consumer (right).</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig060.jpg" mime-subtype="jpeg"/>
</fig>
</section>
<section class="lev2" id="sec9-3-3" label="9.3.3" xreflabel="9.3.3">
<title>Load Balancing</title>
<para><link linkend="fig9_3">Figure <xref linkend="fig9_3" remap="9.3"/></link> shows two network topologies aimed at taking advantage of parallelism by distributing multiple tasks to independent workers. They differ in that in the <emphasis>master-worker</emphasis> topology the same process that produces tasks is also the one that collects the results, whereas in the <emphasis>producer-consumer</emphasis> topology (sometimes called &#8220;farm&#8221;) producer and consumer are different processes. The distinction between the two topologies has important consequences at the communication layer since the channels are bi-directional in the former network and uni-directional in the latter.</para>
<para><link linkend="list9_3">Listings <xref linkend="list9_3" remap="9.3"/></link> and <link linkend="list9_4"><xref linkend="list9_4" remap="9.4"/></link> show the modeling of the network topologies in <link linkend="fig9_3">Figure <xref linkend="fig9_3" remap="9.3"/></link>, both of which are well-typed according to the lock freedom type system implemented in <literal>Hypha</literal>. For the master-worker network, <literal>Hypha</literal> infers the types</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e0209_1.jpg" mime-subtype="jpeg"/></para>
<fig id="lis9_3">
<label>Listing 9.3</label>
<caption><title>Term representation of master-worker (half-duplex channels).</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog099.jpg" mime-subtype="jpeg"/>
</fig>
<fig id="lis9_4">
<label>Listing 9.4</label>
<caption><title>Term representation of producer-consumer.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog100.jpg" mime-subtype="jpeg"/>
</fig>
<para>which describe a communication protocol whereby the master sends a task (represented as an integer number) to each worker along with a continuation channel. By using this continuation channel, the worker will answer back with the processed task (again represented as an integer number) and another continuation that the master uses for starting another iteration.</para>
<para>For the producer-consumer network <literal>Hypha</literal> infers the types</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e210_01.jpg" mime-subtype="jpeg"/></para>
<para>again confirming that the network is lock free.</para>
</section>
<section class="lev2" id="sec9-3-4" label="9.3.4" xreflabel="9.3.4">
<title>Sorting Networks</title>
<para><link linkend="fig9_4">Figure <xref linkend="fig9_4" remap="9.4"/></link> depicts an example of so-called <emphasis>sorting network</emphasis>, that is a network of communicating processes whose overall effect is that of sorting an input vector of fixed size, 6 in this case. The network is made of two different kinds of processes: <emphasis>comparators</emphasis> (the rectangular boxes) input two values and possibly swap them if the first happens to be larger than the second; <emphasis>buffers</emphasis> (the square boxes) simply forward the input value. The input values go through three identical phases; in each stage, the odd-indexed inputs and then the even-indexed inputs are compared to, and possibly swapped with, their successor.</para>
<para>The sorting network in <link linkend="fig9_4">Figure <xref linkend="fig9_4" remap="9.4"/></link> is modeled in the linear <emphasis>&#x03C0;</emphasis>-calculus as shown in <link linkend="list9_5">Listing <xref linkend="list9_5" remap="9.5"/></link>. Note the use of auxiliary services <literal>odd</literal> and <literal>even</literal> corresponding to the two sub-phases of each phase and linked together by restricted channels <emphasis>z</emphasis><emphasis><subscript>i</subscript></emphasis>. This network is well-typed and <literal>Hypha</literal> infers the types</para>
<fig id="fig9_4" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 9.4</label>
<caption><title>Graphical representation of an odd-even 6-input sorting network.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig061.jpg" mime-subtype="jpeg"/>
</fig>
<fig id="lis9_5">
<label>Listing 9.5</label>
<caption><title>Term representation of an odd-even 6-input sorting network.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog101.jpg" mime-subtype="jpeg"/>
</fig>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog102.jpg" mime-subtype="jpeg"/></para>
<para>confirming that each value sent on <emphasis>a</emphasis><emphasis><subscript>i</subscript></emphasis><subscript>1</subscript> is eventually received on some <emphasis>a</emphasis><subscript><emphasis>j</emphasis>4</subscript>. More specifically, the level 12 assigned with the <emphasis>a</emphasis><subscript><emphasis>j</emphasis>4</subscript> channels gives an upper bound to the number of synchronizations needed for producing the output.</para>
<para>Comparators input values in parallel (from the channels <emphasis>x</emphasis> and <emphasis>y</emphasis>) and perform an internal synchronization (on a private linear channel <emphasis>z</emphasis>)tojointhe results of the two receptions and output the results. Alternatively, one could model comparators in such a way that the receive operations on <emphasis>x</emphasis> and <emphasis>y</emphasis> are performed in a fixed order. The choice of a particular modeling affects the levels associated with the input channels, but not the typeability of the network as a whole. This is not the case for buffers: they are operationally irrelevant and are usually omitted in standard presentations of sorting networks. Their use in <link linkend="list9_5">Listing <xref linkend="list9_5" remap="9.5"/></link> is key for the lock freedom analysis to succeed as they make sure that the levels of the channels connecting one phase to the next one remain aligned.</para>
</section>
<section class="lev2" id="sec9-3-5" label="9.3.5" xreflabel="9.3.5">
<title>Ill-typed, Lock-free Process Networks</title>
<para>In general, the problem of verifying whether a <emphasis>&#x03C0;</emphasis>-calculus process is (dead)lock free is undecidable. For this reason, the type system on which <literal>Hypha</literal> is based is necessarily incomplete, in the sense that there exist processes satisfying Definitions 9.1 and 9.2 which are ill typed according to the type system described in Section 9.2. In this section, we discuss two representative examples of processes that cannot be handled by our type system. In all cases, the inability to find a typing derivation is tightly related to the fact that the type system uses integer numbers for reasoning on the dependencies between linear channels and such numbers measure the (abstract) moment of time at which the synchronization occurs on these channels.</para>
<para><link linkend="list9_6">Listing <xref linkend="list9_6" remap="9.6"/></link> shows a process that computes the sequence of prime numbers. The process is modeled after Eratosthenes&#x2019; sieve: the <literal>from</literal> process emits the infinite stream of natural numbers starting from 2; the sequence goes through a growing pipeline of <literal>filter</literal>s, each filter removing those numbers of the sequence that happen to be a multiple of a given prime number <emphasis>m</emphasis>; if a number <emphasis>n</emphasis> manages to cross the entire pipeline and hits the emitter process <literal>output</literal>, then it is prime. In this case <emphasis>n</emphasis> is sent on <literal>print</literal> and a new filter removing the multiples of <emphasis>n</emphasis> is inserted at the end of the pipeline. <literal>Hypha</literal> is able to distinguish linear from service channels and to infer the type of messages exchanged therein, but the process is ill-typed for deadlock freedom even though it is deadlock free. The problem can be traced to the body of <literal>filter</literal>: when the received number <emphasis>n</emphasis> turns out to be a multiple of <emphasis>m</emphasis>, the number is simply discarded and no output is sent on <emphasis>y</emphasis>. So, the recursive invocation of <literal>filter</literal> on line 3 reuses the same output channel <emphasis>y</emphasis> that was received as input. Observe that x&#x0304; is received from <emphasis>x</emphasis>, meaning that the level of x&#x0304; must necessarily be greater than the level of <emphasis>x</emphasis>, and that the level of x&#x0304; must be strictly smaller than the level of <emphasis>y</emphasis>, since the input on performed on x&#x0304; at the next iteration of <literal>filter</literal> blocks the possible output on <emphasis>y</emphasis>. Given that the distribution of prime numbers is irregular, there is no upper bound to the number of inputs on <emphasis>x</emphasis> that may be necessary before the next output on <emphasis>y</emphasis> is guaranteed to be performed. In general, the type system can handle those cases in which communications occur following a regular pattern that is independent of the content of messages themselves.</para>
<fig id="lis9_6">
<label>Listing 9.6</label>
<caption><title>Stream Network computing the sequence of prime numbers.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog103.jpg" mime-subtype="jpeg"/>
</fig>
<para>The second example we consider is a process stream network (<link linkend="fig9_5">Figure <xref linkend="fig9_5" remap="9.5"/></link>) that computes the so-called Thue-Morse sequence, that is the sequence of binary digits 011010011001 <literal>&#x00B7;&#x00B7;&#x00B7;</literal> starting with 0 and obtained by appending the boolean complement of the sequence obtained thus far. The term representation of the process network (<link linkend="list9_7">Listing <xref linkend="list9_7" remap="9.7"/></link>) is modeled after its definition in terms of lazy streams [<link linkend="ch09_b7">7</link>] and makes use of a set of combinators some of which we have already used for the Fibonacci stream network (Section 9.3.1). The network is lock-free, as witnessed by the fact that the corresponding lazy stream definition can be shown to be productive [<link linkend="ch09_b7">7</link>], but also ill typed for deadlock freedom and hence for lock freedom as well. In this network the problematic combinator is <literal>zip</literal>, which interleaves on the output channel <emphasis>f</emphasis> the digits received from the input channels <emphasis>d</emphasis> and <emphasis>e</emphasis> hence producing messages on <emphasis>f</emphasis> at twice the rate at which they are consumed from <emphasis>d</emphasis> and <emphasis>e</emphasis>. This means that there is no fixed offset between the levels of <emphasis>d</emphasis> and <emphasis>e</emphasis> and that of <emphasis>f</emphasis> that could be dealt with by the typing rule [<emphasis role="smaller">t</emphasis>-<emphasis role="smaller">out</emphasis>*]. Note that this phenomenon does not manifest in the Fibonacci stream network (<link linkend="fig9_1">Figure <xref linkend="fig9_1" remap="9.1"/></link>) despite its seemingly similar topology. The key difference is that in the Fibonacci network the <literal>add</literal> process <emphasis>combines</emphasis> the messages received from the two input channels into a single message sent on the output channel. The example is interesting also because the impossibility to type the term is due to an excess of produced messages rather than the lack thereof, as it was with the sequence of prime numbers.</para>
<fig id="fig9_5" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 9.5</label>
<caption><title>Stream network computing the Thue-Morse sequence.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig062.jpg" mime-subtype="jpeg"/>
</fig>
<fig id="lis9_7">
<label>Listing 9.7</label>
<caption><title>Term representation of the stream network computing the Thue-Morse sequence.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog104.jpg" mime-subtype="jpeg"/>
</fig>
</section>
</section>
<section class="lev1" id="sec9-4" label="9.4" xreflabel="9.4">
<title>Related Work</title>
<para>Binary session type disciplines [<link linkend="ch09_b12">12</link>] provide intra-session guarantees of lock freedom but cannot enforce this property in general when multiple sessions are interleaved for types do not carry any information concerning the dependencies between different sessions. Some session type systems [<link linkend="ch09_b1">1</link>, <link linkend="ch09_b24">24</link>] are designed in such a way that a plain session type discipline is sufficient to guarantee deadlock freedom. However, only networks with a tree-like communication topology are well typed. Among the examples we have considered, just the recursive Fibonacci (<link linkend="list9_1">Listing <xref linkend="list9_1" remap="9.1"/></link>) and the master-worker (<link linkend="list9_3">Listing <xref linkend="list9_3" remap="9.3"/></link>) fall in this category.</para>
<para>Multiparty session type disciplines [<link linkend="ch09_b11">11</link>, <link linkend="ch09_b12">12</link>] extend (dead)lock freedom to sessions involving multiple processes. In these framework a <emphasis>global type</emphasis> is used to describe the interactions between participants of a session as opposed to the actions that participants perform on the channel of the session. The global type is given explicitly by the system designer/programmer and a tool is then used to check the consistency of the global type against (a model of) the code that is meant to realize it. This top-down approach is complementary to the one we have pursued in this chapter: <literal>Hypha</literal> analyzes assemblies of processes knowing nothing about the intended communication topology. In general, global types have been designed for describing delimited interactions within sessions, but they cannot dispense completely from the need of interleaving different sessions, in which case they are unable to prevent (dead)locks. This has led to the study of hybrid approaches [<link linkend="ch09_b2">2</link>, <link linkend="ch09_b3">3</link>] that keep track of the order in which different sessions interleave with the purpose of detecting mutual dependencies between sessions that could lead to (dead)locks.</para>
<para>The works most closely related to our own are those where each input/output operation described by a channel/session type is annotated with information that captures the dependencies between different channels/sessions. Such annotations come in the form of integer numbers as in our case, or as abstract events, or as combinations thereof. The original technique and the corresponding analysis tool <literal>TyPiCal</literal>, which our type system and <literal>Hypha</literal> are heavily inspired by, were described by Kobayashi [<link linkend="ch09_b13">13</link>&#x2013;<link linkend="ch09_b15">15</link>]. These type systems and our own are uncomparable: on the one hand, in Kobayashi&#x2019;s works annotations can be used to reason about dependencies between arbitrary channels, whereas we focus on linear channels only. On the other hand, the form of level polymorphism allowed by rule [<emphasis role="smaller">t</emphasis>-<emphasis role="smaller">out</emphasis>*] enables the verification of cyclic networks of recursive processes (most of the examples we have examined in this chapter fall in this category) that cannot be successfully handled by Kobayashi&#x2019;s type systems [<link linkend="ch09_b13">13</link>&#x2013;<link linkend="ch09_b15">15</link>]. A more recent work [<link linkend="ch09_b9">9</link>] improves the precision of the technique, although recursive types (hence recursive communication protocols) are not considered. The annotation-based technique has also been applied directly to binary [<link linkend="ch09_b17">17</link>, <link linkend="ch09_b23">23</link>] and multiparty sessions [<link linkend="ch09_b21">21</link>].</para>
<para>It has been shown that the approaches imposing a tree-like communication topology [<link linkend="ch09_b1">1</link>, <link linkend="ch09_b24">24</link>] are subsumed by those those annotating I/O actions in session types with dependency information [<link linkend="ch09_b6">6</link>].</para>
<para><emphasis role="strong">Acknowledgments</emphasis> I&#x2019;m grateful to the anonymous reviewers whose comments helped me improving both content and presentation of this chapter.</para>
</section>
<section class="lev1" id="sec2a">
<title>References</title>
<orderedlist numeration="arabic" continuation="restarts" spacing="normal">
<listitem><para><anchor id="ch09_b1"/>Lu&#x00ED;s Caires, Frank Pfenning, and Bernardo Toninho. Linear logic propositions as session types. <emphasis>Mathematical Structures in Computer Science</emphasis>, 26(3):367&#x2013;423, 2016. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Lu%EDs+Caires%2C+Frank+Pfenning%2C+and+Bernardo+Toninho%2E+Linear+logic+propositions+as+session+types%2E+Mathematical+Structures+in+Computer+Science%2C+26%283%29%3A367-423%2C+2016%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch09_b2"/>Mario Coppo, Mariangiola Dezani-Ciancaglini, Luca Padovani, and Nobuko Yoshida. Inference of Global Progress Properties for Dynamically Interleaved Multiparty Sessions. In <emphasis>Proceedings of COORDINATION&#x2019;13</emphasis>, LNCS 7890, pages 45&#x2013;59. Springer, 2013. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Mario+Coppo%2C+Mariangiola+Dezani-Ciancaglini%2C+Luca+Padovani%2C+and+Nobuko+Yoshida%2E+Inference+of+Global+Progress+Properties+for+Dynamically+Interleaved+Multiparty+Sessions%2E+In+Proceedings+of+COORDINATION%2713%2C+LNCS+7890%2C+pages+45-59%2E+Springer%2C+2013%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch09_b3"/>Mario Coppo, Mariangiola Dezani-Ciancaglini, Nobuko Yoshida, and Luca Padovani. Global Progress for Dynamically Interleaved Multiparty Sessions. <emphasis>Mathematical Structures in Computer Science</emphasis>, 26:238&#x2013;302, 2016.</para></listitem>
<listitem><para><anchor id="ch09_b4"/>Bruno Courcelle. Fundamental properties of infinite trees. <emphasis>Theoretical Computer Science</emphasis>, 25:95&#x2013;169, 1983. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Bruno+Courcelle%2E+Fundamental+properties+of+infinite+trees%2E+Theoretical+Computer+Science%2C+25%3A95-169%2C+1983%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch09_b5"/>Ornela Dardha, Elena Giachino, and Davide Sangiorgi. Session types revisited. In <emphasis>Proceedings of PPDP&#x2019;12</emphasis>, pages 139&#x2013;150. ACM, 2012. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Ornela+Dardha%2C+Elena+Giachino%2C+and+Davide+Sangiorgi%2E+Session+types+revisited%2E+In+Proceedings+of+PPDP%2712%2C+pages+139-150%2E+ACM%2C+2012%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch09_b6"/>Ornela Dardha and Jorge A. P&#x00E9;rez. Comparing deadlock-free session typed processes. In <emphasis>Proceedings of EXPRESS</emphasis>/<emphasis>SOS&#x2019;15</emphasis>, EPTCS 190, pages 1&#x2013;15, 2015. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Ornela+Dardha+and+Jorge+A%2E+P%E9rez%2E+Comparing+deadlock-free+session+typed+processes%2E+In+Proceedings+of+EXPRESS%2FSOS%2715%2C+EPTCS+190%2C+pages+1-15%2C+2015%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch09_b7"/>J&#x00F6;rg Endrullis, Clemens Grabmayer, Dimitri Hendriks, Ariya Isihara, and Jan Willem Klop. Productivity of stream definitions. <emphasis>Theoretical Computer Science</emphasis>, 411(4-5):765&#x2013;782, 2010. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=J%F6rg+Endrullis%2C+Clemens+Grabmayer%2C+Dimitri+Hendriks%2C+Ariya+Isihara%2C+and+Jan+Willem+Klop%2E+Productivity+of+stream+definitions%2E+Theoretical+Computer+Science%2C+411%284-5%29%3A765-782%2C+2010%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch09_b8"/>Marc Geilen and Twan Basten. Kahn process networks and a reactive extension. In <emphasis>Handbook of Signal Processing Systems</emphasis>, pages 1041&#x2013;1081. Springer, 2013. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Marc+Geilen+and+Twan+Basten%2E+Kahn+process+networks+and+a+reactive+extension%2E+In+Handbook+of+Signal+Processing+Systems%2C+pages+1041-1081%2E+Springer%2C+2013%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch09_b9"/>Elena Giachino, Naoki Kobayashi, and Cosimo Laneve. Deadlock analysis of unbounded process networks. In <emphasis>Proceedings of CONCUR&#x2019;14</emphasis>, LNCS 8704, pages 63&#x2013;77. Springer, 2014. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Elena+Giachino%2C+Naoki+Kobayashi%2C+and+Cosimo+Laneve%2E+Deadlock+analysis+of+unbounded+process+networks%2E+In+Proceedings+of+CONCUR%2714%2C+LNCS+8704%2C+pages+63-77%2E+Springer%2C+2014%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch09_b10"/>Kohei Honda. Types for dyadic interaction. In <emphasis>Proceedings of CONCUR&#x2019;93</emphasis>, LNCS 715, pages 509&#x2013;523. Springer, 1993.</para></listitem>
<listitem><para><anchor id="ch09_b11"/>Kohei Honda, Nobuko Yoshida, and Marco Carbone. Multiparty asynchronous session types. <emphasis>Journal of the ACM</emphasis>, 63(1):9, 2016. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Kohei+Honda%2C+Nobuko+Yoshida%2C+and+Marco+Carbone%2E+Multiparty+asynchronous+session+types%2E+Journal+of+the+ACM%2C+63%281%29%3A9%2C+2016%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch09_b12"/>Hans H&#x00FC;ttel, Ivan Lanese, Vasco T. Vasconcelos, Lu&#x00ED;s Caires, Marco Carbone, Pierre-Malo Deni&#x00E9;lou, Dimitris Mostrous, Luca Padovani, Ant&#x00F3;nio Ravara, Emilio Tuosto, Hugo Torres Vieira, and Gianluigi Zavattaro. Foundations of Session Types and Behavioural Contracts. <emphasis>ACM Computing Surveys</emphasis>, 49:3:1&#x2013;3:36, 2016. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Hans+H%FCttel%2C+Ivan+Lanese%2C+Vasco+T%2E+Vasconcelos%2C+Lu%EDs+Caires%2C+Marco+Carbone%2C+Pierre-Malo+Deni%E9lou%2C+Dimitris+Mostrous%2C+Luca+Padovani%2C+Ant%F3nio+Ravara%2C+Emilio+Tuosto%2C+Hugo+Torres+Vieira%2C+and+Gianluigi+Zavattaro%2E+Foundations+of+Session+Types+and+Behavioural+Contracts%2E+ACM+Computing+Surveys%2C+49%3A3%3A1-3%3A36%2C+2016%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch09_b13"/>Naoki Kobayashi. A type system for lock-free processes. <emphasis>Information and Computation</emphasis>, 177(2):122&#x2013;159, 2002. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Naoki+Kobayashi%2E+A+type+system+for+lock-free+processes%2E+Information+and+Computation%2C+177%282%29%3A122-159%2C+2002%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch09_b14"/>Naoki Kobayashi. Type-based information flow analysis for the pi-calculus. <emphasis>Acta Informatica</emphasis>, 42(4-5):291&#x2013;347, 2005. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Naoki+Kobayashi%2E+Type-based+information+flow+analysis+for+the+pi-calculus%2E+Acta+Informatica%2C+42%284-5%29%3A291-347%2C+2005%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch09_b15"/>Naoki Kobayashi. A new type system for deadlock-free processes. In <emphasis>Proceedings of CONCUR&#x2019;06</emphasis>, LNCS 4137, pages 233&#x2013;247. Springer, 2006. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Naoki+Kobayashi%2E+A+new+type+system+for+deadlock-free+processes%2E+In+Proceedings+of+CONCUR%2706%2C+LNCS+4137%2C+pages+233-247%2E+Springer%2C+2006%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch09_b16"/>Naoki Kobayashi, Benjamin C. Pierce, and David N. Turner. Linearity and the pi-calculus. <emphasis>ACM Transactions on Programming Languages and Systems</emphasis>, 21(5):914&#x2013;947, 1999. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Naoki+Kobayashi%2C+Benjamin+C%2E+Pierce%2C+and+David+N%2E+Turner%2E+Linearity+and+the+pi-calculus%2E+ACM+Transactions+on+Programming+Languages+and+Systems%2C+21%285%29%3A914-947%2C+1999%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch09_b17"/>Luca Padovani. From Lock Freedom to Progress Using Session Types. In <emphasis>Proceedings of PLACES&#x2019;13</emphasis>, EPTCS 137, pages 3&#x2013;19, 2013. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Luca+Padovani%2E+From+Lock+Freedom+to+Progress+Using+Session+Types%2E+In+Proceedings+of+PLACES%2713%2C+EPTCS+137%2C+pages+3-19%2C+2013%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch09_b18"/>Luca Padovani. Deadlock and Lock Freedom in the Linear <emphasis>&#x03C0;</emphasis>-Calculus. In <emphasis>Proceedings of CSL-LICS&#x2019;14</emphasis>, pages 72:1&#x2013;72:10. ACM, 2014. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Luca+Padovani%2E+Deadlock+and+Lock+Freedom+in+the+Linear+pi-Calculus%2E+In+Proceedings+of+CSL-LICS%2714%2C+pages+72%3A1-72%3A10%2E+ACM%2C+2014%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch09_b19"/>Luca Padovani. Type Reconstruction for the Linear <emphasis>&#x03C0;</emphasis>-Calculus with Composite Regular Types. <emphasis>Logical Methods in Computer Science</emphasis>, 11:1&#x2013;45, 2015. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Luca+Padovani%2E+Type+Reconstruction+for+the+Linear+pi-Calculus+with+Composite+Regular+Types%2E+Logical+Methods+in+Computer+Science%2C+11%3A1-45%2C+2015%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch09_b20"/>Luca Padovani, Tzu-Chun Chen, and Andrea Tosatto. Type Reconstruction Algorithms for Deadlock-Free and Lock-Free Linear <emphasis>&#x03C0;</emphasis>-Calculi. In <emphasis>Proceedings of COORDINATION&#x2019;15</emphasis>, LNCS 9037, pages 83&#x2013;98. Springer, 2015.</para></listitem>
<listitem><para><anchor id="ch09_b21"/>Luca Padovani, Vasco T. Vasconcelos, and Hugo Torres Vieira. Typing Liveness in Multiparty Communicating Systems. In <emphasis>Proceedings of COORDINATION&#x2019;14</emphasis>, LNCS 8459, pages 147&#x2013;162. Springer, 2014. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Luca+Padovani%2C+Vasco+T%2E+Vasconcelos%2C+and+Hugo+Torres+Vieira%2E+Typing+Liveness+in+Multiparty+Communicating+Systems%2E+In+Proceedings+of+COORDINATION%2714%2C+LNCS+8459%2C+pages+147-162%2E+Springer%2C+2014%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch09_b22"/>John H. Reppy. <emphasis>Concurrent Programming in ML</emphasis>. Cambridge University Press, 1999. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=John+H%2E+Reppy%2E+Concurrent+Programming+in+ML%2E+Cambridge+University+Press%2C+1999%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch09_b23"/>Hugo Torres Vieira and Vasco T. Vasconcelos. Typing progress in communication-centred systems. In <emphasis>Proceedings of COORDINATION&#x2019;13</emphasis>, LNCS 7890, pages 236&#x2013;250. Springer, 2013. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Hugo+Torres+Vieira+and+Vasco+T%2E+Vasconcelos%2E+Typing+progress+in+communication-centred+systems%2E+In+Proceedings+of+COORDINATION%2713%2C+LNCS+7890%2C+pages+236-250%2E+Springer%2C+2013%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch09_b24"/>Philip Wadler. Propositions as sessions. <emphasis>Journal of Functional Programming</emphasis>, 24(2&#x2013;3):384&#x2013;418, 2014. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Philip+Wadler%2E+Propositions+as+sessions%2E+Journal+of+Functional+Programming%2C+24%282-3%29%3A384-418%2C+2014%2E" target="_blank">Google Scholar</ulink></para></listitem>
</orderedlist>
</section>
</chapter>
<chapter class="chapter" id="ch10" label="10" xreflabel="10">
<title>Session Types with Linearity in Haskell</title>
<para><emphasis role="strong">Dominic Orchard</emphasis><footnote id="fn10_1a" label="1"><para>University of Kent, UK</para></footnote><emphasis role="strong">and Nobuko Yoshida</emphasis><footnote id="fn10_2a" label="2"><para>Imperial College London, UK</para></footnote></para>
<section class="lev2">
<title>Abstract</title>
<para>Type systems with parametric polymorphism can encode a significant proportion of the information contained in session types. This allows concurrent programming with session-type-like guarantees in languages like ML and Java. However, statically enforcing the linearity properties of session types, in a way that is also natural to program with, is more challenging. Haskell provides various language features that can capture concurrent programming with session types, with full linearity invariants and in a mostly idiomatic style. This chapter overviews various approaches in the literature for session typed programming in Haskell.</para>
<para>As a starting point, we use polymorphic types and simple type-level functions to provide session-typed communication in Haskell without linearity. We then overview and compare the varying approaches to implementing session types with static linearity checks. We conclude with a discussion of the remaining open problems.</para>
<para>The code associated with this chapter can be found at <literal>http://github. com/dorchard/betty-book-haskell-sessions</literal>.</para>
</section>
<section class="lev1" id="sec10-1" label="10.1" xreflabel="10.1">
<title>Introduction</title>
<para>Session types are a kind of behavioural type capturing the communication behaviour of concurrent processes. While there are many variants of session types, they commonly capture the sequence of sends and receives performed over a channel and the types of the messages carried by these interactions. A significant aspect of session types is that they enforce <emphasis>linear</emphasis> use of channels: every send must have exactly one receive (no orphan messages), and vice versa (no hanging receives). These properties are often referred to together as <emphasis>communication safety</emphasis>. A channel cannot be reused once it has &#8220;used up&#8221; its capability to perform sends and receives. This aspect of session types makes them hard to implement in languages which do not have built-in notions of linearity and resource consumption in the type system.</para>
<para>The following two example interactions will be used throughout.</para>
<para><emphasis role="strong">Example 1</emphasis> (Integer equality server and client)<emphasis role="strong">.</emphasis> Consider a simple server which provides two modes of interaction (services) to clients. If a client chooses the first service, the server can then receive two integers, compare these for equality, send the result back as a boolean, and then return to the start state. The second service tells the server to stop hence it does not return to providing the initial two services.</para>
<para>A potential client requests the first behaviour, sends two integers, receives a boolean, and then requests that the server stop. These server and client behaviours are captured by the following session types, using the notation of Yoshida and Vasconcelos [<link linkend="ch10_b18">18</link>], which describe the interaction from the perspective of opposite channel endpoints:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/220equ01.jpg" mime-subtype="jpeg"/></para>
<para>The server has a <emphasis>recursive</emphasis> session type, denoted <emphasis>&#x03BC;&#x03B1;.S</emphasis> which binds the variable <emphasis>&#x03B1;</emphasis> in scope of a session type <emphasis>S</emphasis>. Session types are typically equi-recursive, such that <emphasis>&#x03BC;&#x03B1;.S</emphasis> <emphasis>&#x2261;</emphasis> <emphasis>S</emphasis><emphasis role="roman">[</emphasis><emphasis>&#x03BC;&#x03B1;.S/&#x03B1;</emphasis><emphasis role="roman">]</emphasis>. The operator <emphasis role="roman">&#x0026;</emphasis> denotes a choice offered between branches, labelled here as <emphasis role="romanAlt1">eq</emphasis> and <emphasis role="romanAlt1">nil</emphasis>. In the <emphasis role="romanAlt1">eq</emphasis> case, two integers are received and a boolean is sent before recursing with <emphasis>&#x03B1;</emphasis>. In the <emphasis role="romanAlt1">nil</emphasis> case the interaction finishes, denoted by <emphasis role="strong">end</emphasis>.</para>
<para>The client selects the <emphasis role="romanAlt1">eq</emphasis> service, denoted by <emphasis>&#x2295;</emphasis>. Two integers are sent and a boolean is received. Then the <emphasis role="romanAlt1">nil</emphasis> behaviour is selected via <emphasis>&#x2295;</emphasis>, ending the interaction. Session types thus abstract communication over a channel, or equivalently, they describe a channel&#x2019;s <emphasis>capabilities</emphasis>.</para>
<para>The two types are <emphasis>dual</emphasis>: they describe complementary communication behaviour on opposite end-points of a channel. Duality can be defined inductively as a function on session types:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/220equ02.jpg" mime-subtype="jpeg"/></para>
<para>Recursion variables come in two flavours: <emphasis>&#x03B1;</emphasis> and their dual <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/10_equ01.jpg" mime-subtype="jpeg"/>. The dual of a dualised variable <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/10_equ02.jpg" mime-subtype="jpeg"/> is the undualised <emphasis>&#x03B1;</emphasis>. This formulation of duality with recursive types is due to Lindley and Morris [<link linkend="ch10_b7">7</link>].</para>
<para>Duality enforces communication safety. If the communication patterns of the server and client do not match then duality does not hold. Duality also encompasses linearity, as any repetition of actions by the server or client leads to non-matching communication behaviour.</para>
<para><emphasis role="strong">Example 2</emphasis> (Delegating integer equality)<emphasis role="strong">.</emphasis> Following the expressive power of the <emphasis>&#x03C0;</emphasis>-calculus, session types can also capture <emphasis>delegation</emphasis>, where channels are passed over channels. Thus, the types of communicated values <emphasis>&#x03C4;</emphasis> include session types of communicated channels, written &#x3008;<emphasis>S</emphasis>&#x3009;.</para>
<para>As a permutation on the previous example, we introduce a layer of indirection through delegation. The server, after receiving two integers, now receives a channel over which the resulting boolean should be sent. Dually, the client sends a channel which has the capability of sending a boolean. This is captured by the session types:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/10_equ03.jpg" mime-subtype="jpeg"/></para>
<para>The server&#x2019;s capability to receive a channel, over which a boolean is sent, is denoted <emphasis role="roman">?</emphasis> <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/10_equ04.jpg" mime-subtype="jpeg"/> whose dual in the client is <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/10_equ05.jpg" mime-subtype="jpeg"/>: the sending of a channel over which a boolean can be sent.</para>
<para>The reader is referred to the work of Yoshida and Vasconcelos [<link linkend="ch10_b18">18</link>] for a full description of a session type theory for the <emphasis>&#x03C0;</emphasis>-calculus on which our more informal presentation is based here.</para>
<para>To unpack the problem of encoding session type linearity in Haskell, we first introduce a relatively simple encoding of session types capturing sequences of send and receive actions on channels and some notion of session duality. However, this approach does not fully enforce linearity (Section 10.2). We then overview the various approaches in the literature for encoding session types in Haskell, focusing on their approach to linearity (Section 10.3). Outstanding problems and open questions in this area are discussed finally in Section 10.4.</para>
<para>Throughout, &#8220;Haskell&#8221; refers to the variant of Haskell provided by GHC (the Glasgow Haskell Compiler) which provides various type system extensions, the use of which is indicated and explained as required.</para>
</section>
<section class="lev1" id="sec10-2" label="10.2" xreflabel="10.2">
<title>Pre-Session Types in Haskell</title>
<para>Haskell provides a library for message-passing concurrency with channels similar in design to the concurrency primitives of CML [<link linkend="ch10_b14">14</link>]. The core primitives have types:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog105.jpg" mime-subtype="jpeg"/></para>
<para>These functions operate within the <literal>IO</literal> monad for encapsulating side-effectful computations; creating channels (<literal>newChan</literal>), sending and receiving values on these channels (<literal>writeChan</literal> and <literal>readChan</literal>), and forking processes (<literal>forkIO</literal>) are all effectful. Channels have a single type and are bi-directional. The following program implements Example 1:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog106.jpg" mime-subtype="jpeg"/></para>
<para>The choice between the two services is provided via a <literal>Maybe</literal> type, where <literal>server :: Chan (Maybe Int) -&gt; Chan Bool -&gt; IO ()</literal>. Two channels are used so that values of different type can be communicated. The channel types ensure <emphasis>data safety</emphasis>: communicated values are of the expected type. However, this typing cannot ensure communication safety. For example, the following two alternate clients are well-typed but are communication unsafe:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog107.jpg" mime-subtype="jpeg"/></para>
<para>On the left, an additional message is sent which is left unreceived in the server&#x2019;s channel buffer. On the right, a spurious <literal>readChan</literal> occurs after the first <literal>writeChan</literal> leading to a deadlock for the server and client.</para>
<fig id="fig10_1" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 10.1</label>
<caption><title>Implementations of the communication-typed combinators where <literal>link :: Links =&gt; (Chan s, Chan (Dual s)) -&gt; IO ()</literal>.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog107a.jpg" mime-subtype="jpeg"/>
</fig>
<para>A significant proportion of communication safety (mainly the order of interactions) can be enforced with just algebraic data types, polymorphism, and a type-based encoding of duality.</para>
<section class="lev2" id="sec10-2-1" label="10.2.1" xreflabel="10.2.1">
<title>Tracking Send and Receive Actions</title>
<para>Taking inspiration from Gay and Vasconcelos [<link linkend="ch10_b3">3</link>], we define the following alternate combinators (with implementations shown in <link linkend="fig10_1">Figure <xref linkend="fig10_1" remap="10.1"/></link>) and data types:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog108.jpg" mime-subtype="jpeg"/></para>
<para>The <literal>send</literal> combinator takes as parameters a channel which can transfer values of type <literal>Send a t</literal> and a value <literal>x</literal> of type <literal>a</literal> returning a new channel which can transfer the values of type <literal>t</literal>. This is implemented via the constructor <literal>Send</literal>, pairing the value <literal>x</literal> with a new channel <literal>c&#x2019;</literal>, sending those on the channel <literal>c</literal>, and returning the new continuation channel <literal>c&#x2019;</literal>.</para>
<para>The <literal>recv</literal> combinator is somewhat dual to this. It takes a channel <literal>c</literal> on which is received a pair of a value <literal>x</literal> of type <literal>a</literal> and channel <literal>c&#x2019;</literal> which can transfer values of type <literal>t</literal>. The pair <literal>(x, c&#x2019;)</literal> is then returned. The <literal>close</literal> combinator discards its channel which has only the capability of transferring <literal>End</literal> values, which are uninhabited (empty data types).</para>
<para>The following implements a non-recursive version of the integer equality server with delegation from Example 2 (for brevity <literal>C</literal> <emphasis role="roman">=</emphasis> <literal>Chan</literal>):</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog109.jpg" mime-subtype="jpeg"/></para>
<para>The type of the channel <literal>c</literal> gives a representation of the session type <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/10_equ06.jpg" mime-subtype="jpeg"/> from Example 2. At each step of the program, the channel returned by a send or receive is bound to a variable shadowing the channel variable used <emphasis>e.g.</emphasis> <literal>(x,c) &lt;- recv c</literal>. This programming idiom provides linear channel use.</para>
</section>
<section class="lev2" id="sec10-2-2" label="10.2.2" xreflabel="10.2.2">
<title>Partial Safety via a Type-Level Function for Duality</title>
<para>One way to capture duality is via a <emphasis>type family</emphasis>. Type families are primitive recursive type functions, with strong syntactic restrictions to enforce termination. We define the (closed) type family <literal>Dual</literal>:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog110.jpg" mime-subtype="jpeg"/></para>
<para>Duality is used to type the <literal>fork</literal> operation, which spawns a process with a fresh channel, returning a channel of the dual type:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog111.jpg" mime-subtype="jpeg"/></para>
<para><link linkend="fig10_1">Figure <xref linkend="fig10_1" remap="10.1"/></link> shows the implementation which uses a method <literal>link</literal> of the type class <literal>Link</literal> to connect sent messages to received messages and vice versa. A client interacting with <literal>server</literal> above can then be given as:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog112.jpg" mime-subtype="jpeg"/></para>
<para>Thus, the client sends two integers on <literal>c</literal> then creates a new channel <literal>d&#x2019;</literal>,which is sent via <literal>c</literal> before <literal>c</literal> is closed. On the returned channel <literal>d</literal> (with dual session type to <literal>d&#x2019;</literal>), we receive the result, which is output before closing <literal>d</literal>. Thus, <literal>Chan</literal> essentially provides the end-points of a bi-directional channel. The type of <literal>client</literal> can be given as:<sup>1</sup></para>
<footnote id="fn10_1" label="1"><para>A more general type can be inferred, since both <literal>Int</literal> types can be replaced with arbitrary types of the <literal>Num</literal> class and <literal>Bool</literal> with an arbitrary type of the <literal>Show</literal> class.</para></footnote>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog113.jpg" mime-subtype="jpeg"/></para>
<para>Swapping a <literal>send</literal> for a <literal>recv</literal>, or vice versa, means the program will no longer type check. Likewise, sending or receiving a value of the wrong type or at the wrong point in the interaction is also a type error.</para>
</section>
<section class="lev2" id="sec10-2-3" label="10.2.3" xreflabel="10.2.3">
<title>Limitations</title>
<para>The approach described so far captures sequences of actions, but cannot enforce exact linear usage of channels; nothing is enforcing the idiom of shadowing each channel variable once it is used. For example, the first few lines of the above example client could be modified to:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog114.jpg" mime-subtype="jpeg"/></para>
<para>By discarding the linear variable-shadowing discipline, an extra integer is sent on <literal>c</literal> in the third line. This is not prevented by the types. While the typing captures the order of interactions, it allows every action to be repeated, and entire session interactions to be repeated. Thus, the session type theory captured above is a kind of Kleene-star-expanded version where sequences of actions in a session type <emphasis>A</emphasis><subscript>1</subscript><emphasis>.....A</emphasis><emphasis><subscript>n</subscript></emphasis><emphasis>.</emphasis><emphasis role="strong">end</emphasis> are effectively expanded to allow arbitrary repetition of individual actions and entire interaction sequences: <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/10_equ07.jpg" mime-subtype="jpeg"/>.</para>
<para>We thus need some additional mechanism for enforcing proper linear use of channels, rather than relying on the discipline or morality of a programmer writing against a communication specification. We have also not yet considered branching behaviour or recursion, which are highlighted in the approaches from the literature.</para>
</section>
</section>
<section class="lev1" id="sec10-3" label="10.3" xreflabel="10.3">
<title>Approaches in the Literature</title>
<para>There are various different approaches in the literature providing session-typed concurrent, communicating programs in Haskell with linearity:</para>
<itemizedlist mark="bullet" spacing="normal">
<listitem><para>Neubauer and Thiemann [<link linkend="ch10_b9">9</link>] give an encoding of first-order single-channel session types with recursion;</para></listitem>
<listitem><para>Using <emphasis>parameterised monads</emphasis>, Pucella and Tov [<link linkend="ch10_b13">13</link>] provide multiple channels, recursion, and some building blocks for delegation, but require manual manipulation of a session type context; (<literal>http://hackage.haskell.org/package/simple-sessions</literal>)</para></listitem>
<listitem><para>Sackman and Eisenbach [<link linkend="ch10_b15">15</link>] provide an alternate approach where session types are constructed via a value-level witness; (<literal>http://hackage.haskell.org/package/sessions</literal>)</para></listitem>
<listitem><para>Imai et al. [<link linkend="ch10_b5">5</link>] extend Pucella-Tov with delegation and a more user-friendly approach to handling multiple channels; (<literal>http://hackage.haskell.org/package/full-sessions</literal>)</para></listitem>
<listitem><para>Orchard and Yoshida [<link linkend="ch10_b11">11</link>] use an embedding of effect systems into Haskell via graded monads based on a formal encoding of session-typed <emphasis>&#x03C0;</emphasis>-calculus into PCF with an effect system; (<literal>https://github.com/dorchard/sessions-in-haskell</literal>)</para></listitem>
<listitem><para>Lindley and Morris [<link linkend="ch10_b8">8</link>] provide a <emphasis>finally tagless</emphasis> embedding of the GV session-typed functional calculus into Haskell, building on a linear <emphasis>&#x03BB;-</emphasis>calculus embedding due to Polakow [<link linkend="ch10_b12">12</link>]. (<literal>https://github.com/jgbm/GVinHs</literal>)</para></listitem>
</itemizedlist>
<para>The following table summarises the various implementations&#x2019; support for desirable session-type implementation features: recursion, delegation, multiple channels (for which we summarise how session contexts are modelled and its members are accessed), idiomatic Haskell code, and whether manual user-given specification of session types is feasible.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/tab004.jpg" mime-subtype="jpeg"/></para>
<para>We characterise idiomatic Haskell as code which does not require interposing combinators to replace standard syntactic elements of functional languages, <emphasis>e.g.</emphasis>, <emphasis>&#x03BB;</emphasis>-abstraction, application, <emphasis>let</emphasis>-binding, recursive bindings, and variables. In the above, for example, PT08 has one tick and IYA10 has two since PT08 must use specialised combinators for handling multiple channel variables whilst IYA10 does not require such combinators, instead using standard Haskell variables.</para>
<section class="lev2" id="sec10-3-1" label="10.3.1" xreflabel="10.3.1">
<title>Note on Recursion and Duality</title>
<para>Early formulations of session types <emphasis>e.g.</emphasis> [<link linkend="ch10_b18">18</link>], defined duality of recursive types as <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/10_equ08.jpg" mime-subtype="jpeg"/>. Whilst this duality is suitable for tail-recursive session types, it is inadequate when recursive variables appear in a communicated type [<link linkend="ch10_b2">2</link>]. For example, the type <emphasis>&#x03BC;&#x03B1;.</emphasis><emphasis role="roman">!</emphasis> &#x3008;<emphasis>&#x03B1;</emphasis>&#x3009; should have the unfolded dual type of <emphasis role="roman">?</emphasis> &#x3008;<emphasis>&#x03BC;&#x03B1;.</emphasis><emphasis role="roman">!</emphasis> &#x3008;<emphasis>&#x03B1;</emphasis>&#x3009;&#x3009; but under the earlier approach is erroneously <emphasis role="roman">?</emphasis> &#x3008;<emphasis>&#x03BC;&#x03B1;.</emphasis><emphasis role="roman">?</emphasis>&#x3008;<emphasis>&#x03B1;</emphasis>&#x3009;&#x3009;. In Section 10.1, duality was defined using dualisable recursion variables, akin to Lindley and Morris [<link linkend="ch10_b7">7</link>], which solves this problem. However, all session-type implementations which support delegation and recursion (PT08, IYA10, OY16) implement the erroneous duality. This is an area for implementations to improve upon.</para>
</section>
<section class="lev2" id="sec10-3-2" label="10.3.2" xreflabel="10.3.2">
<title>Single Channel; Neubauer and Thiemann [<link linkend="ch10_b9">9</link>]</title>
<para>Neubauer and Thiemann provided the first published implementation of session types in Haskell. Their implementation is based on a translation from a simple session-typed calculus that is restricted to one end of a single communication channel. The session type theory is first order (<emphasis>i.e.</emphasis>, no channel delegation), but includes alternation and recursive sessions using a representation based on the following data types:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog115.jpg" mime-subtype="jpeg"/></para>
<para>Session types are specified by defining a value using the above data constructors which provides a homomorphic type-level representation of the session type. For example, the following value and its type describes a sequence of receiving two integers and sending a bool:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog116.jpg" mime-subtype="jpeg"/></para>
<para>where <literal>intW = 0</literal>, <literal>boolW = False</literal> witness the integer and boolean types and <literal>simple :: RECV MSG Int (RECV MSG Int (SEND MSG Bool EPS))</literal>.</para>
<para>Duality is provided by parameterising such specification values by placeholders for the &#x2018;send&#x2019; and &#x2018;receive&#x2019; actions which can then be applied to <literal>SEND_MSG</literal> and <literal>RECV_MSG</literal> in one order or the other to provide the dual specification. For example, the above specification becomes:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog117.jpg" mime-subtype="jpeg"/></para>
<para>This function specialisations to the dual behaviour of the server via <literal>(simple RECV_MSG SEND_MSG)</literal> and the client <literal>(simple SEND_MSG RECV_MSG)</literal>.</para>
<para>A recursive session type <emphasis role="roman">(</emphasis><emphasis>&#x03BC;&#x03B2;.&#x03B3;</emphasis><emphasis role="roman">)</emphasis> is represented as a fixed-point, via <literal>REC</literal>, of a parametric data type representing <emphasis>&#x03B3;</emphasis>. For Example 1, the body of the server&#x2019;s recursive type <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/10_equ09.jpg" mime-subtype="jpeg"/> can be represented by the following data type, which also uses <literal>ALT</literal>:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog118.jpg" mime-subtype="jpeg"/></para>
<para>where <literal>data Label = Eq | Nil</literal>. The full specification is constructed as:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog119.jpg" mime-subtype="jpeg"/></para>
<para>A computation at one end-point of a channel is represented by the <literal>Session</literal> data type which is indexed by the session type representation and internally wraps the <literal>IO</literal> monad. The main communication primitives produce values of <literal>Session</literal>:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog120.jpg" mime-subtype="jpeg"/></para>
<para>The <literal>SEND</literal> class provides sending values of type <literal>message</literal> given a continuation session with specification <literal>nextst</literal>, returning a computation with specification <literal>st</literal>. The <emphasis>functional dependency</emphasis> <literal>st message -&gt; nextst</literal> enforces that the instantiation of <literal>st</literal> and <literal>message</literal> uniquely determines <literal>nextst</literal>. An instance <literal>SEND (SEND MSG m b) m b</literal> specialises <literal>send</literal> to:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog121.jpg" mime-subtype="jpeg"/></para>
<para>The <literal>RECEIVE</literal> class abstracts receiving, taking a general continuation and returning a computation with communication specified by <literal>st</literal>. For <literal>RECV MSG</literal> and <literal>ALT</literal>,the <literal>receive</literal> method is specialised at the types:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog122.jpg" mime-subtype="jpeg"/></para>
<para>with <literal>RECV</literal> shorthand for <literal>RECEIVE</literal>. The Example 1 server can be defined:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog123.jpg" mime-subtype="jpeg"/></para>
<para>The communication pattern of <literal>session</literal> (line 3), encoded by its type, must match that of the specification <literal>exampleSpec SEND MSG RECV MSG</literal> as enforced by the <literal>run</literal> deconstructor which expects a computation of type <literal>Session st a</literal> and a corresponding specification value of type <literal>st</literal>. Any deviation from the specification is a static type error. Since computations are wrapped in the indexed <literal>Session</literal> type, they can only be executed via <literal>run</literal> and thus are always subject to this linearity check. This contrasts with the simple approach in Section 10.2 where actions on channels produce computations in the (unindexed) <literal>IO</literal> monad, which allowed arbitrary repetition of actions within the specified behaviour.</para>
</section>
<section class="lev2" id="sec10-3-3" label="10.3.3" xreflabel="10.3.3">
<title>Multi-Channel Linearity; Pucella and Tov [<link linkend="ch10_b13">13</link>]</title>
<para>Pucella and Tov improve on the previous approach, providing multi-channel session types with recursion and some higher-order support, though not full delegation. Similarly to Neubauer-Thiemann, the basic structure of session types is represented by several data types: binary type constructors <literal>:!:</literal> and <literal>:?:</literal> for send and receive and <literal>Eps</literal> for a closed session. Offering and selecting of choices are represented by binary type constructors <literal>:</literal><emphasis role="roman">&#x0026;</emphasis><literal>:</literal> and <literal>:+:</literal>, which differs to Neubauer-Thiemann who coalesce these dual perspectives into <literal>ALT</literal>. Duality is defined as a relation via a type class with a functional dependency enforcing bijectivity:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog124.jpg" mime-subtype="jpeg"/></para>
<para>Recursive session types use a De Bruijn encoding where <literal>Rec r</literal> introduces a new recursive binder over <literal>r</literal> and <literal>Var n</literal> is the De Bruijn index of the <emphasis>n</emphasis><emphasis><sup>th</sup></emphasis> binder where <emphasis>n</emphasis> has a unary encoding (<emphasis>e.g.</emphasis>, <literal>Z</literal>, <literal>S Z</literal>, <emphasis>etc.</emphasis>).</para>
<para>Communication is provided by channels <literal>Channel c</literal> (which we abbreviate to <literal>Chan c</literal>) where the type variable <literal>c</literal> represents the name of the channel. The session type of a channel <literal>c</literal> is then a <emphasis>capability</emphasis> provided by the data type <literal>Cap c e s</literal> which associates session type <literal>s</literal> to channel <literal>c</literal> with an environment <literal>e</literal> of recursive variables paired with session types.</para>
<para>A <emphasis>parameterised monad</emphasis> [<link linkend="ch10_b1">1</link>] is used to capture the session types of the free channels in a computation. Parameterised monads generalise monads to type constructors indexed by a pair of types akin to pre- and post-conditions. Its operations are represented via the class:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog125.jpg" mime-subtype="jpeg"/></para>
<para>The &#8220;bind&#8221; operation <literal>&gt;&gt;=</literal> for sequential composition has type indices representing sequential composition of Hoare triples: a computation with postcondition <literal>q</literal> can be composed with a computation with pre-condition <literal>q</literal>. Relatedly, a pure value of type <literal>a</literal> can be lifted into a trivial computation which preserves any pre-condition <literal>p</literal> in its post-condition.</para>
<para>One of the original examples of parameterised monads is for encoding first-order single-channel session-typed computations [<link linkend="ch10_b1">1</link>]. This is expanded upon by Pucella and Tov to multi-channels. They provide a parameterised monad <literal>Session</literal>, indexed by <emphasis>stacks</emphasis> of session type capabilities associated to channels. Pre-conditions are the channel capabilities at the start of a computation, and post-conditions are the remaining channel capabilities after computation.</para>
<para>Stacks are constructed out of tuples where <literal>()</literal> is the empty stack. For example, <literal>(Chan c e s, (Chan c&#x2019; e&#x2019; s&#x2019;, ()))</literal> is a stack of two capabilities for channels <literal>c</literal> and <literal>c&#x2019;</literal>. The core communication primitives then manipulate the capability at the top of the stack:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog126.jpg" mime-subtype="jpeg"/></para>
<para>For example, sending a value of type <literal>a</literal> on channel <literal>c</literal> requires the capability <literal>a :!: s</literal> at the top of the stack for <literal>c</literal> in the pre-condition, which becomes <literal>s</literal> in the post condition. Branching follows a similar scheme.</para>
<para>Recursive behaviour is mediated by combinators which provide the unrolling of a recursive session type (<literal>enter</literal>) and referencing a bound De-Bruijn-indexed recursive variable via <literal>zero</literal> and <literal>suc</literal>:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog127.jpg" mime-subtype="jpeg"/></para>
<para>Thus, entering a recursive sessions type adds the body of the type onto the top of De-Bruijn environment stack; <literal>zero</literal> peeks the session type from the top of the stack and <literal>suc</literal> pops and decrements the variable. The original paper has a slightly different but equivalent formulation for <literal>suc</literal>&#x2013; the above is provided by the online implementation.</para>
<para>Example 1 can then be implemented as follows:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog128.jpg" mime-subtype="jpeg"/></para>
<para>The types of both can be inferred. For example, the type of <literal>server</literal> is:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog129.jpg" mime-subtype="jpeg"/></para>
<para>Dual endpoints of a channel are created by functions <literal>accept</literal> and <literal>request</literal> capturing the notion of <emphasis>shared channels</emphasis> [<link linkend="ch10_b18">18</link>], called a <emphasis>rendezvous</emphasis> here:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog130.jpg" mime-subtype="jpeg"/></para>
<para>Thus, for our example, the server and client processes can be composed by the following code which statically enforces duality through <literal>request</literal>:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog131.jpg" mime-subtype="jpeg"/></para>
<para>with <literal>forkSession :: Session x () () -&gt; Session x () ()</literal> enforcing a closed final state for the forked subcomputation (line 2). Whilst the above code is fairly idiomatic Haskell (modulo the management of recursion variables), the example has only one channel. In the context of multiple channels, the capability of a channel may not be at the top of the session environment stack, thus context manipulating combinators must be used to rearrange the stack:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog132.jpg" mime-subtype="jpeg"/></para>
<para>where <literal>swap</literal> is akin to exchange and <literal>dig</literal> moves down one place in the stack. Thus, multi-channel code requires the user to understand the type-level stack representation and to manipulate it explicitly. Multi-channel code is therefore non-idiomatic, in the sense that we can&#x2019;t just use Haskell variables on their own.</para>
<para>Example 2 cannot be captured as channels cannot be passed. Pucella and Tov provide a way to send and receive capabilities, however there is no primitive for sending channels along with an associated capability. Imai <emphasis>et al.</emphasis> describe a way to build this on top of Pucella and Tov&#x2019;s approach with an existentially quantified channel name, however this is still limited by the lack of a new channel constructor. Instead, channel delegation could be emulated with global shared channels for every delegation but this shifts away from the message-passing paradigm.</para>
<para>In their paper, Pucella and Tov use the <literal>ixdo</literal> notation which copies exactly the style of the <literal>do</literal> notation for monads, but which is desugared by a preprocessor into the operations of the parameterised monad. In modern GHC, this can be replaced with the <literal>RebindableSyntax</literal> extension which desugars the standard <literal>do</literal> notation using any functions in scope named <literal>(&gt;&gt;=)</literal> and <literal>return</literal>, regardless of their type. The operations of a parameterised monad can therefore usurp the regular monad operations. Thus, the non-idiomatic pre-processed <literal>ixdo</literal> notation can be replaced with idiomatic <literal>do</literal> notation. The same applies to the work of Sackman and Eisenbach (Section 10.3.4) and Imai <emphasis>et al.</emphasis> (Section 10.3.5) who also use parameterised monads. Similarly, GHC&#x2019;s rebindable syntax is reused by Orchard and Yoshida with <emphasis>graded monads</emphasis> (Section 10.3.6).</para>
</section>
<section class="lev2" id="sec10-3-4" label="10.3.4" xreflabel="10.3.4">
<title>An Alternate Approach; Sackman and Eisenbach [<link linkend="ch10_b15">15</link>]</title>
<para>In their unpublished manuscript, Sackman and Eisenbach provide an implementation also using a parameterised monad but with quite a different formulation to Pucella and Tov. The encoding of session environments is instead through type-level finite maps from channel names (as types) to session types. This requires significantly more type-level machinery (implemented mostly using classes with functional dependencies), resulting in much more complicated types than Pucella-Tov. However, they provide a parameterised monad <literal>SessionType</literal> for constructing session-type witnesses at the value level (similarly to Neubauer-Thiemann) which is much easier to read and write than the corresponding type-level representation. Session-based computations are then constructed through another parameterised monad called <literal>SessionChain</literal>.</para>
<para>Sackman-Eisenbach represent session types by type-level lists (via constructors <literal>Cons</literal> and <literal>Nil</literal>) of actions given by parametric data types <literal>Send</literal>, <literal>Recv</literal>, <literal>Select</literal>, <literal>Offer</literal>, <literal>Jump</literal>, and (non parametric) <literal>End</literal> similar to the other representations. For Example 2, the recursive session type of the server can be constructed via value-level terms as:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog133.jpg" mime-subtype="jpeg"/></para>
<para>This uses the <literal>SessionType</literal> parameterised monad indexed by <literal>TypeState</literal> types which have further indices managing labels and representing session types. The <literal>makeSessionType</literal> function returns a pair of a value capturing the specification <literal>serverSpec</literal> and the component of the type labelled by <literal>a</literal>. Labels are used to associate types to channels and for recursive types, where <literal>newLabel</literal> generates a fresh label bound to <literal>a</literal>. The third line associates to <literal>a</literal> the expected session behaviour: a choice is offered where <literal>offer</literal> takes a list of behaviours constructed by <literal>~|~</literal> (cons) and <literal>BLNil</literal> (nil). As in Neubauer-Thiemann, <literal>intW</literal> and <literal>boolW</literal> are value witnesses of types. The recursive step is via <literal>jump</literal> on label <literal>a</literal>. The type of <literal>send</literal> illustrates the <literal>SessionType</literal> parameterised monad:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog134.jpg" mime-subtype="jpeg"/></para>
<para>The final parameter to <literal>TypeState</literal> provides a type-level list of labelled session types (themselves lists). In the post-condition, the session type <literal>f</literal> from the head of the list in the pre-condition has <literal>Send</literal> consed onto the front, parameterised by <literal>(Normal, t)</literal> indicating the value type <literal>t</literal>.</para>
<para>The session-type building primitives have computation building counterparts (whose names are prefixed with <literal>s</literal>, <emphasis>e.g.</emphasis> <literal>ssend</literal>) returning computations in the <literal>SessionChain</literal> parameterised monad. We elide the details, but show the implementation of the server from Example 2:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog135.jpg" mime-subtype="jpeg"/></para>
<para>The session type specification <literal>serverSpec</literal> is linked to computation to enforce linearity via <literal>fork</literal>. Above, <literal>client</literal> refers to the client code which is forked and given a channel whose behaviour is dual to that created locally by <literal>createSession</literal>, specified by <literal>serverSpec</literal>. The <literal>sjump</literal> primitive provides the recursive behaviour but has no target which is implicitly provided by the specification. The <literal>withChannel</literal> primitive &#8220;focuses&#8221; the computation on a particular channel such that the communication primitives are not parameterised by channels, similar to Neubauer-Thiemann. This has some advantage over Pucella-Tov, which required manual session-context manipulation, though channel variables still cannot be used directly here. Combined with the complicated type encoding, we therefore characterise this approach as the least idiomatic.</para>
<para>It should be noted that since the appearance of their manuscript, the type checking of functional dependencies in GHC has become more strict (particularly with the additional <emphasis>Coverage Condition</emphasis> [<link linkend="ch10_b16">16</link>, Def. 7]). At the time of writing, the latest available online implementation of Sackman-Eisenbach fails to type check in multiple places due to the coverage conditions added later to GHC. It is not immediately clear how to remedy this due to their reliance on functional dependencies which do not obey the new coverage condition.</para>
</section>
<section class="lev2" id="sec10-3-5" label="10.3.5" xreflabel="10.3.5">
<title>Multi-Channels with Inference; Imai et al. [<link linkend="ch10_b5">5</link>]</title>
<para>Imai, Yuen, and Agusa directly extend the Pucella-Tov approach, providing type inference, delegation, and solving the deficiencies with accessing multiple channels. They replace the positional, stack-based approach for multiple channels with a De Bruijn index encoding which is handled implicitly at the type level. For example, <literal>send</literal> has type</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog136.jpg" mime-subtype="jpeg"/></para>
<para>Computations are modelled by the parameterised monad <literal>Session</literal> as before, but now pre- and post-condition indices <literal>ss</literal> and <literal>ss&#x2019;</literal> are type-level lists of session types, rather than a labelled stack. Whilst these structures are isomorphic, the way session types are accessed within the list representation differs considerably.</para>
<para>A channel <literal>Channel t n</literal> has a type-level natural number <literal>n</literal> representing the position of the channel&#x2019;s session type in the list. The constraint <literal>Pickup</literal> above specifies that at the <literal>n</literal><emphasis><sup>th</sup></emphasis> position in <literal>ss</literal> is the session type <literal>Send v a</literal>. The constraint <literal>Update</literal> then states that <literal>ss&#x2019;</literal> is the list of session types produced by replacing the <literal>n</literal><emphasis><sup>th</sup></emphasis> position in <literal>ss</literal> with the session type <literal>a</literal>. The rest of the communication primitives follow a similar scheme to the above, generalising Pucella-Tov primitives to work with the De Bruijn indices instead of just the capability at the top of the stack.</para>
<para>A fresh channel can be created by the following combinator:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog137.jpg" mime-subtype="jpeg"/></para>
<para>where <literal>l</literal> is the length of the list <literal>ss</literal> as defined by the constraint <literal>SList</literal>, and thus is a fresh variable for the computation.</para>
<para>Using this library leads to highly idiomatic Haskell code, with no additional combinators required for managing the context of session-typed channels. Both examples can be implemented, with code similar to that shown for Pucella-Tov in Section 10.3.3. The one downside of this approach however is that the types, whilst they can be inferred (which is one of the aims of their work), are complex and difficult to read, let alone write. Relatedly, the type errors can be difficult to understand due to the additional type-level mechanisms for managing the contexts.</para>
</section>
<section class="lev2" id="sec10-3-6" label="10.3.6" xreflabel="10.3.6">
<title>Session Types via Effect Types; Orchard and Yoshida [<link linkend="ch10_b11">11</link>]</title>
<para>Orchard and Yoshida studied the connection between effect systems and session types. One part of the work showed the encoding of a session-typed <emphasis>&#x03C0;</emphasis>-calculus into a parallel variant of PCF with a general, parameterised effect system. This formal encoding was then combined with an approach for embedding effect systems in Haskell [<link linkend="ch10_b10">10</link>] to provide a new implementation of session-typed channels in Haskell. The implementation supports multiple channels in an idiomatic style, delegation, and a restricted form of recursion (affine recursion only).</para>
<para>The embedding of general effect systems in Haskell types is provided by a <emphasis>graded monad</emphasis> structure, which generalises monads to type constructors indexed by a type-representation of effect information. This &#8220;effect type&#8221; has the additional structure of a monoid, encoded using type families. The graded monad structure in Haskell is defined:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog138.jpg" mime-subtype="jpeg"/></para>
<para>Thus a value of type <literal>m f a</literal> denotes a computation with effects described by the type index <literal>f</literal> of kind <literal>ef</literal>. The <literal>return</literal> operation lifts a value to a trivially effectful computation, marked with the type <literal>Unit m</literal>. The &#8220;bind&#8221; operation (<literal>&gt;&gt;=</literal>) provides the sequential composition of effectful computations, with effect information composed by the type-level binary function <literal>Plus m</literal>. The session type embedding is provided by a graded monad structure for the data type <literal>Process</literal>:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog139.jpg" mime-subtype="jpeg"/></para>
<para>Type indices <literal>s</literal> are finite maps of the form <literal>&#x2019;[c :-&gt; s, d :-&gt; t,</literal> <emphasis>...</emphasis><literal>]</literal> mapping channel names <literal>c</literal>, <literal>d</literal> to session types <literal>s</literal>, <literal>t</literal>. The <literal>Session</literal> kind is given by a data type (representing a standard grammar of session types) promoted by the <emphasis>data kinds</emphasis> extension of GHC to the kind-level.</para>
<para>The <literal>Plus</literal> type operation of the <literal>Process</literal> graded monad takes the <emphasis>union</emphasis> of two finite maps and sequentially composes the session types of any channels that appear in both of the finite maps. This relies on the closed type family feature of GHC to define type-level functions that can match on their types, <emphasis>e.g.</emphasis>, to compare types for equality.</para>
<para>The core send and received primitives then have the following types:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog140.jpg" mime-subtype="jpeg"/></para>
<para>In each, the type-index on <literal>Process</literal> gives a singleton finite map from the channel name <literal>c</literal> to the session type. We elide the rest of the combinators. Duality is enforced when a pair of channel endpoints is created by <literal>new</literal>:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog141.jpg" mime-subtype="jpeg"/></para>
<para>where <literal>:</literal><emphasis>\</emphasis> removes a channel&#x2019;s session type from the environment.</para>
<para>A non-recursive implementation of Example 2 can be defined:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog142.jpg" mime-subtype="jpeg"/></para>
<para>which are composed by <literal>new (</literal><emphasis>\</emphasis><literal>(c, c&#x2019;) -&gt; client c &#x2018;par&#x2018; server c&#x2019;)</literal>.</para>
<para>One advantage of this approach is that most types are easy to write by hand, with a succinct understandable presentation in terms of the finite maps from channel names to session types. Furthermore, the use of multiple channels is idiomatic, using Haskell&#x2019;s normal variables. The major disadvantage of this approach is that the user must give their own explicit type-level names to the channels, <emphasis>e.g.</emphasis>, type signatures like <literal>Chan (Ch "c")</literal> above. For simple examples this is not a burden, but manually managing uniqueness of variables does not scale well.</para>
<para>Furthermore, the approach is brittle due to complex type-level representation and manipulations of finite maps. For example, GHC has difficulty reasoning about the type-level <emphasis>union</emphasis> operation (used as <literal>Plus</literal>) when applied to types involving some polymorphism.</para>
</section>
<section class="lev2" id="sec10-3-7" label="10.3.7" xreflabel="10.3.7">
<title>GV in Haskell; Lindley and Morris [<link linkend="ch10_b8">8</link>]</title>
<para>GV is a session-typed linear functional calculus, proposed by [<link linkend="ch10_b17">17</link>], based on the work of Gay and Vasconcelos [<link linkend="ch10_b3">3</link>], and adapted further by Lindley and Morris [<link linkend="ch10_b6">6</link>]. The GV presented by Lindley and Morris aims at re-use of standard components, defined as an extension of the linear <emphasis>&#x03BB;</emphasis>-calculus with session-typed communication primitives. This provides a basis for their Haskell implementation by reusing an embedding of the linear <emphasis>&#x03BB;</emphasis>-calculus into Haskell due to Polakow [<link linkend="ch10_b12">12</link>]. Polakow&#x2019;s embedding provides a &#8220;tagless final&#8221; encoding of the linear-<emphasis>&#x03BB;</emphasis> calculus (LLC), meaning that terms of LLC are represented by functions of a type class, whose interpretation/implementation can be varied based on the underlying type. Furthermore, the embedding uses higher-order abstract syntax (HOAS) <emphasis>i.e.</emphasis>, binders in LLC are represented by Haskell binders.</para>
<para>To represent the linear types notion of context <emphasis>consumption</emphasis>, contexts are split in two with judgments of the form: <emphasis role="roman">&#x0394;</emphasis><emphasis><subscript>I</subscript></emphasis> <emphasis>\</emphasis> <emphasis role="roman">&#x0394;</emphasis><emphasis><subscript>O</subscript> </emphasis>&#x22A2;<emphasis></emphasis> <emphasis>e</emphasis> <emphasis role="roman">:</emphasis> <emphasis>A</emphasis> with <emphasis>input context</emphasis> <emphasis role="roman">&#x0394;</emphasis><emphasis><subscript>I</subscript></emphasis> and output context <emphasis role="roman">&#x0394;</emphasis><emphasis><subscript>O</subscript></emphasis> which remains after computing <emphasis>e</emphasis> and thus after some parts of <emphasis role="roman">&#x0394;</emphasis><emphasis><subscript>I</subscript></emphasis> have been consumed. Contexts come equipped with the notion of a &#8220;hole&#8221; (written <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/10_equ10.jpg" mime-subtype="jpeg"/>) denoting a variable that has been consumed. For example, a linear variable use is typed by <emphasis role="roman">&#x0394;</emphasis><emphasis>,x</emphasis> <emphasis role="roman">:</emphasis> <emphasis>A,</emphasis> <emphasis role="roman">&#x0394;</emphasis><sup>&#x2032;</sup> <emphasis>\</emphasis> <emphasis role="roman">&#x0394;</emphasis><emphasis>,</emphasis> <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/10_equ10.jpg" mime-subtype="jpeg"/><emphasis>,</emphasis> <emphasis role="roman">&#x0394;</emphasis><sup>&#x2032;</sup> <emphasis> </emphasis>&#x22A2;<emphasis>x</emphasis> <emphasis role="roman">:</emphasis> <emphasis>A</emphasis>.</para>
<para>The embedding of this linear type system uses natural numbers to represent variables in judgements. Judgements are represented by types <literal>repr :: Nat -&gt; [Maybe Nat] -&gt; [Maybe Nat] -&gt; * -&gt; *</literal>. Thus, the LLC term representation is a type indexed by four pieces of information: a natural number denoting a fresh name for a new variable, the input context (a list of <literal>Maybe Nat</literal> where <literal>Just n</literal> is a variable and <literal>Nothing</literal> denotes <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/10_equ10.jpg" mime-subtype="jpeg"/>), the output context, and the term type.</para>
<para>The core of the embedding for the linear function space fragment, is then given by the <literal>LLC</literal> class, parameterised by a <literal>repr</literal> type:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog143.jpg" mime-subtype="jpeg"/></para>
<para>where <literal>LVar</literal> represents linear variables, defined as the type <literal>forall v i o . (Consume x i o) =&gt; repr v i o a</literal> describing that using a variable leads to its consumption for all input and output contexts <literal>i</literal> and <literal>o</literal>.</para>
<para>The session primitives of GV are added atop the LLC embedding via another tagless final encoding (we elide the primitives for branching):</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog144.jpg" mime-subtype="jpeg"/></para>
<para>The types involve duality as both a predicate (type constraint) <literal>DualS</literal> and as a type-level function <literal>Dual</literal>.</para>
<para>The approach does not provide recursive sessions so we implement a non-recursive version of Example 1 as:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog145.jpg" mime-subtype="jpeg"/></para>
<para>This approach cleanly separates the notion of linearity from the channel capabilities of session types. The main downside is that application, <emphasis>&#x03BB;-</emphasis>abstraction, and composition of terms must be mediated by the combinators of the LLC embedding. Therefore, the approach does not support idiomatic Haskell programming.</para>
</section>
</section>
<section class="lev1" id="sec10-4" label="10.4" xreflabel="10.4">
<title>Future Direction and Open Problems</title>
<para>The table at the beginning of Section 10.3 (p. 226) indicates that there is no one implementation that provides all desirable features: a session-typed library for communication-safe concurrency with linearity, delegation, multiple-channels, recursion, idiomatic Haskell code, and the ability to easily give session type specifications by hand. Furthermore, none correctly implements duality with respect to recursion (Section 10.3.1).</para>
<para>So far there appears to be a trade-off between these different features. Pucella and Tov provide an idiomatic system with relatively simple types, but require the manual management of the capability stack. The work of Imai <emphasis>et al.</emphasis> provides a highly idiomatic system, but the types are hard to manipulate and understand. Orchard and Yoshida provide types that are easy to write, but at the cost of forcing the user to manually manage fresh channel names. Lindley and Morris handle variables idiomatically, but require additional combinators for application, <emphasis>&#x03BB;</emphasis>-abstraction and term composition. Sackman and Eisenbach provide session types which are easily specified by-hand with a value witness, but with non-idiomatic code and hard to manipulate types.</para>
<para>One possible solution is to adapt the approach of Orchard and Yoshida with a way to generate fresh channel names at the type-level automatically via a GHC <emphasis>type checker plugin</emphasis> (see, <emphasis>e.g.</emphasis>, [<link linkend="ch10_b4">4</link>]). Alternatively, existential names can be used for fresh names. However, the implementation of type-level finite maps relies on giving an arbitrary ordering to channel names (for the sake of normalisation) which is not possible for existential names. In which case, a type-checker plugin could provide built-in support for finite maps more naturally, rather than using the current (awkward) approach of Orchard and Yoshida.</para>
<para>We have examined the six major session type implementations for Haskell in this chapter. All of them provide static linear checks, leveraging Haskell&#x2019;s flexible type system, but all have some deficiencies; finding a perfectly balanced system remains an open problem.</para>
<para><emphasis role="strong">Acknoweldgements</emphasis> We thank Garrett Morris and the anonymous reviewers for their helpful comments. This work was supported in part by EPSRC grants EP/K011715/1, EP/K034413/1, EP/L00058X/1, EP/M026124/1, and EU project FP7-612985 UpScale.</para>
</section>
<section class="lev1" id="sec2a">
<title>References</title>
<orderedlist numeration="arabic" continuation="restarts" spacing="normal">
<listitem><para><anchor id="ch10_b1"/>Robert Atkey. Parameterised notions of computation. <emphasis>Journal of functional programming</emphasis>, 19(3&#x2013;4):335&#x2013;376, 2009. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Robert+Atkey%2E+Parameterised+notions+of+computation%2E+Journal+of+functional+programming%2C+19%283-4%29%3A335-376%2C+2009%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch10_b2"/>Giovanni Bernardi, Ornela Dardha, Simon J. Gay, and Dimitrios Kouzapas. On duality relations for session types. In <emphasis>Trustworthy Global Computing 2014</emphasis>, pages 51&#x2013;66, 2014. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Giovanni+Bernardi%2C+Ornela+Dardha%2C+Simon+J%2E+Gay%2C+and+Dimitrios+Kouzapas%2E+On+duality+relations+for+session+types%2E+In+Trustworthy+Global+Computing+2014%2C+pages+51-66%2C+2014%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch10_b3"/>Simon J. Gay and Vasco T. Vasconcelos. Linear type theory for asynchronous session types. <emphasis>Journal of Functional Programming</emphasis>, 20(01): 19&#x2013;50, 2010. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Simon+J%2E+Gay+and+Vasco+T%2E+Vasconcelos%2E+Linear+type+theory+for+asynchronous+session+types%2E+Journal+of+Functional+Programming%2C+20%2801%29%3A+19-50%2C+2010%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch10_b4"/>Adam Gundry. A typechecker plugin for units of measure: domain-specific constraint solving in GHC Haskell. In <emphasis>ACM SIGPLAN Notices</emphasis>, volume 50, pages 11&#x2013;22. ACM, 2015. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Adam+Gundry%2E+A+typechecker+plugin+for+units+of+measure%3A+domain-specific+constraint+solving+in+GHC+Haskell%2E+In+ACM+SIGPLAN+Notices%2C+volume+50%2C+pages+11-22%2E+ACM%2C+2015%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch10_b5"/>Keigo Imai, Shoji Yuen, and Kiyoshi Agusa. Session Type Inference in Haskell. In <emphasis>PLACES</emphasis>, pages 74&#x2013;91, 2010. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Keigo+Imai%2C+Shoji+Yuen%2C+and+Kiyoshi+Agusa%2E+Session+Type+Inference+in+Haskell%2E+In+PLACES%2C+pages+74-91%2C+2010%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch10_b6"/>Sam Lindley and J. Garrett Morris. A semantics for propositions as sessions. In <emphasis>ESOPb</emphasis>, pages 560&#x2013;584. Springer, 2015. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Sam+Lindley+and+J%2E+Garrett+Morris%2E+A+semantics+for+propositions+as+sessions%2E+In+ESOPb%2C+pages+560-584%2E+Springer%2C+2015%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch10_b7"/>Sam Lindley and J. Garrett Morris. Talking Bananas: Structural Recursion for Session Types. In <emphasis>Proceedings of the 21st ACM SIGPLAN International Conference on Functional Programming</emphasis>, ICFP 2016, pages 434&#x2013;447. ACM, 2016. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Sam+Lindley+and+J%2E+Garrett+Morris%2E+Talking+Bananas%3A+Structural+Recursion+for+Session+Types%2E+In+Proceedings+of+the+21st+ACM+SIGPLAN+International+Conference+on+Functional+Programming%2C+ICFP+2016%2C+pages+434-447%2E+ACM%2C+2016%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch10_b8"/>Sam Lindley and J Garrett Morris. Embedding session types in haskell. In <emphasis>Proceedings of the 9th International Symposium on Haskell</emphasis>, pages 133&#x2013;145. ACM, 2016. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Sam+Lindley+and+J+Garrett+Morris%2E+Embedding+session+types+in+haskell%2E+In+Proceedings+of+the+9th+International+Symposium+on+Haskell%2C+pages+133-145%2E+ACM%2C+2016%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch10_b9"/>Matthias Neubauer and Peter Thiemann. An Implementation of Session Types. In <emphasis>PADL</emphasis>, volume 3057 of <emphasis>LNCS</emphasis>, pages 56&#x2013;70. Springer, 2004. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Matthias+Neubauer+and+Peter+Thiemann%2E+An+Implementation+of+Session+Types%2E+In+PADL%2C+volume+3057+of+LNCS%2C+pages+56-70%2E+Springer%2C+2004%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch10_b10"/>Dominic Orchard and Tomas Petricek. Embedding effect systems in Haskell. <emphasis>ACM SIGPLAN Notices</emphasis>, 49(12):13&#x2013;24, 2015. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Dominic+Orchard+and+Tomas+Petricek%2E+Embedding+effect+systems+in+Haskell%2E+ACM+SIGPLAN+Notices%2C+49%2812%29%3A13-24%2C+2015%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch10_b11"/>Dominic Orchard and Nobuko Yoshida. Effects as Sessions, Sessions as Effects. <emphasis>ACM SIGPLAN Notices</emphasis>, 51(1):568&#x2013;581, 2016. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Dominic+Orchard+and+Nobuko+Yoshida%2E+Effects+as+Sessions%2C+Sessions+as+Effects%2E+ACM+SIGPLAN+Notices%2C+51%281%29%3A568-581%2C+2016%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch10_b12"/>Jeff Polakow. Embedding a Full Linear Lambda Calculus in Haskell. <emphasis>ACM SIGPLAN Notices</emphasis>, 50(12):177&#x2013;188, 2016. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Jeff+Polakow%2E+Embedding+a+Full+Linear+Lambda+Calculus+in+Haskell%2E+ACM+SIGPLAN+Notices%2C+50%2812%29%3A177-188%2C+2016%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch10_b13"/>Riccardo Pucella and Jesse A. Tov. Haskell Session Types with (Almost) no Class. In <emphasis>Proc. of Haskell Symposium &#x2019;08</emphasis>, pages 25&#x2013;36. ACM, 2008. ISBN 978-1-60558-064-7. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Riccardo+Pucella+and+Jesse+A%2E+Tov%2E+Haskell+Session+Types+with+%28Almost%29+no+Class%2E+In+Proc%2E+of+Haskell+Symposium+%2708%2C+pages+25-36%2E+ACM%2C+2008%2E+ISBN+978-1-60558-064-7%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch10_b14"/>John H. Reppy. CML: A Higher-Order Concurrent Language. In <emphasis>PLDI</emphasis>, pages 293&#x2013;305, 1991. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=John+H%2E+Reppy%2E+CML%3A+A+Higher-Order+Concurrent+Language%2E+In+PLDI%2C+pages+293-305%2C+1991%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch10_b15"/>Matthew Sackman and Susan Eisenbach. Session Types in Haskell (Updating Message Passing for the 21st Century), 2008. Technical report, Imperial College London. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Matthew+Sackman+and+Susan+Eisenbach%2E+Session+Types+in+Haskell+%28Updating+Message+Passing+for+the+21st+Century%29%2C+2008%2E+Technical+report%2C+Imperial+College+London%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch10_b16"/>Martin Sulzmann, Gregory J Duck, Simon Peyton-Jones, and Peter J Stuckey. Understanding functional dependencies via constraint handling rules. <emphasis>Journal of Functional Programming</emphasis>, 17(01):83&#x2013;129, 2007. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Martin+Sulzmann%2C+Gregory+J+Duck%2C+Simon+Peyton-Jones%2C+and+Peter+J+Stuckey%2E+Understanding+functional+dependencies+via+constraint+handling+rules%2E+Journal+of+Functional+Programming%2C+17%2801%29%3A83-129%2C+2007%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch10_b17"/>Philip Wadler. Propositions as sessions. <emphasis>Journal of Functional Programming</emphasis>, 24(2&#x2013;3):384&#x2013;418, 2014. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Philip+Wadler%2E+Propositions+as+sessions%2E+Journal+of+Functional+Programming%2C+24%282-3%29%3A384-418%2C+2014%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch10_b18"/>Nobuko Yoshida and Vasco Thudichum Vasconcelos. Language Primi tives and Type Discipline for Structured Communication-Based Programming Revisited: Two Systems for Higher-Order Session Communication. <emphasis>Electr. Notes Theor. Comput. Sci.</emphasis>, 171(4):73&#x2013;93, 2007. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Nobuko+Yoshida+and+Vasco+Thudichum+Vasconcelos%2E+Language+Primi+tives+and+Type+Discipline+for+Structured+Communication-Based+Programming+Revisited%3A+Two+Systems+for+Higher-Order+Session+Communication%2E+Electr%2E+Notes+Theor%2E+Comput%2E+Sci%2E%2C+171%284%29%3A73-93%2C+2007%2E" target="_blank">Google Scholar</ulink></para></listitem>
</orderedlist>
</section>
</chapter>
<chapter class="chapter" id="ch11" label="11" xreflabel="11">
<title>An <literal>OCaml</literal> Implementation of Binary Sessions</title>
<para><emphasis role="strong">Hern&#x00E1;n Melgratti</emphasis><footnote id="fn11_1a" label="1"><para>Departamento de Computaci&#x00F3;n, Universidad de Buenos Aires, Argentina</para></footnote>,<footnote id="fn11_2a" label="2"><para>CONICET-Universidad de Buenos Aires, Instituto de Investigaci&#x00F3;n en Ciencias de la Computaci&#x00F3;n (ICC), Buenos Aires, Argentina</para></footnote> <emphasis role="strong">and Luca Padovani</emphasis><footnote id="fn11_3a" label="3"><para>Dipartimento di Informatica, Universit&#x00E0; di Torino, Italy</para></footnote></para>
<section class="lev2">
<title>Abstract</title>
<para>In this chapter we describe <literal>FuSe</literal>, a simple <literal>OCaml</literal> module that implements binary sessions and enables a hybrid form of session type checking without resorting to external tools or extensions of the programming language. The approach combines static and dynamic checks: the former ones are performed at compile time and concern the structure of communication protocols; the latter ones are performed as the program executes and concern the linear usage of session endpoints. We recall the minimum amount of theoretical background for understanding the essential aspects of the approach (Section 11.1) and then describe the API of the <literal>OCaml</literal> module throughout a series of simple examples (Section 11.2). In the second half of the chapter we detail the implementation of the module (Section 11.3) and discuss a more complex and comprehensive example, also arguing about the effectiveness of the hybrid approach with respect to the early detection of protocol violations (Section 11.4). We conclude with a survey of closely related work (Section 11.5).</para>
<para>The source code of <literal>FuSe</literal>, which is partially described in this chapter and can be used to compile and run all the examples given therein, can be downloaded from the second author&#x2019;s home page.</para>
</section>
<section class="lev1" id="sec11-1" label="11.1" xreflabel="11.1">
<title>An API for Sessions</title>
<para>We consider the following grammar of types and session types</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e244_01.jpg" mime-subtype="jpeg"/></para>
<para>where types, ranged over by <emphasis>t</emphasis> and <emphasis>s</emphasis>, include basic types, type variables, session types, disjoint sums, and possibly other (unspecified) types. Session types, ranged over by <emphasis>T</emphasis> and <emphasis>S</emphasis> , comprise the usual constructs for denoting depleted session endpoints, input/output operations, branches and choices, as well as possibly dualized session type variables <emphasis>A</emphasis>, <emphasis>B</emphasis>,etc.</para>
<para>The <emphasis>dual</emphasis> of a session type <emphasis>T</emphasis> , written <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/equ244_1.jpg" mime-subtype="jpeg"/>, is obtained as usual by swapping input and output operations and is defined by the following equations:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e244_02.jpg" mime-subtype="jpeg"/></para>
<para>Following Gay and Vasconcelos [<link linkend="ch11_b4">4</link>], our aim is to incorporate binary sessions into a (concurrent) functional language by implementing the API shown in <link linkend="tab11_1">Table <xref linkend="tab11_1" remap="11.1"/></link>. The <literal>create</literal> function creates a new session and returns a pair with its two peer endpoints with dual session types. The <literal>close</literal> function is used to signal the fact that a session is completed and no more communications are supposed to occur in it. The <literal>send</literal> and <literal>receive</literal> functions are used for sending and receiving a message, respectively: <literal>send</literal> sends a message of type &#x03B1; over an endpoint of type <literal>!</literal>&#x03B1;<literal>.</literal><emphasis>A</emphasis> and returns the same endpoint with its type changed to <emphasis>A</emphasis> to reflect that the communication has occurred; <literal>receive</literal> waits for a message of type &#x03B1; from an endpoint of type <literal>?</literal>&#x03B1;<literal>.</literal><emphasis>A</emphasis> and returns a pair with the message and the same endpoint with its type changed to <emphasis>A</emphasis>. The <literal>branch</literal> and <literal>select</literal> functions deal with sessions that may continue along different paths of interaction, each path being associated with a <emphasis>label</emphasis> <literal>l</literal><emphasis><subscript>i</subscript></emphasis>. Intuitively, <literal>select</literal> takes a label <literal>l</literal><emphasis><subscript>k</subscript></emphasis> and an endpoint of type &#x2295;<literal>[l</literal><emphasis><subscript>i</subscript></emphasis> : <emphasis>A</emphasis><emphasis><subscript>i</subscript></emphasis><literal>]</literal><emphasis><subscript>i</subscript></emphasis><subscript>&#x03B5;</subscript><emphasis><subscript>I</subscript></emphasis> where <emphasis>k</emphasis> <literal>&#x03B5;</literal> <emphasis>I</emphasis>, sends the label over the endpoint and returns the endpoint with its type changed to <emphasis>A</emphasis><emphasis><subscript>k</subscript></emphasis>, which is the continuation corresponding to the selected label. The most convenient <literal>OCaml</literal> representation for labels is as functions that <emphasis>inject</emphasis> an endpoint (say, of type <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/equ245_1.jpg" mime-subtype="jpeg"/>) into a disjoint sum <literal>[l</literal><emphasis><subscript>i</subscript></emphasis> : <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/equ245_2.jpg" mime-subtype="jpeg"/><literal>]</literal><emphasis><subscript>i</subscript></emphasis><subscript>&#x03B5;</subscript><emphasis><subscript>I</subscript></emphasis> where <emphasis>k</emphasis> <literal>&#x03B5;</literal> <emphasis>I</emphasis>. This explains the type of <literal>select</literal>&#x2019;s first argument. Dually, <literal>receive</literal> waits for a label from an endpoint of type <literal>&#x0026;[l</literal><emphasis><subscript>i</subscript></emphasis> : <emphasis>A</emphasis><emphasis><subscript>i</subscript></emphasis><literal>]</literal><emphasis><subscript>i</subscript></emphasis><subscript>&#x03B5;</subscript><emphasis><subscript>I</subscript></emphasis> and returns the continuation endpoint injected into a disjoint union.</para>
<fig id="tab11_1" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Table 11.1</label>
<caption><title>Application programming interface for binary sessions</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/tab005.jpg" mime-subtype="jpeg"/>
</fig>
<para>We note a few more differences between the API we implement in this chapter and the one described by Gay and Vasconcelos [<link linkend="ch11_b4">4</link>]. First of all, we use parametric polymorphism to give session primitives their most general type. Second, we have a single function <literal>create</literal> to initiate a new session instead of a pair of <literal>accept</literal>/<literal>request</literal> functions to synchronize a service and a client. Our choice is purely a matter of simplicity, the alternative API being realizable on top of the one we present (the API implemented in the <literal>FuSe</literal> distribution already provides for the <literal>accept</literal>/<literal>request</literal> functions, which we will see at work in Section 11.4). Finally, our communication primitives are <emphasis>synchronous</emphasis> in that output operations block until the corresponding receive is performed. Again, this choice allows us to provide the simplest implementation of these primitives solely using functions from the standard <literal>OCaml</literal> library. Asynchronous communication can be implemented by choosing a suitable communication framework.</para>
</section>
<section class="lev1" id="sec11-2" label="11.2" xreflabel="11.2">
<title>A Few Simple Examples</title>
<para>Before looking at the implementation of the communication primitives, we illustrate the API at work on a series of simple examples. In doing so, we assume that the API is defined in a module named <literal>Session</literal>. The following code implements a client of an &#x2018;&#x2018;echo&#x2019;&#x2019; service, a service that waits for a message and bounces it back to the client.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog146.jpg" mime-subtype="jpeg"/></para>
<para>The parameter <literal>ep</literal> has type <literal>!</literal>&#x03B1;<literal>.?</literal>&#x03B2;<literal>.end</literal> and <literal>x</literal> has type &#x03B1;. The function <literal>echo_client</literal> starts by sending the message <literal>x</literal> over the endpoint <literal>ep</literal>. The construction <literal>let</literal> rebinds the name <literal>ep</literal> to the endpoint returned by the primitive <literal>send</literal>, which now has type <literal>?</literal>&#x03B2;<literal>.end</literal>. The endpoint is then used for receiving a message of type &#x03B2; from the service. Finally, <literal>echo_client</literal> closes the session and returns the received message.</para>
<para>The service is implemented by the <literal>echo_service</literal> function below, which uses the parameter <literal>ep</literal> of type <literal>?</literal>&#x03B1;<literal>.!</literal>&#x03B1;<literal>.end</literal> to receive a message <literal>x</literal> and then to sent it back to the client before closing the session.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog147.jpg" mime-subtype="jpeg"/></para>
<para>There is an interesting asymmetry between (the types of) client and service in that the message <literal>x</literal> sent by the service is the very same message it receives, whereas the message <literal>res</literal> received by the client does not necessarily have the same type as the message <literal>x</literal> it sends. Indeed, there is nothing in <literal>echo_client</literal> suggesting that <literal>x</literal> and <literal>res</literal> are somewhat related. This explains the reason why the session type of the endpoint used by the client (<literal>!</literal>&#x03B1;<literal>.?</literal>&#x03B2;<literal>.end</literal>) is more general than that used by the service (<literal>?</literal>&#x03B1;<literal>.!</literal>&#x03B1;<literal>.end</literal>) aside from the fact that the two session types describe protocols with complementary actions. In particular, <literal>!</literal>&#x03B1;<literal>.?</literal>&#x03B2;<literal>.end</literal> is <emphasis>not</emphasis> dual of <literal>?</literal>&#x03B1;<literal>.!</literal>&#x03B1;<literal>.end</literal> according to the definition of duality given earlier: in order to connect client and service, &#x03B2; must be <emphasis>unified</emphasis> with &#x03B1;. The code that connects <literal>echo_client</literal> and <literal>echo_service</literal> through a session is shown below:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog148.jpg" mime-subtype="jpeg"/></para>
<para>The code creates a new session, whose endpoints are bound to the names <literal>a</literal> and <literal>b</literal>. Then, it activates a new thread that applies <literal>echo_service</literal> to the endpoint <literal>a</literal>. Finally, it applies <literal>echo_client</literal> to the remaining endpoint <literal>b</literal>.</para>
<para>We now wish to generalize the echo service so that a client may decide whether to use the service or to stop the interaction without using it. A service that offers these two choices is illustrated below:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog149.jpg" mime-subtype="jpeg"/></para>
<para>In this case the service uses the <literal>branch</literal> primitive to wait for a label selected by the client. We use <literal>OCaml</literal>&#x2019;s polymorphic variant tags (&#x0060;<literal>Msg</literal> and &#x0060;<literal>End</literal> in this case) as labels because they do not have to be declared explicitly, unlike data constructors of plain algebraic data types. The initial type of <literal>ep</literal> is now <literal>&#x0026;[End</literal> : <literal>end</literal>, <literal>Msg</literal> : <literal>?</literal>&#x03B1;<literal>.!</literal>&#x03B1;<literal>.end]</literal> and the value returned by <literal>branch</literal> has type <literal>[End</literal> : <literal>end</literal>, <literal>Msg</literal> : <literal>?</literal>&#x03B1;<literal>.!</literal>&#x03B1;<literal>.end]</literal>. In the <literal>Msg</literal> branch the service behaves as before. In the <literal>End</literal> branch the service closes the session without performing any further communication.</para>
<para>The following function realizes a possible client for <literal>opt_echo_service</literal>:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog150.jpg" mime-subtype="jpeg"/></para>
<para>This function has type &#x2295;<literal>[End</literal> : <literal>end</literal>, <literal>Msg</literal> : <literal>!</literal>&#x03B1;<literal>.?</literal>&#x03B1;<literal>.end]</literal> <literal>&#x2192;</literal> <literal>bool</literal> <literal>&#x2192;</literal> &#x03B1; <literal>&#x2192;</literal> &#x03B1; and its behavior depends on the boolean parameter <literal>opt</literal>: when <literal>opt</literal> is <literal>true</literal>, the client selects the label <literal>Msg</literal> and then follows the same protocol as <literal>echo_client</literal>; when <literal>opt</literal> is <literal>false</literal>, the client selects the label <literal>End</literal> and then closes the session. Note that we have to &#x03B7;-expand the polymorphic variant tags &#x0060;<literal>Msg</literal> and &#x0060;<literal>End</literal> so that their type matches that expected by <literal>select</literal>. When the same label is used several times in the same program, it is convenient to define the &#x03B7;-expansion once, for example as</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog151.jpg" mime-subtype="jpeg"/></para>
<para>Note also that the messages sent and received now have the same type in the initial type of <literal>ep</literal>. This is because of the structure of <literal>opt_echo_client</literal>, which returns either <literal>x</literal> or the message returned by the service.</para>
<para>A further elaboration of the echo service allows the client to send an arbitrary number of messages before closing the session. In order to describe this protocol we must extend the syntax of session types presented earlier to permit recursive types. In practice, the representation of session types we will choose in Section 11.3 allows us to describe recursive protocols by piggybacking on <literal>OCaml</literal>&#x2019;s support for equi-recursive types, which is enabled by passing the <literal>-rectypes</literal> option to the compiler. The implementation of the elaborated echo service is therefore a straightforward recursive function:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog152.jpg" mime-subtype="jpeg"/></para>
<para>Note the recursive call <literal>rec_echo_service ep</literal> in the <literal>Msg</literal> branch, which allows the server to accept again a choice from the client after replying back to a request. The <literal>rec_echo_service</literal> function now expects an endpoint <literal>ep</literal> of type <literal>rec</literal> <emphasis>A</emphasis><literal>&#x0026;[End</literal> : <literal>end</literal>, <literal>Msg</literal> : <literal>?</literal>&#x03B1;<literal>.!</literal>&#x03B1;<literal>.</literal><emphasis>A</emphasis><literal>]</literal> where <literal>rec</literal> <emphasis>AT</emphasis> denotes the (equi-recursive) session type <emphasis>T</emphasis> in which occurrences of <emphasis>A</emphasis> stand for the session type itself.</para>
<para>The following client</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog153.jpg" mime-subtype="jpeg"/></para>
<para>has type <literal>rec</literal> <emphasis>A</emphasis>&#x2295;<literal>[End</literal> : <literal>end</literal>, <literal>Msg</literal> : <literal>!</literal>&#x03B1;<literal>.?</literal>&#x03B2;<literal>.</literal><emphasis>A</emphasis><literal>]</literal> <literal>&#x2192;</literal> <literal>list</literal> &#x03B1; <literal>&#x2192;</literal> &#x03B2; <literal>list</literal> and repeatedly invokes the recursive echo service on each element of a list.</para>
</section>
<section class="lev1" id="sec11-3" label="11.3" xreflabel="11.3">
<title>API Implementation</title>
<para>In order to implement the API presented and used in the previous sections we have to make some choices regarding the <literal>OCaml</literal> representation of session types and of session endpoints. In doing so we have to take into account the fact that <literal>OCaml</literal>&#x2019;s type system is not substructural and therefore is unable to statically check that session endpoints are used linearly. In the rest of this section we address these concerns and then detail the implementation of the API in <link linkend="tab11_1">Table <xref linkend="tab11_1" remap="11.1"/></link>.</para>
<para><emphasis role="strong">Representation of session types.</emphasis> <literal>FuSe</literal> relies on the encoding of session types proposed by Dardha <emphasis>et al.</emphasis> [<link linkend="ch11_b1">1</link>] and further refined by Padovani [<link linkend="ch11_b13">13</link>]. The basic idea is that a sequence of communications on a session endpoint can be compiled as a sequence of one-shot communications on linear channels (channels used exactly once) where each exchanged message carries the actual <emphasis>payload</emphasis> along with a <emphasis>continuation</emphasis>, namely a (fresh) channel on which the subsequent communication takes place.</para>
<para>The image of the encoding thus relies on two types:</para>
<itemizedlist mark="bullet" spacing="normal">
<listitem><para>a type <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e249_01.jpg" mime-subtype="jpeg"/> which is not inhabited, and</para></listitem>
<listitem><para>a type &#x3008;&#x03C1;, &#x03C3;&#x3009; which describes channels for receiving messages of type &#x03C1; and sending messages of type &#x03C3;. Both &#x03C1; and &#x03C3; can be instantiated with <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e249_02.jpg" mime-subtype="jpeg"/> to indicate that no message is respectively received and/or sent.</para></listitem>
</itemizedlist>
<para>The correspondence between session types <emphasis>T</emphasis> and types of the form &#x3008;<emphasis>t</emphasis>, <emphasis>s</emphasis>&#x3009; is given by the map <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e249_03.jpg" mime-subtype="jpeg"/> defined below</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog154.jpg" mime-subtype="jpeg"/></para>
<para>and extended homomorphically to all types. We assume that for each session type variable <emphasis>A</emphasis> there exist two distinct type variables &#x03C1;<emphasis><subscript>A</subscript></emphasis> and &#x03C3;<emphasis><subscript>A</subscript></emphasis> that are also different from any other type variable &#x03B1;.</para>
<para>For example, the session type <literal>?</literal>&#x03B1;<literal>.</literal><emphasis>A</emphasis> is encoded as <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e249_04.jpg" mime-subtype="jpeg"/>, which describes a channel for receiving a message of type &#x03B1;&#x00D7;&#x3008;&#x03C1;<emphasis><subscript>A</subscript></emphasis>, &#x03C3;<emphasis><subscript>A</subscript></emphasis>&#x3009; consisting of a component of type &#x03B1; (that is the actual payload of the communication) and a component of type &#x3008;&#x03C1;<emphasis><subscript>A</subscript></emphasis>, &#x03C3;<emphasis><subscript>A</subscript></emphasis>&#x3009; (that is the continuation channel on which the rest of the communication takes place). There is a twist in the encoding of outputs for the session type of the continuation is dualized. The reason for this is that the type associated with the continuation channel in the encoding describes the behavior of the <emphasis>receiver</emphasis> of the continuation rather than that of the <emphasis>sender</emphasis>. As we will see, this twist provides us with a simple way of expressing duality relations between session types, even when they are (partially) unknown. The encodings of <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e249_05.jpg" mime-subtype="jpeg"/> and <literal>&#x0026;[l</literal><emphasis><subscript>i</subscript></emphasis> : <emphasis>T</emphasis><emphasis><subscript>i</subscript></emphasis><literal>]</literal><emphasis><subscript>i</subscript></emphasis><subscript>&#x03B5;</subscript><emphasis><subscript>I</subscript></emphasis> follow the same lines and make use of polymorphic variant types to represent the selected or received choice. As an example, the encoding of <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e249_06.jpg" mime-subtype="jpeg"/> is computed as follows</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog155.jpg" mime-subtype="jpeg"/></para>
<para>If instead we consider the session type <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e250_01.jpg" mime-subtype="jpeg"/> = <literal>&#x0026;[End</literal> : <literal>end</literal>, <literal>Msg</literal> : <literal>?</literal>&#x03B1;<literal>.!</literal>&#x03B2;<literal>.end]</literal>, then we derive:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog156.jpg" mime-subtype="jpeg"/></para>
<para>Remarkably we observe that the encoding of <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e250_02.jpg" mime-subtype="jpeg"/> can be obtained from that of <emphasis>T</emphasis> by swapping the two components of the resulting channel types. This is a general property:</para>
<para><emphasis role="strong">Theorem 1</emphasis> <emphasis>If</emphasis> <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e250_08.jpg" mime-subtype="jpeg"/>, <emphasis>then</emphasis> <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e250_09.jpg" mime-subtype="jpeg"/>.</para>
<para>An equivalent way of expressing this result is the following: if <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e250_03.jpg" mime-subtype="jpeg"/> and <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e250_04.jpg" mime-subtype="jpeg"/>, then</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e250_05.jpg" mime-subtype="jpeg"/></para>
<para>meaning that the chosen encoding allows us to reduce session type duality to type equality. This property holds also for unknown or partially known session types. In particular, <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e250_06.jpg" mime-subtype="jpeg"/> and <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e250_07.jpg" mime-subtype="jpeg"/>.</para>
<para>We end the discussion of session type representation with two remarks. First, although the representation of session types chosen in <literal>FuSe</literal> is based on the continuation-passing encoding of sessions into the linear &#x03C0;-calculus [<link linkend="ch11_b1">1</link>], we will implement the communication primitives in <literal>FuSe</literal> so that only the payload (or the labels) are actually exchanged. Therefore, the semantics of <literal>FuSe</literal> communication primitives is consistent with that given in [<link linkend="ch11_b4">4</link>] and the components corresponding to continuations in the above types are solely used to relate the types of session endpoints as these are passed to, and returned from, <literal>FuSe</literal> communication primitives. Second, the <literal>OCaml</literal> type system is not substructural and there is no way to qualify types of the form &#x3008;<emphasis>t</emphasis>, <emphasis>s</emphasis>&#x3009; as linear, which is a fundamental requirement for the type safety of the API. We will overcome this limitation by means of a mechanism that detects linearity violations at runtime. Similar mechanisms have been proposed by Tov and Pucella [<link linkend="ch11_b20">20</link>] and by Hu and Yoshida [<link linkend="ch11_b5">5</link>].</para>
<fig id="tab11_2" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Table 11.2</label>
<caption><title><literal>OCaml</literal> interface of the API for binary sessions</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/tab006.jpg" mime-subtype="jpeg"/>
</fig>
<para>Having chosen the representation of session types, we can see in <link linkend="tab11_2">Table <xref linkend="tab11_2" remap="11.2"/></link> the <literal>OCaml</literal> interface of the module that implements the binary session API. In <literal>OCaml</literal> syntax, the type &#x3008;<emphasis>t</emphasis>, <emphasis>s</emphasis>&#x3009; is written <literal>(</literal><emphasis>t</emphasis><literal>,</literal><emphasis>s</emphasis><literal>) st</literal>. There is a direct correspondence between the signatures of the functions in <link linkend="tab11_2">Table <xref linkend="tab11_2" remap="11.2"/></link> and those shown in <link linkend="tab11_1">Table <xref linkend="tab11_1" remap="11.1"/></link> so we only make a couple of remarks. First, we extensively use Theorem 1 whenever we need to refer to a session type and its dual. This can be seen in the signatures of <literal>create</literal>, <literal>send</literal> and <literal>select</literal> where both <literal>(</literal>&#x03C1;<literal>,</literal>&#x03C3;<literal>) st</literal> and <literal>(</literal>&#x03C3;<literal>,</literal>&#x03C1;<literal>) st</literal> occur. Second, in the types of <literal>select</literal> and <literal>branch</literal> the syntax <literal>[&gt;] as</literal> &#x03B1; means that &#x03B1; can only be instantiated with a polymorphic variant type. Without this constraint the signatures of <literal>select</literal> and <literal>branch</literal> would be too general and the API unsafe: it would be possible to <literal>receive</literal> a label sent with <literal>select</literal>, or to <literal>branch</literal> over a message sent with <literal>send</literal>. Note that the constraint imposed by <literal>[&gt;] as</literal> &#x03B1; extends to every occurrence of &#x03B1; in the same signature.</para>
<para>By comparing <link linkend="tab11_1">Tables <xref linkend="tab11_1" remap="11.1"/></link> and <link linkend="tab11_2"><xref linkend="tab11_2" remap="11.2"/></link> it is clear that the encoding makes session types difficult to read. This problem becomes more severe as the protocols become more involved. The distribution of <literal>FuSe</literal> includes an auxiliary tool, called <literal>rosetta</literal>, that implements the inverse of the encoding to pretty print encoded session types into their familiar notation. The tool can be useful not only for documentation purposes but also to decipher the likely obscure type error messages issued by <literal>OCaml</literal>. Hereafter, when presenting session types inferred by <literal>OCaml</literal>, we will often show them as pretty printed by <literal>rosetta</literal> for better clarity.</para>
<para><emphasis role="strong">Representation of session endpoints.</emphasis> Session primitives can be easily implemented on top of any framework providing channel-based communications. <literal>FuSe</literal> is based on the <literal>Event</literal> module of <literal>OCaml</literal>&#x2019;s standard library, which provides communication primitives in the style of Concurrent <literal>ML</literal> [<link linkend="ch11_b16">16</link>] and the abstract type <emphasis>t</emphasis> <literal>Event.channel</literal> for representing channels carrying messages of type <emphasis>t</emphasis>. It is convenient to wrap the <literal>Event</literal> module so as to implement <emphasis>unsafe communication channels</emphasis>, thus:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog157.jpg" mime-subtype="jpeg"/></para>
<para>We just need three operations on unsafe channels, <literal>create</literal>, <literal>send</literal> and <literal>receive</literal>. The first one creates a new unsafe channel, which is simply an <literal>Event</literal> channel for exchanging messages of type <literal>unit</literal>. The choice of <literal>unit</literal> over any other <literal>OCaml</literal> type is immaterial: the messages exchanged over a session can be of different types, hence the type parameter we choose here is meaningless because we will perform unsafe cast at each communication. These casts cannot interfere with the internals of the <literal>Event</literal> module because <emphasis>t</emphasis> <literal>Event.channel</literal> is parametric on the type <emphasis>t</emphasis> of messages and therefore the operations in <literal>Event</literal> cannot make any assumption on their content. The implementation of <literal>send</literal> and <literal>receive</literal> on unsafe channels is a straightforward adaptation of the corresponding primitives of the <literal>Event</literal> module. Observe that, consistently with the communication API of Concurrent <literal>ML</literal>, <literal>Event.send</literal> and <literal>Event.receive</literal> do not perform communications themselves. Rather, they create <emphasis>communication events</emphasis> which occur only when they are synchronized through the primitive <literal>Event.sync</literal>. The <literal>Obj.magic</literal> function from the standard <literal>OCaml</literal> library has type &#x03B1; <literal>&#x2192;</literal> &#x03B2; and performs the necessary unsafe casts.</para>
<para>We now have all the ingredients for giving the concrete representation of (encoded) session types. This representation is kept private to the <literal>FuSe</literal> module so that the user can only manipulate session endpoint through the provided API:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog158.jpg" mime-subtype="jpeg"/></para>
<para>A session type is represented as a record with two fields: the <literal>chan</literal> field is a reference to the unsafe channel on which messages are exchanged; the mutable <literal>valid</literal> field is a boolean flag that indicates whether the endpoint can be safely used or not. Every operation that uses the endpoint first checks whether the endpoint is valid. If this is the case, the <literal>valid</literal> flag of the endpoint is reset to <literal>false</literal> so that any subsequent attempt to reuse the same endpoint can be detected. Otherwise, an <literal>InvalidEndpoint</literal> exception is raised. It is convenient to encapsulate this functionality in an auxiliary function <literal>use</literal>, which is private to the module and whose implementation is shown below:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog159.jpg" mime-subtype="jpeg"/></para>
<para>In principle, checking that the <literal>valid</literal> field is <literal>true</literal> and resetting it to <literal>false</literal> should be performed atomically, to account for the possibility that several threads are attempting to use the same endpoint simultaneously. In practice, since <literal>OCaml</literal>&#x2019;s scheduler is not preemptive and <literal>use</literal> allocates no memory, the execution of <literal>use</literal> is guaranteed to be performed atomically in <literal>OCaml</literal>&#x2019;s runtime environment. Different programming languages might require a more robust handling of the validity flag [<link linkend="ch11_b13">13</link>].</para>
<para>Whenever an operation on a session endpoint completes and the session endpoint is returned, its <literal>valid</literal> flag should be set to <literal>true</literal> again. Doing so on the existing record, though, would be unsafe. Instead, a new record referring to the very same unsafe channel must be created. Again it is convenient to provide this functionality as a private, auxiliary function <literal>fresh</literal>:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog160.jpg" mime-subtype="jpeg"/></para>
<para><emphasis role="strong">Implementation of communication primitives.</emphasis> A new session is initiatied by creating a new unsafe channel <literal>ch</literal> and returning the two peer endpoints of the session, which both refer to the same channel. The <literal>valid</literal> flag of each peer is set to <literal>true</literal>, indicating that it can be safely used:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog161.jpg" mime-subtype="jpeg"/></para>
<para>The implementation of <literal>close</literal> simply invalidates the endpoint. <literal>OCaml</literal>&#x2019;s garbage collector takes care of any further finalization that may be necessary to clean up the corresponding unsafe channel:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog162.jpg" mime-subtype="jpeg"/></para>
<para>The <literal>send</literal> operation starts by checking that the endpoint is valid and, in this case, invalidates it. Then, the message <literal>x</literal> is transmitted over the underlying unsafe channel and a refreshed version of the endpoint is returned. The <literal>receive</literal> operation is analogous, except that it returns a pair containing the message received from the underlying unsafe channel and the refreshed endpoint:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog163.jpg" mime-subtype="jpeg"/></para>
<para>The <literal>select</literal> operation is behaviorally equivalent to <literal>send</literal>, since its purpose is to transmit the selected label (which is its first argument) over the channel. On the other hand the <literal>branch</literal> operation injects the refreshed session endpoint with the function received from the channel:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog164.jpg" mime-subtype="jpeg"/></para>
<para>We conclude this section showing the type inferred by <literal>OCaml</literal> for the <literal>rec_echo_client</literal> defined in Section 11.1:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog165.jpg" mime-subtype="jpeg"/></para>
<para>As expected, the type is rather difficult to understand. Part of this difficulty is a consequence of the fact that the type expression <emphasis>t</emphasis> <literal>as</literal> &#x03B1;, which is used in <literal>OCaml</literal> also to denote a recursive type, is placed in a position such that <emphasis>t</emphasis> does not correspond to the encoding of a session type. It is only by unfolding this recursive type that one recovers an image of the encoding function. The same signature pretty printed by <literal>rosetta</literal> becomes</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog166.jpg" mime-subtype="jpeg"/></para>
<para>whose interpretation is straightforward.</para>
</section>
<section class="lev1" id="sec11-4" label="11.4" xreflabel="11.4">
<title>Extended Example: The Bookshop</title>
<para>In this section we develop a <literal>FuSe</literal> version of a known example from the literature [<link linkend="ch11_b4">4</link>], where mother and child order books from an online book-shop. The purpose of the programming exercise is threefold. First, we see a usage instance of the <literal>accept</literal> and <literal>request</literal> primitives provided by <literal>FuSe</literal> for establishing sessions over service channels. Second, we discuss a nontrivial example in which the session types automatically inferred by <literal>OCaml</literal> are at the same time more general and more precise than those given by Gay and Vasconcelos [<link linkend="ch11_b4">4</link>]. This is made possible thanks to the support for parametric polymorphism and subtyping in session types that <literal>FuSe</literal> inherits for free from <literal>OCaml</literal>&#x2019;s type system. Finally, we use the example to argue about the effectiveness of the <literal>FuSe</literal> implementation of binary sessions in detecting protocol violations, considering that <literal>FuSe</literal> combines both static and dynamic checks.</para>
<para>Service channels in <literal>FuSe</literal> are provided by the module <literal>Service</literal>, whose signature is shown below.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog167.jpg" mime-subtype="jpeg"/></para>
<para>The type <emphasis>A</emphasis> <literal>Service.t</literal> describe a service channel that allows initiation of sessions of type <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e255_01.jpg" mime-subtype="jpeg"/>. A session is created when two threads invoke <literal>accept</literal> and <literal>request</literal> over the same service channel. In this case, <literal>accept</literal> returns a session endpoint of type <emphasis>A</emphasis> and <literal>request</literal> returns its peer of type <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e255_02.jpg" mime-subtype="jpeg"/>.</para>
<para>The bookshop is modeled as a function that waits for session initiations on the service channel <literal>showAccess</literal> and invokes <literal>shopLoop</literal> at each connection:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog168.jpg" mime-subtype="jpeg"/></para>
<para>A session initiated with the bookshop is handled by the function <literal>shopLoop</literal>, which operates over the established session endpoint <literal>s</literal> and the current list <literal>order</literal> of books in the shopping cart. The <literal>shopLoop</literal> function is recursive and repeatedly offers the possibility of adding a new book by selecting the <literal>Add</literal> label. When <literal>Checkout</literal> is selected instead, the bookshop waits for a credit card number and an address and sends back an estimated delivery date computed by an unspecified <literal>deliveryOn</literal> function.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog169.jpg" mime-subtype="jpeg"/></para>
<para>The type inferred by <literal>OCaml</literal> for <literal>shopLoop</literal> is</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog170.jpg" mime-subtype="jpeg"/></para>
<para>which is structurally the same given by Gay and Vasconcelos [<link linkend="ch11_b4">4</link>], except for the type variables &#x03B1;, &#x03B2; and &#x03B3;. Indeed, the body of <literal>shopLoop</literal> does not use the received values <literal>book</literal>, <literal>card</literal> and <literal>address</literal> and therefore their type remains generic.</para>
<para>We now model a mother process placing an order for two books, one chosen by her and another selected by her son. In principle, the mother could let the son select his own book by delegating the session with the bookshop to him. However, the mother wants to be sure that her son will buy just one book that is suitable for his age. To enforce these constraints, the mother sends her son a voucher, that is a function providing a controlled interface with the bookshop. Overall, the mother is modeled thus:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog171.jpg" mime-subtype="jpeg"/></para>
<para>where the parameters <literal>card</literal>, <literal>addr</literal> and <literal>book</literal> stand for information about payment, delivery address and mother&#x2019;s book. In addition, <literal>shopAccess</literal> and <literal>sonAccess</literal> are the service channels for connecting with the bookshop and the son, respectively. The mother establishes a session <literal>c</literal> with the bookshop and adds <literal>book</literal> to the shopping cart. Afterwards, she initiates another session <literal>s</literal> for sending the voucher to her son. The voucher is modeled by the function:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog172.jpg" mime-subtype="jpeg"/></para>
<para>where <literal>book</literal> is chosen by the son. If <literal>book</literal> is appropriate &#x2013; something that is checked by the unspecified function <literal>isChildrensBook</literal> &#x2013; the book is added to the shopping cart. Then, the order is completed and the connection with the bookshop closed.</para>
<para>For <literal>voucher</literal> and <literal>mother OCaml</literal> infers the following types:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog173.jpg" mime-subtype="jpeg"/></para>
<para>In contrast to the type of <literal>mother</literal> given by Gay and Vasconcelos [<link linkend="ch11_b4">4</link>], the type inferred by <literal>OCaml</literal> makes it clear that <literal>mother</literal> always adds <emphasis>at least one book</emphasis> to the shopping cart. The connection between <literal>mother</literal> and <literal>shopLoop</literal> is still possible because the protocol followed by <literal>mother</literal> is more deterministic than &#x2013; or a supertype of [<link linkend="ch11_b3">3</link>] &#x2013; the one she is supposed to follow.</para>
<para>To finish the exercise we model the son as the following function:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog174.jpg" mime-subtype="jpeg"/></para>
<para>where <literal>sonAccess</literal> is the service channel used for accepting requests from his mother and <literal>book</literal> is the book he wishes to purchase. Note that the mother sends a function (obtained as the partial application of <literal>voucher</literal>) which is saturated by the son who provides the chosen <literal>book</literal>.</para>
<para>Overall, the code for connecting the three peers is shown below:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog175.jpg" mime-subtype="jpeg"/></para>
<para>It is not possible to qualify session endpoints as linear resources in <literal>OCaml</literal>. This means that there are well-typed programs that, by using session endpoints non-linearly, cause communication errors and/or protocol violations. In the rest of this section we use the example developed so far to do some considerations concerning the effectiveness of the library in detecting programming errors involving session endpoints. In particular we argue that, despite the lack of linear qualification of session endpoints, <literal>OCaml</literal>&#x2019;s type system is still capable of detecting a fair number of linearity violations. In the worst case, those violations that escape <literal>OCaml</literal>&#x2019;s type checker are at least detected at runtime with the mechanism we have put in place in Section 11.3.</para>
<para>We can simulate a linearity violation by replacing the session endpoint bound by a <literal>let</literal> with <literal>_</literal>. For example, we can replace line 10 in the body of <literal>voucher</literal> with</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog176.jpg" mime-subtype="jpeg"/></para>
<para>so that the very same endpoint <literal>c</literal> is used both for this <literal>send</literal> and also for the subsequent <literal>receive</literal>. This linearity violation is detected by <literal>OCaml</literal> because the type of a session endpoint used for an output is incompatible (<emphasis>i.e.</emphasis>, not unifiable) with that of an endpoint used for an input. Now suppose that we replace line 8 in the same function with</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog177.jpg" mime-subtype="jpeg"/></para>
<para>so that the same endpoint <literal>c</literal> is used for both a <literal>select</literal> and the subsequent <literal>send</literal>. Even if <literal>select</literal> and <literal>send</literal> are both output operations, the type of messages resulting from the encoding of a plain message output has a topmost <literal>&#x00D7;</literal> constructor which is incompatible with the polymorphic variant type resulting from the encoding of a label selection. Therefore, also this linearity violation is detected by <literal>OCaml</literal>&#x2019;s type checker. In general, any linearity violation arising from the use of different communication primitives is detected by <literal>OCaml</literal>. Consider then line 9, and suppose that we replace it with</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog178.jpg" mime-subtype="jpeg"/></para>
<para>so that the same endpoint <literal>c</literal> is used for sending both <literal>card</literal> and <literal>address</literal>. In this case the session endpoint is used for performing two plain outputs and the sent messages have compabile (<emphasis>i.e.</emphasis>, unifiable) types. Therefore, taken in isolation, the <literal>voucher</literal> function would be well typed. In the context of the whole program, however, <literal>OCaml</literal> detects a type error also in this case. The point is that the faulty version of <literal>voucher</literal> now implements a different protocol than before. In particular, it appears as if <literal>voucher</literal> sends just one message after selecting <literal>CheckOut</literal> and before receiving the estimated delivery date. On the contrary, the protocol of the bookshop as implemented by <literal>shopLoop</literal> still expects to receive two messages before the delivery date is sent back to the client. Therefore, the protocols of the bookshop and the one inferred by the combination of <literal>mother</literal> and <literal>voucher</literal> are no longer dual to each other and the session request performed by <literal>mother</literal> to the bookshop is ill typed. For this problem to go undetected, there must be <emphasis>another</emphasis> linearity violation in the body of <literal>shopLoop</literal>, in the place that corresponds exactly to the point where the same violation occurs in <literal>voucher</literal>.</para>
<para>A simpler example of linearity violation that goes undetected by <literal>OCaml</literal>&#x2019;s type checker can be obtained by duplicating the <literal>f book</literal> application in the body of the <literal>son</literal> function. This modification might correspond either to a genuine programming error or to a malicious attempt of <literal>son</literal> to purchase more than one book. The reason why this duplication results into a linearity violation is that the closure corresponding to <literal>f</literal> contains the session endpoint <literal>c</literal> from <literal>mother</literal>, so applying <literal>f</literal> twice results in two uses of the same <literal>c</literal>. This error is detected by the type system of Gay and Vasconcelos [<link linkend="ch11_b4">4</link>] where the function <literal>f</literal> has a linear arrow type. In <literal>FuSe</literal>, the program compiles correctly, but the second application of <literal>f</literal> triggers the runtime mechanism that detects linearity violations causing the <literal>InvalidEndpoint</literal> exception to be raised.</para>
</section>
<section class="lev1" id="sec11-5" label="11.5" xreflabel="11.5">
<title>Related Work</title>
<para>Several libraries of binary sessions have been proposed for different functional programming languages. Most libraries for <literal>Haskell</literal> [<link linkend="ch11_b6">6</link>, <link linkend="ch11_b12">12</link>, <link linkend="ch11_b15">15</link>, <link linkend="ch11_b17">17</link>] use a monad that encapsulates the endpoints of open sessions. Besides being a necessity dictated by the lazyness of the language, the monad prevents programmers from accessing session endpoints directly thus guaranteeing that endpoint linearity is not violated. The monad also tracks the evolution of the type of session endpoints automatically, not requiring the programmer to rebind explicitly the same endpoint over and over again. However, the monad has a cost in terms of either expressiveness, usability, or portability: the monad defined by Neubauer and Thiemann [<link linkend="ch11_b12">12</link>] supports communication on a single channel only and is therefore incapable of expressing session interleaving or delegation. Pucella and Tov [<link linkend="ch11_b15">15</link>] propose a monad that stores a stack of endpoints (or, better, of their capabilities) allowing for session interleaving and delegation to some extent. The price for this generality is that the programmer has to write explicit monadic actions to reach the channel/capability to be used within the monad; also for this reason delegation is severely limited. Imai <emphasis>et al.</emphasis> [<link linkend="ch11_b6">6</link>] show how to avoid writing such explicit actions relying on a form of type-level computations. Lindley and Morris [<link linkend="ch11_b9">9</link>] describe another Haskell embedding of session types that provides first-class channels. Linearity is enforced statically using higher-order abstract syntax.</para>
<para>A different approach is taken in <literal>Alms</literal> [<link linkend="ch11_b19">19</link>, <link linkend="ch11_b21">21</link>], a general-purpose programming language whose type system supports parametric polymorphism, abstract and algebraic data types, and built-in affine types as well. Tov [<link linkend="ch11_b19">19</link>] illustrates how to build a library of binary sessions on top of these features. Because <literal>Alms</literal>&#x2019; type system is substructural, affine usage of session endpoints is guaranteed statically by the fact that session types are qualified as affine. Further embeddings of session types in other experimental and domain-specific languages with substructural type systems have been described by Mazurak and Zdancewic [<link linkend="ch11_b10">10</link>], Lindley and Morris [<link linkend="ch11_b8">8</link>], and Morris [<link linkend="ch11_b11">11</link>].</para>
<para>Scalas and Yoshida [<link linkend="ch11_b18">18</link>] propose a library of binary session for <literal>Scala</literal> that is very related to our approach. As in <literal>FuSe</literal>, Scalas and Yoshida use a runtime mechanism to compensate for the lack of affine/linear types in <literal>Scala</literal> and work with the encoded representation of session types given by Dardha <emphasis>et al.</emphasis> [<link linkend="ch11_b1">1</link>]. A notable difference is that <literal>Scala</literal> type system is nominal, so that encoded session types are represented by <literal>Scala</literal> (case) classes which must be either provided by the programmer or generated from the protocol. This means that the protocol cannot be inferred automatically from the code and that the subtyping relation between session types is constrained by the (fixed) subclassing relation between the classes that represent them.</para>
<para>The main source of inspiration for the representation of session types in <literal>FuSe</literal> originates from the continuation-passing encoding of binary sessions [<link linkend="ch11_b1">1</link>] and partially studied also in some earlier works [<link linkend="ch11_b2">2</link>, <link linkend="ch11_b7">7</link>]. Our representation of encoded session types allows session type duality to be expressed solely in terms of type equality, whereas the representation chosen by Dardha <emphasis>et al.</emphasis> [<link linkend="ch11_b1">1</link>] requires a residual albeit simple notion of duality for the topmost channel type capability. Another difference is that we consider the encoding at the type level only, not requiring the explicit exchange of continuation channels for the implementation of communication primitives. For these reasons, the soundness of the encoding [<link linkend="ch11_b1">1</link>] cannot be used directly to argument about the soundness of <literal>FuSe</literal>&#x2019;s typing discipline. Padovani [<link linkend="ch11_b13">13</link>] formalizes <literal>FuSe</literal>&#x2019;s approach to binary sessions along with the necessary conditions under which the program does not raise exceptions. The same paper also illustrates a simple monadic API built on top of the primitives in <link linkend="tab11_1">Table <xref linkend="tab11_1" remap="11.1"/></link> and investigates the overhead of the various approaches to linearity.</para>
<para>In addition to the features described in this chapter, <literal>FuSe</literal> supports sequential composition of session types. This feature makes it possible to describe with greater precision protocols whose set of (finite) traces is <emphasis>context-free</emphasis> as opposed to regular [<link linkend="ch11_b14">14</link>,<link linkend="ch11_b22">22</link>]. As discussed by Thiemann and Vasconcelos [<link linkend="ch11_b22">22</link>], these protocols arise naturally in the serialization of structured data types. Currently, <literal>FuSe</literal> provides the first and only implementation of context-free session type checking and inference.</para>
<para><emphasis role="strong">Acknowledgments</emphasis> We thank the anonymous reviewers for their careful reading of the chapter and suggestions of improvements and related work.</para>
</section>
<section class="lev1" id="sec2a">
<title>References</title>
<orderedlist numeration="arabic" continuation="restarts" spacing="normal">
<listitem><para><anchor id="ch11_b1"/>Ornela Dardha, Elena Giachino, and Davide Sangiorgi. Session types revisited. In <emphasis>Proceedings of PPDP&#x2019;12</emphasis>, pages 139&#x2013;150. ACM, 2012. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Ornela+Dardha%2C+Elena+Giachino%2C+and+Davide+Sangiorgi%2E+Session+types+revisited%2E+In+Proceedings+of+PPDP%2712%2C+pages+139-150%2E+ACM%2C+2012%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch11_b2"/>Romain Demangeon and Kohei Honda. Full abstraction in a subtyped pi-calculus with linear types. In <emphasis>Proceedings of CONCUR&#x2019;11</emphasis>, LNCS 6901, pages 280&#x2013;296. Springer, 2011. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Romain+Demangeon+and+Kohei+Honda%2E+Full+abstraction+in+a+subtyped+pi-calculus+with+linear+types%2E+In+Proceedings+of+CONCUR%2711%2C+LNCS+6901%2C+pages+280-296%2E+Springer%2C+2011%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch11_b3"/>Simon Gay and Malcolm Hole. Subtyping for Session Types in the &#x03C0;-calculus. <emphasis>Acta Informatica</emphasis>, 42(2&#x2013;3):191&#x2013;225, 2005. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Simon+Gay+and+Malcolm+Hole%2E+Subtyping+for+Session+Types+in+the+pi-calculus%2E+Acta+Informatica%2C+42%282-3%29%3A191-225%2C+2005%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch11_b4"/>Simon J. Gay and Vasco Thudichum Vasconcelos. Linear type theory for asynchronous session types. <emphasis>Journal of Functional Programming</emphasis>, 20(1):19&#x2013;50, 2010. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Simon+J%2E+Gay+and+Vasco+Thudichum+Vasconcelos%2E+Linear+type+theory+for+asynchronous+session+types%2E+Journal+of+Functional+Programming%2C+20%281%29%3A19-50%2C+2010%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch11_b5"/>Raymond Hu and Nobuko Yoshida. Hybrid Session Verification through Endpoint API Generation. In <emphasis>Proceedings of FASE&#x2019;16</emphasis>, LNCS 9633, pages 401&#x2013;418. Springer, 2016. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Raymond+Hu+and+Nobuko+Yoshida%2E+Hybrid+Session+Verification+through+Endpoint+API+Generation%2E+In+Proceedings+of+FASE%2716%2C+LNCS+9633%2C+pages+401-418%2E+Springer%2C+2016%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch11_b6"/>Keigo Imai, Shoji Yuen, and Kiyoshi Agusa. Session Type Inference in Haskell. In <emphasis>Proceedings of PLACES&#x2019;10</emphasis>, EPTCS 69, pages 74&#x2013;91, 2010. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Keigo+Imai%2C+Shoji+Yuen%2C+and+Kiyoshi+Agusa%2E+Session+Type+Inference+in+Haskell%2E+In+Proceedings+of+PLACES%2710%2C+EPTCS+69%2C+pages+74-91%2C+2010%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch11_b7"/>Naoki Kobayashi. Type systems for concurrent programs. In <emphasis>10th Anniversary Colloquium of UNU</emphasis>/<emphasis>IIST</emphasis>, LNCS 2757, pages 439&#x2013;453. Springer, 2002. Extended version available at <code>http://www.kb.ecei. tohoku.ac.jp/koba/papers/tutorial-type-extended.pdf</code> <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Naoki+Kobayashi%2E+Type+systems+for+concurrent+programs%2E+In+10th+Anniversary+Colloquium+of+UNU%2FIIST%2C+LNCS+2757%2C+pages+439-453%2E+Springer%2C+2002%2E+Extended+version+available+at+http%3A%2F%2Fwww%2Ekb%2Eecei%2E+tohoku%2Eac%2Ejp%2F+koba%2Fpapers%2Ftutorial-type-extended%2Epdf" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch11_b8"/>Sam Lindley and J. Garrett Morris. Lightweight Functional Session Types. Unpublished manuscript available at <literal>http://homepages.inf. ed.ac.uk/slindley/papers/fst-draft-february2015.pdf</literal>, 2015.</para></listitem>
<listitem><para><anchor id="ch11_b9"/>Sam Lindley and J. Garrett Morris. Embedding session types in haskell. In <emphasis>Proceedings of Haskell&#x2019;16</emphasis>, Haskell 2016, pages 133&#x2013;145, New York, NY, USA, 2016. ACM. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Sam+Lindley+and+J%2E+Garrett+Morris%2E+Embedding+session+types+in+haskell%2E+In+Proceedings+of+Haskell%2716%2C+Haskell+2016%2C+pages+133-145%2C+New+York%2C+NY%2C+USA%2C+2016%2E+ACM%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch11_b10"/>Karl Mazurak and Steve Zdancewic. Lolliproc: to concurrency from classical linear logic via curry-howard and control. In <emphasis>Proceeding of ICFP&#x2019;10</emphasis>, pages 39&#x2013;50. ACM, 2010. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Karl+Mazurak+and+Steve+Zdancewic%2E+Lolliproc%3A+to+concurrency+from+classical+linear+logic+via+curry-howard+and+control%2E+In+Proceeding+of+ICFP%2710%2C+pages+39-50%2E+ACM%2C+2010%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch11_b11"/>J. Garrett Morris. The best of both worlds: linear functional programming without compromise. In <emphasis>Proceedings of ICFP&#x2019;16</emphasis>, pages 448&#x2013;461. ACM, 2016. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=J%2E+Garrett+Morris%2E+The+best+of+both+worlds%3A+linear+functional+programming+without+compromise%2E+In+Proceedings+of+ICFP%2716%2C+pages+448-461%2E+ACM%2C+2016%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch11_b12"/>Matthias Neubauer and Peter Thiemann. An implementation of session types. In <emphasis>Proceedings of PADL&#x2019;04</emphasis>, LNCS 3057, pages 56&#x2013;70. Springer, 2004. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Matthias+Neubauer+and+Peter+Thiemann%2E+An+implementation+of+session+types%2E+In+Proceedings+of+PADL%2704%2C+LNCS+3057%2C+pages+56-70%2E+Springer%2C+2004%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch11_b13"/>Luca Padovani. A Simple Library Implementation of Binary Sessions. <emphasis>Journal of Functional Programming</emphasis>, 27, 2017. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Luca+Padovani%2E+A+Simple+Library+Implementation+of+Binary+Sessions%2E+Journal+of+Functional+Programming%2C+27%2C+2017%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch11_b14"/>Luca Padovani. Context-Free Session Type Inference. In <emphasis>Proceedings of the 26th European Symposium on Programming (ESOP&#x2019;17)</emphasis>, LNCS. Springer, 2017.</para></listitem>
<listitem><para><anchor id="ch11_b15"/>Riccardo Pucella and Jesse A. Tov. Haskell session types with (almost) no class. In <emphasis>Proceedings of HASKELL&#x2019;08</emphasis>, pages 25&#x2013;36. ACM, 2008. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Riccardo+Pucella+and+Jesse+A%2E+Tov%2E+Haskell+session+types+with+%28almost%29+no+class%2E+In+Proceedings+of+HASKELL%2708%2C+pages+25-36%2E+ACM%2C+2008%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch11_b16"/>John H. Reppy. <emphasis>Concurrent Programming in ML</emphasis>. Cambridge University Press, 1999. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=John+H%2E+Reppy%2E+Concurrent+Programming+in+ML%2E+Cambridge+University+Press%2C+1999%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch11_b17"/>Matthew Sackman and Susan Eisenbach. Session Types in Haskell: Updating Message Passing for the 21st Century. Technical report, Imperial College London, 2008. Available at <literal>http://pubs.doc.ic. ac.uk/session-types-in-haskell/</literal> <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Matthew+Sackman+and+Susan+Eisenbach%2E+Session+Types+in+Haskell%3A+Updating+Message+Passing+for+the+21st+Century%2E+Technical+report%2C+Imperial+College+London%2C+2008%2E+Available+at+http%3A%2F%2Fpubs%2Edoc%2Eic%2E+ac%2Euk%2Fsession-types-in-haskell%2F" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch11_b18"/>Alceste Scalas and Nobuko Yoshida. Lightweight Session Programming in Scala. In <emphasis>Proceedings of ECOOP&#x2019;16</emphasis>, LIPIcs 56, pages 21:1&#x2013;21:28. Schloss Dagstuhl, 2016. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Alceste+Scalas+and+Nobuko+Yoshida%2E+Lightweight+Session+Programming+in+Scala%2E+In+Proceedings+of+ECOOP%2716%2C+LIPIcs+56%2C+pages+21%3A1-21%3A28%2E+Schloss+Dagstuhl%2C+2016%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch11_b19"/>Jesse A. Tov. <emphasis>Practical Programming with Substructural Types</emphasis>. PhD thesis, Northeastern University, 2012. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Jesse+A%2E+Tov%2E+Practical+Programming+with+Substructural+Types%2E+PhD+thesis%2C+Northeastern+University%2C+2012%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch11_b20"/>Jesse A. Tov and Riccardo Pucella. Stateful Contracts for Affine Types. In <emphasis>Proceedings of ESOP&#x2019;10</emphasis>, LNCS 6012, pages 550&#x2013;569. Springer, 2010. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Jesse+A%2E+Tov+and+Riccardo+Pucella%2E+Stateful+Contracts+for+Affine+Types%2E+In+Proceedings+of+ESOP%2710%2C+LNCS+6012%2C+pages+550-569%2E+Springer%2C+2010%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch11_b21"/>Jesse A. Tov and Riccardo Pucella. Practical affine types. In <emphasis>Proceedings of POPL&#x2019;11</emphasis>, pages 447&#x2013;458. ACM, 2011. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Jesse+A%2E+Tov+and+Riccardo+Pucella%2E+Practical+affine+types%2E+In+Proceedings+of+POPL%2711%2C+pages+447-458%2E+ACM%2C+2011%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch11_b22"/>Vasco T. Vasconcelos and Peter Thiemann. Context-free session types. In <emphasis>Proceedings of ICFP&#x2019;16</emphasis>, pages 462&#x2013;475. ACM, 2016. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Vasco+T%2E+Vasconcelos+and+Peter+Thiemann%2E+Context-free+session+types%2E+In+Proceedings+of+ICFP%2716%2C+pages+462-475%2E+ACM%2C+2016%2E" target="_blank">Google Scholar</ulink></para></listitem>
</orderedlist>
</section>
</chapter>
<chapter class="chapter" id="ch12" label="12" xreflabel="12">
<title>Lightweight Functional Session Types</title>
<para><emphasis role="strong">Sam Lindley and J. Garrett Morris</emphasis></para>
<para>University of Edinburgh, Edinburgh, UK</para>
<section class="lev2">
<title>Abstract</title>
<para>Row types provide an account of extensibility that combines well with parametric polymorphism and type inference. We discuss the integration of row types and session types in a concurrent functional programming language, and how row types can be used to describe extensibility in session-typed communication.</para>
</section>
<section class="lev1" id="sec12-1" label="12.1" xreflabel="12.1">
<title>Introduction</title>
<para>In prior work, we have developed a core linear &#x03BB;-calculus with session types called GV [<link linkend="ch12_b13">13</link>]. GV is inspired by a functional language with session types developed by Gay and Vasconcelos [<link linkend="ch12_b7">7</link>], which we term LAST (for Linear Asynchronous Session Types), and by the propositions-as-types correspondence between session types and linear logic first introduced by Caires and Pfenning [<link linkend="ch12_b4">4</link>] and later adapted to the classical setting by Wadler [<link linkend="ch12_b23">23</link>]. We have given direct proofs of deadlock freedom, determinism, and termination for GV. We have also given semantics-preserving translations between GV and Wadler&#x2019;s process calculus CP, showing a strong connection between GV&#x2019;s small-step operational semantics and cut elimination in classical linear logic.</para>
<para>In this article, we demonstrate that we can build practical languages based on the primitives and properties of GV. We introduce a language, FST, that extends GV with polymorphism, row types, and subkinding, integrating linear and unlimited data types. FST, while more expressive, is still deadlock-free, deterministic, and terminating. We consider several extensions of FST. Recursion and recursive session types support the definition of long-running services and repeated behavior. Adding recursion and recursive session types results in a system that is no longer terminating, but is still deadlock free and deterministic. Access points support a more flexible mechanism for session initiation. Adding access points results in a system that is not deadlock-free, deterministic, or terminating, but that still satisfies subject reduction and a weak form of progress.</para>
<para><emphasis role="strong">Outline.</emphasis> The article proceeds as follows. Section 12.2 presents some examples illustrating FST and its extensions. Section 12.3 gives a formal account of FST, a linear variant of System F, incorporating polymorphism, row-typing, subkinding, and session types.</para>
<para>Section 12.4 explores extensions of FST with recursion, recursive types, and access points, and demonstrates the expressivity of access points with encodings of state cells, nondeterministic choice, and recursion.</para>
<para>Section 12.5 describes a practical implementation of FST in Links, a functional language for web programming, and discusses our adaptation of the existing Links syntax and type inference mechanisms to support linearity and session types.</para>
<para>Section 12.6 concludes.</para>
<para>In this version of the article, we focus on the FST type system, and omit the formal semantics and statements of correctness. An extended version including the formal semantics and correctness proofs is available online [<link linkend="ch12_b15">15</link>].</para>
</section>
<section class="lev1" id="sec12-2" label="12.2" xreflabel="12.2">
<title>A First Look</title>
<para>Before giving a formal account of the syntax and type system of FST, we present some simple examples of programming in FST. We use a desktop calculator as a running example. Despite its simplicity, it will motivate the features of FST.</para>
<para><emphasis role="strong">A One-Shot Calculator Server.</emphasis> We begin with a process that implements a calculator server. We specify it as a function of one channel, <emphasis>c</emphasis>, on which it will communicate with a user of the calculator.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e266_01.jpg" mime-subtype="jpeg"/></para>
<para>On receiving a channel <emphasis>c</emphasis>, the function <emphasis role="romanAlt1">calc</emphasis> offers a choice of two behaviors, labeled <emphasis role="romanAlt1">Add</emphasis> and <emphasis role="romanAlt1">Neg</emphasis> on <emphasis>c</emphasis>. In the <emphasis role="romanAlt1">Add</emphasis> case, it then expects to read two values from <emphasis>c</emphasis> and send their sum along <emphasis>c</emphasis>. The <emphasis role="romanAlt1">Neg</emphasis> case is similar. The session type of channel <emphasis>c</emphasis> encodes these interactions, so the type of <emphasis role="romanAlt1">calc</emphasis> is</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e267_01.jpg" mime-subtype="jpeg"/></para>
<para>where the session type !<emphasis>T</emphasis><emphasis>.</emphasis><emphasis>S</emphasis> denotes sending a value of type <emphasis>T</emphasis> followed by behavior <emphasis>S</emphasis>, ?<emphasis>T</emphasis><emphasis>.</emphasis><emphasis>S</emphasis> denotes reading a value of type <emphasis>T</emphasis> followed by behavior <emphasis>S</emphasis>, and &#x0026;<emphasis>{</emphasis> <emphasis></emphasis> : <emphasis>S</emphasis><emphasis>,...,</emphasis> <emphasis><subscript>n</subscript></emphasis>: <emphasis>S</emphasis><emphasis><subscript>n</subscript></emphasis><emphasis>}</emphasis> denotes offering an <emphasis>n</emphasis>-ary choice, with the behavior of the <emphasis>i</emphasis><emphasis><sup>th</sup></emphasis> branch given by <emphasis>S</emphasis><emphasis><subscript>i</subscript></emphasis>.</para>
<para>Next, we consider a client for the calculator server:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e267_02.jpg" mime-subtype="jpeg"/></para>
<para>Like <emphasis role="romanAlt1">calc</emphasis>, the <emphasis role="romanAlt1">user</emphasis><subscript>1</subscript> function is passed the channel on which it communicates with the calculator. It begins by selecting the <emphasis role="romanAlt1">Add</emphasis> behavior, which is compatible with the choice offered by <emphasis role="romanAlt1">calc</emphasis>. Its subsequent behavior is unsurprising. We could give the channel a type dual to that provided by the calculator:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e267_03.jpg" mime-subtype="jpeg"/></para>
<para>However, this type overspecifies the behavior of <emphasis role="romanAlt1">user</emphasis><subscript>1</subscript> as the <emphasis role="romanAlt1">Neg</emphasis> branch is unused in the definition of <emphasis role="romanAlt1">user</emphasis><subscript>1</subscript>. In FST, we can use row polymorphism to abstract over the irrelevant labels in a choice, as follows:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e267_04.jpg" mime-subtype="jpeg"/></para>
<para>This type specifies that the argument to <emphasis role="romanAlt1">user</emphasis><subscript>1</subscript> may be instantiated to any session type that offers a choice of <emphasis role="romanAlt1">Add</emphasis> with a suitable behavior along with arbitrary other choices. FST includes explicit type abstractions and type annotations on bound variables; we omit both in the examples in order to improve readability. Our concrete implementation of FST in Links, is able to reconstruct omitted types and type abstractions using a fairly standard Hindley-Milner-style type inference algorithm.</para>
<para>We can plug the calculator server and the user together as follows</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e267_05.jpg" mime-subtype="jpeg"/></para>
<para>yielding the number 42. The <emphasis role="strong">fork</emphasis> primitive creates a new child process and a channel through which it can communicate with its parent process.</para>
<para><emphasis role="strong">Recursive Session Types.</emphasis> The one-shot calculator server allows only one operation to be performed before the communication is exhausted. If we add support for recursive session types, then we can define a calculator that allows an arbitrary number of operations to be performed. In order to make the example more interesting, we define a calculator server with a memory.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e268_01.jpg" mime-subtype="jpeg"/></para>
<para>The idea is that selecting <emphasis role="romanAlt1">M</emphasis><sup>+</sup> adds a number to that currently stored in memory and <emphasis role="romanAlt1">MR</emphasis> reads the current value of the memory. A user must now explicitly select <emphasis role="romanAlt1">Stop</emphasis> in order to terminate communication with the server.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e268_02.jpg" mime-subtype="jpeg"/></para>
<para>With the row variables instantiated appropriately, we can plug <emphasis role="romanAlt1">user</emphasis><subscript>2</subscript> and the recursive calculator together</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e268_03.jpg" mime-subtype="jpeg"/></para>
<para>again yielding 42.</para>
<para>The examples we have seen so far could be implemented using subtyping instead of row polymorphism. We now consider a function that cannot be implemented with subtyping. Suppose we wish to abstract over the memory add operation. We define a function that can be used to communicate with any calculator server that supports <emphasis role="romanAlt1">M</emphasis><sup>+</sup> and arbitrary other operations.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e268_04.jpg" mime-subtype="jpeg"/></para>
<para>The key feature of this function is that the row variable &#x03C1; appears both contravariantly (inside the second argument) and covariantly (inside the return type) in the type of <emphasis role="romanAlt1">mAdd</emphasis>. Thus, in a system with subtyping but without row typing, one would have to explicitly instantiate &#x03C1; , ruling out an extensible calculator server implementation. Let us use <emphasis role="romanAlt1">mAdd</emphasis> to define a client that invokes multiple calculator operations.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e269_01.jpg" mime-subtype="jpeg"/></para>
<para>We can plug <emphasis role="romanAlt1">user</emphasis><subscript>3</subscript> and the recursive calculator together as before</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e269_02.jpg" mime-subtype="jpeg"/></para>
<para>again yielding 42.</para>
<para><emphasis role="strong">Access Points.</emphasis> A key limitation of the examples we have seen so far is that they allow only one user to connect to a calculator server at a time. Access points provide a more flexible mechanism for session initiation than the <emphasis role="strong">fork</emphasis> primitive. Intuitively, we can think of access points as providing a matchmaking service for processes. Processes may either accept or request connections at a given access point; accepting and requesting processes are paired nondeterministically. We now adapt our calculator server to synchronize on an access point instead of a fixed channel:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e269_03.jpg" mime-subtype="jpeg"/></para>
<para>Unlike <emphasis role="romanAlt1">calc</emphasis><emphasis role="strong"><subscript>rec</subscript></emphasis>, this calculator server never stops; rather, it will persist until the access point is no longer accessible by any client code, at which point it may be garbage collected. As <emphasis role="romanAlt1">calc</emphasis><emphasis role="strong"><subscript>rec</subscript></emphasis> never returns, it is polymorphic in its return type. In general, an access point <emphasis>a</emphasis> has type <emphasis role="romanAlt1">AP</emphasis> <emphasis>S</emphasis> for some session type <emphasis>S</emphasis>. The expression <emphasis role="strong">accept</emphasis> <emphasis>a</emphasis> returns an end point of type <emphasis>S</emphasis> and <emphasis role="strong">request</emphasis> <emphasis>a</emphasis> returns an end point of type <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e270_01.jpg" mime-subtype="jpeg"/>.</para>
<para>We can connect our original user to <emphasis role="romanAlt1">calc</emphasis><emphasis role="romanAlt"><subscript>AP</subscript></emphasis>. We use the <emphasis role="strong">new</emphasis> operator to create a fresh access point and the <emphasis role="strong">spawn</emphasis> operator to create child threads (without any shared channels).</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e270_02.jpg" mime-subtype="jpeg"/></para>
<para>The result of evaluation is again 42. More interestingly, we can connect multiple clients to the same server concurrently.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e270_03.jpg" mime-subtype="jpeg"/></para>
<para>The result of evaluating this code is non-deterministic. Depending on the scheduler it may yield 0, 19, 23, or 42.</para>
</section>
<section class="lev1" id="sec12-3" label="12.3" xreflabel="12.3">
<title>The Core Language</title>
<para>The calculus we present in this section, FST (F with Session Types), is a call-by-value linear variant of System F with subkinding, row types, and session types. It combines a variant of GV, our session-typed linear &#x03BB;-calculus [<link linkend="ch12_b13">13</link>], with the row typing and subkinding of our previous core language for Links [<link linkend="ch12_b11">11</link>], and the similar approach to subkinding for linearity of Mazurak et al&#x2019;s lightweight linear types [<link linkend="ch12_b17">17</link>].</para>
<para>As our focus is programming with session types rather than their logical connections, we make some simplifications compared to our earlier work [<link linkend="ch12_b13">13</link>]. Specifically, we have a single unlimited self-dual type of closed channels, and we omit the operation for linking channels together.</para>
<section class="lev2" id="sec12-3-1" label="12.3.1" xreflabel="12.3.1">
<title>Syntax</title>
<para>To avoid duplication and keep the concurrent semantics of FST simple, we strive to implement as much as possible in the functional core of FST, and limit the session typing constructs to the essentials. The only session type constructors are for output, input, and closed channels, and no special typing rules are needed for the primitives, which are specified as constants. Other features such as choice and selection can be straightforwardly encoded using features of the functional core.</para>
<para><emphasis role="strong">Types.</emphasis> The syntax of types and kinds is given in <link linkend="fig12_1">Figure <xref linkend="fig12_1" remap="12.1"/></link>. The function type <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e271_01.jpg" mime-subtype="jpeg"/> takes an argument of type <emphasis>A</emphasis> and returns a value of type <emphasis>B</emphasis> and has linearity <emphasis>Y</emphasis> . (We write <emphasis>A</emphasis> <emphasis>&#x2192;</emphasis> <emphasis>B</emphasis> as an abbreviation for <emphasis>A</emphasis> <emphasis>&#x2192;</emphasis><emphasis><sup>&#x2022;</sup></emphasis> <emphasis>B</emphasis>.) The record type <emphasis></emphasis> <emphasis>R</emphasis> <emphasis></emphasis> has fields given by the labels of row <emphasis>R</emphasis>. The variant type <emphasis role="roman">[</emphasis><emphasis>R</emphasis><emphasis role="roman">]</emphasis> admits tagged values given by the labels of row <emphasis>R</emphasis>. The polymorphic type <emphasis>&#x2200;</emphasis>&#x03B1;<sup><emphasis>K</emphasis>(<emphasis>Y</emphasis><emphasis>,</emphasis><emphasis>Z</emphasis>)</sup><emphasis>.</emphasis><emphasis>A</emphasis> is parameterized over the type variable &#x03B1; of kind <emphasis>K</emphasis><emphasis role="roman">(</emphasis><emphasis>Y</emphasis><emphasis>,</emphasis><emphasis>Z</emphasis><emphasis role="roman">)</emphasis>.</para>
<para>The input type ?<emphasis>A</emphasis><emphasis>.</emphasis><emphasis>S</emphasis> receives an input of type <emphasis>A</emphasis> and proceeds as the session type <emphasis>S</emphasis>. Dually, the output type !<emphasis>A</emphasis><emphasis>.</emphasis><emphasis>S</emphasis> sends an output of type <emphasis>A</emphasis> and proceeds as the session type <emphasis>S</emphasis>. The type <emphasis role="romanAlt1">End</emphasis> terminates a session; it is its own dual. We let &#x03C3; range over session type variables and the dual of session type variable &#x03C3; is <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e271_02.jpg" mime-subtype="jpeg"/>.</para>
<para><emphasis role="strong">Row Types.</emphasis> Records and variants are defined in terms of row types. Intuitively, a row type represents a mapping from labels to ordinary types. In fact, rows also track absent labels, which are, for instance, needed to type polymorphic record extension (a record can only be extended with labels that are not already present). A row type includes a list of distinct labels, each of which is annotated with a presence type. The presence type indicates whether the label is present with type <emphasis>A</emphasis> (<emphasis role="romanAlt1">Pre</emphasis><emphasis role="roman">(</emphasis><emphasis>A</emphasis><emphasis role="roman">)</emphasis>), absent (<emphasis role="romanAlt1">Abs</emphasis>), or polymorphic in its presence (&#x03B8; ).</para>
<para>Row types are either <emphasis>closed</emphasis> or <emphasis>open</emphasis>. A closed row type ends in <emphasis>&#x00B7;</emphasis>. An open row type ends in a <emphasis>row variable</emphasis> &#x03C1; or its dual <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/equ276_1.jpg" mime-subtype="jpeg"/>; the latter are only meaningful for session-kinded rows. The mapping from labels to ordinary types represented by a closed row type is defined only on the labels that are explicitly listed in the row type, and cannot be extended. In contrast, the row variable in an open row type can be instantiated in order to extend the row type with additional labels. As usual, we identify rows up to reordering of labels.</para>
<fig id="fig12_1" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 12.1</label>
<caption><title>Syntax of types and kinds.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig063.jpg" mime-subtype="jpeg"/>
</fig>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e272_01.jpg" mime-subtype="jpeg"/></para>
<para>Furthermore, absent labels in closed rows are redundant:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e272_02.jpg" mime-subtype="jpeg"/></para>
<para><emphasis role="strong">Duality.</emphasis> The syntactic duality operation on type variables extends to a semantic duality operation on session types and is lifted homomorphically to session row types, and session presence types:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e272_03.jpg" mime-subtype="jpeg"/></para>
<para><emphasis role="strong">Kinds.</emphasis> Types are classified by kinds. Ordinary types have kind <emphasis role="romanAlt1">Type</emphasis>. Row types <emphasis>R</emphasis> have kind <emphasis role="romanAlt1">Row</emphasis><subscript>&#x2112;</subscript> where &#x2112; is a set of labels not allowed in <emphasis>R</emphasis>. Presence types have kind <emphasis role="romanAlt1">Presence</emphasis>.</para>
<para>The three primary kinds are refined with a simple subkinding discipline, similar to the system described in our previous work on Links [<link linkend="ch12_b11">11</link>] and the system of Mazurak et al. on lightweight linear types [<link linkend="ch12_b17">17</link>]. A primary kind <emphasis>K</emphasis> is parameterized by a <emphasis>linearity Y</emphasis> and a <emphasis>restriction Z</emphasis>. The linearity can be either unlimited (<emphasis>&#x2022;</emphasis>) or linear (<inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e272_04.jpg" mime-subtype="jpeg"/>). The restriction can be session typed (&#x03C0; ) or unconstrained ( <emphasis></emphasis>). The interpretation of these parameters on row and presence kinds is pointwise on the ordinary types contained within the row or presence types inhabiting those kinds. For instance, the kind <emphasis role="romanAlt1">Row</emphasis><subscript>&#x2112;</subscript> <emphasis role="roman">(</emphasis><emphasis>&#x00BA;</emphasis><emphasis>,</emphasis>&#x03C0; <emphasis role="roman">)</emphasis> is inhabited by row types of linear session type and the kind <emphasis role="romanAlt1">Presence</emphasis><emphasis role="roman">(</emphasis><emphasis>&#x2022;</emphasis><emphasis>,</emphasis> *<emphasis role="roman">)</emphasis> by presence types of unlimited unconstrained ordinary types.</para>
<para>By convention we use &#x03B1; for ordinary type variables or for type variables of unspecified kind, &#x03C1; for type variables of row kind, and &#x03B8; for type variables of presence kind. We sometimes omit the primary kind, either inferring it from context or assuming a default of <emphasis role="romanAlt1">Type</emphasis>. For instance, we write &#x03B1;<sup>&#x2022;,*</sup> instead of &#x03B1;<sup><emphasis role="romanAlt1">Type</emphasis>(&#x2022;,*)</sup>.</para>
<para><emphasis role="strong">Subkinding.</emphasis> The two sources of subkinding are the linearity and restriction parameters.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e273_01.jpg" mime-subtype="jpeg"/></para>
<para>Our notion of linearity corresponds to usage, not alias freedom. Thus, any unlimited type can be used linearly, but not vice versa.</para>
<para><emphasis role="strong">Kind and Type Environments.</emphasis></para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e273_02.jpg" mime-subtype="jpeg"/></para>
<para>Kind environments map type variables to kinds. Type environments map term variables to types.</para>
<para><emphasis role="strong">Terms.</emphasis> The syntax of terms and values is given in <link linkend="fig12_2">Figure <xref linkend="fig12_2" remap="12.2"/></link>. We let <emphasis>x</emphasis> range over term variables and <emphasis>c</emphasis> range over constants. Lambda abstractions &#x03BB;<emphasis><sup>Y</sup></emphasis><emphasis>x</emphasis><emphasis><sup>A</sup></emphasis><emphasis>.</emphasis><emphasis>M</emphasis> are annotated with linearity <emphasis>Y</emphasis> . Type abstractions &#x03BB;&#x03B1;<emphasis><sup>J</sup></emphasis><emphasis>.</emphasis><emphasis>V</emphasis> are annotated with kind <emphasis>J</emphasis>. Note that the body of a type abstraction is restricted to be a syntactic value in the spirit of the ML value restriction (in order to avoid problems with polymorphic linearity and with polymorphic session types). Records are introduced with the unit record <emphasis></emphasis> and record extension <emphasis></emphasis> <emphasis></emphasis> <emphasis role="roman">=</emphasis> <emphasis>M</emphasis>;<emphasis>N</emphasis> <emphasis></emphasis> constructs. They are eliminated with the binding forms <emphasis role="strong">let</emphasis> <emphasis>&#x2190;</emphasis> <emphasis>M</emphasis> <emphasis role="strong">in</emphasis> <emphasis>N</emphasis> and <emphasis role="strong">let</emphasis> <emphasis></emphasis> <emphasis></emphasis> <emphasis role="roman">=</emphasis> <emphasis>x</emphasis>;<emphasis>y</emphasis> <emphasis> &#x2190;</emphasis> <emphasis>M</emphasis> <emphasis role="strong">in</emphasis> <emphasis>N</emphasis>, the latter of which binds the value labeled by <emphasis></emphasis> to <emphasis>x</emphasis> and the remainder of the record to <emphasis>y</emphasis>. Conventional projections <emphasis>M</emphasis><emphasis>.</emphasis></para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e273_03.jpg" mime-subtype="jpeg"/></para>
<fig id="fig12_2" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 12.2</label>
<caption><title>Syntax of terms and values.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig064.jpg" mime-subtype="jpeg"/>
</fig>
<para>are definable using this form, but note that because projection discards the remainder of the record, its applicability to records with linear components is limited. Variants are introduced with the injection <emphasis></emphasis> <emphasis>M</emphasis> and eliminated with <emphasis role="strong">case</emphasis> <emphasis>L</emphasis> <emphasis>{</emphasis> <emphasis>&#8467;</emphasis> <emphasis>x</emphasis> <emphasis>&#x2192;</emphasis> <emphasis>M</emphasis>;<emphasis>y</emphasis> <emphasis>&#x2192;</emphasis> <emphasis>N</emphasis><emphasis>}</emphasis>. Hypothetical empty variants are eliminated with <emphasis role="strong">case</emphasis><emphasis><subscript>&#x22A5;</subscript></emphasis> <emphasis>L</emphasis>.</para>
<para><emphasis role="strong">Concurrency.</emphasis> The concurrency features of FST are provided by special constants. The term <emphasis role="strong">send</emphasis> <emphasis></emphasis> <emphasis>V</emphasis><emphasis>,</emphasis><emphasis>W</emphasis> <emphasis></emphasis> sends <emphasis>V</emphasis> along channel <emphasis>W</emphasis> , returning the updated channel. The term <emphasis role="strong">receive</emphasis> <emphasis>W</emphasis> receives a value along channel <emphasis>W</emphasis> , and returns a pair of the value and the updated channel. The term <emphasis role="strong">fork</emphasis> <emphasis role="roman">(</emphasis>&#x03BB; <emphasis>x</emphasis><emphasis>.</emphasis><emphasis>M</emphasis><emphasis role="roman">)</emphasis> returns one end of a channel and forks a new process <emphasis>M</emphasis> in which <emphasis>x</emphasis> is bound to the other end of the channel.</para>
<para><emphasis role="strong">Notation.</emphasis> We use the following abbreviations:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e274_01.jpg" mime-subtype="jpeg"/></para>
<para>We interpret <emphasis>n</emphasis>-ary record and case extension at the type and term levels in the standard way. For instance</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e274_02.jpg" mime-subtype="jpeg"/></para>
<para>and</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e274_03.jpg" mime-subtype="jpeg"/></para>
<para>where we let &#x03C7; range over sequences of cases:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e274_04.jpg" mime-subtype="jpeg"/></para>
<para>We write fv<emphasis role="roman">(</emphasis><emphasis>M</emphasis><emphasis role="roman">)</emphasis> for the free variables of <emphasis>M</emphasis>. We write ftv<emphasis role="roman">(</emphasis><emphasis>T</emphasis> <emphasis role="roman">)</emphasis> for the free type variables of a type <emphasis>T</emphasis> and ftv<emphasis role="roman">(</emphasis>&#x0393;<emphasis role="roman">)</emphasis> for the free type variables of type environment &#x0393;. We write dom<emphasis role="roman">(</emphasis>&#x0393;<emphasis role="roman">)</emphasis> for the domain of type environment &#x0393;.</para>
</section>
<section class="lev2" id="sec12-3-2" label="12.3.2" xreflabel="12.3.2">
<title>Typing and Kinding Judgments</title>
<para>The kinding rules are given in <link linkend="fig12_3">Figure <xref linkend="fig12_3" remap="12.3"/></link>. The kinding judgment &#x0394; <emphasis></emphasis> <emphasis>A</emphasis> : <emphasis>K</emphasis><emphasis role="roman">(</emphasis><emphasis>Y</emphasis><emphasis>,</emphasis><emphasis>Z</emphasis><emphasis role="roman">)</emphasis> states that in kind environment &#x0394;, the type <emphasis>A</emphasis> has kind <emphasis>K</emphasis><emphasis role="roman">(</emphasis><emphasis>Y</emphasis><emphasis>,</emphasis><emphasis>Z</emphasis><emphasis role="roman">)</emphasis>. Type variables in the kind environment are well-kinded. The rules for forming function, record, variant, universally quantified, and presence types follow the syntactic structure of types. Because of the subkinding relation, a record is linear if any of its fields are linear, and similarly for variants. Recall that <emphasis role="romanAlt1">Row</emphasis><subscript>&#x2112;</subscript> is the kind of row types whose labels cannot appear in &#x2112; . (To be clear, this constraint applies equally to absent and present labels; it is a constraint on the form of <emphasis>row types</emphasis>. In contrast, <emphasis></emphasis> : <emphasis role="romanAlt1">Abs</emphasis> in a row type is a constraint on <emphasis>terms</emphasis>.) An empty row has kind <emphasis role="romanAlt1">Row</emphasis><subscript>&#x2112;</subscript> <emphasis role="roman">(</emphasis><emphasis>Y</emphasis><emphasis>,</emphasis><emphasis>Z</emphasis><emphasis role="roman">)</emphasis> for any label set &#x2112; , linearity <emphasis>Y</emphasis> , and restriction <emphasis>Z</emphasis>. The use of disjoint union in the EXTENDROW rule ensures that row types have distinct labels. A row type can only be used to build a record or variant if it has kind <emphasis role="romanAlt1">Row</emphasis>&#x00F8;; this constraint ensures that any absent labels in an open row type are mentioned explicitly.</para>
<fig id="fig12_3" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 12.3</label>
<caption><title>Kinding rules.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig065.jpg" mime-subtype="jpeg"/>
</fig>
<para>In <link linkend="fig12_4">Figure <xref linkend="fig12_4" remap="12.4"/></link> we define two auxiliary judgments that for use in the typing rules. The linearity judgment &#x0394; <emphasis></emphasis> &#x22A2; &#x0393; : <emphasis>Y</emphasis> is the pointwise extension of the kinding judgment restricted to the linearity component of the kind. It states that in kind environment &#x0394;, each type in environment &#x0393; has linearity <emphasis>Y</emphasis>. The type environment splitting judgment &#x0394; <emphasis></emphasis> &#x22A2; &#x0393; <emphasis role="roman">=</emphasis> &#x0393;<subscript>1</subscript> <emphasis role="roman">+</emphasis> &#x0393;<subscript>2</subscript> states that in kind environment &#x0394;, the type environment &#x0393; can be split into type environments &#x0393;<subscript>1</subscript> and &#x0393;<subscript>2</subscript>. Contraction of unlimited types is built into this judgment.</para>
<fig id="fig12_4" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 12.4</label>
<caption><title>Linearity of contexts and context splitting.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig066.jpg" mime-subtype="jpeg"/>
</fig>
<para>The typing rules are given in <link linkend="fig12_5">Figure <xref linkend="fig12_5" remap="12.5"/></link>. The typing judgment &#x0394;;&#x0393; &#x22A2;<emphasis></emphasis> <emphasis>M</emphasis> : <emphasis>A</emphasis> states that in kind environment &#x0394; and type environment &#x0393;, the term <emphasis>M</emphasis> has type <emphasis>A</emphasis>. We assume that &#x0393; and <emphasis>A</emphasis> are well-kinded with respect to &#x0394;. If &#x0394; and &#x0393; are empty (that is, <emphasis>M</emphasis> is a closed term), then we will often omit them, writing &#x22A2;<emphasis>M</emphasis> : <emphasis>A</emphasis> for <emphasis>&#x00B7;</emphasis>;<emphasis>&#x00B7;</emphasis> &#x22A2;<emphasis>M</emphasis> : <emphasis>A</emphasis>.</para>
<para>We assume a signature &#x03A3; mapping constants to their types. The definition of &#x03A3; on the basic concurrency primitives is given in <link linkend="fig12_6">Figure <xref linkend="fig12_6" remap="12.6"/></link>.</para>
<para>The E<emphasis role="smaller">XTEND</emphasis> rule is strict in the sense that it requires a label to be absent from a record before the record can be extended with the label. The CASE rule refines the type of the value being matched so that in the type of the variable bound by the default branch, the non-matched label is absent.</para>
<para><emphasis role="strong">Selection and Choice.</emphasis> Traditional accounts of session types include types for selection and choice. Following our previous work [<link linkend="ch12_b13">13</link>], inspired by Kobayashi [<link linkend="ch12_b8">8</link>], we encode selection and choice using variant types.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e276_01.jpg" mime-subtype="jpeg"/></para>
<fig id="fig12_5" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 12.5</label>
<caption><title>Typing rules.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig067.jpg" mime-subtype="jpeg"/>
</fig>
<fig id="fig12_6" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 12.6</label>
<caption><title>Type schemas for constants.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig068.jpg" mime-subtype="jpeg"/>
</fig>
<para>The encoding of <emphasis role="strong">select</emphasis> uses <emphasis role="strong">fork</emphasis> in order to generate a fresh channel of the continuation type. In the implementation of Links we support selection and choice in the source language. This is primarily for programming convenience. One might imagine desugaring these using the rules above, and then potentially rediscovering them in the back-end for performance reasons.</para>
<para><emphasis role="strong">Semantics.</emphasis> In the extended version of this article [<link linkend="ch12_b15">15</link>] we give an asynchronous small-step operational semantics for FST. Following Gay and Vasconcelos [<link linkend="ch12_b7">7</link>], whose calculus we call LAST (for Linear Asynchronous Session Types), we factor the semantics into functional and concurrent reduction relations, and introduce explicit buffers to provide asynchrony. For the functional fragment of the language, we give a standard left-to-right call-by-value semantics. The semantics of the concurrent portion of the language is given by a reduction relation on configurations of process and buffers. This semantics differs from our previous work on GV [<link linkend="ch12_b13">13</link>] in that is relies on explicit buffers, allowing asynchrony between the sending and receiving of a message, and it uses standard &#x03B2;-reduction instead of weak explicit substitutions [<link linkend="ch12_b10">10</link>]. FST, like GV but unlike LAST, is deadlock-free, deterministic, and terminating.</para>
</section>
</section>
<section class="lev1" id="sec12-4" label="12.4" xreflabel="12.4">
<title>Extensions</title>
<para>FST can be straightforwardly extended with additional features.</para>
<para>If we add a fixed point constant, then we lose termination, but deadlock freedom and determinism continue to hold. Another standard extension supported by Links is recursive types. While care is needed in defining the dual of a recursive session type, the treatment is otherwise quite standard. Negative recursive types allow a fixed point combinator to be defined, so again we lose termination, but deadlock freedom and determinism continue to hold.</para>
<para>The price we pay for the strong properties we obtain is that our model of concurrency is rather weak. For instance, it gives us no way of implementing a server with any notion of shared state. Drawing on LAST (and previous work on session-typed &#x03C0;-calculi), Links supports <emphasis>access points</emphasis>, which provide a much more expressive model of concurrency at the cost of introducing deadlock. Nevertheless, it is often possible to locally restrict code to a deadlock-free subset of Links.</para>
<section class="lev2" id="sec12-4-1" label="12.4.1" xreflabel="12.4.1">
<title>Recursion</title>
<para>The grammar of session types we have presented so far is rather limited; for example, it cannot express repeated behavior. As illustrated in Section 12.2, we can use recursive session types to define a calculator that supports multiple calculations. In order to support this kind of example, we can straightforwardly extend FST with equi-recursive types. We add a kinding rule for recursive types and identify each recursive type with its unrolling.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e279_01.jpg" mime-subtype="jpeg"/></para>
<para>It is well-known [<link linkend="ch12_b2">2</link>, <link linkend="ch12_b3">3</link>] that recursive types complicate the definition of duality, particularly when the recursion variable appears as a carried type (that is, as <emphasis>A</emphasis> in ?<emphasis>A</emphasis><emphasis>.</emphasis><emphasis>S</emphasis> or !<emphasis>A</emphasis><emphasis>.</emphasis><emphasis>S</emphasis>). For example, consider the simple recursive session type <emphasis role="strong">rec</emphasis> &#x03C3;<emphasis><sup>&#x00BA;</sup></emphasis><emphasis>,</emphasis>&#x03C0; <emphasis>.</emphasis>?&#x03C3; <emphasis>.</emphasis>&#x03C3; . The dual of this type is not <emphasis role="strong">rec</emphasis> &#x03C3;<emphasis><sup>&#x00BA;</sup></emphasis><emphasis>,</emphasis>&#x03C0; <emphasis>.</emphasis>!&#x03C3; <emphasis>.</emphasis>&#x03C3; , as one would obtain by taking the dual of the body of the recursive type directly, but is <emphasis role="strong">rec</emphasis> <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e279_02.jpg" mime-subtype="jpeg"/> instead.</para>
<para>Bernardi and Hennessy [<link linkend="ch12_b2">2</link>] point out that even existing definitions that correctly handle the above instance of recursion variables appearing inside a carried type often fail for other examples. The underlying difficulty arises from attempting to define duality in a setting in which the duality operator may not be applied to atomic type variables. Bernardi and Hennessy show that is is possible to give a correct definition in such a setting, but we prefer the more compositional definition that arises naturally when one admits duals of atomic type variables [<link linkend="ch12_b16">16</link>] (something that we want anyway as our calculus is polymorphic).</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e279_03.jpg" mime-subtype="jpeg"/></para>
<para>Having added recursive types, one can of course encode a fixed point combinator. Alternatively, we can add a fixed point constant to F<emphasis role="smaller">ST</emphasis>, even without recursive types:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e279_04.jpg" mime-subtype="jpeg"/></para>
<para>Of course, these extensions allows us to write nonterminating programs, but it is straightforward to show that subject reduction, progress, deadlock freedom, and determinism continue to hold.</para>
</section>
<section class="lev2" id="sec12-4-2" label="12.4.2" xreflabel="12.4.2">
<title>Access Points</title>
<para>In order to extend FST with access points, we replace the constant <emphasis role="strong">fork</emphasis> with four new constants:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e279_05.jpg" mime-subtype="jpeg"/></para>
<para>A process <emphasis>M</emphasis> is spawned with <emphasis role="strong">spawn</emphasis> <emphasis>M</emphasis>, where <emphasis>M</emphasis> is a thunk that returns an arbitrary unlimited value; we can define <emphasis role="strong">spawn</emphasis> in terms of <emphasis role="strong">fork</emphasis> and vice versa:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e280_01.jpg" mime-subtype="jpeg"/></para>
<para>Session-typed channels are created through access points. A fresh access point of type <emphasis role="romanAlt1">AP</emphasis> <emphasis>S</emphasis> is created with <emphasis role="strong">new</emphasis>. Given an access point <emphasis>L</emphasis> of type <emphasis role="romanAlt1">AP</emphasis> <emphasis>S</emphasis> we can create a new server channel (<emphasis role="strong">accept</emphasis> <emphasis>L</emphasis>), of session type <emphasis>S</emphasis>, or client channel (<emphasis role="strong">request</emphasis> <emphasis>L</emphasis>), of session type <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e280_02.jpg" mime-subtype="jpeg"/>. Processes can accept and request an arbitrary number of times on any given access point. Access points are synchronous in the sense that each <emphasis role="strong">accept</emphasis> will block until it is paired up with a corresponding <emphasis role="strong">request</emphasis> and vice-versa.</para>
<para>Adding access points exposes the difference between asynchronous and synchronous semantics. Here is an example of a term that reduces to a value under an asynchronous semantics, but deadlocks under a synchronous semantics.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e280_03.jpg" mime-subtype="jpeg"/></para>
<para>Under an asynchronous semantics, both sends happen followed by both receives, and the term reduces to the value 0. Under a synchronous semantics both sends are blocked and the term is deadlocked.</para>
<para><emphasis role="strong">Shared State.</emphasis> With access points we can implement shared state cells.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e280_04.jpg" mime-subtype="jpeg"/></para>
<para><emphasis role="strong">Nondeterminism.</emphasis> We can straightforwardly encode nondeterministic choice by using an access point to generate a nondeterministic boolean value. Suppose that we have &#x0394;;&#x0393; &#x22A2; <emphasis>M</emphasis> : <emphasis>T</emphasis> and &#x0394;;&#x0393; &#x22A2; <emphasis>N</emphasis> : <emphasis>T</emphasis> . The following term will nondeterministically choose between terms <emphasis>M</emphasis> and <emphasis>N</emphasis>:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e281_01.jpg" mime-subtype="jpeg"/></para>
<para>One process is left waiting on <emphasis role="strong">accept</emphasis> <emphasis>z</emphasis>. However, as <emphasis>z</emphasis> cannot escape, this process can be safely garbage collected.</para>
<para><emphasis role="strong">Recursion.</emphasis> Recursion can in fact be encoded using access points. We have already seen that access points are expressive enough to simulate higher-order state. We can now use Landin&#x2019;s knot (back-patching) [<link linkend="ch12_b9">9</link>] to implement recursion. For instance, the following term loops forever:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e281_02.jpg" mime-subtype="jpeg"/></para>
</section>
</section>
<section class="lev1" id="sec12-5" label="12.5" xreflabel="12.5">
<title>Links with Session Types</title>
<para>Version 0.6 of the Links web programming language includes an extension based on FST. It is available online from the Links website:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e281_03.jpg" mime-subtype="jpeg"/></para>
<para>Links is a functional programming language for the web. From a single source program, Links generates code to run on all three tiers of a web application: the browser, the server, and the database. Links is a call-by-value language with support for ML-style type inference (extended with support for first-class polymorphism similar to that of provided by the impredicative polymorphism extension of GHC [<link linkend="ch12_b22">22</link>]). It incorporates a row-type system that is used for records, variants, and effects, and provides equi-recursive types. Subkinding is used to distinguish base types from other types. This is important for enforcing the constraint that generated SQL queries must return a list of records whose fields are of base type [<link linkend="ch12_b11">11</link>].</para>
<para>In order to keep the presentation uniform and self-contained we use the concrete syntax of FST throughout rather than that of Links. However, all of the examples presented in this article can be written directly in Links with essentially the same abstract syntax, modulo the fact that Links uses HindleyMilner style type inference.</para>
<section class="lev2" id="sec12-5-1" label="12.5.1" xreflabel="12.5.1">
<title>Design Choices</title>
<para>Before implementing session types for Links we considered a number of design choices. Linearity is central to our description of session types. Most existing functional languages (including vanilla Links) do not provide native support for linear types. We considered three broad approaches:</para>
<orderedlist numeration="arabic" continuation="restarts" spacing="normal">
<listitem><para>encode linearity using existing features of the programming language (as in Pucella and Tov&#x2019;s Haskell encoding of session types [<link linkend="ch12_b19">19</link>] or our Haskell encoding of session types [<link linkend="ch12_b14">14</link>])</para></listitem>
<listitem><para>stratify the language so that the linear fragment of the language is separated out from the host language (as in Toninho et al&#x2019;s work [<link linkend="ch12_b20">20</link>])</para></listitem>
<listitem><para>bake linearity into the type system of the whole language (as in LAST [<link linkend="ch12_b7">7</link>])</para></listitem>
</orderedlist>
<para>The appeal of the first approach is that it does not require any new language features, assuming the starting point is a language with a sufficiently rich type system&#x2014;for example, one that is able to conveniently encode parameterized monads [<link linkend="ch12_b1">1</link>], or parameterized higher-order abstract syntax [<link linkend="ch12_b5">5</link>]. The second approach is somewhere in between. It allows a linear language to be embedded in an existing host language without disrupting the host language. The third approach requires linearity to pervade the whole type system, but opens up interesting possibilities for code reuse, for instance through polymorphism over linearity [<link linkend="ch12_b24">24</link>] or through subkinding [<link linkend="ch12_b17">17</link>].</para>
<para>Given that we are in the business of developing our own programming language, we decided to pursue the third option. We wanted to include the full expressivity of our language in the linear fragment, so we did not see a significant benefit in stratification, and we wanted to explore possibilities for code-reuse offered by baking linearity into the type system. We were also presented with another choice regarding how to accommodate code reuse. Given that Links already supported subkinding [<link linkend="ch12_b11">11</link>] we elected to adopt the linear subkinding approach of Mazurak et al. [<link linkend="ch12_b17">17</link>].</para>
<para>An advantage of the LAST (and FST) approach to session typing is that channels are first class and hence support compositional programming. This is in contrast to the parameterized monad approach and approaches based on process calculi, in which channels are just names. For example, in FST with recursive types we can define broadcasting a value to a whole list of channels:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e282_01.jpg" mime-subtype="jpeg"/></para>
<para>where <emphasis role="romanAlt1">LinList</emphasis> <emphasis>A</emphasis> is a linear list data type and <emphasis role="romanAlt1">linMap</emphasis> is the map operation over linear lists:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e283_01.jpg" mime-subtype="jpeg"/></para>
<para>An attendant drawback to having first-class channels is that one must explicitly rebind channels after each operation. This is in contrast to the parameterized monad approach and approaches based on process calculi, which implicitly rebind channels after each communication. In order to mitigate the need to explicitly rebind channels, we introduce process calculus style syntactic sugar inspired by previous work on the correspondence between classical linear logic and functional sessions [<link linkend="ch12_b12">12</link>, <link linkend="ch12_b13">13</link>, <link linkend="ch12_b23">23</link>]. To ease the job of writing a parser, we explicitly delimit process calculus style syntactic sugar with special brackets <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e283_02.jpg" mime-subtype="jpeg"/>.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e283_03.jpg" mime-subtype="jpeg"/></para>
<para>We let <emphasis>Q</emphasis> range over process calculus style terms. The desugaring of input, output, selection, and branching is direct. The <literal>{</literal> &#x2013; <literal>}</literal> brackets allow values to be returned from the tail of a process calculus expression. As an example, we can more concisely rewrite the one-shot calculator server of Section 12.2 as follows:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e283_04.jpg" mime-subtype="jpeg"/></para>
<para>In general, the syntactic sugar allows us to take advantage of a process-calculus style for communication-heavy sequences of code, but switch back to a functional style for compositional programming.</para>
</section>
<section class="lev2" id="sec12-5-2" label="12.5.2" xreflabel="12.5.2">
<title>Type Reconstruction</title>
<para>Vanilla Links provides type inference, as in many other typed functional languages. However, as a consequence of the typing of application, the types of higher-order functions in FST are not uniquely determined by their uses. As an example, consider the application operator in FST, implemented by the following term:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e284_01.jpg" mime-subtype="jpeg"/></para>
<para>This term is well-typed for arbitrary choices of <emphasis>Y</emphasis><subscript>1</subscript> and <emphasis>Y</emphasis><subscript>2</subscript>, and any choice of <emphasis>Y</emphasis><subscript>3</subscript> more constraining than <emphasis>Y</emphasis><subscript>2</subscript>, giving six distinct well-typed instantiations in all.</para>
<para>There are several ways we might hope to restore complete type inference, but they each come with significant additional complexity. We could introduce bounded quantification over linearities, combining the approaches of Tov and Pucella [<link linkend="ch12_b21">21</link>] and Walker [<link linkend="ch12_b24">24</link>]; in addition to introducing new forms of quantification, the implications of the resulting system for type inference have not been studied. Another approach was recently proposed by Morris [<link linkend="ch12_b18">18</link>]. His approach captures all the variations of the term above in a single term, and provides complete type inference. However, it relies on qualified types, an alternative source of complexity. In Links, we prefer unlimited function types &#x03C4; <emphasis>&#x2192;</emphasis><emphasis><sup>&#x2022;</sup></emphasis> &#x03C4;<sup>&#x2032;</sup> to linear function types &#x03C4; <emphasis>&#x2192;</emphasis><emphasis><sup>&#x00BA;</sup></emphasis> &#x03C4;<sup>&#x2032;</sup> when inferring the types of functions. The programmer is always free to override this choice by explicitly providing types. This approach preserves the simplicity of the language and of type reconstruction, but at the cost of some completeness.</para>
</section>
</section>
<section class="lev1" id="sec12-6" label="12.6" xreflabel="12.6">
<title>Conclusion and Future Work</title>
<para>We have presented an account of lightweight functional session types, extending a core session-typed linear &#x03BB;-calculus [<link linkend="ch12_b13">13</link>] with: the row typing of the core language for Links [<link linkend="ch12_b11">11</link>], the subkinding for linearity of Mazurak et al.&#x2019;s lightweight linear types [<link linkend="ch12_b17">17</link>], and the asynchrony and access points of Gay and Vasconcelos&#x2019;s linear type theory for asynchronous session types [<link linkend="ch12_b7">7</link>].</para>
<para>There is a significant gap between variants of FST with and without access points. We would like to investigate abstractions that add some of the expressive power of access points, but are better behaved. In particular, it would be interesting to explore richer type systems for enforcing deadlock and race freedom, while allowing some amount of stateful concurrency. More immediately, it would also be natural to exploit the existing effect type system of Links to statically enforce desirable properties, for instance, by associating the use of access points with a particular effect type.</para>
</section>
<section class="lev1" id="sec2a">
<title>References</title>
<orderedlist numeration="arabic" continuation="restarts" spacing="normal">
<listitem><para><anchor id="ch12_b1"/>R. Atkey. Parameterised notions of computation. <emphasis>J. Funct. Program.</emphasis>, 19(3&#x2013;4):335&#x2013;376, 2009. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=R%2E+Atkey%2E+Parameterised+notions+of+computation%2E+J%2E+Funct%2E+Program%2E%2C+19%283-4%29%3A335-376%2C+2009%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch12_b2"/>G. Bernardi and M. Hennessy. Using higher-order contracts to model session types. <emphasis>CoRR</emphasis>, abs/1310.6176v4, 2015. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=G%2E+Bernardi+and+M%2E+Hennessy%2E+Using+higher-order+contracts+to+model+session+types%2E+CoRR%2C+abs%2F1310%2E6176v4%2C+2015%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch12_b3"/>V. Bono and L. Padovani. Typing copyless message passing. <emphasis>Logical Methods in Computer Science</emphasis>, 8(1), 2012. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=V%2E+Bono+and+L%2E+Padovani%2E+Typing+copyless+message+passing%2E+Logical+Methods+in+Computer+Science%2C+8%281%29%2C+2012%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch12_b4"/>L. Caires and F. Pfenning. Session types as intuitionistic linear propositions. In <emphasis>CONCUR</emphasis>. Springer, 2010. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=L%2E+Caires+and+F%2E+Pfenning%2E+Session+types+as+intuitionistic+linear+propositions%2E+In+CONCUR%2E+Springer%2C+2010%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch12_b5"/>J. Carette, O. Kiselyov, and C. Shan. Finally tagless, partially evaluated: Tagless staged interpreters for simpler typed languages. <emphasis>J. Funct. Program.</emphasis>, 19(5):509&#x2013;543, 2009. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=J%2E+Carette%2C+O%2E+Kiselyov%2C+and+C%2E+Shan%2E+Finally+tagless%2C+partially+evaluated%3A+Tagless+staged+interpreters+for+simpler+typed+languages%2E+J%2E+Funct%2E+Program%2E%2C+19%285%29%3A509-543%2C+2009%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch12_b6"/>J. Garrigue, G. Keller, and E. Sumii, editors. <emphasis>ICFP</emphasis>. ACM, 2016. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=J%2E+Garrigue%2C+G%2E+Keller%2C+and+E%2E+Sumii%2C+editors%2E+ICFP%2E+ACM%2C+2016%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch12_b7"/>S. J. Gay and V. T. Vasconcelos. Linear type theory for asynchronous session types. <emphasis>J. Funct. Program.</emphasis>, 20(01):19&#x2013;50, 2010. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=S%2E+J%2E+Gay+and+V%2E+T%2E+Vasconcelos%2E+Linear+type+theory+for+asynchronous+session+types%2E+J%2E+Funct%2E+Program%2E%2C+20%2801%29%3A19-50%2C+2010%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch12_b8"/>N. Kobayashi. Type systems for concurrent programs. In <emphasis>10th Anniversary Colloquium of UNU/IIST</emphasis>. Springer, 2002. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=N%2E+Kobayashi%2E+Type+systems+for+concurrent+programs%2E+In+10th+Anniversary+Colloquium+of+UNU%2FIIST%2E+Springer%2C+2002%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch12_b9"/>P. J. Landin. The mechanical evaluation of expressions. <emphasis>Computer Journal</emphasis>, 6(4):308&#x2013;320, 1964. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=P%2E+J%2E+Landin%2E+The+mechanical+evaluation+of+expressions%2E+Computer+Journal%2C+6%284%29%3A308-320%2C+1964%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch12_b10"/>J. L&#x00E9;vy and L. Maranget. Explicit substitutions and programming languages. In <emphasis>FSTTCS</emphasis>, volume 1738 of <emphasis>LNCS</emphasis>, pages 181&#x2013;200. Springer, 1999. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=J%2E+L%E9vy+and+L%2E+Maranget%2E+Explicit+substitutions+and+programming+languages%2E+In+FSTTCS%2C+volume+1738+of+LNCS%2C+pages+181-200%2E+Springer%2C+1999%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch12_b11"/>S. Lindley and J. Cheney. Row-based effect types for database integration. In B. C. Pierce, editor, <emphasis>TLDI</emphasis>. ACM, 2012. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=S%2E+Lindley+and+J%2E+Cheney%2E+Row-based+effect+types+for+database+integration%2E+In+B%2E+C%2E+Pierce%2C+editor%2C+TLDI%2E+ACM%2C+2012%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch12_b12"/>S. Lindley and J. G. Morris. Sessions as propositions. In <emphasis>PLACES</emphasis>, 2014. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=S%2E+Lindley+and+J%2E+G%2E+Morris%2E+Sessions+as+propositions%2E+In+PLACES%2C+2014%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch12_b13"/>S. Lindley and J. G. Morris. A semantics for propositions as sessions. In J. Vitek, editor, <emphasis>ESOP</emphasis>, volume 9032 of <emphasis>Lecture Notes in Computer Science</emphasis>, pages 560&#x2013;584. Springer, 2015. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=S%2E+Lindley+and+J%2E+G%2E+Morris%2E+A+semantics+for+propositions+as+sessions%2E+In+J%2E+Vitek%2C+editor%2C+ESOP%2C+volume+9032+of+Lecture+Notes+in+Computer+Science%2C+pages+560-584%2E+Springer%2C+2015%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch12_b14"/>S. Lindley and J. G. Morris. Embedding session types in haskell. In G. Mainland, editor, <emphasis>Haskell</emphasis>, pages 133&#x2013;145. ACM, 2016. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=S%2E+Lindley+and+J%2E+G%2E+Morris%2E+Embedding+session+types+in+haskell%2E+In+G%2E+Mainland%2C+editor%2C+Haskell%2C+pages+133-145%2E+ACM%2C+2016%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch12_b15"/>S. Lindley and J. G. Morris. Lightweight functional session types (extended version). <literal>http://homepages.inf.ed.ac.uk/slindley/papers/fst-extended.pdf</literal>, 2016. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=S%2E+Lindley+and+J%2E+G%2E+Morris%2E+Lightweight+functional+session+types+%28extended+version%29%2E+http%3A%2F%2Fhomepages%2Einf%2Eed%2Eac%2Euk%2Fslindley%2F+papers%2Ffst-extended%2Epdf%2C+2016%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch12_b16"/>S. Lindley and J. G. Morris. Talking bananas: structural recursion for session types. In Garrigue et al. [<link linkend="ch12_b6">6</link>], pages 434&#x2013;447. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=S%2E+Lindley+and+J%2E+G%2E+Morris%2E+Talking+bananas%3A+structural+recursion+for+session+types%2E+In+Garrigue+et+al%2E+%5B6%5D%2C+pages+434-447%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch12_b17"/>K. Mazurak, J. Zhao, and S. Zdancewic. Lightweight linear types in System F<emphasis><sup>&#x00BA;</sup></emphasis>. In A. Kennedy and N. Benton, editors, <emphasis>TLDI</emphasis>. ACM, 2010. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=K%2E+Mazurak%2C+J%2E+Zhao%2C+and+S%2E+Zdancewic%2E+Lightweight+linear+types+in+System+F%B0%2E+In+A%2E+Kennedy+and+N%2E+Benton%2C+editors%2C+TLDI%2E+ACM%2C+2010%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch12_b18"/>J. G. Morris. The best of both worlds: linear functional programming without compromise. In Garrigue et al. [<link linkend="ch12_b6">6</link>], pages 448&#x2013;461. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=J%2E+G%2E+Morris%2E+The+best+of+both+worlds%3A+linear+functional+programming+without+compromise%2E+In+Garrigue+et+al%2E+%5B6%5D%2C+pages+448-461%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch12_b19"/>R. Pucella and J. A. Tov. Haskell session types with (almost) no class. In A. Gill, editor, <emphasis>Haskell</emphasis>. ACM, 2008. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=R%2E+Pucella+and+J%2E+A%2E+Tov%2E+Haskell+session+types+with+%28almost%29+no+class%2E+In+A%2E+Gill%2C+editor%2C+Haskell%2E+ACM%2C+2008%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch12_b20"/>B. Toninho, L. Caires, and F. Pfenning. Higher-order processes, functions, and sessions: A monadic integration. In <emphasis>ESOP</emphasis>. Springer, 2013. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=B%2E+Toninho%2C+L%2E+Caires%2C+and+F%2E+Pfenning%2E+Higher-order+processes%2C+functions%2C+and+sessions%3A+A+monadic+integration%2E+In+ESOP%2E+Springer%2C+2013%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch12_b21"/>J. A. Tov and R. Pucella. Practical affine types. In T. Ball and M. Sagiv, editors, <emphasis>POPL</emphasis>, pages 447&#x2013;458. ACM, 2011. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=J%2E+A%2E+Tov+and+R%2E+Pucella%2E+Practical+affine+types%2E+In+T%2E+Ball+and+M%2E+Sagiv%2C+editors%2C+POPL%2C+pages+447-458%2E+ACM%2C+2011%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch12_b22"/>D. Vytiniotis, S. Weirich, and S. L. Peyton Jones. FPH: first-class polymorphism for Haskell. In J. Hook and P. Thiemann, editors, <emphasis>ICFP</emphasis>. ACM, 2008. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=D%2E+Vytiniotis%2C+S%2E+Weirich%2C+and+S%2E+L%2E+Peyton+Jones%2E+FPH%3A+first-class+polymorphism+for+Haskell%2E+In+J%2E+Hook+and+P%2E+Thiemann%2C+editors%2C+ICFP%2E+ACM%2C+2008%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch12_b23"/>P. Wadler. Propositions as sessions. <emphasis>J. Funct. Program.</emphasis>, 24(2&#x2013;3):384&#x2013; 418, 2014. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=P%2E+Wadler%2E+Propositions+as+sessions%2E+J%2E+Funct%2E+Program%2E%2C+24%282-3%29%3A384-+418%2C+2014%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch12_b24"/>D. Walker. Substructural Type Systems. In B. C. Pierce, editor, <emphasis>Advanced Topics in Types and Programming Languages</emphasis>, chapter 1. MIT Press, 2005.</para></listitem>
</orderedlist>
</section>
</chapter>
<chapter class="chapter" id="ch13" label="13" xreflabel="13">
<title>Distributed Programming Using Java APIs Generated from Session Types</title>
<para><emphasis role="strong">Raymond Hu</emphasis></para>
<para>Imperial College London, UK</para>
<section class="lev2">
<title>Abstract</title>
<para>This is a tutorial on using Scribble [<link linkend="ch13_b9">9</link>], a toolchain based on multiparty session types [<link linkend="ch13_b1">1</link>, <link linkend="ch13_b4">4</link>], for distributed programming in Java. The methodology is based on the generation of <emphasis>protocol-specific Endpoint APIs</emphasis> from Scribble specifications [<link linkend="ch13_b6">6</link>]. We start with a brief recap of TCP network programming using standard Java APIs, and their limitations with regards to safety assurances. The main tutorial content is an overview of the key stages of the Scribble toolchain, from global protocol specification, through Endpoint API generation, to Java endpoint implementation, with examples. We discuss the hybrid form of session safety promoted by the Endpoint API generation approach. We then consider Scribble specifications and implementations of HTTP as a real-world use case. Finally, we demonstrate some further Scribble features that leverage Endpoint API generation to safely support more advanced communication patterns.</para>
</section>
<section class="lev1" id="sec13-1" label="13.1" xreflabel="13.1">
<title>Background: Distributed Programming in Java</title>
<para>The two core facilities for TCP-based network programming in Java (and other languages) are the <emphasis>socket APIs</emphasis> and Java <emphasis>Remote Method Invocation</emphasis> (RMI). The socket APIs allow the programmer to work directly with TCP connections, and are the basis over which many higher-level networking facilities are built. Java RMI is the Java adaptation of remote procedure call (RPC) functionality; with regards to this discussion, RMI is representative of the corresponding facilities in other languages or platform-independent frameworks, <emphasis>e.g.</emphasis>, RESTful Web services.</para>
<para><emphasis role="strong">Running example: Math Service.</emphasis> As a running Hello World example, we specify and implement a two-party network service for basic arithmetic operations. For the purposes of this tutorial, we are not concerned with the most realistic development of such a service, but rather that this simple example features core constructs of protocol design, such as message sequencing, alternative cases and repeated sequences.</para>
<para><link linkend="fig13_1">Figure <xref linkend="fig13_1" remap="13.1"/></link> depicts the Math Service protocol as a UML sequence diagram [<link linkend="ch13_b8">8</link>, <emphasis>&#x00A7;</emphasis>17]. For some number of repetitions (<literal>loop</literal>), the client <literal>C</literal> sends to the server <literal>S</literal> a <literal>Val</literal> message with an <literal>Integer</literal> payload; <literal>C</literal> then selects between the two alternatives (<literal>alt</literal>), to send an <literal>Add</literal> or a <literal>Mult</literal> message carrying a second <literal>Integer</literal>. <literal>S</literal> respectively replies with a <literal>Sum</literal> or a <literal>Prod</literal> message carrying the result. Finally, <literal>C</literal> sends a <literal>Bye</literal> message, with no payload, ending the session.</para>
<section class="lev2" id="sec13-1-1" label="13.1.1" xreflabel="13.1.1">
<title>TCP Sockets</title>
<para>Sockets are supported in the standard Java API by the <literal>java.net</literal> and <literal>java.nio.channels</literal> packages. <link linkend="fig13_2">Figure <xref linkend="fig13_2" remap="13.2"/></link> gives a client implementation using Math Service for a factorial calculation via the <literal>java.net.Socket</literal> API. For simplicity, we assume serializable Java classes for each of the message types (<emphasis>e.g.</emphasis>, <literal>Add</literal>), with a field <literal>val</literal> for the <literal>Integer</literal> payload, and use standard Java object serialization via <literal>java.io.ObjectOutput</literal>/<literal>InputStream</literal>.</para>
<fig id="fig13_1" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 13.1</label>
<caption><title>Sequence diagram for the Math Service protocol.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig069.jpg" mime-subtype="jpeg"/>
</fig>
<fig id="fig13_2" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 13.2</label>
<caption><title>A factorial calculation using Math Service via the <literal>java.net.Socket</literal> API.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig070.jpg" mime-subtype="jpeg"/>
</fig>
<para>From a networking perspective, TCP sockets offer a high-level abstraction in the sense of reliable and ordered message delivery. From an application perspective, however, the raw communication interface of a TCP channel is simply a pair of unidirectional bit streams&#x2014;essentially a communications machine code, in contrast to the support for high-level data types in &#8220;local&#8221; computations.</para>
<para>Working directly with standard socket APIs thus affords almost no safety assurances with regards to the correctness of protocol implementations. The key kinds of application-level protocol errors are:</para>
<itemizedlist mark="none" spacing="normal">
<listitem><para><emphasis role="strong">Communication mismatches</emphasis> when the sent message is not one of those expected by the receiver (also called a <emphasis>reception error</emphasis>). <emphasis>E.g.</emphasis>, if <literal>C</literal> were to commence a session with an <literal>Add</literal> message: assuming an implementation of <literal>S</literal> in the style of <link linkend="fig13_2">Figure <xref linkend="fig13_2" remap="13.2"/></link>, this would likely manifest as a <literal>ClassCastException</literal> on the object returned by the <literal>readObject</literal> in <literal>S</literal>. Note, the dual error of the receiver applying an incorrect cast are equally possible.</para></listitem>
<listitem><para><emphasis role="strong">Deadlock</emphasis> in situations where some set of participants are all blocked on mutually dependent input actions. <emphasis>E.g.</emphasis>,if <literal>C</literal> were to call <literal>readObject</literal> after sending <literal>Val</literal>, but before sending <literal>Add</literal> or <literal>Mult</literal>; while <literal>S</literal> is (correctly) waiting for one of the latter.</para></listitem>
<listitem><para><emphasis role="strong">Orphan messages</emphasis> if the receiver terminates without reading an incoming message. In practice, an orphan error often manifests as, <emphasis>e.g.</emphasis>, an <literal>EOFException</literal>, since TCP uses a termination handshake. <emphasis>E.g.</emphasis>, if <literal>S</literal> skips the receive of <literal>Bye</literal> before <literal>C</literal> has sent it, leading <literal>C</literal> to attempt the write on a closed connection.</para></listitem>
</itemizedlist>
</section>
<section class="lev2" id="sec13-1-2" label="13.1.2" xreflabel="13.1.2">
<title>Java RMI</title>
<para>RMI is a natural approach towards addressing the mismatch between high-level, typed Java programming and low-level networking interfaces. Distributed computations can be (partially) abstracted away as regular method invocations, while benefiting from static typing of each call and its communicated arguments and return value.</para>
<para>The Math Service protocol may be fitted to a remote interface as in <link linkend="fig13_4">Figure <xref linkend="fig13_4" remap="13.4"/></link>(a), essentially by decomposing the protocol into separate call-return fragments; and <link linkend="fig13_3">Figure <xref linkend="fig13_3" remap="13.3"/></link> re-implements the factorial calculation as a client of this interface. Individual remote calls are now statically typed with respect to their arguments and return. Unfortunately, RMI programs in general remain subject to the same potential protocol errors illustrated for the previous sockets example (although their concrete manifestations may differ). The typed RMI interface does not prevent, for example, a bad client from calling <literal>Add</literal> before <literal>Val</literal>.</para>
<para>Disadvantages of call-return based protocol decomposition are further illustrated by the (minimal) implementation of the remote interface in <link linkend="fig13_4">Figure <xref linkend="fig13_4" remap="13.4"/></link>(b), which suffices to serve a single client but is completely inadequate in the presence of concurrent clients. Basic RMI programs lose the notion of an explicit <emphasis>session</emphasis>-oriented abstraction in the code (cf., the threading of session control flow in <link linkend="fig13_2">Figure <xref linkend="fig13_2" remap="13.2"/></link> wrt. the socket/stream variable usages), which complicates the correlation and management of application-level session flows across the separate methods.</para>
</section>
</section>
<section class="lev1" id="sec13-2" label="13.2" xreflabel="13.2">
<title>Scribble Endpoint API Generation: Toolchain Overview</title>
<para>Using the Math Service running example, we demonstrate the stages of the Scribble toolchain, from global protocol specification, through Endpoint API generation, to Java endpoint implementation. The source code of the toolchain [<link linkend="ch13_b10">10</link>] and tutorial examples [<link linkend="ch13_b5">5</link>] are available online.</para>
<fig id="fig13_3" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 13.3</label>
<caption><title>Factorial calculation as a client of the remote interface in <link linkend="fig13_4">Figure <xref linkend="fig13_4" remap="13.4"/></link>(a).</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig071.jpg" mime-subtype="jpeg"/>
</fig>
<fig id="fig13_4" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 13.4</label>
<caption><title>A remote Math Service: (a) interface, and (b) implementation.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig072.jpg" mime-subtype="jpeg"/>
</fig>
<section class="lev2" id="sec13-2-1" label="13.2.1" xreflabel="13.2.1">
<title>Global Protocol Specification</title>
<para>The tool takes as its primary input a textual description of the source protocol or choreography from a global perspective. <link linkend="fig13_5">Figure <xref linkend="fig13_5" remap="13.5"/></link> is a Scribble <emphasis>global protocol</emphasis> for the Math Service running example.</para>
<para>A <emphasis>payload format type</emphasis> declaration (line 1) gives an alias (<literal>Int</literal>) to data type definitions from external languages (<literal>java.lang.Integer</literal>) used for message formatting. The <emphasis>protocol signature</emphasis> (line 2) declares the name of the <literal>global</literal> protocol (<literal>MathSvc</literal>) and the abstraction of each participant as a named <literal>role</literal> (<literal>C</literal> and <literal>S</literal>).</para>
<para>Message passing is written, <emphasis>e.g.</emphasis>, <literal>Val(Int) from C to S</literal>. A <emphasis>message signature</emphasis> (<literal>Val(Int)</literal>) declares an <emphasis>operator</emphasis> name (<literal>Val</literal>) as an abstract message identifier (which may be, <emphasis>e.g.</emphasis>, a header field value in the concrete message format), and some number of payload types (a single <literal>Int</literal>). Message passing is output-asynchronous: dispatching the message is non-blocking for the sender (<literal>C</literal>), but the message input is blocking for the receiver (<literal>S</literal>). A <emphasis>located choice</emphasis> (<emphasis>e.g.</emphasis>, <literal>choice at C</literal>) states the subject role (<literal>C</literal>) for which selecting one of the cases (the <literal>or</literal>-separated blocks) to follow is a mutually exclusive <emphasis>internal</emphasis> choice. This decision is an <emphasis>external</emphasis> choice to all other roles involved in each block, and must be appropriately coordinated by explicit messages. A <literal>do</literal> statament enacts the specified (sub)protocol, including recursive definitions (<emphasis>e.g.</emphasis>, line 8).</para>
<fig id="fig13_5" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 13.5</label>
<caption><title>Scribble global protocol for Math Service in <link linkend="fig13_1">Figure <xref linkend="fig13_1" remap="13.1"/></link>.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig073.jpg" mime-subtype="jpeg"/>
</fig>
<para>The body of the <literal>MathSvc</literal> protocol may be equivalently written in a similar syntax to standard recursive session types:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog179.jpg" mime-subtype="jpeg"/></para>
<para><emphasis role="strong">Protocol validation.</emphasis> The tool validates the well-formedness of global protocols. We do not discuss the details of this topic in this tutorial, but summarise a few elements. Firstly, the source protocol is subject to a range of syntactic checks. Besides basic details, such as bound role names and recursion variables, the key conditions are <emphasis>role enabling</emphasis>, <emphasis>consistent external choice subjects</emphasis> and <emphasis>reachability</emphasis>. Role enabling is a consistency check on the (transitive) propagation of choice messages originating from a choice subject to the other roles involved in the choice. The following is a very simple example of bad role enabling:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog180.jpg" mime-subtype="jpeg"/></para>
<para>Since the choice is <literal>at C</literal>, <literal>S</literal> should not perform any output before it is <emphasis>enabled</emphasis>, i.e, by receiving a message that directs it into the correct choice case. The second of the above conditions requires that every message of an external choice be communicated <emphasis>from</emphasis> the same role.</para>
<para>Reachability of protocol states is imposed on a per-role basis, <emphasis>i.e</emphasis>, on projections; Scribble protocols are also checked to be tail recursive per role. These rule out some basic syntactic inconsistencies (<emphasis>e.g.</emphasis>, sequential composition after a non-terminating recursion), and support the later FSM translation step (see below).</para>
<para>Together, the syntactic conditions support the main validation step based on explicit checking of safety errors (and progress violations), such as reception errors and deadlocks (outlined in <emphasis>&#x00A7;</emphasis> 13.1, on a bounded model of the protocol. For example, (wrongly) replacing <literal>Bye</literal> by <literal>Val</literal> will be found by the explicit error checking to be invalid.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog181.jpg" mime-subtype="jpeg"/></para>
<para>The ambiguous (non-deterministic) receipt of the decision message by <literal>S</literal> from <literal>C</literal> (<emphasis>i.e.</emphasis>, a message identified by <literal>Val</literal> in both cases&#x2014;Scribble does not introduce any implicit meta data or communications) may lead to various deadlock and orphan message errors, depending on the different permutations of <literal>C</literal> and <literal>S</literal> proceeding in the two cases. <emphasis>E.g.</emphasis>, if <literal>C</literal> proceeds in the right case and <literal>S</literal> in the left, then <literal>S</literal> will be stuck (in the &#8220;<literal>...</literal>&#8221;) waiting for an <literal>Add</literal>/<literal>Mult</literal> (or will encounter a broken connection error).</para>
<para><emphasis role="strong">Endpoint FSM generation.</emphasis> The next key step is the generation of an <emphasis>Endpoint Finite State Machine</emphasis> (EFSM) for each role in the protocol. We use the term EFSM for the particular class of multiparty communicating FSMs given by Scribble&#x2019;s syntax and validation. The construction is based on and extends the syntactic projection of <emphasis>global types</emphasis> to <emphasis>local types</emphasis> [<link linkend="ch13_b4">4</link>], followed by a translation to an EFSM, building on a correspondence between local types and communicating FSMs [<link linkend="ch13_b2">2</link>, <link linkend="ch13_b7">7</link>]. The nodes of an EFSM represent the states in the localised view of the protocol for the target role, and the transitions are the communication actions performed by the role between states. The EFSM for every role of a valid global protocol is defined.</para>
<para><link linkend="fig13_6">Figure <xref linkend="fig13_6" remap="13.6"/></link> depicts the (dual) EFSMs for <literal>C</literal> and <literal>S</literal> in <literal>MathSvc</literal>. The initial states are numbered <literal>1</literal>. The notation, <emphasis>e.g.</emphasis>, <literal>S!Val(Int)</literal> means output of message <literal>Val(Int)</literal> to <literal>S</literal>; <literal>?</literal> dually denotes input. The recursive definition of this protocol manifests as the cycles returning to state <literal>1</literal>.</para>
<fig id="fig13_6" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 13.6</label>
<caption><title>Endpoint FSMs for <literal>C</literal> and <literal>S</literal> in <literal>MathSvc</literal> (<link linkend="fig13_5">Figure <xref linkend="fig13_5" remap="13.5"/></link>).</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig074.jpg" mime-subtype="jpeg"/>
</fig>
</section>
<section class="lev2" id="sec13-2-2" label="13.2.2" xreflabel="13.2.2">
<title>Endpoint API Generation</title>
<para>For a given role of a valid global protocol, the toolchain generates an <emphasis>Endpoint API</emphasis> for implementing that role based on its EFSM. The current implementation generates Java APIs, but the principle may be applied or adapted to many statically-typed languages.</para>
<para>There are two main components of a generated Endpoint API, the <emphasis>Session API</emphasis> and the <emphasis>State Channel API</emphasis>. The generated APIs make use of a few protocol-independent base classes that are part of the Scribble runtime library: <literal>Role</literal>, <literal>Op</literal>, <literal>Session</literal>, <literal>MPSTEndpoint</literal> and <literal>Buf</literal>; the first three are abstract classes. These shall be explained below.</para>
<para><emphasis role="strong">Session API.</emphasis> The frontend class of the Session API, which we refer to as the <emphasis>Session Class</emphasis>, is a generated final subclass of the base <literal>Session</literal> class with the same name as the source protocol, <emphasis>e.g.</emphasis>, <literal>MathSvc</literal>. It has two main purposes. One is to house the family of protocol-specific constants for type-directed session programming in Java, generated as follows.</para>
<para>A session type based protocol specification features various kinds of names, such as role names and message labels. A session type system typically requires these names to be present in the processes to drive the type checking (<emphasis>e.g.</emphasis>, [<link linkend="ch13_b4">4</link>, <link linkend="ch13_b1">1</link>]). For the present Java setting, the Session API is generated to reify these abstract names as <emphasis>singleton types</emphasis> following a basic (eagerly initialised) singleton pattern. For each role or message operator name <emphasis>n</emphasis> in the source protocol, we generate:</para>
<itemizedlist mark="bullet" spacing="normal">
<listitem><para>A final Java class named <emphasis>n</emphasis> that extends the appropriate base class (<literal>Role</literal> or <literal>Op</literal>). The <emphasis>n</emphasis> class has a single private constructor, and a public static final field of type <emphasis>n</emphasis> and with name <emphasis>n</emphasis>, initialised to a singleton instance of this class.</para></listitem>
<listitem><para>In the Session Class, a public static final field of type <emphasis>n</emphasis> and with name <emphasis>n</emphasis>, initialised to the constant declared in the corresponding <emphasis>n</emphasis> class.</para></listitem>
</itemizedlist>
<para>For example, for role <literal>C</literal> of <literal>MathSvc</literal>, the subclass <literal>C</literal> of <literal>Role</literal> is generated to declare the singleton constant <literal>public static final C</literal> <emphasis><literal>C</literal></emphasis> <literal>= new C();</literal>. The <literal>MathSvc</literal> class is generated to collect these various constants together, including the field <literal>public static final C</literal> <emphasis><literal>C</literal></emphasis> <literal>= C.</literal><emphasis><literal>C</literal></emphasis> <literal>;.</literal></para>
<para>The Session API comprises the Session Class with the singleton type classes. The other main purpose of the Session Class is for session initiation in endpoint implementations, as explained below.</para>
<para><emphasis role="strong">An implementation of</emphasis> <literal>C</literal> <emphasis role="strong">via Endpoint API generation.</emphasis> At this point, we give, in <link linkend="fig13_7">Figure <xref linkend="fig13_7" remap="13.7"/></link>, a first version of the factorial calculation using the Endpoint API generated by the Scribble tool for <literal>C</literal> in <literal>MathSvc</literal>.</para>
<para>The code can be read similarly to the socket code in <emphasis>&#x00A7;</emphasis> 13.1.1; <emphasis>e.g.</emphasis>, <literal>s1</literal> is a session channel variable. A difference from the earlier socket code is that the Scribble API is generated as a <emphasis>fluent</emphasis> interface, allowing consecutive I/O operations to be chained (<emphasis>e.g.</emphasis>, line 11). We refer to and explain this code through the following subsections.</para>
<para><emphasis role="strong">Session initiation.</emphasis> Lines 3&#x2013;6 in <link linkend="fig13_7">Figure <xref linkend="fig13_7" remap="13.7"/></link> is a typical preamble for a (client) endpoint implementation using a Scribble-generated API. We start by creating a new <literal>MathSvc</literal> session by instantiating the Session Class. The session object, <literal>sess</literal>, is used to create a new session <emphasis>endpoint</emphasis> object of type <literal>MPSTEndpoint&lt;MathSvc, C&gt;</literal>, parameterised on the type of the session and the endpoint role. The <literal>C</literal> parameter in this type is the singleton type in the Session API; and the <literal>C</literal> argument in the constructor call is the single value of this type.</para>
<para>The third argument required by the <literal>MPSTEndpoint</literal> constructor is an implementation of the <literal>ScribMessageFormatter</literal> interface, that is responsible for the underlying serialization and deserialization of individual messages in this session. For this example, we use the default <literal>ObjectStreamFormatter</literal> provided by the Scribble runtime library based on the standard Java serialization protocol (messages communicated by this formatter must implement the <literal>Serializable</literal> interface).</para>
<fig id="fig13_7" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 13.7</label>
<caption><title>Factorial calculation using the Endpoint API generated for <literal>C</literal>.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig075.jpg" mime-subtype="jpeg"/>
</fig>
<para>Before proceeding to the main body of a protocol implementation, the <literal>MPSTEndpoint</literal> object is used to set up the session topology via connection establishment actions with the appropriate peer endpoints. On line 6, the <literal>MPSTEndpoint</literal> is used to perform the client-side <literal>connect</literal> to <literal>S</literal>. The second argument is a reference to the constructor of <literal>SocketChannelEndpoint</literal> in the Scribble runtime library, which embodies a standard TCP socket; alternatives include HTTP and shared memory endpoints. The connection setup phase is concluded when the <literal>MPSTEndpoint</literal> is passed as a constructor argument to the initial state channel constructor, <literal>MathSvc C 1</literal>, expained next.</para>
<para>The <literal>MPSTEndpoint</literal> implements the Java <literal>AutoCloseable</literal> interface and should be handled using a try-with-resources, as on line 4; the encapsulated network resources are implicitly closed when control flow exits the try statement.</para>
<para><emphasis role="strong">State Channel API.</emphasis> The State Channel API is generated to capture the protocol-specific behaviour of a role in the source global protocol, as represented by its EFSM, via the static typing facilities of Java.</para>
<itemizedlist mark="bullet" spacing="normal">
<listitem><para>Each state in the EFSM is reified as a Java class for a <emphasis>state-specific</emphasis> session channel, thus conferring a distinct Java type to channels at each state in a protocol. We refer to instances of these generated channel classes as <emphasis>state channels</emphasis>.</para></listitem>
<listitem><para>The I/O operations (methods) supported by a channel class are the transitions permitted by the corresponding EFSM state.</para></listitem>
<listitem><para>The return type of each generated I/O operation is the channel type for the successor state following the corresponding transition from the current state. Performing an I/O operation on a state channel returns a new instance of the successor channel type.</para></listitem>
</itemizedlist>
<para>By default, the API generation uses a simple state enumeration (<emphasis>e.g.</emphasis>, <link linkend="fig13_6">Figure <xref linkend="fig13_6" remap="13.6"/></link>) for the generated channel class names; <emphasis>e.g.</emphasis>, <literal>MathSvc C 1</literal> for the initial state channel. More meaningful names for states may be specified by the user as annotations in the Scribble source. The terminal state of an EFSM, if any, is generated as an <literal>EndSocket</literal> class that supports no further I/O operations. The channel class for the initial state is the only class with a public constructor, taking an <literal>MPSTEndpoint</literal> parameterised on the appropriate Session Class and role types; all other state channels are instantiated internally by the generated API operations.</para>
<para><link linkend="fig13_8">Figure <xref linkend="fig13_8" remap="13.8"/></link> summarises the generated channel classes and their main I/O operations for <literal>C</literal> in <literal>MathSvc</literal>. <emphasis>E.g.</emphasis>, a state channel of type <literal>MathSvc C 1</literal> supports methods for sending <literal>Val</literal> and <literal>Bye</literal> to <literal>S</literal>; these <literal>send</literal> methods are overloaded via the parameters for the destination role and message operator (the singleton types of the Session API), as well as the message payloads. Sending a <literal>Val</literal> returns a <literal>MathSvc C 2</literal> channel, <emphasis>i.e.</emphasis>, the state of sending an <literal>Add</literal> or <literal>Mult</literal>; whereas a sending a <literal>Bye</literal> returns an <literal>EndSocket</literal>.</para>
<fig id="fig13_8" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 13.8</label>
<caption><title>State Channel API generated for <literal>C</literal> in <literal>MathSvc</literal> (<link linkend="fig13_5">Figure <xref linkend="fig13_5" remap="13.5"/></link>).</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig076.jpg" mime-subtype="jpeg"/>
</fig>
<para>For unary input states, <emphasis>i.e.</emphasis>, an EFSM state with a single input transition, the generated <literal>receive</literal> method, <emphasis>e.g.</emphasis>, for <literal>Sum</literal> in <literal>MathSvc C 3</literal>, takes <literal>Buf</literal> arguments parameterised according to the expected payload type(s), if any. <literal>Buf&lt;T&gt;</literal> is a simple generic one-slot buffer provided by the Scribble runtime library, whose value is held in a public <literal>val</literal> field. The <literal>receive</literal> method is generated to write the payload(s) of the received message to the respective <literal>Buf</literal> arguments.</para>
<para>In <link linkend="fig13_7">Figure <xref linkend="fig13_7" remap="13.7"/></link>, lines 7&#x2013;14 use the State Channel API for <literal>C</literal> to perform the factorial calculation. Starting from the instance of <literal>MathSvc C 1</literal>, assigned to <literal>s1</literal>, the implementation proceeds by performing <emphasis>one</emphasis> I/O operation on each current state channel to obtain the next. The fluent API permits convenient chaining of I/O operations, <emphasis>e.g.</emphasis>, line 17 in <literal>sub1</literal> starts from state <literal>1</literal>, and proceeds through states <literal>2</literal> and <literal>3</literal> (by sending <literal>Val</literal> and <literal>Add</literal> messages), before returning to <literal>1</literal> (by receiving the <literal>Sum</literal>). The endpoint implementation is complete upon reaching <literal>EndSocket</literal>.</para>
<para>Attempting any I/O action that is not permitted by the current protocol state, as designated by the target state channel, will be caught by Java type checking. For example (from the Eclipse IDE):</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog182.jpg" mime-subtype="jpeg"/></para>
</section>
<section class="lev2" id="sec13-2-3" label="13.2.3" xreflabel="13.2.3">
<title>Hybrid Session Verification</title>
<para>As demonstrated above, Scribble-generated Endpoint APIs leverage standard, static Java type checking to verify protocol conformance, <emphasis>provided every state channel returned by an API operation is used exactly once up to the end of the session</emphasis>. This is the implicit usage contract of a Scribble-generated API, to respect EFSM semantics in terms of following state transitions linearly up to the terminal state.</para>
<para>Much research has been conducted towards static analyses for such resource usage properties: to this end, it may be possible to combine these with API generation tools to recover fully static safety guarantees in certain contexts. However, designing such analyses for mainstream engineering languages, such as Java and C#, in full generality is a significant challenge, and often based on additional language extensions or imposing various conservative restrictions.</para>
<para>As a practical compromise, the Endpoint API generation of Scribble promotes a <emphasis>hybrid</emphasis> approach to session verification. The idea is simply to complement the static type checking of session I/O on state channels with <emphasis>run-time</emphasis> checks that each state channel is indeed used exactly once in a session execution.</para>
<para><emphasis role="strong">Run-time checking of linear state channel usage.</emphasis> The checks on linear state channel usage are inlined into the State Channel API operations by the API generation. There are two cases by which state channel linearity may be violated.</para>
<itemizedlist mark="none" spacing="normal">
<listitem><para><emphasis role="strong">Repeat use</emphasis>. Every state channel instance maintains a boolean state value indicating whether it has been <emphasis>used</emphasis>, <emphasis>i.e.</emphasis>, a session I/O operation has been performed on the channel. The API generation guards each I/O operation with a run-time check on this boolean. If the channel has already been used, a <literal>LinearityException</literal> is raised.</para></listitem>
<listitem><para><emphasis role="strong">Unused</emphasis>. All state channels of a session instance share a boolean state value indicating whether the session is <emphasis>complete</emphasis> for the local endpoint. The API is generated to set this flag when a <emphasis>terminal operation</emphasis>, <emphasis>i.e.</emphasis> an I/O action leading to the terminal EFSM state, is performed. If control flow leaves the enclosing <literal>try</literal> statement of the associated <literal>MPSTEndpoint</literal>, the Scribble runtime checks this flag via the implicit <literal>close</literal> method of the <literal>AutoCloseable</literal> interface. If the session is incomplete, an exception is raised.</para></listitem>
</itemizedlist>
<para>It is not possible for the completion flag to be set if any state channel remains unused on leaving the <literal>try</literal> statement of an <literal>MPSTEndpoint</literal>. IDEs (<emphasis>e.g.</emphasis>, Eclipse) support compile-time warnings in certain situations where <literal>AutoClose</literal>-able resources are not appropriately handled by a <literal>try</literal>.</para>
<para><emphasis role="strong">Hybrid session safety.</emphasis> Together, a statically typed Endpoint API with runtime state channel linearity checking offers the following properties.</para>
<orderedlist numeration="arabic" continuation="restarts" spacing="normal">
<listitem><para>If a session endpoint implementation respects state channel linearity, then the generated API statically ensures freedom from the application errors outlined in <emphasis>&#x00A7;</emphasis> 13.1 (<emphasis>i.e.</emphasis>, <emphasis>communication safety</emphasis>, <emphasis>e.g.</emphasis>, [<link linkend="ch13_b4">4</link>, error-freedom]) when composed with conformant endpoints for the other roles in the protocol.</para></listitem>
<listitem><para>Regardless of state channel linearity, any statically well-typed endpoint implementation will never perform a message passing action that does not conform to the protocol.</para></listitem>
</orderedlist>
<para>These properties follow from the fact that the only way to violate the EFSM of the API, generated from a validated protocol, is to violate state channel linearity, in which case the API raises an exception <emphasis>without</emphasis> actually performing the offending I/O action. This hybrid form of session verification thus guarantees the absence of protocol violation errors during session execution up to premature termination, which is always a possibility in practice due to program errors outside of the immediate session code, or other failures, such as broken connections.</para>
<para>When following the endpoint implementation pattern promoted by a generated API, by associating session code to the <literal>MPSTEndpoint</literal>-try, the Java <literal>IOException</literal> of, <emphasis>e.g.</emphasis>, a broken connection will direct control flow out of the try, safely (w.r.t. session typing) avoiding further I/O actions in the failed session. Finer-grained treatment of session failures is a direction of ongoing development for Scribble (and MPST).</para>
</section>
<section class="lev2" id="sec13-2-4" label="13.2.4" xreflabel="13.2.4">
<title>Additional Math Service Endpoint Examples</title>
<para><emphasis role="strong">A first implementation of</emphasis> <literal>S</literal><emphasis role="strong">.</emphasis> <link linkend="fig13_9">Figure <xref linkend="fig13_9" remap="13.9"/></link> summarises the State Channel API generated for <literal>S</literal> in <literal>MathSvc</literal>. Unlike <literal>C</literal>, the EFSM for <literal>S</literal> features non-unary input states, which correspond at the process implementation level to the <emphasis>branch</emphasis> primitive of formal session calculi (<emphasis>e.g.</emphasis>, [<link linkend="ch13_b1">1</link>]). Java does not directly support a corresponding language construct, but API generation enables some different options.</para>
<para>One option, demonstrated here, is designed for standard Java <literal>switch</literal> patterns. For each branch state, a <emphasis>branch-specific</emphasis> <literal>enum</literal> is generated to enumerate the cases of the choice according to the source protocol. <emphasis>E.g.</emphasis>, for the initial state of <literal>S</literal>: <literal>enum</literal> <literal>MathSvc_S_1_Enum {</literal> <literal><emphasis>V</emphasis></literal><emphasis>al</emphasis>, <literal><emphasis>Bye</emphasis></literal> <literal>}</literal>.</para>
<fig id="fig13_9" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 13.9</label>
<caption><title>State Channel API generated for <literal>S</literal> in <literal>MathSvc</literal>; and an implementation of <literal>S</literal> using the generated API.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig077.jpg" mime-subtype="jpeg"/>
</fig>
<para>The channel class itself (<link linkend="fig13_9">Figure <xref linkend="fig13_9" remap="13.9"/></link>), <literal>MathSvc S 1</literal>, is generated with a single <literal>branch</literal> operation. This method blocks until a message is received, returning a new instance of the generated <literal>MathSvc S 1 Cases</literal> class, which holds the enum value corresponding to the received message in a final <literal>op</literal> field. Unfortunately, since the <emphasis>static</emphasis> type of the <literal>Cases</literal> object reflects the <emphasis>range</emphasis> of possible cases, the API requires the user to manually call the corresponding <literal>receive</literal> method of the <literal>Cases</literal> object, essentially as a form of cast to obtain the appropriately typed state channel.</para>
<para>Lines 11&#x2013;20 in <link linkend="fig13_9">Figure <xref linkend="fig13_9" remap="13.9"/></link> implement a <literal>switch</literal> on the <literal>op</literal> enum of <literal>MathSvc_S_1_Cases</literal>. The Java compiler is able to statically determine whether all enum cases are exhaustively handled. In each of the two cases (<literal>Bye</literal> and <literal>Val</literal>), the corresponding <literal>receive</literal>-cast is called on the <literal>Cases</literal> object to obtain the successor state channel of that (input) transition. Leveraging the hybrid verification approach, the generated API includes an implicit run-time check that the correct cast method is used following a <literal>branch</literal>; calling an incorrect method raises an exception.</para>
<para>&#x00A7; 13.4 discusses an alternative API generation that allows session branches to checked by Java typing <emphasis>without</emphasis> additional run-time checks.</para>
<para><emphasis role="strong">Alternative</emphasis> <literal>C</literal> <emphasis role="strong">factorial implementation.</emphasis> Following is an implementation of a factorial calculation using the <literal>C</literal> endpoint of <literal>MathSvc</literal> in a recursive method, illustrating the use of the State Channel API in an alternative programming style.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog183.jpg" mime-subtype="jpeg"/></para>
<para>Besides conformance to the protocol itself, the state channel parameter and return types help to ensure that the appropriate I/O transitions are performed through the protocol states in order to enact the recursive method call correctly.</para>
</section>
</section>
<section class="lev1" id="sec13-3" label="13.3" xreflabel="13.3">
<title>Real-World Case Study: HTTP (GET)</title>
<para>In this section, we apply the Scribble API generation methodology to a real-world protocol, HTTP/1.1 [<link linkend="ch13_b3">3</link>]. For the purposes of this tutorial, we limit this case study to the GET method of HTTP, and treat a minimal number of message fields required for interoperability with existing real-world clients and servers. The following implementations have been tested against Apache (as currently deployed by the dept. of computing, Imperial College London) and Firefox 5.0.1.</para>
<para>A key point illustrated by this experiment on using session types in practice is the interplay between <emphasis>data types</emphasis> (message structure) and <emphasis>session types</emphasis> (interaction structure) in a complete protocol specification. In particular, that aspects of the former can be refactored into the latter, while fully preserving protocol interoperability, to take advantage of the safety properties offered by Scribble-generated APIs in endpoint implementations.</para>
<section class="lev2" id="sec13-3-1" label="13.3.1" xreflabel="13.3.1">
<title>HTTP in Scribble: First Version</title>
<para>HTTP is well-known as a client-server request-response protocol, typically conducted over TCP. Despite its superficial simplicity, <emphasis>i.e.</emphasis>, a sequential exchange of just two messages between two parties, the standards documentation for HTTP spans several hundred pages, as is often the case for Internet applications and other real-world protocols.</para>
<para><emphasis role="strong">Global protocol.</emphasis> As a first version, we simply express the high-level notion of an HTTP request-response as follows:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog184.jpg" mime-subtype="jpeg"/></para>
<para>A small difference from the Scribble examples seen so far are the <literal>sig</literal> declarations for custom message formatting. Unlike <literal>type</literal> declarations, which pertain specifically to payload types, <literal>sig</literal> is used to work with host language-specific (<emphasis>e.g.</emphasis>, <literal>&lt;java&gt;</literal>) routines for arbitrary message formatting; <emphasis>e.g.</emphasis>, <literal>Req.java</literal> contains Java routines, provided as part of this protocol specification, for performing the serialization and deserialization between Java <literal>Req</literal> objects and the actual ASCII strings that constitute concrete HTTP requests on the wire.</para>
<para><emphasis role="strong">Client implementation.</emphasis> For such a simple specification, we omit the EFSMs and Endpoint APIs for each role, and directly give client code using the generated API (omitting the usual preamble):</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog185.jpg" mime-subtype="jpeg"/></para>
<para>The generated API prevents errors such as attempting to receive <literal>Resp</literal> before sending <literal>Req</literal> or sending multiple <literal>Req</literal>s. However, one may naturally wonder if this is &#8220;all there is&#8221; to a correct HTTP client implementation&#x2014; where is the complexity that is carefully detailed in the RFC specification? The answer lies in the message formatting code that we have conveniently abstracted as the <literal>Req</literal> and <literal>Res</literal> message classes. A basic HTTP session does exchange only two messages, but these messages are richly structured, involving branching, optional and recursive structures. In short, this first version <emphasis>assumes</emphasis> the correctness of the <literal>Req</literal> and <literal>Resp</literal> classes (written by the protocol author, or obtained using other parsing/formatting utilites) as part of the protocol specification.</para>
</section>
<section class="lev2" id="sec13-3-2" label="13.3.2" xreflabel="13.3.2">
<title>HTTP in Scribble: Revised</title>
<para>As defined in RFC 7230 [<link linkend="ch13_b3">3</link>] (<emphasis>&#x00A7;</emphasis> 3 onwards), the message grammar is:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog186.jpg" mime-subtype="jpeg"/></para>
<para>Intuitively, the act of sending a HTTP request may be equivalently understood as sending a request-line, followed by sending zero or more header-fields terminated by <literal>CRLF</literal>, and so on. Following this intuition, we can refactor much of this structure from the data side of the specification to the session types side, giving a Scribble description that captures the target protocol specification in more explicit detail than previously. Consequently, the generated API will promote the Java endpoint to respect this finer-grained protocol structure by static typing, as opposed to assuming the correctness of the supplied message classes.</para>
<para>We are able to refine the Scribble for HTTP/TCP in this way because any application-level notion of &#8220;message&#8221; identified in the specification is ultimately broken down and communicated via the TCP bit streams, in a manner that is transparent to the other party (client or server). This approach may thus be leveraged for any application protocol conducted over a transport with such characteristics.</para>
<para><emphasis role="strong">Global protocol.</emphasis> <link linkend="fig13_10">Figure <xref linkend="fig13_10" remap="13.10"/></link> is an extract of a revised Scribble specification of HTTP. The monolithic request and response messages have been decomposed into smaller constituents; <emphasis>e.g.</emphasis>, <literal>RequestL</literal> and <literal>Host</literal> respectively denote the request-line and host-field in a request. For the most part, the Java code for formatting each message fragment as an HTTP ASCII string is reduced to a simple print instruction with compliant white spacing built in (e.g, <literal>CRLF</literal>s). The structure by which these constituents should be composed to reform whole messages is now expressed in the <literal>Request</literal> and <literal>Response</literal> subprotocols.</para>
<fig id="fig13_10" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 13.10</label>
<caption><title>Extract from the revised specification of HTTP in Scribble.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig078.jpg" mime-subtype="jpeg"/>
</fig>
<para><emphasis role="strong">Client implementation.</emphasis> Taking the revised Scribble HTTP, Endpoint API generation proceeds as usual, generating the EFSMs for each role to give the structure of the State Channel API. Lines 2&#x2013;3 in <link linkend="fig13_11">Figure <xref linkend="fig13_11" remap="13.11"/></link> is an almost minimal implementation of a <emphasis>correctly formatted</emphasis> request according to the <literal>Request</literal> subprotocol. The typed API ensures the initial, mandatory <literal>RequestLine</literal>; then amongst the recursive choice cases we opt to send only the <literal>Host</literal> field, before concluding the request by an empty <literal>Body</literal>. A complete client implementation is given by: <literal>doResponse(doRequest(s1))</literal>.</para>
<para>Besides limiting to a subset of the protocol, this revision is by no means a complete specification of HTTP in terms of capturing the entire message grammar in full detail; the fidelity of the Scribble specification may be pushed further, perhaps towards a &#8220;character-perfect&#8221; specification, via suitably fine-grained message decomposition.</para>
<fig id="fig13_11" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 13.11</label>
<caption><title>Extract from an implementation of a HTTP client via API generation.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig079.jpg" mime-subtype="jpeg"/>
</fig>
</section>
</section>
<section class="lev1" id="sec13-4" label="13.4" xreflabel="13.4">
<title>Further Endpoint API Generation Features</title>
<para><emphasis role="strong">Branch-specific callback interfaces.</emphasis> Scribble also generates a <emphasis>callback-based API</emphasis> for branch states, which does <emphasis>not</emphasis> require additional run-time checks (cf. <emphasis>&#x00A7;</emphasis> 13.2.4). For each branch state, a <emphasis>handler</emphasis> interface is generated with a callback variant of <literal>receive</literal> for each choice case; <emphasis>e.g.</emphasis>, <literal>MathSvc_S_1_Handler</literal> in <link linkend="fig13_12">Figure <xref linkend="fig13_12" remap="13.12"/></link>. Apart from the operator and payloads, each method takes the continuation <emphasis>state channel</emphasis> as a parameter; the return type is <literal>void</literal>. Java typing ensures that a (concrete) implementation of this interface implicitly covers all cases of the branch. Finally, a variant of <literal>branch</literal> is generated in the parent channel class (<emphasis>e.g.</emphasis>, <literal>MathSvc S 1</literal>) that takes an instance of the corresponding handler interface, with return <literal>void</literal>. As before, this <literal>branch</literal> blocks until a message is received; the API then delegates the handling of the message to the appropriate callback method of the supplied handler object.</para>
<para><link linkend="fig13_12">Figure <xref linkend="fig13_12" remap="13.12"/></link> gives a class that implements the handler interfaces of <emphasis>both</emphasis> branch states for <literal>S.</literal> Assuming an <literal>MPSTEndpoint&lt;MathSvc, S&gt; serv</literal>, this handler class may be used in an event-driven implementation of <literal>S</literal> by: <literal>new</literal> <literal>MathSvc_S_1(serv).branch(C,</literal> <literal>new</literal> <literal>MathSHandler())</literal>.</para>
<para><emphasis role="strong">State-specific futures for unary inputs.</emphasis> For unary input states, Scribble additionally generates <emphasis>state-specific input futures</emphasis> as an alternative mechanism to the basic <literal>receive</literal>. For example, in the revised Scribble specification of HTTP (<link linkend="fig13_11">Figure <xref linkend="fig13_11" remap="13.11"/></link>), the channel class <literal>Http C 3</literal> corresponds to the state where <literal>C</literal> should receive the HTTP version element (<literal>HttpV</literal>) of the response status-line (Line 14 in <link linkend="fig13_10">Figure <xref linkend="fig13_10" remap="13.10"/></link>). For this state, Scribble generates the class <literal>Http C 3 Future</literal>. Its key elements are input-specific fields for the message type (<literal>msg</literal>) or payloads (<emphasis>e.g.</emphasis>, <literal>pay1</literal>) to be received, and a <literal>sync</literal> method to force the future. For the <literal>Http C 3</literal> channel class itself, the following variant of <literal>receive</literal> is generated:</para>
<fig id="fig13_12" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 13.12</label>
<caption><title>Additional branch callback interfaces generated for <literal>S</literal> in <literal>MathSvc</literal>; and a corresponding implementation of <literal>S</literal>.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig080.jpg" mime-subtype="jpeg"/>
</fig>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/e306_01.jpg" mime-subtype="jpeg"/></para>
<para>Unlike a basic <literal>receive</literal>, calling <literal>async</literal> returns immediately with a new instance of <literal>Http C 3 Future</literal> in the supplied <literal>Buf</literal>.</para>
<para>Calling <literal>sync</literal> first implicitly forces all pending prior futures, in order, for the same peer role. It then blocks the caller until the expected message is received, and writes the values to the generated fields of the future. This safely preserves the FIFO messaging semantics between each pair of roles in a session, so that endpoint implementations using generated futures retain the same safety properties as using only blocking receives. Repeat forcing of an input future has no effect.</para>
<para>An example usage of <literal>async</literal> was given in <link linkend="fig13_11">Figure <xref linkend="fig13_11" remap="13.11"/></link> (line 6). There, the <literal>async</literal> is used to safely affect a <emphasis>non-blocking input</emphasis> action (the client is not interested in blocking on awaiting just the <literal>HttpV</literal> portion of the response). Since the <literal>HttpV</literal> future is never explicitly forced &#x2013; unlike state channels, input-futures are not linear objects &#x2013; <literal>async</literal> also affects a <emphasis>user</emphasis>-level form of <emphasis>affine</emphasis> input action, in the sense that the user never reads this message. Finally, <literal>async</literal> enables postponing input actions until later in a session, for safe user-level <emphasis>permutation</emphasis> of session I/O actions.</para>
</section>
<section class="lev1" id="sec2a">
<title>References</title>
<orderedlist numeration="arabic" continuation="restarts" spacing="normal">
<listitem><para><anchor id="ch13_b1"/>M. Coppo, M. Dezani-Ciancaglini, N. Yoshida, and L. Padovani. Global progress for dynamically interleaved multiparty sessions. <emphasis>Mathematical Structures in Computer Science</emphasis>, 760:1&#x2013;65, 2015. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=M%2E+Coppo%2C+M%2E+Dezani-Ciancaglini%2C+N%2E+Yoshida%2C+and+L%2E+Padovani%2E+Global+progress+for+dynamically+interleaved+multiparty+sessions%2E+Mathematical+Structures+in+Computer+Science%2C+760%3A1-65%2C+2015%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch13_b2"/>P.-M. Deni&#x00E9;lou and N. Yoshida. Multiparty session types meet communicating automata. In <emphasis>ESOP &#x2019;12</emphasis>, volume 7211 of <emphasis>LNCS</emphasis>, pages 194&#x2013;213. Springer, 2012. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=P%2E-M%2E+Deni%E9lou+and+N%2E+Yoshida%2E+Multiparty+session+types+meet+communicating+automata%2E+In+ESOP+%2712%2C+volume+7211+of+LNCS%2C+pages+194-213%2E+Springer%2C+2012%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch13_b3"/>R. Fielding, Y. Lafon, M. Nottingham, and J. Reschke. IETF RFCs 7230&#x2013;7235 Hypertext Transfer Protocol 1.1. <literal>https://tools.ietf.org/html/rfc7230</literal></para></listitem>
<listitem><para><anchor id="ch13_b4"/>K. Honda, N. Yoshida, and M. Carbone. Multiparty asynchronous session types. In <emphasis>POPL &#x2019;08</emphasis>, pages 273&#x2013;284. ACM, 2008. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=K%2E+Honda%2C+N%2E+Yoshida%2C+and+M%2E+Carbone%2E+Multiparty+asynchronous+session+types%2E+In+POPL+%2708%2C+pages+273-284%2E+ACM%2C+2008%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch13_b5"/>R. Hu. Demo files for this BETTY tutorial chapter. <literal>https://github.com/scribble/scribble-java/tree/master/modules/demos/scrib/bettybook</literal></para></listitem>
<listitem><para><anchor id="ch13_b6"/>R. Hu and N. Yoshida. Hybrid session verification through endpoint API generation. In <emphasis>FASE &#x2019;16</emphasis>, volume 9633 of <emphasis>LNCS</emphasis>, pages 401&#x2013;418. Springer, 2016. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=R%2E+Hu+and+N%2E+Yoshida%2E+Hybrid+session+verification+through+endpoint+API+generation%2E+In+FASE+%2716%2C+volume+9633+of+LNCS%2C+pages+401-418%2E+Springer%2C+2016%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch13_b7"/>J. Lange, E. Tuosto, and N. Yoshida. From communicating machines to graphical choreographies. In <emphasis>POPL &#x2019;15</emphasis>, pages 221&#x2013;232. ACM Press, 2015. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=J%2E+Lange%2C+E%2E+Tuosto%2C+and+N%2E+Yoshida%2E+From+communicating+machines+to+graphical+choreographies%2E+In+POPL+%2715%2C+pages+221-232%2E+ACM+Press%2C+2015%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch13_b8"/>OMG UML 2.5 specification. <literal>http://www.omg.org/spec/UML/2.5</literal></para></listitem>
<listitem><para><anchor id="ch13_b9"/>Scribble homepage. <literal>http://www.scribble.org</literal></para></listitem>
<listitem><para><anchor id="ch13_b10"/>Scribble GitHub repository. <literal>https://github.com/scribble/scri bble-java</literal></para></listitem>
</orderedlist>
</section>
</chapter>
<chapter class="chapter" id="ch14" label="14" xreflabel="14">
<title>Mungo and StMungo: Tools for Typechecking Protocols in Java</title>
<para><emphasis role="strong">Ornela Dardha</emphasis><footnote id="fn14_1a" label="1"><para>School of Computing Science, University of Glasgow, UK</para></footnote><emphasis role="strong">, Simon J. Gay</emphasis><footnoteref linkend="fn14_1a" label="1"/><emphasis role="strong">, Dimitrios Kouzapas</emphasis><footnoteref linkend="fn14_1a" label="1"/><emphasis role="strong">, Roly Perera</emphasis><footnoteref linkend="fn14_1a" label="1"/>, <footnote id="fn14_2a" label="2"><para>School of Informatics, University of Edinburgh, UK</para></footnote>, <emphasis role="strong">A. Laura Voinea</emphasis><footnoteref linkend="fn14_1a" label="1"/> <emphasis role="strong">and Florian Weber</emphasis><footnoteref linkend="fn14_1a" label="1"/></para>
<section class="lev2">
<title>Abstract</title>
<para>We present two tools that support static typechecking of communication protocols in Java. Mungo associates Java classes with typestate specifications, which are state machines defining permitted sequences of method calls. StMungo translates a communication protocol specified in the Scribble protocol description language into a typestate specification for each role in the protocol by following the message sequence. Role implementations can be typechecked by Mungo to ensure that they satisfy their protocols, and then compiled as usual with <literal>javac</literal>. We demonstrate the Scribble, StMungo and Mungo toolchain via a typechecked POP3 client that can communicate with a real-world POP3 server.</para>
</section>
<section class="lev1" id="sec14-1" label="14.1" xreflabel="14.1">
<title>Introduction</title>
<para>Modern computing is dominated by communication, at every level from manycore architectures through multithreaded programs to large-scale distributed systems; this contrasts with the original emphasis on data processing. Early recognition of the importance of structured data meant that high-level programming languages have always incorporated data types and supported programmers through the techniques of static and dynamic typechecking. The foundational status of structured data was explicitly recognised in the title of Wirth&#x2019;s classic 1976 text <emphasis>Algorithms + Data Structures = Programs</emphasis>, but a more appropriate modern slogan would be <emphasis>Programs + Communication Structures = Systems</emphasis>. The new reality of communication-based software development needs to be supported by programming tools based on structuring principles and high-level abstractions. Given the success of data types, it is natural to apply type-theoretic techniques to the specification and verification of communication-based code. During the last twenty years, this goal has been pursued by the expanding and increasingly active research community on session types [<link linkend="ch14_b12">12</link>, <link linkend="ch14_b13">13</link>, <link linkend="ch14_b24">24</link>]. A session type is a formal structured description of a communication protocol, specifying the type, sequence and direction of messages. By embedding this description in the type system of a programming language, adherence to the protocol can be verified by static typechecking; if desired, dynamic monitoring can be introduced into the runtime system.</para>
<para>Several researchers have worked towards making typechecked communication structures available for mainstream software development, by transferring session types from their original setting of pi-calculus to functional and object-oriented languages [<link linkend="ch14_b3">3</link>, <link linkend="ch14_b5">5</link>&#x2013;<link linkend="ch14_b8">8</link>, <link linkend="ch14_b15">15</link>, <link linkend="ch14_b17">17</link>, <link linkend="ch14_b19">19</link>]. Gay <emphasis>et al.</emphasis> [<link linkend="ch14_b9">9</link>] proposed an integration of session types and object-oriented programming through the concept of typestates [<link linkend="ch14_b22">22</link>], in which methods are constrained to be called only in particular sequences. They defined a translation from the session type of a communication channel endpoint into a typestate specification that constrains the use of send and receive methods on an object representing the channel endpoint. Their notation for typestate specifications was inspired by the syntax of session types.</para>
<para>Dardha, Gay, Kouzapas and Perera extended that work and implemented it as Mungo [<link linkend="ch14_b16">16</link>], a front-end typechecking tool for Java. They also generalised the translation from session types to typestate specifications, so that it handles multiparty [<link linkend="ch14_b11">11</link>] instead of binary session types, and made it concrete by implementing StMungo [<link linkend="ch14_b16">16</link>], a translator from the Scribble [<link linkend="ch14_b20">20</link>, <link linkend="ch14_b25">25</link>] protocol description language into Mungo specifications. The Scribble description of a protocol is translated into an API with which to program implementations of protocol roles; the typestate specification associated with the API permits static checking of the correctness of the implementation of a role. Typestate specifications do not represent the notion of duality of session types; compatibility between roles depends on the assumption that their typestate specifications are derived from a single global session type. The paper by Kouzapas <emphasis>et al.</emphasis> [<link linkend="ch14_b16">16</link>] illustrated the use of Mungo and StMungo with a substantial case study of an SMTP client [<link linkend="ch14_b21">21</link>], including the low-level implementation details necessary to enable communication with standard SMTP servers. This achieved the long-standing goal of using session types to specify and verify implementations of real internet protocols.</para>
<para>The present chapter describes Mungo and StMungo in relation to three examples. The first, in Section 14.2.1, illustrates Mungo by defining and checking a typestate specification for an iterator. The second, in Section 14.3, is a simple multiparty scenario based on a travel agency. Finally, in Section 14.4, we show how Mungo and StMungo can be used to typecheck a client for the POP3 protocol [<link linkend="ch14_b18">18</link>].</para>
</section>
<section class="lev1" id="sec14-2" label="14.2" xreflabel="14.2">
<title>Mungo: Typestate Checking for Java</title>
<para>Mungo is a static analysis tool that checks typestate properties in Java programs. Mungo implements two main components. The first is a Java-like syntax to define typestate specifications for classes, and the second is a typechecker that checks whether objects that have typestate specifications are used correctly. Mungo typechecks standard Java code without syntactic extensions; typestate specifications are defined in separate files and are associated with Java classes by means of the Java annotation mechanism. After typechecking with Mungo, programs can be compiled and run using standard Java tools. The declaration of a typestate specification in a single file contrasts with other approaches that take the viewpoint of typestate as pre- and postconditions on methods; we discuss this point in Section 14.5. If a class has a typestate specification, the Mungo typechecker analyses each variable of that class in the program and extracts the method call behaviour (sequences of method calls) through the variable&#x2019;s life. Finally, it checks the extracted information against the sequences of method calls allowed by the typestate specification.</para>
<para>Mungo is implemented in the JastAdd [<link linkend="ch14_b10">10</link>] framework, which is a Reference Attribute Grammar (RAG) meta-compiler suite compatible with Java. JastAdd provides a Java parser and typechecker, and was also used to implement a parser for the typestate specification language.</para>
<para>Mungo supports typechecking for a subset of Java. The programmer can define classes with typestate specifications and classes without them. The typechecking procedure tracks variables storing instances of classes with typestate specifications, through argument passing and return values. Moreover, the typechecking procedure for the fields of a class follows the typestate specification of the class to infer a typestate usage for the fields. For this reason fields that have typestate specifications must be defined in a class that also has a typestate specification.</para>
<para>Mungo first runs the Java typechecker provided by the JastAdd framework. If there are no errors then Mungo performs additional well-formedness checks before it runs the typestate checking procedure. First, the tool checks for well-formed typestate specifications: they must be deterministic and all states must be reachable from the initial state. Second, it checks that a class with a typestate specification implements all the methods required in the typestate. Third, arrays cannot store objects that have typestates, because array access, and thus inference for objects that are stored in an array, cannot be determined at statically. Finally, fields with typestate specifications must be private and non-static, to disallow external interference with their state.</para>
<para>Completing the coverage of Java will require further work. Some features we anticipate to be relatively straightforward extensions, such as synchronised statements, the conditional operator ?:, inner and anonymous classes, and static initialisers. Generics, inheritance and exceptions are non-trivial. Currently, generics are not supported, while inheritance is supported for classes without associated typestate behaviour. Exceptions are supported syntactically but are type-checked under the (unsound) assumption that no exceptions are thrown; a <literal>try{...} catch(Exception e) {...}</literal> statement is typechecked by typechecking the <literal>try</literal> block but not the <literal>catch</literal> block. If the program does not throw exceptions then there will be no violations of typestate specifications, but exception handlers may violate typestates.</para>
<section class="lev2" id="sec14-2-1" label="14.2.1" xreflabel="14.2.1">
<title>Example: Iterator</title>
<para>We introduce some of the features of Mungo through an example that enforces correct usage of a Java Iterator. The example shows how a programmer can define an API and associate it with a typestate specification in order to constrain the order in which methods can be called. In the code below we define class <literal>StateIterator</literal> to wrap a Java Iterator. We use the Java annotation syntax <literal>@Typestate("StateIteratorProtocol")</literal> to associate the class <literal>StateIterator</literal> with the typestate specification <literal>StateIteratorProtocol</literal>. We often refer to a typestate specification as a protocol, following the established terminology of &#8220;object protocol&#8221; in the typestate literature.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog187.jpg" mime-subtype="jpeg"/></para>
<para>We assume that the underlying implementation of the Java Iterator includes the <literal>remove()</literal> method. The implementation of method <literal>hasNext()</literal> uses the Iterator to discover whether the underlying collection has more elements. It assumes the definition of the enumeration</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog188.jpg" mime-subtype="jpeg"/></para>
<para>which is provided as part of the Mungo framework. This enumeration is used to specify dependency of the protocol on the result of a method.</para>
<para>Overall, the <literal>StateIteratorProtocol</literal> protocol ensures that the Java Iterator will be used in a way that throws no exceptions (method <literal>next()</literal> throws <literal>NoSuchElementException</literal> when there are no more elements in the underlying collection, and method <literal>remove()</literal> throws <literal>IllegalStateException</literal> when there is no element to removed). The code below defines the typestate specification <literal>StateIteratorProtocol</literal>.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog189.jpg" mime-subtype="jpeg"/></para>
<para>A new iterator object is in state <literal>HasNext</literal>, because that is the first state in the definition. The only method available is <literal>hasNext()</literal>. If method <literal>next()</literal> were available then <literal>NoSuchElementException</literal> might be thrown in the case where there are no (more) elements in the underlying collection. Similarly, the availability of method <literal>remove()</literal> might result in <literal>IllegalStateException</literal>. A call of method <literal>hasNext()</literal> means that the continuation of the protocol depends on the return value of the method. In the case of <literal>False</literal> no further interaction with the iterator is possible, thus preventing possible exceptions. If the value <literal>True</literal> is returned then the state changes to <literal>Next</literal>, which forces the programmer to call the <literal>next()</literal> method and proceed to state <literal>HasNextOrRemove</literal>. Method <literal>remove()</literal> is not available because it should only be called after <literal>next()</literal> in order to remove the element returned by <literal>next()</literal>. Method <literal>hasNext()</literal> is not available because calling it would be redundant.</para>
<para>The state <literal>HasNextOrRemove</literal> offers a choice between methods <literal>remove</literal> and <literal>hasNext()</literal>. In the former case the iterator removes the current object and proceeds to the <literal>HasNext</literal> state. Alternatively, calling <literal>hasNext()</literal> either proceeds to state <literal>NextOrRemove</literal> or ends the protocol otherwise. In state <literal>NextOrRemove</literal> there is still the possibility of removing the last returned object and proceeding to the <literal>Next</literal> state (this is because a poll has already been done), or getting the next element of the collection using method <literal>next()</literal> and proceeding to the <literal>HasNextOrRemove</literal> state.</para>
<para>To summarise, if we assume semantic correctness of the methods of <literal>iter</literal> (for example, that <literal>iter.hasNext()</literal> correctly reports the state of <literal>iter</literal>), then by using Mungo to typecheck code that uses a <literal>StateIterator</literal>, we can ensure that <literal>NoSuchElementException</literal> and <literal>IllegalStateException</literal> will not occur. Specifically, we guarantee: i) not calling the <literal>next()</literal> method on an empty collection; ii) not calling the <literal>remove()</literal> when there is no element to remove from the underlying collection; iii) additionally, not having redundant calls of the <literal>hasNext()</literal> method.</para>
<para>To avoid conflicting state changes, objects with typestates must not be aliased. Mungo uses linear typing to prevent aliasing.</para>
<para>The code below, which is well-typed according to Mungo, creates and uses a <literal>StateIterator</literal> object. It creates a <literal>HashSet</literal> containing the positive integers smaller than 32, and then removes the even numbers.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog190.jpg" mime-subtype="jpeg"/></para>
<para>The <literal>HashSet</literal>&#x2019;s iterator is wrapped in a <literal>StateIterator</literal> object, which is subsequently used according to its protocol. The loop structure in the protocol is matched by the pattern <literal>label: do { ... } while(true);</literal> together with the <literal>continue label;</literal> and <literal>break label;</literal> statements. The <literal>switch</literal> statement handles the possible results of <literal>hasNext()</literal>, controlling the continuation or termination of the loop. The code on line 9 chooses whether or not to call <literal>remove()</literal>; the state here is <literal>HasNextOrRemove</literal>.</para>
</section>
</section>
<section class="lev1" id="sec14-3" label="14.3" xreflabel="14.3">
<title>StMungo: Typestates from Communication Protocols</title>
<para>StMungo (Scribble to Mungo) is a transpiler from Scribble to Java, which also generates Mungo typestate specifications. It is based on the integration of session types and typestates [<link linkend="ch14_b9">9</link>] which consists of a formal translation of session types for communication channels into typestate specifications for channel objects. The latter define the order in which the methods of the channel objects can be called. This specification of the permitted sequences of method calls is naturally viewed as a channel protocol. We take a step further: we extend this formal translation from binary to multiparty session types [<link linkend="ch14_b11">11</link>] and implement it as StMungo, which translates Scribble local protocols into typestate specifications and prototype implementation code based on TCP/IP sockets. After refinement, the implementation is typechecked using Mungo.</para>
<para>A Scribble local protocol describes the communication between one role and all the other participants in a multiparty scenario, including the way in which messages sent to different participants are interleaved. StMungo is based on the principle that each role in the multiparty communication can be abstracted as a Java class following the typestate corresponding to the role&#x2019;s local protocol. The typestate specification generated by StMungo, together with the Mungo typechecker, guide the programmer in the design and implementation of distributed multiparty communication-based programs with guarantees of communication safety and soundness. StMungo is the first tool to provide a practical embedding of multiparty session type protocols into object-oriented languages with typestate specifications.</para>
<para>The diagram shows how the toolchain consisting of Scribble, StMungo and Mungo is used to generate a Java program from a Scribble protocol.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig081.jpg" mime-subtype="jpeg"/></para>
<para>We start with a global protocol written in Scribble, which is then validated and projected into local protocols, one for each role specified in the global protocol. At this point we run StMungo on the local projections for which we want to generate a typestate. The tool generates a typestate specification, a Java API and a prototype main program. After completing the main program, typechecking with Mungo verifies that it correctly implements the protocol.</para>
<section class="lev2" id="sec14-3-1" label="14.3.1" xreflabel="14.3.1">
<title>Example: Travel Agency</title>
<para>We now illustrate the toolchain of Scribble, StMungo and Mungo by means of a travel agency example, which models the process of booking a flight through a university travel agent.</para>
<para>Three participants are involved: Researcher (abbreviated <literal>R</literal>), who intends to travel; Agent (<literal>A</literal>), who is able to make travel reservations; and Finance (<literal>F</literal>), who approves expenditure from the budget. In the Scribble [<link linkend="ch14_b25">25</link>] language, we first define the global protocol among three <emphasis>roles</emphasis>, which are abstract representations of the participants. The protocol consists of sequences of interactions. Every message (e.g. <literal>request</literal>) can be associated with a payload type (e.g. <literal>Travel</literal>), a sender, and one or more receivers. Typically payload types are structured data types defined separately from the protocol specification.</para>
<para>In the global protocol, after the <literal>check</literal> message requesting authorisation for a trip, <literal>F</literal> can choose to <literal>approve</literal> or <literal>refuse</literal> the request.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog191.jpg" mime-subtype="jpeg"/></para>
<para>The Scribble tools can be used to validate the protocol definition and to derive a <emphasis>local</emphasis> version of the protocol for each role, according to the theory of multiparty session types [<link linkend="ch14_b11">11</link>]. This is known as <emphasis>endpoint projection</emphasis>. Here is the projection for <literal>R</literal>, which describes only the messages involving that role. The <literal>self</literal> keyword indicates that <literal>R</literal> is the local endpoint.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog192.jpg" mime-subtype="jpeg"/></para>
<para>Notice that the exchange of <literal>invoice</literal> and <literal>payment</literal> between <literal>A</literal> and <literal>F</literal> is not included. Similarly, the local projection for <literal>A</literal> omits the <literal>check</literal> message; we omit its local projection. Finally, the local projection for <literal>F</literal> omits the <literal>request</literal>, <literal>quote</literal> and <literal>ticket</literal> messages.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog193.jpg" mime-subtype="jpeg"/></para>
<para>The common theme between protocols and typestate specifications is the requirement to do operations in particular orders. Our methodology for implementing the roles in a Scribble protocol is to define a Java class that encapsulates socket connections to provide the necessary communication, and provides methods that send and receive the messages in the protocol. This class constitutes an API for role programming. To ensure that communication methods are called in the order required by the protocol, we associate a typestate specification with the API, so that Mungo can check the correctness of code that uses the API. StMungo generates a Java API and a Mungo specification. If we are implementing all of the endpoints in a system, then the generated APIs are immediately interoperable with each other. However, interoperability with pre-existing endpoints such as a POP3 server (Section 14.4) typically requires an extra layer in order to translate between the abstract message labels defined in Scribble and the detailed textual message formats required by the protocol.</para>
<para>For the <literal>R</literal> role, StMungo converts the <literal>BuyTicket_R</literal> local projection into the following Mungo definitions:</para>
<orderedlist numeration="arabic" continuation="restarts" spacing="normal">
<listitem><para><literal>RProtocol</literal>, a typestate specification capturing the interactions local to the <literal>R</literal> role.</para></listitem>
<listitem><para><literal>RRole</literal>, a Java class that implements <literal>RProtocol</literal> by communication over Java sockets. This is an API that can be used to implement the <literal>R</literal> endpoint.</para></listitem>
<listitem><para><literal>RMain</literal>, a prototype Java implementation of the <literal>R</literal> endpoint. This runs as a Java process, and provides a <literal>main()</literal> method which uses <literal>RRole</literal> to communicate with the other parties in the session. For testing purposes it provides a command-line interface to choose and display message parameters.</para></listitem>
</orderedlist>
<para>To complete the ticket buying example, we now describe the result of translating the local protocol for <literal>R</literal>. For each choice there is an enumerated type, named according to the numerical position of the choice in the sequence of choices within the local protocol. The values of the enumerated type are the names of the first message in each branch of the choice. For the choice in <literal>BuyTicket_R</literal> we have the following definition.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog194.jpg" mime-subtype="jpeg"/></para>
<para>Every role involved in the choice will have an enumerated type with the same set of values, but the names of the types are not necessarily the same for every role.</para>
<para>The typestate specification <literal>RProtocol</literal> defines the allowed sequences of method calls. As it includes method headers, it also provides similar documentation to an interface. The initial state is the first one defined.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog195.jpg" mime-subtype="jpeg"/></para>
<para>The API is defined by the class <literal>RRole</literal>, which is also generated. When instantiated, it establishes socket connections to the other role objects in the session (<literal>ARole</literal> and <literal>FRole</literal>); we omit the details here.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog196.jpg" mime-subtype="jpeg"/></para>
<para>The <literal>RMain</literal> class provides a prototype implementation of the <literal>R</literal> endpoint, using the <literal>RRole</literal> class to communicate with the other roles in the system. Mungo statically checks the correctness of an <literal>R</literal> implementation (either based on the prototype or written separately), by checking that methods are called in allowed sequences and that all possible responses are handled. For example, <literal>main</literal> below is correct.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog197.jpg" mime-subtype="jpeg"/></para>
<para>This code is checked by computing the sequences of method calls that are made on an <literal>RRole</literal> object, inferring the minimal typestate specification that allows those sequences, and then comparing this specification with the declared specification <literal>RProtocol</literal>. The comparison is based on a simulation relation. Typically the programmer would modify the prototype implementation by defining extra business logic, but she is also free to rewrite it completely. Mungo statically checks <literal>RMain</literal>, or any client of the <literal>RRole</literal> class, to ensure that methods of the protocol are called in a valid sequence and that all possible responses are handled.</para>
</section>
</section>
<section class="lev1" id="sec14-4" label="14.4" xreflabel="14.4">
<title>POP3: Typechecking an Internet Protocol Client</title>
<para>As a more substantial example, we use a standard internet protocol, POP3 [<link linkend="ch14_b18">18</link>] (Post Office Protocol Version 3), to show the applicability of session types in the real world and the use of session type tools to typecheck protocols. The protocol allows an email client to retrieve messages from a server. The diagram below is based on RFC 1939 [<link linkend="ch14_b18">18</link>], the official specification of the protocol. The labels &#8220;+OK&#8221; and &#8220;-ERR&#8221; are part of the textual message format. For simplicity, several transitions from state <literal>TRANSACTION</literal> have been omitted.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig082.jpg" mime-subtype="jpeg"/></para>
<para>The protocol starts with the client connecting to the server and the server authenticating the connection. The client then has the choice to either submit a username to log into a mailbox, or to end the authorization. Upon receiving the username, the server has the choice to accept the username or to send an error message to the client, for example if the username does not exist. After the username has been accepted, the client is then required to send a password or to end the authorization. If the password is accepted, the transaction stage begins. In the transaction stage, the client has a choice of various commands: the diagram shows just <literal>STAT</literal> (status) and <literal>LIST</literal> (summary list). Some of these requests involve a choice at the server side to either fulfil the request or to send an error message.</para>
<para>Alternatively the client can choose to <literal>QUIT</literal>. The specification of the messages and state transitions of POP3 can be converted into a Scribble global protocol, as shown below.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog198.jpg" mime-subtype="jpeg"/></para>
<para>Projection using the Scribble tools produces local protocols for the client and the server. For the rest of this section we focus on the client protocol. For brevity we omit the authentication phase.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog199.jpg" mime-subtype="jpeg"/></para>
<para>We use StMungo to translate the Scribble local protocol into a typestate specification <literal>CProtocol</literal>, which defines the order in which the communication methods are called.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog200.jpg" mime-subtype="jpeg"/></para>
<section class="lev2" id="sec14-4-1" label="14.4.1" xreflabel="14.4.1">
<title>Challenges of Using Mungo and StMungo in the Real World</title>
<para><emphasis role="strong">Programming with loops</emphasis> A POP3 server responds to the <literal>LIST</literal> command by sending any number of lines, terminated by the <literal>DOT</literal> message.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig083.jpg" mime-subtype="jpeg"/></para>
<para>The Scribble description of the state reached by <literal>+OK()</literal> uses explicit recursion in which <literal>continue</literal> jumps to a named state.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog201.jpg" mime-subtype="jpeg"/></para>
<para>The Java code generated by StMungo is a direct translation, using labelled statements and <literal>continue</literal>. Given that we are generating imperative code rather than recursive functions, this seems to be the only systematic way to handle the arbitrary structure of Scribble&#x2019;s <literal>rec</literal>. Although <literal>continue</literal> is a <literal>goto</literal>, its use is controlled and checked by Mungo: it is only allowed when the recursion point in the protocol has been reached. The SJ language [<link linkend="ch14_b15">15</link>] introduces <literal>sendWhile</literal> and <literal>receiveWhile</literal> loops to match particular protocol patterns, but we have chosen not to extend Java with new loop constructs.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog202.jpg" mime-subtype="jpeg"/></para>
<para><emphasis role="strong">Abstract vs. concrete messages</emphasis> When designing a complete system and implementing all the roles, StMungo can generate concrete textual messages in a uniform way; alternatively, we could use a structured message format such as JSON. However, in POP3 and other standard protocols, the client has to work with the textual message formats defined by the protocol. For example, the Scribble message <literal>OK(int, int) from S to C;</literal> corresponds to a line of text such as <literal>+OK 2 200</literal>. In the current implementation of the POP3 example, conversion between abstract and concrete messages is done by handwritten code, but we are working on a tool to generate message converters from a specification.</para>
<para><emphasis role="strong">Naming</emphasis> StMungo converts Scribble message names into Java method names. The method definition depends on whether or not the message appears at the beginning of a Scribble <literal>choice</literal>, and this cause naming conflicts if the same name is used for messages in both kinds of position. For example, <literal>OK</literal> and <literal>OKN</literal> in POP3 would more naturally both be <literal>OK</literal>.</para>
<para><emphasis role="strong">Non-standard implementations</emphasis> Real-world servers do not always follow the RFC exactly. The specification of POP3 states that if the client sends an unknown username, it is rejected and the username must be sent again. However, the server used for this case study, namely <literal>GMX.co.uk</literal>, accepts an unknown username and expects the client to send the password again. Consequently, even after completing the prototype client generated by StMungo and checking it with Mungo, it is necessary to test the client thoroughly with existing servers if we want to ensure correct operation in all cases. When deviations from the RFC are discovered, the Scribble definition of the protocol can be generalised accordingly. This problem could be reduced by promoting the use of formal protocol descriptions within RFCs.</para>
</section>
</section>
<section class="lev1" id="sec14-5" label="14.5" xreflabel="14.5">
<title>Related Work</title>
<para><emphasis role="strong">Session types.</emphasis> The main pieces of related work on session types and Java are the Session Java (SJ) language [<link linkend="ch14_b15">15</link>] and the API generation approach [<link linkend="ch14_b14">14</link>], bothbyHu <emphasis>et al.</emphasis> The API generation approach has been used to to analyse an SMTP client in Java. The API for SMTP implements multiparty session types using a pattern in which each communication method returns the receiver object with a new type that determines which communication methods are available at the next step. Standard Java typechecking can verify the correctness of communication when the pattern is used properly, with runtime monitoring being used to ensure linearity constraints are fulfilled. In contrast with this approach, Mungo&#x2019;s approach is completely static.</para>
<para>SJ [<link linkend="ch14_b15">15</link>] builds on earlier work [<link linkend="ch14_b4">4</link>, <link linkend="ch14_b5">5</link>, <link linkend="ch14_b7">7</link>] to add binary session type channels to Java. SJ implements a library for binary sessions that have a pre-defined interface. The syntax of Java is extended with communication statements to allow typechecking. The scope of a session is restricted to the body of a single method. Mungo removes this restriction by allowing the abstraction of multiparty session types as user-defined objects that can be passed and used throughout different program scopes.</para>
<para><emphasis role="strong">Typestates.</emphasis> There have been many projects that add typestates to practical languages, since the introduction of the concept by Strom and Yemini [<link linkend="ch14_b22">22</link>]. Plural [<link linkend="ch14_b2">2</link>] is a noteworthy example. It is based on Java and has been used to study access control systems. Plural implements typestates by using annotations to define pre- and post-conditions on methods, referring to abstract states and predicates on instance variables. By contrast, Mungo explicitly defines the possible sequences of method calls. Plural and Mungo both allow the typestate after a method call to depend on the return value.</para>
<para>Plaid [<link linkend="ch14_b1">1</link>, <link linkend="ch14_b23">23</link>] introduces typestate-oriented programming as a paradigm. Instead of class definitions, a program consists of state definitions containing methods that cause transitions to other states. Transitions are specified in a similar way to Plural&#x2019;s pre- and post-conditions. Similarly to classes, states can be structured into an inheritance hierarchy. As opposed to Plaid, Mungo focuses on the object-oriented paradigm in order to be applicable to Java.</para>
<para>Our previous paper [<link linkend="ch14_b16">16</link>] discusses related work in more detail.</para>
<para><emphasis role="strong">Acknowledgements</emphasis> This research was supported by the UK EPSRC project &#8220;From Data Types to Session Types: A Basis for Concurrency and Distribution&#8221; (EP/K034413/1) and by COST Action IC1201 &#8220;Behavioural Types for Reliable Large-Scale Software Systems&#8221;. We thank the reviewers for their detailed comments.</para>
</section>
<section class="lev1" id="sec2a">
<title>References</title>
<orderedlist numeration="arabic" continuation="restarts" spacing="normal">
<listitem><para><anchor id="ch14_b1"/>J. Aldrich, J. Sunshine, D. Saini, and Z. Sparks. Typestate-oriented programming. In <emphasis>OOPSLA &#x2019;09</emphasis>, pages 1015&#x2013;1022. ACM Press, 2009. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=J%2E+Aldrich%2C+J%2E+Sunshine%2C+D%2E+Saini%2C+and+Z%2E+Sparks%2E+Typestate-oriented+programming%2E+In+OOPSLA+%2709%2C+pages+1015-1022%2E+ACM+Press%2C+2009%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch14_b2"/>K. Bierhoff, N. E. Beckman, and J. Aldrich. Practical API protocol checking with access permissions. In <emphasis>ECOOP &#x2019;09</emphasis>, volume 5653 of <emphasis>Springer LNCS</emphasis>, pages 195&#x2013;219, 2009. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=K%2E+Bierhoff%2C+N%2E+E%2E+Beckman%2C+and+J%2E+Aldrich%2E+Practical+API+protocol+checking+with+access+permissions%2E+In+ECOOP+%2709%2C+volume+5653+of+Springer+LNCS%2C+pages+195-219%2C+2009%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch14_b3"/>S. Capecchi, M. Coppo, M. Dezani-Ciancaglini, S. Drossopoulou, and E. Giachino. Amalgamating sessions and methods in object-oriented languages with generics. <emphasis>Theoret. Comp. Sci.</emphasis>, 410:142&#x2013;167, 2009. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=S%2E+Capecchi%2C+M%2E+Coppo%2C+M%2E+Dezani-Ciancaglini%2C+S%2E+Drossopoulou%2C+and+E%2E+Giachino%2E+Amalgamating+sessions+and+methods+in+object-oriented+languages+with+generics%2E+Theoret%2E+Comp%2E+Sci%2E%2C+410%3A142-167%2C+2009%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch14_b4"/>M. Dezani-Ciancaglini, S. Drossopoulou, D. Mostrous, and N. Yoshida. Objects and session types. <emphasis>Information and Computation</emphasis>, 207(5): 595&#x2013;641, 2009. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=M%2E+Dezani-Ciancaglini%2C+S%2E+Drossopoulou%2C+D%2E+Mostrous%2C+and+N%2E+Yoshida%2E+Objects+and+session+types%2E+Information+and+Computation%2C+207%285%29%3A+595-641%2C+2009%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch14_b5"/>M. Dezani-Ciancaglini, E. Giachino, S. Drossopoulou, and N. Yoshida. Bounded session types for object oriented languages. In <emphasis>FMCO &#x2019;06</emphasis>, volume 4709 of <emphasis>Springer LNCS</emphasis>, pages 207&#x2013;245, 2006. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=M%2E+Dezani-Ciancaglini%2C+E%2E+Giachino%2C+S%2E+Drossopoulou%2C+and+N%2E+Yoshida%2E+Bounded+session+types+for+object+oriented+languages%2E+In+FMCO+%2706%2C+volume+4709+of+Springer+LNCS%2C+pages+207-245%2C+2006%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch14_b6"/>M. Dezani-Ciancaglini, D. Mostrous, N. Yoshida, and S. Drossopolou. Session types for object-oriented languages. In <emphasis>ECOOP &#x2019;06</emphasis>, volume 4067 of <emphasis>Springer LNCS</emphasis>, pages 328&#x2013;352, 2006. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=M%2E+Dezani-Ciancaglini%2C+D%2E+Mostrous%2C+N%2E+Yoshida%2C+and+S%2E+Drossopolou%2E+Session+types+for+object-oriented+languages%2E+In+ECOOP+%2706%2C+volume+4067+of+Springer+LNCS%2C+pages+328-352%2C+2006%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch14_b7"/>M. Dezani-Ciancaglini, N. Yoshida, A. Ahern, and S. Drossopolou. A distributed object-oriented language with session types. In <emphasis>TGC &#x2019;05</emphasis>, volume 3705 of <emphasis>Springer LNCS</emphasis>, pages 299&#x2013;318, 2005. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=M%2E+Dezani-Ciancaglini%2C+N%2E+Yoshida%2C+A%2E+Ahern%2C+and+S%2E+Drossopolou%2E+A+distributed+object-oriented+language+with+session+types%2E+In+TGC+%2705%2C+volume+3705+of+Springer+LNCS%2C+pages+299-318%2C+2005%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch14_b8"/>S. J. Gay and V. T. Vasconcelos. Linear type theory for asynchronous session types. <emphasis>Journal of Functional Programming</emphasis>, 20(1):19&#x2013;50, 2010. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=S%2E+J%2E+Gay+and+V%2E+T%2E+Vasconcelos%2E+Linear+type+theory+for+asynchronous+session+types%2E+Journal+of+Functional+Programming%2C+20%281%29%3A19-50%2C+2010%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch14_b9"/>S. J. Gay, V. T. Vasconcelos, A. Ravara, N. Gesbert, and A. Z. Caldeira. Modular session types for distributed object-oriented programming. In <emphasis>POPL &#x2019;10</emphasis>, pages 299&#x2013;312. ACM Press, 2010. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=S%2E+J%2E+Gay%2C+V%2E+T%2E+Vasconcelos%2C+A%2E+Ravara%2C+N%2E+Gesbert%2C+and+A%2E+Z%2E+Caldeira%2E+Modular+session+types+for+distributed+object-oriented+programming%2E+In+POPL+%2710%2C+pages+299-312%2E+ACM+Press%2C+2010%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch14_b10"/>G. Hedin. An introductory tutorial on JastAdd attribute grammars. In <emphasis>Generative and Transformational Techniques in Software Engineering III</emphasis>, volume 6491 of <emphasis>Springer LNCS</emphasis>, pages 166&#x2013;200, 2011.</para></listitem>
<listitem><para><anchor id="ch14_b11"/>K. Honda, N. Yoshida, and M. Carbone. Multiparty asynchronous session types. In <emphasis>POPL &#x2019;08</emphasis>, pages 273&#x2013;284. ACM Press, 2008. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=K%2E+Honda%2C+N%2E+Yoshida%2C+and+M%2E+Carbone%2E+Multiparty+asynchronous+session+types%2E+In+POPL+%2708%2C+pages+273-284%2E+ACM+Press%2C+2008%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch14_b12"/>K. Honda. Types for dyadic interaction. In <emphasis>CONCUR &#x2019;93</emphasis>, volume 715 of <emphasis>Springer LNCS</emphasis>, pages 509&#x2013;523, 1993. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=K%2E+Honda%2E+Types+for+dyadic+interaction%2E+In+CONCUR+%2793%2C+volume+715+of+Springer+LNCS%2C+pages+509-523%2C+1993%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch14_b13"/>K. Honda, V. Vasconcelos, and M. Kubo. Language primitives and type discipline for structured communication-based programming. In <emphasis>ESOP &#x2019;98</emphasis>, volume 1381 of <emphasis>Springer LNCS</emphasis>, pages 122&#x2013;138, 1998.</para></listitem>
<listitem><para><anchor id="ch14_b14"/>R. Hu and N. Yoshida. Hybrid session verification through endpoint API generation. In <emphasis>FASE 16</emphasis>, volume 9633 of <emphasis>Springer LNCS</emphasis>, pages 401&#x2013;418, 2016. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=R%2E+Hu+and+N%2E+Yoshida%2E+Hybrid+session+verification+through+endpoint+API+generation%2E+In+FASE+16%2C+volume+9633+of+Springer+LNCS%2C+pages+401-418%2C+2016%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch14_b15"/>R. Hu, N. Yoshida, and K. Honda. Session-based distributed programming in Java. In <emphasis>ECOOP &#x2019;08</emphasis>, volume 5142 of <emphasis>Springer LNCS</emphasis>, pages 516&#x2013;541, 2008. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=R%2E+Hu%2C+N%2E+Yoshida%2C+and+K%2E+Honda%2E+Session-based+distributed+programming+in+Java%2E+In+ECOOP+%2708%2C+volume+5142+of+Springer+LNCS%2C+pages+516-541%2C+2008%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch14_b16"/>D. Kouzapas, O. Dardha, R. Perera, and S. J. Gay. Typechecking protocols with Mungo and StMungo. In <emphasis>PPDP &#x2019;16</emphasis>, pages 146&#x2013;159. ACM Press, 2016. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=D%2E+Kouzapas%2C+O%2E+Dardha%2C+R%2E+Perera%2C+and+S%2E+J%2E+Gay%2E+Typechecking+protocols+with+Mungo+and+StMungo%2E+In+PPDP+%2716%2C+pages+146-159%2E+ACM+Press%2C+2016%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch14_b17"/>M. Neubauer and P. Thiemann. An implementation of session types. In <emphasis>PADL &#x2019;04</emphasis>, volume 3057 of <emphasis>Springer LNCS</emphasis>, pages 56&#x2013;70, 2004. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=M%2E+Neubauer+and+P%2E+Thiemann%2E+An+implementation+of+session+types%2E+In+PADL+%2704%2C+volume+3057+of+Springer+LNCS%2C+pages+56-70%2C+2004%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch14_b18"/>Post office protocol version 3, RFC 1939. <literal>https://www.ietf.org/rfc/rfc1939</literal>.</para></listitem>
<listitem><para><anchor id="ch14_b19"/>R. Pucella and J. A. Tov. Haskell session types with (almost) no class. In <emphasis>Proceedings of the 1st ACM SIGPLAN Symposium on Haskell</emphasis>, pages 25&#x2013;36. ACM Press, 2008. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=R%2E+Pucella+and+J%2E+A%2E+Tov%2E+Haskell+session+types+with+%28almost%29+no+class%2E+In+Proceedings+of+the+1st+ACM+SIGPLAN+Symposium+on+Haskell%2C+pages+25-36%2E+ACM+Press%2C+2008%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch14_b20"/>Scribble project homepage. <literal>www.scribble.org</literal>.</para></listitem>
<listitem><para><anchor id="ch14_b21"/>Simple mail transfer protocol, RFC 821. <literal>https://tools.ietf.org/html/rfc821</literal>.</para></listitem>
<listitem><para><anchor id="ch14_b22"/>R. E. Strom and S. Yemini. Typestate: A programming language concept for enhancing software reliability. <emphasis>IEEE Trans. Softw. Eng.</emphasis>, 12(1): 157&#x2013;171, 1986. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=R%2E+E%2E+Strom+and+S%2E+Yemini%2E+Typestate%3A+A+programming+language+concept+for+enhancing+software+reliability%2E+IEEE+Trans%2E+Softw%2E+Eng%2E%2C+12%281%29%3A+157-171%2C+1986%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch14_b23"/>J. Sunshine, K. Naden, S. Stork, J. Aldrich, and &#x00C9;. Tanter. First-class state change in Plaid. In <emphasis>OOPSLA &#x2019;11</emphasis>, pages 713&#x2013;732. ACM Press, 2011. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=J%2E+Sunshine%2C+K%2E+Naden%2C+S%2E+Stork%2C+J%2E+Aldrich%2C+and+%C9%2E+Tanter%2E+First-class+state+change+in+Plaid%2E+In+OOPSLA+%2711%2C+pages+713-732%2E+ACM+Press%2C+2011%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch14_b24"/>K. Takeuchi, K. Honda, and M. Kubo. An interaction-based language and its typing system. In <emphasis>PARLE &#x2019;94</emphasis>, volume 817 of <emphasis>Springer LNCS</emphasis>, pages 398&#x2013;413, 1994. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=K%2E+Takeuchi%2C+K%2E+Honda%2C+and+M%2E+Kubo%2E+An+interaction-based+language+and+its+typing+system%2E+In+PARLE+%2794%2C+volume+817+of+Springer+LNCS%2C+pages+398-413%2C+1994%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch14_b25"/>N. Yoshida, R. Hu, R. Neykova, and N. Ng. The Scribble protocol language. In <emphasis>TGC &#x2019;13</emphasis>, volume 8358 of <emphasis>Springer LNCS</emphasis>, pages 22&#x2013;41, 2013.</para></listitem>
</orderedlist>
</section>
</chapter>
<chapter class="chapter" id="ch15" label="15" xreflabel="15">
<title>Protocol-Driven MPI Program Generation</title>
<para><emphasis role="strong">Nicholas Ng and Nobuko Yoshida</emphasis></para>
<para>Imperial College London, UK</para>
<section class="lev2">
<title>Abstract</title>
<para>This chapter presents Parameterised Scribble (<emphasis role="romanAlt">Pabble</emphasis>), an extension of the Scribble language to capture scalable protocols, and a top-down, code generation framework of Message-Passing Interface (MPI) programs.</para>
<para>The code generation process begins with defining a <emphasis role="romanAlt">Pabble</emphasis> protocol for the topology of the MPI application. An MPI parallel program skeleton is automatically generated from the protocol, which can then be merged with code kernels defining their behaviours. The merging process is fully automated through the use of an aspect-oriented compilation tool.</para>
<para><emphasis role="romanAlt">Pabble</emphasis> protocols are parameterised over the number of roles at runtime, and are grounded on theories of parameterised multiparty session types (MPST) where valid <emphasis role="romanAlt">Pabble</emphasis> protocols can ensure safety and progress of communication in the generated MPI programs. Using the framework, programmers only need to supply the intended <emphasis role="romanAlt">Pabble</emphasis> protocol and provide code kernels to obtain parallelised programs. Since the skeleton generation and the merging process are automatic, the framework not only simplifies the development of MPI programs, the output programs are efficient and scalable MPI applications, that are guaranteed, free from communication mismatch, type errors or deadlocks by construction, improving productivity of programmers.</para>
</section>
<section class="lev1" id="sec15-1" label="15.1" xreflabel="15.1">
<title>Introduction</title>
<para>The Message Passing Interface (MPI) [<link linkend="ch15_b8">8</link>] is the de-facto standard for parallel programming on high-performance computing systems. Despite the advances in novel techniques and models such as the Partitioned Global Address Space (PGAS) used by X10 [<link linkend="ch15_b3">3</link>, <link linkend="ch15_b10">10</link>, <link linkend="ch15_b17">17</link>] for simplifying parallel programming, MPI is still by far the most widely used parallel programming library in the scientific community. However, parallel programming with the MPI library is a well-documented difficult task, in which reasoning about interactions between distributed processes is difficult at scale, and communication mismatches are amongst the most common pitfalls by MPI users [<link linkend="ch15_b6">6</link>].</para>
<para>To apply behavioural types in safe, scalable parallel programming, this chapter presents a parallel programming workflow based on a protocol language <emphasis role="romanAlt">Pabble</emphasis>, which we will explain in more details in Section 15.5. <link linkend="fig15_1">Figure <xref linkend="fig15_1" remap="15.1"/></link> shows the overview of our approach, and this chapter explains the core use case of the approach highlighted in the figure. A <emphasis role="romanAlt">Pabble</emphasis> protocol is an abstract representation of the communication topology, or <emphasis>parallel communication patterns</emphasis> of a parallel application. We consider every application a coupling between sequential, computation code that defines functional behaviours of processes in the application, and a communication topology that connects the processes together as a coherent application. Hence, to build a parallel application, we first define the communication protocol, written in <emphasis role="romanAlt">Pabble</emphasis>. A valid <emphasis role="romanAlt">Pabble</emphasis> protocol is guaranteed free of interactions and patterns that introduce communication errors and deadlocks. The <emphasis role="romanAlt">Pabble</emphasis> protocol is used to generate an annotated MPI program backbone (Section 15.6), specifying the interactions between parallel processes. Based on the <emphasis role="romanAlt">Pabble</emphasis> protocol, computation kernels are written in C language (C99), using queues to pass data locally between the kernels. The kernels are then merged with the MPI backbone by LARA [<link linkend="ch15_b2">2</link>], an aspect-oriented compilation tool, to transform the backbone and the kernels into a complete MPI application (Section 15.7).</para>
<fig id="fig15_1" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 15.1</label>
<caption><title><emphasis role="romanAlt">Pabble</emphasis>-based MPI program generation workflow (core flow highlighted).</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig084.jpg" mime-subtype="jpeg"/>
</fig>
<para>In addition to the merge, LARA can also perform pragma directed optimisations on the source code to overlap communication and computation, improving the runtime performance. The details of the optimisations, rigorous evaluations of the approach, and a pre-generated repository of common protocols are omitted from this chapter, but can be found in the original paper [<link linkend="ch15_b12">12</link>].</para>
</section>
<section class="lev1" id="sec15-2" label="15.2" xreflabel="15.2">
<title>Pabble: Parameterised Scribble</title>
<para>In this section we introduce <emphasis>Parameterised Scribble</emphasis> (<emphasis role="romanAlt">Pabble</emphasis>) [<link linkend="ch15_b14">14</link>, <link linkend="ch15_b15">15</link>], a developer friendly notation for specifying application level interaction protocol based on the theory of <emphasis>parameterised</emphasis> multiparty session types [<link linkend="ch15_b5">5</link>]. As the name suggests, <emphasis role="romanAlt">Pabble</emphasis> is a parametric evolution of Scribble [<link linkend="ch15_b16">16</link>, <link linkend="ch15_b18">18</link>], which itself is based on the theory of multiparty session types [<link linkend="ch15_b1">1</link>, <link linkend="ch15_b9">9</link>]. We begin with an example Scribble protocol to explain the basic syntax of <emphasis role="romanAlt">Pabble</emphasis> and the Scribble family of protocol languages, and why parameterisation is important for protocols describing scalable, parallel program topologies.</para>
<fig id="lis15_1">
<label>Listing 15.1</label>
<caption><title>Ring protocol in Scribble.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog203.jpg" mime-subtype="jpeg"/>
</fig>
<para>This <literal>Ring</literal> protocol describes a series of communications in which the role <literal>Worker1</literal> passes a message of type <literal>Data(T)</literal> to <literal>Worker3</literal> by forwarding through <literal>Worker2</literal>, and receives back a <literal>DataLast(T)</literal> message from <literal>Worker3</literal> to complete the ring. It is easy to notice that explicitly describing all interactions among distinct roles is verbose and inflexible: for example, when extending the protocol with an additional role <literal>Worker4</literal>, we must rewrite the whole protocol. On the other hand, we observe that these worker roles have identical communication patterns that can be logically grouped together: <literal>Worker</literal><emphasis><subscript>i</subscript></emphasis><subscript>+1</subscript> receives a message from <literal>Worker</literal><emphasis><subscript>i</subscript></emphasis> and the last <literal>Worker</literal> sends a message to <literal>Worker</literal><subscript>1</subscript>. In order to capture these replicable patterns, we introduce an extension of Scribble with dependent types, namely <emphasis role="romanAlt">Pabble</emphasis>. In <emphasis role="romanAlt">Pabble</emphasis>, multiple participants can be grouped in the same role and indexed. This greatly enhances the expressive power and modularity of the protocols. Here &#x2018;parameterised&#x2019; refers to the number of participants in a role that can be changed by parameters.</para>
<fig id="lis15_2">
<label>Listing 15.2</label>
<caption><title>Parametrised Ring protocol in <emphasis role="romanAlt">Pabble</emphasis>.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog204.jpg" mime-subtype="jpeg"/>
</fig>
<para>Our ring example is rewritten in the syntax of <emphasis role="romanAlt">Pabble</emphasis> shown above. The <literal>role Worker[1..N]</literal> declares workers with indices <literal>1</literal> up to an arbitrary integer <literal>N</literal>. The <literal>Worker</literal> roles can be identified individually by their indices, for example, <literal>Worker[<link linkend="ch15_b1">1</link>]</literal> refers to the first and <literal>Worker[N]</literal> refers to the last. In the body of the protocol, the sender, <literal>Worker[i:1..N-1]</literal>, declares multiple <literal>Worker</literal>s, bound by the bound variable <literal>i</literal>, and iterates from <literal>1</literal> to <literal>N-1</literal>. The receivers, <literal>Worker[i+1]</literal>, are calculated on their indices for each instance of the bound variable <literal>i</literal>. The second line is a message sent back from <literal>Worker[N]</literal> to <literal>Worker[<link linkend="ch15_b1">1</link>]</literal>.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog205.jpg" mime-subtype="jpeg"/></para>
<para>The above code shows the <emphasis>local protocol</emphasis> of <literal>Ring</literal>, which is a localised version of <link linkend="list15_2">Listing <xref linkend="list15_2" remap="15.2"/></link> at the <literal>Worker</literal> role. It represents the <literal>Worker[1..N]</literal> parameterised role, and corresponds to multiple endpoints in the same logical grouping. A <emphasis role="romanAlt">Pabble</emphasis> local protocol is automatically generated from its global protocol following the projection algorithm in [<link linkend="ch15_b14">14</link>], and programmers only need to define the global protocol to use <emphasis role="romanAlt">Pabble</emphasis> for MPI development.</para>
<para>Above servers as a primer on the <emphasis role="romanAlt">Pabble</emphasis> language, sufficient for our introductory example; Later, the full syntax and explanations of the <emphasis role="romanAlt">Pabble</emphasis> language will be given in Section 15.5.</para>
</section>
<section class="lev1" id="sec15-3" label="15.3" xreflabel="15.3">
<title>MPI Backbone</title>
<para>A typical MPI program follows a Single Program, Multiple Data (SPMD) parallel programming model, where a single source code is executed by multiple parallel processors. This model shares a lot of similarities with the parameterised local protocols in <emphasis role="romanAlt">Pabble</emphasis> which groups together similar roles in a single protocol, except that local protocols can be generated from global protocols which are easier to express overall communication or topologies. As a running example, we use the <emphasis role="romanAlt">Pabble</emphasis> protocol presented earlier to demonstrate the framework and implement a ring accumulator that calculates a sum of values from each <literal>Worker</literal> and distribute to all.</para>
<fig id="lis15_3">
<label>Listing 15.3</label>
<caption><title>MPI backbone generated from the <literal>Ring</literal> protocol.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog206.jpg" mime-subtype="jpeg"/>
</fig>
<section class="lev2" id="sec15-3-1" label="15.3.1" xreflabel="15.3.1">
<title>MPI Backbone Generation from <literal>Ring</literal> Protocol</title>
<para>Based on the <emphasis role="romanAlt">Pabble</emphasis> <literal>Ring</literal> protocol in the introduction, our code generation framework generates an <emphasis>MPI backbone</emphasis> code (e.g. <link linkend="list15_3">Listing <xref linkend="list15_3" remap="15.3"/></link>). First it automatically generates <emphasis>local protocols</emphasis> from a global protocol as an intermediate step to make MPI code generation more straightforward. The MPI backbone generation procedure is described in details later in Section 15.6, here we focus on the generated MPI backbone code output.</para>
<para>An MPI backbone is a C99 program with boilerplate code for initialising and finalising the MPI environment of a typical MPI application (lines 2&#x2013;4 and 30 respectively), and MPI primitives for message passing (e.g. <literal>MPI_Isend</literal>/<literal>MPI_Irecv</literal><sup>1</sup>). Therefore the MPI backbone realises the interaction between participants as specified in the <emphasis role="romanAlt">Pabble</emphasis> protocol, without supporting any specific application functionality. The backbone has three kinds of <literal>#pragma</literal> annotations as placeholders for kernel functions, types and program logic. The annotations are explained in Section 15.7.1. The boxed code in <link linkend="list15_3">Listing <xref linkend="list15_3" remap="15.3"/></link> represents how the backbone are converted to code that calls the kernel functions in the MPI program.</para>
<para>On lines 5 and 6, <emphasis>generic type</emphasis> <literal>T</literal> and <literal>MPI_T</literal> are defined datatypes for C and MPI respectively. <literal>T</literal> and <literal>MPI_T</literal> are refined later when an exact type (e.g. <literal>int</literal> or composite <literal>struct</literal> type) is known with the kernels.</para>
<para>Following the type declarations, other variable declarations including the buffers (line 9), and their allocation and deallocation are managed by the backbone. They are generated as guarded blocks of code, which come directly from the local protocol. lines 15&#x2013;21 shows a guarded receive that correspond to <literal>if Worker[i:2..N] Data(T)from Worker[i-1]</literal> in the protocol and lines 22&#x2013;28 for <literal>if Worker[i:1..N-1] Data(T)to Worker[i+1]</literal>.</para>
<para>Given the MPI backbone, we can then implement computation kernels for the MPI program.</para>
</section>
</section>
<section class="lev1" id="sec15-4" label="15.4" xreflabel="15.4">
<title>Computation Kernels</title>
<para>Computation kernels are C functions that describe the algorithmic behaviour of the application. Conceptually, each message interaction defined in <emphasis role="romanAlt">Pabble</emphasis> (e.g. <literal>Label(T)from Sender to Receiver</literal>), and &#x2013; through the automatic MPI backbone generation &#x2013; the MPI backbone, can be associated to a kernel by its label (e.g. <literal>Label</literal>).</para>
<footnote id="fn15_1" label="1"><para>We use <literal>MPI_Isend</literal>/<literal>MPI_Irecv</literal> with <literal>MPI_Wait</literal> in place of the equivalent <literal>MPI_Send</literal>/<literal>MPI_Recv</literal> respectively. To simplify presentation we write <literal>MPI_Send</literal>/<literal>MPI_Recv</literal> in the rest of the chapter.</para></footnote>
<fig id="fig15_2" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 15.2</label>
<caption><title>Global view of <literal>Label(T)from Sender to Receiver;</literal>.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig085.jpg" mime-subtype="jpeg"/>
</fig>
<para><link linkend="fig15_2">Figure <xref linkend="fig15_2" remap="15.2"/></link> shows how kernels are invoked in a message-passing statement between two processes named <literal>Sender</literal> and <literal>Receiver</literal> respectively. Since a message interaction statement involves two participants (e.g. <literal>Sender</literal> and <literal>Receiver</literal>), the kernel serves two purposes: (1) produce a message for sending and (2) consume a message after it has been received. The two parts of the kernel are defined in the same function, but runs on the sending process and the receiving process respectively. The kernels are top-level functions and do not send or receive messages directly through MPI calls. Instead, messages are passed between kernels and the MPI backbone (derived from the <emphasis role="romanAlt">Pabble</emphasis> protocol) via a queue API: in order to send a message, the producer kernel (e.g. (1)) of the sending process enqueues the message to its send queue; and a received message can be accessed by a consumer kernel (e.g. (2)), dequeuing from its receive queue. This allows the decoupling between computation (as defined by the kernels) and communication (as described in the MPI backbone).</para>
<section class="lev2" id="sec15-4-1" label="15.4.1" xreflabel="15.4.1">
<title>Writing a Kernel</title>
<para>We now explain how a user writes a kernel file, which contains the set of kernel functions related to a <emphasis role="romanAlt">Pabble</emphasis> protocol for an application. As an example, we implement accumulator in a ring topology below.</para>
<para>A minimal kernel file must define a variable <literal>meta</literal> of <literal>meta_t</literal> type, which contains the process id (i.e. <literal>meta.pid</literal>), total number of spawned processes (i.e. <literal>meta.nprocs</literal>) and a callback function that takes one parameter (message label) and returns the send/receive size of message payload (i.e. <literal>unsigned int meta.bufsize(int label)</literal>). The <literal>meta.buflen</literal> function returns the buffer size for the MPI primitives based on the label given, as a lookup table to manage the buffer sizes centrally. Process id and total number of spawned processes will be populated automatically by the backbone code generated. The kernel file includes the definitions of the kernel functions, annotated with pragmas, associating the kernels with message labels. The pragmas that are allowed are detailed in Section 15.7. The kernels can use file (i.e. <literal>static</literal>) scope variables for local data storage. Our ring accumulator kernel file starts with the following declarations for local data and <literal>meta</literal>:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog207.jpg" mime-subtype="jpeg"/></para>
<section class="lev3" id="sec15-4-1-1" label="15.4.1.1" xreflabel="15.4.1.1">
<title>Initialisation</title>
<para>Most parallel applications require explicit partitioning of input data. In these cases, the programmer writes a kernel function for partitioning, such that each participant has a subset of the input data. Input data are usually partitioned with a layout similar to the layout of the participants. In our ring accumulator example, the processes are arranged linearly, and the input file contains an array of at least <literal>meta.nprocs</literal> elements, so <literal>meta.nprocs</literal> initial values are read into the <literal>local-&gt;values</literal> array. In our example initialisation function below, we also set the current accumulated value to be our initial value of <literal>local-&gt;values[meta.pid]</literal>.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog208.jpg" mime-subtype="jpeg"/></para>
</section>
<section class="lev3" id="sec15-4-1-2" label="15.4.1.2" xreflabel="15.4.1.2">
<title>Passing data between backbone and kernel through queues</title>
<para>The kernels are <literal>void</literal> functions with at least one parameter, which is the label of the kernel. Inside the kernel, no MPI primitive should be used to perform message passing. Data received from another participant or data that need to be sent to another participant can be accessed using a receive queue and send queue. Consider the following kernel for the label <literal>Data</literal> in the ring accumulator example:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog209.jpg" mime-subtype="jpeg"/></para>
<para>Each kernel has access to a send and receive queue local to the whole process, which holds pointers to the buffer to be sent and the buffer containing the received messages, respectively. The queues are the only mechanism for kernels to interface the MPI backbone. The simplest kernel is one that forwards incoming messages from the receive queue directly to the send queue. In the above function, when the kernel function is called, it either consumes a message from the receive queue if it is not empty (i.e. after a receive), or produce a message for the send queue (i.e. before a send).</para>
<para>Kernels can have extra parameters. For example, in the <literal>init</literal> function above, <literal>filename</literal> is a parameter that is not specified by the protocol (i.e. <literal>Init ()</literal>). When such functions are called, all extra parameters are supplied by command-line arguments in the final generated MPI application.</para>
</section>
<section class="lev3" id="sec15-4-1-3" label="15.4.1.3" xreflabel="15.4.1.3">
<title>Predicates</title>
<para>A predicate kernel is similar to a normal <literal>void</literal> kernel, but with a function signature that returns an <literal>int</literal> (as a boolean), it is used as a conditional variable, where the value of the variable is determined by the body of the kernel. In the <literal>iter()</literal> predicate kernel, we use the number of processes to determine when the ring protocol has completed a cycle (i.e. executed <literal>meta.nproc</literal> times) and terminate the <literal>while</literal>-loop.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog210.jpg" mime-subtype="jpeg"/></para>
<para>Aftere writing the computation kernels, we can then use the framework to merge the MPI backbones with the computation kernels, and we get a complete MPI program. The resulting MPI program is shown in <link linkend="list15_3">Listing <xref linkend="list15_3" remap="15.3"/></link> (boxed code).</para>
</section>
</section>
</section>
<section class="lev1" id="sec15-5" label="15.5" xreflabel="15.5">
<title>The Pabble Language</title>
<para>In this section, we present more details of the <emphasis role="romanAlt">Pabble</emphasis> language, including its syntax, and the well-formedness conditions (i.e. syntactic restrictions to ensure protocol correctness) of the language.</para>
<section class="lev2" id="sec15-5-1" label="15.5.1" xreflabel="15.5.1">
<title>Global Protocols Syntax</title>
<para><link linkend="fig15_3">Figure <xref linkend="fig15_3" remap="15.3"/></link> lists the core syntax of <emphasis role="romanAlt">Pabble</emphasis>, which consists of two protocol declarations, <emphasis>global</emphasis> and <emphasis>local</emphasis>. A global protocol is declared with the protocol name (<emphasis>str</emphasis> denotes a string) with role and group parameters followed by the body <emphasis>G</emphasis>. Role <emphasis>R</emphasis> is a name with argument expressions. The argument expressions are ranges or arithmetic expressions <emphasis>h</emphasis>, and the number of arguments corresponds to the dimension of the array of roles: for example, <literal>Worker [1..4][1..2]</literal> denotes a 2-D array with size 4 and 2 in the two dimensions respectively, forming a 4-by-2 array of roles.</para>
<para>Declared roles can be grouped by specifying a named group using the keyword <literal>group</literal>, followed by the group name and the set of roles. For example,</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog211.jpg" mime-subtype="jpeg"/></para>
<para>creates a group which consists of two <literal>Worker</literal>s. A special built-in group, <literal>All</literal>, is defined as <emphasis>all processes in a session</emphasis>. We can encode collective operators such as many-to-many and many-to-one communication with <literal>All</literal>, which will be explained later.</para>
<para>Apart from specifying ranges by constants, ranges can also be specified using expressions. Expression <emphasis>e</emphasis> consists of operators for numbers, logarithm, left and right logical shifts (<literal>&lt;&lt;</literal>, <literal>&gt;&gt;</literal>), numbers, variables (<emphasis>i, j, k</emphasis>), and constants (<literal>M</literal>, <literal>N</literal>). Constants are either <emphasis>bound</emphasis> outside the protocol declaration or are left <emphasis>free</emphasis> (unbound) to represent an arbitrary number. As in [<link linkend="ch15_b11">11</link>], when the constants are bound, they are declared by numbers outside the protocol, e.g. <literal>const N = 10</literal> or lower and upper bounds, e.g. <literal>const N = 1..10</literal>. We also allow leaving the declaration <emphasis>free</emphasis> (unbound), e.g. <literal>const N</literal>, as a shorthand to represent an arbitrary constant with lower and upper bounds <literal>0</literal> and <literal>max</literal> respectively, i.e. <literal>const N = 0..max</literal>, where <literal>max</literal> is a special value representing the maximum possible value or practically unbounded. Binding range expression <emphasis>b</emphasis> takes the form of <emphasis>i</emphasis> <emphasis role="roman">:</emphasis> <emphasis>e</emphasis><subscript>1</subscript><emphasis>..e</emphasis><emphasis><subscript>n</subscript></emphasis> which means <emphasis>i</emphasis> is ranged from <emphasis>e</emphasis><subscript>1</subscript> to <emphasis>e</emphasis><emphasis><subscript>n</subscript></emphasis>. Binding variables always bind to a range expression and not individual values. Indices in a <emphasis role="romanAlt">Pabble</emphasis> protocol must be bound with the binding range expression, the details are omitted here, please see <emphasis>indices well-formed conditions</emphasis> in [<link linkend="ch15_b14">14</link>].</para>
<fig id="fig15_3" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 15.3</label>
<caption><title><emphasis role="romanAlt">Pabble</emphasis> syntax.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig086.jpg" mime-subtype="jpeg"/>
</fig>
<para>In a global protocol <emphasis>G</emphasis>, <emphasis>l</emphasis><emphasis role="roman">(</emphasis><emphasis>T</emphasis> <emphasis role="roman">)</emphasis> <literal>from</literal> <emphasis>R</emphasis><subscript>1</subscript> <literal>to</literal> <emphasis>R</emphasis><subscript>2</subscript> is called an <emphasis>interaction statement</emphasis>, which represents passing a message with label <emphasis>l</emphasis> and type <emphasis>T</emphasis> from one role <emphasis>R</emphasis><subscript>1</subscript> to another role <emphasis>R</emphasis><subscript>2</subscript>. <emphasis>R</emphasis><subscript>1</subscript> is a <emphasis>sender role</emphasis> and <emphasis>R</emphasis><subscript>2</subscript> is a <emphasis>receiver role</emphasis>. <literal>choice at</literal> <emphasis>R</emphasis> <literal>{</literal> <emphasis>G</emphasis><subscript>1</subscript> <literal>} or ... or {</literal> <emphasis>G</emphasis><emphasis><subscript>N</subscript></emphasis> <literal>}</literal> means the role <emphasis>R</emphasis> will select one of the global types <emphasis>G</emphasis><subscript>1</subscript>,...,<emphasis>G</emphasis><emphasis><subscript>N</subscript></emphasis>. <literal>rec</literal> <emphasis>l</emphasis> <literal>{</literal> <emphasis>G</emphasis> <literal>}</literal> is recursion with the label <emphasis>l</emphasis> which declares a label for <literal>continue</literal> <emphasis>l</emphasis> statement. <literal>foreach (</literal><emphasis>b</emphasis><literal>) {</literal><emphasis>G</emphasis><literal>}</literal> denotes a for-loop whose iteration is specified by <emphasis>b</emphasis>. For example, <literal>foreach (i:1..n){</literal> <emphasis>G</emphasis> <literal>}</literal> represents the iteration from <emphasis role="roman">1</emphasis> to <emphasis>n</emphasis> of <emphasis>G</emphasis> where <emphasis>G</emphasis> is parameterised by <emphasis>i</emphasis>.</para>
<para>Finally, <literal>allreduce</literal> <emphasis>op</emphasis><emphasis><subscript>c</subscript></emphasis><literal>(</literal><emphasis>T</emphasis> <literal>)</literal> means all processes perform a distributed reduction of value with type <emphasis>T</emphasis> with the operator <emphasis>op</emphasis><emphasis><subscript>c</subscript></emphasis> (like <literal>MPI_Allreduce</literal> in MPI), and sends the resulting value from the reduction to all processes. It takes a mandatory predefined operator <emphasis>op</emphasis><emphasis><subscript>c</subscript></emphasis> where <emphasis>op</emphasis><emphasis><subscript>c</subscript></emphasis> must be a commutative and associative arithmetic operation so they can correspond to MPI reduction operations which have the same requirements. <emphasis role="romanAlt">Pabble</emphasis> currently supports sum and product.</para>
<para>We allow using simple expressions (e.g. <literal>Worker[i:0..2*N-1]</literal>) to parameterise ranges. In addition, indices can also be calculated by expressions on bound variables (e.g. <literal>Worker[i+1]</literal>) to refer to relative positions of roles.</para>
<para>There are restrictions on the indices on such as relative indices calculations and index bounds presented below. The restrictions ensure termination of the projection algorithm and safety of the communication topology at runtime.</para>
<section class="lev3" id="sec15-5-1-1" label="15.5.1.1" xreflabel="15.5.1.1">
<title>Restriction on constants</title>
<para>In <emphasis role="romanAlt">Pabble</emphasis> protocols, constants can be defined by</para>
<orderedlist numeration="arabic" continuation="restarts" spacing="normal">
<listitem><para>A single numeric value (<literal>const N = 3</literal>); or</para></listitem>
<listitem><para>Lower and upper bound constraints not involving the <literal>max</literal> keyword; or</para></listitem>
<listitem><para>A range defined with the <literal>max</literal> keyword.</para></listitem>
</orderedlist>
<para>(1) sets a fixed value to a constant, as exemplified in <link linkend="list15_2">Listing <xref linkend="list15_2" remap="15.2"/></link>. (2) gives runtime constants a lower bound and an upper bound, e.g. the number of processes spawned in a scalable protocol, which is unknown at design time and will be defined and immutable once the execution begins. To ensure <emphasis role="romanAlt">Pabble</emphasis> protocols are communication-safe in all possible values of constants, we must ensure that all parameterised role indices stay within their declared range. Such conditions prevent sending or receiving from an invalid (non-existent) role which will lead to communication mismatch at runtime.</para>
<para>The following explains how to determine whether the protocol will be valid for all combinations of constants:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog212.jpg" mime-subtype="jpeg"/></para>
<para>The basic constraints from the constants are:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog212a.jpg" mime-subtype="jpeg"/></para>
<para>We then calculate the range of <literal>R[i+1]</literal> as <literal>R[2..M+1]</literal>. Since the objective is to ensure that the role parameters in the protocol body (i.e. <literal>1..M</literal> and <literal>2..M+1</literal>) stay within the bounds of <literal>1..N</literal>, we define a constraint set to be:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog212b.jpg" mime-subtype="jpeg"/></para>
<para>which are lower and upper bound inequalities of the two ranges. From them, we obtain this inequality as a result:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog212c.jpg" mime-subtype="jpeg"/></para>
<para>By comparing this against the basic constraints on the constants, we can check that not all outcomes belong to the regions and thus this is not a communication-safe protocol (an example of a unsafe case is <literal>M</literal> <emphasis role="roman">= 3</emphasis> and <literal>N</literal> <emphasis role="roman">= 2</emphasis>). On the other hand, if we alter line 4 to <literal>T from R[i:1..N-1] to R [i+1];</literal>, the constraints are unconditionally true and so we can guarantee all combinations of constants <literal>M</literal> and <literal>N</literal> will not cause communication errors. (3) is a special case of (2), where the upper bound of a constant is set to the <literal>max</literal> keyword. We write <literal>const N = 0..max</literal> to represent a range without upper bound, here it means the constant <literal>N</literal> can be any integer value larger than <emphasis role="roman">1</emphasis>. Since it is not possible to enumerate all values of <literal>N</literal>, we apply a more restrictive constraint on the expressions, allowing only range calculation that uses addition or subtractions on integers (e.g. <literal>i+1</literal>).</para>
</section>
</section>
<section class="lev2" id="sec15-5-2" label="15.5.2" xreflabel="15.5.2">
<title>Local Protocols</title>
<para>As mentioned in Section 15.2, <emphasis>local protocols</emphasis> are localised versions of the global protocols at each role, and are used directly for skeleton generation. They are generated from a global protocol by a projection algorithm detailed in [<link linkend="ch15_b14">14</link>]. Local protocol <emphasis>L</emphasis> consists of the same syntax of the global type except the input from <emphasis>R</emphasis> (receive) and the output to <emphasis>R</emphasis> (send). The main declaration <literal>local protocol</literal> <emphasis>str</emphasis> <literal>at</literal> <emphasis>R</emphasis><emphasis><subscript>e</subscript></emphasis> <literal>(</literal><emphasis>...</emphasis><literal>) {</literal> <emphasis>L</emphasis> <literal>}</literal> means the protocol is located at role <emphasis>R</emphasis><emphasis><subscript>e</subscript></emphasis>. We call <emphasis>R</emphasis><emphasis><subscript>e</subscript></emphasis> <emphasis>the endpoint role</emphasis>. In <emphasis role="romanAlt">Pabble</emphasis>, multiple local protocol instances can reside in the same parameterised local protocol. This is because each local protocol is a local specification for a participant of the interaction. When there are multiple participants with a similar interaction structure that fulfil the same <emphasis>role</emphasis> in the protocol, such as the <literal>Worker</literal> role from our <literal>Ring</literal> example from the introduction, the participants are grouped together as a single parameterised role. The local protocol for a collection of participants can be specified in a single parameterised local protocol, using <emphasis>conditional statements</emphasis> on the role indices to capture corner cases. For example, in a general case of a pipeline interaction, all participants receive from one neighbour and send to another neighbour, except the first participant which initiates the pipeline and is only a sender and the last participant which ends the pipeline and does not send. In these cases we use conditional statements to guard the input or output statements. To express conditional statements in local protocols, <literal>if</literal> <emphasis>R</emphasis> may be prepended to an input or output statement. <literal>if</literal> <emphasis>R</emphasis> input/output statement will be ignored if the local role does not match <emphasis>R</emphasis>. More complicated matches can be performed with a parameterised role, where the role parameter range of the condition is matched against the parameter of the local role. For example, <literal>if Worker[1..3]</literal> will match <literal>Worker[<link linkend="ch15_b2">2</link>]</literal> but not <literal>Worker[<link linkend="ch15_b4">4</link>]</literal>. It is also possible to bind a variable to the range in the condition, e.g. <literal>if Worker[i:1..3]</literal>, and <literal>i</literal> can be used in the same statement.</para>
</section>
</section>
<section class="lev1" id="sec15-6" label="15.6" xreflabel="15.6">
<title>MPI Backbone Generation</title>
<para>Below we explain how <emphasis role="romanAlt">Pabble</emphasis> statements are translated into MPI blocks.</para>
<section class="lev2" id="sec15-6-1" label="15.6.1" xreflabel="15.6.1">
<title>Interaction</title>
<para>An interaction statement in a <emphasis role="romanAlt">Pabble</emphasis> protocol is projected in the local protocol as two parts: receive and send. The correspondence is shown in <link linkend="fig15_4">Figure <xref linkend="fig15_4" remap="15.4"/></link>.</para>
<para>The first line of the local protocol shows a receive statement, written in <emphasis role="romanAlt">Pabble</emphasis> as <literal>if P[dstId] from P[srcId]</literal>. The statement is translated to a block of MPI code in 3 parts. First, memory is dynamically allocated for the receive buffer (line 2), the buffer is of <literal>Type</literal> and its size fetched from the function <literal>meta.bufsize(Label)</literal>. The function is defined in the kernels and returns the size of message for the given message label. Next, the program calls <literal>MPI_Recv</literal> to receive a message (line 3) from participant <literal>P[srcRole]</literal> in <emphasis role="romanAlt">Pabble</emphasis>. <literal>role_P(srcIdx)</literal> is a lookup macro from the generated backbone to return the process id of the sender. Finally, the received message, stored in the receive buffer <literal>buf</literal>, is enqueued into a global receive queue with <literal>pabble_recvq_enqueue()</literal> (line 4), followed by the pragma indicating a kernel of label <literal>Label</literal> should be inserted. The block of receive code is guarded by an if-condition, which executes the above block of MPI code only if the current process id matches the receiver process id.</para>
<fig id="fig15_4" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 15.4</label>
<caption><title><emphasis role="romanAlt">Pabble</emphasis> interaction statement and its MPI backbone.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig087.jpg" mime-subtype="jpeg"/>
</fig>
<para>The next line in the local protocol is a send statement, converse of the receive statement, written as <literal>if P[srcIdx] Label(Type)to P[dstIdx]</literal>. The MPI code begins with the pragma annotation, then dequeuing the global send queue with <literal>pabble_sendq_dequeue()</literal> and sends the dequeued buffer with <literal>MPI_Send</literal>. After this, the send buffer, which is no longer needed, is deallocated. The block of send code is similarly guarded by an if-condition to ensure it is only executed by the sender. By allocating memory before receive and deallocating memory after send, the backbone manages memory for the user systematically. Since the protocol and the backbone makes no assumption about memory management on user&#x2019;s computation kernel, this mechanism helps the separation of concern between the protocol (i.e. the generated backbone) and the user kernels, and leaves open the possibility of optimal memory management during merge without breaking existing kernels.</para>
</section>
<section class="lev2" id="sec15-6-2" label="15.6.2" xreflabel="15.6.2">
<title>Parallel Interaction</title>
<para>A <emphasis role="romanAlt">Pabble</emphasis> parallel interaction statement is written as <literal>Label(Type)from P[i:1..N-1] to P[i+1]</literal>, meaning all processes with indices from <literal>1</literal> to <literal>N-1</literal> send a message to its next neighbour. <literal>P[<link linkend="ch15_b1">1</link>]</literal> initiates sending to <literal>P[<link linkend="ch15_b2">2</link>]</literal>, and <literal>P[<link linkend="ch15_b2">2</link>]</literal> receives from <literal>P[<link linkend="ch15_b1">1</link>]</literal> then sends a message to <literal>P[<link linkend="ch15_b3">3</link>]</literal>, and so on. As shown in <link linkend="fig15_5">Figure <xref linkend="fig15_5" remap="15.5"/></link>, the local protocol encapsulates the behaviour of all <literal>P[1..N]</literal> processes, and the statement is realised in the local as conditional receive followed by a conditional send, similar to ordinary interaction. The difference is the use of a range of process ids in the condition, and <emphasis>relative</emphasis> indices in the sender/receiver indices. The generated MPI code makes use of expression with <literal>meta.pid</literal> (current process id) to calculate the relative index.</para>
</section>
<section class="lev2" id="sec15-6-3" label="15.6.3" xreflabel="15.6.3">
<title>Internal Interaction</title>
<para>When role with name <literal>__self</literal> is used in a protocol, it means that both the sending and receiving endpoints are internal to the processes, and there is no interaction with external processes. This statement applies to all processes, and is not to be confused with self-messaging, e.g. <literal>Label()from P[<link linkend="ch15_b1">1</link>] to P[<link linkend="ch15_b1">1</link>]</literal>, which would lead to deadlock. The statement does not use any MPI primitives. The purpose of using this special role is to create optional insertion point for the MPI backbone, which may be used for optional kernels such as initialisation or finalisation, hence it generates a pragma in the MPI backbone.</para>
<fig id="fig15_5" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 15.5</label>
<caption><title><emphasis role="romanAlt">Pabble</emphasis> parallel interaction statement and its MPI backbone.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig088.jpg" mime-subtype="jpeg"/>
</fig>
<fig id="fig15_6" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 15.6</label>
<caption><title><emphasis role="romanAlt">Pabble</emphasis> internal interaction statement and its MPI backbone.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig089.jpg" mime-subtype="jpeg"/>
</fig>
</section>
<section class="lev2" id="sec15-6-4" label="15.6.4" xreflabel="15.6.4">
<title>Control-flow: Iteration and For-loop</title>
<para><literal>rec</literal> and <literal>foreach</literal> are iteration statements. Specifically <literal>rec</literal>/<literal>continue</literal> is recursion, where the iteration conditions are not specified explicitly in the protocol, and translates to <literal>while</literal>-loops. The loop condition is the same in all processes, otherwise be known as <emphasis>collective loops</emphasis>. The loop generated by <literal>rec</literal> has a <literal>#pragma pabble predicate</literal> annotation, so that the loop condition can be later replaced by a kernel (see Section 15.7.1).</para>
<para>The <literal>foreach</literal> construct, on the other hand, specifies a counting loop, iterating over the integer values in the range specified in the protocol from the lower bound (e.g. <literal>0</literal>) to the upper bound value (e.g. <literal>N-1</literal>). This construct can be naturally translated into a C <literal>for</literal>-loop.</para>
</section>
<section class="lev2" id="sec15-6-5" label="15.6.5" xreflabel="15.6.5">
<title>Control-flow: Choice</title>
<para>Conditional branching in <emphasis role="romanAlt">Pabble</emphasis> is performed by label branching and selection. We use the example given in <link linkend="fig15_8">Figure <xref linkend="fig15_8" remap="15.8"/></link> to explain. The deciding process, e.g. <literal>P[master]</literal>, makes a choice and executes the statements in the selected branch. Each branch starts by sending a unique label, e.g. <literal>Branch0</literal>, to the decision receiver, e.g. <literal>P[worker]</literal>. Hence for a well-formed <emphasis role="romanAlt">Pabble</emphasis> protocol, the first line of each branch is from the deciding process to the same process but using a different label.</para>
<para>Note that the decision is only known between the two processes in the first statement, and other processes should be explicitly notified or use broadcast to propagate the decision. The MPI backbone is generated with a different structure as the local protocol. First, the MPI backbone contains an outer if-then-else, splitting the deciding process (lines 1&#x2013;9) and the decision receiver (lines 9&#x2013;21). In the deciding process, a block of if-then-else-if code is generated to perform a send with different label (called MPI tag), e.g. line 5. This statement is generated with all the queue and memory management code as described above for ordinary interaction statements. Each of the if-condition is annotated with <literal>#pragma pabble predicate BranchLabel</literal>, so that the conditions can be replaced by predicate kernels (see Section 15.7.1). For the decision receiver, <literal>MPI_Probe</literal> is used to peek the received label, then the <literal>switch</literal> statement is used to perform the correct receive (for different branches).</para>
<fig id="fig15_7" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 15.7</label>
<caption><title>Control-flow: <emphasis role="romanAlt">Pabble</emphasis> iteration statements and their corresponding MPI backbones.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig090.jpg" mime-subtype="jpeg"/>
</fig>
<fig id="fig15_8" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 15.8</label>
<caption><title>Control-flow: <emphasis role="romanAlt">Pabble</emphasis> choice and its corresponding MPI backbone.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig091.jpg" mime-subtype="jpeg"/>
</fig>
</section>
<section class="lev2" id="sec15-6-6" label="15.6.6" xreflabel="15.6.6">
<title>Collective Operations: Scatter, Gather and All-to-all</title>
<para>Collective operations are written in <emphasis role="romanAlt">Pabble</emphasis> as multicast or multi-receive message interactions. While it is possible to convert these interactions into multiple blocks of MPI code following the rules in <link linkend="fig15_7">Figure <xref linkend="fig15_7" remap="15.7"/></link> (e.g. loop through receivers for scatter), we take advantage of the efficient and expressive collective primitives in MPI. <link linkend="fig15_9">Figure <xref linkend="fig15_9" remap="15.9"/></link> shows the conversion of <emphasis role="romanAlt">Pabble</emphasis> statements into MPI collective operations. We describe only the most generic collective operations, i.e. <literal>MPI_Scatter</literal>, <literal>MPI_Gather</literal> and <literal>MPI_Alltoall</literal>.</para>
<para>Translating collective operations from <emphasis role="romanAlt">Pabble</emphasis> to MPI uses both global <emphasis role="romanAlt">Pabble</emphasis> protocol statements and local protocol. If a statement involves the <literal>All</literal> role as sender, receiver or both, it is a collective operation. <link linkend="fig15_9">Figure <xref linkend="fig15_9" remap="15.9"/></link> shows that translated blocks of MPI code do not use <literal>if</literal>-statements to distinguish between sending and receiving processes. This is because collective primitives in MPI are executed by <emphasis>both</emphasis> the senders and the receivers, and the runtime decides whether it is a sender or a receiver by inspecting the <literal>rootRole</literal> parameter (which is a process rank) in the <literal>MPI_Scatter</literal> or <literal>MPI_Gather</literal> call. Otherwise the conversion is similar to their point-to-point counterparts in <link linkend="fig15_4">Figure <xref linkend="fig15_4" remap="15.4"/></link>.</para>
<fig id="fig15_9" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 15.9</label>
<caption><title>Collective operations: <emphasis role="romanAlt">Pabble</emphasis> collectives and their corresponding MPI backbones.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig092.jpg" mime-subtype="jpeg"/>
</fig>
</section>
<section class="lev2" id="sec15-6-7" label="15.6.7" xreflabel="15.6.7">
<title>Process Scaling</title>
<para>In addition to the translation of <emphasis role="romanAlt">Pabble</emphasis> statements into MPI code, we also define the process mapping between a <emphasis role="romanAlt">Pabble</emphasis> protocol and a <emphasis role="romanAlt">Pabble-</emphasis>generated MPI program. Typical usage of MPI programs can be parameterised on the number of spawned processes at runtime via program arguments. Hence, given a <emphasis role="romanAlt">Pabble</emphasis> protocol with <emphasis>scalable</emphasis> roles, we describe the rules below to map (parameterised) roles into MPI processes.</para>
<para>A <emphasis role="romanAlt">Pabble</emphasis> protocol for MPI code generation can contain any number of constant values (e.g. <literal>const M = 10</literal>), which are converted in the backbone as C constants (e.g. <literal>#define M 10</literal>), but it can use at most one <emphasis>scalable constant</emphasis> [<link linkend="ch15_b13">13</link>], and will scale with the total number of spawned processes. A scalable constant, defined in Section 15.5.1.1 as constant type (3), is written:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog213.jpg" mime-subtype="jpeg"/></para>
<para>The constant can then be used for defining parameterised roles, and used in indices of parameterised message interaction statements. For example, to declare an <emphasis>N</emphasis> <emphasis>&#x00D7;</emphasis> <emphasis>N</emphasis> role <literal>P</literal>, we write in the protocol:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog214.jpg" mime-subtype="jpeg"/></para>
<para>which results in a total of <emphasis>N</emphasis><sup>2</sup> participants in the protocol, but <emphasis>N</emphasis> is not known until execution time. MPI backbone code generated based on this <emphasis role="romanAlt">Pabble</emphasis> protocol uses <literal>N</literal> throughout. Since the only parameter in a scalable MPI program is its <literal>size</literal> (i.e. number of spawned processes), the following code is generated in the backbone to calculate, from <literal>size</literal>, the value of C local variable <literal>N</literal>:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog215.jpg" mime-subtype="jpeg"/></para>
</section>
</section>
<section class="lev1" id="sec15-7" label="15.7" xreflabel="15.7">
<title>Merging MPI Backbone and Kernels</title>
<section class="lev2" id="sec15-7-1" label="15.7.1" xreflabel="15.7.1">
<title>Annotation-Guided Merging Process</title>
<para>To combine the MPI backbone with the kernels, our aspect-oriented design-flow inserts kernel function calls into the MPI backbone code. The insertion points are realised as <literal>#pragma</literal>s in the MPI backbone code, generated from the input protocol as placeholders where functional code is inserted. There are multiple types of annotations whose syntax is given as:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog216.jpg" mime-subtype="jpeg"/></para>
<para>where <emphasis>entry point type</emphasis> is one of <literal>kernel</literal>, <literal>type</literal> or <literal>predicate</literal>, and <emphasis>entry point id</emphasis> is an alphanumeric identifier.</para>
</section>
<section class="lev2" id="sec15-7-2" label="15.7.2" xreflabel="15.7.2">
<title>Kernel Function</title>
<para><literal>#pragma pabble kernel Label</literal> defines the insertion point of kernel functions in the MPI backbone code. <literal>Label</literal> is the label of the interaction statement, e.g. <literal>Label(T)from Sender to Receiver</literal>, and the annotation is replaced by the kernel function associated to the label <literal>Label</literal>. Programmers must use the same pragma to manually annotate the implementation of the kernel function. The first row in <link linkend="tab15_1">Table <xref linkend="tab15_1" remap="15.1"/></link> shows an example.</para>
</section>
<section class="lev2" id="sec15-7-3" label="15.7.3" xreflabel="15.7.3">
<title>Datatypes</title>
<para><literal>#pragma pabble type TypeName</literal> annotates a generic type name in the backbone, and also annotates the concrete definition of the datatype in the kernels. In the second row of <link linkend="tab15_1">Table <xref linkend="tab15_1" remap="15.1"/></link>, the C datatype <literal>T</literal> is defined to be void since the protocol does not have any information to realise the type. The kernel defines <literal>T</literal> to be a concrete type of <literal>double</literal>, and hence our tool transforms the <literal>typedef</literal> in the backbone into <literal>double</literal> and infers the corresponding <literal>MPI_Datatype</literal> (MPI derived datatypes) to the built-in MPI integer primitive type, i.e. <literal>MPI_Datatype MPI_T = MPI_DOUBLE</literal>. From the given type we can also generate MPI datatypes for structures of primitive types, e.g. <literal>struct { int x, int y, double m }</literal> is transformed to its MPI-equivalent datatype.</para>
<fig id="tab15_1" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Table 15.1</label>
<caption><title>Annotations in backbone and kernel</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/tab007.jpg" mime-subtype="jpeg"/>
</fig>
</section>
<section class="lev2" id="sec15-7-4" label="15.7.4" xreflabel="15.7.4">
<title>Conditionals</title>
<para><literal>#pragma pabble predicate Label</literal> annotates predicates, e.g. loop conditions or if-conditions, in the backbone. Since a <emphasis role="romanAlt">Pabble</emphasis> communication protocol (and transitively, the MPI backbone) does not specify a loop condition, the default loop condition is <literal>1</literal>, i.e. always true. This annotation introduces a way to insert a conditional expression defined as a kernel function. It precedes the <literal>while</literal>-loop, as shown in the third row of <link linkend="tab15_1">Table <xref linkend="tab15_1" remap="15.1"/></link>, to label the loop with the name <literal>Label</literal>. The kernel function that defines expressions must use the same annotation as the backbone, e.g. <literal>#pragma pabble predicate Label</literal>. After the merge, this kernel function is called when the loop condition is evaluated.</para>
</section>
</section>
<section class="lev1" id="sec15-8" label="15.8" xreflabel="15.8">
<title>Related Work</title>
<para>The general approach of describing parallel patterns and reusing them with different computation modules can date back to [<link linkend="ch15_b4">4</link>] by Darlington et al., where parallel patterns are described as higher order <emphasis>skeleton</emphasis> functions, written in a functional language. Parallel applications are implemented as functions that combine with the skeletons and transformed. Their system targets specialised parallel machines, and our approach targets MPI, a standard for parallel programming in a range of hardware configurations. The approach, also known as <emphasis>algorithmic skeleton frameworks</emphasis> for parallel programming, is surveyed in [<link linkend="ch15_b7">7</link>]. Some of these tools also target MPI for high-level structured parallel programming, and only works with a limited set of parallel patterns. Our code generation workflow based on <emphasis role="romanAlt">Pabble</emphasis> supports generic patterns written in <emphasis role="romanAlt">Pabble</emphasis> and guarantees communication safety in the generated MPI code.</para>
</section>
<section class="lev1" id="sec15-9" label="15.9" xreflabel="15.9">
<title>Conclusion</title>
<para>In this chapter we presented a protocol-based workflow for constructing safe and efficient parallel applications. The framework consists of two parts, a safe-by-construction parallel interaction backbone, generated from the <emphasis role="romanAlt">Pabble</emphasis> protocol language, and an aspect-oriented compilation workflow to mechanically insert computation code into the backbone. Our approach simplifies parallel programming by making use of parallel communication patterns, described with our <emphasis role="romanAlt">Pabble</emphasis> protocol description language, and building independent kernel code around the patterns as sequential C code. This approach is flexible, where multiple sets of kernels can share a common parallel communication pattern, since the computation and the communication are maintained separately.</para>
<para><emphasis role="strong">Acknowledgements</emphasis> This work is supported EPSRC projects EP/K034413/1, EP/K011715/1, EP/L00058X/1 and EP/N027833/1; and by EU FP7 612985 (UpScale).</para>
</section>
<section class="lev1" id="sec2a">
<title>References</title>
<orderedlist numeration="arabic" continuation="restarts" spacing="normal">
<listitem><para><anchor id="ch15_b1"/>L. Bettini, M. Coppo, L. DAntoni, M. D. Luca, M. Dezani-Ciancaglini, and N. Yoshida. Global Progress in Dynamically Interleaved Multiparty Sessions. In <emphasis>CONCUR 2008</emphasis>, volume 5201 of <emphasis>LNCS</emphasis>. Springer, 2008.  <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=L%2E+Bettini%2C+M%2E+Coppo%2C+L%2E+DAntoni%2C+M%2E+D%2E+Luca%2C+M%2E+Dezani-Ciancaglini%2C+and+N%2E+Yoshida%2E+Global+Progress+in+Dynamically+Interleaved+Multiparty+Sessions%2E+In+CONCUR+2008%2C+volume+5201+of+LNCS%2E+Springer%2C+2008%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch15_b2"/>J. a. M. Cardoso, T. Carvalho, J. G. Coutinho, W. Luk, R. Nobre, P. Diniz, and Z. Petrov. LARA: an aspect-oriented programming language for embedded systems. In <emphasis>AOSD &#x2019;12</emphasis>. ACM Press, 2012. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=J%2E+a%2E+M%2E+Cardoso%2C+T%2E+Carvalho%2C+J%2E+G%2E+Coutinho%2C+W%2E+Luk%2C+R%2E+Nobre%2C+P%2E+Diniz%2C+and+Z%2E+Petrov%2E+LARA%3A+an+aspect-oriented+programming+language+for+embedded+systems%2E+In+AOSD+%2712%2E+ACM+Press%2C+2012%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch15_b3"/>P. Charles, C. Grothoff, V. Saraswat, C. Donawa, A. Kielstra, K. Ebcioglu, C. von Praun, and V. Sarkar. X10: an object-oriented approach to non-uniform cluster computing. In <emphasis>OOPSLA &#x2019;05</emphasis>. ACM Press, 2005. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=P%2E+Charles%2C+C%2E+Grothoff%2C+V%2E+Saraswat%2C+C%2E+Donawa%2C+A%2E+Kielstra%2C+K%2E+Ebcioglu%2C+C%2E+von+Praun%2C+and+V%2E+Sarkar%2E+X10%3A+an+object-oriented+approach+to+non-uniform+cluster+computing%2E+In+OOPSLA+%2705%2E+ACM+Press%2C+2005%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch15_b4"/>J. Darlington, A. Field, P. Harrison, P. H. J. Kelly, D. W. N. Sharp, and Q. Wu. Parallel programming using skeleton functions. In <emphasis>PARLE&#x2019;93</emphasis>, 1993. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=J%2E+Darlington%2C+A%2E+Field%2C+P%2E+Harrison%2C+P%2E+H%2E+J%2E+Kelly%2C+D%2E+W%2E+N%2E+Sharp%2C+and+Q%2E+Wu%2E+Parallel+programming+using+skeleton+functions%2E+In+PARLE%2793%2C+1993%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch15_b5"/>P.-M. Denielou, N. Yoshida, A. Bejleri, and R. Hu. Parameterised Multi-party Session Types. <emphasis>Logical Methods in Computer Science</emphasis>, 8(4):1&#x2013;46, October 2012. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=P%2E-M%2E+Denielou%2C+N%2E+Yoshida%2C+A%2E+Bejleri%2C+and+R%2E+Hu%2E+Parameterised+Multi-party+Session+Types%2E+Logical+Methods+in+Computer+Science%2C+8%284%29%3A1-46%2C+October+2012%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch15_b6"/>J. DeSouza, B. Kuhn, B. R. de Supinski, V. Samofalov, S. Zheltov, and S. Bratanov. Automated, scalable debugging of MPI programs with Intel Message Checker. In <emphasis>SE-HPCS &#x2019;05</emphasis>. ACM Press, 2005. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=J%2E+DeSouza%2C+B%2E+Kuhn%2C+B%2E+R%2E+de+Supinski%2C+V%2E+Samofalov%2C+S%2E+Zheltov%2C+and+S%2E+Bratanov%2E+Automated%2C+scalable+debugging+of+MPI+programs+with+Intel+Message+Checker%2E+In+SE-HPCS+%2705%2E+ACM+Press%2C+2005%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch15_b7"/>H. Gonz&#x00E1;lez-V&#x00E9;lez and M. Leyton. A Survey of Algorithmic Skeleton Frameworks: High-level Structured Parallel Programming Enablers. <emphasis>Softw. Pract. Exper.</emphasis>, 40(12):1135&#x2013;1160, 2010. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=H%2E+Gonz%E1lez-V%E9lez+and+M%2E+Leyton%2E+A+Survey+of+Algorithmic+Skeleton+Frameworks%3A+High-level+Structured+Parallel+Programming+Enablers%2E+Softw%2E+Pract%2E+Exper%2E%2C+40%2812%29%3A1135-1160%2C+2010%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch15_b8"/>W. Gropp, E. Lusk, and A. Skjellum. <emphasis>Using MPI: Portable Parallel Programming with the Message-Passing Interface</emphasis>. MIT Press, 1999.</para></listitem>
<listitem><para><anchor id="ch15_b9"/>K. Honda, N. Yoshida, and M. Carbone. Multiparty Asynchronous Session Types. <emphasis>JACM</emphasis>, 63(1):9:1&#x2013;9:67, 2016. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=K%2E+Honda%2C+N%2E+Yoshida%2C+and+M%2E+Carbone%2E+Multiparty+Asynchronous+Session+Types%2E+JACM%2C+63%281%29%3A9%3A1-9%3A67%2C+2016%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch15_b10"/>J. K. Lee and J. Palsberg. Featherweight X10. In <emphasis>PPoPP &#x2019;10</emphasis>. ACM Press, 2010.</para></listitem>
<listitem><para><anchor id="ch15_b11"/>J. Magee and J. Kramer. <emphasis>Concurrency &#x2013; state models and Java programs (2. ed.).</emphasis> Wiley, 2006. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=J%2E+Magee+and+J%2E+Kramer%2E+Concurrency+-+state+models+and+Java+programs+%282%2E+ed%2E%29%2E+Wiley%2C+2006%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch15_b12"/>N. Ng, J. G. Coutinho, and N. Yoshida. Protocols by Default: Safe MPI Code Generation based on Session Types. volume 9031 of <emphasis>LNCS</emphasis>. Springer, 2015.</para></listitem>
<listitem><para><anchor id="ch15_b13"/>N. Ng and N. Yoshida. Pabble: Parameterised Scribble for Parallel Programming. In <emphasis>PDP 2014</emphasis>, pages 707&#x2013;714, 2014. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=N%2E+Ng+and+N%2E+Yoshida%2E+Pabble%3A+Parameterised+Scribble+for+Parallel+Programming%2E+In+PDP+2014%2C+pages+707-714%2C+2014%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch15_b14"/>N. Ng and N. Yoshida. Pabble: parameterised Scribble. <emphasis>SOCA</emphasis>, 9(3&#x2013;4), 2015.</para></listitem>
<listitem><para><anchor id="ch15_b15"/>Pabble project on GitHub. <literal>https://github.com/pabble-lang</literal></para></listitem>
<listitem><para><anchor id="ch15_b16"/>Scribble homepage. <literal>http://scribble.org/</literal> <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Scribble+homepage%2E+http%3A%2F%2Fscribble%2Eorg%2F" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch15_b17"/>X10 homepage. <literal>http://x10-lang.org</literal></para></listitem>
<listitem><para><anchor id="ch15_b18"/>N. Yoshida, R. Hu, R. Neykova, and N. Ng. The Scribble Protocol Language. In <emphasis>TGC 2013</emphasis>, volume 8358 of <emphasis>LNCS</emphasis>. Springer, 2013.</para></listitem>
</orderedlist>
</section>
</chapter>
<chapter class="chapter" id="ch16" label="16" xreflabel="16">
<title>Deductive Verification of MPI Protocols</title>
<para><emphasis role="strong">Vasco T. Vasconcelos</emphasis><footnote id="fn16_1a" label="1"><para>LaSIGE, Faculty of Sciences, University of Lisbon, PT</para></footnote><emphasis role="strong">, Francisco Martins</emphasis><emphasis role="strong"><sup>1</sup></emphasis><emphasis role="strong">, Eduardo R. B. Marques</emphasis><footnote id="fn16_2a" label="2"><para>CRACS/INESC-TEC, Faculty of Sciences, University of Porto, PT</para></footnote><emphasis role="strong">, Nobuko Yoshida</emphasis><footnote id="fn16_3a" label="3"><para>Imperial College London, UK</para></footnote><emphasis role="strong">and Nicholas Ng</emphasis><footnoteref linkend="fn16_3a" label="3"/></para>
<section class="lev2">
<title>Abstract</title>
<para>This chapter presents the PARTYPES framework to statically verify C programs that use the Message Passing Interface, the widely used standard for message-based parallel applications. Programs are checked against a protocol specification that captures the interaction in an MPI program. The protocol language is based on a dependent type system that is able to express various MPI communication primitives, including point-to-point and collective operations. The verification uses VCC, a mechanical verifier for concurrent C programs. It takes the program protocol written in VCC format, an annotated version of the MPI library, and the program to verify, and checks whether the program complies with the protocol.</para>
</section>
<section class="lev1" id="sec16-1" label="16.1" xreflabel="16.1">
<title>Introduction</title>
<para>Message Passing Interface (MPI) [<link linkend="ch16_b3">3</link>] is a portable message-passing API for programming parallel computers running on distributed memory systems. To these days, MPI remains the dominant framework for developing high performance parallel applications.</para>
<para>Usually written in C or Fortran, MPI programs call library functions to perform point-to-point send/receive operations, collective and synchronisation operations (such as broadcast and barrier), and combination of partial results of computations (gather and reduce operations). Developing MPI applications is an error-prone endeavour. For instance, it is quite easy to write programs that cause processes to wait indefinitely for a message, or that exchange data of unexpected sorts or lengths.</para>
<para>Verifying that MPI programs are exempt from communication errors is far from trivial. The state-of-the-art verification tools for MPI programs use advanced techniques such as runtime verification [<link linkend="ch16_b7">7</link>, <link linkend="ch16_b15">15</link>, <link linkend="ch16_b16">16</link>, <link linkend="ch16_b21">21</link>] and model checking [<link linkend="ch16_b5">5</link>&#x2013;<link linkend="ch16_b7">7</link>, <link linkend="ch16_b15">15</link>, <link linkend="ch16_b17">17</link>, <link linkend="ch16_b20">20</link>]. These approaches frequently stumble upon the problem of scalability since the search space grows exponentially with the number of processes. It is often the case that the verification of real applications limits the number of processes to less than a dozen [<link linkend="ch16_b18">18</link>].</para>
<para>We approach the problem of verifying C+MPI code using a type theory for parallel programs. In our framework&#x2014;PARTYPES&#x2014;types describe the communication behaviour programs, that is, protocols. Programs that conform to one such type are guaranteed to follow the protocol and not to run into deadlocks. The verification is scalable, as it does not depend on the number of processes or other input parameters.</para>
<para>A previous work introduces the type theory underlying protocol spec-ification, shows the soundness of the methodology by designing a core language for parallel programming and proving a progress result for well-typed programs, and provides a comparative evaluation of PARTYPES against other state-of-the-art tools [<link linkend="ch16_b10">10</link>].</para>
<para>This chapter takes a pragmatic approach to the verification of C+MPI code, by explaining the procedure from the point of view of someone interested in verifying actual code, omitting theoretic technical details altogether. Protocols are written in a dependent type language that includes specific constructors for some of the most common communication primitives found in MPI programs. The conformance of a program against a protocol is checked using VCC, a software verifier for the C programming language [<link linkend="ch16_b1">1</link>]. In a nutshell, one checks C+MPI source code against a protocol as follows:</para>
<orderedlist numeration="arabic" continuation="restarts" spacing="normal">
<listitem><para>Write a protocol for the program, that can be translated mechanically to VCC format;</para></listitem>
<listitem><para>Introduce special, concise marks in the C+MPI source code to guide the automatic generation of VCC annotations required for verification;</para></listitem>
<listitem><para>Use the VCC tool to check conformance of the source code against the protocol.</para></listitem>
</orderedlist>
<para>If VCC runs successfully, then the program is guaranteed to follow the protocol and to be exempt from deadlocks, regardless of the number of processes, problem dimension, number of iterations, or any other parameters. The verification process is guided by two tools&#x2014;the Protocol Compiler and the Annotation Generator&#x2014;and by the PARTYPES MPI library. All these can be found at the PARTYPES website [<link linkend="ch16_b14">14</link>]. The tools and the library almost completely insulate the user from working with the VCC language.</para>
<para>The rest of this chapter is organised as follows. The next section introduces a running example and discusses typical faults found in MPI programs. Then Section 16.3 describes the protocol language and Sections 16.4 and 16.5 provide an overview of the verification process. Section 16.6 discusses related work and Section 16.7 concludes the chapter.</para>
</section>
<section class="lev1" id="sec16-2" label="16.2" xreflabel="16.2">
<title>The Finite Differences Algorithm and Common Coding Faults</title>
<para>This section introduces a running example and discusses common pitfalls encountered when developing MPI programs.</para>
<para>The <emphasis>finite differences</emphasis> algorithm computes an approximation of derivatives by the finite difference method. Given an initial vector <emphasis>X</emphasis><subscript>0</subscript>, the algorithm calculates successive approximations to the solution <emphasis>X</emphasis><subscript>1</subscript><emphasis>,X</emphasis><subscript>2</subscript><emphasis>,...</emphasis> , until a predefined maximum number of iterations has been reached. A distinguished process, say the one with rank <emphasis role="roman">0</emphasis>, disseminates the problem size (that is, the length of array <emphasis>X</emphasis>) through a broadcast operation. The same process then divides the input array among all processes. Each participant is responsible for computing its local part of the solution. When the pre-defined number of iterations is reached, process rank <literal>0</literal> obtains the global error through a reduce operation and collects the partial arrays in order to build a solution to the problem (<link linkend="fig16_1">Figure <xref linkend="fig16_1" remap="16.1"/></link>, left). In order to compute its part of the solution, each process exchanges boundary values with its left and right neighbours on every iteration (<link linkend="fig16_1">Figure <xref linkend="fig16_1" remap="16.1"/></link>, right).</para>
<para><link linkend="fig16_2">Figure <xref linkend="fig16_2" remap="16.2"/></link> shows C+MPI source code that implements the finite differences algorithm, adapted from a textbook [<link linkend="ch16_b4">4</link>]. The <literal>main</literal> function describes the behaviour of all processes together; the behaviour of each individual process may diverge based on its process number, designated by <emphasis>rank</emphasis>, and set on line 5 using the <literal>MPI_Comm_rank</literal> primitive. The number of processes (<literal>procs</literal> in the figure) is obtained through primitive <literal>MPI_Comm_size</literal> on line 6. Rank <literal>0</literal> starts by reading the problem size and the corresponding input vector <emphasis>X</emphasis><subscript>0</subscript> (lines 8&#x2013;9, variables <literal>n</literal> and <literal>work</literal>). The same participant then broadcasts the problem size (line 11, call to <literal>MPI_Bcast</literal>) and distributes the input vector to all other participants (line 13, call to <literal>MPI_Scatter</literal>).</para>
<fig id="fig16_1" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 16.1</label>
<caption><title>Communication pattern for the finite differences algorithm.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig093.jpg" mime-subtype="jpeg"/>
</fig>
<fig id="fig16_2" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 16.2</label>
<caption><title>Excerpt of an MPI program for the finite differences problem.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig094.jpg" mime-subtype="jpeg"/>
</fig>
<para>Each participant is then responsible for computing its part of the solution. The program enters a loop (lines 16&#x2013;35), specifying point-to-point message exchanges (<literal>MPI_Send</literal>, <literal>MPI_Recv</literal>) between each process and its <literal>left</literal> and <literal>right</literal> neighbours, based on a ring topology. The various message exchanges distribute boundary (<literal>local[0]</literal> and <literal>local[local_n+1]</literal>) values necessary to local calculations. Different send/receive orders for different ranks (lines 19&#x2013;22, lines 24&#x2013;27, and lines 29&#x2013;32) aim at avoiding deadlock situations (<literal>MPI_Send</literal> and <literal>MPI_Recv</literal> are blocking, synchronous, unbuffered operations). The loop ends when a pre-defined number of iterations is attained. Once the loop is over, rank <literal>0</literal> computes the global error through a reduction operation (<literal>MPI_reduce</literal>, line 36) and gathers the solution obtaining from each process (including itself) a part of the vector (<literal>MPI_Gather</literal>, line 37).</para>
<para>For space reasons we have omitted a few actual parameters in some calls to MPI operations: the ellipsis in <link linkend="fig16_2">Figure <xref linkend="fig16_2" remap="16.2"/></link> denote parameters <literal>0</literal> (the message tag number) and <literal>MPI_COMM_WORLD</literal> (the communicator) in all operations, except in <literal>MPI_Recv</literal> where they denote, in addition, parameter <literal>&#x0026;status</literal>.</para>
<para>The code in <link linkend="fig16_2">Figure <xref linkend="fig16_2" remap="16.2"/></link> is extremely sensitive to variations in the structure of MPI operations. We distinguish five kinds of situations that are further discussed below:</para>
<orderedlist numeration="arabic" continuation="restarts" spacing="normal">
<listitem><para>Type mismatches in messages,</para></listitem>
<listitem><para>Array length mismatches in messages,</para></listitem>
<listitem><para>Missing send or receive operations,</para></listitem>
<listitem><para>Wrong send-receive order in messages, and</para></listitem>
<listitem><para>Incompatible MPI operations for the different processes.</para></listitem>
</orderedlist>
<para>The first two situations are related to how MPI primitives describe data transmitted in messages: usually in the form of a pointer to a buffer, the length of the buffer, and the type of elements in the buffer. A <emphasis>type mismatch</emphasis> in a message exchange occurs when, for example, one replaces <literal>MPI_FLOAT</literal> by <literal>MPI_DOUBLE</literal> in line 19. Then process rank <literal>0</literal> sends a value of type <literal>double</literal>, while process rank <literal>procs-1</literal> expects a <literal>float</literal>. An <emphasis>array length mismatch</emphasis> happens, for example, if one replaces <literal>1</literal> with <literal>2</literal> as the second parameter on line 19. Then process rank <literal>0</literal> sends two floating point numbers, while process rank <literal>procs-1</literal> expects exactly one (line 24). It should be emphasised that these mismatches are caught at runtime, if caught at all.</para>
<para>The last three cases all lead to <emphasis>deadlocks</emphasis>. In general, MPI programs enter deadlocked situations when a communication operation is not matched by all the processes involved. For example, the <emphasis>omission of the send operation</emphasis> on line 19 will leave process rank <literal>procs-1</literal> eternally waiting for a message to come on line 24. For another example, <emphasis>exchanging the two receive operations</emphasis> in lines 21 and 22 leads to a deadlock where ranks <literal>0</literal> and <literal>1</literal> will be forever waiting for one another.</para>
<para><emphasis>Incompatible MPI operations</emphasis> for the different processes come in different flavours. For example, replacing the receive operation by an <literal>MPI_Bcast</literal> on line 24 leads to a situation where process rank <literal>0</literal> tries to send a message, while rank <literal>procs-1</literal> tries to broadcast. For another example, replace the root process of the reduce operation at line 36 from <literal>0</literal> to <literal>rank</literal>. We are left with a situation where each process executes a different reduce operation, each trying to collect the maximum of the values provided by all processes. For a last example, enclose the gather operation on line 37 by a conditional of the form <literal>if(rank == 0)</literal>. In this case process rank <literal>0</literal> will be forever waiting for the remaining processes to provide their parts of the array.</para>
</section>
<section class="lev1" id="sec16-3" label="16.3" xreflabel="16.3">
<title>The Protocol Language</title>
<para>This section introduces the protocol language, following a step-by-step construction of the protocol for our running example.</para>
<para>In the beginning, process rank 0 broadcasts the problem size, a natural number. We write this as</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog217.jpg" mime-subtype="jpeg"/></para>
<para>That process rank 0 divides <emphasis>X</emphasis><subscript>0</subscript> (an array of floating pointing numbers) among all processes is described by a <literal>scatter</literal> operation.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog218.jpg" mime-subtype="jpeg"/></para>
<para>Now, each process loops for a given number of iterations, <literal>nIterations</literal>. We write this as follows.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog219.jpg" mime-subtype="jpeg"/></para>
<para>Variable <literal>nIterations</literal> must be somehow introduced in the protocol. It denotes a value that must be known to all processes. Typically, there are two ways for processes to get to know this value:</para>
<itemizedlist mark="bullet" spacing="normal">
<listitem><para>The value is exchanged, resorting to a collective communication operation, in such a way that <emphasis>all</emphasis> processes get to know it, or</para></listitem>
<listitem><para>The value is known to all processes before computation starts, for example because it is hardwired in the source code or is read from the command line.</para></listitem>
</itemizedlist>
<para>In the former case we could add another <literal>broadcast</literal> operation in the first lines of the protocol. In the latter case, the protocol language relies on the <literal>val</literal> constructor, allowing a value to be introduced in the program:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog220.jpg" mime-subtype="jpeg"/></para>
<para>Either solution would solve the problem. If a <literal>broadcast</literal> is used then processes must engage in a broadcast operation; if <literal>val</literal> is chosen then no value exchange is needed, but the programmer must identify the value in the source code that will replace variable <literal>nIterations</literal>.</para>
<para>We may now continue analysing the loop body (<link linkend="fig16_2">Figure <xref linkend="fig16_2" remap="16.2"/></link>, lines 17&#x2013; 34). In each iteration, each process sends a message to its left neighbour and another message to its right neighbour. Such an operation is again described as a <literal>foreach</literal> construct that iterates over all processes. The first process is <literal>0</literal>; the last is <literal>size-1</literal>, where <literal>size</literal> is a distinguished variable that represents the number of processes. The inner loop is then written as follows.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog221.jpg" mime-subtype="jpeg"/></para>
<para>When <literal>i</literal> is the rank of a process, a conditional expression of the form <literal>i=size-1 ? 0 : i+1</literal> denotes the process&#x2019; right neighbour. Similarly, the left neighbour is <literal>i=0 ? size-1 : i-1</literal>.</para>
<para>To send a message from process rank <literal>r1</literal> to process rank <literal>r2</literal> containing a value of a datatype <literal>D</literal>, we write <literal>message r1 r2 D</literal>. In this way, to send a message containing a floating point number to the left process, followed by a message to the right process, we write.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog222.jpg" mime-subtype="jpeg"/></para>
<para>So, now we can assemble the loops.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog223.jpg" mime-subtype="jpeg"/></para>
<para>Once the loop is completed, process rank 0 obtains the global error. Towards this end, each process proposes a floating point number representing the local error. Rank 0 then reads the maximum of all these values. We write all this as follows:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog224.jpg" mime-subtype="jpeg"/></para>
<para>Finally, process rank 0 collects the partial arrays and builds a solution <emphasis>X</emphasis><emphasis><subscript>n</subscript></emphasis> to the problem. This calls for a <literal>gather</literal> operation.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog225.jpg" mime-subtype="jpeg"/></para>
<para>Before we put all the operations together in a protocol, we need to discuss the nature of the arrays distributed and collected in the <literal>scatter</literal> and <literal>gather</literal> operations. In brief, the <literal>scatter</literal> operation distributes <emphasis>X</emphasis><subscript>0</subscript>, dividing it in small pieces, while <literal>gather</literal> collects the subarrays to build <emphasis>X</emphasis><emphasis><subscript>n</subscript></emphasis>. So, we instead write:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog226.jpg" mime-subtype="jpeg"/></para>
<para>Variable <literal>n</literal>, describing the length of the global array, must be introduced in the protocol. This is typically achieved by means of a <literal>val</literal> or a <literal>broadcast</literal> operation. In this case <literal>n</literal> stands for the problem size that was broadcast before. So we name the value that rank <literal>0</literal> provides as follows.</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog227.jpg" mime-subtype="jpeg"/></para>
<para>But <literal>n</literal> cannot be an arbitrary non-negative number. It must evenly divide <emphasis>X</emphasis><subscript>0</subscript>. In this way, each process gets a part of <emphasis>X</emphasis><subscript>0</subscript> of equal length, namely <literal>length(X0)/size</literal>, and we do not risk accessing out-of-bound positions when manipulating the subarrays. So we would like to make sure that the length of <emphasis>X</emphasis><subscript>0</subscript> equal divides the number of processes. For this we use a <emphasis>refinement</emphasis> datatype. Rather that saying that <literal>n</literal> is a natural number we say that it is of datatype <literal>{x: natural | x % size = 0}</literal>. The complete protocol is in <link linkend="fig16_3">Figure <xref linkend="fig16_3" remap="16.3"/></link>.</para>
<para>As an aside, <literal>natural</literal> can be expressed as <literal>{x: integer | x &gt;= 0}</literal>. Similarly, <literal>positive</literal> abbreviates <literal>{x: integer | x &gt; 0}</literal>, and <literal>float[n]</literal> abbreviates a refinement type of the form <literal>{x: float[] | length(x) = n}</literal>.</para>
<para>Further examples of protocols can be found in a previous work [<link linkend="ch16_b10">10</link>] and at the PARTYPES web site [<link linkend="ch16_b14">14</link>]. The current version protocol language supports:</para>
<fig id="fig16_3" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 16.3</label>
<caption><title>Protocol for the finite differences algorithm.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig095.jpg" mime-subtype="jpeg"/>
</fig>
<itemizedlist mark="bullet" spacing="normal">
<listitem><para>Different MPI communication primitives such as <literal>message</literal>, <literal>broadcast</literal>, <literal>reduce</literal>, <literal>allreduce</literal>, <literal>scatter</literal>, <literal>gather</literal>,and <literal>allgather</literal>;</para></listitem>
<listitem><para>Control flow primitives, including sequential composition (<literal>;</literal>), primitive recursion (<literal>foreach</literal>), conditional (<literal>if</literal>-<literal>then</literal>-<literal>else</literal>), and <literal>skip</literal> (that is, the empty block of operations).</para></listitem>
</itemizedlist>
<para>Protocols are subject to certain formation rules [<link linkend="ch16_b10">10</link>], including:</para>
<itemizedlist mark="bullet" spacing="normal">
<listitem><para>Variables must be properly introduced with <literal>val</literal>, <literal>broadcast</literal>, <literal>allreduce</literal>;</para></listitem>
<listitem><para>Ranks must lie between <literal>0</literal> and <literal>size-1</literal>;</para></listitem>
<listitem><para>The two ranks in a <literal>message</literal> must be different;</para></listitem>
<listitem><para>The length of arrays in <literal>scatter</literal> and <literal>gather</literal> must equally divide <literal>size</literal>.</para></listitem>
</itemizedlist>
<para>The P<emphasis role="smaller">ROTOCOL</emphasis>C<emphasis role="smaller">OMPILER</emphasis> checks protocol formation and, in addition, generates a C header file containing the VCC code that describes the protocol. The tool comes as an Eclipse plugin; it may alternatively be used on a web browser from the P<emphasis role="smaller">AR</emphasis>T<emphasis role="smaller">YPES</emphasis> web page [<link linkend="ch16_b14">14</link>]. <link linkend="fig16_4">Figure <xref linkend="fig16_4" remap="16.4"/></link> shows a screenshot of Eclipse when the compiler did not manage to prove that the value of expression <literal>i=size ? 0 : i+1</literal> lies between <literal>0</literal> and <literal>size-1</literal>.</para>
<fig id="fig16_4" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 16.4</label>
<caption><title>Protocol compiler running under the Eclipse IDE.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig096.jpg" mime-subtype="jpeg"/>
</fig>
</section>
<section class="lev1" id="sec16-4" label="16.4" xreflabel="16.4">
<title>Overview of the Verification Procedure</title>
<para>This section and the next present the PARTYPES methodology. <link linkend="fig16_5">Figure <xref linkend="fig16_5" remap="16.5"/></link> illustrates the workflow of the verification procedure. Two inputs are required:</para>
<itemizedlist mark="bullet" spacing="normal">
<listitem><para>The C+MPI source code (example in <link linkend="fig16_2">Figure <xref linkend="fig16_2" remap="16.2"/></link>);</para></listitem>
<listitem><para>The protocol for the program (example in <link linkend="fig16_3">Figure <xref linkend="fig16_3" remap="16.3"/></link>).</para></listitem>
</itemizedlist>
<para>First, the C+MPI source code must be adapted for verification, the reason being that VCC accepts only a subset of the C programming language. Then, special <emphasis>marks</emphasis> are inserted in the C source code. One of our tools, the ANNOTATIONGENERATOR (<emphasis role="romanAlt">AG</emphasis> in the figure), expands the marks. The output is C source code with VCC annotations, which we denote by C+MPI+VCC. The VCC annotations allow the verification of the C code against the protocol.</para>
<para>A second tool, the PROTOCOLCOMPILER (<emphasis role="romanAlt">PC</emphasis> in the figure), checks protocol formation and generates a C header file containing the protocol in VCC format. At this point two C header files need to be included in the C source code: the PARTYPES MPI library, and the protocol in VCC format. The PARTYPES MPI library, <literal>mpi.h</literal>, is a surrogate C header file containing the type theory (as described in a previous work [<link linkend="ch16_b10">10</link>]) in VCC format and available at PARTYPES web page [<link linkend="ch16_b14">14</link>].</para>
<para>The C code is now ready to be submitted to VCC. The outcome is one of three situations:</para>
<itemizedlist mark="bullet" spacing="normal">
<listitem><para>VCC signals success. We know that the C+MPI code, as is, conforms to the protocol, hence is exempt from all the problems discussed in Section 16.2;</para>
<fig id="fig16_5" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 16.5</label>
<caption><title>Workflow of the verification procedure for C+MPI programs.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig097.jpg" mime-subtype="jpeg"/>
</fig></listitem>
<listitem><para>VCC complains presenting the list of failed assertions. In this case, the source of the problem may lie at three different places:</para>
<itemizedlist mark="bullet" spacing="normal">
<listitem><para>the protocol does not capture the communication pattern of the program and needs to be rectified;</para></listitem>
<listitem><para>the C+MPI program is not well annotated, either because it needs additional marks or because some existing marks are misplaced;</para></listitem>
<listitem><para>the C+MPI program itself has a fault that needs to be fixed. In our example, the problem size (stored in variable <literal>n</literal>) must be a multiple of the number of processes (stored in variable <literal>procs</literal>), so that the source code may conform to the protocol. Since the problem size is the value of function <literal>read_problem_size</literal> (line 8, <link linkend="fig16_2">Figure <xref linkend="fig16_2" remap="16.2"/></link>), we may add an explicit contract to the function:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog228.jpg" mime-subtype="jpeg"/></para>
<para>In such cases PARTYPES users must make use of the VCC specification language.</para></listitem>
</itemizedlist></listitem>
<listitem><para>VCC times out. This situation typically happens when the underlying SMT solver fails to check some refinement condition. The PARTYPES user should revise protocol refinements and possibly rewrite them. For instance, to describe that the process with rank <literal>i</literal> sends a floating point value to its right neighbour in a ring topology, we could have written</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog229.jpg" mime-subtype="jpeg"/></para>
<para>It is well-known that non-linear integer arithmetics is undecidable in general and inefficiently supported by SMT solvers. Expressions such as <literal>(i+1)%size</literal> may complicate the verification procedure, possibly leading to timeouts. Instead, we include in our protocol (<link linkend="fig16_3">Figure <xref linkend="fig16_3" remap="16.3"/></link>) an equivalent proposition that is more amenable for the solver, namely, <literal>i=size-1 ? 0 : i+1</literal>.</para></listitem>
</itemizedlist>
<para>The rest of this section describes the source code adaptation required to run VCC. In general, the original C+MPI source code requires routine adjustments in order to be accepted by VCC. Adjustments comprise the deletion or the replacement of code that is not supported by VCC. In particular we:</para>
<itemizedlist mark="bullet" spacing="normal">
<listitem><para>delete functions with a variable number of arguments (such as <literal>printf</literal> and <literal>scanf</literal>);</para></listitem>
<listitem><para>suppress all floating point arithmetic;</para></listitem>
<listitem><para>replace multidimensional by single dimensional arrays and adjust the code accordingly.</para></listitem>
</itemizedlist>
<para>VCC is a verifier for concurrent C. Even though C+MPI code is generally single-threaded, VCC tries to verify that the source code is thread-safe in any concurrent environment that respects the contracts on its functions and data structures. This complicates the verification process and demands additional VCC annotations that are not directly related to the verification of the adherence of code to protocols. In particular, the PARTYPES user needs to guarantee that memory accesses do not introduce data races. He does so by proving that memory locations are not concurrently written (i.e., <literal>\thread_local</literal> in VCC terms) upon reading, and not concurrently written or read upon writing (<literal>\mutable</literal> or <literal>\writable</literal>).</para>
<para>In our running example, and in order to facilitate the explanation and to concentrate on the adherence to the protocol, we inlined all subsidiary functions in the <literal>main</literal> function, made all arrays local to <literal>main</literal>, and omitted the code concerned with the actual computation of the finite differences. This greatly simplifies the annotation process as we must only deal with local memory, and do not have to cope with other verification demands such as maintaining loop invariants or proving that integer arithmetics does not overflow. Such adjustments must be exerted with great care so as not to alter the interactive behaviour of the original program.</para>
</section>
<section class="lev1" id="sec16-5" label="16.5" xreflabel="16.5">
<title>The Marking Process</title>
<para>This section completes the PARTYPES methodology for checking C+MPI code by addressing the marking step.</para>
<para>In general, simple protocols require no marks. Imagine the protocol</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog230.jpg" mime-subtype="jpeg"/></para>
<para>describing a simple algorithm where each process computes its part of the solution and process rank <literal>0</literal> collects the solution by adding the parts. Because the protocol uses a simple communication primitive no source code marking is required.</para>
<para>We require no marking for the MPI primitives supported by PARTYPES since their usage is taken care of by the contracts provided in the PARTYPES MPI library (<literal>mpi.h</literal>). The PARTYPES user must aid verification through appropriate marks when more advanced protocol features come into play, such as dependent functions (<literal>val</literal>), primitive recursion (<literal>foreach</literal>), and conditionals (<literal>if</literal>-<literal>then</literal>-<literal>else</literal>).</para>
<para>We start with <literal>val</literal>. We have seen in Section 16.3 that this primitive introduces a constant in the protocol:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog231.jpg" mime-subtype="jpeg"/></para>
<para>Users must provide the actual program value for <literal>nIterations</literal>. Analysing the code in <link linkend="fig16_2">Figure <xref linkend="fig16_2" remap="16.2"/></link>, one realises that the protocol variable <literal>nIterations</literal> corresponds to the program constant <literal>NUM_ITER</literal>. We then add the mark</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog232.jpg" mime-subtype="jpeg"/></para>
<para>after the three MPI initialisation primitives (<literal>MPI_Init</literal>, <literal>MPI_Comm_rank</literal>, and <literal>MPI_Comm_size</literal>), that is, after line 6.</para>
<para>Next, we address <literal>foreach</literal>. Again, we seek the assistance of the user in pointing out the portion of the source code that matches each occurrence of this primitive. In the protocol of <link linkend="fig16_3">Figure <xref linkend="fig16_3" remap="16.3"/></link>, loop</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog233.jpg" mime-subtype="jpeg"/></para>
<para>is meant to be matched against the <literal>for</literal> loop in <link linkend="fig16_2">Figure <xref linkend="fig16_2" remap="16.2"/></link> starting at line 16. We then introduce the mark</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog234.jpg" mime-subtype="jpeg"/></para>
<para>just before the body of the <literal>for</literal> loop, thus associating the protocol loop variable and its bounds with those in the C code.</para>
<para>For the inner loop in the protocol (that is, lines 6&#x2013;9 in <link linkend="fig16_3">Figure <xref linkend="fig16_3" remap="16.3"/></link>) we could proceed similarly would the source code be perfectly aligned with the protocol, as in the excerpt below meant to replace lines 18&#x2013;33 in <link linkend="fig16_2">Figure <xref linkend="fig16_2" remap="16.2"/></link>:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog235.jpg" mime-subtype="jpeg"/></para>
<para>However, efficient implementations do not exhibit loops to implement this kind of <literal>foreach</literal> protocols. The loop in the protocol states that each process (<literal>0</literal>, ..., <literal>size-1</literal>) must send a message to its left and to its right neighbour. This means that each process will be involved in <emphasis>exactly four</emphasis> message passing operations: send left, send right, receive from left, receive from right. Therefore the above <literal>for</literal> loop can be <emphasis>completely unrolled</emphasis> into a series of conditional instructions, each featuring two message send and two message receive operations, as witnessed by the code in <link linkend="fig16_2">Figure <xref linkend="fig16_2" remap="16.2"/></link>, lines 18&#x2013;33.</para>
<para>How do we check <literal>foreach</literal> protocols against conditional instructions in source code? A possible approach would be to let the verifier unroll the protocol loop. This may work when <literal>size</literal> is known to be a small natural number. In general, however, protocols do not fix the number of processes. That is the case with our running example which must run on any number of processes (starting at 2, for processes cannot send messages to themselves). In such cases VCC takes <literal>size</literal> to be a 64 bits non-negative integer. This poses significant difficulties to the unrolling process both in terms of memory and verification time.</para>
<para>In the running example, the apparent mismatch between the protocol and the program is that there are three different behaviours in the program depending on the rank (<link linkend="fig16_2">Figure <xref linkend="fig16_2" remap="16.2"/></link>, lines 18&#x2013;33), while the protocol specifies a single behaviour, namely:</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog236.jpg" mime-subtype="jpeg"/></para>
<para>At first sight, it may seem as if the protocol does not specify the required diversity of behaviours, but in fact it does. To see why, let us unroll the inner <literal>foreach</literal> loop. This is what we get when we omit the type of the message (<literal>float</literal>):</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog237.jpg" mime-subtype="jpeg"/></para>
<para>From the unrolled protocol we conclude that the behaviour of process rank <literal>0</literal> is the following:</para>
<orderedlist numeration="arabic" continuation="restarts" spacing="normal">
<listitem><para>send a message to its left neighbour (<literal>size-1</literal>);</para></listitem>
<listitem><para>send a message to its right neighbour (<literal>1</literal>);</para></listitem>
<listitem><para>receive a message from its right neighbour; and, finally,</para></listitem>
<listitem><para>receive a message from its left neighbour.</para></listitem>
</orderedlist>
<para>The behaviour is straightforward to obtain: just identify the messages that mention rank 0, and use a send when <literal>0</literal> is the source of the <literal>message</literal> or a receive otherwise. This exactly coincides with the four send/receive operations in the C code for rank <literal>0</literal>, lines 19&#x2013;22.</para>
<para>For the last rank (that is, <literal>size-1</literal>) the relevant send/receive operations are the following:</para>
<orderedlist numeration="arabic" continuation="restarts" spacing="normal">
<listitem><para>receive a message from its right neighbour (<literal>0</literal>);</para></listitem>
<listitem><para>receive a message from its left neighbour (<literal>size-2</literal>),</para></listitem>
<listitem><para>send a message to its left neighbour; and, finally,</para></listitem>
<listitem><para>send a message to its right neighbour.</para></listitem>
</orderedlist>
<para>This pattern coincides with the source code, lines 24&#x2013;27. All other behaviours (when rank is between <literal>1</literal> and <literal>size-2</literal>) are similarly obtained and are left as an exercise for the interested reader. The pattern thus obtained should match the code, lines 29&#x2013;32. Notice that the order of the messages is important, and that we have identified as many behaviours as there are conditional branches in the source code (lines 18&#x2013;33).</para>
<para>Based on this analysis, and in order to guide the verification process we seek the help of the user by selecting the relevant <literal>foreach</literal> steps (iterations) in each branch of the program. A <emphasis>relevant step</emphasis> for rank <literal>k</literal> corresponds to one <literal>foreach</literal> iteration where either the source or the target of a <literal>message</literal> appearing in the loop body is <literal>k</literal>. A step that does not mention <literal>k</literal> (as source or target) is equivalent to <literal>skip</literal>, the empty protocol, and hence irrelevant for verification purposes. In order to check that all non-relevant steps are <literal>skip</literal>, we must provide the loop bounds (<literal>0</literal> and <literal>procs-1</literal> in this case), in addition to the relevant steps.</para>
<para>For example, when rank is <literal>0</literal> the relevant steps are when <literal>i</literal> is equal to <literal>rank</literal>, <literal>right</literal>,and <literal>left</literal>, in this order. So we insert the mark</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog238.jpg" mime-subtype="jpeg"/></para>
<para>just before the code block in lines 19&#x2013;22. For rank equal to <literal>size-1</literal> the relevant steps are the <literal>right</literal>, the <literal>left</literal>, and the <literal>rank</literal>, again in this order. The required mark at line 23 is</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog239.jpg" mime-subtype="jpeg"/></para>
<para>and the annotation to include in line 28 is</para>
<para><graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/prog240.jpg" mime-subtype="jpeg"/></para>
<para><link linkend="fig16_6">Figure <xref linkend="fig16_6" remap="16.6"/></link> presents the marked version of the program in full.</para>
<fig id="fig16_6" position="float" xmlns:xlink="http://www.w3.org/1999/xlink">
<label>Figure 16.6</label>
<caption><title>The code of <link linkend="fig16_2">Figure <xref linkend="fig16_2" remap="16.2"/></link> with verification marks inserted.</title></caption>
<graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="graphics/fig098.jpg" mime-subtype="jpeg"/>
</fig>
</section>
<section class="lev1" id="sec16-6" label="16.6" xreflabel="16.6">
<title>Related Work</title>
<para>There are different aims and different methodologies for the verification of MPI programs [<link linkend="ch16_b6">6</link>]. The verification of interaction-based properties typically seeks to establish the absence of deadlocks and otherwise ill-formed communications among processes (e.g., compatible arguments at both ends in a point-to-point communication, in close relation to type checking safe communication). Several tools exist with this purpose, either for static or runtime verification, usually employing techniques from the realm of model checking and/or symbolic execution. All these tools are hindered by the inherent scalability and state-explosion problems. Notable examples include CIVL [<link linkend="ch16_b19">19</link>], DAMPI [<link linkend="ch16_b21">21</link>], ISP [<link linkend="ch16_b15">15</link>], MOPPER [<link linkend="ch16_b2">2</link>], MUST [<link linkend="ch16_b7">7</link>], and TASS [<link linkend="ch16_b20">20</link>].</para>
<para>In contrast to these tools, PARTYPES follows a deductive verification approach with the explicit aim of attaining scalable verification. A previous work [<link linkend="ch16_b10">10</link>] conducts a comparative evaluation by benchmarking PARTYPES against three state-of-the-art tools: ISP [<link linkend="ch16_b15">15</link>], a runtime verifier that employs dynamic partial order reduction to identify and exercise significant process interleavings in an MPI program; MUST [<link linkend="ch16_b7">7</link>], also a runtime verifier, but that employs a graph-based deadlock detection approach; and TASS [<link linkend="ch16_b20">20</link>], a static analysis tool based on symbolic execution. For the tools and the programs considered, PARTYPES runs in a constant time (the tool is insensitive to the number of processes, problem size, and other parameters), in clear contrast to the running time of all the other tools, which exhibited exponential growth in a significant number of cases.</para>
<para>In addition to PARTYPES, the theory of multi-party session types [<link linkend="ch16_b9">9</link>] inspired other works in the realm of message-passing programs and MPI in particular. Scribble [<link linkend="ch16_b8">8</link>, <link linkend="ch16_b22">22</link>] is a language to describe global protocols for a finite set of participants in message-passing programs using point-to-point communication. Through a notion of projection, a local protocol can be derived for each participant from a global Scribble protocol. Programs based on the local protocols can be implemented using standard message-passing libraries, as in Multiparty Session C [<link linkend="ch16_b13">13</link>]. Pabble [<link linkend="ch16_b12">12</link>], an extension of Scribble, is able to express interaction patterns of MPI programs where the number of participants in a protocol is decided at runtime, rather than fixed a priori, and was used to generate safe-by-construction MPI programs [<link linkend="ch16_b11">11</link>].</para>
<para>In comparison to these works, PARTYPES is specifically aimed at protocols for MPI programs and the verification of the compliance of arbitrary programs against a given protocol. In conceptual terms, we address collective communication primitives in addition to plain point-to-point communication, and require no explicit notion of protocol projection.</para>
</section>
<section class="lev1" id="sec16-7" label="16.7" xreflabel="16.7">
<title>Conclusion</title>
<para>This chapter presents PARTYPES, a type-based methodology to statically verify message-passing parallel programs. By checking that a program follows a given protocol, one guarantees a series of important safety properties, in particular that the program does not run into deadlocks. In contrast to other state-of-the-art approaches that suffer from scalability issues, our approach is insensitive to parameters such as the number of processes, problem size, or the number of iterations of a program.</para>
<para>The limitations of PARTYPES can be discussed along two dimensions:</para>
<itemizedlist mark="bullet" spacing="normal">
<listitem><para>Even though PARTYPES addresses the core features of MPI, it leaves important primitives uncovered. These include non-blocking operations and wildcard receive (the ability to receive from any source), among many others.</para></listitem>
<listitem><para>Our methodology is sound (in the sense that it does not yield false positives) but too intentional at times. For instance, it requires protocol loops and source code loops to be perfectly aligned, while the type theory [<link linkend="ch16_b10">10</link>] allows more flexibility, loop unrolling in particular.</para></listitem>
</itemizedlist>
<para><emphasis role="strong">Acknowledgements</emphasis> This work was supported by FCT (projects Advanced Type Systems for Multicore Programming PTDC/EIA&#x2013;CCO/12254, HYRAX CMUP-ERI/FIA/0048/2013, and the LaSIGE Research Unit UID/CEC/00408/2013), the NORTE 2020 program (project SMILES, NORTE&#x2013;01&#x2013;0145&#x2013;FEDER&#x2013;000020), EPSRC (projects EP/K034413/1, EP/K011715/1, EP/L00058X/1, and EP/N027833/1), and EU (UPSCALE FP7 612985, and Betty COST Action IC1201).</para>
</section>
<section class="lev1" id="sec2a">
<title>References</title>
<orderedlist numeration="arabic" continuation="restarts" spacing="normal">
<listitem><para><anchor id="ch16_b1"/>E. Cohen, M. Dahlweid, M. Hillebrand, D. Leinenbach, M. Moskal, T. Santen, W. Schulte, and S. Tobies. VCC: A practical system for verifying concurrent C. In <emphasis>TPHOLs</emphasis>, volume 5674 of <emphasis>LNCS</emphasis>, pages 23&#x2013;42. Springer, 2009. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=E%2E+Cohen%2C+M%2E+Dahlweid%2C+M%2E+Hillebrand%2C+D%2E+Leinenbach%2C+M%2E+Moskal%2C+T%2E+Santen%2C+W%2E+Schulte%2C+and+S%2E+Tobies%2E+VCC%3A+A+practical+system+for+verifying+concurrent+C%2E+In+TPHOLs%2C+volume+5674+of+LNCS%2C+pages+23-42%2E+Springer%2C+2009%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch16_b2"/>V. Forejt, D. Kroening, G. Narayanswamy, and S. Sharma. Precise predictive analysis for discovering communication deadlocks in MPI programs. In <emphasis>FM</emphasis>, volume 8442 of <emphasis>LNCS</emphasis>, pages 263&#x2013;278. Springer, 2014. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=V%2E+Forejt%2C+D%2E+Kroening%2C+G%2E+Narayanswamy%2C+and+S%2E+Sharma%2E+Precise+predictive+analysis+for+discovering+communication+deadlocks+in+MPI+programs%2E+In+FM%2C+volume+8442+of+LNCS%2C+pages+263-278%2E+Springer%2C+2014%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch16_b3"/>MPI Forum. <emphasis>MPI: A Message-Passing Interface Standard&#x2014;Version 3.0</emphasis>. High-Performance Computing Center Stuttgart, 2012.</para></listitem>
<listitem><para><anchor id="ch16_b4"/>I. Foster. <emphasis>Designing and building parallel programs</emphasis>. Addison-Wesley, 1995. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=I%2E+Foster%2E+Designing+and+building+parallel+programs%2E+Addison-Wesley%2C+1995%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch16_b5"/>X. Fu, Z. Chen, H. Yu, C. Huang, W. Dong, and J. Wang. Symbolic execution of mpi programs. In <emphasis>ICSE</emphasis>, pages 809&#x2013;810. IEEE Press, 2015. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=X%2E+Fu%2C+Z%2E+Chen%2C+H%2E+Yu%2C+C%2E+Huang%2C+W%2E+Dong%2C+and+J%2E+Wang%2E+Symbolic+execution+of+mpi+programs%2E+In+ICSE%2C+pages+809-810%2E+IEEE+Press%2C+2015%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch16_b6"/>G. Gopalakrishnan, R. M. Kirby, S. F. Siegel, R. Thakur, W. Gropp, E. Lusk, B. R. De Supinski, M. Schulz., and G. Bronevetsky. Formal analysis of MPI-based parallel programs. <emphasis>CACM</emphasis>, 54(12):82&#x2013;91, 2011. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=G%2E+Gopalakrishnan%2C+R%2E+M%2E+Kirby%2C+S%2E+F%2E+Siegel%2C+R%2E+Thakur%2C+W%2E+Gropp%2C+E%2E+Lusk%2C+B%2E+R%2E+De+Supinski%2C+M%2E+Schulz%2E%2C+and+G%2E+Bronevetsky%2E+Formal+analysis+of+MPI-based+parallel+programs%2E+CACM%2C+54%2812%29%3A82-91%2C+2011%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch16_b7"/>T. Hilbrich, J. Protze, M. Schulz, B. R. de Supinski, and M. S. M&#x00FC;ller. MPI runtime error detection with MUST: advances in deadlock detection. In <emphasis>SC</emphasis>, pages 30:1&#x2013;30:11. IEEE/ACM, 2012. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=T%2E+Hilbrich%2C+J%2E+Protze%2C+M%2E+Schulz%2C+B%2E+R%2E+de+Supinski%2C+and+M%2E+S%2E+M%FCller%2E+MPI+runtime+error+detection+with+MUST%3A+advances+in+deadlock+detection%2E+In+SC%2C+pages+30%3A1-30%3A11%2E+IEEE%2FACM%2C+2012%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch16_b8"/>K. Honda, R. Hu, R. Neykova, T. C. Chen, R. Demangeon, P. Denielou, and N. Yoshida. Structuring communication with session types. In <emphasis>COB</emphasis>, volume 8665 of <emphasis>LNCS</emphasis>, pages 105&#x2013;127. Springer, 2014. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=K%2E+Honda%2C+R%2E+Hu%2C+R%2E+Neykova%2C+T%2E+C%2E+Chen%2C+R%2E+Demangeon%2C+P%2E+Denielou%2C+and+N%2E+Yoshida%2E+Structuring+communication+with+session+types%2E+In+COB%2C+volume+8665+of+LNCS%2C+pages+105-127%2E+Springer%2C+2014%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch16_b9"/>K. Honda, N. Yoshida, and M. Carbone. Multiparty asynchronous session types. In <emphasis>POPL</emphasis>, pages 273&#x2013;284. ACM, 2008. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=K%2E+Honda%2C+N%2E+Yoshida%2C+and+M%2E+Carbone%2E+Multiparty+asynchronous+session+types%2E+In+POPL%2C+pages+273-284%2E+ACM%2C+2008%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch16_b10"/>H. A. L&#x00F3;pez, E. R. B. Marques, F. Martins, N. Ng, C. Santos, V. T. Vasconcelos, and N. Yoshida. Protocol-based verification of message-passing parallel programs. In <emphasis>OOPSLA</emphasis>, pages 280&#x2013;298. ACM, 2015. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=H%2E+A%2E+L%F3pez%2C+E%2E+R%2E+B%2E+Marques%2C+F%2E+Martins%2C+N%2E+Ng%2C+C%2E+Santos%2C+V%2E+T%2E+Vasconcelos%2C+and+N%2E+Yoshida%2E+Protocol-based+verification+of+message-passing+parallel+programs%2E+In+OOPSLA%2C+pages+280-298%2E+ACM%2C+2015%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch16_b11"/>N. Ng, J. G. F. Coutinho, and N. Yoshida. Protocols by default: Safe MPI code generation based on session types. In <emphasis>CC</emphasis>, volume 9031 of <emphasis>LNCS</emphasis>, pages 212&#x2013;232. Springer, 2015. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=N%2E+Ng%2C+J%2E+G%2E+F%2E+Coutinho%2C+and+N%2E+Yoshida%2E+Protocols+by+default%3A+Safe+MPI+code+generation+based+on+session+types%2E+In+CC%2C+volume+9031+of+LNCS%2C+pages+212-232%2E+Springer%2C+2015%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch16_b12"/>N. Ng and N. Yoshida. Pabble: parameterised scribble. <emphasis>Service Oriented Computing and Applications</emphasis>, 9(3&#x2013;4):269&#x2013;284, 2015. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=N%2E+Ng+and+N%2E+Yoshida%2E+Pabble%3A+parameterised+scribble%2E+Service+Oriented+Computing+and+Applications%2C+9%283-4%29%3A269-284%2C+2015%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch16_b13"/>N. Ng, N. Yoshida, and K. Honda. Multiparty Session C: Safe parallel programming with message optimisation. In <emphasis>TOOLS Europe</emphasis>, volume 7304 of <emphasis>LNCS</emphasis>, pages 202&#x2013;218. Springer, 2012. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=N%2E+Ng%2C+N%2E+Yoshida%2C+and+K%2E+Honda%2E+Multiparty+Session+C%3A+Safe+parallel+programming+with+message+optimisation%2E+In+TOOLS+Europe%2C+volume+7304+of+LNCS%2C+pages+202-218%2E+Springer%2C+2012%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch16_b14"/>Partypes homepage. <literal>http://gloss.di.fc.ul.pt/ParTypes</literal>. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=Partypes+homepage%2E+http%3A%2F%2Fgloss%2Edi%2Efc%2Eul%2Ept%2FParTypes%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch16_b15"/>S. Pervez, G. Gopalakrishnan, R. M. Kirby, R. Palmer, R. Thakur, and W. Gropp. Practical model-checking method for verifying correctness of MPI programs. In <emphasis>PVM/MPI</emphasis>, volume 4757 of <emphasis>LNCS</emphasis>, pages 344&#x2013;353. Springer, 2007. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=S%2E+Pervez%2C+G%2E+Gopalakrishnan%2C+R%2E+M%2E+Kirby%2C+R%2E+Palmer%2C+R%2E+Thakur%2C+and+W%2E+Gropp%2E+Practical+model-checking+method+for+verifying+correctness+of+MPI+programs%2E+In+PVM%2FMPI%2C+volume+4757+of+LNCS%2C+pages+344-353%2E+Springer%2C+2007%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch16_b16"/>M. Schulz and B. R. de Supinski. A flexible and dynamic infrastructure for MPI tool interoperability. In <emphasis>ICPP</emphasis>, pages 193&#x2013;202. IEEE, 2006. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=M%2E+Schulz+and+B%2E+R%2E+de+Supinski%2E+A+flexible+and+dynamic+infrastructure+for+MPI+tool+interoperability%2E+In+ICPP%2C+pages+193-202%2E+IEEE%2C+2006%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch16_b17"/>S. F. Siegel and G. Gopalakrishnan. Formal analysis of message passing. In <emphasis>VMCAI</emphasis>, volume 6538 of <emphasis>LNCS</emphasis>, pages 2&#x2013;18. Springer, 2011. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=S%2E+F%2E+Siegel+and+G%2E+Gopalakrishnan%2E+Formal+analysis+of+message+passing%2E+In+VMCAI%2C+volume+6538+of+LNCS%2C+pages+2-18%2E+Springer%2C+2011%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch16_b18"/>S. F. Siegel and L.F. Rossi. Analyzing BlobFlow: A case study using model checking to verify parallel scientific software. In <emphasis>EuroPVM/MPI</emphasis>, volume 5205 of <emphasis>LNCS</emphasis>, pages 274&#x2013;282. Springer, 2008. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=S%2E+F%2E+Siegel+and+L%2EF%2E+Rossi%2E+Analyzing+BlobFlow%3A+A+case+study+using+model+checking+to+verify+parallel+scientific+software%2E+In+EuroPVM%2FMPI%2C+volume+5205+of+LNCS%2C+pages+274-282%2E+Springer%2C+2008%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch16_b19"/>S. F. Siegel, M. Zheng, Z. Luo, T. K. Zirkel, A. V. Marianiello, J. G. Edenhofner, M. B. Dwyer, and M. S. Rogers. CIVL: The concurrency intermediate verification language. In <emphasis>SC</emphasis>, pages 61:1&#x2013;61:12. IEEE Press, 2015. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=S%2E+F%2E+Siegel%2C+M%2E+Zheng%2C+Z%2E+Luo%2C+T%2E+K%2E+Zirkel%2C+A%2E+V%2E+Marianiello%2C+J%2E+G%2E+Edenhofner%2C+M%2E+B%2E+Dwyer%2C+and+M%2E+S%2E+Rogers%2E+CIVL%3A+The+concurrency+intermediate+verification+language%2E+In+SC%2C+pages+61%3A1-61%3A12%2E+IEEE+Press%2C+2015%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch16_b20"/>S. F. Siegel and T. K. Zirkel. Loop invariant symbolic execution for parallel programs. In <emphasis>VMCAI</emphasis>, volume 7148 of <emphasis>LNCS</emphasis>, pages 412&#x2013;427. Springer, 2012. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=S%2E+F%2E+Siegel+and+T%2E+K%2E+Zirkel%2E+Loop+invariant+symbolic+execution+for+parallel+programs%2E+In+VMCAI%2C+volume+7148+of+LNCS%2C+pages+412-427%2E+Springer%2C+2012%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch16_b21"/>A. Vo, S. Aananthakrishnan, G. Gopalakrishnan, B. R. de Supinski, M. Schulz, and G. Bronevetsky. A scalable and distributed dynamic formal verifier for MPI programs. In <emphasis>SC</emphasis>, pages 1&#x2013;10. IEEE, 2010. <ulink url="https://scholar.google.com/scholar?hl=en&amp;q=A%2E+Vo%2C+S%2E+Aananthakrishnan%2C+G%2E+Gopalakrishnan%2C+B%2E+R%2E+de+Supinski%2C+M%2E+Schulz%2C+and+G%2E+Bronevetsky%2E+A+scalable+and+distributed+dynamic+formal+verifier+for+MPI+programs%2E+In+SC%2C+pages+1-10%2E+IEEE%2C+2010%2E" target="_blank">Google Scholar</ulink></para></listitem>
<listitem><para><anchor id="ch16_b22"/>N. Yoshida, R. Hu, R. Neykova, and N. Ng. The Scribble protocol language. In <emphasis>TGC</emphasis>, volume 8358 of <emphasis>LNCS</emphasis>, pages 22&#x2013;41. Springer, 2013.</para></listitem>
</orderedlist>
</section>
</chapter>
<chapter class="nosec" id="ch17">
<title>About the Editors</title>
<para><emphasis role="strong">Simon Gay</emphasis> received his Ph.D. from Imperial College London and is now Professor of Computing Science at the University of Glasgow. He is Director of Research in the School of Computing Science, and leader of the Formal Analysis, Theory and Algorithms research section. From 2012 to 2016 he was Chair of COST Action IC1201: Behavioural Types for Reliable Large-Scale Software Systems.</para>
<para><emphasis role="strong">Ant&#243;nio Ravara</emphasis> received his Ph.D. from the Technical University of Lisbon and is now Assistant Professor of Informatics at the NOVA University of Lisbon. He is a founding member of the NOVA Laboratory for Computer Science and Informatics (NOVA LINCS), which is a leading Portuguese research unit in the area of Computer Science and Engineering. From 2012 to 2016 he was Vice-Chair of COST Action IC1201: Behavioural Types for Reliable Large-Scale Software Systems.</para>
</chapter>
</book>
