93a2b1eb07cf19a875296860da6d8a84.ppt
- Количество слайдов: 51
Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
What is a XXX Language? XXX = dynamic XXX = imperative XXX = OO
What is a Functional Language A language where functions are first-class citizens
EWD 1073
Easy like Sunday morning
Dan Meyer WTF went wrong?
Doing IO is a side-effect Console. Write. Line(“ha”); != Console. Write. Line(“ha”); var x = Console. Read. Line(); var y = x+x; != var y = Console. Read. Line() + Console. Read. Line()
Doing IO is a side-effect let date = Date. Time. Now() in (date, date) ( Date. Time. Now() , Date. Time. Now() ) Date. Time. Now is not a mathematical function
If I cannot even do IO Fundamentalist Functional Programming is just academic nonsense
Don’t eat The Yellow Snow!
C# Lazy evaluation and side-effects static bool Less. Than. Thirty(int x) { Console. Write("{0}? Less than 30; ", x); return x < 30; } static bool Moret. Han. Twenty(int x) { Console. Write("{0}? More than 20; ", x); return x > 20; } var q 0 = from x in new[]{ 1, 25, 40, 5, 23 } where Less. Than. Thirty(x) select x; var q 1 = from x in q 0 where More. Than. Twenty(x) select x; Order of side effects? foreach (var r in q 1) Console. Write. Line("[{0}]; ", r);
let less. Than. Thirty(x: int)= begin Console. Write. Line ("{0} less than 30? ", x); x < 30; end let more. Than. Twenty(x: int)= begin Console. Write. Line ("{0} more than 20? ", x); x > 20; end let q 0 = seq { for x in [1; 25; 40; 5; 23] do if less. Than. Thirty x then yield x } let q 1 = seq { for x in q 0 do if more. Than. Twenty x then yield x } for r in q 1 do Console. Write. Line("> {0}, ", r) F# Lazy comprehensions
let less. Than. Thirty(x: int)= begin Console. Write. Line ("{0} less than 30? ", x); x < 30; end let more. Than. Twenty(x: int)= begin Console. Write. Line ("{0} more than 20? ", x); x > 20; end let q 0 = [ for x in [1; 25; 40; 5; 23] do if less. Than. Thirty x then yield x ] let q 1 = [ for x in q 0 do if more. Than. Twenty x then yield x ] F# Eager comprehensions for r in q 1 do Console. Write. Line("> {0}, ", r)
Exceptions var xs = new[]{ 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }; IEnumerable
Disposing resources Func
Variable Instantiation var Delayed. Actions = new List
Variable Instantiation var Delayed. Actions = new List
Variable Instantiation var Delayed. Actions = new List
Object creation var o 1 = new object(); //58225482, for instance Console. Write. Line(o 1. Get. Hash. Code()); var o 2 = new object(); // 54267293, for instance Console. Write. Line(o 2. Get. Hash. Code()); Debug. Assert(o 1 != 02);
Concurrency new_cell(X) -> spawn(fun() -> cell(X) end). cell(Value) -> receive {set, New. Value} -> cell(New. Value); {get, Pid} -> Pid!{return, Value}, cell(Value); {dispose} -> {} end. set_cell(Cell, New. Value) -> Cell!{set, New. Value}. get_cell(Cell) -> Cell!{get, self()}, receive {return, Value} -> Value end. dispose_cell(Cell) -> Cell!{dispose}.
Page 165: There is a subtle error in on_exit and keep_alive … Write your code such that race conditions cannot happen. … Fortunately, 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.
*not* idempotent *not* pure GET …/person/age… GET …/clock/seconds… few interesting functions are idempotent
“Dishonest” types Not parametric! static T Identity
HOT Languages To the rescue
less. Than. Thirty x = do { put. Str. Ln $ (show x)++" less than 30? " ; return $ x < 30 } Couldn't match expected type `Bool' more. Than. Twenty x = do against inferred type `IO Bool' { put. Str. Ln $ (show a stmt of a list comprehension: In x)++" more than 20? " ; return $ x > 20 less. Than. Thirty x } In the expression: [x | x <- [1, 25, 50, . . ], less. Than. Thirty x] In the definition of `q 0': q 0 = [ x q 0 = 23 | x <- [1, 25, 50, 5, [x | ]x <- [1, 25, . . ], less. Than. Thirty x ] q 1 = [ x | x <- q 0 , more. Than. Twenty 20 ] Haskell
Forget about effects? Forget it! unsafe. Perform. IO : : IO a -> a unsafe. Cast : : a -> b unsafe. Cast x = unsafe. Perform. IO do{ write. IORef castref x ; read. IORef castref } castref = unsafe. Perform. IO $ do{ new. IORef undefined } $
Cover up effects
Remove all effects (cannot make language more powerful by removing features)
Don’t fight effects Embrace and extend
Mens sana in corpore sano
Mistake Date. Time. Now() Date. Time Type error !!!!!!!!
Values vs Computation How to turn something mutable into something immutable? Time changes, the clock does not
It’s just a collection All problems in computer science can be solved by another level of indirection D. Wheeler … 9: 15 9: 16 9: 17 9: 19 9: 21 9: 22 9: 23 9: 24 9: 25 9: 26 9: 27 9: 28 9: 29 9: 30 … AM AM AM AM
Mathematics is always right
Only one way to be pure, But many ways to be impure Multithreading STM
The “M” word Monads M M IO Exception Animation Collection A computation that produces a value of type A with side-effects described by M
data IO a Sideeffecting computation that yields a value of type a IO put. Char : : Char -> IO () get. Char : : IO Char new. IORef : : a -> IO (IORef a) read. IORef : : IORef a -> IO a write. IORef : : IORef a -> IO () fork. IO : : IO a -> IO Thread. ID
Can Normal Programmers Understand Fundamentalist Functional Programming?
LINQ Monads are the secret sauce behind LINQ Bind IEnumerable Select. Many
Java Throws clause void Foo(string s) throws Exception Explicit Effects
Fundamentalist Functional Programming Abstract from evaluation order Make all effects explicit ==> Compiler can freely rearrange code Runtime can execute in code any order Call-by-name, or call-by-value version?
Call To Action Buy Graham Hutton’s book Nudge and nag MS to produce a fundamentalist functional language
Buy Bryan John & Don’s book Nudge and nag MS to produce a fundamentalist functional language
Thank You