Equational Reasoning with Applicative Functors Andreas Lochbihler - - PowerPoint PPT Presentation

equational reasoning with applicative functors
SMART_READER_LITE
LIVE PREVIEW

Equational Reasoning with Applicative Functors Andreas Lochbihler - - PowerPoint PPT Presentation

Equational Reasoning with Applicative Functors Andreas Lochbihler Joshua Schneider Institute of Information Security Equational Reasoning with Applicative Functors Andreas Lochbihler Joshua Schneider Institute of Information Security model


slide-1
SLIDE 1

Equational Reasoning with Applicative Functors

Andreas Lochbihler Joshua Schneider

Institute of Information Security

slide-2
SLIDE 2

Equational Reasoning with Applicative Functors

Andreas Lochbihler Joshua Schneider

Institute of Information Security

state probabilities

  • error

non-determinism

1 2 3 4 · · ·

streams model effects

slide-3
SLIDE 3

Equational Reasoning with Applicative Functors

Andreas Lochbihler Joshua Schneider

Institute of Information Security

state probabilities

  • error

non-determinism

1 2 3 4 · · ·

streams model effects k g f h f (g ) = h (k )

slide-4
SLIDE 4

Equational Reasoning with Applicative Functors

Andreas Lochbihler Joshua Schneider

Institute of Information Security

state probabilities

  • error

non-determinism

1 2 3 4 · · ·

streams model effects k g f h pure f ⋄ (g ) = pure h ⋄ (k )

slide-5
SLIDE 5

Equational Reasoning with Applicative Functors

Andreas Lochbihler Joshua Schneider

Institute of Information Security

state probabilities

  • error

non-determinism

1 2 3 4 · · ·

streams model effects k g f h pure f ⋄ (g ) = pure h ⋄ (k )

don’t care

slide-6
SLIDE 6

Contributions

◮ Isabelle/HOL package for reasoning about applicative effects

functor registration ⋄ ⋄x = x = proof tactic classify effects

◮ Meta theory formalised and algorithms verified ◮ Used in several examples and case studies

  • A. Lochbihler (ETH Zurich)

ITP 2016 6 / 35

slide-7
SLIDE 7

Task: Label a binary tree with distinct numbers!

c b q a d 0 1 2 3 4 lbl

datatype α tree = L α | N (α tree) (α tree)

Example suggested by G. Hutton, D. Fulger: Reasoning about effects: seeing the wood through the trees. TFP 2008

  • A. Lochbihler (ETH Zurich)

ITP 2016 7 / 35

slide-8
SLIDE 8

Task: Label a binary tree with distinct numbers!

c b q a d 0 1 2 3 4 lbl

datatype α tree = L α | N (α tree) (α tree)

lbl :: α tree ⇒ nat tree

Example suggested by G. Hutton, D. Fulger: Reasoning about effects: seeing the wood through the trees. TFP 2008

  • A. Lochbihler (ETH Zurich)

ITP 2016 8 / 35

slide-9
SLIDE 9

Task: Label a binary tree with distinct numbers!

c b q a d 0 1 2 3 4 lbl

datatype α tree = L α | N (α tree) (α tree)

state lbl :: α tree ⇒ nat tree state

where

α state = nat ⇒ α × nat monadic

α M = α state return :: α ⇒ α M (> > =) :: α M ⇒ (α ⇒ β M) ⇒ β M lbl (L ) = fresh > > = λx′. return (L x′) lbl (N l r) = lbl l > > = λl′. lbl r > > = λr ′. return (N l′ r ′)

Example suggested by G. Hutton, D. Fulger: Reasoning about effects: seeing the wood through the trees. TFP 2008

  • A. Lochbihler (ETH Zurich)

ITP 2016 9 / 35

slide-10
SLIDE 10

Task: Label a binary tree with distinct numbers!

c b q a d 0 1 2 3 4 lbl

datatype α tree = L α | N (α tree) (α tree)

state lbl :: α tree ⇒ nat tree state

where

α state = nat ⇒ α × nat monadic

α M = α state

applicative

α F = α state return :: α ⇒ α M (> > =) :: α M ⇒ (α ⇒ β M) ⇒ β M pure :: α ⇒ α F (⋄) :: (α ⇒ β) F ⇒ α F ⇒ β F lbl (L ) = fresh > > = λx′. return (L x′) lbl (N l r) = lbl l > > = λl′. lbl r > > = λr ′. return (N l′ r ′) lbl (L ) = pure L ⋄ fresh lbl (N l r) = pure N ⋄ lbl l ⋄ lbl r

Example suggested by G. Hutton, D. Fulger: Reasoning about effects: seeing the wood through the trees. TFP 2008

  • A. Lochbihler (ETH Zurich)

ITP 2016 10 / 35

slide-11
SLIDE 11

Labelling trees and lists

c a q 0 1 2 [c, a, q] [0, 1, 2]

lbl lbl′ leaves pure leaves lbl′ :: α list ⇒ nat list state lbl′ [ ] = pure [ ] lbl′ ( · xs) = pure (·) ⋄ fresh ⋄ lbl′ xs leaves :: α tree ⇒ α list leaves (L x) = x · [ ] leaves (N l r) = leaves l + + leaves r

  • A. Lochbihler (ETH Zurich)

ITP 2016 11 / 35

slide-12
SLIDE 12

Labelling trees and lists

c a q 0 1 2 [c, a, q] [0, 1, 2]

lbl lbl′ leaves pure leaves lbl′ :: α list ⇒ nat list state lbl′ [ ] = pure [ ] lbl′ ( · xs) = pure (·) ⋄ fresh ⋄ lbl′ xs leaves :: α tree ⇒ α list leaves (L x) = x · [ ] leaves (N l r) = leaves l + + leaves r

Lemma: pure leaves ⋄ lbl t = lbl′ (leaves t) Proof by induction on t. Case L x: pure leaves ⋄ lbl (L x) = lbl′ (leaves (L x))

slide-13
SLIDE 13

Labelling trees and lists

c a q 0 1 2 [c, a, q] [0, 1, 2]

lbl lbl′ leaves pure leaves lbl′ :: α list ⇒ nat list state lbl′ [ ] = pure [ ] lbl′ ( · xs) = pure (·) ⋄ fresh ⋄ lbl′ xs leaves :: α tree ⇒ α list leaves (L x) = x · [ ] leaves (N l r) = leaves l + + leaves r

Lemma: pure leaves ⋄ lbl t = lbl′ (leaves t) Proof by induction on t. Case L x: pure leaves ⋄ lbl (L x) = lbl′ (leaves (L x)) pure leaves ⋄ (pure L ⋄ fresh) = pure (·) ⋄ fresh ⋄ pure [ ] ∀x. leaves ( L x ) = (·) x [ ]

  • A. Lochbihler (ETH Zurich)

ITP 2016 13 / 35

slide-14
SLIDE 14

Labelling trees and lists

c a q 0 1 2 [c, a, q] [0, 1, 2]

lbl lbl′ leaves pure leaves lbl′ :: α list ⇒ nat list state lbl′ [ ] = pure [ ] lbl′ ( · xs) = pure (·) ⋄ fresh ⋄ lbl′ xs leaves :: α tree ⇒ α list leaves (L x) = x · [ ] leaves (N l r) = leaves l + + leaves r

Lemma: pure leaves ⋄ lbl t = lbl′ (leaves t) Proof by induction on t. Case L x: pure leaves ⋄ lbl (L x) = lbl′ (leaves (L x)) pure leaves ⋄ (pure L ⋄ fresh) = pure (·) ⋄ fresh ⋄ pure [ ]

∀x. leaves ( L x ) = (·) x [ ]

holds by the applicative laws

  • A. Lochbihler (ETH Zurich)

ITP 2016 14 / 35

slide-15
SLIDE 15

Labelling trees and lists

c a q 0 1 2 [c, a, q] [0, 1, 2]

lbl lbl′ leaves pure leaves lbl′ :: α list ⇒ nat list state lbl′ [ ] = pure [ ] lbl′ ( · xs) = pure (·) ⋄ fresh ⋄ lbl′ xs leaves :: α tree ⇒ α list leaves (L x) = x · [ ] leaves (N l r) = leaves l + + leaves r

Lemma: pure leaves ⋄ lbl t = lbl′ (leaves t) Proof by induction on t. Case L x: pure leaves ⋄ lbl (L x) = lbl′ (leaves (L x)) pure leaves ⋄ (pure L ⋄ fresh) = pure (·) ⋄ fresh ⋄ pure [ ]

∀x. leaves ( L x ) = (·) x [ ]

holds by the applicative laws apply applicative lifting

  • A. Lochbihler (ETH Zurich)

ITP 2016 15 / 35

slide-16
SLIDE 16

