Algebraic simple type theory A polynomial approach Nathanael Arkor - - PowerPoint PPT Presentation

algebraic simple type theory
SMART_READER_LITE
LIVE PREVIEW

Algebraic simple type theory A polynomial approach Nathanael Arkor - - PowerPoint PPT Presentation

Algebraic simple type theory A polynomial approach Nathanael Arkor & Marcelo Fiore Department of Computer Science and Technology University of Cambridge Category Theory 2019 What is a type theory? What is a type theory? system of


slide-1
SLIDE 1

Algebraic simple type theory

A polynomial approach

Nathanael Arkor & Marcelo Fiore

Department of Computer Science and Technology University of Cambridge Category Theory 2019

slide-2
SLIDE 2

What is a type theory?

slide-3
SLIDE 3

What is a type theory?

system of mathematical proof

slide-4
SLIDE 4

What is a type theory?

system of mathematical proof specification for a programming language

slide-5
SLIDE 5

What is a type theory?

system of mathematical proof internal language of a category specification for a programming language

slide-6
SLIDE 6

Algebraic type theory

categorical algebra typing / sorting binding polymorphism dependency

slide-7
SLIDE 7

Algebraic simple type theory

categorical algebra typing / sorting binding polymorphism dependency e.g. λ-calculus, computational λ-calculus, predicate logic

slide-8
SLIDE 8

x1 : τ1, …, xn : τn ⊢ t : τ

Examples

Monoid action x : M, a : A ⊢ x • a : A Integration f : R → R ⊢ ∫x f(x) dx : R

Typing judgements

slide-9
SLIDE 9

contexts terms and types

x1 : τ1, …, xn : τn ⊢ t : τ

slide-10
SLIDE 10

contexts terms and types

x1 : τ1, …, xn : τn ⊢ t : τ

slide-11
SLIDE 11

Cartesian context structures

S ∈ Set

the sorts, or types

𝒟 ∈ Cat

the variable contexts

ε (terminal) ∈ 𝒟

the empty context

⟨–⟩ : S → 𝒟

types as contexts

– × ⟨=⟩

: 𝒟 × S → 𝒟

context extension e.g. any cartesian category

slide-12
SLIDE 12

Cartesian context structures

S ∈ Set

the sorts, or types

𝒟 ∈ Cat

the variable contexts

ε (terminal) ∈ 𝒟

the empty context

⟨–⟩ : S → 𝒟

types as contexts

– × ⟨=⟩

: 𝒟 × S → 𝒟

context extension e.g. any cartesian category

slide-13
SLIDE 13

Cartesian context structures

S ∈ Set

the sorts, or types

𝒟 ∈ Cat

the variable contexts

ε (terminal) ∈ 𝒟

the empty context

⟨–⟩ : S → 𝒟

types as contexts

– × ⟨=⟩

: 𝒟 × S → 𝒟

context extension e.g. any cartesian category

slide-14
SLIDE 14

Cartesian context structures

S ∈ Set

the sorts, or types

𝒟 ∈ Cat

the variable contexts

ε (terminal) ∈ 𝒟

the empty context

⟨–⟩ : S → 𝒟

types as contexts

– × ⟨=⟩

: 𝒟 × S → 𝒟

context extension e.g. any cartesian category

slide-15
SLIDE 15

Cartesian context structures

S ∈ Set

the sorts, or types

𝒟 ∈ Cat

the variable contexts

ε (terminal) ∈ 𝒟

the empty context

⟨–⟩ : S → 𝒟

types as contexts

– × ⟨=⟩

: 𝒟 × S → 𝒟

context extension e.g. any cartesian category

slide-16
SLIDE 16

contexts terms and types

x1 : τ1, …, xn : τn ⊢ t : τ

slide-17
SLIDE 17

T(Γ) S(Γ)

Term-typing structure

τΓ

presheaf of terms (in context Γ)

We consider presheaves 𝒟op → Set on a cartesian context structure (𝒟, S), fibred over S.

presheaf of types (in context Γ) assignment of types to terms

slide-18
SLIDE 18

constant presheaf of types

T(Γ) S

Term-typing structure

τΓ

presheaf of terms (in context Γ)

We consider presheaves 𝒟op → Set on a cartesian context structure (𝒟, S), fibred over S.

assignment of types to terms

slide-19
SLIDE 19

Terms with a specified type

  • NB. The fibre Tσ is the set of terms with type σ.

Tσ T 1 S

σ τ

pb

slide-20
SLIDE 20

