This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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}]""" | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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._ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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("+") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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._ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 _ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
NewerOlder