* 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
* Re: Weird cache issue
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
2 siblings, 1 reply; 6+ messages in thread
From: Maxime Devos @ 2022-03-15 19:04 UTC (permalink / raw)
To: Zelphir Kaltstahl, Guile User
[-- Attachment #1: Type: text/plain, Size: 183 bytes --]
Zelphir Kaltstahl schreef op di 15-03-2022 om 17:54 [+0000]:
> ~~~~013.scm~~~~
> (library (fileio)
Shouldn't this be (library (#{013}#) ...) instead of
(library (fileio) ...)?
[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 260 bytes --]
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Weird cache issue
2022-03-15 17:54 Weird cache issue Zelphir Kaltstahl
2022-03-15 19:04 ` Maxime Devos
@ 2022-03-15 19:06 ` Maxime Devos
2022-03-15 19:10 ` Maxime Devos
2 siblings, 0 replies; 6+ messages in thread
From: Maxime Devos @ 2022-03-15 19:06 UTC (permalink / raw)
To: Zelphir Kaltstahl, Guile User
[-- Attachment #1: Type: text/plain, Size: 247 bytes --]
Zelphir Kaltstahl schreef op di 15-03-2022 om 17:54 [+0000]:
> I found out, that this behavior disappears, when I fix the (library ...)
> expression in "013.scm" as follows:
Nevermind, I now see this part of the mail.
Greetings,
Maxime
[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 260 bytes --]
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Weird cache issue
2022-03-15 17:54 Weird cache issue Zelphir Kaltstahl
2022-03-15 19:04 ` Maxime Devos
2022-03-15 19:06 ` Maxime Devos
@ 2022-03-15 19:10 ` Maxime Devos
2022-03-15 22:44 ` Zelphir Kaltstahl
2 siblings, 1 reply; 6+ messages in thread
From: Maxime Devos @ 2022-03-15 19:10 UTC (permalink / raw)
To: Zelphir Kaltstahl, Guile User
[-- Attachment #1: Type: text/plain, Size: 984 bytes --]
Zelphir Kaltstahl schreef op di 15-03-2022 om 17:54 [+0000]:
> ~~~~013.scm~~~~
> (library (fileio)
> (export )
> (import
> (except (rnrs base) let-values)
> (only (guile)
> lambda* λ
> ;; printing
> display
> simple-format)
> (fileio)))
Shouldn't the code below be put inside the library form?
While these imports might be available below as well due to
how 'library' is 'define-module' followed by some extra code
(see ice-9/r6rs-libraries.scm), it is not something you can
rely upon:
;; It would be nice to push the module that was current before the
;; definition, and pop it after the library definition, but I
;; actually can't see a way to do that. Helper procedures perhaps,
;; around a fluid that is rebound in save-module-excursion? Patches
;; welcome!
Greetings,
Maxime.
[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 260 bytes --]
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Weird cache issue
2022-03-15 19:10 ` Maxime Devos
@ 2022-03-15 22:44 ` Zelphir Kaltstahl
0 siblings, 0 replies; 6+ messages in thread
From: Zelphir Kaltstahl @ 2022-03-15 22:44 UTC (permalink / raw)
To: Maxime Devos, Guile User
Hi Maxime!
On 3/15/22 20:10, Maxime Devos wrote:
> Zelphir Kaltstahl schreef op di 15-03-2022 om 17:54 [+0000]:
>> ~~~~013.scm~~~~
>> (library (fileio)
>> (export )
>> (import
>> (except (rnrs base) let-values)
>> (only (guile)
>> lambda* λ
>> ;; printing
>> display
>> simple-format)
>> (fileio)))
> Shouldn't the code below be put inside the library form?
> While these imports might be available below as well due to
> how 'library' is 'define-module' followed by some extra code
> (see ice-9/r6rs-libraries.scm), it is not something you can
> rely upon:
>
> ;; It would be nice to push the module that was current before the
> ;; definition, and pop it after the library definition, but I
> ;; actually can't see a way to do that. Helper procedures perhaps,
> ;; around a fluid that is rebound in save-module-excursion? Patches
> ;; welcome!
>
> Greetings,
> Maxime.
I did not know that! I might be using (library ...) wrong all along then. Thanks
for the hint!
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).