Refinement Session Types
Total Page:16
File Type:pdf, Size:1020Kb
MENG INDIVIDUAL PROJECT IMPERIAL COLLEGE LONDON DEPARTMENT OF COMPUTING Refinement Session Types Supervisor: Prof. Nobuko Yoshida Author: Fangyi Zhou Second Marker: Dr. Iain Phillips 16th June 2019 Abstract We present an end-to-end framework to statically verify multiparty concurrent and distributed protocols with refinements, where refinements are in the form of logical constraints. We combine the theory of multiparty session types and refinement types and provide a type system approach for lightweight static verification. We formalise a variant of the l-calculus, extended with refinement types, and prove their type safety properties. Based on the formalisation, we implement a refinement type system extension for the F# language. We design a functional approach to generate APIs with refinement types from a multiparty protocol in F#. We generate handler-styled APIs, which statically guarantee the linear usage of channels. With our refinement type system extension, we can check whether the implementation is correct with respect to the refinements. We evaluate the expressiveness of our system using three case studies of refined protocols. Acknowledgements I would like to thank Prof. Nobuko Yoshida, Dr. Francisco Ferreira, Dr. Rumyana Neykova and Dr. Raymond Hu for their advice, support and help during the project. Without them I would not be able to complete this project. I would like to thank Prof. Paul Kelly, Prof. Philippa Gardner and Prof. Sophia Drossopoulou, whose courses enlightened me to explore the area of programming languages. Without them I would not be motivated to discover more in this area. I would like to thank Prof. Paul Kelly again, this time for being my personal tutor. I am very grateful to Paul, who continuously supported, encouraged and inspired me throughout the degree. I would like to thank my family for supporting me to pursue a degree abroad. I am truly grateful for their unconditional support throughout my life. Contents 1 Introduction 9 1.1 Motivation . .9 1.2 Objectives . 10 1.3 Contributions and Report Structure . 10 2 Background 13 2.1 Session Types . 13 2.1.1 Asynchronous p-calculus . 14 2.1.2 Binary Session Types . 15 2.1.3 Multiparty Session Types . 18 2.1.4 Scribble and Endpoint API Generation . 21 2.1.5 F# Session Type Provider . 22 2.2 Refinement Types . 24 2.2.1 Liquid Types . 24 2.2.2 F7: Refinement Types for F# . 25 2.2.3 F? ..................................... 25 2.2.4 F# Session Type Provider . 26 3 lH: A Simple Refinement Calculus 29 3.1 Syntax . 29 3.2 Operational Semantics . 32 3.3 Erasing Refinements . 33 3.4 Typing . 34 3.4.1 Type Synthesis and Checking . 34 3.4.2 Well-formedness of Types and Context . 37 3.4.3 Subtyping and Encoding . 38 3.4.4 Typing under Erasure . 40 3.5 Type Safety . 42 4 FLUIDTYPES: A Type System Implementation of lH 49 1 4.1 Implementation of lH Type System . 49 4.1.1 Data Type Definitions . 50 4.1.2 Main Typing Judgements . 50 4.1.3 Subtyping Judgements and SMT Encodings . 52 4.1.4 Summary . 53 4.2 From F# Expression to lH Terms . 54 4.2.1 Handling Expressions and Types . 54 4.2.2 Summary . 55 4.3 Annotations for Refinement Types . 55 4.3.1 Annotation by Custom Attributes . 55 4.3.2 String-based Annotation . 56 4.3.3 Alternative Designs . 56 4.3.4 Summary . 57 4.4 Type System Extensions . 57 4.4.1 Type Aliases . 58 4.4.2 Records . 59 4.4.3 Enumerations . 60 4.4.4 Algebraic Data Types . 61 5 FLUIDSESSION: Towards Statically Verified Protocol Implementation 63 5.1 Protocol Specification with Scribble . 63 5.2 Obtaining the CFSM from Scribble . 63 5.3 API Generation . 64 5.3.1 A Straight-line Protocol without Refinements . 64 5.3.2 Adding Refinements to Payloads . 66 5.3.3 Adding Refinements with Non-payload Variables . 66 5.3.4 Adding Branches . 68 5.3.5 Summary . 72 5.4 Execution of the CFSM . 72 5.5 Limitations . 73 6 Evaluation 75 6.1 Adder Example in Section 5.3 . 75 6.1.1 Client . 76 6.1.2 Server . 80 6.1.3 Summary . 83 6.2 Two Buyers Protocol . 83 6.3 Example Protocols with Unsupported Refinements . 85 2 7 Conclusion 87 7.1 Contributions . 87 7.1.1 Open Source Contributions to F# Libraries . 88 7.2 Future Work . 88 7.2.1 Refinements in Multiparty Session Types . 88 7.2.2 lH and FLUIDTYPES Library . 89 A A Basic Type System for the Simply Typed l-calculus 95 A.1 Types and Typing Judgements . 95 B Lemmas and Proofs 97 B.1 Lemmas and Proofs for Chapter 3 . 97 C Syntax of Refinement Annotations 105 C.1 Syntax of Refinement Type Annotation in F# . 105 D Code for Evaluation 107 D.1 Implementation for Two Buyers . 107 3 4 List of Figures 1.1 Workflow for Verified Implementation of Refined Protocols . 11 2.1 Processes in Asynchronous p-calculus . 14 2.2 Processes in Session Calculus . 16 2.3 Syntax of Session Types . 17 2.4 Processes in Multiparty Session Calculus . 19 2.5 Global and Session Types . 20 2.6 Projection from Global Types to Session Types . 21 2.7 Adder Protocol . 22 2.8 Finite State Machine for role C for Protocol in Figure 2.7 . 23 2.9 Generated Java API for State 2, Role C (truncated) . 23 2.10 Refined Adder Protocol . 26 3.1 Syntax of lH ................................... 30 3.2 Definition of Contexts . 31 3.3 Reduction Rules for lH ............................ 32 3.4 Typing Judgements for lH .......................... 35 3.5 Well-formedness Judgements for Types . 37 3.6 Well-formedness Judgements of Context . 37 3.7 Subtyping Judgements for lH ........................ 38 4.1 Function Signatures of Main Typing Judgements . 50 4.2 Code Snippet of Using an Attribute . 55 4.3 abs Function with an Annotated Refinement Type . 56 4.4 Invalid Code Snippet due to a Limitation of F# Attributes . 58 4.5 Type Alias Definition of Non-negative Integers . 58 4.6 Valid Code Snippet Using Type Alias . 58 4.7 An Example Definition of Record Type . 59 4.8 An Example Definition of Record Type with Refinement . 59 4.9 An Example Definition of Record Type with Data Dependency . 59 4.10 Proving Sum of Two Even Numbers are Even Using Record Type . 60 5 4.11 An Example Type Definition of Enumeration Type . 61 4.12 An Example Type Definition of Discriminated Union with Refinements . 61 5.1 Adder Protocol (without Refinements) . 64 5.2 Finite State Machine for Role C for Protocol in Figure 5.1 . 65 5.3 Generated Handler Types for role C for Figure 5.1 . 65 5.4 Adder Protocol (with Refinements) . 66 5.5 Generated Handler Types for role C for Figure 5.4 . 66 5.6 Adder Protocol (with More Refinements) . 67 5.7 Generated State Record Definition for State 9 . 67 5.8 Adder Protocol (with Branches) . 68 5.9 Finite State Machine for role C for Protocol in Figure 5.8 . 69 6.1 Handler Type Definition for Client in Adder Protocol . 76 6.2 Example Implementation of Client Role . 77 6.3 Auxiliary Functions for Testing Properties . 79 6.4 Alternative Implementation of Client Role . 80 6.5 Handler Type Definition for Server in Adder Protocol . 81 6.6 Example Implementation of Server Role . 82 6.7 Two Buyer Protocol in Scribble . 83 6.8 Implementation for Role B of Two Buyers Protocol . 84 6.9 Accumulator Protocol in Scribble . 85 6.10 Refined Accumulator Protocol in Imaginary Scribble . 86 A.1 Types in l-calculus . 95 A.2 Typing Judgements for l-calculus . 95 D.1.