Implementing Concurrent Futures in Concurrent Haskell

Size: px
Start display at page:

Download "Implementing Concurrent Futures in Concurrent Haskell"

Transcription

1 Implementing Concurrent Futures in Concurrent Haskell Working Draft David Sabel Institut für Informatik Johann Wolfgang Goethe-Universität Postfach D Frankfurt, Germany January 28, Introduction We present an implementation of futures in Concurrent Haskell. Futures are variables whose value is initially not known, but becomes available in the future when the corresponding computation is finished. The futures presented in this paper are concurrent, i.e. the computation necessary to obtain the value of a future is performed in a concurrent thread. We present explicit futures, i.e. where the value of a future must be explicitly forced and more importantly we also implement implicit futures. Implicit futures can be used like any other reference, and to obtain the value of a future there is no need to explicitly force the future. The advantage of futures is their easy use: for a lot of applications futures can be used as basic concurrency primitive without taking explicitly care about the synchronization of concurrent threads. Moreover, the futures perform this synchronization automatically. 2 Concurrent Haskell Concurrent Haskell was proposed in [PGF96], but its current implementation in the Glasgow Haskell Compiler is slightly modified. In this paper we refer to the concurrent implementation in the GHC (a description can also be found in [Pey01]), and give a short overview of some basic constructs of Concurrent Haskell. Concurrent Haskell extends Haskell by a primitive forkio and by synchronizing variables MVar which behave like single one-place buffers: MVars are either empty or filled. The primitive operation newemptymvar creates an empty MVar. The operation takemvar reads the value of a filled MVar and empties it, and blocks the calling thread if the MVar is empty until the MVar becomes filled. Similarly, putmvar v e writes the expression e into the MVar v, if v is empty, and blocks otherwise until the MVar becomes empty. The primitive for thread

2 2 David Sabel creation in Concurrent Haskell is forkio :: IO () -> IO ThreadId. Applied to an IO-action, a concurrent thread is immediately started to compute the action concurrently. From the perspective of the calling thread, the result is a unique identifier of the concurrent thread, which for instance can be used to kill the concurrent thread using killthread. As already observable by the type of forkio, the result of the concurrently started IO-action must be the unit type () (packed in the IO-monad), and the result of forkio itself is only a thread identifier. Using forkio and MVars one can implement explicit futures. The implementation is shown in Fig. 1. An explicit future is represented by an MVar. The creation of an explicit future first creates an empty MVar and then starts the computation of the action corresponding to the future in a concurrent thread such that after finishing the computation the result is written into the empty MVar. From the view of the calling thread a future in form of an MVar is immediately returned. If the value of the future is needed then the future must be forced explicitly by calling force which reads the MVar. If the future value is not computed already, then a wait situation arises until the concurrent computation is finished. A use case for (explicit) futures is shown on the right hand side of Fig. 1, where a parallel binary tree sum is implemented. This example shows that it is necessary to force the evaluation of futures, and this makes programming with explicit futures uncomfortable: The programmer must be careful to force the future at the right moment. E.g., in the code of the binary tree sum it would be better to force the future futr after computing a + resl, which is possible, but increases and complicates the code. It is more desirable that the future gets (automatically) forced when it is needed such that the programmer does not need to care about explicit forces. Unfortunately, this behavior is not implementable using explicit futures. type EFuture a = MVar a efuture :: IO a IO (EFuture a) efuture act = do ack newemptymvar forkio (act >>= putmvar ack) return ack force :: EFuture a IO a force = takemvar data BTree a = Leaf a Node a (BTree a) (BTree a) treesum (Leaf a) = return a treesum (Node a l r) = do futl efuture (treesum l) futr efuture (treesum r) resl force futl resr force futr let result = (a + resl + resr) in seq result (return result) Fig. 1. Implementation of explicit futures and an example

3 Implementing Concurrent Futures in Concurrent Haskell 3 To solve this problem we use a well-known technique to delay the computation of a sequential monadic IO-computation: We use unsafeinterleaveio which is well-used to delay computations in the IO-monad and to break sequentiality. For instance, the standard implementation of readfile for lazy file reading uses unsafeinterleaveio to delay the reading of the single characters of a file. future :: IO a IO a future code = do ack newemptymvar thread forkio (code >>= putmvar ack) unsafeinterleaveio (do result takemvar ack killthread thread return result) Fig. 2. Implementation of implicit futures In Fig. 2 the implementation of implicit futures is shown. First an empty MVar is created, which will be used to store the result of the concurrent computation. This computation is created using forkio which writes its result into the futures when it becomes available. The last part consist of taking the result, killing the concurrent thread and returning the result. This part is delayed using unsafeinterleaveio. Note, that without the use of unsafeinterleaveio the calling thread would be blocked until the concurrent computation has finished which would not implement the desired behavior of futures. For the shown implementation the calling thread only becomes blocked if it demands the result of an unevaluated future. An implementation of parallel tree sum with implicit futures is shown in Fig. 3. Compared to implementation with explicit futures it is not necessary to force the futures, since they get forced automatically when their value is needed. The seq is used to return a value instead of an unevaluated thunk. treesum (Leaf a) = return a treesum (Node a l r) = do futl future (treesum l) futr future (treesum r) let result = (a + futl + futr) in seq result (return result) Fig. 3. Parallel tree sum with implicit futures It is also possible to implement recursive futures, i.e. at the moment of creation of a future its corresponding code is applied to the future. Fig. 4 shows

