[Question] Implementing <!:< #15447
Replies: 2 comments 2 replies
-
I don't think that's possible using match types, in fact the definition of -- Error: try/negst.scala:10:14 ------------------------------------------------
10 | type Map[-X, +Y] = Arr[X, Y] match {
| ^^
|contravariant type parameter X occurs in invariant position in Arr[X, Y] match {
| case Arr[String, Int] => Unit
| case Arr[Int, String] => Nothing
|} The scrutinee of the match is considered to be an invariant position (I don't recall why, but it was probably unsound otherwise). To implement what you want it seems that the language would need to expose the |
Beta Was this translation helpful? Give feedback.
-
Solved! https://scastie.scala-lang.org/O4YhUozxSnO2PdXGKwivwQ compiles in both Scala 2 and Scala 3! Next step is figuring out how to generate the implementations with a macro... |
Beta Was this translation helpful? Give feedback.
-
Prologue
One can define
=!:=
like so:And by defining an invariant type-level
Tuple2
:can implement it like so:
Chapter 1: Subtyping Rules
=!:=
is invariant on both sides.<!:<
should be covariant on the subtype side: IfA <: C
, then(A <!:< B) <: (C <!:< B)
. That is to say, if you have a proof thatA <!:< B
, andA <: C
, then it must not be thatC <: B
due to subtyping transitivity.<!:<
should be contravariant on the supertype side: IfB >: D
, then(A <!:< B) <: (A <!:< D)
. That is to say, if you have a proof thatA <!:< B
, andD <: B
, then it must not be thatA <: D
due to subtyping transitivity.(A <!:< B) <: (A =!:= B)
: If two types are not subtypes, they are also not equal, due to subtyping reflexivity.Immediately we run into some issues:
And if we even decide to discard variance, making the
impossible
method more general doesn't work anyways:Chapter 2: Mapping Sides
Say we ditch the
<!:<
-implies-=!:=
:I could find no such solution that allows one to map the
proof
to get aUnit <:< Nothing
when the types are truly distinct.Replacing the spirit of
InvT
with any ofalways leaves one with errors. For example, with
Arr
:How should one implement
<!:<
?Beta Was this translation helpful? Give feedback.
All reactions