V = ⨿ y⟨σ⟩ S

Presheaf of variables

ν

For any context Γ ∈ 𝒟, V(Γ) is the set of variables in Γ.

σ∈S def

slide-21
SLIDE 21

contexts terms and types

x1 : τ1, …, xn : τn ⊢ t : τ

Models of simple type theory

slide-22
SLIDE 22

contexts terms and types

x1 : τ1, …, xn : τn ⊢ t : τ

Models of simple type theory

algebraic structure

slide-23
SLIDE 23

Models of simple type theory

Γ ⊢ op(t) : Op(τ)

term operators

τ → σ | τ × σ | T(τ) | ...

type operators

(t1, t2) | π1 t | t1 t2 | λ(x) t | ...

slide-24
SLIDE 24

Algebraic structure on types

Type structure is as in universal algebra. For instance, the following operators

τ → σ | τ × σ | T(τ) | U

induce a signature endofunctor on Set

Σty = X ↦ X2 + X2 + X + 1

the algebras for which are sets S with the appropriate structure

[⟦ →⟧, ⟦×⟧, ⟦T⟧, ⟦U⟧] : Σty S → S

(NB. These signature functors are polynomial.)

slide-25
SLIDE 25

How should we define the algebraic structure on terms? Natural deduction rules present algebraic structure Polynomials present natural deduction rules

slide-26
SLIDE 26

How should we define the algebraic structure on terms? Natural deduction rules present algebraic structure Polynomials present natural deduction rules

slide-27
SLIDE 27

How should we define the algebraic structure on terms? Natural deduction rules present algebraic structure Polynomials present natural deduction rules

slide-28
SLIDE 28

Polynomials & polynomial functors

In a locally cartesian-closed category ℰ, a polynomial is a diagram: The polynomial functor associated to the polynomial is given by:

Σh Πg f* : ℰ/A ⟶ ℰ/D A ⟵ B ⟶ C ⟶ D

f g h

slide-29
SLIDE 29

We will consider polynomials in Psh(𝒟), inducing polynomial functors Psh(𝒟)/S → Psh(𝒟)/S, where (𝒟, S) is a cartesian context structure with algebraic structure ΣtyS → S. Let P be a polynomial in Psh(𝒟). Algebras for the corresponding polynomial functor are bundles τ : T → S together with morphisms as in the following.

FP( ) ⟶ ( )

T S τ T S τ

Polynomials & polynomial functors

⟦P⟧

slide-30
SLIDE 30

Algebraic structure & natural deduction

Γ ⊢ a : A Γ ⊢ b : B Γ ⊢ pair(a, b) : Prod(A, B) Prod-ɪɴᴛʀᴏ

slide-31
SLIDE 31

S ⟵ S × S + S × S ⟶ S × S ⟶ S

Algebraic structure & natural deduction

Γ ⊢ a : A Γ ⊢ b : B Γ ⊢ pair(a, b) : Prod(A, B) Prod-ɪɴᴛʀᴏ

two type metavariables A B in Psh(𝒟)

slide-32
SLIDE 32

S ⟵ S × S + S × S ⟶ S × S ⟶ S

Algebraic structure & natural deduction

Γ ⊢ a : A Γ ⊢ b : B Γ ⊢ pair(a, b) : Prod(A, B) Prod-ɪɴᴛʀᴏ

  • utput sort

⟦Prod⟧

in Psh(𝒟)

slide-33
SLIDE 33

Algebraic structure & natural deduction

Γ ⊢ a : A Γ ⊢ b : B Γ ⊢ pair(a, b) : Prod(A, B) Prod-ɪɴᴛʀᴏ

two hypotheses Γ ⊢ a : A Γ ⊢ b : B S ⟵ S × S + S × S ⟶ S × S ⟶ S

⟦Prod⟧

in Psh(𝒟)

slide-34
SLIDE 34

Algebraic structure & natural deduction

Γ ⊢ a : A Γ ⊢ b : B Γ ⊢ pair(a, b) : Prod(A, B) Prod-ɪɴᴛʀᴏ

S ⟵ S × S + S × S ⟶ S × S ⟶ S A' B' A B

∇2 ⟦Prod⟧

in Psh(𝒟)

slide-35
SLIDE 35

Algebraic structure & natural deduction

Γ ⊢ a : A Γ ⊢ b : B Γ ⊢ pair(a, b) : Prod(A, B) Prod-ɪɴᴛʀᴏ

S ⟵ S × S + S × S ⟶ S × S ⟶ S

