Don Syme, Principal Researcher Microsoft Research, Cambridge

Similar documents
F# Succinct, Expressive, Functional. The F# Team Microsoft Developer Division Microsoft Research

The F# Team Microsoft

F# Succinct, Expressive, Efficient. The F# Team Microsoft Developer Division, Redmond Microsoft Research, Cambridge

Why is Microsoft investing in Functional Programming?

F# Succinct, Expressive, Efficient Functional Programming for.net

High Performance and Productivity Computing with Windows HPC George Yan. Group Manager Windows HPC Microsoft China

Functional-first Programming in an Information-Rich World. Dr Kenji Takeda Microsoft Research Connections

Microsoft Research Cambridge

MF#K - Introduction to F# GroupM Mødegruppe for Ƒunktionelle Københavnere (MF#K)

Expert F# Don Syme, Adam Granicz, and Antonio Cisternino

Reactive programming, WinForms,.NET. Björn Dagerman

Introduce C# as Object Oriented programming language. Explain, tokens,

Learning F# and the Functional Point of View. Robert Pickering, LexiFi

Programming Safe Agents in Blueprint. Alex Muscar University of Craiova

Asynchronous Programming Model (APM) 1 Calling Asynchronous Methods Using IAsyncResult 4 Blocking Application Execution by Ending an Async Operation

C# Asynchronous Programming Model

CS 160: Interactive Programming

Frege. purely functional programming on the JVM. JUG Luzern 2016

Programming Languages and Techniques (CIS120)

CSCI 2041: Basic OCaml Syntax and Features

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Winter 2013

CITS 3242 Programming Paradigms Part II. Topic 10: Imperative Programming

Upcoming Features in C# Mads Torgersen, MSFT

Threads are lightweight processes responsible for multitasking within a single application.

Computer Science II Lecture 1 Introduction and Background

Parallel Programming: Background Information

Parallel Programming: Background Information

EEE-448 COMPUTER NETWORKS (Programming) Week -6 C# & IP Programming. The StringBuilder Class. StringBuilder Classes. StringBuilder with Append

MCSA Universal Windows Platform. A Success Guide to Prepare- Programming in C# edusum.com

C#.Net. Course Contents. Course contents VT BizTalk. No exam, but laborations

CS 11 Haskell track: lecture 1

Asynchronous Computations

So what does studying PL buy me?

APS105. Modularity. C pre-defined functions 11/5/2013. Functions. Functions (and Pointers) main. Modularity. Math functions. Benefits of modularity:

Programovací jazyky F# a OCaml. Chapter 6. Sequence expressions and computation expressions (aka monads)

Frege. purely functional programming on the JVM. GOTO Berlin 2015

Functional Programming in Java. CSE 219 Department of Computer Science, Stony Brook University

Abram Hindle Kitchener Waterloo Perl Monger October 19, 2006

Clojure Concurrency Constructs. CSCI 5828: Foundations of Software Engineering Lecture 12 10/02/2014

Recap. Recap. If-then-else expressions. If-then-else expressions. If-then-else expressions. If-then-else expressions

C # 7, 8, and beyond: language features from design to release to IDE support. Kevin

Problems with Concurrency. February 19, 2014

Synchronising Threads

Functional Programming and Parallel Computing

Programming Languages and Techniques (CIS120)

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

Swift 5, ABI Stability and

C# Threading. Hans-Wolfgang Loidl School of Mathematical and Computer Sciences, Heriot-Watt University, Edinburgh

ENGI 1020 Introduction to Computer Programming J U L Y 5, R E Z A S H A H I D I

F# for Industrial Applications Worth a Try?

UNIVERSITY OF THE FREE STATE DEPARTMENT OF COMPUTER SCIENCE AND INFORMATICS RIS 214 MODULE TEST 1

Mutable Data Types. Prof. Clarkson Fall A New Despair Mutability Strikes Back Return of Imperative Programming

CS457/557 Functional Languages

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

Programming Languages and Techniques (CIS120)

Grafting Functional Support on Top of an Imperative Language

CSCI-GA Scripting Languages

Delegate.Sandbox: I/O side-effects safe computations in Prosa F#unctional Copenhageners Meetup Group (MF#K)

Dealing with Bugs. Kenneth M. Anderson University of Colorado, Boulder CSCI 5828 Lecture 27 04/21/2009

Systems Programming & Scripting

Exercise Session Week 8

Object Oriented Programming in C#

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

Simplifying Asynchronous Programming with Microsoft Visual Studio Async CTP

CMSC330. Objects, Functional Programming, and lambda calculus

Inside core.async Channels. Rich Hickey

Rocking with Racket. Marc Burns Beatlight Inc

CS 177 Recitation. Week 1 Intro to Java

Programming Languages and Techniques (CIS120)

DOT NET Syllabus (6 Months)

MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Computer Science. Issued: Wed. 26 April 2017 Due: Wed.

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Fall 2011

Sri Lanka Institute of Information Technology System Programming and Design II Year 3 Tutorial 06

Tomas Petricek. F# Language Overview

Hands-On Lab. Introduction to F# Lab version: Last updated: 12/10/2010. Page 1

News. Programming Languages. Recap. Recap: Environments. Functions. of functions: Closures. CSE 130 : Fall Lecture 5: Functions and Datatypes

SPIN Operating System

Exercise Session Week 8

Background. $VENDOR wasn t sure either, but they were pretty sure it wasn t their code.

Recap: Functions as first-class values

Multicore programming in Haskell. Simon Marlow Microsoft Research

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

DESIGN, EVOLUTION AND USE

Processes and Threads. Industrial Programming. Processes and Threads (cont'd) Processes and Threads (cont'd)

CS 381: Programming Language Fundamentals

Note 12/1/ Review of Inheritance Practice: Please write down 10 most important facts you know about inheritance...

assembler Machine Code Object Files linker Executable File

CSCE 314 Programming Languages. Type System

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Concurrent Programming

Persistent Data Structures and Managed References

Introduction to the D programming language. Marc Fuentes - SED

Thread Safety. Review. Today o Confinement o Threadsafe datatypes Required reading. Concurrency Wrapper Collections

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Programming Languages

G Programming Languages - Fall 2012

CS252 Advanced Programming Language Principles. Prof. Tom Austin San José State University Fall 2013

Next: What s in a name? Programming Languages. Data model in functional PL. What s in a name? CSE 130 : Fall Lecture 13: What s in a Name?

HCIM SUMMER WORKSHOP Introduction to C#

Transcription:

F# Succinct, Expressive, Functional Don Syme, Principal Researcher Microsoft Research, Cambridge

Topics What is F# about? Some Simple F# Programming A Taste of Parallel/Reactive with F#

What is F# about? Or: Why is Microsoft investing in functional programming anyway?

Simplicity

Economics

Fun, Fun and More Fun!

Simplicity

Code! //F# open System let a = 2 Console.WriteLine a More Noise Than Signal! //C# using System; namespace ConsoleApplication1 class Program static int a() return 2; static void Main(string[] args) Console.WriteLine(a);

