# Countable types

**URL:** <https://forums.swift.org/t/countable-types/39593>\
**Category:** Using Swift\
**Created:** [August 22, 2020, 4:07am UTC](https://forums.swift.org/t/countable-types/39593 "2020-08-22T04:07:19Z")\
**Posts on this page:** 17\
**Page:** 2

<div class="post-metadata">

**Author:** ![wowbagger](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/wowbagger/32/13847_2.png) [@wowbagger](https://forums.swift.org/u/wowbagger)\
**Post date:** [August 26, 2020, 5:26pm UTC](https://forums.swift.org/t/countable-types/39593/26 "2020-08-26T17:26:24Z")

</div>

> [@wtedst](#):
>
> by having a `startIndex` and `endIndex`

I don't think having a `startIndex` and `endIndex` makes a type automatically countable.

`Range` has `startIndex` and `endIndex`, but `0.5..<1.5` (which is basically the set [0.5, 1.5) in interval notation) isn't countable.

EDIT: `0.5..<1.5` is only one instance of `Range<Double>`. It would be a better argument to present that `Range<Double>` models the power set of real numbers, which is uncountable.

---

<div class="post-metadata">

**Author:** ![cukr](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/cukr/32/889_2.png) [@cukr](https://forums.swift.org/u/cukr)\
**Post date:** [August 26, 2020, 5:52pm UTC](https://forums.swift.org/t/countable-types/39593/28 "2020-08-26T17:52:09Z")

</div>

Do you want to know the ultimate uncountable type? Here it is:  
`class Foo { init() {} }`  
Yep, that's this simple. You can have a set of instances of this type that has any cardinality (assuming you already have a set of that cardinality. For example:

```swift
for _ in Irrational.allCases {
  Foo()
}

```

You just created uncountable number of instances of `Foo`. You might say that it's cheating, and all of them are the same, but each new instance is distinguishable from one another (for example with `===` or `ObjectIdentifier`) so they are different instances.

---

<div class="post-metadata">

**Author:** ![Lantua](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lantua/32/8868_2.png) [@Lantua](https://forums.swift.org/u/Lantua)\
**Post date:** [August 26, 2020, 5:56pm UTC](https://forums.swift.org/t/countable-types/39593/29 "2020-08-26T17:56:59Z")

</div>

> [@cukr](#):
>
> You just created uncountable number of instances of `Foo` .

If our machine supports hypertask, that is. Well, it can store infinite bytes, so adding a few more features wouldn't hurt.

---

<div class="post-metadata">

**Author:** ![zienag](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/zienag/32/43995_2.png) [@zienag](https://forums.swift.org/u/zienag)\
**Post date:** [August 26, 2020, 6:19pm UTC](https://forums.swift.org/t/countable-types/39593/30 "2020-08-26T18:19:33Z")

</div>

Then what you talking about is "size of `[Int]` with _fixed_ arbitrary number of elements N", not the size of `[Int]` itself.

---

<div class="post-metadata">

**Author:** ![wowbagger](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/wowbagger/32/13847_2.png) [@wowbagger](https://forums.swift.org/u/wowbagger)\
**Post date:** [August 26, 2020, 6:31pm UTC](https://forums.swift.org/t/countable-types/39593/32 "2020-08-26T18:31:28Z")

</div>

By definition, a set _S_ is countable if and only if each member of _S_ can map to a different natural number without running out of all natural numbers, i.e. if there is an injection from _S_ to the ℕ. One reasonable interpretation is that, given an arbitrary member of a countable set, one can figure out what the next member of the same set is. This is because given an arbitrary natural number, one can always find the next natural number. If this interpretation is correct, then we can define a `Countable` protocol like this:

```swift
protocol Countable {
    var next: Self { get }
}

```

If a type is countable, then it must be able to conform to this protocol. For example,

```swift
extension Int: Countable {
    var next: Self { self + 1 }
}

extension Void: Countable { // Let's pretend that void can be extended
    var next: Self { () }
    // There is only 1 possible value, 
    // so we can just map all natural numbers to the same value.
}

```

With the exception of `Never`:

```swift
extension Never: Countable {
    var next: Never { fatalError() }
    // This is the edge case.
    // Never is basically an empty set.
}

```

I'm not sure if this `Countable` protocol is fool-proof, or if it adds much to the discussion, but hopefully in some way it helps clarify things.

---

<div class="post-metadata">

**Author:** ![zienag](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/zienag/32/43995_2.png) [@zienag](https://forums.swift.org/u/zienag)\
**Post date:** [August 26, 2020, 6:42pm UTC](https://forums.swift.org/t/countable-types/39593/33 "2020-08-26T18:42:26Z")

</div>

> [@wtedst](#):
>
> That is, `[Int]` and `String` form a countable set of values even on a computer with infinite memory by having a `startIndex` and `endIndex` , for instance

As I've shown above, `[Int]` size is at least not smaller than the size of `Double`, that's proven (I've found a subset of `String` and `[Int]` with 1 to 1 correspondence to every member of `Double`). You saying that `Double` is uncountable and `[Int]` is countable, but countable is strictly smaller than uncountable, that's a contradiction. So, either `Double` is countable (finite memory case) or `[Int]` is uncountable (infinite memory case).

---

<div class="post-metadata">

**Author:** ![John\_McCall](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/john_mccall/32/1351_2.png) [@John\_McCall](https://forums.swift.org/u/John_McCall)\
**Post date:** [August 26, 2020, 7:22pm UTC](https://forums.swift.org/t/countable-types/39593/34 "2020-08-26T19:22:53Z")

</div>

All types are countable sets, even on theoretical machines with infinite memory. Types are inhabited only by values that can be produced by some program, and there are countably infinite such programs.

Whether a type is _enumerable_ by some program is a totally different question.

---

<div class="post-metadata">

**Author:** ![SDGGiesbrecht](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sdggiesbrecht/32/4768_2.png) [@SDGGiesbrecht](https://forums.swift.org/u/SDGGiesbrecht)\
**Post date:** [August 26, 2020, 7:46pm UTC](https://forums.swift.org/t/countable-types/39593/36 "2020-08-26T19:46:09Z")

</div>

As amusing as this thread seems to have become, mind if I ask how any of it is of practical use? What actually prompted the question in the first place? @wowbagger, when you asked it, were you trying to design some API or functionality when this came up? Was it some sort of homework question or academic challenge? Or is it really just pure abstract mathematical curiosity?

If you were actually hoping for help with something ([the point of the “Using Swift” category](https://forums.swift.org/t/about-the-using-swift-category/21)), then please share more of what it is you are trying to solve, so that we can get back to trying to help you with the underlying issue.

---

<div class="post-metadata">

**Author:** ![Jumhyn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jumhyn/32/3891_2.png) [@Jumhyn](https://forums.swift.org/u/Jumhyn)\
**Post date:** [August 26, 2020, 7:47pm UTC](https://forums.swift.org/t/countable-types/39593/37 "2020-08-26T19:47:11Z")

</div>

> [@wtedst](#):
>
> The same talk goes about `[Int]` — if you let `Int` itself be infinitely large and you have no upper bound on the length of the array, then `[Int]` actually does model the power set of natural numbers, which is uncountable.

This would be the set of all _finite sequences_ of integers, which is still countable, no? Just like there are no infinitely long `String`s, there are no infinitely long `Array`s.

---

<div class="post-metadata">

**Author:** ![Lantua](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lantua/32/8868_2.png) [@Lantua](https://forums.swift.org/u/Lantua)\
**Post date:** [August 26, 2020, 7:47pm UTC](https://forums.swift.org/t/countable-types/39593/38 "2020-08-26T19:47:22Z")

</div>

> [@wtedst](#):
>
> I'm curious of other people find this thread super funny because this whole discussion is very mathematically imprecise, so whatever we are doing here makes very little sense without much stricter definitions 😅

Def not the only one.

---

<div class="post-metadata">

**Author:** ![wowbagger](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/wowbagger/32/13847_2.png) [@wowbagger](https://forums.swift.org/u/wowbagger)\
**Post date:** [August 26, 2020, 7:52pm UTC](https://forums.swift.org/t/countable-types/39593/39 "2020-08-26T19:52:37Z")

</div>

It was a combination of API design and curiosity.

I was trying to calculate degrees of separation between values of `Strideable` types, and realised it would only make sense if the values are in a subset of a countable set. I could define degrees of separation between values of `BinaryInteger` types, and not any other types, so I got curious in general.

I didn't expect this thread to get this long, to be honest.

---

<div class="post-metadata">

**Author:** ![Jumhyn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jumhyn/32/3891_2.png) [@Jumhyn](https://forums.swift.org/u/Jumhyn)\
**Post date:** [August 26, 2020, 8:06pm UTC](https://forums.swift.org/t/countable-types/39593/42 "2020-08-26T20:06:57Z")

</div>

The union you wrote is only a union of finite subsets of the naturals. For instance, `{2, 4, 6, 8, ...}` is not contained in any of the terms of the union, but it is contained in the power set. Same goes for any infinite subset of the naturals.

---

<div class="post-metadata">

**Author:** ![dfsweeney](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dfsweeney/32/12031_2.png) [@dfsweeney](https://forums.swift.org/u/dfsweeney)\
**Post date:** [August 26, 2020, 8:09pm UTC](https://forums.swift.org/t/countable-types/39593/43 "2020-08-26T20:09:26Z")

</div>

Hi,

I was interpreting the original idea for degrees of separation as solving for the number of `T.Stride` between a start and an end.

Binary integers have an intrinsic stride of 1, so it’s easy to use that.

For IEEE floats, the distance between each value varies depending where on the number line you are—the intrinsic stride between valid values is less near 0 and more out near the positive and negative ends. I might have misunderstood.

I feel like there is probably a way to characterize the IEEE float distance between two floats, but it would be a function, not a number, or something like the ratio between the density of the number line at one point and the density of the number line at the other point, scaled by the floating point distance.

But maybe I’m misunderstanding what you meant and stating the obvious, that happens a lot to me.

—Dan

---

<div class="post-metadata">

**Author:** ![wowbagger](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/wowbagger/32/13847_2.png) [@wowbagger](https://forums.swift.org/u/wowbagger)\
**Post date:** [August 26, 2020, 8:30pm UTC](https://forums.swift.org/t/countable-types/39593/44 "2020-08-26T20:30:30Z")

</div>

> [@dfsweeney](#):
>
> I was interpreting the original idea for degrees of separation as solving for the number of `T.Stride` between a start and an end.

This is exactly what I wanted to do at first.

For floating point values, I completely ignored the IEEE standard. I treated them as truncated real numbers, which is what all floating point types intended to model (imprecisely). I think there is also the problem that a number can have multiple equivalent representations, if taking the IEEE standard into account. For example, 0.3 can be represented as 3×10⁻¹, 30×⁻², 300×10⁻³, and so on.

---

<div class="post-metadata">

**Author:** ![Lantua](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lantua/32/8868_2.png) [@Lantua](https://forums.swift.org/u/Lantua)\
**Post date:** [August 27, 2020, 12:04am UTC](https://forums.swift.org/t/countable-types/39593/45 "2020-08-27T00:04:19Z")

</div>

> [@wowbagger](#):
>
> a number can have multiple equivalent representations, if taking the IEEE standard into account. For example, 0.3 can be represented as 3×10⁻¹, 30×⁻², 300×10⁻³

IIRC, that's only true for ±0, and the family of NaN.

* * *

Also, we have [`greatestFiniteMagnitude`](https://developer.apple.com/documentation/swift/floatingpoint/1849534-greatestfinitemagnitude) and [`nextDown`](https://developer.apple.com/documentation/swift/floatingpoint/3017979-nextdown), sooo...

---

<div class="post-metadata">

**Author:** ![zienag](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/zienag/32/43995_2.png) [@zienag](https://forums.swift.org/u/zienag)\
**Post date:** [August 27, 2020, 10:07am UTC](https://forums.swift.org/t/countable-types/39593/46 "2020-08-27T10:07:26Z")

</div>

You're right, restricting `String` and `[Int]` to finite length does make sense. My reasonings are based on assumption that it can be infinite, but that probably is not true 🙂

---

<div class="post-metadata">

**Author:** ![salutis](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/salutis/32/1156_2.png) [@salutis](https://forums.swift.org/u/salutis)\
**Post date:** [August 30, 2020, 9:20pm UTC](https://forums.swift.org/t/countable-types/39593/47 "2020-08-30T21:20:27Z")

</div>

> [@wowbagger](#):
>
> I believe an infinite sequence of bits is uncountable, as per Cantor's diagonal argument.

The diagonal argument is about an infinite sequence of infinite sequences of bits. A single infinite sequence of bits is countable, like naturals are.

[Previous page](https://forums.swift.org/t/countable-types/39593.md?page=1)
