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 effects state probabilities error non-determinism 1 2 3 4 streams
Equational Reasoning with Applicative Functors Andreas Lochbihler Joshua Schneider Institute of Information Security f (g ) h (k ) k g h f model effects state probabilities error non-determinism 1 2 3 4 streams
Equational Reasoning with Applicative Functors Andreas Lochbihler Joshua Schneider Institute of Information Security pure f (g ) pure h (k ) k g h f model effects state probabilities error non-determinism 1 2 3 4 streams
Equational Reasoning with Applicative Functors Andreas Lochbihler Joshua Schneider Institute of Information Security pure f (g ) pure h (k ) g k f h don t care model effects state probabilities error non-determinism 1 2 3 4 streams
Contributions Isabelle/HOL package for reasoning about applicative effects x functor registration 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
Task: Label a binary tree with distinct numbers! c b q a d datatype α tree L α N (α tree) (α tree) 0 1 2 3 4 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
Task: Label a binary tree with distinct numbers! c b q a d datatype α tree L α N (α tree) (α tree) 0 1 2 3 4 :: α 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
Task: Label a binary tree with distinct numbers! c b q a d datatype α tree L α N (α tree) (α tree) 0 1 2 3 4 state :: α tree nat tree state where α state nat α nat monadic α M α state return :: α α M ( >) :: α M (α β M) β M (L ) fresh > λx. return (L x ) (N l r) l > λl. 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
Task: Label a binary tree with distinct numbers! c b q a d datatype α tree L α N (α tree) (α tree) 0 1 2 3 4 state :: α tree nat tree state where α state nat α nat monadic α M α state applicative α F α state return :: α α M pure :: α α F ( >) :: α M (α β M) β M ( ) :: (α β) F α F β F (L ) fresh > λx. return (L x ) (N l r) l > λl. r > λr. return (N l r ) (L ) pure L fresh (N l r) pure 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 10 / 35
Labelling trees and lists c a q 0 1 2 leaves :: α tree α list leaves (L x) x [ ] leaves (N l r) leaves l ++ leaves r leaves pure leaves [c, a, q] [0, 1, 2] :: α list nat list state [ ] pure [ ] ( xs) pure ( ) fresh xs A. Lochbihler (ETH Zurich) ITP 2016 11 / 35
Labelling trees and lists c a q 0 1 2 leaves :: α tree α list leaves (L x) x [ ] leaves (N l r) leaves l ++ leaves r leaves pure leaves [c, a, q] [0, 1, 2] :: α list nat list state [ ] pure [ ] ( xs) pure ( ) fresh xs Lemma: pure leaves t (leaves t) Proof by induction on t. Case L x: pure leaves (L x) (leaves (L x))
Labelling trees and lists c a q 0 1 2 leaves :: α tree α list leaves (L x) x [ ] leaves (N l r) leaves l ++ leaves r leaves pure leaves [c, a, q] [0, 1, 2] :: α list nat list state [ ] pure [ ] ( xs) pure ( ) fresh xs Lemma: pure leaves t (leaves t) Proof by induction on t. Case L x: pure leaves (L x) (leaves (L x)) pure leaves (pure L fresh) pure ( ) fresh pure [ ] x. leaves ( L x ) ( ) x [ ] A. Lochbihler (ETH Zurich) ITP 2016 13 / 35
Labelling trees and lists c a q 0 1 2 leaves :: α tree α list leaves (L x) x [ ] leaves (N l r) leaves l ++ leaves r leaves pure leaves [c, a, q] [0, 1, 2] :: α list nat list state [ ] pure [ ] ( xs) pure ( ) fresh xs Lemma: pure leaves t (leaves t) Proof by induction on t. Case L x: pure leaves (L x) (leaves (L x)) pure leaves (pure L fresh) pure ( ) fresh pure [ ] holds by the applicative laws x. leaves ( L x ) ( ) x [ ] A. Lochbihler (ETH Zurich) ITP 2016 14 / 35
Labelling trees and lists c a q 0 1 2 leaves :: α tree α list leaves (L x) x [ ] leaves (N l r) leaves l ++ leaves r leaves pure leaves [c, a, q] [0, 1, 2] :: α list nat list state [ ] pure [ ] ( xs) pure ( ) fresh xs Lemma: pure leaves t (leaves t) Proof by induction on t. Case L x: pure leaves (L x) (leaves (L x)) pure leaves (pure L fresh) pure ( ) fresh pure [ ] holds by the applicative laws x. leaves ( L x ) ( ) x [ ] apply applicative lifting A. Lochbihler (ETH Zurich) ITP 2016 15 / 35
Lifting equations over applicative functors [Hinze 2010] pure leaves (pure L fresh) pure ( ) fresh pure [ ] x. leaves (L x) x [ ] A. Lochbihler (ETH Zurich) ITP 2016 16 / 35
Lifting equations over applicative functors [Hinze 2010] pure leaves (pure L fresh) pure ( ) fresh pure [ ] λ β Isabelle kernel α ML syntactic formalisation λ β Isabelle HOL α follows x. leaves (L x) x [ ] A. Lochbihler (ETH Zurich) ITP 2016 17 / 35
Lifting equations over applicative functors [Hinze 2010] pure leaves (pure L fresh) pure ( ) fresh pure [ ] 1. Convert to canonical form pure (λx. leaves (L x)) fresh pure (λx. x [ ]) fresh x. leaves (L x) x [ ] A. Lochbihler (ETH Zurich) ITP 2016 18 / 35
Lifting equations over applicative functors Canonical form applicative expression pure f x 1 x 2... x n [Hinze 2010] [McBride, Paterson] pure leaves (pure L fresh) pure ( ) fresh pure [ ] 1. Convert to canonical form pure (λx. leaves (L x)) fresh pure (λx. x [ ]) fresh x. leaves (L x) x [ ] A. Lochbihler (ETH Zurich) ITP 2016 19 / 35
Lifting equations over applicative functors pure function opaque arguments Canonical form applicative expression pure f x 1 x 2... x n [Hinze 2010] [McBride, Paterson] pure leaves (pure L fresh) pure ( ) fresh pure [ ] 1. Convert to canonical form pure (λx. leaves (L x)) fresh pure (λx. x [ ]) fresh x. leaves (L x) x [ ] A. Lochbihler (ETH Zurich) ITP 2016 20 / 35
Lifting equations over applicative functors pure function opaque arguments Canonical form applicative expression pure f x 1 x 2... x n [Hinze 2010] [McBride, Paterson] pure leaves (pure L fresh) pure ( ) fresh pure [ ] 1. Convert to canonical form pure (λx. leaves (L x)) fresh 2. Generalise opaque arguments X. pure (λx. leaves (L x)) X pure (λx. x [ ]) fresh pure (λx. x [ ]) X x. leaves (L x) x [ ] A. Lochbihler (ETH Zurich) ITP 2016 21 / 35
Lifting equations over applicative functors pure function opaque arguments Canonical form applicative expression pure f x 1 x 2... x n [Hinze 2010] [McBride, Paterson] pure leaves (pure L fresh) pure ( ) fresh pure [ ] 1. Convert to canonical form pure (λx. leaves (L x)) fresh 2. Generalise opaque arguments X. pure (λx. leaves (L x)) X 3. Equality is a congruence X. pure (λx. leaves (L x)) X pure (λx. x [ ]) fresh pure (λx. x [ ]) X pure (λx. x [ ]) X x. leaves (L x) x [ ] A. Lochbihler (ETH Zurich) ITP 2016 22 / 35
Lifting equations over applicative functors pure function opaque arguments Canonical form applicative expression pure f x 1 x 2... x n [Hinze 2010] [McBride, Paterson] pure leaves (pure L fresh) pure ( ) fresh pure [ ] pure (λx. leaves (L x)) fresh 2. Generalise opaque arguments X. pure (λx. leaves (L x)) X 3. Equality is a congruence X. pure (λx. leaves (L x)) X 4. Use extensionality 1. Convert to canonical form pure (λx. x [ ]) fresh pure (λx. x [ ]) X pure (λx. x [ ]) X x. leaves (L x) x [ ] A. Lochbihler (ETH Zurich) ITP 2016 23 / 35
Lifting equations over applicative functors pure function opaque arguments Canonical form applicative expression pure f x 1 x 2... x n [Hinze 2010] [McBride, Paterson] pure leaves (pure L fresh) pure ( ) fresh pure [ ] pure (λx. leaves (L x)) fresh 2. Generalise opaque arguments X. pure (λx. leaves (L x)) X 3. Equality is a congruence X. pure (λx. leaves (L x)) X 4. Use extensionality 1. Convert to canonical form pure (λx. x [ ]) fresh pure (λx. x [ ]) X pure (λx. x [ ]) X x. leaves (L x) x [ ] Same opaque args. on both sides! A. Lochbihler (ETH Zurich) ITP 2016 24 / 35
Tree mirroring c a q 0 1 2 mirror :: α tree α tree mirror (L x) L x mirror (N l r) N (mirror r) (mirror l) mirror? pure mirror q a c 0 1 2 :: α tree nat tree state (L ) pure L fresh (N l r) pure N l r Lemma: Proof by induction on t. Case N l r: (mirror t) pure mirror t? pure (λr l. N (mirror r ) (mirror l )) r l pure (λl r. mirror (N l r )) l r A. Lochbihler (ETH Zurich) ITP 2016 25 / 35
Tree mirroring c a q 0 1 2 mirror :: α tree α tree mirror (L x) L x mirror (N l r) N (mirror r) (mirror l) mirror pure mirror q a c 0 1 2 :: α tree nat tree state (L ) pure L fresh (N l r) pure N l r Lemma: Proof by induction on t. Case N l r: (mirror t) pure mirror t? pure (λr l. N (mirror r ) (mirror l )) r l pure (λl r. mirror (N l r )) l r A. Lochbihler (ETH Zurich) ITP 2016 26 / 35
Tree mirroring and random labels c a q mirror :: α tree α tree mirror (L x) L x mirror (N l r) N (mirror r) (mirror l) mirror pure mirror q a c :: α tree nat tree probability (L ) pure L fresh (N l r) pure N l r Lemma: (mirror t) pure mirror t if effects commute Proof by induction on t. Case N l r: pure (λr l. N (mirror r ) (mirror l )) r l pure (λl r. mirror (N l r )) l r A. Lochbihler (ETH Zurich) ITP 2016 27 / 35
Tree mirroring and random labels c a q mirror :: α tree α tree mirror (L x) L x mirror (N l r) N (mirror r) (mirror l) mirror pure mirror q a c Criterion for commutative effects: :: α tree nat tree probability pure (λf (L x y. ) f y x) pure f L x fresh y f y x (N Cl r) puref N x y l f r y x Lemma: (mirror t) pure mirror t if effects commute Proof by induction on t. Case N l r: pure (λr l. N (mirror r ) (mirror l )) r l pure (λl r. mirror (N l r )) l r A. Lochbihler (ETH Zurich) ITP 2016 28 / 35
Subtrees c a q mirror pure mirror q a c Lemma: (right t) pure right t Proof by case analysis on t. right pure right Case N l r: pure (λr. r ) r? pure (λ r. r ) l r a c A. Lochbihler (ETH Zurich) ITP 2016 29 / 35
Subtrees c a q Criterion for omissible effects: pure (λx y. x) x y x mirror pure mirror K x y x q a c Lemma: if effects are omissible (right t) pure right t Proof by case analysis on t. right pure right Case N l r: pure (λr. r ) r pure (λ r. r ) l r a c A. Lochbihler (ETH Zurich) ITP 2016 30 / 35
Combinatorial basis BCKW B K omissible W idempotent commutative Declarative characterisation of liftable equations Modular implementation via bracket abstraction C A. Lochbihler (ETH Zurich) ITP 2016 31 / 35
Combinatorial basis BCKW B K omissible state exception probability reader W idempotent commutative C 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
Combinatorial basis BCKW B ordered non-determinism K omissible nondeterminism probability non-standard numbers reader stream zip list state exception idempotent monoid monoid W idempotent parser non-determinism with failure subprobability commutative commutative monoid C maybe list 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
Summary www.isa-afp.org/entries/applicative Lifting.shtml x B K functor registration x proof tactic C W classify effects guides formalisation of the meta theory A. Lochbihler (ETH Zurich) ITP 2016 34 / 35
Summary www.isa-afp.org/entries/applicative Lifting.shtml x B K W x C functor registration proof tactic classify effects monads? generate? guides beyond equality? formalisation of the meta theory A. Lochbihler (ETH Zurich) ITP 2016 35 / 35