Skip to content

Instantly share code, notes, and snippets.

Pascal Voitot mandubian

Block or report user

Report or block mandubian

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View scalafix_ccc.scala
// Fake Scalafix rule to rewrite Scala to CCC
// Just so that you catch the idea
final case class CCCRule(index: SemanticdbIndex) extends SemanticRule(index, "CCCRule") {
def convertSubTree(ctx: RuleCtx, v: Tree, tree: Tree): Tree = {
tree match {
// the identity morphism
case t@q"$x => ${y: Term.Name}" if (x.name.isEqual(y)) =>
q"""K.id[${x.decltpe.get}]"""
View graph_run.scala
// You can run the plus function rewritten in Graph CCC
// and it will build a nice directed graph of your computation
val dGraph = Graph.runGraph(plus)(14, 28)
// now you can translate that directed graph into a nice format like graphviz
Graph.toGraphViz("plus")(dGraph)
View graph_rewrite.scala
def plus = (x:Int) => (y:Int) => x + y
// First, we need to reify our CCC language
val K = implicitly[CartesianClosedCat[Graph]]
// First, we need to reify our CCC numeric extensions for Int (ok it's a bit hard coded but that's not the point here ;))
val E = implicitly[CCCNumExt[Graph, Int]]
// now we import CCC language into our context
import K._, E._
View graph_ccc.scala
// our CCC with Graph as morphism
implicit val GraphCCC: ClosedCartesianCat[Graph] = new ClosedCartesianCat[Graph] {
// long boring useless code
}
// we can also implement CCCNumExt for Graph
implicit def GraphCCCNumExt[A](implicit N: Numeric[A], gp: GenPorts[A]): CCCNumExt[Graph, A] = new CCCNumExt[Graph, A] {
// genComp just generate a component with 2 inputs, 1 output and a nice name
def negateC: Graph[A, A] = genComp("-")
def addC: Graph[(A, A), A] = genComp("+")
View graph.scala
// A port is just identified by an Int
type Port = Int
// A component has a name and input/ouput ports
final case class Comp[A, B](name: String, inputs: Ports[A], outputs: Ports[B])
// the state monad building the list of components Comp
type GraphM[A] = ((Port, List[Comp[_, _]])) => ((Port, List[Comp[_, _]]), A)
// the kleisli-like directed graph structure based on state monoad
View rewrite_numcat.scala
def plus = (x:Int) => (y:Int) => x + y
// First, we need to reify our CCC language
val K = implicitly[CartesianClosedCat[Function1]]
// First, we need to reify our CCC numeric extensions for Int (ok it's a bit hard coded but that's not the point here ;))
val E = implicitly[CCCNumExt[Function1, Int]]
// now we import CCC language into our context
import K._, E._
View numcat.scala
// We can write this type class parameterized by the morphism and a type A
trait CCCNumExt[->[_, _], A] {
def negateC: A -> A
def addC: (A, A) -> A
def mulC: (A, A) -> A
}
// and implement it for Function1 and any Numeric A
implicit def Function1CCCNumExt[A](implicit N: Numeric[A]): CCCNumExt[Function1, A] = new CCCNumExt[Function1, A] {
def negateC: A => A = N.negate _
View rewrite_plus.scala
// a very basic Scala function performing a simple operation on primitive type Int
def plus = (x:Int) => (y:Int) => x + y
// First, we need to reify our CCC world
val K = implicitly[CartesianClosedCat[Function1]]
// now we import CCC language into our context
import K._
// pseudo-code
// we can always uncurry curried function
View function1_closed.scala
implicit val Function1ClosedCartesianCat: ClosedCartesianCat[Function1] = new ClosedCartesianCat[Function1] {
def id[A]: A => A = identity
def [A, B, C]: (B => C) => (A => B) => (A => C) = f => g => f compose g
def [A, C, D]: (A => C) => (A => D) => (A => (C, D)) = f => g => (a => (f(a), g(a)))
def exl[A, B]: ((A, B)) => A = _._1
def exr[A, B]: ((A, B)) => B = _._2
def it[A]: A => Unit = _ => ()
View closed.scala
trait ClosedCartesianCat[->[_, _]] extends CartesianCat[->] {
def it[A]: A -> Unit
def ap[A, B]: ((A -> B), A) -> B
def curry[A, B, C]: ((A, B) -> C) => (A -> (B :=> C))
def uncurry[A, B, C]: (A -> (B -> C)) => ((A, B) -> C)
}
You can’t perform that action at this time.