Skip to content

Commit

Permalink
Update for polynomial lib renaming: e.g., Store, BiStore, TriStore
Browse files Browse the repository at this point in the history
  • Loading branch information
julianpeeters committed Feb 10, 2024
1 parent 27f7871 commit 9a70d4a
Show file tree
Hide file tree
Showing 26 changed files with 512 additions and 495 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import polynomial.`object`.Mono
import polynomial.morphism.~>
import dynamical.fsm.Moore

type Fsm[S, A, B] = Moore[Mono.Store[S, _] ~> Mono.Interface[A, B, _]]
type Fsm[S, A, B] = Moore[Store[S, _] ~> Interface[A, B, _]]
```

However, in order to run them as a function `(S, A) => (S, B)`, we need the
Expand All @@ -34,7 +34,7 @@ import dynamical.fsm.Moore
import polynomial.morphism.~>
import polynomial.`object`.Mono

def mealified[Y]: Moore[Mono.Store[Boolean, _] ~> Mono.Interface[Int, Int => Int, _]] =
def mealified[Y]: Moore[Store[Boolean, _] ~> Interface[Int, Int => Int, _]] =
Moore[Boolean, Int, Int => Int, Y](
false,
s => x => if s then x + x else x, // if we've seen x > 1, then emit 2x
Expand All @@ -59,13 +59,13 @@ import dynamical.fsm.{Moore, Mealy}
import polynomial.morphism.~>
import polynomial.`object`.Mono

def moore[Y]: Moore[Mono.Store[Boolean, _] ~> Mono.Interface[Int, Int => Int, _]] =
def moore[Y]: Moore[Store[Boolean, _] ~> Interface[Int, Int => Int, _]] =
Moore[Boolean, Int, Int => Int, Y](
false,
s => x => if s then x + x else x, // if we've seen x > 1, then emit 2x
(s, x) => if x > 1 then true else s // change state upon seeing x > 1
)
val m: Mealy[Mono.Store[Boolean, _] ~> Mono.Interface[Int, Int => Int, _]] = moore.asMealy
val m: Mealy[Store[Boolean, _] ~> Interface[Int, Int => Int, _]] = moore.asMealy
val l: List[Int] = List(1, 2, 3).mapAccumulate(m.init)(m.run)._2
// l: List[Int] = List(1, 2, 6)
```
Expand All @@ -92,13 +92,13 @@ There are several aspects to the composition of state systems with wiring diagra
import cats.implicits.given
import dynamical.fsm.{Mealy, Moore, Wiring}
import polynomial.`object`.Mono
import polynomial.`object`.Mono.Store
import polynomial.`object`.Store
import polynomial.morphism.~>
import polynomial.product.

type Plant[Y] = Mono.Interface[(Byte, Byte => Char), Char, Y]
type Controller[Y] = Mono.Interface[Char, Byte => Char, Y]
type System[Y] = Mono.Interface[Byte, Byte => Char, Y]
type Plant[Y] = Interface[(Byte, Byte => Char), Char, Y]
type Controller[Y] = Interface[Char, Byte => Char, Y]
type System[Y] = Interface[Byte, Byte => Char, Y]
type ω[Y] = ((PlantController) ~> System)[Y]