Lifting equations over applicative functors

= pure leaves ⋄ (pure L ⋄ fresh) pure (·) ⋄ fresh ⋄ pure [ ] = ∀x. leaves (L x) x · [ ]

[Hinze 2010]

  • A. Lochbihler (ETH Zurich)

ITP 2016 16 / 35

slide-17
SLIDE 17

Lifting equations over applicative functors

= pure leaves ⋄ (pure L ⋄ fresh) pure (·) ⋄ fresh ⋄ pure [ ] = ∀x. leaves (L x) x · [ ] ML

λ → ∀

=

I s a b e l l e

β α

kernel

syntactic formalisation

λ → ∀

=

I s a b e l l e

β α

HOL

f

  • l

l

  • w

s

[Hinze 2010]

  • A. Lochbihler (ETH Zurich)

ITP 2016 17 / 35

slide-18
SLIDE 18

Lifting equations over applicative functors

= pure leaves ⋄ (pure L ⋄ fresh) pure (·) ⋄ fresh ⋄ pure [ ] = pure (λx. leaves (L x)) ⋄ fresh pure (λx. x · [ ]) ⋄ fresh = ∀x. leaves (L x) x · [ ]

  • 1. Convert to canonical form

[Hinze 2010]

  • A. Lochbihler (ETH Zurich)

ITP 2016 18 / 35

slide-19
SLIDE 19

Lifting equations over applicative functors

= pure leaves ⋄ (pure L ⋄ fresh) pure (·) ⋄ fresh ⋄ pure [ ] = pure (λx. leaves (L x)) ⋄ fresh pure (λx. x · [ ]) ⋄ fresh = ∀x. leaves (L x) x · [ ]

  • 1. Convert to canonical form

applicative expression → pure f ⋄ x1 ⋄ x2 ⋄ . . . ⋄ xn Canonical form

[McBride, Paterson] [Hinze 2010]

  • A. Lochbihler (ETH Zurich)

ITP 2016 19 / 35

slide-20
SLIDE 20

Lifting equations over applicative functors

pure function

  • paque arguments
  • paque arguments
  • paque arguments
  • paque arguments
  • paque arguments

= pure leaves ⋄ (pure L ⋄ fresh) pure (·) ⋄ fresh ⋄ pure [ ] = pure (λx. leaves (L x)) ⋄ fresh pure (λx. x · [ ]) ⋄ fresh = ∀x. leaves (L x) x · [ ]

  • 1. Convert to canonical form

applicative expression → pure f ⋄ x1 ⋄ x2 ⋄ . . . ⋄ xn Canonical form

[McBride, Paterson] [Hinze 2010]

  • A. Lochbihler (ETH Zurich)

ITP 2016 20 / 35

slide-21
SLIDE 21

Lifting equations over applicative functors

pure function

  • paque arguments
  • paque arguments
  • paque arguments
  • paque arguments
  • paque arguments

= pure leaves ⋄ (pure L ⋄ fresh) pure (·) ⋄ fresh ⋄ pure [ ] = pure (λx. leaves (L x)) ⋄ fresh pure (λx. x · [ ]) ⋄ fresh = ∀X. pure (λx. leaves (L x)) ⋄ X pure (λx. x · [ ]) ⋄ X = ∀x. leaves (L x) x · [ ]

  • 1. Convert to canonical form
  • 2. Generalise opaque arguments

applicative expression → pure f ⋄ x1 ⋄ x2 ⋄ . . . ⋄ xn Canonical form

[McBride, Paterson] [Hinze 2010]

  • A. Lochbihler (ETH Zurich)

ITP 2016 21 / 35

slide-22
SLIDE 22

Lifting equations over applicative functors

pure function

  • paque arguments
  • paque arguments
  • paque arguments
  • paque arguments
  • paque arguments

= pure leaves ⋄ (pure L ⋄ fresh) pure (·) ⋄ fresh ⋄ pure [ ] = pure (λx. leaves (L x)) ⋄ fresh pure (λx. x · [ ]) ⋄ fresh = ∀X. pure (λx. leaves (L x)) ⋄ X pure (λx. x · [ ]) ⋄ X = ∀X. pure (λx. leaves (L x)) ⋄ X pure (λx. x · [ ]) ⋄ X = ∀x. leaves (L x) x · [ ]

  • 1. Convert to canonical form
  • 2. Generalise opaque arguments
  • 3. Equality is a congruence

applicative expression → pure f ⋄ x1 ⋄ x2 ⋄ . . . ⋄ xn Canonical form

[McBride, Paterson] [Hinze 2010]

  • A. Lochbihler (ETH Zurich)

ITP 2016 22 / 35

slide-23
SLIDE 23

Lifting equations over applicative functors

pure function

  • paque arguments
  • paque arguments
  • paque arguments
  • paque arguments
  • paque arguments

= pure leaves ⋄ (pure L ⋄ fresh) pure (·) ⋄ fresh ⋄ pure [ ] = pure (λx. leaves (L x)) ⋄ fresh pure (λx. x · [ ]) ⋄ fresh = ∀X. pure (λx. leaves (L x)) ⋄ X pure (λx. x · [ ]) ⋄ X = ∀X. pure (λx. leaves (L x)) ⋄ X pure (λx. x · [ ]) ⋄ X = ∀x. leaves (L x) x · [ ]

  • 1. Convert to canonical form
  • 2. Generalise opaque arguments
  • 3. Equality is a congruence
  • 4. Use extensionality

applicative expression → pure f ⋄ x1 ⋄ x2 ⋄ . . . ⋄ xn Canonical form

[McBride, Paterson] [Hinze 2010]

  • A. Lochbihler (ETH Zurich)

ITP 2016 23 / 35

slide-24
SLIDE 24

Lifting equations over applicative functors

pure function

  • paque arguments
  • paque arguments
  • paque arguments
  • paque arguments
  • paque arguments

= pure leaves ⋄ (pure L ⋄ fresh) pure (·) ⋄ fresh ⋄ pure [ ] = pure (λx. leaves (L x)) ⋄ fresh pure (λx. x · [ ]) ⋄ fresh = ∀X. pure (λx. leaves (L x)) ⋄ X pure (λx. x · [ ]) ⋄ X = ∀X. pure (λx. leaves (L x)) ⋄ X pure (λx. x · [ ]) ⋄ X = ∀x. leaves (L x) x · [ ]

  • 1. Convert to canonical form
  • 2. Generalise opaque arguments
  • 3. Equality is a congruence
  • 4. Use extensionality

applicative expression → pure f ⋄ x1 ⋄ x2 ⋄ . . . ⋄ xn Canonical form

[McBride, Paterson] [Hinze 2010]

Same opaque args. on both sides!

  • A. Lochbihler (ETH Zurich)

ITP 2016 24 / 35

slide-25
SLIDE 25

Tree mirroring

c a q 0 1 2 q a c 1 2

lbl mirror lbl pure mirror

?

lbl :: α tree ⇒ nat tree state lbl (L ) = pure L ⋄ fresh lbl (N l r) = pure N ⋄ lbl l ⋄ lbl r mirror :: α tree ⇒ α tree mirror (L x) = L x mirror (N l r) = N (mirror r) (mirror l)

Lemma: lbl (mirror t) = pure mirror ⋄ lbl t

Proof by induction on t. Case N l r: pure (λr ′ l′. N (mirror r ′) (mirror l′)) ⋄ lbl r ⋄ lbl l

?

= pure (λl′ r ′. mirror (N l′ r ′)) ⋄ lbl l ⋄ lbl r

  • A. Lochbihler (ETH Zurich)

ITP 2016 25 / 35

slide-26
SLIDE 26

Tree mirroring

c a q 0 1 2 q a c 1 2

lbl mirror lbl pure mirror lbl :: α tree ⇒ nat tree state lbl (L ) = pure L ⋄ fresh lbl (N l r) = pure N ⋄ lbl l ⋄ lbl r mirror :: α tree ⇒ α tree mirror (L x) = L x mirror (N l r) = N (mirror r) (mirror l)

Lemma: lbl (mirror t) = pure mirror ⋄ lbl t

Proof by induction on t. Case N l r: pure (λr ′ l′. N (mirror r ′) (mirror l′)) ⋄ lbl r ⋄ lbl l

?

= pure (λl′ r ′. mirror (N l′ r ′)) ⋄ lbl l ⋄ lbl r

  • A. Lochbihler (ETH Zurich)

ITP 2016 26 / 35

slide-27
SLIDE 27

Tree mirroring and random labels

c a q q a c

lbl mirror lbl pure mirror lbl :: α tree ⇒ nat tree probability lbl (L ) = pure L ⋄ fresh lbl (N l r) = pure N ⋄ lbl l ⋄ lbl r mirror :: α tree ⇒ α tree mirror (L x) = L x mirror (N l r) = N (mirror r) (mirror l)

