[When adding IOData, give all variables a new, unique name.
tobias@goedderz.info**20150213164747
 Ignore-this: 5fe929e5fcb80e702f15bf790b69731
] hunk ./src/Igor2/Data/IOData.hs 13
-import Igor2.Data.Rules
+import Igor2.Data.Rules hiding (mapRuleRhsM, mapRuleLhsM, mapRuleM)
+import qualified Igor2.Data.Rules as Rules (mapRuleM)
hunk ./src/Igor2/Data/IOData.hs 17
+import qualified Syntax
hunk ./src/Igor2/Data/IOData.hs 24
-import Data.Maybe (listToMaybe)
+import Data.Maybe (listToMaybe, maybe)
hunk ./src/Igor2/Data/IOData.hs 36
+
+import qualified Control.Monad.State as CMState
+
+import qualified Data.Traversable as T
+
hunk ./src/Igor2/Data/IOData.hs 47
-data IOData = IOD 
+data IOData = IOD
hunk ./src/Igor2/Data/IOData.hs 49
+    , next_var_num :: Int
hunk ./src/Igor2/Data/IOData.hs 76
+    , next_var_num = 0
hunk ./src/Igor2/Data/IOData.hs 99
-insertRules rs iod@(IOD{ evidence = bk }) = (n',iod')
+insertRules rs iod@(IOD{ evidence = bk }) = (n', iod')
hunk ./src/Igor2/Data/IOData.hs 104
-    n' = mkName $ (++) "fun" $ show $ M.size bk
-    iod' = insertUnsafe n' rs iod
+        (rs', new_next_var_num) = renameVars rs (next_var_num iod)
+        n' = mkName $ "fun" ++ show (M.size bk)
+        iod' = insertUnsafe n' rs' $ iod { next_var_num = new_next_var_num }
hunk ./src/Igor2/Data/IOData.hs 108
+type RenameState = (M.Map Name Name, Int)
+type RenameStateMonad = CMState.State RenameState
+
+-- Renames all variables in the first argument to var0, var1 etc., where i in
+-- vari begins with next_var_num. Returns the new Rules and the next unused i.
+renameVars :: Rules -> Int -> (Rules, Int)
+renameVars rs next_var_num = (new_rules, new_next_var_num)
+    where
+        (new_rules, (substitution, new_next_var_num)) = CMState.runState renameVarsM newRenameState
+
+        newRenameState = (M.empty, next_var_num)
+
+        renameVarsM :: RenameStateMonad (Set Rule)
+        renameVarsM = liftM S.fromList $ mapM (Rules.mapRuleM renameNode) $ S.toList rs
+
+        renameNode :: Syntax.TExp -> RenameStateMonad Syntax.TExp
+        renameNode (Syntax.TVarE name type') =
+            do (subst, next_var_num) <- get
+               let next_name = mkName ("var" ++ show next_var_num)
+               new_name <- maybe
+                   (put (M.insert name next_name subst, next_var_num + 1) >> return next_name)
+                   return
+                   (M.lookup name subst)
+               return (Syntax.TVarE new_name type')
+        renameNode node = return node