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

functional programming parametricity types
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Functional Programming, Parametricity, Types

Essential Tools of Programming YOW! West 2016 Tony Morris

slide-2
SLIDE 2

The Premise

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

slide-3
SLIDE 3

The Premise

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

slide-4
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
SLIDE 26

Types

reading the code

We might form a suspicion that add12 adds twelve to its argument

int add12 (int)

slide-27
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) =

  • 29145566

And pat ourselves on the back, concluding, yes, this function adds twelve to its argument

slide-28
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
SLIDE 29

Types

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

slide-30
SLIDE 30

Types

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

slide-31
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
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
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
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
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
SLIDE 36

Fast and Loose Reasoning

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

slide-37
SLIDE 37

Fast and Loose Reasoning

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

slide-38
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
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
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
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
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
SLIDE 43

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"

slide-44
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
SLIDE 45

The Limits of Parametricity

another example (Haskell)

flatMap :: (a -> List b) -> List a -> List b flatMap = . . .

slide-46
SLIDE 46

The Limits of Parametricity

another example (C#)

List <B> SelectMany <A, B>( this List <A>, Func <A, List <B>>) { . . . }

slide-47
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
SLIDE 48

Once-inhabitance

sometimes tests are unnecessary

f :: a -> a

slide-49
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
SLIDE 50

Once-inhabitance

sometimes tests are almost unnecessary

h :: a -> a -> a

A h<A>(A a1 , A a2)

slide-51
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
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
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
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
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
SLIDE 56

Code readability

hang on a minute Did you just work out what that code did, by using types?

slide-57
SLIDE 57

Code readability

Yes, yes I did Types are documentation

slide-58
SLIDE 58

Code readability

Types are documentation reliable and dense documentation

slide-59
SLIDE 59

Code readability

Reliable documentation like comments, except condensed, machine-checked, without the fluff and falsehoods

slide-60
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
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
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
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
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
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
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
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
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
SLIDE 69

Parametricity, practical goals

goals Propose to forgo these practical tools, and a reasonable compromise must be substituted, else dismissal

slide-70
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
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
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.