# 人工智能邏輯編程

## 如何用邏輯編程解決問題

``A∶− B1,B2,...,Bn.``

`ancestor(X，Z): - father(X，Y)，ancestor(Y，Z)`

## 安裝必需的包

Kanren

``pip install kanren``

SymPy
SymPy是符號數學的Python庫。 它旨在成爲一個全功能的計算機代數系統(CAS)，同時保持代碼儘可能簡單，以便易於理解和擴展。 以下命令是用來安裝SymPy -

``pip install sympy``

## 邏輯編程的例子

``````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(associative, mul)

``a, b = var('a'), var('b')``

``Original_pattern = (mul, (add, 5, a), b)``

``````exp1 = (mul, 2, (add, 3, 1))

``````print(run(0, (a,b), eq(original_pattern, exp1)))
print(run(0, (a,b), eq(original_pattern, exp2)))``````

``````((3,2))
()``````

## 查找素數

``````from kanren import isvar, run, membero
from kanren.core import success, fail, goaleval, condeseq, eq, var
from sympy.ntheory.generate import prime, isprime
import itertools as it``````

``````def prime_check(x):
if isvar(x):
return condeseq([(eq,x,p)] for p in map(prime, it.count(1)))
else:
return success if isprime(x) else fail``````

``````x = var()
print((set(run(0,x,(membero,x,(12,14,15,19,20,21,22,23,29,30,41,44,52,62,65,85)),
(prime_check,x)))))
print((run(10,x,prime_check(x))))``````

``````{19, 23, 29, 41}
(2, 3, 5, 7, 11, 13, 17, 19, 23, 29)``````

## 解決難題

``````有五間房子。

``````from kanren import *
from kanren.core import lall
import time``````

``````def left(q, p, list):
return membero((q,p), zip(list, list[1:]))
def next(q, p, list):
return conde([left(q, p, list)], [left(p, q, list)])``````

``houses = var()``

`5`間房子 -

``````rules_zebraproblem = lall(
(eq, (var(), var(), var(), var(), var()), houses),

(membero,('Englishman', var(), var(), var(), 'red'), houses),
(membero,('Swede', var(), var(), 'dog', var()), houses),
(membero,('Dane', var(), 'tea', var(), var()), houses),
(left,(var(), var(), var(), var(), 'green'),
(var(), var(), var(), var(), 'white'), houses),
(membero,(var(), var(), 'coffee', var(), 'green'), houses),
(membero,(var(), 'Pall Mall', var(), 'birds', var()), houses),
(membero,(var(), 'Dunhill', var(), var(), 'yellow'), houses),
(eq,(var(), var(), (var(), var(), 'milk', var(), var()), var(), var()), houses),
(eq,(('Norwegian', var(), var(), var(), var()), var(), var(), var(), var()), houses),
(next,(var(), 'Blend', var(), var(), var()),
(var(), var(), var(), 'cats', var()), houses),
(next,(var(), 'Dunhill', var(), var(), var()),
(var(), var(), var(), 'horse', var()), houses),
(membero,(var(), 'Blue Master', 'beer', var(), var()), houses),
(membero,('German', 'Prince', var(), var(), var()), houses),
(next,('Norwegian', var(), var(), var(), var()),
(var(), var(), var(), var(), 'blue'), houses),
(next,(var(), 'Blend', var(), var(), var()),
(var(), var(), 'water', var(), var()), houses),
(membero,(var(), var(), var(), 'zebra', var()), houses)
)``````

``solutions = run(0, houses, rules_zebraproblem)``

``output_zebra = [house for house in solutions[0] if 'zebra' in house][0][0]``

``print ('\n'+ output_zebra + 'owns zebra.')``

``German owns zebra.``