tclbdd

Check-in [5b78565cad]
Login

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

Overview
Comment:Implemented foreach_sat (no tests yet). Fixed a bug where restrict cannot handle variables out of sequence, and added a test case for same. Made the 8-queens example use foreach_sat to enumerate the solutions.
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:5b78565cadab2dcd3651b6eb9df24dff4175c45a
User & Date: kennykb 2013-12-01 23:14:30
Context
2013-12-01
23:26
Added ===, tautology, satisfiable methods (no tests yet). Replaced 'constant' and 'copy' with a single ':=' method. check-in: f7a47671e9 user: kennykb tags: trunk
23:14
Implemented foreach_sat (no tests yet). Fixed a bug where restrict cannot handle variables out of sequence, and added a test case for same. Made the 8-queens example use foreach_sat to enumerate the solutions. check-in: 5b78565cad user: kennykb tags: trunk
01:09
Add test cases for Restrict and fix the bugs exposed thereby. check-in: 1ff84c529c user: kennykb tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to generic/bdd.c.

1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
....
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
....
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
....
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468

    return stateVector;
}
 
/*
 *-----------------------------------------------------------------------------
 *
 * BDD_AllSat_Next --
 *
 *	Retrieves the next satisfying assignment from an iterator over
 *	a BDD
 *
 * Results:
 *	Returns 1 if there was a satisfying assignment, 0 at the end of
 *	the iterator.
................................................................................
 * It is necessary to call BDD_AllSat_Finish to dispose of the state
 * vector, even after this function has returned 0.
 *
 *-----------------------------------------------------------------------------
 */

int
BDD_AllSat_Next(
    BDD_AllSatState* stateVector,
				/* State vector from BDD_AllSat_Start */
    BDD_ValueAssignment **vPtr, /* OUTPUT: Vector of satisfying literals */
    BDD_VariableIndex *nPtr)	/* OUTPUT: Count of satisfying literals */
{
    BDD_System* sysPtr = stateVector->sysPtr;
				/* System of BDD's */
................................................................................
	 * the diagram, or SECONDTRY, to explore the 'high' transition of
	 * a visited bead. If it's SECONDTRY, advance to the bead's 'high'
	 * transition.
	 */
	if (s == BDD_ALLSAT_SECONDTRY) {
	    stateVector->uStack[depth] = u;
	    stateVector->sStack[depth] = BDD_ALLSAT_RETURN;
	    stateVector->v[depth].var = sysPtr->beads[u].high;
	    stateVector->v[depth].value = 1;
	    ++depth;
	    u = sysPtr->beads[u].high;
	}

	/*
	 * Traverse the 'low' transitions on the current branch
................................................................................
	 */
    }
}
 
/*
 *-----------------------------------------------------------------------------
 *
 * BDD_AllSat_Finish --
 *
 *	Terminates an exhaustive search for satisfying variable assigments
 *	in a BDD.
 *
 * Side effects:
 *	Frees the state vector and decrements the ref count of the start bead.
 *
 *-----------------------------------------------------------------------------
 */

void
BDD_AllSat_Finish(
    BDD_AllSatState* stateVector) /* State vector for tbe search */
{
    ckfree(stateVector->v);
    ckfree(stateVector->sStack);
    BDD_UnrefBead(stateVector->sysPtr, stateVector->uStack[0]);
    ckfree(stateVector->uStack);
    ckfree(stateVector);







|







 







|







 







|







 







|











|







1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
....
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
....
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
....
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468

    return stateVector;
}
 
/*
 *-----------------------------------------------------------------------------
 *
 * BDD_AllSatNext --
 *
 *	Retrieves the next satisfying assignment from an iterator over
 *	a BDD
 *
 * Results:
 *	Returns 1 if there was a satisfying assignment, 0 at the end of
 *	the iterator.
................................................................................
 * It is necessary to call BDD_AllSat_Finish to dispose of the state
 * vector, even after this function has returned 0.
 *
 *-----------------------------------------------------------------------------
 */

int
BDD_AllSatNext(
    BDD_AllSatState* stateVector,
				/* State vector from BDD_AllSat_Start */
    BDD_ValueAssignment **vPtr, /* OUTPUT: Vector of satisfying literals */
    BDD_VariableIndex *nPtr)	/* OUTPUT: Count of satisfying literals */
{
    BDD_System* sysPtr = stateVector->sysPtr;
				/* System of BDD's */
................................................................................
	 * the diagram, or SECONDTRY, to explore the 'high' transition of
	 * a visited bead. If it's SECONDTRY, advance to the bead's 'high'
	 * transition.
	 */
	if (s == BDD_ALLSAT_SECONDTRY) {
	    stateVector->uStack[depth] = u;
	    stateVector->sStack[depth] = BDD_ALLSAT_RETURN;
	    stateVector->v[depth].var = sysPtr->beads[u].level;
	    stateVector->v[depth].value = 1;
	    ++depth;
	    u = sysPtr->beads[u].high;
	}

	/*
	 * Traverse the 'low' transitions on the current branch
................................................................................
	 */
    }
}
 
