Exemple #1
0
def print_solns(known, filter):
    sources = {'rule':[], 'told':[], 'init':[], 'atom':[]}

    for (fact, proof) in known.members():
        if not filter or match(filter, fact, {}, {})[0]:
            if type(proof) == type(()):
                sources['rule'].append((fact, proof))         # deduced
            elif proof == 'told' or proof == 'not':
                sources['told'].append(fact)        
            elif proof == 'initial':
                sources['init'].append(fact)        
            elif proof == 'atomic':
                sources['atom'].append(fact)
    
    if not sources['rule']:
        print 'I have not deduced any new facts.'
    else:
        print 'I deduced these facts...'
        for (fact, proof) in sources['rule']:
            print '  ', external([fact])              #, '(by rule',proof[0]+')'

    if sources['told']:
        print 'You told me these facts...'
        for fact in sources['told']: 
            print '  ', external([fact])

    if sources['init']:
        print 'I started with these facts...'
        for fact in sources['init']: 
            print '  ', external([fact])
Exemple #2
0
def ask_user(goal, dict, known, why):
    fact = substitute(goal, dict)
    if fact in known:
        return 1
    elif (['not'] + fact) in known:
        return 0

    while 1:
        ans = raw_input('is this true: "' + external([fact]) + '" ? ')
        if ans in positive:
            known.append(fact)
            return 1

        elif ans in negative:
            known.append(['not'] + fact)
            return 0

        elif ans == 'why':
            for (goal, dict, rule) in why:
                print 'to prove',
                print '"' + external([substitute(goal, dict)]) + '"',
                print 'by rule', rule
            print 'this was part of your original query.'

        elif ans == 'browse':
            browse_pattern(kbase, raw_input('enter browse pattern: '))

        elif ans == 'stop':
            raise stop_proof

        else:
            print 'what?  (expecting "y", "n", "why", "browse", or "stop")'
def ask_user(kbase, goal, dict, known, why):
    fact = substitute(goal, dict)
    if known.search_unique(fact):
        return 1
    elif known.search_unique(['not'] + fact):
        return 0

    while 1:
        ans = raw_input('is this true: "' + external([fact]) + '" ? ')
        if ans in positive:
            known.store(fact, fact)
            return 1

        elif ans in negative:
            known.store(['not'] + fact, ['not'] + fact)
            return 0

        elif ans == 'why':
            while why:
                (goal, dict, rule), why = why
                print 'to prove',
                print '"' + external([substitute(goal, dict)]) + '"',
                print 'by rule', rule
            print 'this was part of your original query.'

        elif ans == 'browse':
            kbase.browse_pattern(raw_input('enter browse pattern: '))

        elif ans == 'stop':
            raise stop_proof

        else:
            print 'what?  (expecting "y", "n", "why", "browse", or "stop")'
Exemple #4
0
def print_solns(known, filter):
    sources = {'rule': [], 'told': [], 'init': [], 'atom': []}

    for (fact, proof) in known.members():
        if not filter or match(filter, fact, {}, {})[0]:
            if type(proof) == type(()):
                sources['rule'].append((fact, proof))  # deduced
            elif proof == 'told' or proof == 'not':
                sources['told'].append(fact)
            elif proof == 'initial':
                sources['init'].append(fact)
            elif proof == 'atomic':
                sources['atom'].append(fact)

    if not sources['rule']:
        print 'I have not deduced any new facts.'
    else:
        print 'I deduced these facts...'
        for (fact, proof) in sources['rule']:
            print '  ', external([fact])  #, '(by rule',proof[0]+')'

    if sources['told']:
        print 'You told me these facts...'
        for fact in sources['told']:
            print '  ', external([fact])

    if sources['init']:
        print 'I started with these facts...'
        for fact in sources['init']:
            print '  ', external([fact])
Exemple #5
0
def ask_user(goal, dict, known, why):
    fact = substitute(goal, dict)
    if fact in known:
        return 1
    elif (['not'] + fact) in known:
        return 0
    
    while 1:
        ans = raw_input('is this true: "' + external([fact]) + '" ? ')
        if ans in positive:
            known.append(fact)
            return 1

        elif ans in negative:
            known.append(['not'] + fact)
            return 0

        elif ans == 'why':
            for (goal, dict, rule) in why:
                print 'to prove', 
                print '"' + external([substitute(goal,dict)]) + '"', 
                print 'by rule', rule
            print 'this was part of your original query.'
        
        elif ans == 'browse':
            browse_pattern(kbase, raw_input('enter browse pattern: '))

        elif ans == 'stop':
            raise stop_proof

        else:
            print 'what?  (expecting "y", "n", "why", "browse", or "stop")'
