Check-in [45268b8331]

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

Overview
Comment:Added a peephole optimization where a join or antijoin immediately followed by a project gets coalesced into a single operation.
Timelines: family | ancestors | trunk
Files: files | file ages | folders
SHA1:45268b83314435963df7afae068843fa965845fd
User & Date: kbk 2015-01-31 20:17:21
Context
2015-01-31
20:17
Added a peephole optimization where a join or antijoin immediately followed by a project gets coalesced into a single operation. Leaf check-in: 45268b8331 user: kbk tags: trunk
17:06
Add instrumentation of generated code for performance analysis. check-in: ec5ea2c8d2 user: kbk tags: trunk
Changes

Changes to doc/tclbdd.n.

92
93
94
95
96
97
98

99
100
101
102
103
104
105
...
523
524
525
526
527
528
529
530





531
532
533
534
535
536
537
\fIsystem\fR \fBexists_\fR\fIbinop\fR \fIresultBdd\fR \fIvarList\fR \fIbdd1\fR \fIbdd2\fR
\fIsystem\fR \fBforall_\fR\fIbinop\fR \fIresultBdd\fR \fIvarList\fR \fIbdd1\fR \fIbdd2\fR

\fIsystem\fR \fIternop\fR \fIresultBdd\fR \fIbdd1\fR \fIbdd2\fR \fIbdd3\fR

\fIsystem\fR \fBload\fR \fIresultBdd\fR \fImapping\fR \fIvalue...\fR
\fIsystem\fR \fBproject\fR \fBresultBdd\fR \fIindexList\fR \fBbdd\fR

\fIsystem\fR \fBreplace\fR \fBresultBdd\fR \fIindexList\fR \fIindexList2\fR \fIbdd\fR

\fBbdd::foreach_fullsat\fR \fItclVar\fB \fIvarlist\fR \fIterm\fR \fIscript\fR
.fi
.SH "ARGUMENTS"
.TP
\fIbdd\fR, \fIbdd2\fR, \fIbdd3\fR...
................................................................................
No \fIvalue\fR may be greater than the maximum value of a \fBTcl_WideInt\fR.
.IP
It is expected that the \fBload\fR method may be useful to other code that
wishes to express Boolean functions by enumerating rows of their truth tables
or in "sum of products" form.
.TP
\fIsystem\fR \fBproject\fR \fIresultBdd\fR \fIvarList\fR \fIbdd\fR
This is a convenience method equivalent to [\fIsystem\fR \fBforall\fR],





except that the \fIvarList\fR argument contains a list of variable numbers,
rather than of variable names.
.TP
\fIsystem\fR \fBreplace\fR \fIresultBdd\fR \fIvarsToReplace\fR \fIreplacementVars\fR \fIbdd\fR
This is a convenience method equivalent to [\fIsystem\fR \fBcompose\fR]. In
this method, the \fIvarsToReplace\fR parameter is a list of variable
numbers to substitute, and \fIreplacementVars\fI is a list of the same length,







>







 







|
>
>
>
>
>







92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
...
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
\fIsystem\fR \fBexists_\fR\fIbinop\fR \fIresultBdd\fR \fIvarList\fR \fIbdd1\fR \fIbdd2\fR
\fIsystem\fR \fBforall_\fR\fIbinop\fR \fIresultBdd\fR \fIvarList\fR \fIbdd1\fR \fIbdd2\fR

\fIsystem\fR \fIternop\fR \fIresultBdd\fR \fIbdd1\fR \fIbdd2\fR \fIbdd3\fR

\fIsystem\fR \fBload\fR \fIresultBdd\fR \fImapping\fR \fIvalue...\fR
\fIsystem\fR \fBproject\fR \fBresultBdd\fR \fIindexList\fR \fBbdd\fR
\fIsystem\fR \fBproject_\fR\fIbinop\fR \fIresultBdd\fR \fIvarList\fR \fIbdd1\fR \fIbdd2\fR
\fIsystem\fR \fBreplace\fR \fBresultBdd\fR \fIindexList\fR \fIindexList2\fR \fIbdd\fR

\fBbdd::foreach_fullsat\fR \fItclVar\fB \fIvarlist\fR \fIterm\fR \fIscript\fR
.fi
.SH "ARGUMENTS"
.TP
\fIbdd\fR, \fIbdd2\fR, \fIbdd3\fR...
................................................................................
No \fIvalue\fR may be greater than the maximum value of a \fBTcl_WideInt\fR.
.IP
It is expected that the \fBload\fR method may be useful to other code that
wishes to express Boolean functions by enumerating rows of their truth tables
or in "sum of products" form.
.TP
\fIsystem\fR \fBproject\fR \fIresultBdd\fR \fIvarList\fR \fIbdd\fR
This is a convenience method equivalent to [\fIsystem\fR \fBexists\fR],
except that the \fIvarList\fR argument contains a list of variable numbers,
rather than of variable names.
.TP
\fIsystem\fR \fBproject_\fR\fIbinop\fR \fIresultBdd\fR \fIvarList\fR \fIbdd\fR
This is a convenience method equivalent to [\fIsystem\fR \fBexists_\fR\fIbinop\fR],
except that the \fIvarList\fR argument contains a list of variable numbers,
rather than of variable names.
.TP
\fIsystem\fR \fBreplace\fR \fIresultBdd\fR \fIvarsToReplace\fR \fIreplacementVars\fR \fIbdd\fR
This is a convenience method equivalent to [\fIsystem\fR \fBcompose\fR]. In
this method, the \fIvarsToReplace\fR parameter is a list of variable
numbers to substitute, and \fIreplacementVars\fI is a list of the same length,

Changes to doc/tclfddd.n.

75
76
77
78
79
80
81

82
83
84

85
86
87
88
89
90
91
...
265
266
267
268
269
270
271













272
273
274
275
276
277
278
...
295
296
297
298
299
300
301











302
303
304
305
306
307
308
\fIdb\fR \fBrelation\fR \fIrelation\fR ?\fIcolName\fR...?
\fIdb\fR \fBforget_relation\fR ?\fIrelation\fR...?

