Binary sums/coproducts [01GH]
We can encode binary sums A+B of arbitrary types A,B~\mathit {type} using either a large elimination or the axiom of universes. For simplicity, we will use the former, but you should see if you can figure out how to do the latter.
Encoding Formation [01GI]
We wish to encode the following formation rule:
\frac { A,B~\mathit {type} }{ A+B~\mathit {type} }To that end, we define:
A+B :\equiv {\mathop {\mathchoice {\textstyle \sum }{\textstyle \sum }{\scriptstyle \sum }{\scriptscriptstyle \sum } }_{(l:\mathsf {bool})}}\mathsf {If}(l;A;B)This is similar to the idea of a tagged union in C-like languages: we pair a label with the data that it refers to. Unlike in C, the type system keeps track for us of what sort of data can appear paired with what tag: this shows that C should have been dependently typed.
Encoding Introduction [01GJ]
We expect to have two constructors, corresponding to the left and right injections of the binary sum.
\frac { u:A }{ \mathsf {inl}~u:A+B } \qquad \frac { u:B }{ \mathsf {inr}~u:A+B }We can achieve this by defining the two constructors to pair the appropriate label with the data:
\begin {aligned} \mathsf {inl}~u &:\equiv \left \langle \mathsf {true},u\right \rangle \\ \mathsf {inr}~u &:\equiv \left \langle \mathsf {false},u\right \rangle \end {aligned}Make sure you understand for yourself why these are well-typed!
Encoding Elimination [01GK]
We will encode a dependent elimination rule:
\frac { u:A+B\quad x:A+B\vdash C[x]~\mathit {type} \quad x:A\vdash v[x]:C[\mathsf {inl}~x] \quad x:B\vdash w[x]:C[\mathsf {inr}~x] }{ \mathsf {case}_{x\mathpunct {.}{C[x]}}(u; x\mathpunct {.}{v[x]}; x\mathpunct {.}{w[x]}) : C[u] }This is encoded by combining the elimination rules for the booleans and the sum of a family of types. We do so via an implicit definition.
\begin {aligned} \mathsf {case}_{x\mathpunct {.}{C[x]}}(\left \langle \mathsf {true},u\right \rangle ;x\mathpunct {.}{v[x]};x\mathpunct {.}{w[x]}) &:\equiv v[u] \\ \mathsf {case}_{x\mathpunct {.}{C[x]}}(\left \langle \mathsf {false},u\right \rangle ;x\mathpunct {.}{v[x]};x\mathpunct {.}{w[x]}) &:\equiv w[u] \end {aligned}The corresponding explicit definition is more complex, and follows by (un)currying. We could have defined:
\begin {aligned} & \mathsf {caseAux}_{x\mathpunct {.}{C[x]}}( x\mathpunct {.}{v[x]}; x\mathpunct {.}{w[x]} ) : {\mathop {\mathchoice {\textstyle \prod }{\textstyle \prod }{\scriptstyle \prod }{\scriptscriptstyle \prod } }_{(l:\mathsf {bool})}} {\mathop {\mathchoice {\textstyle \prod }{\textstyle \prod }{\scriptstyle \prod }{\scriptscriptstyle \prod } }_{(u:\mathsf {If}(l;A;B))}} C[\left \langle l,u\right \rangle ] \\ &\mathsf {caseAux}_{x\mathpunct {.}{C[x]}}( x\mathpunct {.}{v[x]}; x\mathpunct {.}{w[x]} ) :\equiv \lambda l\mathpunct {.} \mathsf {if}_{ z\mathpunct {.} {\mathop {\mathchoice {\textstyle \prod }{\textstyle \prod }{\scriptstyle \prod }{\scriptscriptstyle \prod } }_{(u:\mathsf {If}(z;A;B))}} C[\left \langle z,u\right \rangle ] }(l, \lambda u\mathpunct {.} v[u]; \lambda u\mathpunct {.} w[u]) \\[12pt] &\mathsf {case}_{x\mathpunct {.}{C[x]}}(u; x\mathpunct {.}{v[x]}; x\mathpunct {.}{w[x]}) :\equiv \mathsf {caseAux}_{x\mathpunct {.}{C[x]}}(x\mathpunct {.}{v[x]}; x\mathpunct {.}{w[x]})~(u.1)~(u.2) \end {aligned}Encoding Computation [01GL]
You can verify for yourself that the following computation rules hold:
\frac { u:A \quad x:A+B\vdash C[x]~\mathit {type} \quad x:A\vdash v[x]:C[\mathsf {inl}~x] \quad x:B\vdash w[x]:C[\mathsf {inr}~x] }{ \mathsf {case}_{x\mathpunct {.}{C[x]}}(\mathsf {inl}~u;x\mathpunct {.}{v[x]}; x\mathpunct {.}{w[x]}) \equiv v[u] :C[\mathsf {inl}~u] } \frac { u:B \quad x:A+B\vdash C[x]~\mathit {type} \quad x:A\vdash v[x]:C[\mathsf {inl}~x] \quad x:B\vdash w[x]:C[\mathsf {inr}~x] }{ \mathsf {case}_{x\mathpunct {.}{C[x]}}(\mathsf {inr}~u;x\mathpunct {.}{v[x]}; x\mathpunct {.}{w[x]}) \equiv w[u] :C[\mathsf {inr}~u] }