Improving the pseudocode structure
In line 7 it appears that you restart every time you find a new element to add to D. However, in pseudocode and mathematical language, you have no guarantee about the order in which foreach
takes the elements of I.
Therefore, you could as well continue the loop to the end of the foreach
and add an outer loop:
D ← { O }
repeat
retry ← false
foreach ∈ I do
P ← getPremises()
if P ∈ D then
C ← getConclusion()
if C ∉ D then
D ← D ∪ { C }
retry ← true
until not retry
return D
If you do not like repeat
/until
, you could also go for a while
but you'd need to initalize retry
in consequence.
If the order of evaluation matters, you'd replace the set I with an ordered list or a priority queue, and use the same approach than before but break the inner loop after setting retry
to true.
Correcting the notation
Instead of !
, you'd better use the more readable not
, or the more comprehensive ∉
.
This being said, if O is a set, and if D is a set that should at start contain all the elements of O, O should not be shown as a set element, and line 1 should be:
D ← O
If P is in fact a set and not elements, line 5 should use set operators instead of element operators:
if P ⊂ D then
...
Improving algorithm
The restart in your original algorithm after the firing of a rule leads to think that the intent is to re-evaluate all the rules due to changed of situation.
However from the comments, it appears that a rule may have an effect on D only once. So there's no need to fire again a rule that already fired. This may lead to an improved algorithm:
D ← O
K ← I // K is the set of rules that could still fire
n ← true // n is true when new facts were added to D
while n do
n ← false
foreach ∈ K do
P ← getPremises()
if P ⊂ D then
K ← K - { } // or K ← {x∈K|x≠ }
C ← getConclusion()
if C ∉ D then
D ← D ∪ { C }
n ← true
return D
Now K contains the rules which did not fire yet.
Depending on the number of rules and the complexity of firing them, you could even go further and remove from K any other rule that could give the same C as conclusion, whether they are ready to be fired or not, since they would never change the final D.