Skip to content

Commit 386814f

Browse files
committed
implies changed to '==>'
Following a suggestion by C.G.Vedant, changed |implies| to |’==>’|
1 parent 75617fa commit 386814f

File tree

3 files changed

+22
-24
lines changed

3 files changed

+22
-24
lines changed

logic.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
from utils import (
3535
removeall, unique, first, every, argmax, probability, num_or_str,
36-
isnumber, issequence, Symbol, Expr, expr, subexpressions, implies
36+
isnumber, issequence, Symbol, Expr, expr, subexpressions
3737
)
3838
import agents
3939

@@ -713,8 +713,8 @@ def translate_to_SAT(init, transition, goal, time):
713713
action_sym[(s, action, t)] = Expr("Transition_{}".format(next(transition_counter)))
714714

715715
# Change the state from s to s_
716-
clauses.append(action_sym[s, action, t] |implies| state_sym[s, t])
717-
clauses.append(action_sym[s, action, t] |implies| state_sym[s_, t + 1])
716+
clauses.append(action_sym[s, action, t] |'==>'| state_sym[s, t])
717+
clauses.append(action_sym[s, action, t] |'==>'| state_sym[s_, t + 1])
718718

719719
#Allow only one state at any time
720720
for t in range(time+1):

tests/test_logic.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import pytest
22
from logic import *
3-
from utils import InfixOp, expr_handle_infix_ops, count, implies, equiv
3+
from utils import expr_handle_infix_ops, count
44

55

66
def test_expr():
77
assert repr(expr('P <=> Q(1)')) == '(P <=> Q(1))'
88
assert repr(expr('P & Q | ~R(x, F(x))')) == '((P & Q) | ~R(x, F(x)))'
99
assert (expr_handle_infix_ops('P & Q ==> R & ~S')
10-
== "P & Q |InfixOp('==>', None)| R & ~S")
10+
== "P & Q |'==>'| R & ~S")
1111

1212
def test_extend():
1313
assert extend({x: 1}, y, 2) == {x: 1, y: 2}
@@ -17,7 +17,7 @@ def test_PropKB():
1717
assert count(kb.ask(expr) for expr in [A, C, D, E, Q]) is 0
1818
kb.tell(A & E)
1919
assert kb.ask(A) == kb.ask(E) == {}
20-
kb.tell(E |implies| C)
20+
kb.tell(E |'==>'| C)
2121
assert kb.ask(C) == {}
2222
kb.retract(E)
2323
assert kb.ask(E) is False
@@ -42,8 +42,8 @@ def test_KB_wumpus():
4242
B[2,1] = Symbol("B[2,1]")
4343

4444
kb_wumpus.tell(~P[1,1])
45-
kb_wumpus.tell(B[1,1] |equiv| ((P[1,2] | P[2,1])))
46-
kb_wumpus.tell(B[2,1] |equiv| ((P[1,1] | P[2,2] | P[3,1])))
45+
kb_wumpus.tell(B[1,1] |'<=>'| ((P[1,2] | P[2,1])))
46+
kb_wumpus.tell(B[2,1] |'<=>'| ((P[1,1] | P[2,2] | P[3,1])))
4747
kb_wumpus.tell(~B[1,1])
4848
kb_wumpus.tell(B[2,1])
4949

utils.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -373,10 +373,11 @@ def __floordiv__(self, rhs): return Expr('//', self, rhs)
373373
def __matmul__(self, rhs): return Expr('@', self, rhs)
374374

375375
def __or__(self, rhs):
376+
"Allow both P | Q, and P |'==>'| Q."
376377
if isinstance(rhs, Expression) :
377378
return Expr('|', self, rhs)
378379
else:
379-
return NotImplemented # So that InfixOp can handle it
380+
return PartialExpr(rhs, self)
380381

381382
# Reverse operator overloads
382383
def __radd__(self, lhs): return Expr('+', lhs, self)
@@ -451,37 +452,34 @@ def arity(expression):
451452

452453
# For operators that are not defined in Python, we allow new InfixOps:
453454

454-
class InfixOp:
455-
"""Allow 'P |implies| Q, where P, Q are Exprs and implies is an InfixOp."""
456-
def __init__(self, op, lhs=None): self.op, self.lhs = op, lhs
457-
def __call__(self, lhs, rhs): return Expr(self.op, lhs, rhs)
458-
def __or__(self, rhs): return Expr(self.op, self.lhs, rhs)
459-
def __ror__(self, lhs): return InfixOp(self.op, lhs)
460-
def __repr__(self): return "InfixOp('{}', {})".format(self.op, self.lhs)
461-
462-
infix_ops = (implies, rimplies, equiv) = [InfixOp(o) for o in ['==>', '<==', '<=>']]
455+
class PartialExpr:
456+
"""Given 'P |'==>'| Q, first form PartialExpr('==>', P), then combine with Q."""
457+
def __init__(self, op, lhs): self.op, self.lhs = op, lhs
458+
def __or__(self, rhs): return Expr(self.op, self.lhs, rhs)
459+
def __repr__(self): return "PartialExpr('{}', {})".format(self.op, self.lhs)
463460

464461
def expr(x):
465462
"""Shortcut to create an Expression. x is a str in which:
466463
- identifiers are automatically defined as Symbols.
467-
- '==>' is treated as an infix |implies|, as are all infix_ops
464+
- ==> is treated as an infix |'==>'|, as are <== and <=>.
468465
If x is already an Expression, it is returned unchanged. Example:
469466
>>> expr('P & Q ==> Q')
470467
((P & Q) ==> Q)
471468
"""
472469
if isinstance(x, str):
473-
return eval(expr_handle_infix_ops(x),
474-
defaultkeydict(Symbol, InfixOp=InfixOp))
470+
return eval(expr_handle_infix_ops(x), defaultkeydict(Symbol))
475471
else:
476472
return x
477473

474+
infix_ops = '==> <== <=>'.split()
475+
478476
def expr_handle_infix_ops(x):
479-
"""Given a str, return a new str with '==>' replaced by |InfixOp('==>')|, etc.
477+
"""Given a str, return a new str with ==> replaced by |'==>'|, etc.
480478
>>> expr_handle_infix_ops('P ==> Q')
481-
"P |InfixOp('==>', None)| Q"
479+
"P |'==>'| Q"
482480
"""
483481
for op in infix_ops:
484-
x = x.replace(op.op, '|' + str(op) + '|')
482+
x = x.replace(op, '|' + repr(op) + '|')
485483
return x
486484

487485
class defaultkeydict(collections.defaultdict):

0 commit comments

Comments
 (0)