def simp_not(x): if isinstance(x, Not): return x.args[0] if is_true(x): return Or() if is_false(x): return And() return Not(x)
def simp_or(x, y): if is_false(x): return y if is_true(x): return x if is_false(y): return x if is_true(y): return y return Or(x, y)
def extensionality(destrs): if not destrs: return Or() c = [] sort = destrs[0].sort.dom[0] x, y = Variable("X", sort), Variable("Y", sort) for d in destrs: vs = variables(d.sort.dom[1:]) eqn = Equals(d(*([x] + vs)), d(*([y] + vs))) if vs: eqn = lg.ForAll(vs, eqn) c.append(eqn) res = Implies(And(*c), Equals(x, y)) return res
def to_constraint(self): if isinstance(self.args[1],Some): if self.args[1].if_value() != None: return And(Implies(self.args[1].args[1], lg.Exists([self.args[1].args[0]], And(self.args[1].args[1],Equals(self.args[0],self.args[1].if_value())))), Or(lg.Exists([self.args[1].args[0]],self.args[1].args[1]), Equals(self.args[0],self.args[1].else_value()))) return lg.ForAll([self.args[1].args[0]], Implies(self.args[1].args[1], lu.substitute(self.args[1].args[1],{self.args[1].args[0]:self.args[0]}))) if is_individual(self.args[0]): return Equals(*self.args) return Iff(*self.args)
def _process_rule(self, a, b): # right part first # a -> b & c --> a -> b ; a -> c # (?) FIXME this is only correct when b & c != null ! if isinstance(b, And): for barg in b.args: self.process_rule(a, barg) # a -> b | c --> !b & !c -> !a # --> a & !b -> c # --> a & !c -> b elif isinstance(b, Or): # detect tautology first if not isinstance(a, Logic): # Atom # tautology: a -> a|c|... if a in b.args: raise TautologyDetected(a,b, 'a -> a|c|...') self.process_rule(And(*[Not(barg) for barg in b.args]), Not(a)) for bidx in range(len(b.args)): barg = b.args[bidx] brest= b.args[:bidx] + b.args[bidx+1:] self.process_rule(And(a, Not(barg)), Or(*brest)) # left part # a & b -> c --> IRREDUCIBLE CASE -- WE STORE IT AS IS # (this will be the basis of beta-network) elif isinstance(a, And): if b in a.args: raise TautologyDetected(a,b, 'a & b -> a') self.proved_rules.append((a,b)) # XXX NOTE at present we ignore !c -> !a | !b elif isinstance(a, Or): if b in a.args: raise TautologyDetected(a,b, 'a | b -> a') for aarg in a.args: self.process_rule(aarg, b) else: # both `a` and `b` are atoms self.proved_rules.append((a,b)) # a -> b self.proved_rules.append((Not(b), Not(a))) # !b -> !a
# no caching of unknown results print "z3 returned: {}".format(res) assert False result.append(None) return result if __name__ == '__main__': S = UninterpretedSort('S') X, Y, Z = (Var(n, S) for n in ['X', 'Y', 'Z']) BinRel = FunctionSort(S, S, Boolean) leq = Const('leq', BinRel) transitive1 = ForAll((X, Y, Z), Implies(And(leq(X, Y), leq(Y, Z)), leq(X, Z))) transitive2 = ForAll((X, Y, Z), Or(Not(leq(X, Y)), Not(leq(Y, Z)), leq(X, Z))) transitive3 = Not( Exists((X, Y, Z), And(leq(X, Y), leq(Y, Z), Not(leq(X, Z))))) antisymmetric = ForAll((X, Y), Implies(And(leq(X, Y), leq(Y, X), true), Eq(Y, X))) print z3_implies(transitive1, transitive2) print z3_implies(transitive2, transitive3) print z3_implies(transitive3, transitive1) print z3_implies(transitive3, antisymmetric) print print z3_implies(true, Iff(transitive1, transitive2)) print x, y = (Const(n, S) for n in ['x', 'y'])
('-', [alpha, alpha, alpha]), ('/', [alpha, alpha, alpha]), ] polymorphic_symbols = dict( (x, lg.Const(x, lg.FunctionSort(*y))) for x, y in polymorphic_symbols_list) polymorphic_macros_map = { '<=': '<', '>': '<', '>=': '<', } macros_expansions = { '<=': lambda t: Or(normalize_symbol(t.func)(*t.args), Equals(*t.args)), '>': lambda t: normalize_symbol(t.func)(t.args[1], t.args[0]), '>=': lambda t: Or( normalize_symbol(t.func)(t.args[1], t.args[0]), Equals(*t.args)), } def is_macro(term): return isinstance( term, lg.Apply ) and term.func.name in macros_expansions and iu.ivy_use_polymorphic_macros def expand_macro(term):
from logic import Symbol, And, Or, Not, Implication, Biconditional, model_check AKnight = Symbol("A is a Knight") AKnave = Symbol("A is a Knave") BKnight = Symbol("B is a Knight") BKnave = Symbol("B is a Knave") CKnight = Symbol("C is a Knight") CKnave = Symbol("C is a Knave") # Puzzle 0 # A says "I am both a knight and a knave." knowledge0 = And( # Structure of the generic problem Or(AKnight, AKnave), Not(And(AKnight, AKnave)), # Information about what the characters said Implication(AKnight, And(AKnight, AKnave)), Implication(AKnave, Not(And(AKnight, AKnave))) ) # Puzzle 1 # A says "We are both knaves." # B says nothing. knowledge1 = And( # Structure of the generic problem Or(AKnight, AKnave), Or(BKnight, BKnave), Not(And(AKnight, AKnave)),
from logic import Symbol, And, Or, Implication, Biconditional, Not, model_check AKnight = Symbol("A is a Knight") AKnave = Symbol("A is a Knave") BKnight = Symbol("B is a Knight") BKnave = Symbol("B is a Knave") CKnight = Symbol("C is a Knight") CKnave = Symbol("C is a Knave") OnlyOneKindForEach = And(Or(AKnave, AKnight), Or(BKnight, BKnave), Or(CKnave, CKnight)) # Puzzle 0 # A says "I am both a knight and a knave." knowledge0 = And(OnlyOneKindForEach, Implication(AKnight, And(AKnight, AKnave))) # Puzzle 1 # A says "We are both knaves." # B says nothing. WereBothKnaves = And(BKnave, AKnave) knowledge1 = And( OnlyOneKindForEach, Implication(AKnight, WereBothKnaves), Implication(AKnave, Not(WereBothKnaves)), ) # Puzzle 2
X, Y, Z = (Var(n, S) for n in ['X', 'Y', 'Z']) r = Const('r', BinaryRelationS) n = Const('n', BinaryRelationS) p = Const('p', UnaryRelationS) q = Const('q', UnaryRelationS) u = Const('u', UnaryRelationS) c11 = Concept([X], And(p(X), q(X))) c10 = Concept([X], And(p(X), Not(q(X)))) c01 = Concept([X], And(Not(p(X)), q(X))) c00 = Concept([X], And(Not(p(X)), Not(q(X)))) cu = Concept([X], u(X)) crn = Concept([X, Y], Or(r(X,Y), n(X,Y))) combiners = get_standard_combiners() combinations = get_standard_combinations() test('c11') test('c10') test('c01') test('c00') test('cu') test('crn') print for k, v in combiners.iteritems(): print k, ':', v print
from logic import And, Or, Symbol, Biconditional, Implication, Not, model_check AKnight = Symbol("A is a Knight") AKnave = Symbol("A is a Knave") BKnight = Symbol("B is a Knight") BKnave = Symbol("B is a Knave") CKnight = Symbol("C is a Knight") CKnave = Symbol("C is a Knave") # Puzzle 0 # A says "I am both a knight and a knave." knowledge0 = And(Or(AKnight, AKnave), Implication(AKnight, Not(AKnave)), Implication(AKnave, Not(AKnight)), Biconditional(AKnight, And(AKnight, AKnave))) # Puzzle 1 # A says "We are both knaves." # B says nothing. knowledge1 = And(Or(AKnight, AKnave), Implication(AKnight, Not(AKnave)), Implication(AKnave, Not(AKnight)), Or(BKnight, BKnave), Implication(BKnight, Not(BKnave)), Implication(BKnave, Not(BKnight)), Biconditional(AKnight, And(AKnave, BKnave))) # Puzzle 2 # A says "We are the same kind." # B says "We are of different kinds." knowledge2 = And( Or(AKnight, AKnave), Implication(AKnight, Not(AKnave)),
X, Y, Z = (Var(n, S) for n in ['X', 'Y', 'Z']) r = Const('r', BinaryRelationS) n = Const('n', BinaryRelationS) p = Const('p', UnaryRelationS) q = Const('q', UnaryRelationS) u = Const('u', UnaryRelationS) c11 = Concept('both',[X], And(p(X), q(X))) c10 = Concept('onlyp',[X], And(p(X), Not(q(X)))) c01 = Concept('onlyq',[X], And(Not(p(X)), q(X))) c00 = Concept('none',[X], And(Not(p(X)), Not(q(X)))) cu = Concept('u',[X], u(X)) crn = Concept('r_or_n',[X, Y], Or(r(X,Y), n(X,Y))) combiners = get_standard_combiners() combinations = get_standard_combinations() test('c11') test('c10') test('c01') test('c00') test('cu') test('crn') print for k, v in combiners.iteritems(): print k, ':', v print
['xy', 'other', 'x', 'y'], ['nstar', 'nplus'], ))]), ) facts = cd.get_facts() print "facts = [" for tag, formula in facts: print ' ', tag, formula, ',' print "]\n" state = And( ForAll([X, Y, Z], And( nstar(X, X), Implies(And(nstar(X, Y), nstar(Y, X)), Eq(X, Y)), Implies(And(nstar(X, Y), nstar(Y, Z)), nstar(X, Z)), Implies(And(nstar(X, Y), nstar(X, Z)), Or(nstar(Y, Z), nstar(Z, Y))), )), nstar(x, y), Not(Eq(x, y)), ForAll([Z], Implies(And(nstar(x, Z), Not(Eq(x, Z))), nstar(y, Z))), ) abstract_value = alpha(cd, state) print "abstract_value = [" for tag, value in abstract_value: print ' ', tag, value, ',' print "]\n"
festas = Symbol("festas") musica = Symbol("musica") jogos = Symbol("jogos") biblioteca = Symbol("biblioteca") escritorio = Symbol("escritorio") rooms = [ hall, estar, cozinha, jantar, festas, musica, jogos, biblioteca, escritorio ] # Initialize symbols list and knowledge base symbols = characters + weapons + rooms # The answer must contain one person, room, and weapon knowledge = And( Or(Mostarda, Black, Violeta, Marinho, Rosa, Branca), Or(faca, castical, revolver, corda, cano, chave), Or(hall, estar, cozinha, jantar, festas, musica, jogos, biblioteca, escritorio)) # If one person, room or weapon is in the answer, # it implicates that the others aren't """ for character1 in characters: for character2 in characters: if character1 != character2: knowledge.add(Implication(character1, Not(character2))) for weapon1 in weapons: for weapon2 in weapons:
knowledge = And() # Enter number of players n_players = 4 # Add another player, the cards of player 0 will be the answer n_players += 1 # Add cards to symbols list for card in cards: for index in range(n_players): symbols.append(Symbol(f"{card}{index}")) # The answer must contain one person, room, and weapon knowledge = And( Or(Symbol("Mostarda0"), Symbol("Black0"), Symbol("Violeta0"), Symbol("Marinho0"), Symbol("Rosa0"), Symbol("Branca0")), Or(Symbol("faca0"), Symbol("castical0"), Symbol("revoler0"), Symbol("corda0"), Symbol("cano0"), Symbol("chave0")), Or(Symbol("hall0"), Symbol("estar0"), Symbol("cozinha0"), Symbol("jantar0"), Symbol("festas0"), Symbol("musica0"), Symbol("jogos0"), Symbol("biblioteca0"), Symbol("escritorio0"))) # If one person, room or weapon is in the answer, # it implicates that the others aren't for character1 in characters: for character2 in characters: if character1 != character2: knowledge.add( Implication(Symbol(f"{character1}0"), Not(Symbol(f"{character2}0"))))
from logic import Symbol, And, Or, Not, Implication, Biconditional, model_check AKnight = Symbol("A is a Knight") AKnave = Symbol("A is a Knave") BKnight = Symbol("B is a Knight") BKnave = Symbol("B is a Knave") CKnight = Symbol("C is a Knight") CKnave = Symbol("C is a Knave") # Puzzle 0 # A says "I am both a knight and a knave." knowledge0 = And( #Puzzle constraints, a person must be either a Knigh or a Knave, not both And(Or(AKnight, AKnave), Not(And(AKnight, AKnave))), #Suposing that A is Knight Implication(AKnight, And(AKnight, AKnave)), #Suposing that A is Knave Implication(AKnave, Or(Not(AKnight), Not(AKnave)))) # Puzzle 1 # A says "We are both knaves." # B says nothing. knowledge1 = And(And(Or(AKnight, AKnave), Not(And(AKnight, AKnave))), And(Or(BKnight, BKnave), Not(And(BKnight, BKnave))), Implication(AKnight, And(AKnave, BKnave)), Implication(AKnave, Not(And(AKnave, BKnave)))) # Puzzle 2