ord_RESULT rpos_GreaterEqual(TERM T1, TERM T2, BOOL VarIsConst) /************************************************************** INPUT: Two terms. RETURNS: ord_GREATER_THAN if <T1> is greater than <T2> ord_EQUAL if both terms are equal ord_UNCOMPARABLE otherwise. CAUTION: The precedence from the order module is used to determine the precedence of symbols! If <VarIsConst> is set then variables are interpreted as constants with lowest precedence. They are ranked to each other using their variable index. ***************************************************************/ { LIST scan; if (term_IsVariable(T1)) { if (term_EqualTopSymbols(T1, T2)) return ord_Equal(); /* T2 is the same variable */ else if(VarIsConst && term_IsVariable(T2)) { if(term_TopSymbol(T1) > term_TopSymbol(T2)) return ord_GreaterThan(); else return ord_Uncomparable(); } else /* A variable can't be greater than another term */ return ord_Uncomparable(); } else if (!VarIsConst && term_IsVariable(T2)) { /* T1 isn't a variable */ if (term_ContainsSymbol(T1, term_TopSymbol(T2))) return ord_GreaterThan(); else return ord_Uncomparable(); } else if(VarIsConst && term_IsVariable(T2)){ return ord_GreaterThan(); } else if (term_EqualTopSymbols(T1, T2)) { if (symbol_HasProperty(term_TopSymbol(T1), ORDMUL)) return rpos_MulGreaterEqual(T1, T2, VarIsConst); else return rpos_LexGreaterEqual(T1, T2, VarIsConst); } else { if (symbol_PrecedenceGreater(ord_PRECEDENCE, term_TopSymbol(T1), term_TopSymbol(T2))) { /* Different top symbols, symbol of T1 > symbol of T2. */ /* Try if T1 > each argument of T2. */ for (scan = term_ArgumentList(T2); !list_Empty(scan); scan = list_Cdr(scan)) if (!rpos_Greater(T1, list_Car(scan), VarIsConst)) return ord_Uncomparable(); return ord_GreaterThan(); } else { /* Try to find an argument of T1 that is >= T2 */ for (scan = term_ArgumentList(T1); !list_Empty(scan); scan = list_Cdr(scan)) if (!ord_IsUncomparable(rpos_GreaterEqual(list_Car(scan), T2, VarIsConst))) return ord_GreaterThan(); /* Argument of T1 >= T2 */ return ord_Uncomparable(); } } }
ord_RESULT rpos_ContGreaterEqual(CONTEXT C1, TERM T1, CONTEXT C2, TERM T2) /************************************************************** INPUT: Two contexts and two terms. RETURNS: ord_GREATER_THAN if <T1> is greater than <T2> ord_EQUAL if both terms are equal ord_UNCOMPARABLE otherwise. EFFECT: Variable bindings are considered. CAUTION: The precedence from the order module is used to determine the precedence of symbols! ***************************************************************/ { LIST scan; T1 = cont_Deref(&C1, T1); T2 = cont_Deref(&C2, T2); if (term_IsVariable(T1)) { if (term_EqualTopSymbols(T1, T2)) return ord_Equal(); /* T2 is the same variable */ else /* A variable can't be greater than another term */ return ord_Uncomparable(); } else if (term_IsVariable(T2)) { /* T1 isn't a variable */ if (cont_TermContainsSymbol(C1, T1, term_TopSymbol(T2))) return ord_GreaterThan(); else return ord_Uncomparable(); } else if (term_EqualTopSymbols(T1, T2)) { if (symbol_HasProperty(term_TopSymbol(T1), ORDMUL)) return rpos_ContMulGreaterEqual(C1, T1, C2, T2); else return rpos_ContLexGreaterEqual(C1, T1, C2, T2); } else { if (symbol_PrecedenceGreater(ord_PRECEDENCE, term_TopSymbol(T1), term_TopSymbol(T2))) { /* Different top symbols, symbol of T1 > symbol of T2. */ /* Try if T1 > each argument of T2. */ for (scan = term_ArgumentList(T2); !list_Empty(scan); scan = list_Cdr(scan)) if (!rpos_ContGreater(C1, T1, C2, list_Car(scan))) return ord_Uncomparable(); return ord_GreaterThan(); } else { /* Try to find an argument of T1 that is >= T2 */ for (scan = term_ArgumentList(T1); !list_Empty(scan); scan = list_Cdr(scan)) if (!ord_IsUncomparable(rpos_ContGreaterEqual(C1,list_Car(scan),C2,T2))) return ord_GreaterThan(); /* Argument of T1 >= T2 */ return ord_Uncomparable(); } } }
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()); }
TERM cont_Deref(CONTEXT GlobalContext, CONTEXT* TermContext, TERM Term) /****************************************************************** INPUT: A global context where the Index variables are bound, a term <Term> and a call-by-ref context for <Term>. RETURNS: The dereferenced term and the corresponding context. SUMMARY: Dereferences bindings of variables. CAUTION: In general, the context of the returned term <TermContext> is different to the input context. ASSUMPTION: All Index variables occuring in <Term> have to be bound in <GlobalContext>, no Index variable is mapped to another index variable *******************************************************************/ { if(term_IsIndexVariable(Term)) { SYMBOL TermTop; TermTop = term_TopSymbol(Term); #ifdef CHECK if(!cont_VarIsBound(GlobalContext, TermTop) || term_IsIndexVariable(cont_ContextBindingTerm(GlobalContext, TermTop))) { misc_StartErrorReport(); misc_ErrorReport("\ncont_Deref: Illegal Context!"); misc_FinishErrorReport(); } #endif Term = cont_ContextBindingTerm(GlobalContext, TermTop); *TermContext = cont_ContextBindingContext(GlobalContext, TermTop); } while (term_IsVariable(Term) && *TermContext != cont_InstanceContext()) { 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 return Term; } return Term; }
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()); }
static LIST red_GetTerminatorPartnerLits(TERM Atom, LITERAL Lit, BOOL UnitsOnly, LIST IndexList) /************************************************************** INPUT: An atom, a literal, a boolean flag and a list of SHARED_INDEXes. RETURNS: A list of literals with sign complementary to <Lit> that are unifiable with <Atom>. The literals are searched in all SHARED_INDEXes from <IndexList>. Additionally, if <Unitsonly> is true, only literals from unit clauses are returned. EFFECT: <Atom> is a copy of <Lit> where some substitution was applied and equality literals might have been swapped. <Lit> is just needed to check whether the unifiable literals are complementary. ***************************************************************/ { LIST Result, Unifiers, LitScan; LITERAL NextLit; Result = list_Nil(); for ( ; !list_Empty(IndexList); IndexList = list_Cdr(IndexList)) { Unifiers = st_GetUnifier(cont_LeftContext(), sharing_Index(list_Car(IndexList)), cont_RightContext(), Atom); for ( ; !list_Empty(Unifiers); Unifiers = list_Pop(Unifiers)) { if (!term_IsVariable(list_Car(Unifiers))) { for (LitScan = sharing_NAtomDataList(list_Car(Unifiers)); !list_Empty(LitScan); LitScan = list_Cdr(LitScan)) { NextLit = list_Car(LitScan); if (clause_LiteralsAreComplementary(Lit, NextLit) && (!UnitsOnly || clause_Length(clause_LiteralOwningClause(NextLit))==1)) /* The partner literals must have complementary sign and if <UnitsOnly> == TRUE they must be from unit clauses. */ Result = list_Cons(NextLit, Result); } } } } return Result; }
static LIST inf_GetURPartnerLits(TERM Atom, LITERAL Lit, BOOL Unit, SHARED_INDEX Index) /************************************************************** INPUT: An atom, a literal, a boolean flag and a SHARED_INDEX. RETURNS: A list of literals with sign complementary to <Lit> that are unifiable with <Atom>. If <Unit> is true, only literals from unit clauses are returned, if <Unit> is false, only literals from non-unit clauses are returned. EFFECT: <Atom> is a copy of <Lit>'s atom where some substitution was applied and equality literals might have been swapped. <Lit> is just needed to check whether the unifiable literals are complementary. ***************************************************************/ { LIST Result, Unifiers, LitScan; LITERAL PLit; int length; Result = list_Nil(); Unifiers = st_GetUnifier(cont_LeftContext(), sharing_Index(Index), cont_RightContext(), Atom); for ( ; !list_Empty(Unifiers); Unifiers = list_Pop(Unifiers)) { if (!term_IsVariable(list_Car(Unifiers))) { for (LitScan = sharing_NAtomDataList(list_Car(Unifiers)); !list_Empty(LitScan); LitScan = list_Cdr(LitScan)) { PLit = list_Car(LitScan); length = clause_Length(clause_LiteralOwningClause(PLit)); if (clause_LiteralsAreComplementary(Lit, PLit) && ((Unit && length==1) || (!Unit && length!=1))) /* The partner literals must have complementary sign and if <Unit> == TRUE they must be from unit clauses, if <Unit> == FALSE they must be from non-unit clauses. */ Result = list_Cons(PLit, Result); } } } return Result; }
static LIST ana_CalculateFunctionPrecedence(LIST Functions, LIST Clauses, FLAGSTORE Flags) /************************************************************** INPUT: A list of functions, a list of clauses and a flag store. RETURNS: A list of function symbols, which should be used for setting the symbol precedence. The list is sorted in descending order, that means function with highest precedence come first. EFFECT: Analyzes the clauses to build a directed graph G with function symbol as nodes. An edge (f,g) or in G means f should have lower precedence than g. An edge (f,g) or (g,f) is created if there's an equation equal(f(...), g(...)) in the clause list. The direction of the edge depends on the degree of the nodes and the symbol arity. Then find the strongly connected components of this graph. The "Ordering" flag will be set in the flag store. CAUTION: The value of "ana_PEQUATIONS" must be up to date. ***************************************************************/ { GRAPH graph; GRAPHNODE n1, n2; LIST result, scan, scan2, distrPairs; int i, j; SYMBOL s, Add, Mult; if (list_Empty(Functions)) return Functions; /* Problem contains no functions */ else if (!ana_PEQUATIONS) { Functions = list_NumberSort(Functions, (NAT (*)(POINTER)) symbol_PositiveArity); return Functions; } graph = graph_Create(); /* First create the nodes: one node for every function symbol. */ for (; !list_Empty(Functions); Functions = list_Pop(Functions)) graph_AddNode(graph, symbol_Index((SYMBOL)list_Car(Functions))); /* Now sort the node list wrt descending symbol arity. */ graph_SortNodes(graph, ana_NodeGreater); /* A list of pairs (add, multiply) of distributive symbols */ distrPairs = list_Nil(); /* Now add undirected edges: there's an undirected edge between */ /* two nodes if the symbols occur as top symbols in a positive */ /* equation. */ for (scan = Clauses; !list_Empty(scan); scan = list_Cdr(scan)) { CLAUSE c = list_Car(scan); for (i = clause_FirstSuccedentLitIndex(c); i <= clause_LastSuccedentLitIndex(c); i++) { if (clause_LiteralIsEquality(clause_GetLiteral(c, i))) { /* Consider only positive equations */ TERM t1, t2; if (fol_DistributiveEquation(clause_GetLiteralAtom(c,i), &Add, &Mult)) { /* Add a pair (Add, Mult) to <distrTerms> */ distrPairs = list_Cons(list_PairCreate((POINTER)Add, (POINTER)Mult), distrPairs); /*fputs("\nDISTRIBUTIVITY: ", stdout); term_PrintPrefix(clause_GetLiteralAtom(c,i)); fputs(" Add=", stdout); symbol_Print(Add); fputs(" Mult=", stdout); symbol_Print(Mult); fflush(stdout); DBG */ } t1 = term_FirstArgument(clause_GetLiteralAtom(c, i)); t2 = term_SecondArgument(clause_GetLiteralAtom(c, i)); if (!term_IsVariable(t1) && !term_IsVariable(t2) && !term_EqualTopSymbols(t1, t2) && /* No self loops! */ !term_HasSubterm(t1, t2) && /* No subterm property */ !term_HasSubterm(t2, t1)) { n1 = graph_GetNode(graph, symbol_Index(term_TopSymbol(t1))); n2 = graph_GetNode(graph, symbol_Index(term_TopSymbol(t2))); /* Create an undirected edge by adding two directed edges */ graph_AddEdge(n1, n2); graph_AddEdge(n2, n1); /* Use the node info for the degree of the node */ ana_IncNodeDegree(n1); ana_IncNodeDegree(n2); } } } } /* putchar('\n'); for (scan = graph_Nodes(graph); !list_Empty(scan); scan = list_Cdr(scan)) { n1 = list_Car(scan); printf("(%s,%d,%u), ", symbol_Name(symbol_GetSigSymbol(graph_NodeNumber(n1))), graph_NodeNumber(n1), ana_NodeDegree(n1)); } graph_Print(graph); fflush(stdout); DBG */ graph_DeleteDuplicateEdges(graph); /* Transform the undirected graph into a directed graph. */ for (scan = graph_Nodes(graph); !list_Empty(scan); scan = list_Cdr(scan)) { n1 = list_Car(scan); result = list_Nil(); /* Collect edges from n1 that shall be deleted */ for (scan2 = graph_NodeNeighbors(n1); !list_Empty(scan2); scan2 = list_Cdr(scan2)) { int a1, a2; n2 = list_Car(scan2); /* Get the node degrees in the undirected graph with multiple edges */ i = ana_NodeDegree(n1); j = ana_NodeDegree(n2); a1 = symbol_Arity(symbol_GetSigSymbol(graph_NodeNumber(n1))); a2 = symbol_Arity(symbol_GetSigSymbol(graph_NodeNumber(n2))); if (i > j || (i==j && a1 >= a2)) { /* symbol2 <= symbol1, so remove edge n1 -> n2 */ result = list_Cons(n2, result); } if (i < j || (i==j && a1 <= a2)) { /* symbol1 <= symbol2, so remove edge n2 -> n1 */ graph_DeleteEdge(n2, n1); } /* NOTE: If (i==j && a1==a2) both edges are deleted! */ } /* Now delete edges from n1 */ for ( ; !list_Empty(result); result = list_Pop(result)) graph_DeleteEdge(n1, list_Car(result)); } if (!list_Empty(distrPairs) && !ana_BidirectionalDistributivity(distrPairs)) { /* Enable RPO ordering, otherwise the default KBO will be used. */ flag_SetFlagIntValue(Flags, flag_ORD, flag_ORDRPOS); } /* Now examine the list of distribute symbols */ /* since they've highest priority. */ for ( ; !list_Empty(distrPairs); distrPairs = list_Pop(distrPairs)) { scan = list_Car(distrPairs); /* A pair (Add, Mult) */ /* Addition */ n1 = graph_GetNode(graph, symbol_Index((SYMBOL)list_PairFirst(scan))); /* Multiplication */ n2 = graph_GetNode(graph, symbol_Index((SYMBOL)list_PairSecond(scan))); /* Remove any edges between n1 and n2 */ graph_DeleteEdge(n1, n2); graph_DeleteEdge(n2, n1); /* Add one edge Addition -> Multiplication */ graph_AddEdge(n1, n2); list_PairFree(scan); } /* fputs("\n------------------------",stdout); graph_Print(graph); fflush(stdout); DBG */ /* Calculate the strongly connected components of the graph. */ /* <i> is the number of SCCs. */ i = graph_StronglyConnectedComponents(graph); /* Now create the precedence list by scanning the nodes. */ /* If there's a link between two strongly connected components */ /* c1 and c2 then component_num(c1) > component_num(c2), so the */ /* following code creates a valid precedence list in descending */ /* order. */ result = list_Nil(); while (i-- > 0) { /* for i = numberOfSCCs -1 dowto 0 */ for (scan = graph_Nodes(graph); !list_Empty(scan); scan = list_Cdr(scan)) { n1 = list_Car(scan); if (graph_NodeCompNum(n1) == i) { /* The symbol represented by the node <n> belongs to component <i> */ s = symbol_GetSigSymbol(graph_NodeNumber(n1)); result = list_Cons((POINTER)s, result); } } } /* putchar('\n'); for (scan = result; !list_Empty(scan); scan = list_Cdr(scan)) { s = (SYMBOL) list_Car(scan); symbol_Print(s); fputs(" > ", stdout); } putchar('\n'); fflush(stdout); DBG */ graph_Delete(graph); return result; }
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; }
ord_RESULT rpos_ContGreaterEqual(CONTEXT GlobalC1, CONTEXT TermC1, TERM T1, CONTEXT GlobalC2, CONTEXT TermC2, TERM T2, BOOL VarIsConst) /************************************************************** INPUT: Two contexts and two terms. RETURNS: ord_GREATER_THAN if <T1> is greater than <T2> ord_EQUAL if both terms are equal ord_UNCOMPARABLE otherwise. EFFECT: Variable bindings are considered. CAUTION: The precedence from the order module is used to determine the precedence of symbols! If <VarIsConst> is set then variables are interpreted as constants with lowest precedence. They are ranked to each other using their variable index. ASSUMPTION: All index variables of <T1> and <T2> are bound in <GlobalC1> and <GlobalCt2>, respectively ***************************************************************/ { LIST scan; T1 = cont_Deref(GlobalC1, &TermC1, T1); T2 = cont_Deref(GlobalC2, &TermC2, T2); if (term_IsVariable(T1)) { if (term_EqualTopSymbols(T1, T2)) return ord_Equal(); /* T2 is the same variable */ else /* A variable can't be greater than another term */ return ord_Uncomparable(); } else if (term_IsVariable(T2)) { /* T1 isn't a variable */ if (cont_TermContainsSymbol(GlobalC1, TermC1, T1, term_TopSymbol(T2))) return ord_GreaterThan(); else return ord_Uncomparable(); } else if (term_EqualTopSymbols(T1, T2)) { if (symbol_HasProperty(term_TopSymbol(T1), ORDMUL)) return rpos_ContMulGreaterEqual(GlobalC1, TermC1, T1, GlobalC2, TermC2, T2, VarIsConst); else return rpos_ContLexGreaterEqual(GlobalC1, TermC1, T1, GlobalC2, TermC2, T2, VarIsConst); } else { if (symbol_PrecedenceGreater(ord_PRECEDENCE, term_TopSymbol(T1), term_TopSymbol(T2))) { /* Different top symbols, symbol of T1 > symbol of T2. */ /* Try if T1 > each argument of T2. */ for (scan = term_ArgumentList(T2); !list_Empty(scan); scan = list_Cdr(scan)) if (!rpos_ContGreaterAux(GlobalC1, TermC1, T1, GlobalC2, TermC2, list_Car(scan), VarIsConst)) return ord_Uncomparable(); return ord_GreaterThan(); } else { /* Try to find an argument of T1 that is >= T2 */ for (scan = term_ArgumentList(T1); !list_Empty(scan); scan = list_Cdr(scan)) if (!ord_IsUncomparable(rpos_ContGreaterEqual(GlobalC1, TermC1,list_Car(scan), GlobalC2, TermC2,T2, VarIsConst))) return ord_GreaterThan(); /* Argument of T1 >= T2 */ return ord_Uncomparable(); } } }