Exemple #6
0
def ask_user(kbase, goal, dict, known, why):
    fact = substitute(goal, dict)
    if known.search_unique(fact):
        return 1
    elif known.search_unique(['not'] + fact):
        return 0
    
    while 1:
        ans = raw_input('is this true: "' + external([fact]) + '" ? ')
        if ans in positive:
            known.store(fact, fact)
            return 1

        elif ans in negative:
            known.store(['not'] + fact, ['not'] + fact)
            return 0

        elif ans == 'why':
            while why:
                (goal, dict, rule), why = why
                print 'to prove', 
                print '"' + external([substitute(goal,dict)]) + '"', 
                print 'by rule', rule
            print 'this was part of your original query.'
        
        elif ans == 'browse':
            kbase.browse_pattern(raw_input('enter browse pattern: '))

        elif ans == 'stop':
            raise stop_proof

        else:
            print 'what?  (expecting "y", "n", "why", "browse", or "stop")'
def ask_user(fact, known, why):
    global user_answers
    if known.search_unique(fact):
        return known.search_unique(fact)[0][1]

    user_answers = 1
    while 1:
        ans = raw_input('what is the certainty of: ' + external([fact]) + ' ?') 
        if ans == 'why':
            print 'to see if rule', why, 'can be applied'
        elif ans == 'where':
            print_solns(known, None)
        elif ans == 'browse':
            kbase.browse_pattern(raw_input('enter browse pattern: '))
        elif ans == 'stop':
            raise stop_chaining
        else: 
            try:
                num = eval(ans)
                if is_numeric(num) and 0.0 <= num <= 1.0: 
                    known.store(fact, (fact, num, 'told')) 
                    return num
            except: pass
            print 'what? (expecting ',
            print '"0.0...1.0", "why", "where", "browse", or "stop")'
Exemple #8
0
def ask_user(fact, known, why):
    global user_answers
    if known.search_unique(fact):
        return known.search_unique(fact)[0][1]

    user_answers = 1
    while 1:
        ans = raw_input('what is the certainty of: ' + external([fact]) + ' ?') 
        if ans == 'why':
            print 'to see if rule', why, 'can be applied'
        elif ans == 'where':
            print_solns(known, None)
        elif ans == 'browse':
            kbase.browse_pattern(raw_input('enter browse pattern: '))
        elif ans == 'stop':
            raise stop_chaining
        else: 
            try:
                num = eval(ans)
                if is_numeric(num) and 0.0 <= num <= 1.0: 
                    known.store(fact, (fact, num, 'told')) 
                    return num
            except: pass
            print 'what? (expecting ',
            print '"0.0...1.0", "why", "where", "browse", or "stop")'
def ask_user(fact, known, why):
    global user_answers
    for (item, proof) in known:
        if item == fact: 
            return 1
        if item == ['not'] + fact: 
            return 0

    user_answers = 1
    while 1:
        ans = raw_input('is this true: ' + external([fact]) + ' ?') 
        if ans in ['y','Y','yes','YES']:
            known.append((fact, 'told'))
            return 1
        elif ans in ['n','N','no','NO']:
            known.append((['not'] + fact), 'told')
            return 0
        elif ans == 'why':
            print 'to see if rule', why, 'can be applied'
        elif ans == 'where':
            print_solns(known, None)
        elif ans == 'browse':
            browse_pattern(rulebase, raw_input('enter browse pattern: '))
        elif ans == 'stop':
            raise stop_chaining
        else:
            print 'what? ',
            print '(expecting "y", "n", "why", "where", "browse", or "stop")'
Exemple #10
0
def ask_user(fact, known, why):
    global user_answers
    if known.search_unique(fact):
        return 1
    elif known.search_unique(['not'] + fact):
        return 0

    user_answers = 1
    while 1:
        ans = raw_input('is this true: ' + external([fact]) + ' ?') 
        if ans in ['y','Y','yes','YES']:
            known.store(fact, (fact, 'told'))
            return 1
        elif ans in ['n','N','no','NO']:
            known.store(['not']+fact, (['not']+fact, 'told'))
            return 0
        elif ans == 'why':
            print 'to see if rule', why, 'can be applied'
        elif ans == 'where':
            print_solns(known, None)
        elif ans == 'browse':
            kbase.browse_pattern(raw_input('enter browse pattern: '))
        elif ans == 'stop':
            raise stop_chaining
        else:
            print 'what? ',
            print '(expecting "y", "n", "why", "where", "browse", or "stop")'
