Lecture 3: Inductive/co-Inductive types
Examples of inductive types:
$Nat$:
- $zero: Nat$
- $succ: Nat ⟶ Nat$
$Bool$:
- $true: Bool$
- $false: Bool$
Inductive | Co-inductive types |
---|---|
Positive connectives | Negative connectives |
built from constructor | built from destructors |
initial algebra | final co-algebra |
co-limits | limits |
ex: $Nat, Bool, \oplus, \otimes$ | ex: record (defined by its projections), streams (we can take the first or the rest), in linear logic: $\&$ |
recursor | “cofix point” (to recursively build objects from the destructor) |
NB: In linear logic, there are two conjunctions: one is positive, the other negative.
Inductive types
“Rec” is an eliminator for the constructor.
Natural numbers
In Coq:
Inductive Nat: U :=
zero: Nat
| succ: Nat -> Nat
or, by just giving the argument of the type:
Inductive Nat: U :=
zero
| succ (n: Nat)
With the system T operator:
\[\cfrac{Γ ⊢ t: Nat \quad Γ ⊢ C: U \quad Γ ⊢ u: C \quad Γ, n:Nat, a: C ⊢ v: C}{Γ ⊢ \texttt{rec } \, t \, [zero → u \mid succ \, n/a → v]: C}\]-
$\texttt{rec } \, zero \, [zero → u \mid succ \, n/a → v] ⟶ u$
- \[\texttt{rec } \, (succ \, n') \, [zero → u \mid succ \, n /a→ v] ⟶ v[n:=n']\left[a:= \texttt{rec } \, n' \, [zero → u \mid succ \, n/a → v]\right]\]
let rec Rec n = match n with
zero -> u
| suc n' -> v[n := n'][a := Rec n']
Dependent version:
\[\cfrac{Γ ⊢ t: Nat \quad Γ, n:Nat ⊢ C: U \quad Γ ⊢ u: C[0/n] \quad Γ, n:Nat, a: C[n/n] ⊢ v: C[succ \, n, n]}{Γ ⊢ \texttt{rec } \, t \, [zero → u \mid succ \, n/a → v]: C[t/n]}\]NB: it’s an “annotated version” of Peano arithmetic induction scheme, where the annotations are the proofs.
Lists
Inductive list (A: U): U :=
nil : list A
| cons: A -> list A
-
$\texttt{rec } \, nil \, [nil → u \mid cons \, a \, l/b → v] ⟶ u$
- \[\texttt{rec } \, (cons \, t \, l) \, [nil → u \mid cons \, a \, l /b→ v] ⟶ v[t/a][l/l]\left[\texttt{rec } \, l \, [nil → u \mid cons \, a \, l /b→ v]/b\right]\]
Dependent version:
\[\cfrac{Γ, A:U ⊢ l: list \, A \quad Γ, A:U, l:list \, A ⊢ C: U \quad Γ, A:U ⊢ u: C[nil/l] \quad Γ, A:U, a:A, l: list \, A, b:C[l/l] ⊢ v: C[cons \, a \, l/l]}{Γ, A:U ⊢ \texttt{rec } \, l \, [nil → u \mid cons \, a \, l/b → v]: C[l/l]}\]NB: if $C$ is a proposition $hProp$, then the proof doesn’t matter!
Booleans
Inductive Bool: U :=
true : Bool
| false: Bool
“if b then t else u”
Trees
Inductive tree (A: Ui): Uj≥i :=
leaf : tree A
| node : A -> tree A -> tree A -> tree A
Co-inductive types
Co-inductive types are defined by their destructors.
Products
Record Prod (A: Ui) (B: Ui'): Uj≥i,i' := {fst: A, snd: B}
- $fst: Prod \, A \, B ⟶ A$
- $snd: Prod \, A \, B ⟶ B$
Now, in let’s build the (almost) dual version of rec: a constructor for the destructors.
\[\cfrac{Γ, A:U_i, B:U_{i'} ⊢ t: A \quad Γ, A:U_i, B:U_{i'} ⊢ u: B}{Γ ⊢ \lbrace fst = t ; snd = u \rbrace: Prod \, A \, B}\]- $fst \, \lbrace fst=t; snd=u \rbrace ⟶ t$
- $snd \, \lbrace fst=t; snd=u \rbrace ⟶ u$
NB: it looks like the categorical product
Streams
They represent infinite lists.
CoInductive Stream (A: Ui) : Uj≥i := {hd: A; tl: Stream A}
- $hd: Stream \, A ⟶ A$
- $tl: Stream \, A ⟶ Stream \, A$
- $hd \, \lbrace hd=t; \; tl/a=u \rbrace ⟶ t$
- $tl \, \lbrace hd=t; \; tl/a=u \rbrace ⟶ u[\lbrace hd=t;\; tl/a =u \rbrace/a]$
Not that interesting, since we have always the same tail.
Notation by Abel (inventor of Agda), Pientka (Beluga), and Setzer.
NB: Warning! the “co-inductive” keyword in Coq isn’t what we’re talking about. Negative connectives are called “primitive projections” in Coq.
Leave a comment