File size: 20,341 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
structure kernel :> kernel =
struct

open HolKernel Abbrev boolLib aiLib dir;
val ERR = mk_HOL_ERR "kernel";
               
val selfdir = dir.selfdir 

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

val maxinput = ref 16
val simple_search = ref false

(* -------------------------------------------------------------------------
   Types
   ------------------------------------------------------------------------- *)

type oper2 = int * int -> int
type oper3 = int * int * int -> int
type exec = int * int -> int
type seq = int list
val seq_compare = list_compare Int.compare
fun string_of_seq il = String.concatWith " " (map its il)

(* -------------------------------------------------------------------------
   Composing semantics and executables
   ------------------------------------------------------------------------- *)

val error = valOf (Int.maxInt)

(* -------------------------------------------------------------------------
   Program
   ------------------------------------------------------------------------- *)

type id = int
datatype prog = Ins of (id * prog list);

fun prog_compare (Ins(s1,pl1),Ins(s2,pl2)) =
  cpl_compare Int.compare (list_compare prog_compare) ((s1,pl1),(s2,pl2))

fun equal_prog (a,b) = (prog_compare (a,b) = EQUAL)

fun prog_size (Ins(s,pl)) = 
  if null pl
  then 1 + sum_int (map prog_size pl) 
  else sum_int (map prog_size pl) 

fun all_subprog (p as Ins (_,pl)) = p :: List.concat (map all_subprog pl);

(* -------------------------------------------------------------------------
   Instructions
   ------------------------------------------------------------------------- *)

val zero_id = 0
val one_id = 1
val two_id = 2
val addi_id = 3
val diff_id = 4
val mult_id = 5
val divi_id = 6
val modu_id = 7
val cond_id = 8
val loop_id = 9
val var_id = 10
val ind_id = 11
val compr_id = 12

val alpha3 = rpt_fun_type 3 alpha
val alpha4 = rpt_fun_type 4 alpha
val operv = Vector.fromList 
  [
   mk_var ("zero",alpha),
   mk_var ("one",alpha),
   mk_var ("two",alpha),
   mk_var ("addi",alpha3),
   mk_var ("diff",alpha3),
   mk_var ("mult",alpha3),
   mk_var ("divi",alpha3),
   mk_var ("modu",alpha3),
   mk_var ("cond",alpha4),
   mk_var ("loop",alpha4),
   mk_var ("var",alpha),
   mk_var ("ind",alpha),
   mk_var ("compr",alpha3)
  ]

val maxoper = Vector.length operv

val operav = Vector.map arity_of operv

fun name_of_oper i = fst (dest_var (Vector.sub (operv,i)))

(* time limit per instruction *)
val timelimit = ref (16*16*16*16+1);
val counter = ref 0;
fun start f x = (counter := 0; f x)

(* nullary *)
val zero_f = (fn _ => 0)
val one_f = (fn _ => 1)
val two_f = (fn _ => 2)
val var_f = (fn (a:int,b:int) => a)
val ind_f = (fn (a:int,b:int) => b)

(* first-order *)
val addi_f = op +
val diff_f = op -
val mult_f = op *
val divi_f = op div
val modu_f = op mod
fun cond_f (a,b,c) = if a <= 0 then b else c

(* higher-order operator *)
fun loop_f_aux i f n x = 
   (
   incr counter;
   if !counter > !timelimit then raise Div
   else if n <= 0 then x 
   else loop_f_aux (i+1) f (n-1) (f (x,i))
   )

fun loop_f f (n,x) =
  if n > !timelimit then raise Div else loop_f_aux 1 f n x

fun compr_f_aux x f n0 n =
   (
   if x > (n0+1)*(n0+1)*256 then raise Div else ();
   incr counter;
   if !counter > !timelimit then raise Div
   else if f (x,0) <= 0 then 
     (if n0 >= n then x else compr_f_aux (x+1) f (n0+1) n)
   else compr_f_aux (x+1) f n0 n
   )

fun compr_f f n =
  if n > !timelimit orelse n < 0 then raise Div else compr_f_aux 0 f 0 n

(* regroup by arity *)
val nullaryl =
 [(zero_id,zero_f),(one_id,one_f),(two_id,two_f),(var_id,var_f),(ind_id,ind_f)]

