DFA *dfaInter(int i, int j, int k) { if (i == j) return dfaSubset(i, k); else if (i == k) return dfaSubset(i, j); else if (j == k) return dfaEq2(i, j); else { int var_index[3]; var_index[0] = i; var_index[1] = j; var_index[2] = k; dfaSetup(3, 3, var_index); /* boolvar */ dfaAllocExceptions(0); dfaStoreState(1); /* state 1 */ dfaAllocExceptions(3); dfaStoreException(1, "111"); dfaStoreException(1, "00X"); dfaStoreException(1, "010"); dfaStoreState(2); /* state 2 */ dfaAllocExceptions(0); dfaStoreState(2); return dfaBuild("0+-"); } }
DFA *dfaPlus2(int i, int j) { if (i == j) return dfaEmpty(i); /* Pi = Pi+1 iff Pi = empty */ else { int var_index[2]; var_index[0] = i; var_index[1] = j; dfaSetup(4, 2, var_index); /* boolvar */ dfaAllocExceptions(0); dfaStoreState(1); /* state 1 */ dfaAllocExceptions(2); dfaStoreException(1, "00"); dfaStoreException(2, "01"); dfaStoreState(3); /* state 2 */ dfaAllocExceptions(2); dfaStoreException(3, "0X"); dfaStoreException(1, "10"); dfaStoreState(2); /* state 3 */ dfaAllocExceptions(0); dfaStoreState(3); return dfaBuild("0+--"); } }
/* an automaton that expresses a non-WS1S property: it accepts iff it reads an empty string or if first-order variable i assumes the value n-1, where n is the length of the string */ DFA *dfaLastPos(int i) { int var_index[1]; var_index[0] = i; dfaSetup(5, 1, var_index); /* boolvar */ dfaAllocExceptions(0); dfaStoreState(4); /* state 1, rejecting */ dfaAllocExceptions(1); dfaStoreException(1, "0"); dfaStoreState(2); /* on "1" go to transitory accepting state */ /* state 2, accepting */ dfaAllocExceptions(0); dfaStoreState(3); /* state 3, rejecting */ dfaAllocExceptions(0); dfaStoreState(3); /* state 4, accepting so as to accept word that describes only Booleans (the empty word when Boolean part is removed) */ dfaAllocExceptions(1); dfaStoreException(1, "0"); dfaStoreState(2); /* on "1" go to transitory accepting state */ return dfaBuild("00+0+"); }
DFA *dfaIn(int i, int j) { int var_index[2]; var_index[0] = i; var_index[1] = j; invariant(i != j); dfaSetup(4, 2, var_index); /* boolvar */ dfaAllocExceptions(0); dfaStoreState(1); /* state 1 */ dfaAllocExceptions(2); dfaStoreException(3, "10"); dfaStoreException(2, "11"); dfaStoreState(1); /* state 2 accept */ dfaAllocExceptions(0); dfaStoreState(2); /* state 3 reject */ dfaAllocExceptions(0); dfaStoreState(3); return dfaBuild("0-+-"); }
DFA *dfaSingleton(int i) { int var_index[1]; var_index[0] = i; dfaSetup(4, 1, var_index); /* boolvar */ dfaAllocExceptions(0); dfaStoreState(1); /* state 1 */ dfaAllocExceptions(1); dfaStoreException(2, "1"); dfaStoreState(1); /* state 2 */ dfaAllocExceptions(1); dfaStoreException(3, "1"); dfaStoreState(2); /* state 3 */ dfaAllocExceptions(0); dfaStoreState(3); return dfaBuild("0-+-"); }
DFA *dfaEq2(int i, int j) { if (i == j) return dfaTrue(); else { int var_index[2]; var_index[0] = i; var_index[1] = j; dfaSetup(3, 2, var_index); /* boolvar */ dfaAllocExceptions(0); dfaStoreState(1); /* state 1 */ dfaAllocExceptions(2); dfaStoreException(1, "00"); dfaStoreException(1, "11"); dfaStoreState(2); /* state 2 */ dfaAllocExceptions(0); dfaStoreState(2); return dfaBuild("0+-"); } }
DFA *dfaFalse() { dfaSetup(2, 0, NULL); /* boolvar */ dfaAllocExceptions(0); dfaStoreState(1); /* state 1 */ dfaAllocExceptions(0); dfaStoreState(1); return dfaBuild("0-"); }
DFA *dfaConst(int n, int i) { DFA *aut; int var_index[1]; int state_no; char *finals; var_index[0] = i; dfaSetup(n+4, 1, var_index); /* boolvar */ dfaAllocExceptions(0); dfaStoreState(3); /* state 1 - Accept */ dfaAllocExceptions(0); dfaStoreState(1); /* state 2 - Reject */ dfaAllocExceptions(0); dfaStoreState(2); /* states 3 .. (n+2) */ for (state_no = 3; state_no < n+3; state_no++) { dfaAllocExceptions(1); dfaStoreException(state_no+1, "0"); dfaStoreState(2); } /* state n+3 */ dfaAllocExceptions(1); dfaStoreException(1, "1"); dfaStoreState(2); { int k; finals = (char *) mem_alloc((n+4) * (sizeof(char *))); for (k = 0; k < n+4; k++) finals[k] = '-'; finals[0] = '0'; finals[1] = '+'; } aut = dfaBuild(finals); mem_free(finals); return aut; }
DFA *build_DFA_eq_nocoef(int vars, int constant, int *indices) { int i, j; char *s = malloc(vars + 1); s[vars] = '\0'; if (constant == 0) { dfaSetup(2, vars, indices); dfaAllocExceptions(1); for (i = 0; i < vars; i++) s[i] = '0'; dfaStoreException(0, s); dfaStoreState(1); dfaAllocExceptions(0); dfaStoreState(1); return dfaBuild("+-"); } if (constant == 1) { dfaSetup(3, vars, indices); dfaAllocExceptions(vars); for (i = 0; i < vars; i++) { for (j = 0; j < vars; j++) if (j == i) s[j] = '1'; else s[j] = '0'; dfaStoreException(1, s); } dfaStoreState(2); dfaAllocExceptions(1); for (i = 0; i < vars; i++) s[i] = '0'; dfaStoreException(1, s); dfaStoreState(2); dfaAllocExceptions(0); dfaStoreState(2); return dfaBuild("-+-"); } return 0; //added by Muath to avoid compiler warning }
/* an automaton that expresses a non-WS1S property: it accepts iff it never reads a 0 on the i'th track and don't-cares otherwise */ DFA *dfaAllPos(int i) { int var_index[1]; var_index[0] = i; dfaSetup(3, 1, var_index); /* boolvar */ dfaAllocExceptions(0); dfaStoreState(1); /* state 1, accepting */ dfaAllocExceptions(1); dfaStoreException(2, "0"); dfaStoreState(1); /* state 2, rejecting */ dfaAllocExceptions(0); dfaStoreState(2); return dfaBuild("0+0"); }
DFA *dfaBoolvar(int b) { int var_index[1]; var_index[0] = b; dfaSetup(3, 1, var_index); /* boolvar */ dfaAllocExceptions(1); dfaStoreException(2, "0"); dfaStoreState(1); /* state 1 */ dfaAllocExceptions(0); dfaStoreState(1); /* state 2 */ dfaAllocExceptions(0); dfaStoreState(2); return dfaBuild("0+-"); }
DFA *dfaFirstOrder(int i) { int var_index[1]; var_index[0] = i; dfaSetup(3, 1, var_index); /* boolvar */ dfaAllocExceptions(0); dfaStoreState(1); /* state 1 */ dfaAllocExceptions(1); dfaStoreException(1, "0"); dfaStoreState(2); /* state 2 */ dfaAllocExceptions(0); dfaStoreState(2); return dfaBuild("0-+"); }
DFA *dfaMax(int i, int j) { if (i == j) return dfaTrue(); else { int var_index[2]; var_index[0] = i; var_index[1] = j; dfaSetup(5, 2, var_index); /* boolvar */ dfaAllocExceptions(0); dfaStoreState(1); /* state 1 */ dfaAllocExceptions(1); dfaStoreException(2, "0X"); dfaStoreState(3); /* state 2 */ dfaAllocExceptions(2); dfaStoreException(2, "0X"); dfaStoreException(4, "10"); dfaStoreState(3); /* state 3 - Accept */ dfaAllocExceptions(1); dfaStoreException(3, "X0"); dfaStoreState(4); /* state 4 - All reject */ dfaAllocExceptions(0); dfaStoreState(4); return dfaBuild("0--+-"); } }
DFA *dfaLesseq(int i, int j) { if (i == j) return dfaTrue(); else { int var_index[2]; var_index[0] = i; var_index[1] = j; dfaSetup(5, 2, var_index); /* boolvar */ dfaAllocExceptions(0); dfaStoreState(1); /* state 1 */ dfaAllocExceptions(3); dfaStoreException(1, "00"); dfaStoreException(2, "10"); dfaStoreException(4, "11"); dfaStoreState(3); /* state 2 */ dfaAllocExceptions(1); dfaStoreException(2, "X0"); dfaStoreState(4); /* state 3 */ dfaAllocExceptions(0); dfaStoreState(3); /* state 4 */ dfaAllocExceptions(0); dfaStoreState(4); return dfaBuild("0---+"); } }
DFA *dfaSetminus(int i, int j, int k) { if (j == k || i == k) return dfaEmpty(i); else if (i == j) { /* make: k inter i = empty */ int var_index[2]; var_index[0] = i; var_index[1] = k; dfaSetup(3, 2, var_index); /* boolvar */ dfaAllocExceptions(0); dfaStoreState(1); /* state 1 */ dfaAllocExceptions(2); dfaStoreException(1,"0X"); dfaStoreException(1,"10"); dfaStoreState(2); /* state 2 */ dfaAllocExceptions(0); dfaStoreState(2); return dfaBuild("0+-"); } else { int var_index[3]; var_index[0] = i; var_index[1] = j; var_index[2] = k; dfaSetup(3, 3, var_index); /* boolvar */ dfaAllocExceptions(0); dfaStoreState(1); /* state 1 */ dfaAllocExceptions(3); dfaStoreException(1, "00X"); dfaStoreException(1, "110"); dfaStoreException(1, "011"); dfaStoreState(2); /* state 2 */ dfaAllocExceptions(0); dfaStoreState(2); return dfaBuild("0+-"); } }
/* is i (first-order) lowest element in j?; i=0 if j is empty */ DFA *dfaMin(int i, int j) { if (i == j) return dfaTrue(); else { int var_index[2]; var_index[0] = i; var_index[1] = j; dfaSetup(6, 2, var_index); /* boolvar */ dfaAllocExceptions(0); dfaStoreState(1); /* state 1 */ dfaAllocExceptions(3); dfaStoreException(2, "00"); dfaStoreException(3, "01"); dfaStoreException(4, "10"); dfaStoreState(5); /* state 2 */ dfaAllocExceptions(2); dfaStoreException(2, "00"); dfaStoreException(5, "11"); dfaStoreState(3); /* state 3 - Reject */ dfaAllocExceptions(0); dfaStoreState(3); /* state 4 - Accept */ dfaAllocExceptions(1); dfaStoreException(4, "X0"); dfaStoreState(3); /* state 5 - Accept */ dfaAllocExceptions(0); dfaStoreState(5); return dfaBuild("0---++"); } }
DFA *dfaMinus1(int i, int j) { if (i == j) { /* <=> pi=0 */ int var_index[1]; var_index[0] = i; dfaSetup(4, 1, var_index); /* boolvar */ dfaAllocExceptions(0); dfaStoreState(1); /* state 1 */ dfaAllocExceptions(1); dfaStoreException(3,"1"); dfaStoreState(2); /* state 2 - Reject */ dfaAllocExceptions(0); dfaStoreState(2); /* state 3 - Accept */ dfaAllocExceptions(0); dfaStoreState(3); return dfaBuild("0--+"); } else { int var_index[2]; var_index[0] = i; var_index[1] = j; dfaSetup(6, 2, var_index); /* boolvar */ dfaAllocExceptions(0); dfaStoreState(1); /* state 1 */ dfaAllocExceptions(3); dfaStoreException(2, "00"); dfaStoreException(3, "01"); dfaStoreException(4, "10"); dfaStoreState(5); /* state 2 */ dfaAllocExceptions(3); dfaStoreException(2, "00"); dfaStoreException(3, "01"); dfaStoreException(4, "10"); dfaStoreState(3); /* state 3 */ dfaAllocExceptions(0); dfaStoreState(3); /* state 4 */ dfaAllocExceptions(1); dfaStoreException(3, "X0"); dfaStoreState(5); /* state 5 */ dfaAllocExceptions(0); dfaStoreState(5); return dfaBuild("0----+"); } }
DFA *dfaPlus1(int i, int j, int n) { if (n == 0) return dfaEq1(i, j); else if (i == j) return dfaFalse(); else { DFA *aut; int var_index[2]; int state_no; char *finals; var_index[0] = i; var_index[1] = j; dfaSetup(n+4, 2, var_index); /* boolvar */ dfaAllocExceptions(0); dfaStoreState(1); /* state 1 */ dfaAllocExceptions(2); dfaStoreException(1, "00"); dfaStoreException(3, "01"); dfaStoreState(2); /* state 2 - Reject */ dfaAllocExceptions(0); dfaStoreState(2); /* state 3 .. (n+1) */ for (state_no = 3; state_no <= n+1; state_no++) { dfaAllocExceptions(1); dfaStoreException(state_no+1,"0X"); dfaStoreState(2); } /* state n+2 */ dfaAllocExceptions(1); dfaStoreException(n+3, "1X"); dfaStoreState(2); /* state n+3 - Accept */ dfaAllocExceptions(0); dfaStoreState(n+3); { int k; finals = (char *) mem_alloc((n+4) * (sizeof(char *))); for (k = 0; k < n+4; k++) finals[k] = '-'; finals[0] = '0'; finals[n+3] = '+'; } aut = dfaBuild(finals); mem_free(finals); return aut; } }
// Constructs a DFA for the equation coeffs*variables+constant=0 // in two's complement arithmetic DFA *build_DFA_eq_2sc(int vars, int *coeffs, int constant, int *indices) { int min, max, states, next_index, next_label, result, target, count; long i; unsigned long j, transitions; struct map_ent *map; char *statuces; DFA *equality, *temp; if (preprocess(vars, coeffs, &constant, 0)) return dfaFalse(); //initialization min = 0; max = 0; for (i = 0; i < vars; i++) if (coeffs[i] > 0) max += coeffs[i]; else min += coeffs[i]; if (constant > max) max = constant; else if (constant < min) min = constant; states = 2 * max - 2 * min + 3; //This array maps state labels (carries) to state indices map = (struct map_ent *) malloc(states * sizeof(struct map_ent)) - min; for (i = min; i < max + 1; i++) { map[i].s = 0; map[i].sr = 0; map[i].i = -1; map[i].ir = -1; } map[constant].sr = 1; //the first state to be expanded next_index = 0; //the next available state index next_label = constant; //the next available state label map[constant].i = -1; map[constant].ir = 0; count = 0; transitions = 1 << vars; //number of transitions from each state //Begin building dfaSetup(states, vars, indices); while (next_label < max + 1) { //there is a state to expand (excuding sink) if (map[next_label].i == count) map[next_label].s = 2; else map[next_label].sr = 2; dfaAllocExceptions(transitions / 2); for (j = 0; j < transitions; j++) { result = next_label + count_ones(j, vars, coeffs); if (!(result & 1)) { target = result / 2; if (target == next_label) { if (map[target].s == 0) { map[target].s = 1; next_index++; map[target].i = next_index; } dfaStoreException(map[target].i, bintostr(j, vars)); } else { if (map[target].sr == 0) { map[target].sr = 1; next_index++; map[target].ir = next_index; } dfaStoreException(map[target].ir, bintostr(j, vars)); } } } dfaStoreState(states - 1); count++; for (next_label = min; (next_label <= max) && (map[next_label].i != count) && (map[next_label].ir != count); next_label++) ; //find next state to expand } for (; count < states; count++) { dfaAllocExceptions(0); dfaStoreState(states - 1); } //define accepting and rejecting states statuces = (char *) malloc(states + 1); for (i = 0; i < states; i++) statuces[i] = '-'; for (next_label = min; next_label <= max; next_label++) if (map[next_label].s == 2) statuces[map[next_label].i] = '+'; statuces[states] = '\0'; temp = dfaBuild(statuces); equality = dfaMinimize(temp); dfaFree(temp); return equality; }
//Constructs a DFA for the inequation coeffs*variables+constant<0 DFA *build_DFA_ineq_new(int vars, int *coeffs, int constant, int *indices) { int min, max, states, next_index, next_label, result, target, count; long i, transitions; unsigned long j; struct map_ent *map; char *statuces; DFA *inequality, *temp; preprocess(vars, coeffs, &constant, 1); //initialization min = 0; max = 0; for (i = 0; i < vars; i++) if (coeffs[i] > 0) max += coeffs[i]; else min += coeffs[i]; if (constant > max) max = constant; else if (constant < min) min = constant; states = max - min + 1; //This array maps state labels (carries) to state indices map = (struct map_ent *) malloc(states * sizeof(struct map_ent)) - min; for (i = min; i < max + 1; i++) { map[i].s = 0; map[i].i = -1; } map[constant].s = 1; //the first state to be expanded next_index = 0; //the next available state index next_label = constant; //the next available state label map[constant].i = 0; count = 0; transitions = 1 << vars; //number of transitions from each state //Begin building dfaSetup(states + 1, vars, indices); dfaAllocExceptions(0); dfaStoreState(1); //count++; while (next_label < max + 1) { //there is a state to expand map[next_label].s = 2; dfaAllocExceptions(transitions); for (j = 0; j < transitions; j++) { result = next_label + count_ones(j, vars, coeffs); if (result >= 0) target = result / 2; else target = (result - 1) / 2; if (map[target].s == 0) { map[target].s = 1; next_index++; map[target].i = next_index; } dfaStoreException(map[target].i + 1, bintostr(j, vars)); } dfaStoreState(count); count++; for (next_label = min; (next_label <= max) && (map[next_label].i != count); next_label++) ; //find next state to expand } for (i = count; i <= states; i++) { dfaAllocExceptions(0); dfaStoreState(i); } //define accepting and rejecting states statuces = (char *) malloc(states + 2); for (i = 0; i <= count; i++) statuces[i] = '-'; for (; i <= states; i++) statuces[i] = '0'; for (i = min; i < 0; i++) if (map[i].s == 2) statuces[map[i].i + 1] = '+'; statuces[states + 1] = '\0'; temp = dfaBuild(statuces); temp->ns -= states - count; inequality = dfaMinimize(temp); return inequality; }
// Constructs a DFA for the inequation coeffs*variables+constant<0 // in two's complement arithmetic DFA *build_DFA_ineq_2sc(int vars, int *coeffs, int constant, int *indices) { int min, max, states, next_index, next_label, result, target, count; long i; unsigned long j, transitions; struct map_ent *map; char *statuces; DFA *inequality, *temp; //int write1, overbits, label1, label2, co; int write1, label1, label2, co; preprocess(vars, coeffs, &constant, 1); //initialization min = 0; max = 0; for (i = 0; i < vars; i++) if (coeffs[i] > 0) max += coeffs[i]; else min += coeffs[i]; if (constant > max) max = constant; else if (constant < min) min = constant; states = max - min + 1; //overbits= ceil(log(states)/log(2)); states *= 2; //This array maps state labels (carries) to state indices map = (struct map_ent *) malloc(states * sizeof(struct map_ent)) - min; for (i = min; i < max + 1; i++) { map[i].s = 0; map[i].sr = 0; map[i].i = -1; map[i].ir = -1; } map[constant].sr = 1; //the first state to be expanded next_index = 0; //the next available state index next_label = constant; //the next available state label map[constant].i = -1; map[constant].ir = 0; count = 0; transitions = 1 << vars; //number of transitions from each state //Begin building dfaSetup(states, vars, indices); while (next_label < max + 1) { //there is a state to expand (excuding sink) if (map[next_label].i == count) map[next_label].s = 2; else map[next_label].sr = 2; dfaAllocExceptions(transitions); for (j = 0; j < transitions; j++) { co = count_ones(j, vars, coeffs); result = next_label + co; if (result >= 0) target = result / 2; else target = (result - 1) / 2; write1 = result & 1; label1 = next_label; label2 = target; while (label1 != label2) { label1 = label2; result = label1 + co; if (result >= 0) label2 = result / 2; else label2 = (result - 1) / 2; write1 = result & 1; } if (write1) { if (map[target].s == 0) { map[target].s = 1; next_index++; map[target].i = next_index; } dfaStoreException(map[target].i, bintostr(j, vars)); } else { if (map[target].sr == 0) { map[target].sr = 1; next_index++; map[target].ir = next_index; } dfaStoreException(map[target].ir, bintostr(j, vars)); } } dfaStoreState(count); count++; for (next_label = min; (next_label <= max) && (map[next_label].i != count) && (map[next_label].ir != count); next_label++) ; //find next state to expand } for (i = count; i < states; i++) { dfaAllocExceptions(0); dfaStoreState(i); } //define accepting and rejecting states statuces = (char *) malloc(states + 1); for (i = 0; i < states; i++) statuces[i] = '-'; for (next_label = min; next_label <= max; next_label++) if (map[next_label].s == 2) statuces[map[next_label].i] = '+'; statuces[states] = '\0'; temp = dfaBuild(statuces); temp->ns -= states - count; inequality = dfaMinimize(temp); return inequality; }
DFA *dfaPresbConst(int i, int n) { /* the following code constructs an automaton for the Presburger relation 'p_i = n' where (non-negative) numbers are encoded with least-significant bit first */ int var_index[1]; /* array of indices of the free variables */ int bits; /* total number of bits required to represent 'n' */ char *status; /* array used for state kinds (-1/0/1) */ DFA *res; int t, s; /* fill 'var_index', only one variable in this case */ var_index[0] = i; /* calculate 'bits' */ for (t = n, bits = 0; t != 0; t >>= 1) bits++; /* prepare construction of automaton with 'bits + 3' states and 1 variable */ status = (char *) mem_alloc(bits + 3); dfaSetup(bits + 3, 1, var_index); /* now create the states on at a time, always start with the initial state (state 0), state 0: */ dfaAllocExceptions(0); dfaStoreState(2); status[0] = '0'; /* '0' denotes "don't care" */ /* these two lines read: there are 0 exceptions for going to state 2, this is what we want since the first symbol being read encodes the values of the Boolean variables of which there are none in this case */ /* we choose to use state 1 as the 'all reject' state, state 1: */ dfaAllocExceptions(0); dfaStoreState(1); status[1] = '-'; /* '-' denotes "reject" */ /* now generate one state for each bit in 'n' */ for (t = n, s = 2; s <= bits+1; s++, t >>= 1) { /* state 's' goes to state 's+1' or all reject depending on the next bit */ dfaAllocExceptions(1); dfaStoreException(1, (t&1) ? "0" : "1"); dfaStoreState(s+1); status[s] = '-'; /* '-' denotes "reject" */ } /* the last state accepts and loops on '0' */ dfaAllocExceptions(1); dfaStoreException(1, "1"); dfaStoreState(bits+2); status[bits+2] = '+'; /* '+' denotes "accept" */ /* finalize the construction */ res = dfaBuild(status); mem_free(status); /* deallocate 'status' */ return res; }
DFA *dfaMinus2(int i, int j) { if (i == j) { int var_index[1]; var_index[0] = i; dfaSetup(4, 1, var_index); /* boolvar */ dfaAllocExceptions(0); dfaStoreState(1); /* state 1 */ dfaAllocExceptions(0); dfaStoreState(2); /* state 2 */ dfaAllocExceptions(1); dfaStoreException(2, "0"); dfaStoreState(3); /* state 3 */ dfaAllocExceptions(0); dfaStoreState(3); return dfaBuild("0++-"); } else { int var_index[2]; var_index[0] = i; var_index[1] = j; dfaSetup(6, 2, var_index); /* boolvar */ dfaAllocExceptions(0); dfaStoreState(1); /* state 1 */ dfaAllocExceptions(3); dfaStoreException(3, "00"); dfaStoreException(4, "10"); dfaStoreException(2, "11"); dfaStoreState(5); /* state 2 */ dfaAllocExceptions(1); dfaStoreException(4, "1X"); dfaStoreState(3); /* state 3 */ dfaAllocExceptions(2); dfaStoreException(3, "00"); dfaStoreException(4, "10"); dfaStoreState(5); /* state 4 */ dfaAllocExceptions(2); dfaStoreException(4, "11"); dfaStoreException(3, "01"); dfaStoreState(5); /* state 5 */ dfaAllocExceptions(0); dfaStoreState(5); return dfaBuild("0+++--"); } }
DFA *dfaSigmaC1toC2(int c1, int c2, int var, int* indices){ int i, n; char *statuces; DFA *result=NULL; if(c2<=-1){ //accept everything after c1 steps n=c1+1; statuces=(char *)malloc((n+1)*sizeof(char)); dfaSetup(n,var,indices); //the 0 to c1-1 states(unaccepted) for( i=0; i<c1; i++){ dfaAllocExceptions(0); dfaStoreState(i+1); statuces[i]='-'; } //the c1 state dfaAllocExceptions(0); dfaStoreState(i); statuces[i]='+'; //i==c1 statuces[n]='\0'; //n==c1+1 }else if(c1<=-1){ n=c2+2; //add one sink state statuces=(char *)malloc((n+1)*sizeof(char)); dfaSetup(n,var,indices); //the 0 to c2 states(accepted) for( i=0; i<=c2; i++){ dfaAllocExceptions(0); dfaStoreState(i+1); statuces[i]='+'; } //the c1 state dfaAllocExceptions(0); dfaStoreState(i); statuces[i]='-'; //i==c2 statuces[n]='\0'; //n==c1+1 }else { assert(c2>=c1); n=c2+2; //add one sink state statuces=(char *)malloc((n+1)*sizeof(char)); dfaSetup(n,var,indices); //the 0 to c2 states(accepted) for( i=0; i<=c2; i++){ dfaAllocExceptions(0); dfaStoreState(i+1); if(i>=c1) statuces[i]='+'; else statuces[i]='-'; } //the c1 state dfaAllocExceptions(0); dfaStoreState(i); statuces[i]='-'; //i==c2 statuces[n]='\0'; //n==c1+1 } result=dfaBuild(statuces); //dfaPrintVerbose(result); free(statuces); if(c1==0) result->f[result->s]=1; DFA *tmp = dfaMinimize(result); dfaFree(result); return tmp; }
DFA *dfaPlusModulo1(int i, int j, int k) /* see plusmodulo.mona */ { if (i == j) { if (i == k) { int var_index[1]; var_index[0] = i; dfaSetup(4, 1, var_index); /* boolvar */ dfaAllocExceptions(0); dfaStoreState(1); /* state 1 */ dfaAllocExceptions(1); dfaStoreException(2, "0"); dfaStoreState(3); /* state 2 */ dfaAllocExceptions(0); dfaStoreState(2); /* state 3 */ dfaAllocExceptions(0); dfaStoreState(3); return dfaBuild("0--+"); } else { int var_index[2]; var_index[0] = i; var_index[1] = k; dfaSetup(5, 2, var_index); /* boolvar */ dfaAllocExceptions(0); dfaStoreState(1); /* state 1 */ dfaAllocExceptions(2); dfaStoreException(2, "0X"); dfaStoreException(3, "10"); dfaStoreState(4); /* state 2 */ dfaAllocExceptions(0); dfaStoreState(2); /* state 3 */ dfaAllocExceptions(1); dfaStoreException(2, "X0"); dfaStoreState(4); /* state 4 */ dfaAllocExceptions(0); dfaStoreState(4); return dfaBuild("0---+"); } } else if (j == k) { int var_index[2]; var_index[0] = i; var_index[1] = k; dfaSetup(7, 2, var_index); /* boolvar */ dfaAllocExceptions(0); dfaStoreState(1); /* state 1 */ dfaAllocExceptions(3); dfaStoreException(2, "00"); dfaStoreException(3, "01"); dfaStoreException(4, "10"); dfaStoreState(5); /* state 2 */ dfaAllocExceptions(1); dfaStoreException(6, "10"); dfaStoreState(3); /* state 3 */ dfaAllocExceptions(0); dfaStoreState(3); /* state 4 */ dfaAllocExceptions(1); dfaStoreException(3, "X0"); dfaStoreState(5); /* state 5 */ dfaAllocExceptions(0); dfaStoreState(5); /* state 6 */ dfaAllocExceptions(1); dfaStoreException(6, "X0"); dfaStoreState(5); return dfaBuild("0----+-"); } else if (i == k) { int var_index[2]; var_index[0] = k; var_index[1] = j; dfaSetup(5, 2, var_index); /* boolvar */ dfaAllocExceptions(0); dfaStoreState(1); /* state 1 */ dfaAllocExceptions(2); dfaStoreException(2, "0X"); dfaStoreException(3, "10"); dfaStoreState(4); /* state 2 */ dfaAllocExceptions(0); dfaStoreState(2); /* state 3 */ dfaAllocExceptions(1); dfaStoreException(3, "X0"); dfaStoreState(4); /* state 4 */ dfaAllocExceptions(0); dfaStoreState(4); return dfaBuild("0---+"); } else { int var_index[3]; var_index[0] = i; var_index[1] = j; var_index[2] = k; dfaSetup(13, 3, var_index); /* boolvar */ dfaAllocExceptions(0); dfaStoreState(1); /* state 1 */ dfaAllocExceptions(6); dfaStoreException(2, "000"); dfaStoreException(3, "0X1"); dfaStoreException(4, "010"); dfaStoreException(5, "100"); dfaStoreException(6, "101"); dfaStoreException(7, "110"); dfaStoreState(8); /* state 2 */ dfaAllocExceptions(3); dfaStoreException(9, "000"); dfaStoreException(4, "010"); dfaStoreException(10, "100"); dfaStoreState(3); /* state 3 */ dfaAllocExceptions(0); dfaStoreState(3); /* state 4 */ dfaAllocExceptions(1); dfaStoreException(11, "1X0"); dfaStoreState(3); /* state 5 */ dfaAllocExceptions(3); dfaStoreException(12, "X00"); dfaStoreException(6, "X01"); dfaStoreException(7, "X10"); dfaStoreState(8); /* state 6 */ dfaAllocExceptions(1); dfaStoreException(6, "X0X"); dfaStoreState(8); /* state 7 */ dfaAllocExceptions(1); dfaStoreException(8, "XX1"); dfaStoreState(3); /* state 8 */ dfaAllocExceptions(0); dfaStoreState(8); /* state 9 */ dfaAllocExceptions(2); dfaStoreException(9, "000"); dfaStoreException(4, "010"); dfaStoreState(3); /* state 10 */ dfaAllocExceptions(2); dfaStoreException(10, "X00"); dfaStoreException(8, "X11"); dfaStoreState(3); /* state 11 */ dfaAllocExceptions(1); dfaStoreException(11, "XX0"); dfaStoreState(8); /* state 12 */ dfaAllocExceptions(3); dfaStoreException(12, "X00"); dfaStoreException(6, "X01"); dfaStoreException(7, "X10"); dfaStoreState(3); return dfaBuild("0-------+----"); } }
DFA *dfa_Suffix(DFA *M, int c1, int c2, int var, int *oldindices) { DFA *result = NULL; DFA *tmpM = NULL; int aux=0; struct int_list_type *states=NULL; struct int_type *tmpState=NULL; int maxCount = 0; int *indices = oldindices; //indices is updated if you need to add auxiliary bits paths state_paths, pp; trace_descr tp; int i, j, z, k; char *exeps; int *to_states; long max_exeps; char *statuces; int len=var; int sink; char *auxbit=NULL; // char *apath =bintostr(a, var); states = reachable_states_bounded_steps(M, c1, c2); maxCount = states->count; if(maxCount>0){ //Need auxiliary bits when there exist some outgoing edges aux = get_hsig(maxCount); if(_FANG_DFA_DEBUG) printf("\n There are %d reachable states, need to add %d auxiliary bits\n", maxCount, aux); auxbit = (char *) malloc(aux*sizeof(char)); len = var+aux; // extra aux bits indices = allocateArbitraryIndex(len); } max_exeps=1<<len; //maybe exponential sink=find_sink(M); assert(sink >-1); //pairs[i] is the list of all reachable states by \sharp1 \bar \sharp0 from i dfaSetup(M->ns+1, len, indices); //add one new initial state exeps=(char *)malloc(max_exeps*(len+1)*sizeof(char)); //plus 1 for \0 end of the string to_states=(int *)malloc(max_exeps*sizeof(int)); statuces=(char *)malloc((M->ns+2)*sizeof(char)); //printf("Before Replace Char\n"); //dfaPrintVerbose(M); k=0; //setup for the initial state tmpState = states->head; for (z=1; z<=states->count; z++) { state_paths = pp = make_paths(M->bddm, M->q[tmpState->value]); while (pp) { if(pp->to!=sink){ to_states[k]=pp->to+1; //insert itself as the initial state for (j = 0; j < var; j++) { //the following for loop can be avoided if the indices are in order for (tp = pp->trace; tp && (tp->index != indices[j]); tp =tp->next); if (tp) { if (tp->value) exeps[k*(len+1)+j]='1'; else exeps[k*(len+1)+j]='0'; }else{ exeps[k*(len+1)+j]='X'; } } set_bitvalue(auxbit, aux, z); // aux = 3, z=4, auxbit 001 for (j = var; j < len; j++) { //set to xxxxxxxx100 exeps[k*(len+1)+j]=auxbit[len-j-1]; } exeps[k*(len+1)+len]='\0'; k++; } pp = pp->next; }//end while kill_paths(state_paths); tmpState = tmpState->next; } //end for dfaAllocExceptions(k); for(k--;k>=0;k--) dfaStoreException(to_states[k],exeps+k*(len+1)); dfaStoreState(sink+1); if(check_accept(M, states)) statuces[0]='+'; else statuces[0]='0'; //for the rest of states (shift one state) for (i = 0; i < M->ns; i++) { state_paths = pp = make_paths(M->bddm, M->q[i]); k=0; while (pp) { if(pp->to!=sink){ for (tp = pp->trace; tp && (tp->index != indices[var]); tp =tp->next); //find the bar value if (!tp || !(tp->value)) { to_states[k]=pp->to+1; for (j = 0; j < var; j++) { //the following for loop can be avoided if the indices are in order for (tp = pp->trace; tp && (tp->index != indices[j]); tp =tp->next); if (tp) { if (tp->value) exeps[k*(len+1)+j]='1'; else exeps[k*(len+1)+j]='0'; } else exeps[k*(len+1)+j]='X'; } for (j = var; j < len; j++) { exeps[k*(len+1)+j]='0'; } exeps[k*(len+1)+len]='\0'; k++; } } pp = pp->next; }//end while dfaAllocExceptions(k); for(k--;k>=0;k--) dfaStoreException(to_states[k],exeps+k*(len+1)); dfaStoreState(sink+1); if(M->f[i]==1) statuces[i+1]='+'; else if(M->f[i]==-1) statuces[i+1]='-'; else statuces[i+1]='0'; kill_paths(state_paths); } statuces[M->ns+1]='\0'; result=dfaBuild(statuces); // dfaPrintVerbose(result); for(i=0; i<aux; i++){ j=len-i-1; tmpM =dfaProject(result, (unsigned) j); dfaFree(result); result = dfaMinimize(tmpM); dfaFree(tmpM); // printf("\n After projecting away %d bits", j); // dfaPrintVerbose(result); } free(exeps); //printf("FREE ToState\n"); free(to_states); //printf("FREE STATUCES\n"); free(statuces); if(maxCount>0) free(auxbit); free_ilt(states); return dfaMinimize(result); }