Skip to content

Commit

Permalink
Add polynomial-mermaid module
Browse files Browse the repository at this point in the history
Basics for generating mermaid definitions for PolyMaps
  • Loading branch information
julianpeeters committed Dec 21, 2023
1 parent ecad5f5 commit 38f93c3
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 17 deletions.
38 changes: 30 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,41 @@ Based on the polynomial functors described in [Niu and Spivak](https://topos.sit
#### `polynomial`

The `polynomial` library provides the following implementation of poly:
- objects: built-in ADTs representing `Monomial`, `Binomial`, `Trinomial` functors
- objects: built-in ADTs representing `Monomial`, `Binomial`, `Trinomial`, and `Store` functors
- morphisms: `PolyMap`, a natural transformation between polynomial functors
- products: `Tensor`, a type match on functors, yielding their Day convolution
- products: `Tensor`, a type match on functors, yielding a parallel product

```scala
import polynomial.`object`.Monomial
import polynomial.`object`.{Binomial, Monomial, Store}
import polynomial.morphism.~>
import polynomial.product.

type Plant[Y] = Monomial[(Byte, Byte => Char), Char, Y]
type Controller[Y] = Monomial[Char, Byte => Char, Y]
type System[Y] = Monomial[Byte, Byte => Char, Y]
type `2y²` = Store[Boolean, _]
type `2y⁵¹²` = Monomial[(Byte, Boolean), Boolean, _]
type `y² + 2y` = Binomial[Boolean, Unit, Unit, Boolean, _]
type `y² + 2y → 2y⁵¹²` = (`y² + 2y` ~> `2y⁵¹²`)[_]
type `4y⁴` = (`2y²` `2y²`)[_]
```

#### `polynomial-mermaid`

Certain lenses lend themselves to graphical interpretation:

```scala
import polynomial.`object`.{Store, Monomial}
import polynomial.mermaid.Mermaid
import polynomial.morphism.~>

type P[Y] = (Store[Int, _] ~> Monomial[Char, Unit, _])[Y]

println(s"${summon[Mermaid[P]].showSpecific}")
// ```mermaid
// graph LR;
// A[Char]:::hidden-->|Char|S[Int]-->|Unit|B[Unit]:::hidden;
// ```
```

type ω[Y] = ((Plant Controller) ~> System)[Y]
```mermaid
graph LR;
A[Char]:::hidden-->|Char|S[java.lang.String]-->|Unit|B[Unit]:::hidden;
```
<small>(Note: example adapted from the poly book, by [Niu and Spivak](https://topos.site/poly-book.pdf))</small>
7 changes: 6 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ lazy val poly = crossProject(JSPlatform, JVMPlatform, NativePlatform)
)
)

lazy val mermaid = crossProject(JSPlatform, JVMPlatform, NativePlatform)
.in(file("modules/mermaid"))
.settings(name := "polynomial-mermaid")
.dependsOn(poly)

lazy val docs = project.in(file("docs/gitignored"))
.settings(
mdocOut := file("."),
Expand All @@ -44,6 +49,6 @@ lazy val docs = project.in(file("docs/gitignored"))
"VERSION" -> version.value.takeWhile(_ != '+'),
)
)
.dependsOn(poly.jvm)
.dependsOn(poly.jvm, mermaid.jvm)
.enablePlugins(MdocPlugin)
.enablePlugins(NoPublishPlugin)
37 changes: 29 additions & 8 deletions docs/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,40 @@ Based on the polynomial functors described in [Niu and Spivak](https://topos.sit
#### `polynomial`

The `polynomial` library provides the following implementation of poly:
- objects: built-in ADTs representing `Monomial`, `Binomial`, `Trinomial` functors
- objects: built-in ADTs representing `Monomial`, `Binomial`, `Trinomial`, and `Store` functors
- morphisms: `PolyMap`, a natural transformation between polynomial functors
- products: `Tensor`, a type match on functors, yielding their Day convolution
- products: `Tensor`, a type match on functors, yielding a parallel product

```scala mdoc
import polynomial.`object`.Monomial
import polynomial.`object`.{Binomial, Monomial, Store}
import polynomial.morphism.~>
import polynomial.product.

type Plant[Y] = Monomial[(Byte, Byte => Char), Char, Y]
type Controller[Y] = Monomial[Char, Byte => Char, Y]
type System[Y] = Monomial[Byte, Byte => Char, Y]
type `2y²` = Store[Boolean, _]
type `2y⁵¹²` = Monomial[(Byte, Boolean), Boolean, _]
type `y² + 2y` = Binomial[Boolean, Unit, Unit, Boolean, _]
type `y² + 2y → 2y⁵¹²` = (`y² + 2y` ~> `2y⁵¹²`)[_]
type `4y⁴` = (`2y²` `2y²`)[_]
```

#### `polynomial-mermaid`

```scala mdoc:reset
import polynomial.`object`.{Store, Monomial}
import polynomial.mermaid.Mermaid
import polynomial.morphism.~>

type P[Y] = (Store[Int, _] ~> Monomial[Char, Unit, _])[Y]

type ω[Y] = ((Plant Controller) ~> System)[Y]
println(s"${summon[Mermaid[P]].showSpecific}")
```
<small>(Note: example adapted from the poly book, by [Niu and Spivak](https://topos.site/poly-book.pdf))</small>

```scala mdoc:reset:passthrough
import polynomial.`object`.{Store, Monomial}
import polynomial.mermaid.Mermaid
import polynomial.morphism.~>

type P[Y] = (Store[String, _] ~> Monomial[Char, Unit, _])[Y]

println(s"${summon[Mermaid[P]].showSpecific}")
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package polynomial.mermaid

import polynomial.morphism.PolyMap
import polynomial.`object`.{Monomial, Store}
import scala.reflect.ClassTag

trait Mermaid[P[_]]:
def showGeneric: String
def showSpecific: String

object Mermaid:

given moore[S, A, B](using
S: ClassTag[S],
A: ClassTag[A],
B: ClassTag[B]
): Mermaid[PolyMap[Store[S, _], Monomial[A, B, _], _]] =
new Mermaid[PolyMap[Store[S, _], Monomial[A, B, _], _]]:

def showGeneric: String =
s"""|```mermaid
|graph LR;
| A:::hidden-->|A|S-->|B|B:::hidden;
|```""".stripMargin

def showSpecific: String =
s"""|```mermaid
|graph LR;
| A[${A.toString}]:::hidden-->|${A.toString}|S[${S.toString}]-->|${B.toString}|B[${B.toString}]:::hidden;
|```""".stripMargin

given mealy[S, A, B](using
S: ClassTag[S],
A: ClassTag[A],
B: ClassTag[B]
): Mermaid[PolyMap[Store[S, _], Monomial[A, A => B, _], _]] =
new Mermaid[PolyMap[Store[S, _], Monomial[A, A => B, _], _]]:

def showGeneric: String =
s"""|```mermaid
|graph LR;
| A:::hidden-->|A|S-->|B|B:::hidden;
|```""".stripMargin

def showSpecific: String =
s"""|```mermaid
|graph LR;
| A[${A.toString}]:::hidden-->|${A.toString}|S[${S.toString}]-->|${A.toString} => ${B.toString}|B[${A.toString} => ${B.toString}]:::hidden;
|```""".stripMargin

0 comments on commit 38f93c3

Please sign in to comment.