Programs
Catlab.Programs.GenerateJuliaPrograms
— ModuleCompile or evaluate morphisms as Julia programs.
Catlab.Programs.GenerateJuliaPrograms.Block
— TypeA block of Julia code with input and output variables.
Catlab.Programs.GenerateJuliaPrograms.CompileState
— TypeInternal state for compilation of morphism into Julia code.
Catlab.Programs.GenerateJuliaPrograms.compile
— MethodCompile a morphism expression into a Julia function.
Catlab.Programs.GenerateJuliaPrograms.compile_block
— MethodCompile a morphism expression into a block of Julia code.
Catlab.Programs.GenerateJuliaPrograms.compile_expr
— MethodCompile a morphism expression into a Julia function expression.
Catlab.Programs.GenerateJuliaPrograms.evaluate
— MethodEvaluate a morphism as a function.
If the morphism will be evaluated only once (possibly with vectorized inputs), then direct evaluation will be much faster than compiling (via compile
) and evaluating a standard Julia function.
Compare with functor
.
Catlab.Programs.ParseJuliaPrograms
— ModuleParse Julia programs into morphisms represented as wiring diagrams.
Catlab.Programs.ParseJuliaPrograms.parse_wiring_diagram
— MethodParse a wiring diagram from a Julia function expression.
For more information, see the corresponding macro @program
.
Catlab.Programs.ParseJuliaPrograms.@program
— MacroParse a wiring diagram from a Julia program.
For the most part, this is standard Julia code but we take a few liberties with the syntax. Products are represented as tuples. So if x
and y
are variables of type $X$ and $Y$, then (x,y)
has type $X \otimes Y$. Also, both ()
and nothing
are interpreted as the monoidal unit $I$.
Unlike in standard Julia, the call expressions f(x,y)
and f((x,y))
are equivalent. Consequently, given morphisms $f: W \to X \otimes Y$ and $g: X \otimes Y \to Z$, the code
x, y = f(w)
g(x,y)
is equivalent to g(f(w))
. In standard Julia, at most one of these calls to g
would be valid, unless g
had multiple signatures.
The diagonals (copying and deleting) are implicit in the Julia syntax: copying is variable reuse and deleting is variable non-use. For the codiagonals (merging and creating), a special syntax is provided, reinterpreting Julia's vector literals. The merge of x1
and x2
is represented by the vector [x1,x2]
and creation by the empty vector []
. For example, f([x1,x2])
translates to compose(mmerge(X),f)
.
This macro is a wrapper around parse_wiring_diagram
.
Catlab.Programs.RelationalPrograms
— ModuleParse relation expressions in Julia syntax into undirected wiring diagrams.
Catlab.Programs.RelationalPrograms.compile_tensor_expr
— MethodGenerate tensor expression from undirected wiring diagram.
This function is used to implement @eval_tensor_network
but may be useful in its own right.
Catlab.Programs.RelationalPrograms.parse_relation_diagram
— MethodParse an undirected wiring diagram from a relation expression.
For more information, see the corresponding macro @relation
.
Catlab.Programs.RelationalPrograms.parse_tensor_network
— MethodParse an undirected wiring diagram from a tensor expression.
For more information, see the corresponding macro @tensor_network
.
Catlab.Programs.RelationalPrograms.@eval_tensor_network
— MacroEvaluate a tensor network using another macro.
This macro takes two arguments: an undirected wiring diagram and a macro call supporting the tensor contraction notation that is de facto standard among Julia tensor packages. For example, to evaluate a tensor network using the @tullio
macro, use:
A = @eval_tensor_network diagram @tullio
The following macros should work:
@tensor
from TensorOperations.jl.@tullio
from Tullio.jl@einsum
from Einsum.jl@ein
from OMEinsum.jl
However,, the macros @cast
and @reduce
from TensorCast.jl will not work because they do not support implicit summation.
See also: @tensor_network
, the "inverse" to this macro.
Catlab.Programs.RelationalPrograms.@relation
— MacroConstruct an undirected wiring diagram using relation notation.
Unlike the @program
macro for directed wiring diagrams, this macro fundamentally alters the usual semantics of the Julia language. Function calls with n arguments are now interpreted as assertions that an n-ary relation holds at a particular point. For example, the operation of composing of binary relations R ⊆ X × Y and S ⊆ Y × Z can be represented as an undirected wiring diagram by the macro call
@relation (x,z) where (x::X, y::Y, z::Z) begin
R(x,y)
S(y,z)
end
In general, the context in the where
clause defines the set of junctions in the diagram and variable sharing defines the wiring of ports to junctions.
Catlab.Programs.RelationalPrograms.@tensor_network
— MacroConstruct an undirected wiring diagram using tensor notation.
The tensor syntax is compatible with that used by packages like TensorOperations.jl and TensorCast.jl. For example, the wiring diagram for composite of two matrices (or two binary relations) is constructed by
@tensor_network (i,j,k) C[i,k] := A[i,j] * B[j,k]
The leading context, or list of variables, may be omitted, in which case it is inferred from the variables used in the tensor expression. So, in this example, an equivalent macro call is
@tensor_network C[i,k] := A[i,j] * B[j,k]
See also: @eval_tensor_network
, the "inverse" to this macro.