# Path: misc.py
# def flipSide(side):
# def _interleave(a, xs):
# 
# Path: calculus/symbol.py
# class Symbol:
# class StringSymbol(Symbol):
#   def __init__(self, type):
#   def _generate_id(self):
#   def __eq__(self, other):
#   def __ne__(self):
#   def __hash__(self):
#   def __repr__(self):
#   def __init__(self, s, type, infix = None):
#   def __repr__(self):
# 
# Path: calculus/variable.py
# class Variable(GeneralizedVariable):
#   def __init__(self):
#     self._generate_id()
# 
#   def _generate_id(self):
#     global n_variables
#     self._id = n_variables
#     n_variables += 1
# 
#   def updateVariables(self):
#     return Variable()
# 
#   def __eq__(self, other):
#     return self.__class__ == other.__class__ and self._id == other._id
#   def __ne__(self, other):
#     return not(self == other)
#   def __hash__(self):
#     return hash(self._id)
# 
#   def applied_variables(self):
#     return Set([])
# 
#   def __repr__(self):
#     return "<abstract variable %s>"%(self._id,)
# 
#   def substituteVariable(self, a, b):
#     if self == a:
#       return b
#     else:
#       return self
# 
#   def freeVariables(self):
#     return Set([self])
# 
# Path: lib/common_symbols.py



from misc import left, right
from calculus import symbol
from calculus.variable import Variable
from lib import common_symbols
from sets import Set
        right =self.right.substituteVariable(a, b))

  def compress(self):
    return self.rightAssociate()._compress_rightAssocitaed()

  # self must be right associated
  # return a right associated, compressed arrow equivalent to self.
  def _compress_rightAssocitaed(self):
    assert(self.left.__class__ != Composite)
    if self.right.__class__ == Composite:
      return Composite(self.left.compress(),
          self.right._compress_rightAssocitaed())._compress_rightAssociatedCompressed()
    else:
      return _compress2(self.left, self.right)

  # self must be right associated
  # self.left must be compressed
  # self.right must be compressed.
  # return a right associated, compressed arrow equivalent to self.
  def _compress_rightAssociatedCompressed(self):
    if self.right.__class__ == Composite:
      x = _compress2(self.left, self.right.left)
      if x.__class__ == Composite:
        # Can't compress further
        return self
      else:
        return Composite(x, self.right.right)._compress_rightAssociatedCompressed()
    else:
      return _compress2(self.left, self.right)


# Path: misc.py
# def flipSide(side):
# def _interleave(a, xs):
# 
# Path: calculus/symbol.py
# class Symbol:
# class StringSymbol(Symbol):
#   def __init__(self, type):
#   def _generate_id(self):
#   def __eq__(self, other):
#   def __ne__(self):
#   def __hash__(self):
#   def __repr__(self):
#   def __init__(self, s, type, infix = None):
#   def __repr__(self):
# 
# Path: calculus/variable.py
# class Variable(GeneralizedVariable):
#   def __init__(self):
#     self._generate_id()
# 
#   def _generate_id(self):
#     global n_variables
#     self._id = n_variables
#     n_variables += 1
# 
#   def updateVariables(self):
#     return Variable()
# 
#   def __eq__(self, other):
#     return self.__class__ == other.__class__ and self._id == other._id
#   def __ne__(self, other):
#     return not(self == other)
#   def __hash__(self):
#     return hash(self._id)
# 
#   def applied_variables(self):
#     return Set([])
# 
#   def __repr__(self):
#     return "<abstract variable %s>"%(self._id,)
# 
#   def substituteVariable(self, a, b):
#     if self == a:
#       return b
#     else:
#       return self
# 
#   def freeVariables(self):
#     return Set([self])
# 
# Path: lib/common_symbols.py



# Path: calculus/basic/formula.py
from misc import left, right
from calculus import symbol
from calculus.variable import Variable
from lib import common_symbols
from sets import Set

        right =self.right.substituteVariable(a, b))

  def compress(self):
    return self.rightAssociate()._compress_rightAssocitaed()

  # self must be right associated
  # return a right associated, compressed arrow equivalent to self.
  def _compress_rightAssocitaed(self):
    assert(self.left.__class__ != Composite)
    if self.right.__class__ == Composite:
      return Composite(self.left.compress(),
          self.right._compress_rightAssocitaed())._compress_rightAssociatedCompressed()
    else:
      return _compress2(self.left, self.right)

  # self must be right associated
  # self.left must be compressed
  # self.right must be compressed.
  # return a right associated, compressed arrow equivalent to self.
  def _compress_rightAssociatedCompressed(self):
    if self.right.__class__ == Composite:
      x = _compress2(self.left, self.right.left)
      if x.__class__ == Composite:
        # Can't compress further
        return self
      else:
        return Composite(x, self.right.right)._compress_rightAssociatedCompressed()
    else:
      return _compress2(self.left, self.right)


  def leftAssociate(self):