Reading infix notation to scheme

Hello all,

Of late, I make simple plots from right within TeXmacs in graphics mode using Shift-! and adding the code ($graph2d a b steps fun).
It is cumbersome to write the fun in prefix notation required by Scheme. But I am wondering, if there is some included module that can convert the typical infix notation found in our math expressions and converts them into prefix. That way, we can simply take a math expression in TeXmacs, do a “Copy to Scheme Code” and paste it into (a modified) $graph2d.

Thanks in advance for your help, as always.
Kind regards,

no to my knowledge. It would be nice to have indeed and the graph editor could have a more user-friendly interface. I found this

which partially would solve the problem. Ideally one would like just to enter a formula in TeXmacs and have it converted in scheme to be used in the graphics mode. In TeXmacs there is a parser which is able to parse mathematical formulas (it is used for checking their semantic correctness when enabled) but to my knowledge there is no inteface to extract the result of such parsing. A possibility would be to use the MathML conversion (or copy the code and modify it) to implement a parser which would produce the correct scheme code form the TeXmacs representation.

(texmacs->mathml (cadr (tree->stree (generic->texmacs "$1+1*(2+3)$" "latex-snippet"))))

For example this scheme code returns the MathML representation for the above expression.

I have found this library which at first sight seems very good:
I haven’t yet had the possibility to test it.
Also SRFI-105 is in theme

1 Like

@pireddag these packages are nice. Unfortunately Infix uses syntax-case which is not available in Guile 1.8. However the SRFI-105 seems to be more likely to work in our scheme in one way or another. Actually I’ve just checked that Guile 3.0 has this SRFI available by default. It could be useful to have it also in TeXmacs, however probably one has to modify the reader.

It seems indeed one can take SRFI-105 and use the reader extensions of Guile 1.8

to implement something like #{ 1 + (b / c) }. The exercise is left… to the reader.

I will check it. I did not yet check what it does for functions as well.

Dear Max @mgubi and Giovanni @pireddag
Thanks for looking up on this. Your conversation has gone above my head quickly. However, further search lead me to this site
The author says that the code was initially written in guile 1.8.8 and converted with minor changes for Racket. Since TeXmacs is written in Guile 1.x, I thought I might bring it to your attention … for what it may be worth.
Thanks again.

Nice find. I will fork it and see if I can turn it (or one of the other packages) into a TeXmacs module. It will take me a bit, please do not expect quick news on this topic.
On the other hand, it might be that it is very easy (I do not know yet), in this case you might try even with no experience in Scheme. It might be enough to copy the functions into a file and add to the main one (I do not have the code under my eyes, but I have in my “mind’s eyes” one file which loads the other ones) a TeXmacs module declaration. After this, place them in the subdirectory of progs corresponding to the declaration, identify the functions that you want to have available in TeXmacs and substitute in them tm-define for define: it is ready for loading into a document with use-modules.

I have played with the code at and I adapted it to TeXmacs; one needs to transform the files loaded with load-relative into modules and change the name of the function operator? (I think that a function of the glue has the same name, but I did not check). One can then use to evaluate arithmetical expressions in the infix form; the exclamation mark for the factorial, which is present in the example of the readme file does not work. At the moment as far as I can see it is possible to use the package to only calculate arithmetical expressions, though maybe one can adapt the functions to reorganize the form of a general expression (I did not put my energy into understanding how the functions work).

I will first look into SRFI-105 though.

Said this, since the task of the function is to reorganize the form of an expression, maybe it is more sensible to use a macro instead of a function.

I will write again when I have more news.

Indeed, a macro solution could be more interesting since you can mix it with regular scheme code. The SRFI-105 is interesting and I checked that the code can be used with not much modifications. It a good exercise for somebody which want to learn. :slight_smile:

The code at the above github does not appear very useful. I loaded all the files into a Scheme session but his main function (calc …) is too limited to be useful.
SRFI-105 — I do not know how these things work, I copied all the code at the bottom of their webpage into a scheme file and again loaded it into a Scheme session in TeXmacs. But there do not appear to be any function in there to do some kind of infix->postfix etc.

So, will wait until you have time next.

Add the following line at the end of your file:

(read-hash-extend #\{ (lambda (ch port) 
    (process-curly (my-read-delimited-list  neoteric-read #\} port))))

and load the file in a scheme session with (load "myfile.scm"). Then you should be able to do

'#{ sin(x) + cos(x) * { 3 + 5 } }

and obtain as a result the corresponding Scheme expression. (you have to use { } instead of parenthesis, see the SRFI)

1 Like


Thanks both of you (@mgubi and @pireddag). This is certainly a great help. I appears I have to provide the precedence of the operators by enclosing the expressions in the curly brackets appropriately, but for simple graphs I am plotting, this is not an issue.