General elimination and catamorphisms David Ripley Monash - - PowerPoint PPT Presentation

general elimination and catamorphisms
SMART_READER_LITE
LIVE PREVIEW

General elimination and catamorphisms David Ripley Monash - - PowerPoint PPT Presentation

General elimination and catamorphisms David Ripley Monash University http://davewripley.rocks The question The question Gentzen Gentzen 1934: The introductions represent, as it were, the definitions of the symbols concerned, and


slide-1
SLIDE 1

General elimination and catamorphisms

David Ripley

Monash University http://davewripley.rocks

slide-2
SLIDE 2

The question

slide-3
SLIDE 3

The question Gentzen

Gentzen 1934: “The introductions represent, as it were, the ‘definitions’

  • f the symbols concerned, and the eliminations are no more…

than the consequences of these definitions.… By making these ideas more precise it should be possible to display the E-inferences as unique functions

  • f their corresponding I-inferences”
slide-4
SLIDE 4

The question Dummett

“Such [introduction] laws will be ‘self-justifying’: we are entitled simply to stipulate [them], because by so doing we fix…the meanings of the logical constants that they govern”

slide-5
SLIDE 5

The question Dummett

“Plainly, the elimination rules are not consequences of the introduction rules in the straightfoward sense of being derivable from them; Gentzen must therefore have had in mind some more powerful means of drawing consequences”

slide-6
SLIDE 6

The question Dummett

“Plainly, the elimination rules are not consequences of the introduction rules in the straightfoward sense of being derivable from them; Gentzen must therefore have had in mind some more powerful means of drawing consequences”

slide-7
SLIDE 7

The question

What is this “more powerful means”? Given introduction rules for a connective, how to determine its elimination rules?

slide-8
SLIDE 8

Examples

slide-9
SLIDE 9

Examples Propositions as types, proofs as programs, etc

A proposition is either:

  • atomic p, q, r, . . ., or
  • A ∧ B, where A and B

are propositions, or

  • A ∨ B, where A and B

are propositions, or … A type is either:

  • basic Int, Char, Double, …, or
  • Pair a b, where a and b

are types, or

  • Either a b, where a and b

are types, or …

slide-10
SLIDE 10

Examples Disjunction (sum)

A

∨Il:

A ∨ B B

∨Ir:

A ∨ B data Either a b = Left a | Right b A ∨ B (A) . . . C (B) . . . C

∨E:

C either :: (a -> c) -> (b -> c)

  • > Either a b -> c
slide-11
SLIDE 11

Examples Disjunction (sum)

A

∨Il:

A ∨ B B

∨Ir:

A ∨ B data Either a b = Left a | Right b A ∨ B (A) . . . C (B) . . . C

∨E:

C either :: (a -> c) -> (b -> c)

  • > Either a b -> c
slide-12
SLIDE 12

Examples Disjunction (sum)

A

∨Il:

A ∨ B B

∨Ir:

A ∨ B data Either a b = Left a | Right b A ∨ B (A) . . . C (B) . . . C

∨E:

C either :: (a -> c) -> (b -> c)

  • > Either a b -> c
slide-13
SLIDE 13

Examples Disjunction (sum)

x A

∨Il:

A ∨ B (A) f C (B) . . . C

∨E:

C ⇝ x A f C either :: (a -> c) -> (b -> c)

  • > Either a b -> c

either f _ (Left x) = f x

slide-14
SLIDE 14

Examples Disjunction (sum)

x A

∨Il:

A ∨ B (A) f C (B) . . . C

∨E:

C ⇝ x A f C either :: (a -> c) -> (b -> c)

  • > Either a b -> c

either f _ (Left x) = f x

slide-15
SLIDE 15

Examples Achievement unlocked: reading the title bar (25 pts)

y B

∨Ir:

A ∨ B (A) . . . C (B) g C

∨E:

C ⇝ y B g C either :: (a -> c) -> (b -> c)

  • > Either a b -> c

either f _ (Left x) = f x either _ g (Right y) = g y

slide-16
SLIDE 16

Examples Two lenses

compound formula compound type I rule type constructor E rule type signature for elimination function reduction step definition of elimination function

slide-17
SLIDE 17

Examples Conjunction (product)

A B

∧I:

A ∧ B data Pair a b = Pr a b A ∧ B (A), (B) . . . C

∧GE:

C uncurry :: (a -> b -> c) -> Pair a b -> c

slide-18
SLIDE 18

Examples Conjunction (product)

x A y B

∧I:

A ∧ B (A), (B) f C

∧GE:

C ⇝ x A y B f C uncurry :: (a -> b -> c) -> Pair a b -> c uncurry f (Pr x y) = f x y

slide-19
SLIDE 19

Method 1: general elimination

slide-20
SLIDE 20

Method 1: general elimination Negri

“[W]hatever follows from the sufficient grounds for deriv- ing a formula must follow from that formula… [W]hatever follows from a for- mula must follow from the sufficient grounds for deriv- ing the formula”

slide-21
SLIDE 21

Method 1: general elimination Read

“The introduction-rule not only shows what is [sufficient for] the conclusion but also [what is] necessary”

slide-22
SLIDE 22

Method 1: general elimination Moriconi & Tesconi

Moriconi & Tesconi: “[I]t is quite natural to ask what consequences can be drawn from A, given that A can be produced only by [certain] rules. The answer is: we can draw all the consequences that we can draw from the premisses of those rules”

slide-23
SLIDE 23

Method 1: general elimination Revisiting the examples

A B

∧I:

A ∧ B A ∧ B (A), (B) . . . C

∧GE:

C A

∨Il:

A ∨ B B

∨Ir:

A ∨ B A ∨ B (A) . . . C (B) . . . C

∨E:

C

slide-24
SLIDE 24

Method 2: Catamorphisms

slide-25
SLIDE 25

Method 2: catamorphisms Background

Catamorphisms give a different way to generate either, uncurry, and the like The first step into so-called ‘recursion schemes’

slide-26
SLIDE 26

Method 2: catamorphisms Lists

Nil:

[A] A [A]

Cons:

[A] data [a] = [] | a : [a] In Dummett’s terminology, Cons is: pure, simple, direct, not sheer, not single-ended, violates the complexity condition

slide-27
SLIDE 27

Method 2: catamorphisms Lists

Nil:

[A] A [A]

Cons:

[A] data [a] = [] | a : [a] In Dummett’s terminology, Cons is: pure, simple, direct, not sheer, not single-ended, violates the complexity condition

slide-28
SLIDE 28

Method 2: catamorphisms Lists

Nil:

[A] A [A]

Cons:

[A] data [a] = [] | a : [a] A B A B . . . B

foldr:

B foldr :: (a -> b -> b) -> b -> [a] -> b

slide-29
SLIDE 29

Method 2: catamorphisms Lists

Nil:

[A] A [A]

Cons:

[A] data [a] = [] | a : [a] [A] B (A), (B) . . . B

foldr:

B foldr :: (a -> b -> b) -> b -> [a] -> b

slide-30
SLIDE 30

Method 2: catamorphisms Lists

Nil:

[A] A [A]

Cons:

[A] data [a] = [] | a : [a] [A] B (A), (B) . . . B

foldr:

B foldr :: (a -> b -> b) -> b -> [a] -> b

slide-31
SLIDE 31

Method 2: catamorphisms Lists

Nil:

[A] A [A]

Cons:

[A] data [a] = [] | a : [a] [A] B (A), (B) . . . B

foldr:

B foldr :: (a -> b -> b) -> b -> [a] -> b

slide-32
SLIDE 32

Method 2: catamorphisms Lists

Nil:

[A] z B (A), (B) . . . B

foldr:

B ⇝ z B foldr :: (a -> b -> b) -> b -> [a] -> b foldr _ z [] = z

slide-33
SLIDE 33

Method 2: catamorphisms Lists

x A xs [A]

Cons:

[A] z B (A), (B) f B

foldr:

B ⇝ x A xs [A] z B (A), (B) f B

foldr:

B

  • f

B foldr :: (a -> b -> b) -> b -> [a] -> b foldr _ z [] = z foldr f z (x : xs) = f x (foldr f z xs)

slide-34
SLIDE 34

Method 2: catamorphisms Motivation: generality

Meijer, Fokkinga, Paterson: “Countless list processing functions are readily recognizable as catamorphisms”

slide-35
SLIDE 35

Method 2: catamorphisms Motivation: generality

ns = 1 : (3 : (5 : (7 : []))) foldr (+) 0 ns = 1 + (3 + (5 + (7 + 0 ))) = sum ns foldr (*) 1 ns = 1 * (3 * (5 * (7 * 1 ))) = product ns

slide-36
SLIDE 36

Method 2: catamorphisms Motivation: generality

append :: [a] -> [a] -> [a] append xs ys = foldr (:) ys xs concat :: [[a]] -> [a] concat xss = foldr append [] xss

