File size: 52,906 Bytes
3bdb76c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from dreamcoder.grammar import *

epsilon = 0.001


def instantiate(context, environment, tp):
    bindings = {}
    context, tp = tp.instantiate(context, bindings)
    newEnvironment = {}
    for i,ti in environment.items():
        context,newEnvironment[i] = ti.instantiate(context, bindings)
    return context, newEnvironment, tp

def unify(*environmentsAndTypes):
    k = Context.EMPTY
    e = {}
    k,t = k.makeVariable()
    for e_,t_ in environmentsAndTypes:
        k, e_, t_ = instantiate(k, e_, t_)
        k = k.unify(t,t_)
        for i,ti in e_.items():
            if i not in e: e[i] = ti
            else: k = k.unify(e[i], ti)
    return {i: ti.apply(k) for i,ti in e.items() }, t.apply(k)

class Union(Program):
    def __init__(self, elements, canBeEmpty=False):
        self.elements = frozenset(elements)
        if not canBeEmpty: assert len(self.elements) > 1
        
    @property
    def isUnion(self): return True
    def __eq__(self,o):
        return isinstance(o,Union) and self.elements == o.elements
    def __hash__(self): return hash(self.elements)
    def __str__(self):
        return "{%s}"%(", ".join(map(str,list(self.elements))))
    def show(self, isFunction):
        return str(self)
    def __repr__(self): return str(self)
    def __iter__(self): return iter(self.elements)

class VersionTable():
    def __init__(self, typed=True, identity=True, factored=False):
        self.factored = factored
        self.identity = identity
        self.typed = typed
        self.debug = False
        if self.debug:
            print("WARNING: running version spaces in debug mode. Will be substantially slower.")
        
        self.expressions = []
        self.recursiveTable = []
        self.substitutionTable = {}
        self.expression2index = {}
        self.maximumShift = []
        # Table containing (minimum cost, set of minimum cost programs)
        self.inhabitantTable = []
        # Table containing (minimum cost, set of minimum cost programs NOT starting w/ abstraction)
        self.functionInhabitantTable = []
        self.superCache = {}

        self.overlapTable = {}
        
        self.universe = self.incorporate(Primitive("U",t0,None))
        self.empty = self.incorporate(Union([], canBeEmpty=True))

    def __len__(self): return len(self.expressions)

    def clearOverlapTable(self):
        self.overlapTable = {}

    def visualize(self, j):
        from graphviz import Digraph
        g = Digraph()

        visited = set()
        def walk(i):
            if i in visited: return

            if i == self.universe:
                g.node(str(i), 'universe')
            elif i == self.empty:
                g.node(str(i), 'nil')
            else:
                l = self.expressions[i]
                if l.isIndex or l.isPrimitive or l.isInvented:
                    g.node(str(i), str(l))
                elif l.isAbstraction:
                    g.node(str(i), "lambda")
                    walk(l.body)
                    g.edge(str(i), str(l.body))
                elif l.isApplication:
                    g.node(str(i), "@")
                    walk(l.f)
                    walk(l.x)
                    g.edge(str(i), str(l.f), label='f')
                    g.edge(str(i), str(l.x), label='x')
                elif l.isUnion:
                    g.node(str(i), "U")
                    for c in l:
                        walk(c)
                        g.edge(str(i), str(c))
                else:
                    assert False
            visited.add(i)
        walk(j)
        g.render(view=True)

    def branchingFactor(self,j):
        l = self.expressions[j]
        if l.isApplication: return max(self.branchingFactor(l.f),
                                       self.branchingFactor(l.x))
        if l.isUnion: return max([len(l.elements)] + [self.branchingFactor(e) for e in l ])
        if l.isAbstraction: return self.branchingFactor(l.body)
        return 0
            
        
    def intention(self,j, isFunction=False):
        l = self.expressions[j]
        if l.isIndex or l.isPrimitive or l.isInvented: return l
        if l.isAbstraction: return Abstraction(self.intention(l.body))
        if l.isApplication: return Application(self.intention(l.f),
                                               self.intention(l.x))
        if l.isUnion: return Union(self.intention(e)
                                   for e in l )
        assert False

    def walk(self,j):
        """yields every subversion space of j"""
        visited = set()
        def r(n):
            if n in visited: return
            visited.add(n)
            l = self.expressions[n]
            yield l
            if l.isApplication:
                yield from r(l.f)
                yield from r(l.x)
            if l.isAbstraction:
                yield from r(l.body)
            if l.isUnion:
                for e in l:
                    yield from r(e)
        yield from r(j)

                
    def incorporate(self,p):
        #assert isinstance(p,Union)# or p.wellTyped()
        if p.isIndex or p.isPrimitive or p.isInvented:
            pass
        elif p.isAbstraction:
            p = Abstraction(self.incorporate(p.body))
        elif p.isApplication:
            p = Application(self.incorporate(p.f),
                            self.incorporate(p.x))
        elif p.isUnion:
            if len(p.elements) > 0:
                p = Union([self.incorporate(e) for e in p ])
        else: assert False

        j = self._incorporate(p)
        return j

    def _incorporate(self,p):
        if p in self.expression2index: return self.expression2index[p]

        j = len(self.expressions)
        
        self.expressions.append(p)
        self.expression2index[p] = j
        self.recursiveTable.append(None)
        self.inhabitantTable.append(None)
        self.functionInhabitantTable.append(None)
        
        return j

    def extract(self,j):
        l = self.expressions[j]
        if l.isAbstraction:
            for b in self.extract(l.body):
                yield Abstraction(b)
        elif l.isApplication:
            for f in self.extract(l.f):
                for x in self.extract(l.x):
                    yield Application(f,x)
        elif l.isIndex or l.isPrimitive or l.isInvented:
            yield l
        elif l.isUnion:
            for e in l:
                yield from self.extract(e)
        else: assert False

    def reachable(self, heads):
        visited = set()
        def visit(j):
            if j in visited: return
            visited.add(j)

            l = self.expressions[j]
            if l.isUnion:
                for e in l:
                    visit(e)
            elif l.isAbstraction: visit(l.body)
            elif l.isApplication:
                visit(l.f)
                visit(l.x)

        for h in heads:
            visit(h)
        return visited

    def size(self,j):
        l = self.expressions[j]
        if l.isApplication:
            return self.size(l.f) + self.size(l.x)
        elif l.isAbstraction:
            return self.size(l.body)
        elif l.isUnion:
            return sum(self.size(e) for e in l )
        else:
            return 1
            

    def union(self,elements):
        if self.universe in elements: return self.universe
        
        _e = []
        for e in elements:
            if self.expressions[e].isUnion:
                for j in self.expressions[e]:
                    _e.append(j)
            elif e != self.empty:
                _e.append(e)

        elements = frozenset(_e)
        if len(elements) == 0: return self.empty
        if len(elements) == 1: return next(iter(elements))
        return self._incorporate(Union(elements))
    def apply(self,f,x):
        if f == self.empty: return f
        if x == self.empty: return x
        return self._incorporate(Application(f,x))
    def abstract(self,b):
        if b == self.empty: return self.empty
        return self._incorporate(Abstraction(b))
    def index(self,i):
        return self._incorporate(Index(i))

    def intersection(self,a,b):
        if a == self.empty or b == self.empty: return self.empty
        if a == self.universe: return b
        if b == self.universe: return a
        if a == b: return a

        x = self.expressions[a]
        y = self.expressions[b]

        if x.isAbstraction and y.isAbstraction:
            return self.abstract(self.intersection(x.body,y.body))
        if x.isApplication and y.isApplication:
            return self.apply(self.intersection(x.f,y.f),
                              self.intersection(x.x,y.x))
        if x.isUnion:
            if y.isUnion:
                return self.union([ self.intersection(x_,y_)
                                    for x_ in x
                                    for y_ in y ])
            return self.union([ self.intersection(x_, b)
                                for x_ in x ])
        if y.isUnion:
            return self.union([ self.intersection(a, y_)
                                for y_ in y ])
        return self.empty

    def haveOverlap(self,a,b):
        if a == self.empty or b == self.empty: return False
        if a == self.universe: return True
        if b == self.universe: return True
        if a == b: return True

        if a in self.overlapTable:
            if b in self.overlapTable[a]:
                return self.overlapTable[a][b]
        else: self.overlapTable[a] = {}

        x = self.expressions[a]
        y = self.expressions[b]

        if x.isAbstraction and y.isAbstraction:
            overlap = self.haveOverlap(x.body,y.body)
        elif x.isApplication and y.isApplication:
            overlap = self.haveOverlap(x.f,y.f) and \
                self.haveOverlap(x.x,y.x)
        elif x.isUnion:
            if y.isUnion:
                overlap = any( self.haveOverlap(x_,y_)
                            for x_ in x
                            for y_ in y )
            overlap = any( self.haveOverlap(x_, b)
                        for x_ in x )
        elif y.isUnion:
            overlap = any( self.haveOverlap(a, y_)
                        for y_ in y )
        else:
            overlap = False
        self.overlapTable[a][b] = overlap
        return overlap

    def minimalInhabitants(self,j):
        """Returns (minimal size, set of singleton version spaces)"""
        assert isinstance(j,int)
        if self.inhabitantTable[j] is not None: return self.inhabitantTable[j]
        e = self.expressions[j]
        if e.isAbstraction:
            cost, members = self.minimalInhabitants(e.body)
            cost = cost + epsilon
            members = {self.abstract(m) for m in members}
        elif e.isApplication:
            fc, fm = self.minimalFunctionInhabitants(e.f)
            xc, xm = self.minimalInhabitants(e.x)
            cost = fc + xc + epsilon
            members = {self.apply(f_,x_)
                       for f_ in fm for x_ in xm }
        elif e.isUnion:
            children = [self.minimalInhabitants(z)
                        for z in e ]
            cost = min(c for c,_ in children)
            members = {zp
                       for c,z in children
                       if c == cost
                       for zp in z }
        else:
            assert e.isIndex or e.isInvented or e.isPrimitive
            cost = 1
            members = {j}


        # if len(members) > 1:
        #     for m in members: break
        #     members = {m}
        self.inhabitantTable[j] = (cost, members)
        
        return cost, members

    def minimalFunctionInhabitants(self,j):
        """Returns (minimal size, set of singleton version spaces)"""
        assert isinstance(j,int)
        if self.functionInhabitantTable[j] is not None: return self.functionInhabitantTable[j]
        e = self.expressions[j]
        if e.isAbstraction:
            cost = POSITIVEINFINITY
            members = set()
        elif e.isApplication:
            fc, fm = self.minimalFunctionInhabitants(e.f)
            xc, xm = self.minimalInhabitants(e.x)
            cost = fc + xc + epsilon
            members = {self.apply(f_,x_)
                       for f_ in fm for x_ in xm }
        elif e.isUnion:
            children = [self.minimalFunctionInhabitants(z)
                        for z in e ]
            cost = min(c for c,_ in children)
            members = {zp
                       for c,z in children
                       if c == cost
                       for zp in z }
        else:
            assert e.isIndex or e.isInvented or e.isPrimitive
            cost = 1
            members = {j}

        # if len(members) > 1:
        #     for m in members: break
        #     members = {m}
            
        self.functionInhabitantTable[j] = (cost, members)
        return cost, members

    def shiftFree(self,j,n,c=0):
        if n == 0: return j
        l = self.expressions[j]
        if l.isUnion:
            return self.union([ self.shiftFree(e,n,c)
                                for e in l ])
        if l.isApplication:
            return self.apply(self.shiftFree(l.f,n,c),
                              self.shiftFree(l.x,n,c))
        if l.isAbstraction:
            return self.abstract(self.shiftFree(l.body,n,c+1))
        if l.isIndex:
            if l.i < c: return j
            if l.i >= n + c: return self.index(l.i - n)
            return self.empty
        assert l.isPrimitive or l.isInvented
        return j

    def substitutions(self,j):
        if self.typed:
            for (v,_),b in self._substitutions(j,0).items():
                yield v,b
        else:
            yield from self._substitutions(j,0).items()

    def _substitutions(self,j,n):
        if (j,n) in self.substitutionTable: return self.substitutionTable[(j,n)]
        
        
        s = self.shiftFree(j,n)
        if self.debug:
            assert set(self.extract(s)) == set( e.shift(-n)
                                                for e in self.extract(j)
                                                if all( f >= n for f in e.freeVariables()  )),\
                                                   "shiftFree_%d: %s"%(n,set(self.extract(s)))
        if s == self.empty: m = {}
        else:
            if self.typed:
                principalType = self.infer(s)
                if principalType == self.bottom:
                    print(self.infer(j))
                    print(list(self.extract(j)))
                    print(list(self.extract(s)))
                    assert False
                m = {(s, self.infer(s)[1].canonical()): self.index(n)}
            else:
                m = {s: self.index(n)}

        l = self.expressions[j]
        if l.isPrimitive or l.isInvented:
            m[(self.universe,t0) if self.typed else self.universe] = j
        elif l.isIndex:
            m[(self.universe,t0) if self.typed else self.universe] = \
                    j if l.i < n else self.index(l.i + 1)
        elif l.isAbstraction:
            for v,b in self._substitutions(l.body, n + 1).items():
                m[v] = self.abstract(b)
        elif l.isApplication and not self.factored:
            newMapping = {}
            fm = self._substitutions(l.f,n)
            xm = self._substitutions(l.x,n)
            for v1,f in fm.items():
                if self.typed: v1,nType1 = v1
                for v2,x in xm.items():
                    if self.typed: v2,nType2 = v2

                    a = self.apply(f,x)
                    # See if the types that they assigned to $n are consistent
                    if self.typed:
                        if self.infer(a) == self.bottom: continue
                        try:
                            nType = canonicalUnification(nType1, nType2,
                                                         self.infer(a)[0].get(n,t0))
                        except UnificationFailure:
                            continue
                        
                    v = self.intersection(v1,v2)
                    if v == self.empty: continue
                    if self.typed and self.infer(v) == self.bottom: continue

                    key = (v,nType) if self.typed else v                        
                        
                    if key in newMapping:
                        newMapping[key].append(a)
                    else:
                        newMapping[key] = [a]
            for v in newMapping:
                newMapping[v] = self.union(newMapping[v])
            newMapping.update(m)
            m = newMapping
            # print(f"substitutions: |{len(fm)}|x|{len(xm)}| = {len(m)}\t{len(m) <= len(fm)+len(xm)}")
        elif l.isApplication and self.factored:
            newMapping = {}
            fm = self._substitutions(l.f,n)
            xm = self._substitutions(l.x,n)
            for v1,f in fm.items():
                if self.typed: v1,nType1 = v1
                for v2,x in xm.items():
                    if self.typed: v2,nType2 = v2
                    v = self.intersection(v1,v2)
                    if v == self.empty: continue
                    if v in newMapping:
                        newMapping[v] = ({f} | newMapping[v][0],
                                         {x} | newMapping[v][1])
                    else:
                        newMapping[v] = ({f},{x})
            for v,(fs,xs) in newMapping.items():
                fs = self.union(list(fs))
                xs = self.union(list(xs))
                m[v] = self.apply(fs,xs)
            # print(f"substitutions: |{len(fm)}|x|{len(xm)}| = {len(m)}\t{len(m) <= len(fm)+len(xm)}")
        elif l.isUnion:
            newMapping = {}
            for e in l:
                for v,b in self._substitutions(e,n).items():
                    if v in newMapping:
                        newMapping[v].append(b)
                    else:
                        newMapping[v] = [b]
            for v in newMapping:
                newMapping[v] = self.union(newMapping[v])
            newMapping.update(m)
            m = newMapping
        else: assert False

        self.substitutionTable[(j,n)] = m

        return m

    def inversion(self,j):
        i = self.union([self.apply(self.abstract(b),v)
                         for v,b in self.substitutions(j)
                         if v != self.universe])
        if self.debug and self.typed:
            if not (self.infer(i) == self.infer(j)):
                print("inversion produced space with a different type!")
                print("the original type was",self.infer(j))
                print("the type of the rewritten expressions is",self.infer(i))
                print("the original extension was")
                n = None
                for e in self.extract(j):
                    print(e, e.infer())
                    # print(f"\t{e.betaNormalForm()} : {e.betaNormalForm().infer()}")
                    assert n is None or e.betaNormalForm() == n
                    n = e.betaNormalForm()
                    print("the rewritten extension is")
                for e in self.extract(i):
                    print(e, e.infer())
                    # print(f"\t{e.betaNormalForm()} : {e.betaNormalForm().infer()}")
                    assert n is None or e.betaNormalForm() == n
                    assert self.infer(i) == self.infer(j)
                assert False
        return i


    def recursiveInversion(self,j):
        if self.recursiveTable[j] is not None: return self.recursiveTable[j]
        
        l = self.expressions[j]
        if l.isUnion:
            return self.union([self.recursiveInversion(e) for e in l ])
        
        t = [self.apply(self.abstract(b),v)
             for v,b in self.substitutions(j)
             if v != self.universe and (self.identity or b != self.index(0))]
        if self.debug and self.typed:
            ru = self.union(t)
            if not (self.infer(ru) == self.infer(j)):
                print("inversion produced space with a different type!")
                print("the original type was",self.infer(j))
                print("the type of the rewritten expressions is",self.infer(ru))
                print("the original extension was")
                n = None
                for e in self.extract(j):
                    print(e, e.infer())
                    # print(f"\t{e.betaNormalForm()} : {e.betaNormalForm().infer()}")
                    assert n is None or e.betaNormalForm() == n
                    n = e.betaNormalForm()
                print("the rewritten extension is")
                for e in self.extract(ru):
                    print(e, e.infer())
                    # print(f"\t{e.betaNormalForm()} : {e.betaNormalForm().infer()}")
                    assert n is None or e.betaNormalForm() == n
            assert self.infer(ru) == self.infer(j)


        if l.isApplication:
            t.append(self.apply(self.recursiveInversion(l.f),l.x))
            t.append(self.apply(l.f,self.recursiveInversion(l.x)))
        elif l.isAbstraction:
            t.append(self.abstract(self.recursiveInversion(l.body)))

        ru = self.union(t)        
        self.recursiveTable[j] = ru
        return ru

    def repeatedExpansion(self,j,n):
        spaces = [j]
        for _ in range(n):
            spaces.append(self.recursiveInversion(spaces[-1]))
        return spaces
            
    def rewriteReachable(self,heads,n):
        vertices = self.reachable(heads)
        spaces = {v: self.repeatedExpansion(v,n)
                  for v in vertices }
        return spaces

    def properVersionSpace(self, j, n):
        return self.union(self.repeatedExpansion(j, n))

    def superVersionSpace(self, j, n):
        """Construct decorated tree and then merge version spaces with subtrees via union operator"""
        if j in self.superCache: return self.superCache[j]
        spaces = self.rewriteReachable({j}, n)
        def superSpace(i):
            assert i in spaces
            e = self.expressions[i]
            components = [i] + spaces[i]
            if e.isIndex or e.isPrimitive or e.isInvented:
                pass
            elif e.isAbstraction:
                components.append(self.abstract(superSpace(e.body)))
            elif e.isApplication:
                components.append(self.apply(superSpace(e.f), superSpace(e.x)))
            elif e.isUnion: assert False
            else: assert False
            
            return self.union(components)
        self.superCache[j] = superSpace(j)
        return self.superCache[j]
            
    def loadEquivalences(self, g, spaces):
        versionClasses = [None]*len(self.expressions)
        def extract(j):
            if versionClasses[j] is not None:
                return versionClasses[j]
            
            l = self.expressions[j]
            if l.isAbstraction:
                ks = g.setOfClasses(g.abstractClass(b)
                                    for b in extract(l.body))
            elif l.isApplication:
                fs = extract(l.f)
                xs = extract(l.x)
                ks = g.setOfClasses(g.applyClass(f,x)
                                    for x in xs for f in fs )
            elif l.isUnion:
                ks = g.setOfClasses(e for u in l for e in extract(u))
            else:
                ks = g.setOfClasses({g.incorporate(l)})
            versionClasses[j] = ks
            return ks
            

        N = len(next(iter(spaces.values())))
        vertices = list(sorted(spaces.keys(), key=lambda v: self.size(v)))

        # maps from a vertex to a map from types to classes
        # the idea is to only enforceable equivalence between terms of the same type
        typedClassesOfVertex = {v: {} for v in vertices }
        
        for n in range(N):
            # print(f"Processing rewrites {n} steps away from original expressions...")
            for v in vertices:
                expressions = list(self.extract(v))
                assert len(expressions) == 1
                expression = expressions[0]
                k = g.incorporate(expression)
                if k is None: continue
                t0 = g.typeOfClass[k]
                if t0 not in typedClassesOfVertex[v]:
                    typedClassesOfVertex[v][t0] = k
                extracted = list(extract(spaces[v][n]))
                for e in extracted:
                    t = g.typeOfClass[e]
                    if t in typedClassesOfVertex[v]:
                        g.makeEquivalent(typedClassesOfVertex[v][t],e)
                    else:
                        typedClassesOfVertex[v][e] = e

    def bestInventions(self, versions, bs=25):
        """versions: [[version index]]"""
        """bs: beam size"""
        """returns: list of (indices to) candidates"""
        import gc
        
        def nontrivial(proposal):
            primitives = 0
            collisions = 0
            indices = set()
            for d, tree in proposal.walk():
                if tree.isPrimitive or tree.isInvented: primitives += 1
                elif tree.isIndex:
                    i = tree.i - d
                    if i in indices: collisions += 1
                    indices.add(i)
            return primitives > 1 or (primitives == 1 and collisions > 0)

        with timing("calculated candidates from version space"):
            candidates = [{j
                           for k in self.reachable(hs)
                           for _,js in [self.minimalInhabitants(k), self.minimalFunctionInhabitants(k)]
                           for j in js }
                          for hs in versions]
            from collections import Counter
            candidates = Counter(k for ks in candidates for k in ks)
            candidates = {k for k,f in candidates.items() if f >= 2 and nontrivial(next(self.extract(k))) }
            # candidates = [k for k in candidates if next(self.extract(k)).isBetaLong()]
            eprint(len(candidates),"candidates from version space")

            # Calculate the number of free variables for each candidate invention
            # This is important because, if a candidate has free variables,
            # then whenever we use it we will have to apply it to those free variables;
            # thus using a candidate with free variables is more expensive
            candidateCost = {k: len(set(next(self.extract(k)).freeVariables())) + 1
                             for k in candidates }

        inhabitTable = self.inhabitantTable
        functionTable = self.functionInhabitantTable

        class B():
            def __init__(self, j):
                cost, inhabitants = inhabitTable[j]
                functionCost, functionInhabitants = functionTable[j]
                self.relativeCost = {inhabitant: candidateCost[inhabitant]
                                     for inhabitant in inhabitants
                                     if inhabitant in candidates}
                self.relativeFunctionCost = {inhabitant: candidateCost[inhabitant]
                                             # INTENTIONALLY, do not use function inhabitants
                                             for inhabitant in inhabitants
                                             if inhabitant in candidates}
                self.defaultCost = cost
                self.defaultFunctionCost = functionCost

            @property
            def domain(self):
                return set(self.relativeCost.keys())
            @property
            def functionDomain(self):
                return set(self.relativeFunctionCost.keys())
            def restrict(self):
                if len(self.relativeCost) > bs:
                    self.relativeCost = dict(sorted(self.relativeCost.items(),
                                                    key=lambda rk: rk[1])[:bs])
                if len(self.relativeFunctionCost) > bs:
                    self.relativeFunctionCost = dict(sorted(self.relativeFunctionCost.items(),
                                                            key=lambda rk: rk[1])[:bs])
            def getCost(self, given):
                return self.relativeCost.get(given, self.defaultCost)
            def getFunctionCost(self, given):
                return self.relativeFunctionCost.get(given, self.defaultFunctionCost)
            def relax(self, given, cost):
                self.relativeCost[given] = min(cost,
                                               self.getCost(given))
            def relaxFunction(self, given, cost):
                self.relativeFunctionCost[given] = min(cost,
                                                       self.getFunctionCost(given))

            def unobject(self):
                return {'relativeCost': self.relativeCost, 'defaultCost': self.defaultCost,
                        'relativeFunctionCost': self.relativeFunctionCost, 'defaultFunctionCost': self.defaultFunctionCost}

        beamTable = [None]*len(self.expressions)

        def costs(j):
            if beamTable[j] is not None:
                return beamTable[j]

            beamTable[j] = B(j)
            
            e = self.expressions[j]
            if e.isIndex or e.isPrimitive or e.isInvented:
                pass
            elif e.isAbstraction:
                b = costs(e.body)
                for i,c in b.relativeCost.items():
                    beamTable[j].relax(i, c + epsilon)
            elif e.isApplication:
                f = costs(e.f)
                x = costs(e.x)
                for i in f.functionDomain | x.domain:
                    beamTable[j].relax(i, f.getFunctionCost(i) + x.getCost(i) + epsilon)
                    beamTable[j].relaxFunction(i, f.getFunctionCost(i) + x.getCost(i) + epsilon)
            elif e.isUnion:
                for z in e:
                    cz = costs(z)
                    for i,c in cz.relativeCost.items(): beamTable[j].relax(i, c)
                    for i,c in cz.relativeFunctionCost.items(): beamTable[j].relaxFunction(i, c)
            else: assert False

            beamTable[j].restrict()
            return beamTable[j]

        with timing("beamed version spaces"):
            beams = parallelMap(numberOfCPUs(),
                                lambda hs: [ costs(h).unobject() for h in hs ],
                                versions,
                                memorySensitive=True,
                                chunksize=1,
                                maxtasksperchild=1)

        # This can get pretty memory intensive - clean up the garbage
        beamTable = None
        gc.collect()
        
        candidates = {d
                      for _bs in beams
                      for b in _bs
                      for d in b['relativeCost'].keys() }
        def score(candidate):
            return sum(min(min(b['relativeCost'].get(candidate, b['defaultCost']),
                               b['relativeFunctionCost'].get(candidate, b['defaultFunctionCost']))
                           for b in _bs )
                       for _bs in beams )
        candidates = sorted(candidates, key=score)
        return candidates

    def rewriteWithInvention(self, i, js):
        """Rewrites list of indices in beta long form using invention"""
        self.clearOverlapTable()
        class RW():
            """rewritten cost/expression either as a function or argument"""            
            def __init__(self, f,fc,a,ac):
                assert not (fc < ac)
                self.f, self.fc, self.a, self.ac = f,fc,a,ac
        
        _i = list(self.extract(i))
        assert len(_i) == 1
        _i = _i[0]
        
        table = {}
        def rewrite(j):
            if j in table: return table[j]
            e = self.expressions[j]
            if self.haveOverlap(i, j): r = RW(fc=1,ac=1,
                                              f=_i,a=_i)
            elif e.isPrimitive or e.isInvented or e.isIndex:
                r = RW(fc=1,ac=1,
                       f=e,a=e)
            elif e.isApplication:
                f = rewrite(e.f)
                x = rewrite(e.x)
                cost = f.fc + x.ac + epsilon
                ep = Application(f.f, x.a) if cost < POSITIVEINFINITY else None
                r = RW(fc=cost, ac=cost,
                       f=ep, a=ep)
            elif e.isAbstraction:
                b = rewrite(e.body)
                cost = b.ac + epsilon
                ep = Abstraction(b.a) if cost < POSITIVEINFINITY else None
                r = RW(f=None, fc=POSITIVEINFINITY,
                       a=ep, ac=cost)
            elif e.isUnion:
                children = [rewrite(z) for z in e ]
                f,fc = min(( (child.f, child.fc) for child in children ),
                           key=cindex(1))
                a,ac = min(( (child.a, child.ac) for child in children ),
                           key=cindex(1))
                r = RW(f=f,fc=fc,
                       a=a,ac=ac)
            else: assert False
            table[j] = r
            return r
        js = [ rewrite(j).a for j in js ]
        self.clearOverlapTable()
        return js
        
    def addInventionToGrammar(self, candidate, g0, frontiers,
                              pseudoCounts=1.):
        candidateSource = next(self.extract(candidate))
        v = RewriteWithInventionVisitor(candidateSource)
        invention = v.invention

        rewriteMapping = list({e.program
                               for f in frontiers
                               for e in f })
        spaces = [self.superCache[self.incorporate(program)]
                  for program in rewriteMapping ]
        rewriteMapping = dict(zip(rewriteMapping,
                                  self.rewriteWithInvention(candidate, spaces)))

        def tryRewrite(program, request=None):
            rw = v.execute(rewriteMapping[program], request=request)
            # print(f"Rewriting {program} ({rewriteMapping[program]}) : rw={rw}")
            # print("slow-motion:")
            # try:
            #     i = rewriteMapping[program].visit(v)
            #     print(f"\ti={i}")
            #     l = EtaLongVisitor().execute(i)
            #     print(f"\tl={l}")
            # except Exception as e: print(e)
            return rw or program

        frontiers = [Frontier([FrontierEntry(program=tryRewrite(e.program, request=f.task.request),
                                             logLikelihood=e.logLikelihood,
                                             logPrior=0.)
                                       for e in f ],
                              f.task)
                     for f in frontiers ]
        # print(invention)
        # for f in frontiers: print(f.entries[0].program)
        # print()
        # print()
        g = Grammar.uniform([invention] + g0.primitives, continuationType=g0.continuationType).\
            insideOutside(frontiers,
                          pseudoCounts=pseudoCounts)
        frontiers = [g.rescoreFrontier(f) for f in frontiers]
        return g, frontiers

class CloseInventionVisitor():
    """normalize free variables - e.g., if $1 & $3 occur free then rename them to $0, $1
    then wrap in enough lambdas so that there are no free variables and finally wrap in invention"""
    def __init__(self, p):
        self.p = p
        freeVariables = list(sorted(set(p.freeVariables())))
        self.mapping = {fv: j for j,fv in enumerate(freeVariables) }
    def index(self, e, d):
        if e.i - d in self.mapping:
            return Index(self.mapping[e.i - d] + d)
        return e
    def abstraction(self, e, d):
        return Abstraction(e.body.visit(self, d + 1))
    def application(self, e, d):
        return Application(e.f.visit(self, d),
                           e.x.visit(self, d))
    def primitive(self, e, d): return e
    def invented(self, e, d): return e

    def execute(self):
        normed = self.p.visit(self, 0)
        closed = normed
        for _ in range(len(self.mapping)):
            closed = Abstraction(closed)
        return Invented(closed)
        
        
class RewriteWithInventionVisitor():
    def __init__(self, p):
        v = CloseInventionVisitor(p)
        self.original = p
        self.mapping = { j: fv for fv, j in v.mapping.items() }
        self.invention = v.execute()

        self.appliedInvention = self.invention
        for j in range(len(self.mapping) - 1, -1, -1):
            self.appliedInvention = Application(self.appliedInvention, Index(self.mapping[j]))
                

    def tryRewrite(self, e):
        if e == self.original:
            return self.appliedInvention
        return None

    def index(self, e): return e
    def primitive(self, e): return e
    def invented(self, e): return e
    def abstraction(self, e):
        return self.tryRewrite(e) or Abstraction(e.body.visit(self))
    def application(self, e):
        return self.tryRewrite(e) or Application(e.f.visit(self),
                                                 e.x.visit(self))
    def execute(self, e, request=None):
        try:
            i = e.visit(self)
            l = EtaLongVisitor(request=request).execute(i)
            return l
        except (UnificationFailure, EtaExpandFailure):
            return None    
        



def induceGrammar_Beta(g0, frontiers, _=None,
                       pseudoCounts=1.,
                       a=3,
                       aic=1.,
                       topK=2,
                       topI=50,
                       structurePenalty=1.,
                       CPUs=1):
    """grammar induction using only version spaces"""
    from dreamcoder.fragmentUtilities import primitiveSize
    import gc
    
    originalFrontiers = frontiers
    frontiers = [frontier for frontier in frontiers if not frontier.empty]
    eprint("Inducing a grammar from", len(frontiers), "frontiers")

    arity = a

    def restrictFrontiers():
        return parallelMap(1,#CPUs,
                           lambda f: g0.rescoreFrontier(f).topK(topK),
                           frontiers,
                           memorySensitive=True,
                           chunksize=1,
                           maxtasksperchild=1)
    restrictedFrontiers = restrictFrontiers()
    
    def objective(g, fs):
        ll = sum(g.frontierMDL(f) for f in fs )
        sp = structurePenalty * sum(primitiveSize(p) for p in g.primitives)
        return ll - sp - aic*len(g.productions)
            
    v = None
    def scoreCandidate(candidate, currentFrontiers, currentGrammar):
        try:
            newGrammar, newFrontiers = v.addInventionToGrammar(candidate, currentGrammar, currentFrontiers,
                                                               pseudoCounts=pseudoCounts)
        except InferenceFailure:
            # And this can occur if the candidate is not well typed:
            # it is expected that this can occur;
            # in practice, it is more efficient to filter out the ill typed terms,
            # then it is to construct the version spaces so that they only contain well typed terms.
            return NEGATIVEINFINITY
            
        o = objective(newGrammar, newFrontiers)

        #eprint("+", end='')
        eprint(o,'\t',newGrammar.primitives[0],':',newGrammar.primitives[0].tp)

        # eprint(next(v.extract(candidate)))
        # for f in newFrontiers:
        #     for e in f:
        #         eprint(e.program)
        
        return o
        
    with timing("Estimated initial grammar production probabilities"):
        g0 = g0.insideOutside(restrictedFrontiers, pseudoCounts)
    oldScore = objective(g0, restrictedFrontiers)
    eprint("Starting grammar induction score",oldScore)
    
    while True:
        v = VersionTable(typed=False, identity=False)
        with timing("constructed %d-step version spaces"%arity):
            versions = [[v.superVersionSpace(v.incorporate(e.program), arity) for e in f]
                        for f in restrictedFrontiers ]
            eprint("Enumerated %d distinct version spaces"%len(v.expressions))
        
        # Bigger beam because I feel like it
        candidates = v.bestInventions(versions, bs=3*topI)[:topI]
        eprint("Only considering the top %d candidates"%len(candidates))

        # Clean caches that are no longer needed
        v.recursiveTable = [None]*len(v)
        v.inhabitantTable = [None]*len(v)
        v.functionInhabitantTable = [None]*len(v)
        v.substitutionTable = {}
        gc.collect()
        
        with timing("scored the candidate inventions"):
            scoredCandidates = parallelMap(CPUs,
                                           lambda candidate: \
                                           (candidate, scoreCandidate(candidate, restrictedFrontiers, g0)),
                                            candidates,
                                           memorySensitive=True,
                                           chunksize=1,
                                           maxtasksperchild=1)
        if len(scoredCandidates) > 0:
            bestNew, bestScore = max(scoredCandidates, key=lambda sc: sc[1])
        if len(scoredCandidates) == 0 or bestScore < oldScore:
            eprint("No improvement possible.")
            # eprint("Runner-up:")
            # eprint(next(v.extract(bestNew)))
            # Return all of the frontiers, which have now been rewritten to use the
            # new fragments
            frontiers = {f.task: f for f in frontiers}
            frontiers = [frontiers.get(f.task, f)
                         for f in originalFrontiers]
            return g0, frontiers
        
        # This is subtle: at this point we have not calculated
        # versions bases for programs outside the restricted
        # frontiers; but here we are rewriting the entire frontier in
        # terms of the new primitive. So we have to recalculate
        # version spaces for everything.
        with timing("constructed versions bases for entire frontiers"):
            for f in frontiers:
                for e in f:
                    v.superVersionSpace(v.incorporate(e.program), arity)
        newGrammar, newFrontiers = v.addInventionToGrammar(bestNew, g0, frontiers,
                                                           pseudoCounts=pseudoCounts)
        eprint("Improved score to", bestScore, "(dS =", bestScore-oldScore, ") w/ invention",newGrammar.primitives[0],":",newGrammar.primitives[0].infer())
        oldScore = bestScore

        for f in newFrontiers:
            eprint(f.summarizeFull())

        g0, frontiers = newGrammar, newFrontiers
        restrictedFrontiers = restrictFrontiers()


        
        
        
        
            
            
