示例#1
0
文件: CNF.py 项目: mmccarty/nell
def goSequent() :
    """goSequent helps a user interactively type a sequent to be proved,
       formats it in cnf (where the goal prop is negated),
       and writes it as a string to a textfile.

       The string has the format,   D1 D2 ... Dn,
       where     D ::=  [F1, F2, ... Fm]
                 F ::=  "p"  |  "-p"
                         where  p  is a string of letters
       Example: the input,    p->r, q->r |- (p | q) -> r,
       is mapped to the cnf form,
               [['-p', 'r'], ['-q', 'r'], ['p', 'q'], ['-r']]
       and the string,
               [-p,r][-q,r][p,q][-r]
       is written to the output file that the user requests.
    """
    import Parse
    premises = True
    answer = []
    while premises:
        text = raw_input("Type premise (or |-): ").strip()
        if text == "|-" :
            premises = False
        else :
            prop = cnf( Parse.parse(Parse.scan(text)) )
            answer = answer + prop
    text = raw_input("Type goal prop: ")
    not_text = "-(" + text + ")"
    not_goal =  cnf( Parse.parse(Parse.scan(not_text)) )
    answer = answer + not_goal

    print "clauses are:", answer
    print
    filename = raw_input("Type name of destination file: ")
    output = open(filename, "w")
    textline = ""
    for clause in answer :
        textline = textline + "["
        items = ""
        for literal in clause :
            items = items + "," + literal
        if items != "" :
            items = items[1:]   # forget leading comma
        textline = textline + items + "]"
    print "wrote to", filename + ":", textline
    output.write(textline)
    output.close
示例#2
0
文件: CNF.py 项目: mmccarty/nell
       if c == NOT :
           opposite = prim[1:]
       else :
           opposite = NOT + prim
       if opposite in d :
           return True
    return False


if __name__ == '__main__':
#def main():
    """main lets you test the algorithm with interactive input"""
    import Parse
    text = raw_input("Type a proposition: ")
    print
    prop0 = Parse.parse(Parse.scan(text))
    print "parse tree: "
    print prop0
    print
    prop1 = removeImplications(prop0)
    print "-> removed:"
    print prop1
    print
    prop2 = moveNegations(prop1)
    print "- shifted inwards:"
    print prop2
    print
    prop3 = makeIntoCNF(prop2)
    print "cnf:"
    print  prop3
    print