pragmatism puritanism and functional programming
play

Pragmatism, Puritanism and Functional Programming Ben Moseley - PowerPoint PPT Presentation

Pragmatism, Puritanism and Functional Programming Ben Moseley ben@moseley.name TechMesh - 4 December Pragmatism, Puritanism and Functional Programming Ben Moseley ben@moseley.name TechMesh - 4 December 2012 Pragmatism, Puritanism and


  1. FilePaths 1 - Strings vs Paths String Strings vs Paths No Distinction data Value =...| TagString String Runtime | TagPath String data Path = Path String Compile time Path “/usr/bm” :: Path Path “bar.csv” :: Path appendPath :: (Path, Path) -> Path

  2. FilePaths 1 - Strings vs Paths String Strings vs Paths No Distinction data Value =...| TagString String Runtime | TagPath String data Path = Path String Compile time Path “/usr/bm” :: Path Path “bar.csv” :: Path appendPath :: (Path, Path) -> Path appendPath (“line1\n line2\r\n...”, Path “bar.csv”) ...static error

  3. FilePaths 1 - Strings vs Paths String Strings vs Paths No Distinction data Value =...| TagString String Runtime | TagPath String data Path = Path String Compile time Path “/usr/bm” :: Path Path “bar.csv” :: Path appendPath :: (Path, Path) -> Path appendPath ( Path “/usr/bm”, Path “bar.csv”)

  4. FilePaths 1 - Strings vs Paths String Strings vs Paths No Distinction data Value =...| TagString String Runtime | TagPath String data Path = Path String Compile time Path “/usr/bm” :: Path Path “bar.csv” :: Path appendPath :: (Path, Path) -> Path appendPath ( Path “foo.txt”, Path “bar.csv”) ...no error

  5. FilePaths 2 - File vs Dir Paths String Strings vs Paths No Distinction data Value =...| TagString String Runtime | TagPath String data Path = Path String Compile time File Paths vs data Path = FilePath String Runtime Dir Paths | DirPath String appendPath :: (Path, Path) -> Path appendPath ( Path “foo.txt”, Path “bar.csv”)

  6. FilePaths 2 - File vs Dir Paths String Strings vs Paths No Distinction data Value =...| TagString String Runtime | TagPath String data Path = Path String Compile time File Paths vs data Path = FilePath String Runtime Dir Paths | DirPath String appendPath :: (Path, Path) -> Path appendPath (FilePath “foo.txt”, FilePath “bar.csv”) ...dynamic error

  7. FilePaths 2 - File vs Dir Paths String Strings vs Paths No Distinction data Value =...| TagString String Runtime | TagPath String data Path = Path String Compile time File Paths vs data Path = FilePath String Runtime Dir Paths | DirPath String data Path = Path String No Distinction Path “/usr” :: Path Path “fred.csv” :: Path appendPath :: (Path , Path ) -> Path

  8. FilePaths 2 - File vs Dir Paths String Strings vs Paths No Distinction data Value =...| TagString String Runtime | TagPath String data Path = Path String Compile time File Paths vs data Path = FilePath String Runtime Dir Paths | DirPath String data Path fd = Path String Compile time Path “/usr” :: Path Path “fred.csv” :: Path appendPath :: (Path , Path ) -> Path

  9. FilePaths 2 - File vs Dir Paths String Strings vs Paths No Distinction data Value =...| TagString String Runtime | TagPath String data Path = Path String Compile time File Paths vs data Path = FilePath String Runtime Dir Paths | DirPath String data Path fd = Path String Compile time Path “/usr” :: Path Dir Path “fred.csv” :: Path File appendPath :: (Path , Path ) -> Path

  10. FilePaths 2 - File vs Dir Paths String Strings vs Paths No Distinction data Value =...| TagString String Runtime | TagPath String data Path = Path String Compile time File Paths vs data Path = FilePath String Runtime Dir Paths | DirPath String data Path fd = Path String Compile time Path “/usr” :: Path Dir Path “fred.csv” :: Path File appendPath :: (Path Dir, Path fd) -> Path fd

  11. Static T ypes - Sliding Scale String Strings vs Paths No Distinction data Value =...| TagString String Runtime | TagPath String data Path = Path String Compile time File Paths vs data Path = Path String No Distinction Dir Paths data Path = FilePath String Runtime | DirPath String data Path fd = Path String Compile time

  12. Static T ype Systems - Benefits ✤ Make code simpler to write ✤ detect errors earlier ✤ the first thing you write - a design language ✤ Refactoring, IDEs, Performance

  13. Static T ype Systems - Benefits ✤ Make code simpler to read ✤ Help locate bugs (in type-correct code !) ✤ Help understand normal code ✤ Help understand very abstract code ✤ Document side-effects

  14. T ypes: Assumptions & Guarantees mychunk :: [Int] -> [Int] mychunk = ...

  15. T ypes: Assumptions & Guarantees mychunk :: [Int] -> [Int] mychunk = ...

  16. T ypes: Assumptions & Guarantees mychunk :: [Int] -> [Int] mychunk = ...

  17. T ypes: Assumptions & Guarantees mychunk :: [a] -> [a] mychunk = ...

  18. T ypes: Assumptions & Guarantees mychunk :: [a] -> [a] mychunk = ...

  19. T ypes: Assumptions & Guarantees mychunk :: [a] -> [a] mychunk = ...

  20. T ypes: Assumptions & Guarantees mychunk :: Eq a => [a] -> [a] mychunk = ...

  21. T ypes: Assumptions & Guarantees mychunk :: Num a => [a] -> [a] mychunk = ...

  22. T ypes: Assumptions & Guarantees mychunk :: Typeable a => [a] -> [a] mychunk = ...

  23. T ypes: Assumptions & Guarantees mychunk :: [a] -> [a] mychunk = ...

  24. T ypes: Locating Bugs processWith f = g (f [100,101]) g xs = ...

  25. T ypes: Locating Bugs processWith :: (forall a. [a] -> [a]) -> [Int] processWith f = g (f [100,101]) g :: [Int] -> [Int] g xs = ...

  26. T ypes: Locating Bugs processWith :: (forall a. [a] -> [a]) -> [Int] processWith f = g (f [100,101]) g :: [a] -> [a] g xs = ...

  27. T ypes: Understanding Code trans xs ys = case xs of [] -> [] ((a,b):xs) -> (map (\(x,y) -> (a,y)) $ f ys) ++ trans xs ys where f [] = [] f ((c,d):ys) | c==b = (b,d) : f ys | otherwise = f ys

  28. T ypes: Understanding Code trans :: [(Int,Int)] -> [(Int,Int)] -> [(Int,Int)] trans xs ys = case xs of [] -> [] ((a,b):xs) -> (map (\(x,y) -> (a,y)) $ f ys) ++ trans xs ys where f [] = [] f ((c,d):ys) | c==b = (b,d) : f ys | otherwise = f ys

  29. T ypes: Understanding Code trans :: Eq b => [(b, b)] -> [(b, b)] -> [(b, b)] trans xs ys = case xs of [] -> [] ((a,b):xs) -> (map (\(x,y) -> (a,y)) $ f ys) ++ trans xs ys where f [] = [] f ((c,d):ys) | c==b = (b,d) : f ys | otherwise = f ys

  30. T ypes: Understanding Code trans :: Eq b => [(b, b)] -> [(b, b)] -> [(b, b)] trans xs ys = case xs of [] -> [] ((a,b):xs) -> (map (\(x,y) -> (a,y)) $ f ys) ++ trans xs ys where f [] = [] f ((c,d):ys) | c==b = (b,d) : f ys | otherwise = f ys

  31. T ypes: Understanding Code trans :: Eq b => [(a, b)] -> [(b, c)] -> [(a, c)] trans xs ys = case xs of [] -> [] ((a,b):xs) -> (map (\(x,y) -> (a,y)) $ f ys) ++ trans xs ys where f [] = [] f ((c,d):ys) | c==b = (b,d) : f ys | otherwise = f ys

  32. T ypes: Understanding Code trans :: Eq b => [(a, b)] -> [(b, c)] -> [(a, c)] trans xs ys = case xs of [] -> [] ((a,b):xs) -> (map (\(x,y) -> (a,y)) $ f ys) ++ trans xs ys where f [] = [] f ((c,d):ys) | c==b = (b,d) : f ys | otherwise = f ys

  33. T ypes: Understanding Code trans :: Eq b => [(a, b)] -> [(b, c)] -> [(a, c)] trans xs ys = [(a,c) | (a,b1) <- xs, (b2,c) <- ys, b1 == b2]

  34. T ypes: Handling very abstract code ala :: Newtype srb => (b -> sb) -> ((a -> sb) -> ta -> srb) -> What is ‘h’ ? (a -> b) -> (ta -> Unwrap srb) ala w h f = unwrap . h (w . f)

  35. T ypes: Handling very abstract code ala :: Newtype srb => (b -> sb) -> ((a -> sb) -> ta -> srb) -> (a -> b) -> (ta -> Unwrap srb) ala w h f = unwrap . h (w . f)

  36. T ypes: Handling very abstract code ala :: Newtype srb => (b -> sb) -> ((a -> sb) -> ta -> srb) -> (a -> b) -> (ta -> Unwrap srb) ala w h f = unwrap . h (w . f)

  37. T ypes: Side Effects do y <- chunk1(a,b) f :: A -> State s B z <- chunk1(a,b) chunk3(x) chunk4(y,z)

  38. T ypes: Side Effects chunk0 a = do y <- chunk1(a,b) z <- chunk1(a,b) chunk3(x) chunk4(y,z) return z

  39. T ypes: Side Effects chunk0 :: A -> State (Int,Bool) Z chunk0 a = do y <- chunk1(a,b) z <- chunk1(a,b) chunk3(x) chunk4(y,z) return z

  40. T ypes: Side Effects chunk0 :: Int -> ReaderT Config (StateT Connection (EitherT DBError Int)) chunk0 a = do y <- chunk1(a,b) z <- chunk1(a,b) chunk3(x) chunk4(y,z) return z

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend