def checkMathPredicates(name, atom): args = get_args(atom) if is_variable(args[0]) or is_variable(args[1]): return False a = get_value(args[0]) b = get_value(args[1]) if name == 'isBigger': return a >= b elif name == 'equal': return a == b elif name == 'sum': if is_constant(args[2]) and (a + b == get_value(args[2])): return True elif is_variable(args[2]): args[2] = substitute(args[2], {get_name(args[2]): make_const(a + b)}) return True elif name == 'dif': if is_constant(args[2]) and (a - b == get_value(args[2])): return True elif is_variable(args[2]): args[2] = substitute(args[2], {get_name(args[2]): make_const(a - b)}) return True return False
def checkMathPredicates(name, atom, subst = None): args = get_args(atom) if is_variable(args[0]) or is_variable(args[1]): return False a = get_value(args[0]) b = get_value(args[1]) if name == 'isBigger': return a >= b elif name == 'isSmaller': return a < b elif name == 'equal': return a == b elif name == 'sum': if is_constant(args[2]) and (a + b == get_value(args[2])): return True elif is_variable(args[2]): subst[get_name(args[2])] = make_const(a + b) return True elif name == 'dif': if is_constant(args[2]) and (a - b == get_value(args[2])): return True elif is_variable(args[2]): subst[get_name(args[2])] = make_const(a - b) return True return False
def equal_terms(t1, t2): if is_constant(t1) and is_constant(t2): return get_value(t1) == get_value(t2) if is_variable(t1) and is_variable(t2): return get_name(t1) == get_name(t2) if is_function_call(t1) and is_function(t2): if get_head(t1) != get_head(t2): return all([equal_terms(get_args(t1)[i], get_args(t2)[i]) for i in range(len(get_args(t1)))]) return False
def forward_chaining(kb, theorem, verbose=True): # Salvăm baza de date originală, lucrăm cu o copie local_kb = deepcopy(kb) # Două variabile care descriu starea căutării got_new_facts = True # s-au găsit fapte noi la ultima căutare is_proved = False # a fost demostrată teorema # Verificăm dacă teorema este deja demonstrată for fact in filter(is_fact, local_kb): if unify(fact, theorem): if verbose: print("This already in KB: " + print_formula(fact, True)) is_proved = True break while (not is_proved) and got_new_facts: got_new_facts = False for rule in filter(is_rule, local_kb): # Pentru fiecare regulă new_facts = apply_rule(rule, list(filter(is_fact, local_kb))) new_facts = list( filter( lambda fact: not any( list( filter(lambda orig: is_equal_to(fact, orig), local_kb))), new_facts)) if new_facts: if verbose: print("Applied rule: " + print_formula(rule, True) + ", obtained " + str(len(new_facts)) + " new facts.") if any(filter(lambda t: is_variable(t), get_args(get_conclusion(rule)))) and \ any(filter(lambda fact: is_equal_to(fact, get_conclusion(rule)), new_facts)): print( "Demonstration is too general, the conclusion is not instantiated (facts obtained:", ",".join([print_formula(f, True) for f in new_facts]), ").") return False got_new_facts = True for fact in new_facts: #if verbose: print("New fact: " + print_formula(fact, True)) if unify(fact, theorem) != False: is_proved = True add_statement(local_kb, fact) if verbose: print("Now in KB: " + print_formula(fact, True)) break add_statement(local_kb, fact) if is_proved: break if verbose: if is_proved: print("The theorem is TRUE!") else: print("The theorem is FALSE!") return is_proved