Ejemplo n.º 1
0
from account import Account
from kanren import unifiable, run, var, eq, membero, variables
from kanren.core import lall
from kanren.arith import add, gt, sub

unifiable(Account)  # Register Account class

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

# optional name strings are helpful for debugging
first = var('first')
last = var('last')
ident = var('ident')
balance = var('balance')
newbalance = var('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),
Ejemplo n.º 2
0
from account import Account
from kanren import unifiable, run, var, eq, membero, variables
from kanren.core import lall
from kanren.arith import add, gt, sub

unifiable(Account)  # Register Account class

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

# optional name strings are helpful for debugging
first = var('first')
last = var('last')
ident = var('ident')
balance = var('balance')
newbalance = var('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))
Ejemplo n.º 3
0
from uuid import uuid4

from unification.core import isground

from kanren.constraints import ConstrainedState, neq, isinstanceo
from kanren.term import applyo
from loguru import logger
from kanren import eq, var, unifiable, membero, conde

from kanren.core import fail, run, lall, lany
from kanren.goals import heado, tailo, conso
from unification import reify, Var, isvar

from evalo.utils import ast_dump_if_possible, strip_ast, replace_ast_name_with_lvar

unifiable(ast.AST)
DEFAULT_REPLACE_VAR = "x"


def typeo(v, t):
    def typeo_goal(S: ConstrainedState):
        nonlocal v, t

        v_rf, t_rf = reify((v, t), S)
        isground_all = [
            isground(reified_value, S) for reified_value in [v_rf, t_rf]
        ]
        if not all(isground_all):
            if not isground(t_rf, S):
                g = eq(type(v_rf), t_rf)
                yield from g(S)