Пример #1
0
def eq_assoccomm(u, v):
    """ Associative/Commutative eq

    Works like logic.core.eq but supports associative/commutative expr trees

    tree-format:  (op, *args)
    example:      (add, 1, 2, 3)

    State that operations are associative or commutative with relations

    >>> from logpy.assoccomm import eq_assoccomm as eq
    >>> from logpy.assoccomm import commutative, associative
    >>> from logpy import fact, run, var

    >>> fact(commutative, 'add')    # declare that 'add' is commutative
    >>> fact(associative, 'add')    # declare that 'add' is associative

    >>> x = var()
    >>> e1 = ('add', 1, 2, 3)
    >>> e2 = ('add', 1, x)
    >>> run(0, x, eq(e1, e2))
    (('add', 2, 3), ('add', 3, 2))
    """
    w = var()
    return lall((eq_comm, u, w, eq_assoccomm),
                (eq_assoc, w, v, eq_assoccomm))
Пример #2
0
def eq_assoccomm(u, v):
    """ Associative/Commutative eq

    Works like logic.core.eq but supports associative/commutative expr trees

    tree-format:  (op, *args)
    example:      (add, 1, 2, 3)

    State that operations are associative or commutative with relations

    >>> from logpy.assoccomm import eq_assoccomm as eq
    >>> from logpy.assoccomm import commutative, associative
    >>> from logpy import fact, run, var

    >>> fact(commutative, 'add')    # declare that 'add' is commutative
    >>> fact(associative, 'add')    # declare that 'add' is associative

    >>> x = var()
    >>> e1 = ('add', 1, 2, 3)
    >>> e2 = ('add', 1, x)
    >>> run(0, x, eq(e1, e2))
    (('add', 2, 3), ('add', 3, 2))
    """
    for typ, fn in seq_registry:
        if isinstance(u, typ):
            u = fn(u)
        if isinstance(v, typ):
            v = fn(v)
    if isinstance(u, tuple) and not isinstance(v, tuple) and not isvar(v):
        return fail
    if isinstance(v, tuple) and not isinstance(u, tuple) and not isvar(u):
        return fail
    if isinstance(u, tuple) and isinstance(v, tuple) and not u[0] == v[0]:
        return fail
    if isinstance(u, tuple) and not (u[0],) in associative.facts:
        return (eq, u, v)
    if isinstance(v, tuple) and not (v[0],) in associative.facts:
        return (eq, u, v)

    if isinstance(u, tuple) and isinstance(v, tuple):
        u, v = (u, v) if len(u) >= len(v) else (v, u)
        n = len(v)-1  # length of shorter tail
    else:
        n = None
    if isinstance(v, tuple) and not isinstance(u, tuple):
        u, v = v, u
    w = var()
    return lall((eq_assoc, u, w, eq_assoccomm, n),
                (eq_comm, v, w, eq_assoccomm))
Пример #3
0
def buildo(op, args, obj):
    """ obj is composed of op on args

    Example: in add(1,2,3) ``add`` is the op and (1,2,3) are the args

    Checks op_regsitry for functions to define op/arg relationships
    """
    if not isvar(obj):
        oop, oargs = op_args(obj)
        return lall((eq, op, oop), (eq, args, oargs))
    else:
        try:
            return eq(obj, build(op, args))
        except TypeError:
            raise EarlyGoalError()
    raise EarlyGoalError()
