-
Notifications
You must be signed in to change notification settings - Fork 4
/
polar.pro
184 lines (131 loc) · 4.06 KB
/
polar.pro
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
% utilities
/*
is_nf(E):-is_neut(E),!.
is_nf(l(_,E)):-is_nf(E).
is_neut(X):-var(X),!.
is_neut(a(E,F)):-is_neut(E),is_nf(F).
lsize(A,R):-var(A),!,R=0.
lsize(A,R):-atomic(A),!,R=0.
lsize(l(_,B),R):-lsize(B,R2),R is 1+R2.
lsize(a(A,B),R):-lsize(A,R1),lsize(B,R2),R is 1+R1+R2.
pol(A,X):-pol(0,A,X).
pol(P,X,R):-var(X),!,R=P:X.
pol(P,A '-o' B,(X '-o' Y)):-N is 1-P,pol(N,A,X),pol(P,B,Y).
*/
% generate polarized
polarized_tree(L,Tree):-L>=0,
K is L+1,
N is 2*L+1,
numlist(0,L,Ps),
length(Qs,K),
polarized_tree(0,Tree,Ps,[],Qs,[],N,0),
perm_of(Ps,Qs).
polarized_tree(P,V,Xs1,Xs2,Ys1,Ys2)-->{dispatch(P,V,Xs1,Xs2,Ys1,Ys2)}.
polarized_tree(P,(A '-o' B),Xs1,Xs3,Ys1,Ys3)-->pred,{Q is 1-P},
polarized_tree(Q,A,Xs1,Xs2,Ys1,Ys2),
polarized_tree(P,B,Xs2,Xs3,Ys2,Ys3).
perm_of([],[]).
perm_of([X|Xs],Zs):-
perm_of(Xs,Ys), % handling the (shorter) tail of the list
ins(X,Ys,Zs). % inductive step: insert X into Ys
ins(X,Xs,[X|Xs]).
ins(X,[Y|Xs], [Y|Ys]):-ins(X,Xs,Ys).
% checks if a proof term in normal form can be associated to it
is_linear_typed_normal_form(N,E,T):-succ(N,N1),
is_linear_typed_normal_form(E,T,N,0,N1,0,[]).
is_linear_typed_normal_form(l(X,E),(S '-o' T),A1,A2,L1,L3,Vs):-pred(L1,L2),
is_linear_typed_normal_form(E,T,A1,A2,L2,L3,[V:S|Vs]),
check_binding(V,X),
!.
is_linear_typed_normal_form(E,T,A1,A2,L1,L3,Vs):-
is_linear_neutral_term(E,T,A1,A2,L1,L3,Vs).
is_linear_neutral_term(X,T,A,A,L,L,Vs):-
member(V:TT,Vs),
bind_once(V,X),T=TT,
!.
is_linear_neutral_term(a(E,F),T,A1,A4,L1,L3,Vs):- pred(A1,A2),
is_linear_neutral_term(E,(S '-o' T),A2,A3,L1,L2,Vs),
is_linear_typed_normal_form(F,S,A3,A4,L2,L3,Vs).
tsize(A,R):-var(A),!,R=0.
tsize(A,R):-atomic(A),!,R=0.
tsize((A'-o'B),R):-tsize(A,R1),tsize(B,R2),R is 1+R1+R2.
%%%%%%%%%% begin prover
% check if polarized
is_polarized_tree(Tree,N):-
is_polarized_tree(0,Tree,Ps,[],Qs,[]),
in_bijection(Ps,Qs,N).
is_polarized_tree(P,V,Xs1,Xs2,Ys1,Ys2):-atomic(V),!,
dispatch(P,V,Xs1,Xs2,Ys1,Ys2).
is_polarized_tree(P,(A '-o' B),Xs1,Xs3,Ys1,Ys3):-Q is 1-P,
is_polarized_tree(Q,A,Xs1,Xs2,Ys1,Ys2),
is_polarized_tree(P,B,Xs2,Xs3,Ys2,Ys3).
dispatch(0,X,[X|Xs],Xs,Ys,Ys).
dispatch(1,Y,Xs,Xs,[Y|Ys],Ys).
% each variable has a counterpart of opposite polarity
in_bijection(Ps,Qs, N):-
length(Ps,K),
length(Qs,K),
sort(Ps,Xs),
sort(Qs,Xs),
length(Xs,K),
N is K-1.
% implicational linear logic prover T in, X out
prove_polarized(X,T):-
is_polarized_tree(T,N),
linear_typed_normal_form(N,X,T).
%%%%%%%%%% end prover
% generators/testers
% test prover - should match linear_typed_normal_form
pol_taut(N,X,T):-
gen_formula2(N,T),
prove_polarized(X,T).
pol_gen_taut(N,X,T):-
polarized_tree(N,T),
is_linear_typed_normal_form(N,X,T).
pol_gen(N,T):-
gen_formula2(N,T),
is_polarized_tree(T,_).
%:-include('tools.pro').
new_ctr(ctr(0)).
%% ctr_inc(Ctr,X): adds value X to Ctr
ctr_inc(Ctr,N):-arg(1,Ctr,V1),V2 is V1+N,nb_setarg(1,Ctr,V2).
ctr_get(Ctr,Val):-arg(1,Ctr,Val).
%% ctr_dec(Ctr,X): decrements Ctr if > 0
ctr_dec(Ctr):-arg(1,Ctr,V1),V1>0,V2 is V1-1,nb_setarg(1,Ctr,V2).
fgo:-save_full_traning_set(3).
save_full_traning_set(M):-
tell('full_training.txt'),
encode_full_map1(M),
told.
encode_full_map1(M):-
do((
between(0,M,N),
encode_full_map(N)
)).
encode_full_map(N):-
set_random(seed(42)),
new_ctr(C),new_ctr(U),
(
gen_formula2(N,T),
( prove_polarized(X,T)->
ctr_inc(C,1),ctr_inc(U,1),
numbervars(X,0,_),
encode_term(X,Xs,[])
; Xs=['?'],random(R),R<1/10,ctr_dec(C)
),
numbervars(T,0,_),
encode_formula(T,Ts,[]),
maplist(write,Ts),write(':'),maplist(write,Xs),nl,
ctr_inc(U,1),
( ctr_get(U,K),K>60000->!
; fail
)
;true
).
/*
LinearTermsAndType(7)=[1,3,26,367,7142,176766,5304356,186954535]
PolarizedLinearTautologies(5)=[1,1,11,119,1679,30239]
*/
gpp(N):-counts_for2(N,polarized_tree,Ks),ppp('PolarizedTrees'(N)=Ks).
gpg(N):-counts_for3(N,pol_gen_taut,Ks),ppp('tests'(N)=Ks). % good
gpf(N):-counts_for3(N,pol_taut,Ks),ppp('tests'(N)=Ks). % good