Support pattern matching on Bytes literals - #6228
Merged
Merged
Conversation
Fixes #6128. Bytes literals (0xs...) were accepted in expression position but rejected as patterns, alongside Text/Nat/Int/Float/Char/ Boolean. This adds full support across parser, typechecker, pattern- match coverage, runtime, hashing, and serialization, with round-trip pretty-printing. Adds a transcript demonstrating basic dispatch, default fallthrough, and underscore-separated literals (0xs01_ef).
runarorama
force-pushed
the
feature/bytes-literal-patterns
branch
from
May 1, 2026 01:58
a4bd74d to
e50bc7f
Compare
aryairani
approved these changes
May 1, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
This adds support for pattern matching on
Bytesliterals (0xs...), bringingthem in line with
Text,Nat,Int,Float,Char, andBoolean, whichalready work as both expressions and patterns.
Old behavior —
0xsabbawas accepted in expression position but rejected as apattern:
New behavior — bytes literals can be used as patterns, including
underscore-separated forms (
0xs01_ef):Closes #6128.
Implementation approach and notes
The same shape as the existing
Textpattern path, mirrored across every layer:BytestoUnison.Pattern(core),Unison.Syntax.Pattern,U.Codebase.Term.Pattern(V2 storage), andUnison.Hashing.V2.Pattern. Conversions between layers updated in both directions.pBytestopLiteralinTermParser.hs, reusing theexisting
bytesTokenlexer.Pattern.Bytesrequires the scrutinee to be a subtype ofType.bytes.BytestoPmLit,Vc'BytestoVarConstraints, and threaded it throughSolve,NormalizedConstraints, andDesugar. Bytes are treated likeText/Char(infinite-domain literals), socoverage analysis works without changes to the
IntervalSetmachinery.0xs<hex>for round-trip, matchingthe existing expression-level renderer.
bytesRefwas added tobuiltinCaseinRuntime.Patternso dispatch goes throughsplitMatrixBuiltinrather thanlookupData.MatchBytes (Map Bytes e) (Maybe e)toBranched,AccumBytestoBranchAccum,TestY/MatchYto MCode, andselectBytesBranchin the machine. Dispatch is
Map-based (likeMatchText), so it's O(log n) ratherthan a chain of equality tests.
V2/Pattern.hs(14),Synhash.hs(17),MtTag(MBytesT),Sqlite/Serialization(14),MCode/Serialize(TestYT), andMurmurHash.Term.intop(which embeds patterns back into terms for some debugpaths) uses
Bytes.fromList [hex...]to mirror how Bytes literal expressionsdesugar.
Interesting/controversial decisions
V4 ANF codec — reject vs. forward-compatible. The V4 codec is a frozen
format predating this feature. I made it explicitly throw on
MatchBytes(bothserialize and deserialize) rather than silently fall through. Anyone serializing
with V4 needs to know they've lost a feature. The current codec gets a new tag
MBytesT = 7.Test coverage
unison-src/transcripts/idempotent/bytes-pattern-match.mdcovering basicdispatch, default fallthrough, the empty
0xs, and underscore-separated literals(
0xs01_ef). Idempotent — re-running produces zero diff../scripts/proofs/tests.sh— all unit test suites + 22 CLI integration testspass.
./scripts/proofs/transcripts.sh— 385 transcripts pass, no diffs againstcommitted
.output.mdfiles.(e.g. a non-exhaustive match on
Bytesshould warn), but that machinery is sharedwith
Text/Charso the existing tests for those exercise the same code path.