Type-Driven Type-Driven Development of Certi ed Tree Tree ... · dependent types? Producing better...

57
Type-Driven Development of Certified Tree Algorithms Introduction Initial take Why dependent types? Producing better code Conclusion Type-Driven Development of Certified Tree Algorithms An Experience Report on Dependently-Typed Programming in Coq Reynald Affeldt 1 Jacques Garrigue 2,4 Xuanrui Qi 2,3 Kazunari Tanaka 2 1 National Institute of Advanced Industrial Science and Technology, Japan 2 Graduate School of Mathematics, Nagoya University, Japan 3 Department of Computer Science, Tufts University, USA 4 Inria Paris, France September 8, 2019 The Coq Workshop 2019, Portland, Oregon 1 / 22

Transcript of Type-Driven Type-Driven Development of Certi ed Tree Tree ... · dependent types? Producing better...

Page 1: Type-Driven Type-Driven Development of Certi ed Tree Tree ... · dependent types? Producing better code Conclusion Type-Driven Development of Certi ed Tree Algorithms An Experience

Type-DrivenDevelopmentof Certified

TreeAlgorithms

Introduction

Initial take

Whydependenttypes?

Producingbetter code

Conclusion

Type-Driven Development of Certified TreeAlgorithms

An Experience Report on Dependently-Typed Programmingin Coq

Reynald Affeldt 1 Jacques Garrigue 2,4

Xuanrui Qi 2,3 Kazunari Tanaka 2

1National Institute of Advanced Industrial Science and Technology, Japan

2Graduate School of Mathematics, Nagoya University, Japan

3Department of Computer Science, Tufts University, USA

4Inria Paris, France

September 8, 2019The Coq Workshop 2019, Portland, Oregon

1 / 22

Page 2: Type-Driven Type-Driven Development of Certi ed Tree Tree ... · dependent types? Producing better code Conclusion Type-Driven Development of Certi ed Tree Algorithms An Experience

Type-DrivenDevelopmentof Certified

TreeAlgorithms

Introduction

Initial take

Whydependenttypes?

Producingbetter code

Conclusion

Our story

Dependently-typed Programming in Coq: Why and How?

The “Why” Why did we use dependent types in Coq? Underwhat occasions are they useful?

The “How” What is the best approach towardsdependently-typed programming in Coq? How did weapproach it?

2 / 22

Page 3: Type-Driven Type-Driven Development of Certi ed Tree Tree ... · dependent types? Producing better code Conclusion Type-Driven Development of Certi ed Tree Algorithms An Experience

Type-DrivenDevelopmentof Certified

TreeAlgorithms

Introduction

Initial take

Whydependenttypes?

Producingbetter code

Conclusion

Our story

Dependently-typed Programming in Coq: Why and How?

The “Why” Why did we use dependent types in Coq? Underwhat occasions are they useful?

The “How” What is the best approach towardsdependently-typed programming in Coq? How did weapproach it?

2 / 22

Page 4: Type-Driven Type-Driven Development of Certi ed Tree Tree ... · dependent types? Producing better code Conclusion Type-Driven Development of Certi ed Tree Algorithms An Experience

Type-DrivenDevelopmentof Certified

TreeAlgorithms

Introduction

Initial take

Whydependenttypes?

Producingbetter code

Conclusion

Our story

Dependently-typed Programming in Coq: Why and How?

The “Why” Why did we use dependent types in Coq? Underwhat occasions are they useful?

The “How” What is the best approach towardsdependently-typed programming in Coq? How did weapproach it?

2 / 22

Page 5: Type-Driven Type-Driven Development of Certi ed Tree Tree ... · dependent types? Producing better code Conclusion Type-Driven Development of Certi ed Tree Algorithms An Experience

Type-DrivenDevelopmentof Certified

TreeAlgorithms

Introduction

Initial take

Whydependenttypes?

Producingbetter code

Conclusion

Quick recap: what were weworking on?

Bit vectors with efficient insert & delete:

100

101 1111

10101 1111

delete 2nd bit

• Represented using a red-black tree• Insertion and deletion might involve inserting/deleting

nodes3 / 22

Page 6: Type-Driven Type-Driven Development of Certi ed Tree Tree ... · dependent types? Producing better code Conclusion Type-Driven Development of Certi ed Tree Algorithms An Experience

Type-DrivenDevelopmentof Certified

TreeAlgorithms

Introduction

Initial take

Whydependenttypes?

Producingbetter code

Conclusion

The motivation

Deletion from red-black trees is too hard.

• long standing problem with a few proposed solutions[Kahrs 2001; Germane & Might 2014], but none of themtotally satisfactory for us;

• complex invariant hard to describe precisely

• difficult to transcribe to our non-standard tree structure(bit-borrowing, leaf merging, etc.)

4 / 22

Page 7: Type-Driven Type-Driven Development of Certi ed Tree Tree ... · dependent types? Producing better code Conclusion Type-Driven Development of Certi ed Tree Algorithms An Experience

Type-DrivenDevelopmentof Certified

TreeAlgorithms

Introduction

Initial take

Whydependenttypes?

Producingbetter code

Conclusion

Take 1

We tried transcribing Kahrs’ Haskell code directly to Coqwithout trying to fully understand it. But you guessed...

5 / 22

Page 8: Type-Driven Type-Driven Development of Certi ed Tree Tree ... · dependent types? Producing better code Conclusion Type-Driven Development of Certi ed Tree Algorithms An Experience

Type-DrivenDevelopmentof Certified

TreeAlgorithms

Introduction

Initial take

Whydependenttypes?

Producingbetter code

Conclusion

Take 1

We tried transcribing Kahrs’ Haskell code directly to Coqwithout trying to fully understand it. But you guessed...

5 / 22

Page 9: Type-Driven Type-Driven Development of Certi ed Tree Tree ... · dependent types? Producing better code Conclusion Type-Driven Development of Certi ed Tree Algorithms An Experience

Type-DrivenDevelopmentof Certified

TreeAlgorithms

Introduction

Initial take

Whydependenttypes?

Producingbetter code

Conclusion

Using dependent types

Problem:

• not sure about how to do case analysis;

• not sure about the exact invariants;

• not sure about the auxiliary structures required.

Idea: use dependently-typed programming to guideprogramming process.

6 / 22

Page 10: Type-Driven Type-Driven Development of Certi ed Tree Tree ... · dependent types? Producing better code Conclusion Type-Driven Development of Certi ed Tree Algorithms An Experience

Type-DrivenDevelopmentof Certified

TreeAlgorithms

Introduction

Initial take

Whydependenttypes?

Producingbetter code

Conclusion

Auxiliary structures?

The intermediate data structures required for re-balancing thetree:

Inductive near_tree : nat -> nat -> nat ->color -> Type :=

| Bad : forall {s1 o1 s2 o2 s3 o3 d},tree s1 o1 d Black ->tree s2 o2 d Black ->tree s3 o3 d Black ->near_tree (s1 + s2 + s3) (o1 + o2 + o3) d Red

| Good: forall {s o d c} p,tree s o d c ->near_tree s o d p.

Re-balancing requires temporarily breaking the red-black treeinvariants, hence the need for auxiliary structures.

7 / 22

Page 11: Type-Driven Type-Driven Development of Certi ed Tree Tree ... · dependent types? Producing better code Conclusion Type-Driven Development of Certi ed Tree Algorithms An Experience

Type-DrivenDevelopmentof Certified

TreeAlgorithms

Introduction

Initial take

Whydependenttypes?

Producingbetter code

Conclusion

Auxiliary structures?

The intermediate data structures required for re-balancing thetree:

Inductive near_tree : nat -> nat -> nat ->color -> Type :=

| Bad : forall {s1 o1 s2 o2 s3 o3 d},tree s1 o1 d Black ->tree s2 o2 d Black ->tree s3 o3 d Black ->near_tree (s1 + s2 + s3) (o1 + o2 + o3) d Red

| Good: forall {s o d c} p,tree s o d c ->near_tree s o d p.

Re-balancing requires temporarily breaking the red-black treeinvariants, hence the need for auxiliary structures.

7 / 22

Page 12: Type-Driven Type-Driven Development of Certi ed Tree Tree ... · dependent types? Producing better code Conclusion Type-Driven Development of Certi ed Tree Algorithms An Experience

Type-DrivenDevelopmentof Certified

TreeAlgorithms

Introduction

Initial take

Whydependenttypes?

Producingbetter code

Conclusion

Take 2: Ltac

• use tactics to develop the program

• we ascribe strict types to each function, allowing to becompletely sure that our code is correct

• as a side effect, we got a very clean specification• no “external” lemmas which can be easy to forget• all desired invariants were encoded into the types

8 / 22

Page 13: Type-Driven Type-Driven Development of Certi ed Tree Tree ... · dependent types? Producing better code Conclusion Type-Driven Development of Certi ed Tree Algorithms An Experience

Type-DrivenDevelopmentof Certified

TreeAlgorithms

Introduction

Initial take

Whydependenttypes?

Producingbetter code

Conclusion

Take 2: Ltac

• use tactics to develop the program

• we ascribe strict types to each function, allowing to becompletely sure that our code is correct

• as a side effect, we got a very clean specification• no “external” lemmas which can be easy to forget• all desired invariants were encoded into the types

8 / 22

Page 14: Type-Driven Type-Driven Development of Certi ed Tree Tree ... · dependent types? Producing better code Conclusion Type-Driven Development of Certi ed Tree Algorithms An Experience

Type-DrivenDevelopmentof Certified

TreeAlgorithms

Introduction

Initial take

Whydependenttypes?

Producingbetter code

Conclusion

Programming Coq with tactics

The Pros

• “No brainer”: no need to fully understand the algorithm[Chlipala 2013]

• Easy to refactor: when underlying data structures change

• Quick fixes & adapting to changes

9 / 22

Page 15: Type-Driven Type-Driven Development of Certi ed Tree Tree ... · dependent types? Producing better code Conclusion Type-Driven Development of Certi ed Tree Algorithms An Experience

Type-DrivenDevelopmentof Certified

TreeAlgorithms

Introduction

Initial take

Whydependenttypes?

Producingbetter code

Conclusion

Programming Coq with tactics

The Cons

• You don’t know what you’re actually doing

• Readability: other people don’t know what you’re doing

• Semantics of Ltac changes frequently

10 / 22

Page 16: Type-Driven Type-Driven Development of Certi ed Tree Tree ... · dependent types? Producing better code Conclusion Type-Driven Development of Certi ed Tree Algorithms An Experience

Type-DrivenDevelopmentof Certified

TreeAlgorithms

Introduction

Initial take

Whydependenttypes?

Producingbetter code

Conclusion

Type-driven development?

“Type-driven” in what sense?

Regular development: design the algorithm, and then writetypes to check that you’re correct.

Type-driven development: write types to declare what youwant, and then code until it type checks

It type checks, ship it!

11 / 22

Page 17: Type-Driven Type-Driven Development of Certi ed Tree Tree ... · dependent types? Producing better code Conclusion Type-Driven Development of Certi ed Tree Algorithms An Experience

Type-DrivenDevelopmentof Certified

TreeAlgorithms

Introduction

Initial take

Whydependenttypes?

Producingbetter code

Conclusion

Type-driven development?

“Type-driven” in what sense?

Regular development: design the algorithm, and then writetypes to check that you’re correct.

Type-driven development: write types to declare what youwant, and then code until it type checks

It type checks, ship it!

11 / 22

Page 18: Type-Driven Type-Driven Development of Certi ed Tree Tree ... · dependent types? Producing better code Conclusion Type-Driven Development of Certi ed Tree Algorithms An Experience

Type-DrivenDevelopmentof Certified

TreeAlgorithms

Introduction

Initial take

Whydependenttypes?

Producingbetter code

Conclusion

Type-driven development?

“Type-driven” in what sense?

Regular development: design the algorithm, and then writetypes to check that you’re correct.

Type-driven development: write types to declare what youwant, and then code until it type checks

It type checks, ship it!

11 / 22

Page 19: Type-Driven Type-Driven Development of Certi ed Tree Tree ... · dependent types? Producing better code Conclusion Type-Driven Development of Certi ed Tree Algorithms An Experience

Type-DrivenDevelopmentof Certified

TreeAlgorithms

Introduction

Initial take

Whydependenttypes?

Producingbetter code

Conclusion

Type-driven development?

“Type-driven” in what sense?

Regular development: design the algorithm, and then writetypes to check that you’re correct.

Type-driven development: write types to declare what youwant, and then code until it type checks

It type checks, ship it!

11 / 22

Page 20: Type-Driven Type-Driven Development of Certi ed Tree Tree ... · dependent types? Producing better code Conclusion Type-Driven Development of Certi ed Tree Algorithms An Experience

Type-DrivenDevelopmentof Certified

TreeAlgorithms

Introduction

Initial take

Whydependenttypes?

Producingbetter code

Conclusion

Applying the TDD methodology

At first, we had no clue about what the delete algorithm shouldlook like!

We began with a complete specification:

Definition ddelete(d: nat)(c: color) (num ones : nat)(i : nat)(B : tree w num ones (incr_black d c) c) :{ B' : tree (num - (i < num))(ones - (daccess B i)) d c |dflatten B' = delete (dflatten B) i }.

12 / 22

Page 21: Type-Driven Type-Driven Development of Certi ed Tree Tree ... · dependent types? Producing better code Conclusion Type-Driven Development of Certi ed Tree Algorithms An Experience

Type-DrivenDevelopmentof Certified

TreeAlgorithms

Introduction

Initial take

Whydependenttypes?

Producingbetter code

Conclusion

Applying the TDD methodology

At first, we had no clue about what the delete algorithm shouldlook like!

We began with a complete specification:

Definition ddelete(d: nat)(c: color) (num ones : nat)(i : nat)(B : tree w num ones (incr_black d c) c) :{ B' : tree (num - (i < num))(ones - (daccess B i)) d c |dflatten B' = delete (dflatten B) i }.

12 / 22

Page 22: Type-Driven Type-Driven Development of Certi ed Tree Tree ... · dependent types? Producing better code Conclusion Type-Driven Development of Certi ed Tree Algorithms An Experience

Type-DrivenDevelopmentof Certified

TreeAlgorithms

Introduction

Initial take

