diff --git a/src/DynamicModel.cc b/src/DynamicModel.cc index b3a7d690ae4d1f017db8ab3aa4df66cfcc218a84..2cb2c10893e25a99f77e148843a2a7c222e65d04 100644 --- a/src/DynamicModel.cc +++ b/src/DynamicModel.cc @@ -5579,6 +5579,13 @@ DynamicModel::substituteLeadLagInternal(AuxVarType type, bool deterministic_mode void DynamicModel::substituteAdl() { + /* Contrary to other substitution methods, we do the substitution in MLV + definitions here, instead of doing it at the ExprNode method level, + because otherwise this would substitute MLV in the original model (see + #65). */ + for (auto &[id, definition] : local_variables_table) + definition = definition->substituteAdl(); + for (auto &equation : equations) equation = dynamic_cast<BinaryOpNode *>(equation->substituteAdl()); } diff --git a/src/ExprNode.cc b/src/ExprNode.cc index 5d4180ca2401f4c64226856cd20e3c2ae87ddc94..9df220c2297ef53794fcaee7ada242682c78402d 100644 --- a/src/ExprNode.cc +++ b/src/ExprNode.cc @@ -1,5 +1,5 @@ /* - * Copyright © 2007-2020 Dynare Team + * Copyright © 2007-2021 Dynare Team * * This file is part of Dynare. * @@ -1541,9 +1541,8 @@ VariableNode::VarMaxLag(const set<expr_t> &lhs_lag_equiv) const expr_t VariableNode::substituteAdl() const { - if (get_type() == SymbolType::modelLocalVariable) - return datatree.getLocalVariable(symb_id)->substituteAdl(); - + /* Do not recurse into model-local variables definition, rather do it at the + DynamicModel method level (see the comment there) */ return const_cast<VariableNode *>(this); } @@ -1988,9 +1987,8 @@ VariableNode::getEndosAndMaxLags(map<string, int> &model_endos_and_lags) const expr_t VariableNode::replaceVarsInEquation(map<VariableNode *, NumConstNode *> &table) const { - if (get_type() == SymbolType::modelLocalVariable) - return datatree.getLocalVariable(symb_id)->replaceVarsInEquation(table); - + /* Do not recurse into model-local variables definition, because otherwise + this would substitute MLV in the original model (see #65). */ for (auto &it : table) if (it.first->symb_id == symb_id) return it.second;