2007-06-11 13:01:04 +01:00
|
|
|
/*
|
|
|
|
* Scyther : An automatic verifier for security protocols.
|
2012-04-24 12:56:51 +01:00
|
|
|
* Copyright (C) 2007-2012 Cas Cremers
|
2007-06-11 13:01:04 +01:00
|
|
|
*
|
|
|
|
* 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-05-15 15:22:44 +01:00
|
|
|
/**
|
|
|
|
*@file knowledge.c
|
|
|
|
*\brief Procedures concerning knowledge structures.
|
|
|
|
*
|
|
|
|
* The main issue of this code is to maintain the minimal property of the knowledge set.
|
|
|
|
*/
|
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"
|
2004-04-23 11:58:43 +01:00
|
|
|
#include "knowledge.h"
|
2004-07-24 16:08:35 +01:00
|
|
|
#include "system.h"
|
2004-04-23 11:58:43 +01:00
|
|
|
#include "debug.h"
|
2007-01-06 14:45:29 +00:00
|
|
|
#include "error.h"
|
2013-04-26 10:36:41 +01:00
|
|
|
#include "specialterm.h"
|
2004-04-23 11:58:43 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Knowledge stuff
|
|
|
|
*/
|
|
|
|
|
2004-05-15 15:22:44 +01:00
|
|
|
//! Open knowledge code.
|
2004-04-23 11:58:43 +01:00
|
|
|
void
|
|
|
|
knowledgeInit (void)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-05-15 15:22:44 +01:00
|
|
|
//! Close knowledge code.
|
2004-04-23 11:58:43 +01:00
|
|
|
void
|
|
|
|
knowledgeDone (void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2004-05-15 15:22:44 +01:00
|
|
|
//! Allocate a fresh memory block the size of a knowledge struct.
|
|
|
|
/**
|
|
|
|
* Memory will not be initialized.
|
|
|
|
*@return Pointer to a fresh memory block.
|
|
|
|
*/
|
2004-04-23 11:58:43 +01:00
|
|
|
Knowledge
|
|
|
|
makeKnowledge ()
|
|
|
|
{
|
2006-03-08 13:58:46 +00:00
|
|
|
return (Knowledge) malloc (sizeof (struct knowledge));
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
|
2004-05-15 15:22:44 +01:00
|
|
|
//! Create a new empty knowledge structure.
|
|
|
|
/**
|
|
|
|
*@return Pointer to an empty knowledge structure.
|
|
|
|
*/
|
2004-04-23 11:58:43 +01:00
|
|
|
Knowledge
|
|
|
|
emptyKnowledge ()
|
|
|
|
{
|
|
|
|
Knowledge know;
|
|
|
|
|
|
|
|
know = makeKnowledge ();
|
|
|
|
know->basic = NULL;
|
|
|
|
know->encrypt = NULL;
|
2013-04-26 10:36:41 +01:00
|
|
|
know->inversekeys = NULL;
|
|
|
|
know->inversekeyfunctions = NULL;
|
2004-04-23 11:58:43 +01:00
|
|
|
know->vars = NULL;
|
2012-06-12 20:36:27 +01:00
|
|
|
know->publicfunctions = NULL;
|
2004-04-23 11:58:43 +01:00
|
|
|
return know;
|
|
|
|
}
|
|
|
|
|
2004-05-15 15:22:44 +01:00
|
|
|
//! Duplicate a knowledge structure.
|
|
|
|
/**
|
|
|
|
* Makes copies using termlistShallow() of knowledge::basic, knowledge::encrypt and
|
|
|
|
* knowledge::vars.
|
|
|
|
* For the inverses, only the pointer is copied.
|
|
|
|
*@param know The knowledge structure to be copied.
|
|
|
|
*@return A pointer to a new memory struct.
|
|
|
|
*\sa termlistShallow(), knowledgeDelete()
|
|
|
|
*/
|
2004-04-23 11:58:43 +01:00
|
|
|
Knowledge
|
|
|
|
knowledgeDuplicate (Knowledge know)
|
|
|
|
{
|
|
|
|
Knowledge newknow;
|
|
|
|
|
|
|
|
if (know == NULL)
|
|
|
|
{
|
2004-07-29 13:47:57 +01:00
|
|
|
warning ("Trying to copy empty knowledge.");
|
2004-04-23 11:58:43 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
newknow = makeKnowledge ();
|
|
|
|
newknow->basic = termlistShallow (know->basic);
|
|
|
|
newknow->encrypt = termlistShallow (know->encrypt);
|
|
|
|
newknow->vars = termlistShallow (know->vars);
|
2013-04-26 10:36:41 +01:00
|
|
|
newknow->inversekeys = know->inversekeys;
|
|
|
|
newknow->inversekeyfunctions = know->inversekeyfunctions;
|
2012-06-12 20:36:27 +01:00
|
|
|
newknow->publicfunctions = termlistShallow (know->publicfunctions);
|
2004-04-23 11:58:43 +01:00
|
|
|
return newknow;
|
|
|
|
}
|
|
|
|
|
2004-05-15 15:22:44 +01:00
|
|
|
//! Delete a knowledge set.
|
|
|
|
/**
|
|
|
|
* Typically used to destroy something made with knowledgeDuplicate().
|
|
|
|
*\sa knowledgeDuplicate()
|
|
|
|
*/
|
2004-04-23 11:58:43 +01:00
|
|
|
void
|
|
|
|
knowledgeDelete (Knowledge know)
|
|
|
|
{
|
|
|
|
if (know != NULL)
|
|
|
|
{
|
|
|
|
termlistDelete (know->basic);
|
|
|
|
termlistDelete (know->encrypt);
|
|
|
|
termlistDelete (know->vars);
|
2012-06-12 20:36:27 +01:00
|
|
|
termlistDelete (know->publicfunctions);
|
2006-03-08 13:58:46 +00:00
|
|
|
free (know);
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-15 15:22:44 +01:00
|
|
|
//! Destroy a knowledge set.
|
|
|
|
/**
|
|
|
|
* Unlike knowledgeDelete(), uses termlistDestroy() to remove knowledge::basic,
|
|
|
|
* knowledge::encrypt and knowledge::vars substructures.
|
|
|
|
*\sa knowledgeDelete()
|
|
|
|
*/
|
2004-04-23 11:58:43 +01:00
|
|
|
void
|
|
|
|
knowledgeDestroy (Knowledge know)
|
|
|
|
{
|
|
|
|
if (know != NULL)
|
|
|
|
{
|
|
|
|
termlistDestroy (know->basic);
|
|
|
|
termlistDestroy (know->encrypt);
|
|
|
|
termlistDestroy (know->vars);
|
2013-04-26 10:36:41 +01:00
|
|
|
// termlistDestroy(know->inversekeys);
|
|
|
|
// termlistDestroy(know->inversekeyfunctions);
|
2012-06-12 20:36:27 +01:00
|
|
|
termlistDestroy (know->publicfunctions);
|
2006-03-08 13:58:46 +00:00
|
|
|
free (know);
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-15 15:22:44 +01:00
|
|
|
//! Add a term to a knowledge set.
|
|
|
|
/**
|
|
|
|
*@param know The knowledge set.
|
|
|
|
*@param term The term to be added.
|
|
|
|
*@return True iff the term was actually new, and added.
|
2004-04-23 11:58:43 +01:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
knowledgeAddTerm (Knowledge know, Term term)
|
|
|
|
{
|
|
|
|
if (know == NULL)
|
|
|
|
{
|
2004-07-29 13:47:57 +01:00
|
|
|
warning ("Trying to add term to uninitialised (NULL) Know pointer.");
|
2004-04-23 11:58:43 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (term == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
term = deVar (term);
|
|
|
|
|
2004-06-13 22:15:14 +01:00
|
|
|
/* for tuples, simply recurse for components */
|
2004-04-23 11:58:43 +01:00
|
|
|
if (isTermTuple (term))
|
|
|
|
{
|
2004-06-13 22:15:14 +01:00
|
|
|
int status;
|
|
|
|
|
2004-11-16 12:07:55 +00:00
|
|
|
status = knowledgeAddTerm (know, TermOp1 (term));
|
|
|
|
return knowledgeAddTerm (know, TermOp2 (term)) || status;
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
|
2004-06-13 22:15:14 +01:00
|
|
|
/* test whether we knew it before */
|
|
|
|
if (inKnowledge (know, term))
|
|
|
|
return 0;
|
|
|
|
|
2004-04-23 11:58:43 +01:00
|
|
|
/* adding variables? */
|
|
|
|
know->vars = termlistAddVariables (know->vars, term);
|
|
|
|
|
|
|
|
knowledgeSimplify (know, term);
|
|
|
|
if (isTermLeaf (term))
|
|
|
|
{
|
|
|
|
know->basic = termlistAdd (know->basic, term);
|
|
|
|
}
|
|
|
|
if (term->type == ENCRYPT)
|
|
|
|
{
|
2013-04-26 10:36:41 +01:00
|
|
|
Term invkey = inverseKey (know, TermKey (term));
|
2004-04-23 11:58:43 +01:00
|
|
|
if (inKnowledge (know, invkey))
|
|
|
|
{
|
|
|
|
/* we can decrypt it */
|
2004-11-16 12:07:55 +00:00
|
|
|
knowledgeAddTerm (know, TermOp (term));
|
|
|
|
if (!inKnowledge (know, TermKey (term)))
|
2004-04-23 11:58:43 +01:00
|
|
|
{
|
|
|
|
/* we know the op now, but not the key, so add it anyway */
|
|
|
|
know->encrypt = termlistAdd (know->encrypt, term);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* we cannot decrypt it, and from the initial test we know we could not construct it */
|
|
|
|
know->encrypt = termlistAdd (know->encrypt, term);
|
|
|
|
}
|
|
|
|
termDelete (invkey);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-05-15 15:22:44 +01:00
|
|
|
//! Try to simplify knowledge based on a term.
|
|
|
|
/**
|
|
|
|
*@param know A knowledge set.
|
2004-06-13 21:58:54 +01:00
|
|
|
*@param key A key, i.e. it can decrypt anything that was encrypted with term^{-1}.
|
2004-05-15 15:22:44 +01:00
|
|
|
*/
|
2004-04-23 11:58:43 +01:00
|
|
|
void
|
|
|
|
knowledgeSimplify (Knowledge know, Term key)
|
|
|
|
{
|
|
|
|
Termlist tldecrypts = NULL;
|
|
|
|
Termlist scan = know->encrypt;
|
2013-04-26 10:36:41 +01:00
|
|
|
Term invkey = inverseKey (know, key);
|
2004-04-23 11:58:43 +01:00
|
|
|
|
|
|
|
while (scan != NULL)
|
|
|
|
{
|
2004-11-16 12:07:55 +00:00
|
|
|
if (isTermEqual (TermKey (scan->term), invkey))
|
2004-04-23 11:58:43 +01:00
|
|
|
{
|
2004-11-16 12:07:55 +00:00
|
|
|
tldecrypts = termlistAdd (tldecrypts, TermOp (scan->term));
|
2004-04-23 11:58:43 +01:00
|
|
|
know->encrypt = termlistDelTerm (scan);
|
|
|
|
scan = know->encrypt;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
scan = scan->next;
|
|
|
|
}
|
|
|
|
termDelete (invkey);
|
|
|
|
knowledgeAddTermlist (know, tldecrypts);
|
|
|
|
termlistDelete (tldecrypts);
|
|
|
|
}
|
|
|
|
|
2004-05-15 15:22:44 +01:00
|
|
|
//! Add a termlist to the knowledge.
|
2004-04-23 11:58:43 +01:00
|
|
|
/*
|
2004-05-15 15:22:44 +01:00
|
|
|
*@return True iff there was at least one new item.
|
|
|
|
*\sa knowledgeAddTerm()
|
2004-04-23 11:58:43 +01:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
knowledgeAddTermlist (Knowledge know, Termlist tl)
|
|
|
|
{
|
|
|
|
int flag = 0;
|
|
|
|
|
|
|
|
while (tl != NULL)
|
|
|
|
{
|
2013-04-26 10:36:41 +01:00
|
|
|
// Evil old fashioned code relies on lazy left-to-right parsing. Get rid of it.
|
2004-04-23 11:58:43 +01:00
|
|
|
flag = knowledgeAddTerm (know, tl->term) || flag;
|
|
|
|
tl = tl->next;
|
|
|
|
}
|
|
|
|
return flag;
|
|
|
|
}
|
|
|
|
|
2004-05-15 15:22:44 +01:00
|
|
|
//! Add an inverse pair to the knowledge
|
2004-04-23 11:58:43 +01:00
|
|
|
void
|
2013-04-26 10:36:41 +01:00
|
|
|
knowledgeAddInverseKeys (Knowledge know, Term t1, Term t2)
|
2004-04-23 11:58:43 +01:00
|
|
|
{
|
2013-04-26 10:36:41 +01:00
|
|
|
know->inversekeys = termlistAdd (know->inversekeys, t1);
|
|
|
|
know->inversekeys = termlistAdd (know->inversekeys, t2);
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
|
2013-04-26 10:36:41 +01:00
|
|
|
//! Add an inverse pair to the knowledge
|
2004-04-23 11:58:43 +01:00
|
|
|
void
|
2013-04-26 10:36:41 +01:00
|
|
|
knowledgeAddInverseKeyFunctions (Knowledge know, Term t1, Term t2)
|
2004-04-23 11:58:43 +01:00
|
|
|
{
|
2013-04-26 10:36:41 +01:00
|
|
|
know->inversekeyfunctions = termlistAdd (know->inversekeyfunctions, t1);
|
|
|
|
know->inversekeyfunctions = termlistAdd (know->inversekeyfunctions, t2);
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
|
2004-05-15 15:22:44 +01:00
|
|
|
//! Is a term a part of the knowledge?
|
|
|
|
/**
|
|
|
|
*@param know The knowledge set.
|
|
|
|
*@param term The term to be inferred.
|
|
|
|
*@return True iff the term can be inferred from the knowledge set.
|
|
|
|
*/
|
2004-04-23 11:58:43 +01:00
|
|
|
int
|
|
|
|
inKnowledge (const Knowledge know, Term term)
|
|
|
|
{
|
2004-05-26 13:17:09 +01:00
|
|
|
mindwipe (know, inKnowledge (know, term));
|
|
|
|
|
2004-04-23 11:58:43 +01:00
|
|
|
/* if there is no term, then it's okay 'fur sure' */
|
|
|
|
if (term == NULL)
|
|
|
|
return 1;
|
|
|
|
/* if there is a term, but no knowledge, we're in trouble */
|
|
|
|
if (know == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
term = deVar (term);
|
|
|
|
if (isTermLeaf (term))
|
|
|
|
{
|
|
|
|
return inTermlist (know->basic, term);
|
|
|
|
}
|
|
|
|
if (term->type == ENCRYPT)
|
|
|
|
{
|
|
|
|
return inTermlist (know->encrypt, term) ||
|
2004-11-16 12:07:55 +00:00
|
|
|
(inKnowledge (know, TermKey (term))
|
|
|
|
&& inKnowledge (know, TermOp (term)));
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
if (term->type == TUPLE)
|
|
|
|
{
|
|
|
|
return (inTermlist (know->encrypt, term) ||
|
2004-11-16 12:07:55 +00:00
|
|
|
(inKnowledge (know, TermOp1 (term)) &&
|
|
|
|
inKnowledge (know, TermOp2 (term))));
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
return 0; /* unrecognized term type, weird */
|
|
|
|
}
|
|
|
|
|
2004-05-15 15:22:44 +01:00
|
|
|
//! Print a knowledge set.
|
2004-04-23 11:58:43 +01:00
|
|
|
void
|
|
|
|
knowledgePrint (Knowledge know)
|
|
|
|
{
|
|
|
|
indent ();
|
|
|
|
if (know == NULL)
|
|
|
|
{
|
2004-07-29 13:47:57 +01:00
|
|
|
eprintf ("Empty.\n");
|
2004-04-23 11:58:43 +01:00
|
|
|
return;
|
|
|
|
}
|
2004-07-29 13:47:57 +01:00
|
|
|
eprintf (" [Basic]: ");
|
2004-04-23 11:58:43 +01:00
|
|
|
termlistPrint (know->basic);
|
2004-07-29 13:47:57 +01:00
|
|
|
eprintf ("\n");
|
2004-04-23 11:58:43 +01:00
|
|
|
indent ();
|
2004-07-29 13:47:57 +01:00
|
|
|
eprintf (" [Encrp]: ");
|
2004-04-23 11:58:43 +01:00
|
|
|
termlistPrint (know->encrypt);
|
2004-07-29 13:47:57 +01:00
|
|
|
eprintf ("\n");
|
2004-04-23 11:58:43 +01:00
|
|
|
indent ();
|
2004-07-29 13:47:57 +01:00
|
|
|
eprintf (" [Vars]: ");
|
2004-04-23 11:58:43 +01:00
|
|
|
termlistPrint (know->vars);
|
2004-07-29 13:47:57 +01:00
|
|
|
eprintf ("\n");
|
2013-04-26 10:36:41 +01:00
|
|
|
eprintf (" [Inversekeys]: ");
|
|
|
|
termlistPrint (know->inversekeys);
|
|
|
|
eprintf ("\n");
|
|
|
|
eprintf (" [Inversekeyfunctions]: ");
|
|
|
|
termlistPrint (know->inversekeyfunctions);
|
|
|
|
eprintf ("\n");
|
|
|
|
eprintf (" [Publicfunctions]: ");
|
|
|
|
termlistPrint (know->publicfunctions);
|
|
|
|
eprintf ("\n");
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
|
2004-08-06 12:59:27 +01:00
|
|
|
//! Print a knowledge set, short version (no newline)
|
|
|
|
void
|
|
|
|
knowledgePrintShort (const Knowledge know)
|
|
|
|
{
|
|
|
|
indent ();
|
|
|
|
if (know == NULL)
|
|
|
|
{
|
|
|
|
eprintf ("Empty");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (know->basic != NULL)
|
|
|
|
{
|
|
|
|
termlistPrint (know->basic);
|
|
|
|
if (know->encrypt != NULL);
|
2004-08-09 11:05:58 +01:00
|
|
|
{
|
|
|
|
eprintf (", ");
|
|
|
|
}
|
2004-08-06 12:59:27 +01:00
|
|
|
}
|
|
|
|
if (know->encrypt != NULL)
|
|
|
|
{
|
|
|
|
termlistPrint (know->encrypt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-15 15:22:44 +01:00
|
|
|
//! Yield the set of representatives for the knowledge.
|
|
|
|
/**
|
|
|
|
* Note: this is a shallow copy, and needs to be termlistDelete'd.
|
|
|
|
*\sa termlistDelete()
|
2004-04-23 11:58:43 +01:00
|
|
|
*/
|
|
|
|
Termlist
|
2004-07-20 10:07:43 +01:00
|
|
|
knowledgeSet (const Knowledge know)
|
2004-04-23 11:58:43 +01:00
|
|
|
{
|
|
|
|
Termlist tl1, tl2;
|
|
|
|
|
|
|
|
tl1 = termlistShallow (know->basic);
|
|
|
|
tl2 = termlistShallow (know->encrypt);
|
|
|
|
return termlistConcat (tl1, tl2);
|
|
|
|
}
|
|
|
|
|
2013-04-26 10:36:41 +01:00
|
|
|
//! Check for elements in the knowledge set
|
|
|
|
int
|
|
|
|
inKnowledgeSet (const Knowledge know, Term t)
|
2004-04-23 11:58:43 +01:00
|
|
|
{
|
2013-04-26 10:36:41 +01:00
|
|
|
return (inTermlist (know->basic, t) || inTermlist (know->encrypt, t));
|
2004-04-23 11:58:43 +01:00
|
|
|
}
|
|
|
|
|
2004-05-15 15:22:44 +01:00
|
|
|
//! check whether any substitutions where made in a knowledge set.
|
|
|
|
/**
|
|
|
|
* Typically, when a substitution is made, a knowledge set has to be reconstructed.
|
|
|
|
* This procedure detects this by checking knowledge::vars.
|
|
|
|
*@return True iff an open variable was later closed by a substitution.
|
|
|
|
*\sa knowledgeReconstruction()
|
2004-04-23 11:58:43 +01:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
knowledgeSubstNeeded (const Knowledge know)
|
|
|
|
{
|
|
|
|
Termlist tl;
|
|
|
|
|
|
|
|
if (know == NULL)
|
|
|
|
return 0;
|
|
|
|
tl = know->vars;
|
|
|
|
while (tl != NULL)
|
|
|
|
{
|
|
|
|
if (tl->term->subst != NULL)
|
|
|
|
return 1;
|
|
|
|
tl = tl->next;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-06-12 20:36:27 +01:00
|
|
|
//! Add public function
|
|
|
|
void
|
|
|
|
knowledgeAddPublicFunction (const Knowledge know, const Term f)
|
|
|
|
{
|
|
|
|
know->publicfunctions = termlistAdd (know->publicfunctions, f);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Check public function
|
|
|
|
int
|
|
|
|
isKnowledgePublicFunction (const Knowledge know, const Term f)
|
|
|
|
{
|
|
|
|
return inTermlist (know->publicfunctions, f);
|
|
|
|
}
|
2013-04-26 10:36:41 +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.
|
|
|
|
*@return A pointer to a duplicate of the inverse key term. Use termDelete to remove it.
|
|
|
|
*\sa termDuplicate(), knowledge::inverses
|
|
|
|
*/
|
|
|
|
Term
|
|
|
|
inverseKey (Knowledge know, Term key)
|
|
|
|
{
|
|
|
|
Term f;
|
|
|
|
|
|
|
|
key = deVar (key);
|
|
|
|
|
|
|
|
/* inverse key function? */
|
|
|
|
f = getTermFunction (key);
|
|
|
|
if (f != NULL)
|
|
|
|
{
|
|
|
|
Termlist tl;
|
|
|
|
|
|
|
|
Term funKey (Term orig, Term f)
|
|
|
|
{
|
|
|
|
/* in: f'{op}, f
|
|
|
|
* out: f{op'} */
|
|
|
|
return makeTermFcall (termDuplicate (TermOp (orig)),
|
|
|
|
termDuplicate (f));
|
|
|
|
}
|
|
|
|
|
|
|
|
tl = know->inversekeyfunctions;
|
|
|
|
while (tl != NULL && tl->next != NULL)
|
|
|
|
{
|
|
|
|
if (isTermEqual (TermKey (key), tl->term))
|
|
|
|
return funKey (key, tl->next->term);
|
|
|
|
if (isTermEqual (TermKey (key), tl->next->term))
|
|
|
|
return funKey (key, tl->term);
|
|
|
|
tl = tl->next->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* scanning for a direct inverse */
|
|
|
|
Termlist tl;
|
|
|
|
|
|
|
|
/* scan the list */
|
|
|
|
tl = know->inversekeys;
|
|
|
|
while (tl != NULL && tl->next != NULL)
|
|
|
|
{
|
|
|
|
if (isTermEqual (key, tl->term))
|
|
|
|
return termDuplicate (tl->next->term);
|
|
|
|
if (isTermEqual (key, tl->next->term))
|
|
|
|
return termDuplicate (tl->term);
|
|
|
|
tl = tl->next->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return termDuplicate (key); /* defaults to symmetrical */
|
|
|
|
}
|