2010-11-01 21:29:12 +00:00
|
|
|
|
|
|
|
:- module(meld_interpreter,
|
|
|
|
[
|
|
|
|
live/0,
|
|
|
|
delete/1,
|
|
|
|
push/1,
|
|
|
|
first/2,
|
|
|
|
min/3,
|
2010-11-03 00:33:26 +00:00
|
|
|
max/3,
|
|
|
|
minval/3,
|
|
|
|
maxval/3
|
2010-11-01 21:29:12 +00:00
|
|
|
]).
|
|
|
|
|
|
|
|
|
|
|
|
:- use_module(meldp,
|
|
|
|
[
|
|
|
|
run/1
|
|
|
|
]).
|
|
|
|
|
|
|
|
|
|
|
|
:- use_module(library(nb),
|
|
|
|
[
|
|
|
|
nb_queue/1,
|
|
|
|
nb_queue_enqueue/2,
|
|
|
|
nb_queue_dequeue/2
|
|
|
|
]).
|
|
|
|
|
|
|
|
|
|
|
|
:- initialization
|
|
|
|
init_meld_queue.
|
|
|
|
|
2010-11-03 16:16:40 +00:00
|
|
|
:- dynamic speculative_delete/3.
|
|
|
|
|
2010-11-01 21:29:12 +00:00
|
|
|
live :-
|
|
|
|
repeat,
|
|
|
|
( pop(Goal) ->
|
|
|
|
format('-~w~n',[Goal]),
|
|
|
|
run(Goal),
|
|
|
|
fail
|
|
|
|
;
|
|
|
|
!,
|
|
|
|
done
|
|
|
|
).
|
|
|
|
|
2010-11-03 16:16:40 +00:00
|
|
|
done :-
|
|
|
|
speculative_delete(_, _, _), !,
|
|
|
|
push_residuals,
|
|
|
|
live.
|
2010-11-01 21:29:12 +00:00
|
|
|
done :-
|
|
|
|
current_predicate(meld_program:P),
|
|
|
|
P \= run/1,
|
|
|
|
% P \= neighbor/2,
|
|
|
|
% P \= root/1,
|
|
|
|
listing(meld_program:P),
|
|
|
|
fail.
|
|
|
|
done.
|
|
|
|
|
|
|
|
|
|
|
|
delete(Fact) :-
|
|
|
|
nb_getval(meld_queue, Queue),
|
|
|
|
retract(meld_program:Fact),
|
2010-11-03 00:33:26 +00:00
|
|
|
nb_queue_enqueue(Queue, deleted(Fact)),
|
2010-11-01 21:29:12 +00:00
|
|
|
live.
|
|
|
|
|
|
|
|
pop(Goal) :-
|
|
|
|
nb_getval(meld_queue, Queue),
|
|
|
|
nb_queue_dequeue(Queue, Goal).
|
|
|
|
|
|
|
|
push(Goal) :-
|
|
|
|
clause(meld_program:Goal,_,Ref),
|
|
|
|
!,
|
|
|
|
increase_reference_count(Ref),
|
|
|
|
fail.
|
|
|
|
push(Goal) :-
|
2010-11-03 00:33:26 +00:00
|
|
|
format('+~w~n',[Goal]),
|
2010-11-01 21:29:12 +00:00
|
|
|
nb_getval(meld_queue, Queue), !,
|
|
|
|
assert(meld_program:Goal),
|
|
|
|
nb_queue_enqueue(Queue, Goal).
|
|
|
|
|
|
|
|
|
|
|
|
% create a queue
|
|
|
|
init_meld_queue :-
|
|
|
|
nb_queue(Queue),
|
|
|
|
nb_setval(meld_queue, Queue).
|
|
|
|
|
|
|
|
first(Skel,Goal) :-
|
|
|
|
meld_program:Skel, !,
|
|
|
|
cache(Goal).
|
|
|
|
first(_,Goal) :-
|
|
|
|
cache(Goal),
|
|
|
|
push(Goal).
|
|
|
|
|
|
|
|
max(Skel,Arg,Goal) :-
|
|
|
|
meld_program:Skel,
|
|
|
|
arg(Arg, Skel, A0),
|
|
|
|
arg(Arg, Goal, AN),
|
|
|
|
AN =< A0, !,
|
2011-02-03 11:33:50 +00:00
|
|
|
delete(Skel),
|
2010-11-01 21:29:12 +00:00
|
|
|
cache(Goal).
|
|
|
|
max(Skel,_,Goal) :-
|
|
|
|
clean(Skel),
|
|
|
|
cache(Goal),
|
|
|
|
push(Goal).
|
|
|
|
|
|
|
|
min(Skel,Arg,Goal) :-
|
|
|
|
meld_program:Skel,
|
|
|
|
arg(Arg, Skel, A0),
|
|
|
|
arg(Arg, Goal, AN),
|
|
|
|
AN >= A0, !,
|
2011-02-03 11:33:50 +00:00
|
|
|
delete(Skel),
|
2010-11-01 21:29:12 +00:00
|
|
|
cache(Goal).
|
|
|
|
min(Skel,_,Goal) :-
|
|
|
|
clean(Skel),
|
|
|
|
cache(Goal),
|
|
|
|
push(Goal).
|
|
|
|
|
|
|
|
clean(Skel) :-
|
|
|
|
% format('D~w~n',[Skel]),
|
|
|
|
retractall(meld_program:Skel).
|
|
|
|
|
|
|
|
cache(Goal) :-
|
|
|
|
clause(meld_cache:Goal,_,Ref),
|
|
|
|
!,
|
|
|
|
increase_reference_count(Ref).
|
|
|
|
cache(Goal) :-
|
|
|
|
assert(meld_cache:Goal).
|
|
|
|
|
|
|
|
|
|
|
|
deleted(Goal) :-
|
|
|
|
clause(meld_program:Goal,_,Ref),
|
|
|
|
decrease_reference_count(Ref),
|
|
|
|
!,
|
2010-11-03 16:16:40 +00:00
|
|
|
force_delete(Goal, Ref),
|
2010-11-03 00:33:26 +00:00
|
|
|
complete_delete(Goal).
|
2010-11-03 16:16:40 +00:00
|
|
|
deleted(Goal) :-
|
|
|
|
retract(speculative_delete(Goal, Ref, Count)), !,
|
|
|
|
NCount is Count-1,
|
|
|
|
(
|
|
|
|
NCount > 0
|
|
|
|
->
|
|
|
|
assert(speculative_delete(Goal, Ref, Count))
|
|
|
|
;
|
|
|
|
true
|
|
|
|
).
|
2010-11-01 21:29:12 +00:00
|
|
|
deleted(Goal) :-
|
|
|
|
% format('-~w~n',[Goal]),
|
2010-11-03 00:33:26 +00:00
|
|
|
complete_delete(Goal).
|
|
|
|
|
|
|
|
complete_delete(Goal) :-
|
2010-11-01 21:29:12 +00:00
|
|
|
nb_getval(meld_queue, Queue), !,
|
|
|
|
retract(meld_program:Goal),
|
|
|
|
nb_queue_enqueue(Queue, deleted(Goal)).
|
|
|
|
|
2010-11-03 16:16:40 +00:00
|
|
|
force_delete(Goal, Ref) :-
|
|
|
|
current_reference_count(Ref, Count),
|
|
|
|
assert(speculative_delete(Goal, Ref, Count)).
|
|
|
|
|
|
|
|
push_residuals :-
|
|
|
|
retract(speculative_delete(Goal, _, _)),
|
|
|
|
push(Goal),
|
|
|
|
fail.
|
|
|
|
push_residuals.
|
|
|
|
|
|
|
|
|
2010-11-01 21:29:12 +00:00
|
|
|
%
|
|
|
|
% first, cleanup cache
|
|
|
|
%
|
|
|
|
delete_from_first(_,Goal) :-
|
2010-11-03 16:16:40 +00:00
|
|
|
clause(meld_program:Goal,_,Ref), !,
|
2010-11-01 21:29:12 +00:00
|
|
|
(
|
2010-11-03 16:16:40 +00:00
|
|
|
decrease_reference_count(Ref)
|
2010-11-01 21:29:12 +00:00
|
|
|
->
|
2010-11-03 16:16:40 +00:00
|
|
|
true
|
2010-11-01 21:29:12 +00:00
|
|
|
;
|
2010-11-03 16:16:40 +00:00
|
|
|
force_delete(Goal, Ref)
|
|
|
|
),
|
|
|
|
erase(Ref),
|
|
|
|
retract(meld_cache:Goal),
|
2010-11-01 21:29:12 +00:00
|
|
|
retract(meld_program:Goal),
|
|
|
|
push(deleted(Goal)),
|
|
|
|
once(meld_cache:VGoal),
|
|
|
|
push(VGoal).
|
2010-11-03 16:16:40 +00:00
|
|
|
delete_from_first(Goal) :-
|
|
|
|
retract(speculative_delete(Goal, Ref, Count)), !,
|
|
|
|
NCount is Count-1,
|
|
|
|
(
|
|
|
|
NCount > 0
|
|
|
|
->
|
|
|
|
assert(speculative_delete(Goal, Ref, Count))
|
|
|
|
;
|
|
|
|
true
|
|
|
|
).
|
|
|
|
delete_from_first(Goal) :-
|
|
|
|
retract(meld_cache:Goal),
|
|
|
|
push(deleted(Goal)).
|
2010-11-01 21:29:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
delete_from_max(VGoal,Arg,Goal) :-
|
2010-11-03 16:16:40 +00:00
|
|
|
clause(meld_program:Goal,_,Ref), !,
|
2010-11-01 21:29:12 +00:00
|
|
|
(
|
2010-11-03 16:16:40 +00:00
|
|
|
decrease_reference_count(Ref)
|
|
|
|
->
|
|
|
|
true
|
|
|
|
;
|
|
|
|
force_delete(Goal, Ref)
|
|
|
|
),
|
|
|
|
erase(Ref),
|
|
|
|
retract(meld_cache:Goal),
|
2010-11-01 21:29:12 +00:00
|
|
|
push(deleted(Goal)),
|
2010-11-03 16:16:40 +00:00
|
|
|
new_max(VGoal, Arg).
|
|
|
|
delete_from_max(Goal) :-
|
|
|
|
retract(speculative_delete(Goal, Ref, Count)), !,
|
|
|
|
NCount is Count-1,
|
|
|
|
(
|
|
|
|
NCount > 0
|
|
|
|
->
|
|
|
|
assert(speculative_delete(Goal, Ref, Count))
|
|
|
|
;
|
|
|
|
true
|
|
|
|
).
|
|
|
|
delete_from_max(Goal) :-
|
|
|
|
retract(meld_cache:Goal),
|
|
|
|
push(deleted(Goal)).
|
|
|
|
|
|
|
|
new_max(VGoal,Arg) :-
|
|
|
|
arg(Arg, VGoal, A),
|
|
|
|
maxval(A, meld_cache:VGoal, VGoal),
|
2010-11-01 21:29:12 +00:00
|
|
|
push(VGoal).
|
|
|
|
|
|
|
|
delete_from_min(VGoal,Arg,Goal) :-
|
2010-11-03 16:16:40 +00:00
|
|
|
clause(meld_program:Goal,_,Ref), !,
|
2010-11-01 21:29:12 +00:00
|
|
|
(
|
2010-11-03 16:16:40 +00:00
|
|
|
decrease_reference_count(Ref)
|
2010-11-01 21:29:12 +00:00
|
|
|
->
|
2010-11-03 16:16:40 +00:00
|
|
|
true
|
|
|
|
;
|
|
|
|
force_delete(Goal, Ref)
|
|
|
|
),
|
|
|
|
erase(Ref),
|
|
|
|
retract(meld_cache:Goal),
|
2010-11-01 21:29:12 +00:00
|
|
|
push(deleted(Goal)),
|
2010-11-03 16:16:40 +00:00
|
|
|
new_min(VGoal, Arg).
|
|
|
|
delete_from_min(Goal) :- !,
|
|
|
|
retract(speculative_delete(Goal, Ref, Count)),
|
|
|
|
NCount is Count-1,
|
|
|
|
(
|
|
|
|
NCount > 0
|
|
|
|
->
|
|
|
|
assert(speculative_delete(Goal, Ref, Count))
|
|
|
|
;
|
|
|
|
true
|
|
|
|
).
|
|
|
|
delete_from_min(Goal) :-
|
|
|
|
retract(meld_cache:Goal),
|
|
|
|
push(deleted(Goal)).
|
|
|
|
|
|
|
|
new_min(VGoal,Arg) :-
|
|
|
|
arg(Arg, VGoal, A),
|
|
|
|
minval(A, meld_cache:VGoal, VGoal),
|
2010-11-01 21:29:12 +00:00
|
|
|
push(VGoal).
|
|
|
|
|
2010-11-03 00:33:26 +00:00
|
|
|
:- meta_predicate minval(+,:,-), maxval(+,:,-).
|
|
|
|
|
2010-11-01 21:29:12 +00:00
|
|
|
maxval(V,G,GMax) :-
|
2010-11-03 09:45:18 +00:00
|
|
|
Memory = f(-inf,[]),
|
|
|
|
(
|
|
|
|
call(G),
|
|
|
|
arg(1, Memory, V0),
|
|
|
|
V > V0,
|
|
|
|
nb_setarg(1, Memory, V),
|
2010-11-03 16:16:40 +00:00
|
|
|
nb_setarg(2, Memory, V.GMax),
|
|
|
|
fail
|
2010-11-03 09:45:18 +00:00
|
|
|
;
|
|
|
|
arg(2, Memory, V.GMax)
|
|
|
|
).
|
2010-11-01 21:29:12 +00:00
|
|
|
|
2010-11-03 09:45:18 +00:00
|
|
|
minval(V,G,GMin) :-
|
|
|
|
Memory = f(+inf,[]),
|
|
|
|
(
|
|
|
|
call(G),
|
|
|
|
arg(1, Memory, V0),
|
|
|
|
V < V0,
|
|
|
|
nb_setarg(1, Memory, V),
|
2010-11-03 16:16:40 +00:00
|
|
|
nb_setarg(2, Memory, V.GMin),
|
|
|
|
fail
|
2010-11-03 09:45:18 +00:00
|
|
|
;
|
|
|
|
arg(2, Memory, V.GMin)
|
|
|
|
).
|