Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

93
Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer

Transcript of Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

Page 1: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

Confessions of a

used programming language

salesperson(an appeal to purity)

Erik Meijer

Page 2: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

Why FunctionalProgramming

MattersReloaded

Page 3: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

…100 …110 x = x+1120 ……

Page 4: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

Artificial Intelligence via massively parallel machines using logic programming

Page 5: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

SASL, Miranda, SKI

[f.x.(f (f x))]=(S (S (K S) (S (K K) I)) (S (S (K S) (S (K K) I)) (K I)))

Only need 3 instructions

and massively parallel reduction

machines

Page 6: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.
Page 7: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.
Page 8: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.
Page 9: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.
Page 10: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

Presentation

Business logic

Data

Page 11: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

Presentation (HaskellScript)

Business logic(HSP,XM)

Data(HaskellDB)

Page 12: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

factors n = [ x Select | x <- [1..n] From , n `mod` x == 0 Where ]

isPrime n = factors n = [1,n]

primes n = [ p | p <- [2..n], isPrime p ]

List ComprehensionsExample

Page 13: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

[ e | True ] = [ e ] [ e | q ] = [ e | q, True ] [ e | b, Q ] = filter b [ e | Q ][ e | x <- l, Q ] = concatMap (\x -> [e | Q ]) l [ e | let decls, Q ] = let decls in [ e | Q ]

List ComprehensionsSimplified translation

Syntactic sugar over standard list

operations

Page 14: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

words :: IO [String]words = do{ putStr “enter a value …” ; x <- getLine ; return (words x) }

class Monad m where { (>>=) :: m a -> (a -> m b) -> m b ; return :: a -> m a }

Monad ComprehensionsExample

Syntactic sugar over standard

monad operations

Parametrized over type constructor

IO monad generalize lists

Page 15: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

SELECT X.FirstName, X.LastNameFROM Authors AS XWHERE X.City = 'OakLand'

oaklands = do{ x <- table authors ; restrict (x!city .==. constant "Oakland") ; project ( au_fname = x!au_fname , au_lname = x!au_lname ) }

HaskellDb Query Monad

Query monadgeneralizes IO

monad

intentional representation for expressions

Page 16: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

table :: TABLEtable = <TABLE border="1"> <% mkRows cells %> </TABLE>

cells :: [[(Int,Int)]]cells = [[ (x,y) | x <- [1..16] ] | y <- [1..16] ]

mkRows :: [[(Int,Int)]] -> [TR]mkRows = map $ \cs -> <TR><% mkColums cs %></TR>

mkColumns :: [(Int,Int)] -> [TD]mkColums = map $ \c -> <TD bgcolor=(color c)><% c %></TD>

Haskell Server PagesXHTML Literals

ASP-style embedding

Translated to universal DOM representation

Page 17: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

http://radar.oreilly.com/erlanghaskellruby-thumb.png

Page 18: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.
Page 19: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

streams

tuples

Unions

Content classes

XML object literals

Generalized member access+ SQL comprehensions

XML 2003: Growing C

Page 20: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

Type System Extensions

T ::= N | T[] | T{} | T(…,T,…) | T|T | T&T | T! | T? | T+ | T* | struct {…, T m,…}

intersection, union

streams

tuples (rows)

(structural)

arrays closures

XQuery data model

Page 21: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

String n = "Wolfram";struct{String? Subject}* subjects = select it.Subject from it in inbox where it.From == n Compiler

plugins

C Query Comprehensions

foreach(r in select CustomerID, ContactName from dbo.Customers where City == mycity order by ContactName) { …} Type inference

Page 22: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.
Page 23: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

SQL = data model + query syntax

Select Name, Age From CustomersWhere City = "Seattle"

Table of rows

Page 24: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

XQuery/XPath = data model + query syntax

From $C In CustomersWhere $C/City = "Seattle"Return <Cust Name={$C/Name}> { $C/Age } </Cust>

Set of nodes

Page 25: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

Foreach C In Customers If C.City = "Seattle" R.Add(New With {C.Name, C.Age}) End IfNext Collection of

objects

Objects = data model + query syntax

Page 26: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

0

1

3

4

56

0

1

3

4

56

2 2

Filtering

X Mod 2 = 0

T (T Bool) T

Page 27: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

0

1

3

4

56

0

2

6

8

1012

2 4

X * 2

Mapping

T (T S) S

Page 28: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

0

1

3

4

56 21

2

Sum

Aggregating

T (S, (S,T) S) S

T ((T,T) T) T

Page 29: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

Monads !

ℳ<T> Unit<T>(T src)ℳ<T> SelectMany<S,T> (ℳ<S> src, Sℳ<T> f)