val nullaryidl = map fst nullaryl
fun is_nullary id = mem id nullaryidl
fun find_nullaryf id = assoc id nullaryl
val nullaryv = Vector.tabulate (maxoper, fn i => 
  if can find_nullaryf i then find_nullaryf i else zero_f);

val binaryl = 
  [(addi_id,addi_f),(diff_id,diff_f),(mult_id,mult_f),(divi_id,divi_f),
   (modu_id,modu_f)]
val binaryidl = map fst binaryl
fun is_binary id = id >= addi_id andalso id <= modu_id
fun find_binaryf id = assoc id binaryl
val binaryv = Vector.tabulate (maxoper, fn i => 
  if can find_binaryf i then find_binaryf i else addi_f);

fun is_comm id = mem id [addi_id, mult_id]
val binaryidl_nocomm = filter (fn x => not (is_comm x)) binaryidl 

(* -------------------------------------------------------------------------
   Compressed programs
   ------------------------------------------------------------------------- *)

fun depend_on_i p = case p of 
    Ins (id,[]) => id = ind_id 
  | Ins (9,[p1,p2,p3]) => depend_on_i p2 orelse depend_on_i p3
  | Ins (_,pl) => exists depend_on_i pl  

fun under_lambda p = case p of
    Ins (id,[]) => []
  | Ins (9,[p1,p2,p3]) => p1 :: List.concat (map under_lambda [p1,p2,p3])
  | Ins (_,pl) => List.concat (map under_lambda pl)

fun has_lambdai p = case p of
    Ins (id,[]) => false
  | Ins (9,[p1,p2,p3]) => depend_on_i p1 
     orelse has_lambdai p2 orelse has_lambdai p3
  | Ins (_,pl) => exists has_lambdai pl

fun suc_prog n p = if n <= 0 then p else 
  Ins (addi_id, [suc_prog (n-1) p, Ins (one_id,[])])

fun shift_prog n p = case p of 
    Ins (id,[]) => if id = var_id then suc_prog n p else p 
  | Ins (9,[p1,p2,p3]) => Ins (9,[p1, shift_prog n p2, shift_prog n p3])
  | Ins (12,[p1,p2]) => Ins (12,[p1, shift_prog n p2])
  | Ins (id,pl) => Ins (id, map (shift_prog n) pl)

(* -------------------------------------------------------------------------
   Compressed programs
   ------------------------------------------------------------------------- *)

type progi = Arbint.int

val progi_compare = Arbint.compare
fun equal_progi (a,b) = (progi_compare (a,b) = EQUAL)

fun suc x = x + 1
fun pred x = x - 1

local open Arbint in

val arbmaxoper = fromInt (suc maxoper)

fun zip_prog prog =
  let
    fun polish (Ins (id,pl)) = fromInt (suc id) :: List.concat (map polish pl)
    fun loop r il = case il of 
        [] => r 
      | a :: m => loop (r * arbmaxoper + a) m 
  in
    loop zero (rev (polish prog))
  end

fun unzip_progl arbi =
  let 
    val (q,r) = Arbint.divmod (arbi, arbmaxoper)
    val id = pred (toInt r)
    val a = Vector.sub (operav,id)
    val pl = if q = zero then [] else unzip_progl q
    val (pla,plb) = part_n a pl
  in
    Ins (id,pla) :: plb
  end

fun unzip_prog arbi = singleton_of_list (unzip_progl arbi)

val hseq_modw = fromInt 2115908027

fun pi_to_hseq pi =
  let val (q,r) = Arbint.divmod (pi, hseq_modw) in
    if q = zero 
    then [toInt r]
    else toInt r :: pi_to_hseq q
  end

end (* local *)

(* -------------------------------------------------------------------------
   Possible inputs
   ------------------------------------------------------------------------- *)

val entryl = List.concat [
  List.tabulate (16,fn x => (x,0)),
  List.tabulate (8,fn x => (x,1)),
  List.tabulate (4,fn x => (x,2)),
  List.tabulate (2,fn x => (x,3)),
  List.tabulate (1,fn x => (x,4))
  ];

val entryl16 = first_n 16 entryl

(* -------------------------------------------------------------------------
   Execute a program on some input
   ------------------------------------------------------------------------- *)

fun compose1 f f1 x = f (f1 x)
fun compose2 f f1 f2 x = f (f1 x, f2 x)
fun compose3 f f1 f2 f3 x = f (f1 x, f2 x, f3 x)

