Journal of Functional Programming, 33(e1), January 2023
Is Sized Typing for Coq Practical?
Jonathan Chan
University of British Columbia,
University of Pennsylvania
Yufeng (Michael) Li
University of Waterloo,
University of Cambridge
William J. Bowman
University of British Columbia
1. Sized Types
2. Contributions
3. Implementation
2
Termination Checking: Guardedness
Fixpoint minus n m : nat :=
match n, m with
| S n′, S m′ => minus n′ m′
| _, _ => O
end.
3
Sized Types Contributions Implementation
✅
Termination Checking: Guardedness
4
Sized Types Contributions Implementation
Fixpoint div n m : nat :=
match n with
| S n′ => S (div minus n′ m m)
| O => O
end.
❌
Fixpoint minus n m : nat :=
match n, m with
| S n′, S m′ => minus n′ m′
| _, _ => O
end.
Termination Checking: Guardedness
5
Sized Types Contributions Implementation
Fixpoint div n m : nat :=
match n with
| S n′ => S (div minus n′ m m)
| O => O
end.
❌
Fixpoint minus n m : nat :=
match n, m with
| S n′, S m′ => minus n′ m′
| _, _ => O n
end.
❌
✅
Termination Checking: Sized Typing
6
Sized Types Contributions Implementation
Γ ⊢ n : nats+1
Γ ⊢ e1
: P O
Γ ⊢ n : nats
Γ, m : nats
⊢ e2
: P (S m)
───────────── ─────────────── ─────────────────────────
Γ ⊢ O : nats+1
Γ ⊢ S n : nats+1
Γ ⊢ match n with .
| O => e1
| S m => e2
end : P n
s ⩴ v | s+1 | ∞
Termination Checking: Sized Typing
7
Sized Types Contributions Implementation
Γ ⊢ n : nats+1
Γ ⊢ e1
: P O
Γ ⊢ n : nats
Γ, m : nats
⊢ e2
: P (S m)
───────────── ─────────────── ─────────────────────────
Γ ⊢ O : nats+1
Γ ⊢ S n : nats+1
Γ ⊢ match n with .
| O => e1
| S m => e2
end : P n
Termination Checking: Sized Typing
8
Sized Types Contributions Implementation
Fixpoint minus : natv
-> nat -> natv
.
Fixpoint div (n : natv+1
) (m : nat) : natv+1
:=
match n with
| S n′ => S (div (minus n′ m) m)
| O => O
end.
natv
natv
Past Work
• dependent types
• size inference algorithm
• nested (co)inductives
• universes
9
Sized Types Contributions Implementation
Hughes & Pareto & Sabry 1996
𝒞𝒞ℛ (Giménez 1998)
Amadio & Coupet-Grimal 1998
λ^ (Barthe et al. 2002; Frade 2004)
Λμ+
(Abel 2004)
λfixμν
(Abel 2003)
F^ (Barthe et al. 2005)
F^ω (Abel 2006)
F^× (Barthe et al. 2008)
MiniAgda (Abel 2010, 2012)
CIC^ (Barthe et al. 2006)
CIC^- (Grégoire & Sacchini 2010;
Sacchini 2011)
CCω
̂ (Sacchini 2013)
CIC^l (Sacchini 2014)
CIC^⊑ (Sacchini 2015)
Fcop
ω (Abel & Pientka 2016)
Abel 2017
─ ─ ─
Contributions
• dependent types
• size inference algorithm
• nested (co)inductives
• universes
10
Sized Types Contributions Implementation
CIC^ (Barthe et al. 2006)
CIC^- (Grégoire & Sacchini 2010;
Sacchini 2011)
CCω
̂ (Sacchini 2013)
CIC^*
(2019 – 2023)
─ ─ ─
Contributions
• dependent types
• annotation inference alg.
• nested (co)inductives
• universes
11
Sized Types Contributions Implementation
CIC^ (Barthe et al. 2006)
CIC^- (Grégoire & Sacchini 2010;
Sacchini 2011)
CCω
̂ (Sacchini 2013)
CIC^*
(2019 – 2023)
─ ─ ─
Contributions
• dependent types
• annotation inference alg.
• nested (co)inductives
• universes
12
Sized Types Contributions Implementation
CIC^ (Barthe et al. 2006)
CIC^- (Grégoire & Sacchini 2010;
Sacchini 2011)
CCω
̂ (Sacchini 2013)
CIC^*
(2019 – 2023)
─ ─ ─
Contributions
• dependent types
• annotation inference alg.
• nested (co)inductives
• cumulative universes
• global + local definitions
13
Sized Types Contributions Implementation
CIC^ (Barthe et al. 2006)
CIC^- (Grégoire & Sacchini 2010;
Sacchini 2011)
CCω
̂ (Sacchini 2013)
CIC^*
(2019 – 2023)
─ ─ ─
Contributions
• implementation in Coq fork
• dependent types
• annotation inference alg.
• nested (co)inductives
• cumulative universes
• global + local definitions
14
Sized Types Contributions Implementation
CIC^ (Barthe et al. 2006)
CIC^- (Grégoire & Sacchini 2010;
Sacchini 2011)
CCω
̂ (Sacchini 2013)
CIC^*
(2019 – 2023)
─ ─ ─
Goal: backward-compatible sized typing for Coq for
expressive, modular, efficient termination checking
15
Sized Types Contributions Implementation
Goal: backward-compatible sized typing for Coq for
expressive, modular, efficient termination checking
16
Sized Types Contributions Implementation
NOPE
17
Sized Types Contributions Implementation
Sized Typing Pipeline
CIC CIC^* + 𝒞 CIC^*
solve &
substitute
type checking
size inference
Coq kernel
18
Sized Types Contributions Implementation
Sized Typing Pipeline
Coq kernel
𝒞; Γ ⊢ e : τ ⇝ 𝒞′; e′ : τ′ ⇝ e″ : τ″
19
Sized Types Contributions Implementation
Sized Typing Pipeline
Coq kernel
𝒞; Γ ⊢ e : τ ⇝ 𝒞′; e′ : τ′ ⇝ e″ : τ″
20
Sized Types Contributions Implementation
Sized Typing Pipeline
Definition mynat [u] : Set ≔ natu
.
Fixpoint div [v, w, …] (n : mynatv
) (m : mynatw
) : mynatv
≔ …
Coq kernel
Size Inference: Fixpoints
𝒞; Γ, f : τv
⊢ … ⇝ 𝒞′; e : τv+1
SAT(𝒞′ ∪ {v ≠ ∞})
───────────────────────────────────────────────────
𝒞; Γ ⊢ … ⇝ 𝒞′; fix f : τ*
≔ e : τv
21
Sized Types Contributions
𝒞; Γ ⊢ e : τ ⇝ 𝒞′; e′ : τ′
Inference Substitution Performance
Size Inference: Fixpoints
𝒞; Γ, f : τv
⊢ … ⇝ 𝒞′; e : τv+1
SAT(𝒞′ ∪ {v ≠ ∞})
───────────────────────────────────────────────────
𝒞; Γ ⊢ … ⇝ 𝒞′; fix f : τ*
≔ e : τv
22
Sized Types Contributions
𝒞; Γ ⊢ e : τ ⇝ 𝒞′; e′ : τ′
Inference Substitution Performance
• nat*
→ nat → nat
• nat → nat*
→ nat
Size Inference: Fixpoints
𝒞; Γ, f : τv
⊢ … ⇝ 𝒞′; e : τv+1
SAT(𝒞′ ∪ {v ≠ ∞})
───────────────────────────────────────────────────
𝒞; Γ ⊢ … ⇝ 𝒞′; fix f : τ*
≔ e : τv
23
Sized Types Contributions
𝒞; Γ ⊢ e : τ ⇝ 𝒞′; e′ : τ′
Inference Substitution Performance
• nat*
→ nat → nat*
• nat*
→ nat → nat
• nat → nat*
→ nat*
• nat → nat*
→ nat
Size Inference: Cumulativity
𝒞; Γ ⊢ … ⇝ 𝒞′; e : τ′ τ′ ≼ τ ⇝ 𝒞″
─────────────────────────────────────
𝒞; Γ ⊢ … ⇝ 𝒞′ ∪ 𝒞″; e : τ
24
Sized Types Contributions
𝒞; Γ ⊢ e : τ ⇝ 𝒞′; e′ : τ′
Inference Substitution Performance
Size Inference: Constraints
nats₁
≼ nats₂
⇝ {s₁ ⊑ s₂}
conats₁
≼ conats₂
⇝ {s₂ ⊑ s₁}
25
Sized Types Contributions
τ1
≼ τ2
⇝ 𝒞
𝒞 ⩴ {v1
+ n1
⊑ v2
+ n2
, …}
Inference Substitution Performance
Size Inference: Constraint Satisfiability
nats₁
≼ nats₂
⇝ {s₁ ⊑ s₂}
conats₁
≼ conats₂
⇝ {s₂ ⊑ s₁}
─────── ──────
s ⊑ s+1 s ⊑ ∞
26
Sized Types Contributions
τ1
≼ τ2
⇝ 𝒞
Inference Substitution Performance
🆕 Constraint Solving
ρ = SAT(𝒞)
───────────────────
𝒞; e : τ ⇝ ρe : ρτ
27
Sized Types Contributions
𝒞′; e′ : τ′ ⇝ e″ : τ″
ρ ⩴ [v1
↦ s1
, …]
Inference Substitution Performance
🆕 Constraint Solving
ρ = SAT(𝒞)
───────────────────
𝒞; e : τ ⇝ ρe : ρτ
28
Sized Types Contributions
negative cycle
detection
e.g. Bellman–Ford
O(|𝒱||𝒞|)
𝒞′; e′ : τ′ ⇝ e″ : τ″
Inference Substitution Performance
Artificial Example: Exponentially Many `nat`s
Time Definition nats1 :=
(nat, nat, nat, nat, nat, nat, nat, nat).
Time Definition nats2 :=
(nats1, nats1, nats1, nats1).
...
Time Definition nats6 :=
(nats5, nats5, nats5, nats5).
29
Sized Types Contributions Inference Substitution Performance
Artificial Example: Exponentially Many `nat`s
Definition nats1 [v1, ..., v21] :
Set *v1
Set *v2
Set *v3
Set *v4
Set *v5
Set *v6
Set *v7
Set ≔
(natv8
, natv9
, natv10
, natv11
, natv12
, natv13
, natv14
, natv15
)v16,v17,v18,v19,v20,v21
.
Definition nats2 [...] ≔ (nats1v1,…,v21
, nats1v22,…,v42
, nats1…
, nats1…
).
...
𝒞nats1
= {v16 ⊑ v1, v17 ⊑ v2, …}
30
Sized Types Contributions Inference Substitution Performance
Artificial Example: Exponentially Many `nat`s
Definition nats1 [v1, ..., v21] :
Set *v1
Set *v2
Set *v3
Set *v4
Set *v5
Set *v6
Set *v7
Set ≔
(natv8
, natv9
, natv10
, natv11
, natv12
, natv13
, natv14
, natv15
)v16,v17,v18,v19,v20,v21
.
Definition nats2 [...] ≔ (nats1v1,…,v21
, nats1v22,…,v42
, nats1…
, nats1…
).
...
𝒞nats1
= {v16 ⊑ v1, v17 ⊑ v2, …}
31
Sized Types Contributions Inference Substitution Performance
Artificial Example: Exponentially Many `nat`s
Time Definition nats1 :=
(nat, nat, nat, nat,
nat, nat, nat, nat).
Time Definition nats2 :=
(nats1, nats1, nats1, nats1).
...
Time Definition nats6 :=
(nats5, nats5, nats5, nats5).
32
Sized Types Contributions
Definition |𝒱| Time (s)
nats1 21 0.004
nats2 93 0.020
nats3 381 0.177
nats4 1533 2.299
nats5 6141 35.385
nats6 24573 > 120
Inference Substitution Performance
Real Example: MSets/MSetList.v
Mean over five trials
33
Sized Types Contributions
Unsized compilation (s) 15.122 ± 0.073
Sized compilation (s) 83.660 ± 0.286
Slowdown 5.5×
SAT ops only (s) 64.600 ± 0.437
SAT ops only (%) 77.2%
Inference Substitution Performance
Real Example: MSets/MSetList.v
34
Sized Types Contributions
log(count)
log distribution of |𝒱| × |𝒞| during SAT operations
|𝒱| × |𝒞|
Inference Substitution Performance
~4K vs. × ~250 cs.
size inference
35
backward-compatible sized typing
definitions
constraint checking/solving
poor performance
Sized Types Contributions Implementation
Future Work?: Manual Size Annotation + Size Inference
36
Fixpoint minus [v] : natv
→ nat∞
→ natv
.
Fixpoint div [v] (n : natv
) (m : nat∞
) : natv
≔
match n with
| S n′ ⇒ S (div [?w] (minus [?u] n′ m) m)
| O ⇒ O
End.
𝒞div
= {?u = ?w, ?w+1 ⊑ v}
Sized Types Contributions Implementation
Thank you!
● Full spec of size
inference algorithm
& soundness wrt typing
● Metatheoretical results
& attempt at
set-theoretic model
● More detailed
performance profiling
& statistics
+ Closed draft PR:
https://github.com/coq/
coq/pull/12426
37
Check out the paper for:
38
bonus content
Concrete sized naturals
O : natv+1
S O : natv+2
S (S O) : natv+3
O : natv+2
S O : natv+3
S (S O) : natv+4
39
Real Example: setoid_ring/Field_theory.v
40
Mean over two trials (August 2023)
Unsized compilation (s) 17.815 ± 0.545
Sized compilation (s) 106.87 ± 1.94
Slowdown 6.0×
SAT ops only (s) 84.70
SAT ops only (%) 79.3% 17755 vs. × 14057 cs. ≈ 250M
Artificial Example: Universe Polymorphism
Set Printing Universes.
Set Universe Polymorphism.
Time Definition T1 : Type := Type -> Type
-> Type -> Type -> Type -> Type. Print T1.
Time Definition T2 : Type :=
T1 -> T1 -> T1 -> T1 -> T1 -> T1. Print T2.
Time Definition T3 : Type :=
T2 -> T2 -> T2 -> T2 -> T2 -> T2. Print T3.
Time Definition T4 : Type :=
T3 -> T3 -> T3 -> T3 -> T3 -> T3. Print T4.
Time Definition T5 : Type :=
T4 -> T4 -> T4 -> T4 -> T4 -> T4. Print T5.
Time Definition T6 : Type :=
T5 -> T5 -> T5 -> T5 -> T5 -> T5. Print T6.
Time Definition T7 : Type :=
T6 -> T6 -> T6 -> T6 -> T6 -> T6. Print T7.
41
Definition #u Time (s)
T1 7 ~ 0
T2 43 0.002
T3 259 0.026
T4 1555 0.057
T5 9331 0.374
T6 55987 3.300
T7 335921 18.170

