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

Similar documents
Don Syme, Principal Researcher Microsoft Research, Cambridge

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

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

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

Microsoft Research Cambridge

Why is Microsoft investing in Functional Programming?

Option Values, Arrays, Sequences, and Lazy Evaluation

F# Overview: Immutable Data + Pure Func7ons

Tomas Petricek. F# Language Overview

The F# Team Microsoft

CSCI-GA Scripting Languages

Abram Hindle Kitchener Waterloo Perl Monger October 19, 2006

Some Advanced ML Features

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

F# for Industrial Applications Worth a Try?

Programming Languages and Techniques (CIS120)

Recap: Functions as first-class values

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

02157 Functional Programming. Michael R. Ha. Lecture 3: Programming as a model-based activity. Michael R. Hansen

Programovací jazyky F# a OCaml. Chapter 5. Hiding recursion using function-as-values

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

So what does studying PL buy me?

Introduction to OCaml

CS457/557 Functional Languages

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

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

Functional Programming. Pure Functional Programming

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

Formal Semantics. Prof. Clarkson Fall Today s music: Down to Earth by Peter Gabriel from the WALL-E soundtrack

List Functions, and Higher-Order Functions

More Lambda Calculus and Intro to Type Systems

CMSC 330: Organization of Programming Languages. Rust Basics

CIS 120 Midterm II November 8, 2013 SOLUTIONS

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

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

The Substitution Model. Nate Foster Spring 2018

Imperative programming in F#

Grade Weights. Language Design and Overview of COOL. CS143 Lecture 2. Programming Language Economics 101. Lecture Outline

Outline. What is semantics? Denotational semantics. Semantics of naming. What is semantics? 2 / 21

CS558 Programming Languages

Programming in Scala Second Edition

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

CMSC330. Objects, Functional Programming, and lambda calculus

CS558 Programming Languages

C# Programming in the.net Framework

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

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

L3 Programming September 19, OCaml Cheatsheet

Programmazione Avanzata

CS 381: Programming Language Fundamentals

Programming Languages and Techniques (CIS120)

CMSC 330: Organization of Programming Languages

CIS 120 Midterm II November 8, Name (printed): Pennkey (login id):

Concepts of Programming Languages

CSCI 2041: Basic OCaml Syntax and Features

Topic 1: Introduction

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Basics

The Substitution Model

Objectives. Introduce the core C# language features class Main types variables basic input and output operators arrays control constructs comments

Type Declarations. Γ <num> : num [... <id> τ... ] <id> : τ Γ true : bool Γ false : bool Γ e 1 : num Γ e 2 : num Γ. {+ e 1 e 2 } : num

Programming Languages and Techniques (CIS120)

Event Handling Java 7

CS558 Programming Languages

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

Functional Programming. Big Picture. Design of Programming Languages

CSCE 314 Programming Languages

Functions. Nate Foster Spring 2018

Handout 2 August 25, 2008

More Lambda Calculus and Intro to Type Systems

CS-XXX: Graduate Programming Languages. Lecture 22 Class-Based Object-Oriented Programming. Dan Grossman 2012

Learn C# Errata. 3-9 The Nullable Types The Assignment Operators

More Lambda Calculus and Intro to Type Systems

Types, Type Inference and Unification

Advanced Systems Programming

Non-numeric types, boolean types, arithmetic. operators. Comp Sci 1570 Introduction to C++ Non-numeric types. const. Reserved words.

Lexical Considerations

Index. Cambridge University Press Functional Programming Using F# Michael R. Hansen and Hans Rischel. Index.

OCaml. ML Flow. Complex types: Lists. Complex types: Lists. The PL for the discerning hacker. All elements must have same type.

OGSI.NET UVa Grid Computing Group. OGSI.NET Developer Tutorial

bitwise inclusive OR Logical logical AND && logical OR Ternary ternary? : Assignment assignment = += -= *= /= %= &= ^= = <<= >>= >>>=

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

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

Functional Programming and Parallel Computing

COS 320. Compiling Techniques

SNU Programming Language Theory

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

New Programming Paradigms

