tclbdd

Check-in [fec3a9366b]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Finished and debugged implementation and test suite of 'compose', plus fixed several additional memory management bugs.
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:fec3a9366bc94971a30f2afcb1309ff87cec0dac
User & Date: kbk 2013-12-09 03:38:48
Context
2013-12-10
04:30
Shorten parameter lists of internal routines by keeping state in the BDD_System object. Start drafting ApplyAndQuantify - but it's not right, so define it away. check-in: 75bd8ee9ed user: kbk tags: trunk
2013-12-09
03:38
Finished and debugged implementation and test suite of 'compose', plus fixed several additional memory management bugs. check-in: fec3a9366b user: kbk tags: trunk
2013-12-07
03:08
Revised caching in Quantify and Restrict, cleaned up commentary. check-in: f726758694 user: kbk tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to generic/bdd.c.

1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
....
1331
1332
1333
1334
1335
1336
1337








1338
1339
1340
1341
1342
1343
1344
....
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
....
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753

1754
1755
1756
1757
1758
1759
1760
....
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908

    u[0] = u1;
    u[1] = u2;
    u[2] = u3;
    entry = Tcl_CreateHashEntry(G, u, &newFlag);
    if (!newFlag) {
	result = (BDD_BeadIndex) Tcl_GetHashValue(entry);
	++sysPtr->beads[result].refCount;
    } else if (op == BDD_TERNOP_IFTHENELSE && u2 == u3) {
	/*
	 * Special case - if the op is IFTHENELSE, and both the IF and
	 * ELSE cases are the same bead, we can prune the search.
	 * (This case is here because it helps performance in Compose)
	 */
	result = u2;
	++sysPtr->beads[result].refCount;
    } else {
	/* 
	 * Check if the result is constant or equal to one of the operands
	 */
	opmask = 0xFF;
	if (u1 == 0) {
................................................................................
	    result = u2;
	    ++sysPtr->beads[result].refCount;
	} else if (opfiltered == (0xAA & opmask)) {
	    /*
	     * Result is the third operand
	     */
	    result = u3;








	    ++sysPtr->beads[result].refCount;
	} else {
	    /*
	     * Result is not constant. Find the top variable
	     */
	    level = u1Ptr->level;
	    if (u2Ptr->level < level) level = u2Ptr->level;
................................................................................
	return result;
    } else if (sysPtr->beads[u].level >= n) {
	result = u;
	++sysPtr->beads[result].refCount;
    } else {
	low = Compose(sysPtr, G, H, low, n, r);
	high = Compose(sysPtr, G, H, high, n, r);
	result = Apply3(sysPtr, G, BDD_TERNOP_IFTHENELSE, r[level], low, high);
	BDD_UnrefBead(sysPtr, low);
	BDD_UnrefBead(sysPtr, high);
    }
    Tcl_SetHashValue(entryPtr, (ClientData) result);
    ++sysPtr->beads[result].refCount;
    return result;
}
................................................................................
    r = Compose(sysPtr, &G, &H, u, n, replacements);

    for (entryPtr = Tcl_FirstHashEntry(&H, &search);
	 entryPtr != NULL;
	 entryPtr = Tcl_NextHashEntry(&search)) {
	BDD_UnrefBead(sysPtr, (BDD_BeadIndex)Tcl_GetHashValue(entryPtr));
    }
    Tcl_DeleteHashTable(&H);
    for (entryPtr = Tcl_FirstHashEntry(&G, &search);
	 entryPtr != NULL;
	 entryPtr = Tcl_NextHashEntry(&search)) {
	BDD_UnrefBead(sysPtr, (BDD_BeadIndex)Tcl_GetHashValue(entryPtr));
    }

    Tcl_DeleteHashTable(&G);

    return r;
}
 
/*
 *-----------------------------------------------------------------------------
................................................................................
    BDD_VariableIndex nVars = BDD_GetVariableCount(sysPtr);

    /*
     * Allocate the stacks
     */
    stateVector->sysPtr = sysPtr;
    stateVector->uStack = (BDD_BeadIndex*)
	ckalloc(nVars * sizeof(BDD_BeadIndex));
    stateVector->sStack = (unsigned char*)
	ckalloc(nVars);
    stateVector->v = (BDD_ValueAssignment*)
	ckalloc(nVars * sizeof(BDD_ValueAssignment));

    /*
     * Store the initial state, in which the top of the expression
     * is on the stack and the stack depth is 1, with the first
     * action being to explore the expression's left hand side.
     */








<
<
<
<
<
<
<
<







 







>
>
>
>
>
>
>
>







 







|







 







<





>







 







|

|

|







1272
1273
1274
1275
1276
1277
1278








1279
1280
1281
1282
1283
1284
1285
....
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
....
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
....
1741
1742
1743
1744
1745
1746
1747

1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
....
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908

    u[0] = u1;
    u[1] = u2;
    u[2] = u3;
    entry = Tcl_CreateHashEntry(G, u, &newFlag);
    if (!newFlag) {
	result = (BDD_BeadIndex) Tcl_GetHashValue(entry);








	++sysPtr->beads[result].refCount;
    } else {
	/* 
	 * Check if the result is constant or equal to one of the operands
	 */
	opmask = 0xFF;
	if (u1 == 0) {
................................................................................
	    result = u2;
	    ++sysPtr->beads[result].refCount;
	} else if (opfiltered == (0xAA & opmask)) {
	    /*
	     * Result is the third operand
	     */
	    result = u3;
	    ++sysPtr->beads[result].refCount;
	} else if (op == BDD_TERNOP_IFTHENELSE && u2 == u3) {
	    /*
	     * Special case - if the op is IFTHENELSE, and both the IF and
	     * ELSE cases are the same bead, we can prune the search.
	     * (This case is here because it helps performance in Compose)
	     */
	    result = u2;
	    ++sysPtr->beads[result].refCount;
	} else {
	    /*
	     * Result is not constant. Find the top variable
	     */
	    level = u1Ptr->level;
	    if (u2Ptr->level < level) level = u2Ptr->level;
................................................................................
	return result;
    } else if (sysPtr->beads[u].level >= n) {
	result = u;
	++sysPtr->beads[result].refCount;
    } else {
	low = Compose(sysPtr, G, H, low, n, r);
	high = Compose(sysPtr, G, H, high, n, r);
	result = Apply3(sysPtr, G, BDD_TERNOP_IFTHENELSE, r[level], high, low);
	BDD_UnrefBead(sysPtr, low);
	BDD_UnrefBead(sysPtr, high);
    }
    Tcl_SetHashValue(entryPtr, (ClientData) result);
    ++sysPtr->beads[result].refCount;
    return result;
}
................................................................................
    r = Compose(sysPtr, &G, &H, u, n, replacements);

    for (entryPtr = Tcl_FirstHashEntry(&H, &search);
	 entryPtr != NULL;
	 entryPtr = Tcl_NextHashEntry(&search)) {
	BDD_UnrefBead(sysPtr, (BDD_BeadIndex)Tcl_GetHashValue(entryPtr));
    }

    for (entryPtr = Tcl_FirstHashEntry(&G, &search);
	 entryPtr != NULL;
	 entryPtr = Tcl_NextHashEntry(&search)) {
	BDD_UnrefBead(sysPtr, (BDD_BeadIndex)Tcl_GetHashValue(entryPtr));
    }
    Tcl_DeleteHashTable(&H);
    Tcl_DeleteHashTable(&G);

    return r;
}
 
/*
 *-----------------------------------------------------------------------------
................................................................................
    BDD_VariableIndex nVars = BDD_GetVariableCount(sysPtr);

    /*
     * Allocate the stacks
     */
    stateVector->sysPtr = sysPtr;
    stateVector->uStack = (BDD_BeadIndex*)
	ckalloc((nVars + 1) * sizeof(BDD_BeadIndex));
    stateVector->sStack = (unsigned char*)
	ckalloc(nVars + 1);
    stateVector->v = (BDD_ValueAssignment*)
	ckalloc((nVars + 1) * sizeof(BDD_ValueAssignment));

    /*
     * Store the initial state, in which the top of the expression
     * is on the stack and the stack depth is 1, with the first
     * action being to explore the expression's left hand side.
     */

Changes to generic/tclBdd.c.

101
102
103
104
105
106
107


108
109
110
111
112
113
114
...
170
171
172
173
174
175
176







177
178
179
180
181
182
183
...
268
269
270
271
272
273
274

275
276
277
278
279
280
281
...
580
581
582
583
584
585
586




































































































































587
588
589
590
591
592
593
....
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
static void DeletePerInterpData(PerInterpData*);
static int BddSystemConstructor(ClientData, Tcl_Interp*, Tcl_ObjectContext,
				int, Tcl_Obj* const[]);
static int BddSystemBeadindexMethod(ClientData, Tcl_Interp*, Tcl_ObjectContext,
				    int, Tcl_Obj* const[]);
static int BddSystemBinopMethod(ClientData, Tcl_Interp*, Tcl_ObjectContext,
				int, Tcl_Obj* const[]);


static int BddSystemCopyMethod(ClientData, Tcl_Interp*, Tcl_ObjectContext,
			       int, Tcl_Obj* const[]);
static int BddSystemDumpMethod(ClientData, Tcl_Interp*, Tcl_ObjectContext,
			       int, Tcl_Obj* const[]);
static int BddSystemForeachSatMethod(ClientData, Tcl_Interp*, Tcl_ObjectContext,
				     int, Tcl_Obj *const[]);
