Пример #1
0
def isFirstOrderFormula(expr):
    """Check whether this is a "Formula".

    Return false if it's not a Formula *or* it's not first-order.

    or raise exception if not a formula...???
    """
    if expr.isAtomic():
        if isinstance(expr, Proposition):
            return 1
        else:
            return 0
    else:
        if isinstance(expr.function, Predicate):
            for arg in expr.args:
                if not isFirstOrderTerm(arg): return 0
            return 1
        else:
            if isinstance(expr.function, ExistentialQuantifier):
                assert(isinstance(expr.args[0], ExiVar))
                return isFirstOrderFormula(expr.args[1])
            elif isinstance(expr.function, UniversalQuantifier):
                assert(isinstance(expr.args[0], UniVar))
                return isFirstOrderFormula(expr.args[1])
            elif isinstance(expr.function, Connective):
                for arg in expr.args:
                    if not isFirstOrderFormula(arg): return 0
                return 1
            else:
                return 0
Пример #2
0
def gatherURIs(expr, set):
    if expr.isAtomic():
        try:
            set[expr.uri] = 1
        except AttributeError:
            pass
    else:
        for term in expr.all:
            gatherURIs(term, set)
Пример #3
0
def gatherURIs(expr, set):
    if expr.isAtomic():
        try:
            set[expr.uri] = 1
        except AttributeError:
            pass
    else:
        for term in expr.all:
            gatherURIs(term, set)
Пример #4
0
def isFirstOrderFormula(expr):
    """Check whether this is a "Formula".

    Return false if it's not a Formula *or* it's not first-order.
    """
    if expr.isAtomic():
        if isinstance(expr, Proposition):
            return 1
        else:
            return 0
    else:
        if isinstance(expr.function, Predicate):
            for arg in expr.args:
                if not isFirstOrderTerm(arg): return 0
        else:
            return 0
Пример #5
0
def isFirstOrderFormula(expr):
    """Check whether this is a "Formula".

    Return false if it's not a Formula *or* it's not first-order.
    """
    if expr.isAtomic():
        if isinstance(expr, Proposition):
            return 1
        else:
            return 0
    else:
        if isinstance(expr.function, Predicate):
            for arg in expr.args:
                if not isFirstOrderTerm(arg): return 0
        else:
            return 0
Пример #6
0
def isFirstOrderTerm(expr):
    """Check whether this is a "Term".

    >>> import LX.fol   
    >>> a = LX.fol.Constant("a")
    >>> LX.fol.isFirstOrderTerm(a)
    1
    >>> LX.fol.isFirstOrderFormula(a)
    0


    >>> b = LX.fol.Constant("b")
    >>> f = LX.fol.Function("f")
    >>> fab = LX.expr.CompoundExpr(f,a,b)
    >>> print fab
    f(a, b)
    >>> LX.fol.isFirstOrderTerm(fab)
    1
    >>> LX.fol.isFirstOrderFormula(fab)
    0

    >>> p = LX.fol.Proposition("p")
    >>> LX.fol.isFirstOrderTerm(p)
    0
    >>> LX.fol.isFirstOrderFormula(p)
    1

    >>> q = LX.fol.Proposition("q")
    >>> p_or_q = p | q
    >>> print p_or_q
    or(p, q)
    >>> LX.fol.isFirstOrderTerm(p_or_q)
    0
    >>> LX.fol.isFirstOrderFormula(p_or_q)
    1

    >>> h = LX.fol.Predicate("h")
    >>> LX.fol.isFirstOrderTerm(h)
    0
    >>> LX.fol.isFirstOrderFormula(h)
    0
    >>> hfab = h(fab)
    >>> print hfab
    h(f(a, b))
    >>> LX.fol.isFirstOrderTerm(hfab)
    0
    >>> LX.fol.isFirstOrderFormula(hfab)
    1
    
    Return false if it's not a Term *or* it's not first-order.
    """
    if expr.isAtomic():
        if isinstance(expr, Constant) or isinstance(expr, Variable):
            return 1
        else:
            return 0
    else:
        if isinstance(expr.function, Function):
            for arg in expr.args:
                if not isFirstOrderTerm(arg): return 0
            return 1
        else:
            return 0
Пример #7
0
def getOpenVariables(expr, unusedButQuantified=None):
    """Return A list of all the un-quantified variables and optionally
    also a list of those quantified but not used.

    >>> from LX.logic import *
    >>> a=Constant("a")
    >>> b=Constant("b")
    >>> f=Function("f")
    >>> fab=f(a,b)
    >>> print fab
    f(a, b)
    >>> x=Variable("?x")
    >>> fx=f(x)
    >>> fax=f(a,x)
    >>> print fx
    f(?x)
    >>> print fax
    f(a, ?x)
    >>> print getOpenVariables(fab)
    []
    >>> print getOpenVariables(fx)[0]
    ?x
    >>> print getOpenVariables(fax)[0]
    ?x

    >>> s1 = FORALL(x, fax)
    >>> print s1
    forall(?x, f(a, ?x))
    >>> print getOpenVariables(s1)
    []

    >>> y=Variable("?y")
    >>> s2 = FORALL(y, fax)
    >>> print s2
    forall(?y, f(a, ?x))
    >>> print getOpenVariables(s2)[0]
    ?x
    >>> unused=[]
    >>> print getOpenVariables(s2, unused)[0]
    ?x
    >>> print unused[0]
    ?y
    
    """

    if expr.isAtomic():
        if isinstance(expr, Constant):
            return []
        elif isinstance(expr, Function):
            return []
        elif isinstance(expr, Proposition):
            return []
        elif isinstance(expr, Predicate):
            return []
        elif isinstance(expr, Variable):
            return [expr]
        else:
            raise RuntimeError, "unknown atomic term: %s" % expr
    else:
        if isinstance(expr.function, Quantifier):
            assert(len(expr.args) == 2)
            result = getOpenVariables(expr.args[1])
            try:
                result.remove(expr.args[0])
            except ValueError:
                if unusedButQuantified is not None:
                    unusedButQuantified.append(expr.args[0])
                else:
                    pass    # the caller doesn't want to know
            return result
        else:
            result = []
            for child in expr.all:
                result.extend(getOpenVariables(child))
            return result
Пример #8
0
def getOpenVariables(expr, unusedButQuantified=None):
    """Return A list of all the un-quantified variables and optionally
    also a list of those quantified but not used.

    >>> from LX.logic import *
    >>> a=Constant("a")
    >>> b=Constant("b")
    >>> f=Function("f")
    >>> fab=f(a,b)
    >>> print fab
    f(a, b)
    >>> x=Variable("?x")
    >>> fx=f(x)
    >>> fax=f(a,x)
    >>> print fx
    f(?x)
    >>> print fax
    f(a, ?x)
    >>> print getOpenVariables(fab)
    []
    >>> print getOpenVariables(fx)[0]
    ?x
    >>> print getOpenVariables(fax)[0]
    ?x

    >>> s1 = FORALL(x, fax)
    >>> print s1
    forall(?x, f(a, ?x))
    >>> print getOpenVariables(s1)
    []

    >>> y=Variable("?y")
    >>> s2 = FORALL(y, fax)
    >>> print s2
    forall(?y, f(a, ?x))
    >>> print getOpenVariables(s2)[0]
    ?x
    >>> unused=[]
    >>> print getOpenVariables(s2, unused)[0]
    ?x
    >>> print unused[0]
    ?y
    
    """

    if expr.isAtomic():
        if isinstance(expr, Constant):
            return []
        elif isinstance(expr, Function):
            return []
        elif isinstance(expr, Proposition):
            return []
        elif isinstance(expr, Predicate):
            return []
        elif isinstance(expr, Variable):
            return [expr]
        else:
            raise RuntimeError, "unknown atomic term: %s" % expr
    else:
        if isinstance(expr.function, Quantifier):
            assert (len(expr.args) == 2)
            result = getOpenVariables(expr.args[1])
            try:
                result.remove(expr.args[0])
            except ValueError:
                if unusedButQuantified is not None:
                    unusedButQuantified.append(expr.args[0])
                else:
                    pass  # the caller doesn't want to know
            return result
        else:
            result = []
            for child in expr.all:
                result.extend(getOpenVariables(child))
            return result