Tom Van Cutsem, Vrije Universiteit Brussel

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

1 Polyphonic C# Nick Benton Luca Cardelli Cédric Fournet Microsoft Research.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
CS 5704 Fall 00 1 Monitors in Java Model and Examples.
Concurrency (3) CSE 132. iClicker/WUTexter Question The following four numbers are in 8-bit 2’s complement form: Which.
An Associative Broadcast Based Coordination Model for Distributed Processes James C. Browne Kevin Kane Hongxia Tian Department of Computer Sciences The.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
Remote Procedure Call in SR Programming Language By Tze-Kin Tsang 3/20/2000.
1 Conditionals In many cases we want our program to make a decision about whether a piece of code should be executed or not, based on the truth of a condition.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 7: Methods.
VB in Context Michael B. Spring Department of Information Science and Telecommunications University of Pittsburgh Pittsburgh, Pa 15260
Ivan Lanese Computer Science Department University of Bologna/INRIA Italy Fault in the Future Joint work with Gianluigi Zavattaro and Einar Broch Johnsen.
Distributed Programming in Scala with APGAS Philippe Suter, Olivier Tardieu, Josh Milthorpe IBM Research Picture by Simon Greig.
1 Ivan Lanese Computer Science Department University of Bologna Italy Streaming Services in SSCC Joint work with Francisco Martins, Vasco Vasconcelos and.
.NET Mobile Application Development Remote Procedure Call.
SE-565 Software System Requirements More UML Diagrams.
Maria-Cristina Marinescu Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology A Synthesis Algorithm for Modular Design of.
Python quick start guide
Symbol Table (  ) Contents Map identifiers to the symbol with relevant information about the identifier All information is derived from syntax tree -
Modern Concurrency Abstractions for C# by Nick Benton, Luca Cardelli & C´EDRIC FOURNET Microsoft Research.
CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);
Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Functional Programming With examples in F#. Pure Functional Programming Functional programming involves evaluating expressions rather than executing commands.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
Denis Caromel1 Institut universitaire de France (IUF) INRIA Sophia-Antipolis – CNRS – I3S – Université de Nice Luis Mateu DCC – Universidad de Chile Eric.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
ICS 313: Programming Language Theory Chapter 13: Concurrency.
WRITING CONTROL STRUCTURES (CONDITIONAL CONTROL).
Distributed Programming Concepts and Notations. Inter-process Communication Synchronous Messages Asynchronous Messages Select statement Remote procedure.
Sep/05/2001PaCT Fusion of Concurrent Invocations of Exclusive Methods Yoshihiro Oyama (Japan Science and Technology Corporation, working in University.
CS162 Week 1 Kyle Dewey. Overview Basic Introduction CS Accounts Scala survival guide.
Introduction to C Programming CE Lecture 3 Control Structures in C.
Advanced Arithmetic, Conditionals, and Loops INFSY 535.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 24 Critical Regions.
Compiler (scalac, gcc) Compiler (scalac, gcc) I = 0 while (i < 10) { i = i + 1 } I = 0 while (i < 10) { i = i + 1 } source code i d 3 = 0 LF w i d 3 =
13-1 Chapter 13 Concurrency Topics Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads C# Threads.
CS533 – Spring Jeanie M. Schwenk Experiences and Processes and Monitors with Mesa What is Mesa? “Mesa is a strongly typed, block structured programming.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Decision Making and Branching (cont.)
Comunication&Synchronization threads 1 Programación Concurrente Benemérita Universidad Autónoma de Puebla Facultad de Ciencias de la Computación Comunicación.
Decision Statements, Short- Circuit Evaluation, Errors.
Lesson thirteen Conditional Statement "if- else" ©
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
1 Concurrency Abstractions in C#. Concurrency in C# CS 5204 – Fall, Motivation Concurrency critical factor in behavior/performance affects semantics.
An Overview of Scala Philipp Haller, EPFL (Lots of things taken from Martin Odersky's Scala talks)
Java Thread Programming
Lightweight Language Support for Type-Based, Concurrent Event Processing Philipp Haller EPFL Scala Workshop 2010.
cs612/2002sp/projects/ CS612 Term Projects cs612/2002sp/projects/
CNG 140 C Programming (Lecture set 3)
“Language Mechanism for Synchronization”
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Pattern-Directed Programming
Iterative Constructs Review
Other Important Synchronization Primitives
On the Complexity of Buffer Allocation in Message Passing Systems
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
Setac: A Phased Deterministic Testing Framework for Scala Actors
Threads and Memory Models Hal Perkins Autumn 2011
Compiler Design 18. Object Oriented Semantic Analysis (Symbol Tables, Type Checking) Kanat Bolazar March 30, 2010.
Chapter 6 Methods: A Deeper Look
Setac: A Phased Deterministic Testing Framework for Scala Actors
Code Shape III Booleans, Relationals, & Control flow
Threads and Memory Models Hal Perkins Autumn 2009
Subject : T0152 – Programming Language Concept
Controlling Program Flow
Exception Handling.
CSE341: Programming Languages Lecture 11 Type Inference
Presentation transcript:

Tom Van Cutsem, Vrije Universiteit Brussel Implementing Joins using Extensible Pattern Matching Philipp Haller, EPFL joint work with Tom Van Cutsem, Vrije Universiteit Brussel

Scala Joins/Haller/COORDINATION'08 Introduction Concurrency is indispensable: multi-cores, asynchronous, distributed applications Two interesting pieces of the puzzle: Joins: high-level, declarative specification of synchronization constraints (origin: join calculus) Extensible pattern matching constructs of modern languages (e.g., Scala, F#) Idea: integrate join synchronization using extensible pattern matching June 4, 2008 Scala Joins/Haller/COORDINATION'08

Scala Joins/Haller/COORDINATION'08 Example Simple buffer in Cω: public class Buffer { public async Put(int s); public int Get() & Put(int s) { return s; } Asynchronous methods Never block Return unit/void Join operator & Definition of join pattern Exactly one sync method (Get) Multiple async methods (Put) Result returned to caller of sync method June 4, 2008 Scala Joins/Haller/COORDINATION'08

Example using Scala Joins enable join patterns class Buffer extends Joins { val Put = new AsyncEvent[Int] val Get = new SyncEvent[Int] join { case Get() & Put(x) => Get reply x } } Event types: Asynchronous/synchronous Types of argument(s)/result Multiple sync events per join pattern allowed Reply to sync events explicitly June 4, 2008 Scala Joins/Haller/COORDINATION'08

Scala Joins/Haller/COORDINATION'08 Reader/Writer Lock Nested pattern class ReaderWriterLock extends Joins { val Exclusive, ReleaseExclusive = new NullarySyncEvent val Shared, ReleaseShared = new NullarySyncEvent private val Sharing = new AsyncEvent[Int] join { case Exclusive() & Sharing(0) => Exclusive.reply() case ReleaseExclusive() => Sharing(0); ReleaseExclusive.reply() case Shared() & Sharing(n) => Sharing(n+1); Shared.reply() case ReleaseShared() & Sharing(n) => Sharing(n-1); ReleaseShared.reply() } Sharing(0) Invoking (private) events inside bodies/constructor June 4, 2008 Scala Joins/Haller/COORDINATION'08

Reader/Writer Lock: C# Joins public class ReaderWriter { public Synchronous.Channel Exclusive, ReleaseExclusive; public Synchronous.Channel Shared, ReleaseShared; private Asynchronous.Channel Idle; private Asynchronous.Channel<int> Sharing; public ReaderWriter() { Join j = Join.Create(); ... // Boilerplate omitted j.When(Exclusive).And(Idle).Do(delegate {}); j.When(ReleaseExclusive).Do(delegate{ Idle(); }); j.When(Shared).And(Idle).Do(delegate{ Sharing(1); }); j.When(Shared).And(Sharing).Do(delegate(int n) { Sharing(n+1); }); j.When(ReleaseShared).And(Sharing).Do(delegate(int n) { if (n==1) Idle(); else Sharing(n-1); }); Idle(); } } June 4, 2008 Scala Joins/Haller/COORDINATION'08

Expressiveness of Scala Joins Nested patterns Avoid if-else inside join bodies Less redundancy, fewer messages Guards No need for internal events to maintain state Dynamic joins Beyond compiled schemes, such as Cω Sequence matching Deconstruct sequence inside pattern June 4, 2008 Scala Joins/Haller/COORDINATION'08

Pattern Matching in Scala { case pat1 => body1 ... case patn => bodyn } class PartialFunction[A,B] extends Function[A,B] { def isDefinedAt(x: A): Boolean } class Function[A,B] { def apply(x: A): B } is compiled to (anonymous) class that extends June 4, 2008 Scala Joins/Haller/COORDINATION'08

Join Patterns as Partial Functions join { case Get() & Put(x) => Get reply x } isDefinedAt: check whether a join pattern matches apply: execute body of first matching join pattern (is compiled to) val pats = new PartialFunction[?, Unit] { def isDefinedAt(arg: ?): Boolean = ... def apply(arg: ?): Unit = ... } join(pats) June 4, 2008 Scala Joins/Haller/COORDINATION'08

Matching Join Patterns: Example join { case Buy(a, x) & Sell(a, y) => ... } & Buy(a, x) Sell(b, y) a == b patFun := (B, U) (A, W) (A, V) (C, V) Invocations: FAIL (1, 1) Matching: FAIL (1, 2) MATCH (2, 1) June 4, 2008 Scala Joins/Haller/COORDINATION'08

Scala Joins/Haller/COORDINATION'08 Matching Problem Problem: outcome of matching depends on history of event invocations Ex.: When invoking Get, join pattern matches iff Put has been invoked previously Solution Buffer event invocations Consult these buffers during matching For this, use extensible pattern matching! join { case Get() & Put(x) => Get reply x } June 4, 2008 Scala Joins/Haller/COORDINATION'08

Extensible Pattern Matching Extractors (Scala), Active Patterns (F#) Implicitly apply type conversions Ex.: (x: Int) match { case Twice(y) => println("x = 2*"+y) case _ => println("x uneven") } (is compiled to) Twice.unapply(x) match { case Some(y) => println("x = 2*"+y) case None => println("x uneven") } June 4, 2008 Scala Joins/Haller/COORDINATION'08

Matching Join Patterns join { case &(Get(), Put(x)) => Get reply x } new PartialFunction[?, Unit] { def isDefinedAt(arg: ?) = &.unapply(arg) match { case Some((Get(), Put(x))) => true case _ => false } (is compiled to) June 4, 2008 Scala Joins/Haller/COORDINATION'08

Matching Join Patterns (2) join { case &(Get(), Put(x)) => Get reply x } (is compiled to) new PartialFunction[?, Unit] { def isDefinedAt(arg: ?) = &.unapply(arg) match { case Some((u, v)) => Get.unapply(u) match { case true => Put.unapply(v) match { case Some(x) => true case _ => false } case _ => false } case _ => false } Permits information to be passed down from & to the events! -> Control matching of events June 4, 2008 Scala Joins/Haller/COORDINATION'08

Matching Join Patterns: Example join { case Buy(a, x) & Sell(a, y) => ... } 1. & Buy(a, x) Sell(b, y) a == b patFun := 2. c patFun.isDefinedAt(c) c 5. Evaluate guard 3. Buy.unapply(c) return invocation c(this) 4. Sell.unapply(c) (B, U) (A, W) (A, V) (C, V) Buffered invocations: FAIL (1, 1) Matching: For each combination c: call patFun.isDefinedAt(c) until match found or all combinations have been tried FAIL (1, 2) MATCH (2, 1) June 4, 2008 Scala Joins/Haller/COORDINATION'08

Scala Joins/Haller/COORDINATION'08 Joins for Actors Enables join patterns alongside normal message patterns: More general: generalization of existing libraries that use pattern matching By sticking to simple PartialFunction interface Compilation to locks would require adding compiler support for actors! receive { case Put(x) & Get() => Get reply x case Some(other) => ... } June 4, 2008 Scala Joins/Haller/COORDINATION'08

Ongoing and Future Work Generalization of programming model e.g., mobility of events/join messages Search strategies (e.g., first match) Optimizations Experimental evaluation Compiler plug-in More consistency checks Offline optimization June 4, 2008 Scala Joins/Haller/COORDINATION'08

Scala Joins/Haller/COORDINATION'08 Exploits extensible pattern matching High expressiveness Nested patterns and guards Dynamic joins Library-based design Enables generalization of existing libraries Strong consistency checks Download: http://lamp.epfl.ch/~phaller/joins/ June 4, 2008 Scala Joins/Haller/COORDINATION'08