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).
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classPatternCoverageChecker.DataTypeSlotPayload of aSat.Variablethat is an algebraic type.private static classPatternCoverageChecker.ElideList<E>List that removes one particular element from a backing list.private static classPatternCoverageChecker.PathIdentifies a point in a nested pattern.private static classPatternCoverageChecker.SubPathPath that is a child of a given parent path.
-
Field Summary
Fields Modifier and Type Field Description (package private) Map<PatternCoverageChecker.Path,PatternCoverageChecker.DataTypeSlot>pathSlots(package private) Satsat(package private) TypeSystemtypeSystem
-
Constructor Summary
Constructors Modifier Constructor Description privatePatternCoverageChecker(TypeSystem typeSystem)Creates a PatternCoverageChecker.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidaddConsTerms(PatternCoverageChecker.Path path, List<Sat.Term> terms, Core.TuplePat tuplePat)booleanisCoveredBy(Core.Pat pat, List<Core.Pat> patList)Returns whether a pattern is covered by a list of patterns.(package private) static booleanisCoveredBy(TypeSystem typeSystem, List<Core.Pat> prevPatList, Core.Pat pat)Returns whether every possible value that could be matched by patternpatwould already have been matched by one or more ofprevPatList.(package private) static booleanisExhaustive(TypeSystem typeSystem, List<Core.Pat> patList)Returns whether a list of patterns covers every possible value.private Core.PatlistToCons(Core.ListPat listPat)Converts a list pattern into a pattern made up of theCONSandNILconstructors of the built-indatatype list.private Core.PatlistToConsRecurse(DataType listDataType, List<Core.Pat> args)private Sat.TermtoTerm(Core.Pat pat)Converts a pattern to a logical term.private voidtoTerm(Core.Pat pat, PatternCoverageChecker.Path path, List<Sat.Term> terms)private Sat.VariabletypeConstructorTerm(PatternCoverageChecker.Path path, String con)
-
-
-
Field Detail
-
typeSystem
final TypeSystem typeSystem
-
sat
final Sat sat
-
pathSlots
final Map<PatternCoverageChecker.Path,PatternCoverageChecker.DataTypeSlot> pathSlots
-
-
Constructor Detail
-
PatternCoverageChecker
private PatternCoverageChecker(TypeSystem typeSystem)
Creates a PatternCoverageChecker.
-
-
Method Detail
-
isCoveredBy
static boolean isCoveredBy(TypeSystem typeSystem, List<Core.Pat> prevPatList, Core.Pat pat)
Returns whether every possible value that could be matched by patternpatwould already have been matched by one or more ofprevPatList.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 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 theCONSandNILconstructors of the built-indatatype list.For example, converts: "[]" to "NIL", "[x]" to "CONS (x, NIL)", "[x, y]" to "CONS (x, CONS (y, NIL))", etc.
-
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
patis said to be covered by a list of patternspatListif any possible value would be caught by one of the patterns inpatListbefore reachingpat. Thuspatis said to be redundant in that context, and could be removed without affecting behavior.
-
-