Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- :- meta_predicate
- nop(0),
- call_w_detr(0,-),
- scce1(0,0,0),
- scce2(0,0,0),
- scce3(0,0,0),
- scce4(0,0,0).
- :- dynamic(scce0/0).
- % hide from optimizations (well as anything)
- call_w_detr(Goal,Det):- call((Goal,deterministic(Det),true)).
- scce1(Setup,Goal,Undo):-
- once(Setup),
- (call_w_detr(Goal,Det)
- *-> (Det == true -> once(Undo) ; (once(Undo);(once(Setup),fail)))
- ; (once(Undo),fail)).
- y11:- scce1(writeln(start),(between(1,3,X),between(1,X,Y),writeln(X-Y)), writeln(end)),fail. % Works
- y12:- scce1(asserta(scce0,REF),(between(1,3,X),between(1,X,Y),writeln(X-Y)),writeln(REF)),fail. % Broken
- /*
- ?- y11.
- start
- 1-1
- end
- start
- 2-1
- end
- start
- 2-2
- end
- start
- 3-1
- end
- start
- 3-2
- end
- start
- 3-3
- end
- false.
- ?- y12.
- 1-1
- <clause>(0x21a0970)
- ERROR: Uninstantiated argument expected, found <clause>(0x21a0970) (2-nd argument)
- ERROR: In:
- ERROR: [14] asserta(scce0,<clause>(0x21a0970))
- */
- % Try again
- scce2(Setup,Goal,Undo):-
- setup_call_cleanup(
- Setup,
- (call_w_detr(Goal,Det)
- *-> (Det == true -> true; (once(Undo);(once(Setup),fail)))
- ; fail),
- Undo).
- y21:- scce2(writeln(start),(between(1,3,X),between(1,X,Y),writeln(X-Y)), writeln(end)),fail. % Works
- y22:- scce2(asserta(scce0,REF),(between(1,3,X),between(1,X,Y),writeln(X-Y)),writeln(REF)),fail. % Broken
- /*
- ?- y21.
- start
- 1-1
- end
- start
- 2-1
- end
- start
- 2-2
- end
- start
- 3-1
- end
- start
- 3-2
- end
- start
- 3-3
- end
- end
- false.
- ?- y22.
- 1-1
- <clause>(0xd8e7b0)
- ERROR: Uninstantiated argument expected, found <clause>(0xd8e7b0) (2-nd argument)
- ERROR: In:
- ERROR: [17] asserta(scce0,<clause>(0xd8e7b0))
- */
- % Douglas Try
- scce3(Setup0,Goal0,Undo0):-
- Setup = call(throw(sss)),
- Goal = call(throw(ggg)),
- Undo = call(throw(uuu)),
- Start = (\+ \+ ((Setup0,nb_setarg(1,Setup,Setup0),nb_setarg(1,Goal,Goal0),nb_setarg(1,Undo,Undo0)))),
- End = (\+ \+ ((Undo0,nb_setarg(1,Setup,Setup0),nb_setarg(1,Goal,Goal0),nb_setarg(1,Undo,Undo0)))),
- scce1(Start,Goal,End).
- y31:- scce3(writeln(start),(between(1,3,X),between(1,X,Y),writeln(X-Y)), writeln(end)),fail. % Works
- y32:- scce3(asserta(scce0,REF),(between(1,3,X),between(1,X,Y),writeln(X-Y)),writeln(REF)),fail. % Broken
- /*
- ?- y31.
- start
- 1-1
- end
- start
- 2-1
- end
- start
- 2-2
- end
- start
- 3-1
- end
- start
- 3-2
- end
- start
- 3-3
- end
- false.
- ?- y32.
- 1-1
- _G1707
- 2-1
- _G1707
- 2-2
- _G1707
- 3-1
- _G1707
- 3-2
- _G1707
- 3-3
- _G1707
- false.
- */
- % skip a goal
- nop(_).
- % Douglas de-evolved
- scce4(Setup0,Goal0,Undo0):-
- Goal = (true,Goal0),
- Undo = call(nop,(Undo0)), % toggled between call/nop
- CallSetup = (Setup0,setarg(1,Undo,call)),
- CallGoal = (true,call_w_detr(Goal,Det)),
- CallUndo = (Undo,setarg(1,Undo,nop)),
- setup_call_cleanup(true,
- ( repeat,
- once(CallSetup),
- CallGoal,
- ( Det == true -> ! ; (once(CallUndo);(once(CallSetup),setarg(1,CallGoal,fail),fail)))),
- CallUndo).
- y41:- scce4(writeln(start),(between(1,3,X),between(1,X,Y),writeln(X-Y)), writeln(end)),fail. % Works
- y42:- scce4(asserta(scce0,REF),(between(1,3,X),between(1,X,Y),writeln(X-Y)),writeln(REF)),fail. % Broken
- /*
- ?- y41.
- start
- 1-1
- end
- start
- 2-1
- end
- start
- 2-2
- end
- start
- 3-1
- end
- start
- 3-2
- end
- start
- 3-3
- end
- false.
- ?- y42.
- 1-1
- <clause>(0xa36ed0)
- ERROR: Uninstantiated argument expected, found <clause>(0xa36ed0) (2-nd argument)
- ERROR: In:
- ERROR: [18] asserta(scce0,<clause>(0xa36ed0))
- */
- y52:- ASSERTA= asserta(scce0,REF),
- ERASE = writeln(REF),
- scce1(ASSERTA,
- (between(1,3,X),writeln(X:REF)),
- (ERASE,nb_setarg(2,ASSERTA,NewVar),nb_setarg(1,ERASE,NewVar))),
- fail.
- /*
- ?- y52.
- 1:<clause>(0x28bcc10)
- <clause>(0x28bcc10)
- 2:_G1707
- _G1707
- 3:_G1707
- _G1707
- false.
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement