public static void undefineModule(Module oldmodule, Module newmodule) {
    System.out.println("Redefining the module `" + oldmodule.contextName() + "'");
    if (Stella.$SUBCONTEXT_REVISION_POLICY$ == Stella.KWD_DESTROY) {
      oldmodule.destroyContext();
      return;
    }
    else if (Stella.$SUBCONTEXT_REVISION_POLICY$ == Stella.KWD_PRESERVE) {
    }
    else if (Stella.$SUBCONTEXT_REVISION_POLICY$ == Stella.KWD_CLEAR) {
      { Context c = null;
        AllPurposeIterator iter000 = Context.allSubcontexts(oldmodule, Stella.KWD_PREORDER);

        while (iter000.nextP()) {
          c = ((Context)(iter000.value));
          Context.clearContext(c);
        }
      }
    }
    else {
      { OutputStringStream stream000 = OutputStringStream.newOutputStringStream();

        stream000.nativeStream.print("`" + Stella.$SUBCONTEXT_REVISION_POLICY$ + "' is not a valid case option");
        throw ((StellaException)(StellaException.newStellaException(stream000.theStringReader()).fillInStackTrace()));
      }
    }
    if (Module.cardinalModuleP(oldmodule)) {
      Stella.$ROOT_MODULE$.childContexts.remove(oldmodule);
    }
    else {
      { Module p = null;
        Cons iter001 = oldmodule.parentModules.theConsList;

        for (;!(iter001 == Stella.NIL); iter001 = iter001.rest) {
          p = ((Module)(iter001.value));
          p.childContexts.remove(oldmodule);
        }
      }
    }
    { Context c = null;
      Cons iter002 = oldmodule.childContexts.theConsList;

      for (;!(iter002 == Stella.NIL); iter002 = iter002.rest) {
        c = ((Context)(iter002.value));
        { Surrogate testValue000 = Stella_Object.safePrimaryType(c);

          if (Surrogate.subtypeOfP(testValue000, Stella.SGT_STELLA_MODULE)) {
            { Module c000 = ((Module)(c));

              c000.parentModules.theConsList.substitute(newmodule, oldmodule);
            }
          }
          else if (Surrogate.subtypeOfP(testValue000, Stella.SGT_STELLA_WORLD)) {
            { World c000 = ((World)(c));

              c000.parentContext = newmodule;
            }
          }
          else {
            { OutputStringStream stream001 = OutputStringStream.newOutputStringStream();

              stream001.nativeStream.print("`" + testValue000 + "' is not a valid case option");
              throw ((StellaException)(StellaException.newStellaException(stream001.theStringReader()).fillInStackTrace()));
            }
          }
        }
      }
    }
    newmodule.childContexts.theConsList = oldmodule.childContexts.theConsList;
    oldmodule.childContexts.theConsList = Stella.NIL;
    oldmodule.surrogateValueInverse = null;
    oldmodule.unfinalizeModule();
    oldmodule.free();
  }