unify :: Equations -> Maybe Substitution
unify equations = do equations' <- step equations
                     if equations == equations'
                        then return $ equationsToSubstitution equations
                        else unify equations'
  where equationsToSubstitution = S.map converter
          where converter (TEVar v, t) = (v,t)
                converter _ = error "unification algorithm didn't yield a substitution"
unify :: Equations -> Maybe Substitution
unify equations = do equations' <- step equations
                     if equations == equations'
                        then return $ equationsToSubstitution equations
                        else unify equations'
  where equationsToSubstitution = S.map converter
          where converter (TEVar v, t) = (v,t)
                converter _ = error "unification algorithm didn't yield a substitution"
unify equations = do equations' <- step equations
                     if equations == equations'
                        then return $ equationsToSubstitution equations
                        else unify equations'
  where equationsToSubstitution = S.map converter
          where converter (TEVar v, t) = (v,t)
                converter _ = error "unification algorithm didn't yield a substitution"