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 streams 1 2 3 4 · · ·
Equational Reasoning with Applicative Functors Andreas Lochbihler Joshua Schneider Institute of Information Security f ( g � ) = h ( k � ) model effects k state probabilities g � error h non-determinism f streams 1 2 3 4 · · ·
Equational Reasoning with Applicative Functors Andreas Lochbihler Joshua Schneider Institute of Information Security pure f ⋄ ( g � ) = pure h ⋄ ( k � ) model effects k state probabilities g � error h non-determinism f streams 1 2 3 4 · · ·
Equational Reasoning with Applicative Functors Andreas Lochbihler Joshua Schneider Institute of Information Security pure f ⋄ ( g � ) = pure h ⋄ ( k � ) model effects k state probabilities g � error h don’t non-determinism care f streams 1 2 3 4 · · ·
Contributions ◮ Isabelle/HOL package for reasoning about applicative effects ⋄ ⋄ x = x = functor registration 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! lbl q a d 2 3 4 c b datatype α tree = 0 1 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
Task: Label a binary tree with distinct numbers! lbl q a d 2 3 4 c b datatype α tree = 0 1 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
Task: Label a binary tree with distinct numbers! lbl q a d 2 3 4 c b datatype α tree = 0 1 L α | N ( α tree ) ( α tree ) state lbl :: α tree ⇒ nat tree state α state = nat ⇒ α × nat where monadic α M = α state return :: α ⇒ α M ( > =) :: α M ⇒ ( α ⇒ β M ) ⇒ β M > lbl (L ) = fresh > = λ x ′ . return (L x ′ ) > lbl (N l r ) = = λ r ′ . return (N l ′ r ′ ) lbl l > = λ 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 9 / 35
Task: Label a binary tree with distinct numbers! lbl q a d 2 3 4 c b datatype α tree = 0 1 L α | N ( α tree ) ( α tree ) state lbl :: α tree ⇒ nat tree state α state = nat ⇒ α × nat where monadic applicative α M = α state α F = α state return :: α ⇒ α M pure :: α ⇒ α F ( > =) :: α M ⇒ ( α ⇒ β M ) ⇒ β M ( ⋄ ) :: ( α ⇒ β ) F ⇒ α F ⇒ β F > lbl (L ) = fresh > = λ x ′ . return (L x ′ ) lbl (L ) = pure L ⋄ fresh > lbl (N l r ) = lbl (N l r ) = pure N ⋄ lbl l ⋄ lbl r = λ r ′ . return (N l ′ r ′ ) lbl l > = λ 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
Labelling trees and lists leaves :: α tree ⇒ α list lbl leaves (L x ) = x · [ ] q 2 leaves (N l r ) = leaves l + + leaves r c a 0 1 leaves pure leaves lbl ′ :: α list ⇒ nat list state lbl ′ [ ] lbl ′ = pure [ ] [c, a, q] [0, 1, 2] lbl ′ ( · xs ) = pure ( · ) ⋄ fresh ⋄ lbl ′ xs A. Lochbihler (ETH Zurich) ITP 2016 11 / 35
Labelling trees and lists leaves :: α tree ⇒ α list lbl leaves (L x ) = x · [ ] q 2 leaves (N l r ) = leaves l + + leaves r c a 0 1 leaves pure leaves lbl ′ :: α list ⇒ nat list state lbl ′ [ ] lbl ′ = pure [ ] [c, a, q] [0, 1, 2] lbl ′ ( · xs ) = pure ( · ) ⋄ fresh ⋄ lbl ′ xs pure leaves ⋄ lbl t = lbl ′ (leaves t ) Lemma: Proof by induction on t . = lbl ′ (leaves (L x )) Case L x : pure leaves ⋄ lbl (L x )
Labelling trees and lists leaves :: α tree ⇒ α list lbl leaves (L x ) = x · [ ] q 2 leaves (N l r ) = leaves l + + leaves r c a 0 1 leaves pure leaves lbl ′ :: α list ⇒ nat list state lbl ′ [ ] lbl ′ = pure [ ] [c, a, q] [0, 1, 2] lbl ′ ( · xs ) = pure ( · ) ⋄ fresh ⋄ lbl ′ xs pure leaves ⋄ lbl t = lbl ′ (leaves t ) Lemma: Proof by induction on t . = lbl ′ (leaves (L x )) Case L x : pure leaves ⋄ lbl (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 leaves :: α tree ⇒ α list lbl leaves (L x ) = x · [ ] q 2 leaves (N l r ) = leaves l + + leaves r c a 0 1 leaves pure leaves lbl ′ :: α list ⇒ nat list state lbl ′ [ ] lbl ′ = pure [ ] [c, a, q] [0, 1, 2] lbl ′ ( · xs ) = pure ( · ) ⋄ fresh ⋄ lbl ′ xs pure leaves ⋄ lbl t = lbl ′ (leaves t ) Lemma: Proof by induction on t . = lbl ′ (leaves (L x )) Case L x : pure leaves ⋄ lbl (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 leaves :: α tree ⇒ α list lbl leaves (L x ) = x · [ ] q 2 leaves (N l r ) = leaves l + + leaves r c a 0 1 leaves pure leaves lbl ′ :: α list ⇒ nat list state lbl ′ [ ] lbl ′ = pure [ ] [c, a, q] [0, 1, 2] lbl ′ ( · xs ) = pure ( · ) ⋄ fresh ⋄ lbl ′ xs pure leaves ⋄ lbl t = lbl ′ (leaves t ) Lemma: Proof by induction on t . = lbl ′ (leaves (L x )) Case L x : pure leaves ⋄ lbl (L x ) pure leaves ⋄ (pure L ⋄ fresh) = pure ( · ) ⋄ fresh ⋄ pure [ ] ⇑ holds by the applicative laws apply applicative lifting ∀ x . leaves ( L x ) = ( · ) x [ ] 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 [ ] ML e e kernel HOL l l l syntactic l e e b b a a ∀ ∀ s s I I = = formalisation α α λ λ β → β → f o l l o w s ∀ 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 [Hinze 2010] Canonical form [McBride, Paterson] applicative expression �→ pure f ⋄ x 1 ⋄ x 2 ⋄ . . . ⋄ x n 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 [Hinze 2010] pure function opaque arguments opaque arguments opaque arguments opaque arguments opaque arguments Canonical form [McBride, Paterson] applicative expression �→ pure f ⋄ x 1 ⋄ x 2 ⋄ . . . ⋄ x n 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 [Hinze 2010] pure function opaque arguments opaque arguments opaque arguments opaque arguments opaque arguments Canonical form [McBride, Paterson] applicative expression �→ pure f ⋄ x 1 ⋄ x 2 ⋄ . . . ⋄ x n pure leaves ⋄ (pure L ⋄ fresh) = pure ( · ) ⋄ fresh ⋄ pure [ ] 1. Convert to canonical form pure ( λ x . leaves (L x )) ⋄ fresh = pure ( λ x . x · [ ]) ⋄ fresh 2. Generalise opaque arguments ∀ X . pure ( λ x . leaves (L x )) ⋄ X = pure ( λ x . x · [ ]) ⋄ X ∀ x . leaves (L x ) = x · [ ] A. Lochbihler (ETH Zurich) ITP 2016 21 / 35
Lifting equations over applicative functors [Hinze 2010] pure function opaque arguments opaque arguments opaque arguments opaque arguments opaque arguments Canonical form [McBride, Paterson] applicative expression �→ pure f ⋄ x 1 ⋄ x 2 ⋄ . . . ⋄ x n pure leaves ⋄ (pure L ⋄ fresh) = pure ( · ) ⋄ fresh ⋄ pure [ ] 1. Convert to canonical form pure ( λ x . leaves (L x )) ⋄ fresh = pure ( λ x . x · [ ]) ⋄ fresh 2. Generalise opaque arguments ∀ X . pure ( λ x . leaves (L x )) ⋄ X = pure ( λ x . x · [ ]) ⋄ X 3. Equality is a congruence ∀ X . pure ( λ x . leaves (L x )) ⋄ X = pure ( λ x . x · [ ]) ⋄ X ∀ x . leaves (L x ) = x · [ ] A. Lochbihler (ETH Zurich) ITP 2016 22 / 35
Recommend
More recommend