• Aucun résultat trouvé

E cient and automatic recognition of mathematical structures in Coq

N/A
N/A
Protected

Academic year: 2022

Partager "E cient and automatic recognition of mathematical structures in Coq"

Copied!
45
0
0

Texte intégral

(1)

Ecient and automatic recognition of mathematical structures in Coq

Matthias Puech

Laboratoire d'Informatique de l'Ecole Polytechnique, dir. Hugo Herbelin

October 31 2008

(2)

My view of Coq

I High-level tactical language (Ltac)

I Low-level proof/type language (CIC) Some tactics relie on mathematical structures

Dene, Declare

I Dene the object

I Let the system know they fulll some properties

(3)

My view of Coq

I High-level tactical language (Ltac)

I Low-level proof/type language (CIC) Some tactics relie on mathematical structures Dene, Declare

I Dene the object

I Let the system know they fulll some properties

(4)

Example

Definition R := [...].

Lemma R_refl : forall A, reflexive R A.

Lemma R_sym : forall A, symmetric R A.

Lemma R_trans : forall A, transitive R A.

Add Parametric Relation x1 x2 : (A x1 x2) R reflexivity proved by R_refl

symmetry proved by R_sym

transitivity proved by R_trans as R_rel.

Lemma foo : [...].

Proof. transitivity y; reflexivity. Qed.

(5)

Example

Definition R := [...].

Lemma R_refl : forall A, reflexive R A.

Lemma R_sym : forall A, symmetric R A.

Lemma R_trans : forall A, transitive R A.

Add Parametric Relation x1 x2 : (A x1 x2) R reflexivity proved by R_refl

symmetry proved by R_sym

transitivity proved by R_trans as R_rel.

Lemma foo : [...].

Proof. transitivity y; reflexivity. Qed.

(6)

Example

Definition R := [...].

Lemma R_refl : forall A, reflexive R A.

Lemma R_sym : forall A, symmetric R A.

Lemma R_trans : forall A, transitive R A.

Add Parametric Relation x1 x2 : (A x1 x2) R reflexivity proved by R_refl

symmetry proved by R_sym

transitivity proved by R_trans as R_rel.

Lemma foo : [...].

Proof. transitivity y; reflexivity. Qed.

(7)

Exemple

Definition R := [...].

Lemma R_equiv : equivalence R.

Lemma foo : [...].

Proof. transitivity y; reflexivity. Qed.

(8)

Initial goal

Questions

I Can Coq have a consciousness of these structures ?

I Can they be automatically inferred ? More generally

I A step towards inferring trivial reasoning steps ?

(9)

Typeclasses

Ad-hoc polymorphism

Overloading function names, depending on the context.

Example

(+) : int →int →int = plus_int (+) : oat →oat →oat = plus_float (+) : string →string →string = concat

(10)

Typeclasses

Principle

Relation between :

Classes (specication of the functions to overload) Instances (actual implementations in dierent contexts)

I Relation between classes also (inheritance)

I Overloading resolution à la PROLOG

(11)

Example

Class Addable (A:Type) :=

(+) : A -> A -> A.

Instance ex1 : Addable nat :=

(+) := Peano.plus.

Instance ex2 : Addable Z :=

(+) := ZArith.Zplus.

(12)

Example

Class Monoid (A:Type) :=

(*) : A -> A -> A;

assoc : forall a b c, (a * b) * c = a * (b * c);

e : A;

ident_l : forall a, a * e = a;

ident_r : forall a, e * a = a.

Class [Monoid A] => Group :=

inverse : forall x:A, exists y:A, x * y = y * x = e.

(13)

Under the hood

First class implementation (almost only syntactic sugar)

Class =⇒ Record type

Instance =⇒ (dependent) Record Overloaded method =⇒ Field

Parent class =⇒ inferred argument

Resolution =⇒ eauto

Structure recognition =⇒ Instance search

=⇒ Proof search

(14)

Under the hood

First class implementation (almost only syntactic sugar)

Class =⇒ Record type

Instance =⇒ (dependent) Record Overloaded method =⇒ Field

Parent class =⇒ inferred argument

Resolution =⇒ eauto

Structure recognition =⇒ Instance search

=⇒ Proof search

(15)

Under the hood

First class implementation (almost only syntactic sugar)

