Prolog – Basic Programs

Prolog – Basic Programs ”; Previous Next In the following chapter, we are going to discuss basic prolog examples to − Find minimum maximum of two numbers Find the equivalent resistance of a resistive circuit Verify whether a line segment is horizontal, vertical or oblique Max and Min of two numbers Here we will see one Prolog program, that can find the minimum of two numbers and the maximum of two numbers. First, we will create two predicates, find_max(X,Y,Max). This takes X and Y values, and stores the maximum value into the Max. Similarly find_min(X,Y,Min) takes X and Y values, and store the minimum value into the Min variable. Program find_max(X, Y, X) :- X >= Y, !. find_max(X, Y, Y) :- X < Y. find_min(X, Y, X) :- X =< Y, !. find_min(X, Y, Y) :- X > Y. Output | ?- find_max(100,200,Max). Max = 200 yes | ?- find_max(40,10,Max). Max = 40 yes | ?- find_min(40,10,Min). Min = 10 yes | ?- find_min(100,200,Min). Min = 100 yes | ?- Resistance and Resistive Circuits In this section, we will see how to write a prolog program that will help us find the equivalent resistance of a resistive circuit. Let us consider the following circuit to understand this concept − We have to find the equivalent resistance of this network. At first, we will try to get the result by hand, then try to see whether the result is matching with the prolog output or not. We know that there are two rules − If R1 and R2 are in Series, then equivalent resistor Re = R1 + R2. If R1 and R2 are in Parallel, then equivalent resistor Re = (R1 * R2)/(R1 + R2). Here 10 Ohm and 40 Ohm resistors are in parallel, then that is in series with 12 Ohm, and the equivalent resistor of the lower half is parallel with 30 Ohm. So let’s try to calculate the equivalent resistance. R3 = (10 * 40)/(10 + 40) = 400/50 = 8 Ohm R4 = R3 + 12 = 8 + 12 = 20 Ohm R5 = (20 * 30)/(20 + 30) = 12 Ohm Program series(R1,R2,Re) :- Re is R1 + R2. parallel(R1,R2,Re) :- Re is ((R1 * R2) / (R1 + R2)). Output | ?- [resistance]. compiling D:/TP Prolog/Sample_Codes/resistance.pl for byte code… D:/TP Prolog/Sample_Codes/resistance.pl compiled, 1 lines read – 804 bytes written, 14 ms yes | ?- parallel(10,40,R3). R3 = 8.0 yes | ?- series(8,12,R4). R4 = 20 yes | ?- parallel(20,30,R5). R5 = 12.0 yes | ?- parallel(10,40,R3),series(R3,12,R4),parallel(R4,30,R5). R3 = 8.0 R4 = 20.0 R5 = 12.0 yes | ?- Horizontal and Vertical Line Segments There are three types of line segments, horizontal, vertical or oblique. This example verifies whether a line segment is horizontal, vertical or oblique. From this diagram we can understand that − For Horizontal lines, the y coordinate values of two endpoints are same. For Vertical lines, the x coordinate values of two endpoints are same. For Oblique lines, the (x,y) coordinates of two endpoints are different. Now let us see how to write a program to check this. Program vertical(seg(point(X,_),point(X,_))). horizontal(seg(point(_,Y),point(_,Y))). oblique(seg(point(X1,Y1),point(X2,Y2))) :-X1 == X2, Y1 == Y2. Output | ?- [line_seg]. compiling D:/TP Prolog/Sample_Codes/line_seg.pl for byte code… D:/TP Prolog/Sample_Codes/line_seg.pl compiled, 6 lines read – 1276 bytes written, 26 ms yes | ?- vertical(seg(point(10,20), point(10,30))). yes | ?- vertical(seg(point(10,20), point(15,30))). no | ?- oblique(seg(point(10,20), point(15,30))). yes | ?- oblique(seg(point(10,20), point(15,20))). no | ?- horizontal(seg(point(10,20), point(15,20))). yes | ?- Print Page Previous Next Advertisements ”;

Recursion and Structures

Prolog – Recursion and Structures ”; Previous Next This chapter covers recursion and structures. Recursion Recursion is a technique in which one predicate uses itself (may be with some other predicates) to find the truth value. Let us understand this definition with the help of an example − is_digesting(X,Y) :- just_ate(X,Y). is_digesting(X,Y) :-just_ate(X,Z),is_digesting(Z,Y). So this predicate is recursive in nature. Suppose we say that just_ate(deer, grass), it means is_digesting(deer, grass) is true. Now if we say is_digesting(tiger, grass), this will be true if is_digesting(tiger, grass) :- just_ate(tiger, deer), is_digesting(deer, grass), then the statement is_digesting(tiger, grass) is also true. There may be some other examples also, so let us see one family example. So if we want to express the predecessor logic, that can be expressed using the following diagram − So we can understand the predecessor relationship is recursive. We can express this relationship using the following syntax − predecessor(X, Z) :- parent(X, Z). predecessor(X, Z) :- parent(X, Y),predecessor(Y, Z). Structures Structures are Data Objects that contain multiple components. For example, the date can be viewed as a structure with three components — day, month and year. Then the date 9th April, 2020 can be written as: date(9, apr, 2020). Note − Structure can in turn have another structure as a component in it. So we can see views as tree structure and Prolog Functors. Now let us see one example of structures in Prolog. We will define a structure of points, Segments and Triangle as structures. To represent a point, a line segment and a triangle using structure in Prolog, we can consider following statements − p1 − point(1, 1) p2 − point(2,3) S − seg( Pl, P2): seg( point(1,1), point(2,3)) T − triangle( point(4,Z), point(6,4), point(7,1) ) Note − Structures can be naturally pictured as trees. Prolog can be viewed as a language for processing trees. Matching in Prolog Matching is used to check whether two given terms are same (identical) or the variables in both terms can have the same objects after being instantiated. Let us see one example. Suppose date structure is defined as date(D,M,2020) = date(D1,apr, Y1), this indicates that D = D1, M = feb and Y1 = 2020. Following rules are to be used to check whether two terms S and T match − If S and T are constants, S=T if both are same objects. If S is a variable and T is anything, T=S. If T is variable and S is anything, S=T. If S and T are structures, S=T if − S and T have same functor. All their corresponding arguments components have to match. Binary Trees Following is the structure of binary tree using recursive structures − The definition of the structure is as follows − node(2, node(1,nil,nil), node(6, node(4,node(3,nil,nil), node(5,nil,nil)), node(7,nil,nil)) Each node has three fields, data and two nodes. One node with no child (leaf node) structure is written as node(value, nil, nil), node with only one left child is written as node(value, left_node, nil), node with only one right child is written as node(value, nil; right_node), and node with both child has node(value, left_node, right_node). Print Page Previous Next Advertisements ”;

Conjunctions & Disjunctions

Prolog – Conjunctions & Disjunctions ”; Previous Next In this chapter, we shall discuss Conjunction and Disjunction properties. These properties are used in other programming languages using AND and OR logics. Prolog also uses the same logic in its syntax. Conjunction Conjunction (AND logic) can be implemented using the comma (,) operator. So two predicates separated by comma are joined with AND statement. Suppose we have a predicate, parent(jhon, bob), which means “Jhon is parent of Bob”, and another predicate, male(jhon), which means “Jhon is male”. So we can make another predicate that father(jhon,bob), which means “Jhon is father of Bob”. We can define predicate father, when he is parent AND he is male. Disjunction Disjunction (OR logic) can be implemented using the semi-colon (;) operator. So two predicates separated by semi-colon are joined with OR statement. Suppose we have a predicate, father(jhon, bob). This tells that “Jhon is father of Bob”, and another predicate, mother(lili,bob), this tells that “lili is mother of bob”. If we create another predicate as child(), this will be true when father(jhon, bob) is true OR mother(lili,bob) is true. Program parent(jhon,bob). parent(lili,bob). male(jhon). female(lili). % Conjunction Logic father(X,Y) :- parent(X,Y),male(X). mother(X,Y) :- parent(X,Y),female(X). % Disjunction Logic child_of(X,Y) :- father(X,Y);mother(X,Y). Output | ?- [conj_disj]. compiling D:/TP Prolog/Sample_Codes/conj_disj.pl for byte code… D:/TP Prolog/Sample_Codes/conj_disj.pl compiled, 11 lines read – 1513 bytes written, 24 ms yes | ?- father(jhon,bob). yes | ?- child_of(jhon,bob). true ? yes | ?- child_of(lili,bob). yes | ?- Print Page Previous Next Advertisements ”;

Prolog – Examples of Cuts

Prolog – Examples of Cuts ”; Previous Next In this section, we will see some examples of cuts in prolog. Let us consider, we want to find the maximum of two elements. So we will check these two conditions. If X > Y, then Max := X if X <= Y, then Max := Y Now from these two lines, we can understand that these two statements are mutually exclusive, so when one is true, another one must be false. In such cases we can use the cut. So let us see the program. We can also define a predicate where we use the two cases using disjunction (OR logic). So when first one satisfies, it does not check for the second one, otherwise, it will check for the second statement. Program max(X,Y,X) :- X >= Y,!. max(X,Y,Y) :- X < Y. max_find(X,Y,Max) :- X >= Y,!, Max = X; Max = Y. Output | ?- [cut_example]. 1 1 Call: [cut_example] ? compiling D:/TP Prolog/Sample_Codes/cut_example.pl for byte code… D:/TP Prolog/Sample_Codes/cut_example.pl compiled, 3 lines read – 1195 bytes written, 43 ms 1 1 Exit: [cut_example] ? yes {trace} | ?- max(10,20,Max). 1 1 Call: max(10,20,_23) ? 2 2 Call: 10>=20 ? 2 2 Fail: 10>=20 ? 2 2 Call: 10<20 ? 2 2 Exit: 10<20 ? 1 1 Exit: max(10,20,20) ? Max = 20 yes {trace} | ?- max_find(20,10,Max). 1 1 Call: max_find(20,10,_23) ? 2 2 Call: 20>=10 ? 2 2 Exit: 20>=10 ? 1 1 Exit: max_find(20,10,20) ? Max = 20 yes {trace} | ?- Program Let us see another example, where we will use list. In this program we will try to insert an element into a list, if it is not present in the list before. And if the list has the element before we will simply cut it. For the membership checking also, if the item is at the head part, we should not check further, so cut it, otherwise check into the tail part. list_member(X,[X|_]) :- !. list_member(X,[_|TAIL]) :- list_member(X,TAIL). list_append(A,T,T) :- list_member(A,T),!. list_append(A,T,[A|T]). Output | ?- [cut_example]. compiling D:/TP Prolog/Sample_Codes/cut_example.pl for byte code… D:/TP Prolog/Sample_Codes/cut_example.pl compiled, 9 lines read – 1954 bytes written, 15 ms yes | ?- trace. The debugger will first creep — showing everything (trace) yes {trace} | ?- list_append(a,[a,b,c,d,e], L). 1 1 Call: list_append(a,[a,b,c,d,e],_33) ? 2 2 Call: list_member(a,[a,b,c,d,e]) ? 2 2 Exit: list_member(a,[a,b,c,d,e]) ? 1 1 Exit: list_append(a,[a,b,c,d,e],[a,b,c,d,e]) ? L = [a,b,c,d,e] yes {trace} | ?- list_append(k,[a,b,c,d,e], L). 1 1 Call: list_append(k,[a,b,c,d,e],_33) ? 2 2 Call: list_member(k,[a,b,c,d,e]) ? 3 3 Call: list_member(k,[b,c,d,e]) ? 4 4 Call: list_member(k,[c,d,e]) ? 5 5 Call: list_member(k,[d,e]) ? 6 6 Call: list_member(k,[e]) ? 7 7 Call: list_member(k,[]) ? 7 7 Fail: list_member(k,[]) ? 6 6 Fail: list_member(k,[e]) ? 5 5 Fail: list_member(k,[d,e]) ? 4 4 Fail: list_member(k,[c,d,e]) ? 3 3 Fail: list_member(k,[b,c,d,e]) ? 2 2 Fail: list_member(k,[a,b,c,d,e]) ? 1 1 Exit: list_append(k,[a,b,c,d,e],[k,a,b,c,d,e]) ? L = [k,a,b,c,d,e] (16 ms) yes {trace} | ?- Print Page Previous Next Advertisements ”;

Prolog – Backtracking

Prolog – Backtracking ”; Previous Next In this chapter, we will discuss the backtracking in Prolog. Backtracking is a procedure, in which prolog searches the truth value of different predicates by checking whether they are correct or not. The backtracking term is quite common in algorithm designing, and in different programming environments. In Prolog, until it reaches proper destination, it tries to backtrack. When the destination is found, it stops. Let us see how backtracking takes place using one tree like structure − Suppose A to G are some rules and facts. We start from A and want to reach G. The proper path will be A-C-G, but at first, it will go from A to B, then B to D. When it finds that D is not the destination, it backtracks to B, then go to E, and backtracks again to B, as there is no other child of B, then it backtracks to A, thus it searches for G, and finally found G in the path A-C-G. (Dashed lines are indicating the backtracking.) So when it finds G, it stops. How Backtracking works? Now we know, what is the backtracking in Prolog. Let us see one example, Note − While we are running some prolog code, during backtracking there may be multiple answers, we can press semicolon (;) to get next answers one by one, that helps to backtrack. Otherwise when we get one result, it will stop. Now, consider a situation, where two people X and Y can pay each other, but the condition is that a boy can pay to a girl, so X will be a boy, and Y will be a girl. So for these we have defined some facts and rules − Knowledge Base boy(tom). boy(bob). girl(alice). girl(lili). pay(X,Y) :- boy(X), girl(Y). Following is the illustration of the above scenario − As X will be a boy, so there are two choices, and for each boy there are two choices alice and lili. Now let us see the output, how backtracking is working. Output | ?- [backtrack]. compiling D:/TP Prolog/Sample_Codes/backtrack.pl for byte code… D:/TP Prolog/Sample_Codes/backtrack.pl compiled, 5 lines read – 703 bytes written, 22 ms yes | ?- pay(X,Y). X = tom Y = alice ? (15 ms) yes | ?- pay(X,Y). X = tom Y = alice ? ; X = tom Y = lili ? ; X = bob Y = alice ? ; X = bob Y = lili yes | ?- trace. The debugger will first creep — showing everything (trace) (16 ms) yes {trace} | ?- pay(X,Y). 1 1 Call: pay(_23,_24) ? 2 2 Call: boy(_23) ? 2 2 Exit: boy(tom) ? 3 2 Call: girl(_24) ? 3 2 Exit: girl(alice) ? 1 1 Exit: pay(tom,alice) ? X = tom Y = alice ? ; 1 1 Redo: pay(tom,alice) ? 3 2 Redo: girl(alice) ? 3 2 Exit: girl(lili) ? 1 1 Exit: pay(tom,lili) ? X = tom Y = lili ? ; 1 1 Redo: pay(tom,lili) ? 2 2 Redo: boy(tom) ? 2 2 Exit: boy(bob) ? 3 2 Call: girl(_24) ? 3 2 Exit: girl(alice) ? 1 1 Exit: pay(bob,alice) ? X = bob Y = alice ? ; 1 1 Redo: pay(bob,alice) ? 3 2 Redo: girl(alice) ? 3 2 Exit: girl(lili) ? 1 1 Exit: pay(bob,lili) ? X = bob Y = lili yes {trace} | ?- Preventing Backtracking So far we have seen some concepts of backtracking. Now let us see some drawbacks of backtracking. Sometimes we write the same predicates more than once when our program demands, for example to write recursive rules or to make some decision making systems. In such cases uncontrolled backtracking may cause inefficiency in a program. To resolve this, we will use the Cut in Prolog. Suppose we have some rules as follows − Double step function Rule 1 &minnus; if X < 3 then Y = 0 Rule 2 &minnus; if 3 <= X and X < 6 then Y = 2 Rule 3 &minnus; if 6 <= X then Y = 4 In Prolog syntax we can write, f(X,0) :- X < 3. % Rule 1 f(X,2) :- 3 =< X, X < 6. % Rule 2 f(X,4) :- 6 =< X. % Rule 3 Now if we ask for a question as f (1,Y), 2 The first goal f(1,Y) instantiated Y to 0. The second goal becomes 2 < 0 which fails. Prolog tries through backtracking two unfruitful alternatives (Rule 2 and Rule 3). If we see closer, we can observe that − The three rules are mutually exclusive and one of them at most will succeed. As soon as one of them succeeds there is no point in trying to use the others as they are bound to fail. So we can use cut to resolve this. The cut can be expressed using Exclamation symbol. The prolog syntax is as follows − f (X,0) :- X < 3, !. &percnt; Rule 1 f (X,2) :- 3 =< X, X < 6, !. &percnt; Rule 2 f (X,4) :- 6 =< X. &percnt; Rule 3 Now if we use the same question, ?- f (1,Y), 2 < Y. Prolog choose rule 1 since 1 < 3 and fails the goal 2 < Y fails. Prolog will try to backtrack, but not beyond the point marked ! In the program, rule 2 and rule 3 will not be generated. Let us see this in below execution − Program f(X,0) :- X < 3. &percnt; Rule 1 f(X,2) :- 3 =< X, X < 6. &percnt; Rule 2 f(X,4) :- 6 =< X. &percnt; Rule 3 Output | ?- [backtrack]. compiling D:/TP Prolog/Sample_Codes/backtrack.pl for byte code… D:/TP Prolog/Sample_Codes/backtrack.pl compiled, 10 lines read – 1224 bytes written, 17 ms yes | ?- f(1,Y), 2<Y. no | ?- trace . The debugger will first creep — showing everything (trace) yes {trace} | ?- f(1,Y), 2<Y. 1 1 Call: f(1,_23) ? 2 2 Call:

Prolog – Basics

Prolog – Basics ”; Previous Next In this chapter, we will gain some basic knowledge about Prolog. So we will move on to the first step of our Prolog Programming. The different topics that will be covered in this chapter are − Knowledge Base − This is one of the fundamental parts of Logic Programming. We will see in detail about the Knowledge Base, and how it helps in logic programming. Facts, Rules and Queries − These are the building blocks of logic programming. We will get some detailed knowledge about facts and rules, and also see some kind of queries that will be used in logic programming. Here, we will discuss about the essential building blocks of logic programming. These building blocks are Facts, Rules and the Queries. Facts We can define fact as an explicit relationship between objects, and properties these objects might have. So facts are unconditionally true in nature. Suppose we have some facts as given below − Tom is a cat Kunal loves to eat Pasta Hair is black Nawaz loves to play games Pratyusha is lazy. So these are some facts, that are unconditionally true. These are actually statements, that we have to consider as true. Following are some guidelines to write facts − Names of properties/relationships begin with lower case letters. The relationship name appears as the first term. Objects appear as comma-separated arguments within parentheses. A period “.” must end a fact. Objects also begin with lower case letters. They also can begin with digits (like 1234), and can be strings of characters enclosed in quotes e.g. color(penink, ‘red’). phoneno(agnibha, 1122334455). is also called a predicate or clause. Syntax The syntax for facts is as follows − relation(object1,object2…). Example Following is an example of the above concept − cat(tom). loves_to_eat(kunal,pasta). of_color(hair,black). loves_to_play_games(nawaz). lazy(pratyusha). Rules We can define rule as an implicit relationship between objects. So facts are conditionally true. So when one associated condition is true, then the predicate is also true. Suppose we have some rules as given below − Lili is happy if she dances. Tom is hungry if he is searching for food. Jack and Bili are friends if both of them love to play cricket. will go to play if school is closed, and he is free. So these are some rules that are conditionally true, so when the right hand side is true, then the left hand side is also true. Here the symbol ( :- ) will be pronounced as “If”, or “is implied by”. This is also known as neck symbol, the LHS of this symbol is called the Head, and right hand side is called Body. Here we can use comma (,) which is known as conjunction, and we can also use semicolon, that is known as disjunction. Syntax rule_name(object1, object2, …) :- fact/rule(object1, object2, …) Suppose a clause is like : P :- Q;R. This can also be written as P :- Q. P :- R. If one clause is like : P :- Q,R;S,T,U. Is understood as P :- (Q,R);(S,T,U). Or can also be written as: P :- Q,R. P :- S,T,U. Example happy(lili) :- dances(lili). hungry(tom) :- search_for_food(tom). friends(jack, bili) :- lovesCricket(jack), lovesCricket(bili). goToPlay(ryan) :- isClosed(school), free(ryan). Queries Queries are some questions on the relationships between objects and object properties. So question can be anything, as given below − Is tom a cat? Does Kunal love to eat pasta? Is Lili happy? Will Ryan go to play? So according to these queries, Logic programming language can find the answer and return them. Knowledge Base in Logic Programming In this section, we will see what knowledge base in logic programming is. Well, as we know there are three main components in logic programming − Facts, Rules and Queries. Among these three if we collect the facts and rules as a whole then that forms a Knowledge Base. So we can say that the knowledge base is a collection of facts and rules. Now, we will see how to write some knowledge bases. Suppose we have our very first knowledge base called KB1. Here in the KB1, we have some facts. The facts are used to state things, that are unconditionally true of the domain of interest. Knowledge Base 1 Suppose we have some knowledge, that Priya, Tiyasha, and Jaya are three girls, among them, Priya can cook. Let’s try to write these facts in a more generic way as shown below − girl(priya). girl(tiyasha). girl(jaya). can_cook(priya). Note − Here we have written the name in lowercase letters, because in Prolog, a string starting with uppercase letter indicates a variable. Now we can use this knowledge base by posing some queries. “Is priya a girl?”, it will reply “yes”, “is jamini a girl?” then it will answer “No”, because it does not know who jamini is. Our next question is “Can Priya cook?”, it will say “yes”, but if we ask the same question for Jaya, it will say “No”. Output GNU Prolog 1.4.5 (64 bits) Compiled Jul 14 2018, 13:19:42 with x86_64-w64-mingw32-gcc By Daniel Diaz Copyright (C) 1999-2018 Daniel Diaz | ?- change_directory(”D:/TP Prolog/Sample_Codes”). yes | ?- [kb1] . compiling D:/TP Prolog/Sample_Codes/kb1.pl for byte code… D:/TP Prolog/Sample_Codes/kb1.pl compiled, 3 lines read – 489 bytes written, 10 ms yes | ?- girl(priya) . yes | ?- girl(jamini). no | ?- can_cook(priya). yes | ?- can_cook(jaya). no | ?- Let us see another knowledge base, where we have some rules. Rules contain some information that are conditionally true about the domain of interest. Suppose our knowledge base is as follows − sing_a_song(ananya). listens_to_music(rohit). listens_to_music(ananya) :- sing_a_song(ananya). happy(ananya) :- sing_a_song(ananya). happy(rohit) :- listens_to_music(rohit). playes_guitar(rohit) :- listens_to_music(rohit). So there are some facts and rules given above. The first two are facts, but the rest are rules. As we know that Ananya sings a song, this implies she also listens to

Prolog – Introduction

Prolog – Introduction ”; Previous Next Prolog as the name itself suggests, is the short form of LOGical PROgramming. It is a logical and declarative programming language. Before diving deep into the concepts of Prolog, let us first understand what exactly logical programming is. Logic Programming is one of the Computer Programming Paradigm, in which the program statements express the facts and rules about different problems within a system of formal logic. Here, the rules are written in the form of logical clauses, where head and body are present. For example, H is head and B1, B2, B3 are the elements of the body. Now if we state that “H is true, when B1, B2, B3 all are true”, this is a rule. On the other hand, facts are like the rules, but without any body. So, an example of fact is “H is true”. Some logic programming languages like Datalog or ASP (Answer Set Programming) are known as purely declarative languages. These languages allow statements about what the program should accomplish. There is no such step-by-step instruction on how to perform the task. However, other languages like Prolog, have declarative and also imperative properties. This may also include procedural statements like “To solve the problem H, perform B1, B2 and B3”. Some logic programming languages are given below − ALF (algebraic logic functional programming language). ASP (Answer Set Programming) CycL Datalog FuzzyCLIPS Janus Parlog Prolog Prolog++ ROOP Logic and Functional Programming We will discuss about the differences between Logic programming and the traditional functional programming languages. We can illustrate these two using the below diagram − From this illustration, we can see that in Functional Programming, we have to define the procedures, and the rule how the procedures work. These procedures work step by step to solve one specific problem based on the algorithm. On the other hand, for the Logic Programming, we will provide knowledge base. Using this knowledge base, the machine can find answers to the given questions, which is totally different from functional programming. In functional programming, we have to mention how one problem can be solved, but in logic programming we have to specify for which problem we actually want the solution. Then the logic programming automatically finds a suitable solution that will help us solve that specific problem. Now let us see some more differences below − Functional Programming Logic Programming Functional Programming follows the Von-Neumann Architecture, or uses the sequential steps. Logic Programming uses abstract model, or deals with objects and their relationships. The syntax is actually the sequence of statements like (a, s, I). The syntax is basically the logic formulae (Horn Clauses). The computation takes part by executing the statements sequentially. It computes by deducting the clauses. Logic and controls are mixed together. Logics and controls can be separated. What is Prolog? Prolog or PROgramming in LOGics is a logical and declarative programming language. It is one major example of the fourth generation language that supports the declarative programming paradigm. This is particularly suitable for programs that involve symbolic or non-numeric computation. This is the main reason to use Prolog as the programming language in Artificial Intelligence, where symbol manipulation and inference manipulation are the fundamental tasks. In Prolog, we need not mention the way how one problem can be solved, we just need to mention what the problem is, so that Prolog automatically solves it. However, in Prolog we are supposed to give clues as the solution method. Prolog language basically has three different elements − Facts − The fact is predicate that is true, for example, if we say, “Tom is the son of Jack”, then this is a fact. Rules − Rules are extinctions of facts that contain conditional clauses. To satisfy a rule these conditions should be met. For example, if we define a rule as − grandfather(X, Y) :- father(X, Z), parent(Z, Y) This implies that for X to be the grandfather of Y, Z should be a parent of Y and X should be father of Z. Questions − And to run a prolog program, we need some questions, and those questions can be answered by the given facts and rules. History of Prolog The heritage of prolog includes the research on theorem provers and some other automated deduction system that were developed in 1960s and 1970s. The Inference mechanism of the Prolog is based on Robinson’s Resolution Principle, that was proposed in 1965, and Answer extracting mechanism by Green (1968). These ideas came together forcefully with the advent of linear resolution procedures. The explicit goal-directed linear resolution procedures, gave impetus to the development of a general purpose logic programming system. The first Prolog was the Marseille Prolog based on the work by Colmerauer in the year 1970. The manual of this Marseille Prolog interpreter (Roussel, 1975) was the first detailed description of the Prolog language. Prolog is also considered as a fourth generation programming language supporting the declarative programming paradigm. The well-known Japanese Fifth-Generation Computer Project, that was announced in 1981, adopted Prolog as a development language, and thereby grabbed considerable attention on the language and its capabilities. Some Applications of Prolog Prolog is used in various domains. It plays a vital role in automation system. Following are some other important fields where Prolog is used − Intelligent Database Retrieval Natural Language Understanding Specification Language Machine Learning Robot Planning Automation System Problem Solving Print Page Previous Next Advertisements ”;

Loop & Decision Making

Prolog – Loop & Decision Making ”; Previous Next In this chapter, we will discuss loops and decision making in Prolog. Loops Loop statements are used to execute the code block multiple times. In general, for, while, do-while are loop constructs in programming languages (like Java, C, C++). Code block is executed multiple times using recursive predicate logic. There are no direct loops in some other languages, but we can simulate loops with few different techniques. Program count_to_10(10) :- write(10),nl. count_to_10(X) :- write(X),nl, Y is X + 1, count_to_10(Y). Output | ?- [loop]. compiling D:/TP Prolog/Sample_Codes/loop.pl for byte code… D:/TP Prolog/Sample_Codes/loop.pl compiled, 4 lines read – 751 bytes written, 16 ms (16 ms) yes | ?- count_to_10(3). 3 4 5 6 7 8 9 10 true ? yes | ?- Now create a loop that takes lowest and highest values. So, we can use the between() to simulate loops. Program Let us see an example program − count_down(L, H) :- between(L, H, Y), Z is H – Y, write(Z), nl. count_up(L, H) :- between(L, H, Y), Z is L + Y, write(Z), nl. Output | ?- [loop]. compiling D:/TP Prolog/Sample_Codes/loop.pl for byte code… D:/TP Prolog/Sample_Codes/loop.pl compiled, 14 lines read – 1700 bytes written, 16 ms yes | ?- count_down(12,17). 5 true ? ; 4 true ? ; 3 true ? ; 2 true ? ; 1 true ? ; 0 yes | ?- count_up(5,12). 10 true ? ; 11 true ? ; 12 true ? ; 13 true ? ; 14 true ? ; 15 true ? ; 16 true ? ; 17 yes | ?- Decision Making The decision statements are If-Then-Else statements. So when we try to match some condition, and perform some task, then we use the decision making statements. The basic usage is as follows − If <condition> is true, Then <do this>, Else In some different programming languages, there are If-Else statements, but in Prolog we have to define our statements in some other manner. Following is an example of decision making in Prolog. Program % If-Then-Else statement gt(X,Y) :- X >= Y,write(”X is greater or equal”). gt(X,Y) :- X < Y,write(”X is smaller”). % If-Elif-Else statement gte(X,Y) :- X > Y,write(”X is greater”). gte(X,Y) :- X =:= Y,write(”X and Y are same”). gte(X,Y) :- X < Y,write(”X is smaller”). Output | ?- [test]. compiling D:/TP Prolog/Sample_Codes/test.pl for byte code… D:/TP Prolog/Sample_Codes/test.pl compiled, 3 lines read – 529 bytes written, 15 ms yes | ?- gt(10,100). X is smaller yes | ?- gt(150,100). X is greater or equal true ? yes | ?- gte(10,20). X is smaller (15 ms) yes | ?- gte(100,20). X is greater true ? yes | ?- gte(100,100). X and Y are same true ? yes | ?- Print Page Previous Next Advertisements ”;

Prolog – Environment Setup

Prolog – Environment Setup ”; Previous Next In this chapter, we will discuss how to install Prolog in our system. Prolog Version In this tutorial, we are using GNU Prolog, Version: 1.4.5 Official Website This is the official GNU Prolog website where we can see all the necessary details about GNU Prolog, and also get the download link. http://www.gprolog.org/ Direct Download Link Given below are the direct download links of GNU Prolog for Windows. For other operating systems like Mac or Linux, you can get the download links by visiting the official website (Link is given above) − http://www.gprolog.org/#download (32 Bit System) Installation Guide Download the exe file and run it. You will see the window as shown below, then click on next − Select proper directory where you want to install the software, otherwise let it be installed on the default directory. Then click on next. You will get the below screen, simply go to next. You can verify the below screen, and check/uncheck appropriate boxes, otherwise you can leave it as default. Then click on next. In the next step, you will see the below screen, then click on Install. Then wait for the installation process to finish. Finally click on Finish to start GNU Prolog. The GNU prolog is installed successfully as shown below − Print Page Previous Next Advertisements ”;