static int ForeachSatPre(Tcl_Interp* interp, BddSystemData*,
................................................................................
const static Tcl_MethodType BddSystemBinopMethodType = {
    TCL_OO_METHOD_VERSION_CURRENT, /* version */
    "binop",			   /* name */
    BddSystemBinopMethod,	   /* callProc */
    DeleteMethod,		   /* method delete proc */
    CloneMethod			   /* method clone proc */
};				   /* common to all ten binary operators */







const static Tcl_MethodType BddSystemCopyMethodType = {
    TCL_OO_METHOD_VERSION_CURRENT, /* version */
    "copy",			   /* name */
    BddSystemCopyMethod,	   /* callProc */
    DeleteMethod,		   /* method delete proc */
    CloneMethod			   /* method clone proc */
};
................................................................................
    { ">=",        &BddSystemBinopMethodType,     (ClientData) BDD_BINOP_GE },
    { "^",         &BddSystemBinopMethodType,     (ClientData) BDD_BINOP_XOR },
    { "^3",        &BddSystemTernopMethodType,    (ClientData) BDD_TERNOP_XOR },
    { "?:",        &BddSystemTernopMethodType,    
                                           (ClientData) BDD_TERNOP_IFTHENELSE },
    { "beadindex", &BddSystemBeadindexMethodType, NULL },
    { "borrow",    &BddSystemTernopMethodType, (ClientData) BDD_TERNOP_BORROW },

    { "concur3",   &BddSystemTernopMethodType, (ClientData) BDD_TERNOP_CONCUR },
    { "differ3",   &BddSystemTernopMethodType, (ClientData) BDD_TERNOP_DIFFER },
    { "dump",      &BddSystemDumpMethodType,      NULL },
    { "even3",     &BddSystemTernopMethodType,   (ClientData) BDD_TERNOP_EVEN },
    { "exists",	   &BddSystemQuantifyMethodType,  
      					        (ClientData) BDD_QUANT_EXISTS },
    { "forall",	   &BddSystemQuantifyMethodType,  
................................................................................
    }
    beadIndexResult = BDD_Apply(sdata->system, (BDD_BinOp) (size_t)clientData,
				beadIndexOpd1, beadIndexOpd2);
    SetNamedExpression(sdata, objv[skipped], beadIndexResult);
    BDD_UnrefBead(sdata->system, beadIndexResult);
    return TCL_OK;
}




































































































































 
/*
 *-----------------------------------------------------------------------------
 *
 * BddSystemCopyMethod --
 *
 *	Copies a named expression in a system of BDD's and assigns
................................................................................
			    &u) != TCL_OK) {
	return TCL_ERROR;
    }
    if (Tcl_ListObjGetElements(interp, objv[skipped+1],
			       &varc, &varv) != TCL_OK) {
	return TCL_ERROR;
    }
    v = ckalloc(varc);
    for (i = 0; i < varc; ++i) {
	if (FindNamedExpression(interp, sdata, varv[i],
				&literalIndex) != TCL_OK) {
	    ckfree(v);
	    return TCL_ERROR;
	}
	if (!BDD_Literal(sdata->system, literalIndex, &a)







>
>







 







>
>
>
>
>
>
>







 







>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







|







101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
...
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
...
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
...
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
....
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
static void DeletePerInterpData(PerInterpData*);
static int BddSystemConstructor(ClientData, Tcl_Interp*, Tcl_ObjectContext,
				int, Tcl_Obj* const[]);
static int BddSystemBeadindexMethod(ClientData, Tcl_Interp*, Tcl_ObjectContext,
				    int, Tcl_Obj* const[]);
static int BddSystemBinopMethod(ClientData, Tcl_Interp*, Tcl_ObjectContext,
				int, Tcl_Obj* const[]);
static int BddSystemComposeMethod(ClientData, Tcl_Interp*, Tcl_ObjectContext,
				  int, Tcl_Obj* const[]);
static int BddSystemCopyMethod(ClientData, Tcl_Interp*, Tcl_ObjectContext,
			       int, Tcl_Obj* const[]);
static int BddSystemDumpMethod(ClientData, Tcl_Interp*, Tcl_ObjectContext,
			       int, Tcl_Obj* const[]);
static int BddSystemForeachSatMethod(ClientData, Tcl_Interp*, Tcl_ObjectContext,
				     int, Tcl_Obj *const[]);
static int ForeachSatPre(Tcl_Interp* interp, BddSystemData*,
................................................................................
const static Tcl_MethodType BddSystemBinopMethodType = {
    TCL_OO_METHOD_VERSION_CURRENT, /* version */
    "binop",			   /* name */
    BddSystemBinopMethod,	   /* callProc */
    DeleteMethod,		   /* method delete proc */
    CloneMethod			   /* method clone proc */
};				   /* common to all ten binary operators */
const static Tcl_MethodType BddSystemComposeMethodType = {
    TCL_OO_METHOD_VERSION_CURRENT, /* version */
    "compose",			   /* name */
    BddSystemComposeMethod,	   /* callProc */
    DeleteMethod,		   /* method delete proc */
    CloneMethod			   /* method clone proc */
};
const static Tcl_MethodType BddSystemCopyMethodType = {
    TCL_OO_METHOD_VERSION_CURRENT, /* version */
    "copy",			   /* name */
    BddSystemCopyMethod,	   /* callProc */
    DeleteMethod,		   /* method delete proc */
    CloneMethod			   /* method clone proc */
};
................................................................................
    { ">=",        &BddSystemBinopMethodType,     (ClientData) BDD_BINOP_GE },
    { "^",         &BddSystemBinopMethodType,     (ClientData) BDD_BINOP_XOR },
    { "^3",        &BddSystemTernopMethodType,    (ClientData) BDD_TERNOP_XOR },
    { "?:",        &BddSystemTernopMethodType,    
                                           (ClientData) BDD_TERNOP_IFTHENELSE },
    { "beadindex", &BddSystemBeadindexMethodType, NULL },
    { "borrow",    &BddSystemTernopMethodType, (ClientData) BDD_TERNOP_BORROW },
    { "compose",   &BddSystemComposeMethodType,   NULL },
    { "concur3",   &BddSystemTernopMethodType, (ClientData) BDD_TERNOP_CONCUR },
    { "differ3",   &BddSystemTernopMethodType, (ClientData) BDD_TERNOP_DIFFER },
    { "dump",      &BddSystemDumpMethodType,      NULL },
    { "even3",     &BddSystemTernopMethodType,   (ClientData) BDD_TERNOP_EVEN },
    { "exists",	   &BddSystemQuantifyMethodType,  
      					        (ClientData) BDD_QUANT_EXISTS },
    { "forall",	   &BddSystemQuantifyMethodType,  
................................................................................
    }
    beadIndexResult = BDD_Apply(sdata->system, (BDD_BinOp) (size_t)clientData,
				beadIndexOpd1, beadIndexOpd2);
    SetNamedExpression(sdata, objv[skipped], beadIndexResult);
    BDD_UnrefBead(sdata->system, beadIndexResult);
    return TCL_OK;
}
 
/*
 *-----------------------------------------------------------------------------
 *
 * BddSystemComposeMethod --
 *
 *	Rewrites a BDD, substituting each of a group of variables with
 *	some other expression
 *
 * Usage:
 *	$system compose expr ?var value?...
 *
 * Parameters:
 *	system - System of BDD's
 *	expr - Name of the expression to rewrite
 *	var - Name of a variable to substitute
 *	value - Name of the expression that is to substitute for the variable.
 *
 * Results:
 *	Returns a standard Tcl result
 *
 * Side effects:
 *	Creates the named expression if successful.
 *
 * The substitutions proceed in parallel from the highest numbered variable
 * to the lowest.
 *
 *-----------------------------------------------------------------------------
 */
static int
BddSystemComposeMethod(
    ClientData clientData,	/* Operation to perform */
    Tcl_Interp* interp,		/* Tcl interpreter */
    Tcl_ObjectContext ctx,	/* Object context */
    int objc,			/* Parameter count */
    Tcl_Obj *const objv[])	/* Parameter vector */
{
    Tcl_Object thisObject = Tcl_ObjectContextObject(ctx);
				/* The current object */
    BddSystemData* sdata = (BddSystemData*)
	Tcl_ObjectGetMetadata(thisObject, &BddSystemDataType);
				/* The current system of expressions */
    int skipped = Tcl_ObjectContextSkippedArgs(ctx);
				/* The number of args used in method dispatch */

    BDD_BeadIndex u;		/* Expression to quantify */
    BDD_VariableIndex nVars = 0;
				/* Index of the highest numbered variable
				 * to substitute */
    BDD_BeadIndex* replace = NULL;
				/* BDD's of the replacement values */
    BDD_BeadIndex literalIndex;	/* Index of the current variable as a BDD */
    BDD_ValueAssignment a;	/* Current variable's number */
    BDD_BeadIndex replacementIndex;
				/* Replacement value */
    Tcl_Obj* errorMessage;	/* Error message of a failed execution */
    BDD_BeadIndex result;	/* Result of the quantification */
    int status = TCL_OK;	/* Status return */
    int i;
    BDD_VariableIndex j;

    /* Check syntax */

    if (objc < skipped+2 || ((objc-skipped) % 2) != 0) {
	Tcl_WrongNumArgs(interp, skipped, objv, "name expr ?var value?...");
	return TCL_ERROR;
    }

    /*
     * Expression to rewrite
     */
    status = FindNamedExpression(interp, sdata, objv[skipped+1], &u);

    for (i = skipped+2; status == TCL_OK && i < objc; i += 2) {
	/* 
	 * Variable to substitute
	 */
	if (FindNamedExpression(interp, sdata, objv[i],
				&literalIndex) != TCL_OK) {
	    status = TCL_ERROR;
	} else if (!BDD_Literal(sdata->system, literalIndex, &a)
	    || (a.value == 0)) {
	    errorMessage = Tcl_ObjPrintf("%s is not a variable",
					 Tcl_GetString(objv[i]));
	    Tcl_SetObjResult(interp, errorMessage);
	    Tcl_SetErrorCode(interp, "BDD", "NotVariable",
			     Tcl_GetString(objv[i]), NULL);
	    status = TCL_ERROR;

	    /*
	     * Replacement value
	     */
	} else if (FindNamedExpression(interp, sdata, objv[i+1],
				       &replacementIndex) != TCL_OK) {
	    
	    status = TCL_ERROR;
	} else {
	    BDD_IncrBeadRefCount(sdata->system, replacementIndex);
	    if (a.var >= nVars) {
		replace = ckrealloc(replace, (a.var+1) * sizeof(BDD_BeadIndex));
		for (j = nVars; j < a.var; ++j) {
		    replace[j] = BDD_NthVariable(sdata->system, j);
		}
		nVars = a.var + 1;
	    } else {
		BDD_UnrefBead(sdata->system, replace[a.var]);
	    }
	    replace[a.var] = replacementIndex;
	}
    }

    /*
     * Perform the composition
     */
    if (status == TCL_OK) {
	result = BDD_Compose(sdata->system, u, nVars, replace);
	SetNamedExpression(sdata, objv[skipped], result);
	BDD_UnrefBead(sdata->system, result);
    }

    /* 
     * Clean up references
     */
    for (j = 0; j < nVars; ++j) {
	BDD_UnrefBead(sdata->system, replace[j]);
    }
    if (replace != NULL) {
	ckfree(replace);
    }

    return status;
}
 
