-
Notifications
You must be signed in to change notification settings - Fork 0
/
kat.ml
302 lines (257 loc) · 9.12 KB
/
kat.ml
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
(*===========================================================================*)
(* PARAMETERS *)
(*===========================================================================*)
(** Primitive tests. May want to functorize over this type. *)
type test = T1 | T2 | T3 | T4 [@@deriving sexp]
(** Actions. The set of actions is often denoted by Σ. *)
type action = A1 | A2 | A3 | A4 [@@deriving sexp]
(** Atoms are truth assignments, mapping tests to true/false. *)
type atom = test -> bool
(*===========================================================================*)
(* AUTOMATA *)
(*===========================================================================*)
(** deterministic KAT automaton - possibly with infinitely many states. *)
type 'state dfa = {
start : 'state;
obs : 'state -> atom -> bool;
trans : 'state -> atom -> action -> 'state;
}
(*===========================================================================*)
(* EXPRESSIONS *)
(*===========================================================================*)
(** KAT expressions, as usually defined on paper. *)
module Exp = struct
(* boolean expressions *)
type b =
| True
| False
| Test of test
| And of b * b
| Or of b * b
| Not of b
(* KAT expressions *)
type t =
| Test of b
| Action of action
| Seq of t * t
| Union of t * t
| Star of t
let skip = Test True
let abort = Test False
(*=========================================================================*)
(* BRZOZOWKSI DERIVATIVES & DFA *)
(* See, for example, Section 15.4.2 in the following paper: *)
(* http://www.cs.cornell.edu/~kozen/Papers/ChenPucella.pdf *)
(*=========================================================================*)
let rec eval_bexp (b : b) (a : atom) : bool =
match b with
| True -> true
| False -> false
| Test t -> a t
| And (c1, c2) -> eval_bexp c1 a && eval_bexp c2 a
| Or (c1, c2) -> eval_bexp c1 a || eval_bexp c2 a
| Not c -> not (eval_bexp c a)
let rec epsilon (e : t) (a : atom) : bool =
match e with
| Test b -> eval_bexp b a
| Action _ -> false
| Seq (f1, f2) -> epsilon f1 a && epsilon f2 a
| Union (f1, f2) -> epsilon f1 a || epsilon f2 a
| Star _ -> true
let rec delta (e : t) (a : atom) (p : action) : t =
match e with
| Test _ ->
abort
| Action q ->
if p = q then skip else abort
| Seq (f1, f2) ->
let d = Seq (delta f1 a p, f2) in
if epsilon f1 a then
Union (d, delta f2 a p)
else
d
| Union (f1, f2) ->
Union (delta f1 a p, delta f2 a p)
| Star f ->
Seq (delta f a p, Star f)
(** The Brzozowksi automaton for a given expression.
Caveat: The state space of this automaton is the set of expressions,
which is infinite.
*)
let brzozowski_dfa (e : t) : t dfa =
{ start = e;
obs = epsilon;
trans = delta;
}
end
(*===========================================================================*)
(* EXPRESSIONS MODULO ACI *)
(*===========================================================================*)
(*
The key issue with the Brozozowksi automaton, as defined above, is that it
has infinitely many states. This problem can be solved by equating expressions
that are equivalent modulo ACI (associativity, commutativity, and idempotence).
To make this practical, we define another type of expressions, which is
modulo ACI by construction.
*)
(** KAT expressions modulo ACI. *)
module ExpACI = struct
open Hashcons
(** Consult the following links for documentation:
* https://en.wikipedia.org/wiki/Hash_consing
* https://www.lri.fr/~filliatr/ftp/publis/hash-consing2.pdf
* https://github.com/backtracking/ocaml-hashcons/blob/master/hashcons.mli
*)
type t = node Hashcons.hash_consed
and node =
| Test of test * bool (** positive or negated test *)
| Action of action
| Seq of t list (** modulo associativity *)
| Union of node Hashcons.Hset.t (** modulo associativity, commutativity, idempotence *)
| Star of t
let tbl = Hashcons.create 10000
let hashcons : node -> t = Hashcons.hashcons tbl
(** skip and abort, also know as true and false *)
let skip = hashcons (Seq [])
let abort = hashcons (Union Hashcons.Hset.empty)
let equal e f = (e.tag = f.tag)
let compare e f = Pervasives.compare e.tag f.tag
let hash e = e.hkey
let is_skip e = equal e skip
let is_abort e = equal e abort
let mk_seq (e: t) (f: t) : t =
let sequents e =
match e.node with
| Seq xs -> xs
| _ -> [e]
in
if is_abort e || is_abort f then abort else
match sequents e @ sequents f with
| [x] -> x
| xs -> hashcons (Seq xs)
let mk_union (e: t) (f: t) : t =
let open Hashcons.Hset in
let disjuncts e =
match e.node with
| Union xs -> xs
| _ -> singleton e
in
let xs = union (disjuncts e) (disjuncts f) in
match elements xs with
| [x] -> x
| _ -> hashcons (Union xs)
let mk_star (e : t) : t =
if is_skip e || is_abort e then skip else
match e.node with
| Star f -> f
| _ -> hashcons (Star e)
let rec of_bexp (b : Exp.b) ~negate : t =
match b with
| True -> if negate then abort else skip
| False -> if negate then skip else abort
| Test t -> hashcons (Test (t, not negate))
| Not b -> of_bexp b ~negate:(not negate)
| And (b1, b2) -> mk_seq (of_bexp b1 ~negate) (of_bexp b2 ~negate)
| Or (b1, b2) -> mk_union (of_bexp b1 ~negate) (of_bexp b2 ~negate)
let rec of_exp (e : Exp.t) : t =
match e with
| Test b -> of_bexp b ~negate:false
| Action a -> hashcons (Action a)
| Seq (e1, e2) -> mk_seq (of_exp e1) (of_exp e2)
| Union (e1, e2) -> mk_union (of_exp e1) (of_exp e2)
| Star e -> mk_star (of_exp e)
(*=========================================================================*)
(* BRZOZOWKSI DERIVATIVES & DFA *)
(*=========================================================================*)
let rec epsilon (e : t) (a : atom) : bool =
match e.node with
| Test (t, positive) ->
if positive then a t else not (a t)
| Action _ ->
false
| Seq es ->
List.for_all (fun e -> epsilon e a) es
| Union es ->
Hashcons.Hset.exists (fun e -> epsilon e a) es
| Star _ ->
true
let rec delta (e : t) (a : atom) (p : action) : t =
match e.node with
| Test _ ->
abort
| Action q ->
if p = q then skip else abort
| Seq es ->
delta_seq es abort a p
| Union es ->
Hashcons.Hset.elements es
|> List.map (fun e -> delta e a p)
|> List.fold_left mk_union abort
| Star e0 ->
mk_seq (delta e0 a p) e
and delta_seq (es : t list) (acc : t) (a : atom) (p : action) : t =
match es with
| [] -> acc
| e::es ->
let d = mk_seq (delta e a p) (hashcons (Seq es)) in
let acc = mk_union acc d in
if epsilon e a then
delta_seq es acc a p
else
acc
(** The Brzozowksi automaton for a given expression.
In contrast to Exp.brzozowski_dfa, this function gives a finite state
(and in fact fairly small) DFA.
*)
let brzozowski_dfa (e : Exp.t) : t dfa =
{ start = (of_exp e);
obs = epsilon;
trans = delta;
}
(*=========================================================================*)
(* pretty-printing *)
(*=========================================================================*)
let pp fmt (t : t) =
let rec pp_star fmt t =
match t.node with
| Test (t, positive) ->
Format.fprintf fmt !"@[<h>%s%{sexp:test}@]"
(if positive then "" else "¬")
t
| Action a ->
Format.fprintf fmt !"@[<h>%{sexp:action}@]" a
| Seq [] ->
Format.fprintf fmt "1"
| Seq _ ->
Format.fprintf fmt "@[(%a)@]" pp_seq t
| Union xs when Hashcons.Hset.is_empty xs->
Format.fprintf fmt "0"
| Union _ ->
Format.fprintf fmt "@[(%a)@]" pp_union t
| Star e ->
Format.fprintf fmt "@[%a*@]" pp_star e
and pp_seq fmt t =
match t.node with
| Seq (_::_ as xs) ->
Format.pp_print_list
~pp_sep:(fun fmt () -> Format.pp_print_string fmt "·")
(fun fmt x -> Format.fprintf fmt "@[%a@]" pp_star x)
fmt
xs
| _ ->
Format.fprintf fmt "@[(%a)@]" pp_union t
and pp_union fmt t =
match t.node with
| Union xs when not (Hashcons.Hset.is_empty xs) ->
Format.pp_print_list
~pp_sep:(fun fmt () -> Format.pp_print_text fmt " + ")
(fun fmt x -> Format.fprintf fmt "@[%a@]" pp_union x)
fmt
(Hashcons.Hset.elements xs)
| Seq (_::_) ->
pp_seq fmt t
| _ ->
pp_star fmt t
in
pp_union fmt t
end