Class =⇒ Record type

Instance =⇒ (dependent) Record Overloaded method =⇒ Field

Parent class =⇒ inferred argument

Resolution =⇒ eauto

Structure recognition =⇒ Instance search

=⇒ Proof search

(16)

Instance search

Given a classC=hx1:T1, . . . ,xn:Tni, we're searching for i:C.

Decidable subset of the type system:

(var)

Γ,t :T ` t :T

(inst) Γ ` t1 :T1 · · · Γ ` tn:Tn

Γ ` ht1:T1 . . . tn:Tni

Γ ` ti :∀x :T ·Ti Γ,x :T ` h t1:T1. . .(ti x) :Ti. . .tn:Tni Γ ` ∀x :T · ht1 :T1. . .(ti x) :Ti. . .tn:Tni

(17)

Overview of the algorithm

Combinatorial work

We are looking forall possible proofs of C Textual recognition

I ∀ x, _ x x : reexivity lemma⇒ instance of Class Reflexive A (R:relation A) :=

reflexive : forall x, R x x

I ∀ a b c, _ (_ a b) c = _ a (_ b c) : associativity lemmay. Maybe a monoid ?

= Filtering on types

(18)

Usage example

Welcome to Coq trunk (11262) Coq <

Definition R := [...]. R is defined

Coq < Lemma R_sym : symmetric R. auto. Qed. R_refl is defined

new instance Symmetric_1 : Symmetric R

Coq < Lemma R_trans : transitive R. auto. Qed. R_trans is defined

new instance Transitive_1 : Transitive R new instance PER_1 : PER R

Coq < Lemma R_refl : reflexive R. auto. Qed. R_refl is defined

new instance Reflexive_1 : Reflexive R new instance Equivalence_1 : Equivalence R new instance Setoid_1 : Setoid A R

Coq <

(19)

Usage example

Welcome to Coq trunk (11262) Coq < Definition R := [...].

R is defined Coq <

Lemma R_sym : symmetric R. auto. Qed. R_refl is defined

new instance Symmetric_1 : Symmetric R

Coq < Lemma R_trans : transitive R. auto. Qed. R_trans is defined

new instance Transitive_1 : Transitive R new instance PER_1 : PER R

Coq < Lemma R_refl : reflexive R. auto. Qed. R_refl is defined

new instance Reflexive_1 : Reflexive R new instance Equivalence_1 : Equivalence R new instance Setoid_1 : Setoid A R

Coq <

(20)

Usage example

Welcome to Coq trunk (11262) Coq < Definition R := [...].

R is defined

Coq < Lemma R_sym : symmetric R. auto. Qed.

R_refl is defined

new instance Symmetric_1 : Symmetric R Coq <

Lemma R_trans : transitive R. auto. Qed. R_trans is defined

new instance Transitive_1 : Transitive R new instance PER_1 : PER R

Coq < Lemma R_refl : reflexive R. auto. Qed. R_refl is defined

new instance Reflexive_1 : Reflexive R new instance Equivalence_1 : Equivalence R new instance Setoid_1 : Setoid A R

Coq <

(21)

Usage example

Welcome to Coq trunk (11262) Coq < Definition R := [...].

R is defined

Coq < Lemma R_sym : symmetric R. auto. Qed.

R_refl is defined

new instance Symmetric_1 : Symmetric R

Coq < Lemma R_trans : transitive R. auto. Qed.

R_trans is defined

new instance Transitive_1 : Transitive R new instance PER_1 : PER R

Coq <

Lemma R_refl : reflexive R. auto. Qed. R_refl is defined

new instance Reflexive_1 : Reflexive R new instance Equivalence_1 : Equivalence R new instance Setoid_1 : Setoid A R

Coq <

(22)

Usage example

Welcome to Coq trunk (11262) Coq < Definition R := [...].

R is defined

Coq < Lemma R_sym : symmetric R. auto. Qed.

R_refl is defined

new instance Symmetric_1 : Symmetric R

Coq < Lemma R_trans : transitive R. auto. Qed.

R_trans is defined

new instance Transitive_1 : Transitive R new instance PER_1 : PER R

Coq < Lemma R_refl : reflexive R. auto. Qed.

R_refl is defined

new instance Reflexive_1 : Reflexive R new instance Equivalence_1 : Equivalence R new instance Setoid_1 : Setoid A R

Coq <

(23)

Discrimination nets

An ecient structure for one-to-many ltering.

The problem

Given an algebra of termsΛ, I have :

I A pattern p

I A (big) set of terms S

Which terms in S lter the pattern p ?

(24)

Discrimination nets

An ecient structure for one-to-many ltering.

The problem

Given an algebra of termsΛ, I have :

I A pattern p

I A (big) set of terms S

Which terms in S lter the pattern p ?

(25)

Discrimination nets

= A collection datastructure, with pattern searching.

I To a datatype, we associate a structure where each-node represents a list of sub-terms.

I At the leaf of the structure, we store unique identiers Example

type t =

| Var of int

* ident list

| Lam of t

list

| App of t

list

* t

list

(26)

Discrimination nets

= A collection datastructure, with pattern searching.

I To a datatype, we associate a structure where each-node represents a list of sub-terms.

I At the leaf of the structure, we store unique identiers

Example

type t =

| Var of int

* ident list

| Lam of t

list

| App of t

list

* t

list

(27)

Discrimination nets

= A collection datastructure, with pattern searching.

I To a datatype, we associate a structure where each-node represents a list of sub-terms.

I At the leaf of the structure, we store unique identiers

Example

type t =

| Var of int

* ident list

| Lam of t list

| App of t list * t list

(28)

Discrimination nets

= A collection datastructure, with pattern searching.

I To a datatype, we associate a structure where each-node represents a list of sub-terms.

I At the leaf of the structure, we store unique identiers Example

type t =

| Var of int

* ident list

| Lam of t list

| App of t list * t list

(29)

Discrimination nets

= A collection datastructure, with pattern searching.

I To a datatype, we associate a structure where each-node represents a list of sub-terms.

I At the leaf of the structure, we store unique identiers Example

type t =

| Var of int * ident list

| Lam of t list

| App of t list * t list

(30)

Term Search

I To search for a term is to search for a path in the discrimination net.

Example This net :

[Lam; App]

A

A PP

PPP [Var 1; Lam]

1

[Lam] [Lam] [Var 1]

3

[Var 1] 3 [Var 1]

2

contains terms : 12 Lam(Var 1)Lam(Lam(Var 1))

3 App(Lam(Var 1),Lam(Var 1))

=⇒O(|T|)

(31)

Term Search

I To search for a term is to search for a path in the discrimination net.

Example This net :

[Lam; App]

A

A PP

PPP [Var 1; Lam]

1

[Lam] [Lam]

[Var 1]

3

[Var 1]

3 [Var 1]

2

contains terms :

1 Lam(Var 1) 2 Lam(Lam(Var 1))

3 App(Lam(Var 1),Lam(Var 1))

=⇒O(|T|)

(32)

Term Search

I To search for a term is to search for a path in the discrimination net.

Example This net :

[Lam; App]

A

A PP

PPP [Var 1; Lam]

1

[Lam] [Lam]

[Var 1]

3

[Var 1]

3 [Var 1]

2

contains terms : 1 Lam(Var 1)

2 Lam(Lam(Var 1))

3 App(Lam(Var 1),Lam(Var 1))

=⇒O(|T|)

(33)

Term Search

I To search for a term is to search for a path in the discrimination net.

Example This net :

[Lam; App]

A

A PP

PPP [Var 1; Lam]

1

[Lam] [Lam]

[Var 1]

3

[Var 1]

3 [Var 1]

2

contains terms : 12 Lam(Var 1)Lam(Lam(Var 1))

3 App(Lam(Var 1),Lam(Var 1))

=⇒O(|T|)

(34)

Term Search

I To search for a term is to search for a path in the discrimination net.

Example This net :

[Lam; App]

A

A PP

PPP [Var 1; Lam]

1

[Lam] [Lam]

[Var 1]

3

[Var 1]

3 [Var 1]

2

contains terms : 12 Lam(Var 1)Lam(Lam(Var 1))

3 App(Lam(Var 1),Lam(Var 1))

=⇒O(|T|)

(35)

Term Search

I To search for a term is to search for a path in the discrimination net.

Example This net :

[Lam; App]

A

A PP

PPP [Var 1; Lam]

1

[Lam] [Lam]

[Var 1]

3

[Var 1]

3 [Var 1]

2

contains terms : 12 Lam(Var 1)Lam(Lam(Var 1))

3 App(Lam(Var 1),Lam(Var 1))

=⇒O(|T|)

(36)

Filtering

I To search for a pattern is to search for a term, stop at the holes and enumerate terms underneith.

Example

[Lam; App]

A

A PP

PPP [Var 1; Lam]

1

[Lam] [Lam; Var 1]

[Var 1]

3

[Var 1]

3 [Var 1]

2

4

4

App(X,Lam(Y)) ?

= ⇒ T

3 4 ; 3 = 3

(37)

Filtering

I To search for a pattern is to search for a term, stop at the holes and enumerate terms underneith.

Example

[Lam; App]

A

A PP

PPP [Var 1; Lam]

1

[Lam] [Lam; Var 1]

[Var 1]

3

[Var 1]

3 [Var 1]

2

4

4

App(X,Lam(Y)) ?

= ⇒ T

3 4 ; 3 = 3

(38)

Filtering

I To search for a pattern is to search for a term, stop at the holes and enumerate terms underneith.

Example

[Lam; App]

A

A PP

PPP [Var 1; Lam]

1

[Lam] [Lam; Var 1]

[Var 1]

3

[Var 1]

3 [Var 1]

2

4

4

App(X,Lam(Y)) ?

= ⇒ T

3 4 ; 3 = 3

(39)

Current Implementation

Functor :

type t val map val fold val compare





−→





type dn val add

val find_all val fold_pattern Applied to Coq's constr

Primitives :

I add

I find_all

I fold_pattern

Allow to code many typical search problems. . .

(40)

Current Implementation

Functor :

type t val map val fold val compare





−→





type dn val add

val find_all val fold_pattern Applied to Coq's constr

Primitives :

I add

I find_all

I fold_pattern

Allow to code many typical search problems. . .

(41)

Current Implementation

Functor :

type t val map val fold val compare





−→





type dn val add

val find_all val fold_pattern Applied to Coq's constr

Primitives :

I add

I find_all

I fold_pattern

Allow to code many typical search problems. . .

(42)

Current Implementation

Head search

let search_concl pat = possibly_under prod_pat

(search_pat pat) all_types Search for equalities

let search_eq_concl pat = possibly_under prod_pat

(under (eq_pat) (search_pat pat) ) all_types

(43)

Discrimination nets

Multiple variations

I Term/Pattern or Pattern/Term

I Full unication

I Filtering modulo δ,β. . . Numerous applications

I Rewriting systems

I Ecient proof search (Hints)

I Interactive search tools

(44)

To go further

I Use discrimination nets pervasively

I Relax the textual recognition (isomorphisms of types)

I Unify with all the other proof search frameworks

But also,

I Typeclasses were just a pretext, reify all meta-objects to gain control.

(45)

To go further

I Use discrimination nets pervasively

I Relax the textual recognition (isomorphisms of types)

I Unify with all the other proof search frameworks But also,

I Typeclasses were just a pretext, reify all meta-objects to gain control.

Références

Documents relatifs

Now values of type subgoals sensitive act as tactics, for instance by applying them to one particular goal or to all the goals simultaneously.. We could imagine other kinds

For the topmost structure in the hierarchy, encapsulation just amounts to using a dependent record to package a mixin with its representation type.. The sort :&gt; Type

A new transition is created from the first state of the incoming path to the end state of the outgoing path, labeled with the input of the incoming path and the output of the

In a recent, still unpublished paper (&#34;Charged dangling bonds and two-level systems in amorphous solids&#34;) the authors have developed a model for the 2-level sy- stems

The second interpretation is suggested by practices underlying climate modelling as a social and epistemic process: Models are aggregations of expert judgements that result from

We have recently made significant improvements to the system: speed-ups of the checking al- gorithms allow handling of chapter-sized texts with an argumentative granularity

Some properties of the new systems are analyzed: their relation with already known Boussinesq models, the identification of those systems with additional Hamiltonian structure as

As we discuss below, thanks to the high-frequency behavior of the linearized equations, and contrarily to both the full model and the Miyata–Choi–Camassa model, the Kakinuma model has