Class PatternCoverageChecker

java.lang.Object
net.hydromatic.morel.compile.PatternCoverageChecker

class PatternCoverageChecker extends Object
Checks whether patterns are exhaustive and/or redundant.

The algorithm converts a list of patterns into a boolean formula with several variables, then checks whether the formula is satisfiable (that is, whether there is a combination of assignments of boolean values to the variables such that the formula evaluates to true).

  • Field Details

  • Constructor Details

    • PatternCoverageChecker

      private PatternCoverageChecker(TypeSystem typeSystem)
      Creates a PatternCoverageChecker.
  • Method Details

    • isCoveredBy

      static boolean isCoveredBy(TypeSystem typeSystem, List<Core.Pat> prevPatList, Core.Pat pat)
      Returns whether every possible value that could be matched by pattern pat would already have been matched by one or more of prevPatList.

      For example, the pattern "(1, b: bool)" is covered by "[(1, true), (_, false)]" but not by "[(1, true)]" or "[(_, false)]".

    • isExhaustive

      static boolean isExhaustive(TypeSystem typeSystem, List<Core.Pat> patList)
      Returns whether a list of patterns covers every possible value. If so, any pattern added to this list would be redundant.
    • toTerm

      private Sat.Term toTerm(Core.Pat pat)
      Converts a pattern to a logical term.
    • toTerm

      private void toTerm(Core.Pat pat, PatternCoverageChecker.Path path, List<Sat.Term> terms)
    • listToCons

      private Core.Pat listToCons(Core.ListPat listPat)
      Converts a list pattern into a pattern made up of the CONS and NIL constructors of the built-in datatype list.

      For example, converts: "[]" to "NIL", "[x]" to "CONS (x, NIL)", "[x, y]" to "CONS (x, CONS (y, NIL))", etc.

    • listToConsRecurse

      private Core.Pat listToConsRecurse(DataType listDataType, List<Core.Pat> args)
    • addConsTerms

      private void addConsTerms(PatternCoverageChecker.Path path, List<Sat.Term> terms, Core.TuplePat tuplePat)
    • typeConstructorTerm

      private Sat.Variable typeConstructorTerm(PatternCoverageChecker.Path path, String con)
    • isCoveredBy

      public boolean isCoveredBy(Core.Pat pat, List<Core.Pat> patList)
      Returns whether a pattern is covered by a list of patterns.

      A pattern pat is said to be covered by a list of patterns patList if any possible value would be caught by one of the patterns in patList before reaching pat. Thus pat is said to be redundant in that context, and could be removed without affecting behavior.