Create Prolog Vocabulary

扶醉桌前 提交于 2019-12-11 02:39:47

问题


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

I dont know if "vocabulary" is the right world in english, but i need to create one to describe an eletronic circuit.

My problem is, how do i create these functions and how i use the "=" statement since prolog doesnt seems do acept it.

Im using SWI Prolog.

(tried my best to translate to english)Thats 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)) - wich ports are conected

Signal(t) – signal value for terminal t.

Thtas what i tried until now. I dont 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 dont know how to continue from here. All my approachs trying to implement the functions seems to be wrong (cant consult).


回答1:


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


来源:https://stackoverflow.com/questions/21209862/create-prolog-vocabulary

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!