packaging mathematical structuresgarillot/tphols-slides.pdf · packaging mathematical structures f....

38
Packaging Mathematical Structures F. Garillot 1 , G. Gonthier 2 , A. Mahboubi 3 , L. Rideau 4 1 : Microsoft Research - INRIA Joint Centre 2 : Microsoft Research 3 : INRIA Saclay-Île de France 4 : INRIA Sophia-Antipolis 1

Upload: others

Post on 30-Jun-2020

11 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Packaging Mathematical Structuresgarillot/tphols-slides.pdf · Packaging Mathematical Structures F. Garillot1, G. Gonthier2, A. Mahboubi3, L. Rideau4 1: Microsoft Research - INRIA

Packaging Mathematical Structures

F. Garillot1, G. Gonthier2,A. Mahboubi3, L. Rideau4

1: Microsoft Research - INRIA Joint Centre2: Microsoft Research

3: INRIA Saclay-Île de France4: INRIA Sophia-Antipolis

1

Page 2: Packaging Mathematical Structuresgarillot/tphols-slides.pdf · Packaging Mathematical Structures F. Garillot1, G. Gonthier2, A. Mahboubi3, L. Rideau4 1: Microsoft Research - INRIA

Formalizing Finite Group Algebra

the Feit-Thompson proof

2007 2008 2009 2010 2011

local analysis character theory

problems

linear algebra,

infinite setting

combinatorics,

finite setting

This paper presents our so-

lutions using Coq’s de-

pendent records, coercions,

type inference.

2

Page 3: Packaging Mathematical Structuresgarillot/tphols-slides.pdf · Packaging Mathematical Structures F. Garillot1, G. Gonthier2, A. Mahboubi3, L. Rideau4 1: Microsoft Research - INRIA

Packaging Mathematical Structures

Two proof examples, in depth

Hierarchy details,

a nice alternative to Σ-types

Our algebraic hierarchy

Composable mathematical structures

This talk

Our paper

3

Page 4: Packaging Mathematical Structuresgarillot/tphols-slides.pdf · Packaging Mathematical Structures F. Garillot1, G. Gonthier2, A. Mahboubi3, L. Rideau4 1: Microsoft Research - INRIA

Our Algebraic Hierarchy

Type

Equality

Choice

Zmodule

Ring

Commutative Ring Unit Ring

Commutative Unit Ring

Integral Domain

Field Decidable field Closed Field

Countable

Finite

. . .

See A Modular Formaliza-

tion Of Finite Group Theory,

(TPHOLs 07)

Type

SubType

See the

paper,

§ 3.1.

4

Page 5: Packaging Mathematical Structuresgarillot/tphols-slides.pdf · Packaging Mathematical Structures F. Garillot1, G. Gonthier2, A. Mahboubi3, L. Rideau4 1: Microsoft Research - INRIA

Our Algebraic Hierarchy

Type

Equality

Choice

Zmodule

Ring

Commutative Ring Unit Ring

Commutative Unit Ring

Integral Domain

Field Decidable field Closed Field

Countable

Finite

. . .

See A Modular Formaliza-

tion Of Finite Group Theory,

(TPHOLs 07)

Type

SubType

See the

paper,

§ 3.1.

depth = 10

4-a

Page 6: Packaging Mathematical Structuresgarillot/tphols-slides.pdf · Packaging Mathematical Structures F. Garillot1, G. Gonthier2, A. Mahboubi3, L. Rideau4 1: Microsoft Research - INRIA

Our Algebraic Hierarchy

practical

purpose

(nothing here)

Type

Equality

Choice

Zmodule

Ring

Commutative Ring Unit Ring

Commutative Unit Ring

Integral Domain

Field Decidable field Closed Field

Countable

Finite

. . .

See A Modular Formaliza-

tion Of Finite Group Theory,

(TPHOLs 07)

Type

SubType

See the

paper,

§ 3.1.

4-b

Page 7: Packaging Mathematical Structuresgarillot/tphols-slides.pdf · Packaging Mathematical Structures F. Garillot1, G. Gonthier2, A. Mahboubi3, L. Rideau4 1: Microsoft Research - INRIA

Our Algebraic Hierarchy

multiple

inheri-

tance

Type

Equality

Choice

Zmodule

Ring