∇2 ⟦Prod⟧ [π1, π2]

A B in Psh(𝒟)

slide-36
SLIDE 36

Algebraic structure & natural deduction

Γ ⊢ a : A Γ ⊢ b : B Γ ⊢ pair(a, b) : Prod(A, B) Prod-ɪɴᴛʀᴏ

S ⟵ S × S + S × S ⟶ S × S ⟶ S

∇2 ⟦Prod⟧ [π1, π2]

A B in Psh(𝒟)

slide-37
SLIDE 37

Algebraic structure & natural deduction

S ⟵ S × S + S × S ⟶ S × S ⟶ S

∇2 ⟦Prod⟧ [π1, π2]

T × T T S × S S

⟦pair⟧ ⟦Prod⟧ τ × τ τ

polynomial functor algebra

Γ ⊢ a : A Γ ⊢ b : B Γ ⊢ pair(a, b) : Prod(A, B) Prod-ɪɴᴛʀᴏ

introduction rule

in Psh(𝒟)

slide-38
SLIDE 38

Binding algebraic structure & natural deduction

Γ, x : A ⊢ t : B Γ ⊢ abs(x : A . t) : Fun(A, B) Fun-ɪɴᴛʀᴏ

slide-39
SLIDE 39

Binding algebraic structure & natural deduction

Γ, x : A ⊢ t : B Γ ⊢ abs(x : A . t) : Fun(A, B) Fun-ɪɴᴛʀᴏ

S ⟵ V × S ⟶ S × S ⟶ S two type metavariables A B in Psh(𝒟)

slide-40
SLIDE 40

Binding algebraic structure & natural deduction

Γ, x : A ⊢ t : B Γ ⊢ abs(x : A . t) : Fun(A, B) Fun-ɪɴᴛʀᴏ

S ⟵ V × S ⟶ S × S ⟶ S

  • utput sort

⟦Fun⟧

in Psh(𝒟)

slide-41
SLIDE 41

Binding algebraic structure & natural deduction

Γ, x : A ⊢ t : B Γ ⊢ abs(x : A . t) : Fun(A, B) Fun-ɪɴᴛʀᴏ

S ⟵ V × S ⟶ S × S ⟶ S

  • ne variable
  • ne term

A B

⟦Fun⟧

in Psh(𝒟)

slide-42
SLIDE 42

Binding algebraic structure & natural deduction

Γ, x : A ⊢ t : B Γ ⊢ abs(x : A . t) : Fun(A, B) Fun-ɪɴᴛʀᴏ

S ⟵ V × S ⟶ S × S ⟶ S

types of variables ⟦Fun⟧ ν × id

in Psh(𝒟)

slide-43
SLIDE 43

Binding algebraic structure & natural deduction

Γ, x : A ⊢ t : B Γ ⊢ abs(x : A . t) : Fun(A, B) Fun-ɪɴᴛʀᴏ

S ⟵ V × S ⟶ S × S ⟶ S

ν × id ⟦Fun⟧ π2

A B term sort in Psh(𝒟)

slide-44
SLIDE 44

Binding algebraic structure & natural deduction

⨿ TB(Γ · A)

T(Γ) S × S S

⟦abs⟧Γ ⟦Fun⟧Γ τΓ

S ⟵ V × S ⟶ S × S ⟶ S

ν × id ⟦Fun⟧

A, B ∈ S

π

in Psh(𝒟)

π2 Γ, x : A ⊢ t : B Fun-ɪɴᴛʀᴏ Γ ⊢ abs(x : A . t) : Fun(A, B)

polynomial functor algebra introduction rule

slide-45
SLIDE 45

Algebraic structure & natural deduction