\fIdb\fR \fBloader\fR \fIrelation\fR

\fIdb\fR \fB===\fR \fIrelation1\fR \fIrelation2\fR
\fIdb\fR \fBantijoin\fR \fIresultRelation\fR \fIrelation1\fR \fRrelation2\fR

\fIdb\fR \fBequate\fR \fIresultRelation\fR \fIcolName1\fR \fIcolName2\fR
\fIdb\fR \fBinequality\fR \fIresultRelation\fR \fIcolName1\fR \fIcolName2\fR
\fIdb\fR \fBjoin\fR \fIresultRelation\fR \fIrelation1\fR \fRrelation2\fR

\fIdb\fR \fBnegate\fR \fIresultRelation\fR \fIrelation1\fR
\fIdb\fR \fBproject\fR \fIresultRelation\fR \fIrelation1\fR
\fIdb\fR \fBreplace\fR \fIresultRelation\fR \fIrelation1\fR ?\fIoutColName inColName\fR?...
\fIdb\fR \fBset\fR \fIresultRelation\fR \fIrelation1\fR
\fIdb\fR \fBunion\fR \fIresultRelation\fR \fIrelation1\fR

\fIdb\fR \fBcolumns\fR ?\fIrelation\fR?
................................................................................
that are present in \fIrelation1\fR but not in \fIrelation2\fR. 
.IP
If either of the input relations contains columns that are not present
in the other, the tuples of the other relation are presumed to contain
every possible value for the missing columns. Most commonly, \fIrelation2\fR
will have no excess columns, and will contain a set of values identifying
rows to be deleted.













.TP
\fIdb\fR \fBequate\fR \fIresultRelation\fR \fIcolName1\fR \fIcolName2\fR
This method generates and returns a codeburst that sets \fIresultRelation\fR
so that it contains every possible value for columns other than
\fIcolName1\fR and \fIcolName2\fR and only equal pairs of values for
those two columns. For any \fIresultRelation\fR, this set is determined 
uniquely.
................................................................................
have different values in the given columns.
.TP
\fIdb\fR \fBjoin\fR \fIresultRelation\fR \fIrelation1\fR \fRrelation2\fR
This method generates and returns a codeburst that computes the \fIjoin\fR
of two tables in \fIdb\fR whose names are \fIrelation1\fR and \fIrelation2\fR.
The result, which is placed in \fIresultRelation\fR, is the set of tuples
that are present in both \fIrelation1\fR and \fIrelation2\fR. 











.IP
If either of the input relations contains columns that are not present
in the other, the tuples of the other relation are presumed to contain
every matching value for the missing columns. 
.TP
\fIdb\fR \fBnegate\fR \fIresultRelation\fR \fIrelation1\fR
This method generates and returns







>



>







 







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







 







>
>
>
>
>
>
>
>
>
>
>







75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
...
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
...
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
\fIdb\fR \fBrelation\fR \fIrelation\fR ?\fIcolName\fR...?
\fIdb\fR \fBforget_relation\fR ?\fIrelation\fR...?

\fIdb\fR \fBloader\fR \fIrelation\fR

\fIdb\fR \fB===\fR \fIrelation1\fR \fIrelation2\fR
\fIdb\fR \fBantijoin\fR \fIresultRelation\fR \fIrelation1\fR \fRrelation2\fR
\fIdb\fR \fBantijoin+project\fR \fIresultRelation\fR \fIrelation1\fR \fRrelation2\fR
\fIdb\fR \fBequate\fR \fIresultRelation\fR \fIcolName1\fR \fIcolName2\fR
\fIdb\fR \fBinequality\fR \fIresultRelation\fR \fIcolName1\fR \fIcolName2\fR
\fIdb\fR \fBjoin\fR \fIresultRelation\fR \fIrelation1\fR \fRrelation2\fR
\fIdb\fR \fBjoin+project\fR \fIresultRelation\fR \fIrelation1\fR \fRrelation2\fR
\fIdb\fR \fBnegate\fR \fIresultRelation\fR \fIrelation1\fR
\fIdb\fR \fBproject\fR \fIresultRelation\fR \fIrelation1\fR
\fIdb\fR \fBreplace\fR \fIresultRelation\fR \fIrelation1\fR ?\fIoutColName inColName\fR?...
\fIdb\fR \fBset\fR \fIresultRelation\fR \fIrelation1\fR
\fIdb\fR \fBunion\fR \fIresultRelation\fR \fIrelation1\fR

\fIdb\fR \fBcolumns\fR ?\fIrelation\fR?
................................................................................
that are present in \fIrelation1\fR but not in \fIrelation2\fR. 
.IP
If either of the input relations contains columns that are not present
in the other, the tuples of the other relation are presumed to contain
every possible value for the missing columns. Most commonly, \fIrelation2\fR
will have no excess columns, and will contain a set of values identifying
rows to be deleted.
.TP
\fIdb\fR \fBantijoin+project\fR \fIresultRelation\fR \fIrelation1\fR \fIrelation2\fR
This method generates and returns a codeburst that computes the \fIantijoin\fR
of two tables in \fIdb\fR whose names are \fIrelation1\fR and \fIrelation2\fR.
The result, which is placed in \fIresultRelation\fR, is the set of tuples
that are present in \fIrelation1\fR but not in \fIrelation2\fR, with any
columns that are not present in \fIresultRelation\fR projected away.
.IP
If either of the input relations contains columns that are not present
in the other, the tuples of the other relation are presumed to contain
every possible value for the missing columns. Most commonly, \fIrelation2\fR
will have no excess columns, and will contain a set of values identifying
rows to be deleted.
.TP
\fIdb\fR \fBequate\fR \fIresultRelation\fR \fIcolName1\fR \fIcolName2\fR
This method generates and returns a codeburst that sets \fIresultRelation\fR
so that it contains every possible value for columns other than
\fIcolName1\fR and \fIcolName2\fR and only equal pairs of values for
those two columns. For any \fIresultRelation\fR, this set is determined 
uniquely.
................................................................................
have different values in the given columns.
.TP
\fIdb\fR \fBjoin\fR \fIresultRelation\fR \fIrelation1\fR \fRrelation2\fR
This method generates and returns a codeburst that computes the \fIjoin\fR
of two tables in \fIdb\fR whose names are \fIrelation1\fR and \fIrelation2\fR.
The result, which is placed in \fIresultRelation\fR, is the set of tuples
that are present in both \fIrelation1\fR and \fIrelation2\fR. 
.IP
If either of the input relations contains columns that are not present
in the other, the tuples of the other relation are presumed to contain
every matching value for the missing columns. 
.TP
\fIdb\fR \fBjoin+project\fR \fIresultRelation\fR \fIrelation1\fR \fRrelation2\fR
This method generates and returns a codeburst that computes the \fIjoin\fR
of two tables in \fIdb\fR whose names are \fIrelation1\fR and \fIrelation2\fR.
The result, which is placed in \fIresultRelation\fR, is the set of tuples
that are present in both \fIrelation1\fR and \fIrelation2\fR. Any columns not 
present in \fIresultRelation\fR are projected away.
.IP
If either of the input relations contains columns that are not present
in the other, the tuples of the other relation are presumed to contain
every matching value for the missing columns. 
.TP
\fIdb\fR \fBnegate\fR \fIresultRelation\fR \fIrelation1\fR
This method generates and returns

