17

My current understanding about Mathematica is that everything, at the lowest level, ends up as replacement rules.
First question: is this true?
Second question: does Mathematica "gloss over" these rules by representing them as functions? doesn't seem to be expressed as a rule.

Third question: does the by value/by reference question have any when you "pass" a value to a Mathematica function?

George Wolfe
  • 5,462
  • 21
  • 43
  • 6
    Well, obviously a system based on rules only, would only be able to do endless expression rewritings, but not much more. Even if one can build such a model of computation consistently, this is not what we see in Mathematica. From the user's viewpoint, (many) built-in functions are terminals, because their actions are no longer governed by rules. For example, when Mathematica sees Sin[0.15], it calls the built-in numerical implementation of Sin to make a computation - at which point it arguably leaves the rule-based paradigm. And similarly for most other useful lower-level actions. – Leonid Shifrin Oct 22 '13 at 01:56
  • @LeonidShifrin-I guess this should have been obvious. Rules are enough for doing algebra, but not for calculating a sine or loading a file. Is my simple function transformed into a rule? – George Wolfe Oct 22 '13 at 02:11
  • 6
    I was tempted to edit your title: "Do rules rule?" But my nose is still bleeding as a result of a limerick. – Dr. belisarius Oct 22 '13 at 02:19
  • @belisarius-I like your title more than mine. – George Wolfe Oct 22 '13 at 02:20
  • 3
    Have a look at DownValues[f] that's where the rules are hiding in this case – ssch Oct 22 '13 at 02:25
  • 1
    Rules are not capable to express many underlying results produced by Mathematica. For example consider what Reduce can do better than Solve, for more details read this post: What is the difference between Reduce and Solve? – Artes Oct 22 '13 at 02:33
  • 1
    David Wagners book, which is available as a free download here, has a good description of this. – Mike Honeychurch Oct 22 '13 at 02:56
  • 2
    @GeorgeWolfe Your simple function is a rule. The only top-level (user-defined) functions in Mathematica which are not rules, AFAIK, are pure functions (defined using Function). – Leonid Shifrin Oct 22 '13 at 02:56
  • 2
    @GeorgeWolfe But generally, rules seem to be the core paradigm of the Mathematica language - more fundamental than functional or structural or procedural ones. Roman Maeder directly admits this in his "Programming in Mathematica". – Leonid Shifrin Oct 22 '13 at 03:00
  • @belisarius You made my profile quotes. Again. – Mr.Wizard Oct 22 '13 at 11:19
  • @Mr.Wizard At your service :) – Dr. belisarius Oct 22 '13 at 13:25

1 Answers1

8

When you define a function f, you are (a) applying Set or SetDelayed to a pattern, (b) creating a rule, (c) associating the rule with a symbol (f), and (d) storing that in DownValues (usually).

FullForm[Hold[f[x_] := x^2]]
(* Hold[SetDelayed[f[Pattern[x, Blank[]]], Power[x, 2]]] *)

f[x_] := x^2
DownValues@f
(* {HoldPattern[f[x_]] :> x^2} *)

First question: is it true that "everything in Mathematica is ultimately stored as a rule?"

No: for example 2 is not a rule. It's just 2. So not everything is a rule, just functions. Everything is an expression, ... some expressions (as a side-effect) create and store rules in DownValues.

Second question: does Mathematica "gloss over" these rules by representing them as functions?

Yes and no. They give you a glossy shortcut for input:

f[x_] := x^2

instead of requiring you to write something like

AppendTo[DownValues[f], HoldPattern[f[x_]] :> x^2]

There was one misunderstanding in your question; you asked about the output of

FullForm[f[x]]

This is not showing you the definition of the function f. It is showing you the result of applying f to x. See the following:

FullForm[f[2]]
(* 4 *)

If you want to see the definition of f you should either use Information (which is glossy and not very useful) or DownValues which contains what you're after.

Third question: does the by value/by reference question have any when you "pass" a value to a Mathematica function?

Work on the assumption that Mathematica is pretty smart "under the hood" about pass by value/ pass by reference.

djp
  • 1,493
  • 14
  • 18