Advertisement
logicmoo

Cant get any of the y?2s written?!

Apr 4th, 2016
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 3.80 KB | None | 0 0
  1. :- meta_predicate
  2.      nop(0),
  3.      call_w_detr(0,-),
  4.      scce1(0,0,0),
  5.      scce2(0,0,0),
  6.      scce3(0,0,0),
  7.      scce4(0,0,0).
  8.  
  9.  
  10.  
  11. :- dynamic(scce0/0).
  12.  
  13. % hide from optimizations (well as anything)
  14. call_w_detr(Goal,Det):- call((Goal,deterministic(Det),true)).
  15.  
  16.  
  17. scce1(Setup,Goal,Undo):-
  18.       once(Setup),
  19.       (call_w_detr(Goal,Det)
  20.         *-> (Det == true -> once(Undo) ; (once(Undo);(once(Setup),fail)))
  21.         ; (once(Undo),fail)).
  22.  
  23. y11:- scce1(writeln(start),(between(1,3,X),between(1,X,Y),writeln(X-Y)), writeln(end)),fail.  % Works
  24. y12:- scce1(asserta(scce0,REF),(between(1,3,X),between(1,X,Y),writeln(X-Y)),writeln(REF)),fail.  % Broken
  25.  
  26. /*
  27.  
  28. ?- y11.
  29. start
  30. 1-1
  31. end
  32. start
  33. 2-1
  34. end
  35. start
  36. 2-2
  37. end
  38. start
  39. 3-1
  40. end
  41. start
  42. 3-2
  43. end
  44. start
  45. 3-3
  46. end
  47. false.
  48.  
  49. ?- y12.
  50. 1-1
  51. <clause>(0x21a0970)
  52. ERROR: Uninstantiated argument expected, found <clause>(0x21a0970) (2-nd argument)
  53. ERROR: In:
  54. ERROR:   [14] asserta(scce0,<clause>(0x21a0970))
  55.  
  56. */
  57.  
  58.  
  59. % Try again
  60. scce2(Setup,Goal,Undo):-
  61.  setup_call_cleanup(
  62.     Setup,
  63.     (call_w_detr(Goal,Det)
  64.         *-> (Det == true -> true; (once(Undo);(once(Setup),fail)))
  65.         ; fail),
  66.     Undo).
  67.  
  68. y21:- scce2(writeln(start),(between(1,3,X),between(1,X,Y),writeln(X-Y)), writeln(end)),fail.  % Works
  69. y22:- scce2(asserta(scce0,REF),(between(1,3,X),between(1,X,Y),writeln(X-Y)),writeln(REF)),fail.  % Broken
  70.  
  71. /*
  72.  
  73. ?- y21.
  74. start
  75. 1-1
  76. end
  77. start
  78. 2-1
  79. end
  80. start
  81. 2-2
  82. end
  83. start
  84. 3-1
  85. end
  86. start
  87. 3-2
  88. end
  89. start
  90. 3-3
  91. end
  92.  
  93. end
  94. false.
  95.  
  96. ?- y22.
  97. 1-1
  98. <clause>(0xd8e7b0)
  99. ERROR: Uninstantiated argument expected, found <clause>(0xd8e7b0) (2-nd argument)
  100. ERROR: In:
  101. ERROR:   [17] asserta(scce0,<clause>(0xd8e7b0))
  102.  
  103. */
  104.  
  105.  
  106. % Douglas Try
  107. scce3(Setup0,Goal0,Undo0):-
  108.     Setup = call(throw(sss)),
  109.     Goal = call(throw(ggg)),
  110.     Undo = call(throw(uuu)),
  111.     Start = (\+ \+ ((Setup0,nb_setarg(1,Setup,Setup0),nb_setarg(1,Goal,Goal0),nb_setarg(1,Undo,Undo0)))),
  112.     End = (\+ \+ ((Undo0,nb_setarg(1,Setup,Setup0),nb_setarg(1,Goal,Goal0),nb_setarg(1,Undo,Undo0)))),
  113.     scce1(Start,Goal,End).
  114.  
  115. y31:- scce3(writeln(start),(between(1,3,X),between(1,X,Y),writeln(X-Y)), writeln(end)),fail.  % Works
  116. y32:- scce3(asserta(scce0,REF),(between(1,3,X),between(1,X,Y),writeln(X-Y)),writeln(REF)),fail.  % Broken
  117. /*
  118. ?- y31.
  119. start
  120. 1-1
  121. end
  122. start
  123. 2-1
  124. end
  125. start
  126. 2-2
  127. end
  128. start
  129. 3-1
  130. end
  131. start
  132. 3-2
  133. end
  134. start
  135. 3-3
  136. end
  137. false.
  138.  
  139. ?- y32.
  140. 1-1
  141. _G1707
  142. 2-1
  143. _G1707
  144. 2-2
  145. _G1707
  146. 3-1
  147. _G1707
  148. 3-2
  149. _G1707
  150. 3-3
  151. _G1707
  152.  
  153. false.
  154.  
  155. */
  156.  
  157.  
  158. % skip a goal
  159. nop(_).
  160.  
  161. % Douglas de-evolved
  162. scce4(Setup0,Goal0,Undo0):-
  163.  Goal = (true,Goal0),
  164.  Undo = call(nop,(Undo0)),  % toggled between call/nop
  165.  CallSetup = (Setup0,setarg(1,Undo,call)),
  166.  CallGoal = (true,call_w_detr(Goal,Det)),
  167.  CallUndo = (Undo,setarg(1,Undo,nop)),
  168.  setup_call_cleanup(true,
  169.     ( repeat,
  170.       once(CallSetup),
  171.       CallGoal,
  172.       ( Det == true -> ! ; (once(CallUndo);(once(CallSetup),setarg(1,CallGoal,fail),fail)))),
  173.     CallUndo).
  174.  
  175. y41:- scce4(writeln(start),(between(1,3,X),between(1,X,Y),writeln(X-Y)), writeln(end)),fail.  % Works
  176. y42:- scce4(asserta(scce0,REF),(between(1,3,X),between(1,X,Y),writeln(X-Y)),writeln(REF)),fail.  % Broken
  177.  
  178. /*
  179. ?- y41.
  180. start
  181. 1-1
  182. end
  183. start
  184. 2-1
  185. end
  186. start
  187. 2-2
  188. end
  189. start
  190. 3-1
  191. end
  192. start
  193. 3-2
  194. end
  195. start
  196. 3-3
  197. end
  198. false.
  199.  
  200. ?- y42.
  201. 1-1
  202. <clause>(0xa36ed0)
  203. ERROR: Uninstantiated argument expected, found <clause>(0xa36ed0) (2-nd argument)
  204. ERROR: In:
  205. ERROR:   [18] asserta(scce0,<clause>(0xa36ed0))
  206.  
  207. */
  208.  
  209.  
  210.  
  211. y52:- ASSERTA= asserta(scce0,REF),
  212.       ERASE = writeln(REF),
  213.       scce1(ASSERTA,
  214.             (between(1,3,X),writeln(X:REF)),
  215.             (ERASE,nb_setarg(2,ASSERTA,NewVar),nb_setarg(1,ERASE,NewVar))),
  216.      fail.
  217.  
  218. /*
  219. ?- y52.
  220. 1:<clause>(0x28bcc10)
  221. <clause>(0x28bcc10)
  222. 2:_G1707
  223. _G1707
  224. 3:_G1707
  225. _G1707
  226. false.
  227.  
  228. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement