A simple way to parse/check equations?



On 10/9/07, Juhana Antero Yrj?l? <jayrjola at cc.hut.fi> wrote:
> I have some kind of Maxima equation with functions and possible variables.
> Now I need to determine that all function calls are correct  and if not
> then display the incorrect function name with call parameters

You have two issues here: one is parsing, the other is analysis.

Maxima already does the parsing for you:

      EX: x^2-1$
      op(EX) => "+"      (Maxima treats the expressions as x^2 + (-1) )
      args(EX) => [ x^2 , -1 ]

(you might also want to look at inflag if you care about the
underlying internal representations)

As for the analysis, what is your definition of "correct"? In your
example below, how do you know that ex is "incorrect"? It seems that
you want to check not only the number but the type of arguments. In
this case, you will need to build some sort of structure defining
correct types, e.g.
          argtypes[sin]: [ [ scalar ] ] $
          argtypes[diff]: [ [any, variable], [any, variable, integer]
] $   -- Two possibilities

This is not built in to Maxima -- type checking is done by each
function in an ad hoc way.

Then you code to check the type. And you need to define a policy about
the interpretation of variables: clearly diff(x,'x,1) is legal, but
how about diff(Q,R,S)?  And your system will probably not want to
handle all cases: for example, there is no Q such that diff(Q,Q,Q) is
legal, since Q can't be both an integer and a variable).

> ...recursion must go all the way down.

Maxima (like all modern languages) supports recursion.  For example,
to see if an expression includes the variable Q, you can write

 includes_Q(expr):=
       if expr=Q then true
       elseif atom(expr) then false
       else member(true,maplist(includes_Q,expr))$

Good luck.  When is the assignment due?

           -s