SLIDE 1 Functional Programming, Parametricity, Types
Essential Tools of Programming YOW! West 2016 Tony Morris
SLIDE 2
The Premise
the following are essential to programming success. . . adherence to the functional programming thesis parametricity (and types)
SLIDE 3
The Premise
the following are essential to programming success. . . adherence to the functional programming thesis parametricity (and types)
SLIDE 4
The Parametricity Trick
parametricity will only work with. . . an inveterate exploitation of the functional programming thesis let’s revisit functional programming
SLIDE 5
The Parametricity Trick
parametricity will only work with. . . an inveterate exploitation of the functional programming thesis let’s revisit functional programming
SLIDE 6
Reminder
so what is functional programming? a means of programming by which expressions are referentially transparent. but what is referential transparency?
SLIDE 7
Reminder
so what is functional programming? a means of programming by which expressions are referentially transparent. but what is referential transparency?
SLIDE 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.
SLIDE 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.
SLIDE 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.
SLIDE 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?
SLIDE 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?
SLIDE 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?
SLIDE 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?
SLIDE 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?
SLIDE 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?
SLIDE 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
SLIDE 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
SLIDE 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
SLIDE 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
SLIDE 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.
SLIDE 22 What is Parametricity
Philip Wadler [Wad89] tells us: Write down the definition of a polymorphic function
- n 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.
SLIDE 23
Types
first let’s talk about types Suppose we encountered the following function definition: int add12(int) by the type alone, there are (232)232 possible implementations but this is a significantly smaller number than 8 Importantly, we know nothing more about this function from its type
SLIDE 24
Types
first let’s talk about types Suppose we encountered the following function definition: int add12(int) by the type alone, there are (232)232 possible implementations but this is a significantly smaller number than 8 Importantly, we know nothing more about this function from its type
SLIDE 25
Types
first let’s talk about types Suppose we encountered the following function definition: int add12(int) by the type alone, there are (232)232 possible implementations but this is a significantly smaller number than 8 Importantly, we know nothing more about this function from its type
SLIDE 26
Types
reading the code
We might form a suspicion that add12 adds twelve to its argument
int add12 (int)
SLIDE 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) =
And pat ourselves on the back, concluding, yes, this function adds twelve to its argument
SLIDE 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.
SLIDE 29
Types
another monomorphic example List <int > function(List <int >) adds 17 to every 11th element? drops every prime number?
SLIDE 30
Types
another monomorphic example List <int > function(List <int >) adds 17 to every 11th element? drops every prime number?
SLIDE 31 Parametricity
a polymorphic example <A> List <A> function(List <A>) this function returns elements in a list that always appear in the argument
- r it would not have compiled
Convince yourself of this. Commit to this statement.
SLIDE 32 Parametricity
a polymorphic example <A> List <A> function(List <A>) this function returns elements in a list that always appear in the argument
- r it would not have compiled
Convince yourself of this. Commit to this statement.
SLIDE 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
SLIDE 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
SLIDE 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?
SLIDE 36
Fast and Loose Reasoning
boolean even(int i) = ... We casually say, “This function returns one of two things.”
SLIDE 37
Fast and Loose Reasoning
boolean even(int i) = even(i) and we can discard this third possibility in analysis.
SLIDE 38 Fast and Loose Reasoning
many programming environments involve null exceptions Type-casing Type-casting Side-effects a universal equals/toString
aremember, FP has won, don’t forget
These must all be discarded. The penalty for this is zero.
SLIDE 39 The Limits of Parametricity
C# type signature
List <int > function(List <int >)
From the monomorphic type, what does this function do?
SLIDE 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?
SLIDE 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
SLIDE 42 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
SLIDE 43 The Limits of Parametricity
what does this function do?
function does not reverse.
- - >>> function []
- - []
- - prop > (function . function) x == x
- - prop > function (x ++ y) == (function y ++ function x)
function :: [a]
function = error "todo"
SLIDE 44 The Limits of Parametricity
what does this function do?
// csharp > function(List.empty) == List.empty // // csharp > x => function(function(x)) == x // // csharp > (x, y) => function(x.Append(y)) == function(y). Append(function(x)) List <A> <A>function(List <A> x) { . . . }
SLIDE 45
The Limits of Parametricity
another example (Haskell)
flatMap :: (a -> List b) -> List a -> List b flatMap = . . .
SLIDE 46 The Limits of Parametricity
another example (C#)
List <B> SelectMany <A, B>( this List <A>, Func <A, List <B>>) { . . . }
SLIDE 47 The Limits of Parametricity
another example
flatMap :: (a -> List b) -> List a -> List b flatMap = . . .
List <B> SelectMany <A, B>( this List <A>, Func <A, List <B>>) { . . . }
If the input list is empty, so is the result Every (b) in the result came from application of the given function
SLIDE 48
Once-inhabitance
sometimes tests are unnecessary
f :: a -> a
SLIDE 49
Once-inhabitance
sometimes tests are unnecessary
g :: Functor f => y -> f x -> f y
We already know that
λ> g "hi" [1,2,3] ["hi","hi","hi"]
SLIDE 50 Once-inhabitance
sometimes tests are almost unnecessary
h :: a -> a -> a
A h<A>(A a1 , A a2)
SLIDE 51 Once-inhabitance
sometimes tests are almost unnecessary
h :: a -> a -> a
A h<A>(A a1 , A a2)
λ> h 7 8 7
csharp > h(7, 8) 7
We now know precisely what this function does
SLIDE 52 Parametricity
non-trivial example
both :: (Applicative f, Bitraversable r) => (a -> f b) -> r a a -> f (r b b)
This function can only bitraverse 1 on ( r ) will work with Either at call site will work with (,) at call site will work with Const at call site but both cannot do anything specific to these data types This function can only (<*>) and pure on ( f ) will work with Maybe at call site will work with IO at call site e.g. call site can open network connections using both however both definitely does not open any network connections itself ( a ) and ( b ) might be anything may be Int at call site may be String at call site however both definitely does not perform any Int-specific operations 1(and derivatives)
SLIDE 53 Parametricity
non-trivial example
both :: (Applicative f, Bitraversable r) => (a -> f b) -> r a a -> f (r b b)
This function can only bitraverse 1 on ( r ) will work with Either at call site will work with (,) at call site will work with Const at call site but both cannot do anything specific to these data types This function can only (<*>) and pure on ( f ) will work with Maybe at call site will work with IO at call site e.g. call site can open network connections using both however both definitely does not open any network connections itself ( a ) and ( b ) might be anything may be Int at call site may be String at call site however both definitely does not perform any Int-specific operations 1(and derivatives)
SLIDE 54 Parametricity
non-trivial example
both :: (Applicative f, Bitraversable r) => (a -> f b) -> r a a -> f (r b b)
This function can only bitraverse 1 on ( r ) will work with Either at call site will work with (,) at call site will work with Const at call site but both cannot do anything specific to these data types This function can only (<*>) and pure on ( f ) will work with Maybe at call site will work with IO at call site e.g. call site can open network connections using both however both definitely does not open any network connections itself ( a ) and ( b ) might be anything may be Int at call site may be String at call site however both definitely does not perform any Int-specific operations 1(and derivatives)
SLIDE 55
Parametricity
and on it goes
(<.) :: Indexable i p => (Indexed i s t -> r) -> ((a -> b) -> s -> t) -> p a b -> r
SLIDE 56
Code readability
hang on a minute Did you just work out what that code did, by using types?
SLIDE 57
Code readability
Yes, yes I did Types are documentation
SLIDE 58
Code readability
Types are documentation reliable and dense documentation
SLIDE 59
Code readability
Reliable documentation like comments, except condensed, machine-checked, without the fluff and falsehoods
SLIDE 60
Parametricity, practical goals
typical software development goals can fix bugs independently of the possibility of creating more can introduce features without adversely affecting others can have hundreds of projects requiring zero maintenance can reliably and efficiently determine what goal existing code achieves avoid endless tail-chasing that prevails in corporate dev
SLIDE 61
Parametricity, practical goals
typical software development goals can fix bugs independently of the possibility of creating more can introduce features without adversely affecting others can have hundreds of projects requiring zero maintenance can reliably and efficiently determine what goal existing code achieves avoid endless tail-chasing that prevails in corporate dev
SLIDE 62
Parametricity, practical goals
typical software development goals can fix bugs independently of the possibility of creating more can introduce features without adversely affecting others can have hundreds of projects requiring zero maintenance can reliably and efficiently determine what goal existing code achieves avoid endless tail-chasing that prevails in corporate dev
SLIDE 63
Parametricity, practical goals
typical software development goals can fix bugs independently of the possibility of creating more can introduce features without adversely affecting others can have hundreds of projects requiring zero maintenance can reliably and efficiently determine what goal existing code achieves avoid endless tail-chasing that prevails in corporate dev
SLIDE 64
Parametricity, practical goals
typical software development goals can fix bugs independently of the possibility of creating more can introduce features without adversely affecting others can have hundreds of projects requiring zero maintenance can reliably and efficiently determine what goal existing code achieves avoid endless tail-chasing that prevails in corporate dev
SLIDE 65
Parametricity, practical goals
anti-goals The Marine Corps’ F-35B aircraft are being delivered with Block 2B software, which Gilmore said has “hundreds of unresolved deficiencies.” And those problems have compounded in Block 3F software. That’s because the first round of Block 3 was created by “re-hosting the immature Block 2B software. . . into new processors to create Block 3i,” the initial release for the code, Gilmore noted. This led to “avionics instabilities and other new problems, resulting in poor performance during developmental testing.” DO NOT WANT TO BE HERE
SLIDE 66
Parametricity, practical goals
common questions pertaining to goals what tools assist in achieving these goals? what tools do we know do not achieve these goals?
SLIDE 67
Parametricity, practical goals
common snarks distracting from goals what’s it like for you haskell programmers in the ivory tower? why do you hate programming language environment X? “but all tools have a job for which they are suited” why are you so fundamentalist? why are you so extweemust?
SLIDE 68
Parametricity, practical goals
goals “Here is programming language environment X, which undermines your capability to exploits types and parametricity.” for what benefit?
SLIDE 69
Parametricity, practical goals
goals Propose to forgo these practical tools, and a reasonable compromise must be substituted, else dismissal
SLIDE 70
Parametricity, practical goals
goals You may one day be persuaded that this is an unreasonable approach to your objective. IT’S A MIND TRAP
SLIDE 71
Parametricity, practical goals
goals Parametricity is for winners who achieve their goals. Let’s all be winners. Spread the polymorphic love.
SLIDE 72
References
Nils Anders Danielsson, John Hughes, Patrik Jansson, and Jeremy Gibbons, Fast and loose reasoning is morally correct, ACM SIGPLAN Notices, vol. 41, ACM, 2006, pp. 206–217. Philip Wadler, Theorems for free!, Proceedings of the fourth international conference on Functional programming languages and computer architecture, ACM, 1989, pp. 347–359.