Evaluation control

The SourceEvaluation menu contains several primitives to control the way expressions in the style-sheet language are evaluated. The most frequent use of these primitives is when you want to write a “meta-macro” like new-theorem which is used for defining or computing on other macros. For instance:

<assign|new-theorem|<macro|name|text|<quasi|<assign|<unquote|name>|<macro|body|<surround|<no-indent><strong|<unquote|text>. >|<right-flush>|body>>>>>>

When calling <new-theorem|theorem|Theorem> in this example, we first evaluate all unquote instructions inside the quasi primitive, which yields the expression

<assign|theorem|<macro|body|<surround|<no-indent><strong|Theorem. >|<right-flush>|body>>>

Next, this expression is evaluated, thereby defining a macro theorem.

It should be noticed that the TeXmacs conventions for evaluation are slightly different then those from conventional functional languages like Scheme. The subtle differences are motivated by our objective to make it as easy as possible for the user to write macros for typesetting purposes.

For instance, when TeXmacs calls a macro <macro|x1||xn|body> with arguments y1 until yn, the argument variables x1 until xn are bound to the unevaluated expressions y1 until yn, and the body is evaluated with these bindings. The evaluation of yi takes place each time we request for the argument xi. In particular, when applying the macro <macro|x|x and again x> to an expression y, the expression y is evaluated twice.

In Scheme, the bodies of Scheme macros are evaluated twice, whereas the arguments of functions are evaluated. On the other hand, when retrieving a variable (whether it is an argument or an environment variable), the value is not evaluated. Consequently, a TeXmacs macro

<assign|foo|<macro|x|<blah|x|x>>>

would correspond to a Scheme macro

(define-macro (foo x)

‘(let ((x (lambda () ,x)))

(blah (x) (x)))

Conversely, the Scheme macro and function

(define-macro (foo x) (blah x x))

(define (fun x) (blah x x))

admit the following analogues in TeXmacs:

<document|<assign|foo|<macro|x|<eval|<blah|<quote-arg|x>|<quote-arg|x>>>>>>

<document|<assign|fun|<macro|x|<with|x*|x|<blah|<quote-value|x*>|<quote-value|x*>>>>>>

Here the primitives quote-arg and quote-value are used to retrieve the value of an argument resp. an environment variable. The TeXmacs primitives eval, quote, quasiquote and unquote behave in the same way as their Scheme analogues. The quasi primitive is a shortcut for quasi-quotation followed by evaluation.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".