laws
play

Laws! George Wilson Data61/CSIRO george.wilson@data61.csiro.au - PowerPoint PPT Presentation

Laws! George Wilson Data61/CSIRO george.wilson@data61.csiro.au 28th August 2018 class Monoid m where mempty :: m (<>) :: m -> m -> m class Monoid m where mempty :: m (<>) :: m -> m -> m Left identity: mempty <>


  1. Laws! George Wilson Data61/CSIRO george.wilson@data61.csiro.au 28th August 2018

  2. class Monoid m where mempty :: m (<>) :: m -> m -> m

  3. class Monoid m where mempty :: m (<>) :: m -> m -> m Left identity: mempty <> y = y Right identity: x <> mempty = x Associativity: (x <> y) <> z = x <> (y <> z)

  4. data Sum = Sum Int instance Monoid Sum where mempty = Sum 0 Sum x <> Sum y = Sum (x + y)

  5. Left identity: Right identity: 0 + y = y x + 0 = x

  6. Left identity: Right identity: 0 + 5 = 5 x + 0 = x

  7. Left identity: Right identity: 0 + 5 = 5 7 + 0 = 7

  8. Associativity: 3 + 4 + 5 (3 + 4) + 5 3 + (4 + 5)

  9. + + + 5 3 + 3 4 4 5

  10. + + 7 5 3 + 4 5

  11. 12 + 3 + 4 5

  12. 12 + 3 9

  13. 12 12

  14. So?

  15. + + + + 5 6 7 1 + + 4 2 3

  16. + + + + + + 7 1 2 3 4 5 6

  17. + + 7 + 6 + 5 + 4 + 3 1 2

  18. + 1 + 2 + 3 + 4 + 5 + 6 7

  19. mconcat :: Monoid m => [m] -> m

  20. mconcat :: Monoid m => [m] -> m mconcat list = case list of [] -> mempty (h : t) -> h <> mconcat t

  21. mconcat [ Sum 1, Sum 2, Sum 3, Sum 4]

  22. mconcat [ Sum 1, Sum 2, Sum 3, Sum 4] Sum 1 <> ( Sum 2 <> ( Sum 3 <> ( Sum 4 <> mempty)))

  23. mconcat [ Sum 1, Sum 2, Sum 3, Sum 4] Sum 1 <> ( Sum 2 <> ( Sum 3 <> ( Sum 4 <> mempty))) ==> Sum 10

  24. mconcatR :: NotMonoid m => [m] -> m mconcatR list = case list of [] -> mempty (h : t) -> h <> mconcatR t

  25. mconcatR :: NotMonoid m => [m] -> m mconcatR list = case list of [] -> mempty (h : t) -> h <> mconcatR t mconcatL :: NotMonoid m => [m] -> m mconcatL list = helper mempty list where helper acc xs = case xs of [] -> acc (h : t) -> helper (acc <> h) t

  26. foldr :: (a -> b -> b) -> b -> [a] -> b foldl :: (b -> a -> b) -> b -> [a] -> b

  27. Laws give us freedom when working in terms of our abstractions

  28. instance Monoid [a] where mempty = [] left <> right = case left of [] -> right (h : t) -> h : (t <> right)

  29. instance Monoid [a] where mempty = [] left <> right = case left of [] -> right (h : t) -> h : (t <> right) Left identity: [] ++ y = y Right identity: x ++ [] = x Associativity: (x ++ y) ++ z = x ++ (y ++ z)

  30. greeting :: [ Char ] -> [ Char ] greeting name = "(" <> "Hello, " <> name <> ", how are you?" <> ")"

  31. greeting :: [ Char ] -> [ Char ] greeting name = "(" <> "Hello, " <> name <> ", how are you?" <> ")" between op cl x = op <> x <> cl

  32. greeting :: [ Char ] -> [ Char ] greeting name = between "(" ")" $ "Hello, " <> name <> ", how are you?" between op cl x = op <> x <> cl

  33. greeting :: [ Char ] -> [ Char ] greeting name = between "(" ")" $ between "Hello, " ", how are you?" name between op cl x = op <> x <> cl

  34. <> "(" <> "Hello, " <> name <> ", how are you?" ")"

  35. <> "(" <> <> ")" "Hello, " <> name ", how are you?"

  36. Laws let us refactor and reuse more

  37. ([1,2,3] <> [4,5,6]) <> [7,8,9]

  38. ([1,2,3] <> [4,5,6]) <> [7,8,9] :(

  39. : : : Nil : : : Nil : : : Nil 1 2 3 4 5 6 7 8 9

  40. : : : Nil : : : Nil : : : Nil 1 2 3 4 5 6 7 8 9 1 :

  41. : : : Nil : : : Nil : : : Nil 1 2 3 4 5 6 7 8 9 : : 1 2

  42. : : : Nil : : : Nil : : : Nil 1 2 3 4 5 6 7 8 9 : : : 1 2 3

  43. : : : Nil : : : Nil 4 5 6 7 8 9 : : : 1 2 3

  44. : : : Nil : : : Nil 4 5 6 7 8 9 : : : 1 2 3 1 :

  45. : : : Nil : : : Nil 4 5 6 7 8 9 : : : 1 2 3 : : 1 2

  46. : : : Nil : : : Nil 4 5 6 7 8 9 : : : 1 2 3 : : : 1 2 3

  47. : : : Nil : : : Nil 4 5 6 7 8 9 : : : 1 2 3 : : : 4 : 1 2 3

  48. : : : Nil : : : Nil 4 5 6 7 8 9 : : : 1 2 3 : : : 4 : : 1 2 3 5

  49. : : : Nil : : : Nil 4 5 6 7 8 9 : : : 1 2 3 : : : 4 : : : 1 2 3 5 6

  50. data DList a

  51. data DList a instance Monoid ( DList a) -- O(1) append

  52. data DList a instance Monoid ( DList a) -- O(1) append fromList :: [a] -> DList a -- O(1) toList :: DList a -> [a] -- O(n)

  53. result :: [a] result = (((((((x <> y) <> z) <> ...

  54. result :: [a] result = (((((((x <> y) <> z) <> ... appended :: DList a appended = (((((((fromList x <> fromList y) <> fromList z) <> ... result' :: [a] result' = toList appended

  55. O(n 2 ) left-associated appends list list

  56. O(n 2 ) left-associated appends list list O(n) fromList DList

  57. O(n 2 ) left-associated appends list list O(n) fromList DList DList left-associated appends O(n)

  58. O(n 2 ) left-associated appends list list O(n) O(n) fromList toList DList DList left-associated appends O(n)

  59. Optimisation is altering the program to get the same answer more efficiently

  60. toList is the left inverse of fromList toList (fromList x) = x

  61. fromList is a monoid homomorphism fromList :: [a] -> DList a

  62. fromList is a monoid homomorphism fromList :: [a] -> DList a fromList mempty = mempty fromList(x <> y) = fromList x <> fromList y

  63. x <> y <> z

  64. x <> y <> z Left inverse: toList (fromList (x)) = x

  65. toList (fromList (x <> y <> z)) Left inverse: toList (fromList (x)) = x

  66. toList (fromList (x <> y <> z)) Monoid homomorphism: fromList (x <> y <> z) = fromList x <> fromList y <> fromList z

  67. toList (fromList x <> fromList y <> fromList z) Monoid homomorphism: fromList (x <> y <> z) = fromList x <> fromList y <> fromList z

  68. What about a world without laws?

  69. class Default a where def :: a

  70. class Default a where def :: a instance Default [a] where def = []

  71. class Default a where def :: a instance Default [a] where def = [] instance Default Int where def = 0

  72. orDefault :: Default a => Maybe a -> a orDefault ma = case ma of Just a -> a Nothing -> def

  73. orDefault :: Default a => Maybe a -> a orDefault ma = case ma of Just a -> a Nothing -> def orElse :: a -> Maybe a -> a orElse d ma = case ma of Just a -> a Nothing -> d

  74. -- | Current default -1 chosen by ertes, -- the largest negative number. instance Default Int64 where def = -1

  75. -- | Current default -1 chosen by ertes, -- the largest negative number. instance Default Int64 where def = -1 -- | Current default 'False' chosen by ertes, -- the answer to the question -- whether mniip has a favourite 'Bool'. instance Default Bool where def = False

  76. -- | Current default -1 chosen by ertes, -- the largest negative number. instance Default Int64 where def = -1 -- | Current default 'False' chosen by ertes, -- the answer to the question -- whether mniip has a favourite 'Bool'. instance Default Bool where def = False instance Default String where def = "Call me Ishmael. Some years ago - never mind how long precisely

  77. How do I know whether I obey the laws?

  78. QuickCheck + checkers Property-based testing for laws!

  79. monoid :: ( Monoid a, Show a, Arbitrary a, EqProp a) => a -> TestBatch

  80. monoid :: ( Monoid a, Show a, Arbitrary a, EqProp a) => a -> TestBatch functor :: ( Functor t, Arbitrary a, Arbitrary b, Arbitrary c, CoArbitrary a, CoArbitrary b, Show (t a), Arbitrary (t a), EqProp (t a), EqProp (t c)) => t (a, b, c) -> TestBatch

  81. data Subtraction = Subt Int -- totally dodgy instance Monoid Subtraction where mempty = Subt 0 Subt x <> Subt y = Subt (x - y)

  82. data Subtraction = Subt Int -- totally dodgy instance Monoid Subtraction where mempty = Subt 0 Subt x <> Subt y = Subt (x - y) main :: IO () main = do quickBatch (monoid ( Sum 0)) quickBatch (monoid ( Subt 0))

  83. Sum monoid: left identity: +++ OK, passed 500 tests. right identity: +++ OK, passed 500 tests. associativity: +++ OK, passed 500 tests. Subtraction "monoid": left identity: *** Failed! Falsifiable (after 2 tests) right identity: +++ OK, passed 500 tests. associativity: *** Failed! Falsifiable (after 2 tests)

  84. Laws give rise to useful functions Laws allow us to refactor more Laws help us to optimise

  85. Laws are the difference between an overloaded name and an abstraction

  86. Thanks for listening!

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