Changes to generic/tclBdd.c.

114
115
116
117
118
119
120


121
122
123
124
125
126
127
...
189
190
191
192
193
194
195







196
197
198
199
200
201
202
...
507
508
509
510
511
512
513

514









515
516
517
518
519
520
521
...
655
656
657
658
659
660
661

















































































































662
663
664
665
666
667
668
...
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
...
708
709
710
711
712
713
714
715




716
717
718
719
720
721
722
			       BDD_BeadIndex*);
static int UnsetNamedExpression(Tcl_Interp*, BddSystemData*, Tcl_Obj*);
static int CompareValueAssignments(const void* a, const void* b);
static int CompareVariableIndices(const void* a, const void* b);
static void DeletePerInterpData(PerInterpData*);
static int BddSystemConstructor(ClientData, Tcl_Interp*, Tcl_ObjectContext,
				int, Tcl_Obj* const[]);


static int BddSystemAppquantMethod(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,
................................................................................
    TCL_OO_METHOD_VERSION_CURRENT, /* version */
    "CONSTRUCTOR",		   /* name */
    BddSystemConstructor,	   /* callProc */
    DeleteMethod,		   /* method delete proc */
    CloneMethod			   /* method clone proc */
};








const static Tcl_MethodType BddSystemAppquantMethodType = {
    TCL_OO_METHOD_VERSION_CURRENT, /* version */
    "appquant",			   /* name */
    BddSystemAppquantMethod,	   /* callProc */
    DeleteMethod,		   /* method delete proc */
    CloneMethod			   /* method clone proc */
};
................................................................................
	    Tcl_AppendToObj(nameObj, "_", -1);
	    Tcl_AppendToObj(nameObj, opPtr->name, -1);
	    Tcl_IncrRefCount(nameObj);
	    Tcl_NewMethod(interp, curClass, nameObj, 1,
			  &BddSystemAppquantMethodType,
			  (ClientData) (size_t) ((quantPtr->quant<<4)
						 | (opPtr->op)));

	}









    }

    /* Provide the package */

    if (Tcl_PkgProvideEx(interp, PACKAGE_NAME, PACKAGE_VERSION,
			 ( ClientData) NULL) == TCL_ERROR) {
	DecrPerInterpRefCount(pidata);
................................................................................
    Tcl_SetObjResult(interp, Tcl_NewWideIntObj(beadIndex));
    return TCL_OK;
}
 
/*
 *-----------------------------------------------------------------------------
 *

















































































































 * BddSystemAppquantMethod --
 *
 *	Applies a binary operation to a pair of BDD's, then applies a
 *	quantification over a set of variables to the result.
 *
 * Usage:
 *	$system ${quant}_${op} $result $vars $expr1 $expr2
................................................................................
 *	quant  - 'exists' or 'forall' according to which quantification
 *               is desired
 *	op     - One of the binary operators !=, &, <, <=, ==, >. >=,
 *               ^, nand, nor, and |
 *      result - Name to be assigned to the resulting BDD.
 *	vars   - List of names of variables to quantify over
 *	expr1  - Left operand
 *	expr2  - Rught operand
 */

