1 | module Data.ByteString.Search.DFA.Internal
3 | import Data.Array.Core
5 | import Data.ByteString
6 | import Data.ByteString.Search.DFA.Types
7 | import Data.ByteString.Search.KnuthMorrisPratt.Internal
10 | import Data.Linear.Ref1
12 | %hide Data.Buffer.Core.get
13 | %hide Data.Buffer.Core.set
14 | %hide Data.List.Elem.get
32 | automaton : (bs : ByteString)
33 | -> F1 s (Maybe (DFAutomaton s))
35 | let bord # t := kmpBorders bs t
36 | Just (MkKMPBorders stspace bord') := bord
39 | arr # t := newDFATable {states = stspace.states} {statesPrf = stspace.statesBounded} t
40 | result # t := go stspace Z arr bord' t
41 | Just result' := result
44 | in Just (MkDFAutomaton stspace result') # t
46 | fillState : (space : DFAStateSpace)
47 | -> (state : DFAState space.states)
49 | -> (patbyte : Maybe Bits8)
50 | -> (bordcur : DFAState space.states)
51 | -> (arr : DFATable s space.states)
52 | -> F1 s (Maybe (DFATable s space.states))
53 | fillState space state byte patbyte bordcur arr t =
54 | let stateval := dfaStateValue state
55 | Just patbyte' := patbyte
57 | let False := stateval == 0
59 | let zero := zeroDFAState space
60 | () # t := setDFATransition {statesPrf = space.statesBounded} arr state byte zero t
63 | assert_total (fillState space state (byte - 1) patbyte bordcur arr t)
65 | bordcur' # t := dfaTransition {statesPrf = space.statesBounded} arr bordcur byte t
66 | () # t := setDFATransition {statesPrf = space.statesBounded} arr state byte bordcur' t
69 | assert_total (fillState space state (byte - 1) patbyte bordcur' arr t)
71 | False := byte == patbyte'
73 | let Just next := toDFAState space (S $
cast stateval)
76 | () # t := setDFATransition {statesPrf = space.statesBounded} arr state byte next t
79 | assert_total (fillState space state (byte - 1) patbyte bordcur arr t)
81 | False := stateval == 0
83 | let zero := zeroDFAState space
84 | () # t := setDFATransition {statesPrf = space.statesBounded} arr state byte zero t
87 | assert_total (fillState space state (byte - 1) patbyte bordcur arr t)
89 | bordcur' # t := dfaTransition {statesPrf = space.statesBounded} arr bordcur byte t
90 | () # t := setDFATransition {statesPrf = space.statesBounded} arr state byte bordcur' t
93 | assert_total (fillState space state (byte - 1) patbyte bordcur' arr t)
101 | go : (stspace : DFAStateSpace)
103 | -> (arr : DFATable s stspace.states)
104 | -> (bord : KMPBorderTable s stspace.states)
105 | -> F1 s (Maybe (DFATable s stspace.states))
106 | go stspace state arr bord t =
107 | let False := state > length bs
110 | Just state' := toDFAState stspace state
113 | bordcur # t := kmpBorder bord state' t
114 | patbyte := index state bs
115 | arr' # t := fillState stspace state' 255 patbyte bordcur arr t
119 | in assert_total (go stspace (S state) arr'' bord t)