-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils.pl
6879 lines (6112 loc) · 245 KB
/
utils.pl
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
%%- -*-Mode: Prolog;-*--------------------------------------------------
%%
%% $Revision: 1.151 $
%%
%% File : utils.pl
%%
%% Author: Josef Urban
%%
%% MPTP2 Prolog utilities, tested only with SWI Prolog 5.2 now.
%% The top-level predicate is now mk_nonnumeric/0
%% or mk_first100/0, see below. You also need to set the mml_dir/1
%% location here appropriately.
%% The top-level predicate for createing MPTPChallenge problems is e.g.
%% :- mk_problems_from_file('mptp_chall_problems').
%%------------------------------------------------------------------------
%% TODO: These two are only needed for formula abstraction 0 currently an AI experiment.
%% Can be safely removed if ausing problems.
:- use_module(library(assoc)).
:- use_module(library(ordsets)).
%%%%%%%%%%%%%%%%%%%% Settings %%%%%%%%%%%%%%%%%%%%
%% set this to the location of Prolog files created from MML
%% ('pl' directory in the distro).
%mml_dir("/home/urban/miztmp/distro/pl/").
%mml_dir("/home/urban/mptp/pl/").
mml_dir("/home/urban/mizwrk/7.13.01_4.181.1147/MPTP2/pl/").
%mml_dir("/home/urban/rsrch/MPTP2/pl/").
%mml_dir("/big/urban/miztmp/mml3/tmp/").
mml_dir_atom(A):- mml_dir(S), string_to_atom(S,A).
%% version of MML needed for requirements (encoding of numbers)
%mml_version([4,48,930]).
mml_version([4,181,1147]).
%mml_version([4,100,1011]).
%% switch to fail for debug
optimize_fraenkel. % :- fail.
%% appends __Article to local constants, lemmas, etc. - use it for
%% mixing local stuff from different articles consistently.
%% (all atoms starting with c[0-9]+_
absolute_locals. %%:- fail.
%% special articles not in mml.lar - changed in 1191; we omit hidden
mml_added_spc([tarski]):- mml_version([_,_,X]), X < 1191,!.
mml_added_spc([tarski_0, tarski_a]).
%% debugging, Flags can be: [dbg_FRAENKELS,dbg_CLUSTERS,dbg_REQS,dbg_FIXPOINT,dbg_LEVEL_REFS]
dbg_flags([]).
dbg(Flag, Goal):-
dbg_flags(Flags), member(Flag, Flags), !, Goal.
dbg(_,_).
%%%%%%%%%%%%%%%%%%%% End of settings %%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%% Options %%%%%%%%%%%%%%%%%%%%
opt_available([opt_REM_SCH_CONSTS, %% generalize local constants in scheme instances
opt_LEMMA_GLOBAL_GEN, %% MoMM-like export of proof-local lemmas by generelizing
opt_MK_TPTP_INF, %% better tptp inference slot and no mptp_info
opt_TPTP_SHORT, %% short tptp format (parsable by vampire too)
opt_NO_EX_BG_FLAS, %% do not include existential background in fixpoint
opt_FXP_CHECK_CNSDR, %% add rclusters for suspisous consider_justifications
opt_ADDED_NON_MML, %% unary functor passing a list of nonMML article names
opt_NON_MML_DIR, %% unary functor passing a nonstandard directory for the article
opt_LOAD_MIZ_ERRORS, %% import err2 file with Mizar's errors
opt_PROB_PRINT_FUNC, %% unary functor passing a special printing func
opt_SORT_TRANS_FUNC, %% unary functor passing a special sort transformation func
opt_USE_ORIG_SCHEMES, %% use the original schemes instead of their instances in the problem
opt_PRINT_PROB_PROGRESS, %% print processed problems to stdout
opt_ARTICLE_AS_TPTP_AXS, %% print the whole article as tptp axioms
opt_PP_SMALLER_INCLUDES, %% unary functor passing a list of possible includes,
%% used with print_refs_as_tptp_includes
opt_TPTPFIX_ND_ROLE, %% make the role acceptable to GDV
opt_ADD_INTEREST, %% add interestingness to useful info
opt_LINE_COL_NMS, %% problem are named LINE_COL instead
opt_CONJECTURE_NMS, %% problem are named as its conjecture
opt_LEVEL_REF_INFO, %% .refspec file with immediate references is printed
opt_ALLOWED_REF_INFO, %% .allowed_local file with accessible references is printed
opt_PROVED_BY_INFO, %% .proved_by0 file with orig refs (no bg) printed
opt_DBG_LEVS_POS, %% print a debugging info for levels and positions
opt_DBG_ART_POS, %% print a debugging info for article positions
opt_NO_FRAENKEL_CONST_GEN, %% do not generalize local consts when abstracting fraenkels
%% (useful for fast translation, when consts are not loaded)
opt_LEARN_EDGE, %% print fromula as labeled graph edges for learning
opt_LEARN_STDFILES, %% print the symnr and refnr files without the prolog syntax
opt_LEARN_SYMS_SMALL %% symbols are numbered starting from 0 instead of references
]).
%%%%%%%%%%%%%%%%%%%% End of options %%%%%%%%%%%%%%%%%%%%
zip([],[],[]).
zip([H|T],[H1|T1],[[H,H1]|T2]):- zip(T,T1,T2).
zip_s(_,[],[],[]).
zip_s(S,[H|T],[H1|T1],[Trm|T2]):-
Trm =.. [S,H,H1],
zip_s(S,T,T1,T2).
% when the second is list of lists
zip1([],[],[]).
zip1([H|T],[H1|T1],[[H|H1]|T2]):- zip1(T,T1,T2).
append_l(In,Out):- reverse(In,In1),append_l1(In1,[],Out).
append_l1([],P,P).
append_l1([H|T],Done,Res):-
append(H,Done,Res1),
append_l1(T,Res1,Res).
%% nonempty_intersection(Set1, Set2) (not used for anything now)
nonempty_intersection([H|_],Set2):- memberchk(H,Set2),!.
nonempty_intersection([_|T],Set2):- nonempty_intersection(T,Set2),!.
%% succeed once or throw ecxeption
ensure(Goal, Exception):- Goal,!; throw(Exception).
%% equivalence classes under binary predicate P
%% eqclasses(EquivalencePredicate, InputList, EqClasses)
eqclasses(_,[],[]).
eqclasses(P,[H|T],[E_h|O_t]):-
eqcl1(P,H,[H|T],E_h,N_h),
eqclasses(P,N_h,O_t).
% eqcl1(EquivalencePredicate, Member, InputList, Equivalent, NonEquivalent)
eqcl1(_,_,[],[],[]).
eqcl1(P,H1,[H2|I],[H2|O],R):-
apply(P,[H1,H2]) -> eqcl1(P,H1,I,O,R),!.
eqcl1(P,H1,[H2|I],O,[H2|R]):-
eqcl1(P,H1,I,O,R).
% insert into P-eqclasses (keeping copies)
% eqc_insert(EquivalencePredicate, Member, InputEqClasses, OutputEqClasses)
eqc_insert(_,M,[],[[M]]).
eqc_insert(P,M,[[M1|H]|T],[[M,M1|H]|T]):-
apply(P,[M,M1]),!.
eqc_insert(P,M,[H|T],[H|T1]):-
eqc_insert(P,M,T,T1).
% stolen from tptp2X.main
%----Runtime version of operators
declare_TPTP_operators:-
op(99,fx,'$'),
op(100,fx,++),
op(100,fx,--),
op(100,xf,'!'),
op(400,fx,'^'),
op(405,xfx,'='),
op(405,xfx,'~='),
op(450,fy,~),
op(501,yfx,'@'),
op(502,xfy,'|'),
op(502,xfy,'~|'),
op(503,xfy,&),
op(503,xfy,~&),
op(504,xfy,=>),
op(504,xfy,<=),
op(505,xfy,<=>),
op(505,xfy,<~>),
%----! and ? are of higher precedence than : so !X:p(X) is :(!(X),p(X))
%----Otherwise !X:!Y:p(X,Y) cannot be parsed.
op(400,fx,!),
op(400,fx,?),
%----Need : stronger than + for equality and otter in tptp2X
%----Need : weaker than quantifiers for !X : ~p
op(450,xfy,:),
%---- .. used for range in tptp2X. Needs to be stronger than :
op(400,xfx,'..').
% untested - for newer swi
declare_TPTP_operators1:-
op(99,fx,'$'),
op(100,fx,++),
op(100,fx,--),
op(100,xf,'!'),
op(400,fx,'^'),
op(405,xfx,'='),
op(405,xfx,'~='),
op(450,fy,~),
op(501,yfx,'@'),
(system_mode(true),op(502,xfy,'|'),system_mode(false)),
op(502,xfy,'~|'),
op(503,xfy,&),
op(503,xfy,~&),
op(504,xfy,=>),
op(504,xfy,<=),
op(505,xfy,<=>),
op(505,xfy,<~>),
%----! and ? are of higher precedence than : so !X:p(X) is :(!(X),p(X))
%----Otherwise !X:!Y:p(X,Y) cannot be parsed.
op(400,fx,!),
op(400,fx,?),
%----Need : stronger than + for equality and otter in tptp2X
%----Need : weaker than quantifiers for !X : ~p
op(450,xfy,:),
%---- .. used for range in tptp2X. Needs to be stronger than :
op(400,xfx,'..').
:- declare_TPTP_operators.
logic_syms([++,--,'@','$',~,'|','~|',true,&,~&,=>,<=,<=>,<~>,!,?,:,'..',sort,all,'.',[]]).
%% uncomment this for E prover versions earlier than 0.9
% portray(A = B):- format(' equal(~p,~p) ',[A,B]).
portray(A @ B):- format(' (~p @ ~p) ',[A,B]).
portray(~A):- write(' ~ ('), print(A), write(') ').
portray(A & B):- format(' (~p & ~p) ',[A,B]).
portray(A | B):- format(' (~p | ~p) ',[A,B]).
portray(A => B):- format(' (~p => ~p) ',[A,B]).
portray(A <=> B):- format(' (~p <=> ~p) ',[A,B]).
portray(A : B):- var(A), format(' ( ~p : ~p) ',[A,B]).
portray(! A : B):- format(' (! ~p : ~p) ',[A,B]).
portray(? A : B):- format(' (? ~p : ~p) ',[A,B]).
portray('^' A : B):- format(' (^ ~p : ~p) ',[A,B]).
portray(A):- atom(A), constr_name1(A,_,_,S,_), write(S).
portray(A):- atom(A), constr_name(A,Name,Quote),
((Quote==0, write(Name));
(Quote==1, write(''''),write(Name),write(''''))).
portray(A):- atom(A), abs_name(A,Name), write(Name).
portray(A):- compound(A), A =.. [F|L], constr_name(F,Name,Quote),
((Quote==0, write(Name));
(Quote==1, write(''''),write(Name),write(''''))),
write('('), print_many(L), write(')').
% this creates the constr_name1 predicate for pretty-printing (see mk_constr_names1.pl for 00constrnamesarity):
% urban@zen:~/gr/MPTP2$ ./mk_constr_names1.pl 00constrnamesarity > foo1.pl
% [foo1].
portray(A):-
compound(A), A =.. [F|L], constr_name1(F,LA,RA,S,RS),
length(L,NL),
(RA=circumfix ->
FirstPos is NL - LA,
write(S), print_many_from(FirstPos,L), write(RS)
;
TA is LA + RA,
FirstPos is NL - TA,
(LA=0 -> Rest = L
;
write('('), print_many_from_times(FirstPos,LA,L,Rest), write(')')
),
write(S),
(Rest = [] -> true
;
write('('), print_many(Rest), write(')')
)
).
print_many([]).
print_many([X]):- print(X),!.
print_many([X|Y]):- print(X), write(','), print_many(Y).
print_many_from(_,[]):-!.
print_many_from(0,L):-!, print_many(L).
print_many_from(N,[_|T]):- N1 is N - 1, print_many_from(N1, T).
print_many_times(_,[],[]):-!.
print_many_times(0,L,L):- !.
print_many_times(1,[X|Y],Y):- !, print(X).
print_many_times(N,[X|Y],Res):- !, N1 is N - 1, print(X), write(','), print_many_times(N1,Y,Res).
% print from the From position Times elements, return Res
print_many_from_times(_,_,[],[]):-!.
print_many_from_times(_,0,L,L):-!.
print_many_from_times(0,Times,L,Res):-!, print_many_times(Times,L,Res).
print_many_from_times(N,Times,[_|T],Res):- N1 is N - 1, print_many_from_times(N1, Times, T, Res).
% explain tstp parsing
d2l(X,X):- atomic(X);var(X).
d2l([],[]).
d2l(X,Y):- X =.. Z, d2ls(Z,Y).
d2ls([],[]).
d2ls([H|T],[H1|T1]):- d2l(H,H1), d2ls(T,T1).
term2list(X,X):- (atomic(X);var(X)),!.
term2list(X,[H|T1]):- X =.. [H|T], maplist(term2list,T,T1).
declare_mptp_predicates:-
abolish(fof/4),
abolish(fof/5),
abolish(theory/2),
abolish(constr_name/3),
abolish(constr_name/5),
multifile(fof/4),
multifile(fof/5),
dynamic(fof/5),
dynamic(constr_name/3),
abolish(constr_name1/5),
multifile(constr_name/3),
multifile(constr_name1/5),
multifile(theory/2),
abolish(fof_name/2),
abolish(fof_file/2),
abolish(fof_eq_def/2),
abolish(fof_sort_def/2),
abolish(fof_pred_def/2),
abolish(fof_section/2),
abolish(fof_level/2),
abolish(fof_toplevel/2),
abolish(fof_newlevel/3),
dynamic(fof_newlevel/3),
abolish(fof_parentlevel/2),
dynamic(fof_parentlevel/2),
abolish(fof_cluster/3),
dynamic(fof_cluster/3),
abolish(fof_identifyexp/3),
dynamic(fof_identifyexp/3),
abolish(rc_syms_for_consider/7),
abolish(fof_req/3),
dynamic(fof_req/3),
abolish(fof_syms/2),
dynamic(fof_syms/2),
abolish(sym_ref_graph/2),
dynamic(sym_ref_graph/2),
abolish(fof_ante_sym_cnt/4),
dynamic(fof_ante_sym_cnt/4),
abolish(abs_name/2),
dynamic(abs_name/2),
abolish(miz_errors/1),
dynamic(miz_errors/1),
abolish(fof_redefines/4),
dynamic(fof_redefines/4),
abolish(fraenkel_cached/3),
dynamic(fraenkel_cached/3),
abolish(fraenkels_loaded/1),
dynamic(fraenkels_loaded/1),
abolish(sch_orig_copy/2),
dynamic(sch_orig_copy/2),
abolish(articles_numbered/1),
dynamic(articles_numbered/1),
abolish(article_position/2),
dynamic(article_position/2),
abolish(fxp_refsin_/1),
dynamic(fxp_refsin_/1),
abolish(fxp_allsyms_/1),
dynamic(fxp_allsyms_/1),
abolish(zerotyp/2),
multifile(zerotyp/2),
dynamic(zerotyp/2),
abolish(nonzerotyp/2),
multifile(nonzerotyp/2),
dynamic(nonzerotyp/2),
abolish(rec_sch_inst_name/2),
dynamic(rec_sch_inst_name/2).
% index(fof(1,1,0,1,1)),
% index(fof(1,1,0,1)).
%% this makes things very slow, do not use it
% index(fof_name(1,1)).
%% collect subterms satisfying P with count, test by equality
collect_with_count_top(Pred,Term,OutList,OutCounts):-
collect_with_count(Pred,Term,[],[],OutList,OutCounts),!.
collect_with_count(P,X,In,InC,Out,OutC):-
(var(X);atomic(X)), !, collect_with_count1(P,X,In,InC,Out,OutC).
%% neglects the head functor!
collect_with_count(P,X,In,InC,Out,OutC):-
collect_with_count1(P,X,In,InC,Out1,OutC1),
X =.. [_|T1],
collect_with_countl(P,T1,Out1,OutC1,Out,OutC).
collect_with_countl(_,[],In,InC,In,InC).
collect_with_countl(P,[H|T],In,InC,Out,OutC):-
collect_with_count(P,H,In,InC,Out1,OutC1),
collect_with_countl(P,T,Out1,OutC1,Out,OutC).
%% do the collecting
collect_with_count1(P,X,In,InC,Out,OutC):- apply(P,[X]),!,
(enth1(N,In,X) ->
(Out = In, remove_at(C_X,InC,N,Tmp),
succ(C_X,C1_X), insert_at(C1_X,Tmp,N,OutC));
(Out = [X|In], OutC = [1|InC])).
collect_with_count1(_,_,In,InC,In,InC).
%% exact select
eselect(A, [C|B], B):- A == C,!.
eselect(A, [B|C], [B|D]):- eselect(A, C, D).
%% exact nth1
enth1(A, B, C):- var(A), !, enth_gen(B, C, 1, A).
enth_gen([A|_], A1, C, C):- A == A1.
enth_gen([_|B], C, D, E) :-
succ(D, F),
enth_gen(B, C, F, E).
%% remove K-th element (1-based)
% remove_at(X,L,K,R) :- X is the K'th element of the list L; R is the
% list that remains when the K'th element is removed from L.
% (element,list,integer,list) (?,?,+,?)
remove_at(X,[X|Xs],1,Xs).
remove_at(X,[Y|Xs],K,[Y|Ys]) :- K > 1,
K1 is K - 1, remove_at(X,Xs,K1,Ys).
% Insert an element at a given position into a list (1-based)
% insert_at(X,L,K,R) :- X is inserted into the list L such that it
% occupies position K. The result is the list R.
% (element,list,integer,list) (?,?,+,?)
insert_at(X,L,K,R) :- remove_at(X,R,K,L).
% Split a list into two parts
% split(L,N,L1,L2) :- the list L1 contains the first N elements
% of the list L, the list L2 contains the remaining elements.
% (list,integer,list,list) (?,+,?,?)
split(L,0,[],L).
split([X|Xs],N,[X|Ys],Zs) :- N > 0, N1 is N - 1, split(Xs,N1,Ys,Zs).
%% Digit is a digit character
%% digit(Digit):- member(Digit, ['0','1','2','3','4','5','6','7','8','9']).
digit('0'). digit('1'). digit('2'). digit('3'). digit('4').
digit('5'). digit('6'). digit('7'). digit('8'). digit('9').
%% mptp_func with args
mptp_func(X):- X =..[H|_],mptp_func_sym(H).
ground_mptp_func(X):- ground(X),mptp_func(X).
sch_symbol(X):- atom_chars(X,[F|_]),member(F,[f,p]).
mptp_local_const(X):- atom_chars(X,[c|_]).
mptp_func_sym(X):- atom_chars(X,[F|_]),member(F,[k,g,u,'0','1','2','3','4','5','6','7','8','9']).
mptp_attr_sym(X):- atom_chars(X,[v|_]).
mptp_mode_sym(X):- atom_chars(X,[F|_]),member(F,[m,l]).
mptp_attr_or_mode_sym(X):- atom_chars(X,[F|_]),member(F,[v,m,l]).
mptp_req_name(X):- atom_chars(X,[r,q|_]).
mptp_fraenkel_sym(X):- atom_chars(X,[a|_]).
mptp_choice_sym(X):- atom_chars(X,[o|_]).
mptp_sch_func_sym(X):- atom_chars(X,[f|_]).
mptp_sch_pred_sym(X):- atom_chars(X,[p|_]).
% X/_ is an MPTP object translated as tptp functor
mptp_tptp_func_arity(X/_):- atom_chars(X,[F|_]),member(F,[k,g,u,c,f,a,o,'0','1','2','3','4','5','6','7','8','9']).
% avoids "f" and "p" - left to the scheme variant below
mptp_tptp_func_arity_thf(X/_):- atom_chars(X,[F|_]),member(F,[k,g,u,c,a,o,'0','1','2','3','4','5','6','7','8','9']).
mptp_tptp_pred_arity_thf(X/_):- atom_chars(X,[F|_]),member(F,[r,v,m,l]).
mptp_scheme_arity_thf(X/_):- atom_chars(X,[F|_]),member(F,[f,p]).
%% collect ground counts into buk/3, stack failure otherwise
%% then print and run perl -e 'while(<>) { /.([0-9]+), (.*)./; $h{$2}+=$1;} foreach $k (sort {$h{$b} <=> $h{$a}} (keys %h)) {print "$h{$k}:$k\n";}'
%% on the result
get_ground_info:-
fof(Ref,_,Fla,file(_,_), [mptp_info(_,_,theorem,_,_)|_]),
collect_with_count_top(ground_mptp_func,Fla,Out,OutC),
not(buk(Ref,_,_)),assert(buk(Ref,Out,OutC)),
fail.
print_ground_info:-
tell('00ground'),
findall(e,(buk(_,B,C),zip(C,B,S),findall(d,(member(X,S),print(X),nl),_)),_),
told.
%% like collect_symbols, but with arity
collect_symbols_arity_top(X,L):-
collect_symbols_arity(X,L1),!,
flatten(L1,L2),
sort(L2,L).
collect_symbols_arity(X,[]):- var(X),!.
collect_symbols_arity(X,[X/0]):- atomic(X),!.
collect_symbols_arity(X1,T2):-
X1 =.. [H1|T1],
length(T1,A1),
maplist(collect_symbols_arity,T1,T3),
flatten(T3,T4),
sort([H1/A1|T4],T2).
% like logic_syms, but with a fake arity - is dangerous to use and hackish
logic_syms_arity([(=)/_,'++'/_,'--'/_,'@'/_,'$'/_,(~)/_,'|'/_,'~|'/_,true/_,(&)/_,(~&)/_,(=>)/_,(<=)/_,(<=>)/_,(<~>)/_,(!)/_,(?)/_,(:)/_,'..'/_,sort/_,the/_,all/_,'.'/_,([])/_]).
%% collect funcs and preds with arity from a formula
get_func_pred_syms(X,F,P):-
collect_symbols_arity_top(X,L),
logic_syms_arity(LA),
subtract(L,LA,L1),
sublist(mptp_tptp_func_arity,L1,F),
subtract(L1,F,P).
%% collect funcs and preds with arity from a formula, avoid "f" and "p" - schemes
get_func_pred_syms_thf(X,F,P):-
collect_symbols_arity_top(X,L),
logic_syms_arity(LA),
subtract(L,LA,L1),
sublist(mptp_tptp_func_arity_thf,L1,F),
subtract(L1,F,L2),
sublist(mptp_tptp_pred_arity_thf,L2,P).
%% collect nonvar symbols from term
collect_symbols_top(X,L):-
collect_symbols(X,L1),!,
flatten(L1,L2),
sort(L2,L).
collect_symbols(X,[]):- var(X),!.
collect_symbols(X,[X]):- atomic(X),!.
collect_symbols(X1,T2):-
X1 =.. [H1|T1],
maplist(collect_symbols,T1,T3),
flatten(T3,T4),
sort([H1|T4],T2).
union1([],In,In).
union1([H|T],In,Out):-
union(H,In,R1),
union1(T,R1,Out).
%% check_if_symbol(+Term,+Symbol)
%%
%% faster check for a symbol than collecting
%% e.g. check_if_symbol(Fla,all) checks for a fraenkel term
check_if_symbol(X,_):- var(X),!,fail.
check_if_symbol(X, S):- atomic(X),!, X == S.
check_if_symbol(X1,S):-
X1 =.. [H1|T1],
(
H1 = S,!
;
check_if_symbol_l(T1,S)
).
check_if_symbol_l([H|_],S):- check_if_symbol(H,S),!.
check_if_symbol_l([_|T],S):- check_if_symbol_l(T,S),!.
%%%%%%%%%%%%%%%%%%%% Propositional Abstractor %%%%%%%%%%%%%%%%%%%%
% to forbid backtracking
propabstr_transform_top(X,Y):- sort_transform_top(X,X1),!, propabstr_transform(X1,Y), !.
% end of traversal
propabstr_transform(! _ : X, Y):- !, propabstr_transform(X, Y).
propabstr_transform(? _ : X, Y):- !, propabstr_transform(X, Y).
propabstr_transform($true,$true):- !.
propabstr_transform($false,$false):- !.
propabstr_transform(X & Y,X1 & Y1):- !,
propabstr_transform_l([X,Y],[X1,Y1]).
propabstr_transform(X,TermOut):-
X =.. [F|Args],!,
(
memberchk(F,[&,'|',~,=>,<=,<=>]) ->
propabstr_transform_l(Args,Args1),
TermOut =.. [F|Args1]
;
F == '=' ->
TermOut = eq
;
TermOut = F
).
propabstr_transform_l([],[]).
propabstr_transform_l([H|T],[H1|T1]):-
propabstr_transform(H,H1),
propabstr_transform_l(T,T1).
%%%%%%%%%%%%%%%%%%%% Abstractor %%%%%%%%%%%%%%%%%%%%
%% ##TEST: :- declare_mptp_predicates,load_mml,install_index,all_articles(AA),checklist(abstract_fraenkels_if, AA),!,member(A,[card_1]),time(mk_article_problems(A,[[mizar_by,mizar_from,mizar_proof],[theorem, top_level_lemma, sublemma] ],[opt_LOAD_MIZ_ERRORS,opt_ARTICLE_AS_TPTP_AXS,opt_REM_SCH_CONSTS,opt_TPTP_SHORT,opt_LINE_COL_NMS,opt_PRINT_PROB_PROGRESS,opt_ALLOWED_REF_INFO,opt_PROVED_BY_INFO,opt_SORT_TRANS_FUNC(set_of_abstr_trm_top)])),fail.
% to forbid backtracking
abstr_transform_top(X,Y):- abstr_transform(X,Y,[],_), !.
% end of traversal
abstr_transform(X,X,SubstIn,SubstIn):- var(X),!.
abstr_transform(X & Y,X1 & Y1,SubstIn,SubstOut):-
abstr_transform_l([X,Y],[X1,Y1],SubstIn,SubstOut).
abstr_transform(X,TermOut,SubstIn,SubstOut):-
X =.. [F|Args],!,
(
memberchk(F,[&,'|',~,=>,<=,!,?,:,<=>,'..','.','$',true,[]]) ->
abstr_transform_l(Args,Args1,SubstIn,SubstOut),
TermOut =.. [F|Args1]
;
(
memberchk((F/NewVar),SubstIn) -> Subst1 = SubstIn
;
Subst1 = [(F/NewVar)|SubstIn]
),
abstr_transform_l(Args,Args1,Subst1,SubstOut),
TermOut =.. [ap,NewVar|Args1]
).
abstr_transform_l([],[],SubstIn,SubstIn).
abstr_transform_l([H|T],[H1|T1],SubstIn,SubstOut):-
abstr_transform(H,H1,SubstIn,Subst1),
abstr_transform_l(T,T1,Subst1,SubstOut).
% Does not work when here - moved to the top
% :- use_module(library(ordsets)).
set_of_terms_top(X,Res) :- !,
copy_term(X,X1),
numbervars(X1,0,_),
set_of_terms(X1,[],Res).
set_of_terms_l([],In,In).
set_of_terms_l([H|T],In,Out):-
set_of_terms(H,In,S1),
set_of_terms_l(T,S1,Out).
set_of_terms(X,In,Out):-
ord_add_element(In, X, S1),
X =.. [F|Args],
list_to_ord_set([F|Args], S2),
ord_union(S1, S2, S3),
set_of_terms_l(Args, S3, Out).
set_of_abstr_trm_top(X,Out):-
sort_transform_top(X,X1),
abstr_transform_top(X1,Y),
set_of_terms_top(Y,Out1),!,
findall(K,(member(T,Out1),with_output_to(string(K), print(T))),Out).
%%format(string(K),'~w',T)),Out).
% maplist(format('~w'),Out1,Out).
% repeat,(member(K,Out),write('"'),write(K),write('",'),fail; nl).
%%%%%%%%% the version that add the counts of occurences
%% ##TEST: :- declare_mptp_predicates,load_mml,install_index,all_articles(AA),checklist(abstract_fraenkels_if, AA),!,member(A,[card_1]),time(mk_article_problems(A,[[mizar_by,mizar_from,mizar_proof],[theorem, top_level_lemma, sublemma] ],[opt_LOAD_MIZ_ERRORS,opt_ARTICLE_AS_TPTP_AXS,opt_REM_SCH_CONSTS,opt_TPTP_SHORT,opt_LINE_COL_NMS,opt_PRINT_PROB_PROGRESS,opt_ALLOWED_REF_INFO,opt_PROVED_BY_INFO,opt_SORT_TRANS_FUNC(bag_of_abstr_trm_top)])),fail.
% Does not work when here - moved to the top
:- use_module(library(assoc)).
bag_insert(X,In,Out):-
(get_assoc(X, In, V) ->
V1 is V + 1
;
V1 is 1
),
put_assoc(X, In, V1, Out).
bag_insert_l([],In,In).
bag_insert_l([H|T],In,Out):-
bag_insert(H,In,Out1),
bag_insert_l(T,Out1,Out).
bag_of_terms_top(X,Res) :- !,
copy_term(X,X1),
numbervars(X1,0,_),
empty_assoc(Assoc),
bag_of_terms(X1,Assoc,Res).
bag_of_terms_l([],In,In).
bag_of_terms_l([H|T],In,Out):-
bag_of_terms(H,In,S1),
bag_of_terms_l(T,S1,Out).
bag_of_terms($true,In,In):- !.
bag_of_terms(X,In,Out):-
X =.. [F|Args],
bag_insert_l([X,F],In,In1),
bag_of_terms_l(Args, In1, Out).
bag_of_abstr_trm_top(X,Out):-
sort_transform_top(X,X1),
abstr_transform_top(X1,Y),
bag_of_terms_top(Y,Out0),!,
assoc_to_list(Out0,Out1),
findall(K,(member(T,Out1),with_output_to(string(K), print(T))),Out).
%%%%%%%%% the version that distinguishes funcs and preds
mptp_pred_tptp(X):- atom_chars(X,[F|_]), member(F,[v,r,m,l,p]).
% to forbid backtracking
abstr1_transform_top(X,Y):- abstr1_transform(X,Y,[],[],_,_), !.
% end of traversal
abstr1_transform(X,X,SubstPIn,SubstFIn,SubstPIn,SubstFIn):- var(X),!.
abstr1_transform(X & Y,X1 & Y1,SubstPIn,SubstFIn,SubstPOut,SubstFOut):-
abstr1_transform_l([X,Y],[X1,Y1],SubstPIn,SubstFIn,SubstPOut,SubstFOut).
abstr1_transform(X,TermOut,SubstPIn,SubstFIn,SubstPOut,SubstFOut):-
X =.. [F|Args],!,
(
memberchk(F,[&,'|',~,=>,<=,!,?,:,<=>,'..','.','$',true,[]]) ->
abstr1_transform_l(Args,Args1,SubstPIn,SubstFIn,SubstPOut,SubstFOut),
TermOut =.. [F|Args1]
;
(
mptp_pred_tptp(F) ->
(
memberchk((F/NewVar),SubstPIn) ->
Subst1P = SubstPIn
;
Subst1P = [(F/NewVar)|SubstPIn]
),
abstr1_transform_l(Args,Args1,Subst1P,SubstFIn,SubstPOut,SubstFOut),
TermOut =.. [p,NewVar|Args1]
;
(
memberchk((F/NewVar),SubstFIn) ->
Subst1F = SubstFIn
;
Subst1F = [(F/NewVar)|SubstFIn]
),
abstr1_transform_l(Args,Args1,SubstPIn,Subst1F,SubstPOut,SubstFOut),
TermOut =.. [f,NewVar|Args1]
)
).
abstr1_transform_l([],[],SubstPIn,SubstFIn,SubstPIn,SubstFIn).
abstr1_transform_l([H|T],[H1|T1],SubstPIn,SubstFIn,SubstPOut,SubstFOut):-
abstr1_transform(H,H1,SubstPIn,SubstFIn,Subst1P,Subst1F),
abstr1_transform_l(T,T1,Subst1P,Subst1F,SubstPOut,SubstFOut).
%% ##TEST: :- declare_mptp_predicates,load_mml,install_index,all_articles(AA),checklist(abstract_fraenkels_if, AA),!,member(A,[card_1]),time(mk_article_problems(A,[[mizar_by,mizar_from,mizar_proof],[theorem, top_level_lemma, sublemma] ],[opt_LOAD_MIZ_ERRORS,opt_ARTICLE_AS_TPTP_AXS,opt_REM_SCH_CONSTS,opt_TPTP_SHORT,opt_LINE_COL_NMS,opt_PRINT_PROB_PROGRESS,opt_ALLOWED_REF_INFO,opt_PROVED_BY_INFO,opt_SORT_TRANS_FUNC(bag_of_abstr1_trm_top)])),fail.
bag_of_abstr1_trm_top(X,Out):-
sort_transform_top(X,X1),
abstr1_transform_top(X1,Y),
bag_of_terms_top(Y,Out0),!,
assoc_to_list(Out0,Out1),
findall(K,(member(T,Out1),with_output_to(string(K), print(T))),Out).
%%%%%%%%%%%%%%%%%%%% Putting variable types into terms for learning %%%%%%%%%%%%%%%%%%%%
%% not used yet because of various explosions
tvar_transforml(NewVar,Y1 & Y2,List):- !,
tvar_transforml(NewVar,Y1,L1),
tvar_transforml(NewVar,Y2,L2),
append(L1,L2,List).
tvar_transforml(_, $true, []):- !.
tvar_transforml(NewVar, ~S, [~S1]):- !,
S =.. [F|Args],
S1 =.. [F|[NewVar|Args]].
tvar_transforml(NewVar, S, [S1]):- !,
S =.. [F|Args],
S1 =.. [F|[NewVar|Args]].
% to forbid backtracking
tvar_transform_top(X,Y):- tvar_transform(X,Y), !.
% end of traversal
tvar_transform(X,X):- atomic(X); var(X).
tvar_transform(! [(X:S)|T] : Y, Result):-
(
S == $true -> X = NewVar
;
tvar_transforml(NewVar,S,List),
(X = List,!; print(zzzfail(X,List)))
%ensure(X = List, tvar_transforml(X,List))
),
(
T = [] -> tvar_transform(Y, Result)
;
tvar_transform(! T : Y, Result)
), !.
tvar_transform(? [(X:S)|T] : Y, Result):- !,
tvar_transform(! [(X:S)|T] : Y, Result).
tvar_transform(sort(_,$true),$true).
tvar_transform(sort(_,$false),$false).
%% a list of predicates applied to a list
tvar_transform(sort(X,S),List):- !,
tvar_transforml(X, S, List).
%% removal of $true
tvar_transform((A | B), Result):-
maplist(tvar_transform,[A,B],[A1,B1]),!,
(
(A1 == $true;B1 == $true) -> Result = $true;
Result = (A1 | B1)
).
tvar_transform(A & B, Result):-
maplist(tvar_transform,[A,B],[A1,B1]),!,
(
A1== $true -> Result = B1;
(
B1== $true -> Result = A1;
Result = (A1 & B1)
)
).
tvar_transform(A => B, Result):-
maplist(tvar_transform,[A,B],[A1,B1]),!,
(
A1== $true -> Result = B1;
(
B1== $true -> Result = $true;
Result = (A1 => B1)
)
).
tvar_transform(A <=> B, Result):-
maplist(tvar_transform,[A,B],[A1,B1]),!,
(
A1== $true -> Result = B1;
(
B1== $true -> Result = A1;
Result = (A1 <=> B1)
)
).
% functor traversal
tvar_transform(X1,X2):-
X1 =.. [H1|T1],
maplist(tvar_transform,T1,T2),
X2 =.. [H1|T2].
%%%%%%%%%%%%%%%%%%%% Sort relativization %%%%%%%%%%%%%%%%%%%%
% return the list of quantified variables and conjunction of predicates
sort_transform_qlist([],[],$true). % needed only for fraenkel
sort_transform_qlist([X:S],[X],S1):- !,
sort_transform(sort(X,S),S1).
sort_transform_qlist([(X:S)|T],[X|Qvars1],SortPreds1):-
sort_transform(sort(X,S),S1),
sort_transform_qlist(T,Qvars1,Preds1), !,
(
S1 == $true -> SortPreds1 = Preds1;
(
Preds1 == $true -> SortPreds1 = S1;
SortPreds1 = (S1 & Preds1)
)
).
% to forbid backtracking
sort_transform_top(X,Y):- sort_transform(X,Y), !.
% end of traversal
sort_transform(X,X):- atomic(X); var(X).
% do sort relativization, and simple removal of $true
sort_transform(! Svars : Y, Result):-
sort_transform_qlist(Svars,Qvars,Preds),
sort_transform(Y,Y1), !,
(
Y1 == $true -> Result = $true;
Result = (! Qvars : UnivRelat),
(
Preds == $true -> UnivRelat = Y1;
UnivRelat = (Preds => Y1)
)
).
sort_transform(? Svars : Y, Result):-
sort_transform_qlist(Svars,Qvars,Preds),
sort_transform(Y,Y1), !,
(
Preds == $true ->
(
Y1 == $true -> Result = $true;
Result = (? Qvars : Y1)
)
;
(
Y1 == $true -> Result = (? Qvars : Preds);
Result = (? Qvars : (Preds & Y1))
)
).
% This clause is redundant now, sort trafo can be done only after 'all' removal
sort_transform(all(Svars,Trm,Frm),all(Qvars,Trm1,RelatFrm1)):-
sort_transform_qlist(Svars,Qvars,Preds),
sort_transform(Trm,Trm1),
sort_transform(Frm,Frm1), !,
(
Preds == $true -> RelatFrm1 = Frm1;
RelatFrm1 = (Preds & Frm1)
).
sort_transform(sort(X,Y1 & Y2),SortPreds):-
sort_transform(sort(X,Y1),Z1),
sort_transform(sort(X,Y2),Z2), !,
(
Z1 == $true -> SortPreds = Z2;
(
Z2 == $true -> SortPreds = Z1;
SortPreds = (Z1 & Z2)
)
).
sort_transform(sort(X,~Y),~Z):-
sort_transform(sort(X,Y),Z).
sort_transform(sort(_,$true),$true).
sort_transform(sort(_,$false),$false).
sort_transform(sort(X,Y),Z):-
Y =.. [F|Args],
maplist(sort_transform,[X|Args],Args1),
Z =.. [F|Args1].
% we should not get here
sort_transform(sort(_,_),_):- throw(sort).
% removal of $true
sort_transform((A | B), Result):-
maplist(sort_transform,[A,B],[A1,B1]),!,
(
(A1 == $true;B1 == $true) -> Result = $true;
Result = (A1 | B1)
).
sort_transform(A & B, Result):-
maplist(sort_transform,[A,B],[A1,B1]),!,
(
A1== $true -> Result = B1;
(
B1== $true -> Result = A1;
Result = (A1 & B1)
)
).
sort_transform(A => B, Result):-
maplist(sort_transform,[A,B],[A1,B1]),!,
(
A1== $true -> Result = B1;
(
B1== $true -> Result = $true;
Result = (A1 => B1)
)
).
sort_transform(A <=> B, Result):-
maplist(sort_transform,[A,B],[A1,B1]),!,
(
A1== $true -> Result = B1;
(
B1== $true -> Result = A1;
Result = (A1 <=> B1)
)
).
% functor traversal
sort_transform(X1,X2):-
X1 =.. [H1|T1],
maplist(sort_transform,T1,T2),
X2 =.. [H1|T2].
%%%%%%%%%%%%%%%%%%%% THF translation %%%%%%%%%%%%%%%%%%%%
% return the list of quantified variables and conjunction of predicates
sort_transform_thf_qlist(S1,S1,[],[],$true). % needed only for fraenkel
sort_transform_thf_qlist(SubstsIn,SubstsOut,[X:S],[X : $i],S1):- !,
sort_transform_thf(SubstsIn,SubstsOut,sort(X,S),S1).
sort_transform_thf_qlist(SubstsIn,SubstsOut,[(X:S)|T],[X : $i|Qvars1],SortPreds1):-
sort_transform_thf(SubstsIn,SubstsOut1,sort(X,S),S1),
sort_transform_thf_qlist(SubstsOut1,SubstsOut,T,Qvars1,Preds1), !,
(
S1 == $true -> SortPreds1 = Preds1;
(
Preds1 == $true -> SortPreds1 = S1;
SortPreds1 = (S1 & Preds1)
)
).
add_lambda_l(_,[],[]).
add_lambda_l(X, [H|T], [Z | T1]) :-
Z = ('^' [ X : $i] : H),
add_lambda_l(X,T,T1).
sort_transform_thf_qlist_fr(S1,S1,[],[],[]). % needed only for fraenkel
sort_transform_thf_qlist_fr(SubstsIn,SubstsOut,[(X:S)|T],[X : $i|Qvars1],Preds2):-
sort_transform_thf(SubstsIn,SubstsOut1,sort(X,S),S1),
sort_transform_thf_qlist_fr(SubstsOut1,SubstsOut,T,Qvars1,Preds1),
add_lambda_l(X,[S1|Preds1],Preds2), !.
add_univ_context1([],Fla,Fla).
add_univ_context1([[_,Var,Decl]|T], FlaIn, ( ! [Var:Decl] : ( FlaOut) )):-
add_univ_context1(T,FlaIn,FlaOut).
apply_univ_context1([],_).
apply_univ_context1([[Sym,Var,_]|T], FlaIn):-
Var = Sym,
apply_univ_context1(T,FlaIn).
% sort_transform_thf_top(+NoGen,+X,-Y):-
%
% If NoGen == 1, we do not generalize the scheme symbols like "f" and
% "p" into variables. This means that we just apply the substitutions
% collected.
sort_transform_thf_top(NoGen,X,Y):-
sort_transform_thf([],SubstsOut,X,Y1), !,
(
NoGen == 1 -> apply_univ_context1(SubstsOut,Y1), Y = Y1
;
add_univ_context1(SubstsOut,Y1,Y)
).
% sort_transform_thf(+SubstsIn, -SubstsOut, +In , -Out)
%
% transform to THF and collect the substitutions (context) that may
% later be applied in sort_transform_thf_top .
%