Abstract algebra in Scala