From 7122c657c0e0e2d11fe1a8720cae00b8c80f8047 Mon Sep 17 00:00:00 2001 From: Vitor Santos Costa Date: Wed, 11 May 2011 09:28:40 +0100 Subject: [PATCH 1/2] more updates to meld. --- packages/meld/examples/pagerank/l4.meld | 7 ++++ packages/meld/examples/pagerank/pagerank.meld | 23 ++++++++--- packages/meld/meldc.yap | 8 ++-- packages/meld/meldi.yap | 41 +++++++++++++------ packages/meld/meldp.yap | 1 + 5 files changed, 58 insertions(+), 22 deletions(-) create mode 100644 packages/meld/examples/pagerank/l4.meld diff --git a/packages/meld/examples/pagerank/l4.meld b/packages/meld/examples/pagerank/l4.meld new file mode 100644 index 000000000..e727e038c --- /dev/null +++ b/packages/meld/examples/pagerank/l4.meld @@ -0,0 +1,7 @@ +%edge(0,1). +%edge(0,4). +%edge(1,4). +%edge(1,2). +edge(2,3). +edge(2,4). +edge(3,4). diff --git a/packages/meld/examples/pagerank/pagerank.meld b/packages/meld/examples/pagerank/pagerank.meld index 0e27b90b0..3167955ec 100644 --- a/packages/meld/examples/pagerank/pagerank.meld +++ b/packages/meld/examples/pagerank/pagerank.meld @@ -1,20 +1,24 @@ type rank(node, int, float). +type reachable(node, node). type calcRank(node, int, sum float). -type persistent numPages(node, int). +% type persistent numPages(node, int). +type persistent numPages(node, sum int). type numLinks(node, sum int). +type path(node, node). const damping = 0.85. const num_iterations = 4. % extern float to_float(int). % extern float float_abs(float). -rank(A, 0, 1.0 / to_float(T)) :- numPages(A, T). +rank(A, 0, 1.0 / to_float(T)) :- numPages(A,T). rank(A, I, V) :- + numPages(A, Ps), calcRank(A, I, T), Before = I - 1, rank(A, Before, VOld), - V = damping + (1.0 - damping) * T, + V = (damping + (1.0 - damping) * T)/to_float(Ps), I =< num_iterations. % //float_abs((damping + (1.0 - damping) * T) - VOld) > 0.001. @@ -27,8 +31,17 @@ calcRank(A, I + 1, O / to_float(C)) :- numLinks(A, 1) :- edge(A,B). -numPages(A, 1) :- +numPages(A, 1) :- path(A,_). + +path(A, B) :- + edge(A,B). +path(A, B) :- edge(B,A). - +path(A, B) :- + edge(A,C), + path(C,B). +path(A, B) :- + edge(C,A), + path(C,B). diff --git a/packages/meld/meldc.yap b/packages/meld/meldc.yap index 11b74cc01..a4228e0d3 100644 --- a/packages/meld/meldc.yap +++ b/packages/meld/meldc.yap @@ -186,8 +186,8 @@ compile_goal((forall G then Do), Goals, Gs, Head) :- !, listtobody(BLF2, Body), listtobody(DelBLF2, DelBody), % done - assert(meld_program:(run(NG) :- Body)), - assert(meld_program:(run(delete(DelNG)) :- DelBody)). + assert_static(meld_program:(run(NG) :- Body)), + assert_static(meld_program:(run(delete(DelNG)) :- DelBody)). compile_goal(Goal, Goals, Gs, Head) :- collect_body(Gs, Goals, BLF, BL1), copy_term(h(Head,BLF,BL1,Goal), h(Head,DelBLF,DelBL1,DelGoal)), @@ -198,8 +198,8 @@ compile_goal(Goal, Goals, Gs, Head) :- reorder_builtins(DelGoal, DelBLF, DelBLF2), listtobody(BLF2, Body), listtobody(DelBLF2, DelBody), - assert(meld_program:(run(Goal) :- Body)), - assert(meld_program:(run(deleted(DelGoal)) :- DelBody)). + assert_static(meld_program:(run(Goal) :- Body)), + assert_static(meld_program:(run(deleted(DelGoal)) :- DelBody)). % quantified variables should not leave the scope of the forall. quantified_vars(G,Extern,NG) :- diff --git a/packages/meld/meldi.yap b/packages/meld/meldi.yap index 5f4d75d55..d1ff1799c 100644 --- a/packages/meld/meldi.yap +++ b/packages/meld/meldi.yap @@ -23,7 +23,8 @@ [ nb_queue/1, nb_queue_enqueue/2, - nb_queue_dequeue/2 + nb_queue_dequeue/2, + nb_queue_replace/3 ]). @@ -35,7 +36,7 @@ live :- repeat, ( pop(Goal) -> - format('-~w~n',[Goal]), +% format('<- ~w~n',[Goal]), run(Goal), fail ; @@ -50,6 +51,7 @@ done :- done :- current_predicate(meld_program:P), P \= run/1, + P \= trace/2, % P \= neighbor/2, % P \= root/1, listing(meld_program:P), @@ -58,9 +60,18 @@ done. delete(Fact) :- + once(retract(meld_program:Fact)), nb_getval(meld_queue, Queue), - retract(meld_program:Fact), - nb_queue_enqueue(Queue, deleted(Fact)). + ( +% nb:nb_queue_show(Queue, L ), writeln(show:Fact:L), + nb_queue_replace(Queue, Fact, [] ), +% format('R ~w~n', [Fact]), + deleted(Fact) + -> + true + ; + nb_queue_enqueue(Queue, deleted(Fact)) + ). pop(Goal) :- nb_getval(meld_queue, Queue), @@ -72,10 +83,12 @@ push(Goal) :- increase_reference_count(Ref), fail. push(Goal) :- - format('+~w~n',[Goal]), +% format('-> ~w~n',[Goal]), nb_getval(meld_queue, Queue), !, assert(meld_program:Goal), - nb_queue_enqueue(Queue, Goal). + nb_queue_enqueue(Queue, Goal), +% nb:nb_queue_show(Queue, L ), writeln(show:Goal:L), + fail. % create a queue @@ -118,16 +131,17 @@ sum(Skel,Arg,Goal) :- copy_term(Skel, NGoal), meld_program:Skel, !, arg(Arg, Skel, A0), - delete(Skel), arg(Arg, Goal, A), AN is A0+A, + AN \= A0, + delete(Skel), arg(Arg, NGoal, AN), - format('S ~w~n',[Goal]), +% format('S ~w~n',[NGoal-Skel]), push(NGoal). sum(_Skel,_Arg,Goal) :- - format('S ~w~n',[Goal]), - assert(meld_program:Goal), - fail. +% format('S ~w~n',[Goal]), + push(Goal). + clean(Skel) :- % format('D~w~n',[Skel]), @@ -239,11 +253,12 @@ delete_from_max(Goal) :- delete_from_sum(Skel,Arg,Goal) :- copy_term(Skel, NGoal), - meld_program:Skel, + once(meld_program:Skel), arg(Arg, Skel, A0), - delete(Skel), arg(Arg, Goal, A), AN is A0-A, + AN \= A0, + delete(Skel), arg(Arg, NGoal, AN), push(NGoal). diff --git a/packages/meld/meldp.yap b/packages/meld/meldp.yap index 1dc4ba72f..5c7e02011 100644 --- a/packages/meld/meldp.yap +++ b/packages/meld/meldp.yap @@ -21,3 +21,4 @@ trace(A,B) :- !, trace. +run([]) :- fail. From 2064e304ac11caf573142b50f88fb7488873736f Mon Sep 17 00:00:00 2001 From: Vitor Santos Costa Date: Wed, 11 May 2011 09:29:07 +0100 Subject: [PATCH 2/2] new library to replace elements in queue --- C/globals.c | 37 +++++++++++++++++++++++++++++++++++++ library/nb.yap | 1 + 2 files changed, 38 insertions(+) diff --git a/C/globals.c b/C/globals.c index b5eebee55..d54861c40 100644 --- a/C/globals.c +++ b/C/globals.c @@ -1677,6 +1677,31 @@ p_nb_queue_dequeue( USES_REGS1 ) return Yap_unify(out, ARG2); } +/* purge an entry from the queue, replacing it by [] */ +static Int +p_nb_queue_replace( USES_REGS1 ) +{ + CELL *qd = GetQueue(ARG1,"dequeue"); + UInt qsz; + Term queue, t = Deref(ARG2); + + if (!qd) + return FALSE; + qsz = IntegerOfTerm(qd[QUEUE_SIZE]); + if (qsz == 0) + return FALSE; + + queue = qd[QUEUE_HEAD]; + for (; qsz > 0; qsz--) { + if (Yap_eq(HeadOfTerm(queue), t)) { + *RepPair(Deref(queue)) = Deref(ARG3); + return TRUE; + } + queue = TailOfTerm(queue); + } + return FALSE; +} + static Int p_nb_queue_peek( USES_REGS1 ) { @@ -1711,6 +1736,16 @@ p_nb_queue_size( USES_REGS1 ) return Yap_unify(ARG2,qd[QUEUE_SIZE]); } +static Int +p_nb_queue_show( USES_REGS1 ) +{ + CELL *qd = GetQueue(ARG1,"queue_size"); + + if (!qd) + return FALSE; + return Yap_unify(ARG2,qd[QUEUE_HEAD]); +} + static CELL * GetHeap(Term t, char* caller) @@ -2562,7 +2597,9 @@ void Yap_InitGlobals(void) Yap_InitCPred("nb_queue_dequeue", 2, p_nb_queue_dequeue, SafePredFlag); Yap_InitCPred("nb_queue_peek", 2, p_nb_queue_peek, SafePredFlag); Yap_InitCPred("nb_queue_empty", 1, p_nb_queue_empty, SafePredFlag); + Yap_InitCPred("nb_queue_replace", 3, p_nb_queue_replace, SafePredFlag); Yap_InitCPred("nb_queue_size", 2, p_nb_queue_size, SafePredFlag); + Yap_InitCPred("nb_queue_show", 2, p_nb_queue_show, SafePredFlag); Yap_InitCPred("nb_heap", 2, p_nb_heap, 0L); Yap_InitCPred("nb_heap_close", 1, p_nb_heap_close, SafePredFlag); Yap_InitCPred("nb_heap_add", 3, p_nb_heap_add_to_heap, 0L); diff --git a/library/nb.yap b/library/nb.yap index ae3d8889d..308a43927 100644 --- a/library/nb.yap +++ b/library/nb.yap @@ -24,6 +24,7 @@ nb_queue_peek/2, nb_queue_empty/1, nb_queue_size/2, + nb_queue_replace/3, nb_heap/2, nb_heap_close/1, nb_heap_add/3,