4 4 David Sabel the implementation of recursive futures. Reading from the MVar which will store the result of the concurrent computation is performed (but delayed using unsafeinterleaveio) before the concurrent computation is started, i.e. the name of future is known for the concurrent computation and thus can be used as an argument for the code. If a recursive future gets triggered we use the evaluate-function to trigger the takemvar-call recursivefuture :: (a IO a) IO a recursivefuture code = do ack newemptymvar res unsafeinterleaveio (takemvar ack) thread forkio (code res >>= putmvar ack) unsafeinterleaveio (do res evaluate res killthread thread return res ) Fig. 4. Implementation of recursive futures 3 Other Kinds of Futures The futures presented so far are sometimes not sufficient, since their evaluation is not controlled. There is no guarantee, that the futures are evaluated. Like the primitive forkio all concurrent evaluations of futures are abruptly killed if the main thread of the program stops. One may want to have futures with a guarantee that their evaluation is finished before the main program can terminate. A brute-force method is to add a call seqlist [f 1,..., f m] as a last IO-action to the program, where f 1,..., f n are all introduced futures, f i = seq f i () for all i, and seqlist sequentially enforces the evaluation of all elements of a list, i.e. it is defined by seqlist [] = return () seqlist (x:xs) = seq x (seqlist xs) But, again the programmer is responsible to add all futures in this list, which is annoying. We present another implementation which adds a global manager to keep track of the introduced futures an ensures the evaluation of all (strict) futures before terminating the main thread. From the programmer s view this is easy to use. All what is necessary is to add a global wrapper function withfuturesdo around the main function. I.e., instead of writing main = code, one writes main = withfuturesdo code. The global manager is an MVar which contains a list of trivial unit-tupels. In Fig. 5 the manager and the operations newmanager which creates an MVar containing the empty list, register, which registers a future l, by adding (seq l ()) to the list (here seq is only used to ensure correct typing, since

5 Implementing Concurrent Futures in Concurrent Haskell 5 we cannot add l directly), and synchronizeman which ensures the evaluation of all futures stored in the list. type Manager = MVar [()] newmanager :: IO Manager newmanager = newmvar [] register :: a Manager IO () register l man = do list takemvar man putmvar man ((seq l ()):list) synchronizeman :: Manager IO () synchronizeman man = do list takemvar man seqlist list Fig. 5. Implementation of the manager The implementation of strict futures (with a non-recursive variant strictfuture and a recursive version strictrecursivefuture) and the global wrapper function is shown in Fig. 6, where strict futures are implemented like futures where in addition a call to the register-function is performed. We aso create a global variable globalman for the manager using unsafeperformio. It is also possible to introduce another kind of futures, which may be called lazy futures or suspended futures. The desired behavior of those futures is that there is no computation performed unless their value is needed (if their value is needed, then the futures behave like strict futures). Their implementation is really easy using another unsafeinterleaveio-call: lazyfuture :: IO a IO a lazyfuture code = unsafeinterleaveio (strictfuture code) lazyrecursivefuture :: (a IO a) IO a lazyrecursivefuture code = unsafeinterleaveio (strictrecursivefuture code) References Pey01. Simon Peyton Jones. Tackling the awkward squad: monadic input/output, concurrency, exceptions, and foreign-language calls in Haskell. In Ralf Steinbruggen Tony Hoare, Manfred Broy, editor, Engineering theories of software construction, pages IOS-Press, Presented at the 2000 Marktoberdorf Summer School.

6 6 David Sabel withfuturesdo :: IO () IO () withfuturesdo code = do code synchronizeman globalman globalman = unsafeperformio newmanager strictfuture :: IO a IO a strictfuture code = do fut future code register fut globalman return fut strictrecursivefuture :: (a IO a) IO a strictrecursivefuture code = do fut recursivefuture code register fut globalman return fut Fig. 6. The global wrapper and the implementation of strict futures PGF96. S. Peyton Jones, A. Gordon, and S. Finne. Concurrent Haskell. In Proc. 23th Principles of Programming Languages, 1996.

Conservative Concurrency in Haskell

Conservative Concurrency in Haskell Conservative Concurrency in Haskell David Sabel and Manfred Schmidt-Schauß Goethe-University, Frankfurt am Main, Germany LICS 12, Dubrovnik, Croatia Motivation a View on Haskell Purely functional core

More information

A short introduction to concurrency and parallelism in Haskell

A short introduction to concurrency and parallelism in Haskell A short introduction to concurrency and parallelism in Haskell Matteo Pradella matteo.pradella@polimi.it DEIB, Politecnico di Milano 2012-2015 Explicit (classical) concurrency: Threads and MVars As in

More information

An Abstract Machine for Concurrent Haskell with Futures

An Abstract Machine for Concurrent Haskell with Futures An Abstract Machine for Concurrent Haskell with Futures David Sabel Goethe-University, Frankfurt am Main, Germany ATPS 12, Berlin, Germany Motivation Concurrent Haskell (Peyton Jones, Gordon, Finne 1996)

More information

LiU-FP2010 Part II: Lecture 7 Concurrency

