Skip to content

Commit

Permalink
Renaming to reduce code footprint
Browse files Browse the repository at this point in the history
  • Loading branch information
julianpeeters committed Feb 4, 2024
1 parent 8ca617b commit 27f7871
Show file tree
Hide file tree
Showing 26 changed files with 513 additions and 513 deletions.
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ The most basic finite state machines are those parameterized by a polymap from
a store to a monomial:

```scala
import polynomial.`object`.Monomial
import polynomial.`object`.Mono
import polynomial.morphism.~>
import dynamical.fsm.Moore

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

However, in order to run them as a function `(S, A) => (S, B)`, we need the
Expand All @@ -32,9 +32,9 @@ output `B` to be a function from input to output, `A => B`. For example:
import cats.implicits.given
import dynamical.fsm.Moore
import polynomial.morphism.~>
import polynomial.`object`.Monomial
import polynomial.`object`.Mono

def mealified[Y]: Moore[Monomial.Store[Boolean, _] ~> Monomial.Interface[Int, Int => Int, _]] =
def mealified[Y]: Moore[Mono.Store[Boolean, _] ~> Mono.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 @@ -57,15 +57,15 @@ example:
import cats.implicits.given // for `mapAccumulate`
import dynamical.fsm.{Moore, Mealy}
import polynomial.morphism.~>
import polynomial.`object`.Monomial
import polynomial.`object`.Mono

def moore[Y]: Moore[Monomial.Store[Boolean, _] ~> Monomial.Interface[Int, Int => Int, _]] =
def moore[Y]: Moore[Mono.Store[Boolean, _] ~> Mono.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[Monomial.Store[Boolean, _] ~> Monomial.Interface[Int, Int => Int, _]] = moore.asMealy
val m: Mealy[Mono.Store[Boolean, _] ~> Mono.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,14 +91,14 @@ There are several aspects to the composition of state systems with wiring diagra
```scala
import cats.implicits.given
import dynamical.fsm.{Mealy, Moore, Wiring}
import polynomial.`object`.Monomial
import polynomial.`object`.Monomial.Store
import polynomial.`object`.Mono
import polynomial.`object`.Mono.Store
import polynomial.morphism.~>
import polynomial.product.

type Plant[Y] = Monomial.Interface[(Byte, Byte => Char), Char, Y]
type Controller[Y] = Monomial.Interface[Char, Byte => Char, Y]
type System[Y] = Monomial.Interface[Byte, Byte => Char, Y]
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 ω[Y] = ((PlantController) ~> System)[Y]

