<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Karan Davuluri on Medium]]></title>
        <description><![CDATA[Stories by Karan Davuluri on Medium]]></description>
        <link>https://medium.com/@karandavuluri?source=rss-1a38cc79fc0b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*7S7f04MwN43e813n.jpg</url>
            <title>Stories by Karan Davuluri on Medium</title>
            <link>https://medium.com/@karandavuluri?source=rss-1a38cc79fc0b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 22:26:47 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@karandavuluri/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Safety:]]></title>
            <link>https://medium.com/@karandavuluri/safety-e1b729d03ee9?source=rss-1a38cc79fc0b------2</link>
            <guid isPermaLink="false">https://medium.com/p/e1b729d03ee9</guid>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Karan Davuluri]]></dc:creator>
            <pubDate>Tue, 23 Apr 2019 03:28:19 GMT</pubDate>
            <atom:updated>2019-04-23T04:27:55.995Z</atom:updated>
            <content:encoded><![CDATA[<h3>Benchmarking PROLOG, Ease of Use</h3><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*k9uEnH8FBGhTXggGEE4tlg.png" /></figure><h3>Ease of Use (Prolog):</h3><p>Prolog is a programming language centered around a small set of basic mechanisms, including pattern matching, tree-based data structuring, and automatic backtracking. This small set constitutes a surprisingly powerful and flexible programming framework.</p><p>Prolog is especially well suited for problems that involve objects — in particular, structured objects — and relations between them. For example, it is an easy exercise in Prolog to express spatial relationships between objects, such as the blue sphere is behind the green one.</p><p>It is also easy to state a more general rule: if object X is closer to the observer than object Y, and Y is closer than Z, then X must be closer than Z. Prolog can now reason about the spatial relationships and their consistency with respect to the general rule. Features like this make Prolog a powerful language for artificial intelligence (AI) and non-numerical programming in general.</p><p>There are well-known examples of symbolic computation whose implementation in other standard languages took tens of pages of indigestible code. When the same algorithms were implemented in Prolog, the result was a crystal-clear program easily fitting on one page.</p><h3>Safety:</h3><p>Prolog language was developed in the early 1970 and was created for the practical tool of logic programming and efficiency of the language was considered as one of the primary objectives.</p><p>The language consists of clauses and in order to provide efficiency the language employs a simple search and computation rule. The search rule searches the search tree using a depth-first search. Whenever it cannot find a clause it uses trace back to consider another clause.</p><p>All the clauses are executed from left to right which means that all the sub-goals are executed in a sequence.</p><p>This strategy of DFS and left to right greatly reduces the needed runtime structures.but this search or computation procedure doesn’t give rise to a procedural semantics that is similar to declarative semantics. The result is that the prolog would not be able to find a possible solution even if the logic is written correctly.</p><p>So we can say that a prolog programmer needs to be careful about the prolog’s procedural semantics while writing a program and cannot have a declarative mindset.</p><p>One more and final problem with Prolog is that the unification operation is used without any occur-check. Which implies that any variable P can be unified with any other term even without that the variable P occurs within that term.</p><h4>Benchmarking Speed:</h4><p>The benchmark is the Fibonacci function written in the naive recursive way so as to quickly obtain a large number of recursive calls. The advantage is that the code can be easily transcribed into other programming languages. Below, I give you also a Python, and a Java version using BigInts.</p><p>Also, we do not claim that the benchmark shows that Prolog is faster than Java in general. My only claim is that if an application is well suited to Prolog, its performance can be surprisingly good.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xobViA6tojAgLkmA4L7W-A.png" /></figure><p>Below is the Prolog program that we used for benchmarking the speed of different prolog interpreters for the same program.</p><p>Now, take the file halt.pl with contents:</p><pre>main :- print(hello),nl,halt.<br> :- main.</pre><p>The following takes 0.028 seconds in real time and 0.015 seconds of user time.</p><pre>time sicstus -l halt.pl</pre><p>The following takes 0.204 seconds real time the first time and 0.015 seconds real time the second time:</p><pre>time swipl -c halt.pl</pre><h3><strong>Memory footprint:</strong></h3><p>After further developments now in a compiler based systems, the prolog does not contain any passive data structure. One can assume this step of converting an interpreter based language to a compiled based language as a union of the interpreter with the Prolog program.</p><p>Most of the modern prolog interpreters use the conventional three stack-based systems.</p><p>(1) Local stack: On this stack, all the choice points are saved and also the environment is saved on this stack.</p><p>(2) Global stack: This stack is used for the storage of all the structured object such as the lists.</p><p>(3) Trail stack: ON this stack all the variables will be stored i.e, pushed which are to be reset when the process of backtracking occurs in the execution of the program.</p><p>Environments are the continuous blocks of memory which are given space on the local stack or the primary stack. For each and every clause which is having more than one goal, a separate environment has to be created.</p><h4><strong>Reasons for choosing PROLOG:</strong></h4><p>Free, open source and well maintained is one way to describe prolog’s usefulness.</p><p>Prolog is very interactive as it comes along with command interface and a top-level interpreter.</p><p>In Prolog you need not to worry about pointers, such difficulties are eliminated when using Prolog.</p><p>Prolog has been a talking point because firstly it can represent implicit relationships, secondly helps to find implicit relationships that are not obvious, third for pattern matching, fourth to represent multiple solutions to a single problem, fifth work with incomplete data structures and finally polymorphism.</p><p>Prolog can do few things easily that are otherwise difficult to implement in conventional languages i.e. knowledge representation and logical deduction. It helps the assists to form a more powerful frame of both the problem and the solution at hand.</p><p>Prolog has no side effects. Prolog is the language that kind of has no use for (local or class member) variables, it’s kind of forces the programmer does not use variable and proper use of the language improves the programmer’s ability to reason.</p><p>Prolog allows for an almost direct match between a FOL algorithm and its execution syntax. Since it is based on FOL, if the facts are true, then the program’s output will be true results. It has a declarative nature. Such nature is useful to build and consult multi-relational data.</p><p>Prolog has an in-built list handler which makes it easy to understand any kind of algorithm which makes use of lists in them.</p><p><strong>CONTRIBUTORS TO THIS GUIDE :</strong></p><p><em>Sai Kiran Reddy Surabhi @ </em><a href="https://twitter.com/saikira74573386"><em>Kiran</em></a></p><p><em>Kalyan Nagavaram @ </em><a href="https://twitter.com/KalyanNagavaram"><em>Kalyan</em></a></p><p><em>Sri Karan Davuluri @ </em><a href="https://twitter.com/KaranDavuluri"><em>Karan</em></a></p><p><em>Sree Harsha Donepudi @ </em><a href="https://twitter.com/donepudisree"><em>Harsha</em></a></p><p><em>Sankalp Mogulothu @ </em><a href="http://twitter.com/29_sankalp"><em>sankalp</em></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e1b729d03ee9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Toolchain and Internals, Internal Representation In PROLOG]]></title>
            <link>https://medium.com/@karandavuluri/toolchain-and-internals-internal-representation-in-prolog-4afedf42b435?source=rss-1a38cc79fc0b------2</link>
            <guid isPermaLink="false">https://medium.com/p/4afedf42b435</guid>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Karan Davuluri]]></dc:creator>
            <pubDate>Mon, 01 Apr 2019 16:08:01 GMT</pubDate>
            <atom:updated>2019-04-01T16:11:42.172Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*xIjKwbtNxtM1ZugJdmX8ow.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*nY2lHq8W5tqlES87Z-EhPQ.png" /><figcaption>Toolchain and internals → PROLOG</figcaption></figure><p>Some of the main features of the Prolog language are :</p><p>→non-deterministic</p><p>→control</p><p>→backtracking</p><p>This is needed to be known so as to be able to use the built-in predicates and also assess the behavior of the Prolog program. The steps in the execution of a Prolog program consists of loading the user-defined predicates into the current database and then executing the goals that are ready.</p><p><strong>The Storing Database and execution environment:</strong></p><p>In the Prolog language, there are many built-in functions that are used for modifying and altering the various objects that are present in the environment. The most important types of objects are :</p><p>→The database</p><p>→The sinks and sources</p><p>→The op and ch conversion table</p><p>→The flags</p><p>Let&#39;s discuss each of them in details now:</p><h4><strong>The database:</strong></h4><p>This consists of all the procedures that can be uniquely identified. It includes all the user-defined and the built-in predicates of the prolog. Here each procedure is linked with a sequence of clauses whose order is as given by the user input but the final ordering of these clauses is implementation-defined.</p><p>All the variables in the database such as X are replaced by call (X) and also the facts have a body whose value is true.</p><p>The principle that is used by the prolog in the database is defined as follows :</p><blockquote><em>“The different pathways that are to be executed are explored for a prediction are not affected by the sequential actions.”</em></blockquote><p>In the storage, all the procedures are dynamic or static. A procedure id by default declared as static and cannot be modified, it can be declared dynamic using the prolog manual. By default al the predefined predicates are private, and all the user-defined predicates are public.</p><p>The division of procedure inside a database is as given :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/664/1*9L2IS95ATiXOkQ3kDg7HmQ.png" /></figure><h4><strong>Sources and sinks:</strong></h4><p>All the output data is given to the sink and all the data required for the execution is accessed from the source. These are files, terminal input and every other form of input or output.</p><p>Both sources and sinks are by default considered as streams. whenever a stream is opened or created a handle is created to use with it for controlling it .it is also called as an alias for that stream.</p><h4><strong>Flags:</strong></h4><p>The flags in Prolog are the mono valued that is either the user or implementation-defined. They can be used as parameters for saying how the model should be executed if a specified procedure is not found that is the value of the flag is not known.</p><p><strong>Operator and character conversion table :</strong></p><p>There are two global tables that are present during the whole execution of the Prolog program. The operators are used I/O in the terms in a form, the type of the operator is also defined in the operator table. The character conversion table consists of the inbuilt character and also the extended character set.</p><p><strong>The Execution model for definite prolog:</strong></p><p>In more definite prolog there are only user-defined predicates and no built-in predicates are present. In the body, it consists of the sequence of terms. The execution model of the prolog is described by the “<em> General resolution algorithm</em>”.</p><p><strong>The general Resolution algorithm :</strong></p><p>To attain a goal A with database B :</p><blockquote>1- Start with current goal and current substitution which is normally empty.</blockquote><blockquote>2- If A is true stop , otherwise continue</blockquote><blockquote>3- Choose a predication P in A an if P is true delete it and go to step 2</blockquote><blockquote>4- If there ifs no lastely named clause in B has a head that unifies with P then stop ,els</blockquote><blockquote>5- choose from A an renamed clause and repeat the above procedure.</blockquote><p>A renamed clause means a clause that is renamed with respect to all the variables which have been processed in the previous resolution steps. The algorithm works in an indeterminate manner which can lead to success, failure, or probably possible number of the infinite solution as because it is not fixed that in what order the computation occurs.</p><p><strong>The Prolog computation rule :</strong></p><blockquote><strong>* </strong>The predicate has only one choice of selecting the first predicate in the given sequence.</blockquote><blockquote>*The clause has the choice of choosing the unifiable clause as defined in the sequential order.</blockquote><p><strong>The Prolog search tree :</strong></p><p>All the computation that is carried out by the algorithm is represented using the PST.which has the following features.</p><blockquote>*Each node is labeled by its substitution and current goal.</blockquote><blockquote>*The root node has the initial goal and it has no substitution.</blockquote><p>There are two kinds of leaf-nodes :</p><blockquote>* The success nodes are labeled true.</blockquote><blockquote>* The failure nodes are named different from that of true and there is no remained clause whose head is unifiable.</blockquote><p>A leaf node has its children equal to the no of clauses whose heads are unifiable with the targeted prediction. The children I.e the clauses are in the same order as of the databases and left to right model is used</p><p>There are three kinds of branches in a PST :</p><blockquote>*A success branch goes to a success node and a failed branch goes to a failed node.</blockquote><blockquote>*An infinite branch is non-terminating prolog computation rule.</blockquote><p>At each node, a substitution is calculated as the grouping of all the available local substitutions along that path.</p><h3>Example:</h3><p>Let us assume a database and goal (u,v)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2vWl1joHNW7lpvkrPywhEQ.png" /></figure><p><strong>Figure</strong>:- For the above conditions following is the Prolog search-tree.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/563/1*5MNoipOrt8Q9Zrw2nAO3rQ.png" /></figure><blockquote>{U &lt;- c, V &lt;- c1, X &lt;- c, Y &lt;- c1 }</blockquote><blockquote>{U &lt;- c, V &lt;- c1}</blockquote><blockquote>{ U &lt;- b, V &lt;- b1 };</blockquote><blockquote>{ U &lt;- c, V &lt;- c1 };</blockquote><blockquote>{ U &lt;- d };</blockquote><h4><strong>Definite Prolog and its execution:</strong></h4><p>The following 7 steps explain prolog execution in two main parts: the “down walk” and “backtracking”. In the first part i.e. down walk, it is the same as the general resolution algorithm. And the later part i.e. backtracking emphasizes on not yet visited path.</p><blockquote>1. Initially begin at root i.e. current node, Let G be the primary goal,</blockquote><blockquote>2. It is a success then backtrack, if and only if the present node i.e. current node is true. else</blockquote><blockquote>3. Consider A as the initial prediction in our goal.</blockquote><blockquote>4. Delete A if it is true, and then head to step-2. The tail of sequence G will be the latest current goal.</blockquote><blockquote>5. If there is no revamped clause in P with a head unifying with A, then backtrack.</blockquote><blockquote>6. Add as many children as are newly renamed to the current node, clauses H:- B in P, the head of which can be unified with A in a similar pattern as those in P.</blockquote><blockquote>7. The present node will be the first child and then head to step-2.</blockquote><p>A node is considered to be non-deterministic if it has multiple children. A node for which A can be re-executed is termed a choice point. This algorithm defines a path to achieve success or failure is attained.</p><h4>Backtracking</h4><p>After Building a successful or failed branch, possible nodes to be visited are nodes with children that are still not visited ‘right-hand side’ of that branch. Given the children’s left to right order, this is shown in Figure below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/554/1*xPZZ1mXum0sKm_4TREIwuA.png" /></figure><p>The updated current node was the first unvisited child and implementation heads to step-2. When there are no unvisited children, the original goal will be fulfilled.</p><h3>The execution model for Standard Prolog</h3><p>Until now, the procedures have been presumed to be user-defined ones. The model goes way directly to predicates that are built-in. This algorithm is therefore tailored to interpret the achievement of a final goal in a specific environment (database, sources, and sinks, flags and tables).</p><p>Here in the above-discussed steps -5,6 are changed to take the built-in propositions and their potential side effects into consideration.</p><blockquote><strong>A) The predication selected determined correlates to an additional procedure defined by the user</strong></blockquote><p>→ The execution goes on as shown in steps-5, 6 of the visit and construction algorithm to the Prolog search tree above.</p><blockquote><strong>B) The prediction determined doesn’t match any existing procedure</strong></blockquote><p>Its action is based on the value of the flag. Some of the flags are</p><p><strong>— error</strong>: An error is produced which has the impact of executing the built-in predicate at the same node.</p><pre>throw (existence..error (procedure. PI))</pre><p>where PI is predicate indicator</p><p><strong>— warning</strong>: A warning dependent on implementation is triggered and the current objective fails.</p><p><strong>— fail</strong>: it is a failure of the current objective.</p><p>In the cases of warning, fail the execution progresses as shown in backtracking.</p><blockquote><strong>C) The predication chosen is an integrated predicate</strong></blockquote><p>If the predicate(internal) produces side effects such as asserta/1 (or) cut/0, I/O actions, and table updates. The internal predicates with the side-effects mentioned will be performed and the further computation goes on to any one of the below cases.</p><p>— The built-in predicate is a logic and control built-in predicate not in error.</p><p>— The built-in predicate is deterministic and succeeds with local substitution.</p><p>— The built-in predicate is re-executable and has several possible successes<br>with possibly different local substitutions.</p><p>— The built-in predicate fails.</p><p>— The built-in predicate throws an error.</p><h4>Additional error situations</h4><ol><li><strong>System error → </strong>It may<strong> </strong>prevail at any part of the execution. The parameters under which a system error occurs and the actions a processor takes after a system error are dependent on implementation. The accompanying error-term is ‘<em>system_error’.</em></li></ol><p>2.<strong> Resource error</strong> → If the processor is short of required resources at any stage of execution, it throws this <em>‘resource error’. </em>The accompanying error-term is ‘ resource_error<em>’, </em>where Resource is independent of implementation.</p><h4>The side-effects of cut</h4><p>We have an integrated predicate “cut” (!/0), it basically operates by pruning the search tree. “Cut” always gets it right, but on the search — tree it has some adverse side effects: It deletes some search tree branches to push a prediction to quickly execute without designing and visiting all sub-search trees.</p><p>Consider a clause in a database</p><pre>M(A, B):- n(A), !, o(A, B)</pre><p>The M/2 clauses shall be :</p><pre>M(A, B):- n(A), !, o(A, B)<br>M(A, B):- p(X)</pre><p>The figure below that the search-tree accompanying to the goal M(U, V),<br>depicted in Figure, now has only one failed branch.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/450/1*gReaMW-I_BMIUp5A0mwp8A.jpeg" /></figure><p>The impact of the “cut” is therefore to remove all the dangling nodes between the current node and the parent node of the primary goal it contained. When backtracking takes place, all the nodes that have been made deterministic will be skipped. Note that the percentage of success branches can be increased by several “embedded cuts”.</p><p><strong>CONTRIBUTORS TO THIS GUIDE :</strong></p><p><em>Sai Kiran Reddy Surabhi @ </em><a href="https://twitter.com/saikira74573386"><em>Kiran</em></a></p><p><em>Sri Karan Davuluri @ </em><a href="https://twitter.com/KaranDavuluri"><em>Karan</em></a></p><p><em>Kalyan Nagavaram @ </em><a href="https://twitter.com/KalyanNagavaram"><em>Kalyan</em></a></p><p><em>Sree Harsha Donepudi @ </em><a href="https://twitter.com/donepudisree"><em>Harsha</em></a></p><p><em>Sankalp Mogulothu @ </em><a href="http://twitter.com/29_sankalp"><em>sankalp</em></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4afedf42b435" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Debugging and Testing: PROLOG]]></title>
            <link>https://medium.com/@karandavuluri/debugging-and-testing-prolog-9d269c7296d1?source=rss-1a38cc79fc0b------2</link>
            <guid isPermaLink="false">https://medium.com/p/9d269c7296d1</guid>
            <category><![CDATA[testing]]></category>
            <dc:creator><![CDATA[Karan Davuluri]]></dc:creator>
            <pubDate>Wed, 20 Mar 2019 07:03:23 GMT</pubDate>
            <atom:updated>2019-03-20T07:06:49.585Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*1Am3TAP8KJK7B9Xpzyv6zQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*1rpu9oIlsvVJeDX2kusY6Q.png" /><figcaption>PROLOG DEBUGGING</figcaption></figure><p>SWI-Prolog’s debugger is a command-line debugger which also provides access to the code. Graphical Debugger can be provided using this feature.</p><h4><em>Testing</em>:</h4><p>Testing the software during the development stage is probably the most important part of building the software. Testing is done to ensure that the code is used correctly. Testing can be used to validate the claims we make while implementing Prolog. By writing a test, the claim can be made explicit.</p><p><strong>Example: length of a list</strong></p><blockquote>Let’s consider a simple program with a Prolog error:</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/981/1*7TM2tn2Z9O9r1It94BOtgw.png" /></figure><p>Here we are describing the list and its corresponding length. At first, there seems the program work for many conditions as we assumed.</p><blockquote>For Example:</blockquote><p>The Query ?- l_len([ ], 0). Executes correctly as intended.</p><p>We write the program to get solutions.</p><pre>?- l_len(Ls, _).<br>St = [ ] ;<br>St = [_GA24] ;<br>St = [_GA52, _GW36 ] ;</pre><p>Actually, there are some mistakes in the program above.</p><h4>Testing nontermination</h4><p>The first example shows the required termination properties of the program. Let us consider a case of<strong> l_length / 2</strong> from the above example, we know that for general queries the program must not be terminated. Its because we assume the relationship is valid for a list of all lengths and there is no another way to get the report for this set with a finite number of answers.</p><p>The Prolog Query Q to study termination:</p><pre>?- Q, false.</pre><p>If and only if this query is not executed, then Q terminates universally. For the concrete case, we use:</p><pre>?- l_len(Ls, L), <strong>false.</strong></pre><p>Here, the programs run as expected and no terminations are seen. However, the properties of testing terminations are not enough to find all the mistakes. So we can try for other testing processes like concrete test cases.</p><h4><strong>Concrete test cases</strong></h4><p>Generally, the testing for pure programs in Prolog is very easy in this way: Start with simple try queries on the top level. In addition to these, the backtracking mechanism in Prolog helps in expressing more and more sets of concrete test cases at a time.</p><p>For example, as stated above we know that for lists of all lengths the relation should yield answers. In particular, it should succeed for lists in the form [ ], [ a ], [ a,a ], …..</p><p>Let us check for <strong>counterexample</strong> if exists:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/551/1*ONp7DZnk7cXKnoGhYWIH9w.png" /></figure><p>No counterexample reported. So, in this case, also the program performs as assumed. We can extend this and can be more generally for the above test cases for example to:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/735/1*fPx3jnYTbf5SOQGF1CDq7Q.png" /></figure><p>For the most generalization query also, No counterexample is reported.</p><h4><strong>Testing against a reference implementation</strong></h4><p>In addition to the above testing cases, we can test against a reference implementation. In our concrete case, this testing process is easily possible, because most of the programs in prolog ship with a predicate called length/2 this is the relation we are describing above. This testing process reveals an error in our program immediately.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/737/1*yvIM7tzkS2C0TSuHsNcp_A.png" /></figure><p>Generally, the reference implementation is not seen in most of the cases.</p><h4><strong>Testing declarative properties</strong></h4><p>By using further properties we can test the relationship and checks for if the relationship between them is implemented correctly.</p><blockquote>For example</blockquote><p>If the length of a list Ls is K, then the length of [_lLs ] is K+1. The counterexample of this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/621/1*ujhE4ysc031N_m1sjyPxmQ.png" /></figure><p>Here, the length of [_] should be 1 but is showing as 2(incorrect). The length of [ ] or the length of [ _ ] is not shown correctly. In particular, the below query incorrectly fails:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/580/1*5ajjrhsJEsPrEt9kOl0jEQ.png" /></figure><h3>DEBUGGING</h3><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2F-OnE4Owf7xE%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D-OnE4Owf7xE&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2F-OnE4Owf7xE%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/94d1d65a2e768727389db04ce31047d0/href">https://medium.com/media/94d1d65a2e768727389db04ce31047d0/href</a></iframe><p>→ Debugging is useful in identifying the errors/mistakes in the code. Declarative debugging is one of the mechanisms to identify those mistakes made by the coder.</p><p>→ <strong><em>Tracing</em></strong>: Following are the commands to trace programs in the Prolog environment:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KwCzI47YignhyKNgXtpfvQ.png" /></figure><p><strong>→ Drawbacks of tracing:</strong> Major drawback of tracers is that they involve mistakes too. Apart from the errors in the user’s code, tracer also sometimes has errors. The alternate solution to such a procedural approach is a declarative approach, they provide us with the advantage of finding mistakes quickly.</p><h4><strong>Mistakes in Prolog code:</strong></h4><p>→ These mistakes are classified into 2 main types:</p><p>1. too general — gives unnecessary solutions.</p><p>2. too specific — unreported solutions.</p><p><strong>Example:</strong> The following sample code contains a mistake:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/925/1*qiqVPF_Voug8NGwFP4E4TQ.png" /></figure><blockquote><em>a) Too general case:</em></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/672/1*YT9VYzIgOOQS4aQBBENW8A.png" /></figure><blockquote><em>b) Too specific case:</em></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/873/1*txd83h4Q7YJBixOq3chDBQ.png" /></figure><h4><strong>Finding Mistakes:</strong></h4><p>To locate the mistakes in the prolog code, we trace the code step by step and see if the given input satisfies the conditions in the programs. The debugger finds the mistake in the code and highlights and flags the conditions when there is a mistake.</p><h3>The UNIT testing framework in PROLOG</h3><p>Testing is coded in Prolog, and it is included in this way stating with begin_tests(<em>name</em>) and ends with end_tests(<em>name</em>). This can be framed inside a normal module, or they can be in a different test case file that calls the files to be tested. The starting point is defined by a set of rules — test (+<em>name</em>) OR test (+<em>name</em>, +<em>options</em>). Here “options” justifies additional features of the test case.</p><blockquote>Consider the example below:</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/733/1*Y8w6kcMmm8tplYJafHSsww.png" /></figure><p>The second argument (‘options’) carries different additional options.</p><blockquote>Have a look at some of them</blockquote><p><strong>Blocked(+<em>reason:atom</em>)</strong></p><p>Here<strong> </strong>the test will be disabled. If a test can’t be run for some reason, tests are marked as blocked.</p><p><strong>fixme</strong>(<em>+Reason:atom</em>)</p><p>It is identical to the above à blocked(reason). If a failure occurs ‘-‘ is printed rather than character</p><p><strong>condition</strong>(<em>:Goal</em>)</p><p>It is basically a pre-condition before running the test. When the condition fails à test will be skipped. As an alternative solution to the ‘setup’ option, the condition can be used. So, the only distinction is that a condition’s failure skips the test and when using the ‘setup’ option is considered an error.</p><p><strong>cleanup</strong>(:<em>Goal</em>)</p><p>The Goal is often called just after the test — the body has been completed, irrespective of whether an exception fails succeeds or throws. This option should be used by tests requiring side effects to be changed back after completion of the test.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/767/1*FqaVNTliFy0nYJBXcOlKSA.png" /></figure><p><strong>true </strong>(<em>AnswerTerm Cmp Value</em>)</p><p>The body has to be succeeded deterministically. Using the Cmp comparison operator, AnswerTerm is contrasted to value.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/752/1*rBiKei1sKbH9A6O14wMeGw.png" /></figure><p><strong>fail → </strong>It must fail.</p><h4>Writing the test body</h4><p>The code for testing in PROLOG is an ordinary prolog code which should be able to execute without any options and must succeed deterministically. Options such as fail, true,etc.… can be used for different results.</p><h4>Testing deterministic predicates</h4><p>These are the predicates that are succeeded only once and there are no other choices. These usually have one or more inputs. The test program inputs proper values for argument and verifies the output.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/760/1*JJcbNO_HSmTtGHJiCrorEw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/745/1*1hv5gMh5trTON6_627JY_Q.png" /></figure><h4>Testing semi-deterministic predicates</h4><p>There is the type of predicates that succeed or fail exactly one and has no other choices. Testing them is similar to testing deterministic predicates.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/765/1*o29HBleCNLsjlCd7qrFUPw.png" /></figure><h4>Testing error conditions</h4><p>The throws(error) option is used for testing error conditions.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/797/1*v-6y8UN-89vsr0UKgImnuw.png" /></figure><h4>Using separate test files</h4><p>The tests can be written in normal prolog source code and they can also be placed in another file. Test files use a separate extension called .plt (which refers to prolog tests).</p><h4>Running the test suite</h4><p>For running the test suite we must initially load the program and then run to using the command run_tests(+unit). If you want to run some specified tests we can give the name of the test as a parameter to the function run_tests. This is used for tracing a specific test as demonstrated in the <a href="https://www.youtube.com/watch?v=-OnE4Owf7xE"><strong><em>video</em></strong></a><strong><em>.</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/631/1*-0Ou93bmQwcXYGRiTARHzw.png" /></figure><h4>Tests and production systems</h4><p>As we don’t require the test suite to be available with the exported application, one method to avoid this is — place all the tests in separate files and don’t load this during the running of the final application. We can also use the test option as shown below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/541/1*BeR1sKyd52lADIwcJePFAA.png" /></figure><h3>Controlling the test suite</h3><blockquote>Some of the test suite options are</blockquote><p><strong>Silent(+<em>boot</em>) → </strong>when this option is enabled there is no output statements that are failed, only the output is displayed for the errors.</p><p>There are other options like sto, load and run.</p><p><strong>Test_report(+<em>what</em>) → </strong>When the test is executed and need to print the report, this can be used.</p><h4>Coverage analysis</h4><p>This shows the coverage report of our code. This is run using the command show_coverage(:<em>Goal</em>).</p><h4>Portability of the test-suite</h4><p>The tests which are written for the application needs to have less integration for the application so that they can be portable so that they can test the application in different environments. Some of the options as mentions before such as load — is used to avoid the automatic loading of the test suites.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9d269c7296d1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Guide to PROLOG — a logic programming language]]></title>
            <link>https://medium.com/@karandavuluri/guide-to-prolog-a-logic-programming-language-4e1042b5eee2?source=rss-1a38cc79fc0b------2</link>
            <guid isPermaLink="false">https://medium.com/p/4e1042b5eee2</guid>
            <category><![CDATA[programming-languages]]></category>
            <category><![CDATA[uh]]></category>
            <category><![CDATA[prolog]]></category>
            <dc:creator><![CDATA[Karan Davuluri]]></dc:creator>
            <pubDate>Mon, 18 Feb 2019 08:45:07 GMT</pubDate>
            <atom:updated>2019-03-05T04:03:38.463Z</atom:updated>
            <content:encoded><![CDATA[<h3>Guide to PROLOG — a logic programming language</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*k9uEnH8FBGhTXggGEE4tlg.png" /><figcaption>SWI-Prolog</figcaption></figure><p><strong>Introduction</strong></p><p>In our arrangement on AI innovations and their significance for society, we are presently taking a gander at a case of what is known as the symbolic way to answer the man-made reasoning.</p><p>Prolog, a programming language created toward the start of the 1970s, combines this symbolic methodology with fundamental ideas from formal logic, to make it conceivable to program PCs “decisively.” Most regular programming languages are imperative languages</p><p>Prolog (programming in logic) is one of the established programming languages created explicitly for use in Artificial Intelligence. Rather than basic imperative languages, for example, other programming languages(the last of which likewise happens to be object-arranged) it is a declarative language. This means, when you execute the answer for a problem, of indicating how to accomplish a specific objective under a certain condition, you can find what the condition(guidelines and certainties) and the objectives (question) are and given the Prolog translator a way to determine the solution. Prolog is specifically useful for certain critical thinking that is being undertaken in AI, in areas, for example, search, planning, and knowledge representation.</p><h3>Installing Prolog on your machine</h3><p><strong><em>Installation steps for Windows:</em></strong></p><p>1. The initial step is to download SWI-Prolog from the link <a href="http://www.swi-prolog.org/download/stable">here <br> </a><strong>(NOTE</strong>: For Windows: It is highly preferable to download the 64-bit edition.)</p><p>2. After downloading the swipl-.x64.exe file, run the executable file following the instructions.</p><p>3. Make sure that you select “ add swipl to the system PATH for all users” during installation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/499/1*r682PdYKSbBJjc5kcZFZRg.png" /></figure><p>4. Select the path where you want to install Prolog and continue to the next step.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/495/1*0-pl8Ee8IZRn_OOItSV1mA.png" /></figure><p>5. Upon completing the setup process, you will be able to access Prolog from the command prompt.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*qWmjgDSMiVrLUAfFlNE33w.png" /></figure><p><strong><em>Installation steps for Linux (Ubuntu):</em></strong></p><p>1. Firstly, add the “ppa” which abbreviates to “Personal Package Archive”.<br>- ppa swi-prolog/stable, you are required to add ppa to system’s software sources.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/720/1*B0EJ8oDmSC53YnHfg3OYbQ.png" /></figure><p>Now, follow the below steps:</p><p>In a new terminal window (Shortcut: Ctrl+Alt+T) and input<br>the following command:</p><pre>$ sudo apt-get update</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/712/1*1Cl9NlTBJJD_DTGpwpIASA.png" /></figure><p>Now, you’ll need to update the package information</p><p>2. Install SWI-Prolog by using the newly installed package manager.<br> Open the terminal and input the following command:</p><pre>sudo apt-get install swi-prolog</pre><p>3. After successful installation, verify by typing “swipl” command in terminal</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/647/1*-Fm7lzojgNjilD-W4CTtwA.png" /></figure><p><strong><em>Writing your first program in Prolog: (Linux)</em></strong></p><p><strong>Method (1):</strong><br>1. Create a new prolog program with file extension as “.pl”.<br>(EX: first_program.pl)</p><p>2. Now open a new terminal and proceed to the directory of your Prolog program.</p><p>3. Use the command “swipl” to open SWI-Prolog.</p><p>4. To load the program, use the command [first_program]. <strong>Note</strong>: Only mention the file name in the above command without the file extension.</p><p>5. Command to terminate SWI-prolog is “halt”.</p><p><strong>Method (2):<br> </strong>1. Create a new file i.e. “testing.pl” with any content you want to display:</p><pre>:- initialization testing, halt.</pre><pre>hello_world :-<br>    write(&#39;Hello, World!&#39;), nl.</pre><p>The saved file can be executed by the PROLOG Gui that is already installed. Follow the upcoming steps below</p><p>2. Run the below command</p><pre>$ swipl -q -l testing.pl</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*suk7QNyztSPaHMgZMXxk9w.png" /></figure><h4>Package Manager for Prolog</h4><p>The <strong><em>library(prolog_pack)</em></strong> handles the packages in PROLOG.</p><p>Through this library, you can add/remove packages etc. It is complemented by the built-in <em>attach packs</em> that ensures packages installed are shown as libraries.</p><ol><li>This command lets you know the locally installed packages — pack_list/1</li><li>If you want to display all the packages available on the internet, then use pack_list(&#39;&#39;)</li><li>To install a new package, use the command below</li></ol><pre>pack_install(package_name,[upgrade(true)])</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/882/1*HqQybBSohC5a1F0KtBEytQ.png" /></figure><p>4. pack_list(+Query) — It queries the package server for any installed packages and shows the status for the packages. For each identified package, a single row of information is displayed which contains:</p><p>a) <em>Installation status</em></p><p>→ p: package, is the package installed or not.</p><p>→ i: the package is installed and up to date.</p><p>→ U: an update is available for the installed package.</p><p>→ A: a Newer version is installed than that is publicly available.</p><p>→ l: installed package; not on the server</p><p><strong>CONTRIBUTORS TO THIS GUIDE :</strong></p><p><em>Sai Kiran Reddy Surabhi @ </em><a href="https://twitter.com/saikira74573386"><em>Kiran</em></a></p><p><em>Kalyan Nagavaram @ </em><a href="https://twitter.com/KalyanNagavaram"><em>Kalyan</em></a></p><p><em>Sri Karan Davuluri @ </em><a href="https://twitter.com/KaranDavuluri"><em>Karan</em></a></p><p><em>Sree Harsha Donepudi @ </em><a href="https://twitter.com/donepudisree"><em>Harsha</em></a></p><p><em>Sankalp Mogulothu @ </em><a href="http://twitter.com/29_sankalp"><em>sankalp</em></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4e1042b5eee2" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>