Top Banner
Introduction to Laws
48

Introduction to Laws

May 12, 2015

Download

Technology

nkpart

Talk given on 2013-01-22 to the BFPG: http://www.bfpg.org/events/87030712/

Video: http://vimeo.com/58236838
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction to Laws

Introduction to Laws

Page 2: Introduction to Laws

About @nkpart

Scalaz

Functional Java

FunctionalKit

Kit

Various ruby gems

Ruby/Scala/Haskell/Objective-C

Mogeneration

Page 3: Introduction to Laws

Why Laws?

They are the footnote in every monad tutorial.

Too hard at the beginning.

Programming isn’t even Maths anyway.

Page 4: Introduction to Laws

Laws are the New/Old Hotness

You are already programming with Laws.

Laws are fundamental to understanding most typeclasses.

Programming *is* maths.

Page 5: Introduction to Laws

Legal Benefits Breaking Laws

What are Laws Understanding Laws

Page 6: Introduction to Laws

Legal Benefits Breaking Laws

What are Laws Common Laws

Page 7: Introduction to Laws

(7 + 5) + 3 = 7 + (5 + 3)

Page 8: Introduction to Laws

(a + b) + c = a + (b + c)

Page 9: Introduction to Laws

(a ⊗ b) ⊗ c = a ⊗ (b ⊗ c)

Page 10: Introduction to Laws

Laws are statements made about equivalence of expressions.

Some Laws come from Maths

Abstract Algebra

What are Laws?

Page 11: Introduction to 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?

Page 12: Introduction to Laws

add :: Int -> Int -> Int

Page 13: Introduction to Laws

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?

Page 14: Introduction to Laws

Statements of Equivalence

Contracts for the Implementor

Laws are not checked by the type system

Origins in Maths. (Programming)

What are Laws?

Page 15: Introduction to Laws

Legal Benefits Breaking Laws

What are Laws Common Laws

Page 16: Introduction to Laws

Abstract Algebra

Associative Law

Commutative Law

Identity

Typeclasses and their Laws

Monoid

Functor

Common Laws

Page 17: Introduction to Laws

Satisfied by: +, *, concat

Uses: Parallelism. String building (refactoring)

Associative Law

(a ⊗ b) ⊗ c = a ⊗ (b ⊗ c)

Page 18: Introduction to Laws

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))))

Page 19: Introduction to Laws

Satisfied by: +, *, but not concat!

Uses: Parallelism (again!)

Commutativity Law

a ⊗ b = b ⊗ a

Page 20: Introduction to Laws

Commutative Law

1 + (2 + ((3 + 4) + (5 + 6))))apply the law!

1 + (2 + ((5 + 6) + (3 + 4))))

Page 21: Introduction to Laws

Satisfied by: +/0, */1, concat/[]

Identity Law

a ⊗ Id = a = Id ⊗ a

Page 22: Introduction to Laws

Typeclasses

Page 23: Introduction to Laws

class Monoid a where mappend :: a -> a -> a mempty :: a

Monoid

Page 24: Introduction to Laws

mappend satisfies the Associative Law

mempty is the Identity for the mappend operation.

Monoid

Page 25: Introduction to Laws

class Monoid a where mappend :: a -> a -> a mempty :: a

Page 26: Introduction to Laws

CODE TIME

Monoid

Page 27: Introduction to Laws

Functor

Page 28: Introduction to Laws

class Functor f where fmap :: (a -> b) -> f a -> f b

Page 29: Introduction to Laws

fmap id x = id xfmap (g . h) = (fmap g) . (fmap h)

where id a = a

Functor Laws

Page 30: Introduction to Laws

instance Functor [] where fmap f [] = [] fmap f (x:xs) = (f x):(f x):(fmap f xs)

Not a Functor

Page 31: Introduction to Laws

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

Page 32: Introduction to Laws

Legal Benefits Breaking Laws

What are Laws Common Laws

Page 33: Introduction to Laws

Meaning to Multi-function Typeclasses

Greater understanding of Typeclasses

Substitution of Expressions

Legal Benefits

Page 34: Introduction to Laws

CODE TIME

Instancing Functor

Page 35: Introduction to Laws

Some Haskell tooling can use this

HLint

GHC Rewrite Rules

Subsituting Expressions

Page 36: Introduction to Laws

CODE TIME

HLint

Page 37: Introduction to Laws

Rewrite Rules

{-# RULES “map/map” forall f g xs.map f (map g xs) = map (f . g) xs#-}

Page 38: Introduction to Laws

Legal Benefits Breaking Laws

What are Laws Common Laws

Page 39: Introduction to 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

Page 40: Introduction to Laws

“Lawless Typeclasses”

class Zero a where zero :: a

class Pointed f where return :: a -> f a

Page 41: Introduction to Laws

Broken typeclass instances exist

Verification is hard.

The Gen Monad: CODE TIME

Broken Instances

Page 42: Introduction to Laws

Consequences ofIllegal Behaviour

Page 43: Introduction to Laws

Consequences ofIllegal Behaviour

Bijection[Int, String]

Page 44: Introduction to Laws

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]?

Page 45: Introduction to Laws

Legal Benefits Breaking Laws

What are Laws Understanding Laws

Page 46: Introduction to 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!

Page 47: Introduction to Laws

Useful Resources

Typeclassopedia

#scalaz

#haskell[.au]

#bfpg

Haskell Packages

- Lens

- Semigroupoids

- Pipes

Scalaz

Page 48: Introduction to Laws

Thanks!