next up previous contents index
Next: Inference Class Up: An Example Previous: An Example

Another example

Consider the following example, which is used to compute Pythagorean triangles. Only one axiom and two rules are used. The axiom and the first rule are used to assert the relations num(1) to num(n) into the database, where n is limited by the term limit(n). The second inference rule is responsible for printing out only the appropriate combinations of numbers.

   datatype Number :: relation = num int | limit int;

   inference class Triangle {};

   inference Triangle
   {  ->  num 1;

          num m
      and limit n | n > m
      ->  num (m+1);

          num a
      and num b
      and num c | a < b && b < c && a*a + b*b == c*c
      ->  { cout << a << " * " << a << " + "
                 << b << " * " << b << " = "
                 << c << " * " << c << "\n";
          };
   };

Now, to print all the triangle identities lying in range of 1 to 100, we only have to create an instance of the inference class, insert the limit, and start the inference process, as in below:

   Triangle triangle;
   triangle += limit(100);
   triangle.infer();



Allen Leung
Mon Apr 7 14:33:55 EDT 1997