1 //////////////////////////////////////////////////////////////////////////////
2 // This is a simple self-contained test to benchmark the speed of
3 // naive inferencing in Prop.
8 // gcc -I<prop-include-dir> triangle.cc -o triangle -L<ADLib-library-dir>
9 // -lad -liostream -lg++
10 //////////////////////////////////////////////////////////////////////////////
14 //////////////////////////////////////////////////////////////////////////////
16 // Defines two relations to be used during inference
18 //////////////////////////////////////////////////////////////////////////////
19 datatype Number :: relation = num (int);
20 datatype Limit :: relation = limit (int);
21 instantiate datatype Number, Limit;
23 //////////////////////////////////////////////////////////////////////////////
25 // The following inference construct defines an inference class
26 // with two rules and one axiom.
28 //////////////////////////////////////////////////////////////////////////////
29 inference class Triangle {};
40 // Assert all numbers from 1 to n
43 and limit (n) where m < n
44 ---------------------------
47 ///////////////////////////////////////////////////////////////////////////
49 // Now try all combination of x, y, and z's and look for witnesses
50 // of the Pythagorean Theorem.
52 // Notice that we are in effect performing a three way
53 // cartesian product. Since Prop doesn't yet have indexing,
54 // all permutations have to be enumerated. So this is in effect
55 // a good benchmark for the basic (naive) inference speed of Prop.
56 // This is NOT a recommended use of inference, of course.
58 // Notice that the inferencing will go progressively slower as
59 // more and more facts are entered into the database. The time
60 // complexity is O(n^3), where n is the limit. Space complexity
61 // is O(n^2), since the RETE algorithm caches intermediate results
62 // from the first join in its beta memory.
65 and num (y) where x < y
66 and num (z) where y < z && x * x + y * y == z * z
67 -----------------------------------------------------
68 { cout << x << " * " << x << " + "
69 << y << " * " << y << " = "
70 << z << " * " << z << '\n';
76 Triangle triangle; // instantiate an inference module
79 ///////////////////////////////////////////////////////////////////////////
83 ///////////////////////////////////////////////////////////////////////////
84 cout << "Please input a limit (say, between 10 - 100): " << flush;
86 cout << "Trying all numbers from 1 to " << top << '\n';
88 ///////////////////////////////////////////////////////////////////////////
90 // Insert the initial parameter into the database.
92 ///////////////////////////////////////////////////////////////////////////
93 triangle << limit (top);
95 ///////////////////////////////////////////////////////////////////////////
97 // Run the inference engine until it is stable.
98 // The inference rules defined above will be fired and triangular
99 // identities will be printed.
101 ///////////////////////////////////////////////////////////////////////////