import java.util.*;
import java.util.zip.*;
import java.util.List;
import java.util.regex.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import java.util.concurrent.locks.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.table.*;
import java.io.*;
import java.net.*;
import java.lang.reflect.*;
import java.lang.ref.*;
import java.lang.management.*;
import java.security.*;
import java.security.spec.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.imageio.*;
import java.math.*;


import javax.net.ssl.*;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.text.SimpleDateFormat;
import java.text.*;
import javax.imageio.metadata.*;
import javax.imageio.stream.*;
import java.text.NumberFormat;
import java.awt.datatransfer.StringSelection;

class main {

public static void main(final String[] args) throws Exception { pExperiment(); 
  FactsBot bot = new FactsBot();
}
static void pExperiment() {
  if (!isMain()) return;
  //substance();
  autoRestart(5);
  experimentIcon();
  centerHigherConsole();
  tt();
}


static boolean isMain() {
  return isMainProgram();
}
static double autoRestart_interval = 10;
static boolean autoRestart_on, autoRestart_debug, autoRestart_simulate;
static java.util.Timer autoRestart_timer;

static void autoRestart(double interval) {
  autoRestart_interval = interval;
  autoRestart();
}

static void autoRestart() {
  if (!isMain() || autoRestart_on) return;
  autoRestart_on = true;
  autoRestart_schedule();
  preloadProgramTitle();
}

static void autoRestart_off() {
  if (!autoRestart_on) return;
  stopTimer(autoRestart_timer);
  autoRestart_timer = null;
}

static void autoRestart_schedule() {
  autoRestart_timer = doLater_daemon(toMS(autoRestart_interval), "autoRestart_check");
}

static void autoRestart_check() {
  try {
    String newMD5;
    try {
      newMD5 = serverAutoRestartMD5(programID());
    } catch (Throwable e) {
      return;
    }
    if (!isMD5(newMD5)) { if (autoRestart_debug) print("autoRestart: no server transpilation"); return; }
    if (autoRestart_localMD5 == null)
      autoRestart_localMD5 = md5(loadCachedTranspilation(programID()));

    String localMD5 = autoRestart_localMD5();
    if (neq(localMD5, newMD5)) {
      if (autoRestart_simulate)
        print("Would upgrade now. " + localMD5 + " -> " + newMD5);
      else {
        infoBox("Upgrading " + programTitle());
        restartWithDelay(500);
        sleep();
      }
    }
  } finally {
    if (autoRestart_debug) print("autoRestart: Done");
    autoRestart_schedule();
  }
}

static void experimentIcon() {
  consoleAndDefaultFrameIcon("#1101173" /*#1101171*/);
}
static void centerHigherConsole() {
  if (headless()) return;
  setConsoleHeight(600);
  centerConsole();
}
static void tt() {
  typeWriterConsole();
}

static String tt(Object contents, Object... params) {
  return tag("tt", contents, params);
}


static boolean isMainProgram() {
  return creator() == null;
}
static void preloadProgramTitle() {
  { Thread _t_0 = new Thread() {
public void run() { try { programTitle(); } catch (Throwable __e) { _handleException(__e); } }
};
startThread(_t_0); }
}
static void stopTimer(java.util.Timer timer) {
  if (timer != null) timer.cancel();
}
static java.util.Timer doLater_daemon(long delay, final Object r) {
  final java.util.Timer timer = new java.util.Timer(true);
  timer.schedule(timerTask(r), delay);
  return timer;
}

static java.util.Timer doLater_daemon(double delaySeconds, final Object r) {
  return doLater_daemon(toMS(delaySeconds), r);
}
static long toMS(double seconds) {
  return (long) (seconds*1000);
}
static int serverAutoRestartMD5_timeout = 10000;

static String serverAutoRestartMD5(String programID) {
  return loadPageSilentlyWithTimeout("http://botcompany.de/1010693/raw?id=" + psI(programID), serverAutoRestartMD5_timeout);
}

static String serverAutoRestartMD5() {
  return serverAutoRestartMD5(programID());
}
static String programID() {
  return getProgramID();
}

static String programID(Object o) {
  return getProgramID(o);
}
static boolean isMD5(String s) {
  return l(s) == 32 && isLowerHexString(s);
}
static volatile StringBuffer local_log = new StringBuffer(); // not redirected
static volatile Appendable print_log = local_log; // might be redirected, e.g. to main bot

// in bytes - will cut to half that
static volatile int print_log_max = 1024*1024;
static volatile int local_log_max = 100*1024;
//static int print_maxLineLength = 0; // 0 = unset

static boolean print_silent; // total mute if set

static Object print_byThread_lock = new Object();
static volatile ThreadLocal<Object> print_byThread; // special handling by thread - prefers F1<S, Bool>
static volatile Object print_allThreads;

static void print() {
  print("");
}

static <A> A print(String s, A o) {
  print((endsWithLetterOrDigit(s) ? s + ": " : s) + o);
  return o;
}

// slightly overblown signature to return original object...
static <A> A print(A o) {
  ping();
  if (print_silent) return o;
  String s = String.valueOf(o) + "\n";
  print_noNewLine(s);
  return o;
}

static void print_noNewLine(String s) {
  
  Object f = print_byThread == null ? null : print_byThread.get();
  if (f == null) f = print_allThreads;
  if (f != null)
    // We do need the general callF machinery here as print_byThread is sometimes shared between modules
    if (isFalse(
      
        f instanceof F1 ? ((F1) f).get(s) :
      
      callF(f, s))) return;
  

  print_raw(s);
}

static void print_raw(String s) {
  s = fixNewLines(s);
  // TODO if (print_maxLineLength != 0)
  StringBuffer loc = local_log;
  Appendable buf = print_log;
  int loc_max = print_log_max;
  if (buf != loc && buf != null) {
    print_append(buf, s, print_log_max);
    loc_max = local_log_max;
  }
  if (loc != null) 
    print_append(loc, s, loc_max);
  System.out.print(s);
}
static String md5(String text) { try {
  if (text == null) return "-";
  return bytesToHex(md5_impl(text.getBytes("UTF-8"))); // maybe different than the way PHP does it...
} catch (Exception __e) { throw rethrow(__e); } }

static String md5(byte[] data) {
  if (data == null) return "-";
  return bytesToHex(md5_impl(data));
}

static MessageDigest md5_md;

/*static byte[] md5_impl(byte[] data) ctex {
  if (md5_md == null)
    md5_md = MessageDigest.getInstance("MD5");
  return ((MessageDigest) md5_md.clone()).digest(data);
}*/

static byte[] md5_impl(byte[] data) { try {
  return MessageDigest.getInstance("MD5").digest(data);
} catch (Exception __e) { throw rethrow(__e); } }

static String md5(File file) {
  return md5OfFile(file);
}
static String loadCachedTranspilation(String id) {
  return loadTextFilePossiblyGZipped(new File(getCodeProgramDir(id), "Transpilation"));
}

static String autoRestart_localMD5;

static String autoRestart_localMD5() { 
  if (autoRestart_localMD5 == null)
    autoRestart_localMD5 = md5(loadCachedTranspilation(programID()));
  return autoRestart_localMD5;
}
static boolean neq(Object a, Object b) {
  return !eq(a, b);
}
static JWindow infoBox(String text) {
  return infoMessage(text);
}

static JWindow infoBox(String text, double seconds) {
  return infoMessage(text, seconds);
}

static JWindow infoBox(Throwable e) {
  return infoMessage(e);
}
static String programTitle() {
  return getProgramName();
}
static void restartWithDelay(int delay) {
  Object j = getJavaX();
  call(j, "preKill");
  call(j, "nohupJavax", smartJoin((String[]) get(j, "fullArgs")), call(j, "fullVMArguments"));
  sleep(delay);
  System.exit(0);
  sleep();
}
static volatile boolean sleep_noSleep;

static void sleep(long ms) {
  ping();
  if (ms < 0) return;
  // allow spin locks
  if (isAWTThread() && ms > 100) throw fail("Should not sleep on AWT thread");
  try {
    Thread.sleep(ms);
  } catch (Exception e) { throw new RuntimeException(e); }
}

static void sleep() { try {
  if (sleep_noSleep) throw fail("nosleep");
  print("Sleeping.");
  sleepQuietly();
} catch (Exception __e) { throw rethrow(__e); } }
static void consoleAndDefaultFrameIcon(String icon) {
  consoleIcon(icon);
  setDefaultFrameIcon(icon);
}
static boolean headless() {
  return isHeadless();
}
static void setConsoleHeight(int h) {
  setFrameHeight(consoleFrame(), h);
}
static void centerConsole() {
  centerConsoleFrame();
}

static void centerConsole(int w, int h) {
  setConsoleSize(w, h);
  centerConsoleFrame();
}
static void typeWriterConsole() {
  
  if (isHeadless()) return;
  Font f = typeWriterFont();
  consoleFont(f);
  consoleInputFont(f);
  
}
static String tag(String tag) {
  return htag(tag);
}

static String tag(String tag, Object contents, Object... params) {
  return htag(tag, str(contents), params);
}

static String tag(String tag, StringBuilder contents, Object... params) {
  return htag(tag, contents, params);
}

static String tag(String tag, StringBuffer contents, Object... params) {
  return htag(tag, contents, params);
}


static WeakReference<Object> creator_class;

static Object creator() {
  return creator_class == null ? null : creator_class.get();
}
static volatile PersistableThrowable _handleException_lastException;
static List _handleException_onException = synchroList(ll("printStackTrace2"));

static void _handleException(Throwable e) {
  _handleException_lastException = persistableThrowable(e);
  
  Throwable e2 = innerException(e);
  if (e2.getClass() == RuntimeException.class && eq(e2.getMessage(), "Thread cancelled."))
    return;
    
  for (Object f : cloneList(_handleException_onException)) try {
    callF(f, e);
  } catch (Throwable e3) {
    printStackTrace2(e3); // not using pcall here - it could lead to endless loops
  }
}

static Thread startThread(Object runnable) {
  return startThread(defaultThreadName(), runnable);
}

static Thread startThread(String name, Object runnable) {
  runnable = wrapAsActivity(runnable);
  return startThread(newThread(toRunnable(runnable), name));
}

static Thread startThread(Thread t) {
  
  _registerThread(t);
  
  t.start();
  return t;
}
static TimerTask timerTask(final Object r, final java.util.Timer timer) {
  return new TimerTask() {
    public void run() {
      if (!licensed())
        timer.cancel();
      else
        pcallF(r);
    }
  };
}

static TimerTask timerTask(final Object r) {
  return new TimerTask() {
    public void run() {
      ping();
      pcallF(r);
    }
  };
}
static String loadPageSilentlyWithTimeout(String url, int timeout) { try {
  url = loadPage_preprocess(url);
  URL _url = new URL(url);
  return loadPage(
    setURLConnectionTimeouts(_url.openConnection(), timeout),
    _url);
} catch (Exception __e) { throw rethrow(__e); } }
static long psI(String snippetID) {
  return parseSnippetID(snippetID);
}
static String programID;

static String getProgramID() {
  return nempty(programID) ? formatSnippetIDOpt(programID) : "?";
}


// TODO: ask JavaX instead
static String getProgramID(Class c) {
  String id = (String) getOpt(c, "programID");
  if (nempty(id))
    return formatSnippetID(id);
  return "?";
}


static String getProgramID(Object o) {
  return getProgramID(getMainClass(o));
}
static int l(Object[] a) { return a == null ? 0 : a.length; }
static int l(boolean[] a) { return a == null ? 0 : a.length; }
static int l(byte[] a) { return a == null ? 0 : a.length; }
static int l(short[] a) { return a == null ? 0 : a.length; }
static int l(long[] a) { return a == null ? 0 : a.length; }
static int l(int[] a) { return a == null ? 0 : a.length; }
static int l(float[] a) { return a == null ? 0 : a.length; }
static int l(double[] a) { return a == null ? 0 : a.length; }
static int l(char[] a) { return a == null ? 0 : a.length; }
static int l(Collection c) { return c == null ? 0 : c.size(); }
static int l(Map m) { return m == null ? 0 : m.size(); }
static int l(CharSequence s) { return s == null ? 0 : s.length(); } static long l(File f) { return f == null ? 0 : f.length(); }

static int l(Object o) {
  return o == null ? 0
    : o instanceof String ? l((String) o)
    : o instanceof Map ? l((Map) o)
    : o instanceof Collection ? l((Collection) o)
    : o instanceof Object[] ? l((Object[]) o)
    : (Integer) call(o, "size");
}




static boolean isLowerHexString(String s) {
  for (int i = 0; i < l(s); i++) {
    char c = s.charAt(i);
    if (c >= '0' && c <= '9' || c >= 'a' && c <= 'f') {
      // ok
    } else
      return false;
  }
  return true;
}
static boolean endsWithLetterOrDigit(String s) {
  return s != null && s.length() > 0 && Character.isLetterOrDigit(s.charAt(s.length()-1));
}
//sbool ping_actions_shareable = true;
static volatile boolean ping_pauseAll;
static int ping_sleep = 100; // poll pauseAll flag every 100

static volatile boolean ping_anyActions;
static Map<Thread, Object> ping_actions = newWeakHashMap();


// always returns true
static boolean ping() {
  if (ping_pauseAll  || ping_anyActions ) ping_impl();
  //ifndef LeanMode ping_impl(); endifndef
  return true;
}

// returns true when it slept
static boolean ping_impl() { try {
  if (ping_pauseAll && !isAWTThread()) {
    do
      Thread.sleep(ping_sleep);
    while (ping_pauseAll);
    return true;
  }
  
  
  if (ping_anyActions) { // don't allow sharing ping_actions
    Object action = null;
    synchronized(ping_actions) {
      if (!ping_actions.isEmpty()) {
        action = ping_actions.get(currentThread());
        if (action instanceof Runnable)
          ping_actions.remove(currentThread());
        if (ping_actions.isEmpty()) ping_anyActions = false;
      }
    }
    
    if (action instanceof Runnable)
      ((Runnable) action).run();
    else if (eq(action, "cancelled"))
      throw fail("Thread cancelled.");
  }
  
  
  return false;
} catch (Exception __e) { throw rethrow(__e); } }
static boolean isFalse(Object o) {
  return eq(false, o);
}
static Map<Class, ArrayList<Method>> callF_cache = newDangerousWeakHashMap();


  static <A> A callF(F0<A> f) {
    return f == null ? null : f.get();
  }



  static <A, B> B callF(F1<A, B> f, A a) {
    return f == null ? null : f.get(a);
  }



  static <A, B, C> C callF(F2<A, B, C> f, A a, B b) {
    return f == null ? null : f.get(a, b);
  }



  static <A> void callF(VF1<A> f, A a) {
    if (f != null) f.get(a);
  }


static Object callF(Object f, Object... args) { try {
  if (f instanceof String)
    return callMC((String) f, args);
  if (f instanceof Runnable) {
    ((Runnable) f).run();
    return null;
  }
  if (f == null) return null;
  
  Class c = f.getClass();
  ArrayList<Method> methods;
  synchronized(callF_cache) {
    methods = callF_cache.get(c);
    if (methods == null)
      methods = callF_makeCache(c);
  }
  
  int n = l(methods);
  if (n == 0) {
    
    throw fail("No get method in " + getClassName(c));
  }
  if (n == 1) return invokeMethod(methods.get(0), f, args);
  for (int i = 0; i < n; i++) {
    Method m = methods.get(i);
    if (call_checkArgs(m, args, false))
      return invokeMethod(m, f, args);
  }
  throw fail("No matching get method in " + getClassName(c));
} catch (Exception __e) { throw rethrow(__e); } }

// used internally
static ArrayList<Method> callF_makeCache(Class c) {
  ArrayList<Method> l = new ArrayList();
  Class _c = c;
  do {
    for (Method m : _c.getDeclaredMethods())
      if (m.getName().equals("get")) {
        m.setAccessible(true);
        l.add(m);
      }
    if (!l.isEmpty()) break;
    _c = _c.getSuperclass();
  } while (_c != null);
  callF_cache.put(c, l);
  return l;
}
static String fixNewLines(String s) {
  int i = indexOf(s, '\r');
  if (i < 0) return s;
  int l = s.length();
  StringBuilder out = new StringBuilder(l);
  out.append(s, 0, i);
  for (; i < l; i++) {
    char c = s.charAt(i);
    if (c != '\r')
      out.append(c);
    else {
      out.append('\n');
      if (i+1 < l && s.charAt(i+1) == '\n') ++i;
    }
  }
  return out.toString();
}
static void print_append(Appendable _buf, String s, int max) { try {
  synchronized(_buf) {
    _buf.append(s);
    if (!(_buf instanceof StringBuilder)) return;
    StringBuilder buf = (StringBuilder) ( _buf);
    max /= 2;
    if (buf.length() > max) try {
      int newLength = max/2;
      int ofs = buf.length()-newLength;
      String newString = buf.substring(ofs);
      buf.setLength(0);
      buf.append("[...] ").append(newString);
    } catch (Exception e) {
      buf.setLength(0);
    }
  }
} catch (Exception __e) { throw rethrow(__e); } }
  public static String bytesToHex(byte[] bytes) {
    return bytesToHex(bytes, 0, bytes.length);
  }

  public static String bytesToHex(byte[] bytes, int ofs, int len) {
    StringBuilder stringBuilder = new StringBuilder(len*2);
    for (int i = 0; i < len; i++) {
      String s = "0" + Integer.toHexString(bytes[ofs+i]);
      stringBuilder.append(s.substring(s.length()-2, s.length()));
    }
    return stringBuilder.toString();
  }

static RuntimeException rethrow(Throwable t) {
  
  if (t instanceof Error)
    _handleError((Error) t);
  
  throw t instanceof RuntimeException ? (RuntimeException) t : new RuntimeException(t);
}
static String md5OfFile(File f) { try {
  if (!f.exists()) return "-";
  
  MessageDigest md5 = MessageDigest.getInstance("MD5");
   FileInputStream in = new FileInputStream(f); try {

  byte buf[] = new byte[65536];
  int l;
  while (true) {
    l = in.read(buf);
    if (l <= 0) break;
    md5.update(buf, 0, l);
  }
  
  return bytesToHex(md5.digest());
} finally { _close(in); }} catch (Exception __e) { throw rethrow(__e); } }
static String loadTextFilePossiblyGZipped(String fileName) {
  return loadTextFilePossiblyGZipped(fileName, null);
}
  
static String loadTextFilePossiblyGZipped(String fileName, String defaultContents) {
  File gz = new File(fileName + ".gz");
  return gz.exists() ? loadGZTextFile(gz) : loadTextFile(fileName, defaultContents);
}

static String loadTextFilePossiblyGZipped(File fileName) {
  return loadTextFilePossiblyGZipped(fileName, null);
}

static String loadTextFilePossiblyGZipped(File fileName, String defaultContents) {
  return loadTextFilePossiblyGZipped(fileName.getPath(), defaultContents);
}

static File getCodeProgramDir() {
  return getCodeProgramDir(getProgramID());
}

static File getCodeProgramDir(String snippetID) {
  return new File(javaxCodeDir(), formatSnippetID(snippetID));
}

static File getCodeProgramDir(long snippetID) {
  return getCodeProgramDir(formatSnippetID(snippetID));
}
static boolean eq(Object a, Object b) {
  return a == null ? b == null : a == b || a.equals(b);
}


static boolean infoMessage_alwaysOnTop = true;
static double infoMessage_defaultTime = 5.0;

// automatically switches to AWT thread for you
static JWindow infoMessage(String text) {
  return infoMessage(text, infoMessage_defaultTime);
}

static JWindow infoMessage(final String text, final double seconds) {
  printHidingCredentials(text);
  return infoMessage_noprint(text, seconds);
}

static JWindow infoMessage_noprint(String text) {
  return infoMessage_noprint(text, infoMessage_defaultTime);
}

static JWindow infoMessage_noprint(final String _text, final double seconds) {
  final String text = hideCredentials(_text);
  if (empty(text)) return null;
  logQuotedWithDate(infoBoxesLogFile(), text); 
  if (isHeadless()) return null;
  return (JWindow) swingAndWait(new F0<Object>() { Object get() { try { 
    final JWindow window = showWindow(infoMessage_makePanel(text));
    window.setSize(300, 150);
    moveToTopRightCorner(window);
    if (infoMessage_alwaysOnTop)
      window.setAlwaysOnTop(true);
    window.setVisible(true);
    disposeWindowAfter(iround(seconds*1000), window);
    return window;
   } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "final JWindow window = showWindow(infoMessage_makePanel(text));\r\n    window.s..."; }});
}

static JWindow infoMessage(Throwable e) {
  showConsole();
  printStackTrace(e);
  return infoMessage(exceptionToStringShort(e));
}
static String getProgramName_cache;

static String getProgramName() {
  Lock __78 = downloadLock(); lock(__78); try {
  if (getProgramName_cache == null)
    getProgramName_cache = getSnippetTitleOpt(programID());
  return getProgramName_cache;
} finally { unlock(__78); } }

static void _onLoad_getProgramName() {
  { Thread _t_0 = new Thread() {
public void run() { try { getProgramName(); } catch (Throwable __e) { _handleException(__e); } }
};
startThread(_t_0); }
}
static Class __javax;

static Class getJavaX() { try {
  
  return __javax;
} catch (Exception __e) { throw rethrow(__e); } }
// TODO: remove
static Object call(Object o) {
  return callF(o);
}

// varargs assignment fixer for a single string array argument
static Object call(Object o, String method, String[] arg) {
  return call(o, method, new Object[] {arg});
}

static Object call(Object o, String method, Object... args) { try {
  if (o == null) return null;
  if (o instanceof Class) {
    Method m = call_findStaticMethod((Class) o, method, args, false);
    m.setAccessible(true);
    return invokeMethod(m, null, args);
  /*} else if (o instanceof DynamicMethods) {
    ret ((DynamicMethods) o)._dynCall(method, args);*/
  } else {
    Method m = call_findMethod(o, method, args, false);
    m.setAccessible(true);
    return invokeMethod(m, o, args);
  }
} catch (Exception __e) { throw rethrow(__e); } }

static Method call_findStaticMethod(Class c, String method, Object[] args, boolean debug) {
  Class _c = c;
  while (c != null) {
    for (Method m : c.getDeclaredMethods()) {
      if (debug)
        System.out.println("Checking method " + m.getName() + " with " + m.getParameterTypes().length + " parameters");;
      if (!m.getName().equals(method)) {
        if (debug) System.out.println("Method name mismatch: " + method);
        continue;
      }

      if ((m.getModifiers() & java.lang.reflect.Modifier.STATIC) == 0 || !call_checkArgs(m, args, debug))
        continue;

      return m;
    }
    c = c.getSuperclass();
  }
  throw new RuntimeException("Method '" + method + "' (static) with " + args.length + " parameter(s) not found in " + _c.getName());
}

static Method call_findMethod(Object o, String method, Object[] args, boolean debug) {
  Class c = o.getClass();
  while (c != null) {
    for (Method m : c.getDeclaredMethods()) {
      if (debug)
        System.out.println("Checking method " + m.getName() + " with " + m.getParameterTypes().length + " parameters");;
      if (m.getName().equals(method) && call_checkArgs(m, args, debug))
        return m;
    }
    c = c.getSuperclass();
  }
  throw new RuntimeException("Method '" + method + "' (non-static) with " + args.length + " parameter(s) not found in " + o.getClass().getName());
}
// Try to get the quoting right...

static String smartJoin(String[] args) {
  if (args.length == 1) return args[0];
  
  String[] a = new String[args.length];
  for (int i = 0; i < a.length; i++)
    a[i] = !isJavaIdentifier(args[i]) && !isQuoted(args[i]) ? quote(args[i]) : args[i];
  return join(" ", a);
}

static String smartJoin(List<String> args) {
  return smartJoin(toStringArray(args));
}
// get purpose 1: access a list/array/map (safer version of x.get(y))

static <A> A get(List<A> l, int idx) {
  return l != null && idx >= 0 && idx < l(l) ? l.get(idx) : null;
}

// seems to conflict with other signatures
/*static <A, B> B get(Map<A, B> map, A key) {
  ret map != null ? map.get(key) : null;
}*/

static <A> A get(A[] l, int idx) {
  return idx >= 0 && idx < l(l) ? l[idx] : null;
}

// default to false
static boolean get(boolean[] l, int idx) {
  return idx >= 0 && idx < l(l) ? l[idx] : false;
}

// get purpose 2: access a field by reflection or a map

static Object get(Object o, String field) {
  try {
    if (o == null) return null;
    if (o instanceof Class) return get((Class) o, field);
    
    if (o instanceof Map)
      return ((Map) o).get(field);
      
    Field f = getOpt_findField(o.getClass(), field);
    if (f != null) {
      f.setAccessible(true);
      return f.get(o);
    }
      
    
      if (o instanceof DynamicObject)
        return ((DynamicObject) o).fieldValues.get(field);
    
  } catch (Exception e) {
    throw asRuntimeException(e);
  }
  throw new RuntimeException("Field '" + field + "' not found in " + o.getClass().getName());
}

static Object get_raw(Object o, String field) {
  try {
    Field f = get_findField(o.getClass(), field);
    f.setAccessible(true);
    return f.get(o);
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
}

static Object get(Class c, String field) {
  try {
    Field f = get_findStaticField(c, field);
    f.setAccessible(true);
    return f.get(null);
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
}

static Field get_findStaticField(Class<?> c, String field) {
  Class _c = c;
  do {
    for (Field f : _c.getDeclaredFields())
      if (f.getName().equals(field) && (f.getModifiers() & java.lang.reflect.Modifier.STATIC) != 0)
        return f;
    _c = _c.getSuperclass();
  } while (_c != null);
  throw new RuntimeException("Static field '" + field + "' not found in " + c.getName());
}

static Field get_findField(Class<?> c, String field) {
  Class _c = c;
  do {
    for (Field f : _c.getDeclaredFields())
      if (f.getName().equals(field))
        return f;
    _c = _c.getSuperclass();
  } while (_c != null);
  throw new RuntimeException("Field '" + field + "' not found in " + c.getName());
}

// TODO: test if android complains about this
static boolean isAWTThread() {
  if (isAndroid()) return false;
  if (isHeadless()) return false;
  return isAWTThread_awt();
}

static boolean isAWTThread_awt() {
  return SwingUtilities.isEventDispatchThread();
}
static RuntimeException fail() { throw new RuntimeException("fail"); }
static RuntimeException fail(Throwable e) { throw asRuntimeException(e); }
static RuntimeException fail(Object msg) { throw new RuntimeException(String.valueOf(msg)); }
static RuntimeException fail(String msg) { throw new RuntimeException(msg == null ? "" : msg); }
static RuntimeException fail(String msg, Throwable innerException) { throw new RuntimeException(msg, innerException); }

static Object sleepQuietly_monitor = new Object();

static void sleepQuietly() { try {
  assertFalse(isAWTThread());
  synchronized(sleepQuietly_monitor) { sleepQuietly_monitor.wait(); }
} catch (Exception __e) { throw rethrow(__e); } }
static void consoleIcon(String imageID) {
  frameIcon(consoleFrame(), or2(imageID, javaxDefaultIcon()));
}
static void setDefaultFrameIcon(String icon) {
  makeFrame_defaultIcon = icon;
}
static Boolean isHeadless_cache;

static boolean isHeadless() {
  if (isHeadless_cache != null) return isHeadless_cache;
  if (isAndroid()) return isHeadless_cache = true;
  if (GraphicsEnvironment.isHeadless()) return isHeadless_cache = true;
  
  // Also check if AWT actually works.
  // If DISPLAY variable is set but no X server up, this will notice.
  
  try {
    SwingUtilities.isEventDispatchThread();
    return isHeadless_cache = false;
  } catch (Throwable e) { return isHeadless_cache = true; }
}
static JFrame setFrameHeight(JFrame frame, int h) {
  frame.setSize(frame.getWidth(), h);
  return frame;
}

static JFrame setFrameHeight(int h, JFrame frame) {
  return setFrameHeight(frame, h);
}

static JFrame consoleFrame() {
  return (JFrame) getOpt(get(getJavaX(), "console"), "frame");
}
static void centerConsoleFrame() {
  centerFrame(consoleFrame());
}
static void setConsoleSize(int w, int h) {
  setFrameSize(consoleFrame(), w, h);
}
static Font typeWriterFont() {
  return typeWriterFont(iround(14*getSwingFontScale()));
}
  
static Font typeWriterFont(int size) {
  return new Font("Courier", Font.PLAIN, size);
}
static void consoleFont(Font font) {
  callOpt(getConsoleTextArea_gen(), "setFont", font);
}
static void consoleInputFont(final Font f) {
  { swing(new Runnable() {  public void run() { try { 
    JTextField input = consoleInputField();
    if (input != null) {
      input.setFont(f);
      revalidateFrame(input);
    }
  
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "JTextField input = consoleInputField();\r\n    if (input != null) {\r\n      inpu..."; }}); }
}
static String htag(String tag) {
  return htag(tag, "");
}

static String htag(String tag, Object contents, Object... params) {
  String openingTag = hopeningTag(tag, params);
  String s = str(contents);
  if (empty(s) && neqic(tag, "script"))
    return dropLast(openingTag) + "/>";
  return openingTag + s + "</" + tag + ">";
}
static String str(Object o) {
  return o == null ? "null" : o.toString();
}

static String str(char[] c) {
  return new String(c);
}


static Throwable printStackTrace2(Throwable e) {
  // we go to system.out now - system.err is nonsense
  print(getStackTrace2(e));
  return e;
}

static void printStackTrace2() {
  printStackTrace2(new Throwable());
}

static void printStackTrace2(String msg) {
  printStackTrace2(new Throwable(msg));
}

static String makeFrame_defaultIcon;
static boolean makeFrame_hideConsole;
static ThreadLocal < VF1 < JFrame > > makeFrame_post = new ThreadLocal();

static JFrame makeFrame() {
  return makeFrame((Component) null);
}

static JFrame makeFrame(Object content) {
  return makeFrame(programTitle(), content);
}

static JFrame makeFrame(String title) {
  return makeFrame(title, null);
}

static JFrame makeFrame(String title, Object content) {
  return makeFrame(title, content, true);
}

static JFrame makeFrame(final String title, final Object content, final boolean showIt) {
  final VF1<JFrame> post = optParam(makeFrame_post);
  return swing(new F0<JFrame>() { JFrame get() { try { 
    if (getFrame(content) != null)
      return getFrame(setFrameTitle((Component) content, title));
    final JFrame frame = new JFrame(title);
    if (makeFrame_defaultIcon != null)
      setFrameIconLater(frame, makeFrame_defaultIcon);
    _initFrame(frame);
    Component wrapped = wrap(content);
    if (wrapped != null)
      frame.getContentPane().add(wrapped);
    frame.setBounds(defaultNewFrameBounds());
    
    callF(post, frame);
    
    if (showIt)
      frame.setVisible(true);
    //callOpt(content, "requestFocus");
    //exitOnFrameClose(frame);
    
    if (showIt && makeFrame_hideConsole) {
      hideConsole();
      makeFrame_hideConsole = false;
    }
    
    return frame;
   } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "if (getFrame(content) != null)\r\n      ret getFrame(setFrameTitle((Component) ..."; }});
}
static <A> List<A> synchroList() {
  return Collections.synchronizedList(new ArrayList<A>());
}

static <A> List<A> synchroList(List<A> l) {
  return Collections.synchronizedList(l);
}

static <A> List<A> ll(A... a) {
  ArrayList l = new ArrayList(a.length);
  for (A x : a) l.add(x);
  return l;
}
static PersistableThrowable persistableThrowable(Throwable e) {
  return e == null ? null : new PersistableThrowable(e);
}
static Throwable innerException(Throwable e) {
  return getInnerException(e);
}
static <A> ArrayList<A> cloneList(Iterable<A> l) {
  return l instanceof Collection ? cloneList((Collection) l) : asList(l);
}

static <A> ArrayList<A> cloneList(Collection<A> l) {
  if (l == null) return new ArrayList();
  synchronized(collectionMutex(l)) {
    return new ArrayList<A>(l);
  }
}
static String defaultThreadName_name;

static String defaultThreadName() {
  if (defaultThreadName_name == null)
    defaultThreadName_name = "A thread by " + programID();
  return defaultThreadName_name;
}
static Runnable wrapAsActivity(Object r) {
  return toRunnable(r);
}
// runnable = Runnable or String (method name)
static Thread newThread(Object runnable) {
  return new Thread(_topLevelErrorHandling(toRunnable(runnable)));
}

static Thread newThread(Object runnable, String name) {
  if (name == null) name = defaultThreadName();
  return new Thread(_topLevelErrorHandling(toRunnable(runnable)), name);
}

static Thread newThread(String name, Object runnable) {
  return newThread(runnable, name);
}
static Runnable toRunnable(final Object o) {
  if (o instanceof Runnable) return (Runnable) o;
  return new Runnable() {  public void run() { try {  callF(o) ;
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "callF(o)"; }};
}
static Map<Thread, Boolean> _registerThread_threads;
static Object _onRegisterThread; // voidfunc(Thread)

static Thread _registerThread(Thread t) {
  if (_registerThread_threads == null)
    _registerThread_threads = newWeakHashMap();
  _registerThread_threads.put(t, true);
  vm_generalWeakSubMap("thread2mc").put(t, weakRef(mc()));
  callF(_onRegisterThread, t);
  return t;
}

static void _registerThread() { _registerThread(Thread.currentThread()); }
static volatile boolean licensed_yes = true;

static boolean licensed() {
  ping();
  return licensed_yes;
}

static void licensed_off() {
  licensed_yes = false;
}
static Object pcallF(Object f, Object... args) {
  return pcallFunction(f, args);
}


static <A> A pcallF(F0<A> f) { try {
  return f == null ? null : f.get();
} catch (Throwable __e) { return null; } }



static <A, B> B pcallF(F1<A, B> f, A a) { try {
  return f == null ? null : f.get(a);
} catch (Throwable __e) { return null; } }

static int loadPage_defaultTimeout = 60000;
static ThreadLocal<String> loadPage_charset = new ThreadLocal();
static boolean loadPage_allowGzip = true, loadPage_debug;
static boolean loadPage_anonymous; // don't send computer ID
static int loadPage_verboseness = 100000;
static int loadPage_retries = 1; //60; // seconds
static ThreadLocal<Boolean> loadPage_silent = new ThreadLocal();
static volatile int loadPage_forcedTimeout; // ms
static ThreadLocal<Integer> loadPage_forcedTimeout_byThread = new ThreadLocal(); // ms
static ThreadLocal<Map<String, List<String>>> loadPage_responseHeaders = new ThreadLocal();
static ThreadLocal<Map<String, String>> loadPage_extraHeaders = new ThreadLocal();

public static String loadPageSilently(String url) { try {
  return loadPageSilently(new URL(loadPage_preprocess(url)));
} catch (Exception __e) { throw rethrow(__e); } }

public static String loadPageSilently(URL url) { try {
  if (url.getProtocol().equals("https"))
    disableCertificateValidation();
    
  if (!networkAllowanceTest(str(url))) throw fail("Not allowed: " + url);
    
  IOException e = null;
  for (int tries = 0; tries < loadPage_retries; tries++)
    try {
      URLConnection con = loadPage_openConnection(url);
      return loadPage(con, url);
    } catch (IOException _e) {
      e = _e;
      if (loadPageThroughProxy_enabled) {
        print("Trying proxy because of: " + e);
        try {
          return loadPageThroughProxy(str(url));
        } catch (Throwable e2) {
          print("  " + exceptionToStringShort(e2));
        }
      } else if (loadPage_debug)
        print(exceptionToStringShort(e));
      if (tries < loadPage_retries-1) sleepSeconds(1);
    }
  throw e;
} catch (Exception __e) { throw rethrow(__e); } }

static String loadPage_preprocess(String url) {  
  if (url.startsWith("tb/")) // don't think we use this anymore
    url = tb_mainServer() + "/" + url;
  if (url.indexOf("://") < 0)
    url = "http://" + url;
  return url;
}

static String loadPage(String url) { try {
  url = loadPage_preprocess(url);
  if (!isTrue(loadPage_silent.get()))
    printWithTime("Loading: " + hideCredentials(url));
  return loadPageSilently(new URL(url));
} catch (Exception __e) { throw rethrow(__e); } }

static String loadPage(URL url) {
  return loadPage(url.toExternalForm());
}

static String loadPage(URLConnection con, URL url) throws IOException {
  return loadPage(con, url, true);
}

static String loadPage(URLConnection con, URL url, boolean addHeaders) throws IOException {
  Map<String, String> extraHeaders = getAndClearThreadLocal(loadPage_extraHeaders);
  if (addHeaders) try {
    if (!loadPage_anonymous)
      setHeaders(con);
    if (loadPage_allowGzip)
      con.setRequestProperty("Accept-Encoding", "gzip");
    con.setRequestProperty("X-No-Cookies", "1");
    for (String key : keys(extraHeaders))
      con.setRequestProperty(key, extraHeaders.get(key));
  } catch (Throwable e) {} // fails if within doPost
  
  vm_generalSubMap("URLConnection per thread").put(currentThread(), con);
  loadPage_responseHeaders.set(con.getHeaderFields());
  InputStream in = null;
  try {
    in = con.getInputStream();
  //vm_generalSubMap("InputStream per thread").put(currentThread(), in);
  if (loadPage_debug)
    print("Put stream in map: " + currentThread());
    String contentType = con.getContentType();
    if (contentType == null) {
      //printStruct("Headers: ", con.getHeaderFields());
      throw new IOException("Page could not be read: " + hideCredentials(url));
    }
    //print("Content-Type: " + contentType);
    String charset = loadPage_charset == null ? null : loadPage_charset.get();
    if (charset == null) charset = loadPage_guessCharset(contentType);
    
    if ("gzip".equals(con.getContentEncoding())) {
      if (loadPage_debug)
        print("loadPage: Using gzip.");
      in = newGZIPInputStream(in);
    }
    Reader r;
    try {
      r = new InputStreamReader(in, unquote(charset));
    } catch (UnsupportedEncodingException e) {
      print(toHex(utf8(charset)));
      throw e;
    }
    
    StringBuilder buf = new StringBuilder();
    int n = 0;
    while (true) {
      int ch = r.read();
      if (ch < 0)
        break;
      buf.append((char) ch);
      ++n;
      if ((n % loadPage_verboseness) == 0) print("  " + n + " chars read");
    }
    return buf.toString();
  } finally {
    if (loadPage_debug)
      print("loadPage done");
    //vm_generalSubMap("InputStream per thread").remove(currentThread());
    vm_generalSubMap("URLConnection per thread").remove(currentThread());
    if (in != null) in.close();
  }
}

static String loadPage_guessCharset(String contentType) {
  Pattern p = Pattern.compile("text/[a-z]+;\\s*charset=([^\\s]+)\\s*");
  Matcher m = p.matcher(contentType);
  String match = m.matches() ? m.group(1) : null;
  if (loadPage_debug)
    print("loadPage: contentType=" + contentType + ", match: " + match);
  /* If Content-Type doesn't match this pre-conception, choose default and hope for the best. */
  //return or(match, "ISO-8859-1");
  return or(match, "UTF-8");
}

static URLConnection loadPage_openConnection(URL url) {
  URLConnection con = openConnection(url);
  int timeout = toInt(loadPage_forcedTimeout_byThread.get());
  if (timeout == 0) timeout = loadPage_forcedTimeout;
  if (timeout != 0)
    setURLConnectionTimeouts(con, loadPage_forcedTimeout);
  else
    setURLConnectionDefaultTimeouts(con, loadPage_defaultTimeout);
  return con;
}
static URLConnection setURLConnectionTimeouts(URLConnection con, long timeout) {
  con.setConnectTimeout(toInt(timeout));
  con.setReadTimeout(toInt(timeout));
  if (con.getConnectTimeout() != timeout || con.getReadTimeout() != timeout)
    print("Warning: Timeouts not set by JDK.");
  return con;
}
public static long parseSnippetID(String snippetID) {
  long id = Long.parseLong(shortenSnippetID(snippetID));
  if (id == 0) throw fail("0 is not a snippet ID");
  return id;
}
static boolean nempty(Collection c) {
  return !isEmpty(c);
}

static boolean nempty(CharSequence s) {
  return !isEmpty(s);
}

static boolean nempty(Object[] o) { return !isEmpty(o); }
static boolean nempty(byte[] o) { return !isEmpty(o); }

static boolean nempty(Map m) {
  return !isEmpty(m);
}

static boolean nempty(Iterator i) {
  return i != null && i.hasNext();
}

static boolean nempty(Object o) { return !empty(o); }
static String formatSnippetIDOpt(String s) {
  return isSnippetID(s) ? formatSnippetID(s) : s;
}
static Object getOpt(Object o, String field) {
  return getOpt_cached(o, field);
}

static Object getOpt_raw(Object o, String field) {
  try {
    Field f = getOpt_findField(o.getClass(), field);
    if (f == null) return null;
    f.setAccessible(true);
    return f.get(o);
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
}

// access of static fields is not yet optimized
static Object getOpt(Class c, String field) {
  try {
    if (c == null) return null;
    Field f = getOpt_findStaticField(c, field);
    if (f == null) return null;
    f.setAccessible(true);
    return f.get(null);
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
}

static Field getOpt_findStaticField(Class<?> c, String field) {
  Class _c = c;
  do {
    for (Field f : _c.getDeclaredFields())
      if (f.getName().equals(field) && (f.getModifiers() & java.lang.reflect.Modifier.STATIC) != 0)
        return f;
    _c = _c.getSuperclass();
  } while (_c != null);
  return null;
}

static String formatSnippetID(String id) {
  return "#" + parseSnippetID(id);
}

static String formatSnippetID(long id) {
  return "#" + id;
}
static Class getMainClass() {
  return mc();
}

static Class getMainClass(Object o) { try {
  if (o == null) return null;
  if (o instanceof Class && eq(((Class) o).getName(), "x30")) return (Class) o;
  return (o instanceof Class ? (Class) o : o.getClass()).getClassLoader().loadClass("main");
} catch (Exception __e) { throw rethrow(__e); } }
static <A, B> Map<A, B> newWeakHashMap() {
  return _registerWeakMap(synchroMap(new WeakHashMap()));
}
static Thread currentThread() {
  return Thread.currentThread();
}
static <A, B> Map<A, B> newDangerousWeakHashMap() {
  return _registerDangerousWeakMap(synchroMap(new WeakHashMap()));
}

// initFunction: voidfunc(Map) - is called initially, and after clearing the map
static <A, B> Map<A, B> newDangerousWeakHashMap(Object initFunction) {
  return _registerDangerousWeakMap(synchroMap(new WeakHashMap()), initFunction);
}
static HashMap<String, List<Method>> callMC_cache = new HashMap();
static String callMC_key;
static Method callMC_value;

// varargs assignment fixer for a single string array argument
static Object callMC(String method, String[] arg) {
  return callMC(method, new Object[] {arg});
}

static Object callMC(String method, Object... args) { try {
  Method me;
  if (callMC_cache == null) callMC_cache = new HashMap(); // initializer time workaround
  synchronized(callMC_cache) {
    me = method == callMC_key ? callMC_value : null;
  }
  if (me != null) try {
    return invokeMethod(me, null, args);
  } catch (IllegalArgumentException e) {
    throw new RuntimeException("Can't call " + me + " with arguments " + classNames(args), e);
  }

  List<Method> m;
  synchronized(callMC_cache) {
    m = callMC_cache.get(method);
  }
  if (m == null) {
    if (callMC_cache.isEmpty()) {
      callMC_makeCache();
      m = callMC_cache.get(method);
    }
    if (m == null) throw fail("Method named " + method + " not found in main");
  }
  int n = m.size();
  if (n == 1) {
    me = m.get(0);
    synchronized(callMC_cache) {
      callMC_key = method;
      callMC_value = me;
    }
    try {
      return invokeMethod(me, null, args);
    } catch (IllegalArgumentException e) {
      throw new RuntimeException("Can't call " + me + " with arguments " + classNames(args), e);
    }
  }
  for (int i = 0; i < n; i++) {
    me = m.get(i);
    if (call_checkArgs(me, args, false))
      return invokeMethod(me, null, args);
  }
  throw fail("No method called " + method + " with matching arguments found in main");
} catch (Exception __e) { throw rethrow(__e); } }

static void callMC_makeCache() {
  synchronized(callMC_cache) {
    callMC_cache.clear();
    Class _c = (Class) mc(), c = _c;
    while (c != null) {
      for (Method m : c.getDeclaredMethods())
        if ((m.getModifiers() & java.lang.reflect.Modifier.STATIC) != 0) {
          m.setAccessible(true);
          multiMapPut(callMC_cache, m.getName(), m);
        }
      c = c.getSuperclass();
    }
  }
}
static String getClassName(Object o) {
  return o == null ? "null" : o instanceof Class ? ((Class) o).getName() : o.getClass().getName();
}
static Object invokeMethod(Method m, Object o, Object... args) { try {
  try {
    return m.invoke(o, args);
  } catch (InvocationTargetException e) {
    throw rethrow(getExceptionCause(e));
  } catch (IllegalArgumentException e) {
    throw new IllegalArgumentException(e.getMessage() + " - was calling: " + m + ", args: " + joinWithSpace(classNames(args)));
  }
} catch (Exception __e) { throw rethrow(__e); } }
static boolean call_checkArgs(Method m, Object[] args, boolean debug) {
  Class<?>[] types = m.getParameterTypes();
  if (types.length != args.length) {
    if (debug)
      System.out.println("Bad parameter length: " + args.length + " vs " + types.length);
    return false;
  }
  for (int i = 0; i < types.length; i++)
    if (!(args[i] == null || isInstanceX(types[i], args[i]))) {
      if (debug)
        System.out.println("Bad parameter " + i + ": " + args[i] + " vs " + types[i]);
      return false;
    }
  return true;
}
static <A> int indexOf(List<A> l, A a, int startIndex) {
  if (l == null) return -1;
  for (int i = startIndex; i < l(l); i++)
    if (eq(l.get(i), a))
      return i;
  return -1;
}

static <A> int indexOf(List<A> l, int startIndex, A a) {
  return indexOf(l, a, startIndex);
}

static <A> int indexOf(List<A> l, A a) {
  if (l == null) return -1;
  return l.indexOf(a);
}

static int indexOf(String a, String b) {
  return a == null || b == null ? -1 : a.indexOf(b);
}

static int indexOf(String a, String b, int i) {
  return a == null || b == null ? -1 : a.indexOf(b, i);
}

static int indexOf(String a, char b) {
  return a == null ? -1 : a.indexOf(b);
}

static int indexOf(String a, int i, char b) {
  return indexOf(a, b, i);
}

static int indexOf(String a, char b, int i) {
  return a == null ? -1 : a.indexOf(b, i);
}

static int indexOf(String a, int i, String b) {
  return a == null || b == null ? -1 : a.indexOf(b, i);
}

static <A> int indexOf(A[] x, A a) {
  if (x == null) return -1;
  for (int i = 0; i < l(x); i++)
    if (eq(x[i], a))
      return i;
  return -1;
}
static void _handleError(Error e) {
  call(javax(), "_handleError", e);
}
static void _close(AutoCloseable c) { try {
  if (c != null) c.close();
} catch (Exception __e) { throw rethrow(__e); } }
static String loadGZTextFile(File file) { try {
  if (!file.isFile()) return null;
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  InputStream fis = new FileInputStream(file);
  GZIPInputStream gis = newGZIPInputStream(fis);
  try {
    byte[] buffer = new byte[1024];
    int len;
    while((len = gis.read(buffer)) != -1){
        baos.write(buffer, 0, len);
    }
  } finally {
    fis.close();
  }
  baos.close();
  return fromUtf8(baos.toByteArray()); // TODO: use a Reader
} catch (Exception __e) { throw rethrow(__e); } }
static String loadTextFile(String fileName) {
  return loadTextFile(fileName, null);
}

static String loadTextFile(File f, String defaultContents) { try {
  checkFileNotTooBigToRead(f);
  if (f == null || !f.exists()) return defaultContents;

  FileInputStream fileInputStream = new FileInputStream(f);
  InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
  return loadTextFile(inputStreamReader);
} catch (Exception __e) { throw rethrow(__e); } }

public static String loadTextFile(File fileName) {
  return loadTextFile(fileName, null);
}

static String loadTextFile(String fileName, String defaultContents) {
  return fileName == null ? defaultContents : loadTextFile(newFile(fileName), defaultContents);
}

static String loadTextFile(Reader reader) throws IOException {
  StringBuilder builder = new StringBuilder();
  try {
    char[] buffer = new char[1024];
    int n;
    while (-1 != (n = reader.read(buffer)))
      builder.append(buffer, 0, n);
  } finally {
    reader.close();
  }
  return str(builder);
}
static File javaxCodeDir_dir; // can be set to work on different base dir

static File javaxCodeDir() {
  return javaxCodeDir_dir != null ? javaxCodeDir_dir : new File(userHome(), "JavaX-Code");
}
static <A> A printHidingCredentials(A o) {
  print(hideCredentials(str(o)));
  return o;
}
static String hideCredentials(URL url) { return url == null ? null : hideCredentials(str(url)); }

static String hideCredentials(String url) {
  return url.replaceAll("([&?])(_pass|key)=[^&\\s\"]*", "$1$2=<hidden>");
}

static String hideCredentials(Object o) {
  return hideCredentials(str(o));
}
static boolean empty(Collection c) { return c == null || c.isEmpty(); }
static boolean empty(CharSequence s) { return s == null || s.length() == 0; }
static boolean empty(Map map) { return map == null || map.isEmpty(); }
static boolean empty(Object[] o) { return o == null || o.length == 0; }
static boolean empty(Object o) {
  if (o instanceof Collection) return empty((Collection) o);
  if (o instanceof String) return empty((String) o);
  if (o instanceof Map) return empty((Map) o);
  if (o instanceof Object[]) return empty((Object[]) o);
  if (o instanceof byte[]) return empty((byte[]) o);
  if (o == null) return true;
  throw fail("unknown type for 'empty': " + getType(o));
}

static boolean empty(float[] a) { return a == null || a.length == 0; }
static boolean empty(int[] a) { return a == null || a.length == 0; }
static boolean empty(long[] a) { return a == null || a.length == 0; }
static boolean empty(byte[] a) { return a == null || a.length == 0; }


static void logQuotedWithDate(String s) {
  logQuotedWithTime(s);
}

static void logQuotedWithDate(String logFile, String s) {
  logQuotedWithTime(logFile, s);
}

static void logQuotedWithDate(File logFile, String s) {
  logQuotedWithTime(logFile, s);
}
static File infoBoxesLogFile() {
  return new File(javaxDataDir(), "Logs/infoBoxes.txt");
}
static void swingAndWait(Runnable r) { try {
  if (isAWTThread())
    r.run();
  else
    EventQueue.invokeAndWait(addThreadInfoToRunnable(r));
} catch (Exception __e) { throw rethrow(__e); } }

static Object swingAndWait(final Object f) {
  if (isAWTThread())
    return callF(f);
  else {
    final Var result = new Var();
    swingAndWait(new Runnable() {  public void run() { try { 
      result.set(callF(f));
    
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "result.set(callF(f));"; }});
    return result.get();
  }
}
static JWindow showWindow(Component c) {
  JWindow w = new JWindow();
  w.add(wrap(c));
  return w;
}
static JPanel infoMessage_makePanel(String text) {
  final JTextArea ta = wrappedTextArea(text);
  onClick(ta, new Runnable() {  public void run() { try {  disposeWindow(ta) ;
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "disposeWindow(ta)"; }});
  int size = 14;
  if (l(text) <= 50) size *= 2;
  else if (l(text) < 100) size = iround(size*1.5);
  ta.setFont(typeWriterFont(size));
  JScrollPane sp = jscroll(ta);
  return withMargin(sp);
}
static int moveToTopRightCorner_inset = 20;

static <A extends Component> A moveToTopRightCorner(A a) {
  return moveToTopRightCorner(moveToTopRightCorner_inset, moveToTopRightCorner_inset, a);
}

static <A extends Component> A moveToTopRightCorner(int insetX, int insetY, A a) {
  Window w = getWindow(a);
  if (w != null)
    w.setLocation(getScreenSize().width-w.getWidth()-insetX, insetY);
  return a;
}

static <A extends Window> A disposeWindowAfter(int delay, final A w) {
  if (w != null)
    swingLater(delay, new Runnable() {  public void run() { try { 
      w.dispose();
    
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "w.dispose();"; }});
  return w;
}

static <A extends Window> A disposeWindowAfter(A w, double seconds) {
  return disposeWindowAfter(toMS_int(seconds), w);
}
static int iround(double d) {
  return (int) Math.round(d);
}
static void showConsole() {
  callOpt(get(javax(), "console"), "showConsole");
}
static Throwable printStackTrace(Throwable e) {
  // we go to system.out now - system.err is nonsense
  print(getStackTrace(e));
  return e;
}

static void printStackTrace() {
  printStackTrace(new Throwable());
}

static void printStackTrace(String msg) {
  printStackTrace(new Throwable(msg));
}
static String exceptionToStringShort(Throwable e) {
  lastException(e);
  e = getInnerException(e);
  String msg = hideCredentials(unnull(e.getMessage()));
  if (msg.indexOf("Error") < 0 && msg.indexOf("Exception") < 0)
    return baseClassName(e) + prependIfNempty(": ", msg);
  else
    return msg;
}
static Lock downloadLock_lock = fairLock();

static Lock downloadLock() {
  return downloadLock_lock;
}
static void lock(Lock lock) { try {
  ping();
  if (lock == null) return;
  try {
    lock.lockInterruptibly();
  } catch (InterruptedException e) {
    print("Locking interrupted! I probably deadlocked, oops.");
    printStackTrace(e);
    rethrow(e);
  }
  ping();
} catch (Exception __e) { throw rethrow(__e); } }

static void lock(Lock lock, String msg) {
  print("Locking: " + msg);
  lock(lock);
}

static void lock(Lock lock, String msg, long timeout) {
  print("Locking: " + msg);
  lockOrFail(lock, timeout);
}

static ReentrantLock lock() {
  return fairLock();
}
static String getSnippetTitleOpt(String s) {
  return isSnippetID(s) ? getSnippetTitle(s) : s;
}
static void unlock(Lock lock, String msg) {
  if (lock == null) return;
  print("Unlocking: " + msg);
  lock.unlock();
}

static void unlock(Lock lock) {
  if (lock == null) return;
  lock.unlock();
}
static boolean isJavaIdentifier(String s) {
  if (empty(s) || !Character.isJavaIdentifierStart(s.charAt(0)))
    return false;
  for (int i = 1; i < s.length(); i++)
    if (!Character.isJavaIdentifierPart(s.charAt(i)))
      return false;
  return true;
}
// supports the usual quotings (", variable length double brackets) except ' quoting
static boolean isQuoted(String s) {
  
  
  if (isNormalQuoted(s)) return true; // use the exact version
  
  return isMultilineQuoted(s);
}
static String quote(Object o) {
  if (o == null) return "null";
  return quote(str(o));
}

static String quote(String s) {
  if (s == null) return "null";
  StringBuilder out = new StringBuilder((int) (l(s)*1.5+2));
  quote_impl(s, out);
  return out.toString();
}
  
static void quote_impl(String s, StringBuilder out) {
  out.append('"');
  int l = s.length();
  for (int i = 0; i < l; i++) {
    char c = s.charAt(i);
    if (c == '\\' || c == '"')
      out.append('\\').append(c);
    else if (c == '\r')
      out.append("\\r");
    else if (c == '\n')
      out.append("\\n");
    else if (c == '\0')
      out.append("\\0");
    else
      out.append(c);
  }
  out.append('"');
}
public static String join(String glue, Iterable<String> strings) {
  if (strings == null) return "";
  StringBuilder buf = new StringBuilder();
  Iterator<String> i = strings.iterator();
  if (i.hasNext()) {
    buf.append(i.next());
    while (i.hasNext())
      buf.append(glue).append(i.next());
  }
  return buf.toString();
}

public static String join(String glue, String... strings) {
  return join(glue, Arrays.asList(strings));
}

static String join(Iterable<String> strings) {
  return join("", strings);
}

static String join(Iterable<String> strings, String glue) {
  return join(glue, strings);
}

public static String join(String[] strings) {
  return join("", strings);
}


static String join(String glue, Pair p) {
  return p == null ? "" : str(p.a) + glue + str(p.b);
}

static String[] toStringArray(Collection<String> c) {
  String[] a = new String[l(c)];
  Iterator<String> it = c.iterator();
  for (int i = 0; i < l(a); i++)
    a[i] = it.next();
  return a;
}

static String[] toStringArray(Object o) {
  if (o instanceof String[])
    return (String[]) o;
  else if (o instanceof Collection)
    return toStringArray((Collection<String>) o);
  else
    throw fail("Not a collection or array: " + getClassName(o));
}

static Field getOpt_findField(Class<?> c, String field) {
  Class _c = c;
  do {
    for (Field f : _c.getDeclaredFields())
      if (f.getName().equals(field))
        return f;
    _c = _c.getSuperclass();
  } while (_c != null);
  return null;
}
static RuntimeException asRuntimeException(Throwable t) {
  
  if (t instanceof Error)
    _handleError((Error) t);
  
  return t instanceof RuntimeException ? (RuntimeException) t : new RuntimeException(t);
}
static int isAndroid_flag;

static boolean isAndroid() {
  if (isAndroid_flag == 0)
    isAndroid_flag = System.getProperty("java.vendor").toLowerCase().indexOf("android") >= 0 ? 1 : -1;
  return isAndroid_flag > 0;
}

static void assertFalse(Object o) {
  if (!(eq(o, false) /*|| isFalse(pcallF(o))*/))
    throw fail(str(o));
}
  
static boolean assertFalse(boolean b) {
  if (b) throw fail("oops");
  return b;
}

static boolean assertFalse(String msg, boolean b) {
  if (b) throw fail(msg);
  return b;
}

static Component frameIcon(Component c, String imageID) {
  setFrameIconLater(c, imageID);
  return c;
}

static Component frameIcon(String imageID, Component c) {
  setFrameIconLater(c, imageID);
  return c;
}
static String or2(String a, String b) {
  return nempty(a) ? a : b;
}

static String or2(String a, String b, String c) {
  return or2(or2(a, b), c);
}
static String javaxDefaultIcon() {
  return "#1005557";
}
static <A extends Component> A centerFrame(A c) {
  Window w = getWindow(c);
  if (w != null)
    w.setLocationRelativeTo(null); // magic trick
  return c;
}

static <A extends Component> A centerFrame(int w, int h, A c) {
  return centerFrame(setFrameSize(w, h, c));
}

static <A extends Component> A setFrameSize(A c, int w, int h) {
  JFrame f = getFrame(c);
  if (f != null)
    f.setSize(w, h);
  return c;
}

static <A extends Component> A setFrameSize(int w, int h, A c) {
  return setFrameSize(c, w, h);
}
static float getSwingFontScale() {
  return or((Float) vm_generalMap_get("swingFontScale_value"), 1f);
}
static Object callOpt(Object o) {
  if (o == null) return null;
  return callF(o);
}

static Object callOpt(Object o, String method, Object... args) {
  try {
    if (o == null) return null;
    if (o instanceof Class) {
      Method m = callOpt_findStaticMethod((Class) o, method, args, false);
      if (m == null) return null;
      m.setAccessible(true);
      return invokeMethod(m, null, args);
    /*} else if (o instanceof DynamicMethods) {
      ret ((DynamicMethods) o)._dynCall(method, args);*/
    } else {
      Method m = callOpt_findMethod(o, method, args, false);
      if (m == null) return null;
      m.setAccessible(true);
      return invokeMethod(m, o, args);
    }
  } catch (Exception e) {
    //fail(e.getMessage() + " | Method: " + method + ", receiver: " + className(o) + ", args: (" + join(", ", map(f className, args) + ")");
    throw rethrow(e);
  }
}

static Method callOpt_findStaticMethod(Class c, String method, Object[] args, boolean debug) {
  Class _c = c;
  while (c != null) {
    for (Method m : c.getDeclaredMethods()) {
      if (debug)
        System.out.println("Checking method " + m.getName() + " with " + m.getParameterTypes().length + " parameters");;
      if (!m.getName().equals(method)) {
        if (debug) System.out.println("Method name mismatch: " + method);
        continue;
      }

      if ((m.getModifiers() & java.lang.reflect.Modifier.STATIC) == 0 || !callOpt_checkArgs(m, args, debug))
        continue;

      return m;
    }
    c = c.getSuperclass();
  }
  return null;
}

static Method callOpt_findMethod(Object o, String method, Object[] args, boolean debug) {
  Class c = o.getClass();
  while (c != null) {
    for (Method m : c.getDeclaredMethods()) {
      if (debug)
        System.out.println("Checking method " + m.getName() + " with " + m.getParameterTypes().length + " parameters");;
      if (m.getName().equals(method) && callOpt_checkArgs(m, args, debug))
        return m;
    }
    c = c.getSuperclass();
  }
  return null;
}

private static boolean callOpt_checkArgs(Method m, Object[] args, boolean debug) {
  Class<?>[] types = m.getParameterTypes();
  if (types.length != args.length) {
    if (debug)
      System.out.println("Bad parameter length: " + args.length + " vs " + types.length);
    return false;
  }
  for (int i = 0; i < types.length; i++)
    if (!(args[i] == null || isInstanceX(types[i], args[i]))) {
      if (debug)
        System.out.println("Bad parameter " + i + ": " + args[i] + " vs " + types[i]);
      return false;
    }
  return true;
}


static Object getConsoleTextArea_gen() {
  return getOpt(get(getJavaX(), "console"), "textArea");
}
static Object swing(Object f) {
  return swingAndWait(f);
}

static <A> A swing(F0<A> f) {
  return (A) swingAndWait(f);
}
static JTextField consoleInputField() {
  Object console = get(getJavaX(), "console");
  return (JTextField) getOpt(console, "tfInput");
}
static void revalidateFrame(Component c) {
  revalidate(getFrame(c));
}
static String hopeningTag(String tag, Map params) {
  return hopeningTag(tag, mapToParams(params));
}

static String hopeningTag(String tag, Object... params) {
  StringBuilder buf = new StringBuilder();
  buf.append("<" + tag);
  for (int i = 0; i < l(params); i += 2) {
    String name = (String) get(params, i);
    Object val = get(params, i+1);
    if (nempty(name) && val != null) {
      String s = str(val);
      if (!empty(s))
        buf.append(" " + name + "=" + htmlQuote(s));
    }
  }
  buf.append(">");
  return str(buf);
}
static boolean neqic(String a, String b) {
  return !eqic(a, b);
}
static String[] dropLast(String[] a, int n) {
  n = Math.min(n, a.length);
  String[] b = new String[a.length-n];
  System.arraycopy(a, 0, b, 0, b.length);
  return b;
}

static <A> List<A> dropLast(List<A> l) {
  return subList(l, 0, l(l)-1);
}

static <A> List<A> dropLast(int n, List<A> l) {
  return subList(l, 0, l(l)-n);
}

static <A> List<A> dropLast(Iterable<A> l) {
  return dropLast(asList(l));
}

static String dropLast(String s) {
  return substring(s, 0, l(s)-1);
}

static String dropLast(String s, int n) {
  return substring(s, 0, l(s)-n);
}

static String dropLast(int n, String s) {
  return dropLast(s, n);
}



static String getStackTrace2(Throwable e) {
  return hideCredentials(getStackTrace(unwrapTrivialExceptionWraps(e)) + replacePrefix("java.lang.RuntimeException: ", "FAIL: ",
    hideCredentials(str(getInnerException(e)))) + "\n");
}
static <A> A optParam(ThreadLocal<A> tl, A defaultValue) {
  return optPar(tl, defaultValue);
}

static <A> A optParam(ThreadLocal<A> tl) {
  return optPar(tl);
}

static <A> A optParam(Object[] opt, String name, A defaultValue) {
  if (!even(l(opt))) throw fail("Odd parameter length");
  for (int i = 0; i < l(opt); i += 2)
    if (eq(opt[i], name))
      return (A) opt[i+1];
  return defaultValue;
}

static <A> A optParam(Object[] opt, String name) {
  return optParam(opt, name, null);
}
static JFrame getFrame(final Object _o) {
  return swing(new F0<JFrame>() { JFrame get() { try { 
    Object o = _o;
    if (o instanceof ButtonGroup) o = first(buttonsInGroup((ButtonGroup) o));
    if (!(o instanceof Component)) return null;
    Component c = (Component) o;
    while (c != null) {
      if (c instanceof JFrame) return (JFrame) c;
      c = c.getParent();
    }
    return null;
   } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "O o = _o;\r\n    if (o instanceof ButtonGroup) o = first(buttonsInGroup((Button..."; }});
}
static <A> A setFrameTitle(A c, final String title) {
  final Frame f = getAWTFrame(c);
  if (f != null) { swing(new Runnable() {  public void run() { try {  f.setTitle(title); 
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "f.setTitle(title);"; }}); }
  return c;
}

static <A extends Component> A setFrameTitle(String title, A c) {
  return setFrameTitle(c, title);
}

// magically find a field called "frame" in main class :-)
static JFrame setFrameTitle(String title) {
  Object f = getOpt(mc(), "frame");
  if (f instanceof JFrame)
    return setFrameTitle((JFrame) f, title);
  return null;
}
static JFrame setFrameIconLater(Component c, final String imageID) {
  final JFrame frame = getFrame(c);
  if (frame != null)
    { Thread _t_0 = new Thread("Loading Icon") {
public void run() { try {
  
      final Image i = imageIcon(or2(imageID, "#1005557")).getImage();
      swingLater(new Runnable() {  public void run() { try { 
        frame.setIconImage(i);
      
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "frame.setIconImage(i);"; }});
    } catch (Throwable __e) { _handleException(__e); } }
};
startThread(_t_0); }
  return frame;
}
static void _initFrame(JFrame f) {
  myFrames_list.put(f, Boolean.TRUE);
  standardTitlePopupMenu(f);
}
// c = Component or something implementing swing()
static Component wrap(Object swingable) {
  return _recordNewSwingComponent(wrap_2(swingable));
}

static Component wrap_2(Object swingable) {
  if (swingable == null) return null;
  Component c;
  if (swingable instanceof Component) c = (Component) swingable;
  
  else c = (Component) callOpt(swingable, "swing");
  if (c instanceof JTable || c instanceof JList
    || c instanceof JTextArea || c instanceof JEditorPane
    || c instanceof JTextPane || c instanceof JTree)
    return jscroll(c);
  return c == null ? jlabel(str(swingable)) : c;
}
static Rectangle defaultNewFrameBounds_r = new Rectangle(300, 100, 500, 400);

static Rectangle defaultNewFrameBounds() {
  return swing(new F0<Rectangle>() { Rectangle get() { try { 
    defaultNewFrameBounds_r.translate(60, 20);
    if (!screenRectangle().contains(defaultNewFrameBounds_r))
      defaultNewFrameBounds_r.setLocation(30+random(30), 20+random(20));
    return new Rectangle(defaultNewFrameBounds_r);
   } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "defaultNewFrameBounds_r.translate(60, 20);\r\n    if (!screenRectangle().contai..."; }});
}
static void hideConsole() {
  final JFrame frame = consoleFrame();
  if (frame != null) {
    autoVMExit();
    swingLater(new Runnable() {  public void run() { try { 
      frame.setVisible(false);
    
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "frame.setVisible(false);"; }});
  }
}
static Throwable getInnerException(Throwable e) {
  while (e.getCause() != null)
    e = e.getCause();
  return e;
}
static <A> ArrayList<A> asList(A[] a) {
  return a == null ? new ArrayList<A>() : new ArrayList<A>(Arrays.asList(a));
}

static ArrayList<Integer> asList(int[] a) {
  ArrayList<Integer> l = new ArrayList();
  for (int i : a) l.add(i);
  return l;
}

static <A> ArrayList<A> asList(Iterable<A> s) {
  if (s instanceof ArrayList) return (ArrayList) s;
  ArrayList l = new ArrayList();
  if (s != null)
    for (A a : s)
      l.add(a);
  return l;
}


static <A> ArrayList<A> asList(Producer<A> p) {
  ArrayList l = new ArrayList();
  A a;
  if (p != null) while ((a = p.next()) != null)
    l.add(a);
  return l;
}


static <A> ArrayList<A> asList(Enumeration<A> e) {
  ArrayList l = new ArrayList();
  if (e != null)
    while (e.hasMoreElements())
      l.add(e.nextElement());
  return l;
}
static Object collectionMutex(Object o) {
  String c = className(o);
  if (eq(c, "java.util.TreeMap$KeySet"))
    c = className(o = getOpt(o, "m"));
  else if (eq(c, "java.util.HashMap$KeySet"))
    c = className(o = get_raw(o, "this$0"));

  
  
  if (eqOneOf(c, "java.util.TreeMap$AscendingSubMap", "java.util.TreeMap$DescendingSubMap"))
    c = className(o = get_raw(o, "m"));
    
  
    
  return o;
}
static Runnable _topLevelErrorHandling(final Runnable runnable) {
  final Object info = _threadInfo();
  return new Runnable() {  public void run() { try { 
    try {
      _threadInheritInfo(info);
      runnable.run();
    } catch (Throwable __e) { _handleException(__e); }
  
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "pcall {\r\n      _threadInheritInfo(info);\r\n      runnable.run();\r\n    }"; }};
}
static Map vm_generalWeakSubMap(Object name) {
  synchronized(get(javax(), "generalMap")) {
    Map map = (Map) ( vm_generalMap_get(name));
    if (map == null)
      vm_generalMap_put(name, map = newWeakMap());
    return map;
  }
}

static <A> WeakReference<A> weakRef(A a) {
  return newWeakReference(a);
}
static Class mc() {
  return main.class;
}
static Object pcallFunction(Object f, Object... args) {
  try { return callFunction(f, args); } catch (Throwable __e) { _handleException(__e); }
  return null;
}




static volatile boolean disableCertificateValidation_attempted;

static void disableCertificateValidation() { try {
  if (disableCertificateValidation_attempted) return;
  disableCertificateValidation_attempted = true;
  
  try {
    // Create a trust manager that does not validate certificate chains
    TrustManager[] trustAllCerts = new TrustManager[] { 
      new X509TrustManager() {
        public X509Certificate[] getAcceptedIssuers() { 
          return new X509Certificate[0]; 
        }
        public void checkClientTrusted(X509Certificate[] certs, String authType) {}
        public void checkServerTrusted(X509Certificate[] certs, String authType) {}
    }};
  
    // Ignore differences between given hostname and certificate hostname
    HostnameVerifier hv = new HostnameVerifier() {
      public boolean verify(String hostname, SSLSession session) { return true; }
    };
  
    // Install the all-trusting trust manager
    SSLContext sc = SSLContext.getInstance("SSL");
    sc.init(null, trustAllCerts, new SecureRandom());
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    HttpsURLConnection.setDefaultHostnameVerifier(hv);
  } catch (Throwable __e) { _handleException(__e); }
} catch (Exception __e) { throw rethrow(__e); } }
static boolean networkAllowanceTest(String url) {
  return isAllowed("networkAllowanceTest", url);
}
static final boolean loadPageThroughProxy_enabled = false;

static String loadPageThroughProxy(String url) {
  return null;
}
static void sleepSeconds(double s) {
  if (s > 0) sleep(round(s*1000));
}
static String tb_mainServer_default = "http://tinybrain.de:8080";
static Object tb_mainServer_override; // func -> S

static String tb_mainServer() {
  if (tb_mainServer_override != null) return (String) callF(tb_mainServer_override);
  return trim(loadTextFile(tb_mainServer_file(),
    tb_mainServer_default));
}

static File tb_mainServer_file() {
  return getProgramFile("#1001638", "mainserver.txt");
}

static boolean tb_mainServer_isDefault() {
  return eq(tb_mainServer(), tb_mainServer_default);
}
static boolean isTrue(Object o) {
  if (o instanceof Boolean)
    return ((Boolean) o).booleanValue();
  if (o == null) return false;
  if (o instanceof ThreadLocal)
    return isTrue(((ThreadLocal) o).get());
  throw fail(getClassName(o));
}
static void printWithTime(String s) {
  print(hmsWithColons() + ": " + s);
}
static <A> A getAndClearThreadLocal(ThreadLocal<A> tl) {
  A a = tl.get();
  tl.set(null);
  return a;
}
static void setHeaders(URLConnection con) throws IOException {
  
  String computerID = getComputerID_quick();
  if (computerID != null) try {
    con.setRequestProperty("X-ComputerID", computerID);
    con.setRequestProperty("X-OS", System.getProperty("os.name") + " " + System.getProperty("os.version"));
  } catch (Throwable e) {
    //printShortException(e);
  }
  
}
static <A, B> Set<A> keys(Map<A, B> map) {
  return map == null ? new HashSet() : map.keySet();
}

static Set keys(Object map) {
  return keys((Map) map);
}




  static <A, B> Set<A> keys(MultiMap<A, B> mm) {
    return mm.keySet();
  }



static Map vm_generalSubMap(Object name) {
  synchronized(get(javax(), "generalMap")) {
    Map map = (Map) ( vm_generalMap_get(name));
    if (map == null)
      vm_generalMap_put(name, map = synchroMap());
    return map;
  }
}

static GZIPInputStream newGZIPInputStream(File f) {
  return gzInputStream(f);
}

static GZIPInputStream newGZIPInputStream(InputStream in) {
  return gzInputStream(in);
}
static String unquote(String s) {
  if (s == null) return null;
  if (startsWith(s, '[')) {
    int i = 1;
    while (i < s.length() && s.charAt(i) == '=') ++i;
    if (i < s.length() && s.charAt(i) == '[') {
      String m = s.substring(1, i);
      if (s.endsWith("]" + m + "]"))
        return s.substring(i+1, s.length()-i-1);
    }
  }
  
  if (s.length() > 1) {
    char c = s.charAt(0);
    if (c == '\"' || c == '\'') {
      int l = endsWith(s, c) ? s.length()-1 : s.length();
      StringBuilder sb = new StringBuilder(l-1);
  
      for (int i = 1; i < l; i++) {
        char ch = s.charAt(i);
        if (ch == '\\') {
          char nextChar = (i == l - 1) ? '\\' : s.charAt(i + 1);
          // Octal escape?
          if (nextChar >= '0' && nextChar <= '7') {
              String code = "" + nextChar;
              i++;
              if ((i < l - 1) && s.charAt(i + 1) >= '0'
                      && s.charAt(i + 1) <= '7') {
                  code += s.charAt(i + 1);
                  i++;
                  if ((i < l - 1) && s.charAt(i + 1) >= '0'
                          && s.charAt(i + 1) <= '7') {
                      code += s.charAt(i + 1);
                      i++;
                  }
              }
              sb.append((char) Integer.parseInt(code, 8));
              continue;
          }
          switch (nextChar) {
          case '\"': ch = '\"'; break;
          case '\\': ch = '\\'; break;
          case 'b': ch = '\b'; break;
          case 'f': ch = '\f'; break;
          case 'n': ch = '\n'; break;
          case 'r': ch = '\r'; break;
          case 't': ch = '\t'; break;
          case '\'': ch = '\''; break;
          // Hex Unicode: u????
          case 'u':
              if (i >= l - 5) {
                  ch = 'u';
                  break;
              }
              int code = Integer.parseInt(
                      "" + s.charAt(i + 2) + s.charAt(i + 3)
                         + s.charAt(i + 4) + s.charAt(i + 5), 16);
              sb.append(Character.toChars(code));
              i += 5;
              continue;
          default:
            ch = nextChar; // added by Stefan
          }
          i++;
        }
        sb.append(ch);
      }
      return sb.toString();
    }
  }
    
  return s; // not quoted - return original
}
static String toHex(byte[] bytes) {
  return bytesToHex(bytes);
}

static String toHex(byte[] bytes, int ofs, int len) {
  return bytesToHex(bytes, ofs, len);
}

static byte[] utf8(String s) {
  return toUtf8(s);
}
static <A> A or(A a, A b) {
  return a != null ? a : b;
}
static URLConnection openConnection(URL url) { try {
  ping();
  
  callOpt(javax(), "recordOpenURLConnection", str(url));
  
  return url.openConnection();
} catch (Exception __e) { throw rethrow(__e); } }
static int toInt(Object o) {
  if (o == null) return 0;
  if (o instanceof Number)
    return ((Number) o).intValue();
  if (o instanceof String)
    return parseInt((String) o);
  throw fail("woot not int: " + getClassName(o));
}

static int toInt(long l) {
  if (l != (int) l) throw fail("Too large for int: " + l);
  return (int) l;
}
static URLConnection setURLConnectionDefaultTimeouts(URLConnection con, long timeout) {
  if (con.getConnectTimeout() == 0) {
    con.setConnectTimeout(toInt(timeout));
    if (con.getConnectTimeout() != timeout)
      print("Warning: URL connect timeout not set by JDK.");
  }
  if (con.getReadTimeout() == 0) {
    con.setReadTimeout(toInt(timeout));
    if (con.getReadTimeout() != timeout)
      print("Warning: URL read timeout not set by JDK.");
  }
  return con;
}
static String shortenSnippetID(String snippetID) {
  if (snippetID.startsWith("#"))
    snippetID = snippetID.substring(1);
  String httpBlaBla = "http://tinybrain.de/";
  if (snippetID.startsWith(httpBlaBla))
    snippetID = snippetID.substring(httpBlaBla.length());
  return "" + parseLong(snippetID);
}
static boolean isEmpty(Collection c) {
  return c == null || c.isEmpty();
}

static boolean isEmpty(CharSequence s) {
  return s == null || s.length() == 0;
}

static boolean isEmpty(Object[] a) { return a == null || a.length == 0; }
static boolean isEmpty(byte[] a) { return a == null || a.length == 0; }

static boolean isEmpty(Map map) {
  return map == null || map.isEmpty();
}
  public static boolean isSnippetID(String s) {
    try {
      parseSnippetID(s);
      return true;
    } catch (RuntimeException e) {
      return false;
    }
  }
//static final Map<Class, HashMap<S, Field>> getOpt_cache = newDangerousWeakHashMap(f getOpt_special_init);

static class getOpt_Map extends WeakHashMap {
  getOpt_Map() {
    if (getOpt_special == null) getOpt_special = new HashMap();
    clear();
  }
  
  public void clear() {
    super.clear();
    //print("getOpt clear");
    put(Class.class, getOpt_special);
    put(String.class, getOpt_special);
  }
}

static final Map<Class, HashMap<String, Field>> getOpt_cache = _registerDangerousWeakMap(synchroMap(new getOpt_Map()));
//static final Map<Class, HashMap<S, Field>> getOpt_cache = _registerWeakMap(synchroMap(new getOpt_Map));
static HashMap getOpt_special; // just a marker

/*static void getOpt_special_init(Map map) {
  map.put(Class.class, getOpt_special);
  map.put(S.class, getOpt_special);
}*/

static Object getOpt_cached(Object o, String field) { try {
  if (o == null) return null;

  Class c = o.getClass();
  HashMap<String, Field> map;
  synchronized(getOpt_cache) {
    map = getOpt_cache.get(c);
    if (map == null)
      map = getOpt_makeCache(c);
  }
  
  if (map == getOpt_special) {
    if (o instanceof Class)
      return getOpt((Class) o, field);
    /*if (o instanceof S)
      ret getOpt(getBot((S) o), field);*/
    if (o instanceof Map)
      return ((Map) o).get(field);
  }
    
  Field f = map.get(field);
  if (f != null) return f.get(o);
  
    if (o instanceof DynamicObject)
      return ((DynamicObject) o).fieldValues.get(field);
  
  return null;
} catch (Exception __e) { throw rethrow(__e); } }

// used internally - we are in synchronized block
static HashMap<String, Field> getOpt_makeCache(Class c) {
  HashMap<String, Field> map;
  if (isSubtypeOf(c, Map.class))
    map = getOpt_special;
  else {
    map = new HashMap();
    Class _c = c;
    do {
      for (Field f : _c.getDeclaredFields()) {
        f.setAccessible(true);
        String name = f.getName();
        if (!map.containsKey(name))
          map.put(name, f);
      }
      _c = _c.getSuperclass();
    } while (_c != null);
  }
  if (getOpt_cache != null) getOpt_cache.put(c, map);
  return map;
}
static List _registerWeakMap_preList;

static <A> A _registerWeakMap(A map) {
  if (javax() == null) {
    // We're in class init
    if (_registerWeakMap_preList == null) _registerWeakMap_preList = synchroList();
    _registerWeakMap_preList.add(map);
    return map;
  }
  
  try {
    call(javax(), "_registerWeakMap", map);
  } catch (Throwable e) {
    printException(e);
    print("Upgrade JavaX!!");
  }
  return map;
}

static void _onLoad_registerWeakMap() {
  assertNotNull(javax());
  if (_registerWeakMap_preList == null) return;
  for (Object o : _registerWeakMap_preList)
    _registerWeakMap(o);
  _registerWeakMap_preList = null;
}
static Map synchroMap() {
  return synchroHashMap();
}

static <A, B> Map<A, B> synchroMap(Map<A, B> map) {
  return Collections.synchronizedMap(map);
}
static List<Pair> _registerDangerousWeakMap_preList;

static <A> A _registerDangerousWeakMap(A map) {
  return _registerDangerousWeakMap(map, null);
}

static <A> A _registerDangerousWeakMap(A map, Object init) {
  callF(init, map);
  
  if (init instanceof String) {
    final String f = (String) ( init);
    init = new VF1<Map>() { public void get(Map map) { try {  callMC(f, map) ; } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "callMC(f, map)"; }};
  }
    
  if (javax() == null) {
    // We're in class init
    if (_registerDangerousWeakMap_preList == null) _registerDangerousWeakMap_preList = synchroList();
    _registerDangerousWeakMap_preList.add(pair(map, init));
    return map;
  }
  
  call(javax(), "_registerDangerousWeakMap", map, init);
  return map;
}

static void _onLoad_registerDangerousWeakMap() {
  assertNotNull(javax());
  if (_registerDangerousWeakMap_preList == null) return;
  for (Pair p : _registerDangerousWeakMap_preList)
    _registerDangerousWeakMap(p.a, p.b);
  _registerDangerousWeakMap_preList = null;
}
static List<String> classNames(Collection l) {
  return getClassNames(l);
}

static List<String> classNames(Object[] l) {
  return getClassNames(Arrays.asList(l));
}
static <A, B> void multiMapPut(Map<A, List<B>> map, A a, B b) {
  List<B> l = map.get(a);
  if (l == null)
    map.put(a, l = new ArrayList());
  l.add(b);
}
static Throwable getExceptionCause(Throwable e) {
  Throwable c = e.getCause();
  return c != null ? c : e;
}
static String joinWithSpace(Collection<String> c) {
  return join(" ", c);
}

static String joinWithSpace(String... c) {
  return join(" ", c);
}

static boolean isInstanceX(Class type, Object arg) {
  if (type == boolean.class) return arg instanceof Boolean;
  if (type == int.class) return arg instanceof Integer;
  if (type == long.class) return arg instanceof Long;
  if (type == float.class) return arg instanceof Float;
  if (type == short.class) return arg instanceof Short;
  if (type == char.class) return arg instanceof Character;
  if (type == byte.class) return arg instanceof Byte;
  if (type == double.class) return arg instanceof Double;
  return type.isInstance(arg);
}
static Class javax() {
  return getJavaX();
}
static String fromUtf8(byte[] bytes) { try {
  return new String(bytes, "UTF-8");
} catch (Exception __e) { throw rethrow(__e); } }
static ThreadLocal < VF1 < File > > checkFileNotTooBigToRead_tl = new ThreadLocal();

static void checkFileNotTooBigToRead(File f) {
  callF(checkFileNotTooBigToRead_tl.get(), f);
}
static File newFile(File base, String... names) {
  for (String name : names) base = new File(base, name);
  return base;
}

static File newFile(String name) {
  return name == null ? null : new File(name);
}
static String _userHome;
static String userHome() {
  if (_userHome == null) {
    if (isAndroid())
      _userHome = "/storage/sdcard0/";
    else
      _userHome = System.getProperty("user.home");
    //System.out.println("userHome: " + _userHome);
  }
  return _userHome;
}

static File userHome(String path) {
  return new File(userDir(), path);
}
static String getType(Object o) {
  return getClassName(o);
}
static void logQuotedWithTime(String s) {
  logQuotedWithTime(standardLogFile(), s);
}

static void logQuotedWithTime(File logFile, String s) {
  logQuoted(logFile, logQuotedWithTime_format(s));
}

static void logQuotedWithTime(String logFile, String s) {
  logQuoted(logFile, logQuotedWithTime_format(s));
}

static String logQuotedWithTime_format(String s) {
  return formatGMTWithDate_24(now()) + " " + s;
}
static File javaxDataDir_dir; // can be set to work on different base dir

static File javaxDataDir() {
  return javaxDataDir_dir != null ? javaxDataDir_dir : new File(userHome(), "JavaX-Data");
}

static File javaxDataDir(String sub) {
  return newFile(javaxDataDir(), sub);
}
static Runnable addThreadInfoToRunnable(final Object r) {
  final Object info = _threadInfo();
  return info == null ? asRunnable(r) : new Runnable() {  public void run() { try {  _inheritThreadInfo(info); callF(r); 
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "_inheritThreadInfo(info); callF(r);"; }};
}
static JTextArea wrappedTextArea(final JTextArea ta) {
  enableWordWrapForTextArea(ta);
  return ta;
}

static JTextArea wrappedTextArea() {
  return wrappedTextArea(jtextarea());
}

static JTextArea wrappedTextArea(String text) {
  JTextArea ta = wrappedTextArea();
  ta.setText(text);
  return ta;
}
static void onClick(JComponent c, final Object runnable) {
  c.addMouseListener(new MouseAdapter() {
    public void mouseClicked(MouseEvent e) {
      callF(runnable, e);
    }
  });
}

// re-interpreted for buttons
static void onClick(JButton btn, final Object runnable) {
  onEnter(btn, runnable);
}
static void disposeWindow(final Window window) {
  if (window != null) { swing(new Runnable() {  public void run() { try { 
    window.dispatchEvent(new WindowEvent(window, WindowEvent.WINDOW_CLOSING)); // call listeners
    myFrames_list.remove(window);
    window.dispose();
  
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "window.dispatchEvent(new WindowEvent(window, WindowEvent.WINDOW_CLOSING)); //..."; }}); }
}

static void disposeWindow(final Component c) {
  disposeWindow(getWindow(c));
}

static void disposeWindow(Object o) {
  if (o != null) disposeWindow(((Component) o));
}

static void disposeWindow() {
  disposeWindow(heldInstance(Component.class));
}
static JScrollPane jscroll(final Component c) {
  return swing(new F0<JScrollPane>() { JScrollPane get() { try { return  new JScrollPane(c) ; } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "new JScrollPane(c)"; }});
}
static int withMargin_defaultWidth = 6;

static JPanel withMargin(Component c) {
  return withMargin(withMargin_defaultWidth, c);
}

static JPanel withMargin(int w, Component c) {
  return withMargin(w, w, c);
}

static JPanel withMargin(int w, int h, Component c) {
  return withMargin(w, h, w, h, c);
}

static JPanel withMargin(final int top, final int left, final int bottom, final int right, final Component c) {
  return swing(new F0<JPanel>() { JPanel get() { try { 
    JPanel p = new JPanel(new BorderLayout());
    p.setBorder(BorderFactory.createEmptyBorder(top, left, bottom, right));
    p.add(c);
    return p;
   } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "JPanel p = new JPanel(new BorderLayout);\r\n    p.setBorder(BorderFactory.creat..."; }});
}
static Window getWindow(Object o) {
  if (!(o instanceof Component)) return null;
  Component c = (Component) o;
  while (c != null) {
    if (c instanceof Window) return (Window) c;
    c = c.getParent();
  }
  return null;
}
static Dimension getScreenSize() {
  return Toolkit.getDefaultToolkit().getScreenSize();
}
static void swingLater(long delay, final Object r) {
  javax.swing.Timer timer = new javax.swing.Timer(toInt(delay), actionListener(wrapAsActivity(r)));
  timer.setRepeats(false);
  timer.start();
}

static void swingLater(Object r) {
  SwingUtilities.invokeLater(toRunnable(r));
}

static int toMS_int(double seconds) {
  return toInt_checked((long) (seconds*1000));
}
static String getStackTrace(Throwable throwable) {
  lastException(throwable);
  return getStackTrace_noRecord(throwable);
}

static String getStackTrace_noRecord(Throwable throwable) {
  StringWriter writer = new StringWriter();
  throwable.printStackTrace(new PrintWriter(writer));
  return hideCredentials(writer.toString());
}

static String getStackTrace() {
  return getStackTrace_noRecord(new Throwable());
}
// PersistableThrowable doesn't hold GC-disturbing class references in backtrace
static volatile PersistableThrowable lastException_lastException;

static PersistableThrowable lastException() {
  return lastException_lastException;
}

static void lastException(Throwable e) {
  lastException_lastException = persistableThrowable(e);
}
static String unnull(String s) {
  return s == null ? "" : s;
}

static <A> List<A> unnull(List<A> l) {
  return l == null ? emptyList() : l;
}

static <A, B> Map<A, B> unnull(Map<A, B> l) {
  return l == null ? emptyMap() : l;
}

static <A> Iterable<A> unnull(Iterable<A> i) {
  return i == null ? emptyList() : i;
}

static <A> A[] unnull(A[] a) {
  return a == null ? (A[]) new Object[0] : a;
}

static BitSet unnull(BitSet b) {
  return b == null ? new BitSet() : b;
}



//ifclass Symbol

static String baseClassName(String className) {
  return substring(className, className.lastIndexOf('.')+1);
}

static String baseClassName(Object o) {
  return baseClassName(getClassName(o));
}
static String prependIfNempty(String prefix, String s) {
  return empty(s) ? s : prefix + s;
}
static ReentrantLock fairLock() {
  return new ReentrantLock(true);
}
static void lockOrFail(Lock lock, long timeout) { try {
  ping();
  if (!lock.tryLock(timeout, TimeUnit.MILLISECONDS)) {
    String s = "Couldn't acquire lock after " + timeout + " ms.";
    if (lock instanceof ReentrantLock) {
      ReentrantLock l = (ReentrantLock) ( lock);
      s += " Hold count: " + l.getHoldCount() + ", owner: " + call(l, "getOwner");
    }
    throw fail(s);
  }
  ping();
} catch (Exception __e) { throw rethrow(__e); } }
static String getSnippetTitle(String id) { try {
  if (id == null) return null;
  if (!isSnippetID(id)) return "?";
  long parsedID = parseSnippetID(id);
  String url;
  if (isImageServerSnippet(parsedID))
    url = "http://ai1.space/images/raw/title/" + parsedID + muricaCredentialsQuery();
  else if (isGeneralFileServerSnippet(parsedID))
    url = "http://butter.botcompany.de:8080/files/name/" + parsedID;
  else
    url = tb_mainServer() + "/tb-int/getfield.php?id=" + parsedID + "&field=title" + standardCredentials_noCookies();
  return or2(trim(loadPageSilently(url)), "?");
} catch (Exception __e) { throw rethrow(__e); } }

static String getSnippetTitle(long id) {
  return getSnippetTitle(fsI(id));
}

static boolean isNormalQuoted(String s) {
  int l = l(s);
  if (!(l >= 2 && s.charAt(0) == '"' && lastChar(s) == '"')) return false;
  int j = 1;
  while (j < l)
    if (s.charAt(j) == '"')
      return j == l-1;
    else if (s.charAt(j) == '\\' && j+1 < l)
      j += 2;
    else
      ++j;
  return false;
}
static boolean isMultilineQuoted(String s) {
  if (!startsWith(s, "[")) return false;
  int i = 1;
  while (i < s.length() && s.charAt(i) == '=') ++i;
  return i < s.length() && s.charAt(i) == '[';
}
static <A> A vm_generalMap_get(Object key) {
  return (A) vm_generalMap().get(key);
}
static void revalidate(final Component c) {
  if (c == null || !c.isShowing()) return;
  { swing(new Runnable() {  public void run() { try { 
    // magic combo to actually relayout and repaint
    c.revalidate();
    c.repaint();
  
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "// magic combo to actually relayout and repaint\r\n    c.revalidate();\r\n    c.r..."; }}); }
}

static void revalidate(RootPaneContainer c) { revalidate((Component) c); }
static void revalidate(JFrame f) { revalidate((Component) f); }
static void revalidate(JInternalFrame f) { revalidate((Component) f); }
static Object[] mapToParams(Map map) {
  return mapToObjectArray(map);
}
static String htmlQuote(String s) {
  return "\"" + htmlencode_forParams(s) + "\"";
}
static boolean eqic(String a, String b) {
  
  
    if ((a == null) != (b == null)) return false;
    if (a == null) return true;
    return a.equalsIgnoreCase(b);
  
}



static boolean eqic(char a, char b) {
  if (a == b) return true;
  
    char u1 = Character.toUpperCase(a);
    char u2 = Character.toUpperCase(b);
    if (u1 == u2) return true;
  
  return Character.toLowerCase(u1) == Character.toLowerCase(u2);
}
static <A> List<A> subList(List<A> l, int startIndex) {
  return subList(l, startIndex, l(l));
}

static <A> List<A> subList(List<A> l, int startIndex, int endIndex) {
  startIndex = Math.max(0, Math.min(l(l), startIndex));
  endIndex = Math.max(0, Math.min(l(l), endIndex));
  if (startIndex > endIndex) return litlist();
  return l.subList(startIndex, endIndex);
}


static <A> List<A> subList(List<A> l, IntRange r) {
  return subList(l, r.start, r.end);
}

static String substring(String s, int x) {
  return substring(s, x, l(s));
}

static String substring(String s, int x, int y) {
  if (s == null) return null;
  if (x < 0) x = 0;
  if (x >= s.length()) return "";
  if (y < x) y = x;
  if (y > s.length()) y = s.length();
  return s.substring(x, y);
}


static String substring(String s, IntRange r) {
  return r == null ? null : substring(s, r.start, r.end);
}



static Map<JFrame, Boolean> myFrames_list = weakHashMap();

static List<JFrame> myFrames() {
  return swing(new F0<List<JFrame>>() { List<JFrame> get() { try { return  keysList(myFrames_list) ; } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "keysList(myFrames_list)"; }});
}

static Throwable unwrapTrivialExceptionWraps(Throwable e) {
  if (e == null) return e;
  while (e.getClass() == RuntimeException.class
    && e.getCause() != null && eq(e.getMessage(), str(e.getCause())))
    e = e.getCause();
  return e;
}
static String replacePrefix(String prefix, String replacement, String s) {
  if (!startsWith(s, prefix)) return s;
  return replacement + substring(s, l(prefix));
}
static <A> A optPar(ThreadLocal<A> tl, A defaultValue) {
  A a = tl.get();
  if (a != null) {
    tl.set(null);
    return a;
  }
  return defaultValue;
}

static <A> A optPar(ThreadLocal<A> tl) {
  return optPar(tl, null);
}
static boolean even(int i) {
  return (i & 1) == 0;
}

static boolean even(long i) {
  return (i & 1) == 0;
}
static Object first(Object list) {
  return empty((List) list) ? null : ((List) list).get(0);
}

static <A> A first(List<A> list) {
  return empty(list) ? null : list.get(0);
}

static <A> A first(A[] bla) {
  return bla == null || bla.length == 0 ? null : bla[0];
}

static <A> A first(Iterable<A> i) {
  if (i == null) return null;
  Iterator<A> it = i.iterator();
  return it.hasNext() ? it.next() : null;
}

static Character first(String s) { return empty(s) ? null : s.charAt(0); }


static <A, B> A first(Pair<A, B> p) {
  return p == null ? null : p.a;
}



static <A, B, C> A first(T3<A, B, C> t) {
  return t == null ? null : t.a;
}

static List<AbstractButton> buttonsInGroup(ButtonGroup g) {
  if (g == null) return ll();
  return asList(g.getElements());
}
static Frame getAWTFrame(final Object _o) {
  return swing(new F0<Frame>() { Frame get() { try { 
    Object o = _o;
    /*
    ifdef HaveProcessing
      if (o instanceof PApplet) o = ((PApplet) o).getSurface();
    endifdef
    */
    if (o instanceof ButtonGroup) o = first(buttonsInGroup((ButtonGroup) o));
    if (!(o instanceof Component)) return null;
    Component c = (Component) o;
    while (c != null) {
      if (c instanceof Frame) return (Frame) c;
      c = c.getParent();
    }
    return null;
   } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "O o = _o;\r\n    /*\r\n    ifdef HaveProcessing\r\n      if (o instanceof PApplet) ..."; }});
}
static int imageIcon_cacheSize = 10;
static boolean imageIcon_verbose;
static Map<String, ImageIcon> imageIcon_cache;
static Lock imageIcon_lock = lock();
static ThreadLocal<Boolean> imageIcon_fixGIF = new ThreadLocal();

// not going through BufferedImage preserves animations
static ImageIcon imageIcon(String imageID) { try {
  Lock __379 = imageIcon_lock; lock(__379); try {
  if (imageIcon_cache == null)
    imageIcon_cache = new MRUCache(imageIcon_cacheSize);
  imageID = fsI(imageID);
  ImageIcon ii = imageIcon_cache.get(imageID);
  if (ii == null) {
    if (imageIcon_verbose) print("Loading image icon: " + imageID);
    File f = loadBinarySnippet(imageID);
    
      Boolean b = imageIcon_fixGIF.get();
      if (!isFalse(b))
        ii = new ImageIcon(loadBufferedImageFixingGIFs(f));
      else
    
    ii = new ImageIcon(f.toURI().toURL());
  } else
    imageIcon_cache.remove(imageID); // move to front of cache on access
  imageIcon_cache.put(imageID, ii);
  return ii;
} finally { unlock(__379); } } catch (Exception __e) { throw rethrow(__e); } }

static ImageIcon imageIcon(Image img) {
  return new ImageIcon(img);
}


static void standardTitlePopupMenu(final JFrame frame) {
  // standard right-click behavior on titles
  if (isSubstanceLAF())
    titlePopupMenu(frame,
      new VF1<JPopupMenu>() { public void get(JPopupMenu menu) { try { 
        boolean alwaysOnTop = frame.isAlwaysOnTop();
        menu.add(jmenuItem("Restart Program", "restart"));
        menu.add(jmenuItem("Duplicate Program", "duplicateThisProgram"));
        menu.add(jmenuItem("Show Console", "showConsole"));
        menu.add(jCheckBoxMenuItem("Always On Top", alwaysOnTop, new Runnable() {  public void run() { try { 
          toggleAlwaysOnTop(frame) ;
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "toggleAlwaysOnTop(frame)"; }}));
        menu.add(jMenuItem("Shoot Window", new Runnable() {  public void run() { try {  shootWindowGUI_external(frame, 500) ;
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "shootWindowGUI_external(frame, 500)"; }}));
        //addMenuItem(menu, "Bigger fonts", f swingBiggerFonts);
        //addMenuItem(menu, "Smaller fonts", f swingSmallerFonts);
       } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "bool alwaysOnTop = frame.isAlwaysOnTop();\r\n        menu.add(jmenuItem(\"Restar..."; }});
}

static <A extends Component> A _recordNewSwingComponent(A c) {
  if (c != null)
    callF(vm_generalMap_get("newSwingComponentRegistry"), c);
  return c;
}
static JLabel jlabel(final String text) {
  return swingConstruct(BetterLabel.class, text);
}

static JLabel jlabel() {
  return jlabel(" ");
}
static Rectangle screenRectangle() {
  return new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
}

static Random random_random = new Random();

static int random(int n) {
  return n <= 0 ? 0 : random_random.nextInt(n);
}

static double random(double max) {
  return random()*max;
}

static double random() {
  return random_random.nextInt(100001)/100000.0;
}

static double random(double min, double max) {
  return min+random()*(max-min);
}

// min <= value < max
static int random(int min, int max) {
  return min+random(max-min);
}

static <A> A random(List<A> l) {
  return oneOf(l);
}

static <A> A random(Collection<A> c) {
  if (c instanceof List) return random((List<A>) c);
  int i = random(l(c));
  return collectionGet(c, i);
}
static void autoVMExit() {
  call(getJavaX(), "autoVMExit");
}
static String className(Object o) {
  return getClassName(o);
}
static boolean eqOneOf(Object o, Object... l) {
  for (Object x : l) if (eq(o, x)) return true; return false;
}
static List<VF1<Map>> _threadInfo_makers = synchroList();

static Object _threadInfo() {
  if (empty(_threadInfo_makers)) return null;
  HashMap map = new HashMap();
  pcallFAll(_threadInfo_makers, map);
  return map;
}
static List<VF1<Map>> _threadInheritInfo_retrievers = synchroList();

static void _threadInheritInfo(Object info) {
  if (info == null) return;
  pcallFAll(_threadInheritInfo_retrievers, (Map) info);
}
static Object vm_generalMap_put(Object key, Object value) {
  return mapPutOrRemove(vm_generalMap(), key, value);
}
static <A, B> Map<A, B> newWeakMap() {
  return newWeakHashMap();
}
static <A> WeakReference<A> newWeakReference(A a) {
  return a == null ? null : new WeakReference(a);
}
static Object callFunction(Object f, Object... args) {
  return callF(f, args);
}
static volatile Object isAllowed_function; // func(S, O[]) -> bool
static volatile boolean isAllowed_all = true;

static boolean isAllowed(String askingMethod, Object... args) {
  // check on VM level
  Object f = vm_generalMap_get("isAllowed_function");
  if (f != null && !isTrue(callF(f, askingMethod, args))) return false;
  
  // check locally
  return isAllowed_all || isTrue(callF(isAllowed_function, askingMethod, args));
}
static long round(double d) {
  return Math.round(d);
}
static String trim(String s) { return s == null ? null : s.trim(); }
static String trim(StringBuilder buf) { return buf.toString().trim(); }
static String trim(StringBuffer buf) { return buf.toString().trim(); }
static File getProgramFile(String progID, String fileName) {
  if (new File(fileName).isAbsolute())
    return new File(fileName);
  return new File(getProgramDir(progID), fileName);
}

static File getProgramFile(String fileName) {
  return getProgramFile(getProgramID(), fileName);
}



static String hmsWithColons() {
  return hmsWithColons(now());
}

static String hmsWithColons(long time) {
  return new SimpleDateFormat("HH:mm:ss").format(time);
}

static String getComputerID_quick() {
  return computerID();
}
static int gzInputStream_defaultBufferSize = 65536;

static GZIPInputStream gzInputStream(File f) { try {
  return gzInputStream(new FileInputStream(f));
} catch (Exception __e) { throw rethrow(__e); } }

static GZIPInputStream gzInputStream(File f, int bufferSize) { try {
  return gzInputStream(new FileInputStream(f), bufferSize);
} catch (Exception __e) { throw rethrow(__e); } }

static GZIPInputStream gzInputStream(InputStream in) {
  return gzInputStream(in, gzInputStream_defaultBufferSize);
}

static GZIPInputStream gzInputStream(InputStream in, int bufferSize) { try {
  return _registerIOWrap(new GZIPInputStream(in, gzInputStream_defaultBufferSize), in);
} catch (Exception __e) { throw rethrow(__e); } }
static boolean startsWith(String a, String b) {
  return a != null && a.startsWith(b);
}

static boolean startsWith(String a, char c) {
  return nempty(a) && a.charAt(0) == c;
}


  static boolean startsWith(String a, String b, Matches m) {
    if (!startsWith(a, b)) return false;
    m.m = new String[] {substring(a, l(b))};
    return true;
  }


static boolean startsWith(List a, List b) {
  if (a == null || l(b) > l(a)) return false;
  for (int i = 0; i < l(b); i++)
    if (neq(a.get(i), b.get(i)))
      return false;
  return true;
}


static boolean endsWith(String a, String b) {
  return a != null && a.endsWith(b);
}

static boolean endsWith(String a, char c) {
  return nempty(a) && lastChar(a) == c;
}


  static boolean endsWith(String a, String b, Matches m) {
    if (!endsWith(a, b)) return false;
    m.m = new String[] {dropLast(l(b), a)};
    return true;
  }


static byte[] toUtf8(String s) { try {
  return s.getBytes("UTF-8");
} catch (Exception __e) { throw rethrow(__e); } }
static int parseInt(String s) {
  return empty(s) ? 0 : Integer.parseInt(s);
}

static int parseInt(char c) {
  return Integer.parseInt(str(c));
}
static long parseLong(String s) {
  if (s == null) return 0;
  return Long.parseLong(dropSuffix("L", s));
}

static long parseLong(Object s) {
  return Long.parseLong((String) s);
}
static void clear(Collection c) {
  if (c != null) c.clear();
}
static <A, B> void put(Map<A, B> map, A a, B b) {
  if (map != null) map.put(a, b);
}
static boolean isSubtypeOf(Class a, Class b) {
  return b.isAssignableFrom(a); // << always hated that method, let's replace it!
}
static void printException(Throwable e) {
  printStackTrace(e);
}
static <A> A assertNotNull(A a) {
  assertTrue(a != null);
  return a;
}

static <A> A assertNotNull(String msg, A a) {
  assertTrue(msg, a != null);
  return a;
}
static Map synchroHashMap() {
  return Collections.synchronizedMap(new HashMap());
}

static <A, B> Pair<A, B> pair(A a, B b) {
  return new Pair(a, b);
}

static <A> Pair<A, A> pair(A a) {
  return new Pair(a, a);
}
static List<String> getClassNames(Collection l) {
  List<String> out = new ArrayList();
  if (l != null) for (Object o : l)
    out.add(o == null ? null : getClassName(o));
  return out;
}
static File userDir() {
  return new File(userHome());
}

static File userDir(String path) {
  return new File(userHome(), path);
}
static File standardLogFile() {
  return getProgramFile("log");
}
static void logQuoted(String logFile, String line) {
  logQuoted(getProgramFile(logFile), line);
}

static void logQuoted(File logFile, String line) {
  appendToFile(logFile, quote(line) + "\n");
}


static String formatGMTWithDate_24(long time) {
  return simpleDateFormat_GMT("yyyy/MM/dd").format(time) + " " + formatGMT_24(time);
}
static long now_virtualTime;
static long now() {
  return now_virtualTime != 0 ? now_virtualTime : System.currentTimeMillis();
}

static Runnable asRunnable(Object o) {
  return toRunnable(o);
}


static void _inheritThreadInfo(Object info) {
  _threadInheritInfo(info);
}
static void enableWordWrapForTextArea(final JTextArea ta) {
  if (ta != null) { swing(new Runnable() {  public void run() { try { 
    ta.setLineWrap(true);
    ta.setWrapStyleWord(true);
  
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "ta.setLineWrap(true);\r\n    ta.setWrapStyleWord(true);"; }}); }
}
static JTextArea jtextarea() {
  return jTextArea();
}

static JTextArea jtextarea(String text) {
  return jTextArea(text);
}
static JTextField onEnter(final JTextField tf, final Object action) {
  if (action == null || tf == null) return tf;
  tf.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent _evt) { try {
    tf.selectAll();
    callF(action);
  } catch (Throwable __e) { messageBox(__e); }}});
  return tf;
}

static JButton onEnter(JButton btn, final Object action) {
  if (action == null || btn == null) return btn;
  btn.addActionListener(actionListener(action));
  return btn;
}

static JList onEnter(JList list, Object action) {
  list.addKeyListener(enterKeyListener(rCallOnSelectedListItem(list, action)));
  return list;
}

// editable only
static JComboBox onEnter(final JComboBox cb, final Object action) {
  JTextField text = (JTextField) cb.getEditor().getEditorComponent();
  /*onEnter(text, r {
    cb.hidePopup();
    pcallF(action);
  });*/
  onEnter(text, action);
  return cb;
}

static JTable onEnter(final JTable table, final Object action) {  
  table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
    .put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "Enter");
    
  table.getActionMap().put("Enter", new AbstractAction() {
    public void actionPerformed(ActionEvent e) {
      callF(action, table.getSelectedRow());
    }
  });
  return table;
}

/*static JTextArea onEnter(final JTextArea ta, fO action) {
  addKeyListener(ta, enterKeyListener(action));
  ret ta;
}*/
static <A> A heldInstance(Class<A> c) {
  List<Object> l = holdInstance_l.get();
  for (int i = l(l)-1; i >= 0; i--) {
    Object o = l.get(i);
    if (isInstanceOf(o, c))
      return (A) o;
  }
  throw fail("No instance of " + className(c) + " held");
}
static ActionListener actionListener(final Object runnable) {
  return actionListener(runnable, null);
}

static ActionListener actionListener(final Object runnable, final Object instanceToHold) {
  if (runnable instanceof ActionListener) return (ActionListener) runnable;
  final Object info = _threadInfo();
  return new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent _evt) { try {
    _threadInheritInfo(info);
     AutoCloseable __461 = holdInstance(instanceToHold); try {
    callF(runnable);
  } finally { _close(__461); }} catch (Throwable __e) { messageBox(__e); }}};
}
static int toInt_checked(long l) {
  if (l != (int) l) throw fail("Too large for int: " + l);
  return (int) l;
}
static List emptyList() {
  return new ArrayList();
  //ret Collections.emptyList();
}

static List emptyList(int capacity) {
  return new ArrayList(max(0, capacity));
}

// Try to match capacity
static List emptyList(Iterable l) {
  return l instanceof Collection ? emptyList(((Collection) l).size()) : emptyList();
}
static Map emptyMap() {
  return new HashMap();
}
static boolean isImageServerSnippet(long id) {
  return id >= 1100000 && id < 1200000;
}
static String muricaCredentialsQuery() {
  return htmlQuery(muricaCredentials());
}
static boolean isGeneralFileServerSnippet(long id) {
  return id >= 1400000 && id < 1500000;
}
static String standardCredentials_noCookies() {
  return standardCredentials() + "&noCookies=1";
}
static String fsI(String id) {
  return formatSnippetID(id);
}

static String fsI(long id) {
  return formatSnippetID(id);
}
static char lastChar(String s) {
  return empty(s) ? '\0' : s.charAt(l(s)-1);
}
static Map vm_generalMap_map;

static Map vm_generalMap() {
  if (vm_generalMap_map == null)
    vm_generalMap_map = (Map) get(javax(), "generalMap");
  return vm_generalMap_map;
}
static Object[] mapToObjectArray(Map map) {
  List l = new ArrayList();
  for (Object o : keys(map)) {
    l.add(o);
    l.add(map.get(o));
  }
  return toObjectArray(l);
}
static String htmlencode_forParams(String s) {
  if (s == null) return "";
  StringBuilder out = new StringBuilder(Math.max(16, s.length()));
  for (int i = 0; i < s.length(); i++) {
      char c = s.charAt(i);
      /*if (c >= 0x100)
        out.append("&#x").append(charToHex(c)).append(';');
      else*/ if (c > 127 || c == '"' || c == '<' || c == '>') {
          out.append("&#");
          out.append((int) c);
          out.append(';');
      } else
          out.append(c);
  }
  return out.toString();
}
static String asString(Object o) {
  return o == null ? null : o.toString();
}
static <A> ArrayList<A> litlist(A... a) {
  ArrayList l = new ArrayList(a.length);
  for (A x : a) l.add(x);
  return l;
}


static void restart() {
  Object j = getJavaX();
  call(j, "cleanRestart", get(j, "fullArgs"));
}
static void duplicateThisProgram() {
  nohupJavax(trim(programID() + " " + smartJoin((String[]) get(getJavaX(), "fullArgs"))));
}
static ThreadLocal < List < Object > > holdInstance_l = new ThreadLocal();

static AutoCloseable holdInstance(Object o) {
  if (o == null) return null;
  listThreadLocalAdd(holdInstance_l, o);
  return new AutoCloseable() {
    public void close() {
      listThreadLocalPopLast(holdInstance_l);
    }
  };
}
static <A, B> Map<A, B> weakHashMap() {
  return newWeakHashMap();
}
static <A, B> List<A> keysList(Map<A, B> map) {
  return cloneListSynchronizingOn(keys(map), map);
}
static File loadBinarySnippet(String snippetID) { try {
  long id = parseSnippetID(snippetID);
  File f = DiskSnippetCache_getLibrary(id);
  if (fileSize(f) == 0)
    f = loadDataSnippetToFile(snippetID);
  return f;
} catch (Exception __e) { throw rethrow(__e); } }





static boolean loadBufferedImageFixingGIFs_debug;
static ThreadLocal<Var<byte[]>> loadBufferedImageFixingGIFs_output = new ThreadLocal();

static Image loadBufferedImageFixingGIFs(File file) { try {
  if (!file.exists()) return null;

  // Load anything but GIF the normal way
  if (!isGIF(file))
    return ImageIO.read(file);
    
  if (loadBufferedImageFixingGIFs_debug) print("loadBufferedImageFixingGIFs" + ": checking gif");

  // Get GIF reader
  ImageReader reader = ImageIO.getImageReadersByFormatName("gif").next();
  // Give it the stream to decode from
  reader.setInput(ImageIO.createImageInputStream(file));

  int numImages = reader.getNumImages(true);

  // Get 'metaFormatName'. Need first frame for that.
  IIOMetadata imageMetaData = reader.getImageMetadata(0);
  String metaFormatName = imageMetaData.getNativeMetadataFormatName();

  // Find out if GIF is bugged
  boolean foundBug = false;
  for (int i = 0; i < numImages && !foundBug; i++) {
      // Get metadata
      IIOMetadataNode root = (IIOMetadataNode)reader.getImageMetadata(i).getAsTree(metaFormatName);

      // Find GraphicControlExtension node
      int nNodes = root.getLength();
      for (int j = 0; j < nNodes; j++) {
          org.w3c.dom.Node node = root.item(j);
          if (node.getNodeName().equalsIgnoreCase("GraphicControlExtension")) {
              // Get delay value
              String delay = ((IIOMetadataNode)node).getAttribute("delayTime");

              // Check if delay is bugged
              if (Integer.parseInt(delay) == 0) {
                  foundBug = true;
              }

              break;
          }
      }
  }

  if (loadBufferedImageFixingGIFs_debug) print("loadBufferedImageFixingGIFs" + ": " + f2s(file) + " foundBug=" + foundBug);
  
  // Load non-bugged GIF the normal way
  Image image;
  if (!foundBug) {
    image = Toolkit.getDefaultToolkit().createImage(f2s(file));
  } else {
    // Prepare streams for image encoding
    ByteArrayOutputStream baoStream = new ByteArrayOutputStream();
    {
       ImageOutputStream ios = ImageIO.createImageOutputStream(baoStream); try {
      // Get GIF writer that's compatible with reader
      ImageWriter writer = ImageIO.getImageWriter(reader);
      // Give it the stream to encode to
      writer.setOutput(ios);

      writer.prepareWriteSequence(null);

      for (int i = 0; i < numImages; i++) {
          // Get input image
          BufferedImage frameIn = reader.read(i);

          // Get input metadata
          IIOMetadataNode root = (IIOMetadataNode)reader.getImageMetadata(i).getAsTree(metaFormatName);

          // Find GraphicControlExtension node
          int nNodes = root.getLength();
          for (int j = 0; j < nNodes; j++) {
              org.w3c.dom.Node node = root.item(j);
              if (node.getNodeName().equalsIgnoreCase("GraphicControlExtension")) {
                  // Get delay value
                  String delay = ((IIOMetadataNode)node).getAttribute("delayTime");

                  // Check if delay is bugged
                  if (Integer.parseInt(delay) == 0) {
                      // Overwrite with a valid delay value
                      ((IIOMetadataNode)node).setAttribute("delayTime", "10");
                  }

                  break;
              }
          }

          // Create output metadata
          IIOMetadata metadata = writer.getDefaultImageMetadata(new ImageTypeSpecifier(frameIn), null);
          // Copy metadata to output metadata
          metadata.setFromTree(metadata.getNativeMetadataFormatName(), root);

          // Create output image
          IIOImage frameOut = new IIOImage(frameIn, null, metadata);

          // Encode output image
          writer.writeToSequence(frameOut, writer.getDefaultWriteParam());
      }

      writer.endWriteSequence();
    } finally { _close(ios); }}

    // Create image using encoded data
    byte[] data = baoStream.toByteArray();
    setVar(loadBufferedImageFixingGIFs_output.get(), data);
    if (loadBufferedImageFixingGIFs_debug) print("Data size: " + l(data));
    image = Toolkit.getDefaultToolkit().createImage(data);
  }

  return image;
} catch (Exception __e) { throw rethrow(__e); } }


static boolean isSubstanceLAF() {
  return substanceLookAndFeelEnabled();
}
// menuMaker = voidfunc(JPopupMenu)
static void titlePopupMenu(final Component c, final Object menuMaker) {
  JComponent titleBar = getTitlePaneComponent(getPossiblyInternalFrame(c));
  if (titleBar == null)
    print("Can't add title right click!");
  else
    componentPopupMenu(titleBar, menuMaker);
}
static boolean jmenuItem_newThreads;

static JMenuItem jmenuItem(final String text) {
  return jMenuItem(text, null);
}

static JMenuItem jmenuItem(final String text, final Object r) {
  return swing(new F0<JMenuItem>() { JMenuItem get() { try { 
    Pair<String, Integer> p = jmenu_autoMnemonic(text);
    JMenuItem mi = new JMenuItem(p.a);
    if (p.b != 0) mi.setMnemonic(p.b);
    mi.addActionListener(jmenuItem_newThreads
      ? actionListenerInNewThread(r)
      : actionListener(r));
    return mi;
   } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "Pair<S, Int> p = jmenu_autoMnemonic(text);\r\n    JMenuItem mi = new JMenuItem(..."; }});
}
static JCheckBoxMenuItem jCheckBoxMenuItem(String text, boolean checked, final Object r) {
  JCheckBoxMenuItem mi = new JCheckBoxMenuItem(text, checked);
  mi.addActionListener(actionListener(r));
  return mi;
}
static void toggleAlwaysOnTop(JFrame frame) {
  frame.setAlwaysOnTop(!frame.isAlwaysOnTop());
}
static JMenuItem jMenuItem(final String text) {
  return jmenuItem(text);
}

static JMenuItem jMenuItem(String text, Object r) {
  return jmenuItem(text, r);
}
static void shootWindowGUI_external(JFrame frame) {
  call(hotwireOnce("#1007178"), "shootWindowGUI", frame);
}

static void shootWindowGUI_external(final JFrame frame, int delay) {
  call(hotwireOnce("#1007178"), "shootWindowGUI", frame, delay);
}

static <A> A swingConstruct(final Class<A> c, final Object... args) {
  return swing(new F0<A>() { A get() { try { return  nuObject(c, args) ; } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "nuObject(c, args)"; }});
}
static <A> A oneOf(List<A> l) {
  return l.isEmpty() ? null : l.get(new Random().nextInt(l.size()));
}

static char oneOf(String s) {
  return empty(s) ? '?' : s.charAt(random(l(s)));
}

static String oneOf(String... l) {
  return oneOf(asList(l));
}
static <A> A collectionGet(Collection<A> c, int idx) {
  if (c == null || idx < 0 || idx >= l(c)) return null;
  if (c instanceof List) return listGet((List<A>) c, idx);
  Iterator<A> it = c.iterator();
  for (int i = 0; i < idx; i++) if (it.hasNext()) it.next(); else return null;
  return it.hasNext() ? it.next() : null;
}
static void pcallFAll(Collection l, Object... args) {
  if (l != null) for (Object f : cloneList(l)) pcallF(f, args);
}
static <A, B> B mapPutOrRemove(Map<A, B> map, A key, B value) {
  if (map != null && key != null)
    if (value != null) return map.put(key, value);
    else return map.remove(key);
  return null;
}
static File getProgramDir() {
  return programDir();
}

static File getProgramDir(String snippetID) {
  return programDir(snippetID);
}
static String _computerID;
static Lock computerID_lock = lock();

public static String computerID() {
  if (_computerID == null) {
    Lock __534 = computerID_lock; lock(__534); try {
    if (_computerID != null) return _computerID;
    File file = computerIDFile();
    _computerID = loadTextFile(file.getPath());
    if (_computerID == null) {
      // legacy load
      _computerID = loadTextFile(userDir(".tinybrain/computer-id"));
      if (_computerID == null)
        _computerID = makeRandomID(12);
      saveTextFile(file, _computerID);
    }
  } finally { unlock(__534); } }
  return _computerID;
}
static <A> A _registerIOWrap(A wrapper, Object wrapped) {
  return wrapper;
}
static String dropSuffix(String suffix, String s) {
  return s.endsWith(suffix) ? s.substring(0, l(s)-l(suffix)) : s;
}
static void assertTrue(Object o) {
  if (!(eq(o, true) /*|| isTrue(pcallF(o))*/))
    throw fail(str(o));
}
  
static boolean assertTrue(String msg, boolean b) {
  if (!b)
    throw fail(msg);
  return b;
}

static boolean assertTrue(boolean b) {
  if (!b)
    throw fail("oops");
  return b;
}
static Lock appendToFile_lock = lock();
static boolean appendToFile_keepOpen;
static HashMap<String,Writer> appendToFile_writers = new HashMap();

static void appendToFile(String path, String s) { try {
  Lock __540 = appendToFile_lock; lock(__540); try { // Let's just generally synchronize this to be safe.
  mkdirsForFile(new File(path));
  path = getCanonicalPath(path);
  Writer writer = appendToFile_writers.get(path);
  if (writer == null) {
    //print("[Logging to " + path + "]");
    writer = new BufferedWriter(new OutputStreamWriter(
      newFileOutputStream(path, true), "UTF-8"));
    if (appendToFile_keepOpen)
      appendToFile_writers.put(path, writer);
  }
  writer.write(s);
  if (!appendToFile_keepOpen)
    writer.close();
} finally { unlock(__540); } } catch (Exception __e) { throw rethrow(__e); } }
  
static void appendToFile(File path, String s) {
  if (path != null)
    appendToFile(path.getPath(), s);
}

static void cleanMeUp_appendToFile() {
  Lock __541 = appendToFile_lock; lock(__541); try {
  closeAllWriters(values(appendToFile_writers));
  appendToFile_writers.clear();
} finally { unlock(__541); } }


static SimpleDateFormat simpleDateFormat_GMT(String format) {
  SimpleDateFormat sdf = new SimpleDateFormat(format);
  sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
  return sdf;
}


static String formatGMT_24(long time) {
  SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
  sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
  return sdf.format(time) + " GMT";
}
static JTextArea jTextArea() {
  return jTextArea("");
}

static JTextArea jTextArea(final String text) {
  return swingNu(JTextArea.class, text);
}
static void messageBox(final String msg) {
  if (headless()) print(msg);
  else { swing(new Runnable() {  public void run() { try { 
    JOptionPane.showMessageDialog(null, msg, "JavaX", JOptionPane.INFORMATION_MESSAGE);
  
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "JOptionPane.showMessageDialog(null, msg, \"JavaX\", JOptionPane.INFORMATION_MES..."; }}); }
}

static void messageBox(Throwable e) {
  //showConsole();
  printStackTrace(e);
  messageBox(hideCredentials(innerException(e)));
}
static KeyListener enterKeyListener(final Object action) {
  return new KeyAdapter() {
    public void keyReleased(KeyEvent ke) {
      if (ke.getKeyCode() == KeyEvent.VK_ENTER)
        pcallF(action);
    }
  };
}
static Runnable rCallOnSelectedListItem(final JList list, final Object action) {
  return new Runnable() {  public void run() { try {  pcallF(action, getSelectedItem(list)) ;
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "pcallF(action, getSelectedItem(list))"; }};
}
static boolean isInstanceOf(Object o, Class type) {
  return type.isInstance(o);
}
static int max(int a, int b) { return Math.max(a, b); }
static int max(int a, int b, int c) { return max(max(a, b), c); }
static long max(int a, long b) { return Math.max((long) a, b); }
static long max(long a, long b) { return Math.max(a, b); }
static double max(int a, double b) { return Math.max((double) a, b); }
static float max(float a, float b) { return Math.max(a, b); }
static double max(double a, double b) { return Math.max(a, b); }

static int max(Collection<Integer> c) {
  int x = Integer.MIN_VALUE;
  for (int i : c) x = max(x, i);
  return x;
}

static double max(double[] c) {
  if (c.length == 0) return Double.MIN_VALUE;
  double x = c[0];
  for (int i = 1; i < c.length; i++) x = Math.max(x, c[i]);
  return x;
}

static float max(float[] c) {
  if (c.length == 0) return Float.MAX_VALUE;
  float x = c[0];
  for (int i = 1; i < c.length; i++) x = Math.max(x, c[i]);
  return x;
}

static byte max(byte[] c) {
  byte x = -128;
  for (byte d : c) if (d > x) x = d;
  return x;
}

static short max(short[] c) {
  short x = -0x8000;
  for (short d : c) if (d > x) x = d;
  return x;
}

static int max(int[] c) {
  int x = Integer.MIN_VALUE;
  for (int d : c) if (d > x) x = d;
  return x;
}
static String htmlQuery(Map params) {
  return params.isEmpty() ? "" : "?" + makePostData(params);
}

static String htmlQuery(Object... data) {
  return htmlQuery(litmap(data));
}
static Object[] muricaCredentials() {
  String pass = muricaPassword();
  return nempty(pass) ? new Object[] {"_pass", pass } : new Object[0];
}
static String standardCredentials() {
  String user = standardCredentialsUser();
  String pass = standardCredentialsPass();
  if (nempty(user) && nempty(pass))
    return "&_user=" + urlencode(user) + "&_pass=" + urlencode(pass);
  return "";
}
static Object[] toObjectArray(Collection c) {
  List l = asList(c);
  return l.toArray(new Object[l.size()]);
}



static void nohupJavax(final String javaxargs) {
  { Thread _t_0 = new Thread() {
public void run() { try { call(hotwireOnce("#1008562"), "nohupJavax", javaxargs); } catch (Throwable __e) { _handleException(__e); } }
};
startThread(_t_0); }
}

static void nohupJavax(final String javaxargs, final String vmArgs) {
  { Thread _t_1 = new Thread() {
public void run() { try { call(hotwireOnce("#1008562"), "nohupJavax", javaxargs, vmArgs); } catch (Throwable __e) { _handleException(__e); } }
};
startThread(_t_1); }
}
static <A> void listThreadLocalAdd(ThreadLocal<List<A>> tl, A a) {
  List<A> l = tl.get();
  if (l == null) tl.set(l = new ArrayList());
  l.add(a);
}
static <A> A listThreadLocalPopLast(ThreadLocal<List<A>> tl) {
  List<A> l = tl.get();
  if (l == null) return null;
  A a = popLast(l);
  if (empty(l)) tl.set(null);
  return a;
}
static <A> ArrayList<A> cloneListSynchronizingOn(Collection<A> l, Object mutex) {
  if (l == null) return new ArrayList();
  synchronized(mutex) {
    return new ArrayList<A>(l);
  }
}
// If you change this, also change DiskSnippetCache_fileToLibID
static File DiskSnippetCache_file(long snippetID) {
  return new File(getGlobalCache(), "data_" + snippetID + ".jar");
}
  
  // Data files are immutable, use centralized cache
public static File DiskSnippetCache_getLibrary(long snippetID) throws IOException {
  File file = DiskSnippetCache_file(snippetID);
  return file.exists() ? file : null;
}

public static void DiskSnippetCache_putLibrary(long snippetID, byte[] data) throws IOException {
  saveBinaryFile(DiskSnippetCache_file(snippetID), data);
}

static byte[] loadDataSnippetImpl(String snippetID) throws IOException {
  byte[] data;
  try {
    URL url = new URL(dataSnippetLink(snippetID));
    print("Loading library: " + hideCredentials(url));
    try {
      data = loadBinaryPage(url.openConnection());
    } catch (RuntimeException e) {
      data = null;
    }
    
    if (data == null || data.length == 0) {
      url = new URL("http://data.tinybrain.de/blobs/"
        + parseSnippetID(snippetID));
      print("Loading library: " + hideCredentials(url));
      data = loadBinaryPage(url.openConnection());
    }
    print("Bytes loaded: " + data.length);
  } catch (FileNotFoundException e) {
    throw new IOException("Binary snippet #" + snippetID + " not found or not public");
  }
  return data;
}
static long fileSize(String path) { return getFileSize(path); }
static long fileSize(File f) { return getFileSize(f); }

static File loadDataSnippetToFile(String snippetID) { try {
  snippetID = fsI(snippetID);
  File f = DiskSnippetCache_file(parseSnippetID(snippetID));
  List<URL> urlsTried = new ArrayList();
  List<Throwable> errors = new ArrayList();
  try {
    URL url = addAndReturn(urlsTried, new URL(dataSnippetLink(snippetID)));
    print("Loading library: " + hideCredentials(url));
    try {
      loadBinaryPageToFile(openConnection(url), f);
      if (fileSize(f) == 0) throw fail();
    } catch (Throwable e) {
      errors.add(e);
      url = addAndReturn(urlsTried, new URL("http://data.tinybrain.de/blobs/"
        + psI(snippetID)));
      print("Trying other server: " + hideCredentials(url));
      loadBinaryPageToFile(openConnection(url), f);
      print("Got bytes: " + fileSize(f));
    }
    // TODO: check if we hit the "LOADING" message
    if (fileSize(f) == 0) throw fail();
    System.err.println("Bytes loaded: " + fileSize(f));
  } catch (Throwable e) {
    printStackTrace(e);
    errors.add(e);
    throw fail("Binary snippet " + snippetID + " not found or not public. URLs tried: " + allToString(urlsTried) + ", errors: " + allToString(errors));
  }
  return f;
} catch (Exception __e) { throw rethrow(__e); } }
static byte[] isGIF_magic = bytesFromHex("47494638"); // Actual signature is longer, but we're lazy

static boolean isGIF(byte[] data) {
  return byteArrayStartsWith(data, isGIF_magic);
}

static boolean isGIF(File f) {
  return isGIF(loadBeginningOfBinaryFile(f, l(isGIF_magic)));
}
static String f2s(File f) {
  return f == null ? null : f.getAbsolutePath();
}

static String f2s(java.nio.file.Path p) {
  return p == null ? null : f2s(p.toFile());
}
static <A> void setVar(Var<A> v, A value) {
  if (v != null) v.set(value);
}
static boolean substanceLookAndFeelEnabled() {
  return startsWith(getLookAndFeel(), "org.pushingpixels.");
}
static JComponent getTitlePaneComponent(RootPaneContainer window) {
  if (window instanceof JInternalFrame)
	  return getInternalFrameTitlePaneComponent((JInternalFrame) window);
	  
  if (!substanceLookAndFeelEnabled() || window == null) return null;
	JRootPane rootPane = window.getRootPane();
	if (rootPane != null) {
		Object /*SubstanceRootPaneUI*/ ui = rootPane.getUI();
		return (JComponent) call(ui, "getTitlePane");
	}
	return null;
}
static RootPaneContainer getPossiblyInternalFrame(Component c) {
  JInternalFrame f = getInternalFrame(c);
  if (f != null) return f;
  return optCast(RootPaneContainer.class, getWindow(c));
}


static Map<JComponent, List> componentPopupMenu_map;
static ThreadLocal<MouseEvent> componentPopupMenu_mouseEvent;

static void componentPopupMenu_init() {
  { swing(new Runnable() {  public void run() { try { 
    if (componentPopupMenu_map == null)
      componentPopupMenu_map = or((Map<JComponent, List>) getOpt(creator(), "componentPopupMenu_map"), (Map<JComponent, List>) (Map) newWeakHashMap());
    if (componentPopupMenu_mouseEvent == null)
      componentPopupMenu_mouseEvent = or((ThreadLocal<MouseEvent>) getOpt(creator(), "componentPopupMenu_mouseEvent"), new ThreadLocal());
  
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "if (componentPopupMenu_map == null)\r\n      componentPopupMenu_map = or((Map<J..."; }}); }
}

// menuMaker = voidfunc(JPopupMenu)
static void componentPopupMenu(final JComponent component, final Object menuMaker) {
  componentPopupMenu_init();
  if (component != null && menuMaker != null) { swing(new Runnable() {  public void run() { try { 
    List maker = componentPopupMenu_map.get(component);
    if (maker == null) {
      componentPopupMenu_map.put(component, maker = new ArrayList());
      component.addMouseListener(new componentPopupMenu_Adapter(maker));
    }
    maker.add(menuMaker);
  
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "List maker = componentPopupMenu_map.get(component);\r\n    if (maker == null) {..."; }}); }
}

static class componentPopupMenu_Adapter extends MouseAdapter {
  List maker;
  
  componentPopupMenu_Adapter(List maker) {
  this.maker = maker;}
  
  public void mousePressed(MouseEvent e) { displayMenu(e); }
  public void mouseReleased(MouseEvent e) { displayMenu(e); }

  void displayMenu(MouseEvent e) {
    if (e.isPopupTrigger()) displayMenu2(e);
  }
    
  void displayMenu2(MouseEvent e) {
    JPopupMenu menu = new JPopupMenu();
    int emptyCount = menu.getComponentCount();
    
    componentPopupMenu_mouseEvent.set(e);
    for (Object menuMaker : maker)
      pcallF(menuMaker, menu);
    
    // show menu if any items in it
    if (menu.getComponentCount() != emptyCount)
      menu.show(e.getComponent(), e.getX(), e.getY());
  }
}


static Pair<String, Integer> jmenu_autoMnemonic(String s) {
  int i = indexOf(s, '&');
  if (i >= 0 && i < l(s) && isLetterOrDigit(s.charAt(i+1)))
    return pair(substring(s, 0, i) + substring(s, i+1), (int) s.charAt(i+1));
  return pair(s, 0);
}
static ActionListener actionListenerInNewThread(final Object runnable) {
  return actionListenerInNewThread(runnable, null);
}

static ActionListener actionListenerInNewThread(final Object runnable, final Object instanceToHold) {
  if (runnable instanceof ActionListener) return (ActionListener) runnable;
  return new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent _evt) { try {
    { Thread _t_0 = new Thread("Action Listener") {
public void run() { try {
  
       AutoCloseable __600 = holdInstance(instanceToHold); try {
      callF(runnable);
    } finally { _close(__600); }} catch (Throwable __e) { _handleException(__e); } }
};
startThread(_t_0); }
  } catch (Throwable __e) { messageBox(__e); }}};
}
static Class hotwireOnce(String programID) {
  return hotwireCached(programID, false);
}
static Object nuObject(String className, Object... args) { try {
  return nuObject(classForName(className), args);
} catch (Exception __e) { throw rethrow(__e); } }

// too ambiguous - maybe need to fix some callers
/*static O nuObject(O realm, S className, O... args) {
  ret nuObject(_getClass(realm, className), args);
}*/

static <A> A nuObject(Class<A> c, Object... args) { try {
  if (args.length == 0) return nuObjectWithoutArguments(c); // cached!
  
  Constructor m = nuObject_findConstructor(c, args);
  m.setAccessible(true);
  return (A) m.newInstance(args);
} catch (Exception __e) { throw rethrow(__e); } }

static Constructor nuObject_findConstructor(Class c, Object... args) {
  for (Constructor m : c.getDeclaredConstructors()) {
    if (!nuObject_checkArgs(m.getParameterTypes(), args, false))
      continue;
    return m;
  }
  throw fail("Constructor " + c.getName() + getClasses(args) + " not found"
    + (args.length == 0 && (c.getModifiers() & java.lang.reflect.Modifier.STATIC) == 0 ? " - hint: it's a non-static class!" : ""));
}

 static boolean nuObject_checkArgs(Class[] types, Object[] args, boolean debug) {
    if (types.length != args.length) {
      if (debug)
        System.out.println("Bad parameter length: " + args.length + " vs " + types.length);
      return false;
    }
    for (int i = 0; i < types.length; i++)
      if (!(args[i] == null || isInstanceX(types[i], args[i]))) {
        if (debug)
          System.out.println("Bad parameter " + i + ": " + args[i] + " vs " + types[i]);
        return false;
      }
    return true;
  }
static <A> A listGet(List<A> l, int idx) {
  return l != null && idx >= 0 && idx < l(l) ? l.get(idx) : null;
}
static File programDir_mine; // set this to relocate program's data

static File programDir() {
  return programDir(getProgramID());
}

static File programDir(String snippetID) {
  boolean me = sameSnippetID(snippetID, programID());
  if (programDir_mine != null && me)
    return programDir_mine;
  File dir = new File(javaxDataDir(), formatSnippetID(snippetID));
  if (me) {
    String c = caseID();
    if (nempty(c)) dir = newFile(dir, c);
  }
  return dir;
}

static File programDir(String snippetID, String subPath) {
  return new File(programDir(snippetID), subPath);
}
static File computerIDFile() {
  return javaxDataDir("Basic Info/computer-id.txt");
}
static String makeRandomID(int length) {
  Random random = new Random();
  char[] id = new char[length];
  for (int i = 0; i < id.length; i++)
    id[i] = (char) ((int) 'a' + random.nextInt(26));
  return new String(id);
}
/** writes safely (to temp file, then rename) */
static File saveTextFile(String fileName, String contents) throws IOException {
  CriticalAction action = beginCriticalAction("Saving file " + fileName + " (" + l(contents) + " chars)");
  try {
    File file = new File(fileName);
    mkdirsForFile(file);
    String tempFileName = fileName + "_temp";
    File tempFile = new File(tempFileName);
    if (contents != null) {
      if (tempFile.exists()) try {
        String saveName = tempFileName + ".saved." + now();
        copyFile(tempFile, new File(saveName));
      } catch (Throwable e) { printStackTrace(e); }
      FileOutputStream fileOutputStream = newFileOutputStream(tempFile.getPath());
      OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8");
      PrintWriter printWriter = new PrintWriter(outputStreamWriter);
      printWriter.print(contents);
      printWriter.close();
    }
    
    if (file.exists() && !file.delete())
      throw new IOException("Can't delete " + fileName);
  
    if (contents != null)
      if (!tempFile.renameTo(file))
        throw new IOException("Can't rename " + tempFile + " to " + file);
        
    return file;
  } finally {
    action.done();
  }
}

static File saveTextFile(File fileName, String contents) { try {
  saveTextFile(fileName.getPath(), contents);
  return fileName;
} catch (Exception __e) { throw rethrow(__e); } }
public static File mkdirsForFile(File file) {
  File dir = file.getParentFile();
  if (dir != null) { // is null if file is in current dir
    dir.mkdirs();
    if (!dir.isDirectory())
      if (dir.isFile()) throw fail("Please delete the file " + f2s(dir) + " - it is supposed to be a directory!");
      else throw fail("Unknown IO exception during mkdirs of " + f2s(file));
  }
  return file;
}

public static String mkdirsForFile(String path) {
  mkdirsForFile(new File(path));
  return path;
}
static String getCanonicalPath(String path) { try {
  return new File(path).getCanonicalPath();
} catch (Exception __e) { throw rethrow(__e); } }
static FileOutputStream newFileOutputStream(File path) throws IOException {
  return newFileOutputStream(path.getPath());
}

static FileOutputStream newFileOutputStream(String path) throws IOException {
  return newFileOutputStream(path, false);
}

static FileOutputStream newFileOutputStream(File path, boolean append) throws IOException {
  return newFileOutputStream(path.getPath(), append);
}

static FileOutputStream newFileOutputStream(String path, boolean append) throws IOException {
  mkdirsForFile(path);
  FileOutputStream f = new FileOutputStream(path, append);
  
  _registerIO(f, path, true);
  
  return f;
}
static void closeAllWriters(Collection<? extends Writer> l) {
  for (Writer w : unnull(l)) try {
    w.close();
  } catch (Throwable __e) { _handleException(__e); }
}
static <A, B> Collection<B> values(Map<A, B> map) {
  return map == null ? emptyList() : map.values();
}


static <A, B> Collection<B> values(MultiMap<A, B> mm) {
  return mm == null ? emptyList() : concatLists(values(mm.data));
}



static <A> A swingNu(final Class<A> c, final Object... args) {
  return swingConstruct(c, args);
}
static String getSelectedItem(JList l) {
  return (String) l.getSelectedValue();
}

static String getSelectedItem(JComboBox cb) {
  return strOrNull(cb.getSelectedItem());
}
static String makePostData(Map<Object, Object> map) {
  List<String> l = new ArrayList();
  for (Map.Entry<Object, Object> e : map.entrySet()) {
    String key = (String) ( e.getKey());
    Object val = e.getValue();
    if (val != null) {
      String value = str(val); //structureOrText(val);
      l.add(urlencode(key) + "=" + urlencode(/*escapeMultichars*/(value)));
    }
  }
  return join("&", l);
}

static String makePostData(Object... params) {
  return makePostData(litorderedmap(params));
}

static HashMap litmap(Object... x) {
  HashMap map = new HashMap();
  litmap_impl(map, x);
  return map;
}

static void litmap_impl(Map map, Object... x) {
  for (int i = 0; i < x.length-1; i += 2)
    if (x[i+1] != null)
      map.put(x[i], x[i+1]);
}
static volatile boolean muricaPassword_pretendNotAuthed;

static String muricaPassword() {
  if (muricaPassword_pretendNotAuthed) return null;
  return trim(loadTextFile(muricaPasswordFile()));
}
static String standardCredentialsUser() {
  return trim(loadTextFile(new File(userHome(), ".tinybrain/username")));
}
static String standardCredentialsPass() {
  return trim(loadTextFile(new File(userHome(), ".tinybrain/userpass")));
}
static String urlencode(String x) {
  try {
    return URLEncoder.encode(unnull(x), "UTF-8");
  } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); }
}


static <A> A popLast(List<A> l) {
  return liftLast(l);
}
static File getGlobalCache() {
  File file = new File(javaxCachesDir(), "Binary Snippets");
  file.mkdirs();
  return file;
}

  /** writes safely (to temp file, then rename) */
  public static void saveBinaryFile(String fileName, byte[] contents) throws IOException {
    File file = new File(fileName);
    File parentFile = file.getParentFile();
    if (parentFile != null)
      parentFile.mkdirs();
    String tempFileName = fileName + "_temp";
    FileOutputStream fileOutputStream = newFileOutputStream(tempFileName);
    fileOutputStream.write(contents);
    fileOutputStream.close();
    if (file.exists() && !file.delete())
      throw new IOException("Can't delete " + fileName);

    if (!new File(tempFileName).renameTo(file))
      throw new IOException("Can't rename " + tempFileName + " to " + fileName);
  }

  static void saveBinaryFile(File fileName, byte[] contents) {
    try {
      saveBinaryFile(fileName.getPath(), contents);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
static String dataSnippetLink(String snippetID) {
  long id = parseSnippetID(snippetID);
  if (id >= 1100000 && id < 1200000)
    return "http://ai1.space/images/raw/" + id;
  if (id >= 1400000 && id < 1500000)
    return "http://butter.botcompany.de:8080/files/" + id + "?_pass=" + muricaPassword();
  if (id >= 1200000 && id < 1300000) { // Woody files, actually
    String pw = muricaPassword();
    if (empty(pw)) throw fail("Please set 'murica password by running #1008829");
    return "http://butter.botcompany.de:8080/1008823/raw/" + id + "?_pass=" + pw; // XXX, although it typically gets hidden when printing
  } else
    return "http://eyeocr.sourceforge.net/filestore/filestore.php?cmd=serve&file=blob_"
      + id + "&contentType=application/binary";
}
static ThreadLocal<Map<String, List<String>>> loadBinaryPage_responseHeaders = new ThreadLocal();
static ThreadLocal<Map<String, String>> loadBinaryPage_extraHeaders = new ThreadLocal();

static byte[] loadBinaryPage(String url) { try {
  print("Loading " + url);
  return loadBinaryPage(loadPage_openConnection(new URL(url)));
} catch (Exception __e) { throw rethrow(__e); } }

static byte[] loadBinaryPage(URLConnection con) { try {
  Map<String, String> extraHeaders = getAndClearThreadLocal(loadBinaryPage_extraHeaders);
  setHeaders(con);
  for (String key : keys(extraHeaders))
    con.setRequestProperty(key, extraHeaders.get(key));
  return loadBinaryPage_noHeaders(con);
} catch (Exception __e) { throw rethrow(__e); } }

static byte[] loadBinaryPage_noHeaders(URLConnection con) { try {
  ByteArrayOutputStream buf = new ByteArrayOutputStream();
  InputStream inputStream = con.getInputStream();
  loadBinaryPage_responseHeaders.set(con.getHeaderFields());
  long len = 0;
  try { len = con.getContentLength/*Long*/(); } catch (Throwable e) { printStackTrace(e); }
int n = 0;
  while (true) {
    int ch = inputStream.read();
    if (ch < 0)
      break;
    buf.write(ch);
    if (++n % 100000 == 0)
      println("  " + n + (len != 0 ? "/" + len : "") + " bytes loaded.");
  }
  inputStream.close();
  return buf.toByteArray();
} catch (Exception __e) { throw rethrow(__e); } }

static long getFileSize(String path) {
  return path == null ? 0 : new File(path).length();
}

static long getFileSize(File f) {
  return f == null ? 0 : f.length();
}
static <B, A extends B> A addAndReturn(Collection<B> c, A a) {
  if (c != null) c.add(a);
  return a;
}
static void loadBinaryPageToFile(String url, File file) { try {
  print("Loading " + url);
  loadBinaryPageToFile(openConnection(new URL(url)), file);
} catch (Exception __e) { throw rethrow(__e); } }

static void loadBinaryPageToFile(URLConnection con, File file) { try {
  setHeaders(con);
  loadBinaryPageToFile_noHeaders(con, file);
} catch (Exception __e) { throw rethrow(__e); } }

static void loadBinaryPageToFile_noHeaders(URLConnection con, File file) { try {
  File ftemp = new File(f2s(file) + "_temp");
  FileOutputStream buf = newFileOutputStream(mkdirsFor(ftemp));
  try {
    InputStream inputStream = con.getInputStream();
    long len = 0;
    try { len = con.getContentLength/*Long*/(); } catch (Throwable e) { printStackTrace(e); }
    String pat = "  {*}" + (len != 0 ? "/" + len : "") + " bytes loaded.";
    copyStreamWithPrints(inputStream, buf, pat);
    inputStream.close();
    buf.close();
    file.delete();
    renameFile_assertTrue(ftemp, file);
  } finally {
    if (buf != null) buf.close();
  }
} catch (Exception __e) { throw rethrow(__e); } }

static List<String> allToString(Collection c) {
  List<String> l = new ArrayList();
  for (Object o : unnull(c)) l.add(str(o));
  return l;
}

static List<String> allToString(Object[] c) {
  List<String> l = new ArrayList();
  for (Object o : unnull(c)) l.add(str(o));
  return l;
}
static byte[] bytesFromHex(String s) {
  return hexToBytes(s);
}
static boolean byteArrayStartsWith(byte[] a, byte[] b) {
  if (a == null || b == null) return false;
  if (a.length < b.length) return false;
  for (int i = 0; i < b.length; i++)
    if (a[i] != b[i])
      return false;
  return true;
}
static byte[] loadBeginningOfBinaryFile(File file, int maxBytes) {
  return loadBinaryFilePart(file, 0, maxBytes);
}
static String getLookAndFeel() {
  return getClassName(UIManager.getLookAndFeel());
}
static JComponent getInternalFrameTitlePaneComponent(JInternalFrame f) {
  return (JComponent) childWithClassNameEndingWith(f, "InternalFrameTitlePane");
}

static JInternalFrame getInternalFrame(final Object _o) {
  return swing(new F0<JInternalFrame>() { JInternalFrame get() { try { 
    Object o = _o;
    if (o instanceof ButtonGroup) o = first(buttonsInGroup((ButtonGroup) o));
    if (!(o instanceof Component)) return null;
    Component c = (Component) o;
    while (c != null) {
      if (c instanceof JInternalFrame) return (JInternalFrame) c;
      c = c.getParent();
    }
    return null;
   } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "O o = _o;\r\n    if (o instanceof ButtonGroup) o = first(buttonsInGroup((Button..."; }});
}
static <A> A optCast(Class<A> c, Object o) {
  return isInstance(c, o) ? (A) o : null;
}
static boolean isLetterOrDigit(char c) {
  return Character.isLetterOrDigit(c);
}
static TreeMap<String,Class> hotwireCached_cache = new TreeMap();
static Lock hotwireCached_lock = lock();

static Class hotwireCached(String programID) {
  return hotwireCached(programID, true);
}

static Class hotwireCached(String programID, boolean runMain) {
  return hotwireCached(programID, runMain, false);
}

static Class hotwireCached(String programID, boolean runMain, boolean dependent) {
  Lock __677 = hotwireCached_lock; lock(__677); try {
  
  programID = formatSnippetID(programID);
  Class c = hotwireCached_cache.get(programID);
  if (c == null) {
    c = hotwire(programID);
    if (dependent)
      makeDependent(c);
    if (runMain)
      callMain(c);
    hotwireCached_cache.put(programID, c);
  }
  return c;
} finally { unlock(__677); } }
static Map<String, Class> classForName_cache = synchroHashMap();

static Class classForName(String name) { try {
  Class c = classForName_cache.get(name);
  if (c == null)
    classForName_cache.put(name, c = Class.forName(name));
  return c;
} catch (Exception __e) { throw rethrow(__e); } }
static Map<Class, Constructor> nuObjectWithoutArguments_cache = newDangerousWeakHashMap();

static Object nuObjectWithoutArguments(String className) { try {
  return nuObjectWithoutArguments(classForName(className));
} catch (Exception __e) { throw rethrow(__e); } }

static <A> A nuObjectWithoutArguments(Class<A> c) { try {
  if (nuObjectWithoutArguments_cache == null)
    // in class init
    return (A) nuObjectWithoutArguments_findConstructor(c).newInstance();
    
  Constructor m = nuObjectWithoutArguments_cache.get(c);
  if (m == null)
    nuObjectWithoutArguments_cache.put(c, m = nuObjectWithoutArguments_findConstructor(c));
  return (A) m.newInstance();
} catch (Exception __e) { throw rethrow(__e); } }

static Constructor nuObjectWithoutArguments_findConstructor(Class c) {
  for (Constructor m : c.getDeclaredConstructors())
    if (empty(m.getParameterTypes())) {
      m.setAccessible(true);
      return m;
    }
  throw fail("No default constructor found in " + c.getName());
}

static List<Class> getClasses(Object[] array) {
  List<Class> l = new ArrayList();
  for (Object o : array) l.add(_getClass(o));
  return l;
}
static boolean sameSnippetID(String a, String b) {
  if (!isSnippetID(a) || !isSnippetID(b)) return false;
  return parseSnippetID(a) == parseSnippetID(b);
}
static volatile String caseID_caseID;

static String caseID() { return caseID_caseID; }

static void caseID(String id) {
  caseID_caseID = id;
}
static List<CriticalAction> beginCriticalAction_inFlight = synchroList();

static class CriticalAction {
  String description;
  
  CriticalAction() {}
  CriticalAction(String description) {
  this.description = description;}
  
  void done() {
    beginCriticalAction_inFlight.remove(this);
  }
}

static CriticalAction beginCriticalAction(String description) {
  ping();
  CriticalAction c = new CriticalAction(description);
  beginCriticalAction_inFlight.add(c);
  return c;
}

static void cleanMeUp_beginCriticalAction() {
  int n = 0;
  while (nempty(beginCriticalAction_inFlight)) {
    int m = l(beginCriticalAction_inFlight);
    if (m != n) {
      n = m;
      try {
        print("Waiting for " + n2(n, "critical actions") + ": " + join(", ", collect(beginCriticalAction_inFlight, "description")));
      } catch (Throwable __e) { _handleException(__e); }
    }
    sleepInCleanUp(10);
  }
}
public static void copyFile(File src, File dest) { try {
  FileInputStream inputStream = new FileInputStream(src.getPath());
  FileOutputStream outputStream = newFileOutputStream(dest.getPath());
  try {
    copyStream(inputStream, outputStream);
    inputStream.close();
  } finally {
    outputStream.close();
  }
} catch (Exception __e) { throw rethrow(__e); } }
static void _registerIO(Object object, String path, boolean opened) {
}
static <A> List<A> concatLists(Collection<A>... lists) {
  List<A> l = new ArrayList();
  for (Collection<A> list : lists)
    if (list != null)
      l.addAll(list);
  return l;
}

static <A> List<A> concatLists(Collection<? extends Collection<A>> lists) {
  List<A> l = new ArrayList();
  for (Collection<A> list : lists)
    if (list != null)
      l.addAll(list);
  return l;
}

static String strOrNull(Object o) {
  return o == null ? null : str(o);
}
static LinkedHashMap litorderedmap(Object... x) {
  LinkedHashMap map = new LinkedHashMap();
  litmap_impl(map, x);
  return map;
}
static File muricaPasswordFile() {
  return new File(javaxSecretDir(), "murica/muricaPasswordFile");
}


static <A> A liftLast(List<A> l) {
  if (l.isEmpty()) return null;
  int i = l(l)-1;
  A a = l.get(i);
  l.remove(i);
  return a;
}
static File javaxCachesDir_dir; // can be set to work on different base dir

static File javaxCachesDir() {
  return javaxCachesDir_dir != null ? javaxCachesDir_dir : new File(userHome(), "JavaX-Caches");
}

static File javaxCachesDir(String sub) {
  return newFile(javaxCachesDir(), sub);
}
static <A> A println(A a) {
  return print(a);
}
public static File mkdirsFor(File file) {
  return mkdirsForFile(file);
}

static void copyStreamWithPrints(InputStream in, OutputStream out, String pat) { try {
  byte[] buf = new byte[65536];
  int total = 0;
  while (true) {
    int n = in.read(buf);
    if (n <= 0) return;
    out.write(buf, 0, n);
    if ((total+n)/100000 > total/100000)
      print(pat.replace("{*}", str(roundDownTo(total, 100000))));
    total += n;
  }
} catch (Exception __e) { throw rethrow(__e); } }
static void renameFile_assertTrue(File a, File b) { try {
  if (!a.exists()) throw fail("Source file not found: " + f2s(a));
  if (b.exists()) throw fail("Target file exists: " + f2s(b));
  mkdirsForFile(b);
  
  
  if (!a.renameTo(b))
    throw fail("Can't rename " + f2s(a) + " to " + f2s(b));
  
} catch (Exception __e) { throw rethrow(__e); } }
static byte[] hexToBytes(String s) {
  if (odd(l(s))) throw fail("Hex string has odd length: " + quote(shorten(10, s)));
  int n = l(s) / 2;
  byte[] bytes = new byte[n];
  for (int i = 0; i < n; i++) {
    int a = parseHexChar(s.charAt(i*2));
    int b = parseHexChar(s.charAt(i*2+1));
    if (a < 0 || b < 0)
      throw fail("Bad hex byte: " + quote(substring(s, i*2, i*2+2)) + " at " + i*2 + "/" + l(s));
    bytes[i] = (byte) ((a << 4) | b);
  }
  return bytes;
}
static byte[] loadBinaryFilePart(File file, long start, long end) { try {
  RandomAccessFile raf = new RandomAccessFile(file, "r");
  int n = toInt(min(raf.length(), end-start));
  byte[] buffer = new byte[n];
  try {
    raf.seek(start);
    raf.readFully(buffer, 0, n);
    return buffer;
  } finally {
    raf.close();
  }
} catch (Exception __e) { throw rethrow(__e); } }
static Component childWithClassNameEndingWith(Component c, String suffix) {
  if (endsWith(className(c), suffix)) return c;
  Component x;
  for (Component comp : getComponents(c))
    if ((x = childWithClassNameEndingWith(comp, suffix)) != null) return x;
  return null;
}
static boolean isInstance(Class type, Object arg) {
  return type.isInstance(arg);
}
static Class<?> hotwire(String src) {
  assertFalse(_inCore());
  Class j = getJavaX();
  if (isAndroid()) {
    synchronized(j) { // hopefully this goes well...
      List<File> libraries = new ArrayList<File>();
      File srcDir = (File) call(j, "transpileMain", src, libraries);
      if (srcDir == null)
        throw fail("transpileMain returned null (src=" + quote(src) + ")");
    
      Object androidContext = get(j, "androidContext");
      return (Class) call(j, "loadx2android", srcDir, src);
    }
  } else {
    
    
    Class c = (Class) ( call(j, "hotwire", src));
    hotwire_copyOver(c);
    return c;
    
  }
}
static void makeDependent(Object c) {
  if (c == null) return;
  assertTrue("Not a class", c instanceof Class);
  dependentClasses(); // cleans up the list
  hotwire_classes.add(new WeakReference(c));
  
  Object local_log = getOpt(mc(), "local_log");
  if (local_log != null)
    setOpt(c, "local_log", local_log);
    
  /*if (isTrue(getOpt(c, 'ping_actions_shareable)))
    setOpt(c, +ping_actions);*/
    
  Object print_byThread = getOpt(mc(), "print_byThread");
  if (print_byThread != null)
    setOpt(c, "print_byThread", print_byThread);
}

static <A> A callMain(A c, String... args) {
  callOpt(c, "main", new Object[] {args});
  return c;
}

static void callMain() {
  callMain(mc());
}
static Class<?> _getClass(String name) {
  try {
    return Class.forName(name);
  } catch (ClassNotFoundException e) {
    return null; // could optimize this
  }
}

static Class _getClass(Object o) {
  return o == null ? null
    : o instanceof Class ? (Class) o : o.getClass();
}

static Class _getClass(Object realm, String name) { try {
  return getClass(realm).getClassLoader().loadClass(classNameToVM(name));
} catch (Exception __e) { throw rethrow(__e); } }
static String n2(long l) { return formatWithThousands(l); }
static String n2(Collection l) { return n2(l(l)); }

static String n2(long l, String singular, String plural) {
  return n_fancy2(l, singular, plural);
}

static String n2(long l, String singular) {
  return n_fancy2(l, singular, singular + "s");
}

static String n2(Collection l, String singular) {
  return n2(l(l), singular);
}

static String n2(Collection l, String singular, String plural) {
  return n_fancy2(l, singular, plural);
}

static String n2(Map m, String singular, String plural) {
  return n_fancy2(m, singular, plural);
}

static String n2(Map m, String singular) {
  return n2(l(m), singular);
}

static String n2(Object[] a, String singular) { return n2(l(a), singular); }
static String n2(Object[] a, String singular, String plural) { return n_fancy2(a, singular, plural); }


static List collect(Collection c, String field) {
  return collectField(c, field);
}

static List collect(String field, Collection c) {
  return collectField(c, field);
}


static void sleepInCleanUp(long ms) { try {
  if (ms < 0) return;
  Thread.sleep(ms);
} catch (Exception __e) { throw rethrow(__e); } }
static void copyStream(InputStream in, OutputStream out) { try {
  byte[] buf = new byte[65536];
  while (true) {
    int n = in.read(buf);
    if (n <= 0) return;
    out.write(buf, 0, n);
  }
} catch (Exception __e) { throw rethrow(__e); } }
static File javaxSecretDir_dir; // can be set to work on different base dir

static File javaxSecretDir() {
  return javaxSecretDir_dir != null ? javaxSecretDir_dir : new File(userHome(), "JavaX-Secret");
}

static File javaxSecretDir(String sub) {
  return newFile(javaxSecretDir(), sub);
}


static int roundDownTo(int x, int n) {
  return x/n*n;
}

static long roundDownTo(long x, long n) {
  return x/n*n;
}
static boolean odd(int i) {
  return (i & 1) != 0;
}

static boolean odd(long i) {
  return (i & 1) != 0;
}

static boolean odd(BigInteger i) { return odd(toInt(i)); }
static int shorten_default = 100;

static String shorten(String s) { return shorten(s, shorten_default); }

static String shorten(String s, int max) {
  return shorten(s, max, "...");
}

static String shorten(String s, int max, String shortener) {
  if (s == null) return "";
  if (max < 0) return s;
  return s.length() <= max ? s : substring(s, 0, min(s.length(), max-l(shortener))) + shortener;
}

static String shorten(int max, String s) { return shorten(s, max); }
static int parseHexChar(char c) {
  if (c >= '0' && c <= '9') return charDiff(c, '0');
  if (c >= 'a' && c <= 'f') return charDiff(c, 'a')+10;
  if (c >= 'A' && c <= 'F') return charDiff(c, 'A')+10;
  return -1;
}
static int min(int a, int b) {
  return Math.min(a, b);
}

static long min(long a, long b) {
  return Math.min(a, b);
}

static float min(float a, float b) { return Math.min(a, b); }
static float min(float a, float b, float c) { return min(min(a, b), c); }

static double min(double a, double b) {
  return Math.min(a, b);
}

static double min(double[] c) {
  double x = Double.MAX_VALUE;
  for (double d : c) x = Math.min(x, d);
  return x;
}

static float min(float[] c) {
  float x = Float.MAX_VALUE;
  for (float d : c) x = Math.min(x, d);
  return x;
}

static byte min(byte[] c) {
  byte x = 127;
  for (byte d : c) if (d < x) x = d;
  return x;
}

static short min(short[] c) {
  short x = 0x7FFF;
  for (short d : c) if (d < x) x = d;
  return x;
}

static int min(int[] c) {
  int x = Integer.MAX_VALUE;
  for (int d : c) if (d < x) x = d;
  return x;
}
static List<Component> getComponents(final Component c) {
  return !(c instanceof Container) ? emptyList() : asList(swing(new F0<Component[]>() { Component[] get() { try { return  ((Container) c).getComponents() ; } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "((Container) c).getComponents()"; }}));
}
static boolean _inCore() {
  return false;
}
static List hotwire_copyOver_after = synchroList();

static void hotwire_copyOver(Class c) {
  // TODO: make a mechanism for making such "inheritable" fields
  for (String field : ll("print_log", "print_silent", "androidContext", "_userHome"))
    setOptIfNotNull(c, field, getOpt(mc(), field));
    
  
  
  setOptIfNotNull(c, "mainBot" , getMainBot());
  setOpt(c, "creator_class" , new WeakReference(mc()));
  pcallFAll(hotwire_copyOver_after, c);
}
static List<Class> dependentClasses() {
  return cleanUpAndGetWeakReferencesList(hotwire_classes);
}
static Field setOpt_findField(Class c, String field) {
  HashMap<String, Field> map;
  synchronized(getOpt_cache) {
    map = getOpt_cache.get(c);
    if (map == null)
      map = getOpt_makeCache(c);
  }
  return map.get(field);
}

static void setOpt(Object o, String field, Object value) { try {
  if (o == null) return;
  
  
  
  Class c = o.getClass();
  HashMap<String, Field> map;
  
  if (getOpt_cache == null)
    map = getOpt_makeCache(c); // in class init
  else synchronized(getOpt_cache) {
    map = getOpt_cache.get(c);
    if (map == null)
      map = getOpt_makeCache(c);
  }
  
  if (map == getOpt_special) {
    if (o instanceof Class) {
      setOpt((Class) o, field, value);
      return;
    }
    
    // It's probably a subclass of Map. Use raw method
    setOpt_raw(o, field, value);
    return;
  }
  
  Field f = map.get(field);
  if (f != null)
    smartSet(f, o, value); // possible improvement: skip setAccessible
} catch (Exception __e) { throw rethrow(__e); } }

static void setOpt(Class c, String field, Object value) {
  if (c == null) return;
  try {
    Field f = setOpt_findStaticField(c, field);
    if (f != null)
      smartSet(f, null, value);
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
}
  
static Field setOpt_findStaticField(Class<?> c, String field) {
  Class _c = c;
  do {
    for (Field f : _c.getDeclaredFields())
      if (f.getName().equals(field) && (f.getModifiers() & java.lang.reflect.Modifier.STATIC) != 0) {
        f.setAccessible(true);
        return f;
      }
    _c = _c.getSuperclass();
  } while (_c != null);
  return null;
}
static Class<?> getClass(String name) {
  try {
    return Class.forName(name);
  } catch (ClassNotFoundException e) {
    return null;
  }
}

static Class getClass(Object o) {
  return o instanceof Class ? (Class) o : o.getClass();
}

static Class getClass(Object realm, String name) { try {
  try {
    return getClass(realm).getClassLoader().loadClass(classNameToVM(name));
  } catch (ClassNotFoundException e) {
    return null;
  }
} catch (Exception __e) { throw rethrow(__e); } }
static String classNameToVM(String name) {
  return name.replace(".", "$");
}
static String formatWithThousands(long l) {
  return formatWithThousandsSeparator(l);
}
static String n_fancy2(long l, String singular, String plural) {
  return formatWithThousandsSeparator(l) + " " + trim(l == 1 ? singular : plural);
}

static String n_fancy2(Collection l, String singular, String plural) {
  return n_fancy2(l(l), singular, plural);
}

static String n_fancy2(Map m, String singular, String plural) {
  return n_fancy2(l(m), singular, plural);
}

static String n_fancy2(Object[] a, String singular, String plural) {
  return n_fancy2(l(a), singular, plural);
}


static List collectField(Collection c, String field) {
  List l = new ArrayList();
  if (c != null) for (Object a : c)
    l.add(getOpt(a, field));
  return l;
}

static List collectField(String field, Collection c) {
  return collectField(c, field);
}


static List<WeakReference<Class>> hotwire_classes = synchroList();

static Class<?> hotwireDependent(String src) {
  Class c = hotwire(src);
  makeDependent(c);
  return c;
}

static int charDiff(char a, char b) {
  return (int) a-(int) b;
}

static int charDiff(String a, char b) {
  return charDiff(stringToChar(a), b);
}
static void setOptIfNotNull(Object o, String field, Object value) {
  if (value != null) setOpt(o, field, value);
}
static Object mainBot;

static Object getMainBot() {
  return mainBot;
}
static <A> List<A> cleanUpAndGetWeakReferencesList(List<WeakReference<A>> l) {
  synchronized(l) {
    List<A> out = new ArrayList();
    for (int i = 0; i < l(l); i++) {
      A a = l.get(i).get();
      if (a == null)
        l.remove(i--);
      else
        out.add(a);
    }
    return out;
  }
}
static void setOpt_raw(Object o, String field, Object value) { try {
  if (o == null) return;
  if (o instanceof Class) setOpt_raw((Class) o, field, value);
  else {
    Field f = setOpt_raw_findField(o.getClass(), field);
    if (f != null) {
      f.setAccessible(true);
      smartSet(f, o, value);
    }
  }
} catch (Exception __e) { throw rethrow(__e); } }

static void setOpt_raw(Class c, String field, Object value) { try {
  if (c == null) return;
  Field f = setOpt_raw_findStaticField(c, field);
  if (f != null) {
    f.setAccessible(true);
    smartSet(f, null, value);
  }
} catch (Exception __e) { throw rethrow(__e); } }
  
static Field setOpt_raw_findStaticField(Class<?> c, String field) {
  Class _c = c;
  do {
    for (Field f : _c.getDeclaredFields())
      if (f.getName().equals(field) && (f.getModifiers() & java.lang.reflect.Modifier.STATIC) != 0)
        return f;
    _c = _c.getSuperclass();
  } while (_c != null);
  return null;
}

static Field setOpt_raw_findField(Class<?> c, String field) {
  Class _c = c;
  do {
    for (Field f : _c.getDeclaredFields())
      if (f.getName().equals(field))
        return f;
    _c = _c.getSuperclass();
  } while (_c != null);
  return null;
}
static void smartSet(Field f, Object o, Object value) throws Exception {
  try {
    f.set(o, value);
  } catch (Exception e) {
    Class type = f.getType();
    
    // take care of common case (long to int)
    if (type == int.class && value instanceof Long)
      value = ((Long) value).intValue();
    
    if (type == LinkedHashMap.class && value instanceof Map)
      { f.set(o, asLinkedHashMap((Map) value)); return; }
    
    
    throw e;
  }
}


static String formatWithThousandsSeparator(long l) {
  return NumberFormat.getInstance(new Locale("en_US")).format(l);
}


static char stringToChar(String s) {
  if (l(s) != 1) throw fail("bad stringToChar: " + s);
  return firstChar(s);
}
static <A, B> LinkedHashMap<A, B> asLinkedHashMap(Map<A, B> map) {
  if (map instanceof LinkedHashMap) return (LinkedHashMap) map;
  LinkedHashMap<A,B> m = new LinkedHashMap();
  if (map != null) synchronized(collectionMutex(map)) {
    m.putAll(map);
  }
  return m;
}


static char firstChar(String s) {
  return s.charAt(0);
}


static abstract class VF1<A> {
  abstract void get(A a);
}static class BetterLabel extends JLabel {
  boolean autoToolTip = true;
  
  BetterLabel() {
    // Listeners given out to componentPopupMenu must not directly
    // reference the outer object (-> weak map problem).
    final WeakReference < BetterLabel > me = new WeakReference(this);
    componentPopupMenu(this, BetterLabel_menuItems(me));
  }
  
  BetterLabel(String text) {
    this();
    this.setText(text);
  }
  
  public void setText(String text) {
    super.setText(text);
    if (autoToolTip)
      if (!swic(text, "<html>")) // HTML labels make super-huge, confusing tool tips
        setToolTipText(nullIfEmpty(text));
  }
}

// moved outside of class for GC reasons (see above)
static VF1<JPopupMenu> BetterLabel_menuItems(final WeakReference<BetterLabel> me) {
  return new VF1<JPopupMenu>() { public void get(JPopupMenu menu) { try { 
    addMenuItem(menu, "Copy text to clipboard", new Runnable() {  public void run() { try { 
      copyTextToClipboard(me.get().getText());
    
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "copyTextToClipboard(me.get().getText());"; }});
   } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "addMenuItem(menu, \"Copy text to clipboard\", r {\r\n      copyTextToClipboard(me..."; }};
}// elements are put to front when added (not when accessed)
static class MRUCache<A, B> extends LinkedHashMap<A, B> {
  int maxSize = 10;

  MRUCache() {}
  MRUCache(int maxSize) {
  this.maxSize = maxSize;}
  
  protected boolean removeEldestEntry(Map.Entry eldest) {
    return size() > maxSize;
  }
}static class FactsBot {
  List<Map> recentHistory;
  List<String> outMessages = synchroList();
  
  
  boolean debug;
  boolean imagineMode;
  boolean collectCheckingLog = true;
  LinkedHashSet<String> imaginedFacts = new LinkedHashSet();
  List<String> checkingLog;
  long processingTime;
  List<IfThen> activeTempRules = new ArrayList();
  int evalTimeout = 20000;
  
  void thinkAbout(String s) {
    long _startTime_5 = sysNow(); 
      thinkAbout_impl(s);
     done2_always("Processing Time For Message", _startTime_5); 
    processingTime = lastTiming();
  }
  
  void thinkAbout_impl(final String input) {
    final Thinking t = new Thinking();
    final Map<String, Object> tl = getMCThreadLocals(ai_staticVars());
    /*Thread thread = startThread("Think!", r {
      setMCThreadLocals(tl);*/
      t.thinkAbout(input);
    /*});*/
    int n = 0;
    do {
      while (l(t.output) > n)
        postMessage(t.output.get(n++));
      //joinThread(thread, 50);
    } while (!t.done);
    
    activeTempRules.clear();
  }
  
  void _postMessage(String s) { postMessage(s); }
  
  class Thinking {
    long cancelTime = now()+20000;
    Map<String,Integer> ruleScores = new HashMap();
    LinkedHashSet<String> rewrittenInputs = new LinkedHashSet();
    LinkedHashSet<String> newRewrittenInputs = new LinkedHashSet();
    volatile boolean posted, done;
    List<IfThen> rules = new ArrayList();
    
    PlanInMotion<Runnable> plan = new PlanInMotion();
    HashSet<String> outputSet = new HashSet();
    List<String> output = synchroList();

    void postMessage(String s) {
      //_postMessage(s);
      if (outputSet.add(s))
        output.add(s);
      posted = true;
    }
    
    void done() { done = true; }
    
    void thinkAbout(String input) {
      try {
        thinkAbout_impl(input);
      } finally {
        done();
      }
    }
    
    void thinkAbout_impl(final String input0) {
      final String input1 = unixLineBreaks(input0);
      plan.add(new Runnable() {  public void run() { try { 
        if (!eq(input1, "!initiative")) return;
        List<IfThen> rules = mL_parsedNLRulesWithoutIDs("Initiative rules");
        for (IfThen rule : shuffledIterator(rules)) {
          Pair<Exp, Exp> p = nlLogic_extractFirstCondition(rule.in);
          if (p == null) continue;
          if (nlLogic_isFunc(p.a, "initiative")) {
            postMessage(nlLogic_text(((Func) p.a).arg));
            IfThen temp = new IfThen(p.b, rule.out);
            temp.globalID = aGlobalID();
            print("Have temp rule: " + sfu(temp));
            activeTempRules.add(temp);
            { done(); return; }
          }
        }
        { done(); return; }
      
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "!initiative"; }  public String _shortenedSourceCode() { return "if (!eq(input1, \"!initiative\")) return;\r\n        List<IfThen> rules = mL_pars..."; }});
      
      plan.add(new Runnable() {  public void run() { try { 
        Matches mm = new Matches();
        if (!startsWith_trim(input1, "!step ", mm)) return;
        // TODO
      
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "!step"; }  public String _shortenedSourceCode() { return "new Matches mm;\r\n        if (!startsWith_trim(input1, \"!step \", mm)) return;\r..."; }});
      
      runPlanWithCheck(plan, new F0<Boolean>() { Boolean get() { try { return  !done ; } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "!done"; }});
      if (done) return;
      
      stdCommands(input1);
      
      String input2 = input1;
      final List<String> options = new ArrayList();
      if (startsWith(input2, "[")) {
        addAll(options, leadingSquareBracketOptions(input2));
        input2 = dropLeadingSquareBracketStuff(input2);
      }
      
      final String input = input2;
      ai_tg_collectRuleFeedback(ruleScores, null);
      
      newRewrittenInputs.add(input);
      final Map msg = telegram_msg();
      
      final NLLogicChecker_v3 c = new NLLogicChecker_v3() {
        boolean checkExpression_impl(Exp e, Matching m) {
          if (e instanceof Literal) {
            // TEXT CONDITIONS
            
            String text = nlLogic_text(e);
            if (eq(text, "authorized"))
              return telegram_amIAuthorized();
            else if(eq(text, "imagineMode"))
              return imagineMode;
          }
          
          return super.checkExpression_impl(e, m);
        }
      };

      if (collectCheckingLog) {
        checkingLog = new ArrayList();
        nlLogic_collectCheckingLog(c, checkingLog);
      }
      
      Pair<List<IfThen>, List<String>> rulesAndFacts = getRulesAndFacts();
      c.facts = reversed(rulesAndFacts.b);
      rules = concatLists_conservative(activeTempRules, reversed(rulesAndFacts.a)); // latest rules first!
      rules = filter(rules , new F1<IfThen, Object>() { Object get(IfThen r) { try { return  ai_ruleAccessesInput(r.originalText) ; } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "ai_ruleAccessesInput(r.originalText)"; }});
      c.rules = rules;

      while (!posted && now() < cancelTime && nempty(newRewrittenInputs)) {
        final String s = first(newRewrittenInputs);
        rewrittenInputs.add(s);
        newRewrittenInputs.remove(s);
        
        c.input = s;
        c.recentHistory = recentHistory;
        
        final List<RuleWithParams> battleSpace = new ArrayList();
        
        fillBattleSpace(c, battleSpace);
        
        print("Have " + n2(battleSpace, "possible rule")
          + (empty(battleSpace) ? "" : ": " + joinWithComma(map(new F1<RuleWithParams, String>() { String get(RuleWithParams r) { try { return  r.ruleID() + "/" + toInt(ruleScores.get(r.ruleID())) ; } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "r.ruleID() + \"/\" + toInt(ruleScores.get(r.ruleID()))"; }}, battleSpace))));
        
        boolean all = options.contains("all");
        if (!all) nlLogic_battleItOut(battleSpace, c.facts);
        
        List<RuleWithParams> winners = all ? battleSpace : nlLogic_highestScore(battleSpace, ruleScores);
    
        final String msgGlobalID = getString(msg, "globalID");
        //print("msgGlobalID=" + msgGlobalID);
        for (RuleWithParams r : winners) {
          print("Firing rule " + r.rule.globalID);
          
          // Save rule fire
          if (nempty(msgGlobalID) && nempty(r.rule.globalID)) {
            String fact = format("Rule * fired on message * at *", r.rule.globalID, msgGlobalID, localDateWithMilliseconds());
             if (nempty(r.matches))
               fact += " with vars " + dropPrefix("lhm", struct(r.matches));
            print("Saving rule fire. Mech mode: " + mechMode());
            print(addToMechList("Telegram Rule Fires", print(fact)));
          }
          
          executeRule(c, r, this);
        }
        
        if (collectCheckingLog)
          print("Checking log length: " + l(checkingLog));
      }
    }

    void fillBattleSpace(NLLogicChecker_v3 c, final List<RuleWithParams> battleSpace) {
      applyNLLogicFacts_v4_verbose.set(debug);
      applyNLLogicFacts_v4(c, new VF2<IfThen, NLLogicChecker_v2.Matching>() { public void get(IfThen rule, NLLogicChecker_v2.Matching m) { try { 
        // Rule matches completely, add to battle space
        
        battleSpace.add(new RuleWithParams(rule, m.matches));
       } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "// Rule matches completely, add to battle space\r\n        \r\n        battleSpac..."; }}, rules);
    }
  } // end class Thinking

  void executeRule(NLLogicChecker_v3 c, RuleWithParams r, Thinking t) {
    // Execute rule
    for (Exp e : nlLogic_unrollAnd(r.rule.out)) {
      e = c.apply(e, r.matches);
      if (e instanceof Func) {
        String name = ((Func) e).name, arg = nlLogic_text(((Func) e).arg);
        if (eqOneOf(name, "output", "say"))
          t.postMessage(arg);
        else if (eq(name, "fact")) {
          String fact = arg;
          if (imagineMode) {
            if (!containsNL(imaginedFacts, fact)) {
              imaginedFacts.add(fact);
              t.postMessage("Storing imaginary fact: " + fact);
            }
          } else if (!ai_isQuestion_2(fact) && ai_storeActualFact(fact))
            t.postMessage("Storing fact: " + fact);
        } else if (eq(name, "storeRule")) {
          String rule = arg;
          if (!telegram_authorizedToStoreRule(rule)) continue;
          rule = nlLogic_addGlobalID(rule);
          if (mech_changed(appendToMechList_noUniq("NL Logic Examples", "\n" + rule)))
            t.postMessage("Rule stored");
        } else if (eq(name, "imagineMode")) {
          imagineMode = match("true", arg);
          if (!imagineMode) imaginedFacts.clear();
        } else if (eq(name, "input")) {
          String x = arg;
          if (!t.rewrittenInputs.contains(x) && t.newRewrittenInputs.add(x))
            print("New rewritten input: " + x);
        } else if (c.checkHelper(((Func) e), ai_matchingWrapping(r.matches))) {
        } else
          { print("Skipping rule with unknown RHS func: " + ((Func) e).name); return; }
      } else
        { print("Skipping rule with unknown RHS element: " + e); return; }
    }
  }
 
  Pair<List<IfThen>, List<String>> getRulesAndFacts() { 
    return ai_activeRulesAndFacts(imagineMode ? asList(imaginedFacts) : null);
  }

  void stdCommands(String s) {
    s = trim(s);
    
    final Matches m = new Matches();
    String procedure = nlLookup(mechMap("Telegram Procedures"), s, m);
    if (procedure != null) {
      procedure = expandDollarRefsToMatches(procedure, m, true);
      print(">> " + procedure);
      postMessage(strOrNull(javaEvalOrInterpret(procedure)));
    }
    
    print("s=" + s);
    String language = "english";
    if (swic_trim(s, "!german ", m)) {
      language = "german";
      s = m.rest();
      print("s=" + s);
    }
    ai_setLanguage(language);

    if (swic_trim(s, "!say ", m))
      postMessage(m.rest());
    
    if (eq(s, "!gac"))
      postMessage(random_gac36k());
    
    if (swic_trim(s, "!parse ", m))
      postMessage(ai_renderCulledParseTree(ai_parseToTreeWithGuessing(m.rest())));
    
    if (swic_trim(s, "!simplify ", m))
      postMessage(lines(ai_parseTree_simplifiedTexts(ai_parseToTreeWithGuessing(m.rest()))));
      
    if (swicOneOf_trim(s, m, "!factsFrom ", "!factsFrom\n")) {
      String x = m.rest();
      ai_deriveFacts_debug = leadingSquareBracketOptions(x).contains("debug");
       AutoCloseable __4 = tempSet(NLLogicChecker_v2.class, "staticVerbose" , ai_deriveFacts_debug); try {
      final String _x = dropActuallyLeadingSquareBracketStuff(x);
      postMessage(or2(evalWithTimeout_text(evalTimeout, new F0<Object>() { Object get() { try { return 
        lines(ai_factsFromNewFacts(tlft(_x)))
      ; } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "lines(ai_factsFromNewFacts(tlft(_x)))"; }}), "No new facts found"));
    } finally { _close(__4); }}
    
    if (swic_trim(s, "!askBack ", m)) {
      NLLogicChecker_v2 c = nlLogicCheckerWithRulesAndFacts(getRulesAndFacts());
      c.input = m.rest();
      askBack(c);
    }

    if (swic_trim(s, "!askBackAboutFact ", m)) {
      NLLogicChecker_v2 c = nlLogicCheckerWithRulesAndFacts(getRulesAndFacts());
      c.facts = listPlus_inFront(c.facts, m.rest());
      askBack(c);
    }

    /*if (telegram_amIAuthorized())*/ {
      if (telegram_amIFullyAuthorized()) {
        boolean freshMe = swic_trim(s, "!fresh ", m);
        if (freshMe || eq(s, "!fresh")) { dm_refreshTranspiler(); if (!freshMe) postMessage("OK"); }
      
        if (swic_trim(s, "!eval ", m))
          postMessage(pcallOrExceptionText(new F0<Object>() { Object get() { try { return  strOrNull(javaEvalOrInterpret(m.rest())) ; } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "strOrNull(javaEvalOrInterpret(m.rest()))"; }}));
        
        if (freshMe || swic_trim(s, "!real-eval ", m))
          postMessage(evalWithTimeout_text(evalTimeout, new F0<Object>() { Object get() { try { return 
            javaEval(m.rest())
          ; } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "javaEval(m.rest())"; }}));
      }
        
      if (swic_trim(s, "!rule ", m)) {
        String rule = m.rest();
        if (!telegram_authorizedToStoreRule(rule)) return;
        List<String> rules = trimAll(ai_unparsedTalkRules());
        if (!contains(rules, rule)) { // TODO: ignore global IDs
          rule = nlLogic_addGlobalID(rule);
          appendToMechList_noUniq("NL Logic Examples", "\n" + rule);
          postMessage("Rule saved as " + leadingSquareBracketOptions_id(rule) + ". Have " + n2(l(rules)+1, "rule") + ".");
        }
      }
      
      if (swic_trim(s, "!fact ", m)) {
        String fact = m.rest();
        List<String> facts = mL("Random facts");
        if (!contains(facts, fact)) {
          appendToMechList_noUniq("Random facts", fact);
          postMessage("Fact saved. Have " + n2(l(facts)+1, "fact") + ".");
        }
      }
      
      if (eqic(s, "!deriveFacts"))
        postMessage("Got " + n2(ai_applyFactToFactRules(), "fact") + ". Total: " + l(mL("Derived facts")));
        
      // end of authorized functions
    }
  }

  void askBack(NLLogicChecker_v2 c) {  
    List<RuleFailInfo> fails = nlLogic_sortedRuleFails(nlLogic_ruleFails(c, c.rules));
    print("Have " + n2(fails, "fail"));
    //pnl(fails);
    postMessage(lines(nlLogic_unresolvedFactsToQuestions(fails)));
  }

void postMessage(String s) {
    outMessages.add(s);
  }
}static class Var<A> implements IVar<A> {
  A v; // you can access this directly if you use one thread
  
  Var() {}
  Var(A v) {
  this.v = v;}
  
  public synchronized void set(A a) {
    if (v != a) {
      v = a;
      notifyAll();
    }
  }
  
  public synchronized A get() { return v; }
  public synchronized boolean has() { return v != null; }
  public synchronized void clear() { v = null; }
  
  public String toString() { return str(get()); }
}static class PersistableThrowable {
  String className;
  String msg;
  String stacktrace;
  
  PersistableThrowable() {}
  PersistableThrowable(Throwable e) {
    if (e == null)
      className = "Crazy Null Error";
    else {
      className = getClassName(e).replace('/', '.');
      msg = e.getMessage();
      stacktrace = getStackTrace_noRecord(e);
    }
  }
  
  public String toString() {
    return nempty(msg) ? className + ": " + msg : className;
  }
}static abstract class F0<A> {
  abstract A get();
}static class Pair<A, B> implements Comparable<Pair<A, B>> {
  A a;
  B b;

  Pair() {}
  Pair(A a, B b) {
  this.b = b;
  this.a = a;}
  
  public int hashCode() {
    return hashCodeFor(a) + 2*hashCodeFor(b);
  }
  
  public boolean equals(Object o) {
    if (o == this) return true;
    if (!(o instanceof Pair)) return false;
    Pair t = (Pair) o;
    return eq(a, t.a) && eq(b, t.b);
  }
  
  public String toString() {
    return "<" + a + ", " + b + ">";
  }
  
  public int compareTo(Pair<A, B> p) {
    if (p == null) return 1;
    int i = ((Comparable<A>) a).compareTo(p.a);
    if (i != 0) return i;
    return ((Comparable<B>) b).compareTo(p.b);
  }
}

static interface IVar<A> {
  void set(A a);
  A get();
  boolean has();
  void clear();
}

static boolean swic(String a, String b) {
  return startsWithIgnoreCase(a, b);
}


  static boolean swic(String a, String b, Matches m) {
    if (!swic(a, b)) return false;
    m.m = new String[] {substring(a, l(b))};
    return true;
  }

static <A extends JComponent> A setToolTipText(final A c, final Object toolTip) {
  if (c == null) return null;
  { swing(new Runnable() {  public void run() { try { 
    String s = nullIfEmpty(str(toolTip));
    if (neq(s, c.getToolTipText()))
      c.setToolTipText(s);
  
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "String s = nullIfEmpty(str(toolTip));\r\n    if (neq(s, c.getToolTipText()))\r\n ..."; }}); }
  return c;
}

static <A extends JComponent> A setToolTipText(Object toolTip, A c) {
  return setToolTipText(c, toolTip);
}
static String nullIfEmpty(String s) {
  return isEmpty(s) ? null : s;
}

static Map nullIfEmpty(Map map) {
  return isEmpty(map) ? null : map;
}

static <A> List<A> nullIfEmpty(List<A> l) {
  return isEmpty(l) ? null : l;
}
static void addMenuItem(JPopupMenu menu, String text, Object action) {
  menu.add(jmenuItem(text, action));
}

static void addMenuItem(JPopupMenu menu, JMenuItem menuItem) {
  menu.add(menuItem);
}

static void addMenuItem(JMenu menu, String text, Object action) {
  menu.add(jmenuItem(text, action));
}

static void addMenuItem(Menu menu, String text, Object action) {
  menu.add(menuItem(text, action));
}

static void addMenuItem(JMenu menu, JMenuItem menuItem) {
  menu.add(menuItem);
}



static String copyTextToClipboard(String text) {
  StringSelection selection = new StringSelection(text);
  Toolkit.getDefaultToolkit().getSystemClipboard().setContents(selection, selection);
  return text;
}
static int hashCodeFor(Object a) {
  return a == null ? 0 : a.hashCode();
}


static boolean startsWithIgnoreCase(String a, String b) {
  return regionMatchesIC(a, 0, b, 0, b.length());
}
static MenuItem menuItem(String text, final Object r) {
  MenuItem mi = new MenuItem(text);
  mi.addActionListener(actionListener(r));
  return mi;
}


static boolean regionMatchesIC(String a, int offsetA, String b, int offsetB, int len) {
  
  
    return a != null && a.regionMatches(true, offsetA, b, offsetB, len);
  
}


static class RuleFailInfo {
  static String _fieldOrder = "rule succeeded matches remainingConditions false x h h h h h h rule";
  IfThen rule;
  int succeeded;
  Map<String, String> matches;
  Exp remainingConditions;
  RuleFailInfo() {}
  RuleFailInfo(IfThen rule, int succeeded, Map<String, String> matches, Exp remainingConditions) {
  this.remainingConditions = remainingConditions;
  this.matches = matches;
  this.succeeded = succeeded;
  this.rule = rule;}public boolean equals(Object o) {
    if (!(o instanceof RuleFailInfo)) return false;
    RuleFailInfo x = (RuleFailInfo) ( o);
    return eq(rule, x.rule) && eq(succeeded, x.succeeded) && eq(matches, x.matches) && eq(remainingConditions, x.remainingConditions);
  }
  
  public int hashCode() {
    int h = 1031308328;
    h = h*2+_hashCode(rule);
    h = h*2+_hashCode(succeeded);
    h = h*2+_hashCode(matches);
    h = h*2+_hashCode(remainingConditions);
    return h;
  }
  
  public String toString() {
    return succeeded + " succeeded, " + "remaining: " + remainingConditions + ", matches: " + sfu(matches) + ", rule: " + rule;
  }
}static class PlanInMotion<A> extends DynamicObject {
  List<A> doneSteps = synchroList();
  List<A> plannedSteps = synchroLinkedList();
  volatile boolean paused, cancelled;
  
  PlanInMotion() {}
  PlanInMotion(Iterable<A> l) { addAll(plannedSteps, l); }
  
  void add(A a) { plannedSteps.add(a); }
  
  IterableIterator<A> master() {
    return iteratorFromFunction(new F0<A>() { A get() { try { return  master_next() ; } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "master_next()"; }});
  }
  
  A master_next() {
    return paused || cancelled ? null
      : addAndReturnIfNotNull(doneSteps, syncPopFirst(plannedSteps));
  }
}static abstract class VF2<A, B> {
  abstract void get(A a, B b);
}static class NLLogicChecker_v3 extends NLLogicChecker_v2 {
}static class Matches {
  String[] m;
  
  Matches() {}
  Matches(String... m) {
  this.m = m;}
  
  String get(int i) { return i < m.length ? m[i] : null; }
  String unq(int i) { return unquote(get(i)); }
  String fsi(int i) { return formatSnippetID(unq(i)); }
  String fsi() { return fsi(0); }
  String tlc(int i) { return unq(i).toLowerCase(); }
  boolean bool(int i) { return "true".equals(unq(i)); }
  String rest() { return m[m.length-1]; } // for matchStart
  int psi(int i) { return Integer.parseInt(unq(i)); }
}
static class NLLogicChecker_v2 {
  String input;
  List<String> facts = new ArrayList();
  List<IfThen> rules = new ArrayList();
  StringMatcher matcher = ai_standardMatcher();
  List<String> posted = new ArrayList();
  ThreadLocal<IfThen> checkingRule = new ThreadLocal();
  VF2<Object, Matching> onChecking; // first arg: IfThen or Exp
  VF3<Object, Matching, Object> onChecked; // first arg: IfThen or Exp, last arg: Bool or Throwable
  static boolean staticVerbose;

  List<Map> recentHistory;
  List<String> entities;
  F2<Exp, Matching, Object> evaluator = new F2<Exp, Matching, Object>() { Object get(Exp e, Matching m) { try { return  nlLogic_evalExp(NLLogicChecker_v2.this, e, m) ; } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "nlLogic_evalExp(NLLogicChecker_v2.this, e, m)"; }};

  static class Matching {
    Map<String, String> matches = new LinkedHashMap(); // var -> match
    List<Exp> output = new ArrayList();
    //L<RuleFailInfo> ruleFails;
    boolean verbose = staticVerbose;
    
    public String toString() { return sfu(matches) + "/" + sfu(output); }
  }
  
  boolean checkRule(final IfThen rule, final Matching m) {
    if (rule == null) return false;
    return logCheck(rule, m, new F0<Boolean>() { Boolean get() { try { return  checkRule_impl(rule, m) ; } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "checkRule_impl(rule, m)"; }});
  }
  
  boolean checkRule_impl(IfThen rule, Matching m) {
     AutoCloseable __9 = tempSetThreadLocal(checkingRule, rule); try {
    if (!checkExpression(rule.in, m)) return false;
    m.output.add(apply(rule.out, m));
    return true;
  } finally { _close(__9); }}
  
  // returns (success, remaining conditions)
  Pair<Boolean, Exp> checkFirstCondition(IfThen rule, Matching m) {
     AutoCloseable __10 = tempSetThreadLocal(checkingRule, rule); try {
    return checkFirstCondition(rule.in, m);
  } finally { _close(__10); }}
  
  Pair<Boolean, Exp> checkFirstCondition(Exp exp, Matching m) {
    Pair<Exp, Exp> p = nlLogic_extractFirstCondition(exp);
    if (p == null) return pair(true, null);
    if (!checkExpression(p.a, m)) return pair(false, null);
    return pair(true, p.b);
  }
  
  Exp apply(Exp e, Matching m) {
    return apply(e, m.matches);
  }
  
  Exp apply(Exp e, Map<String, String> m) {
    if (e == null) return null;
    if (e instanceof And)
      return new And(apply(((And) e).a, m), apply(((And) e).b, m));
    if (e instanceof ExpNot)
      return new ExpNot(apply(((ExpNot) e).a, m));
    if (e instanceof Func)
      return new Func(((Func) e).name, apply(((Func) e).arg, m));
      
    if (e instanceof Sentence)
      return new Sentence(javaTok(matcher.apply(((Sentence) e).text(), m)));
    if (e instanceof Sentence2)
      return new Sentence2(matcher.apply(((Sentence2) e).text(), m));
      
    return e;
  }
  
  <A> A logCheck(Object o, Matching m, F0<A> f) {
    callF(onChecking, o, m);
    try {
      A result = callF(f);
      callF(onChecked, o, m, result);
      return result;
    } catch (Throwable e) {
      callF(onChecked, o, m, e);
      throw rethrow(e);
    }
  }
  
  final boolean checkExpression(final Exp e, final Matching m) {
    return logCheck(e, m, new F0<Boolean>() { Boolean get() { try { return  checkExpression_impl(e, m) ; } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "checkExpression_impl(e, m)"; }});
  }
  
  boolean checkExpression_impl_1(Exp e, Matching m) {
    if (e instanceof And)
      return checkExpression(((And) e).a, m) && checkExpression(((And) e).b, m);
    return checkExpression_single(e, m);
  }
  
  boolean checkExpression_single(Exp e, Matching m) {
    if (e instanceof ExpNot) {
       AutoCloseable __11 = tempBackupMatches(m); try {
      return !checkExpression(((ExpNot) e).a, m);
    } finally { _close(__11); }}
    if (e instanceof Func) {
      if (eq(((Func) e).name, "input") && !cic(((Func) e).options, "flexMatch")) {
        String pat = nlLogic_text(((Func) e).arg);
        return nlLogic_matchVBarPattern(this, pat, input, m);
      } else if (eq(((Func) e).name, "fact")) {
        String pat = nlLogic_text(((Func) e).arg);
        if (contains(((Func) e).options, "random"))
          return nlLogic_matchRandomFact(this, pat, m);
        else
          return nlLogic_matchAnyFact(this, pat, m);
      } else if (eq(((Func) e).name, "allFacts")) {
        String pat = nlLogic_text(((Func) e).arg);
        List<LinkedHashMap<String, String>> l = nlLogic_matchAllFacts(this, pat, m);
        for (int i = 0; i < l(l); i++) {
          Map<String, String> vars = reverseGet(l, i); // facts are normally reversed, so re-reverse to get them in order
          int n = i+1;
          for (String var : keys( vars)) { String value =vars.get(var); 
            if (!strictPutIC(m.matches, var + n, value)) return false;
          }
        }
        return true;
      }
    }
    return false;
  }
  
  AutoCloseable tempBackupMatches(final Matching m) {
    final LinkedHashMap<String, String> oldMatches = (LinkedHashMap) cloneMap(m.matches);
    return new AutoCloseable() { public void close() throws Exception { m.matches = oldMatches; }};
  }
  
  Object evalExp(Exp e, Matching m) {
    return callF(evaluator, e, m);
  }
  
  boolean checkExpression_impl(Exp e, Matching m) {
    Matches mm = new Matches();
    if (e instanceof Func) {
      // FUNCTION CONDITIONS
      
      if (eqOneOf(((Func) e).name, "verbPhraseFromThirdPerson", "verbFromThirdPerson"))
        return nlLogic_stringFunction("ai_verbPhraseFromThirdPerson", ((Func) e), m.matches);
      else if (eq(((Func) e).name, "singular"))
        return nlLogic_stringFunction("singular", ((Func) e), m.matches);
      else if (eq(((Func) e).name, "eval"))
        return eq("true", str(evalExp(((Func) e).arg, m)));
      else if (eq(((Func) e).name, "entity")) {
        if (entities == null) {
          long time = sysNow();
          final String switched = switcheroo(input);
          print("Switched >> " + switched);
          entities = evalWithTimeoutOrNull(5000, new F0<List<String>>() { List<String> get() { try { return 
            mapMethod("text", ai_extractEntities_v1(switched))
          ; } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "mapMethod('text, ai_extractEntities_v1(switched))"; }});
          print("Entities (" + elapsedMS(time) + " ms): " + joinWithComma(entities));
          if (entities == null) entities = new ArrayList();
        }
        for (String entity : entities)
          if (matcher.match(nlLogic_text(((Func) e).arg), entity, m.matches)) return true;
      } else if (startsWith(((Func) e).name, "line", mm) && isInteger(mm.rest()) && checkingRule.get() != null) {
        int n = parseInt(mm.rest())-nlLogic_numberOfLinesReferenced(checkingRule.get().in);
        String line = n == 0 ? input : getString(get(recentHistory, l(recentHistory)+n), "text");
        //print("Recent: " + recentHistory);
        String pat = nlLogic_text(((Func) e).arg);
        //print("n=" + n + ", Matching " + e + " with " + line);
        return matcher.match(pat, line, m.matches);
      } else if (eq(((Func) e).name, "unknownIf")) {
        String statement = nlLogic_text(apply(((Func) e).arg, m));
        print("Checking statement: " + statement);
        return !cic(facts, statement) && !cic(facts, "Untrue: " + statement);
      } else if (eq(((Func) e).name, "inputContainsTokens"))
        return jcontains(input, nlLogic_text(((Func) e).arg));
      else if (eq(((Func) e).name, "inputStartsWith"))
        return startsWith(input, nlLogic_text(((Func) e).arg));
      else if (eq(((Func) e).name, "anyInput"))
        return nempty(input);
      else if (eq(((Func) e).name, "preciseInput"))
        return eq(nlLogic_text(apply(((Func) e).arg, m)), input); // Incomplete var support
      else if (eq(((Func) e).name, "followingUpOn")) {
        String text = nlLogic_text(((Func) e).arg);
        Map prevPrev = nextToLast(recentHistory);
        String msgID = getString(prevPrev, "globalID");
        String input = getString(prevPrev, "text");
        
        String ruleID = null, dollarInput = null;
        
        // e.g. "lhnshnhcklhabvmu with input=$input"
        if (match("* with input=*", text, mm) && !isDollarVar(mm.get(1))) {
          ruleID = mm.unq(0); dollarInput = mm.get(1);
        } else
          ruleID = text;
          
        //print("followingUpOn: msgID=" + msgID + ", input=" + input);
        //printStruct(+prevPrev);
        String pat = format("Rule * fired on message * ", ruleID, msgID);
        //print("pat=" + pat);
        for (String ruleFired : mL("Telegram Rule Fires"))
          if (swic(ruleFired, pat)) {
            if (dollarInput != null && !strictPutIC(m.matches, dollarInput, input)) return false;
            List<String> tok = javaTokWithBrackets2(ruleFired);
            if (find3("with vars *", tok, mm)) {
              Map<String, String> vars = safeUnstructMap(mm.get(0));
              if (!addMapToMapWithoutOverwritingIC(m.matches, vars)) return false;
            }
            return true;
          }
        return false;
      }
    }
    
    if (e instanceof Eq) {
      // EQUATION CONDITIONS
      
      Exp r = ((Eq) e).right;
      String var = nlLogic_text(((Eq) e).left);
      
      if (endsWith(var, "!")) { // $x != bla
         AutoCloseable __12 = tempBackupMatches(m); try {
        return !matcher.match(dropSuffixTrim("!", var), r.text(), m.matches);
      } finally { _close(__12); }}
      
      if (r instanceof Func) try {
        if (eq(((Func) r).name, "eval")) {
          Object result = evalExp(((Func) r).arg, m);
          if (result != null) { // Not allowing null results anymore
            List<String> vars = tok_splitAtComma(var);

            if (l(vars) == 2 && shortClassNameIs(result, "Pair"))
              return ai_matchMulti(vars, ll(strGet(result, "a"), strGet(result, "b")), m.matches);

            if (l(vars) == 3 && shortClassNameIs(result, "T3"))
              return ai_matchMulti(vars, ll(strGet(result, "a"), strGet(result, "b"), strGet(result, "c")), m.matches);
              
            if (result instanceof Collection)
              if (l(((Collection) result)) == l(vars))
                return ai_matchMulti(vars, allToString(((Collection) result)), m.matches);
              
            String sResult = str(result);
            StringMatcher _matcher = matcher;
            if (isDollarVar(var)) _matcher = new NLStringMatcher_singleDollarVar();
            else if (contains(var, ',')) { // TODO: use Pair instead - there is only like one rule using this
              if (allDollarVars(vars)
                && ai_matchMulti(vars, tok_splitAtComma(sResult), m.matches)) return true;
            }
              
            return _matcher.match(var, sResult, m.matches);
          }
        }
      } catch (Throwable __e) { _handleException(__e); }
    }
    
    return checkExpression_impl_1(e, m);
  }

  void iterate(final Exp e, final Matching m, final Runnable onMatch) {
    if (e instanceof And) {
      iterate(((And) e).a, m, new Runnable() {  public void run() { try {  iterate(((And) e).b, m, onMatch) ;
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "iterate(((And) e).b, m, onMatch)"; }});
      return;
    }

    if (e instanceof Func) {
      if (eq(((Func) e).name, "fact")) {
        String pat = nlLogic_text(((Func) e).arg);
        LinkedHashMap<String, String> oldMatches = (LinkedHashMap) cloneMap(m.matches);
        for (String fact : facts) {
          if (m.verbose)
            print("iterate matching: " + pat + " with " + fact);
          if (matcher.match(pat, fact, m.matches)) {
            if (m.verbose)
              print("iterate match: " + pat + " / " + fact + " - " + m);
            try { callF(onMatch); } finally { m.matches = oldMatches; }
          }
        }
        return;
      } else if (eq(((Func) e).name, "input") && cic(((Func) e).options, "flexMatch")) {
        String pat = nlLogic_text(((Func) e).arg);
        LinkedHashMap<String, String> oldMatches = (LinkedHashMap) cloneMap(m.matches);

        for (String pat2 : tok_splitAtVerticalBar(pat))
          nlLogic_flexMatch_iterate(pat2, input, m, onMatch, oldMatches);
        return;
      }
    }
    
     AutoCloseable __13 = tempBackupMatches(m); try {
    if (checkExpression(e, m)) {
      if (m.verbose)
        print("iterate fallback match: " + e); 
      callF(onMatch);
    }
  } finally { _close(__13); }}
  
  boolean checkHelper(Exp e, Matching m) {
    if (e instanceof Func) {
      String arg = ((Func) e).arg.text();
      if (eq(((Func) e).name, "newID") && isDollarVar(arg))
        return matcher.match(arg, aGlobalID(), m.matches);
    }
    return false;
  }
}
static abstract class F1<A, B> {
  abstract B get(A a);
}static class RuleWithParams {
  static String _fieldOrder = "rule matches false x h h h h globalID";
  IfThen rule;
  Map<String, String> matches;
  RuleWithParams() {}
  RuleWithParams(IfThen rule, Map<String, String> matches) {
  this.matches = matches;
  this.rule = rule;}
  public String toString() { return "RuleWithParams(" + rule + ", " + matches + ")"; }public boolean equals(Object o) {
    if (!(o instanceof RuleWithParams)) return false;
    RuleWithParams x = (RuleWithParams) ( o);
    return eq(rule, x.rule) && eq(matches, x.matches);
  }
  
  public int hashCode() {
    int h = 1254104616;
    h = h*2+_hashCode(rule);
    h = h*2+_hashCode(matches);
    return h;
  }
  
  String ruleID() { return rule.globalID; }
}

static class IfThen {
  static String _fieldOrder = "in out false x h h h h globalID options originalText opt opt";
  Exp in;
  Exp out;
  IfThen() {}
  IfThen(Exp in, Exp out) {
  this.out = out;
  this.in = in;}public boolean equals(Object o) {
    if (!(o instanceof IfThen)) return false;
    IfThen x = (IfThen) ( o);
    return eq(in, x.in) && eq(out, x.out);
  }
  
  public int hashCode() {
    int h = -2108234502;
    h = h*2+_hashCode(in);
    h = h*2+_hashCode(out);
    return h;
  }
  
  String globalID;
  Set<String> options;
  String originalText;
  
  String text() {
    Collection<String> opt = options;
    if (nempty(globalID)) opt = concatLists(ll("id: " + globalID), opt);
    return (nempty(opt) ? "[" + joinWithComma(opt) + "] " : "")
      + (in == null ? "" : in.text() + " => ") + nlLogic_text(out);
  }
  
  public String toString() { return text(); }
}

abstract static class Exp {
  abstract String text();
  public String toString() { return text(); }
}

static  class Func extends Exp {
  static String _fieldOrder = "name arg false x h h h h options";
  String name;
  Exp arg;
  Func() {}
  Func(String name, Exp arg) {
  this.arg = arg;
  this.name = name;}public boolean equals(Object o) {
    if (!(o instanceof Func)) return false;
    Func x = (Func) ( o);
    return eq(name, x.name) && eq(arg, x.arg);
  }
  
  public int hashCode() {
    int h = 2201316;
    h = h*2+_hashCode(name);
    h = h*2+_hashCode(arg);
    return h;
  }
  
  List<String> options;
  
  Func(String name, List<String> options, Exp arg) {
  this.arg = arg;
  this.options = options;
  this.name = name;}
  
  String text() {
    return name + "(" + arg.text() + ")";
  }
  
  String argText() { return nlLogic_text(arg); }
}

static  class And extends Exp {
  static String _fieldOrder = "a b false x h h h h";
  Exp a;
  Exp b;
  And() {}
  And(Exp a, Exp b) {
  this.b = b;
  this.a = a;}public boolean equals(Object o) {
    if (!(o instanceof And)) return false;
    And x = (And) ( o);
    return eq(a, x.a) && eq(b, x.b);
  }
  
  public int hashCode() {
    int h = 65975;
    h = h*2+_hashCode(a);
    h = h*2+_hashCode(b);
    return h;
  }
  
  String text() {
    return a.text() + " && " + b.text();
  }
}

static  class ExpNot extends Exp {
  static String _fieldOrder = "a false x h h h";
  Exp a;
  ExpNot() {}
  ExpNot(Exp a) {
  this.a = a;}public boolean equals(Object o) {
    if (!(o instanceof ExpNot)) return false;
    ExpNot x = (ExpNot) ( o);
    return eq(a, x.a);
  }
  
  public int hashCode() {
    int h = 2089649046;
    h = h*2+_hashCode(a);
    return h;
  }
  
  String text() {
    return "!" + a.text();
  }
}

abstract static class Literal extends Exp {}
static  class Sentence extends Literal {
  static String _fieldOrder = "tok false x h h h";
  List<String> tok;
  Sentence() {}
  Sentence(List<String> tok) {
  this.tok = tok;}public boolean equals(Object o) {
    if (!(o instanceof Sentence)) return false;
    Sentence x = (Sentence) ( o);
    return eq(tok, x.tok);
  }
  
  public int hashCode() {
    int h = 1327381123;
    h = h*2+_hashCode(tok);
    return h;
  }
   String text() { return join(tok); } }
static  class Sentence2 extends Literal {
  static String _fieldOrder = "text false x h h h text";
  String text;
  Sentence2() {}
  Sentence2(String text) {
  this.text = text;}public boolean equals(Object o) {
    if (!(o instanceof Sentence2)) return false;
    Sentence2 x = (Sentence2) ( o);
    return eq(text, x.text);
  }
  
  public int hashCode() {
    int h = -1800858097;
    h = h*2+_hashCode(text);
    return h;
  }
   String text() { return text; } }

static  class Eq extends Exp {
  static String _fieldOrder = "left right false x h h h h";
  Exp left;
  Exp right;
  Eq() {}
  Eq(Exp left, Exp right) {
  this.right = right;
  this.left = left;}public boolean equals(Object o) {
    if (!(o instanceof Eq)) return false;
    Eq x = (Eq) ( o);
    return eq(left, x.left) && eq(right, x.right);
  }
  
  public int hashCode() {
    int h = 2252;
    h = h*2+_hashCode(left);
    h = h*2+_hashCode(right);
    return h;
  }
  
  String text() {
    return left.text() + " = " + right.text();
  }
}

static abstract class VF3<A, B, C> {
  abstract void get(A a, B b, C c);
}static ThreadLocal<Boolean> DynamicObject_loading = new ThreadLocal();

static class DynamicObject {
  String className; // just the name, without the "main$"
  LinkedHashMap<String,Object> fieldValues = new LinkedHashMap();
  
  DynamicObject() {}
  // className = just the name, without the "main$"
  DynamicObject(String className) {
  this.className = className;}
}static class NLStringMatcher_singleDollarVar extends StringMatcher {
  // change matches only if returning true
  boolean match(String pat, String input, Map<String, String> matches) {
    if (!isDollarVar(pat)) return match3(pat, input);
    return addMapToMapWithoutOverwritingIC(matches, litmap(pat, input));
  }
  
  String apply(String input, Map<String, String> matches) {
    return replaceVars(input, matches);
  }
}abstract static class StringMatcher {
  // change matches only if returning true
  abstract boolean match(String pat, String input, Map<String, String> matches);
  
  // seems to be the same in all subclasses
  String apply(String input, Map<String, String> matches) {
    return ai_standardReplaceVars(input, matches);
  }
  
  boolean matchAnyPattern(Collection<String> patterns, String input, Map<String, String> matches) {
    for (String pat : unnull(patterns))
      if (match(pat, input, matches)) return true;
    return false;
  }
  
  boolean matchAnyInput(String pat, Collection<String> inputs, Map<String, String> matches) {
    for (String input : unnull(inputs))
      if (match(pat, input, matches)) return true;
    return false;
  }
}// you still need to implement hasNext() and next()
static abstract class IterableIterator<A> implements Iterator<A>, Iterable<A> {
  public Iterator<A> iterator() {
    return this;
  }
  
  public void remove() {
    unsupportedOperation();
  }
}static abstract class F2<A, B, C> {
  abstract C get(A a, B b);
}

static String ai_verbPhraseFromThirdPerson(String s) {
  List<String> tok = javaTok(s);
  for (int i = 1; i < l(tok); i += 2)
    if (endsWithIC(tok.get(i), "s")) {
      tok.set(i, dropLast(tok.get(i)));
      break;
    }
  return join(tok);
}
static Map<String, String> singular_specials = litmap(
  "children", "child", "images", "image", "chess", "chess");
  
static Set<String> singular_specials2 = litset("time", "machine", "line");

static String singular(String s) {
  if (s == null) return null;
  { String _a_845 = singular_specials.get(s); if (!empty(_a_845)) return _a_845; }
  //try answer hippoSingulars().get(lower(s));
  if (singular_specials2.contains(dropSuffix("s", afterLastSpace(s))))
    return dropSuffix("s", s);
  if (s.endsWith("ness")) return s;
  if (s.endsWith("ges")) return dropSuffix("s", s);
  if (endsWith(s, "bases")) return dropLast(s);
  s = dropSuffix("es", s);
  s = dropSuffix("s", s);
  return s;
}
static long sysNow() {
  return System.nanoTime()/1000000;
}
static long done2_always(long startTime, String desc) {
  long time = sysNow()-startTime;
  print(desc + " [" + time + " ms]");
  return time;
}

static long done2_always(String desc, long startTime) {
  return done2_always(startTime, desc);
}

static long done2_always(long startTime) {
  return done2_always(startTime, "");
}
static long lastTiming() {
  return getLastTiming();
}
static Map<String, Object> getMCThreadLocals(Collection<String> names) {
  HashMap<String,Object> map = new HashMap();
  for (String name : names) {
    Object var = getOptMC(name);
    if (var instanceof ThreadLocal)
      map.put(name, ((ThreadLocal) var).get());
  }
  return map;
}
static List<String> ai_staticVars() {
  return ll("telegram_msg_tl", "telegram_recentHistory_tl", "ai_context_tl");
}
static int done_minPrint = 10;

static long done(long startTime, String desc) {
  long time = now()-startTime;
  if (time >= done_minPrint)
    print(desc + " [" + time + " ms]");
  return time;
}

static long done(String desc, long startTime) {
  return done(startTime, desc);
}

static long done(long startTime) {
  return done(startTime, "");
}
static String unixLineBreaks(String s) {
  return s.replace("\r", "");
}
static List<IfThen> mL_parsedNLRulesWithoutIDs(String listName) {
  return map_pcall("nlLogic_parseRule",mL_rulesWithoutIDs(listName));
}

static <A> IterableIterator<A> shuffledIterator(final List<A> l) {
  return new IterableIterator<A>() {
    int n = l(l);
    int i = 0;
    Map<Integer,A> shuffled = new HashMap();
    
    public boolean hasNext() { return i < n; }
    
    public A next() {
      int j = random(i, n);
      A a = get(i), b = get(j);
      shuffled.put(j, a);
      shuffled.remove(i);
      ++i;
      return b;
    }
    
    A get(int i) {
      return shuffled.containsKey(i) ? shuffled.get(i) : l.get(i);
    }
  };
}
// returns (first condition, rest) or null on null; rest may be null
static Pair<Exp, Exp> nlLogic_extractFirstCondition(Exp e) {
  if (e == null) return null;
  if (e instanceof And)
    return pair(((And) e).a, ((And) e).b);
  return pair(e, null);
}
static boolean nlLogic_isFunc(Exp e, String name) {
  return e instanceof Func && eq(((Func) e).name, name);
}
static String nlLogic_text(Exp e) {
  return e == null ? null : e.text();
}

static String nlLogic_text(IfThen r) {
  return r == null ? null : r.text();
}
static String aGlobalID() {
  return randomID(globalIDLength());
}
static String sfu(Object o) { return structureForUser(o); }
static boolean startsWith_trim(String a, String b, Matches m) {
  if (!startsWith(a, b)) return false;
  m.m = new String[] {trim(substring(a, l(b)))};
  return true;
}
static <A extends Runnable> void runPlanWithCheck(PlanInMotion<A> plan, F0<Boolean> shouldKeepRunning) {
  while (isTrue(callF(shouldKeepRunning))) {
    A step = plan.master_next();
    if (step == null) return;
    callF(step);
  }
}

static <A, B extends A> void addAll(Collection<A> c, Iterable<B> b) {
  if (c != null && b != null) for (A a : b) c.add(a);
}

static <A, B extends A> boolean addAll(Collection<A> c, Collection<B> b) {
  return c != null && b != null && c.addAll(b);
}

static <A, B extends A> boolean addAll(Collection<A> c, B... b) {
  return c != null && c.addAll(Arrays.asList(b));
}
static List<String> leadingSquareBracketOptions(String s) {
  List<String> tok = javaTokPlusBrackets2(s);
  if (!isSquareBracketed(get(tok, 1))) return emptyList();
  return tok_splitAtComma(javaTokPlusBrackets2(deSquareBracket(get(tok, 1))));
}
static String dropLeadingSquareBracketStuff(String s) {
  return trimSubstring(s, indexOf(s, ']')+1);
}
static void ai_tg_collectRuleFeedback(Map<String, Integer> ruleScores, Collection<String> out_facts) {
   // We're lazy!
  
  List<Pair<TelegramMsg, List<String>>> l = serverTelegramLog_plusFiredRules();
  for (int i = 0; i < l(l); i++) {
    List<String> rules = l.get(i).b;
    if (empty(rules)) continue;
    TelegramMsg msg = l.get(i).a;
    int j = i+1;
    while (j < l(l) && l.get(j).a.sent) ++j; // Find next user msg
    List<TelegramMsg> nextMsgs = pairsA(subList(l, i+1, j+1));
    TelegramMsg userMsg = null;
    if (nempty(nextMsgs)) {
      for (TelegramMsg m : nextMsgs)
        if (userMsg == null && !m.sent) userMsg = m;
    }
    if (userMsg == null) continue;
    int score = 0, ruleNr = 0;
    String s = userMsg.text;
    
    Matches m = new Matches();
    if (match("* to number *", s, m)) {
      s = m.unq(0); ruleNr = parseInt(m.unq(1));
    }
    
    if (matchAny(mL("Yes Feedback Words"), s))
      ++score;
    if (matchAny(mL("No Feedback Words"), s))
      --score;

    // Rules seem to be in the right order w/respect to output lines
    for (String rule : ruleNr == 0 ? rules : ll(rules.get(ruleNr-1))) {
      if (score != 0 && ruleScores != null)
        ruleScores.put(rule, toInt(ruleScores.get(rule))+score);
      if (out_facts != null)
        out_facts.add("rule " + format_arg(rule) + " had "
          + (score > 0 ? "positive" : score < 0 ? "negative" : "possible") + " feedback (" + quote(userMsg.text) + ") when fired on msg " + msg.globalID + " at " + msg.date);
    }
  }
  
  if (out_facts != null)
    addToMechList("Rule Feedback", out_facts);
}
static ThreadLocal<Map> telegram_msg_tl = new ThreadLocal();

static Map telegram_msg() {
  return telegram_msg_tl.get();
}

static void telegram_msg(Map msg) {
  telegram_msg_tl.set(msg);
}
static boolean telegram_amIAuthorized() {
  return contains(mL_tlftj("Authorized Telegram User IDs"), str(get(telegram_msg(), "userID")));
}
static void nlLogic_collectCheckingLog(NLLogicChecker_v2 c, final List<String> log) {
  /*c.onChecking = voidfunc(O o, NLLogicChecker_v2.Matching m) {
    pcall { log.add("Checking: " + o + " with " + sfu(m.matches)); }
  };*/
  
  c.onChecked = new VF3<Object, NLLogicChecker_v2.Matching, Object>() { public void get(Object o, NLLogicChecker_v2.Matching m, Object result) { try { 
    try { 
      String status = isTrue(result) ? "OK" : isFalse(result) ? "FAIL" : str(result);
      log.add(status + ": " + o + " with " + sfu(m.matches));
    } catch (Throwable __e) { _handleException(__e); }
   } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "pcall { \r\n      S status = isTrue(result) ? \"OK\" : isFalse(result) ? \"FAIL\" :..."; }};
}
static <A> List<A> reversed(Collection<A> l) {
  return reversedList(l);
}

static <A> List<A> reversed(A[] l) {
  return reversedList(asList(l));
}

static String reversed(String s) {
  return reversedString(s);
}
static <A> List<A> concatLists_conservative(List<A> a, List<A> b) {
  if (empty(a)) return b;
  if (empty(b)) return a;
  return concatLists(a, b);
}
static <A> List<A> filter(Iterable<A> c, Object pred) {
  if (pred instanceof F1) return filter(c, (F1<A, Boolean>) pred);

  
  List x = new ArrayList();
  if (c != null) for (Object o : c)
    if (isTrue(callF(pred, o)))
      x.add(o);
  return x;
}

static List filter(Object pred, Iterable c) {
  return filter(c, pred);
}

static <A, B extends A> List filter(Iterable<B> c, F1<A, Boolean> pred) {
  List x = new ArrayList();
  if (c != null) for (B o : c)
    if (pred.get(o).booleanValue())
      x.add(o);
  return x;
}

static <A, B extends A> List filter(F1<A, Boolean> pred, Iterable<B> c) {
  return filter(c, pred);
}

static boolean ai_ruleAccessesInput(String rule) {
  List<String> tok = javaTokC(rule);
  String t;
  for (int i = 0; i < l(tok); i++)
    if (eqGet(tok, i+1, "(") && isIdentifier(t = tok.get(i)))
      if (eq(t, "i") || swic(t, "input") || ewic(t, "input")) return true;
  return false;
}
static String joinWithComma(Collection<String> c) {
  return join(", ", c);
}

static String joinWithComma(String... c) {
  return join(", ", c);
}


static String joinWithComma(Pair p) {
  return p == null ? "" : joinWithComma(str(p.a), str(p.b));
}

static List map(Iterable l, Object f) {
  return map(f, l);
}

static List map(Object f, Iterable l) {
  List x = emptyList(l);
  if (l != null) for (Object o : l)
    x.add(callF(f, o));
  return x;
}


  static List map(F1 f, Iterable l) {
    List x = emptyList(l);
    if (l != null) for (Object o : l)
      x.add(callF(f, o));
    return x;
  }


static List map(Object f, Object[] l) { return map(f, asList(l)); }
static List map(Object[] l, Object f) { return map(f, l); }

static List map(Object f, Map map) {
  return map(map, f);
}

// map: func(key, value) -> list element
static List map(Map map, Object f) {
  List x = new ArrayList();
  if (map != null) for (Object _e : map.entrySet()) {
    Map.Entry e = (Map.Entry) _e;
    x.add(callF(f, e.getKey(), e.getValue()));
  }
  return x;
}
static void nlLogic_battleItOut(List<RuleWithParams> battleSpace, List<String> facts) {
  Matches mm = new Matches();
  MultiMap<String, String> trumps = new MultiMap();
  
  // get conflict clusters
  Map < String , Collection < String > > conflictClusters = new HashMap();
  Set<String> _ruleIDs = new HashSet();
  for (RuleWithParams r : battleSpace) {
    addIfNotNull(_ruleIDs, r.rule.globalID);
    for (String option : unnull(r.rule.options))
      if (match_vbar("conflicts with *|trumps *", option, mm)) {
        String r1 = r.rule.globalID, r2 = mm.unq(0);
        treeSetClusters_add(conflictClusters, r1, r2);
        if (match("trumps *", option))
          trumps.put(r1, r2);
      }
  }
      
  for (String s : facts) {
    boolean trump = match("rule * trumps rule *", s, mm);
    if (trump || match("rule * conflicts with rule *", s, mm)) {
      String r1 = mm.unq(0), r2 = mm.unq(1);
      if (_ruleIDs.contains(r1) && _ruleIDs.contains(r2)) {
        treeSetClusters_add(conflictClusters, r1, r2);
        if (trump)
          trumps.put(r1, r2);
      }
    }
  }
        
  if (nempty(conflictClusters))
    print("Have conflict clusters: " + sfu(conflictClusters));

  // Detect winners
  final HashSet<String> toRemove = new HashSet();
  for (Collection<String> ruleIDs : uniquifyList_identity(values(conflictClusters))) {
    ruleIDs = new LinkedHashSet(ruleIDs);
    for (String id : cloneList(ruleIDs)) {
      List<String> losers = trumps.get(id);
      removeAll(ruleIDs, losers);
      toRemove.addAll(losers);
    }
    String winner = random(ruleIDs);
    List<String> removing = listWithout(ruleIDs, winner);
    print("Winner: " + winner + ", removing: " + removing);
    toRemove.addAll(removing);
  }
  
  // Remove losers
  if (nempty(toRemove)) {
    for (ListIterator<RuleWithParams> i = listIterator(battleSpace); i.hasNext(); )
      if (toRemove.contains(i.next().rule.globalID))
        i.remove();
    print("Rules narrowed down to: " + collect(collect(battleSpace, "rule"), "globalID"));
  }
}
static List<RuleWithParams> nlLogic_highestScore(List<RuleWithParams> battleSpace, final Map<String, Integer> ruleScores) {
  int highest = Integer.MIN_VALUE;
  for (RuleWithParams r : battleSpace)
    highest = max(highest, toInt(mapGet(ruleScores, r.rule.globalID)));
  final int _highest = highest;
  return filter(battleSpace , new F1<RuleWithParams, Object>() { Object get(RuleWithParams r) { try { return  toInt(mapGet(ruleScores, r.rule.globalID)) == _highest ; } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "toInt(mapGet(ruleScores, r.rule.globalID)) == _highest"; }});
}
static String getString(Map map, Object key) {
  return map == null ? null : (String) map.get(key);
}

static String getString(List l, int idx) {
  return (String) get(l, idx);
}

static String getString(Object o, Object key) {
  if (o instanceof Map) return getString((Map) o, key);
  if (key instanceof String)
    return (String) getOpt(o, (String) key);
  throw fail("Not a string key: " + getClassName(key));
}
  static String format(String pat, Object... args) {
    return format3(pat, args);
  }



static String localDateWithMilliseconds(long time) {
  SimpleDateFormat format = simpleDateFormat_local("yyyy/MM/dd HH:mm:ss''SSSS");
  return format.format(time);
}

static String localDateWithMilliseconds() {
  return localDateWithMilliseconds(now());
}
static String dropPrefix(String prefix, String s) {
  return s == null ? null : s.startsWith(prefix) ? s.substring(l(prefix)) : s;
}
static String struct(Object o) {
  return structure(o);
}

static String struct(Object o, structure_Data data) {
  return structure(o, data);
}
static MechMode mechMode_value = new MechMode();

static MechMode mechMode() {
  return mechMode_value;
}
static String addToMechList(String name, String text) {
  return botAppendToMechList_uniqCI(name, text);
}

static String addToMechList(String name, Collection<String> lines) {
  return addToMechList(name, lines(lines));
}
static ThreadLocal<Boolean> applyNLLogicFacts_v4_verbose = new ThreadLocal();

static void applyNLLogicFacts_v4(String input, VF2<IfThen, NLLogicChecker_v2.Matching> onRuleFired) {
  applyNLLogicFacts_v4(input, onRuleFired, mL_facts());
}

static void applyNLLogicFacts_v4(String input, VF2<IfThen, NLLogicChecker_v2.Matching> onRuleFired, List<String> facts) {
  applyNLLogicFacts_v4(input, onRuleFired, facts, ai_mL_parsedLogicExamples2());
}

static void applyNLLogicFacts_v4(String input, VF2<IfThen, NLLogicChecker_v2.Matching> onRuleFired, List<String> facts, List<IfThen> rules) {
  NLLogicChecker_v2 c = new NLLogicChecker_v2();
  c.matcher = new NLStringMatcher_dollarVars();
  addAll(c.facts, facts);
  c.input = input;
  
  applyNLLogicFacts_v4(c, onRuleFired, rules);
}

static void applyNLLogicFacts_v4(NLLogicChecker_v2 c, VF2<IfThen, NLLogicChecker_v2.Matching> onRuleFired, List<IfThen> rules) {
  c.rules = rules;
  for (IfThen rule : rules) try {
    NLLogicChecker_v2.Matching m = new NLLogicChecker_v2.Matching();
    m.verbose = isTrue(applyNLLogicFacts_v4_verbose.get());
    if (m.verbose)
      print("Checking rule: " + rule);
    if (c.checkRule(rule, m)) {
      print("Matched " + quote(c.input) + " with rule " + quote(rule));
      printStruct(m.matches);
      callF(onRuleFired, rule, m);
    }
  } catch (Throwable __e) { _handleException(__e); }
}
static List<Exp> nlLogic_unrollAnd(Exp e) {
  List<Exp> l = new ArrayList();
  nlLogic_unrollAnd_collect(e, l);
  return l;
}

static void nlLogic_unrollAnd_collect(Exp e, List<Exp> out) {
  if (e instanceof And) {
    nlLogic_unrollAnd_collect(((And) e).a, out);
    nlLogic_unrollAnd_collect(((And) e).b, out);
  } else
    addIfNotNull(out, e);
}
static boolean containsNL(Collection<String> l, String pat) {
  if (l == null) return false;
  if (l.contains(pat)) return true;
  for (String s : l) if (match(pat, s)) return true;
  return false;
}
static boolean ai_isQuestion_2(String s) {
  return ai_isYesNoQuestion_1(s) || ai_questionWord_1(firstWord2(s));
}
static boolean ai_storeActualFact(String fact) {
  return !contains(mL("Random facts"), fact) && mech_changed(addToMechList("Derived facts", fact));
}

static boolean telegram_authorizedToStoreRule(String rule) {
  return telegram_amIFullyAuthorized() || /*telegram_amIAuthorized() &&*/ !ai_ruleContainsEval(rule);
}
static String nlLogic_addGlobalID(String rule) {
  if (!startsWith(trim(rule), "[id:")) rule = "[id: " + aGlobalID() + "] " + rule;
  return rule;
}

static boolean mech_changed(String result) {
  return cic(result, "changed");
}
static String appendToMechList_noUniq(String name, String text) {
  return botAppendToMechList(name, text);
}
static boolean match(String pat, String s) {
  return match3(pat, s);
}

static boolean match(String pat, String s, Matches matches) {
  return match3(pat, s, matches);
}

static NLLogicChecker_v2.Matching ai_matchingWrapping(Map<String, String> matches) {
  NLLogicChecker_v2.Matching m = new NLLogicChecker_v2.Matching();
  m.matches = matches;
  return m;
}
static Pair<List<IfThen>, List<String>> ai_activeRulesAndFacts() { 
  return ai_activeRulesAndFacts(null);
}

static Pair<List<IfThen>, List<String>> ai_activeRulesAndFacts(List<String> extraFacts) { 
 long _startTime_961 = sysNow(); 
  List<String> facts = concatLists_conservative(extraFacts, mL_facts());
  
  List<String> unparsedRules = ai_unparsedTalkRules();
  
  for (String listName : mL("Rule & Fact Lists"))
    for (String x : splitAtEmptyLines(mL_raw(listName)))
      if (anyJavaTokens(x))
        if (nlLogic_parseRule(x) != null)
          unparsedRules.add(x);
        else
          facts.add(x);

  unparsedRules = sortedByCalculatedField(unparsedRules, new F1<String, Object>() { Object get(String s) { try { return  jcontains(s, "entity(") ; } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "jcontains(s, \"entity(\")"; }});
  List<IfThen> rules = map_pcall("nlLogic_parseRule",unparsedRules);
  
  for (String listName : mL("Default active scripts"))
    addAll(rules, nlLogic_loadMechScript(listName));
  
  nlLogic_expandMacros(rules);
  done2_always("ai_activeRulesAndFacts", _startTime_961); 
 return pair(rules, facts);
}

static <A> A nlLookup(Map<String, A> map, String s) {
  return lookupByNLMatch(map, s);
}

static <A> A nlLookup(Map<String, A> map, String s, Matches m) {
  return lookupByNLMatch(map, s, m);
}

static <A> List<A> nlLookup(MultiMap<String, A> map, String s) {
  return lookupByNLMatch(map, s);
}
static Map<String, String> mechMap(String listName) {
  return mechMapCI(listName);
}
static String expandDollarRefsToMatches(String s, Matches m) {
  return expandDollarRefsToMatches(s, m, false);
}

static String expandDollarRefsToMatches(String s, Matches m, boolean alwaysQuote) {
  List<String> tok = javaTok(s);
  for (int i = 1; i < l(tok); i += 2) {
    String t = tok.get(i);
    if (!t.startsWith("$")) continue;
    t = dropFirst(t);
    int j = parseIntAtEnd(t);
    if (j < 1) continue;
    String x = get(m.m, j-1);
    if (x == null) continue;
    if (alwaysQuote || startsWith(t, "q"))
      x = quote(x);
    tok.set(i, x);
  }
  return join(tok);
}

static Object javaEvalOrInterpret(String code) {
  if (canQuickEval(code)) return quickEval(code);
  Pair<String, List<String>> p = tok_parseFunctionCall(code);
  if (p != null)
    return callAndMake(p.a, toObjectArray(map("javaEvalOrInterpret",p.b)));
  return javaEval(code);
}

static boolean swic_trim(String a, String b, Matches m) {
  if (!swic(a, b)) return false;
  m.m = new String[] {trim(substring(a, l(b)))};
  return true;
}
static ThreadLocal<String> ai_setLanguage_value = new ThreadLocal();

static void ai_setLanguage(String language) {
  ai_setLanguage_value.set(language);
}

static void _onLoad_ai_setLanguage() {
  _threadInfo_addMakerAndRetriever(
    new VF1<Map>() { public void get(Map map) { try {  mapPut(map, "ai_setLanguage_value", ai_setLanguage_value.get()) ; } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "mapPut(map, 'ai_setLanguage_value, ai_setLanguage_value!)"; }},
    new VF1<Map>() { public void get(Map map) { try {  ai_setLanguage((String) map.get("ai_setLanguage_value")) ; } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "ai_setLanguage((S) map.get('ai_setLanguage_value))"; }});
}
static String random_gac36k() {
  return random(keys(gac36k()));
}
static String ai_renderCulledParseTree(PTElement e) {
  ai_cullParseTree(e);
  return ai_renderParseTree(e);
}
static PTElement ai_parseToTreeWithGuessing(String input) {
    List<String> words = ai_uncategorizedWordsInSentence(input);
  //print("Have " + n2(words, "unknown word"));
  
  AI_BottomUpParser1 parser = new AI_BottomUpParser1();
  parser.wordsToCategories = new DerivedMap(ai_wordToCategories());
  Map<String, Map<String, Double>> guessMap = new HashMap();
   AutoCloseable __978 = tempSetThreadLocal(ai_getWordToCategoryWeight_map, guessMap); try {
  for (String word : words) {
    Map<String, Double> map = ai_guessCategoriesForUnknownWord_weighted(word);
    guessMap.put(word, map);
    parser.wordsToCategories.put(word, keys(map));
  }
  return ai_parseBottomUpToTree(parser, input);
} finally { _close(__978); }}
static String lines(Collection lines) { return fromLines(lines); }
static List<String> lines(String s) { return toLines(s); }
static LinkedHashSet<String> ai_parseTree_simplifiedTexts(PTElement e) {
  LinkedHashSet<String> out = new LinkedHashSet();
  ai_parseTree_simplifiedTexts_collect(e, out);
  return out;
}

static void ai_parseTree_simplifiedTexts_collect(PTElement e, LinkedHashSet<String> out) {
  if (e == null) return;
  PTElement ee = ai_parseTree_descendToComposite(e);
  if (ee == null) { out.add(e.text()); return; }
  e = ee;
  if (e instanceof ChoosePart) {
    ai_Reconstructed r = ((ChoosePart) e).reconstruction;
    WeightedProduction prod = r.production;
    //print("Hint: " + prod.simplificationHint + " in prod: " + sfu(prod));
    //if (eq(prod.simplificationHint, "L")) out.add(r.a.text());
    //else if (eq(prod.simplificationHint, "R")) out.add(r.b.text());
    
    PTElement e1 = first(e.children), e2 = second(e.children);
    List<String> a = asList(ai_parseTree_simplifiedTexts(e1));
    List<String> b = asList(ai_parseTree_simplifiedTexts(e2));
    
    String hint = prod.simplificationHint;
    if (eqOneOf(hint, "L", "B")) insertFirst(b, "");
    /*else XXX*/ if (eqOneOf(hint, "R", "B")) insertFirst(a, "");
    
    //print("L " + className(e1) + " " + className(ai_parseTree_descendToComposite(e1)) + " " + r.a.text() + " => " + a);
    //print("R " + className(e2) + " " + className(ai_parseTree_descendToComposite(e2)) + " " + r.b.text() + " => " + b);
    
    /*for (S aa : a)
      for (S bb : b)
        out.add(trim(aa + " " + bb));*/
    for (Pair<String, String> p : combineZigZag(a, b))
      out.add(trim(p.a + " " + p.b));
    
    // Add full text too
    out.add(((ChoosePart) e).word.text());
  }
}
static boolean swicOneOf_trim(String s, Matches m, String... l) {
  for (String x : l) if (swic_trim(s, x, m)) return true; return false;
}
static AutoCloseable tempSet(Object o, final String field, Object value) {
  return tempSetField(o, field, value);
}
static String dropActuallyLeadingSquareBracketStuff(String s) {
  return startsWith(s, "[") ? dropLeadingSquareBracketStuff(s) : s;
}
static String evalWithTimeout_text(double timeoutSeconds, final Object f) {
  return evalWithTimeout_text(toMS(timeoutSeconds), f);
}

// f : func -> O
static String evalWithTimeout_text(final int timeoutMS, final Object f) {
  return pcallOrExceptionText(new F0<Object>() { Object get() { try { return 
    evalWithTimeoutOrException(timeoutMS, new F0<Object>() { Object get() { try { return  strOrNull(callF(f)) ; } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "strOrNull(callF(f))"; }})
  ; } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "evalWithTimeoutOrException(timeoutMS, func -> O { strOrNull(callF(f)) })"; }});
}
static Set<String> ai_factsFromNewFacts(List<String> newFacts) {
  Pair<List<IfThen>, List<String>> p = ai_rulesAndFacts();
  return ai_deriveFacts(p.a, concatLists(newFacts, p.b));
}

static Set<String> ai_factsFromNewFacts(String... newFacts) {
  return ai_factsFromNewFacts(asList(newFacts));
}
static List<String> tlft(String s) { return toLinesFullTrim(s); }
static List<String> tlft(File f) { return toLinesFullTrim(f); }

static NLLogicChecker_v2 nlLogicCheckerWithRulesAndFacts(Pair<List<IfThen>, List<String>> p) {
  NLLogicChecker_v2 c = new NLLogicChecker_v2();
  c.rules = p.a;
  c.facts = p.b;
  return c;
}
static <A> List<A> listPlus_inFront(Collection<A> l, A... more) {
  return concatLists(asList(more), l);
}
static boolean telegram_amIFullyAuthorized() {
  return contains(mL_tlftj("Fully Authorized Telegram User IDs"), str(telegram_msg().get("userID")));
}
static void dm_refreshTranspiler() {
  call(dm_moduleOfType("#1017065/Transpiler"), "refresh");
}
static String pcallOrExceptionText(Object f, Object... args) {
  try {
    return str(callF(f, args));
  } catch (Throwable e) {
    _handleException(e);
    return exceptionToStringShort(e);
  }
}
static Object javaEval(String code) {
  return evalJava(code);
}
static List<String> trimAll(Collection<String> l) {
  List<String> l2 = new ArrayList();
  if (l != null) for (String s : l)
    l2.add(trim(s));
  return l2;
}
static List<String> ai_unparsedTalkRules() {
  return splitAtEmptyLines(mL_raw("NL Logic Examples"));
}
static boolean contains(Collection c, Object o) {
  return c != null && c.contains(o);
}

static boolean contains(Object[] x, Object o) {
  if (x != null)
    for (Object a : x)
      if (eq(a, o))
        return true;
  return false;
}

static boolean contains(String s, char c) {
  return s != null && s.indexOf(c) >= 0;
}

static boolean contains(String s, String b) {
  return s != null && s.indexOf(b) >= 0;
}

static boolean contains(BitSet bs, int i) {
  return bs != null && bs.get(i);
}
static String leadingSquareBracketOptions_id(String s) {
  List<String> options = leadingSquareBracketOptions(s);
  Matches m = new Matches();
  for (String option : options)
    if (startsWith_trim(option, "id: ", m))
      return m.rest();
  return null;
}
static List<String> mL(String name) {
  return mechList_opt(name);
}
static boolean ai_applyFactToFactRules_debug;

static Collection<String> ai_applyFactToFactRules() {
  List<IfThen> rules = ai_parseRules(mL_raw("Fact-to-fact rules"));
  if (ai_applyFactToFactRules_debug)
    print("ai_applyFactToFactRules: " + n2(rules, "rule"));
  Set<String> outFacts = ai_deriveFacts(rules, mL("Random facts"));
  appendToMechList("Derived facts", outFacts);
  return outFacts;
}
static List<RuleFailInfo> nlLogic_sortedRuleFails(List<RuleFailInfo> l) {
  return sortedByFieldDesc("successes", l);
}
static List<RuleFailInfo> nlLogic_ruleFails(NLLogicChecker_v2 c, List<IfThen> rules) {
  List<RuleFailInfo> out = new ArrayList();
  for (IfThen rule : unnull(rules)) {
    NLLogicChecker_v2.Matching m = new NLLogicChecker_v2.Matching();
    Pair<Integer, Exp> p = nlLogic_checkFirstConditions2(c, rule, m);
    if (p.b != null)
      out.add(new RuleFailInfo(rule, p.a, m.matches, p.b));
  }
  return out;
}


static boolean nlLogic_unresolvedFactsToQuestions_debug;

static Collection<String> nlLogic_unresolvedFactsToQuestions(RuleFailInfo ruleFail) {
  if (ruleFail == null) return emptyList();
  Exp e = nlLogic_applyMatches(nlLogic_firstCondition(ruleFail.remainingConditions), ruleFail.matches);
  // Now have: fact(bla) in e
  
  if (e instanceof Func)
    if (eq(((Func) e).name, "fact")) {
      String fact = "Unresolved fact: " + ((Func) e).arg.text();
      if (nlLogic_unresolvedFactsToQuestions_debug) print(fact);
      Pair<List<IfThen>, List<String>> p = ai_activeRulesAndFacts(ll(fact));
      return ai_deriveOutput(p.a, p.b);
    }
    
  return emptyList();
}

static Collection<String> nlLogic_unresolvedFactsToQuestions(Collection<RuleFailInfo> ruleFails) {
  LinkedHashSet<String> out = new LinkedHashSet();
  for (RuleFailInfo fail : ruleFails)
    addAll(out, nlLogic_unresolvedFactsToQuestions(fail));
  return out;
}


static int _hashCode(Object a) {
  return a == null ? 0 : a.hashCode();
}
static <A> List<A> synchroLinkedList() {
  return Collections.synchronizedList(new LinkedList<A>());
}

// f: func -> A (stream ends when f returns null)
static <A> IterableIterator<A> iteratorFromFunction(final Object f) {
  class IFF extends IterableIterator<A> {
    A a;
    boolean done;
    
    public boolean hasNext() {
      getNext();
      return !done;
    }
    
    public A next() {
      getNext();
      if (done) throw fail();
      A _a = a;
      a = null;
      return _a;
    }
    
    void getNext() {
      if (done || a != null) return;
      a = (A) callF(f);
      done = a == null;
    }
  };
  return new IFF();
}

// optimized version for F0 argument
static <A> IterableIterator<A> iteratorFromFunction(final F0<A> f) {
  return iteratorFromFunction_f0(f);
}
static <A> A addAndReturnIfNotNull(Collection<A> l, A a) {
  if (a != null && l != null) l.add(a);
  return a;
}


static <A> A syncPopFirst(List<A> l) {
  if (empty(l)) return null;
  synchronized(l) {
    A a = first(l);
    l.remove(0);
    return a;
  }
}
static String fsi(String id) {
  return formatSnippetID(id);
}
static StringMatcher ai_standardMatcher() {
  return new CombinedStringMatcher(
    new NLStringMatcher_dollarVars_underscores_dollarDollar(),
    new NLStringMatcher_dollarVars_plusBrackets());
}
static Object nlLogic_evalExp(final NLLogicChecker_v2 c, Exp e, NLLogicChecker_v2.Matching m) {
  // prepare context
  final Map<String, String> vars = m.matches;
   AutoCloseable __1042 = tempSetThreadLocal(ai_context_tl(), new Object() {
    List<String> getList(String var) {
      int i = 1;
      String s;
      List<String> l = new ArrayList();
      while ((s = vars.get(var + i++)) != null)
        l.add(s);
      return l;
    }
    
    IfThen getRule(String id) {
      return findByField(c.rules, "globalID" , id);
    }
  }); try {

  String code = nlLogic_text(e);
  print("Eval'ing: " + code);
   AutoCloseable __1044 = tempAdd(hotwire_copyOver_after, new VF1<Class>() { public void get(Class c) { try { 
    copyFields(mc(), c, ai_staticVars())
  ; } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "copyFields(mc(), c, ai_staticVars())"; }}); try {
  Object result = null;
  try {
    result = evalWithDollarVars_assertAllVarsSet(code, m.matches);
  } catch (Throwable __e) { _handleException(__e); }
  print("Result: " + shorten(str(result), 100));
  return result;
} finally { _close(__1044); }} finally { _close(__1042); }}
static <A> AutoCloseable tempSetThreadLocal(final ThreadLocal<A> tl, A a) {
  final A prev = setThreadLocal(tl, a);
  return new AutoCloseable() { public void close() {
    tl.set(prev);
  }};
}
// TODO: extended multi-line strings

static int javaTok_n, javaTok_elements;
static boolean javaTok_opt;

static List<String> javaTok(String s) {
  ++javaTok_n;
  ArrayList<String> tok = new ArrayList();
  int l = s == null ? 0 : s.length();
  
  int i = 0, n = 0;
  while (i < l) {
    int j = i;
    char c, d;
    
    // scan for whitespace
    while (j < l) {
      c = s.charAt(j);
      d = j+1 >= l ? '\0' : s.charAt(j+1);
      if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
        ++j;
      else if (c == '/' && d == '*') {
        do ++j; while (j < l && !s.substring(j, Math.min(j+2, l)).equals("*/"));
        j = Math.min(j+2, l);
      } else if (c == '/' && d == '/') {
        do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0);
      } else
        break;
    }
    
    tok.add(javaTok_substringN(s, i, j));
    ++n;
    i = j;
    if (i >= l) break;
    c = s.charAt(i);
    d = i+1 >= l ? '\0' : s.charAt(i+1);

    // scan for non-whitespace
    
    // Special JavaX syntax: 'identifier
    if (c == '\'' && Character.isJavaIdentifierStart(d) && i+2 < l && "'\\".indexOf(s.charAt(i+2)) < 0) {
      j += 2;
      while (j < l && Character.isJavaIdentifierPart(s.charAt(j)))
        ++j;
    } else if (c == '\'' || c == '"') {
      char opener = c;
      ++j;
      while (j < l) {
        int c2 = s.charAt(j);
        if (c2 == opener || c2 == '\n' && opener == '\'') { // allow multi-line strings, but not for '
          ++j;
          break;
        } else if (c2 == '\\' && j+1 < l)
          j += 2;
        else
          ++j;
      }
    } else if (Character.isJavaIdentifierStart(c))
      do ++j; while (j < l && (Character.isJavaIdentifierPart(s.charAt(j)) || "'".indexOf(s.charAt(j)) >= 0)); // for stuff like "don't"
    else if (Character.isDigit(c)) {
      do ++j; while (j < l && Character.isDigit(s.charAt(j)));
      if (j < l && s.charAt(j) == 'L') ++j; // Long constants like 1L
    } else if (c == '[' && d == '[') {
      do ++j; while (j+1 < l && !s.substring(j, j+2).equals("]]"));
      j = Math.min(j+2, l);
    } else if (c == '[' && d == '=' && i+2 < l && s.charAt(i+2) == '[') {
      do ++j; while (j+2 < l && !s.substring(j, j+3).equals("]=]"));
      j = Math.min(j+3, l);
    } else
      ++j;
      
    tok.add(javaTok_substringC(s, i, j));
    ++n;
    i = j;
  }
  
  if ((tok.size() % 2) == 0) tok.add("");
  javaTok_elements += tok.size();
  return tok;
}

static List<String> javaTok(List<String> tok) {
  return javaTokWithExisting(join(tok), tok);
}
static boolean cic(Collection<String> l, String s) {
  return containsIgnoreCase(l, s);
}



static boolean cic(String[] l, String s) {
  return containsIgnoreCase(l, s);
}

static boolean cic(String s, char c) {
  return containsIgnoreCase(s, c);
}

static boolean cic(String a, String b) {
  return containsIgnoreCase(a, b);
}

static boolean nlLogic_matchVBarPattern(NLLogicChecker_v2 c, String pat, String s, NLLogicChecker_v2.Matching m) {
  return nlLogic_matchVBarPattern(c, pat, s, m.matches);
}

static boolean nlLogic_matchVBarPattern(NLLogicChecker_v2 c, String pat, String s, Map<String, String> matches) {
  for (String pat2 : tok_splitAtVerticalBar(pat))
    if (c.matcher.match(pat2, s, matches)) return true;
  return false;
}
static boolean nlLogic_matchRandomFact(NLLogicChecker_v2 c, String pat, NLLogicChecker_v2.Matching m) {
  List<LinkedHashMap<String, String>> choices = nlLogic_matchAllFacts(c, pat, m);
  if (empty(choices)) return false;
  m.matches = random(choices);
  return true;
}
static boolean nlLogic_matchAnyFact(NLLogicChecker_v2 c, String pat, NLLogicChecker_v2.Matching m) {
  List<String> patterns = tok_splitAtVerticalBar(pat);
  for (String fact : c.facts) {
    boolean result =  nlLogic_matchMultiplePatterns(c, patterns, fact, m);
    if (m.verbose) print("matched " + quote(pat) + " with " + quote(fact) + " & " + sfu(m.matches) + " => " + result);
    if (result) return true;
  }
  return false;
}
static List<LinkedHashMap<String, String>> nlLogic_matchAllFacts(String pat, List<String> facts) {
  NLLogicChecker_v2 c = new NLLogicChecker_v2();
  c.facts = facts;
  return nlLogic_matchAllFacts(c, pat, new NLLogicChecker_v2.Matching());
}

static List<LinkedHashMap<String, String>> nlLogic_matchAllFacts(NLLogicChecker_v2 c, String pat, NLLogicChecker_v2.Matching m) {
  List<String> patterns = tok_splitAtVerticalBar(pat);
  LinkedHashMap<String, String> m2 = null;
  List < LinkedHashMap < String , String > > choices = new ArrayList();
  for (String fact : c.facts) {
    if (m2 == null) m2 = cloneLinkedHashMap(m.matches);
    boolean result = nlLogic_matchMultiplePatterns(c, patterns, fact, m2);
    if (m.verbose) print("matched " + quote(pat) + " with " + quote(fact) + " & " + sfu(m.matches) + " => " + result);
    if (result) {
      choices.add(m2);
      m2 = null;
    }
  }
  return choices;
}
static <A> A reverseGet(List<A> l, int idx) {
  if (l == null || idx < 0) return null;
  int n = l(l);
  return idx < n ? l.get(n-1-idx) : null;
}
static <A> boolean strictPutIC(Map<A, String> map, A key, String value) {
  if (!(map != null && key != null && value != null)) return true;
  String old = map.get(key);
  if (old == null) { map.put(key, value); return true; }
  return eqic(old, value);
}


static <A, B> Map<A, B> cloneMap(Map<A, B> map) {
  if (map == null) return new HashMap();
  // assume mutex is equal to collection
  synchronized(map) {
    return map instanceof TreeMap ? new TreeMap((TreeMap) map) // copies comparator
      : map instanceof LinkedHashMap ? new LinkedHashMap(map)
      : new HashMap(map);
  }
}
static boolean nlLogic_stringFunction(String f, Func e, Map<String, String> matches) {
  List<String> l = tok_splitAtComma(javaTokWithBrackets(nlLogic_text(e.arg)));
  if (l(l) != 2) return false;
  l = mapReplaceVars2(l, matches);
  if (isDollarVar(second(l)) && !isDollarVar(first(l)))
    return strictPutIC(matches, second(l), str(callAndMake(f, first(l))));
  return eqic(second(l), first(l));
}
static List<String> switcheroo_themap = ll(
  "mine", "yours",
  "my", "your",
  "you are", "i am",
  "are you", "am i",
  "you're", "i'm",
  "you've", "i've",
  "me", "you",
  "i", "you");

static String switcheroo(String s) {
  List<String> tok = nlTok5(s);
  for (int i = 1; i < l(tok); i += 2) {
    for (int j = 0; j < l(switcheroo_themap); j += 2) {
      String x = switcheroo_themap.get(j), y = switcheroo_themap.get(j+1);
      List<String> tokx = nlTok5(x), toky = nlTok5(y);
      switcheroo_matchReplace(tok, i, tokx, toky);
      switcheroo_matchReplace(tok, i, toky, tokx);
    }
  }
  return join(map("switcheroo_dropAsterisk", tok));
}

static String switcheroo_dropAsterisk(String t) {
  return startsWith(t, "*") && l(t) > 1 ? substring(t, 1) : t;
}

static void switcheroo_matchReplace(List<String> tok, int i, List<String> tokx, List<String> toky) {
  //printFormat("Matching * * *", i, get(tok, i), tokx);
  if (switcheroo_tokRegionMatch(tok, i, tokx)) {
    //printFormat("Replacing * *", tokx, toky);
    for (int j = 0; j*2+1 < l(tokx); j++)
      tok.set(i+j*2, "*" + toky.get(1+j*2));
  }
}

static boolean switcheroo_tokRegionMatch(List<String> tok, int i, List<String> tokx) {
  for (int j = 0; j*2+1 < l(tokx); j++)
    if (!eqic(get(tok, i+j*2), get(tokx, 1+j*2)))
      return false;
  return true;
}

static Object evalWithTimeoutOrNull(final Object f, int timeoutMS) {
  return evalWithTimeoutOrNull(timeoutMS, f);
}

static Object evalWithTimeoutOrNull(int timeoutMS, final Object f) {
  return eitherAOpt(evalWithTimeout(timeoutMS, f));
}

static Object evalWithTimeoutOrNull(double timeoutSeconds, final Object f) {
  return eitherAOpt(evalWithTimeout(timeoutSeconds, f));
}

static <A> A evalWithTimeoutOrNull(int timeoutMS, F0<A> f) {
  return (A) eitherAOpt(evalWithTimeout(timeoutMS, f));
}

static <A> A evalWithTimeoutOrNull(double timeoutSeconds, F0<A> f) {
  return (A) eitherAOpt(evalWithTimeout(timeoutSeconds, f));
}
static List mapMethod(Iterable c, final String methodName) {
  return map(c, new F1<Object, Object>() { Object get(Object o) { try { return  callOpt(o, methodName) ; } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "callOpt(o, methodName)"; }});
}

static List mapMethod(String methodName, Iterable c) {
  return mapMethod(c, methodName);
}

static List mapMethod(String methodName, Enumeration c) {
  return mapMethod(methodName, enumerationToIterator(c));
}
static List<PTElement> ai_extractEntities_v1(String input) {
  if (startsWith(input, "!")) return emptyList(); // skip commands
  input = ai_lookupCurlyCheatSheet(input);
  return ai_extractEntities_v1(ai_parseToCulledTree(input));
}

static List<PTElement> ai_extractEntities_v1(PTElement tree) {
  HashSet<PTElement> toSkip = new HashSet();
  List<PTElement> out = new ArrayList();
  for (PTElement e : ai_scanParseTreeForCategory(tree, "<noun>")) {
    if (toSkip.contains(e)) continue;
    //print("Entity found: " + e.text());
    out.add(e);
    PTElement ee = ai_parseTree_simplifyNode(e);
    if (ee != null && ee != e)
      toSkip.add(ee);
  }
  return out;
}
static long elapsedMS(long time) {
  return sysNow()-time;
}
static boolean isInteger(String s) {
  if (s == null) return false;
  int n = l(s);
  if (n == 0) return false;
  int i = 0;
  if (s.charAt(0) == '-')
    if (++i >= n) return false;
  while (i < n) {
    char c = s.charAt(i);
    if (c < '0' || c > '9') return false;
    ++i;
  }
  return true;
}
static int nlLogic_numberOfLinesReferenced(Exp e) {
  if (e instanceof And)
    return max(nlLogic_numberOfLinesReferenced(((And) e).a), nlLogic_numberOfLinesReferenced(((And) e).b));
  if (e instanceof Func) {
    Matches m = new Matches();
    if (startsWith(((Func) e).name, "line", m))
      return parseIntOpt(m.rest());
  }
  return 0;
}
static boolean jcontains(String s, String pat) {
  return jfind(s, pat) >= 0;
}
static <A> A nextToLast(List<A> l) {
  return get(l, l(l)-2);
}
static boolean isDollarVar(String s) {
  return startsWith(s, '$') && isJavaIdentifierAfter(s, 1);
}


static List<String> javaTokWithBrackets2(String s) {
  return javaTokPlusBrackets2(s);
}
static boolean find3(String pat, String s) {
  return find3(pat, s, null);
}

static boolean find3(String pat, String s, Matches matches) {
  return find3(pat, parse3(s), matches);
}
  
static boolean find3(String pat, List<String> toks, Matches matches) {
  List<String> tokpat = parse3(pat);
  String[] m = find2(tokpat, toks);
  //print(structure(tokpat) + " on " + structure(toks) + " => " + structure(m));
  if (m == null) return false;
  if (matches != null) matches.m = m;
  return true;
}
static Map safeUnstructMap(String s) {
  return safeUnstructureMap(s);
}
static <A> boolean addMapToMapWithoutOverwritingIC(Map<A, String> map, Map<A, String> adding) {
  if (!canPutAllStrictlyIC(map, adding)) return false;
  { final Map<A, String> __1095 = unnull(adding); for (A key : keys( __1095)) { String value =__1095.get(key); 
    strictPutIC(map, key, value);
  }
  }return true;
}
static String dropSuffixTrim(String suffix, String s) {
  return dropSuffixAndTrim(suffix, s);
}

static boolean dropSuffixTrim(String suffix, String s, Matches m) {
  if (!endsWith(s, suffix)) return false;
  m.m = new String[] { dropLast(l(suffix), s) };
  return true;
}
static List<String> tok_splitAtComma(String s) {
  return tok_splitAtComma(javaTok(s));
}
  
static List<String> tok_splitAtComma(List<String> tok) {
  List<String> out = new ArrayList();
  for (int i = 0; i < l(tok); i++) {
    int j = smartIndexOf(tok, ",", i);
    out.add(joinSubList(tok, i+1, j-1));
    i = j;
  }
  return out;
}
static boolean shortClassNameIs(Object o, String name) {
  return eq(shortClassName(o), name);
}
static boolean ai_matchMulti(List<String> vars, List<String> values, Map<String, String> matches) {
  int n = l(vars);
  if (n != l(values)) return false;
  Map<String, String> newMatches = new HashMap();
  for (int i = 0; i < n; i++)
    if (!strictPutIC(newMatches, vars.get(i), values.get(i))) return false;
  return addMapToMapWithoutOverwritingIC(matches, newMatches);
}
static String strGet(Object o, String field) {
  return str(get(o, field));
}
static boolean allDollarVars(Collection<String> l) {
  if (l != null) for (String s : l)
    if (!isDollarVar(s)) return false;
  return true;
}
static List<String> tok_splitAtVerticalBar(String s) {
  if (!contains(s, '|')) return ll(s);
  return splitAtTokens(javaTokPlusBrackets2(s), tok_splitAtVerticalBar_list);
}

static List<String> tok_splitAtVerticalBar_list = ll("|");
static void nlLogic_flexMatch_iterate(String pat, String input, NLLogicChecker_v2.Matching m, Runnable onMatch, LinkedHashMap<String, String> oldMatches) {
  List<String> tokPat = parse3_cachedPattern(pat), tokInput = parse3_cachedInput(input);
  // fail if crucial words not found (could be optimized more)
  List<Integer> varIndices = new ArrayList();
  for (int i = 1; i < l(tokPat); i += 2) {
    String t = tokPat.get(i);
    if (!isDollarVar(t))
      varIndices.add(i);
    else if (isIdentifier(t) && !cicAtOddPosition(tokInput, t)) return;
  }
  List<List<String>> spreadInputs = ai_spreadCertainIndices(tokPat, tokInput, varIndices);
  for (List<String> tok : spreadInputs) {
    Map<String, String> newMatches = matchDollarVarsIC(pat, tok);
    if (newMatches != null && addMapToMapWithoutOverwritingIC(m.matches, newMatches))
      try { callF(onMatch); } finally { m.matches = oldMatches; }
  }
}
static boolean match3(String pat, String s) {
  return match3(pat, s, null);
}

static boolean match3(String pat, String s, Matches matches) {
  if (pat == null || s == null) return false;
  return match3(pat, parse3_cachedInput(s), matches);
}
  
static boolean match3(String pat, List<String> toks, Matches matches) {
  List<String> tokpat = parse3_cachedPattern(pat);
  return match3(tokpat, toks, matches);
}

static boolean match3(List<String> tokpat, List<String> toks, Matches matches) {
  String[] m = match2(tokpat, toks);
  //print(structure(tokpat) + " on " + structure(toks) + " => " + structure(m));
  if (m == null) return false;
  if (matches != null) matches.m = m; return true;
}
static String replaceVars(String s, Map<String, String> map) {
  return empty(map) ? s : join(replaceVars(javaTok(s), map));
}
  
static List<String> replaceVars(List<String> tok, Map<String, String> map) {
  if (empty(map)) return tok;
  tok = cloneList(tok);
  for (String var : keys(map))
    jreplace(tok, var, map.get(var));
  return tok;
}
static String ai_standardReplaceVars(String s, Map<String, String> matches) {
  return replaceVars_withDollarQ(s, matches);
}
static <A> Iterator<A> iterator(Iterable<A> c) {
  return c == null ? emptyIterator() : c.iterator();
}
static UnsupportedOperationException unsupportedOperation() {
  throw new UnsupportedOperationException();
}


static IfThen nlLogic_parseRule(String s) {
  IfThen rule = new IfThen();
  rule.originalText = s;
  List<String> tok = javaTokPlusBrackets2(s);
  if (isSquareBracketed(get(tok, 1))) {
    List<String> tokOptions = javaTokPlusBrackets2(deSquareBracket(get(tok, 1)));
    Matches m = new Matches();
    for (String option : tok_splitAtComma(tokOptions)) {
      if (startsWith_trim(option, "id: ", m))
        rule.globalID = m.rest();
      else
        rule.options = linkedHashSet_addWithAutoInit(rule.options, option);
    }
    tok = dropFirst(2, tok);
  }
  
  List<String> l = splitAtDoubleArrow(tok);
  if (l(l) > 2 || l(l) == 0) return null;
  rule.out = nlLogic_parseExpression(last(l));
  rule.in = nlLogic_parseExpression(nextToLast(l));
  
  if (rule.globalID == null)
    //rule.globalID = aGlobalID();
    rule.globalID = textMD5ToGlobalID(str(rule));
    
  return rule;
}
static boolean endsWithIC(String a, String b) {
  return endsWithIgnoreCase(a, b);
}
static <A> HashSet<A> litset(A... items) {
  return lithashset(items);
}
static String afterLastSpace(String s) {
  return s == null ? null : substring(s, s.lastIndexOf(' ')+1);
}
static long getLastTiming() {
  ThreadLocal<Long> tl = (ThreadLocal<Long>) ( getOpt(getMainClass(), "saveTiming_last"));
  if (tl == null) return -1;
  Long l = tl.get();
  return l == null ? -1 : l;
}
static Object getOptMC(String field) {
  return getOpt(mc(), field);
}
static List map_pcall(Iterable l, Object f) {
  return map_pcall(f, l);
}

static List map_pcall(Object f, Iterable l) {
  List x = new ArrayList();
  for (Object o : unnull(l)) try { x.add(callF(f, o)); } catch (Throwable __e) { _handleException(__e); }
  return x;
}
static List<String> mL_rulesWithoutIDs(String listName) {
  return bOfPairs(mL_rules_addGlobalIDsIfAuthed(listName));
}
static int randomID_defaultLength = 12;

static String randomID(int length) {
  return makeRandomID(length);
}

static String randomID() {
  return randomID(randomID_defaultLength);
}
static int globalIDLength() {
  return 16;
}
static String structureForUser(Object o) {
  return beautifyStructure(struct_noStringSharing(o));
}
static List<String> javaTokPlusBrackets2(String s) {
  return tok_combineRoundCurlySquareBrackets_keep(javaTok(s));
}
static boolean isSquareBracketed(String s) {
  return s != null && s.startsWith("[") && s.endsWith("]");
}
static String deSquareBracket(String s) {
  if (startsWith(s, "[") && endsWith(s, "]"))
    return substring(s, 1, l(s)-1);
  return s;
}
static String trimSubstring(String s, int x) {
  return trim(substring(s, x));
}

static String trimSubstring(String s, int x, int y) {
  return trim(substring(s, x, y));
}


static String trimSubstring(String s, IntRange r) {
  return trim(substring(s, r));
}

static List<Pair<TelegramMsg, List<String>>> serverTelegramLog_plusFiredRules() {
  List<TelegramMsg> msgs = serverTelegramLog();
  MultiMap<String, String> ruleFires = ai_tg_rulesFiredOnMessages_fullMap();
  List<Pair<TelegramMsg, List<String>>> l = emptyList(msgs);
  for (TelegramMsg msg : msgs)
    addPair(l, msg, nullIfEmpty(ruleFires.get(msg.globalID)));
  return l;
}
static <A, B> List<A> pairsA(Collection<Pair<A, B>> l) {
  return firstOfPairs(l);
}
static boolean matchAny(Collection<String> patterns, String s, Matches m) {
  for (String pattern : patterns)
    if (match(pattern, s, m))
      return true;
  return false;
}

static boolean matchAny(Collection<String> patterns, String s) {
  return matchAny(patterns, s, null);
}

// 2nd version

static boolean matchAny(String pat, List<String> l, Matches m) {
  for (String s : l)
    if (match(pat, s, m))
      return true;
  return false;
}
static String format_arg(Object arg) {
  return format3_formatArg(arg);
}
static List<String> mL_tlftj(String name) {
  return mechList_tlftj(name);
}
static <A> List<A> reversedList(Collection<A> l) {
  List<A> x = cloneList(l);
  Collections.reverse(x);
  return x;
}
static String reversedString(String s) {
  return reverseString(s);
}
static List<String> javaTokC(String s) {
  if (s == null) return null;
  int l = s.length();
  ArrayList<String> tok = new ArrayList();
  
  int i = 0;
  while (i < l) {
    int j = i;
    char c, d;
    
    // scan for whitespace
    while (j < l) {
      c = s.charAt(j);
      d = j+1 >= l ? '\0' : s.charAt(j+1);
      if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
        ++j;
      else if (c == '/' && d == '*') {
        do ++j; while (j < l && !s.substring(j, Math.min(j+2, l)).equals("*/"));
        j = Math.min(j+2, l);
      } else if (c == '/' && d == '/') {
        do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0);
      } else
        break;
    }
    
    i = j;
    if (i >= l) break;
    c = s.charAt(i);
    d = i+1 >= l ? '\0' : s.charAt(i+1);

    // scan for non-whitespace
    if (c == '\'' || c == '"') {
      char opener = c;
      ++j;
      while (j < l) {
        if (s.charAt(j) == opener || s.charAt(j) == '\n') { // end at \n to not propagate unclosed string literal errors
          ++j;
          break;
        } else if (s.charAt(j) == '\\' && j+1 < l)
          j += 2;
        else
          ++j;
      }
    } else if (Character.isJavaIdentifierStart(c))
      do ++j; while (j < l && (Character.isJavaIdentifierPart(s.charAt(j)) || "'".indexOf(s.charAt(j)) >= 0)); // for stuff like "don't"
    else if (Character.isDigit(c)) {
      do ++j; while (j < l && Character.isDigit(s.charAt(j)));
      if (j < l && s.charAt(j) == 'L') ++j; // Long constants like 1L
    } else if (c == '[' && d == '[') {
      do ++j; while (j+1 < l && !s.substring(j, j+2).equals("]]"));
      j = Math.min(j+2, l);
    } else if (c == '[' && d == '=' && i+2 < l && s.charAt(i+2) == '[') {
      do ++j; while (j+2 < l && !s.substring(j, j+3).equals("]=]"));
      j = Math.min(j+3, l);
    } else
      ++j;
      
    tok.add(javaTok_substringC(s, i, j));
    i = j;
  }
  
  return tok;
}
static boolean eqGet(List l, int i, Object o) {
  return eq(get(l, i), o);
}

static <A, B> boolean eqGet(Map<A, B> map, A key, Object o) {
  return eq(mapGet(map, key), o);
}
static boolean isIdentifier(String s) {
  return isJavaIdentifier(s);
}
static boolean ewic(String a, String b) {
  return endsWithIgnoreCase(a, b);
}


static boolean ewic(String a, String b, Matches m) {
  return endsWithIgnoreCase(a, b, m);
}

static <A> void addIfNotNull(Collection<A> l, A a) {
  if (a != null && l != null) l.add(a);
}


static boolean match_vbar(String pat, String s) {
  return match_vbar(pat, s, null);
}

static boolean match_vbar(String pat, String s, Matches matches) {
  for (String pat2 : splitAtTokens(pat, ll("|")))
    if (match3(pat2, s, matches)) return true;
  return false;
}

static <A> Collection<A> treeSetClusters_add(Map<A, Collection<A>> clusters, List<A> l) {
  for (int i = 0; i < l(l)-1; i++)
    treeSetClusters_add(clusters, l.get(i), l.get(i+1));
  return clusters.get(first(l));
}

static <A> void treeSetClusters_add(Map<A, Collection<A>> clusters, A a, A b) {
  if (eq(a, b)) {
    Collection<A> cluster = clusters.get(a);
    if (cluster == null)
      clusters.put(a, littreeset(a));
    return;
  }
  
  Collection<A> clusterA = clusters.get(a);
  Collection<A> clusterB = clusters.get(b);
  if (clusterA == null && clusterB == null) {
    List<A> cluster = ll(a, b);
    clusters.put(a, cluster);
    clusters.put(b, cluster);
  } else if (clusterA == null) {
    clusterB.add(a);
    clusters.put(a, clusterB);
  } else if (clusterB == null) {
    clusterA.add(b);
    clusters.put(b, clusterA);
  } else if (clusterA != clusterB)
    treeSetClusters_merge(clusters, clusterA, clusterB);
}
static <A> List<A> uniquifyList_identity(Collection<A> l) {
  if (l == null) return null;
  if (l(l) < 2) return asList(l);
  Set<A> set = identityHashSet();
  List<A> out = new ArrayList();
  for (A a : l)
    if (set.add(a))
      out.add(a);
  return out;
}
static <A> void removeAll(Collection<A> a, Collection<A> b) {
  if (a != null && b != null) a.removeAll(b);
}

static <A, B> void removeAll(Map<A, B> a, Collection<A> b) {
  if (a != null && b != null)
    for (A x : b)
      a.remove(x);
}
static <A> List<A> listWithout(Collection<A> a, Object... b) {
  return minus(a, b);
}
static <A> ListIterator<A> listIterator(List<A> l) {
  return l == null ? emptyListIterator() : l.listIterator();
}
static <A, B> B mapGet(Map<A, B> map, A a) {
  return map == null || a == null ? null : map.get(a);
}
  static String format3(String pat, Object... args) {
    if (args.length == 0) return pat;
    
    List<String> tok = javaTokPlusPeriod(pat);
    int argidx = 0;
    for (int i = 1; i < tok.size(); i += 2)
      if (tok.get(i).equals("*"))
        tok.set(i, format3_formatArg(argidx < args.length ? args[argidx++] : "null"));
    return join(tok);
  }
  
  static String format3_formatArg(Object arg) {
    if (arg == null) return "null";
    if (arg instanceof String) {
      String s = (String) arg;
      return isIdentifier(s) || isNonNegativeInteger(s) ? s : quote(s);
    }
    if (arg instanceof Integer || arg instanceof Long) return String.valueOf(arg);
    return quote(structure(arg));
  }
  



static SimpleDateFormat simpleDateFormat_local(String format) {
  SimpleDateFormat sdf = new SimpleDateFormat(format);
  sdf.setTimeZone(localTimeZone());
  return sdf;
}
static boolean structure_showTiming, structure_checkTokenCount;

static String structure(Object o) {
  return structure(o, new structure_Data());
}

static String structure(Object o, structure_Data d) {
  StringWriter sw = new StringWriter();
  d.out = new PrintWriter(sw);
  structure_go(o, d);
  String s = str(sw);
  if (structure_checkTokenCount) {
    print("token count=" + d.n);
    assertEquals("token count", l(javaTokC(s)), d.n);
  }
  return s;
}

static void structure_go(Object o, structure_Data d) {
  structure_1(o, d);
  while (nempty(d.stack))
    popLast(d.stack).run();
}

static void structureToPrintWriter(Object o, PrintWriter out) {
  structure_Data d = new structure_Data();
  d.out = out;
  structure_go(o, d);
}

// leave to false, unless unstructure() breaks
static boolean structure_allowShortening = false;

static class structure_Data {
  PrintWriter out;
  int stringSizeLimit;
  int shareStringsLongerThan = 20;
  boolean noStringSharing;

  IdentityHashMap<Object,Integer> seen = new IdentityHashMap();
  //new BitSet refd;
  HashMap<String,Integer> strings = new HashMap();
  HashSet<String> concepts = new HashSet();
  HashMap<Class, List<Field>> fieldsByClass = new HashMap();
  HashMap<Class,Field> persistenceInfo = new HashMap();
  int n; // token count
  List<Runnable> stack = new ArrayList();
  
  // append single token
  structure_Data append(String token) { out.print(token); ++n; return this; }
  structure_Data append(int i) { out.print(i); ++n; return this; }
  
  // append multiple tokens
  structure_Data append(String token, int tokCount) { out.print(token); n += tokCount; return this; }
  
  // extend last token
  structure_Data app(String token) { out.print(token); return this; }
  structure_Data app(int i) { out.print(i); return this; }
}

static void structure_1(final Object o, final structure_Data d) { try {
  if (o == null) { d.append("null"); return; }
  
  Class c = o.getClass();
  boolean concept = false;
  
  List<Field> lFields = d.fieldsByClass.get(c);
  
  if (lFields == null) {
    // these are never back-referenced (for readability)
    
    if (o instanceof Number) {
      PrintWriter out = d.out;
if (o instanceof Integer) { int i = ((Integer) o).intValue(); out.print(i); d.n += i < 0 ? 2 : 1; return; }
      if (o instanceof Long) { long l = ((Long) o).longValue(); out.print(l); out.print("L"); d.n += l < 0 ? 2 : 1; return; }
      if (o instanceof Short) { short s = ((Short) o).shortValue(); d.append("sh ", 2); out.print(s); d.n += s < 0 ? 2 : 1; return; }
      if (o instanceof Float) { d.append("fl ", 2); quoteToPrintWriter(str(o), out); return; }
      if (o instanceof Double) { d.append("d(", 3); quoteToPrintWriter(str(o), out); d.append(")"); return; }
      if (o instanceof BigInteger) { out.print("bigint("); out.print(o); out.print(")"); d.n += ((BigInteger) o).signum() < 0 ? 5 : 4; return; }
    }
  
    if (o instanceof Boolean) {
      d.append(((Boolean) o).booleanValue() ? "t" : "f"); return;
    }
      
    if (o instanceof Character) {
      d.append(quoteCharacter((Character) o)); return;
    }
      
    if (o instanceof File) {
      d.append("File ").append(quote(((File) o).getPath())); return;
    }
      
    // referencable objects follow
    
    Integer ref = d.seen.get(o);
    if (o instanceof String && ref == null) ref = d.strings.get((String) o);
    if (ref != null) { /*d.refd.set(ref);*/ d.append("t").app(ref); return; }

    if (!(o instanceof String))
      d.seen.put(o, d.n); // record token number
    else {
      String s = d.stringSizeLimit != 0 ? shorten((String) o, d.stringSizeLimit) : (String) o;
      if (!d.noStringSharing) {
        if (d.shareStringsLongerThan == Integer.MAX_VALUE)
          d.seen.put(o, d.n);
        if (l(s) >= d.shareStringsLongerThan)
          d.strings.put(s, d.n);
      }
      quoteToPrintWriter(s, d.out); d.n++; return;
    }
      
    if (o instanceof HashSet) {
      d.append(o instanceof LinkedHashSet ? "lhs" : "hashset");
      structure_1(new ArrayList((Set) o), d);
      return;
    }
  
    if (o instanceof TreeSet) {
      d.append("treeset");
      structure_1(new ArrayList((Set) o), d);
      return;
    }
    
    String name = c.getName();
    if (o instanceof Collection
      && !startsWith(name, "main$")
      /* && neq(name, "main$Concept$RefL") */) {
      d.append("[");
      final int l = d.n;
      final Iterator it = ((Collection) o).iterator();
      d.stack.add(new Runnable() {  public void run() { try { 
        if (!it.hasNext())
          d.append("]");
        else {
          d.stack.add(this);
          if (d.n != l) d.append(", ");
          structure_1(it.next(), d);
        }
      
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "if (!it.hasNext())\r\n          d.append(\"]\");\r\n        else {\r\n          d.sta..."; }});
      return;
    }
    
    if (o instanceof Map && !startsWith(name, "main$")) {
      if (o instanceof LinkedHashMap) d.append("lhm");
      else if (o instanceof HashMap) d.append("hm");
      else if (name.equals("java.util.Collections$SynchronizedMap")) d.append("sync");
      else if (name.equals("java.util.Collections$SynchronizedSortedMap")) { d.append("sync tm", 2); }
      
      d.append("{");
      final int l = d.n;
      final Iterator it = ((Map) o).entrySet().iterator();
      
      d.stack.add(new Runnable() {
        boolean v;
        Map.Entry e;
        
        public void run() {
          if (v) {
            d.append("=");
            v = false;
            d.stack.add(this);
            structure_1(e.getValue(), d);
          } else {
            if (!it.hasNext())
              d.append("}");
            else {
              e = (Map.Entry) it.next();
              v = true;
              d.stack.add(this);
              if (d.n != l) d.append(", ");
              structure_1(e.getKey(), d);
            }
          }
        }
      });
      return;
    }
    
    if (c.isArray()) {
      if (o instanceof byte[]) {
        d.append("ba ").append(quote(bytesToHex((byte[]) o))); return;
      }
  
      final int n = Array.getLength(o);
  
      if (o instanceof boolean[]) {
        String hex = boolArrayToHex((boolean[]) o);
        int i = l(hex);
        while (i > 0 && hex.charAt(i-1) == '0' && hex.charAt(i-2) == '0') i -= 2;
        d.append("boolarray ").append(n).app(" ").append(quote(substring(hex, 0, i))); return;
      }
      
      String atype = "array", sep = ", ";
  
      if (o instanceof int[]) {
        //ret "intarray " + quote(intArrayToHex((int[]) o));
        atype = "intarray";
        sep = " ";
      }
      
      d.append(atype).append("{");
      d.stack.add(new Runnable() {
        int i;
        public void run() {
          if (i >= n)
            d.append("}");
          else {
            d.stack.add(this);
            if (i > 0) d.append(", ");
            structure_1(Array.get(o, i++), d);
          }
        }
      });
      return;
    }
  
    if (o instanceof Class) {
      d.append("class(", 2).append(quote(((Class) o).getName())).append(")"); return;
    }
      
    if (o instanceof Throwable) {
      d.append("exception(", 2).append(quote(((Throwable) o).getMessage())).append(")"); return;
    }
      
    if (o instanceof BitSet) {
      BitSet bs = (BitSet) o;
      d.append("bitset{", 2);
      int l = d.n;
      for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i+1)) {
        if (d.n != l) d.append(", ");
        d.append(i);
      }
      d.append("}"); return;
    }
      
    // Need more cases? This should cover all library classes...
    if (name.startsWith("java.") || name.startsWith("javax.")) {
      d.append("j ").append(quote(str(o))); return; // Hm. this is not unstructure-able
    }
    
    
      
    /*if (name.equals("main$Lisp")) {
      fail("lisp not supported right now");
    }*/
    
    String dynName = shortDynamicClassName(o);
    if (concept && !d.concepts.contains(dynName)) {
      d.concepts.add(dynName);
      d.append("c ");
    }
    
    // serialize an object with fields.
    // first, collect all fields and values in fv.
    
    TreeSet<Field> fields = new TreeSet<Field>(new Comparator<Field>() {
      public int compare(Field a, Field b) {
        return stdcompare(a.getName(), b.getName());
      }
    });
    
    Class cc = c;
    while (cc != Object.class) {
      for (Field field : getDeclaredFields_cached(cc)) {
        String fieldName = field.getName();
        if (fieldName.equals("_persistenceInfo"))
          d.persistenceInfo.put(c, field);
        if ((field.getModifiers() & (java.lang.reflect.Modifier.STATIC | java.lang.reflect.Modifier.TRANSIENT)) != 0)
          continue;

        fields.add(field);
        
        // put special cases here...?
      }
        
      cc = cc.getSuperclass();
    }
    
    // TODO: S fieldOrder = getOpt(c, "_fieldOrder");
    lFields = asList(fields);
    
    // Render this$1 first because unstructure needs it for constructor call.
    
    for (int i = 0; i < l(lFields); i++) {
      Field f = lFields.get(i);
      if (f.getName().equals("this$1")) {
        lFields.remove(i);
        lFields.add(0, f);
        break;
      }
    }
  
    
    d.fieldsByClass.put(c, lFields);
  } // << if (lFields == null)
  else { // ref handling for lFields != null
    Integer ref = d.seen.get(o);
    if (ref != null) { /*d.refd.set(ref);*/ d.append("t").app(ref); return; }
    d.seen.put(o, d.n); // record token number
  }

  Field persistenceInfoField = (Field) ( d.persistenceInfo.get(c));
  Map<String, Object> persistenceInfo = persistenceInfoField == null ? null : (Map) persistenceInfoField.get(o);
  
  LinkedHashMap<String,Object> fv = new LinkedHashMap();
  for (Field f : lFields) {
    Object value;
    try {
      value = f.get(o);
    } catch (Exception e) {
      value = "?";
    }
      
    if (value != null && (persistenceInfo == null
      || !Boolean.FALSE.equals(persistenceInfo.get(f.getName()))))
      fv.put(f.getName(), value);
    
  }
  
  String name = c.getName();
  String shortName = dropPrefix("main$", name);
    
  // Now we have fields & values. Process fieldValues if it's a DynamicObject.
  
  // omit field "className" if equal to class's name
  if (concept && eq(fv.get("className"), shortName))
    fv.remove("className");
          
  if (o instanceof DynamicObject) {
    fv.putAll((Map) fv.get("fieldValues"));
    fv.remove("fieldValues");
    shortName = shortDynamicClassName(o);
    fv.remove("className");
  }
  
  String singleField = fv.size() == 1 ? first(fv.keySet()) : null;
  
  d.append(shortName);
  
  
  final int l = d.n;
  final Iterator it = fv.entrySet().iterator();
  
  d.stack.add(new Runnable() {  public void run() { try { 
    if (!it.hasNext()) {
      if (d.n != l)
        d.append(")");
    } else {
      Map.Entry e = (Map.Entry) it.next();
      d.append(d.n == l ? "(" : ", ");
      d.append((String) e.getKey()).append("=");
      d.stack.add(this);
      structure_1(e.getValue(), d);
    }
  
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "if (!it.hasNext()) {\r\n      if (d.n != l)\r\n        d.append(\")\");\r\n    } else..."; }});
} catch (Exception __e) { throw rethrow(__e); } }

static String botAppendToMechList_uniqCI(String name, String text) {
  return botAppendToMechList_mode(name, text, "uniqCI");
}

static String botAppendToMechList_uniqCI(String name, Collection<String> lines) {
  return botAppendToMechList_mode(name, lines, "uniqCI");
}
static List<String> mL_facts() {
  return concatLists(mL("Random facts"), mL("Derived facts"));
}
static List<IfThen> ai_mL_parsedLogicExamples2() {
  return map_pcall("nlLogic_parseRule",ai_unparsedTalkRules());
}

static <A> A printStruct(String prefix, A a) {
  printStructure(prefix, a);
  return a;
}

static <A> A printStruct(A a) {
  printStructure(a);
  return a;
}

static boolean ai_isYesNoQuestion_1(String s) {
  return ai_yesNoQuestionWord_1(firstWord(s));
}
static Set<String> ai_questionWord_1_list = litciset("what", "who", "why", "when", "where", "how");

static boolean ai_questionWord_1(String s) {
  return containsNotNull(ai_questionWord_1_list, s);
}
static String firstWord2(String s) {
  s = xltrim(s);
  if (empty(s)) return "";
  if (isLetterOrDigit(first(s)))
    return takeCharsWhile(s, "isLetterOrDigit");
  else return "" + first(s);
}

static boolean ai_ruleContainsEval(String rule) {
  List<String> tok = javaTokC(rule);
  return containsOneOf(tok, "eval", "outputEval", "evalOutput");
}
static String botAppendToMechList(String name, String text) {
  return botAppendToMechList_now(name, text);
}
static List<String> splitAtEmptyLines(String text) {
  List<String> lines = toLines(text);
  int n = l(lines), i = 0;
  List<String> parts = new ArrayList();
  while (true) {
    while (i < n && emptyAfterTrim(lines.get(i))) ++i;
    if (i >= n) break;
    int j = i;
    while (j < n && !emptyAfterTrim(lines.get(j))) ++j;
    parts.add(fromLines(subList(lines, i, j)));
    i = j;
  }
  return parts;
}


static String mL_raw(String name) {
  return mechList_raw(name);
}
static boolean anyJavaTokens(String s) {
  return !noJavaTokens(s);
}
// f: A -> Comparable
static <A> List<A> sortedByCalculatedField(Iterable<A> c, Object f) {
  return sortByCalculatedField(c, f);
}
static List<IfThen> nlLogic_loadMechScript(String listName) {
  List<IfThen> out = new ArrayList();
  Matches m = new Matches();
  String scriptID = "";
  for (String s : splitAtEmptyLines(mL_raw(listName))) try {
    if (match("this is script *", s, m))
      { scriptID = m.unq(0) + "-"; continue; }
    IfThen rule = nlLogic_parseRule(s);
    if (rule != null)
      out.add(rule);
    else
      print("Couldn't parse: " + s);
  } catch (Throwable __e) { _handleException(__e); }
  nlLogic_setScriptID(out, scriptID);
  return out;
}
static Map<String, String> nlLogic_expandMacros_funcShortNames = litmap(
  "o", "output",
  "i", "input");

// changes original rules
static void nlLogic_expandMacros(List<IfThen> rules) {
  for (IfThen rule : unnull(rules)) try {
    nlLogic_expandMacros(rule);
  } catch (Throwable __e) { _handleException(__e); }
}

static IfThen nlLogic_expandMacros(IfThen r) {
  if (r == null) return r;
  int varCount = 0;

  String rendered;
  
  do {
    ping();
    rendered = str(r);
    nlLogic_visit(r, new VF1<Object>() { public void get(Object o) { try { 
      if (o instanceof Func) {
        String name = nlLogic_expandMacros_funcShortNames.get(((Func) o).name);
        if (name != null) ((Func) o).name = name;
      }
     } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "if (o cast Func) {\r\n        S name = nlLogic_expandMacros_funcShortNames.get(..."; }});
    
    Exp out = r.out;
    if (out instanceof Func) {
      String name = ((Func) out).name;
      if (eq(name, "sayIfTrue")) {
        r.out = new Func("output", ((Func) out).arg);
        r.in = new And(r.in, new Func("fact", ((Func) out).arg));
      } else if (eq(name, "inputToFact")) {
        r.out = new Func("fact", ((Func) out).arg);
        r.in = nlLogic_and(new Func("input", restructure(((Func) out).arg)), r.in);
      } else if (eqOneOf(name, "outputEval", "evalOutput")) {
        String var = "$_" + ++varCount;
        r.out = new Func("output", new Sentence2(var));
        r.in = new And(r.in, new Eq(new Sentence2(var), new Func("eval", ((Func) out).arg)));
        //print("Rewritten rule => " + sfu(r));
      } else if (eq(name, "output")) {
        do {
          // in: output(... $x1, $x2 [...] and $x_n ...)
          String pat = "*, * [...] * *";
          T3<String, String, String> t = jextractWithRests(pat, ((Func) out).argText());
          if (t == null) break;
          Matches m = new Matches();
          List<String> tok = javaTok(t.b);
          if (!jmatch(pat, tok, m)) break;
          String x1 = m.get(0);
          if (!isDollarVar(x1) || !endsWith(x1, "1")) { print("x1"); break; }
          String x = dropSuffix("1", x1);
          if (!eq(m.get(1), x + "2")) { print("x2"); break; }
          //S lastSep = m.get(2);
          if (!eq(m.get(3), x + "_n")) { print("x_n"); break; }
          // out: outputEval(joinWithComma_specialLast((LS) call(ai_context(), 'getList, "x"), " and "))
          ((Func) out).name = "outputEval";
          ((Func) out).arg = new Sentence2(quote(t.a) + " + joinWithComma_specialLast((LS) call(ai_context(), 'getList, " + quote(x) + "), "
            + quote(joinSubList(tok, l(tok)-5, l(tok)-2)) + ")"
            + " + " + quote(t.c));
        } while (false);
      }
    }
    
    // LHS only
    nlLogic_visit(r.in, new VF1<Object>() { public void get(Object o) { try { 
      // english a/an magic
      if (o instanceof Func)
        if (eqOneOf(((Func) o).name, "input", "fact")) {
          String s = ai_expandPatternForAAndAn(((Func) o).arg.text());
          if (s != null)
            ((Func) o).arg = new Sentence2(s);
        } else if (eq(((Func) o).name, "unresolvedFact")) {
          ((Func) o).name = "fact";
          ((Func) o).arg = new Sentence2("Unresolved fact: " + ((Func) o).arg.text());
        }
     } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "// english a/an magic\r\n      if (o cast Func)\r\n        if (eqOneOf(o.name, 'i..."; }});
  } while (neq(rendered, str(r)));
  
  return r;
}
static <A> A lookupByNLMatch(Map<String, A> map, String s) {
  return lookupByNLMatch(map, s, null);
}

static <A> A lookupByNLMatch(Map<String, A> map, String s, Matches m) {
  for (String key : keys(map))
    if (match(key, s, m))
      return map.get(key);
  return null;
}


static <A> List<A> lookupByNLMatch(MultiMap<String, A> map, String s) {
  for (String key : keys(map))
    if (match(key, s))
      return map.get(key);
  return emptyList();
}

// name -> (name, parsed map)
static Map<String, Pair<String, Map<String, String>>> mechMapCI_cache = synchroMap();

static Map<String, String> mechMapCI(String listName) {
  Pair<String, Map<String, String>> p = mechMapCI_cache.get(listName);
  String text = mechList_raw(listName);
  if (p != null && eq(p.a, text)) return p.b;
  String parsing = first(mechList_opt(listName + " | Parsing"));
  Map<String, String> map;
  if (parsing == null)
    map = parseDoubleArrowMapCI(tlft(text));
  else
    
      map = asCIMap((Map<String, String>) callAndMake(parsing, text));
    
    
  p = pair(text, map);
  mechMapCI_cache.put(listName, p);
  return p.b;
}
static String[] dropFirst(int n, String[] a) {
  return drop(n, a);
}

static String[] dropFirst(String[] a) {
  return drop(1, a);
}

static Object[] dropFirst(Object[] a) {
  return drop(1, a);
}

static <A> List<A> dropFirst(List<A> l) {
  return dropFirst(1, l);
}

static <A> List<A> dropFirst(int n, Iterable<A> i) { return dropFirst(n, toList(i)); }
static <A> List<A> dropFirst(Iterable<A> i) { return dropFirst(toList(i)); }

static <A> List<A> dropFirst(int n, List<A> l) {
  return n <= 0 ? l : new ArrayList(l.subList(Math.min(n, l.size()), l.size()));
}

static <A> List<A> dropFirst(List<A> l, int n) {
  return dropFirst(n, l);
}

static String dropFirst(int n, String s) { return substring(s, n); }
static String dropFirst(String s) { return substring(s, 1); }
static int parseIntAtEnd(String s) {
  int i = l(s);
  while (i > 0 && isDigit(charAt(s, i-1))) --i;
  return parseInt(substring(s, i));
}
static boolean canQuickEval(String t) {
  return isQuoted(t) || isIdentifier(t) || isInteger(t) || isSnippetID(t);
}
static Object quickEval(String t) {
  if (isQuoted(t)) return unquote(t);
  if (isInteger(t)) return parseInt(t);
  if (eq(t, "null")) return null;
  if (eq(t, "false")) return false;
  if (eq(t, "true")) return true;
  if (isIdentifier(t)) return getMC(t);
  if (isSnippetID(t)) return t;
  throw quickFail("Can't quick eval: " + t);
}
// returns pair(functionName, args)
static Pair<String, List<String>> tok_parseFunctionCall(String s) {
  return tok_parseFunctionCall(javaTok(s));
}
  
static Pair<String, List<String>> tok_parseFunctionCall(List<String> tok) {
  if (!(isIdentifier(get(tok, 1)) && eqGet(tok, 3, "("))) return null;
  Map<Integer, Integer> bracketMap = getBracketMap(tok);
  Integer iClosing = bracketMap.get(3);
  if (!(eq(iClosing, l(tok)-4) && eqGet(tok, l(tok)-2, ";"))
    && neq(iClosing, l(tok)-2)) return null;
  int i = 5, argStart = 5;
  List<String> args = new ArrayList();
  while (i < iClosing) {
    Integer j = bracketMap.get(i);
    if (j != null) { i = j+2; continue; }
    if (eqGetOneOf(tok, i, ",")) {
      if (i > argStart) args.add(trimJoinSubList(tok, argStart, i));
      argStart = i+2;
    }
    i += 2;
  }
  if (i > argStart) args.add(trimJoinSubList(tok, argStart, i));
  return pair(tok.get(1), args);
}
static Object callAndMake(String functionName, Object... args) {
  return callAndMake_orDirect(functionName, args);
}
static void _threadInfo_addMakerAndRetriever(VF1<Map> maker, VF1<Map> retriever) {
  _threadInfo_makers.add(maker);
  _threadInheritInfo_retrievers.add(retriever);
}
static <A, B> void mapPut(Map<A, B> map, A key, B value) {
  if (map != null && key != null && value != null) map.put(key, value);
}
static boolean gac3k_verbose;

static Cache<TreeMap<String, Float>> gac36k_cache = new Cache("gac36k_load");
static TreeMap<String, Float> gac36k() { return gac36k_cache.get(); }

static TreeMap<String, Float> gac36k_load() {
  TreeMap<String,Float> map = new TreeMap();
  for (String s : tlft(loadSnippet("#2000592"))) try {
    int i = indexOfAny(s, 0, " \t");
    float f = parseFloat(takeFirst(i, s));
    Float previous = mapPutIfChange2(map, trim(substring(s, i)), f);
    if (gac3k_verbose && previous != null)
      print("Gac-36K: Duplicate question: " + s + " (was: " + previous + ")");
  } catch (Throwable __e) { _handleException(__e); }
  print("GAC-36K: Have " + n(map, "question"));
  return map;
}

static PTElement ai_cullParseTree(PTElement e) {
  if (e == null) return e;
  if (!(e instanceof ChoosePart))
    removeAllButFirst(e.children);
  for (PTElement child : unnull(e.children))
    ai_cullParseTree(child);
  return e;
}
static String ai_renderParseTree(final PTElement e) {
  return hijackPrint(new Runnable() {  public void run() { try {  ai_printParseTree(e) ;
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "ai_printParseTree(e)"; }});
}
static List<String> ai_uncategorizedWordsInSentence(String s) {
  return filter(uniquify(words(s)) , new F1<String, Object>() { Object get(String word) { try { return  empty(ai_wordToCategories(word)) ; } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "empty(ai_wordToCategories(word))"; }});
}


static TimedCache<Map<String, Map<String, Set<String>>>> ai_wordToCategories_cache = new TimedCache("ciMap", 10.0); // language -> word -> categories

static Map<String, Set<String>> ai_wordToCategories() {
  Map<String, Map<String, Set<String>>> languages = ai_wordToCategories_cache.get();
  Map<String, Set<String>> map = languages.get(ai_language());
  if (map == null)
    languages.put(ai_language(), map = ai_wordToCategories_make());
  return map;
}

static Map<String, Set<String>> ai_wordToCategories_make() {
  Map<String, Set<String>> catMap = ai_wordCategoriesWithElements();
  
  final Map<String, Set<String>> map = ciMap();
  for (String cat : keys(catMap))
    for (String word : catMap.get(cat)) {
      Set<String> set = map.get(word);
      if (set == null) map.put(word, set = new LinkedHashSet());
      set.add(cat);
    }
  return map;
}

static Set<String> ai_wordToCategories(String word) {
  return ai_wordToCategories().get(word);
}



static Map<String, Double> ai_guessCategoriesForUnknownWord_weighted(String word) {
  Map<String, Double> map = lithashmap("<noun>" , 0.3, "<verb>" , 0.3, "<adjective>" , 0.2);
  if (ewic(word, "ing")) map.put("<ingform>" , 0.6);
  if (ewic(word, "ly")) map.put("<adverb>" , 0.6);
  if (eqic(word, "ed")) map.put("<past_participle>" , 0.5);
  return map;
}
static PTElement ai_parseBottomUpToTree(String input) {
  return ai_parseBottomUpToTree(new AI_BottomUpParser1(), input);
}

static PTElement ai_parseBottomUpToTree(AI_BottomUpParser1 parser, String input) {
  parser.parse(input);
  return ai_parseBottomUpToTree(parser);
}

static PTElement ai_parseBottomUpToTree(AI_BottomUpParser1 parser, List<String> tok) {
  parser.parse(tok);
  return ai_parseBottomUpToTree(parser);
}

static PTElement ai_parseBottomUpToTree(AI_BottomUpParser1 parser) {
  AI_BottomUpParser1.Word w = parser.fullGroup();
  if (w == null) return null;
  List<String> classes = withoutQuoted(w.classes);
  if (empty(classes)) return null;
  return ai_completeParseTree(parser, new ChooseCategory(w, classes));
}
// usually L<S>
static String fromLines(Collection lines) {
  StringBuilder buf = new StringBuilder();
  if (lines != null)
    for (Object line : lines)
      buf.append(str(line)).append('\n');
  return buf.toString();
}

static String fromLines(String... lines) {
  return fromLines(asList(lines));
}
static IterableIterator<String> toLines(File f) {
  return linesFromFile(f);
}

static List<String> toLines(String s) {
  List<String> lines = new ArrayList<String>();
  if (s == null) return lines;
  int start = 0;
  while (true) {
    int i = toLines_nextLineBreak(s, start);
    if (i < 0) {
      if (s.length() > start) lines.add(s.substring(start));
      break;
    }

    lines.add(s.substring(start, i));
    if (s.charAt(i) == '\r' && i+1 < s.length() && s.charAt(i+1) == '\n')
      i += 2;
    else
      ++i;

    start = i;
  }
  return lines;
}

static int toLines_nextLineBreak(String s, int start) {
  for (int i = start; i < s.length(); i++) {
    char c = s.charAt(i);
    if (c == '\r' || c == '\n')
      return i;
  }
  return -1;
}
static ChoosePart ai_parseTree_descendToComposite(PTElement e) {
  /*while (e instanceof ChooseCategory
    || e instanceof HaveCategory
      && (e/HaveCategory.reconstruction == null || e/HaveCategory.reconstruction.b == null)) {*/
  while (e != null && !(e instanceof ChoosePart)) {
    PTElement e2 = e;
    e = first(e.children); // Choose most likely reconstruction
    //print("Went from " + className(e2) + "(" + e + ") to " + className(e) + " (" + n2(e2.children, "child", "children") + ")");
  }
  return (ChoosePart) e;
}

static <A> A second(List<A> l) {
  return get(l, 1);
}

static <A> A second(A[] bla) {
  return bla == null || bla.length <= 1 ? null : bla[1];
}


static <A, B> B second(Pair<A, B> p) {
  return p == null ? null : p.b;
}



static <A, B, C> B second(T3<A, B, C> t) {
  return t == null ? null : t.b;
}

static <A> void insertFirst(List<A> l, A a) {
  if (l != null) l.add(0, a);
}
static <A, B> List<Pair<A, B>> combineZigZag(List<A> l1, List<B> l2) {
  int lA = l(l1), lB = l(l2), maxSum = max(lA, lB);
  List<Pair<A, B>> out = new ArrayList();
  
  int ndiags = lA + lB - 1;
  for (int diag = 0; diag < ndiags; diag++) {
    int row_stop = max(0, diag - lA + 1);
    int row_start = min(diag, lB - 1);
    for (int row = row_start; row >= row_stop; row--) {
      // on a given diagonal row + col = constant "diag"
      // diag labels the diagonal number
      int col = diag - row;
      out.add(pair(l1.get(col), l2.get(row)));
    }
  }

  return out;
}
static AutoCloseable tempSetField(final Object o, final String field, Object value) {
  final Object oldValue = get(o, field);
  set(o, field, value);
  return new AutoCloseable() { public void close() {
    set(o, field, oldValue);
  }};
}
static Object evalWithTimeoutOrException(final Object f, int timeoutMS) {
  return evalWithTimeoutOrException(timeoutMS, f);
}

static Object evalWithTimeoutOrException(int timeoutMS, final Object f) {
  return eitherAOpt(evalWithTimeout(timeoutMS, f));
}

static Object evalWithTimeoutOrException(double timeoutSeconds, final Object f) {
  return eitherAOpt(evalWithTimeout(timeoutSeconds, f));
}

static <A> A evalWithTimeoutOrException(int timeoutMS, F0<A> f) {
  Either<Object, Thread> e = evalWithTimeout(timeoutMS, f);
  if (e.isA()) return (A) e.a();
  throw fail("Timeout after " + timeoutMS + " ms");
}

static <A> A evalWithTimeoutOrException(double timeoutSeconds, F0<A> f) {
  return evalWithTimeoutOrException(toMS(timeoutSeconds), f);
}
static Pair<List<IfThen>, List<String>> ai_rulesAndFacts() {
  return ai_activeRulesAndFacts();
}



static Pair<List<IfThen>, List<String>> ai_rulesAndFacts(List<String> extraFacts) {
  return ai_activeRulesAndFacts(extraFacts);
}
static boolean ai_deriveFacts_debug;

static Set<String> ai_deriveFacts(List<IfThen> rules, List<String> facts) {
  final NLLogicChecker_v3 c = new NLLogicChecker_v3();
  c.facts = facts;
  final NLLogicChecker_v2.Matching m = new NLLogicChecker_v2.Matching();
  final LinkedHashSet<String> outFacts = new LinkedHashSet();
  for (final IfThen rule : nlLogic_filterByRHSConstructs(rules, "fact")) {
    if (ai_deriveFacts_debug)
      print("ai_deriveFacts: Checking " + rule);
    c.iterate(rule.in, m, new Runnable() {  public void run() { try { 
      if (ai_deriveFacts_debug)
        print("Got matches: " + sfu(m.matches));
        
      for (Exp e : nlLogic_unrollAnd(rule.out)) {
        if (e instanceof Func)
          if (eq(((Func) e).name, "fact"))
            { outFacts.add(nlLogic_text(c.apply(((Func) e).arg, m))); continue; }
        if (c.checkHelper(e, m))
          continue;
        print("Ignoring unknown RHS element: " + e);
      }
    
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "if (ai_deriveFacts_debug)\r\n        print(\"Got matches: \" + sfu(m.matches));\r\n..."; }});
  }
  return outFacts;
}
static List<String> toLinesFullTrim(String s) {
  List<String> l = new ArrayList();
  for (String line : toLines(s)) if (nempty(line = trim(line))) l.add(line);
  return l;
}

static List<String> toLinesFullTrim(File f) {
  List<String> l = new ArrayList();
  for (String line : linesFromFile(f)) if (nempty(line = trim(line))) l.add(line);
  return l;
}

static Object dm_moduleOfType(String type) {
  return first(dm_modulesOfType(type));
}
static Object evalJava(String code) {
  return evalJava_main(evalJava_prep(code));
}
static List<String> mechList_opt(String name) {
  return mechList_opt_tlft(name);
}
static List<IfThen> ai_parseRules(String s) {
  return map_pcall_notNull("nlLogic_parseRule",splitAtEmptyLines(s));
}

static String appendToMechList(String name, String text) {
  return botAppendToMechList_uniqCI(name, text);
}

static String appendToMechList(String name, Collection<String> lines) {
  return botAppendToMechList_uniqCI(name, lines);
}
static <A> List<A> sortedByFieldDesc(Collection<A> c, String field) {
  List<A> l = new ArrayList(c);
  sort(l, descFieldComparator(field));
  return l;
}

static <A> List<A> sortedByFieldDesc(String field, Collection<A> c) {
  return sortedByFieldDesc(c, field);
}
static boolean nlLogic_checkFirstConditions2_debug;

// returns (number of succeeded conditions, remaining conditions)
static Pair<Integer, Exp> nlLogic_checkFirstConditions2(NLLogicChecker_v2 c, IfThen rule, NLLogicChecker_v2.Matching m) {
  Exp exp = rule.in;
  int succeeded = 0;
  while  (exp != null) { ping(); 
    Pair<Boolean, Exp> p = c.checkFirstCondition(exp, m);
    if (nlLogic_checkFirstConditions2_debug)
      print("nlLogic_checkFirstConditions2: exp=" + exp + ", p=" + p);
    if (!p.a) break;
    ++succeeded;
    exp = p.b;
  }
  return pair(succeeded, exp);
}
static Exp nlLogic_applyMatches(Exp e, Map<String, String> m) {
  if (e == null) return null;
  if (e instanceof And)
    return new And(nlLogic_applyMatches(((And) e).a, m), nlLogic_applyMatches(((And) e).b, m));
  if (e instanceof ExpNot)
    return new ExpNot(nlLogic_applyMatches(((ExpNot) e).a, m));
  if (e instanceof Func)
    return new Func(((Func) e).name, nlLogic_applyMatches(((Func) e).arg, m));
    
  if (e instanceof Sentence)
    return new Sentence(javaTok(ai_standardReplaceVars(((Sentence) e).text(), m)));
  if (e instanceof Sentence2)
    return new Sentence2(ai_standardReplaceVars(((Sentence2) e).text(), m));
    
  return e;
}
static Exp nlLogic_firstCondition(Exp e) {
  if (e instanceof And) return ((And) e).a;
  return e;
}

static Exp nlLogic_firstCondition(IfThen rule) {
  return rule == null ? null : nlLogic_firstCondition(rule.in);
}
static boolean ai_deriveOutput_debug;

static Set<String> ai_deriveOutput(List<IfThen> rules, List<String> facts) {
  final NLLogicChecker_v3 c = new NLLogicChecker_v3();
  c.facts = facts;
  c.rules = rules;
  final NLLogicChecker_v2.Matching m = new NLLogicChecker_v2.Matching();
  final LinkedHashSet<String> out = new LinkedHashSet();
  for (final IfThen rule : nlLogic_filterByRHSConstructs(rules, "output")) {
    if (ai_deriveOutput_debug)
      print("ai_deriveOutput: Checking " + rule);
    c.iterate(rule.in, m, new Runnable() {  public void run() { try { 
      if (ai_deriveOutput_debug)
        print("Got matches: " + sfu(m.matches));
        
      for (Exp e : nlLogic_unrollAnd(rule.out)) {
        if (e instanceof Func)
          if (eq(((Func) e).name, "output"))
            { out.add(nlLogic_text(c.apply(((Func) e).arg, m))); continue; }
        if (c.checkHelper(e, m))
          continue;
        print("Ignoring unknown RHS element: " + e);
      }
    
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "if (ai_deriveOutput_debug)\r\n        print(\"Got matches: \" + sfu(m.matches));\r..."; }});
  }
  return out;
}
static <A> IterableIterator<A> iteratorFromFunction_f0(final F0<A> f) {
  class IFF2 extends IterableIterator<A> {
    A a;
    boolean done;
    
    public boolean hasNext() {
      getNext();
      return !done;
    }
    
    public A next() {
      getNext();
      if (done) throw fail();
      A _a = a;
      a = null;
      return _a;
    }
    
    void getNext() {
      if (done || a != null) return;
      a = f.get();
      done = a == null;
    }
  };
  return new IFF2();
}
static ThreadLocal<Object> ai_context_tl() { return ai_context_tl; }
static <A> A findByField(Collection<A> c, String field, Object value) {
  for (A a : c)
    if (eq(getOpt(a, field), value))
      return a;
  return null;
}
static <A> AutoCloseable tempAdd(final Collection<A> l, final A a) {
  if (l == null || l.contains(a)) return null;
  l.add(a);
  return new AutoCloseable() { public void close() throws Exception { l.remove(a); }};
}
static <A> A copyFields(Object x, A y, String... fields) {
  if (empty(fields)) { // assume we should copy all fields
    Map<String, Object> map = objectToMap(x);
    for (String field : map.keySet())
      setOpt(y, field, map.get(field));
  } else 
    for (String field : fields) {
      Object o = getOpt(x, field);
      if (o != null)
        setOpt(y, field, o);
    }
  return y;
}

static <A> A copyFields(Object x, A y, Collection<String> fields) {
  return copyFields(x, y, asStringArray(fields));
}
static Object evalWithDollarVars_assertAllVarsSet(String code, final Map<String, String> values) {
  Pair<List<String>, Object> p = prepareEvalWithDollarVars_assertAllVarsSet(code, values);
  return call(p.b, "calc", toObjectArray(map(p.a, new F1<String, String>() { String get(String var) { try { return  mapGet(values, var) ; } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "mapGet(values, var)"; }})));
}
static <A> A setThreadLocal(ThreadLocal<A> tl, A value) {
  if (tl == null) return null;
  A old = tl.get();
  tl.set(value);
  return old;
}
static String javaTok_substringN(String s, int i, int j) {
  if (i == j) return "";
  if (j == i+1 && s.charAt(i) == ' ') return " ";
  return s.substring(i, j);
}
static String javaTok_substringC(String s, int i, int j) {
  return s.substring(i, j);
}
static List<String> javaTokWithExisting(String s, List<String> existing) {
  ++javaTok_n;
  int nExisting = javaTok_opt && existing != null ? existing.size() : 0;
  ArrayList<String> tok = existing != null ? new ArrayList(nExisting) : new ArrayList();
  int l = s.length();
  
  int i = 0, n = 0;
  while (i < l) {
    int j = i;
    char c, d;
    
    // scan for whitespace
    while (j < l) {
      c = s.charAt(j);
      d = j+1 >= l ? '\0' : s.charAt(j+1);
      if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
        ++j;
      else if (c == '/' && d == '*') {
        do ++j; while (j < l && !s.substring(j, Math.min(j+2, l)).equals("*/"));
        j = Math.min(j+2, l);
      } else if (c == '/' && d == '/') {
        do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0);
      } else
        break;
    }
    
    if (n < nExisting && javaTokWithExisting_isCopyable(existing.get(n), s, i, j))
      tok.add(existing.get(n));
    else
      tok.add(javaTok_substringN(s, i, j));
    ++n;
    i = j;
    if (i >= l) break;
    c = s.charAt(i);
    d = i+1 >= l ? '\0' : s.charAt(i+1);

    // scan for non-whitespace
    
    // Special JavaX syntax: 'identifier
    if (c == '\'' && Character.isJavaIdentifierStart(d) && i+2 < l && "'\\".indexOf(s.charAt(i+2)) < 0) {
      j += 2;
      while (j < l && Character.isJavaIdentifierPart(s.charAt(j)))
        ++j;
    } else if (c == '\'' || c == '"') {
      char opener = c;
      ++j;
      while (j < l) {
        if (s.charAt(j) == opener /*|| s.charAt(j) == '\n'*/) { // allow multi-line strings
          ++j;
          break;
        } else if (s.charAt(j) == '\\' && j+1 < l)
          j += 2;
        else
          ++j;
      }
    } else if (Character.isJavaIdentifierStart(c))
      do ++j; while (j < l && (Character.isJavaIdentifierPart(s.charAt(j)) || "'".indexOf(s.charAt(j)) >= 0)); // for stuff like "don't"
    else if (Character.isDigit(c)) {
      do ++j; while (j < l && Character.isDigit(s.charAt(j)));
      if (j < l && s.charAt(j) == 'L') ++j; // Long constants like 1L
    } else if (c == '[' && d == '[') {
      do ++j; while (j+1 < l && !s.substring(j, j+2).equals("]]"));
      j = Math.min(j+2, l);
    } else if (c == '[' && d == '=' && i+2 < l && s.charAt(i+2) == '[') {
      do ++j; while (j+2 < l && !s.substring(j, j+3).equals("]=]"));
      j = Math.min(j+3, l);
    } else
      ++j;
      
    if (n < nExisting && javaTokWithExisting_isCopyable(existing.get(n), s, i, j))
      tok.add(existing.get(n));
    else
      tok.add(javaTok_substringC(s, i, j));
    ++n;
    i = j;
  }
  
  if ((tok.size() % 2) == 0) tok.add("");
  javaTok_elements += tok.size();
  return tok;
}

static boolean javaTokWithExisting_isCopyable(String t, String s, int i, int j) {
  return t.length() == j-i
    && s.regionMatches(i, t, 0, j-i); // << could be left out, but that's brave
}
static boolean containsIgnoreCase(Collection<String> l, String s) {
  for (String x : l)
    if (eqic(x, s))
      return true;
  return false;
}

static boolean containsIgnoreCase(String[] l, String s) {
  for (String x : l)
    if (eqic(x, s))
      return true;
  return false;
}

static boolean containsIgnoreCase(String s, char c) {
  return indexOfIgnoreCase(s, String.valueOf(c)) >= 0;
}

static boolean containsIgnoreCase(String a, String b) {
  return indexOfIgnoreCase(a, b) >= 0;
}
static boolean nlLogic_matchMultiplePatterns(NLLogicChecker_v2 c, List<String> patterns, String s, NLLogicChecker_v2.Matching m) {
  return nlLogic_matchMultiplePatterns(c, patterns, s, m.matches);
}

static boolean nlLogic_matchMultiplePatterns(NLLogicChecker_v2 c, List<String> patterns, String s, Map<String, String> matches) {
  for (String pat : unnull(patterns))
    if (c.matcher.match(pat, s, matches)) return true;
  return false;
}
static <A, B> LinkedHashMap<A, B> cloneLinkedHashMap(Map<A, B> map) {
  return map == null ? new LinkedHashMap() : new LinkedHashMap(map);
}
static List<String> javaTokWithBrackets(String s) {
  return javaTokPlusBrackets(s);
}
static List<String> mapReplaceVars2(List<String> patterns, final Map<String, String> map) {
  return map(new F1<String, Object>() { Object get(String pat) { try { return  replaceVars(pat, map) ; } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "replaceVars(pat, map)"; }}, patterns);
}
static List<String> nlTok5(String s) {
  List<String> tok = new ArrayList();
  int l = l(s);
  
  int i = 0;
  while (i < l) {
    int j = i;
    char c; String cc;
    
    // scan for whitespace
    while (j < l) {
      c = s.charAt(j);
      cc = s.substring(j, Math.min(j+2, l));
      if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
        ++j;
      else if (cc.equals("/*")) {
        do ++j; while (j < l && !s.substring(j, Math.min(j+2, l)).equals("*/"));
        j = Math.min(j+2, l);
      } else if (cc.equals("//")) {
        do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0);
      } else
        break;
    }
    
    tok.add(s.substring(i, j));
    i = j;
    if (i >= l) break;
    c = s.charAt(i);
    cc = s.substring(i, Math.min(i+2, l));

    // scan for non-whitespace
    if (c == '\u201C' || c == '\u201D') c = '"'; // normalize quotes
    if (c == '"') {
      char opener = c;
      ++j;
      while (j < l) {
        char _c = s.charAt(j);
        if (_c == '\u201C' || _c == '\u201D') _c = '"'; // normalize quotes
        if (_c == opener) {
          ++j;
          break;
        } else if (s.charAt(j) == '\\' && j+1 < l)
          j += 2;
        else
          ++j;
      }
      if (j-1 >= i+1) {
        tok.add(opener + s.substring(i+1, j-1) + opener);
        i = j;
        continue;
      }
    } else if (Character.isJavaIdentifierStart(c))
      do ++j; while (j < l && Character.isJavaIdentifierPart(s.charAt(j)));
    else if (Character.isDigit(c))
      do ++j; while (j < l && Character.isDigit(s.charAt(j)));
    else if (cc.equals("[[")) {
      do ++j; while (j+1 < l && !s.substring(j, j+2).equals("]]"));
      j = Math.min(j+2, l);
    } else if (cc.equals("[=") && i+2 < l && s.charAt(i+2) == '[') {
      do ++j; while (j+2 < l && !s.substring(j, j+3).equals("]=]"));
      j = Math.min(j+3, l);
    } else if (s.substring(j, Math.min(j+3, l)).equals("..."))
      j += 3;
    else if (c == '$' || c == '#')
      do ++j; while (j < l && Character.isDigit(s.charAt(j)));
    else
      ++j;

    tok.add(s.substring(i, j));
    i = j;
  }
  
  if ((tok.size() % 2) == 0) tok.add("");
  return tok;
}

static <A, B> A eitherAOpt(Either<A, B> e) {
  return e != null && e.isA() ? e.a() : null;
}
static Set<Thread> evalWithTimeout_inTime = synchroSet();
static Set<Thread> evalWithTimeout_allThreads = newWeakHashSet();

static ThreadLocal<String> evalWithTimeout_threadName = new ThreadLocal();

// Either calculation result or Thread with timed-out computation
// Tries to interrupt thread after timeout
// Errors are rethrown.
static Either<Object, Thread> evalWithTimeout(int timeoutMS, final Object r) {
  final Flag done = new Flag();
  final Flag doneWaiting = new Flag();
  final Var var = new Var();
  final Var<Throwable> error = new Var();
  
  Thread t = newThread(getAndClearThreadLocal(evalWithTimeout_threadName), new Runnable() {  public void run() { try { 
    try {
      try {
        var.set(callF(r));
      } finally {
        evalWithTimeout_allThreads.remove(currentThread());
      }
    } catch (Throwable e) {
      error.set(e);
      if (doneWaiting.isUp())
        printStackTrace(e);
    } finally {
      done.raise();
    }
  
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "try {\r\n      try {\r\n        var.set(callF(r));\r\n      } finally {\r\n        ev..."; }});
  
  beforeDelegatingToThread(t);
  try {
    startThread(t);
    
    evalWithTimeout_inTime.add(t);
    evalWithTimeout_allThreads.add(t);
    try {
      done.waitUntilUp(timeoutMS);
      doneWaiting.raise();
    } finally {
      evalWithTimeout_inTime.remove(t);
    }
      
    // timeout! cancel/interrupt and return thread object
    if (!done.isUp()) {
      print("Cancelling thread (timeout)");
      cancelAndInterruptThread(t);
      return either2(t);
    }
    
    // thread ended with error
    if (error.get() != null) rethrow(error.get());
    
    // thread ended naturally
    return either1(var.get());
  } finally {
    afterDelegatingToThread(t);
  }
}

static Either<Object, Thread> evalWithTimeout(double timeoutSeconds, final Object r) {
  return evalWithTimeout(toMS_int(timeoutSeconds), r);
}
static <A> IterableIterator<A> enumerationToIterator(final Enumeration<A> e) {
  return e == null ? null : new IterableIterator() {
    public boolean hasNext() {
      return e.hasMoreElements();
    }
    
    public A next() {
      return e.nextElement();
    }
  };
}
static String ai_lookupCurlyCheatSheet(String s) {
  if (contains(s, "{")) return s;
  for (String line : mL(ai_language() + " | Curly parses cheat sheet"))
    if (match(line, s))
      return line;
  return s;
}
static PTElement ai_parseToCulledTree(String input) {
  return ai_cullParseTree(ai_parseToTreeWithCurlyPhrases(input));
}
static List<PTElement> ai_scanParseTreeForCategory(PTElement e, String category) {
  List<PTElement> out = new ArrayList();
  ai_scanParseTreeForCategory_scan(e, category, out);
  return out;
}

static void ai_scanParseTreeForCategory_scan(PTElement e, String category, List<PTElement> out) {
  if (e == null) return;
  if (e instanceof HaveCategory)
    if (eq(((HaveCategory) e).category, category))
      out.add(((HaveCategory) e));
  for (PTElement child : unnull(e.children))
    ai_scanParseTreeForCategory_scan(child, category, out);
}
static PTElement ai_parseTree_simplifyNode(PTElement e) {
  if (e == null) return null;
  e = ai_parseTree_descendToComposite(e);
  if (e == null) return null;
  if (e instanceof ChoosePart) {
    ai_Reconstructed r = ((ChoosePart) e).reconstruction;
    WeightedProduction prod = r.production;
    String hint = prod.simplificationHint;
    if (eq(hint, "L")) return first(e.children);
    if (eq(hint, "R")) return second(e.children);
  }
  return e;
}
static int parseIntOpt(String s) {
  return isInteger(s) ? parseInt(s) : 0;
}
static int jfind(String s, String in) {
  return jfind(javaTok(s), in);
}

static int jfind(List<String> tok, String in) {
  return jfind(tok, 1, in);
}

static int jfind(List<String> tok, int startIdx, String in) {
  return jfind(tok, startIdx, in, null);
}

static int jfind(List<String> tok, String in, Object condition) {
  return jfind(tok, 1, in, condition);
}

static int jfind(List<String> tok, int startIdx, String in, Object condition) {
  List<String> tokin = javaTok(in);
  jfind_preprocess(tokin);
  return jfind(tok, startIdx, tokin, condition);
}

// assumes you preprocessed tokin
static int jfind(List<String> tok, List<String> tokin) {
  return jfind(tok, 1, tokin);
}

static int jfind(List<String> tok, int startIdx, List<String> tokin) {
  return jfind(tok, startIdx, tokin, null);
}

static int jfind(List<String> tok, int startIdx, List<String> tokin, Object condition) {
  return findCodeTokens(tok, startIdx, false, toStringArray(codeTokensOnly(tokin)), condition);
}

static List<String> jfind_preprocess(List<String> tok) {
  for (String type : litlist("quoted", "id", "int"))
    replaceSublist(tok, ll("<", "", type, "", ">"), ll("<" + type + ">"));
  replaceSublist(tok, ll("\\", "", "*"), ll("\\*"));
  return tok;
}
static boolean isJavaIdentifierAfter(String s, int i) {
  int n = l(s);
  if (i >= n || !Character.isJavaIdentifierStart(s.charAt(i)))
    return false;
  for (i++; i < n; i++)
    if (!Character.isJavaIdentifierPart(s.charAt(i)))
      return false;
  return true;
}
static List<String> parse3(String s) {
  return dropPunctuation(javaTokPlusPeriod(s));
}
// We dropped the "***" support here (use match3 for that)

static String[] find2(List<String> pat, List<String> tok) {
  for (int idx = 0; idx < tok.size(); idx += 2) {
    String[] result = find2(pat, tok, idx);
    if (result != null) return result;
  }
  return null;
}

static String[] find2(List<String> pat, List<String> tok, int idx) {
  if (idx+pat.size() > tok.size())
    return null;
  List<String> result = new ArrayList();
  for (int i = 1; i < pat.size(); i += 2) {
    String p = pat.get(i), t = tok.get(idx+i);
    if (eq(p, "*"))
      result.add(t);
    else if (!p.equalsIgnoreCase(t))
      return null;
  }
  return toStringArray(result);
}

static Map safeUnstructureMap(String s) {
  return (Map) safeUnstructure(s);
}
static <A> boolean canPutAllStrictlyIC(Map<A, String> map, Map<A, String> map2) {
  { final Map<A, String> __1366 = unnull(map2); for (A key : keys( __1366)) { String value =__1366.get(key); 
    if (!canPutStrictlyIC(map, key, value))
      return false;
  }
  }return true;
}
static String dropSuffixAndTrim(String suffix, String s) {
  return trim(dropSuffix(suffix, s));
}
// returns l(s) if not found
static int smartIndexOf(String s, String sub, int i) {
  if (s == null) return 0;
  i = s.indexOf(sub, min(i, l(s)));
  return i >= 0 ? i : l(s);
}

static int smartIndexOf(String s, int i, char c) {
  return smartIndexOf(s, c, i);
}

static int smartIndexOf(String s, char c, int i) {
  if (s == null) return 0;
  i = s.indexOf(c, min(i, l(s)));
  return i >= 0 ? i : l(s);
}

static int smartIndexOf(String s, String sub) {
  return smartIndexOf(s, sub, 0);
}

static int smartIndexOf(String s, char c) {
  return smartIndexOf(s, c, 0);
}

static <A> int smartIndexOf(List<A> l, A sub) {
  return smartIndexOf(l, sub, 0);
}

static <A> int smartIndexOf(List<A> l, int start, A sub) {
  return smartIndexOf(l, sub, start);
}

static <A> int smartIndexOf(List<A> l, A sub, int start) {
  int i = indexOf(l, sub, start);
  return i < 0 ? l(l) : i;
}
static String joinSubList(List<String> l, int i, int j) {
  return join(subList(l, i, j));
}

static String joinSubList(List<String> l, int i) {
  return join(subList(l, i));
}
static String shortClassName(Object o) {
  if (o == null) return null;
  Class c = o instanceof Class ? (Class) o : o.getClass();
  String name = c.getName();
  return shortenClassName(name);
}
static List<String> splitAtTokens(String s, List<String> tokens) {
  return splitAtTokens(javaTok(s), tokens);
}

static List<String> splitAtTokens(List<String> tok, List<String> tokens) {
  List<String> l = new ArrayList();
  int i = 0;
  while (i < l(tok)) {
    int j = indexOfSubList(tok, tokens, i);
    if (i >= l(tok)) break;
    if (j < 0) j = l(tok);
    l.add(trimJoin(tok.subList(i, j)));
    i = j+l(tokens);
  }
  return l;
}


static Map<String, List<String>> parse3_cachedPattern_cache = synchronizedMRUCache(100);

static synchronized List<String> parse3_cachedPattern(String s) {
  List<String> tok = parse3_cachedPattern_cache.get(s);
  if (tok == null) parse3_cachedPattern_cache.put(s, tok = parse3(s));
  return tok;
}




static Map<String, List<String>> parse3_cachedInput_cache = synchronizedMRUCache(100);

static synchronized List<String> parse3_cachedInput(String s) {
  List<String> tok = parse3_cachedInput_cache.get(s);
  if (tok == null) parse3_cachedInput_cache.put(s, tok = parse3(s));
  return tok;
}


static boolean cicAtOddPosition(List<String> l, String s) {
  int n = l(l);
  for (int i = 1; i < n; i += 2)
    if (eqic(l.get(i), s))
      return true;
  return false;
}
static List<List<String>> ai_spreadCertainIndices(List<String> tokPat, List<String> tokS, List<Integer> indices) {
  int extra = l(tokS)-l(tokPat);
  if (extra <= 0 || empty(indices)) return emptyList();

  List < List < String > > results = new ArrayList();
  for (List<Integer> spread : allSpreads(extra, l(indices))) {
    List<String> tok = new ArrayList();
    int pos = 0, iSpread = 0, iIndices = 0;
    for (int i = 0; i < l(tokPat); i++)
      if (!eq(get(indices, iIndices), i))
        tok.add(tokS.get(pos++));
      else {
        ++iIndices;
        int take = spread.get(iSpread++);
        tok.add(joinWithSpace(subList(tokS, pos, pos+take+1)));
        pos += take+1;
      }
    results.add(tok);
  }
  return results;
}
static Map<String, String> matchDollarVarsIC(String pat, String s) {
  if (s == null) return null;
  return matchDollarVarsIC(pat, parse3_cachedInput(s));
}

static Map<String, String> matchDollarVarsIC(String pat, List<String> tokS) {
  if (pat == null || tokS == null) return null;
  List<String> vars = new ArrayList();
  String pat2 = dollarVarsToStars(pat, vars);
  Matches m = new Matches();
  if (!match3(pat2, tokS, m)) return null;
  Map<String, String> map = new HashMap();
  for (int i = 0; i < l(m.m); i++) {
    String var = vars.get(i);
    String value = m.get(i);
    String oldValue = map.get(var);
    if (oldValue == null) map.put(var, value);
    else if (neqic(oldValue, value)) return null;
  }
  return map;
}
// match2 matches multiple "*" (matches a single token) wildcards and zero or one "..." wildcards (matches multiple tokens)

static String[] match2(List<String> pat, List<String> tok) {
  // standard case (no ...)
  int i = pat.indexOf("...");
  if (i < 0) return match2_match(pat, tok);
  
  pat = new ArrayList<String>(pat); // We're modifying it, so copy first
  pat.set(i, "*");
  while (pat.size() < tok.size()) {
    pat.add(i, "*");
    pat.add(i+1, ""); // doesn't matter
  }
  
  return match2_match(pat, tok);
}

static String[] match2_match(List<String> pat, List<String> tok) {
  List<String> result = new ArrayList<String>();
  if (pat.size() != tok.size()) {
    
    return null;
  }
  for (int i = 1; i < pat.size(); i += 2) {
    String p = pat.get(i), t = tok.get(i);
    
    if (eq(p, "*"))
      result.add(t);
    else if (!equalsIgnoreCase(unquote(p), unquote(t))) // bold change - match quoted and unquoted now
      return null;
  }
  return result.toArray(new String[result.size()]);
}

static String jreplace(String s, String in, String out) {
  return jreplace(s, in, out, null);
}

static String jreplace(String s, String in, String out, Object condition) {
  List<String> tok = javaTok(s);
  return jreplace(tok, in, out, condition) ? join(tok) : s;
}

// leaves tok properly tokenized
// returns true iff anything was replaced
static boolean jreplace(List<String> tok, String in, String out) {
  return jreplace(tok, in, out, false, true, null);
}

static boolean jreplace(List<String> tok, String in, String out, Object condition) {
  return jreplace(tok, in, out, false, true, condition);
}

static boolean jreplace(List<String> tok, String in, String out, boolean ignoreCase, boolean reTok, Object condition) {
  List<String> tokin = javaTok(in);
  jfind_preprocess(tokin);
  
  String[] toks = toStringArray(codeTokensOnly(tokin));

  boolean anyChange = false;
  for (int n = 0; n < 10000; n++) {
    int i = findCodeTokens(tok, 1, ignoreCase, toks, condition);
    if (i < 0)
      return anyChange;
    List<String> subList = tok.subList(i-1, i+l(tokin)-1); // N to N
    String expansion = jreplaceExpandRefs(out, subList);
    int end = i+l(tokin)-2;
    clearAllTokens(tok, i, end); // C to C
    tok.set(i, expansion);
    if (reTok) // would this ever be false??
      reTok(tok, i, end);
    anyChange = true;
  }
  throw fail("woot? 10000! " + quote(in) + " => " + quote(out));
}

static boolean jreplace_debug;
static String replaceVars_withDollarQ(String s, Map<String, String> map) {
  return empty(map) ? s : join(replaceVars_withDollarQ(javaTok(s), map));
}
  
static List<String> replaceVars_withDollarQ(List<String> tok, Map<String, String> map) {
  if (empty(map)) return tok;
  tok = cloneList(tok);
  for (int i = 1; i < l(tok); i +=2) {
    String t = tok.get(i);
    String value = map.get(t);
    if (value != null)
      tok.set(i, value);
    else if (t.startsWith("$_q_")) {
      value = map.get("$" + substring(t, 4));
      if (value != null) tok.set(i, quote(value));
    }
  }
  return tok;
}
static Iterator emptyIterator() {
  return Collections.emptyIterator();
}


static Map<String, String> parseDoubleLeftArrowMapCI_tlft(String s) {
  return parseDoubleLeftArrowMapCI(tlft(s));
}
static <A> TreeMap<String, A> ciMap() {
  return caseInsensitiveMap();
}
static ThreadLocal<Object> ai_context_tl = new ThreadLocal();

static Object ai_context() {
  return getThreadLocal(ai_context_tl);
}
static <A> Set<A> linkedHashSet_addWithAutoInit(Set<A> set, A a) {
  if (set == null) set = new LinkedHashSet();
  set.add(a);
  return set;
}
static List<String> splitAtDoubleArrow(String s) {
  return splitAtDoubleArrow(javaTok(s));
}

static List<String> splitAtDoubleArrow(List<String> tok) {
  List<String> l = new ArrayList();
  int i = 0;
  while (i < l(tok)) {
    int j = indexOfSubList(tok, ll("=", "", ">"), i);
    if (i >= l(tok)) break;
    if (j < 0) j = l(tok);
    l.add(trimJoin(tok.subList(i, j)));
    i = j+3;
  }
  return l;
}
static Exp nlLogic_parseExpression(String s) {
  if (s == null) return null;
  List<String> tok = javaTokPlusAllThreeBrackets(s);
  
  // a && b
  Pair<String, String> p = splitAtTokens_once(tok, ll("&", "", "&"));
  if (p != null)
    return new And(nlLogic_parseExpression(first(p)), nlLogic_parseExpression(second(p)));
    
  // !a
  if (eqGet(tok, 1, "!"))
    return new ExpNot(nlLogic_parseExpression(join(dropFirst(2, tok))));
    
  // function with options - a[bla, blubb](...)
  if (l(tok) == 7 && isIdentifier(tok.get(1))
    && isSquareBracketed(tok.get(3))
    && isRoundBracketed(tok.get(5)))
    return new Func(tok.get(1),
      tok_splitAtComma(deSquareBracket(tok.get(3))),
      nlLogic_parseExpression(deRoundBracket(tok.get(5))));
    
  // function - a(...)
  if (l(tok) == 5 && isIdentifier(tok.get(1)) && isRoundBracketed(tok.get(3))) {
    String name = tok.get(1), arg = deRoundBracket(tok.get(3));
    boolean parseInnerPart = false;
    return new Func(name, parseInnerPart ? nlLogic_parseExpression(arg)
      : new Sentence2(unquoteIfNormalQuoted(arg)));
  }
    
  // a = b
  List<String> l = splitAtTokens(tok, ll("="));
  if (l(l) == 2)
    return new Eq(nlLogic_parseExpression(first(l)), nlLogic_parseExpression(second(l)));
    
  // default
  return new Sentence2(l(tok) == 3 && isQuoted(tok.get(1)) ? unquote(second(tok)) : join(tok));
}
static <A> A last(List<A> l) {
  return empty(l) ? null : l.get(l.size()-1);
}

static char last(String s) {
  return empty(s) ? '#' : s.charAt(l(s)-1);
}

static int last(int[] a) {
  return l(a) != 0 ? a[l(a)-1] : 0;
}

static <A> A last(A[] a) {
  return l(a) != 0 ? a[l(a)-1] : null;
}

static <A> A last(Iterator<A> it) {
  A a = null;
  while  (it.hasNext()) { ping(); a = it.next(); }
  return a;
}
static String textMD5ToGlobalID(String s) {
  return md5ToGlobalID(md5(s));
}
static boolean endsWithIgnoreCase(String a, String b) {
  int la = l(a), lb = l(b);
  return la >= lb && regionMatchesIC(a, la-lb, b, 0, lb);
}


static boolean endsWithIgnoreCase(String a, String b, Matches m) {
  if (!endsWithIgnoreCase(a, b)) return false;
  m.m = new String[] { substring(a, 0, l(a)-l(b)) };
  return true;
}

static <A> HashSet<A> lithashset(A... items) {
  HashSet<A> set = new HashSet();
  for (A a : items) set.add(a);
  return set;
}
static <A, B> List<B> bOfPairs(Collection<Pair<A, B>> l) {
  return secondOfPairs(l);
}
// returns list(pair(id, rule))
static List<Pair<String, String>> mL_rules_addGlobalIDsIfAuthed(String listName) {
  List<String> l = splitAtEmptyLines(mL_raw(listName));
  List<Pair<String, String>> withIDs = combineWithGlobalIDs(l);
  boolean change = false;
  for (Pair<String, String> p : withIDs)
    if (empty(p.a)) { p.a = aGlobalID(); change = true; }
  if (change && isMechAuthed()) {
    String newText = joinWithEmptyLines(map(withIDs, new F1<Pair<String, String>, Object>() { Object get(Pair<String, String> p) { try { return 
      prependGlobalIDInNewLineAndIndent(p.a, p.b) ; } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "prependGlobalIDInNewLineAndIndent(p.a, p.b)"; }}));
    editMechList(listName, newText);
  }
  return withIDs;
}
static String beautifyStructure(String s) {
  return structure_addTokenMarkers(s);
}
static String struct_noStringSharing(Object o) {
  structure_Data d = new structure_Data();
  d.noStringSharing = true;
  return structure(o, d);
}
static List<String> tok_combineRoundCurlySquareBrackets_keep(List<String> tok) {
  List<String> l = new ArrayList();
  for (int i = 0; i < l(tok); i++) {
    String t = tok.get(i);
    if (odd(i) && eqOneOf(t, "{", "(", "[")) {
      int j = findEndOfBracketPart2(tok, i);
      l.add(joinSubList(tok, i, j));
      i = j-1;
    } else
      l.add(t);
  }
  return l;
}
static List<TelegramMsg> serverTelegramLog() {
  List<TelegramMsg> msgs = new ArrayList();
  for (String s : mL("Telegram Bot Log")) try {
    Map map = safeUnstructureMap(s);
    String type = (String) ( map.get("type"));
    String text = (String) ( map.get("text"));
    long originalDate = toInt(map.get("originalDate"))*1000L;
    long date = parseLocalDateWithMilliseconds_orKeepLong(map.get("date"));
    msgs.add(nu(TelegramMsg.class, "date", date, "originalDate", originalDate, "text", text,
      "firstName" , map.get("userFirstName"), 
      "lastName" , map.get("userLastName"), 
      "userID" , toInt(map.get("userID")),
      "sent" , eq(type, "sent"),
      "globalID" , map.get("globalID"),
      "chatID" , toLong(map.get("chatID"))));
  } catch (Throwable __e) { _handleException(__e); }
  return msgs;
}
// msg id -> rule id
static MultiMap<String, String> ai_tg_rulesFiredOnMessages_fullMap() {
  Matches m = new Matches();
  MultiMap<String, String> map = new MultiMap();
  for (String s : mL("Telegram Rule Fires"))
    if (matchStart("Rule * fired on message *", s, m))
      map.put(m.unq(1), m.unq(0));
  return map;
}

static <A, B> void addPair(Collection<Pair<A, B>> c, A a, B b) {
  if (c != null) c.add(pair(a, b));
}
static <A, B> List<A> firstOfPairs(Collection<Pair<A, B>> l) {
  List<A> out = new ArrayList();
  for (Pair<A, B> p : unnull(l)) out.add(firstOfPair(p));
  return out;
}
static List<String> mechList_tlftj(String name) {
  return tlftj(mechList_raw(name));
}
static String reverseString(String s) {
  return new StringBuilder(s).reverse().toString();
}
static <A> TreeSet<A> littreeset(A... items) {
  TreeSet<A> set = new TreeSet();
  for (A a : items) set.add(a);
  return set;
}
static <A> void treeSetClusters_merge(Map<A, Collection<A>> clusters, Collection<A> a, Collection<A> b) {
  for (A x : b) { a.add(x); clusters.put(x, a); }
}
static <A> Set<A> identityHashSet() {
  return Collections.newSetFromMap(new IdentityHashMap());
}
static <A> List<A> minus(Collection<A> a, Object... b) {
  Set set = asSet(b);
  List l = new ArrayList();
  for (Object s : unnull(a))
    if (!set.contains(s))
      l.add(s);
  return l;
}

static BigInteger minus(BigInteger a, BigInteger b) {
  return a.subtract(b);
}
static ListIterator emptyListIterator() {
  return Collections.emptyListIterator();
}
// This is made for NL parsing.
// It's javaTok extended with "..." token, "$n" and "#n" and
// special quotes (which are converted to normal ones).

static List<String> javaTokPlusPeriod(String s) {
  List<String> tok = new ArrayList<String>();
  int l = s.length();
  
  int i = 0;
  while (i < l) {
    int j = i;
    char c; String cc;
    
    // scan for whitespace
    while (j < l) {
      c = s.charAt(j);
      cc = s.substring(j, Math.min(j+2, l));
      if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
        ++j;
      else if (cc.equals("/*")) {
        do ++j; while (j < l && !s.substring(j, Math.min(j+2, l)).equals("*/"));
        j = Math.min(j+2, l);
      } else if (cc.equals("//")) {
        do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0);
      } else
        break;
    }
    
    tok.add(s.substring(i, j));
    i = j;
    if (i >= l) break;
    c = s.charAt(i);
    cc = s.substring(i, Math.min(i+2, l));

    // scan for non-whitespace
    if (c == (char) 0x201C || c == (char) 0x201D) c = '"'; // normalize quotes
    if (c == '\'' || c == '"') {
      char opener = c;
      ++j;
      while (j < l) {
        char _c = s.charAt(j);
        if (_c == (char) 0x201C || _c == (char) 0x201D) _c = '"'; // normalize quotes
        if (_c == opener) {
          ++j;
          break;
        } else if (s.charAt(j) == '\\' && j+1 < l)
          j += 2;
        else
          ++j;
      }
      if (j-1 >= i+1) {
        tok.add(opener + s.substring(i+1, j-1) + opener);
        i = j;
        continue;
      }
    } else if (Character.isJavaIdentifierStart(c))
      do ++j; while (j < l && (Character.isJavaIdentifierPart(s.charAt(j)) || s.charAt(j) == '\'')); // for things like "this one's"
    else if (Character.isDigit(c))
      do ++j; while (j < l && Character.isDigit(s.charAt(j)));
    else if (cc.equals("[[")) {
      do ++j; while (j+1 < l && !s.substring(j, j+2).equals("]]"));
      j = Math.min(j+2, l);
    } else if (cc.equals("[=") && i+2 < l && s.charAt(i+2) == '[') {
      do ++j; while (j+2 < l && !s.substring(j, j+3).equals("]=]"));
      j = Math.min(j+3, l);
    } else if (s.substring(j, Math.min(j+3, l)).equals("..."))
      j += 3;
    else if (c == '$' || c == '#')
      do ++j; while (j < l && Character.isDigit(s.charAt(j)));
    else
      ++j;

    tok.add(s.substring(i, j));
    i = j;
  }
  
  if ((tok.size() % 2) == 0) tok.add("");
  return tok;
}

static boolean isNonNegativeInteger(String s) {
  return s != null && Pattern.matches("\\d+", s);
}
static TimeZone localTimeZone() {
  return getTimeZone(standardTimeZone());
  // TimeZone.getDefault()?
}
static <A> A assertEquals(Object x, A y) {
  return assertEquals(null, x, y);
}

static <A> A assertEquals(String msg, Object x, A y) {
  if (assertVerbose()) return assertEqualsVerbose(msg, x, y);
  if (!(x == null ? y == null : x.equals(y)))
    throw fail((msg != null ? msg + ": " : "") + y + " != " + x);
  return y;
}
static void quoteToPrintWriter(String s, PrintWriter out) {
  if (s == null) { out.print("null"); return; }
  out.print('"');
  int l = s.length();
  for (int i = 0; i < l; i++) {
    char c = s.charAt(i);
    if (c == '\\' || c == '"') {
      out.print('\\'); out.print(c);
    } else if (c == '\r')
      out.print("\\r");
    else if (c == '\n')
      out.print("\\n");
    else if (c == '\0')
      out.print("\\0");
    else
      out.print(c);
  }
  out.print('"');
}
static String quoteCharacter(char c) {
  if (c == '\'') return "'\\''";
  if (c == '\\') return "'\\\\'";
  if (c == '\r') return "'\\r'";
  if (c == '\n') return "'\\n'";
  if (c == '\t') return "'\\t'";
  return "'" + c + "'";
}

static String boolArrayToHex(boolean[] a) {
  return bytesToHex(boolArrayToBytes(a));
}
static String shortDynamicClassName(Object o) {
 if (o instanceof DynamicObject && ((DynamicObject) o).className != null)
    return ((DynamicObject) o).className;
  return shortClassName(o);
}
static int stdcompare(Number a, Number b) {
  return cmp(a, b);
}

static int stdcompare(String a, String b) {
  return cmp(a, b);
}

static int stdcompare(long a, long b) {
  return a < b ? -1 : a > b ? 1 : 0;
}

static int stdcompare(Object a, Object b) {
  return cmp(a, b);
}

static Map<Class, Field[]> getDeclaredFields_cache = newDangerousWeakHashMap();

static Field[] getDeclaredFields_cached(Class c) {
  Field[] fields;
  synchronized(getDeclaredFields_cache) {
    fields = getDeclaredFields_cache.get(c);
    if (fields == null) {
      getDeclaredFields_cache.put(c, fields = c.getDeclaredFields());
      for (Field f : fields)
        f.setAccessible(true);
    }
  }
  return fields;
}
static String botAppendToMechList_mode(String name, String text, String mode) {
  if (empty(name) || empty(text)) return "";
  
  
  
  String result = postPageSilently("http://butter.botcompany.de:8080/mech/raw/bot-list-append", arrayPlus(muricaCredentials(), "name", name, "text", text, "mode", mode));
  if (!swic(result, "No change")) {
    if (eq(mechMode().readMode, "localCopies")) {
      print("Appending to local list copy: " + name);
      appendToLocalMechListCopy(name, text);
    }
    mech_notifyBusOfChange(name);
    mechList_clearCache(name);
  }
  //infoBox(name + ": " + result);
  return result;
}

static String botAppendToMechList_mode(String name, Collection<String> lines, String mode) {
  if (empty(lines)) return "";
  return botAppendToMechList_mode(name, lines(lines), mode);
}
static void printStructure(String prefix, Object o) {
  if (endsWithLetter(prefix)) prefix += ": ";
  print(prefix + structureForUser(o));
}

static void printStructure(Object o) {
  print(structureForUser(o));
}

static Set<String> ai_yesNoQuestionWord_1_list = litciset("am", "are", "is", "can", "does", "do", "did", "would", "might", "will", "must", "should", "has", "have");

static boolean ai_yesNoQuestionWord_1(String s) {
  return containsNotNull(ai_yesNoQuestionWord_1_list, s);
}
static String firstWord(String s) {
  return firstJavaToken(s);
}
static TreeSet<String> litciset(String... items) {
  TreeSet<String> set = caseInsensitiveSet();
  for (String a : items) set.add(a);
  return set;
}


static boolean containsNotNull(Collection c, Object o) {
  return o != null && contains(c, o);
}

static boolean containsNotNull(Object[] x, Object o) {
  return o != null && contains(x, o);
}
static String xltrim(String s) {
  int i = 0;
  while (i < s.length() && contains(" \t\r\n", s.charAt(i)))
    ++i;
  return substr(s, i);
}
// pred: char -> bool
static String takeCharsWhile(String s, Object pred) {
  int i = 0;
  while (i < l(s) && isTrue(callF(pred, s.charAt(i)))) ++i;
  return substring(s, 0, i);
}
static <A> boolean containsOneOf(Collection<A> l, A... x) {
  for (A a : unnull(l))
    if (eqOneOf(a, x))
      return true;
  return false;
}

static <A> boolean containsOneOf(Collection<A> l, Set<A> set) {
  if (set == null) return false;
  for (A a : unnull(l))
    if (set.contains(a))
      return true;
  return false;
}

static boolean containsOneOf(String s, String... x) {
  for (String o : x)
    if (contains(s, o)) return true;
  return false;
}
static String botAppendToMechList_now(String name, String text) {
  return botAppendToMechList_mode(name, text, "noUniq");
}
static boolean emptyAfterTrim(String s) {
  return empty(trim(s));
}
static ExpiringMap2<String, String> mechList_raw_cache = new ExpiringMap2(10000).setMap(ciMap());
static Lock mechList_raw_lock = lock();

// timeout for loading
static int mechList_raw_timeout = 60000;

static String mechList_raw(String name) { try {
  Lock __1483 = mechList_raw_lock; lock(__1483); try {
  
  // get from cache
  String src = mechList_raw_cache.get(name);
  if (src != null) return src;
  
  // load & put in cache - TODO: make postPage honor this
   AutoCloseable __1485 = tempSetThreadLocal(loadPage_forcedTimeout_byThread, mechList_raw_timeout); try {
  src = mechList_raw_fresh(name);
  if (src != null)
    mechList_raw_cache.put(name, src);
  return src;
} finally { _close(__1485); }} finally { unlock(__1483); } } catch (Exception __e) { throw rethrow(__e); } }

static VF2<String, Object> mechList_raw_listener = new VF2<String, Object>() { public void get(String msg, Object arg) { try { 
  if (eq(msg, "mechChange") && arg instanceof String) {
    print("Got change notification: " + arg);
    mechList_raw_cache.remove((String) arg);
  }
 } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "if (eq(msg, 'mechChange) && arg instanceof S) {\r\n    print(\"Got change notifi..."; }};

static void _onLoad_mechList_raw() { add(vm_busListeners_live(), mechList_raw_listener); }
static void cleanMeUp_mechList_raw() { remove(vm_busListeners_live(), mechList_raw_listener); }
static boolean noJavaTokens(String s) {
  return numberOfJavaTokens(s) == 0;
}
// f: A -> Comparable
static <A> List<A> sortByCalculatedField(Iterable<A> c, final Object f) {
  List<A> l = cloneList(c);
  sort(l, new Comparator<A>() {
    public int compare(A a, A b) {
      return stdcompare(callF(f, a), callF(f, b));
    }
  });
  return l;
}
static void nlLogic_setScriptID(List<IfThen> rules, final String scriptID) {
  final Set<String> ids = collectAsHashSet(rules, "globalID");
  for (IfThen rule : rules) {
    rule.globalID = scriptID + rule.globalID;
    mapIntoSameCollection(rule.options, new F1<String, String>() { String get(String s) { try { 
      Matches m = new Matches();
      for (String pat : splitAtVerticalBar("trumps *|conflicts with"))
        if (match(pat, s, m))
          return format(pat, scriptID + m.unq(0));
      return s;
     } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "new Matches m;\r\n      for (S pat : splitAtVerticalBar(\"trumps *|conflicts wit..."; }});
    nlLogic_visit(rule, new VF1<Object>() { public void get(Object o) { try { 
      String name;
      if (o instanceof Func) {
        if (eq(((Func) o).name, "followingUpOn")) {
          List<String> tok = javaTok(nlLogic_text(((Func) o).arg));
          if (ids.contains(_get(tok, 1))) {
            tok.set(1, scriptID + tok.get(1));
            ((Func) o).arg = new Sentence2(join(tok));
          }
        }
      }
     } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "S name;\r\n      if (o cast Func) {\r\n        if (eq(o.name, 'followingUpOn)) {\r..."; }});
  }
}
static void nlLogic_visit(IfThen rule, VF1<Object> visitor) {
  if (rule == null) return;
  callF(visitor, rule);
  nlLogic_visit(rule.in, visitor);
  nlLogic_visit(rule.out, visitor);
}

static void nlLogic_visit(Exp e, VF1<Object> visitor) {
  if (e == null) return;
  callF(visitor, e);
  if (e instanceof And) {
    nlLogic_visit(((And) e).a, visitor);
    nlLogic_visit(((And) e).b, visitor);
  } else if (e instanceof ExpNot)
    nlLogic_visit(((ExpNot) e).a, visitor);
  else if (e instanceof Func)
    nlLogic_visit(((Func) e).arg, visitor);
}

static Exp nlLogic_and(Exp a, Exp b) {
  return a == null ? b : b == null ? a : new And(a, b);
}

static Exp nlLogic_and(List<Exp> l) {
  if (empty(l)) return null;
  Exp e = last(l);
  for (int i = l(l)-2; i >= 0; i--)
    e = new And(l.get(i), e);
  return e;
}
static <A> A restructure(A a) {
  return (A) unstructure(structure(a));
}
// returns from C to C
static T3<String, String, String> jextractWithRests(String pat, String s) {
  return jextractWithRests(pat, javaTok(s));
}

static T3<String, String, String> jextractWithRests(String pat, List<String> tok) {
  List<String> tokpat = javaTok(pat);
  jfind_preprocess(tokpat);
  int i = jfind(tok, tokpat);
  if (i < 0) return null;
  int j = i + l(tokpat) - 2;
  return t3(joinSubList(tok, 0, i), joinSubList(tok, i, j), joinSubList(tok, j));
}

static boolean jmatch(String pat, String s) {
  return jmatch(pat, s, null);
}

static boolean jmatch(String pat, String s, Matches matches) {
  if (s == null) return false;
  return jmatch(pat, javaTok(s), matches);
}

static boolean jmatch(String pat, List<String> toks) {
  return jmatch(pat, toks, null);
}

static boolean jmatch(String pat, List<String> toks, Matches matches) {
  List<String> tokpat = javaTok(pat);
  String[] m = match2(tokpat, toks);
  //print(structure(tokpat) + " on " + structure(toks) + " => " + structure(m));
  if (m == null)
    return false;
  else {
    if (matches != null) matches.m = m;
    return true;
  }
}
static String ai_expandPatternForAAndAn(String s) {
  List<String> tok = javaTok(s);
  if (!cicOneOf(tok, "a", "an") || contains(tok, "|")) return null; // TODO: allow |
  List<Integer> indices = indicesOfAnyIC(tok, "a", "an");
  int n = l(indices);
  if (n == 0) return null;
  List<String> out = new ArrayList();
  for (List<String> l : allCombinations(rep(n, ll("a", "an")))) {
    for (int i = 0; i < l(l); i++)
      tok.set(indices.get(i), l.get(i));
    out.add(join(tok));
  }
  return join("|", out);
}
static Map<String, String> parseDoubleArrowMapCI(List<String> l) {
  return parseDoubleArrowMap((Map) ciMap(), l);
}
static <B> TreeMap<String, B> asCIMap(Map<String, B> map) {
  return asCaseInsensitiveMap(map);
}
static String[] drop(int n, String[] a) {
  n = Math.min(n, a.length);
  String[] b = new String[a.length-n];
  System.arraycopy(a, n, b, 0, b.length);
  return b;
}

static Object[] drop(int n, Object[] a) {
  n = Math.min(n, a.length);
  Object[] b = new Object[a.length-n];
  System.arraycopy(a, n, b, 0, b.length);
  return b;
}
static <A> ArrayList<A> toList(A[] a) { return asList(a); }
static ArrayList<Integer> toList(int[] a) { return asList(a); }
static <A> ArrayList<A> toList(Set<A> s) { return asList(s); }
static <A> ArrayList<A> toList(Iterable<A> s) { return asList(s); }
static boolean isDigit(char c) {
  return Character.isDigit(c);
}
static char charAt(String s, int i) {
  return s != null && i >= 0 && i < s.length() ? s.charAt(i) : '\0';
}
static Object getMC(String field) {
  return get(mc(), field);
}
static RuntimeException quickFail() { throw new QuickException("fail"); }
static RuntimeException quickFail(Throwable e) { throw asQuickException(e); }
static RuntimeException quickFail(Object msg) { throw new QuickException(String.valueOf(msg)); }
static RuntimeException quickFail(String msg) { throw new QuickException(unnull(msg)); }
static RuntimeException quickFail(String msg, Throwable innerException) { throw new QuickException(msg, innerException); }

// map: index of opening bracket -> index of closing bracket
static Map<Integer, Integer> getBracketMap(List tok) {
  return getBracketMap(tok, getBracketMap_opening, getBracketMap_closing);
}

static Map<Integer, Integer> getBracketMap(List tok, Collection<String> opening, Collection<String> closing) {
  TreeMap<Integer,Integer> map = new TreeMap();
  List<Integer> stack = new ArrayList();
  for (int i = 1; i < l(tok); i+= 2) {
    if (opening.contains(tok.get(i)))
      stack.add(i);
    else if (closing.contains(tok.get(i))) {
      if (!empty(stack))
        map.put(liftLast(stack), i);
    }
  }
  return map;
}

static Set<String> getBracketMap_opening = lithashset("{", "(");
static Set<String> getBracketMap_closing = lithashset("}", ")");
static <A> boolean eqGetOneOf(List<A> l, int i, A... options) {
  return eqOneOf(get(l, i), options);
}
static String trimJoinSubList(List<String> l, int i, int j) {
  return trim(join(subList(l, i, j)));
}

static String trimJoinSubList(List<String> l, int i) {
  return trim(join(subList(l, i)));
}
static Object callAndMake_orDirect(String functionName, List args) {
  return callAndMake_orDirect(functionName, toObjectArray(args));
}

static Object callAndMake_orDirect(String functionName, Object... args) {
  if (functionName == null) return null;
  if (hasMethod(mc(), functionName, args))
    return callMC(functionName, args);
  else
    return makeAndCall_cached(functionName, args);
}
static boolean preferCached = false;
static boolean loadSnippet_debug = false;
static ThreadLocal<Boolean> loadSnippet_silent = new ThreadLocal();
static int loadSnippet_timeout = 30000;


static String loadSnippet(Snippet s) {
  return loadSnippet(s.id);
}


static String loadSnippet(String snippetID) { try {
  if (snippetID == null) return null;
  return loadSnippet(parseSnippetID(snippetID), preferCached);
} catch (Exception __e) { throw rethrow(__e); } }

static String loadSnippet(String snippetID, boolean preferCached) throws IOException {
  return loadSnippet(parseSnippetID(snippetID), preferCached);
}

public static String loadSnippet(long snippetID) { try {
  return loadSnippet(snippetID, preferCached);
} catch (Exception __e) { throw rethrow(__e); } }

public static String loadSnippet(long snippetID, boolean preferCached) throws IOException {
  String text;
  
  // boss bot disabled for now for shorter transpilations
  
  /*text = getSnippetFromBossBot(snippetID);
  if (text != null) return text;*/
  
  initSnippetCache();
  text = DiskSnippetCache_get(snippetID);
  
  if (preferCached && text != null)
    return text;
  
  try {
    if (loadSnippet_debug && text != null) System.err.println("md5: " + md5(text));
    String url = tb_mainServer() + "/getraw.php?id=" + snippetID + "&utf8=1";
    if (nempty(text)) url += "&md5=" + md5(text);
    url += standardCredentials();
    
    String text2 = loadSnippet_loadFromServer(url);
    
    boolean same = eq(text2, "==*#*==");
    if (loadSnippet_debug) print("loadSnippet: same=" + same);
    if (!same) text = text2;
  } catch (RuntimeException e) {
    e.printStackTrace();
    throw new IOException("Snippet #" + snippetID + " not found or not public");
  }

  try {
    initSnippetCache();
    DiskSnippetCache_put(snippetID, text);
  } catch (IOException e) {
    System.err.println("Minor warning: Couldn't save snippet to cache ("  + DiskSnippetCache_getDir() + ")");
  }

  return text;
}

static File DiskSnippetCache_dir;

public static void initDiskSnippetCache(File dir) {
  DiskSnippetCache_dir = dir;
  dir.mkdirs();
}

public static synchronized String DiskSnippetCache_get(long snippetID) throws IOException {
  return loadTextFile(DiskSnippetCache_getFile(snippetID).getPath(), null);
}

private static File DiskSnippetCache_getFile(long snippetID) {
  return new File(DiskSnippetCache_dir, "" + snippetID);
}

public static synchronized void DiskSnippetCache_put(long snippetID, String snippet) throws IOException {
  saveTextFile(DiskSnippetCache_getFile(snippetID).getPath(), snippet);
}

public static File DiskSnippetCache_getDir() {
  return DiskSnippetCache_dir;
}

public static void initSnippetCache() {
  if (DiskSnippetCache_dir == null)
    initDiskSnippetCache(getGlobalCache());
}

static String loadSnippet_loadFromServer(String url) {
  Integer oldTimeout = setThreadLocal(loadPage_forcedTimeout_byThread, loadSnippet_timeout);
  try {
    return isTrue(loadSnippet_silent.get()) ? loadPageSilently(url) : loadPage(url);
  } finally {
    loadPage_forcedTimeout_byThread.set(oldTimeout);
  }
}

static <A> int indexOfAny(List<A> l, int i, A... x) {
  while (i < l(l))
    if (eqOneOf(l.get(i), x)) return i; else ++i;
  return -1;
}

static <A> int indexOfAny(List<A> l, Collection<A> x) {
  return indexOfAny(l, 0, x);
}

static <A> int indexOfAny(List<A> l, int i, Collection<A> x) {
  if (nempty(x))
    while (i < l(l))
      if (x.contains(l.get(i))) return i; else ++i;
  return -1;
}

static int indexOfAny(String s, int i, String chars) {
  for (; i < l(s); i++)
    if (chars.indexOf(s.charAt(i)) >= 0)
      return i;
  return -1;
}
static float parseFloat(String s) {
  return Float.parseFloat(s);
}
static <A> List<A> takeFirst(List<A> l, int n) {
  return l(l) <= n ? l : newSubList(l, 0, n);
}

static <A> List<A> takeFirst(int n, List<A> l) {
  return takeFirst(l, n);
}

static String takeFirst(int n, String s) { return substring(s, 0, n); }
static String takeFirst(String s, int n) { return substring(s, 0, n); }

static <A> List<A> takeFirst(int n, Iterable<A> i) {
  List l = new ArrayList();
  Iterator<A> it = i.iterator();
  for (int _repeat_1533 = 0; _repeat_1533 < n; _repeat_1533++)  { if (it.hasNext()) l.add(it.next()); else break; }
  return l;
}

static <A, B> B mapPutIfChange2(Map<A, B> map, A key, B value) {
  if (map != null && key != null && value != null && neq(map.get(key), value))
    return map.put(key, value);
  return null;
}
static String n(long l, String name) {
  return l + " " + trim(l == 1 ? singular(name) : getPlural(name));
}

static String n(Collection l, String name) {
  return n(l(l), name);
}

static String n(Map m, String name) {
  return n(l(m), name);
}

static String n(Object[] a, String name) {
  return n(l(a), name);
}


static <A> void removeAllButFirst(List<A> l) {
  if (l(l) > 1) removeSubList(l, 1);
}
static String hijackPrint(Object r) {
  final StringBuilder buf = new StringBuilder();
  Object old = interceptPrintInThisThread(new F1<String, Boolean>() {
    Boolean get(String s) {
      buf.append(s);
      return false;
    }
  });
  try {
    callF(r);
    return str(buf);
  } finally {
    interceptPrintInThisThread(old);
  }
}
static void ai_printParseTree(PTElement e) {
  ai_printParseTree(e, "");
}

static void ai_printParseTree(PTElement e, String indent) {
  if (e == null) { print("No parse tree"); return; }
  print(indent + e);
  indent += "  ";
  for (PTElement child : unnull(e.children))
    ai_printParseTree(child, indent);
}

static <A> List<A> uniquify(Collection<A> l) {
  return uniquifyList(l);
}
static List<String> words(String s) {
  return codeTokens(dropPunctuation(javaTok(s)));
}
static String ai_language() {
  return or2(ai_setLanguage_value.get(), "English");
}


static TimedCache<Map<String, Map<String, Set<String>>>> ai_wordCategoriesWithElements_cache = new TimedCache("ciMap", 10.0); // language -> category -> words

static Map<String, Set<String>> ai_wordCategoriesWithElements() {
  Map<String, Map<String, Set<String>>> languages = ai_wordCategoriesWithElements_cache.get();
  Map<String, Set<String>> map = languages.get(ai_language());
  if (map == null)
    languages.put(ai_language(), map = ai_wordCategoriesWithElements_make());
  return map;
}

static Map<String, Set<String>> ai_wordCategoriesWithElements_make() {
  final Map<String, Set<String>> wordCategories = ciMap();
  for (String cat : mL("All " + ai_language() + " word categories"))
    putIfValueNempty(wordCategories, cat, combinedMechSetsCI(ai_mechListNamesForAngleBracketCategory(cat)));
  return wordCategories;
}



static HashMap lithashmap(Object... x) {
  HashMap map = new HashMap();
  litmap_impl(map, x);
  return map;
}
static List<String> withoutQuoted(Iterable<String> l) {
  if (l == null) return null;
  List<String> l2 = new ArrayList();
  for (String s : l)
    if (!isQuoted(s))
      l2.add(s);
  return l2;
}
static PTElement ai_completeParseTree(AI_BottomUpParser1 parser, PTElement root) {
  if (root == null) return null;
  List<WeightedProduction> productions = ai_buParser_parseWeightedProductions();
  ai_parseTree_makeWeights(parser, productions, root);
  ai_parseTree_makeTotalWeights(root, 1);
  return root;
}
static CloseableIterableIterator<String> linesFromFile(File f) { try {
  if (!f.exists()) return emptyCloseableIterableIterator();
  if (ewic(f.getName(), ".gz"))
    return linesFromReader(utf8bufferedReader(newGZIPInputStream(f)));
  return linesFromReader(utf8bufferedReader(f));
} catch (Exception __e) { throw rethrow(__e); } }
static void set(Object o, String field, Object value) {
  if (o instanceof Class) set((Class) o, field, value);
  else try {
    Field f = set_findField(o.getClass(), field);
    f.setAccessible(true);
    smartSet(f, o, value);
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
}

static void set(Class c, String field, Object value) {
  try {
    Field f = set_findStaticField(c, field);
    f.setAccessible(true);
    smartSet(f, null, value);
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
}
  
static Field set_findStaticField(Class<?> c, String field) {
  Class _c = c;
  do {
    for (Field f : _c.getDeclaredFields())
      if (f.getName().equals(field) && (f.getModifiers() & java.lang.reflect.Modifier.STATIC) != 0)
        return f;
    _c = _c.getSuperclass();
  } while (_c != null);
  throw new RuntimeException("Static field '" + field + "' not found in " + c.getName());
}

static Field set_findField(Class<?> c, String field) {
  Class _c = c;
  do {
    for (Field f : _c.getDeclaredFields())
      if (f.getName().equals(field))
        return f;
    _c = _c.getSuperclass();
  } while (_c != null);
  throw new RuntimeException("Field '" + field + "' not found in " + c.getName());
}
static List<IfThen> nlLogic_filterByRHSConstructs(List<IfThen> rules, String... constructs) {
  return nlLogic_filterByRHSConstructs(rules, asHashSet(constructs));
}

static List<IfThen> nlLogic_filterByRHSConstructs(List<IfThen> rules, final Set<String> _constructs) {
  final Set<String> constructs = setPlus(_constructs, "newID");
  return filter(rules , new F1<IfThen, Object>() { Object get(IfThen r) { try { return  isSuperSet(constructs, nlLogic_constructNames(r.out)) ; } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "isSuperSet(constructs, nlLogic_constructNames(r.out))"; }});
}
static List dm_modulesOfType(String type) {
  List<String> l = splitAtSlash(type);
  if (l(l) == 2)
    return filterByProgramIDAndShortClassName(dm_listModules(), first(l), second(l));
  else
    return filterByShortClassName(dm_listModules(), type);
}
static Object evalJava_main(String main) {
  return callCalc(evalJava_prep2(main));
}
static String evalJava_prep(String code) {
  return evalJava_prep(code, "calc");
}

static String evalJava_prep(String code, String mainName) {
  return evalJava_prep(code, mainName, "");
}

static ThreadLocal<String> evalJava_prep_args = new ThreadLocal();

static String evalJava_prep(String code, String mainName, String preCode) {
  List<String> tok = javaTok(trim(code));
  
  if (tok_shouldAddReturn(tok) && eqGet(tok, 3, "(") && isIdentifier(get(tok, 1)) && isKnownVoidFunction(get(tok, 1)))
    tokAppend_reTok(tok, l(tok)-2, ";");
    
  code = tok_addReturn(tok);
  String returnType = containsReturnWithArgument(code) ? "O" : "void";
  String main =
    "static " + returnType + " " + mainName + "(" + unnull(evalJava_prep_args.get()) + ") throws Exception { " 
      + preCode + code + "\n" + "}";
  return main;
}
static List<String> mechList_opt_tlft(String name) {
  return tlft(mechList_opt_raw(name));
}
static List map_pcall_notNull(Iterable l, Object f) {
  return map_pcall_notNull(f, l);
}

static List map_pcall_notNull(Object f, Iterable l) {
  List x = new ArrayList();
  for (Object o : unnull(l)) try { addIfNotNull(x, callF(f, o)); } catch (Throwable __e) { _handleException(__e); }
  return x;
}
static <T> void sort(T[] a, Comparator<? super T> c) {
  Arrays.sort(a, c);
}

static <T> void sort(T[] a) {
  Arrays.sort(a);
}

static <T> void sort(List<T> a, Comparator<? super T> c) {
  Collections.sort(a, c);
}

static void sort(List a) {
  Collections.sort(a);
}
static <A> Comparator<A> descFieldComparator(final String field) {
  return new Comparator<A>() {
    public int compare(A a, A b) {
      return cmp(getOpt(b, field), getOpt(a, field));
    }
  };
}
// o is either a map already (string->object) or an arbitrary object,
// in which case its fields are converted into a map.
static Map<String, Object> objectToMap(Object o) { try {
  if (o instanceof Map) return (Map) o;
  
  TreeMap<String,Object> map = new TreeMap();
  Class c = o.getClass();
  while (c != Object.class) {
    Field[] fields = c.getDeclaredFields();
    for (final Field field : fields) {
      if ((field.getModifiers() & Modifier.STATIC) != 0)
        continue;
      field.setAccessible(true);
      final Object value = field.get(o);
      if (value != null)
        map.put(field.getName(), value);
    }
    c = c.getSuperclass();
  }
  
  // XXX NEW - hopefully this doesn't break anything
  if (o instanceof DynamicObject)
    map.putAll(((DynamicObject) o).fieldValues);

  return map;
} catch (Exception __e) { throw rethrow(__e); } }

// same for a collection (convert each element)
static List<Map<String, Object>> objectToMap(Collection l) {
  List x = new ArrayList();
  for (Object o : l)
    x.add(objectToMap(o));
  return x;
}
static String[] asStringArray(Collection<String> c) {
  return toStringArray(c);
}

static String[] asStringArray(Object o) {
  return toStringArray(o);
}

static Pair<List<String>, Object> prepareEvalWithDollarVars_assertAllVarsSet(String code, Map<String, String> values) {
  Set<String> vars = tok_dollarVarTokens(javaTok(code));
  for (String v : vars)
    if (!containsKey(values, v))
      throw fail("No value for " + v);
  return prepareEvalWithDollarVars(code, vars);
}
// works on lists and strings and null

static int indexOfIgnoreCase(List<String> a, String b) {
  return indexOfIgnoreCase(a, b, 0);
}

static int indexOfIgnoreCase(List<String> a, String b, int i) {
  int n = l(a);
  for (; i < n; i++)
    if (eqic(a.get(i), b)) return i;
  return -1;
}

static int indexOfIgnoreCase(String a, String b) {
  return indexOfIgnoreCase_manual(a, b);
  /*Matcher m = Pattern.compile(b, Pattern.CASE_INSENSITIVE + Pattern.LITERAL).matcher(a);
  if (m.find()) return m.start(); else ret -1;*/
}
static List<String> javaTokPlusBrackets(String s) {
  return tok_combineRoundOrCurlyBrackets_keep(javaTok(s));
}
static <A> Set<A> synchroSet() {
  return synchroHashSet();
}

static <A> Set<A> synchroSet(Set<A> set) {
  return Collections.synchronizedSet(set);
}
static <A> Set<A> newWeakHashSet() {
  return synchroWeakHashSet();
}
static List beforeDelegatingToThread_operations = synchroList();

static void beforeDelegatingToThread(Thread t) {
  for (Object op : cloneList(beforeDelegatingToThread_operations))
    pcallF(op, t);
}

static void beforeDelegatingToThread_do(Object f) {
  setAdd(beforeDelegatingToThread_operations, f);
}
static void cancelAndInterruptThread(Thread t) {
  if (t == null) return;
  cancelThread(t);
  t.interrupt();
}
static <A, B> Either<A, B> either2(B b) {
  return new Either(2, b);
}
static <A, B> Either<A, B> either1(A a) {
  return new Either(1, a);
}
static List afterDelegatingToThread_operations = synchroList();

static void afterDelegatingToThread(Thread t) {
  for (Object op : cloneList(afterDelegatingToThread_operations))
    pcallF(op, t);
}

static void afterDelegatingToThread_do(Object f) {
  setAdd(afterDelegatingToThread_operations, f);
}
static PTElement ai_parseToTreeWithCurlyPhrases(String input) {
  List<String> tok = javaTokPlusBrackets(input);
  tok = dropPunctuation(tok);
  //pnlStruct(tok);

  // Pre-parse curly fragments & guess categories for unknown words
  Map<String, Map<String, Double>> guessMap = new HashMap();
  AI_BottomUpParser1 parser = new AI_BottomUpParser1();
  parser.wordsToCategories = new DerivedMap(ai_wordToCategories());
  for (int i = 1; i < l(tok); i += 2) {
    String t = tok.get(i);
    if (isCurlyBraced(t)) {
      tok.set(i, t = uncurly(t));
      PTElement tree = ai_parseToTreeWithGuessing(t);
      if (tree != null) {
        //preparsed.put(i, tree);
        //print(className(tree));
        //print(ai_renderCulledParseTree(tree));
        
        if (!(tree instanceof ChooseCategory))
          print("Weird: " + className(tree));
        else {
          for (HaveCategory c : (List<HaveCategory>) (List) tree.children)
            subHashMap(guessMap, t).put(c.category, c.totalWeight);
        }
      } else {
        //print("No parse for " + t);
      }
    } else if (ai_isUncategorizedWord(t))
      guessMap.put(t, ai_guessCategoriesForUnknownWord_weighted(t));
  }
  
   AutoCloseable __1603 = tempSetThreadLocal(ai_getWordToCategoryWeight_map, guessMap); try {
  for (String word : keys(guessMap))
    parser.wordsToCategories.put(word, keys(guessMap.get(word)));
  return ai_parseBottomUpToTree(parser, tok);
} finally { _close(__1603); }}
static int findCodeTokens(List<String> tok, String... tokens) {
  return findCodeTokens(tok, 1, false, tokens);
}

static int findCodeTokens(List<String> tok, boolean ignoreCase, String... tokens) {
  return findCodeTokens(tok, 1, ignoreCase, tokens);
}

static int findCodeTokens(List<String> tok, int startIdx, boolean ignoreCase, String... tokens) {
  return findCodeTokens(tok, startIdx, ignoreCase, tokens, null);
}

static List<String> findCodeTokens_specials = litlist("*", "<quoted>", "<id>", "<int>", "\\*");
static boolean findCodeTokens_debug;
static int findCodeTokens_indexed, findCodeTokens_unindexed;
static int findCodeTokens_bails, findCodeTokens_nonbails;

static int findCodeTokens(List<String> tok, int startIdx, boolean ignoreCase, String[] tokens, Object condition) {
  if (findCodeTokens_debug) {
    if (eq(getClassName(tok), "main$IndexedList2"))
      findCodeTokens_indexed++;
    else
      findCodeTokens_unindexed++;
  }
  // bail out early if first token not found (works great with IndexedList)
  if (!findCodeTokens_specials.contains(tokens[0])
    && !tok.contains(tokens[0] /*, startIdx << no signature in List for this, unfortunately */)) {
      ++findCodeTokens_bails;
      return -1;
    }
  ++findCodeTokens_nonbails;
  
  outer: for (int i = startIdx | 1; i+tokens.length*2-2 < tok.size(); i += 2) {
    for (int j = 0; j < tokens.length; j++) {
      String p = tokens[j], t = tok.get(i+j*2);
      boolean match;
      if (eq(p, "*")) match = true;
      else if (eq(p, "<quoted>")) match = isQuoted(t);
      else if (eq(p, "<id>")) match = isIdentifier(t);
      else if (eq(p, "<int>")) match = isInteger(t);
      else if (eq(p, "\\*")) match = eq("*", t);
      else match = ignoreCase ? eqic(p, t) : eq(p, t);
      
      if (!match)
        continue outer;
    }
    
    if (condition == null || checkTokCondition(condition, tok, i-1)) // pass N index
      return i;
  }
  return -1;
}
  static List<String> codeTokensOnly(List<String> tok) {
    List<String> l = new ArrayList();
    for (int i = 1; i < tok.size(); i += 2)
      l.add(tok.get(i));
    return l;
  }
static <A> List<A> replaceSublist(List<A> l, List<A> x, List<A> y) {
  if (x == null) return l;
  
  int i = 0;
  while (true) {
    i = indexOfSubList(l, x, i);
    if (i < 0) break;
    
    // It's inefficient :D
    for (int j = 0; j < l(x); j++) l.remove(i);
    l.addAll(i, y);
    i += l(y);
  }
  return l;
}

static <A> List<A> replaceSublist(List<A> l, int fromIndex, int toIndex, List<A> y) {
  // TODO: optimize more
  removeSubList(l, fromIndex, toIndex);
  l.addAll(fromIndex, y);
  return l;
}
static List<String> dropPunctuation_keep = litlist("*", "<", ">");

static List<String> dropPunctuation(List<String> tok) {
  tok = new ArrayList<String>(tok);
  for (int i = 1; i < tok.size(); i += 2) {
    String t = tok.get(i);
    if (t.length() == 1 && !Character.isLetter(t.charAt(0)) && !Character.isDigit(t.charAt(0)) && !dropPunctuation_keep.contains(t)) {
      tok.set(i-1, tok.get(i-1) + tok.get(i+1));
      tok.remove(i);
      tok.remove(i);
      i -= 2;
    }
  }
  return tok;
}

static String dropPunctuation(String s) {
  return join(dropPunctuation(nlTok(s)));
}
static Object safeUnstructure(String s) {
  return unstructure(s, true);
}
static <A> boolean canPutStrictlyIC(Map<A, String> map, A key, String value) {
  if (!(map != null && key != null && value != null)) return true;
  String old = map.get(key);
  return old == null || eqic(old, value);
}
static String shortenClassName(String name) {
  if (name == null) return null;
  int i = lastIndexOf(name, "$");
  if (i < 0) i = lastIndexOf(name, ".");
  return i < 0 ? name : substring(name, i+1);
}
static <A> int indexOfSubList(List<A> x, List<A> y) {
  return indexOfSubList(x, y, 0);
}

static <A> int indexOfSubList(List<A> x, List<A> y, int i) {
  outer: for (; i+l(y) <= l(x); i++) {
    for (int j = 0; j < l(y); j++)
      if (neq(x.get(i+j), y.get(j)))
        continue outer;
    return i;
  }
  return -1;
}

static <A> int indexOfSubList(List<A> x, A[] y, int i) {
  outer: for (; i+l(y) <= l(x); i++) {
    for (int j = 0; j < l(y); j++)
      if (neq(x.get(i+j), y[j]))
        continue outer;
    return i;
  }
  return -1;
}
static String trimJoin(List<String> s) {
  return trim(join(s));
}
static <A, B> Map<A, B> synchronizedMRUCache(int maxSize) {
  return synchroMap(new MRUCache(maxSize));
}


static class allSpreads_Data {
  int n;
  List<Integer> collecting;
}

static class allSpreads_StackEntry {
  int remaining, take;
  int position; // position in code
  
  Object step(List<allSpreads_StackEntry> stack, allSpreads_Data d) {
    //printStruct(stack);
    if (position == 0) {
      if (l(d.collecting) >= d.n-1) {
        List<Integer> result = listPlus(d.collecting, remaining);
        popLast(stack);
        return result;
      }
      position++;
    }
    if (position == 1) {
      if (take <= remaining) {
        d.collecting.add(take);
        allSpreads_StackEntry e = new allSpreads_StackEntry();
        e.remaining = remaining-take;
        stack.add(e);
        position++;
      } else
        popLast(stack);
    } else if (position == 2) {
      removeLast(d.collecting);
      take++;
      position = 1;
    }
    return null;
  }
}

static IterableIterator<List<Integer>> allSpreads(final int toSpread, final int buckets) {
  final allSpreads_Data d = new allSpreads_Data();
  d.n = buckets;
  d.collecting = new ArrayList();
  
  final List<allSpreads_StackEntry> stack = new ArrayList();
  allSpreads_StackEntry e = new allSpreads_StackEntry();
  e.remaining = toSpread;
  stack.add(e);
  
  return iff(new F0<Object>() { Object get() { try { 
    while (nempty(stack)) {
      ping();
      { Object __31= last(stack).step(stack, d); if (__31 != null) return __31; }
    }
    return endMarker();
   } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "while (nempty(stack)) {\r\n      ping();\r\n      try object last(stack).step(sta..."; }});
}


static String dollarVarsToStars(String s) {
  return dollarVarsToStars(s, null);
}

static String dollarVarsToStars(String s, List<String> varNames_out) {
  List<String> tok = javaTok(s);
  for (int i = 1; i < l(tok); i += 2) {
    String t = tok.get(i);
    if (isDollarVar(t)) {
      listAdd(varNames_out, t);
      tok.set(i, "*");
    } else if (eq(t, "*"))
      listAdd(varNames_out, "*");
  }
  return join(tok);
}


static boolean equalsIgnoreCase(String a, String b) {
  return eqic(a, b);
}

static boolean equalsIgnoreCase(char a, char b) {
  return eqic(a, b);
}
// "$1" is first code token, "$2" second code token etc.
static String jreplaceExpandRefs(String s, List<String> tokref) {
  List<String> tok = javaTok(s);
  for (int i = 1; i < l(tok); i += 2) {
    if (tok.get(i).startsWith("$") && isInteger(tok.get(i).substring(1))) {
      String x = tokref.get(-1+parseInt(tok.get(i).substring(1))*2);
      tok.set(i, x);
    }
  }
  return join(tok);
}

  static void clearAllTokens(List<String> tok) {
    for (int i = 0; i < tok.size(); i++)
      tok.set(i, "");
  }
  
  static void clearAllTokens(List<String> tok, int i, int j) {
    for (; i < j; i++)
      tok.set(i, "");
  }
static List<String> reTok(List<String> tok) {
  replaceCollection(tok, javaTok(tok));
  return tok;
}

static List<String> reTok(List<String> tok, int i) {
  return reTok(tok, i, i+1);
}

static List<String> reTok(List<String> tok, int i, int j) {
  // extend i to an "N" token
  // and j to "C" (so j-1 is an "N" token)
  i = i & ~1;
  j = j | 1;
  
  List<String> t = javaTok(join(subList(tok, i, j)));
  replaceListPart(tok, i, j, t);
  
  // fallback to safety
  // reTok(tok);
  
  return tok;
}



static Map<String, String> parseDoubleLeftArrowMapCI(List<String> l) {
  return parseDoubleLeftArrowMap((Map) ciMap(), l);
}
static <A> TreeMap<String, A> caseInsensitiveMap() {
  return new TreeMap(caseInsensitiveComparator());
}
static Object getThreadLocal(Object o, String name) {
  ThreadLocal t = (ThreadLocal) ( getOpt(o, name));
  return t != null ? t.get() : null;
}

static <A> A getThreadLocal(ThreadLocal<A> tl) {
  return tl == null ? null : tl.get();
}
static List<String> javaTokPlusAllThreeBrackets(String s) {
  return javaTokPlusBrackets2(s);
}
static Pair<String, String> splitAtTokens_once(String s, List<String> tokens) {
  return splitAtTokens_once(javaTok(s), tokens);
}

static Pair<String, String> splitAtTokens_once(List<String> tok, List<String> tokens) {
  int i = indexOfSubList(tok, tokens, 0);
  if (i < 0) return null;
  return pair(
    trimJoinSubList(tok, 0, i),
    trimJoinSubList(tok, i+l(tokens)));
}
static boolean isRoundBracketed(String s) {
  return startsWith(s, "(") && endsWith(s, ")");
}
static String deRoundBracket(String s) {
  if (startsWith(s, "(") && endsWith(s, ")"))
    return substring(s, 1, l(s)-1);
  return s;
}
static String unquoteIfNormalQuoted(String s) {
  return isNormalQuoted(s) ? unquote(s) : s;
}
static String md5ToGlobalID(String md5) {
  return bigintToGlobalID(bigintFromHex(assertPossibleMD5(md5)));
}
static <A, B> List<B> secondOfPairs(Collection<Pair<A, B>> l) {
  return map("secondOfPair", l);
}

static List<Pair<String, String>> combineWithGlobalIDs(List<String> l) {
  List < Pair < String , String > > out = new ArrayList();
  for (int i = 0; i < l(l); i++) {
    if (i < l(l)-1) {
      String id = regexpFirstGroup("^\\[id: (" + regexp_globalID() + ")\\]$", trim(l.get(i)));
      if (id != null)
        { out.add(pair(id, l.get(++i))); continue; }
    }
    out.add(pair("", l.get(i)));
  }
  return out;
}
static boolean isMechAuthed() {
  return nempty(muricaPassword());
}
static String joinWithEmptyLines(List<String> l) {
  return join("\n\n", map("rtrim",l));
}

static String joinWithEmptyLines(String... l) {
  return joinWithEmptyLines(asList(l));
}

static String prependGlobalIDInNewLineAndIndent(String s) {
  return prependGlobalIDInNewLineAndIndent(aGlobalID(), s);
}

static String prependGlobalIDInNewLineAndIndent(String globalID, String s) {
  return prependGlobalIDInNewLine(globalID, indentx(autoUnindent(s)));
}
static String editMechList(String name, String text) {
  return botEditMechList(name, text);
}

static String editMechList(String name, List<String> lines) {
  return botEditMechList(name, lines);
}
static String structure_addTokenMarkers(String s) {
  List<String> tok = javaTok(s);
  
  // find references
  
  TreeSet<Integer> refs = new TreeSet();
  for (int i = 1; i < l(tok); i += 2) {
    String t = tok.get(i);
    if (t.startsWith("t") && isInteger(t.substring(1)))
      refs.add(parseInt(t.substring(1)));
  }
  
  if (empty(refs)) return s;
  
  // add markers
  for (int i : refs) {
    int idx = i*2+1;
    String t = "";
    if (endsWithLetterOrDigit(tok.get(idx-1))) t = " ";
    tok.set(idx, t + "m" + i + " " + tok.get(idx));
  }
  
  return join(tok);
}
// i must point at the (possibly imaginary) opening bracket (any of the 3 types, not type parameters)
// index returned is index of closing bracket + 1
static int findEndOfBracketPart2(List<String> cnc, int i) {
  int j = i+2, level = 1;
  while (j < cnc.size()) {
    if (eqOneOf(cnc.get(j), "{", "(", "[")) ++level;
    else if (eqOneOf(cnc.get(j), "}", ")", "]")) --level;
    if (level == 0)
      return j+1;
    ++j;
  }
  return cnc.size();
}
static long parseLocalDateWithMilliseconds_orKeepLong(Object o) {
  if (o instanceof Long) return ((Long) o);
  if (o instanceof String) return parseLocalDateWithMilliseconds(((String) o));
  return 0;
}
static <A> A nu(Class<A> c, Object... values) {
  A a = nuObject(c);
  setAll(a, values);
  return a;
}
static long toLong(Object o) {
  if (o instanceof Number)
    return ((Number) o).longValue();
  if (o instanceof String)
    return parseLong((String) o);
  return 0;
}
  static boolean matchStart(String pat, String s) {
    return matchStart(pat, s, null);
  }
  
  // matches are as you expect, plus an extra item for the rest string
  static boolean matchStart(String pat, String s, Matches matches) {
    if (s == null) return false;
    List<String> tokpat = parse3(pat), toks = parse3(s);
    if (toks.size() < tokpat.size()) return false;
    String[] m = match2(tokpat, toks.subList(0, tokpat.size()));
    //print(structure(tokpat) + " on " + structure(toks) + " => " + structure(m));
    if (m == null)
      return false;
    else {
      if (matches != null) {
        matches.m = new String[m.length+1];
        arraycopy(m, matches.m);
        matches.m[m.length] = join(toks.subList(tokpat.size(), toks.size())); // for Matches.rest()
      }
      return true;
    }
  }
static <A, B> A firstOfPair(Pair<A, B> p) {
  return p == null ? null : p.a;
}
static List<String> tlftj(String text) {
  return toLinesFullTrim_java(text);
}
static Set asSet(Object[] array) {
  HashSet set = new HashSet();
  for (Object o : array)
    if (o != null)
      set.add(o);
  return set;
}

static Set<String> asSet(String[] array) {
  TreeSet<String> set = new TreeSet();
  for (String o : array)
    if (o != null)
      set.add(o);
  return set;
}

static <A> Set<A> asSet(Iterable<A> l) {
  if (l instanceof Set) return (Set) l;
  HashSet<A> set = new HashSet();
  for (A o : unnull(l))
    if (o != null)
      set.add(o);
  return set;
}
static TimeZone getTimeZone(String name) {
  return TimeZone.getTimeZone(name);
}
static String standardTimeZone_name = "Europe/Berlin";

static String standardTimeZone() {
  return standardTimeZone_name;
}
static ThreadLocal<Boolean> assertVerbose_value = new ThreadLocal();

static void assertVerbose(boolean b) {
  assertVerbose_value.set(b);
}

static boolean assertVerbose() { return isTrue(assertVerbose_value.get()); }
static <A> A assertEqualsVerbose(Object x, A y) {
  assertEqualsVerbose((String) null, x, y);
  return y;
}

static <A> A assertEqualsVerbose(String msg, Object x, A y) {
  if (!eq(x, y)) {
    

    throw fail((msg != null ? msg + ": " : "") + /*sfu*/(y) + " != " + /*sfu*/(x));
  } else
    print("OK: " + /*sfu*/(x));
  return y;
}


static byte[] boolArrayToBytes(boolean[] a) {
  byte[] b = new byte[(l(a)+7)/8];
  for (int i = 0; i < l(a); i++)
    if (a[i])
      b[i/8] |= 1 << (i & 7);
  return b;
}
static int cmp(Number a, Number b) {
  return a == null ? b == null ? 0 : -1 : cmp(a.doubleValue(), b.doubleValue());
}

static int cmp(double a, double b) {
  return a < b ? -1 : a == b ? 0 : 1;
}

static int cmp(String a, String b) {
  return a == null ? b == null ? 0 : -1 : a.compareTo(b);
}

static int cmp(Object a, Object b) {
  if (a == null) return b == null ? 0 : -1;
  if (b == null) return 1;
  return ((Comparable) a).compareTo(b);
}
static <A, B extends A> List<A> listMinusSet(Collection<A> l, Collection<B> stuff) {
  if (empty(stuff)) return asList(l);
  Set<B> set = asSet(stuff);
  List<A> l2 = new ArrayList();
  for (A a : l) if (!set.contains(a)) l2.add(a);
  return l2;
}
static TreeSet<String> asCISet(Collection<String> c) {
  return toCaseInsensitiveSet(c);
}

static TreeSet<String> asCISet(String... x) {
  return toCaseInsensitiveSet(x);
}
static void mech_notifyBusOfChange(String listName) {
  vmBus_send("mechChange", listName);
}
static <A> A printIndent(A o) {
  print(indentx(str(o)));
  return o;
}

static void printIndent(String indent, Object o) {
  print(indentx(indent, str(o)));
}

static void printIndent(int indent, Object o) {
  print(indentx(indent, str(o)));
}
static void mechList_clearCache(String listName) {
  for (String field : mcFields())
    if (startsWithEndsWith(field, "mech", "_cache")) {
      Object o = getOptMC(field);
      if (o instanceof Map)
        removeFromMapCI_sync((Map) o, listName);
    }
    
  if (mechList_goToCreator())
    callCreatorOpt("mechList_clearCache", listName);
}
static String postPageSilently(String url, Object... params) {
  return doPostSilently(litmap(params), url);
}
// one array plus more elements
static Object[] arrayPlus(Object[] a1, Object... a2) {
  return concatArrays(a1, a2);
}
static void appendToLocalMechListCopy(String listName, String text) {
  appendToTextFile(remoteMechListMirrorFile(listName), "\n" + text);
}
static boolean endsWithLetter(String s) {
  return nempty(s) && isLetter(last(s));
}
static String firstJavaToken(String s) {
  if (s == null) return null;
  int l = s.length();

  int j = 0;
  char c, d;
    
  // scan for whitespace
  while (j < l) {
    c = s.charAt(j);
    d = j+1 >= l ? '\0' : s.charAt(j+1);
    if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
      ++j;
    else if (c == '/' && d == '*') {
      do ++j; while (j < l && !s.substring(j, Math.min(j+2, l)).equals("*/"));
      j = Math.min(j+2, l);
    } else if (c == '/' && d == '/') {
      do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0);
    } else
      break;
  }
    
  if (j >= l) return null;
  int i = j;
  c = s.charAt(i);
  d = i+1 >= l ? '\0' : s.charAt(i+1);

  // scan for non-whitespace
  if (c == '\'' || c == '"') {
    char opener = c;
    ++j;
    while (j < l) {
      if (s.charAt(j) == opener || s.charAt(j) == '\n') { // end at \n to not propagate unclosed string literal errors
        ++j;
        break;
      } else if (s.charAt(j) == '\\' && j+1 < l)
        j += 2;
      else
        ++j;
    }
  } else if (Character.isJavaIdentifierStart(c))
    do ++j; while (j < l && (Character.isJavaIdentifierPart(s.charAt(j)) || "'".indexOf(s.charAt(j)) >= 0)); // for stuff like "don't"
  else if (Character.isDigit(c)) {
    do ++j; while (j < l && Character.isDigit(s.charAt(j)));
    if (j < l && s.charAt(j) == 'L') ++j; // Long constants like 1L
  } else if (c == '[' && d == '[') {
    do ++j; while (j+1 < l && !s.substring(j, j+2).equals("]]"));
    j = Math.min(j+2, l);
  } else if (c == '[' && d == '=' && i+2 < l && s.charAt(i+2) == '[') {
    do ++j; while (j+2 < l && !s.substring(j, j+3).equals("]=]"));
    j = Math.min(j+3, l);
  } else
    ++j;
    
  return quickSubstring(s, i, j);
}
static TreeSet<String> caseInsensitiveSet() {
  return new TreeSet(caseInsensitiveComparator());
}

static TreeSet<String> caseInsensitiveSet(Collection<String> c) {
  return toCaseInsensitiveSet(c);
}
static <A> TreeSet<A> treeSet() {
  return new TreeSet();
}
static String substr(String s, int x) {
  return substring(s, x);
}

static String substr(String s, int x, int y) {
  return substring(s, x, y);
}
static String mechList_raw_fresh(String name) {
  return mechList_opt_raw_fresh(name);
}
static void add(BitSet bs, int i) {
  bs.set(i);
}

static <A> boolean add(Collection<A> c, A a) {
  return c != null && c.add(a);
}

static void add(Container c, Component x) {
  addToContainer(c, x);
}
static Set vm_busListeners_live() {
  return vm_generalIdentityHashSet("busListeners");
}
static <A> void remove(List<A> l, int i) {
  if (l != null && i >= 0 && i < l(l))
    l.remove(i);
}

static <A> void remove(Collection<A> l, A a) {
  if (l != null) l.remove(a);
}
static int numberOfJavaTokens(String s) {
  return l(javaTokC(s));
}
static HashSet collectAsHashSet(Iterable c, String field) {
  HashSet set = new HashSet();
  if (c != null) for (Object a : c)
    set.add(getOpt(a, field));
  return set;
}

static HashSet collectAsHashSet(String field, Iterable c) {
  return collectAsHashSet(c, field);
}
static void mapIntoSameCollection(Object f, Collection c) {
  if (c == null) return;
  replaceCollection(c, map(f, c));
}

static void mapIntoSameCollection(Collection c, Object f) {
  mapIntoSameCollection(f, c);
}
static List<String> splitAtVerticalBar(String s) {
  return s == null ? new ArrayList() : trimAll(asList(s.split("\\|")));
}
static <A> A _get(List<A> l, int idx) {
  return l != null && idx >= 0 && idx < l(l) ? l.get(idx) : null;
}

static Object _get(Object o, String field) {
  return get(o, field);
}

static <A> A _get(A[] l, int idx) {
  return idx >= 0 && idx < l(l) ? l[idx] : null;
}
static Object unstructure(String text) {
  return unstructure(text, false);
}

static Object unstructure(String text, final boolean allDynamic) {
  return unstructure(text, allDynamic, null);
}

static int structure_internStringsLongerThan = 50;
static int unstructure_unquoteBufSize = 100;

static int unstructure_tokrefs; // stats

abstract static class unstructure_Receiver {
  abstract void set(Object o);
}

// classFinder: func(name) -> class (optional)
static Object unstructure(String text, boolean allDynamic,
  Object classFinder) {
  if (text == null) return null;
  return unstructure_tok(javaTokC_noMLS_iterator(text), allDynamic, classFinder);
}

static Object unstructure_reader(BufferedReader reader) {
  return unstructure_tok(javaTokC_noMLS_onReader(reader), false, null);
}

static Object unstructure_tok(final Producer<String> tok, final boolean allDynamic, final Object classFinder) {
  final boolean debug = unstructure_debug;
  
  final class X {
    int i = -1;
    HashMap<Integer,Object> refs = new HashMap();
    HashMap<Integer,Object> tokrefs = new HashMap();
    HashSet<String> concepts = new HashSet();
    HashMap<String,Class> classesMap = new HashMap();
    List<Runnable> stack = new ArrayList();
    String curT;
    char[] unquoteBuf = new char[unstructure_unquoteBufSize];
    
    String unquote(String s) {
      return unquoteUsingCharArray(s, unquoteBuf); 
    }

    // look at current token
    String t() {
      return curT;
    }
    
    // get current token, move to next
    String tpp() {
      String t = curT;
      consume();
      return t;
    }
    
    void parse(final unstructure_Receiver out) {
      String t = t();
      
      int refID = 0;
      if (structure_isMarker(t, 0, l(t))) {
        refID = parseInt(t.substring(1));
        consume();
      }
      final int _refID = refID;
      
      // if (debug) print("parse: " + quote(t));
      
      final int tokIndex = i;  
      parse_inner(refID, tokIndex, new unstructure_Receiver() {
        void set(Object o) {
          if (_refID != 0)
            refs.put(_refID, o);
          if (o != null)
            tokrefs.put(tokIndex, o);
          out.set(o);
        }
      });
    }
    
    void parse_inner(int refID, int tokIndex, final unstructure_Receiver out) {
      String t = t();
      
      // if (debug) print("parse_inner: " + quote(t));
      
      Class c = classesMap.get(t);
      if (c == null) {
        if (t.startsWith("\"")) {
          String s = internIfLongerThan(unquote(tpp()), structure_internStringsLongerThan);
          out.set(s); return;
        }
        
        if (t.startsWith("'")) {
          out.set(unquoteCharacter(tpp())); return;
        }
        if (t.equals("bigint")) {
          out.set(parseBigInt()); return;
        }
        if (t.equals("d")) {
          out.set(parseDouble()); return;
        }
        if (t.equals("fl")) {
          out.set(parseFloat()); return;
        }
        if (t.equals("sh")) {
          consume();
          t = tpp();
          if (t.equals("-")) {
            t = tpp();
            out.set((short) (-parseInt(t))); return;
          }
          out.set((short) parseInt(t)); return;
        }
        if (t.equals("-")) {
          consume();
          t = tpp();
          out.set(isLongConstant(t) ? (Object) (-parseLong(t)) : (Object) (-parseInt(t))); return;
        }
        if (isInteger(t) || isLongConstant(t)) {
          consume();
          //if (debug) print("isLongConstant " + quote(t) + " => " + isLongConstant(t));
          if (isLongConstant(t)) {
            out.set(parseLong(t)); return;
          }
          long l = parseLong(t);
          boolean isInt = l == (int) l;
          if (debug)
            print("l=" + l + ", isInt: " + isInt);
          out.set(isInt ? (Object) new Integer((int) l) : (Object) new Long(l)); return;
        }
        if (t.equals("false") || t.equals("f")) {
          consume(); out.set(false); return;
        }
        if (t.equals("true") || t.equals("t")) {
          consume(); out.set(true); return;
        }
        if (t.equals("-")) {
          consume();
          t = tpp();
          out.set(isLongConstant(t) ? (Object) (-parseLong(t)) : (Object) (-parseInt(t))); return;
        }
        if (isInteger(t) || isLongConstant(t)) {
          consume();
          //if (debug) print("isLongConstant " + quote(t) + " => " + isLongConstant(t));
          if (isLongConstant(t)) {
            out.set(parseLong(t)); return;
          }
          long l = parseLong(t);
          boolean isInt = l == (int) l;
          if (debug)
            print("l=" + l + ", isInt: " + isInt);
          out.set(isInt ? (Object) new Integer((int) l) : (Object) new Long(l)); return;
        }
        
        if (t.equals("File")) {
          consume();
          File f = new File(unquote(tpp()));
          out.set(f); return;
        }
        
        if (t.startsWith("r") && isInteger(t.substring(1))) {
          consume();
          int ref = Integer.parseInt(t.substring(1));
          Object o = refs.get(ref);
          if (o == null)
            print("Warning: unsatisfied back reference " + ref);
          out.set(o); return;
        }
      
        if (t.startsWith("t") && isInteger(t.substring(1))) {
          consume();
          int ref = Integer.parseInt(t.substring(1));
          Object o = tokrefs.get(ref);
          if (o == null)
            print("Warning: unsatisfied token reference " + ref);
          out.set(o); return;
        }
        
        if (t.equals("hashset")) { parseHashSet(out); return; }
        if (t.equals("lhs")) { parseLinkedHashSet(out); return; }
        if (t.equals("treeset")) { parseTreeSet(out); return; }
        
        if (eqOneOf(t, "hashmap", "hm")) {
          consume();
          parseMap(new HashMap(), out);
          return;
        }
        if (t.equals("lhm")) {
          consume();
          parseMap(new LinkedHashMap(), out);
          return;
        }
        if (t.equals("sync")) {
          consume();
          if (t().equals("tm")) {
            consume();
            { parseMap(synchronizedTreeMap(), out); return; }
          }
          { parseMap(synchronizedMap(), out); return; }
        }
        if (t.equals("{")) {
          parseMap(out); return;
        }
        if (t.equals("[")) {
          this.parseList(out); return;
        }
        if (t.equals("bitset")) {
          parseBitSet(out); return;
        }
        if (t.equals("array") || t.equals("intarray")) {
          parseArray(out); return;
        }
        if (t.equals("ba")) {
          consume();
          String hex = unquote(tpp());
          out.set(hexToBytes(hex)); return;
        }
        if (t.equals("boolarray")) {
          consume();
          int n = parseInt(tpp());
          String hex = unquote(tpp());
          out.set(boolArrayFromBytes(hexToBytes(hex), n)); return;
        }
        if (t.equals("class")) {
          out.set(parseClass()); return;
        }
        if (t.equals("l")) {
          parseLisp(out); return;
        }
        if (t.equals("null")) {
          consume(); out.set(null); return;
        }
        
        if (eq(t, "c")) {
          consume("c");
          t = t();
          assertTrue(isJavaIdentifier(t));
          concepts.add(t);
        }
      }
      
      if (eq(t, "j")) {
        consume("j");
        out.set(parseJava()); return;
      }

      if (c == null && !isJavaIdentifier(t))
        throw new RuntimeException("Unknown token " + (i+1) + ": " + t);
        
      // any other class name
      if (c == null) {
        // First, find class
        if (allDynamic) c = null;
        else c = classFinder != null ? (Class) callF(classFinder, "main$" + t) : findClass(t);
        if (c != null)
          classesMap.put(t, c);
      }
          
      // Check if it has an outer reference
      consume();
      boolean hasBracket = eq(t(), "(");
      if (hasBracket) consume();
      boolean hasOuter = hasBracket && eq(t(), "this$1");
      
      DynamicObject dO = null;
      Object o = null;
      final String thingName = t;
      if (c != null) {
        o = hasOuter ? nuStubInnerObject(c, classFinder) : nuEmptyObject(c);
        if (o instanceof DynamicObject) dO = (DynamicObject) o;
      } else {
        if (concepts.contains(t) && (c = findClass("Concept")) != null)
          o = dO = (DynamicObject) nuEmptyObject(c);
        else
          dO = new DynamicObject();
        dO.className = t;
        if (debug) print("Made dynamic object " + t + " " + shortClassName(dO));
      }
      
      // Save in references list early because contents of object
      // might link back to main object
      
      if (refID != 0)
        refs.put(refID, o != null ? o : dO);
      tokrefs.put(tokIndex, o != null ? o : dO);
      
      // NOW parse the fields!
      
      final LinkedHashMap<String,Object> fields = new LinkedHashMap(); // preserve order
      final Object _o = o;
      final DynamicObject _dO = dO;
      if (hasBracket) {
        stack.add(new Runnable() {  public void run() { try { 
          if (eq(t(), ")")) {
            consume(")");
            objRead(_o, _dO, fields);
            out.set(_o != null ? _o : _dO);
          } else {
            final String key = unquote(tpp());
            if (!eq(tpp(), "="))
              throw fail("= expected, got " + t() + " after " + quote(key) + " in object " + thingName /*+ " " + sfu(fields)*/);
            stack.add(this);
            parse(new unstructure_Receiver() {
              void set(Object value) {
                fields.put(key, value);
                if (eq(t(), ",")) consume();
              }
            });
          }
        
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "if (eq(t(), \")\")) {\r\n            consume(\")\");\r\n            objRead(_o, _dO, ..."; }});
      } else {
        objRead(o, dO, fields);
        out.set(o != null ? o : dO);
      }
    }
    
    void objRead(Object o, DynamicObject dO, Map<String, Object> fields) {
      
      if (o != null)
        if (dO != null) {
          if (debug)
            printStructure("setOptAllDyn", fields);
          setOptAllDyn(dO, fields);
        } else {
          setOptAll_pcall(o, fields);
          
        }
      else for (String field : keys(fields))
        dO.fieldValues.put(intern(field), fields.get(field));

      if (o != null)
        pcallOpt_noArgs(o, "_doneLoading");
    }
    
    void parseSet(final Set set, final unstructure_Receiver out) {
      this.parseList(new unstructure_Receiver() {
        void set(Object o) {
          set.addAll((List) o);
          out.set(set);
        }
      });
    }
    
    void parseLisp(final unstructure_Receiver out) {
      
      
      throw fail("class Lisp not included");
    }
    
    void parseBitSet(final unstructure_Receiver out) {
      consume("bitset");
      consume("{");
      final BitSet bs = new BitSet();
      stack.add(new Runnable() {  public void run() { try { 
        if (eq(t(), "}")) {
          consume("}");
          out.set(bs);
        } else {
          stack.add(this);
          parse(new unstructure_Receiver() {
            void set(Object o) {
              bs.set((Integer) o);
              if (eq(t(), ",")) consume();
            }
          });
        }
      
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "if (eq(t(), \"}\")) {\r\n          consume(\"}\");\r\n          out.set(bs);\r\n       ..."; }});
    }
    
    void parseList(final unstructure_Receiver out) {
      consume("[");
      final ArrayList list = new ArrayList();
      stack.add(new Runnable() {  public void run() { try { 
        if (eq(t(), "]")) {
          consume("]");
          out.set(list);
        } else {
          stack.add(this);
          parse(new unstructure_Receiver() {
            void set(Object o) {
              //if (debug) print("List element type: " + getClassName(o));
              list.add(o);
              if (eq(t(), ",")) consume();
            }
          });
        }
      
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "if (eq(t(), \"]\")) {\r\n          consume(\"]\");\r\n          out.set(list);\r\n     ..."; }});
    }
    
    void parseArray(final unstructure_Receiver out) {
      final String type = tpp();
      consume("{");
      final List list = new ArrayList();
      
      stack.add(new Runnable() {  public void run() { try { 
        if (eq(t(), "}")) {
          consume("}");
          out.set(type.equals("intarray") ? toIntArray(list) : list.toArray());
        } else {
          stack.add(this);
          parse(new unstructure_Receiver() {
            void set(Object o) {
              list.add(o);
              if (eq(t(), ",")) consume();
            }
          });
        }
      
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "if (eq(t(), \"}\")) {\r\n          consume(\"}\");\r\n          out.set(type.equals(\"..."; }});
    }
    
    Object parseClass() {
      consume("class");
      consume("(");
      String name = unquote(tpp());
      consume(")");
      name = dropPrefix("main$", name);
      Class c = allDynamic ? null : classFinder != null ? (Class) callF(classFinder, name) : findClass(name);
      if (c != null) return c;
      DynamicObject dO = new DynamicObject();
      dO.className = "java.lang.Class";
      dO.fieldValues.put("name", name);
      return dO;
    }
    
    Object parseBigInt() {
      consume("bigint");
      consume("(");
      String val = tpp();
      if (eq(val, "-"))
        val = "-" + tpp();
      consume(")");
      return new BigInteger(val);
    }
    
    Object parseDouble() {
      consume("d");
      consume("(");
      String val = unquote(tpp());
      consume(")");
      return Double.parseDouble(val);
    }
    
    Object parseFloat() {
      consume("fl");
      String val;
      if (eq(t(), "(")) {
        consume("(");
        val = unquote(tpp());
        consume(")");
      } else {
        val = unquote(tpp());
      }
      return Float.parseFloat(val);
    }
    
    void parseHashSet(unstructure_Receiver out) {
      consume("hashset");
      parseSet(new HashSet(), out);
    }
    
    void parseLinkedHashSet(unstructure_Receiver out) {
      consume("lhs");
      parseSet(new LinkedHashSet(), out);
    }
    
    void parseTreeSet(unstructure_Receiver out) {
      consume("treeset");
      parseSet(new TreeSet(), out);
    }
    
    void parseMap(unstructure_Receiver out) {
      parseMap(new TreeMap(), out);
    }
    
    Object parseJava() {
      String j = unquote(tpp());
      Matches m = new Matches();
      if (jmatch("java.awt.Color[r=*,g=*,b=*]", j, m))
        return nuObject("java.awt.Color", parseInt(m.unq(0)), parseInt(m.unq(1)), parseInt(m.unq(2)));
      else {
        warn("Unknown Java object: " + j);
        return null;
      }
    }
    
    void parseMap(final Map map, final unstructure_Receiver out) {
      consume("{");
      stack.add(new Runnable() {
        boolean v;
        Object key;
        
        public void run() { 
          if (v) {
            v = false;
            stack.add(this);
            if (!eq(tpp(), "="))
              throw fail("= expected, got " + t() + " in map of size " + l(map));

            parse(new unstructure_Receiver() {
              void set(Object value) {
                map.put(key, value);
                if (debug)
                  print("parseMap: Got value " + getClassName(value) + ", next token: " + quote(t()));
                if (eq(t(), ",")) consume();
              }
            });
          } else {
            if (eq(t(), "}")) {
              consume("}");
              out.set(map);
            } else {
              v = true;
              stack.add(this);
              parse(new unstructure_Receiver() {
                void set(Object o) {
                  key = o;
                }
              });
            }
          } // if v else
        } // run()
      });
    }
    
    /*void parseSub(unstructure_Receiver out) {
      int n = l(stack);
      parse(out);
      while (l(stack) > n)
        stack
    }*/
    
    void consume() { curT = tok.next(); ++i; }
    
    void consume(String s) {
      if (!eq(t(), s)) {
        /*S prevToken = i-1 >= 0 ? tok.get(i-1) : "";
        S nextTokens = join(tok.subList(i, Math.min(i+2, tok.size())));
        fail(quote(s) + " expected: " + prevToken + " " + nextTokens + " (" + i + "/" + tok.size() + ")");*/
        throw fail(quote(s) + " expected, got " + quote(t()));
      }
      consume();
    }
    
    void parse_x(unstructure_Receiver out) {
      consume(); // get first token
      parse(out);
      while (nempty(stack))
        popLast(stack).run();
    }
  }
  
  Boolean b = DynamicObject_loading.get();
  DynamicObject_loading.set(true);
  try {
    final Var v = new Var();
    X x = new X();
    x.parse_x(new unstructure_Receiver() {
      void set(Object o) { v.set(o); }
    });
    unstructure_tokrefs = x.tokrefs.size();
    return v.get();
  } finally {
    DynamicObject_loading.set(b);
  }
}

static boolean unstructure_debug;
static <A, B, C> T3<A, B, C> t3(A a, B b, C c) {
  return new T3(a, b, c);
}
static boolean cicOneOf(String s, String... l) {
  for (String x : l) if (cic(s, x)) return true; return false;
}

static boolean cicOneOf(Collection<String> l, String... bla) {
  for (String x : unnull(l))
    if (eqicOneOf(x, bla)) return true;
  return false;
}
static List<Integer> indicesOfAnyIC(List<String> l, String... bla) {
  List<Integer> x = new ArrayList();
  for (int i = 0; i < l(l); i++)
    if (eqicOneOf(l.get(i), bla))
      x.add(i);
  return x;
}
static <A> IterableIterator<List<A>> allCombinations(final List<List<A>> base) {
  return iteratorFromFunction(new F0<List<A>>() {
    final int n = l(base);
    List<Integer> l = rep((Integer) 0, n);
    
    public List<A> get() {
      if (l == null) return null;
      List<A> out = makeList();
      int i = 0;
      while (i < n) {
        int n = l.get(i);
        if (n+1 < l(base.get(i))) {
          l.set(i, n+1);
          break;
        }
        l.set(i++, 0);
      }
      if (i >= n) l = null;
      return out;
    }
    
    List<A> makeList() {
      List<A> list = emptyList(n);
      for (int i = 0; i < n; i++)
        list.add(base.get(i).get(l.get(i)));
      return list;
    }
  });
}
static String rep(int n, char c) {
  return repeat(c, n);
}

static String rep(char c, int n) {
  return repeat(c, n);
}

static <A> List<A> rep(A a, int n) {
  return repeat(a, n);
}

static <A> List<A> rep(int n, A a) {
  return repeat(n, a);
}

static Map<String, String> parseDoubleArrowMap(Map<String, String> map, List<String> l) {
  for (String s : l) {
    List<String> x = splitAtDoubleArrow_bothDirections(s);
    if (l(x) == 2) map.put(first(x), second(x));
  }
  return map;
}
static <B> TreeMap<String, B> asCaseInsensitiveMap(Map<String, B> map) {
  if (isCIMap(map)) return (TreeMap) map;
  TreeMap<String, B> m = ciMap();
  m.putAll(map);
  return m;
}
static RuntimeException asQuickException(Throwable t) {
  return t instanceof RuntimeException ? (RuntimeException) t : new QuickException(t);
}
static boolean hasMethod(Object o, String method, Object... args) {
  return findMethod(o, method, args) != null;
}
// contains voidfunc(childClass)
static ThreadLocal makeAndCall_initChild = new ThreadLocal();

static Object makeAndCall_cached(String functionName, Object... args) {
  Class c = loadFunctions_cached(functionName);
  callF(makeAndCall_initChild.get(), c);
  return preciseCall(c, functionName, args);
}
static <A> List<A> newSubList(List<A> l, int startIndex, int endIndex) {
  return cloneList(subList(l, startIndex, endIndex));
}

static <A> List<A> newSubList(List<A> l, int startIndex) {
  return cloneList(subList(l, startIndex));
}
static List<String> getPlural_specials = ll("sheep", "fish");

static String getPlural(String s) {
  if (containsIgnoreCase(getPlural_specials, s)) return s;
  if (ewic(s, "y")) return dropSuffixIgnoreCase("y", s) + "ies";
  if (ewic(s, "ss")) return s + "es";
  if (ewic(s, "s")) return s;
  return s + "s";
}
static void removeSubList(List l, int from, int to) {
  if (l != null) subList(l, from, to).clear();
}

static void removeSubList(List l, int from) {
  if (l != null) subList(l, from).clear();
}
// f can return false to suppress regular printing
// call print_raw within f to actually print something
// f preferrably is F1<S, Bool>
static Object interceptPrintInThisThread(Object f) {
  Object old = print_byThread().get();
  print_byThread().set(f);
  return old;
}
static <A> List<A> uniquifyList(Collection<A> l) {
  if (l == null) return null;
  if (l(l) < 2) return asList(l);
  HashSet<A> set = new HashSet();
  List<A> out = new ArrayList();
  for (A a : l)
    if (set.add(a))
      out.add(a);
  return out;
}
static List<String> codeTokens(List<String> tok) {
  return codeTokensOnly(tok);
}
static <A, B> void putIfValueNempty(Map<A, B> map, A key, B value) {
  if (map != null && nempty(value)) map.put(key, value);
}
static Set<String> combinedMechSetsCI(Collection<String> listNames) {
  if (empty(listNames)) return ciSet();
  if (l(listNames) == 1) return mechSetCI(first(listNames));
  Set<String> set = ciSet();
  for (String listName : listNames)
    set.addAll(mL(listName));
  return set;
}


// TODO: clean on language change
static TimedCache<Map<String, Set<String>>> ai_mechListNamesForAngleBracketCategory_cache = new TimedCache(5.0);

static Set<String> ai_mechListNamesForAngleBracketCategory(String s) {
  if (!isAngleBracketed(s)) return null;
  Map<String, Set<String>> map = ai_mechListNamesForAngleBracketCategory_cache.get("newHashMap");
  s = dropTrailingNumberFromAngleBracketCategory(s);
  if (map.containsKey(s)) return map.get(s);
  // Derive from the category name - use LinkedHashSet to keep first
  Set<String> set = litorderedset(mechLanguagePrefix() + firstToUpper(plural_mech(replace(deAngleBracket(s), "_", " "))));
  set.addAll(mechMultiMapCI(mechLanguagePrefix() + "Word category => List").get(s));
  map.put(s, set);
  return set;
}



static List<WeightedProduction> ai_buParser_parseWeightedProductions() {
  return ai_buParser_parseWeightedProductions(mL_raw(ai_language() + " bottom-up groupings"));
}

static List<WeightedProduction> ai_buParser_parseWeightedProductions(String text) {
  List<WeightedProduction> l = new ArrayList();
  for (String s : tlftj(text)) {
    s = trim(javaDropAllComments(s));
    List<String> meta = trimAll(tok_splitAtComma(trailingAngleBracketStuff(s)));
    s = dropTrailingAngleBracketStuff(s);
    //print("s=" + s +", meta=" + meta);
    List<String> tok = javaTokWithAngleBracketsC(s);
    WeightedProduction p = null;
    double weight = 1.0;
    String f = null;
    for (String met : meta)
      if (startsWithDigit(met))
        weight = parseDouble(met);
      else if (startsWith(met, "ai"))
        f = met;
    if (l(tok) == 5)
      p = new WeightedProduction(tok.get(0), tok.get(2), tok.get(4), weight);
    else if (l(tok) == 3)
      p = new WeightedProduction(tok.get(0), tok.get(2), weight);
      
    if (p != null) {
      p.plausibilityFunction = f;
      for (String met : meta)
        if (eqOneOf(met, "L", "R", "B")) {
          //print("Have simplification hint: " + met);
          p.simplificationHint = met;
        }
      l.add(p);
    }
  }
  return l;
}
static void ai_parseTree_makeWeights(AI_BottomUpParser1 parser, List<WeightedProduction> productions, final PTElement e) {
  ping();
  
  // Descend first
  for (PTElement child : ai_parseTree_getChildren(parser, productions, e))
    ai_parseTree_makeWeights(parser, productions, child);
    
  // Then calculate this node's weight
  
  // check for explicit anti-examples
  if (e instanceof HaveCategory && contains(ai_antiWordCategoriesWithElements().get(((HaveCategory) e).category), e.text()))
    { e.weight = 0.1; return; }
    
  if (e instanceof ChoosePart)
    e.weight = avg(first(e.children).weight, second(e.children).weight);
  else if (e instanceof HaveCategory && empty(e.children))
    e.weight = or(ai_getWordToCategoryWeight(e.text(), ((HaveCategory) e).category), 1.0);
  else
    e.weight = collectMaxDouble(e.children, "weight");
    
  ai_Reconstructed r =
    e instanceof ChoosePart ? ((ChoosePart) e).reconstruction
    : e instanceof HaveCategory ? ((HaveCategory) e).reconstruction : null;
  if (r != null)
    e.weight *= r.production.weight;
}
static void ai_parseTree_makeTotalWeights(PTElement e, double weight) {
  ping();
  e.totalWeight = weight*e.weight;
  
  for (PTElement child : unnull(e.children))
    ai_parseTree_makeTotalWeights(child, e.totalWeight);
    
  if (!(e instanceof ChoosePart))
    sortInPlaceByCalculatedFieldDesc(e.children, ai_parseTree_sortingFunction());
}
static CloseableIterableIterator emptyCloseableIterableIterator_instance = new CloseableIterableIterator() {
  public Object next() { throw fail(); }
  public boolean hasNext() { return false; }
};

static <A> CloseableIterableIterator<A> emptyCloseableIterableIterator() {
  return emptyCloseableIterableIterator_instance; 
}
static CloseableIterableIterator<String> linesFromReader(Reader r) {
  final BufferedReader br = bufferedReader(r);
  return iteratorFromFunction_f0_autoCloseable(new F0<String>() { String get() { try { return  readLineFromReaderWithClose(br) ; } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "readLineFromReaderWithClose(br)"; }}, _wrapIOCloseable(r));
}
static BufferedReader utf8bufferedReader(InputStream in) { try {
  return bufferedReader(_registerIOWrap(new InputStreamReader(in, "UTF-8"), in));
} catch (Exception __e) { throw rethrow(__e); } }

static BufferedReader utf8bufferedReader(File f) { try {
  return utf8bufferedReader(newFileInputStream(f));
} catch (Exception __e) { throw rethrow(__e); } }
static <A> HashSet<A> asHashSet(Collection<A> c) {
  synchronized(collectionMutex(c)) {
    return new HashSet(c);
  }
}

static <A> HashSet<A> asHashSet(A[] a) {
  return a == null ? null : new HashSet(Arrays.asList(a));
}
static <A> Set<A> setPlus(Collection<A> l, A... more) {
  Set<A> set = similarEmptySet(l);
  set.addAll(l);
  for (A a : more) set.add(a);
  return set;
}
static <A> boolean isSuperSet(Collection<? extends A> a, Collection<A> b) {
  return containsAll(asSet(a), b);
}
static Set<String> nlLogic_constructNames(Exp exp) {
  final HashSet<String> set = new HashSet();
  nlLogic_visitShallow(exp, new VF1<Object>() { public void get(Object o) { try { 
    if (o instanceof Func) set.add(((Func) o).name);
    if (o instanceof Literal) set.add(((Literal) o).text());
   } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "if (o cast Func) set.add(o.name);\r\n    if (o cast Literal) set.add(o.text());"; }});
  return set;
}
static List<String> splitAtSlash(String s) {
  return trimAll(asList(s.split("/")));
}
static List filterByProgramIDAndShortClassName(Iterable l, String progID, String className) {
  List out = new ArrayList();
  for (Object o : unnull(l))
    if (shortClassNameIs(o, className) && sameSnippetID(getProgramID(o), progID))
      out.add(o);
  return out;
}
static List dm_listModules() {
  return callCreator("listModules");
}
static List filterByShortClassName(Iterable l, String name) {
  List out = new ArrayList();
  for (Object o : unnull(l))
    if (shortClassNameIs(o, name))
      out.add(o);
  return out;
}
static Object callCalc(Object o) {
  return call(o, "calc");
}
static Object evalJava_prep2(String main) {
  Object obj = veryQuickJava(main);
  makeDependent(obj);
  setOpt(obj, "getProgramName_cache", "User Code");
  return obj;
}
static boolean tok_shouldAddReturn(List<String> tok) {
  String lastToken = get(tok, l(tok)-2);
  return !eqOneOf(lastToken, "}", ";");
}

static boolean tok_shouldAddReturn(String s) {
  return tok_shouldAddReturn(javaTok(s));
}
static boolean isKnownVoidFunction(String name) {
  //ret mL_localCopy("Void Standard Functions").contains(name);
  return standardFunctionAlwaysReturnsVoid(name);
}
static void tokAppend_reTok(List<String> tok, int i, String s) {
  tok.set(i, tok.get(i)+s);
  reTok(tok, i, i+1);
}
// optionally convert expression to return statement
static String tok_addReturn(List<String> tok) {
  return tok_shouldAddReturn(tok) ? "ret " + join(tok) + ";" : join(tok);
}

static String tok_addReturn(String s) {
  return tok_addReturn(javaTok(s));
}
// We'd be really fancy if we filtered out return statements in
// inner blocks.
static boolean containsReturnWithArgument(List<String> tok) {
  for (int i = 1; i+2 < l(tok); i += 2)
    if (eqOneOf(tok.get(i), "ret", "return") && neq(tok.get(i+2), ";"))
      return true;
  return false;
}

static boolean containsReturnWithArgument(String code) {
  return containsReturnWithArgument(javaTok(code));
}
static String mechList_opt_raw(String name) { try {
  Lock __1823 = mechList_raw_lock; lock(__1823); try {
  
  if (mechList_goToCreator() || eq(mechMode().readMode, "creator"))
    return (String) call(creator(), "mechList_opt_raw", name);
  
  // get from cache
  String src = mechList_raw_cache.get(name);
  if (src != null) return src;
  
  // load & put in cache
  src = mechList_opt_raw_fresh(name);
  mechList_raw_cache.put(name, unnull(src));
  return src;
} finally { unlock(__1823); } } catch (Exception __e) { throw rethrow(__e); } }
static LinkedHashSet<String> tok_dollarVarTokens(List<String> tok) {
  LinkedHashSet<String> vars = new LinkedHashSet();
  for (int i = 1; i < l(tok); i += 2) {
    String t = tok.get(i);
    if (isDollarVar(t))
      vars.add(t);
  }
  return vars;
}
static <A, B> boolean containsKey(Map<A, B> map, A key) {
  return map != null && map.containsKey(key);
}
static Pair<List<String>, Object> prepareEvalWithDollarVars(String code) {
  return prepareEvalWithDollarVars(code, tok_dollarVarTokens(javaTok(code)));
}
  
static Pair<List<String>, Object> prepareEvalWithDollarVars(String code, Set<String> vars) {
   AutoCloseable __1830 = tempSetThreadLocal(evalJava_prep_args, joinWithComma(map(new F1<String, String>() { String get(String var) { try { return  "S " + var ; } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "\"S \" + var"; }}, vars))); try {
  String src = evalJava_prep(code);
  Object c = evalJava_prep2(src);
  return pair((List<String>) asList(vars), c);
} finally { _close(__1830); }}
static int indexOfIgnoreCase_manual(String a, String b) {
  int la = l(a), lb = l(b);
  if (la < lb) return -1;
  int n = la-lb;
  
  loop: for (int i = 0; i <= n; i++) {
    for (int j = 0; j < lb; j++) {
      char c1 = a.charAt(i+j), c2 = b.charAt(j);
      if (!eqic(c1, c2))
        continue loop;
    }
    return i;
  }
  return -1;
}
static List<String> tok_combineRoundOrCurlyBrackets_keep(List<String> tok) {
  List<String> l = new ArrayList();
  for (int i = 0; i < l(tok); i++) {
    String t = tok.get(i);
    if (odd(i) && eqOneOf(t, "{", "(")) {
      int j = findEndOfBracketPart(tok, i);
      l.add(joinSubList(tok, i, j));
      i = j-1;
    } else
      l.add(t);
  }
  return l;
}
static <A> Set<A> synchroHashSet() {
  return Collections.synchronizedSet(new HashSet<A>());
}

static <A> Set<A> synchroWeakHashSet() {
  return Collections.newSetFromMap((Map) newWeakHashMap());
}
static <A> boolean setAdd(Collection<A> c, A a) {
  if (c == null || c.contains(a)) return false;
  c.add(a);
  return true;
}
static void cancelThread(Thread t) {
  if (t == null) return;
  ping();
  /*O mc = getWeakRef((WeakReference) vm_generalWeakSubMap("thread2mc").get(t));
  ifdef cancelThread_verbose
  print("cancelThread: mc=" + mc);
  endifdef
  if (mc != null) {
    Map ping_actions = cast get(mc, 'ping_actions);
    synchronized(ping_actions) {
      ping_actions.put(t, "cancelled");
      set(mc, ping_anyActions := true);
    }
  } else*/ synchronized(ping_actions) {
    ping_actions.put(t, "cancelled");
    ping_anyActions = true;
  }
}
static boolean isCurlyBraced(String s) {
  List<String> tok = tok_combineCurlyBrackets_keep(javaTok(s));
  return l(tok) == 3 && startsWithAndEndsWith(tok.get(1), "{", "}");
}
static String uncurly(String s) {
  return tok_unCurlyBracket(s);
}
static <A, B, C> Map<B, C> subHashMap(Map<A, Map<B, C>> map, A key) {
  Map<B, C> subMap = map.get(key);
  if (subMap == null)
    map.put(key, subMap = new HashMap());
  return subMap;
}
static boolean ai_isUncategorizedWord(String word) {
  return empty(ai_wordToCategories(word));
}
static boolean checkTokCondition(Object condition, List<String> tok, int i) {
  if (condition instanceof TokCondition)
    return ((TokCondition) condition).get(tok, i);
  return checkCondition(condition, tok, i);
}
static List<String> nlTok(String s) {
  return javaTokPlusPeriod(s);
}
static int lastIndexOf(String a, String b) {
  return a == null || b == null ? -1 : a.lastIndexOf(b);
}

static int lastIndexOf(String a, char b) {
  return a == null ? -1 : a.lastIndexOf(b);
}
static <A> List<A> listPlus(Collection<A> l, A... more) {
  return concatLists(l, asList(more));
}
static void removeLast(List l) {
  if (!l.isEmpty())
    l.remove(l(l)-1);
}

static void removeLast(List l, int n) {
  removeSubList(l, l(l)-n, l(l));
}
// f: func -> A | endMarker
static <A> IterableIterator<A> iff(Object f) {
  return iteratorFromFunction_withEndMarker(f);
}

static <A> IterableIterator<A> iff(F0 f) {
  return iteratorFromFunction_withEndMarker(f);
}
static Object endMarker() {
  return iteratorFromFunction_endMarker;
}
static <A> void listAdd(Collection<A> c, A a) {
  if (c != null) c.add(a);
}
static <A> void replaceCollection(Collection<A> dest, Collection<A> src) {
  dest.clear();
  dest.addAll(src);
}
static void replaceListPart(List l, int i, int j, List l2) {
  l.subList(i, j).clear();
  l.addAll(i, l2);
}


static <A, B> B secondOfPair(Pair<A, B> p) {
  return p == null ? null : p.b;
}
public static String rtrim(String s) {
  if (s == null) return null;
  int i = s.length();
  while (i > 0 && " \t\r\n".indexOf(s.charAt(i-1)) >= 0)
    --i;
  return i < s.length() ? s.substring(0, i) : s;
}
static <A, B> HashMap<A, B> newHashMap() {
  return new HashMap();
}
// requires ugly casting when used (O -> A)
static Object iteratorFromFunction_endMarker = new Object();

// f: func -> A | endMarker
static <A> IterableIterator<A> iteratorFromFunction_withEndMarker(final Object f) {
  class IFF extends IterableIterator<A> {
    A a;
    boolean done;
    
    public boolean hasNext() {
      getNext();
      return !done;
    }
    
    public A next() {
      getNext();
      if (done) throw fail();
      A _a = a;
      a = null;
      return _a;
    }
    
    void getNext() {
      if (done || a != null) return;
      a = (A) callF(f);
      if (a == iteratorFromFunction_endMarker)
        done = true;
    }
  };
  return new IFF();
}

// optimized version for F0 argument
static <A> IterableIterator<A> iteratorFromFunction_withEndMarker(final F0 f) {
  return iteratorFromFunction_withEndMarker_f0(f);
}
static Map<String, String> parseDoubleLeftArrowMap(Map<String, String> map, List<String> l) {
  for (String s : l) {
    List<String> x = splitAtDoubleLeftArrow(s);
    if (l(x) == 2) map.put(second(x), first(x));
  }
  return map;
}
static Comparator<String> caseInsensitiveComparator() {
  
  
  return betterCIComparator();
  
}
static String bigintToGlobalID(BigInteger value) {
  char[] buf = new char[16];
  for (int i = 16-1; i >= 0; i--) {
    buf[i] = charPlus('a', mod(value, 26).intValue());
    value = div(value, 26);
  }
  return str(buf);
}
static BigInteger bigintFromHex(String s) {
  return new BigInteger(s, 16);
}
static String assertPossibleMD5(String s) {
  if (!possibleMD5(s))
    throw fail("Not an MD5 signature: " + s);
  return s;
}
static String regexpFirstGroup(String pat, String s) {
  Matcher m = Pattern.compile(pat).matcher(s);
  if (m.find()) return m.group(1); else return null;
}
static String regexp_globalID() {
  return "[a-z]{" + globalIDLength() + "}";
}
static String prependGlobalIDInNewLine(String s) {
  return prependGlobalIDInNewLine(aGlobalID(), s);
}

static String prependGlobalIDInNewLine(String globalID, String s) {
  return "[id: " + globalID + "]\n\n" + s;
}
static String indentx(String s) {
  return indentx(indent_default, s);
}

static String indentx(int n, String s) {
  return dropSuffix(repeat(' ', n), indent(n, s));
}

static String indentx(String indent, String s) {
  return dropSuffix(indent, indent(indent, s));
}
static String autoUnindent(String s) {
  int n = getIndent(s);
  if (n == 0) return s;
  List<String> l = toLines(s);
  for (int i = 0; i < l(l); i++)
    l.set(i, substring(l.get(i), n));
  return fromLines(l);
}
static String botEditMechList(String name, String text) {
  try {
  
    
  
    if (mechPlaying()) {
      if (eq(mechList_raw(name), text)) return "No change";
      print("Setting mech list " + name + ":");
      printIndent(text);
      return "Would change";
    }
    
    String answer = postPageSilently("http://butter.botcompany.de:8080/mech/raw/bot-list-edit", arrayPlus(muricaCredentials(), "name", name, "text", text));
    print(name + ": " + answer);
    return answer;
  } finally {
    mechList_clearCache(name);
  }
}

static String botEditMechList(String name, List<String> lines) {
  return botEditMechList(name, lines(lines));
}
static long parseLocalDateWithMilliseconds(String s) { try {
  if (s == null) return 0;
  return simpleDateFormat_local("yyyy/MM/dd HH:mm:ss''SSSS").parse(s).getTime();
} catch (Exception __e) { throw rethrow(__e); } }
static void setAll(Object o, Map<String, Object> fields) {
  if (fields == null) return;
  for (String field : keys(fields))
    set(o, field, fields.get(field));
}

static void setAll(Object o, Object... values) {
  //values = expandParams(c.getClass(), values);
  failIfOddCount(values);
  for (int i = 0; i+1 < l(values); i += 2) {
    String field = (String) values[i];
    Object value = values[i+1];
    set(o, field, value);
  }
}
static void arraycopy(Object[] a, Object[] b) {
  int n = min(a.length, b.length);
  for (int i = 0; i < n; i++)
    b[i] = a[i];
}

static void arraycopy(Object src, int srcPos, Object dest, int destPos, int n) {
  System.arraycopy(src, srcPos, dest, destPos, n);
}
static List<String> toLinesFullTrim_java(String text) {
  return toLinesFullTrim(joinLines(map("javaDropComments", toLinesFullTrim(text))));
}

static TreeSet<String> toCaseInsensitiveSet(Collection<String> c) {
  if (isCISet(c)) return (TreeSet) c;
  TreeSet<String> set = caseInsensitiveSet();
  if (c != null) set.addAll(c);
  return set;
}

static TreeSet<String> toCaseInsensitiveSet(String... x) {
  TreeSet<String> set = caseInsensitiveSet();
  addAll(set, x);
  return set;
}
static void vmBus_send(String msg, Object arg) {
  pcallFAll(vm_busListeners_live(), msg, arg);
}
static Set<String> mcFields() {
  return staticFieldNames(mc());
}
static boolean startsWithEndsWith(String s, String prefix, String suffix) {
  return startsWith(s, prefix) && endsWith(s, suffix);
}
static void removeFromMapCI_sync(Map<String, Object> map, String key) {
  if (map == null) return;
  synchronized(getMutex(map)) {
    for (String k : cloneList(keys(map)))
      if (eqic(key, k))
        map.remove(k);
  }
}
static boolean mechList_goToCreator_dont;

static boolean mechList_goToCreator() {
  return eqOneOf(mechMode().readMode, null, "creator") && !mechList_goToCreator_dont && hasMethodNamed(creator(), "mechList_opt_raw");
}
static <A> A callCreatorOpt(String functionName, Object... args) {
  return (A) callOpt(creator(), functionName, args);
}
static String doPostSilently(Map urlParameters, String url) {
  return doPostSilently(makePostData(urlParameters), url);
}

static String doPostSilently(String urlParameters, String url) {
  doPost_silently.set(true);
  return doPost(urlParameters, url);
}
static Object[] concatArrays(Object[]... arrays) {
  int l = 0;
  for (Object[] a : arrays) l += l(a);
  Object[] x = new Object[l];
  int i = 0;
  for (Object[] a : arrays) if (a != null) {
    System.arraycopy(a, 0, x, i, l(a));
    i += l(a);
  }
  return x;
}

static void appendToTextFile(File file, String contents) { try {
  File parentFile = file.getParentFile();
  if (parentFile != null)
    parentFile.mkdirs();
  if (contents != null) {
    FileOutputStream fileOutputStream = new FileOutputStream(file, true);
    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8");
    PrintWriter printWriter = new PrintWriter(outputStreamWriter);
    printWriter.print(contents);
    printWriter.close();
  }
} catch (Exception __e) { throw rethrow(__e); } }

static File remoteMechListMirrorFile(String listName) {
  return newFile(remoteMechListMirrorsDir(), uniqueFileNameUsingMD5_80_v2(upper(listName)));
}
static boolean isLetter(char c) {
  return Character.isLetter(c);
}
static String quickSubstring(String s, int i, int j) {
  if (i == j) return "";
  return s.substring(i, j);
}
static String mechList_opt_raw_fresh(String name) {
  
  
  Object readMode = mechMode().readMode;
  if (readMode instanceof File) {
    String md5Name = uniqueFileNameUsingMD5_80_v2(upper(name));
    return loadTextFileFromZipFile((File) readMode, md5Name);
  }
  

  boolean useLocal = false, useRemote = true;
  if (eq(readMode, "mergeLocalAndRemote"))
    useLocal = true;
  else if (eq(readMode, "local")) {
    useLocal = true; useRemote = false;
  }
   
  String s = "";
  
  if (useRemote) {
    if (eq(mechMode().readMode, "localCopies"))
      s = unnull(loadTextFile(remoteMechListMirrorFile(name)));
    else
      s = serverMechList_raw_fresh(name, true);
  }
  
  if (useLocal)
    s = appendNewLineIfNempty(s) + localMechList_opt_raw_fresh(name);
  
  return s;
}
static void addToContainer(final Container a, final Component b) {
  if (a != null && b != null) { swing(new Runnable() {  public void run() { try {  a.add(b); 
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "a.add(b);"; }}); }
}
static Set vm_generalIdentityHashSet(Object name) {
  synchronized(get(javax(), "generalMap")) {
    Set set = (Set) ( vm_generalMap_get(name));
    if (set == null)
      vm_generalMap_put(name, set = syncIdentityHashSet());
    return set;
  }
}

static Producer<String> javaTokC_noMLS_iterator(final String s) {
  return javaTokC_noMLS_iterator(s, 0);
}

static Producer<String> javaTokC_noMLS_iterator(final String s, final int startIndex) {
  return new Producer<String>() {
    final int l = s.length();
    int i = startIndex;
    
    public String next() {
      if (i >= l) return null;
      
      int j = i;
      char c, d;
      
      // scan for whitespace
      while (j < l) {
        c = s.charAt(j);
        d = j+1 >= l ? '\0' : s.charAt(j+1);
        if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
          ++j;
        else if (c == '/' && d == '*') {
          do ++j; while (j < l && !s.substring(j, Math.min(j+2, l)).equals("*/"));
          j = Math.min(j+2, l);
        } else if (c == '/' && d == '/') {
          do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0);
        } else
          break;
      }
      
      i = j;
      if (i >= l) return null;
      c = s.charAt(i);
      d = i+1 >= l ? '\0' : s.charAt(i+1);
  
      // scan for non-whitespace
      if (c == '\'' || c == '"') {
        char opener = c;
        ++j;
        while (j < l) {
          if (s.charAt(j) == opener || s.charAt(j) == '\n') { // end at \n to not propagate unclosed string literal errors
            ++j;
            break;
          } else if (s.charAt(j) == '\\' && j+1 < l)
            j += 2;
          else
            ++j;
        }
      } else if (Character.isJavaIdentifierStart(c))
        do ++j; while (j < l && (Character.isJavaIdentifierPart(s.charAt(j)) || "'".indexOf(s.charAt(j)) >= 0)); // for stuff like "don't"
      else if (Character.isDigit(c)) {
        do ++j; while (j < l && Character.isDigit(s.charAt(j)));
        if (j < l && s.charAt(j) == 'L') ++j; // Long constants like 1L
      } else
        ++j;
        
      String t = quickSubstring(s, i, j);
      i = j;
      return t;
    }
  };
}

static Producer<String> javaTokC_noMLS_onReader(final BufferedReader r) {
  final class X implements Producer<String> {
    StringBuilder buf = new StringBuilder(); // stores from "i"
    char c, d, e = 'x'; // just not '\0'
    
    X() {
      // fill c, d and e
      nc();
      nc();
      nc();
    }
    
    // get next character(s) into c, d and e
    void nc() { try {
      c = d;
      d = e;
      if (e == '\0') return;
      int i = r.read();
      e = i < 0 ? '\0'
        : i == '\0' ? '_' // shouldn't happen anymore
        : (char) i;
    } catch (Exception __e) { throw rethrow(__e); } }
    
    void ncSave() {
      if (c != '\0') {
        buf.append(c);
        nc();
      }
    }
    
    public String next() {
      // scan for whitespace
      while (c != '\0') {
        if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
          nc();
        else if (c == '/' && d == '*') {
          do nc(); while (c != '\0' && !(c == '*' && d == '/'));
          nc(); nc();
        } else if (c == '/' && d == '/') {
          do nc(); while (c != '\0' && "\r\n".indexOf(c) < 0);
        } else
          break;
      }
      
      if (c == '\0') return null;

      // scan for non-whitespace
      if (c == '\'' || c == '"') {
        char opener = c;
        ncSave();
        while (c != '\0') {
          if (c == opener || c == '\n') { // end at \n to not propagate unclosed string literal errors
            ncSave();
            break;
          } else if (c == '\\') {
            ncSave();
            ncSave();
          } else
            ncSave();
        }
      } else if (Character.isJavaIdentifierStart(c))
        do ncSave(); while (Character.isJavaIdentifierPart(c) || c == '\''); // for stuff like "don't"
      else if (Character.isDigit(c)) {
        do ncSave(); while (Character.isDigit(c));
        if (c == 'L') ncSave(); // Long constants like 1L
      } else
        ncSave();
        
      String t = buf.toString();
      buf.setLength(0);
      return t;
    }
  }
  
  return new X();
}

static String unquoteUsingCharArray(String s, char[] buf) {
  if (s == null) return null;
  if (startsWith(s, '[')) {
    int i = 1;
    while (i < s.length() && s.charAt(i) == '=') ++i;
    if (i < s.length() && s.charAt(i) == '[') {
      String m = s.substring(1, i);
      if (s.endsWith("]" + m + "]"))
        return s.substring(i+1, s.length()-i-1);
    }
  }
  
  if (s.length() > 1) {
    char c = s.charAt(0);
    if (c == '\"' || c == '\'') {
      int l = endsWith(s, c) ? s.length()-1 : s.length();
      if (l > buf.length) return unquote(s); // fallback
      int n = 0;
  
      for (int i = 1; i < l; i++) {
        char ch = s.charAt(i);
        if (ch == '\\') {
          char nextChar = (i == l - 1) ? '\\' : s.charAt(i + 1);
          // Octal escape?
          if (nextChar >= '0' && nextChar <= '7') {
              String code = "" + nextChar;
              i++;
              if ((i < l - 1) && s.charAt(i + 1) >= '0'
                      && s.charAt(i + 1) <= '7') {
                  code += s.charAt(i + 1);
                  i++;
                  if ((i < l - 1) && s.charAt(i + 1) >= '0'
                          && s.charAt(i + 1) <= '7') {
                      code += s.charAt(i + 1);
                      i++;
                  }
              }
              buf[n++] = (char) Integer.parseInt(code, 8);
              continue;
          }
          switch (nextChar) {
          case '\"': ch = '\"'; break;
          case '\\': ch = '\\'; break;
          case 'b': ch = '\b'; break;
          case 'f': ch = '\f'; break;
          case 'n': ch = '\n'; break;
          case 'r': ch = '\r'; break;
          case 't': ch = '\t'; break;
          case '\'': ch = '\''; break;
          // Hex Unicode: u????
          case 'u':
              if (i >= l - 5) {
                  ch = 'u';
                  break;
              }
              int code = Integer.parseInt(
                      "" + s.charAt(i + 2) + s.charAt(i + 3)
                         + s.charAt(i + 4) + s.charAt(i + 5), 16);
              char[] x = Character.toChars(code);
              int lx = x.length;
              for (int j = 0; j < lx; j++)
                buf[n++] = x[j];
              i += 5;
              continue;
          default:
            ch = nextChar; // added by Stefan
          }
          i++;
        }
        buf[n++] = ch;
      }
      return new String(buf, 0, n);
    }
  }
    
  return s; // not quoted - return original
}
static boolean structure_isMarker(String s, int i, int j) {
  if (i >= j) return false;
  if (s.charAt(i) != 'm') return false;
  ++i;
  while (i < j) {
    char c = s.charAt(i);
    if (c < '0' || c > '9') return false;
    ++i;
  }
  return true;
}
static String internIfLongerThan(String s, int l) {
  return s == null ? null : l(s) >= l ? intern(s) : s;
}
static char unquoteCharacter(String s) {
  assertTrue(s.startsWith("'") && s.length() > 1);
  return unquote("\"" + s.substring(1, s.endsWith("'") ? s.length()-1 : s.length()) + "\"").charAt(0);
}
static double parseDouble(String s) {
  return Double.parseDouble(s);
}
static boolean isLongConstant(String s) {
  if (!s.endsWith("L")) return false;
  s = s.substring(0, l(s)-1);
  return isInteger(s);
}
static SortedMap synchronizedTreeMap() {
  return synchroTreeMap();
}
static Map synchronizedMap() {
  return synchroMap();
}
static boolean[] boolArrayFromBytes(byte[] a, int n) {
  boolean[] b = new boolean[n];
  int m = min(n, l(a)*8);
  for (int i = 0; i < m; i++)
    b[i] = (a[i/8] & 1 << (i & 7)) != 0;
  return b;
}
static HashMap<String,Class> findClass_cache = new HashMap();

// currently finds only inner classes of class "main"
// returns null on not found
// this is the simple version that is not case-tolerant
static Class findClass(String name) {
  synchronized(findClass_cache) {
    if (findClass_cache.containsKey(name))
      return findClass_cache.get(name);
      
    if (!isJavaIdentifier(name)) return null;
    
    Class c;
    try {
      c = Class.forName("main$" + name);
    } catch (ClassNotFoundException e) {
      c = null;
    }
    findClass_cache.put(name, c);
    return c;
  }
}
static <A> A nuStubInnerObject(Class<A> c) {
  return nuStubInnerObject(c, null);
}

static <A> A nuStubInnerObject(Class<A> c, Object classFinder) { try {
  Class outerType = getOuterClass(c, classFinder);
  Constructor m = c.getDeclaredConstructor(outerType);
  m.setAccessible(true);
  return (A) m.newInstance(new Object[] {null});
} catch (Exception __e) { throw rethrow(__e); } }
static Map<Class, Constructor> nuEmptyObject_cache = newDangerousWeakHashMap();

static <A> A nuEmptyObject(Class<A> c) { try {
  Constructor ctr;
  
  synchronized(nuEmptyObject_cache) {
    ctr = nuEmptyObject_cache.get(c);
    if (ctr == null) {
      nuEmptyObject_cache.put(c, ctr = nuEmptyObject_findConstructor(c));
      ctr.setAccessible(true);
    }
  }

  try {
    return (A) ctr.newInstance();
  } catch (InstantiationException e) {
    if (empty(e.getMessage()))
      if ((c.getModifiers() & Modifier.ABSTRACT) != 0)
        throw fail("Can't instantiate abstract class " + className(c), e);
      else
        throw fail("Can't instantiate " + className(c), e);  
    else throw rethrow(e);
  }
} catch (Exception __e) { throw rethrow(__e); } }

static Constructor nuEmptyObject_findConstructor(Class c) {
  for (Constructor m : c.getDeclaredConstructors())
    if (m.getParameterTypes().length == 0)
      return m;
  throw fail("No default constructor declared in " + c.getName());
}

static void setOptAllDyn(DynamicObject o, Map<String, Object> fields) {
  if (fields == null) return;
  HashMap<String, Field> fieldMap = instanceFieldsMap(o);
  for (Map.Entry<String, Object> e : fields.entrySet()) {
    String field = e.getKey();
    Object val = e.getValue();
    boolean has = fieldMap.containsKey(field);
    if (has)
      setOpt(o, field, val);
    else {
      o.fieldValues.put(intern(field), val);
      
    }
  }
}
static void setOptAll_pcall(Object o, Map<String, Object> fields) {
  if (fields == null) return;
  for (String field : keys(fields))
    try { setOpt(o, field, fields.get(field)); } catch (Throwable __e) { print(exceptionToStringShort(__e)); }
}

static void setOptAll_pcall(Object o, Object... values) {
  //values = expandParams(c.getClass(), values);
  warnIfOddCount(values);
  for (int i = 0; i+1 < l(values); i += 2) {
    String field = (String) values[i];
    Object value = values[i+1];
    try { setOpt(o, field, value); } catch (Throwable __e) { print(exceptionToStringShort(__e)); }
  }
}
static String intern(String s) {
  return fastIntern(s);
}
static void pcallOpt_noArgs(Object o, String method) {
  try { callOpt_noArgs(o, method); } catch (Throwable __e) { _handleException(__e); }
}
static int[] toIntArray(List<Integer> l) {
  int[] a = new int[l(l)];
  for (int i = 0; i < a.length; i++)
    a[i] = l.get(i);
  return a;
}
static boolean warn_on = true;

static void warn(String s) {
  if (warn_on)
    print("Warning: " + s);
}

static void warn(String s, List<String> warnings) {
  warn(s);
  if (warnings != null)
    warnings.add(s);
}
static boolean eqicOneOf(String s, String... l) {
  for (String x : l) if (eqic(s, x)) return true; return false;
}
static JList makeList(String... strings) {
  JList list = new JList();
  fillListWithStrings(list, strings);
  return list;
}
static String repeat(char c, int n) {
  n = Math.max(n, 0);
  char[] chars = new char[n];
  for (int i = 0; i < n; i++)
    chars[i] = c;
  return new String(chars);
}

static <A> List<A> repeat(A a, int n) {
  n = Math.max(n, 0);
  List<A> l = new ArrayList(n);
  for (int i = 0; i < n; i++)
    l.add(a);
  return l;
}

static <A> List<A> repeat(int n, A a) {
  return repeat(a, n);
}
static List<String> splitAtDoubleArrow_bothDirections(String s) {
  return splitAtDoubleArrow_bothDirections(javaTok(s));
}

static List<String> splitAtDoubleArrow_bothDirections(List<String> tok) {
  List<String> l = splitAtDoubleArrow(tok);
  if (l(l) != 2) l = reversedList(splitAtDoubleLeftArrow(tok));
  return l;
}
static boolean isCIMap(Map m) {
  return m instanceof TreeMap && ((TreeMap) m).comparator() == caseInsensitiveComparator();
}
  static Method findMethod(Object o, String method, Object... args) {
    try {
      if (o == null) return null;
      if (o instanceof Class) {
        Method m = findMethod_static((Class) o, method, args, false);
        if (m == null) return null;
        m.setAccessible(true);
        return m;
      } else {
        Method m = findMethod_instance(o, method, args, false);
        if (m == null) return null;
        m.setAccessible(true);
        return m;
      }
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }

  static Method findMethod_static(Class c, String method, Object[] args, boolean debug) {
    Class _c = c;
    while (c != null) {
      for (Method m : c.getDeclaredMethods()) {
        if (debug)
          System.out.println("Checking method " + m.getName() + " with " + m.getParameterTypes().length + " parameters");;
        if (!m.getName().equals(method)) {
          if (debug) System.out.println("Method name mismatch: " + method);
          continue;
        }

        if ((m.getModifiers() & Modifier.STATIC) == 0 || !findMethod_checkArgs(m, args, debug))
          continue;

        return m;
      }
      c = c.getSuperclass();
    }
    return null;
  }

  static Method findMethod_instance(Object o, String method, Object[] args, boolean debug) {
    Class c = o.getClass();
    while (c != null) {
      for (Method m : c.getDeclaredMethods()) {
        if (debug)
          System.out.println("Checking method " + m.getName() + " with " + m.getParameterTypes().length + " parameters");;
        if (m.getName().equals(method) && findMethod_checkArgs(m, args, debug))
          return m;
      }
      c = c.getSuperclass();
    }
    return null;
  }

  static boolean findMethod_checkArgs(Method m, Object[] args, boolean debug) {
    Class<?>[] types = m.getParameterTypes();
    if (types.length != args.length) {
      if (debug)
        System.out.println("Bad parameter length: " + args.length + " vs " + types.length);
      return false;
    }
    for (int i = 0; i < types.length; i++)
      if (!(args[i] == null || isInstanceX(types[i], args[i]))) {
        if (debug)
          System.out.println("Bad parameter " + i + ": " + args[i] + " vs " + types[i]);
        return false;
      }
    return true;
  }


static HashMap<Set<String>, Class> loadFunctions_cache = new HashMap();
static Lock loadFunctions_cached_lock = lock();

static boolean loadFunctions_preferDiskCache; // Note: This may use outdated versions of standard functions

static Class loadFunctions_cached(String... functions) {
  return loadFunctions_cached(asList(functions));
}

static Class loadFunctions_cached(Collection<String> functions) {
  TreeSet<String> functionSet = new TreeSet<String>(functions);
  Lock __1980 = loadFunctions_cached_lock; lock(__1980); try {
  Class c = loadFunctions_cache.get(functionSet);
  if (c != null) return c;
  String transpilation = loadFunctions_preferDiskCache ? getTranspiledStandardFunctionsIfCached(functionSet) : null;
  if (transpilation != null) {
    // TODO: LIBS
    print("Loading " + joinWithPlus(functionSet));
    c = veryQuickJava_finish(transpilation, emptyList());
  }
  
  if (c == null) c = loadFunctions(functionSet);
  
  loadFunctions_cache.put(functionSet, c);
  assertNotNull(loadFunctions_cache.get(functionSet));
  return c;
} finally { unlock(__1980); } }
static boolean preciseCall_debug;

static Object preciseCall(Object o, String method, Object... args) { try {
  if (o instanceof Class) {
    List<Method> methods = findMethodsNamed2((Class) o, method, true);
    Lowest<Method> best = new Lowest();
    for (Method m : methods) {
      int score = methodApplicabilityScore(m, args);
      if (score < Integer.MAX_VALUE) {
        if (preciseCall_debug)
          print("Method score: " + m + " " + score);
        best.put(m, score);
      }
    }
    Method m = best.get();
    if (m == null) throw fail("No suitable method found: " + methodSignature(method, args));
    return invokeMethod(m, null, args);
  } else {
    throw todo();
  }
} catch (Exception __e) { throw rethrow(__e); } }

static String dropSuffixIgnoreCase(String suffix, String s) {
  return ewic(s, suffix) ? s.substring(0, l(s)-l(suffix)) : s;
}
static ThreadLocal<Object> print_byThread() {
  synchronized(print_byThread_lock) {
    if (print_byThread == null)
      print_byThread = new ThreadLocal();
  }
  return print_byThread;
}
static TreeSet<String> ciSet() {
  return caseInsensitiveSet();
}
static TreeSet<String> mechSetCI(String name) {
  return mechCISet(name);
}
static boolean isAngleBracketed(String s) {
  return s.startsWith("<") && s.endsWith(">");
}
static String dropTrailingNumberFromAngleBracketCategory(String s) {
  if (!isAngleBracketed(s)) return s;
  int i = l(s)-1;
  while (i > 1 && isDigit(s.charAt(i-1))) --i;
  if (i == l(s)-1) return s;
  return substring(s, 0, i) + ">";
}
static <A> LinkedHashSet<A> litorderedset(A... items) {
  LinkedHashSet<A> set = new LinkedHashSet();
  for (A a : items) set.add(a);
  return set;
}
static String mechLanguagePrefix() {
  return ai_mechLanguagePrefix();
}
static String firstToUpper(String s) {
  if (empty(s)) return s;
  return Character.toUpperCase(s.charAt(0)) + s.substring(1);
}
static String plural_mech(String s) {
  { String _a_2004 = mechMapCI("Singular => Plural").get(s); if (!empty(_a_2004)) return _a_2004; }
  return plural(s);
}
static <A> List<A> replace(List<A> l, A a, A b) {
  for (int i = 0; i < l(l); i++)
    if (eq(l.get(i), a))
      l.set(i, b);
  return l;
}

static String replace(String s, String a, String b) {
  return s == null ? null : a == null || b == null ? s : s.replace(a, b);
}
static String deAngleBracket(String s) {
  return isAngleBracketed(s) ? dropFirstAndLast(s) : s;
}
// name -> (name, parsed map)
static Map<String, Pair<String, MultiMap<String, String>>> mechMultiMapCI_cache = synchroMap();

static MultiMap<String, String> mechMultiMapCI(String listName) {
  Pair<String, MultiMap<String, String>> p = mechMultiMapCI_cache.get(listName);
  String text = mechList_opt_raw(listName);
  if (p != null && eq(p.a, text)) return p.b;
  MultiMap<String, String> map = parseDoubleArrowMultiMapCI(tlft(text));
  p = pair(text, map);
  mechMultiMapCI_cache.put(listName, p);
  return p.b;
}
static String javaDropAllComments(String s) {
  List<String> tok = javaTok(s);
  for (int i = 0; i < l(tok); i += 2)
    tok.set(i, tok_javaDropCommentsFromWhitespace(tok.get(i)));
  return join(tok);
}
static String trailingAngleBracketStuff(String s) {
  if (!endsWith(s, ']')) return null;
  return substring(s, smartLastIndexOf(s, '[') + 1, l(s)-1);
}
static String dropTrailingAngleBracketStuff(String s) {
  if (!endsWith(s, ']')) return s;
  return trimSubstring(s, 0, lastIndexOf(s, '['));
}
static List<String> javaTokWithAngleBracketsC(String s) {
  return codeTokens(javaTokWithAngleBrackets(s));
}
static boolean startsWithDigit(String s) {
  return nempty(s) && isDigit(s.charAt(0));
}
static List<PTElement> ai_parseTree_getChildren(AI_BottomUpParser1 parser, List<WeightedProduction> productions, PTElement e) {
  if (e.children == null) {
    e.children = ai_parseTree_makeChildren(parser, productions, e);
    for (PTElement child : unnull(e.children)) child.parent = e;
  }
  return e.children;
}

static List<PTElement> ai_parseTree_makeChildren(AI_BottomUpParser1 parser, List<WeightedProduction> productions, final PTElement e) {
  if (e instanceof ChooseCategory)
    return map(((ChooseCategory) e).categories, new F1<String, Object>() { Object get(String cat) { try { return  new HaveCategory(((ChooseCategory) e).word, cat) ; } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "new HaveCategory(((ChooseCategory) e).word, cat)"; }});
  if (e instanceof HaveCategory) {
    final HaveCategory h = (HaveCategory) ( e);
    List<ai_Reconstructed> l = ai_buParser1_reconstructedProductions(parser, productions, h.word, h.category);
    return map(l, new F1<ai_Reconstructed, Object>() { Object get(ai_Reconstructed r) { try { return 
      r.production.b == null
        ? new HaveCategory(r.a, r.production.a, r)
        : new ChoosePart(h.word, r)
    ; } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "r.production.b == null\r\n        ? new HaveCategory(r.a, r.production.a, r)\r\n ..."; }});
  }
  if (e instanceof ChoosePart) {
    ai_Reconstructed r = ((ChoosePart) e).reconstruction;
    return (List) ll(new HaveCategory(r.a, r.production.a),
      new HaveCategory(r.b, r.production.b));
  }
  return null;
}


static TimedCache<Map<String, Map<String, Set<String>>>> ai_antiWordCategoriesWithElements_cache = new TimedCache("ciMap", 10.0); // language -> category -> words

static Map<String, Set<String>> ai_antiWordCategoriesWithElements() {
  Map<String, Map<String, Set<String>>> languages = ai_antiWordCategoriesWithElements_cache.get();
  Map<String, Set<String>> map = languages.get(ai_language());
  if (map == null)
    languages.put(ai_language(), map = ai_antiWordCategoriesWithElements_make());
  return map;
}

static Map<String, Set<String>> ai_antiWordCategoriesWithElements_make() {
  final Map<String, Set<String>> wordCategories = ciMap();
  for (String cat : mL("All " + ai_language() + " word categories"))
    putIfValueNempty(wordCategories, cat, combinedMechSetsCI(ai_antiMechListNamesForAngleBracketCategory(cat)));
  return wordCategories;
}



static float avg(float a, float b) {
  return (a+b)/2;
}

static double avg(double a, double b) {
  return (a+b)/2;
}
static ThreadLocal<Map<String, Map<String, Double>>> ai_getWordToCategoryWeight_map = newThreadLocal();

static Double ai_getWordToCategoryWeight(String word, String category) {
  return mapGet(mapGet(ai_getWordToCategoryWeight_map.get(), word), category);
}
static double collectMaxDouble(Collection c, String field) {
  double x = Double.NEGATIVE_INFINITY;
  for (Object o : unnull(c)) {
    Double d = (Double) ( getOpt(o, field));
    if (d != null)
      x = max(x, d);
  }
  return x;
}

// f: A -> Comparable
static <A> List<A> sortInPlaceByCalculatedFieldDesc(List<A> l, F1<A, ?> f) {
  return sortInPlaceByCalculatedFieldDesc_f1(l, f);
}
static F1<PTElement, Object> ai_parseTree_sortingFunction() {
  return new F1<PTElement, Object>() { Object get(PTElement e) { try { return  e.totalWeight ; } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "e.totalWeight"; }};
}
static BufferedReader bufferedReader(Reader r) {
  return r instanceof BufferedReader ? (BufferedReader) r : _registerIOWrap(new BufferedReader(r), r);
}
static <A> CloseableIterableIterator<A> iteratorFromFunction_f0_autoCloseable(final F0<A> f, final AutoCloseable closeable) {
  class IFF2 extends CloseableIterableIterator<A> {
    A a;
    boolean done;
    
    public boolean hasNext() {
      getNext();
      return !done;
    }
    
    public A next() {
      getNext();
      if (done) throw fail();
      A _a = a;
      a = null;
      return _a;
    }
    
    void getNext() {
      if (done || a != null) return;
      a = f.get();
      done = a == null;
    }
    
    public void close() throws Exception {
      if (closeable != null) closeable.close();
    }
  };
  return new IFF2();
}
static String readLineFromReaderWithClose(BufferedReader r) { try {
  String s = r.readLine();
  if (s == null) r.close();
  return s;
} catch (Exception __e) { throw rethrow(__e); } }
static AutoCloseable _wrapIOCloseable(final AutoCloseable c) {
  return c == null ? null : new AutoCloseable() { public void close() throws Exception {
    c.close();
    _registerIO(c, null, false);
  }};
}

static FileInputStream newFileInputStream(File path) throws IOException {
  return newFileInputStream(path.getPath());
}

static FileInputStream newFileInputStream(String path) throws IOException {
  FileInputStream f = new FileInputStream(path);
  _registerIO(f, path, true);
  return f;
}
static Set similarEmptySet(Collection m) {
  if (m instanceof TreeSet) return new TreeSet(((TreeSet) m).comparator());
  if (m instanceof LinkedHashSet) return new LinkedHashSet();
  
  return new HashSet();
}
static <A> boolean containsAll(Collection<? extends A> a, Collection<A> b) {
  for (A o : b)
    if (!a.contains(o))
      return false;
  return true;
}

static <A> boolean containsAll(Collection<? extends A> a, A... b) {
  for (A o : b)
    if (!a.contains(o))
      return false;
  return true;
}
static void nlLogic_visitShallow(Exp e, VF1<Object> visitor) {
  if (e == null) return;
  callF(visitor, e);
  if (e instanceof And) {
    nlLogic_visitShallow(((And) e).a, visitor);
    nlLogic_visitShallow(((And) e).b, visitor);
  } else if (e instanceof ExpNot)
    nlLogic_visitShallow(((ExpNot) e).a, visitor);
}

static <A> A callCreator(String functionName, Object... args) {
  return (A) call(creator(), functionName, args);
}
// mainJava is a complete program, but without the !752/!759 at the top
// returns link to main class
static Class veryQuickJava(CharSequence mainJava) {
  return veryQuickJava3(str(mainJava)); // Latest version with internal compiler bot.
}
static boolean standardFunctionAlwaysReturnsVoid(String name) {
  return tok_staticFunctionAlwaysReturnsVoid(javaTok(textOfStandardFunction_cached(name)), name);
}
// i must point at the (possibly imaginary) opening bracket (any of the 2 types, not type parameters)
// index returned is index of closing bracket + 1
static int findEndOfBracketPart(List<String> cnc, int i) {
  int j = i+2, level = 1;
  while (j < cnc.size()) {
    if (eqOneOf(cnc.get(j), "{", "(")) ++level;
    else if (eqOneOf(cnc.get(j), "}", ")")) --level;
    if (level == 0)
      return j+1;
    ++j;
  }
  return cnc.size();
}
static List<String> tok_combineCurlyBrackets_keep(List<String> tok) {
  List<String> l = new ArrayList();
  for (int i = 0; i < l(tok); i++) {
    String t = tok.get(i);
    if (odd(i) && eq(t, "{")) {
      int j = findEndOfCurlyBracketPart(tok, i);
      l.add(joinSubList(tok, i, j));
      i = j-1;
    } else
      l.add(t);
  }
  return l;
}
static boolean startsWithAndEndsWith(String s, String prefix, String suffix) {
  return startsWith(s, prefix) && endsWith(s, suffix);
}
static String tok_unCurlyBracket(String s) {
  return isCurlyBraced(s) ? join(dropFirstThreeAndLastThree(javaTok(s))) : s;
}
static boolean checkCondition(Object condition, Object... args) {
  return isTrue(callF(condition, args));
}


static String javaDropComments(String s) {
  return javaDropAllComments(s);
}
static <A> IterableIterator<A> iteratorFromFunction_withEndMarker_f0(final F0<A> f) {
  class IFF2 extends IterableIterator<A> {
    A a;
    boolean have, done;
    
    public boolean hasNext() {
      getNext();
      return !done;
    }
    
    public A next() {
      getNext();
      if (done) throw fail();
      A _a = a;
      a = null;
      have = false;
      return _a;
    }
    
    void getNext() {
      if (done || have) return;
      a = f.get();
      have = true;
      if (a == iteratorFromFunction_endMarker)
        done = true;
    }
  };
  return new IFF2();
}
static List<String> splitAtDoubleLeftArrow(String s) {
  return splitAtDoubleLeftArrow(javaTok(s));
}

static List<String> splitAtDoubleLeftArrow(List<String> tok) {
  List<String> l = new ArrayList();
  int i = 0;
  while (i < l(tok)) {
    int j = indexOfSubList(tok, ll("<", "", "="), i);
    if (i >= l(tok)) break;
    if (j < 0) j = l(tok);
    l.add(trimJoin(tok.subList(i, j)));
    i = j+3;
  }
  return l;
}
static betterCIComparator_C betterCIComparator_instance;

static betterCIComparator_C betterCIComparator() {
  if (betterCIComparator_instance == null)
    betterCIComparator_instance = new betterCIComparator_C();
  return betterCIComparator_instance;
}

static class betterCIComparator_C implements Comparator<String> {
  public int compare(String s1, String s2) {
    if (s1 == null) return s2 == null ? 0 : -1;
    if (s2 == null) return 1;
  
    int n1 = s1.length();
    int n2 = s2.length();
    int min = Math.min(n1, n2);
    for (int i = 0; i < min; i++) {
        char c1 = s1.charAt(i);
        char c2 = s2.charAt(i);
        if (c1 != c2) {
            c1 = Character.toUpperCase(c1);
            c2 = Character.toUpperCase(c2);
            if (c1 != c2) {
                c1 = Character.toLowerCase(c1);
                c2 = Character.toLowerCase(c2);
                if (c1 != c2) {
                    // No overflow because of numeric promotion
                    return c1 - c2;
                }
            }
        }
    }
    return n1 - n2;
  }
}
static char charPlus(char a, int b) {
  return (char) (((int) a) + b);
}
// better modulo that gives positive numbers always
static int mod(int n, int m) {
  return (n % m + m) % m;
}

static long mod(long n, long m) {
  return (n % m + m) % m;
}

static BigInteger mod(BigInteger n, int m) {
  return n.mod(bigint(m));
}

static double mod(double n, double m) {
  return (n % m + m) % m;
}

static String div(Object contents, Object... params) {
  return hfulltag("div", contents, params);
}

static BigInteger div(BigInteger a, BigInteger b) {
  return a.divide(b);
}

static BigInteger div(BigInteger a, int b) {
  return a.divide(bigint(b));
}
static boolean possibleMD5(String s) { return isMD5(s); }
static int indent_default = 2;

static String indent(int indent) {
  return repeat(' ', indent);
}

static String indent(int indent, String s) {
  return indent(repeat(' ', indent), s);
}

static String indent(String indent, String s) {
  return indent + s.replace("\n", "\n" + indent);
}

static String indent(String s) {
  return indent(indent_default, s);
}

static List<String> indent(String indent, List<String> lines) {
  List<String> l = new ArrayList();
  for (String s : lines)
    l.add(indent + s);
  return l;
}
static int getIndent(String s) {
  int n = Integer.MAX_VALUE;
  for (String l : toLines(s)) {
    int i = 0;
    while (i < l(l) && l.charAt(i) == ' ')
      ++i;
    n = min(n, i);
  }
  return n;
}

static boolean mechPlaying() {
  return mechPlay_on();
}
static void failIfOddCount(Object... list) {
  if (odd(l(list)))
    throw fail("Odd list size: " + list);
}
static String joinLines(List<String> lines) {
  return fromLines(lines);
}

static String joinLines(String glue, String text) {
  return join(glue, toLines(text));
}
static boolean isCISet(Collection<String> l) {
  return l instanceof TreeSet && ((TreeSet) l).comparator() == caseInsensitiveComparator();
}
static Set<String> staticFieldNames(Object o) {
  TreeSet<String> fields = new TreeSet();
  Class _c = _getClass(o);
  do {
    for (Field f : _c.getDeclaredFields())
      if ((f.getModifiers() & Modifier.STATIC) != 0)
        fields.add(f.getName());
    _c = _c.getSuperclass();
  } while (_c != null);
  return fields;
}
static Object getMutex(Object o) {
  return collectionMutex(o);
}
// This is a bit rough... finds static and non-static methods.
// It's usually just for error messages though, so no worries.

static boolean hasMethodNamed(Object obj, String method) {
  if (obj == null) return false;
  if (obj instanceof Class)
    return hasMethodNamed((Class) obj, method);
  return hasMethodNamed(obj.getClass(), method);
}

static boolean hasMethodNamed(Class c, String method) {
  while (c != null) {
    for (Method m : c.getDeclaredMethods())
      if (m.getName().equals(method))
        return true;
    c = c.getSuperclass();
  }
  return false;
}
static ThreadLocal<Boolean> doPost_silently = new ThreadLocal();
static ThreadLocal<Long> doPost_timeout = new ThreadLocal();

static String doPost(Map urlParameters, String url) {
  return doPost(makePostData(urlParameters), url);
}

static String doPost(String urlParameters, String url) { try {
  URL _url = new URL(url);
  ping();
  return doPost(urlParameters, _url.openConnection(), _url);
} catch (Exception __e) { throw rethrow(__e); } }

static String doPost(String urlParameters, URLConnection conn, URL url) { try {
  boolean silently = isTrue(optParam(doPost_silently));
  Long timeout = optParam(doPost_timeout);
  setHeaders(conn);

  int l = lUtf8(urlParameters);
  if (!silently)
    print("Sending POST request: " + hideCredentials(url) + " (" + l + " bytes)");
      
  // connect and do POST
  if (timeout != null) setURLConnectionTimeouts(conn, timeout);
  ((HttpURLConnection) conn).setRequestMethod("POST");
  conn.setDoOutput(true);
  conn.setRequestProperty("Content-Length", str(l));

  OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
  writer.write(urlParameters);
  writer.flush();

  loadPage_charset.set("UTF-8");
  String contents = loadPage(conn, url, false);
  writer.close();
  return contents;
} catch (Exception __e) { throw rethrow(__e); } }
static File remoteMechListMirrorsDir() {
  return javaxDataDir("Remote Mech Lists");
}
static String uniqueFileNameUsingMD5_80_v2(String fullName) {
  return uniqueFileNameUsingMD5_80_v2(fullName, md5(fullName));
}

static String uniqueFileNameUsingMD5_80_v2(String fullName, String md5) {
  return takeFirst(80-33, fileNameEncode(fullName)) + " - " + md5;
}
static String upper(String s) {
  return s == null ? null : s.toUpperCase();
}
static String loadTextFileFromZipFile(File inZip, String fileName) { try {
  if (!fileExists(inZip)) return null;
  ZipFile zip = new ZipFile(inZip);
  try {
    return loadTextFileFromZipFile(zip, fileName);
  } finally {
    zip.close();
  }
} catch (Exception __e) { throw rethrow(__e); } }
    
static String loadTextFileFromZipFile(ZipFile zip, String fileName) { try {
  ZipEntry entry = zip.getEntry(fileName);
  if (entry == null) return null;
  InputStream fin = zip.getInputStream(entry);
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  copyStream(fin, baos);
  fin.close();
  return fromUTF8(baos.toByteArray());
} catch (Exception __e) { throw rethrow(__e); } }
static boolean serverMechList_raw_fresh_verbose;

static String serverMechList_raw_fresh(String name) {
  return serverMechList_raw_fresh(name, false);
}

static String serverMechList_raw_fresh(String name, boolean opt) {
  Lock __2110 = downloadLock(); lock(__2110); try {
  if (serverMechList_raw_fresh_verbose)
    print("serverMechList_raw_fresh " + name);
  String text = null;
  try { text = loadTextFile(remoteMechListMirrorFile(name)); } catch (Throwable __e) { _handleException(__e); }
  Object[] params = muricaCredentialsPlus("md5" , md5OrNull(text), "l" , l(text), "opt" , opt ? 1 : 0, "withStatus" , 1);
  String url = "http://butter.botcompany.de:8080/mech/raw/list-text/" + urlencode(name);
  String page = postPageSilently(url, params);
  Map map = jsonDecodeMap(page);
  boolean same = eq(map.get("Same"), true);
  boolean appended = eq(map.get("Appended"), true);
  if (serverMechList_raw_fresh_verbose)
    print("Mech list " + name + ": " + (appended ? "appended" : same ? "same" : "downloaded"));
  saveTextFile(remoteMechListMirrorMetaFile(name), struct(getMultipleKeys(map, "Name", "Status")));
  if (same) return text;
  if (appended) text += (String) map.get("Text");
  else text = (String) map.get("Text");
  saveTextFile(remoteMechListMirrorFile(name), text);
  File nameFile = remoteMechListMirrorNameFile(name);
  if (!fileExists(nameFile)) {
    String actualName = or((String) map.get("Name"), name);
    saveTextFile(nameFile, actualName);
  }
  return text;
} finally { unlock(__2110); } }
static String appendNewLineIfNempty(String s) {
  return empty(s) ? "" : s + "\n";
}
static String localMechList_opt_raw_fresh(String name) {
  return unnull(loadTextFile(localMechListFile(name)));
}
static <A> Set<A> syncIdentityHashSet() {
  return (Set) synchronizedSet(identityHashSet());
}
static SortedMap synchroTreeMap() {
  return Collections.synchronizedSortedMap(new TreeMap());
}

static Class getOuterClass(Class c) {
  return getOuterClass(c, null);
}

static Class getOuterClass(Class c, Object classFinder) { try {
  String s = c.getName();
  int i = s.lastIndexOf('$');
  String name = substring(s, 0, i);
  if (classFinder != null) return (Class) callF(classFinder, name);
  return Class.forName(name);
} catch (Exception __e) { throw rethrow(__e); } }
static HashMap<String, Field> instanceFieldsMap(Object o) {
  Class c = o.getClass();
  HashMap<String, Field> map;
  synchronized(getOpt_cache) {
    map = getOpt_cache.get(c);
    if (map == null)
      map = getOpt_makeCache(c);
  }
  return map;
}

static void warnIfOddCount(Object... list) {
  if (odd(l(list)))
    printStackTrace("Odd list size: " + list);
}
static Method fastIntern_method;

static String fastIntern(String s) { try {
  if (s == null) return null;
  if (fastIntern_method == null) {
    fastIntern_method = findMethodNamed(javax(), "internPerProgram");
    if (fastIntern_method == null) upgradeJavaXAndRestart();
  }
    
  return (String) fastIntern_method.invoke(null, s);
} catch (Exception __e) { throw rethrow(__e); } }
static Map<Class, HashMap<String, Method>> callOpt_noArgs_cache = newDangerousWeakHashMap();

static Object callOpt_noArgs(Object o, String method) { try {
  if (o == null) return null;
  if (o instanceof Class)
    return callOpt(o, method); // not optimized
  
  Class c = o.getClass();
  HashMap<String, Method> map;
  synchronized(callOpt_noArgs_cache) {
    map = callOpt_noArgs_cache.get(c);
    if (map == null)
      map = callOpt_noArgs_makeCache(c);
  }

  Method m = map.get(method);
  return m != null ? m.invoke(o) : null;
} catch (Exception __e) { throw rethrow(__e); } }

// used internally - we are in synchronized block
static HashMap<String, Method> callOpt_noArgs_makeCache(Class c) {
  HashMap<String,Method> map = new HashMap();
  Class _c = c;
  do {
    for (Method m : c.getDeclaredMethods())
      if (m.getParameterTypes().length == 0) {
        m.setAccessible(true);
        String name = m.getName();
        if (!map.containsKey(name))
          map.put(name, m);
      }
    _c = _c.getSuperclass();
  } while (_c != null);
  callOpt_noArgs_cache.put(c, map);
  return map;
}
// thread-safe
static void fillListWithStrings(final JList list, List<String> contents) {
  if (list == null) return;
  final DefaultListModel model = new DefaultListModel();
  if (contents != null) for (String s : contents)
    model.addElement(empty(s) ? " " : s);
  { swing(new Runnable() {  public void run() { try {  list.setModel(model); 
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "list.setModel(model);"; }}); }
}

static void fillListWithStrings(final JList list, String[] contents) {
  fillListWithStrings(list, asList(contents));
}

static String getTranspiledStandardFunctionsIfCached(TreeSet<String> functionSet) { try {
  File f = transpiledStandardFunctions_file(functionSet);
  //printFileStatus(f);
  return loadTextFile(f);
} catch (Throwable __e) { return null; } }
static String joinWithPlus(Collection<String> c) {
  return join("+", c);
}

static String joinWithPlus(String... c) {
  return join("+", c);
}

static Class veryQuickJava_finish(String src, List<String> libs) {
  libs = cloneList(libs);
  src = findTranslators2(src, libs);
  //print("Libs found: " + struct(libs));
  
  String dehlibs = join(" ", libs);
  File bytecode = null;
  bytecode = javaCompile_overInternalBot(src, dehlibs);

  return hotwireCore(concatLists(ll(bytecode), loadLibraries(libs)));
}
static boolean loadFunctions_debug;
static Object loadFunctions_veryQuickJava;

static Class loadFunctions(String... functions) {
  return loadFunctions(asList(functions));
}

static Class loadFunctions(Collection<String> functions) {
  print("Compiling " + joinWithPlus(functions));
  TreeSet<String> functionSet = new TreeSet<String>(functions);
  StringBuilder buf = new StringBuilder();
  for (String s : functionSet)
    buf.append("please include function " + assertIdentifier(s) + ".\n");
  long time = sysNow();
  Class c;
  if (loadFunctions_veryQuickJava != null)
    c = (Class) callF(loadFunctions_veryQuickJava, str(buf));
  else {
    veryQuickJava_transpiled.set(""); // request transpilation
    c = veryQuickJava(str(buf));
    saveTranspiledStandardFunctions(functionSet, getAndClearThreadLocal(veryQuickJava_transpiled));
  }
  if (loadFunctions_debug)
    done2_always(time, "loadFunctions: " + join(" ", functions));
  return c;
}
static List<Method> findMethodsNamed2(Object obj, String method, boolean staticOnly) {
  if (obj == null) return null;
  return findMethodsNamed2(_getClass(obj), method, staticOnly);
}

static List<Method> findMethodsNamed2(Class c, String method, boolean staticOnly) {
  List<Method> l = new ArrayList();
  while (c != null) {
    for (Method m : c.getDeclaredMethods())
      if (m.getName().equals(method) &&
        (!staticOnly || (m.getModifiers() & Modifier.STATIC) != 0)) {
        m.setAccessible(true);
        l.add(m);
      }
    c = c.getSuperclass();
  }
  return l;
}
// return Int.MAX_VALUE if not applicable
// Lower score is better
static int methodApplicabilityScore(Method m, Object[] args) {
   Class<?>[] types = m.getParameterTypes();
   if (types.length != args.length) return Integer.MAX_VALUE;
   int score = 0;
   for (int i = 0; i < types.length; i++) {
     Object a = args[i];
     Class c = types[i];
     if (a == null) ++score;
     else if (c == a.getClass()) {} // perfect match
     else if (isInstanceX(c, a)) ++score;
     else return Integer.MAX_VALUE;
  }
  return score;
}
static String methodSignature(String name, Object[] args) {
  StringBuilder buf = new StringBuilder(name + "(");
  for (int i = 0; i < l(args); i++) {
    if (i != 0) buf.append(", ");
    buf.append(getClassName(args[i]));
  }
  buf.append(")");
  return str(buf);
}
  static RuntimeException todo() {
    throw new RuntimeException("TODO");
  }
  
  static RuntimeException todo(String msg) {
    throw new RuntimeException("TODO: " + msg);
  }
static TreeSet<String> mechCISet(String name) {
  return asCISet(mechList(name));
}
static String ai_mechLanguagePrefix() {
  String l = ai_language();
  return eqic(l, "English") ? "" : l + " | ";
}
static String plural(String s) {
  return getPlural(s);
}
static <A> List<A> dropFirstAndLast(int n, List<A> l) {
  return new ArrayList(subList(l, n, l(l)-n));
}

static <A> List<A> dropFirstAndLast(List<A> l) {
  return dropFirstAndLast(1, l);
}

static String dropFirstAndLast(String s) {
  return substring(s, 1, l(s)-1);
}
static MultiMap<String, String> parseDoubleArrowMultiMapCI(List<String> l) {
  return parseDoubleArrowMultiMap((MultiMap) ciMultiMap(), l);
}
static String tok_javaDropCommentsFromWhitespace(String s) {
  int l = l(s), j = 0;
  StringBuilder buf = new StringBuilder();
  while (j < l) {
    char c = s.charAt(j);
    char d = j+1 >= l ? '\0' : s.charAt(j+1);
    if (c == '/' && d == '*') {
      do ++j; while (j < l && !s.substring(j, Math.min(j+2, l)).equals("*/"));
      j = Math.min(j+2, l);
    } else if (c == '/' && d == '/') {
      do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0);
    } else {
      buf.append(c); ++j;
    }
  }
  return str(buf);
}
static int smartLastIndexOf(String s, char c) {
  if (s == null) return 0;
  int i = s.lastIndexOf(c);
  return i >= 0 ? i : l(s);
}
static List<String> javaTokWithAngleBrackets(String s) {
  return tok_combineAngleBrackets_keep(javaTok(s));
}
static List<ai_Reconstructed> ai_buParser1_reconstructedProductions(AI_BottomUpParser1 parser, List<WeightedProduction> productions, AI_BottomUpParser1.Word group, String category) {
  List<ai_Reconstructed> l = new ArrayList();
  if (group == null) return l;
  
  for (WeightedProduction prod : productions)
    if (eq(category, prod.c)) {
      //print("Matching prod: " + sfu(prod));
      if (prod.b == null) {
        if (group.hasClass(prod.a))
          l.add(new ai_Reconstructed(prod, group));
        continue;
      }
        
      for (List<AI_BottomUpParser1.Word> parts : group.constituents) {
        if (l(parts) != 2) continue; // shouldn't happen
        AI_BottomUpParser1.Word partA = first(parts), partB = second(parts);
        if (!contains(partA.classes, prod.a)) continue;
        if (!contains(partB.classes, prod.b)) continue;
        l.add(new ai_Reconstructed(prod, partA, partB));
        //print("  Found grouping: " + partA.text + " + " + partB.text);
      }
    }

  return l;
}


// TODO: clean on language change
static TimedCache<Map<String, Set<String>>> ai_antiMechListNamesForAngleBracketCategory_cache = new TimedCache(5.0);

static Set<String> ai_antiMechListNamesForAngleBracketCategory(String s) {
  if (!isAngleBracketed(s)) return null;
  Map<String, Set<String>> map = ai_antiMechListNamesForAngleBracketCategory_cache.get("newHashMap");
  s = dropTrailingNumberFromAngleBracketCategory(s);
  if (map.containsKey(s)) return map.get(s);
  // Derive from the category name - use LinkedHashSet to keep first
  Set<String> set = litorderedset(mechLanguagePrefix() + "Not " + plural_mech(replace(deAngleBracket(s), "_", " ")));
  //set.addAll(mechMultiMapCI(mechLanguagePrefix() + "Word category => List").get(s));
  map.put(s, set);
  return set;
}



static <A> ThreadLocal newThreadLocal() {
  return new ThreadLocal();
}
// f: A -> Comparable
static <A> List<A> sortInPlaceByCalculatedFieldDesc_f1(List<A> l, final F1<A, ?> f) {
  sort(l, new Comparator<A>() {
    public int compare(A a, A b) {
      return stdcompare(f.get(b), f.get(a));
    }
  });
  return l;
}
static boolean veryQuickJava_silent = true;
static boolean veryQuickJava_useCompilerBot = true; // we always use it now
static ThreadLocal<String> veryQuickJava_transpiled = new ThreadLocal();

// mainJava is a complete program, but without the !752/!759 at the top
// returns link to main class
static Class veryQuickJava3(String mainJava) {
  return veryQuickJava3(mainJava, emptyList());
}

static Class veryQuickJava3(String mainJava, List<String> libs) {
  transpileRaw_silent = veryQuickJava_silent;
  String src = transpileRaw(mainJava); // transpiled, with lib references
  if (veryQuickJava_transpiled.get() != null)
    veryQuickJava_transpiled.set(src);
  return veryQuickJava_finish(src, libs);
}
static boolean tok_staticFunctionAlwaysReturnsVoid(List<String> tok, String name) {
  Set<String> types = tok_returnTypesOfStaticFunction_uncleaned(tok, name);
  if (empty(types)) return false;
  for (String type : types)
    if (!containsOneOf(javaTokC(type), "void", "svoid")) return false;
  return true;
}
static String textOfStandardFunction_cached(String sfName) {
  return loadSnippet_cached(stdFunctions_cached().get(sfName));
}
// i must point at the (possibly imaginary) opening bracket
// index returned is index of closing bracket + 1
static int findEndOfCurlyBracketPart(List<String> cnc, int i) {
  int j = i+2, level = 1;
  while (j < cnc.size()) {
    if (eq(cnc.get(j), "{")) ++level;
    else if (eq(cnc.get(j), "}")) --level;
    if (level == 0)
      return j+1;
    ++j;
  }
  return cnc.size();
}

static <A> List<A> dropFirstThreeAndLastThree(List<A> l) {
  return dropFirstAndLast(3, l);
}


static BigInteger bigint(String s) {
  return new BigInteger(s);
}

static BigInteger bigint(long l) {
  return BigInteger.valueOf(l);
}
static String hfulltag(String tag) {
  return hfulltag(tag, "");
}

static String hfulltag(String tag, Object contents, Object... params) {
  return hopeningTag(tag, params) + str(contents) + "</" + tag + ">";
}
static boolean mechPlay_on;

static boolean mechPlay_on() {
  return mechPlay_on;
}
static int lUtf8(String s) {
  return l(utf8(s));
}
static String fileNameEncode_safeChars = " ()[]#,!";

static String fileNameEncode(String s) {
  StringBuilder buf = new StringBuilder();
  int n = l(s);
  for (int i = 0; i < n; i++) {
    char c = s.charAt(i);
    if (contains(fileNameEncode_safeChars, c))
      buf.append(c);
    else
      buf.append(urlencode(str(c)));
  }
  return str(buf);
}
static boolean fileExists(String path) {
  return path != null && new File(path).exists();
}

static boolean fileExists(File f) {
  return f != null && f.exists();
}
static String fromUTF8(byte[] bytes) {
  return fromUtf8(bytes);
}
static Object[] muricaCredentialsPlus(Object... params) {
  return concatArrays(muricaCredentials(), params);
}
static String md5OrNull(String s) {
  return s == null ? null : md5(s);
}
static Map jsonDecodeMap(String s) {
  Object o = jsonDecode(s);
  if (o instanceof List && empty((List) o))
    return new HashMap();
  if (o instanceof Map)
    return (Map) o;
  else
    throw fail("Not a JSON map: " + s);
}
static File remoteMechListMirrorMetaFile(String listName) {
  return javaxDataDir("Remote Mech Lists/" + uniqueFileNameUsingMD5_80_v2(upper(listName)) + ".meta");
}
static <A, B> Map<A, B> getMultipleKeys(Map<A, B> map, A... keys) {
  Map<A, B> map2 = similarEmptyMap(map);
  if (map != null) for (A key : keys)
    map2.put(key, map.get(key));
  return map2;
}
static File remoteMechListMirrorNameFile(String listName) {
  return javaxDataDir("Remote Mech Lists/" + uniqueFileNameUsingMD5_80_v2(upper(listName)) + ".name");
}
static File localMechListFile(String listName) {
  return newFile(localMechListsDir(), uniqueFileNameUsingMD5_80_v2(listName) + ".text");
}
static <A> Set<A> synchronizedSet() {
  return synchroHashSet();
}

static <A> Set<A> synchronizedSet(Set<A> set) {
  return Collections.synchronizedSet(set);
}
// This is a bit rough... finds static and non-static methods.

static Method findMethodNamed(Object obj, String method) {
  if (obj == null) return null;
  if (obj instanceof Class)
    return findMethodNamed((Class) obj, method);
  return findMethodNamed(obj.getClass(), method);
}

static Method findMethodNamed(Class c, String method) {
  while (c != null) {
    for (Method m : c.getDeclaredMethods())
      if (m.getName().equals(method)) {
        m.setAccessible(true);
        return m;
      }
    c = c.getSuperclass();
  }
  return null;
}
static void upgradeJavaXAndRestart() {
  
    run("#1001639");
    restart();
    sleep();
  
  
}
static File transpiledStandardFunctions_file(TreeSet<String> functionSet) {
  String name = uniqueFileNameUsingMD5_80_v2(join("+", functionSet));
  File dir = javaxCachesDir("Transpiled Standard Functions");
  saveTextFileUnlessExists(newFile(dir, name + ".functions"), lines(functionSet));
  return newFile(dir, name + ".java");
}
// probably better than findTranslators (uses tokens)
// removes invocations from src
static String findTranslators2(String src, List<String> libsOut) {
  return join(findTranslators2(javaTok(src), libsOut));
}

// modifies original tok
static List<String> findTranslators2(List<String> tok, List<String> libsOut) {
  int i;
  while ((i = jfind(tok, "!<int>")) >= 0) {
    setAdd(libsOut, tok.get(i+2));
    clearTokens(tok, i, i+3);
  }
  return tok;
}
static File javaCompile_overInternalBot(String src) {
  return javaCompile_overInternalBot(src, "");
}

// returns path to jar
static synchronized File javaCompile_overInternalBot(String src, String dehlibs) {
  return CompilerBot.compile(src, dehlibs);
}

static Class hotwireCore(List<File> files) { try {
  // make class loader
  JavaXClassLoader classLoader = hotwire_makeClassLoader(files);

  // load & return main class
  Class<?> theClass = classLoader.loadClass("main");
  
  setOpt(theClass, "__javax", getJavaX());
  if (getOpt(theClass, "programID") == null)
    setOpt(theClass, "programID", "#3999999");
  
  if (!_inCore())
    hotwire_copyOver(theClass);
  
  return theClass;
} catch (Exception __e) { throw rethrow(__e); } }
static List<File> loadLibraries(List<String> snippetIDs) {
  return map("loadLibrary", snippetIDs);
}
static String assertIdentifier(String s) {
  return assertIsIdentifier(s);
}
static void saveTranspiledStandardFunctions(TreeSet<String> functionSet, String transpiledSrc) {
  try {
    saveTextFile(transpiledStandardFunctions_file(functionSet), transpiledSrc);
  } catch (Throwable __e) { _handleException(__e); }
}
static List<String> mechList(String name) {
  return mechList_tlft(name);
}
static MultiMap<String, String> parseDoubleArrowMultiMap(MultiMap<String, String> map, List<String> l) {
  for (String s : l) {
    List<String> x = splitAtDoubleArrow_bothDirections(s);
    if (l(x) == 2) map.put(first(x), second(x));
  }
  return map;
}
static <A> MultiMap<String, A> ciMultiMap() {
  return caseInsensitiveMultiMap();
}
static List<String> tok_combineAngleBrackets_keep(List<String> tok) {
  List<String> l = new ArrayList();
  for (int i = 0; i < l(tok); i++) {
    String t = tok.get(i);
    if (odd(i) && eq(t, "<")) {
      int j = findEndOfAngleBracketPart(tok, i);
      l.add(joinSubList(tok, i, j));
      i = j-1;
    } else
      l.add(t);
  }
  return l;
}
static boolean transpileRaw_silent = true;




static Class transpileRaw_trans;
static boolean transpileRaw_verySilent;
static boolean transpileRaw_useDiskCache;
static Lock transpileRaw_lock = lock();

static String transpileRaw(String mainJava) {
  return transpileRaw(mainJava, false);
}

static String transpileRaw(String mainJava, boolean fragment) {
  mainJava = dropTranslators(mainJava);
  transpileRaw_copyFromCreator();
  Lock __2238 = transpileRaw_lock; lock(__2238); try {
  { JWindow _loading_window = showLoadingAnimation("Transpiling..."); try {
    File cacheFile = null;
    if (transpileRaw_useDiskCache) {
      String md5 = md5(mainJava);
      cacheFile = new File(javaxCodeDir(), "Transpilations/" + md5 + ".java");
      { String _a_2244 = loadTextFile(cacheFile); if (!empty(_a_2244)) return _a_2244; }
    }
    
    transpileRaw_translator();
   
    if (fragment)
      setOpt(transpileRaw_trans, "localStuffOnly" , true);
    set(transpileRaw_trans, "mainJava", mainJava);
    // TODO: setOpt(transpileRaw_trans, transpilingSnippetID := );
    if (!transpileRaw_verySilent)
      print("Running translator " + getOpt(transpileRaw_trans, "programID"));
    callMain(transpileRaw_trans);
    //print("Ran translator " + identityHashCode(transpileRaw_trans));
    String main = (String) ( get(transpileRaw_trans, "mainJava"));
    if (transpileRaw_useDiskCache)
      saveTextFile(cacheFile, main);
    return main;
  } finally { disposeWindow(_loading_window); }}
} finally { unlock(__2238); } }

static Class transpileRaw_translator() {
  if (transpileRaw_trans == null) {
    //print("Loading translator.");
    transpileRaw_trans = hotwireSharingLibraries_silently("#759");
    //print("Loaded translator: " + identityHashCode(transpileRaw_trans));
  }
  setOpt(transpileRaw_trans, "print_silent", transpileRaw_silent);
  return transpileRaw_trans;
}

static void transpileRaw_copyFromCreator() {
  Lock __2239 = transpileRaw_lock; lock(__2239); try {
  if (transpileRaw_trans != null) return;
  Object c = creator();
  if (c == null) return;
  Class trans = (Class) ( getOpt(c, "transpileRaw_trans"));
  Lock lock = (Lock) ( getOpt(c, "transpileRaw_lock"));
  if (trans != null && lock != null) {
    print("Using creator's transpiler: " + getProgramID(c) + " => " + programID());
    transpileRaw_lock = lock;
    transpileRaw_trans = trans;
  }
} finally { unlock(__2239); } }

static void cleanMeUp_transpileRaw() {
  transpileRaw_trans = null; // release proactively (it's big)
}

static Set<String> tok_returnTypesOfStaticFunction_uncleaned(List<String> tok, String functionName) {
  List<List<String>> funcs = findFullFunctionDefs(tok, true);
  TreeSet<String> out = new TreeSet();
  for (List<String> tokF : funcs) {
    int i = indexOfAny(tokF, 0, "(", "{");
    if (i < 0) continue;
    String fname = get(tokF, i-2);
    if (!eq(fname, functionName)) continue;
    out.add(joinSubList(tokF, 1, i-3));
  }
  return out;
}
static String loadSnippet_cached(String id) {
  return loadSnippet_simpleCache(id);
}
static Map<String, String> stdFunctions_cached_map; // name -> snippet ID
static Lock stdFunctions_cached_lock = lock();

static Map<String, String> stdFunctions_cached() {
  Lock __2247 = stdFunctions_cached_lock; lock(__2247); try {
  if (stdFunctions_cached_map == null) {
    stdFunctions_cached_map = parseStdFunctionsList(loadSnippetSilently("#1006654"));
    stdFunctions_cached_map.putAll(parseStdFunctionsList(loadSnippetSilently("#761")));
  }
  return stdFunctions_cached_map;
} finally { unlock(__2247); } }

static synchronized void stdFunctions_clearCache() {
  stdFunctions_cached_map = null;
}


static File loadLibrary(String snippetID) {
  return loadBinarySnippet(snippetID);
}
static boolean jsonDecode_useOrderedMaps = true;

static Object jsonDecode(final String text) {
  final List<String> tok = jsonTok(text);
  
  class Y {
    int i = 1;

    Object parse() {
      String t = tok.get(i);
      if (t.startsWith("\"")) {
        String s = unquote(tok.get(i));
        i += 2;
        return s;
      }
      if (t.equals("{"))
        return parseMap();
      if (t.equals("["))
        return this.parseList(); // avoid loading standard function "parseList"
      if (t.equals("null")) {
        i += 2; return null;
      }
      if (t.equals("false")) {
        i += 2; return false;
      }
      if (t.equals("true")) {
        i += 2; return true;
      }
      boolean minus = false;
      if (t.equals("-")) {
        minus = true;
        i += 2;
        t = get(tok, i);
      }
      if (isInteger(t)) {
        i += 2;
        if (eq(get(tok, i), ".")) {
          String x = t + "." + get(tok, i+2);
          i += 4;
          double d = parseDouble(x);
          if (minus) d = -d;
          return d;
        } else {
          long l = parseLong(t);
          if (minus) l = -l;
          return l != (int) l ? new Long(l) : new Integer((int) l);
        }
      }
      
      throw new RuntimeException("Unknown token " + (i+1) + ": " + t + ": " + text);
    }
    
    Object parseList() {
      consume("[");
      List list = new ArrayList();
      while (!tok.get(i).equals("]")) {
        list.add(parse());
        if (tok.get(i).equals(",")) i += 2;
      }
      consume("]");
      return list;
    }
    
    Object parseMap() {
      consume("{");
      Map map = jsonDecode_useOrderedMaps ? new LinkedHashMap() : new TreeMap();
      while (!tok.get(i).equals("}")) {
        String key = unquote(tok.get(i));
        i += 2;
        consume(":");
        Object value = parse();
        map.put(key, value);
        if (tok.get(i).equals(",")) i += 2;
      }
      consume("}");
      return map;
    }
    
    void consume(String s) {
      if (!tok.get(i).equals(s)) {
        String prevToken = i-2 >= 0 ? tok.get(i-2) : "";
        String nextTokens = join(tok.subList(i, Math.min(i+4, tok.size())));
        throw fail(quote(s) + " expected: " + prevToken + " " + nextTokens + " (" + i + "/" + tok.size() + ")");
      }
      i += 2;
    }
  }
  
  return new Y().parse();
}
static Map similarEmptyMap(Map m) {
  if (m instanceof TreeMap) return new TreeMap(((TreeMap) m).comparator());
  if (m instanceof LinkedHashMap) return new LinkedHashMap();
  
  // default to a hash map
  return new HashMap();
}
static File localMechListsDir() {
  return javaxDataDir("Mech Lists");
}
static Class run(String progID, String... args) {
  Class main = hotwire(progID);
  callMain(main, args);
  return main;
}
static void saveTextFileUnlessExists(File f, String contents) {
  if (f != null && !f.exists())
    saveTextFile(f, contents);
}
  static void clearTokens(List<String> tok) {
    clearAllTokens(tok);
  }
  
  static void clearTokens(List<String> tok, int i, int j) {
    clearAllTokens(tok, i, j);
  }
static JavaXClassLoader hotwire_makeClassLoader(List<File> files) {
  return new JavaXClassLoader(null, files);
}
static String assertIsIdentifier(String s) {
  if (!isIdentifier(s))
    throw fail("Not an identifier: " + quote(s));
  return s;
}
static List<String> mechList_tlft(String name) {
  return tlft(mechList_raw(name));
}
static <A> MultiMap<String, A> caseInsensitiveMultiMap() {
  MultiMap<String,A> mm = new MultiMap();
  mm.data = caseInsensitiveMap();
  return mm;
}
// i must point at the (possibly imaginary) opening bracket
// index returned is index of closing bracket + 1
static int findEndOfAngleBracketPart(List<String> cnc, int i) {
  int j = i+2, level = 1;
  while (j < cnc.size()) {
    if (eq(cnc.get(j), "<")) ++level;
    else if (eq(cnc.get(j), ">")) --level;
    if (level == 0)
      return j+1;
    ++j;
  }
  return cnc.size();
}

static String dropTranslators(String src) {
  return findTranslators2(src, null);
}

// modifies original tok
static List<String> dropTranslators(List<String> tok) {
  return findTranslators2(tok, null);
}
static JWindow showLoadingAnimation() {
  return showLoadingAnimation("Hold on user...");
}

static JWindow showLoadingAnimation(String text) { try {
  return showAnimationInTopRightCorner("#1003543", text);
} catch (Throwable __e) { return null; } }
static Class<?> hotwireSharingLibraries_silently(String progID) {
   AutoCloseable __2279 = tempSetThreadLocal(loadPage_silent, true); try {
  return hotwireSharingLibraries(progID);
} finally { _close(__2279); }}
static Set<String> findFullFunctionDefs_keywords = new HashSet(splitAtSpace("static svoid ssvoid ssynchronized sbool sS sO sL"));

// returns actual CNC
static List<List<String>> findFullFunctionDefs(List<String> tok, boolean topLevelOnly) {
  int n = l(tok);
  List < List < String > > functions = new ArrayList();
  for (int i = 1; i < n; i += 2) {
    String t = tok.get(i);
    if (topLevelOnly && eq(t, "{")) i = findEndOfBlock(tok, i)-1;
    else if (findFullFunctionDefs_keywords.contains(t)) {
      
      int j = i+2;
      while (j < n && !eqOneOf(tok.get(j), ";", "=", "(", "{"))
        j += 2;
      if ((eqGet(tok, j, "(") || eq(t, "svoid") && eqGet(tok, j, "{"))
        && isIdentifier(tok.get(j-2))) {
        int k = smartIndexOf(tok, "{", j);
        if (k < l(tok)) {
          k = findEndOfBlock(tok, k)+1;
          functions.add(subList(tok, i-1, k));
          i = k-2;
        }
      }
    }
  }
  return functions;
}
static ExpiringMap2<String, String> loadSnippet_simpleCache_map = new ExpiringMap2(10000);
static Lock loadSnippet_simpleCache_lock = lock();

// timeout for loadPage
static int loadSnippet_simpleCache_timeout = 60000;

static String loadSnippet_simpleCache(String id) {
  if (id == null) return null;
  Lock __2283 = loadSnippet_simpleCache_lock; lock(__2283); try {
  id = fsI(id);
  
  // get from cache
  String src = loadSnippet_simpleCache_map.get(id);
  if (src != null) return src;
  
  // load & put in cache
  Integer oldTimeout = setThreadLocal(loadPage_forcedTimeout_byThread, loadSnippet_simpleCache_timeout);
  try {
    src = loadSnippet(id);
    if (src != null)
      loadSnippet_simpleCache_map.put(id, src);
    return src;
  } finally {
    loadPage_forcedTimeout_byThread.set(oldTimeout);
  }
} finally { unlock(__2283); } }
// map contains function name -> snippet id
static Map<String, String> parseStdFunctionsList(String snippetSrc) {
  List<String> tok = javaTok(snippetSrc);
  int i = findCodeTokens(tok, "standardFunctions", "=", "litlist", "(");
  int opening = i+6;
  int closing = indexOf(tok, ")", opening)-1;
  Map<String, String> map = litorderedmap();
  for (i = opening+2; i < closing; i += 4) {
    String[] f = unquote(tok.get(i)).split("/");
    map.put(f[1], f[0]);
  }
  return map;
}

static String loadSnippetSilently(Snippet s) {
  return loadSnippetQuietly(s);
}



static String loadSnippetSilently(String snippetID) {
  return loadSnippetQuietly(snippetID);
}


static List<String> jsonTok(String s) {
  List<String> tok = new ArrayList<String>();
  int l = s.length();
  
  int i = 0;
  while (i < l) {
    int j = i;
    char c; String cc;
    
    // scan for whitespace
    while (j < l) {
      c = s.charAt(j);
      cc = s.substring(j, Math.min(j+2, l));
      if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
        ++j;
      else if (cc.equals("/*")) {
        do ++j; while (j < l && !s.substring(j, Math.min(j+2, l)).equals("*/"));
        j = Math.min(j+2, l);
      } else if (cc.equals("//")) {
        do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0);
      } else
        break;
    }
    
    tok.add(s.substring(i, j));
    i = j;
    if (i >= l) break;
    c = s.charAt(i); // cc is not needed in rest of loop body

    // scan for non-whitespace (json strings, "null" identifier, numbers. everything else automatically becomes a one character token.)
    if (c == '\'' || c == '"') {
      char opener = c;
      ++j;
      while (j < l) {
        if (s.charAt(j) == opener) {
          ++j;
          break;
        } else if (s.charAt(j) == '\\' && j+1 < l)
          j += 2;
        else
          ++j;
      }
    } else if (Character.isLetter(c))
      do ++j; while (j < l && Character.isLetter(s.charAt(j)));
    else if (Character.isDigit(c))
      do ++j; while (j < l && Character.isDigit(s.charAt(j)));
    else
      ++j;

    tok.add(s.substring(i, j));
    i = j;
  }
  
  if ((tok.size() % 2) == 0) tok.add("");
  return tok;
}

static boolean showAnimationInTopRightCorner_alwaysOnTop = true;
static boolean showAnimationInTopRightCorner_on = true;

// automatically switches to AWT thread for you
// text is optional text below image
static JWindow showAnimationInTopRightCorner(String imageID, String text) {
  if (isHeadless() || !showAnimationInTopRightCorner_on) return null;
  return showAnimationInTopRightCorner(imageIcon(imageID), text);
}

static JWindow showAnimationInTopRightCorner(final BufferedImage image, final String text) {
  if (isHeadless() || !showAnimationInTopRightCorner_on) return null;
  return showAnimationInTopRightCorner(imageIcon(image), text);
}

static JWindow showAnimationInTopRightCorner(final ImageIcon imageIcon, final String text) {
  if (isHeadless() || !showAnimationInTopRightCorner_on) return null;
  return (JWindow) swingAndWait(new F0<Object>() { Object get() { try { 
    JLabel label = new JLabel(imageIcon);
    if (nempty(text)) {
      label.setText(text);
      label.setVerticalTextPosition(SwingConstants.BOTTOM);
      label.setHorizontalTextPosition(SwingConstants.CENTER);
    }
    final JWindow window = showInTopRightCorner(label);
    onClick(label, new Runnable() {  public void run() { try {  window.dispose() ;
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "window.dispose()"; }});
    if (showAnimationInTopRightCorner_alwaysOnTop)
      window.setAlwaysOnTop(true);
    return window;
   } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "JLabel label = new JLabel(imageIcon);\r\n    if (nempty(text)) {\r\n      label.s..."; }});
}

static JWindow showAnimationInTopRightCorner(final String imageID) {
  return showAnimationInTopRightCorner(imageID, "");
}

static JWindow showAnimationInTopRightCorner(String imageID, double seconds) {
  return showAnimationInTopRightCorner(imageID, "", seconds);
}

static JWindow showAnimationInTopRightCorner(String imageID, String text, double seconds) {
  if (isHeadless()) return null;
  return disposeWindowAfter(iround(seconds*1000), showAnimationInTopRightCorner(imageID, text));
}

static JWindow showAnimationInTopRightCorner(BufferedImage img, String text, double seconds) {
  return disposeWindowAfter(iround(seconds*1000), showAnimationInTopRightCorner(img, text));
}

static Class<?> hotwireSharingLibraries(String progID) { try {
  Pair<File, String> p = CompilerBot.compileSnippet2(progID);
  File jar = p.a;
  assertTrue(f2s(jar), jar.isFile());
  
  // collect files (program + libraries)
  
  List<File> files = ll(jar);
  String dehlibs = unnull(loadTextFileFromZip(jar, "libraries"));
  
  List<File> myLibraries = myLibraryFiles();
  //print("My libraries: " + myLibraries);
  
  Matcher matcher = Pattern.compile("\\d+").matcher(dehlibs);
  while (matcher.find()) {
    String libID = matcher.group();
    File lib = loadLibrary(libID);
    if (myLibraries.contains(lib)) {
      //print("Skipping lib " + lib);
    } else {
      //print("Adding lib " + lib);
      files.add(lib);
    }
  }

  // make class loader
  JavaXClassLoaderWithParent classLoader = new JavaXClassLoaderWithParent(progID, files, myClassLoader());
  
  return hotwire_finish(classLoader, progID, p.b);
} catch (Exception __e) { throw rethrow(__e); } }
static List<String> splitAtSpace(String s) {
  return asList(s.split("\\s+"));
}
// i must point at the (possibly imaginary) opening bracket ("{")
// index returned is index of closing bracket + 1 (or l(cnc))
static int findEndOfBlock(List<String> cnc, int i) {
  int j = i+2, level = 1;
  while (j < cnc.size()) {
    if (cnc.get(j).equals("{")) ++level;
    else if (cnc.get(j).equals("}")) --level;
    if (level == 0)
      return j+1;
    ++j;
  }
  return cnc.size();
}

static String loadSnippetQuietly(Snippet s) {
  return loadSnippetQuietly(s.id);
}


static String loadSnippetQuietly(String snippetID) {
  loadSnippet_silent.set(true);
  try {
    return loadSnippet(snippetID);
  } finally {
    loadSnippet_silent.set(null);
  }
}


static JWindow showInTopRightCorner(Component c) {
  JWindow w = new JWindow();
  w.add(c);
  w.pack();
  moveToTopRightCorner(w);
  w.setVisible(true);
  return w;
}
static String loadTextFileFromZip(File inZip, String fileName) {
  return loadTextFileFromZipFile(inZip, fileName);
}
static List<File> myLibraryFiles() {
  return (List<File>) get(myClassLoader(), "files");
}
static ClassLoader myClassLoader() {
  return _getClass(mc()).getClassLoader();
}
static Class hotwire_finish(ClassLoader classLoader, String progID, String javaSource) { try {
  // load & return main class
  Class<?> theClass = classLoader.loadClass("main");
  
  Class j = getJavaX();
  
  setOpt(theClass, "myJavaSource_code", javaSource);
  
  synchronized(j) { // hopefully this goes well...
    call(j, "setVars", theClass, progID);
    callOpt(j, "addInstance", progID, theClass);
  }
  
  hotwire_copyOver(theClass);
  return theClass;
} catch (Exception __e) { throw rethrow(__e); } }


static class TimedCache<A> {
  long timeout;
  A value;
  Object function;
  long set;
  Lock lock = lock();
  
  // stats
  int stores, fails, hits;
  
  TimedCache(Object function, double timeoutSeconds) {
    this(timeoutSeconds);
    this.function = function;
  }

  // 0 = no timeout  
  TimedCache(double timeoutSeconds) {
    timeout = toMS(timeoutSeconds);
  }
  
  A set(A a) {
    Lock __2311 = lock; lock(__2311); try {
    ++stores;
    value = a;
    set = now();
    return a;
  } finally { unlock(__2311); } }
  
  boolean has() {
    Lock __2312 = lock; lock(__2312); try {
    clean();
    if (set != 0) {
      ++hits;
      return true;
    }
    ++fails;
    return false;
  } finally { unlock(__2312); } }
  
  A get() {
    Lock __2313 = lock; lock(__2313); try {
    if (function != null) return get(function);
    clean();
    if (set != 0) ++hits; else ++fails;
    return value;
  } finally { unlock(__2313); } }
  
  A get(Object makerFunction) {
    Lock __2314 = lock; lock(__2314); try {
    if (this.has()) return getNoClean();
    A a = (A) ( callF(makerFunction));
    return set(a);
  } finally { unlock(__2314); } }
  
  A getNoClean() {
    Lock __2315 = lock; lock(__2315); try { // lock to wait for potentially running calculation
    return value;
  } finally { unlock(__2315); } }
  
  // clear if timed out
  void clean() {
    Lock __2316 = lock; lock(__2316); try {
    if (timeout > 0 && now() > set+timeout) clear();
  } finally { unlock(__2316); } }
  
  // clear now
  void clear() {
    Lock __2317 = lock; lock(__2317); try {
    set = 0;
    value = null;
  } finally { unlock(__2317); } }
  
  String stats() {
    return "Stores: " + stores + ", hits: " + hits + ", fails: " + fails;
  }
}static abstract class TokCondition {
  abstract boolean get(List<String> tok, int i); // i = N Index
}static class TelegramMsg {
  String globalID;
  long date, originalDate;
  String firstName, lastName;
  int userID;
  String text;
  boolean sent;
  long chatID;
  
  public String toString() { return sfu(this); }
}
static class DerivedMap<A, B> extends AbstractMap<A, B> {
  Map<A, B> base;
  Map<A, B> additions = new HashMap();
  
  DerivedMap() {}
  DerivedMap(Map<A, B> base) {
  this.base = base;}
  DerivedMap(Map<A, B> base, Map<A, B> additions) {
  this.additions = additions;
  this.base = base;}
  
  public B get(Object key) {
    B b = additions.get(key);
    if (b != null) return b;
    return base.get(key);
  }
  
  public B put(A key, B value) {
    return additions.put(key, value);
  }
  
  public Set<Map.Entry<A,B>> entrySet() {
    throw fail();
  }
}
static class MultiMap<A,B> {
  Map<A, List<B>> data = new HashMap<A, List<B>>();
  
  MultiMap() {}
  MultiMap(boolean useTreeMap) { if (useTreeMap) data = new TreeMap(); }
  MultiMap(MultiMap<A, B> map) { putAll(map); }
  MultiMap(Map<A, List<B>> data) {
  this.data = data;}

  void put(A key, B value) { synchronized(data) {
    List<B> list = data.get(key);
    if (list == null)
      data.put(key, list = _makeEmptyList());
    list.add(value);
  }}

  void addAll(A key, Collection<B> values) { synchronized(data) {
    putAll(key, values);
  }}
  
  void addAllIfNotThere(A key, Collection<B> values) { synchronized(data) {
    for (B value : values)
      setPut(key, value);
  }}
  
  void setPut(A key, B value) { synchronized(data) {
    if (!containsPair(key, value))
      put(key, value);
  }}
  
  boolean containsPair(A key, B value) { synchronized(data) {
    return get(key).contains(value);
  }}
  
  void putAll(A key, Collection<B> values) { synchronized(data) {
    for (B value : values)
      put(key, value);
  }}

  void removeAll(A key, Collection<B> values) { synchronized(data) {
    for (B value : values)
      remove(key, value);
  }}
  
  List<B> get(A key) { synchronized(data) {
    List<B> list = data.get(key);
    return list == null ? Collections.<B> emptyList() : list;
  }}

  // returns actual mutable live list
  // creates the list if not there
  List<B> getActual(A key) { synchronized(data) {
    List<B> list = data.get(key);
    if (list == null)
      data.put(key, list = _makeEmptyList());
    return list;
  }}
 
  void clean(A key) { synchronized(data) {
    List<B> list = data.get(key);
    if (list != null && list.isEmpty())
      data.remove(key);
  }}

  Set<A> keySet() { synchronized(data) {
    return data.keySet();
  }}

  Set<A> keys() { synchronized(data) {
    return data.keySet();
  }}

  void remove(A key) { synchronized(data) {
    data.remove(key);
  }}

  void remove(A key, B value) { synchronized(data) {
    List<B> list = data.get(key);
    if (list != null) {
      list.remove(value);
      if (list.isEmpty())
        data.remove(key);
    }
  }}

  void clear() { synchronized(data) {
    data.clear();
  }}

  boolean containsKey(A key) { synchronized(data) {
    return data.containsKey(key);
  }}

  B getFirst(A key) { synchronized(data) {
    List<B> list = get(key);
    return list.isEmpty() ? null : list.get(0);
  }}
  
  void putAll(MultiMap<A, B> map) { synchronized(data) {
    for (A key : map.keySet())
      putAll(key, map.get(key));
  }}
  
  int keysSize() { synchronized(data) { return l(data); }}
  
  // full size - note: expensive operation
  int size() { synchronized(data) {
    int n = 0;
    for (List l : data.values())
      n += l(l);
    return n;
  }}
  
  // expensive operation
  List<A> reverseGet(B b) { synchronized(data) {
    List<A> l = new ArrayList();
    for (A key : data.keySet())
      if (data.get(key).contains(b))
        l.add(key);
    return l;
  }}
  
  Map<A, List<B>> asMap() { synchronized(data) {
    return cloneMap(data);
  }}
  
  boolean isEmpty() { synchronized(data) { return data.isEmpty(); }}
  
  // override in subclasses
  List<B> _makeEmptyList() {
    return new ArrayList();
  }
  
  Collection<List<B>> allLists() {
    synchronized(data) {
      return new ArrayList(data.values());
    }
  }
}static class CombinedStringMatcher extends StringMatcher {
  List<StringMatcher> matchers = new ArrayList();
  
  CombinedStringMatcher() {}
  CombinedStringMatcher(StringMatcher... matchers) { addAll(this.matchers, matchers); }
  
  boolean match(String pat, String input, Map<String, String> matches) {
    for (StringMatcher m : matchers)
      if (m.match(pat, input, matches)) return true;
    return false;
  }
}static class QuickException extends RuntimeException {
  public Throwable fillInStackTrace() { return this; }
  
  QuickException() {}
  QuickException(Throwable e) { super(e); }
  QuickException(String msg) { super(msg); }
  QuickException(String msg, Throwable e) { super(msg, e); }
}static class NLStringMatcher_dollarVars_underscores_dollarDollar extends StringMatcher {
  // change matches only if returning true
  boolean match(String pat, String input, Map<String, String> matches) {
    pat = trim(pat);
    // full-string match with $$x
    if (startsWith(pat, "$$") && isDollarVar(pat))
      return strictPutIC(matches, pat, input);
      
    Map<String, String> newMatches = matchDollarVarsIC_underscores(pat, input);
    return newMatches != null && addMapToMapWithoutOverwritingIC(matches, newMatches);
  }
  
  String apply(String input, Map<String, String> matches) {
    return replaceVars(input, matches);
  }
}static class NLStringMatcher_dollarVars extends StringMatcher {
  // change matches only if returning true
  boolean match(String pat, String input, Map<String, String> matches) {
    Map<String, String> newMatches = matchDollarVarsIC(pat, input);
    return newMatches != null && addMapToMapWithoutOverwritingIC(matches, newMatches);
  }
  
  String apply(String input, Map<String, String> matches) {
    return replaceVars(input, matches);
  }
}static class Lowest<A> {
  A best;
  double score;
  transient Object onChange;
  
  boolean isNewBest(double score) {
    return best == null || score < this.score;
  }
  
  double bestScore() {
    return best == null ? Double.NaN : score;
  }
  
  float floatScore() {
    return best == null ? Float.NaN : (float) score;
  }
  
  float floatScoreOr(float defaultValue) {
    return best == null ? defaultValue : (float) score;
  }
  
  boolean put(A a, double score) {
    if (a != null && isNewBest(score)) {
      best = a;
      this.score = score;
      pcallF(onChange);
      return true;
    }
    return false;
  }
  
  A get() { return best; }
  boolean has() { return best != null; }
}static class Either<A, B> {
  byte which;
  Object value;
  
  Either() {}
  Either(int which, Object value) {
  this.value = value; this.which = (byte) which; }
  
  boolean isA() { return which == 1; }
  boolean isB() { return which == 2; }
  
  A a() { if (which != 1) _failMe(); return (A) value; }
  B b() { if (which != 2) _failMe(); return (B) value; }
  
  void _failMe() {
    throw fail("Either object is of wrong type: " + shortClassName(value));
  }
}static class ai_Reconstructed {
  WeightedProduction production;
  AI_BottomUpParser1.Word a, b; // b may be null if it's a subclassing
  
  ai_Reconstructed(WeightedProduction production, AI_BottomUpParser1.Word a, AI_BottomUpParser1.Word b) {
  this.b = b;
  this.a = a;
  this.production = production;}
  ai_Reconstructed(WeightedProduction production, AI_BottomUpParser1.Word a) {
  this.a = a;
  this.production = production;}
  
  public String toString() { return sfu(production) + " - " + a.text() + (b == null ? "" : " + " + b.text()); }
}static class CompilerBot {
  static boolean verbose;

  static File compileSnippet(String snippetID) {
    return compileSnippet(snippetID, "");
  }
  
  static Pair<File, String> compileSnippet2(String snippetID) {
    return compileSnippet2(snippetID, "");
  }
  
  // returns jar path
  static File compileSnippet(String snippetID, String javaTarget) {
    return compileSnippet2(snippetID, javaTarget).a;
  }
  
  // returns jar path, Java source
  static Pair<File, String> compileSnippet2(String snippetID, String javaTarget) {
    String transpiledSrc = getServerTranspiled2(snippetID);
    int i = transpiledSrc.indexOf('\n');
    String libs = transpiledSrc.substring(0, Math.max(0, i));
    if (verbose)
      print("Compiling snippet: " + snippetID + ". Libs: " + libs);
    transpiledSrc = transpiledSrc.substring(i+1);
    return pair(compile(transpiledSrc, libs, javaTarget, snippetID), transpiledSrc);
  }

  static File compile(String src) {
    return compile(src, "");
  }
  
  static File compile(String src, String libs) {
    return compile(src, libs, null);
  }

  static File compile(String src, String dehlibs, String javaTarget) {
    return compile(src, dehlibs, javaTarget, null);
  }
  
  static File compile(String src, String dehlibs, String javaTarget, String progID) {
    if (verbose)
      print("Compiling " + l(src) + " chars");
      
    // Note: This is different from the calculation in x30
    // (might lead to programs being compiled twice)
    String md5 = md5(dehlibs + "\n" + src + "\n" + progID);
    File jar = getJarFile(md5);
    if (jar == null || jar.length() <= 22) {
      // have to compile
      
      boolean canRename = useDummyMainClasses() && isSnippetID(progID) && !tok_classHasModifier(findMainClass(javaTok(src)), "public");
      if (verbose)
        print("useRenaming: " + useDummyMainClasses() + ", canRename: " + canRename + ", progID: " + progID);

      javaCompileToJar_optionalRename(src, dehlibs, jar, canRename ? progID : null);
    } else {
      if (verbose)
        print("Getting classes from cache (" + jar.getAbsolutePath() + ", " + jar.length() + " bytes)");
      touchFile(jar); // so we can find the unused ones easier
    }
    
    return jar;
  }

  static File getJarFile(String md5) {
    assertTrue(isMD5(md5));
    return new File(getCacheProgramDir("#1002203"), md5 + ".jar");
  }
}static class T3<A, B, C> {
  A a;
  B b;
  C c;
  
  T3() {}
  T3(A a, B b, C c) {
  this.c = c;
  this.b = b;
  this.a = a;}
  T3(T3<A, B, C> t) { a = t.a; b = t.b; c = t.c; }
  
  public int hashCode() {
    return _hashCode(a) + 2*_hashCode(b) - 4*_hashCode(c);
  }
  
  public boolean equals(Object o) {
    if (o == this) return true;
    if (!(o instanceof T3)) return false;
    T3 t = (T3) o;
    return eq(a, t.a) && eq(b, t.b) && eq(c, t.c);
  }
  
  public String toString() {
    return "(" + quoteBorderless(a) + ", " + quoteBorderless(b) + ", " + quoteBorderless(c) + ")";
  }
}static class AI_BottomUpParser1 {
  Map<String, Set<String>> wordsToCategories;
  boolean splitSplittables = true; // e.g. what's => what is
  String productionsText;
  
  List<Word> words;
  List<String> rawWords;
  List<String> cnc; // words + N tokens (null if unknown)
  
  LinkedHashSet<Updatable> allObjects = new LinkedHashSet();
  long changes;
  MultiMap<String,WeightedProduction> productionsByA = new MultiMap();
  MultiMap<String, String> subClasses = new MultiMap();
  boolean mergeGroupsInSamePosition = true;
  HashMap<IntRange,Word> groupsByPosition = new HashMap();
  boolean verbose_callPlausibilityFunction;
  Map<Integer, List<String>> categoriesAtWordIndex; // user can set categories for each token

  class Updatable {
    void update() {}
    
    void setField(String field, Object value) {
      if (eq(get(this, field), value)) return;
      set(this, field, value);
      change();
    }
  }
  
  class Expectation {
    String ifClass;
    Runnable action;
    
    Expectation() {}
    Expectation(String ifClass, Runnable action) {
  this.action = action;
  this.ifClass = ifClass;}
  }
  
  class Word extends Updatable {
    String text; // or null if unknown
    int wordFrom, wordTo; // token indices
    LinkedHashSet<Word> prev = new LinkedHashSet();
    LinkedHashSet<Word> next = new LinkedHashSet();
    LinkedHashSet < List < Word > > constituents = new LinkedHashSet(); // if group
    List<Expectation> expectations = new ArrayList();
    List<Expectation> fulfilledExpectations = new ArrayList();
    TreeSet<String> classes = new TreeSet();
    int classesConvertedToTraits;
    LinkedHashSet<Word> groups = new LinkedHashSet(); // I am part of
    List<Trait> traits = new ArrayList();
    
    Word() {}
    Word(String text, int wordFrom, int wordTo) {
  this.wordTo = wordTo;
  this.wordFrom = wordFrom;
  this.text = text;
      classes.add(quote(lower(text)));
    }
    
    void update() {
      // Add direct word classes
      if (text != null)
        for (String c : unnull(wordsToCategories.get(text)))
          addClass(c);
      
      // Process expectations
      for (Expectation e : cloneList(expectations)) {
        //print("Checking expected class " + e.ifClass);
        if (classes.contains(e.ifClass)) {
          moveElementFromCollectionToCollection(e, expectations, fulfilledExpectations);
          change();
          callF(e.action);
        }
      }
      
      if (l(classes) > classesConvertedToTraits) {
        for (final String c : dropFirst(classesConvertedToTraits, classes))
          addTraitsForClass(c);
        classesConvertedToTraits = l(classes);
      }
      
      for (Trait t : iterateListConcurrently(traits))
        t.update();
    }
    
    boolean isGroup() { return nempty(constituents); }
    
    boolean addClass(String c) {
      if (!classes.addAll(makeHull_optimized(subClasses, c))) return false;
      change(); return true;
    }
    
    void addExpectation(Expectation e) {
      //print("addExpectation " + e);
      expectations.add(e);
      change();
    }
    
    void addTraitsForClass(String c) {
      for (WeightedProduction p : productionsByA.get(c))
        addTrait(new LinkWithTo(p.b, p.c, p.plausibilityFunction));
    }
    
    void addTrait(Trait t) {
      set(t, "w" , this);
      traits.add(t);
    }
    
    public String toString() {
      return textAndClasses(this);
    }
    
    boolean hasClass(String c) { return contains(classes, c); }
    String text() { return text; }
  } // end of class Word
  
  Word makeGroup(Word a, Word b, String newClass) {
    List<Word> list = null;
    Word g = null;
    if (mergeGroupsInSamePosition)
      g = groupsByPosition.get(new IntRange(a.wordFrom, b.wordTo));
    else { 
      list = ll(a, b);
      // look for existing group
      for (Word _g : a.groups)
        if (contains(_g.constituents, list)) { g = _g; break; }
    }
    
    if (list == null) list = ll(a, b);
    if (g != null) {
      g.constituents.add(list);
      if (g.addClass(newClass)) {
        //print("Added class " + newClass + " to existing group: " + a.text + " + " + b.text);
      }
      return g;
    }

    // new group
    //print("Making group " + newClass + " " + a.text + " + " + b.text);
    //print("  prev=" + sfu(collect(a.prev, 'text)));
    //print("  next=" + sfu(collect(b.next, 'text)));
    g = new Word(joinWithSpace(a.text, b.text), a.wordFrom, b.wordTo);
    allObjects.add(g);
    if (mergeGroupsInSamePosition)
      groupsByPosition.put(new IntRange(a.wordFrom, b.wordTo), g);
    g.addClass(newClass);
    g.constituents.add(list);
    for (Word w : list)
      w.groups.add(g);
    g.prev.addAll(a.prev);
    g.next.addAll(b.next);
    for (Word prev : a.prev) prev.next.add(g);
    for (Word next : b.next) next.prev.add(g);
    return g;
  }
  
  class Trait extends Updatable {
    Word w;
  }
  
  class LinkWithTo extends Trait {
    String linkWith, linkTo; // classes
    String plausibilityFunction;
    int expectationsSentToNext;
    
    LinkWithTo() {}
    LinkWithTo(String linkWith, String linkTo, String plausibilityFunction) {
  this.plausibilityFunction = plausibilityFunction;
  this.linkTo = linkTo;
  this.linkWith = linkWith;}
    
    void update() {
      if (l(w.next) > expectationsSentToNext) {
        for (final Word next : dropFirst(expectationsSentToNext, w.next))
          next.addExpectation(new Expectation(linkWith, new Runnable() {  public void run() { try { 
            if (ai_parser_activateStandardFunctions_get() && plausibilityFunction != null) {
              loadFunctions_preferDiskCache(); // note: changes this for whole program
              Object result = pcallAndMake(plausibilityFunction, w.text, next.text);
              if (verbose_callPlausibilityFunction)
                print("Called plausibility function " + plausibilityFunction + ": " + w.text + " + " + next.text + " => " + result);
              if (isFalse(result))
                return;
            }
            makeGroup(w, next, linkTo)
          ;
} catch (Exception __e) { throw rethrow(__e); } }  public String toString() { return "if (ai_parser_activateStandardFunctions_get() && plausibilityFunction != null..."; }}));
        expectationsSentToNext = l(w.next);
      }
    }
  }
  
  void parse(List<String> tok) {
    cnc = /*simpleSpaces2*/(tok);
    rawWords = codeTokens(cnc);
    _parse();
  }
  
  AI_BottomUpParser1 parse(final String sentence) {
    rawWords = words_withAngleBrackets(sentence);
    _parse();
    return this;
  }
  
  void _parse() {
    if (words != null) throw fail("only call once");
    if (splitSplittables)
      ai_splitSplittables(rawWords);
    
    if (wordsToCategories == null) wordsToCategories = ai_wordToCategories();
    parseProductions();
    
    words = new ArrayList();
    for (int i = 0; i < l(rawWords); i++) {
      Word w = new Word(rawWords.get(i), i, i+1);
      words.add(w);
      if (isAngleBracketed(w.text)) w.addClass(w.text);
      else if (isQuoted(w.text)) w.addClass("<noun>");
      else if (isInteger(w.text)) w.addClass("<number>");
      for (String cat : unnull(mapGet(categoriesAtWordIndex, i))) w.addClass(cat);
    }
    for (int i = 0; i < l(words)-1; i++)
      linkWords(words.get(i), words.get(i+1));
    //printStruct(first(words));
    
    addAll(allObjects, words);
    long lastChanges;
    do {
      lastChanges = changes;
      //print(n2(changes, "change"));
      for (Updatable w : cloneList(allObjects))
        w.update();
    } while (licensed() && lastChanges != changes);
  }
  
  void printWordsAndGroups() {
    for (Word w : words) print("  " + textAndClasses(w));
    print();
      
    List<Word> groups = groups();
    print();
    print(n2(groups, "group"));
    for (Word g : groups)
      print("Group: " + groupedTextAndClasses(g));
  }
  
  void printConstituentsOfFullGroup() {
    Word g = fullGroup();
    if (g == null) return;
    print();
    pnl(allGroupings(g));
  }
  
  List<Word> words() { return instancesOf(Word.class, allObjects); }
  List<Word> groups() { return filter(words() , new F1<Word, Object>() { Object get(Word w) { try { return  w.isGroup() ; } catch (Exception __e) { throw rethrow(__e); } }
  public String toString() { return "w.isGroup()"; }}); }
  
  // only one with default flags
  List<Word> fullGroups() {
    return filterByFields(words(), "wordFrom" , 0, "wordTo" , l(words));
  }
  
  Word fullGroup() {
    return findByFields(words(), "wordFrom" , 0, "wordTo" , l(words));
  }
  
  Set<String> fullClasses() {
    TreeSet<String> set = new TreeSet();
    for (Word g : fullGroups())
      set.addAll(g.classes);
    return set;
  }
  
  String bracketStuff(Word w) {
    return " (" + joinWithComma(w.classes) + (empty(w.constituents) ? "" : ", " + n2(w.constituents, "grouping")) +  ")";
  }
  
  String textAndClasses(Word w) { return w.text + bracketStuff(w); }
  String groupedTextAndClasses(Word w) { return grouped(w) + bracketStuff(w); }
  
  String fullGrouped() { return grouped(fullGroup()); }
  
  void linkWords(Word a, Word b) {
    a.next.add(b);
    b.prev.add(a);
  }
  
  void change() { ++changes; }

  void parseProductions() {
    for (WeightedProduction p : 
      productionsText == null ? ai_buParser_parseWeightedProductions() : ai_buParser_parseWeightedProductions(productionsText))
      if (p.b != null)
        productionsByA.put(p.a, p);
      else
        subClasses.put(p.a, p.c);
  }

  // TODO: now there are multiple groupings
  String grouped(Word g) {
    if (g == null) return null;
    if (empty(g.constituents)) return g.text;
    return groupedConstituents(first(g.constituents));
  }
  
  String groupedConstituents(List<Word> constituents) {
    List<String> l = new ArrayList();
    for (Word w : constituents)
      l.add(curlyBraceIfMultipleTokens(grouped(w)));
    return joinWithSpace(l);
  }
  
  Set<String> allGroupings(Word g) {
    if (empty(g.constituents)) return litorderedset(g.text);
    LinkedHashSet<String> set = new LinkedHashSet();
    for (List<Word> l : g.constituents)
      set.add(groupedConstituents(l));
    return set;
  }
}static class NLStringMatcher_dollarVars_plusBrackets extends StringMatcher {
  boolean match(String pat, String input, Map<String, String> matches) {
    Map<String, String> newMatches = matchDollarVarsIC_plusBrackets(pat, input);
    return newMatches != null && addMapToMapWithoutOverwritingIC(matches, newMatches);
  }
}static class JavaXClassLoader extends URLClassLoader {
  String progID;
  List<File> files;
  Set<Class> loadedClasses = synchroSet();
  boolean retired;
  Object retiredMarker;
  
  JavaXClassLoader(String progID, List<File> files) {
    this(progID, files, getSystemClassLoader());
  }
  
  JavaXClassLoader(String progID, List<File> files, ClassLoader parent) {
    super(new URL[0], parent);
    this.progID = progID;
    this.files = files;
    
    { try {
      for (File f : files)
        addURL(f.toURI().toURL());
    } catch (Exception __e) { throw rethrow(__e); } }
    
    fixACCInClassLoader(this);
  }
  
  protected Class<?> findClass(String name) throws ClassNotFoundException {
    try {
      Class<?> c = super.findClass(name);
      loadedClasses.add(c);
      if (eq(name, "main"))
        callOpt(javax(), "registerAMainClass", c);
      return c;
    } catch (ClassNotFoundException e) {
      throw new ClassNotFoundException("Class " + name + " not found in " + joinWithComma(map("f2s", files)) + " (progID=" + progID + ")");
    }
  }
  
  public String toString() {
    return "JavaXClassLoader[" + systemHashCodeHex(this) + "] - " + progID;
  }
}
static class JavaXClassLoaderWithParent extends JavaXClassLoader {
  ClassLoader virtualParent;

  JavaXClassLoaderWithParent(String progID, List<File> files, ClassLoader virtualParent) {
    super(progID, files);
    this.virtualParent = virtualParent;
  }
  
   protected Class<?> findClass(String name) throws ClassNotFoundException {
    if (!eq(name, "main") && !name.startsWith("main$")) {
      try {
        return virtualParent.loadClass(name);
      } catch (ClassNotFoundException e) {}
    }
    return super.findClass(name);
  }
}
    static class Snippet {
  String id, title, md5, type, text;
  
  Snippet() {}
  Snippet(String id, String title, String md5) {
  this.md5 = md5;
  this.title = title;
  this.id = id;}
  
  public String toString() { return id + " - " + title; }
}/** this class is fully thread-safe */
static class Flag {
  private boolean up;

  /** returns true if flag was down before */
  public synchronized boolean raise() {
    if (!up) {
      up = true;
      notifyAll();
      return true;
    } else
      return false;
  }

  public synchronized void waitUntilUp() {
    while (!up) {
      try {
        wait();
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
  }

  public synchronized void waitUntilUp(long timeout) {
    if (!up) {
      try {
        wait(timeout);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
  }

  public synchronized boolean isUp() {
    return up;
  }

  public String toString() {
    return isUp() ? "up" : "down";
  }

  // currently does a semi-active wait with latency = 50 ms
  public void waitForThisOr(Flag otherFlag) { try {
    while (!isUp() && !otherFlag.isUp())
      Thread.sleep(50);
  } catch (Exception __e) { throw rethrow(__e); } }
}static abstract class CloseableIterableIterator<A> extends IterableIterator<A> implements AutoCloseable {
  public void close() throws Exception {}
}static class ExpiringMap2<A, B> extends AbstractMap<A, B> {
  Map<A, Pair<Long, B>> byKey = new HashMap();                 // key -> pair(expiry sys time, value)
  PriorityBlockingQueue < PairComparedByA < Long , A > > queue = new PriorityBlockingQueue(); // queue(pair(expiry sys time, key))
  long standardExpiryTime; // ms
  boolean renewOnOverwrite = true;
  Object onChange;
  // new RestartableCountdown countdown; // TODO
  
  ExpiringMap2() {}
  ExpiringMap2(long standardExpiryTime) {
  this.standardExpiryTime = standardExpiryTime;}
  ExpiringMap2(long standardExpiryTime, Object onChange) {
  this.onChange = onChange;
  this.standardExpiryTime = standardExpiryTime;}
  
  boolean clean() {
    boolean changes = false;
    Pair<Long, A> p;
    while ((p = queue.peek()) != null && sysTime() >= p.a) {
      p = queue.poll();
      
      Pair<Long, B> v = byKey.get(p.b);
      if (v != null /*&& v.a == p.a*/) {
        
        byKey.remove(p.b);
        changes = true;
        change();
      }
    }
    return changes;
  }
  
  void change() { callF(onChange); }
  
  public B put(A a, B b) {
    clean();
    long timeout = sysTime()+standardExpiryTime;
    Pair<Long, B> p = byKey.get(a);
    if (p != null && renewOnOverwrite)
      queue.remove(new PairComparedByA(p.a, a));
    byKey.put(a, pair(timeout, b));
    change();
    if (p == null || renewOnOverwrite)
      queue.add(new PairComparedByA(timeout, a));
    return pairB(p);
  }
  
  public B remove(Object a) {
    clean();
    Pair<Long, B> p = byKey.get(a);
    if (p == null) return null;
    queue.remove(new PairComparedByA(p.a, a));
    byKey.remove(a);
    change();
    return p.b;
  }
  
  public B get(Object a) {
    clean();
    return pairB(byKey.get(a));
  }
  
  public Set<Map.Entry<A,B>> entrySet() {
    clean();
    // TODO: mutex
    return mapValues("pairB", byKey).entrySet();
  }
  
  public Set<A> keySet() {
    clean();
    return byKey.keySet();
  }
  
  public int size() {
    clean();
    return byKey.size();
  }
  
  void setStandardExpiryTime(long ms) { standardExpiryTime = ms; }
  
  ExpiringMap2<A, B> setMap(Map innerMap) {
    byKey = innerMap;
    return this;
  }
}
static interface Producer<A> {
  public A next();
}static class WeightedProduction {
  String a, b, c; // a + b = c; for subclassing, b = null
  double weight;
  String simplificationHint, plausibilityFunction;
  
  WeightedProduction() {}
  WeightedProduction(String a, String b, String c, double weight) {
  this.weight = weight;
  this.c = c;
  this.b = b;
  this.a = a;}
  WeightedProduction(String a, String c, double weight) {
  this.weight = weight;
  this.c = c;
  this.a = a;}
  
  public String toString() {
    return a + (b == null ? "" : " + " + b) + " = " + c
      + appendSquareBracketStuff(simplificationHint);
  }
}static class PTElement {
  AI_BottomUpParser1.Word word;
  double weight = 1, totalWeight;
  List<PTElement> children;
  PTElement parent;
  
  String plusWeight(String s) {
    if (weight == 1 && totalWeight == 1) return s;
    String a = formatDouble(weight, 2), b = formatDouble(totalWeight, 2);
    return s + " [" + a + (eq(a, b) ? "" : ", total " + b) + "]";
  }
  
  String text() { return word == null ? null : word.text(); }
  String category() { return null; }
}

static class ChooseCategory extends PTElement {
  List<String> categories;
  
  ChooseCategory(AI_BottomUpParser1.Word word, List<String> categories) {
  this.categories = categories;
  this.word = word;}

  public String toString() { return plusWeight(word.text() + " (" + n2(categories, "category", "categories") + ")"); }
}

static class HaveCategory extends PTElement {
  String category;
  ai_Reconstructed reconstruction; // optional
  
  HaveCategory(AI_BottomUpParser1.Word word, String category) {
  this.category = category;
  this.word = word;}
  HaveCategory(AI_BottomUpParser1.Word word, String category, ai_Reconstructed reconstruction) {
  this.reconstruction = reconstruction;
  this.category = category;
  this.word = word;}
  
  public String toString() { return plusWeight(quote(word.text()) + " as " + category); }
  String category() { return category; }
}

static class ChoosePart extends PTElement {
  ai_Reconstructed reconstruction;
  
  ChoosePart(AI_BottomUpParser1.Word word, ai_Reconstructed reconstruction) {
  this.reconstruction = reconstruction;
  this.word = word;}
  
  public String toString() {
    String hint = reconstruction.production.simplificationHint;
    return plusWeight(
      reconstruction.a.text() + " + " + reconstruction.b.text()
        + " [" + reconstruction.production + "]");
  }
  String category() { return reconstruction.production.c; }
}
static class MechMode {
  static String _fieldOrder = "readMode writeMode false x h h h h";
  Object readMode;
  Object writeMode;
  MechMode() {}
  MechMode(Object readMode, Object writeMode) {
  this.writeMode = writeMode;
  this.readMode = readMode;}
  public String toString() { return "MechMode(" + readMode + ", " + writeMode + ")"; }public boolean equals(Object o) {
    if (!(o instanceof MechMode)) return false;
    MechMode x = (MechMode) ( o);
    return eq(readMode, x.readMode) && eq(writeMode, x.writeMode);
  }
  
  public int hashCode() {
    int h = -866263200;
    h = h*2+_hashCode(readMode);
    h = h*2+_hashCode(writeMode);
    return h;
  }
  }static class Cache<A> {
  Object maker; // func -> A
  A value;
  long loaded;
  static boolean debug;
  long changeCount;
  Lock lock = lock();
  
  Cache() {}
  Cache(Object maker) {
  this.maker = maker;}

  A get() {
    if (hasLock(lock)) return value; // Must be called from within maker
    Lock __2371 = lock; lock(__2371); try {
    if (loaded == 0) {
      value = (A) callF(maker);
      changeCount++;
      loaded = sysNow();
    }
    return value;
  } finally { unlock(__2371); } }

  void clear() {
    Lock __2372 = lock; lock(__2372); try {
    if (debug && loaded != 0)
      print("Clearing cache");
    value = null;
    changeCount++;
    loaded = 0;
  } finally { unlock(__2372); } }

  // clear if older than x seconds
  // 0 does not do anything
  void clear(double seconds) {
    Lock __2373 = lock; lock(__2373); try {
    if (seconds != 0 && loaded != 0 && sysNow() >= loaded+seconds*1000)
      clear();
  } finally { unlock(__2373); } }
  
  // override
  void set(A a) {
    Lock __2374 = lock; lock(__2374); try {
    value = a;
    ++changeCount;
    loaded = sysNow();
  } finally { unlock(__2374); } }
}

static class PairComparedByA<A, B> extends Pair<A, B> {
  PairComparedByA() {}
  PairComparedByA(A a, B b) {
  this.b = b;
  this.a = a;}
  
  public boolean equals(Object o) {
    return o instanceof Pair && eq(a, ((Pair) o).a);
  }
  
  public int compareTo(Pair<A, B> p) {
    return stdcmp(a, p.a);
  }
}final static class IntRange {
  int start, end;
  
  IntRange() {}
  IntRange(int start, int end) {
  this.end = end;
  this.start = start;}
  
  public boolean equals(Object o) { return stdEq2(this, o); }
public int hashCode() { return stdHash2(this); }
  
  int length() { return end-start; }
  
  static String _fieldOrder = "start end";
}

static <A, B> B pairB(Pair<A, B> p) {
  return p == null ? null : p.b;
}
static <A, B> Map<A, B> putAll(Map<A, B> a, Map<? extends A,? extends B> b) {
  if (a != null) a.putAll(b);
  return a;
}
static <A, B> Set<A> keySet(Map<A, B> map) {
  return map == null ? new HashSet() : map.keySet();
}

static Set keySet(Object map) {
  return keys((Map) map);
}




  static <A, B> Set<A> keySet(MultiMap<A, B> mm) {
    return mm.keySet();
  }



static Map<String, String> matchDollarVarsIC_underscores(String pat, String s) {
 try {
  if (pat == null || s == null) return null;
  List<String> vars = new ArrayList();
  String pat2 = dropSuffix("...", dollarVarsToStars_underscoresWithDollarQ(pat, vars));
  s = dropSuffix("...", s);
  Matches m = new Matches();
  if (!match(pat2, s, m)) return null;
  Map<String, String> map = new HashMap();
  int v = 0;
  for (int i = 0; i < l(m.m); ) {
    String var = vars.get(v++);
    boolean quoted = startsWith(var, "$_q_");
    if (quoted) {
      String value = m.m[i++];
      if (!isQuoted(value)) return null;
      var = "$" + substring(var, 4);
      if (!strictPutIC(map, var, unquote(value))) return null;
    } else {
      int n = underscoreWordCount(var);
      String value = joinWithSpace(subStringArray(m.m, i, i+n));
      if (!strictPutIC(map, var, value)) return null;
      i += n;
    }
  }
  return map;
 } catch (Throwable __e) { _handleException(__e); } return null;
}
static String b(Object contents, Object... params) {
  return tag("b", contents, params);
}
static String getServerTranspiled2(String id) {
  String transpiled = loadCachedTranspilation(id);
  String md5 = null;
  if (isOfflineMode()) return transpiled;
  if (transpiled != null)
    md5 = md5(transpiled);
  String transpiledSrc = getServerTranspiled(formatSnippetID(id), md5);
  if (eq(transpiledSrc, "SAME")) {
    if (!isTrue(loadPage_silent.get())) print("SAME");
    return transpiled;
  }
  return transpiledSrc;
}
static boolean useDummyMainClasses() {
  return true;
  //ret eq("1", trim(loadTextFile(getProgramFile(#1008755, "use-dummy-main-classes"))));
}
static boolean tok_classHasModifier(List<String> classDecl, String modifier) {
  int i = classDecl.indexOf("class");
  return subList(classDecl, 0, i).contains(modifier);
}
// returns actual CNC
static List<String> findMainClass(List<String> tok) {
  for (List<String> c : reversedList(allClasses(tok))) {
    String name = getClassDeclarationName(c);
    if (eq(name, "main") || name.startsWith("x"))
      return c;
  }
  return findBlock("m {", tok);
}
// before you use this, add a RAM disk cleaner
static boolean javaCompileToJar_useRAMDisk;

static File javaCompileToJar_optionalRename(String src, File destJar, String progID) {
  return javaCompileToJar_optionalRename(src, "", destJar, progID);
}

// returns path to jar
static synchronized File javaCompileToJar_optionalRename(String src, String dehlibs, File destJar, String progID) {
  String javaTarget = null; // use default target
  
  //print("Compiling " + l(src) + " chars");
  String dummyClass = "main";
  if (progID != null) {
    dummyClass = dummyMainClassName(progID);
    src += "\nclass " + dummyClass + "{}";
  }
  String md5 = md5(src);
  File jar = destJar;

  Class j = getJavaX();
  if (javaTarget != null)
    setOpt(j, "javaTarget", javaTarget);
  //setOpt(j, "verbose", true);
  File srcDir = tempDir();
  String className = "main";
  String fileName = dummyClass + ".java";
  File mainJava = new File(srcDir, fileName);
  //print("main java: " + mainJava.getAbsolutePath());
  saveTextFile(mainJava, src);
  File classesDir = javaCompileToJar_useRAMDisk ? tempDirPossiblyInRAMDisk() : tempDir();
  //print("Compiling to " + f2s(classesDir));
  try {
    List<File> libraries = new ArrayList();
    
    Matcher m = Pattern.compile("\\d+").matcher(dehlibs);
    while (m.find()) {
      String libID = m.group();
      //print("libID=" + quote(libID));
      assertTrue(isSnippetID(libID));
      libraries.add(loadLibrary(libID));
    }
    
    libraries.add(pathToJavaxJar());
      
    String compilerOutput;
    try {
      compilerOutput = (String) call(j, "compileJava", srcDir, libraries, classesDir);
    } catch (Throwable e) {
      compilerOutput = (String) get(getJavaX(), "javaCompilerOutput");
      throw fail("Compile Error. " + compilerOutput + " " + e);
    }
    
    if (nempty(compilerOutput)) {
      print("Compiler said: " + compilerOutput);
      //fail("Compile Error. " + compilerOutput);
    }
  
    // sanity test
    if (!new File(classesDir, className + ".class").exists())
      throw fail("No class generated (" + className + ")");
        
    // add sources to .jar
    saveTextFile(new File(classesDir, "main.java"), src);
    
    // add information about libraries to jar
    if (nempty(dehlibs))
      saveTextFile(new File(classesDir, "libraries"), dehlibs);
  
    //print("Zipping: " + classesDir.getAbsolutePath() + " to " + jar.getAbsolutePath());
    dir2zip_recurse_verbose = false;
    int n = dir2zip_recurse(classesDir, jar); // cache on success only
    //print("Files zipped: " + n);
  
    return jar;
  } finally {
    if (isInRAMDisk(classesDir)) deleteDirectory(classesDir);
  }
}

// will create the file or update its last modified timestamp
static void touchFile(File file) { try {
  closeRandomAccessFile(newRandomAccessFile(mkdirsForFile(file), "rw"));
} catch (Exception __e) { throw rethrow(__e); } }
static File getCacheProgramDir() {
  return getCacheProgramDir(getProgramID());
}

static File getCacheProgramDir(String snippetID) {
  return new File(userHome(), "JavaX-Caches/" + formatSnippetIDOpt(snippetID));
}
static String quoteBorderless(Object o) {
  if (o == null) return "null";
  return quoteBorderless(str(o));
}

static String quoteBorderless(String s) {
  if (s == null) return "null";
  StringBuilder out = new StringBuilder((int) (l(s)*1.5));
  quoteBorderless_impl(s, out);
  return out.toString();
}
  
static void quoteBorderless_impl(String s, StringBuilder out) {
  int l = s.length();
  for (int i = 0; i < l; i++) {
    char c = s.charAt(i);
    if (c == '\\' || c == '"')
      out.append('\\').append(c);
    else if (c == '\r')
      out.append("\\r");
    else if (c == '\n')
      out.append("\\n");
    else
      out.append(c);
  }
}
static void change() {
  //mainConcepts.allChanged();
  // safe version for now cause function is sometimes included unnecessarily (e.g. by EGDiff)
  callOpt(getOptMC("mainConcepts"), "allChanged");
}
static String lower(String s) {
  return s == null ? null : s.toLowerCase();
}

static char lower(char c) {
  return Character.toLowerCase(c);
}
static <A> void moveElementFromCollectionToCollection(A a, Collection<A> src, Collection<A> target) {
  if (src != null) src.remove(a);
  if (target != null) target.add(a);
}
// iterate safely (& quickly) in the face of concurrent modifications
static <A> IterableIterator<A> iterateListConcurrently(List<A> l) {
  return concurrentlyIterateList(l);
}
static LinkedHashSet<String> makeHull_optimized(MultiMap<String, String> map, String e) {
  LinkedHashSet<String> l = new LinkedHashSet();
  makeHull_optimized_impl(map, e, l);
  return l;
}

static void makeHull_optimized_impl(MultiMap<String, String> map, String e, LinkedHashSet<String> dest) {
  if (dest.contains(e))
    return;
  dest.add(e);
  for (String f : map.get(e))
    makeHull_optimized_impl(map, f, dest);
}

static boolean ai_parser_activateStandardFunctions_flag = true;

static void ai_parser_activateStandardFunctions() {
  ai_parser_activateStandardFunctions_flag = true;
  loadFunctions_preferDiskCache();
}

static boolean ai_parser_activateStandardFunctions_get() {
  return ai_parser_activateStandardFunctions_flag;
}
static void loadFunctions_preferDiskCache() {
  loadFunctions_preferDiskCache = true;
}
static Object pcallAndMake(String functionName, Object... args) {
  try {
    return callAndMake(functionName, args);
  } catch (Throwable __e) { _handleException(__e); }
  return null;
}
static List<String> words_withAngleBrackets(String s) {
  return codeTokens(dropPunctuation(javaTokWithAngleBrackets(s)));
}
static void ai_splitSplittables(List<String> tok) {
  MultiMap<String, String> splits = mechMultiMapCI(ai_language() + " splittables => split");
  for (int i = 0; i < l(tok); i++) {
    List<String> l = splits.get(tok.get(i));
    if (nempty(l))
      replaceSubList(tok, i, i+1, words(first(l))); // For now, take first possibility
  }
}
static <A extends Iterable> A pnl(A l) {
  printNumberedLines(l);
  return l;
}

static void pnl(Map map) {
  printNumberedLines(map);
}

static void pnl(Object[] a) {
  printNumberedLines(a);
}

static void pnl(String s) {
  printNumberedLines(lines(s));
}
static <A> List<A> instancesOf(Iterable i, Class<A> c) {
  return collectInstances(i, c);
}



static <A> List<A> instancesOf(Class<A> c, Iterable i) {
  return collectInstances(c, i);
}
static <A> List<A> filterByFields(Collection<A> c, Object... data) {
  List<A> l = new ArrayList();
  for (A x : c)
    if (checkFields(x, data))
      l.add(x);
  return l;
}
static <A> A findByFields(Iterable<A> c, Object... data) {
  if (c != null) for (A x : c)
    if (checkFields(x, data))
      return x;
  return null;
}
static String curlyBraceIfMultipleTokens(String s) {
  return curlyBraceIfMoreThanOneToken(s);
}
static Map<String, String> matchDollarVarsIC_plusBrackets(String pat, String s) {
  if (pat == null || s == null) return null;
  List<String> vars = new ArrayList();
  String pat2 = dollarVarsToStars(pat, vars);
  Matches m = new Matches();
  if (!match3_plusBrackets(pat2, s, m)) return null;
  Map<String, String> map = new HashMap();
  for (int i = 0; i < l(m.m); i++) {
    String var = vars.get(i);
    String value = m.get(i);
    if (startsWith(var, "$_q_")) {
      if (!isQuoted(value)) return null;
      var = "$" + substring(var, 4);
      value = unquote(value);
    }
    if (!strictPutIC(map, var, value)) return null;
  }
  return map;
}
static void fixACCInClassLoader(Object o) {
  try {
    AccessControlContext acc = vm_globalACC();
    if (acc != null)
      replaceACCInClassLoader(o, acc);
  } catch (Throwable __e) { _handleException(__e); }
}
static String systemHashCodeHex(Object o) {
  return intToHex(identityHashCode(o));
}
static long sysTime() {
  return sysNow();
}
static Map mapValues(Object func, Map map) {
  Map m = similarEmptyMap(map);
  for (Object key : keys(map))
    m.put(key, callF(func, map.get(key)));
  return m;
}

static Map mapValues(Map map, Object func) {
  return mapValues(func, map);
}
static String appendSquareBracketStuff(String b) {
  return appendSquareBracketed(b);
}



static String appendSquareBracketStuff(String a, String b) {
  return appendSquareBracketed(a, b);
}
static String formatDouble(double d, int digits) {
  String format = "0." + rep(digits, '#');
  return new java.text.DecimalFormat(format, new java.text.DecimalFormatSymbols(Locale.ENGLISH)).format(d);
}

static boolean hasLock(Lock lock) {
  return ((ReentrantLock) lock).isHeldByCurrentThread();
}
static int stdcmp(Number a, Number b) {
  return cmp(a, b);
}

static int stdcmp(String a, String b) {
  return cmp(a, b);
}

static int stdcmp(long a, long b) {
  return a < b ? -1 : a > b ? 1 : 0;
}

static int stdcmp(Object a, Object b) {
  return cmp(a, b);
}

static boolean stdEq2(Object a, Object b) {
  if (a == null) return b == null;
  if (b == null) return false;
  if (a.getClass() != b.getClass()) return false;
  for (String field : allFields(a))
    if (neq(getOpt(a, field), getOpt(b, field)))
      return false;
  return true;
}
static int stdHash2(Object a) {
  if (a == null) return 0;
  return stdHash(a, toStringArray(allFields(a)));
}


static String dollarVarsToStars_underscoresWithDollarQ(String s) {
  return dollarVarsToStars_underscoresWithDollarQ(s, null);
}

static String dollarVarsToStars_underscoresWithDollarQ(String s, List<String> varNames_out) {
  return join(dollarVarsToStars_underscoresWithDollarQ(javaTok(s), varNames_out));
}

static List<String> dollarVarsToStars_underscoresWithDollarQ(List<String> tok, List<String> varNames_out) {
  for (int i = 1; i < l(tok); i += 2) {
    String t = tok.get(i);
    if (isDollarVar(t)) {
      listAdd(varNames_out, t);
      tok.set(i,
          t.startsWith("$_q_") ? "*" // "quoted" var
        : joinWithSpace(rep("*", underscoreWordCount(t))));
    } else if (eq(t, "*"))
      listAdd(varNames_out, "*");
  }
  return tok;
}

static int underscoreWordCount(String s) {
  return countChar(s, '_')+1;
}
static String[] subStringArray(String[] b, int start, int end) {
  start = max(start, 0); end = min(end, l(b));
  if (start == 0 && end == l(b)) return b;
  if (start >= end) return new String[0];
  String[] x = new String[end-start];
  System.arraycopy(b, start, x, 0, end-start);
  return x;
}
static boolean isOfflineMode() {
  return eq("1", trim(loadProgramTextFile("#1005806", "offline-mode")));
}
static String getServerTranspiled(String snippetID) {
  return getServerTranspiled(snippetID, null);
}

// returns "SAME" if md5 matches
static String getServerTranspiled(String snippetID, String expectedMD5) { try {
  long id = parseSnippetID(snippetID);
  /*S t = getTranspilationFromBossBot(id);
  if (t != null) return t;*/
  
  String text = loadPage_utf8(tb_mainServer() + "/tb-int/get-transpiled.php?raw=1&withlibs=1&id=" + id + "&utf8=1"
    + (l(expectedMD5) > 1 ? "&md5=" + urlencode(expectedMD5) : "")
    + standardCredentials());
  if (nempty(text) && neq(text, "SAME"))
    saveTranspiledCode(snippetID, text);
  return text;
} catch (Exception __e) { throw rethrow(__e); } }
static HashSet<String> allClasses_keywords = lithashset("class", "interface", "enum", "sclass", "sinterface");

// lists returned are actual CNC (N/C/N/.../C/N) - and connected to
// original list
// only returns the top level classes
static List<List<String>> allClasses(List<String> tok) {
  List<List<String>> l = new ArrayList();
  for (int i = 1; i < tok.size(); i += 2) {
    if (eq(tok.get(i), "{")) // skip functions
      i = findEndOfBlock(tok, i)-1;
    else if (allClasses_keywords.contains(tok.get(i)) && (i == 1 || !tok.get(i-2).equals("."))) {
      int j = i;
      while (j < tok.size() && !tok.get(j).equals("{"))
        j += 2;
      j = findEndOfBlock(tok, j)+1;
      i = leftScanModifiers(tok, i);
      l.add(tok.subList(i-1, Math.min(tok.size(), j)));
      i = j-2;
    }
  }
  return l;
}

static List<List<String>> allClasses(String text) {
  return allClasses(javaTok(text));
}
static String getClassDeclarationName(List<String> c) {
  if (c != null)
    for (int i = 1; i+2 < c.size(); i += 2)
      if (allClasses_keywords.contains(c.get(i)))
        return c.get(i+2);
  return null;
}

static List<String> findBlock(String pat, List<String> tok) {
  List<String> tokpat = javaTok(pat);
  int i = findCodeTokens(tok, toStringArray(codeTokensOnly(tokpat)));
  //print("index of block " + quote(pat) + ": " + i);
  if (i < 0) return null;
  int bracketIdx = i+tokpat.size()-3;
  assertEquals("{", tok.get(bracketIdx));
  int endIdx = findEndOfBlock(tok, bracketIdx);
  return subList(tok, i-1, endIdx+1); // make it actual CNC
}
static String dummyMainClassName(String progID) {
  return "m" + psI(progID);
}
  static File tempDir() {
    return makeTempDir();
  }
static File tempDirPossiblyInRAMDisk() {
  File f = linux_fileInRamDisk(aGlobalID());
  if (f != null) { f.mkdirs(); return f; }
  return makeTempDir();
}
static File pathToJavaxJar() { try {
  int x = latestInstalledJavaX();
  File xfile = new File(userHome(), ".javax/x" + Math.max(x, 30) + ".jar");
  if (!xfile.isFile()) {
    print("Saving " + f2s(xfile));
    String url = "http://tinybrain.de/x30.jar";
    byte[] data = loadBinaryPage(url);
    if (data.length < 1000000)
      throw fail("Could not load " + url);
    saveBinaryFile(xfile.getPath(), data);
  }
  return xfile;
} catch (Exception __e) { throw rethrow(__e); } }
static boolean dir2zip_recurse_verbose;

static int dir2zip_recurse(File inDir, File zip) {
  return dir2zip_recurse(inDir, zip, "");
}

// TODO: the zero files case?
static int dir2zip_recurse(File inDir, File zip, String outPrefix) { try {
  mkdirsForFile(zip);
  FileOutputStream fout = newFileOutputStream(zip);
  ZipOutputStream outZip = new ZipOutputStream(fout);
  try {
    return dir2zip_recurse(inDir, outZip, outPrefix, 0);
  } finally {
    outZip.close();
  }
} catch (Exception __e) { throw rethrow(__e); } }

static int dir2zip_recurse(File inDir, ZipOutputStream outZip) {
  return dir2zip_recurse(inDir, outZip, "", 0);
}

static int dir2zip_recurse(File inDir, ZipOutputStream outZip, String outPrefix, int level) { try {
  if (++level >= 20) throw fail("woot? 20 levels in zip?");
  
  List<File> files = new ArrayList();
  for (File f : listFiles(inDir))
    files.add(f);

  int n = 0;
  sortFilesByName(files);
  for (File f : files) {
    if (f.isDirectory()) {
      print("dir2zip_recurse: Scanning " + f.getAbsolutePath());
      n += dir2zip_recurse(f, outZip, outPrefix + f.getName() + "/", level);
    } else {
      if (dir2zip_recurse_verbose) print("Copying " + f.getName());
      outZip.putNextEntry(new ZipEntry(outPrefix + f.getName()));
      InputStream fin = new FileInputStream(f);
      copyStream(fin, outZip);
      fin.close();
      ++n;
    }
  }
  return n;
} catch (Exception __e) { throw rethrow(__e); } }

static boolean isInRAMDisk(File f) {
  return startsWith(f2s(f), "/dev/shm");
}
static int deleteDirectory_minPathLength = 10;

static void deleteDirectory(File dir) {
  deleteDirectory(dir, false, false);
}

static void deleteDirectory(File dir, boolean verbose, boolean testRun) {
  dir = getCanonicalFile(dir);
  assertTrue(f2s(dir), l(f2s(dir)) >= deleteDirectory_minPathLength);
  File[] files = dir.listFiles();
  if (files == null) return;
  for (File f : files) {
    if (f.isDirectory())
      deleteDirectory(f, verbose, testRun);
    else {
      if (verbose)
        print((testRun ? "Would delete " : "Deleting ") + f.getAbsolutePath());
      if (!testRun)
        f.delete();
    }
  }
  if (verbose) print((testRun ? "Would delete " : "Deleting ") + dir.getAbsolutePath());
  if (!testRun)
    dir.delete();
}

static void closeRandomAccessFile(RandomAccessFile f) {
  if (f != null) try {
    f.close();
    callJavaX("dropIO", f);
  } catch (Throwable e) {
    printStackTrace(e);
  }
}
static RandomAccessFile newRandomAccessFile(File path, String mode) throws IOException {
  RandomAccessFile f = new RandomAccessFile(path, mode);
  callJavaX("registerIO", f, path, mode.indexOf('w') >= 0);
  return f;
}
// iterate safely (& quickly) in the face of concurrent modifications
static <A> IterableIterator<A> concurrentlyIterateList(final List<A> l) {
  return iteratorFromFunction_withEndMarker_f0(new F0<A>() {
    int i;

    A get() {
      int _i = i++;
      synchronized(l) {
        if (_i < l(l)) {
          A a = l.get(_i);
          if (a == iteratorFromFunction_endMarker) throw fail("no"); // ugly comparison fail-fast redemption
          return a;
        }
        return (A) iteratorFromFunction_endMarker; // ugly cast
      }
    }
  });
}
static List<String> replaceSubList(List<String> l, List<String> x, List<String> y) {
  return replaceSublist(l, x, y);
}

static <A> List<A> replaceSubList(List<A> l, int fromIndex, int toIndex, List<A> y) {
  return replaceSublist(l, fromIndex, toIndex, y);
}
static <A> List<A> printNumberedLines(List<A> l) {
  printNumberedLines((Collection<A>) l);
  return l;
}

static void printNumberedLines(Map map) {
  printNumberedLines(mapToLines(map));
}

static <A extends Iterable> A printNumberedLines(A l) {
  int i = 0;
  if (l != null) for (Object a : l) print((++i) + ". " + str(a));
  return l;
}

static void printNumberedLines(Object[] l) {
  printNumberedLines(asList(l));
}

static void printNumberedLines(Object o) {
  printNumberedLines(lines(str(o)));
}
static <A> List<A> collectInstances(Iterable i, Class<A> c) {
  List l = new ArrayList();
  if (i == null) return l;
  c = primitiveToBoxedTypeOpt(c);
  for (Object o : i)
    if (isInstance(c, o))
      l.add(o);
  return l;
}

static <A> List<A> collectInstances(Class<A> c, Iterable i) {
  return collectInstances(i, c);
}
static boolean checkFields(Object x, Object... data) {
  for (int i = 0; i < l(data); i += 2)
    if (neq(getOpt(x, (String) data[i]), data[i+1]))
      return false;
  return true;
}
static String curlyBraceIfMoreThanOneToken(String s) {
  return numberOfJavaTokens(s) > 1 ? curlyBrace(s) : s;
}
static boolean match3_plusBrackets(String pat, String s) {
  return match3_plusBrackets(pat, s, null);
}

static boolean match3_plusBrackets(String pat, String s, Matches matches) {
  if (pat == null || s == null) return false;
  return match3_plusBrackets(pat, parse3_plusBrackets/* TODO: _cachedInput*/(s), matches);
}
  
static boolean match3_plusBrackets(String pat, List<String> toks, Matches matches) {
  List<String> tokpat = parse3_cachedPattern(pat);
  return match3(tokpat, toks, matches);
}
static AccessControlContext vm_globalACC() {
  return vm_generalMap_get("Global ACC");
}
static void replaceACCInClassLoader(Object o, AccessControlContext newACC) {
  ClassLoader cl = getClassLoaderOrSame(o);
  if (cl instanceof URLClassLoader) try {
    setOpt(cl, "acc" , newACC);
    try {
      setOpt(getOpt(cl, "ucp"), "acc" , newACC);
    } catch (Throwable e) {
      printShortException(e);
      if (java10OrHigher())
        if (addDefaultVMOption("--add-opens java.base/jdk.internal.loader=ALL-UNNAMED"))
          print("Please restart the OS");
    }
  } catch (Throwable __e) { _handleException(__e); }
}
static String intToHex(int i) {
  return bytesToHex(intToBytes(i));
}
static int identityHashCode(Object o) {
  return System.identityHashCode(o);
}
static String appendSquareBracketed(String b) {
  return empty(b) ? "" : "" + " [" + b + "]";
}

static String appendSquareBracketed(String a, String b) {
  return a + appendSquareBracketed(b);
}
static Set<String> allFields(Object o) {
  TreeSet<String> fields = new TreeSet();
  Class _c = _getClass(o);
  do {
    for (Field f : _c.getDeclaredFields())
      fields.add(f.getName());
    _c = _c.getSuperclass();
  } while (_c != null);
  return fields;
}
static int stdHash(Object a, String... fields) {
  if (a == null) return 0;
  int hash = getClassName(a).hashCode();
  for (String field : fields)
    hash = hash*2+hashCode(getOpt(a, field));
  return hash;
}


static int countChar(String s, char c) {
  int n = 0, l = l(s), i = 0;
  while (true) {
    int j = s.indexOf(c, i);
    if (j < 0) break;
    ++n;
    i = j+1;
  }
  return n;
}
static String loadProgramTextFile(String name) {
  return loadTextFile(getProgramFile(name));
}

static String loadProgramTextFile(String progID, String name) {
  return loadTextFile(getProgramFile(progID, name));
}

static String loadProgramTextFile(String progID, String name, String defaultText) {
  return loadTextFile(getProgramFile(progID, name), defaultText);
}
static String loadPage_utf8(URL url) {
  return loadPage_utf8(url.toString());
}

static String loadPage_utf8(String url) {
  loadPage_charset.set("UTF-8");
  try {
    return loadPage(url);
  } finally {
    loadPage_charset.set(null);
  }
}
static void saveTranspiledCode(String progID, String code) {
  File dir = getCodeProgramDir(progID);
  new File(dir, "Transpilation").delete();
  saveGZTextFile(new File(dir, "Transpilation.gz"), code);
}
static int leftScanModifiers(List<String> tok, int i) {
  List<String> mod = getJavaModifiers();
  while (i > 1 && mod.contains(tok.get(i-2)))
    i -= 2;
  return i;
}

  static File makeTempDir() {
    return (File) call(getJavaX(), "TempDirMaker_make");
  }
static File linux_fileInRamDisk(String name) {
  if (!isLinux()) return null;
  File dir = newFile("/dev/shm");
  if (dir.isDirectory()) return newFile(dir, name);
  return null;
}
static int latestInstalledJavaX() {
  File[] files = new File(userHome(), ".javax").listFiles();
  int v = 0;
  if (files != null) for (File f : files) {
    Matcher m = Pattern.compile("x(\\d\\d\\d?)\\.jar").matcher(f.getName());
    if (m.matches())
      v = Math.max(v, Integer.parseInt(m.group(1)));
  }
  return v;
}
static File[] listFiles(File dir) {
  File[] files = dir.listFiles();
  return files == null ? new File[0] : files;
}

static File[] listFiles(String dir) {
  return listFiles(new File(dir));
}
static void sortFilesByName(List<File> l) {
  sort(l, new Comparator<File>() {
    public int compare(File a, File b) {
      return stdcompare(a.getName(), b.getName());
    }
  });
}
static File getCanonicalFile(File f) { try {
  return f == null ? null : f.getCanonicalFile();
} catch (Exception __e) { throw rethrow(__e); } }
static <A> A callJavaX(String method, Object... args) {
  return (A) callOpt(getJavaX(), method, args);
}
static List<String> mapToLines(Map map) {
  List<String> l = new ArrayList();
  for (Object key : keys(map))
    l.add(str(key) + " = " + str(map.get(key)));
  return l;
}
static Class primitiveToBoxedTypeOpt(Class type) {
  return or(primitiveToBoxedType(type), type);
}
static String curlyBrace(String s) {
  return "{" + s + "}";
}
static List<String> parse3_plusBrackets(String s) {
  return dropPunctuation(javaTokPlusBrackets2(s));
}
static ClassLoader getClassLoaderOrSame(Object o) {
  return o instanceof ClassLoader ? (ClassLoader) o : getClassLoader(o);
}
static void printShortException(Throwable e) {
  print(exceptionToStringShort(e));
}
static boolean java10OrHigher() {
  return parseFirstInt(javaVersion()) >= 10;
}
static boolean addDefaultVMOption(String option) {
  String s = defaultVMArgs(), old = s;
  if (!s.contains(option)) // rough
    s = trim(s + " " + option);
  if (eq(old, s)) return false;
  { setDefaultVMArgs(s); return true; }
}
static byte[] intToBytes(int i) {
  return new byte[] {
          (byte) (i >>> 24),
          (byte) (i >>> 16),
          (byte) (i >>> 8),
          (byte) i};
}
static int hashCode(Object a) {
  return a == null ? 0 : a.hashCode();
}


static void saveGZTextFile(File file, String contents) { try {
  File parentFile = file.getParentFile();
  if (parentFile != null)
    parentFile.mkdirs();
  String tempFileName = file.getPath() + "_temp";
  File tempFile = new File(tempFileName);
  if (contents != null) {
    if (tempFile.exists()) try {
      String saveName = tempFileName + ".saved." + now();
      copyFile(tempFile, new File(saveName));
    } catch (Throwable e) { printStackTrace(e); }
    FileOutputStream fileOutputStream = newFileOutputStream(tempFile.getPath());
    GZIPOutputStream gos = new GZIPOutputStream(fileOutputStream);
    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(gos, "UTF-8");
    PrintWriter printWriter = new PrintWriter(outputStreamWriter);
    printWriter.print(contents);
    printWriter.close();
    gos.close();
    fileOutputStream.close();
  }
  
  if (file.exists() && !file.delete())
    throw new IOException("Can't delete " + file.getPath());

  if (contents != null)
    if (!tempFile.renameTo(file))
      throw new IOException("Can't rename " + tempFile + " to " + file);
} catch (Exception __e) { throw rethrow(__e); } }
static List<String> getJavaModifiers_list = litlist("static", "abstract", "public", "private", "protected", "final", "native", "volatile", "synchronized", "transient");

static List<String> getJavaModifiers() {
  return getJavaModifiers_list;
}
static boolean isLinux() {
  return !isWindows() && !isMac() && !isAndroid();
}
static Class primitiveToBoxedType(Class type) {
  if (type == boolean.class) return Boolean.class;
  if (type == int.class) return Integer.class;
  if (type == long.class) return Long.class;
  if (type == float.class) return Float.class;
  if (type == short.class) return Short.class;
  if (type == char.class) return Character.class;
  if (type == byte.class) return Byte.class;
  if (type == double.class) return Double.class;
  return null;
}
static ClassLoader getClassLoader(Object o) {
  return o == null ? null : _getClass(o).getClassLoader();
}
static int parseFirstInt(String s) {
  return parseInt(jextract("<int>", s));
}
static String javaVersion() {
  return System.getProperty("java.version");
}
static String defaultVMArgs() {
  return javaxDefaultVMArgs();
}
static void setDefaultVMArgs(String args) {
  String oldArgs = javaxDefaultVMArgs();
  args = trim(args);
  if (neq(unnull(oldArgs), unnull(args))) {
    print();
    print("Changing default VM arguments from");
    print("  " + oldArgs);
    print("to");
    print("  " + args);
    print();
    saveProgramTextFile("#1005850", "default-vm-args", nullIfEmpty(args));
  }
}


public static boolean isWindows() {
  return System.getProperty("os.name").contains("Windows");
}
  static boolean isMac() {
    return System.getProperty("os.name").toLowerCase().contains("mac");
	}
// returns from C to C
static String jextract(String pat, String s) {
  return jextract(pat, javaTok(s));
}

static String jextract(String pat, List<String> tok) {
  List<String> tokpat = javaTok(pat);
  jfind_preprocess(tokpat);
  int i = jfind(tok, tokpat);
  if (i < 0) return null;
  int j = i + l(tokpat) - 2;
  return join(subList(tok, i, j));
}

static String javaxDefaultVMArgs() {
  return trim(loadProgramTextFile("#1005850", "default-vm-args", ""));
}
static File saveProgramTextFile(String name, String contents) {
  return saveTextFile(getProgramFile(name), contents);
}

static File saveProgramTextFile(String progID, String name, String contents) {
  return saveTextFile(getProgramFile(progID, name), contents);
}

}