Std.Regex.Core (Cure Standard Library v0.37.2)

View Source

Shared indexed Regex vocabulary and definitionally relevant shape operations.

Examples

use Std.Regex.Core

Simplify(PairC(UnitC, CharC))           # => CharC
Simplify(ListC(UnitC))                  # => NatC
simplify_value(OptionC(UnitC), Some(())) # => true

Summary

Types

Ordinary boundaries remain the small ten-field context used by the existing Thompson proof. Lookaround evaluators carry their suffix and bounded history separately, so adding assertions does not perturb the ordinary proof indices.

Cure does not otherwise expose a general-purpose Either in the current prelude. Keep the branch witness local to the typed regex result universe; alternatives preserve which branch accepted.

A suffix-indexed intrinsic grammar. Encodes(shape, input, rest) proves that one value of shape consumes a prefix of input and leaves rest. This is the context-threaded formulation from the thesis: concatenation and repetition compose by sharing a middle suffix, with no append theorem or runtime boundary witness.

The runtime remainder returned by one extraction is also its result index. Recursive callers therefore receive the exact boundary required by the next erased Encodes witness without recovering runtime data from that witness.

Evidence is a postfix program result stored in reverse order, so emitting is constant-time. Pair/branch/list markers precede their operands in the final list, which lets the total extractor consume it in one pass.

State identifiers are naturals below state_count. Thompson construction allocates disjoint contiguous ranges and preserves that invariant by shifting the right machine by the left machine's count. Accepted is an accepting edge (the paper's Nothing), not an allocated state. The runtime transition relation is ordered and epsilon-free. Atomic markers are semantic no-ops in the evidence VM. The ordered search consumes them as lexical commit boundaries; keeping them in the same routine stream makes replay and proof paths share one trace.

Minimal regular-language algebra. Every constructor fixes its result shape definitionally, so Thompson construction and evidence extraction cannot disagree later about what a successful path produces.

A transition row fixes the source state once, outside the input loop. The spine is indexed by both its remaining rows and the machine's total state count, so lookup by Bounded(n) has no malformed-table fallback.

Functions

Count the number of elements in a List(Unit) as a Nat.

Wrap a decoded left-branch value into Sem(ChoiceC(left, right)).

Wrap a decoded right-branch value into Sem(ChoiceC(left, right)).

Reverse a List(Char) in the evidence VM without depending on external list helpers.

Tail-recursive accumulator helper for evidence_reverse_chars.

Extract a complete Sem(shape) value from a fully consumed Evidence stream proved by Encodes(shape, input, Nil()).

Total proof-directed extraction (thesis §5.3 / Fig. 5.8). input is an explicit runtime argument, exactly like the paper's ev; only encoding is erased. The evidence marker is inspected first and its index leaves one possible proof constructor. There is no malformed-evidence branch and no Option after an accepting run has produced the certificate.

Extract a reversed list of repeated Sem(shape) items from input using the erased EncodesMany proof witness, returning the remaining evidence.

Project a MachineState(n) to its underlying ThreadState(n), discarding attached evidence instructions and boundary constraints.

Push a matched char onto every open CaptureFrame in frames.

One canonical resource bound shared by syntax admission, runtime child evaluation, and proof-facing assertion entry points. Keeping this in the core module prevents compile-time and runtime limits from drifting.

Reverse a list of decoded Sem(shape) values into forward match order.

Genuine large elimination: each value-level shape computes to its Cure result type. No runtime tag, Dynamic value, cast, or postulate is involved.

Recursively normalize a ShapeCode by eliminating redundant UnitC structure across pairs, choices, options, and lists.

Simplify a ChoiceC(left, right) shape, collapsing two UnitC branches into BoolC.

Simplify a choice shape whose left branch is UnitC, collapsing ChoiceC(UnitC, UnitC) to BoolC.

Simplify a ListC(inner) shape, collapsing ListC(UnitC) to NatC.

Simplify an OptionC(inner) shape, collapsing OptionC(UnitC) to BoolC.

Simplify a PairC(left, right) shape by eliding UnitC on either side.

Simplify a pair shape whose left component is non-unit, dropping a trailing UnitC on the right.

Normalize structural extraction shapes into ergonomic public results. Every companion value function is total at the corresponding computed type; no equality cast connects the raw and simplified interpretations.

Reduction witness checking that Sem(CharC) reduces definitionally to Char.

Reduction witness checking that nested Sem(PairC(CharC, ListC(UnitC))) reduces definitionally to Tuple(Char, List(Unit)).

Phase-1 reduction witnesses. These bodies only check if Sem computes at constructor applications rather than behaving as an opaque type function.

Companion value transformer for SimplifyChoiceUnit, mapping Choice(Unit, Unit) to Bool (true for left, false for right).

Apply simplify_value elementwise across a list of Sem(inner) values.

Companion value transformer for SimplifyList, collapsing List(Unit) to its length as a Nat.

Companion value transformer for SimplifyOption, mapping Option(Unit) to Bool.

Companion value transformer for SimplifyPairPresent, dropping a trailing Unit or pairing left_value with right_value.

Companion value transformer for SimplifyPairUnit, returning right_value directly after dropping a leading Unit.

Companion value transformer for SimplifyPair, combining simplified left and right pair components.

Recursively simplify a value of type Sem(shape) into Sem(Simplify(shape)).

Types

enum Boundary

 enum Boundary
   Boundary

Ordinary boundaries remain the small ten-field context used by the existing Thompson proof. Lookaround evaluators carry their suffix and bounded history separately, so adding assertions does not perturb the ordinary proof indices.

enum BoundaryConstraint

 enum BoundaryConstraint
   BoundaryConstraint

enum CaptureCondition

 enum CaptureCondition
   CaptureParticipated
   CaptureAbsent

enum CaptureFrame

 enum CaptureFrame
   CaptureFrame

enum Choice

 enum Choice
   ChoseLeft
   ChoseRight

Cure does not otherwise expose a general-purpose Either in the current prelude. Keep the branch witness local to the typed regex result universe; alternatives preserve which branch accepted.

type Encodes indices (shape: ShapeCode, input: List(Evidence), rest: List(Evidence))

 type Encodes indices (shape: ShapeCode, input: List(Evidence), rest: List(Evidence))
   EncodesUnit : Encodes(UnitC, Cons(UnitEvidence(), rest), rest)
   EncodesCharacter : (char: Char) -> Encodes(CharC, Cons(CharacterEvidence(char), rest), rest)
   EncodesString : (value: String) -> Encodes(StringC, Cons(StringEvidence(value), rest), rest)
   EncodesPair : (@erased right : Encodes(right_shape, pair_evidence, left_evidence)) -> (@erased left : Encodes(left_shape, left_evidence, rest)) -> Encodes(PairC(left_shape, right_shape), Cons(PairEvidence(), pair_evidence), rest)
   EncodesLeft : (@erased branch : Encodes(left, branch_evidence, rest)) -> Encodes(ChoiceC(left, right), Cons(LeftEvidence(), branch_evidence), rest)
   EncodesRight : (@erased branch : Encodes(right, branch_evidence, rest)) -> Encodes(ChoiceC(left, right), Cons(RightEvidence(), branch_evidence), rest)
   EncodesList : (@erased items : EncodesMany(inner, items_evidence, rest)) -> Encodes(ListC(inner), Cons(EndListEvidence(), items_evidence), rest)

A suffix-indexed intrinsic grammar. Encodes(shape, input, rest) proves that one value of shape consumes a prefix of input and leaves rest. This is the context-threaded formulation from the thesis: concatenation and repetition compose by sharing a middle suffix, with no append theorem or runtime boundary witness.

type EncodesMany indices (shape: ShapeCode, input: List(Evidence), rest: List(Evidence))

 type EncodesMany indices (shape: ShapeCode, input: List(Evidence), rest: List(Evidence))
   EncodesManyEmpty : EncodesMany(shape, Cons(BeginListEvidence(), rest), rest)
   EncodesManyItem : (@erased item : Encodes(shape, item_evidence, prior_evidence)) -> (@erased prior : EncodesMany(shape, prior_evidence, rest)) -> EncodesMany(shape, item_evidence, rest)

