# Prolog math

Note: It seemed that there was some kind of publishing Error in this post. Few symbols were missing. Now things are rectified and post is clear.

There should be a alternative to Prolog in the pattern matching stuff, RegEx sounds convincing, but not as much like prolog. Symbolic Computation is one of the interesting fields of mathematical computation that is implemented in computer for performing the symbolic mathematical operations such as Algebra, Differentiation, Integration. Lot of computer software packages like MatLab, Mathematics supports symbolic computations.

All the mathematical expression, including variable with numeric sometime based expression which are computed under certain mathematical rules are known as symbolic computation. Integrations are computed by integral rules, differentiation is differential calculus rules and so on.

The above are some examples of symbolic computing. But in manual work this seems to be easier, because humans know how to perform a simple simplification, derivative and integral. But for computers, it is not possible. It need to be teached by writing programs to perform these operations.

But now the question is, which programming language to use. Prolog was one of popular language used for Some of the AI stuffs.

Since my problem is some what related to applying intelligence for a machine to perform symbolic computation, I choose Prolog. Since prolog language works on pattern matching, My symbolic computation also works in the same way i. The same applies to y, x, a and any variable.

Prolog is easy to write and understand. All the function in prolog are named as Facts. Prolog is freely dustributed programming language, available for both windows and Linux platforms. Here in this article I take an example of Differential Calculus derivative i.Every predicate that you write should begin with a comment that describes the predicate in a declarative statement. Do not describe procedurally, what the predicate does, but write down a logical statement which includes the arguments of the predicate.

You should also indicate the intended data types of the arguments and the allowed flow patterns. The problems have different levels of difficulty. If you have successfully solved the preceeding problems you should be able to solve them within a few say 15 minutes. If you are a skilled Prolog programmer it shouldn't take you more than minutes to solve them.

You may need more time i. Working with Prolog lists A list is either empty or it is composed of a first element head and a tail, which is a list itself. In Prolog we represent the empty list by the atom [] and a non-empty list by a term [H T] where H denotes the head and T denotes the tail.

The first element in the list is number 1. A palindrome can be read forward or backward; e. If a list contains repeated elements they should be replaced with a single copy of the element. The order of the elements should not be changed. If a list contains repeated elements they should be placed in separate sublists. Use the result of problem P09 to implement the so-called run-length encoding data compression method. Consecutive duplicates of elements are encoded as terms [N,E] where N is the number of duplicates of the element E.

Modify the result of problem P10 in such a way that if an element has no duplicates it is simply copied into the result list. Only elements with duplicates are transferred as [N,E] terms. Given a run-length code list generated as specified in problem P Construct its uncompressed version. Implement the so-called run-length encoding data compression method directly. As in problem P11, simplify the result list by replacing the singleton terms [1,X] by X. Do not use any predefined predicates.

Given two indices, I and K, the slice is the list containing the elements between the I'th and K'th element of the original list both limits included. Start counting the elements with 1. The selected items shall be put into a result list. The selected numbers shall be put into a result list. For pure mathematicians, this result may be great. But we want to really generate all the possibilities via backtracking.

