An Example of Coding RDR's in iProlog

Describing the problem

This example is about building a set of rules to determine if someone should wear contact lens and if so, if the lens should be hard or soft.

The most convenient way of using RDR's in iProlog is to use them in conjunction with frames. So the first thing we do is define a generic frame to describe the type of object we are dealing with.

patient ako object with
        age:
                range
                        new value in [young, pre_presbyopic, presbyopic];
        prescription:
                range
                        new value in [myope, hypermetrope];
        astigmatism:
                range
                        new value in [astigmatic, not_astigmatic];
        tear_production:
                range
                        new value in [reduced, normal];
        lens:
                range
                        new value in [none, soft, hard]
                if_needed
                        if true then none because case0!

It is not important to know what presbyopic, etc mean. They are just the names and optometrist would use describe a person's age, and other attributes.

Given the values of the other attributes, we want to build an RDR that will return the correct value for lens, that is, what is the appropriate type of contact lens for this person. The RDR routines will construct a rule that will appear as an if_needed dæmon in the lens slot of the generic frame patient. We initialise the dæmon by inserting a dummy rule:

where case0 is a dummy case, as described below.


Example Cases

Here is a list of all the cases that will be entered into the RDR system.


An RDR Session

We write a simple predicate to manage a session for entering a sequence of new cases.

When called as:

This will backtrack through all patient cases, except the dummy case0, calling rdr on the lens slot. This call will evaluate lens of X and ask the user if the result is correct. If it is, the program will move to the next case. If it is not, rdr will ask the user for the correct result and then, the system looks at the differences between the corner stone case, ie. the case which caused the creation of the rule that fired and the new case. The system asks if each of the differences is relevant to distinguishing the cases.

Each yes answer causes a condition to be added to a new rule. The new RDR is printed out when all the differences have been checked.

iProlog (20 October 1997)
: go?
New case: case1 isa [patient] with
        age: young;
        prescription: myope;
        astigmatism: not_astigmatic;
        tear_production: reduced!
Old case: case0 isa [patient]!
Is none the correct conclusion? y

New case: case2 isa [patient] with
        age: young;
        prescription: myope;
        astigmatism: not_astigmatic;
        tear_production: normal!
Old case: case0 isa [patient]!
Is none the correct conclusion? n
What is the correct conclusion? soft
Is age of this object = young correct? n
Is prescription of this object = myope correct? n
Is astigmatism of this object = not_astigmatic correct? y
Is tear_production of this object = normal correct? y

        if true then none because case0 except
                if astigmatism of this object = not_astigmatic and tear_production of this object = normal then soft because case2

New case: case3 isa [patient] with
        age: young;
        prescription: myope;
        astigmatism: astigmatic;
        tear_production: reduced!
Old case: case0 isa [patient]!
Is none the correct conclusion? y

New case: case4 isa [patient] with
        age: young;
        prescription: myope;
        astigmatism: astigmatic;
        tear_production: normal!
Old case: case0 isa [patient]!
Is none the correct conclusion? n
What is the correct conclusion? hard
Is age of this object = young correct? n
Is prescription of this object = myope correct? y
Is astigmatism of this object = astigmatic correct? n
Is tear_production of this object = normal correct? y

        if true then none because case0 except
                if astigmatism of this object = not_astigmatic and tear_production of this object = normal then soft because case2
                else if prescription of this object = myope and tear_production of this object = normal then hard because case4

New case: case5 isa [patient] with
        age: young;
        prescription: hypermetrope;
        astigmatism: not_astigmatic;
        tear_production: reduced!
Old case: case0 isa [patient]!
Is none the correct conclusion? y

New case: case6 isa [patient] with
        age: young;
        prescription: hypermetrope;
        astigmatism: not_astigmatic;
        tear_production: normal!
Old case: case2 isa [patient] with
        age: young;
        prescription: myope;
        astigmatism: not_astigmatic;
        tear_production: normal!
Is soft the correct conclusion? y

New case: case7 isa [patient] with
        age: young;
        prescription: hypermetrope;
        astigmatism: astigmatic;
        tear_production: reduced!
Old case: case0 isa [patient]!
Is none the correct conclusion? y

New case: case8 isa [patient] with
        age: young;
        prescription: hypermetrope;
        astigmatism: astigmatic;
        tear_production: normal!
Old case: case0 isa [patient]!
Is none the correct conclusion? n
What is the correct conclusion? hard
Is age of this object = young correct? y
Is prescription of this object = hypermetrope correct? n
Is astigmatism of this object = astigmatic correct? n
Is tear_production of this object = normal correct? y

        if true then none because case0 except
                if astigmatism of this object = not_astigmatic and tear_production of this object = normal then soft because case2 except
                else if prescription of this object = myope and tear_production of this object = normal then hard because case4
                else if age of this object = young and tear_production of this object = normal then hard because case8

New case: case9 isa [patient] with
        age: pre_presbyopic;
        prescription: myope;
        astigmatism: not_astigmatic;
        tear_production: reduced!
