From 602d9dc32e7dd66552691f0e7d55a44861e9a180 Mon Sep 17 00:00:00 2001 From: Cas Cremers Date: Mon, 24 Dec 2018 14:29:02 +0100 Subject: [PATCH] Adapting subtermUnify to have state. --- src/arachne.c | 9 +++++---- src/mgu.c | 18 ++++++++++++------ src/mgu.h | 2 +- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/arachne.c b/src/arachne.c index 6e824ac..cbcc816 100644 --- a/src/arachne.c +++ b/src/arachne.c @@ -1012,7 +1012,7 @@ bind_existing_to_goal (const Binding b, const int run, const int index, { Term bigterm; - int unifiesWithKeys (Termlist substlist, Termlist keylist) + int unifiesWithKeys (Termlist substlist, Termlist keylist, void *state) { int old_length; int newgoals; @@ -1086,7 +1086,7 @@ bind_existing_to_goal (const Binding b, const int run, const int index, } bigterm = roledef_shift (sys->runs[run].start, index)->message; - subtermUnify (bigterm, b->term, NULL, NULL, unifiesWithKeys); + subtermUnify (bigterm, b->term, NULL, NULL, unifiesWithKeys, NULL); } @@ -1451,7 +1451,7 @@ debug_send_candidate (const Protocol p, const Role r, const Roledef rd, //! Dummy helper function for iterator; abort if sub-unification found int -test_sub_unification (Termlist substlist, Termlist keylist) +test_sub_unification (Termlist substlist, Termlist keylist, void *state) { // A unification exists; return the signal return false; @@ -1555,7 +1555,8 @@ bind_this_role_send (Protocol p, Role r, Roledef rd, int index, debug_send_candidate (p, r, rd, index); if (!subtermUnify - (rd->message, (bs->binding)->term, NULL, NULL, test_sub_unification)) + (rd->message, (bs->binding)->term, NULL, NULL, test_sub_unification, + NULL)) { // A good candidate bs->found++; diff --git a/src/mgu.c b/src/mgu.c index ef8ccb2..a964a89 100644 --- a/src/mgu.c +++ b/src/mgu.c @@ -292,7 +292,8 @@ unify (Term t1, Term t2, Termlist tl, int (*callback) (), void *state) */ struct su_kcb_state { - int (*callback) (Termlist, Termlist); + void *oldstate; + int (*callback) (Termlist, Termlist, void *); Termlist keylist; }; @@ -300,7 +301,8 @@ int keycallback (Termlist tl, struct su_kcb_state *ptr_kcb_state) { assert (ptr_kcb_state != NULL); - return ptr_kcb_state->callback (tl, ptr_kcb_state->keylist); + return ptr_kcb_state->callback (tl, ptr_kcb_state->keylist, + ptr_kcb_state->oldstate); } //! Subterm unification @@ -320,11 +322,12 @@ keycallback (Termlist tl, struct su_kcb_state *ptr_kcb_state) */ int subtermUnify (Term tbig, Term tsmall, Termlist tl, Termlist keylist, - int (*callback) (Termlist, Termlist)) + int (*callback) (Termlist, Termlist, void *), void *state) { int proceed; struct su_kcb_state kcb_state; + kcb_state.oldstate = state; kcb_state.callback = callback; kcb_state.keylist = keylist; @@ -346,9 +349,11 @@ subtermUnify (Term tbig, Term tsmall, Termlist tl, Termlist keylist, if (realTermTuple (tbig)) { proceed = proceed - && subtermUnify (TermOp1 (tbig), tsmall, tl, keylist, callback); + && subtermUnify (TermOp1 (tbig), tsmall, tl, keylist, callback, + state); proceed = proceed - && subtermUnify (TermOp2 (tbig), tsmall, tl, keylist, callback); + && subtermUnify (TermOp2 (tbig), tsmall, tl, keylist, callback, + state); } // 3. unification with encryption needed @@ -357,7 +362,8 @@ subtermUnify (Term tbig, Term tsmall, Termlist tl, Termlist keylist, // extend the keylist keylist = termlistAdd (keylist, tbig); proceed = proceed - && subtermUnify (TermOp (tbig), tsmall, tl, keylist, callback); + && subtermUnify (TermOp (tbig), tsmall, tl, keylist, callback, + state); // remove last item again keylist = termlistDelTerm (keylist); } diff --git a/src/mgu.h b/src/mgu.h index f06c05e..2fc430b 100644 --- a/src/mgu.h +++ b/src/mgu.h @@ -39,6 +39,6 @@ int checkRoletermMatch (const Term t1, const Term t2, const Termlist tl); int unify (Term t1, Term t2, Termlist tl, int (*callback) (), void *state); int subtermUnify (Term tbig, Term tsmall, Termlist tl, Termlist keylist, - int (*callback) (Termlist, Termlist)); + int (*callback) (Termlist, Termlist, void *), void *state); #endif