## How to build symbolic differential & integral calculus engine in Prolog

Math is all about symbolic calculation and there are only handful of programming languages to do the symbolic math. But when need arises, you can write your own symbolic math processing engine easily and I will show you how you can use Prolog to do it and integrate into any java application.

Prolog a well known language in the academia areas of computer science such as Artificial Intelligence and related studies. Prolog is a logic programming and pattern matching language. It is a first-order programming model and helps you to build code in a mathematical or inference based approach. Prolog is a simple language to learn and yet powerful to use ( just for fun, try to convert the rules listed in here into Java code ). It is pretty easy to get start with it in matter of hours.

This article shows you how to use Prolog to write Differential and Integral calculus rules. I’ve coded few modules for some of my bachelors projects such as MinT and jLogics.

If you are new to prolog, I would recommend to get some basics of Prolog before diving in here. Do not worry you are not learning anything weird like this.

In nutshell, just understand that

- It can be used a Compiled or Interpreted language and works based on the pattern matching.
- You start with simple facts & relations and extend those as rules.
- Expressions are separated by COMMA(,) and the function is terminated by a DOT(.)
- Just build the foundations correct and rest everything would fit ( just like the axioms in Math )
- Prolog is good at backtracking. i.e if one of the input couldn’t derive to a ‘Truth’ value, it would try with nex set of values until it finds ‘Truth’.

Here is an example of backtracking. Prolog, will match for truth always and until it matches, it will backtrack and keep looking.

In this program, when you ask for `good_menu_in(thai)`

, it will find two matches `restaurant4`

and `restaurant1`

and then for each match, it will try to find the matching `good()`

menu item. When it checks for `restaurant4`

, it doesn’t match any `good()`

item. So it will backtrack to `restaurant1`

.

when `restaurant1()`

rule is executed, it would fetch `restaurant4`

and `restaurant1`

. When it tries to match `good(restaurant4)`

, it wont find any, hence it will backtrack to `good(restaurant1)`

.

Result:
`ginger_chicken`

, `fried_noodles`

Here is another example

Now, let us get started.

## Differential rules

### Define the basic axioms

### Now little complex pattern.

As prolog is good at pattern matching, you can define rules with complex inputs. When you invoke `d(sin(X))`

or `d(sin(sin(X)))`

,it will match the rule. And then it will still track for `d(X)`

. In the first case it is just `X`

and in 2nd case it is `sin(X)`

and see if anything matches up.

### List of basic trigonometric rules

## Integral rules

Similar to Differential, Integral rules are defined. Start with the basic axioms and expand.

## Java Integration

You can use GnuProlog for java to integrate your prolog program into your java app. Build the basic prolog engine using the gnuprolog APIs and you should be set.

### jCalculus - java library

I’ve built this as part of my final year project and it is available over github for anyone to use.

**Note**: I’m not maintaining this anymore