Whydependenttypes?

Producingbetter code

Conclusion

Applying the TDD methodology

Finding the missing auxiliary structure

We started to develop our function and found out that weneeded to keep track of whether the height of a node has beendecreased:

Inductive del_tree : nat -> nat -> nat -> color ->Type :=

| Stay : forall {num ones d c} pc,color_ok c (inv pc) -> tree w num ones d c ->del_tree num ones d pc

| Down : forall {num ones d},tree w num ones d Black ->del_tree num ones d.+1 Black.

13 / 22

Page 23: Type-Driven Type-Driven Development of Certi ed Tree Tree ... · dependent types? Producing better code Conclusion Type-Driven Development of Certi ed Tree Algorithms An Experience

Type-DrivenDevelopmentof Certified

TreeAlgorithms

Introduction

Initial take

Whydependenttypes?

Producingbetter code

Conclusion

Applying the TDD methodology

Refining the type

Now, we can write specifications for helper functions as well:

Definition balleft {lnum rnum lones rones d cl cr}(c : color)(l : del_tree lnum lones d cl)(r : tree w rnum rones d cr)(ok_l : color_ok c cl)(ok_r : color_ok c cr) :

{ B' : del_tree (lnum + rnum) (lones + rones)(incr_black d c) c |dflattend B' = dflattend l ++ dflatten r }.

Iterative development process similar to using holes andcase-split iteratively in Agda or Idris.

14 / 22

Page 24: Type-Driven Type-Driven Development of Certi ed Tree Tree ... · dependent types? Producing better code Conclusion Type-Driven Development of Certi ed Tree Algorithms An Experience

Type-DrivenDevelopmentof Certified

TreeAlgorithms

Introduction

Initial take

Whydependenttypes?

Producingbetter code

Conclusion

Extraction

Two types of extraction:

• Extracting ML code from code defined using tactics

• “Extracting” a non-dependently-typed core of thealgorithm within Coq (see ITP talk tomorrow)

15 / 22

Page 25: Type-Driven Type-Driven Development of Certi ed Tree Tree ... · dependent types? Producing better code Conclusion Type-Driven Development of Certi ed Tree Algorithms An Experience

Type-DrivenDevelopmentof Certified

TreeAlgorithms

Introduction

Initial take

Whydependenttypes?

Producingbetter code

Conclusion

Extraction

Two types of extraction:

• Extracting ML code from code defined using tactics

• “Extracting” a non-dependently-typed core of thealgorithm within Coq (see ITP talk tomorrow)

15 / 22

Page 26: Type-Driven Type-Driven Development of Certi ed Tree Tree ... · dependent types? Producing better code Conclusion Type-Driven Development of Certi ed Tree Algorithms An Experience

Type-DrivenDevelopmentof Certified

TreeAlgorithms

Introduction

Initial take

Whydependenttypes?

Producingbetter code

Conclusion

Extraction

Two types of extraction:

• Extracting ML code from code defined using tactics

• “Extracting” a non-dependently-typed core of thealgorithm within Coq (see ITP talk tomorrow)

15 / 22

Page 27: Type-Driven Type-Driven Development of Certi ed Tree Tree ... · dependent types? Producing better code Conclusion Type-Driven Development of Certi ed Tree Algorithms An Experience

Type-DrivenDevelopmentof Certified

TreeAlgorithms

Introduction

Initial take

Whydependenttypes?

Producingbetter code

Conclusion

Take 3: rewrite using Program

Program is a framework for dependently-typed programming inCoq [Sozeau 2006; 2008].

• Cleaner code: automatically generate type coercions forterms

16 / 22

Page 28: Type-Driven Type-Driven Development of Certi ed Tree Tree ... · dependent types? Producing better code Conclusion Type-Driven Development of Certi ed Tree Algorithms An Experience

Type-DrivenDevelopmentof Certified

TreeAlgorithms

Introduction

Initial take

Whydependenttypes?

Producingbetter code

Conclusion

Experiences with Program

The Bad

• Many problems with unification engine: existentialvariables caused a lot of problems

• program_simpl was too aggressive sometimes, destroyinggoals in the process

• Solution: disable program_simpl, unless the goal wasdirectly solved by it.

• Bad error messages and mysterious failures• Error: the kernel does not support existential

variables• Workaround: explicitly match on each argument that

needs to be matched

• Performance issues with Program

• Simplifying and rewriting

17 / 22

Page 29: Type-Driven Type-Driven Development of Certi ed Tree Tree ... · dependent types? Producing better code Conclusion Type-Driven Development of Certi ed Tree Algorithms An Experience

Type-DrivenDevelopmentof Certified

TreeAlgorithms

Introduction

Initial take

Whydependenttypes?

Producingbetter code

Conclusion

Experiences with Program

The Bad

• Many problems with unification engine: existentialvariables caused a lot of problems

