Theory Presentation Combinators

Jacques Carette, and Russell O’Connor McMaster University

CICM 2012, Bremen, Germany

12th July 2012 Motivation As part of MathScheme, we wish to efficiently encode mathematical knowledge. 1. Efficient for the library developper 2. Efficient for the user 3. Efficient for processing Focus first on Theory Presentations

I Over a (dependently) typed expression language I Syntax for meaningful content

Carette, O’Connor Theory Presentation Combinators 2/18 Theories

Monoid := Theory { U: type ; ∗ :(U,U) −> U; e : U; axiom rightIdentity ∗ e : f o r a l l x :U. x∗ e = x ; axiom leftIdentity ∗ e : f o r a l l x :U. e ∗x = x ; axiom associative ∗ : f o r a l l x , y , z :U. ( x∗y )∗ z=x ∗( y∗ z ) }

Carette, O’Connor Theory Presentation Combinators 3/18 Theories

Monoid := Theory { U: type ; ∗ :(U,U) −> U; e : U; axiom rightIdentity ∗ e : f o r a l l x :U. x∗ e = x ; axiom leftIdentity ∗ e : f o r a l l x :U. e ∗x = x ; axiom associative ∗ : f o r a l l x , y , z :U. ( x∗y )∗ z=x ∗( y∗ z ) }

CommutativeMonoid := Theory { U: type ; ∗ :(U,U) −> U; e : U; axiom rightIdentity ∗ e : f o r a l l x :U. x∗ e = x ; axiom leftIdentity ∗ e : f o r a l l x :U. e ∗x = x ; axiom associative ∗ : f o r a l l x , y , z :U. ( x∗y )∗ z=x ∗( y∗ z ) axiom commutative ∗ : f o r a l l x , y , z :U. x∗y=y∗x }

Carette, O’Connor Theory Presentation Combinators 3/18 Theories

Monoid := Theory { U: type ; ∗ :(U,U) −> U; e : U; axiom rightIdentity ∗ e : f o r a l l x :U. x∗ e = x ; axiom leftIdentity ∗ e : f o r a l l x :U. e ∗x = x ; axiom associative ∗ : f o r a l l x , y , z :U. ( x∗y )∗ z=x ∗( y∗ z ) }

AdditiveMonoid := Theory { U: type ; + : (U, U) −> U; 0 : U; axiom rightIdentity + 0 : f o r a l l x :U. x+0 = x ; axiom leftIdentity + 0 : f o r a l l x :U. 0+x = x ; axiom associative + : f o r a l l x,y,z:U. (x+y)+z=x+(y+z) }

Carette, O’Connor Theory Presentation Combinators 3/18 Theories

Monoid := Theory { U: type ; ∗ :(U,U) −> U; e : U; axiom rightIdentity ∗ e : f o r a l l x :U. x∗ e = x ; axiom leftIdentity ∗ e : f o r a l l x :U. e ∗x = x ; axiom associative ∗ : f o r a l l x , y , z :U. ( x∗y )∗ z=x ∗( y∗ z ) }

AdditiveCommutativeMonoid := Theory { U: type ; + : (U, U) −> U; 0 : U; axiom rightIdentity + 0 : f o r a l l x :U. x+0 = x ; axiom leftIdentity + 0 : f o r a l l x :U. 0+x = x ; axiom associative + : f o r a l l x,y,z:U. (x+y)+z=x+(y+z) axiom commutative + : f o r a l l x , y , z :U. x+y=y+x }

Carette, O’Connor Theory Presentation Combinators 3/18 Renaming: AdditiveMonoid := Monoid[ ∗ |−> +, e |−> 0 ] Combination: AdditiveCommutativeMonoid := combine AdditiveMonoid , CommutativeMonoid over Monoid

Combinators for theories Extension: CommutativeMonoid := Monoid extended by { axiom commutative ∗ : f o r a l l x , y , z :U. x∗y=y∗x }

Carette, O’Connor Theory Presentation Combinators 4/18 Combination: AdditiveCommutativeMonoid := combine AdditiveMonoid , CommutativeMonoid over Monoid

Combinators for theories Extension: CommutativeMonoid := Monoid extended by { axiom commutative ∗ : f o r a l l x , y , z :U. x∗y=y∗x } Renaming: AdditiveMonoid := Monoid[ ∗ |−> +, e |−> 0 ]

Carette, O’Connor Theory Presentation Combinators 4/18 Combinators for theories Extension: CommutativeMonoid := Monoid extended by { axiom commutative ∗ : f o r a l l x , y , z :U. x∗y=y∗x } Renaming: AdditiveMonoid := Monoid[ ∗ |−> +, e |−> 0 ] Combination: AdditiveCommutativeMonoid := combine AdditiveMonoid , CommutativeMonoid over Monoid

Carette, O’Connor Theory Presentation Combinators 4/18 A fraction of the Algebraic Zoo

ModalAlgebra

ModalFixes ModalDistributes KleeneLattice VectorSpace BooleanAlgebra

UnaryDistributes DeMorganAlgebra PseudoComplement RModule GoedelAlgebra

FixedPoint OckhamAlgebra RightRModule LeftRModule CommutativeRingAction HeytingAlgebra

DualdeMorgan RightRingAction LeftRingAction ImplicationUnipotentMagma ModularOrthoLattice BoundedDistributiveLattice LeftDistributiveImplication Implicative

AnddeMorgan OrdeMorgan PreRightRingAction PreLeftRingAction OrthoLattice IntegralDomain BoundedModularLattice ComplementedLattice BooleanRing LeftImplicative RightImplicative

PrimeRingoidSig MultRightMonoidAction PrimeAdditiveGroup MultMonoidAction DivisionRing BoundedLattice CommutativeRing DistributiveLattice MeetImplicativeSig

RightMonoid1Action LeftMonoid1Action OrthoModularLattice KleeneAlgebra NonassociativeRing NoZeroDivisors ModularLattice BoundedJoinLattice BoundedMeetLattice

OrthoComplement StarSemiring MoufangLoop Dioid Rng NonassociativeNondistributiveRing Modularity BoundedJoinSemilattice

Asteration AbelianGroup Loop NearRing SemiRng MoufangIdentity AbelianAdditiveGroup Semifield BoundedMeetSemilattice LeftAbsorptionOp DualSemilattices IdempotentAdditiveMagma

LeftAsteration RightAsteration RightGroupAction ModalAlgebraSig LeftGroupAction Group1 OrthoLatticeSig RightCommutator LeftCommutator Left0 AdditiveCommutativeMonoid AdditiveGroup NearSemifield HeytingAlgebraSig CommutativeMonoid1 Ringoid LeftAbsorption JoinSemilattice

StarSemiringSig LogicAlgebra RightMonoidAction LeftMonoidAction Ringoid01Sig Monoid1 AdditiveMonoid BooleanGroup Sloop LeftLoop RightQuasiGroup CommutativeMonoid NearSemiring CommutativeAdditiveMagma LeftRingoid MeetSemilattice Quandle

Inverse ComplementRightZero RightMagmaAction RightUnitalAction Right0 InvolutiveSemigroup Zero LeftUnitalAction LeftSemigroupAction Ringoid1Sig Monoid UnipotentPointedMagma PointedSteinerMagma RightCancellative RightCancellativeOp LeftQuasiGroup AdditiveSemigroup Ringoid0Sig RightRingoid Squag CommutativeBand RectangularBand NormalBand Directoid LeftSpindle RightSpindle Rack Shelf Chain

InvolutiveMagma RightInverse LeftInverse RightUnitalActionSig RightZero LeftZero LeftUnitalActionSig LeftMagmaAction Unital RightSemigroupAction LeftMonoid RightMonoid RightBiMagma LeftCancellativeOp LeftCancellative CommutativeSemigroup PointedCommutativeMagma RingoidSig SteinerMagma Band InducedRelation LeftIdempotence RightIdempotence LeftBinaryInverse RightBinaryInverse LeftShelf RightShelf UpDirectedSet TotalOrder DownDirectedSet

Involution UnaryAntiDistribution PointComplement LogicAlgebraSig InverseSig RightMagmaActionSig LeftMagmaActionSig RightPointAction LeftPointAction LeftUnital RightUnital LeftBiMagma CommutativeMagma MiddleCommute IdempotentMagma RackSig Sink PartialOrder UpperBound TotalPreorder LowerBound SimpleGraph

InvolutiveMagmaSig ComplementedSig InvolutivePointedMagmaSig RightOperation LeftOperation Pair PointedMagma LeftDivisionMagma ImplicativeSig AdditiveMagma AntiAbsorbent MiddleAbsorption DirectoidIdentities RightShelfSig LeftShelfSig LeftDistributiveMagma RightDistributiveMagma SinkSig AntisymmetricOrderRelation Preorder EquivalenceRelation Graph Digraph

