Prevent possibility of unbounded computation in :search

Attack typeclasses in a much more principled manner. Now, even if we didn't
cut off when the score got too great, computation would eventually end.
Additionally, break the matching of types into distinct phases, which
should also help with performance.
Use a priority queue to improve performance when matching types in :search
Merge pull request #1333 from bmsherman/improvedSearch

Improved performance for :search