static int
BddSystemAppquantMethod(
    ClientData clientData,	/* unused */
    Tcl_Interp* interp,		/* Tcl interpreter */
    Tcl_ObjectContext ctx,	/* Object context */
................................................................................
    Tcl_Obj* errorMessage;	/* Error message for a failed execution */
    BDD_BeadIndex result;	/* Result of the operation */
    BDD_VariableIndex i;

    /* Check syntax */

    if (objc != skipped+4) {
	Tcl_WrongNumArgs(interp, skipped, objv, "name vars expr");




	return TCL_ERROR;
    }
    if (FindNamedExpression(interp, sdata, objv[skipped+2],
			    &u1) != TCL_OK
	|| FindNamedExpression(interp, sdata, objv[skipped+3],
			       &u2) != TCL_OK) {
	return TCL_ERROR;







>
>







 







>
>
>
>
>
>
>







 







>

>
>
>
>
>
>
>
>
>







 







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







 







|







 







|
>
>
>
>







114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
...
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
...
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
...
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
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
...
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
...
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
			       BDD_BeadIndex*);
static int UnsetNamedExpression(Tcl_Interp*, BddSystemData*, Tcl_Obj*);
static int CompareValueAssignments(const void* a, const void* b);
static int CompareVariableIndices(const void* a, const void* b);
static void DeletePerInterpData(PerInterpData*);
static int BddSystemConstructor(ClientData, Tcl_Interp*, Tcl_ObjectContext,
				int, Tcl_Obj* const[]);
static int BddSystemAppprojMethod(ClientData, Tcl_Interp*, Tcl_ObjectContext,
				  int, Tcl_Obj* const[]);
static int BddSystemAppquantMethod(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,
................................................................................
    TCL_OO_METHOD_VERSION_CURRENT, /* version */
    "CONSTRUCTOR",		   /* name */
    BddSystemConstructor,	   /* callProc */
    DeleteMethod,		   /* method delete proc */
    CloneMethod			   /* method clone proc */
};

const static Tcl_MethodType BddSystemAppprojMethodType = {
    TCL_OO_METHOD_VERSION_CURRENT, /* version */
    "appproj",			   /* name */
    BddSystemAppprojMethod,	   /* callProc */
    DeleteMethod,		   /* method delete proc */
    CloneMethod			   /* method clone proc */
};
const static Tcl_MethodType BddSystemAppquantMethodType = {
    TCL_OO_METHOD_VERSION_CURRENT, /* version */
    "appquant",			   /* name */
    BddSystemAppquantMethod,	   /* callProc */
    DeleteMethod,		   /* method delete proc */
    CloneMethod			   /* method clone proc */
};
................................................................................
	    Tcl_AppendToObj(nameObj, "_", -1);
	    Tcl_AppendToObj(nameObj, opPtr->name, -1);
	    Tcl_IncrRefCount(nameObj);
	    Tcl_NewMethod(interp, curClass, nameObj, 1,
			  &BddSystemAppquantMethodType,
			  (ClientData) (size_t) ((quantPtr->quant<<4)
						 | (opPtr->op)));
	    Tcl_DecrRefCount(nameObj);
	}
    }
    for (opPtr = BinOpTable; opPtr->name != NULL; ++opPtr) {
	nameObj = Tcl_NewStringObj("project_", -1);
	Tcl_AppendToObj(nameObj, opPtr->name, -1);
	Tcl_IncrRefCount(nameObj);
	Tcl_NewMethod(interp, curClass, nameObj, 1,
		      &BddSystemAppprojMethodType,
		      (ClientData) (size_t) (opPtr->op));
	Tcl_DecrRefCount(nameObj);
    }

    /* Provide the package */

    if (Tcl_PkgProvideEx(interp, PACKAGE_NAME, PACKAGE_VERSION,
			 ( ClientData) NULL) == TCL_ERROR) {
	DecrPerInterpRefCount(pidata);
................................................................................
    Tcl_SetObjResult(interp, Tcl_NewWideIntObj(beadIndex));
    return TCL_OK;
}
 
/*
 *-----------------------------------------------------------------------------
 *
 * BddSystemAppprojMethod --
 *
 *	Applies a binary operation to a pair of BDD's, then projects away a
 *	set of variables from the result, that is, applies the 'there exists'
 *	quantifier to those variables.
 *
 * Usage:
 *	$system project_${op} $result $vars $expr1 $expr2
 *
 * Parameters:
 *	system - System of BDD's
 *	op - Operation to apply before projection: one of the binary
 *	     operators !=, &, <, <=, ==, >. >=, ^, nand, nor, and |
 *	result - Name to be assigned to the resulting BDD
 *	vars - List of integers giving the positions of variables to project
 *	       away
 *	expr1 - Left operand to 'op'
 *	expr2 - Right operand to 'op'
 *
 * Results:
 *	Returns a standard Tcl result.
 *
 * Side effects:
 *	Creates the named 'result' expression if successful.
 *
 * This is the same operation as existentially quantified operator
 * application. It is provided for the convenience of Finite Domain
 * Decision Diagrams, where it implements the relational 'project'
 * operator following a 'join' or 'antijoin'. Note that the notation
 * is different from the usual 'project'; the variable list to this
 * command specifies the variables to discard, while the conventional
 * 'project' operation specifies the columns to keep.
 *
 *-----------------------------------------------------------------------------
 */

static int
BddSystemAppprojMethod(
    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 */
    BDD_BinOp op = (BDD_BinOp) ((size_t) clientData);
				/* The operation to apply */
    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 */
    int varc;			/* Number of variables to discard */
    Tcl_Obj** varv;		/* Indices of variables to project away */
    BDD_BeadIndex u1;		/* Left hand operand */
    BDD_BeadIndex u2;		/* Right hand operand */
    int vIndex;			/* Variable index as parsed by Tcl */
    BDD_VariableIndex* v;	/* Variables to discard */
    Tcl_Obj* errorMessage;	/* Error message from this method */
    BDD_BeadIndex result;	/* Result of the operation */
    BDD_VariableIndex i;

    /* Check syntax */

    if (objc != skipped+4) {
	Tcl_WrongNumArgs(interp, skipped, objv, "name varList expr1 expr2");
	return TCL_ERROR;
    }  
    if (Tcl_ListObjGetElements(interp, objv[skipped+1],
			       &varc, &varv) != TCL_OK
	|| FindNamedExpression(interp, sdata, objv[skipped+2],
			       &u1) != TCL_OK
	|| FindNamedExpression(interp, sdata, objv[skipped+3],
			       &u2) != TCL_OK) {
	return TCL_ERROR;
    }
    v = ckalloc(varc * sizeof(BDD_VariableIndex));
    for (i = 0; i < varc; ++i) {
	if (Tcl_GetIntFromObj(interp, varv[i], &vIndex) != TCL_OK) {
	    ckfree(v);
	    return TCL_ERROR;
	}
	if (vIndex < 0) {
	    errorMessage =
		Tcl_ObjPrintf("expected nonnegative integer but got \"%s\"",
			      Tcl_GetString(varv[i]));
	    Tcl_SetObjResult(interp, errorMessage);
	    Tcl_SetErrorCode(interp, "BDD", "NegativeVarIndex", 
			     Tcl_GetString(varv[i]), NULL);
	    return TCL_ERROR;
	}
	v[i] = (BDD_VariableIndex) vIndex;
	if (i > 0 && v[i] <= v[i-1]) {
	    Tcl_SetObjResult(interp, Tcl_NewStringObj("variables are not in "
						      "increasing order", -1));
	    Tcl_SetErrorCode(interp, "BDD", "VarsOutOfOrder", NULL);
	    ckfree(v);
	    return TCL_ERROR;
	}
    }

    result = BDD_ApplyAndQuantify(sdata->system, BDD_QUANT_EXISTS,
				  varc, v, op, u1, u2);
    ckfree(v);
    SetNamedExpression(sdata, objv[skipped], result);
    BDD_UnrefBead(sdata->system, result);
    return TCL_OK;
}
 
