!7

p-autorestart {
  new Matches m;
  for (S s : printIterate(mL("Things that should be"))) {
    temp tempIndent();
    //if "... should always be ..."
    //printIndent("I'm asking: Are \*$1*/ \*$2*/?");
    
    // Find question
    S q = lookupNL(mechMap("Should => Question"), s);
    if (q == null) continue with print("No question found");
    print(q);
    
    // Find code
    S code = lookupNL(mechMap("Question => Iteration"), q);
    if (code == null) continue with print("No code found");
    print(code);
    
    // Parse Forall
    L<S> tok = javaTok(code);
    int i = tok_forall(tok, m);
    if (i < 0) continue with print("No forall found");
    print("For each " + $1 + ": " + $2);
    
    S innerCode = $2;
    L<S> tok2 = javaTok($1);
    S var = assertIdentifier(lastToken(tok2));
    S entity = trimJoin(dropLastToken(tok2));
    
    S entityList = lookupNL(mechMap("Entity => List that lists all instances"), entity);
    if (entityList == null) continue with print("No entity list found");
    L<S> entities = mL(entityList);
    print("Have " + n2(entities, "entity", "entities") + " of type " + quote(entity) + "!");
    
    new L<S> questions;
    for (S e : entities) {
      printIndent("QUESTION: " + addAndReturn(questions, jreplace(innerCode, var, e)));
    }
    
    appendToMechList("Things that should be | Questions", questions);
  }
}