Git as a HIT
Dan Licata Wesleyan University
1 1 Darcs Git as a HIT
Dan Licata Wesleyan University
1 1 HITs
2 Generator for 2 equality of equality HITs
Homotopy Type Theory is an extension of Agda/Coq based on connections with homotopy theory [Hofmann&Streicher,Awodey&Warren,Voevodsky,Lumsdaine,Garner&van den Berg]
2 Generator for 2 equality of equality HITs
Homotopy Type Theory is an extension of Agda/Coq based on connections with homotopy theory [Hofmann&Streicher,Awodey&Warren,Voevodsky,Lumsdaine,Garner&van den Berg]
Higher inductive types (HITs) are a new type former!
2 Generator for 2 equality of equality HITs
Homotopy Type Theory is an extension of Agda/Coq based on connections with homotopy theory [Hofmann&Streicher,Awodey&Warren,Voevodsky,Lumsdaine,Garner&van den Berg]
Higher inductive types (HITs) are a new type former!
They were originally invented[Lumsdaine,Shulman,…] to model basic spaces (circle, spheres, the torus, …) and constructions in homotopy theory
2 Generator for 2 equality of equality HITs
Homotopy Type Theory is an extension of Agda/Coq based on connections with homotopy theory [Hofmann&Streicher,Awodey&Warren,Voevodsky,Lumsdaine,Garner&van den Berg]
Higher inductive types (HITs) are a new type former!
They were originally invented[Lumsdaine,Shulman,…] to model basic spaces (circle, spheres, the torus, …) and constructions in homotopy theory
But they have many other applications, including some programming ones!
2 Generator for 2 equality of equality Patches Patch
a a 2c2 diff b d = < b c c --- > d
3 3 id a a b b c c
4 4 id a a b b c c
a p a q a b d d c c e
4 4 id a a b b c c
q o p
a p a q a b d d c c e
4 4 id p a a a a b b b d c c c c
q o p
a p a q a b d d c c e
4 4 id p a a a a b b b d c c c c !p
q o p
a p a q a b d d c c e
4 4 id p a a a a b b b d c c c c !p
q o p undo/rollback
a p a q a b d d c c e
4 4 id p a a a a b b b d c c c c !p
q o p undo/rollback
a p a q a b d d c c e
[Yorgey,Jacobson,…]
4 4 Simple Setup
a u s s o i s
a ↔ b at 0
a u s b u s
a ↔ b at 0
5 5 Simple Setup “Repository” is a char vector of fixed length n
a u s s o i s
Basic patch is a ↔ b at i where i a ↔ b at 0 a u s b u s a ↔ b at 0 5 5 Domain-Specific Language 6 6 Domain-Specific Language 7 7 Domain-Specific Language swapat a b i v permutes a and b at position i in v 7 7 Domain-Specific Language Spec: ∀ p. interp p is a bijection: ∀ v. g (f v) = v where (f,g)=interp p ∀ v. f (g v) = v 8 8 Domain-Specific Language undo really un-does Spec: ∀ p. interp p is a bijection: ∀ v. g (f v) = v where (f,g)=interp p ∀ v. f (g v) = v 8 8 Domain-Specific Language undo really un-does Spec: ∀ p. interp p is a bijection: ∀ v. g (f v) = v where (f,g)=interp p ∀ v. f (g v) = v Can package this as: 8 8 Merging a p b q c a a d b c e 9 9 Merging a p b q c a a d b c e q’ a p’ d e 9 9 Merging p=b↔d at 1 a q=c↔e at 2 p b q c a a d b c e q’ a p’ d e 9 9 Merging p=b↔d at 1 a q=c↔e at 2 p b q c a a d b c e q’ a p’ d p’=p e q’=q 9 9 Merging p=b↔d at 1 a q=c↔e at 2 p b q c a a d b c = e q’ a p’ d p’=p e q’=q 9 9 Merging merge : (p q : Patch) Σq’,p’:Patch. Maybe(q’ o p = p’ o q) 10 10 Merging merge : (p q : Patch) Σq’,p’:Patch. Maybe(q’ o p = p’ o q) When are two patches equal? 10 10 Patch Equality (a↔b at i)o(c↔d at j) = (c↔d at j)o(a↔b at i) if i≠j 11 11 Patch Equality (a↔b at i)o(c↔d at j) = (c↔d at j)o(a↔b at i) if i≠j (a↔a at i) = id !(a↔b at i) = (a↔b at i) (a↔b at i) = (b↔a at i) 11 11 Patch Equality Basic Axioms: (a↔b at i)o(c↔d at j) = (c↔d at j)o(a↔b at i) if i≠j (a↔a at i) = id !(a↔b at i) = (a↔b at i) (a↔b at i) = (b↔a at i) 11 11 Patch Equality Basic axioms: (a↔b at i)o(c↔d at j) =(c↔d at j)o(a↔b at i) 12 12 Patch Equality Basic axioms: Group laws: (a↔b at i)o(c↔d at j) id o p = p = p o id =(c↔d at j)o(a↔b at i) po(qor) = (poq)or !p o p = id = p o !p 12 12 Patch Equality Basic axioms: Group laws: (a↔b at i)o(c↔d at j) id o p = p = p o id =(c↔d at j)o(a↔b at i) po(qor) = (poq)or !p o p = id = p o !p Congruence: p=p !p = !p’ if p = p’ p=q if q=p p o q = p’ o q’ if p=r if p=q and q=r p = p’ and q = q’ 12 12 Patch as Quotient Type Elements: Equality: (a↔b at i)o(c↔d at j)~ (c↔d at j)o(a↔b at i) ... id o p ~ p ~ p o id po(qor) ~ (poq)or !p o p ~ id ~ p o !p p~p p~q if q~p p~r if p~q and q~r !p ~ !p’ if p ~ p’ p o q ~ p’ o q’ if p ~ p’ and q ~ q’ 13 13 Patch as Quotient Type Elements: Quotient Type: Patch := Patch’/~ Equality: (a↔b at i)o(c↔d at j)~ (c↔d at j)o(a↔b at i) ... id o p ~ p ~ p o id po(qor) ~ (poq)or !p o p ~ id ~ p o !p p~p p~q if q~p p~r if p~q and q~r !p ~ !p’ if p ~ p’ p o q ~ p’ o q’ if p ~ p’ and q ~ q’ 13 13 Patch as Quotient Type Elements: Quotient Type: Patch := Patch’/~ Equality: Elimination rule: (a↔b at i)o(c↔d at j)~ (c↔d at j)o(a↔b at i) ... id o p ~ p ~ p o id define on Patch’ as before, po(qor) ~ (poq)or then prove p ~ q implies !p o p ~ id ~ p o !p p~p interp p = interp q p~q if q~p for all 14+ rules for ~ p~r if p~q and q~r !p ~ !p’ if p ~ p’ p o q ~ p’ o q’ if p ~ p’ and q ~ q’ 13 13 Patches as a HIT 1.How do you define Patch using a higher inductive type? 2.What is the elimination rule? 3.How do you use the elim. rule to define interp? 14 14 Patches as a HIT 1.How do you define Patch using a higher inductive type? 2.What is the elimination rule? 3.How do you use the elim. rule to define interp? 15 15 Higher Inductive Type 16 Generator for 16 equality of equality Higher Inductive Type Type freely generated by constructors for elements, equalities, equalities between equalities, … 16 Generator for 16 equality of equality Higher Inductive Type Type freely generated by constructors for elements, equalities, equalities between equalities, … RepoDesc : Type 16 Generator for 16 equality of equality Higher Inductive Type Type freely generated by constructors for elements, equalities, equalities between equalities, … RepoDesc : Type vec : RepoDesc generator for element 16 Generator for 16 equality of equality Higher Inductive Type Type freely generated by constructors for elements, equalities, equalities between equalities, … RepoDesc : Type vec : RepoDesc generator for element (a↔b at i) : vec = vec generator for equality 16 Generator for 16 equality of equality Higher Inductive Type Type freely generated by constructors for elements, equalities, equalities between equalities, … RepoDesc : Type proof-relevant! vec : RepoDesc generator for element (a↔b at i) : vec = vec generator for equality 16 Generator for 16 equality of equality Higher Inductive Type Type freely generated by constructors for elements, equalities, equalities between equalities, … RepoDesc : Type proof-relevant! vec : RepoDesc generator for element (a↔b at i) : vec = vec generator for equality commute: generator for equality (a↔b at i)o(c↔d at j) between equalities =(c↔d at j)o(a↔b at i) 16 Generator for 16 equality of equality Type: Patch Elements: Equality: (a↔b at i)o(c↔d at j)= (c↔d at j)o(a↔b at i) ... id o p = p = p o id po(qor) = (poq)or !p o p = id = p o !p p=p p=q if q=p p=r if p=q and q=r !p = !p’ if p = p’ p o q = p’ o q’ if p = p’ and q = q’ 17 17 Type: Patch Type: RepoDesc Elements: Equality: (a↔b at i)o(c↔d at j)= (c↔d at j)o(a↔b at i) ... id o p = p = p o id po(qor) = (poq)or !p o p = id = p o !p p=p p=q if q=p p=r if p=q and q=r !p = !p’ if p = p’ p o q = p’ o q’ if p = p’ and q = q’ 17 17 Type: Patch Type: RepoDesc Element: vec : RepoDesc Elements: Equality: (a↔b at i)o(c↔d at j)= (c↔d at j)o(a↔b at i) ... id o p = p = p o id po(qor) = (poq)or !p o p = id = p o !p p=p p=q if q=p p=r if p=q and q=r !p = !p’ if p = p’ p o q = p’ o q’ if p = p’ and q = q’ 17 17 Type: Patch Type: RepoDesc Element: vec : RepoDesc Elements: Equality: a↔b at i : vec = vec Equality: (a↔b at i)o(c↔d at j)= (c↔d at j)o(a↔b at i) ... id o p = p = p o id po(qor) = (poq)or !p o p = id = p o !p p=p p=q if q=p p=r if p=q and q=r !p = !p’ if p = p’ p o q = p’ o q’ if p = p’ and q = q’ 17 17 Type: Patch Type: RepoDesc Element: vec : RepoDesc Elements: Equality: Patch { a↔b at i : vec = vec Equality: (a↔b at i)o(c↔d at j)= (c↔d at j)o(a↔b at i) ... id o p = p = p o id po(qor) = (poq)or !p o p = id = p o !p p=p p=q if q=p p=r if p=q and q=r !p = !p’ if p = p’ p o q = p’ o q’ if p = p’ and q = q’ 17 17 Type: Patch Type: RepoDesc Element: vec : RepoDesc Elements: Equality: Patch { a↔b at i : vec = vec Equality: Equality between equalities: (a↔b at i)o(c↔d at j)= commute : (c↔d at j)o(a↔b at i) (a↔b at i)o(c↔d at j)= ... id o p = p = p o id (c↔d at j)o(a↔b at i) po(qor) = (poq)or !p o p = id = p o !p … basic axioms only! p=p p=q if q=p p=r if p=q and q=r !p = !p’ if p = p’ p o q = p’ o q’ if p = p’ and q = q’ 17 17 Type: Patch Type: RepoDesc Element: vec : RepoDesc Elements: Equality: Patch { a↔b at i : vec = vec Equality: Equality between equalities: (a↔b at i)o(c↔d at j)= commute : (c↔d at j)o(a↔b at i) (a↔b at i)o(c↔d at j)= ... id o p = p = p o id (c↔d at j)o(a↔b at i) po(qor) = (poq)or !p o p = id = p o !p … basic axioms only! p=p p=q if q=p Everything else comes p=r if p=q and q=r “for free” from !p = !p’ if p = p’ p o q = p’ o q’ if p = p’ and q = q’ the equality type! 17 17 Typed Patches RepoDesc : Type vec : RepoDesc generators for elements compressed : RepoDesc a↔b at i : vec = vec generators for equalities gzip : vec = compressed 18 Generator for 18 equality of equality Typed Patches RepoDesc : Type vec : RepoDesc generators for elements compressed : RepoDesc a↔b at i : vec = vec generators for equalities gzip : vec = compressed { Patch vec compressed 18 Generator for 18 equality of equality Patches as a HIT 1.How do you define Patch using a higher inductive type? 2.What is the elimination rule for RepoDesc? 3.How do you use the elim. rule to define interp? 19 19 RepoDesc recursion To define a function RepoDesc A it suffices to 20 Generator for 20 equality of equality RepoDesc recursion To define a function RepoDesc A it suffices to map the element generators of RepoDesc to elements of A 20 Generator for 20 equality of equality RepoDesc recursion To define a function RepoDesc A it suffices to map the element generators of RepoDesc to elements of A map the equality generators of RepoDesc to equalities between the corresponding elements of A 20 Generator for 20 equality of equality RepoDesc recursion To define a function RepoDesc A it suffices to map the element generators of RepoDesc to elements of A map the equality generators of RepoDesc to equalities between the corresponding elements of A map the equality-between-equality generators to equalities between the corresponding equalities in A 20 Generator for 20 equality of equality RepoDesc recursion To define a function f : RepoDesc A it suffices to give 21 Generator for 21 equality of equality RepoDesc recursion To define a function f : RepoDesc A it suffices to give f(vec) := … : A 21 Generator for 21 equality of equality RepoDesc recursion To define a function f : RepoDesc A it suffices to give f(vec) := … : A f1(a↔b at i) := … : f(vec) = f(vec) 21 Generator for 21 equality of equality RepoDesc recursion To define a function f : RepoDesc A it suffices to give f(vec) := … : A f1(a↔b at i) := … : f(vec) = f(vec) f2(compose a b c d i j i≠j) := … : f1((a↔b at i)o(c↔d at j)) = f1((c↔d at j)o(a↔b at j)) 21 Generator for 21 equality of equality RepoDesc recursion To define a function f : RepoDesc A it suffices to give f(vec) := … : A f1(a↔b at i) := … : f(vec) = f(vec) f2(compose a b c d i j i≠j) := … : f1((a↔b at i)o(c↔d at j)) = f1((c↔d at j)o(a↔b at j)) You only specify f on generators, not id,o,!,group laws,congruence,… (1 patch and 4 basic axioms, instead of 4 and 14!) 21 Generator for 21 equality of equality RepoDesc recursion To define a function f : RepoDesc A it suffices to give f(vec) := … : A f1(a↔b at i) := … : f(vec) = f(vec) f2(compose a b c d i j i≠j) := … : f1((a↔b at i)o(c↔d at j)) = f1((c↔d at j)o(a↔b at j)) 22 Generator for 22 equality of equality RepoDesc recursion To define a function f : RepoDesc A it suffices to give f(vec) := … : A f1(a↔b at i) := … : f(vec) = f(vec) f2(compose a b c d i j i≠j) := … : f1((a↔b at i)o(c↔d at j)) = f1((c↔d at j)o(a↔b at j)) Type-generic equality rules say that functions act homomorphically on id,o,!,… 22 Generator for 22 equality of equality RepoDesc recursion To define a function f : RepoDesc A it suffices to give =f1(a↔b at i)o f(vec) := … : A f1(c↔d at j) f1(a↔b at i) := … : f(vec) = f(vec) f2(compose a b c d i j i≠j) := … : f1((a↔b at i)o(c↔d at j)) = f1((c↔d at j)o(a↔b at j)) Type-generic equality rules say that functions act homomorphically on id,o,!,… 22 Generator for 22 equality of equality RepoDesc recursion To define a function f : RepoDesc A it suffices to give f(vec) := … : A f1(a↔b at i) := … : f(vec) = f(vec) f2(compose a b c d i j i≠j) := … : f1((a↔b at i)o(c↔d at j)) = f1((c↔d at j)o(a↔b at j)) 23 Generator for 23 equality of equality RepoDesc recursion To define a function f : RepoDesc A it suffices to give f(vec) := … : A f1(a↔b at i) := … : f(vec) = f(vec) f2(compose a b c d i j i≠j) := … : f1((a↔b at i)o(c↔d at j)) = f1((c↔d at j)o(a↔b at j)) All functions on RepoDesc respect patches All functions on patches respect patch equality 23 Generator for 23 equality of equality Patches as a HIT 1.How do you define Patch using a higher inductive type? 2.What is the elimination rule for RepoDesc? 3.How do you use the elim. rule to define interp? 24 24 Interp Goal is to define: interp : vec = vec Bijection (Vec Char n) (Vec Char n) interp(id) = (λx.x, …) interp(q o p) = (interp q) ob (interp p) interp(!p) = !b (interp p) interp(a↔b at i) = swapat a b i 25 Generator for 25 equality of equality Interp Goal is to define: interp : vec = vec Bijection (Vec Char n) (Vec Char n) interp(id) = (λx.x, …) interp(q o p) = (interp q) ob (interp p) interp(!p) = !b (interp p) interp(a↔b at i) = swapat a b i But only tool available is RepoDesc recursion: no direct recursion over proofs of equality 25 Generator for 25 equality of equality interp : vec = vec Bijection (Vec Char n) (Vec Char n) interp(a↔b at i) = swapat a b i Need to pick A and define f(vec) := … : A f1(a↔b at i) := … : f(vec) = f(vec) f2(compose) := … 26 Generator for 26 equality of equality interp : vec = vec Bijection (Vec Char n) (Vec Char n) interp(a↔b at i) = swapat a b i Key idea: pick A = Type and define f(vec) := … : Type f1(a↔b at i) := … : f(vec) = f(vec) f2(compose) := … 27 Generator for 27 equality of equality interp : vec = vec Bijection (Vec Char n) (Vec Char n) interp(a↔b at i) = swapat a b i Key idea: pick A = Type and define f(vec) := Vec Char n : Type f1(a↔b at i) := … : f(vec) = f(vec) f2(compose) := … 28 Generator for 28 equality of equality interp : vec = vec Bijection (Vec Char n) (Vec Char n) interp(a↔b at i) = swapat a b i Key idea: pick A = Type and define f(vec) := Vec Char n : Type f1(a↔b at i) := … : Vec Char n = Vec Char n f2(compose) := … 29 Generator for 29 equality of equality interp : vec = vec Bijection (Vec Char n) (Vec Char n) interp(a↔b at i) = swapat a b i Key idea: pick A = Type and define f(vec) := Vec Char n : Type f1(a↔b at i) := ua(swapat a b i) : Vec Char n = Vec Char n f2(compose) := … 30 Generator for 30 equality of equality interp : vec = vec Bijection (Vec Char n) (Vec Char n) interp(a↔b at i) = swapat a b i Key idea: pick A = Type and define f(vec) := Vec Char n : Type f1(a↔b at i) := ua(swapat a b i) : Vec Char n = Vec Char n f2(compose) := … Voevodky’s univalence axiom ⊃ bijective types are equal 30 Generator for 30 equality of equality interp : vec = vec Bijection (Vec Char n) (Vec Char n) interp(a↔b at i) = swapat a b i Key idea: pick A = Type and define f(vec) := Vec Char n : Type f1(a↔b at i) := ua(swapat a b i) : Vec Char n = Vec Char n f2(compose) := 31 Generator for 31 equality of equality interp : vec = vec Bijection (Vec Char n) (Vec Char n) interp(a↔b at i) = swapat a b i Key idea: pick A = Type and define I(vec) := Vec Char n : Type I1(a↔b at i) := ua(swapat a b i) : Vec Char n = Vec Char n I2(compose) := 32 Generator for 32 equality of equality interp : vec = vec Bijection (Vec Char n) (Vec Char n) -1 interp(p) = ua (I1(p)) Key idea: pick A = Type and define I(vec) := Vec Char n : Type I1(a↔b at i) := ua(swapat a b i) : Vec Char n = Vec Char n I2(compose) := 33 Generator for 33 equality of equality interp : vec = vec Bijection (Vec Char n) (Vec Char n) -1 interp(p) = ua (I1(p)) Satisfies the desired equations (as propositional equalities): interp(id) = (λx.x, …) interp(q o p) = (interp q) ob (interp p) interp(!p) = !b (interp p) interp(a↔b at i) = swapat a b i 34 Generator for 34 equality of equality Summary 35 Generator for 35 equality of equality Summary I : RepoDesc Type interprets RepoDesc’s as Types, patches as bijections, satisfying patch equalities 35 Generator for 35 equality of equality Summary I : RepoDesc Type interprets RepoDesc’s as Types, patches as bijections, satisfying patch equalities Higher inductive elim. defines functions that respect equality: you specify what happens on the generators; homomorphically extended to id,o,!,... 35 Generator for 35 equality of equality Summary I : RepoDesc Type interprets RepoDesc’s as Types, patches as bijections, satisfying patch equalities Higher inductive elim. defines functions that respect equality: you specify what happens on the generators; homomorphically extended to id,o,!,... Univalence lets you give a computational model of equality proofs (here, patches); guaranteed to satisfy laws 35 Generator for 35 equality of equality Summary I : RepoDesc Type interprets RepoDesc’s as Types, patches as bijections, satisfying patch equalities Higher inductive elim. defines functions that respect equality: you specify what happens on the generators; homomorphically extended to id,o,!,... Univalence lets you give a computational model of equality proofs (here, patches); guaranteed to satisfy laws Shorter definition and code than using quotients: 1 basic patch & 4 basic axioms of equality, instead of 4 patches & 14 equations 35 Generator for 35 equality of equality Where does this programming technique come from? 36 Generator for 36 equality of equality Homotopy type theory p b a 37 37 Homotopy type theory a space is a type A p b a 37 37 Homotopy type theory a space is a type A p b a points are elements a:A 37 37 Homotopy type theory a space is a type A p b a points are elements paths are a:A proofs of equality p : a =A b 37 37 Homotopy type theory a space is a type A path operations p b a points are elements paths are a:A proofs of equality p : a =A b 37 37 Homotopy type theory a space is a type A path operations id p id : a = a (refl) b a points are elements paths are a:A proofs of equality p : a =A b 37 37 Homotopy type theory a space is a type A path operations id p id : a = a (refl) !p b a !p : b = a (sym) points are elements paths are a:A proofs of equality p : a =A b 37 37 Homotopy type theory a space is a type A path operations id p id : a = a (refl) !p b a !p : b = a (sym) q o p : a = c (trans) q c points are elements paths are a:A proofs of equality p : a =A b 37 37 Homotopy type theory a space is a type A path operations id p id : a = a (refl) !p b a !p : b = a (sym) q o p : a = c (trans) q c homotopies id o p = p points are !p o p = id elements paths are a:A r o (q o p) proofs of equality = (r o q) o p p : a =A b 37 37 Homotopy type theory a space is a type A path operations id : a = a (refl) !p : b = a (sym) q o p : a = c (trans) homotopies id o p = p points are !p o p = id elements paths are a:A r o (q o p) proofs of equality = (r o q) o p p : a =A b 37 37 Equality elimination rule Type of equalities is inductively between a and - generated by y2 p2 id y1 y3 p1 p3 a a 38 38 Equality elimination rule Type of equalities is inductively between a and - generated by y2 p2 id y1 y3 p1 p3 a a Fix a type A with element a:A. For a family of types C(y:A, p:a=y), to give an element of C(y,p) for all y and p:a=y, suffices to give an element of C(a,id) 38 38 Composition and Assoc _o_ : a = b b = c a = c id o p = p o-assoc : (p : a=b)(q : b=c)(r : c=d) p o (q o r) = (p o q) o r o-assoc id id id = id 39 39 Functions are functors f : A B has action at all levels f1 : (a1 a2 : A)