Skip to content

Commit

Permalink
Add composition product for monomials
Browse files Browse the repository at this point in the history
  • Loading branch information
julianpeeters committed Jan 15, 2024
1 parent 8bf884b commit b6e8c82
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ Based on the polynomial functors described in [Niu and Spivak](https://topos.sit
The `polynomial` library provides the following implementation of poly:
- objects: built-in ADTs for monomial, binomial, and trinomial `Store` and `Interface` functors
- morphisms: `PolyMap`, or `~>`, a natural transformation between polynomial functors
- products: `Tensor`, or ``, a parallel product implemented as match types
- products:
- `Composition`, or ``, a composition product implemented as match types
- `Tensor`, or ``, a parallel product implemented as match types

```scala
import polynomial.`object`.*
import polynomial.morphism.~>
import polynomial.product.
import polynomial.product.{, }

type `2y⁵¹²` = Monomial.Interface[(Byte, Boolean), Boolean, _]
type `y² + 2y` = Binomial[Boolean, Unit, Unit, Boolean, _]
Expand All @@ -39,6 +41,7 @@ type `0` = Monomial.Interface[Nothing, Nothing, _]
type `1` = Monomial.Interface[Unit, Nothing, _]
type `y² + 2y → 2y⁵¹²` = (`y² + 2y` ~> `2y⁵¹²`)[_]
type `4y⁴` = (`2y²` ⊗ `2y²`)[_]
type `8y⁴` = (`2y²` ◁ `2y²`)[_]
```

#### FAQ
Expand Down Expand Up @@ -76,7 +79,7 @@ import polynomial.morphism.~>
type F[Y] = (Store[Boolean, _] ~> Interface[Byte, Char, _])[Y]

val M: Mermaid[F] = summon[Mermaid[F]]
// M: Mermaid[F] = polynomial.mermaid.Mermaid$$anon$1@1a26ca38
// M: Mermaid[F] = polynomial.mermaid.Mermaid$$anon$1@388f51ae

println(M.showGraph(graphFmt = Format.Specific))
// ```mermaid
Expand Down
7 changes: 5 additions & 2 deletions docs/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ Based on the polynomial functors described in [Niu and Spivak](https://topos.sit
The `polynomial` library provides the following implementation of poly:
- objects: built-in ADTs for monomial, binomial, and trinomial `Store` and `Interface` functors
- morphisms: `PolyMap`, or `~>`, a natural transformation between polynomial functors
- products: `Tensor`, or ``, a parallel product implemented as match types
- products:
- `Composition`, or ``, a composition product implemented as match types
- `Tensor`, or ``, a parallel product implemented as match types

```scala mdoc
import polynomial.`object`.*
import polynomial.morphism.~>
import polynomial.product.
import polynomial.product.{, }

type `2y⁵¹²` = Monomial.Interface[(Byte, Boolean), Boolean, _]
type `y² + 2y` = Binomial[Boolean, Unit, Unit, Boolean, _]
Expand All @@ -39,6 +41,7 @@ type `0` = Monomial.Interface[Nothing, Nothing, _]
type `1` = Monomial.Interface[Unit, Nothing, _]
type `y² + 2y → 2y⁵¹²` = (`y² + 2y` ~> `2y⁵¹²`)[_]
type `4y⁴` = (`2y²` ⊗ `2y²`)[_]
type `8y⁴` = (`2y²` ◁ `2y²`)[_]
```

#### FAQ
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package polynomial.morphism

import polynomial.`object`.{Binomial, Monomial}
import polynomial.product.{Tensor, }
import polynomial.product.{Tensor, Composition, }

type ~>[P[_], Q[_]] = PolyMap[P, Q, _]

Expand Down Expand Up @@ -32,6 +32,7 @@ object PolyMap:
case (PolyMap[o, p, Y], Tensor[q, r, Y]) => Phi[o, Tensor[q, r, _], Y]
case (Monomial.Store[s, Y], Binomial.Interface[a1, b1, a2, b2, Y]) => (s => b1, s => b2)
case (Monomial.Store[s, Y], Monomial.Interface[a, b, Y]) => s => b
case (Monomial.Store[s, Y], Composition[p, q, Y]) => (Phi[Monomial.Store[s, _], p, Y], Phi[p, q, Y])
case (Tensor[p, q, Y], Monomial.Interface[a1, b1, Y]) => Phi[Tensor.DayConvolution[p, q, _], Monomial.Interface[a1, b1, _], Y]
case (Tensor[p, q, Y], Binomial.Interface[a1, b1, a2, b2, Y]) => Phi[Tensor.DayConvolution[p, q, _], Binomial.Interface[a1, b1, a2, b2, _], Y]
case (Tensor[o, p, Y], Tensor[q, r, Y]) => Phi[Tensor.DayConvolution[o, p, _], Tensor.DayConvolution[q, r, _], Y]
Expand All @@ -43,6 +44,7 @@ object PolyMap:
case (Monomial.Interface[a1, b1, Y], Monomial.Interface[a2, b2, Y]) => (b1, a2) => a1
case (Monomial.Store[s, Y], Binomial.Interface[a1, b1, a2, b2, Y]) => ((s, a1) => s, (s, a2) => s)
case (Monomial.Store[s, Y], Monomial.Interface[a, b, Y]) => (s, a) => s
case (Monomial.Store[s, Y], Composition[p, q, Y]) => (s, Composition.DecomposeSharp[p, q, Y]) => s
case (PolyMap[p, q, Y], Binomial.Interface[a3, b3, a4, b4, Y]) => PhiSharp[p, Binomial.Interface[a3, b3, a4, b4, _], Y]
case (PolyMap[p, q, Y], Monomial.Interface[a2, b2, Y]) => PhiSharp[p, Monomial.Interface[a2, b2, _], Y]
case (PolyMap[o, p, Y], Tensor[q, r, Y]) => PhiSharp[o, Tensor[q, r, _], Y]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package polynomial.product

import polynomial.`object`.{Binomial, Monomial}
import polynomial.morphism.PolyMap

type [P[_], Q[_]] = Composition[P, Q, _]

abstract class Composition[P[_], Q[_], Y]
object Composition:

type Decompose[P[_], Q[_], Y] = (P[Y], Q[Y]) match
case (Monomial.Interface[a1, b1, Y], Monomial.Interface[a2, b2, Y]) => (b1, b2)
case (Monomial.Store[s1, Y], Monomial.Store[s2, Y]) => (s1, s2)

type DecomposeSharp[P[_], Q[_], Y] = (P[Y], Q[Y]) match
case (Monomial.Interface[a1, b1, Y], Monomial.Interface[a2, b2, Y]) => (a1, a2)
case (Monomial.Store[s1, Y], Monomial.Store[s2, Y]) => (s1, s2)

0 comments on commit b6e8c82

Please sign in to comment.