2

Through Curry-Howard isomorphism Scala's Unit corresponds to logical true and Nothing to logical false. The fact that logical true is implied by anything is witnessed by a simple function that just discards the argument:

def toUnit[A](x: A): Unit = { }

Is there a function that witnesses the fact that logical false implies anything, that is a function of type Nothing => A? Or is there an idiomatic way how to construct one?

One can always do something like

def fromNothing[A](n: Nothing): A = throw new RuntimeException();

but this is just ugly - it doesn't use the fact that Nothing has no values. There should be a way how to do it without exceptions.

Petr
  • 62,528
  • 13
  • 153
  • 317
  • 2
    Shouldn't the top type (i.e. Any) be true ? – Jamil Nov 24 '12 at 09:56
  • @Jamil Yes, we could take `Any` to be logical true as well. From the CH perspective, they're equivalent, because we can easily construct witnessing functions `(_ => ()) : Any => Unit` and `identity : Unit => Any`. – Petr Nov 24 '12 at 11:07
  • @Jamil In CH correspondence we don't compare two types `A` and `B` according to type hierarchy, we compare them according to the existence of a function of type `A => B`. So if we didn't have type hierarchy (like in Haskell) then still any empty data type would correspond to _false_ and any one-element data type would correspond to _true_. If we have a type hierarchy and `A` is a subtype of `B` then we have `identity: A => B` so 'A' corresponds to a stronger proposition than `B`. But we can have `f: A => B` even though `A` is not a subtype of `B`. – Petr Nov 24 '12 at 13:16

1 Answers1

8

You may do that

def emptyFunction[A]: Nothing => A = {n => n}

or

def emptyFunction[A](n: Nothing): A = n
Didier Dupont
  • 29,398
  • 7
  • 71
  • 90
  • Oh very nice. I didn't realize that as `Nothing` is a subtype of anything I can simply use it in place of `A`. – Petr Nov 24 '12 at 10:54