BOOL rpos_ContEqual(CONTEXT C1, TERM T1, CONTEXT C2, TERM T2) /************************************************************** INPUT: Two contexts and two terms. RETURNS: TRUE, if <T1> is equal to <T2> and FALSE otherwise. EFFECT: Variable bindings are considered. ***************************************************************/ { LIST l1, l2; T1 = cont_Deref(&C1, T1); T2 = cont_Deref(&C2, T2); if (!term_EqualTopSymbols(T1, T2)) return FALSE; else if (!term_IsComplex(T1)) return TRUE; else { if (symbol_HasProperty(term_TopSymbol(T1), ORDMUL)) { l1 = rpos_ContMultisetDifference(C1, T1, C2, T2); if (list_Empty(l1)) return TRUE; else { list_Delete(l1); return FALSE; } } else { /* LEX case */ for (l1 = term_ArgumentList(T1), l2 = term_ArgumentList(T2); !list_Empty(l1) && rpos_ContEqual(C1,list_Car(l1),C2,list_Car(l2)); l1 = list_Cdr(l1), l2 = list_Cdr(l2)); /* empty body */ return list_Empty(l1); /* All arguments were equal */ } } }
static TERM cont_CopyAndApplyIndexVariableBindings(const CONTEXT Context, TERM Term) { SYMBOL TermTop; #ifdef CHECK if (symbol_IsIndexVariable(term_TopSymbol(Term)) && !cont_VarIsBound(Context, term_TopSymbol(Term))) { misc_StartErrorReport(); misc_ErrorReport("\n In cont_CopyAndApplyIndexVariableBindings:"); misc_ErrorReport(" Expected bound index variable."); misc_FinishErrorReport(); } #endif TermTop = term_TopSymbol(Term); while (symbol_IsIndexVariable(TermTop)) { if (cont_VarIsBound(Context, TermTop)) { Term = cont_ContextBindingTerm(Context, TermTop); TermTop = term_TopSymbol(Term); } } if (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_CopyAndApplyIndexVariableBindings(Context, list_Car(Scan))); return term_Create(TermTop, ArgumentList); } else return term_Create(TermTop, list_Nil()); }
BOOL rpos_Equal(TERM T1, TERM T2) /************************************************************** INPUT: Two terms. RETURNS: TRUE, if <T1> is equal to <T2> and FALSE otherwise. ***************************************************************/ { LIST l1, l2; if (!term_EqualTopSymbols(T1, T2)) return FALSE; else if (!term_IsComplex(T1)) /* Equal variable or constant */ return TRUE; else { if (symbol_HasProperty(term_TopSymbol(T1), ORDMUL)) { /* MUL case */ l1 = rpos_MultisetDifference(T1, T2); if (list_Empty(l1)) return TRUE; else { list_Delete(l1); return FALSE; } } else { /* LEX case */ for (l1 = term_ArgumentList(T1), l2 = term_ArgumentList(T2); !list_Empty(l1) && rpos_Equal(list_Car(l1), list_Car(l2)); l1 = list_Cdr(l1), l2 = list_Cdr(l2)) /* empty */; return list_Empty(l1); /* All arguments were equal */ } } }
void cont_TermPrintPrefix(CONTEXT GlobalContext, CONTEXT TermContext, TERM Term) /************************************************************** INPUT: A global context where index variables are bound, a context and a term. RETURNS: none. SUMMARY: Prints the term modulo the context to stdout. CAUTION: Variables of <Term1> are bound in <TermContext1> and the index variables are bound in <GlobalContext1> ***************************************************************/ { Term = cont_Deref(GlobalContext,&TermContext, Term); symbol_Print(term_TopSymbol(Term)); if (term_IsComplex(Term)) { LIST List; putchar('('); for (List = term_ArgumentList(Term); !list_Empty(List); List = list_Cdr(List)) { cont_TermPrintPrefix(GlobalContext, TermContext, list_Car(List)); if (!list_Empty(list_Cdr(List))) putchar(','); } putchar(')'); } }
TERM cont_CopyAndApplyBindings(CONTEXT TermContext, TERM Term) { while (term_IsVariable(Term)) { SYMBOL TermTop; TermTop = term_TopSymbol(Term); if (cont_VarIsBound(TermContext, TermTop)) { CONTEXT HelpContext; HelpContext = cont_ContextBindingContext(TermContext, TermTop); Term = cont_ContextBindingTerm(TermContext, TermTop); TermContext = HelpContext; } else break; } if (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); } else return term_Create(term_TopSymbol(Term), list_Nil()); }
void cont_TermPrintPrefix(CONTEXT Context, TERM Term) /************************************************************** INPUT: A context and a term. RETURNS: none. SUMMARY: Prints the term modulo the context to stdout. CAUTION: none. ***************************************************************/ { Term = cont_Deref(&Context, Term); symbol_Print(term_TopSymbol(Term)); if (term_IsComplex(Term)) { LIST List; putchar('('); for (List = term_ArgumentList(Term); !list_Empty(List); List = list_Cdr(List)) { cont_TermPrintPrefix(Context, list_Car(List)); if (!list_Empty(list_Cdr(List))) putchar(','); } putchar(')'); } }
TERM cont_CopyAndApplyBindingsCom(const CONTEXT Context, TERM Term) { while (term_IsVariable(Term) && cont_VarIsBound(Context, term_TopSymbol(Term))) Term = cont_ContextBindingTerm(Context, term_TopSymbol(Term)); if (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_CopyAndApplyBindingsCom(Context, list_Car(Scan))); return term_Create(term_TopSymbol(Term), ArgumentList); } else return term_Create(term_TopSymbol(Term), list_Nil()); }
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; }
static int kbo_CompVarCondAndWeight(TERM Term1, BOOL *VarCond1, TERM Term2, BOOL *VarCond2) /************************************************************** INPUT: Two terms and two pointers to booleans. EFFECT: Sets the booleans with respect to the kbo variable condition. Computes the kbo weight difference. ***************************************************************/ { SYMBOL MaxVar1,MaxVar2; TERM Term; LIST Scan; int i,Stack,Weight; *VarCond1 = *VarCond2 = TRUE; MaxVar1 = term_MaxVar(Term1); MaxVar2 = term_MaxVar(Term2); Stack = stack_Bottom(); Weight = 0; if (MaxVar1 < MaxVar2) MaxVar1 = MaxVar2; for (i = 0; i <= MaxVar1; i++) { ord_VARCOUNT[i][0] = 0; ord_VARCOUNT[i][1] = 0; } Term = Term1; if (term_IsStandardVariable(Term)) { ord_VARCOUNT[term_TopSymbol(Term)][0]++; Weight += kbo_MINWEIGHT; } else { Weight += symbol_Weight(term_TopSymbol(Term)); if (term_IsComplex(Term)) stack_Push(term_ArgumentList(Term)); } while (!stack_Empty(Stack)) { Scan = stack_Top(); Term = (TERM)list_Car(Scan); stack_RplacTop(list_Cdr(Scan)); if (term_IsStandardVariable(Term)) { Weight += kbo_MINWEIGHT; ord_VARCOUNT[term_TopSymbol(Term)][0]++; } else { Weight += symbol_Weight(term_TopSymbol(Term)); if (term_IsComplex(Term)) stack_Push(term_ArgumentList(Term)); } while (!stack_Empty(Stack) && list_Empty(stack_Top())) stack_Pop(); } Term = Term2; if (term_IsStandardVariable(Term)) { Weight -= kbo_MINWEIGHT; ord_VARCOUNT[term_TopSymbol(Term)][1]++; } else { Weight -= symbol_Weight(term_TopSymbol(Term)); if (term_IsComplex(Term)) stack_Push(term_ArgumentList(Term)); } while (!stack_Empty(Stack)) { Scan = stack_Top(); Term = (TERM)list_Car(Scan); stack_RplacTop(list_Cdr(Scan)); if (term_IsStandardVariable(Term)) { Weight -= kbo_MINWEIGHT; ord_VARCOUNT[term_TopSymbol(Term)][1]++; } else { Weight -= symbol_Weight(term_TopSymbol(Term)); if (term_IsComplex(Term)) stack_Push(term_ArgumentList(Term)); } while (!stack_Empty(Stack) && list_Empty(stack_Top())) stack_Pop(); } for (i = 0; i <= MaxVar1; i++) { if (ord_VARCOUNT[i][0] < ord_VARCOUNT[i][1]) { *VarCond1 = FALSE; if (!*VarCond2) return Weight; } if (ord_VARCOUNT[i][0] > ord_VARCOUNT[i][1]) { *VarCond2 = FALSE; if (!*VarCond1) return Weight; } } return Weight; }
BOOL cont_TermEqualModuloBindings(CONTEXT IndexContext, CONTEXT CtL, TERM TermL, CONTEXT CtR, TERM TermR) /********************************************************* INPUT: Two contexts, two terms. RETURNS: The boolean value TRUE if the terms are equal. CAUTION: EQUAL FUNCTION- OR PREDICATE SYMBOLS SHARE THE SAME ARITY. THIS IS NOT VALID FOR JUNCTORS! *******************************************************/ { #ifdef CHECK if (!(term_IsTerm(TermL) && term_IsTerm(TermR))) { misc_StartErrorReport(); misc_ErrorReport("\n In cont_TermEqualModuloBindings: Input terms are corrupted.\n"); misc_FinishErrorReport(); } #endif while (term_IsVariable(TermL)) { SYMBOL TermTop; TermTop = term_TopSymbol(TermL); if (symbol_IsIndexVariable(TermTop)) CtL = IndexContext; else if (CtL == cont_InstanceContext()) break; if (cont_VarIsBound(CtL, TermTop)) { CONTEXT CHelp; CHelp = cont_ContextBindingContext(CtL, TermTop); TermL = cont_ContextBindingTerm(CtL, TermTop); CtL = CHelp; } else break; } while (term_IsVariable(TermR)) { SYMBOL TermTop; TermTop = term_TopSymbol(TermR); if (symbol_IsIndexVariable(TermTop)) CtR = IndexContext; else if (CtR == cont_InstanceContext()) break; if (cont_VarIsBound(CtR, TermTop)) { CONTEXT CHelp; CHelp = cont_ContextBindingContext(CtR, TermTop); TermR = cont_ContextBindingTerm(CtR, TermTop); CtR = CHelp; } else break; } if (!term_EqualTopSymbols(TermL, TermR)) return FALSE; else if (term_IsVariable(TermL)) { if (CtL == CtR) return TRUE; else return FALSE; } else if (term_IsComplex(TermL)) { LIST ScanL, ScanR; for (ScanL=term_ArgumentList(TermL), ScanR=term_ArgumentList(TermR); list_Exist(ScanL) && list_Exist(ScanR); ScanL=list_Cdr(ScanL), ScanR=list_Cdr(ScanR)) if (!cont_TermEqualModuloBindings(IndexContext, CtL, list_Car(ScanL), CtR, list_Car(ScanR))) return FALSE; return (list_Empty(ScanL) ? list_Empty(ScanR) : FALSE); } else return TRUE; }