Message Boards Message Boards

0
|
8257 Views
|
9 Replies
|
5 Total Likes
View groups...
Share
Share this post:

Implement Mathematica in Mathematica

Anonymous User
Anonymous User
Posted 6 years ago

Has anyone written Mathematica code simulating the Mathematica evaluation process (or at least some of its basics)? A Mathematica emulator (not the heavy math stuff like integration and reduce), written Mathematica, but using a small subset of Mathematica's functions? I hope one of you thinks it is fun, because it would be far beyond my ability, but the results would at least be helpful to new users trying to understand what Mathematica does. Once you learned said small subset of functions, you could understand the emulator's code; and once you understood the emulator's code, you would understand Mathematica. I cannot pay though, so I hope this sounds fun.

POSTED BY: Anonymous User
9 Replies

Concerning the interest of the subject, since WL is an interpreted language, the compilation bootstrapping (wikipedia Compiler bootstraping) is not necessarily an applicable paradigm (although it will eventually happen in the near future). Nevertheless, considering the interpretation as a compilation substitute, I guess that this is somehow related... isn't it?

It is my understanding that the WL runtime library is being revamped mostly with C++ (or code of the same family), I guess with the purpose of having the fastest implementation possible. Nevertheless, I don't think it would be completely out of the question to have a runtime written on WL and compiled with the WL (compiled) compilation library... Most likely not as fast, as the compiler automatic optimization is eventually not capable of compensating for some astute human brain speed optimization strategies, but it could probably facilitate the port of the runtime to other platforms? (well, I guess that the programming language of the runtime is already LLVM compilation "compatible", facilitating the port to other platforms...). Would love to have a micro^2 kernel (one micro for the subset, another micro for the device aspect) running on a ESP32, both running binary and the interpreter (similar to micropython); but since there's still no LLVM translation to Xtensa... (in a few years...)

POSTED BY: Pedro Fonseca

I know this note is only slightly related but anyway: I guess you could find an ability to perform a 'step by step' evaluation useful. A set of functions related to Trace can help with that. There are even 3rd part utilities to make the result more readable:

The clearest way to represent Mathematica's evaluation sequence

POSTED BY: Kuba Podkalicki
Anonymous User
Anonymous User
Posted 6 years ago

That looks very interesting to me. Thank you.

POSTED BY: Anonymous User

The tutorial I linked to has this list:

  • Evaluate the head of the expression.
  • Evaluate each element in turn.
  • Apply transformations associated with the attributes Orderless, Listable, and Flat.
  • Apply any definitions that you have given.
  • Apply any built-in definitions.
  • Evaluate the result.

It also states:

As discussed in "Associating Definitions with Different Symbols", you can associate definitions with symbols either as upvalues or downvalues. The Wolfram System always tries upvalue definitions before downvalue ones.

Then, for additional clarification,

Definitions associated with g are applied before definitions associated with f in the expression f[g[x]].

and

in a sequence of elements, upvalues associated with earlier elements take precedence over those associated with later elements.


A special symbol is Sequence. It is flattened automatically by the evaluator. I am not sure about the order of operations related to Sequence, but a nice way to track is with On[] and Off[]. Do this in a command line session only, otherwise recent M versions will produce a flood of messages.

For example, define f[x_, y_, z___] := f[x + y, z], then evaluate On[], then

In[11]:= f[Sequence[1,2], 3]                                                    

f::trace: f[Sequence[1, 2], 3] --> f[1, 2, 3].

f::trace: f[1, 2, 3] --> f[1 + 2, 3].

Plus::trace: 1 + 2 --> 3.

f::trace: f[1 + 2, 3] --> f[3, 3].

f::trace: f[3, 3] --> f[3 + 3].

Plus::trace: 3 + 3 --> 6.

f::trace: f[3 + 3] --> f[6].

Out[11]= f[6]

What we see here is that Sequence is flattened out before any of f's transformation rules are applied.

Now you can tracing off with Off[].

I find this a clearer representation of what is happening than what can be obtained with the Trace functions. See also https://mathematica.stackexchange.com/q/29339/12


Finally, there are the Hold* attributes. These prevent the evaluation of arguments (second bullet point in first list).

