Introduction to Laws
-
Upload
nkpart -
Category
Technology
-
view
2.318 -
download
3
description
Transcript of Introduction to Laws
Introduction to Laws
About @nkpart
Scalaz
Functional Java
FunctionalKit
Kit
Various ruby gems
Ruby/Scala/Haskell/Objective-C
Mogeneration
Why Laws?
They are the footnote in every monad tutorial.
Too hard at the beginning.
Programming isn’t even Maths anyway.
Laws are the New/Old Hotness
You are already programming with Laws.
Laws are fundamental to understanding most typeclasses.
Programming *is* maths.
Legal Benefits Breaking Laws
What are Laws Understanding Laws
Legal Benefits Breaking Laws
What are Laws Common Laws
(7 + 5) + 3 = 7 + (5 + 3)
(a + b) + c = a + (b + c)
(a ⊗ b) ⊗ c = a ⊗ (b ⊗ c)
Laws are statements made about equivalence of expressions.
Some Laws come from Maths
Abstract Algebra
What are Laws?
Integer addition obeys the Associativity Law
Wherever we use Integer Addition, we can use the properties of the law to our advantage.
7 + 3 + 5 + 2 = 7 + (3 + 5) + 2
For an implementor, the Law is a Contract!
What are Laws?
add :: Int -> Int -> Int
Laws are not checked by the type system
Laws can be broken by implementations
Verification is usually done by hand
Alternatively, QuickCheck.
What are Laws?
Statements of Equivalence
Contracts for the Implementor
Laws are not checked by the type system
Origins in Maths. (Programming)
What are Laws?
Legal Benefits Breaking Laws
What are Laws Common Laws
Abstract Algebra
Associative Law
Commutative Law
Identity
Typeclasses and their Laws
Monoid
Functor
Common Laws
Satisfied by: +, *, concat
Uses: Parallelism. String building (refactoring)
Associative Law
(a ⊗ b) ⊗ c = a ⊗ (b ⊗ c)
Associative Law
[1,2,3,4,5,6]fold/each/inject
1 + (2 + (3 + (4 + (5 + 6))))apply the law!
1 + (2 + ((3 + 4) + (5 + 6))))
Satisfied by: +, *, but not concat!
Uses: Parallelism (again!)
Commutativity Law
a ⊗ b = b ⊗ a
Commutative Law
1 + (2 + ((3 + 4) + (5 + 6))))apply the law!
1 + (2 + ((5 + 6) + (3 + 4))))
Satisfied by: +/0, */1, concat/[]
Identity Law
a ⊗ Id = a = Id ⊗ a
Typeclasses
class Monoid a where mappend :: a -> a -> a mempty :: a
Monoid
mappend satisfies the Associative Law
mempty is the Identity for the mappend operation.
Monoid
class Monoid a where mappend :: a -> a -> a mempty :: a
CODE TIME
Monoid
Functor
class Functor f where fmap :: (a -> b) -> f a -> f b
fmap id x = id xfmap (g . h) = (fmap g) . (fmap h)
where id a = a
Functor Laws
instance Functor [] where fmap f [] = [] fmap f (x:xs) = (f x):(f x):(fmap f xs)
Not a Functor
Functor is a structure with an `fmap` that does not affect that structure, just the values inside.
To modify the structure you need a different typeclass. The laws prevent it.
Functor Laws
Legal Benefits Breaking Laws
What are Laws Common Laws
Meaning to Multi-function Typeclasses
Greater understanding of Typeclasses
Substitution of Expressions
Legal Benefits
CODE TIME
Instancing Functor
Some Haskell tooling can use this
HLint
GHC Rewrite Rules
Subsituting Expressions
CODE TIME
HLint
Rewrite Rules
{-# RULES “map/map” forall f g xs.map f (map g xs) = map (f . g) xs#-}
Legal Benefits Breaking Laws
What are Laws Common Laws
Lawless Typeclasses
Pointed (Haskell [deprecated[ and Scalaz [never released])
Zero (Scalaz [never released])
Real World Broken Instances
Bijection (Twitter), ListT (Haskell Platform), Gen (from QuickCheck)
Breaking Laws
“Lawless Typeclasses”
class Zero a where zero :: a
class Pointed f where return :: a -> f a
Broken typeclass instances exist
Verification is hard.
The Gen Monad: CODE TIME
Broken Instances
Consequences ofIllegal Behaviour
Consequences ofIllegal Behaviour
Bijection[Int, String]
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]?
Legal Benefits Breaking Laws
What are Laws Understanding Laws
Laws are Good
Laws have a huge impact on the way we code (already).
Refactoring, Algebraic Laws
Taking advantage of laws is a powerful programming technique.
Understanding typeclasses, writing new instances
Watch out for Law breakers!
Useful Resources
Typeclassopedia
#scalaz
#haskell[.au]
#bfpg
Haskell Packages
- Lens
- Semigroupoids
- Pipes
Scalaz
Thanks!