Domanda

I am quite new to prolog, and I have some basic questions...

I don't know if "vocabulary" is the right world in English, but I need to create one to describe an electronic circuit.

My problem is, how do I create these functions and how I use the "=" statement since prolog doesn't seems do accept it?

I'm using SWI Prolog.

That's what I have to put in prolog:

Decide the vocabulary (Predicates, functions, constants):

Ports are represented by constants (X1, X2, ...) –

Gate(X1)

Type(X1) = Xor – types: AND, OR, XOR or NOT

Circuits(C1)

Terminals(x) – returns inputs and outputs of x

In(1, X1) – function that returns first input of X1

Out – function that returns output

Arity(c, i, j) – function, circuit c has i inputs and j outputs

Connected(Out(1, X1), In(1, X2)) - which ports are connected

Signal(t) – signal value for terminal t.

That's what I tried until now. I don't think my approach to the "=" is right...

gate(x1).
gate(x2).
gate(a1).
gate(a2).
gate(o1).
type(x1, xor).
type(x2, xor).
type(a1, and).
type(a2, and).
type(o1, or).
circuit(c1).

Should i use an predicate named Equal(X, Y) ?, like "equal (type(x1), xor).

How should I implement these?

Gate(X1) , Type(X1) = XOR
Gate(X2) , Type(X2) = XOR
Gate(A1) , Type(A1) = AND
Gate(A2) , Type(A2) = AND
Gate(O1) , Type(O1) = OR 

I don't know how to continue from here. All my approaches trying to implement the functions seems to be wrong (can't consult).

È stato utile?

Soluzione

You should read this document to get some inspiration :)

For instance, basic functions (i.e. gates) can be described like

and(0, 0, 0).
and(0, 1, 0).
and(1, 0, 0).
and(1, 1, 1).

xor(0, 0, 0).
...

and then combined to get more complex building blocks

fulladder(A, B, Carryin, Sum, Carryout):-
 xor(A, B, X),
 and(A, B, Y),
 and(X, Carryin, Z),
 xor(Carryin, X, Sum),
 or(Y, Z, Carryout).

that compute the logic function:

?- fulladder(X, Y, Z, 0, 1).
X = 0, Y = 1, Z = 1 ? ;
X = 1, Y = 0, Z = 1 ? ;
X = 1, Y = 1, Z = 0 ? ;
no
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top