functional programming parametricity types
play

Functional Programming, Parametricity, Types Essential Tools of - PowerPoint PPT Presentation

Functional Programming, Parametricity, Types Essential Tools of Programming YOW! West 2016 Tony Morris The Premise the following are essential to programming success. . . adherence to the functional programming thesis parametricity (and


  1. Functional Programming, Parametricity, Types Essential Tools of Programming YOW! West 2016 Tony Morris

  2. The Premise the following are essential to programming success. . . adherence to the functional programming thesis parametricity (and types)

  3. The Premise the following are essential to programming success. . . adherence to the functional programming thesis parametricity (and types)

  4. The Parametricity Trick parametricity will only work with. . . an inveterate exploitation of the functional programming thesis let’s revisit functional programming

  5. The Parametricity Trick parametricity will only work with. . . an inveterate exploitation of the functional programming thesis let’s revisit functional programming

  6. Reminder so what is functional programming? a means of programming by which expressions are referentially transparent . but what is referential transparency?

  7. Reminder so what is functional programming? a means of programming by which expressions are referentially transparent . but what is referential transparency?

  8. Referential Transparency referential transparency is a decidable property of program expressions functions provide programmers a tool to create referentially transparent expressions The Test for Referential Transparency An expression expr is referentially transparent if in a program p , all occurrences of expr in p can be replaced by an assignment to expr without effecting an observable change in p .

  9. Referential Transparency referential transparency is a decidable property of program expressions functions provide programmers a tool to create referentially transparent expressions The Test for Referential Transparency An expression expr is referentially transparent if in a program p , all occurrences of expr in p can be replaced by an assignment to expr without effecting an observable change in p .

  10. Referential Transparency referential transparency is a decidable property of program expressions functions provide programmers a tool to create referentially transparent expressions The Test for Referential Transparency An expression expr is referentially transparent if in a program p , all occurrences of expr in p can be replaced by an assignment to expr without effecting an observable change in p .

  11. Referential Transparency Example program p = { r = buffer.append(x) r = buffer.append(x) f(r, r) } Refactoring of program p = { f(buffer.append(x), buffer.append(x)) } Is the program refactoring observable for all values of f ?

  12. Referential Transparency Example program p = { r = buffer.append(x) r = buffer.append(x) f(r, r) } Refactoring of program p = { f(buffer.append(x), buffer.append(x)) } Is the program refactoring observable for all values of f ?

  13. Referential Transparency Example program p = { r = buffer.append(x) r = buffer.append(x) f(r, r) } Refactoring of program p = { f(buffer.append(x), buffer.append(x)) } Is the program refactoring observable for all values of f ?

  14. Referential Transparency Example program p = { r = str.length () r = str.length () f(r, r) } Refactoring of program p = { f(str.length (), str.length ()) } Is the program refactoring observable for all values of f ?

  15. Referential Transparency Example program p = { r = str.length () r = str.length () f(r, r) } Refactoring of program p = { f(str.length (), str.length ()) } Is the program refactoring observable for all values of f ?

  16. Referential Transparency Example program p = { r = str.length () r = str.length () f(r, r) } Refactoring of program p = { f(str.length (), str.length ()) } Is the program refactoring observable for all values of f ?

  17. Functional Programming FP is a commitment to preserving referential transparency Quite a while ago, FP won by not-a-little-bit. Moving on. we use tools to achieve this commitment parametricity is one such tool with high reward

  18. Functional Programming FP is a commitment to preserving referential transparency Quite a while ago, FP won by not-a-little-bit. Moving on. we use tools to achieve this commitment parametricity is one such tool with high reward

  19. Functional Programming FP is a commitment to preserving referential transparency Quite a while ago, FP won by not-a-little-bit. Moving on. we use tools to achieve this commitment parametricity is one such tool with high reward

  20. Functional Programming FP is a commitment to preserving referential transparency Quite a while ago, FP won by not-a-little-bit. Moving on. we use tools to achieve this commitment parametricity is one such tool with high reward

  21. What is Parametricity Danielsson, Hughes, Jansson & Gibbons [DHJG06] tell us: Functional programmers often reason about programs as if they were written in a total language, expecting the results to carry over to non-total (partial) languages. We justify such reasoning.

  22. What is Parametricity Philip Wadler [Wad89] tells us: 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. The purpose of this paper is to explain the trick.

  23. Types first let’s talk about types Suppose we encountered the following function definition: int add12(int) by the type alone, there are (2 32 ) 2 32 possible implementations but this is a significantly smaller number than 8 Importantly, we know nothing more about this function from its type

  24. Types first let’s talk about types Suppose we encountered the following function definition: int add12(int) by the type alone, there are (2 32 ) 2 32 possible implementations but this is a significantly smaller number than 8 Importantly, we know nothing more about this function from its type

  25. Types first let’s talk about types Suppose we encountered the following function definition: int add12(int) by the type alone, there are (2 32 ) 2 32 possible implementations but this is a significantly smaller number than 8 Importantly, we know nothing more about this function from its type

  26. Types reading the code We might form a suspicion that add12 adds twelve to its argument int add12 (int)

  27. Types So we write some speculative tests to relieve our anxiety: add12 (0) = 12 add12 (5) = 17 add12 (-5) = 7 add12 (223) = 235 add12 (5096) = 5104 add12 (2914578) = 29145590 add12 ( -2914578) = -29145566 And pat ourselves on the back, concluding, yes, this function adds twelve to its argument

  28. Types Woops! and then def add12(n: Int): Int = if(n < 8000000) n + 12 else n * 7 We need to narrow down the potential propositions about what this function does not do.

  29. Types another monomorphic example List <int > function(List <int >) adds 17 to every 11th element? drops every prime number?

  30. Types another monomorphic example List <int > function(List <int >) adds 17 to every 11th element? drops every prime number?

  31. Parametricity a polymorphic example <A> List <A> function(List <A>) this function returns elements in a list that always appear in the argument or it would not have compiled Convince yourself of this. Commit to this statement.

  32. Parametricity a polymorphic example <A> List <A> function(List <A>) this function returns elements in a list that always appear in the argument or it would not have compiled Convince yourself of this. Commit to this statement.

  33. Parametricity the goal a significant number of possible things that this function does are eliminated, by no expenditure of effort theorems about this function can be reliably constructed

  34. Parametricity the goal a significant number of possible things that this function does are eliminated, by no expenditure of effort theorems about this function can be reliably constructed

  35. Reasoning with parametricity Fast and loose reasoning is morally correct [DHJG06] Functional programmers often reason about programs as if they were written in a total language, expecting the results to carry over to non-total (partial) languages. We justify such reasoning. but what does this mean exactly?

  36. Fast and Loose Reasoning boolean even(int i) = ... We casually say, “This function returns one of two things.”

  37. Fast and Loose Reasoning boolean even(int i) = even(i) and we can discard this third possibility in analysis.

  38. Fast and Loose Reasoning many programming environments involve null exceptions Type-casing Type-casting Side-effects a universal equals / toString a remember, FP has won, don’t forget These must all be discarded . The penalty for this is zero .

  39. The Limits of Parametricity C# type signature List <int > function(List <int >) From the monomorphic type , what does this function do?

  40. The Limits of Parametricity C# type signature List <A> function <A>(List <A>) From the polymorphic type , what does this function do? FACT: all elements in the result appear in the input. How do we narrow down to disambiguity?

  41. The Limits of Parametricity Do we? write comments above the function /* This function twiddles the database to twoddle out the twip twop */ OR write true testable statements about the function

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