/*
 *-----------------------------------------------------------------------------
 *
 * BddSystemAppquantMethod --
 *
 *	Applies a binary operation to a pair of BDD's, then applies a
 *	quantification over a set of variables to the result.
 *
 * Usage:
 *	$system ${quant}_${op} $result $vars $expr1 $expr2
................................................................................
 *	quant  - 'exists' or 'forall' according to which quantification
 *               is desired
 *	op     - One of the binary operators !=, &, <, <=, ==, >. >=,
 *               ^, nand, nor, and |
 *      result - Name to be assigned to the resulting BDD.
 *	vars   - List of names of variables to quantify over
 *	expr1  - Left operand
 *	expr2  - Right operand
 */

static int
BddSystemAppquantMethod(
    ClientData clientData,	/* unused */
    Tcl_Interp* interp,		/* Tcl interpreter */
    Tcl_ObjectContext ctx,	/* Object context */
................................................................................
    Tcl_Obj* errorMessage;	/* Error message for a failed execution */
    BDD_BeadIndex result;	/* Result of the operation */
    BDD_VariableIndex i;

    /* Check syntax */

    if (objc != skipped+4) {
	Tcl_WrongNumArgs(interp, skipped, objv, "name vars expr1 expr2");
	return TCL_ERROR;
    }
    if (Tcl_ListObjGetElements(interp, objv[skipped+1],
			       &varc, &varv) != TCL_OK) {
	return TCL_ERROR;
    }
    if (FindNamedExpression(interp, sdata, objv[skipped+2],
			    &u1) != TCL_OK
	|| FindNamedExpression(interp, sdata, objv[skipped+3],
			       &u2) != TCL_OK) {
	return TCL_ERROR;

Changes to library/datalog.tcl.

1494
1495
1496
1497
1498
1499
1500














1501
1502
1503

1504
1505
1506
1507
1508
1509
1510
....
1609
1610
1611
1612
1613
1614
1615






1616
1617
1618
1619
1620
1621
1622
....
1638
1639
1640
1641
1642
1643
1644






1645
1646
1647
1648
1649
1650
1651
....
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
	    if {[dict exists $destColumn $col]} {
		lappend projectColumns $col
	    } else {
		set needProject 1
	    }
	}
	if {$needProject} {














	    lappend intcode \
		[list RELATION $projector $projectColumns] \
		[list PROJECT $projector $sourceRelation]

	    set renameSource $projector
	} else {
	    set renameSource $sourceRelation
	}

	# Rename columns from literal to destination.
	if {[llength $renamedFrom] > 0} {
................................................................................
		
		ANTIJOIN {
		    my startMeasure $ind body $instrumentLevel
		    append body $ind \
			[$db antijoin {*}[lrange $instr 1 end]] \n
		    my endMeasure $ind body $instrumentLevel $instr
		}






		BEGINLOOP {
		    append body $ind "while 1 \{\n"
		    set ind "$ind    "
		}
		ENDLOOP {
		    set command [$db === [lindex $instr 1] [lindex $instr 2]]
		    append body \
................................................................................
		}
		JOIN {
		    my startMeasure $ind body $instrumentLevel
		    append body $ind \
			[$db join {*}[lrange $instr 1 end]] \n
		    my endMeasure $ind body $instrumentLevel $instr
		}






		LOAD {
		    # append body $ind # $instr \n
		    set relation [lindex $instr 1]
		    if {![dict exists $loaders $relation]} {
			dict set loaders $relation [$db loader $relation]
		    }
		    append body $ind \
................................................................................
		    append body \
			[list $db enumerate [lindex $args 0] \
			     [lindex $instr 1] \
			     [lindex $args 1]] \n
		}

		default {
		    error "in generateCode: can't happen"
		}
	    }

	}
	if {$instrumentLevel > 0} {
	    append prologue $ind "puts \"\[info level 0\]:\""
	}







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







 







>
>
>
>
>
>







 







>
>
>
>
>
>







 







|







1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
....
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
....
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
....
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
	    if {[dict exists $destColumn $col]} {
		lappend projectColumns $col
	    } else {
		set needProject 1
	    }
	}
	if {$needProject} {

	    # Peephole optimization. If the previous operation was a join
	    # or antijoin resulting in a temporary variable, coalesce it with
	    # the projection, because the coalesced operation will be
	    # more than twice as fast as the two operations performed 
	    # separately.
	    if {[lindex $intcode end-1 0] eq "RELATION"
		&& [lindex $intcode end-1 1] eq $sourceRelation
		&& [lindex $intcode end 0] in {JOIN ANTIJOIN}
		&& [lindex $intcode end 1] eq $sourceRelation} {
		lset intcode end-1 [list RELATION $projector $projectColumns]
		lset intcode end 0 [lindex $intcode end 0]+PROJECT
		lset intcode end 1 $projector
	    } else {
		lappend intcode \
		    [list RELATION $projector $projectColumns] \
		    [list PROJECT $projector $sourceRelation]
	    }
	    set renameSource $projector
	} else {
	    set renameSource $sourceRelation
	}

	# Rename columns from literal to destination.
	if {[llength $renamedFrom] > 0} {
................................................................................
		
		ANTIJOIN {
		    my startMeasure $ind body $instrumentLevel
		    append body $ind \
			[$db antijoin {*}[lrange $instr 1 end]] \n
		    my endMeasure $ind body $instrumentLevel $instr
		}
		ANTIJOIN+PROJECT {
		    my startMeasure $ind body $instrumentLevel
		    append body $ind \
			[$db antijoin+project {*}[lrange $instr 1 end]] \n
		    my endMeasure $ind body $instrumentLevel $instr
		}
		BEGINLOOP {
		    append body $ind "while 1 \{\n"
		    set ind "$ind    "
		}
		ENDLOOP {
		    set command [$db === [lindex $instr 1] [lindex $instr 2]]
		    append body \
................................................................................
		}
		JOIN {
		    my startMeasure $ind body $instrumentLevel
		    append body $ind \
			[$db join {*}[lrange $instr 1 end]] \n
		    my endMeasure $ind body $instrumentLevel $instr
		}
		JOIN+PROJECT {
		    my startMeasure $ind body $instrumentLevel
		    append body $ind \
			[$db join+project {*}[lrange $instr 1 end]] \n
		    my endMeasure $ind body $instrumentLevel $instr
		}
		LOAD {
		    # append body $ind # $instr \n
		    set relation [lindex $instr 1]
		    if {![dict exists $loaders $relation]} {
			dict set loaders $relation [$db loader $relation]
		    }
		    append body $ind \
................................................................................
		    append body \
			[list $db enumerate [lindex $args 0] \
			     [lindex $instr 1] \
			     [lindex $args 1]] \n
		}

		default {
		    error "in generateCode: instr=$instr can't happen"
		}
	    }

	}
	if {$instrumentLevel > 0} {
	    append prologue $ind "puts \"\[info level 0\]:\""
	}

Changes to library/tclfddd.tcl.

390
391
392
393
394
395
396

































































397
398
399
400
401
402
403
...
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
...
756
757
758
759
760
761
762

































































763
764
765
766
767
768
769
...
886
887
888
889
890
891
892

893
894
895
896
897
898
899
	lappend destcolumns {*}[dict get $m_relcolumns $source2]
	set destcolumns [lsort -dictionary -unique $destcolumns]
	my ColumnsMustBe $dest $destcolumns

	# Make code to do the antijoin
	return [list [namespace which sys] > $dest $source1 $source2]
    }


































































    # Method: columnMustExist
    #
    #	Makes sure that a given column exists in the database
    #
    # Usage:
    #	my columnMustExist $name
................................................................................
    # Usage:
    #	$db join dest source1 source2
    #
    # Results:
    #	Returns a fragment of code that will perform the join.
    #
    # The result of the join is the set of rows that are
    # members of the $source relation but not members of the $dest
    # relation. This is well defined for any two relations: if either
    # relation contains excess columns, the other relation is presumed
    # to contain tuples with every possible value in those columns.
    # Of course, the excess columns may be projected away before or
    # after the join. Most commonly, the columns of $source2 will
    # all appear in $source1.
    #
................................................................................
	lappend destcolumns {*}[dict get $m_relcolumns $source2]
	set destcolumns [lsort -dictionary -unique $destcolumns]
	my ColumnsMustBe $dest $destcolumns

	# Make code to do the join
	return [list [namespace which sys] & $dest $source1 $source2]
    }


































































    # Method: loader
    #
    #	Generates code to call the BDD engine to construct a term 
    #   corresponding to a tuple in a finite domain.
    #
    # Usage:
