2006-01-02 21:06:08 +00:00
|
|
|
/** @file term.c \brief Term related base functions.
|
2004-07-20 13:21:01 +01:00
|
|
|
*
|
|
|
|
* Intended to be a standalone file, however during development it turned out
|
|
|
|
* that a termlist structure was needed to define term types, so there is now a
|
|
|
|
* dependency loop with termlists.c.
|
2004-05-15 13:39:49 +01:00
|
|
|
*
|
2004-07-20 13:21:01 +01:00
|
|
|
* Until now, symbols were unique and never deleted. The same holds for basic
|
|
|
|
* terms; leaves are equal when their pointers are equal. We are looking to
|
|
|
|
* extend this to whole terms. At that point, term equality is be reduced to
|
2006-01-02 21:06:08 +00:00
|
|
|
* pointer comparison, which is what we want.
|
2004-05-15 13:39:49 +01:00
|
|
|
*/
|
2004-07-20 13:21:01 +01:00
|
|
|
|
2004-04-23 11:58:43 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <limits.h>
|
2006-03-08 13:58:46 +00:00
|
|
|
#include <string.h>
|
2004-07-24 20:07:29 +01:00
|
|
|
#include "term.h"
|
2004-04-23 11:58:43 +01:00
|
|
|
#include "debug.h"
|
2007-01-06 14:45:29 +00:00
|
|
|
#include "error.h"
|
2004-04-23 11:58:43 +01:00
|
|
|
#include "ctype.h"
|
2005-06-16 15:10:07 +01:00
|
|
|
#include "specialterm.h"
|
2004-04-23 11:58:43 +01:00
|
|
|
|
2004-08-14 15:23:21 +01:00
|
|
|
/* public flag */
|
|
|
|
int rolelocal_variable;
|
2006-03-08 15:12:58 +00:00
|
|
|
char *RUNSEP;
|
2004-08-14 15:23:21 +01:00
|
|
|
|
2004-04-23 11:58:43 +01:00
|
|
|
/* external definitions */
|
|
|
|
|
|
|
|
extern int inTermlist (); // suppresses a warning, but at what cost?
|
|
|
|
|
|
|
|
/* forward declarations */
|
|
|
|
|
|
|
|
void indent (void);
|
|
|
|
|
|
|
|
/* useful macros */
|
|
|
|
|
2006-01-02 21:06:08 +00:00
|
|
|
//! Undefined run identifier in a term
|
2004-04-23 11:58:43 +01:00
|
|
|
#define RID_UNDEF MIN_INT
|
|
|
|
/* main code */
|
|
|
|
|
|
|
|
/* Two types of terms: general, and normalized. Normalized rewrites all
|
|
|
|
tuples to (x,(y,z))..NULL form, making list traversal easy. */
|
|
|
|
|
2004-05-15 13:33:01 +01:00
|
|
|
//! Initialization of terms code.
|
2004-04-23 11:58:43 +01:00
|
|
|
void
|
|
|
|
termsInit (void)
|
|
|
|
{
|
2004-08-14 15:23:21 +01:00
|
|
|
rolelocal_variable = 0;
|
2006-03-08 15:12:58 +00:00
|
|
|
RUNSEP = "#";
|
2004-04-23 11:58:43 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-05-15 13:33:01 +01:00
|
|
|
//! Cleanup of terms code.
|
2004-04-23 11:58:43 +01:00
|
|
|
void
|
|
|
|
termsDone (void)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-05-15 13:33:01 +01:00
|
|
|
//! Allocate memory for a term.
|
|
|
|
/**
|
|
|
|
*@return A pointer to the new term memory, which is not yet initialised.
|
|
|
|
*/
|
2004-04-23 11:58:43 +01:00
|
|
|
Term
|
|
|
|
makeTerm ()
|
|
|
|
{
|
2006-03-08 13:58:46 +00:00
|
|
|
return (Term) malloc (sizeof (struct term));
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
|
2004-05-15 13:33:01 +01:00
|
|
|
//! Create a fresh encrypted term from two existing terms.
|
|
|
|
/**
|
|
|
|
*@return A pointer to the new term.
|
|
|
|
*/
|
2004-04-23 11:58:43 +01:00
|
|
|
Term
|
|
|
|
makeTermEncrypt (Term t1, Term t2)
|
|
|
|
{
|
|
|
|
Term term = makeTerm ();
|
|
|
|
term->type = ENCRYPT;
|
|
|
|
term->stype = NULL;
|
2004-11-16 12:07:55 +00:00
|
|
|
TermOp (term) = t1;
|
|
|
|
TermKey (term) = t2;
|
2004-04-23 11:58:43 +01:00
|
|
|
return term;
|
|
|
|
}
|
|
|
|
|
2004-05-15 13:33:01 +01:00
|
|
|
//! Create a fresh term tuple from two existing terms.
|
|
|
|
/**
|
|
|
|
*@return A pointer to the new term.
|
|
|
|
*/
|
2004-04-23 11:58:43 +01:00
|
|
|
Term
|
|
|
|
makeTermTuple (Term t1, Term t2)
|
|
|
|
{
|
2004-05-26 09:52:15 +01:00
|
|
|
Term tt;
|
|
|
|
|
2004-04-23 11:58:43 +01:00
|
|
|
if (t1 == NULL)
|
|
|
|
{
|
|
|
|
if (t2 == NULL)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
debug (5, "Trying to make a tuple node with an empty term.");
|
|
|
|
#endif
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else
|
2004-05-26 13:17:09 +01:00
|
|
|
{
|
|
|
|
return t2;
|
|
|
|
}
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
if (t2 == NULL)
|
|
|
|
{
|
|
|
|
return t1;
|
|
|
|
}
|
|
|
|
|
2004-05-26 09:52:15 +01:00
|
|
|
tt = makeTerm ();
|
2004-04-23 11:58:43 +01:00
|
|
|
tt->type = TUPLE;
|
|
|
|
tt->stype = NULL;
|
2004-12-29 16:05:32 +00:00
|
|
|
tt->roleVar = 0;
|
2004-11-16 12:07:55 +00:00
|
|
|
TermOp1 (tt) = t1;
|
|
|
|
TermOp2 (tt) = t2;
|
2004-04-23 11:58:43 +01:00
|
|
|
return tt;
|
|
|
|
}
|
|
|
|
|
2004-05-15 13:33:01 +01:00
|
|
|
//! Make a term of the given type with run identifier and symbol.
|
|
|
|
/**
|
|
|
|
*@return A pointer to the new term.
|
|
|
|
*\sa GLOBAL, VARIABLE, LEAF, ENCRYPT, TUPLE
|
|
|
|
*/
|
2004-04-23 11:58:43 +01:00
|
|
|
Term
|
|
|
|
makeTermType (const int type, const Symbol symb, const int runid)
|
|
|
|
{
|
|
|
|
Term term = makeTerm ();
|
|
|
|
term->type = type;
|
|
|
|
term->stype = NULL;
|
2004-12-29 16:05:32 +00:00
|
|
|
term->roleVar = 0;
|
2004-04-23 11:58:43 +01:00
|
|
|
term->subst = NULL;
|
2004-11-16 12:07:55 +00:00
|
|
|
TermSymb (term) = symb;
|
|
|
|
TermRunid (term) = runid;
|
2004-04-23 11:58:43 +01:00
|
|
|
return term;
|
|
|
|
}
|
|
|
|
|
2004-05-15 13:33:01 +01:00
|
|
|
//! Unwrap any substitutions.
|
|
|
|
/**
|
|
|
|
* For speed, it is also a macro. Sometimes it will call
|
2004-04-23 11:58:43 +01:00
|
|
|
* deVarScan to do the actual unwinding.
|
2004-05-15 13:33:01 +01:00
|
|
|
*@return A term that is either not a variable, or has a NULL substitution.
|
|
|
|
*\sa deVar()
|
2004-04-23 11:58:43 +01:00
|
|
|
*/
|
2004-07-20 13:41:56 +01:00
|
|
|
__inline__ Term
|
2004-04-23 11:58:43 +01:00
|
|
|
deVarScan (Term t)
|
|
|
|
{
|
|
|
|
while (realTermVariable (t) && t->subst != NULL)
|
|
|
|
t = t->subst;
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2004-05-15 13:33:01 +01:00
|
|
|
//! Determine whether a term contains an unsubstituted variable as subterm.
|
|
|
|
/**
|
|
|
|
*@return True iff there is an open variable as subterm.
|
|
|
|
*/
|
2004-04-23 11:58:43 +01:00
|
|
|
int
|
|
|
|
hasTermVariable (Term term)
|
|
|
|
{
|
|
|
|
if (term == NULL)
|
|
|
|
return 0;
|
|
|
|
term = deVar (term);
|
|
|
|
if (realTermLeaf (term))
|
|
|
|
return realTermVariable (term);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (realTermTuple (term))
|
2004-11-16 12:07:55 +00:00
|
|
|
return (hasTermVariable (TermOp1 (term))
|
|
|
|
|| hasTermVariable (TermOp2 (term)));
|
2004-04-23 11:58:43 +01:00
|
|
|
else
|
2004-11-16 12:07:55 +00:00
|
|
|
return (hasTermVariable (TermOp (term))
|
|
|
|
|| hasTermVariable (TermKey (term)));
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-07-21 15:26:28 +01:00
|
|
|
//! Safe wrapper for isTermEqual
|
|
|
|
|
2004-08-09 11:05:58 +01:00
|
|
|
int
|
|
|
|
isTermEqualDebug (Term t1, Term t2)
|
2004-07-20 13:21:01 +01:00
|
|
|
{
|
2004-08-27 11:08:03 +01:00
|
|
|
return isTermEqualFn (t1, t2);
|
2004-07-20 13:21:01 +01:00
|
|
|
}
|
2004-04-23 11:58:43 +01:00
|
|
|
|
2004-05-15 13:33:01 +01:00
|
|
|
//!Tests whether two terms are completely identical.
|
|
|
|
/**
|
|
|
|
* This also includes
|
|
|
|
* variables. This is the recursive function.
|
|
|
|
* We assume the term is normalized, e.g. no tupling has direct
|
|
|
|
* subtupling.
|
|
|
|
*@return True iff the terms are equal.
|
|
|
|
*\sa isTermEqual()
|
|
|
|
*/
|
2004-04-23 11:58:43 +01:00
|
|
|
|
|
|
|
int
|
|
|
|
isTermEqualFn (Term term1, Term term2)
|
|
|
|
{
|
|
|
|
term1 = deVar (term1);
|
|
|
|
term2 = deVar (term2);
|
|
|
|
|
|
|
|
if (term1 == term2)
|
|
|
|
return 1;
|
|
|
|
if ((term1 == NULL) || (term2 == NULL))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (term1->type != term2->type)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (realTermLeaf (term1))
|
|
|
|
{
|
2004-11-16 12:07:55 +00:00
|
|
|
return (TermSymb (term1) == TermSymb (term2)
|
|
|
|
&& TermRunid (term1) == TermRunid (term2));
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* ENCRYPT or TUPLE */
|
|
|
|
|
|
|
|
if (realTermEncrypt (term1))
|
|
|
|
{
|
|
|
|
/* for optimization of encryption equality, we compare
|
|
|
|
operator 2 first (we expect it to be a smaller term)
|
|
|
|
*/
|
2004-11-16 12:07:55 +00:00
|
|
|
return (isTermEqualFn (TermKey (term1), TermKey (term2)) &&
|
|
|
|
isTermEqualFn (TermOp (term1), TermOp (term2)));
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* tuple */
|
|
|
|
|
2004-11-16 12:07:55 +00:00
|
|
|
return (isTermEqualFn (TermOp1 (term1), TermOp1 (term2)) &&
|
|
|
|
isTermEqualFn (TermOp2 (term1), TermOp2 (term2)));
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-15 13:33:01 +01:00
|
|
|
//! See if a term is a subterm of another.
|
|
|
|
/**
|
|
|
|
*@param t Term to be checked for a subterm.
|
|
|
|
*@param tsub Subterm.
|
2005-08-11 13:56:36 +01:00
|
|
|
* Note that if t is non-null and tsub is null, it is a valid subterm.
|
2004-05-15 13:33:01 +01:00
|
|
|
*@return True iff tsub is a subterm of t.
|
|
|
|
*/
|
2004-04-23 11:58:43 +01:00
|
|
|
int
|
2004-08-17 15:11:25 +01:00
|
|
|
termSubTerm (Term t, Term tsub)
|
2004-04-23 11:58:43 +01:00
|
|
|
{
|
|
|
|
t = deVar (t);
|
|
|
|
tsub = deVar (tsub);
|
|
|
|
|
|
|
|
if (isTermEqual (t, tsub))
|
2005-08-11 13:56:36 +01:00
|
|
|
{
|
2006-03-05 15:18:39 +00:00
|
|
|
return true;
|
2005-08-11 13:56:36 +01:00
|
|
|
}
|
2004-04-23 11:58:43 +01:00
|
|
|
else
|
2005-08-11 13:56:36 +01:00
|
|
|
{
|
|
|
|
if (t == NULL)
|
|
|
|
{
|
2006-03-05 15:18:39 +00:00
|
|
|
return false;
|
2005-08-11 13:56:36 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (tsub == NULL)
|
|
|
|
{
|
2006-03-05 15:18:39 +00:00
|
|
|
return true;
|
2005-08-11 13:56:36 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (realTermLeaf (t))
|
|
|
|
{
|
2006-03-05 15:18:39 +00:00
|
|
|
return false;
|
2005-08-11 13:56:36 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (realTermTuple (t))
|
|
|
|
return (termSubTerm (TermOp1 (t), tsub)
|
|
|
|
|| termSubTerm (TermOp2 (t), tsub));
|
|
|
|
else
|
|
|
|
return (termSubTerm (TermOp (t), tsub)
|
|
|
|
|| termSubTerm (TermKey (t), tsub));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-08-17 15:11:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//! See if a term is an interm of another.
|
|
|
|
/**
|
|
|
|
*@param t Term to be checked for a subterm.
|
|
|
|
*@param tsub interm.
|
|
|
|
*@return True iff tsub is an interm of t.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
termInTerm (Term t, Term tsub)
|
|
|
|
{
|
|
|
|
t = deVar (t);
|
|
|
|
tsub = deVar (tsub);
|
|
|
|
|
|
|
|
if (isTermEqual (t, tsub))
|
2006-03-05 15:18:39 +00:00
|
|
|
return true;
|
2004-08-17 15:11:25 +01:00
|
|
|
if (realTermLeaf (t))
|
2006-03-05 15:18:39 +00:00
|
|
|
return false;
|
2004-08-17 15:11:25 +01:00
|
|
|
if (realTermTuple (t))
|
2004-11-16 12:07:55 +00:00
|
|
|
return (termInTerm (TermOp1 (t), tsub) || termInTerm (TermOp2 (t), tsub));
|
2004-08-17 15:11:25 +01:00
|
|
|
else
|
2006-03-05 15:18:39 +00:00
|
|
|
return false;
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
|
2004-05-15 13:33:01 +01:00
|
|
|
//! Print a term to stdout.
|
2004-05-19 09:39:39 +01:00
|
|
|
/**
|
|
|
|
* The tuple printing only works correctly for normalized terms.
|
|
|
|
* If not, they might are displayed as "((x,y),z)". Maybe that is even
|
|
|
|
* desirable to distinguish them.
|
|
|
|
*\sa termTuplePrint()
|
|
|
|
*/
|
2004-04-23 11:58:43 +01:00
|
|
|
void
|
2006-03-15 21:30:19 +00:00
|
|
|
termPrintCustom (Term term, char *leftvar, char *rightvar, char *lefttup,
|
|
|
|
char *righttup, char *leftenc, char *rightenc,
|
2006-12-05 10:10:17 +00:00
|
|
|
void (*callback) (const Term t))
|
2004-04-23 11:58:43 +01:00
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
2004-08-15 17:08:53 +01:00
|
|
|
if (!DEBUGL (4))
|
2004-04-23 11:58:43 +01:00
|
|
|
{
|
|
|
|
term = deVar (term);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
term = deVar (term);
|
|
|
|
#endif
|
2006-05-16 16:00:21 +01:00
|
|
|
if (term == NULL)
|
|
|
|
{
|
|
|
|
eprintf ("*");
|
|
|
|
return;
|
|
|
|
}
|
2004-04-23 11:58:43 +01:00
|
|
|
if (realTermLeaf (term))
|
|
|
|
{
|
2006-03-15 21:30:19 +00:00
|
|
|
if (term->type == VARIABLE && TermRunid (term) >= 0)
|
|
|
|
eprintf (leftvar);
|
2004-11-16 12:07:55 +00:00
|
|
|
symbolPrint (TermSymb (term));
|
2005-11-12 21:16:02 +00:00
|
|
|
if (term->type == VARIABLE && TermRunid (term) >= 0)
|
2006-03-15 21:30:19 +00:00
|
|
|
eprintf (rightvar);
|
2004-11-16 12:07:55 +00:00
|
|
|
if (TermRunid (term) >= 0)
|
2004-04-23 11:58:43 +01:00
|
|
|
{
|
2006-03-15 21:30:19 +00:00
|
|
|
if (callback == NULL)
|
|
|
|
{
|
|
|
|
eprintf ("%s%i", RUNSEP, TermRunid (term));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-12-05 10:10:17 +00:00
|
|
|
callback (term);
|
2006-03-15 21:30:19 +00:00
|
|
|
}
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
if (term->subst != NULL)
|
|
|
|
{
|
2006-03-08 13:58:46 +00:00
|
|
|
eprintf ("->");
|
2006-03-15 21:30:19 +00:00
|
|
|
termPrintCustom (term->subst, leftvar, rightvar, lefttup, righttup,
|
|
|
|
leftenc, rightenc, callback);
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (realTermTuple (term))
|
|
|
|
{
|
2004-07-29 13:36:24 +01:00
|
|
|
eprintf ("(");
|
2006-03-15 21:30:19 +00:00
|
|
|
termTuplePrintCustom (term, leftvar, rightvar, lefttup, righttup,
|
|
|
|
leftenc, rightenc, callback);
|
2004-07-29 13:36:24 +01:00
|
|
|
eprintf (")");
|
2004-04-23 11:58:43 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (realTermEncrypt (term))
|
|
|
|
{
|
2004-11-16 12:07:55 +00:00
|
|
|
if (isTermLeaf (TermKey (term))
|
|
|
|
&& inTermlist (TermKey (term)->stype, TERM_Function))
|
2004-04-23 11:58:43 +01:00
|
|
|
{
|
|
|
|
/* function application */
|
2006-03-15 21:30:19 +00:00
|
|
|
termPrintCustom (TermKey (term), leftvar, rightvar, lefttup,
|
|
|
|
righttup, leftenc, rightenc, callback);
|
|
|
|
eprintf (lefttup);
|
|
|
|
termTuplePrintCustom (TermOp (term), leftvar, rightvar, lefttup,
|
|
|
|
righttup, leftenc, rightenc, callback);
|
|
|
|
eprintf (righttup);
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* normal encryption */
|
2006-03-15 21:30:19 +00:00
|
|
|
eprintf (leftenc);
|
|
|
|
termTuplePrintCustom (TermOp (term), leftvar, rightvar, lefttup,
|
|
|
|
righttup, leftenc, rightenc, callback);
|
|
|
|
eprintf (rightenc);
|
|
|
|
termPrintCustom (TermKey (term), leftvar, rightvar, lefttup,
|
|
|
|
righttup, leftenc, rightenc, callback);
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-03-15 21:30:19 +00:00
|
|
|
void
|
|
|
|
termPrint (Term term)
|
|
|
|
{
|
|
|
|
termPrintCustom (term, "", "V", "(", ")", "{ ", " }", NULL);
|
|
|
|
}
|
|
|
|
|
2004-05-19 09:39:39 +01:00
|
|
|
//! Print an inner (tuple) term to stdout, without brackets.
|
|
|
|
/**
|
|
|
|
* The tuple printing only works correctly for normalized terms.
|
|
|
|
* If not, they might are displayed as "((x,y),z)". Maybe that is even
|
|
|
|
* desirable to distinguish them.
|
|
|
|
*/
|
|
|
|
void
|
2006-03-15 21:30:19 +00:00
|
|
|
termTuplePrintCustom (Term term, char *leftvar, char *rightvar, char *lefttup,
|
|
|
|
char *righttup, char *leftenc, char *rightenc,
|
2006-12-05 10:10:17 +00:00
|
|
|
void (*callback) (const Term t))
|
2004-05-19 09:39:39 +01:00
|
|
|
{
|
|
|
|
if (term == NULL)
|
|
|
|
{
|
2004-07-29 13:36:24 +01:00
|
|
|
eprintf ("Empty term");
|
2004-05-19 09:39:39 +01:00
|
|
|
return;
|
|
|
|
}
|
2004-08-09 11:05:58 +01:00
|
|
|
term = deVar (term);
|
2004-05-19 09:39:39 +01:00
|
|
|
while (realTermTuple (term))
|
|
|
|
{
|
|
|
|
// To remove any brackets, change this into termTuplePrint.
|
2006-03-15 21:30:19 +00:00
|
|
|
termPrintCustom (TermOp1 (term), leftvar, rightvar, lefttup, righttup,
|
|
|
|
leftenc, rightenc, callback);
|
2004-07-29 13:36:24 +01:00
|
|
|
eprintf (",");
|
2004-11-16 12:07:55 +00:00
|
|
|
term = deVar (TermOp2 (term));
|
2004-05-19 09:39:39 +01:00
|
|
|
}
|
2006-03-15 21:30:19 +00:00
|
|
|
termPrintCustom (term, leftvar, rightvar, lefttup, righttup, leftenc,
|
|
|
|
rightenc, callback);
|
2004-05-19 09:39:39 +01:00
|
|
|
return;
|
|
|
|
}
|
2004-04-23 11:58:43 +01:00
|
|
|
|
2006-03-15 21:30:19 +00:00
|
|
|
//! Print inner tuple
|
|
|
|
void
|
|
|
|
termTuplePrint (Term term)
|
|
|
|
{
|
|
|
|
termTuplePrintCustom (term, "", "V", "(", ")", "{ ", " }", NULL);
|
|
|
|
}
|
|
|
|
|
2004-05-15 13:33:01 +01:00
|
|
|
//! Make a deep copy of a term.
|
|
|
|
/**
|
|
|
|
* Leaves are not copied.
|
|
|
|
*@return If the original was a leaf, then the pointer is simply returned. Otherwise, new memory is allocated and the node is copied recursively.
|
|
|
|
*\sa termDuplicateDeep()
|
|
|
|
*/
|
2004-04-23 11:58:43 +01:00
|
|
|
|
|
|
|
Term
|
|
|
|
termDuplicate (const Term term)
|
|
|
|
{
|
|
|
|
Term newterm;
|
|
|
|
|
|
|
|
if (term == NULL)
|
|
|
|
return NULL;
|
|
|
|
if (realTermLeaf (term))
|
|
|
|
return term;
|
|
|
|
|
2006-03-08 13:58:46 +00:00
|
|
|
newterm = (Term) malloc (sizeof (struct term));
|
2004-08-19 15:49:03 +01:00
|
|
|
memcpy (newterm, term, sizeof (struct term));
|
2004-04-23 11:58:43 +01:00
|
|
|
if (realTermEncrypt (term))
|
|
|
|
{
|
2004-11-16 12:07:55 +00:00
|
|
|
TermOp (newterm) = termDuplicate (TermOp (term));
|
|
|
|
TermKey (newterm) = termDuplicate (TermKey (term));
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-11-16 12:07:55 +00:00
|
|
|
TermOp1 (newterm) = termDuplicate (TermOp1 (term));
|
|
|
|
TermOp2 (newterm) = termDuplicate (TermOp2 (term));
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
return newterm;
|
|
|
|
}
|
|
|
|
|
2004-10-12 16:12:20 +01:00
|
|
|
//! Make a deep copy of a term node (one-level)
|
|
|
|
/**
|
|
|
|
* Leaves are not copied.
|
|
|
|
*@return If the original was a leaf, then the pointer is simply returned. Otherwise, new memory is allocated and the node is copied recursively.
|
|
|
|
*\sa termDuplicateDeep()
|
|
|
|
*/
|
|
|
|
|
|
|
|
Term
|
|
|
|
termNodeDuplicate (const Term term)
|
|
|
|
{
|
|
|
|
Term newterm;
|
|
|
|
|
|
|
|
if (term == NULL)
|
|
|
|
return NULL;
|
|
|
|
if (realTermLeaf (term))
|
|
|
|
return term;
|
|
|
|
|
2006-03-08 13:58:46 +00:00
|
|
|
newterm = (Term) malloc (sizeof (struct term));
|
2004-10-12 16:12:20 +01:00
|
|
|
memcpy (newterm, term, sizeof (struct term));
|
|
|
|
return newterm;
|
|
|
|
}
|
|
|
|
|
2004-05-15 13:33:01 +01:00
|
|
|
//! Make a true deep copy of a term.
|
|
|
|
/**
|
2005-11-12 21:13:00 +00:00
|
|
|
* Currently, this function is not to be used, so we can be sure leaf nodes occur only once in the system.
|
2004-05-15 13:33:01 +01:00
|
|
|
*@return New memory is allocated and the node is copied recursively.
|
|
|
|
*\sa termDuplicate()
|
|
|
|
*/
|
2004-04-23 11:58:43 +01:00
|
|
|
|
|
|
|
|
|
|
|
Term
|
|
|
|
termDuplicateDeep (const Term term)
|
|
|
|
{
|
|
|
|
Term newterm;
|
|
|
|
|
|
|
|
if (term == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2006-03-08 13:58:46 +00:00
|
|
|
newterm = (Term) malloc (sizeof (struct term));
|
2004-08-19 15:49:03 +01:00
|
|
|
memcpy (newterm, term, sizeof (struct term));
|
|
|
|
if (!realTermLeaf (term))
|
2004-04-23 11:58:43 +01:00
|
|
|
{
|
|
|
|
if (realTermEncrypt (term))
|
|
|
|
{
|
2004-11-16 12:07:55 +00:00
|
|
|
TermOp (newterm) = termDuplicateDeep (TermOp (term));
|
|
|
|
TermKey (newterm) = termDuplicateDeep (TermKey (term));
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-11-16 12:07:55 +00:00
|
|
|
TermOp1 (newterm) = termDuplicateDeep (TermOp1 (term));
|
|
|
|
TermOp2 (newterm) = termDuplicateDeep (TermOp2 (term));
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return newterm;
|
|
|
|
}
|
|
|
|
|
2004-05-15 13:33:01 +01:00
|
|
|
//! Make a copy of a term, but remove substituted variable nodes.
|
|
|
|
/**
|
2004-04-23 11:58:43 +01:00
|
|
|
* Remove all instantiated variables on the way down.
|
2004-05-15 13:33:01 +01:00
|
|
|
*\sa termDuplicate()
|
2004-04-23 11:58:43 +01:00
|
|
|
*/
|
|
|
|
Term
|
|
|
|
termDuplicateUV (Term term)
|
|
|
|
{
|
|
|
|
Term newterm;
|
|
|
|
|
2004-08-19 15:49:03 +01:00
|
|
|
term = deVar (term);
|
2004-04-23 11:58:43 +01:00
|
|
|
if (term == NULL)
|
|
|
|
return NULL;
|
|
|
|
if (realTermLeaf (term))
|
|
|
|
return term;
|
|
|
|
|
2006-03-08 13:58:46 +00:00
|
|
|
newterm = (Term) malloc (sizeof (struct term));
|
2004-08-19 15:49:03 +01:00
|
|
|
memcpy (newterm, term, sizeof (struct term));
|
2004-04-23 11:58:43 +01:00
|
|
|
if (realTermEncrypt (term))
|
|
|
|
{
|
2004-11-16 12:07:55 +00:00
|
|
|
TermOp (newterm) = termDuplicateUV (TermOp (term));
|
|
|
|
TermKey (newterm) = termDuplicateUV (TermKey (term));
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-11-16 12:07:55 +00:00
|
|
|
TermOp1 (newterm) = termDuplicateUV (TermOp1 (term));
|
|
|
|
TermOp2 (newterm) = termDuplicateUV (TermOp2 (term));
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
return newterm;
|
|
|
|
}
|
|
|
|
|
2006-01-02 21:06:08 +00:00
|
|
|
//! Make a deep copy of a term, also of leaves
|
2004-04-23 11:58:43 +01:00
|
|
|
Term
|
|
|
|
realTermDuplicate (const Term term)
|
|
|
|
{
|
|
|
|
Term newterm;
|
|
|
|
|
|
|
|
if (term == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2006-03-08 13:58:46 +00:00
|
|
|
newterm = (Term) malloc (sizeof (struct term));
|
2004-04-23 11:58:43 +01:00
|
|
|
if (realTermLeaf (term))
|
|
|
|
{
|
|
|
|
memcpy (newterm, term, sizeof (struct term));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
newterm->type = term->type;
|
|
|
|
if (realTermEncrypt (term))
|
|
|
|
{
|
2004-11-16 12:07:55 +00:00
|
|
|
TermOp (newterm) = realTermDuplicate (TermOp (term));
|
|
|
|
TermKey (newterm) = realTermDuplicate (TermKey (term));
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-11-16 12:07:55 +00:00
|
|
|
TermOp1 (newterm) = realTermDuplicate (TermOp1 (term));
|
|
|
|
TermOp2 (newterm) = realTermDuplicate (TermOp2 (term));
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return newterm;
|
|
|
|
}
|
|
|
|
|
2004-05-15 13:33:01 +01:00
|
|
|
//!Removes a term and deallocates memory.
|
|
|
|
/**
|
|
|
|
* Is meant to remove terms make with termDuplicate. Only deallocates memory
|
|
|
|
* of nodes, not of leaves.
|
|
|
|
*\sa termDuplicate(), termDuplicateUV()
|
|
|
|
*/
|
2004-04-23 11:58:43 +01:00
|
|
|
|
|
|
|
void
|
|
|
|
termDelete (const Term term)
|
|
|
|
{
|
|
|
|
if (term != NULL && !realTermLeaf (term))
|
|
|
|
{
|
|
|
|
if (realTermEncrypt (term))
|
|
|
|
{
|
2004-11-16 12:07:55 +00:00
|
|
|
termDelete (TermOp (term));
|
|
|
|
termDelete (TermKey (term));
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-11-16 12:07:55 +00:00
|
|
|
termDelete (TermOp1 (term));
|
|
|
|
termDelete (TermOp2 (term));
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
2006-03-08 13:58:46 +00:00
|
|
|
free (term);
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-15 13:33:01 +01:00
|
|
|
//! Normalize a term with respect to tupling.
|
|
|
|
/**
|
|
|
|
* Avoids problems with associativity by rewriting every ((x,y),z) to
|
|
|
|
* (x,(y,z)), i.e. a normal form for terms, after which equality is
|
|
|
|
* okay. No memory was allocated or deallocated, as only pointers are swapped.
|
|
|
|
*
|
|
|
|
*@return After execution, the term pointed at has been normalized. */
|
2004-04-23 11:58:43 +01:00
|
|
|
|
|
|
|
void
|
|
|
|
termNormalize (Term term)
|
|
|
|
{
|
|
|
|
term = deVar (term);
|
|
|
|
if (term == NULL || realTermLeaf (term))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (realTermEncrypt (term))
|
|
|
|
{
|
2004-11-16 12:07:55 +00:00
|
|
|
termNormalize (TermOp (term));
|
|
|
|
termNormalize (TermKey (term));
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* normalize left hand first,both for tupling and for
|
|
|
|
encryption */
|
2004-11-16 12:07:55 +00:00
|
|
|
termNormalize (TermOp1 (term));
|
2004-04-23 11:58:43 +01:00
|
|
|
/* check for ((x,y),z) construct */
|
2004-11-16 12:07:55 +00:00
|
|
|
if (realTermTuple (TermOp1 (term)))
|
2004-04-23 11:58:43 +01:00
|
|
|
{
|
|
|
|
/* temporarily store the old terms */
|
2004-11-16 12:07:55 +00:00
|
|
|
Term tx = TermOp1 (TermOp1 (term));
|
|
|
|
Term ty = TermOp2 (TermOp1 (term));
|
|
|
|
Term tz = TermOp2 (term);
|
2004-04-23 11:58:43 +01:00
|
|
|
/* move node */
|
2004-11-16 12:07:55 +00:00
|
|
|
TermOp2 (term) = TermOp1 (term);
|
2004-04-23 11:58:43 +01:00
|
|
|
/* construct (x,(y,z)) version */
|
2004-11-16 12:07:55 +00:00
|
|
|
TermOp1 (term) = tx;
|
|
|
|
TermOp1 (TermOp2 (term)) = ty;
|
|
|
|
TermOp2 (TermOp2 (term)) = tz;
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
2004-11-16 12:07:55 +00:00
|
|
|
termNormalize (TermOp2 (term));
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-15 13:33:01 +01:00
|
|
|
//! Copy a term, and ensure all run identifiers are set to the new value.
|
|
|
|
/**
|
|
|
|
* Strange code. Only to be used on locals, as is stupidly replaces all run identifiers.
|
|
|
|
*@return The new term.
|
|
|
|
*\sa termDuplicate()
|
|
|
|
*/
|
2004-04-23 11:58:43 +01:00
|
|
|
Term
|
|
|
|
termRunid (Term term, int runid)
|
|
|
|
{
|
|
|
|
if (term == NULL)
|
|
|
|
return NULL;
|
|
|
|
if (realTermLeaf (term))
|
|
|
|
{
|
|
|
|
/* leaf */
|
2004-11-16 12:07:55 +00:00
|
|
|
if (TermRunid (term) == runid)
|
2004-04-23 11:58:43 +01:00
|
|
|
return term;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Term newt = termDuplicate (term);
|
2004-11-16 12:07:55 +00:00
|
|
|
TermRunid (newt) = runid;
|
2004-04-23 11:58:43 +01:00
|
|
|
return newt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* anything else, recurse */
|
|
|
|
if (realTermEncrypt (term))
|
|
|
|
{
|
2004-11-16 12:07:55 +00:00
|
|
|
return makeTermEncrypt (termRunid (TermOp (term), runid),
|
|
|
|
termRunid (TermKey (term), runid));
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-11-16 12:07:55 +00:00
|
|
|
return makeTermTuple (termRunid (TermOp1 (term), runid),
|
|
|
|
termRunid (TermOp2 (term), runid));
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-15 13:33:01 +01:00
|
|
|
//! Determine tuple width of a given term.
|
|
|
|
/**
|
|
|
|
*\sa tupleProject()
|
|
|
|
*/
|
2004-04-23 11:58:43 +01:00
|
|
|
int
|
|
|
|
tupleCount (Term tt)
|
|
|
|
{
|
|
|
|
if (tt == NULL)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-08-19 15:49:03 +01:00
|
|
|
tt = deVar (tt);
|
2004-04-23 11:58:43 +01:00
|
|
|
if (!realTermTuple (tt))
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-11-16 12:07:55 +00:00
|
|
|
return (tupleCount (TermOp1 (tt)) + tupleCount (TermOp2 (tt)));
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-15 13:33:01 +01:00
|
|
|
//! Yield the projection Pi(n) of a term.
|
|
|
|
/**
|
|
|
|
*@param tt Term
|
2005-05-17 19:45:01 +01:00
|
|
|
*@param n The index in the tuple [0..tupleCount-1]
|
2004-05-15 13:33:01 +01:00
|
|
|
*@return Returns either a pointer to a term, or NULL if the index is out of range.
|
|
|
|
*\sa tupleCount()
|
|
|
|
*/
|
2004-04-23 11:58:43 +01:00
|
|
|
Term
|
|
|
|
tupleProject (Term tt, int n)
|
|
|
|
{
|
|
|
|
if (tt == NULL)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
2004-08-19 15:49:03 +01:00
|
|
|
tt = deVar (tt);
|
2004-04-23 11:58:43 +01:00
|
|
|
if (!realTermTuple (tt))
|
|
|
|
{
|
|
|
|
if (n > 0)
|
|
|
|
{
|
|
|
|
/* no tuple, adressing error */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* no tuple */
|
|
|
|
return tt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* there is a tuple to traverse */
|
2004-11-16 12:07:55 +00:00
|
|
|
int left = tupleCount (TermOp1 (tt));
|
2004-04-23 11:58:43 +01:00
|
|
|
if (n >= left)
|
|
|
|
{
|
|
|
|
/* it's in the right hand side */
|
2004-11-16 12:07:55 +00:00
|
|
|
return tupleProject (TermOp2 (tt), n - left);
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* left hand side */
|
2004-11-16 12:07:55 +00:00
|
|
|
return tupleProject (TermOp1 (tt), n);
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-15 13:33:01 +01:00
|
|
|
//! Determine size of term.
|
|
|
|
/**
|
|
|
|
* Determines the size of a term according to some heuristic.
|
2004-04-23 11:58:43 +01:00
|
|
|
* Currently, the encryption operator is weighed as well.
|
2004-05-15 13:33:01 +01:00
|
|
|
*@return Returns a nonnegative integer.
|
|
|
|
*\sa termDistance()
|
2004-04-23 11:58:43 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
2004-08-09 11:05:58 +01:00
|
|
|
termSize (Term t)
|
2004-04-23 11:58:43 +01:00
|
|
|
{
|
|
|
|
if (t == NULL)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-08-09 11:05:58 +01:00
|
|
|
t = deVar (t);
|
|
|
|
if (realTermLeaf (t))
|
2004-04-23 11:58:43 +01:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-08-09 11:05:58 +01:00
|
|
|
if (realTermEncrypt (t))
|
2004-04-23 11:58:43 +01:00
|
|
|
{
|
2004-11-16 12:07:55 +00:00
|
|
|
return 1 + termSize (TermOp (t)) + termSize (TermKey (t));
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-11-16 12:07:55 +00:00
|
|
|
return termSize (TermOp1 (t)) + termSize (TermOp2 (t));
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-15 13:33:01 +01:00
|
|
|
//! Determine distance between two terms.
|
|
|
|
/**
|
|
|
|
*@return A float value between 0, completely dissimilar, and 1, equal.
|
|
|
|
*\sa termSize()
|
2004-04-23 11:58:43 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
float
|
2004-08-09 11:05:58 +01:00
|
|
|
termDistance (Term t1, Term t2)
|
2004-04-23 11:58:43 +01:00
|
|
|
{
|
2004-05-26 09:49:36 +01:00
|
|
|
int t1s;
|
|
|
|
int t2s;
|
|
|
|
|
2004-04-23 11:58:43 +01:00
|
|
|
/* First the special cases: no equal subterms, completely equal */
|
2004-08-09 11:05:58 +01:00
|
|
|
if (isTermEqual (t1, t2))
|
|
|
|
return 1;
|
2004-04-23 11:58:43 +01:00
|
|
|
|
2004-08-09 11:05:58 +01:00
|
|
|
t1 = deVar (t1);
|
|
|
|
t2 = deVar (t2);
|
2004-04-23 11:58:43 +01:00
|
|
|
|
2004-08-09 11:05:58 +01:00
|
|
|
t1s = termSize (t1);
|
|
|
|
t2s = termSize (t2);
|
2004-04-23 11:58:43 +01:00
|
|
|
|
|
|
|
if (t1 == NULL || t2 == NULL)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (t1->type != t2->type)
|
|
|
|
{
|
|
|
|
/* unequal type, maybe one is a subterm of the other? */
|
2004-08-17 15:11:25 +01:00
|
|
|
if (t1s > t2s && termSubTerm (t1, t2))
|
2004-04-23 11:58:43 +01:00
|
|
|
{
|
|
|
|
return (float) t2s / t1s;
|
|
|
|
}
|
2004-08-17 15:11:25 +01:00
|
|
|
if (t2s > t1s && termSubTerm (t2, t1))
|
2004-04-23 11:58:43 +01:00
|
|
|
{
|
|
|
|
return (float) t1s / t2s;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* equal types */
|
2004-08-09 11:05:58 +01:00
|
|
|
if (isTermLeaf (t1))
|
2004-04-23 11:58:43 +01:00
|
|
|
{
|
|
|
|
/* we had established before that they are not equal */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* non-leaf recurse */
|
2004-08-09 11:05:58 +01:00
|
|
|
if (isTermEncrypt (t1))
|
2004-04-23 11:58:43 +01:00
|
|
|
{
|
|
|
|
/* encryption */
|
2004-11-16 12:07:55 +00:00
|
|
|
return (termDistance (TermOp (t1), TermOp (t2)) +
|
|
|
|
termDistance (TermKey (t1), TermKey (t2))) / 2;
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-11-16 12:07:55 +00:00
|
|
|
return (termDistance (TermOp1 (t1), TermOp1 (t2)) +
|
|
|
|
termDistance (TermOp2 (t1), TermOp2 (t2))) / 2;
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-07-14 09:17:49 +01:00
|
|
|
|
|
|
|
/**
|
2004-07-15 12:04:15 +01:00
|
|
|
* Enforce a (arbitrary) ordering on terms
|
2004-07-14 09:17:49 +01:00
|
|
|
* <0 means a<b, 0 means a=b, >0 means a>b.
|
|
|
|
*/
|
2004-08-09 11:05:58 +01:00
|
|
|
int
|
|
|
|
termOrder (Term t1, Term t2)
|
2004-07-14 09:17:49 +01:00
|
|
|
{
|
|
|
|
t1 = deVar (t1);
|
|
|
|
t2 = deVar (t2);
|
2004-08-09 11:05:58 +01:00
|
|
|
if (isTermEqual (t1, t2))
|
2004-07-14 09:17:49 +01:00
|
|
|
{
|
|
|
|
/* equal terms */
|
|
|
|
return 0;
|
|
|
|
}
|
2004-07-15 12:04:15 +01:00
|
|
|
|
|
|
|
/* differ */
|
2004-07-14 09:17:49 +01:00
|
|
|
if (t1->type != t2->type)
|
|
|
|
{
|
2004-07-15 12:04:15 +01:00
|
|
|
/* different types, so ordering on types first */
|
2004-07-14 09:17:49 +01:00
|
|
|
if (t1->type < t2->type)
|
2004-08-09 11:05:58 +01:00
|
|
|
return -1;
|
2004-07-14 09:17:49 +01:00
|
|
|
else
|
2004-08-09 11:05:58 +01:00
|
|
|
return 1;
|
2004-07-14 09:17:49 +01:00
|
|
|
}
|
2004-07-15 12:04:15 +01:00
|
|
|
|
|
|
|
/* same type
|
|
|
|
* distinguish cases
|
|
|
|
*/
|
|
|
|
if (realTermLeaf (t1))
|
|
|
|
{
|
|
|
|
/* compare names */
|
|
|
|
int comp;
|
|
|
|
|
2004-11-16 12:07:55 +00:00
|
|
|
comp = strcmp (TermSymb (t1)->text, TermSymb (t2)->text);
|
2004-07-15 12:04:15 +01:00
|
|
|
if (comp != 0)
|
|
|
|
{
|
|
|
|
/* names differ */
|
|
|
|
return comp;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* equal names, compare run identifiers */
|
2004-11-16 12:07:55 +00:00
|
|
|
if (TermRunid (t1) == TermRunid (t2))
|
2004-07-15 12:04:15 +01:00
|
|
|
{
|
2004-08-09 11:05:58 +01:00
|
|
|
error
|
|
|
|
("termOrder: two terms seem to be identical although local precondition says they aren't.");
|
2004-07-15 12:04:15 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-11-16 12:07:55 +00:00
|
|
|
if (TermRunid (t1) < TermRunid (t2))
|
2004-08-09 11:05:58 +01:00
|
|
|
return -1;
|
2004-07-15 12:04:15 +01:00
|
|
|
else
|
2004-08-09 11:05:58 +01:00
|
|
|
return 1;
|
2004-07-15 12:04:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* non-leaf */
|
2004-08-09 11:05:58 +01:00
|
|
|
int compL, compR;
|
2004-07-15 12:04:15 +01:00
|
|
|
|
|
|
|
if (isTermEncrypt (t1))
|
|
|
|
{
|
2004-11-16 12:07:55 +00:00
|
|
|
compL = termOrder (TermOp (t1), TermOp (t2));
|
|
|
|
compR = termOrder (TermKey (t1), TermKey (t2));
|
2004-07-15 12:04:15 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-11-16 12:07:55 +00:00
|
|
|
compL = termOrder (TermOp1 (t1), TermOp1 (t2));
|
|
|
|
compR = termOrder (TermOp2 (t1), TermOp2 (t2));
|
2004-07-15 12:04:15 +01:00
|
|
|
}
|
|
|
|
if (compL != 0)
|
2004-08-09 11:05:58 +01:00
|
|
|
return compL;
|
2004-07-15 12:04:15 +01:00
|
|
|
else
|
2004-08-09 11:05:58 +01:00
|
|
|
return compR;
|
2004-07-15 12:04:15 +01:00
|
|
|
}
|
2007-01-06 14:45:29 +00:00
|
|
|
// @TODO Should be considered an error
|
|
|
|
return 0;
|
2004-07-14 09:17:49 +01:00
|
|
|
}
|
2004-08-12 10:14:31 +01:00
|
|
|
|
2004-08-18 13:12:29 +01:00
|
|
|
//! Generic term iteration
|
|
|
|
int
|
2006-02-27 16:08:17 +00:00
|
|
|
term_iterate (const Term term, int (*leaf) (Term t), int (*nodel) (Term t),
|
|
|
|
int (*nodem) (Term t), int (*noder) (Term t))
|
2004-08-18 13:12:29 +01:00
|
|
|
{
|
|
|
|
if (term != NULL)
|
|
|
|
{
|
|
|
|
if (realTermLeaf (term))
|
|
|
|
{
|
|
|
|
// Leaf
|
|
|
|
if (leaf != NULL)
|
|
|
|
{
|
|
|
|
return leaf (term);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int flag;
|
|
|
|
|
|
|
|
flag = 1;
|
|
|
|
|
|
|
|
if (nodel != NULL)
|
|
|
|
flag = flag && nodel (term);
|
|
|
|
|
2004-08-30 21:08:11 +01:00
|
|
|
// Left part
|
2004-08-18 13:12:29 +01:00
|
|
|
if (realTermTuple (term))
|
|
|
|
flag = flag
|
2004-11-16 12:07:55 +00:00
|
|
|
&& (term_iterate (TermOp1 (term), leaf, nodel, nodem, noder));
|
2004-08-18 13:12:29 +01:00
|
|
|
else
|
|
|
|
flag = flag
|
2004-11-16 12:07:55 +00:00
|
|
|
&& (term_iterate (TermOp (term), leaf, nodel, nodem, noder));
|
2004-08-18 13:12:29 +01:00
|
|
|
|
|
|
|
if (nodem != NULL)
|
|
|
|
flag = flag && nodem (term);
|
|
|
|
|
2004-08-30 21:08:11 +01:00
|
|
|
// Right part
|
2004-08-18 13:12:29 +01:00
|
|
|
if (realTermTuple (term))
|
|
|
|
flag = flag
|
2004-11-16 12:07:55 +00:00
|
|
|
&& (term_iterate (TermOp2 (term), leaf, nodel, nodem, noder));
|
2004-08-18 13:12:29 +01:00
|
|
|
else
|
|
|
|
flag = flag
|
2004-11-16 12:07:55 +00:00
|
|
|
&& (term_iterate (TermKey (term), leaf, nodel, nodem, noder));
|
2004-08-18 13:12:29 +01:00
|
|
|
|
|
|
|
if (noder != NULL)
|
|
|
|
flag = flag && noder (term);
|
2004-08-19 15:49:03 +01:00
|
|
|
|
|
|
|
return flag;
|
2004-08-18 13:12:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2004-08-19 15:49:03 +01:00
|
|
|
//! Generic term iteration
|
2004-08-18 13:12:29 +01:00
|
|
|
int
|
2006-02-27 16:08:17 +00:00
|
|
|
term_iterate_deVar (Term term, int (*leaf) (Term t), int (*nodel) (Term t),
|
|
|
|
int (*nodem) (Term t), int (*noder) (Term t))
|
2004-08-18 13:12:29 +01:00
|
|
|
{
|
2004-08-19 15:49:03 +01:00
|
|
|
term = deVar (term);
|
|
|
|
if (term != NULL)
|
|
|
|
{
|
|
|
|
if (realTermLeaf (term))
|
|
|
|
{
|
|
|
|
// Leaf
|
|
|
|
if (leaf != NULL)
|
|
|
|
{
|
|
|
|
return leaf (term);
|
|
|
|
}
|
2004-08-30 21:08:11 +01:00
|
|
|
else
|
|
|
|
{
|
2006-02-28 13:41:36 +00:00
|
|
|
return true;
|
2004-08-30 21:08:11 +01:00
|
|
|
}
|
2004-08-19 15:49:03 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int flag;
|
|
|
|
|
2006-02-28 13:41:36 +00:00
|
|
|
flag = true;
|
2004-08-19 15:49:03 +01:00
|
|
|
|
|
|
|
if (nodel != NULL)
|
|
|
|
flag = flag && nodel (term);
|
|
|
|
|
2004-08-30 21:08:11 +01:00
|
|
|
// Left part
|
2004-08-19 15:49:03 +01:00
|
|
|
if (realTermTuple (term))
|
|
|
|
flag = flag
|
2004-08-20 20:16:56 +01:00
|
|
|
&&
|
|
|
|
(term_iterate_deVar
|
2004-11-16 12:07:55 +00:00
|
|
|
(TermOp1 (term), leaf, nodel, nodem, noder));
|
2004-08-19 15:49:03 +01:00
|
|
|
else
|
|
|
|
flag = flag
|
2004-08-20 20:16:56 +01:00
|
|
|
&&
|
2004-11-16 12:07:55 +00:00
|
|
|
(term_iterate_deVar (TermOp (term), leaf, nodel, nodem, noder));
|
2004-08-19 15:49:03 +01:00
|
|
|
|
2006-02-28 13:41:36 +00:00
|
|
|
// Center
|
|
|
|
|
2004-08-19 15:49:03 +01:00
|
|
|
if (nodem != NULL)
|
|
|
|
flag = flag && nodem (term);
|
|
|
|
|
2004-08-30 21:08:11 +01:00
|
|
|
// right part
|
2004-08-19 15:49:03 +01:00
|
|
|
if (realTermTuple (term))
|
|
|
|
flag = flag
|
2004-08-20 20:16:56 +01:00
|
|
|
&&
|
|
|
|
(term_iterate_deVar
|
2004-11-16 12:07:55 +00:00
|
|
|
(TermOp2 (term), leaf, nodel, nodem, noder));
|
2004-08-19 15:49:03 +01:00
|
|
|
else
|
|
|
|
flag = flag
|
2004-08-20 20:16:56 +01:00
|
|
|
&&
|
2004-11-16 12:07:55 +00:00
|
|
|
(term_iterate_deVar
|
|
|
|
(TermKey (term), leaf, nodel, nodem, noder));
|
2004-08-19 15:49:03 +01:00
|
|
|
|
|
|
|
if (noder != NULL)
|
|
|
|
flag = flag && noder (term);
|
|
|
|
|
|
|
|
return flag;
|
|
|
|
}
|
|
|
|
}
|
2006-02-28 13:41:36 +00:00
|
|
|
return true;
|
2004-08-18 13:12:29 +01:00
|
|
|
}
|
|
|
|
|
2004-08-12 10:14:31 +01:00
|
|
|
//! Iterate over the leaves in a term
|
|
|
|
/**
|
|
|
|
* Note that this function iterates over real leaves; thus closed variables can occur as
|
|
|
|
* well. It is up to func to decide wether or not to recurse.
|
|
|
|
*/
|
|
|
|
int
|
2006-02-27 16:08:17 +00:00
|
|
|
term_iterate_leaves (const Term term, int (*func) (Term t))
|
2004-08-12 10:14:31 +01:00
|
|
|
{
|
|
|
|
if (term != NULL)
|
|
|
|
{
|
|
|
|
if (realTermLeaf (term))
|
|
|
|
{
|
|
|
|
if (!func (term))
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (realTermTuple (term))
|
2004-11-16 12:07:55 +00:00
|
|
|
return (term_iterate_leaves (TermOp1 (term), func)
|
|
|
|
&& term_iterate_leaves (TermOp2 (term), func));
|
2004-08-12 10:14:31 +01:00
|
|
|
else
|
2004-11-16 12:07:55 +00:00
|
|
|
return (term_iterate_leaves (TermOp (term), func)
|
|
|
|
&& term_iterate_leaves (TermKey (term), func));
|
2004-08-12 10:14:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Iterate over open leaves (i.e. respect variable closure)
|
|
|
|
int
|
2006-02-27 16:08:17 +00:00
|
|
|
term_iterate_open_leaves (const Term term, int (*func) (Term t))
|
2004-08-12 10:14:31 +01:00
|
|
|
{
|
|
|
|
int testleaf (const Term t)
|
|
|
|
{
|
|
|
|
if (substVar (t))
|
|
|
|
{
|
2006-02-27 16:08:17 +00:00
|
|
|
return term_iterate_open_leaves (t->subst, func);
|
2004-08-12 10:14:31 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return func (t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return term_iterate_leaves (term, testleaf);
|
|
|
|
}
|
2004-08-14 15:23:21 +01:00
|
|
|
|
|
|
|
//! Turn all rolelocals into variables
|
|
|
|
void
|
|
|
|
term_rolelocals_are_variables ()
|
|
|
|
{
|
|
|
|
rolelocal_variable = 1;
|
|
|
|
}
|
2004-08-18 15:06:14 +01:00
|
|
|
|
|
|
|
//! Count the encryption level of a term
|
2006-01-17 16:18:26 +00:00
|
|
|
/**
|
|
|
|
* Note that this stops at any variable that is of ticket type.
|
|
|
|
*/
|
2004-08-18 15:06:14 +01:00
|
|
|
int
|
|
|
|
term_encryption_level (const Term term)
|
|
|
|
{
|
2005-04-18 06:51:25 +01:00
|
|
|
int iter_maxencrypt (Term t)
|
2004-11-16 12:07:55 +00:00
|
|
|
{
|
2006-01-17 16:18:26 +00:00
|
|
|
if (isTicketTerm (t))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2005-04-18 06:51:25 +01:00
|
|
|
t = deVar (t);
|
|
|
|
if (t == NULL)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
2005-05-17 19:45:01 +01:00
|
|
|
if (DEBUGL (2))
|
2005-04-18 06:51:25 +01:00
|
|
|
{
|
|
|
|
eprintf ("Warning: Term encryption level finds a NULL for term ");
|
|
|
|
termPrint (term);
|
|
|
|
eprintf ("\n");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (realTermLeaf (t))
|
2004-11-16 12:07:55 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-04-18 06:51:25 +01:00
|
|
|
if (realTermTuple (t))
|
2004-11-16 12:07:55 +00:00
|
|
|
{
|
|
|
|
int l, r;
|
|
|
|
|
2005-04-18 06:51:25 +01:00
|
|
|
l = iter_maxencrypt (TermOp1 (t));
|
|
|
|
r = iter_maxencrypt (TermOp2 (t));
|
2004-11-16 12:07:55 +00:00
|
|
|
if (l > r)
|
|
|
|
return l;
|
|
|
|
else
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// encrypt
|
2005-04-18 06:51:25 +01:00
|
|
|
return 1 + iter_maxencrypt (TermOp (t));
|
2004-11-16 12:07:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-08-18 15:06:14 +01:00
|
|
|
|
2004-08-30 21:08:11 +01:00
|
|
|
return iter_maxencrypt (term);
|
2004-08-18 15:06:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//! Determine 'constrained factor' of a term
|
|
|
|
/**
|
2006-01-02 21:06:08 +00:00
|
|
|
* Actually this is (number of vars/structure).
|
2004-08-18 15:06:14 +01:00
|
|
|
* Thus, 0 means very constrained, no variables.
|
|
|
|
* Everything else has higher float, but always <=1. In fact, only a single variable has a level 1.
|
|
|
|
*/
|
|
|
|
float
|
|
|
|
term_constrain_level (const Term term)
|
|
|
|
{
|
|
|
|
int vars;
|
|
|
|
int structure;
|
|
|
|
|
2004-08-20 20:16:56 +01:00
|
|
|
void tcl_iterate (Term t)
|
|
|
|
{
|
|
|
|
t = deVar (t);
|
|
|
|
structure++;
|
|
|
|
if (realTermLeaf (t))
|
|
|
|
{
|
|
|
|
if (realTermVariable (t))
|
|
|
|
vars++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (realTermTuple (t))
|
|
|
|
{
|
2004-11-16 12:07:55 +00:00
|
|
|
tcl_iterate (TermOp1 (t));
|
|
|
|
tcl_iterate (TermOp2 (t));
|
2004-08-20 20:16:56 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-11-16 12:07:55 +00:00
|
|
|
tcl_iterate (TermOp (t));
|
|
|
|
tcl_iterate (TermKey (t));
|
2004-08-20 20:16:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-08-18 15:06:14 +01:00
|
|
|
|
|
|
|
if (term == NULL)
|
|
|
|
error ("Cannot determine constrain level of empty term.");
|
|
|
|
|
|
|
|
vars = 0;
|
|
|
|
structure = 0;
|
2004-08-19 15:49:03 +01:00
|
|
|
tcl_iterate (term);
|
2004-08-18 15:06:14 +01:00
|
|
|
return ((float) vars / (float) structure);
|
|
|
|
}
|
2004-08-20 20:16:56 +01:00
|
|
|
|
|
|
|
//! Adjust the keylevels of the symbols in a term.
|
|
|
|
/**
|
|
|
|
* This is used to scan the roles. For each symbol, this function does the bookkeeping of the keylevels at which they occur.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
term_set_keylevels (const Term term)
|
|
|
|
{
|
|
|
|
void scan_levels (int level, Term t)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (DEBUGL (5))
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
|
|
|
|
c = 0;
|
|
|
|
while (c < level)
|
|
|
|
{
|
|
|
|
eprintf (" ");
|
|
|
|
c++;
|
|
|
|
}
|
|
|
|
eprintf ("Scanning keylevel %i for term ", level);
|
|
|
|
termPrint (t);
|
|
|
|
eprintf ("\n");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (realTermLeaf (t))
|
|
|
|
{
|
|
|
|
Symbol sym;
|
|
|
|
|
|
|
|
// So, it occurs at 'level' as key. If that is less than known, store.
|
2004-11-16 12:07:55 +00:00
|
|
|
sym = TermSymb (t);
|
2004-08-20 20:16:56 +01:00
|
|
|
if (level < sym->keylevel)
|
|
|
|
{
|
|
|
|
// New minimum level
|
|
|
|
sym->keylevel = level;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (realTermTuple (t))
|
|
|
|
{
|
2004-11-16 12:07:55 +00:00
|
|
|
scan_levels (level, TermOp1 (t));
|
|
|
|
scan_levels (level, TermOp2 (t));
|
2004-08-20 20:16:56 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-11-16 12:07:55 +00:00
|
|
|
scan_levels (level, TermOp (t));
|
|
|
|
scan_levels ((level + 1), TermKey (t));
|
2004-08-20 20:16:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
scan_levels (0, term);
|
|
|
|
}
|
2004-11-01 14:06:26 +00:00
|
|
|
|
|
|
|
//! Print the term diff of two terms
|
|
|
|
/**
|
|
|
|
* This is not correct yet. We need to add function application and correct tuple handing.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
termPrintDiff (Term t1, Term t2)
|
|
|
|
{
|
|
|
|
void termFromTo (Term t1, Term t2)
|
2004-11-16 12:07:55 +00:00
|
|
|
{
|
|
|
|
t1 = deVar (t1);
|
|
|
|
t2 = deVar (t2);
|
|
|
|
|
|
|
|
eprintf (" [");
|
|
|
|
termPrint (t1);
|
|
|
|
eprintf ("]->[");
|
|
|
|
termPrint (t2);
|
|
|
|
eprintf ("] ");
|
|
|
|
}
|
2004-11-01 14:06:26 +00:00
|
|
|
|
2007-01-06 18:01:36 +00:00
|
|
|
t1 = deVar (t1);
|
|
|
|
t2 = deVar (t2);
|
|
|
|
|
2004-11-01 14:06:26 +00:00
|
|
|
if (isTermEqual (t1, t2))
|
|
|
|
{
|
|
|
|
// Equal, simply print
|
|
|
|
termPrint (t1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (t1->type != t2->type)
|
|
|
|
{
|
|
|
|
// Different types
|
2004-11-16 12:07:55 +00:00
|
|
|
termFromTo (t1, t2);
|
2004-11-01 14:06:26 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Equal types, but not the same
|
|
|
|
// If component type, but both components different, we simply do moveto at the node level.
|
|
|
|
if (realTermLeaf (t1))
|
|
|
|
{
|
|
|
|
// Different constants
|
|
|
|
termFromTo (t1, t2);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (realTermEncrypt (t1))
|
|
|
|
{
|
|
|
|
// Encryption
|
2004-11-16 12:07:55 +00:00
|
|
|
if (isTermEqual (TermOp (t1), TermOp (t2))
|
|
|
|
|| isTermEqual (TermKey (t1), TermKey (t2)))
|
2004-11-01 14:06:26 +00:00
|
|
|
{
|
|
|
|
eprintf ("{");
|
2004-11-16 12:07:55 +00:00
|
|
|
termPrintDiff (TermOp (t1), TermOp (t2));
|
2004-11-01 14:06:26 +00:00
|
|
|
eprintf ("}");
|
2004-11-16 12:07:55 +00:00
|
|
|
termPrintDiff (TermKey (t1), TermKey (t2));
|
2004-11-01 14:06:26 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-11-16 12:07:55 +00:00
|
|
|
termFromTo (t1, t2);
|
2004-11-01 14:06:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Tupling
|
2004-11-16 12:07:55 +00:00
|
|
|
if (isTermEqual (TermOp1 (t1), TermOp1 (t2))
|
|
|
|
|| isTermEqual (TermOp2 (t1), TermOp2 (t2)))
|
2004-11-01 14:06:26 +00:00
|
|
|
{
|
|
|
|
eprintf ("(");
|
2004-11-16 12:07:55 +00:00
|
|
|
termPrintDiff (TermOp1 (t1), TermOp1 (t2));
|
2004-11-01 14:06:26 +00:00
|
|
|
eprintf (")");
|
2004-11-16 12:07:55 +00:00
|
|
|
termPrintDiff (TermOp2 (t1), TermOp2 (t2));
|
2004-11-01 14:06:26 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-11-16 12:07:55 +00:00
|
|
|
termFromTo (t1, t2);
|
2004-11-01 14:06:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-11-12 21:13:00 +00:00
|
|
|
|
|
|
|
//! Test two leaf terms for abstract equality (abstracting from the run identifiers)
|
|
|
|
int
|
|
|
|
isLeafNameEqual (Term t1, Term t2)
|
|
|
|
{
|
|
|
|
return (TermSymb (t1) == TermSymb (t2));
|
|
|
|
}
|
2005-12-28 11:50:17 +00:00
|
|
|
|
|
|
|
//! Generate a fresh term, that does not occur yet, prefixed with the string of the given term.
|
|
|
|
Term
|
|
|
|
freshTermPrefix (Term prefixterm)
|
|
|
|
{
|
|
|
|
Symbol prefixsymbol;
|
|
|
|
Symbol freshsymbol;
|
|
|
|
|
|
|
|
prefixsymbol = NULL;
|
|
|
|
if (prefixterm != NULL && realTermLeaf (prefixterm))
|
|
|
|
{
|
|
|
|
prefixsymbol = TermSymb (prefixterm);
|
|
|
|
}
|
|
|
|
freshsymbol = symbolNextFree (prefixsymbol);
|
|
|
|
return makeTermType (GLOBAL, freshsymbol, -1);
|
|
|
|
}
|
2006-01-02 19:55:34 +00:00
|
|
|
|
|
|
|
//! Determine whether a term is a functor
|
|
|
|
int
|
|
|
|
isTermFunctionName (Term t)
|
|
|
|
{
|
|
|
|
t = deVar (t);
|
|
|
|
if (t != NULL && isTermLeaf (t) && t->stype != NULL
|
|
|
|
&& inTermlist (t->stype, TERM_Function))
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Determine whether a term is a function application. Returns the function term.
|
|
|
|
Term
|
|
|
|
getTermFunction (Term t)
|
|
|
|
{
|
|
|
|
t = deVar (t);
|
|
|
|
if (t != NULL)
|
|
|
|
{
|
|
|
|
if (realTermEncrypt (t) && isTermFunctionName (TermKey (t)))
|
|
|
|
{
|
|
|
|
return TermKey (t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2006-02-21 20:29:05 +00:00
|
|
|
|
|
|
|
//! Return hidelevel for a single term within another
|
|
|
|
unsigned int
|
|
|
|
termHidelevel (const Term tsmall, Term tbig)
|
|
|
|
{
|
|
|
|
tbig = deVar (tbig);
|
|
|
|
if (isTermEqual (tsmall, tbig))
|
|
|
|
{
|
|
|
|
// It's simply here: not hidden at any level
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (realTermLeaf (tbig))
|
|
|
|
{
|
|
|
|
// Not here: infinitely hidden
|
|
|
|
return INT_MAX;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Nested term
|
|
|
|
unsigned int t1, t2;
|
|
|
|
|
|
|
|
if (realTermTuple (tbig))
|
|
|
|
{
|
|
|
|
// Tupling
|
|
|
|
t1 = termHidelevel (tsmall, TermOp1 (tbig));
|
|
|
|
t2 = termHidelevel (tsmall, TermOp2 (tbig));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Encryption
|
|
|
|
t1 = termHidelevel (tsmall, TermOp (tbig));
|
|
|
|
t2 = termHidelevel (tsmall, TermKey (tbig));
|
|
|
|
if (t2 < INT_MAX)
|
|
|
|
t2++;
|
|
|
|
}
|
|
|
|
// Return minimum
|
|
|
|
if (t1 < t2)
|
|
|
|
{
|
|
|
|
return t1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return t2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-02-27 16:08:17 +00:00
|
|
|
|
2006-02-27 22:27:09 +00:00
|
|
|
//! Show a substitution of t
|
|
|
|
void
|
|
|
|
termSubstPrint (Term t)
|
|
|
|
{
|
|
|
|
if (realTermVariable (t))
|
|
|
|
{
|
|
|
|
Term tbuf;
|
|
|
|
|
|
|
|
tbuf = t->subst;
|
|
|
|
t->subst = NULL;
|
|
|
|
termPrint (t);
|
|
|
|
t->subst = tbuf;
|
|
|
|
eprintf (":=");
|
|
|
|
termSubstPrint (t->subst);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
termPrint (t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-02-27 16:08:17 +00:00
|
|
|
// Iterate over subterm constants of other runs in a term
|
|
|
|
// Callback should return true to progress. This is reported in the final thing.
|
|
|
|
int
|
|
|
|
iterateTermOther (const int myrun, Term t, int (*callback) (Term t))
|
|
|
|
{
|
|
|
|
int testOther (Term t)
|
|
|
|
{
|
|
|
|
int run;
|
|
|
|
|
|
|
|
run = TermRunid (t);
|
|
|
|
if (run >= 0 && run != myrun)
|
|
|
|
{
|
|
|
|
return callback (t);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return term_iterate_deVar (t, testOther, NULL, NULL, NULL);
|
|
|
|
}
|
2007-01-06 18:44:04 +00:00
|
|
|
|
|
|
|
//! Unused function (here for testing of unused function detectors)
|
|
|
|
int
|
|
|
|
unusedFunction (int test)
|
|
|
|
{
|
|
|
|
return test + 1;
|
|
|
|
}
|