Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mathematica: what is symbolic programming?

I am a big fan of Stephen Wolfram, but he is definitely one not shy of tooting his own horn. In many references, he extols Mathematica as a different symbolic programming paradigm. I am not a Mathematica user.

My questions are: what is this symbolic programming? And how does it compare to functional languages (such as Haskell)?

like image 332
zenna Avatar asked Dec 13 '10 16:12

zenna


People also ask

What is symbolic programming used for?

In computer programming, symbolic programming is a programming paradigm in which the program can manipulate its own formulas and program components as if they were plain data.

What is symbolic machine language?

The symbols of machine language are binary digits (bits). As a rule, the symbols are grouped into constructs (morphemes), that is, addresses within instructions; operation codes; and instruction attributes. These instructions are compiled into programs which embody the algorithms for solving the problems at hand.

What is symbolic language computer architecture?

From Wikipedia, the free encyclopedia In computer science, a symbolic language is a language that uses characters or symbols to represent concepts, such as mathematical operations and the entities (or operands) on which these operations are performed.

Is known as symbolic language?

(1) A programming language that uses symbols, or mnemonics, for expressing operations and operands. All modern programming languages are symbolic languages. (2) A language that manipulates symbols rather than numbers.


1 Answers

When I hear the phrase "symbolic programming", LISP, Prolog and (yes) Mathematica immediately leap to mind. I would characterize a symbolic programming environment as one in which the expressions used to represent program text also happen to be the primary data structure. As a result, it becomes very easy to build abstractions upon abstractions since data can easily be transformed into code and vice versa.

Mathematica exploits this capability heavily. Even more heavily than LISP and Prolog (IMHO).

As an example of symbolic programming, consider the following sequence of events. I have a CSV file that looks like this:

r,1,2 g,3,4 

I read that file in:

Import["somefile.csv"] --> {{r,1,2},{g,3,4}} 

Is the result data or code? It is both. It is the data that results from reading the file, but it also happens to be the expression that will construct that data. As code goes, however, this expression is inert since the result of evaluating it is simply itself.

So now I apply a transformation to the result:

% /. {c_, x_, y_} :> {c, Disk[{x, y}]} --> {{r,Disk[{1,2}]},{g,Disk[{3,4}]}} 

Without dwelling on the details, all that has happened is that Disk[{...}] has been wrapped around the last two numbers from each input line. The result is still data/code, but still inert. Another transformation:

% /. {"r" -> Red, "g" -> Green} --> {{Red,Disk[{1,2}]},{Green,Disk[{3,4}]}} 

Yes, still inert. However, by a remarkable coincidence this last result just happens to be a list of valid directives in Mathematica's built-in domain-specific language for graphics. One last transformation, and things start to happen:

% /. x_ :> Graphics[x] --> Graphics[{{Red,Disk[{1,2}]},{Green,Disk[{3,4}]}}] 

Actually, you would not see that last result. In an epic display of syntactic sugar, Mathematica would show this picture of red and green circles:

alt text

But the fun doesn't stop there. Underneath all that syntactic sugar we still have a symbolic expression. I can apply another transformation rule:

% /. Red -> Black 

alt text

Presto! The red circle became black.

It is this kind of "symbol pushing" that characterizes symbolic programming. A great majority of Mathematica programming is of this nature.

Functional vs. Symbolic

I won't address the differences between symbolic and functional programming in detail, but I will contribute a few remarks.

One could view symbolic programming as an answer to the question: "What would happen if I tried to model everything using only expression transformations?" Functional programming, by contrast, can been seen as an answer to: "What would happen if I tried to model everything using only functions?" Just like symbolic programming, functional programming makes it easy to quickly build up layers of abstractions. The example I gave here could be easily be reproduced in, say, Haskell using a functional reactive animation approach. Functional programming is all about function composition, higher level functions, combinators -- all the nifty things that you can do with functions.

Mathematica is clearly optimized for symbolic programming. It is possible to write code in functional style, but the functional features in Mathematica are really just a thin veneer over transformations (and a leaky abstraction at that, see the footnote below).

Haskell is clearly optimized for functional programming. It is possible to write code in symbolic style, but I would quibble that the syntactic representation of programs and data are quite distinct, making the experience suboptimal.

Concluding Remarks

In conclusion, I advocate that there is a distinction between functional programming (as epitomized by Haskell) and symbolic programming (as epitomized by Mathematica). I think that if one studies both, then one will learn substantially more than studying just one -- the ultimate test of distinctness.


Leaky Functional Abstraction in Mathematica?

Yup, leaky. Try this, for example:

f[x_] := g[Function[a, x]]; g[fn_] := Module[{h}, h[a_] := fn[a]; h[0]]; f[999] 

Duly reported to, and acknowledged by, WRI. The response: avoid the use of Function[var, body] (Function[body] is okay).

like image 181
WReach Avatar answered Sep 20 '22 03:09

WReach