Skip to content

Commit

Permalink
alias definitions in @instance rather than @theory
Browse files Browse the repository at this point in the history
fix
  • Loading branch information
Kris Brown committed Feb 7, 2025
1 parent a1c16f8 commit c4451ab
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 43 deletions.
15 changes: 12 additions & 3 deletions src/models/ModelInterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ function generate_instance(

# Parse the body into functions defined here and functions defined elsewhere
typechecked_functions = parse_instance_body(body, theory)

methodnames = Vector{Symbol}(nameof.(typechecked_functions))
# Adds keyword arguments to the functions, and qualifies them by
# `theory_module`, i.e. changes
# `Ob(x) = blah`
Expand All @@ -237,6 +237,9 @@ function generate_instance(
end
end

type_aliases, term_aliases = make_alias_definitions(
theory, theory_module, jltype_by_sort, model_type, whereparams, methodnames)

impl_type_declarations = if isnothing(model_type)
Expr[]
else
Expand All @@ -254,6 +257,8 @@ function generate_instance(
code = Expr(:block,
generate_function.(qualified_typecons)...,
generate_function.(qualified_functions)...,
generate_function.(type_aliases)...,
generate_function.(term_aliases)...,
runtime_impl_checks...,
impl_type_declarations...,
:(function $docsink end),
Expand Down Expand Up @@ -399,15 +404,19 @@ end
"""
Returns two lists of JuliaFunctions: one for aliases of type constructors, one
for aliases of term constructors.
Optional `methodnames` argument restricts aliases to only being generated if the
name they are an alias for is included in this list.
"""
function make_alias_definitions(theory, theory_module, jltype_by_sort, model_type, whereparams)
function make_alias_definitions(theory, theory_module, jltype_by_sort, model_type,
whereparams, methodnames=nothing)
typelines, termlines = [], []
oldinstance = isnothing(model_type)
for segment in theory.segments.scopes
for binding in segment
alias = getvalue(binding)
name = nameof(binding)
if alias isa Alias
if alias isa Alias && (isnothing(methodnames) || nameof(alias.ref) methodnames)
for (argsorts, method) in allmethods(theory.resolvers[alias.ref])
args = [(gensym(), jltype_by_sort[sort]) for sort in argsorts]
args = if oldinstance
Expand Down
2 changes: 1 addition & 1 deletion src/models/SymbolicModels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ macro symbolic_model(decl, theoryname, body)
theoryname,
Dict(sort => :($name.$(nameof(sort))) for sort in sorts(theory)),
nothing,
[]
[],
)

Expr(
Expand Down
41 changes: 2 additions & 39 deletions src/syntax/TheoryInterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,8 @@ function theory_impl(head, body, __module__)
:(Core.@__doc__ $(doctarget)),
:(
module $name
$(structlines...)
$(modulelines...)
$(make_alias_definitions(name, theory)...)
$(structlines...)
$(modulelines...)
end
),
:(@doc ($(Markdown.MD)($mdp(@doc $doctarget), $docstr)) $name)
Expand Down Expand Up @@ -414,40 +413,4 @@ parse_wrapper_input(n::Symbol) = n, Any
parse_wrapper_input(n::Expr) = n.head == :<: ? n.args : error("Bad input for wrapper")


"""
Automatically add statements like
```
ThCategory.→(m::WithModel, xs...; kw...) =
ThCategory.Hom(m, xs...; kw...)
```
"""
function make_alias_definitions(theory_module, theory)
lines = []
for segment in theory.segments.scopes
for binding in segment
alias = getvalue(binding)
name = nameof(binding)
if alias isa Alias
args = [Expr(:(::), :m, Expr(:., TheoryInterface, QuoteNode(:WithModel))),
Expr(:(...), :args)]
overload = JuliaFunction(;
name = :($theory_module.$name),
args,
kwargs = [Expr(:(...), :kwargs)],
whereparams = [],
impl = :($(nameof(alias.ref))(m, args...; kwargs...))
)
push!(lines, quote
if !hasmethod($theory_module.$name, ($(GlobalRef(TheoryInterface, :WithModel)),))
$(generate_function(overload))
end
end)
end
end
end
lines
end


end # module

0 comments on commit c4451ab

Please sign in to comment.