BOOL cmdlne_SetFlags(FLAGSTORE flagstore) /**************************************************************** INPUT: a FLAGSTORE RETURNS: TRUE if all arguments of <cmdlne_ArgumentsList> are valid arguments and the respective flags of <flagstore> could be set and FALSE otherwise. EFFECT: Set flags of <flagstore> according to arguments stored in <cmdlne_ArgumentsList> *****************************************************************/ { int id, tk; LIST Scan; BOOL found; for(Scan=cmdlne_ArgumentsList; !list_Empty(Scan); Scan = list_Cdr(Scan)) { found = FALSE; for (id=0; id < flag_GetMaxFlag() && !found; id++) { if (!flag_IsUndefined(id) && string_Equal(flag_Name(id), (char*) list_PairFirst(list_Car(Scan))) ) { if (flag_IsOfValueType(id, flag_INTEGER)) { if(!string_StringIsInteger(list_PairSecond(list_Car(Scan)))){ misc_StartUserErrorReport(); misc_UserErrorReport("\nError: Argument of option %s must be an integer.\n\n", flag_Name(id)); misc_FinishUserErrorReport(); return FALSE; } tk = atoi((char*) list_PairSecond(list_Car(Scan))); flag_SetFlagIntValue(flagstore, id, tk); } else { flag_SetFlagStringValue(flagstore, id, string_StringCopy((char*)list_PairSecond(list_Car(Scan)))); } found = TRUE; } } if(!found) { misc_StartUserErrorReport(); misc_UserErrorReport("\n Unrecognized option %s\n\n", (char*) list_PairFirst(list_Car(Scan))); misc_FinishUserErrorReport(); return FALSE; } } return TRUE; }
BOOL cmdlne_SetArgument(const char* token, const char* value) /************************************************************** INPUT: two pointers RETURNS: TRUE if the argument <token> has not been added to the list of arguments before and FALSE otherwise. EFFECT: Adds argument <token> with value <value> to list of arguments (<cmdlne_ArgumentsList>) CAUTION: <value> has to be a character pointer pointing to a string representing an integer ***************************************************************/ { LIST Pair; LIST Scan; /* Check if Argument has already been defined */ for(Scan=cmdlne_ArgumentsList; !list_Empty(Scan); Scan = list_Cdr(Scan)) { if(string_Equal(list_PairFirst(list_Car(Scan)),token)) { misc_StartUserErrorReport(); misc_UserErrorReport("\n Option %s is multiply defined.\n\n", token); misc_FinishUserErrorReport(); return FALSE; } } /* Add <token> to argument list with value <value>*/ Pair = list_PairCreate(string_StringCopy(token), string_StringCopy(value)); cmdlne_ArgumentsList = list_Cons(Pair, cmdlne_ArgumentsList); return TRUE; }
static CLAUSE inf_CreateURUnitResolvent(CLAUSE Clause, int i, SUBST Subst, LIST FoundMap, FLAGSTORE Flags, PRECEDENCE Precedence) /************************************************************** INPUT: A non-unit clause, a literal index from the clause, a substitution, a list of pairs (l1, l2) of literals, where l1 is from the non-unit clause and l2 is from a unit clause, a flag store and a precedence. RETURNS: The resolvent of this UR resolution inference. The clause consists of the literal at index <i> in <Clause> after application of <Subst>. EFFECT: The flag store and the precedence are needed to create the new clause. ***************************************************************/ { CLAUSE Result, PClause; LITERAL Lit; TERM Atom; LIST Parents; NAT depth; /* Create atom for resolvent */ Atom = subst_Apply(Subst, term_Copy(clause_GetLiteralAtom(Clause, i))); /* Create clause */ Parents = list_List(Atom); if (i <= clause_LastConstraintLitIndex(Clause)) Result = clause_Create(Parents, list_Nil(), list_Nil(), Flags, Precedence); else if (i <= clause_LastAntecedentLitIndex(Clause)) Result = clause_Create(list_Nil(), Parents, list_Nil(), Flags, Precedence); else Result = clause_Create(list_Nil(), list_Nil(), Parents, Flags, Precedence); list_Delete(Parents); /* Get parent clauses and literals, calculate depth of resolvent */ Parents = list_List(Clause); depth = clause_Depth(Clause); for ( ; !list_Empty(FoundMap); FoundMap = list_Cdr(FoundMap)) { Lit = list_PairSecond(list_Car(FoundMap)); /* Literal from unit */ PClause = clause_LiteralOwningClause(Lit); Parents = list_Cons(PClause, Parents); depth = misc_Max(depth, clause_Depth(PClause)); clause_AddParentClause(Result, clause_Number(PClause)); clause_AddParentLiteral(Result, clause_LiteralGetIndex(Lit)); Lit = list_PairFirst(list_Car(FoundMap)); /* Is from <Clause> */ clause_AddParentClause(Result, clause_Number(Clause)); clause_AddParentLiteral(Result, clause_LiteralGetIndex(Lit)); } clause_SetFromURResolution(Result); clause_SetDepth(Result, depth+1); clause_SetSplitDataFromList(Result, Parents); list_Delete(Parents); return Result; }
static BOOL ana_BidirectionalDistributivity(LIST SymbolPairs) /************************************************************** INPUT: A list of symbol pairs defining distributivity. RETURNS: TRUE, if the list contains two pairs (s1, s2) and (s2, s1) FALSE otherwise. EFFECT: This function is used to detect symbols that are distributive in both directions, logical OR and AND for example. ***************************************************************/ { LIST scan, actPair, nextPair; for ( ; !list_Empty(SymbolPairs); SymbolPairs = list_Cdr(SymbolPairs)) { actPair = list_Car(SymbolPairs); /* If actPair = (s1, s2), check whether there's a pair (s2, s1) in list */ for (scan = list_Cdr(SymbolPairs); !list_Empty(scan); scan = list_Cdr(scan)) { nextPair = list_Car(scan); if (symbol_Equal((SYMBOL)list_PairFirst(actPair),(SYMBOL)list_PairSecond(nextPair)) && symbol_Equal((SYMBOL)list_PairSecond(actPair),(SYMBOL)list_PairFirst(nextPair))) return TRUE; } } return FALSE; }
void cmdlne_FreePair(LIST Pair) /*************************************************************** INPUT: a Pair RETURNS: Nothing. EFFECT: Free the Pair and its elements. ********************* ******************************************/ { string_StringFree(list_PairFirst(Pair)); string_StringFree(list_PairSecond(Pair)); list_PairFree(Pair); }
LIST list_AssocListPair(LIST List, POINTER Key) /************************************************************** INPUT: An association list and a key. RETURNS: The (<key>.<value) in the list. If <key> is not contained, the NULL pair. ***************************************************************/ { LIST Scan; for (Scan=List;!list_Empty(Scan);Scan = list_Cdr(Scan)) if (Key == list_PairFirst(list_Car(Scan))) return list_Car(Scan); return list_PairNull(); }
POINTER list_AssocListValue(LIST List, POINTER Key) /************************************************************** INPUT: An association list and a key. RETURNS: The value for <key> in the list. If <key> is not contained, NULL. ***************************************************************/ { LIST Scan; for (Scan=List;!list_Empty(Scan);Scan = list_Cdr(Scan)) if (Key == list_PairFirst(list_Car(Scan))) return list_PairSecond(list_Car(Scan)); return NULL; }
static CLAUSE red_CreateTerminatorEmptyClause(LIST FoundMap, FLAGSTORE Flags, PRECEDENCE Precedence) /************************************************************** INPUT: A list of pairs (l1, l2), where l1 and l2 are unifiable literals with complementary sign and a flag store. More accurately, a substitution s exists, such that l1 s = l2 s for all pairs (l1,l2) in <FoundMap>. For all literals l from the involved clauses there exists one pair (l1,l2) in <FoundMap> with l1=l or l2=l. The flags store and the precedence are needed to create the new clause. RETURNS: A newly created empty clause, where the data (parents,...) is set according to <FoundMap>. ***************************************************************/ { CLAUSE Result, PClause; LITERAL Lit; LIST Parents; NAT depth; Result = clause_Create(list_Nil(), list_Nil(), list_Nil(), Flags, Precedence); Parents = list_Nil(); depth = 0; for (; !list_Empty(FoundMap); FoundMap = list_Cdr(FoundMap)) { Lit = list_PairSecond(list_Car(FoundMap)); PClause = clause_LiteralOwningClause(Lit); Parents = list_Cons(PClause, Parents); depth = misc_Max(depth, clause_Depth(PClause)); clause_AddParentClause(Result, clause_Number(PClause)); clause_AddParentLiteral(Result, clause_LiteralGetIndex(Lit)); Lit = list_PairFirst(list_Car(FoundMap)); PClause = clause_LiteralOwningClause(Lit); Parents = list_Cons(PClause, Parents); depth = misc_Max(depth, clause_Depth(PClause)); clause_AddParentClause(Result, clause_Number(PClause)); clause_AddParentLiteral(Result, clause_LiteralGetIndex(Lit)); } clause_SetFromTerminator(Result); clause_SetDepth(Result, depth+1); clause_SetSplitDataFromList(Result, Parents); list_Delete(Parents); return Result; }
void hsh_Print(HASH H, void (*KeyPrint)(POINTER), void (*ValuePrint)(POINTER)) /************************************************************** INPUT: A hasharray and a print function for the values EFFECT: Prints all values in the hash array ***************************************************************/ { int i; LIST Scan; for (i = 0; i < hsh__SIZE; i++) { Scan = H[i]; printf("\n %d:",i); while (!list_Empty(Scan)) { KeyPrint(list_PairFirst(list_Car(Scan))); printf("-"); ValuePrint(list_PairSecond(list_Car(Scan))); printf("\n "); Scan = list_Cdr(Scan); } } }
void hsh_Check(HASH H) /************************************************************** INPUT: A hasharray EFFECT: Traverses the whole array and the lists to find dangling pointers. ***************************************************************/ { LIST Scan, Scan2, Pair; NAT i; unsigned long Key; for (i = 0; i < hsh__SIZE; i++) { for (Scan = H[i]; !list_Empty(Scan); Scan = list_Cdr(Scan)) { Pair = list_Car(Scan); Key = (unsigned long)list_PairFirst(Pair); for (Scan2 = list_PairSecond(Pair); !list_Empty(Scan2); Scan2 = list_Cdr(Scan2)) { POINTER Value; char Z; Value = list_Car(Scan2); Z = * ((char*) Value); } } } }
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; }
int main(int argc, const char* argv[]) { LIST Clauses,Axioms,Conjectures,Sorts,Scan, UserPrecedence,UserSelection,ClAxRelation; FILE *Input; CLAUSE Clause; const char *Filename; FLAGSTORE Flags; PRECEDENCE Precedence; BOOL HasPlainClauses; DFGDESCRIPTION Description; memory_Init(memory__UNLIMITED); atexit(memory_FreeAllMem); symbol_Init(TRUE); stack_Init(); term_Init(); flag_Init(flag_SPASS); cmdlne_Init(); Flags = flag_CreateStore(); flag_InitStoreByDefaults(Flags); Precedence = symbol_CreatePrecedence(); Description = desc_Create(); fol_Init(TRUE, Precedence); eml_Init(Precedence); clause_Init(); if (argc < 2 || !cmdlne_Read(argc, argv)) { fputs("\n\t dfg2ascii Version ", stdout); fputs(DFG2ASCII__VERSION, stdout); puts("\n\t Usage: dfg2ascii <input-file>\n"); return EXIT_FAILURE; } if (!cmdlne_SetFlags(Flags)) return EXIT_FAILURE; Axioms = list_Nil(); Conjectures = list_Nil(); Sorts = list_Nil(); UserPrecedence = list_Nil(); UserSelection = list_Nil(); ClAxRelation = list_Nil(); Filename = cmdlne_GetInputFile(); Input = misc_OpenFile(Filename,"r"); Clauses = dfg_DFGParser(Input, Flags, Precedence, Description, &Axioms, &Conjectures, &Sorts, &UserPrecedence, &UserSelection, &ClAxRelation, &HasPlainClauses); misc_CloseFile(Input,Filename); Axioms = list_Nconc(Axioms, Sorts); if (!list_Empty(Axioms) || !list_Empty(Conjectures)) { puts("\n\n\t\t Axioms:\n"); if (list_Empty(Axioms)) puts("None.\n"); else for (Scan=Axioms; !list_Empty(Scan);Scan=list_Cdr(Scan)) { if (list_PairFirst(list_Car(Scan)) != NULL) printf("%s:\n",(char *)list_PairFirst(list_Car(Scan))); fol_PrettyPrintDFG(list_PairSecond(list_Car(Scan))); puts("\n"); } puts("\n\n\t\t Conjectures:\n"); if (list_Empty(Conjectures)) puts("None.\n"); else for (Scan=Conjectures; !list_Empty(Scan);Scan=list_Cdr(Scan)) { if (list_PairFirst(list_Car(Scan)) != NULL) printf("%s:\n",(char *)list_PairFirst(list_Car(Scan))); fol_PrettyPrintDFG(list_PairSecond(list_Car(Scan))); puts("\n"); } } else { BOOL SetExist; LIST ClauseScan; /* Before we sort the clauses, we need to make sure that they have been assigned a weight. */ for (ClauseScan = Clauses; !list_Empty(ClauseScan); ClauseScan = list_Cdr(ClauseScan)) { clause_UpdateWeight((CLAUSE) list_Car(ClauseScan), Flags); } Clauses = clause_ListSortWeighed(Clauses); clause_SetCounter(1); for (Scan = Clauses;!list_Empty(Scan);Scan=list_Cdr(Scan)) { Clause = (CLAUSE)list_Car(Scan); clause_SetSortConstraint(Clause, FALSE, Flags, Precedence); clause_NewNumber(Clause); clause_OrientAndReInit(Clause, Flags, Precedence); } puts("\n\n\t\t Axiom Clauses:\n"); SetExist = FALSE; for (Scan = Clauses;!list_Empty(Scan);Scan=list_Cdr(Scan)) { Clause = (CLAUSE)list_Car(Scan); if (!clause_GetFlag(Clause,CONCLAUSE)) { SetExist = TRUE; clause_Print(Clause); putchar('\n'); } } if (SetExist) SetExist = FALSE; else puts("None.\n"); puts("\n\n\t\t Conjecture Clauses:\n"); for (Scan = Clauses;!list_Empty(Scan);Scan=list_Cdr(Scan)) { Clause = (CLAUSE)list_Car(Scan); if (clause_GetFlag(Clause,CONCLAUSE)) { SetExist = TRUE; clause_Print(Clause); putchar('\n'); } } if (SetExist) SetExist = FALSE; else puts("None.\n"); } clause_DeleteClauseList(Clauses); dfg_StripLabelsFromList(Axioms); dfg_StripLabelsFromList(Conjectures); term_DeleteTermList(Axioms); term_DeleteTermList(Conjectures); eml_Free(); flag_DeleteStore(Flags); symbol_DeletePrecedence(Precedence); list_Delete(UserPrecedence); list_Delete(UserSelection); dfg_DeleteClAxRelation(ClAxRelation); desc_Delete(Description); /*symbol_Dump();*/ cmdlne_Free(); fol_Free(); symbol_FreeAllSymbols(); #ifdef CHECK memory_Print(); #endif return 0; }