Lecture 4: Streams

Streams:
\[\cfrac{Γ, c: C ⊢ A: U\quad Γ, c: C ⊢ t:A\quad Γ ⊢ v: C\quad Γ ⊢ C: U\quad Γ, s: \prod_{c:C} Stream \, A, c:C ⊢ U: Stream \, A}{Γ ⊢ \lbrace hd=t; \; tl/s = u \rbrace_{/c} \, v : Stream \, A}\]
  • \[hd \lbrace hd=t; \; tl/s = u \rbrace_{/c}^v ⟶ t[c:=v]\]
  • \[tl \lbrace hd=t; \; tl/s = u \rbrace_{/c}^v ⟶ u[c := v]\left[s := λx. \lbrace hd=t; \; tl/s = u \rbrace_{/c} \, x\right]\]

Exs:

  • $prod \, n \, A = \underbrace{A × ⋯ × A}_{n \text{ times}}$
\[number\_generator: Nat ⟶ Stream \, Nat\] \[numbers ≝ \lbrace hd=c; \; tl/s = s (succ \, c) \rbrace_c \, zero : Stream \, Nat\]
  • $hd \, numbers ⟶ zero$
  • $tl numbers ⟶ (λx. \lbrace hd = c; \; tl/s = s \, (succ \, c) \rbrace_c \, x) (succ \, zero)$
  • $hd \, tl \, numbers ⟶ succ \, zero$
  • $tl \, tl \, number ⟶ \lbrace hd=c; \; tl/s = s \, (succ \, c) \rbrace_c \, (succ \, (succ \, zero))$

Write some functions about streams:

  1. a function which takes a stream of $Nat$ and multiplies all elements by $2$
  2. a function which takes two streams of $Nat$ and add all elements pointwise.

1.

\[multstream ≝ λs: Stream \, Nat. \lbrace hd=(hd \, c) × 2; \; tl/s' = s' \, (tl \, c) \rbrace_{/c} \, s\]

Indeed:

\[hd \Big(multstream \, \lbrace hd=3; \; tl=\lbrace hd=7; \; tl=\lbrace hd=2; \; ⋯ \rbrace \rbrace \rbrace \Big) ⟶ 6\] \[tl \, \Big(multstream \, \lbrace hd=3; \; tl=\lbrace hd=7; \; tl=\lbrace hd=2; \; ⋯ \rbrace \rbrace \rbrace \Big) ⟶ \lbrace hd=(hd \, c) × 2; \; tl/s' = s' \, (tl \, c) \rbrace_{/c} \, \lbrace hd = 7; ⋯ \rbrace\]

2.

\[λ s_1: Stream \, Nat, s_2: Stream \, Nat.\\ \lbrace hd: hd \, c.1 + hd \, c.2; \quad tl/s' = s' \, (tl \, c.1, tl \, c.2) \rbrace_{/c} \, (s_1, s_2)\]

$Σ$-type as a co-inductive type

A $Σ$-type is just an instance of a negative type, i.e a co-inductive type without recursivity.

\[\sum\limits_{ a:A } B\] \[\cfrac{Γ ⊢ A: U_i \quad Γ, a:A ⊢ B: U_j}{Γ ⊢ \underbrace{\lbrace fst: A; snd: B \, (fst) \rbrace}_{\text{an alternative notation for }\sum\limits_{ a:A } B }: U_{\max(i, j)}}\]

Destructors:

\[\cfrac{Γ ⊢ t: \sum\limits_{ a:A } B}{Γ ⊢ fst \, t: A}\]

and

\[\cfrac{Γ ⊢ t: \sum\limits_{ a:A } B}{Γ ⊢ snd \, t: B \, (fst \, t)}\] \[\cfrac{Γ ⊢ t: A \quad Γ ⊢ u: B(t)}{Γ ⊢ \underbrace{\lbrace fst=t; snd=u \rbrace}_{\text{ also denoted by } (t, u)}: \sum\limits_{ a:A } B}\]
  • \[fst \, (t, u) = fst \, \lbrace fst=t; snd=u \rbrace ⟶ t\]
  • \[snd \, (t, u) = snd \, \lbrace fst=t; snd=u \rbrace ⟶ u\]

