Compiling Pattern Matches into Decision Trees

Share tests for constructors while preserving the first matching clause and the order of effectful guards.

Source
Anbloq
Language
English
Created
Updated
Chapters
2
Reading time
1 min
In this article2 chapters
  1. A decision can discard several impossible rows together
  2. Preserve the priority of overlapping cases

A decision can discard several impossible rows together

Represent match clauses as rows of patterns and choose a value position with useful constructor tests. On a constructor branch, specialize the remaining rows to that constructor and continue with its fields. For an optional pair, test presence once, then inspect the pair components only on the present branch. Rows with wildcards may remain possible on several branches.

Preserve the priority of overlapping cases

If an early specific clause overlaps a later wildcard, reaching the wildcard must mean all earlier matching opportunities have failed. Guard evaluation can have observable effects, so sharing and reordering must respect the language’s model. A decision DAG can share repeated subtrees and limit code expansion. Exhaustiveness checking uses similar specialization to find uncovered inputs. Algebraic data types determine the legal constructor tests and field projections.[1][2]

References

  1. Andrew C. Myers, Cornell CS 4120: Compiler Course Notes ↩
  2. Shriram Krishnamurthi, Programming Languages: Application and Interpretation ↩