nonmonotonic.doctest 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. .. Copyright (C) 2001-2019 NLTK Project
  2. .. For license information, see LICENSE.TXT
  3. ======================
  4. Nonmonotonic Reasoning
  5. ======================
  6. >>> from nltk import *
  7. >>> from nltk.inference.nonmonotonic import *
  8. >>> from nltk.sem import logic
  9. >>> logic._counter._value = 0
  10. >>> read_expr = logic.Expression.fromstring
  11. ------------------------
  12. Closed Domain Assumption
  13. ------------------------
  14. The only entities in the domain are those found in the assumptions or goal.
  15. If the domain only contains "A" and "B", then the expression "exists x.P(x)" can
  16. be replaced with "P(A) | P(B)" and an expression "all x.P(x)" can be replaced
  17. with "P(A) & P(B)".
  18. >>> p1 = read_expr(r'all x.(man(x) -> mortal(x))')
  19. >>> p2 = read_expr(r'man(Socrates)')
  20. >>> c = read_expr(r'mortal(Socrates)')
  21. >>> prover = Prover9Command(c, [p1,p2])
  22. >>> prover.prove()
  23. True
  24. >>> cdp = ClosedDomainProver(prover)
  25. >>> for a in cdp.assumptions(): print(a) # doctest: +SKIP
  26. (man(Socrates) -> mortal(Socrates))
  27. man(Socrates)
  28. >>> cdp.prove()
  29. True
  30. >>> p1 = read_expr(r'exists x.walk(x)')
  31. >>> p2 = read_expr(r'man(Socrates)')
  32. >>> c = read_expr(r'walk(Socrates)')
  33. >>> prover = Prover9Command(c, [p1,p2])
  34. >>> prover.prove()
  35. False
  36. >>> cdp = ClosedDomainProver(prover)
  37. >>> for a in cdp.assumptions(): print(a) # doctest: +SKIP
  38. walk(Socrates)
  39. man(Socrates)
  40. >>> cdp.prove()
  41. True
  42. >>> p1 = read_expr(r'exists x.walk(x)')
  43. >>> p2 = read_expr(r'man(Socrates)')
  44. >>> p3 = read_expr(r'-walk(Bill)')
  45. >>> c = read_expr(r'walk(Socrates)')
  46. >>> prover = Prover9Command(c, [p1,p2,p3])
  47. >>> prover.prove()
  48. False
  49. >>> cdp = ClosedDomainProver(prover)
  50. >>> for a in cdp.assumptions(): print(a) # doctest: +SKIP
  51. (walk(Socrates) | walk(Bill))
  52. man(Socrates)
  53. -walk(Bill)
  54. >>> cdp.prove()
  55. True
  56. >>> p1 = read_expr(r'walk(Socrates)')
  57. >>> p2 = read_expr(r'walk(Bill)')
  58. >>> c = read_expr(r'all x.walk(x)')
  59. >>> prover = Prover9Command(c, [p1,p2])
  60. >>> prover.prove()
  61. False
  62. >>> cdp = ClosedDomainProver(prover)
  63. >>> for a in cdp.assumptions(): print(a) # doctest: +SKIP
  64. walk(Socrates)
  65. walk(Bill)
  66. >>> print(cdp.goal()) # doctest: +SKIP
  67. (walk(Socrates) & walk(Bill))
  68. >>> cdp.prove()
  69. True
  70. >>> p1 = read_expr(r'girl(mary)')
  71. >>> p2 = read_expr(r'dog(rover)')
  72. >>> p3 = read_expr(r'all x.(girl(x) -> -dog(x))')
  73. >>> p4 = read_expr(r'all x.(dog(x) -> -girl(x))')
  74. >>> p5 = read_expr(r'chase(mary, rover)')
  75. >>> c = read_expr(r'exists y.(dog(y) & all x.(girl(x) -> chase(x,y)))')
  76. >>> prover = Prover9Command(c, [p1,p2,p3,p4,p5])
  77. >>> print(prover.prove())
  78. False
  79. >>> cdp = ClosedDomainProver(prover)
  80. >>> for a in cdp.assumptions(): print(a) # doctest: +SKIP
  81. girl(mary)
  82. dog(rover)
  83. ((girl(rover) -> -dog(rover)) & (girl(mary) -> -dog(mary)))
  84. ((dog(rover) -> -girl(rover)) & (dog(mary) -> -girl(mary)))
  85. chase(mary,rover)
  86. >>> print(cdp.goal()) # doctest: +SKIP
  87. ((dog(rover) & (girl(rover) -> chase(rover,rover)) & (girl(mary) -> chase(mary,rover))) | (dog(mary) & (girl(rover) -> chase(rover,mary)) & (girl(mary) -> chase(mary,mary))))
  88. >>> print(cdp.prove())
  89. True
  90. -----------------------
  91. Unique Names Assumption
  92. -----------------------
  93. No two entities in the domain represent the same entity unless it can be
  94. explicitly proven that they do. Therefore, if the domain contains "A" and "B",
  95. then add the assumption "-(A = B)" if it is not the case that
  96. "<assumptions> \|- (A = B)".
  97. >>> p1 = read_expr(r'man(Socrates)')
  98. >>> p2 = read_expr(r'man(Bill)')
  99. >>> c = read_expr(r'exists x.exists y.-(x = y)')
  100. >>> prover = Prover9Command(c, [p1,p2])
  101. >>> prover.prove()
  102. False
  103. >>> unp = UniqueNamesProver(prover)
  104. >>> for a in unp.assumptions(): print(a) # doctest: +SKIP
  105. man(Socrates)
  106. man(Bill)
  107. -(Socrates = Bill)
  108. >>> unp.prove()
  109. True
  110. >>> p1 = read_expr(r'all x.(walk(x) -> (x = Socrates))')
  111. >>> p2 = read_expr(r'Bill = William')
  112. >>> p3 = read_expr(r'Bill = Billy')
  113. >>> c = read_expr(r'-walk(William)')
  114. >>> prover = Prover9Command(c, [p1,p2,p3])
  115. >>> prover.prove()
  116. False
  117. >>> unp = UniqueNamesProver(prover)
  118. >>> for a in unp.assumptions(): print(a) # doctest: +SKIP
  119. all x.(walk(x) -> (x = Socrates))
  120. (Bill = William)
  121. (Bill = Billy)
  122. -(William = Socrates)
  123. -(Billy = Socrates)
  124. -(Socrates = Bill)
  125. >>> unp.prove()
  126. True
  127. -----------------------
  128. Closed World Assumption
  129. -----------------------
  130. The only entities that have certain properties are those that is it stated
  131. have the properties. We accomplish this assumption by "completing" predicates.
  132. If the assumptions contain "P(A)", then "all x.(P(x) -> (x=A))" is the completion
  133. of "P". If the assumptions contain "all x.(ostrich(x) -> bird(x))", then
  134. "all x.(bird(x) -> ostrich(x))" is the completion of "bird". If the
  135. assumptions don't contain anything that are "P", then "all x.-P(x)" is the
  136. completion of "P".
  137. >>> p1 = read_expr(r'walk(Socrates)')
  138. >>> p2 = read_expr(r'-(Socrates = Bill)')
  139. >>> c = read_expr(r'-walk(Bill)')
  140. >>> prover = Prover9Command(c, [p1,p2])
  141. >>> prover.prove()
  142. False
  143. >>> cwp = ClosedWorldProver(prover)
  144. >>> for a in cwp.assumptions(): print(a) # doctest: +SKIP
  145. walk(Socrates)
  146. -(Socrates = Bill)
  147. all z1.(walk(z1) -> (z1 = Socrates))
  148. >>> cwp.prove()
  149. True
  150. >>> p1 = read_expr(r'see(Socrates, John)')
  151. >>> p2 = read_expr(r'see(John, Mary)')
  152. >>> p3 = read_expr(r'-(Socrates = John)')
  153. >>> p4 = read_expr(r'-(John = Mary)')
  154. >>> c = read_expr(r'-see(Socrates, Mary)')
  155. >>> prover = Prover9Command(c, [p1,p2,p3,p4])
  156. >>> prover.prove()
  157. False
  158. >>> cwp = ClosedWorldProver(prover)
  159. >>> for a in cwp.assumptions(): print(a) # doctest: +SKIP
  160. see(Socrates,John)
  161. see(John,Mary)
  162. -(Socrates = John)
  163. -(John = Mary)
  164. all z3 z4.(see(z3,z4) -> (((z3 = Socrates) & (z4 = John)) | ((z3 = John) & (z4 = Mary))))
  165. >>> cwp.prove()
  166. True
  167. >>> p1 = read_expr(r'all x.(ostrich(x) -> bird(x))')
  168. >>> p2 = read_expr(r'bird(Tweety)')
  169. >>> p3 = read_expr(r'-ostrich(Sam)')
  170. >>> p4 = read_expr(r'Sam != Tweety')
  171. >>> c = read_expr(r'-bird(Sam)')
  172. >>> prover = Prover9Command(c, [p1,p2,p3,p4])
  173. >>> prover.prove()
  174. False
  175. >>> cwp = ClosedWorldProver(prover)
  176. >>> for a in cwp.assumptions(): print(a) # doctest: +SKIP
  177. all x.(ostrich(x) -> bird(x))
  178. bird(Tweety)
  179. -ostrich(Sam)
  180. -(Sam = Tweety)
  181. all z7.-ostrich(z7)
  182. all z8.(bird(z8) -> ((z8 = Tweety) | ostrich(z8)))
  183. >>> print(cwp.prove())
  184. True
  185. -----------------------
  186. Multi-Decorator Example
  187. -----------------------
  188. Decorators can be nested to utilize multiple assumptions.
  189. >>> p1 = read_expr(r'see(Socrates, John)')
  190. >>> p2 = read_expr(r'see(John, Mary)')
  191. >>> c = read_expr(r'-see(Socrates, Mary)')
  192. >>> prover = Prover9Command(c, [p1,p2])
  193. >>> print(prover.prove())
  194. False
  195. >>> cmd = ClosedDomainProver(UniqueNamesProver(ClosedWorldProver(prover)))
  196. >>> print(cmd.prove())
  197. True
  198. -----------------
  199. Default Reasoning
  200. -----------------
  201. >>> logic._counter._value = 0
  202. >>> premises = []
  203. define the taxonomy
  204. >>> premises.append(read_expr(r'all x.(elephant(x) -> animal(x))'))
  205. >>> premises.append(read_expr(r'all x.(bird(x) -> animal(x))'))
  206. >>> premises.append(read_expr(r'all x.(dove(x) -> bird(x))'))
  207. >>> premises.append(read_expr(r'all x.(ostrich(x) -> bird(x))'))
  208. >>> premises.append(read_expr(r'all x.(flying_ostrich(x) -> ostrich(x))'))
  209. default the properties using abnormalities
  210. >>> premises.append(read_expr(r'all x.((animal(x) & -Ab1(x)) -> -fly(x))')) #normal animals don't fly
  211. >>> premises.append(read_expr(r'all x.((bird(x) & -Ab2(x)) -> fly(x))')) #normal birds fly
  212. >>> premises.append(read_expr(r'all x.((ostrich(x) & -Ab3(x)) -> -fly(x))')) #normal ostriches don't fly
  213. specify abnormal entities
  214. >>> premises.append(read_expr(r'all x.(bird(x) -> Ab1(x))')) #flight
  215. >>> premises.append(read_expr(r'all x.(ostrich(x) -> Ab2(x))')) #non-flying bird
  216. >>> premises.append(read_expr(r'all x.(flying_ostrich(x) -> Ab3(x))')) #flying ostrich
  217. define entities
  218. >>> premises.append(read_expr(r'elephant(el)'))
  219. >>> premises.append(read_expr(r'dove(do)'))
  220. >>> premises.append(read_expr(r'ostrich(os)'))
  221. print the augmented assumptions list
  222. >>> prover = Prover9Command(None, premises)
  223. >>> command = UniqueNamesProver(ClosedWorldProver(prover))
  224. >>> for a in command.assumptions(): print(a) # doctest: +SKIP
  225. all x.(elephant(x) -> animal(x))
  226. all x.(bird(x) -> animal(x))
  227. all x.(dove(x) -> bird(x))
  228. all x.(ostrich(x) -> bird(x))
  229. all x.(flying_ostrich(x) -> ostrich(x))
  230. all x.((animal(x) & -Ab1(x)) -> -fly(x))
  231. all x.((bird(x) & -Ab2(x)) -> fly(x))
  232. all x.((ostrich(x) & -Ab3(x)) -> -fly(x))
  233. all x.(bird(x) -> Ab1(x))
  234. all x.(ostrich(x) -> Ab2(x))
  235. all x.(flying_ostrich(x) -> Ab3(x))
  236. elephant(el)
  237. dove(do)
  238. ostrich(os)
  239. all z1.(animal(z1) -> (elephant(z1) | bird(z1)))
  240. all z2.(Ab1(z2) -> bird(z2))
  241. all z3.(bird(z3) -> (dove(z3) | ostrich(z3)))
  242. all z4.(dove(z4) -> (z4 = do))
  243. all z5.(Ab2(z5) -> ostrich(z5))
  244. all z6.(Ab3(z6) -> flying_ostrich(z6))
  245. all z7.(ostrich(z7) -> ((z7 = os) | flying_ostrich(z7)))
  246. all z8.-flying_ostrich(z8)
  247. all z9.(elephant(z9) -> (z9 = el))
  248. -(el = os)
  249. -(el = do)
  250. -(os = do)
  251. >>> UniqueNamesProver(ClosedWorldProver(Prover9Command(read_expr('-fly(el)'), premises))).prove()
  252. True
  253. >>> UniqueNamesProver(ClosedWorldProver(Prover9Command(read_expr('fly(do)'), premises))).prove()
  254. True
  255. >>> UniqueNamesProver(ClosedWorldProver(Prover9Command(read_expr('-fly(os)'), premises))).prove()
  256. True