Ejemplo n.º 1
0
def parseClause(lexer):
    """
    Parse a clause. A clause in (slightly simplified) TPTP-3 syntax is
    written as
       cnf(<name>, <type>, <literal list>).
    where <name> is a lower-case ident, type is a lower-case ident
    from a specific list, and <literal list> is a "|" separated list
    of literals, optionally enclosed in parenthesis.

    For us, all clause types are essentially the same, so we only
    distinguish "axiom", "negated_conjecture", and map everything else
    to "plain".
    """
    lexer.AcceptLit("cnf")
    lexer.AcceptTok(Token.OpenPar)
    name = lexer.LookLit()
    lexer.AcceptTok(Token.IdentLower)
    lexer.AcceptTok(Token.Comma)
    type = lexer.LookLit()
    if not type in ["axiom", "negated_conjecture"]:
        type = "plain"
    lexer.AcceptTok(Token.IdentLower)
    lexer.AcceptTok(Token.Comma)
    if lexer.TestTok(Token.OpenPar):
        lexer.AcceptTok(Token.OpenPar)
        lits = parseLiteralList(lexer)
        lexer.AcceptTok(Token.ClosePar)
    else:
        lits = parseLiteralList(lexer)
    lexer.AcceptTok(Token.ClosePar)
    lexer.AcceptTok(Token.FullStop)

    res = Clause(lits, type, name)
    res.setDerivation(Derivation("input"))
    return res
Ejemplo n.º 2
0
def parseWFormula(lexer):
    """
    Parse a formula in (slightly simplified) TPTP-3 syntax. It is
    written
       fof(<name>, <type>, <lformula>).
    where <name> is a lower-case ident, type is a lower-case ident
    from a specific list, and <lformula> is a Formula.

    For us, all clause types are essentially the same, so we only
    distinguish "axiom", "conjecture", and "negated_conjecture", and
    map everything else to "plain".
    """
    lexer.AcceptLit("fof")
    lexer.AcceptTok(Token.OpenPar)
    name = lexer.LookLit()
    lexer.AcceptTok([Token.IdentLower, Token.SQString])
    lexer.AcceptTok(Token.Comma)
    type = lexer.LookLit()
    if not type in ["axiom", "conjecture", "negated_conjecture"]:
        type = "plain"
    lexer.AcceptTok(Token.IdentLower)
    lexer.AcceptTok(Token.Comma)

    form = parseFormula(lexer)

    lexer.AcceptTok(Token.ClosePar)
    lexer.AcceptTok(Token.FullStop)

    res = WFormula(form, type, name)
    res.setDerivation(Derivation("input"))

    return res
Ejemplo n.º 3
0
def wFormulaCNF(wf):
    """
    Convert a (wrapped) formula to Conjunctive Normal Form.
    """
    f, m0 = formulaOpSimplify(wf.formula)
    f, m1 = formulaSimplify(f)
    if m0 or m1:
        tmp = WFormula(f, wf.type)
        tmp.setDerivation(flatDerivation("fof_simplification", [wf]))
        wf = tmp

    f, m = formulaNNF(f, 1)
    if m:
        tmp = WFormula(f, wf.type)
        tmp.setDerivation(flatDerivation("fof_nnf", [wf]))
        wf = tmp

    f, m = formulaMiniScope(f)
    if m:
        tmp = WFormula(f, wf.type)
        tmp.setDerivation(flatDerivation("shift_quantors", [wf]))
        wf = tmp

    f = formulaVarRename(f)
    if not f.isEqual(wf.formula):
        tmp = WFormula(f, wf.type)
        tmp.setDerivation(flatDerivation("variable_rename", [wf]))
        wf = tmp

    f = formulaSkolemize(f)
    if not f.isEqual(wf.formula):
        tmp = WFormula(f, wf.type)
        tmp.setDerivation(flatDerivation("skolemize", [wf], "status(esa)"))
        wf = tmp

    f = formulaShiftQuantorsOut(f)
    if not f.isEqual(wf.formula):
        tmp = WFormula(f, wf.type)
        tmp.setDerivation(Derivation("shift_quantors", [wf]))
        wf = tmp

    f = formulaDistributeDisjunctions(f)
    if not f.isEqual(wf.formula):
        tmp = WFormula(f, wf.type)
        tmp.setDerivation(flatDerivation("distribute", [wf]))
        wf = tmp

    return wf
Ejemplo n.º 4
0
def generateEquivAxioms():
    """
    Return a list with the three axioms describing an equivalence
    relation. We are lazy here...
    """
    lex = Lexer("""
    cnf(reflexivity, axiom, X=X).
    cnf(symmetry, axiom, X!=Y|Y=X).
    cnf(transitivity, axiom, X!=Y|Y!=Z|X=Z).
    """)
    res = []
    while not lex.TestTok(Token.EOFToken):
        c = parseClause(lex)
        c.setDerivation(Derivation("eq_axiom"))
        res.append(c)
    return res
Ejemplo n.º 5
0
def generateFunCompatAx(f, arity):
    """
    Generate axioms for the form
    X1!=Y1|...|Xn!=Yn|f(X1,...,Xn)=f(Y1,...Yn)    
    for f with the given arity.
    """
    res = generateEqPremise(arity)
    lterm = list([f])
    lterm.extend(generateVarList("X", arity))
    rterm = list([f])
    rterm.extend(generateVarList("Y", arity))
    concl = Literal(["=", lterm, rterm], False)
    res.append(concl)

    resclause = Clause(res)
    resclause.setDerivation(Derivation("eq_axiom"))
    return resclause
Ejemplo n.º 6
0
def generatePredCompatAx(p, arity):
    """
    Generate axioms for the form
    X1!=Y1|...|Xn!=Yn|~p(X1,...,Xn)|p(Y1,...Yn)    
    for f with the given arity.
    """
    res = generateEqPremise(arity)

    negp = list([p])
    negp.extend(generateVarList("X", arity))
    res.append(Literal(negp, True))

    posp = list([p])
    posp.extend(generateVarList("Y", arity))
    res.append(Literal(posp, False))

    resclause = Clause(res)
    resclause.setDerivation(Derivation("eq_axiom"))
    return resclause