Unnesting of Copatterns RTA-TLCA, 15 July 2014 Anton Setzer - - PowerPoint PPT Presentation

unnesting of copatterns
SMART_READER_LITE
LIVE PREVIEW

Unnesting of Copatterns RTA-TLCA, 15 July 2014 Anton Setzer - - PowerPoint PPT Presentation

Unnesting of Copatterns RTA-TLCA, 15 July 2014 Anton Setzer Andreas Abel Brigitte Pientka David Thibodeau Swansea Gothenburg Montreal Montreal UK Sweden Canada Canada Setzer, Abel, Pientka, Thibodeau Unnesting of Copatterns 1/ 30


slide-1
SLIDE 1

Unnesting of Copatterns

RTA-TLCA, 15 July 2014 Anton Setzer Swansea UK Andreas Abel Gothenburg Sweden Brigitte Pientka Montreal Canada David Thibodeau Montreal Canada

Setzer, Abel, Pientka, Thibodeau Unnesting of Copatterns 1/ 30

slide-2
SLIDE 2

Pattern and Copattern Matching Unnesting of Copatterns/Patterns Proof of Conservatity and Preservation of SN/WN

Setzer, Abel, Pientka, Thibodeau Unnesting of Copatterns 2/ 30

slide-3
SLIDE 3

Pattern and Copattern Matching

Pattern and Copattern Matching Unnesting of Copatterns/Patterns Proof of Conservatity and Preservation of SN/WN

Setzer, Abel, Pientka, Thibodeau Unnesting of Copatterns 3/ 30

slide-4
SLIDE 4

Pattern and Copattern Matching

Natural Numbers

Syntax for Algebraic Data Types in Paper Nat := µX.zero 1 | suc X Introduction Rule (All constructors will have exactly one argument) zero : 1 → Nat suc : Nat → Nat Elimination Rule (Pattern Matching) pred : Nat → Nat pred (zero x) = ? pred (suc n) = ? Full recursion allowed (normalisation for individual terms see later)

Setzer, Abel, Pientka, Thibodeau Unnesting of Copatterns 4/ 30

slide-5
SLIDE 5

Pattern and Copattern Matching

Nested Patterns and CC-Pattern-Sets

pred : Nat → Nat pred (zero x) = ? pred (suc n) = ? Pattern matching on 1 (containing ()) yields the nested pattern pred : Nat → Nat pred (zero ()) = ? pred (suc n) = ? which formally is the coverage complete (cc) pattern set pred : Nat → Nat ⊳ | ( · ⊢ pred (zero ()) : Nat) (n : Nat ⊢ pred (suc n) : Nat)

Setzer, Abel, Pientka, Thibodeau Unnesting of Copatterns 5/ 30

slide-6
SLIDE 6

Pattern and Copattern Matching

Coverage Complete Rule Sets (CC-Rule Sets)

After full pattern derived we fill in the “?” pred : Nat → Nat pred (zero ()) = zero () pred (suc n) = n corresponds to the coverage complete (cc) rule set pred : Nat → Nat ⊳ | ( · ⊢ pred (zero ()) − → zero () : Nat) (n : Nat ⊢ pred (suc n) − → n : Nat)

Setzer, Abel, Pientka, Thibodeau Unnesting of Copatterns 6/ 30

slide-7
SLIDE 7

Pattern and Copattern Matching

Stream

Stream := νX.{head : Nat, tail : X} (In paper called StrN)

· · · s2 s3 n2 n3 n1 s1 .tail .tail .tail .head .head .head

Setzer, Abel, Pientka, Thibodeau Unnesting of Copatterns 7/ 30

slide-8
SLIDE 8

Pattern and Copattern Matching

Stream

Elimination Rule If s : Stream then s .head : Nat s .tail : Stream .head, .tail treated like application. Introduction Rule (Copattern Matching) inc : Nat → Stream inc n .head = n inc n .tail = inc (n + 1) Informally inc n = n, n + 1, n + 2, . . .

Setzer, Abel, Pientka, Thibodeau Unnesting of Copatterns 8/ 30

slide-9
SLIDE 9

Pattern and Copattern Matching

CC-Rule/Pattern Set

inc : Nat → Stream inc n .head = n inc n .tail = inc (n + 1) This corresponds to the cc-pattern-set inc : Nat → Stream ⊳ | (n : Nat ⊢ inc n .head : Nat) (n : Nat ⊢ inc n .tail : Stream) and cc-rule-set inc : Nat → Stream ⊳ | (n : Nat ⊢ inc n .head − → n : Nat) (n : Nat ⊢ inc n .tail − → inc (n + 1) : Stream)

Setzer, Abel, Pientka, Thibodeau Unnesting of Copatterns 9/ 30

slide-10
SLIDE 10

Pattern and Copattern Matching

cyc n

Let N be fixed. For n we define a stream which is informally given as cyc n = n, n − 1, n − 2, . . . , 0, N, N − 1, N − 2, . . . , 0, N, N − 1, . . .

cyc 0 · · · N − 1 cyc N cyc N − 1 N .tail .tail .tail .tail .head .head .head

Setzer, Abel, Pientka, Thibodeau Unnesting of Copatterns 10/ 30

slide-11
SLIDE 11

Pattern and Copattern Matching

Development of cyc

(Paper contains rules for deriving cc-pattern/rule-sets.) The simplest pattern matching is by itself: cyc : Nat → Stream ⊳ | ( · ⊢ cyc : Nat → Stream) Copattern matching for functions is application: cyc : Nat → Stream ⊳ | (n : Nat ⊢ cyc n : Stream) Copattern matching on Stream yields: cyc : Nat → Stream ⊳ | (n : Nat ⊢ cyc n .head : Nat) (n : Nat ⊢ cyc n .tail : Stream)

Setzer, Abel, Pientka, Thibodeau Unnesting of Copatterns 11/ 30

slide-12
SLIDE 12

Pattern and Copattern Matching

Development of cyc (Cont.)

Pattern matching on Nat yields: cyc : Nat → Stream ⊳ | (n : Nat ⊢ cyc n .head : Nat) (x : 1 ⊢ cyc (zero x) .tail : Stream) (n : Nat ⊢ cyc (suc n) .tail : Stream) Pattern matching on 1 yields: cyc : Nat → Stream ⊳ | (n : Nat ⊢ cyc n .head : Nat) ( · ⊢ cyc (zero ()) .tail : Stream) (n : Nat ⊢ cyc (suc n) .tail : Stream) By adding results we obtain a cc-rule-set: cyc : Nat → Stream ⊳| (n : Nat ⊢ cyc n .head − → n :Nat) ( · ⊢ cyc (zero ()) .tail − →cyc N :Stream) (n : Nat ⊢ cyc (suc n) .tail − →cyc n : Stream)

Setzer, Abel, Pientka, Thibodeau Unnesting of Copatterns 12/ 30

slide-13
SLIDE 13

Unnesting of Copatterns/Patterns

Pattern and Copattern Matching Unnesting of Copatterns/Patterns Proof of Conservatity and Preservation of SN/WN

Setzer, Abel, Pientka, Thibodeau Unnesting of Copatterns 13/ 30

slide-14
SLIDE 14

Unnesting of Copatterns/Patterns

Unnesting of cyc

cyc : Nat → Stream ⊳| (n : Nat ⊢ cyc n .head − → n :Nat) ( · ⊢ cyc (zero ()) .tail − → cyc N :Stream) (n : Nat ⊢ cyc (suc n) .tail − → cyc n : Stream) We unnest the last step (pattern matching on 1) and delegate it to a new function g2 cyc : Nat → Stream ⊳| (n : Nat ⊢ cyc n .head − → n :Nat) (x : 1 ⊢ cyc (zero x) .tail − → g2 x :Stream) (n : Nat ⊢ cyc (suc n) .tail − → cyc n : Stream) g2 : 1 → Stream ⊳| ( · ⊢ g2 () − → cyc N : Stream)

Setzer, Abel, Pientka, Thibodeau Unnesting of Copatterns 14/ 30

slide-15
SLIDE 15

Unnesting of Copatterns/Patterns

Unnesting of cyc (Cont)

cyc : Nat → Stream ⊳| (n : Nat ⊢ cyc n .head − → n :Nat) (x : 1 ⊢ cyc (zero x) .tail − → g2 x :Stream) (n : Nat ⊢ cyc (suc n) .tail − → cyc n : Stream) g2 : 1 → Stream ⊳| ( · ⊢ g2 () − → cyc N : Stream) Now we unnest pattern matching on x : Nat and delegate it to a new function g1: cyc : Nat → Stream ⊳| (n : Nat ⊢ cyc n .head − → n : Nat) (n : Nat ⊢ cyc n .tail − → g1 n : Stream) g1 : Nat → Stream ⊳| (x : 1 ⊢ g1 (zero x) − → g2 x : Stream) (n : Nat ⊢ g1 (suc n) − → cyc n : Stream) g2 : 1 → Stream ⊳| ( · ⊢ g2 () − → cyc N : Stream)