/*
 *-----------------------------------------------------------------------------
 *
 * BDD_AllSatFinish --
 *
 *	Terminates an exhaustive search for satisfying variable assigments
 *	in a BDD.
 *
 * Side effects:
 *	Frees the state vector and decrements the ref count of the start bead.
 *
 *-----------------------------------------------------------------------------
 */

void
BDD_AllSatFinish(
    BDD_AllSatState* stateVector) /* State vector for tbe search */
{
    ckfree(stateVector->v);
    ckfree(stateVector->sStack);
    BDD_UnrefBead(stateVector->sysPtr, stateVector->uStack[0]);
    ckfree(stateVector->uStack);
    ckfree(stateVector);

Changes to generic/bdd.h.

108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
					 BDD_VariableIndex n);
extern BDDAPI int BDD_SatCount(BDD_System* sysPtr, BDD_BeadIndex x,
			       mp_int* count);
extern BDDAPI BDD_AllSatState* BDD_AllSatStart(BDD_System* sysPtr,
					       BDD_BeadIndex u);
extern BDDAPI int BDD_AllSatNext(BDD_AllSatState* state,
				 BDD_ValueAssignment** vPtr,
				 int* nPtr);
extern BDDAPI int BDD_AllSatFinish(BDD_AllSatState*);
extern BDDAPI int BDD_Dump(Tcl_Interp*, Tcl_Obj*, BDD_System*, BDD_BeadIndex);
extern BDDAPI void BDD_DeleteSystem(BDD_System* sysPtr);


#endif /* not BDD_H_INCLUDED */

/*
 * Local Variables:
 * mode: c
 * c-basic-offset: 4
 * End:
 */







|
|












108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
					 BDD_VariableIndex n);
extern BDDAPI int BDD_SatCount(BDD_System* sysPtr, BDD_BeadIndex x,
			       mp_int* count);
extern BDDAPI BDD_AllSatState* BDD_AllSatStart(BDD_System* sysPtr,
					       BDD_BeadIndex u);
extern BDDAPI int BDD_AllSatNext(BDD_AllSatState* state,
				 BDD_ValueAssignment** vPtr,
				 BDD_VariableIndex* nPtr);
extern BDDAPI void BDD_AllSatFinish(BDD_AllSatState*);
extern BDDAPI int BDD_Dump(Tcl_Interp*, Tcl_Obj*, BDD_System*, BDD_BeadIndex);
extern BDDAPI void BDD_DeleteSystem(BDD_System* sysPtr);


#endif /* not BDD_H_INCLUDED */

/*
 * Local Variables:
 * mode: c
 * c-basic-offset: 4
 * End:
 */

Changes to generic/tclBdd.c.

92
93
94
95
96
97
98

99
100
101
102
103
104
105
106
107
108
109
110
111






112
113
114
115
116
117
118
...
176
177
178
179
180
181
182
183
184
185
186







187
188
189
190
191
192
193
...
241
242
243
244
245
246
247


248
249
250
251
252
253
254
...
708
709
710
711
712
713
714



























































































































































































































715
716
717
718
719
720
721
...
959
960
961
962
963
964
965







966
967
968
969
970
971
972
....
1275
1276
1277
1278
1279
1280
1281


























1282
1283
1284
1285
1286
1287
1288
static void SetNamedExpression(BddSystemData*, Tcl_Obj*, BDD_BeadIndex);
static Tcl_HashEntry* FindHashEntryForNamedExpression(Tcl_Interp*,
						      BddSystemData*,
						      Tcl_Obj*);
static int FindNamedExpression(Tcl_Interp*, BddSystemData*, Tcl_Obj*,
			       BDD_BeadIndex*);
