Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.

Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.

Successfully reported this slideshow.

Like this presentation? Why not share!

8,402 views

Published on

Video: http://vimeo.com/58236838

Published in:
Technology

No Downloads

Total views

8,402

On SlideShare

0

From Embeds

0

Number of Embeds

49

Shares

0

Downloads

148

Comments

0

Likes

6

No embeds

No notes for slide

- 1. Introduction to Laws
- 2. About @nkpart Scalaz Functional JavaRuby/Scala/Haskell/Objective-C FunctionalKitMogeneration Kit Various ruby gems
- 3. Why Laws?They are the footnote in everymonad tutorial.Too hard at the beginning.Programming isn’t even Mathsanyway.
- 4. Laws are theNew/Old HotnessYou are already programming withLaws.Laws are fundamental tounderstanding most typeclasses.Programming *is* maths.
- 5. What are Laws Understanding LawsLegal Benefits Breaking Laws
- 6. What are Laws Common LawsLegal Benefits Breaking Laws
- 7. (7 + 5) + 3 = 7 + (5 + 3)
- 8. (a + b) + c = a + (b + c)
- 9. (a ⊗ b) ⊗ c = a ⊗ (b ⊗ c)
- 10. What are Laws?Laws are statements made aboutequivalence of expressions.Some Laws come from Maths Abstract Algebra
- 11. What are Laws?Integer addition obeys theAssociativity LawWherever we use IntegerAddition, we can use theproperties of the law to ouradvantage. 7 + 3 + 5 + 2 = 7 + (3 + 5) + 2For an implementor, the Law is aContract!
- 12. add :: Int -> Int -> Int
- 13. What are Laws?Laws are not checked by the typesystemLaws can be broken byimplementationsVerification is usually done byhand Alternatively, QuickCheck.
- 14. What are Laws?Statements of EquivalenceContracts for the ImplementorLaws are not checked by the typesystemOrigins in Maths. (Programming)
- 15. What are Laws Common LawsLegal Benefits Breaking Laws
- 16. Common LawsAbstract Algebra Associative Law Commutative Law IdentityTypeclasses and their Laws Monoid Functor
- 17. Associative Law(a ⊗ b) ⊗ c = a ⊗ (b ⊗ c) Satisfied by: +, *, concat Uses: Parallelism. String building (refactoring)
- 18. Associative Law [1,2,3,4,5,6] fold/each/inject1 + (2 + (3 + (4 + (5 + 6)))) apply the law!1 + (2 + ((3 + 4) + (5 + 6))))
- 19. Commutativity Lawa ⊗ b = b ⊗ a Satisfied by: +, *, but not concat! Uses: Parallelism (again!)
- 20. Commutative Law1 + (2 + ((3 + 4) + (5 + 6)))) apply the law!1 + (2 + ((5 + 6) + (3 + 4))))
- 21. Identity Lawa ⊗ Id = a = Id ⊗ a Satisfied by: +/0, */1, concat/ []
- 22. Typeclasses
- 23. Monoidclass Monoid a where mappend :: a -> a -> a mempty :: a
- 24. Monoidmappend satisfies theAssociative Lawmempty is the Identity for themappend operation.
- 25. class Monoid a where mappend :: a -> a -> a mempty :: a
- 26. MonoidCODE TIME
- 27. Functor
- 28. class Functor f where fmap :: (a -> b) -> f a -> f b
- 29. Functor Lawsfmap id x = id xfmap (g . h) = (fmap g) . (fmap h)where id a = a id a = a
- 30. Not a Functorinstance Functor [] where fmap f [] = [] fmap f (x:xs) = (f x):(f x):(fmap f xs)
- 31. Functor LawsFunctor is a structure with an`fmap` that does not affect thatstructure, just the valuesinside.To modify the structure you needa different typeclass. The lawsprevent it.
- 32. What are Laws Common LawsLegal Benefits Breaking Laws
- 33. Legal BenefitsMeaning to Multi-functionTypeclassesGreater understanding ofTypeclassesSubstitution of Expressions
- 34. Instancing Functor CODE TIME
- 35. Subsituting ExpressionsSome Haskell tooling can usethisHLintGHC Rewrite Rules
- 36. HLintCODE TIME
- 37. Rewrite Rules{-# RULES“map/map” forall f g xs.map f (map g xs) = map (f . g) xs#-}
- 38. What are Laws Common LawsLegal Benefits Breaking Laws
- 39. Breaking LawsLawless Typeclasses Pointed (Haskell [deprecated[ and Scalaz [never released]) Zero (Scalaz [never released])Real World Broken Instances Bijection (Twitter), ListT (Haskell Platform), Gen (from QuickCheck)
- 40. “Lawless Typeclasses”class Zero a where zero :: aclass Pointed f where return :: a -> f a
- 41. Broken Instances Broken typeclass instances exist Verification is hard. The Gen Monad: CODE TIME
- 42. Consequences ofIllegal Behaviour
- 43. Consequences ofIllegal Behaviour Bijection[Int, String]
- 44. Consequences ofIllegal Behaviour Specific code using a Bijection[Int, String] might be fine. What if I write a function that uses a Bijection[A,B]?
- 45. What are Laws Understanding LawsLegal Benefits Breaking Laws
- 46. Laws are GoodLaws have a huge impact on the waywe code (already). Refactoring, Algebraic LawsTaking advantage of laws is apowerful programming technique. Understanding typeclasses, writing new instancesWatch out for Law breakers!
- 47. Useful ResourcesHaskell Packages- Lens Typeclassopedia- Semigroupoids #scalaz- Pipes #haskell[.au] #bfpgScalaz
- 48. Thanks!

No public clipboards found for this slide

Be the first to comment