2

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).

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Diedre
  • 515
  • 4
  • 21

1 Answers1

2

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
CapelliC
  • 59,646
  • 5
  • 47
  • 90
  • youre saying that i should start with general rules first, then codify a instance of the problem right? i think that "vocabulary" doesnt needs to be inserted in the .pro file, and i should start with general rules. But how i implement lines ta use the "="? Like, gate(x1) , type(x1) = xor. (x1 is a gate and x1 is a xor) – Diedre Jan 18 '14 at 21:56
  • In Prolog, use pattern matching: `?- gate(Name), type(Name, Type).` will provide in Type (note - uppercase symbols are **variables**) the Type of Name. That is, you **already** assigned the type of `x1` to be `xor` in the Prolog fragment you show. – CapelliC Jan 18 '14 at 22:14