Skip to content

Instantly share code, notes, and snippets.

View mandubian's full-sized avatar

Pascal Voitot mandubian

View GitHub Profile
txt="""private bool isEven(int number) {
if (number == 1) return false;
else if (number == 2) return true;
else if (number == 3) return false;
else if (number == 4) return false;
"""
openai.Completion.create(
engine="davinci",
prompt=txt,
>>> import torch
# a Float tensor
>>> a = torch.tensor([[0, 0], [1, 1], [2, 2]], dtype=torch.float)
>>> a
tensor([[0., 0.],
[1., 1.],
[2., 2.]])
# a Long tensor
>>> b = torch.tensor([[3], [4], [5]], dtype=torch.long)
// 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}]"""
// 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)
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._
// 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("+")
// 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
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._
// 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 _
// 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