type EncodingExtraction indices (shape: ShapeCode, input: List(Evidence), rest: List(Evidence))

 type EncodingExtraction indices (shape: ShapeCode, input: List(Evidence), rest: List(Evidence))
   EncodingExtracted : (remaining: List(Evidence)) -> (value: Sem(shape)) -> EncodingExtraction(shape, input, remaining)

The runtime remainder returned by one extraction is also its result index. Recursive callers therefore receive the exact boundary required by the next erased Encodes witness without recovering runtime data from that witness.

enum Evidence

 enum Evidence
   CharacterEvidence
   UnitEvidence
   PairEvidence
   LeftEvidence
   RightEvidence
   BeginListEvidence
   EndListEvidence
   StringEvidence

Evidence is a postfix program result stored in reverse order, so emitting is constant-time. Pair/branch/list markers precede their operands in the final list, which lets the total extractor consume it in one pass.

enum EvidenceExecution

 enum EvidenceExecution
   EvidenceExecution

enum EvidenceInstruction

 enum EvidenceInstruction
   EmitChar
   EmitUnit
   EmitPair
   EmitLeft
   EmitRight
   BeginList
   EndList
   BeginCapture
   EndCapture
   BeginCaptureSlot
   EndCaptureSlot
   BeginAtomic
   EndAtomic

State identifiers are naturals below state_count. Thompson construction allocates disjoint contiguous ranges and preserves that invariant by shifting the right machine by the left machine's count. Accepted is an accepting edge (the paper's Nothing), not an allocated state. The runtime transition relation is ordered and epsilon-free. Atomic markers are semantic no-ops in the evidence VM. The ordered search consumes them as lexical commit boundaries; keeping them in the same routine stream makes replay and proof paths share one trace.

enum EvidencePrefix

 enum EvidencePrefix
   EvidencePrefix

type EvidenceThread indices (n: Nat)

 type EvidenceThread indices (n: Nat)
   EvidenceThread : ThreadState(n) -> List(Evidence) -> List(CaptureFrame) -> EvidenceThread(n)

enum InitialPosition

 enum InitialPosition
   InitialPosition

enum LookaroundCondition

 enum LookaroundCondition
   LookaheadCondition
   LookbehindCondition

type LookaroundMachine

 type LookaroundMachine

type MachineState indices (n: Nat)

 type MachineState indices (n: Nat)
   Active : Bounded(n) -> List(EvidenceInstruction) -> List(BoundaryConstraint) -> MachineState(n)
   Accepted : List(EvidenceInstruction) -> List(BoundaryConstraint) -> MachineState(n)

type ManyEncodingExtraction indices (shape: ShapeCode, input: List(Evidence), rest: List(Evidence))

 type ManyEncodingExtraction indices (shape: ShapeCode, input: List(Evidence), rest: List(Evidence))
   ManyEncodingExtracted : (remaining: List(Evidence)) -> (values: List(Sem(shape))) -> ManyEncodingExtraction(shape, input, remaining)

enum NewlinePolicy

 enum NewlinePolicy
   LFNewline
   CRNewline
   CRLFNewline
   AnyCRLFNewline
   AnyUnicodeNewline

type Pattern indices (shape: ShapeCode)

 type Pattern indices (shape: ShapeCode)
   PatternPredicate : Function(Char, Bool) -> Pattern(CharC)
   PatternEmpty : Pattern(UnitC)
   PatternConcat : Pattern(left) -> Pattern(right) -> Pattern(PairC(left, right))
   PatternGroup : Pattern(inner) -> Pattern(StringC)
   PatternAlternate : Pattern(left) -> Pattern(right) -> Pattern(ChoiceC(left, right))
   PatternAlternateMode : Pattern(left) -> Pattern(right) -> Bool -> Pattern(ChoiceC(left, right))
   PatternRepeat : Pattern(inner) -> Pattern(ListC(inner))
   PatternRepeatMode : Pattern(inner) -> Bool -> Pattern(ListC(inner))
   PatternBoundary : BoundaryConstraint -> Pattern(UnitC)

