Differentiation in SICM

The paper The Role of Programming in the Formulation of Ideas is by Sussman and Wisdom, the authors of the SICM book. It describes how differentiation of functions is performed. It is a great Introduction to the book.

Do it in code

(This section still needs a lot of work) (It should show how the differentiation algorithm of Sicmutils works)

We include the Sicmutils library and the transpiler


var insertCommas = (txt) =>
txt.replace(/(\w+)/g,'"$1",')
.replace(/\,\s+\]/g," ]");

var makeBrackets = (txt) =>
txt.trim()
.replace(/\(/g,"[ ")
.replace(/\)/g," ],")
.replace(/,$/,"");

var textToJson = (txt) =>
insertCommas(makeBrackets(txt));

var jsonToJs = (j) =>
j.constructor == Array
? jsonToJs(j[0]) + ".call(null,"  +  j.slice(1).map(jsonToJs) + ")"
: j;

var expressionToJs = (expr) =>
jsonToJs(JSON.parse(textToJson(expr))) + ";";

    

Converting the Scheme syntax into a nested JavaScript array (the so called AST).


(up (cos 0) (sin 0))
    

Scheme syntax transpiled to Javascript


;((up cos sin) 0)
    

Some JS imports


["up","cos","sin","square","compose"].map(requireEmmy);
requireEmmyRename("D","Drv");
    

Scheme syntax executed


((up cos sin) 0) 
    

((up cos (compose cos sin)) 0) 
    

Here we do differentiation


((Drv square) 3) 
    

((up
(compose square sin)
(Drv (compose square sin))) 1)