Check for cycles in unification results

Fixes #1754.