def extract_mg_h(sai):
    """
    Given an SAI, this find the most general pattern that will generate a
    match.

    E.g., ('sai', ('name', '?foa0'), 'UpdateTable', ('value', '?foa1'))
    will yield: {('name', '?foa0'), ('value', '?foa1')}
    """
    return frozenset({tuple(list(elem) + ['?constraint-val%i' % i])
                      for i, elem in enumerate(sai)
                      if isinstance(elem, tuple)})