................................................................................
    # source BDD.

    method project {dest source} {
	# columns to project away are determined by dest and source
	my relationMustExist $dest
	my relationMustExist $source
	set discards {}

	foreach col [dict get $m_relcolumns $dest] {
	    dict set want $col {}
	}
	foreach col [dict get $m_relcolumns $source] {
	    if {![dict exists $want $col]} {
		lappend discards {*}[dict get $m_columns $col]
	    } else {







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







 







|







 







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







 







>







390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
...
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
...
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
....
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
	lappend destcolumns {*}[dict get $m_relcolumns $source2]
	set destcolumns [lsort -dictionary -unique $destcolumns]
	my ColumnsMustBe $dest $destcolumns

	# Make code to do the antijoin
	return [list [namespace which sys] > $dest $source1 $source2]
    }

    # Method: antijoin+project
    #
    #	Generates code to antijoin a pair of relations and project away a
    #	set of columns
    #
    # Usage:
    #	$db antijoin+project dest source1 source2
    #
    # Results:
    #	Returns a fragment of code that will perform the antijoin and projection
    #
    # The result of the antijoin is the set of rows that are
    # members of the $source relation but not members of the $dest
    # relation. This is well defined for any two relations: if either
    # relation contains excess columns, the other relation is presumed
    # to contain tuples with every possible value in those columns.
    # Of course, the excess columns may be projected away before or
    # after the antijoin. Most commonly, the columns of $source2 will
    # all appear in $source1.
    #
    # The columns of the result relation must be the union of the
    # columns of the two inputs.
    #
    # This method does not perform the copy directly: it generates
    # code to perform the copy.
    #
    # The antijoin executes in time proportional to the size of the
    # source1 BDD.

    method antijoin+project {dest source1 source2} {

	my relationMustExist $dest
	my relationMustExist $source1
	my relationMustExist $source2

	# Calculate the union of the columns in the relations being joined.
	set sourcecolumns [dict get $m_relcolumns $source1]
	lappend sourcecolumns {*}[dict get $m_relcolumns $source2]
	set sourcecolumns [lsort -dictionary -unique $sourcecolumns]

	# Partition the source columns into those to be kept and those
	# to be projected away
	set discards {}
	set want {}
	foreach col [dict get $m_relcolumns $dest] {
	    dict set want $col {}
	}
	foreach col $sourcecolumns {
	    if {![dict exists $want $col]} {
		lappend discards {*}[dict get $m_columns $col]
	    } else {
		dict unset want $col
	    }
	}
	if {[dict size $want] != 0} {
	    return -code error \
		-errorcode [list FDDD ProjectColumnsMissing \
				[dict keys $want]]\
		"column(s) missing from source relation: [dict keys $want]"
	}

	return [list [namespace which sys] project_> \
		    $dest [lsort -integer $discards] $source1 $source2]
    }

    # Method: columnMustExist
    #
    #	Makes sure that a given column exists in the database
    #
    # Usage:
    #	my columnMustExist $name
................................................................................
    # Usage:
    #	$db join dest source1 source2
    #
    # Results:
    #	Returns a fragment of code that will perform the join.
    #
    # The result of the join is the set of rows that are
    # members of the $source relation and members of the $dest
    # relation. This is well defined for any two relations: if either
    # relation contains excess columns, the other relation is presumed
    # to contain tuples with every possible value in those columns.
    # Of course, the excess columns may be projected away before or
    # after the join. Most commonly, the columns of $source2 will
    # all appear in $source1.
    #
................................................................................
	lappend destcolumns {*}[dict get $m_relcolumns $source2]
	set destcolumns [lsort -dictionary -unique $destcolumns]
	my ColumnsMustBe $dest $destcolumns

	# Make code to do the join
	return [list [namespace which sys] & $dest $source1 $source2]
    }

    # Method: join+project
    #
    #	Generates code to join a pair of relations and project down to
    #	specific columns
    #
    # Usage:
    #	$db join+project dest source1 source2
    #
    # Results:
    #	Returns a fragment of code that will perform the join.
    #
    # The result of the join is the set of rows that are
    # members of the $source relation and members of the $dest
    # relation. This is well defined for any two relations: if either
    # relation contains excess columns, the other relation is presumed
    # to contain tuples with every possible value in those columns.
    # Of course, the excess columns may be projected away before or
    # after the join. Most commonly, the columns of $source2 will
    # all appear in $source1.
    #
    # The columns of the result relation must be the union of the
    # columns of the two inputs.
    #
    # This method does not perform the copy directly: it generates
    # code to perform the copy.
    #
    # The join executes in time proportional to the size of the
    # source1 BDD.

    method join+project {dest source1 source2} {

	my relationMustExist $dest
	my relationMustExist $source1
	my relationMustExist $source2

	# Calculate the union of the columns in the relations being joined.
	set sourcecolumns [dict get $m_relcolumns $source1]
	lappend sourcecolumns {*}[dict get $m_relcolumns $source2]
	set sourcecolumns [lsort -dictionary -unique $sourcecolumns]

	# Partition the source columns into those to be kept and those
	# to be projected away
	set discards {}
	set want {}
	foreach col [dict get $m_relcolumns $dest] {
	    dict set want $col {}
	}
	foreach col $sourcecolumns {
	    if {![dict exists $want $col]} {
		lappend discards {*}[dict get $m_columns $col]
	    } else {
		dict unset want $col
	    }
	}
	if {[dict size $want] != 0} {
	    return -code error \
		-errorcode [list FDDD ProjectColumnsMissing \
				[dict keys $want]]\
		"column(s) missing from source relation: [dict keys $want]"
	}

	return [list [namespace which sys] project_& \
		    $dest [lsort -integer $discards] $source1 $source2]
    }

    # Method: loader
    #
    #	Generates code to call the BDD engine to construct a term 
    #   corresponding to a tuple in a finite domain.
    #
    # Usage:
................................................................................
    # source BDD.

    method project {dest source} {
	# columns to project away are determined by dest and source
	my relationMustExist $dest
	my relationMustExist $source
	set discards {}
	set want {}
	foreach col [dict get $m_relcolumns $dest] {
	    dict set want $col {}
	}
	foreach col [dict get $m_relcolumns $source] {
	    if {![dict exists $want $col]} {
		lappend discards {*}[dict get $m_columns $col]
	    } else {

Changes to tests/fddd.test.

328
329
330
331
332
333
334

335
336
337
338
339
340
341
...
396
397
398
399
400
401
402














































































































403
404
405
406
407
408
409
...
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
...
729
730
731
732
733
734
735



















































































736
737
738
739
740
741
742
             [bdd::fddd::domain bit 1] \
             [bdd::fddd::interleave \
                  [bdd::fddd::domain parent 3 bigendian] \
                  [bdd::fddd::domain child 3 bigendian] \
                  [bdd::fddd::domain xyz 3 bigendian]]]
    ::db relation isParentOf parent child
    ::db relation result parent child

    ::db relation troll parent
    ::db relation temp1 parent xyz
    ::db relation temp2 xyz child
    ::db relation temp3 parent xyz child
    ::db relation everything bit parent child xyz
    interp alias {} loadIsParentOf {} {*}[::db loader isParentOf]
    interp alias {} loadTroll {} {*}[::db loader troll]
................................................................................
        string map [list \t {        }] $r
    }
    -result {
        parent 1 child 2
        parent 1 child 3
        parent 3 child 6
        parent 3 child 7














































































































    }
    -cleanup {db destroy}
}

# fddd-7 - columns

test fddd-7.1 {columns} {
................................................................................
test fddd-10.1 {forget - do nothing gracefully} {*}{
    -setup makeDB
    -body {
        db forget_relation rubbish
        db relations
    }
    -cleanup {db destroy}
    -result {everything isParentOf result temp1 temp2 temp3 troll}
}

test fddd-10.2 {forget - forget things} {*}{
    -setup makeDB
    -body {
        db forget_relation everything temp1 temp2 temp3 troll 
        db relations
    }
    -cleanup {db destroy}
    -result {isParentOf result}
}

# fddd-11 - inequality
................................................................................
        string map [list \t {        }] $r
    }
    -result {
        parent 0 child 0
        parent 0 child 1
        parent 2 child 4
        parent 2 child 5



















































































    }
    -cleanup {db destroy}
}

# fddd-13 - negate

test fddd-13.1 {negate} {*}{







>







 







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







 







|





|







 







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







328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
...
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
...
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
...
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
             [bdd::fddd::domain bit 1] \
             [bdd::fddd::interleave \
                  [bdd::fddd::domain parent 3 bigendian] \
                  [bdd::fddd::domain child 3 bigendian] \
                  [bdd::fddd::domain xyz 3 bigendian]]]
    ::db relation isParentOf parent child
    ::db relation result parent child
    ::db relation hasTrolledParent child
    ::db relation troll parent
    ::db relation temp1 parent xyz
    ::db relation temp2 xyz child
    ::db relation temp3 parent xyz child
    ::db relation everything bit parent child xyz
    interp alias {} loadIsParentOf {} {*}[::db loader isParentOf]
    interp alias {} loadTroll {} {*}[::db loader troll]