static int UnsetNamedExpression(Tcl_Interp*, BddSystemData*, Tcl_Obj*);

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 BddSystemConstantMethod(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 BddSystemNegateMethod(ClientData, Tcl_Interp*, Tcl_ObjectContext,
				 int, Tcl_Obj* const[]);
static int BddSystemNotnthvarMethod(ClientData, Tcl_Interp*, Tcl_ObjectContext,
				    int, Tcl_Obj* const[]);
static int BddSystemNthvarMethod(ClientData, Tcl_Interp*, Tcl_ObjectContext,
				 int, Tcl_Obj* const[]);
static int BddSystemRestrictMethod(ClientData, Tcl_Interp*, Tcl_ObjectContext,
................................................................................
    "copy",			   /* name */
    BddSystemCopyMethod,	   /* callProc */
    DeleteMethod,		   /* method delete proc */
    CloneMethod			   /* method clone proc */
};
const static Tcl_MethodType BddSystemDumpMethodType = {
    TCL_OO_METHOD_VERSION_CURRENT, /* version */
    "copy",			   /* name */
    BddSystemDumpMethod,	   /* callProc */
    DeleteMethod,		   /* method delete proc */
    CloneMethod			   /* method clone proc */







};
const static Tcl_MethodType BddSystemNegateMethodType = {
    TCL_OO_METHOD_VERSION_CURRENT, /* version */
    "negate",			   /* name */
    BddSystemNegateMethod,	   /* callProc */
    DeleteMethod,		   /* method delete proc */
    CloneMethod			   /* method clone proc */
................................................................................
    { ">",         &BddSystemBinopMethodType,     (ClientData) BDD_BINOP_GT },
    { ">=",        &BddSystemBinopMethodType,     (ClientData) BDD_BINOP_GE },
    { "^",         &BddSystemBinopMethodType,     (ClientData) BDD_BINOP_XOR },
    { "beadindex", &BddSystemBeadindexMethodType, NULL },
    { "constant",  &BddSystemConstantMethodType,  NULL },
    { "copy",      &BddSystemCopyMethodType,      NULL },
    { "dump",      &BddSystemDumpMethodType,      NULL },


    { "nand",      &BddSystemBinopMethodType,     (ClientData) BDD_BINOP_NAND },
    { "nor",       &BddSystemBinopMethodType,     (ClientData) BDD_BINOP_NOR },
    { "notnthvar", &BddSystemNotnthvarMethodType, NULL },
    { "nthvar",    &BddSystemNthvarMethodType,    NULL },
    { "restrict",  &BddSystemRestrictMethodType,  NULL },
    { "satcount",  &BddSystemSatcountMethodType,  NULL },
    { "unset",     &BddSystemUnsetMethodType,     NULL },
................................................................................
    }
    return status;
}
 
/*
 *-----------------------------------------------------------------------------
 *



























































































































































































































 * BddSystemNegateMethod --
 *
 *	Computes the negation of a named expression in a system of
 *	BDD's and assigns it a name
 *
 * Usage:
 *	$system negate result operand
................................................................................
		Tcl_SetErrorCode(interp, "BDD", "NotLiteral",
				 Tcl_GetString(objv[i]), NULL);
		ckfree(restrictions);
		return TCL_ERROR;
	    }
	}
    }







    result = BDD_Restrict(sdata->system, expr, restrictions, nRestrictions);
    SetNamedExpression(sdata, objv[skipped], result);
    BDD_UnrefBead(sdata->system, result);
    return TCL_OK;
}
 
/*
................................................................................
    } else {
	BDD_UnrefBead(sdata->system,
		      (BDD_BeadIndex) (size_t)Tcl_GetHashValue(entryPtr));
	Tcl_DeleteHashEntry(entryPtr);
	return TCL_OK;
    }
}


























 
/*
 *-----------------------------------------------------------------------------
 *
 * DeleteBddSystemData --
 *
 *	Final cleanup when a system of BDD's is deleted







>













>
>
>
>
>
>







 







|



>
>
>
>
>
>
>







 







>
>







 







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







 







>
>
>
>
>
>
>







 







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







92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
...
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
...
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
...
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
....
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
....
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
static void SetNamedExpression(BddSystemData*, Tcl_Obj*, BDD_BeadIndex);
static Tcl_HashEntry* FindHashEntryForNamedExpression(Tcl_Interp*,
						      BddSystemData*,
						      Tcl_Obj*);
static int FindNamedExpression(Tcl_Interp*, BddSystemData*, Tcl_Obj*,
			       BDD_BeadIndex*);
static int UnsetNamedExpression(Tcl_Interp*, BddSystemData*, Tcl_Obj*);
static int CompareValueAssignments(const void* a, const void* b);
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 BddSystemConstantMethod(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*,
			 Tcl_Obj* varName, Tcl_Obj* script,
			 BDD_AllSatState* stateVector);
static int ForeachSatPost(ClientData data[4], Tcl_Interp*, int);
static int BddSystemNegateMethod(ClientData, Tcl_Interp*, Tcl_ObjectContext,
				 int, Tcl_Obj* const[]);
static int BddSystemNotnthvarMethod(ClientData, Tcl_Interp*, Tcl_ObjectContext,
				    int, Tcl_Obj* const[]);
static int BddSystemNthvarMethod(ClientData, Tcl_Interp*, Tcl_ObjectContext,
				 int, Tcl_Obj* const[]);
static int BddSystemRestrictMethod(ClientData, Tcl_Interp*, Tcl_ObjectContext,
................................................................................
    "copy",			   /* name */
    BddSystemCopyMethod,	   /* callProc */
    DeleteMethod,		   /* method delete proc */
    CloneMethod			   /* method clone proc */
};
const static Tcl_MethodType BddSystemDumpMethodType = {
    TCL_OO_METHOD_VERSION_CURRENT, /* version */
    "dump",			   /* name */
    BddSystemDumpMethod,	   /* callProc */
    DeleteMethod,		   /* method delete proc */
    CloneMethod			   /* method clone proc */
};
const static Tcl_MethodType BddSystemForeachSatMethodType = {
    TCL_OO_METHOD_VERSION_CURRENT, /* version */
    "foreach_sat",		   /* name */
    BddSystemForeachSatMethod,	   /* callProc */
    DeleteMethod,		   /* method delete proc */
    CloneMethod			   /* method clone proc */
};
const static Tcl_MethodType BddSystemNegateMethodType = {
    TCL_OO_METHOD_VERSION_CURRENT, /* version */
    "negate",			   /* name */
    BddSystemNegateMethod,	   /* callProc */
    DeleteMethod,		   /* method delete proc */
    CloneMethod			   /* method clone proc */
................................................................................
    { ">",         &BddSystemBinopMethodType,     (ClientData) BDD_BINOP_GT },
    { ">=",        &BddSystemBinopMethodType,     (ClientData) BDD_BINOP_GE },
    { "^",         &BddSystemBinopMethodType,     (ClientData) BDD_BINOP_XOR },
    { "beadindex", &BddSystemBeadindexMethodType, NULL },
    { "constant",  &BddSystemConstantMethodType,  NULL },
    { "copy",      &BddSystemCopyMethodType,      NULL },
    { "dump",      &BddSystemDumpMethodType,      NULL },
    { "foreach_sat",
                   &BddSystemForeachSatMethodType,NULL },
    { "nand",      &BddSystemBinopMethodType,     (ClientData) BDD_BINOP_NAND },
    { "nor",       &BddSystemBinopMethodType,     (ClientData) BDD_BINOP_NOR },
    { "notnthvar", &BddSystemNotnthvarMethodType, NULL },
    { "nthvar",    &BddSystemNthvarMethodType,    NULL },
    { "restrict",  &BddSystemRestrictMethodType,  NULL },
    { "satcount",  &BddSystemSatcountMethodType,  NULL },
    { "unset",     &BddSystemUnsetMethodType,     NULL },
................................................................................
    }
    return status;
}
 