Commutative Ring Unit Ring

Commutative Unit Ring

Integral Domain

Field Decidable field Closed Field

Countable

Finite

. . .

See A Modular Formaliza-

tion Of Finite Group Theory,

(TPHOLs 07)

Type

SubType

See the

paper,

§ 3.1.

4-c

Page 8: Packaging Mathematical Structuresgarillot/tphols-slides.pdf · Packaging Mathematical Structures F. Garillot1, G. Gonthier2, A. Mahboubi3, L. Rideau4 1: Microsoft Research - INRIA

Our Algebraic Hierarchy

Blue nodes collapse

on green in a clas-

sical/untyped setting.

infinite

quotients in

intentional

setting

Type

Equality

Choice

Zmodule

Ring

Commutative Ring Unit Ring

Commutative Unit Ring

Integral Domain

Field Decidable field Closed Field

Countable

Finite

. . .

See A Modular Formaliza-

tion Of Finite Group Theory,

(TPHOLs 07)

Type

SubType

See the

paper,

§ 3.1.

4-d

Page 9: Packaging Mathematical Structuresgarillot/tphols-slides.pdf · Packaging Mathematical Structures F. Garillot1, G. Gonthier2, A. Mahboubi3, L. Rideau4 1: Microsoft Research - INRIA

Our Algebraic Hierarchy

better

composi-

tionality

Type

Equality

Choice

Zmodule

Ring

Commutative Ring Unit Ring

Commutative Unit Ring

Integral Domain

Field Decidable field Closed Field

Countable

Finite

. . .

See A Modular Formaliza-

tion Of Finite Group Theory,

(TPHOLs 07)

Type

SubType

See the

paper,

§ 3.1.

4-e

Page 10: Packaging Mathematical Structuresgarillot/tphols-slides.pdf · Packaging Mathematical Structures F. Garillot1, G. Gonthier2, A. Mahboubi3, L. Rideau4 1: Microsoft Research - INRIA

Our Algebraic Hierarchy

5

Page 11: Packaging Mathematical Structuresgarillot/tphols-slides.pdf · Packaging Mathematical Structures F. Garillot1, G. Gonthier2, A. Mahboubi3, L. Rideau4 1: Microsoft Research - INRIA

Our Algebraic Hierarchy

(1) This hierarchy is populated by ground types, e.g.: Z/nZ,Fp

5-a

Page 12: Packaging Mathematical Structuresgarillot/tphols-slides.pdf · Packaging Mathematical Structures F. Garillot1, G. Gonthier2, A. Mahboubi3, L. Rideau4 1: Microsoft Research - INRIA

Our Algebraic Hierarchy

(1) This hierarchy is populated by ground types, e.g.: Z/nZ,Fp

(2) . . . and parametric types, e.g.: matrix, polynomials.

5-b

Page 13: Packaging Mathematical Structuresgarillot/tphols-slides.pdf · Packaging Mathematical Structures F. Garillot1, G. Gonthier2, A. Mahboubi3, L. Rideau4 1: Microsoft Research - INRIA

Our Algebraic Hierarchy

(1) This hierarchy is populated by ground types, e.g.: Z/nZ,Fp

(2) . . . and parametric types, e.g.: matrix, polynomials.

(3) Very little computational content or theory in a Ring

5-c

Page 14: Packaging Mathematical Structuresgarillot/tphols-slides.pdf · Packaging Mathematical Structures F. Garillot1, G. Gonthier2, A. Mahboubi3, L. Rideau4 1: Microsoft Research - INRIA

Our Algebraic Hierarchy

(1) This hierarchy is populated by ground types, e.g.: Z/nZ,Fp

(2) . . . and parametric types, e.g.: matrix, polynomials.

(3) Very little computational content or theory in a Ring

(4) but it is a nice package that occurs several times in lemma statements.

5-d

Page 15: Packaging Mathematical Structuresgarillot/tphols-slides.pdf · Packaging Mathematical Structures F. Garillot1, G. Gonthier2, A. Mahboubi3, L. Rideau4 1: Microsoft Research - INRIA

Our Algebraic Hierarchy

(1) This hierarchy is populated by ground types, e.g.: Z/nZ,Fp

(2) . . . and parametric types, e.g.: matrix, polynomials.

(3) Very little computational content or theory in a Ring

