Skip to content

Commit

Permalink
Allow WithModel to take subtypes of model_type
Browse files Browse the repository at this point in the history
  • Loading branch information
Kris Brown committed Dec 8, 2024
1 parent 4c86234 commit be9b7d5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
4 changes: 2 additions & 2 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
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

0 comments on commit be9b7d5

Please sign in to comment.