slide-37
SLIDE 37

Method 2: catamorphisms Motivation: generality

[A]

Nil:

[B] (A) . . . B ([B])

Cons:

[B]

foldr:

[B]

slide-38
SLIDE 38

Method 2: catamorphisms Motivation: generality

Nil:

[A]

Nil:

[B] (A) . . . B ([B])

Cons:

[B]

foldr:

[B] ⇝

Nil:

[B]

slide-39
SLIDE 39

Method 2: catamorphisms Motivation: generality

x A xs [A]

Cons:

[A]

Nil:

[B] (A) f B ([B])

Cons:

[B]

foldr:

[B] ⇝ x A f B xs [A]

Nil:

[B] (A) f B ([B])

Cons:

[B]

foldr:

[B]

Cons:

[B]

slide-40
SLIDE 40

Method 2: catamorphisms Intuition: translation

Nil:

[A] A [A]

Cons:

[A] This is a language for building proofs of [A] We can translate this language into a language for proofs of B if we can translate each rule

slide-41
SLIDE 41

Method 2: catamorphisms Disjunction (sum)

A

∨Il:

A ∨ B B

∨Ir:

A ∨ B To get a catamorphism from A ∨ B to C, provide proofs f from A to C and g from B to C Replace ∨Il with f and ∨Ir with g either :: (a -> c) -> (b -> c) -> Either a b -> c either f _ (Left x) = f x either _ g (Right y) = g y

slide-42
SLIDE 42

Method 2: catamorphisms Conjunction (product)

A B

∧I:

A ∧ B To get a catamorphism from A ∧ B to C, provide a proof f from A and B to C Replace ∧I with f uncurry :: (a -> b -> c) -> Pair a b -> c uncurry f (Pr x y) = f x y

slide-43
SLIDE 43

Differences

slide-44
SLIDE 44

Differences Disjunction and conjunction

General elimination and catamorphisms have a lot in common! Catamorphisms for disjunction give us the usual (general) elimination rule And catamorphisms for conjunction give us the general elimination rule

slide-45
SLIDE 45

Differences GE for lists

[A] B (A), ([A])

  • .

. . B

[ ]GE:

B [A] B (A), (B) . . . B

foldr:

B

slide-46
SLIDE 46

Differences GE for lists

Nil:

[A] z B (A), ([A])

  • .

. . B

[ ]GE:

B ⇝ z B

Nil:

[A] z B (A), (B) . . . B

foldr:

B ⇝ z B

slide-47
SLIDE 47

Differences GE for lists

x A xs [A]

Cons:

[A] z B (A), ([A])

  • f

B

[ ]GE:

B ⇝ x A xs [A]

  • f

B x A xs [A]

Cons:

[A] z B (A), (B) f B

foldr:

B ⇝ x A xs [A] z B (A), (B) f B

foldr:

B

  • f

B

slide-48
SLIDE 48

Differences Looking for strength

“The fact that the [rules for a connective] are in harmony … shows only that we draw no consequences its meaning does not entitle us to draw. It does not show that we fully exploit that meaning”

slide-49
SLIDE 49

Differences Looking for strength

Jacinto & Read 2017: “[T]he E-rules should not merely be justified by the meaning conferred by the I-rules; they should allow one to infer everything that is warranted by that meaning”

slide-50
SLIDE 50

Differences Looking for strength

So which of these approaches allows us to capture more?

slide-51
SLIDE 51

Differences General elimination is not a catamorphism

safeTail :: [a] -> [a] safeTail [] = [] safeTail (x : xs) = xs safeTail [1, 2] = [2] safeTail [1, 7] = [7] safeTail [2] = [] safeTail [7] = [] if safeTail = foldr f z, then safeTail (x : xs) = f x (safeTail xs) But f 1 [] can’t be both [2] and [7]! So safeTail is not foldr f z for any f, z

slide-52
SLIDE 52

Differences General elimination is not a catamorphism

General elimination gives us safeTail: [A]

Nil:

[A] ([A])

[ ]GE:

[A] So catamorphisms don’t fully capture general elimination

slide-53
SLIDE 53

Differences Catamorphisms are not general eliminations

If we have any proof f from [A] to B, we can make it into a GE: xs [A]

Nil:

[A] f B (A) ([A])

Cons:

[A] f B

[ ]GE:

B

slide-54
SLIDE 54

Differences Catamorphisms are not general eliminations

So GE is in some sense universal: any proof we’re able to rig up, we can embed in an equivalent GE This isn’t true of foldr, as we’ve seen

slide-55
SLIDE 55

Differences Catamorphisms are not general eliminations

And for a given list, we can rig up something with [ ]GE that does what (foldr f z) would do with the list But can’t do it once for all lists: foldr is recursive and [ ]GE ain’t What we rig up will include a number of [ ]GEs depending on the length of the list

slide-56
SLIDE 56

Differences Catamorphisms are not general eliminations

So we can’t give a single definition of append or concat,

  • r of mapping a proof (even a fixed proof) from A to B
  • ver an arbitrary [A] to get the resulting [B].

Even given 0 and + or 1 and *, we can’t define sum or product with GE alone.

slide-57
SLIDE 57

Differences No clear winner

foldr and [ ]GE differ We want the strongest thing we can get, but neither is stronger than the other

slide-58
SLIDE 58

We need a hybrid!

slide-59
SLIDE 59

Combining the approaches Paramorphisms

foldr :: (a -> b -> b) -> b -> [a] -> b listGE :: (a -> [a] -> b) -> b -> [a] -> b para :: (a -> [a] -> b -> b) -> b -> [a] -> b foldr _ z [] = z listGE _ z [] = z para _ z [] = z foldr f z (x:xs) = f x (foldr f z xs) listGE g z (x:xs) = g x xs para h z (x:xs) = h x xs (para h z xs)

slide-60
SLIDE 60

Combining the approaches Paramorphisms

[A] B (A), ([A])

  • .

. . B

[ ]GE:

B [A] B (A), (B) . . . B

foldr:

B [A] B (A), ([A]), (B)

  • .

. . B

para:

B

slide-61
SLIDE 61

Combining the approaches Paramorphisms

x A xs [A]

Cons:

[A] z B (A), ([A]), (B)

  • h

B

para:

B ⇝ x A xs [A] xs [A] z B (A), ([A]), (B)

  • h

B

para:

B

  • h

B

slide-62
SLIDE 62

Upshots

slide-63
SLIDE 63

Upshots Taking stock

Nil:

[A] A [A]

Cons:

[A] General elimination focuses on the grounds for introduction [A] B (A), ([A])

  • .

. . B

[ ]GE:

B

slide-64
SLIDE 64

Upshots Taking stock

Nil:

[A] A [A]

Cons:

[A] Catamorphisms focus on translating the introduction rules [A] B (A), (B) . . . B

foldr:

B

slide-65
SLIDE 65

Upshots Taking stock

Nil:

[A] A [A]

Cons:

[A] Paramorphisms take both into account, using the grounds both translated and untranslated [A] B (A), ([A]), (B)

  • .

. . B

para:

B

slide-66
SLIDE 66

Upshots Taking stock

slide-67
SLIDE 67

Upshots Extra power

This allows us to define things out of reach

  • f both [ ]GE and foldr individually

Eg the proof from [A] to [[A]] that replaces each member of the original list with the list of its followers

slide-68
SLIDE 68

Upshots Extra power

[A]

Nil:

[[A]] ([A]) ([[A]])

Cons:

[[A]]

para:

[[A]]

slide-69
SLIDE 69

Upshots Extra power

Question: Can para do anything that foldr and [ ]GE can’t do together? Known (Meertens): With foldr, ∧I and ∧GE, we can recover para

slide-70
SLIDE 70

Upshots Summing up

General elimination works fine for disjunctions, conjunctions, and the like But it cannot exploit the structure

  • f recursive types
slide-71
SLIDE 71

Upshots Summing up

Catamorphisms do exactly the same for disjunctions, conjunctions, and the like And they can exploit the structure

  • f recursive types

But they miss some things general eliminations can do

slide-72
SLIDE 72

Upshots Summing up

Paramorphisms combine the two, giving all the power of both They are the most promising way to extend elimination rules to recursive types

slide-73
SLIDE 73

Thanks!

slide-74
SLIDE 74

Achievement unlocked! Dependent type theory (50 pts)

slide-75
SLIDE 75

Dependent type theory Meertens

Meertens: “The recursive pattern involved [in paramorphisms] is well known: it is essentially the same as the standard pattern used in the so-called elimination rules for a data type in constructive type theory”

slide-76
SLIDE 76

Dependent type theory List elimination

[A] B (A), ([A]), (B)

  • .

. . B

para:

B x :: [A] y :: B([ ]) (a :: A), (l :: [A]), (h :: B(l))

  • .

. . z(a, l, h) :: B(a : l) Listelim(x, y, z) :: B(x)