logic.py 36,9 ko
Newer Older
MircoT's avatar
MircoT a validé
    list(map(expr,
             ['(American(x) & Weapon(y) & Sells(x, y, z) & Hostile(z)) ==> Criminal(x)',  # noqa
MircoT's avatar
MircoT a validé
              'Owns(Nono, M1)',
              'Missile(M1)',
              '(Missile(x) & Owns(Nono, x)) ==> Sells(West, x, Nono)',
              'Missile(x) ==> Weapon(x)',
              'Enemy(x, America) ==> Hostile(x)',
              'American(West)',
              'Enemy(Nono, America)'
              ]))
withal's avatar
withal a validé
def fol_bc_ask(KB, query):
    """A simple backward-chaining algorithm for first-order logic. [Fig. 9.6]
C.G.Vedant's avatar
C.G.Vedant a validé
    KB should be an instance of FolKB, and query an atomic sentence. """
withal's avatar
withal a validé
    return fol_bc_or(KB, query, {})

withal's avatar
withal a validé
def fol_bc_or(KB, goal, theta):
    for rule in KB.fetch_rules_for_goal(goal):
        lhs, rhs = parse_definite_clause(standardize_variables(rule))
        for theta1 in fol_bc_and(KB, lhs, unify(rhs, goal, theta)):
            yield theta1

withal's avatar
withal a validé
def fol_bc_and(KB, goals, theta):
    if theta is None:
        pass
    elif not goals:
        yield theta
withal's avatar
withal a validé
    else:
        first, rest = goals[0], goals[1:]
        for theta1 in fol_bc_or(KB, subst(theta, first), theta):
            for theta2 in fol_bc_and(KB, rest, theta1):
                yield theta2
# ______________________________________________________________________________

# Example application (not in the book).
# You can use the Expr class to do symbolic differentiation.  This used to be
# a part of AI; now it is considered a separate field, Symbolic Algebra.

def diff(y, x):
    """Return the symbolic derivative, dy/dx, as an Expr.
    However, you probably want to simplify the results with simp.
    >>> diff(x * x, x)
    ((x * 1) + (x * 1))
    """
MircoT's avatar
MircoT a validé
    if y == x:
        return ONE
    elif not y.args:
        return ZERO
    else:
        u, op, v = y.args[0], y.op, y.args[-1]
MircoT's avatar
MircoT a validé
        if op == '+':
            return diff(u, x) + diff(v, x)
        elif op == '-' and len(args) == 1:
            return -diff(u, x)
        elif op == '-':
            return diff(u, x) - diff(v, x)
        elif op == '*':
            return u * diff(v, x) + v * diff(u, x)
        elif op == '/':
            return (v*diff(u, x) - u*diff(v, x)) / (v * v)
        elif op == '**' and isnumber(x.op):
            return (v * u ** (v - 1) * diff(u, x))
MircoT's avatar
MircoT a validé
        elif op == '**':
            return (v * u ** (v - 1) * diff(u, x) +
                    u ** v * Expr('log')(u) * diff(v, x))
MircoT's avatar
MircoT a validé
        elif op == 'log':
            return diff(u, x) / u
        else:
            raise ValueError("Unknown op: {} in diff({}, {})".format(op, y, x))
Peter Norvig's avatar
Peter Norvig a validé
    "Simplify the expression x."
MircoT's avatar
MircoT a validé
    if not x.args:
        return x
MircoT's avatar
MircoT a validé
    args = list(map(simp, x.args))
    u, op, v = args[0], x.op, args[-1]
withal's avatar
withal a validé
    if op == '+':
MircoT's avatar
MircoT a validé
        if v == ZERO:
            return u
        if u == ZERO:
            return v
        if u == v:
            return TWO * u
        if u == -v or v == -u:
            return ZERO
withal's avatar
withal a validé
    elif op == '-' and len(args) == 1:
MircoT's avatar
MircoT a validé
        if u.op == '-' and len(u.args) == 1:
            return u.args[0]  # --y ==> y
withal's avatar
withal a validé
    elif op == '-':
MircoT's avatar
MircoT a validé
        if v == ZERO:
            return u
        if u == ZERO:
            return -v
        if u == v:
            return ZERO
        if u == -v or v == -u:
            return ZERO
withal's avatar
withal a validé
    elif op == '*':
MircoT's avatar
MircoT a validé
        if u == ZERO or v == ZERO:
            return ZERO
        if u == ONE:
            return v
        if v == ONE:
            return u
        if u == v:
            return u ** 2
withal's avatar
withal a validé
    elif op == '/':
MircoT's avatar
MircoT a validé
        if u == ZERO:
            return ZERO
        if v == ZERO:
            return Expr('Undefined')
        if u == v:
            return ONE
        if u == -v or v == -u:
            return ZERO
withal's avatar
withal a validé
    elif op == '**':
MircoT's avatar
MircoT a validé
        if u == ZERO:
            return ZERO
        if v == ZERO:
            return ONE
        if u == ONE:
            return ONE
        if v == ONE:
            return u
withal's avatar
withal a validé
    elif op == 'log':
MircoT's avatar
MircoT a validé
        if u == ONE:
            return ZERO
    else:
        raise ValueError("Unknown op: " + op)
    # If we fall through to here, we can not simplify further
def d(y, x):
    "Differentiate and then simplify."
withal's avatar
withal a validé
    return simp(diff(y, x))
# _________________________________________________________________________

# Utilities for doctest cases
# These functions print their arguments in a standard order
# to compensate for the random order in the standard representation

# ________________________________________________________________________
peter.norvig's avatar
peter.norvig a validé