Equational Reasoning with Applicative Functors. Institute of Information Security

Similar documents
Towards abstract and executable multivariate polynomials in Isabelle

The Lifting Lemma. Ralf Hinze

Understanding Idiomatic Traversals Backwards and Forwards

Universal Algebra for Termination of Higher-Order Rewriting. Makoto Hamana

Inductive Theorem Proving

Automated Reasoning Lecture 17: Inductive Proof (in Isabelle)

Lattices and Orders in Isabelle/HOL

Reasoning about Codata

A Calculus of Definitions

A Categorical Characterization of Untyped Lambda Calculus

Why walk when you can take the tube?

1 Problem 1. (20 pts)

Red-black Trees of smlnj Studienarbeit. Angelika Kimmig

A New Look At Generalized Rewriting in Type Theory

The Under-Performing Unfold

Concrete Stream Calculus

Functional Data Structures

Code Generation for a Simple First-Order Prover

A few bridges between operational and denotational semantics of programming languages

Interactive Theorem Provers

On the Semantics of Parsing Actions

Free Groups. Joachim Breitner. April 17, 2016

Automated Reasoning Lecture 2: Propositional Logic and Natural Deduction

Mathematical Synthesis of Equational Deduction Systems. Marcelo Fiore. Computer Laboratory University of Cambridge

Logic for Computational Effects: work in progress

F U N C T I O N A L P E A R L S The Bird tree

Higher Order Containers

The Reflection Theorem

EDA045F: Program Analysis LECTURE 10: TYPES 1. Christoph Reichenbach

Isomorphism of Finitary Inductive Types

NICTA Advanced Course. Theorem Proving Principles, Techniques, Applications. Gerwin Klein Formal Methods

Semantics for algebraic operations

Uncountable computable model theory

From parametric polymorphism to models of polymorphic FPC

A Recursion Combinator for Nominal Datatypes Implemented in Isabelle/HOL

Abstract Convexity: Results and Speculations

Completeness of Star-Continuity

Type inference in context

Types and Programming Languages (15-814), Fall 2018 Assignment 4: Data Representation (Sample Solutions)

FORMALISING THE π-calculus USING NOMINAL LOGIC

Semantic Labelling for Proving Termination of Combinatory Reduction Systems. Makoto Hamana

Implementation of the stable revivals model in CSP-Prover

A Foundation for GADTs and Inductive Families. Makoto Hamana Gunma University, Japan

CERTIFIED CONTEXT-FREE PARSING: A FORMALISATION OF VALIANT S ALGORITHM IN AGDA

Reckhow s Theorem. Yuval Filmus. November 2010

Nunchaku: Flexible Model Finding for Higher-Order Logic

LCF + Logical Frameworks = Isabelle (25 Years Later)

Second-Order Algebraic Theories

Interpreting the Second-Order Lambda Calculus using Qualitative Domains

M a s t e r r e s e a rc h I n t e r n s h i p. Formalisation of Ground Inference Systems in a Proof Assistant

Minimally Strict Polymorphic Functions

Lecture 7. Logic. Section1: Statement Logic.

Programming with Dependent Types in Coq

Notions of Computation Determine Monads

Beyond First-Order Logic

Normalization by Evaluation

HOL: Well-founded and Primitive Recursion

Generic Fibrational Induction

A Differential Model Theory for Resource Lambda Calculi - Part II

Introduction to lambda calculus Part 6

Equational Logic. Chapter Syntax Terms and Term Algebras

NOTES ON POLYNOMIAL REPRESENTATIONS OF GENERAL LINEAR GROUPS

Dynamic Rippling, Middle-Out Reasoning and Lemma Discovery

A Canonical 1 Local Representation of Binding. α -equivalence is identity. Randy Pollack. Masahiko Sato. LFCS, University of Edinburgh

Foundations of Programming Languages and Software Engineering

Theoretical Computer Science. Representing model theory in a type-theoretical logical framework

Local Representations of Binding

CSCE 222 Discrete Structures for Computing. Propositional Logic. Dr. Hyunyoung Lee. !!!!!! Based on slides by Andreas Klappenecker

A Verified ODE solver and Smale s 14th Problem

Element x is R-minimal in X if y X. R(y, x).

A NEW FOUNDATION OF A COMPLETE BOOLEAN EQUATIONAL LOGIC

A New Look at Generalized Rewriting in Type Theory

Structure. Background. them to their higher order equivalents. functionals in Standard ML source code and converts

Proofs Regarding Specification of Spark Aggregation

Lecture Notes on Inductive Definitions

1. The Method of Coalgebra

OMEGA-CATEGORIES AND CHAIN COMPLEXES. 1. Introduction. Homology, Homotopy and Applications, vol.6(1), 2004, pp RICHARD STEINER

arxiv: v1 [math.lo] 30 Aug 2018

Formalising the π-calculus in Isabelle

Indexed Containers. Thorsten Altenkirch Peter Morris University of Nottingham Abstract. 1 Introduction

Modular Architecture for Proof Advice AITP Components

Permutation Groups and Transformation Semigroups Lecture 2: Semigroups

Naturality, but not as you know it

Inductive Predicates

Relational Algebra by Way of Adjunctions. Jeremy Gibbons (joint work with Fritz Henglein, Ralf Hinze, Nicolas Wu) DBPL, October 2015

Categories and Functors (Lecture Notes for Midlands Graduate School, 2013) Uday S. Reddy The University of Birmingham

Church and Curry: Combining Intrinsic and Extrinsic Typing

Algebras for Combinatorial Search

A Formalised Proof of Craig s Interpolation Theorem in Nominal Isabelle

Witnessing (Co)datatypes

Intensionality, Extensionality, and Proof Irrelevance in Modal Type Theory

BiLog: Spatial Logics for Bigraphs 1

What are Iteration Theories?

Science of Computer Programming

1. Prove: A full m- ary tree with i internal vertices contains n = mi + 1 vertices.

Write your own Theorem Prover

CIS 500: Software Foundations

ACTING FREELY GABRIEL GASTER

An Introduction to Proof Assistants

Transcription:

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