{-
Rename variables in a Forall expression that conflict with the given expression
which is about to be substituted.
Fixes the problems with assignment subsitution conflicting with foralls.
-}
fixForall :: Expression -> Expression -> Expression
fixForall subExp e = 
 case e of
  Forall (vn, vt) body ->
    if vn `occursIn` subExp
       --Recursively call to make sure our new name isn't also in the list
    then
      let
         genNewName n = 
           if n `occursIn` subExp
              then genNewName (ToName $ "freshVar" ++ fromName n)
              else n
         newName = genNewName vn
         newBody = subExpForName [vn] (EName newName) body
         ret = Forall (newName, vt) newBody
       in ret 
    else e 
  _ -> e

{-
Use Scrap-Your-Boilerplate to recursively apply our substutitions
to our expression, bottom-up
-}
fixForall :: Expression -> Expression -> Expression
fixForall subExp e = 
 case e of
  Forall (vn, vt) body ->
    if vn `occursIn` subExp
       --Recursively call to make sure our new name isn't also in the list
    then
      let
         genNewName n = 
           if n `occursIn` subExp
              then genNewName (ToName $ "freshVar" ++ fromName n)
              else n
         newName = genNewName vn
         newBody = subExpForName [vn] (EName newName) body
         ret = Forall (newName, vt) newBody
       in ret 
    else e 
  _ -> e

{-
Use Scrap-Your-Boilerplate to recursively apply our substutitions
to our expression, bottom-up
-}
fixForall subExp e = 
 case e of
  Forall (vn, vt) body ->
    if vn `occursIn` subExp
       --Recursively call to make sure our new name isn't also in the list
    then
      let
         genNewName n = 
           if n `occursIn` subExp
              then genNewName (ToName $ "freshVar" ++ fromName n)
              else n
         newName = genNewName vn
         newBody = subExpForName [vn] (EName newName) body
         ret = Forall (newName, vt) newBody
       in ret 
    else e 
  _ -> e

{-
Use Scrap-Your-Boilerplate to recursively apply our substutitions
to our expression, bottom-up
-}