-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdecidable.fsx
More file actions
295 lines (238 loc) · 10.7 KB
/
decidable.fsx
File metadata and controls
295 lines (238 loc) · 10.7 KB
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
// ========================================================================= //
// Copyright (c) 2003-2007, John Harrison. //
// Copyright (c) 2012 Eric Taucher, Jack Pappas, Anh-Dung Phan //
// (See "LICENSE.txt" for details.) //
// ========================================================================= //
#load "initialization.fsx"
open FSharpx.Books.AutomatedReasoning.initialization
open FSharpx.Books.AutomatedReasoning.lib
open FSharpx.Books.AutomatedReasoning.formulas
open FSharpx.Books.AutomatedReasoning.prop
open FSharpx.Books.AutomatedReasoning.dp
open FSharpx.Books.AutomatedReasoning.fol
open FSharpx.Books.AutomatedReasoning.skolem
open FSharpx.Books.AutomatedReasoning.herbrand
open FSharpx.Books.AutomatedReasoning.tableaux
open FSharpx.Books.AutomatedReasoning.resolution
open FSharpx.Books.AutomatedReasoning.meson
open FSharpx.Books.AutomatedReasoning.equal
open FSharpx.Books.AutomatedReasoning.decidable
fsi.AddPrinter sprint_fol_formula
// pg. 309
// ------------------------------------------------------------------------- //
// Special procedures for decidable subsets of first order logic. //
// ------------------------------------------------------------------------- //
// decidable.p001
// Process is terminated due to StackOverflowException, even with 16MB stack
meson002 (parse @"forall x. p(x)")
// decidable.p002
// Process is terminated due to StackOverflowException, even with 16MB stack
tab (parse @"forall x. p(x)")
// pg. 309
// ------------------------------------------------------------------------- //
// Resolution does actually terminate with failure in simple cases! //
// ------------------------------------------------------------------------- //
// decidable.p003
// System.Exception: No proof found.
resolution001 (parse @"forall x. p(x)")
// pg. 309
// ------------------------------------------------------------------------- //
// The Los example; see how Skolemized form has no non-nullary functions. //
// ------------------------------------------------------------------------- //
// Los #1
let los =
(parse @"
(forall x y z. P(x,y) /\ P(y,z) ==> P(x,z)) /\
(forall x y z. Q(x,y) /\ Q(y,z) ==> Q(x,z)) /\
(forall x y. P(x,y) ==> P(y,x)) /\
(forall x y. P(x,y) \/ Q(x,y))
==> (forall x y. P(x,y)) \/ (forall x y. Q(x,y))")
// decidable.p004
skolemize (Not los)
// pg. 310
// ------------------------------------------------------------------------- //
// The old DP procedure works. //
// ------------------------------------------------------------------------- //
// decidable.p005
davisputnam los
// pg. 310
// ------------------------------------------------------------------------- //
// In this case it's quicker. //
// ------------------------------------------------------------------------- //
// decidable.p006
aedecide los
// pg. 312
// ------------------------------------------------------------------------- //
// Show how we need to do PNF transformation with care. //
// ------------------------------------------------------------------------- //
let fm001 = (parse @"(forall x. p(x)) \/ (exists y. p(y))")
// decidable.p007
pnf fm001
// pg. 312
// ------------------------------------------------------------------------- //
// Also the group theory problem. //
// ------------------------------------------------------------------------- //
// decidable.p008
// Real: 00:03:13.533, CPU: 00:05:45.906, GC gen0: 35, gen1: 20, gen2: 0
runWithEnlargedStack (fun () ->
aedecide (parse @"
(forall x. P(1,x,x)) /\ (forall x. P(x,x,1)) /\
(forall u v w x y z.
P(x,y,u) /\ P(y,z,w) ==> (P(x,w,v) <=> P(u,z,v)))
==> forall a b c. P(a,b,c) ==> P(b,a,c)"))
// decidable.p009
// Real: 00:11:45.855, CPU: 00:11:43.421, GC gen0: 55, gen1: 19, gen2: 1
runWithEnlargedStack (fun () ->
aedecide (parse @"
(forall x. P(x,x,1)) /\
(forall u v w x y z.
P(x,y,u) /\ P(y,z,w) ==> (P(x,w,v) <=> P(u,z,v)))
==> forall a b c. P(a,b,c) ==> P(b,a,c)"))
// pg. 313
// ------------------------------------------------------------------------- //
// A bigger example. //
// ------------------------------------------------------------------------- //
// decidable.p010
runWithEnlargedStack (fun () ->
aedecide (parse @"
(exists x. P(x)) /\ (exists x. G(x))
==> ((forall x. P(x) ==> H(x)) /\ (forall x. G(x) ==> J(x)) <=>
(forall x y. P(x) /\ G(y) ==> H(x) /\ J(y)))"))
// pg. 313
// ------------------------------------------------------------------------- //
// The following, however, doesn't work with aedecide. //
// ------------------------------------------------------------------------- //
// This is p18
// decidable.p011
// System.Exception: Not decidable
// Pelletier #18
aedecide (parse @"exists y. forall x. P(y) ==> P(x)")
// decidable.p012
// Pelletier #18
davisputnam (parse @"exists y. forall x. P(y) ==> P(x)")
// pg. 315
// ------------------------------------------------------------------------- //
// Examples. //
// ------------------------------------------------------------------------- //
// decidable.p013
// Pelletier #18
miniscope(nnf (parse @"exists y. forall x. P(y) ==> P(x)"))
// decidable.p014
// Pelletier #20
let fm002 =
miniscope(nnf (parse @"(
forall x y. exists z. forall w. P(x) /\ Q(y) ==> R(z) /\ U(w))
==> (exists x y. P(x) /\ Q(y)) ==> (exists z. R(z))"))
// decidable.p015
pnf (nnf fm002)
// pg. 316
// ------------------------------------------------------------------------- //
// It works well on simple monadic formulas. //
// ------------------------------------------------------------------------- //
// decidable.p016
// Pelletier #20
wang
(parse @"
(forall x y. exists z. forall w. P(x) /\ Q(y) ==> R(z) /\ U(w))
==> (exists x y. P(x) /\ Q(y)) ==> (exists z. R(z))")
// pg. 316
// ------------------------------------------------------------------------- //
// But not on this one! //
// ------------------------------------------------------------------------- //
// decidable.p017
// Note: This works, but the output is huge.
// Pelletier #34
let fm003 =
pnf(nnf(miniscope(nnf (parse @"
((exists x. forall y. P(x) <=> P(y)) <=>
((exists x. Q(x)) <=> (forall y. Q(y)))) <=>
((exists x. forall y. Q(x) <=> Q(y)) <=>
((exists x. P(x)) <=> (forall y. P(y))))"))))
// pg. 319
// ------------------------------------------------------------------------- //
// Checking classic Aristotelean syllogisms. //
// ------------------------------------------------------------------------- //
// decidable.p018
let all_valid_syllogisms = List.filter aedecide all_possible_syllogisms
// decidable.p019
List.length all_valid_syllogisms
// decidable.p020
List.map anglicize_syllogism all_valid_syllogisms
// pg. 320
// ------------------------------------------------------------------------- //
// We can "fix" the traditional list by assuming nonemptiness. //
// ------------------------------------------------------------------------- //
// decidable.p021
// Note: This works, but the output is huge.
let all_valid_syllogisms' = List.filter aedecide all_possible_syllogisms'
// decidable.p022
List.length all_valid_syllogisms'
// decidable.p023
List.map (anglicize_syllogism << consequent) all_valid_syllogisms'
// pg. 323
// ------------------------------------------------------------------------- //
// Decision procedure in principle for formulas with finite model property. //
// ------------------------------------------------------------------------- //
// decidable.p024
decide_fmp
(parse @"
(forall x y. R(x,y) \/ R(y,x)) ==> forall x. R(x,x)")
// decidable.p025
decide_fmp
(parse @"
(forall x y z. R(x,y) /\ R(y,z) ==> R(x,z)) ==> forall x. R(x,x)")
// decidable.p026
//** This fails to terminate: has countermodels, but only infinite ones
// Process is terminated due to StackOverflowException, even with 16MB stack
runWithEnlargedStack (fun () ->
decide_fmp (parse @"
~((forall x. ~R(x,x)) /\
(forall x. exists z. R(x,z)) /\
(forall x y z. R(x,y) /\ R(y,z) ==> R(x,z)))"))
// pg. 325
// ------------------------------------------------------------------------- //
// Example. //
// ------------------------------------------------------------------------- //
// decidable.p027
// Pelletier #34
decide_monadic
(parse @"
((exists x. forall y. P(x) <=> P(y)) <=>
((exists x. Q(x)) <=> (forall y. Q(y)))) <=>
((exists x. forall y. Q(x) <=> Q(y)) <=>
((exists x. P(x)) <=> (forall y. P(y))))")
// decidable.p028
// This is not feasible
// Process is terminated due to StackOverflowException.
// Pelletier #20
//decide_monadic
// (parse @"
// (forall x y. exists z. forall w. P(x) /\ Q(y) ==> R(z) /\ U(w))
// ==> (exists x y. P(x) /\ Q(y)) ==> (exists z. R(z))")
// pg. 326
// ------------------------------------------------------------------------- //
// Little auxiliary results for failure of finite model property. //
// ------------------------------------------------------------------------- //
//** Our claimed equivalences are indeed correct **//
// decidable.p029
// Process is terminated due to StackOverflowException.
meson002 (parse @"
(exists x y z. forall u.
R(x,x) \/ ~R(x,u) \/ (R(x,y) /\ R(y,z) /\ ~R(x,z))) <=>
~((forall x. ~R(x,x)) /\
(forall x. exists z. R(x,z)) /\
(forall x y z. R(x,y) /\ R(y,z) ==> R(x,z)))")
// decidable.p030
meson002
(parse @"
(exists x. forall y. exists z. R(x,x) \/ ~R(x,y) \/ (R(y,z) /\ ~R(x,z))) <=>
~((forall x. ~R(x,x)) /\
(forall x. exists y. R(x,y) /\ forall z. R(y,z) ==> R(x,z)))")
//** The second formula implies the first **//
// decidable.p031
meson002 (parse @"
~((forall x. ~R(x,x)) /\
(forall x. exists y. R(x,y) /\ forall z. R(y,z) ==> R(x,z)))
==> ~((forall x. ~R(x,x)) /\
(forall x. exists z. R(x,z)) /\
(forall x y z. R(x,y) /\ R(y,z) ==> R(x,z)))")