$η$-rule for $Σ$-types

\[v ≡ \underbrace{(v.1, v.2)}_{\text{surjective pairing, a.k.a } η\text{-rule for } Σ\text{ types}}\\ v ≡ \lbrace fst = \underbrace{fst}_{\text{just syntactic sugar}} \, v; snd = snd \, v\rbrace\]

co-Nat

CoNat:

like Nat, but with an infinite element $∞$: it’s the largest set generated by $zero$ and $succ$, whereas Nat is the smallest one.

Nat CoNat
zero  
succ  
\[tl: CoNat ⟶ option \; CoNat\]

where

Inductive option (A: Ui): Ui :=
      none : option A
    | some : A -> option A

In category theory: $1+A$

CoInductive CoNat: U1 := {tl: option CoNat}
\[\cfrac{}{Γ ⊢ μ_X \lbrace tl = option \, X \rbrace: U_1}\] \[\cfrac{Γ ⊢ t: CoNat}{Γ ⊢ tl \, t: option \, CoNat}\] \[\cfrac{Γ, s: CoNat ⊢ U: option \, CoNat}{Γ ⊢ \lbrace tl/s = u \rbrace : CoNat}\] \[∞ ≝ \lbrace tl/s = Some \, s \rbrace\\ 0 ≝ \lbrace tl/s = none \rbrace \\ 1 ≝ \lbrace tl/s = Some \, \lbrace tl/s = None \rbrace \rbrace\] \[tl \, \lbrace tl/s = u\rbrace ⟶ u[s := \lbrace tl/s = u \rbrace]\]

Two ways to build elements of $CoNat$:

  • by co-induction
  • by induction on $Nat$

General constructions

\[t, A ≝ \lbrace d_1: N; ⋯ ; d_n: N \rbrace \qquad \text{negative: destructors}\\ \mid \lbrace c_1: P; ⋯ ; c_n: P \rbrace \qquad \text{positive: constructors}\]

ex:

  • $0: Unit ⟶ Nat$
  • $S: Nat ⟶ Nat$
  • $\lbrace 0: Unit; S: Nat \rbrace$

No dependency for constructors right now, but we have dependent types for destructors (cf. $Σ$-types).

\[cons: A ⟶ list \, A ⟶ list \, A \\ cons: A \otimes list \, A ⟶ list \, A\]

where $\otimes$ is the positive conjunction (in linear logic): $Σ$-type is the negative one.

\[P ≝ 1 \mid (a:A) \otimes P\\ N ≝ \prod\limits_{ a:A } N \mid A\]

$\prod$ is a negative type which is not co-inductive, that is: its introduction rules are reversible (you can recover the premises from the conclusion).

Ex:

Not reversible:

\[\cfrac{?}{Γ ⊢ n: Nat}\]

because it could come from $zero$ or $succ$

Revertible (negative)

\[\cfrac{⊢ A \quad ⊢ B}{⊢ A \& B}\]

Usually:

  • negative types have

    • an $η$-rule on the constructor/eliminator
    • universal properties that include arrows going into them (limits)
  • positive types have

    • an $η$-rule on the destructors/eliminator
    • universal properties that include arrows going out of them (co-limits)

\[t, A ≝ ⋯ \mid case \; t \; of \; [c_1 \, p ⇒ t \mid ⋯ \mid c_n \, p ⇒ t]\\ p ≝ () \mid (a, p) \\ t ≝ ⋯ \mid c_i \, u \mid d_i \, t\\ u ≝ () \mid (t, u) \mid \lbrace d_1 = v; ⋯ ; d_n = v \rbrace\\ v ≝ λa:A. v \mid t\]

Leave a comment