sbool lispMatchIC_xyzVars_debug;

static Map<S, Lisp> lispMatchIC_xyzVars(Lisp pat, Lisp nl) {
  new Map<S, Lisp> matches;
  ret lispMatchIC_xyzVars_sub(pat, nl, matches) ? matches : null;
}

// put additional vars in m with value = null
static bool lispMatchIC_xyzVars_sub(Lisp pat, Lisp nl, Map<S, Lisp> m) {
  if (pat == null || nl == null) ret false;
  if (pat.isLeaf() && (isXYZVar(pat.head) || m.containsKey(pat.head))) {
    if (lispMatchIC_xyzVars_debug) print("Var: " + pat.head + " => " + nl);
    ret lispMatchIC_xyzVars_putMatch(m, pat.head, nl);
  }
    
  if (neqic(pat.head, nl.head)) ret false;
  
  // heads identical, proceed to children
  int n = pat.size();
  if (n != nl.size()) ret false;
  
  for (int i = 0; i < n; i++) {
    if (lispMatchIC_xyzVars_debug) print("Sub " + i + ": " + pat.get(i) + " => " + nl.get(i));
    if (!lispMatchIC_xyzVars_sub(pat.get(i), nl.get(i), m))
      false;
  }
      
  true;
}

static bool lispMatchIC_xyzVars_putMatch(Map<S, Lisp> matches, S key, Lisp val) {
  Lisp oldValue = matches.get(key);
  if (oldValue != null) {
    if (!lispEqic(oldValue, val))
      false;
      //fail("multi-matching not implemented");
  } else
    matches.put(key, val);
  true;
}