-
Notifications
You must be signed in to change notification settings - Fork 1
/
lr_parser.ml
677 lines (549 loc) · 20 KB
/
lr_parser.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
(* A GADT that captures Yacc production rule
e.g. E : X Y Z { sem_act }
this should have type (a -> b -> c -> d, d) syn
*)
module O = Ordering
module type UDT = sig
type t
type eof
val compare : t -> t -> int
val to_string :t -> string
val eof_to_string : string
end
module Token (UserDefinedToken : UDT) = struct
module UDT = UserDefinedToken
type _ token =
| Tk : UDT.t -> UDT.t token
| Eps : unit token
| Eof : UDT.eof token
and tbox = TokBox : 'a token -> tbox
let tok t = Tk t
let compare_token : type a b. a token -> b token -> (a, b) O.ordering =
fun l r ->
match l, r with
| Tk t1, Tk t2 -> begin
let c = UDT.compare t1 t2 in
if c = 0 then O.EQ else (if c < 0 then O.LT else O.GT)
end
| Eps, Eps -> O.EQ
| Eof, Eof -> O.EQ
| _, _ -> begin
match () with
| () when TokBox l < TokBox r -> O.LT
| () -> O.GT
end
module TokenOrder = struct
type 'a t = 'a token
let compare = compare_token
end
include Hset.Make(TokenOrder)
let token_to_string : type a. a token -> string = fun t ->
match t with
| Tk t -> UDT.to_string t
| Eps -> "Epsilon"
| Eof -> UDT.eof_to_string
let token_list_to_string tl =
let f = {fold = fun t acc -> token_to_string t :: acc} in
let strs = List.rev (fold f tl []) in
String.concat " " strs
end
module CharToken = struct
type t = char
type eof
let compare c1 c2 = Char.compare c1 c2
let to_string t = String.make 1 t
let eof_to_string = "EOF"
end
module Tok = Token(CharToken)
(* Define symbol and prod_rule as extensible types, so later we
can redefine a constructor. Totally hack *)
module rec Symbol : sig
type 'a symbol =
| T : 'a Tok.token -> 'a symbol
| NT : ('a SRMap.key * 'a Rules.prod_rule list) Lazy.t -> 'a symbol
val symbol_to_string : 'a symbol -> string
val compare_symbols : 'a symbol -> 'b symbol -> ('a, 'b) O.ordering
val normalize_symbol : 'a symbol -> 'a Rules.norm_prod_rule list
val build_srmap : 'a symbol -> SRMap.t
end = struct
type 'a symbol =
| T : 'a Tok.token -> 'a symbol
| NT : ('a SRMap.key * 'a Rules.prod_rule list) Lazy.t -> 'a symbol
type sbox = SymBox : 'a symbol -> sbox
let symbol_to_string : type a. a symbol -> string = fun s ->
match s with
| T token -> Tok.token_to_string token
| NT (lazy (k, _)) -> SRMap.string_of_key k
let compare_symbols : type a b. a symbol -> b symbol -> (a, b) O.ordering = fun s1 s2 ->
match s1, s2 with
| T t1 , T t2 -> Tok.compare_token t1 t2
| NT (lazy (k1,_)), NT (lazy (k2, _)) -> SRMap.compare_keys k1 k2
| T _, NT _ -> O.LT
| _ -> O.GT
let normalize_symbol: type a. a symbol -> a Rules.norm_prod_rule list = function
| T _ as t -> [Rules.S {Rules.semantic= (fun x -> x);
syntax = Syntax.SCons (t, Syntax.SNil)}]
| NT (lazy (_, k_rules)) -> List.map Rules.split k_rules
let build_srmap s =
let srmap = ref (SRMap.empty) in
let rec dfs : type a. a symbol -> unit = fun s ->
match s with
| T _ -> ()
| NT (lazy (k, _)) as nt -> begin
match SRMap.find !srmap k with
| Some _ -> ()
| None -> begin
let npr = normalize_symbol nt in
srmap := SRMap.add !srmap k npr;
List.iter (fun (Rules.S s) -> Syntax.iter_syntax {Syntax.iter = dfs} s.Rules.syntax) npr
end
end in
dfs s;
!srmap
end
and Syntax : sig
type (_, _) syn =
| SNil : ('a, 'a) syn
| SCons : 'c Symbol.symbol * ('a, 'b) syn -> ('c -> 'a, 'b) syn
type iter_syn = {iter : 'a. 'a Symbol.symbol -> unit}
val iter_syntax : iter_syn -> ('a, 'b) syn -> unit
val snoc : ('a, 'c -> 'b) syn -> 'c Symbol.symbol -> ('a, 'b) syn
val syn_to_string: ('a, 'b) syn -> string
end = struct
type (_, _) syn =
| SNil : ('a, 'a) syn
| SCons : 'c Symbol.symbol * ('a, 'b) syn -> ('c -> 'a, 'b) syn
type iter_syn = {iter : 'a. 'a Symbol.symbol -> unit}
let rec iter_syntax : type a b. iter_syn -> (a, b) syn -> unit = fun iter s ->
match s with
| SNil -> ()
| SCons (hd, tl) -> iter.iter hd; iter_syntax iter tl
(* Append symbol at the end of syntax list *)
let rec snoc : type a b c. (a, c -> b) syn -> c Symbol.symbol -> (a, b) syn =
fun l sym ->
match l with
| SNil -> SCons (sym, SNil)
| SCons (sym', rhs) -> SCons (sym', snoc rhs sym)
let syn_to_string : type a b. (a, b) syn -> string = fun s ->
let buf = Buffer.create 20 in
let f s =
Buffer.add_string buf (Symbol.symbol_to_string s); Buffer.add_string buf " " in
let iter = {iter = f} in
iter_syntax iter s;
Buffer.contents buf
end
and Rules : sig
(* production rule *)
type _ prod_rule =
| SemAct : 'a -> 'a prod_rule
| Appl : ('a -> 'b) prod_rule * 'a Symbol.symbol -> 'b prod_rule
(* 'a is the result type, 'b is the type of semantic function *)
type ('a, 'b) ss = {semantic : 'b; syntax : ('b, 'a) Syntax.syn}
(* normalized Yacc rule *)
type _ norm_prod_rule =
| S : ('a, 'b) ss -> 'a norm_prod_rule
val split : 'a prod_rule -> 'a norm_prod_rule
val normalize_rule_lists : 'a prod_rule list -> 'a norm_prod_rule list
end = struct
(* production rule *)
type _ prod_rule =
| SemAct : 'a -> 'a prod_rule
| Appl : ('a -> 'b) prod_rule * 'a Symbol.symbol -> 'b prod_rule
(* 'a is the result type, 'b is the type of semantic function *)
type ('a, 'b) ss = {semantic : 'b; syntax : ('b, 'a) Syntax.syn}
(* normalized Yacc rule *)
type _ norm_prod_rule =
| S : ('a, 'b) ss -> 'a norm_prod_rule
(* split semantic action and syntax from an applicative structure *)
let rec split : type a. a prod_rule -> a norm_prod_rule = function
| SemAct semantic -> S {semantic; syntax = Syntax.SNil}
| Appl (f, sym) ->
let S {semantic; syntax} = split f in
S {semantic; syntax = Syntax.snoc syntax sym}
let normalize_rule_lists : type a. (a prod_rule list) -> (a norm_prod_rule list) = fun pl ->
List.map split pl
end
(* [module SRMap] is an heterogeneous map of [norm_prod_rule list] *)
and SRMap: sig
type t
type 'a key
type 'a value = 'a Rules.norm_prod_rule list
val fresh_key : 'a Rules.prod_rule list -> 'a key
val gen : 'a Rules.prod_rule list -> 'a key * 'a Rules.prod_rule list
val compare_keys : 'a key -> 'b key -> ('a, 'b) O.ordering
val string_of_key : 'a key -> string
val empty : t
val find : t -> 'a key -> 'a value option
val add : t -> 'a key -> 'a value -> t
end = struct
type _ acc = ..
type boxed_acc = Boxed_acc : _ acc -> boxed_acc
type (_, _) equality =
| Refl : ('a, 'a) equality
type 'a key = {
k : 'a Rules.prod_rule list;
tag : 'a acc;
stamp: string;
eq : 'b. 'b acc -> ('a, 'b) equality option;
cmp : 'b. 'b acc -> ('a, 'b) O.ordering;
}
type 'a value = 'a Rules.norm_prod_rule list
let stamp =
let i = ref 0 in
fun () -> incr i; Printf.sprintf "T%d" !i
let string_of_key k = k.stamp
let fresh_key (type a) (w: a Rules.prod_rule list) : a key =
let module M = struct type _ acc += T : a acc end in
let eq : type b. b acc -> (a, b) equality option =
function M.T -> Some Refl | _ -> None in
let cmp : type b. b acc -> (a, b) O.ordering = function
M.T -> O.EQ
| v when Boxed_acc M.T < Boxed_acc v -> O.LT
| _ -> O.GT
in
{k = w; tag = M.T; stamp = stamp (); eq; cmp}
let gen rules =
(fresh_key rules), rules
let compare_keys : 'a 'b. 'a key -> 'b key -> ('a, 'b) O.ordering =
fun {cmp} {tag} -> cmp tag
(* mapping from ['a key] to ['a value] *)
module KVMap = Hmap.Make
(struct
type 'a t = 'a key
type 'a value = 'a Rules.norm_prod_rule list
let compare l r = compare_keys l r
end)
type t = KVMap.t
let find: type a. t -> a key -> a value option =
fun map k -> KVMap.find k map
let add : type a. t -> a key -> a value -> t =
fun map k v -> KVMap.add k v map
let empty = KVMap.empty
end
include Symbol
include Syntax
include Rules
include SRMap
exception Unnormalized_rule
let pure f = SemAct f
let (<*>) a b = Appl (a, b)
let exact c = T (Tok.tok c)
module ItemSet = struct
type (_, _) item =
| Item : 'c SRMap.key * (* Key associated with item *)
('a, 'b) syn * (* symbols before dot *)
('b, 'c) syn * (* symbols after dot *)
Tok.t (* look ahead *)
-> ('a, 'c) item
let item_to_string : type a b. (a, b) item -> string = fun item ->
match item with
| Item (k, fst, snd, tl) ->
Printf.sprintf "%s -> %s . %s [%s]"
(SRMap.string_of_key k)
(syn_to_string fst)
(syn_to_string snd)
(if Tok.is_empty tl then "$" else Tok.token_list_to_string tl)
let rec compare_syns :
type a b c d. (a, b) syn -> (c, d) syn -> (_, _) O.ordering = fun s1 s2 ->
match s1, s2 with
| SNil, SNil -> O.EQ
| SCons (_, _), SNil -> O.GT
| SNil, SCons (_, _) -> O.LT
| SCons (hdx, tlx), SCons (hdy, tly) ->
match compare_symbols hdx hdy, compare_syns tlx tly with
| O.EQ, O.EQ -> O.EQ
| O.EQ, O.GT -> O.GT
| O.EQ, O.LT -> O.LT
| O.GT, _ -> O.GT
| O.LT, _ -> O.LT
let compare_items :
type a b c d. (a, b) item -> (c, d) item -> (_, _) O.ordering = fun s1 s2 ->
match s1, s2 with
| Item (k1, sx1, sy1, t1), Item (k2, sx2, sy2, t2) ->
match SRMap.compare_keys k1 k2 with
| O.EQ -> begin
match compare_syns sx1 sx2, compare_syns sy1 sy2 with
| O.EQ, O.EQ -> begin
let c = Tok.compare t1 t2 in
if c = 0 then O.EQ else (if c < 0 then O.LT else O.GT)
end
| O.EQ, O.LT -> O.LT
| O.EQ, O.GT -> O.GT
| O.LT, _ -> O.LT
| O.GT, _ -> O.GT
end
| O.LT -> O.LT (* I have to explicitly write this out ?? *)
| O.GT -> O.GT
module ItemOrder = struct
type ('a, 'b) t = ('a, 'b) item
let compare : type a b c d. (a, b) t -> (c, d) t -> (_, _) O.ordering =
compare_items
end
include Hset2.Make(ItemOrder)
let union_all ss =
List.fold_left union empty ss
let to_string s =
let buf = Buffer.create 20 in
let f item =
Buffer.add_string buf (item_to_string item);
Buffer.add_string buf "\n" in
let it = {iter=f} in
iter it s;
Buffer.contents buf
exception Unpreprocessed_non_terminal_symbol
let augment_start : type a b. a symbol -> SRMap.t -> SRMap.t * t = fun s env ->
match s with
| NT n as nt -> begin
let k = fst (Lazy.force n) in
match SRMap.find env k with
| None -> raise Unpreprocessed_non_terminal_symbol
| Some r ->
let hd_syn = SNil and tl_syn = SCons (nt, SNil) in
let augmented_rules = [pure (fun x -> x) <*> nt] in
let normed_rules = List.map split augmented_rules in
let key = SRMap.fresh_key augmented_rules in
let new_env = SRMap.add env key normed_rules in
new_env, singleton (Item (key, hd_syn, tl_syn, Tok.empty))
end
| _ -> invalid_arg "ItemSet.argument_start: Invalid symbol"
let shift_dot : type a b. (a, b) item -> (a, b) item option = fun item ->
match item with
| Item (k, alpha, SNil, token_list) -> None
| Item (k, alpha, SCons (hd, tl), token_list) ->
Some (Item (k, (snoc alpha hd), tl, token_list))
let shift_dot_exn : type a b. (a, b) item -> (a, b) item = fun item ->
match item with
| Item (k, alpha, SNil, token_list) -> invalid_arg "ItemSet.shift_dot_exn"
| Item (k, alpha, SCons (hd, tl), token_list) ->
Item (k, (snoc alpha hd), tl, token_list)
let rule_to_itemset :
type a b. a norm_prod_rule -> a SRMap.key -> Tok.t -> t =
fun r k tl ->
match r with
| S ss ->
let syn = ss.syntax in
singleton (Item (k, SNil, syn, tl))
let first_set : type a b. (a, b) syn -> SRMap.t -> Tok.t = fun s env ->
let rec loop : type a b. (a, b) syn -> Tok.t = fun s ->
match s with
| SNil -> Tok.empty
| SCons (hd, tl) -> begin
match hd with
| T t -> begin
let cont : type a. a Tok.token -> Tok.t = fun t ->
match t with
| Tok.Eps -> loop tl
| _ -> Tok.add t Tok.empty in
cont t
end
| NT p -> begin
let k, _ = Lazy.force p in
match SRMap.find env k with
| Some rules ->
let fold_f acc r =
match r with
| S ss -> Tok.union acc (loop ss.syntax) in
List.fold_left fold_f Tok.empty rules
| None -> Tok.empty
end
end in
loop s
let close_item : type a b c. (a, b) item -> SRMap.t -> t = fun item env ->
let loop : type a b c. (a, b) item -> SRMap.t -> Tok.t -> t = fun item env l ->
match item with
| Item (_, _, SNil, _) -> empty
| Item (k, _, SCons (hd, tl), token_list) -> begin
match hd with
| T _ -> empty
| NT pair -> begin
let k, _ = Lazy.force pair in
match SRMap.find env k with
| None -> raise Unpreprocessed_non_terminal_symbol
| Some rules -> begin
let first =
let t = first_set tl env in
if Tok.is_empty t then token_list else t in
let items = List.map (fun r -> rule_to_itemset r k first) rules in
union_all items
end
end
end in
match item with
| Item (_, _, _, token_list) -> loop item env token_list
let close_items set env =
let f = {fold = fun item acc -> union acc (close_item item env)} in
fold f set empty
let closure : t -> SRMap.t -> t = fun set env ->
let rec loop set acc =
if is_empty set then acc else begin
loop (close_items set env) (union set acc) end in
loop (close_items set env) set
end
module Automata = struct
module rec Trans : sig
module TransOrder : sig
type 'a t = 'a symbol
type 'a value = State.state
val compare : 'a symbol -> 'b symbol -> ('a, 'b) O.ordering
end
module Transitions : Hmap.S with type 'a key = 'a TransOrder.t
and type 'a value = 'a TransOrder.value
end = struct
module TransOrder = struct
type 'a t = 'a symbol
type 'a value = State.state
let compare = compare_symbols
end
module Transitions = Hmap.Make(TransOrder)
end
and State : sig
type state = {
items : ItemSet.t;
mutable trans : Trans.Transitions.t;
}
end = State
include Trans
include State
module StateOrder = struct
type t = state
let compare s1 s2 = ItemSet.compare s1.items s2.items
end
module Am = Set.Make(StateOrder)
let exists_items items' t = Am.exists (fun s ->
ItemSet.compare s.items items' = 0) t
let rec add_item_to_transet :
's symbol -> ('a, 'b) ItemSet.item -> Transitions.t -> Transitions.t =
fun sym item ts ->
match Transitions.find sym ts with
| Some state -> begin
let new_items = ItemSet.add item state.items in
Transitions.add sym {state with items = new_items} ts
end
| None -> begin
let state = {
items = ItemSet.add item ItemSet.empty;
trans = Transitions.empty;} in
Transitions.add sym state ts
end
let build_transet : ItemSet.t -> Transitions.t = fun its ->
let fold :
type a b. (a, b) ItemSet.item -> Transitions.t -> Transitions.t =
fun it l ->
match it with
| ItemSet.Item (_, _, SCons (s, _), _) ->
add_item_to_transet s (ItemSet.shift_dot_exn it) l
| _ -> l in
ItemSet.fold {ItemSet.fold} its Transitions.empty
let rec aug_transet : Transitions.t -> SRMap.t -> Am.t -> Transitions.t =
fun l env am ->
let fold sym state acc =
let {items; trans} = state in
let new_items = ItemSet.closure items env in
let phantom_set = {items = new_items; trans = Transitions.empty} in
try
let exist_set = Am.find phantom_set am in
Transitions.add sym exist_set acc
with Not_found ->
let new_state = {items = new_items; trans;} in
Transitions.add sym new_state acc in
Transitions.fold {Transitions.fold} l Transitions.empty
(* The above line looks really weird, isn't it? *)
module ItemSetSet = Set.Make(struct
type t = ItemSet.t
let compare = ItemSet.compare
end)
let build_automata : ItemSet.t -> SRMap.t -> state * Am.t = fun is env ->
let states = ref Am.empty in
let rec visit state () =
let {items; trans} = state in
if exists_items items !states then () else begin
states := Am.add state !states;
let new_trans = aug_transet (build_transet items) env !states in
state.trans <- new_trans;
let iter : type a. a symbol -> state -> unit = fun _ s' ->
visit s' () in
Transitions.iter {Transitions.iter} new_trans
end in
let s0 = {items = is; trans = Transitions.empty} in
visit s0 ();
s0, !states
let automata_to_string state =
let htb : (int, int) Hashtbl.t = Hashtbl.create 64 in
let hadd, hlookup =
let i = ref 0 in
(fun is -> incr i; Hashtbl.add htb (Hashtbl.hash is) !i),
(fun is -> try Some (Hashtbl.find htb (Hashtbl.hash is)) with _ -> None) in
let buf = Buffer.create 64 in
let rec visit s () =
let {items; trans} = s in
match hlookup items with
| Some _ -> ()
| None -> begin
hadd items;
let iter : type a. a symbol -> state -> unit =
fun _ st -> visit st () in
Transitions.iter {Transitions.iter} trans
end in
visit state ();
let htb1 : (int, bool) Hashtbl.t = Hashtbl.create 64 in
let rec visit s () =
let {items; trans} = s in
let k = Hashtbl.hash items in
if Hashtbl.mem htb1 k then () else begin
Hashtbl.add htb1 k true;
Buffer.add_string buf (Printf.sprintf "State S%d:\n" (Hashtbl.find htb k));
Buffer.add_string buf (ItemSet.to_string items);
let iter : type s. s symbol -> state -> unit = fun sym state ->
let items = state.items in
Buffer.add_string buf (Printf.sprintf "from: %s to " (symbol_to_string sym));
Buffer.add_string buf (Printf.sprintf "S%d \n" (Hashtbl.find htb (Hashtbl.hash items)))
in
Transitions.iter {Transitions.iter} trans;
Buffer.add_string buf "---------\n";
let iter : type a. a symbol -> state -> unit =
fun _ st -> visit st () in
Transitions.iter {Transitions.iter} trans
end
in
visit state ();
Buffer.contents buf
end
(*
S -> C C
C -> c C | d
*)
(* Grammar 4.55 from Aho's Dragon book *)
type s = SS of c * c
and c = C1 of char * c | C2 of char
let rec s = NT (lazy (SRMap.gen [
(pure (fun c1 c2 -> SS (c1, c2)) <*> c <*> c)]))
and c = NT (lazy (SRMap.gen [
(pure (fun ch c -> C1 (ch, c)) <*> exact 'c' <*> c);
(pure (fun c -> C2 c) <*> exact 'd')]))
module Test = struct
let env = build_srmap s
(* [s_set] is kernel item set *)
let env, s_set = ItemSet.augment_start s env
let s_first = ItemSet.closure s_set env
let s0, states = Automata.build_automata s_first env
let ams () = Automata.automata_to_string s0
end
let () = print_endline (Test.ams ())
(* Notes on challenges:
Not to mention all ugliness brought by CSP and let-rec-and generation,
what we are attempting to do constantly hits the limit of MetaOCaml or OCaml.
1. reduce function
say we have a function of form ``fun x y z -> x + y * z``.
we would like to generate a function ``f`` of form:
``fun g x y z -> g (x + y * z)``
2. pattern matching generation
plan: using nested if-then-else
3. performance problem
our code is complete type-safe, so all comparison
functions only tell you if two things are equal or not. This
means we cannot have tree-like data structure to do O(logN) operation.
e.g: ``equal_symbols`` can be very costly
Thought: maybe using some hash?
4. let-rec-and generation are almost solved by Jun Inoue and Oleg3.
Actually, using Jeremy's current approach is not bad
*)