(4) but it is a nice package that occurs several times in lemma statements.

(5)[

Components, objects]

6=[

APIs, interfaces]

5-e

Page 16: Packaging Mathematical Structuresgarillot/tphols-slides.pdf · Packaging Mathematical Structures F. Garillot1, G. Gonthier2, A. Mahboubi3, L. Rideau4 1: Microsoft Research - INRIA

Our Algebraic Hierarchy

(1) This hierarchy is populated by ground types, e.g.: Z/nZ,Fp

(2) . . . and parametric types, e.g.: matrix, polynomials.

(3) Very little computational content or theory in a Ring

(4) but it is a nice package that occurs several times in lemma statements.

(5)[

Components, objects]

6=[

APIs, interfaces]

(6) algebraic properties =̂ interface programming

5-f

Page 17: Packaging Mathematical Structuresgarillot/tphols-slides.pdf · Packaging Mathematical Structures F. Garillot1, G. Gonthier2, A. Mahboubi3, L. Rideau4 1: Microsoft Research - INRIA

Representing objects and structures

We want to manipulate objects:

6

Page 18: Packaging Mathematical Structuresgarillot/tphols-slides.pdf · Packaging Mathematical Structures F. Garillot1, G. Gonthier2, A. Mahboubi3, L. Rideau4 1: Microsoft Research - INRIA

Representing objects and structures

We want to manipulate objects:

(Cayley-Hamilton)

[

P (A) = 0

where P (X) = det(X • In − A)

]

6-a

Page 19: Packaging Mathematical Structuresgarillot/tphols-slides.pdf · Packaging Mathematical Structures F. Garillot1, G. Gonthier2, A. Mahboubi3, L. Rideau4 1: Microsoft Research - INRIA

Representing objects and structures

We want to manipulate objects:

(Cayley-Hamilton)

[

P (A) = 0

where P (X) = det(X • In − A)

]

But we do not want to specify the corresponding structure:

The Ring of polynomials over

the Ring of matrices

over a general Commutative Ring.

6-b

Page 20: Packaging Mathematical Structuresgarillot/tphols-slides.pdf · Packaging Mathematical Structures F. Garillot1, G. Gonthier2, A. Mahboubi3, L. Rideau4 1: Microsoft Research - INRIA

A Type class

� a constant zero : M

� an operation : add : M-> M -> M

� axiom(s) verified by add on M

associative add;

� . . .

ZModule (M:Type)

C elements per structure,

n nested (parametric) struc-

tures in which the parame-

ter occurs at every element:

term size in Cn

� How do we pass structures when enunciating lemmas ?

� Proofs introduce structures.

7

Page 21: Packaging Mathematical Structuresgarillot/tphols-slides.pdf · Packaging Mathematical Structures F. Garillot1, G. Gonthier2, A. Mahboubi3, L. Rideau4 1: Microsoft Research - INRIA

A Typical structure

We fill the blanks using Canonical Structures.

operation on the type

Module Equality.

Record mixin_of (T : Type) : Type :=

Mixin { op : rel T; _ : forall x y, reflect (x = y) (op x y) }.

Structure type : Type :=

Pack { sort :> Type; mixin : mixin_of sort}.

End Equality.

representation type

axiom(s) verified by the operation

projection

8

Page 22: Packaging Mathematical Structuresgarillot/tphols-slides.pdf · Packaging Mathematical Structures F. Garillot1, G. Gonthier2, A. Mahboubi3, L. Rideau4 1: Microsoft Research - INRIA

A Typical structure

We fill the blanks using Canonical Structures.

Module Equality.

Record mixin_of (T : Type) : Type :=

Mixin { : _ ; _ : _ }.

Structure type : Type :=

Pack {sort :> Type; mixin : mixin_of sort}.

End Equality.

This is generic: we use modules as namespaces only:

Notation eqType := Equality.type.

9

Page 23: Packaging Mathematical Structuresgarillot/tphols-slides.pdf · Packaging Mathematical Structures F. Garillot1, G. Gonthier2, A. Mahboubi3, L. Rideau4 1: Microsoft Research - INRIA

Canonical Structure Composition

� Most widely used: Telescopes

T

MixinEq

opzmod

axiomszmod

MixinZmodopzmod

axiomszmod

eqType

zmodType

