Library Reference
Syntax
GATlab.Syntax.Scopes.Binding
— TypeBinding{T}
A binding associates some T
-typed value to a name.
name
is an optional distinguished name value
is the element
GATlab.Syntax.Scopes.Context
— TypeA Context
is anything which contains an ordered list of scopes.
Scopes within a context are referred to by level, which is their index within this list.
Context
s should overload:
getscope(c::Context, level::Int) -> Scope
nscopes(c::Context) -> Int
hastag(c::Context, tag::ScopeTag) -> Bool
hasname(c::Context, name::Symbol) -> Bool
getlevel(c::Context, tag::ScopeTag) -> Int
getlevel(c::Context, name::Symbol) -> Int
alltags(c::Context) -> Set{ScopeTag}
GATlab.Syntax.Scopes.HasScope
— TypeAn abstract type for wrappers around a single scope.
Must overload
getscope(hs::HasScope) -> Scope
GATlab.Syntax.Scopes.HasScopeList
— TypeA type for things which contain a scope list.
Notably, GATs contain a scope list.
Must implement:
getscopelist(hsl::HasScopeList) -> ScopeList
GATlab.Syntax.Scopes.Ident
— TypeIdent
An identifier.
tag
refers to the scope that this Ident is bound in lid
indexes the scope that Ident is bound in name
is an optional field for the sake of printing. A variable in a scope might be associated with several names
GATlab.Syntax.Scopes.LID
— TypeA LID (Local ID) indexes a given scope.
Currently, scopes assign LIDs sequentially – this is not a stable guarantee however, and in theory scopes could have "sparse" LIDs.
GATlab.Syntax.Scopes.Scope
— TypeScope{T}
In GATlab, we handle shadowing with a notion of scope. Names shadow between scopes. Anything which binds variables introduces a scope, for instance a @theory
declaration or a context. For example, here is a scope with 3 elements:
x = 3
+Library Reference · GATlab.jl Library Reference
Syntax
GATlab.Syntax.Scopes.Binding
— TypeBinding{T}
A binding associates some T
-typed value to a name.
name
is an optional distinguished name value
is the element
sourceGATlab.Syntax.Scopes.Context
— TypeA Context
is anything which contains an ordered list of scopes.
Scopes within a context are referred to by level, which is their index within this list.
Context
s should overload:
getscope(c::Context, level::Int) -> Scope
nscopes(c::Context) -> Int
hastag(c::Context, tag::ScopeTag) -> Bool
hasname(c::Context, name::Symbol) -> Bool
getlevel(c::Context, tag::ScopeTag) -> Int
getlevel(c::Context, name::Symbol) -> Int
alltags(c::Context) -> Set{ScopeTag}
sourceGATlab.Syntax.Scopes.HasScope
— TypeAn abstract type for wrappers around a single scope.
Must overload
getscope(hs::HasScope) -> Scope
sourceGATlab.Syntax.Scopes.HasScopeList
— TypeA type for things which contain a scope list.
Notably, GATs contain a scope list.
Must implement:
getscopelist(hsl::HasScopeList) -> ScopeList
sourceGATlab.Syntax.Scopes.Ident
— TypeIdent
An identifier.
tag
refers to the scope that this Ident is bound in lid
indexes the scope that Ident is bound in name
is an optional field for the sake of printing. A variable in a scope might be associated with several names
sourceGATlab.Syntax.Scopes.LID
— TypeA LID (Local ID) indexes a given scope.
Currently, scopes assign LIDs sequentially – this is not a stable guarantee however, and in theory scopes could have "sparse" LIDs.
sourceGATlab.Syntax.Scopes.Scope
— TypeScope{T}
In GATlab, we handle shadowing with a notion of scope. Names shadow between scopes. Anything which binds variables introduces a scope, for instance a @theory
declaration or a context. For example, here is a scope with 3 elements:
x = 3
y = "hello"
-z = x
Here z is introduced as an alias for x. It is illegal to shadow within a scope. Overloading is not explicitly treated but can be managed by having values which refer to identifiers earlier / the present scope. See GATs.jl, for example.
sourceGATlab.Syntax.Scopes.ScopeTag
— TypeThe tag that makes reference to a specific scope possible.
sourceGATlab.Syntax.Scopes.ScopeTagError
— TypeScopeTagError
An error to throw when an identifier has an unexpected scope tag
sourceGATlab.Syntax.Scopes.ScopedBinding
— TypeType for printing out bindings with colored keys
sourceGATlab.Syntax.Scopes.getidents
— MethodThis collects all the idents in a scope
sourceGATlab.Syntax.Scopes.getlid
— MethodDetermine the level of a binding given the name
sourceGATlab.Syntax.Scopes.hasident
— Methodhasident
checks whether an identifier with specified data exists, by attempting to create it and returning whether or not that attempt failed.
sourceGATlab.Syntax.Scopes.ident
— Methodident
creates an Ident
from a context and some partial data supplied as keywords.
Keywords arguments:
tag::Union{ScopeTag, Nothing}
. The tag of the scope that the Ident
is in.name::Union{Symbol, Nothing}
. The name of the identifier.lid::Union{LID, Nothing}
. The lid of the identifier within its scope.level::Union{Int, Nothing}
. The level of the scope within the context.strict::Bool
. If strict
is true, throw an error if not found, else return nothing.
sourceGATlab.Syntax.Scopes.idents
— MethodThis is a broadcasted version of ident
sourceGATlab.Syntax.Scopes.rename
— Methodrename(tag::ScopeTag, replacements::Dict{Symbol, Symbol}, x::T) where {T} -> T
Recurse through the structure of x
, and change any name n
in scope tag
to get(replacements, n, n)
. Overload this function on structs that have names in them.
sourceGATlab.Syntax.Scopes.retag
— Methodretag(replacements::Dict{ScopeTag, ScopeTag}, x::T) where {T} -> T
Recurse through the structure of x
, swapping any instance of a ScopeTag t
with get(replacements, t, t)
. Overload this function on structs that have ScopeTags within them.
sourceGATlab.Syntax.Scopes.unsafe_pushbinding!
— MethodAdd a new binding to the end of Scope s
.
sourceGATlab.Syntax.GATs.AbstractConstant
— TypeWe need this to resolve a mutual reference loop; the only subtype is Constant
sourceGATlab.Syntax.GATs.AbstractDot
— TypeWe need this to resolve a mutual reference loop; the only subtype is Dot
sourceGATlab.Syntax.GATs.AlgAccessor
— TypeAlgAccessor
The arguments to a term constructor serve a dual function as both arguments and also methods to extract the value of those arguments.
I.e., declaring Hom(dom::Ob, codom::Ob)::TYPE
implicitly overloads a previous declaration for dom
and codom
, or creates declarations if none previously exist.
sourceGATlab.Syntax.GATs.AlgAxiom
— TypeAlgAxiom
A declaration of an axiom
sourceGATlab.Syntax.GATs.AlgDeclaration
— TypeAlgDeclaration
A declaration of a constructor; constructor methods in the form of AlgTermConstructors
or the accessors for AlgTypeConstructors
follow later in the theory.
sourceGATlab.Syntax.GATs.AlgDot
— TypeAccessing a name from a term of tuple type
sourceGATlab.Syntax.GATs.AlgEqSort
— TypeAlgSort
A sort for equality judgments of terms for a particular sort
sourceGATlab.Syntax.GATs.AlgFunction
— TypeA shorthand for a function, such as "square(x) := x * x". It is relevant for models but can be ignored by theory maps, as it is fully determined by other judgments in the theory.
sourceGATlab.Syntax.GATs.AlgSort
— TypeAlgSort
A sort, which is essentially a type constructor without arguments
sourceGATlab.Syntax.GATs.AlgSorts
— TypeAlgSorts
A description of the argument sorts for a term constructor, used to disambiguate multiple term constructors of the same name.
sourceGATlab.Syntax.GATs.AlgStruct
— TypeAlgStruct
A declaration which is sugar for an AlgTypeConstructor, an AlgTermConstructor which constructs an element of that type, and projection term constructors. E.g.
struct Cospan(dom, codom) ⊣ [dom:Ob, codom::Ob]
+z = x
Here z is introduced as an alias for x. It is illegal to shadow within a scope. Overloading is not explicitly treated but can be managed by having values which refer to identifiers earlier / the present scope. See GATs.jl, for example.
sourceGATlab.Syntax.Scopes.ScopeTag
— TypeThe tag that makes reference to a specific scope possible.
sourceGATlab.Syntax.Scopes.ScopeTagError
— TypeScopeTagError
An error to throw when an identifier has an unexpected scope tag
sourceGATlab.Syntax.Scopes.ScopedBinding
— TypeType for printing out bindings with colored keys
sourceGATlab.Syntax.Scopes.getidents
— MethodThis collects all the idents in a scope
sourceGATlab.Syntax.Scopes.getlid
— MethodDetermine the level of a binding given the name
sourceGATlab.Syntax.Scopes.hasident
— Methodhasident
checks whether an identifier with specified data exists, by attempting to create it and returning whether or not that attempt failed.
sourceGATlab.Syntax.Scopes.ident
— Methodident
creates an Ident
from a context and some partial data supplied as keywords.
Keywords arguments:
tag::Union{ScopeTag, Nothing}
. The tag of the scope that the Ident
is in.name::Union{Symbol, Nothing}
. The name of the identifier.lid::Union{LID, Nothing}
. The lid of the identifier within its scope.level::Union{Int, Nothing}
. The level of the scope within the context.strict::Bool
. If strict
is true, throw an error if not found, else return nothing.
sourceGATlab.Syntax.Scopes.idents
— MethodThis is a broadcasted version of ident
sourceGATlab.Syntax.Scopes.rename
— Methodrename(tag::ScopeTag, replacements::Dict{Symbol, Symbol}, x::T) where {T} -> T
Recurse through the structure of x
, and change any name n
in scope tag
to get(replacements, n, n)
. Overload this function on structs that have names in them.
sourceGATlab.Syntax.Scopes.retag
— Methodretag(replacements::Dict{ScopeTag, ScopeTag}, x::T) where {T} -> T
Recurse through the structure of x
, swapping any instance of a ScopeTag t
with get(replacements, t, t)
. Overload this function on structs that have ScopeTags within them.
sourceGATlab.Syntax.Scopes.unsafe_pushbinding!
— MethodAdd a new binding to the end of Scope s
.
sourceGATlab.Syntax.GATs.AbstractConstant
— TypeWe need this to resolve a mutual reference loop; the only subtype is Constant
sourceGATlab.Syntax.GATs.AbstractDot
— TypeWe need this to resolve a mutual reference loop; the only subtype is Dot
sourceGATlab.Syntax.GATs.AlgAccessor
— TypeAlgAccessor
The arguments to a term constructor serve a dual function as both arguments and also methods to extract the value of those arguments.
I.e., declaring Hom(dom::Ob, codom::Ob)::TYPE
implicitly overloads a previous declaration for dom
and codom
, or creates declarations if none previously exist.
sourceGATlab.Syntax.GATs.AlgAxiom
— TypeAlgAxiom
A declaration of an axiom
sourceGATlab.Syntax.GATs.AlgDeclaration
— TypeAlgDeclaration
A declaration of a constructor; constructor methods in the form of AlgTermConstructors
or the accessors for AlgTypeConstructors
follow later in the theory.
sourceGATlab.Syntax.GATs.AlgDot
— TypeAccessing a name from a term of tuple type
sourceGATlab.Syntax.GATs.AlgEqSort
— TypeAlgSort
A sort for equality judgments of terms for a particular sort
sourceGATlab.Syntax.GATs.AlgFunction
— TypeA shorthand for a function, such as "square(x) := x * x". It is relevant for models but can be ignored by theory maps, as it is fully determined by other judgments in the theory.
sourceGATlab.Syntax.GATs.AlgSort
— TypeAlgSort
A sort, which is essentially a type constructor without arguments
sourceGATlab.Syntax.GATs.AlgSorts
— TypeAlgSorts
A description of the argument sorts for a term constructor, used to disambiguate multiple term constructors of the same name.
sourceGATlab.Syntax.GATs.AlgStruct
— TypeAlgStruct
A declaration which is sugar for an AlgTypeConstructor, an AlgTermConstructor which constructs an element of that type, and projection term constructors. E.g.
struct Cospan(dom, codom) ⊣ [dom:Ob, codom::Ob]
apex::Ob
i1::dom->apex
i2::codom->apex
@@ -22,12 +22,14 @@
⊣ [(dom, codom, apex)::Ob, i_1::dom->apex, i_2::codom->apex]
cospan(apex(csp), i1(csp), i2(csp)) == csp
- ⊣ [(dom, codom)::Ob, csp::Cospan(dom, codom)]
sourceGATlab.Syntax.GATs.AlgTerm
— TypeAlgTerm
One syntax tree to rule all the terms.
sourceGATlab.Syntax.GATs.AlgTermConstructor
— TypeAlgTermConstructor
A declaration of a term constructor as a method of an AlgFunction
.
sourceGATlab.Syntax.GATs.AlgType
— TypeAlgType
One syntax tree to rule all the types.
sourceGATlab.Syntax.GATs.AlgTypeConstructor
— TypeAlgTypeConstructor
A declaration of a type constructor.
sourceGATlab.Syntax.GATs.Constant
— TypeConstant
A Julia value in an algebraic context. Type checked elsewhere.
sourceGATlab.Syntax.GATs.Eq
— TypeEq
The type of equality judgments.
sourceGATlab.Syntax.GATs.GAT
— TypeGAT
A generalized algebraic theory. Essentially, just consists of a name and a list of GATSegment
s, but there is also some caching to make access faster. Specifically, there is a dictionary to map ScopeTag to position in the list of segments, and there are lists of all of the identifiers for term constructors, type constructors, and axioms so that they can be iterated through faster.
GATs allow overloading but not shadowing.
sourceGATlab.Syntax.GATs.GATContext
— TypeGATContext
A context consisting of two parts: a GAT and a TypeCtx
Certain types (like AlgTerm) can only be parsed in a GATContext, because they require access to the method resolving in the GAT.
sourceGATlab.Syntax.GATs.GATSegment
— TypeGATSegment
A piece of a GAT, consisting of a scope that binds judgments to names, possibly disambiguated by argument sorts.
This is a struct rather than just a type alias so that we can customize the show method.
sourceGATlab.Syntax.GATs.InCtx
— MethodGet the canonical type + ctx associated with a type constructor.
sourceGATlab.Syntax.GATs.InCtx
— MethodGet the canonical term + ctx associated with a method.
sourceGATlab.Syntax.GATs.Judgment
— TypeA GAT is conceptually a bunch of Judgment
s strung together.
sourceGATlab.Syntax.GATs.MethodApp
— TypeMethodApp
An application of a method of a constructor to arguments. We need a type parameter T
because AlgTerm
hasn't been defined yet, but the only type used for T
will in fact be AlgTerm
.
method
either points to an AlgTermConstructor
, an AlgTypeConstructor
or an AlgAccessor
,
sourceGATlab.Syntax.GATs.MethodResolver
— TypeMethodResolver
Right now, this just maps a sort signature to the resolved method.
When we eventually support varargs, this will have to do something slightly fancier.
sourceGATlab.Syntax.GATs.TypeScope
— TypeTypeScope
A scope where variables are assigned to AlgType
s. We use a wrapper here so that it pretty prints as [a::B]
instead of {a => AlgType(B)}
sourceBase.show
— MethodCommon methods for AlgType and AlgTerm
sourceGATlab.Syntax.ExprInterop.fromexpr
— MethodCoerce GATs to GAT contexts
sourceGATlab.Syntax.ExprInterop.toexpr
— MethodThis only works when seg
is a segment of theory
sourceGATlab.Syntax.GATs.equations
— MethodGet equations for a term or type constructor
sourceGATlab.Syntax.GATs.equations
— MethodImplicit equations defined by a context.
This function allows a generalized algebraic theory (GAT) to be expressed as an essentially algebraic theory, i.e., as partial functions whose domains are defined by equations.
References:
- (Cartmell, 1986, Sec 6: "Essentially algebraic theories and categories with finite limits")
- (Freyd, 1972, "Aspects of topoi")
This function gives expressions for computing the elements of c.context
which can be inferred from applying accessor functions to elements of args
.
Example:
equations({f::Hom(a,b), g::Hom(b,c)}, {a::Ob, b::Ob, c::Ob}, ThCategory)
waysofcomputing = Dict(a => [dom(f)], b => [codom(f), dom(g)], c => [codom(g)], f => [f], g => [g])
sourceGATlab.Syntax.GATs.normalize_judgment
— MethodThis is necessary because the intuitive precedence rules for the symbols that we use do not match the Julia precedence rules. In theory, this could be written with some algorithm that recalculates precedence, but I am too lazy to write that, so instead I just special case everything.
sourceGATlab.Syntax.GATs.parse_scope!
— Methodf(pushbinding!, expr)
should inspect expr
and call pushbinding!
0 or more times with two arguments: the name and value of a new binding.
sourceGATlab.Syntax.GATs.sortcheck
— Methodsortcheck(ctx::Context, t::AlgTerm)
Throw an error if a the head of an AlgTerm (which refers to a term constructor) has arguments of the wrong sort. Returns the sort of the term.
sourceGATlab.Syntax.GATs.sortcheck
— Methodsortcheck(ctx::Context, t::AlgType)
Throw an error if a the head of an AlgType (which refers to a type constructor) has arguments of the wrong sort.
sourceGATlab.Syntax.GATs.structs
— MethodGet all structs in a theory
sourceGATlab.Syntax.GATs.substitute_funs
— MethodReplace all functions with their desugared expressions
sourceGATlab.Syntax.GATs.substitute_term
— MethodReplace idents with AlgTerms.
sourceGATlab.Syntax.ExprInterop.fromexpr
— MethodParse, e.g.:
(a,b,c)::Ob
+ ⊣ [(dom, codom)::Ob, csp::Cospan(dom, codom)]
sourceGATlab.Syntax.GATs.AlgTerm
— TypeAlgTerm
One syntax tree to rule all the terms.
sourceGATlab.Syntax.GATs.AlgTermConstructor
— TypeAlgTermConstructor
A declaration of a term constructor as a method of an AlgFunction
.
sourceGATlab.Syntax.GATs.AlgType
— TypeAlgType
One syntax tree to rule all the types.
sourceGATlab.Syntax.GATs.AlgTypeConstructor
— TypeAlgTypeConstructor
A declaration of a type constructor.
sourceGATlab.Syntax.GATs.Constant
— TypeConstant
A Julia value in an algebraic context. Type checked elsewhere.
sourceGATlab.Syntax.GATs.Eq
— TypeEq
The type of equality judgments.
sourceGATlab.Syntax.GATs.GAT
— TypeGAT
A generalized algebraic theory. Essentially, just consists of a name and a list of GATSegment
s, but there is also some caching to make access faster. Specifically, there is a dictionary to map ScopeTag to position in the list of segments, and there are lists of all of the identifiers for term constructors, type constructors, and axioms so that they can be iterated through faster.
GATs allow overloading but not shadowing.
sourceGATlab.Syntax.GATs.GATContext
— TypeGATContext
A context consisting of two parts: a GAT and a TypeCtx
Certain types (like AlgTerm) can only be parsed in a GATContext, because they require access to the method resolving in the GAT.
sourceGATlab.Syntax.GATs.GATSegment
— TypeGATSegment
A piece of a GAT, consisting of a scope that binds judgments to names, possibly disambiguated by argument sorts.
This is a struct rather than just a type alias so that we can customize the show method.
sourceGATlab.Syntax.GATs.InCtx
— MethodGet the canonical type + ctx associated with a type constructor.
sourceGATlab.Syntax.GATs.InCtx
— MethodGet the canonical term + ctx associated with a method.
sourceGATlab.Syntax.GATs.Judgment
— TypeA GAT is conceptually a bunch of Judgment
s strung together.
sourceGATlab.Syntax.GATs.MethodApp
— TypeMethodApp
An application of a method of a constructor to arguments. We need a type parameter T
because AlgTerm
hasn't been defined yet, but the only type used for T
will in fact be AlgTerm
.
method
either points to an AlgTermConstructor
, an AlgTypeConstructor
or an AlgAccessor
,
sourceGATlab.Syntax.GATs.MethodResolver
— TypeMethodResolver
Right now, this just maps a sort signature to the resolved method.
When we eventually support varargs, this will have to do something slightly fancier.
sourceGATlab.Syntax.GATs.TypeScope
— TypeTypeScope
A scope where variables are assigned to AlgType
s. We use a wrapper here so that it pretty prints as [a::B]
instead of {a => AlgType(B)}
sourceBase.show
— MethodCommon methods for AlgType and AlgTerm
sourceGATlab.Syntax.ExprInterop.fromexpr
— MethodCoerce GATs to GAT contexts
sourceGATlab.Syntax.ExprInterop.toexpr
— MethodThis only works when seg
is a segment of theory
sourceGATlab.Syntax.GATs.equations
— MethodGet equations for a term or type constructor
sourceGATlab.Syntax.GATs.equations
— MethodImplicit equations defined by a context.
This function allows a generalized algebraic theory (GAT) to be expressed as an essentially algebraic theory, i.e., as partial functions whose domains are defined by equations.
References:
- (Cartmell, 1986, Sec 6: "Essentially algebraic theories and categories with finite limits")
- (Freyd, 1972, "Aspects of topoi")
This function gives expressions for computing the elements of c.context
which can be inferred from applying accessor functions to elements of args
.
Example:
equations({f::Hom(a,b), g::Hom(b,c)}, {a::Ob, b::Ob, c::Ob}, ThCategory)
waysofcomputing = Dict(a => [dom(f)], b => [codom(f), dom(g)], c => [codom(g)], f => [f], g => [g])
sourceGATlab.Syntax.GATs.normalize_judgment
— MethodThis is necessary because the intuitive precedence rules for the symbols that we use do not match the Julia precedence rules. In theory, this could be written with some algorithm that recalculates precedence, but I am too lazy to write that, so instead I just special case everything.
sourceGATlab.Syntax.GATs.parse_scope!
— Methodf(pushbinding!, expr)
should inspect expr
and call pushbinding!
0 or more times with two arguments: the name and value of a new binding.
sourceGATlab.Syntax.GATs.sortcheck
— Methodsortcheck(ctx::Context, t::AlgTerm)
Throw an error if a the head of an AlgTerm (which refers to a term constructor) has arguments of the wrong sort. Returns the sort of the term.
sourceGATlab.Syntax.GATs.sortcheck
— Methodsortcheck(ctx::Context, t::AlgType)
Throw an error if a the head of an AlgType (which refers to a type constructor) has arguments of the wrong sort.
sourceGATlab.Syntax.GATs.structs
— MethodGet all structs in a theory
sourceGATlab.Syntax.GATs.substitute_funs
— MethodReplace all functions with their desugared expressions
sourceGATlab.Syntax.GATs.substitute_term
— MethodReplace idents with AlgTerms.
sourceGATlab.Syntax.ExprInterop.fromexpr
— MethodParse, e.g.:
(a,b,c)::Ob
f::Hom(a, b)
g::Hom(b, c)
h::Hom(a, c)
h′::Hom(a, c)
-compose(f, g) == h == h′
sourceGATlab.Syntax.ExprInterop.fromexpr
— Functionfromexpr(c::Context, e::Any, T::Type) -> Union{T,Nothing}
Converts a Julia Expr into type T, in a certain scope.
sourceGATlab.Syntax.ExprInterop.toexpr
— Functiontoexpr(c::Context, t) -> Any
Converts GATlab syntax into an Expr that can be read in via fromexpr
to get the same thing. Crucially, the output of this will depend on the order of the scopes in c
, and if read back in with a different c
may end up with different results.
sourceGATlab.Syntax.TheoryInterface.GAT_MODULE_LOOKUP
— ConstantWhen we declare a new theory, we add the scope tag of its new segment to this dictionary pointing to the module corresponding to the new theory.
sourceGATlab.Syntax.TheoryInterface.fqmn
— MethodFully Qualified Module Name
sourceGATlab.Syntax.TheoryInterface.mk_struct
— Methodsource Models
GATlab.Models.ModelInterface.ImplementationNotes
— TypeImplementationNotes
Information about how a model implements a GATSegment
. Right now, just the docstring attached to the @instance
macro, but could contain more info in the future.
sourceGATlab.Models.ModelInterface.implements
— Methodimplements(m::Model, tag::ScopeTag) -> Union{ImplementationNotes, Nothing}
If m
implements the GATSegment referred to by tag
, then return the corresponding implementation notes.
sourceGATlab.Models.ModelInterface.parse_instance_body
— MethodParses the raw julia expression into JuliaFunctions
sourceGATlab.Models.ModelInterface.qualify_function
— MethodAdd WithModel
param first, if this is not an old instance (it shouldn't have it already) Qualify method name to be in theory module Qualify args to struct types Add context
kwargs if not already present
sourceGATlab.Models.ModelInterface.to_call_impl
— MethodCompile an AlgTerm into a Julia call Expr where termcons (e.g. f
) are interpreted as mod.f[model.model](...)
.
sourceGATlab.Models.ModelInterface.typecheck_instance
— MethodThrow error if missing a term constructor. Provides default instances for type constructors and type arguments, which return true or error, respectively.
sourceGATlab.Syntax.TheoryMaps.migrator
— MethodGiven a Theory Morphism T->U and a model Mᵤ which implements U, obtain a model Mₜ which wraps Mᵤ and is a model of T.
Future work: There is some subtlety in how accessor functions should be handled. TODO: The new instance methods do not yet handle the context
keyword argument.
sourceGATlab.Models.ModelInterface.@instance
— MacroUsage:
struct TypedFinSetC <: Model{Tuple{Vector{Int}, Vector{Int}}}
+compose(f, g) == h == h′
sourceGATlab.Syntax.GATContexts.@symbolic
— Macro@symbolic ThRing function v(a, b, c)
+ (a*b, c, b)
+end
sourceGATlab.Syntax.ExprInterop.fromexpr
— Functionfromexpr(c::Context, e::Any, T::Type) -> Union{T,Nothing}
Converts a Julia Expr into type T, in a certain scope.
sourceGATlab.Syntax.ExprInterop.toexpr
— Functiontoexpr(c::Context, t) -> Any
Converts GATlab syntax into an Expr that can be read in via fromexpr
to get the same thing. Crucially, the output of this will depend on the order of the scopes in c
, and if read back in with a different c
may end up with different results.
sourceGATlab.Syntax.TheoryInterface.GAT_MODULE_LOOKUP
— ConstantWhen we declare a new theory, we add the scope tag of its new segment to this dictionary pointing to the module corresponding to the new theory.
sourceGATlab.Syntax.TheoryInterface.fqmn
— MethodFully Qualified Module Name
sourceGATlab.Syntax.TheoryInterface.mk_struct
— Methodsource Models
GATlab.Models.ModelInterface.ImplementationNotes
— TypeImplementationNotes
Information about how a model implements a GATSegment
. Right now, just the docstring attached to the @instance
macro, but could contain more info in the future.
sourceGATlab.Models.ModelInterface.implements
— Methodimplements(m::Model, tag::ScopeTag) -> Union{ImplementationNotes, Nothing}
If m
implements the GATSegment referred to by tag
, then return the corresponding implementation notes.
sourceGATlab.Models.ModelInterface.parse_instance_body
— MethodParses the raw julia expression into JuliaFunctions
sourceGATlab.Models.ModelInterface.qualify_function
— MethodAdd WithModel
param first, if this is not an old instance (it shouldn't have it already) Qualify method name to be in theory module Qualify args to struct types Add context
kwargs if not already present
sourceGATlab.Models.ModelInterface.to_call_impl
— MethodCompile an AlgTerm into a Julia call Expr where termcons (e.g. f
) are interpreted as mod.f[model.model](...)
.
sourceGATlab.Models.ModelInterface.typecheck_instance
— MethodThrow error if missing a term constructor. Provides default instances for type constructors and type arguments, which return true or error, respectively.
sourceGATlab.Syntax.TheoryMaps.migrator
— MethodGiven a Theory Morphism T->U and a model Mᵤ which implements U, obtain a model Mₜ which wraps Mᵤ and is a model of T.
Future work: There is some subtlety in how accessor functions should be handled. TODO: The new instance methods do not yet handle the context
keyword argument.
sourceGATlab.Models.ModelInterface.@instance
— MacroUsage:
struct TypedFinSetC <: Model{Tuple{Vector{Int}, Vector{Int}}}
ntypes::Int
end
@@ -48,7 +50,7 @@
end
@instance ThCategory{Tuple{Ob, Hom}, Hom} [model::SliceCat{Ob, Hom, C}] where {Ob, Hom, C<:Model{Tuple{Ob, Hom}}} begin
-end
sourceGATlab.Models.SymbolicModels.GATExpr
— TypeBase type for expressions in the syntax of a GAT. This is an alternative to AlgTerm
used for backwards compatibility with Catlab.
We define Julia types for each type constructor in the theory, e.g., object, morphism, and 2-morphism in the theory of 2-categories. Of course, Julia's type system does not support dependent types, so the type parameters are incorporated in the Julia types. (They are stored as extra data in the expression instances.)
The concrete types are structurally similar to the core type Expr
in Julia. However, the term constructor is represented as a type parameter, rather than as a head
field. This makes dispatch using Julia's type system more convenient.
sourceBase.nameof
— MethodGet name of GAT generator expression as a Symbol
.
If the generator has no name, returns nothing
.
sourceGATlab.Models.SymbolicModels.constructor_name
— MethodName of constructor that created expression.
sourceGATlab.Models.SymbolicModels.functor
— MethodFunctor from GAT expression to GAT instance.
Strictly speaking, we should call these "structure-preserving functors" or, better, "model homomorphisms of GATs". But this is a category theory library, so we'll go with the simpler "functor".
A functor is completely determined by its action on the generators. There are several ways to specify this mapping:
Specify a Julia instance type for each GAT type, using the required types
tuple. For this to work, the generator constructors must be defined for the instance types.
Explicitly map each generator term to an instance value, using the generators
dictionary.
For each GAT type (e.g., object and morphism), specify a function mapping generator terms of that type to an instance value, using the terms
dictionary.
The terms
dictionary can also be used for special handling of non-generator expressions. One use case for this capability is defining forgetful functors, which map non-generators to generators.
FIXME
sourceGATlab.Models.SymbolicModels.generator_like
— MethodCreate generator of the same type as the given expression.
sourceGATlab.Models.SymbolicModels.parse_json_sexpr
— MethodDeserialize expression from JSON-able S-expression.
If symbols
is true (the default), strings are converted to symbols.
sourceGATlab.Models.SymbolicModels.show_latex
— MethodShow the expression in infix notation using LaTeX math.
Does not include $
or \[begin|end]{equation}
delimiters.
sourceGATlab.Models.SymbolicModels.show_sexpr
— MethodShow the syntax expression as an S-expression.
Cf. the standard library function Meta.show_sexpr
.
sourceGATlab.Models.SymbolicModels.show_unicode
— MethodShow the expression in infix notation using Unicode symbols.
sourceGATlab.Models.SymbolicModels.symbolic_instance_methods
— MethodJulia function for every type constructor, accessor, and term constructor. Term constructors can be overwritten by overrides
.
sourceGATlab.Models.SymbolicModels.syntax_module
— MethodGet syntax module of given expression.
sourceGATlab.Models.SymbolicModels.to_json_sexpr
— MethodSerialize expression as JSON-able S-expression.
The format is an S-expression encoded as JSON, e.g., "compose(f,g)" is represented as ["compose", f, g].
sourceGATlab.Models.SymbolicModels.@symbolic_model
— Macro@symbolic_model generates the free model of a theory, generated by symbols.
This is backwards compatible with the @syntax macro in Catlab.
One way of thinking about this is that for every type constructor, we add an additional term constructor that builds a "symbolic" element of that type from any Julia value. This term constructor is called "generator".
An invocation of @symbolic_model
creates the following.
- A module with a struct for each type constructor, which has a single type
parameter T
and two fields, args
and type_args
. Instances of this struct are thought of as elements of the type given by the type constructor applied to type_args
. The type parameter refers to the term constructor that was used to generate the element, including the special term constructor :generator
, which has as its single argument an Any
.
For instance, in the theory of categories, we might have the following elements.
using .FreeCategory
+end
sourceGATlab.Models.SymbolicModels.GATExpr
— TypeBase type for expressions in the syntax of a GAT. This is an alternative to AlgTerm
used for backwards compatibility with Catlab.
We define Julia types for each type constructor in the theory, e.g., object, morphism, and 2-morphism in the theory of 2-categories. Of course, Julia's type system does not support dependent types, so the type parameters are incorporated in the Julia types. (They are stored as extra data in the expression instances.)
The concrete types are structurally similar to the core type Expr
in Julia. However, the term constructor is represented as a type parameter, rather than as a head
field. This makes dispatch using Julia's type system more convenient.
sourceBase.nameof
— MethodGet name of GAT generator expression as a Symbol
.
If the generator has no name, returns nothing
.
sourceGATlab.Models.SymbolicModels.constructor_name
— MethodName of constructor that created expression.
sourceGATlab.Models.SymbolicModels.functor
— MethodFunctor from GAT expression to GAT instance.
Strictly speaking, we should call these "structure-preserving functors" or, better, "model homomorphisms of GATs". But this is a category theory library, so we'll go with the simpler "functor".
A functor is completely determined by its action on the generators. There are several ways to specify this mapping:
Specify a Julia instance type for each GAT type, using the required types
tuple. For this to work, the generator constructors must be defined for the instance types.
Explicitly map each generator term to an instance value, using the generators
dictionary.
For each GAT type (e.g., object and morphism), specify a function mapping generator terms of that type to an instance value, using the terms
dictionary.
The terms
dictionary can also be used for special handling of non-generator expressions. One use case for this capability is defining forgetful functors, which map non-generators to generators.
FIXME
sourceGATlab.Models.SymbolicModels.generator_like
— MethodCreate generator of the same type as the given expression.
sourceGATlab.Models.SymbolicModels.parse_json_sexpr
— MethodDeserialize expression from JSON-able S-expression.
If symbols
is true (the default), strings are converted to symbols.
sourceGATlab.Models.SymbolicModels.show_latex
— MethodShow the expression in infix notation using LaTeX math.
Does not include $
or \[begin|end]{equation}
delimiters.
sourceGATlab.Models.SymbolicModels.show_sexpr
— MethodShow the syntax expression as an S-expression.
Cf. the standard library function Meta.show_sexpr
.
sourceGATlab.Models.SymbolicModels.show_unicode
— MethodShow the expression in infix notation using Unicode symbols.
sourceGATlab.Models.SymbolicModels.symbolic_instance_methods
— MethodJulia function for every type constructor, accessor, and term constructor. Term constructors can be overwritten by overrides
.
sourceGATlab.Models.SymbolicModels.syntax_module
— MethodGet syntax module of given expression.
sourceGATlab.Models.SymbolicModels.to_json_sexpr
— MethodSerialize expression as JSON-able S-expression.
The format is an S-expression encoded as JSON, e.g., "compose(f,g)" is represented as ["compose", f, g].
sourceGATlab.Models.SymbolicModels.@symbolic_model
— Macro@symbolic_model generates the free model of a theory, generated by symbols.
This is backwards compatible with the @syntax macro in Catlab.
One way of thinking about this is that for every type constructor, we add an additional term constructor that builds a "symbolic" element of that type from any Julia value. This term constructor is called "generator".
An invocation of @symbolic_model
creates the following.
- A module with a struct for each type constructor, which has a single type
parameter T
and two fields, args
and type_args
. Instances of this struct are thought of as elements of the type given by the type constructor applied to type_args
. The type parameter refers to the term constructor that was used to generate the element, including the special term constructor :generator
, which has as its single argument an Any
.
For instance, in the theory of categories, we might have the following elements.
using .FreeCategory
x = Ob{:generator}([:x], [])
y = Ob{:generator}([:y], [])
f = Hom{:generator}([:f], [x, y])
@@ -109,4 +111,4 @@
function ThCategory.compose(f::FreeCategory.Hom, g::FreeCategory.Hom; strict=true)
associate_unit(FreeCategory.compose(f, g; strict), id)
-end
sourceUtilities
GATlab.Util.MetaUtils
— ModuleGeneral-purpose tools for metaprogramming in Julia.
sourceGATlab.Util.MetaUtils.JuliaFunctionSigNoWhere
— TypeFor comparing JuliaFunctionSigs modulo the where parameters
sourceGATlab.Util.MetaUtils.concat_expr
— MethodConcatenate two Julia expressions into a block expression.
sourceGATlab.Util.MetaUtils.generate_docstring
— MethodWrap Julia expression with docstring.
sourceGATlab.Util.MetaUtils.generate_function
— MethodGenerate Julia expression for function definition.
sourceGATlab.Util.MetaUtils.parse_docstring
— MethodParse Julia expression that is (possibly) annotated with docstring.
sourceGATlab.Util.MetaUtils.parse_function
— MethodParse Julia function definition into standardized form.
sourceGATlab.Util.MetaUtils.parse_function_sig
— MethodParse signature of Julia function.
sourceGATlab.Util.MetaUtils.replace_symbols
— MethodReplace symbols occuring anywhere in a Julia expression.
sourceGATlab.Util.MetaUtils.replace_symbols
— MethodReplace symbols occurring anywhere in a Julia function (except the name).
sourceGATlab.Util.MetaUtils.strip_lines
— MethodRemove all LineNumberNodes from a Julia expression.
sourceSettings
This document was generated with Documenter.jl version 1.2.1 on Wednesday 3 January 2024. Using Julia version 1.10.0.
+end
Utilities
GATlab.Util.MetaUtils
— ModuleGeneral-purpose tools for metaprogramming in Julia.
GATlab.Util.MetaUtils.JuliaFunctionSigNoWhere
— TypeFor comparing JuliaFunctionSigs modulo the where parameters
GATlab.Util.MetaUtils.concat_expr
— MethodConcatenate two Julia expressions into a block expression.
GATlab.Util.MetaUtils.generate_docstring
— MethodWrap Julia expression with docstring.
GATlab.Util.MetaUtils.generate_function
— MethodGenerate Julia expression for function definition.
GATlab.Util.MetaUtils.parse_docstring
— MethodParse Julia expression that is (possibly) annotated with docstring.
GATlab.Util.MetaUtils.parse_function
— MethodParse Julia function definition into standardized form.
GATlab.Util.MetaUtils.parse_function_sig
— MethodParse signature of Julia function.
GATlab.Util.MetaUtils.replace_symbols
— MethodReplace symbols occuring anywhere in a Julia expression.
GATlab.Util.MetaUtils.replace_symbols
— MethodReplace symbols occurring anywhere in a Julia function (except the name).
GATlab.Util.MetaUtils.strip_lines
— MethodRemove all LineNumberNodes from a Julia expression.