Exemple #11
0
def ask_user(fact, known, why):
    global user_answers
    for (item, proof) in known:
        if item == fact:
            return 1
        if item == ['not'] + fact:
            return 0

    user_answers = 1
    while 1:
        ans = raw_input('is this true: ' + external([fact]) + ' ?')
        if ans in ['y', 'Y', 'yes', 'YES']:
            known.append((fact, 'told'))
            return 1
        elif ans in ['n', 'N', 'no', 'NO']:
            known.append((['not'] + fact), 'told')
            return 0
        elif ans == 'why':
            print 'to see if rule', why, 'can be applied'
        elif ans == 'where':
            print_solns(known, None)
        elif ans == 'browse':
            browse_pattern(rulebase, raw_input('enter browse pattern: '))
        elif ans == 'stop':
            raise stop_chaining
        else:
            print 'what? ',
            print '(expecting "y", "n", "why", "where", "browse", or "stop")'
Exemple #12
0
def report(binding, query, pmode, proof):
    if not pmode:
        pmode = print_mode  # use current global mode
    else:
        pmode = pmode[0]
        if pmode == None:
            return binding  # return solns to caller
            # else use pmode passed
    if binding == {}:
        print 'yes: (no variables)'
    else:
        if pmode[0] == 'vars':
            print 'yes: ',
            for var in binding.keys():
                print var, '=', binding[var], ' ',
            print
        else:
            print 'yes: ' + external(substitute(query, binding))

    if pmode[1] == 'all':
        raise backtrack
    else:
        print
        if input_yes('show proof ? '):
            ignore = trace_tree(proof, binding, 0)

        if input_yes('more solutions? '):
            print
            raise backtrack
        else:
            raise stop_proof
Exemple #13
0
def report(binding, query, pmode, proof):
    if not pmode:
        pmode = print_mode                         # use current global mode
    else:
        pmode = pmode[0]
        if pmode == None:
            return binding                         # return solns to caller
                                                   # else use pmode passed
    if binding == {}:
        print 'yes: (no variables)'
    else:
        if pmode[0] == 'vars':
            print 'yes: ', 
            for var in binding.keys():
                print var, '=', binding[var], ' ',
            print
        else:
            print 'yes: ' + external(substitute(query, binding))
    
    if pmode[1] == 'all':
        raise backtrack
    else:
        print
        if input_yes('show proof ? '):
            ignore = trace_tree(proof, binding, 0)

        if input_yes('more solutions? '):
            print
            raise backtrack
        else:
            raise stop_proof
Exemple #14
0
def ask_user(fact, known, why):
    global user_answers
    if known.search_unique(fact):
        return 1
    elif known.search_unique(['not'] + fact):
        return 0

    user_answers = 1
    while 1:
        ans = raw_input('is this true: ' + external([fact]) + ' ?')
        if ans in ['y', 'Y', 'yes', 'YES']:
            known.store(fact, (fact, 'told'))
            return 1
        elif ans in ['n', 'N', 'no', 'NO']:
            known.store(['not'] + fact, (['not'] + fact, 'told'))
            return 0
        elif ans == 'why':
            print 'to see if rule', why, 'can be applied'
        elif ans == 'where':
            print_solns(known, None)
        elif ans == 'browse':
            kbase.browse_pattern(raw_input('enter browse pattern: '))
        elif ans == 'stop':
            raise stop_chaining
        else:
            print 'what? ',
            print '(expecting "y", "n", "why", "where", "browse", or "stop")'
Exemple #15
0
def trace_tree(proof, dict, level): 
    while proof[0] != ():
        (goal, how), proof = proof[0], proof[1:]

        print ' ' * level,
        print '"' + external([substitute(goal, dict)]) + '"',

        if how == 'not':
            print 'by failure to prove', \
                     '"' + external([substitute(goal[1:], dict)]) + '"'
    
        elif how == 'told':
            if goal == ['true']:
                print 'is an absolute truth'
            else:
                print 'by your answer'
 
        else:
            print 'by rule', how[0]
            proof = trace_tree(proof, how[1], level + 3)
    
    return proof[1:]     
Exemple #16
0
def trace_tree(proof, dict, level):
    while proof[0] != ():
        (goal, how), proof = proof[0], proof[1:]

        print ' ' * level,
        print '"' + external([substitute(goal, dict)]) + '"',

        if how == 'not':
            print 'by failure to prove', \
                     '"' + external([substitute(goal[1:], dict)]) + '"'

        elif how == 'told':
            if goal == ['true']:
                print 'is an absolute truth'
            else:
                print 'by your answer'

        else:
            print 'by rule', how[0]
            proof = trace_tree(proof, how[1], level + 3)

    return proof[1:]
Exemple #17
0
def report(kbase, binding, query, pmode):
    if not pmode:
        pmode = print_mode  # use current global mode
    else:
        pmode = pmode[0]

    if binding == {}:
        print 'yes: (no variables)'
    else:
        if pmode[0] == 'vars':
            print 'yes: ',
            for var in binding.keys():
                print var, '=', binding[var], ' ',
            print
        else:
            print 'yes: ' + external(substitute(query, binding))

    if pmode[1] == 'all':
        return 1
    else:
        return input_yes(kbase, 'more solutions? ')
