Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 31204b3

Browse files
committed
logic.py back into the bathtub. Fixed variables() and pretty().
1 parent 2473d8d commit 31204b3

File tree

1 file changed

+20
-83
lines changed

1 file changed

+20
-83
lines changed

logic.py

Lines changed: 20 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -256,70 +256,25 @@ def is_prop_symbol(s):
256256
TRUE or FALSE."""
257257
return is_symbol(s) and s[0].isupper() and s != 'TRUE' and s != 'FALSE'
258258

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-
308259
def variables(s):
309260
"""Return a set of the variables in expression s.
310261
>>> ppset(variables(F(x, A, y)))
311262
set([x, y])
263+
>>> ppset(variables(F(G(x), z)))
264+
set([x, z])
312265
>>> ppset(variables(expr('F(x, x) & G(x, y) & H(y, z) & R(A, z, z)')))
313266
set([x, y, z])
314267
"""
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+
323278
def is_definite_clause(s):
324279
"""returns True for exprs s of the form A & B & ... & C ==> D,
325280
where all literals are positive. In clause form, this is
@@ -1158,46 +1113,28 @@ def d(y, x):
11581113

11591114
def pretty(x):
11601115
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)
11651119

11661120
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.
11721122
>>> pretty_dict({'m': 'M', 'a': 'A', 'r': 'R', 'k': 'K'})
11731123
"{'a': 'A', 'k': 'K', 'm': 'M', 'r': 'R'}"
11741124
>>> pretty_dict({z: C, y: B, x: A})
11751125
'{x: A, y: B, z: C}'
11761126
"""
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))
11881129

11891130
def pretty_set(s):
1190-
"""Print the set s.
1191-
1131+
"""Return set s's repr but with the items sorted.
11921132
>>> pretty_set(set(['A', 'Q', 'F', 'K', 'Y', 'B']))
11931133
"set(['A', 'B', 'F', 'K', 'Q', 'Y'])"
11941134
>>> pretty_set(set([z, y, x]))
11951135
'set([x, y, z])'
11961136
"""
1197-
1198-
slist = list(s)
1199-
slist.sort(key=str)
1200-
return 'set(%s)' % slist
1137+
return 'set(%r)' % sorted(s, key=repr)
12011138

12021139
def pp(x):
12031140
print pretty(x)

0 commit comments

Comments
 (0)