val w: Wiring[ω] = Wiring(
Expand Down Expand Up @@ -147,7 +147,7 @@ import fs2.Stream
import polynomial.morphism.~>
import polynomial.`object`.Mono

val m: Mealy[Mono.Store[Boolean, _] ~> Mono.Interface[Int, Int => Int, _]] = Mealy(false, s => i => i + i, (s, i) => s)
val m: Mealy[Store[Boolean, _] ~> Interface[Int, Int => Int, _]] = Mealy(false, s => i => i + i, (s, i) => s)
// m: Mealy[~>[[_$9 >: Nothing <: Any] => Store[Boolean, _$9], [_$10 >: Nothing <: Any] => Interface[Int, Function1[Int, Int], _$10]]] = dynamical.fsm.methods.polymap.asMealy$$anon$1@3b4b6f8f

val l: List[Int] = Stream(1, 2, 3).through(m.transducer).compile.toList
Expand Down
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ val CatsV = "2.10.0"
val DestructuredV = "0.2.0"
val Fs2V = "3.9.3"
val MUnitV = "0.7.29"
val PolynomialV = "0.4.0+9-4a9eba9b+20240203-1620-SNAPSHOT"
val PolynomialV = "0.4.0+13-83c271ca+20240209-1837-SNAPSHOT"

inThisBuild(List(
crossScalaVersions := Seq(scalaVersion.value),
Expand Down
18 changes: 9 additions & 9 deletions docs/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import polynomial.`object`.Mono
import polynomial.morphism.~>
import dynamical.fsm.Moore

type Fsm[S, A, B] = Moore[Mono.Store[S, _] ~> Mono.Interface[A, B, _]]
type Fsm[S, A, B] = Moore[Store[S, _] ~> Interface[A, B, _]]
```

However, in order to run them as a function `(S, A) => (S, B)`, we need the
Expand All @@ -34,7 +34,7 @@ import dynamical.fsm.Moore
import polynomial.morphism.~>
import polynomial.`object`.Mono

def mealified[Y]: Moore[Mono.Store[Boolean, _] ~> Mono.Interface[Int, Int => Int, _]] =
def mealified[Y]: Moore[Store[Boolean, _] ~> Interface[Int, Int => Int, _]] =
Moore[Boolean, Int, Int => Int, Y](
false,
s => x => if s then x + x else x, // if we've seen x > 1, then emit 2x
Expand All @@ -58,13 +58,13 @@ import dynamical.fsm.{Moore, Mealy}
import polynomial.morphism.~>
import polynomial.`object`.Mono

def moore[Y]: Moore[Mono.Store[Boolean, _] ~> Mono.Interface[Int, Int => Int, _]] =
def moore[Y]: Moore[Store[Boolean, _] ~> Interface[Int, Int => Int, _]] =
Moore[Boolean, Int, Int => Int, Y](
false,
s => x => if s then x + x else x, // if we've seen x > 1, then emit 2x
(s, x) => if x > 1 then true else s // change state upon seeing x > 1
)
val m: Mealy[Mono.Store[Boolean, _] ~> Mono.Interface[Int, Int => Int, _]] = moore.asMealy
val m: Mealy[Store[Boolean, _] ~> Interface[Int, Int => Int, _]] = moore.asMealy
val l: List[Int] = List(1, 2, 3).mapAccumulate(m.init)(m.run)._2
// l: List[Int] = List(1, 2, 6)
```
Expand All @@ -91,13 +91,13 @@ There are several aspects to the composition of state systems with wiring diagra
import cats.implicits.given
import dynamical.fsm.{Mealy, Moore, Wiring}
import polynomial.`object`.Mono
import polynomial.`object`.Mono.Store
import polynomial.`object`.Store
import polynomial.morphism.~>
import polynomial.product.

type Plant[Y] = Mono.Interface[(Byte, Byte => Char), Char, Y]
type Controller[Y] = Mono.Interface[Char, Byte => Char, Y]
type System[Y] = Mono.Interface[Byte, Byte => Char, Y]
type Plant[Y] = Interface[(Byte, Byte => Char), Char, Y]
type Controller[Y] = Interface[Char, Byte => Char, Y]
type System[Y] = Interface[Byte, Byte => Char, Y]
type ω[Y] = ((PlantController) ~> System)[Y]

val w: Wiring[ω] = Wiring(
Expand Down Expand Up @@ -137,7 +137,7 @@ import fs2.Stream
import polynomial.morphism.~>
import polynomial.`object`.Mono

val m: Mealy[Mono.Store[Boolean, _] ~> Mono.Interface[Int, Int => Int, _]] = Mealy(false, s => i => i + i, (s, i) => s)
val m: Mealy[Store[Boolean, _] ~> Interface[Int, Int => Int, _]] = Mealy(false, s => i => i + i, (s, i) => s)

val l: List[Int] = Stream(1, 2, 3).through(m.transducer).compile.toList
```
2 changes: 1 addition & 1 deletion modules/fs2/jvm/src/test/scala/dynamical/fs2Suite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import polynomial.`object`.Mono
class fs2Suite extends FunSuite:

test("fs2"):
val m: Mealy[Mono.Store[Boolean, _] ~> Mono.Interface[Int, Int => Int, _]] = Mealy(false, s => i => i + i, (s, i) => s)
val m: Mealy[Store[Boolean, _] ~> Interface[Int, Int => Int, _]] = Mealy(false, s => i => i + i, (s, i) => s)
val obtained: List[Int] = Stream(1, 2, 3).through(m.transducer).compile.toList
val expected: List[Int] = Stream(2, 4, 6).compile.toList
assertEquals(obtained, expected)
7 changes: 4 additions & 3 deletions modules/fs2/shared/src/main/scala/dynamical/stream.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ import fs2.Pipe
import dynamical.fsm.Mealy
import dynamical.fsm.methods.types.Unify2
import polynomial.morphism.~>
import polynomial.`object`.{Bi, Mono}
import polynomial.`object`.Binomial.BiInterface
import polynomial.`object`.Monomial.{Interface, Store}

extension [S, A, B] (m: Mealy[Mono.Store[S, _] ~> Mono.Interface[A, A => B, _]])
extension [S, A, B] (m: Mealy[Store[S, _] ~> Interface[A, A => B, _]])
@scala.annotation.targetName("transducerStoreToMono")
def transducer[F[_], Y]: Pipe[F, A, B] =
stream =>
stream
.mapAccumulate(m.init)(m.run)
.map(_._2)

extension [S, A1, B1, A2, B2] (m: Mealy[Mono.Store[S, _] ~> Bi.Interface[A1, A1 => B1, A2, A2 => B2, _]])
extension [S, A1, B1, A2, B2] (m: Mealy[Store[S, _] ~> BiInterface[A1, A1 => B1, A2, A2 => B2, _]])
@scala.annotation.targetName("transducerStoreToBi")
def transducer[F[_], Y]: Pipe[F, Unify2[A1, A2], Unify2[B1, B2]] =
stream =>
Expand Down
11 changes: 6 additions & 5 deletions modules/fsm/jvm/src/test/scala/dynamical/BiSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import cats.implicits.given
import dynamical.fsm.{Mealy, Moore}
import munit.FunSuite
import polynomial.morphism.~>
import polynomial.`object`.{Bi, Mono}
import polynomial.`object`.Binomial.BiInterface
import polynomial.`object`.Monomial.{Interface, Store}

class BiSuite extends FunSuite:

test("moore"):
val m: Moore[
Mono.Store[Boolean, _] ~>
Bi.Interface[
Store[Boolean, _] ~>
BiInterface[
Some[Int], Some[Int] => Some[Int],
None.type, None.type => None.type,
_
Expand All @@ -28,8 +29,8 @@ class BiSuite extends FunSuite:

test("mealy"):
def m[Y]: Mealy[
Mono.Store[Boolean, _] ~>
Bi.Interface[
Store[Boolean, _] ~>
BiInterface[
Some[Int], Some[Int] => None.type,
None.type, None.type => Some[Int],
_
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import polynomial.product.◁
class CompositionSuite extends FunSuite:

test("mealy composition product"):
type P[Y] = (Mono.Store[Boolean, _] ~> (Mono.Interface[Int, Int, _] ◁ Mono.Interface[String, String => String, _]))[Y]
type P[Y] = (Store[Boolean, _] ~> (Interface[Int, Int, _] ◁ Interface[String, String => String, _]))[Y]
val n: Moore[P] =
Moore(
i = false,
Expand Down
30 changes: 15 additions & 15 deletions modules/fsm/jvm/src/test/scala/dynamical/MonoSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,75 +10,75 @@ import polynomial.`object`.Mono
class MonoSuite extends FunSuite:

test("moore"):
val m: Moore[Mono.Store[Boolean, _] ~> Mono.Interface[Int, Int => Int, _]] = Moore(false, s => i => i + i, (s, i) => s)
val m: Moore[Store[Boolean, _] ~> Interface[Int, Int => Int, _]] = Moore(false, s => i => i + i, (s, i) => s)
val obtained: List[Int] = List(1, 2, 3).mapAccumulate(m.init)((s, i) => (m.update(s, i), m.readout(s)(i)))._2
val expected: List[Int] = List(2, 4, 6)
assertEquals(obtained, expected)

test("mealy"):
val m: Mealy[Mono.Store[Boolean, _] ~> Mono.Interface[Int, Int => Int, _]] = Mealy(false, s => i => if s then i + 1 else i, (s, i) => if i > 1 then true else s)
val m: Mealy[Store[Boolean, _] ~> Interface[Int, Int => Int, _]] = Mealy(false, s => i => if s then i + 1 else i, (s, i) => if i > 1 then true else s)
val obtained: List[Int] = List(1, 2, 3).mapAccumulate(m.init)(m.run)._2
val expected: List[Int] = List(1, 2, 4)
assertEquals(obtained, expected)

test("wrap moore"):
val l: Moore[Mono.Store[Boolean, _] ~> Mono.Interface[Int, Int, _]] = Moore(false, s => if s then 1 else 0, (s, i) => s)
val m: Mealy[Mono.Store[Boolean, _] ~> Mono.Interface[Int, Int, _] ~> Mono.Interface[Int, Int => Int, _]] =
val l: Moore[Store[Boolean, _] ~> Interface[Int, Int, _]] = Moore(false, s => if s then 1 else 0, (s, i) => s)
val m: Mealy[Store[Boolean, _] ~> Interface[Int, Int, _] ~> Interface[Int, Int => Int, _]] =
l.andThen(Wiring(i => j => j + j, (i1, i2) => i2)).asMealy
val obtained: List[Int] = List(1, 2, 3).mapAccumulate(m.init)((s, i) => m.run(s, i))._2
val expected: List[Int] = List(2, 4, 6)
assertEquals(obtained, expected)

test("wrapper"):
def w[Y]: Wiring[Mono.Interface[Int, Int, _] ~> Mono.Interface[Int, Int => Int, _]] = Wiring[Id, Int, Int, Y](i => j => j + j, (i1, i2) => i2)
val l: Moore[Mono.Store[Boolean, _] ~> Mono.Interface[Int, Int, _]] = Moore(false, s => if s then 1 else 0, (s, i) => s)
val m: Mealy[Mono.Store[Boolean, _] ~> Mono.Interface[Int, Int, _] ~> Mono.Interface[Int, Int => Int, _]] = l.andThen(w).asMealy
def w[Y]: Wiring[Interface[Int, Int, _] ~> Interface[Int, Int => Int, _]] = Wiring[Id, Int, Int, Y](i => j => j + j, (i1, i2) => i2)
val l: Moore[Store[Boolean, _] ~> Interface[Int, Int, _]] = Moore(false, s => if s then 1 else 0, (s, i) => s)
val m: Mealy[Store[Boolean, _] ~> Interface[Int, Int, _] ~> Interface[Int, Int => Int, _]] = l.andThen(w).asMealy
val obtained: List[Int] = List(1, 2, 3).mapAccumulate(m.init)((s, i) => m.run(s, i))._2
val expected: List[Int] = List(2, 4, 6)
assertEquals(obtained, expected)

test("semi-choice"):
val m: Moore[Mono.Store[Option[Boolean], _] ~> Mono.Interface[Int, Int => Int, _]] =
val m: Moore[Store[Option[Boolean], _] ~> Interface[Int, Int => Int, _]] =
Moore(Some(true), s => i => s.fold(i)(b => if b then i + i else 10*i), (s, i) => if i > 1 then None else s.map(b => !b))
val obtained: List[Int] = List(1, 2, 3).mapAccumulate(m.init)((s, i) => (m.update(s, i), m.readout(s)(i)))._2
val expected: List[Int] = List(2, 20, 3)
assertEquals(obtained, expected)

test("wrapped semi-choice"):
def w[Y]: Wiring[Mono.Interface[Int, Int, _] ~> Mono.Interface[Int, Int => Int, _]] = Wiring[Id, Int, Int, Y](i => j => j + j, (i1, i2) => i2)
val n: Moore[Mono.Store[Option[Boolean], _] ~> Mono.Interface[Int, Int, _]] =
def w[Y]: Wiring[Interface[Int, Int, _] ~> Interface[Int, Int => Int, _]] = Wiring[Id, Int, Int, Y](i => j => j + j, (i1, i2) => i2)
val n: Moore[Store[Option[Boolean], _] ~> Interface[Int, Int, _]] =
Moore(Some(true), s => s.fold(0)(b => if b then 1 else 0), (s, i) => if i > 1 then None else s.map(b => !b))
val m = n.andThen(w)
val obtained: List[Int] = List(1, 2, 3).mapAccumulate(m.init)((s, i) => (m.update(s, i), m.readout(s)(i)))._2
val expected: List[Int] = List(2, 4, 6)
assertEquals(obtained, expected)

test("choice"):
val m: Moore[Mono.Store[Option[Boolean], _] ~> Mono.Interface[Int, Int => Option[Int], _]] =
val m: Moore[Store[Option[Boolean], _] ~> Interface[Int, Int => Option[Int], _]] =
Moore(Some(true), s => i => s.map(b => if b then i + i else 10*i), (s, i) => if i > 1 then None else s.map(b => !b))
val obtained: List[Option[Int]] = List(1, 2, 3).mapAccumulate(m.init)((s, i) => (m.update(s, i), m.readout(s)(i)))._2
val expected: List[Option[Int]] = List(Some(2), Some(20), None)
assertEquals(obtained, expected)

test("wrapped choice (prism composition)"):
val w: Wiring[Mono.Interface[Int, Int, _] ~> Mono.Interface[Int, Int => Option[Int], _]] =
val w: Wiring[Interface[Int, Int, _] ~> Interface[Int, Int => Option[Int], _]] =
Wiring(i => j => if j > 1 then Some(j + j) else Some(i), (i1, i2) => i2)
val n: Moore[Mono.Store[Option[Boolean], _] ~> Mono.Interface[Int, Option[Int], _]] =
val n: Moore[Store[Option[Boolean], _] ~> Interface[Int, Option[Int], _]] =
Moore(Some(true), s => s.map(b => if b then 1 else 0), (s, i) => if i > 1 then None else s.map(b => !b))
val m = n.andThen(w)
val obtained: List[Option[Int]] = List(1, 2, 3).mapAccumulate(m.init)((s, i) => (m.update(s, i), m.readout(s)(i)))._2
val expected: List[Option[Int]] = List(Some(1), Some(4), None)
assertEquals(obtained, expected)

test("semi-choice-strong"):
val m: Moore[Mono.Store[Option[(Int, Boolean)], _] ~> Mono.Interface[Long, Long => Long, _]] =
val m: Moore[Store[Option[(Int, Boolean)], _] ~> Interface[Long, Long => Long, _]] =
Moore(Some(0, true), s => i => s.fold(i)(b => if b._2 then i + i else 10*i), (s, i) => if i > 1 then None else s.map(b => b.copy(_2 = false)))
val obtained: List[Long] = List(1L, 2L, 3L).mapAccumulate(m.init)((s, i) => (m.update(s, i), m.readout(s)(i)))._2
val expected: List[Long] = List(2L, 20L, 3L)
assertEquals(obtained, expected)

test("choice-strong"):
val m: Moore[Mono.Store[Option[(Int, Boolean)], _] ~> Mono.Interface[Long, Long => Option[Long], _]] =
val m: Moore[Store[Option[(Int, Boolean)], _] ~> Interface[Long, Long => Option[Long], _]] =
Moore(Some((0, true)), s => i => s.map(b => if b._2 then i + i else 10*i), (s, i) => if i > 1 then None else s.map(b => b.copy(_2 = false)))
val obtained: List[Option[Long]] = List(1L, 2L, 3L).mapAccumulate(m.init)((s, i) => (m.update(s, i), m.readout(s)(i)))._2
val expected: List[Option[Long]] = List(Some(2L), Some(20L), None)
Expand Down

0 comments on commit 9a70d4a

Please sign in to comment.