1

Teaching and learning rules. is then given a goal to try and mathematics with Prolog satisfy, which sets it off finding combinations of Tom Bensky, Cal Poly San Luis Obispo, the facts that follow the rules. Such combina- [email protected] tions will comprise the “solution” to the stated 24 August 2021 goal. As an example, suppose one is designing a travel-planning application in Prolog. The facts would be cities on a map that are ABSTRACT connected by roads, and what daily traffic pat- terns typically look like on the roads. Facts are Procedural computer languages have long usually fairly straightforward to come by. In been used in many aspects of mathematics ped- this case, the road and traffic data could easily agogy. In this work, we examine the use of Pro- be found on the Internet or even based on one’s log, a declarative language for the same pur- personal experience. Facts are usually quanti- pose. We find the facts+rules aspect of Pro- ties one knows or can easily find or identify and log to be a novel platform for developing cod- are simply thought of as true. ing lessons to supplement the learning of math- The rules about the problem are more diffi- ematics. Specific examples are presented. cult to formulate, and are more specific to the problem itself. Rules express in general terms, some relationship one would like to find from I. INTRODUCTION the facts. In the travel application, the rules would involve distance and traffic considera- We have all likely used a computer to enhance tions when traveling from city A to city B, and our understanding of mathematics in some form some formulation that would minimize travel or another. For mathematics pedagogy, there is time. The rules would be very general, free no shortage of books on doing so.1–7 Websites of any specifics about either A or B, and read- are also sources of mathematical lessons on cod- ily applicable to the facts provided. A travel ing.8 rule may very well include concerns for time of Procedural coding languages are the familiar travel, speeds, distances, etc. This rule is to ones, like Python, , Lua, Pascal, BASIC, or be applied to the facts to find for example, an Javascript. Using these languages involve solv- optimal list of cities to visit. ing a problem with code, using a sequence of Prolog is thus known as a declarative lan- variables, loops, functions, that collectively im- guage since one does not supply step-by-step plement some step-by-step logic (i.e. the “pro- instructions on how to solve a problem. Facts cedure”) to solve the problem. and rules are declared, and this is all Prolog This article suggests that coding in Prolog, a needs to search for a solution. The eventual declarative language offers some novel opportu- outcome (or solution) will be all combinations nities to study mathematics, not made available of the facts that are consistent with the rules. with procedural languages or How does this work? systems. Internal to Prolog is a run-time engine based around a well-implemented search algorithm. arXiv:2108.09893v1 [cs.CY] 23 Aug 2021 The algorithm reads in the facts and rules, and II. PROLOG: A BRIEF INTRODUCTION works to find combinations of facts that are consistent with the rules, automatically adjust- Coding in Prolog9–12 involves an entirely dif- ing to the search landscape as it runs. It will ferent thought process than needed by a proce- for example, abandon search paths that have dural language. Instead of providing a step-by- no possibility of succeeding, while also keeping step procedure, one provides two sets of infor- track of areas where a search looks more promis- mation about the problem at hand: facts and ing. We emphasize that one does not code the 2 search functionality; this is built into the Prolog relatively easy to come by, with the rules being run-time engine. (Rules are sometimes called more difficult. We assert here that the formula- “clauses” in Prolog, and will be used somewhat tion of the rules into mathematics into Prolog is interchangeably here.) very natural, and is the supplemental exercises The goal in Prolog launches a search of of the study of mathematics that we propose. the facts+rules in an attempt to satisfy the goal. A goal is usually a statement or se- quence of statements that guides Prolog in A. Example: Adding two fractions examaining the rules, to drive some problem specific logic. Again in the travel application, As an example, think of coding a tutorial that after the city and road facts are set, followed shows how to add two fractions.14 The facts are by some travel rules, a goal may have the plainly seen: the numerators and denominators form of go(paris,rome)., where go is some of two fractions. Now what rules apply to add top-level clause that starts the Prolog search. them properly? This is a bit more difficult, In very general terms, this may resemble some- because we know for instance that 1/4+1/2 is thing like go(A,B) :- road exists(A,B), not 2/6. It would be incorrect to supply a rule low traffic(A,B), ... and on, as required. that said “The sum of two fractions is found by In the travel application, it hopefully seems adding the numerators, then adding the denom- plausible that that a shortest route between inators.” A correct rule would say “First look at cities could in principle be found by looking at the denominators. If they are the same then the facts about roads and traffic. How the search denominator is also used in the answer, and add should proceed however is not so intuitive. But the numerators to find the answer’s numerator.” Prolog handles the search aspects, so one only Another rule is needed if the denominators are need to focus on writing rules and presenting different. facts. This is a huge burden lifted off of the Facts can be put in almost immediately and , and allows for sole focus on de- is a rather fun and inventive process, owing scribing the problem at hand via the facts and to Prolog’s flexibility as a coding language. rules. In this regard, code is Prolog tends to be Code representing fractions for example could much shorter than an equivalent solution imple- be something like fract(1,4) or fract(1,2), mented in a procedural language. Also, since to express the facts about the two fractions, one the facts and rules are so plainly seen, coding 1/2 and the other 1/4. We note that fract is features of the problem being solved stay in the not a functor built into the Prolog language. It forefront. Simple changes to the code are also is of our own convenient making, that Prolog usually straightforward to implement.13 happily accepts. Here fract takes two param- eters, a numerator and a denominator. The rules however require more care. Inde- III. STUDYING MATHEMATICS WITH pendent of any coding, one must first be com- PROLOG fortable with all of the mathematical rules for adding fractions, then must adapt them into We suggest that the facts, rules, and search Prolog code. Here’s a rule to add two fractions characteristics of Prolog are ideal for study- that have the same denominator: ing mathematics. Why? Because, it’s hard to add(fract(N1,D1),fract(N2,D2),fract(N,D)) :- think of anything more fact and rule-based than D1 == D2, D = D1, N is N1 + N2. mathematics. Facts here would be the state- ment of the problem. The rules would be the The clause (or rule) called add takes in two rules of mathematics itself, which are mostly parameters, fract(N1,D1) and fract(N2,D2). what is taught in math classes. As in the travel You can see the use of the facts of the fract application, facts for a math problem are again functor. On entry, we expect N1, D1, N2 and 3