StarSig Necessity ComplementSig PointedUnarySystem DoublyPointed MultiCarrier Pointed1Sig Pointed0Sig TransitiveOrderRelation StrictPartialOrder ReflexiveOrderRelation SymmetricOrderRelation TotalRelation PseudoGraph

UnaryOperation CarrierS PointedCarrier TransitiveRelation IrreflexiveRelation OrderRelation

Pointed2Carrier UtoS SubType BinaryRelation

Carrier

Empty

Base

Carette, O’Connor Theory Presentation Combinators 5/18 Library fragment 1

NearSemiring := combine AdditiveSemigroup , Semigroup , RightRingoid over R i n g o i d S i g NearSemifield := combine NearSemiring , Group over Semigroup Semifield := combine NearSemifield , LeftRingoid over R i n g o i d S i g NearRing := combine AdditiveGroup , Semigroup , RightRingoid over R i n g o i d S i g Rng := combine AbelianAdditiveGroup , Semigroup , Ringoid over R i n g o i d S i g S e m i r i n g := combine AdditiveCommutativeMonoid , Monoid1, Ringoid , Left0 over Ringoid0Sig SemiRng := combine AdditiveCommutativeMonoid , Semigroup , Ringoid over R i n g o i d S i g D i o i d := combine Semiring , IdempotentAdditiveMagma over AdditiveMagma Ring := combine Rng, Semiring over SemiRng CommutativeRing := combine Ring , CommutativeMagma over Magma BooleanRing := combine CommutativeRing , IdempotentMagma over Magma NoZeroDivisors := Ringoid0Sig extended by { axiom onlyZeroDivisor ∗ 0 : f o r a l l x :U. ((exists b:U. x∗b = 0) and (exists b:U. b∗x = 0 ) ) i m p l i e s ( x = 0) } Domain := combine Ring, NoZeroDivisors over Ringoid0Sig IntegralDomain := combine CommutativeRing , NoZeroDivisors over Ringoid0Sig DivisionRing := Ring extended by { axiom d i v i s i b l e : f o r a l l x :U. not ( x=0) i m p l i e s ((exists! y:U. y∗x = 1) and (exists! y:U. x∗y = 1 ) ) } F i e l d := combine DivisionRing , IntegralDomain over Ring

Carette, O’Connor Theory Presentation Combinators 6/18 Library fragment 2

MoufangLoop := combine Loop, MoufangIdentity over Magma LeftShelfSig := Magma[ ∗ |−> |> ] LeftShelf := LeftDistributiveMagma [ ∗ |−> |> ] RightShelfSig := Magma[ ∗ |−> <| ] RightShelf := RightDistributiveMagma[ ∗ |−> <| ] RackSig := combine LeftShelfSig , RightShelfSig over C a r r i e r S h e l f := combine LeftShelf , RightShelf over RackSig LeftBinaryInverse := RackSig extended by { axiom leftInverse |> <| : f o r a l l x , y :U. ( x |> y ) <| x = y } RightBinaryInverse := RackSig extended by { axiom rightInverse |> <| : f o r a l l x , y :U. x |> ( y <| x ) = y } Rack := combine RightShelf , LeftShelf , LeftBinaryInverse , RightBinaryInverse over RackSig LeftIdempotence := IdempotentMagma[ ∗ |−> |> ] RightIdempotence := IdempotentMagma[ ∗ |−> <| ] LeftSpindle := combine LeftShelf , LeftIdempotence over LeftShelfSig RightSpindle := combine RightShelf , RightIdempotence over RightShelfSig Quandle := combine Rack, LeftSpindle , RightSpindle over S h e l f

Carette, O’Connor Theory Presentation Combinators 7/18 What we have A decent library of theories An expander Mostly complete export (of expanded version) to MMT/OpenMath Mostly complete export (of expanded version) to Matita In-progress: “export” to metaocaml and Template Haskell

Carette, O’Connor Theory Presentation Combinators 8/18 We don’t think it works well enough!

T1 := Theory { n : I n t e g e r } T2 := Theory { n : N a t u r a l } T3 := combine T1 , T2 over Empty result(s):

But what does it mean? Intuitively: work in some category of signatures

I extend: embedding I renaming: renaming! I combine: pushout Lots of precedent (Goguen and Burstall, D. Smith, and many many followers)