News. CSE 130: Programming Languages. Environments & Closures. Functions are first-class values. Recap: Functions as first-class values

Introduction to Functional Programming in Haskell 1 / 56

Tracing Ambiguity in GADT Type Inference

Object Oriented Programming in C#

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

Type Soundness. Type soundness is a theorem of the form If e : τ, then running e never produces an error

Introduction to the D programming language. Marc Fuentes - SED

Programmiersprachen (Programming Languages)

Programming Languages

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

02157 Functional Programming Lecture 1: Introduction and Getting Started

Upcoming Features in C# Mads Torgersen, MSFT

Transcription:

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

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 abstract class Command type Command = Command of (Rover -> unit) { public virtual void Execute(); } let BreakCommand = abstract class MarsRoverCommand : Command Command(fun rover -> rover.accelerate(-1.0)) { 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

Everything Can Interoperate With You

Economics

Fun!

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

F# in More Detail

Objects Meta Programming Functional Data Functional Core Computation Expressions Units of Measure Imperative Mutation & I/O

Quick Tour // comment Comments (* comment *) /// XML doc comment let x = 1

Quick Tour Overloaded Arithmetic x + y Addition x - y Subtraction x * y Multiplication x / y Division x % y Remainder/modulus -x Unary negation Booleans not expr Boolean negation expr && expr Boolean and expr expr Boolean or

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

Demo: Some Basics

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, Anonymous many Function value uses Declare a function value A pair of function values A function type predicate = 'a -> bool send = 'a -> unit threadstart = unit -> unit comparer = 'a -> 'a -> int hasher = 'a -> int equality = 'a -> 'a -> bool

F# - Functional let f x = x+1 let pair x = (x,x) let fst (x,y) = x let data = (Some [1;2;3], Some [4;5;6]) match data with Some(nums1), Some(nums2) -> nums1 @ nums2 None, Some(nums) -> nums Some(nums), None -> nums None, None -> failwith "missing!"

F# - Functional List.map Seq.fold Range Array.filter Lazy.force Expressions Set.union Map LazyList Events Async... List via query [ 0..1000 ] [ for x in 0..10 -> (x, x * x) ] [ for x in 0..10 -> (x, x * x) ] seq { for x in 0..10 -> (x, x * x) } Array via query IEnumerable via query

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

F# - Lists Generated Lists open System.IO let rec allfiles(dir) = [ for file in Directory.GetFiles(dir) do yield file for sub in Directory.GetDirectories(dir) do yield! allfiles(sub) ] allfiles(@"c:\demo")

F# - Sequences On-demand sequences open System.IO let rec allfiles(dir) = seq { for file in Directory.GetFiles(dir) do yield file for sub in Directory.GetDirectories(dir) do yield! allfiles(sub) } allfiles(@"c:\windows") > Seq.take 100 > show Pipelines

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

Objects Class Types type ObjectType(args) = let internalvalue = expr let internalfunction args = expr let mutable internalstate = expr member x.prop1 = expr member x.meth2 args = expr Constructing Objects new FileInfo(@"c:\misc\test.fs")

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 Immutable inputs type HuffmanEncoding(freq:seq<char*int>) =... < 50 lines of beautiful functional code>... Internal tables member x.encode(input: seq<char>) = encode(input) member x.decode(input: seq<char>) = decode(input) 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

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

UNITS OF MEASURE

1985 Mirror on underside of shuttle SDI experiment: The plan Big mountain in Hawaii

1985 SDI experiment: The reality

1985 The reality

NASA Mars Climate Orbiter, 1999

NASA Mars Climate Orbiter, 1999

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)

F# Async/Parallel

async {... } A Building Block for Async/Parallel/Reactive Design Patterns 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

DEMO

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())))))

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

Books about F# Visit www.fsharp.net

Getting F# September 2008: CTP released F# will be a supported language in Visual Studio 2010 Next stop: Visual Studio 2010 Beta 1 Look for it soon!

Questions & Discussion

2007 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. MICROSOFT MAKES NO WARRANTIES, EXPRESS OR IMPLIED, IN THIS SUMMARY.