More Related Content

PDF
Formal methods 4 - Z notation
PDF
Expressiveness and Model of the Polymorphic λ Calculus
PDF
Trends In Functional Programming Henrik Nilsson
PPTX
20100522 software verification_sharygina_lecture02
PDF
Kolmogorov complexity and topological arguments
PDF
hw-sol.pdf
PDF
types, types, types
PDF
The Fuss about || Haskell | Scala | F# ||
Formal methods 4 - Z notation
Expressiveness and Model of the Polymorphic λ Calculus
Trends In Functional Programming Henrik Nilsson
20100522 software verification_sharygina_lecture02
Kolmogorov complexity and topological arguments
hw-sol.pdf
types, types, types
The Fuss about || Haskell | Scala | F# ||

Similar to Is Sized Typing for Coq Practical? (20)

PDF
State Space C-Reductions @ ETAPS Workshop GRAPHITE 2013
KEY
Pontificating quantification
PDF
Trends in Functional Programming Meng Wang
DOCX
Low power cost rns comparison via partitioning the dynamic range
PPT
Csr2011 june17 14_00_bulatov
PDF
Genetic programming
PDF
constructing_generic_algorithms__ben_deane__cppcon_2020.pdf
PDF
C++20 新功能:Concepts & Ranges
PDF
P versus NP
PDF
Scala Functional Patterns
PDF
Compiler Construction | Lecture 7 | Type Checking
DOCX
Divide-and-Conquer & Dynamic ProgrammingDivide-and-Conqu.docx
PPTX
CS2303-TOC.pptx
PDF
[GRCPP] Introduction to concepts (C++20)
PDF
Proofsummit2011a
PDF
Programming fundamentals using c++ question paper 2014 tutorialsduniya
PDF
Study of the Subtyping Machine of Nominal Subtyping with Variance
PDF
09 - 27 Jan - Recursion Part 1
PDF
downey08semaphores.pdf
PDF
downey08semaphores.pdf
State Space C-Reductions @ ETAPS Workshop GRAPHITE 2013
Pontificating quantification
Trends in Functional Programming Meng Wang
Low power cost rns comparison via partitioning the dynamic range
Csr2011 june17 14_00_bulatov
Genetic programming
constructing_generic_algorithms__ben_deane__cppcon_2020.pdf
C++20 新功能:Concepts & Ranges
P versus NP
Scala Functional Patterns
Compiler Construction | Lecture 7 | Type Checking
Divide-and-Conquer & Dynamic ProgrammingDivide-and-Conqu.docx
CS2303-TOC.pptx
[GRCPP] Introduction to concepts (C++20)
Proofsummit2011a
Programming fundamentals using c++ question paper 2014 tutorialsduniya
Study of the Subtyping Machine of Nominal Subtyping with Variance
09 - 27 Jan - Recursion Part 1
downey08semaphores.pdf
downey08semaphores.pdf

