Skip to content

Instantly share code, notes, and snippets.

@beezee
Created October 7, 2018 21:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beezee/9c181ac150966b6e853049b0d99bf15f to your computer and use it in GitHub Desktop.
Save beezee/9c181ac150966b6e853049b0d99bf15f to your computer and use it in GitHub Desktop.
Arity abstracted Prod/Cop of unrelated * -> * and their *s with adjoining transformations and operations in terms of them
import cats.arrow.FunctionK
import cats.{Functor, Monad}
import cats.syntax.either._
import scala.language.higherKinds
trait AxoAdjunction3[F1[_], F2[_], F3[_], A1, A2, A3] {
type Prod = (F1[A1], F2[A2], F3[A3])
type Cop = (F1[A1] Either (F2[A2] Either F3[A3]))
def r1: (F1 FunctionK F2)
def l1: (F2 FunctionK F1)
def r2: (F2 FunctionK F3)
def l2: (F3 FunctionK F2)
def combine21P[C](f: (A1, A2) => C)(implicit F: Monad[F1]):
Prod => AxoAdjunction3[F1, F2, F3, C, A2, A3]#Prod =
(p: Prod) => (F.flatMap(l1(p._2))(a2 =>
F.map(p._1)(a1 => f(a1, a2))), p._2, p._3)
def combine21C[C](f: (A1 Either A2) => C)(implicit F: Functor[F1]):
Cop => AxoAdjunction3[F1, F2, F3, C, A2, A3]#Cop =
(c: Cop) => c match {
case Left(f1a1) =>
F.map(f1a1)(a1 =>
f(a1.asLeft[A2])).asLeft[(F2[A2] Either F3[A3])]
case Right(Left(f2a2)) =>
F.map(l1(f2a2))(a2 =>
f(a2.asRight[A1])).asLeft[(F2[A2] Either F3[A3])]
case Right(Right(f3a3)) => f3a3.asRight[F2[A2]].asRight[F1[C]]
}
def combine12P[C](f: (A1, A2) => C)(implicit F: Monad[F2]):
Prod => AxoAdjunction3[F1, F2, F3, A1, C, A3]#Prod =
(p: Prod) => (p._1, F.flatMap(r1(p._1))(a1 =>
F.map(p._2)(a2 => f(a1, a2))), p._3)
def combine12C[C](f: (A1 Either A2) => C)(implicit F: Functor[F2]):
Cop => AxoAdjunction3[F1, F2, F3, A1, C, A3]#Cop =
(c: Cop) => c match {
case Left(f1a1) =>
F.map(r1(f1a1))(a1 =>
f(a1.asLeft[A2])).asLeft[F3[A3]].asRight[F1[A1]]
case Right(Left(f2a2)) =>
F.map(f2a2)(a2 => f(a2.asRight[A1])).asLeft[F3[A3]].asRight[F1[A1]]
case Right(Right(f3a3)) =>
f3a3.asRight[F2[C]].asRight[F1[A1]]
}
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment