- Matching is now typed.

This commit is contained in:
ccremers 2004-08-15 16:08:53 +00:00
parent c7e290197c
commit c3d5123ab0
5 changed files with 99 additions and 36 deletions

View File

@ -7,6 +7,7 @@
#include "binding.h" #include "binding.h"
#include "warshall.h" #include "warshall.h"
#include "memory.h" #include "memory.h"
#include "debug.h"
/* /*
* Idea is the ev_from *has to* precede the ev_to * Idea is the ev_from *has to* precede the ev_to
@ -203,7 +204,13 @@ binding_add (int run_from, int ev_from, int run_to, int ev_to)
Binding b; Binding b;
b = binding_create (run_from, ev_from, run_to, ev_to); b = binding_create (run_from, ev_from, run_to, ev_to);
eprintf ("Adding binding (%i,%i) --->> (%i,%i)\n",run_from, ev_from, run_to, ev_to); #ifdef DEBUG
if (DEBUGL (5))
{
eprintf ("Adding binding (%i,%i) --->> (%i,%i)\n", run_from, ev_from,
run_to, ev_to);
}
#endif
sys->bindings = list_insert (sys->bindings, b); sys->bindings = list_insert (sys->bindings, b);
/* /*

View File

@ -701,8 +701,11 @@ protocolCompile (Symbol prots, Tac tc, Tac tcroles)
{ {
if (sys->engine == ARACHNE_ENGINE) if (sys->engine == ARACHNE_ENGINE)
{ {
pr->rolenames = Term rolename;
termlistAppend (pr->rolenames, levelVar (tcroles->t1.sym));
rolename = levelVar (tcroles->t1.sym);
rolename->stype = termlistAdd (NULL, TERM_Agent);
pr->rolenames = termlistAppend (pr->rolenames, rolename);
} }
else else
{ {

112
src/mgu.c
View File

@ -14,6 +14,85 @@
New version yields a termlist with substituted variables, which can later be reset to NULL. New version yields a termlist with substituted variables, which can later be reset to NULL.
*/ */
//! Global constant. If true, typed checking
int welltyped = 1;
void
showSubst (Term t)
{
#ifdef DEBUG
if (!DEBUGL (5))
return;
indent ();
printf ("Substituting ");
termPrint (t);
printf (", typed ");
termlistPrint (t->stype);
if (realTermLeaf (t->subst))
{
printf ("->");
termlistPrint (t->subst->stype);
}
else
{
printf (", composite term");
}
if (t->type != VARIABLE)
{
printf (" (bound roleconstant)");
}
printf ("\n");
#endif
}
//! See if a substitution is valid
__inline__ int
goodsubst (Term tvar, Term tsubst)
{
if (tvar->stype == NULL || (!welltyped))
{
return 1;
}
else
{
/**
* Check if each type of the substitution is allowed in the variable
*/
if (!realTermLeaf (tsubst))
{
// Typed var cannot match with non-leaf
return 0;
}
else
{
// It's a leaf, but what type?
if (termlistContained (tvar->stype, tsubst->stype))
{
return 1;
}
else
{
#ifdef DEBUG
if (DEBUGL (5))
{
eprintf ("Substitution fails on ");
termPrint (tvar);
eprintf (" -/-> ");
termPrint (tsubst);
eprintf (", because type: \n");
termlistPrint (tvar->stype);
eprintf (" does not contain ");
termlistPrint (tsubst->stype);
eprintf ("\n");
}
#endif
return 0;
}
}
}
}
//! Undo all substitutions in a list of variables. //! Undo all substitutions in a list of variables.
/** /**
* The termlist should contain only variables. * The termlist should contain only variables.
@ -43,34 +122,6 @@ termMguTerm (Term t1, Term t2)
if (t1 == t2) if (t1 == t2)
return NULL; return NULL;
#ifdef DEBUG
void showSubst (Term t)
{
if (!DEBUGL (5))
return;
indent ();
printf ("Substituting ");
termPrint (t);
printf (", typed ");
termlistPrint (t->stype);
if (realTermLeaf (t->subst))
{
printf ("->");
termlistPrint (t->subst->stype);
}
else
{
printf (", composite term");
}
if (t->type != VARIABLE)
{
printf (" (bound roleconstant)");
}
printf ("\n");
}
#endif
if (!(hasTermVariable (t1) || hasTermVariable (t2))) if (!(hasTermVariable (t1) || hasTermVariable (t2)))
{ {
if (isTermEqual (t1, t2)) if (isTermEqual (t1, t2))
@ -86,7 +137,7 @@ termMguTerm (Term t1, Term t2)
/* symmetrical tests for single variable */ /* symmetrical tests for single variable */
if (realTermVariable (t1)) if (realTermVariable (t1))
{ {
if (termOccurs (t2, t1)) if (termOccurs (t2, t1) || !goodsubst (t1, t2))
return MGUFAIL; return MGUFAIL;
else else
{ {
@ -99,7 +150,7 @@ termMguTerm (Term t1, Term t2)
} }
if (realTermVariable (t2)) if (realTermVariable (t2))
{ {
if (termOccurs (t1, t2)) if (termOccurs (t1, t2) || !goodsubst (t2, t1))
return MGUFAIL; return MGUFAIL;
else else
{ {
@ -265,4 +316,3 @@ termMguSubTerm (Term t1, Term t2, int (*iterator) (),
} }
return flag; return flag;
} }

View File

@ -597,6 +597,7 @@ roleInstance (const System sys, const Protocol protocol, const Role role,
// Make new var for this run // Make new var for this run
newt = makeTermType (VARIABLE, newt->left.symb, rid); newt = makeTermType (VARIABLE, newt->left.symb, rid);
artefacts = termlistAdd (artefacts, newt); artefacts = termlistAdd (artefacts, newt);
newt->stype = oldt->stype;
// Copy substitution // Copy substitution
newt->subst = oldt->subst; newt->subst = oldt->subst;
// Remove any old substitution! It is now propagated! // Remove any old substitution! It is now propagated!
@ -604,7 +605,9 @@ roleInstance (const System sys, const Protocol protocol, const Role role,
} }
// Add to agent list, possibly // Add to agent list, possibly
if (inTermlist (protocol->rolenames, oldt)) if (inTermlist (protocol->rolenames, oldt))
runs[rid].agents = termlistAdd (runs[rid].agents, newt); {
runs[rid].agents = termlistAdd (runs[rid].agents, newt);
}
fromlist = termlistAdd (fromlist, oldt); fromlist = termlistAdd (fromlist, oldt);
tolist = termlistAdd (tolist, newt); tolist = termlistAdd (tolist, newt);

View File

@ -297,7 +297,7 @@ termPrint (Term term)
return; return;
} }
#ifdef DEBUG #ifdef DEBUG
if (!DEBUGL (1)) if (!DEBUGL (4))
{ {
term = deVar (term); term = deVar (term);
} }