Prefix table for the Knuth-Morris-Pratt matching algorithm
This is an array of the form t = [(x₀,n₀), (x₁,n₁), (x₂, n₂), ...]
where for each i
, nᵢ
is
the length of the longest proper prefix of xs = [x₀,x₁,...,xᵢ]
which is also a suffix of xs
.
- valid : ∀ {i : Nat} (h : i < Array.size self.toArray), self.toArray[i].snd ≤ i
Validity condition to help with termination proofs
Instances For
Equations
- Array.instInhabitedPrefixTable = { default := { toArray := #[], valid := (_ : ∀ {i : Nat} (a : i < Array.size #[]), #[][i].snd ≤ i) } }
Returns the size of the prefix table
Equations
- Array.PrefixTable.size t = Array.size t.toArray
Instances For
Transition function for the KMP matcher
Assuming we have an input xs
with a suffix that matches the pattern prefix t.pattern[:len]
where len : Fin (t.size+1)
. Then xs.push x
has a suffix that matches the pattern prefix
t.pattern[:t.step x len]
. If len
is as large as possible then t.step x len
will also be
as large as possible.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Extend a prefix table by one element
If t
is the prefix table for xs
then t.extend x
is the prefix table for xs.push x
.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Make prefix table from a pattern array
Equations
- Array.mkPrefixTable xs = Array.foldl (fun (x : Array.PrefixTable α) => Array.PrefixTable.extend x) default xs 0
Instances For
Make prefix table from a pattern stream
Equations
- Array.mkPrefixTableOfStream stream = Array.mkPrefixTableOfStream.loop default stream
Instances For
Inner loop for mkPrefixTableOfStream
KMP matcher structure
- table : Array.PrefixTable α
Prefix table for the pattern
- state : Fin (Array.PrefixTable.size self.table + 1)
Current longest matching prefix
Instances For
Make a KMP matcher for a given pattern array
Equations
- Array.Matcher.ofArray pat = { table := Array.mkPrefixTable pat, state := 0 }
Instances For
Make a KMP matcher for a given a pattern stream
Equations
- Array.Matcher.ofStream pat = { table := Array.mkPrefixTableOfStream pat, state := 0 }
Instances For
Find next match from a given stream
Runs the stream until it reads a sequence that matches the sought pattern, then returns the stream state at that point and an updated matcher state.