Skip to content

Commit 8a1492b

Browse files
committed
despecialize epoch implementation to use Sum Type code path
1 parent d8380df commit 8a1492b

File tree

2 files changed

+25
-25
lines changed

2 files changed

+25
-25
lines changed

Modules/RDTs/src/main/scala/rdts/base/Lattice.scala

+17-11
Original file line numberDiff line numberDiff line change
@@ -154,33 +154,39 @@ object Lattice {
154154
* For an `enum E { case A, B, C }` it will be `A < B < C` */
155155
inline def sumLattice[T](using sm: Mirror.SumOf[T]): Lattice[T] =
156156
val lattices: Tuple = summonAll[Tuple.Map[sm.MirroredElemTypes, Lattice]]
157-
new Derivation.SumLattice[T](sm, lattices)
157+
new Derivation.SumLattice[T](Derivation.MirrorOrdinal(sm, lattices))
158158

159159
inline def productLattice[T <: Product](using pm: Mirror.ProductOf[T]): Lattice[T] = {
160160
val lattices: Tuple = summonAll[Tuple.Map[pm.MirroredElemTypes, Lattice]]
161161
val bottoms: Tuple = Derivation.summonAllMaybe[Tuple.Map[pm.MirroredElemTypes, Bottom]]
162162
new Derivation.ProductLattice[T](lattices, bottoms, pm, valueOf[pm.MirroredLabel])
163163
}
164164

165+
trait OrdinalLattices[T] {
166+
def compare(left: T, right: T): Int
167+
def lattice(elem: T): Lattice[T]
168+
}
169+
170+
165171
object Derivation {
166172

167-
class SumLattice[T](sm: Mirror.SumOf[T], lattices: Tuple) extends Lattice[T] {
173+
case class MirrorOrdinal[T](sm: Mirror.SumOf[T], lattices: Tuple) extends OrdinalLattices[T] {
174+
def compare(left: T, right: T): Int = Integer.compare(sm.ordinal(left), sm.ordinal(right))
175+
override def lattice(elem: T): Lattice[T] = lattices.productElement(sm.ordinal(elem)).asInstanceOf[Lattice[T]]
176+
}
177+
168178

169-
private def lat(i: Int): Lattice[T] = lattices.productElement(i).asInstanceOf[Lattice[T]]
179+
class SumLattice[T](ol: OrdinalLattices[T]) extends Lattice[T] {
170180

171181
def merge(left: T, right: T): T =
172-
val lo = sm.ordinal(left)
173-
val ro = sm.ordinal(right)
174-
Integer.compare(lo, ro) match
175-
case 0 => lat(lo).merge(left, right)
182+
ol.compare(left, right) match
183+
case 0 => ol.lattice(left).merge(left, right)
176184
case x if x < 0 => right
177185
case x if x > 0 => left
178186

179187
override def subsumption(left: T, right: T): Boolean =
180-
val lo = sm.ordinal(left)
181-
val ro = sm.ordinal(right)
182-
Integer.compare(lo, ro) match
183-
case 0 => lat(lo).subsumption(left, right)
188+
ol.compare(left, right) match
189+
case 0 => ol.lattice(left).subsumption(left, right)
184190
case other => other < 0
185191
}
186192

Modules/RDTs/src/main/scala/rdts/datatypes/Epoch.scala

+8-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package rdts.datatypes
22

3+
import rdts.base.Lattice.OrdinalLattices
34
import rdts.base.{Bottom, Decompose, Lattice}
45
import rdts.dotted.HasDots
56
import rdts.time.{Dots, Time}
@@ -33,19 +34,12 @@ object Epoch {
3334
given decomposeInstance[E: Decompose]: Decompose[Epoch[E]] =
3435
case Epoch(c, v) => Decompose.decompose(v).map(Epoch(c, _))
3536

36-
given latticeInstance[E: Lattice as E]: Lattice[Epoch[E]] = new Lattice[Epoch[E]] {
37-
38-
override def subsumption(left: Epoch[E], right: Epoch[E]): Boolean = (left, right) match {
39-
case (Epoch(cLeft, vLeft), Epoch(cRight, vRight)) =>
40-
cLeft < cRight || (cLeft == cRight && E.subsumption(vLeft, vRight))
41-
}
42-
43-
/** By assumption: associative, commutative, idempotent. */
44-
override def merge(left: Epoch[E], right: Epoch[E]): Epoch[E] = (left, right) match {
45-
case (Epoch(cLeft, vLeft), Epoch(cRight, vRight)) =>
46-
if cLeft > cRight then left
47-
else if cRight > cLeft then right
48-
else Epoch(cLeft, E.merge(vLeft, vRight))
49-
}
37+
given latticeInstance[E: Lattice as E]: Lattice[Epoch[E]] = {
38+
given Lattice[Time] = Lattice.assertEquals
39+
val prodEpoche = Lattice.productLattice[Epoch[E]]
40+
Lattice.Derivation.SumLattice[Epoch[E]](new OrdinalLattices[Epoch[E]] {
41+
override def compare(left: Epoch[E], right: Epoch[E]): Int = java.lang.Long.compare(left.counter, right.counter)
42+
override def lattice(elem: Epoch[E]): Lattice[Epoch[E]] = prodEpoche
43+
})
5044
}
5145
}

0 commit comments

Comments
 (0)