-
Notifications
You must be signed in to change notification settings - Fork 0
/
sizesLM.py
executable file
·1616 lines (1442 loc) · 59.3 KB
/
sizesLM.py
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
def ina():
var('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r')
def inb():
var('s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R')
def inc():
var('S','T','U','V','W','X','Y','Z','All','Some','new','non','frogs','animals','insects','mammals')
def ind():
var('skunks','rabbits','quadrupeds', 'deer','pests','beautiful_creatures', 'ugly_creatures', 'birds', 'bats', 'horses','ducks','pos','neg')
def initialize():
ina()
inb()
inc()
ind()
initialize()
## 1 axioms 'all x are x',
## 2 assumption in both justification and justificationc NOT ON LIST
## 3 antitone for subsets
## 4 zero
## 5 one
## 6 subsets have smaller cardinality
## 7 antitone for card
## 8 card mix
## 9 the half rule
##### 1 At least as many (q, pos) as (q, neg) Assumption
##### 2 At least as many (p, pos) as (p, neg) Assumption
##### 3 At least as many (p, pos) as (q, neg) Half 1 2
## 10 the maj rule
##### 1 At least as many (x, pos) as (x, neg) Assumption
##### 2 At least as many (y, pos) as (y, neg) Assumption
##### 3 Some ('non', x) are ('non', y) Assumption
##### 4 Some x are y Maj 1 2 3
## 11 Barbara
## 12 cardinality transitivity
## 13 conversion for some
## 14 'some' rule
## 15 Darii
## 16 Some/Card
## 17 More-Some
## 18 More==>At least
## 19 TIPPING IS GONE: there are more x than x',
##there are at least as many y as y' ===> some x is a y
## NOTE THAT TIPPING IS DERIVABLE FROM Strict Half and More-Some
#### 1 At least as many (y, pos) as (y, neg) Assumption
#### 2 There are more (x, pos) than (x, neg) Assumption
#### 3 There are more (x, pos) than (y, neg) Strict Half 1 2
#### 4 Some x are y More-Some 3
## 20 More-Left in connection with At Least transitivity
## 21 More-Right in connection with At Least transitivity
## only one of 20 /21 is needed!!
## 22 MORE-ANTI
## 23 MORE some i is not a j, all j are i ==> more i's than j's
## 24 INT Some i exists and the j's are at least 1/2 ==> some j exists
## 25 Half-strict
#### 1 At least as many (y, pos) as (y, neg) Assumption
#### 2 There are more (x, pos) than (x, neg) Assumption
#### 3 There are more (x, pos) than (y, neg) Strict Half 1 2
def maxx(list_arg):
if len(list_arg)==0:
return 0
else:
return max(list_arg)
def ssup(list):
list2 = map(lambda x: x+1, list)
return maxx(list2)
def last(v):
return(v[len(v)-1])
def elementwise(operator, M, N):
assert(M.parent() == N.parent())
nc, nr = M.ncols(), M.nrows()
A = copy(M.parent().zero_element())
for r in xrange(nr):
for c in xrange(nc):
A[r,c] = operator(M[r,c], N[r,c])
return A
def make_variables(Gamma):
global variables
global polarized_variables
global pvars
polarities = [pos,neg]
vset = Set([])
for item in Gamma:
vset = vset.union(Set([item[1][0],item[2][0]]))
variables = list(vset)
variables.sort(cmp = lambda x,y: cmp(x,y))
polarized_variables = list(cartesian_product([variables,polarities]))
pvars = map(tuple,polarized_variables)
def make_eyes(Gamma):
global Is
global Ms
for item in Gamma:
if item[0] == 'Some':
Is.append((tuple(item[1]),tuple(item[2])))
elif item[0] == 'More':
Ms.append((tuple(item[1]),tuple(item[2])))
Is.append((tuple(item[1]),negation(tuple(item[2]))))
def Apply_using_numbers():
global mg
global mgc
global mgmore
global proven
global g
global g_c
global zeros
global ones
global small_or_half
global large_or_half
global large_or_half_original
global small
global large
global half
global edgeset
global cedgeset
global lis
mgtemp = (mg * mg).apply_map(sgn) ## BARBARA
for i in range(n):
for j in range(n):
if justification[i,j] == 0:
#v1 = mg.row(i)
#v2 = mg.column(j)
#w1 = justification.column(i)
#w2 = justification.column(j)
#x1 = [((1 + w1[entry]+w2[entry]) * v1[entry] * v2[entry]) for entry in range(n)]
#k_found = -1
#shortest_length = -1
#for entry in range(n):
# if (x1[entry] > 0 ) and ((x1[entry] < shortest_length) or (shortest_length == -1)):
# k_found = entry
# shortest_length = x1[entry]
#if k_found > -1:
# mg[i,j] = 1
# justification[i,j] = 11
# proof_length[i,j] = shortest_length
# Barbara_cut_premise[i,j] = k_found
k_found = exists(range(n), lambda k: (mg[i,k]==1 and mg[k,j]==1))
if k_found[0] == True:
k = k_found[1]
justification[i,j] = 11
mg[i,j] =1
proof_length[i,j] = 1 + proof_length[i,k] + proof_length[k,j]
Barbara_cut_premise[i,j] = k
## CARD TRANSITIVITY
for i in range(n):
for j in range(n):
if mgc[i,j] == 0:
k_found = exists(range(n), lambda k: (mgc[i,k]==1 and mgc[k,j]==1))
if k_found[0] == True:
k = k_found[1]
justificationc[i,j] = 12
mgc[i,j]=1
proof_lengthc[i,j] = 1 + proof_lengthc[i,k] + proof_lengthc[k,j]
card_transitivity_cut_premise[i,j] = k
for i in range(n): ## DARII
for j in range(n):
if mgsome[i,j] == 0:
k_found = exists(range(n), lambda k: (mg[k,j]==1 and mgsome[i,k]==1))
if k_found[0] == True:
k = k_found[1]
mgsome[i,j] = 1
justificationsome[i,j] = 15
proof_lengthsome[i,j] = 1 + proof_length[k,j] + proof_lengthsome[i,k]
Darii_missing[i,j] = k
for i in range(n): ## CONVERSION
for j in range(n):
if mgsome[i,j] == 1 and mgsome[j,i] == 0:
mgsome[j,i] = 1
justificationsome[j,i] = 13
proof_lengthsome[j,i] = 1 + proof_lengthsome[i,j]
for i in range(n): ## SOME RULE
for j in range(n):
if mgsome[i,j] == 1 and mgsome[i,i] == 0:
mgsome[i,i] = 1
justificationsome[i,i] = 14
proof_lengthsome[i,i] = 1 + proof_lengthsome[i,j]
Some_rule_missing_premise[0,i] = j
for i in range(n): ## ANTI
for j in range(n):
k = neg_internal[i]
l = neg_internal[j]
if mg[l,k] ==0 and mg[i,j] == 1:
mg[l,k] = 1
justification[l,k] = 3
proof_length[l,k] = 1 + proof_length[i,j]
zeros = [i for i in range(n) if mg[i,neg_internal[i]] == 1]
ones = [i for i in range(n) if mg[neg_internal[i],i] == 1]
#half = [i for i in range(n) if mgc[i,neg_internal[i]] == 1 and mgc[neg_internal[i],i] == 1]
small_or_half = [i for i in range(n) if mgc[i,neg_internal[i]] == 1]
large_or_half = [i for i in range(n) if mgc[neg_internal[i],i] == 1]
large_or_half_original = large_or_half
small = list(Set(small_or_half).difference(Set(large_or_half)))
large = list(Set(large_or_half).difference(Set(small_or_half)))
half = list(Set(small_or_half).intersection(Set(large_or_half)))
for zero in zeros:
for i in range(n):
if mg[zero,i] == 0:
#proven.append([['All', zero, pvar],'zero',
#[['All', zero, negation(zero)]]])
mg[zero,i] = 1
justification[zero,i] = 4
proof_length[zero,i] = 1 + proof_length[zero,neg_internal[zero]]
for one in ones: ## ONE
for i in range(n):
if mg[i,one] == 0:
mg[i,one] = 1
justification[i,one] = 5
proof_length[i,one] = 1 + proof_length[neg_internal[one],one]
for i in range(n): ## SUBSET CARD
for j in range(n):
if (mg[i,j] == 1) and (mgc[i,j] == 0):
mgc[i,j] = 1
justificationc[i,j] = 6
proof_lengthc[i,j] = 1 + proof_length[i,j]
for i in range(n): ## MORE-CARD
for j in range(n):
if (justificationmore[i,j] != 0) and (justificationc[i,j] == 0):
mgc[i,j] = 1
justificationc[i,j] = 18
proof_lengthc[i,j] = 1 + proof_lengthmore[i,j]
for i in range(n): ## MORE-SOME
for j in range(n):
jneg = neg_internal[j]
if (mgmore[j,i] == 1) and (mgsome[i,jneg] == 0):
mgsome[i,jneg] =1
justificationsome[i,jneg] = 17
proof_lengthsome[i,jneg] = 1 + proof_lengthmore[i,j]
for i in range(n): ## 24 INT Some i exists and the j's are at least 1/2 ==> some j exists
for j in range(n):
jneg = neg_internal[j]
if (mgsome[j,j] == 0) and (mgsome[i,i] == 1) and (mgc[jneg,j] == 1):
mgsome[j,j] =1
justificationsome[j,j] = 24
proof_lengthsome[j,j] = 1 + proof_lengthc[jneg,j] + proof_lengthsome[i,i]
Int_rule_missing_premise[0,j] = i
for i in range(n): # some i is not a j, all j are i ==> more i's than j's
for j in range(n):
jneg = neg_internal[j]
if (mgmore[j,i] == 0) \
and (mgsome[i,jneg] == 1) and (mg[j,i] ==1):
mgmore[j,i] = 1
justificationmore[j,i] = 23
proof_lengthmore[j,i] = \
1 + proof_lengthsome[i,jneg] + proof_length[j,i]
for i in range(n): ## CARD-SOME
if mgsome[i,i] == 0:
k_list = exists(range(n), lambda num: mgsome[num,num]==1 and mgc[num,i] == 1)
if k_list[0] == True:
k1 = k_list[1]
mgsome[i,i] = 1
justificationsome[i,i] = 16
proof_lengthsome[i,i] = 1 + proof_lengthsome[k1,k1] + proof_lengthc[k1,i]
Card_Some_missing[i,i] = k1
for i in range(n): ## CARD ANTI
for j in range(n):
k= neg_internal[i]
l = neg_internal[j]
if mgc[l,k] == 0 and mgc[i,j] == 1:
mgc[l,k] = 1
justificationc[l,k] = 7
proof_lengthc[l,k] = 1 + proof_lengthc[i,j]
for i in range(n): ## MORE ANTI
for j in range(n):
k= neg_internal[i]
l = neg_internal[j]
if mgmore[l,k] == 0 and mgmore[i,j] == 1:
mgmore[l,k] = 1
justificationmore[l,k] = 22
proof_lengthmore[l,k] = 1 + proof_lengthmore[i,j]
for i in range(n): ## CARD MIX
for j in range(n):
if mg[j,i] == 0 and mg[i,j] == 1 and mgc[j,i]==1:
mg[j,i] = 1
justification[j,i] = 8
proof_length[j,i] = 1 + proof_length[i,j] + proof_lengthc[j,i]
for i in range(n): ## THE HALF RULE
for j in range(n):
if mgc[i,neg_internal[i]] == 1 and \
mgc[neg_internal[j],j] ==1 and mgc[i,j] == 0:
mgc[i,j]=1
justificationc[i,j]= 9
proof_lengthc[i,j] = 1 + proof_lengthc[i,neg_internal[i]] + proof_lengthc[neg_internal[j],j]
for i in range(n): ## THE STRICT HALF RULE
for j in range(n):
if mgc[i,neg_internal[i]] == 1 and \
mgmore[neg_internal[j],j] and mgmore[i,j] == 0:
mgmore[i,j]=1
justificationmore[i,j]= 25
proof_lengthmore[i,j] = 1 + proof_lengthc[i,neg_internal[i]] + proof_lengthmore[neg_internal[j],j]
# MAJ
#['At least', (i,pos), (i,neg)],['At least', (j, pos), (j, neg)],
#['Some', (i, neg), (j, neg)]]
# conclusion ['Some', (i, pos), (j, pos)]
for i in range(n): ## THE MAJ RULE
for j in range(n):
if mgc[neg_internal[i],i] ==1 and \
mgc[neg_internal[j],j] ==1 and \
mgsome[neg_internal[i],neg_internal[j]] ==1 and \
mgsome[i,j] == 0:
mgsome[i,j]=1
justificationsome[i,j]= 10
proof_lengthsome[i,j] = 1 + proof_lengthmore[neg_internal[i],i] + proof_lengthc[neg_internal[j],j]
for i in range(n): ## MORE-RIGHT
for j in range(n):
if mgmore[i,j] == 0:
k_found = exists(range(n), lambda k: (mgc[k,j]==1 and mgmore[i,k]==1))
if k_found[0] == True:
k = k_found[1]
mgmore[i,j] = 1
justificationmore[i,j] = 21
proof_lengthmore[i,j] = 1 + proof_lengthc[k,j] + proof_lengthmore[i,k]
More_Right_cut_premise[i,j] = k
for i in range(n): ## MORE-LEFT
for j in range(n):
if mgmore[i,j] == 0:
k_found = exists(range(n), lambda k: (mgc[i,k]==1 and mgmore[k,j]==1))
if k_found[0] == True:
k = k_found[1]
mgmore[i,j] = 1
justificationmore[i,j] = 20
proof_lengthmore[i,j] = 1 + proof_lengthc[i,k] + proof_lengthmore[j,k]
More_Left_cut_premise[i,j] = k
# tree form: [ [word, noun1, noun2], leaf/interior, subtree1, subtree2 (possibly), rule name]
def tree_by_numbers(i,j):
if justification[i,j] == 1:
return [['All',i,j],'Axiom']
if justification[i,j] == 2:
return [['All',i,j],'Assumption']
if justification[i,j] == 3:
k = neg_internal[i]
l = neg_internal[j]
return [['All',i,j], tree_by_numbers(l,k), 'Antitone']
if justification[i,j] == 4:
return [['All',i,j], tree_by_numbers(i,neg_internal[i]), 'Zero']
if justification[i,j] == 5:
return [['All',i,j], tree_by_numbers(neg_internal[j],j), 'One']
if justification[i,j] == 8:
return [['All',i,j], tree_by_numbers(j,i),tree_by_numbersc(i,j), 'Card Mix']
if justification[i,j] == 11:
k = Barbara_cut_premise[i,j]
return [['All',i,j], tree_by_numbers(i,k),tree_by_numbers(k,j), 'Barbara']
else:
return "Problem in the justification for", i, "and", j
def tree_by_numbersc(i,j):
if justificationc[i,j] == 2:
return [['At least',j,i],'Assumption']
if justificationc[i,j] == 6:
return [['At least',j,i],tree_by_numbers(i,j),'Subset Card']
if justificationc[i,j] == 7:
k = neg_internal[i]
l = neg_internal[j]
return [['At least',j,i], tree_by_numbersc(l,k), 'Card Antitone']
if justificationc[i,j] == 9:
return [['At least',j,i], tree_by_numbersc(i,neg_internal[i]),tree_by_numbersc(neg_internal[j],j), 'Half']
if justificationc[i,j] == 12:
k = card_transitivity_cut_premise[i,j]
return [['At least',j,i],
tree_by_numbersc(k,j),tree_by_numbersc(i,k),'Card Trans']
if justificationc[i,j] == 18:
return [['At least',j,i], tree_by_numbersmore(i,j), 'More-Card']
else:
return "Problem in the cardinality justification for", i, "and", j
def tree_by_numberssome(i,j):
if justificationsome[i,j] == 2:
return [['Some',i,j],'Assumption']
if justificationsome[i,j] == 13:
return [['Some',i,j], tree_by_numberssome(j,i), 'Conversion']
if justificationsome[i,j] == 14:
k=Some_rule_missing_premise[0,i]
return [['Some',i,i], tree_by_numberssome(i,k), 'Some Rule']
if justificationsome[i,j] == 15:
k=Darii_missing[i,j]
return [['Some',i,j], tree_by_numbers(k,j), tree_by_numberssome(i,k), 'Darii']
if justificationsome[i,j] == 16:
k=Card_Some_missing[i,j]
return [['Some',i,j],
tree_by_numberssome(k,k), tree_by_numbersc(k,i), 'Card-Some']
if justificationsome[i,j] == 17:
jneg = neg_internal[j]
return [['Some',i,j], tree_by_numbersmore(jneg,i), 'More-Some']
# tipping is gone
if justificationsome[i,j] == 10:
jneg = neg_internal[j]
ineg = neg_internal[i]
return [['Some',i,j], tree_by_numbersc(neg_internal[i],i),
tree_by_numbersc(neg_internal[j],j),
tree_by_numberssome(neg_internal[i],neg_internal[j]), 'Maj']
if justificationsome[i,j] == 24:
k = Int_rule_missing_premise[0,i]
return [['Some',i,i], tree_by_numberssome(k,k),
tree_by_numbersc(neg_internal[i],i), 'Integer']
else:
return "Problem in the justification for", 'some', i, "are", j
def tree_by_numbersmore(i,j):
if justificationmore[i,j] == 2:
return [['More',j,i],'Assumption']
if justificationmore[i,j] == 21:
k = More_Right_cut_premise[i,j]
return [['More',j,i], tree_by_numbersc(k,j),tree_by_numbersmore(i,k), 'More-Right']
if justificationmore[i,j] == 20:
k = More_Left_cut_premise[i,j]
return [['More',j,i], tree_by_numbersmore(k,j),tree_by_numbersc(i,k), 'More-Left']
if justificationmore[i,j] == 22:
k = neg_internal[i]
l = neg_internal[j]
return [['More',j,i], tree_by_numbersmore(l,k), 'More Antitone']
if justificationmore[i,j] == 23:
l = neg_internal[i]
return [['More',j,i],tree_by_numbers(i,j), tree_by_numberssome(j,l), 'More']
if justificationmore[i,j] == 25:
return [['More',j,i], tree_by_numbersc(i,neg_internal[i]),tree_by_numbersmore(neg_internal[j],j), 'Strict Half']
else:
return "Problem in the MORE justification for", i, "and", j
def size(t):
if last(t) == 'Assumption' or last(t) == "Axiom":
return 1
else:
smallt = t[1:len(t)-1]
return 1 + sum(map(size,smallt))
def negation(pvar):
if pvar[1] == pos:
return tuple([pvar[0],neg])
if pvar[1] == neg:
return tuple([pvar[0],pos])
def sentence_negation(phi):
if phi[0] == 'All':
return ['Some',phi[1],negation(phi[2])]
if phi[0] == 'Some':
return ['All',phi[1],negation(phi[2])]
if phi[0] == 'At least':
return ['More',phi[2],phi[1]]
if phi[0] == 'More':
return ['At least',phi[2],phi[1]]
def simple(pvar):
if pvar[1] == pos:
return pvar[0]
else:
#return (non,pvar[0])
return 'non-'+pvar[0]
def spell(word,first,second): # word is either 'All' or 'Some' or 'At least'
pvar1 = lis[first]
pvar2 = lis[second]
if pvar1[1] == pos and pvar2[1] == neg and word == 'All':
return ['No', simple(pvar1), 'are', simple(negation(pvar2))]
elif word == 'At least':
return ['At least as many',simple(pvar1), 'as', simple(pvar2)]
elif word == 'More':
return ['There are more',simple(pvar1), 'than', simple(pvar2)]
elif word == 'All' or word == 'Some':
return [word, simple(pvar1),'are', simple(pvar2)]
#if pvar1[1] == pos and pvar2[1] == pos and (word == 'All' or word == 'Some'):
# return [word, pvar1[0], 'are', pvar2[0]]
#if pvar1[1] == pos and pvar2[1] == neg and word == 'Some':
# return ['Some', pvar1[0], 'are', ('non',pvar2[0])]
#if pvar1[1] == neg and pvar2[1] == pos:
# return [word, ('non',pvar1[0]), 'are', pvar2[0]]
#if pvar1[1] == neg and pvar2[1] == neg:
# return [word, ('non',pvar1[0]), 'are', ('non',pvar2[0])]
else:
print "error in spell"
def flatten(t,i):
# flattens a proof tree into a list
# the cases depend on the number of premises: 0,1,2, or 3
if last(t) == 'Axiom' or last(t) == 'Assumption':
return ([[1+i] + spell(t[0][0],t[0][1],t[0][2]) + [table([last(t)])]])
elif last(t) == 'Antitone' or last(t) == 'Zero' or last(t) == 'One' or last(t) == 'Subset Card' \
or last(t) == 'Card Antitone' or last(t) == 'Conversion' or last(t)== 'Some Rule' \
or last(t)== 'More-Some' or last(t)== 'More-Card' or last(t)== 'More Antitone':
return (flatten(t[1],i) + [[size(t) +i] + spell(t[0][0],t[0][1],t[0][2]) + [table([last(t),size(t)+i-1])]])
elif last(t) == 'Card Mix' or last(t) == 'Barbara' or last(t) == 'Card Trans' \
or last(t) == 'Darii' or last(t) == 'Half' or last(t) == 'Card-Some' \
or last(t)== 'More-Left' or last(t)== 'More-Right' or last(t) == 'Strict Half' \
or last(t) == 'Tipping' or last(t) == 'More' or last(t) == 'Integer':
return (flatten(t[1],i) + \
flatten(t[2],i+size(t[1])) +\
[[i + size(t)] + spell(t[0][0],t[0][1],t[0][2]) + [table([last(t),size(t[1])+i,size(t)+i-1])]] \
)
elif last(t) == 'Maj':
return (flatten(t[1],i) + \
flatten(t[2],i+size(t[1])) +\
flatten(t[3],i+size(t[1]) + size(t[2])) +\
[[i + size(t)] + spell(t[0][0],t[0][1],t[0][2]) + \
[table([last(t),size(t[1])+i,i+size(t[1]) + size(t[2]), size(t)+i-1])]] \
)
def read(Gamma):
global mg
global mgc
global mgsome
global mgmore
global justification
global justificationc
global justificationsome
global justificationmore
global proof_length
global proof_lengthc
global proof_lengthsome
global proof_lengthmore
global Barbara_cut_premise
global card_transitivity_cut_premise
global Some_rule_missing_premise
global Int_rule_missing_premise
global Card_Some_missing
global Darii_missing
global More_Left_cut_premise
global More_Right_cut_premise
global dim
global n
global lis
mg = matrix(ZZ,n,n,0)
mgc = matrix(ZZ,n,n,0)
mgsome = matrix(ZZ,n,n,0)
mgmore = matrix(ZZ,n,n,0)
justification = matrix(ZZ,n,n,0)
justificationc = matrix(ZZ,n,n,0)
justificationsome = matrix(ZZ,n,n,0)
justificationmore = matrix(ZZ,n,n,0)
proof_length = matrix(ZZ,n,n,0)
proof_lengthc = matrix(ZZ,n,n,0)
proof_lengthsome = matrix(ZZ,n,n,0)
proof_lengthmore = matrix(ZZ,n,n,0)
Barbara_cut_premise = matrix(ZZ,n,n,0)
card_transitivity_cut_premise = matrix(ZZ,n,n,0)
Card_Some_missing = matrix(ZZ,n,n,0)
More_Left_cut_premise = matrix(ZZ,n,n,0)
More_Right_cut_premise = matrix(ZZ,n,n,0)
Some_rule_missing_premise = matrix(ZZ,1,n,0)
Darii_missing = matrix(ZZ,n,n,0)
Int_rule_missing_premise = matrix(ZZ,1,n,0)
for item in Gamma:
if item[0] == 'All':
i = lis.index(item[1])
j = lis.index(item[2])
mg[i,j] = 1
justification[i,j] = 2
proof_length[i,j] = 1
elif item[0] == 'At least':
## At least as many i as j ===> mgc[j,i] = 1
## That is, bigger sets are the targets
i = lis.index(item[1])
j = lis.index(item[2])
mgc[j,i] = 1
justificationc[j,i] = 2
proof_lengthc[j,i] = 1
elif item[0] == 'Some':
i = lis.index(item[1])
j = lis.index(item[2])
mgsome[i,j] = 1
justificationsome[i,j] = 2
proof_lengthsome[i,j] = 1
elif item[0] == 'More':
i = lis.index(item[1])
j = lis.index(item[2])
mgmore[j,i] = 1
justificationmore[j,i] = 2
proof_lengthmore[j,i] = 1
else:
print "ERROR READING GAMMA"
#print "mgc here is"
#print mgc
n = len(lis)
def make_matrices(Gamma):
#global mgc
#global mgsome
#global mgmore
global small_or_half
small_or_half = []
global large_or_half
large_or_half = []
global large_or_half_original
large_or_half_original = []
global small
small = []
global large
large = []
global half
half = []
for i in range(n):
mg[i,i] = 1
justification[i,i] = 1
proof_length[i,i]=1
old_max = -1
new_max = 0
while old_max < new_max:
old_max = new_max
Apply_using_numbers()
m1 = proof_length.height()
m2 = proof_lengthc.height()
m3 = proof_lengthsome.height()
m4 = proof_lengthmore.height()
new_max = max([m1,m2,m3,m4])
#time_limit = len(pvars)
#for tick in range(time_limit):
#Apply_using_numbers()
#Rule_filter(10) #
## checked for 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 23, 25
if verbosity >= 3:
print "The sets called small, large, and half start out as:"
print "small", [lis[ind] for ind in small]
print "large", [lis[ind] for ind in large]
print "half", [lis[ind] for ind in half]
print ""
def rule_filter(num):
global mg
global mgc
global mgsome
global mgmore
global justification
global justificationc
global justificationsome
global justificationmore
if (num == 10) or (num == 13) or (num == 17) or (num == 14) or (num == 15)\
or (num == 16) or (num == 24) or (num == 19):
for i in range(n):
for j in range(n):
if justificationsome[i,j]==num:
justificationsome[i,j]=0
mgsome[i,j] = 0
if (num == 7) or (num == 9) or (num == 12) or (num == 18) or (num == 20) or (num == 21) or (num == 18):
for i in range(n):
for j in range(n):
if justificationc[i,j]==num:
justificationc[i,j]=0
mgc[i,j] = 0
if (num == 20) or (num == 21) or (num == 23) or (num == 25):
for i in range(n):
for j in range(n):
if justificationmore[i,j]==num:
justificationmore[i,j]=0
mgmore[i,j] = 0
if (num == 8) or (num == 11) :
for i in range(n):
for j in range(n):
if justification[i,j]==num:
justification[i,j]=0
mg[i,j] = 0
def sdagger(Gamma,phi): ## works for Gamma and phi sentences in 'All' and 'At least'
global variables
global polarized_variables
global pvars
global Is
Is = []
global Ms
Ms = []
make_eyes(Gamma)
global edgeset_pre
global zeros
global ones
global pre_points
global outtree
global firstlist
global secondlist
global thirdlist
global fourthlist
global n
make_variables(Gamma+[phi])
global g
global g_c
global lis
global lis_c
g = DiGraph({var:Set([var]) for var in pvars})
g_c = DiGraph({var:Set([var]) for var in pvars})
lis = g.vertices()
n = len(lis)
lis_c = g_c.vertices()
global neg_internal
neg_internal = {i:lis.index(negation(lis[i])) for i in range(n)}
dim = len(g.vertices())
read(Gamma)
make_matrices(Gamma)
cp = cartesian_product([range(n),range(n)])
badlist = [item for item in cp if mgsome[item[0],item[1]] == 1 and mg[item[0],neg_internal[item[1]]] ==1 ]
badlist2 = [item for item in cp if mgmore[item[0],item[1]] == 1 and mgc[item[1],item[0]] ==1 ]
if badlist2 != []:
if verbosity >= 1:
print "The assumptions are inconsistent due to cardinality problems."
if verbosity >= 2:
print "So the conclusion follows from a contradiction:"
print ""
qq = range(len(badlist2))
lengths = map(lambda it: proof_lengthmore[badlist2[it][1],badlist2[it][0]]
+ proof_lengthc[badlist2[it][0],badlist2[it][1]],qq)
MIN = min(lengths)
found_index = [ind for ind in qq if lengths[ind] == MIN][0]
num0 = badlist2[found_index][0]
num1 = badlist2[found_index][1]
tree_non_strict = tree_by_numbersc(num0,num1)
tree_strict = tree_by_numbersmore(num1,num0)
size_of_tree_non_strict = size(tree_non_strict)
size_of_tree_strict = size(tree_strict)
size_total = size_of_tree_non_strict + size_of_tree_strict
flattened_tree_strict = flatten(tree_strict,0)
flattened_tree_non_strict = flatten(tree_non_strict,size_of_tree_strict)
conclusion = [[size_total + 1] +
spell(phi[0], lis.index(phi[1]), lis.index(phi[2])) +
[table(['X',size_of_tree_strict,size_total])]]
flattened_tree = flattened_tree_strict + flattened_tree_non_strict + conclusion
print table(flattened_tree)
elif badlist != []:
if verbosity >= 1:
print "The assumptions are inconsistent."
print "So the conclusion follows from a contradiction:"
print ""
if verbosity >= 2:
qq = range(len(badlist))
lengths = map(lambda it: proof_lengthsome[badlist[it][0],
badlist[it][1]] + \
proof_length[badlist[it][0],neg_internal[badlist[it][1]]],qq)
MIN = min(lengths)
found_index = [ind for ind in qq if lengths[ind] == MIN][0]
num0 = badlist[found_index][0]
num1 = badlist[found_index][1]
tree_some = tree_by_numberssome(num0,num1)
tree_no = tree_by_numbers(num0,neg_internal[num1])
size_of_tree_some = size(tree_some)
size_of_tree_no = size(tree_no)
size_total = size_of_tree_some + size_of_tree_no
flattened_tree_some = flatten(tree_some,0)
flattened_tree_no = flatten(tree_no,size_of_tree_some)
conclusion = [[size_total + 1] +
spell(phi[0], lis.index(phi[1]), lis.index(phi[2])) +
[table(['X',size_of_tree_some,size_total])]]
flattened_tree = flattened_tree_some + flattened_tree_no + conclusion
print table(flattened_tree)
elif phi[0] == 'All':
i = lis.index(phi[1])
j = lis.index(phi[2])
if mg[i,j] == 1:
if verbosity >= 1:
does_it_follow()
if verbosity >= 2:
print(table(flatten(tree_by_numbers(i,j),0)))
else:
does_not_follow()
Gamma = Gamma + [sentence_negation(phi)]
Is = []
Ms = []
make_eyes(Gamma+ [sentence_negation(phi)])
read(Gamma)
make_matrices(Gamma+ [sentence_negation(phi)])
cardinality_helpers()
builder()
elif phi[0] == 'At least':
i = lis.index(phi[1])
j = lis.index(phi[2])
if mgc[j,i] == 1:
if verbosity >= 1:
does_it_follow()
if verbosity >= 2:
print table(flatten(tree_by_numbersc(j,i),0))
else:
does_not_follow()
make_eyes(Gamma+ [sentence_negation(phi)])
read(Gamma+ [sentence_negation(phi)])
make_matrices(Gamma+ [sentence_negation(phi)])
cardinality_helpers()
builder()
elif phi[0] == 'Some':
i = lis.index(phi[1])
j = lis.index(phi[2])
if mgsome[i,j] == 1:
if verbosity >= 1:
does_it_follow()
if verbosity >= 2:
print table(flatten(tree_by_numberssome(i,j),0))
else:
does_not_follow()
Is = []
Ms = []
make_eyes(Gamma+ [sentence_negation(phi)])
read(Gamma+ [sentence_negation(phi)])
make_matrices(Gamma+ [sentence_negation(phi)])
cardinality_helpers()
builder()
elif phi[0] == 'More':
i = lis.index(phi[1])
j = lis.index(phi[2])
if mgmore[j,i] == 1:
if verbosity >= 1:
does_it_follow()
if verbosity >= 2:
print table(flatten(tree_by_numbersmore(j,i),0))
else:
does_not_follow()
Is = []
Ms = []
make_eyes(Gamma+ [sentence_negation(phi)])
read(Gamma+ [sentence_negation(phi)])
make_matrices(Gamma+ [sentence_negation(phi)])
cardinality_helpers()
builder()
def cardinality_helpers():
global small
global large
global small_or_half
global large_or_half
if verbosity >=3:
print "At this point, we need to divide the set of polarized variables"
print "into three subsets called small, large, and half. Before we do that, we have:"
print ""
print "small:", [lis[index] for index in small]
print "large:", [lis[index] for index in large]
print "half:", [lis[index] for index in half]
print ""
for x in variables:
x_pos_as_number = lis.index((x,pos))
x_neg_as_number = lis.index((x,neg))
if not(x_pos_as_number in half) and not(x_neg_as_number in half):
if exists(large_or_half, lambda y: mgc[y][x_pos_as_number])[0]:
large = list(Set(large).union(Set([x_pos_as_number])))
small = list(Set(small).union(Set([x_neg_as_number])))
else:
large = list(Set(large).union(Set([x_neg_as_number])))
small = list(Set(small).union(Set([x_pos_as_number])))
large_or_half = list(Set(large).union(Set(half)))
small_or_half = list(Set(small).union(Set(half)))
if verbosity >=3:
print "The algorithm completes the division, giving"
print "small:", [lis[index] for index in small]
print "large:", [lis[index] for index in large]
print "half:", [lis[index] for index in half]
print ""
def builder():
## builds a model of a list of Is, respecting graph1 but not leq information
global pre_points
global iterator1
global preliminary_model ## maybe delete this global declaration
global N
N = len(Is)
#print "Is", Is, "N",N
from sage.misc.lazy_list import lazy_list
from itertools import count
#mpp = lazy_list(count(),start=N)
# the line above was taken out on May 9
mpp = lazy_list(N+i for i in count())
iterator1 = iter(mpp)
#print "the iterators starts at",N
pre_points = {i:Set([Is[i][0],Is[i][1]]) for i in range(N)}
for i in range(N):
for var in variables:
if exists(pre_points[i], lambda xvar:\
mg[lis.index(xvar)][lis.index((var,pos))] == 1)[0]:
pre_points.update({i:pre_points[i].union(Set([(var,pos)]))})
else:
pre_points.update({i: pre_points[i].union(Set([(var,neg)]))})
#print "pre_points at the first semantics:"
#for index in range(N):
# print index, pre_points[index]
#print ""
semantics = {var:Set([i for i in range(N) if (var,pos) in pre_points[i]]) for var in variables}
M = Set(range(N))
if verbosity == 3:
print "The first semantics, based on the existential assumptions:"
print table(list([[x,semantics[x],M.difference(semantics[x])] for x in variables]))
print "The universe is", M
print ""
def f(x):
return Set([i for i in range(N) if lis[x] in pre_points[i]])
global s_or_h
s_or_h = small_or_half
#print "s_or_h is:",s_or_h
preliminary_model = {xx:f(xx) for xx in s_or_h}
global G1
G1 = DiGraph({x:Set([y for y in s_or_h if mg[x][y]==1 ]) for x in s_or_h})
#print "G1:", G1.edges()
#print ""
G2 = DiGraph({x:Set([y for y in s_or_h if mgc[x][y]==1 ]) for x in s_or_h})
#print "G2:",G2.edges()
#print ""
G3= DiGraph({x:Set([y for y in s_or_h if mgmore[x][y]==1 ]) for x in s_or_h})
model_builder(G1,s_or_h,G2,G3,preliminary_model)
def model_builder(G,vars,cgraph,mgr,preliminary_model):
from sage.misc.lazy_list import lazy_list
from itertools import count
m = len(vars)
global Goal
Goal = preliminary_model
Include = {xx: Set([yy for yy in vars if mg[yy,xx]==1 ]) for xx in vars}
## it had been (mg[y,x]==1 and mg[x][y]==0)
#print "Include = "
#print Include