29
$\begingroup$

I have come across the (internal) use of the function:

Internal`LocalizedBlock

I am trying to determine its purpose. It seem to behave like Internal`InheritedBlock except that a starting value (e.g. {x = 3}) cannot be set.

x = "global"; f[] := x

Internal`LocalizedBlock[{x}, {x, x = 7, f[], Hold[x]}]
x

Internal`InheritedBlock[{x}, {x, x = 7, f[], Hold[x]}]
x
{"global", 7, 7, Hold[x]}

"global"

{"global", 7, 7, Hold[x]}

"global"

What purpose does this function serve? Why would it be used in place of InheritedBlock?

$\endgroup$

2 Answers 2

35
+500
$\begingroup$

Internal`LocalizedBlock behaves the same as Block, but it can localize non-Symbols (e.g. f[1], Subscript[x, 0], etc.).

For example,

Internal`LocalizedBlock[{Subscript[x, 0]}, Subscript[x, 0] = 1]
(* 1 *)

Compare this to

Block[{Subscript[x, 0]}, Subscript[x, 0] = 1]
(* During evaluation of In[79]:= Block::lvsym: Local variable specification {Subscript[x, 0]} contains Subscript[x, 0], which is not a symbol or an assignment to a symbol. >> *)
(* Block[{Subscript[x, 0]}, Subscript[x, 0] = 1] *)

It's also worth noting that one cannot assign values in the first argument of Internal`LocalizedBlock

$\endgroup$
5
  • $\begingroup$ So would Internal`LocalizedBlock be an answer to this question: mathematica.stackexchange.com/questions/64673/… $\endgroup$
    – QuantumDot
    Jan 16, 2015 at 20:37
  • $\begingroup$ @QuantumDot It seems so. Perhaps you wish to self-answer? $\endgroup$
    – Mr.Wizard
    Jan 16, 2015 at 20:41
  • $\begingroup$ @Mr.Wizard I guess I'm not familiar enough with Inherited`LocalizedBlock to provide a confident answer. Maybe I'll add it in the comments section. $\endgroup$
    – QuantumDot
    Jan 16, 2015 at 22:29
  • 9
    $\begingroup$ @Mr.Wizard just a little tidbit, if I recall correctly this was added for the purpose of fitted models being able to work with subscripted variables so I think Dan is right on here. $\endgroup$
    – Andy Ross
    Jan 17, 2015 at 5:55
  • $\begingroup$ @Andy Thanks for the note. $\endgroup$
    – Mr.Wizard
    Jan 17, 2015 at 14:46
12
$\begingroup$

Disclaimer: these are all guesses, believe at your own risk.

As the first argument, you specify a list of patterns to localize. For example, {x}, {x[2]}, {x[][2]}, or x[2]/;True

This creates a dynamic environment around the second argument inside which the following happens:

  • Changes to those patterns' own/down/subvalues while in the environment aren't kept after its execution.
  • If the pattern is

    • a symbol (e.g x, f):

      works like block, and all of the symbol's definitions don't apply inside the localized block

    • a non-pattern pattern that doesn't have a downvalue/subvalue outside the environment: (by non-pattern pattern I just mean a pattern that don't have conditions, pattern tests, blanks, etc. such as x[2]] or x[][5]. I wonder if there's a standard name in the community for these)

      does nothing

    • any other pattern patt:

      Temporarily set the definition patt:=System`Private`$Localized. This is a definition that makes the whole downvalue/subvalue lookup fail to find a match. Here is a usage example;

Low importance note: this changes seem to be applied in order of appearance, so if the first argument is {x, x[2]/;True}, then the definition x[2]/;True:=SystemPrivate$Localized will be added, while it won't be with the order reversed to x[2]/;True, x}

Summary

You can use it to

  • Change specific down/sub/ownvalues of a symbol while in a dynamic environment without it affecting the outside, but allowing all other definitions changes to leak.
  • Work with the outside definitions of the symbol but making some particular pattern behave as undefined.
$\endgroup$
5
  • $\begingroup$ Is this how you propose Internal`LocalizedBlock works or simply an idea you wish to implement some time? $\endgroup$
    – Mr.Wizard
    Jan 16, 2015 at 18:47
  • $\begingroup$ @Mr.Wizard how I think it works. Does it fail easy tests? $\endgroup$
    – Rojo
    Jan 16, 2015 at 18:47
  • $\begingroup$ No, I just wasn't sure of your intent. :-) $\endgroup$
    – Mr.Wizard
    Jan 16, 2015 at 18:48
  • $\begingroup$ Ok, I now can test this. Let's see $\endgroup$
    – Rojo
    Jan 16, 2015 at 19:15
  • 2
    $\begingroup$ believe it or not $\endgroup$ Jan 16, 2015 at 20:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.