Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.
-
Upload
sharleen-hensley -
Category
Documents
-
view
215 -
download
0
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.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/1.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/2.jpg)
Why FunctionalProgramming
MattersReloaded
![Page 3: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/3.jpg)
…100 …110 x = x+1120 ……
![Page 4: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/4.jpg)
Artificial Intelligence via massively parallel machines using logic programming
![Page 5: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/5.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/6.jpg)
![Page 7: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/7.jpg)
![Page 8: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/8.jpg)
![Page 9: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/9.jpg)
![Page 10: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/10.jpg)
Presentation
Business logic
Data
![Page 11: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/11.jpg)
Presentation (HaskellScript)
Business logic(HSP,XM)
Data(HaskellDB)
![Page 12: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/12.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/13.jpg)
[ 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.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/14.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/15.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/16.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/17.jpg)
http://radar.oreilly.com/erlanghaskellruby-thumb.png
![Page 18: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/18.jpg)
![Page 19: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/19.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/20.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/21.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/22.jpg)
![Page 23: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/23.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/24.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/25.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/26.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/27.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/28.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/29.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/30.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/31.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/32.jpg)
• 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.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/33.jpg)
![Page 34: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/34.jpg)
![Page 35: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/35.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/36.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/37.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/38.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/39.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/40.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/41.jpg)
“Reduce”
“Map”Repartition
![Page 42: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/42.jpg)
![Page 43: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/43.jpg)
![Page 44: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/44.jpg)
Division By 0 Is The Goal, Not An Error
![Page 45: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/45.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/46.jpg)
![Page 47: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/47.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/48.jpg)
C
Haskell
Haskell98
Haskell’
10x better
Moore’sLaw
![Page 49: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/49.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/50.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/51.jpg)
![Page 52: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/52.jpg)
![Page 53: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/53.jpg)
![Page 54: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/54.jpg)
![Page 55: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/55.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/56.jpg)
Shared NothingMessage Passing
Really?
![Page 57: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/57.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/58.jpg)
![Page 59: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/59.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/60.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/61.jpg)
![Page 62: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/62.jpg)
![Page 63: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/63.jpg)
![Page 64: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/64.jpg)
![Page 65: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/65.jpg)
ThoseSneakySide Effects
![Page 66: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/66.jpg)
![Page 67: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/67.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/68.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/69.jpg)
![Page 70: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/70.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/71.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/72.jpg)
Mens sana in corpore sano
![Page 73: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/73.jpg)
Haskell
Purity is the key to success
![Page 74: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/74.jpg)
Mistake
DateTime.Now.Ticks long
Type error!!!!!!!!!!!!!!!
![Page 75: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/75.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/76.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/77.jpg)
![Page 78: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/78.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/79.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/80.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/81.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/82.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/83.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/84.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/85.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/86.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/87.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/88.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/89.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/90.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/91.jpg)
![Page 92: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/92.jpg)
![Page 93: Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.](https://reader031.fdocuments.in/reader031/viewer/2022012922/56649f555503460f94c7962b/html5/thumbnails/93.jpg)
C
Haskell
Haskell98
Haskell’
10x better
Moore’sLaw
Is thePerceived Real
Crisis >>>>
Perceived pain of adoption?