Skip to main content

PatternCompiler

Struct PatternCompiler 

Source
pub struct PatternCompiler { /* private fields */ }
Expand description

Pattern matching compiler.

Implementations§

Source§

impl PatternCompiler

Source

pub fn analyze_usefulness( &self, rows: &[PatternRow], new_pattern: &[Pattern], ) -> bool

Analyze pattern usefulness using basic algorithm.

Source§

impl PatternCompiler

Source

pub fn bound_var_set(&self, pattern: &Pattern) -> HashSet<String>

Get all variables bound by a pattern as a set.

Source

pub fn same_bindings(&self, patterns: &[Pattern]) -> bool

Check if patterns bind the same variables.

Source§

impl PatternCompiler

Source

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

Source

pub fn count_bindings(&self, pattern: &Pattern) -> usize

Count the number of variable bindings in a pattern.

Source§

impl PatternCompiler

Source

pub fn flatten_or_pattern(&self, pattern: &Pattern) -> Vec<Pattern>

Flatten a nested or-pattern into a flat list of alternatives.

Source§

impl PatternCompiler

Source

pub fn fresh_var(&mut self) -> Name

Generate a fresh variable name.

Source§

impl PatternCompiler

Source

pub fn max_pattern_depth(&self, pattern: &Pattern) -> usize

Check if pattern depth is reasonable (prevent stack overflow).

Source§

impl PatternCompiler

Source

pub fn pattern_to_string(&self, pattern: &Pattern) -> String

Pretty-print a pattern to a string.

Source

pub fn canonicalize(&self, pattern: &Pattern) -> String

Convert patterns to a canonical form for comparison.

Source§

impl PatternCompiler

Source

pub fn patterns_equivalent(&self, p1: &Pattern, p2: &Pattern) -> bool

Check if two patterns are structurally equivalent.

Source§

impl PatternCompiler

Source

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

Source

pub fn new() -> Self

Create a new pattern compiler.

Source

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.

Source

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.

Source

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.

Source

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 column col with args
  • If the pattern is a wildcard/variable, expand it to arity wildcards

Rows that have a different constructor in column col are removed.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn extract_literal_range(&self, patterns: &[Pattern]) -> Option<(i64, i64)>

Extract literal values from a pattern for range analysis.

Source

pub fn check_range_coverage( &self, patterns: &[Pattern], min: i64, max: i64, ) -> bool

Check if patterns cover all values in a given range.

Source

pub fn find_dead_patterns(&self, rows: &[PatternRow]) -> Vec<usize>

Analyze pattern matrix for dead code.

Source

pub fn extract_bound_names(&self, pattern: &Pattern) -> Vec<String>

Extract all bound variable names from a pattern.

Source§

impl PatternCompiler

Source

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.

Trait Implementations§

Source§

impl Default for PatternCompiler

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.