D2 to be instantiated (that is, equal to some tions that have a common denominator: as the numbers coming in). Prolog’s job in the search denominator of the two incoming fractions, and as it uses this rule, is to find what N and D (the as the denominator of the resulting fraction. numerator and denominator of the fraction) will The body now only needs to evaluate N into the result from the sum of the two initial fractions, sum of N1 + N2. This will also work: that is consistent with the add rule. When writing code, clauses are usually qui- add(fract(N1,D),fract(N2,D),fract(N1+N2,D)). etly translated into one’s native tongue (here English). Prolog can be difficult to translate To complete the rule, we still need to han- and perspectives on reading it are available.15 dle fractions with different denominators. Our mathematical logic is to convert the original (Quick Prolog review: :- means “if,” , means fractions N /D and N /D into two fractions “and,” ; means “or,” variables are in upper- 1 1 2 2 case, everything else must be in lowercase, and each with the common denominator of Dsum = D D . The new numerator of N will be N 0 = the period (.) terminates facts and rules.) 1 2 1 1 N D and that of N will be N 0 = N D . The so called “procedural reading” of the 1 2 2 2 2 1 We thus change the problem to that of adding clause add would be “add can successfully add the fractions N D /(D D ) + N D /(D D ). together N /D and N /D into N/D IF the 1 2 1 2 2 1 1 2 1 1 2 2 Here is this logic implemented in Prolog. two ingoing denominators are equal AND D can be instantiated to one of them (here D1) AND add(fract(N1,D1),fract(N2,D2),R) :- N can be instantiated to the sum of the two in- D1 \= D2, coming numerators.” Procedural readings often N1p = (N1 * D2), go from the clause head to the body of clause, N2p = (N2 * D1), in the order it is shown. In other words, the Dsum = D1 * D2, clause will succeed if each part of it succeeds in add(fract(N1p,Dsum),fract(N2p,Dsum),R). order they appear. A “declarative reading,” which focuses on re- Again in words, “add can add two fractions only 0 lationships between objects in the clause would IF the two denominators are different AND N1 0 be “if we can set the sum numerator to the sum can be set equal to N1D2 AND N2 can be set of the ingoing numerators and set the sum de- equal to N2D1 AND Dsum can be set equal to nominator to one of the ingoing denominators, D1D2 AND the established “same denomina- and find that the two denominators equal, then tor” clause can finish the job, placing the result into R.” adding N1/D1 and N2/D2 will result in N/D.” Declarative readings go from the clause body Here we also see more practice on the rules of back to its head as in: “if each part of the body mathematics: we first form two fractions with 0 can be made to succeed, then the whole clause the same denominator namely N1/Dsum and 0 can succeed.” The body is often even inter- N2/Dsum, then reuse the first clause that knows preted entirely in reverse order, from the period how to handle like-denominator fraction addi- to the clause head. tion. Both clauses would appear in the Prolog These readings can be satisfying intellectual code, and both will be used as needed during challenges, even more so with the backdrop of the search for solutions. mathematics. Finally, adding two fractions can now be done Prolog is excellent at pattern matching, so by specifying this goal using add: add can also be simplified as add(fract(3,8),fract(4,7),R). add(fract(N1,D),fract(N2,D),fract(N,D)) :- N is N1 + N2. where R (for result) is going in uninstantiated (or unassigned), to which Prolog will respond Here you’ll see how the denominator D is placed R = fract(3*7+4*8, 8*7) . This evaluates to in all positions it must be, for adding two frac- 53/56 (which is the sum of 3/8 + 4/7). 4

B. Summary some expression (so if one does P is 3*7, P will indeed hold 21). Thus doing N1p = N1 * N2 Using Prolog in the context of mathematics merely instantiates Np1 to the literal N1 * N2 can provide a novel new viewpoint for the stu- (with current values for N1 and N2 filled in). dent in working with, reviewing, and learning We highlight this point by illustrating two the rules of mathematics and their appropri- more possibilities of the add clause. First, sup- ate use, with the interactivity a computer af- pose numbers were not even used, as in fords. We find Prolog’s syntactical flexibility add(fract(red,blue),fract(green,yellow),R). makes the mathematical facts+rules implemen- tation an engaging intellectual challenge, which This will result in R = will test one’s understanding of the rules them- fract(red*yellow+green*blue, selves. More examples are presented below. blue*yellow). Second, familiar variables from algebra can also be used as in IV. NON-NUMERICAL CODING FOR add(fract(x,2*y),fract(2*x,z),R). MATHEMATICS This will result in R = fract(x*z+2*x* Looking at math with Prolog is also some- (2*y), 2*y*z), which will need to be simpli- what contradictory, as Prolog is not known to fied by the student. be a language particularly well suited for the We find the “everything is just a symbol” numerical computation. For example, using a idea in Prolog to be a desirable property for the for-loop to compute a sum (very natural for in a study of mathematics, as the meaning of sym- procedural language), would require a carefully bols used can be linked to some desired peda- thought out plan involving an accumulator and gogy. recursion.16 Thus, our ideas presented here to As these two cases show, Prolog is willing study mathematics with Prolog do not focus on to look at both numbers and symbols as facts, having code that generates numerical results (as and only treat one or the other differently, as in the answer to a math problem). directed by the code. Because Prolog doesn’t Prolog is very convenient however, in its abil- immediately differentiate between symbols and ity to manipulate symbols, which may include possible expressions that could be evaluated, variables, or objects that might have other the code unknowingly presents steps for the stu- meanings such as fract(1,2) to represent the dent to see, and how results came to be. If fraction 1/2. This text and symbolic manipula- Prolog merely output 53/56 for the fraction ad- tions is our focus when using Prolog and math- dition, the student might still be left confused ematics. at how such numbers were found. The sequence An an example, in the fraction addition of steps toward an answer may be tracked with above, the output was R = fract(3*7+4*8, an answer like (3 ∗ 7 + 4 ∗ 8)/(8 ∗ 7), even there 8*7) . One may notice that the calculations is still some work for the student to do (the fi- were not carried out. This is because the mul- nal simplification). In this context, Prolog may tiplication operator in Prolog or * is merely a be seen as a “lazy” tutor, presenting core logic functor that take two arguments. It is defined in to the students, but not bothering with the (te- Prolog as *(A,B), similar to our own fract used dious) details that lead to an actual solution. above with its two arguments (a numerator and There is some contrast here between what denominator). So telling Prolog 3*7 is merely Prolog does with these symbols and what would the infix notation for the functor *(3,7). With be done by a “symbolic math” or computer al- this, Prolog does not explicitly see * as a direc- gebra system (CAS) such as Mathematica17. In tive to multiply, but does has a built in func- response to red/blue + green/yellow, Math- red green tor is that will force a numerical evaluation of ematica outputs blue + yellow , with no other sim- 5 plification forthcoming. In response to x/(2 y) the Pythagorean theorem, 2) the sum of all an- x 2x ◦ + 2 x/z, it outputs 2y + z . A simplification gles is 180 , 3-5) the sine, cosine, and tangent   of one angle, and 6-8) the sine, cosine and tan- steps yields 1 x 1 + 4 . The Together func- 2 y z gent of the other angle. Each of these rules has tion does handle common denominators as seen three inputs, and our logic is that if any two in elementary math, but is a “just the answer” of three are known, the third can be computed exercise, without the opportunity to code the using the mathematics of the rule, with the re- needed rules. sult becoming newly found knowledge, that may trigger the use of another rule. These rules are put into Prolog as shown here: V. EXAMPLES rule([c,a,b],’c^2=a^2+b^2’). rule([d,e],’d+e+90=180’). Here we present three more examples, all of rule([d,a,c],’sin(d)=a/c’). 18 which are available for use online. . These in- rule([d,b,c],’cos(d)=b/c’). clude solving a right triangle, reasoning about rule([d,a,b],’tan(d)=a/b’). lines and triangles from a collection of points, rule([e,b,c],’sin(e)=b/c’). and converting units. rule([e,a,c],’cos(e)=a/c’). rule([e,b,a],’tan(e)=b/a’).

