<p>The short answer is: they are not dual. </p>

<p>The <code>Try[T]</code> type is basically the sum type $T + E$, where $E$ is the type of exceptions. </p>

<p>A typical way of rendering a future <code>Future[T]</code> is via <em>temporal logic</em>. Here, a future can be understood as a "weak eventually" operator, $\Diamond T$, saying that at some point in the future you may receive a $T$. This can be decomposed as the coinductive type $\nu \alpha.\;T + \bullet\alpha$, where $\bullet$ is the "next-step" modality. </p>

CC BY-SA 3.0
2013-12-10T18:17:08.923Z
Thanks for the answer, I will definitely look at temporal logic! I would like to point out that `Future[T]` in Scala accepts a callback of type `Try[T] -> Unit`. The callback is registered using the method `Future[T].onComplete()`. I.e. when the future completes successfully or with an error, it calls all registered callback with the resulting `Try[T]` value. So can it be that the Scala interpretation of futures corresponds to $\diamond (T + E)$ instead of $\diamond T$?
Giorgio
CC BY-SA 3.0
2013-12-11T15:28:21.050Z
@Giorgio: Yes, that's probably right -- I didn't consider exceptions in my answer.
Neel Krishnaswami
CC BY-SA 3.0
2013-12-10T10:00:35.193Z
2013-12-10T10:00:35.193Z
Neel Krishnaswami
<p>In a recent course based on Scala I found a hint that the Scala types <code>Try[T]</code> and <code>Future[T]</code> are <strong>dual</strong>. This was explained only informally and the reader was referred to duality in category theory for a more precise definition.</p>

<p>I have some basic knowledge of category theory but I cannot see how these two types can correspond to categories that are dual to each other.</p>

<p>So, what categories correspond to the Scala types <code>Try[T]</code> and <code>Future[T]</code>, and why are these categories dual?</p>

CC BY-SA 3.0
2013-12-06T18:23:25.627Z
Duality doesn't necessarily mean "there is a category in which these concepts are _categorically_ dual. It can be a bit more general than that.
cody
CC BY-SA 3.0
2013-12-07T09:03:45.813Z
@cody: I gave this interpretation because the author mentioned duality in category theory (and provided a link to a wikipedia page http://en.wikipedia.org/wiki/Dual_%28category_theory%29). Of course, my interpretation can be wrong / not general enough.
Giorgio
CC BY-SA 3.0
2013-12-07T09:16:25.767Z
@Giorgio In an informal sense! Scala and its typing system are quite complex and, as far as I'm aware, not currently known to correspond to any particular category. Scala's futures are to do with concurrency, and concurrency doesn't not fit well with categories.
Martin Berger
CC BY-SA 3.0
2015-08-11T20:57:48.597Z
I was confused by this part of the coursera class as well.
Chris Bedford
<pl.programming-languages><type-theory><ct.category-theory>
In what sense are Scala's Try[T] and Future[T] dual?
Giorgio
# In what sense are Scala's Try[T] and Future[T] dual?

(Asked by: username_0 on 2013-12-06)

In a recent course based on Scala I found a hint that the Scala types `Try[T]` and `Future[T]` are **dual**. This was explained only informally and the reader was referred to duality in category theory for a more precise definition.

I have some basic knowledge of category theory but I cannot see how these two types can correspond to categories that are dual to each other.

So, what categories correspond to the Scala types `Try[T]` and `Future[T]`, and why are these categories dual?

**Tags:** pl.programming-languages, type-theory, ct.category-theory
> **Comments:**
> **username_1**: Duality doesn't necessarily mean "there is a category in which these concepts are _categorically_ dual. It can be a bit more general than that.  
> **username_0**: @cody: I gave this interpretation because the author mentioned duality in category theory (and provided a link to a wikipedia page http://en.wikipedia.org/wiki/Dual_%28category_theory%29). Of course, my interpretation can be wrong / not general enough.  
> **username_2**: @Giorgio In an informal sense! Scala and its typing system are quite complex and, as far as I'm aware, not currently known to correspond to any particular category. Scala's futures are to do with concurrency, and concurrency doesn't not fit well with categories.  
> **username_3**: I was confused by this part of the coursera class as well.  

---

## 1 answers

### Answer by username_4 on 2013-12-10. Score: 10

The short answer is: they are not dual.

The `Try[T]` type is basically the sum type $T + E$, where $E$ is the type of exceptions.

A typical way of rendering a future `Future[T]` is via *temporal logic*. Here, a future can be understood as a "weak eventually" operator, $\Diamond T$, saying that at some point in the future you may receive a $T$. This can be decomposed as the coinductive type $\nu \alpha.\;T + \bullet\alpha$, where $\bullet$ is the "next-step" modality.

> **Comments:**
> **username_0**: Thanks for the answer, I will definitely look at temporal logic! I would like to point out that `Future[T]` in Scala accepts a callback of type `Try[T] -> Unit`. The callback is registered using the method `Future[T].onComplete()`. I.e. when the future completes successfully or with an error, it calls all registered callback with the resulting `Try[T]` value. So can it be that the Scala interpretation of futures corresponds to $\diamond (T + E)$ instead of $\diamond T$?  
> **username_4**: @Giorgio: Yes, that's probably right -- I didn't consider exceptions in my answer.  

---