/*
 *-----------------------------------------------------------------------------
 *
 * BddSystemForeachSatMethod --
 *
 *	Iterates over all satisfying variable assignments of a BDD.
 *
 * Usage:
 *	$system foreach_sat $var $expr $script
 *
 * Parameters:
 *	system - System of BDD's (this object)
 *	var - Name of a Tcl variable that will hold an assignment
 *	expr - Name of the expression whose variable assignments are to
 *	       be enumerated
 *	script - Tcl script to execute once per satisfying assignment
 *
 * Results:
 *	Returns a standard Tcl result.
 *
 * Side effects:
 *	Whatever the script does.
 *
 * The script, like any Tcl loop body, may return any Tcl status code.
 * A status code of OK or CONTINUE advances to the next satisfying assignment,
 * or causes the loop to return OK if no more satisfying assignments exist.
 * A status code of BREAK causes the loop to return OK immediately.
 * A status code of RETURN, or any other return code, is reported as
 * the status of the 'foreach_sat' construct.
 *
 *-----------------------------------------------------------------------------
 */

static int
BddSystemForeachSatMethod(
    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 exprIndex;	/* Bead index of the named expression */
    BDD_AllSatState* stateVector;
				/* State vector for the enumeration */

    if (objc != skipped+3) {
	Tcl_WrongNumArgs(interp, skipped, objv, "varName exprName script");
	return TCL_ERROR;
    }
    if (FindNamedExpression(interp, sdata, objv[skipped+1],
			    &exprIndex) != TCL_OK) {
	return TCL_ERROR;
    }
    stateVector = BDD_AllSatStart(sdata->system, exprIndex);
    IncrBddSystemDataRefCount(sdata);
    return ForeachSatPre(interp, sdata, objv[skipped], objv[skipped+2],
			 stateVector);
}
 
/*
 *-----------------------------------------------------------------------------
 *
 * ForeachSatPre --
 *
 *	Sets up a call to a 'foreach_sat' loop body.
 *
 * Results:
 *	Returns a standard Tcl result.
 *
 * Side effects:
 *	If the enumeration has further assignments to process, gets the next
 *	one and sets up to invoke the loop body through Tcl_NREvalObj. 
 *	Otherwise, destroys the enumeration and returns TCL_OK to finish the
 *	loop.
 *
 *	Invoking the loop body, of course, has arbitrary side effects.
 *
 *-----------------------------------------------------------------------------
 */

