2004-04-23 11:58:43 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2004-07-24 20:07:29 +01:00
|
|
|
#include "term.h"
|
|
|
|
#include "termlist.h"
|
|
|
|
#include "substitution.h"
|
2004-04-23 11:58:43 +01:00
|
|
|
#include "mgu.h"
|
|
|
|
#include "memory.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
Most General Unifier
|
|
|
|
|
|
|
|
Unification etc.
|
|
|
|
|
|
|
|
New version yields a termlist with substituted variables, which can later be reset to NULL.
|
|
|
|
*/
|
|
|
|
|
2004-08-15 17:08:53 +01:00
|
|
|
//! Global constant. If true, typed checking
|
2004-08-15 17:44:54 +01:00
|
|
|
/**
|
|
|
|
* Analoguous to sys->match
|
|
|
|
* 0 typed
|
|
|
|
* 1 basic typeflaws
|
|
|
|
* 2 all typeflaws
|
|
|
|
*/
|
|
|
|
int mgu_match = 0;
|
2004-08-15 17:08:53 +01:00
|
|
|
|
2004-08-20 16:09:49 +01:00
|
|
|
extern Term TERM_Hidden;
|
|
|
|
|
2004-08-15 17:08:53 +01:00
|
|
|
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)
|
|
|
|
{
|
2004-08-15 17:44:54 +01:00
|
|
|
if (tvar->stype == NULL || (mgu_match == 2))
|
2004-08-15 17:08:53 +01:00
|
|
|
{
|
|
|
|
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?
|
2004-08-15 17:44:54 +01:00
|
|
|
if (mgu_match == 1
|
|
|
|
|| termlistContained (tvar->stype, tsubst->stype))
|
2004-08-15 17:08:53 +01:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-15 17:43:20 +01:00
|
|
|
//! Undo all substitutions in a list of variables.
|
|
|
|
/**
|
|
|
|
* The termlist should contain only variables.
|
|
|
|
*/
|
2004-04-23 11:58:43 +01:00
|
|
|
void
|
|
|
|
termlistSubstReset (Termlist tl)
|
|
|
|
{
|
|
|
|
while (tl != NULL)
|
|
|
|
{
|
|
|
|
tl->term->subst = NULL;
|
|
|
|
tl = tl->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-15 17:43:20 +01:00
|
|
|
//! Most general unifier.
|
|
|
|
/**
|
|
|
|
* Try to determine the most general unifier of two terms.
|
2004-08-19 14:55:16 +01:00
|
|
|
* Resulting termlist must be termlistDelete'd.
|
|
|
|
*
|
2004-05-15 17:43:20 +01:00
|
|
|
*@return Returns a list of variables, that were previously open, but are now closed
|
|
|
|
* in such a way that the two terms unify. Returns \ref MGUFAIL if it is impossible.
|
|
|
|
*/
|
2004-04-23 11:58:43 +01:00
|
|
|
Termlist
|
|
|
|
termMguTerm (Term t1, Term t2)
|
|
|
|
{
|
|
|
|
/* added for speed */
|
|
|
|
t1 = deVar (t1);
|
|
|
|
t2 = deVar (t2);
|
|
|
|
if (t1 == t2)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!(hasTermVariable (t1) || hasTermVariable (t2)))
|
|
|
|
{
|
|
|
|
if (isTermEqual (t1, t2))
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return MGUFAIL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* symmetrical tests for single variable */
|
2004-08-16 10:50:37 +01:00
|
|
|
if (realTermVariable (t2))
|
2004-04-23 11:58:43 +01:00
|
|
|
{
|
2004-08-17 15:11:25 +01:00
|
|
|
if (termSubTerm (t1, t2) || !goodsubst (t2, t1))
|
2004-04-23 11:58:43 +01:00
|
|
|
return MGUFAIL;
|
|
|
|
else
|
|
|
|
{
|
2004-08-16 10:50:37 +01:00
|
|
|
t2->subst = t1;
|
2004-04-23 11:58:43 +01:00
|
|
|
#ifdef DEBUG
|
2004-08-16 10:50:37 +01:00
|
|
|
showSubst (t2);
|
2004-04-23 11:58:43 +01:00
|
|
|
#endif
|
2004-08-16 10:50:37 +01:00
|
|
|
return termlistAdd (NULL, t2);
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
}
|
2004-08-16 10:50:37 +01:00
|
|
|
if (realTermVariable (t1))
|
2004-04-23 11:58:43 +01:00
|
|
|
{
|
2004-08-17 15:11:25 +01:00
|
|
|
if (termSubTerm (t2, t1) || !goodsubst (t1, t2))
|
2004-04-23 11:58:43 +01:00
|
|
|
return MGUFAIL;
|
|
|
|
else
|
|
|
|
{
|
2004-08-16 10:50:37 +01:00
|
|
|
t1->subst = t2;
|
2004-04-23 11:58:43 +01:00
|
|
|
#ifdef DEBUG
|
2004-08-16 10:50:37 +01:00
|
|
|
showSubst (t1);
|
2004-04-23 11:58:43 +01:00
|
|
|
#endif
|
2004-08-16 10:50:37 +01:00
|
|
|
return termlistAdd (NULL, t1);
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* left & right are compounds with variables */
|
|
|
|
if (t1->type != t2->type)
|
|
|
|
return MGUFAIL;
|
|
|
|
|
|
|
|
/* identical compounds */
|
|
|
|
/* encryption first */
|
2004-08-13 21:09:12 +01:00
|
|
|
if (realTermEncrypt (t1))
|
2004-04-23 11:58:43 +01:00
|
|
|
{
|
|
|
|
Termlist tl1, tl2;
|
|
|
|
|
2004-05-26 09:40:33 +01:00
|
|
|
tl1 = termMguTerm (t1->right.key, t2->right.key);
|
2004-04-23 11:58:43 +01:00
|
|
|
if (tl1 == MGUFAIL)
|
|
|
|
{
|
|
|
|
return MGUFAIL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-05-26 09:40:33 +01:00
|
|
|
tl2 = termMguTerm (t1->left.op, t2->left.op);
|
2004-04-23 11:58:43 +01:00
|
|
|
if (tl2 == MGUFAIL)
|
|
|
|
{
|
|
|
|
termlistSubstReset (tl1);
|
|
|
|
termlistDelete (tl1);
|
|
|
|
return MGUFAIL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return termlistConcat (tl1, tl2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* tupling second
|
|
|
|
non-associative version ! TODO other version */
|
|
|
|
if (isTermTuple (t1))
|
|
|
|
{
|
|
|
|
Termlist tl1, tl2;
|
|
|
|
|
2004-05-26 09:40:33 +01:00
|
|
|
tl1 = termMguTerm (t1->left.op1, t2->left.op1);
|
2004-04-23 11:58:43 +01:00
|
|
|
if (tl1 == MGUFAIL)
|
|
|
|
{
|
|
|
|
return MGUFAIL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-05-26 09:40:33 +01:00
|
|
|
tl2 = termMguTerm (t1->right.op2, t2->right.op2);
|
2004-04-23 11:58:43 +01:00
|
|
|
if (tl2 == MGUFAIL)
|
|
|
|
{
|
|
|
|
termlistSubstReset (tl1);
|
|
|
|
termlistDelete (tl1);
|
|
|
|
return MGUFAIL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return termlistConcat (tl1, tl2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return MGUFAIL;
|
|
|
|
}
|
2004-08-09 22:22:24 +01:00
|
|
|
|
|
|
|
//! Most general interm unifiers of t1 interm t2
|
|
|
|
/**
|
|
|
|
* Try to determine the most general interm unifiers of two terms.
|
2004-08-11 15:09:12 +01:00
|
|
|
*@returns Nothing. Iteration gets termlist of substitutions.
|
2004-08-09 22:22:24 +01:00
|
|
|
*/
|
2004-08-11 15:09:12 +01:00
|
|
|
int
|
|
|
|
termMguInTerm (Term t1, Term t2, int (*iterator) ())
|
2004-08-09 22:22:24 +01:00
|
|
|
{
|
|
|
|
Termlist tl;
|
2004-08-11 15:09:12 +01:00
|
|
|
int flag;
|
2004-08-09 22:22:24 +01:00
|
|
|
|
2004-08-11 15:09:12 +01:00
|
|
|
flag = 1;
|
2004-08-09 22:22:24 +01:00
|
|
|
t2 = deVar (t2);
|
2004-08-13 14:25:25 +01:00
|
|
|
if (t2 != NULL)
|
2004-08-09 22:22:24 +01:00
|
|
|
{
|
2004-08-13 21:09:12 +01:00
|
|
|
if (realTermTuple (t2))
|
2004-08-13 14:25:25 +01:00
|
|
|
{
|
|
|
|
// t2 is a tuple, consider interm options as well.
|
|
|
|
flag = flag && termMguInTerm (t1, t2->left.op1, iterator);
|
|
|
|
flag = flag && termMguInTerm (t1, t2->right.op2, iterator);
|
|
|
|
}
|
|
|
|
// simple clause or combined
|
|
|
|
tl = termMguTerm (t1, t2);
|
|
|
|
if (tl != MGUFAIL)
|
|
|
|
{
|
|
|
|
// Iterate
|
|
|
|
flag = flag && iterator (tl);
|
|
|
|
// Reset variables
|
|
|
|
termlistSubstReset (tl);
|
2004-08-19 14:55:16 +01:00
|
|
|
// Remove list
|
|
|
|
termlistDelete (tl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (deVar (t1) != NULL)
|
|
|
|
{
|
|
|
|
flag = 0;
|
2004-08-13 14:25:25 +01:00
|
|
|
}
|
2004-08-10 12:26:14 +01:00
|
|
|
}
|
2004-08-11 15:09:12 +01:00
|
|
|
return flag;
|
2004-08-09 22:22:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//! Most general subterm unifiers of t1 subterm t2
|
|
|
|
/**
|
|
|
|
* Try to determine the most general subterm unifiers of two terms.
|
2004-08-11 15:09:12 +01:00
|
|
|
*@returns Nothing. Iteration gets termlist of subst, and list of keys needed to decrypt.
|
2004-08-09 22:22:24 +01:00
|
|
|
*/
|
2004-08-11 15:09:12 +01:00
|
|
|
int
|
|
|
|
termMguSubTerm (Term t1, Term t2, int (*iterator) (),
|
2004-08-13 11:50:56 +01:00
|
|
|
Termlist inverses, Termlist keylist)
|
2004-08-09 22:22:24 +01:00
|
|
|
{
|
2004-08-11 15:09:12 +01:00
|
|
|
int flag;
|
2004-08-09 22:22:24 +01:00
|
|
|
Termlist tl;
|
2004-08-11 15:09:12 +01:00
|
|
|
|
|
|
|
flag = 1;
|
2004-08-09 22:22:24 +01:00
|
|
|
t2 = deVar (t2);
|
2004-08-13 15:35:22 +01:00
|
|
|
if (t2 != NULL)
|
2004-08-09 22:22:24 +01:00
|
|
|
{
|
2004-08-13 21:09:12 +01:00
|
|
|
if (!realTermLeaf (t2))
|
2004-08-09 22:22:24 +01:00
|
|
|
{
|
2004-08-13 21:09:12 +01:00
|
|
|
if (realTermTuple (t2))
|
2004-08-13 15:35:22 +01:00
|
|
|
{
|
|
|
|
// 'simple' tuple
|
|
|
|
flag =
|
|
|
|
flag && termMguSubTerm (t1, t2->left.op1, iterator, inverses,
|
|
|
|
keylist);
|
|
|
|
flag =
|
|
|
|
flag && termMguSubTerm (t1, t2->right.op2, iterator, inverses,
|
|
|
|
keylist);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Must be encryption
|
|
|
|
// So, we need the key, and try to get the rest
|
|
|
|
Termlist keylist_new;
|
|
|
|
Term newkey;
|
|
|
|
|
|
|
|
newkey = inverseKey (inverses, t2->right.key);
|
|
|
|
|
2004-08-20 16:09:49 +01:00
|
|
|
// We can never produce the TERM_Hidden key, thus, this is not a valid iteration.
|
|
|
|
if (!isTermEqual (newkey, TERM_Hidden))
|
|
|
|
{
|
|
|
|
keylist_new = termlistShallow (keylist);
|
|
|
|
keylist_new = termlistAdd (keylist_new, newkey);
|
2004-08-13 15:35:22 +01:00
|
|
|
|
2004-08-20 16:09:49 +01:00
|
|
|
// Recurse
|
|
|
|
flag =
|
|
|
|
flag && termMguSubTerm (t1, t2->left.op, iterator, inverses,
|
|
|
|
keylist_new);
|
|
|
|
|
|
|
|
termlistDelete (keylist_new);
|
|
|
|
}
|
2004-08-13 15:35:22 +01:00
|
|
|
termDelete (newkey);
|
|
|
|
}
|
2004-08-09 22:22:24 +01:00
|
|
|
}
|
2004-08-13 15:35:22 +01:00
|
|
|
// simple clause or combined
|
|
|
|
tl = termMguTerm (t1, t2);
|
|
|
|
if (tl != MGUFAIL)
|
2004-08-09 22:22:24 +01:00
|
|
|
{
|
2004-08-13 15:35:22 +01:00
|
|
|
// Iterate
|
|
|
|
flag = flag && iterator (tl, keylist);
|
|
|
|
// Reset variables
|
|
|
|
termlistSubstReset (tl);
|
2004-08-19 14:55:16 +01:00
|
|
|
// Remove list
|
|
|
|
termlistDelete (tl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (deVar (t1) != NULL)
|
|
|
|
{
|
|
|
|
flag = 0;
|
2004-08-09 22:22:24 +01:00
|
|
|
}
|
|
|
|
}
|
2004-08-11 15:09:12 +01:00
|
|
|
return flag;
|
2004-08-09 22:22:24 +01:00
|
|
|
}
|