LiU-FP2010 Part II: Lecture 7 Concurrency LiU-FP2010 Part II: Lecture 7 Concurrency Henrik Nilsson University of Nottingham, UK LiU-FP2010 Part II: Lecture 7 p.1/36 This Lecture A concurrency monad (adapted from Claessen (1999)) Basic concurrent

More information

COMP3151/9151 Foundations of Concurrency Lecture 8

COMP3151/9151 Foundations of Concurrency Lecture 8 1 COMP3151/9151 Foundations of Concurrency Lecture 8 Liam O Connor CSE, UNSW (and data61) 8 Sept 2017 2 Shared Data Consider the Readers and Writers problem from Lecture 6: Problem We have a large data

More information

Lightweight Concurrency Primitives for GHC. Peng Li Simon Peyton Jones Andrew Tolmach Simon Marlow

Lightweight Concurrency Primitives for GHC. Peng Li Simon Peyton Jones Andrew Tolmach Simon Marlow Lightweight Concurrency Primitives for GHC Peng Li Simon Peyton Jones Andrew Tolmach Simon Marlow The Problem GHC has rich support for concurrency & parallelism: Lightweight threads (fast) Transparent

More information

Imprecise Exceptions - Exceptions in Haskell

Imprecise Exceptions - Exceptions in Haskell Imprecise Exceptions - Exceptions in Haskell Christopher Krauß Universität des Saarlandes Informatik 22nd March 2006 Outline Introduction Exceptions Where do we use exceptions? Different kinds of exceptions

More information

PARALLEL AND CONCURRENT PROGRAMMING IN HASKELL

PARALLEL AND CONCURRENT PROGRAMMING IN HASKELL Introduction Concurrent Haskell Data Parallel Haskell Miscellenous References PARALLEL AND CONCURRENT PROGRAMMING IN HASKELL AN OVERVIEW Eva Burrows BLDL-Talks Department of Informatics, University of

More information

Lightweight Concurrency in GHC. KC Sivaramakrishnan Tim Harris Simon Marlow Simon Peyton Jones

Lightweight Concurrency in GHC. KC Sivaramakrishnan Tim Harris Simon Marlow Simon Peyton Jones Lightweight Concurrency in GHC KC Sivaramakrishnan im Harris Simon Marlow Simon Peyton Jones 1 GHC: Concurrency and Parallelism forkio MVars Safe foreign calls Bound threads Par Monad Asynchronous exceptions

More information

Implementation of Axiomatic Language

Implementation of Axiomatic Language Implementation of Axiomatic Language Walter W. Wilson 1 1 Dept. of Computer Science & Engineering The University of Texas at Arlington Arlington, Texas 76019, USA wwwilson@acm.org Abstract This report

More information

Haskell & functional programming, some slightly more advanced stuff. Matteo Pradella

Haskell & functional programming, some slightly more advanced stuff. Matteo Pradella Haskell & functional programming, some slightly more advanced stuff Matteo Pradella pradella@elet.polimi.it IEIIT, Consiglio Nazionale delle Ricerche & DEI, Politecnico di Milano PhD course @ UniMi - Feb

More information

Concurrent and Multicore Haskell

Concurrent and Multicore Haskell Concurrent and Multicore Haskell These slides are licensed under the terms of the Creative Commons Attribution-Share Alike 3.0 United States License. 1 Concurrent Haskell For responsive programs that multitask

More information

Bright The smart founder s copilot

Bright The smart founder s copilot A note about me SaaS data analytics Haskell for analytics, Rails for presentation Graduated Engineering Physics at LTH Bright The smart founder s copilot Bdellium Fugue Bright Functional Programming in

More information

COMP3151/9151 Foundations of Concurrency Lecture 8

COMP3151/9151 Foundations of Concurrency Lecture 8 1 COMP3151/9151 Foundations of Concurrency Lecture 8 Transactional Memory Liam O Connor CSE, UNSW (and data61) 8 Sept 2017 2 The Problem with Locks Problem Write a procedure to transfer money from one

More information

Advances in Programming Languages

Advances in Programming Languages T O Y H Advances in Programming Languages APL8: Multiparameter Type Classes, Constructor Classes Ian Stark School of Informatics The University of Edinburgh Thursday 4 February Semester 2 Week 4 E H U

More information

CS The IO Monad. Slides from John Mitchell, K Fisher, and S. Peyton Jones

CS The IO Monad. Slides from John Mitchell, K Fisher, and S. Peyton Jones CS 242 2012 The IO Monad Slides from John Mitchell, K Fisher, and S. Peyton Jones Reading: Tackling the Awkward Squad, Sections 1-2 Real World Haskell, Chapter 7: I/O Beauty... Functional programming is

More information

Composable Shared Memory Transactions Lecture 20-2

Composable Shared Memory Transactions Lecture 20-2 Composable Shared Memory Transactions Lecture 20-2 April 3, 2008 This was actually the 21st lecture of the class, but I messed up the naming of subsequent notes files so I ll just call this one 20-2. This

More information

Multicore programming in Haskell. Simon Marlow Microsoft Research

