File size: 44,269 Bytes
d5bfab8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
structure mcts :> mcts =
struct

open HolKernel Abbrev boolLib aiLib smlParallel psMCTS 
  mlNeuralNetwork smlExecScripts mlTreeNeuralNetworkAlt kernel bloom

val ERR = mk_HOL_ERR "mcts"

(* -------------------------------------------------------------------------
   Globals
   ------------------------------------------------------------------------- *)

val simple_target = ref []

val use_mkl = ref false
val use_ob = ref false
val use_para = ref false
val dim_glob = ref 64
val ncore = 20 (* 20 *)
val ntarget = 20 * 6 * 2 (* 20 * 12 * 2 *)

(* -------------------------------------------------------------------------
   Conversions betwen lists of reals/ints and strings
   ------------------------------------------------------------------------- *)

fun string_to_real x = valOf (Real.fromString x)

fun ilts x = String.concatWith " " (map its x)
fun stil x = map string_to_int (String.tokens Char.isSpace x)
fun rlts x = String.concatWith " " (map rts x)
fun strl x = map string_to_real (String.tokens Char.isSpace x)

(* -------------------------------------------------------------------------
   Parameters
   ------------------------------------------------------------------------- *)

val target_glob = ref []
val maxbigsteps = 1
val noise_flag = ref false
val noise_coeff_glob = ref 0.1
val nsim_opt = ref (NONE)
val time_opt = ref (SOME 60.0)

fun string_of_timeo () = (case !time_opt of
    NONE => "Option.NONE"
  | SOME s => "Option.SOME " ^ rts s)

fun spiky_noise () = if random_real () > 0.9 then 1.0 else 0.0
fun uniform_noise () = 1.0
val uniform_flag = ref false
fun random_noise () = random_real ()

fun mctsparam () =
  {explo_coeff = 1.0,
   noise = !noise_flag, noise_coeff = !noise_coeff_glob, 
   noise_gen = if !uniform_flag then uniform_noise else random_noise,
   nsim = !nsim_opt : int option, time = !time_opt: real option};


val coreid_glob = ref 0
val ngen_glob = ref 0

val in_search = ref false

val embd = ref (dempty Term.compare)
val seqwind = ref (eempty seq_compare)
val progwind = ref (eempty progi_compare)

val progd = ref (eempty progi_compare)
val notprogd = ref (eempty progi_compare)

val semb = ref (BoolArray.fromList [])
val use_semb = ref true
val semd = ref (eempty seq_compare)
val progsemd = ref (eempty seq_compare)

val minid = ref (dempty seq_compare)
val initd = ref (eempty progi_compare)

fun eaddi x d = d := eadd x (!d)
fun ememi x d = emem x (!d)

fun spacetime space tim = 
  Math.pow (2.0, Real.fromInt space) * Real.fromInt tim

(* -------------------------------------------------------------------------
   Main process loging function
   ------------------------------------------------------------------------- *)

val expname = ref "test"

fun log s =
  let val expdir = selfdir ^ "/exp/" ^ !expname in
    print_endline s;
    append_endline (expdir ^ "/log") s
  end

(* -------------------------------------------------------------------------
   Type abbreviations
   ------------------------------------------------------------------------- *)

type seq = kernel.seq
type prog = kernel.prog
type progi = kernel.progi
type tnn = mlTreeNeuralNetworkAlt.tnn
type 'a set = 'a Redblackset.set

(* -------------------------------------------------------------------------
   Move
   ------------------------------------------------------------------------- *)

datatype move = Unit of int | Oper of int * int | Pair

fun postpair b a = (a,b)
val l05 = List.tabulate (6,I)
val movelg_org = 
  map Unit nullaryidl @
  map Oper (map (postpair 0) binaryidl @ 
            map (postpair 1) binaryidl_nocomm) @
  map (fn x => Oper (cond_id,x)) l05 @
  map (fn x => Oper (loop_id,x)) l05 @
  [Pair] @
  map (fn x => Oper (compr_id,x)) [0,1]

fun id_of_move m = case m of 
    Unit id => [id] 
  | Oper (id,ord) => [id,ord]
  | Pair => [1000]

fun move_compare (m1,m2) = seq_compare (id_of_move m1, id_of_move m2)

fun string_of_move m =
  let val l = id_of_move m in
    if hd l = 1000 then "pair" else
    name_of_oper (hd l) ^ "-" ^ String.concatWith "-" (map its (tl l))
  end

val movelg = dict_sort move_compare movelg_org
val movev = Vector.fromList movelg
val maxmove = length movelg
val moved = dnew move_compare (number_snd 0 movelg)
fun index_of_move move = dfind move moved
fun move_of_index index = Vector.sub (movev,index)

fun is_unit move = case move of Unit _ => true | _ => false
val movelg_nounit = filter (not o is_unit) movelg

(* -------------------------------------------------------------------------
   Build a list of programs
   ------------------------------------------------------------------------- *)

fun partopt_aux n acc l =
  if n <= 0 then SOME (rev acc,l)
  else if null l then NONE
  else partopt_aux (n - 1) (hd l :: acc) (tl l);

fun partopt n l = partopt_aux n [] l;

fun longereq n l =
  if n <= 0 then true else if null l then false else longereq (n-1) (tl l)

(* -------------------------------------------------------------------------
   Board
   ------------------------------------------------------------------------- *)

type clause = int * progi
datatype clausex = C1 of clause | C2 of clause * clause

fun prog_to_clause p = (prog_size p, zip_prog p)
val clause_compare = cpl_compare Int.compare progi_compare

fun prog_compare_size (p1,p2) = 
  clause_compare (prog_to_clause p1, prog_to_clause p2)

fun equal_clause (a,b) = (clause_compare (a,b) = EQUAL)

fun clausex_compare (a,b) = case (a,b) of
    (C1 a', C1 b') => clause_compare (a',b')
  | (C1 _ , C2 _ ) => LESS
  | (C2 _ , C1 _ ) => GREATER
  | (C2 a', C2 b') => cpl_compare clause_compare clause_compare (a',b')