def testTyping(p):
    v = VersionTable()
    j = v.incorporate(p)
    
    wellTyped = set(v.extract(v.inversion(j)))
    print(len(wellTyped))
    v = VersionTable(typed=False)
    j = v.incorporate(p)
    arbitrary = set(v.extract(v.recursiveInversion(v.recursiveInversion(v.recursiveInversion(j)))))
    print(len(arbitrary))
    assert wellTyped <= arbitrary
    assert wellTyped == {e
                         for e in arbitrary if e.wellTyped() }
    assert all( e.wellTyped() for e in wellTyped  )

    import sys
    sys.exit()
    
def testSharing(projection=2):
    
    source = "(+ 1 1)"
    N = 4 # maximum number of refactorings
    L = 6 # maximum size of expression

    # def literalSize(v,j):
    #     hs = []
    #     vp = VersionTable(typed=False)
    #     for i in v.extract(j):
    #         hs.append(vp.incorporate(i))
    #     return len(set(vp.reachable(hs)))
    
    # smart = {}
    # dumb = {}
    # for l in range(L):
    #     for n in range(N):
    #         v = VersionTable(typed=False)
            
    #         j = v.properVersionSpace(v.incorporate(Program.parse(source)),n)
    #         smart[(l,n)] = len(v.reachable({j}))
    #         dumb[(l,n)] = literalSize(v,j)
    #         print(f"vs l={l}\tn={n} sz={smart[(l,n)]}")
    #         print(f"db l={l}\tn={n} sz={dumb[(l,n)]}")
    #     # increase the size of the expression
    #     source = "(+ 1 %s)"%source
    #     print("Increased size to",l + 1)

    import numpy as np
    distinct_programs = np.zeros((L,N))
    version_size = np.zeros((L,N))
    program_memory = np.zeros((L,N))

    version_size[0,1] = 24
    distinct_programs[0,1] = 8
    program_memory[0,1] = 28
    version_size[0,2] = 155
    distinct_programs[0,2] = 63
    program_memory[0,2] = 201
    version_size[0,3] = 1126
    distinct_programs[0,3] = 534
    program_memory[0,3] = 1593
    version_size[1,1] = 48
    distinct_programs[1,1] = 24
    program_memory[1,1] = 78
    version_size[1,2] = 526
    distinct_programs[1,2] = 457
    program_memory[1,2] = 1467
    version_size[1,3] = 6639
    distinct_programs[1,3] = 8146
    program_memory[1,3] = 26458
    version_size[2,1] = 74
    distinct_programs[2,1] = 57
    program_memory[2,1] = 193
    version_size[2,2] = 1095
    distinct_programs[2,2] = 2234
    program_memory[2,2] = 7616
    version_size[2,3] = 19633
    distinct_programs[2,3] = 74571
    program_memory[2,3] = 260865
    version_size[3,1] = 101
    distinct_programs[3,1] = 123
    program_memory[3,1] = 438
    version_size[3,2] = 1751
    distinct_programs[3,2] = 9209
    program_memory[3,2] = 32931
    version_size[3,3] = 38781
    distinct_programs[3,3] = 540315
    program_memory[3,3] = 1984171
    version_size[4,1] = 129
    distinct_programs[4,1] = 254
    program_memory[4,1] = 942
    version_size[4,2] = 2488
    distinct_programs[4,2] = 35011
    program_memory[4,2] = 129513
    version_size[4,3] = 63271
    distinct_programs[4,3] = 3477046
    program_memory[4,3] = 13179440
    version_size[5,1] = 158
    distinct_programs[5,1] = 514
    program_memory[5,1] = 1962
    version_size[5,2] = 3308
    distinct_programs[5,2] = 128319
    program_memory[5,2] = 485862
    version_size[5,3] = 93400
    distinct_programs[5,3] = 21042591
    program_memory[5,3] = 81433633


    
    import matplotlib.pyplot as plot
    from matplotlib import rcParams
    rcParams.update({'figure.autolayout': True})

    if projection == 3:
        f = plot.figure()
        a = f.add_subplot(111, projection='3d')
        X = np.arange(0,N)
        Y = np.arange(0,L)
        X,Y = np.meshgrid(X,Y)
        Z = np.zeros((L,N))
        for l in range(L):
            for n in range(N):
                Z[l,n] = smart[(l,n)]

        a.plot_surface(X,
                       Y,
                       np.log10(Z),
                       color='blue',
                       alpha=0.3)
        for l in range(L):
            for n in range(N):
                Z[l,n] = dumb[(l,n)]


        a.plot_surface(X,
                       Y,
                       np.log10(Z),
                       color='red',
                       alpha=0.3)


    else:
        plot.figure(figsize=(3.5,3))
        plot.tight_layout()
        logarithmic = False
        if logarithmic: P = plot.semilogy
        else: P = plot.plot
        for n in range(1, 2):
            xs = np.array(range(L))*2 + 3
            P(xs,
              [version_size[l,n] for l in range(L) ],
              'purple',
              label=None if n > 1 else 'version space')
            P(xs,
              [program_memory[l,n] for l in range(L) ],
              'green',
              label=None if n > 1 else 'no version space')
            if n > 1: dy = 1
            if n == 1 and logarithmic: dy = 0.6
            if n == 1 and not logarithmic: dy = 1
            # plot.text(xs[-1], dy*version_size[L - 1,n], "n=%d"%n)
            # plot.text(xs[-1], dy*program_memory[L - 1,n], "n=%d"%n)
            
        plot.legend()
        plot.xlabel('Size of program being refactored')
        plot.ylabel('Size of VS (purple) or progs (green)')
        plot.xticks(list(xs) + [xs[-1] + 2],
                    [ str(x) if j == 0 or j == L - 1 else ''
                      for j,x in enumerate(list(xs) + [xs[-1] + 2])])
        # if not logarithmic:
        #     plot.ylim([0,100000])
                


    plot.savefig('/tmp/vs.eps')
    assert False

