930 likes | 1.04k Views
Confessions of a used programming language salesperson ( an appeal to purity ) Erik Meijer. Why Functional Programming Matters Reloaded. … 100 … 110 x = x+1 120 … …. Artificial Intelligence via massively parallel machines using logic programming. SASL, Miranda, SKI.
E N D
Confessions of a used programming language salesperson (an appeal to purity)Erik Meijer
Why Functional Programming Matters Reloaded
… 100 … 110 x = x+1 120 … …
Artificial Intelligence via massively parallel machines using logic programming
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
Data Presentation Business logic
Presentation (HaskellScript) Business logic(HSP,XM) Data(HaskellDB)
List ComprehensionsExample 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 ComprehensionsSimplified translation [ 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 ] Syntactic sugar over standard list operations
Monad ComprehensionsExample IO monad generalize lists 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 } Parametrized over type constructor Syntactic sugar over standard monad operations
HaskellDb Query Monad 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 ) } Query monadgeneralizes IO monad intentional representation for expressions
Haskell Server Pages XHTML Literals table :: TABLE table = <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> Translated to universal DOM representation ASP-style embedding
XML 2003: Growing C streams tuples Unions Content classes XML object literals Generalized member access+ SQL comprehensions
Type System Extensions (structural) closures arrays T ::= N | T[] | T{} | T(…,T,…) | T|T | T&T | T! | T? | T+ | T* | struct {…, T m,…} intersection, union streams tuples (rows) XQuery data model
C Query Comprehensions String n = "Wolfram"; struct{String? Subject}* subjects = selectit.Subject fromitin inboxwhereit.From == n Compiler plugins foreach(r in select CustomerID, ContactName from dbo.Customers where City == mycityorder by ContactName) { … } Type inference
SQL = data model + query syntax Select Name, Age From CustomersWhere City = "Seattle" Table of rows
XQuery/XPath = data model + query syntax From $C In CustomersWhere $C/City = "Seattle"Return<CustName={$C/Name}> { $C/Age }</Cust> Set of nodes
Objects = data model + query syntax Foreach C In Customers If C.City="Seattle"R.Add(New With {C.Name, C.Age})End IfNext Collection of objects
T (T Bool) T Filtering 2 2 0 3 X Mod 2 = 0 0 3 6 5 6 5 1 1 4 4
Mapping T (T S) S 2 4 X * 2 0 3 0 6 6 5 12 10 1 2 4 8
Aggregating T (S, (S,T) S) S 2 Sum 0 3 6 5 21 1 4 T ((T,T) T) T
Monads ! A container type ℳ<T> A function type ST A constructor ℳ<T> Unit<T>(T src) ℳ<T> SelectMany<S,T> (ℳ<S> src, Sℳ<T> f) A composer
Monads ! IEnumerable<T> IQueryable<T> ℳ<T> Func<S,T>Expr<Func<S,T>> ST ℳ<T> Unit<T>(T src) ℳ<T> SelectMany<S,T> (ℳ<S> src, Sℳ<T> f) Standard Query Pattern(generics not expressive enough)
LINQ Project == monad comprehensions in C# & VB VB 9 C# 3.0 … StandardQueryOperators DLinq(relational) XLinq(xml) LINQ Framework
Features • 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) Enables Language Extensions via libraries
LINQ 2.0 A better paradigm for programming massive clusters of commodity hardware than Google MapReduce based on LINQ
From W In WordsGroup By WAggregate N = Count()Select W, N Map Group By Repartition Aggregate
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;
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 selectnew { minute = g.Key , count = g.Count() };
Using Visual Basic 9Comprehensions Dim SubmitsThroughWeek =From s In db.Submits GroupBy Minute = s.SubmitTime.Minute + 60*(s.SubmitTime.Hour + 24*s.SubmitTime.DayOfWeek) Into Minute, Count()
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()} );
Repartition “Map” “Reduce”
Do you see a pattern? Functional PL Object-Oriented PL Smalltalk Java Haskell OCaml XML JSON LaTex MS Word Betamax VHS Nouvelle Cuisine Fastfood Semantic WebSearch … …
“For Dummies” Version Next release is even better! Cool! Geeks P(success) = F(10X improvement * Moore’s Law) P(success) = F(perceived crisis/perceived pain of adoption) Coerce users to believe gadgets will make them happy Marketing Users Just want to get job done No patience to learn new stuff
C 10x better Haskell Haskell98 Moore’sLaw Haskell’
Change Function to the rescue What is the user biggest crisis? P(success) = F(perceived crisis perceived pain of adoption) How can we make adoption truly painless?
Change Function to the rescue P(success) = F(perceived crisis perceived pain of adoption) P(success) = F(100% 0) P(success) =