static int
ForeachSatPre(
    Tcl_Interp* interp,		/* Tcl interpreter */
    BddSystemData* sdata,	/* System of BDD's */
    Tcl_Obj* varName,		/* Name of the loop variable */
    Tcl_Obj* script,		/* Loop body */
    BDD_AllSatState* stateVector) /* State vector for the enumeration */
{
    BDD_ValueAssignment* v;	/* Value assignments for the current
				 * satisfier */
    BDD_VariableIndex n;	/* Count of value assignments */
    Tcl_Obj* sat;		/* Satisfying values */
    BDD_VariableIndex i;

    /*
     * Time to terminate the loop?
     */
    if (!BDD_AllSatNext(stateVector, &v, &n)) {
	BDD_AllSatFinish(stateVector);
	DecrBddSystemDataRefCount(sdata);
	return TCL_OK;
    }

    /*
     * Make a satisfying assignment
     */
    sat = Tcl_NewObj();
    for (i = 0; i < n; ++i) {
	Tcl_ListObjAppendElement(NULL, sat,
				 Tcl_NewWideIntObj((Tcl_WideInt) v[i].var));
	Tcl_ListObjAppendElement(NULL, sat,
				 Tcl_NewBooleanObj((int) v[i].value));
    }
    Tcl_IncrRefCount(sat);

    /*
     * Stash the satisfying assignment in the loop variable
     */
    if (Tcl_ObjSetVar2(interp, varName, NULL, sat, TCL_LEAVE_ERR_MSG) == NULL) {
	Tcl_DecrRefCount(sat);
	BDD_AllSatFinish(stateVector);
	DecrBddSystemDataRefCount(sdata);
	return TCL_ERROR;
    }
    Tcl_DecrRefCount(sat);

    Tcl_NRAddCallback(interp, ForeachSatPost, 
		      (ClientData) sdata,
		      (ClientData) varName,
		      (ClientData) script,
		      (ClientData) stateVector);
    if (Tcl_NREvalObj(interp, script, 0) != TCL_OK) {
	BDD_AllSatFinish(stateVector);
	DecrBddSystemDataRefCount(sdata);
	return TCL_ERROR;
    }
    return TCL_OK;
}
 
/*
 *-----------------------------------------------------------------------------
 *
 * ForeachSatPost --
 *
 *	Handle return from the loop body in a 'foreach_sat' construct.
 *
 * Results:
 *	Returns a standard Tcl return code.
 *
 * Side effects:
 *	Schedules the next iteration of the loop body.
 *
 * If the script returns TCL_OK or TCL_CONTINUE, this procedure schedules the
 * next iteration and returns TCL_OK.
 * If the script returns TCL_BREAK, this procedure returns TCL_OK without
 * scheduling the next iteration.
 * If the script returns TCL_RETURN, this procedure returns TCL_RETURN.
 * If the script returns TCL_ERROR or a nonstandard return code, this
 * procedure returns the same error or return code.
 *
 * The client data layout is:
 *	0 - Pointer to the BDD system data
 *	1 - Loop variable name
 *	2 - Loop body
 *	3 - State vector for the enumeration.
 *
 *-----------------------------------------------------------------------------
 */

static int
ForeachSatPost(
    ClientData clientData[4],	/* Client data */
    Tcl_Interp* interp,		/* Tcl interpreter */
    int result)			/* Result code from the loop body */
{
    BddSystemData* sdata = (BddSystemData*) clientData[0];
    Tcl_Obj* var = (Tcl_Obj*) clientData[1];
    Tcl_Obj* script = (Tcl_Obj*) clientData[2];
    BDD_AllSatState* stateVector = (BDD_AllSatState*) clientData[3];

    /* Handle the result and continue iterating if appropriate */

    switch(result) {
    case TCL_OK:
    case TCL_CONTINUE:
	/*
	 * We need to reset the result, so that an error message will not
	 * be appended to the result of the last evaluation.
	 */
	Tcl_ResetResult(interp);
	return ForeachSatPre(interp, sdata, var, script, stateVector);
    case TCL_BREAK:
	result = TCL_OK;
	Tcl_ResetResult(interp);
	break;
    case TCL_ERROR:
	Tcl_AppendObjToErrorInfo(interp,
				 Tcl_ObjPrintf("\n   ('foreach_sat' body, "
					       "line %d)",
					       Tcl_GetErrorLine(interp)));
	/* add to backtrace */
	break;
    default:
	break;
    }

    /* Clean up memory */
    BDD_AllSatFinish(stateVector);
    DecrBddSystemDataRefCount(sdata);
    return result;
}
 
