Prerequisite -
Prolog | An Introduction
Overview :
Digital circuits or digital electronics is a branch of electronics which deals with digital signals to perform the various task to meet various requirement. The input signal applied to these circuits is of digital form, which is represented in 0’s and 1’s binary language format. These circuits are designed by using logical gates like AND, OR, NOT, NANAD, NOR, XOR gates which perform logical operations. This representation helps the circuit to switch from one state to another for providing precise output. Digital circuit systems are mainly designed to overcome the disadvantage of analog systems which are slower and the output data which is obtained may contain an error. There are two types of digital circuits Combinational Circuit and Sequential Circuit.
Implementation using Prolog :
Prolog is a logical and declarative programming language. The name itself, Prolog, is short for Programming in Logic.
Example :
Here the question arises, how do we depict a circuit in a prolog code. Consider the following circuit diagram.

There are 3 inputs in the circuit diagram, A, B, and C. We will consider and pass the above circuit diagram as a prolog code in the following line of code as follows.
circuit1(A,B,C,Q,R):-
not(A,T1),
and(A,B,T2),
nand(T1,T2,T3),
xnor(T3,C,T4),
dff(T4,Q,R).
Description :
Here Q and R are the output variables. T1- T4 are the instance values or outputs of the in-between gates. The logic gates which has been used are:- NOT, AND, NAND, and XNOR gate. There is one D-FLIP FLOP and 2 outputs Q and Q'. We will consider the truth tables as facts and the circuit diagram as rules in prolog, As written in the following code.
Note -
In the following code, the truth tables are written for g-prolog. In swi-prolog, the truth tables are predefined.
Code implementation :
Here, we will implement the logic and will write the code in prolog.
Step-1 :
Here, we will implement the truth tables of all logic gates for 2 inputs A and B.
% Made by - Maninder kaur
% Following are the truth tables of all logic gates for 2 inputs A and B.
and(0,0,0).
and(0,1,0).
and(1,0,0).
and(1,1,1).
or(0,0,0).
or(0,1,1).
or(1,0,1).
or(1,1,1).
not(0,1).
not(1,0).
nand(0,0,1).
nand(0,1,1).
nand(1,0,1).
nand(1,1,0).
nor(0,0,1).
nor(0,1,0).
nor(1,0,0).
nor(1,1,0).
xor(0,0,0).
xor(0,1,1).
xor(1,0,1).
xor(1,1,0).
xnor(0,0,1).
xnor(0,1,0).
xnor(1,0,0).
xnor(1,1,1).
Step-2 :
Here, we will implement the truth tables of OR GATE for 4 inputs A, B, C and D.
% Following is the truth tables of OR GATE for 4 inputs A ,B ,C and D.
% (Used in 8X3 Encoder)
or4(0,0,0,0,0).
or4(0,0,0,1,1).
or4(0,0,1,0,1).
or4(0,0,1,1,1).
or4(0,1,0,0,1).
or4(0,1,0,1,1).
or4(0,1,1,0,1).
or4(0,1,1,1,1).
or4(1,0,0,0,1).
or4(1,0,0,1,1).
or4(1,0,1,0,1).
or4(1,0,1,1,1).
or4(1,1,0,0,1).
or4(1,1,0,1,1).
or4(1,1,1,0,1).
or4(1,1,1,1,1).
Step-3 :
Here, we will implement the Half adder.
% HALF ADDER :-
% INPUT VARIABLES - A,B,C
% OUTPUT VARIABLES - S ,Ca(Sum and Carry)
half_adder(A,B,S,Ca):-
xor(A,B,S),
and(A,B,Ca).
Step-4 :
Here, we will implement the Full adder.
%FULL ADDER :-
%INPUT VARIABLES - A,B,C
%OUTPUT VARIABLES - S ,Ca (Sum and Carry)
full_adder(A,B,C,S,Ca):-
xor(A,B,T1),
xor(C,T1,S),
and(T1,C,T2),
and(A,B,T3),
or(T3,T2,Ca).
Step-5 :
Here, we will implement the Half Subtractor.
% HALF SUBTRACTOR :-
% INPUT VARIABLES - A,B
% OUTPUT VARIABLES - D ,BO (Difference and borrow)
half_sub(A,B,D,BO):-
xor(A,B,D),
not(A,T1),
and(B,T1,BO).
Step-6 :
Here, we will implement the full subtractor.
% FULL SUBTRACTOR :-
% INPUT VARIABLES - A,B
% OUTPUT VARIABLES - D ,BO (Difference and borrow)
full_sub(A,B,BI,D,BO) :-
xor(A,B,T1),
xor(T1,BI,D),
not(T1,T2),
not(A,T3),
nand(T2,BI,T4),
nand(T3,B,T5),
nand(T4,T5,BO).
Step-7 :
Now, we will implement the 2 × 4 DECODER.
% 2 X 4 DECODER
% INPUT VARIABLES - A,B
% OUTPUT VARIABLES - D0,D1,D2,D3
decoder_2x4(A,B,D0,D1,D2,D3):-
not(A,A_0),
not(B,B_0),
and(A_0,B_0,D0),
and(A_0,B,D1),
and(A,B_0,D2),
and(A,B,D3).
Step-8 :
Now, we will implement the 3 × 8 DECODER.
% 3 X 8 ENCODER
% OUTPUT VARIABLES - A,B
% INPUT VARIABLES - D0,D1,D2,D3,D4,D5,D6,D7
encoder_8x3(_,D1,D2,D3,D4,D5,D6,D7,A,B,C):-
or4(D1,D3,D5,D7,A),
or4(D2,D3,D6,D7,B),
or4(D4,D5,D6,D7,C).
Step-9 :
Now, we will implement the 2 X 1 MULTIPLEXER.
% 2 X 1 MULTIPLEXER
% INPUT VARIABLES - A,B,S (Selector)
% OUTPUT VARIABLES - Z
mux_2x1(A,B,S,Z):-
not(S,S1),
and(A,S1,I0),
and(B,S,I1),
or(I0,I1,Z).
Step-10 :
Now, we will implement the 1 × 2 DEMULTIPLEXER.
% 1 X 2 DEMULTIPLEXER
% INPUT VARIABLES - I (Input) ,S (Selector)
% OUTPUT VARIABLES - A,B
demux_1x2(I,S,A,B):-
not(S,S_0),
and(I,S_0,A),
and(I,S,B).
Step-11 :
Now, we will implement the 1 × 4 DEMULTIPLEXER.
% 1 X 4 DEMULTIPLEXER
% INPUT VARIABLES - I (Input) ,S0 and S1(Selectors)
% OUTPUT VARIABLES - A,B,C,D
demux_1x4(I,S0,S1,A,B,C,D):-
decoder_2x4(S0,S1,T0,T1,T2,T3),
and(I,T0,A),
and(I,T1,B),
and(I,T2,C),
and(I,T3,D).
Step-12 :
Now, we will implement the D FLIP FLOP.
% D FLIP FLOP TRUTH TABLE
dff(0,0,1).
dff(1,1,0).
Step-13 :
Now, we will implement the circuit code.
% CIRCUITS
circuit1(A,B,C,Q,R):-
not(A,T1),
and(A,B,T2),
nand(T1,T2,T3),
xnor(T3,C,T4),
dff(T4,Q,R).
Output :

Similar Reads
Combinational and Sequential Circuits Digital logic circuits are fundamental components in modern electronic devices, enabling operations in systems like computers, mobile phones and calculators by processing binary data through logic gates such as AND, OR and NOT. These circuits are essential for tasks like data storage, decision-makin
3 min read
Difference between Combinational and Sequential Circuit In digital electronics, circuits are classified into two primary categories: The combinational circuits and the sequential circuits. Where the outputs depend on the current inputs are called combination circuit, combinational circuits are simple and effective for functions like addition, subtraction
4 min read
Analysis and Design of Combinational and Sequential circuits Logic circuits can be combinational or sequential. A combinational circuit consists of logic gates whose outputs at any time are determined from only the present combination of inputs. The operation of combinational circuits can be specified logically by a set of Boolean functions. Sequential circui
3 min read
Classifications of Combinational and Sequential circuits 1. Classifications of Combinational Circuits: There are three main categories of combinational circuits: arithmetic or logical functions, data transmission and code converter as given below in category diagram. Functions of Combinational circuits are generally expressed by Boolean algebra, Truth tab
2 min read
Implementing Any Circuit Using NAND Gate Only A universal gate is such a gate that we can implement any Boolean function, no matter how complex, using a circuit that consists of only that particular universal gate. The NAND & NOR gates are the most commonly encountered universal gates in digital logic.In this article, we will take a look at
8 min read
Combinational circuits using Decoder Combinational circuits utilizing decoders are basic parts in a computerized plan, assuming a significant part in making an interpretation of parallel data into noteworthy results. Decoders are combinational rationale gadgets that convert twofold information signals into an extraordinary arrangement
8 min read
Difference between Characteristics of Combinational and Sequential circuits Combinational and sequential are important building blocks used in the many electronic devices. The main difference between them is how they work with the time. Combinational circuits dont care about the time they just react to what is happening right now. Sequential circuits on remember what happen
6 min read
Construction of Combinational Circuits Combinational circuits are digital circuits. In these circuits output is determined by the current input values. The current output is not dependent on any memory or feedback from previous results. There are various functions performed by Combinational Circuits. Example include logic operations, ari
5 min read
Synchronous Sequential Circuits in Digital Logic Synchronous sequential circuits are digital circuits that use clock signals to determine the timing of their operations. They are commonly used in digital systems to implement timers, counters, and memory elements.What is Sequential Circuit?A sequential circuit is a digital circuit, whose output dep
5 min read
Introduction of Sequential Circuits Sequential circuits are digital circuits that store and use the previous state information to determine their next state. Unlike combinational circuits, which only depend on the current input values to produce outputs, sequential circuits depend on both the current inputs and the previous state stor
7 min read