/*
 *-----------------------------------------------------------------------------
 *
 * BddSystemCopyMethod --
 *
 *	Copies a named expression in a system of BDD's and assigns
................................................................................
			    &u) != TCL_OK) {
	return TCL_ERROR;
    }
    if (Tcl_ListObjGetElements(interp, objv[skipped+1],
			       &varc, &varv) != TCL_OK) {
	return TCL_ERROR;
    }
    v = ckalloc(varc * sizeof(BDD_VariableIndex));
    for (i = 0; i < varc; ++i) {
	if (FindNamedExpression(interp, sdata, varv[i],
				&literalIndex) != TCL_OK) {
	    ckfree(v);
	    return TCL_ERROR;
	}
	if (!BDD_Literal(sdata->system, literalIndex, &a)

Changes to library/tclbdd.tcl.

6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
#------------------------------------------------------------------------------

package require grammar::aycock;	# TEMP - Build parser in advance!
package require grammar::aycock::runtime

namespace eval bdd {
    namespace export system
}

oo::class create bdd::system {
    method === {exprName1 exprName2} {







|







6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
#------------------------------------------------------------------------------

package require grammar::aycock;	# TEMP - Build parser in advance!
# package require grammar::aycock::runtime

namespace eval bdd {
    namespace export system
}

oo::class create bdd::system {
    method === {exprName1 exprName2} {

Changes to tests/bdd.test.

1909
1910
1911
1912
1913
1914
1915

































































































































































































































































































1916
1917
1918
1919
1920
1921
1922
    }
    -body {
	sys |3 y p q r
	sys | z p q
	sys | z z r
	expr {[sys beadindex y] == [sys beadindex z]}
    }

































































































































































































































































































    -result 1
    -cleanup {rename sys {}}
}

test bdd-21.1 {eight queens} {*}{
    -setup {
	bdd::system create sys







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
    }
    -body {
	sys |3 y p q r
	sys | z p q
	sys | z z r
	expr {[sys beadindex y] == [sys beadindex z]}
    }
    -result 1
    -cleanup {rename sys {}}
}

test bdd-17.1 {compose - wrong # args} {*}{
    -setup {
	bdd::system create sys
    }
    -body {
	sys compose
    }
    -cleanup {rename sys {}}
    -returnCodes error
    -match glob
    -result {wrong # args: *}
}

test bdd-17.2 {compose - wrong # args} {*}{
    -setup {
	bdd::system create sys
    }
    -body {
	sys compose a b c
    }
    -cleanup {rename sys {}}
    -returnCodes error
    -match glob
    -result {wrong # args: *}
}

test bdd-17.3 {compose - bad expr} {*}{
    -setup {
	bdd::system create sys
    }
    -body {
	list [catch {sys compose a garbage} result] $result $::errorCode
    }
    -cleanup {rename sys {}}
    -result \
	{1 {expression named "garbage" not found} {BDD ExprNotFound garbage}}
}

test bdd-17.4 {compose - bad var} {*}{
    -setup {
	bdd::system create sys
	sys nthvar a 0
    }
    -body {
	list [catch {sys compose x a garbage 0} result] $result $::errorCode
    }
    -cleanup {rename sys {}}
    -result \
	{1 {expression named "garbage" not found} {BDD ExprNotFound garbage}}
}

test bdd-17.5 {compose - bad var} {*}{
    -setup {
	bdd::system create sys
	sys nthvar a 0
	sys nthvar b 1
	sys & c a b
    }
    -body {
	list [catch {sys compose x a c 0} result] $result $::errorCode
    }
    -cleanup {rename sys {}}
    -result \
	{1 {c is not a variable} {BDD NotVariable c}}
}

test bdd-17.6 {compose - bad var} {*}{
    -setup {
	bdd::system create sys
	sys nthvar a 0
	sys notnthvar !b 1
    }
    -body {
	list [catch {sys compose x a !b 0} result] $result $::errorCode
    }
    -cleanup {rename sys {}}
    -result \
	{1 {!b is not a variable} {BDD NotVariable !b}}
}

test bdd-17.7 {compose - bad replacement expr} {*}{
    -setup {
	bdd::system create sys
	sys nthvar a 0
	sys nthvar b 1
    }
    -body {
	list [catch {sys compose x a b garbage} result] $result $::errorCode
    }
    -cleanup {rename sys {}}
    -result \
	{1 {expression named "garbage" not found} {BDD ExprNotFound garbage}}
}

test bdd-17.8 {compose - do nothing gracefully} {*}{
    -setup {
	bdd::system create sys
	sys nthvar a 0
	sys nthvar b 1
	sys & c a b
    }
    -body {
	sys compose d c
	expr {[sys beadindex d] == [sys beadindex c]}
    }
    -cleanup {rename sys {}}
    -result 1
}

test bdd-17.9 {compose with irrelevant variable} {*}{
    -setup {
	bdd::system create sys
	sys nthvar p 0
	sys nthvar a 1
	sys nthvar q 2
	sys nthvar b 3
	sys nthvar r 4
	sys & c a b
    }
    -body {
	sys compose d c p 0
	expr {[sys beadindex d] == [sys beadindex c]}
    }
    -cleanup {rename sys {}}
    -result 1
}

test bdd-17.10 {compose with irrelevant variable} {*}{
    -setup {
	bdd::system create sys
	sys nthvar p 0
	sys nthvar a 1
	sys nthvar q 2
	sys nthvar b 3
	sys nthvar r 4
	sys & c a b
    }
    -body {
	sys compose d c q 0
	expr {[sys beadindex d] == [sys beadindex c]}
    }
    -cleanup {rename sys {}}
    -result 1
}

test bdd-17.10 {compose with irrelevant variable} {*}{
    -setup {
	bdd::system create sys
	sys nthvar p 0
	sys nthvar a 1
	sys nthvar q 2
	sys nthvar b 3
	sys nthvar r 4
	sys & c a b
    }
    -body {
	sys compose d c r 0
	expr {[sys beadindex d] == [sys beadindex c]}
    }
    -cleanup {rename sys {}}
    -result 1
}

test bdd-17.11 {compose - replace with constant} {*}{
    -setup {
	bdd::system create sys
	sys nthvar a 0; sys notnthvar !a 0
	sys nthvar b 1; sys notnthvar !b 1
	sys & c a b
    } -body {
	set trouble {}
	foreach s {{a 0} {a 1} {b 0} {b 1} {a 1 b 1}} \
	    y {0 b 0 a 1} \
	    {
		sys compose z c {*}$s
		if {[sys beadindex z] != [sys beadindex $y]} {
		    lappend trouble "a&b\[[lindex $s 1]/[lindex $s 0]] " \
			"should be $y"
		}
	    }
	join $trouble \n
    }
    -cleanup {rename sys {}}
    -result {}
}

test bdd-17.12 {compose - replace with variable} {*}{
    -setup {
	bdd::system create sys
	sys nthvar p 0; sys notnthvar !p 0
	sys nthvar a 1; sys notnthvar !a 1
	sys nthvar q 2; sys notnthvar !q 2
	sys nthvar b 3; sys notnthvar !b 3
	sys nthvar r 4; sys notnthvar !r 4
	sys & c a b
    }
    -body {
	set trouble {} 
	foreach x {p a q b r} {
	    sys compose d c a $x
	    sys & e $x b
	    if {[sys beadindex d] != [sys beadindex d]} {
		lappend trouble "a&$x should be the same as (a&b)\[$x/b]"
	    }
	}
	join $trouble \n
    }
    -cleanup {rename sys {}}
    -result {}
}

test bdd-17.13 {compose - interchange variables} {
    -setup {
	bdd::system create sys
	sys nthvar a 1; sys notnthvar !a 1
	sys nthvar b 2; sys notnthvar !b 2
	sys < c a b
	sys < y b a
    } 
    -body {
	sys compose z c a b b a
	expr {[sys beadindex z] == [sys beadindex y]}
    }
    -result 1
    -cleanup {rename sys {}}
}

test bdd-17.14 {compose - interchange variables} {
    -setup {
	bdd::system create sys
	sys nthvar a 1; sys notnthvar !a 1
	sys nthvar b 2; sys notnthvar !b 2
	sys nthvar c 3; sys notnthvar !c 3
	sys < x a b
	sys < t b c
	sys & x x t
	sys < y b c
	sys < t c a
	sys & y y t
    } 
    -body {
	sys compose z x a b b c c a
	expr {[sys beadindex z] == [sys beadindex y]}
    }
    -result 1
    -cleanup {rename sys {}}
}

test bdd-17.15 {compose - interchange variables} {
    -setup {
	bdd::system create sys
	sys nthvar a 1; sys notnthvar !a 1
	sys nthvar b 2; sys notnthvar !b 2
	sys nthvar c 3; sys notnthvar !c 3
	sys < x a b
	sys < t b c
	sys & x x t
	sys < y c a
	sys < t a b
	sys & y y t
    } 
    -body {
	sys compose z x b a c b a c
	expr {[sys beadindex z] == [sys beadindex y]}
    }
    -result 1
    -cleanup {rename sys {}}
}

test bdd-17.15 {compose - more complex substitution} {
    -setup {
	bdd::system create sys
	sys nthvar a 0
	sys nthvar b 1
	sys nthvar c 2
	sys nthvar d 3
	sys | x a b
	sys & t a b
	sys & u c d
	sys | y t u
    } 
    -body {
	sys compose z x a t b u
	expr {[sys beadindex z] == [sys beadindex y]}
    }
    -result 1
    -cleanup {rename sys {}}
}

test bdd-21.1 {eight queens} {*}{
    -setup {
	bdd::system create sys