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

typeparameters of Terms changes during substitution #699

Closed
hexaeder opened this issue Dec 28, 2020 · 2 comments · Fixed by #700
Closed

typeparameters of Terms changes during substitution #699

hexaeder opened this issue Dec 28, 2020 · 2 comments · Fixed by #700

Comments

@hexaeder
Copy link
Contributor

Simliar to #692, some substitutions seem to change the type of the expression. This leads to strange effects, for example back and forth substituting is not possible. I'm not quite sure whether this Issue should be in MTK or SymbolicUtils. I'm posting it here since I was not able to reproduce it with anything other than the MTK paramter type.

using ModelingToolkit
using SymbolicUtils: to_symbolic, substitute
@parameters t a(t) b(t)

# back and forth substitution does not work for parameters with dependencies
term = to_symbolic(a) # a(t)
term2 = substitute(term, a=>b) # b(t)
term3 = substitute(term2, b=>a) # b(t), should be a(t)

The only difference seems to be the type after substitution

@show typeof(to_symbolic(b)) # Term{ModelingToolkit.Parameter{Real}}
@show typeof(term2) # Term{Real}

However, the isequal function does not care, the pattern matching in SymbolicUtils seems to use something different here.

@show isequal(term2, to_symbolic(b)) # true

During exploration I found a weird hack for this problem but thats not usefull as an solution.

bnew = substitute(b, b=>b)
substitute(term2, bnew=>a) # a(t)
@ChrisRackauckas
Copy link
Member

Yes, this seems like a bug. But more generally, I think we need to be fixing up how we tag contextual information to symbols. I would propose using @MasonProtter's idea from

JuliaLang/julia#37790 (comment)

of how to do type tags in the current type system. Sym{ModelingToolkit.Parameter{Real}} is a non-extendable version of this, and interferes with the current rules handling. If it's always Sym{Real,{ModelingToolkit.Parameter}}, then the contextual information is all in extendable tags and the rules can be written to use the algebraic set of the first part.

Thoughts @YingboMa @shashi ?

@YingboMa
Copy link
Member

Differentiating similarterm and symtype will fix the wrapper issue. I.e. similarterm creates a term with the wrapper type, but symtype unwraps the type for value promotion. For nested wrappers, we can make more sophisticated latter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants