I have two stacks, a goal and a stack I'm altering. As I accomplish each step of the goal-stack, I remove a field from it. I know I've finished a particular goal when that stack is now empty. I'm trying to write a rule to test when the variable is empty, but I keep getting an error: [EXPRNPSR1] A function name must be a symbol. Here is my rule.
(defrule done
(declare (salience 30))
?stack <- (curr-stack $?thusfar)
?goal <- (goal-stack ?H)
(test (= ?H ()))
=>
(retract ?stack ?goal))
Any help is appreciated! Thanks!
$?foo is a multi-field variable (0 or more things in it).
?foo is a single field variable (1 thing only).
So, testing ?foo for "emptiness" won't do you much good as far as you've indicated for your stacks.
Does my multi-field variable have at least one thing in it?
(test (> (length $?foo) 0))
Is my multi-field variable empty of things?
(test (= (length $?foo) 0))
Related
I'm trying to add completion at point for frame-local variables from backtrace-frames during invocations of read--expression by debugger-eval-expression or edebug-eval-expression.
I constructed the following completion table to add frame-local variables to the already available table for local elisp variables,
;; completion table for locals in current frame
(defvar my-backtrace-locals-completion-table
(completion-table-in-turn
(completion-table-dynamic
(lambda (_string)
(when-let* ((idx (backtrace-get-index)) ;backtrace.el
(frame (nth idx backtrace-frames)))
(backtrace-frame-locals frame)))
'do-switch-buffer)
elisp--local-variables-completion-table)) ;elisp-mode.el
which seems to work fine, eg. to reproduce
(1) evaluate
;; debug-on-error = t
(let ((my-local-var '(1 2))) (mapcan #'car this-local-var))
(2) from debugger's second frame, evaluate with eval-expression
(funcall my-backtrace-locals-completion-table "my-" nil t)
returns expected ("my-local-var").
The problem is following the above steps, but calling instead calling debugger-eval-expression doesn't work. The environment where the table is evaluated isn't finding a backtrace-frame (with or without do-switch-buffer).
How can I define the table to be evaluated in the proper buffer?
emacs v27.0.50
The completion table above doesn't quite return the expected candidates for debugger-eval-expression. The environment where the expression is evaluated has locals from frames higher than, but not including, the one at point in the Backtrace buffer.
So, the available locals should be only those from higher frames, eg.
(eval-when-compile (require 'dash))
(defvar my-backtrace-locals-completion-table
(completion-table-dynamic
(lambda (_string)
(when backtrace-frames
(--mapcat
(-some->> (backtrace-frame-locals it) (--map (car it)))
(nthcdr (1+ (backtrace-get-index)) backtrace-frames))))
'do-switch-buffer))
Then, redefining debugger-eval-expression's interactive spec to use the new locals table in place of the normal elisp table provides the correct completions (passing the 'do-switch-buffer arg completion-table-dynamic to find completions in the original buffer).
(defun my-backtrace#eval (orig-fn exp &optional nframe)
(interactive
(let ((elisp--local-variables-completion-table
my-backtrace-locals-completion-table))
(list (read--expression "[my] Eval in stack frame: "))))
(apply orig-fn (list exp nframe)))
(advice-add 'debugger-eval-expression :around #'my-backtrace#eval)
I need help to write two rules, that add and remove values from queue. Rule that removes value from queue must use forall structure.
As far as I know, CLIPS does not have arrays, stack, lists or any other type of collections. So I have started with defining template queue with a slot item, that should represent value of a queue, but have not succeeded with the rules. Does anyone have any idea how this can be done?
CLIPS has multifield objects which act as lists.
You can define your queue as a global variable and modify it in your rules as for this example (LIFO queue).
CLIPS> (defglobal ?*queue* = (create$))
; add "foo" to the queue
CLIPS> (bind ?*queue* (insert$ ?*queue* 1 "foo"))
("foo")
; add "bar" to the queue
CLIPS> (bind ?*queue* (insert$ ?*queue* 1 "bar"))
("bar" "foo")
; remove first element from the queue
(bind ?*queue* (delete$ ?*queue* 1 1))
("foo")
I'm working through the Programming Languages: Application and Interpretation book chapter 6 http://cs.brown.edu/courses/cs173/2012/book/From_Substitution_to_Environments.html
I've applied a fix as described in the book but the cons is not adding the type to the empty list refered to in the source.
I think it's a pass-by-value/pass-by-ref thing, any clues on how to set the mt-env when it's not being passed in as a parameter?
#lang plai-typed
;; Binding types
(define-type Binding
[bind (name : symbol) (val : number)])
;; some helper functions:
(define-type-alias Env (listof Binding))
(define mt-env empty)
(define extend-env cons)
;; testing function
(define (addBinding [b : Binding] [env : Env])
(extend-env b env)
)
(addBinding (bind 'x 5) mt-env) ;; returns (list (bind x 5))
(display mt-env) ;; returns empty list
Below is a link to the complete code for context if required, the interp function's appC case is the specific location of my problem area, thanks.
https://github.com/MickDuprez/plai/blob/master/Chapter%206/chapter-6.rkt
After re-reading the last part of this chapter a few times I don't think there is a simple solution to this problem. The 'change' only makes the revised interpreter behave the same as the previous 'substitution' interpreter but highlights the problem of scope with a special test case.
This is eluded to in the next part '6.4 Scope' where the author writes:
"The broken environment interpreter above implements what is known as dynamic scope."
I'm sure this will be addressed in future chapters, thanks for looking anyway.
I'm working with CLIPS and I'm getting in trouble when trying to add instances in an empty list. I'm using the insert$ function but does not seem to work properly. The concrete code is this:
(loop-for-count (?i 1 (length$ ?listaConvocatoriasAlumno))
(if (neq (nth$ ?i ?listaConvocatoriasAlumno) ?convocatoria)
then
(if (eq (str-compare (send (instance-address * (nth$ ?i ?listaConvocatoriasAlumno)) get-cuadrimestre) ?cuadrimestre) 0)
then
(insert$ ?listaConvocatoriasMismoCuadrimestre (+ (length$ ?listaConvocatoriasMismoCuadrimestre) 1) (nth$ ?i ?listaConvocatoriasAlumno))
)
)
)
?listaConvocatoriasAlumno contains multiples instances of a certain class and I would like to add some of these into another list (?listaConvocatoriasMismoCuadrimestre in this case) which at the beginning of the loop is empty.
Any idea?
Thank you very much in advance!
Just forgot to store the insert return value into the list variable... I'm getting old
I'd like to export or replicate a scheme environment in another guile process. The algorithm I'm imagining would do something like this to serialize:
(map (lambda (var val) (display (quasiquote (define ,var ,val))
(newline))
(get-current-environment))
And then I'd read/eval that on the other end.
However, while there are functions that return the current environment, they are in some internal format that I can't just map across. How can I "walk" the environment as the above? Alternatively, how else can I replicate an environment into another process?
you may decompose the so-called "current-environment" like this:
(define (get-current-binding-list)
(let* ((e (current-module)) ;; assume checking current-module
(h (struct-ref e 0)) ;; index 0 is current vars hashtable
)
(hash-map->list cons h) ;; return a vars binding list
))
and you can call (get-current-binding-list) to get variables binding list in current-module.
Please note that each element in this list is a pair of symbol and variable type, say, (symbol-name . variable-type). So you may print it like this:
for a instance ,you got a var binding:
(define abc 5)
then:
(let ((vl (get-current-binding-list)))
(assoc-ref vl 'abc)
)
==> #<variable 9bb5108 value: 5>
This result is a "variable type" of variable "abc". You can get it's value with variable-ref procedure.
So you can trace all the bindings and do something ,in your code ,it's simply print var-name and var-value.
I know my answer is too brief, but I think there's enough information to help you to find more details in the manual.
Hope this will help you.
You can't really serialize Scheme environment. I don't known even it's possible to (portably) serialize continuations. Oh, and don't forget about FFIs. Ports and threads are unserializable too.