Implementing Joins using Extensible Pattern Matching

Similar documents
An Overview of Scala. Philipp Haller, EPFL. (Lots of things taken from Martin Odersky's Scala talks)

Implementing Joins using Extensible Pattern Matching

Concurrency Abstractions in C#

Concurrency Abstractions in C#

Concurrency Abstractions in C#

Let s Unify With Scala Pattern Matching!

What the optimizer does to your code

Written Presentation: JoCaml, a Language for Concurrent Distributed and Mobile Programming

CS 6112 (Fall 2011) Foundations of Concurrency

AmbientTalk: Object-oriented Event-driven programming in Mobile Ad hoc Networks

Asynchronous Functions in C#

Scala Actors. Scalable Multithreading on the JVM. Philipp Haller. Ph.D. candidate Programming Methods Lab EPFL, Lausanne, Switzerland

Advances in Programming Languages

Simplifying Asynchronous Code with

Declarations and Access Control SCJP tips

AmbientTalk: Object-oriented Event-driven programming in Mobile Ad hoc Networks

Functional programming

Enforcing Mutual Exclusion Using Monitors

CompSci 220. Programming Methodology 16: Understanding FP Error Handling Part 2

Today: Distributed Middleware. Middleware

Linguistic Symbiosis between Actors and Threads

EECS168 Exam 3 Review

Last Class: Synchronization

Thrift specification - Remote Procedure Call

Chapter 4 Defining Classes I

Models and languages of concurrent computation

Lecture 09: Introduction to Scala

Decision Making in C

What should we do with this freedom?

Capabilities for Uniqueness and Borrowing

The Dining Philosophers Problem CMSC 330: Organization of Programming Languages

SELECTION. (Chapter 2)

Shacl: Operational Semantics

Distributed Systems Principles and Paradigms

CMSC 330: Organization of Programming Languages. The Dining Philosophers Problem

Problems with Concurrency. February 19, 2014

ADVANTAGES. Via PL/SQL, all sorts of calculations can be done quickly and efficiently without use of Oracle engine.

Java Threads and intrinsic locks

Objects as Session-Typed Processes

Module 10: Open Multi-Processing Lecture 19: What is Parallelization? The Lecture Contains: What is Parallelization? Perfectly Load-Balanced Program

Optimizing Higher-Order Functions in Scala

Programming Safe Agents in Blueprint. Alex Muscar University of Craiova

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Distributed Object-Based. Systems. Chapter 9

State Machine Diagrams

Assertions, pre/postconditions

Session Scala. Multiparty Session Programming with Scala, Scribble and AMQP.

Parallel Actor Monitors

The Trouble with Types

Introduction to Software Testing Chapter 2.4 Graph Coverage for Design Elements Paul Ammann & Jeff Offutt

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

A Formal Model for Direct-style Asynchronous Observables

Process Synchronization

Synchronization SPL/2010 SPL/20 1

Distributed Objects and Remote Invocation. Programming Models for Distributed Applications

Java Threads. COMP 585 Noteset #2 1

Buy a Feature: an Adventure in Immutability and Actors. David Pollak BayFP August 22, 2008

Remote Procedure Call

CMSC 330: Organization of Programming Languages

Communication. Outline

Interprocess Communication By: Kaushik Vaghani

Actor-Based Concurrency: Implementation and Comparative Analysis With Shared Data and Locks Alex Halter Randy Shepherd

Today: Distributed Objects. Distributed Objects

Topic VIII. The state of the art Scala < > Scala By Example by M. Odersky. Programming Methods Laboratory, EPFL, 2008.

CS558 Programming Languages

Functions and Objects. Week 7: Symbolic Computation

PL/SQL Block structure

Informatica 3. Marcello Restelli. Laurea in Ingegneria Informatica Politecnico di Milano 9/15/07 10/29/07

Submit your answers to these questions to the Curator under Quizzes as HW08 by the posted due date and time. No late submissions will be accepted.

Distributed Object-Based Systems The WWW Architecture Web Services Handout 11 Part(a) EECS 591 Farnam Jahanian University of Michigan.

CSCI 447 Operating Systems Filip Jagodzinski

Overview. Elements of Programming Languages. Advanced constructs. Motivating inner class example

David Pollak CUFP September 26 th, 2008

Joint Entity Resolution

CMSC 330: Organization of Programming Languages. Threads Classic Concurrency Problems

Concurrent Programming using Threads

SSEA Computer Science: Track A. Dr. Cynthia Lee Lecturer in Computer Science Stanford

Parallel And Distributed Compilers

Parallel Actor Monitors: Disentangling Task-Level Parallelism from Data Partitioning in the Actor Model.

Combining Concurrency Abstractions

Chapter 13 Topics. Introduction. Introduction

Concurrent Programming. CS105 Programming Languages Supplement

Resource management. Real-Time Systems. Resource management. Resource management

CS558 Programming Languages

1 Process Coordination

Actors in the Small. Making Actors more Useful. Bill La

C Programming for Engineers Functions

Distributed Deadlock Detection

Akka Streams and Bloom Filters

Executive Summary. It is important for a Java Programmer to understand the power and limitations of concurrent programming in Java using threads.

Lecture08: Scope and Lexical Address

Java Methods. Lecture 8 COP 3252 Summer May 23, 2017

Chapter Machine instruction level 2. High-level language statement level 3. Unit level 4. Program level

Dealing with Issues for Interprocess Communication

Overview. Elements of Programming Languages. Objects. Self-Reference

Scala in Martin Odersky

Summary Semaphores. Passing the Baton any await statement. Synchronisation code not linked to the data

The SPIN Model Checker

Intermediate representations IR #1: CPS/L 3. Code example. Advanced Compiler Construction Michel Schinz

learning objectives learn about variables, their types and their values learn about different number representations

Transcription:

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

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 2/18

Simple buffer in Cω: Example 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 3/18

Example using Scala Joins enable join class Buffer extends Joins { patterns 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 4/18

Reader/Writer Lock Nested class ReaderWriterLock extends Joins { val Exclusive, ReleaseExclusive = new NullarySyncEvent val Shared, ReleaseShared = new NullarySyncEvent private val Sharing = new AsyncEvent[Int] pattern 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 5/18

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 6/18

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

Pattern Matching in Scala { case pat 1 => body 1... case pat n => body n 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 8/18

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

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

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 join { case Get() & Put(x) => Get reply x Buffer event invocations Consult these buffers during matching For this, use extensible pattern matching! June 4, 2008 Scala Joins/Haller/COORDINATION'08 11/18

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 12/18

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

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 14/18

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

Joins for Actors Enables join patterns alongside normal message patterns: receive { case Put(x) & Get() => Get reply x case Some(other) =>... 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! June 4, 2008 Scala Joins/Haller/COORDINATION'08 16/18

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 17/18

Scala Joins 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 18/18