Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow WithModel dispatch to accept subtypes #170

Merged
merged 1 commit into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/models/ModelInterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ function make_alias_definitions(theory, theory_module, jltype_by_sort, model_typ
args
end
else
[(gensym(:m), :($(TheoryInterface.WithModel){$model_type})); args]
[(gensym(:m), :($(TheoryInterface.WithModel){<:$model_type})); args]
end
argexprs = [Expr(:(::), p...) for p in args]
overload = JuliaFunction(;
Expand Down Expand Up @@ -560,7 +560,7 @@ function qualify_function(fun::JuliaFunction, theory_module, model_type::Union{E

m = gensym(:m)
(
[Expr(:(::), m, Expr(:curly, TheoryInterface.WithModel, model_type)), args...],
[Expr(:(::), m, Expr(:curly, TheoryInterface.WithModel, Expr(:<:, model_type))), args...],
Expr(:let, Expr(:(=), :model, :($m.model)), fun.impl)
)
else
Expand Down Expand Up @@ -649,6 +649,10 @@ function migrator(tmap, dom_module, codom_module, dom_theory, codom_theory)
v => whereparamdict[AlgSort(tmap(v.method).val)]
end)

whereparams2 = map(sorts(dom_theory)) do v
whereparamdict[AlgSort(tmap(v.method).val)]
end

# Create input for instance_code
################################
accessor_funs = JuliaFunction[] # added to during typecon_funs loop
Expand Down Expand Up @@ -726,11 +730,12 @@ function migrator(tmap, dom_module, codom_module, dom_theory, codom_theory)
)

tup_params = Expr(:curly, :Tuple, whereparams...)
tup_params2 = Expr(:curly, :Tuple, whereparams2...)

model_expr = Expr(
:curly,
GlobalRef(Syntax.TheoryInterface, :Model),
tup_params
tup_params2 # Types associated with *domain* sorts
)

# The second whereparams needs to be reordered by the sorts of the DOM theory
Expand Down
1 change: 1 addition & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[deps]
GATlab = "f0ffcf3b-d13a-433e-917c-cc44ccf5ead2"
Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a"
StructEquality = "6ec83bb0-ed9f-11e9-3b4c-2b04cb4e219c"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
20 changes: 20 additions & 0 deletions test/models/ModelInterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -195,5 +195,25 @@ end

@test h(false) == false

# Test models with abstract types
#################################

""" Assume this implements Base.iterate """
abstract type MyAbsIter{V} <: Model{Tuple{V}} end

struct MyVect{V} <: MyAbsIter{V}
v::Vector{V}
end

Base.iterate(m::MyVect, i...) = iterate(m.v, i...)

@instance ThSet{V} [model::MyAbsIter{V}] where V begin
default(v::V) = v ∈ model ? v : @fail "Bad $v not in $model"
end

@test implements(MyVect([1,2,3]), ThSet)

# this will fail unless WithModel accepts subtypes
@test ThSet.default[MyVect([1,2,3])](1) == 1

end # module
Loading