Guile + SWIG
Hi, I am trying to bring my swigged Gtk2 + guile 1.8.8 + guile-gtk application up to date and I'd like some help please. My SWIG usage wraps many function, including enhanced_coot_ligand_p(). I am confused between the difference between functions that are available from C/C++ and those from a scheme script: if my inner_main looks like this: void inner_main(void *closure, int argc, char **argv) { SWIG_init(); std::string handler_string = "(lambda (key . args) "; handler_string += "(display (list \"Error in proc:\" key \" args: \" args)) (newline))"; SCM handler = scm_c_eval_string(handler_string.c_str()); std::string thunk_str = "(lambda() (display (list 44 (enhanced-ligand-coot-p))) (newline))\n"; SCM thunk = scm_c_eval_string(thunk_str.c_str()); scm_catch(SCM_BOOL_T, thunk, handler); gtk_main(); } then I get on the terminal what I expected: (44 #f) (i.e. enhanced-ligand-coot-p is evaluated) if my inner_main looks like this: void inner_main(void *closure, int argc, char **argv) { SWIG_init(); std::string handler_string = "(lambda (key . args) "; handler_string += "(display (list \"Error in proc:\" key \" args: \" args)) (newline))"; SCM handler = scm_c_eval_string(handler_string.c_str()); std::string thunk_str = "(use-modules (test-embedding))\n"; different /// SCM thunk = scm_c_eval_string(thunk_str.c_str()); scm_catch(SCM_BOOL_T, thunk, handler); gtk_main(); } then I get: ;; compiling /home/paule/autobuild/Linux-pen-pre-release-gtk3/share/guile/site/test-embedding.scm ;;; test-embedding.scm:21:30: warning: possibly unbound variable `enhanced-ligand-coot-p' ;;; compiled /home/paule/.cache/guile/ccache/2.2-LE-8-3.A/home/paule/autobuild/Linux-pen-pre-release- gtk3/share/guile/site/test-embedding.scm.go --- test embedding! --- Backtrace: 18 (apply-smob/1 #) In ice-9/boot-9.scm: 2312:4 17 (save-module-excursion _) In ice-9/eval-string.scm: 38:6 16 (read-and-eval # #:lang _) In ice-9/eval.scm: 721:20 15 (primitive-eval _) In ice-9/psyntax.scm: 1262:36 14 (expand-top-sequence _ _ _ #f _ _ _) 1209:24 13 (parse _ (("placeholder" placeholder)) ((top) #(# # …)) …) 285:10 12 (parse _ (("placeholder" placeholder)) (()) _ c&e (eval) …) In ice-9/boot-9.scm: 3377:20 11 (process-use-modules _) 222:17 10 (map1 (((test-embedding 3378:31 9 (_ ((test-embedding))) 2800:17 8 (resolve-interface (test-embedding) #:select _ #:hide _ …) In ice-9/threads.scm: 390:8 7 (_ _) In ice-9/boot-9.scm: 2726:13 6 (_) In ice-9/threads.scm: 390:8 5 (_ _) In ice-9/boot-9.scm: 2994:20 4 (_) 2312:4 3 (save-module-excursion _) 3014:26 2 (_) In unknown file: 1 (primitive-load-path "test-embedding" #) In test-embedding.scm: 19:0 0 (_) test-embedding.scm:4:0: In procedure module-lookup: Unbound variable: enhanced-ligand-coot-p test-embedding.scm looks like this and is installed in $prfx/share/guile/site (display "- test embedding! \n") (display (list 55 (enhanced-ligand-coot-p))) (newline) So, inner_main() knows that enhanced-ligand-coot-p is available, but the scheme script does not. I don't know how to resolve this. How do I get a list of the available functions? Thanks in advance for your help. Paul.
Syntax-Case macro that selects the N-th element from a list
Hi, In dryads-wake I need selection of the element in a list in a macro from user-input. Currently I have multiple macros, and the correct one (which strips the non-selected choices) is selected in a simple cond: (define-syntax-rule (Choose resp . choices) "Ask questions, apply consequences" (cond ((equal? resp 1) ;; resp is user-input. It is a natural number. (Respond1 choices)) ((equal? resp 2) (Respond2 choices)) ((equal? resp 3) (Respond3 choices)) (else #f))) For this however I have three syntax-case macros: (define-syntax Respond1 (lambda (x) (syntax-case x () ((_ ((question consequences ...) choices ...)) #`(begin (respond consequences ...))) ((_ (choices ...)) #`(begin #f) (define-syntax Respond2 (lambda (x) (syntax-case x () ((_ (choice choices ...)) #`(begin (Respond1 (choices ... ((_ (choices ...)) #`(begin #f) (define-syntax Respond3 (lambda (x) (syntax-case x () ((_ (a b choices ...)) #`(Respond1 (choices ...))) ((_ (choices ...)) #`(begin #f) I would like to get rid of those three definitions and replace them by at most two (one that strips N initial list entries, and Respond1). I cannot move to procedures, because I have code that must be executed only during final processing, and when I evaluate any of the consequences (as it happens with procedure-arguments), then the timing of the code execution does not match anymore. So I must absolutely do this in macros. I’ve tried to get that working, but all my tries failed. Is there a way and can you show it to me? This is a minimal working example. The output should stay the same, except for part 4, which needs this change to work (see at the bottom), but I would like to: - replace Respond2 and Respond3 by something recursive, so resp can have arbitrary high values (not infinite: max the length of the options) and - replace the cond-clause by a call to the recursive macro. (define-syntax-rule (respond consequence consequence2 ...) (begin (write consequence) (when (not (null? '(consequence2 ...))) (write (car (cdr (car `(consequence2 ... (define-syntax Respond1 (lambda (x) (syntax-case x () ((_ ((question consequences ...) choices ...)) #`(begin (respond consequences ...))) ((_ (choices ...)) #`(begin #f) (define-syntax Respond2 (lambda (x) (syntax-case x () ((_ (choice choices ...)) #`(begin (Respond1 (choices ... ((_ (choices ...)) #`(begin #f) (define-syntax Respond3 (lambda (x) (syntax-case x () ((_ (a b choices ...)) #`(Respond1 (choices ...))) ((_ (choices ...)) #`(begin #f) (define-syntax-rule (Choose resp . choices) "Ask questions, apply consequences" (cond ((equal? resp 1) (Respond1 choices)) ((equal? resp 2) (Respond2 choices)) ((equal? resp 3) (Respond3 choices)) (else #f))) (display "Choose 1: should be bar:") (Choose 1 (foo 'bar) (foo 'war 'har) (foo 'mar) (foo 'tar)) (newline) (display "Choose 2: should be warhar:") (Choose 2 (foo 'bar) (foo 'war 'har) (foo 'mar) (foo 'tar)) (newline) (display "Choose 3: should be mar:") (Choose 3 (foo 'bar) (foo 'war 'har) (foo 'mar) (foo 'tar)) (newline) (display "Choose 4: should be tar:") (Choose 4 (foo 'bar) (foo 'war 'har) (foo 'mar) (foo 'tar)) (newline) (display "Choose 5: should be #f:") (Choose 5 (foo 'bar) (foo 'war 'har) (foo 'mar) (foo 'tar)) (newline) Best wishes, Arne -- Unpolitisch sein heißt politisch sein ohne es zu merken signature.asc Description: PGP signature
Re: Syntax-Case macro that selects the N-th element from a list
Can you use the procedural part of syntax-rules? You have the power of using scheme at expansion time, which means you could do list-ref all you want. The only thing is that guile lacks syntax->list, so sometimes you have to manually turn it into a list. Say you are matching ((_ stuff ...) Body) stuff is a syntax object. You could turn it into a list of syntax objects by doing #'(stuff ...). Then you can treat it as a regular list, and use quasisyntax to put it back into your output syntax. Writing this on my phone. Sorry for the brevity (and lack of code). -- Linus Björnstam On Mon, 5 Apr 2021, at 13:30, Dr. Arne Babenhauserheide wrote: > Hi, > > In dryads-wake I need selection of the element in a list in a macro from > user-input. Currently I have multiple macros, and the correct one (which > strips the non-selected choices) is selected in a simple cond: > > (define-syntax-rule (Choose resp . choices) >"Ask questions, apply consequences" >(cond > ((equal? resp 1) ;; resp is user-input. It is a natural number. > (Respond1 choices)) > ((equal? resp 2) > (Respond2 choices)) > ((equal? resp 3) > (Respond3 choices)) > (else > #f))) > > For this however I have three syntax-case macros: > > (define-syntax Respond1 > (lambda (x) > (syntax-case x () > ((_ ((question consequences ...) choices ...)) > #`(begin >(respond consequences ...))) > ((_ (choices ...)) > #`(begin #f) > > (define-syntax Respond2 > (lambda (x) > (syntax-case x () > ((_ (choice choices ...)) > #`(begin >(Respond1 (choices ... > ((_ (choices ...)) > #`(begin #f) > > (define-syntax Respond3 > (lambda (x) > (syntax-case x () > ((_ (a b choices ...)) > #`(Respond1 (choices ...))) > ((_ (choices ...)) > #`(begin #f) > > > I would like to get rid of those three definitions and replace them by > at most two (one that strips N initial list entries, and Respond1). > > I cannot move to procedures, because I have code that must be executed > only during final processing, and when I evaluate any of the > consequences (as it happens with procedure-arguments), then the timing > of the code execution does not match anymore. So I must absolutely do > this in macros. > > > I’ve tried to get that working, but all my tries failed. Is there a way > and can you show it to me? > > This is a minimal working example. The output should stay the same, > except for part 4, which needs this change to work (see at the bottom), > but I would like to: > > - replace Respond2 and Respond3 by something recursive, so resp can have > arbitrary high values (not infinite: max the length of the options) and > - replace the cond-clause by a call to the recursive macro. > > (define-syntax-rule (respond consequence consequence2 ...) > (begin > (write consequence) > (when (not (null? '(consequence2 ...))) > (write (car (cdr (car `(consequence2 ... > > (define-syntax Respond1 > (lambda (x) > (syntax-case x () > ((_ ((question consequences ...) choices ...)) > #`(begin >(respond consequences ...))) > ((_ (choices ...)) > #`(begin #f) > > (define-syntax Respond2 > (lambda (x) > (syntax-case x () > ((_ (choice choices ...)) > #`(begin >(Respond1 (choices ... > ((_ (choices ...)) > #`(begin #f) > > (define-syntax Respond3 > (lambda (x) > (syntax-case x () > ((_ (a b choices ...)) > #`(Respond1 (choices ...))) > ((_ (choices ...)) > #`(begin #f) > > > (define-syntax-rule (Choose resp . choices) >"Ask questions, apply consequences" >(cond > ((equal? resp 1) > (Respond1 choices)) > ((equal? resp 2) > (Respond2 choices)) > ((equal? resp 3) > (Respond3 choices)) > (else > #f))) > > > (display "Choose 1: should be bar:") > (Choose 1 (foo 'bar) (foo 'war 'har) (foo 'mar) (foo 'tar)) > (newline) > (display "Choose 2: should be warhar:") > (Choose 2 (foo 'bar) (foo 'war 'har) (foo 'mar) (foo 'tar)) > (newline) > (display "Choose 3: should be mar:") > (Choose 3 (foo 'bar) (foo 'war 'har) (foo 'mar) (foo 'tar)) > (newline) > (display "Choose 4: should be tar:") > (Choose 4 (foo 'bar) (foo 'war 'har) (foo 'mar) (foo 'tar)) > (newline) > (display "Choose 5: should be #f:") > (Choose 5 (foo 'bar) (foo 'war 'har) (foo 'mar) (foo 'tar)) > (newline) > > > Best wishes, > Arne > -- > Unpolitisch sein > heißt politisch sein > ohne es zu merken > > Attachments: > * signature.asc
Re: Syntax-Case macro that selects the N-th element from a list
That "syntax-rules" is of course syntax-case. Try writing it first with unhygienic macros and get that working before porting to syntax-case if you don't know the ins-and-outs of syntax-case. -- Linus Björnstam On Mon, 5 Apr 2021, at 14:21, Linus Björnstam wrote: > Can you use the procedural part of syntax-rules? You have the power of > using scheme at expansion time, which means you could do list-ref all > you want. > > The only thing is that guile lacks syntax->list, so sometimes you have > to manually turn it into a list. Say you are matching ((_ stuff ...) > Body) stuff is a syntax object. You could turn it into a list of syntax > objects by doing #'(stuff ...). Then you can treat it as a regular > list, and use quasisyntax to put it back into your output syntax. > > Writing this on my phone. Sorry for the brevity (and lack of code). > > -- > Linus Björnstam > > On Mon, 5 Apr 2021, at 13:30, Dr. Arne Babenhauserheide wrote: > > Hi, > > > > In dryads-wake I need selection of the element in a list in a macro from > > user-input. Currently I have multiple macros, and the correct one (which > > strips the non-selected choices) is selected in a simple cond: > > > > (define-syntax-rule (Choose resp . choices) > >"Ask questions, apply consequences" > >(cond > > ((equal? resp 1) ;; resp is user-input. It is a natural number. > > (Respond1 choices)) > > ((equal? resp 2) > > (Respond2 choices)) > > ((equal? resp 3) > > (Respond3 choices)) > > (else > > #f))) > > > > For this however I have three syntax-case macros: > > > > (define-syntax Respond1 > > (lambda (x) > > (syntax-case x () > > ((_ ((question consequences ...) choices ...)) > > #`(begin > >(respond consequences ...))) > > ((_ (choices ...)) > > #`(begin #f) > > > > (define-syntax Respond2 > > (lambda (x) > > (syntax-case x () > > ((_ (choice choices ...)) > > #`(begin > >(Respond1 (choices ... > > ((_ (choices ...)) > > #`(begin #f) > > > > (define-syntax Respond3 > > (lambda (x) > > (syntax-case x () > > ((_ (a b choices ...)) > > #`(Respond1 (choices ...))) > > ((_ (choices ...)) > > #`(begin #f) > > > > > > I would like to get rid of those three definitions and replace them by > > at most two (one that strips N initial list entries, and Respond1). > > > > I cannot move to procedures, because I have code that must be executed > > only during final processing, and when I evaluate any of the > > consequences (as it happens with procedure-arguments), then the timing > > of the code execution does not match anymore. So I must absolutely do > > this in macros. > > > > > > I’ve tried to get that working, but all my tries failed. Is there a way > > and can you show it to me? > > > > This is a minimal working example. The output should stay the same, > > except for part 4, which needs this change to work (see at the bottom), > > but I would like to: > > > > - replace Respond2 and Respond3 by something recursive, so resp can have > > arbitrary high values (not infinite: max the length of the options) and > > - replace the cond-clause by a call to the recursive macro. > > > > (define-syntax-rule (respond consequence consequence2 ...) > > (begin > > (write consequence) > > (when (not (null? '(consequence2 ...))) > > (write (car (cdr (car `(consequence2 ... > > > > (define-syntax Respond1 > > (lambda (x) > > (syntax-case x () > > ((_ ((question consequences ...) choices ...)) > > #`(begin > >(respond consequences ...))) > > ((_ (choices ...)) > > #`(begin #f) > > > > (define-syntax Respond2 > > (lambda (x) > > (syntax-case x () > > ((_ (choice choices ...)) > > #`(begin > >(Respond1 (choices ... > > ((_ (choices ...)) > > #`(begin #f) > > > > (define-syntax Respond3 > > (lambda (x) > > (syntax-case x () > > ((_ (a b choices ...)) > > #`(Respond1 (choices ...))) > > ((_ (choices ...)) > > #`(begin #f) > > > > > > (define-syntax-rule (Choose resp . choices) > >"Ask questions, apply consequences" > >(cond > > ((equal? resp 1) > > (Respond1 choices)) > > ((equal? resp 2) > > (Respond2 choices)) > > ((equal? resp 3) > > (Respond3 choices)) > > (else > > #f))) > > > > > > (display "Choose 1: should be bar:") > > (Choose 1 (foo 'bar) (foo 'war 'har) (foo 'mar) (foo 'tar)) > > (newline) > > (display "Choose 2: should be warhar:") > > (Choose 2 (foo 'bar) (foo 'war 'har) (foo 'mar) (foo 'tar)) > > (newline) > > (display "Choose 3: should be mar:") > > (Choose 3 (foo 'bar) (foo 'war 'har) (foo 'mar) (foo 'tar)) > > (newline) > > (display "Choose 4: should be tar:") > > (Choose 4 (foo 'bar) (foo 'war 'har) (foo 'mar) (foo 'tar)) > > (newlin
Re: Syntax-Case macro that selects the N-th element from a list
On Monday, 5 April 2021 13:30:21 CEST you wrote: > Hi, > > In dryads-wake I need selection of the element in a list in a macro from > user-input. Currently I have multiple macros, and the correct one (which > strips the non-selected choices) is selected in a simple cond: > > (define-syntax-rule (Choose resp . choices) >"Ask questions, apply consequences" >(cond > ((equal? resp 1) ;; resp is user-input. It is a natural number. > (Respond1 choices)) > ((equal? resp 2) > (Respond2 choices)) > ((equal? resp 3) > (Respond3 choices)) > (else > #f))) > > For this however I have three syntax-case macros: > > (define-syntax Respond1 > (lambda (x) > (syntax-case x () > ((_ ((question consequences ...) choices ...)) > #`(begin >(respond consequences ...))) > ((_ (choices ...)) > #`(begin #f) > > (define-syntax Respond2 > (lambda (x) > (syntax-case x () > ((_ (choice choices ...)) > #`(begin >(Respond1 (choices ... > ((_ (choices ...)) > #`(begin #f) > > (define-syntax Respond3 > (lambda (x) > (syntax-case x () > ((_ (a b choices ...)) > #`(Respond1 (choices ...))) > ((_ (choices ...)) > #`(begin #f) > > > I would like to get rid of those three definitions and replace them by > at most two (one that strips N initial list entries, and Respond1). > > I cannot move to procedures, because I have code that must be executed > only during final processing, and when I evaluate any of the > consequences (as it happens with procedure-arguments), then the timing > of the code execution does not match anymore. So I must absolutely do > this in macros. > > > I’ve tried to get that working, but all my tries failed. Is there a way > and can you show it to me? > > This is a minimal working example. The output should stay the same, > except for part 4, which needs this change to work (see at the bottom), > but I would like to: > > - replace Respond2 and Respond3 by something recursive, so resp can have > arbitrary high values (not infinite: max the length of the options) and > - replace the cond-clause by a call to the recursive macro. > > (define-syntax-rule (respond consequence consequence2 ...) > (begin > (write consequence) > (when (not (null? '(consequence2 ...))) > (write (car (cdr (car `(consequence2 ... > > (define-syntax Respond1 > (lambda (x) > (syntax-case x () > ((_ ((question consequences ...) choices ...)) > #`(begin >(respond consequences ...))) > ((_ (choices ...)) > #`(begin #f) > > (define-syntax Respond2 > (lambda (x) > (syntax-case x () > ((_ (choice choices ...)) > #`(begin >(Respond1 (choices ... > ((_ (choices ...)) > #`(begin #f) > > (define-syntax Respond3 > (lambda (x) > (syntax-case x () > ((_ (a b choices ...)) > #`(Respond1 (choices ...))) > ((_ (choices ...)) > #`(begin #f) > > > (define-syntax-rule (Choose resp . choices) >"Ask questions, apply consequences" >(cond > ((equal? resp 1) > (Respond1 choices)) > ((equal? resp 2) > (Respond2 choices)) > ((equal? resp 3) > (Respond3 choices)) > (else > #f))) > > > (display "Choose 1: should be bar:") > (Choose 1 (foo 'bar) (foo 'war 'har) (foo 'mar) (foo 'tar)) > (newline) > (display "Choose 2: should be warhar:") > (Choose 2 (foo 'bar) (foo 'war 'har) (foo 'mar) (foo 'tar)) > (newline) > (display "Choose 3: should be mar:") > (Choose 3 (foo 'bar) (foo 'war 'har) (foo 'mar) (foo 'tar)) > (newline) > (display "Choose 4: should be tar:") > (Choose 4 (foo 'bar) (foo 'war 'har) (foo 'mar) (foo 'tar)) > (newline) > (display "Choose 5: should be #f:") > (Choose 5 (foo 'bar) (foo 'war 'har) (foo 'mar) (foo 'tar)) > (newline) > > > Best wishes, > Arne Hello, Dr. Arne, would simply transforming the question . response pairs to a list of lists of responses work? E.G. #+begin_src scheme (define-syntax questions->responses (syntax-rules () ((_ (question response ...) choices ...) (cons (list response ...) (questions->responses choices ...))) ((_ choices ...) '( #+end_src Vale, -Tim
Re: Syntax-Case macro that selects the N-th element from a list
On 05.04.2021 13:30, Dr. Arne Babenhauserheide wrote: > Hi, > > In dryads-wake I need selection of the element in a list in a macro from > user-input. Currently I have multiple macros, and the correct one (which > strips the non-selected choices) is selected in a simple cond: > > (define-syntax-rule (Choose resp . choices) >"Ask questions, apply consequences" >(cond > ((equal? resp 1) ;; resp is user-input. It is a natural number. > (Respond1 choices)) > ((equal? resp 2) > (Respond2 choices)) > ((equal? resp 3) > (Respond3 choices)) > (else > #f))) > > For this however I have three syntax-case macros: > > (define-syntax Respond1 > (lambda (x) > (syntax-case x () > ((_ ((question consequences ...) choices ...)) > #`(begin >(respond consequences ...))) > ((_ (choices ...)) > #`(begin #f) > > (define-syntax Respond2 > (lambda (x) > (syntax-case x () > ((_ (choice choices ...)) > #`(begin >(Respond1 (choices ... > ((_ (choices ...)) > #`(begin #f) > > (define-syntax Respond3 > (lambda (x) > (syntax-case x () > ((_ (a b choices ...)) > #`(Respond1 (choices ...))) > ((_ (choices ...)) > #`(begin #f) > > > I would like to get rid of those three definitions and replace them by > at most two (one that strips N initial list entries, and Respond1). > > I cannot move to procedures, because I have code that must be executed > only during final processing, and when I evaluate any of the > consequences (as it happens with procedure-arguments), then the timing > of the code execution does not match anymore. So I must absolutely do > this in macros. > > > I’ve tried to get that working, but all my tries failed. Is there a way > and can you show it to me? > > This is a minimal working example. The output should stay the same, > except for part 4, which needs this change to work (see at the bottom), > but I would like to: > > - replace Respond2 and Respond3 by something recursive, so resp can have > arbitrary high values (not infinite: max the length of the options) and > - replace the cond-clause by a call to the recursive macro. > > (define-syntax-rule (respond consequence consequence2 ...) > (begin > (write consequence) > (when (not (null? '(consequence2 ...))) > (write (car (cdr (car `(consequence2 ... > > (define-syntax Respond1 > (lambda (x) > (syntax-case x () > ((_ ((question consequences ...) choices ...)) > #`(begin >(respond consequences ...))) > ((_ (choices ...)) > #`(begin #f) > > (define-syntax Respond2 > (lambda (x) > (syntax-case x () > ((_ (choice choices ...)) > #`(begin >(Respond1 (choices ... > ((_ (choices ...)) > #`(begin #f) > > (define-syntax Respond3 > (lambda (x) > (syntax-case x () > ((_ (a b choices ...)) > #`(Respond1 (choices ...))) > ((_ (choices ...)) > #`(begin #f) > > > (define-syntax-rule (Choose resp . choices) >"Ask questions, apply consequences" >(cond > ((equal? resp 1) > (Respond1 choices)) > ((equal? resp 2) > (Respond2 choices)) > ((equal? resp 3) > (Respond3 choices)) > (else > #f))) > > > (display "Choose 1: should be bar:") > (Choose 1 (foo 'bar) (foo 'war 'har) (foo 'mar) (foo 'tar)) > (newline) > (display "Choose 2: should be warhar:") > (Choose 2 (foo 'bar) (foo 'war 'har) (foo 'mar) (foo 'tar)) > (newline) > (display "Choose 3: should be mar:") > (Choose 3 (foo 'bar) (foo 'war 'har) (foo 'mar) (foo 'tar)) > (newline) > (display "Choose 4: should be tar:") > (Choose 4 (foo 'bar) (foo 'war 'har) (foo 'mar) (foo 'tar)) > (newline) > (display "Choose 5: should be #f:") > (Choose 5 (foo 'bar) (foo 'war 'har) (foo 'mar) (foo 'tar)) > (newline) > > > Best wishes, > Arne > Is there a reason you want to separate 'choose' from the various 'respondN' macros? That seems superfluous to me. And if the number of choices is limited, you don't even need to use procedural macros. The following works: ;; Note: I like using the notation for template variables. ;; Feels highly intuitive to me. (define-syntax choose (syntax-rules () ((_ 1 ...) (respond )) ((_ 2...) (respond )) ((_ 3 ...) (respond )) ((_ 4 ...) (respond )) ((_ 5 ...) (respond )) ((_ ...) #f))) You could also leave out the final clause and thus let it be an implicit syntax error, or explicitly invoke 'syntax-error' with an explanation. But if you want to support an arbitrary numbers of choices, the following procedural macro will do it: (define-syntax choose (lambda (stx) (syntax-case stx () ((_ . ) (let* ((n (syntax->datum #')) (choice-list (syntax->datum #')) (choice-datum (list-ref choic
Re: Guile + SWIG
Hi Paul, Conventionally, a guile module would look something like (define-module (test-embedding)) (use-modules (srfi srfi-1)) ; .. etc (define (enahanced-ligand-coot-p) #t) ; etc. (export enahanced-ligand-coot-p) In your example, you never explained how "enahanced-ligand-coot-p" magically showed up in your environment. When a module is defined, it starts with a new environment. The module cannot access stuff you've defined previously; you would have to import it somehow. --linas On Mon, 2021-04-05 at 11:24 +0100, Paul Emsley wrote: > Hi, > > I am trying to bring my swigged Gtk2 + guile 1.8.8 + guile-gtk > application up to date and I'd like some help please. > > My SWIG usage wraps many function, including > enhanced_coot_ligand_p(). I am confused between the difference > between > functions that are available from C/C++ and those from a scheme > script: > > if my inner_main looks like this: > > void inner_main(void *closure, int argc, char **argv) { > >SWIG_init(); >std::string handler_string = "(lambda (key . args) "; >handler_string += "(display (list \"Error in proc:\" key \" args: > \" args)) (newline))"; >SCM handler = scm_c_eval_string(handler_string.c_str()); >std::string thunk_str = "(lambda() (display (list > 44 (enhanced-ligand-coot-p))) (newline))\n"; >SCM thunk = scm_c_eval_string(thunk_str.c_str()); >scm_catch(SCM_BOOL_T, thunk, handler); >gtk_main(); > > } > > then I get on the terminal what I expected: > > (44 #f) > > (i.e. enhanced-ligand-coot-p is evaluated) > > > if my inner_main looks like this: > void inner_main(void *closure, int argc, char **argv) { > >SWIG_init(); >std::string handler_string = "(lambda (key . args) "; >handler_string += "(display (list \"Error in proc:\" key \" args: > \" args)) (newline))"; >SCM handler = scm_c_eval_string(handler_string.c_str()); >std::string thunk_str = "(use-modules (test- > embedding))\n"; different /// >SCM thunk = scm_c_eval_string(thunk_str.c_str()); >scm_catch(SCM_BOOL_T, thunk, handler); >gtk_main(); > > } > > then I get: > > ;; compiling /home/paule/autobuild/Linux-pen-pre-release- > gtk3/share/guile/site/test-embedding.scm > ;;; test-embedding.scm:21:30: warning: possibly unbound variable > `enhanced-ligand-coot-p' > ;;; compiled /home/paule/.cache/guile/ccache/2.2-LE-8- > 3.A/home/paule/autobuild/Linux-pen-pre-release- > gtk3/share/guile/site/test-embedding.scm.go > --- test embedding! --- > Backtrace: > 18 (apply-smob/1 #) > In ice-9/boot-9.scm: >2312:4 17 (save-module-excursion _) > In ice-9/eval-string.scm: > 38:6 16 (read-and-eval # #:lang _) > In ice-9/eval.scm: >721:20 15 (primitive-eval _) > In ice-9/psyntax.scm: > 1262:36 14 (expand-top-sequence _ _ _ #f _ _ _) > 1209:24 13 (parse _ (("placeholder" placeholder)) ((top) #(# # …)) > …) >285:10 12 (parse _ (("placeholder" placeholder)) (()) _ c&e (eval) > …) > In ice-9/boot-9.scm: > 3377:20 11 (process-use-modules _) >222:17 10 (map1 (((test-embedding > 3378:31 9 (_ ((test-embedding))) > 2800:17 8 (resolve-interface (test-embedding) #:select _ #:hide _ > …) > In ice-9/threads.scm: > 390:8 7 (_ _) > In ice-9/boot-9.scm: > 2726:13 6 (_) > In ice-9/threads.scm: > 390:8 5 (_ _) > In ice-9/boot-9.scm: > 2994:20 4 (_) >2312:4 3 (save-module-excursion _) > 3014:26 2 (_) > In unknown file: >1 (primitive-load-path "test-embedding" # 7f98d…>) > In test-embedding.scm: > 19:0 0 (_) > > test-embedding.scm:4:0: In procedure module-lookup: Unbound variable: > enhanced-ligand-coot-p > > > > test-embedding.scm looks like this and is installed in > $prfx/share/guile/site > > (display "- test embedding! \n") > (display (list 55 (enhanced-ligand-coot-p))) > (newline) > > > So, inner_main() knows that enhanced-ligand-coot-p is available, but > the scheme script does not. I don't know how to > resolve this. > > How do I get a list of the available functions? > > Thanks in advance for your help. > > Paul. > > >
Guile + SWIG
Hi, I am bringing my swigged Gtk2 + guile 1.8.8 + guile-gtk application up to date and I'd like some help please. My SWIG usage wraps many function, including enhanced_coot_ligand_p(). I am confused between the difference between functions that are available from C/C++ and those from a scheme script: if my inner_main looks like this: void inner_main(void *closure, int argc, char **argv) { SWIG_init(); std::string handler_string = "(lambda (key . args) "; handler_string += "(display (list \"Error in proc:\" key \" args: \" args)) (newline))"; SCM handler = scm_c_eval_string(handler_string.c_str()); std::string thunk_str = "(lambda() (display (list 44 (enhanced-ligand-coot-p))) (newline))\n"; SCM thunk = scm_c_eval_string(thunk_str.c_str()); scm_catch(SCM_BOOL_T, thunk, handler); gtk_main(); } then I get on the terminal what I expected: (44 #f) (i.e. enhanced-ligand-coot-p is evaluated) if my inner_main looks like this: void inner_main(void *closure, int argc, char **argv) { SWIG_init(); std::string handler_string = "(lambda (key . args) "; handler_string += "(display (list \"Error in proc:\" key \" args: \" args)) (newline))"; SCM handler = scm_c_eval_string(handler_string.c_str()); std::string thunk_str = "(use-modules (test-embedding))\n"; different /// SCM thunk = scm_c_eval_string(thunk_str.c_str()); scm_catch(SCM_BOOL_T, thunk, handler); gtk_main(); } then I get: ;; compiling /home/paule/autobuild/Linux-pen-pre-release-gtk3/share/guile/site/test-embedding.scm ;;; test-embedding.scm:21:30: warning: possibly unbound variable `enhanced-ligand-coot-p' ;;; compiled /home/paule/.cache/guile/ccache/2.2-LE-8-3.A/home/paule/autobuild/Linux-pen-pre-release- gtk3/share/guile/site/test-embedding.scm.go --- test embedding! --- Backtrace: 18 (apply-smob/1 #) In ice-9/boot-9.scm: 2312:4 17 (save-module-excursion _) In ice-9/eval-string.scm: 38:6 16 (read-and-eval # #:lang _) In ice-9/eval.scm: 721:20 15 (primitive-eval _) In ice-9/psyntax.scm: 1262:36 14 (expand-top-sequence _ _ _ #f _ _ _) 1209:24 13 (parse _ (("placeholder" placeholder)) ((top) #(# # …)) …) 285:10 12 (parse _ (("placeholder" placeholder)) (()) _ c&e (eval) …) In ice-9/boot-9.scm: 3377:20 11 (process-use-modules _) 222:17 10 (map1 (((test-embedding 3378:31 9 (_ ((test-embedding))) 2800:17 8 (resolve-interface (test-embedding) #:select _ #:hide _ …) In ice-9/threads.scm: 390:8 7 (_ _) In ice-9/boot-9.scm: 2726:13 6 (_) In ice-9/threads.scm: 390:8 5 (_ _) In ice-9/boot-9.scm: 2994:20 4 (_) 2312:4 3 (save-module-excursion _) 3014:26 2 (_) In unknown file: 1 (primitive-load-path "test-embedding" #) In test-embedding.scm: 19:0 0 (_) test-embedding.scm:4:0: In procedure module-lookup: Unbound variable: enhanced-ligand-coot-p test-embedding.scm looks like this and is installed in $prfx/share/guile/site (display "--- test embedding! ---\n") (display (list 55 (enhanced-ligand-coot-p))) (newline) So, inner_main() knows that enhanced-ligand-coot-p is available, but the scheme script does not. I don't know how to resolve this. How do I get a list of the available functions? Thanks in advance for your help. Paul.
Re: Syntax-Case macro that selects the N-th element from a list
Hi Linus, thank you for your tipps! Linus Björnstam writes: > Can you use the procedural part of syntax-rules? You have the power of > using scheme at expansion time, which means you could do list-ref all > you want. > > That "syntax-rules" is of course syntax-case. > > The only thing is that guile lacks syntax->list, so sometimes you have > to manually turn it into a list. Say you are matching ((_ stuff ...) > Body) stuff is a syntax object. You could turn it into a list of syntax > objects by doing #'(stuff ...). Then you can treat it as a regular > list, and use quasisyntax to put it back into your output syntax. … > Try writing it first with unhygienic macros and get that working > before porting to syntax-case if you don't know the ins-and-outs of > syntax-case. I have not yet written an unhygienic macro in Guile. Do I use the internal macros for that? Best wishes, Arne -- Unpolitisch sein heißt politisch sein ohne es zu merken signature.asc Description: PGP signature
Re: Guile + SWIG
Hi Linas, Thanks for your reply. Let me clarify if I can. I have many C++ functions that I would like to use from scheme. I am using SWIG to generate wrappers for these functions, and these get added at the "root module" http://www.swig.org/Doc3.0/Guile.html The default linkage is the simplest; nothing special is done. In this case the function SWIG_init() is exported. Simple linkage can be used in several ways: Embedded Guile, no modules. You want to embed a Guile interpreter into your program; all bindings made by SWIG shall show up in the root module. Then call SWIG_init() in the inner_main() function So that is what I was trying to do. My understanding was that enhanced-ligand-coot-p and its friends would be added so that their usage would be no different to list, string?, map and any number of basic scheme functions. However something is amiss. If I try this: ./coot Enter `,help' for help. scheme@(guile-user)> (load "test-embedding.scm") (55 #f) Now, if I install that in $prefix/share/guile/site If I try this: ./coot Enter `,help' for help. scheme@(guile-user)> (use-modules (test-embedding)) While compiling expression: In procedure module-lookup: Unbound variable: enhanced-ligand-coot-p scheme@(guile-user)> Is that because the "compiler" doesn't know where enhanced-ligand-coot-p is? Maybe the compiler is not my application, so that's why it's not there? If that is the case, then can I not compile my .scm files? How to do that? Or can I somehow say "don't worry that it's not there now, when the application runs, it will be there"? Thanks, Paul. p.s. It's quite likely that Coot was used in the analysis of spike proteins from coronavirus that lead to 2P and hexapro stabilization mutants, and they have been taken up by the pharmaceutical companies and embedded into the mRNA or more conventional preps. So you can have some guile-1.8.8-based technology squirted into your arm next week. On Mon, 2021-04-05 at 10:27 -0500, Linas Vepstas wrote: > Hi Paul, > > Conventionally, a guile module would look something like > > > (define-module (test-embedding)) > (use-modules (srfi srfi-1)) ; .. etc > > (define (enhanced-ligand-coot-p) #t) ; etc. > > (export enhanced-ligand-coot-p) > > > In your example, you never explained how "enhanced-ligand-coot-p" > magically showed up in your environment. > > When a module is defined, it starts with a new environment. The module > cannot access stuff you've defined previously; you would have to import > it somehow. > > --linas > > > On Mon, 2021-04-05 at 11:24 +0100, Paul Emsley wrote: > > Hi, > > > > I am trying to bring my swigged Gtk2 + guile 1.8.8 + guile-gtk > > application up to date and I'd like some help please. > > > > My SWIG usage wraps many function, including > > enhanced_coot_ligand_p(). I am confused between the difference > > between > > functions that are available from C/C++ and those from a scheme > > script: > > > > if my inner_main looks like this: > > > > void inner_main(void *closure, int argc, char **argv) { > > > >SWIG_init(); > >std::string handler_string = "(lambda (key . args) "; > >handler_string += "(display (list \"Error in proc:\" key \" args: > > \" args)) (newline))"; > >SCM handler = scm_c_eval_string(handler_string.c_str()); > >std::string thunk_str = "(lambda() (display (list > > 44 (enhanced-ligand-coot-p))) (newline))\n"; > >SCM thunk = scm_c_eval_string(thunk_str.c_str()); > >scm_catch(SCM_BOOL_T, thunk, handler); > >gtk_main(); > > > > } > > > > then I get on the terminal what I expected: > > > > (44 #f) > > > > (i.e. enhanced-ligand-coot-p is evaluated) > > > > > > if my inner_main looks like this: > > void inner_main(void *closure, int argc, char **argv) { > > > >SWIG_init(); > >std::string handler_string = "(lambda (key . args) "; > >handler_string += "(display (list \"Error in proc:\" key \" args: > > \" args)) (newline))"; > >SCM handler = scm_c_eval_string(handler_string.c_str()); > >std::string thunk_str = "(use-modules (test- > > embedding))\n"; different /// > >SCM thunk = scm_c_eval_string(thunk_str.c_str()); > >scm_catch(SCM_BOOL_T, thunk, handler); > >gtk_main(); > > > > } > > > > then I get: > > > > ;; compiling /home/paule/autobuild/Linux-pen-pre-release- > > gtk3/share/guile/site/test-embedding.scm > > ;;; test-embedding.scm:21:30: warning: possibly unbound variable > > `enhanced-ligand-coot-p' > > ;;; compiled /home/paule/.cache/guile/ccache/2.2-LE-8- > > 3.A/home/paule/autobuild/Linux-pen-pre-release- > > gtk3/share/guile/site/test-embedding.scm.go > > --- test embedding! --- > > Backtrace: > > 18 (apply-smob/1 #) > > In ice-9/boot-9.scm: > >2312:4 17 (save-module-excursion _) > > In ice-9/eval-string.scm: > > 38:6 16 (read-and-eval # #:lang _) > > In ice-9/
Re: Guile + SWIG
When scripts with load-extension are compiled, the compiler does not know what symbols are defined in the extension so you may get warnings. Are you getting an error or just the warning? On 4/5/21 10:44 AM, Paul Emsley wrote: Hi Linas, Thanks for your reply. Let me clarify if I can. I have many C++ functions that I would like to use from scheme. I am using SWIG to generate wrappers for these functions, and these get added at the "root module" http://www.swig.org/Doc3.0/Guile.html The default linkage is the simplest; nothing special is done. In this case the function SWIG_init() is exported. Simple linkage can be used in several ways: Embedded Guile, no modules. You want to embed a Guile interpreter into your program; all bindings made by SWIG shall show up in the root module. Then call SWIG_init() in the inner_main() function So that is what I was trying to do. My understanding was that enhanced-ligand-coot-p and its friends would be added so that their usage would be no different to list, string?, map and any number of basic scheme functions. However something is amiss. If I try this: ./coot Enter `,help' for help. scheme@(guile-user)> (load "test-embedding.scm") (55 #f) Now, if I install that in $prefix/share/guile/site If I try this: ./coot Enter `,help' for help. scheme@(guile-user)> (use-modules (test-embedding)) While compiling expression: In procedure module-lookup: Unbound variable: enhanced-ligand-coot-p scheme@(guile-user)> Is that because the "compiler" doesn't know where enhanced-ligand-coot-p is? Maybe the compiler is not my application, so that's why it's not there? If that is the case, then can I not compile my .scm files? How to do that? Or can I somehow say "don't worry that it's not there now, when the application runs, it will be there"? Thanks, Paul. p.s. It's quite likely that Coot was used in the analysis of spike proteins from coronavirus that lead to 2P and hexapro stabilization mutants, and they have been taken up by the pharmaceutical companies and embedded into the mRNA or more conventional preps. So you can have some guile-1.8.8-based technology squirted into your arm next week. On Mon, 2021-04-05 at 10:27 -0500, Linas Vepstas wrote: Hi Paul, Conventionally, a guile module would look something like (define-module (test-embedding)) (use-modules (srfi srfi-1)) ; .. etc (define (enhanced-ligand-coot-p) #t) ; etc. (export enhanced-ligand-coot-p) In your example, you never explained how "enhanced-ligand-coot-p" magically showed up in your environment. When a module is defined, it starts with a new environment. The module cannot access stuff you've defined previously; you would have to import it somehow. --linas On Mon, 2021-04-05 at 11:24 +0100, Paul Emsley wrote: Hi, I am trying to bring my swigged Gtk2 + guile 1.8.8 + guile-gtk application up to date and I'd like some help please. My SWIG usage wraps many function, including enhanced_coot_ligand_p(). I am confused between the difference between functions that are available from C/C++ and those from a scheme script: if my inner_main looks like this: void inner_main(void *closure, int argc, char **argv) { SWIG_init(); std::string handler_string = "(lambda (key . args) "; handler_string += "(display (list \"Error in proc:\" key \" args: \" args)) (newline))"; SCM handler = scm_c_eval_string(handler_string.c_str()); std::string thunk_str = "(lambda() (display (list 44 (enhanced-ligand-coot-p))) (newline))\n"; SCM thunk = scm_c_eval_string(thunk_str.c_str()); scm_catch(SCM_BOOL_T, thunk, handler); gtk_main(); } then I get on the terminal what I expected: (44 #f) (i.e. enhanced-ligand-coot-p is evaluated) if my inner_main looks like this: void inner_main(void *closure, int argc, char **argv) { SWIG_init(); std::string handler_string = "(lambda (key . args) "; handler_string += "(display (list \"Error in proc:\" key \" args: \" args)) (newline))"; SCM handler = scm_c_eval_string(handler_string.c_str()); std::string thunk_str = "(use-modules (test- embedding))\n"; different /// SCM thunk = scm_c_eval_string(thunk_str.c_str()); scm_catch(SCM_BOOL_T, thunk, handler); gtk_main(); } then I get: ;; compiling /home/paule/autobuild/Linux-pen-pre-release- gtk3/share/guile/site/test-embedding.scm ;;; test-embedding.scm:21:30: warning: possibly unbound variable `enhanced-ligand-coot-p' ;;; compiled /home/paule/.cache/guile/ccache/2.2-LE-8- 3.A/home/paule/autobuild/Linux-pen-pre-release- gtk3/share/guile/site/test-embedding.scm.go --- test embedding! --- Backtrace: 18 (apply-smob/1 #) In ice-9/boot-9.scm: 2312:4 17 (save-module-excursion _) In ice-9/eval-string.scm: 38:6 16 (read-and-eval # #:lang _) In ice-9/eval.scm: 721:20 15 (primitive-eval _) In ice-9/psyntax.s