val w: Wiring[ω] = Wiring(
Expand Down Expand Up @@ -145,9 +145,9 @@ import dynamical.fsm.Mealy
import dynamical.stream.transducer
import fs2.Stream
import polynomial.morphism.~>
import polynomial.`object`.Monomial
import polynomial.`object`.Mono

val m: Mealy[Monomial.Store[Boolean, _] ~> Monomial.Interface[Int, Int => Int, _]] = Mealy(false, s => i => i + i, (s, i) => s)
val m: Mealy[Mono.Store[Boolean, _] ~> Mono.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+7-b9881620+20240129-1509-SNAPSHOT"
val PolynomialV = "0.4.0+9-4a9eba9b+20240203-1620-SNAPSHOT"

inThisBuild(List(
crossScalaVersions := Seq(scalaVersion.value),
Expand Down
28 changes: 14 additions & 14 deletions docs/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ The most basic finite state machines are those parameterized by a polymap from
a store to a monomial:

```scala mdoc
import polynomial.`object`.Monomial
import polynomial.`object`.Mono
import polynomial.morphism.~>
import dynamical.fsm.Moore

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

However, in order to run them as a function `(S, A) => (S, B)`, we need the
Expand All @@ -32,9 +32,9 @@ output `B` to be a function from input to output, `A => B`. For example:
import cats.implicits.given
import dynamical.fsm.Moore
import polynomial.morphism.~>
import polynomial.`object`.Monomial
import polynomial.`object`.Mono

def mealified[Y]: Moore[Monomial.Store[Boolean, _] ~> Monomial.Interface[Int, Int => Int, _]] =
def mealified[Y]: Moore[Mono.Store[Boolean, _] ~> Mono.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 @@ -56,15 +56,15 @@ example:
import cats.implicits.given // for `mapAccumulate`
import dynamical.fsm.{Moore, Mealy}
import polynomial.morphism.~>
import polynomial.`object`.Monomial
import polynomial.`object`.Mono

def moore[Y]: Moore[Monomial.Store[Boolean, _] ~> Monomial.Interface[Int, Int => Int, _]] =
def moore[Y]: Moore[Mono.Store[Boolean, _] ~> Mono.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[Monomial.Store[Boolean, _] ~> Monomial.Interface[Int, Int => Int, _]] = moore.asMealy
val m: Mealy[Mono.Store[Boolean, _] ~> Mono.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 @@ -90,14 +90,14 @@ There are several aspects to the composition of state systems with wiring diagra
```scala mdoc:reset:height=5
import cats.implicits.given
import dynamical.fsm.{Mealy, Moore, Wiring}
import polynomial.`object`.Monomial
import polynomial.`object`.Monomial.Store
import polynomial.`object`.Mono
import polynomial.`object`.Mono.Store
import polynomial.morphism.~>
import polynomial.product.

type Plant[Y] = Monomial.Interface[(Byte, Byte => Char), Char, Y]
type Controller[Y] = Monomial.Interface[Char, Byte => Char, Y]
type System[Y] = Monomial.Interface[Byte, Byte => Char, Y]
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 ω[Y] = ((PlantController) ~> System)[Y]

val w: Wiring[ω] = Wiring(
Expand Down Expand Up @@ -135,9 +135,9 @@ import dynamical.fsm.Mealy
import dynamical.stream.transducer
import fs2.Stream
import polynomial.morphism.~>
import polynomial.`object`.Monomial
import polynomial.`object`.Mono

val m: Mealy[Monomial.Store[Boolean, _] ~> Monomial.Interface[Int, Int => Int, _]] = Mealy(false, s => i => i + i, (s, i) => s)
val m: Mealy[Mono.Store[Boolean, _] ~> Mono.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
```
4 changes: 2 additions & 2 deletions modules/fs2/jvm/src/test/scala/dynamical/fs2Suite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import dynamical.stream.transducer
import fs2.Stream
import munit.FunSuite
import polynomial.morphism.~>
import polynomial.`object`.Monomial
import polynomial.`object`.Mono

class fs2Suite extends FunSuite:

test("fs2"):
val m: Mealy[Monomial.Store[Boolean, _] ~> Monomial.Interface[Int, Int => Int, _]] = Mealy(false, s => i => i + i, (s, i) => s)
val m: Mealy[Mono.Store[Boolean, _] ~> Mono.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)
6 changes: 3 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,17 @@ import fs2.Pipe
import dynamical.fsm.Mealy
import dynamical.fsm.methods.types.Unify2
import polynomial.morphism.~>
import polynomial.`object`.{Binomial, Monomial}
import polynomial.`object`.{Bi, Mono}

extension [S, A, B] (m: Mealy[Monomial.Store[S, _] ~> Monomial.Interface[A, A => B, _]])
extension [S, A, B] (m: Mealy[Mono.Store[S, _] ~> Mono.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[Monomial.Store[S, _] ~> Binomial.Interface[A1, A1 => B1, A2, A2 => B2, _]])
extension [S, A1, B1, A2, B2] (m: Mealy[Mono.Store[S, _] ~> Bi.Interface[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
10 changes: 5 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,14 @@ import cats.implicits.given
import dynamical.fsm.{Mealy, Moore}
import munit.FunSuite
import polynomial.morphism.~>
import polynomial.`object`.{Binomial, Monomial}
import polynomial.`object`.{Bi, Mono}

class BiSuite extends FunSuite:

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

test("mealy"):
def m[Y]: Mealy[
Monomial.Store[Boolean, _] ~>
Binomial.Interface[
Mono.Store[Boolean, _] ~>
Bi.Interface[
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 @@ -4,13 +4,13 @@ import cats.implicits.given
import dynamical.fsm.{Mealy, Moore}
import munit.FunSuite
import polynomial.morphism.~>
import polynomial.`object`.Monomial
import polynomial.`object`.Mono
import polynomial.product.

class CompositionSuite extends FunSuite:

test("mealy composition product"):
type P[Y] = (Monomial.Store[Boolean, _] ~> (Monomial.Interface[Int, Int, _] ◁ Monomial.Interface[String, String => String, _]))[Y]
type P[Y] = (Mono.Store[Boolean, _] ~> (Mono.Interface[Int, Int, _] ◁ Mono.Interface[String, String => String, _]))[Y]
val n: Moore[P] =
Moore(
i = false,
Expand Down
32 changes: 16 additions & 16 deletions modules/fsm/jvm/src/test/scala/dynamical/MonoSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,80 +5,80 @@ import cats.implicits.given
import dynamical.fsm.{Mealy, Moore, Wiring}
import munit.FunSuite
import polynomial.morphism.~>
import polynomial.`object`.Monomial
import polynomial.`object`.Mono

class MonoSuite extends FunSuite:

test("moore"):
val m: Moore[Monomial.Store[Boolean, _] ~> Monomial.Interface[Int, Int => Int, _]] = Moore(false, s => i => i + i, (s, i) => s)
val m: Moore[Mono.Store[Boolean, _] ~> Mono.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[Monomial.Store[Boolean, _] ~> Monomial.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[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 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[Monomial.Store[Boolean, _] ~> Monomial.Interface[Int, Int, _]] = Moore(false, s => if s then 1 else 0, (s, i) => s)
val m: Mealy[Monomial.Store[Boolean, _] ~> Monomial.Interface[Int, Int, _] ~> Monomial.Interface[Int, Int => Int, _]] =
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(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[Monomial.Interface[Int, Int, _] ~> Monomial.Interface[Int, Int => Int, _]] = Wiring[Id, Int, Int, Y](i => j => j + j, (i1, i2) => i2)
val l: Moore[Monomial.Store[Boolean, _] ~> Monomial.Interface[Int, Int, _]] = Moore(false, s => if s then 1 else 0, (s, i) => s)
val m: Mealy[Monomial.Store[Boolean, _] ~> Monomial.Interface[Int, Int, _] ~> Monomial.Interface[Int, Int => Int, _]] = l.andThen(w).asMealy
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
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[Monomial.Store[Option[Boolean], _] ~> Monomial.Interface[Int, Int => Int, _]] =
val m: Moore[Mono.Store[Option[Boolean], _] ~> Mono.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[Monomial.Interface[Int, Int, _] ~> Monomial.Interface[Int, Int => Int, _]] = Wiring[Id, Int, Int, Y](i => j => j + j, (i1, i2) => i2)
val n: Moore[Monomial.Store[Option[Boolean], _] ~> Monomial.Interface[Int, Int, _]] =
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, _]] =
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[Monomial.Store[Option[Boolean], _] ~> Monomial.Interface[Int, Int => Option[Int], _]] =
val m: Moore[Mono.Store[Option[Boolean], _] ~> Mono.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[Monomial.Interface[Int, Int, _] ~> Monomial.Interface[Int, Int => Option[Int], _]] =
val w: Wiring[Mono.Interface[Int, Int, _] ~> Mono.Interface[Int, Int => Option[Int], _]] =
Wiring(i => j => if j > 1 then Some(j + j) else Some(i), (i1, i2) => i2)
val n: Moore[Monomial.Store[Option[Boolean], _] ~> Monomial.Interface[Int, Option[Int], _]] =
val n: Moore[Mono.Store[Option[Boolean], _] ~> Mono.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[Monomial.Store[Option[(Int, Boolean)], _] ~> Monomial.Interface[Long, Long => Long, _]] =
val m: Moore[Mono.Store[Option[(Int, Boolean)], _] ~> Mono.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[Monomial.Store[Option[(Int, Boolean)], _] ~> Monomial.Interface[Long, Long => Option[Long], _]] =
val m: Moore[Mono.Store[Option[(Int, Boolean)], _] ~> Mono.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 27f7871

Please sign in to comment.