pub struct PatternCompiler { /* private fields */ }Expand description
Pattern matching compiler.
Implementations§
Source§impl PatternCompiler
impl PatternCompiler
Sourcepub fn analyze_usefulness(
&self,
rows: &[PatternRow],
new_pattern: &[Pattern],
) -> bool
pub fn analyze_usefulness( &self, rows: &[PatternRow], new_pattern: &[Pattern], ) -> bool
Analyze pattern usefulness using basic algorithm.
Source§impl PatternCompiler
impl PatternCompiler
Sourcepub fn bound_var_set(&self, pattern: &Pattern) -> HashSet<String>
pub fn bound_var_set(&self, pattern: &Pattern) -> HashSet<String>
Get all variables bound by a pattern as a set.
Sourcepub fn same_bindings(&self, patterns: &[Pattern]) -> bool
pub fn same_bindings(&self, patterns: &[Pattern]) -> bool
Check if patterns bind the same variables.
Source§impl PatternCompiler
impl PatternCompiler
Sourcepub fn check_exhaustive(&self, patterns: &[Pattern]) -> Result<(), String>
pub fn check_exhaustive(&self, patterns: &[Pattern]) -> Result<(), String>
Check if a pattern set is exhaustive.
A pattern set is exhaustive when it contains at least one irrefutable pattern (wildcard or variable binding). When only constructor patterns are present, exhaustiveness cannot be verified without the full type definition, so the check is optimistically accepted.
Source§impl PatternCompiler
impl PatternCompiler
Sourcepub fn count_bindings(&self, pattern: &Pattern) -> usize
pub fn count_bindings(&self, pattern: &Pattern) -> usize
Count the number of variable bindings in a pattern.
Source§impl PatternCompiler
impl PatternCompiler
Sourcepub fn flatten_or_pattern(&self, pattern: &Pattern) -> Vec<Pattern>
pub fn flatten_or_pattern(&self, pattern: &Pattern) -> Vec<Pattern>
Flatten a nested or-pattern into a flat list of alternatives.
Source§impl PatternCompiler
impl PatternCompiler
Sourcepub fn max_pattern_depth(&self, pattern: &Pattern) -> usize
pub fn max_pattern_depth(&self, pattern: &Pattern) -> usize
Check if pattern depth is reasonable (prevent stack overflow).
Source§impl PatternCompiler
impl PatternCompiler
Sourcepub fn pattern_to_string(&self, pattern: &Pattern) -> String
pub fn pattern_to_string(&self, pattern: &Pattern) -> String
Pretty-print a pattern to a string.
Sourcepub fn canonicalize(&self, pattern: &Pattern) -> String
pub fn canonicalize(&self, pattern: &Pattern) -> String
Convert patterns to a canonical form for comparison.
Source§impl PatternCompiler
impl PatternCompiler
Sourcepub fn patterns_equivalent(&self, p1: &Pattern, p2: &Pattern) -> bool
pub fn patterns_equivalent(&self, p1: &Pattern, p2: &Pattern) -> bool
Check if two patterns are structurally equivalent.
Source§impl PatternCompiler
impl PatternCompiler
Sourcepub fn is_irrefutable(&self, pattern: &Pattern) -> bool
pub fn is_irrefutable(&self, pattern: &Pattern) -> bool
Check if a pattern is irrefutable (always matches).
Wildcards and variable bindings are irrefutable. Constructor and literal patterns are not. An or-pattern is irrefutable if either branch is irrefutable.
Source§impl PatternCompiler
impl PatternCompiler
Sourcepub fn compile_match(
&mut self,
scrutinee: &SurfaceExpr,
clauses: &[MatchClause],
) -> Result<SurfaceExpr, String>
pub fn compile_match( &mut self, scrutinee: &SurfaceExpr, clauses: &[MatchClause], ) -> Result<SurfaceExpr, String>
Compile a pattern match to a case tree.
Builds a SurfaceExpr::Match over the given scrutinee with one
arm per clause. All clauses are included in textual order; the
elaborator handles the actual case-tree construction.
Sourcepub fn check_redundant(&self, patterns: &[Pattern]) -> Vec<usize>
pub fn check_redundant(&self, patterns: &[Pattern]) -> Vec<usize>
Return the indices of redundant (unreachable) patterns.
A pattern at index i is redundant when some earlier pattern in the
list is irrefutable (wildcard or variable), meaning it would always
match before the pattern at i is reached.
Sourcepub fn compile_matrix(
&mut self,
rows: &[PatternRow],
num_cols: usize,
) -> CaseTree
pub fn compile_matrix( &mut self, rows: &[PatternRow], num_cols: usize, ) -> CaseTree
Compile a pattern matrix into a case tree.
This implements the standard pattern matrix compilation algorithm. Each row contains a vector of patterns (one per column) and a body. The result is a decision tree that tests columns in an efficient order.
Sourcepub fn specialize(
&self,
rows: &[PatternRow],
col: usize,
ctor: &str,
arity: usize,
) -> Vec<PatternRow>
pub fn specialize( &self, rows: &[PatternRow], col: usize, ctor: &str, arity: usize, ) -> Vec<PatternRow>
Specialize the pattern matrix for a particular constructor.
For each row where column col matches constructor ctor:
- If the pattern is
Ctor(ctor, args), replace columncolwithargs - If the pattern is a wildcard/variable, expand it to
aritywildcards
Rows that have a different constructor in column col are removed.
Sourcepub fn default_rows(&self, rows: &[PatternRow], col: usize) -> Vec<PatternRow>
pub fn default_rows(&self, rows: &[PatternRow], col: usize) -> Vec<PatternRow>
Compute the default matrix.
Rows where column col is a wildcard or variable are kept (with the
column removed). Rows with a specific constructor are dropped.
Sourcepub fn collect_constructors(
&self,
rows: &[PatternRow],
col: usize,
) -> Vec<(String, usize)>
pub fn collect_constructors( &self, rows: &[PatternRow], col: usize, ) -> Vec<(String, usize)>
Collect constructors appearing in a particular column.
Returns a deduplicated list of (constructor_name, arity) pairs.
Sourcepub fn check_exhaustive_with_ctors(
&self,
patterns: &[Pattern],
ctors: &TypeConstructors,
) -> Result<(), Vec<String>>
pub fn check_exhaustive_with_ctors( &self, patterns: &[Pattern], ctors: &TypeConstructors, ) -> Result<(), Vec<String>>
Check exhaustiveness given known type constructors.
Returns Ok(()) if the patterns are exhaustive, or Err(missing) with
a list of constructor names that are not covered.
Sourcepub fn check_nested_exhaustive(
&self,
patterns: &[Vec<Pattern>],
ctors: &[TypeConstructors],
) -> Result<(), Vec<Vec<String>>>
pub fn check_nested_exhaustive( &self, patterns: &[Vec<Pattern>], ctors: &[TypeConstructors], ) -> Result<(), Vec<Vec<String>>>
Check exhaustiveness for multi-column pattern matching.
Each inner Vec<Pattern> represents a row of patterns (one per scrutinee).
ctors provides constructor info for each column’s type.
Returns Ok(()) if exhaustive, or Err with missing pattern combinations.
Sourcepub fn simplify_pattern(&self, pattern: &Pattern) -> Pattern
pub fn simplify_pattern(&self, pattern: &Pattern) -> Pattern
Simplify a pattern by flattening nested or-patterns.
Transforms (a | b) | c into a flat structure and removes
redundant wildcards in or-patterns.
Sourcepub fn extract_literal_range(&self, patterns: &[Pattern]) -> Option<(i64, i64)>
pub fn extract_literal_range(&self, patterns: &[Pattern]) -> Option<(i64, i64)>
Extract literal values from a pattern for range analysis.
Sourcepub fn check_range_coverage(
&self,
patterns: &[Pattern],
min: i64,
max: i64,
) -> bool
pub fn check_range_coverage( &self, patterns: &[Pattern], min: i64, max: i64, ) -> bool
Check if patterns cover all values in a given range.
Sourcepub fn find_dead_patterns(&self, rows: &[PatternRow]) -> Vec<usize>
pub fn find_dead_patterns(&self, rows: &[PatternRow]) -> Vec<usize>
Analyze pattern matrix for dead code.
Sourcepub fn extract_bound_names(&self, pattern: &Pattern) -> Vec<String>
pub fn extract_bound_names(&self, pattern: &Pattern) -> Vec<String>
Extract all bound variable names from a pattern.
Source§impl PatternCompiler
impl PatternCompiler
Sourcepub fn select_column(&self, rows: &[PatternRow], num_cols: usize) -> usize
pub fn select_column(&self, rows: &[PatternRow], num_cols: usize) -> usize
Select the best column to split on.
Uses a simple heuristic: pick the column with the most constructor patterns. This tends to produce smaller decision trees.