def report(kbase, binding, query, pmode):
    if not pmode:
        pmode = print_mode                         # use current global mode
    else:
        pmode = pmode[0]
    
    if binding == {}:
        print 'yes: (no variables)'
    else:
        if pmode[0] == 'vars':
            print 'yes: ', 
            for var in binding.keys():
                print var, '=', binding[var], ' ',
            print
        else:
            print 'yes: ' + external(substitute(query, binding))
    
    if pmode[1] == 'all':
        return 1
    else:
        return input_yes(kbase, 'more solutions? ')
Exemple #19
0
def report(binding, query, pmode):
    if not pmode:
        pmode = print_mode  # use current global mode
    else:
        pmode = pmode[0]

    if binding == {}:
        print "yes: (no variables)"
    else:
        if pmode[0] == "vars":
            print "yes: ",
            for var in binding.keys():
                print var, "=", binding[var], " ",
            print
        else:
            print "yes: " + external(substitute(query, binding))

    if pmode[1] == "all":
        return 1
    else:
        return input_yes("more solutions? ")
Exemple #20
0
        elif ans in ['n','N','no','NO']:
            break
        elif ans == 'where':
            print_solns(known, None)
        elif ans == 'browse':
            kbase.browse_pattern(raw_input('enter browse pattern: '))
        else:
            print 'what?  (expecting "y", "n", "where", or "browse")'





def trace_tree((fact, cf, proof), level):
    print ' ' * level,
    print '"' + external([fact]) + '"', 'cf:'+cf, 
    if proof == 'told':
        print 'was your answer'
    elif proof == 'initial':
        print 'was on your initial facts list'
    elif proof == 'atomic':
        print 'is an absolute truth'
    elif proof == 'not':
        print 'was a negative answer, or was ommitted'
    else:
        rule, subproof = proof
        print 'was deduced by firing rule', rule
        for branch in subproof:
            trace_tree(branch, level+3)

Exemple #21
0
            for (fact, proof) in known.members():
                if match(patt, fact, {}, {})[0]:
                    trace_tree((fact, proof), 0)

        elif ans in ['n', 'N', 'no', 'NO']:
            break
        elif ans == 'where':
            print_solns(known, None)
        elif ans == 'browse':
            kbase.browse_pattern(raw_input('enter browse pattern: '))
        else:
            print 'what?  (expecting "y", "n", "where", or "browse")'


def trace_tree((fact, proof), level):
    print ' ' * level,
    print '"' + external([fact]) + '"',
    if proof == 'told':
        print 'was your answer'
    elif proof == 'initial':
        print 'was on your initial facts list'
    elif proof == 'atomic':
        print 'is an absolute truth'
    elif proof == 'not':
        print 'was a negative answer, or was ommitted'
    else:
        rule, subproof = proof
        print 'was deduced by firing rule', rule
        for branch in subproof:
            trace_tree(branch, level + 3)
        elif ans in ['n','N','no','NO']:
            break
        elif ans == 'where':
            print_solns(known, None)
        elif ans == 'browse':
            kbase.browse_pattern(raw_input('enter browse pattern: '))
        else:
            print 'what?  (expecting "y", "n", "where", or "browse")'





def trace_tree((fact, cf, proof), level):
    print ' ' * level,
    print '"' + external([fact]) + '"', 'cf:'+cf, 
    if proof == 'told':
        print 'was your answer'
    elif proof == 'initial':
        print 'was on your initial facts list'
    elif proof == 'atomic':
        print 'is an absolute truth'
    elif proof == 'not':
        print 'was a negative answer, or was ommitted'
    else:
        rule, subproof = proof
        print 'was deduced by firing rule', rule
        for branch in subproof:
            trace_tree(branch, level+3)

Exemple #23
0
        elif ans in ['n','N','no','NO']:
            break
        elif ans == 'where':
            print_solns(known, None)
        elif ans == 'browse':
            kbase.browse_pattern(raw_input('enter browse pattern: '))
        else:
            print 'what?  (expecting "y", "n", "where", or "browse")'





def trace_tree((fact, proof), level):
    print ' ' * level,
    print '"' + external([fact]) + '"', 
    if proof == 'told':
        print 'was your answer'
    elif proof == 'initial':
        print 'was on your initial facts list'
    elif proof == 'atomic':
        print 'is an absolute truth'
    elif proof == 'not':
        print 'was a negative answer, or was ommitted'
    else:
        rule, subproof = proof
        print 'was deduced by firing rule', rule
        for branch in subproof:
            trace_tree(branch, level+3)