• program_simpl was too aggressive sometimes, destroyinggoals in the process

• Solution: disable program_simpl, unless the goal wasdirectly solved by it.

• Bad error messages and mysterious failures• Error: the kernel does not support existential

variables• Workaround: explicitly match on each argument that

needs to be matched

• Performance issues with Program

• Simplifying and rewriting

17 / 22

Page 30: Type-Driven Type-Driven Development of Certi ed Tree Tree ... · dependent types? Producing better code Conclusion Type-Driven Development of Certi ed Tree Algorithms An Experience

Type-DrivenDevelopmentof Certified

TreeAlgorithms

Introduction

Initial take

Whydependenttypes?

Producingbetter code

Conclusion

Experiences with Program

The Bad

• Many problems with unification engine: existentialvariables caused a lot of problems

• program_simpl was too aggressive sometimes, destroyinggoals in the process

• Solution: disable program_simpl, unless the goal wasdirectly solved by it.

• Bad error messages and mysterious failures• Error: the kernel does not support existential

variables• Workaround: explicitly match on each argument that

needs to be matched

• Performance issues with Program

• Simplifying and rewriting

17 / 22

Page 31: Type-Driven Type-Driven Development of Certi ed Tree Tree ... · dependent types? Producing better code Conclusion Type-Driven Development of Certi ed Tree Algorithms An Experience

Type-DrivenDevelopmentof Certified

TreeAlgorithms

Introduction

Initial take

Whydependenttypes?

Producingbetter code

Conclusion

Experiences with Program

The Bad

• Many problems with unification engine: existentialvariables caused a lot of problems

• program_simpl was too aggressive sometimes, destroyinggoals in the process

• Solution: disable program_simpl, unless the goal wasdirectly solved by it.

• Bad error messages and mysterious failures• Error: the kernel does not support existential

variables• Workaround: explicitly match on each argument that

needs to be matched

• Performance issues with Program

• Simplifying and rewriting

17 / 22

Page 32: Type-Driven Type-Driven Development of Certi ed Tree Tree ... · dependent types? Producing better code Conclusion Type-Driven Development of Certi ed Tree Algorithms An Experience

Type-DrivenDevelopmentof Certified

TreeAlgorithms

Introduction

Initial take

Whydependenttypes?

Producingbetter code

Conclusion

Experiences with Program

The Bad

• Many problems with unification engine: existentialvariables caused a lot of problems

• program_simpl was too aggressive sometimes, destroyinggoals in the process

• Solution: disable program_simpl, unless the goal wasdirectly solved by it.

• Bad error messages and mysterious failures• Error: the kernel does not support existential

variables• Workaround: explicitly match on each argument that

needs to be matched

• Performance issues with Program

• Simplifying and rewriting

17 / 22

Page 33: Type-Driven Type-Driven Development of Certi ed Tree Tree ... · dependent types? Producing better code Conclusion Type-Driven Development of Certi ed Tree Algorithms An Experience

Type-DrivenDevelopmentof Certified

TreeAlgorithms

Introduction

Initial take

Whydependenttypes?

Producingbetter code

Conclusion

Experiences with Program

The Bad

• Many problems with unification engine: existentialvariables caused a lot of problems

• program_simpl was too aggressive sometimes, destroyinggoals in the process

• Solution: disable program_simpl, unless the goal wasdirectly solved by it.

• Bad error messages and mysterious failures• Error: the kernel does not support existential

variables• Workaround: explicitly match on each argument that

needs to be matched

• Performance issues with Program

• Simplifying and rewriting

17 / 22

Page 34: Type-Driven Type-Driven Development of Certi ed Tree Tree ... · dependent types? Producing better code Conclusion Type-Driven Development of Certi ed Tree Algorithms An Experience

Type-DrivenDevelopmentof Certified

TreeAlgorithms

Introduction

Initial take

Whydependenttypes?

Producingbetter code

Conclusion

Experiences with Program

The Bad

• Many problems with unification engine: existentialvariables caused a lot of problems

• program_simpl was too aggressive sometimes, destroyinggoals in the process

• Solution: disable program_simpl, unless the goal wasdirectly solved by it.

• Bad error messages and mysterious failures• Error: the kernel does not support existential

variables• Workaround: explicitly match on each argument that

needs to be matched

• Performance issues with Program

• Simplifying and rewriting

17 / 22

Page 35: Type-Driven Type-Driven Development of Certi ed Tree Tree ... · dependent types? Producing better code Conclusion Type-Driven Development of Certi ed Tree Algorithms An Experience

Type-DrivenDevelopmentof Certified

TreeAlgorithms

Introduction

Initial take

Whydependenttypes?

Producingbetter code

Conclusion

Experiences with Program

The Bad

• Many problems with unification engine: existentialvariables caused a lot of problems

• program_simpl was too aggressive sometimes, destroyinggoals in the process