A. Solving a right triangle Each rule contains a list of required vari- ables, followed by a (minimal) description of a right triangle rules that relates the variables. Consider a Prolog-based example that would The goal, not shown here (but presented on- help someone solve a right triangle. The typical line18) is called , and is passed in a list exercise would be to present the student with a advise of triangle quantities that are known (the facts), right triangle, and give them two knowns about to start Prolog’s search. Running for example the triangle, either two sides, an angle and a would result in the following side, or two angles. From this, all angles and advise([b,c]). output: sides of the triangle are to be solved for. In this example, a model triangle has sides Solve for a using a, b, and c, with c being the hypotenuse, with c^2=a^2+b^2 angles d and e, as in Fig. 1: You now know [a,b,c]

Solve for d using sin(d)=a/c You now know [d,a,b,c]

Solve for e using d+e+90=180 FIG. 1. A right triangle (see text). You now know [e,d,a,b,c] The facts of a right triangle are what is known at the onset of the problem, as in “you are given Once again, the emphasis is not to gener- that b = 5 and c = 7.” As mentioned above, ate numerical results. The “solution” is logical these would be easy to identify, simply by care- method of solving the triangle that will lead to fully reading the stated problem. Next, there the needed results, while leaving any numerical are eight rules for solving a right triangle: 1) computation up to the student. 6

B. Reasoning about a collection of points L = [(3 , -3),(3,3)] ; L = [(3 , -3),(-4 , -4)] ; In this example, the facts are a collection of With lines now established, we can also check points placed on a standard xy-coordinate grid, if a line is horizontal with this rule: as shown here: horiz([(_,Y),(_,Y)]). point(-4,4). point(-3,2). This short rule says a line is horizontal point(-1,1). if both y-coordinates are the same. Since point(0,0). the x-coordinate is immaterial, we put in point(-3,0). point(1,3). Prolog’s “anonymous” or “don’t care” vari- point(3,3). able in for the x-coodinates, which is an un- point(-4,-4). derbar ( ). This clause would also work: point(3,-3). horiz([( ,Y1),( ,Y2)]) :- Y1 = Y2. If variables were put in for x1 and x2 (in ei- With these points, we implement some Prolog ther clause), as in horiz([(X1,Y1),(X2,Y2)]), code to “reason” about them, in particular, find Prolog would throw a warning since they are lines made by pairs of points, then lines that are not used in the definition of a horizontal horizontal and vertical. line. Testing for vertical lines is similar, as in As we know, a line is made of any two points, vert([(X, ),(X, )]).. so Prolog code that will search for such lines Now we may look for horizontal lines amongst amongst the facts (of points) would look like the points. The goal for this would be line(L), this: horiz(L). Here we first find a line L amongst line([(X1,Y1),(X2,Y2)]) :- the points, then test if L is horizontal. For the point(X1,Y1), point data shown, Prolog yields: point(X2,Y2), L = [(0,0),(-3,0)] ; point(X1,Y1) \= point(X2,Y2). L = [(-3,0),(0,0)] ; L = [(1,3),(3,3)] ; Here we see a clause called line that is as- L = [(3,3),(1,3)] ; sociated with a list of two xy-points. (A list in Prolog is a comma separated sequence of ob- The student may verify (by hand) that these jects enclosed in [ and ].) The clause would are indeed horizontal lines. Vertical lines can be read like this: “A line between points (X1,Y1) found using the goal line(L), vert(L). and and (X2,Y2) exists IF a point at (X1,Y1) exists will output: AND a point at (X2,Y2) exists, AND be sure that the two points are different (to ensure we L = [(-4,4),(-4 , -4)] ; don’t have line whose endpoints are both the L = [(-3,2),(-3,0)] ; L = [(-3,0),(-3,2)] ; same point). This goal run against the point L = [(3,3),(3 , -3)] ; facts given will yield: L = [(-4 , -4),(-4,4)] ; L = [(-4,4),(-3,2)] ; L = [(3 , -3),(3,3)] ; L = [(-4,4),(-1,1)] ; L = [(-4,4),(0,0)] ; From horizontal and vertical lines, we can L = [(-4,4),(-3,0)] ; also find perpendicular lines, which have two L = [(-4,4),(1,3)] ; considerations. The first is having two lines L1 L = [(-4,4),(3,3)] ; and L2, with one is vertical and the other is L = [(-4,4),(3 , -3)] ; horizontal. This requires two clauses as per ...... perp(L1,L2) :- horiz(L1), vert(L2). L = [(3 , -3),(1,3)] ; perp(L1,L2) :- vert(L1), horiz(L2). 7

This read “Lines L1 and L2 are perpendicular if one is horizontal and one is vertical (or vice-   ¨ versa).” (We note here the lines are thought of 10inches 2.54cm 1¨m 1 km ×  × × , as containing the (x, y) points in the data, but 1 1inch 100cm 1000¨¨m not necessarily as their end-points.) Present- which requires the student to find a step-by- ing Prolog with the goal of line(A), line(B), path from the original unit to the desired (or perp(A,B) will generate this (partial) output: terminating) unit. The facts for such a problem A = [(0,0),(-3,0)], B = [(-4,4),(-4 , -4)] ; would be easily found, many of which are com- A = [(0,0),(-3,0)], B = [(-3,2),(-3,0)] ; monly known ways of directly converting one A = [(0,0),(-3,0)], B = [(-3,0),(-3,2)] ; unit into another, such as this collection: A = [(0,0),(-3,0)], B = [(3,3),(3 , -3)] ; A = [(0,0),(-3,0)], B = [(-4 , -4),(-4,4)] ; fact(1,ft,12,inch). A = [(0,0),(-3,0)], B = [(3 , -3),(3,3)] ; fact(1,yard,3,ft). A = [(-3,0),(0,0)], B = [(-4,4),(-4 , -4)] ; fact(1,mile,5280,ft). ... fact(220,yard,1,furlong). ... fact(2.54,cm,1,inch). fact(10,mm,1,cm). The second consideration shown online18 con- fact(100,cm,1,meter). siders two lines, neither of which is vertical, fact(1000,meter,1,km). whose slopes have a product of −1. Online, the lessons are extended to include The issue at hand is in searching the facts looking for right triangles within the points via that will find a path from the original unit to the desired final unit. For this discussion, we’ll line([P1,P2]), focus on converting inches into kilometers (km). line([P2,P3]), Using only the facts shown, a quick scan would line([P3,P1]), see the steps 1) inches to cm 2) cm to meter, perp([P1,P2],[P2,P3]). them 3) meter to km. Algorithmically, this This clause supposes that a triangle exists is not an easy task, either in a declarative or procedural language. In Prolog, the facts are amongst the points P1, P2 and P3, and starts by looking for three lines containing these points. straightforward handle, as shown above. Our Note the usage of these points in the search for solution proceeds as follows. the three lines that will make a (closed) trian- To begin, we expect the facts to work in both gle, in particular that P1 connects to P2, P2 directions. That is, if a fact states how to con- to P3 and P3 back to P1. Once suitable lines vert from unit A to B, this same fact can be are found, at least two of them are checked for used to convert from B to A. This is not au- perpendicularity. Rectangles can be found sim- tomatically handled by Prolog, so we include a ilarly, by looking for 4 lines and 3 pairs that are rule called direct (as in “direct conversion”) as 19 perpendicular. Circles could be found using a follows: rule that looks for points that are all equidistant direct(X, Y) :- fact(_,X,_,Y). from a center point. direct(X, Y) :- fact(_,Y,_,X).

Note the reversal of X and Y in the 2nd C. Converting Units clause. When looking for conversion facts, the code will always used direct and not fact. Converting units includes problems like “con- The top-level conversion rule is vert 10 inches in to kilometers.” Initial lessons convert(From,To,How), which will con- usually require a strict accounting of the units vert from a unit From to a unit To, with themselves, to be sure they all cancel properly. directions for how to do so (given the facts) An example is returned in How. 8

This rule calls another rule called path, which direct(X,Z), treats the facts given as a tree to traverse, look- \+ member(Z,Trail), ing for a path from unit From to unit To. By path(Z,Y,[X|Trail],How0). “tree,” we mean each unit in a fact is a node that has a variable number of branches from it, The variables are X and Y, the starting and to units it can be directly converted into. This terminal units, Z is some intermediate unit, is shown in Fig. 2. Trail keeps track of where the search has gone to avoid repeats, and How0 is the directions that will be given as output. With this, the rule reads “it is possible to find a path from X to Y keeping track in Trail and directions in How0 IF the facts allow for direct conversion of X into some unit Z AND Z is not (\+) already in the Trail AND a path from Z to Y can be found, while prepending X onto Trail and keep- ing How0 available for final directions.” Since this is a recursive call, we must have a terminal condition. This is another rule, which is a non-recursive clause for path which reads: path(X,X,Trail,[X|Trail]). FIG. 2. A tree representation of the unit conversion facts presented. This terminates the recursion if we try to find a path from unit X to the same unit X. This The algorithm’s job is to find a path through is an odd expectation, going from X to X, as the tree from the original unit to the termi- why would Prolog try to do this? Also, we’d nating unit. The rule path is complicated, as never expect a fact to be given that shows us was adapted from a treatment by Clocksin9 on how to go from unit to the same unit. We use searching graphs. We explain it here. this oddity as an opportunity to illustrate the The core logic here is that if unit X cannot execution order of Prolog. be directly converted into Y from the stated Those familiar with recursion in procedural facts, we’ll have Prolog search for what X can languages might think the path call in the re- be converted into, in this case some interme- cursive clause will again reenter the recursive diate unit Z. Then we see if Z can be con- clause. Prolog will not do this, as it always verted into Y . Thus the logic is to find a direct calls rules in the order in which they appear from X to Z as in direct(X,Z). We must keep in the code. Here the non-recursive terminal track of what we have converted to in a variable clause appears before the recursive one, so the Trail as to avoid endless and repeated conver- terminal rule will always be the first called when sions into the same units. (Like the need for path(...) is ever queried. the direct clause, this is also something Pro- It is still a curious point on when and why log does not handle automatically.) Lastly, once the unit path search would try to convert from we find a unit Z that X can be converted into, one unit to the same unit, terminating the re- we re-enter the same search in a recursive man- cursion. This happens when the intermediate ner, now looking to convert Z to Y . (This may unit Z reaches the desired terminal unit. One lead to finding an intermediate unit that Z can notes in the recursive rule be converted to, and so on; this is the power of recursion.) This is all handled in the recursive path(X,Y,Trail,How0) :- . clause of path: . path(X,Y,Trail,How0) :- path(Z,Y,[X|Trail],How0). 9 that the parameter Y is always held as the sec- As for learning, many books on Prolog were ond argument to path. Thus in all of the re- written in the 1980s, and are now out of print. cursive calls, the desired terminal unit is al- It is rare to find one on the shelf of a bookstore ways preserved in variable Y, in both the non- (even used). Some are now free and in the pub- recursive (terminal) and recursive clauses. This lic domain.23. Few (if any) new books have ap- is how the terminal clause is triggered: when peared since, although two of the best books on the intermediate unit Z finally matches the de- the subject (that we hold in high regard), have sired terminal unit, which is always held in this been revised somewhat recently.9,10. Online re- second argument position.20 sources like “Learn Prolog now”24, the “Power If we call on convert then, with something like of Prolog,”25 and other collections of lessons26 convert(inch,km,Plan)., Prolog will respond are very instructive. with Plan = [inch,cm,meter,km], which in- Adding to Prolog’s difficulty is a lack a mod- deed it a step by step conversion from inches ern, dedicated, and true IDE, such as what the to km, using the direct conversions possible, as Spyder IDE does for Python.27 Most work we do supplied by the facts. The entire code is avail- with Prolog is (painfully) at the Unix prompt. able for running live online.18 If one wishes to lightly experiment with Prolog, we’d recommend finding an online IDE28,29 to get started. D. Prolog in use Often Prolog code found in books and on- line lessons comes in the form of challenging The advantage to using Prolog versus a pro- puzzles such as the N-queens problem, traveling cedural language here, as it ties to a quote in salesperson, or solving a Sudoku. While these Ref. 13, is somewhat clear. In the examples leave one with a “gee-whiz” feeling of what Pro- presented, reflect how continually the meaning log can do, they give very little inspiration for of the needed rules are in plain view, and how one’s everyday coding needs. Practical uses for straightforward it is to change or extend them. Prolog seem hard to come by, but we again re- In the example, the logic of perpendicular lines fer to Ref. 22. Common introductory lessons is always there, with the actual code more or on Prolog are important, yet uninspiring, as in less saying “perpendicular lines A and B ex- “write a clause to see if item A is a member of ist if A is vertical and B is horizontal or vice- list L,” which is a very common example. This versa.” Facts are in a similar situation: adding can be contrasted with tutorials on procedural or editing known units conversion facts are a languages, that may quickly get into graphics, few keystrokes away, as are adding or changing web-apps, and the like. (x, y) points. State-of-the-art Prolog compilers and inter- preters do continue to see regular develop- ment30–32, and constraint VI. SUGGESTIONS ON USING PROLOG (CLP) seems to be reinvigorating the lan- guage.33 We close this paper by sharing a few of our CLP has the following power in the declara- own observations and experiences using Prolog. tive nature of Prolog: Suppose a clause you are We have been using Prolog on and off since the working on says X = Y +1. If Y is not known, late 1980s, and have managed to produce some the clause will fail, since X cannot be instanti- published work using it.21 We always think of ated. With CLP, this would be represented with Prolog as a fun, powerful and intriguing tool X #= Y +1, where #= is the CLP version of =. In for solving problems with code, but also find this case, evaluation of the clause will continue, it perennially difficult use.22 The mathematical where Prolog will consider satisfying X #= Y+1 context discussed in this paper is an actual a either by finding Y, in which case X can be found, learning motivator for us. or X, in which case Y can be found. A first-order 10 understanding of this tells us that CLP allows version of units have been presented, with more for elementary algebra to work within a clause, found online.14 which indeed is a powerful mechanism to add to Prolog’s search mechanisms. A higher-order thought is that CLP kind of allows Prolog to 1A. Engel, “Exploring Mathematics with your Com- run code in reverse, as in telling you what in- puter (Anneli Lax New Mathematical Library, Se- ries Number 35),” The Mathematical Association of puts will produce some output you may desire. America; Pap/Dskt edition (August 14, 1997). We see applications of CLP in studying math 2E.W. Cheney and D.R. Kincaid, “Numerical Mathe- with Prolog. Returning to the fraction addi- matics and Computing,” (7th ed.), Cengage Learning, tion, add(fract(N1,D1),fract(N2,D2),R), (2012). 3K. Becker and Michael Dorfler, Dynamical Systems CLP would allow us to pose a goal of and Fractals, Computer Graphics Experiments with add(fract(2,D1),fract(3,7),frac(1,2)) Pascal, Cambridge, 1989. for example. This would have Prolog search 4R.L. Devaney, Chaos, Fractals, and Dynamics, for the denominator D1 of 2/D such that Addison-Wesley, 1990. 1 5 2/D + 3/7 = 1/2. CLP will also work with H. Guggenheimer, BASIC Mathematical Programs for 1 Scientists and Engineers, Petrocelli, 1987. more than one unknown variable. Expected 6S. Wolfram, “An Elementary Introduction to the Wol- ranges of solutions can be specified to nar- fram Language - Second Edition An Elementary Intro- row the search, but even without such, CLP duction to the Wolfram Language - Second Edition,” Wolfram Media, 2017. over the integer domain comprises a finite 7 search field. Another interesting example of C. Wolfram, “The Math(s) Fix: An Education Blueprint for the AI Age,” Wolfram Media, 2020. using CLP in mathematics is in dealing with 8See https://projecteuler.net and Fibonacci numbers in reverse.34 https://codebymath.com. Lastly, in a production environment, we don’t 9W. Clocksin and C. Mellish, “Programming in Pro- see Prolog as well suited to be the primary code log,” (5th ed.), Springer, 2003. 10I. Bratko, “Prolog Programming for Artificial Intelli- base. It has always had weak input and out- gence,” (3rd ed.), Addison-Wesley, 2001. put capabilities, and sanitizing input data from 11M.A. Covington, D. Nute, and Andre Vellino, “Pro- a user in Prolog would simply not be worth the log Programming in Depth,” (2nd ed), Prentice-Hall, effort. Instead, we see a front-end procedural 1997. 12 language used to generate Prolog code based J. Bos, “Drawing Prolog Search Trees: A Manual for Teachers and Students of Logic Programming,” on user input, perhaps as assembling a bunch of arXiv:2001.08133 [cs.PL]. facts to be paired with some pre-written rules. 13P.V. Hentenryck, “Constraint Satisfaction in Logic The procedural language would then run a Pro- Programming,” MIT Press, 1989. This author has log environment, piping in the generated code some compelling comments on translating seach-type and capturing the results coming out, which can algorithms to procedural languages: “They achieve a reasonable efficiency but are tedious to develop, then be formatted and presented to the user. extend and maintain. The amount of programming is generally very significant and simple conceptual changes in the problem specification or in the reso- lution strategy cannot be accommodated easily.” VII. CONCLUSIONS 14This example can be run and used here https://www.codebymath.com/index.php/welcome/ lesson/prolog-add-fractions. We have shown how Prolog can be used to 15 examine topics in elementary mathematics. We See “Reading Prolog Programs” at https://www. metalevel.at/prolog/reading. find the declarative nature of the language well 16See https://www.codebymath.com/index.php/ suited for this task, even more so with CLP. welcome/lesson/prolog-count. Prolog may provide a potential platform for cre- 17See Mathematica at https://wolfram.com. 18 ating tutorials, lessons, and student projects in- See https://www.codebymath.com. 19We note that fact(X,Y) :- fact(Y,X). will not volving mathematics. Examples presented from work. This instead leads to a recursion overflow, since elementary math, including addition of frac- fact continually calls itself, with no opportunity to tions, reasoning about (x, y) points, and con- exit. 11

