7

There seems to be a somewhat widespread conception that Scala types form a lattice.

I can get three quarters of the way there, and define a semilattice by defining top = Any, bottom = Nothing, and meet = with. Then is at least mostly equivalent to<:, ie the subtyping relation.

Scala is even kind enough to help us test at least some of the lattice laws:

scala> implicitly[Nothing =:= Nothing]
res0: =:=[Nothing,Nothing] = <function1>

scala> implicitly[Nothing =:= String with Nothing]
res1: =:=[Nothing,String with Nothing] = <function1>

scala> implicitly[Nothing =:= String with Any]
<console>:11: error: Cannot prove that Nothing =:= String with Any.
       implicitly[Nothing =:= String with Any]
                 ^

scala> implicitly[Any =:= String with Any]
<console>:11: error: Cannot prove that Any =:= String with Any.
       implicitly[Any =:= String with Any]
                 ^

scala> implicitly[Any =:= Any]
res4: =:=[Any,Any] = <function1>

scala> implicitly[Any =:= Any with String]
<console>:11: error: Cannot prove that Any =:= Any with String.
       implicitly[Any =:= Any with String]
                 ^

scala> implicitly[String =:= Any with String]
res6: =:=[String,Any with String] = <function1>

scala> implicitly[String =:= String with Any]
res7: =:=[String,String with Any] = <function1>

scala> implicitly[String =:= (String with Any)]
res8: =:=[String,String with Any] = <function1>

scala> implicitly[Int with String =:= String with Int]
res9: =:=[Int with String,String with Int] = <function1>

scala> implicitly[Boolean with (Int with String) =:= (Boolean with Int) with (Boolean with String)]                            
res11: =:=[Boolean with Int with String,Boolean with Int with Boolean with String] = <function1>

So, now all that is missing is a suitable definition of join. Is there an equivalent to join in Scala?

You need not use my above definitions for top, bottom, and meet, but it is preferred as this is more than just a theoretical question. I actually want to use the join of two types. As a workaround for now, since I don't know how to write A join B, I'll define a new trait with abstract methods for the methods in both A and B, and modify A and B to inherit from it.

aij
  • 5,903
  • 3
  • 37
  • 41
  • 4
    I don't think so. You might be able to define a lattice using this [brilliant hack from miles sabin](http://milessabin.com/blog/2011/06/09/scala-union-types-curry-howard/), but for a straightforward usage you would have to wait for [dotty](https://github.com/lampepfl/dotty) – Rüdiger Klaehn Jan 26 '16 at 15:54

0 Answers0