Carette, O’Connor Theory Presentation Combinators 9/18 But what does it mean? Intuitively: work in some category of signatures

I extend: embedding I renaming: renaming! I combine: pushout Lots of precedent (Goguen and Burstall, D. Smith, and many many followers) We don’t think it works well enough!

T1 := Theory { n : I n t e g e r } T2 := Theory { n : N a t u r a l } T3 := combine T1 , T2 over Empty result(s):

Carette, O’Connor Theory Presentation Combinators 9/18 But what does it mean? Intuitively: work in some category of signatures

I extend: embedding I renaming: renaming! I combine: pushout Lots of precedent (Goguen and Burstall, D. Smith, and many many followers) We don’t think it works well enough!

T1 := Theory { n : I n t e g e r } T2 := Theory { n : N a t u r a l } T3 := combine T1 , T2 over Empty result(s):

T3 := Theory { n$234 : n$235 : Natural }

Carette, O’Connor Theory Presentation Combinators 9/18 But what does it mean? Intuitively: work in some category of signatures

I extend: embedding I renaming: renaming! I combine: pushout Lots of precedent (Goguen and Burstall, D. Smith, and many many followers) We don’t think it works well enough!

T1 := Theory { n : I n t e g e r } T2 := Theory { n : N a t u r a l } T3 := combine T1 , T2 over Empty result(s):

T3 := Theory { T1/n : Integer T2/n : Natural }

Carette, O’Connor Theory Presentation Combinators 9/18 But what does it mean? Intuitively: work in some category of signatures

I extend: embedding I renaming: renaming! I combine: pushout Lots of precedent (Goguen and Burstall, D. Smith, and many many followers) We don’t think it works well enough!

T1 := Theory { n : I n t e g e r } T2 := Theory { n : N a t u r a l } T3 := combine T1 , T2 over Empty result(s):

The problem: 1. theory does not distinguish between isomorphic presentations 2. humans distinguish them, to a point

Carette, O’Connor Theory Presentation Combinators 9/18 The Semantics of Syntax qua Syntax We need a semantics of our language(s) as syntax.

Requirements: Names matter in the presentation Arrows matter (categorical thinking) Independent of the underlying logic and type theory Coherent with semantics (aka models)

I Induces transport of conservative extensions I Induces transport of theorems

Focus on: the intensional content of Theory Presentations

Carette, O’Connor Theory Presentation Combinators 10/18 Crucial Observation

Observation ThyPres ' Contextop

Theory Presentation + translations Theory { U: type ; ∗ :(U,U) −> U; axiom associative ∗ : f o r a l l x , y , z :U. ( x∗y )∗ z=x ∗( y∗ z ) }

λ-calculus (or logical) context + substitutions

U : type, ∗ :(U, U) → U, assoc : ∀x, y, z : U.(x ∗y)∗z = x ∗(y ∗z)

Carette, O’Connor Theory Presentation Combinators 11/18 Basic definitions

Definition A context Γ is a of pairs of labels and types (or kinds or propositions), Γ := hx0 : σ0; ... ; xn−1 : σn−1i , such that for i < n

