<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<title>My Blog</title>
	<subtitle>My blog site.</subtitle>
	<link rel="self" type="application/atom+xml" href="https://fazeneo.in/posts/feed.xml"/>
  <link rel="alternate" type="text/html" href="https://fazeneo.in/posts/"/>
  
	<updated>2024-12-01T00:00:00+00:00</updated>
	
	<id>https://fazeneo.in/posts/feed.xml</id>
	<entry xml:lang="en">
		<title>A Beginner’s guide to Writing Data Flip Flop in Verilog</title>
		<published>2024-12-01T00:00:00+00:00</published>
		<updated>2024-12-01T00:00:00+00:00</updated>
		<link rel="alternate" type="text/html" href="https://fazeneo.in/posts/a-beginners-guide-to-writing-data-flip-flop-in-verilog/"/>
		<id>https://fazeneo.in/posts/a-beginners-guide-to-writing-data-flip-flop-in-verilog/</id>
    
		<content type="html" xml:base="https://fazeneo.in/posts/a-beginners-guide-to-writing-data-flip-flop-in-verilog/">&lt;p&gt;In this blog post we will see a beginners guide on how to write DFF (Data Flip Flop) in Verilog. Verilog is a Hardware Description Language (HDL) which is used to describe the functionality of a hardware (electronic systems) in a programming language. There are many resources online if you want to learn more about Verilog.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;disclaimer&quot;&gt;DISCLAIMER&lt;a class=&quot;zola-anchor&quot; href=&quot;#disclaimer&quot; aria-label=&quot;Anchor link for: disclaimer&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h3&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;We will be only looking at the &lt;strong&gt;Behavior modelling&lt;&#x2F;strong&gt; and not &lt;strong&gt;Gate level modelling&lt;&#x2F;strong&gt;. Since &lt;em&gt;behavior modelling&lt;&#x2F;em&gt; is easier to understand and read.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;what-is-a-data-flip-flop&quot;&gt;What is a Data Flip Flop?&lt;a class=&quot;zola-anchor&quot; href=&quot;#what-is-a-data-flip-flop&quot; aria-label=&quot;Anchor link for: what-is-a-data-flip-flop&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h2&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;Flip-Flops are part of the family of sequential logic gates because flip-flops deals with &lt;em&gt;clock signals&lt;&#x2F;em&gt; (the heartbeat of sequential logic gates).&lt;&#x2F;p&gt;
&lt;p&gt;To put it simply, a data flip flop is a storage element to store a &lt;em&gt;single bit of data&lt;&#x2F;em&gt;. Since it can only store a single bit, its states will be either &lt;strong&gt;1&lt;&#x2F;strong&gt; or &lt;strong&gt;0&lt;&#x2F;strong&gt;. We will see how it stores the data in detail in later part of this post to understand more about it.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;mataroa.blog&#x2F;images&#x2F;7d6da2f5.png&quot; alt=&quot;dff-drawio&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Now that we have a decent understanding of what a data flip flop is and what it is used for, let’s look at the implementation.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;implementing-dff&quot;&gt;Implementing DFF&lt;a class=&quot;zola-anchor&quot; href=&quot;#implementing-dff&quot; aria-label=&quot;Anchor link for: implementing-dff&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h2&gt;
&lt;hr &#x2F;&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;verilog&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;module&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; DFF&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	input&lt;&#x2F;span&gt;&lt;span&gt; D, CLK,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	output&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; reg&lt;&#x2F;span&gt;&lt;span&gt; Q&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	always&lt;&#x2F;span&gt;&lt;span&gt; @(&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;posedge&lt;&#x2F;span&gt;&lt;span&gt; CLK) &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;begin&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;		Q &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;&amp;lt;=&lt;&#x2F;span&gt;&lt;span&gt; D;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	end&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;endmodule&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We have declared a &lt;em&gt;module&lt;&#x2F;em&gt; in Verilog named &lt;strong&gt;DFF&lt;&#x2F;strong&gt;, which stands for &lt;em&gt;Data Flip Flop&lt;&#x2F;em&gt;. This module takes in 3 things.&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;D&lt;&#x2F;strong&gt; - which stands for &lt;strong&gt;Data&lt;&#x2F;strong&gt; as an &lt;em&gt;input&lt;&#x2F;em&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;CLK&lt;&#x2F;strong&gt; - which stands for &lt;strong&gt;Clock Signal&lt;&#x2F;strong&gt; as an &lt;em&gt;input&lt;&#x2F;em&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Q&lt;&#x2F;strong&gt; - which is our &lt;em&gt;output&lt;&#x2F;em&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;In the body of the module, we have a &lt;em&gt;procedural block&lt;&#x2F;em&gt; called &lt;strong&gt;&lt;code&gt;always&lt;&#x2F;code&gt;&lt;&#x2F;strong&gt; block. In Verilog, &lt;strong&gt;&lt;code&gt;always&lt;&#x2F;code&gt;&lt;&#x2F;strong&gt; block is used to describe behaviour that occur continuously or repeatedly based on certain conditions. In our case, we have &lt;code&gt;posedge CLK&lt;&#x2F;code&gt;. &lt;strong&gt;posedge&lt;&#x2F;strong&gt; is short for &lt;em&gt;positive edge&lt;&#x2F;em&gt;, and &lt;strong&gt;posedge CLK&lt;&#x2F;strong&gt; means &lt;em&gt;positive edge of the clock&lt;&#x2F;em&gt;. Positive edge is nothing but, when the clock signal goes from 0 to 1. So whenever the value of &lt;code&gt;*CLK*&lt;&#x2F;code&gt; goes from 0 to 1, the &lt;code&gt;always&lt;&#x2F;code&gt; block gets executed.&lt;&#x2F;p&gt;
&lt;p&gt;Inside the always block we have, &lt;code&gt;Q &amp;lt;= D&lt;&#x2F;code&gt; which means we are assigning the value of &lt;strong&gt;D&lt;&#x2F;strong&gt; to &lt;strong&gt;Q&lt;&#x2F;strong&gt;. If suppose, &lt;strong&gt;D = 1&lt;&#x2F;strong&gt; then the value of &lt;strong&gt;Q&lt;&#x2F;strong&gt; becomes 1.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;&amp;lt;=&lt;&#x2F;code&gt; means &lt;em&gt;&lt;strong&gt;non-blocking assignment&lt;&#x2F;strong&gt;&lt;&#x2F;em&gt;. I’m not gonna go into the details of it. You can google what’s the difference between blocking and non-blocking assignment in Verilog.&lt;&#x2F;p&gt;
&lt;p&gt;We have successfully implemented a Data Flip Flop. Now let’s test it.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;testing-our-dff&quot;&gt;Testing our DFF&lt;a class=&quot;zola-anchor&quot; href=&quot;#testing-our-dff&quot; aria-label=&quot;Anchor link for: testing-our-dff&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h2&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;Now that we have implemented our DFF, we have to test it to make sure its behaving as expected.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Question:&lt;&#x2F;strong&gt; What’s the behavior that we want to test?&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Answer:&lt;&#x2F;strong&gt; At the positive edge of the clock, whatever value(usually binary) is passed as input (&lt;strong&gt;D&lt;&#x2F;strong&gt;) should be the output. During other times, the output will be the previous output.&lt;&#x2F;p&gt;
&lt;p&gt;Let’s write our &lt;em&gt;testbench:&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;verilog&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;module&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; dff_tb&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	reg&lt;&#x2F;span&gt;&lt;span&gt; D &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	reg&lt;&#x2F;span&gt;&lt;span&gt; CLK &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	wire&lt;&#x2F;span&gt;&lt;span&gt; Q;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-tag&quot;&gt;	DFF&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-tag&quot;&gt; dff&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;		.D(D),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;		.CLK(CLK),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;		.Q(Q)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	)&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	always&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; #5&lt;&#x2F;span&gt;&lt;span&gt; CLK &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;&amp;lt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; ~&lt;&#x2F;span&gt;&lt;span&gt;CLK;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	initial&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; begin&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;		#10&lt;&#x2F;span&gt;&lt;span&gt; D &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;		#10&lt;&#x2F;span&gt;&lt;span&gt; D &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;		#10&lt;&#x2F;span&gt;&lt;span&gt; D &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;		&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;		#10&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; $finish&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	end&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	initial&lt;&#x2F;span&gt;&lt;span&gt; $monitor(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;[Time=%0t] D = %0b CLK = %0b Q = %0b&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt;$time&lt;&#x2F;span&gt;&lt;span&gt;, D, CLK, Q);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;endmodule&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In our testbench, we have defined two variables (&lt;strong&gt;D&lt;&#x2F;strong&gt; and &lt;strong&gt;CLK&lt;&#x2F;strong&gt;) which will be the inputs of the data flip flop and we have declared one variable (&lt;strong&gt;Q&lt;&#x2F;strong&gt;) which will be the output of our flip flop.&lt;&#x2F;p&gt;
&lt;p&gt;Next we have &lt;em&gt;initialized&lt;&#x2F;em&gt; our data flip flop (&lt;strong&gt;DFF&lt;&#x2F;strong&gt;) by passing in our variables.&lt;&#x2F;p&gt;
&lt;p&gt;Now comes the meaty part. We have three procedural blocks,&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;always&lt;&#x2F;strong&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;initial&lt;&#x2F;strong&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;The &lt;strong&gt;always&lt;&#x2F;strong&gt; block toggles the value of &lt;strong&gt;CLK&lt;&#x2F;strong&gt; every &lt;em&gt;5 seconds.&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;p&gt;In the first &lt;strong&gt;initial&lt;&#x2F;strong&gt; block, we are assigning values to the &lt;code&gt;D&lt;&#x2F;code&gt; register.&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;At time unit &lt;code&gt;10ns&lt;&#x2F;code&gt; , the value of &lt;code&gt;D&lt;&#x2F;code&gt; is &lt;code&gt;1&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;At time unit &lt;code&gt;20ns&lt;&#x2F;code&gt;, the value of &lt;code&gt;D&lt;&#x2F;code&gt; is &lt;code&gt;0&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;At time unit &lt;code&gt;30ns&lt;&#x2F;code&gt; , the value of &lt;code&gt;D&lt;&#x2F;code&gt; is &lt;code&gt;1&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;In the second &lt;strong&gt;initial&lt;&#x2F;strong&gt; block, we have a &lt;em&gt;&lt;strong&gt;system task&lt;&#x2F;strong&gt;&lt;&#x2F;em&gt; called &lt;code&gt;monitor&lt;&#x2F;code&gt;. It continuously tracks and displays the values of specified signals whenever there is a change in any of them.&lt;&#x2F;p&gt;
&lt;p&gt;When we run the testbench, we will get the following output:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[Time:                    0], D=0, CLK=0, Q=x&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[Time:                    5], D=0, CLK=1, Q=0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[Time:                   10], D=1, CLK=0, Q=0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[Time:                   15], D=1, CLK=1, Q=1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[Time:                   20], D=0, CLK=0, Q=1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[Time:                   25], D=0, CLK=1, Q=0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[Time:                   30], D=1, CLK=0, Q=0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[Time:                   35], D=1, CLK=1, Q=1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;dff_tb.v:21: $finish called at 40 (1s)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[Time:                   40], D=1, CLK=0, Q=1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Whenever the value of &lt;code&gt;CLK&lt;&#x2F;code&gt; is 1, the value of &lt;code&gt;Q&lt;&#x2F;code&gt; is whatever the value of &lt;code&gt;D&lt;&#x2F;code&gt;. And other times (value of &lt;code&gt;CLK=0&lt;&#x2F;code&gt;), the value of &lt;code&gt;Q&lt;&#x2F;code&gt; is the previous value of &lt;code&gt;D&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Voila 🎉 we have successfully implemented and tested our Data Flip Flop. To take it further, you can use this to build &lt;strong&gt;Registers&lt;&#x2F;strong&gt;.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Writing a simple Lexer</title>
		<published>2024-04-08T00:00:00+00:00</published>
		<updated>2024-04-08T00:00:00+00:00</updated>
		<link rel="alternate" type="text/html" href="https://fazeneo.in/posts/writing-a-simple-lexer/"/>
		<id>https://fazeneo.in/posts/writing-a-simple-lexer/</id>
    
		<content type="html" xml:base="https://fazeneo.in/posts/writing-a-simple-lexer/">&lt;p&gt;In this blog post, I’ll be going over how to write a simple &lt;em&gt;Lexer&lt;&#x2F;em&gt; that parses and generates &lt;em&gt;Tokens&lt;&#x2F;em&gt; for few characters. We’ll also add few rules to the parsing logic on how to parse certain characters.&lt;&#x2F;p&gt;
&lt;p&gt;The code can be found on &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;M-krishna&#x2F;lexer&quot;&gt;Github&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Let’s first define what we are trying to &lt;em&gt;parse&lt;&#x2F;em&gt;. We’ll parse the following characters.&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;{&lt;&#x2F;code&gt; → open curly bracket&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;}&lt;&#x2F;code&gt; → closed curly bracket&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;(&lt;&#x2F;code&gt; → open parenthesis&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;)&lt;&#x2F;code&gt; → closed parenthesis&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;*&lt;&#x2F;code&gt; → star&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;+&lt;&#x2F;code&gt; → plus&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;,&lt;&#x2F;code&gt; → comma&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;.&lt;&#x2F;code&gt; → dot&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;-&lt;&#x2F;code&gt; → minus&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Our goal is to parse these characters and generate token out of these. Generally we call these characters &lt;em&gt;lexemes&lt;&#x2F;em&gt;. For example, &lt;code&gt;{&lt;&#x2F;code&gt; is represented as &lt;code&gt;LEFT_BRACE&lt;&#x2F;code&gt; and you can name it whatever you want as long it represents the correct token. Let’s breakdown our problem. Currently, our goal is to parse only one character. Let’s choose &lt;code&gt;{&lt;&#x2F;code&gt; for our initial implementation.&lt;&#x2F;p&gt;
&lt;p&gt;We should parse the &lt;code&gt;{&lt;&#x2F;code&gt; and generate a token like &lt;code&gt;LEFT_BRACE {&lt;&#x2F;code&gt; in a string format.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;initial-implementation&quot;&gt;Initial implementation&lt;a class=&quot;zola-anchor&quot; href=&quot;#initial-implementation&quot; aria-label=&quot;Anchor link for: initial-implementation&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h2&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;Let’s start by defining our token type because eventually we’ll be generating token once we parse a character.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt; enum&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; import&lt;&#x2F;span&gt;&lt;span&gt; Enum&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; TokenType&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity&quot;&gt;Enum&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;	LEFT_BRACE&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In the above code snippet, I have created a class named &lt;code&gt;TokenType&lt;&#x2F;code&gt; which is an &lt;em&gt;enum&lt;&#x2F;em&gt;, which holds all the required &lt;em&gt;tokens&lt;&#x2F;em&gt; for our parser(lexer). Currently we have only one token which is the left curly brace (&lt;code&gt;{&lt;&#x2F;code&gt;), we’ll add more once we setup our parser.&lt;&#x2F;p&gt;
&lt;p&gt;Now let’s create a class for our Lexer which takes in a &lt;em&gt;source&lt;&#x2F;em&gt;, which are the characters that we want to parse.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; Lexer&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;	def&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; __init__&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter z-function&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter z-function&quot;&gt; source&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-language&quot;&gt;		self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;source&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; source&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;		&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;if&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-variable&quot;&gt; __name__&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;__main__&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	characters&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; str&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;{&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-comment&quot;&gt; #&lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt; for now only one&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	lexer&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; Lexer&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;characters&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In the above code snippet, we have defined a class named &lt;code&gt;Lexer&lt;&#x2F;code&gt; which will hold everything that we need to achieve our goal. Our &lt;em&gt;Lexer&lt;&#x2F;em&gt; takes in a &lt;em&gt;source&lt;&#x2F;em&gt; which is nothing but a &lt;em&gt;string of characters,&lt;&#x2F;em&gt; that contains the character that we need to parse.&lt;&#x2F;p&gt;
&lt;p&gt;Under the &lt;em&gt;if&lt;&#x2F;em&gt; condition, we have a variable named &lt;code&gt;characters&lt;&#x2F;code&gt; which will hold the things we need to parse. Currently it contains &lt;code&gt;{&lt;&#x2F;code&gt; , we’ll add more later. Right after that, we have initialized our &lt;code&gt;Lexer&lt;&#x2F;code&gt; class by passing in the &lt;em&gt;characters&lt;&#x2F;em&gt; as &lt;em&gt;source&lt;&#x2F;em&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;The next step is to “consume” the character and for now, let’s print it.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;consume-characters&quot;&gt;Consume characters&lt;a class=&quot;zola-anchor&quot; href=&quot;#consume-characters&quot; aria-label=&quot;Anchor link for: consume-characters&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h2&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;Let’s add a method that consume characters one by one. For now, we implement this method that’ll print the character it consumes.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; Lexer&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;	def&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; __init__&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter z-function&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter z-function&quot;&gt; source&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; None&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-language&quot;&gt;		self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;source&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; source&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-language&quot;&gt;		self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;start_position&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-language&quot;&gt;		self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;current_position&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;	def&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; consume_characters&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter z-function&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; None&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;		while&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;not&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-language&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;isAtEnd&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-language&quot;&gt;			self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;start_position&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-language&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;current_position&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-language&quot;&gt;			self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;_consume_character&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;			&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;	def&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; _consume_character&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter z-function&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; None&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;		current_character&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; str&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-language&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;eat_and_move_on&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support&quot;&gt;		print&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;current_character&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;		&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;	def&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; eat_and_move_on&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter z-function&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-language&quot;&gt;		self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;current_position&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-language&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;current_position&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; +&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;		return&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-language&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;source&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-language&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;current_position&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; -&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;		&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;	def&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; isAtEnd&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter z-function&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; bool&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;		return&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-language&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;current_position&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; &amp;gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; len&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-language&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;source&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;There are a few things to unpack from the above code snippet. Let’s look at one by one. We have two similar methods &lt;code&gt;consume_characters&lt;&#x2F;code&gt; and &lt;code&gt;_consume_character&lt;&#x2F;code&gt;. And we have a couple of helper methods &lt;code&gt;isAtEnd&lt;&#x2F;code&gt; and &lt;code&gt;eat_and_move_on&lt;&#x2F;code&gt; which we are using to make a clear abstraction.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;consume_characters&lt;&#x2F;code&gt; is a top level method. The goal of this method is to consume all the characters that we pass one by one until it reaches the EOF(End of file). This method uses the &lt;code&gt;isAtEnd&lt;&#x2F;code&gt; helper method to constantly check whether it reached the end of the file or not.&lt;&#x2F;p&gt;
&lt;p&gt;If it doesn’t reach the end of the file, we are doing two things&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Setting the value of &lt;code&gt;start_position&lt;&#x2F;code&gt; to the current value of &lt;code&gt;current_position&lt;&#x2F;code&gt; . We will see why we are doing this.&lt;&#x2F;li&gt;
&lt;li&gt;Second we are calling the &lt;code&gt;_consume_character&lt;&#x2F;code&gt; method. The goal of the method is to consume the one character at a time and process it. For now we are just printing it.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;The &lt;code&gt;isAtEnd&lt;&#x2F;code&gt; method is pretty self explanatory, it checks whether the &lt;em&gt;current_position&lt;&#x2F;em&gt; reaches the end of the file. It does that by comparing the value of the &lt;em&gt;current_position&lt;&#x2F;em&gt; and the length of the &lt;em&gt;source&lt;&#x2F;em&gt; (which is the string of characters that we want to parse).&lt;&#x2F;p&gt;
&lt;p&gt;As the name suggests, the &lt;code&gt;eat_and_move_on&lt;&#x2F;code&gt; method “eats” the current character that the &lt;code&gt;current_position&lt;&#x2F;code&gt; points to and then increment the value of the &lt;code&gt;current_position&lt;&#x2F;code&gt; so that on the next iteration, it can “eat” the next character. Here “eat” is just a metaphor for &lt;em&gt;consume&lt;&#x2F;em&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;We’ll update our initialization code and test our current functionality.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;if&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-variable&quot;&gt; __name__&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;__main__&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	characters&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; str&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;{&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-comment&quot;&gt; #&lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt; for now only one&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	lexer&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; Lexer&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;characters&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	lexer&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;consume_characters&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Go ahead and run this code and see what’s happening. You should see &lt;code&gt;{&lt;&#x2F;code&gt; on the console.&lt;&#x2F;p&gt;
&lt;p&gt;We can add more characters to our &lt;code&gt;characters&lt;&#x2F;code&gt; variable and our program will still consume and eat all the characters. Try to run the program by modifying the value of the &lt;code&gt;characters&lt;&#x2F;code&gt; variable to this: &lt;code&gt;{}{}{}{}&lt;&#x2F;code&gt; , you should see all the characters print in a new line. Now that we’ve written our logic to consume character one by one, we can start generating token from it. But first of all what are &lt;em&gt;tokens&lt;&#x2F;em&gt;?&lt;&#x2F;p&gt;
&lt;h2 id=&quot;tokens&quot;&gt;Tokens&lt;a class=&quot;zola-anchor&quot; href=&quot;#tokens&quot; aria-label=&quot;Anchor link for: tokens&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h2&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;Tokens are the smallest unit of meaningful text in programming languages, such as keywords, identifiers, operators, literals and punctuation marks. Tokens serve as the basic building blocks of the language’s syntax and used to represent individual elements within code.&lt;&#x2F;p&gt;
&lt;p&gt;As you have seen from the code snippet at the very beginning of this blog post, we have a &lt;em&gt;token type&lt;&#x2F;em&gt; called &lt;code&gt;LEFT_BRACE&lt;&#x2F;code&gt; which is a meaningful representation of &lt;code&gt;{&lt;&#x2F;code&gt;. These are what we call as &lt;em&gt;tokens&lt;&#x2F;em&gt;. Let’s look at a few more single character token types.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt; enum&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; import&lt;&#x2F;span&gt;&lt;span&gt; Enum&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; TokenType&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity&quot;&gt;Enum&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;  LEFT_BRACE&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;        =&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;  RIGHT_BRACE&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;       =&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 2&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;  LEFT_PAREN&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;        =&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 3&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;  RIGHT_PAREN&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;       =&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 4&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;  STAR&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;              =&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 5&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;  PLUS&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;              =&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 6&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;  MINUS&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;             =&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 7&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;  COMMA&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;             =&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 8&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;  DOT&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;               =&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 9&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In the above code snippet, we’ve added few more meaningful representation of the characters. You’ll see how tokens come together once we start generating it buy parsing the characters. Let’s generate tokens now, shall we?&lt;&#x2F;p&gt;
&lt;h2 id=&quot;generating-tokens&quot;&gt;Generating Tokens&lt;a class=&quot;zola-anchor&quot; href=&quot;#generating-tokens&quot; aria-label=&quot;Anchor link for: generating-tokens&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h2&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;We’ll start from where we left off. In our &lt;code&gt;Lexer&lt;&#x2F;code&gt; class, specifically in the &lt;code&gt;consume_character&lt;&#x2F;code&gt; method, we were just printing the characters. Now we’ll use the characters to generate tokens. Let’s first generate a token for the &lt;code&gt;{&lt;&#x2F;code&gt; (LEFT_BRACE) character.&lt;&#x2F;p&gt;
&lt;p&gt;But how do we represent &lt;em&gt;tokens&lt;&#x2F;em&gt;? Currently we have represented them as text like &lt;code&gt;LEFT_BRACE&lt;&#x2F;code&gt; , &lt;code&gt;RIGHT_BRACE&lt;&#x2F;code&gt; and so on. But we have to represent them in some way. We’ll create a &lt;code&gt;Token&lt;&#x2F;code&gt; class which for now only holds the &lt;em&gt;character&lt;&#x2F;em&gt; and the &lt;em&gt;textual representation&lt;&#x2F;em&gt; of that character.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; Token&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;	def&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; __init__&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter z-function&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter z-function&quot;&gt; tt&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span&gt; TokenType&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter z-function&quot;&gt; value&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; chr&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; None&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-language&quot;&gt;		self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;token_type&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; tt&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-language&quot;&gt;		self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;value&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;      =&lt;&#x2F;span&gt;&lt;span&gt; value&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;	def&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; __repr__&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter z-function&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; str&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt; f&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-language&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;token_type&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; {&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-language&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;value&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In the above code snippet, we’ve created a &lt;code&gt;Token&lt;&#x2F;code&gt; class, which holds the &lt;em&gt;token type&lt;&#x2F;em&gt; and the &lt;em&gt;value&lt;&#x2F;em&gt;(character) of the token. Now that we can represent &lt;em&gt;tokens&lt;&#x2F;em&gt; in some form, let’s start generating them.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; Lexer&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;	def&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; __init__&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter z-function&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span&gt; ...&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; None&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;		...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;		...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-language&quot;&gt;		self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;tokens&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; list&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-comment&quot;&gt; #&lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt; we store all the tokens in a list&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;	def&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; _consume_character&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter z-function&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; None&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;		current_character&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; str&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-language&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;eat_and_move_on&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;		if&lt;&#x2F;span&gt;&lt;span&gt; current_character&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt; &amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;{&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-language&quot;&gt;			self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;generate_token&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;TokenType&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;LEFT_BRACE&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;name&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;		&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;	def&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; generate_token&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter z-function&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter z-function&quot;&gt; tt&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span&gt; TokenType&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; None&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;		text&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; str&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-language&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;source&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-language&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;start_position&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-language&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;current_position&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;		token&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span&gt; Token&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; Token&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;tt&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span&gt; text&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-language&quot;&gt;		self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;tokens&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;append&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;token&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Let’s unpack the above code.&lt;&#x2F;p&gt;
&lt;p&gt;I’ve added a new method named &lt;code&gt;generate_token&lt;&#x2F;code&gt; which takes in the &lt;code&gt;TokenType&lt;&#x2F;code&gt; based on the character which we are currently parsing and it gets the &lt;em&gt;text&lt;&#x2F;em&gt;(character) from the &lt;em&gt;source&lt;&#x2F;em&gt; using the &lt;em&gt;start position&lt;&#x2F;em&gt; and the &lt;em&gt;current position&lt;&#x2F;em&gt;. Finally it creates a new &lt;em&gt;Token&lt;&#x2F;em&gt; object and pushes it into our &lt;em&gt;tokens&lt;&#x2F;em&gt; list.&lt;&#x2F;p&gt;
&lt;p&gt;In the &lt;code&gt;consume_character&lt;&#x2F;code&gt; , I’ve added an &lt;em&gt;if condition&lt;&#x2F;em&gt; which checks for the &lt;code&gt;{&lt;&#x2F;code&gt; character and if it’s &lt;em&gt;true&lt;&#x2F;em&gt; then, we use the &lt;code&gt;generate_token&lt;&#x2F;code&gt; method by passing in the &lt;em&gt;token type&lt;&#x2F;em&gt; and generate the token.&lt;&#x2F;p&gt;
&lt;p&gt;Go ahead and execute this code and try to print the &lt;code&gt;tokens&lt;&#x2F;code&gt; list and see what is in that list for yourself.&lt;&#x2F;p&gt;
&lt;p&gt;Currently, we are generating token only for the &lt;code&gt;{&lt;&#x2F;code&gt; character. We will now add all the characters that our &lt;em&gt;lexer&lt;&#x2F;em&gt; needs to parse and generate &lt;em&gt;token.&lt;&#x2F;em&gt; If you look at the above &lt;code&gt;TokenType&lt;&#x2F;code&gt; class we’ve added all the lexemes that we are trying to parse. Now the only thing that we have to do is generate tokens based on the characters. Let’s do that.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;def&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; _consume_character&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter z-function&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; None&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;		current_character&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; str&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-language&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;eat_and_move_on&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;		if&lt;&#x2F;span&gt;&lt;span&gt; current_character&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt; &amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;{&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-language&quot;&gt;			self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;generate_token&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;TokenType&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;LEFT_BRACE&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;name&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;		if&lt;&#x2F;span&gt;&lt;span&gt; current_character&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt; &amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-language&quot;&gt;			self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;generate_token&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;TokenType&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;RIGHT_BRACE&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;name&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;		if&lt;&#x2F;span&gt;&lt;span&gt; current_character&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt; &amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-language&quot;&gt;			self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;generate_token&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;TokenType&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;LEFT_PAREN&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;name&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;		if&lt;&#x2F;span&gt;&lt;span&gt; current_character&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt; &amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-language&quot;&gt;			self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;generate_token&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;TokenType&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;RIGHT_PAREN&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;name&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;		if&lt;&#x2F;span&gt;&lt;span&gt; current_character&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt; &amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;*&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-language&quot;&gt;			self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;generate_token&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;TokenType&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;STAR&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;name&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;		if&lt;&#x2F;span&gt;&lt;span&gt; current_character&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt; &amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;+&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-language&quot;&gt;			self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;generate_token&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;TokenType&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;PLUS&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;name&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;		if&lt;&#x2F;span&gt;&lt;span&gt; current_character&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt; &amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;-&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-language&quot;&gt;			self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;generate_token&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;TokenType&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;MINUS&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;name&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;		if&lt;&#x2F;span&gt;&lt;span&gt; current_character&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt; &amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-language&quot;&gt;			self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;generate_token&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;TokenType&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;COMMA&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;name&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;		if&lt;&#x2F;span&gt;&lt;span&gt; current_character&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt; &amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-language&quot;&gt;			self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;generate_token&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;TokenType&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;DOT&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;name&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We’ve added few more &lt;em&gt;if conditions&lt;&#x2F;em&gt; to check for other types of characters and generate tokens for them. Don’t worry about the beauty of the code, since Python doesn’t support switch cases we have used &lt;em&gt;if conditions&lt;&#x2F;em&gt;, we can also avoid using &lt;em&gt;if statements&lt;&#x2F;em&gt; and do this much more cleaner. But to keep things simple we are gonna stay with this for now.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;ignoring-whitespaces&quot;&gt;Ignoring whitespaces&lt;a class=&quot;zola-anchor&quot; href=&quot;#ignoring-whitespaces&quot; aria-label=&quot;Anchor link for: ignoring-whitespaces&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h2&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;Let’s add the ability to ignore (discard) whitespaces to our Lexer. We will take three things into consideration.&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;Simple space (’ ‘)&lt;&#x2F;li&gt;
&lt;li&gt;Carriage return (’\r’)&lt;&#x2F;li&gt;
&lt;li&gt;New line (’\n’)&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;If we come across these characters during our source language parsing we should ignore them. We’ll add this functionality to our &lt;code&gt;_consume_character&lt;&#x2F;code&gt; function. But first we’ll define a function to check whitespace characters.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;def&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; isWhitespace&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter z-function&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter z-function&quot;&gt; character&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; str&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; bool&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;	SPACE&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt; &amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt; &amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;	NEWLINE&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt; &amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;	CARRIAGE_RETURN&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt; &amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;\r&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	return&lt;&#x2F;span&gt;&lt;span&gt; character&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; in&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;SPACE&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; NEWLINE&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; CARRIAGE_RETURN&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;def&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; _consume_character&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter z-function&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; None&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	current_character&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; str&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-language&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;eat_and_move_on&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;	...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	if&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-language&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;isWhitespace&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;current_character&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;		return&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We’ve added a method named &lt;code&gt;isWhitespace&lt;&#x2F;code&gt; which takes in a character we are currently parsing and checks whether it is a &lt;em&gt;white space&lt;&#x2F;em&gt; or not. In the &lt;code&gt;_consume_character&lt;&#x2F;code&gt; method we are using that function by passing in the character. If it returns &lt;em&gt;true&lt;&#x2F;em&gt; we are returning from the function, which means we are doing nothing (discarding). That’s it, we’ve successfully added our functionality to ignore whitespaces. Go ahead and play around with the code and see what happens.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;two-character-lexemes&quot;&gt;Two character lexemes&lt;a class=&quot;zola-anchor&quot; href=&quot;#two-character-lexemes&quot; aria-label=&quot;Anchor link for: two-character-lexemes&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h2&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;So far we’ve seen only one character lexemes like {, }, (, and so on. We’ll look at how to handle &lt;em&gt;&lt;strong&gt;relational operators&lt;&#x2F;strong&gt;&lt;&#x2F;em&gt;. Things like:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;!=&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;==&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;&amp;lt;=&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;&amp;gt;=&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;As you can see, there will be two cases that we have to deal with when parsing these characters and generating tokens. Let’s look at an example for &lt;code&gt;!=&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;parsing&quot;&gt;Parsing &lt;code&gt;!=&lt;&#x2F;code&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#parsing&quot; aria-label=&quot;Anchor link for: parsing&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h3&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;The bang operator (&lt;code&gt;!&lt;&#x2F;code&gt;) can be used as a standalone operator, which stands for &lt;em&gt;negation&lt;&#x2F;em&gt; or &lt;em&gt;not.&lt;&#x2F;em&gt; So when parsing &lt;code&gt;!&lt;&#x2F;code&gt; we have to check whether the next character is &lt;code&gt;=&lt;&#x2F;code&gt; or something else. If it is &lt;code&gt;=&lt;&#x2F;code&gt; we have to consider both of the character as a single token, which represents &lt;code&gt;!=&lt;&#x2F;code&gt; (&lt;em&gt;not equal to&lt;&#x2F;em&gt;).&lt;&#x2F;p&gt;
&lt;p&gt;If it is only &lt;code&gt;!&lt;&#x2F;code&gt; we have to consider it as &lt;em&gt;not&lt;&#x2F;em&gt; (negation) operator. Let’s add the functionality to achieve this.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; TokenType&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity&quot;&gt;Enum&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;	...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;	...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;	BANG&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 10&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;	BANG_EQUAL&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 11&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; Lexer&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;	...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;	def&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; match_next_character&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter z-function&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter z-function&quot;&gt; expected_character&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; str&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; bool&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;		if&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-language&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;isAtEnd&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; return&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; False&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;		if&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-language&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;source&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-language&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;current_position&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; !=&lt;&#x2F;span&gt;&lt;span&gt; expected_character&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; return&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; False&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;		&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-language&quot;&gt;		self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;current_position&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-language&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;current_position&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; +&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;		return&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; True&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;		&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;	def&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; _consume_character&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter z-function&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; None&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;		current_character&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; str&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-language&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;eat_and_move_on&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;		...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;		...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;		if&lt;&#x2F;span&gt;&lt;span&gt; current_character&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt; &amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;!&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;			if&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-language&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;match_next_character&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-language&quot;&gt;				self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;generate_and_add_token&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;TokenType&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;BANG_EQUAL&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;name&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-language&quot;&gt;			self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;generate_and_add_token&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;TokenType&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;BANG&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;name&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;			&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;characters&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; str&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;!==&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;lexer&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;consume_characters&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Let’s unpack. We’ve added a new method named &lt;code&gt;match_next_character&lt;&#x2F;code&gt; which takes in the &lt;em&gt;expected_character&lt;&#x2F;em&gt; which we are looking for and returns &lt;em&gt;true&lt;&#x2F;em&gt; if it matches else &lt;em&gt;false&lt;&#x2F;em&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;In the &lt;code&gt;_consume_character&lt;&#x2F;code&gt; method we’ve added a condition to check if the current character that we are parsing is &lt;code&gt;!&lt;&#x2F;code&gt; , if its &lt;em&gt;true&lt;&#x2F;em&gt;, we are checking whether the &lt;em&gt;next character&lt;&#x2F;em&gt; is equal to &lt;code&gt;=&lt;&#x2F;code&gt;. If its &lt;em&gt;true&lt;&#x2F;em&gt;, we are generating a token with the &lt;code&gt;BANG_EQUAL&lt;&#x2F;code&gt; type, else &lt;code&gt;BANG&lt;&#x2F;code&gt; type. We are using the keyword &lt;code&gt;BANG&lt;&#x2F;code&gt; because, this operator (&lt;code&gt;!&lt;&#x2F;code&gt;) is called as &lt;em&gt;bang operator&lt;&#x2F;em&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;If you notice, in the &lt;code&gt;match_next_character&lt;&#x2F;code&gt; method, we are &lt;em&gt;incrementing the value of &lt;code&gt;current_position&lt;&#x2F;code&gt;&lt;&#x2F;em&gt;. Why is that? It’s because, we’ve confirmed that the next character matches the &lt;code&gt;=&lt;&#x2F;code&gt; character, so we have to treat &lt;code&gt;!=&lt;&#x2F;code&gt; as a single operator. For this particular reason, we are incrementing the &lt;code&gt;current_position&lt;&#x2F;code&gt; value.&lt;&#x2F;p&gt;
&lt;p&gt;If we didn’t do that, our Lexer will parse the &lt;code&gt;=&lt;&#x2F;code&gt; again even after we consider the &lt;code&gt;!=&lt;&#x2F;code&gt; as a single operator. Go ahead and comment out that line and run the code to see the difference.&lt;&#x2F;p&gt;
&lt;p&gt;Voila 🎉 we’ve successfully parsed the &lt;code&gt;!=&lt;&#x2F;code&gt; operator. In a similar way, we can able to parse the remaining three &lt;em&gt;relational operators&lt;&#x2F;em&gt;.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; current_character&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt; &amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-language&quot;&gt;    self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;generate_and_add_token&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        TokenType&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;EQUAL_EQUAL&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;name&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; if&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-language&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;match_next_character&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; else&lt;&#x2F;span&gt;&lt;span&gt; TokenType&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;EQUAL&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;name&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    )&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; current_character&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt; &amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-language&quot;&gt;    self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;generate_and_add_token&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        TokenType&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;LESS_THAN_EQUAL&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;name&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; if&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-language&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;match_next_character&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; else&lt;&#x2F;span&gt;&lt;span&gt; TokenType&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;LESS_THAN&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;name&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    )&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; current_character&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt; &amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-language&quot;&gt;    self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;generate_and_add_token&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        TokenType&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;GREATER_THAN_EQUAL&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;name&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; if&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-language&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;match_next_character&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; else&lt;&#x2F;span&gt;&lt;span&gt; TokenType&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;GREATER_THAN&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;name&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    )&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;keyword-parsing&quot;&gt;Keyword Parsing&lt;a class=&quot;zola-anchor&quot; href=&quot;#keyword-parsing&quot; aria-label=&quot;Anchor link for: keyword-parsing&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h2&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;Let’s examine some keywords commonly found in most programming languages. To keep things simpler, let’s take some keywords from Python.&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;and&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;or&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;not&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;if&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;elif&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;else&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;while&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;for&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;break&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;continue&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;def&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;return&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;class&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;print&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Let’s breakdown the approach for parsing the keywords.&lt;&#x2F;p&gt;
&lt;p&gt;Whenever we come across a character, we will first check whether it is an alphabet or not, why? because all the &lt;em&gt;keywords&lt;&#x2F;em&gt; above only contain alphabets. Once we confirm that it’s an alphabet, we’ll &lt;em&gt;peek&lt;&#x2F;em&gt; through the following characters and check each and every character is an alphabet or not. Once we reach a &lt;em&gt;non-alphabet&lt;&#x2F;em&gt; character, we will stop there and take the &lt;em&gt;text&lt;&#x2F;em&gt; we parsed. Then we will check the parsed word with our keywords list, if we find a match, we will be generating the appropriate token.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; TokenType&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity&quot;&gt;Enum&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;	...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;	...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment&quot;&gt;  #&lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt; Keywords&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;  AND&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 18&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;  OR&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 19&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;  NOT&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 20&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;  IF&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 21&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;  ELIF&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 22&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;  ELSE&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 23&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;  WHILE&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 24&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;  FOR&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 25&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;  BREAK&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 26&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;  CONTINUE&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 27&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;  DEF&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 28&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;  RETURN&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 29&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;  CLASS&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 30&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;  PRINT&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 31&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; Lexer&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment&quot;&gt;   #&lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt; Keywords object which we will use to match against the parsed text&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;    KEYWORDS&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;    &amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;and&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span&gt; TokenType&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;AND&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;name&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;    &amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;or&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span&gt; TokenType&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;OR&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;name&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;    &amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;not&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span&gt; TokenType&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;NOT&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;name&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;    &amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;if&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span&gt; TokenType&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;IF&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;name&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;    &amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;elif&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span&gt; TokenType&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;ELIF&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;name&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;    &amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;else&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span&gt; TokenType&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;ELSE&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;name&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;    &amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;while&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span&gt; TokenType&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;WHILE&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;name&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;    &amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;for&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span&gt; TokenType&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;FOR&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;name&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;    &amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;break&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span&gt; TokenType&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;BREAK&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;name&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;    &amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;continue&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span&gt; TokenType&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;CONTINUE&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;name&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;    &amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;def&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span&gt; TokenType&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;DEF&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;name&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;    &amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;return&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span&gt; TokenType&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;RETURN&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;name&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;    &amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span&gt; TokenType&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;CLASS&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;name&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;    &amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;print&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span&gt; TokenType&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;PRINT&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;name&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;	...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;	...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	if&lt;&#x2F;span&gt;&lt;span&gt; current_character&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;isalpha&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	  while&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-language&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;peek&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;isalpha&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-language&quot;&gt;	      self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;eat_and_move_on&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	  keyword&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; str&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-language&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;source&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-language&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;start_position&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-language&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;current_position&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-language&quot;&gt;	  self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;generate_and_add_token&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	      Lexer&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;KEYWORDS&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;get&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;keyword&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; None&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	  )&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	  &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;    def&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; peek&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter z-function&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; chr&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;        if&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-language&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;isAtEnd&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; return&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt; &amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;\0&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;        return&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-language&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;source&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-language&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;current_position&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;There is a lot to unpack. Let’s look at it one by one.&lt;&#x2F;p&gt;
&lt;p&gt;We have added all the keywords that we needed under &lt;code&gt;TokenType&lt;&#x2F;code&gt; and we have also created a &lt;code&gt;KEYWORDS&lt;&#x2F;code&gt; object, which holds all the keywords with their names as &lt;em&gt;keys&lt;&#x2F;em&gt; and their respective token types as their &lt;em&gt;values&lt;&#x2F;em&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;We have also added a new method named &lt;code&gt;peek&lt;&#x2F;code&gt; . The job of this method is to return the character in the &lt;em&gt;current_position&lt;&#x2F;em&gt;. We will use this method to read (parse) keywords.&lt;&#x2F;p&gt;
&lt;p&gt;Finally we’ve added an &lt;em&gt;if condition&lt;&#x2F;em&gt; which checks whether the character we are parsing is an &lt;em&gt;alphabet&lt;&#x2F;em&gt; or not. If it is an alphabet we will continue reading the following characters using the &lt;code&gt;peek&lt;&#x2F;code&gt; method until we reach any &lt;em&gt;non-alphabetic&lt;&#x2F;em&gt; character. Once we have reached the end of the non-alphabetic character, we will use the value of &lt;code&gt;start_position&lt;&#x2F;code&gt; and &lt;code&gt;current_position&lt;&#x2F;code&gt; to get the parsed text or character. Since our &lt;code&gt;KEYWORDS&lt;&#x2F;code&gt; object holds all the necessary keyword, we’ll match it against that object. If we find a match then generate the appropriate token, else do nothing.&lt;&#x2F;p&gt;
&lt;p&gt;We have reached the end of this blog post. We have created a Lexer that parses quite a lot of content. On that note, try adding support for parsing &lt;em&gt;strings&lt;&#x2F;em&gt;, &lt;em&gt;numbers&lt;&#x2F;em&gt; and &lt;em&gt;identifiers.&lt;&#x2F;em&gt; To take it one step further modify the code to parse a subset of Javascript.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Associative and Commutative property of Addition in Coq</title>
		<published>2024-03-28T00:00:00+00:00</published>
		<updated>2024-03-28T00:00:00+00:00</updated>
		<link rel="alternate" type="text/html" href="https://fazeneo.in/posts/associative-and-commutative-property-of-addition-in-coq/"/>
		<id>https://fazeneo.in/posts/associative-and-commutative-property-of-addition-in-coq/</id>
    
		<content type="html" xml:base="https://fazeneo.in/posts/associative-and-commutative-property-of-addition-in-coq/">&lt;p&gt;In this blog post, I’ll be going over &lt;strong&gt;associative&lt;&#x2F;strong&gt; and &lt;strong&gt;commutative&lt;&#x2F;strong&gt; property of &lt;em&gt;&lt;strong&gt;addition&lt;&#x2F;strong&gt;&lt;&#x2F;em&gt;. Also, I’ll be going over how to prove that using Coq. But first of all, what is &lt;em&gt;addition&lt;&#x2F;em&gt;?&lt;&#x2F;p&gt;
&lt;h2 id=&quot;addition&quot;&gt;Addition&lt;a class=&quot;zola-anchor&quot; href=&quot;#addition&quot; aria-label=&quot;Anchor link for: addition&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h2&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;We all know what &lt;em&gt;addition&lt;&#x2F;em&gt; is but I’ll still explain it in brief. In mathematics, &lt;em&gt;addition&lt;&#x2F;em&gt; is the process of adding two or more numbers together to find the final result. Addition is represented by the &lt;strong&gt;+&lt;&#x2F;strong&gt; symbol. For example: &lt;strong&gt;1 + 1&lt;&#x2F;strong&gt; equals &lt;strong&gt;2, 10 + 10&lt;&#x2F;strong&gt; equals &lt;strong&gt;20&lt;&#x2F;strong&gt; so on.&lt;&#x2F;p&gt;
&lt;p&gt;In programming perspective, think of &lt;strong&gt;+&lt;&#x2F;strong&gt; as a function that takes in two arguments of type number(Integer) and adds them together and returns the result. Now we know what &lt;em&gt;addition&lt;&#x2F;em&gt; is let’s take a look at some properties of &lt;em&gt;addition&lt;&#x2F;em&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;induction&quot;&gt;Induction&lt;a class=&quot;zola-anchor&quot; href=&quot;#induction&quot; aria-label=&quot;Anchor link for: induction&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h2&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;Before looking into the properties of &lt;em&gt;addition&lt;&#x2F;em&gt;, let’s first take a look at what &lt;em&gt;induction&lt;&#x2F;em&gt; is. Because we are going to use &lt;strong&gt;proof by induction&lt;&#x2F;strong&gt; method to solve the theorems&#x2F;proofs.&lt;&#x2F;p&gt;
&lt;p&gt;What is &lt;strong&gt;Proof by Induction&lt;&#x2F;strong&gt;?&lt;&#x2F;p&gt;
&lt;p&gt;“Mathematical Induction is a method for proving that a statement &lt;em&gt;P(n)&lt;&#x2F;em&gt; is true for every &lt;em&gt;natural number n&lt;&#x2F;em&gt;, that is, infinitely many cases &lt;em&gt;P(0), P(1), P(2), P(3)…..P(n)&lt;&#x2F;em&gt; all hold. This is done by first proving a simple case, then also showing that if we assume the claim is true for a given case, then the next case is also true.” — &lt;strong&gt;From Wikipedia&lt;&#x2F;strong&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;If we break this down step by step, Mathematical induction contains two steps.&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;Show it is true for the first one.&lt;&#x2F;li&gt;
&lt;li&gt;Show that if any one is true then the next one is true.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;Then all are true.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;induction-step-1&quot;&gt;Induction step 1&lt;a class=&quot;zola-anchor&quot; href=&quot;#induction-step-1&quot; aria-label=&quot;Anchor link for: induction-step-1&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h3&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;We have to prove the base case, ie., &lt;em&gt;&lt;strong&gt;n = 0 or n = 1&lt;&#x2F;strong&gt;&lt;&#x2F;em&gt;.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;induction-step-2&quot;&gt;Induction step 2&lt;a class=&quot;zola-anchor&quot; href=&quot;#induction-step-2&quot; aria-label=&quot;Anchor link for: induction-step-2&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h3&gt;
&lt;hr &#x2F;&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Assume&lt;&#x2F;strong&gt; it is true for &lt;em&gt;&lt;strong&gt;n = k&lt;&#x2F;strong&gt;&lt;&#x2F;em&gt;, where &lt;strong&gt;k&lt;&#x2F;strong&gt; is an arbitrary number.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Prove&lt;&#x2F;strong&gt; it is true for &lt;em&gt;&lt;strong&gt;n = k + 1&lt;&#x2F;strong&gt;&lt;&#x2F;em&gt; (we can use the &lt;em&gt;&lt;strong&gt;n = k&lt;&#x2F;strong&gt;&lt;&#x2F;em&gt; as a fact).&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;We’ll see how this(&lt;em&gt;induction&lt;&#x2F;em&gt;) works out when proving the &lt;em&gt;associative&lt;&#x2F;em&gt; and &lt;em&gt;commutative&lt;&#x2F;em&gt; property of &lt;em&gt;addition&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;associative-property&quot;&gt;Associative property&lt;a class=&quot;zola-anchor&quot; href=&quot;#associative-property&quot; aria-label=&quot;Anchor link for: associative-property&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h2&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;Associative property of addition states that, &lt;em&gt;no matter how a set of three or more numbers are grouped together, the sum remains the same&lt;&#x2F;em&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;For example, if &lt;em&gt;a, b and c&lt;&#x2F;em&gt; are natural numbers then &lt;strong&gt;a + (b + c) = (a + b) + c&lt;&#x2F;strong&gt;. Let’s apply some natural numbers to &lt;em&gt;a, b and c&lt;&#x2F;em&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Let &lt;strong&gt;a = 10, b = 20 and c = 30,&lt;&#x2F;strong&gt; therefore:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;10 + (20 + 30) = (10 + 20) + 30&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;10 + 50 = 30 + 30&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;60 = 60&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This is associative property in a nutshell. In the above example, we’ve only taken three numbers &lt;em&gt;a, b and c&lt;&#x2F;em&gt; but it can be more as well.&lt;&#x2F;p&gt;
&lt;p&gt;Okay, now that we know the associative property of addition, let’s prove that using Coq.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;coq&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;(*&lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt; Prove associative proerty of addition &lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt;*)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;Theorem&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; proving_add_associative&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; forall&lt;&#x2F;span&gt;&lt;span&gt; a b c&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; nat&lt;&#x2F;span&gt;&lt;span&gt;, a &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;+&lt;&#x2F;span&gt;&lt;span&gt; (b &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;+&lt;&#x2F;span&gt;&lt;span&gt; c) &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; (a &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;+&lt;&#x2F;span&gt;&lt;span&gt; b) &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;+&lt;&#x2F;span&gt;&lt;span&gt; c.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	Proof&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support&quot;&gt;	intros&lt;&#x2F;span&gt;&lt;span&gt; a b c.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support&quot;&gt;	induction&lt;&#x2F;span&gt;&lt;span&gt; a &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;as&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;|&lt;&#x2F;span&gt;&lt;span&gt; a&amp;#39; IHa&amp;#39;].&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	-&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; simpl&lt;&#x2F;span&gt;&lt;span&gt;. &lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt;reflexivity&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	-&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; simpl&lt;&#x2F;span&gt;&lt;span&gt;. &lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt;rewrite&lt;&#x2F;span&gt;&lt;span&gt; IHa&amp;#39;. &lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt;reflexivity&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	Qed&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The above theorem states that, &lt;em&gt;for all natural numbers a, b and c, a + (b + c) is equal to (a + b) + c.&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Between the &lt;code&gt;Proof&lt;&#x2F;code&gt; and &lt;code&gt;Qed&lt;&#x2F;code&gt; tactic is where we write our proof. Since we have three variables &lt;em&gt;a, b and c,&lt;&#x2F;em&gt; we have to bring them into our proof. To do that, we can use the &lt;code&gt;intros&lt;&#x2F;code&gt; tactic which is short for &lt;em&gt;introduction or introduce&lt;&#x2F;em&gt;.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;induction-1&quot;&gt;induction&lt;a class=&quot;zola-anchor&quot; href=&quot;#induction-1&quot; aria-label=&quot;Anchor link for: induction-1&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h3&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;The line &lt;code&gt;induction a as [| a&#x27; IHa&#x27;].&lt;&#x2F;code&gt; does induction on variable &lt;em&gt;a&lt;&#x2F;em&gt;. When &lt;em&gt;induction&lt;&#x2F;em&gt; is applied on &lt;em&gt;a,&lt;&#x2F;em&gt; we have two cases to prove.&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;When a = 0&lt;&#x2F;li&gt;
&lt;li&gt;When a = k, where &lt;em&gt;k&lt;&#x2F;em&gt; is an arbitrary number.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;In the above command, we have two things mentioned inside the square brackets( &lt;code&gt;[]&lt;&#x2F;code&gt; ). These are the names of the &lt;em&gt;variables&lt;&#x2F;em&gt; we assigned as part of induction. Our base case is &lt;strong&gt;a = 0&lt;&#x2F;strong&gt;, so we don’t have to assign&#x2F;create any variable for that. For our second case, &lt;strong&gt;a = k&lt;&#x2F;strong&gt; we have to create a variable to hold the arbitrary number, for that we have created a variable named &lt;strong&gt;a’&lt;&#x2F;strong&gt;.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;what-is-iha&quot;&gt;What is &lt;strong&gt;IHa’&lt;&#x2F;strong&gt;?&lt;a class=&quot;zola-anchor&quot; href=&quot;#what-is-iha&quot; aria-label=&quot;Anchor link for: what-is-iha&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h3&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;&lt;strong&gt;IH&lt;&#x2F;strong&gt; is short for &lt;strong&gt;Induction Hypothesis&lt;&#x2F;strong&gt;. And &lt;strong&gt;IHa’,&lt;&#x2F;strong&gt; &lt;em&gt;a’&lt;&#x2F;em&gt; is added in the name because this hypothesis is for &lt;em&gt;a’&lt;&#x2F;em&gt;. So for readability we have named it as &lt;strong&gt;IHa’&lt;&#x2F;strong&gt;. You can name the variables whatever you want. I could’ve done like this as well &lt;code&gt;[| k&#x27; IHk&#x27;]&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;going-further&quot;&gt;Going further&lt;a class=&quot;zola-anchor&quot; href=&quot;#going-further&quot; aria-label=&quot;Anchor link for: going-further&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h3&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;Now, let’s see what is the state of our theorem when we execute the &lt;code&gt;intros.&lt;&#x2F;code&gt; tactic and &lt;code&gt;induction&lt;&#x2F;code&gt; tactic.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;mataroa.blog&#x2F;images&#x2F;2a73d35c.png&quot; alt=&quot;Screenshot_2024-03-28_at_7-44-07_PM&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;As you can see, after applying &lt;em&gt;proof by induction&lt;&#x2F;em&gt; we have two goals.&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;When &lt;strong&gt;a = 0&lt;&#x2F;strong&gt;&lt;&#x2F;li&gt;
&lt;li&gt;When &lt;strong&gt;a = S a’&lt;&#x2F;strong&gt;, where &lt;em&gt;S a’&lt;&#x2F;em&gt; is an arbitrary number.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;Now, lets solve the first goal. We can focus on the goal using &lt;code&gt;-&lt;&#x2F;code&gt; sign.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;mataroa.blog&#x2F;images&#x2F;0b7e724d.png&quot; alt=&quot;Screenshot_2024-03-28_at_9-02-11_PM&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;We know that &lt;em&gt;0 plus anything equals anything&lt;&#x2F;em&gt;. In this case, on the left hand side we have &lt;strong&gt;0 + (b + c)&lt;&#x2F;strong&gt; and on the right hand side we have &lt;strong&gt;0 + b + c&lt;&#x2F;strong&gt;. So, we can simplify the LHS and RHS like &lt;strong&gt;b + c = b + c&lt;&#x2F;strong&gt; using the &lt;code&gt;simpl.&lt;&#x2F;code&gt; tactic, which gives us&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;mataroa.blog&#x2F;images&#x2F;1b1e70d3.png&quot; alt=&quot;Screenshot_2024-03-28_at_9-04-18_PM&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Now we can use &lt;code&gt;reflexivity.&lt;&#x2F;code&gt; tactic to prove the equality. Since both LHS and RHS are equal, executing the &lt;em&gt;reflexivity&lt;&#x2F;em&gt; tactic would solve our first goal.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;mataroa.blog&#x2F;images&#x2F;4034b7d7.png&quot; alt=&quot;Screenshot_2024-03-28_at_9-05-28_PM&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Let’s focus on our second goal using the &lt;code&gt;-&lt;&#x2F;code&gt; .&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;mataroa.blog&#x2F;images&#x2F;dbf7f9cf.png&quot; alt=&quot;Screenshot_2024-03-28_at_9-06-00_PM&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;In our second goal, we have to prove &lt;strong&gt;S a&#x27; + (b + c) = S a&#x27; + b + c&lt;&#x2F;strong&gt;. We also got an induction hypothesis(IHa&#x27;), which states that &lt;strong&gt;a&#x27; + (b + c) = a&#x27; + b + c&lt;&#x2F;strong&gt;. Here, we’ve assumed that this hypothesis is &lt;em&gt;&lt;strong&gt;true,&lt;&#x2F;strong&gt;&lt;&#x2F;em&gt; so that we can use this hypothesis to prove our goal.&lt;&#x2F;p&gt;
&lt;p&gt;Let’s use the &lt;code&gt;simpl.&lt;&#x2F;code&gt; tactic to simplify both LHS and RHS.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;mataroa.blog&#x2F;images&#x2F;7a92ee2e.png&quot; alt=&quot;Screenshot_2024-03-28_at_9-09-37_PM&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Executing the &lt;code&gt;simpl.&lt;&#x2F;code&gt; tactic grouped our variables. If you look at LHS, we have &lt;strong&gt;a&#x27; + (b + c)&lt;&#x2F;strong&gt;. In our induction hypothesis we have assumed that &lt;strong&gt;a&#x27; + (b + c) = a&#x27; + b + c&lt;&#x2F;strong&gt; is &lt;em&gt;true&lt;&#x2F;em&gt;. Therefore, we can use this hypothesis to prove our goal.&lt;&#x2F;p&gt;
&lt;p&gt;Let’s rewrite our statement using the induction hypothesis. We can use the &lt;code&gt;rewrite.&lt;&#x2F;code&gt; tactic to do this. Executing &lt;code&gt;rewrite IHa&#x27;&lt;&#x2F;code&gt; results in&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;mataroa.blog&#x2F;images&#x2F;8df62d3f.png&quot; alt=&quot;Screenshot_2024-03-28_at_9-13-48_PM&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;As you can see, we’ve replaced(rewrote) &lt;strong&gt;a&#x27; + (b + c)&lt;&#x2F;strong&gt; with &lt;strong&gt;a&#x27; + b + c,&lt;&#x2F;strong&gt; because we have assumed &lt;strong&gt;a&#x27; + (b + c) = a&#x27; + b + c&lt;&#x2F;strong&gt; to be &lt;em&gt;true&lt;&#x2F;em&gt;. Now if you look at both LHS and RHS, its equal. We can check the equality property using the &lt;code&gt;reflexivity.&lt;&#x2F;code&gt; command.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;mataroa.blog&#x2F;images&#x2F;c1b11477.png&quot; alt=&quot;Screenshot_2024-03-28_at_9-16-31_PM&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Boom! we’ve proved the &lt;em&gt;associative property&lt;&#x2F;em&gt; of &lt;em&gt;addition&lt;&#x2F;em&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;commutative-property&quot;&gt;Commutative property&lt;a class=&quot;zola-anchor&quot; href=&quot;#commutative-property&quot; aria-label=&quot;Anchor link for: commutative-property&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h2&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;I’m too lazy to add the images to explain each and every step, so I&#x27;m planning to make a video explaining things. Will be uploading soon once the blog goes live!&lt;&#x2F;p&gt;
&lt;h2 id=&quot;references&quot;&gt;References&lt;a class=&quot;zola-anchor&quot; href=&quot;#references&quot; aria-label=&quot;Anchor link for: references&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h2&gt;
&lt;hr &#x2F;&gt;
&lt;ul&gt;
&lt;li&gt;Induction: &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Mathematical_induction&quot;&gt;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Mathematical_induction&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;Associative property: &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Associative_property&quot;&gt;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Associative_property&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;Commutative property: &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Commutative_property&quot;&gt;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Commutative_property&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Modelling and Proving logic gates in Coq</title>
		<published>2024-03-23T00:00:00+00:00</published>
		<updated>2024-03-23T00:00:00+00:00</updated>
		<link rel="alternate" type="text/html" href="https://fazeneo.in/posts/modelling-and-proving-logic-gates-in-coq/"/>
		<id>https://fazeneo.in/posts/modelling-and-proving-logic-gates-in-coq/</id>
    
		<content type="html" xml:base="https://fazeneo.in/posts/modelling-and-proving-logic-gates-in-coq/">&lt;p&gt;In this post, we’ll explore how to model primitive logic gates such as AND, OR and NOT in Coq. Furthermore, we’ll take an additional step by proving their properties. First of all what is Coq?&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;DISCLAIMER:&lt;&#x2F;strong&gt; I’m not an expect in Coq. I’m learning it out of curiosity. So whatever I’m saying in this post, take it with a grain of salt.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;coq&quot;&gt;Coq&lt;a class=&quot;zola-anchor&quot; href=&quot;#coq&quot; aria-label=&quot;Anchor link for: coq&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h2&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;Coq is a functional programming language which is also a proof assistant used for formal verification and theorem proving. It provides a formal language for expressing mathematical statements and proofs.&lt;&#x2F;p&gt;
&lt;p&gt;I’m not gonna go into Coq as there are many resources available online if you want to learn. I’m gonna only focus on modelling the logic gates and proving their properties. I highly recommend you to follow along by writing code. Use the &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https:&#x2F;&#x2F;coq.vercel.app&#x2F;scratchpad.html&quot;&gt;Coq online interpreter&lt;&#x2F;a&gt; to write code. We’ll be doing two things.&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;Model all the three primitive logic gates(AND, OR and NOT)&lt;&#x2F;li&gt;
&lt;li&gt;Prove the properties of the primitive gates we modelled.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;First, let’s focus on modelling them.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;and-gate&quot;&gt;And gate&lt;a class=&quot;zola-anchor&quot; href=&quot;#and-gate&quot; aria-label=&quot;Anchor link for: and-gate&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h2&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;Let’s first look at the truth table of AND gate.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;| A | B | OUT |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;|---|---|-----|&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;| 0 | 0 | 0   |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;| 0 | 1 | 0   |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;| 1 | 0 | 0   |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;| 1 | 1 | 1   |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;By looking at the above truth table, we can tell that:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;If both the inputs are true, then the output is true.&lt;&#x2F;li&gt;
&lt;li&gt;If any one of the input is false then the output is false.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;Now, let’s model it in Coq.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;coq&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;Definition&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; AND&lt;&#x2F;span&gt;&lt;span&gt; (a&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; bool&lt;&#x2F;span&gt;&lt;span&gt;) (b&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; bool&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; bool&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; :&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	match&lt;&#x2F;span&gt;&lt;span&gt; a &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;with&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	|&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; true&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; b&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	|&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; false&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; false&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	end&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In the above code, we created a &lt;strong&gt;Definition,&lt;&#x2F;strong&gt; which in imperative programming languages we call it &lt;em&gt;functions&lt;&#x2F;em&gt;. The name of the definition is &lt;strong&gt;AND&lt;&#x2F;strong&gt;, which takes two arguments of type boolean. We are doing pattern matching expression in the body of the definition.&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;em&gt;a&lt;&#x2F;em&gt; is the term being matched.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;em&gt;true&lt;&#x2F;em&gt; and &lt;em&gt;false&lt;&#x2F;em&gt; are the patterns being matched against &lt;em&gt;a&lt;&#x2F;em&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;em&gt;b&lt;&#x2F;em&gt; is the expression to be evaluated if &lt;em&gt;a&lt;&#x2F;em&gt; matches &lt;em&gt;true&lt;&#x2F;em&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;em&gt;false&lt;&#x2F;em&gt; is the expression to be evaluated if &lt;em&gt;a&lt;&#x2F;em&gt; matches &lt;em&gt;false&lt;&#x2F;em&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;By using the &lt;code&gt;match&lt;&#x2F;code&gt; construct, we we’re able to pattern match the expression and model the AND gate.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;or-gate&quot;&gt;OR gate&lt;a class=&quot;zola-anchor&quot; href=&quot;#or-gate&quot; aria-label=&quot;Anchor link for: or-gate&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h2&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;Let’s first look at the truth table of OR gate.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;| A | B | OUT |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;|---|---|-----|&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;| 0 | 0 | 0   |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;| 0 | 1 | 1   |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;| 1 | 0 | 1   |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;| 1 | 1 | 1   |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;By looking at the above truth table, we can tell that&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;If both the inputs are false, then the output is false&lt;&#x2F;li&gt;
&lt;li&gt;If any one of the input is true, then the output is true.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;Now, let’s model it in Coq.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;coq&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;Definition&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; OR&lt;&#x2F;span&gt;&lt;span&gt; (a&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; bool&lt;&#x2F;span&gt;&lt;span&gt;) (b&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; bool&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; bool&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; :&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;=&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	match&lt;&#x2F;span&gt;&lt;span&gt; a &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;with&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	|&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; true&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; true&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	|&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; false&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; b&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	end&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The name of the definition is &lt;strong&gt;OR&lt;&#x2F;strong&gt;, which takes two arguments of type boolean.&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;em&gt;a&lt;&#x2F;em&gt; is the term being matched.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;em&gt;true&lt;&#x2F;em&gt; and &lt;em&gt;false&lt;&#x2F;em&gt; are the pattern being matched against.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;em&gt;b&lt;&#x2F;em&gt; is the expression to be evaluated if &lt;em&gt;a&lt;&#x2F;em&gt; matches &lt;em&gt;false&lt;&#x2F;em&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;em&gt;true&lt;&#x2F;em&gt; is the expression to be evaluated if &lt;em&gt;a&lt;&#x2F;em&gt; matches &lt;em&gt;true&lt;&#x2F;em&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;h2 id=&quot;not-gate&quot;&gt;NOT gate&lt;a class=&quot;zola-anchor&quot; href=&quot;#not-gate&quot; aria-label=&quot;Anchor link for: not-gate&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h2&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;Let’s first look at the truth table of NOT gate&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;| A | OUT |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;|---|-----|&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;| 0 | 1   |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;| 1 | 0   |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;By looking at the above truth table, we can tell that&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;If the input is 0(&lt;em&gt;false&lt;&#x2F;em&gt;), then the output is 1(&lt;em&gt;true&lt;&#x2F;em&gt;)&lt;&#x2F;li&gt;
&lt;li&gt;If the input is 1(&lt;em&gt;true&lt;&#x2F;em&gt;), then the output is 0(&lt;em&gt;false&lt;&#x2F;em&gt;)&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;Now, let’s model it in Coq.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;coq&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;Definition&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; NOT&lt;&#x2F;span&gt;&lt;span&gt; (a&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; bool&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; bool&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; :&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;=&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	match&lt;&#x2F;span&gt;&lt;span&gt; a &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;with&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	|&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; true&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; false&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	|&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; false&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; true&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	end&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The name of the definition is &lt;strong&gt;NOT,&lt;&#x2F;strong&gt; which takes in one argument of type boolean.&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;em&gt;a&lt;&#x2F;em&gt; is the term being matched.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;em&gt;true&lt;&#x2F;em&gt; and &lt;em&gt;false&lt;&#x2F;em&gt; are the pattern being matched against&lt;&#x2F;li&gt;
&lt;li&gt;&lt;em&gt;false&lt;&#x2F;em&gt; is the expression to be evaluated if &lt;em&gt;a&lt;&#x2F;em&gt; matched &lt;em&gt;true&lt;&#x2F;em&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;em&gt;true&lt;&#x2F;em&gt; is the expression to be evaluated if &lt;em&gt;a&lt;&#x2F;em&gt; matched &lt;em&gt;false&lt;&#x2F;em&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;Now that we’ve modelled the primitive gates, let’s prove their properties. In simple terms, think of it like a test case that we would write to test some functionality. In this case, we’ve modelled primitive gates like a function that would return something based on the input. So, we would be proving if the function we’ve created works as expected or not.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;proving-not-gate-property&quot;&gt;Proving NOT gate property&lt;a class=&quot;zola-anchor&quot; href=&quot;#proving-not-gate-property&quot; aria-label=&quot;Anchor link for: proving-not-gate-property&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h2&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;Let’s first prove NOT gate, because we only have to prove couple of cases. In Coq, we’ll use the &lt;strong&gt;Theorem&lt;&#x2F;strong&gt; command to prove mathematical theorem or properties. &lt;strong&gt;Theorem&lt;&#x2F;strong&gt; is used to declare propositions that we want to prove formally. The thing that we want to prove here is that the property of the &lt;strong&gt;NOT&lt;&#x2F;strong&gt; definition.&lt;&#x2F;p&gt;
&lt;p&gt;There are two cases we must prove:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;true = false&lt;&#x2F;li&gt;
&lt;li&gt;false = true&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;Let’s just look at the theorem.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;coq&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;Theorem&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; proving_not&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; forall&lt;&#x2F;span&gt;&lt;span&gt; (a&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; bool&lt;&#x2F;span&gt;&lt;span&gt;), NOT (NOT a) &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; a.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support&quot;&gt;	intros&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support&quot;&gt;	destruct&lt;&#x2F;span&gt;&lt;span&gt; a.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support&quot;&gt;	simpl&lt;&#x2F;span&gt;&lt;span&gt;; &lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt;reflexivity&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support&quot;&gt;	simpl&lt;&#x2F;span&gt;&lt;span&gt;; &lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt;reflexivity&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;Qed&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;There are a lot of thing to breakdown in the above code. Let’s do it one by one.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;theorem&quot;&gt;Theorem&lt;a class=&quot;zola-anchor&quot; href=&quot;#theorem&quot; aria-label=&quot;Anchor link for: theorem&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h3&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;Theorem’s start with the &lt;strong&gt;Theorem&lt;&#x2F;strong&gt; keyword, followed by the name of the theorem. In this case, the name of our theorem is &lt;strong&gt;proving_not&lt;&#x2F;strong&gt;. So the syntax would be,&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;Theorem &amp;lt;name_of_the_theorem&amp;gt;:&lt;&#x2F;code&gt; , followed by what we are trying to prove. In this case, we are trying to prove &lt;em&gt;&lt;strong&gt;for all “a”(which is a boolean type), NOT (NOT a) = a,&lt;&#x2F;strong&gt;&lt;&#x2F;em&gt; which means, if we expand the &lt;strong&gt;NOT (NOT a)&lt;&#x2F;strong&gt; definition by replacing &lt;em&gt;a&lt;&#x2F;em&gt; with &lt;em&gt;true&lt;&#x2F;em&gt; or &lt;em&gt;false&lt;&#x2F;em&gt;, the result should be equal to &lt;em&gt;a&lt;&#x2F;em&gt;. For example,&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;a = true, so&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;NOT (NOT a) = a =&amp;gt; NOT (NOT true)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;			    =&amp;gt; NOT false&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;		        =&amp;gt; true&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;a = false, so&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;NOT (NOT a) = a =&amp;gt; NOT (NOT false)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;			    =&amp;gt; NOT true&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;			    =&amp;gt; false					&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;When we execute &lt;code&gt;Theorem proving_not: forall (a: bool), NOT (NOT a) = a.&lt;&#x2F;code&gt; , this is what Coq creates for us to prove. It creates a &lt;em&gt;&lt;strong&gt;goal&lt;&#x2F;strong&gt;&lt;&#x2F;em&gt;, which we should prove. The &lt;em&gt;goal&lt;&#x2F;em&gt; is nothing but, what we stated in our &lt;em&gt;Theorem&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;mataroa.blog&#x2F;images&#x2F;a104bd0b.png&quot; alt=&quot;Screenshot_2024-03-17_at_8-56-54_PM&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;intros&quot;&gt;intros&lt;a class=&quot;zola-anchor&quot; href=&quot;#intros&quot; aria-label=&quot;Anchor link for: intros&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h3&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;&lt;code&gt;intros&lt;&#x2F;code&gt; is a keyword that is part of &lt;em&gt;&lt;strong&gt;tactics&lt;&#x2F;strong&gt;&lt;&#x2F;em&gt;. What are &lt;em&gt;tactics&lt;&#x2F;em&gt;? Tactics are commands or procedures used to manipulate and reason about proofs within the proof environment.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;intros&lt;&#x2F;code&gt; is part of the &lt;em&gt;Introduction tactics&lt;&#x2F;em&gt;. It is used to introduce variables into the proof context. In our case, we have only one variable which is &lt;em&gt;&lt;strong&gt;a&lt;&#x2F;strong&gt;&lt;&#x2F;em&gt;. So it brings &lt;em&gt;a&lt;&#x2F;em&gt; into the proof context so that we can manipulate it and prove our theorem.&lt;&#x2F;p&gt;
&lt;p&gt;When &lt;code&gt;intros.&lt;&#x2F;code&gt; gets executed, it brings &lt;em&gt;a&lt;&#x2F;em&gt; into the context. This is what happens when it gets executed.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;mataroa.blog&#x2F;images&#x2F;43de8023.png&quot; alt=&quot;Screenshot_2024-03-17_at_8-58-21_PM&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;You can see a horizontal line, the things above the line contains variables, hypothesis, etc in the context that we use to manipulate to solve the proof. As you can see here, we have &lt;code&gt;a: bool&lt;&#x2F;code&gt; which is the only variable that belongs in our &lt;em&gt;Theorem&lt;&#x2F;em&gt;. The things below the horizontal line is when we need to prove to achieve our goal.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;destruct&quot;&gt;destruct&lt;a class=&quot;zola-anchor&quot; href=&quot;#destruct&quot; aria-label=&quot;Anchor link for: destruct&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h3&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;&lt;code&gt;destruct&lt;&#x2F;code&gt; keyword is used to perform case analysis on an &lt;em&gt;inductive type&lt;&#x2F;em&gt;, breaking down the proofs into separate cases based on the possible constructors of the type. It is commonly used to reason about values of an inductive type by considering all possible cases individually.&lt;&#x2F;p&gt;
&lt;p&gt;When we execute &lt;code&gt;destruct a.&lt;&#x2F;code&gt; , it creates &lt;em&gt;two subgoals&lt;&#x2F;em&gt; for us to prove.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;mataroa.blog&#x2F;images&#x2F;3b6a53ad.png&quot; alt=&quot;Screenshot_2024-03-17_at_9-09-31_PM&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;As you can see from the above picture, the &lt;code&gt;destruct&lt;&#x2F;code&gt; tactic breaks down our initial goal into two subgoals because we have to solve for two different cases which is &lt;em&gt;true&lt;&#x2F;em&gt; and &lt;em&gt;false&lt;&#x2F;em&gt;. Now we have to solve these two goals. First we’ll solve &lt;code&gt;NOT (NOT true) = true&lt;&#x2F;code&gt; because that is what is currently in the context.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;simpl&quot;&gt;simpl&lt;a class=&quot;zola-anchor&quot; href=&quot;#simpl&quot; aria-label=&quot;Anchor link for: simpl&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h3&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;&lt;code&gt;simpl&lt;&#x2F;code&gt; keyword is short for &lt;em&gt;simplify&lt;&#x2F;em&gt;. We use this tactic to simplify the expression in the proof context by performing computation and reduction according to the definitions of the involved terms. For example, In our case we have an expression &lt;code&gt;NOT (NOT true)&lt;&#x2F;code&gt; . What &lt;code&gt;simpl&lt;&#x2F;code&gt; does is reduce the expression recursively until it cannot be reduced further. If we reduce this expression:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;NOT (NOT true) =&amp;gt; NOT (false) =&amp;gt; true&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If we run the &lt;code&gt;simpl.&lt;&#x2F;code&gt; command this is what we get:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;mataroa.blog&#x2F;images&#x2F;7dd25930.png&quot; alt=&quot;Screenshot_2024-03-17_at_9-17-14_PM&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;As you can see, it reduced the expression &lt;code&gt;NOT (NOT true)&lt;&#x2F;code&gt; to &lt;code&gt;true&lt;&#x2F;code&gt; . Now that we have &lt;code&gt;true = true&lt;&#x2F;code&gt; we have to check it. For that we have to use one more tactic.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;reflexivity&quot;&gt;reflexivity&lt;a class=&quot;zola-anchor&quot; href=&quot;#reflexivity&quot; aria-label=&quot;Anchor link for: reflexivity&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h3&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;The &lt;code&gt;reflexivity&lt;&#x2F;code&gt; tactic is used to prove goals that assert equality between two terms or expressions. It asserts that any term is equal to itself, which is known as the reflexivity property of equality. When we execute &lt;code&gt;reflexivity.&lt;&#x2F;code&gt; , it checks if the LHS is equal to the RHS syntactically. If yes, we have proved the goal, otherwise we didn’t. Let’s see.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;mataroa.blog&#x2F;images&#x2F;d3358725.png&quot; alt=&quot;Screenshot_2024-03-17_at_9-24-17_PM&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;As you see from the above screenshot, now we have only one goal to prove. This is because we have proved our first goal ie. &lt;code&gt;true = true&lt;&#x2F;code&gt;. Now let’s prove our second goal, which is &lt;code&gt;NOT (NOT false) = false&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Given the current knowledge, how would you prove it? Its exactly the same as how we solved our first goal. We’ll first &lt;em&gt;simplify&lt;&#x2F;em&gt; the expression and then check the &lt;em&gt;equality&lt;&#x2F;em&gt;. To simplify the expression we’ll use &lt;code&gt;simpl.&lt;&#x2F;code&gt; , once its simplified we’ll use &lt;code&gt;reflexivity.&lt;&#x2F;code&gt; to check the equality. Once we’ve executed these two tactics, this is what we get.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;mataroa.blog&#x2F;images&#x2F;f088a6fc.png&quot; alt=&quot;Screenshot_2024-03-17_at_9-27-47_PM&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Congratulations, you’ve solved your first theorem. As you can see, there are &lt;em&gt;no more goals&lt;&#x2F;em&gt; to prove. Which means we’ve successfully solved&#x2F;proved the property of &lt;strong&gt;NOT gate&lt;&#x2F;strong&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;proving-and-gate-property&quot;&gt;Proving AND gate property&lt;a class=&quot;zola-anchor&quot; href=&quot;#proving-and-gate-property&quot; aria-label=&quot;Anchor link for: proving-and-gate-property&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h2&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;Let’s first look at the theorem and break it down step by step.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;coq&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;Theorem&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; and_property&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; forall&lt;&#x2F;span&gt;&lt;span&gt; (b c&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; bool&lt;&#x2F;span&gt;&lt;span&gt;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	(AND b c) &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; (AND c b).&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	Proof&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support&quot;&gt;	intros&lt;&#x2F;span&gt;&lt;span&gt; b c.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support&quot;&gt;	destruct&lt;&#x2F;span&gt;&lt;span&gt; b.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	-&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; destruct&lt;&#x2F;span&gt;&lt;span&gt; c.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;		+&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; simpl&lt;&#x2F;span&gt;&lt;span&gt;. &lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt;reflexivity&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;		+&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; simpl&lt;&#x2F;span&gt;&lt;span&gt;. &lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt;reflexivity&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	-&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; destruct&lt;&#x2F;span&gt;&lt;span&gt; c.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;		+&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; simpl&lt;&#x2F;span&gt;&lt;span&gt;. &lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt;reflexivity&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;		+&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; simpl&lt;&#x2F;span&gt;&lt;span&gt;. &lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt;reflexivity&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;Qed&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;What does the above theorem asks us to prove? It says that, for all &lt;em&gt;b&lt;&#x2F;em&gt; and &lt;em&gt;c&lt;&#x2F;em&gt; which is of type &lt;em&gt;boolean&lt;&#x2F;em&gt;, prove &lt;em&gt;(AND b c) = (AND c b)&lt;&#x2F;em&gt;. What is &lt;strong&gt;AND&lt;&#x2F;strong&gt;? You can think of &lt;strong&gt;AND&lt;&#x2F;strong&gt; as a definition or function which takes in two arguments of type boolean and returns a boolean. Basically it does logical &lt;em&gt;AND&lt;&#x2F;em&gt; operation on the arguments and returns the result.&lt;&#x2F;p&gt;
&lt;p&gt;In simple terms, this is what we have to prove:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;true = true&lt;&#x2F;li&gt;
&lt;li&gt;false = false&lt;&#x2F;li&gt;
&lt;li&gt;false = false&lt;&#x2F;li&gt;
&lt;li&gt;false = false&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;Now, let’s breakdown our proof step by step:&lt;&#x2F;p&gt;
&lt;p&gt;What’s the goal that we have to prove?&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;mataroa.blog&#x2F;images&#x2F;5ca48d94.png&quot; alt=&quot;Screenshot_2024-03-22_at_9-14-56_PM&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Currently we have one goal, as we prove, we’ll break it down into subgoals and prove it step by step. As always, the first step is to bring the variables into context. Here the variables are &lt;em&gt;b&lt;&#x2F;em&gt; and &lt;em&gt;c&lt;&#x2F;em&gt;. We know what tactic to use to introduce variables into the context.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;intros-1&quot;&gt;intros&lt;a class=&quot;zola-anchor&quot; href=&quot;#intros-1&quot; aria-label=&quot;Anchor link for: intros-1&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h3&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;The line &lt;code&gt;intros b c&lt;&#x2F;code&gt; in our proof, brings the &lt;em&gt;b&lt;&#x2F;em&gt; and &lt;em&gt;c&lt;&#x2F;em&gt; variables into the context so that we can use them in subsequent tactics. This is what happens when that tactic gets executed.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;mataroa.blog&#x2F;images&#x2F;1f2f8613.png&quot; alt=&quot;Screenshot_2024-03-22_at_9-37-33_PM&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;destruct-1&quot;&gt;destruct&lt;a class=&quot;zola-anchor&quot; href=&quot;#destruct-1&quot; aria-label=&quot;Anchor link for: destruct-1&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h3&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;We use &lt;code&gt;destruct&lt;&#x2F;code&gt; to breakdown our goals into small chunks. The line &lt;code&gt;destruct b&lt;&#x2F;code&gt; replaces &lt;em&gt;b&lt;&#x2F;em&gt; with &lt;em&gt;true&lt;&#x2F;em&gt; and &lt;em&gt;false&lt;&#x2F;em&gt; because that’s the type of &lt;em&gt;b&lt;&#x2F;em&gt;. And it breaks it into individual goals. This is how it looks once we execute &lt;code&gt;destruct b&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;mataroa.blog&#x2F;images&#x2F;1e21002a.png&quot; alt=&quot;Screenshot_2024-03-23_at_11-11-17_AM&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;As you can see, it replaces &lt;em&gt;b&lt;&#x2F;em&gt; with &lt;em&gt;true&lt;&#x2F;em&gt; and &lt;em&gt;false&lt;&#x2F;em&gt;. The first goal is &lt;em&gt;true&lt;&#x2F;em&gt; case and the second goal is for &lt;em&gt;false&lt;&#x2F;em&gt; case.&lt;&#x2F;p&gt;
&lt;p&gt;The next line &lt;code&gt;destruct c&lt;&#x2F;code&gt; does the same thing for &lt;em&gt;c,&lt;&#x2F;em&gt; it replaces &lt;em&gt;c&lt;&#x2F;em&gt; with &lt;em&gt;true&lt;&#x2F;em&gt; and &lt;em&gt;false&lt;&#x2F;em&gt;. But since we’ve already broken down the goals before, executing this command won’t break it down further. You might have noticed the &lt;strong&gt;-&lt;&#x2F;strong&gt; sign before the &lt;code&gt;destruct c&lt;&#x2F;code&gt; command, we’ll get to it shortly. This is how it looks once we execute &lt;code&gt;- destruct c&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;mataroa.blog&#x2F;images&#x2F;191e3557.png&quot; alt=&quot;Screenshot_2024-03-23_at_11-15-46_AM&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;As you can see, both &lt;em&gt;b&lt;&#x2F;em&gt; and &lt;em&gt;c&lt;&#x2F;em&gt; got replaced with &lt;em&gt;true&lt;&#x2F;em&gt; and &lt;em&gt;false&lt;&#x2F;em&gt;. Now we can able to prove it one by one.&lt;&#x2F;p&gt;
&lt;p&gt;The line &lt;code&gt;+ simpl. reflexivity.&lt;&#x2F;code&gt; , let’s break this down. What is &lt;code&gt;+&lt;&#x2F;code&gt; ? Just like &lt;code&gt;-&lt;&#x2F;code&gt; , plus(&lt;strong&gt;+&lt;&#x2F;strong&gt;) is used to focus on subgoals that we are gonna prove. Let’s what happens to the goal when we execute only &lt;code&gt;+&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;mataroa.blog&#x2F;images&#x2F;c8f14b0e.png&quot; alt=&quot;Screenshot_2024-03-23_at_10-02-49_PM&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;As you can see, previously we have two goals, now we have only one. Because &lt;code&gt;+&lt;&#x2F;code&gt; focuses on only one goal at a time. Now that we’ve got a goal to prove, let’s prove it.&lt;&#x2F;p&gt;
&lt;p&gt;From the original statement &lt;code&gt;+ simpl. reflexivity&lt;&#x2F;code&gt; we’ve seen what &lt;code&gt;+&lt;&#x2F;code&gt; does. Now let’s look at &lt;code&gt;simpl.&lt;&#x2F;code&gt; Let’s try and execute &lt;code&gt;simpl.&lt;&#x2F;code&gt; and see what happens.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;simpl-1&quot;&gt;simpl&lt;a class=&quot;zola-anchor&quot; href=&quot;#simpl-1&quot; aria-label=&quot;Anchor link for: simpl-1&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h3&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;mataroa.blog&#x2F;images&#x2F;433a1880.png&quot; alt=&quot;Screenshot_2024-03-23_at_10-05-08_PM&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;As the name suggests, it simplifies our statement&#x2F;condition. Now we have &lt;strong&gt;true = true&lt;&#x2F;strong&gt;, which we know is equal. Can you guess what is the &lt;em&gt;tactic&lt;&#x2F;em&gt; we use to check equality?? You guessed it right.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;reflexivity-1&quot;&gt;reflexivity&lt;a class=&quot;zola-anchor&quot; href=&quot;#reflexivity-1&quot; aria-label=&quot;Anchor link for: reflexivity-1&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h3&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;Let’s execute &lt;code&gt;reflexivity.&lt;&#x2F;code&gt; and see what happens.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;mataroa.blog&#x2F;images&#x2F;d12a480d.png&quot; alt=&quot;Screenshot_2024-03-23_at_10-07-54_PM&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Reflexivity checks for equality. If LHS and RHS are equal, its success. Since we’ve proved our subgoal, Coq says we are done with our subgoal and we should be focusing on our next subgoal. To do that, we need to use the &lt;code&gt;+&lt;&#x2F;code&gt; symbol. Just like the previous subgoal we can use the same set of commands to solve the next subgoal. But let’s first see what’s our subgoal is.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;mataroa.blog&#x2F;images&#x2F;40ffe207.png&quot; alt=&quot;Screenshot_2024-03-23_at_10-10-30_PM&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Now that we know how to simplify the goal and check for equality let’s do that. Let’s execute &lt;code&gt;simpl. reflexivity&lt;&#x2F;code&gt; . Which first simplifies the statement and checks for equality.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;mataroa.blog&#x2F;images&#x2F;6dd5936e.png&quot; alt=&quot;Screenshot_2024-03-23_at_10-11-55_PM&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;mataroa.blog&#x2F;images&#x2F;177cf967.png&quot; alt=&quot;Screenshot_2024-03-23_at_10-12-08_PM&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;It says, we’ve completed our subproof and asking us to focus on our goals. To do that, we should use &lt;code&gt;-&lt;&#x2F;code&gt;. But before that let’s recap what we’ve proved in our first two subgoals. So far, we’ve proved — &lt;strong&gt;true = true&lt;&#x2F;strong&gt; and &lt;strong&gt;false = false&lt;&#x2F;strong&gt;. We’ve couple more cases to prove, which are — &lt;strong&gt;false = false&lt;&#x2F;strong&gt; and &lt;strong&gt;false = false&lt;&#x2F;strong&gt;. That is what our next goal is gonna be.&lt;&#x2F;p&gt;
&lt;p&gt;Let’s focus on our next goal by executing &lt;code&gt;-&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;mataroa.blog&#x2F;images&#x2F;9aa9a844.png&quot; alt=&quot;Screenshot_2024-03-23_at_10-19-13_PM&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;We’ve to expand &lt;em&gt;c&lt;&#x2F;em&gt; now, we can use the &lt;code&gt;destruct&lt;&#x2F;code&gt; tactic to do that. Executing &lt;code&gt;destruct c.&lt;&#x2F;code&gt; would give us:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;mataroa.blog&#x2F;images&#x2F;bf163d39.png&quot; alt=&quot;Screenshot_2024-03-23_at_10-20-16_PM&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;We’ve got two subgoals. We’ve seen a similar pattern before, when we were proving for &lt;em&gt;b&lt;&#x2F;em&gt;. The same way we prove these two subgoals. Executing the below statements would solve the proof.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;coq&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;+&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; simpl&lt;&#x2F;span&gt;&lt;span&gt;. &lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt;reflexivity&lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt; (*&lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt; solves the 1st subgoal &lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt;*)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;+&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; simpl&lt;&#x2F;span&gt;&lt;span&gt;. &lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt;reflexivity&lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt; (*&lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt; solves the 2nd subgoal &lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt;*)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;mataroa.blog&#x2F;images&#x2F;c4d6d778.png&quot; alt=&quot;Screenshot_2024-03-23_at_10-22-23_PM&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Voila! We have proved our &lt;em&gt;theorem&lt;&#x2F;em&gt;. As you can see, we’ve no more goals which means we have proved &lt;strong&gt;(AND b c) = (AND c b)&lt;&#x2F;strong&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Armed with the basics of proving theorems, take on the challenge of proving (OR b c) = (OR c b). The steps mirror those we&#x27;ve used for solving (AND b c) = (AND c b). If you&#x27;ve grasped at least half of what I&#x27;ve shared, you&#x27;ve already achieved a great deal. Now, dive into theorem proving with confidence!&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Unlocking the Mystery of Coq&#x27;s Pattern Matching: The Journey Through minustwo</title>
		<published>2024-03-18T00:00:00+00:00</published>
		<updated>2024-03-18T00:00:00+00:00</updated>
		<link rel="alternate" type="text/html" href="https://fazeneo.in/posts/unlocking-the-mystery-of-coqs-pattern-matching-the-journey-through-minustwo/"/>
		<id>https://fazeneo.in/posts/unlocking-the-mystery-of-coqs-pattern-matching-the-journey-through-minustwo/</id>
    
		<content type="html" xml:base="https://fazeneo.in/posts/unlocking-the-mystery-of-coqs-pattern-matching-the-journey-through-minustwo/">&lt;p&gt;I was learning Coq for fun, hoping someday that I would prove some theorems(JK). While learning Coq, specifically &lt;em&gt;pattern matching&lt;&#x2F;em&gt;, I stumbled upon this definition named &lt;em&gt;&lt;strong&gt;minustwo&lt;&#x2F;strong&gt;&lt;&#x2F;em&gt;. As the name suggests, it subtracts 2 from the number you provide but with some cases. Let’s look at the definition to understand more.&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;DISCLAIMER:&lt;&#x2F;strong&gt; I’m not an expert in Coq, I’m just experimenting&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;coq&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;Definition&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; minustwo&lt;&#x2F;span&gt;&lt;span&gt; (n&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; nat&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; nat&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; :&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;=&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	match&lt;&#x2F;span&gt;&lt;span&gt; n &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;with&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	|&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; O&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; O&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	|&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; S&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; O&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; O&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	|&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; S&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;S&lt;&#x2F;span&gt;&lt;span&gt; m) &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; m&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	end&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Let’s breakdown the above code.&lt;&#x2F;p&gt;
&lt;p&gt;We have created a &lt;em&gt;definition&lt;&#x2F;em&gt; which in imperative languages we call them &lt;em&gt;functions&lt;&#x2F;em&gt;. The name of the definition is &lt;strong&gt;minustwo,&lt;&#x2F;strong&gt; which takes one argument &lt;em&gt;n&lt;&#x2F;em&gt; of type &lt;em&gt;nat&lt;&#x2F;em&gt; (short for Natural number).&lt;&#x2F;p&gt;
&lt;p&gt;The body of the definition contains a &lt;em&gt;match&lt;&#x2F;em&gt; statement. In Coq, we use &lt;em&gt;match&lt;&#x2F;em&gt; statement for &lt;em&gt;pattern matching&lt;&#x2F;em&gt;. You can think of &lt;em&gt;match&lt;&#x2F;em&gt; as &lt;em&gt;switch case&lt;&#x2F;em&gt; in other languages, but not exactly the same.&lt;&#x2F;p&gt;
&lt;p&gt;We have three cases:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;If &lt;em&gt;n&lt;&#x2F;em&gt; is &lt;em&gt;O&lt;&#x2F;em&gt;, return &lt;em&gt;O&lt;&#x2F;em&gt;&lt;&#x2F;li&gt;
&lt;li&gt;If &lt;em&gt;n&lt;&#x2F;em&gt; matches with &lt;em&gt;S O&lt;&#x2F;em&gt;, return &lt;em&gt;O&lt;&#x2F;em&gt;&lt;&#x2F;li&gt;
&lt;li&gt;If &lt;em&gt;n&lt;&#x2F;em&gt; matches with &lt;em&gt;S (S m)&lt;&#x2F;em&gt;, return &lt;em&gt;m&lt;&#x2F;em&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;&lt;em&gt;O&lt;&#x2F;em&gt; represents capital “O” and not numeral “0”. In Coq, both represents the same thing. We can replace “O” with “0” as well.&lt;&#x2F;p&gt;
&lt;p&gt;But, what is &lt;em&gt;&lt;code&gt;S O&lt;&#x2F;code&gt;&lt;&#x2F;em&gt;? What exactly is &lt;em&gt;S&lt;&#x2F;em&gt; ? &lt;em&gt;S&lt;&#x2F;em&gt; is short for &lt;strong&gt;Successor&lt;&#x2F;strong&gt; which is a &lt;em&gt;constructor&lt;&#x2F;em&gt; in Coq(in-built). We can look at the signature of &lt;em&gt;S&lt;&#x2F;em&gt; by executing this command: &lt;code&gt;Check S.&lt;&#x2F;code&gt; Which gives us:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;mataroa.blog&#x2F;images&#x2F;9980440e.png&quot; alt=&quot;Screenshot_2024-03-18_at_10-00-30_PM&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;This states that &lt;em&gt;S&lt;&#x2F;em&gt; takes in a natural number(nat) and returns a natural number(nat). So,&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;S O.&lt;&#x2F;code&gt; returns 1&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;S 1.&lt;&#x2F;code&gt; returns 2&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;S 2.&lt;&#x2F;code&gt; returns 3&lt;&#x2F;li&gt;
&lt;li&gt;and so on. We can check that by executing &lt;code&gt;Compute S n&lt;&#x2F;code&gt; , replace &lt;em&gt;n&lt;&#x2F;em&gt; which a natural number&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;mataroa.blog&#x2F;images&#x2F;9719e746.png&quot; alt=&quot;Screenshot_2024-03-18_at_10-02-32_PM&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Instead of doing &lt;em&gt;S O&lt;&#x2F;em&gt;, &lt;em&gt;S 1&lt;&#x2F;em&gt;, &lt;em&gt;S 2&lt;&#x2F;em&gt; and so on. We could also do something like:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;S (S O)&lt;&#x2F;code&gt; which returns 2&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;S (S (S O))&lt;&#x2F;code&gt; which returns 3&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;S (S (S (S O)))&lt;&#x2F;code&gt; which returns 4&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;S (S (S (S (S O))))&lt;&#x2F;code&gt; which returns 5&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Now that we know what &lt;em&gt;S&lt;&#x2F;em&gt; is, and what it does, let’s go back to original code.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;executing-minustwo&quot;&gt;Executing minustwo&lt;a class=&quot;zola-anchor&quot; href=&quot;#executing-minustwo&quot; aria-label=&quot;Anchor link for: executing-minustwo&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h2&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;How can we execute(call) &lt;em&gt;&lt;strong&gt;minustwo&lt;&#x2F;strong&gt;&lt;&#x2F;em&gt;? We can use the &lt;em&gt;&lt;strong&gt;Compute&lt;&#x2F;strong&gt;&lt;&#x2F;em&gt; vernacular which Coq provides. We can do something like:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;coq&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;Compute&lt;&#x2F;span&gt;&lt;span&gt; minustwo &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;. &lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt;(*&lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt; returns 0 &lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt;*)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;Compute&lt;&#x2F;span&gt;&lt;span&gt; minustwo &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;. &lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt;(*&lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt; returns 0 &lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt;*)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;Compute&lt;&#x2F;span&gt;&lt;span&gt; minustwo &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;. &lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt;(*&lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt; returns 0 &lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt;*)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;Compute&lt;&#x2F;span&gt;&lt;span&gt; minustwo &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;. &lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt;(*&lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt; returns 1 &lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt;*)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Let’s take a look at the first statement &lt;code&gt;Compute minustwo 0&lt;&#x2F;code&gt;. Essentially, we are passing &lt;code&gt;O&lt;&#x2F;code&gt; to &lt;em&gt;&lt;strong&gt;minustwo&lt;&#x2F;strong&gt;&lt;&#x2F;em&gt; definition.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;coq&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;match&lt;&#x2F;span&gt;&lt;span&gt; n &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;with&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	|&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; O&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; O&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	|&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; S&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; O&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; O&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	|&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; S&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;S&lt;&#x2F;span&gt;&lt;span&gt; m) &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; m&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;end&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;By looking at the body of the definition, we are handling the case for &lt;code&gt;O&lt;&#x2F;code&gt; , which is &lt;code&gt;O =&amp;gt; O&lt;&#x2F;code&gt;. That is why when we pass &lt;code&gt;O&lt;&#x2F;code&gt; it returns &lt;code&gt;O&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;For, &lt;code&gt;Compute minustwo 1&lt;&#x2F;code&gt; , we are passing &lt;code&gt;1&lt;&#x2F;code&gt; to the &lt;em&gt;&lt;strong&gt;minustwo&lt;&#x2F;strong&gt;&lt;&#x2F;em&gt; definition, we are handling the case for &lt;em&gt;1&lt;&#x2F;em&gt;, which is &lt;em&gt;S O&lt;&#x2F;em&gt;. As we know, &lt;em&gt;S O = 1&lt;&#x2F;em&gt;. This case will match and it returns &lt;em&gt;O&lt;&#x2F;em&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;For, &lt;code&gt;Compute minustwo 2&lt;&#x2F;code&gt;, we are passing &lt;em&gt;2&lt;&#x2F;em&gt; to the &lt;em&gt;&lt;strong&gt;minustwo&lt;&#x2F;strong&gt;&lt;&#x2F;em&gt; definition. For this case, we don’t have any direct case. What we have is &lt;em&gt;S (S m) ⇒ m.&lt;&#x2F;em&gt; So how does this work?&lt;&#x2F;p&gt;
&lt;p&gt;When we pass &lt;em&gt;2&lt;&#x2F;em&gt;, under the hood, Coq transforms &lt;em&gt;2&lt;&#x2F;em&gt; to &lt;em&gt;S (S O)&lt;&#x2F;em&gt;. So if we match this with the pattern in our case, it looks something like:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;S (S O) = S (S m)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In our &lt;em&gt;match&lt;&#x2F;em&gt; case, we are returning &lt;em&gt;m&lt;&#x2F;em&gt; if the pattern matches to &lt;em&gt;S (S m)&lt;&#x2F;em&gt;. But what is &lt;em&gt;m&lt;&#x2F;em&gt;? If we look at the above analysis &lt;code&gt;S (S O) = S (S m)&lt;&#x2F;code&gt; , in the place of &lt;em&gt;m&lt;&#x2F;em&gt; we have &lt;em&gt;O&lt;&#x2F;em&gt; on LHS. So we’ll be returning &lt;em&gt;O&lt;&#x2F;em&gt; as the result which is exactly what we needed. Because &lt;strong&gt;2 - 2 = 0&lt;&#x2F;strong&gt;. Let’s do &lt;strong&gt;5 - 2&lt;&#x2F;strong&gt; now and see how it works.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;5-2&quot;&gt;5 - 2&lt;a class=&quot;zola-anchor&quot; href=&quot;#5-2&quot; aria-label=&quot;Anchor link for: 5-2&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h2&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;&lt;code&gt;Compute minustwo 5.&lt;&#x2F;code&gt;  When we run this command we expect the output to be &lt;em&gt;3.&lt;&#x2F;em&gt; Let’s breakdown. As we know, Coq represents &lt;em&gt;5&lt;&#x2F;em&gt; under the hood as,&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;5 = S (S (S (S (S 0))))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So, let’s match this with our case:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;S (S (S (S (S 0)))) = S (S m)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;		            =&amp;gt; S (S (S (S (S 0))))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;		        m   =&amp;gt; S (S (S 0))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;em&gt;m&lt;&#x2F;em&gt; matches with &lt;code&gt;S (S (S 0))&lt;&#x2F;code&gt; which when reduced we get &lt;em&gt;3&lt;&#x2F;em&gt;. That is exactly what we want, because &lt;strong&gt;5 - 2 = 3&lt;&#x2F;strong&gt;. The reduction happens like:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;m	=&amp;gt; S (S (S 0))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	=&amp;gt; S (S 1)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	=&amp;gt; S 2&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	=&amp;gt; 3&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;a class=&quot;zola-anchor&quot; href=&quot;#conclusion&quot; aria-label=&quot;Anchor link for: conclusion&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h2&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;I was struggling to understand this for the past couple of hours before starting to write this post. I was finally able to grasp the concept after delving into the &lt;strong&gt;&lt;code&gt;minustwo&lt;&#x2F;code&gt;&lt;&#x2F;strong&gt; example and experimenting with different inputs. Through this journey, I gained a deeper appreciation for the elegance and power of pattern matching in Coq. It&#x27;s amazing how a seemingly simple construct can unlock so much potential in our code.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Lazy evaluation</title>
		<published>2024-03-15T00:00:00+00:00</published>
		<updated>2024-03-15T00:00:00+00:00</updated>
		<link rel="alternate" type="text/html" href="https://fazeneo.in/posts/lazy-evaluation/"/>
		<id>https://fazeneo.in/posts/lazy-evaluation/</id>
    
		<content type="html" xml:base="https://fazeneo.in/posts/lazy-evaluation/">&lt;p&gt;Trying to figure out what &lt;em&gt;&lt;strong&gt;lazy evaluation&lt;&#x2F;strong&gt;&lt;&#x2F;em&gt; is after knowing Haskell is fully lazy. Let’s write a normal sum function which takes in two arguments and add them and return the value. There would be two variants of the function:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;Eager evaluation&lt;&#x2F;li&gt;
&lt;li&gt;Lazy evaluation&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;h2 id=&quot;eager-evaluation&quot;&gt;Eager evaluation&lt;a class=&quot;zola-anchor&quot; href=&quot;#eager-evaluation&quot; aria-label=&quot;Anchor link for: eager-evaluation&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h2&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;tsx&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;function&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; sum&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable&quot;&gt;a&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; number&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable&quot;&gt; b&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; number&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; number&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	return&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; a&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; +&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; b&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;console&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;log&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;sum&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;10&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; +&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 20&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 100&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In the above code snippet, we are calling the &lt;code&gt;sum&lt;&#x2F;code&gt; function by passing two arguments. First is &lt;strong&gt;10 + 20&lt;&#x2F;strong&gt; and the second is &lt;strong&gt;100&lt;&#x2F;strong&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;What happens in this situation is, Typescript will first evaluate &lt;strong&gt;10 + 20&lt;&#x2F;strong&gt; and gets the result which would be &lt;strong&gt;30&lt;&#x2F;strong&gt;. Then it would call the &lt;code&gt;sum&lt;&#x2F;code&gt; function by passing in &lt;strong&gt;30 and 100&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;lazy-evaluation&quot;&gt;Lazy evaluation&lt;a class=&quot;zola-anchor&quot; href=&quot;#lazy-evaluation&quot; aria-label=&quot;Anchor link for: lazy-evaluation&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h2&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;tsx&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;function&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; sum&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;a&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; number&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; b&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; number&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; number&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	return&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; a&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; +&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; b&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;console&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;log&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;sum&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;10&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; +&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 20&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 100&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In the above code snippet, what happens is, we’ve defined the arguments and return types as functions, which would return a number.&lt;&#x2F;p&gt;
&lt;p&gt;So, when the calling the &lt;code&gt;sum&lt;&#x2F;code&gt; function, we wouldn’t pass the numbers directly, instead we pass in functions which returns a number. Now the return type of the &lt;code&gt;sum&lt;&#x2F;code&gt; function is a &lt;em&gt;function that returns a number,&lt;&#x2F;em&gt; which would be the final result.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;creating-a-lazy-type&quot;&gt;Creating a Lazy type&lt;a class=&quot;zola-anchor&quot; href=&quot;#creating-a-lazy-type&quot; aria-label=&quot;Anchor link for: creating-a-lazy-type&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h2&gt;
&lt;p&gt;Let’s create a lazy type and update the above lazy sum function&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;tsx&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;type&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; Lazy&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; T&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;function&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; sum&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable&quot;&gt;a&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; Lazy&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt;number&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable&quot;&gt; b&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; Lazy&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt;number&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; Lazy&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt;number&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	return&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; a&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; +&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; b&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;don-t-need-don-t-evaluate&quot;&gt;Don’t need, don’t evaluate&lt;a class=&quot;zola-anchor&quot; href=&quot;#don-t-need-don-t-evaluate&quot; aria-label=&quot;Anchor link for: don-t-need-don-t-evaluate&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h2&gt;
&lt;p&gt;How can we avoid big computations that are not needed? We don’t evaluate them. Let’s take a look at couple of examples that demonstrate this.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;tsx&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;function&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; recurse&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; T&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	return&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; recurse&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;function&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; sum&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable&quot;&gt;a&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; number&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable&quot;&gt; b&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; number&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; number&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	return&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; a&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;console&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;log&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;sum&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;10&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; +&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 20&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; recurse&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In the above code snippet, we have two functions. One is a recursive function named &lt;code&gt;recurse&lt;&#x2F;code&gt; and another one is &lt;code&gt;sum&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;recurse&lt;&#x2F;code&gt; function just calls itself and it doesn’t contain any base case so it won’t exit. The &lt;code&gt;sum&lt;&#x2F;code&gt; function, doesn’t actually adds the two arguments, instead it returns the first argument as it is.&lt;&#x2F;p&gt;
&lt;p&gt;Finally we call the &lt;code&gt;sum&lt;&#x2F;code&gt; function by passing two things. One is &lt;strong&gt;10 + 20 = 30&lt;&#x2F;strong&gt; and the other is we are passing(calling) the &lt;code&gt;recurse&lt;&#x2F;code&gt; function. What happens when we run the code?&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;We’ll get: &lt;code&gt;[ERR]: Maximum call stack size exceeded&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;Even before actually executing the function, Javascript will first evaluate 10 + 20 which gives 30. And after that, it calls the recurse function. Since this function doesn’t have a base case, it won’t exit. Which leads to stack size exceed. Even though we didn’t do anything with the “return value” of the &lt;code&gt;recurse&lt;&#x2F;code&gt; function inside the &lt;code&gt;sum&lt;&#x2F;code&gt; function, our program crashes. How can we resolve this?&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;tsx&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;function&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; sum&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable&quot;&gt;a&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; Lazy&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt;number&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable&quot;&gt; b&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; Lazy&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt;number&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; Lazy&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt;number&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	return&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; a&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;console&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;log&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;sum&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 10&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; +&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 20&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; recurse&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;What happens if we run the above program? We’ll get:&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;[LOG]: () =&amp;gt; 10 + 20&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;This is because our &lt;code&gt;sum&lt;&#x2F;code&gt; function returns the first argument as it is, which is nothing but a function. As you see from the above result, it doesn’t evaluate the expression because we haven’t really called the function. Let’s now call the &lt;code&gt;sum&lt;&#x2F;code&gt; function.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;tsx&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;console&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;log&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;sum&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 10&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; +&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 20&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; recurse&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It’ll return &lt;code&gt;30&lt;&#x2F;code&gt; as the return value.&lt;&#x2F;p&gt;
&lt;p&gt;As you can see, we are only returning &lt;code&gt;a&lt;&#x2F;code&gt;(the first argument) and not the “value” of the &lt;code&gt;recurse&lt;&#x2F;code&gt; function. But since we made it evaluate lazily, it won’t crash our program by going into &lt;em&gt;infinite recursion&lt;&#x2F;em&gt; before actually executing our function. This is the advantage of lazy evaluation.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;generators&quot;&gt;Generators&lt;a class=&quot;zola-anchor&quot; href=&quot;#generators&quot; aria-label=&quot;Anchor link for: generators&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h2&gt;
&lt;p&gt;Generators are part of Javascript that does lazy evaluation. Let’s modify our sum function and make it use generator.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;tsx&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;function&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;*&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; sum&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable&quot;&gt;a&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; number&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable&quot;&gt; b&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; number&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; Generator&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt;number&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	yield&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; a&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; +&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; b&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;let&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; sumGenerator&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; Generator&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt;number&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; sum&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;10&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; +&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 20&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 100&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;console&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;log&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;sumGenerator&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;next&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;value&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In the above code snippet, we have a &lt;em&gt;generator function&lt;&#x2F;em&gt; named &lt;strong&gt;sum&lt;&#x2F;strong&gt;. The generator function returns a &lt;em&gt;generator&lt;&#x2F;em&gt;. We call the &lt;strong&gt;sum&lt;&#x2F;strong&gt; function by passing in &lt;strong&gt;10 + 20 = 30&lt;&#x2F;strong&gt; and &lt;strong&gt;100&lt;&#x2F;strong&gt;. The execution won’t happen, instead it returns a &lt;em&gt;generator&lt;&#x2F;em&gt;. The generator will have various different states that I’m not gonna go into.&lt;&#x2F;p&gt;
&lt;p&gt;The generator will give have a &lt;strong&gt;next&lt;&#x2F;strong&gt; function, which upon called will execute the expression(30 + 100), which gives the result 130.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;real-world&quot;&gt;Real world&lt;a class=&quot;zola-anchor&quot; href=&quot;#real-world&quot; aria-label=&quot;Anchor link for: real-world&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h2&gt;
&lt;p&gt;Let’s try to create a function that generates an infinite list of numbers. Let’s look at two different ways:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;Naive approach&lt;&#x2F;li&gt;
&lt;li&gt;Generator approach (Lazy evaluation)&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;h2 id=&quot;naive-approach&quot;&gt;Naive approach&lt;a class=&quot;zola-anchor&quot; href=&quot;#naive-approach&quot; aria-label=&quot;Anchor link for: naive-approach&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h2&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;tsx&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;function&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; infiniteList&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; number&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;	let&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; n&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; Array&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt;number&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	for&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;let&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; i&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;span&gt; ;&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; i&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;++&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;		n&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;push&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;i&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	return&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; n&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;infiniteList&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If we try to execute the above code snippet, the function will keep on executing and doesn’t return anything. We haven’t mentioned the upper limit in the for loop because we wanted to generate an infinite list. Since the function won’t return anything, its not useful. How would we solve this?&lt;&#x2F;p&gt;
&lt;h2 id=&quot;generator-approach&quot;&gt;Generator approach&lt;a class=&quot;zola-anchor&quot; href=&quot;#generator-approach&quot; aria-label=&quot;Anchor link for: generator-approach&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h2&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;tsx&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;function&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;*&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; infiniteList&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; Generator&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt;number&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	for&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;let&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; i&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; i&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;++&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; yield&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; i&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;let&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; infiniteListGenerator&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; Generator&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt;number&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; infiniteList&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;let&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; i&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; of&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; infiniteListGenerator&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;	console&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;log&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;i&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;DISCLAIMER&lt;&#x2F;strong&gt;: Running the above code eats up your memory fast&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;In the above code snippet, we’ve created a &lt;em&gt;generator function&lt;&#x2F;em&gt; named &lt;strong&gt;infiniteList,&lt;&#x2F;strong&gt; which iterates from 0 to Infinity. Then we are calling the generator function which returns a generator(&lt;strong&gt;infiniteListGenerator&lt;&#x2F;strong&gt;). Finally we are iterating over the generator which gives us values from 0 to Infinity.&lt;&#x2F;p&gt;
&lt;p&gt;The difference between the naive approach and the generator approach is that, the naive approach won’t even return anything until it completes the calculation. On the other hand, the generator approach evaluates it one by one, once it evaluates it returns the result of the evaluation back to the caller.&lt;&#x2F;p&gt;
&lt;p&gt;Using this feature, now we can easily able to generate infinite lists. Let’s say we wanted first 10, 100, 1000 and 10,000 values. We could do something like:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;tsx&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;Array&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;new&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; Array&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;10&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; infiniteListGenerator&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;next&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;value&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;Array&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;new&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; Array&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;100&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; infiniteListGenerator&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;next&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;value&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;Array&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;new&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; Array&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;1000&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; infiniteListGenerator&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;next&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;value&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;Array&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;new&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; Array&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;10000&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; infiniteListGenerator&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;next&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;value&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;Array&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;new&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; Array&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;100000&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; infiniteListGenerator&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;next&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;value&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Of course it would take performance hits once the value gets bigger. But the point is, its possible to generate infinite list with the help of lazy evaluation. Next time when you want to avoid big computations when generating something, try lazy evaluation.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Sequential Logic</title>
		<published>2024-03-07T00:00:00+00:00</published>
		<updated>2024-03-07T00:00:00+00:00</updated>
		<link rel="alternate" type="text/html" href="https://fazeneo.in/posts/sequential-logic/"/>
		<id>https://fazeneo.in/posts/sequential-logic/</id>
    
		<content type="html" xml:base="https://fazeneo.in/posts/sequential-logic/">&lt;p&gt;To know more about &lt;strong&gt;Sequential logic&lt;&#x2F;strong&gt;, we need to first know about &lt;strong&gt;Combinational logic&lt;&#x2F;strong&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;combinational-logic&quot;&gt;Combinational logic&lt;a class=&quot;zola-anchor&quot; href=&quot;#combinational-logic&quot; aria-label=&quot;Anchor link for: combinational-logic&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h2&gt;
&lt;p&gt;In digital circuits combinational logic refers to circuits whose output solely depends on the current values of the input. Whenever the input the changes, the output changes respectively. Combinational logic doesn’t involve time, it is purely depend on the current state of the values.&lt;&#x2F;p&gt;
&lt;p&gt;Some examples of logic circuits that comes under Combination logic are &lt;strong&gt;NAND, AND, OR, NOT, XOR&lt;&#x2F;strong&gt; gates. These are elementary gates that are used to perform boolean algebra. Let’s look at a specific gate: &lt;strong&gt;AND&lt;&#x2F;strong&gt;. The truth table of the AND gate looks like this:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;| A | B | OUT |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;|---|---|-----|&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;| 0 | 0 |  0  |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;| 0 | 1 |  0  |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;| 1 | 0 |  0  |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;| 1 | 1 |  1  |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;To reiterate, whenever the value of the input signals changes, the output changes.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;sequential-logic&quot;&gt;Sequential logic&lt;a class=&quot;zola-anchor&quot; href=&quot;#sequential-logic&quot; aria-label=&quot;Anchor link for: sequential-logic&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h2&gt;
&lt;p&gt;Sequential logic on the other hand deals with &lt;em&gt;&lt;strong&gt;time&lt;&#x2F;strong&gt;&lt;&#x2F;em&gt;. Whenever we take &lt;em&gt;&lt;strong&gt;time&lt;&#x2F;strong&gt;&lt;&#x2F;em&gt; into consideration, we have to deal with memory and state. In Sequential logic, the output not only depends on the current input but also on the previous inputs. Sequential logic has &lt;em&gt;&lt;strong&gt;state&lt;&#x2F;strong&gt;&lt;&#x2F;em&gt;, which is nothing but &lt;em&gt;&lt;strong&gt;memory&lt;&#x2F;strong&gt;&lt;&#x2F;em&gt;. But the combinational logic doesn’t have state since it doesn’t deal with &lt;em&gt;&lt;strong&gt;time&lt;&#x2F;strong&gt;&lt;&#x2F;em&gt;. Some examples of logic circuits that comes under Sequential logic are &lt;strong&gt;Flip-Flop, Bit, Registers, RAM&lt;&#x2F;strong&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;There are two types of Sequential logic circuits:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Synchronous&lt;&#x2F;strong&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Asynchronous&lt;&#x2F;strong&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;But before looking into these two types of sequential logic circuits, we need to know about &lt;em&gt;&lt;strong&gt;clock signals&lt;&#x2F;strong&gt;&lt;&#x2F;em&gt;. What are clock signals?&lt;&#x2F;p&gt;
&lt;h2 id=&quot;clock-signals&quot;&gt;Clock signals&lt;a class=&quot;zola-anchor&quot; href=&quot;#clock-signals&quot; aria-label=&quot;Anchor link for: clock-signals&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h2&gt;
&lt;p&gt;In electronics, a clock signal is an electronic logic signal(voltage or current) that oscillates between a high and a low state at a constant frequency.&lt;&#x2F;p&gt;
&lt;p&gt;In a Synchronous logic circuit, the clock signal is applied to all storage devices, like flip-flops and causes them to change state simultaneously.&lt;&#x2F;p&gt;
&lt;p&gt;A clock signal is produced by electronic oscillator called a clock generator. The most common form of clock signal is the square wave form.&lt;&#x2F;p&gt;
&lt;p&gt;Circuits using clock signal for synchronization may become active at either raising edge, falling edge or in the case of double data rate, both in the raising edge and falling edge of the clock cycle.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;synchronous-sequential-logic&quot;&gt;Synchronous Sequential logic&lt;a class=&quot;zola-anchor&quot; href=&quot;#synchronous-sequential-logic&quot; aria-label=&quot;Anchor link for: synchronous-sequential-logic&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h2&gt;
&lt;p&gt;In Synchronous Sequential logic circuits, the state of the device(circuit) changes only at discrete time intervals in response to a &lt;em&gt;&lt;strong&gt;clock signal&lt;&#x2F;strong&gt;&lt;&#x2F;em&gt;. For example, a Data Flip Flop(DFF) can be implemented using Synchronous sequential logic:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;verilog&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;module&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; DFF&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	input&lt;&#x2F;span&gt;&lt;span&gt; D, clk,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	output&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; reg&lt;&#x2F;span&gt;&lt;span&gt; Q&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	always&lt;&#x2F;span&gt;&lt;span&gt; @(&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;posedge&lt;&#x2F;span&gt;&lt;span&gt; clk) Q &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;&amp;lt;=&lt;&#x2F;span&gt;&lt;span&gt; D;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;endmodule&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In the above code, we have implemented DFF using Verilog. DFF takes a data input(&lt;strong&gt;D&lt;&#x2F;strong&gt;) and a clock signal(&lt;strong&gt;clk&lt;&#x2F;strong&gt;). Whenever the clock signal goes from &lt;em&gt;&lt;strong&gt;negedge(0)&lt;&#x2F;strong&gt;&lt;&#x2F;em&gt; to &lt;em&gt;&lt;strong&gt;posedge(1),&lt;&#x2F;strong&gt;&lt;&#x2F;em&gt; the output would be the input.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;asynchronous-sequential-logic&quot;&gt;Asynchronous Sequential logic&lt;a class=&quot;zola-anchor&quot; href=&quot;#asynchronous-sequential-logic&quot; aria-label=&quot;Anchor link for: asynchronous-sequential-logic&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h2&gt;
&lt;p&gt;In Asynchronous Sequential logic, the state of the device(circuit) can change at any time in response to changing inputs. For example, the same DFF can be implemented using Asynchronous sequential logic:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;verilog&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;module&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; DFF&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	input&lt;&#x2F;span&gt;&lt;span&gt; D, clk, reset,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	output&lt;&#x2F;span&gt;&lt;span&gt; Q&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	always&lt;&#x2F;span&gt;&lt;span&gt; @(&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;posedge&lt;&#x2F;span&gt;&lt;span&gt; clk &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;or&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; posedge&lt;&#x2F;span&gt;&lt;span&gt; reset) &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;begin&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;		if&lt;&#x2F;span&gt;&lt;span&gt; (reset) &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;begin&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;			Q &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;&amp;lt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;		end&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; else&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; begin&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;			Q &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;&amp;lt;=&lt;&#x2F;span&gt;&lt;span&gt; D;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;		end&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	end&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;endmodule&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In the above code example, we’ve added an input named &lt;strong&gt;reset&lt;&#x2F;strong&gt;, which is part of the &lt;code&gt;always&lt;&#x2F;code&gt; block sensitive list. Along with the clock signal(&lt;code&gt;clk&lt;&#x2F;code&gt;), whenever the &lt;code&gt;reset&lt;&#x2F;code&gt; is &lt;code&gt;1&lt;&#x2F;code&gt; our circuit resets the value of the output to &lt;code&gt;0&lt;&#x2F;code&gt;. In this case, the DFF resets the value of the output to &lt;code&gt;0&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;This is what we mean by &lt;em&gt;the state of the device(circuit) can change at any time in response to changing inputs&lt;&#x2F;em&gt;.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Writing a Counter in Verilog</title>
		<published>2024-03-07T00:00:00+00:00</published>
		<updated>2024-03-07T00:00:00+00:00</updated>
		<link rel="alternate" type="text/html" href="https://fazeneo.in/posts/writing-a-counter-in-verilog/"/>
		<id>https://fazeneo.in/posts/writing-a-counter-in-verilog/</id>
    
		<content type="html" xml:base="https://fazeneo.in/posts/writing-a-counter-in-verilog/">&lt;p&gt;This post is about how to write a &lt;strong&gt;4-bit Counter with synchronous reset&lt;&#x2F;strong&gt; in Verilog. But what do I mean by &lt;strong&gt;Counter&lt;&#x2F;strong&gt;? A Counter counts from 0 to n, where n &amp;gt; 0. Usually we count in decimals like 0, 1, 2, 3, 4, 5...n. But computers don’t count like that, they count in &lt;em&gt;binary&lt;&#x2F;em&gt; as we all know.&lt;&#x2F;p&gt;
&lt;p&gt;The 4-bit Counter counts from 0 to 15. But why only 15? because that’s the highest value of the number that we can represent in 4-bits (0000 = 0, 1111 = 15).&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Let’s count from 0 to 15 in Binary:&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;0000  = 0 &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;0001 = 1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;0010 = 2&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;0011 = 3&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;0100 = 4 &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;0101 = 5 &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;0110 = 6 &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;0111 = 7 &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;1000 = 8 &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;1001 = 9 &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;1010 = 10 &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;1011 = 11 &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;1100 = 12 &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;1101 = 13 &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;1110 = 14 &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;1111 = 15 &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now let’s first look at how to write a 4-bit Counter in Verilog &lt;em&gt;&lt;strong&gt;without Synchronous reset&lt;&#x2F;strong&gt;&lt;&#x2F;em&gt;.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;verilog&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment&quot;&gt;&#x2F;&#x2F;&lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt; counter.v&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;module&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; Counter4Bit&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	input&lt;&#x2F;span&gt;&lt;span&gt; clk,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	output&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; reg&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;] count &lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-comment&quot;&gt;&#x2F;&#x2F;&lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt; 4-bit to hold value till 1111 (15 in Decimal)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	always&lt;&#x2F;span&gt;&lt;span&gt; @(&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;posedge&lt;&#x2F;span&gt;&lt;span&gt; clk) &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;begin&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;		count &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;&amp;lt;=&lt;&#x2F;span&gt;&lt;span&gt; count &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;+&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	end&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;endmodule&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;verilog&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;module&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; counter_tb&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	reg&lt;&#x2F;span&gt;&lt;span&gt; clk;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	wire&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;] count;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-tag&quot;&gt;	Counter4Bit&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-tag&quot;&gt; counter&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;		.clk(clk), .count(count)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	)&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	always&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; #1&lt;&#x2F;span&gt;&lt;span&gt; clk &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; ~&lt;&#x2F;span&gt;&lt;span&gt;clk;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	initial&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; begin&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;		clk &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;; &lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-comment&quot;&gt;&#x2F;&#x2F;&lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt; initial value of clk at 0ns time unit.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;		#100&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; $finish&lt;&#x2F;span&gt;&lt;span&gt;; &lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-comment&quot;&gt;&#x2F;&#x2F;&lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt; stop the simulation after 100ns time unit&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	end&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;endmodule&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If we run the above &lt;em&gt;design&lt;&#x2F;em&gt; code along with the &lt;em&gt;testbench&lt;&#x2F;em&gt;, we can see that the output of &lt;em&gt;count&lt;&#x2F;em&gt; will always be &lt;strong&gt;x&lt;&#x2F;strong&gt;. But what is &lt;strong&gt;x&lt;&#x2F;strong&gt;? In Verilog, &lt;strong&gt;x&lt;&#x2F;strong&gt; represents &lt;em&gt;unknown&lt;&#x2F;em&gt; or &lt;em&gt;undefined&lt;&#x2F;em&gt; value.&lt;&#x2F;p&gt;
&lt;p&gt;The reason is because:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;At time unit &lt;code&gt;0ns&lt;&#x2F;code&gt;, the value of &lt;code&gt;clk&lt;&#x2F;code&gt; is &lt;code&gt;0&lt;&#x2F;code&gt; and the value of &lt;code&gt;count&lt;&#x2F;code&gt; is &lt;code&gt;x&lt;&#x2F;code&gt; which is the initial value.&lt;&#x2F;li&gt;
&lt;li&gt;At time unit &lt;code&gt;1ns&lt;&#x2F;code&gt;, the value of &lt;code&gt;clk&lt;&#x2F;code&gt; is &lt;code&gt;1&lt;&#x2F;code&gt; and the value of &lt;code&gt;count&lt;&#x2F;code&gt; is still &lt;code&gt;x&lt;&#x2F;code&gt; . This is because we can’t add anything with unknown or undefined value. That is why at the positive edge of the &lt;code&gt;clk&lt;&#x2F;code&gt; , &lt;code&gt;count = x + 1&lt;&#x2F;code&gt; which results to &lt;code&gt;x&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;mataroa.blog&#x2F;images&#x2F;74493cae.png&quot; alt=&quot;Screenshot_2024-02-23_at_7-57-18_PM&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;So, how can we fix this? We need to provide a way to set the value of &lt;code&gt;count&lt;&#x2F;code&gt; to &lt;code&gt;0&lt;&#x2F;code&gt; when the &lt;code&gt;clk&lt;&#x2F;code&gt; (clock) goes from negative edge to positive edge (0 to 1). This is where &lt;code&gt;reset&lt;&#x2F;code&gt; comes in. Let’s take a look at the slightly modified version of the &lt;em&gt;design code&lt;&#x2F;em&gt; and &lt;em&gt;testbench&lt;&#x2F;em&gt;.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;verilog&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment&quot;&gt;&#x2F;&#x2F;&lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt; design code&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;module&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; Counter4Bit&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	input&lt;&#x2F;span&gt;&lt;span&gt; clk, reset,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	output&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; reg&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;] count&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	always&lt;&#x2F;span&gt;&lt;span&gt; @(&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;posedge&lt;&#x2F;span&gt;&lt;span&gt; clk) &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;begin&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;		if&lt;&#x2F;span&gt;&lt;span&gt; (reset) &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;begin&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;			count &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;&amp;lt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;		end&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; else&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; begin&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;			count &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;&amp;lt;=&lt;&#x2F;span&gt;&lt;span&gt; count &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;+&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;		end&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	end&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;endmodule&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;verilog&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment&quot;&gt;&#x2F;&#x2F;&lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt; testbench&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;module&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; counter_tb&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	reg&lt;&#x2F;span&gt;&lt;span&gt; clk, reset;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	wire&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;] count;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-tag&quot;&gt;	Counter4Bit&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-tag&quot;&gt; counter&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;		.clk(clk),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;		.reset(reset),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;		.count(count)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	)&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment&quot;&gt;	&#x2F;&#x2F;&lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt; for every 2ns time unit, clk goes from 0 to 1 to 0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	always&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; #1&lt;&#x2F;span&gt;&lt;span&gt; clk &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; ~&lt;&#x2F;span&gt;&lt;span&gt;clk;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	initial&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; begin&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;		clk &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;; reset &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;; &lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-comment&quot;&gt;&#x2F;&#x2F;&lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt; set initial values at 0ns time unit&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;		#1&lt;&#x2F;span&gt;&lt;span&gt; reset&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;; &lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-comment&quot;&gt;&#x2F;&#x2F;&lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt; set the initial value of count to 0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;		#1&lt;&#x2F;span&gt;&lt;span&gt; reset&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;; &lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-comment&quot;&gt;&#x2F;&#x2F;&lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt; increment count&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;		#30&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; $finish&lt;&#x2F;span&gt;&lt;span&gt;; &lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-comment&quot;&gt;&#x2F;&#x2F;&lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt; stop the simulation after 33ns time unit&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;	end&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;endmodule&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Let’s breakdown the above &lt;em&gt;design code&lt;&#x2F;em&gt; and &lt;em&gt;testbench&lt;&#x2F;em&gt;:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;We’ve added a &lt;code&gt;reset&lt;&#x2F;code&gt; wire as output in &lt;code&gt;Counter4Bit&lt;&#x2F;code&gt; module. Whenever the value of &lt;code&gt;reset&lt;&#x2F;code&gt; is &lt;code&gt;1&lt;&#x2F;code&gt; it sets &lt;code&gt;count&lt;&#x2F;code&gt; to &lt;code&gt;0&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
&lt;li&gt;In the &lt;em&gt;testbench,&lt;&#x2F;em&gt; we’ve set the initial value of &lt;code&gt;reset&lt;&#x2F;code&gt; to &lt;code&gt;0&lt;&#x2F;code&gt; at &lt;code&gt;0ns&lt;&#x2F;code&gt; time unit.&lt;&#x2F;li&gt;
&lt;li&gt;At &lt;code&gt;1ns&lt;&#x2F;code&gt; time unit, we are making &lt;code&gt;reset&lt;&#x2F;code&gt; as &lt;code&gt;1&lt;&#x2F;code&gt; , this make sure that the initial value of &lt;code&gt;count&lt;&#x2F;code&gt; is set to &lt;code&gt;0&lt;&#x2F;code&gt;. Also this happens when the value of &lt;code&gt;clk&lt;&#x2F;code&gt; is &lt;code&gt;1&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
&lt;li&gt;At &lt;code&gt;2ns&lt;&#x2F;code&gt; time unit, we are making &lt;code&gt;reset&lt;&#x2F;code&gt; as &lt;code&gt;0&lt;&#x2F;code&gt; to start incrementing the value of &lt;code&gt;count&lt;&#x2F;code&gt; by 1.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;When we run the above &lt;em&gt;design code&lt;&#x2F;em&gt; and &lt;em&gt;testbench&lt;&#x2F;em&gt;:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;At time unit &lt;code&gt;0ns&lt;&#x2F;code&gt;, clk=0, reset=0 and count=xxxx&lt;&#x2F;li&gt;
&lt;li&gt;At time unit &lt;code&gt;1ns&lt;&#x2F;code&gt; , clk=1, reset=1 and count=0&lt;&#x2F;li&gt;
&lt;li&gt;At time unit &lt;code&gt;2ns&lt;&#x2F;code&gt;, clk=0, reset=0 and count=0&lt;&#x2F;li&gt;
&lt;li&gt;At time unit &lt;code&gt;3ns&lt;&#x2F;code&gt;, clk=1, reset=0 and count=1&lt;&#x2F;li&gt;
&lt;li&gt;At time unit &lt;code&gt;4ns&lt;&#x2F;code&gt;, clk=0, reset=0 and count=1&lt;&#x2F;li&gt;
&lt;li&gt;At time unit &lt;code&gt;5ns&lt;&#x2F;code&gt;, clk=1, reset=0 and count=10&lt;&#x2F;li&gt;
&lt;li&gt;At time unit &lt;code&gt;6ns&lt;&#x2F;code&gt; , clk=0, reset=0 and count=10&lt;&#x2F;li&gt;
&lt;li&gt;….&lt;&#x2F;li&gt;
&lt;li&gt;At time unit &lt;code&gt;32ns&lt;&#x2F;code&gt;, clk=0, reset=0 and count=1111&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;mataroa.blog&#x2F;images&#x2F;b8e4df3a.png&quot; alt=&quot;Screenshot_2024-02-23_at_8-01-59_PM&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;This is a brief description about how to write a 4-bit Counter with Synchronous reset in Verilog. The reference code can be found &lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;M-krishna&#x2F;counter&quot;&gt;here&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
</content>
	</entry>
</feed>