Recently uploaded (20)

PPTX
Preformulation.pptx Preformulation studies-Including all parameter
PPTX
2currentelectricity1-201006102815 (1).pptx
PDF
Worlds Next Door: A Candidate Giant Planet Imaged in the Habitable Zone of ↵ ...
PDF
Worlds Next Door: A Candidate Giant Planet Imaged in the Habitable Zone of ↵ ...
PDF
From Molecular Interactions to Solubility in Deep Eutectic Solvents: Explorin...
PPTX
LIPID & AMINO ACID METABOLISM UNIT-III, B PHARM II SEMESTER
PPTX
congenital heart diseases of burao university.pptx
PPTX
perinatal infections 2-171220190027.pptx
PPT
Animal tissues, epithelial, muscle, connective, nervous tissue
PPTX
Substance Disorders- part different drugs change body
PPT
Mutation in dna of bacteria and repairss
PPTX
Introduction to Immunology (Unit-1).pptx
PPT
Enhancing Laboratory Quality Through ISO 15189 Compliance
PDF
Social preventive and pharmacy. Pdf
PDF
7.Physics_8_WBS_Electricity.pdfXFGXFDHFHG
PPTX
TORCH INFECTIONS in pregnancy with toxoplasma
PPT
Biochemestry- PPT ON Protein,Nitrogenous constituents of Urine, Blood, their ...
PDF
Unit 5 Preparations, Reactions, Properties and Isomersim of Organic Compounds...
PPTX
Understanding the Circulatory System……..
PPTX
HAEMATOLOGICAL DISEASES lack of red blood cells, which carry oxygen throughou...
Preformulation.pptx Preformulation studies-Including all parameter
2currentelectricity1-201006102815 (1).pptx
Worlds Next Door: A Candidate Giant Planet Imaged in the Habitable Zone of ↵ ...
Worlds Next Door: A Candidate Giant Planet Imaged in the Habitable Zone of ↵ ...
From Molecular Interactions to Solubility in Deep Eutectic Solvents: Explorin...
LIPID & AMINO ACID METABOLISM UNIT-III, B PHARM II SEMESTER
congenital heart diseases of burao university.pptx
perinatal infections 2-171220190027.pptx
Animal tissues, epithelial, muscle, connective, nervous tissue
Substance Disorders- part different drugs change body
Mutation in dna of bacteria and repairss
Introduction to Immunology (Unit-1).pptx
Enhancing Laboratory Quality Through ISO 15189 Compliance
Social preventive and pharmacy. Pdf
7.Physics_8_WBS_Electricity.pdfXFGXFDHFHG
TORCH INFECTIONS in pregnancy with toxoplasma
Biochemestry- PPT ON Protein,Nitrogenous constituents of Urine, Blood, their ...
Unit 5 Preparations, Reactions, Properties and Isomersim of Organic Compounds...
Understanding the Circulatory System……..
HAEMATOLOGICAL DISEASES lack of red blood cells, which carry oxygen throughou...

