5
$\begingroup$

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?

$\endgroup$
  • 1
    $\begingroup$ 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. $\endgroup$ – cody Dec 6 '13 at 18:23
  • $\begingroup$ @cody: I gave this interpretation because the author mentioned duality in category theory (and provided a link to a wikipedia page en.wikipedia.org/wiki/Dual_%28category_theory%29). Of course, my interpretation can be wrong / not general enough. $\endgroup$ – Giorgio Dec 7 '13 at 9:03
  • $\begingroup$ @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. $\endgroup$ – Martin Berger Dec 7 '13 at 9:16
  • $\begingroup$ I was confused by this part of the coursera class as well. $\endgroup$ – Chris Bedford Aug 11 '15 at 20:57
10
$\begingroup$

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.

$\endgroup$
  • 2
    $\begingroup$ 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$? $\endgroup$ – Giorgio Dec 10 '13 at 18:17
  • $\begingroup$ @Giorgio: Yes, that's probably right -- I didn't consider exceptions in my answer. $\endgroup$ – Neel Krishnaswami Dec 11 '13 at 15:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.