unofficial mirror of help-guix@gnu.org 
 help / color / mirror / Atom feed
* how to fix this G-exp?
@ 2022-09-23 17:26 zimoun
  2022-09-24 18:40 ` Csepp
  0 siblings, 1 reply; 5+ messages in thread
From: zimoun @ 2022-09-23 17:26 UTC (permalink / raw)
  To: help-guix

Hi,

I am missing something and I do not find my way, so maybe someone could
explain me what I am doing wrong.

Let start with this example.

--8<---------------cut here---------------start------------->8---
(use-modules (srfi srfi-1)
             (ice-9 match)
             (guix packages)
             (gnu packages))

(define (package+propagated-inputs package)
  (match (package-transitive-propagated-inputs package)
    (((labels packages) ...)
     (cons package packages))))


(define (something)
  42)


(define (an-example)

  (define build
    (with-extensions (package+propagated-inputs
                      (specification->package "guile-json"))
      (with-imported-modules '((guix build utils)
                               (json))
        #~(begin
            (use-modules (guix build utils)
                         (json))

            (define that
              #+(something))

            (define file-name
              (string-append #$output "/example.json"))

            (mkdir-p (dirname file-name))

            (with-output-to-file file-name
              (lambda ()
                (scm->json that)))

            ))))

  (computed-file "example-json"
                 build))

(manifest
 (list (manifest-entry
         (name "bang")
         (version "0")
         (item (an-example)))))
--8<---------------cut here---------------end--------------->8---


Then

    guix build -m /tmp/example.scm

produces something

    cat /gnu/store/zsqg38qkwhjbhfl4hkhsb4288rbnxxic-example-json/example.json
    42


Let remove the scm->json part and focus on what I do not understand:
’something’.

For instance, if instead something returns a list,

--8<---------------cut here---------------start------------->8---
--- /tmp/example.scm	2022-09-23 19:11:34.769456052 +0200
+++ /tmp/example-fail.scm	2022-09-23 19:16:30.018780090 +0200
@@ -10,7 +10,7 @@
 
 
 (define (something)
-  42)
+  (list 42))
 
 
 (define (an-example)
@@ -32,9 +32,6 @@
 
             (mkdir-p (dirname file-name))
 
-            (with-output-to-file file-name
-              (lambda ()
-                (scm->json that)))
 
             ))))
 --8<---------------cut here---------------end--------------->8---

then I get this error,

--8<---------------cut here---------------start------------->8---
Backtrace:
           3 (primitive-load "/gnu/store/vd87i0gf8f48kvcwm2c4796cmsn?")
In ice-9/eval.scm:
    619:8  2 (_ #f)
   626:19  1 (_ #<directory (guile-user) 7fffeffcfc80>)
Exception thrown while printing backtrace:
In procedure frame-local-ref: Argument 2 out of range: 1

ice-9/eval.scm:626:19: Wrong type to apply: 42
--8<---------------cut here---------------end--------------->8---

hum, maybe it is because a slice or something.  Let replace #+ by #+@
abd the backtrace is fixed.  Then, let add an item to the list,

--8<---------------cut here---------------start------------->8---
 (define (something)
-  42)
+  (list 42 43))


             (define that
-              #+(something))
+              #+@(something))
--8<---------------cut here---------------end--------------->8---

then bang

--8<---------------cut here---------------start------------->8---
ice-9/psyntax.scm:2794:12: In procedure syntax-violation:
Syntax error:
/gnu/store/zwizrn4xacyn2ynpnb5i2wgqvjj15rwa-example-json-builder:1:47: source expression failed to match any pattern in form (define that 42 43)
--8<---------------cut here---------------end--------------->8---


I would like that my procedure ’something’ returns a list, so I would
like that ’that’ would also be a list.  Because I would like to feed
scm->json with a list.

I have tried various things (list of list, slice or not, etc.) and I am
missing a point for understanding.  Could someone explain me?


Thanks in advance.


Cheers,
simon


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: how to fix this G-exp?
  2022-09-23 17:26 how to fix this G-exp? zimoun
@ 2022-09-24 18:40 ` Csepp
  2022-09-25 11:18   ` zimoun
  0 siblings, 1 reply; 5+ messages in thread
From: Csepp @ 2022-09-24 18:40 UTC (permalink / raw)
  To: zimoun; +Cc: help-guix


zimoun <zimon.toutoune@gmail.com> writes:

> Hi,
>
> I am missing something and I do not find my way, so maybe someone could
> explain me what I am doing wrong.

I'll try. UwU

> Let start with this example.
>
> (use-modules (srfi srfi-1)
>              (ice-9 match)
>              (guix packages)
>              (gnu packages))
>
> (define (package+propagated-inputs package)
>   (match (package-transitive-propagated-inputs package)
>     (((labels packages) ...)
>      (cons package packages))))
>
>
> (define (something)
>   42)
>
So this will take one of these values with the later edits you talk
about:
42
'(42)
'(42 42)
And this is used...
>
> (define (an-example)
>
>   (define build
>     (with-extensions (package+propagated-inputs
>                       (specification->package "guile-json"))
>       (with-imported-modules '((guix build utils)
>                                (json))
>         #~(begin
>             (use-modules (guix build utils)
>                          (json))
>
>             (define that
>               #+(something))
...here!  The later use of "that" is not important.
>...
When you spliced (not sliced) it, you got:
```
(define that (42))
```
As you can see, that is an unqouted list.  The fix should be pretty
simple: just add an extra quote.  Haven't tested it, but one of these
should work:
```
(define (something) ''(42))
;; or
(quote #+(something))
```

I'm pretty sure I've run into this in a service definition and used the
latter.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: how to fix this G-exp?
  2022-09-24 18:40 ` Csepp
@ 2022-09-25 11:18   ` zimoun
  2022-09-26  8:58     ` Ludovic Courtès
  0 siblings, 1 reply; 5+ messages in thread
From: zimoun @ 2022-09-25 11:18 UTC (permalink / raw)
  To: Csepp; +Cc: help-guix

Hi,

Thanks for your attempt to explain.

On Sat, 24 Sep 2022 at 20:40, Csepp <raingloom@riseup.net> wrote:

> As you can see, that is an unqouted list.  The fix should be pretty
> simple: just add an extra quote.  Haven't tested it, but one of these
> should work:
> ```
> (define (something) ''(42))
> ;; or
> (quote #+(something))
> ```

The extra quote does not work.  Quoting #+something does not work
either.

I guess my example is a wrong use of G-Exp and I would like to
understand why.


Cheers,
simon


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: how to fix this G-exp?
  2022-09-25 11:18   ` zimoun
@ 2022-09-26  8:58     ` Ludovic Courtès
  2022-11-04 16:09       ` zimoun
  0 siblings, 1 reply; 5+ messages in thread
From: Ludovic Courtès @ 2022-09-26  8:58 UTC (permalink / raw)
  To: zimoun; +Cc: Csepp, help-guix

[-- Attachment #1: Type: text/plain, Size: 1693 bytes --]

Hi,

zimoun <zimon.toutoune@gmail.com> skribis:

> On Sat, 24 Sep 2022 at 20:40, Csepp <raingloom@riseup.net> wrote:
>
>> As you can see, that is an unqouted list.  The fix should be pretty
>> simple: just add an extra quote.  Haven't tested it, but one of these
>> should work:
>> ```
>> (define (something) ''(42))
>> ;; or
>> (quote #+(something))
>> ```
>
> The extra quote does not work.

I think Csepp was on the right track though.

Attached is a version where I added that quote:

  #~(begin
      …
      (define that
        '#+(something))
      …)

AFAICS, that was the only fix that needed to be made.  (I also removed
(json) from the imported modules; it’s already taken care of by
‘with-extensions’.)

Pro tip: to debug code staging issues like this, where you end up
staging code that doesn’t behave as you would expect, it’s useful to
check what the staged code looks like, like so:

--8<---------------cut here---------------start------------->8---
$ cat $(guix gc -R $(guix build -m /tmp/zimoun.scm -d)|grep example-json-build)
(begin (use-modules (guix build utils) (json)) (define that (quote (42 43 44))) (define file-name (string-append ((@ (guile) getenv) "out") "/example.json")) (mkdir-p (dirname file-name)) (with-output-to-file file-name (lambda () (scm->json (list->vector that)))))
--8<---------------cut here---------------end--------------->8---

That ‘example-json-builder’ file contains the staged version of your
code.  We can see '(42 43 44) there; if we remove the quote mentioned
above, we get:

  (define that (42 43 44))

which leads to a wrong-type-to-apply error.

HTH!

Ludo’.


[-- Attachment #2: the file --]
[-- Type: text/plain, Size: 1235 bytes --]

(use-modules (srfi srfi-1)
             (ice-9 match)
             (guix packages)
             (gnu packages))

(define (package+propagated-inputs package)
  (match (package-transitive-propagated-inputs package)
    (((labels packages) ...)
     (cons package packages))))

(define (something)
  (list 42 43 44))

(define (an-example)
  (define build
    (with-extensions (package+propagated-inputs
                      (specification->package "guile-json"))
      (with-imported-modules '((guix build utils))
        #~(begin
            (use-modules (guix build utils)
                         (json))

            (define that
              ;; 'something' returns a list of integers; we need to quote
              ;; that list: it's data, not code, that we are inserting here.
              '#+(something))

            (define file-name
              (string-append #$output "/example.json"))

            (mkdir-p (dirname file-name))
            (with-output-to-file file-name
              (lambda ()
                (scm->json (list->vector that))))))))

  (computed-file "example-json"
                 build))

(manifest
 (list (manifest-entry
         (name "bang")
         (version "0")
         (item (an-example)))))

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: how to fix this G-exp?
  2022-09-26  8:58     ` Ludovic Courtès
@ 2022-11-04 16:09       ` zimoun
  0 siblings, 0 replies; 5+ messages in thread
From: zimoun @ 2022-11-04 16:09 UTC (permalink / raw)
  To: Ludovic Courtès; +Cc: Csepp, help-guix

Hi,

On Mon, 26 Sep 2022 at 10:58, Ludovic Courtès <ludo@gnu.org> wrote:

> I think Csepp was on the right track though.

Thank you both for explaining.  I hope to resume soon. :-)


Cheers,
simon


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-11-04 16:39 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-23 17:26 how to fix this G-exp? zimoun
2022-09-24 18:40 ` Csepp
2022-09-25 11:18   ` zimoun
2022-09-26  8:58     ` Ludovic Courtès
2022-11-04 16:09       ` zimoun

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).