fun mk_exec_aux prog = case prog of
    Ins (id,[]) => Vector.sub (nullaryv,id) 
  | Ins (12,[p1,p2]) =>
    compose1 (compr_f (mk_exec_aux p1)) (mk_exec_aux p2)
  | Ins (id,[p1,p2]) => 
    compose2 (Vector.sub (binaryv,id)) (mk_exec_aux p1) (mk_exec_aux p2)
  | Ins (8,[p1,p2,p3]) => 
    compose3 cond_f (mk_exec_aux p1) (mk_exec_aux p2) (mk_exec_aux p3)
  | Ins (9,[p1,p2,p3]) =>
    compose2 (loop_f (mk_exec_aux p1)) (mk_exec_aux p2) (mk_exec_aux p3)
  | _ => raise ERR "mk_exec" ""  

fun mk_exec p =
  let val exec = start (mk_exec_aux p) in
    (fn x => ((exec x,!counter) handle Overflow => (error,!counter)))
  end 

fun not_semdep_on_i e31 =
  let
    val (e16,e15) = part_n 16 e31
    val (e8,e7) = part_n 8 e15
    val (e4,e3) = part_n 4 e7
    val (e2,e1) = part_n 2 e3
  in
    seq_compare (first_n 8 e16, e8) = EQUAL andalso
    seq_compare (first_n 4 e16, e4) = EQUAL andalso
    seq_compare (first_n 2 e16, e2) = EQUAL andalso
    seq_compare (first_n 1 e16, e1) = EQUAL
  end

fun semtimo_of_prog p =
  let val f = mk_exec p in
    if not (depend_on_i p) 
    then 
      let 
        val e16 = map f entryl16
        val r = 
          (e16 @ first_n 8 e16 @ first_n 4 e16 @ first_n 2 e16 @ first_n 1 e16)
        val (sem,timl) = split r
        val tim = 31 + sum_int timl
      in
        SOME (sem,tim)
      end
      handle Div => NONE
    else
      let 
        val r = map f entryl 
        val (sem,timl) = split r
        val tim = 31 + sum_int timl
      in
        if not_semdep_on_i sem then NONE else SOME (sem,tim)
      end
      handle Div => NONE
  end

fun semo_of_prog p = Option.map fst (semtimo_of_prog p)

fun same_sem p1 p2 = case (semo_of_prog p1, semo_of_prog p2) of
     (SOME l1, SOME l2) => seq_compare (l1,l2) = EQUAL
   | _ => false

val sem_of_prog = valOf o semo_of_prog

fun seq_of_prog p = first_n 16 (sem_of_prog p)

fun is_executable x = isSome (semo_of_prog x)

(* -------------------------------------------------------------------------
   Applications of an instruction syntactically from compressed programs
   ------------------------------------------------------------------------- *)

fun papp_nullop id = Ins (id,[])
fun papp_binop id (p1,p2) = Ins (id, [p1,p2])
fun papp_ternop id (p1,p2,p3) = Ins (id,[p1,p2,p3])

(* -------------------------------------------------------------------------
   Human readable output
   ------------------------------------------------------------------------- *)

val constnorm_flag = ref false
val polynorm_flag = ref true
val imperative_flag = ref false

fun itsm i = if i < 0 then "-" ^ its (~i) else its i

fun mk_xn vn = if vn = ~1 then "x" else "X"
fun mk_in vn = if vn = ~1 then "i" else "I"

fun constnorm prog = case prog of
    Ins (0,[]) => 0
  | Ins (1,[]) => 1
  | Ins (2,[]) => 2
  | Ins (3,[p1,p2]) => addi_f (constnorm p1,constnorm p2)
  | Ins (4,[p1,p2]) => diff_f (constnorm p1,constnorm p2)
  | Ins (5,[p1,p2]) => mult_f (constnorm p1,constnorm p2)
  | Ins (6,[p1,p2]) => divi_f (constnorm p1,constnorm p2)
  | Ins (7,[p1,p2]) => modu_f (constnorm p1,constnorm p2)
  | _ => raise ERR "constnorm" ""

fun polyaddi l1 l2 = case (l1,l2) of
    (l1,[]) => l1
  | ([],l2) => l2
  | (a1 :: m1, a2 :: m2) => addi_f (a1,a2) :: polyaddi m1 m2