ℳ<T>

ST

A container type

A function type

A constructor

A composer

Page 30: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

Monads !

ℳ<T> Unit<T>(T src)ℳ<T> SelectMany<S,T> (ℳ<S> src, Sℳ<T> f)

ℳ<T>

ST

IEnumerable<T> IQueryable<T>

Func<S,T>Expr<Func<S,T>>

Standard Query Pattern(generics not expressive enough)

Page 31: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

LINQ Project== monad comprehensions in C# & VB

VB 9 C# 3.0 …

StandardQuery

Operators

DLinq(relational)

XLinq(xml)

LINQ Framework

Page 32: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

• Local Type Inference• Object & Collection Initializers• Anonymous Types• Lambda Expressions• Query Comprehensions• Extension Methods• Expression Trees• Simplified Properties• Partial Methods• Deep XML Support (VB)• Nullable Types (VB)

Features

EnablesLanguageExtensionsvia libraries

Page 33: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.
Page 34: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.
Page 35: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

A better paradigm for programming massive clusters of commodity hardware than Google MapReduce based on LINQ

LINQ 2.0

Page 36: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

Map Group By Repartition Aggregate

From W In WordsGroup By WAggregate N = Count()Select W, N

Page 37: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

SawzallExample 3

submitsthroughweek: table sum[minute: int] of count: int;

log: P4ChangelistStats = input;

t: time = log.time; # microseconds

minute: int = minuteof(t) +60*(hourof(t) +24*(dayofweek(t)-1));

emit submitsthroughweek[minute] <- 1;

Page 38: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

Using C# 3.0Comprehensions

var SubmitsThroughWeek = from s in db.Submits group s by s.SubmitTime.Minute + 60*(s.SubmitTime.Hour + 24*s.SubmitTime.DayOfWeek) into g select new { minute = g.Key , count = g.Count() };

Page 39: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

Using Visual Basic 9Comprehensions

Dim SubmitsThroughWeek = From s In db.Submits Group By Minute = s.SubmitTime.Minute + 60*(s.SubmitTime.Hour + 24*s.SubmitTime.DayOfWeek) Into Minute, Count()

Page 40: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

UsingStandard Sequence Operators

var SubmitsThroughWeek = db.Submits .GroupBy(s=>s.SubmitTime.Minute + 60*(s.SubmitTime.Hour + 24*s.SubmitTime.DayOfWeek)) .Select(g=>new { minute=g.Key , count=g.Count()} );

Page 41: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

“Reduce”

“Map”Repartition

Page 42: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.
Page 43: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.
Page 44: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

Division By 0 Is The Goal, Not An Error

Page 45: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

Functional PL Object-Oriented PLSmalltalk JavaHaskell OCamlXML JSONLaTex MS WordBetamax VHSNouvelle Cuisine FastfoodSemantic Web Search… …

Do you see a pattern?

Page 46: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.
Page 47: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

P(success) = F(10X improvement * Moore’s Law)

P(success) = F(perceived crisis/perceived pain of adoption)

Geeks

Users

Marketing

Cool!Next release is even better!

Just want to get job done

No patience to learn new stuff

Coerce users to believe gadgets will make them

happy

“For Dummies” Version

Page 48: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

C

Haskell

Haskell98

Haskell’

10x better

Moore’sLaw

Page 49: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

P(success) = F(perceived crisis perceived pain of adoption)

What is the user biggest crisis?

How can we make adoption truly painless?

Change Function to the rescue

Page 50: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

P(success) = F(perceived crisis perceived pain of adoption)

Change Function to the rescue

P(success) = F(100% 0)

P(success) =

Page 51: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.
Page 52: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.
Page 53: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.
Page 54: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.
Page 55: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

Silver Bullet

Are “functional” languages such as Erlang and F# the Silver Bullet for the many-core problem?

Page 56: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

Shared NothingMessage Passing

Really?

Page 57: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

Shared NothingMessage Passing

Really?

Page 165:

There is a subtle error in on_exit and keep_alive … Write your code such that race conditions cannot happen.…Fortunatley, the OTP libraries have code for building servers, supervisor trees, and so on.These libraries have been well tested and should not suffer from any race conditions.

Page 58: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.
Page 59: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

Unsafe and Useless

safeunsafe

useful

uselessHaskell

C++

VBC#

F#Erlang

Nirvana

Page 60: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

What is a Functional Language

A language where functions are first-class citizens

Page 61: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.
Page 62: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.
Page 63: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.
Page 64: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.
Page 65: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

ThoseSneakySide Effects

Page 66: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.
Page 67: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

unsafeCast :: a -> bunsafeCast x = unsafePerformIO $ writeIORef castref x >> readIORef castref