Minimal regular-language algebra. Every constructor fixes its result shape definitionally, so Thompson construction and evidence extraction cannot disagree later about what a successful path produces.

type PatternMachine indices (n: Nat)

 type PatternMachine indices (n: Nat)
   MkPatternMachine : List(MachineState(n)) -> Function(Bounded(n), Char, List(MachineState(n))) -> PatternMachine(n)

enum ShapeCode

 enum ShapeCode
   UnitC
   CharC
   StringC
   BoolC
   NatC
   IntC
   PairC
   ChoiceC
   OptionC
   ListC

type ThreadState indices (n: Nat)

 type ThreadState indices (n: Nat)
   ThreadActive : Bounded(n) -> ThreadState(n)
   ThreadAccepted : ThreadState(n)

type TransitionRows indices (n: Nat)

 type TransitionRows indices (n: Nat)
   TransitionRows : TransitionRowsFrom(n, n) -> TransitionRows(n)

type TransitionRowsFrom indices (remaining: Nat, total: Nat)

 type TransitionRowsFrom indices (remaining: Nat, total: Nat)
   NoTransitionRows : TransitionRowsFrom(Z(), total)
   MoreTransitionRows : Function(Char, List(MachineState(total))) -> TransitionRowsFrom(remaining, total) -> TransitionRowsFrom(S(remaining), total)

A transition row fixes the source state once, outside the input loop. The spine is indexed by both its remaining rows and the machine's total state count, so lookup by Bounded(n) has no malformed-table fallback.

Functions

count_units(values: List(Unit)) -> Nat

 fn count_units(values: List(Unit)) -> Nat

Count the number of elements in a List(Unit) as a Nat.

decoded_left_value(left: ShapeCode, right: ShapeCode, value: Sem(left)) -> Sem(ChoiceC(left, right))

 fn decoded_left_value(left: ShapeCode, right: ShapeCode, value: Sem(left)) -> Sem(ChoiceC(left, right))

Wrap a decoded left-branch value into Sem(ChoiceC(left, right)).

decoded_right_value(left: ShapeCode, right: ShapeCode, value: Sem(right)) -> Sem(ChoiceC(left, right))

 fn decoded_right_value(left: ShapeCode, right: ShapeCode, value: Sem(right)) -> Sem(ChoiceC(left, right))

Wrap a decoded right-branch value into Sem(ChoiceC(left, right)).

evidence_reverse_chars(input: List(Char)) -> List(Char)

 fn evidence_reverse_chars(input: List(Char)) -> List(Char)

Reverse a List(Char) in the evidence VM without depending on external list helpers.

evidence_reverse_chars_into(input: List(Char), output: List(Char)) -> List(Char)

 fn evidence_reverse_chars_into(input: List(Char), output: List(Char)) -> List(Char)

Tail-recursive accumulator helper for evidence_reverse_chars.

extract_encoding(shape: ShapeCode, input: List(Evidence), encoding: Encodes(shape, input, Nil())) -> Sem(shape)

 fn extract_encoding(shape: ShapeCode, input: List(Evidence), encoding: Encodes(shape, input, Nil())) -> Sem(shape)

Extract a complete Sem(shape) value from a fully consumed Evidence stream proved by Encodes(shape, input, Nil()).

extract_encoding_result(shape: ShapeCode, input: List(Evidence), rest: List(Evidence), encoding: Encodes(shape, input, rest)) -> EncodingExtraction(shape, input, rest)

 fn extract_encoding_result(shape: ShapeCode, input: List(Evidence), rest: List(Evidence), encoding: Encodes(shape, input, rest)) -> EncodingExtraction(shape, input, rest)

Total proof-directed extraction (thesis §5.3 / Fig. 5.8). input is an explicit runtime argument, exactly like the paper's ev; only encoding is erased. The evidence marker is inspected first and its index leaves one possible proof constructor. There is no malformed-evidence branch and no Option after an accepting run has produced the certificate.

extract_many_encoding_result(shape: ShapeCode, input: List(Evidence), rest: List(Evidence), encoding: EncodesMany(shape, input, rest)) -> ManyEncodingExtraction(shape, input, rest)

 fn extract_many_encoding_result(shape: ShapeCode, input: List(Evidence), rest: List(Evidence), encoding: EncodesMany(shape, input, rest)) -> ManyEncodingExtraction(shape, input, rest)