The natural deduction rules corresponding to introduction/elimination can be described by a second-order arity (describing the typing and binding data for each argument). Each second-order arity induces a polynomial in Psh(𝒟). The algebras for their associated polynomial functors are presheaves with the corresponding (typed & binding) term structure. We can collect the arities into a term signature Σtm, which itself induces a polynomial. (NB. We're using the same notation for a signature and the polynomial functor it induces.)

slide-46
SLIDE 46

contexts terms and types

x1 : τ1, …, xn : τn ⊢ t : τ

Models of simple type theory

algebraic structure

slide-47
SLIDE 47

Model homomorphisms

h : S → S'

a Σty-algebra homomorphism

H : 𝒟 → 𝒟'

a structure-preserving functor

f : T → H⭑(T')

a morphism in Psh(𝒟)/S preserving the Σtm-algebra structure (S, 𝒟, ε, ⟨–⟩, – × ⟨=⟩, T, τ, ⟦–⟧ty, ⟦–⟧tm) ⟶ (S', 𝒟', ε', ⟨–⟩', – × ⟨=⟩', T', τ', ⟦–⟧'ty, ⟦–⟧'tm)

slide-48
SLIDE 48

Model homomorphisms

h : S → S'

a Σty-algebra homomorphism

f : T → H⭑(T')

a morphism in Psh(𝒟)/S preserving the Σtm-algebra structure

ΣtyS S S'

h

ΣtyS' Σtyh ΣtmT T 𝖎(T')

f

Σtm𝖎(T') Σty f 𝖎(Σ'tmT')

where 𝖎 = h*(– H)

slide-49
SLIDE 49

Syntactic models of simple type theory

For any given term and type signature, we want a model of simple type theory freely generated by the syntax. The model freely generated by the syntax is exactly the initial model. Since we have no type dependency, we can construct the initial model piecewise.

Σty Σtm

syntactic model

slide-50
SLIDE 50

Initial models of simple type theory

slide-51
SLIDE 51

Initial models of simple type theory

  • _S_

initial Σty-algebra

  • as in universal algebra
slide-52
SLIDE 52

Initial models of simple type theory

  • _S_

initial Σty-algebra

  • as in universal algebra
  • _(𝒟, ε)_

free cartesian category on _S_

  • concretely, the opposite of the comma category
  • (𝔾 → Set) ↓ (𝟚 → Set)

S

slide-53
SLIDE 53

Initial models of simple type theory

  • _S_

initial Σty-algebra

  • as in universal algebra
  • _(𝒟, ε)_

free cartesian category on _S_

  • concretely, the opposite of the comma category
  • (𝔾 → Set) ↓ (𝟚 → Set)
  • _(T, τ)_

initial Σtm-algebra

  • using Adámek's initial algebra construction

S

slide-54
SLIDE 54

There's one last thing...

slide-55
SLIDE 55

Substitution

Γ ⊢ t [u/x] : τ

???

slide-56
SLIDE 56

Substitution

Γ ⊢ t [u/x] : τ

an algebraic operation on terms

slide-57
SLIDE 57

Substitution

Γ, x : A ⊢ t : B Γ ⊢ u : A Γ ⊢ subst(x : A . t, u) : B subst Γ, x : A ⊢ var(x) : A var

slide-58
SLIDE 58

Substitution

Γ, x : A ⊢ t : B Γ ⊢ u : A Γ ⊢ subst(x : A . t, u) : B subst Γ, x : A ⊢ var(x) : A var

S ⟵ 0 ⟶ V ⟶ S

ν

S ⟵ V × S + S × S ⟶ S × S ⟶ S

ν × id + id π2 [π2, π1]

slide-59
SLIDE 59

Substitution

Γ, x : A ⊢ t : B Γ ⊢ u : A Γ ⊢ subst(x : A . t, u) : B subst Γ, x : A ⊢ var(x) : A var subject to equational laws…

slide-60
SLIDE 60

Initial models of simple type theories with substitution

Σty Σtm

syntactic model with substitution

slide-61
SLIDE 61

Initial models of simple type theories with substitution

Σty Σtm

syntactic model with substitution

Σsubst

slide-62
SLIDE 62
  • Q. What is a simple type theory?
  • A. An initial model

(ℂ, T ⟶ S, ⟦-⟧ty, ⟦-⟧tm + subst) A partial answer

slide-63
SLIDE 63
  • Q. What is a simple type theory?
  • A. An initial model

(𝒟, T ⟶ S, ⟦-⟧ty, ⟦-⟧tm + subst)

τ

A partial answer

slide-64
SLIDE 64
  • Q. What is a simple type theory?
  • A. An initial model

(𝒟, T ⟶ S, ⟦-⟧ty, ⟦-⟧tm + subst)

τ

A partial answer

We can now construct the classifying category and equational logic...

slide-65
SLIDE 65

Conclusion

  • Models of simple type theory consist of structures for contexts, typed

terms and algebraic structure.

  • Natural deduction rules that present simple type theories can themselves

be presented by polynomials.

  • The initial model of simple type theory is the syntactic model of the type

theory, and can be constructed explicitly with a free algebra construction.

  • We can construct the syntactic model with substitution, from which we

can derive a classifying category, demonstrating that the type theory is its internal language.