Algebraic Data Types
Total Page:16
File Type:pdf, Size:1020Kb
Composite Data Types as Algebra, Logic Recursive Types Algebraic Data Types Christine Rizkallah CSE, UNSW (and data61) Term 3 2019 1 Classes Tuples Structs Unions Records 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: 2 Classes Unions 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: Tuples Structs Records 3 Unions 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 Records 4 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 5 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 Tuples type Point=(Float, Float) midpoint (x1,y1) (x2,y2) = ((x1+x2)/2, (y1+y2)/2) 6 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 data Point= Pnt{x:: Float Haskell Tuples , y:: Float } 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) 7 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= typedef struct point { Pnt{x:: Float float x; Haskell Tuples , y:: Float float y; } type Point=}(Float point;, Float) point midPoint (point p1, point p2) { midpoint(Pnt x1 y1) (Pnt x2 y2) midpoint (x1,y1)point (x2,y2) mid; = ((x1+x2)/2, (y1+y2)/2) = ((x1+x2)/mid.x2, (y1= +(p1.xy2)/2+) 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) 8 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= typedef struct point {Java Pnt{x:: Float floatclass x; Point{ Haskell Tuples , y:: Float float y; public floatx; } type Point=}(Float point;public, Float float) y; point midPoint (point p1, point p2) { } midpoint(Pnt x1 y1) (Pnt x2 y2) midpoint (x1,y1)point (x2,y2) mid; Point midPoint(=Point ((x1+ p1x2), /Point2, (y1 p2+y2)){ /2) = ((x1+x2)/mid.x2, (y1Point= +(p1.xy2)/ mid2+) p2.x)= new /Point 2.0;(); mid.y= (p2.y+ p2.y)/ 2.0; mid.x=(p1.x+midpoint' p2.x)/ 2.0 p1; p2= return mid; mid.y=(p2.y+ =p2 ((x.y)/ p1 2.0+ x; p2)/2, } return mid; (y p1+ y p2)/2) } 9 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= typedef struct point {Java Pnt\Better"{x:: Float Java floatclass x; Point{ Haskell Tuplesclass Point{ , y:: Float float y; publicprivate float floatx;; } type Point=}(Float point;public, Floatprivate float) floaty;; public Point(floatx, floaty){ point midPoint (point p1, point p2) { } this.x=x;midpoint this.y=y; (Pnt x1 y1) (Pnt x2 y2) midpoint (x1,y1)point (x2,y2) mid;} Point midPoint(=Point ((x1+ p1x2), /Point2, (y1 p2+y2)){ /2) = ((x1+x2)/2, (y1+publicy2)/2 float) getX(){return this.x;} mid.xPoint= (p1.xpublic mid +float p2.x)= new getY /Point ()2.0{return;(); this.y;} mid.y= (p2.ypublic +float p2.y) setX/ (2.0float;x){this.x=x;} mid.x=(p1.x+midpoint' p2.x)/ 2.0 p1; p2= returnmid mid;public.y=( p2float.y +setY p2(.floaty)/ y2.0){this; .y=y;} } = ((x p1+ x p2)/2, } returnPoint midPoint mid; (Point(y p1, p1 Point+ y p2) p2){/2) } return new Point((p1.getX()+ p2.getX())/ 2.0, (p2.getY()+ p2.getY())/ 2.0); } 10 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) 11 Composite Data Types as Algebra, Logic Recursive Types Constructors and Eliminators We can construct a product type similar to Haskell tuples: Γ ` e1 : τ1 Γ ` e2 : τ2 Γ ` (e1; e2): τ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 12 Composite Data Types as Algebra, Logic Recursive Types Examples Example (Midpoint) recfun midpoint :: ((Int × Int) ! (Int × Int) ! (Int × Int)) p1 = recfun midpoint0 :: ((Int × Int) ! (Int × Int)) p2 = ((fst p1 + fst p2) ÷ 2; (snd p1 + snd p2) ÷ 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) 13 Composite Data Types as Algebra, Logic Recursive Types Dynamic Semantics 0 0 e1 7!M e1 e2 7!M e2 0 0 (e1; e2) 7!M (e1; e2) (v1; e2) 7!M (v1; e2) e 7! e0 e 7! e0 0 0 fst e 7!M fst e snd e 7!M snd e fst (v1; v2) 7!M v1 snd (v1; v2) 7!M v2 14 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 15 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. 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 16 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. 17 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) 18 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; Γ ` e1 : τ y : τ2; Γ ` e2 : τ Γ ` (case e of InL x ! e1; InR y ! e2): τ (Using concrete syntax here, for readability.) (Feel free to replace it with abstract syntax of your choosing.) 19 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) Red ' InL () Amber ' InR (InL ()) Green ' InR (InR ()) 20 Composite Data Types as Algebra, Logic Recursive Types Examples We can convert most (non-recursive) Haskell types to equivalent MinHs types now. 1 Replace all constructors with 1 2 Add a × between all constructor arguments. 3 Change the | character that separates constructors to a+. Example data Shape= Rect Length Length | Circle Length| Point | Triangle Angle Length Length ' 1 × (Int × Int) + 1 × Int + 1 + 1 × (Int × (Int × Int)) 21 Composite Data Types as Algebra, Logic Recursive Types Dynamic Semantics 0 0 e 7!M e e 7!M e 0 0 InL e 7!M InL e InR e 7!M InR e 0 e 7!M e 0 (case e of InL x: e1; InR y: e2) 7!M (case e of InL x: e1; InR y: e2) (case (InL v) of InL x: e1; InR y: e2) 7!M e1[x := v] (case (InR v) of InL x: e1; InR y: e2) 7!M e2[y := v] 22 Composite Data Types as Algebra, Logic Recursive Types Dynamic Semantics 0 0 e 7!M e e 7!M e 0 0 InL e 7!M InL e InR e 7!M InR e (case (InL v) of InL x: e1; InR y: e2) 7!M e1[x := v] (case (InR v) of InL x: e1; InR y: e2) 7!M e2[y