When the evaluator encounters an expression that has a head with the HoldAll attribute, it does not evaluate or examine its arguments, except for one thing: it checks for the presence of Evaluate as a direct (first-level) argument, and if it finds one, it evaluates it. This is again a special rule.

Finally, there is Unevaluated, which is explained on its doc page:

f[Unevaluated[expr]] effectively works by temporarily setting attributes so that f holds its argument unevaluated, then evaluating f[expr].

For a practical introduction to working with Hold, Unevaluated and the Hold* attributes, see http://library.wolfram.com/infocenter/Courseware/377/

POSTED BY: Szabolcs Horvát
Anonymous User
Anonymous User
Posted 6 years ago

I had not seen On/Off in action until your example; they along with the helpful replies here may be enough to end my uncertainty about evaluation.

POSTED BY: Anonymous User

Since we were discussing Apply in another thread, consider coding it in Mathematica.

apply[f_, _[args___]] := f[args]

The SetDelayed machinery rewrites this a bit, and puts it into the database of downvalues keyed by the head apply. We can inspect this:

FullForm[DownValues[apply]

yields

List[RuleDelayed[HoldPattern[apply[Pattern[f,Blank[]],Blank[][Pattern[args,BlankNullSequence[]]]]],f[args]]]

All this pattern and blank stuff is really the foundation. Mathematica's design revolves around mathematics, not programming. Suppose I have the expression:

a (b + c) == a b + a c

The left and right sides are different structurally, so == doesn't recognize the equality. But Mathematica has functions that can rewrite this in a way that the two sides are identical, and thus further rewrite it to True. Applying either Expand or Factor does this. Simplify tries various rewrites until it finds one that leads to True. Mathematica is fundamentally a system for rewriting expressions, not programming. The language is designed to code rewrite rules. Expression rewriting can present a façade that looks like conventional programming, as in the apply definition above. If you really want to understand Mathematica from bottom up, though, you need to understand what things like RuleDelayed and Pattern represent. It's more like Unix sed than like an ordinary programming language.

I don't think implementing rewrite rules as Mathematica rewrite rules that imitate procedures that rewrite expressions is likely to be illuminating.

POSTED BY: John Doty
Anonymous User
Anonymous User
Posted 6 years ago

All this pattern and blank stuff is really the foundation. Mathematica's design revolves around mathematics, not programming..... Mathematica is fundamentally a system for rewriting expressions, not programming.

It is a turing machine, and while that may not interest some of its users, it is of the highest interest to others.

POSTED BY: Anonymous User

I am not sure this would be a very useful exercise because:

  • The best representation for a Mathematica expression in Mathematica is ... a Mathematica expression.
  • Then the evaluation of Mathematica expressions might be best done using the standard evaluator too.

Thus implementing Mathematica in Mathematica would simply mean sandboxing part of the system.

Of course one could construct a more complicated implementation, but that would not be an implementation of the smallest core of the Wolfram Language using the simplest parts of the Wolfram Language. Perhaps a simulation of a single evaluation step could be done using Replace.

If you want to understand the evaluation procedure in detail, I still suggest: http://reference.wolfram.com/language/tutorial/EvaluationOfExpressionsOverview.html

POSTED BY: Szabolcs Horvát
Anonymous User
Anonymous User
Posted 6 years ago

The best representation for a Mathematica expression in Mathematica is ... a Mathematica expression

That would teach no one anything about Mathematica's evaluation procedure, so I would not call it the best expression.

If you want to understand the evaluation procedure in detail, I still suggest: http://reference.wolfram.com/language/tutorial/EvaluationOfExpressionsOverview.html

I appreciate you reminding me of that link, as it is the official specification of evaluation in the Wolfram Language, but unfortunately I do not understand Wolfram-Language evaluation procedure in detail, even after studying that link. It is written in English prose, whereas the language specs I'm accustomed to are expressed formally. Ironically, where Wolfram uses prosaic English to specify their language, others use math to specify theirs. A Mathematica-emulator could be a first step in creating a formal language specification for the Wolfram programming language. A.I. wants to write Mathematica programs, but it can't write programs in languages it does not understand.

POSTED BY: Anonymous User
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract