inference.doctest 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. .. Copyright (C) 2001-2019 NLTK Project
  2. .. For license information, see LICENSE.TXT
  3. ====================================
  4. Logical Inference and Model Building
  5. ====================================
  6. >>> from nltk import *
  7. >>> from nltk.sem.drt import DrtParser
  8. >>> from nltk.sem import logic
  9. >>> logic._counter._value = 0
  10. ------------
  11. Introduction
  12. ------------
  13. Within the area of automated reasoning, first order theorem proving
  14. and model building (or model generation) have both received much
  15. attention, and have given rise to highly sophisticated techniques. We
  16. focus therefore on providing an NLTK interface to third party tools
  17. for these tasks. In particular, the module ``nltk.inference`` can be
  18. used to access both theorem provers and model builders.
  19. ---------------------------------
  20. NLTK Interface to Theorem Provers
  21. ---------------------------------
  22. The main class used to interface with a theorem prover is the ``Prover``
  23. class, found in ``nltk.api``. The ``prove()`` method takes three optional
  24. arguments: a goal, a list of assumptions, and a ``verbose`` boolean to
  25. indicate whether the proof should be printed to the console. The proof goal
  26. and any assumptions need to be instances of the ``Expression`` class
  27. specified by ``nltk.sem.logic``. There are currently three theorem provers
  28. included with NLTK: ``Prover9``, ``TableauProver``, and
  29. ``ResolutionProver``. The first is an off-the-shelf prover, while the other
  30. two are written in Python and included in the ``nltk.inference`` package.
  31. >>> from nltk.sem import Expression
  32. >>> read_expr = Expression.fromstring
  33. >>> p1 = read_expr('man(socrates)')
  34. >>> p2 = read_expr('all x.(man(x) -> mortal(x))')
  35. >>> c = read_expr('mortal(socrates)')
  36. >>> Prover9().prove(c, [p1,p2])
  37. True
  38. >>> TableauProver().prove(c, [p1,p2])
  39. True
  40. >>> ResolutionProver().prove(c, [p1,p2], verbose=True)
  41. [1] {-mortal(socrates)} A
  42. [2] {man(socrates)} A
  43. [3] {-man(z2), mortal(z2)} A
  44. [4] {-man(socrates)} (1, 3)
  45. [5] {mortal(socrates)} (2, 3)
  46. [6] {} (1, 5)
  47. <BLANKLINE>
  48. True
  49. ---------------------
  50. The ``ProverCommand``
  51. ---------------------
  52. A ``ProverCommand`` is a stateful holder for a theorem
  53. prover. The command stores a theorem prover instance (of type ``Prover``),
  54. a goal, a list of assumptions, the result of the proof, and a string version
  55. of the entire proof. Corresponding to the three included ``Prover``
  56. implementations, there are three ``ProverCommand`` implementations:
  57. ``Prover9Command``, ``TableauProverCommand``, and
  58. ``ResolutionProverCommand``.
  59. The ``ProverCommand``'s constructor takes its goal and assumptions. The
  60. ``prove()`` command executes the ``Prover`` and ``proof()``
  61. returns a String form of the proof
  62. If the ``prove()`` method has not been called,
  63. then the prover command will be unable to display a proof.
  64. >>> prover = ResolutionProverCommand(c, [p1,p2])
  65. >>> print(prover.proof()) # doctest: +ELLIPSIS
  66. Traceback (most recent call last):
  67. File "...", line 1212, in __run
  68. compileflags, 1) in test.globs
  69. File "<doctest nltk/test/inference.doctest[10]>", line 1, in <module>
  70. File "...", line ..., in proof
  71. raise LookupError("You have to call prove() first to get a proof!")
  72. LookupError: You have to call prove() first to get a proof!
  73. >>> prover.prove()
  74. True
  75. >>> print(prover.proof())
  76. [1] {-mortal(socrates)} A
  77. [2] {man(socrates)} A
  78. [3] {-man(z4), mortal(z4)} A
  79. [4] {-man(socrates)} (1, 3)
  80. [5] {mortal(socrates)} (2, 3)
  81. [6] {} (1, 5)
  82. <BLANKLINE>
  83. The prover command stores the result of proving so that if ``prove()`` is
  84. called again, then the command can return the result without executing the
  85. prover again. This allows the user to access the result of the proof without
  86. wasting time re-computing what it already knows.
  87. >>> prover.prove()
  88. True
  89. >>> prover.prove()
  90. True
  91. The assumptions and goal may be accessed using the ``assumptions()`` and
  92. ``goal()`` methods, respectively.
  93. >>> prover.assumptions()
  94. [<ApplicationExpression man(socrates)>, <Alread_expression all x.(man(x) -> mortal(x))>]
  95. >>> prover.goal()
  96. <ApplicationExpression mortal(socrates)>
  97. The assumptions list may be modified using the ``add_assumptions()`` and
  98. ``retract_assumptions()`` methods. Both methods take a list of ``Expression``
  99. objects. Since adding or removing assumptions may change the result of the
  100. proof, the stored result is cleared when either of these methods are called.
  101. That means that ``proof()`` will be unavailable until ``prove()`` is called and
  102. a call to ``prove()`` will execute the theorem prover.
  103. >>> prover.retract_assumptions([read_expr('man(socrates)')])
  104. >>> print(prover.proof()) # doctest: +ELLIPSIS
  105. Traceback (most recent call last):
  106. File "...", line 1212, in __run
  107. compileflags, 1) in test.globs
  108. File "<doctest nltk/test/inference.doctest[10]>", line 1, in <module>
  109. File "...", line ..., in proof
  110. raise LookupError("You have to call prove() first to get a proof!")
  111. LookupError: You have to call prove() first to get a proof!
  112. >>> prover.prove()
  113. False
  114. >>> print(prover.proof())
  115. [1] {-mortal(socrates)} A
  116. [2] {-man(z6), mortal(z6)} A
  117. [3] {-man(socrates)} (1, 2)
  118. <BLANKLINE>
  119. >>> prover.add_assumptions([read_expr('man(socrates)')])
  120. >>> prover.prove()
  121. True
  122. -------
  123. Prover9
  124. -------
  125. Prover9 Installation
  126. ~~~~~~~~~~~~~~~~~~~~
  127. You can download Prover9 from http://www.cs.unm.edu/~mccune/prover9/.
  128. Extract the source code into a suitable directory and follow the
  129. instructions in the Prover9 ``README.make`` file to compile the executables.
  130. Install these into an appropriate location; the
  131. ``prover9_search`` variable is currently configured to look in the
  132. following locations:
  133. >>> p = Prover9()
  134. >>> p.binary_locations() # doctest: +NORMALIZE_WHITESPACE
  135. ['/usr/local/bin/prover9',
  136. '/usr/local/bin/prover9/bin',
  137. '/usr/local/bin',
  138. '/usr/bin',
  139. '/usr/local/prover9',
  140. '/usr/local/share/prover9']
  141. Alternatively, the environment variable ``PROVER9HOME`` may be configured with
  142. the binary's location.
  143. The path to the correct directory can be set manually in the following
  144. manner:
  145. >>> config_prover9(path='/usr/local/bin') # doctest: +SKIP
  146. [Found prover9: /usr/local/bin/prover9]
  147. If the executables cannot be found, ``Prover9`` will issue a warning message:
  148. >>> p.prove() # doctest: +SKIP
  149. Traceback (most recent call last):
  150. ...
  151. LookupError:
  152. ===========================================================================
  153. NLTK was unable to find the prover9 executable! Use config_prover9() or
  154. set the PROVER9HOME environment variable.
  155. <BLANKLINE>
  156. >> config_prover9('/path/to/prover9')
  157. <BLANKLINE>
  158. For more information, on prover9, see:
  159. <http://www.cs.unm.edu/~mccune/prover9/>
  160. ===========================================================================
  161. Using Prover9
  162. ~~~~~~~~~~~~~
  163. The general case in theorem proving is to determine whether ``S |- g``
  164. holds, where ``S`` is a possibly empty set of assumptions, and ``g``
  165. is a proof goal.
  166. As mentioned earlier, NLTK input to ``Prover9`` must be
  167. ``Expression``\ s of ``nltk.sem.logic``. A ``Prover9`` instance is
  168. initialized with a proof goal and, possibly, some assumptions. The
  169. ``prove()`` method attempts to find a proof of the goal, given the
  170. list of assumptions (in this case, none).
  171. >>> goal = read_expr('(man(x) <-> --man(x))')
  172. >>> prover = Prover9Command(goal)
  173. >>> prover.prove()
  174. True
  175. Given a ``ProverCommand`` instance ``prover``, the method
  176. ``prover.proof()`` will return a String of the extensive proof information
  177. provided by Prover9, shown in abbreviated form here::
  178. ============================== Prover9 ===============================
  179. Prover9 (32) version ...
  180. Process ... was started by ... on ...
  181. ...
  182. The command was ".../prover9 -f ...".
  183. ============================== end of head ===========================
  184. ============================== INPUT =================================
  185. % Reading from file /var/...
  186. formulas(goals).
  187. (all x (man(x) -> man(x))).
  188. end_of_list.
  189. ...
  190. ============================== end of search =========================
  191. THEOREM PROVED
  192. Exiting with 1 proof.
  193. Process 6317 exit (max_proofs) Mon Jan 21 15:23:28 2008
  194. As mentioned earlier, we may want to list some assumptions for
  195. the proof, as shown here.
  196. >>> g = read_expr('mortal(socrates)')
  197. >>> a1 = read_expr('all x.(man(x) -> mortal(x))')
  198. >>> prover = Prover9Command(g, assumptions=[a1])
  199. >>> prover.print_assumptions()
  200. all x.(man(x) -> mortal(x))
  201. However, the assumptions are not sufficient to derive the goal:
  202. >>> print(prover.prove())
  203. False
  204. So let's add another assumption:
  205. >>> a2 = read_expr('man(socrates)')
  206. >>> prover.add_assumptions([a2])
  207. >>> prover.print_assumptions()
  208. all x.(man(x) -> mortal(x))
  209. man(socrates)
  210. >>> print(prover.prove())
  211. True
  212. We can also show the assumptions in ``Prover9`` format.
  213. >>> prover.print_assumptions(output_format='Prover9')
  214. all x (man(x) -> mortal(x))
  215. man(socrates)
  216. >>> prover.print_assumptions(output_format='Spass')
  217. Traceback (most recent call last):
  218. . . .
  219. NameError: Unrecognized value for 'output_format': Spass
  220. Assumptions can be retracted from the list of assumptions.
  221. >>> prover.retract_assumptions([a1])
  222. >>> prover.print_assumptions()
  223. man(socrates)
  224. >>> prover.retract_assumptions([a1])
  225. Statements can be loaded from a file and parsed. We can then add these
  226. statements as new assumptions.
  227. >>> g = read_expr('all x.(boxer(x) -> -boxerdog(x))')
  228. >>> prover = Prover9Command(g)
  229. >>> prover.prove()
  230. False
  231. >>> import nltk.data
  232. >>> new = nltk.data.load('grammars/sample_grammars/background0.fol')
  233. >>> for a in new:
  234. ... print(a)
  235. all x.(boxerdog(x) -> dog(x))
  236. all x.(boxer(x) -> person(x))
  237. all x.-(dog(x) & person(x))
  238. exists x.boxer(x)
  239. exists x.boxerdog(x)
  240. >>> prover.add_assumptions(new)
  241. >>> print(prover.prove())
  242. True
  243. >>> print(prover.proof()) # doctest: +ELLIPSIS
  244. ============================== prooftrans ============================
  245. Prover9 (...) version ...
  246. Process ... was started by ... on ...
  247. ...
  248. The command was ".../prover9".
  249. ============================== end of head ===========================
  250. <BLANKLINE>
  251. ============================== end of input ==========================
  252. <BLANKLINE>
  253. ============================== PROOF =================================
  254. <BLANKLINE>
  255. % -------- Comments from original proof --------
  256. % Proof 1 at ... seconds.
  257. % Length of proof is 13.
  258. % Level of proof is 4.
  259. % Maximum clause weight is 0.000.
  260. % Given clauses 0.
  261. <BLANKLINE>
  262. <BLANKLINE>
  263. 1 (all x (boxerdog(x) -> dog(x))). [assumption].
  264. 2 (all x (boxer(x) -> person(x))). [assumption].
  265. 3 (all x -(dog(x) & person(x))). [assumption].
  266. 6 (all x (boxer(x) -> -boxerdog(x))). [goal].
  267. 8 -boxerdog(x) | dog(x). [clausify(1)].
  268. 9 boxerdog(c3). [deny(6)].
  269. 11 -boxer(x) | person(x). [clausify(2)].
  270. 12 boxer(c3). [deny(6)].
  271. 14 -dog(x) | -person(x). [clausify(3)].
  272. 15 dog(c3). [resolve(9,a,8,a)].
  273. 18 person(c3). [resolve(12,a,11,a)].
  274. 19 -person(c3). [resolve(15,a,14,a)].
  275. 20 $F. [resolve(19,a,18,a)].
  276. <BLANKLINE>
  277. ============================== end of proof ==========================
  278. ----------------------
  279. The equiv() method
  280. ----------------------
  281. One application of the theorem prover functionality is to check if
  282. two Expressions have the same meaning.
  283. The ``equiv()`` method calls a theorem prover to determine whether two
  284. Expressions are logically equivalent.
  285. >>> a = read_expr(r'exists x.(man(x) & walks(x))')
  286. >>> b = read_expr(r'exists x.(walks(x) & man(x))')
  287. >>> print(a.equiv(b))
  288. True
  289. The same method can be used on Discourse Representation Structures (DRSs).
  290. In this case, each DRS is converted to a first order logic form, and then
  291. passed to the theorem prover.
  292. >>> dp = DrtParser()
  293. >>> a = dp.parse(r'([x],[man(x), walks(x)])')
  294. >>> b = dp.parse(r'([x],[walks(x), man(x)])')
  295. >>> print(a.equiv(b))
  296. True
  297. --------------------------------
  298. NLTK Interface to Model Builders
  299. --------------------------------
  300. The top-level to model builders is parallel to that for
  301. theorem-provers. The ``ModelBuilder`` interface is located
  302. in ``nltk.inference.api``. It is currently only implemented by
  303. ``Mace``, which interfaces with the Mace4 model builder.
  304. Typically we use a model builder to show that some set of formulas has
  305. a model, and is therefore consistent. One way of doing this is by
  306. treating our candidate set of sentences as assumptions, and leaving
  307. the goal unspecified.
  308. Thus, the following interaction shows how both ``{a, c1}`` and ``{a, c2}``
  309. are consistent sets, since Mace succeeds in a building a
  310. model for each of them, while ``{c1, c2}`` is inconsistent.
  311. >>> a3 = read_expr('exists x.(man(x) and walks(x))')
  312. >>> c1 = read_expr('mortal(socrates)')
  313. >>> c2 = read_expr('-mortal(socrates)')
  314. >>> mace = Mace()
  315. >>> print(mace.build_model(None, [a3, c1]))
  316. True
  317. >>> print(mace.build_model(None, [a3, c2]))
  318. True
  319. We can also use the model builder as an adjunct to theorem prover.
  320. Let's suppose we are trying to prove ``S |- g``, i.e. that ``g``
  321. is logically entailed by assumptions ``S = {s1, s2, ..., sn}``.
  322. We can this same input to Mace4, and the model builder will try to
  323. find a counterexample, that is, to show that ``g`` does *not* follow
  324. from ``S``. So, given this input, Mace4 will try to find a model for
  325. the set ``S' = {s1, s2, ..., sn, (not g)}``. If ``g`` fails to follow
  326. from ``S``, then Mace4 may well return with a counterexample faster
  327. than Prover9 concludes that it cannot find the required proof.
  328. Conversely, if ``g`` *is* provable from ``S``, Mace4 may take a long
  329. time unsuccessfully trying to find a counter model, and will eventually give up.
  330. In the following example, we see that the model builder does succeed
  331. in building a model of the assumptions together with the negation of
  332. the goal. That is, it succeeds in finding a model
  333. where there is a woman that every man loves; Adam is a man; Eve is a
  334. woman; but Adam does not love Eve.
  335. >>> a4 = read_expr('exists y. (woman(y) & all x. (man(x) -> love(x,y)))')
  336. >>> a5 = read_expr('man(adam)')
  337. >>> a6 = read_expr('woman(eve)')
  338. >>> g = read_expr('love(adam,eve)')
  339. >>> print(mace.build_model(g, [a4, a5, a6]))
  340. True
  341. The Model Builder will fail to find a model if the assumptions do entail
  342. the goal. Mace will continue to look for models of ever-increasing sizes
  343. until the end_size number is reached. By default, end_size is 500,
  344. but it can be set manually for quicker response time.
  345. >>> a7 = read_expr('all x.(man(x) -> mortal(x))')
  346. >>> a8 = read_expr('man(socrates)')
  347. >>> g2 = read_expr('mortal(socrates)')
  348. >>> print(Mace(end_size=50).build_model(g2, [a7, a8]))
  349. False
  350. There is also a ``ModelBuilderCommand`` class that, like ``ProverCommand``,
  351. stores a ``ModelBuilder``, a goal, assumptions, a result, and a model. The
  352. only implementation in NLTK is ``MaceCommand``.
  353. -----
  354. Mace4
  355. -----
  356. Mace4 Installation
  357. ~~~~~~~~~~~~~~~~~~
  358. Mace4 is packaged with Prover9, and can be downloaded from the same
  359. source, namely http://www.cs.unm.edu/~mccune/prover9/. It is installed
  360. in the same manner as Prover9.
  361. Using Mace4
  362. ~~~~~~~~~~~
  363. Check whether Mace4 can find a model.
  364. >>> a = read_expr('(see(mary,john) & -(mary = john))')
  365. >>> mb = MaceCommand(assumptions=[a])
  366. >>> mb.build_model()
  367. True
  368. Show the model in 'tabular' format.
  369. >>> print(mb.model(format='tabular'))
  370. % number = 1
  371. % seconds = 0
  372. <BLANKLINE>
  373. % Interpretation of size 2
  374. <BLANKLINE>
  375. john : 0
  376. <BLANKLINE>
  377. mary : 1
  378. <BLANKLINE>
  379. see :
  380. | 0 1
  381. ---+----
  382. 0 | 0 0
  383. 1 | 1 0
  384. <BLANKLINE>
  385. Show the model in 'tabular' format.
  386. >>> print(mb.model(format='cooked'))
  387. % number = 1
  388. % seconds = 0
  389. <BLANKLINE>
  390. % Interpretation of size 2
  391. <BLANKLINE>
  392. john = 0.
  393. <BLANKLINE>
  394. mary = 1.
  395. <BLANKLINE>
  396. - see(0,0).
  397. - see(0,1).
  398. see(1,0).
  399. - see(1,1).
  400. <BLANKLINE>
  401. The property ``valuation`` accesses the stored ``Valuation``.
  402. >>> print(mb.valuation)
  403. {'john': 'a', 'mary': 'b', 'see': {('b', 'a')}}
  404. We can return to our earlier example and inspect the model:
  405. >>> mb = MaceCommand(g, assumptions=[a4, a5, a6])
  406. >>> m = mb.build_model()
  407. >>> print(mb.model(format='cooked'))
  408. % number = 1
  409. % seconds = 0
  410. <BLANKLINE>
  411. % Interpretation of size 2
  412. <BLANKLINE>
  413. adam = 0.
  414. <BLANKLINE>
  415. eve = 0.
  416. <BLANKLINE>
  417. c1 = 1.
  418. <BLANKLINE>
  419. man(0).
  420. - man(1).
  421. <BLANKLINE>
  422. woman(0).
  423. woman(1).
  424. <BLANKLINE>
  425. - love(0,0).
  426. love(0,1).
  427. - love(1,0).
  428. - love(1,1).
  429. <BLANKLINE>
  430. Here, we can see that ``adam`` and ``eve`` have been assigned the same
  431. individual, namely ``0`` as value; ``0`` is both a man and a woman; a second
  432. individual ``1`` is also a woman; and ``0`` loves ``1``. Thus, this is
  433. an interpretation in which there is a woman that every man loves but
  434. Adam doesn't love Eve.
  435. Mace can also be used with propositional logic.
  436. >>> p = read_expr('P')
  437. >>> q = read_expr('Q')
  438. >>> mb = MaceCommand(q, [p, p>-q])
  439. >>> mb.build_model()
  440. True
  441. >>> mb.valuation['P']
  442. True
  443. >>> mb.valuation['Q']
  444. False