Pattern und Copattern Matching Anton Setzer Swansea, UK (Sect 1 - - - PowerPoint PPT Presentation

pattern und copattern matching
SMART_READER_LITE
LIVE PREVIEW

Pattern und Copattern Matching Anton Setzer Swansea, UK (Sect 1 - - - PowerPoint PPT Presentation

Pattern und Copattern Matching Anton Setzer Swansea, UK (Sect 1 - 3 joint work with Andreas Abel, Brigitte Pientka, and David Thibodeau) Advances in Proof Theory Bern, 13 14 December 2013 To celebrate Gerhard J agers 60th birthday


slide-1
SLIDE 1

Pattern und Copattern Matching

Anton Setzer Swansea, UK (Sect 1 - 3 joint work with Andreas Abel, Brigitte Pientka, and David Thibodeau) Advances in Proof Theory Bern, 13 – 14 December 2013 To celebrate Gerhard J¨ ager’s 60th birthday

Anton Setzer Pattern und Copattern Matching 1/ 38

slide-2
SLIDE 2

Happy Birthday

Happy Birthday

Anton Setzer Pattern und Copattern Matching 2/ 38

slide-3
SLIDE 3

One Key Achievement of G. J¨ ager

◮ Shift in Proof Theory from Subsystems of Analysis to Kripke-Platek

set theory.

  • Anton Setzer

Pattern und Copattern Matching 3/ 38

slide-4
SLIDE 4

One Key Achievement of G. J¨ ager

◮ Shift in Proof Theory from Subsystems of Analysis to Kripke-Platek

set theory.

◮ Reason for its success:

◮ Allows to principles from recursion theory very well

Therefore provide an excellent layering of theories of different proof theoretic strength.

◮ Sets have a natural tree like structure, which corresponds well to proofs

which are trees as well.

  • ai1

a = {bi | i ∈ I} ai = {bj | j ∈ J}

Anton Setzer Pattern und Copattern Matching 3/ 38

slide-5
SLIDE 5

Second Key Shift by Gerhard J¨ ager

◮ Use of Feferman’s Theory of Explicit Mathematics as a laboratory for

the formalisation and proof theoretic analysis of recursion principles.

◮ Feferman are very flexible for this purpose.

◮ One example by R. Kahle and A.S.:

Mahlo universe can be completely predicatively described in Feferman systems (extended predicative Mahlo).

◮ It would be nice to try out the use of EM as a programming language.

Anton Setzer Pattern und Copattern Matching 4/ 38

slide-6
SLIDE 6

PX

◮ One implementation of Feferman Systems is the PX system. ◮ Hayashi, Susumu and Nakano, Hiroshi: PX: a computational logic.

MIT Press, 1988.

Anton Setzer Pattern und Copattern Matching 5/ 38

slide-7
SLIDE 7

PX

◮ One implementation of Feferman Systems is the PX system. ◮ Hayashi, Susumu and Nakano, Hiroshi: PX: a computational logic.

MIT Press, 1988.

◮ My third year student Nathan Smith contacted the creator Susumu

Hayashi:

Anton Setzer Pattern und Copattern Matching 5/ 38

slide-8
SLIDE 8

PX

◮ One implementation of Feferman Systems is the PX system. ◮ Hayashi, Susumu and Nakano, Hiroshi: PX: a computational logic.

MIT Press, 1988.

◮ My third year student Nathan Smith contacted the creator Susumu

Hayashi:

Anton Setzer Pattern und Copattern Matching 5/ 38

slide-9
SLIDE 9

PX

◮ One implementation of Feferman Systems is the PX system. ◮ Hayashi, Susumu and Nakano, Hiroshi: PX: a computational logic.

MIT Press, 1988.

◮ My third year student Nathan Smith contacted the creator Susumu

Hayashi: Dear Nathan, Thank you for your mail. I stopped to maintain PX system about a quarter century ago. I do not even keep the codes of the system. Sorry, but I have left computer science and software engineering long time ago. Best regards, Susumu Hayashi

Anton Setzer Pattern und Copattern Matching 5/ 38

slide-10
SLIDE 10

One more Achievement by J¨ ager

◮ Metaprediacativity

◮ Exploration of the limit of predicative methods in proof theory. Anton Setzer Pattern und Copattern Matching 6/ 38

slide-11
SLIDE 11

From Codata to Coalgebras Algebras and Coalgebras Patterns and Copatterns Codata types and Decidable Equality Conclusion Appendix: Example of Objects as Coalgebras Appendix: Reduction of Mixed Pattern/Copattern Matching to Operators

Anton Setzer Pattern und Copattern Matching 7/ 38

slide-12
SLIDE 12

From Codata to Coalgebras

From Codata to Coalgebras Algebras and Coalgebras Patterns and Copatterns Codata types and Decidable Equality Conclusion Appendix: Example of Objects as Coalgebras Appendix: Reduction of Mixed Pattern/Copattern Matching to Operators

Anton Setzer Pattern und Copattern Matching 8/ 38

slide-13
SLIDE 13

From Codata to Coalgebras

Dependent Type Theory with Decidable Type Checking

◮ We explore the use of coalgebras in the context of dependent type

theory with decidable type checking.

◮ This allows to program in dependent type theory in a similar way as

when programming in other typed languages.

◮ No need to derive that something is a correct program.

◮ Type checking in dependent type theory requires checking of

(definitional) equality.

◮ Such an equality cannot be extensional. ◮ Instead, two functions f , g : A → B are equal if they (or their

program codes) reduce to the same normal form.

Anton Setzer Pattern und Copattern Matching 9/ 38

slide-14
SLIDE 14

From Codata to Coalgebras

Codata Type

◮ Idea of Codata Types non-well-founded versions of inductive data

types: codata Stream : Set where cons : N → Stream → Stream

◮ Same definition as inductive data type but we are allowed to have

infinite chains of constructors cons n0 (cons n1 (cons n2 · · · ))

◮ Problem 1: Non-normalisation. ◮ Problem 2: Equality between streams is equality between all ni, and

therefore undecidable.

◮ Problem 3: Underlying assumption is

∀s : Stream.∃n, s′.s = cons n s′ which results in undecidable equality.

Anton Setzer Pattern und Copattern Matching 10/ 38

slide-15
SLIDE 15

From Codata to Coalgebras

Subject Reduction Problem

◮ In order to repair problem of normalisation restrictions on reductions

were introduced.

◮ Resulted in Coq in a long known problem of subject reduction. ◮ In order to avoid this, in Agda dependent elimination for coalgebras

disallowed.

◮ Makes it difficult to use. Anton Setzer Pattern und Copattern Matching 11/ 38

slide-16
SLIDE 16

From Codata to Coalgebras

Coalgebraic Formulation of Coalgebras

◮ Solution is to follow the long established categorical formulation of

coalgebras.

◮ Final coalgebras will be replaced by weakly final coalgebras. ◮ Two streams will be equal if the programs producing them reduce to

the same normal form.

Anton Setzer Pattern und Copattern Matching 12/ 38

slide-17
SLIDE 17

Algebras and Coalgebras

From Codata to Coalgebras Algebras and Coalgebras Patterns and Copatterns Codata types and Decidable Equality Conclusion Appendix: Example of Objects as Coalgebras Appendix: Reduction of Mixed Pattern/Copattern Matching to Operators

Anton Setzer Pattern und Copattern Matching 13/ 38

slide-18
SLIDE 18

Algebras and Coalgebras

Algebras and Coalgebras

Elaboration of notions of (co)iteration, (co)recursion, induction is result of discussions with Peter Hancock.

◮ Algebraic data types correspond to initial algebras.

◮ N as an algebra can be represented as introduction rules for N:

: N S : N → N

◮ Coalgebra obtained by “reversing the arrows”.

◮ Stream as a coalgebra can be expressed as as elimination rules for it:

head : Stream → N tail : Stream → Stream

Anton Setzer Pattern und Copattern Matching 14/ 38

slide-19
SLIDE 19

Algebras and Coalgebras

Weakly Initial Algebras and Final Coalgebras

◮ N as a weakly initial algebra corresponds to iteration

(elimination rule): For A : Set, a : A, f : A → A there exists g : N → A g 0 = a g (S n) = f (g n) (or g n = f n a).

◮ Stream as a weakly final coalgebra corresponds to coiteration or

guarded iteration (introduction rule): For A : Set, f0 : A → N, f1 : A → A there exists g s.t. g : A → Stream head (g a) = f0 a tail (g a) = g (f1 a)

Anton Setzer Pattern und Copattern Matching 15/ 38

slide-20
SLIDE 20

Algebras and Coalgebras

Example

◮ Using coiteration we can define

inc : N → Stream head (inc n) = n tail (inc n) = inc (n + 1)

Anton Setzer Pattern und Copattern Matching 16/ 38

slide-21
SLIDE 21

Algebras and Coalgebras

Recursion and Corecursion

◮ N as an initial algebra corresponds to uniqueness of g above.

◮ Allows to derive primitive recursion:

For A : Set, a : A, f : (N × A) → A there exists g : N → A g 0 = a g (S n) = f n, (g n)

◮ Stream as a final coalgebra corresponds to uniqueness of h.

◮ Allows to derive primitive corecursion:

For A : Set, f0 : A → N, f1 : A → (Stream + A) there exists g : A → Stream head (g a) = f0 a tail (g a) = s if f1 a = inl s tail (g a) = g a′ if f1 a = inr a′

Anton Setzer Pattern und Copattern Matching 17/ 38

slide-22
SLIDE 22

Algebras and Coalgebras

Recursion vs Iteration

◮ Using recursion we can define inverse case of the constructors of N as

follows: case : N → (1 + N) case 0 = inl case (S n) = inr n

◮ Using iteration, we cannot make use of n and therefore case is defined

inefficiently: case : N → (1 + N) case 0 = inl case (S n) = caseaux (case n) caseaux : (1 + N) → (1 + N) caseaux inl = inr 0 caseaux (inr n) = inr (S n)

Anton Setzer Pattern und Copattern Matching 18/ 38

slide-23
SLIDE 23

Algebras and Coalgebras

Defininition of pred

◮ In the talk given we defined pred in a wrong way (using iteration).

One way of defining pred by iteration is by defining first case and then to define predaux : (1 + N) → N predaux inl = predaux (inr n) = n pred : N → N pred n = predaux (case n)

Anton Setzer Pattern und Copattern Matching 19/ 38

slide-24
SLIDE 24

Algebras and Coalgebras

Corecursion vs Coiteration

◮ Definition of cons (inverse of the destructors) using coiteration

inefficient: cons : N → Stream → Stream head (cons n s) = n tail (cons n s) = cons (head s) (tail s)

◮ Using primitive corecursion we can define more easily

cons : N → Stream → Stream head (cons n s) = n tail (cons n s) = s

Anton Setzer Pattern und Copattern Matching 20/ 38

slide-25
SLIDE 25

Algebras and Coalgebras

Induction - Coinduction?

◮ Induction is dependent primitive recursion:

For A : N → Set, a : A 0, f : (n : N) → A n → A (S n) there exists g : (n : N) → A n g 0 = a g (S n) = f n (g n)

◮ Equivalent to uniqueness of arrows with respect to propositional

equality and interpreting equality on arrows extensionally.

◮ Uniqueness of arrows in final coalgebras expresses that equality is

bisimulation equality.

◮ How to dualise dependent primitive recursion? Anton Setzer Pattern und Copattern Matching 21/ 38

slide-26
SLIDE 26

Algebras and Coalgebras

Weakly Final Coalgebra

◮ Equality for final coalgebras is undecidable:

Two streams s = (a0 , a1 , a2 , . . . t = (b0 , b1 , b2 , . . . are equal iff ai = bi for all i.

◮ Even the weak assumption

∀s.∃n, s′.s = cons n s′ results in an undecidable equality.

◮ Weakly final coalgebras obtained by omitting uniqueness of g in

diagram for coalgebras.

◮ However, one can extend schema of coiteration as above, and still

preserve decidability of equality.

◮ Those schemata are usually not derivable in weakly final coalgebras. Anton Setzer Pattern und Copattern Matching 22/ 38

slide-27
SLIDE 27

Algebras and Coalgebras

Definition of Coalgebras by Observations

◮ We see now that elements of coalgebras are defined by their

  • bservations:

An element s of Stream is anything for which we can define head s : N tail s : Stream

◮ This generalises the function type.

Functions are as well determined by observations.

◮ An f : A → B is any program which if applied to a : A returns some

b : B.

◮ Inductive data types are defined by construction

coalgebraic data types and functions by observations.

Anton Setzer Pattern und Copattern Matching 23/ 38

slide-28
SLIDE 28

Algebras and Coalgebras

Relationship to Objects in Object-Oriented Programming

◮ Objects in Object-Oriented Programming are types which are defined

by their observations.

◮ Therefore objects are coalgebraic types by nature.

Anton Setzer Pattern und Copattern Matching 24/ 38

slide-29
SLIDE 29

Patterns and Copatterns

From Codata to Coalgebras Algebras and Coalgebras Patterns and Copatterns Codata types and Decidable Equality Conclusion Appendix: Example of Objects as Coalgebras Appendix: Reduction of Mixed Pattern/Copattern Matching to Operators

Anton Setzer Pattern und Copattern Matching 25/ 38

slide-30
SLIDE 30

Patterns and Copatterns

Patterns and Copatterns

◮ We can define now functions by patterns and copatterns. ◮ Example define stream:

f n = n, n, n−1, n−1, . . . 0, 0, N, N, N −1, N −1, . . . 0, 0, N, N, N −1, N −1,

Anton Setzer Pattern und Copattern Matching 26/ 38

slide-31
SLIDE 31

Patterns and Copatterns

Patterns and Copatterns

f n = n, n, n−1, n−1, . . . 0, 0, N, N, N −1, N −1, . . . 0, 0, N, N, N −1, N −1, f : N → Stream f = ?

Anton Setzer Pattern und Copattern Matching 27/ 38

slide-32
SLIDE 32

Patterns and Copatterns

Patterns and Copatterns

f n = n, n, n−1, n−1, . . . 0, 0, N, N, N −1, N −1, . . . 0, 0, N, N, N −1, N −1, f : N → Stream f = ? Copattern matching on f : N → Stream: f : N → Stream f n = ?

Anton Setzer Pattern und Copattern Matching 27/ 38

slide-33
SLIDE 33

Patterns and Copatterns

Patterns and Copatterns

f n = n, n, n−1, n−1, . . . 0, 0, N, N, N −1, N −1, . . . 0, 0, N, N, N −1, N −1, f : N → Stream f n = ? Copattern matching on f n : Stream: f : N → Stream head (f n) = ? tail (f n) = ?

Anton Setzer Pattern und Copattern Matching 27/ 38

slide-34
SLIDE 34

Patterns and Copatterns

Patterns and Copatterns

f n = n, n, n−1, n−1, . . . 0, 0, N, N, N −1, N −1, . . . 0, 0, N, N, N −1, N −1, f : N → Stream f n = ? Solve first case, copattern match on second case: f : N → Stream head (f n) = n head (tail (f n)) = ? tail (tail (f n)) = ?

Anton Setzer Pattern und Copattern Matching 27/ 38

slide-35
SLIDE 35

Patterns and Copatterns

Patterns and Copatterns

f n = n, n, n−1, n−1, . . . 0, 0, N, N, N −1, N −1, . . . 0, 0, N, N, N −1, N −1, f : N → Stream f n = ? Solve second line, pattern match on n f : N → Stream head (f n) = n head (tail (f n)) = n tail (tail (f 0)) = ? tail (tail (f (S n))) = ?

Anton Setzer Pattern und Copattern Matching 27/ 38

slide-36
SLIDE 36

Patterns and Copatterns

Patterns and Copatterns

f n = n, n, n−1, n−1, . . . 0, 0, N, N, N −1, N −1, . . . 0, 0, N, N, N −1, N −1, f : N → Stream f n = ? Solve remaining cases f : N → Stream head (f n) = n head (tail (f n)) = n tail (tail (f 0)) = f N tail (tail (f (S n))) = f n

Anton Setzer Pattern und Copattern Matching 27/ 38

slide-37
SLIDE 37

Patterns and Copatterns

Results of paper in POPL (2013)

◮ Development of a recursive simply typed calculus (no termination

check).

◮ Allows to derive schemata for pattern/copattern matching. ◮ Proof that subject reduction holds.

t : A, t − → t′ implies t′ : A

Anton Setzer Pattern und Copattern Matching 28/ 38

slide-38
SLIDE 38

Codata types and Decidable Equality

From Codata to Coalgebras Algebras and Coalgebras Patterns and Copatterns Codata types and Decidable Equality Conclusion Appendix: Example of Objects as Coalgebras Appendix: Reduction of Mixed Pattern/Copattern Matching to Operators

Anton Setzer Pattern und Copattern Matching 29/ 38

slide-39
SLIDE 39

Codata types and Decidable Equality

Result

◮ We show that a decidable equality on Stream is incompatible with

the assumption ∀s.∃n, s′.s == cons n s′

Anton Setzer Pattern und Copattern Matching 30/ 38

slide-40
SLIDE 40

Codata types and Decidable Equality

Theorem Regarding Undecidability of Equality

Theorem

Assume the following:

◮ There exists a subset Stream ⊆ N, ◮ computable functions

head : Stream → N, tail : Stream → Stream,

◮ a decidable binary relation

==

  • n Stream which is an equivalence

relation,

◮ the possibility to define elements of Stream by primitive corecursion

based on primitive recursive functions f , g : N → N, such that the equalities related to guarded recursion hold.

◮ All operation above respect equality

== . Then the following does not hold: ∀s, s′ ∈ Stream.head s == head s′∧tail s == tail s′ → s == s′ (∗)

Anton Setzer Pattern und Copattern Matching 31/ 38

slide-41
SLIDE 41

Codata types and Decidable Equality

Consequences for Codata Approach

Remark

Condition (∗) is fulfilled if we have an operation cons : N → Stream → Stream preserving equalities s.t. ∀s : Stream.s == cons (head s) (tail s) So we cannot have a type theory with streams, decidable type checking and decidable equality on streams such that ∀s.∃n, s′.s == cons n s′ as assumed by the codata approach.

Anton Setzer Pattern und Copattern Matching 32/ 38

slide-42
SLIDE 42

Codata types and Decidable Equality

Proof of Theorem

◮ Assume we had the above. ◮ By

s ≈ n0 :: n1 :: n2 :: · · · nk :: s′ we mean the equations using head, tail expressing that s behaves as the stream indicated on the right hand side.

◮ Define by guarded recursion l : Stream

l ≈ 1 :: 1 :: 1 :: · · ·

Anton Setzer Pattern und Copattern Matching 33/ 38

slide-43
SLIDE 43

Codata types and Decidable Equality

Proof of Theorem

◮ For e code for a Turing machine define by guarded recursion based on

primitive recursion functions f , g s.t. if e terminates after n steps and returns result k then f e ≈ 0 :: 0 :: 0 :: · · · :: 0

  • n times

:: l g e ≈        0 :: 0 :: 0 :: · · · :: 0

  • n times

:: l if k = 0 0 :: 0 :: 0 :: · · · :: 0

  • n+1 times

:: l if k > 0

Anton Setzer Pattern und Copattern Matching 34/ 38

slide-44
SLIDE 44

Codata types and Decidable Equality

Proof of Theorem

f e ≈ 0 :: 0 :: 0 :: · · · :: 0

  • n times

:: l g e ≈        0 :: 0 :: 0 :: · · · :: 0

  • n times

:: l if k = 0 0 :: 0 :: 0 :: · · · :: 0

  • n+1 times

:: l if k > 0

◮ If e terminates after n steps with result 0 then

f e == g e

◮ If e terminates after n steps with result > 0 then

¬(f e == g e)

Anton Setzer Pattern und Copattern Matching 35/ 38

slide-45
SLIDE 45

Codata types and Decidable Equality

Proof of Theorem

◮ So

λe.(f e == g e) separates the TM with result 0 from those with result > 0.

◮ But these two sets are inseparable.

(See e.g. Odifreddi, 1999, p. 148, Theorem II.2.5.)

Anton Setzer Pattern und Copattern Matching 36/ 38

slide-46
SLIDE 46

Conclusion

From Codata to Coalgebras Algebras and Coalgebras Patterns and Copatterns Codata types and Decidable Equality Conclusion Appendix: Example of Objects as Coalgebras Appendix: Reduction of Mixed Pattern/Copattern Matching to Operators

Anton Setzer Pattern und Copattern Matching 37/ 38

slide-47
SLIDE 47

Conclusion

Conclusion

◮ Symmetry between

◮ algebras and coalgebras, ◮ iteration and coiteration, ◮ recursion and corecursion, ◮ patterns and copatterns.

◮ Final algebras are defined by construction,

coalgebras and function types by observation.

◮ Codata types make the implicit assumption

∀s : Stream.∃n, s′.s = cons n s′ which cannot be combined with a decidable equality.

◮ Weakly final coalgebras solve this problem.

◮ Wheras in attempts to use codata types sophisticated reduction rules

were used which depend on context, in the coalgebra approach we have recursion rules which can always be applied independent of context.

Anton Setzer Pattern und Copattern Matching 38/ 38

slide-48
SLIDE 48

Appendix: Example of Objects as Coalgebras

From Codata to Coalgebras Algebras and Coalgebras Patterns and Copatterns Codata types and Decidable Equality Conclusion Appendix: Example of Objects as Coalgebras Appendix: Reduction of Mixed Pattern/Copattern Matching to Operators

Anton Setzer Pattern und Copattern Matching 39/ 38

slide-49
SLIDE 49

Appendix: Example of Objects as Coalgebras

Example

class Angle { int angle; Angle(int myangle) {angle = myangle mod 360;}; void set(int myangle){angle = myangle mod 360; return;}; int get(){return angle;}; } Interface of Angle (which is the true type) can be given as coalg Angle where set : Angle → N → Angle get : Angle → N

Anton Setzer Pattern und Copattern Matching 40/ 38

slide-50
SLIDE 50

Appendix: Example of Objects as Coalgebras

Example

class Angle { int angle; Angle(int myangle){angle = myangle mod 360;}; void set(int myangle){angle = myangle mod 360; return;}; int get(){return angle;}; } Implementation of the methods can be given as (argument N corresponds to the instance variable): angleImpl : N → Angle set (angleImpl n) m = angleImpl (m mod 360) get (angleImpl n) = n

Anton Setzer Pattern und Copattern Matching 41/ 38

slide-51
SLIDE 51

Appendix: Example of Objects as Coalgebras

Example

class Angle { int angle; Angle(int myangle){angle = myangle mod 360;}; void set(int myangle){angle = myangle mod 360; return;}; int get(){return angle;}; } } Implementation of the constructor can be given as createAngle : N → Angle createAngle n = angleImpl n

Anton Setzer Pattern und Copattern Matching 42/ 38

slide-52
SLIDE 52

Appendix: Reduction of Mixed Pattern/Copattern Matching to Operators

From Codata to Coalgebras Algebras and Coalgebras Patterns and Copatterns Codata types and Decidable Equality Conclusion Appendix: Example of Objects as Coalgebras Appendix: Reduction of Mixed Pattern/Copattern Matching to Operators

Anton Setzer Pattern und Copattern Matching 43/ 38

slide-53
SLIDE 53

Appendix: Reduction of Mixed Pattern/Copattern Matching to Operators

Operators for Primitive (Co)Recursion

PN,A : A → (N → A → A) → N → A PN,A step0 stepS 0 = step0 PN,A step0 stepS (S n) = stepS n (PN,A step0 stepS n) coPStream,A : (A → N) → (A → (Stream + A)) → A → Stream head (coPStream,A stephead steptail a) = stephead a tail (coPStream,A stephead steptail a) = caseStream,A,Stream id (coPStream,A stephead steptail) (steptail a)

Anton Setzer Pattern und Copattern Matching 44/ 38

slide-54
SLIDE 54

Appendix: Reduction of Mixed Pattern/Copattern Matching to Operators

Operators for full/primitive (co)recursion

RN,A : ((N → A) → A) → ((N → A) → N → A) → N → A RN,A step0 stepS 0 = step0 (RN,A step0 stepS) RN,A step0 stepS (S n) = stepS (RN,A step0 stepS) n coRStream,A : ((A → Stream) → A → N) → ((A → Stream) → A → Stream) → Stream head (coRStream,A stephead steptail a) = stephead (coRStream,A stephead steptail) a tail (coRStream,A stephead steptail a) = steptail (coRStream,A stephead steptail) a

Anton Setzer Pattern und Copattern Matching 45/ 38

slide-55
SLIDE 55

Appendix: Reduction of Mixed Pattern/Copattern Matching to Operators

Consider Example from above

f : N → Stream head (f n) = n head (tail (f n)) = n tail (tail (f 0)) = f N tail (tail (f (S n))) = f n This example can be reduced to primitive (co)recursion. Step 1: Following the development of the (co)pattern matching definition, unfold it into simultaneous non-nested (co)pattern matching definitions.

Anton Setzer Pattern und Copattern Matching 46/ 38

slide-56
SLIDE 56

Appendix: Reduction of Mixed Pattern/Copattern Matching to Operators

Step 1: Unnesting of Nested (Co)Pattern Matching

We follow the steps in the pattern matching: We start with f : N → Stream head (f n) = n tail (f n) = ?

Anton Setzer Pattern und Copattern Matching 47/ 38

slide-57
SLIDE 57

Copattern matching on tail (f n): f : N → Stream head (f n) = n head (tail (f n) = n tail (tail (f n) = ? corresponds to f : N → Stream head (f n) = n tail (f n) = g n g : N → Stream (head (tail (f n)) =) head (g n) = n (tail (tail (f n)) =) tail (g n) = ?

slide-58
SLIDE 58

Pattern matching on tail (tail (f n)): f : N → Stream head (f n) = n head (tail (f n) = n tail (tail (f 0) = f N tail (tail (f (S n)) = f n corresponds to f : N → Stream head (f n) = n tail (f n) = g n g : N → Stream (head (tail (f n)) =) head (g n) = n (tail (tail (f n)) =) tail (g n) = k n k : N → Stream (tail (tail (f 0)) =) k = f N (tail (tail (f (S n))) =) k (S n) = f n

slide-59
SLIDE 59

Appendix: Reduction of Mixed Pattern/Copattern Matching to Operators

Step 2: Reduction to Primitive (Co)recursion

◮ This can now easily be reduced to full (co)recursion. ◮ In this example we can reduce it to primitive (co)recursion. ◮ First combine f , g into one function f + g.

Anton Setzer Pattern und Copattern Matching 50/ 38

slide-60
SLIDE 60

f : N → Stream f n = (f + g) (f n) (f + g) : (f(N) + g(N)) → Stream head ((f + g) (f n)) = n head ((f + g) (g n)) = n tail ((f + g) (f n)) = (f + g) (g n) tail ((f + g) (f n)) = k n k : N → Stream k 0 = (f + g) (f N) k (S n) = (f + g) (f n)

slide-61
SLIDE 61

Appendix: Reduction of Mixed Pattern/Copattern Matching to Operators

Unfolding of the Pattern Matchings

◮ The call of k has result always of the form (f + g)( fbf n)).

So we can replace the recursive call k n by (f + g)(f (k′ n)).

Anton Setzer Pattern und Copattern Matching 52/ 38

slide-62
SLIDE 62

f : N → Stream f n = (f + g) (f n) (f + g) : (f(N) + g(N)) → Stream head ((f + g) (f n)) = n head ((f + g) (g n)) = n tail ((f + g) (f n)) = (f + g) (g n) tail ((f + g) (f n)) = (f + g) (f (k′ n)) k′ : N → N k 0 = N k (S n) = n

slide-63
SLIDE 63

Appendix: Reduction of Mixed Pattern/Copattern Matching to Operators

Unfolding of the Pattern Matchings

◮ (f + g) can be defined by primitive corecursion. ◮ k′ can be defined by primitive recursion.

Anton Setzer Pattern und Copattern Matching 54/ 38

slide-64
SLIDE 64

f : N → Stream f n = (f + g) (f n) (f + g) : (f(N) + g(N)) → Stream (f + g) = coPStream,(f(N)+g(N) (λx.caser(x) of (f n) − → n (g n) − → n) (λx.caser(x) of (f n) − → g n (g n) − → f (k′ n)) k′ : N → N k′ = PN,N N (λn, ih.n)

slide-65
SLIDE 65

Appendix: Reduction of Mixed Pattern/Copattern Matching to Operators

Reduction to Primitive (Co)Recursion

◮ The case distinction can be trivially replaced by the case distinction

  • perator.

Anton Setzer Pattern und Copattern Matching 56/ 38

slide-66
SLIDE 66

f : N → Stream f n = (f + g) (f n) (f + g) : (f(N) + g(N)) → Stream (f + g) = coPStream,f(N)+g(N) (casef(N)+g(N) id id) (casef(N)+g(N) g (f ◦ k′)) k′ : N → N k′ = PN,N N (λn, ih.n)