fun polydiff l1 l2 = case (l1,l2) of
    (l1,[]) => l1
  | ([],l2) => polydiff (List.tabulate (length l2, fn _ => 0)) l2
  | (a1 :: m1, a2 :: m2) => diff_f (a1,a2) :: polydiff m1 m2

fun polymult l1 l2 = case (l1,l2) of 
    (l1,[]) => raise ERR "polymult" ""
  | ([],l2) => raise ERR "polymult" ""
  | (l1,[a]) => map (fn x => mult_f (a,x)) l1
  | ([a],l2) => map (fn x => mult_f (a,x)) l2
  | (a1 :: m1, l2) => 
    polyaddi (map (fn x => mult_f (a1,x)) l2)  (polymult m1 (0 :: l2))

fun polydivi l1 l2 = case (l1,l2) of
    ([a],[b]) => [divi_f (a,b)]
  | _ => raise ERR "polydivi" ""
fun polymodu l1 l2 = case (l1,l2) of
    ([a],[b]) => [modu_f (a,b)]
  | _ => raise ERR "polymodu" ""

fun polynorm prog = case prog of
    Ins (0,[]) => [0]
  | Ins (1,[]) => [1]
  | Ins (2,[]) => [2]
  | Ins (3,[p1,p2]) => polyaddi (polynorm p1) (polynorm p2)
  | Ins (4,[p1,p2]) => polydiff (polynorm p1) (polynorm p2)
  | Ins (5,[p1,p2]) => polymult (polynorm p1) (polynorm p2)
  | Ins (6,[p1,p2]) => polydivi (polynorm p1) (polynorm p2)
  | Ins (7,[p1,p2]) => polymodu (polynorm p1) (polynorm p2)
  | Ins (10,[]) => [0,1]
  | _ => raise ERR "polynorm" ""

fun string_of_mono vn (a,i) = 
  (if a = 1 andalso i <> 0 then "" else 
   if a = ~1 andalso i <> 0 then "-" else itsm a) ^
  (if (a = 1 andalso i <> 0) orelse
      (a = ~1 andalso i <> 0) orelse
      (i = 0)
   then "" else "*") ^
  (if i = 0 then "" else if i = 1 then mk_xn vn else mk_xn vn ^ 
     (if !imperative_flag then "**" else "^") ^ its i)

fun string_of_poly vn l = 
  let val l1 = filter (fn x => fst x <> 0) (number_snd 0 l) in
    if null l1 then "0" else
    let val s = String.concatWith " + " (map (string_of_mono vn) (rev l1)) in
      if length l1 = 1 then s else "(" ^ s ^ ")"
    end
  end

fun polyaddil l = case l of 
    [] => [] 
  | a :: m => polyaddi a (polyaddil m)

fun polymultl l = case l of 
    [] => raise ERR "polymultl" ""
  | [a] => a 
  | a :: m => polymult a (polymultl m)

fun rm_par s = 
  if String.size s = 0 then s else
  if String.sub (s,0) = #"(" andalso String.sub (s,String.size s - 1) =  #")" 
  then String.substring (s,1,String.size s - 2)
  else s;

(* regroup polynoms in repeated addition/substraction and
   multiplications *)

fun strip_addi p = case p of
    Ins (3,[p1,p2]) => 
    let 
      val (a1,b1) = strip_addi p1 
      val (a2,b2) = strip_addi p2
    in
      (a1 @ a2, b1 @ b2)
    end 
  | Ins (4,[p1,p2]) =>
    let 
      val (a1,b1) = strip_addi p1
      val (a2,b2) = strip_addi p2
    in
      (a1 @ b2, a2 @ b1)
    end 
  | _ => ([p],[])

fun strip_mult p = case p of
    Ins (5,[p1,p2]) => strip_mult p1 @ strip_mult p2 
  | _ => [p]

fun is_loop (Ins(id,pl)) = (mem id [9,12])

val ctxt = ref []
val funn = ref 0
fun incrs s = s ^ " = " ^ s ^ " + 1";
fun decrs s = s ^ " = " ^ s ^ " - 1";

