def possible(y, x, n): ''' initialize variables''' global grid array = [] ''' initialize for loop and if true there is no number similar to the input number inside the row or column of the grid or board.''' for i in range(0, 9): if grid[y][i] == n: if grid[i][x] == n: return False ''' initialize variables and calculate the index location of the board.''' output = var() x0 = (x // 3) * 3 y0 = (y // 3) * 3 ''' initialize a for loop and store the values inside array''' for i in range(0, 3): for j in range(0, 3): array.append(grid[y0 + i][x0 + j]) ''' Applying Kanren functions into the Sudoku algorithm. The functions are use to implement a logic program to verify whether the input variable is identical to the numbers inside the array. If there is no numbers identical to the numbers inside the array, then the function will return True.''' find_number = run(1, output, eq(output, n), membero(output, array)) if len(find_number) != 0: return False return True
print("Practice with Kanren") # Function for displaying results statement def results(): print("\n**Results**") # Declare Variables x = kanren.var() z = kanren.var() # Heading intro() practice() print(run(1, x, eq(x, 5))) print(run(1, x, eq(x, z), eq(z, 3))) print(run(1, x, eq((1, 2), (1, x)))) print(run(2, x, membero(x, (1, 2, 3)), membero(x, (2, 3, 4)))) z = kanren.var('test') print(z) a, b, c = vars(3) print(a) print(b) print(c) # Defining operations add = 'add' mul = 'mul'
from kanren import run, var, fact from kanren.assoccomm import eq_assoccomm as eq from kanren.assoccomm import commutative, associative add = 'add' mul = 'mul' fact(commutative, mul) fact(commutative, add) fact(associative, mul) fact(associative, add) a, b = var('a'), var('b') original_pattern = (mul, (add, 5, a), b) exp1 = (mul, 2, (add, 3, 1)) exp2 = (add, 5, (mul, 8, 1)) print(run(0, (a, b), eq(original_pattern, exp1))) print(run(0, (a, b), eq(original_pattern, exp2)))
from kanren import run, var, fact from kanren.assoccomm import eq_assoccomm as eq from kanren.assoccomm import commutative, associative add = 'add' mul = 'mul' fact(commutative, mul) fact(commutative, add) fact(associative, mul) fact(associative, add) x, y = var('x'), var('y') pattern = (mul, (add, 1, x), y) expr = (mul, 2, (add, 3, 1)) print(run(0, (x, y), eq(pattern, expr)))
from kanren import run, var, fact from kanren.assoccomm import eq_assoccomm as eq from kanren.assoccomm import commutative, associative # Define some dummy Operationss add = "add" mul = "mul" # Declare that these ops are commutative using the facts system fact(commutative, mul) fact(commutative, add) fact(associative, mul) fact(associative, add) # Define some logic variables x, y = var(), var() # Two expressions to match pattern = (mul, (add, 1, x), y) # (1 + x) * y expr = (mul, 2, (add, 3, 1)) # 2 * (3 + 1) res = run(0, (x, y), eq(pattern, expr)) print(res) # prints ((3, 2),) meaning # x matches to 3 # y matches to 2
from kanren import run, var, fact from kanren.assoccomm import eq_assoccomm as eq from kanren.assoccomm import commutative, associative add = 'add' #Defining operations mul = 'mul' fact(commutative, mul) #Addition and multiplication are commutative and associative fact(commutative, add) fact(associative, mul) fact(associative, add) x = var() print( "This program solves variable x for the general equation \"ax + b = 0\" with the provided a and b from user" ) print("Enter a: ") a = int(input()) print("Enter b: ") b = int(input()) expression = (add, (mul, a, x), b) expr1 = expression expression = (add, (mul, a, (-b / a)), b) print("x = ", run(1, x, eq(expr1, expression)))
from kanren import run, var, fact from kanren.assoccomm import eq_assoccomm as eq from kanren.assoccomm import commutative, associative add = 'add' # Defining operations mul = 'mul' # Addition and multiplication are commutative and associative fact(commutative, mul) fact(commutative, add) fact(associative, mul) fact(associative, add) a, b, c = var('a'), var('b'), var('c') # Defining variables # 2ab+b+3c is the expression we have' expression = (add, (mul, 2, a, b), b, (mul, 3, c)) expression = (add, (mul, 3, -2), (mul, (add, 1, (mul, 2, 3)), -1) ) # Expression expr1 = (add, (mul, (add, 1, (mul, 2, a)), b), (mul, 3, c) ) # Expressions to match expr2 = (add, (mul, c, 3), (mul, b, (add, (mul, 2, a), 1))) expr3 = (add, (add, (mul, (mul, 2, a), b), b), (mul, 3, c)) res = run(0, (a, b, c), eq(expr1, expression)) # Calls to run() print(expr1) print(expr2) print(expr3) print(res)
from kanren import run, var, fact from kanren.assoccomm import eq_assoccomm as eq from kanren.assoccomm import commutative, associative # Define some dummy Operationss add = 'add' mul = 'mul' # Declare that these ops are commutative using the facts system fact(commutative, mul) fact(commutative, add) fact(associative, mul) fact(associative, add) # Define some wild variables x, y = var('x'), var('y') # Two expressions to match pattern = (mul, (add, 1, x), y) # (1 + x) * y expr = (mul, 2, (add, 3, 1)) # 2 * (3 + 1) print(run(0, (x,y), eq(pattern, expr))) # prints ((3, 2),) meaning # x matches to 3 # y matches to 2