10

Page 24: Packaging Mathematical Structuresgarillot/tphols-slides.pdf · Packaging Mathematical Structures F. Garillot1, G. Gonthier2, A. Mahboubi3, L. Rideau4 1: Microsoft Research - INRIA

Telescopes : Canonical Structure inference

2+ 2

Zmodule.add α 2 2

Equality.sort(Zmodule.sortα) ≡βιδ int

Zmodule.sortα ≡βιδ inteqType

Equality.sort (Zmodule.sort (intzmodType))

Notation

type inference

lookup (Equality.sort,int) ; inteqType

lookup (Zmodule.sort,inteqType) ; intzmodType

11

Page 25: Packaging Mathematical Structuresgarillot/tphols-slides.pdf · Packaging Mathematical Structures F. Garillot1, G. Gonthier2, A. Mahboubi3, L. Rideau4 1: Microsoft Research - INRIA

Telescopes : Canonical Structure inference

2+ 2

Zmodule.add α 2 2

Equality.sort(Zmodule.sortα) ≡βιδ int

Zmodule.sortα ≡βιδ inteqType

Equality.sort (Zmodule.sort (intzmodType))

Notation

type inference

lookup (Equality.sort,int) ; inteqType

lookup (Zmodule.sort,inteqType) ; intzmodType

� Simple packaging, but

11-a

Page 26: Packaging Mathematical Structuresgarillot/tphols-slides.pdf · Packaging Mathematical Structures F. Garillot1, G. Gonthier2, A. Mahboubi3, L. Rideau4 1: Microsoft Research - INRIA

Telescopes : Canonical Structure inference

2+ 2

Zmodule.add α 2 2

Equality.sort(Zmodule.sortα) ≡βιδ int

Zmodule.sortα ≡βιδ inteqType

Equality.sort (Zmodule.sort (intzmodType))

Notation

type inference

lookup (Equality.sort,int) ; inteqType

lookup (Zmodule.sort,inteqType) ; intzmodType

� Simple packaging, but

� head constant always the same x:T is interpreted as

x:Equality.sort(Zmodule.sort T)

11-b

Page 27: Packaging Mathematical Structuresgarillot/tphols-slides.pdf · Packaging Mathematical Structures F. Garillot1, G. Gonthier2, A. Mahboubi3, L. Rideau4 1: Microsoft Research - INRIA

Telescopes : Canonical Structure inference

2+ 2

Zmodule.add α 2 2

Equality.sort(Zmodule.sortα) ≡βιδ int

Zmodule.sortα ≡βιδ inteqType

Equality.sort (Zmodule.sort (intzmodType))

Notation

type inference

lookup (Equality.sort,int) ; inteqType

lookup (Zmodule.sort,inteqType) ; intzmodType

� Simple packaging, but

� head constant always the same x:T is interpreted as

x:Equality.sort(Zmodule.sort T)

� we’re defining coercions/canonical projections on Equality.sort !

11-c

Page 28: Packaging Mathematical Structuresgarillot/tphols-slides.pdf · Packaging Mathematical Structures F. Garillot1, G. Gonthier2, A. Mahboubi3, L. Rideau4 1: Microsoft Research - INRIA

Telescopes : Canonical Structure inference

2+ 2

Zmodule.add α 2 2

Equality.sort(Zmodule.sortα) ≡βιδ int

Zmodule.sortα ≡βιδ inteqType

Equality.sort (Zmodule.sort (intzmodType))

Notation

type inference

lookup (Equality.sort,int) ; inteqType

lookup (Zmodule.sort,inteqType) ; intzmodType

� Simple packaging, but

� head constant always the same x:T is interpreted as

x:Equality.sort(Zmodule.sort T)

� we’re defining coercions/canonical projections on Equality.sort !

� as is, no multiple inheritance

11-d

Page 29: Packaging Mathematical Structuresgarillot/tphols-slides.pdf · Packaging Mathematical Structures F. Garillot1, G. Gonthier2, A. Mahboubi3, L. Rideau4 1: Microsoft Research - INRIA

Packed Classes

T

ClassEq

opeq

axiomseq

MixinZmodopzmod

axiomszmod

Classzmod

zmodType

12