Setzer, Abel, Pientka, Thibodeau Unnesting of Copatterns 15/ 30

slide-16
SLIDE 16

Unnesting of Copatterns/Patterns

Simple Pattern

◮ End result is simple pattern:

◮ There is at most one proper pattern/copattern step

which is the last one.

Setzer, Abel, Pientka, Thibodeau Unnesting of Copatterns 16/ 30

slide-17
SLIDE 17

Proof of Conservatity and Preservation of SN/WN

Pattern and Copattern Matching Unnesting of Copatterns/Patterns Proof of Conservatity and Preservation of SN/WN

Setzer, Abel, Pientka, Thibodeau Unnesting of Copatterns 17/ 30

slide-18
SLIDE 18

Proof of Conservatity and Preservation of SN/WN

Programs

Definition

(a) A program P is given by constants with their types and a cc-rule-set for each constant (referring to terms in the same language). (b) A program P′ extends P if the it contains all the constants of P with the same types (but not necessarily the same cc-rule-sets).

Setzer, Abel, Pientka, Thibodeau Unnesting of Copatterns 18/ 30

slide-19
SLIDE 19

Proof of Conservatity and Preservation of SN/WN

Conservative Extensions, Preservation of SN, WN

Definition

Let P′ be a program extending P. (a) P′ is a conservative extension of P iff ∀t, t′ ∈ TermP.t − →∗

P t′ ⇔ t −

→∗

P′ t′

(b) P′ preserves strong normalisation (SN) iff ∀t ∈ TermP.t ∈ SN(P) ⇔ t ∈ SN(P′) (c) P′ preserves weak normalisation (WN) iff ∀t ∈ TermP.t ∈ WN(P) ⇔ t ∈ WN(P′)

Setzer, Abel, Pientka, Thibodeau Unnesting of Copatterns 19/ 30

slide-20
SLIDE 20

Proof of Conservatity and Preservation of SN/WN

Conservative Extension

t1 ∈ TermP′ t2 ∈ TermP′ t ∈ TermP P′ P′ tn ∈ TermP′ P′ t′ ∈ TermP P′ P′ · · ·

Setzer, Abel, Pientka, Thibodeau Unnesting of Copatterns 20/ 30

slide-21
SLIDE 21

Proof of Conservatity and Preservation of SN/WN

Conservative Extension

t1 ∈ TermP′ t2 ∈ TermP′ t ∈ TermP P′ P′ tn ∈ TermP′ P′ t′ ∈ TermP P′ P′ · · · P · · · P P P P ∃t′

1 ∈ TermP

∃t′

2 ∈ TermP

∃t′

m ∈ TermP

Setzer, Abel, Pientka, Thibodeau Unnesting of Copatterns 20/ 30

slide-22
SLIDE 22

Proof of Conservatity and Preservation of SN/WN

Preservation of ¬SN

t1 ∈ TermP′ · · · t ∈ SN(P′) t2 ∈ TermP′ t3 ∈ TermP′

Setzer, Abel, Pientka, Thibodeau Unnesting of Copatterns 21/ 30

slide-23
SLIDE 23

Proof of Conservatity and Preservation of SN/WN

Preservation of ¬SN

t1 ∈ TermP′ · · · t ∈ SN(P′) t2 ∈ TermP′ t3 ∈ TermP′ ∃t′

1 ∈ TermP

∃t′

2 ∈ TermP

∃t′

2 ∈ TermP

· · · t ∈ SN(P)

Setzer, Abel, Pientka, Thibodeau Unnesting of Copatterns 21/ 30

slide-24
SLIDE 24

Proof of Conservatity and Preservation of SN/WN

Main Theorem

Theorem

Let P be a program. There exist an extension of P which is

◮ conservative, ◮ preserves SN, ◮ preserves WN ◮ and has only simple patterns.

Setzer, Abel, Pientka, Thibodeau Unnesting of Copatterns 22/ 30

slide-25
SLIDE 25

Proof of Conservatity and Preservation of SN/WN

Proof of Theorem General Methodology

◮ P′ is obtained from P by replacing last step of an nested pattern until

all patterns are simple.

◮ Show

◮ reduction of one non-nested reduction step

yields a conservative extension preserving SN/WN.

◮ Illustration by considering the last step in the example above.

Setzer, Abel, Pientka, Thibodeau Unnesting of Copatterns 23/ 30

slide-26
SLIDE 26

Proof of Conservatity and Preservation of SN/WN

Last Step in Example

Program P (g2 omitted since unchanged): cyc : Nat → Stream ⊳| (n : Nat ⊢ cyc n .head − → n :Nat) (x : 1 ⊢ cyc (zero x) .tail − → g2 x :Stream) (n : Nat ⊢ cyc (suc n) .tail − → cyc n : Stream) Program P′: cyc : Nat → Stream ⊳| (n : Nat ⊢ cyc n .head − → n : Nat) (n : Nat ⊢ cyc n .tail − → g1 n : Stream) g1 : Nat → Stream ⊳| (x : 1 ⊢ g1 (zero x) − → g2 x : Stream) (n : Nat ⊢ g1 (suc n) − → cyc n : Stream)

Setzer, Abel, Pientka, Thibodeau Unnesting of Copatterns 24/ 30

slide-27
SLIDE 27

Proof of Conservatity and Preservation of SN/WN

Proof of Theorem - First easy steps

◮ Easy to see for t, t′ ∈ TermP

t − →P t′ ⇒ t − →≥1

P′ t′

In example cyc (zero x) .head − →P′ g1 (zero x) − →P′ g2 x cyc (suc n) .head − →P′ g1 (suc n) − →P′ cyc n

◮ Implies for t, t′ ∈ TermP

t − →∗

P t′ ⇒ t −

→∗

P′ t′

and t ∈ SN(P) ⇒ t ∈ SN(P′)

Setzer, Abel, Pientka, Thibodeau Unnesting of Copatterns 25/ 30

slide-28
SLIDE 28

Proof of Conservatity and Preservation of SN/WN

Back Translation and Conservativity

◮ Let (in above example)

Good = {t ∈ TermP′ | g1 always applied at least once }

◮ Let the back translation be

int : Good → TermP int(t) = result of replacing in t subterms (g1 s) by (cyc s .tail)

◮ We have

∀t ∈ TermP.t ∈ Good ∧ int(t) = t Good closed under − →P′ ∀t, t′ ∈ TermP′.t − →P′ t′ ⇒ int(t) − →∗

P int(t′) ◮ Therefore for t, t′ ∈ TermP

t − →∗

P′ t′ ⇒ t = int(t) −

→∗

P int(t′) = t′

Setzer, Abel, Pientka, Thibodeau Unnesting of Copatterns 26/ 30

slide-29
SLIDE 29

Proof of Conservatity and Preservation of SN/WN

Preservation of Normalisation

◮ We might have

t − →P′ t′ but int(t) = int(t′)

◮ However, there are no infinitely long chains leaving int(t) unchanged. ◮ Therefore we obtain for t ∈ TermP′

t ∈ SN(P′) ⇒ t ∈ SN(P)

◮ Preservation of WN (thanks to referee!):

t ∈ WN(P)⇒ t − →∗

P t′ ∈ NF(P)

⇒ t − →∗

P t′ ∈ SN(P)

⇒ t − →∗

P′ t′ ∈ SN(P′)

⇒ t ∈ WN(P′) Similarly in other direction (using back translation).

Setzer, Abel, Pientka, Thibodeau Unnesting of Copatterns 27/ 30

slide-30
SLIDE 30

Proof of Conservatity and Preservation of SN/WN

Conclusion

Algebras Coalgebras defined by defined by introduction rules elimination rules elimination rules introduction rules given by given by pattern matching copattern matching

Setzer, Abel, Pientka, Thibodeau Unnesting of Copatterns 28/ 30

slide-31
SLIDE 31

Proof of Conservatity and Preservation of SN/WN

Conclusion

◮ Calculus for deriving nested coverage complete rule sets. ◮ Reduction of nested (co)patterns to simple (co)patterns. ◮ Proof of correctness.

◮ Conservative extension, ◮ preservation of SN, ◮ preservation of WN. Setzer, Abel, Pientka, Thibodeau Unnesting of Copatterns 29/ 30

slide-32
SLIDE 32

Proof of Conservatity and Preservation of SN/WN

Future Work

◮ Reduction to combinators (writing up phase). ◮ Having conservative extension, preservation of SN and of WN sounds

ad hoc.

◮ What is a general notion of properties to be preserved? ◮ Probably all formulas expressible in a certain language to be defined.

◮ Use for compilaton of copatterns. ◮ Development of termination checker based on principles terminating

programs are those reducible to primitive corecursion.

Setzer, Abel, Pientka, Thibodeau Unnesting of Copatterns 30/ 30