Composite Data Types as Algebra, Logic Recursive Types
1
Algebraic Data Types
Christine Rizkallah
CSE, UNSW Term 3 2020
Composite Data Types as Algebra, Logic Recursive Types
2
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
Unions
Structs
Records
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)
3
typedef struct point {
floactlxa;ss Point { class Point {
P“nBtet{texr”::JaFvlaoat
Haskell Tuples
, y :: Float }
floatpyu;blic float x; private float x;
private float y; type Point }= p(oFilnota;t, Float)
C Structs
data Point = Java
public float y;
public Point (float x, float y) {
point}midPoint (point p1, point p2) { this.x = x; this.y = y;
Haskell Datatypes
}
returmnimdi.dy;= (p2.y + p2.y) / 2.0;
} = ((x p1 + x p2) / 2,
midpoint (Pnt x1 y1) (Pnt x2 y2) Point midPoint (Point p1, Point p2) {
midpoint (x1p,oyi1n)t(mxi2}d,;y2)
public float getX() {return this.x;}
= ((x1+x2)/2, (y1+y2)/2) = ((x1+x2)/m2i,d.(xy1=+y(2p)1/.2x)+ p2.x) / 2.0;
}
(y p1 + y p2) / 2)
return new Point((p1.getX() + p2.getX()) / 2.0,
Point mid = new Point();
public float getY() {return this.y;}
mid.ym=ipd(u.pbxl2i.=cyf(l+poa1pt.2xs.eyt+)X(pf/2l.o2ax.t)0x;)/ {2t.hi0s;.x=x;}
midpoint’ p1 p2 = public float setY(float y) {this.y=y;}
return mid;
Point midPoint (Point p1, Point p2) {
(p2.getY() + p2.getY()) / 2.0);
Composite Data Types as Algebra, Logic Recursive Types
4
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)
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
5
Composite Data Types as Algebra, Logic Recursive Types
Examples
Example (Midpoint)
recfun midpoint :: ((Int×Int)→(Int×Int)→(Int×Int))p1 = recfun midpoint′ ::
((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 )
6
Composite Data
Types as Algebra, Logic
Recursive Types
Dynamic Semantics
e1 →M e1′ (e1,e2)→M (e1′,e2)
e → e′ fste→M fste′
fst (v1, v2) →M v1
e2 →M e2′ (v1,e2)→M (v1,e2′)
e → e′ snde→M snde′
snd (v1, v2) →M v2
7
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
Composite Data Types as Algebra, Logic Recursive Types
9
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.
Composite Data Types as Algebra, Logic Recursive Types
10
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)
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.)
11
Composite Data Types as Algebra, Logic Recursive Types
Examples
Example (Traffic Lights)
12
Our traffic light type has three values as required:
TrafficLight
Red
Amber
Green
≃ 1 + (1 + 1)
≃ InL()
≃ InR (InL ()) ≃ InR (InR ())
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))
13
Composite Data
Types as Algebra, Logic
Recursive Types
Dynamic Semantics
e→M e′ e→M e′ InLe→M InLe′ InRe→M InRe′
e →M e′
(case e of InL x. e1;InR y. e2) →M (case e′ of InL x. e1;InR y. e2)
(case (InL v) of InL x. e1;InR y. e2) →M e1[x := v] (case (InR v) of InL x. e1;InR y. e2) →M e2[y := v]
14
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 Γ⊢absurde: τ
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
Composite Data Types as Algebra, Logic Recursive Types
16
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?
Composite Data Types as Algebra, Logic Recursive Types
17
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)
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 : τ
Γ ⊢ e : τ1
Γ ⊢ InL e : τ1 + τ2
Γ ⊢ () : 1 Γ ⊢ e : τ2
Γ ⊢ InR e : τ1 + τ2
Γ ⊢ e : τ1 + τ2 x : τ1, Γ ⊢ e1 : τ y : τ2, Γ ⊢ e2 : τ
Γ ⊢ (case e of InL x → e1;InR y → e2) : τ
Γ⊢e1 :τ1 Γ⊢e2 :τ2 Γ ⊢ (e1, e2) : τ1 × τ2
Γ ⊢ e1 : τ1 → τ2
Γ⊢e :τ1 ×τ2 Γ⊢e :τ1 ×τ2 Γ ⊢ fst e : τ1 Γ ⊢ snd e : τ2
Γ ⊢ e2 : τ1 x : τ1,Γ ⊢ e : τ2 Γ⊢e1 e2 :τ2 Γ⊢λx.e:τ1 →τ2
18
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 Γ⊢τ2
τ1, Γ ⊢ τ Γ⊢τ Γ⊢τ1 ×τ2
Γ ⊢ τ1 Γ⊢τ1
Γ⊢τ1
Γ ⊢ τ1 × τ2
τ2, Γ ⊢ τ Γ⊢τ1 ×τ2
Γ ⊢ τ2 τ1,Γ⊢τ2
Γ ⊢ τ1 → τ2
Γ⊢τ1 →τ2
Γ ⊢ τ2
19
Does this resemble anything you’ve seen before?
Composite Data Types as Algebra, Logic Recursive Types
A surprising coincidence!
Types are exactly the same structure as constructive logic: Γ⊢⊥
Γ⊢P Γ⊢⊤
Γ ⊢ P1 Γ ⊢ P2 Γ⊢P1 ∨P2 Γ⊢P1 ∨P2
Γ ⊢ P1 ∨ P2 Γ⊢P1 Γ⊢P2
Γ ⊢ P1 ∧ P2 Γ⊢P1 →P2
Γ ⊢ P2
P2, Γ ⊢ P Γ⊢P1 ∧P2
Γ ⊢ P2 P1,Γ⊢P2
Γ ⊢ P1 → P2
P1, Γ ⊢ P Γ⊢P Γ⊢P1 ∧P2
Γ ⊢ P1 Γ⊢P1
20
This means, if we can construct a program of a certain type, we have also created a constructive proof of a certain proposition.
Composite Data Types as Algebra, Logic Recursive Types
The Curry-Howard Isomorphism
This correspondence goes by many names, but is usually attributed to Haskell Curry and William Howard.
It is a very deep result:
Programming
Logic
Types Programs Evaluation
Propositions Proofs
Proof Simplification
It turns out, no matter what logic you want to define, there is always a corresponding λ-calculus, and vice versa.
Constructive Logic Classical Logic Modal Logic Linear Logic Separation Logic
Typed λ-Calculus Continuations Monads
Linear Types, Session Types Region Types
21
Composite Data Types as Algebra, Logic Recursive Types
Examples
Example (Commutativity of Conjunction)
andComm :: A × B → B × A andComm p = (snd p, fst p)
This proves A ∧ B → B ∧ A.
Example (Transitivity of Implication)
transitive ::(A→B)→(B →C)→(A→C) transitive f g x =g (f x)
Transitivity of implication is just function composition.
22
Composite Data Types as Algebra, Logic Recursive Types
23
Caveats
All functions we define have to be total and terminating.
Otherwise we get an inconsistent logic that lets us prove false things:
proof1 ::P=NP proof 1 = proof 1
proof2 :: P ̸= NP proof 2 = proof 2
Most common calculi correspond to constructive logic, not classical ones, so principles like the law of excluded middle or double negation elimination do not hold:
¬¬P → P
Composite Data Types as Algebra, Logic Recursive Types
24
What about types like lists?
data IntList = Nil | Cons Int IntList We can’t express these in MinHS yet:
Inductive Structures
1 + (Int×??)
We need a way to do recursion!
Composite Data Types as Algebra, Logic Recursive Types
25
Recursive Types
We introduce a new form of type, written rec t. τ, that allows us to refer to the entire type:
IntList ≃
≃ 1+(Int×(rec t. 1+(Int×t)))
≃ 1+(Int×(1+(Int×(rec t. 1+(Int×t))))) ≃ ···
rec t. 1+(Int×t)
Composite Data Types as Algebra, Logic Recursive Types
Typing Rules
We construct a recursive type with roll, and unpack the recursion one level with unroll:
Γ ⊢ e : τ[t := rec t. τ] Γ ⊢ roll e : rec t. τ
Γ ⊢ e : rec t. τ
Γ ⊢ unroll e : τ[t := rec t. τ]
26
Composite Data
Types as Algebra, Logic
Recursive Types
Example
Example
Take our IntList example:
[] = [1] = [1, 2] =
rec t. 1+(Int×t)
roll (InL ())
roll (InR (1, roll (InL ())))
roll (InR (1, roll (InR (2, roll (InL ())))))
27
Composite Data Types as Algebra, Logic Recursive Types
Nothing interesting here:
Dynamic Semantics
e→M e′ e→M e′
roll e →M roll e′ unroll e →M unroll e′
28
unroll (roll e) →M e