Free Theorems — The Basics Janis Voigtl¨ ander Technische Universit¨ at Dresden January 6, 2006
Outline Example in Haskell Parametric polymorphism Polymorphic lambda calculus Parametricity theorem Back to Haskell 2
Haskell Example: filter :: ∀ α. ( α → Bool ) → [ α ] → [ α ] filter p [] = [] filter p ( x : xs ) = if p x then x : filter p xs else filter p xs 3
Haskell Example: filter :: ∀ α. ( α → Bool ) → [ α ] → [ α ] filter p [] = [] filter p ( x : xs ) = if p x then x : filter p xs else filter p xs Claim: filter p ( map h l ) = map h ( filter ( p ◦ h ) l ) (1) Can be proved by induction on l , using the definition of filter . 3
Haskell Example: Theorems for free! [Wadler 1989] filter :: ∀ α. ( α → Bool ) → [ α ] → [ α ] Claim: filter p ( map h l ) = map h ( filter ( p ◦ h ) l ) (1) Can be derived from the parametric polymorphic type of filter ! 3
Haskell Example: Theorems for free! [Wadler 1989] filter :: ∀ α. ( α → Bool ) → [ α ] → [ α ] Claim: filter p ( map h l ) = map h ( filter ( p ◦ h ) l ) (1) Can be derived from the parametric polymorphic type of filter ! Where is the magic? Where is the induction? 3
Parametric Polymorphism, Intuitively ◮ filter :: ∀ α. ( α → Bool ) → [ α ] → [ α ] must work uniformly for every instantiation of α . 4
Parametric Polymorphism, Intuitively ◮ filter :: ∀ α. ( α → Bool ) → [ α ] → [ α ] must work uniformly for every instantiation of α . ◮ The output list can only contain elements from the input list l . 4
Parametric Polymorphism, Intuitively ◮ filter :: ∀ α. ( α → Bool ) → [ α ] → [ α ] must work uniformly for every instantiation of α . ◮ The output list can only contain elements from the input list l . ◮ Which, and in which order/multiplicity, can only be decided based on l and the input predicate p . 4
Parametric Polymorphism, Intuitively ◮ filter :: ∀ α. ( α → Bool ) → [ α ] → [ α ] must work uniformly for every instantiation of α . ◮ The output list can only contain elements from the input list l . ◮ Which, and in which order/multiplicity, can only be decided based on l and the input predicate p . ◮ The only means for this decision are to inspect the length of l and to check the outcome of p on its elements. 4
Parametric Polymorphism, Intuitively ◮ filter :: ∀ α. ( α → Bool ) → [ α ] → [ α ] must work uniformly for every instantiation of α . ◮ The output list can only contain elements from the input list l . ◮ Which, and in which order/multiplicity, can only be decided based on l and the input predicate p . ◮ The only means for this decision are to inspect the length of l and to check the outcome of p on its elements. ◮ The lists ( map h l ) and l always have equal length. 4
Parametric Polymorphism, Intuitively ◮ filter :: ∀ α. ( α → Bool ) → [ α ] → [ α ] must work uniformly for every instantiation of α . ◮ The output list can only contain elements from the input list l . ◮ Which, and in which order/multiplicity, can only be decided based on l and the input predicate p . ◮ The only means for this decision are to inspect the length of l and to check the outcome of p on its elements. ◮ The lists ( map h l ) and l always have equal length. ◮ Applying p to an element of ( map h l ) always has the same outcome as applying ( p ◦ h ) to the corresponding element of l . 4
Parametric Polymorphism, Intuitively ◮ filter :: ∀ α. ( α → Bool ) → [ α ] → [ α ] must work uniformly for every instantiation of α . ◮ The output list can only contain elements from the input list l . ◮ Which, and in which order/multiplicity, can only be decided based on l and the input predicate p . ◮ The only means for this decision are to inspect the length of l and to check the outcome of p on its elements. ◮ The lists ( map h l ) and l always have equal length. ◮ Applying p to an element of ( map h l ) always has the same outcome as applying ( p ◦ h ) to the corresponding element of l . ◮ filter with p always chooses “the same” elements from ( map h l ) for output as does filter with ( p ◦ h ) from l , 4
Parametric Polymorphism, Intuitively ◮ filter :: ∀ α. ( α → Bool ) → [ α ] → [ α ] must work uniformly for every instantiation of α . ◮ The output list can only contain elements from the input list l . ◮ Which, and in which order/multiplicity, can only be decided based on l and the input predicate p . ◮ The only means for this decision are to inspect the length of l and to check the outcome of p on its elements. ◮ The lists ( map h l ) and l always have equal length. ◮ Applying p to an element of ( map h l ) always has the same outcome as applying ( p ◦ h ) to the corresponding element of l . ◮ filter with p always chooses “the same” elements from ( map h l ) for output as does filter with ( p ◦ h ) from l , except that it outputs their images under h . 4
Parametric Polymorphism, Intuitively ◮ filter :: ∀ α. ( α → Bool ) → [ α ] → [ α ] must work uniformly for every instantiation of α . ◮ The output list can only contain elements from the input list l . ◮ Which, and in which order/multiplicity, can only be decided based on l and the input predicate p . ◮ The only means for this decision are to inspect the length of l and to check the outcome of p on its elements. ◮ The lists ( map h l ) and l always have equal length. ◮ Applying p to an element of ( map h l ) always has the same outcome as applying ( p ◦ h ) to the corresponding element of l . ◮ filter with p always chooses “the same” elements from ( map h l ) for output as does filter with ( p ◦ h ) from l , except that it outputs their images under h . ◮ ( filter p ( map h l )) is equivalent to ( map h ( filter ( p ◦ h ) l )). 4
Parametric Polymorphism, Intuitively ◮ filter :: ∀ α. ( α → Bool ) → [ α ] → [ α ] must work uniformly for every instantiation of α . ◮ The output list can only contain elements from the input list l . ◮ Which, and in which order/multiplicity, can only be decided based on l and the input predicate p . ◮ The only means for this decision are to inspect the length of l and to check the outcome of p on its elements. ◮ The lists ( map h l ) and l always have equal length. ◮ Applying p to an element of ( map h l ) always has the same outcome as applying ( p ◦ h ) to the corresponding element of l . ◮ filter with p always chooses “the same” elements from ( map h l ) for output as does filter with ( p ◦ h ) from l , except that it outputs their images under h . ◮ ( filter p ( map h l )) is equivalent to ( map h ( filter ( p ◦ h ) l )). ◮ That is what we wanted to prove! 4
Parametric Polymorphism, More Formally Question: What functions are in ∀ α. ( α → Bool ) → [ α ] → [ α ] ? Approach: Give denotations of types as sets. 5
Parametric Polymorphism, More Formally Question: What functions are in ∀ α. ( α → Bool ) → [ α ] → [ α ] ? Approach: Give denotations of types as sets. [ [ Bool ] ] θ = { True , False } = B [ [ Nat ] ] θ = { 0 , 1 , 2 , . . . } = N 5
Parametric Polymorphism, More Formally Question: What functions are in ∀ α. ( α → Bool ) → [ α ] → [ α ] ? Approach: Give denotations of types as sets. [ [ Bool ] ] θ = { True , False } = B [ [ Nat ] ] θ = { 0 , 1 , 2 , . . . } = N [ [( τ 1 , τ 2 )] ] θ = [ [ τ 1 ] ] θ × [ [ τ 2 ] ] θ [ [[ τ ]] ] θ = { [ x 1 , . . . , x n ] | n ≥ 0 , x i ∈ [ [ τ ] ] θ } 5
Parametric Polymorphism, More Formally Question: What functions are in ∀ α. ( α → Bool ) → [ α ] → [ α ] ? Approach: Give denotations of types as sets. [ [ Bool ] ] θ = { True , False } = B [ [ Nat ] ] θ = { 0 , 1 , 2 , . . . } = N [ [( τ 1 , τ 2 )] ] θ = [ [ τ 1 ] ] θ × [ [ τ 2 ] ] θ [ [[ τ ]] ] θ = { [ x 1 , . . . , x n ] | n ≥ 0 , x i ∈ [ [ τ ] ] θ } [ [ τ 1 → τ 2 ] ] θ = { f : [ [ τ 1 ] ] θ → [ [ τ 2 ] ] θ } 5
Parametric Polymorphism, More Formally Question: What functions are in ∀ α. ( α → Bool ) → [ α ] → [ α ] ? Approach: Give denotations of types as sets. [ [ Bool ] ] θ = { True , False } = B [ [ Nat ] ] θ = { 0 , 1 , 2 , . . . } = N [ [( τ 1 , τ 2 )] ] θ = [ [ τ 1 ] ] θ × [ [ τ 2 ] ] θ [ [[ τ ]] ] θ = { [ x 1 , . . . , x n ] | n ≥ 0 , x i ∈ [ [ τ ] ] θ } [ [ τ 1 → τ 2 ] ] θ = { f : [ [ τ 1 ] ] θ → [ [ τ 2 ] ] θ } [ [ ∀ α. τ ] ] θ = ? 5
Parametric Polymorphism, More Formally Question: What functions are in ∀ α. ( α → Bool ) → [ α ] → [ α ] ? Approach: Give denotations of types as sets. [ [ Bool ] ] θ = { True , False } = B [ [ Nat ] ] θ = { 0 , 1 , 2 , . . . } = N [ [( τ 1 , τ 2 )] ] θ = [ [ τ 1 ] ] θ × [ [ τ 2 ] ] θ [ [[ τ ]] ] θ = { [ x 1 , . . . , x n ] | n ≥ 0 , x i ∈ [ [ τ ] ] θ } [ [ τ 1 → τ 2 ] ] θ = { f : [ [ τ 1 ] ] θ → [ [ τ 2 ] ] θ } [ [ ∀ α. τ ] ] θ = ? ◮ g ∈ [ [ ∀ α. τ ] ] θ should be a “collection” of values: for every type τ ′ , there is an instance of type τ [ τ ′ /α ]. 5
Recommend
More recommend