................................................................................
        string map [list \t {        }] $r
    }
    -result {
        parent 1 child 2
        parent 1 child 3
        parent 3 child 6
        parent 3 child 7
    }
    -cleanup {db destroy}
}

# fddd-6a - antijoin+project

test fddd-6a.1 {antijoin+project - relation doesn't exist} {
    -setup makeDB
    -body {
        list [catch {
            db antijoin+project rubbish isParentOf troll
        } result] $result $::errorCode
    }
    -cleanup {db destroy}
    -result {1 {relation "rubbish" is not defined in this database} {FDDD RelationNotDefined rubbish}}
}

test fddd-6a.2 {antijoin+project - relation doesn't exist} {
    -setup makeDB
    -body {
        list [catch {
            db antijoin+project result rubbish troll
        } result] $result $::errorCode
    }
    -cleanup {db destroy}
    -result {1 {relation "rubbish" is not defined in this database} {FDDD RelationNotDefined rubbish}}
}

test fddd-6a.3 {antijoin+project - relation doesn't exist} {
    -setup makeDB
    -body {
        list [catch {
            db antijoin+project result isParentOf rubbish
        } result] $result $::errorCode
    }
    -cleanup {db destroy}
    -result {1 {relation "rubbish" is not defined in this database} {FDDD RelationNotDefined rubbish}}
}

test fddd-6a.4 {antijoin+project} {*}{
    -setup {
        makeDB
        foreach i {0 2} {
            loadTroll $i
        }
    }
    -body {
        eval [db antijoin+project result isParentOf troll]
        set r \n
        ::db enumerate d result {
            append r {        } $d \n
        }
        append r {    }
        string map [list \t {        }] $r
    }
    -result {
        parent 1 child 2
        parent 1 child 3
        parent 3 child 6
        parent 3 child 7
    }
    -cleanup {db destroy}
}

test fddd-6a.4 {antijoin+project} {*}{
    -setup {
        makeDB
        foreach i {0 2} {
            loadTroll $i
        }
    }
    -body {
        eval [db antijoin+project result isParentOf troll]
        set r \n
        ::db enumerate d result {
            append r {        } $d \n
        }
        append r {    }
        string map [list \t {        }] $r
    }
    -result {
        parent 1 child 2
        parent 1 child 3
        parent 3 child 6
        parent 3 child 7
    }
    -cleanup {db destroy}
}

test fddd-6a.5 {antijoin+project} {*}{
    -setup {
        makeDB
        foreach i {0 2} {
            loadTroll $i
        }
    }
    -body {
        eval [db antijoin+project hasTrolledParent isParentOf troll]
        set r \n
        ::db enumerate d hasTrolledParent {
            append r {        } $d \n
        }
        append r {    }
        string map [list \t {        }] $r
    }
    -result {
        child 2
        child 6
        child 3
        child 7
    }
    -cleanup {db destroy}
}

# fddd-7 - columns

test fddd-7.1 {columns} {
................................................................................
test fddd-10.1 {forget - do nothing gracefully} {*}{
    -setup makeDB
    -body {
        db forget_relation rubbish
        db relations
    }
    -cleanup {db destroy}
    -result {everything hasTrolledParent isParentOf result temp1 temp2 temp3 troll}
}

test fddd-10.2 {forget - forget things} {*}{
    -setup makeDB
    -body {
        db forget_relation everything hasTrolledParent temp1 temp2 temp3 troll 
        db relations
    }
    -cleanup {db destroy}
    -result {isParentOf result}
}

# fddd-11 - inequality
................................................................................
        string map [list \t {        }] $r
    }
    -result {
        parent 0 child 0
        parent 0 child 1
        parent 2 child 4
        parent 2 child 5
    }
    -cleanup {db destroy}
}

test fddd-12a.1 {join - relation doesn't exist} {
    -setup makeDB
    -body {
        list [catch {
            db join+project rubbish isParentOf troll
        } result] $result $::errorCode
    }
    -cleanup {db destroy}
    -result {1 {relation "rubbish" is not defined in this database} {FDDD RelationNotDefined rubbish}}
}

test fddd-12.2 {join+project - relation doesn't exist} {
    -setup makeDB
    -body {
        list [catch {
            db join result rubbish troll
        } result] $result $::errorCode
    }
    -cleanup {db destroy}
    -result {1 {relation "rubbish" is not defined in this database} {FDDD RelationNotDefined rubbish}}
}

test fddd-12.3 {join+project - relation doesn't exist} {
    -setup makeDB
    -body {
        list [catch {
            db join result isParentOf rubbish
        } result] $result $::errorCode
    }
    -cleanup {db destroy}
    -result {1 {relation "rubbish" is not defined in this database} {FDDD RelationNotDefined rubbish}}
}

test fddd-12.4 {join+project} {*}{
    -setup {
        makeDB
        foreach i {0 2} {
            loadTroll $i
        }
    }
    -body {
        eval [db join result isParentOf troll]
        set r \n
        ::db enumerate d result {
            append r {        } $d \n
        }
        append r {    }
        string map [list \t {        }] $r
    }
    -result {
        parent 0 child 0
        parent 0 child 1
        parent 2 child 4
        parent 2 child 5
    }
    -cleanup {db destroy}
}

test fddd-12.5 {join+project} {*}{
    -setup {
        makeDB
        foreach i {0 2} {
            loadTroll $i
        }
    }
    -body {
        eval [db join+project hasTrolledParent isParentOf troll]
        set r \n
        ::db enumerate d hasTrolledParent {
            append r {        } $d \n
        }
        append r {    }
        string map [list \t {        }] $r
    }
    -result {
        child 0
        child 4
        child 1
        child 5
    }
    -cleanup {db destroy}
}

# fddd-13 - negate

test fddd-13.1 {negate} {*}{