Pattern Matching in AI Compilers and its Formalization (Extended Version)