fun human vn prog =
  if !constnorm_flag andalso can constnorm prog then its (constnorm prog) else
  case prog of
    Ins (3,[p1,p2]) => 
    if !polynorm_flag 
    then reg_add vn prog
    else "(" ^ human vn p1 ^ " + " ^ human vn p2 ^ ")"   
  | Ins (4,[p1,p2]) =>  
    if !polynorm_flag 
    then reg_add vn prog
    else "(" ^ human vn p1 ^ " - " ^ human vn p2 ^ ")"   
  | Ins (5,[p1,p2]) =>
    if !polynorm_flag 
    then reg_mult vn prog
    else "(" ^ human vn p1 ^ " * " ^ human vn p2 ^ ")"
  | Ins (6,[p1,p2]) => 
    if !imperative_flag
    then "(" ^ human vn p1 ^ " // " ^ human vn p2 ^ ")"
    else "(" ^ human vn p1 ^ " div " ^ human vn p2 ^ ")"
  | Ins (7,[p1,p2]) => 
    if !imperative_flag
    then "(" ^ human vn p1 ^ " % " ^ human vn p2 ^ ")"
    else "(" ^ human vn p1 ^ " mod " ^ human vn p2 ^ ")"
  | Ins (8,[p1,p2,p3]) => 
     if !imperative_flag
     then "(" ^ rm_par (human vn p2) ^ " if " ^ 
                rm_par (human vn p1) ^ " <= 0 else " ^
                rm_par (human vn p3) ^ ")"
     else "(if " ^ 
       rm_par (human vn p1) ^ " <= 0 then " ^ rm_par (human vn p2)  ^ " else " ^ 
       rm_par (human vn p3) ^ ")"
  | Ins (9,[p1,p2,p3]) => 
      let
        val wn = (!funn)
        val _ = incr funn
        val s1 = rm_par (human (~1) p1)
        val s2 = human wn p2 ^ " + 1"
        val s3 = rm_par (human wn p3)
        val fs = "f" ^ its wn
        val fprev1 = if depend_on_i prog 
          then fs ^ "(" ^ mk_xn wn ^ "," ^ mk_in wn ^ ")"
          else fs ^ "(" ^ mk_xn wn ^ ")"
        val fprev2 = if depend_on_i prog 
          then fs ^ "(" ^ mk_xn vn ^ "," ^ mk_in vn ^ ")"
          else fs ^ "(" ^ mk_xn vn ^ ")"
        val fs_head = "def " ^ fprev1 ^ ":"
      in
        if !imperative_flag then
          let val cs = 
            [fs_head,
             "  x = " ^ s3,
             "  " ^ "for i in range (1," ^ s2 ^ "):",
             "    x = " ^ s1,
             "  return x", ""]
          in
            ctxt := !ctxt @ cs; fprev2
          end
        else
          "loop(\\(x,i)." ^ s1  ^ ", " ^ 
          rm_par (human (~1) p2)  ^ ", " ^ rm_par (human (~1) p3) ^ ")"
      end
  | Ins (10,[]) => mk_xn vn
  | Ins (11,[]) => mk_in vn
  | Ins (12,[p1,p2]) => 
    let
      val wn = (!funn)
      val _ = incr funn
      val s1 = rm_par (human (~1) p1)
      val s2 = rm_par (human wn p2)
      val fs = "f" ^ its wn
      val fprev1 = if depend_on_i prog 
        then fs ^ "(" ^ mk_xn wn ^ "," ^ mk_in wn ^ ")"
        else fs ^ "(" ^ mk_xn wn ^ ")"
      val fprev2 = if depend_on_i prog 
        then fs ^ "(" ^ mk_xn vn ^ "," ^ mk_in vn ^ ")"
        else fs ^ "(" ^ mk_xn vn ^ ")"
      val fs_head = "def " ^ fprev1 ^ ":"
    in
      if !imperative_flag then
        let val cs = [fs_head,
          "  x,i = 0,0",
          "  while i <= " ^ s2 ^ ":",
          "    if " ^ s1 ^ " <= 0:",
          "      i = i + 1",
          "    x = x + 1",
          "  return x - 1", ""]
        in
          ctxt := !ctxt @ cs; fprev2
        end
      else
        "compr(\\x." ^ s1 ^ ", " ^ rm_par (human (~1) p2) ^ ")" 
    end
  | Ins (s,[]) => its s
  | Ins (s,l) => "(" ^ its s ^ " " ^ 
      String.concatWith " " (map (human vn) l) ^ ")"