Pleasure Pain type Command = Command of (Rover -> unit) let BreakCommand = Command(fun rover -> rover.accelerate(-1.0)) abstract class Command public virtual void Execute(); abstract class MarsRoverCommand : Command protected MarsRover Rover get; priva let TurnLeftCommand = Command(fun rover -> rover.rotate(-5.0<degs>)) public MarsRoverCommand(MarsRover rove this.rover = rover; class BreakCommand : MarsRoverCommand public BreakCommand(MarsRover rover) : base(rover) public override void Execute() Rover.Rotate(-5.0); class TurnLeftCommand : MarsRoverCommand public TurnLeftCommand(MarsRover rover

Pleasure let swap (x, y) = (y, x) Pain Tuple<U,T> Swap<T,U>(Tuple<T,U> t) return new Tuple<U,T>(t.Item2, t.item1) let rotations (x, y, z) = [ (x, y, z); (z, x, y); (y, z, x) ] ReadOnlyCollection<Tuple<T,T,T>> Rotations<T>(Tuple<T,T,T> t) new ReadOnlyCollection<int> (new Tuple<T,T,T>[] ); new Tuple<T,T,T>(t.Item1,t.Item2,t.Item3); new Tuple<T,T,T>(t.Item3,t.Item1,t.Item2); new Tuple<T,T,T>(t.Item2,t.Item3,t.Item1); let reduce f (x, y, z) = f x + f y + f z int Reduce<T>(Func<T,int> f,tuple<t,t,t> t) return f(t.item1) + f(t.item2) + f (t.item3)

Pleasure type Expr = True And of Expr * Expr Nand of Expr * Expr Or of Expr * Expr Xor of Expr * Expr Not of Expr Pain public abstract class Expr public abstract class UnaryOp :Expr public Expr First get; private set; public UnaryOp(Expr first) this.first = first; public abstract class BinExpr : Expr public Expr First get; private set; public Expr Second get; private set; public BinExpr(Expr first, Expr second) this.first = first; this.second = second;

You Can Interoperate With Everything

People Love Programming in F#

F#: Influences OCaml F# C#/.NET Similar core language Similar object model

F#: Combining Paradigms I've been coding in F# lately, for a production task. F# allows you to move smoothly in your programming style... I start with pure functional code, shift slightly towards an object-oriented style, and in production code, I sometimes have to do some imperative programming. I can start with a pure idea, and still finish my project with realistic code. You're never disappointed in any phase of the project! Julien Laugel, Chief Software Architect, www.eurostocks.com

F#: The Combination Counts! Scalable Libraries Explorative Succinct Interoperable Statically Typed F# Efficient

Let s WebCrawl...

Orthogonal & Unified Constructs Let let simplify your life Bind a static value let data = (1, 2, 3) Type inference. The safety of C# with the succinctness of a scripting language Bind a static function Bind a local value let f (a, b, c) = let sum = a + b + c let g x = sum + x*x (g a, g b, g c) Bind a local function

Fundamentals - Whitespace Matters let computederiative f x = let p1 = f (x - 0.05) let p2 = f (x + 0.05) (p2 p1) / 0.1 Offside (bad indentation)

Fundamentals - Whitespace Matters let computederiative f x = let p1 = f (x - 0.05) let p2 = f (x + 0.05) (p2 p1) / 0.1

Orthogonal & Unified Constructs Functions: like delegates + unified and simple (fun x -> x + 1) let f x = x + 1 (f, f) val f : int -> int One simple mechanism, many uses Declare a function Lambdapredicate = 'T -> bool A pair of function values A function type send = 'T -> unit threadstart = unit -> unit comparer = 'T -> 'T -> int hasher = 'T -> int equality = 'T -> 'T -> bool

Functional Pipelines The pipeline operator x > f

Functional Pipelines Successive stages in a pipeline x > f1 > f2 > f3

Immutability the norm Data is immutable by default Values may not be changed Not Mutate Copy & Update

In Praise of Immutability Immutable objects can be relied upon Immutable objects can transfer between threads Immutable objects can be aliased safely Immutable objects lead to (different) optimization opportunities

Weakly Typed? Slow? //F# #light open System let a = 2 Console.WriteLine(a) //C# using System; namespace ConsoleApplication1 class Program static int a() return 2; Looks Weakly typed? Maybe Dynamic? args) static void Main(string[] Console.WriteLine(a);

Typed Untyped F# Yet rich, dynamic Efficient Yet succinct Interpreted Reflection Invoke

F# Objects

F# - Objects + Functional type Vector2D (dx:double, dy:double) = member v.dx = dx Inputs to object construction member v.dy = dy Exported properties member v.length = sqrt (dx*dx+dy*dy) member v.scale (k) = Vector2D (dx*k,dy*k) Exported method

F# - Objects + Functional type Vector2D(dx:double,dy:double) = let norm2 = dx*dx+dy*dy Internal (precomputed) values and functions member v.dx = dx member v.dy = dy member v.length = sqrt(norm2) member v.norm2 = norm2

F# - Objects + Functional type HuffmanEncoding(freq:seq<char*int>) = Immutable inputs... < 50 lines of beautiful functional code>... member x.encode(input: seq<char>) = encode(input) member x.decode(input: seq<char>) = decode(input) Internal tables Publish access

F# - Objects + Functional type Vector2D(dx:double,dy:double) = let mutable currdx = dx Internal state let mutable currdx = dy member v.dx = currdx member v.dy = currdy member v.move(x,y) = currdx <- currdx+x currdy <- currdy+y Publish internal state Mutate internal state

Interlude: Case Study

The Scale of Things Weeks of data in training: N,000,000,000 impressions, 6TB data 2 weeks of CPU time during training: 2 wks 7 days 86,400 sec/day = 1,209,600 seconds Learning algorithm speed requirement: N,000 impression updates / sec N00.0 µs per impression update

F# and adcenter 4 week project, 4 machine learning experts 100million probabilistic variables Processes 6TB of training data Real time processing

F# s powerful type inference means less typing, more thinking AdPredict: What We Observed Quick Coding Agile Coding Scripting Performance Memory-Faithful Succinct Symbolic.NET Integration Type-inferred code is easily refactored Hands-on exploration. Immediate scaling to massive data sets mega-data structures, 16GB machines Live in the domain, not the language Schema compilation and Schedules Especially Excel, SQL Server

Smooth Transitions Researcher s Brain Realistic, Efficient Code Realistic, Efficient Code Component Component Deployment

F# Async/Parallel

F# is a Parallel Language (Multiple active computations) F# is a Reactive Language (Multiple pending reactions) e.g. GUI Event Page Load Timer Callback Query Response HTTP Response Web Service Response Disk I/O Completion Agent Gets Message

async... A Building Block for Writing Reactive Code async... For users: You can run it, but it may take a while Or, your builder says... OK, I can do the job, but I might have to talk to someone else about it. I ll get back to you when I m done

async... async let! image = ReadAsync "cat.jpg" let image2 = f image do! WriteAsync image2 "dog.jpg" do printfn "done!" return image2 Asynchronous "nonblocking" action Continuation/ Event callback You're actually writing this (approximately): async.delay(fun () -> async.bind(readasync "cat.jpg", (fun image -> let image2 = f image async.bind(writeasync "dog.jpg",(fun () -> printfn "done!" async.return())))))

Code: Web Translation

Typical F# Reactive Architecture Async.Start (async... Single Threaded GUI Or Async.Parallel [... ] new Agent<_>(async... )... (queued) Single Threaded Page Handler Or Command Line Driver new WebCrawler<_>() Internally: new Agent<_>(...)... event x.started event x.crawledpage event x.finished

Taming Asynchronous I/O using System; using System.IO; using System.Threading; public static void ReadInImageCallback(IAsyncResult asyncresult) public static void ProcessImagesInBulk() public class BulkImageProcAsync ImageStateObject state = (ImageStateObject)asyncResult.AsyncState; Stream stream = state.fs; Console.WriteLine("Processing images... "); public const String ImageBaseName = "tmpimage-"; int bytesread = stream.endread(asyncresult); long t0 = Environment.TickCount; public const int numimages = 200; if (bytesread!= numpixels) NumImagesToFinish = numimages; public const int numpixels = 512 * 512; throw new Exception(String.Format AsyncCallback readimagecallback = new ("In ReadInImageCallback, got the wrong number of " + AsyncCallback(ReadInImageCallback); // ProcessImage has a simple O(N) loop, and you can "bytes vary from the number the image: 0.", bytesread)); for (int i = 0; i < numimages; i++) // of times you repeat that loop to make the ProcessImage(state.pixels, application more CPU- state.imagenum); // bound or more IO-bound. stream.close(); ImageStateObject state = new ImageStateObject(); public static int processimagerepeats = 20; state.pixels = new byte[numpixels]; // Now write out the image. state.imagenum = i; // Threads must decrement NumImagesToFinish, // Using and protect asynchronous I/O here appears not to be best practice. // Very large items are read only once, so you can make the // their access to it through a mutex. // It ends up swamping the threadpool, because the threadpool // buffer on the FileStream very small to save memory. public static int NumImagesToFinish = numimages; // threads are blocked on I/O requests that were just queued tofilestream fs = new FileStream(ImageBaseName + i + ".tmp", public static Object[] NumImagesMutex = new // Object[0]; the threadpool. FileMode.Open, FileAccess.Read, FileShare.Read, 1, true); // WaitObject is signalled when all image FileStream processing fs is = done. new FileStream(ImageBaseName + state.imagenum + state.fs = fs; public static Object[] WaitObject = new Object[0]; ".done", FileMode.Create, FileAccess.Write, FileShare.None, public class ImageStateObject public byte[] pixels; public int imagenum; public let ProcessImageAsync FileStream fs; () = let! pixels 4096, false); fs.write(state.pixels, 0, numpixels); fs.close(); // This application model uses too much memory. async let instream = File.OpenRead(sprintf // Releasing memory "Image%d.tmp" as soon as i) possible is a good idea, = instream.readasync(numpixels) // especially global state. let pixels' = TransformImage(pixels,i) state.pixels = null; let outstream = File.OpenWrite(sprintf fs = null; "Image%d.done" i) do! outstream.writeasync(pixels') // Record that an image is finished now. do Console.WriteLine "done!" lock (NumImagesMutex) let ProcessImagesAsyncWorkflow() = NumImagesToFinish--; Async.Run (Async.Parallel if (NumImagesToFinish == 0) [ for i in 1.. numimages -> ProcessImageAsync i ]) Monitor.Enter(WaitObject); Monitor.Pulse(WaitObject); Monitor.Exit(WaitObject); fs.beginread(state.pixels, 0, numpixels, readimagecallback, state); // Determine whether all images are done being processed. // If not, block until all are finished. bool mustblock = false; lock (NumImagesMutex) if (NumImagesToFinish > 0) mustblock = true; if (mustblock) Processing 200 images in parallel Console.WriteLine("All worker threads are queued. " + " Blocking until they complete. numleft: 0", NumImagesToFinish); Monitor.Enter(WaitObject); Monitor.Wait(WaitObject); Monitor.Exit(WaitObject); long t1 = Environment.TickCount; Console.WriteLine("Total time processing images: 0ms", (t1 - t0));

Units of Measure

let EarthMass = 5.9736e24<kg> // Average between pole and equator radii let EarthRadius = 6371.0e3<m> // Gravitational acceleration on surface of Earth let g = PhysicalConstants.G * EarthMass / (EarthRadius * EarthRadius)

8 Ways to Learn FSI.exe Samples Included Go to definition Lutz Reflector http://cs.hubfs.net Codeplex Fsharp Samples Books ML/Erlang/Haskell/ Clojure

Books about F# Visit www.fsharp.net

Books about F# Visit www.fsharp.net

F# Ahead F# will be a supported language in Visual Studio 2010 Next stop: Visual Studio 2010 Beta 2 Look for it soon!

Questions & Discussion