On Static Analysis for Expressive Pattern Matching

Pattern matching is a widespread programming language construct that enables definitions of values by cases, generalizing if-then-else and case statements. The cases in a pattern matching expression should be exhaustive: when the value does not match any of the cases, the expression throws a run-time exception. Similarly, each pattern should be reachable, and, if possible, patterns should be disjoint to facilitate reasoning. Current compilers use simple analyses to check patterns. Such analyses ignore pattern guards, use static types to approximate possible expression values, and do not take into account properties of user-defined functions. We present a design and implementation of a new analysis of pattern matching expressions. Our analysis detects a wider class of errors and reports fewer false alarms than previous approaches. It checks disjointness, reachability, and exhaustiveness of patterns by expressing these conditions as formulas and proving them using decision procedures and theorem provers. It achieves precision by propagating possible values through nested expressions and approximating pattern-matching guards with formulas. It supports user-defined ``extractor'' functions in patterns by relying on specifications of relationships between the domains of such functions. The result is the first analysis that enables verified, declarative pattern matching with guards in the presence of data abstraction. We have implemented our analysis and describe our experience in checking a range of pattern matching expressions in a subset of the Scala programming language.

[1]  Stephen J. Garland,et al.  Larch: Languages and Tools for Formal Specification , 1993, Texts and Monographs in Computer Science.

[2]  Frank Pfenning,et al.  Refinement types for ML , 1991, PLDI '91.

[3]  Rod M. Burstall,et al.  HOPE: An experimental applicative language , 1980, LISP Conference.

[4]  Martin Odersky,et al.  Translation Correctness for First-Order Object-Oriented Pattern Matching , 2007, APLAS.

[5]  Don Syme,et al.  Extensible pattern matching via a lightweight language extension , 2007, ICFP '07.

[6]  Robert Pickering,et al.  Foundations of F , 2007 .

[7]  Christophe Ringeissen,et al.  A Pattern Matching Compiler for Multiple Target Languages , 2003, CC.

[8]  David Detlefs,et al.  Simplify: a theorem prover for program checking , 2005, JACM.

[9]  Chris Okasaki,et al.  Functional Data Structures , 1996, Handbook of Data Structures and Applications.

[10]  Greg Nelson,et al.  Extended static checking for Java , 2002, PLDI '02.

[11]  Cesare Tinelli,et al.  Solving quantified verification conditions using satisfiability modulo theories , 2007, Annals of Mathematics and Artificial Intelligence.

[12]  Lawrence Charles Paulson,et al.  Isabelle/HOL: A Proof Assistant for Higher-Order Logic , 2002 .

[13]  Simon J. Thompson,et al.  A logic for Miranda, revisited , 1995, Formal Aspects of Computing.

[14]  Martin Odersky,et al.  Matching Objects with Patterns , 2007, ECOOP.

[15]  K. Rustan M. Leino,et al.  A Verification Methodology for Model Fields , 2006, ESOP.

[16]  Joshua Dunfield,et al.  A unified system of type refinements , 2007 .

[17]  Martin Odersky,et al.  Programming in Scala: A Comprehensive Step-by-Step Guide, 2nd Edition , 2010 .

[18]  Philip Wadler,et al.  Views: a way for pattern matching to cohabit with data abstraction , 1987, POPL '87.

[19]  Todd D. Millstein,et al.  Practical predicate dispatch , 2004, OOPSLA.

[20]  Viktor Kuncak,et al.  Modular data structure verification , 2007 .

[21]  Peyton Jones,et al.  Haskell 98 language and libraries : the revised report , 2003 .

[22]  Craig S. Kaplan,et al.  Predicate Dispatching: A Unified Theory of Dispatch , 1998, ECOOP.

[23]  John Tang Boyland,et al.  Statically checkable pattern abstractions , 1997, ICFP '97.

[24]  Xavier Leroy The objective caml system release 3 , 2001 .

[25]  Yaron Minsky Caml trading , 2008, POPL '08.