def main(): x = logic.Var('x') y = logic.Var('y') z = logic.Var('z') a = logic.Var('a') nil = logic.Atom('nil') more = logic.Var('more') zero = logic.Atom('0') length_nil = logic.Clause(logic.Relation('length', (nil, zero))) length_one = logic.Clause( logic.Relation('length', (logic.Relation('pair', (x, more)), logic.Relation('+1', [a]))), [logic.Relation('length', (more, a))]) db = {} logic.store(db, length_nil) logic.store(db, length_one) print 'Database:' print db print list = logic.Relation( 'pair', (x, logic.Relation('pair', (y, logic.Relation('pair', (z, nil)))))) query = logic.Relation('length', (list, a)) print 'Query:', query print logic.prolog_prove([query], db)
def main(): x = logic.Var('x') y = logic.Var('y') z = logic.Var('z') a = logic.Var('a') nil = logic.Atom('nil') more = logic.Var('more') zero = logic.Atom('0') length_nil = logic.Clause(logic.Relation('length', (nil, zero))) length_one = logic.Clause( logic.Relation('length', (logic.Relation('pair', (x, more)), logic.Relation('+1', [a]))), [logic.Relation('length', (more, a))]) db = {} logic.store(db, length_nil) logic.store(db, length_one) print 'Database:' print db print four = logic.Relation( '+1', [logic.Relation( '+1', [logic.Relation( '+1', [logic.Relation('+1', [zero])])])]) query = logic.Relation('length', (x, four)) print 'Query:', query print logic.prolog_prove([query], db)
def main(): x = logic.Var('x') y = logic.Var('y') a = logic.Var('a') more = logic.Var('more') member_first = logic.Clause( logic.Relation('member', (x, logic.Relation('pair', (x, more))))) member_last = logic.Clause( logic.Relation('member', (x, logic.Relation('pair', (y, x))))) member_rest = logic.Clause( logic.Relation('member', (x, logic.Relation('pair', (y, more)))), [logic.Relation('member', (x, more))]) db = {} logic.store(db, member_first) logic.store(db, member_last) logic.store(db, member_rest) print 'Database:' print db print query = logic.Relation('member', (logic.Atom('foo'), x)) print 'Query:', query print logic.prolog_prove([query], db)
def main(): x = logic.Var('x') y = logic.Var('y') z = logic.Var('z') a = logic.Var('a') nil = logic.Atom('nil') more = logic.Var('more') zero = logic.Atom('0') length_nil = logic.Clause(logic.Relation('length', (nil, zero))) length_one = logic.Clause( logic.Relation('length', (logic.Relation('pair', (x, more)), logic.Relation('+1', [a]))), [logic.Relation('length', (more, a))]) member_first = logic.Clause( logic.Relation('member', (x, logic.Relation('pair', (x, more))))) member_last = logic.Clause( logic.Relation('member', (x, logic.Relation('pair', (y, x))))) member_end = logic.Clause( logic.Relation('member', (x, logic.Relation('pair', (x, nil))))) member_rest = logic.Clause( logic.Relation('member', (x, logic.Relation('pair', (y, more)))), [logic.Relation('member', (x, more))]) db = {} logic.store(db, length_nil) logic.store(db, length_one) logic.store(db, member_end) logic.store(db, member_first) logic.store(db, member_last) logic.store(db, member_rest) print 'Database:' print db print four = logic.Relation( '+1', [logic.Relation( '+1', [logic.Relation( '+1', [logic.Relation('+1', [zero])])])]) foo = logic.Atom('foo') has_foo = logic.Relation('member', (foo, x)) length_4 = logic.Relation('length', (x, a)) print 'Query:', has_foo, length_4 print logic.prolog_prove([has_foo, length_4], db)
def main(): x = logic.Var('x') y = logic.Var('y') z = logic.Var('z') a = logic.Var('a') nil = logic.Atom('nil') more = logic.Var('more') zero = logic.Atom('0') length_nil = logic.Clause(logic.Relation('length', (nil, zero))) length_one = logic.Clause( logic.Relation('length', (logic.Relation('pair', (x, more)), logic.Relation('+1', [a]))), [logic.Relation('length', (more, a))]) member_first = logic.Clause( logic.Relation('member', (x, logic.Relation('pair', (x, more))))) member_last = logic.Clause( logic.Relation('member', (x, logic.Relation('pair', (y, x))))) member_end = logic.Clause( logic.Relation('member', (x, logic.Relation('pair', (x, nil))))) member_rest = logic.Clause( logic.Relation('member', (x, logic.Relation('pair', (y, more)))), [logic.Relation('member', (x, more))]) db = {} logic.store(db, length_nil) logic.store(db, length_one) logic.store(db, member_end) logic.store(db, member_first) logic.store(db, member_last) logic.store(db, member_rest) print 'Database:' print db print four = logic.Relation('+1', [ logic.Relation('+1', [logic.Relation('+1', [logic.Relation('+1', [zero])])]) ]) foo = logic.Atom('foo') has_foo = logic.Relation('member', (foo, x)) length_4 = logic.Relation('length', (x, a)) print 'Query:', has_foo, length_4 print logic.prolog_prove([has_foo, length_4], db)
def main(): #logging.basicConfig(level=logging.DEBUG) db = {} kim = logic.Atom('Kim') robin = logic.Atom('Robin') sandy = logic.Atom('Sandy') lee = logic.Atom('Lee') cats = logic.Atom('cats') x = logic.Var('x') sandy_likes = logic.Relation('likes', (sandy, x)) likes_cats = logic.Relation('likes', (x, cats)) sandy_likes_rule = logic.Clause(sandy_likes, [likes_cats]) kim_likes = logic.Relation('likes', (kim, x)) likes_lee = logic.Relation('likes', (x, lee)) likes_kim = logic.Relation('likes', (x, kim)) kim_likes_rule = logic.Clause(kim_likes, [likes_lee, likes_kim]) likes_self = logic.Clause(logic.Relation('likes', (x, x))) klr = logic.Clause(logic.Relation('likes', (kim, robin))) sll = logic.Clause(logic.Relation('likes', (sandy, lee))) slk = logic.Clause(logic.Relation('likes', (sandy, kim))) rlc = logic.Clause(logic.Relation('likes', (robin, cats))) logic.store(db, sandy_likes_rule) logic.store(db, kim_likes_rule) logic.store(db, likes_self) logic.store(db, klr) logic.store(db, sll) logic.store(db, slk) logic.store(db, rlc) print 'Database:' print db print query = logic.Relation('likes', (sandy, logic.Var('who'))) print 'Query:', str(query) print logic.prolog_prove([query], db)
def main(): #logging.basicConfig(level=logging.DEBUG) db = {} kim = logic.Atom('Kim') robin = logic.Atom('Robin') sandy = logic.Atom('Sandy') lee = logic.Atom('Lee') cats = logic.Atom('cats') x = logic.Var('x') y = logic.Var('y') z = logic.Var('z') self_likes = logic.Clause(logic.Relation('likes', (x, x))) transitive_likes = logic.Clause( logic.Relation('likes', (x, y)), (logic.Relation('likes', (x, z)), logic.Relation('likes', (z, y)))) klr = logic.Clause(logic.Relation('likes', (kim, robin))) sll = logic.Clause(logic.Relation('likes', (sandy, lee))) slk = logic.Clause(logic.Relation('likes', (sandy, kim))) rlc = logic.Clause(logic.Relation('likes', (robin, cats))) llr = logic.Clause(logic.Relation('likes', (lee, robin))) logic.store(db, klr) logic.store(db, sll) logic.store(db, slk) logic.store(db, rlc) logic.store(db, llr) logic.store(db, self_likes) logic.store(db, transitive_likes) print 'Database:' print db print query = logic.Relation('likes', (sandy, logic.Var('who'))) print 'Query:', str(query) print logic.prolog_prove([query], db)
def main(): #logging.basicConfig(level=logging.DEBUG) db = {} kim = logic.Atom('Kim') robin = logic.Atom('Robin') sandy = logic.Atom('Sandy') lee = logic.Atom('Lee') cats = logic.Atom('cats') x = logic.Var('x') y = logic.Var('y') z = logic.Var('z') self_likes = logic.Clause(logic.Relation('likes', (x, x))) transitive_likes = logic.Clause(logic.Relation('likes', (x, y)), (logic.Relation('likes', (x, z)), logic.Relation('likes', (z, y)))) klr = logic.Clause(logic.Relation('likes', (kim, robin))) sll = logic.Clause(logic.Relation('likes', (sandy, lee))) slk = logic.Clause(logic.Relation('likes', (sandy, kim))) rlc = logic.Clause(logic.Relation('likes', (robin, cats))) llr = logic.Clause(logic.Relation('likes', (lee, robin))) logic.store(db, klr) logic.store(db, sll) logic.store(db, slk) logic.store(db, rlc) logic.store(db, llr) logic.store(db, self_likes) logic.store(db, transitive_likes) print 'Database:' print db print query = logic.Relation('likes', (sandy, logic.Var('who'))) print 'Query:', str(query) print logic.prolog_prove([query], db)
def main(): print 'Welcome to PyLogic. Type "help" for help.' args = argparser.parse_args() db = read_db(args.db_file) if args.db_file else {} if args.log: logging.basicConfig(level=logging.DEBUG) print_db(db) while True: try: line = raw_input('>> ') except EOFError: break if not line: continue if line == 'quit': break if line == 'help': print help continue try: q = parse(line) except ParseError as e: print e continue except TokenError as e: print e continue if isinstance(q, logic.Relation): try: logic.prolog_prove([q], db) except KeyboardInterrupt: print 'Cancelled.' elif isinstance(q, logic.Clause): logic.store(db, q) print_db(db) print 'Goodbye.'