nonmonotonic.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. # Natural Language Toolkit: Nonmonotonic Reasoning
  2. #
  3. # Author: Daniel H. Garrette <dhgarrette@gmail.com>
  4. #
  5. # Copyright (C) 2001-2020 NLTK Project
  6. # URL: <http://nltk.org>
  7. # For license information, see LICENSE.TXT
  8. """
  9. A module to perform nonmonotonic reasoning. The ideas and demonstrations in
  10. this module are based on "Logical Foundations of Artificial Intelligence" by
  11. Michael R. Genesereth and Nils J. Nilsson.
  12. """
  13. from collections import defaultdict
  14. from functools import reduce
  15. from nltk.inference.prover9 import Prover9, Prover9Command
  16. from nltk.sem.logic import (
  17. VariableExpression,
  18. EqualityExpression,
  19. ApplicationExpression,
  20. Expression,
  21. AbstractVariableExpression,
  22. AllExpression,
  23. BooleanExpression,
  24. NegatedExpression,
  25. ExistsExpression,
  26. Variable,
  27. ImpExpression,
  28. AndExpression,
  29. unique_variable,
  30. operator,
  31. )
  32. from nltk.inference.api import Prover, ProverCommandDecorator
  33. class ProverParseError(Exception):
  34. pass
  35. def get_domain(goal, assumptions):
  36. if goal is None:
  37. all_expressions = assumptions
  38. else:
  39. all_expressions = assumptions + [-goal]
  40. return reduce(operator.or_, (a.constants() for a in all_expressions), set())
  41. class ClosedDomainProver(ProverCommandDecorator):
  42. """
  43. This is a prover decorator that adds domain closure assumptions before
  44. proving.
  45. """
  46. def assumptions(self):
  47. assumptions = [a for a in self._command.assumptions()]
  48. goal = self._command.goal()
  49. domain = get_domain(goal, assumptions)
  50. return [self.replace_quants(ex, domain) for ex in assumptions]
  51. def goal(self):
  52. goal = self._command.goal()
  53. domain = get_domain(goal, self._command.assumptions())
  54. return self.replace_quants(goal, domain)
  55. def replace_quants(self, ex, domain):
  56. """
  57. Apply the closed domain assumption to the expression
  58. - Domain = union([e.free()|e.constants() for e in all_expressions])
  59. - translate "exists x.P" to "(z=d1 | z=d2 | ... ) & P.replace(x,z)" OR
  60. "P.replace(x, d1) | P.replace(x, d2) | ..."
  61. - translate "all x.P" to "P.replace(x, d1) & P.replace(x, d2) & ..."
  62. :param ex: ``Expression``
  63. :param domain: set of {Variable}s
  64. :return: ``Expression``
  65. """
  66. if isinstance(ex, AllExpression):
  67. conjuncts = [
  68. ex.term.replace(ex.variable, VariableExpression(d)) for d in domain
  69. ]
  70. conjuncts = [self.replace_quants(c, domain) for c in conjuncts]
  71. return reduce(lambda x, y: x & y, conjuncts)
  72. elif isinstance(ex, BooleanExpression):
  73. return ex.__class__(
  74. self.replace_quants(ex.first, domain),
  75. self.replace_quants(ex.second, domain),
  76. )
  77. elif isinstance(ex, NegatedExpression):
  78. return -self.replace_quants(ex.term, domain)
  79. elif isinstance(ex, ExistsExpression):
  80. disjuncts = [
  81. ex.term.replace(ex.variable, VariableExpression(d)) for d in domain
  82. ]
  83. disjuncts = [self.replace_quants(d, domain) for d in disjuncts]
  84. return reduce(lambda x, y: x | y, disjuncts)
  85. else:
  86. return ex
  87. class UniqueNamesProver(ProverCommandDecorator):
  88. """
  89. This is a prover decorator that adds unique names assumptions before
  90. proving.
  91. """
  92. def assumptions(self):
  93. """
  94. - Domain = union([e.free()|e.constants() for e in all_expressions])
  95. - if "d1 = d2" cannot be proven from the premises, then add "d1 != d2"
  96. """
  97. assumptions = self._command.assumptions()
  98. domain = list(get_domain(self._command.goal(), assumptions))
  99. # build a dictionary of obvious equalities
  100. eq_sets = SetHolder()
  101. for a in assumptions:
  102. if isinstance(a, EqualityExpression):
  103. av = a.first.variable
  104. bv = a.second.variable
  105. # put 'a' and 'b' in the same set
  106. eq_sets[av].add(bv)
  107. new_assumptions = []
  108. for i, a in enumerate(domain):
  109. for b in domain[i + 1 :]:
  110. # if a and b are not already in the same equality set
  111. if b not in eq_sets[a]:
  112. newEqEx = EqualityExpression(
  113. VariableExpression(a), VariableExpression(b)
  114. )
  115. if Prover9().prove(newEqEx, assumptions):
  116. # we can prove that the names are the same entity.
  117. # remember that they are equal so we don't re-check.
  118. eq_sets[a].add(b)
  119. else:
  120. # we can't prove it, so assume unique names
  121. new_assumptions.append(-newEqEx)
  122. return assumptions + new_assumptions
  123. class SetHolder(list):
  124. """
  125. A list of sets of Variables.
  126. """
  127. def __getitem__(self, item):
  128. """
  129. :param item: ``Variable``
  130. :return: the set containing 'item'
  131. """
  132. assert isinstance(item, Variable)
  133. for s in self:
  134. if item in s:
  135. return s
  136. # item is not found in any existing set. so create a new set
  137. new = set([item])
  138. self.append(new)
  139. return new
  140. class ClosedWorldProver(ProverCommandDecorator):
  141. """
  142. This is a prover decorator that completes predicates before proving.
  143. If the assumptions contain "P(A)", then "all x.(P(x) -> (x=A))" is the completion of "P".
  144. If the assumptions contain "all x.(ostrich(x) -> bird(x))", then "all x.(bird(x) -> ostrich(x))" is the completion of "bird".
  145. If the assumptions don't contain anything that are "P", then "all x.-P(x)" is the completion of "P".
  146. walk(Socrates)
  147. Socrates != Bill
  148. + all x.(walk(x) -> (x=Socrates))
  149. ----------------
  150. -walk(Bill)
  151. see(Socrates, John)
  152. see(John, Mary)
  153. Socrates != John
  154. John != Mary
  155. + all x.all y.(see(x,y) -> ((x=Socrates & y=John) | (x=John & y=Mary)))
  156. ----------------
  157. -see(Socrates, Mary)
  158. all x.(ostrich(x) -> bird(x))
  159. bird(Tweety)
  160. -ostrich(Sam)
  161. Sam != Tweety
  162. + all x.(bird(x) -> (ostrich(x) | x=Tweety))
  163. + all x.-ostrich(x)
  164. -------------------
  165. -bird(Sam)
  166. """
  167. def assumptions(self):
  168. assumptions = self._command.assumptions()
  169. predicates = self._make_predicate_dict(assumptions)
  170. new_assumptions = []
  171. for p in predicates:
  172. predHolder = predicates[p]
  173. new_sig = self._make_unique_signature(predHolder)
  174. new_sig_exs = [VariableExpression(v) for v in new_sig]
  175. disjuncts = []
  176. # Turn the signatures into disjuncts
  177. for sig in predHolder.signatures:
  178. equality_exs = []
  179. for v1, v2 in zip(new_sig_exs, sig):
  180. equality_exs.append(EqualityExpression(v1, v2))
  181. disjuncts.append(reduce(lambda x, y: x & y, equality_exs))
  182. # Turn the properties into disjuncts
  183. for prop in predHolder.properties:
  184. # replace variables from the signature with new sig variables
  185. bindings = {}
  186. for v1, v2 in zip(new_sig_exs, prop[0]):
  187. bindings[v2] = v1
  188. disjuncts.append(prop[1].substitute_bindings(bindings))
  189. # make the assumption
  190. if disjuncts:
  191. # disjuncts exist, so make an implication
  192. antecedent = self._make_antecedent(p, new_sig)
  193. consequent = reduce(lambda x, y: x | y, disjuncts)
  194. accum = ImpExpression(antecedent, consequent)
  195. else:
  196. # nothing has property 'p'
  197. accum = NegatedExpression(self._make_antecedent(p, new_sig))
  198. # quantify the implication
  199. for new_sig_var in new_sig[::-1]:
  200. accum = AllExpression(new_sig_var, accum)
  201. new_assumptions.append(accum)
  202. return assumptions + new_assumptions
  203. def _make_unique_signature(self, predHolder):
  204. """
  205. This method figures out how many arguments the predicate takes and
  206. returns a tuple containing that number of unique variables.
  207. """
  208. return tuple(unique_variable() for i in range(predHolder.signature_len))
  209. def _make_antecedent(self, predicate, signature):
  210. """
  211. Return an application expression with 'predicate' as the predicate
  212. and 'signature' as the list of arguments.
  213. """
  214. antecedent = predicate
  215. for v in signature:
  216. antecedent = antecedent(VariableExpression(v))
  217. return antecedent
  218. def _make_predicate_dict(self, assumptions):
  219. """
  220. Create a dictionary of predicates from the assumptions.
  221. :param assumptions: a list of ``Expression``s
  222. :return: dict mapping ``AbstractVariableExpression`` to ``PredHolder``
  223. """
  224. predicates = defaultdict(PredHolder)
  225. for a in assumptions:
  226. self._map_predicates(a, predicates)
  227. return predicates
  228. def _map_predicates(self, expression, predDict):
  229. if isinstance(expression, ApplicationExpression):
  230. func, args = expression.uncurry()
  231. if isinstance(func, AbstractVariableExpression):
  232. predDict[func].append_sig(tuple(args))
  233. elif isinstance(expression, AndExpression):
  234. self._map_predicates(expression.first, predDict)
  235. self._map_predicates(expression.second, predDict)
  236. elif isinstance(expression, AllExpression):
  237. # collect all the universally quantified variables
  238. sig = [expression.variable]
  239. term = expression.term
  240. while isinstance(term, AllExpression):
  241. sig.append(term.variable)
  242. term = term.term
  243. if isinstance(term, ImpExpression):
  244. if isinstance(term.first, ApplicationExpression) and isinstance(
  245. term.second, ApplicationExpression
  246. ):
  247. func1, args1 = term.first.uncurry()
  248. func2, args2 = term.second.uncurry()
  249. if (
  250. isinstance(func1, AbstractVariableExpression)
  251. and isinstance(func2, AbstractVariableExpression)
  252. and sig == [v.variable for v in args1]
  253. and sig == [v.variable for v in args2]
  254. ):
  255. predDict[func2].append_prop((tuple(sig), term.first))
  256. predDict[func1].validate_sig_len(sig)
  257. class PredHolder(object):
  258. """
  259. This class will be used by a dictionary that will store information
  260. about predicates to be used by the ``ClosedWorldProver``.
  261. The 'signatures' property is a list of tuples defining signatures for
  262. which the predicate is true. For instance, 'see(john, mary)' would be
  263. result in the signature '(john,mary)' for 'see'.
  264. The second element of the pair is a list of pairs such that the first
  265. element of the pair is a tuple of variables and the second element is an
  266. expression of those variables that makes the predicate true. For instance,
  267. 'all x.all y.(see(x,y) -> know(x,y))' would result in "((x,y),('see(x,y)'))"
  268. for 'know'.
  269. """
  270. def __init__(self):
  271. self.signatures = []
  272. self.properties = []
  273. self.signature_len = None
  274. def append_sig(self, new_sig):
  275. self.validate_sig_len(new_sig)
  276. self.signatures.append(new_sig)
  277. def append_prop(self, new_prop):
  278. self.validate_sig_len(new_prop[0])
  279. self.properties.append(new_prop)
  280. def validate_sig_len(self, new_sig):
  281. if self.signature_len is None:
  282. self.signature_len = len(new_sig)
  283. elif self.signature_len != len(new_sig):
  284. raise Exception("Signature lengths do not match")
  285. def __str__(self):
  286. return "(%s,%s,%s)" % (self.signatures, self.properties, self.signature_len)
  287. def __repr__(self):
  288. return "%s" % self
  289. def closed_domain_demo():
  290. lexpr = Expression.fromstring
  291. p1 = lexpr(r"exists x.walk(x)")
  292. p2 = lexpr(r"man(Socrates)")
  293. c = lexpr(r"walk(Socrates)")
  294. prover = Prover9Command(c, [p1, p2])
  295. print(prover.prove())
  296. cdp = ClosedDomainProver(prover)
  297. print("assumptions:")
  298. for a in cdp.assumptions():
  299. print(" ", a)
  300. print("goal:", cdp.goal())
  301. print(cdp.prove())
  302. p1 = lexpr(r"exists x.walk(x)")
  303. p2 = lexpr(r"man(Socrates)")
  304. p3 = lexpr(r"-walk(Bill)")
  305. c = lexpr(r"walk(Socrates)")
  306. prover = Prover9Command(c, [p1, p2, p3])
  307. print(prover.prove())
  308. cdp = ClosedDomainProver(prover)
  309. print("assumptions:")
  310. for a in cdp.assumptions():
  311. print(" ", a)
  312. print("goal:", cdp.goal())
  313. print(cdp.prove())
  314. p1 = lexpr(r"exists x.walk(x)")
  315. p2 = lexpr(r"man(Socrates)")
  316. p3 = lexpr(r"-walk(Bill)")
  317. c = lexpr(r"walk(Socrates)")
  318. prover = Prover9Command(c, [p1, p2, p3])
  319. print(prover.prove())
  320. cdp = ClosedDomainProver(prover)
  321. print("assumptions:")
  322. for a in cdp.assumptions():
  323. print(" ", a)
  324. print("goal:", cdp.goal())
  325. print(cdp.prove())
  326. p1 = lexpr(r"walk(Socrates)")
  327. p2 = lexpr(r"walk(Bill)")
  328. c = lexpr(r"all x.walk(x)")
  329. prover = Prover9Command(c, [p1, p2])
  330. print(prover.prove())
  331. cdp = ClosedDomainProver(prover)
  332. print("assumptions:")
  333. for a in cdp.assumptions():
  334. print(" ", a)
  335. print("goal:", cdp.goal())
  336. print(cdp.prove())
  337. p1 = lexpr(r"girl(mary)")
  338. p2 = lexpr(r"dog(rover)")
  339. p3 = lexpr(r"all x.(girl(x) -> -dog(x))")
  340. p4 = lexpr(r"all x.(dog(x) -> -girl(x))")
  341. p5 = lexpr(r"chase(mary, rover)")
  342. c = lexpr(r"exists y.(dog(y) & all x.(girl(x) -> chase(x,y)))")
  343. prover = Prover9Command(c, [p1, p2, p3, p4, p5])
  344. print(prover.prove())
  345. cdp = ClosedDomainProver(prover)
  346. print("assumptions:")
  347. for a in cdp.assumptions():
  348. print(" ", a)
  349. print("goal:", cdp.goal())
  350. print(cdp.prove())
  351. def unique_names_demo():
  352. lexpr = Expression.fromstring
  353. p1 = lexpr(r"man(Socrates)")
  354. p2 = lexpr(r"man(Bill)")
  355. c = lexpr(r"exists x.exists y.(x != y)")
  356. prover = Prover9Command(c, [p1, p2])
  357. print(prover.prove())
  358. unp = UniqueNamesProver(prover)
  359. print("assumptions:")
  360. for a in unp.assumptions():
  361. print(" ", a)
  362. print("goal:", unp.goal())
  363. print(unp.prove())
  364. p1 = lexpr(r"all x.(walk(x) -> (x = Socrates))")
  365. p2 = lexpr(r"Bill = William")
  366. p3 = lexpr(r"Bill = Billy")
  367. c = lexpr(r"-walk(William)")
  368. prover = Prover9Command(c, [p1, p2, p3])
  369. print(prover.prove())
  370. unp = UniqueNamesProver(prover)
  371. print("assumptions:")
  372. for a in unp.assumptions():
  373. print(" ", a)
  374. print("goal:", unp.goal())
  375. print(unp.prove())
  376. def closed_world_demo():
  377. lexpr = Expression.fromstring
  378. p1 = lexpr(r"walk(Socrates)")
  379. p2 = lexpr(r"(Socrates != Bill)")
  380. c = lexpr(r"-walk(Bill)")
  381. prover = Prover9Command(c, [p1, p2])
  382. print(prover.prove())
  383. cwp = ClosedWorldProver(prover)
  384. print("assumptions:")
  385. for a in cwp.assumptions():
  386. print(" ", a)
  387. print("goal:", cwp.goal())
  388. print(cwp.prove())
  389. p1 = lexpr(r"see(Socrates, John)")
  390. p2 = lexpr(r"see(John, Mary)")
  391. p3 = lexpr(r"(Socrates != John)")
  392. p4 = lexpr(r"(John != Mary)")
  393. c = lexpr(r"-see(Socrates, Mary)")
  394. prover = Prover9Command(c, [p1, p2, p3, p4])
  395. print(prover.prove())
  396. cwp = ClosedWorldProver(prover)
  397. print("assumptions:")
  398. for a in cwp.assumptions():
  399. print(" ", a)
  400. print("goal:", cwp.goal())
  401. print(cwp.prove())
  402. p1 = lexpr(r"all x.(ostrich(x) -> bird(x))")
  403. p2 = lexpr(r"bird(Tweety)")
  404. p3 = lexpr(r"-ostrich(Sam)")
  405. p4 = lexpr(r"Sam != Tweety")
  406. c = lexpr(r"-bird(Sam)")
  407. prover = Prover9Command(c, [p1, p2, p3, p4])
  408. print(prover.prove())
  409. cwp = ClosedWorldProver(prover)
  410. print("assumptions:")
  411. for a in cwp.assumptions():
  412. print(" ", a)
  413. print("goal:", cwp.goal())
  414. print(cwp.prove())
  415. def combination_prover_demo():
  416. lexpr = Expression.fromstring
  417. p1 = lexpr(r"see(Socrates, John)")
  418. p2 = lexpr(r"see(John, Mary)")
  419. c = lexpr(r"-see(Socrates, Mary)")
  420. prover = Prover9Command(c, [p1, p2])
  421. print(prover.prove())
  422. command = ClosedDomainProver(UniqueNamesProver(ClosedWorldProver(prover)))
  423. for a in command.assumptions():
  424. print(a)
  425. print(command.prove())
  426. def default_reasoning_demo():
  427. lexpr = Expression.fromstring
  428. premises = []
  429. # define taxonomy
  430. premises.append(lexpr(r"all x.(elephant(x) -> animal(x))"))
  431. premises.append(lexpr(r"all x.(bird(x) -> animal(x))"))
  432. premises.append(lexpr(r"all x.(dove(x) -> bird(x))"))
  433. premises.append(lexpr(r"all x.(ostrich(x) -> bird(x))"))
  434. premises.append(lexpr(r"all x.(flying_ostrich(x) -> ostrich(x))"))
  435. # default properties
  436. premises.append(
  437. lexpr(r"all x.((animal(x) & -Ab1(x)) -> -fly(x))")
  438. ) # normal animals don't fly
  439. premises.append(
  440. lexpr(r"all x.((bird(x) & -Ab2(x)) -> fly(x))")
  441. ) # normal birds fly
  442. premises.append(
  443. lexpr(r"all x.((ostrich(x) & -Ab3(x)) -> -fly(x))")
  444. ) # normal ostriches don't fly
  445. # specify abnormal entities
  446. premises.append(lexpr(r"all x.(bird(x) -> Ab1(x))")) # flight
  447. premises.append(lexpr(r"all x.(ostrich(x) -> Ab2(x))")) # non-flying bird
  448. premises.append(lexpr(r"all x.(flying_ostrich(x) -> Ab3(x))")) # flying ostrich
  449. # define entities
  450. premises.append(lexpr(r"elephant(E)"))
  451. premises.append(lexpr(r"dove(D)"))
  452. premises.append(lexpr(r"ostrich(O)"))
  453. # print the assumptions
  454. prover = Prover9Command(None, premises)
  455. command = UniqueNamesProver(ClosedWorldProver(prover))
  456. for a in command.assumptions():
  457. print(a)
  458. print_proof("-fly(E)", premises)
  459. print_proof("fly(D)", premises)
  460. print_proof("-fly(O)", premises)
  461. def print_proof(goal, premises):
  462. lexpr = Expression.fromstring
  463. prover = Prover9Command(lexpr(goal), premises)
  464. command = UniqueNamesProver(ClosedWorldProver(prover))
  465. print(goal, prover.prove(), command.prove())
  466. def demo():
  467. closed_domain_demo()
  468. unique_names_demo()
  469. closed_world_demo()
  470. combination_prover_demo()
  471. default_reasoning_demo()
  472. if __name__ == "__main__":
  473. demo()