From f1fe52325ec4f9f980b877e219b626da6872ae46 Mon Sep 17 00:00:00 2001 From: Kris Brown Date: Tue, 8 Oct 2024 14:44:47 -0700 Subject: [PATCH 1/3] fix bug in migrations when there are different sorts --- src/models/ModelInterface.jl | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/models/ModelInterface.jl b/src/models/ModelInterface.jl index 6d008bab..d1b2d6e6 100644 --- a/src/models/ModelInterface.jl +++ b/src/models/ModelInterface.jl @@ -652,6 +652,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 @@ -729,11 +733,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 From 19d7d3733caac2a00a28838ec2a11e3d2479320c Mon Sep 17 00:00:00 2001 From: Kris Brown Date: Mon, 28 Oct 2024 11:25:44 -0700 Subject: [PATCH 2/3] Reindex argument names in migrated model typecon too --- src/models/ModelInterface.jl | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/models/ModelInterface.jl b/src/models/ModelInterface.jl index d1b2d6e6..ca22245f 100644 --- a/src/models/ModelInterface.jl +++ b/src/models/ModelInterface.jl @@ -494,7 +494,9 @@ function mk_fun(f::AlgFunction, theory, mod, jltype_by_sort) args = map(zip(f.args, sortsignature(f))) do (i,s) Expr(:(::),nameof(f[i]),jltype_by_sort[s]) end - impl = to_call_impl(f.value,theory, mod, false) + argnames = Vector{Symbol}(undef, length(getcontext(f))) + setindex!.(Ref(argnames), [nameof(f[i]) for i in f.args], getvalue.(f.args)) + impl = to_call_impl(f.value,theory, mod, argnames, false) JuliaFunction(;name=name, args, impl) end @@ -695,11 +697,13 @@ function migrator(tmap, dom_module, codom_module, dom_theory, codom_theory) args = [:($k::$(v)) for (k, v) in zip(argnames, sig.types)] return_type = first(sig.types) + argnames′ = Array{Symbol}(undef, length(getcontext(typecon))) + setindex!.(Ref(argnames′), argnames[2:end], getvalue.(typecon.args)) - impls = to_call_impl.(codom_body.args, Ref(codom_theory), Ref(codom_module), true) + impls = to_call_impl.(codom_body.args, Ref(codom_theory), Ref(codom_module), + Ref(argnames′), true) impl = Expr(:call, Expr(:ref, :($codom_module.$fxname), :(model.model)), _x, impls...) - JuliaFunction(;name, args, return_type, impl) end @@ -711,8 +715,11 @@ function migrator(tmap, dom_module, codom_module, dom_theory, codom_theory) name = nameof(termcon.declaration) return_type = jltype_by_sort[AlgSort(termcon.type)] - args = [:($k::$v) for (k, v) in zip(nameof.(argsof(termcon)), sig.types)] - impl = to_call_impl(fx.val, codom_theory, codom_module, true) + argnames = nameof.(argsof(termcon)) + argnames′ = Array{Symbol}(undef, length(getcontext(termcon))) + setindex!.(Ref(argnames′), argnames, getvalue.(termcon.args)) + args = [:($k::$v) for (k, v) in zip(argnames, sig.types)] + impl = to_call_impl(fx.val, codom_theory, codom_module, argnames′, true) JuliaFunction(;name, args, return_type, impl) end @@ -759,19 +766,19 @@ end Compile an AlgTerm into a Julia call Expr where termcons (e.g. `f`) are interpreted as `mod.f[model.model](...)`. """ -function to_call_impl(t::AlgTerm, theory::GAT, mod::Union{Symbol,Module}, migrate::Bool) +function to_call_impl(t::AlgTerm, theory::GAT, mod::Union{Symbol,Module}, argnames::Vector{Symbol}, migrate::Bool) b = bodyof(t) if GATs.isvariable(t) - nameof(b) + argnames[getvalue(getlid(b))] elseif GATs.isdot(t) - impl = to_call_impl(b.body, theory, mod, migrate) + impl = to_call_impl(b.body, theory, mod, argnames, migrate) if isnamed(b.head) Expr(:., impl, QuoteNode(nameof(b.head))) else Expr(:ref, impl, getlid(b.head).val) end else - args = to_call_impl.(argsof(b), Ref(theory), Ref(mod), migrate) + args = to_call_impl.(argsof(b), Ref(theory), Ref(mod), Ref(argnames), migrate) name = nameof(headof(b)) newhead = if name ∈ nameof.(structs(theory)) Expr(:., :($mod), QuoteNode(name)) From fb7579f06455adb935b74a6e0f742630ba0f0a68 Mon Sep 17 00:00:00 2001 From: Kris Brown Date: Wed, 20 Nov 2024 11:45:55 -0800 Subject: [PATCH 3/3] allow subtyping in WithModel --- src/models/ModelInterface.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/models/ModelInterface.jl b/src/models/ModelInterface.jl index ca22245f..952cfe25 100644 --- a/src/models/ModelInterface.jl +++ b/src/models/ModelInterface.jl @@ -519,7 +519,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(; @@ -565,7 +565,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