-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from julianpeeters/dev
Add StoreF and basic monomial Cartesian products
- Loading branch information
Showing
16 changed files
with
655 additions
and
79 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
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,74 @@ | ||
package dynamical | ||
|
||
import cats.effect.IO | ||
import cats.effect.kernel.Ref | ||
import dynamical.fsm.{Mealy, Moore, Wiring} | ||
import dynamical.stream.transducer | ||
import fs2.Stream | ||
import munit.CatsEffectSuite | ||
import polynomial.morphism.~> | ||
import polynomial.`object`.Monomial.{Interface, StoreF} | ||
|
||
class RefSuite extends CatsEffectSuite: | ||
|
||
test("ref"): | ||
val moore: IO[Moore[StoreF[IO, Ref[IO, Boolean], _] ~> Interface[Int, IO[Int], _]]] = | ||
Ref.of[IO, Boolean](false).map(ref => | ||
Moore( | ||
ref, | ||
s => s.get.map(b => if b then 1 else 0), | ||
(s, i) => s.update(b => b).as(s) | ||
) | ||
) | ||
val mealy: IO[Mealy[(StoreF[IO, Ref[IO, Boolean], _] ~> Interface[Int, IO[Int], _]) ~> Interface[Int, Int => IO[Int], _]]] = | ||
moore.map(moore => moore.andThen(Wiring(i => j => i.as(j + j), (i1, i2) => i2)).asMealy) | ||
val obtained: IO[List[Int]] = | ||
for | ||
m <- mealy | ||
r <- Stream(1, 2, 3).covary[IO].through(m.transducer).compile.toList | ||
yield r | ||
val expected: List[Int] = List(2, 4, 6) | ||
assertIO(obtained, expected) | ||
|
||
test("broadcast ref"): | ||
def moore(ref: Ref[IO, Boolean]): Moore[StoreF[IO, Ref[IO, Boolean], _] ~> Interface[Int, IO[Int], _]] = | ||
Moore( | ||
ref, | ||
s => s.get.map(b => if b then 1 else 0), | ||
(s, i) => s.update(b => b).as(s) | ||
) | ||
def mealy( | ||
m: Moore[StoreF[IO, Ref[IO, Boolean], _] ~> Interface[Int, IO[Int], _]] | ||
): Mealy[(StoreF[IO, Ref[IO, Boolean], _] ~> Interface[Int, IO[Int], _]) ~> Interface[Int, Int => IO[Int], _]] = | ||
m.andThen(Wiring(i => j => i.as(j + j), (i1, i2) => i2)).asMealy | ||
val obtained: IO[List[Int]] = | ||
for | ||
ref <- Ref.of[IO, Boolean](false) | ||
m1 = mealy(moore(ref)) | ||
m2 = mealy(moore(ref)) | ||
m3 = mealy(moore(ref)) | ||
r <- Stream(1, 2, 3).covary[IO].broadcastThrough(m1.transducer, m2.transducer, m3.transducer).compile.toList | ||
yield r.sorted | ||
val expected: List[Int] = List(2, 2, 2, 4, 4, 4, 6, 6, 6) | ||
assertIO(obtained, expected) | ||
|
||
test("merge and sort"): | ||
def moore(ref: Ref[IO, Boolean]): Moore[StoreF[IO, Ref[IO, Boolean], _] ~> Interface[Int, IO[Int], _]] = | ||
Moore( | ||
ref, | ||
s => s.get.map(b => if b then 1 else 0), | ||
(s, i) => s.update(b => b).as(s) | ||
) | ||
def mealy( | ||
m: Moore[StoreF[IO, Ref[IO, Boolean], _] ~> Interface[Int, IO[Int], _]] | ||
): Mealy[(StoreF[IO, Ref[IO, Boolean], _] ~> Interface[Int, IO[Int], _]) ~> Interface[Int, Int => IO[Int], _]] = | ||
m.andThen(Wiring(i => j => i.as(j + j), (i1, i2) => i2)).asMealy | ||
val obtained: IO[List[Int]] = | ||
for | ||
ref <- Ref.of[IO, Boolean](false) | ||
m1 = mealy(moore(ref)) | ||
m2 = mealy(moore(ref)) | ||
r <- Stream(1, 2, 3).covary[IO].through(m1.transducer).merge(Stream(11, 22, 33).covary[IO].through(m2.transducer)).compile.toList | ||
yield r.sorted | ||
val expected: List[Int] = List(2, 4, 6, 22, 44, 66) | ||
assertIO(obtained, expected) |
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
109 changes: 109 additions & 0 deletions
109
modules/fsm/jvm/src/test/scala/dynamical/CartesianSuite.scala
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,109 @@ | ||
package dynamical | ||
|
||
import cats.implicits.given | ||
import dynamical.fsm.{Mealy, Moore, Wiring} | ||
import munit.FunSuite | ||
import polynomial.morphism.~> | ||
import polynomial.`object`.Monomial.{Interface, Store} | ||
import polynomial.product.{×, ⊗} | ||
|
||
class CartesianSuite extends FunSuite: | ||
|
||
test("mealy cartesian product"): | ||
|
||
val n: Moore[(Store[Boolean, _] ~> (Interface[Int, Int, _] × Interface[String, String, _]))] = | ||
Moore( | ||
i = false, | ||
r = (s: Boolean) => (if s then 1 else 0, "hello"), | ||
u = (s: Boolean, a: Either[Int, String]) => a match | ||
case Left(value) => !s | ||
case Right(value) => s | ||
, | ||
) | ||
val w: Wiring[(Interface[Int, Int, _] × Interface[String, String, _]) ~> Interface[Either[Int, String], Either[Int, String] => (Int, String), _]] = | ||
Wiring( | ||
r = b => a => b, | ||
u = (b, a) => a | ||
) | ||
val m: Mealy[Store[Boolean, _] ~> (Interface[Int, Int, _] × Interface[String, String, _]) ~> Interface[Either[Int, String], Either[Int, String] => (Int, String), _]] = | ||
n.andThen(w).asMealy | ||
val obtained: List[(Int, String)] = List(Right("foo"), Left(2), Right("bar")).mapAccumulate(m.init)(m.run)._2 | ||
val expected: List[(Int, String)] = List((0, "hello"), (0, "hello"), (1, "hello")) | ||
assertEquals(obtained, expected) | ||
|
||
test("mealy cartesian tensored"): | ||
val n: Moore[Store[String, _] ~> ((Interface[Int, Int, _] × Interface[String, String, _]) ⊗ Interface[Boolean, Boolean, _])] = | ||
Moore( | ||
i = "", | ||
r = s => ((s.length, s), s.length >= 2), | ||
u = (s, a) => if a._2 then s else a._1 match | ||
case Left(value) => value.toString | ||
case Right(value) => s + value, | ||
) | ||
val w: Wiring[ | ||
((Interface[Int, Int, _] × Interface[String, String, _]) ⊗ Interface[Boolean, Boolean, _]) ~> | ||
Interface[((Either[Int, String], Boolean)), ((Either[Int, String], Boolean)) => ((Int, String), Boolean), _] | ||
] = | ||
Wiring( | ||
r = b => a => b, | ||
(b, a) => a | ||
) | ||
val m: Mealy[Store[String, _] ~> ((Interface[Int, Int, _] × Interface[String, String, _]) ⊗ Interface[Boolean, Boolean, _]) ~> Interface[((Either[Int, String], Boolean)), ((Either[Int, String], Boolean)) => ((Int, String), Boolean), _]] = | ||
n.andThen(w).asMealy | ||
val obtained: List[((Int, String), Boolean)] = List((Right("foo"), true), (Left(2), false), (Right("bar"), true)).mapAccumulate(m.init)(m.run)._2 | ||
val expected: List[((Int, String), Boolean)] = List(((0, ""), false), ((0, ""), false), ((1, "2"), false)) | ||
assertEquals(obtained, expected) | ||
|
||
test("mealy tensored cartesian"): | ||
val n: Moore[Store[String, _] ~> (Interface[Int, Int, _] × (Interface[String, String, _] ⊗ Interface[Boolean, Boolean, _]))] = | ||
Moore( | ||
i = "", | ||
r = s => (s.length, (s, s.length > 3)), | ||
u = (s, a) => a match | ||
case Left(value) => s + "!" | ||
case Right(value) => s + value._1 | ||
) | ||
val w: Wiring[ | ||
(Interface[Int, Int, _] × (Interface[String, String, _] ⊗ Interface[Boolean, Boolean, _])) ~> | ||
Interface[Either[Int, (String, Boolean)], Either[Int, (String, Boolean)] => (Int, (String, Boolean)), _] | ||
] = | ||
Wiring( | ||
r = b => a => b, | ||
(b, a) => a | ||
) | ||
val m: Mealy[ | ||
Store[String, _] ~> | ||
(Interface[Int, Int, _] × (Interface[String, String, _] ⊗ Interface[Boolean, Boolean, _])) ~> | ||
Interface[Either[Int, (String, Boolean)], Either[Int, (String, Boolean)] => (Int, (String, Boolean)), _] | ||
] = | ||
n.andThen(w).asMealy | ||
val obtained: List[(Int, (String, Boolean))] = List(Right(("foo", true)), Left(2), Right(("bar", false))).mapAccumulate(m.init)(m.run)._2 | ||
val expected: List[(Int, (String, Boolean))] = List((0, ("", false)), (3, ("foo", false)), (4, ("foo!", true))) | ||
assertEquals(obtained, expected) | ||
|
||
test("mealy linearly distributive"): | ||
type P[Y] = Interface[Int, Double, Y] | ||
type Q[Y] = Interface[String, Char, Y] | ||
type R[Y] = Interface[Boolean, Unit, Y] | ||
type S[Y] = Store[String, Y] | ||
type T[Y] = Interface[Either[Int, (String, Boolean)], Either[Int, (String, Boolean)] => (Double, (Char, Unit)), Y] | ||
val n: Moore[S ~> ((P × Q) ⊗ R)] = | ||
Moore( | ||
i = "", | ||
r = s => ((s.length, s.headOption.getOrElse('!')), ()), | ||
u = (s, a) => if a._2 then s else a._1 match | ||
case Left(value) => value.toString | ||
case Right(value) => s + value, | ||
) | ||
val w: Wiring[((P × Q) ⊗ R) ~> (P × (Q ⊗ R))] = | ||
Wiring( | ||
b => (b._1._1, (b._1._2, b._2)), // re-tuple, | ||
(b, a) => a match // exhibit strength: | ||
case Left(value) => (Left(value), value < 1) | ||
case Right(value) => (Right(b._1._2+:value._1), value._2) | ||
) | ||
val z: Wiring[(P × (Q ⊗ R)) ~> T] = Wiring(b => a => b, (b, a) => a) | ||
val m: Mealy[((S ~> ((P × Q) ⊗ R)) ~> (P × (Q ⊗ R))) ~> T] = n.andThen(w).andThen(z).asMealy | ||
val obtained: List[(Double, (Char, Unit))] = List(Right(("foo", true)), Left(2), Right(("bar", false))).mapAccumulate(m.init)(m.run)._2 | ||
val expected: List[(Double, (Char, Unit))] = List((0, ('!', ())), (0, ('!', ())), (1, ('2', ()))) | ||
assertEquals(obtained, expected) |
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
Oops, something went wrong.