def and_or_leaflists(remaining, indexed_kb, depth, antecedents = [], assumptions = []): '''Returns list of all entailing sets of leafs in the and-or backchaining tree''' if depth == 0 and len(antecedents) > 0: # fail return [] # (empty) list of lists elif len(remaining) == 0: # done with this level if len(antecedents) == 0: # found one return [assumptions] # list of lists else: return and_or_leaflists(antecedents, indexed_kb, depth - 1, [], assumptions) else: # more to go on this level literal = remaining[0] # first of remaining predicate = literal[0] if predicate not in indexed_kb: return and_or_leaflists(remaining[1:], indexed_kb, depth, antecedents, [literal] + assumptions) # shift literal to assumptions else: revisions = [] for rule in indexed_kb[predicate]: # indexed by predicate of literal theta = unify.unify(literal, parse.consequent(rule)) if theta != None: if depth == 0: # no depth for revision return [] # (empty) list of lists revisions.append([unify.subst(theta, remaining[1:]), # new remaining with substitutions indexed_kb, depth, unify.standardize(unify.subst(theta, parse.antecedent(rule))) + unify.subst(theta, antecedents), # new antecedents with substitutions unify.subst(theta, assumptions)]) # new assumptions with substitutions return itertools.chain(*[and_or_leaflists(*rev) for rev in revisions]) # list of lists (if any)
seen_antecedent_predicates = set() seen_consequent_predicates = set() for dc in definite_clauses: seen_consequent_predicates.add(parse.consequent(dc)[0]) for literal in parse.antecedent(dc): if literal[0][0:3] != 'etc': seen_antecedent_predicates.add(literal[0]) for predicate in seen_consequent_predicates: if predicate in seen_antecedent_predicates: seen_antecedent_predicates.remove(predicate) for predicate in seen_antecedent_predicates: warnings += "\n no etcetera axiom for literal: " + predicate if len(warnings) == 0: return "none" else: return warnings # run args = argparser.parse_args() inlines = args.infile.readlines() intext = "".join(inlines) kb, obs = parse.definite_clauses(parse.parse(intext)) obs = unify.standardize(obs) report = parsecheck(obs, kb) print(report, file=args.outfile) sys.exit()
def contextual_and_or_leaflists(remaining, indexed_kb, depth, context, antecedents=[], assumptions=[]): '''Returns list of all entailing sets of leafs in the and-or backchaining tree, with belief context''' if depth == 0 and len(antecedents) > 0: # fail return [] # (empty) list of lists elif len(remaining) == 0: # done with this level if len(antecedents) == 0: # found one return [assumptions] # list of lists else: return contextual_and_or_leaflists(antecedents, indexed_kb, depth - 1, context, [], assumptions) else: # more to go on this level literal = remaining[0] # first of remaining predicate = literal[0] if predicate not in indexed_kb: return contextual_and_or_leaflists( remaining[1:], indexed_kb, depth, context, antecedents, [literal] + assumptions) # shift literal to assumptions else: revisions = [] for rule in indexed_kb[ predicate]: # indexed by predicate of literal theta = unify.unify(literal, parse.consequent(rule)) if theta != None: if depth == 0: # no depth for revision return [] # (empty) list of lists revisions.append([ unify.subst( theta, remaining[1:]), # new remaining with substitutions indexed_kb, depth, context, unify.standardize( unify.subst(theta, parse.antecedent(rule))) + unify.subst( theta, antecedents), # new antecedents with substitutions unify.subst(theta, assumptions) ]) # new assumptions with substitutions for contextliteral in context: theta = unify.unify(literal, contextliteral) if theta != None: # literal unifies with context #print(str(literal) + " unifies with " + str(contextliteral) + " with theta " + str(theta) + " creating " + str(unify.subst(theta, literal))) revisions.append([ unify.subst( theta, remaining[1:]), # new remaining with substitutions indexed_kb, depth, context, # not revised unify.subst(theta, antecedents), # antecedents unify.subst(theta, assumptions) # assumptions ]) # should we "break" here now that we've found one? return itertools.chain(*[ contextual_and_or_leaflists(*rev) for rev in revisions ]) # list of lists (if any)