Extract a reversed list of repeated Sem(shape) items from input using the erased EncodesMany proof witness, returning the remaining evidence.

machine_state_thread(n: Nat, state: MachineState(n)) -> ThreadState(n)

 fn machine_state_thread(n: Nat, state: MachineState(n)) -> ThreadState(n)

Project a MachineState(n) to its underlying ThreadState(n), discarding attached evidence instructions and boundary constraints.

record_capture_char(frames: List(CaptureFrame), char: Char) -> List(CaptureFrame)

 fn record_capture_char(frames: List(CaptureFrame), char: Char) -> List(CaptureFrame)

Push a matched char onto every open CaptureFrame in frames.

regex_assertion_depth_limit() -> Nat

 fn regex_assertion_depth_limit() -> Nat

One canonical resource bound shared by syntax admission, runtime child evaluation, and proof-facing assertion entry points. Keeping this in the core module prevents compile-time and runtime limits from drifting.

reverse_sem_values(shape: ShapeCode, values: List(Sem(shape))) -> List(Sem(shape))

 fn reverse_sem_values(shape: ShapeCode, values: List(Sem(shape))) -> List(Sem(shape))

Reverse a list of decoded Sem(shape) values into forward match order.

reverse_sem_values_into(shape: ShapeCode, values: List(Sem(shape)), output: List(Sem(shape))) -> List(Sem(shape))

 fn reverse_sem_values_into(shape: ShapeCode, values: List(Sem(shape)), output: List(Sem(shape))) -> List(Sem(shape))

Tail-recursive accumulator helper for reverse_sem_values.

Sem(shape: ShapeCode) -> Type

 fn Sem(shape: ShapeCode) -> Type

Genuine large elimination: each value-level shape computes to its Cure result type. No runtime tag, Dynamic value, cast, or postulate is involved.

Simplify(shape: ShapeCode) -> ShapeCode

 fn Simplify(shape: ShapeCode) -> ShapeCode

Recursively normalize a ShapeCode by eliminating redundant UnitC structure across pairs, choices, options, and lists.

SimplifyChoice(left: ShapeCode, right: ShapeCode) -> ShapeCode

 fn SimplifyChoice(left: ShapeCode, right: ShapeCode) -> ShapeCode

Simplify a ChoiceC(left, right) shape, collapsing two UnitC branches into BoolC.

SimplifyChoiceUnit(right: ShapeCode) -> ShapeCode

 fn SimplifyChoiceUnit(right: ShapeCode) -> ShapeCode

Simplify a choice shape whose left branch is UnitC, collapsing ChoiceC(UnitC, UnitC) to BoolC.

SimplifyList(inner: ShapeCode) -> ShapeCode

 fn SimplifyList(inner: ShapeCode) -> ShapeCode

Simplify a ListC(inner) shape, collapsing ListC(UnitC) to NatC.

SimplifyOption(inner: ShapeCode) -> ShapeCode

 fn SimplifyOption(inner: ShapeCode) -> ShapeCode

Simplify an OptionC(inner) shape, collapsing OptionC(UnitC) to BoolC.

SimplifyPair(left: ShapeCode, right: ShapeCode) -> ShapeCode

 fn SimplifyPair(left: ShapeCode, right: ShapeCode) -> ShapeCode

Simplify a PairC(left, right) shape by eliding UnitC on either side.

SimplifyPairPresent(left: ShapeCode, right: ShapeCode) -> ShapeCode

 fn SimplifyPairPresent(left: ShapeCode, right: ShapeCode) -> ShapeCode

Simplify a pair shape whose left component is non-unit, dropping a trailing UnitC on the right.

SimplifyPairUnit(right: ShapeCode) -> ShapeCode

 fn SimplifyPairUnit(right: ShapeCode) -> ShapeCode

Normalize structural extraction shapes into ergonomic public results. Every companion value function is total at the corresponding computed type; no equality cast connects the raw and simplified interpretations.

