'load', try again added by sytse on Fri Feb 5 16:17:31 2021


;; The definition of "load" in the prelude:

($define! load
  (wrap
   ($vau (filename) env
     ($import! emacs-global kernel-read-file)
     (eval (cons $sequence
                 ($compile-time
                   (kernel-read-file (string-append filename ".knl"))))
           env))))

;; Importing from "emacs-global" means, that if you call the binding
;; of "kernel-read-file", it will produce an elisp function call to
;; the symbol 'kernel-read-file interned in "obarray".
;; 
;; This then gets compiled into a meta-operative, where "arg0" is the
;; filename argument, and "arg1" is the dynamic environment at the
;; "load" call site:

((arg0 arg1)
  (=<< /eval-sequence
       (pure arg1)
       (=<< /call-compile-time
            (/bind-closures #{() #ignore ((kernel-read-file (string-append filename ".knl")))} arg0))))


;; The intent of "$compile-time" is, to have the body compiled to a
;; program, that gets executed only when all closures are known
;; values: in this case, that means that the value of "filename" as a
;; string has to be known at compile time.
;;
;; The compiler should always inline a function, like "load", that
;; contains such a "call-compile-time" graph node.  The most common
;; use for "load" is actually via "load-module", which simply calls
;; it; then the meta-operative for "load-module" will still contain
;; /call-compile-time, such that when you call
;;
;; (load-module "miniKanren")
;;
;; it will inline load-module, and THEN the compiler knows the string
;; that it should use as an argument for the lambda function produced
;; by $compile-time, so then it will call it.

;; That lambda function,
;;
;; ($λ () (kernel-read-file (string-append filename ".knl"))),
;;
;; is then compiled into the following meta-operative:

((closure-filename)
  (=<< ^block-return
       (=<< /call-elisp
            (constant %el:{kernel-read-file})
            (/concat closure-filename ".knl"))))

;; which turns into elisp byte code:

load → λ (1 args): 6 ops in 6 bytes, maxdepth 4, 0 tags, 2 constants
constant %el:{kernel-read-file}
stack-ref 1
constant ".knl"
concat2
call 1
return

;; ...which is then finally called with the string "miniKanren" as its
;; argument, returning the source code of a "miniKanren.knl" in
;; (load-path) as a sexpr.
;;
;; The return value is then given to /eval-sequence (see the
;; meta-operative for "load"), which does a monadic fold over the
;; statements in miniKanren.knl, giving each list element to /eval: in
;; other words, compilation proceeds as normal.