Overview Concepts Application Algebraic Data Types Mark Hibberd Mar 28, 2011 Mark Hibberd Algebraic Data Types
Overview Concepts Application Outline Introduction Outline Gentle(ish) introduction to Algebraic Data Types (ADTs). Demonstrate patterns, or encodings, for reprsenting ADTs. Examples, using Haskell, Scala and Java. Trade-offs, and ways of dealing with them. Designing an algebraric data type. Mark Hibberd Algebraic Data Types
Overview Concepts Application Outline Introduction Terminology Overload Sum type Product type Disjoint, or tagged, union Variant type Recursive, or inductive, data type Enumeratated type Composite type Type constructor Data constructor Class constructor Mark Hibberd Algebraic Data Types
Overview Concepts Application Outline Introduction Algebraic Data Types Composite . Definition by cases. Each case is composed into a single type. Closed . A finite set of cases. Safe . Provides mechanism for helping with (and enforcing) correct handling of all cases. Prevalant . The types of data we use every day. Generalizable? . There are a number of reliable patterns for defining combinators on algraic data-types. Mark Hibberd Algebraic Data Types
Overview Concepts Application Encoding Morphisms Patterns and Combinators Encoding Algebraic Data Types Mark Hibberd Algebraic Data Types
Overview Concepts Application Encoding Morphisms Patterns and Combinators Boolean: Haskell data Boolean = True | False 1 Mark Hibberd Algebraic Data Types
Overview Concepts Application Encoding Morphisms Patterns and Combinators Boolean: Haskell data Boolean = True | False 1 Type constructor - Boolean Two data constructors - True and False Disjoint union - exactly one of True or False Enumerated type - data constructors have no arguments. Mark Hibberd Algebraic Data Types
Overview Concepts Application Encoding Morphisms Patterns and Combinators Boolean: Scala sealed trait Boolean 1 case object True extends Boolean 2 case object False extends Boolean 3 Mark Hibberd Algebraic Data Types
Overview Concepts Application Encoding Morphisms Patterns and Combinators Boolean: Scala sealed trait Boolean 1 case object True extends Boolean 2 case object False extends Boolean 3 data Boolean = True | False 1 Mark Hibberd Algebraic Data Types
Overview Concepts Application Encoding Morphisms Patterns and Combinators Church Encoding Representing data types (and their) operations in λ calculus. More simply, can we represent values as functions? Why is this useful? Mark Hibberd Algebraic Data Types
Overview Concepts Application Encoding Morphisms Patterns and Combinators Church Booleans Formal Definition true ≡ λ a .λ b . a false ≡ λ a .λ b . b Mark Hibberd Algebraic Data Types
Overview Concepts Application Encoding Morphisms Patterns and Combinators Church Booleans: Scala sealed trait Boolean { 1 def fold[A](t: => A, f: => A): A 2 } 3 Mark Hibberd Algebraic Data Types
Overview Concepts Application Encoding Morphisms Patterns and Combinators Church Booleans: Scala sealed trait Boolean { 1 def fold[A](t: => A, f: => A): A 2 } 3 4 object Boolean { 5 def True: Boolean = new Boolean { 6 def fold[A](t: => A, f: => A) = t 7 } 8 9 def False: Boolean = new Boolean { 10 def fold[A](t: => A, f: => A) = f 11 } 12 } 13 Mark Hibberd Algebraic Data Types
Overview Concepts Application Encoding Morphisms Patterns and Combinators Church Booleans: Scala sealed trait Boolean { true ≡ λ a .λ b . a 1 def fold[A](t: => A, f: => A): A 2 false ≡ λ a .λ b . b } 3 4 object Boolean { 5 def True: Boolean = new Boolean { 6 def fold[A](t: => A, f: => A) = t 7 } 8 9 def False: Boolean = new Boolean { 10 def fold[A](t: => A, f: => A) = f 11 } 12 } 13 Mark Hibberd Algebraic Data Types
Overview Concepts Application Encoding Morphisms Patterns and Combinators Fold Fold is a catamorphism. A catamorphism is a higher order function that defines a data type. A catamorphism has the form: A ∗ → B where A ∗ is an algebraic data type, and B , is any other type. All functions on a type can be defined in terms of the catamorphism. If/Else is the catamorphism for boolean. Mark Hibberd Algebraic Data Types
Overview Concepts Application Encoding Morphisms Patterns and Combinators Church Boolean Operations: Scala sealed trait Boolean { 1 def fold[A](t: => A, f: => A): A 2 3 def and(b: Boolean) = fold(b, m) 4 5 def or(b: Boolean) = fold(this, b) 6 7 def not = fold(False, True) 8 9 def xor(b: Boolean) = fold( 10 b.fold(False, True), 11 b.fold(True, False) 12 ) 13 } 14 Mark Hibberd Algebraic Data Types
Overview Concepts Application Encoding Morphisms Patterns and Combinators Church Boolean Operations: Scala sealed trait Boolean { and ≡ λ m .λ n . m n m 1 def fold[A](t: => A, f: => A): A 2 3 def and(b: Boolean) = fold(b, m) 4 5 def or(b: Boolean) = fold(this, b) 6 7 def not = fold(False, True) 8 9 def xor(b: Boolean) = fold( 10 b.fold(False, True), 11 b.fold(True, False) 12 ) 13 } 14 Mark Hibberd Algebraic Data Types
Overview Concepts Application Encoding Morphisms Patterns and Combinators Church Boolean Operations: Scala sealed trait Boolean { or ≡ λ m .λ n . m m n 1 def fold[A](t: => A, f: => A): A 2 3 def and(b: Boolean) = fold(b, m) 4 5 def or(b: Boolean) = fold(this, b) 6 7 def not = fold(False, True) 8 9 def xor(b: Boolean) = fold( 10 b.fold(False, True), 11 b.fold(True, False) 12 ) 13 } 14 Mark Hibberd Algebraic Data Types
Overview Concepts Application Encoding Morphisms Patterns and Combinators Church Boolean Operations: Scala sealed trait Boolean { not ≡ λ m .λ a .λ b . m b a 1 def fold[A](t: => A, f: => A): A 2 3 def and(b: Boolean) = fold(b, m) 4 5 def or(b: Boolean) = fold(this, b) 6 7 def not = fold(False, True) 8 9 def xor(b: Boolean) = fold( 10 b.fold(False, True), 11 b.fold(True, False) 12 ) 13 } 14 Mark Hibberd Algebraic Data Types
Overview Concepts Application Encoding Morphisms Patterns and Combinators Church Boolean Operations: Scala sealed trait Boolean { xor ≡ λ m .λ n .λ a .λ b . 1 def fold[A](t: => A, f: => A): A 2 m ( n b a ) ( n a b ) 3 def and(b: Boolean) = fold(b, m) 4 5 def or(b: Boolean) = fold(this, b) 6 7 def not = fold(False, True) 8 9 def xor(b: Boolean) = fold( 10 b.fold(False, True), 11 b.fold(True, False) 12 ) 13 } 14 Mark Hibberd Algebraic Data Types
Overview Concepts Application Encoding Morphisms Patterns and Combinators There is a machine in your type Side bar There is a striking similarity between the catamorphism of a datastructure, A ∗ → B , and the type of a program, or even a virtual machine. Mark Hibberd Algebraic Data Types
Overview Concepts Application Encoding Morphisms Patterns and Combinators Church Booleans: Haskell data Boolean = B { true ≡ λ a .λ b . a 1 fold :: forall a. a -> a -> a 2 false ≡ λ a .λ b . b } 3 4 true = B (\a _ -> a) 5 false = B (\_ b -> b) 6 7 and m n = fold m n m 8 or m n = fold m m n 9 not m = fold m true false 10 xor m n = fold m 11 (fold n false true) 12 (fold n false true) 13 Mark Hibberd Algebraic Data Types
Overview Concepts Application Encoding Morphisms Patterns and Combinators Church Booleans: Haskell data Boolean = B { and ≡ 1 fold :: forall a. a -> a -> a 2 λ m .λ n . m n m } 3 4 true = B (\a _ -> a) 5 false = B (\_ b -> b) 6 7 and m n = fold m n m 8 or m n = fold m m n 9 not m = fold m true false 10 xor m n = fold m 11 (fold n false true) 12 (fold n false true) 13 Mark Hibberd Algebraic Data Types
Overview Concepts Application Encoding Morphisms Patterns and Combinators Church Booleans: Haskell data Boolean = B { or ≡ λ m .λ n . m m n 1 fold :: forall a. a -> a -> a 2 } 3 4 true = B (\a _ -> a) 5 false = B (\_ b -> b) 6 7 and m n = fold m n m 8 or m n = fold m m n 9 not m = fold m true false 10 xor m n = fold m 11 (fold n false true) 12 (fold n false true) 13 Mark Hibberd Algebraic Data Types
Overview Concepts Application Encoding Morphisms Patterns and Combinators Church Booleans: Haskell data Boolean = B { not ≡ 1 fold :: forall a. a -> a -> a 2 λ m .λ a .λ b . m b a } 3 4 true = B (\a _ -> a) 5 false = B (\_ b -> b) 6 7 and m n = fold m n m 8 or m n = fold m m n 9 not m = fold m true false 10 xor m n = fold m 11 (fold n false true) 12 (fold n false true) 13 Mark Hibberd Algebraic Data Types
Overview Concepts Application Encoding Morphisms Patterns and Combinators Church Booleans: Haskell data Boolean = B { xor ≡ 1 fold :: forall a. a -> a -> a 2 λ m .λ n .λ a .λ b . } 3 m ( n b a ) ( n a b ) 4 true = B (\a _ -> a) 5 false = B (\_ b -> b) 6 7 and m n = fold m n m 8 or m n = fold m m n 9 not m = fold m true false 10 xor m n = fold m 11 (fold n false true) 12 (fold n false true) 13 Mark Hibberd Algebraic Data Types
Recommend
More recommend