• Solution: disable program_simpl, unless the goal wasdirectly solved by it.

• Bad error messages and mysterious failures• Error: the kernel does not support existential

variables• Workaround: explicitly match on each argument that

needs to be matched

• Performance issues with Program

• Simplifying and rewriting

17 / 22

Page 36: Type-Driven Type-Driven Development of Certi ed Tree Tree ... · dependent types? Producing better code Conclusion Type-Driven Development of Certi ed Tree Algorithms An Experience

Type-DrivenDevelopmentof Certified

TreeAlgorithms

Introduction

Initial take

Whydependenttypes?

Producingbetter code

Conclusion

Experiences with Program

The Bad

• Many problems with unification engine: existentialvariables caused a lot of problems

• program_simpl was too aggressive sometimes, destroyinggoals in the process

• Solution: disable program_simpl, unless the goal wasdirectly solved by it.

• Bad error messages and mysterious failures• Error: the kernel does not support existential

variables• Workaround: explicitly match on each argument that

needs to be matched

• Performance issues with Program

• Simplifying and rewriting

17 / 22

Page 37: Type-Driven Type-Driven Development of Certi ed Tree Tree ... · dependent types? Producing better code Conclusion Type-Driven Development of Certi ed Tree Algorithms An Experience

Type-DrivenDevelopmentof Certified

TreeAlgorithms

Introduction

Initial take

Whydependenttypes?

Producingbetter code

Conclusion

Experiences with Program

The Good

• Readability and writability

• Obligation mechanism improves “modularity”

• Non-structural recursion using measure

18 / 22

Page 38: Type-Driven Type-Driven Development of Certi ed Tree Tree ... · dependent types? Producing better code Conclusion Type-Driven Development of Certi ed Tree Algorithms An Experience

Type-DrivenDevelopmentof Certified

TreeAlgorithms

Introduction

Initial take

Whydependenttypes?

Producingbetter code

Conclusion

Experiences with Program

The Good

• Readability and writability

• Obligation mechanism improves “modularity”

• Non-structural recursion using measure

18 / 22

Page 39: Type-Driven Type-Driven Development of Certi ed Tree Tree ... · dependent types? Producing better code Conclusion Type-Driven Development of Certi ed Tree Algorithms An Experience

Type-DrivenDevelopmentof Certified

TreeAlgorithms

Introduction

Initial take

Whydependenttypes?

Producingbetter code

Conclusion

Experiences with Program

The Good

• Readability and writability

• Obligation mechanism improves “modularity”

• Non-structural recursion using measure

18 / 22

Page 40: Type-Driven Type-Driven Development of Certi ed Tree Tree ... · dependent types? Producing better code Conclusion Type-Driven Development of Certi ed Tree Algorithms An Experience

Type-DrivenDevelopmentof Certified

TreeAlgorithms

Introduction

Initial take

Whydependenttypes?

Producingbetter code

Conclusion

Experiences with Program

The Good

• Readability and writability

• Obligation mechanism improves “modularity”

• Non-structural recursion using measure

18 / 22

Page 41: Type-Driven Type-Driven Development of Certi ed Tree Tree ... · dependent types? Producing better code Conclusion Type-Driven Development of Certi ed Tree Algorithms An Experience

Type-DrivenDevelopmentof Certified

TreeAlgorithms

Introduction

Initial take

Whydependenttypes?

Producingbetter code

Conclusion

The other alternative: Equations

Dependent pattern-matching compiler for Coq [Sozeau 2010;Sozeau & Mangin 2019].

• Even more readable code (Agda-like)

• funelim tactic supports easy pattern-matching in proofs

• No more Axiom K [Sozeau & Mangin 2019]

Equations was the perfect alternative for us, but currently somebugs prevent us from using it with MathComp.

e.g. issues #195, #212, #216, #217, #81 (closed), #179(closed)

19 / 22

Page 42: Type-Driven Type-Driven Development of Certi ed Tree Tree ... · dependent types? Producing better code Conclusion Type-Driven Development of Certi ed Tree Algorithms An Experience

Type-DrivenDevelopmentof Certified

TreeAlgorithms

Introduction

Initial take

Whydependenttypes?

Producingbetter code

Conclusion

The other alternative: Equations

Dependent pattern-matching compiler for Coq [Sozeau 2010;Sozeau & Mangin 2019].

• Even more readable code (Agda-like)

• funelim tactic supports easy pattern-matching in proofs

• No more Axiom K [Sozeau & Mangin 2019]

Equations was the perfect alternative for us, but currently somebugs prevent us from using it with MathComp.

e.g. issues #195, #212, #216, #217, #81 (closed), #179(closed)

19 / 22

Page 43: Type-Driven Type-Driven Development of Certi ed Tree Tree ... · dependent types? Producing better code Conclusion Type-Driven Development of Certi ed Tree Algorithms An Experience