Пример #4
0
def eq_assoccomm(u, v):
    """ Associative/Commutative eq

    Works like logic.core.eq but supports associative/commutative expr trees

    tree-format:  (op, *args)
    example:      (add, 1, 2, 3)

    State that operations are associative or commutative with relations

    >>> from logpy.assoccomm import eq_assoccomm as eq
    >>> from logpy.assoccomm import commutative, associative
    >>> from logpy import fact, run, var

    >>> fact(commutative, 'add')    # declare that 'add' is commutative
    >>> fact(associative, 'add')    # declare that 'add' is associative

    >>> x = var()
    >>> e1 = ('add', 1, 2, 3)
    >>> e2 = ('add', 1, x)
    >>> run(0, x, eq(e1, e2))
    (('add', 2, 3), ('add', 3, 2))
    """
    if isinstance(u, tuple) and not isinstance(v, tuple) and not isvar(v):
        return fail
    if isinstance(v, tuple) and not isinstance(u, tuple) and not isvar(u):
        return fail
    if isinstance(u, tuple) and isinstance(v, tuple) and not u[0] == v[0]:
        return fail
    if isinstance(u, tuple) and not (u[0],) in associative.facts:
        return (eq, u, v)
    if isinstance(v, tuple) and not (v[0],) in associative.facts:
        return (eq, u, v)

    if isinstance(u, tuple) and isinstance(v, tuple):
        u, v = (u, v) if len(u) >= len(v) else (v, u)
        n = len(v)-1  # length of shorter tail
    else:
        n = None
    if isinstance(v, tuple) and not isinstance(u, tuple):
        u, v = v, u
    w = var()
    return lall((eq_assoc, u, w, eq_assoccomm, n),
                (eq_comm, v, w, eq_assoccomm))
Пример #5
0
# Define the rules
rules = lall(
    # There are 4 people
    (eq, (var(), var(), var(), var()), people),

    # Steve's car is blue
    (membero, ('Steve', var(), 'blue', var()), people),

    # Person who owns the cat lives in Canada
    (membero, (var(), 'cat', var(), 'Canada'), people),

    # Matthew lives in USA
    (membero, ('Matthew', var(), var(), 'USA'), people),

    # The person who has a black car lives in Australia
    (membero, (var(), var(), 'black', 'Australia'), people),

    # Jack has a cat
    (membero, ('Jack', 'cat', var(), var()), people),

    # Alfred lives in Australia
    (membero, ('Alfred', var(), var(), 'Australia'), people),

    # Person who owns the dog lives in France
    (membero, (var(), 'dog', var(), 'France'), people),

    # Who is the owner of the rabbit?
    (membero, (var(), 'rabbit', var(), var()), people)
)

# Run the solver
Пример #6
0
def test_lall():
    x = var("x")
    assert results(lall((eq, x, 2))) == ({x: 2},)
    assert results(lall((eq, x, 2), (eq, x, 3))) == ()
peopleRules = lall(
	# there are 4 people
	(eq,		(var(), var(), var(), var()), people),
	# Carlos wears blue
	(membero,	('Carlos', var(), 'blue'), people),
	# Loz drinks coffee
	(membero,	('Loz', 'coffee', var()), people),
	# Ted sits left of Bruce
	(lefto, 	('Ted', var(), var()),
				('Bruce', var(), var()), people),
	# A person who drinks tea sits next to Bruce
	(nexto,		(var(), 'tea', var()),
				('Bruce', var(), var()), people),
	# Carlos sits next to someone wearing red
	(nexto,		(var(), var(), 'red'),
				('Carlos', var(), var()), people),
	# Loz is last
	(eq, 		(var(), var(), var(), ('Loz', var(), var())), people),
	# A tea-drinker is first
	(eq, 		((var(), 'tea', var()), var(), var(), var()), people),
	# A coffee-drinker is last
	(eq, 		(var(), var(), var(), (var(), 'coffee', var())), people),
	# The black-shirt-wearer drinks tea
	(membero,	(var(), 'tea', 'black'), people),
	# The red-shirt-wearer is second
	(eq, 		(var(), (var(), var(), 'red'), var(), var()), people),
	# Carlos is left of the yellow-shirt-wearer
	(lefto,		('Carlos', var(), var()),
				(var(), var(), 'yellow'), people),
	# a coffee drinker wears red
	(membero,	(var(), 'coffee', 'red'), people),
	# blue drinks tea
	(membero,	(var(), 'tea', 'blue'), people)
)
Пример #8
0
            Account('Carl', 'Marx', 2, 3),
            Account('John', 'Rockefeller', 3, 1000))

# variables are arbitrary Python objects, not LogPy Var objects
first = 'FIRST'
last = 'LAST'
ident = -1111
balance = -2222
newbalance = -3333
vars = {first, last, ident, balance, newbalance}


