unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Weird cache issue
@ 2022-03-15 17:54 Zelphir Kaltstahl
  2022-03-15 19:04 ` Maxime Devos
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Zelphir Kaltstahl @ 2022-03-15 17:54 UTC (permalink / raw)
  To: Guile User

Hello Guile users!

I have a weird cache issue:

I have a directory, where I have the following files: "fileio.scm" and 
"013.scm". The following shows the contents of the files:

~~~~fileio.scm~~~~
(library (fileio)
   (export get-string-from-file
           get-lines-from-file
           get-lines-from-port
           get-lines-from-file-using-splitting
           put-lines-to-file
           put-string-to-file)
   (import
    (except (rnrs base)
            let-values
            map)
    (only (guile)
          lambda* λ
          ;; file io
          call-with-input-file
          call-with-output-file
          set-port-encoding!
          eof-object?
          ;; strings
          string-split
          string-join)
    (ice-9 textual-ports)  ; for textual reading and writing procedures
    (ice-9 binary-ports)  ; not sure if needed
    (ice-9 rdelim)  ; for `eof-object?`
    (ice-9 optargs)  ; for keyword arguments
    (srfi srfi-1)))  ; not sure if needed


(define get-string-from-file
   (lambda* (file-path
             #:key
             (encoding "UTF-8"))
     (call-with-input-file file-path
       (λ (port)
         (set-port-encoding! port encoding)
         (get-string-all port)))))


(define get-lines-from-file
   (lambda* (file-path
             #:key
             (encoding "UTF-8"))
     ;; another common encoding is: "ISO-8859-1"
     ;; see http://www.iana.org/assignments/character-sets for more
     (define (get-lines-from-port port)
       (let ([line (get-line port)])
         (cond [(eof-object? line) '()]
               [else
                (cons line
                      (get-lines-from-port port))])))
     (call-with-input-file file-path
       (λ (port)
         (set-port-encoding! port encoding)
         (get-lines-from-port port)))))


(define get-lines-from-file-using-splitting
   (lambda* (file-path
             #:key
             (encoding "UTF-8"))
     (string-split (get-string-from-file file-path #:encoding encoding)
                   ;; You could use simple character here, but I am using
                   ;; lambda to show that it can be used as well.
                   (λ (char) (char=? char #\newline)))))


(define put-lines-to-file
   (lambda* (file-path
             lines
             #:key
             (encoding "UTF-8")
             (mode 'replace))
     (call-with-output-file file-path
       (λ (port)
         (set-port-encoding! port encoding)
         (put-string port
                     (cond
                      [(eq? mode 'append)
                       (string-append (get-string-from-file
                                       file-path
                                       #:encoding encoding)
                                      "\n"
                                      (string-join lines "\n"))]
                      [(equal? mode 'replace) (string-join lines "\n")]
                      [else (string-join lines "\n")]))))))


(define put-string-to-file
   (lambda* (file-path
             string
             #:key
             (encoding "UTF-8")
             (mode 'replace))
     (call-with-output-file file-path
       (λ (port)
         (set-port-encoding! port encoding)
         (put-string port
                     (cond
                      [(eq? mode 'append)
                       (string-append (get-string-from-file
                                       file-path
                                       #:encoding encoding)
                                      "\n"
                                      string)]
                      [(equal? mode 'replace) string]
                      [else string]))))))
~~~~

And:

~~~~013.scm~~~~
(library (fileio)
   (export )
   (import
    (except (rnrs base) let-values)
    (only (guile)
          lambda* λ
          ;; printing
          display
          simple-format)
    (fileio)))


(define numbers
   (map string->number
        (get-lines-from-file "013-input.txt")))


(let* ([sum (apply + numbers)]
        [digits
         (substring (number->string sum) 0 10)])
   (display
    (simple-format
     #f "first 10 digits: ~a\n"
     digits)))
~~~~

When I call: "guile -L . 013.scm" I get:

~~~~
$ guile -q -L . 013.scm
;;; note: source file /home/user/dev/guile/guile-project-euler-solutions/013.scm
;;;       newer than compiled /home/user/.cache/guile/ccache/3.0-LE-8-4.6/home/user/dev/guile/guile-project-euler-solutions/013.scm.go
;;; note: auto-compilation is enabled, set GUILE_AUTO_COMPILE=0
;;;       or pass the --no-auto-compile argument to disable.
;;; compiling /home/user/dev/guile/guile-project-euler-solutions/013.scm
;;; compiled /home/user/.cache/guile/ccache/3.0-LE-8-4.6/home/user/dev/guile/guile-project-euler-solutions/013.scm.go
first 10 digits: 5537376230

$ guile -q -L . 013.scm
Backtrace:
In ice-9/boot-9.scm:
   1752:10  6 (with-exception-handler _ _ #:unwind? _ #:unwind-for-type _)
In unknown file:
            5 (apply-smob/0 #<thunk 7f191b1a82e0>)
In ice-9/boot-9.scm:
     724:2  4 (call-with-prompt ("prompt") #<procedure 7f191b1ba920 at ice-9/eval.scm:330…> …)
In ice-9/eval.scm:
     619:8  3 (_ #(#(#<directory (guile-user) 7f191b1adc80>)))
In ice-9/boot-9.scm:
    2836:4  2 (save-module-excursion #<procedure 7f191b1a0210 at ice-9/boot-9.scm:4393:3 ()>)
   4388:12  1 (_)
In 013.scm:
     123:8  0 (_)

013.scm:123:8: Unbound variable: get-lines-from-file
~~~~

So the first time I run the program, it works. The second time it does not.

This is reproducible by clearing the cache and trying again:

~~~~
rm -rf /home/user/.cache/guile/
~~~~

I found out, that this behavior disappears, when I fix the (library ...) 
expression in "013.scm" as follows:

~~~~013.scm~~~~
(import
  (except (rnrs base) let-values map)
  (only (guile)
        lambda* λ
        ;; printing
        display
        simple-format)
  (fileio))

(define numbers
   (map string->number
        (get-lines-from-file "013-input.txt")))


(let* ([sum (apply + numbers)]
        [digits
         (substring (number->string sum) 0 10)])
   (display
    (simple-format
     #f "first 10 digits: ~a\n"
     digits)))
~~~~

So it does not stop me from making progress, but it is weird, that I don't get 
an error about the (library ...) expression and instead the program works once 
and then not a second time, unless I clear the cache.

Is there a logic to this, or is it somehow a result of a conscious design decision?

Regards,
Zelphir

-- 
repositories: https://notabug.org/ZelphirKaltstahl




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

end of thread, other threads:[~2022-03-15 22:45 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-15 17:54 Weird cache issue Zelphir Kaltstahl
2022-03-15 19:04 ` Maxime Devos
2022-03-15 22:45   ` Zelphir Kaltstahl
2022-03-15 19:06 ` Maxime Devos
2022-03-15 19:10 ` Maxime Devos
2022-03-15 22:44   ` Zelphir Kaltstahl

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).