Type-DrivenDevelopmentof Certified

TreeAlgorithms

Introduction

Initial take

Whydependenttypes?

Producingbetter code

Conclusion

The other alternative: Equations

Dependent pattern-matching compiler for Coq [Sozeau 2010;Sozeau & Mangin 2019].

• Even more readable code (Agda-like)

• funelim tactic supports easy pattern-matching in proofs

• No more Axiom K [Sozeau & Mangin 2019]

Equations was the perfect alternative for us, but currently somebugs prevent us from using it with MathComp.

e.g. issues #195, #212, #216, #217, #81 (closed), #179(closed)

19 / 22

Page 44: Type-Driven Type-Driven Development of Certi ed Tree Tree ... · dependent types? Producing better code Conclusion Type-Driven Development of Certi ed Tree Algorithms An Experience

Type-DrivenDevelopmentof Certified

TreeAlgorithms

Introduction

Initial take

Whydependenttypes?

Producingbetter code

Conclusion

The other alternative: Equations

Dependent pattern-matching compiler for Coq [Sozeau 2010;Sozeau & Mangin 2019].

• Even more readable code (Agda-like)

• funelim tactic supports easy pattern-matching in proofs

• No more Axiom K [Sozeau & Mangin 2019]

Equations was the perfect alternative for us, but currently somebugs prevent us from using it with MathComp.

e.g. issues #195, #212, #216, #217, #81 (closed), #179(closed)

19 / 22

Page 45: Type-Driven Type-Driven Development of Certi ed Tree Tree ... · dependent types? Producing better code Conclusion Type-Driven Development of Certi ed Tree Algorithms An Experience

Type-DrivenDevelopmentof Certified

TreeAlgorithms

Introduction

Initial take

Whydependenttypes?

Producingbetter code

Conclusion

The other alternative: Equations

Dependent pattern-matching compiler for Coq [Sozeau 2010;Sozeau & Mangin 2019].

• Even more readable code (Agda-like)

• funelim tactic supports easy pattern-matching in proofs

• No more Axiom K [Sozeau & Mangin 2019]

Equations was the perfect alternative for us, but currently somebugs prevent us from using it with MathComp.

e.g. issues #195, #212, #216, #217, #81 (closed), #179(closed)

19 / 22

Page 46: Type-Driven Type-Driven Development of Certi ed Tree Tree ... · dependent types? Producing better code Conclusion Type-Driven Development of Certi ed Tree Algorithms An Experience

Type-DrivenDevelopmentof Certified

TreeAlgorithms

Introduction

Initial take

Whydependenttypes?

Producingbetter code

Conclusion

The other alternative: Equations

Dependent pattern-matching compiler for Coq [Sozeau 2010;Sozeau & Mangin 2019].

• Even more readable code (Agda-like)

• funelim tactic supports easy pattern-matching in proofs

• No more Axiom K [Sozeau & Mangin 2019]

Equations was the perfect alternative for us, but currently somebugs prevent us from using it with MathComp.

e.g. issues #195, #212, #216, #217, #81 (closed), #179(closed)

19 / 22

Page 47: Type-Driven Type-Driven Development of Certi ed Tree Tree ... · dependent types? Producing better code Conclusion Type-Driven Development of Certi ed Tree Algorithms An Experience

Type-DrivenDevelopmentof Certified

TreeAlgorithms

Introduction

Initial take

Whydependenttypes?

Producingbetter code

Conclusion

Our takeaways

• Dependent types in Coq are often useful. Use them as yousee fit!

• Type-driven development is a great way to write programsthat you don’t know how to write!

• Tactics can be used to write programs, quite reliably

• Coq community needs to look at dependent types more(fix bugs, develop tools, etc.)

20 / 22

Page 48: Type-Driven Type-Driven Development of Certi ed Tree Tree ... · dependent types? Producing better code Conclusion Type-Driven Development of Certi ed Tree Algorithms An Experience

Type-DrivenDevelopmentof Certified

TreeAlgorithms

Introduction

Initial take

Whydependenttypes?

Producingbetter code

Conclusion

Our takeaways

• Dependent types in Coq are often useful. Use them as yousee fit!

• Type-driven development is a great way to write programsthat you don’t know how to write!

• Tactics can be used to write programs, quite reliably

• Coq community needs to look at dependent types more(fix bugs, develop tools, etc.)

20 / 22

Page 49: Type-Driven Type-Driven Development of Certi ed Tree Tree ... · dependent types? Producing better code Conclusion Type-Driven Development of Certi ed Tree Algorithms An Experience

Type-DrivenDevelopmentof Certified

TreeAlgorithms

Introduction

Initial take

Whydependenttypes?

Producingbetter code

Conclusion

Our takeaways

• Dependent types in Coq are often useful. Use them as yousee fit!

• Type-driven development is a great way to write programsthat you don’t know how to write!

• Tactics can be used to write programs, quite reliably

