algebraic data types christine rizkallah
play

Algebraic Data Types Christine Rizkallah CSE, UNSW (and data61) - PowerPoint PPT Presentation

Composite Data Types as Algebra, Logic Recursive Types Algebraic Data Types Christine Rizkallah CSE, UNSW (and data61) Term 3 2019 1 Composite Data Types as Algebra, Logic Recursive Types Composite Data Types Most of the types we have


  1. Composite Data Types as Algebra, Logic Recursive Types Algebraic Data Types Christine Rizkallah CSE, UNSW (and data61) Term 3 2019 1

  2. Composite Data Types as Algebra, Logic Recursive Types Composite Data Types Most of the types we have seen so far are basic types, in the sense that they represent built-in machine data representations. Real programming languages feature ways to compose types together to produce new types, such as: Classes Tuples Structs Unions Records 2

  3. Composite Data Types as Algebra, Logic Recursive Types 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 { “Better” Java Pnt { x :: Float float x; class Point { Haskell Tuples , y :: Float class Point { float y; public float x; private float x; } type Point = (Float, Float) } point; private float y; public float y; public Point (float x, float y) { point midPoint (point p1, point p2) { } this.x = x; this.y = y; 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) public float getX() {return this.x;} = ((x1+x2)/2, (y1+y2)/2) mid.x = (p1.x + p2.x) / 2.0; Point mid = new Point(); public float getY() {return this.y;} mid.y = (p2.y + p2.y) / 2.0; mid.x = (p1.x + p2.x) / 2.0; public float setX(float x) {this.x=x;} midpoint' p1 p2 = public float setY(float y) {this.y=y;} return mid; mid.y = (p2.y + p2.y) / 2.0; = ((x p1 + x p2) / 2, } } return mid; Point midPoint (Point p1, Point p2) { (y p1 + y p2) / 2) return new Point((p1.getX() + p2.getX()) / 2.0, } (p2.getY() + p2.getY()) / 2.0); } 3

  4. Composite Data Types as Algebra, Logic Recursive Types Product Types In MinHS, we will have a very minimal way to accomplish this, called a product type : τ 1 × τ 2 We won’t have type declarations, named fields or anything like that. More than two values can be combined by nesting products, for example a three dimensional vector: Int × ( Int × Int ) 4

  5. Composite Data Types as Algebra, Logic Recursive Types Constructors and Eliminators We can construct a product type similar to Haskell tuples: Γ ⊢ e 1 : τ 1 Γ ⊢ e 2 : τ 2 Γ ⊢ ( e 1 , e 2 ) : τ 1 × τ 2 The only way to extract each component of the product is to use the fst and snd eliminators: Γ ⊢ e : τ 1 × τ 2 Γ ⊢ e : τ 1 × τ 2 Γ ⊢ fst e : τ 1 Γ ⊢ snd e : τ 2 5

  6. Composite Data Types as Algebra, Logic Recursive Types Examples Example (Midpoint) recfun midpoint :: (( Int × Int ) → ( Int × Int ) → ( Int × Int )) p 1 = recfun midpoint ′ :: (( Int × Int ) → ( Int × Int )) p 2 = ((fst p 1 + fst p 2 ) ÷ 2 , (snd p 1 + snd p 2 ) ÷ 2) Example (Uncurried Division) recfun div :: (( Int × Int ) → Int ) args = if (fst args < snd args ) then 0 else div (fst args − snd args , snd args ) 6

  7. Composite Data Types as Algebra, Logic Recursive Types Dynamic Semantics e 1 �→ M e ′ e 2 �→ M e ′ 1 2 ( e 1 , e 2 ) �→ M ( e ′ ( v 1 , e 2 ) �→ M ( v 1 , e ′ 1 , e 2 ) 2 ) e �→ e ′ e �→ e ′ fst e �→ M fst e ′ snd e �→ M snd e ′ fst ( v 1 , v 2 ) �→ M v 1 snd ( v 1 , v 2 ) �→ M v 2 7

  8. Composite Data Types as Algebra, Logic Recursive Types Unit Types Currently, we have no way to express a type with just one value. This may seem useless at first, but it becomes useful in combination with other types. We’ll introduce a type, 1 , pronounced unit , that has exactly one inhabitant, written () : Γ ⊢ () : 1 8

  9. Composite Data Types as Algebra, Logic Recursive Types Disjunctive Composition We can’t, with the types we have, express a type with exactly three values. Example (Trivalued type) data TrafficLight = Red | Amber | Green In general we want to express data that can be one of multiple alternatives, that contain different bits of data. Example (More elaborate alternatives) type Length = Int type Angle = Int data Shape = Rect Length Length | Circle Length | Point | Triangle Angle Length Length This is awkward in many languages. In Java we’d have to use inheritance. In C we’d have to use unions. 9

  10. Composite Data Types as Algebra, Logic Recursive Types Sum Types We will use sum types to express the possibility that data may be one of two forms. τ 1 + τ 2 This is similar to the Haskell Either type. Our TrafficLight type can be expressed (grotesquely) as a sum of units: TrafficLight ≃ 1 + ( 1 + 1 ) 10

  11. Composite Data Types as Algebra, Logic Recursive Types Constructors and Eliminators for Sums To make a value of type τ 1 + τ 2 , we invoke one of two constructors: Γ ⊢ e : τ 1 Γ ⊢ e : τ 2 Γ ⊢ InL e : τ 1 + τ 2 Γ ⊢ InR e : τ 1 + τ 2 We can branch based on which alternative is used using pattern matching: Γ ⊢ e : τ 1 + τ 2 x : τ 1 , Γ ⊢ e 1 : τ y : τ 2 , Γ ⊢ e 2 : τ Γ ⊢ ( case e of InL x → e 1 ; InR y → e 2 ) : τ (Using concrete syntax here, for readability.) (Feel free to replace it with abstract syntax of your choosing.) 11

  12. Composite Data Types as Algebra, Logic Recursive Types Examples Example (Traffic Lights) Our traffic light type has three values as required: TrafficLight ≃ 1 + ( 1 + 1 ) ≃ InL () Red Amber ≃ InR (InL () ) ≃ InR (InR () ) Green 12

  13. Composite Data Types as Algebra, Logic Recursive Types Examples We can convert most (non-recursive) Haskell types to equivalent MinHs types now. Replace all constructors with 1 1 Add a × between all constructor arguments. 2 Change the | character that separates constructors to a +. 3 Example data Shape = Rect Length Length | Circle Length | Point | Triangle Angle Length Length ≃ 1 × ( Int × Int ) + 1 × Int + 1 + 1 × ( Int × ( Int × Int )) 13

  14. Composite Data Types as Algebra, Logic Recursive Types Dynamic Semantics e �→ M e ′ e �→ M e ′ InL e �→ M InL e ′ InR e �→ M InR e ′ e �→ M e ′ ( case e of InL x . e 1 ; InR y . e 2 ) �→ M ( case e ′ of InL x . e 1 ; InR y . e 2 ) ( case (InL v ) of InL x . e 1 ; InR y . e 2 ) �→ M e 1 [ x := v ] ( case (InR v ) of InL x . e 1 ; InR y . e 2 ) �→ M e 2 [ y := v ] 14

  15. Composite Data Types as Algebra, Logic Recursive Types The Empty Type We add another type, called 0 , that has no inhabitants. Because it is empty, there is no way to construct it. We do have a way to eliminate it, however: Γ ⊢ e : 0 Γ ⊢ absurd e : τ If I have a variable of the empty type in scope, we must be looking at an expression that will never be evaluated. Therefore, we can assign any type we like to this expression, because it will never be executed. 15

  16. Composite Data Types as Algebra, Logic Recursive Types Semiring Structure These types we have defined form an algebraic structure called a commutative semiring . Laws for ( τ, + , 0 ): Associativity: ( τ 1 + τ 2 ) + τ 3 ≃ τ 1 + ( τ 2 + τ 3 ) Identity: 0 + τ ≃ τ Commutativity: τ 1 + τ 2 ≃ τ 2 + τ 1 Laws for ( τ, × , 1 ) Associativity: ( τ 1 × τ 2 ) × τ 3 ≃ τ 1 × ( τ 2 × τ 3 ) Identity: 1 × τ ≃ τ Commutativity: τ 1 × τ 2 ≃ τ 2 × τ 1 Combining × and +: Distributivity: τ 1 × ( τ 2 + τ 3 ) ≃ ( τ 1 × τ 2 ) + ( τ 1 × τ 3 ) Absorption: 0 × τ ≃ 0 What does ≃ mean here? 16

  17. Composite Data Types as Algebra, Logic Recursive Types Isomorphism Two types τ 1 and τ 2 are isomorphic , written τ 1 ≃ τ 2 , if there exists a bijection between them. This means that for each value in τ 1 we can find a unique value in τ 2 and vice versa. We can use isomorphisms to simplify our Shape type: 1 × ( Int × Int ) + 1 × Int + 1 + 1 × ( Int × ( Int × Int )) ≃ Int × Int + Int + 1 + Int × ( Int × Int ) 17

  18. Composite Data Types as Algebra, Logic Recursive Types Examining our Types Lets look at the rules for typed lambda calculus extended with sums and products: Γ ⊢ e : 0 Γ ⊢ absurd e : τ Γ ⊢ () : 1 Γ ⊢ e : τ 1 Γ ⊢ e : τ 2 Γ ⊢ InL e : τ 1 + τ 2 Γ ⊢ InR e : τ 1 + τ 2 Γ ⊢ e : τ 1 + τ 2 x : τ 1 , Γ ⊢ e 1 : τ y : τ 2 , Γ ⊢ e 2 : τ Γ ⊢ ( case e of InL x → e 1 ; InR y → e 2 ) : τ Γ ⊢ e 1 : τ 1 Γ ⊢ e 2 : τ 2 Γ ⊢ e : τ 1 × τ 2 Γ ⊢ e : τ 1 × τ 2 Γ ⊢ ( e 1 , e 2 ) : τ 1 × τ 2 Γ ⊢ fst e : τ 1 Γ ⊢ snd e : τ 2 Γ ⊢ e 1 : τ 1 → τ 2 Γ ⊢ e 2 : τ 1 x : τ 1 , Γ ⊢ e : τ 2 Γ ⊢ e 1 e 2 : τ 2 Γ ⊢ λ x . e : τ 1 → τ 2 18

  19. Composite Data Types as Algebra, Logic Recursive Types Squinting a Little Lets remove all the terms, leaving just the types and the contexts: Γ ⊢ 0 Γ ⊢ τ Γ ⊢ 1 Γ ⊢ τ 1 Γ ⊢ τ 2 Γ ⊢ τ 1 + τ 2 Γ ⊢ τ 1 + τ 2 Γ ⊢ τ 1 + τ 2 τ 1 , Γ ⊢ τ τ 2 , Γ ⊢ τ Γ ⊢ τ Γ ⊢ τ 1 Γ ⊢ τ 2 Γ ⊢ τ 1 × τ 2 Γ ⊢ τ 1 × τ 2 Γ ⊢ τ 1 × τ 2 Γ ⊢ τ 1 Γ ⊢ τ 2 Γ ⊢ τ 1 → τ 2 Γ ⊢ τ 1 τ 1 , Γ ⊢ τ 2 Γ ⊢ τ 2 Γ ⊢ τ 1 → τ 2 Does this resemble anything you’ve seen before? 19

Recommend


More recommend