Haskell Type System with Dzmitry Ivashnev

Download Haskell Type System with Dzmitry Ivashnev

Post on 28-Jun-2015

223 views

Category:

Technology

4 download

Embed Size (px)

DESCRIPTION

Slides from http://www.meetup.com/fsharpminsk/events/176930252/?comment_table_id=344084692&comment_table_name=event_comment

TRANSCRIPT

<ul><li> 1. Haskell Type System email: 0xff0x666@gmail.com jabber: xffox@headcounter.org 25 2014 . </li></ul> <p> 2. (functional) (purely functional) (lazy) (static typing) (strong typing) 3. (functional) fact(n) = 1 if n = 0 n fact(n 1) otherwise 4. (functional) module Fact where fact 0 = 1 fact n = n*fact (n -1) 5. (purely functional) const 6. (purely functional) f (x) = ax2 + bx + c x f (x) = 0 = b2 4ac x1,2 = b 2a 7. (purely functional) module Discriminant where solveQuadratic a b c = let d = b*b - 4*a*c in if d &lt; 0 then [] else if d == 0 then [-b/(2*a)] else [-b + (sqrt d)/(2*a), -b - (sqrt d)/(2*a)] 8. (purely functional) d = b * b - 4 * a * c 9. (lists) [] - [1, 2, 42] - [[], [1], [1, 2], [1, 2, 42]] - 10. (lists) [1, 2 .. 5] = [1, 2, 3, 4, 5] [1, 2 .. ] [1 .. ] 11. (lists) 42:[] [42] 8:[42] [8 ,42] 12. (lists) length - O(n) : null length ls == 0 C++ .empty() STL 13. odd 1 True filter odd [1, 2, 3] [1, 3] 14. (lazy) filter odd [1, 2, 3, 4, 5] filter odd [1, 2 .. 5] [1, 3, 5] 15. (lazy) filter odd [1 ..] take 3 (filter odd [1 ..]) [1, 3, 5] 16. (static typing) 17. (strong typing) Int, Float, ... 18. http://www.haskell.org/ http://www.haskell.org/ghc/ 19. A B F F : A B 20. Haskell : a b f f :: a -&gt; b 21. : a, b c f f :: a -&gt; b -&gt; c f :: a -&gt; [a] 22. filter :: (a -&gt; Bool) -&gt; [a] -&gt; [a] map :: (a -&gt; b) -&gt; [a] -&gt; [b] foldr :: (a -&gt; b -&gt; b) -&gt; b -&gt; [a] -&gt; b 23. f :: a -&gt; a 24. f :: a -&gt; a f a = a f = id id 42 42 25. ? f :: a -&gt; [a] 26. fact module Fact where fact 0 = 1 fact n = n*fact (n -1) fact :: (Eq a, Num a) =&gt; a -&gt; a 27. (joke) 28. C++11 auto auto value = 42; auto value = 1 + 2; auto value = f(42); C++14 , auto auto foobar(int value) { return value + 1; } 29. (Algebraic Data Types) , - - (wikipedia) 30. A a data Name a = Constructor a 31. T, A B a b data Name a b = Constructor a b - T = A B 32. T A a data Name a = ContructorF a | ContructorS a - Af = {(x, 1) : x A} As = {(x, 2) : x A} T = Af As 33. T, A, B a b data Name a b = ContructorF a b | ContructorS a Af = {(x, 1) : x A B} As = {(x, 2) : x A} T = Af As 34. (Disjoint union) iI {(x, i) : x Ai } 35. : enum, struct, union 36. data State a b = First a | Second b | Third a b handle (First s) v = Second (f s) handle (Second s) v = First s handle (Third s t) v = Third s (f t v) 37. enum data Bool = True | False data Color = Red | Green | Blue enum Bool { TRUE , FALSE }; 38. struct data State a b = St a b struct State { int a; int b; }; 39. union data State a b = First a b | Second a union State { struct { int a; int b; } first; struct { int a; } second; }; 40. module Tree where data Tree a = Leaf | Node (Tree a) a (Tree a) sumTree Leaf = 0 sumTree (Node left v right) = sumTree left + v + sumTree right import Tree main = putStrLn $ show (sumTree (Node (Node (Leaf) 2 (Node (Leaf) 3 (Leaf))) 1 ( Leaf))) 6 41. List a = [] | (:) a (List a) 42. , 43. Typeclasses - . (a b) c = a (b c) a e = a : . ? 44. Typeclasses module Monoid where class Monoid a where binop :: a -&gt; a -&gt; a identity :: a twice :: Monoid a =&gt; a -&gt; a twice a = binop a a 45. Typeclasses In fact, trolls traditionally count like this: one, two, three . . . many, and people assume this means they can have no grasp of higher numbers. Men at Arms, Terry Pratchett 46. Typeclasses module TrollNum(TrollNum (..) , plus , binop , identity) where import Monoid data TrollNum = Zero | One | Two | Three | Many deriving Show plus :: TrollNum -&gt; TrollNum -&gt; TrollNum plus a b = fromNum (toNum a + toNum b) instance Monoid TrollNum where binop = plus identity = Zero toNum Zero = 0 toNum One = 1 toNum Two = 2 toNum Three = 3 toNum Many = 4 fromNum 0 = Zero fromNum 1 = One fromNum 2 = Two fromNum 3 = Three fromNum _ = Many 47. Typeclasses import Monoid import TrollNum main = putStrLn $ show $ twice Two Many 48. module Ops where (-|-) a b = (a + b)/2 49. Pointfree style f (x), g(x) z(x) z(x) = f (g(x)) z = f . g (.) :: (b -&gt; c) -&gt; (a -&gt; b) -&gt; a -&gt; c 50. Pointfree style , 1 addOne v = v + 1 addOne v = (+) v 1 addOne v = (+) 1 v addOne = (+) 1 ((+) 1) :: Num a =&gt; a -&gt; a 51. Pointfree style module LongestWord where longestWord = (foldr max 0) . (map length) . words map-reduce 52. . - , (wikipedia). 53. Info class Monad m where (&gt;&gt;=) :: m a -&gt; (a -&gt; m b) -&gt; m b (&gt;&gt;) :: m a -&gt; m b -&gt; m b return :: a -&gt; m a fail :: String -&gt; m a 54. m (bind): (=) :: m a -&gt; (a -&gt; m b) -&gt; m b (inject): return :: a -&gt; m a 55. Monad 56. IO IO ? IO monad 57. module XFact where xfact n = xfact 1 n where xfact v 0 = v xfact v n = xfact (v*n) (n-1) 58. module ModInt where newtype ModInt = ModInt { fromModInt :: Int } deriving (Show) instance Num ModInt where (+) (ModInt a) (ModInt b) = ModInt ((a + b) mod 23) (*) (ModInt a) (ModInt b) = ModInt ((a * b) mod 23) abs (ModInt a) = ModInt (abs a) signum (ModInt a) = ModInt (signum a) fromInteger v = ModInt (fromInteger v mod 23) import ModInt pow a 0 = 1 pow a b = a * pow a (b-1) main = putStrLn $ show $ fromModInt $ pow 2 5 9 59. DSL import Fuzz import DumbFuzzer authRequest = "AUTH" wait authResponses getRequest = "GET" wait getResponses authResponses = ("AUTHED" |-&gt; getRequest) // (other (report 0)) getResponses = ("200OK" |-&gt; report 1) // ("418Imateapot" |-&gt; report 42) // (other (report 0)) main = do r ), (//) , other , fuzz , Fuzzer (..)) where data Response r = Response (Msg , Request r) | Other (Request r) data Request r = Send Msg [Response r] | Report r report :: r -&gt; Request r report r = Report r wait :: Msg -&gt; [Response r] -&gt; Request r wait message reactions = Send message reactions other :: Request r -&gt; [Response r] other response = [Other response] (|-&gt;) :: Msg -&gt; Request r -&gt; [Response r] (|-&gt;) message action = [Response (message , action)] (//) :: [Response r] -&gt; [Response r] -&gt; [Response r] (//) a b = a ++ b fuzz :: Fuzzer f =&gt; f -&gt; Request r -&gt; IO r fuzz f (Send msg responses) = do r Msg -&gt; IO Msg 61. Fuzz II next f _ [Other request] = fuzz f request next f resp (( Other request):rs) = next f resp (rs ++ [Other request ]) next f resp (( Response (msg , request)):rs) | resp == msg = fuzz f request | otherwise = next f resp rs type Msg = String 62. Fuzzer module DumbFuzzer where import Fuzz data DumbFuzzer = DumbFuzzer instance Fuzzer DumbFuzzer where send _ m = do putStrLn m getLine 63. Real World Haskell by Bryan OSullivan, John Goerzen, and Don Stewart The Haskell Road to Logic, Math and Programming by Kees Doets and Jan van Eijck Parallel and Concurrent Programming in Haskell by Simon Marlow Haskell Beats C Using Generalized Stream Fusion by Georey Mainland, Roman Leshchinskiy, and Simon Peyton Jones (paper) </p>