0

Consider the following (python) code

x = [1, 2, 3, 4]
y = 2 + 3
z = x[0] + y

Here, I can substitute the expressions for x and y verbatim in the expression for z, possibly by surrounding then in some grouping syntax:

y = [1, 2, 3, 4][0] + (2 + 3)

Not all languages have this feature. Consider the following equivalent Matlab code

x = [1, 2, 3, 4];
y = 2 + 3
z = x(1) + y;

Here, no such transformation is possible, and both of the following would be a syntax error:

z = [1, 2, 3, 4](1) + (2 + 3);
z = ([1, 2, 3, 4])(1) + (2 + 3);

PHP behaves in a similar way.


Does this property of a language have a name? Is there a concise way to describe this property of a language? Specifically, being able to make source-code substitutions of expression subtrees

  • 2
    Yes, it's called substitution. If you're looking for an adjective, substitutability. Two functions or expressions may or may not be compatible with each other for reasons of type compatibility. See cs.cornell.edu/courses/cs312/2008sp/lectures/lec05.html for more information on "The Substitution Model of Evaluation." – Robert Harvey May 12 '17 at 17:30
  • 1
    I don't think it has anything to do with grammar. It has to do with the language specifications. Matlab apparently does not support the required expression evaluations to make this work. It's not a "feature," as such, nor is it likely to have a name other than something like "incremental evaluation of nested or complex numeric expressions." I think it's fair to say that this capability is generally expected in any programming language worth its salt; perhaps it's omitted from Matlab for performance reasons. – Robert Harvey May 12 '17 at 17:45
  • 2
    It's also entirely possible you're not doing it right in Matlab and it does have this capability. I haven't written anything in Matlab myself, so I can't say for sure. – Robert Harvey May 12 '17 at 17:54
  • 1
    have you considered "referential transparency"? It talks about substituting values, but might possibly apply to substituting expressions as symbolic values. – Erik Eidt May 12 '17 at 18:45
  • 2
    @RobertHarvey: MATLAB does not have this property. Array subscripting can only be performed on variables and certain expressions (such as cell subscripts and field references) but not general expressions. So even if you think MATLAB" not worth its salt, it is a widely used language and the question is perfectly legitimate – Andreas H. May 12 '17 at 18:53
1

It's called "variables"! In a language with immutable variables (versus mutable/assignable variables), the semantics by definition says that we just replace a variable by the expression assigned to it wherever it occurs. This is generally a property of languages based on the lambda calculus (e.g. System F, Haskell, ML, Idris, etc).

The substitution operator is written [M/x]N: we replace any free occurrences of the variable x in N with M. For example, the dynamics of function application makes use of this operator: if we have a lambda abstraction lambda x: int. x + x, then

(lambda x: int: t. x + x)(5) -> [5/x](x+x) -> 5+5 -> 10.

0

What you're describing is call substitution, or inlining.

Typically inlining is used to refer the process done by the compiler, in which expressions like y are evaluated, and their result substituted in place of the references to y.

It's not necessarily limited to variables, either. Functions can be inlined, which allows the body of the function to be substituted for the call to the function. This typically increases the application's size, but improves performances by removing the need for a function call to be performed.

  • "Inlining" is a compiler-specific term. "Substitution" is a more general term that can be applied to more situations (including language syntax) than the very specific thing that a compiler does when it inlines. So "substitution" is the better term here. – Robert Harvey May 12 '17 at 19:50
  • 1
    @RobertHarvey I agree, but inlining is used colloquially here too. Actually, more than colloquially, because some IDEs refactorers call it that – Alexander May 12 '17 at 20:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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