# Slides from a talk about Swift generics

**URL:** <https://forums.swift.org/t/slides-from-a-talk-about-swift-generics/68774>\
**Category:** Compiler\
**Created:** [December 2, 2023, 3:48pm UTC](https://forums.swift.org/t/slides-from-a-talk-about-swift-generics/68774 "2023-12-02T15:48:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Slava\_Pestov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/slava_pestov/32/30204_2.png) [@Slava\_Pestov](https://forums.swift.org/u/Slava_Pestov)\
**Post date:** [December 2, 2023, 3:48pm UTC](https://forums.swift.org/t/slides-from-a-talk-about-swift-generics/68774/1 "2023-12-02T15:48:16Z")

</div>

I recently visited the PL group at Cornell to give a talk about our formal model and implementation of generics in Swift. While the talk was not recorded, the slides are available online: [https://pl.cs.cornell.edu/pldg/slava-pestov.pdf](https://pl.cs.cornell.edu/pldg/slava-pestov.pdf).

The talk was meant for a mathematically-inclined audience unfamiliar with Swift, and the slides might be hard to parse without the narration, but perhaps some of you will find them interesting anyway. Let me know if you have questions!

---

<div class="post-metadata">

**Author:** ![Sajjon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sajjon/32/12044_2.png) [@Sajjon](https://forums.swift.org/u/Sajjon)\
**Post date:** [December 2, 2023, 10:27pm UTC](https://forums.swift.org/t/slides-from-a-talk-about-swift-generics/68774/2 "2023-12-02T22:27:21Z")

</div>

Beamer \<3

Looks great at first glance, will go through it in full on Desktop.

Thanks for sharing!

---

<div class="post-metadata">

**Author:** ![ibex](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ibex/32/30439_2.png) [@ibex](https://forums.swift.org/u/ibex)\
**Post date:** [December 3, 2023, 10:12am UTC](https://forums.swift.org/t/slides-from-a-talk-about-swift-generics/68774/3 "2023-12-03T10:12:42Z")

</div>

Thank you for sharing it.

I have started reading it, and I am going to finish it. 🙂

But I have a question (more may follow.)

On page, 15/108:

> Type parameters
> 
> Recursively:  
> Root: `τi` for `0≤i<n`  
> Member: U.A for type parameter U and associated type name A

What are `root` and `τi`, and how are they related to `U` ?

---

<div class="post-metadata">

**Author:** ![Slava\_Pestov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/slava_pestov/32/30204_2.png) [@Slava\_Pestov](https://forums.swift.org/u/Slava_Pestov)\
**Post date:** [December 3, 2023, 5:13pm UTC](https://forums.swift.org/t/slides-from-a-talk-about-swift-generics/68774/4 "2023-12-03T17:13:27Z")

</div>

> [@ibex](#):
>
> On page, 15/108:
> 
> > Type parameters
> > 
> > Recursively:  
> > Root: `τi` for `0≤i<n`  
> > Member: U.A for type parameter U and associated type name A
> 
> What are `root` and `τi`, and how are they related to `U` ?

Imagine you're implementing a Swift compiler. To type check the `binarySearch()` function here, you need a data type in the compiler to model type parameters, like `C`, `E`, `C.Element`, etc that can be written in the function's declaration and body:

```swift
func binarySearch<C: Collection, E>(_ c: C, _ e: E) where C.Element == E, E: Comparable { ... }

```

The type parameters `C` and `E` are called root type parameters. The _names_ of root type parameters don't play any role in the language semantics, so to simplify the formal model to the bare minimum, we replace them with _numbers_. We assign 0 to `C` and 1 to `E` and represent each root type parameter by an integer. A member type parameter like `C.Element` has a base type `C` and a name `Element`; the most general form of a member type parameter is `U.A` where `U` is some other type parameter and `A` is a name.

So a data type to model type parameters looks exactly like this:

```swift
enum TypeParameter {
  case root(Int)
  indirect case member(base: TypeParameter, name: String)
}

```

Eg, `C` becomes `.root(0)`, `C.Slice.Element` becomes `.member(.member(.root(0), "Slice"), "Element")`, etc.

---

<div class="post-metadata">

**Author:** ![RukbaNitgar](https://avatars.discourse-cdn.com/v4/letter/r/6a8cbe/32.png) [@RukbaNitgar](https://forums.swift.org/u/RukbaNitgar)\
**Post date:** [December 4, 2023, 8:14am UTC](https://forums.swift.org/t/slides-from-a-talk-about-swift-generics/68774/5 "2023-12-04T08:14:32Z")

</div>

Thanks, appreciate that you shared this[.](https://paybyplatema.site/)