/*
 *-----------------------------------------------------------------------------
 *
 * BddSystemNegateMethod --
 *
 *	Computes the negation of a named expression in a system of
 *	BDD's and assigns it a name
 *
 * Usage:
 *	$system negate result operand
................................................................................
		Tcl_SetErrorCode(interp, "BDD", "NotLiteral",
				 Tcl_GetString(objv[i]), NULL);
		ckfree(restrictions);
		return TCL_ERROR;
	    }
	}
    }
    
    /*
     * Order the literals
     */
    qsort(restrictions, nRestrictions, sizeof(BDD_ValueAssignment),
	  CompareValueAssignments);

    result = BDD_Restrict(sdata->system, expr, restrictions, nRestrictions);
    SetNamedExpression(sdata, objv[skipped], result);
    BDD_UnrefBead(sdata->system, result);
    return TCL_OK;
}
 
/*
................................................................................
    } else {
	BDD_UnrefBead(sdata->system,
		      (BDD_BeadIndex) (size_t)Tcl_GetHashValue(entryPtr));
	Tcl_DeleteHashEntry(entryPtr);
	return TCL_OK;
    }
}
 
/*
 *-----------------------------------------------------------------------------
 *
 * CompareValueAssignments --
 *
 *	Callback for 'qsort' to compare value assignments based on their
 *	variable indices.
 *
 * Results:
 *	Returns -1 if a < b; 0 if a == b, 1 if a > b.
 *
 *-----------------------------------------------------------------------------
 */

static int
CompareValueAssignments(
    const void* a,		/* Pointer to first assignment */
    const void* b)		/* Pointer to second assignment */
{
    const BDD_ValueAssignment* aPtr = (const BDD_ValueAssignment*) a;
    const BDD_ValueAssignment* bPtr = (const BDD_ValueAssignment*) b;
    if (aPtr->var < bPtr->var) return -1;
    else if (aPtr->var > bPtr->var) return 1;
    else return 0;
}
 
