From 620f04a422ddeb6a36525f1df21b8ebfc21d3f3a Mon Sep 17 00:00:00 2001
From: Houtan Bastani <houtan@dynare.org>
Date: Wed, 13 Sep 2017 11:36:14 +0200
Subject: [PATCH] preprocessor: histval lag check must be made after
 transformation to the dynamic model due to ramsey_policy. closes #1193

---
 ComputingTasks.cc |  5 -----
 ModFile.cc        | 40 +++++++++++++++++-----------------------
 ParsingDriver.cc  |  2 +-
 Statement.hh      |  2 --
 4 files changed, 18 insertions(+), 31 deletions(-)

diff --git a/ComputingTasks.cc b/ComputingTasks.cc
index 3d97098f..9ff522f8 100644
--- a/ComputingTasks.cc
+++ b/ComputingTasks.cc
@@ -547,11 +547,6 @@ RamseyPolicyStatement::checkPass(ModFileStructure &mod_file_struct, WarningConso
       mod_file_struct.order_option = max(mod_file_struct.order_option, order + 1);
     }
 
-  OptionsList::symbol_list_options_t::const_iterator itsl =
-    options_list.symbol_list_options.find("instruments");
-  if (itsl != options_list.symbol_list_options.end())
-    mod_file_struct.ramsey_state_variables = itsl->second.get_symbols();
-
   // Fill in mod_file_struct.partial_information
   it = options_list.num_options.find("partial_information");
   if (it != options_list.num_options.end() && it->second == "1")
diff --git a/ModFile.cc b/ModFile.cc
index 32a72115..42fa7f64 100644
--- a/ModFile.cc
+++ b/ModFile.cc
@@ -149,29 +149,6 @@ ModFile::checkPass(bool nostrict, bool stochastic)
       exit(EXIT_FAILURE);
     }
 
-  // Workaround for #1193
-  if (!mod_file_struct.hist_vals_wrong_lag.empty())
-    {
-      for (vector<string>::const_iterator it = mod_file_struct.ramsey_state_variables.begin();
-           it != mod_file_struct.ramsey_state_variables.end(); it++)
-        {
-          int symb_id = symbol_table.getID(*it);
-          map<int,int>::const_iterator it1 = mod_file_struct.hist_vals_wrong_lag.find(symb_id);
-          if (it1 != mod_file_struct.hist_vals_wrong_lag.end() && it1->second < 0)
-            mod_file_struct.hist_vals_wrong_lag.erase(symb_id);
-        }
-
-      if (!mod_file_struct.hist_vals_wrong_lag.empty())
-        {
-          for (map<int, int>::const_iterator it = mod_file_struct.hist_vals_wrong_lag.begin();
-               it != mod_file_struct.hist_vals_wrong_lag.end(); it++)
-            cerr << "ERROR: histval: variable " << symbol_table.getName(it->first)
-                 << " does not appear in the model with the lag " << it->second
-                 << " (see the reference manual for the timing convention in 'histval')" << endl;
-          exit(EXIT_FAILURE);
-        }
-    }
-
   if ((mod_file_struct.ramsey_model_present || mod_file_struct.ramsey_policy_present)
       && mod_file_struct.discretionary_policy_present)
     {
@@ -417,6 +394,23 @@ ModFile::transformPass(bool nostrict, bool stochastic, bool compute_xrefs)
       mod_file_struct.ramsey_eq_nbr = dynamic_model.equation_number() - mod_file_struct.orig_eq_nbr;
     }
 
+  // Workaround for #1193
+  if (!mod_file_struct.hist_vals_wrong_lag.empty())
+    {
+      bool err = false;
+      for (map<int, int>::const_iterator it = mod_file_struct.hist_vals_wrong_lag.begin();
+           it != mod_file_struct.hist_vals_wrong_lag.end(); it++)
+          if (dynamic_model.minLagForSymbol(it->first) > it->second - 1)
+            {
+              cerr << "ERROR: histval: variable " << symbol_table.getName(it->first)
+                   << " does not appear in the model with the lag " << it->second - 1
+                   << " (see the reference manual for the timing convention in 'histval')" << endl;
+              err = true;
+            }
+      if (err)
+        exit(EXIT_FAILURE);
+    }
+
   if (mod_file_struct.stoch_simul_present
       || mod_file_struct.estimation_present
       || mod_file_struct.osr_present
diff --git a/ParsingDriver.cc b/ParsingDriver.cc
index 30c4336a..41972786 100644
--- a/ParsingDriver.cc
+++ b/ParsingDriver.cc
@@ -552,7 +552,7 @@ ParsingDriver::hist_val(string *name, string *lag, expr_t rhs)
   pair<int, int> key(symb_id, ilag);
 
   if (mod_file->dynamic_model.minLagForSymbol(symb_id) > ilag - 1)
-    hist_vals_wrong_lag[symb_id] = ilag-1;
+    hist_vals_wrong_lag[symb_id] = ilag;
 
   if (hist_values.find(key) != hist_values.end())
     error("hist_val: (" + *name + ", " + *lag + ") declared twice");
diff --git a/Statement.hh b/Statement.hh
index 70395a35..a6a1d5c3 100644
--- a/Statement.hh
+++ b/Statement.hh
@@ -123,8 +123,6 @@ public:
   bool steady_state_model_present;
   //! Whether there is a write_latex_steady_state_model statement present
   bool write_latex_steady_state_model_present;
-  //! Set list of variables that become state variables when ramsey_policy is used
-  vector<string> ramsey_state_variables;
   //! Histval values that do not have the appropriate lag
   map<int, int> hist_vals_wrong_lag;
 };
-- 
GitLab