@@ -256,70 +256,25 @@ def is_prop_symbol(s):
256
256
TRUE or FALSE."""
257
257
return is_symbol (s ) and s [0 ].isupper () and s != 'TRUE' and s != 'FALSE'
258
258
259
- def is_positive (s ):
260
- """s is an unnegated logical expression
261
- >>> is_positive(expr('F(A, B)'))
262
- True
263
- >>> is_positive(expr('~F(A, B)'))
264
- False
265
- """
266
- return s .op != '~'
267
-
268
- def is_negative (s ):
269
- """s is a negated logical expression
270
- >>> is_negative(expr('F(A, B)'))
271
- False
272
- >>> is_negative(expr('~F(A, B)'))
273
- True
274
- """
275
- return s .op == '~'
276
-
277
- def is_literal (s ):
278
- """Is s a FOL literal?
279
- >>> is_literal(expr('~F(A, B)'))
280
- True
281
- >>> is_literal(expr('F(A, B)'))
282
- True
283
- >>> is_literal(expr('F(A, B) & G(B, C)'))
284
- False
285
- >>> is_literal(expr('~~A'))
286
- False
287
- >>> is_literal(expr('x')) # XXX I guess this is intended?
288
- True
289
- """
290
- return is_symbol (s .op ) or (s .op == '~' and is_symbol (s .args [0 ].op ))
291
-
292
- def literals (s ):
293
- """Return a list of the literals in expression s.
294
- >>> literals(expr('F(A, B)'))
295
- [F(A, B)]
296
- >>> literals(expr('~F(A, B)'))
297
- [~F(A, B)]
298
- >>> literals(expr('(F(A, B) & G(B, C)) ==> R(A, C)'))
299
- [F(A, B), G(B, C), R(A, C)]
300
- """
301
- if is_literal (s ):
302
- return [s ]
303
- else :
304
- return flatten (map (literals , s .args ))
305
-
306
- def flatten (seqs ): return sum (seqs , [])
307
-
308
259
def variables (s ):
309
260
"""Return a set of the variables in expression s.
310
261
>>> ppset(variables(F(x, A, y)))
311
262
set([x, y])
263
+ >>> ppset(variables(F(G(x), z)))
264
+ set([x, z])
312
265
>>> ppset(variables(expr('F(x, x) & G(x, y) & H(y, z) & R(A, z, z)')))
313
266
set([x, y, z])
314
267
"""
315
- if is_literal (s ):
316
- return set ([v for v in s .args if is_variable (v )])
317
- else :
318
- vars = set ([])
319
- for lit in literals (s ):
320
- vars = vars .union (variables (lit ))
321
- return vars
322
-
268
+ result = set ([])
269
+ def walk (s ):
270
+ if is_variable (s ):
271
+ result .add (s )
272
+ else :
273
+ for arg in s .args :
274
+ walk (arg )
275
+ walk (s )
276
+ return result
277
+
323
278
def is_definite_clause (s ):
324
279
"""returns True for exprs s of the form A & B & ... & C ==> D,
325
280
where all literals are positive. In clause form, this is
@@ -1158,46 +1113,28 @@ def d(y, x):
1158
1113
1159
1114
def pretty (x ):
1160
1115
t = type (x )
1161
- if t == dict :
1162
- return pretty_dict (x )
1163
- elif t == set :
1164
- return pretty_set (x )
1116
+ if t is dict : return pretty_dict (x )
1117
+ elif t is set : return pretty_set (x )
1118
+ else : return repr (x )
1165
1119
1166
1120
def pretty_dict (d ):
1167
- """Print the dictionary d.
1168
-
1169
- Prints a string representation of the dictionary
1170
- with keys in sorted order according to their string
1171
- representation: {a: A, d: D, ...}.
1121
+ """Return dictionary d's repr but with the items sorted.
1172
1122
>>> pretty_dict({'m': 'M', 'a': 'A', 'r': 'R', 'k': 'K'})
1173
1123
"{'a': 'A', 'k': 'K', 'm': 'M', 'r': 'R'}"
1174
1124
>>> pretty_dict({z: C, y: B, x: A})
1175
1125
'{x: A, y: B, z: C}'
1176
1126
"""
1177
-
1178
- def format (k , v ):
1179
- return "%s: %s" % (repr (k ), repr (v ))
1180
-
1181
- ditems = d .items ()
1182
- ditems .sort (key = str )
1183
- k , v = ditems [0 ]
1184
- dpairs = format (k , v )
1185
- for (k , v ) in ditems [1 :]:
1186
- dpairs += (', ' + format (k , v ))
1187
- return '{%s}' % dpairs
1127
+ return '{%s}' % ', ' .join ('%r: %r' % (k , v )
1128
+ for k , v in sorted (d .items (), key = repr ))
1188
1129
1189
1130
def pretty_set (s ):
1190
- """Print the set s.
1191
-
1131
+ """Return set s's repr but with the items sorted.
1192
1132
>>> pretty_set(set(['A', 'Q', 'F', 'K', 'Y', 'B']))
1193
1133
"set(['A', 'B', 'F', 'K', 'Q', 'Y'])"
1194
1134
>>> pretty_set(set([z, y, x]))
1195
1135
'set([x, y, z])'
1196
1136
"""
1197
-
1198
- slist = list (s )
1199
- slist .sort (key = str )
1200
- return 'set(%s)' % slist
1137
+ return 'set(%r)' % sorted (s , key = repr )
1201
1138
1202
1139
def pp (x ):
1203
1140
print pretty (x )
0 commit comments