• Coq community needs to look at dependent types more(fix bugs, develop tools, etc.)

20 / 22

Page 50: Type-Driven Type-Driven Development of Certi ed Tree Tree ... · dependent types? Producing better code Conclusion Type-Driven Development of Certi ed Tree Algorithms An Experience

Type-DrivenDevelopmentof Certified

TreeAlgorithms

Introduction

Initial take

Whydependenttypes?

Producingbetter code

Conclusion

Our takeaways

• Dependent types in Coq are often useful. Use them as yousee fit!

• Type-driven development is a great way to write programsthat you don’t know how to write!

• Tactics can be used to write programs, quite reliably

• Coq community needs to look at dependent types more(fix bugs, develop tools, etc.)

20 / 22

Page 51: Type-Driven Type-Driven Development of Certi ed Tree Tree ... · dependent types? Producing better code Conclusion Type-Driven Development of Certi ed Tree Algorithms An Experience

Type-DrivenDevelopmentof Certified

TreeAlgorithms

Introduction

Initial take

Whydependenttypes?

Producingbetter code

Conclusion

Our takeaways

• Dependent types in Coq are often useful. Use them as yousee fit!

• Type-driven development is a great way to write programsthat you don’t know how to write!

• Tactics can be used to write programs, quite reliably

• Coq community needs to look at dependent types more(fix bugs, develop tools, etc.)

20 / 22

Page 52: Type-Driven Type-Driven Development of Certi ed Tree Tree ... · dependent types? Producing better code Conclusion Type-Driven Development of Certi ed Tree Algorithms An Experience

Type-DrivenDevelopmentof Certified

TreeAlgorithms

Introduction

Initial take

Whydependenttypes?

Producingbetter code

Conclusion

Future directions

• Editor support (esp. for Equations)

• Erasure of type indices a la [Brady, McBride & McKinna2003]

• Showing only computationally-relevant terms

• Bug fixes and a better program_simpl tactic

21 / 22

Page 53: Type-Driven Type-Driven Development of Certi ed Tree Tree ... · dependent types? Producing better code Conclusion Type-Driven Development of Certi ed Tree Algorithms An Experience

Type-DrivenDevelopmentof Certified

TreeAlgorithms

Introduction

Initial take

Whydependenttypes?

Producingbetter code

Conclusion

Future directions

• Editor support (esp. for Equations)

• Erasure of type indices a la [Brady, McBride & McKinna2003]

• Showing only computationally-relevant terms

• Bug fixes and a better program_simpl tactic

21 / 22

Page 54: Type-Driven Type-Driven Development of Certi ed Tree Tree ... · dependent types? Producing better code Conclusion Type-Driven Development of Certi ed Tree Algorithms An Experience

Type-DrivenDevelopmentof Certified

TreeAlgorithms

Introduction

Initial take

Whydependenttypes?

Producingbetter code

Conclusion

Future directions

• Editor support (esp. for Equations)

• Erasure of type indices a la [Brady, McBride & McKinna2003]

• Showing only computationally-relevant terms

• Bug fixes and a better program_simpl tactic

21 / 22

Page 55: Type-Driven Type-Driven Development of Certi ed Tree Tree ... · dependent types? Producing better code Conclusion Type-Driven Development of Certi ed Tree Algorithms An Experience

Type-DrivenDevelopmentof Certified

TreeAlgorithms

Introduction

Initial take

Whydependenttypes?

Producingbetter code

Conclusion

Future directions

• Editor support (esp. for Equations)

• Erasure of type indices a la [Brady, McBride & McKinna2003]

• Showing only computationally-relevant terms

• Bug fixes and a better program_simpl tactic

21 / 22

Page 56: Type-Driven Type-Driven Development of Certi ed Tree Tree ... · dependent types? Producing better code Conclusion Type-Driven Development of Certi ed Tree Algorithms An Experience

Type-DrivenDevelopmentof Certified

TreeAlgorithms

Introduction

Initial take

Whydependenttypes?

Producingbetter code

Conclusion

Future directions

• Editor support (esp. for Equations)

• Erasure of type indices a la [Brady, McBride & McKinna2003]

• Showing only computationally-relevant terms

• Bug fixes and a better program_simpl tactic

21 / 22

Page 57: Type-Driven Type-Driven Development of Certi ed Tree Tree ... · dependent types? Producing better code Conclusion Type-Driven Development of Certi ed Tree Algorithms An Experience

Type-DrivenDevelopmentof Certified

TreeAlgorithms

Introduction

Initial take

Whydependenttypes?

Producingbetter code

Conclusion

Final remarks

Come to our ITP talk tomorrow at 16:30!

R. Affeldt, J. Garrigue, X. Qi, K. Tanaka. “Proving TreeAlgorithms for Succinct Data Structures”.

https://github.com/affeldt-aist/succinct

22 / 22