HANDBOOK / GENTLE PRIMER / At a Glance /

# Transformation

We now look at a more interesting algorithm that works on the abstract syntax. We introduce the placeholder `x` and interpret an expression as a function in `x`. Given such an expression E, we will compute an expression E' that represents the derivative of the function.

We extend the abstract syntax by the alternative

       x

and add to the concrete syntax the rule

       'rule' expr3(-> x): "x"

The root clause now takes the form

    'root' expression(-> X) deriv(X -> D) print(D)

The procedure `deriv` is then defined as follows

    'action' deriv (Expr -> Expr)

    'rule' deriv(mult (U,V) -> plus(mult(Ud,V), mult(U,Vd))):
       deriv(U -> Ud)
       deriv(V -> Vd)
    'rule' deriv(div(U,V) -> div(minus(mult(Ud,V),mult(U,Vd)),mult(V,V))):
       deriv(U -> Ud)
       deriv(V -> Vd)
    'rule' deriv(plus(U,V) -> plus(Ud, Vd)):
       deriv(U -> Ud)
       deriv(V -> Vd)
    'rule' deriv(minus(U,V) -> minus(Ud, Vd)):
       deriv(U -> Ud)
       deriv(V -> Vd)
    'rule' deriv(num(N) -> num(0))
    'rule' deriv(x -> num(1))