fun string_of_clause clause = humanf (unzip_prog (snd clause))
fun string_of_clausex clausex = case clausex of
    C1 a => string_of_clause a
  | C2 (a,b) => "(pair " ^ string_of_clause a ^ " " ^ string_of_clause b ^ ")"

type board = clausex list
type player = (board,move) psMCTS.player
fun string_of_board (board : board) = 
  "# " ^ String.concatWith "\n# " (map string_of_clausex board)
fun board_compare (a,b) = list_compare clausex_compare (a,b)

(* -------------------------------------------------------------------------
   Permutation of arguments
   ------------------------------------------------------------------------- *)

fun permute_triple n (a,b,c) = 
  if n = 0 then SOME (a,b,c) else
  if n = 1 then (if equal_clause (c,b) then NONE else SOME (a,c,b)) else
  if n = 2 then (if equal_clause (b,a) then NONE else SOME (b,a,c)) else
  if n = 3 then (if equal_clause (c,a) then NONE else SOME (b,c,a)) else
  if n = 4 then (if equal_clause (c,a) then NONE else SOME (c,a,b)) else
  if n = 5 then (if equal_clause (c,b) orelse equal_clause (b,a) 
                 then NONE else SOME (c,b,a)) 
  else raise ERR "permute_triple" "" 

fun permute_double n (a,b) =
  if n = 0 then SOME (a,b) else
  if n = 1 then (if equal_clause (a,b) then NONE else SOME (b,a))
  else raise ERR "permute_double" ""

(* -------------------------------------------------------------------------
   Application of a move to a board
   ------------------------------------------------------------------------- *)

val complexity_compare = cpl_compare Real.compare progi_compare

fun update_minid p (leafn,pi) (sem,tim) =
  let 
    val rep = dfind sem (!minid) 
    val newrep = (spacetime leafn tim, pi)
  in
    if complexity_compare (newrep,rep) = LESS
    then minid := dadd sem newrep (!minid)
    else ()
  end
  handle NotFound => ()

fun update_wind p pi sem =
  let 
    val winseql = find_wins p sem
    val b = ref false
    fun f x = if not (ememi x seqwind) 
      then (eaddi x seqwind; b := true) else ()
  in
    app f winseql;
    if !b then eaddi pi progwind else ()
  end

exception ResultP of prog;

fun check_simple_target p =
  let 
    val f = mk_exec p
    fun test (x,e) = fst (f(x,0)) = e handle Div => false
    fun loop i maxn = 
      if i >= maxn then NONE else
      if all test (number_fst i (!simple_target)) 
      then SOME (shift_prog i p)
      else loop (i+1) maxn
  in
    loop 0 16
  end

fun raise_simple_target p = case check_simple_target p of
    SOME newp => raise ResultP newp
  | NONE => ()

fun is_incr newc board = case board of
    C1 c :: m => clause_compare (newc,c) <> GREATER
  | C2 (c,_) :: m => clause_compare (newc,c) <> GREATER
  | [] => true

fun exec_fun_insearch p (leafn,pi) plb =
  if ememi pi progd then SOME (C1 (leafn,pi) :: plb)
  else if ememi pi notprogd then NONE
  else (* unseen program *)
    case semtimo_of_prog p of 
      NONE => (eaddi pi notprogd; NONE) 
    | SOME (sem,tim) =>
      (
      if !simple_search then raise_simple_target p else ();
      update_minid p (leafn,pi) (sem,tim);
      if not (ememi pi initd) andalso 
         (if !use_semb then bmem sem (!semb) else ememi sem semd) 
      then (eaddi pi notprogd; NONE) 
      else (* unseen sequence *)
        (
        if not (!simple_search) then update_wind p pi sem else ();
        eaddi pi progd;
        (if !use_semb then badd sem (!semb) else eaddi sem semd); 
        SOME (C1 (leafn,pi) :: plb)
        )
      )

val check_order = ref true

fun exec_fun p leafn plb =
  let val pi = zip_prog p in
    if !check_order andalso not (is_incr (leafn,pi) plb) then NONE 
    else if !in_search
      then exec_fun_insearch p (leafn,pi) plb
      else SOME (C1 (leafn,pi) :: plb)
  end

fun apply_moveo move board = case move of
    Unit id => exec_fun (papp_nullop id) 1 board
  | Oper (id,ord) =>
    (case partopt 2 board of NONE => NONE | SOME (pla,plb) =>
    (case pla of
      [C1 cb, C1 ca] => 
      if not (is_binary id) andalso not (id = compr_id) then NONE else
      let val doubleo = permute_double ord (ca,cb) in
        case doubleo of NONE => NONE | SOME ((na,pia),(nb,pib)) =>
        let 
          val (pa,pb) = (unzip_prog pia,unzip_prog pib)
          val p = papp_binop id (pa,pb)
        in
          if id = compr_id andalso depend_on_i pa then NONE else 
          exec_fun p (na + nb) plb
        end     
      end
    | [C1 cc, C2 (cb,ca)] => 
      if not (mem id [cond_id,loop_id]) then NONE else
      let val tripleo = permute_triple ord (ca,cb,cc) in
        case tripleo of NONE => NONE | SOME ((na,pia),(nb,pib),(nc,pic)) =>
        let val p = papp_ternop id 
          (unzip_prog pia, unzip_prog pib, unzip_prog pic)
        in
          exec_fun p (na + nb + nc) plb
        end
      end
    | _ => NONE
    ))
  | Pair =>
    (case partopt 2 board of NONE => NONE | SOME (pla,plb) =>
    (case pla of
       [C1 ca, C1 cb] => SOME (C2 (ca,cb) :: plb)
     | _ => NONE))
  
fun apply_move move board = valOf (apply_moveo move board)
  handle Option => raise ERR "apply_move" ""

(* -------------------------------------------------------------------------
   Available moves
   ------------------------------------------------------------------------- *)

fun leafn_of_clausex cx = case cx of
    C1 a => fst a
  | C2 (a,b) => fst a + fst b

fun leafn_of_board board = sum_int (map leafn_of_clausex board)