sem_char_witness(value: Char) -> Sem(CharC)

 fn sem_char_witness(value: Char) -> Sem(CharC)

Reduction witness checking that Sem(CharC) reduces definitionally to Char.

sem_nested_witness(value: Char) -> Sem(PairC(CharC, ListC(UnitC)))

 fn sem_nested_witness(value: Char) -> Sem(PairC(CharC, ListC(UnitC)))

Reduction witness checking that nested Sem(PairC(CharC, ListC(UnitC))) reduces definitionally to Tuple(Char, List(Unit)).

sem_unit_witness() -> Sem(UnitC)

 fn sem_unit_witness() -> Sem(UnitC)

Phase-1 reduction witnesses. These bodies only check if Sem computes at constructor applications rather than behaving as an opaque type function.

simplify_choice_unit_value(right: ShapeCode, value: Choice(Unit, Sem(right))) -> Sem(SimplifyChoiceUnit(right))

 fn simplify_choice_unit_value(right: ShapeCode, value: Choice(Unit, Sem(right))) -> Sem(SimplifyChoiceUnit(right))

Companion value transformer for SimplifyChoiceUnit, mapping Choice(Unit, Unit) to Bool (true for left, false for right).

simplify_choice_value(left: ShapeCode, right: ShapeCode, value: Choice(Sem(left), Sem(right))) -> Sem(SimplifyChoice(left, right))

 fn simplify_choice_value(left: ShapeCode, right: ShapeCode, value: Choice(Sem(left), Sem(right))) -> Sem(SimplifyChoice(left, right))

Companion value transformer for SimplifyChoice.

simplify_list(inner: ShapeCode, values: List(Sem(inner))) -> List(Sem(Simplify(inner)))

 fn simplify_list(inner: ShapeCode, values: List(Sem(inner))) -> List(Sem(Simplify(inner)))

Apply simplify_value elementwise across a list of Sem(inner) values.

simplify_list_values(inner: ShapeCode, values: List(Sem(inner))) -> Sem(SimplifyList(inner))

 fn simplify_list_values(inner: ShapeCode, values: List(Sem(inner))) -> Sem(SimplifyList(inner))

Companion value transformer for SimplifyList, collapsing List(Unit) to its length as a Nat.

simplify_option_value(inner: ShapeCode, value: Option(Sem(inner))) -> Sem(SimplifyOption(inner))

 fn simplify_option_value(inner: ShapeCode, value: Option(Sem(inner))) -> Sem(SimplifyOption(inner))

Companion value transformer for SimplifyOption, mapping Option(Unit) to Bool.

simplify_pair_present_value(left: ShapeCode, right: ShapeCode, left_value: Sem(left), right_value: Sem(right)) -> Sem(SimplifyPairPresent(left, right))

 fn simplify_pair_present_value(left: ShapeCode, right: ShapeCode, left_value: Sem(left), right_value: Sem(right)) -> Sem(SimplifyPairPresent(left, right))

Companion value transformer for SimplifyPairPresent, dropping a trailing Unit or pairing left_value with right_value.

simplify_pair_unit_value(right: ShapeCode, right_value: Sem(right)) -> Sem(SimplifyPairUnit(right))

 fn simplify_pair_unit_value(right: ShapeCode, right_value: Sem(right)) -> Sem(SimplifyPairUnit(right))

Companion value transformer for SimplifyPairUnit, returning right_value directly after dropping a leading Unit.

simplify_pair_value(left: ShapeCode, right: ShapeCode, left_value: Sem(left), right_value: Sem(right)) -> Sem(SimplifyPair(left, right))

 fn simplify_pair_value(left: ShapeCode, right: ShapeCode, left_value: Sem(left), right_value: Sem(right)) -> Sem(SimplifyPair(left, right))

Companion value transformer for SimplifyPair, combining simplified left and right pair components.

simplify_value(shape: ShapeCode, value: Sem(shape)) -> Sem(Simplify(shape))

 fn simplify_value(shape: ShapeCode, value: Sem(shape)) -> Sem(Simplify(shape))

Recursively simplify a value of type Sem(shape) into Sem(Simplify(shape)).