Пример #1
0
def proveUniversal(C6, bfactlist, bgoal) :
    """ Tries to prove a bgoal of form,   ["forall", lo, i, hi, bprop_i_]
        from  bfactlist.   First, attempts to show  hi <= lo,  meaning
        quantification ranges over empty set.   If this fails, tries
        to establish numerical lower and upper bounds for the quantification
        and enumerate proofs for all elements within these bounds.
        If this fails, then searches
        for a  bfact in bfactlist that is a forall of the same form,
        but where its upper bound is  hi-1.   If success, then tries
        to prove  bprop_hi-1_.

        If I have the energy, I'll try to make this smarter later....
    """
    #print "proveUNIVERSAL: bfactlist=", bfactlist
    #print "goal=", bgoal
    lo = bgoal[1]
    hi = bgoal[3]
    i = bgoal[2]
    bprop = bgoal[4]

    # first, see if  bgoal in premises:
    success = bgoal in bfactlist

    if not success :
        # next, try to prove that domain is empty, ie, hi <= lo :
        success = verifyRelation(C6, bfactlist, ["<=", hi, lo])

    if not success:
        # next, try to establish numerical lower and upper bounds and
        # prove  bprop  for all elements in the numerical range:
        lonum = PE.evallToInt(C6, lo)
        hinum = PE.evallToInt(C6, hi)
        if isinstance(lonum, int)  and  isinstance(hinum, int):
            success = True  # so far, so good...
            for j in range(lonum, hinum):
                stree = Parse.substituteTree(["int", str(j)], i, bprop)
                success = success and proveSequent(C6, bfactlist, stree)

    if not success:
        # then search bfactlist for a forall goal whose body
        #  matches  bprop and whose bounds cover  bgoal's all but one:
        possibles = [ f for f in bfactlist
                      if f[0] == "forall" \
                         and Parse.substituteTree(i, f[2], f[4]) == bprop \
                         #and verifyRelation(C6, bfactlist, ["==", f[1], lo]) \
                         and verifyRelation(C6, bfactlist, ["<=", f[1], lo]) \
                         and verifyRelation(C6, bfactlist,  \
                                     ["==", ["+", f[3], ["int", "1"]], hi]) \
                    ]
        if len(possibles) > 0 :
            success = proveSequent(C6, bfactlist, 
                                   Parse.substituteTree \
                                       (["-", hi, ["int", "1"]], i, bprop) )

    if not success:
        #search bfactlist for a forall goal whose body
        #  matches  bprop and whose bounds cover  bgoal's:
        possibles = [ f for f in bfactlist
                      if f[0] == "forall" \
                         and Parse.substituteTree(i, f[2], f[4]) == bprop \
                         and verifyRelation(C6, bfactlist, ["<=", f[1], lo]) \
                         and verifyRelation(C6, bfactlist, [">=", f[3], hi]) \
                    ]
        success = (len(possibles) > 0)

    return success