fun is_extendable_aux board = case board of
    C1 a :: m =>
    (#1 a >= leafn_of_board m + 1 andalso is_extendable_aux m)
  | C2 (a,b) :: m => 
    (#1 a >= leafn_of_board m + 1 andalso is_extendable_aux m)
  | _ => true

fun is_extendable board = is_extendable_aux (rev board)

fun available_move board move = isSome (apply_moveo move board)
fun available_movel board = 
  filter (available_move board) 
  (if is_extendable board then movelg else movelg_nounit)

(* -------------------------------------------------------------------------
   For debugging
   ------------------------------------------------------------------------- *)

fun random_step board =
  apply_move (random_elem (available_movel board)) board

fun random_board n = funpow n random_step []
  handle Interrupt => raise Interrupt 
    | _ => random_board n

fun random_prog n = case last (random_board n) of
    C1 c => unzip_prog (snd c)
  | _ => random_prog n
  
fun apply_movel movel board = foldl (uncurry apply_move) board movel

(* -------------------------------------------------------------------------
   Board status (all the checking/caching is done during apply move now)
   ------------------------------------------------------------------------- *)

fun status_of (board : board) = Undecided

(* -------------------------------------------------------------------------
   Game
   ------------------------------------------------------------------------- *)

val game : (board,move) game =
  {
  status_of = status_of,
  available_movel = available_movel,
  apply_move = apply_move,
  string_of_board = string_of_board,
  string_of_move = string_of_move,
  board_compare = board_compare,
  move_compare = move_compare,
  movel = movelg
  }

(* -------------------------------------------------------------------------
   Represent board as a term to give to the TNN
   ------------------------------------------------------------------------- *)

val alpha2 = rpt_fun_type 2 alpha
val alpha3 = rpt_fun_type 3 alpha

(* encoding sequences *)
val natbase = 10
val nat_cat = mk_var ("nat_cat", alpha3);
val nat_neg = mk_var ("nat_neg", alpha2);
val nat_big = mk_var ("nat_big", alpha);
fun embv_nat i = mk_var ("nat" ^ its i,alpha);
val natoperl = List.tabulate (natbase,embv_nat) @ [nat_cat,nat_neg,nat_big];

fun term_of_nat n =
  if n < 0 then mk_comb (nat_neg, term_of_nat (~ n))
  else if n > 1000000 then nat_big
  else if n < natbase then embv_nat n
  else list_mk_comb (nat_cat,
    [embv_nat (n mod natbase), term_of_nat (n div natbase)])

val seq_empty = mk_var ("seq_empty", alpha);
val seq_cat = mk_var ("seq_cat", alpha3);

fun term_of_seq seq = case seq of
    [] => seq_empty
  | a :: m => list_mk_comb 
    (seq_cat, [term_of_nat a, term_of_seq m]);

val seqoperl = natoperl @ [seq_empty,seq_cat]

(* faking two layers *)
fun cap_tm tm = 
  let val name = fst (dest_var tm) in
    mk_var ("cap_" ^ name,alpha2)
  end

fun is_capped tm = 
  let val name = fst (dest_var (fst (strip_comb tm))) in
    String.isPrefix "cap_" name
  end

fun cap_opt tm = 
  if arity_of tm <= 0 
  then NONE
  else SOME (cap_tm tm)

fun cap tm = 
  let val oper = fst (strip_comb tm) in
    mk_comb (cap_tm oper, tm)
  end

(* syntactic *)
fun term_of_prog (Ins (id,pl)) = 
  if null pl then Vector.sub (operv,id) else
  cap (list_mk_comb (Vector.sub (operv,id), map term_of_prog pl))

(* stack *)
val pair_cat = mk_var ("pair_cat",alpha3);
val stack_empty = mk_var ("stack_empty", alpha);
val stack_cat = mk_var ("stack_cat", alpha3);

fun term_of_clause clause = term_of_prog (unzip_prog (snd clause))

fun term_of_clausex clausex = case clausex of
    C1 clause => term_of_clause clause
  | C2 (a,b) => 
    cap (list_mk_comb (pair_cat, [term_of_clause a,term_of_clause b]))
fun term_of_stack_aux board = case board of 
    [] => stack_empty
  | a :: m => 
    cap (list_mk_comb (stack_cat, [term_of_clausex a,term_of_stack_aux m]));

val pair_progseq = mk_var ("pair_progseq", alpha3);

fun term_of_stack board = 
  list_mk_comb (pair_progseq, 
    [term_of_stack_aux board, cap (term_of_seq (first_n 8 (!target_glob)))])

(* policy head *)
val prepoli = mk_var ("prepoli",alpha2)
val head_poli = mk_var ("head_poli", alpha2)

fun term_of_board board = mk_comb (head_poli, 
  mk_comb (prepoli, term_of_stack board))

(* embedding dimensions *)
val operl = vector_to_list operv @ [stack_empty,stack_cat,pair_cat]
val operlcap = operl @ List.mapPartial cap_opt operl
val seqoperlcap = seqoperl @ [cap_tm seq_cat, cap_tm seq_empty]
val allcap = [pair_progseq] @ operlcap @ seqoperlcap

val operlext = allcap @ [prepoli,head_poli]
val opernd = dnew Term.compare (number_snd 0 operlext)

fun dim_std_alt oper =
  if arity_of oper = 0 
  then [0,!dim_glob] 
  else [!dim_glob * arity_of oper, !dim_glob]

fun get_tnndim () = 
  map_assoc dim_std_alt allcap @ 
    [(prepoli,[!dim_glob,!dim_glob]),(head_poli,[!dim_glob,maxmove])] 

(* -------------------------------------------------------------------------
   OpenBlas Foreign Function Interface
   Be aware that using it (use_ob := true + installing openblas) 
   creates a difficult to reproduce bug.
   ------------------------------------------------------------------------- *)

fun fp_op_default oper embl = Vector.fromList [100.0]
val fp_op_glob = ref fp_op_default
val biais = Vector.fromList ([1.0])

local open Foreign in

fun update_fp_op () =
  let
    val lib = loadLibrary (selfdir ^ "/tnn_in_c/ob.so");
    val fp_op_sym = getSymbol lib "fp_op";
    val cra = cArrayPointer cDouble;
    val fp_op0 = buildCall3 (fp_op_sym,(cLong,cra,cra),cVoid);
    fun fp_op oper embl =
      let 
        val n = dfind oper opernd 
        val Xv = Vector.concat (embl @ [biais])
        val X = Array.tabulate (Vector.length Xv, fn i => Vector.sub (Xv,i))
        val Y = Array.tabulate (!dim_glob,fn i => 0.0)
      in 
        fp_op0 (n,X,Y);
        Array.vector Y
      end
  in
    fp_op_glob := fp_op
  end

end (* local *)

(* -------------------------------------------------------------------------
   Create the sequence of moves that would produce a program p
   ------------------------------------------------------------------------- *)

fun inv_cmp cmp (a,b) = cmp (b,a)

fun commute (Ins (oper,argl)) = 
  if is_comm oper
  then Ins (oper, dict_sort (inv_cmp prog_compare_size) (map commute argl))
  else Ins (oper, map commute argl)

fun is_appsyn target prev move =
  case apply_moveo move prev of (* extendable not tested here *)
    NONE => false
  | SOME (C1 clause :: m) => equal_prog (target,unzip_prog (snd clause))
  | _ => false

fun invapp_move board = case board of
    [] => NONE
  | C1 topclause :: m => 
    let val target = unzip_prog (snd topclause) in 
    case target of
      Ins (_,[]) =>
      let 
        val prev = m 
        val move = valOf (List.find (is_appsyn target prev) movelg) 
          handle Option => raise ERR "invapp_move" (humanf target)
      in
        SOME (prev, move)
      end
    | Ins (_,[p1,p2]) =>
      let
        val argl = dict_sort clause_compare (map prog_to_clause [p1,p2])
        val prev = map C1 argl @ m
        val move = valOf (List.find (is_appsyn target prev) movelg) 
          handle Option => 
            (
            print_endline (string_of_board board);
            print_endline (string_of_board prev);
            print_endline (humanf p1 ^ " " ^ humanf p2);
            raise ERR "invapp_move" (string_of_board board)
            )
      in
        SOME (prev, move)
      end
    | Ins (_,[p1,p2,p3]) => 
      let 
        val (c1,c2,c3) = triple_of_list
          (dict_sort clause_compare (map prog_to_clause [p1,p2,p3]))
        val prev = C1 c1 :: C2 (c2,c3) :: m
        val move = valOf (List.find (is_appsyn target prev) movelg) 
          handle Option => raise ERR "invapp_move" (humanf target)
      in
        SOME (prev, move)
      end
    | _ => raise ERR "invapp_move" "unexpected"
    end
  | C2 (ca,cb) :: m => SOME (C1 ca :: C1 cb :: m, Pair)

fun linearize_aux acc board = case invapp_move board of
    SOME (prev,move) => linearize_aux ((prev,move) :: acc) prev
  | NONE => acc

fun linearize p = linearize_aux [] [C1 (prog_to_clause p)]

(* -------------------------------------------------------------------------
   Merging solutions from searches with different semantic quotient
   ------------------------------------------------------------------------- *)

exception Rewrite of prog;

fun rewrite_merge repd (p as Ins (id,pl)) = 
  let 
    val sem = sem_of_prog p
      handle Option => raise Rewrite p
    val newp = dfind sem (!repd) handle NotFound =>
      let
        val newpl = map (rewrite_merge repd) pl
        val newptemp = Ins (id,newpl)
      in
        repd := dadd sem newptemp (!repd);
        newptemp
      end
  in
    newp
  end

fun rewrite_winl winl =
   let 
     val i = ref 0
     val repd = ref (dempty seq_compare)
     fun f p = 
       let val newp = commute (rewrite_merge repd p) in
         if depend_on_i newp then NONE 
         else if equal_prog (p,newp) then SOME newp
         else if same_sem newp p then (incr i; SOME newp) else NONE
       end
       handle Rewrite x => (log ("rewrite_winl: " ^ humanf x); NONE)
     val r = List.mapPartial f winl
   in
     (r,!i)
   end

fun find_minirep_aux pl =
  let
    val psemtiml = map_assoc (valOf o semtimo_of_prog) pl
      handle Option => raise ERR "find_minirep" "" 
    val repd = ref (dempty seq_compare)
    fun f (p,(sem,tim)) = 
      let
        val winl = find_wins p sem
        val r = spacetime (prog_size p) tim
        val pi = zip_prog p
        val new = (r,pi)
        fun g seq =
          let val old = dfind seq (!repd) in
            if complexity_compare (new,old) = LESS
            then repd := dadd seq new (!repd)
            else ()
          end
          handle NotFound => repd := dadd seq new (!repd)
      in
        app g winl
      end
  in
    app f psemtiml;
    (dlist (!repd))   
  end

fun find_minirep_merge pl = 
  map (unzip_prog o snd o snd) (find_minirep_aux pl)

fun find_minirep_train pl = 
  let 
    val l = find_minirep_aux pl 
    fun f (seq,(_,pi)) = (seq, unzip_prog pi)
  in
    map f l
  end

fun merge_sol pl =
  let
    val _ = log ("all solutions (past + concurrent): " ^ its (length pl))
    val plmini = find_minirep_merge pl
    val pl0 = dict_sort prog_compare_size plmini
    val _ = log ("smallest representants: " ^ its (length pl0))
    val (pl1,n1) = rewrite_winl pl0
    val _ = log ("rewritten solutions: " ^ its (length pl1) ^ " " ^ its n1)
  in
    pl1
  end

(* -------------------------------------------------------------------------
   Merge examples from different solutions
   ------------------------------------------------------------------------- *)

fun regroup_ex bml = 
  let fun f ((board,move),d) = 
    let 
      val oldv = dfind board d handle NotFound => 
        (Vector.tabulate (maxmove, fn _ => 0))
      val movei = index_of_move move
      val newv = Vector.update (oldv, movei, Vector.sub (oldv,movei) + 1)
    in
      dadd board newv d
    end
  in
    foldl f (dempty board_compare) bml
  end

fun pol_of_progl pold board = 
  normalize_proba (map Real.fromInt (vector_to_list (dfind board pold)))

fun create_ex pold (board,_) =
  (term_of_board board, pol_of_progl pold board)

fun create_exl seqprogl =
  let 
    val zerov = Vector.tabulate (maxmove, fn _ => 0.0)
    fun f (seq,p) = 
      let
        val _ = target_glob := seq
        val bml = linearize p
        fun g (board,move) =
           let 
             val movei = index_of_move move
             val newv = Vector.update (zerov, movei, 1.0)
             val newl = vector_to_list newv
           in
             (term_of_board board, newl)
           end
      in
        map g bml    
      end
    val r = map f seqprogl
    val _ = print_endline "examples created"
  in
    r
  end

(* -------------------------------------------------------------------------
   Export examples to C
   ------------------------------------------------------------------------- *)

fun order_subtm tml =
  let
    val d = ref (dempty (cpl_compare Int.compare Term.compare))
    fun traverse tm =
      let
        val (oper,argl) = strip_comb tm
        val nl = map traverse argl
        val n = 1 + sum_int nl
      in
        d := dadd (n, tm) () (!d); n
      end
    val subtml = (app (ignore o traverse) tml; dkeys (!d))
  in
    map snd subtml
  end;

val empty_sobj = rlts (List.tabulate (!dim_glob, fn _ => 9.0))

fun linearize_ex tmobjl =
  let
    val objd = dnew Term.compare tmobjl
    val subtml = order_subtm (map fst tmobjl);
    val indexd = dnew Term.compare (number_snd 0 subtml);
    fun enc_sub x = 
      let val (oper,argl) = strip_comb x in
        dfind oper opernd :: map (fn x => dfind x indexd) argl
      end
    fun enc_obj x = dfind x objd handle NotFound => []
    fun pad_sub l = 
       ilts (l @ List.tabulate (4 - length l, fn _ => 99))
    fun pad_obj l = 
       if null l then empty_sobj else 
       rlts (l @ List.tabulate (!dim_glob - length l, fn _ => 9.0))
  in
    (String.concatWith " " (map (pad_sub o enc_sub) subtml),
     String.concatWith " " (map (pad_obj o enc_obj) subtml),
     length subtml
    )
  end;

fun export_traindata ex =
  let
    val datadir = selfdir ^ "/tnn_in_c/data"
    val _ =
      (
      erase_file (datadir ^ "/arg.txt");
      erase_file (datadir ^ "/dag.txt");
      erase_file (datadir ^ "/obj.txt");
      erase_file (datadir ^ "/size.txt");
      erase_file (datadir ^ "/arity.txt");
      erase_file (datadir ^ "/head.txt")
      )
    val noper = length operlext
    val tmobjll = ex
    val nex = length tmobjll
    val (dagl,objl,sizel) = split_triple (map linearize_ex tmobjll);
    fun find_head tm = if term_eq tm head_poli then maxmove else 0
  in
    writel (datadir ^ "/arg.txt") (map its [noper,nex,!dim_glob]);
    writel (datadir ^ "/dag.txt") dagl;
    writel (datadir ^ "/obj.txt") objl;
    writel (datadir ^ "/size.txt") (map its sizel);
    writel (datadir ^ "/arity.txt") (map (its o arity_of) operlext);
    writel (datadir ^ "/head.txt") (map (its o find_head) operlext)
  end

(* -------------------------------------------------------------------------
   TNN cache
   ------------------------------------------------------------------------- *)

fun fp_emb_either tnn oper newembl = 
  (* if !use_ob andalso !ngen_glob > 0 
  then (!fp_op_glob) oper newembl
  else *) fp_emb tnn oper newembl 
     
   
fun infer_emb_cache tnn tm =
  if is_capped tm 
  then 
    (
    Redblackmap.findKey (!embd,tm) handle NotFound =>
    let
      val (oper,argl) = strip_comb tm
      val embl = map (infer_emb_cache tnn) argl
      val (newargl,newembl) = split embl
      val emb = fp_emb_either tnn oper newembl
      val newtm = list_mk_comb (oper,newargl)
    in
      embd := dadd newtm emb (!embd); 
      (newtm,emb)
    end
    )
  else
    let
      val (oper,argl) = strip_comb tm
      val embl = map (infer_emb_cache tnn) argl
      val (newargl,newembl) = split embl
      val emb = fp_emb_either tnn oper newembl
    in
      (tm,emb)
    end

(* -------------------------------------------------------------------------
   Players
   ------------------------------------------------------------------------- *)

fun rewardf board = 0.0

fun player_uniform tnn board = 
  (0.0, map (fn x => (x,1.0)) (available_movel board))

fun player_wtnn tnn board =
  let 
    val rl = infer_tnn tnn [term_of_board board]
    val pol1 = Vector.fromList (snd (singleton_of_list rl))
    val amovel = available_movel board 
    val pol2 = map (fn x => (x, Vector.sub (pol1, index_of_move x))) amovel
  in
    (rewardf board, pol2)
  end

fun player_wtnn_cache tnn board =
  let
    val _ = if dlength (!embd) > 1000000
            then embd := dempty Term.compare else ()
    val preboarde = snd (infer_emb_cache tnn (term_of_stack board))
    val prepolie = fp_emb_either tnn prepoli [preboarde]
    val pol1 = Vector.fromList (descale_out (fp_emb_either tnn head_poli 
      [prepolie]))
    val amovel = available_movel board 
    val pol2 = map (fn x => (x, Vector.sub (pol1, index_of_move x))) amovel
  in
    (rewardf board, pol2)
  end

(* -------------------------------------------------------------------------
   Search
   ------------------------------------------------------------------------- *)

val player_glob = ref player_wtnn_cache

fun mctsobj tnn = 
  {game = game, mctsparam = mctsparam (), player = !player_glob tnn};

fun tree_size tree = case tree of
    Leaf => 0
  | Node (node,ctreev) => 1 + 
     sum_int (map (tree_size o #3) (vector_to_list ctreev))

(* -------------------------------------------------------------------------
   Parallelization of the search
   ------------------------------------------------------------------------- *)

local open HOLsexp in

fun enc_prog (Ins x) = pair_encode (Integer, list_encode enc_prog) x
val enc_progl = list_encode enc_prog
fun dec_prog t = 
  Option.map Ins (pair_decode (int_decode, list_decode dec_prog) t)
val dec_progl = list_decode dec_prog

end

fun write_result file r = write_data enc_progl file r
fun read_result file = read_data dec_progl file

(* -------------------------------------------------------------------------
   Reinforcement learning loop utils
   ------------------------------------------------------------------------- *)

fun tnn_file ngen = selfdir ^ "/exp/" ^ !expname ^ "/tnn" ^ its ngen
fun sold_file ngen = selfdir ^ "/exp/" ^ !expname ^ "/sold" ^ its ngen

fun get_expdir () = selfdir ^ "/exp/" ^ !expname

fun mk_dirs () = 
  let val expdir = selfdir ^ "/exp/" ^ !expname in
    mkDir_err (selfdir ^ "/exp");
    mkDir_err expdir;
    mkDir_err (selfdir ^ "/parallel_search");
    expdir
  end

fun update_sold ((seq,prog),sold) =
  let 
    val oldprog = dfind seq sold
  in
    if prog_compare_size (prog,oldprog) = LESS 
    then dadd seq prog sold
    else sold
  end
  handle NotFound => dadd seq prog sold

fun write_sold ngen tmpname d = 
  (
  write_result (sold_file ngen ^ tmpname) (elist d);
  write_result (sold_file ngen) (elist d)
  )

fun read_sold ngen = enew prog_compare (read_result (sold_file ngen))

(* -------------------------------------------------------------------------
   training
   ------------------------------------------------------------------------- *)

val ncoretrain = 4

val schedule =
  [
   {ncore = ncoretrain, verbose = true, learning_rate = 0.03,
    batch_size = 16, nepoch = 40},
   {ncore = ncoretrain, verbose = true, learning_rate = 0.02,
    batch_size = 16, nepoch = 10},
   {ncore = ncoretrain, verbose = true, learning_rate = 0.01,
    batch_size = 16, nepoch = 10},
   {ncore = ncoretrain, verbose = true, learning_rate = 0.007,
    batch_size = 16, nepoch = 10},
   {ncore = ncoretrain, verbose = true, learning_rate = 0.005,
    batch_size = 16, nepoch = 10}
  ];



fun read_mat acc sl = case sl of
    [] => (rev acc, [])
  | "A" :: m => (rev acc, sl)
  | x :: m => 
    let 
      val line1 = strl x
      val line2 = last line1 :: butlast line1 
      val line = Vector.fromList line2
    in
      read_mat (line :: acc) m
    end

fun read_cmatl sl = case sl of 
    [] => []
  | "A" :: m => 
    let 
      val (mat1,cont) = read_mat [] m
      val w1 = Vector.fromList mat1 
    in 
      (
      if Vector.length (Vector.sub (w1,0)) = 1
      then 
        [{a = mlNeuralNetwork.idactiv, 
          da = mlNeuralNetwork.didactiv,
          w = w1}]
      else 
        [{a = mlNeuralNetwork.tanh, 
          da = mlNeuralNetwork.dtanh,
          w = w1}]
      )
      :: read_cmatl cont
    end
  | x :: m => raise ERR "read_cmatl" x

fun read_ctnn sl = case sl of
    [] => raise ERR "read_ctnn" ""
  | "START MATRICES" :: m => read_cmatl m
  | a :: m => read_ctnn m

fun read_ctnn_fixed () = 
  let val matl = read_ctnn (readl (selfdir ^ "/tnn_in_c/tnn")) in
    dnew Term.compare (combine (operlext,matl))
  end



fun trainf tmpname =
  let 
    val sold = read_sold (!ngen_glob) 
    val seqpl = find_minirep_train (elist sold)
    val ex = create_exl (shuffle seqpl)
  in
    if !use_mkl then
      let
        val cfile = tnn_file (!ngen_glob) ^ tmpname ^ "_C"
        val sbin = if !use_para then "./tree_para" else "./tree"
        val _= 
          (
          print_endline "exporting training data";
          export_traindata ex;
          print_endline "exporting end";
          OS.Process.sleep (Time.fromReal 1.0);
          cmd_in_dir (selfdir ^ "/tnn_in_c") 
          (sbin ^ " > " ^ cfile)
          )
        val _ = OS.Process.sleep (Time.fromReal 1.0)
        (* val _ = if !use_ob then 
          cmd_in_dir (selfdir ^ "/tnn_in_c") ("sh compile_ob.sh")
          else () *)
        val matl = read_ctnn (readl cfile)
        val tnn = dnew Term.compare (combine (operlext,matl))
      in
        write_tnn (tnn_file (!ngen_glob) ^ tmpname) tnn;
        write_tnn (tnn_file (!ngen_glob)) tnn
      end
    else
    let
      val tnndim = get_tnndim ()
      val (tnn,t) = add_time (train_tnn schedule (random_tnn tnndim)) 
        (part_pct 1.0 (shuffle ex))
    in
      write_tnn (tnn_file (!ngen_glob) ^ tmpname) tnn;
      write_tnn (tnn_file (!ngen_glob)) tnn
    end
  end

fun wrap_trainf ngen tmpname =
  let
    val scriptfile = !buildheap_dir ^ "/train.sml" 
    val makefile = !buildheap_dir ^ "/Holmakefile"
  in
    writel makefile ["INCLUDES = " ^ selfdir]; 
    writel scriptfile
    ["open mcts;",
     "expname := " ^ mlquote (!expname) ^ ";",
     "smlExecScripts.buildheap_dir := " ^ mlquote (!buildheap_dir) ^ ";",
     "mcts.ngen_glob := " ^ its (!ngen_glob) ^ ";",
     "mcts.dim_glob := " ^ its (!dim_glob) ^ ";",
     "mcts.use_mkl := " ^ bts (!use_mkl) ^ ";",
     "mcts.use_para := " ^ bts (!use_para) ^ ";",
     "mcts.use_ob := " ^ bts (!use_ob) ^ ";",
     "bloom.init_od ();",
     "trainf " ^ mlquote tmpname];
     exec_script scriptfile
  end

(* -------------------------------------------------------------------------
   Minimizing solutions using an initialized minid
   ------------------------------------------------------------------------- *)

fun minimize p =
  let 
    val sem = sem_of_prog p 
      handle Option => raise ERR "minimize" ""
    val (Ins (id,pl)) = (unzip_prog o snd o (dfind sem)) (!minid) 
      handle NotFound => p
  in
    Ins (id, map minimize pl)
  end

fun minimize_winl winl =
   let 
     val i = ref 0
     fun f p = 
       if not (is_executable p) then raise ERR "minimize_winl" (humanf p) else
       let val newp = commute (minimize p) in
         if equal_prog (p,newp) orelse depend_on_i newp then p
         else if same_sem newp p then (incr i; newp) else p
       end
     val r = map f winl
   in
     (r,!i)
   end

(* -------------------------------------------------------------------------
   Initialized dictionaries with previous solutions and their subterms
   ------------------------------------------------------------------------- *)


fun zerob b =
  let 
    val n = BoolArray.length b
    fun loop i = 
      if i >= n then () else 
      (BoolArray.update (b,i,false); loop (i+1))
  in
    loop 0
  end

val use_cache = ref false

fun init_dicts pl =
  let
    val _ = if not (!use_cache) then () else 
      let val plsol = List.mapPartial check_simple_target pl in
        if null plsol then () else 
        raise ResultP (hd (dict_sort prog_compare_size plsol))
      end
    val pil = map zip_prog pl
    val psemtiml = map_assoc (valOf o semtimo_of_prog) pl
      handle Option => raise ERR "init_dicts" ""  
    val seml = map (fst o snd) psemtiml
    fun g (p,(sem,tim)) = 
      (sem, (spacetime (prog_size p) tim, zip_prog p))
  in
    progd := eempty progi_compare;
    notprogd := eempty progi_compare;
    if !use_semb then (semb := BoolArray.tabulate (bmod, fn _ => false);
                       zerob (!semb)) else semd := eempty seq_compare;
    embd := dempty Term.compare;
    seqwind := eempty seq_compare;
    progwind := eempty progi_compare;
    initd := enew progi_compare pil;
    minid := dnew seq_compare (map g psemtiml)
  end

(* -------------------------------------------------------------------------
   Main search function
   ------------------------------------------------------------------------- *)

val wnoise_flag = ref false

fun search tnn coreid =
  let
    val _ = print_endline "initialization"
    val _ = coreid_glob := coreid
    val _ = player_glob := player_wtnn_cache
    val sold = if !ngen_glob <= 0
               then eempty prog_compare
               else read_sold (!ngen_glob - 1)
    val _ = noise_flag := false
    val _ = if !coreid_glob mod 2 = 0 
            then (noise_flag := true; noise_coeff_glob := 0.1) else ()
    val (targetseq,seqnamel) = random_elem (dlist (!odname_glob))
    val _ = target_glob := targetseq
    val _ = print_endline 
      ("target: " ^ String.concatWith "-" seqnamel ^ ": " ^ 
        string_of_seq (!target_glob));
    val _ = init_dicts (elist sold)
    val _ = print_endline "start search"
    val _ = in_search := true
    val _ = avoid_lose := true
    val tree = starting_tree (mctsobj tnn) []
    val (newtree,t) = add_time (mcts (mctsobj tnn)) tree
    val _ = print_endline "end search"
    val n = tree_size newtree
    val _ = avoid_lose := false
    val _ = in_search := false
    val winl = map unzip_prog (elist (!progwind))
    val (minwinl,minn) = minimize_winl winl
    val _ = if emem (!target_glob) (!seqwind)
      then print_endline "target acquired"
      else print_endline "target missed"
  in
    print_endline ("search time: "  ^ rts_round 2 t ^ " seconds");
    print_endline ("tree_size: " ^ its n);
    print_endline ("prog sol: " ^ its (elength (!progwind)));
    print_endline ("prog mini: " ^ its minn);
    print_endline ("seq sol: " ^ its (elength (!seqwind)));
    minwinl
  end

val parspec : (tnn,int,prog list) extspec =
  {
  self_dir = selfdir,
  self = "mcts.parspec",
  parallel_dir = selfdir ^ "/parallel_search",
  reflect_globals = (fn () => "(" ^
    String.concatWith "; "
    ["bloom.init_od ()",
     "smlExecScripts.buildheap_dir := " ^ mlquote (!buildheap_dir), 
     "mcts.expname := " ^ mlquote (!expname),
     "mcts.ngen_glob := " ^ its (!ngen_glob),
     "mcts.coreid_glob := " ^ its (!coreid_glob),
     "mcts.dim_glob := " ^ its (!dim_glob),
     "mcts.time_opt := " ^ string_of_timeo (),
     "mcts.use_ob := " ^ bts (!use_ob)
   ] 
    ^ ")"),
  function = search,
  write_param = write_tnn,
  read_param = read_tnn,
  write_arg = let fun f file arg = writel file [its arg] in f end,
  read_arg = let fun f file = string_to_int (hd (readl file)) in f end,
  write_result = write_result,
  read_result = read_result
  }

fun search_target_aux (tnn,sold) tim target =
  let
    val _ = simple_search := true
    val _ = time_opt := SOME tim;
    val _ = player_glob := player_wtnn_cache
    val _ = simple_target := target
    val _ = target_glob := target
    val _ = debug "init dicts"
    val _ = init_dicts (elist sold)
    val _ = in_search := true
    val _ = avoid_lose := true
    val tree = starting_tree (mctsobj tnn) []
    val _ = debug "start search"
    val (newtree,t) = add_time (mcts (mctsobj tnn)) tree
    val _ = avoid_lose := false
    val _ = in_search := false
  in
    NONE
  end
  handle ResultP p => (debug (humanf p); SOME (minimize p))

fun parsearch_target tim target =
  let 
    val tnn = read_tnn (selfdir ^ "/main_tnn")
    val sold = enew prog_compare (read_result (selfdir ^ "/main_sold"))
    val (p,t) = add_time (search_target_aux (tnn,sold) tim) target 
  in
    (true,humanf (valOf p),t) handle Option => (false, "", t)
  end

val partargetspec : (real, seq, bool * string * real) extspec =
  {
  self_dir = selfdir,
  self = "mcts.partargetspec",
  parallel_dir = selfdir ^ "/parallel_search",
  reflect_globals = (fn () => "(" ^
    String.concatWith "; "
    ["smlExecScripts.buildheap_dir := " ^ mlquote (!buildheap_dir),
     "bloom.init_od ()",
     "mcts.use_semb := " ^ bts (!use_semb),
     "mcts.use_ob := " ^ bts (!use_ob)] 
    ^ ")"),
  function = parsearch_target,
  write_param = let fun f file param = writel file [rts param] in f end,
  read_param =  let fun f file = string_to_real (hd (readl file)) in f end,
  write_arg = let fun f file arg = writel file [ilts arg] in f end,
  read_arg = let fun f file = stil (hd (readl file)) in f end,
  write_result = let fun f file (b,s,t) = writel file [bts b, s, rts t] 
     in f end,
  read_result = let fun f file = 
       let val (s1,s2,s3) = triple_of_list (readl file) in 
         (string_to_bool s1, s2, string_to_real s3)
       end
     in f end
  }

fun parsearch_targetl ncore tim targetl =
  (
  buildheap_options := "--maxheap 10000";
  parmap_queue_extern ncore partargetspec tim targetl
  )

(* -------------------------------------------------------------------------
   Statistics
   ------------------------------------------------------------------------- *)

fun human_progseq p = 
  let 
    val seq = seq_of_prog p
    val seql = find_wins p seq
    val _ = if null seql 
      then log ("Error: human_progseq 1: " ^ (humanf p)) else ()
    fun f x = String.concatWith "-" (dfind x (!odname_glob)) ^ ": " ^ 
      String.concatWith " " (map its x)
  in
    humanf p ^ "\n" ^ String.concatWith "\n" (map f seql)
  end
  handle Interrupt => raise Interrupt | _ => 
    (log ("Error: human_progseq 2: " ^ (humanf p)); "")

fun human_progfreq (prog,freq) = its freq ^ ": " ^ humanf prog;

fun compute_freq f sol1 =
  let val freql = dlist 
    (count_dict (dempty prog_compare) (List.concat (map f sol1)))
  in
    dict_sort compare_imax freql
  end

fun stats_sol prefix sol =
  let
    val solsort = dict_sort prog_compare_size sol
    val freql1 = compute_freq all_subprog sol
    val freql2 = compute_freq under_lambda sol
  in
    polynorm_flag := false;
    writel (prefix ^ "prog") (map human_progseq solsort);
    writel (prefix ^ "freq") (map human_progfreq freql1);  
    writel (prefix ^ "freqlam") (map human_progfreq freql2);
    polynorm_flag := true;
    writel (prefix ^ "prog_poly") (map human_progseq solsort);
    writel (prefix ^ "freq_poly") (map human_progfreq freql1);
    writel (prefix ^ "freqlam_poly") (map human_progfreq freql2)
  end

fun stats_ngen dir ngen =
  let 
    val solprev = 
      if ngen = 0 then [] else #read_result parspec (sold_file (ngen - 1))
    val solnew = #read_result parspec (sold_file ngen)
    val prevd = enew prog_compare solprev
    val soldiff = filter (fn x => not (emem x prevd)) solnew
  in
    stats_sol (dir ^ "/full_") solnew;
    stats_sol (dir ^ "/diff_") soldiff
  end
  handle Interrupt => raise Interrupt | _ => log ("Error: stats_ngen")

(* -------------------------------------------------------------------------
   Reinforcement learning loop
   ------------------------------------------------------------------------- *)

fun rl_search_only tmpname ngen =
  let 
    val expdir = mk_dirs ()
    val _ = log ("Search " ^ its ngen)
    val _ = buildheap_dir := expdir ^ "/search" ^ its ngen ^ tmpname;
    val _ = mkDir_err (!buildheap_dir)
    val _ = ngen_glob := ngen
    val _ = buildheap_options := "--maxheap 10000"
    val tnn = if ngen <= 0 
              then random_tnn (get_tnndim ())
              else read_tnn (tnn_file (ngen - 1))
    val sold = if ngen <= 0
               then eempty prog_compare
               else read_sold (ngen - 1)
    val (progll,t) = add_time
      (parmap_queue_extern ncore parspec tnn) 
         (List.tabulate (ntarget,I))
    val _ = log ("search time: " ^ rts_round 6 t)
    val _ = log ("solutions for each core:")
    val _ = log (String.concatWith " " (map (its o length) progll))
    val seqd = enew seq_compare (mapfilter seq_of_prog (elist sold))
    fun is_new p = not (emem (seq_of_prog p) seqd) 
      handle Interrupt => raise Interrupt | _ => false
    val newprogll = map (filter is_new) progll
    val _ = log ("new solutions for each core:")
    val _ = log (String.concatWith " " (map (its o length) newprogll))
    val progl = mk_fast_set prog_compare (
      List.concat progll @ elist sold)
    val newsold = enew prog_compare (merge_sol progl)
  in
    write_sold ngen tmpname newsold;
    stats_ngen (!buildheap_dir) ngen
  end

and rl_search tmpname ngen = 
  (rl_search_only tmpname ngen; rl_train tmpname ngen)

and rl_train_only tmpname ngen =
  let
    val expdir = mk_dirs ()
    val _ = log ("Train " ^ its ngen)
    val _ = buildheap_dir := expdir ^ "/train" ^ its ngen ^ tmpname
    val _ = mkDir_err (!buildheap_dir)
    val _ = buildheap_options := "--maxheap 100000"
    val _ = ngen_glob := ngen
    val (_,t) = add_time (wrap_trainf ngen) tmpname 
    val _ = log ("train time: " ^ rts_round 6 t)
  in
    () 
  end

and rl_train tmpname ngen = 
  (rl_train_only tmpname ngen; rl_search tmpname (ngen + 1))

end (* struct *)


(*  
  -------------------------------------------------------------------------
  Train oeis-synthesis
  ------------------------------------------------------------------------- 

load "mcts"; open mcts;
expname := "run102";
time_opt := SOME 600.0;
(* use_mkl := true; *)
bloom.init_od ();
rl_search "_init6" 0;

*)