# Describe a couple of transformations on accounts
source = Account(first, last, ident, balance)
target = Account(first, last, ident, newbalance)

theorists = ('Adam', 'Carl')
# Give $10 to theorists
theorist_bonus = lall((membero, source, accounts),
                      (membero, first, theorists),
                      (add, 10, balance, newbalance))

# Take $10 from anyone with more than $100
tax_the_rich = lall((membero, source, accounts),
                    (gt, balance, 100),
                    (sub, balance, 10, newbalance))

with variables(*vars):
    print run(0, target, tax_the_rich)
    print run(0, target, theorist_bonus)
Пример #9
0
zebraRules = lall(
	# there are 5 houses
	(eq, 		(var(), var(), var(), var(), var()), houses),
	# the Englishman's house is red
	(membero,	('Englishman', var(), var(), var(), 'red'), houses),
	# the Swede has a dog
	(membero,	('Swede', var(), var(), 'dog', var()), houses),
	# the Dane drinks tea
	(membero,	('Dane', var(), 'tea', var(), var()), houses),
	# the Green house is left of the White house
	(lefto,		(var(), var(), var(), var(), 'green'),
				(var(), var(), var(), var(), 'white'), houses),
	# coffee is the drink of the green house
	(membero,	(var(), var(), 'coffee', var(), 'green'), houses),
	# the Pall Mall smoker has birds
	(membero,	(var(), 'Pall Mall', var(), 'birds', var()), houses),
	# the yellow house smokes Dunhills
	(membero,	(var(), 'Dunhill', var(), var(), 'yellow'), houses),
	# the middle house drinks milk
	(eq,		(var(), var(), (var(), var(), 'milk', var(), var()), var(), var()), houses),
	# the Norwegian is the first house
	(eq,		(('Norwegian', var(), var(), var(), var()), var(), var(), var(), var()), houses),
	# the Blend smoker is in the house next to the house with cats
	(nexto,		(var(), 'Blend', var(), var(), var()),
				(var(), var(), var(), 'cats', var()), houses),
	# the Dunhill smoker is next to the house where they have a horse
	(nexto,		(var(), 'Dunhill', var(), var(), var()),
				(var(), var(), var(), 'horse', var()), houses),
	# the Blue Master smoker drinks beer
	(membero,	(var(), 'Blue Master', 'beer', var(), var()), houses),
	# the German smokes Prince
	(membero,	('German', 'Prince', var(), var(), var()), houses),
	# the Norwegian is next to the blue house
	(nexto,		('Norwegian', var(), var(), var(), var()),
				(var(), var(), var(), var(), 'blue'), houses),
	# the house next to the Blend smoker drinks water
	(nexto,		(var(), 'Blend', var(), var(), var()),
				(var(), var(), 'water', var(), var()), houses),
	# one of the houses has a zebra--but whose?
	(membero,	(var(), var(), var(), 'zebra', var()), houses)
)
Пример #10
0
zebraRules = lall(
    # there are 5 houses
    (eq, (var(), var(), var(), var(), var()), houses),
    # the Englishman's house is red
    (membero, ('Englishman', var(), var(), var(), 'red'), houses),
    # the Swede has a dog
    (membero, ('Swede', var(), var(), 'dog', var()), houses),
    # the Dane drinks tea
    (membero, ('Dane', var(), 'tea', var(), var()), houses),
    # the Green house is left of the White house
    (lefto, (var(), var(), var(), var(), 'green'),
     (var(), var(), var(), var(), 'white'), houses),
    # coffee is the drink of the green house
    (membero, (var(), var(), 'coffee', var(), 'green'), houses),
    # the Pall Mall smoker has birds
    (membero, (var(), 'Pall Mall', var(), 'birds', var()), houses),
    # the yellow house smokes Dunhills
    (membero, (var(), 'Dunhill', var(), var(), 'yellow'), houses),
    # the middle house drinks milk
    (eq, (var(), var(),
          (var(), var(), 'milk', var(), var()), var(), var()), houses),
    # the Norwegian is the first house
    (eq, (('Norwegian', var(), var(), var(), var()), var(), var(), var(),
          var()), houses),
    # the Blend smoker is in the house next to the house with cats
    (nexto, (var(), 'Blend', var(), var(), var()),
     (var(), var(), var(), 'cats', var()), houses),
    # the Dunhill smoker is next to the house where they have a horse
    (nexto, (var(), 'Dunhill', var(), var(), var()),
     (var(), var(), var(), 'horse', var()), houses),
    # the Blue Master smoker drinks beer
    (membero, (var(), 'Blue Master', 'beer', var(), var()), houses),
    # the German smokes Prince
    (membero, ('German', 'Prince', var(), var(), var()), houses),
    # the Norwegian is next to the blue house
    (nexto, ('Norwegian', var(), var(), var(), var()),
     (var(), var(), var(), var(), 'blue'), houses),
    # the house next to the Blend smoker drinks water
    (nexto, (var(), 'Blend', var(), var(), var()),
     (var(), var(), 'water', var(), var()), houses),
    # one of the houses has a zebra--but whose?
    (membero, (var(), var(), var(), 'zebra', var()), houses))
Пример #11
0
def test_lall():
    x = var('x')
    assert results(lall((eq, x, 2))) == ({x: 2}, )
    assert results(lall((eq, x, 2), (eq, x, 3))) == ()
Пример #12
0
rules = lall(
    # There are 5 people
    (eq, (var(), var(), var(), var(), var()), people),

    # Англичанин живет в красном доме
    (membero, ('Englishman', 'red', var(), var(), var()), people),

    # У испанца есть собкака
    (membero, ('Spain', var(), var(), 'dog', var()), people),

    # В зеленом доме пьют кофе
    (membero, (var(), 'green', var(), var(), 'coffee'), people),

    # Украинец пьет чай
    (membero, ('Ukraine', var(), var(), var(), 'tea'), people),

    # Тот, кто курит Old Gold, разводит улиток.
    (membero, (var(), var(), 'Old Gold', 'snails', var()), people),

    # В жёлтом доме курят Kool.
    (membero, (var(), 'yelow', 'Kool', var(), var()), people),

    # В центральном доме пьют молоко.
    (membero, (var(), var(), var(), var(), 'milk'), people),

    # Норвежец живет в первом доме
    (membero, ('norway', var(), var(), var(), var()), people),

    # В доме по соседству с тем, в котором держат лошадь, курят Kool.
    (membero, (var(), var(), 'Kool', var(), var()), people),

    # Японец курит Parliament.
    (membero, ('Otaku', var(), 'Parliament', var(), var()), people),

    # Сосед того, кто курит Rothmans, пьет воду
    (membero, (var(), var(), var(), var(), 'water'), people),

    # Тот, кто выращивает лошадей, живет в синем доме
    (membero, (var(), 'blue', var(), 'horse', var()), people),

    # Тот, кто курит Philip Morris, пьет пиво.
    (membero, (var(), var(), ' Philip Morris', var(), 'beer'), people),

    # Кто держит зебру??
    (membero, (var(), var(), var(), 'zebra', var()), people))
