// Logic thread

static interface IEngine {
  public boolean yo(Native n);
  public void memorize(Lisp statement);
  public void forget(Lisp statement);
}

static class LThread {
  IEngine engine;
  new L<Lisp> statements;
  new SNLMatches m; // current variable assignments
  L<Lisp> ops;
  int opIdx;
  new L<Lisp> output;
  L<S> log;
  
  *(Lisp code) {
    this(null, code);
  }
  
  *(IEngine *engine, Lisp code) {
    ops = snlSplitOps(code);
  }
  
  boolean done() {
    ret opIdx >= l(ops);
  }
  
  // returns false if failed
  boolean step() {
    Lisp code = ops.get(opIdx++);
    boolean ok = stepImpl(code);
    if (log != null)
      log.add((ok ? "[OK] ": "[FAIL] ") + " step " + snlFromTree(code) + " with " + structure(m));
    ret ok;
  }
  
  void say(Lisp b) {
    output.add(snlApply(b, m.map));
  }
  
  boolean stepImpl(Lisp code) {
    if (code.isic("if *", "and *", "assume *")) {
      Lisp a = code.get(0);
      Lisp b = snlApply(a, m.map);
      
      if (query(b))
        ret true;
      
      // Not matched, fail
      
      if (code.isic("assume *"))
        say(snlToTree("assumption < failed < : < [" + snlFromTree(b) + "]"));
            
      ret false;
    }
    
    if (code.isic("say *", "then *")) {
      Lisp b = code.get(0);
      //print ("m: " + structure (m) + ", b: " + structure (b));
      output.add(snlApply(b, m.map));
      ret true;
    }
    
    if (code.isic("memorize *")) {
      if (engine != null) {
        Lisp b = snlApply(code.get(0), m.map);
        engine.memorize(b);
      }
      ret true;
    }
    
    if (code.isic("forget *")) {
      if (engine != null) {
        Lisp b = snlApply(code.get(0), m.map);
        engine.forget(b);
      }
      ret true;
    }
    
    // unknown code
    say(snlToTree("unknown < instruction < : < [" + snlFromTree(code) + "]"));
    ret false;
  }
  
  // true if succeeded, false if failed
  boolean run() {
    while (!done())
      if (!step()) {
        print("fail");
        ret false;
      }
    ret true;
  }
  
  boolean query(Lisp b) {
    // try all statements in context
    
    for (Lisp s : statements) {
    /*  if (snlMatch2(b, s, m)) */

      if (snlMatch2(s, b, new SNLMatches))
        ret true;
    }

    // try the low-level engine
    
    try {
      if (engine != null) {
        Native n = new Native(b);
        if (engine.yo(n))
          ret m.addAll(n.m);
      }
    } catch (Exception e) {
      
      if (log != null) {
        log.add("ERROR: " + getStackTrace(e));
        print(e);
      }
    }
      
    ret false;
  }
}