castref = unsafePerformIO $ newIORef undefined

Page 68: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

new_cell(X) -> spawn(fun() -> cell(X) end).cell(Value) -> receive {set, NewValue} -> cell(NewValue); {get, Pid} -> Pid!{return, Value}, cell(Value); {dispose} -> {} end.set_cell(Cell, NewValue) -> Cell!{set, NewValue}.

get_cell(Cell) -> Cell!{get, self()}, receive {return, Value} -> Value end.dispose_cell(Cell) -> Cell!{dispose}.

4711

c.set_cell(v)

v =c.get_cell()

Page 69: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.
Page 70: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

What is a function

-calculusE ::= EE | …

let x = E1 in E0[x] E0[x:=E1]

(x.E0[x])E1 E0[x:=E1]

Page 71: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

What is a function

let t = DateTime.Now.Ticksin (x,x)( DateTime.Now.Ticks, DateTime.Now.Ticks)

Racecondition

Page 72: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

Mens sana in corpore sano

Page 73: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

Haskell

Purity is the key to success

Page 74: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

Mistake

DateTime.Now.Ticks long

Type error!!!!!!!!!!!!!!!

Page 75: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

What is a function

let t = ().DateTime.Now.Ticksin (x,x)( ().DateTime.Now.Ticks, ().DateTime.Now.Ticks)

Valuerestriction

Page 76: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

Values vs Computation

How to turn something mutable into something immutable?

Time changes, the clock does not

Page 77: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.
Page 78: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

Generalization

Monads

M<A>

A computation that produces a value of type A with side-effects described by M

M IO Exception Animation Collection

Page 79: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

Algebra/API

Return A M<A>

Bind M<A>(AM<B>)M<B>

Join M<M<A>>M<A>

UnsafePerformIO M<A>A

Page 80: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

LINQ

Monads are the secret sauce behind LINQ

IEnumerable<S> SelectMany<T,S>( IEnumerable<T> src, Func<T, IEnumerable<S>> selector)

Bind

Page 81: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

IO

data IO a

putChar :: Char -> IO ()getChar :: IO Char

newIORef :: a -> IO (IORef a)readIORef :: IORef a -> IO awriteIORef :: IORef a -> a -> IO ()

forkIO :: IO a -> IO ThreadID

Side-effecting

computation that yields a value of type

a

Page 82: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

IO

main :: IO()main = do{ x <- newIORef “hello” ; c <- getChar ; s <- readIORef x ; writeIORef x (c:s) ; ... }

Does nothing

Page 83: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

STMdata STM a

atomic :: STM a -> IO aretry :: STM aorElse :: STM a -> STM a -> STM a

newTVar :: a -> STM (TVar a)readTVar :: TVar a -> STM awriteTVar :: TVar a -> a -> STM ()

Page 84: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

STMputR :: TVar Int -> Int -> STM ()putR r i = do { v <- readTVar r ; writeTVar r (v+i) }

main :: IO()main = do{ … ; atomic $ putR 4711 ; … }

Page 85: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

STM

getR :: TVar Int -> Int -> STM ()getR r i = do { v <- readTVar r ; if (v < i) then retry else writeTVar r (v-i) }

Page 86: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

STMnonBlockGetR :: TVar Int -> Int -> STM BoolnonBlockGetR r i = do { getR r i ; return True }‘orElse‘ do { return False }

retry

Page 87: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

STMnonBlockGetR :: TVar Int -> Int -> STM BoolnonBlockGetR r i = do { getR r i ; return True }‘orElse‘ do { return False }

retry

Page 88: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

STMAn MVar is a mutable location either empty, or full with a value.

takeMVar function leaves a full MVar empty, blocks on an empty MVar.

putMVar on an empty MVar leaves it full, and blocks on a full MVar.

type MVar a = TVar (Maybe a)

newEmptyMVar :: STM (MVar a)newEmptyMVar = newTVar Nothing

Page 89: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

STMRead the contents of the TVar retry until not Nothing:

takeMVar :: MVar a -> STM atakeMVar mv = do { v <- readTVar mv ; case v of Nothing -> retry Just val -> do { writeTVar mv Nothing ; return val } }

Page 90: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

STM

Retry until Nothing,Update the underlying TVar

putMVar :: MVar a -> a -> STM ()putMVar mv val = do { v <- readTVar mv ; case v of Nothing -> do{ writeTVar mv (Just val) } Just val -> retry }

Page 91: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.
Page 92: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.
Page 93: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.

C

Haskell

Haskell98

Haskell’

10x better

Moore’sLaw

Is thePerceived Real

Crisis >>>>

Perceived pain of adoption?