Пример #13
0
rules = lall(
    (logpy.eq, (logpy.var(), logpy.var(), logpy.var(), logpy.var(),
                logpy.var()), people),
    (logpy.membero,
     ('Англичанин', logpy.var(), logpy.var(), logpy.var(), 'красный'), people),
    (logpy.membero,
     ('Швед', logpy.var(), logpy.var(), 'собака', logpy.var()), people),
    (logpy.membero,
     ('Датчанин', logpy.var(), 'чай', logpy.var(), logpy.var()), people),
    (logpy.membero,
     (logpy.var(), logpy.var(), 'кофе', logpy.var(), 'зеленый'), people),
    (logpy.membero,
     (logpy.var(), 'Pall Mall', logpy.var(), 'птицы', logpy.var()), people),
    (logpy.membero,
     (logpy.var(), 'Dunhill', logpy.var(), logpy.var(), 'желтый'), people),
    (logpy.membero,
     (logpy.var(), 'Rothmans', logpy.var(), logpy.var(), logpy.var()), people),
    (logpy.membero,
     (logpy.var(), logpy.var(), 'молоко', logpy.var(), logpy.var()), people),
    (logpy.membero,
     (logpy.var(), 'Philip Morris', 'пиво', logpy.var(), logpy.var()), people),
    (logpy.membero,
     (logpy.var(), logpy.var(), 'вода', logpy.var(), logpy.var()), people),
    (logpy.membero,
     (logpy.var(), logpy.var(), logpy.var(), 'лошади', 'синий'), people),
    (logpy.membero,
     (logpy.var(), logpy.var(), logpy.var(), 'кошки', logpy.var()), people),
    (logpy.membero,
     ('Немец', 'Marlboro', logpy.var(), logpy.var(), logpy.var()), people),
    (logpy.membero,
     (logpy.var(), logpy.var(), logpy.var(), 'рыбки', logpy.var()), people))
Пример #14
0
unifiable(Account)  # Register Account class

accounts = (Account('Adam', 'Smith', 1,
                    20), Account('Carl', 'Marx', 2,
                                 3), Account('John', 'Rockefeller', 3, 1000))

# variables are arbitrary Python objects, not LogPy Var objects
first = 'FIRST'
last = 'LAST'
ident = -1111
balance = -2222
newbalance = -3333
vars = {first, last, ident, balance, newbalance}

# Describe a couple of transformations on accounts
source = Account(first, last, ident, balance)
target = Account(first, last, ident, newbalance)

theorists = ('Adam', 'Carl')
# Give $10 to theorists
theorist_bonus = lall((membero, source, accounts), (membero, first, theorists),
                      (add, 10, balance, newbalance))

# Take $10 from anyone with more than $100
tax_the_rich = lall((membero, source, accounts), (gt, balance, 100),
                    (sub, balance, 10, newbalance))

with variables(*vars):
    print run(0, target, tax_the_rich)
    print run(0, target, theorist_bonus)
Пример #15
0
rules = lall(
    # There are 5 people
    (eq, (var(), var(), var(), var(), var()), people),

    # Норвежец живет в первом доме
    (membero, ('норвежец', var(), var(), var(), var()), people),

    # Англичанин живет в красном доме
    (membero, ('англичанин', 'красный', var(), var(), var()), people),
    # Зеленый дом находится левее белого!?!?
    # Датчанин пьет чай
    (membero, ('датчанин', var(), var(), var(), 'чай'), people),

    # Tот, кто курит Rothmans, живет рядом с тем, кто
    # выращивает кошек
    (membero, (var(), var(), 'Rothmans', var(), var()), people),

    # Тот, кто живет в желтом доме, курит Dunhill
    (membero, (var(), 'желтый', 'Dunhill', 'var()', var()), people),

    # Немец курит Marlboro
    (membero, ('Немец', var(), 'Marlboro', var(), var()), people),

    # Тот, кто живет в центре, пьет молоко
    (membero, (var(), var(), var(), var(), 'молоко'), people),

    # Сосед того, кто курит Rothmans, пьет воду
    (membero, (var(), var(), var(), var(), 'воду'), people),

    # Тот, кто курит Pall Mall, выращивает птиц
    (membero, (var(), var(), 'Pall Mall', 'птиц', var()), people),

    # Швед выращивает собак
    (membero, ('швед', var(), var(), 'собака', var()), people),

    # Норвежец живет рядом с синим домом

    # Тот, кто выращивает лошадей, живет в синем доме
    (membero, (var(), 'синий', var(), 'лошадь', var()), people),

    # Тот, кто курит Philip Morris, пьет пиво.
    (membero, (var(), var(), ' Philip Morris', var(), 'пиво'), people),

    # В зеленом доме пьют кофе
    (membero, (var(), 'зеленый', var(), var(), 'кофе'), people),

    # а кто ж выращивает рыб?
    (membero, (var(), var(), var(), 'рыбки', var()), people))