Advertisement
Guest User

Resolution Prover

a guest
May 8th, 2020
426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.16 KB | None | 0 0
  1. /* Clause Form Program
  2.  
  3. Propositional operators are: neg, and, or, imp, revimp, uparrow, downarrow, notimp, and notrevimp.
  4. */
  5.  
  6. ?-op(140, fy, neg).
  7. ?-op(160, xfy, [and, or, imp, revimp, uparrow, downarrow, notimp, notrevimp, equiv, notequiv]).
  8.  
  9. /* member(Item, List) :- Item occurs in List.
  10. */
  11.  
  12. member(X, [X | _]).
  13. member(X, [_ | Tail]) :- member(X, Tail).
  14.  
  15. /* remove(Item, List, Newlist) :-
  16. Newlist is the result of removing all occurenves of Item from List.
  17. */
  18.  
  19. remove(_, [], []).
  20. remove(X, [X | Tail], Newtail) :-
  21. remove(X, Tail, Newtail).
  22. remove(X, [Head | Tail], [Head | Newatil]) :-
  23. remove(X, Tail, Newatil).
  24.  
  25. /* conjunctive(X) :- X is an alpha-formula.
  26. */
  27.  
  28. conjunctive(_ and _).
  29. conjunctive(neg (_ or _)).
  30. conjunctive(neg (_ imp _)).
  31. conjunctive(neg (_ revimp _)).
  32. conjunctive(neg (_ uparrow _)).
  33. conjunctive(_ downarrow _).
  34. conjunctive(_ notimp _).
  35. conjunctive(_ notrevimp _).
  36.  
  37. /* Extras for equiv and notequiv. */
  38.  
  39. conjunctive(_ equiv _).
  40. conjunctive(neg (_ notequiv _)).
  41.  
  42. /* disjunctive(X) :- X is a beta-formula.
  43. */
  44.  
  45. disjunctive(neg (_ and _)).
  46. disjunctive(_ or _).
  47. disjunctive(_ imp _).
  48. disjunctive(_ revimp _).
  49. disjunctive(_ uparrow _).
  50. disjunctive(neg (_ downarrow _)).
  51. disjunctive(neg (_ notimp _)).
  52. disjunctive(neg (_ notrevimp _)).
  53.  
  54. /* Extras. */
  55.  
  56. disjunctive(neg (_ equiv _)).
  57. disjunctive(_ notequiv _).
  58.  
  59. /* unary(X) :- X is a double negation or a negated constant.
  60. */
  61.  
  62. unary(neg neg _).
  63. unary(neg true).
  64. unary(neg false).
  65.  
  66. /* components(X, Y, Z) :- Y and Z are components of the formula X as defined in the alpha/beta table (FOR CNF).
  67. */
  68.  
  69. components(X and Y, X, Y).
  70. components(neg (X and Y), neg X, neg Y).
  71.  
  72. components(X or Y, X, Y).
  73. components(neg (X or Y), neg X, neg Y).
  74.  
  75. components(X imp Y, neg X, Y).
  76. components(neg (X imp Y), X, neg Y).
  77.  
  78. components(X revimp Y, X, neg Y).
  79. components(neg (X revimp Y), neg X, Y).
  80.  
  81. components(X uparrow Y, neg X, neg Y).
  82. components(neg (X uparrow Y), X, Y).
  83.  
  84. components(X downarrow Y, neg X, neg Y).
  85. components(neg (X downarrow Y), X, Y).
  86.  
  87. components(X notimp Y, X, neg Y).
  88. components(neg (X notimp Y), neg X, Y).
  89.  
  90. components(X notrevimp Y, neg X, Y).
  91. components(neg (X notrevimp Y), X, neg Y).
  92.  
  93. /* Extras for equiv and notequiv. */
  94.  
  95. components(X equiv Y, neg X or Y, neg Y or X).
  96. components(neg (X equiv Y), neg X and Y, neg Y and X).
  97.  
  98. components(X notequiv Y, neg X and Y, neg Y and X).
  99. components(neg (X notequiv Y), neg X or Y, neg Y or X).
  100.  
  101. /* component(X, Y) :- Y is the component of the UNARY formula X.
  102. */
  103.  
  104. component(neg neg X, X).
  105. component(neg true, false).
  106. component(neg false, true).
  107.  
  108. /* singlestep(Old, New) :- New is the result of applying a single step of the expansion process to Old,
  109. which is a CONJUNCTION of DISJUNCTIONS.
  110. */
  111.  
  112. singlestep([Conjunction | Rest], New) :-
  113. member(Formula, Conjunction),
  114. unary(Formula),
  115. component(Formula, Newformula),
  116. remove(Formula, Conjunction, Temporary),
  117. Newconjunction = [Newformula | Temporary],
  118. New = [Newconjunction | Rest].
  119.  
  120. singlestep([Conjunction | Rest], New) :-
  121. member(Alpha, Conjunction),
  122. conjunctive(Alpha),
  123. components(Alpha, Alphaone, Alphatwo),
  124. remove(Alpha, Conjunction, Temporary),
  125. Newconone = [Alphaone | Temporary],
  126. Newcontwo = [Alphatwo | Temporary],
  127. New = [Newconone, Newcontwo | Rest].
  128.  
  129. singlestep([Conjunction | Rest], New) :-
  130. member(Beta, Conjunction),
  131. disjunctive(Beta),
  132. components(Beta, Betaone, Betatwo),
  133. remove(Beta, Conjunction, Temporary),
  134. Newcon = [Betaone, Betatwo | Temporary],
  135. New = [Newcon | Rest].
  136.  
  137. singlestep([Conjunction | Rest], [Conjunction | Newrest]) :-
  138. singlestep(Rest, Newrest).
  139.  
  140. /* Resolution steps */
  141.  
  142. resolutionstep(Conjunction, New) :-
  143. member(Disone, Conjunction),
  144. member(X, Disone),
  145. remove(Disone, Conjunction, Newcon),
  146. remove(X, Disone, Disonenew),
  147.  
  148. member(Distwo, Newcon),
  149. member(neg X, Distwo),
  150. remove(Distwo, Newcon, Newercon),
  151. remove(neg X, Distwo, Distwonew),
  152.  
  153. New = [Disonenew, Distwonew | Newercon].
  154.  
  155. resolutionstep([Disjunction | Rest], New) :-
  156. member(false, Disjunction),
  157. remove(false, Disjunction, Temp),
  158. New = [Temp | Rest].
  159.  
  160. resolutionstep([Conjunction | Rest], [Conjunction | Newrest]) :-
  161. resolutionstep(Rest, Newrest).
  162.  
  163. /* Resolution initialiser. */
  164.  
  165. resolve(Con, Newcon) :-
  166. resolutionstep(Con, Temp),
  167. resolve(Temp, Newcon).
  168.  
  169. resolve(Con, Con).
  170.  
  171. /* expand(Old, New) :- New is the result of applying singlestep to Old, as many times as possible.
  172. */
  173.  
  174. expand(Con, Newcon) :-
  175. singlestep(Con, Temp),
  176. expand(Temp, Newcon).
  177.  
  178. expand(Con, Con).
  179.  
  180. /* clauseform(X, Y) :- Y is the CNF form of X.
  181. */
  182.  
  183. clauseform(X, Y) :- expand([[X]], Y).
  184.  
  185. /* Check a resolved conjunction for closure. */
  186.  
  187. closed(Con) :-
  188. member([], Con).
  189.  
  190. /* if_then_else(P, Q, R) :- either P and Q, or neg P and R. */
  191.  
  192. if_then_else(P, Q, R) :- P, !, Q.
  193. if_then_else(P, Q, R) :- R.
  194.  
  195. /* Test the input and give the appropriate output. */
  196.  
  197. yes :- write('YES'), nl.
  198. no :- write('NO'), nl.
  199.  
  200. test(X) :-
  201. expand([[neg X]], Y),
  202. resolve(Y, Z),
  203. if_then_else(closed(Z), yes, no).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement