[racket] Writing algebraic maths expressions

From: Norman Gray (norman at astro.gla.ac.uk)
Date: Sat Mar 7 07:11:28 EST 2015

Greetings.

Is there a recommended way of writing algebraic maths expressions within Racket programs?  I mean something like (maths 1 + 3 * x) rather than (+ 1 (* 3 x)).

I'm trying to evaluate a number of expressions which aren't mathematically exotic, but which are numerous and fiddly, and it's really painful trying to convince myself that what I've transcribed into Racket matches the expression on the paper.  I've briefly considered writing a parser myself, and writing a LaTeX prettyprinter, to help compare what I've transcribed with what I started with, but while both of these are doubtless feasible and educational, neither should be my first priority at this point in this project.

----

What I've got so far:

Honu is an obvious place to look, but it appears that it's expected to be used as the syntax for a complete module rather than fragments, and that's unattractive, because then I'd have to familiarise myself with the complete syntax rather than just the syntax of mathematical expressions (interesting, of course, but in this context a distraction).  Also, because (i) the documentation stresses that it's provisional.  I thought (ii) I might be able to use (require honu/core/read) and honu-read, but when I try (call-with-input-string "a * b" honu-read) I get '(a * b) which makes me think that Honu's goals are not what I think they are, and a skim through <http://www.cs.utah.edu/~rafkind/papers/honu11.pdf> corroborates that.

Another possibility is Sweet expressions, as described at <http://pkg-build.racket-lang.org/doc/sweet/index.html> and <http://readable.sourceforge.net>.  That feels plausible, but (i) the port doesn't seem complete, since when I try:

#lang sweet-exp racket/base

let
  \\
    a 1
  *(a a)

(see <http://sourceforge.net/p/readable/wiki/Scheme-tutorial/>, at 'Grouping and Splitting'), I get 'let: bad syntax (not an identifier and expression for a binding) in: |\\|'.  Also, because (ii) while the syntax does have provision for including sweet-expressions within normal Racket syntax (the inline {...}) syntax), it's clear that this interacts in a slightly complicated way with the surrounding syntax, so that one has to be mindful of where semicolons and closing braces go; that again makes it a little harder to be confident I'm evaluating the expression I think I'm evaluating.  Finally, (iii) the inline syntax deliberately (and intelligibly) doesn't support a precedence-based syntax for things like '1 + 2*3'.

I don't see anything in the list at <http://pkgs.racket-lang.org>, searching for 'algebra', 'math' or 'syntax' (well, looking at the goodies in the last case lets me find 'ragg', which might be useful if I do write myself a parser, but... must. not. get. deflected...)

----

So:

I'm sure I can work through the puzzles above, but at this point the path is feeling awfully untrodden, and since I can't believe I'm the first person with this goal, I feel sure I've taken a wrong turning someplace.

Thanks for any pointers.  Best wishes,

Norman


-- 
Norman Gray  :  http://nxg.me.uk
SUPA School of Physics and Astronomy, University of Glasgow, UK



Posted on the users mailing list.