Page 30: Packaging Mathematical Structuresgarillot/tphols-slides.pdf · Packaging Mathematical Structures F. Garillot1, G. Gonthier2, A. Mahboubi3, L. Rideau4 1: Microsoft Research - INRIA

Packed Classes

T

ClassEq

opeq

axiomseq

MixinZmodopzmod

axiomszmod

Classzmod

zmodType

T Classeq

ϕϕ

eqType

x : T denotes

x : (Zmodule.sort T α),

which, thanks to ϕ, has a

canonical eqType structure.

12-a

Page 31: Packaging Mathematical Structuresgarillot/tphols-slides.pdf · Packaging Mathematical Structures F. Garillot1, G. Gonthier2, A. Mahboubi3, L. Rideau4 1: Microsoft Research - INRIA

Packed Classes

2+ 2 == 4

Equality.op γ(Zmodule.add α 2 2) 4

Zmodule.sort α ≡ int︸ ︷︷ ︸

Zmodule.sort intzmodType

Equality.sort γ ≡ Zmodule.sort intzmodType

Equality.op (Zmodule.eqType(intzmodType))(Zmodule.add ...) 4

Notation

lookup(Zmodule.sort,int)

; intzmodType

lookup(Equality.sort,Zmodule.sort intzmodType)

; Zmodule.eqType(intzmodType)

4 == 2+ 2

Equality.op γ (Zmodule.add α 2 2) 4

Equality.op inteqType (Zmodule.add α 2 2) 4

...

Notation

lookup(Equality.sort,int) ; inteqType

≡βιδ

13

Page 32: Packaging Mathematical Structuresgarillot/tphols-slides.pdf · Packaging Mathematical Structures F. Garillot1, G. Gonthier2, A. Mahboubi3, L. Rideau4 1: Microsoft Research - INRIA

Packed Classes

� Coherent coercions,

� x:T interpreted as Zmodule.sort T.

� Multiple inheritance:Module ComUnitRing.

Record class_of (R : Type) : Type := Class {

base1 :> ComRing.class_of R;

ext :> UnitRing.mixin_of (Ring.Pack base1 R)}.

Coercion base2 R m := UnitRing.Class (@ext R m).

...

End ComUnitRing.

14

Page 33: Packaging Mathematical Structuresgarillot/tphols-slides.pdf · Packaging Mathematical Structures F. Garillot1, G. Gonthier2, A. Mahboubi3, L. Rideau4 1: Microsoft Research - INRIA

Conclusion

� algebraic structure composition is interface programming

� representing a large hierarchy implies packaging

� know the work ahead,

� and take the time to build the tools you will need

15

Page 34: Packaging Mathematical Structuresgarillot/tphols-slides.pdf · Packaging Mathematical Structures F. Garillot1, G. Gonthier2, A. Mahboubi3, L. Rideau4 1: Microsoft Research - INRIA

Cn

16

Page 35: Packaging Mathematical Structuresgarillot/tphols-slides.pdf · Packaging Mathematical Structures F. Garillot1, G. Gonthier2, A. Mahboubi3, L. Rideau4 1: Microsoft Research - INRIA

Cn

� A + x

16-a

Page 36: Packaging Mathematical Structuresgarillot/tphols-slides.pdf · Packaging Mathematical Structures F. Garillot1, G. Gonthier2, A. Mahboubi3, L. Rideau4 1: Microsoft Research - INRIA

Cn

� A + x

� add (matrix T) (zeromx T) (addmx T)

16-b

Page 37: Packaging Mathematical Structuresgarillot/tphols-slides.pdf · Packaging Mathematical Structures F. Garillot1, G. Gonthier2, A. Mahboubi3, L. Rideau4 1: Microsoft Research - INRIA

Cn

� A + x

� add (matrix T) (zeromx T) (addmx T)

� ... (addmx (poly T) (zeropx T) (addpx T) )

16-c

Page 38: Packaging Mathematical Structuresgarillot/tphols-slides.pdf · Packaging Mathematical Structures F. Garillot1, G. Gonthier2, A. Mahboubi3, L. Rideau4 1: Microsoft Research - INRIA

Cn

� A + x

� add (matrix T) (zeromx T) (addmx T)

� ... (addmx (poly T) (zeropx T) (addpx T) )

� and zeromx is itself (zeromx (poly T) (zeropx T) (addpx T) ) ...

16-d