/*
 *-----------------------------------------------------------------------------
 *
 * DeleteBddSystemData --
 *
 *	Final cleanup when a system of BDD's is deleted

Changes to tests/bdd.test.

830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
....
1103
1104
1105
1106
1107
1108
1109

















1110
1111
1112
1113
1114
1115
1116
1117
....
1157
1158
1159
1160
1161
1162
1163



































































































































































1164
1165
1166
1167
1168
1169
    }
    -cleanup {
	rename sys {}
    }
    -result {257 80000000000000000000000000000000}
}

test bdd-12.12 {eight queens} {*}{
    -setup {
	bdd::system create sys
	for {set i 0} {$i < 64} {incr i} {
	    sys nthvar v$i $i; sys notnthvar !v$i $i
	}
    }
    -body {

	set result {}

	sys constant solution 1

	# iterate through the cells

	for {set row 0} {$row < 8} {incr row} {
	    sys constant thisRowC 1
	    for {set col 7} {$col >= 0} {incr col -1} {
		sys constant cellC 1
		set q1 v[expr {8* $row + $col}]

		# queen here <= no other queen in the same column

		sys constant columnC 1
		for {set col2 7} {$col2 >= 0} {incr col2 -1} {
		    if {$col2 != $col} {
			set q2 v[expr {8 * $row + $col2}]
			sys & columnC columnC !$q2
		    }
		}
		sys <= columnC $q1 columnC
		sys & cellC cellC columnC
		
		# queen here <= no other queen in the same row

		sys constant rowC 1
		for {set row2 7} {$row2 >= 0} {incr row2 -1} {
		    if {$row2 != $row} {
			set q2 v[expr {8 * $row2 + $col}]
			sys & rowC rowC !$q2
		    }
		}
		sys <= rowC $q1 rowC
		sys & cellC cellC rowC

		# queen here <= no other queen in the same diagonal

		sys constant diag1C 1
		sys constant diag2C 1
		for {set row2 7} {$row2 >= 0} {incr row2 -1} {
		    if {$row2 != $row} {
			set col2 [expr {$col + $row2 - $row}]
			if {$col2 >= 0 && $col2 < 8} {
			    set q2 v[expr {8 * $row2 + $col2}]
			    sys & diag1C diag1C !$q2
			}
			set col2 [expr {$col + $row - $row2}]
			if {$col2 >= 0 && $col2 < 8} {
			    set q2 v[expr {8 * $row2 + $col2}]
			    sys & diag2C diag2C !$q2
			}
		    }
		}
		sys <= diag1C $q1 diag1C
		sys & cellC cellC diag1C

		sys <= diag2C $q1 diag2C
		sys & cellC cellC diag2C

		# accumulate into the constraint set for the row

		sys & thisRowC thisRowC cellC
	    }

	    # accumulate all constraints for a row into the solution

	    sys & solution solution thisRowC
	}
		
	# at least one queen in each column
	for {set col 0} {$col < 8} {incr col} {
	    sys constant columnC 0
	    for {set row 0} {$row < 8} {incr row} {
		set q v[expr {8*$row + $col}]
		sys | columnC columnC $q
	    }
	    sys & solution solution columnC
	}

	sys satcount solution

    }
    -cleanup {rename sys {}}
    -result 92
}

test bdd-12.13 {verification of a full adder} {
    -setup {
	bdd::system create sys
	sys nthvar x 0; sys notnthvar !x 0
	sys nthvar y 1; sys notnthvar !y 1
	sys nthvar ci 2; sys notnthvar !ci 2
    }
    -body {

	# specification is a truth table
	sys constant spec_co 0
	sys constant spec_z 0
	foreach {x y ci co z} {
	    0 0 0 0 0
	    0 0 1 0 1
	    0 1 0 0 1
	    0 1 1 1 0

	    1 0 0 0 1
	    1 0 1 1 0
	    1 1 0 1 0
	    1 1 1 1 1
	} {
	    if {$x} {
		set xlit x
	    } else {
		set xlit !x
	    }
	    if {$y} {
		set ylit y
	    } else {
		set ylit !y
	    }
	    if {$ci} {
		set cilit ci
	    } else {
		set cilit !ci
	    }
	    sys & input $cilit $ylit
	    sys & input input $xlit
	    if {$co} {
		sys | spec_co spec_co input
	    }
	    if {$z} {
		sys | spec_z spec_z input
	    }
	}

	# implementation is a gate array
	sys & gate1 x y
	sys ^ gate2 x y
	sys & gate3 gate2 ci
	sys ^ z gate2 ci
	sys | co gate1 gate3

	list [expr {[sys beadindex co] == [sys beadindex spec_co]}] \
	    [expr {[sys beadindex z] == [sys beadindex spec_z]}]
    }
    -cleanup {rename sys {}}
    -result {1 1}
}
		
test bdd-13.1 {restrict - wrong # args} {*}{
    -setup {
	bdd::system create sys
	sys nthvar a 0
	sys nthvar b 1
	sys nthvar c 2
    }
................................................................................
	sys restrict r1 r2 a c
	expr {[sys beadindex r1] == [sys beadindex b]}
    }
    -cleanup {rename sys {}}
    -result 1
}


















test bdd-13.8 {restrict - reduce to constant} {*}{
    -setup {
	bdd::system create sys
	sys nthvar a 0
	sys nthvar b 1
	sys nthvar c 2
    }
    -body {
................................................................................
	sys restrict r1 1 a c
	sys beadindex r1
    }
    -cleanup {rename sys {}}
    -result 1
}




































































































































































cleanupTests
return

# Local Variables:
# mode: tcl
# End:







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|







 







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






830
831
832
833
834
835
836






























































































































































837
838
839
840
841
842
843
...
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
....
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
    }
    -cleanup {
	rename sys {}
    }
    -result {257 80000000000000000000000000000000}
}































































































































































test bdd-13.1 {restrict - wrong # args} {*}{
    -setup {
	bdd::system create sys
	sys nthvar a 0
	sys nthvar b 1
	sys nthvar c 2
    }
................................................................................
	sys restrict r1 r2 a c
	expr {[sys beadindex r1] == [sys beadindex b]}
    }
    -cleanup {rename sys {}}
    -result 1
}

test bdd-13.8 {restrict by two disordered literals} {*}{
    -setup {
	bdd::system create sys
	sys nthvar a 0; sys notnthvar !a 0
	sys nthvar b 1; sys notnthvar !b 1
	sys nthvar c 2; sys notnthvar !c 2
	sys ^ r2 c b
	sys ^ r2  a r2
    }
    -body {
	sys restrict r1 r2 c a
	expr {[sys beadindex r1] == [sys beadindex b]}
    }
    -cleanup {rename sys {}}
    -result 1
}

test bdd-13.9 {restrict - reduce to constant} {*}{
    -setup {
	bdd::system create sys
	sys nthvar a 0
	sys nthvar b 1
	sys nthvar c 2
    }
    -body {
................................................................................
	sys restrict r1 1 a c
	sys beadindex r1
    }
    -cleanup {rename sys {}}
    -result 1
}

test bdd-21.1 {eight queens} {*}{
    -setup {
	bdd::system create sys
	for {set i 0} {$i < 64} {incr i} {
	    sys nthvar v$i $i; sys notnthvar !v$i $i
	}
    }
    -body {
	sys copy solution 1

	# iterate through the cells

	for {set row 0} {$row < 8} {incr row} {
	    sys copy thisRowC 1
	    for {set col 7} {$col >= 0} {incr col -1} {
		sys copy cellC 1
		set q1 v[expr {8* $row + $col}]

		# queen here <= no other queen in the same column

		sys copy columnC 1
		for {set col2 7} {$col2 >= 0} {incr col2 -1} {
		    if {$col2 != $col} {
			set q2 v[expr {8 * $row + $col2}]
			sys & columnC columnC !$q2
		    }
		}
		sys <= columnC $q1 columnC
		sys & cellC cellC columnC
		
		# queen here <= no other queen in the same row

		sys copy rowC 1
		for {set row2 7} {$row2 >= 0} {incr row2 -1} {
		    if {$row2 != $row} {
			set q2 v[expr {8 * $row2 + $col}]
			sys & rowC rowC !$q2
		    }
		}
		sys <= rowC $q1 rowC
		sys & cellC cellC rowC

		# queen here <= no other queen in the same diagonal

		sys copy diag1C 1
		sys copy diag2C 1
		for {set row2 7} {$row2 >= 0} {incr row2 -1} {
		    if {$row2 != $row} {
			set col2 [expr {$col + $row2 - $row}]
			if {$col2 >= 0 && $col2 < 8} {
			    set q2 v[expr {8 * $row2 + $col2}]
			    sys & diag1C diag1C !$q2
			}
			set col2 [expr {$col + $row - $row2}]
			if {$col2 >= 0 && $col2 < 8} {
			    set q2 v[expr {8 * $row2 + $col2}]
			    sys & diag2C diag2C !$q2
			}
		    }
		}
		sys <= diag1C $q1 diag1C
		sys & cellC cellC diag1C

		sys <= diag2C $q1 diag2C
		sys & cellC cellC diag2C

		# accumulate into the constraint set for the row

		sys & thisRowC thisRowC cellC
	    }

	    # accumulate all constraints for a row into the solution

	    sys & solution solution thisRowC
	}
		
	# at least one queen in each column
	for {set col 0} {$col < 8} {incr col} {
	    sys copy columnC 0
	    for {set row 0} {$row < 8} {incr row} {
		set q v[expr {8*$row + $col}]
		sys | columnC columnC $q
	    }
	    sys & solution solution columnC
	}
	sys foreach_sat A solution {
	    set sol [lrepeat 8 {}]
	    foreach {var value} $A {
		if {$value} {
		    lset sol [expr {$var/8}] [expr {$var%8}]
		}
	    }
	    lappend sols [join $sol {}]
	}
	lreverse $sols

    }
    -cleanup {rename sys {}}
    -result {04752613 05726314 06357142 06471352 13572064 14602753 14630752 15063724 15720364 16257403 16470352 17502463 20647135 24170635 24175360 24603175 24730615 25147063 25160374 25164073 25307461 25317460 25703641 25704613 25713064 26174035 26175304 27360514 30471625 30475261 31475026 31625704 31625740 31640752 31746025 31750246 35041726 35716024 35720641 36074152 36271405 36415027 36420571 37025164 37046152 37420615 40357162 40731625 40752613 41357206 41362750 41506372 41703625 42057136 42061753 42736051 46027531 46031752 46137025 46152037 46152073 46302751 47302516 47306152 50417263 51602473 51603742 52064713 52073164 52074136 52460317 52470316 52613704 52617403 52630714 53047162 53174602 53602417 53607142 57130642 60275314 61307425 61520374 62057413 62714053 63147025 63175024 64205713 71306425 71420635 72051463 73025164}
}

test bdd-21.2 {verification of a full adder} {
    -setup {
	bdd::system create sys
	sys nthvar x 0; sys notnthvar !x 0
	sys nthvar y 1; sys notnthvar !y 1
	sys nthvar ci 2; sys notnthvar !ci 2
    }
    -body {

	# specification is a truth table
	sys copy spec_co 0
	sys copy spec_z 0
	foreach {x y ci co z} {
	    0 0 0 0 0
	    0 0 1 0 1
	    0 1 0 0 1
	    0 1 1 1 0

	    1 0 0 0 1
	    1 0 1 1 0
	    1 1 0 1 0
	    1 1 1 1 1
	} {
	    if {$x} {
		set xlit x
	    } else {
		set xlit !x
	    }
	    if {$y} {
		set ylit y
	    } else {
		set ylit !y
	    }
	    if {$ci} {
		set cilit ci
	    } else {
		set cilit !ci
	    }
	    sys & input $cilit $ylit
	    sys & input input $xlit
	    if {$co} {
		sys | spec_co spec_co input
	    }
	    if {$z} {
		sys | spec_z spec_z input
	    }
	}

	# implementation is a gate array
	sys & gate1 x y
	sys ^ gate2 x y
	sys & gate3 gate2 ci
	sys ^ z gate2 ci
	sys | co gate1 gate3

	list [expr {[sys beadindex co] == [sys beadindex spec_co]}] \
	    [expr {[sys beadindex z] == [sys beadindex spec_z]}]
    }
    -cleanup {rename sys {}}
    -result {1 1}
}
		
cleanupTests
return

# Local Variables:
# mode: tcl
# End: