8000 Instrumentation Ideas by pangloss · Pull Request #288 · sicmutils/sicmutils · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Instrumentation Ideas #288

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

pangloss
Copy link
Contributor
@pangloss pangloss commented Feb 17, 2021

While working on this project I found it difficult to be sure which function was actually getting dispatched with given arguments, and without searching the source code it was not easy to know what is implemented for a given function. This is an attempt to provide some tooling to address those issues.

The basic tools are g/method-dispatched and an added keyword argument to any function declared with g/defgeneric: :specifics:

(g/method-dispatched g/invert 1N)
;; => [:sicmutils.value/real]

(g/invert :specifics)
;; => ([:sicmutils.series/series]
;;     [:sicmutils.value/function]
;;     [:sicmutils.differential/differential]
;;     [:sicmutils.structure/structure]
;;     [:sicmutils.complex/complex]
;;     [:sicmutils.expression/numeric]
;;     [clojure.lang.Keyword]
;;     [:sicmutils.value/real]
;;     [:sicmutils.series/power-series]
;;     [:sicmutils.matrix/matrix]
;;     [:sicmutils.polynomial/polynomial])

The other idea is to optionally extend defmethod to capture data about nested calls to operators. This just provides a mechanism that the user can then work with in the repl as needed. Here is an example:

(def calls (atom []))
(defn call-stats [call]
  (swap! calls conj call))

(defn print-call-info [calls]
  (doseq [{:keys [name dispatch-with dispatch args chain result]} calls]
    (do
      (println (apply str (repeat (count chain) "  "))
               (if result
                 (str "=> " (pr-str result))
                 (pr-str (list* name args))))
      (when chain
        (println (apply str (repeat (inc (count chain)) "  "))
                 "types " dispatch-with)
        (println (apply str (repeat (inc (count chain)) "  "))
                 "using " dispatch)))))


(binding [sicmutils.generic/*generic-call-tap?* (constantly true)
          sicmutils.generic/*on-generic-call* call-stats]
  (((partial 2) (L3-central 'm V)) spherical-state))

(print-call-info @calls)

producing

(- #function[sicmutils.demo/T3-spherical/fn--48533] #function[sicmutils.demo/L3-central/Vs--48546])
   types  [:sicmutils.value/function :sicmutils.value/function]
   using  [:sicmutils.value/function :sicmutils.value/function]
 => #function[clojure.lang.AFunction/1]
 (partial-derivative #function[clojure.lang.AFunction/1] (2))
   types  [:sicmutils.value/function clojure.lang.ArraySeq]
   using  [:sicmutils.value/function clojure.lang.Sequential]
 => #function[clojure.lang.AFunction/1]
 (square D[[] → rdot [1004] → 1])
   types  [:sicmutils.differential/differential]
   using  [:sicmutils.differential/differential]
   (* rdot rdot)
     types  [clojure.lang.Symbol clojure.lang.Symbol]
     using  [:sicmutils.expression/numeric :sicmutils.expression/numeric]
   => (* rdot rdot)
   (+ rdot rdot)
     types  [clojure.lang.Symbol clojure.lang.Symbol]
     using  [:sicmutils.expression/numeric :sicmutils.expression/numeric]
   => (+ rdot rdot)
 => D[[] → (* rdot rdot) [1004] → (+ rdot rdot)]
...

@sritchie
Copy link
Member

Adding https://github.com/gfredericks/debug-repl here as a note, since you had mentioned this as a great resource. @pangloss , thank you for this! It's in my queue, which I know I keep mentioning.

This time I did not replace all of the calls to defmethod with this instrumented version, but if we were to use this, it would need to be used in all namespaces. It is probably more practical to exclude the core defmethod and import this one to the namespace to minimize the size of the diff
@pangloss
Copy link
Contributor Author
pangloss commented Apr 17, 2021

The original version of this PR became a big mess because I had replaced all of the calls to defmethod with g/defmethod. That aged faster than a fruitfly. I've cleaned that up in the hope that the core idea here can be a little more readily apparent now.

@codecov-commenter
Copy link

Codecov Report

Merging #288 (b538563) into master (b953c90) will decrease coverage by 0.35%.
The diff coverage is 53.57%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #288      +/-   ##
==========================================
- Coverage   83.04%   82.68%   -0.36%     
==========================================
  Files          92       92              
  Lines       10932    10957      +25     
  Branches      526      563      +37     
==========================================
- Hits         9078     9060      -18     
- Misses       1328     1334       +6     
- Partials      526      563      +37     
Impacted Files Coverage Δ
src/sicmutils/util/def.cljc 63.01% <53.57%> (-8.42%) ⬇️
src/sicmutils/generic.cljc 64.20% <0.00%> (-14.32%) ⬇️
src/sicmutils/numsymb.cljc 90.14% <0.00%> (+0.24%) ⬆️
src/sicmutils/numerical/elliptic.cljc 89.76% <0.00%> (+0.68%) ⬆️
src/sicmutils/polynomial/gcd.cljc 88.46% <0.00%> (+2.19%) ⬆️
src/sicmutils/util.cljc 87.27% <0.00%> (+3.63%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b953c90...b538563. Read the comment docs.

@sritchie sritchie added the researchy These are potentially open ended, tough design problems. label Nov 28, 2021
@sritchie sritchie force-pushed the main branch 3 times, most recently from afa6dd7 to c33ef42 Compare February 15, 2022 22:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
researchy These are potentially open ended, tough design problems.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0