Write a predicate that generates all the possibilities via backtracking. Note that we do not want permutations of the group members; i. However, we make a difference between [[aldo,beat],[carla,david], You may find more about this combinatorial problem in a good book on discrete mathematics under the term "multinomial coefficients".

The objective is to sort the elements of InList according to their length. But this time the objective is to sort the elements of InList according to their length frequency ; i. The third and forth list have length 3 which appears, there are two list of this length.

And finally, the last three lists have length 2. This is the most frequent length.Knights always tell the truth, and knaves always lie. A says: "Either I am a knave or B is a knight. Example 4 : You meet 3 inhabitants.

A says: "All of us are knaves. Example 5 : A says: "B is a knave. Which answer is correct? All of the below. None of the below. All of the above. At least one of the above. None of the above.

### P-99: Ninety-Nine Prolog Problems

None of the unnoticed things, met with at sea, are mermaids. Things entered in the log, as met with at sea, are sure to be worth remembering. I have never met with anything worth remembering, when on a voyage. Things met with at sea, that are noticed, are sure to be recorded in the log. N it is noticed M it is a mermaid L it is entered in the log R it is worth remembering I I have seen it. Their inhabitants are from different nations, own different pets, drink different beverages and smoke different brands of cigarettes.

The Englishman lives in the red house. The Spaniard owns the dog. Coffee is drunk in the green house. The Ukrainian drinks tea. From your perspective, the green house is immediately to the right of the ivory house.

The Old Gold smoker owns snails. Kools are smoked in the yellow house. Milk is drunk in the middle house. The Norwegian lives in the first house. The man who smokes Chesterfields lives in the house next to the man with the fox. Kools are smoked in the house next to the house where the horse is kept. The Lucky Strike smoker drinks orange juice. The Japanese smokes Parliaments. The Norwegian lives next to the blue house. Who drinks water?Logic Puzzle is a very funny thing.

We are all very interest to read and try our brains to solve 1 or 2 of these puzzles. We thought that would improve our brain power after proving that we are logical enough to solve these logic puzzles. Of course, we are having fun for doing that too. But does it really improve our brain function, or this simply shows our brain is incapable to handle logical jumps in large recipe, like 6 houses, 5 couples and 7 kinds of tea. Through the process of elimination and the deductive reasoning, we would come to a possible answer.

After a few rough analysis, we can see the puzzles are a search problem. If we are dumb enough to try, we can exhaustively list all possible answers and go back to the list of constraints to test if any one of the answer will satisfy all of the constraints.

If the answer produces no contradiction, we know we have the right answer. However, our hand and brain are usually too lazy to do this type of dumb search. The puzzle book producer helps with giving you a table of all combinations so that you can pretend to not doing a dumb search. You are actually writing down something to eliminate some obvious dumb choices. The logical combination table serves as a smarter-dumb search device.

This makes us feel really good. Here is the popular Grid Method using by many puzzle enthusiasts and available in magazines dedicated to the subject. If we could not reach a solution, we are not sure if we are reading all the English sentence properly after all. We would run through the original statements to verify if there is any contradictions in our possible solution.

This is a good idea until when you actually start to write some Prolog codes. You may ask, what is the problem? I said, did you actually try to do it. The problem is encoding a logic puzzle is more difficult than solving it.

Luckily, the consequence of spending infinite amount of time to code is the expectation to solve the later problems faster. Under the amortization principle, we are coming out with a gain.This tutorial provides introductory knowledge on Artificial Intelligence.

It would come to a great help if you are about to select Artificial Intelligence as a course subject.

Prolog development with GNU Emacs

You can briefly know about the areas of AI in which research is prospering. It also covers the implementation of AI problems using Prolog. This tutorial is prepared for the students at beginner level who aspire to learn Artificial Intelligence and having a knowledge in Prolog programming.

Arnab Chakraborty is a Calcutta University alumnus with B. Tech and M. He is also empaneled trainer for multiple corporates, e. Teach with Us. Subscription Includes. Add To Cart. Artificial Intelligence - Introduction. Artificial Intelligence - Goals. Prolog - Overview. Prolog - Applications of Prolog.

Prolog - Relations in Prolog. Prolog - Programming Facts, Rules, Queries.

Prolog - Family Relationship in Prolog. Prolog Data Objects - Introduction. Prolog Data Objects - Atoms and Numbers. Prolog Data Objects - Variables. Prolog Data Objects - Structures. Prolog - Representation of Lists. Prolog - List Operations Membership. Prolog - List Operations Length Calculation. Prolog - List Operations Concatenation. Prolog - Comparison Operators. Prolog - Arithmetic Operators. Prolog - Using Structures. Prolog - Matching.

Prolog - Preventing Backtracking. Prolog - Using Cut Example 1.GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.

Skip to content. Permalink Dismiss Join GitHub today GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together. Sign up. Branch: master. Find file Copy path. Cannot retrieve contributors at this time. Raw Blame History. Wielemaker vu. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

Copy lines Copy permalink View git blame Reference in new issue. You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Author: Jan Wielemaker. E-mail: J. Copyright cVU University Amsterdam. All rights reserved. Redistribution and use in source and binary forms, with or without. Redistributions of source code must retain the above copyright. Redistributions in binary form must reproduce the above copyright. This module provides a portable partial replacement of SWI-Prolog's.

It defines the.Prolog, like any other programming language, has a representation for numbers. Numbers are used like constants, and represented like they are anywhere on the computer, the following are valid ways of dealing with numbers in a predicate:. To perform mathematical operations on numbers, we will need functions. To store the result of a mathematical operation in a variable, we will need to look more closely at equality.

Until now, predicates have always represented a simple true or false. Predicate a A, B is true or false, depending on the values of A and B. Functions are predicates that represent a value.

The sin predicate, for instance, is a function. Functions can be used anywhere a number or constant can be used, in queries, predicates and rules. For instance, if the fact p 0. Note that functions themselves cannot be evaluated. One difference between functions and predicates is that the meaning or definition of a predicate is usually defined by you, in your program.

When you use functions like sinthey've already been defined in your prolog implementation. In other words, prolog will not find the definition in your program, but in it's library of built-in predicates. It is possible to create your own functions, but that's something you will usually not need. There are several kinds of equality, with slightly different meanings.

First, look at:. So Prolog will answer:. This idea may be familiar from other programming languages. It doesn't solve the right-hand side, but instead keeps it as a formula. So you get this:. Instead of assigning the result of the operation to the variable A, prolog assigns the operation to A, without evaluating it.

You'll get "No", because Prolog will compare a number 31 to a formularather than to the result of solving the formula. The is operator is meant specifically for mathematical functions.

The left argument has to be a variable and the right argument has to be a mathematical function with all variables instantiated.

Prolog knows many other ways of comparing two terms or instantiating variables, but for now, these two will suffice. When working with functions, we will almost always use the is operator. This predicate adds two numbers A and Band unifies the result with C.