Download presentation
Presentation is loading. Please wait.
Published bySharleen Hensley Modified over 9 years ago
1
Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer
2
Why Functional Programming Matters Reloaded
3
… 100 … 110 x = x+1 120 … …
4
Artificial Intelligence via massively parallel machines using logic programming
5
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
10
Presentation Business logic Data
11
Presentation (HaskellScript) Business logic (HSP, XM) Data (Haskell DB)
12
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 Comprehensions Example
13
[ e | True ] = [ e ] [ e | q ] = [ e | q, True ] [ e | b, Q ] = filter b [ e | Q ] [ e | x [e | Q ]) l [ e | let decls, Q ] = let decls in [ e | Q ] List Comprehensions Simplified translation Syntactic sugar over standard list operations
14
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 Comprehensions Example Syntactic sugar over standard monad operations Parametrized over type constructor IO monad generalize lists
15
SELECT X.FirstName, X.LastName FROM Authors AS X WHERE 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 monad generalizes IO monad intentional representation for expressions
16
table :: TABLE table = cells :: [[(Int,Int)]] cells = [[ (x,y) | x <- [1..16] ] | y <- [1..16] ] mkRows :: [[(Int,Int)]] -> [TR] mkRows = map $ \cs -> mkColumns :: [(Int,Int)] -> [TD] mkColums = map $ \c -> Haskell Server Pages XHTML Literals ASP-style embedding Translated to universal DOM representation
17
http://radar.oreilly.com/erlanghaskellruby-thumb.png
19
streams tuples Unions Content classes XML object literals Generalized member access + SQL comprehensions XML 2003: Growing C
20
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
21
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
23
SQL = data model + query syntax Select Name, Age From Customers Where City = "Seattle" Table of rows
24
XQuery/XPath = data model + query syntax From $C In Customers Where $C/City = "Seattle" Return { $C/Age } Set of nodes
25
Foreach C In Customers If C.City = "Seattle" R.Add(New With {C.Name, C.Age}) End If Next Collection of objects Objects = data model + query syntax
26
0 1 3 4 56 0 1 3 4 56 2 2 Filtering X Mod 2 = 0 T (T Bool) T
27
0 1 3 4 56 0 2 6 8 1012 2 4 X * 2 Mapping T (T S) S
28
0 1 3 4 5621 2 Sum Aggregating T (S, (S,T) S) S T ((T,T) T) T
29
Monads ! ℳ Unit (T src) ℳ SelectMany ( ℳ src, S ℳ f) ℳ STST A container type A function type A constructor A composer
30
Monads ! ℳ Unit (T src) ℳ SelectMany ( ℳ src, S ℳ f) ℳ STST IEnumerable IQueryable Func Expr > Standard Query Pattern (generics not expressive enough)
31
LINQ Project == monad comprehensions in C# & VB VB 9C# 3.0… Standard Query Operators DLinq (relational) XLinq (xml) LINQ Framework
32
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 Enables Language Extensions via libraries
35
A better paradigm for programming massive clusters of commodity hardware than Google MapReduce based on LINQ LINQ 2.0
36
MapGroup ByRepartitionAggregate From W In Words Group By W Aggregate N = Count() Select W, N
37
Sawzall Example 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;
38
Using C# 3.0 Comprehensions 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() };
39
Using Visual Basic 9 Comprehensions Dim SubmitsThroughWeek = From s In db.Submits Group By Minute = s.SubmitTime.Minute + 60*(s.SubmitTime.Hour + 24*s.SubmitTime.DayOfWeek) Into Minute, Count()
40
Using Standard 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()} );
41
“Reduce” “Map” Repartition
44
Division By 0 Is The Goal, Not An Error
45
Functional PLObject-Oriented PL SmalltalkJava HaskellOCaml XMLJSON LaTexMS Word BetamaxVHS Nouvelle CuisineFastfood Semantic WebSearch… Do you see a pattern?
47
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
48
C Haskell Haskell98 Haskell’ 10x better Moore’s Law
49
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
50
P(success) = F(perceived crisis perceived pain of adoption) Change Function to the rescue P(success) = F(100% 0) P(success) =
55
Erlang/F# Is A Hoax
56
Silver Bullet Are “functional” languages such as Erlang and F# the Silver Bullet for the many-core problem?
57
Shared Nothing Message Passing Really ?
58
Shared Nothing Message 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.
60
Unsafe and Useless safeunsafe useful useless Haskell C++ VB C# F# Erlang Nirvana
61
What is a Functional Language A language where functions are first-class citizens
66
Those Sneaky Side Effects
68
unsafeCast :: a -> b unsafeCast x = unsafePerformIO $ writeIORef castref x >> readIORef castref castref = unsafePerformIO $ newIORef undefined
69
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()
71
What is a function -calculus E ::= EE | … let x = E 1 in E 0 [x] E 0 [x:=E 1 ] (x.E 0 [x])E 1 E 0 [x:=E 1 ]
72
What is a function let t = DateTime.Now.Ticks in (x,x) ( DateTime.Now.Ticks, DateTime.Now.Ticks ) Race condition
73
Mens sana in corpore sano
74
Haskell Purity is the key to success
75
Mistake DateTime.Now.Ticks long Type error !!!!!!!!!!!!!!!
76
What is a function let t = ().DateTime.Now.Ticks in (x,x) ( ().DateTime.Now.Ticks, ().DateTime.Now.Ticks ) Value restriction
77
Values vs Computation How to turn something mutable into something immutable? Time changes, the clock does not
79
Generalization Monads M A computation that produces a value of type A with side- effects described by M M IO Exception Animation Collection
80
Algebra/API Return A M Bind M (A M ) M Join M > M UnsafePerformIO M A
81
LINQ Monads are the secret sauce behind LINQ IEnumerable SelectMany ( IEnumerable src, Func > selector ) Bind
82
IO data IO a putChar :: Char -> IO () getChar :: IO Char newIORef :: a -> IO (IORef a) readIORef :: IORef a -> IO a writeIORef :: IORef a -> a -> IO () forkIO :: IO a -> IO ThreadID Side- effecting computation that yields a value of type a
83
IO main :: IO() main = do{ x <- newIORef “hello” ; c <- getChar ; s <- readIORef x ; writeIORef x (c:s) ;... } Does nothing
84
STM data STM a atomic :: STM a -> IO a retry :: STM a orElse :: STM a -> STM a -> STM a newTVar :: a -> STM (TVar a) readTVar :: TVar a -> STM a writeTVar :: TVar a -> a -> STM ()
85
STM putR :: TVar Int -> Int -> STM () putR r i = do { v <- readTVar r ; writeTVar r (v+i) } main :: IO() main = do{ … ; atomic $ putR 4711 ; … }
86
STM getR :: TVar Int -> Int -> STM () getR r i = do { v <- readTVar r ; if (v < i) then retry else writeTVar r (v-i) }
87
STM nonBlockGetR :: TVar Int -> Int -> STM Bool nonBlockGetR r i = do { getR r i ; return True } ‘orElse‘ do { return False } retry
88
STM nonBlockGetR :: TVar Int -> Int -> STM Bool nonBlockGetR r i = do { getR r i ; return True } ‘orElse‘ do { return False } retry
89
STM An 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
90
STM Read the contents of the TVar retry until not Nothing: takeMVar :: MVar a -> STM a takeMVar mv = do { v <- readTVar mv ; case v of Nothing -> retry Just val -> do { writeTVar mv Nothing ; return val }
91
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 }
94
C Haskell Haskell98 Haskell’ 10x better Moore’s Law Is the Perceived Real Crisis >>>> Perceived pain of adoption?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.