if __name__ == "__main__":
    
    from dreamcoder.domains.arithmetic.arithmeticPrimitives import *
    from dreamcoder.domains.list.listPrimitives import *
    from dreamcoder.fragmentGrammar import *
    bootstrapTarget_extra()
    McCarthyPrimitives()
    testSharing()

    # p = Program.parse("(#(lambda (lambda (lambda (fold $0 empty ($1 $2))))) cons (lambda (lambda (lambda ($2 (+ (+ 5 5) (+ $1 $1)) $0)))))")
    # print(EtaLongVisitor().execute(p))

    # BOOTSTRAP
    programs = [# "(lambda (fix1 $0 (lambda (lambda (if (empty? $0) 0 (+ ($1 (cdr $0)) 1))))))",
                # "(lambda (fix1 $0 (lambda (lambda (if (empty? $0) 0 (+ ($1 (cdr $0)) 1))))))",
                # "(lambda (+ $0 1))",
                # "(lambda (+ (car $0) 1))",
                # "(lambda (+ $0 (+ 1 1)))",
                # "(lambda (- $0 1))",
                # "(lambda (- $0 (+ 1 1)))",
                # "(lambda (- (car $0) 1))",
        ("(lambda (fix1 $0 (lambda (lambda (if (eq? 0 $0) empty (cons (- 0 $0) ($1 (+ 1 $0))))))))",None),
        # ("(lambda (fix1 $0 (lambda (lambda (if (empty? $0) empty (cons (cdr $0) ($1 (cdr $0))))))))",arrow(tlist(tint),tlist(tlist(tint)))),
        # drop the last element
        # ("(lambda (fix1 $0 (lambda (lambda (if (empty? (cdr $0)) empty (cons (car $0) ($1 (cdr $0))))))))",arrow(tlist(tint),tlist(tint))),
        # take in till 1
        # ("(lambda (fix1 $0 (lambda (lambda (if (eq? (car $0) 1) empty (cons (car $0) ($1 (cdr $0))))))))",arrow(tlist(tint),tlist(tint))),
                # "(lambda (lambda (fix2 $1 $0 (lambda (lambda (lambda (if (eq? $1 0) (car $0) ($2 (- $1 1) (cdr $0)))))))))",
                # "(lambda (lambda (fix2 $1 $0 (lambda (lambda (lambda (if (eq? $1 0) (car $0) ($2 (- $1 1) (cdr $0)))))))))",
                ("(lambda (fix1 $0 (lambda (lambda (if (empty? $0) 0 (+ (car $0) ($1 (cdr $0))))))))",None),
                ("(lambda (fix1 $0 (lambda (lambda (if (empty? $0) 1 (- (car $0) ($1 (cdr $0))))))))",None),
        ("(lambda (fix1 $0 (lambda (lambda (if (empty? $0) (cons 0 empty) (cons (car $0) ($1 (cdr $0))))))))",None),
                ("(lambda (fix1 $0 (lambda (lambda (if (empty? $0) (empty? empty) (if (car $0) ($1 (cdr $0)) (eq? 1 0)))))))",None),
                # "(lambda (lambda (fix2 $1 $0 (lambda (lambda (lambda (if (empty? $1) $0 (cons (car $1) ($2 (cdr $1) $0)))))))))",
        #         ("(lambda (fix1 $0 (lambda (lambda (if (empty? $0) empty (cons (+ (car $0) (car $0)) ($1 (cdr $0))))))))",None),
        #         ("(lambda (fix1 $0 (lambda (lambda (if (empty? $0) empty (cons (+ (car $0) 1) ($1 (cdr $0))))))))",None),
        # ("(lambda (fix1 $0 (lambda (lambda (if (empty? $0) empty (cons (- (car $0) 1) ($1 (cdr $0))))))))",None),
        # ("(lambda (fix1 $0 (lambda (lambda (if (empty? $0) empty (cons (cons (car $0) empty) ($1 (cdr $0))))))))",arrow(tlist(tint),tlist(tlist(tint)))),
        # ("(lambda (fix1 $0 (lambda (lambda (if (empty? $0) empty (cons (- 0 (car $0)) ($1 (cdr $0))))))))",None)
    ]
    programs = [(Program.parse(p),t) for p,t in programs ]
    N=3

    primitives = McCarthyPrimitives()
    # for p, _ in programs:
    #     for _, s in p.walk():
    #         if s.isPrimitive:
    #             primitives.add(s)
    g0 = Grammar.uniform(list(primitives))
    print(g0)

    # with timing("RUST test"):
    #     g = induceGrammar(g0, [Frontier.dummy(p, tp=tp) for p, tp in programs],
    #                   CPUs=1,
    #                   a=N,
    #                   backend="vs")
    #     eprint(g)

    # with open('vs.pickle','rb') as handle:
    #     a,kw = pickle.load(handle)
    #     induceGrammar_Beta(*a,**kw)

    with timing("induced DSL"):
        induceGrammar_Beta(g0, [Frontier.dummy(p, tp=tp) for p, tp in programs],
                           CPUs=1,
                           a=N,
                           structurePenalty=0.)

# if __name__ == "__main__":
#     import argparse
#     parser = argparse.ArgumentParser(description = "Version-space based compression")
#     parser.add_argument("--CPUs", type=int, default=1)
#     parser.add_argument("--arity", type=int, default=3)
#     parser.add_argument("--bs", type=int, default=25,
#                         help="beam size")
#     parser.add_argument("--topK", type=int, default=2)
#     parser.add_argument("--topI", type=int, default=None,
#                         help="defaults to beam size")
#     parser.add_argument("--pseudoCounts",
#                         type=float,
#                         default=1.)
#     parser.add_argument("--structurePenalty",
#                         type=float, default=1.)
#     arguments = parser.parse_args()