PyPm is a Python-based domain specific language (DSL) for building rewrite-based optimization passes on deep learning computation graphs. Users define individual optimizations by writing (a) \emph{patterns} that match subgraphs of a computation graph and (b) corresponding \emph{rules} which replace a matched subgraph with an optimized kernel. PyPm is distinguished from the many other DSLs for defining rewriting passes by its complex and novel pattern language which borrows concepts from logic programming. PyPm patterns can be recursive, nondeterminstic, and can require checking domain-specific constraints such as the shapes of tensors. The PyPm implementation is thus similarly complicated, consisting of thousands of lines of C++ code. In this paper, we present our work formalizing and distilling and this complexity to an understandable mathematical core. We have developed a formal core calculus expressing the main operations of the PyPm pattern language. We define both a declarative semantics – describing which patterns match which terms – and an algorithmic semantics – an idealized version of the PyPm pattern interpreter – and prove their equivalence. The development is fully mechanized in the Coq proof assistant.