20We find this search algorithm clever and instructive use Python for this. So we think the difficulty isn’t re- but was not very intuitive to us. It illustrates the dif- ally with Prolog per se, but with the problems Prolog ficulty one must consider when handling looping and allows us to think about solving. They are difficult accumulators in Prolog, but also note how short (and problems anyway, and would also be made even more still declarative) the code is, as compared to an equiv- so if solved in a procedural language. alent implementation in a procedural language. Such 23See http://www.covingtoninnovations.com/books. complications are likely why Prolog has never seen html. widespread adoptions: looping is always based on re- 24See http://www.let.rug.nl/bos/lpn//index.php. cursion, which is an advanced and relatively difficult 25See “Power of Prolog” at https://www.metalevel.at/ programming topic versus for example, a basic for- prolog/. loop found in procedural languages. 26See https://skolemmachines.org/ 21T.J. Bensky and C.A. Taff, “Computer-Guided Solu- ThePrologTutorial/www/prolog_tutorial/ tions to Physics Problems Using Prolog,” Comp. Sci. contents.html. Eng., Vol. 12, Iss. 1, Jan. 1, 2010, pages 88-95. The pa- 27Imagine something like the Spyder IDE at https:// per can be found here: https://github.com/tbensky/ www.spyder-ide.org, which is (for Python), but for physics_prolog/blob/master/paper.pdf. Prolog. 22There are reasons for this. Prolog is a powerful lan- 28See https://swish.swi-prolog.org. guage. Its free and expressive format opens up one’s 29See https://www.codebymath.com/index.php/ thinking on some of the strangest coding problems welcome/sandbox. Place the line % prolog as the we might consider. Scheduling? Understanding nat- first line of code to trigger the integrated Prolog ural language? Searching a graph? Advising a stu- interpreter.32 dent on how to learn math? In this work alone, we 30See https://www.swi-prolog.org. presented short, digestible code that gives us step-by- 31See http://www.gprolog.org. step directions on how solve a right triangle! In any 32See http://tau-prolog.org. procedural language, this would be loaded with ar- 33See “The Power of Prolog” at https://www. rays, loops, and calls to string processing libraries. metalevel.at/prolog, and in particular https:// The result would be a boring “so what” kind of out- www.metalevel.at/prolog/optimization. We note come not really worth presenting. In Prolog we have that both SWI and Gnu-Prolog support constraint a list of variables directly paired with mathematical logic programming. expressions at the onset. This is about as low of a 34See https://m00nlight.github.io/constraint% thought-to-code barrier as one could possibly expect. 20logic%20programming/2016/01/01/ Although not always easy either, we simply don’t use using-clpfd-to-solve-factorial-and-fibonacci-problems-reversely. Prolog for some task like opening a file, reading data from columns #2 and #7 to making an XY plot. We’d