Is Sized Typing for Coq Practical?

  • 1. Journal of Functional Programming, 33(e1), January 2023 Is Sized Typing for Coq Practical? Jonathan Chan University of British Columbia, University of Pennsylvania Yufeng (Michael) Li University of Waterloo, University of Cambridge William J. Bowman University of British Columbia
  • 2. 1. Sized Types 2. Contributions 3. Implementation 2
  • 3. Termination Checking: Guardedness Fixpoint minus n m : nat := match n, m with | S n′, S m′ => minus n′ m′ | _, _ => O end. 3 Sized Types Contributions Implementation ✅
  • 4. Termination Checking: Guardedness 4 Sized Types Contributions Implementation Fixpoint div n m : nat := match n with | S n′ => S (div minus n′ m m) | O => O end. ❌ Fixpoint minus n m : nat := match n, m with | S n′, S m′ => minus n′ m′ | _, _ => O end.
  • 5. Termination Checking: Guardedness 5 Sized Types Contributions Implementation Fixpoint div n m : nat := match n with | S n′ => S (div minus n′ m m) | O => O end. ❌ Fixpoint minus n m : nat := match n, m with | S n′, S m′ => minus n′ m′ | _, _ => O n end. ❌ ✅
  • 6. Termination Checking: Sized Typing 6 Sized Types Contributions Implementation Γ ⊢ n : nats+1 Γ ⊢ e1 : P O Γ ⊢ n : nats Γ, m : nats ⊢ e2 : P (S m) ───────────── ─────────────── ───────────────────────── Γ ⊢ O : nats+1 Γ ⊢ S n : nats+1 Γ ⊢ match n with . | O => e1 | S m => e2 end : P n s ⩴ v | s+1 | ∞
  • 7. Termination Checking: Sized Typing 7 Sized Types Contributions Implementation Γ ⊢ n : nats+1 Γ ⊢ e1 : P O Γ ⊢ n : nats Γ, m : nats ⊢ e2 : P (S m) ───────────── ─────────────── ───────────────────────── Γ ⊢ O : nats+1 Γ ⊢ S n : nats+1 Γ ⊢ match n with . | O => e1 | S m => e2 end : P n
  • 8. Termination Checking: Sized Typing 8 Sized Types Contributions Implementation Fixpoint minus : natv -> nat -> natv . Fixpoint div (n : natv+1 ) (m : nat) : natv+1 := match n with | S n′ => S (div (minus n′ m) m) | O => O end. natv natv
  • 9. Past Work • dependent types • size inference algorithm • nested (co)inductives • universes 9 Sized Types Contributions Implementation Hughes & Pareto & Sabry 1996 𝒞𝒞ℛ (Giménez 1998) Amadio & Coupet-Grimal 1998 λ^ (Barthe et al. 2002; Frade 2004) Λμ+ (Abel 2004) λfixμν (Abel 2003) F^ (Barthe et al. 2005) F^ω (Abel 2006) F^× (Barthe et al. 2008) MiniAgda (Abel 2010, 2012) CIC^ (Barthe et al. 2006) CIC^- (Grégoire & Sacchini 2010; Sacchini 2011) CCω ̂ (Sacchini 2013) CIC^l (Sacchini 2014) CIC^⊑ (Sacchini 2015) Fcop ω (Abel & Pientka 2016) Abel 2017 ─ ─ ─
  • 10. Contributions • dependent types • size inference algorithm • nested (co)inductives • universes 10 Sized Types Contributions Implementation CIC^ (Barthe et al. 2006) CIC^- (Grégoire & Sacchini 2010; Sacchini 2011) CCω ̂ (Sacchini 2013) CIC^* (2019 – 2023) ─ ─ ─
  • 11. Contributions • dependent types • annotation inference alg. • nested (co)inductives • universes 11 Sized Types Contributions Implementation CIC^ (Barthe et al. 2006) CIC^- (Grégoire & Sacchini 2010; Sacchini 2011) CCω ̂ (Sacchini 2013) CIC^* (2019 – 2023) ─ ─ ─
  • 12. Contributions • dependent types • annotation inference alg. • nested (co)inductives • universes 12 Sized Types Contributions Implementation CIC^ (Barthe et al. 2006) CIC^- (Grégoire & Sacchini 2010; Sacchini 2011) CCω ̂ (Sacchini 2013) CIC^* (2019 – 2023) ─ ─ ─
  • 13. Contributions • dependent types • annotation inference alg. • nested (co)inductives • cumulative universes • global + local definitions 13 Sized Types Contributions Implementation CIC^ (Barthe et al. 2006) CIC^- (Grégoire & Sacchini 2010; Sacchini 2011) CCω ̂ (Sacchini 2013) CIC^* (2019 – 2023) ─ ─ ─
  • 14. Contributions • implementation in Coq fork • dependent types • annotation inference alg. • nested (co)inductives • cumulative universes • global + local definitions 14 Sized Types Contributions Implementation CIC^ (Barthe et al. 2006) CIC^- (Grégoire & Sacchini 2010; Sacchini 2011) CCω ̂ (Sacchini 2013) CIC^* (2019 – 2023) ─ ─ ─
  • 15. Goal: backward-compatible sized typing for Coq for expressive, modular, efficient termination checking 15 Sized Types Contributions Implementation
  • 16. Goal: backward-compatible sized typing for Coq for expressive, modular, efficient termination checking 16 Sized Types Contributions Implementation NOPE
  • 17. 17 Sized Types Contributions Implementation Sized Typing Pipeline CIC CIC^* + 𝒞 CIC^* solve & substitute type checking size inference Coq kernel
  • 18. 18 Sized Types Contributions Implementation Sized Typing Pipeline Coq kernel 𝒞; Γ ⊢ e : τ ⇝ 𝒞′; e′ : τ′ ⇝ e″ : τ″
  • 19. 19 Sized Types Contributions Implementation Sized Typing Pipeline Coq kernel 𝒞; Γ ⊢ e : τ ⇝ 𝒞′; e′ : τ′ ⇝ e″ : τ″
  • 20. 20 Sized Types Contributions Implementation Sized Typing Pipeline Definition mynat [u] : Set ≔ natu . Fixpoint div [v, w, …] (n : mynatv ) (m : mynatw ) : mynatv ≔ … Coq kernel
  • 21. Size Inference: Fixpoints 𝒞; Γ, f : τv ⊢ … ⇝ 𝒞′; e : τv+1 SAT(𝒞′ ∪ {v ≠ ∞}) ─────────────────────────────────────────────────── 𝒞; Γ ⊢ … ⇝ 𝒞′; fix f : τ* ≔ e : τv 21 Sized Types Contributions 𝒞; Γ ⊢ e : τ ⇝ 𝒞′; e′ : τ′ Inference Substitution Performance
  • 22. Size Inference: Fixpoints 𝒞; Γ, f : τv ⊢ … ⇝ 𝒞′; e : τv+1 SAT(𝒞′ ∪ {v ≠ ∞}) ─────────────────────────────────────────────────── 𝒞; Γ ⊢ … ⇝ 𝒞′; fix f : τ* ≔ e : τv 22 Sized Types Contributions 𝒞; Γ ⊢ e : τ ⇝ 𝒞′; e′ : τ′ Inference Substitution Performance • nat* → nat → nat • nat → nat* → nat
  • 23. Size Inference: Fixpoints 𝒞; Γ, f : τv ⊢ … ⇝ 𝒞′; e : τv+1 SAT(𝒞′ ∪ {v ≠ ∞}) ─────────────────────────────────────────────────── 𝒞; Γ ⊢ … ⇝ 𝒞′; fix f : τ* ≔ e : τv 23 Sized Types Contributions 𝒞; Γ ⊢ e : τ ⇝ 𝒞′; e′ : τ′ Inference Substitution Performance • nat* → nat → nat* • nat* → nat → nat • nat → nat* → nat* • nat → nat* → nat
  • 24. Size Inference: Cumulativity 𝒞; Γ ⊢ … ⇝ 𝒞′; e : τ′ τ′ ≼ τ ⇝ 𝒞″ ───────────────────────────────────── 𝒞; Γ ⊢ … ⇝ 𝒞′ ∪ 𝒞″; e : τ 24 Sized Types Contributions 𝒞; Γ ⊢ e : τ ⇝ 𝒞′; e′ : τ′ Inference Substitution Performance
  • 25. Size Inference: Constraints nats₁ ≼ nats₂ ⇝ {s₁ ⊑ s₂} conats₁ ≼ conats₂ ⇝ {s₂ ⊑ s₁} 25 Sized Types Contributions τ1 ≼ τ2 ⇝ 𝒞 𝒞 ⩴ {v1 + n1 ⊑ v2 + n2 , …} Inference Substitution Performance
  • 26. Size Inference: Constraint Satisfiability nats₁ ≼ nats₂ ⇝ {s₁ ⊑ s₂} conats₁ ≼ conats₂ ⇝ {s₂ ⊑ s₁} ─────── ────── s ⊑ s+1 s ⊑ ∞ 26 Sized Types Contributions τ1 ≼ τ2 ⇝ 𝒞 Inference Substitution Performance
  • 27. 🆕 Constraint Solving ρ = SAT(𝒞) ─────────────────── 𝒞; e : τ ⇝ ρe : ρτ 27 Sized Types Contributions 𝒞′; e′ : τ′ ⇝ e″ : τ″ ρ ⩴ [v1 ↦ s1 , …] Inference Substitution Performance
  • 28. 🆕 Constraint Solving ρ = SAT(𝒞) ─────────────────── 𝒞; e : τ ⇝ ρe : ρτ 28 Sized Types Contributions negative cycle detection e.g. Bellman–Ford O(|𝒱||𝒞|) 𝒞′; e′ : τ′ ⇝ e″ : τ″ Inference Substitution Performance
  • 29. Artificial Example: Exponentially Many `nat`s Time Definition nats1 := (nat, nat, nat, nat, nat, nat, nat, nat). Time Definition nats2 := (nats1, nats1, nats1, nats1). ... Time Definition nats6 := (nats5, nats5, nats5, nats5). 29 Sized Types Contributions Inference Substitution Performance
  • 30. Artificial Example: Exponentially Many `nat`s Definition nats1 [v1, ..., v21] : Set *v1 Set *v2 Set *v3 Set *v4 Set *v5 Set *v6 Set *v7 Set ≔ (natv8 , natv9 , natv10 , natv11 , natv12 , natv13 , natv14 , natv15 )v16,v17,v18,v19,v20,v21 . Definition nats2 [...] ≔ (nats1v1,…,v21 , nats1v22,…,v42 , nats1… , nats1… ). ... 𝒞nats1 = {v16 ⊑ v1, v17 ⊑ v2, …} 30 Sized Types Contributions Inference Substitution Performance
  • 31. Artificial Example: Exponentially Many `nat`s Definition nats1 [v1, ..., v21] : Set *v1 Set *v2 Set *v3 Set *v4 Set *v5 Set *v6 Set *v7 Set ≔ (natv8 , natv9 , natv10 , natv11 , natv12 , natv13 , natv14 , natv15 )v16,v17,v18,v19,v20,v21 . Definition nats2 [...] ≔ (nats1v1,…,v21 , nats1v22,…,v42 , nats1… , nats1… ). ... 𝒞nats1 = {v16 ⊑ v1, v17 ⊑ v2, …} 31 Sized Types Contributions Inference Substitution Performance
  • 32. Artificial Example: Exponentially Many `nat`s Time Definition nats1 := (nat, nat, nat, nat, nat, nat, nat, nat). Time Definition nats2 := (nats1, nats1, nats1, nats1). ... Time Definition nats6 := (nats5, nats5, nats5, nats5). 32 Sized Types Contributions Definition |𝒱| Time (s) nats1 21 0.004 nats2 93 0.020 nats3 381 0.177 nats4 1533 2.299 nats5 6141 35.385 nats6 24573 > 120 Inference Substitution Performance
  • 33. Real Example: MSets/MSetList.v Mean over five trials 33 Sized Types Contributions Unsized compilation (s) 15.122 ± 0.073 Sized compilation (s) 83.660 ± 0.286 Slowdown 5.5× SAT ops only (s) 64.600 ± 0.437 SAT ops only (%) 77.2% Inference Substitution Performance
  • 34. Real Example: MSets/MSetList.v 34 Sized Types Contributions log(count) log distribution of |𝒱| × |𝒞| during SAT operations |𝒱| × |𝒞| Inference Substitution Performance ~4K vs. × ~250 cs.
  • 35. size inference 35 backward-compatible sized typing definitions constraint checking/solving poor performance Sized Types Contributions Implementation
  • 36. Future Work?: Manual Size Annotation + Size Inference 36 Fixpoint minus [v] : natv → nat∞ → natv . Fixpoint div [v] (n : natv ) (m : nat∞ ) : natv ≔ match n with | S n′ ⇒ S (div [?w] (minus [?u] n′ m) m) | O ⇒ O End. 𝒞div = {?u = ?w, ?w+1 ⊑ v} Sized Types Contributions Implementation
  • 37. Thank you! ● Full spec of size inference algorithm & soundness wrt typing ● Metatheoretical results & attempt at set-theoretic model ● More detailed performance profiling & statistics + Closed draft PR: https://github.com/coq/ coq/pull/12426 37 Check out the paper for:
  • 39. Concrete sized naturals O : natv+1 S O : natv+2 S (S O) : natv+3 O : natv+2 S O : natv+3 S (S O) : natv+4 39
  • 40. Real Example: setoid_ring/Field_theory.v 40 Mean over two trials (August 2023) Unsized compilation (s) 17.815 ± 0.545 Sized compilation (s) 106.87 ± 1.94 Slowdown 6.0× SAT ops only (s) 84.70 SAT ops only (%) 79.3% 17755 vs. × 14057 cs. ≈ 250M
  • 41. Artificial Example: Universe Polymorphism Set Printing Universes. Set Universe Polymorphism. Time Definition T1 : Type := Type -> Type -> Type -> Type -> Type -> Type. Print T1. Time Definition T2 : Type := T1 -> T1 -> T1 -> T1 -> T1 -> T1. Print T2. Time Definition T3 : Type := T2 -> T2 -> T2 -> T2 -> T2 -> T2. Print T3. Time Definition T4 : Type := T3 -> T3 -> T3 -> T3 -> T3 -> T3. Print T4. Time Definition T5 : Type := T4 -> T4 -> T4 -> T4 -> T4 -> T4. Print T5. Time Definition T6 : Type := T5 -> T5 -> T5 -> T5 -> T5 -> T5. Print T6. Time Definition T7 : Type := T6 -> T6 -> T6 -> T6 -> T6 -> T6. Print T7. 41 Definition #u Time (s) T1 7 ~ 0 T2 43 0.002 T3 259 0.026 T4 1555 0.057 T5 9331 0.374 T6 55987 3.300 T7 335921 18.170