2007-06-11 13:01:04 +01:00
|
|
|
/*
|
|
|
|
* Scyther : An automatic verifier for security protocols.
|
|
|
|
* Copyright (C) 2007 Cas Cremers
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2004-04-23 11:58:43 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2004-07-24 20:07:29 +01:00
|
|
|
#include "termlist.h"
|
2005-06-16 15:10:07 +01:00
|
|
|
#include "specialterm.h"
|
2004-04-23 11:58:43 +01:00
|
|
|
#include "debug.h"
|
2007-01-06 14:45:29 +00:00
|
|
|
#include "error.h"
|
2007-05-18 13:06:29 +01:00
|
|
|
#include "switches.h"
|
2004-04-23 11:58:43 +01:00
|
|
|
|
2006-01-02 21:06:08 +00:00
|
|
|
/*
|
2005-10-08 20:56:04 +01:00
|
|
|
* Shared stuff
|
|
|
|
*/
|
|
|
|
|
|
|
|
//! Termlist error thing (for global use)
|
|
|
|
Termlist TERMLISTERROR;
|
|
|
|
|
2006-01-02 21:06:08 +00:00
|
|
|
/*
|
2005-10-08 20:56:04 +01:00
|
|
|
* Forward declarations
|
|
|
|
*/
|
|
|
|
|
|
|
|
Termlist makeTermlist ();
|
|
|
|
|
2004-05-15 13:33:01 +01:00
|
|
|
//! Open termlists code.
|
2004-04-23 11:58:43 +01:00
|
|
|
void
|
|
|
|
termlistsInit (void)
|
|
|
|
{
|
2005-10-08 20:56:04 +01:00
|
|
|
TERMLISTERROR = makeTermlist ();
|
|
|
|
TERMLISTERROR->term = NULL;
|
|
|
|
TERMLISTERROR->prev = NULL;
|
|
|
|
TERMLISTERROR->next = NULL;
|
2004-04-23 11:58:43 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-05-15 13:33:01 +01:00
|
|
|
//! Close termlists code.
|
2004-04-23 11:58:43 +01:00
|
|
|
void
|
|
|
|
termlistsDone (void)
|
|
|
|
{
|
2005-10-08 20:56:04 +01:00
|
|
|
termlistDelete (TERMLISTERROR);
|
2004-04-23 11:58:43 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-05-15 13:33:01 +01:00
|
|
|
//! Allocate memory for a termlist node.
|
|
|
|
/**
|
|
|
|
*@return A pointer to uninitialised memory of the size of a termlist node.
|
|
|
|
*/
|
2004-04-23 11:58:43 +01:00
|
|
|
Termlist
|
|
|
|
makeTermlist ()
|
|
|
|
{
|
2004-05-15 13:33:01 +01:00
|
|
|
/* inline candidate */
|
2006-03-08 13:58:46 +00:00
|
|
|
return (Termlist) malloc (sizeof (struct termlist));
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
|
2004-05-15 13:33:01 +01:00
|
|
|
//! Duplicate a termlist.
|
|
|
|
/**
|
|
|
|
* Uses termDuplicate to copy the elements, and allocated new memory for the list nodes.
|
|
|
|
*\sa termDuplicate(), termlistShallow()
|
|
|
|
*/
|
2004-04-23 11:58:43 +01:00
|
|
|
Termlist
|
|
|
|
termlistDuplicate (Termlist tl)
|
|
|
|
{
|
|
|
|
Termlist newtl;
|
|
|
|
|
|
|
|
if (tl == NULL)
|
|
|
|
return NULL;
|
|
|
|
newtl = makeTermlist ();
|
|
|
|
newtl->term = termDuplicate (tl->term);
|
|
|
|
newtl->prev = NULL;
|
|
|
|
newtl->next = termlistDuplicate (tl->next);
|
|
|
|
if (newtl->next != NULL)
|
|
|
|
(newtl->next)->prev = newtl;
|
|
|
|
return newtl;
|
|
|
|
}
|
|
|
|
|
2004-05-15 13:33:01 +01:00
|
|
|
//! Shallow reverse copy of a termlist.
|
|
|
|
/**
|
|
|
|
* Just copies the element pointers. Allocates new memory for the list nodes.
|
|
|
|
* Note that it reverses the order of the list.
|
|
|
|
*\sa termlistDuplicate()
|
|
|
|
*/
|
2004-04-23 11:58:43 +01:00
|
|
|
Termlist
|
|
|
|
termlistShallow (Termlist tl)
|
|
|
|
{
|
|
|
|
Termlist newtl;
|
|
|
|
|
|
|
|
newtl = NULL;
|
|
|
|
while (tl != NULL)
|
|
|
|
{
|
|
|
|
newtl = termlistAdd (newtl, tl->term);
|
|
|
|
tl = tl->next;
|
|
|
|
}
|
|
|
|
return newtl;
|
|
|
|
}
|
|
|
|
|
2004-05-15 13:33:01 +01:00
|
|
|
//! Shallow deletion of a termlist.
|
|
|
|
/**
|
|
|
|
* Deletes the termlist nodes only. Elements are intact after exit.
|
|
|
|
*\sa termlistShallow()
|
|
|
|
*/
|
2004-04-23 11:58:43 +01:00
|
|
|
void
|
|
|
|
termlistDelete (Termlist tl)
|
|
|
|
{
|
|
|
|
if (tl == NULL)
|
|
|
|
return;
|
2005-10-08 20:56:04 +01:00
|
|
|
#ifdef DEBUG
|
|
|
|
if (tl == TERMLISTERROR)
|
|
|
|
{
|
|
|
|
static int count = 0;
|
|
|
|
|
|
|
|
count++;
|
|
|
|
if (count > 1)
|
|
|
|
{
|
|
|
|
// TERMLISTERROR should only be destroyed once (by the done function)
|
|
|
|
error ("Trying to delete TERMLISTERROR a second time, whazzup?");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2004-04-23 11:58:43 +01:00
|
|
|
termlistDelete (tl->next);
|
2006-03-08 13:58:46 +00:00
|
|
|
free (tl);
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-05-15 13:33:01 +01:00
|
|
|
//! Deep deletion of a termlist.
|
|
|
|
/**
|
|
|
|
* Deletes the termlist nodes as well as the elements.
|
|
|
|
*\sa termlistDuplicate(), termDuplicate(), termDelete()
|
|
|
|
*/
|
2004-04-23 11:58:43 +01:00
|
|
|
void
|
|
|
|
termlistDestroy (Termlist tl)
|
|
|
|
{
|
|
|
|
if (tl == NULL)
|
|
|
|
return;
|
|
|
|
termlistDestroy (tl->next);
|
|
|
|
termDelete (tl->term);
|
2006-03-08 13:58:46 +00:00
|
|
|
free (tl);
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
|
2004-05-15 13:33:01 +01:00
|
|
|
//! Determine whether a term is an element of a termlist.
|
|
|
|
/**
|
2005-04-18 06:51:25 +01:00
|
|
|
* The NULL term is not an element of any list. (Not even of the NULL list)
|
2004-07-20 09:51:23 +01:00
|
|
|
*
|
2004-05-15 13:33:01 +01:00
|
|
|
*@return True iff the term is an element of the termlist.
|
|
|
|
*/
|
2004-07-20 09:51:23 +01:00
|
|
|
__inline__ int
|
|
|
|
inTermlist (Termlist tl, const Term term)
|
2004-04-23 11:58:43 +01:00
|
|
|
{
|
2004-07-20 09:51:23 +01:00
|
|
|
if (term == NULL)
|
2004-04-23 11:58:43 +01:00
|
|
|
{
|
2005-04-18 06:51:25 +01:00
|
|
|
return 0;
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
2004-07-20 09:51:23 +01:00
|
|
|
while (tl != NULL)
|
2004-04-23 11:58:43 +01:00
|
|
|
{
|
|
|
|
if (isTermEqual (tl->term, term))
|
2004-07-20 09:51:23 +01:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
tl = tl->next;
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
2004-07-20 09:51:23 +01:00
|
|
|
return 0;
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
|
2004-08-27 14:10:46 +01:00
|
|
|
//! Determine whether a term is an element of a termlist: yield pointer
|
2004-09-20 13:40:01 +01:00
|
|
|
__inline__ Termlist
|
|
|
|
termlistFind (Termlist tl, const Term term)
|
2004-08-27 14:10:46 +01:00
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (term == NULL)
|
|
|
|
{
|
|
|
|
error ("Trying to do inTermlist for a NULL term.");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
while (tl != NULL)
|
|
|
|
{
|
|
|
|
if (isTermEqual (tl->term, term))
|
|
|
|
{
|
|
|
|
return tl;
|
|
|
|
}
|
|
|
|
tl = tl->next;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-05-15 13:33:01 +01:00
|
|
|
//! Equality of two term lists.
|
|
|
|
/**
|
|
|
|
* Are all elements of list 1 in list 2, and vice versa?
|
|
|
|
* Note that we assume unique elements!
|
2004-05-15 17:43:20 +01:00
|
|
|
*@return True iff every element of the list is in the other list.
|
2004-04-23 11:58:43 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
isTermlistEqual (Termlist tl1, Termlist tl2)
|
|
|
|
{
|
|
|
|
if (termlistLength (tl1) != termlistLength (tl2))
|
|
|
|
return 0;
|
|
|
|
while (tl2 != NULL)
|
|
|
|
{
|
|
|
|
if (!inTermlist (tl1, tl2->term))
|
|
|
|
return 0;
|
|
|
|
tl2 = tl2->next;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2004-05-15 13:33:01 +01:00
|
|
|
//! Adds a term to the front of a termlist.
|
|
|
|
/**
|
|
|
|
* Duplicates are allowed.
|
|
|
|
*@return A new list pointer.
|
|
|
|
*\sa termlistAppend()
|
|
|
|
*/
|
2004-04-23 11:58:43 +01:00
|
|
|
Termlist
|
|
|
|
termlistAdd (Termlist tl, Term term)
|
|
|
|
{
|
|
|
|
Termlist newtl;
|
|
|
|
|
|
|
|
newtl = makeTermlist ();
|
|
|
|
newtl->term = term;
|
|
|
|
newtl->next = tl;
|
|
|
|
|
|
|
|
if (tl == NULL)
|
|
|
|
{
|
|
|
|
newtl->prev = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
newtl->prev = tl->prev;
|
|
|
|
if (newtl->prev != NULL)
|
|
|
|
(newtl->prev)->next = newtl;
|
|
|
|
tl->prev = newtl;
|
|
|
|
}
|
|
|
|
return newtl;
|
|
|
|
}
|
|
|
|
|
2004-05-15 13:33:01 +01:00
|
|
|
//! Adds a term to the end of a termlist.
|
|
|
|
/**
|
|
|
|
* Duplicates are allowed.
|
|
|
|
*@return A new list pointer if the termlist was NULL.
|
|
|
|
*\sa termlistAdd()
|
|
|
|
*/
|
2004-04-23 11:58:43 +01:00
|
|
|
Termlist
|
|
|
|
termlistAppend (const Termlist tl, const Term term)
|
|
|
|
{
|
|
|
|
Termlist newtl;
|
|
|
|
Termlist scantl;
|
|
|
|
|
|
|
|
newtl = makeTermlist ();
|
|
|
|
newtl->term = term;
|
|
|
|
newtl->next = NULL;
|
|
|
|
|
|
|
|
if (tl == NULL)
|
|
|
|
{
|
|
|
|
newtl->prev = NULL;
|
|
|
|
return newtl;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
scantl = tl;
|
|
|
|
while (scantl->next != NULL)
|
|
|
|
scantl = scantl->next;
|
|
|
|
scantl->next = newtl;
|
|
|
|
newtl->prev = scantl;
|
|
|
|
}
|
|
|
|
return tl;
|
|
|
|
}
|
|
|
|
|
2004-07-25 19:14:21 +01:00
|
|
|
//! Add a term only to a list if it wasn't in it before.
|
|
|
|
/**
|
|
|
|
* Mimics a basic set type behaviour.
|
|
|
|
*/
|
|
|
|
Termlist
|
2004-07-25 19:24:50 +01:00
|
|
|
termlistAddNew (const Termlist tl, const Term t)
|
2004-07-25 19:14:21 +01:00
|
|
|
{
|
2004-08-27 13:36:23 +01:00
|
|
|
if (t == NULL || inTermlist (tl, t))
|
2004-08-09 11:05:58 +01:00
|
|
|
return tl;
|
2004-07-25 19:14:21 +01:00
|
|
|
else
|
2004-08-09 11:05:58 +01:00
|
|
|
return termlistAdd (tl, t);
|
2004-07-25 19:14:21 +01:00
|
|
|
}
|
|
|
|
|
2004-05-15 13:33:01 +01:00
|
|
|
//! Concatenates two termlists.
|
|
|
|
/**
|
|
|
|
* The last pointer of the first list is made to point to the second list.
|
|
|
|
*@return The pointer to the concatenated list.
|
|
|
|
*/
|
2004-04-23 11:58:43 +01:00
|
|
|
Termlist
|
|
|
|
termlistConcat (Termlist tl1, Termlist tl2)
|
|
|
|
{
|
2004-05-26 13:17:09 +01:00
|
|
|
Termlist scan;
|
|
|
|
|
2004-04-23 11:58:43 +01:00
|
|
|
if (tl1 == NULL)
|
|
|
|
return tl2;
|
|
|
|
if (tl2 == NULL)
|
|
|
|
return tl1;
|
|
|
|
|
2004-05-26 13:17:09 +01:00
|
|
|
scan = tl1;
|
2004-04-23 11:58:43 +01:00
|
|
|
while (scan->next != NULL)
|
|
|
|
scan = scan->next;
|
|
|
|
scan->next = tl2;
|
|
|
|
return tl1;
|
|
|
|
}
|
|
|
|
|
2006-02-26 17:18:59 +00:00
|
|
|
//! Concatenates two termlists.
|
|
|
|
/**
|
|
|
|
* Creates a completely new list that can be deleted.
|
|
|
|
*
|
|
|
|
* Note that the order is not preserved currently.
|
|
|
|
*/
|
|
|
|
Termlist
|
|
|
|
termlistConcatStatic (Termlist tl1, Termlist tl2)
|
|
|
|
{
|
|
|
|
Termlist tl, tls;
|
|
|
|
|
|
|
|
tl = NULL;
|
|
|
|
for (tls = tl1; tls != NULL; tls = tls->next)
|
|
|
|
{
|
|
|
|
tl = termlistAdd (tl, tls->term);
|
|
|
|
}
|
|
|
|
for (tls = tl2; tls != NULL; tls = tls->next)
|
|
|
|
{
|
|
|
|
tl = termlistAdd (tl, tls->term);
|
|
|
|
}
|
|
|
|
return tl;
|
|
|
|
}
|
|
|
|
|
2004-05-15 13:33:01 +01:00
|
|
|
//! Remove the pointed at element from the termlist.
|
|
|
|
/**
|
2005-05-17 19:45:01 +01:00
|
|
|
* Easier because of the double linked list. Note: does not do termDelete on the term.
|
|
|
|
*
|
2004-05-15 13:33:01 +01:00
|
|
|
*@param tl The pointer to the termlist node to be deleted from the list.
|
|
|
|
*@return The possibly new head pointer to the termlist.
|
|
|
|
*/
|
2004-04-23 11:58:43 +01:00
|
|
|
Termlist
|
|
|
|
termlistDelTerm (Termlist tl)
|
|
|
|
{
|
|
|
|
Termlist newhead;
|
|
|
|
|
|
|
|
if (tl == NULL)
|
|
|
|
return NULL;
|
|
|
|
if (tl->prev != NULL)
|
|
|
|
{
|
|
|
|
(tl->prev)->next = tl->next;
|
|
|
|
newhead = tl->prev;
|
|
|
|
while (newhead->prev != NULL)
|
|
|
|
newhead = newhead->prev;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
newhead = tl->next;
|
|
|
|
}
|
|
|
|
if (tl->next != NULL)
|
|
|
|
(tl->next)->prev = tl->prev;
|
2006-03-08 13:58:46 +00:00
|
|
|
free (tl);
|
2004-04-23 11:58:43 +01:00
|
|
|
return newhead;
|
|
|
|
}
|
|
|
|
|
2004-05-15 13:33:01 +01:00
|
|
|
//! Construct the conjunction of two termlists.
|
|
|
|
/**
|
|
|
|
*@return A new termlist containing the elements in both lists.
|
|
|
|
*/
|
2004-04-23 11:58:43 +01:00
|
|
|
Termlist
|
|
|
|
termlistConjunct (Termlist tl1, Termlist tl2)
|
|
|
|
{
|
|
|
|
Termlist newtl;
|
|
|
|
Termlist scan;
|
|
|
|
|
|
|
|
scan = tl1;
|
|
|
|
newtl = NULL;
|
|
|
|
while (scan != NULL)
|
|
|
|
{
|
|
|
|
if (inTermlist (tl2, scan->term))
|
|
|
|
newtl = termlistAdd (newtl, scan->term);
|
|
|
|
scan = scan->next;
|
|
|
|
}
|
|
|
|
return newtl;
|
|
|
|
}
|
|
|
|
|
2004-05-15 13:33:01 +01:00
|
|
|
//! Construct the conjunction of two termlists, and a certain type.
|
|
|
|
/**
|
|
|
|
*@return A new termlist containing the elements in both lists, that are also of the desired type.
|
|
|
|
*/
|
2004-04-23 11:58:43 +01:00
|
|
|
Termlist
|
|
|
|
termlistConjunctType (Termlist tl1, Termlist tl2, int termtype)
|
|
|
|
{
|
|
|
|
Termlist newtl;
|
|
|
|
Termlist scan;
|
|
|
|
|
|
|
|
scan = tl1;
|
|
|
|
newtl = NULL;
|
|
|
|
while (scan != NULL)
|
|
|
|
{
|
|
|
|
if (((scan->term)->type == termtype) && (inTermlist (tl2, scan->term)))
|
|
|
|
newtl = termlistAdd (newtl, scan->term);
|
|
|
|
scan = scan->next;
|
|
|
|
}
|
|
|
|
return newtl;
|
|
|
|
}
|
|
|
|
|
2004-05-15 13:33:01 +01:00
|
|
|
//! Construct the conjunction of a termlist and a certain type.
|
|
|
|
/**
|
|
|
|
*@return A new termlist containing the elements in the list that are of the desired type.
|
|
|
|
*/
|
2004-04-23 11:58:43 +01:00
|
|
|
Termlist
|
|
|
|
termlistType (Termlist tl, int termtype)
|
|
|
|
{
|
|
|
|
Termlist newtl;
|
|
|
|
Termlist scan;
|
|
|
|
|
|
|
|
scan = tl;
|
|
|
|
newtl = NULL;
|
|
|
|
while (scan != NULL)
|
|
|
|
{
|
|
|
|
if ((scan->term)->type == termtype)
|
|
|
|
newtl = termlistAdd (newtl, scan->term);
|
|
|
|
scan = scan->next;
|
|
|
|
}
|
|
|
|
return newtl;
|
|
|
|
}
|
|
|
|
|
2004-05-15 13:33:01 +01:00
|
|
|
//! Display a termlist.
|
|
|
|
/**
|
|
|
|
* Lists of terms are displayed between square brackets, and seperated by commas.
|
|
|
|
*/
|
2004-04-23 11:58:43 +01:00
|
|
|
void
|
|
|
|
termlistPrint (Termlist tl)
|
|
|
|
{
|
|
|
|
if (tl == NULL)
|
|
|
|
{
|
2004-07-29 13:36:24 +01:00
|
|
|
eprintf ("[Empty]");
|
2004-04-23 11:58:43 +01:00
|
|
|
return;
|
|
|
|
}
|
2004-07-29 13:36:24 +01:00
|
|
|
eprintf ("[");
|
2004-04-23 11:58:43 +01:00
|
|
|
while (tl != NULL)
|
|
|
|
{
|
|
|
|
termPrint (tl->term);
|
|
|
|
tl = tl->next;
|
|
|
|
if (tl != NULL)
|
2004-08-09 11:05:58 +01:00
|
|
|
eprintf (", ");
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
2004-07-29 13:36:24 +01:00
|
|
|
eprintf ("]");
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
|
2004-05-15 13:33:01 +01:00
|
|
|
//! Append all open variables in a term to a list.
|
|
|
|
/**
|
|
|
|
*@param tl The list to which to append to.
|
|
|
|
*@param t The term possibly containing open variables.
|
|
|
|
*@return The pointer to the extended list.
|
|
|
|
*\sa termlistAddRealVariables()
|
|
|
|
*/
|
2004-04-23 11:58:43 +01:00
|
|
|
Termlist
|
|
|
|
termlistAddVariables (Termlist tl, Term t)
|
|
|
|
{
|
|
|
|
if (t == NULL)
|
|
|
|
return tl;
|
|
|
|
|
|
|
|
t = deVar (t);
|
|
|
|
if (isTermLeaf (t))
|
|
|
|
{
|
|
|
|
if (isTermVariable (t) && !inTermlist (tl, t))
|
|
|
|
return termlistAdd (tl, t);
|
|
|
|
else
|
|
|
|
return tl;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (isTermEncrypt (t))
|
2004-11-16 12:07:55 +00:00
|
|
|
return termlistAddVariables (termlistAddVariables (tl, TermOp (t)),
|
|
|
|
TermKey (t));
|
2004-04-23 11:58:43 +01:00
|
|
|
else
|
|
|
|
return
|
2004-11-16 12:07:55 +00:00
|
|
|
termlistAddVariables (termlistAddVariables (tl, TermOp1 (t)),
|
|
|
|
TermOp2 (t));
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
}
|
2004-08-09 11:05:58 +01:00
|
|
|
|
2004-05-15 13:33:01 +01:00
|
|
|
//! Append all variables in a term to a list.
|
|
|
|
/**
|
|
|
|
*@param tl The list to which to append to.
|
|
|
|
*@param t The term possibly containing open and closed variables.
|
|
|
|
*@return The pointer to the extended list.
|
|
|
|
*\sa termlistAddVariables()
|
|
|
|
*/
|
2004-04-23 11:58:43 +01:00
|
|
|
Termlist
|
|
|
|
termlistAddRealVariables (Termlist tl, Term t)
|
|
|
|
{
|
|
|
|
if (t == NULL)
|
|
|
|
return tl;
|
|
|
|
|
|
|
|
if (realTermLeaf (t))
|
|
|
|
{
|
|
|
|
if (realTermVariable (t))
|
|
|
|
{
|
|
|
|
Term tbuf = t->subst;
|
|
|
|
t->subst = NULL;
|
2004-08-09 11:05:58 +01:00
|
|
|
if (!inTermlist (tl, t))
|
2004-04-23 11:58:43 +01:00
|
|
|
{
|
2004-08-09 11:05:58 +01:00
|
|
|
tl = termlistAdd (tl, t);
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
t->subst = tbuf;
|
2004-08-09 11:05:58 +01:00
|
|
|
return termlistAddRealVariables (tl, t->subst);
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return tl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (realTermEncrypt (t))
|
2004-11-16 12:07:55 +00:00
|
|
|
return termlistAddVariables (termlistAddVariables (tl, TermOp (t)),
|
|
|
|
TermKey (t));
|
2004-04-23 11:58:43 +01:00
|
|
|
else
|
|
|
|
return
|
2004-11-16 12:07:55 +00:00
|
|
|
termlistAddVariables (termlistAddVariables (tl, TermOp1 (t)),
|
|
|
|
TermOp2 (t));
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-15 13:33:01 +01:00
|
|
|
//! Append all basic terms in a term to a list.
|
|
|
|
/**
|
|
|
|
*@param tl The list to which to append to.
|
|
|
|
*@param t The term containing basic terms.
|
|
|
|
*@return The pointer to the extended list.
|
|
|
|
*\sa termlistAddBasics()
|
|
|
|
*/
|
2004-04-23 11:58:43 +01:00
|
|
|
Termlist
|
|
|
|
termlistAddBasic (Termlist tl, Term t)
|
|
|
|
{
|
2006-03-31 13:24:32 +01:00
|
|
|
t = deVar (t);
|
|
|
|
|
2004-04-23 11:58:43 +01:00
|
|
|
if (t == NULL)
|
|
|
|
return tl;
|
2006-03-31 13:24:32 +01:00
|
|
|
if (!realTermLeaf (t))
|
2004-04-23 11:58:43 +01:00
|
|
|
{
|
2006-03-31 13:24:32 +01:00
|
|
|
if (realTermEncrypt (t))
|
2004-11-16 12:07:55 +00:00
|
|
|
return termlistAddBasic (termlistAddBasic (tl, TermOp (t)),
|
|
|
|
TermKey (t));
|
2004-04-23 11:58:43 +01:00
|
|
|
else
|
2004-11-16 12:07:55 +00:00
|
|
|
return termlistAddBasic (termlistAddBasic (tl, TermOp1 (t)),
|
|
|
|
TermOp2 (t));
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!inTermlist (tl, t))
|
|
|
|
{
|
|
|
|
return termlistAdd (tl, t);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return tl;
|
|
|
|
}
|
|
|
|
|
2004-05-15 13:33:01 +01:00
|
|
|
//! Append all basic terms in a termlist to another list.
|
|
|
|
/**
|
|
|
|
*@param tl The list to which to append to.
|
|
|
|
*@param scan The termlist with terms containing basic terms.
|
|
|
|
*@return The pointer to the extended list.
|
|
|
|
*\sa termlistAddBasic()
|
|
|
|
*/
|
2004-04-23 11:58:43 +01:00
|
|
|
Termlist
|
|
|
|
termlistAddBasics (Termlist tl, Termlist scan)
|
|
|
|
{
|
|
|
|
while (scan != NULL)
|
|
|
|
{
|
|
|
|
tl = termlistAddBasic (tl, scan->term);
|
|
|
|
scan = scan->next;
|
|
|
|
}
|
|
|
|
return tl;
|
|
|
|
}
|
|
|
|
|
2004-05-15 13:33:01 +01:00
|
|
|
//! Remove a term from a termlist.
|
|
|
|
/**
|
|
|
|
* Removes the first occurrence of the term.
|
|
|
|
*@return A new termlist pointer.
|
2004-04-23 11:58:43 +01:00
|
|
|
*/
|
|
|
|
Termlist
|
|
|
|
termlistMinusTerm (Termlist tl, Term t)
|
|
|
|
{
|
|
|
|
Termlist scan;
|
|
|
|
|
|
|
|
scan = tl;
|
|
|
|
while (scan != NULL)
|
|
|
|
{
|
|
|
|
if (isTermEqual (scan->term, t))
|
|
|
|
return termlistDelTerm (scan);
|
|
|
|
else
|
|
|
|
scan = scan->next;
|
|
|
|
}
|
|
|
|
return tl;
|
|
|
|
}
|
|
|
|
|
2004-05-15 13:33:01 +01:00
|
|
|
//! Determine the length of a termlist.
|
2004-04-23 11:58:43 +01:00
|
|
|
int
|
|
|
|
termlistLength (Termlist tl)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
while (tl != NULL)
|
|
|
|
{
|
|
|
|
tl = tl->next;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2004-05-15 13:33:01 +01:00
|
|
|
//! Give the inverse key term of a term.
|
|
|
|
/**
|
|
|
|
* Gives a duplicate of the inverse Key of some term (which is used to encrypt something), as is defined
|
|
|
|
* by the termlist, which is a list of key1,key1inv, key2, key2inv, etc...
|
|
|
|
*@param inverses The list of inverses, typically from the knowledge.
|
|
|
|
*@param key Any term of which the inverse will be determined.
|
2004-08-09 22:22:06 +01:00
|
|
|
*@return A pointer to a duplicate of the inverse key term. Use termDelete to remove it.
|
2004-05-15 13:33:01 +01:00
|
|
|
*\sa termDuplicate(), knowledge::inverses
|
|
|
|
*/
|
2004-04-23 11:58:43 +01:00
|
|
|
|
|
|
|
Term
|
|
|
|
inverseKey (Termlist inverses, Term key)
|
|
|
|
{
|
|
|
|
key = deVar (key);
|
|
|
|
|
|
|
|
/* is this a function application? i.e. hash? */
|
|
|
|
if (isTermLeaf (key) && inTermlist (key->stype, TERM_Function))
|
|
|
|
{
|
|
|
|
/* functions cannot be inverted by default */
|
|
|
|
return termDuplicate (TERM_Hidden);
|
|
|
|
}
|
|
|
|
/* check for the special case first: when it is effectively a function application */
|
2004-11-16 12:07:55 +00:00
|
|
|
if (isTermEncrypt (key) && isTermLeaf (TermKey (key))
|
|
|
|
&& inTermlist (deVar (TermKey (key))->stype, TERM_Function))
|
2004-04-23 11:58:43 +01:00
|
|
|
{
|
|
|
|
/* we are scanning for functions */
|
|
|
|
/* scan the list */
|
|
|
|
/* key is function application kk(op), or {op}kk */
|
|
|
|
Term funKey (Term orig, Term newk)
|
|
|
|
{
|
|
|
|
/* in: {op}kk, nk
|
|
|
|
* out: {op'}nk */
|
2004-11-16 12:07:55 +00:00
|
|
|
return makeTermEncrypt (termDuplicate (TermOp (orig)),
|
2004-04-23 11:58:43 +01:00
|
|
|
termDuplicate (newk));
|
|
|
|
}
|
|
|
|
while (inverses != NULL && inverses->next != NULL)
|
|
|
|
{
|
|
|
|
|
2004-11-16 12:07:55 +00:00
|
|
|
if (isTermEqual (TermKey (key), inverses->term))
|
2004-04-23 11:58:43 +01:00
|
|
|
return funKey (key, inverses->next->term);
|
2004-11-16 12:07:55 +00:00
|
|
|
if (isTermEqual (TermKey (key), inverses->next->term))
|
2004-04-23 11:58:43 +01:00
|
|
|
return funKey (key, inverses->term);
|
|
|
|
inverses = inverses->next->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* scanning for a direct inverse */
|
|
|
|
|
|
|
|
/* scan the list */
|
|
|
|
while (inverses != NULL && inverses->next != NULL)
|
|
|
|
{
|
|
|
|
if (isTermEqual (key, inverses->term))
|
|
|
|
return termDuplicate (inverses->next->term);
|
|
|
|
if (isTermEqual (key, inverses->next->term))
|
|
|
|
return termDuplicate (inverses->term);
|
|
|
|
inverses = inverses->next->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return termDuplicate (key); /* defaults to symmetrical */
|
|
|
|
}
|
|
|
|
|
2004-05-15 13:33:01 +01:00
|
|
|
//! Create a term local to a run.
|
2004-04-23 11:58:43 +01:00
|
|
|
/*
|
|
|
|
* We assume that at this point, no variables have been instantiated yet that occur in this term.
|
2004-08-12 10:14:31 +01:00
|
|
|
* We also assume that fromlist, tolist only hold real leaves.
|
2004-09-20 13:40:01 +01:00
|
|
|
*
|
|
|
|
* variable instantiations are not followed through.
|
|
|
|
*
|
2004-05-15 13:33:01 +01:00
|
|
|
*\sa termlistLocal()
|
2004-04-23 11:58:43 +01:00
|
|
|
*/
|
|
|
|
Term
|
2005-10-08 20:56:04 +01:00
|
|
|
termLocal (const Term t, Termlist fromlist, Termlist tolist)
|
2004-04-23 11:58:43 +01:00
|
|
|
{
|
|
|
|
if (t == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (realTermLeaf (t))
|
|
|
|
{
|
|
|
|
while (fromlist != NULL && tolist != NULL)
|
|
|
|
{
|
|
|
|
if (isTermEqual (fromlist->term, t))
|
|
|
|
{
|
|
|
|
// matches!
|
|
|
|
return tolist->term;
|
|
|
|
}
|
|
|
|
fromlist = fromlist->next;
|
|
|
|
tolist = tolist->next;
|
|
|
|
}
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-10-12 16:12:20 +01:00
|
|
|
Term newt = termNodeDuplicate (t);
|
2004-04-23 11:58:43 +01:00
|
|
|
if (realTermTuple (t))
|
|
|
|
{
|
2004-12-08 16:25:27 +00:00
|
|
|
TermOp1 (newt) = termLocal (TermOp1 (t), fromlist, tolist);
|
|
|
|
TermOp2 (newt) = termLocal (TermOp2 (t), fromlist, tolist);
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-12-08 16:25:27 +00:00
|
|
|
TermOp (newt) = termLocal (TermOp (t), fromlist, tolist);
|
|
|
|
TermKey (newt) = termLocal (TermKey (t), fromlist, tolist);
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
return newt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-15 13:33:01 +01:00
|
|
|
//! Create a list of instance terms.
|
|
|
|
/**
|
|
|
|
* We expand the termlocal concept to termlists.
|
|
|
|
*\sa termLocal()
|
2004-04-23 11:58:43 +01:00
|
|
|
*/
|
|
|
|
Termlist
|
2004-12-08 16:25:27 +00:00
|
|
|
termlistLocal (Termlist tl, const Termlist fromlist, const Termlist tolist)
|
2004-04-23 11:58:43 +01:00
|
|
|
{
|
|
|
|
Termlist newtl = NULL;
|
|
|
|
|
|
|
|
while (tl != NULL)
|
|
|
|
{
|
2005-05-17 19:45:01 +01:00
|
|
|
newtl = termlistAdd (newtl, termLocal (tl->term, fromlist, tolist));
|
2004-04-23 11:58:43 +01:00
|
|
|
tl = tl->next;
|
|
|
|
}
|
|
|
|
return newtl;
|
|
|
|
}
|
|
|
|
|
2004-05-15 13:33:01 +01:00
|
|
|
//! Check whether a termlist is contained in another.
|
|
|
|
/**
|
|
|
|
*@param tlbig The big list.
|
|
|
|
*@param tlsmall The list that is possibly contained in the big one.
|
|
|
|
*@return True iff tlsmall is contained in tlbig.
|
2004-04-23 11:58:43 +01:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
termlistContained (const Termlist tlbig, Termlist tlsmall)
|
|
|
|
{
|
|
|
|
while (tlsmall != NULL)
|
|
|
|
{
|
|
|
|
if (!inTermlist (tlbig, tlsmall->term))
|
|
|
|
return 0;
|
|
|
|
tlsmall = tlsmall->next;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2004-05-15 13:33:01 +01:00
|
|
|
//! Check substitution validity
|
|
|
|
/**
|
2004-04-23 11:58:43 +01:00
|
|
|
* Determine whether a variable has been substituted with something with
|
|
|
|
* the right type.
|
2004-05-15 13:33:01 +01:00
|
|
|
*@param term The closed variable term.
|
|
|
|
*@return True iff the substitution is valid in the current mode.
|
|
|
|
*\sa system::match
|
2004-04-23 11:58:43 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
2007-05-18 13:06:29 +01:00
|
|
|
validSubst (const Term term)
|
2004-04-23 11:58:43 +01:00
|
|
|
{
|
|
|
|
if (!realTermVariable (term) || term->subst == NULL)
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
{
|
2007-05-18 13:06:29 +01:00
|
|
|
switch (switches.match)
|
2004-04-23 11:58:43 +01:00
|
|
|
{
|
|
|
|
case 0: /* real type match */
|
|
|
|
return realTermLeaf (term->subst)
|
|
|
|
&& termlistContained (term->stype, term->subst->stype);
|
|
|
|
case 1: /* basic type match */
|
|
|
|
/* subst must be a leaf */
|
|
|
|
/* TODO: what about functions? */
|
|
|
|
return realTermLeaf (term->subst);
|
|
|
|
case 2: /* no type match */
|
|
|
|
/* anything goes */
|
|
|
|
return 1;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-15 13:33:01 +01:00
|
|
|
//! Yield the result of f(x)
|
|
|
|
/**
|
|
|
|
* This function interpretes two termlists as the domain and range of a function,
|
|
|
|
* and if the term occurs in the domain, returns the matching value from the range.
|
|
|
|
* Note that these functions cannot have NULL in the domain or the range.
|
|
|
|
*@param fromlist The domain list.
|
|
|
|
*@param tolist The range list, in a one-to-one correspondence with the fromlist.
|
|
|
|
*@param tx The point on which the function is to be evaluated.
|
|
|
|
*@return The result of the function application or NULL if the point is not within the domain.
|
2004-04-23 11:58:43 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
Term
|
|
|
|
termFunction (Termlist fromlist, Termlist tolist, Term tx)
|
|
|
|
{
|
|
|
|
while (fromlist != NULL && tolist != NULL)
|
|
|
|
{
|
|
|
|
if (isTermEqual (fromlist->term, tx))
|
|
|
|
{
|
|
|
|
return tolist->term;
|
|
|
|
}
|
|
|
|
fromlist = fromlist->next;
|
|
|
|
tolist = tolist->next;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2004-05-15 13:33:01 +01:00
|
|
|
//! Yield the last node of a termlist.
|
2004-04-23 11:58:43 +01:00
|
|
|
Termlist
|
|
|
|
termlistForward (Termlist tl)
|
|
|
|
{
|
|
|
|
if (tl == NULL)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (tl->next != NULL)
|
|
|
|
{
|
|
|
|
tl = tl->next;
|
|
|
|
}
|
|
|
|
return tl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-07-14 09:17:49 +01:00
|
|
|
/**
|
|
|
|
* Compare two termlists containing only basic terms, and yield ordering.
|
|
|
|
*/
|
2004-08-09 11:05:58 +01:00
|
|
|
int
|
|
|
|
termlistOrder (Termlist tl1, Termlist tl2)
|
2004-07-14 09:17:49 +01:00
|
|
|
{
|
|
|
|
int order;
|
|
|
|
|
|
|
|
order = 0;
|
|
|
|
while (order == 0 && tl1 != NULL && tl2 != NULL)
|
|
|
|
{
|
|
|
|
order = termOrder (tl1->term, tl2->term);
|
|
|
|
tl1 = tl1->next;
|
|
|
|
tl2 = tl2->next;
|
|
|
|
}
|
|
|
|
if (order != 0)
|
2004-08-09 11:05:58 +01:00
|
|
|
return order;
|
|
|
|
if (tl1 == NULL && tl2 == NULL)
|
|
|
|
return order;
|
2004-07-14 09:17:49 +01:00
|
|
|
if (tl1 == NULL)
|
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-08-12 10:28:50 +01:00
|
|
|
|
|
|
|
//! Iterate over terms in termlist
|
|
|
|
/**
|
|
|
|
* Function gets terms
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
termlist_iterate (Termlist tl, int (*func) ())
|
|
|
|
{
|
|
|
|
while (tl != NULL)
|
|
|
|
{
|
|
|
|
if (!func (tl->term))
|
|
|
|
return 0;
|
|
|
|
tl = tl->next;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
2004-08-26 13:36:01 +01:00
|
|
|
|
|
|
|
//! Create a tuple term from a termlist
|
2004-09-20 13:40:01 +01:00
|
|
|
Term
|
|
|
|
termlist_to_tuple (Termlist tl)
|
2004-08-26 13:36:01 +01:00
|
|
|
{
|
|
|
|
int width;
|
|
|
|
|
|
|
|
width = termlistLength (tl);
|
|
|
|
if (width > 1)
|
|
|
|
{
|
|
|
|
// 2 parts
|
|
|
|
// Make two termlists for each side.
|
|
|
|
Term tresult;
|
|
|
|
Termlist tl1, tl2;
|
|
|
|
int split, i;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This can be done much more efficiently by cutting
|
|
|
|
* the list temporarily, and reconnecting it afterwards.
|
|
|
|
*/
|
|
|
|
tl1 = NULL;
|
|
|
|
tl2 = NULL;
|
|
|
|
split = width / 2;
|
|
|
|
i = 0;
|
|
|
|
while (tl != NULL)
|
|
|
|
{
|
|
|
|
if (i < split)
|
2004-09-20 13:40:01 +01:00
|
|
|
tl1 = termlistAdd (tl1, tl->term);
|
2004-08-26 13:36:01 +01:00
|
|
|
else
|
2004-09-20 13:40:01 +01:00
|
|
|
tl2 = termlistAdd (tl2, tl->term);
|
2004-08-26 13:36:01 +01:00
|
|
|
tl = tl->next;
|
|
|
|
i++;
|
|
|
|
}
|
2004-09-20 13:40:01 +01:00
|
|
|
tresult =
|
|
|
|
makeTermTuple (termlist_to_tuple (tl1), termlist_to_tuple (tl2));
|
2004-08-26 13:36:01 +01:00
|
|
|
termlistDelete (tl1);
|
|
|
|
termlistDelete (tl2);
|
|
|
|
return tresult;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (tl == NULL)
|
|
|
|
{
|
|
|
|
// W00t! Wtf?
|
|
|
|
error ("termlist_to_tuple called (internally?) with NULL");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Single node, simple
|
2004-09-20 13:40:01 +01:00
|
|
|
return termDuplicate (tl->term);
|
2004-08-26 13:36:01 +01:00
|
|
|
}
|
|
|
|
}
|
2007-01-06 14:45:29 +00:00
|
|
|
// @TODO Should be considered an error
|
|
|
|
return NULL;
|
2004-08-26 13:36:01 +01:00
|
|
|
}
|
2005-10-07 15:02:46 +01:00
|
|
|
|
|
|
|
//! Split a tuple term into termlist components.
|
|
|
|
Termlist
|
|
|
|
tuple_to_termlist (Term t)
|
|
|
|
{
|
|
|
|
t = deVar (t);
|
|
|
|
if (t == NULL)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (realTermTuple (t))
|
|
|
|
{
|
|
|
|
return termlistConcat (tuple_to_termlist (TermOp1 (t)),
|
|
|
|
tuple_to_termlist (TermOp2 (t)));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return termlistAdd (NULL, t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-10-08 20:56:04 +01:00
|
|
|
|
|
|
|
//! Remove all items from tlbig that occur in tlsmall, and return the pointer to the new tlbig.
|
|
|
|
Termlist
|
|
|
|
termlistMinusTermlist (const Termlist tlbig, const Termlist tlsmall)
|
|
|
|
{
|
|
|
|
Termlist tl;
|
|
|
|
Termlist tlnewstart;
|
|
|
|
|
|
|
|
tl = tlbig;
|
|
|
|
tlnewstart = tlbig;
|
|
|
|
while (tl != NULL)
|
|
|
|
{
|
|
|
|
if (inTermlist (tlsmall, tl->term))
|
|
|
|
{
|
|
|
|
Termlist tlnext;
|
|
|
|
|
|
|
|
// Remember next node.
|
|
|
|
tlnext = tl->next;
|
|
|
|
// This node should be removed.
|
|
|
|
tlnewstart = termlistDelTerm (tl);
|
|
|
|
// Skip to next.
|
|
|
|
tl = tlnext;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// This item will remain in the list.
|
2006-01-02 16:07:56 +00:00
|
|
|
tl = tl->next;
|
2005-10-08 20:56:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return tlnewstart;
|
|
|
|
}
|