nonmonotonic.py 18 KB

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