Multicore programming in Haskell. Simon Marlow Microsoft Research Multicore programming in Haskell Simon Marlow Microsoft Research A concurrent web server server :: Socket -> IO () server sock = forever (do acc

More information

Conservative Concurrency in Haskell

Conservative Concurrency in Haskell Conservative Concurrency in Haskell David Sabel and Manfred Schmidt-Schauß Computer Science Institute, Goethe-University, Frankfurt am Main, Germany Email: {sabel,schauss}@ki.informatik.uni-frankfurt.de

More information

Chalmers University of Technology. Without adding any primitives to the language, we dene a concurrency monad transformer

Chalmers University of Technology. Without adding any primitives to the language, we dene a concurrency monad transformer J. Functional Programming 1 (1): 1{000, January 1993 c 1993 Cambridge University Press 1 F U N C T I O N A L P E A R L S A Poor Man's Concurrency Monad Koen Claessen Chalmers University of Technology email:

More information

SAFE HASKELL. David Terei David Mazières. Simon Marlow Simon Peyton Jones

SAFE HASKELL. David Terei David Mazières. Simon Marlow Simon Peyton Jones SAFE HASKELL David Terei David Mazières Stanford University Simon Marlow Simon Peyton Jones Microsoft Research MOTIVATION Haskell is a great language for building secure systems in: Information flow control

More information

References. Monadic I/O in Haskell. Digression, continued. Digression: Creating stand-alone Haskell Programs

References. Monadic I/O in Haskell. Digression, continued. Digression: Creating stand-alone Haskell Programs References Monadic I/O in Haskell Jim Royer CIS 352 March 6, 2018 Chapter 18 of Haskell: the Craft of Functional Programming by Simon Thompson, Addison-Wesley, 2011. Chapter 9 of Learn you a Haskell for

More information

Using Haskell at Bdellium

Using Haskell at Bdellium Using Haskell at Bdellium Parallelism, Lenses, Pipes and Industry Report A note about me (Fredrik Olsen) Studied Engineering Physics at LTH Started working for Bdellium in 2011 We help business grow Developing

More information

Asynchronous communication between threads

Asynchronous communication between threads Asynchronous communication between threads Marcin Kowalczyk Institute of Informatics, Warsaw University Banacha 2, 02-097 Warszawa, Poland September 2, 2005 Abstract In programming

More information

Implementing Mobile Haskell

Implementing Mobile Haskell Chapter 6 Implementing Mobile Haskell André Rauber Du Bois 1, Phil Trinder 1, Hans-Wolfgang Loidl 2 Abstract Mobile computation enables computations to move between a dynamic set of locations and is becoming

More information

A Functional Specification of Effects

A Functional Specification of Effects A Functional Specification of Effects Wouter Swierstra November 2008 Thesis submitted to the University of Nottingham for the degree of Doctor of Philosophy. Abstract This dissertation is about effects

More information

Parallel Functional Programming Lecture 1. John Hughes

Parallel Functional Programming Lecture 1. John Hughes Parallel Functional Programming Lecture 1 John Hughes Moore s Law (1965) The number of transistors per chip increases by a factor of two every year two years (1975) Number of transistors What shall we

More information

Accurate Step Counting

Accurate Step Counting Accurate Step Counting Catherine Hope and Graham Hutton School of Computer Science and IT University of Nottingham, UK {cvh,gmh}@cs.nott.ac.uk Abstract Starting with an evaluator for a language, an abstract

More information

Once Upon a Polymorphic Type

Once Upon a Polymorphic Type Once Upon a Polymorphic Type Keith Wansbrough Computer Laboratory University of Cambridge kw217@cl.cam.ac.uk http://www.cl.cam.ac.uk/users/kw217/ Simon Peyton Jones Microsoft Research Cambridge 20 January,

More information

CS 11 Haskell track: lecture 1

CS 11 Haskell track: lecture 1 CS 11 Haskell track: lecture 1 This week: Introduction/motivation/pep talk Basics of Haskell Prerequisite Knowledge of basic functional programming e.g. Scheme, Ocaml, Erlang CS 1, CS 4 "permission of

More information

University of Utrecht. 1992; Fokker, 1995), the use of monads to structure functional programs (Wadler,

University of Utrecht. 1992; Fokker, 1995), the use of monads to structure functional programs (Wadler, J. Functional Programming 1 (1): 1{000, January 1993 c 1993 Cambridge University Press 1 F U N C T I O N A L P E A R L S Monadic Parsing in Haskell Graham Hutton University of Nottingham Erik Meijer University

More information

Haskell: From Basic to Advanced. Part 3 A Deeper Look into Laziness

Haskell: From Basic to Advanced. Part 3 A Deeper Look into Laziness Haskell: From Basic to Advanced Part 3 A Deeper Look into Laziness Haskell is a lazy language Laziness again A particular function argument is only evaluated when it is needed, and if it is needed then

More information

A Deterministic Multi-Way Rendezvous Library for Haskell

A Deterministic Multi-Way Rendezvous Library for Haskell A Deterministic Multi-Way Rendezvous Library for Haskell Nalini Vasudevan Department of Computer Science Columbia University New York, USA naliniv@cs.columbia.edu Stephen A. Edwards Department of Computer

More information

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

PROGRAMMING IN HASKELL. Chapter 2 - First Steps PROGRAMMING IN HASKELL Chapter 2 - First Steps 0 Glasgow Haskell Compiler GHC is the leading implementation of Haskell, and comprises a compiler and interpreter; The interactive nature of the interpreter

More information

Value Recursion in Monadic Computations

Value Recursion in Monadic Computations Value Recursion in Monadic Computations Levent Erkök OGI School of Science and Engineering, OHSU Advisor: John Launchbury June 24th, 2002 Outline Recursion and effects Motivating examples Value recursion

More information

Simon Peyton Jones (Microsoft Research) Tokyo Haskell Users Group April 2010

Simon Peyton Jones (Microsoft Research) Tokyo Haskell Users Group April 2010 Simon Peyton Jones (Microsoft Research) Tokyo Haskell Users Group April 2010 Geeks Practitioners 1,000,000 10,000 100 1 The quick death 1yr 5yr 10yr 15yr Geeks Practitioners 1,000,000 10,000 100 The slow

More information

Homework 4 Due Midnight, Thursday, 10/2/2014

Homework 4 Due Midnight, Thursday, 10/2/2014 A possible layout for the entire code is given below : CSCI 131 Fall 2014 Homework 4 Due Midnight, Thursday, 10/2/2014 Please turn in your homework solutions online at http://www.dci.pomona.edu/tools-bin/cs131upload.php

More information

Monad Background (3A) Young Won Lim 11/8/17

Monad Background (3A) Young Won Lim 11/8/17 Copyright (c) 2016-2017 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published

More information

which a value is evaluated. When parallelising a program, instances of this class need to be produced for all the program's types. The paper commented

which a value is evaluated. When parallelising a program, instances of this class need to be produced for all the program's types. The paper commented A Type-Sensitive Preprocessor For Haskell Noel Winstanley Department of Computer Science University of Glasgow September 4, 1997 Abstract This paper presents a preprocessor which generates code from type

More information

JVM ByteCode Interpreter

JVM ByteCode Interpreter JVM ByteCode Interpreter written in Haskell (In under 1000 Lines of Code) By Louis Jenkins Presentation Schedule ( 15 Minutes) Discuss and Run the Virtual Machine first

More information

FUNCTIONAL PEARLS The countdown problem

FUNCTIONAL PEARLS The countdown problem To appear in the Journal of Functional Programming 1 FUNCTIONAL PEARLS The countdown problem GRAHAM HUTTON School of Computer Science and IT University of Nottingham, Nottingham, UK www.cs.nott.ac.uk/

More information

Side Effects (3A) Young Won Lim 1/13/18

Side Effects (3A) Young Won Lim 1/13/18 Side Effects (3A) Copyright (c) 2016-2018 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later

More information

Lazy Database Access with Persistent Predicates

Lazy Database Access with Persistent Predicates WFLP 2006 Lazy Database Access with Persistent Predicates Sebastian Fischer Institut für Informatik Universität Kiel Olshausenstraße 40, 24098 Kiel, Germany Abstract Programmers need mechanisms to store

More information

Monad Background (3A) Young Won Lim 11/18/17

Monad Background (3A) Young Won Lim 11/18/17 Copyright (c) 2016-2017 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published

More information

GHC(STG,Cmm,asm) illustrated

GHC(STG,Cmm,asm) illustrated GHC(STG,Cmm,asm) illustrated for hardware persons exploring some mental models and implementations Takenobu T. Rev. 0.02.0 WIP Any sufficiently advanced technology is indistinguishable from magic. Arthur

More information

Optimising Functional Programming Languages. Max Bolingbroke, Cambridge University CPRG Lectures 2010

Optimising Functional Programming Languages. Max Bolingbroke, Cambridge University CPRG Lectures 2010 Optimising Functional Programming Languages Max Bolingbroke, Cambridge University CPRG Lectures 2010 Objectives Explore optimisation of functional programming languages using the framework of equational

More information

CS 457/557: Functional Languages

CS 457/557: Functional Languages CS 457/557: Functional Languages I/O Actions in Haskell Mark P Jones Portland State University 1 Question: If functional programs don t have any side-effects, then how can we ever do anything useful? 2

More information

Introduction. chapter Functions

Introduction. chapter Functions chapter 1 Introduction In this chapter we set the stage for the rest of the book. We start by reviewing the notion of a function, then introduce the concept of functional programming, summarise the main

More information

Polymorphic Contexts FP-Dag 2015

Polymorphic Contexts FP-Dag 2015 Polymorphic Contexts FP-Dag 2015 Doaitse Swierstra January 14, 2015 Goal of this Talk To show you: that lazy evaluation requires a type system which extend beyond system-f how the Utrecht Haskell Compiler

More information

Comparing the Performance of Concurrent Linked-List Implementations in Haskell

Comparing the Performance of Concurrent Linked-List Implementations in Haskell Comparing the Performance of Concurrent Linked-List Implementations in Haskell Martin Sulzmann IT University of Copenhagen, Denmark martin.sulzmann@gmail.com Edmund S. L. Lam National University of Singapore,

More information

Faster Haskell. Neil Mitchell

Faster Haskell. Neil Mitchell Faster Haskell Neil Mitchell www.cs.york.ac.uk/~ndm The Goal Make Haskell faster Reduce the runtime But keep high-level declarative style Full automatic - no special functions Different from foldr/build,

More information

Monads. Lecture 12. Prof. Aiken CS 264 Lecture 12 1

Monads. Lecture 12. Prof. Aiken CS 264 Lecture 12 1 Monads Lecture 12 Prof. Aiken CS 264 Lecture 12 1 Monads A language without side effects can t do I/O Side effects are critical in some applications For expressiveness and/or efficiency Haskell has a general

More information

Programming in Haskell

Programming in Haskell January 25, 2013 Writing Fast Haskell Laziness is both blessing and curse minimum = head. sort is O(n) for carefully-defined sort Memory usage seq, $! and bang patterns Memoization Any thunk is only ever

More information

CS457/557 Functional Languages

CS457/557 Functional Languages CS457/557 Functional Languages Spring 2018 Lecture 1: Course Introduction Andrew Tolmach Portland State University (with thanks to Mark P. Jones) 1 Goals of this course Introduce the beautiful ideas of

More information

Compiling Parallel Algorithms to Memory Systems

Compiling Parallel Algorithms to Memory Systems Compiling Parallel Algorithms to Memory Systems Stephen A. Edwards Columbia University Presented at Jane Street, April 16, 2012 (λx.?)f = FPGA Parallelism is the Big Question Massive On-Chip Parallelism

More information

Asynchronous Exceptions in Haskell

Asynchronous Exceptions in Haskell Asynchronous Exceptions in Haskell Simon Marlow and Simon Peyton Jones Microsoft Research, Cambridge John Reppy Bell Labs, Lucent Technologies Andrew Moran Oregon Graduate Institute Abstract Asynchronous

More information

Applicative, traversable, foldable

Applicative, traversable, foldable Applicative, traversable, foldable Advanced functional programming - Lecture 3 Wouter Swierstra 1 Beyond the monad So far, we have seen how monads define a common abstraction over many programming patterns.

More information

Introduction to Functional Programming and Haskell. Aden Seaman

Introduction to Functional Programming and Haskell. Aden Seaman Introduction to Functional Programming and Haskell Aden Seaman Functional Programming Functional Programming First Class Functions Expressions (No Assignment) (Ideally) No Side Effects Different Approach

More information

Concurrent Programming Method for Embedded Systems

Concurrent Programming Method for Embedded Systems Concurrent Programming Method for Embedded Systems Norbert Schramm UVA, 24000 Subotica, Serbia norbert.schramm@gmail.com Anita Sabo Polytechnical Engineering College Subotica M. Oreškovića 16, 24000 Subotica,

More information

FUNCTIONAL PEARL. Global variables in Haskell

FUNCTIONAL PEARL. Global variables in Haskell JFP 14 (5): 489 502, September 2004. c 2004 Cambridge University Press DOI: 10.1017/S0956796802004471 Printed in the United Kingdom 489 FUNCTIONAL PEARL Global variables in Haskell JOHN HUGHES Department

More information

Introduction to Haskell

Introduction to Haskell Introduction to Haskell Matt Mullins Texas A&M Computing Society October 6, 2009 Matt Mullins (TACS) Introduction to Haskell October 6, 2009 1 / 39 Outline Introduction to Haskell Functional Programming

More information

Applicative, traversable, foldable

Applicative, traversable, foldable Applicative, traversable, foldable Advanced functional programming - Lecture 4 Wouter Swierstra and Alejandro Serrano 1 Beyond the monad So far, we have seen how monads define a common abstraction over

More information

Improving the Performance of Parallel LISP by Compile Time Analysis

Improving the Performance of Parallel LISP by Compile Time Analysis Improving the Performance of Parallel LISP by Compile Time Analysis J~irgen Knopp Siemens AG. Otto Hahn Ring 6 8000 Miinchen 83, Germany email: jk@ km21.zfe.siemens.de Abstract This paper presents a method

More information

Shared state model. April 3, / 29

Shared state model. April 3, / 29 Shared state April 3, 2012 1 / 29 the s s limitations of explicit state: cells equivalence of the two s programming in limiting interleavings locks, monitors, transactions comparing the 3 s 2 / 29 Message

More information

Advanced Topics in Programming Languages Lecture 2 - Introduction to Haskell

Advanced Topics in Programming Languages Lecture 2 - Introduction to Haskell Advanced Topics in Programming Languages Lecture 2 - Introduction to Haskell Ori Bar El Maxim Finkel 01/11/17 1 History Haskell is a lazy, committee designed, pure functional programming language that

More information

Haskell Monads CSC 131. Kim Bruce

Haskell Monads CSC 131. Kim Bruce Haskell Monads CSC 131 Kim Bruce Monads The ontological essence of a monad is its irreducible simplicity. Unlike atoms, monads possess no material or spatial character. They also differ from atoms by their

More information

A Functional Graph Library

A Functional Graph Library A Functional Graph Library Christian Doczkal Universität des Saarlandes Abstract. Algorithms on graphs are of great importance, both in teaching and in the implementation of specific problems. Martin Erwig

More information

Static Contract Checking for Haskell

Static Contract Checking for Haskell Static Contract Checking for Haskell Dana N. Xu INRIA France Work done at University of Cambridge Simon Peyton Jones Microsoft Research Cambridge Joint work with Koen Claessen Chalmers University of Technology

More information

Homework 4 Due Friday, 10/1/10

Homework 4 Due Friday, 10/1/10 Homework 4 Due Friday, 10/1/10 Please turn in your homework solutions online at http://www.dci.pomona.edu/tools-bin/cs131upload.php before the beginning of class on the due date. Note that some of the

More information

Stream Fusion. Wouter Swierstra 30/11/2010

Stream Fusion. Wouter Swierstra 30/11/2010 Stream Fusion Wouter Swierstra 30/11/2010 1 Exercise: Write a function that sums the square of all the odd integers between two arguments m and n. 2 Haskell solution f : (Int,Int) -> Int f = sum. map square.

More information

Idris, a language with dependent types Extended Abstract

Idris, a language with dependent types Extended Abstract Idris, a language with dependent types Extended Abstract Edwin Brady School of Computer Science, University of St Andrews, St Andrews, Scotland. Email: eb@cs.st-andrews.ac.uk. Tel: +44-1334-463253, Fax:

More information

Lazy Functional Programming in Haskell

Lazy Functional Programming in Haskell Lazy Functional Programming in Haskell David Raymond Christiansen 25 November, 2013 What is Haskell? 2 What is Haskell? Pure functional language: no side effects 2 What is Haskell? Pure functional language:

More information

Assigning to a Variable

Assigning to a Variable What is the result of this program? Is it 0 or 1? Assigning to a Variable let f = proc(x) set x = 1 in let y = 0 in { (f y); y } 1 Assigning to a Variable let f = proc(x) set x = 1 in let y = 0 in { (f

More information

Tackling the Awkward Squad: monadic input/output, concurrency, exceptions, and foreign-language calls in Haskell

Tackling the Awkward Squad: monadic input/output, concurrency, exceptions, and foreign-language calls in Haskell Tackling the Awkward Squad: monadic inputoutput, concurrency, exceptions, and foreign-language calls in Haskell Simon PEYTON JONES Microsoft Research, Cambridge simonpj@microsoft.com http:research.microsoft.comuserssimonpj

More information

Structural polymorphism in Generic Haskell

Structural polymorphism in Generic Haskell Structural polymorphism in Generic Haskell Andres Löh andres@cs.uu.nl 5 February 2005 Overview About Haskell Genericity and other types of polymorphism Examples of generic functions Generic Haskell Overview

More information

Extended Static Checking for Haskell (ESC/Haskell)

Extended Static Checking for Haskell (ESC/Haskell) Extended Static Checking for Haskell (ESC/Haskell) Dana N. Xu University of Cambridge advised by Simon Peyton Jones Microsoft Research, Cambridge Program Errors Give Headache! Module UserPgm where f ::

More information

Type families and data kinds

Type families and data kinds Type families and data kinds AFP Summer School Wouter Swierstra 1 Today How do GADTs work? Kinds beyond * Programming with types 2 Calling functions on vectors Given two vectors xs : Vec a n and ys : Vec

More information

Reasoning about programs. Chapter 9 of Thompson

Reasoning about programs. Chapter 9 of Thompson Reasoning about programs Chapter 9 of Thompson Proof versus testing A proof will state some property of a program that holds for all inputs. Testing shows only that a property holds for a particular set

More information

Depending on Types. Stephanie Weirich University of Pennsylvania

Depending on Types. Stephanie Weirich University of Pennsylvania Depending on Types Stephanie Weirich University of Pennsylvania TDD Type- Driven Development with Dependent Types The Agda Experience On 2012-01- 11 03:36, Jonathan Leivent wrote on the Agda mailing list:

More information

LVars: Lattice-based Data Structures for Deterministic Parallelism. FHPC 13, Boston, MA, USA September 23, 2013

LVars: Lattice-based Data Structures for Deterministic Parallelism. FHPC 13, Boston, MA, USA September 23, 2013 LVars: Lattice-based Data Structures for Deterministic Parallelism Lindsey Kuper and Ryan Newton Indiana University FHPC 13, Boston, MA, USA September 23, 2013 What does this program evaluate to? p = do

More information

MPI bindings in Haskell

MPI bindings in Haskell in Haskell Exploring the design space Outline Overview of MPI and demo. The Haskell FFI. C2HS, a tool for helping to write bindings to C. A first stab. A second stab. A tricky case: non-blocking receive.

More information

Fastest Lambda First. Neil Mitchell.

Fastest Lambda First. Neil Mitchell. Fastest Lambda First λ Neil Mitchell www.cs.york.ac.uk/~ndm/ The Problem Count the number of lines in a file = 0 test = 1 test\n = 1 test\ntest = 2 Read from the console Using getchar only No buffering

More information

Introduction to OpenCL!

Introduction to OpenCL! Lecture 6! Introduction to OpenCL! John Cavazos! Dept of Computer & Information Sciences! University of Delaware! www.cis.udel.edu/~cavazos/cisc879! OpenCL Architecture Defined in four parts Platform Model

More information

G Programming Languages - Fall 2012

G Programming Languages - Fall 2012 G22.2110-003 Programming Languages - Fall 2012 Lecture 3 Thomas Wies New York University Review Last week Names and Bindings Lifetimes and Allocation Garbage Collection Scope Outline Control Flow Sequencing

More information

Parallel & Concurrent Haskell 1: Basic Pure Parallelism. Simon Marlow (Microsoft Research, Cambridge, UK)

Parallel & Concurrent Haskell 1: Basic Pure Parallelism. Simon Marlow (Microsoft Research, Cambridge, UK) Parallel & Concurrent Haskell 1: Basic Pure Parallelism Simon Marlow (Microsoft Research, Cambridge, UK) Background The parallelism landscape is getting crowded Every language has their own solution(s)

More information

amiri advanced databases '05

amiri advanced databases '05 More on indexing: B+ trees 1 Outline Motivation: Search example Cost of searching with and without indices B+ trees Definition and structure B+ tree operations Inserting Deleting 2 Dense ordered index

More information

Simple Unification-based Type Inference for GADTs

Simple Unification-based Type Inference for GADTs Simple Unification-based Type Inference for GADTs Stephanie Weirich University of Pennsylvania joint work with Dimitrios Vytiniotis, Simon Peyton Jones and Geoffrey Washburn Overview Goal: Add GADTs to

More information

Concurrent Haskell. Andrew Gordon. and 4). transformations that preserve the correctness of. a sequential Haskell program also preserve correctness

Concurrent Haskell. Andrew Gordon. and 4). transformations that preserve the correctness of. a sequential Haskell program also preserve correctness Concurrent Haskell Simon Peyton Jones University of Glasgow Andrew Gordon University of Cambridge Sigbjorn Finne University of Glasgow Abstract Some applications are most easily expressed in a programming

More information

Implementing Coroutines with call/cc. Producer/Consumer using Coroutines

Implementing Coroutines with call/cc. Producer/Consumer using Coroutines Implementing Coroutines with call/cc Producer/Consumer using Coroutines Coroutines are a very handy generalization of subroutines. A coroutine may suspend its execution and later resume from the point

More information

Haskell in the corporate environment. Jeff Polakow October 17, 2008

Haskell in the corporate environment. Jeff Polakow October 17, 2008 Haskell in the corporate environment Jeff Polakow October 17, 2008 Talk Overview Haskell and functional programming System description Haskell in the corporate environment Functional Programming in Industry

More information

TEST CONCURRENT PROGRAMMING. code: date: 19 June 2015 time:

TEST CONCURRENT PROGRAMMING. code: date: 19 June 2015 time: TEST CONCURRENT PROGRAMMING code: 01400537 date: 19 June 015 time: 13.45 16.45 - This is an open book examination. You are allowed to have a copy of Java Concurrency in Practice, and a copy of the (unannotated)

More information

CSE 230. Parallelism. The Grand Challenge. How to properly use multi-cores? Need new programming models! Parallelism vs Concurrency

CSE 230. Parallelism. The Grand Challenge. How to properly use multi-cores? Need new programming models! Parallelism vs Concurrency The Grand Challenge CSE 230 Parallelism How to properly use multi-cores? Need new programming models! Slides due to: Kathleen Fisher, Simon Peyton Jones, Satnam Singh, Don Stewart Parallelism vs Concurrency

More information

Language. Institut fur Informatik, Technische Universitat Munchen. Abstract

Language. Institut fur Informatik, Technische Universitat Munchen.   Abstract Modelling Inheritance in an Algebraic Specication Language Dieter Nazareth Institut fur Informatik, Technische Universitat Munchen Postfach 20 24 20, D-8000 Munchen 2, Germany e-mail: nazareth@informatik.tu-muenchen.de

More information

A Monadic Account of First-class Synchronous Events

A Monadic Account of First-class Synchronous Events A Monadic Account of First-class Synchronous Events Matthew Fluet Cornell University fluet@cs.cornell.edu January 20, 2006 Abstract 1 Introduction A value of type IO a is an action that, when performed,

More information

ECE 3574: Applied Software Design. Threads

ECE 3574: Applied Software Design. Threads ECE 3574: Applied Software Design Threads Today we are going to start looking at threads, multiple executing programs within the same process that share the code segment and heap, but have separate stacks.

More information

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub Lebanese University Faculty of Science Computer Science BS Degree Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub 2 Crash Course in JAVA Classes A Java

More information

Debugging in Haskell

Debugging in Haskell Debugging in Haskell Lasse Folger June 29, 2016 Abstract In this paper we will give an overview on techniques to debug Haskell programs. At first the problems are presented which occur, when debugging

More information

doi: / Composable Memory Transactions By Tim Harris, Simon Marlow, Simon Peyton Jones, and Maurice Herlihy

doi: / Composable Memory Transactions By Tim Harris, Simon Marlow, Simon Peyton Jones, and Maurice Herlihy doi:10.1145/1378704.1378725 Composable Memory Transactions By Tim Harris, Simon Marlow, Simon Peyton Jones, and Maurice Herlihy Abstract Writing concurrent programs is notoriously difficult and is of increasing

More information

Implicit and Explicit Parallel Programming in Haskell

Implicit and Explicit Parallel Programming in Haskell Implicit and Explicit Parallel Programming in Haskell Research Report YALEU/DCS/RR-982 Mark P. Jones and Paul Hudak Department of Computer Science Yale University New Haven, CT 06520-2158 {jones-mark,hudak-paul}@cs.yale.edu

More information

An introduction to functional programming. July 23, 2010

An introduction to functional programming. July 23, 2010 An introduction to functional programming July 23, 2010 About Outline About About What is functional programming? What is? Why functional programming? Why? is novel. is powerful. is fun. About A brief

More information