1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
module Fin = Strong.Finite
module type DFA = sig
type states
val states : states Fin.set
type transitions
val transitions : transitions Fin.set
type label
val label : transitions Fin.elt -> label
val source : transitions Fin.elt -> states Fin.elt
val target : transitions Fin.elt -> states Fin.elt
end
module type INPUT = sig
include DFA
val initials : (states Fin.elt -> unit) -> unit
val finals : (states Fin.elt -> unit) -> unit
val refinements :
refine:(iter:((states Fin.elt -> unit) -> unit) -> unit) -> unit
end
let index_transitions (type state) (type transition)
(states : state Fin.set)
(transitions : transition Fin.set)
(target : transition Fin.elt -> state Fin.elt)
: state Fin.elt -> (transition Fin.elt -> unit) -> unit
=
let f = Array.make (Fin.Set.cardinal states + 1) 0 in
Fin.Set.iter transitions (fun t ->
let state = (target t :> int) in
f.(state) <- f.(state) + 1
);
for i = 0 to Fin.Set.cardinal states - 1 do
f.(i + 1) <- f.(i + 1) + f.(i)
done;
let a = Array.make (Fin.Set.cardinal transitions)
(Fin.Elt.of_int transitions 0)
in
Fin.Set.rev_iter transitions (fun t ->
let state = (target t :> int) in
let index = f.(state) - 1 in
f.(state) <- index;
a.(index) <- t
);
(fun st fn ->
let st = (st : state Fin.elt :> int) in
for i = f.(st) to f.(st + 1) - 1 do fn a.(i) done
)
let discard_unreachable
(type state) (type transition)
(blocks : state Partition.t)
(transitions_of : state Fin.elt -> (transition Fin.elt -> unit) -> unit)
(target : transition Fin.elt -> state Fin.elt)
=
Partition.iter_marked_elements blocks 0 (fun state ->
transitions_of state
(fun transition -> Partition.mark blocks (target transition))
);
Partition.discard_unmarked blocks
module Minimize
(Label : Map.OrderedType)
(In: INPUT with type label := Label.t) :
sig
include DFA with type label = Label.t
val initials : states Fin.elt array
val finals : states Fin.elt array
val transport_state :
In.states Fin.elt -> states Fin.elt option
val transport_transition :
In.transitions Fin.elt -> transitions Fin.elt option
val represent_state :
states Fin.elt -> In.states Fin.elt
val represent_transition :
transitions Fin.elt -> In.transitions Fin.elt
end = struct
let blocks = Partition.create In.states
let () =
In.initials (Partition.mark blocks);
let transitions_source =
index_transitions In.states In.transitions In.source in
discard_unreachable blocks transitions_source In.target
let transitions_targeting =
index_transitions In.states In.transitions In.target
let () =
In.finals (Partition.mark blocks);
discard_unreachable blocks transitions_targeting In.source
let () =
In.finals (Partition.mark blocks);
Partition.split blocks
let () =
let refine ~iter =
iter (Partition.mark blocks);
Partition.split blocks
in
In.refinements ~refine
let cords =
let partition t1 t2 = Label.compare (In.label t1) (In.label t2) in
Partition.create In.transitions ~partition
let () =
Partition.discard cords (fun t ->
Partition.set_of blocks (In.source t) = -1 ||
Partition.set_of blocks (In.target t) = -1
)
let () =
let block_set = ref 1 in
let cord_set = ref 0 in
while !cord_set < Partition.set_count cords do
Partition.iter_elements cords !cord_set
(fun transition -> Partition.mark blocks (In.source transition));
Partition.split blocks;
while !block_set < Partition.set_count blocks do
Partition.iter_elements blocks !block_set (fun state ->
transitions_targeting state (Partition.mark cords)
);
Partition.split cords;
incr block_set;
done;
incr cord_set;
done
module States =
Strong.Natural.Nth(struct let n = Partition.set_count blocks end)
type states = States.n
let states = States.n
module Transitions = Fin.Array.Of_array(struct
type a = In.transitions Fin.elt
let table =
let count = ref 0 in
Fin.Set.iter In.transitions (fun tr ->
if Partition.is_first blocks (In.source tr) &&
Partition.set_of blocks (In.target tr) > -1
then incr count
);
match !count with
| 0 -> [||]
| n -> Array.make n (Fin.Elt.of_int In.transitions 0)
let () =
let count = ref 0 in
Fin.Set.iter In.transitions (fun tr ->
if Partition.is_first blocks (In.source tr) &&
Partition.set_of blocks (In.target tr) > -1
then (
let index = !count in
incr count;
table.(index) <- tr
)
);
end)
type transitions = Transitions.n
let transitions = Transitions.n
type label = Label.t
let transport_state_unsafe =
let table =
Fin.Array.init In.states (Partition.set_of blocks)
in
Fin.Array.get table
let represent_state =
let table =
Fin.Array.init states
(fun st -> Partition.choose blocks (st : states Fin.elt :> int))
in
Fin.Array.get table
let represent_transition transition =
Fin.(Transitions.table.(transition))
let label transition : Label.t =
In.label (represent_transition transition)
let source transition =
Fin.Elt.of_int states
(transport_state_unsafe (In.source (represent_transition transition)))
let target transition =
Fin.Elt.of_int states
(transport_state_unsafe (In.target (represent_transition transition)))
let initials =
In.initials (Partition.mark blocks);
let sets = Partition.marked_sets blocks in
Partition.clear_marks blocks;
Array.map (Fin.Elt.of_int states) (Array.of_list sets)
let finals =
In.finals (Partition.mark blocks);
let sets = Partition.marked_sets blocks in
Partition.clear_marks blocks;
Array.map (Fin.Elt.of_int states) (Array.of_list sets)
let transport_state state =
match transport_state_unsafe state with
| -1 -> None
| n -> Some (Fin.Elt.of_int states n)
let transport_transition =
let table = Fin.Array.make In.transitions None in
Fin.Array.iteri (fun tr trin ->
assert (Fin.Array.get table trin = None);
Fin.Array.set table trin (Some tr);
) Transitions.table;
Fin.Array.get table
end