and reg_add vn p = 
  let 
    val (pl1,pl2) = strip_addi p
    val (pl1a,pl1b) = partition (can polynorm) pl1
    val (pl2a,pl2b) = partition (can polynorm) pl2
    val pl1n = map polynorm pl1a
    val pl2n = map polynorm pl2a
    fun human_addl pl = case pl of 
         [] => raise ERR "reg_add" ""
       | [a] => human vn a 
       | _ => "(" ^ String.concatWith " + " (map (human vn) pl) ^ ")"
    val polyo = if null pl1n andalso null pl2n then NONE else 
      SOME (polydiff (polyaddil pl1n) (polyaddil pl2n)) 
  in
    case (polyo,pl1b,pl2b) of
      (NONE,[],[]) => raise ERR "reg_add 2" ""
    | (NONE,_,[]) => human_addl pl1b
    | (NONE,[],_) => "-" ^ human_addl pl2b
    | (NONE,_,_) => "(" ^ human_addl pl1b ^ " - " ^ human_addl pl2b ^ ")"
    | (SOME poly,[],[]) => string_of_poly vn poly 
    | (SOME poly,_,[]) => "(" ^ string_of_poly vn poly ^ 
      " + " ^ human_addl pl1b ^ ")"
    | (SOME poly,[],_) => "(" ^ string_of_poly vn poly ^ 
      " - " ^ human_addl pl2b ^ ")"
    | (SOME poly,_,_) => "(" ^ string_of_poly vn poly ^ 
      " + " ^ human_addl pl1b ^ " - " ^ human_addl pl2b ^ ")"
  end

and reg_mult vn p =
  let 
    val pl = strip_mult p 
    val (pla,plb) = partition (can polynorm) pl
    fun human_multl pl = case pl of 
         [] => raise ERR "reg_mult" ""
       | [a] => human vn a 
       | _ => "(" ^ String.concatWith " * " (map (human vn) pl) ^ ")"
    val polyo = if null pla then NONE else 
      SOME (polymultl (map polynorm pla))  
  in
    case (polyo,plb) of 
      (NONE,[]) => raise ERR "reg_mult 2" ""
    | (NONE,_) => human_multl plb
    | (SOME poly,[]) => string_of_poly vn poly
    | (SOME poly,_) => "(" ^ string_of_poly vn poly ^ " * " ^ 
      human_multl plb ^ ")"
  end

fun humanf p =
  let 
    val _ = imperative_flag := false
    val s = human (~1) p
    val _ = imperative_flag := false
  in s end

fun humani ntop p =
  let 
    val _ = imperative_flag := true
    val _ = ctxt := [] 
    val _ = funn := 0
    val ps = 
      if not (is_loop p) then
      let
        val wn = !funn
        val _ = incr funn
        val fs = "f" ^ its wn
        val xs = mk_xn wn
        val s = rm_par (human wn p)
        val head = "def " ^ fs ^ "(" ^ xs ^ "):\n  return " ^ s ^ "\n"
        val test = "for x in range(" ^ its ntop ^ 
          "):\n  print (" ^ fs ^ "(x))"    
        val ps' = String.concatWith "\n" (!ctxt @ [head,test])
      in ps' end
      else
      let
        val wn = !funn
        val fs = "f" ^ its wn
        val s = rm_par (human wn p)
        val test = "for x in range(" ^ its ntop ^ 
          "):\n  print (" ^ fs ^ "(x))"    
        val ps' = String.concatWith "\n" (!ctxt @ [test])
      in ps' end
    val _ = ctxt := [] 
    val _ = imperative_flag := false
  in
    ps
  end

(*
load "mcts"; open aiLib kernel mcts;
let val p = random_prog 20 in 
  print_endline (humanf p ^ "\n"); 
  print_endline (humani p) 
end;

*)

(* -------------------------------------------------------------------------
   Timer
   ------------------------------------------------------------------------- *)

val search_time = ref (Time.fromReal 60.0)
val search_steps = ref (valOf (Int.maxInt))
val rt_glob = ref (Timer.startRealTimer ())
val rti_glob = ref 0

exception SearchTimeout;

fun init_timer () = 
  (rti_glob := 0; rt_glob := Timer.startRealTimer ())

fun check_timer () = 
  (
  if !rti_glob mod 100000 = 0 then print "." else ();
  if !rti_glob > !search_steps orelse
     (!rti_glob mod 100 = 0 andalso 
      Timer.checkRealTimer (!rt_glob) > !search_time)
  then raise SearchTimeout 
  else incr rti_glob
  )

end (* struct *)