Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Parametricity and other principles - CompCon 2015

Parametricity and other principles - CompCon 2015

Presentation to university students about parametricity and other important principles for working programmers, at CompCon 2015.

Fraser Tweedale

October 04, 2015
Tweet

More Decks by Fraser Tweedale

Other Decks in Programming

Transcript

  1. Target audience Software developers whose goals are: Correct software Maintainable

    software Still correct after maintenance Move fast and don’t break things
  2. Move fast and break things? “When you build something that

    you don’t have to fix 10 times, you can move forward on top of what you’ve built. It may not be quite as catchy as ‘move fast and break things’ but it’s how we operate now.” Mark Zuckerberg http://is.gd/vx3tgj
  3. Outline Principles and tools for the working programmer Lots of

    opinions Mine, not my employer’s Some facts May contain traces of unfamiliar syntax Manufactured on equipment that also processes tree nuts
  4. Correctness Doesn’t matter that much right? “But I’m only ever

    going to write _______” web things? boring business logic? games? Broken software has consequences Start caring; embrace the tools
  5. Data modelling Model data responsibly Do your research Falsehoods programmers

    believe about {names, time, . . . } Make illegal state unrepresentable Newtypes, units of measure Immutability
  6. Dynamically typed languages Duck typing, type juggling, argh! Checking argument

    “types” is boring yet fraught Little help with program construction, refactoring Type checkers eliminate whole classes of errors for free Principled abstractions. . . where are they?
  7. Criticisms of static typing Too verbose Types can’t capture the

    information in docs Types can’t catch all errors I need rapid feedback and the ability to prototype quickly But I like {Python, JavaScript, . . . }!
  8. Verbosity # Python print("Enter your name: ", end=’’) name =

    input() print("Hi, " + name) -- Haskell main = do putStr "Enter your name: " name <- getLine putStrLn ("Hi, " ++ name)
  9. Type safety “People first need to feel the pain of

    runtime errors to understand why a type system is useful.” Python aficionado
  10. Escape hatches null values type casing (instanceof) type casting exceptions

    general recursion side-effects (I/O, impure functions)
  11. I know what you’re thinking How will I do X

    without feature Y? How will I reason about your program to do X if you use Y?
  12. I know what you’re thinking How will I do X

    without feature Y? How will I reason about your program to do X if you use Y? What use is a program without side effects?
  13. I know what you’re thinking How will I do X

    without feature Y? How will I reason about your program to do X if you use Y? What use is a program without side effects? What use is a program with uncontrolled side effects?
  14. I know what you’re thinking How will I do X

    without feature Y? How will I reason about your program to do X if you use Y? What use is a program without side effects? What use is a program with uncontrolled side effects? What about algorithms that are inherently stateful?
  15. I know what you’re thinking How will I do X

    without feature Y? How will I reason about your program to do X if you use Y? What use is a program without side effects? What use is a program with uncontrolled side effects? What about algorithms that are inherently stateful? They don’t exist. (Church-Turing thesis)
  16. I know what you’re thinking How will I do X

    without feature Y? How will I reason about your program to do X if you use Y? What use is a program without side effects? What use is a program with uncontrolled side effects? What about algorithms that are inherently stateful? They don’t exist. (Church-Turing thesis) It’s fine, I’ve got this.
  17. I know what you’re thinking How will I do X

    without feature Y? How will I reason about your program to do X if you use Y? What use is a program without side effects? What use is a program with uncontrolled side effects? What about algorithms that are inherently stateful? They don’t exist. (Church-Turing thesis) It’s fine, I’ve got this. No you don’t (and neither do I).
  18. I know what you’re thinking How will I do X

    without feature Y? How will I reason about your program to do X if you use Y? What use is a program without side effects? What use is a program with uncontrolled side effects? What about algorithms that are inherently stateful? They don’t exist. (Church-Turing thesis) It’s fine, I’ve got this. No you don’t (and neither do I). Apologise to your team mates and future-self right now.
  19. Total languages languages that guarantee program termination examples: Agda, Coq,

    Idris can express most programs (all that we care about in industry) most programmers are using non-total languages
  20. Fast and loose reasoning reasoning about programs written in non-total

    langauges as if they were written in a total language. justified (mathematically) in Fast and loose reasoning is morally correct, Danielsson et al. 2006 costs nothing; you can write any program without using escape hatches and you gain composability referential integrity equational reasoning reuse
  21. Parametric polymorphism (syntax) // Java generics static <A> List<A> f(List<A>

    xs) { ... } # Python (with PEP 484 type hinting) def f(xs : List[T]) -> List[T]: ... -- Haskell f :: [a] -> [a] f xs = ...
  22. Parametric polymorphism Two function signatures: f :: [Int] -> [Int]

    g :: [ a ] -> [ a ] Which gives the programmer more information? Which is better?
  23. Parametricity “Write down the definition of a polymorphic function on

    a piece of paper. Tell me its type, but be careful not to let me see the function’s definition. I will tell you a theorem that the function satisfies.” Theorems for Free! Wadler 1989 Polymorphic types are: partial (sometimes complete) specifications implementor cannot use any type-specific behaviour caller has guarantee function will work for any type machine-checkable documentation a free source of useful theorems (parametricity)
  24. Parametricity f :: [a] -> [a] What does this function

    do? reverse a list? drop the first element?
  25. Parametricity f :: [a] -> [a] What does this function

    do? reverse a list? drop the first element? ignore its argument return an empty list?
  26. Parametricity f :: [a] -> [a] What does this function

    do? reverse a list? drop the first element? ignore its argument return an empty list? Theorem: every element in the output appears in the input
  27. Parametricity f :: [a] -> [a] What does this function

    do? reverse a list? drop the first element? ignore its argument return an empty list? Theorem: every element in the output appears in the input Theorem: ∀ g. map g ◦ f = f ◦ map g
  28. Parametricity f :: a -> (a, a) g :: a

    -> a -> a h :: b -> a -> a https://twitter.com/parametricity
  29. Testing Recall our old friend: f :: [a] -> [a]

    It turns out we do want f to reverse lists Alas! ([a] -> [a]) does not mean reverse
  30. Testing ∀ x. f [x] = [x] ∀ xs ys.

    f (xs ++ ys) = f ys ++ f xs f :: [a] -> [a] Now we’re good. A minimal (yet complete) algebraic specification for list reversal
  31. Property-based testing write down properties of your functions tell the

    framework how to generate random values framework generates lots of data, tries to falsify properties the best test data is random test data QuickCheck: A Lightweight Tool for Random Testing of Haskell Programs. Claessen and Hughes 2000 Talk: https://youtu.be/TSDTDqoH_vM
  32. Theorem proving the best test data is no test data

    proof assistants: Agda, Coq, Idris no proof, no program can prove correctness, equivalence, complexity, . . . program extraction to other languages Idris talk/demo: https://youtu.be/4i7KrG1Afbk
  33. Theorem proving “Beware of bugs in the above code; I

    have only proved it correct, not tried it.” Donald Knuth
  34. Theorem proving “We are waiting for you to catch up,

    so we can employ you.” anonymous, NICTA (data61)
  35. Excuses But we are a {Java, C#, . . .

    } shop! We won’t be able to hire {Haskell, Scala, . . . } devs! The academic-ness, it burns!
  36. Languages Scala, Frege, Clojure, Ermine (JVM) F# (Common Language Infrastructure)

    Swift (iThings) Do you really need to stick to {JVM, CLI}? Haskell, Rust
  37. Resources Brisbane Functional Programming Group http://bfpg.org/ #bfpg on Freenode or

    find an FP group in your city https://github.com/NICTA/course Software Foundations Course on mathematical underpinnings of reliable software http://www.cis.upenn.edu/~bcpierce/sf
  38. Conclusion Responsible modelling of data is paramount Types are your

    friend Avoid traps and use fast and loose reasoning Parametric polymorphism gives you theorems for free Algebraic properties → more thorough tests with less effort Use the right tools
  39. Fin Copyright 2015 Fraser Tweedale This work is licensed under

    the Creative Commons Attribution 4.0 International License. To view a copy of this license, visit http://creativecommons.org/licenses/by/4.0/. Slides https://github.com/frasertweedale/talks/ Twitter @hackuador Email [email protected]