TERM cont_ApplyBindingsModuloMatching(const CONTEXT Context, TERM Term, BOOL VarCheck) /********************************************************** INPUT: A context, a term, and a boolean flag. RETURNS: <Term> is destructively changed with respect to established bindings in the context. If <VarCheck> is true, all variables in <Term> must be bound in the context. When compiled with "CHECK" on, this condition is in fact checked. This function only makes sense after a matching operation. ***********************************************************/ { TERM RplacTerm; LIST Arglist; SYMBOL Top; #ifdef CHECK if (VarCheck && symbol_IsVariable(term_TopSymbol(Term)) && !cont_VarIsBound(Context, term_TopSymbol(Term))) { misc_StartErrorReport(); misc_ErrorReport("\n In cont_ApplyBindingsModuloMatching:"); misc_ErrorReport(" Used in forbidden context.\n"); misc_FinishErrorReport(); } #endif Top = term_TopSymbol(Term); if (symbol_IsVariable(Top)) { if (cont_VarIsBound(Context, Top)) { RplacTerm = cont_ContextBindingTerm(Context, Top); Arglist = term_CopyTermList(term_ArgumentList(RplacTerm)); term_RplacTop(Term, term_TopSymbol(RplacTerm)); term_DeleteTermList(term_ArgumentList(Term)); term_RplacArgumentList(Term, Arglist); } } else { for (Arglist = term_ArgumentList(Term); !list_Empty(Arglist); Arglist = list_Cdr(Arglist)) cont_ApplyBindingsModuloMatching(Context, list_Car(Arglist), VarCheck); } return Term; }
void symbol_Delete(SYMBOL Symbol) /************************************************************** INPUT: A symbol. RETURNS: Nothing. SUMMARY: Deletes the symbol from the symbol table and frees its memory. ***************************************************************/ { #ifdef CHECK if (!symbol_SignatureExists()) { misc_StartErrorReport(); misc_ErrorReport("\n In symbol_Delete: Module was initialized without signature.\n"); misc_FinishErrorReport(); } if (!symbol_IsSymbol(Symbol)) { misc_StartErrorReport(); misc_ErrorReport("\n In symbol_Delete: Illegal input.\n"); misc_FinishErrorReport(); } #endif if (!symbol_IsVariable(Symbol)) { int Index; SIGNATURE Entry; Index = symbol_Index(Symbol); symbol_FREEDSYMBOLS = list_Cons((POINTER)Index,symbol_FREEDSYMBOLS); Entry = symbol_Signature(Index); symbol_SetSignature(Index, NULL); symbol_FreeSignature(Entry); } }
static __inline__ int table_Index(SYMBOL symbol) { if (symbol_IsVariable(symbol)) return -(int) symbol; else return symbol_Index(symbol); }
TERM cont_ApplyBindingsModuloMatchingReverse(const CONTEXT Context, TERM Term) /********************************************************** INPUT: A term. RETURNS: <Term> is destructively changed with respect to established bindings in the leftmost context. This function only make sense after a matching operation (reverse). ***********************************************************/ { TERM RplacTerm; LIST Arglist; SYMBOL Top; #ifdef CHECK if (symbol_IsVariable(term_TopSymbol(Term)) && !cont_VarIsBound(Context, term_TopSymbol(Term))) { misc_StartErrorReport(); misc_ErrorReport("\n In cont_ApplyBindingsModuloMatchingReverse:"); misc_ErrorReport(" Used in forbidden context.\n"); misc_FinishErrorReport(); } #endif Top = term_TopSymbol(Term); if (symbol_IsVariable(Top)) { if (cont_VarIsBound(Context, Top)) { RplacTerm = cont_CopyAndApplyIndexVariableBindings(Context, cont_ContextBindingTerm(Context, Top)); term_RplacTop(Term, term_TopSymbol(RplacTerm)); term_DeleteTermList(term_ArgumentList(Term)); term_RplacArgumentList(Term, term_ArgumentList(RplacTerm)); term_Free(RplacTerm); } } else { for (Arglist = term_ArgumentList(Term); !list_Empty(Arglist); Arglist = list_Cdr(Arglist)) cont_ApplyBindingsModuloMatchingReverse(Context, list_Car(Arglist)); } return Term; }
BOOL symbol_IsSymbol(SYMBOL Symbol) /************************************************************** INPUT: A symbol. RETURNS: TRUE if the symbols is a variable or contained in the symbol table. ***************************************************************/ { return (!symbol_SignatureExists() || ((!symbol_Equal(Symbol, symbol__NULL)) && ((symbol_IsVariable(Symbol) && Symbol<symbol_MaxVars()) || (symbol_IsSignature(Symbol) && symbol_Index(Symbol)<symbol_ACTINDEX)))); }
void symbol_FPrintOtter(FILE* File, SYMBOL Symbol) /************************************************************** INPUT: A file and a symbol. RETURNS: None. SUMMARY: Prints a symbol in Otter format to stdout. ***************************************************************/ { #ifdef CHECK if (!symbol_IsSymbol(Symbol)) { misc_StartErrorReport(); misc_ErrorReport("\n In symbol_FPrintOtter: Illegal input.\n"); misc_FinishErrorReport(); } #endif if (symbol_IsVariable(Symbol)) { SYMBOL NormSymbol; NormSymbol = symbol_NormVar(Symbol); if (symbol_IsStandardVariable(Symbol)) { if (Symbol <= 6) /* u, v, w, x, y, z */ sprintf(symbol_VARSTRING,"%c", 116 + NormSymbol); else /* x1, x2, x3, ... */ sprintf(symbol_VARSTRING,"x%d", NormSymbol - 6); } else if (symbol_IsIndexVariable(Symbol)) /* I1, I2, I3, ... */ sprintf(symbol_VARSTRING,"I%d", NormSymbol); fputs(symbol_VARSTRING, File); } else if (symbol_SignatureExists()) { if (symbol_IsConstant(Symbol)) fprintf(File, "c%s", symbol_Name(Symbol)); else if (symbol_IsFunction(Symbol)) fprintf(File, "f%s", symbol_Name(Symbol)); else if (symbol_IsPredicate(Symbol)) fprintf(File, "P%s", symbol_Name(Symbol)); else fputs(symbol_Name(Symbol), File); } else fprintf(File, "%d", Symbol); }
BOOL cont_BindingsAreRenamingModuloMatching(const CONTEXT RenamingContext) { CONTEXT Context; #ifdef CHECK if (!cont_IsContextEmpty(RenamingContext)) { misc_StartErrorReport(); misc_ErrorReport("\n In cont_BindingsAreRenamingModuloMatching:"); misc_ErrorReport(" Renaming context contains bindings.\n"); misc_FinishErrorReport(); } #endif cont_StartBinding(); Context = cont_LastBinding(); while (Context) { if (!symbol_IsIndexVariable(cont_BindingSymbol(Context))) { SYMBOL CodomainSymbol; CodomainSymbol = term_TopSymbol(cont_BindingTerm(Context)); if (symbol_IsVariable(CodomainSymbol)) { if (cont_VarIsRenamed(RenamingContext, CodomainSymbol)) { cont_BackTrack(); return FALSE; } else { cont_CreateBinding(RenamingContext, CodomainSymbol, NULL, NULL); cont_SetContextBindingRenaming(RenamingContext, CodomainSymbol, CodomainSymbol); } } else { cont_BackTrack(); return FALSE; } } Context = cont_BindingLink(Context); } cont_BackTrack(); return TRUE; }
void symbol_FPrint(FILE* File, SYMBOL Symbol) /************************************************************** INPUT: A file and a symbol. RETURNS: None. SUMMARY: Prints a symbol to the file. ***************************************************************/ { #ifdef CHECK if (!symbol_IsSymbol(Symbol)) { misc_StartErrorReport(); misc_ErrorReport("\n In symbol_FPrint: Illegal input.\n"); misc_FinishErrorReport(); } #endif if (symbol_Equal(symbol_Null(),Symbol)) fputs("NULL", File); else if (symbol_IsVariable(Symbol)) { SYMBOL NormSymbol; NormSymbol = symbol_NormVar(Symbol); if (symbol_IsStandardVariable(Symbol)) { if (Symbol <= 6) /* U, V, W, X, Y, Z */ sprintf(symbol_VARSTRING,"%c", 'U' + NormSymbol - 1); else /* X1, X2, X3, ... */ sprintf(symbol_VARSTRING,"X%d", NormSymbol - 6); } else if (symbol_IsIndexVariable(Symbol)) /* I1, I2, I3, ... */ sprintf(symbol_VARSTRING,"I%d", NormSymbol); fputs(symbol_VARSTRING, File); } else if (symbol_SignatureExists()) fputs(symbol_Name(Symbol), File); else fprintf(File, "%d", Symbol); }
TERM cont_SymbolApplyBindings(CONTEXT TermContext, SYMBOL Symbol) /************************************************************** INPUT: A call-by-ref context and a variable symbol. RETURNS: The recursively dereferenced term and the corresponding context, NULL if the symbol is not bound SUMMARY: Dereferences bindings of variables. CAUTION: In general, the context of the returned term is different to the input context. ***************************************************************/ { TERM Term; Term = (TERM)NULL; while (symbol_IsVariable(Symbol)) { if (cont_VarIsBound(TermContext, Symbol)) { CONTEXT HelpContext; HelpContext = cont_ContextBindingContext(TermContext, Symbol); Term = cont_ContextBindingTerm(TermContext, Symbol); TermContext = HelpContext; Symbol = term_TopSymbol(Term); } else break; } if (Term != (TERM)NULL && term_IsComplex(Term)) { LIST Scan, ArgumentList; for (Scan = ArgumentList = list_Copy(term_ArgumentList(Term)); !list_Empty(Scan); Scan = list_Cdr(Scan)) list_Rplaca(Scan, cont_CopyAndApplyBindings(TermContext, list_Car(Scan))); return term_Create(term_TopSymbol(Term), ArgumentList); } return Term; }