Use heap to allocate PatternMatch to avoid potential stack overflow