I am applying the Logic De Morgan rule to all nodes of a set. Whenever I assert/retract the nodes matching the specified format, the query seems to stop there and return no. While this doesn't cause problems for applying the De Morgan rule once, it cause problems when I try to apply it multiple times as it fails the query immediately after. I've done a decent amount of testing so I am fairly sure where the problem is stemming but feel free to recommended something I probably missed.---------- De Morgan Rule: -(A V B) -> -A & -B____ Displays as not(or(A,B) -> and(not(A),not(B)). new_node_name just produces new tags for the adjusted nodes.
%These are the preset nodes
:- dynamic(node/4).
node(n1, 'and', n3, n2).
node(n2, 'not', n6, void).
node(n3, 'not', n4, void).
node(n4, 'or', n5, n9).
node(n5, 't', void, void).
node(n9, 'u', void, void).
%node(n3, 'or', n4, n5).
%node(n4, 'r', void, void).
%node(n5, 'or', n9, n10).
node(n6, 'or', n7, n8).
node(n7, 'p', void, void).
node(n8, 's', void, void).
printTree(X) :-
node(X, Value, Left, Right),
format('~w', [Value]),
( Value = 'and' -> write('(') ; true),
( Value = 'or' -> write('(') ; true),
( Value = 'not' -> write('(') ; true),
printTree(Left),
( Value = 'and' -> write(',') ; true),
( Value = 'or' -> write(',') ; true),
printTree(Right),
( Value = 'and' -> write(')') ; true),
( Value = 'not' -> write(')') ; true),
( Value = 'or' -> write(')') ; true).
printTree(void).
deMorgan(X) :-
node(X, 'not', Y, void),
node(Y, Value, Left, Right),
node(Left, Value1, void, void),
node(Right, Value2, void, void),
new_node_name(B, 2),
new_node_name(C, 3), %simply generating tags for the asserted nodes
new_node_name(D, 4),
new_node_name(E, 5),
asserta(node(X, 'and', B, C)),
asserta(node(B, 'not', D, void)),
asserta(node(C, 'not', E, void)),
asserta(node(D, Value1, void, void)),
asserta(node(E, Value2, void, void)),
retract(node(X, 'not', Y, void)),
retract(node(Y, Value, Left, Right)),
retract(node(Left, Value1, void, void)),
retract(node(Right, Value2, void, void)).
% seems to return 'no' after I retract, no matter where I place it.
new_node_name(X, Y):-
count_nodes(n1, N),
Ne is N + Y,
number_atom(Ne,NewAtom),
atom_concat(n, NewAtom, X).
count_nodes(X,N):-
node(X, Value, L, R),
count_nodes(L,NL),
count_nodes(R,NR),
N is NL + NR + 1.
count_nodes(void,0).
to run: 1. printTree(n1).
2. deMorgan(n2). or deMorgan(n3).
3. printTree(n1).
output: no
expected output: yes
Corresponding branch should adjust but the query will return no nonetheless