!7

p-exp {
  PTElement input = ai_parseToCulledTree("What is {this funny thing}");
  PTElement pat = ai_parseToCulledTree("What is X");
  
  matchTrees(input, pat);
}

svoid matchTrees(PTElement pat, PTElement input) {
  print("Matching: " + pat + " / " + input);
  if (pat == null || input == null) ret;
  if (pat instanceof ChooseCategory && input instanceof ChooseCategory) {
    HaveCategory l = cast first(pat.children);
    HaveCategory r = cast first(input.children);
    if (l != null && r != null && eq(l.category, r.category)) {
      print("Using category: " + l.category);
      matchTrees(l, r);
    }
    ret;
  }
  if (pat instanceof HaveCategory && input instanceof HaveCategory) {
    matchTrees(first(pat.children), first(input.children));
    ret;
  }
  if (pat instanceof ChoosePart && input instanceof ChoosePart) {
    for i to 2:
      matchTrees(pat.children.get(i), input.children.get(i));
    ret;
  }
  print("Non-matching classes: " + className(pat) + " vs " + className(input));
}