forked from sicmutils/sicmutils
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from sicmutils/sritchie/sci_tests
Get SCI macros tested + working
- Loading branch information
Showing
10 changed files
with
289 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,113 @@ | ||
;; | ||
;; Copyright © 2020 Colin Smith. | ||
;; This work is based on the Scmutils system of MIT/GNU Scheme: | ||
;; Copyright © 2002 Massachusetts Institute of Technology | ||
;; | ||
;; This is free software; you can redistribute it and/or modify | ||
;; it under the terms of the GNU General Public License as published by | ||
;; the Free Software Foundation; either version 3 of the License, or (at | ||
;; your option) any later version. | ||
;; | ||
;; This software is distributed in the hope that it will be useful, but | ||
;; WITHOUT ANY WARRANTY; without even the implied warranty of | ||
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
;; General Public License for more details. | ||
;; | ||
;; You should have received a copy of the GNU General Public License | ||
;; along with this code; if not, see <http://www.gnu.org/licenses/>. | ||
;; | ||
|
||
(ns sicmutils.env.sci | ||
(:refer-clojure :exclude [eval]) | ||
(:require [clojure.set :as set] | ||
[sci.core :as sci] | ||
(:require [sci.core :as sci] | ||
[sicmutils.env] | ||
[sicmutils.env.sci.macros :as macros] | ||
[sicmutils.env :as env] | ||
[sicmutils.abstract.function :as af] | ||
[sicmutils.calculus.coordinate :as cc])) | ||
|
||
(defn ->sci-var [[var-name the-var]] | ||
[var-name (cond-> (deref the-var) | ||
(-> the-var meta :macro) | ||
(with-meta {:sci/macro true}))]) | ||
[sicmutils.util :as u])) | ||
|
||
(defn ->sci-ns [publics] | ||
(into {} (map ->sci-var) publics)) | ||
(def macro? (comp :macro meta)) | ||
(def dynamic? (comp :dynamic meta)) | ||
|
||
(def namespaces | ||
{'sicmutils.env (-> 'sicmutils.env | ||
ns-publics | ||
(dissoc 'literal-function | ||
'with-literal-functions | ||
'bootstrap-repl! | ||
'let-coordinates | ||
'using-coordinates) | ||
->sci-ns | ||
(defn ns-macros | ||
"Given a map of symbol => var, returns a sequence of the symbols associated with | ||
macro value." | ||
[sym->var] | ||
(mapcat (fn [[sym var]] | ||
(if (macro? var) [sym] [])) | ||
sym->var)) | ||
|
||
(merge (select-keys macros/all ['literal-function | ||
'with-literal-functions | ||
'let-coordinates | ||
'using-coordinates]))) | ||
'sicmutils.abstract.function (-> 'sicmutils.abstract.function ns-publics ->sci-ns) | ||
'sicmutils.calculus.coordinate (-> 'sicmutils.calculus.coordinate | ||
ns-publics | ||
->sci-ns | ||
(merge (select-keys macros/all ['let-coordinates | ||
'using-coordinates])))}) | ||
(defn sci-ns | ||
"Given a map of symbol => var, returns a map of symbol => var with: | ||
(def opts {:namespaces (set/rename-keys namespaces {'sicmutils.env 'user})}) | ||
- any pair removed whose value is a macro (tagged with `:macro true` metadata) | ||
- all other values resolved" | ||
[sym->var] | ||
(letfn [(process [[sym var]] | ||
(cond | ||
;; Inside SCI, macros are replaced by rewritten-as-functions | ||
;; versions of themselves, with additional slots for `&form` and | ||
;; `&env`. We exclude them here so they can be replaced later. | ||
(macro? var) [] | ||
|
||
(def ctx (sci/init opts)) | ||
;; Keep dynamic variables as unresolved vars, so that they can | ||
;; at least be inspected (at which point they'll reveal any | ||
;; rebindings applied by the system) | ||
(dynamic? var) [[sym var]] | ||
|
||
(comment | ||
(defn eval [form] | ||
(sci/eval-string* ctx (pr-str form))) | ||
;; by default, the SCI environment holds values, not the vars | ||
;; that they were attached to in non-SCI land. | ||
:else [[sym @var]]))] | ||
(into {} (mapcat process) sym->var))) | ||
|
||
(eval '(simplify (+ (square (sin 'x)) | ||
(square (cos 'x))))) | ||
(def ^{:doc "Map whose values are the symbols of of all namespaces explicitly | ||
checked and whitelisted for SCI compilation and interesting enough in their own | ||
right to expose to a user by default. Each value is the sym->var map for the | ||
corresponding namespace."} | ||
ns-map | ||
{'sicmutils.env (ns-publics 'sicmutils.env) | ||
'sicmutils.generic (ns-publics 'sicmutils.generic) | ||
'sicmutils.function (ns-publics 'sicmutils.function) | ||
'sicmutils.operator (ns-publics 'sicmutils.operator) | ||
'sicmutils.series (ns-publics 'sicmutils.series) | ||
'sicmutils.simplify (ns-publics 'sicmutils.simplify) | ||
'sicmutils.structure (ns-publics 'sicmutils.structure) | ||
'sicmutils.matrix (ns-publics 'sicmutils.matrix) | ||
'sicmutils.abstract.function (ns-publics 'sicmutils.abstract.function) | ||
'sicmutils.calculus.basis (ns-publics 'sicmutils.calculus.basis) | ||
'sicmutils.calculus.coordinate (ns-publics 'sicmutils.calculus.coordinate) | ||
'sicmutils.calculus.covariant (ns-publics 'sicmutils.calculus.covariant) | ||
'sicmutils.calculus.derivative (ns-publics 'sicmutils.calculus.derivative) | ||
'sicmutils.calculus.form-field (ns-publics 'sicmutils.calculus.form-field) | ||
'sicmutils.calculus.manifold (ns-publics 'sicmutils.calculus.manifold) | ||
'sicmutils.calculus.map (ns-publics 'sicmutils.calculus.map) | ||
'sicmutils.calculus.vector-field (ns-publics 'sicmutils.calculus.vector-field) | ||
'sicmutils.expression.compile (ns-publics 'sicmutils.expression.compile) | ||
'sicmutils.expression.render (ns-publics 'sicmutils.expression.render) | ||
'sicmutils.mechanics.lagrange (ns-publics 'sicmutils.mechanics.lagrange) | ||
'sicmutils.mechanics.hamilton (ns-publics 'sicmutils.mechanics.hamilton) | ||
'sicmutils.mechanics.rigid (ns-publics 'sicmutils.mechanics.rigid) | ||
'sicmutils.mechanics.rotation (ns-publics 'sicmutils.mechanics.rotation) | ||
'sicmutils.numerical.derivative (ns-publics 'sicmutils.numerical.derivative) | ||
'sicmutils.numerical.quadrature (ns-publics 'sicmutils.numerical.quadrature) | ||
'sicmutils.numerical.ode (ns-publics 'sicmutils.numerical.ode) | ||
'sicmutils.numerical.minimize (ns-publics 'sicmutils.numerical.minimize) | ||
'sicmutils.numerical.interpolate.polynomial (ns-publics 'sicmutils.numerical.interpolate.polynomial) | ||
'sicmutils.numerical.interpolate.rational (ns-publics 'sicmutils.numerical.interpolate.rational) | ||
'sicmutils.numerical.interpolate.richardson (ns-publics 'sicmutils.numerical.interpolate.richardson) | ||
'sicmutils.numerical.multimin.nelder-mead (ns-publics 'sicmutils.numerical.multimin.nelder-mead) | ||
'sicmutils.numerical.unimin.bracket (ns-publics 'sicmutils.numerical.unimin.bracket) | ||
'sicmutils.numerical.unimin.brent (ns-publics 'sicmutils.numerical.unimin.brent) | ||
'sicmutils.numerical.unimin.golden (ns-publics 'sicmutils.numerical.unimin.golden)}) | ||
|
||
(eval '(->TeX (simplify (+ (square (sin (square 'x))) | ||
(square (cos 'x)))))) | ||
(def ^{:doc "Default sci context options required (currently only `:namespace` | ||
bindings) required to evaluate SICMUtils forms from inside of an SCI | ||
context. Pass these to `sci/init` to generate an sci context."} | ||
context-opts | ||
(let [ns-map (into {} | ||
(map (fn [[k v]] [k (sci-ns v)])) | ||
ns-map) | ||
with-macros (merge-with merge ns-map macros/ns-bindings)] | ||
{:namespaces with-macros})) | ||
|
||
(eval '(literal-function 'U)) | ||
(eval '(do (defn L-central-polar [m U] | ||
(fn [[_ [r] [rdot φdot]]] | ||
(- (* 1/2 m | ||
(+ (square rdot) | ||
(square (* r φdot)))) | ||
(U r)))) | ||
(let [potential-fn (literal-function 'U) | ||
L (L-central-polar 'm potential-fn) | ||
state (up (literal-function 'r) | ||
(literal-function 'φ))] | ||
(->TeX | ||
(simplify | ||
(((Lagrange-equations L) state) 't))))))) | ||
(def ^{:doc "sci context (currently only `:namespace` bindings) required to | ||
evaluate SICMUtils forms via SCI"} | ||
context | ||
(sci/init context-opts)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.