def bindings(ifs, facts, dict, why): if ifs == []: return [(copy_dict(dict), [])] # all conjuncts matched res = [] head, tail = ifs[0], ifs[1:] if head[0] == 'ask': ground = substitute(head[1:], dict) if ask_user(ground, facts, why): for (dict2, proof2) in bindings(tail, facts, dict, why): res.append((dict2, [(ground, 'told')] + proof2)) elif head[0] == 'not': ground = substitute(head[1:], dict) if not asserted(ground, facts) or asserted(['not']+ground, facts): for (dict2, proof2) in bindings(tail, facts, dict, why): res.append((dict2, [(ground, 'not')] + proof2)) else: for (fact, proof) in facts: matched, changes = match(head, fact, dict, {}) if matched: for (dict2, proof2) in bindings(tail, facts, dict, why): res.append((dict2, [(fact, proof)] + proof2)) for (var, env) in changes: env[var] = '?' return res
def conjunct(ifs, known, dict, why): if ifs == []: return [(copy_dict(dict), [])] # all conjuncts matched res = [] head, tail = ifs[0], ifs[1:] if head[0] == 'ask': term = substitute(head[1:], dict) if ask_user(term, known, why): for (dict2, proof2) in conjunct(tail, known, dict, why): res.append((dict2, [(term, 'told')] + proof2)) elif head[0] == 'not': term = substitute(head[1:], dict) if not known.search_unique(term) or \ known.search_unique(['not'] + term): for (dict2, proof2) in conjunct(tail, known, dict, why): res.append((dict2, [(term, 'not')] + proof2)) else: for (fact, proof) in known.search(head, dict): matched, changes = match(head, fact, dict, {}) if matched: for (dict2, proof2) in conjunct(tail, known, dict, why): res.append((dict2, [(fact, proof)] + proof2)) for (var, env) in changes: env[var] = '?' return res