Lemma: lbl (mirror t) = pure mirror ⋄ lbl t if effects commute

Proof by induction on t. Case N l r: pure (λr ′ l′. N (mirror r ′) (mirror l′)) ⋄ lbl r ⋄ lbl l = pure (λl′ r ′. mirror (N l′ r ′)) ⋄ lbl l ⋄ lbl r

  • A. Lochbihler (ETH Zurich)

ITP 2016 27 / 35

slide-28
SLIDE 28

Tree mirroring and random labels

c a q q a c

lbl mirror lbl pure mirror lbl :: α tree ⇒ nat tree probability lbl (L ) = pure L ⋄ fresh lbl (N l r) = pure N ⋄ lbl l ⋄ lbl r mirror :: α tree ⇒ α tree mirror (L x) = L x mirror (N l r) = N (mirror r) (mirror l) Criterion for commutative effects: pure (λf x y. f y x) ⋄ f ⋄ x ⋄ y = f ⋄ y ⋄ x C f x y = f y x

Lemma: lbl (mirror t) = pure mirror ⋄ lbl t if effects commute

Proof by induction on t. Case N l r: pure (λr ′ l′. N (mirror r ′) (mirror l′)) ⋄ lbl r ⋄ lbl l = pure (λl′ r ′. mirror (N l′ r ′)) ⋄ lbl l ⋄ lbl r

  • A. Lochbihler (ETH Zurich)

ITP 2016 28 / 35

slide-29
SLIDE 29

Subtrees

q a c a c

lbl right lbl pure right

c a q

lbl mirror pure mirror

Lemma: lbl (right t) = pure right ⋄ lbl t

Proof by case analysis on t. Case N l r: pure (λr ′. r ′) ⋄ lbl r

?

= pure (λ r ′. r ′) ⋄ lbl l ⋄ lbl r

  • A. Lochbihler (ETH Zurich)

ITP 2016 29 / 35

slide-30
SLIDE 30

Subtrees

q a c a c

lbl right lbl pure right

c a q

lbl mirror pure mirror

Lemma: if effects are omissible lbl (right t) = pure right ⋄ lbl t

Proof by case analysis on t. Case N l r: pure (λr ′. r ′) ⋄ lbl r = pure (λ r ′. r ′) ⋄ lbl l ⋄ lbl r Criterion for omissible effects: pure (λx y. x) ⋄ x ⋄ y = x K x y = x

  • A. Lochbihler (ETH Zurich)

ITP 2016 30 / 35

slide-31
SLIDE 31

Combinatorial basis BCKW B C K W

commutative idempotent

  • missible

◮ Declarative characterisation of “liftable” equations ◮ Modular implementation via bracket abstraction

  • A. Lochbihler (ETH Zurich)

ITP 2016 31 / 35

slide-32
SLIDE 32

Combinatorial basis BCKW B C K W

commutative idempotent

  • missible

state probability exception reader

◮ Declarative characterisation of “liftable” equations ◮ Modular implementation via bracket abstraction ◮ User declares and proves combinator properties at registration

  • A. Lochbihler (ETH Zurich)

ITP 2016 32 / 35

slide-33
SLIDE 33

Combinatorial basis BCKW B C K W

commutative idempotent

  • missible

state probability exception reader

stream non-standard numbers maybe zip list non- determinism non-determinism with failure subprobability commutative monoid idempotent monoid

  • rdered

non-determinism parser list monoid ◮ Declarative characterisation of “liftable” equations ◮ Modular implementation via bracket abstraction ◮ User declares and proves combinator properties at registration

  • A. Lochbihler (ETH Zurich)

ITP 2016 33 / 35

slide-34
SLIDE 34

Summary

www.isa-afp.org/entries/Applicative Lifting.shtml

functor registration ⋄ ⋄x = x = proof tactic formalisation

  • f the meta theory

guides

B C K W

classify effects

  • A. Lochbihler (ETH Zurich)

ITP 2016 34 / 35

slide-35
SLIDE 35

Summary

www.isa-afp.org/entries/Applicative Lifting.shtml

functor registration ⋄ ⋄x = x = proof tactic formalisation

  • f the meta theory

guides generate? beyond equality? monads?

B C K W

classify effects

  • A. Lochbihler (ETH Zurich)

ITP 2016 35 / 35