I have a question that is on the topic of this one (Create Prolog Vocabulary), but uses a slightly different vocabulary.
I've seen the answer, and, even though I know it's correct, I don't want to describe the circuit that way. I want to be able to determine terminals and signals.
The main difference between the vocabularies used is that mine uses
signal(in(inPortNumber, portName), signalValue)
With that in mind, I have a few questions:
SOLVED 1 - How do I write "If C is a circuit, let its arity be I, J (I = numInPorts, J = numOutPorts). For all possible values of N (0 < N < I), the in port number N of C is a terminal"?
This is what I have, but it's not working (infinite loop):
% Check arity of IN wires
% If more in wires than gate can support, it's an error
terminal(in(N, C)) :- circuit(C), arity(C, I, _J), N < I, N > 0.
EDIT 2 - How do I write "If terminals T1 and T2 are connected, and T2 has been assigned a signal, T1 is also assigned that signal value"?
This is what I have:
% FACTS
circuit('c1').
arity('c1', 3, 2).
gate('x1').
type('x1', xorGate).
are_connected(in(1, 'c1'), in(1, 'x1')).
are_connected(in(2, 'c1'), in(2, 'x1')).
signal(in(1, 'c1'), 1).
signal(in(2, 'c1'), 1).
signal(in(3, 'c1'), 1).
% RULES
% All gates are circuits
circuit(G) :- gate(G).
% Check arity of IN wires
terminal(in(N, G)) :- circuit(G), arity(G, I, _J), N =< I, N > 0.
% Check arity of OUT wires
terminal(out(N, G)) :- circuit(G), arity(G, _I, J), N =< J, N > 0.
% Signals do only exist in terminals
terminal(T) :- signal(T, _V).
% Arity
arity(G, 1, 1) :- gate(G), type(G, notGate). % NOT gate
arity(G, 2, 1) :- gate(G), type(G, V), not(V = notGate). % Other gates
% Commutativity
connected(T1, T2) :- are_connected(T1, T2), !.
connected(T2, T1) :- are_connected(T1, T2), !.
% Connectivity
same_signal(T1, T2) :- terminal(T1), terminal(T2), not(T1 = T2), connected(T1, T2).
signal(T1, V) :- same_signal(T1, T2), signal(T2, V), !.
signal(T2, V) :- same_signal(T1, T2), signal(T1, V), !.
The problem is that, when asking:
signal(in(1, x1), V).
It throws an error, as things aren't sufficiently instantiated. I know where and what the problem is, but I don't know how to solve it.
Looking forward to answers/suggestions. I'm new to Prolog, so all tips are welcome (but yes, I do know I should put clauses of the same predicate together).