Recap: Logic Typed Lambda Calculus Algebraic Type Isomorphism Polymorphism and Parametricity Software System Design and Implementation Theory of Types Liam O’Connor University of Edinburgh LFCS (and UNSW) Term 2 2020 1
Recap: Logic Typed Lambda Calculus Algebraic Type Isomorphism Polymorphism and Parametricity Natural Deduction Logic We can specify a logical system as a deductive system by providing a set of rules and axioms that describe how to prove various connectives. Each connective typically has introduction and elimination rules. 2
Recap: Logic Typed Lambda Calculus Algebraic Type Isomorphism Polymorphism and Parametricity Natural Deduction Logic We can specify a logical system as a deductive system by providing a set of rules and axioms that describe how to prove various connectives. Each connective typically has introduction and elimination rules. For example, to prove an implication A → B holds, we must show that B holds assuming A . This introduction rule is written as: A , Γ ⊢ B derivability Γ ⊢ A → B → -I (if the top, then the bottom) entailment (assuming the left, we can prove the right) 3
Recap: Logic Typed Lambda Calculus Algebraic Type Isomorphism Polymorphism and Parametricity More rules Implication also has an elimination rule, that is also called modus ponens : Γ ⊢ A → B Γ ⊢ A → -E Γ ⊢ B Conjunction (and) has an introduction rule that follows our intuition: Γ ⊢ A Γ ⊢ B ∧ -I Γ ⊢ A ∧ B It has two elimination rules: Γ ⊢ A ∧ B Γ ⊢ A ∧ B ∧ -E 1 ∧ -E 2 Γ ⊢ A Γ ⊢ B 4
Recap: Logic Typed Lambda Calculus Algebraic Type Isomorphism Polymorphism and Parametricity More rules Disjunction (or) has two introduction rules: Γ ⊢ A Γ ⊢ B Γ ⊢ A ∨ B ∨ -I 1 Γ ⊢ A ∨ B ∨ -I 2 Disjunction elimination is a little unusual: Γ ⊢ A ∨ B A , Γ ⊢ P B , Γ ⊢ P ∨ -E Γ ⊢ P The true literal, written ⊤ , has only an introduction: Γ ⊢ ⊤ And false, written ⊥ , has just elimination ( ex falso quodlibet ): Γ ⊢ ⊥ Γ ⊢ P 5
Recap: Logic Typed Lambda Calculus Algebraic Type Isomorphism Polymorphism and Parametricity Example Proofs Example Prove: A ∧ B → B ∧ A 6
Recap: Logic Typed Lambda Calculus Algebraic Type Isomorphism Polymorphism and Parametricity Example Proofs Example Prove: A ∧ B → B ∧ A A ∨ ⊥ → A 7
Recap: Logic Typed Lambda Calculus Algebraic Type Isomorphism Polymorphism and Parametricity Example Proofs Example Prove: A ∧ B → B ∧ A A ∨ ⊥ → A What would negation be equivalent to? 8
Recap: Logic Typed Lambda Calculus Algebraic Type Isomorphism Polymorphism and Parametricity Example Proofs Example Prove: A ∧ B → B ∧ A A ∨ ⊥ → A What would negation be equivalent to? Typically we just define ¬ A ≡ ( A → ⊥ ) . Example Prove: A → ( ¬¬ A ) 9
Recap: Logic Typed Lambda Calculus Algebraic Type Isomorphism Polymorphism and Parametricity Example Proofs Example Prove: A ∧ B → B ∧ A A ∨ ⊥ → A What would negation be equivalent to? Typically we just define ¬ A ≡ ( A → ⊥ ) . Example Prove: A → ( ¬¬ A ) ( ¬¬ A ) → A 10
Recap: Logic Typed Lambda Calculus Algebraic Type Isomorphism Polymorphism and Parametricity Example Proofs Example Prove: A ∧ B → B ∧ A A ∨ ⊥ → A What would negation be equivalent to? Typically we just define ¬ A ≡ ( A → ⊥ ) . Example Prove: A → ( ¬¬ A ) ( ¬¬ A ) → A We get stuck here! 11
Recap: Logic Typed Lambda Calculus Algebraic Type Isomorphism Polymorphism and Parametricity Constructive Logic The logic we have expressed so far does not admit the law of the excluded middle: P ∨ ¬ P Or the equivalent double negation elimination: ( ¬¬ P ) → P This is because it is a constructive logic that does not allow us to do proof by contradiction. 12
Recap: Logic Typed Lambda Calculus Algebraic Type Isomorphism Polymorphism and Parametricity Boiling Haskell Down The theoretical properties we will describe also apply to Haskell, but we need a smaller language for demonstration purposes. No user-defined types, just a small set of built-in types. No polymorphism (type variables) Just lambdas ( λ x . e ) to define functions or bind variables. This language is a very minimal functional language, called the simply typed lambda calculus, originally due to Alonzo Church. Our small set of built-in types are intended to be enough to express most of the data types we would otherwise define. We are going to use logical inference rules to specify how expressions are given types ( typing rules ). 13
Recap: Logic Typed Lambda Calculus Algebraic Type Isomorphism Polymorphism and Parametricity Function Types We create values of a function type A → B using lambda expressions: x :: A , Γ ⊢ e :: B Γ ⊢ λ x . e :: A → B The typing rule for function application is as follows: Γ ⊢ e 1 :: A → B Γ ⊢ e 2 :: A Γ ⊢ e 1 e 2 :: B What other types would be needed? 14
Recap: Logic Typed Lambda Calculus Algebraic Type Isomorphism Polymorphism and Parametricity Composite Data Types In addition to functions, most programming languages feature ways to compose types together to produce new types, such as: 15
Recap: Logic Typed Lambda Calculus Algebraic Type Isomorphism Polymorphism and Parametricity Composite Data Types In addition to functions, most programming languages feature ways to compose types together to produce new types, such as: Tuples Structs Records
Recap: Logic Typed Lambda Calculus Algebraic Type Isomorphism Polymorphism and Parametricity Composite Data Types In addition to functions, most programming languages feature ways to compose types together to produce new types, such as: Classes Tuples Structs Records
Recap: Logic Typed Lambda Calculus Algebraic Type Isomorphism Polymorphism and Parametricity Composite Data Types In addition to functions, most programming languages feature ways to compose types together to produce new types, such as: Classes Tuples Structs Unions Records 18
Recap: Logic Typed Lambda Calculus Algebraic Type Isomorphism Polymorphism and Parametricity Combining values conjunctively We want to store two things in one value. (might want to use non-compact slides for this one) Haskell Tuples type Point = (Float, Float) midpoint (x1,y1) (x2,y2) = ((x1+x2)/2, (y1+y2)/2) 19
Recap: Logic Typed Lambda Calculus Algebraic Type Isomorphism Polymorphism and Parametricity Combining values conjunctively We want to store two things in one value. (might want to use non-compact slides for this one) Haskell Datatypes data Point = Pnt { x :: Float , y :: Float Haskell Tuples } type Point = (Float, Float) midpoint (Pnt x1 y1) (Pnt x2 y2) midpoint (x1,y1) (x2,y2) = ((x1+x2)/2, (y1+y2)/2) = ((x1+x2)/2, (y1+y2)/2) midpoint' p1 p2 = = ((x p1 + x p2) / 2, (y p1 + y p2) / 2) 20
Recap: Logic Typed Lambda Calculus Algebraic Type Isomorphism Polymorphism and Parametricity Combining values conjunctively We want to store two things in one value. (might want to use non-compact slides for this one) Haskell Datatypes C Structs data Point = typedef struct point { Pnt { x :: Float float x; , y :: Float Haskell Tuples float y; } type Point = (Float, Float) } point; point midPoint (point p1, point p2) { midpoint (Pnt x1 y1) (Pnt x2 y2) midpoint (x1,y1) (x2,y2) point mid; = ((x1+x2)/2, (y1+y2)/2) = ((x1+x2)/2, (y1+y2)/2) mid.x = (p1.x + p2.x) / 2.0; mid.y = (p2.y + p2.y) / 2.0; midpoint' p1 p2 = return mid; = ((x p1 + x p2) / 2, } (y p1 + y p2) / 2) 21
Recap: Logic Typed Lambda Calculus Algebraic Type Isomorphism Polymorphism and Parametricity Combining values conjunctively We want to store two things in one value. (might want to use non-compact slides for this one) Haskell Datatypes C Structs data Point = Java typedef struct point { Pnt { x :: Float float x; class Point { , y :: Float Haskell Tuples float y; public float x; } type Point = (Float, Float) } point; public float y; point midPoint (point p1, point p2) { } midpoint (Pnt x1 y1) (Pnt x2 y2) midpoint (x1,y1) (x2,y2) point mid; Point midPoint (Point p1, Point p2) { = ((x1+x2)/2, (y1+y2)/2) = ((x1+x2)/2, (y1+y2)/2) mid.x = (p1.x + p2.x) / 2.0; Point mid = new Point(); mid.y = (p2.y + p2.y) / 2.0; mid.x = (p1.x + p2.x) / 2.0; midpoint' p1 p2 = return mid; mid.y = (p2.y + p2.y) / 2.0; = ((x p1 + x p2) / 2, } return mid; (y p1 + y p2) / 2) } 22
Recommend
More recommend