3. Product type
1 Type Formation
Γ A : ∗ Γ B : ∗
Γ A × B : ∗
2 Constructor
Γ a : A Γ b : B
Γ (a, b) : A × B
3 Eliminators
Γ p : A × B
Γ fst p : A
Γ p : A × B
Γ snd p : B
Dmytro Mitin Product type
4. Product type
4 Computation rules (”β-reduction“)
Γ a : A Γ b : B
Γ fst(a, b) ≡ a : A
Γ a : A Γ b : B
Γ snd(a, b) ≡ b : B
5 Uniqueness principle (”η-conversion“)
Γ p : A × B
Γ (fst p, snd p) ≡ p : A × B
Dmytro Mitin Product type
5. Partial case
Partial case of
A × B
is sum type (coproduct)
A1 + A2 or B1 + B2
Dmytro Mitin Product type
6. Haskell, Scala and Java
Haskell
data Pair a b = P {first :: a, second :: b}
Scala
case class Pair[A, B](fst: A, snd: B)
Java
public class Pair<A, B> {
private A fst;
private B snd;
}
Dmytro Mitin Product type
7. ProvingGround. Built-in type
git clone https://github.com/siddhartha-gadgil/ProvingGround.git
cd ProvingGround
sbt mantle/test:console
val A = "A" :: Type
val B = "B" :: Type
val a = "a" :: A
val b = "b" :: B
val pair = PairTerm(a, b) !: ProdTyp(A, B)
pair.first == a
pair.second == b
val recAandBA = ProdTyp(A, B).rec(A)
val recAandBB = ProdTyp(A, B).rec(B)
val first = recAandBA(a :-> (b :-> a))
val second = recAandBB(a :-> (b :-> b))
first(pair) == a
second(pair) == b
Dmytro Mitin Product type
8. ProvingGround. Built-in type
val recAandBAandB = ProdTyp(A, B).rec(ProdTyp(A, B))
val id = recAandBAandB(a :-> (b :-> PairTerm(a, b)))
id(pair) == pair
Dmytro Mitin Product type
9. ProvingGround. Custom type
import TLImplicits.
import shapeless.
val A = "A" :: Type
val B = "B" :: Type
val a = "a" :: A
val b = "b" :: B
val AandB = "(A, B)" :: Type
val ProdInd = ("mkPair" ::: A ->>: (B ->>: AandB)) =: AandB
val makePair :: HNil = ProdInd.intros
val pair = makePair(a)(b) !: AandB
val recAandBA = ProdInd.rec(A)
val recAandBB = ProdInd.rec(B)
val first = recAandBA(a :-> (b :-> a))
val second = recAandBB(a :-> (b :-> b))
first(pair) == a
second(pair) == b
Dmytro Mitin Product type
10. ProvingGround. Custom type
val recAandBAandB = ProdInd.rec(AandB)
val id = recAandBAandB(a :-> (b :-> makePair(a)(b)))
id(pair) == pair
Dmytro Mitin Product type