def prove(kb, ans_body, indent=""): """returns True if kb |- ans_body """ kb.display(2, indent, 'yes <-', ' & '.join(ans_body)) if ans_body: selected = ans_body[0] # select first atom from ans_body if selected in kb.askables: return (yes(input("Is " + selected + " true? ")) and prove(kb, ans_body[1:], indent + " ")) else: return any( prove(kb, cl.body + ans_body[1:], indent + " ") for cl in kb.clauses_for_atom(selected)) else: return True # empty body is true
def prove_all_ass(self, ans_body, assumed=set()): """returns a list of sets of assumables that extends assumed to imply ans_body from self. ans_body is a list of atoms (it is the body of the answer clause). assumed is a set of assumables already assumed """ if ans_body: selected = ans_body[0] # select first atom from ans_body if selected in self.askables: if yes(input("Is "+selected+" true? ")): return self.prove_all_ass(ans_body[1:],assumed) else: return [] # no answers elif selected in self.assumables: return self.prove_all_ass(ans_body[1:],assumed|{selected}) else: return [ass for cl in self.clauses_for_atom(selected) for ass in self.prove_all_ass(cl.body+ans_body[1:],assumed) ] # union of answers for each clause with head=selected else: # empty body return [assumed] # one answer
def ask_askables(kb): return {at for at in kb.askables if yes(input("Is "+at+" true? "))}