-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle higher-kinded comparisons involving GADTs
Higher-kinded comparisons did not account for the fact that a type constructor in a higher-kinded application could have a narrowed GADT bound.
- Loading branch information
Showing
3 changed files
with
122 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import scala.reflect.ClassTag | ||
object tagging { | ||
|
||
// Tagged[S, T] means that S is tagged with T | ||
opaque type Tagged[X, Y] = X | ||
|
||
object Tagged { | ||
def tag[S, T](s: S): Tagged[S, T] = (s: S) | ||
def untag[S, T](st: Tagged[S, T]): S = st | ||
|
||
def tags[F[_], S, T](fs: F[S]): F[Tagged[S, T]] = fs | ||
def untags[F[_], S, T](fst: F[Tagged[S, T]]): F[S] = fst | ||
|
||
implicit def taggedClassTag[S, T](implicit ct: ClassTag[S]): ClassTag[Tagged[S, T]] = | ||
ct | ||
} | ||
|
||
import Tagged._ | ||
|
||
type @@[S, T] = Tagged[S, T] | ||
|
||
implicit class UntagOps[S, T](st: S @@ T) extends AnyVal { | ||
def untag: S = Tagged.untag(st) | ||
} | ||
|
||
implicit class UntagsOps[F[_], S, T](fs: F[S @@ T]) extends AnyVal { | ||
def untags: F[S] = Tagged.untags(fs) | ||
} | ||
|
||
implicit class TagOps[S](s: S) extends AnyVal { | ||
def tag[T]: S @@ T = Tagged.tag(s) | ||
} | ||
|
||
implicit class TagsOps[F[_], S](fs: F[S]) extends AnyVal { | ||
def tags[T]: F[S @@ T] = Tagged.tags(fs) | ||
} | ||
|
||
trait Meter | ||
trait Foot | ||
trait Fathom | ||
|
||
val x: Double @@ Meter = (1e7).tag[Meter] | ||
val y: Double @@ Foot = (123.0).tag[Foot] | ||
val xs: Array[Double @@ Meter] = Array(1.0, 2.0, 3.0).tags[Meter] | ||
|
||
val o: Ordering[Double] = implicitly | ||
val om: Ordering[Double @@ Meter] = o.tags[Meter] | ||
om.compare(x, x) // 0 | ||
om.compare(x, y) // error | ||
xs.min(om) // 1.0 | ||
xs.min(o) // error | ||
|
||
// uses ClassTag[Double] via 'Tagged.taggedClassTag'. | ||
val ys = new Array[Double @@ Foot](20) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import scala.reflect.ClassTag | ||
object tagging { | ||
|
||
// Tagged[S, T] means that S is tagged with T | ||
opaque type Tagged[X, Y] = X | ||
|
||
object Tagged { | ||
def tag[S, T](s: S): Tagged[S, T] = (s: S) | ||
def untag[S, T](st: Tagged[S, T]): S = st | ||
|
||
def tags[F[_], S, T](fs: F[S]): F[Tagged[S, T]] = fs | ||
def untags[F[_], S, T](fst: F[Tagged[S, T]]): F[S] = fst | ||
|
||
implicit def taggedClassTag[S, T](implicit ct: ClassTag[S]): ClassTag[Tagged[S, T]] = | ||
ct | ||
} | ||
|
||
import Tagged._ | ||
|
||
type @@[S, T] = Tagged[S, T] | ||
|
||
implicit class UntagOps[S, T](st: S @@ T) extends AnyVal { | ||
def untag: S = Tagged.untag(st) | ||
} | ||
|
||
implicit class UntagsOps[F[_], S, T](fs: F[S @@ T]) extends AnyVal { | ||
def untags: F[S] = Tagged.untags(fs) | ||
} | ||
|
||
implicit class TagOps[S](s: S) extends AnyVal { | ||
def tag[T]: S @@ T = Tagged.tag(s) | ||
} | ||
|
||
implicit class TagsOps[F[_], S](fs: F[S]) extends AnyVal { | ||
def tags[T]: F[S @@ T] = Tagged.tags(fs) | ||
} | ||
|
||
trait Meter | ||
trait Foot | ||
trait Fathom | ||
|
||
val x: Double @@ Meter = (1e7).tag[Meter] | ||
val y: Double @@ Foot = (123.0).tag[Foot] | ||
val xs: Array[Double @@ Meter] = Array(1.0, 2.0, 3.0).tags[Meter] | ||
|
||
val o: Ordering[Double] = implicitly | ||
val om: Ordering[Double @@ Meter] = o.tags[Meter] | ||
om.compare(x, x) // 0 | ||
// om.compare(x, y) // does not compile | ||
xs.min(om) // 1.0 | ||
// xs.min(o) // does not compile | ||
|
||
// uses ClassTag[Double] via 'Tagged.taggedClassTag'. | ||
val ys = new Array[Double @@ Foot](20) | ||
} |