# haskell type system with dzmitry ivashnev

Post on 28-Jun-2015

223 views

Category:

## Technology

Embed Size (px)

DESCRIPTION

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

TRANSCRIPT

• 1. Haskell Type System email: 0xff0x666@gmail.com jabber: xffox@headcounter.org 25 2014 .

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 < 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 -> b 21. : a, b c f f :: a -> b -> c f :: a -> [a] 22. filter :: (a -> Bool) -> [a] -> [a] map :: (a -> b) -> [a] -> [b] foldr :: (a -> b -> b) -> b -> [a] -> b 23. f :: a -> a 24. f :: a -> a f a = a f = id id 42 42 25. ? f :: a -> [a] 26. fact module Fact where fact 0 = 1 fact n = n*fact (n -1) fact :: (Eq a, Num a) => a -> 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 -> a -> a identity :: a twice :: Monoid a => a -> 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 -> TrollNum -> 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 -> c) -> (a -> b) -> a -> c 50. Pointfree style , 1 addOne v = v + 1 addOne v = (+) v 1 addOne v = (+) 1 v addOne = (+) 1 ((+) 1) :: Num a => a -> a 51. Pointfree style module LongestWord where longestWord = (foldr max 0) . (map length) . words map-reduce 52. . - , (wikipedia). 53. Info class Monad m where (>>=) :: m a -> (a -> m b) -> m b (>>) :: m a -> m b -> m b return :: a -> m a fail :: String -> m a 54. m (bind): (=) :: m a -> (a -> m b) -> m b (inject): return :: a -> 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" |-> getRequest) // (other (report 0)) getResponses = ("200OK" |-> report 1) // ("418Imateapot" |-> 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 -> Request r report r = Report r wait :: Msg -> [Response r] -> Request r wait message reactions = Send message reactions other :: Request r -> [Response r] other response = [Other response] (|->) :: Msg -> Request r -> [Response r] (|->) message action = [Response (message , action)] (//) :: [Response r] -> [Response r] -> [Response r] (//) a b = a ++ b fuzz :: Fuzzer f => f -> Request r -> IO r fuzz f (Send msg responses) = do r Msg -> 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)

Recommended