![Page 1: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/1.jpg)
Functional Programming, Parametricity, Types
Essential Tools of Programming
YOW! West 2016
Tony Morris
![Page 2: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/2.jpg)
The Premise
the following are essential to programming success. . .adherence to the functional programming thesisparametricity (and types)
![Page 3: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/3.jpg)
The Premise
the following are essential to programming success. . .adherence to the functional programming thesisparametricity (and types)
![Page 4: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/4.jpg)
The Parametricity Trick
parametricity will only work with. . .an inveterate exploitation of the functional programmingthesislet’s revisit functional programming
![Page 5: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/5.jpg)
The Parametricity Trick
parametricity will only work with. . .an inveterate exploitation of the functional programmingthesislet’s revisit functional programming
![Page 6: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/6.jpg)
Reminder
so what is functional programming?a means of programming by which expressions are referentiallytransparent.but what is referential transparency?
![Page 7: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/7.jpg)
Reminder
so what is functional programming?a means of programming by which expressions are referentiallytransparent.but what is referential transparency?
![Page 8: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/8.jpg)
Referential Transparency
referential transparency is a decidable property of programexpressionsfunctions provide programmers a tool to create referentiallytransparent expressions
The Test for Referential TransparencyAn expression expr is referentially transparent if in a program p,all occurrences of expr in p can be replaced by an assignment toexpr without effecting an observable change in p.
![Page 9: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/9.jpg)
Referential Transparency
referential transparency is a decidable property of programexpressionsfunctions provide programmers a tool to create referentiallytransparent expressions
The Test for Referential TransparencyAn expression expr is referentially transparent if in a program p,all occurrences of expr in p can be replaced by an assignment toexpr without effecting an observable change in p.
![Page 10: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/10.jpg)
Referential Transparency
referential transparency is a decidable property of programexpressionsfunctions provide programmers a tool to create referentiallytransparent expressions
The Test for Referential TransparencyAn expression expr is referentially transparent if in a program p,all occurrences of expr in p can be replaced by an assignment toexpr without effecting an observable change in p.
![Page 11: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/11.jpg)
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?
![Page 12: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/12.jpg)
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?
![Page 13: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/13.jpg)
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?
![Page 14: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/14.jpg)
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?
![Page 15: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/15.jpg)
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?
![Page 16: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/16.jpg)
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?
![Page 17: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/17.jpg)
Functional Programming
FP is a commitment to preserving referential transparencyQuite a while ago, FP won by not-a-little-bit. Moving on.we use tools to achieve this commitmentparametricity is one such tool with high reward
![Page 18: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/18.jpg)
Functional Programming
FP is a commitment to preserving referential transparencyQuite a while ago, FP won by not-a-little-bit. Moving on.we use tools to achieve this commitmentparametricity is one such tool with high reward
![Page 19: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/19.jpg)
Functional Programming
FP is a commitment to preserving referential transparencyQuite a while ago, FP won by not-a-little-bit. Moving on.we use tools to achieve this commitmentparametricity is one such tool with high reward
![Page 20: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/20.jpg)
Functional Programming
FP is a commitment to preserving referential transparencyQuite a while ago, FP won by not-a-little-bit. Moving on.we use tools to achieve this commitmentparametricity is one such tool with high reward
![Page 21: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/21.jpg)
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 theresults to carry over to non-total (partial) languages. Wejustify such reasoning.
![Page 22: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/22.jpg)
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 notto let me see the function’s definition. I will tell you atheorem that the function satisfies.
The purpose of this paper is to explain the trick.
![Page 23: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/23.jpg)
Types
first let’s talk about typesSuppose we encountered the following function definition:
int add12(int)
by the type alone, there are (232)232 possible implementationsbut this is a significantly smaller number than 8
Importantly, we know nothing more about this function fromits type
![Page 24: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/24.jpg)
Types
first let’s talk about typesSuppose we encountered the following function definition:
int add12(int)
by the type alone, there are (232)232 possible implementationsbut this is a significantly smaller number than 8
Importantly, we know nothing more about this function fromits type
![Page 25: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/25.jpg)
Types
first let’s talk about typesSuppose we encountered the following function definition:
int add12(int)
by the type alone, there are (232)232 possible implementationsbut this is a significantly smaller number than 8
Importantly, we know nothing more about this function fromits type
![Page 26: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/26.jpg)
Typesreading the code
We might form a suspicion that add12 adds twelve to its argumentint add12 (int)
![Page 27: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/27.jpg)
Types
So we write some speculative tests to relieve our anxiety:
add12 (0) = 12add12 (5) = 17add12 (-5) = 7add12 (223) = 235add12 (5096) = 5104add12 (2914578) = 29145590add12 ( -2914578) = -29145566
And pat ourselves on the back, concluding, yes, this function addstwelve to its argument
![Page 28: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/28.jpg)
TypesWoops!
and then
def add12(n: Int ): Int =if(n < 8000000) n + 12else n * 7
We need to narrow down the potential propositions about whatthis function does not do.
![Page 29: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/29.jpg)
Types
another monomorphic example
List <int > function (List <int >)
adds 17 to every 11th element?drops every prime number?
![Page 30: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/30.jpg)
Types
another monomorphic example
List <int > function (List <int >)
adds 17 to every 11th element?drops every prime number?
![Page 31: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/31.jpg)
Parametricity
a polymorphic example
<A> List <A> function (List <A >)
this function returns elements in a list that always appear inthe argumentor it would not have compiledConvince yourself of this. Commit to this statement.
![Page 32: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/32.jpg)
Parametricity
a polymorphic example
<A> List <A> function (List <A >)
this function returns elements in a list that always appear inthe argumentor it would not have compiledConvince yourself of this. Commit to this statement.
![Page 33: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/33.jpg)
Parametricity
the goala significant number of possible things that this function doesare eliminated, by no expenditure of efforttheorems about this function can be reliably constructed
![Page 34: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/34.jpg)
Parametricity
the goala significant number of possible things that this function doesare eliminated, by no expenditure of efforttheorems about this function can be reliably constructed
![Page 35: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/35.jpg)
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 theresults to carry over to non-total (partial) languages. Wejustify such reasoning.
but what does this mean exactly?
![Page 36: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/36.jpg)
Fast and Loose Reasoning
boolean even(int i) =...
We casually say, “This function returns one of two things.”
![Page 37: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/37.jpg)
Fast and Loose Reasoning
boolean even(int i) =even(i)
and we can discard this third possibility in analysis.
![Page 38: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/38.jpg)
Fast and Loose Reasoning
many programming environments involvenull
exceptionsType-casingType-castingSide-effects a
universal equals/toString
aremember, FP has won, don’t forget
These must all be discarded. The penalty for this is zero.
![Page 39: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/39.jpg)
The Limits of Parametricity
C# type signature
List <int > function (List <int >)
From the monomorphic type, what does this function do?
![Page 40: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/40.jpg)
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?
![Page 41: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/41.jpg)
The Limits of Parametricity
Do we?write comments above the function/* This function twiddles the database to twoddle out the twip twop */
ORwrite true testable statements about the function
![Page 42: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/42.jpg)
The Limits of Parametricity
Do we?write comments above the function/* This function twiddles the database to twoddle out the twip twop */
ORwrite true testable statements about the function
![Page 43: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/43.jpg)
The Limits of Parametricity
what does this function do?
-- | This function does not reverse .---- >>> function []-- []---- prop > ( function . function ) x == x---- prop > function (x ++ y) == ( function y ++ function x)function ::
[a]-> [a]
function =error "todo"
![Page 44: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/44.jpg)
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) {
. . .}
![Page 45: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/45.jpg)
The Limits of Parametricity
another example (Haskell)
flatMap :: (a -> List b) -> List a -> List bflatMap = . . .
![Page 46: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/46.jpg)
The Limits of Parametricity
another example (C#)
List <B> SelectMany <A, B >( this List <A>, Func <A, List <B>>) {. . .
}
![Page 47: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/47.jpg)
The Limits of Parametricity
another example
flatMap :: (a -> List b) -> List a -> List bflatMap = . . .
List <B> SelectMany <A, B >( this List <A>, Func <A, List <B>>) {. . .
}
If the input list is empty, so is the resultEvery (b) in the result came from application of the givenfunction
![Page 48: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/48.jpg)
Once-inhabitance
sometimes tests are unnecessary
f :: a -> a
![Page 49: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/49.jpg)
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"]
![Page 50: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/50.jpg)
Once-inhabitance
sometimes tests are almost unnecessary
h :: a -> a -> a
A h<A >(A a1 , A a2)
![Page 51: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/51.jpg)
Once-inhabitance
sometimes tests are almost unnecessary
h :: a -> a -> a
A h<A >(A a1 , A a2)
λ> h 7 87
csharp > h(7, 8)7
We now know precisely what this function does
![Page 52: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/52.jpg)
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 sitewill work with (,) at call sitewill work with Const at call sitebut both cannot do anything specific to these data types
This function can only (<*>) and pure on ( f )will work with Maybe at call sitewill work with IO at call sitee.g. call site can open network connections using bothhowever both definitely does not open any network connections itself
( a ) and ( b ) might be anythingmay be Int at call sitemay be String at call sitehowever both definitely does not perform any Int-specific operations
1(and derivatives)
![Page 53: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/53.jpg)
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 sitewill work with (,) at call sitewill work with Const at call sitebut both cannot do anything specific to these data types
This function can only (<*>) and pure on ( f )will work with Maybe at call sitewill work with IO at call sitee.g. call site can open network connections using bothhowever both definitely does not open any network connections itself
( a ) and ( b ) might be anythingmay be Int at call sitemay be String at call sitehowever both definitely does not perform any Int-specific operations
1(and derivatives)
![Page 54: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/54.jpg)
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 sitewill work with (,) at call sitewill work with Const at call sitebut both cannot do anything specific to these data types
This function can only (<*>) and pure on ( f )will work with Maybe at call sitewill work with IO at call sitee.g. call site can open network connections using bothhowever both definitely does not open any network connections itself
( a ) and ( b ) might be anythingmay be Int at call sitemay be String at call sitehowever both definitely does not perform any Int-specific operations
1(and derivatives)
![Page 55: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/55.jpg)
Parametricity
and on it goes
(<.) ::Indexable i p =>( Indexed i s t -> r) -> ((a -> b) -> s -> t) -> p a b -> r
![Page 56: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/56.jpg)
Code readability
hang on a minuteDid you just work out what that code did, by using types?
![Page 57: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/57.jpg)
Code readability
Yes, yes I didTypes are documentation
![Page 58: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/58.jpg)
Code readability
Types are documentationreliable and dense documentation
![Page 59: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/59.jpg)
Code readability
Reliable documentationlike comments, except condensed, machine-checked, without thefluff and falsehoods
![Page 60: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/60.jpg)
Parametricity, practical goals
typical software development goalscan fix bugs independently of the possibility of creating morecan introduce features without adversely affecting otherscan have hundreds of projects requiring zero maintenancecan reliably and efficiently determine what goal existing codeachievesavoid endless tail-chasing that prevails in corporate dev
![Page 61: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/61.jpg)
Parametricity, practical goals
typical software development goalscan fix bugs independently of the possibility of creating morecan introduce features without adversely affecting otherscan have hundreds of projects requiring zero maintenancecan reliably and efficiently determine what goal existing codeachievesavoid endless tail-chasing that prevails in corporate dev
![Page 62: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/62.jpg)
Parametricity, practical goals
typical software development goalscan fix bugs independently of the possibility of creating morecan introduce features without adversely affecting otherscan have hundreds of projects requiring zero maintenancecan reliably and efficiently determine what goal existing codeachievesavoid endless tail-chasing that prevails in corporate dev
![Page 63: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/63.jpg)
Parametricity, practical goals
typical software development goalscan fix bugs independently of the possibility of creating morecan introduce features without adversely affecting otherscan have hundreds of projects requiring zero maintenancecan reliably and efficiently determine what goal existing codeachievesavoid endless tail-chasing that prevails in corporate dev
![Page 64: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/64.jpg)
Parametricity, practical goals
typical software development goalscan fix bugs independently of the possibility of creating morecan introduce features without adversely affecting otherscan have hundreds of projects requiring zero maintenancecan reliably and efficiently determine what goal existing codeachievesavoid endless tail-chasing that prevails in corporate dev
![Page 65: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/65.jpg)
Parametricity, practical goals
anti-goalsThe Marine Corps’ F-35B aircraft are being delivered
with Block 2B software, which Gilmore said has“hundreds of unresolved deficiencies.” And thoseproblems have compounded in Block 3F software. That’sbecause the first round of Block 3 was created by“re-hosting the immature Block 2B software. . . into newprocessors to create Block 3i,” the initial release for thecode, Gilmore noted. This led to “avionics instabilitiesand other new problems, resulting in poor performanceduring developmental testing.”
DO NOT WANT TO BE HERE
![Page 66: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/66.jpg)
Parametricity, practical goals
common questions pertaining to goalswhat tools assist in achieving these goals?what tools do we know do not achieve these goals?
![Page 67: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/67.jpg)
Parametricity, practical goals
common snarks distracting from goalswhat’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?
![Page 68: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/68.jpg)
Parametricity, practical goals
goals“Here is programming language environment X, which underminesyour capability to exploits types and parametricity.”
for what benefit?
![Page 69: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/69.jpg)
Parametricity, practical goals
goalsPropose to forgo these practical tools, and a reasonablecompromise must be substituted, else dismissal
![Page 70: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/70.jpg)
Parametricity, practical goals
goalsYou may one day be persuaded that this is an unreasonableapproach to your objective.
IT’S A MIND TRAP
![Page 71: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/71.jpg)
Parametricity, practical goals
goalsParametricity is for winners who achieve their goals.Let’s all be winners.
Spread the polymorphic love.
![Page 72: Functional Programming, Parametricity, Typesdata.tmorris.net › talks › yow-west-2016 › 1d388b6263e7... · 2016-05-04 · Functional Programming, Parametricity, Types Essential](https://reader036.vdocuments.net/reader036/viewer/2022070821/5f2283954dafaf73ce1e7a11/html5/thumbnails/72.jpg)
References
Nils Anders Danielsson, John Hughes, Patrik Jansson, andJeremy 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 fourthinternational conference on Functional programming languagesand computer architecture, ACM, 1989, pp. 347–359.