resolution.doctest 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. .. Copyright (C) 2001-2019 NLTK Project
  2. .. For license information, see LICENSE.TXT
  3. =========================
  4. Resolution Theorem Prover
  5. =========================
  6. >>> from nltk.inference.resolution import *
  7. >>> from nltk.sem import logic
  8. >>> from nltk.sem.logic import *
  9. >>> logic._counter._value = 0
  10. >>> read_expr = logic.Expression.fromstring
  11. >>> P = read_expr('P')
  12. >>> Q = read_expr('Q')
  13. >>> R = read_expr('R')
  14. >>> A = read_expr('A')
  15. >>> B = read_expr('B')
  16. >>> x = read_expr('x')
  17. >>> y = read_expr('y')
  18. >>> z = read_expr('z')
  19. -------------------------------
  20. Test most_general_unification()
  21. -------------------------------
  22. >>> print(most_general_unification(x, x))
  23. {}
  24. >>> print(most_general_unification(A, A))
  25. {}
  26. >>> print(most_general_unification(A, x))
  27. {x: A}
  28. >>> print(most_general_unification(x, A))
  29. {x: A}
  30. >>> print(most_general_unification(x, y))
  31. {x: y}
  32. >>> print(most_general_unification(P(x), P(A)))
  33. {x: A}
  34. >>> print(most_general_unification(P(x,B), P(A,y)))
  35. {x: A, y: B}
  36. >>> print(most_general_unification(P(x,B), P(B,x)))
  37. {x: B}
  38. >>> print(most_general_unification(P(x,y), P(A,x)))
  39. {x: A, y: x}
  40. >>> print(most_general_unification(P(Q(x)), P(y)))
  41. {y: Q(x)}
  42. ------------
  43. Test unify()
  44. ------------
  45. >>> print(Clause([]).unify(Clause([])))
  46. []
  47. >>> print(Clause([P(x)]).unify(Clause([-P(A)])))
  48. [{}]
  49. >>> print(Clause([P(A), Q(x)]).unify(Clause([-P(x), R(x)])))
  50. [{R(A), Q(A)}]
  51. >>> print(Clause([P(A), Q(x), R(x,y)]).unify(Clause([-P(x), Q(y)])))
  52. [{Q(y), Q(A), R(A,y)}]
  53. >>> print(Clause([P(A), -Q(y)]).unify(Clause([-P(x), Q(B)])))
  54. [{}]
  55. >>> print(Clause([P(x), Q(x)]).unify(Clause([-P(A), -Q(B)])))
  56. [{-Q(B), Q(A)}, {-P(A), P(B)}]
  57. >>> print(Clause([P(x,x), Q(x), R(x)]).unify(Clause([-P(A,z), -Q(B)])))
  58. [{-Q(B), Q(A), R(A)}, {-P(A,z), R(B), P(B,B)}]
  59. >>> a = clausify(read_expr('P(A)'))
  60. >>> b = clausify(read_expr('A=B'))
  61. >>> print(a[0].unify(b[0]))
  62. [{P(B)}]
  63. -------------------------
  64. Test is_tautology()
  65. -------------------------
  66. >>> print(Clause([P(A), -P(A)]).is_tautology())
  67. True
  68. >>> print(Clause([-P(A), P(A)]).is_tautology())
  69. True
  70. >>> print(Clause([P(x), -P(A)]).is_tautology())
  71. False
  72. >>> print(Clause([Q(B), -P(A), P(A)]).is_tautology())
  73. True
  74. >>> print(Clause([-Q(A), P(R(A)), -P(R(A)), Q(x), -R(y)]).is_tautology())
  75. True
  76. >>> print(Clause([P(x), -Q(A)]).is_tautology())
  77. False
  78. -------------------------
  79. Test subsumes()
  80. -------------------------
  81. >>> print(Clause([P(A), Q(B)]).subsumes(Clause([P(A), Q(B)])))
  82. True
  83. >>> print(Clause([-P(A)]).subsumes(Clause([P(A)])))
  84. False
  85. >>> print(Clause([P(A), Q(B)]).subsumes(Clause([Q(B), P(A)])))
  86. True
  87. >>> print(Clause([P(A), Q(B)]).subsumes(Clause([Q(B), R(A), P(A)])))
  88. True
  89. >>> print(Clause([P(A), R(A), Q(B)]).subsumes(Clause([Q(B), P(A)])))
  90. False
  91. >>> print(Clause([P(x)]).subsumes(Clause([P(A)])))
  92. True
  93. >>> print(Clause([P(A)]).subsumes(Clause([P(x)])))
  94. True
  95. ------------
  96. Test prove()
  97. ------------
  98. >>> print(ResolutionProverCommand(read_expr('man(x)')).prove())
  99. False
  100. >>> print(ResolutionProverCommand(read_expr('(man(x) -> man(x))')).prove())
  101. True
  102. >>> print(ResolutionProverCommand(read_expr('(man(x) -> --man(x))')).prove())
  103. True
  104. >>> print(ResolutionProverCommand(read_expr('-(man(x) & -man(x))')).prove())
  105. True
  106. >>> print(ResolutionProverCommand(read_expr('(man(x) | -man(x))')).prove())
  107. True
  108. >>> print(ResolutionProverCommand(read_expr('(man(x) -> man(x))')).prove())
  109. True
  110. >>> print(ResolutionProverCommand(read_expr('-(man(x) & -man(x))')).prove())
  111. True
  112. >>> print(ResolutionProverCommand(read_expr('(man(x) | -man(x))')).prove())
  113. True
  114. >>> print(ResolutionProverCommand(read_expr('(man(x) -> man(x))')).prove())
  115. True
  116. >>> print(ResolutionProverCommand(read_expr('(man(x) <-> man(x))')).prove())
  117. True
  118. >>> print(ResolutionProverCommand(read_expr('-(man(x) <-> -man(x))')).prove())
  119. True
  120. >>> print(ResolutionProverCommand(read_expr('all x.man(x)')).prove())
  121. False
  122. >>> print(ResolutionProverCommand(read_expr('-all x.some y.F(x,y) & some x.all y.(-F(x,y))')).prove())
  123. False
  124. >>> print(ResolutionProverCommand(read_expr('some x.all y.sees(x,y)')).prove())
  125. False
  126. >>> p1 = read_expr('all x.(man(x) -> mortal(x))')
  127. >>> p2 = read_expr('man(Socrates)')
  128. >>> c = read_expr('mortal(Socrates)')
  129. >>> ResolutionProverCommand(c, [p1,p2]).prove()
  130. True
  131. >>> p1 = read_expr('all x.(man(x) -> walks(x))')
  132. >>> p2 = read_expr('man(John)')
  133. >>> c = read_expr('some y.walks(y)')
  134. >>> ResolutionProverCommand(c, [p1,p2]).prove()
  135. True
  136. >>> p = read_expr('some e1.some e2.(believe(e1,john,e2) & walk(e2,mary))')
  137. >>> c = read_expr('some e0.walk(e0,mary)')
  138. >>> ResolutionProverCommand(c, [p]).prove()
  139. True
  140. ------------
  141. Test proof()
  142. ------------
  143. >>> p1 = read_expr('all x.(man(x) -> mortal(x))')
  144. >>> p2 = read_expr('man(Socrates)')
  145. >>> c = read_expr('mortal(Socrates)')
  146. >>> logic._counter._value = 0
  147. >>> tp = ResolutionProverCommand(c, [p1,p2])
  148. >>> tp.prove()
  149. True
  150. >>> print(tp.proof())
  151. [1] {-mortal(Socrates)} A
  152. [2] {-man(z2), mortal(z2)} A
  153. [3] {man(Socrates)} A
  154. [4] {-man(Socrates)} (1, 2)
  155. [5] {mortal(Socrates)} (2, 3)
  156. [6] {} (1, 5)
  157. <BLANKLINE>
  158. ------------------
  159. Question Answering
  160. ------------------
  161. One answer
  162. >>> p1 = read_expr('father_of(art,john)')
  163. >>> p2 = read_expr('father_of(bob,kim)')
  164. >>> p3 = read_expr('all x.all y.(father_of(x,y) -> parent_of(x,y))')
  165. >>> c = read_expr('all x.(parent_of(x,john) -> ANSWER(x))')
  166. >>> logic._counter._value = 0
  167. >>> tp = ResolutionProverCommand(None, [p1,p2,p3,c])
  168. >>> sorted(tp.find_answers())
  169. [<ConstantExpression art>]
  170. >>> print(tp.proof()) # doctest: +SKIP
  171. [1] {father_of(art,john)} A
  172. [2] {father_of(bob,kim)} A
  173. [3] {-father_of(z3,z4), parent_of(z3,z4)} A
  174. [4] {-parent_of(z6,john), ANSWER(z6)} A
  175. [5] {parent_of(art,john)} (1, 3)
  176. [6] {parent_of(bob,kim)} (2, 3)
  177. [7] {ANSWER(z6), -father_of(z6,john)} (3, 4)
  178. [8] {ANSWER(art)} (1, 7)
  179. [9] {ANSWER(art)} (4, 5)
  180. <BLANKLINE>
  181. Multiple answers
  182. >>> p1 = read_expr('father_of(art,john)')
  183. >>> p2 = read_expr('mother_of(ann,john)')
  184. >>> p3 = read_expr('all x.all y.(father_of(x,y) -> parent_of(x,y))')
  185. >>> p4 = read_expr('all x.all y.(mother_of(x,y) -> parent_of(x,y))')
  186. >>> c = read_expr('all x.(parent_of(x,john) -> ANSWER(x))')
  187. >>> logic._counter._value = 0
  188. >>> tp = ResolutionProverCommand(None, [p1,p2,p3,p4,c])
  189. >>> sorted(tp.find_answers())
  190. [<ConstantExpression ann>, <ConstantExpression art>]
  191. >>> print(tp.proof()) # doctest: +SKIP
  192. [ 1] {father_of(art,john)} A
  193. [ 2] {mother_of(ann,john)} A
  194. [ 3] {-father_of(z3,z4), parent_of(z3,z4)} A
  195. [ 4] {-mother_of(z7,z8), parent_of(z7,z8)} A
  196. [ 5] {-parent_of(z10,john), ANSWER(z10)} A
  197. [ 6] {parent_of(art,john)} (1, 3)
  198. [ 7] {parent_of(ann,john)} (2, 4)
  199. [ 8] {ANSWER(z10), -father_of(z10,john)} (3, 5)
  200. [ 9] {ANSWER(art)} (1, 8)
  201. [10] {ANSWER(z10), -mother_of(z10,john)} (4, 5)
  202. [11] {ANSWER(ann)} (2, 10)
  203. [12] {ANSWER(art)} (5, 6)
  204. [13] {ANSWER(ann)} (5, 7)
  205. <BLANKLINE>