Algebraic Theory of Patterns as Generalized Symmetries