hx0 : σ0; ... ; xi−1 : σi−1i ` σi : Type

holds (resp. : Kind, or : Prop) n−1 m−1 Notation: Γ = hx : σi0 and ∆ = hy : τi0 . Definition C has as objects contexts Γ, and morphisms Γ → ∆ are m−1 assignments [y0 7→ t0,..., ym 7→ tm−1], abbreviated as [y 7→ t]0 where the t0,..., tm−1 are terms such that

m−2 Γ ` t0 : τ0 ... Γ ` tm−1 : τm−1 [y 7→ t]0

i all hold, where τ [y 7→ t]0 denotes substitution application.

Carette, O’Connor Theory Presentation Combinators 12/18 More definitions

Definition The category of nominal assignments, B, has the same objects as C, but only those morphisms whose terms are labels. Definition Those nominal assignments where every label occurs at most once will be called general extensions (≈ no confusion). Definition f + E Γ+ ∆+ The category of general extensions has all general extensions from B as objects, and + A B given two general extensions A :Γ → Γ and B : ∆+ → ∆, a morphism f : A → B is a commutative square from B. We will denote Γ ∆ f − this commutative square by hf +, f −i : A → B.

Carette, O’Connor Theory Presentation Combinators 13/18 id hU : Typei f

+  U : Type  u¯(u) hU : Typei U0 : Type id u∗(u) u

hU : Typei hi u

f : hU : Typei → U : Type; U0 : Type f := U 7→ U, U0 7→ U

Structure Theorem

Theorem The functor cod : E → B is a fibration.

Carette, O’Connor Theory Presentation Combinators 14/18 Structure Theorem

Theorem The functor cod : E → B is a fibration.

id hU : Typei f

+  U : Type  u¯(u) hU : Typei U0 : Type id u∗(u) u

hU : Typei hi u

f : hU : Typei → U : Type; U0 : Type f := U 7→ U, U0 7→ U

Carette, O’Connor Theory Presentation Combinators 14/18 The MathScheme Theory Presentation Language

tpc ::= extend A by {l} a, b, c ∈ labels | combine A r1, B r2 A, B, C ∈ names | A ; B l ∈ judgments∗ ∗ | A r r ∈ (ai 7→ bi ) | Empty | Theory {l} Note: completely generic over the underlying type theory.

Carette, O’Connor Theory Presentation Combinators 15/18 T1 := Theory { n : I n t e g e r } T2 := Theory { n : N a t u r a l } T3 := combine T1 [ n |−> m] , T2

T3 := Theory { m : Integer, n : Natural }

Object-level semantics

r ◦ δ B 1 π A1 − B : tpc * | | D J K A1 J K Empty B = h i J K Theory {l} B ∼ hli = r2 π ◦ δA2 δA J K J K A r B = r π · A B J K J K J K A; B B = B B A2 A J K J K δ extend A by {l} B =∼ A B hli A J K ∼ J K # B B combine A1r1, A2r2 B = D where A = A1 u A2 . J K J K J K

Carette, O’Connor Theory Presentation Combinators 16/18 T3 := Theory { m : Integer, n : Natural }

Object-level semantics

r ◦ δ B 1 π A1 − B : tpc * | | D J K A1 J K Empty B = h i J K Theory {l} B ∼ hli = r2 π ◦ δA2 δA J K J K A r B = r π · A B J K J K J K A; B B = B B A2 A J K J K δ extend A by {l} B =∼ A B hli A J K ∼ J K # B B combine A1r1, A2r2 B = D where A = A1 u A2 . J K J K J K T1 := Theory { n : I n t e g e r } T2 := Theory { n : N a t u r a l } T3 := combine T1 [ n |−> m] , T2

Carette, O’Connor Theory Presentation Combinators 16/18 Object-level semantics

r ◦ δ B 1 π A1 − B : tpc * | | D J K A1 J K Empty B = h i J K Theory {l} B ∼ hli = r2 π ◦ δA2 δA J K J K A r B = r π · A B J K J K J K A; B B = B B A2 A J K J K δ extend A by {l} B =∼ A B hli A J K ∼ J K # B B combine A1r1, A2r2 B = D where A = A1 u A2 . J K J K J K T1 := Theory { n : I n t e g e r } T2 := Theory { n : N a t u r a l } T3 := combine T1 [ n |−> m] , T2

T3 := Theory { m : Integer, n : Natural }

Carette, O’Connor Theory Presentation Combinators 16/18 Morphism-level semantics

− E : tpc * |E| J K E Empty = idh i r1 π ◦ δT1 J K ∼ D J K T1 Theory {l} E = !hli J K A r E = r π · A E J K J K J K r2 π ◦ δT2 A1 A; B E = A E ◦ B E J K J K ∼ J K J K extend A by {l} E = δA J K ∼ T2 T combine A1r1, A2r2 E = r1 π ◦ δT1 ◦ A1 E A2 J K ∼ J K J K = r2 π ◦ δT2 ◦ A2 E J K J K

Carette, O’Connor Theory Presentation Combinators 17/18 Conclusion There is a lot of structure in Mathematics, and it can be leveraged to simplify builder’s lives. can really help you Follow the math, don’t follow what you think the math should be

Future Work and Conclusion Future Work Functorial semantics – diagram-level “constructions” Definitions [done] Port library to new semantics [ongoing]

Carette, O’Connor Theory Presentation Combinators 18/18 Future Work and Conclusion Future Work Functorial semantics – diagram-level “constructions” Definitions [done] Port library to new semantics [ongoing] Conclusion There is a lot of structure in Mathematics, and it can be leveraged to simplify builder’s lives. Category theory can really help you Follow the math, don’t follow what you think the math should be

Carette, O’Connor Theory Presentation Combinators 18/18