Old case: case0 isa [patient]!
Is none the correct conclusion? y

New case: case10 isa [patient] with
        age: pre_presbyopic;
        prescription: myope;
        astigmatism: not_astigmatic;
        tear_production: normal!
Old case: case2 isa [patient] with
        age: young;
        prescription: myope;
        astigmatism: not_astigmatic;
        tear_production: normal!
Is soft the correct conclusion? y

New case: case11 isa [patient] with
        age: pre_presbyopic;
        prescription: myope;
        astigmatism: astigmatic;
        tear_production: reduced!
Old case: case0 isa [patient]!
Is none the correct conclusion? y

New case: case12 isa [patient] with
        age: pre_presbyopic;
        prescription: myope;
        astigmatism: astigmatic;
        tear_production: normal!
Old case: case4 isa [patient] with
        age: young;
        prescription: myope;
        astigmatism: astigmatic;
        tear_production: normal!
Is hard the correct conclusion? y

New case: case13 isa [patient] with
        age: pre_presbyopic;
        prescription: hypermetrope;
        astigmatism: not_astigmatic;
        tear_production: reduced!
Old case: case0 isa [patient]!
Is none the correct conclusion? y

New case: case14 isa [patient] with
        age: pre_presbyopic;
        prescription: hypermetrope;
        astigmatism: not_astigmatic;
        tear_production: normal!
Old case: case2 isa [patient] with
        age: young;
        prescription: myope;
        astigmatism: not_astigmatic;
        tear_production: normal!
Is soft the correct conclusion? y

New case: case15 isa [patient] with
        age: pre_presbyopic;
        prescription: hypermetrope;
        astigmatism: astigmatic;
        tear_production: reduced!
Old case: case0 isa [patient]!
Is none the correct conclusion? y

New case: case16 isa [patient] with
        age: pre_presbyopic;
        prescription: hypermetrope;
        astigmatism: astigmatic;
        tear_production: normal!
Old case: case0 isa [patient]!
Is none the correct conclusion? y

New case: case17 isa [patient] with
        age: presbyopic;
        prescription: myope;
        astigmatism: not_astigmatic;
        tear_production: reduced!
Old case: case0 isa [patient]!
Is none the correct conclusion? y

New case: case18 isa [patient] with
        age: presbyopic;
        prescription: myope;
        astigmatism: not_astigmatic;
        tear_production: normal!
Old case: case2 isa [patient] with
        age: young;
        prescription: myope;
        astigmatism: not_astigmatic;
        tear_production: normal!
Is soft the correct conclusion? n
What is the correct conclusion? none
Is age of this object = presbyopic correct? y

        if true then none because case0 except
                if astigmatism of this object = not_astigmatic and tear_production of this object = normal then soft because case2 except
                        if age of this object = presbyopic then none because case18
                else if prescription of this object = myope and tear_production of this object = normal then hard because case4
                else if age of this object = young and tear_production of this object = normal then hard because case8

New case: case19 isa [patient] with
        age: presbyopic;
        prescription: myope;
        astigmatism: astigmatic;
        tear_production: reduced!
Old case: case0 isa [patient]!
Is none the correct conclusion? y

New case: case20 isa [patient] with
        age: presbyopic;
        prescription: myope;
        astigmatism: astigmatic;
        tear_production: normal!
Old case: case4 isa [patient] with
        age: young;
        prescription: myope;
        astigmatism: astigmatic;
        tear_production: normal!
Is hard the correct conclusion? y

New case: case21 isa [patient] with
        age: presbyopic;
        prescription: hypermetrope;
        astigmatism: not_astigmatic;
        tear_production: reduced!
Old case: case0 isa [patient]!
Is none the correct conclusion? y

New case: case22 isa [patient] with
        age: presbyopic;
        prescription: hypermetrope;
        astigmatism: not_astigmatic;
        tear_production: normal!
Old case: case18 isa [patient] with
        age: presbyopic;
        prescription: myope;
        astigmatism: not_astigmatic;
        tear_production: normal!
Is none the correct conclusion? n
What is the correct conclusion? soft
Is prescription of this object = hypermetrope correct? n
Is prescription of this object \= myope correct? y

        if true then none because case0 except
                if astigmatism of this object = not_astigmatic and tear_production of this object = normal then soft because case2 except
                        if age of this object = presbyopic then none because case18 except
                                if prescription of this object \= myope then soft because case22
                else if prescription of this object = myope and tear_production of this object = normal then hard because case4
                else if age of this object = young and tear_production of this object = normal then hard because case8

New case: case23 isa [patient] with
        age: presbyopic;
        prescription: hypermetrope;
        astigmatism: astigmatic;
        tear_production: reduced!
Old case: case0 isa [patient]!
Is none the correct conclusion? y

New case: case24 isa [patient] with
        age: presbyopic;
        prescription: hypermetrope;
        astigmatism: astigmatic;
        tear_production: normal!
Old case: case0 isa [patient]!
Is none the correct conclusion? y

** yes