Skip to content

Commit ec58c42

Browse files
authored
Fix substitute promotion (#86)
1 parent b7ec988 commit ec58c42

File tree

1 file changed

+72
-9
lines changed

1 file changed

+72
-9
lines changed

src/substitution.jl

Lines changed: 72 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,59 @@
11
const Substitution{Name} = Pair{<:Variable{Name}}
22

3-
## Variables
4-
MP.substitute(st::MP.AbstractSubstitutionType, v::Variable{Name}, s::Substitution{Name}) where {Name} = s.second
5-
MP.substitute(st::MP.AbstractSubstitutionType, v::Variable, s::Substitution) = v
6-
# subs(x, x=>y, y=>1) should be y, not 1 so as soon as we see the right variable, we stop, in subs(x, x=>y, x=>1), "x=>1" is ignored.
7-
MP.substitute(st::MP.AbstractSubstitutionType, v::Variable{Name}, s::Substitution{Name}, ::MP.AbstractSubstitution...) where {Name} = s.second
3+
function MA.promote_operation(
4+
::typeof(MP.substitute),
5+
::Type{<:MP.AbstractSubstitutionType},
6+
::Type{V},
7+
) where {V<:Variable}
8+
return V
9+
end
10+
11+
function MP.substitute(
12+
::MP.AbstractSubstitutionType,
13+
v::Variable,
14+
)
15+
return v
16+
end
17+
18+
function MA.promote_operation(
19+
::typeof(MP.substitute),
20+
::Type{<:MP.AbstractSubstitutionType},
21+
::Type{Variable{Name}},
22+
::Type{Pair{Variable{Name},T}},
23+
args::Vararg{Type,N}
24+
) where {Name,T,N}
25+
return T
26+
end
27+
28+
function MP.substitute(
29+
::MP.AbstractSubstitutionType,
30+
::Variable{Name},
31+
s::Substitution{Name},
32+
::Vararg{MP.AbstractSubstitution,N},
33+
) where {Name,N}
34+
return s.second
35+
end
36+
37+
function MA.promote_operation(
38+
::typeof(MP.substitute),
39+
S::Type{<:MP.AbstractSubstitutionType},
40+
::Type{V},
41+
::Type{<:Pair{<:Variable}},
42+
args::Vararg{Type,N}
43+
) where {V<:Variable,N}
44+
return MA.promote_operation(MP.substitute, S, V, args...)
45+
end
46+
47+
function MP.substitute(
48+
s::MP.AbstractSubstitutionType,
49+
v::Variable,
50+
::Substitution,
51+
args::Vararg{MP.AbstractSubstitution,N},
52+
) where {N}
53+
return MP.substitute(s, v, args...)
54+
end
55+
56+
# TODO remove below
857

958
_remove_variable(::Tuple{}, ::Type) = nothing
1059
function _remove_variable(t::Tuple{V,Vararg{Variable,N}}, ::Type{V}) where {V,N}
@@ -20,9 +69,13 @@ function _remove_variable(t::Tuple{V,Vararg{Variable,N}}, ::Type{W}) where {V,W,
2069
end
2170

2271
_mult_monomial_type(::Type{U}, ::Tuple{}) where {U} = U
23-
_mult_monomial_type(::Type{U}, V::Tuple) where {U} = MA.promote_operation(*, U, Monomial{V,length(V)})
72+
_mult_monomial_type(::Type{U}, V::Tuple) where {U} =
73+
MA.promote_operation(*, U, Monomial{V,length(V)})
2474

25-
function _promote_subs(::Type{Monomial{V,N}}, ::Type{Pair{Variable{Name},T}}) where {V,N,Name,T}
75+
function _promote_subs(
76+
::Type{Monomial{V,N}},
77+
::Type{Pair{Variable{Name},T}},
78+
) where {V,N,Name,T}
2679
U = MA.promote_operation(*, T, T)
2780
VV = _remove_variable(V, Variable{Name})
2881
if isnothing(VV)
@@ -33,10 +86,20 @@ function _promote_subs(::Type{Monomial{V,N}}, ::Type{Pair{Variable{Name},T}}) wh
3386
end
3487
end
3588

36-
function MA.promote_operation(::typeof(MP.substitute), ::Type{MP.Subs}, ::Type{Monomial{V,N}}, ::Type{Pair{Variable{Name},T}}) where {V,N,Name,T}
89+
function MA.promote_operation(
90+
::typeof(MP.substitute),
91+
::Type{MP.Subs},
92+
::Type{Monomial{V,N}},
93+
::Type{Pair{Variable{Name},T}},
94+
) where {V,N,Name,T}
3795
return _promote_subs(Monomial{V,N}, Pair{Variable{Name},T})
3896
end
3997

40-
function MA.promote_operation(::typeof(MP.substitute), ::Type{MP.Eval}, ::Type{Monomial{V,N}}, ::Type{Pair{Variable{Name},T}}) where {V,N,Name,T}
98+
function MA.promote_operation(
99+
::typeof(MP.substitute),
100+
::Type{MP.Eval},
101+
::Type{Monomial{V,N}},
102+
::Type{Pair{Variable{Name},T}},
103+
) where {V,N,Name,T}
41104
return _promote_subs(Monomial{V,N}, Pair{Variable{Name},T})
42105
end

0 commit comments

Comments
 (0)