<?xml version="1.0" encoding="utf-8" ?>
<rdf:RDF
       xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
       xmlns="http://purl.org/rss/1.0/"
       xmlns:dc="http://purl.org/dc/elements/1.1/"
       xmlns:content="http://purl.org/rss/1.0/modules/content/"
      >
<channel rdf:about="http://blog.practical-scheme.net/gauche"><title>Gauche Devlog</title>
<link>http://blog.practical-scheme.net/gauche</link>
<description>Blog on Gauche development</description>
<items><rdf:Seq><rdf:li rdf:resource="http://blog.practical-scheme.net/gauche/20260106-extension-registry" />
<rdf:li rdf:resource="http://blog.practical-scheme.net/gauche/20251211-alternative-array-complex-format" />
<rdf:li rdf:resource="http://blog.practical-scheme.net/gauche/20250413-exact-repeating-decimals" />
<rdf:li rdf:resource="http://blog.practical-scheme.net/gauche/20240628-setup-gauche" />
<rdf:li rdf:resource="http://blog.practical-scheme.net/gauche/20240117-formatter-procedure" />
<rdf:li rdf:resource="http://blog.practical-scheme.net/gauche/20230930-pipeworks" />
<rdf:li rdf:resource="http://blog.practical-scheme.net/gauche/20230929-real-numerical-functions" />
<rdf:li rdf:resource="http://blog.practical-scheme.net/gauche/20230928-pretty-print-indent" />
<rdf:li rdf:resource="http://blog.practical-scheme.net/gauche/20230926a-segmented-completion" />
<rdf:li rdf:resource="http://blog.practical-scheme.net/gauche/20230926-unbound-variable" />
<rdf:li rdf:resource="http://blog.practical-scheme.net/gauche/20230413-init-once" />
<rdf:li rdf:resource="http://blog.practical-scheme.net/gauche/20221005-macro-source-info" />
<rdf:li rdf:resource="http://blog.practical-scheme.net/gauche/20220617-setup-gauche" />
<rdf:li rdf:resource="http://blog.practical-scheme.net/gauche/20220402-running-gosh-docker" />
<rdf:li rdf:resource="http://blog.practical-scheme.net/gauche/20211024-type-punning" />
</rdf:Seq></items>

</channel>
<item rdf:about="http://blog.practical-scheme.net/gauche/20260106-extension-registry"><title>Extension package registry</title>
<link>http://blog.practical-scheme.net/gauche/20260106-extension-registry</link>
<content:encoded><![CDATA[<h2 id="H-10rxqnn">Extension package registry
</h2
><p>We just renewed the <a href="https://practical-scheme.net/gauche/">Gauche homepage</a
>.
It's mostly cosmetics, but one notable change is the
<a href="https://practical-scheme.net/gauche/packages.html">Extension Packages</a
>
page
</p
><p>It's been in our todo list for very long time to create
some system to track Gauche extension packages.
It is trivial to create a site where users can put
the info.  What's not trivial is how to keep the info updated.
</p
><p>It's a burden to the user if we ask them to keep updating
such info whenever they update their extension package.
</p
><p>If a user puts their website/email for the package,
but then moves away from Gauche development, and eventually
the site/email become inactive and go away, we don't know
what to do with the entry; it'd be also difficult if
somebody wants to take over the project.
</p
><p>Should anybody be able to update the package's info?  Limiting it to
the original authors becomes an issue if they go inactive and
out of touch.  Allowing it may cause a security issue if
someone replaces the distribution URL to malicious one.
</p
><p>To vet the users entering info, we need some mechanism of
user registration and authentication, which adds another
database to maintain.
</p
><p>These implications kept us from implementing the official
mechanism to provide the extension package registry.
</p
><hr /><p>Things has changed in the last decade or so.
</p
><p>First, distributed VCS and their hosting services have become
a norm.  Instead of having personal websites to serve
extension package tarballs and documents, developers
can put their repository on one of those services
and make it public.
</p
><p>Recent Gauche provides a standard framework of building extensions.
One important aspect of it is <code>package.scm</code
> in the
source tree to keep meta information about the package,
including version number, authors, &quot;official&quot; repository url, dependencies, etc.
</p
><p>So, once we know the package's repository URL, we can get its
meta information!
</p
><p>The author updates <code>package.scm</code
> as the development proceeds,
because it is a part of the source.  No need to update information
on the registry separately.
</p
><p>Anybody can create account on those services, but the service
gives certain identity to each one and the place to interact with
each other.  Sure, people move away eventually, but it's rarely that they
bother to remove the repositories; and it's easy to inherit
the abandoned project.
</p
><p>We already have a official way to state such transfer of control
in <code>package.scm</code
> (<code>superseded-by</code
> slot).  If the successor
can contact the original author/maitainer/committer, the <code>package.scm</code
>
in the original repository can have <code>superseded-by</code
> field
pointing to the new repository.   It is not mandatory, but it can
make it clear where is the &quot;official&quot; successor.
</p
><p>In other words, we can use the existing public repositories as the metadata
database, and merely maintain pointers to them by ourselves.
</p
><hr /><p>So, how to manage those pointers?   We don't have thousands of
extension packages updated daily, so we don't need a sophisticated
database server for it.
</p
><p>We decided to piggyback on the public DVCS service again.
<a href="https://github.com/practical-scheme/Gauche-package-repository-index">Gauche package repository index</a
> github repo maintains
the list of package urls under its <code>packages</code
> directory.
If you want your packages to be listed, just fork it,
add your package, and send a pull request.
(If you don't want to use GitHub, just send a patch
via email.)
</p
><p>Which repository is added when, by whose request, is recorded
in the commits of that repo.
</p
><p>Currenly, pulling metadata and reflecting it on the webpage
is done in occasional batch process.  We'll adjust the frequency
as it goes.  If we ever get very popular and receiving
tons of new package registration requests, we might need to upgrade
the system, but until then, this will be the least-maintenance-cost
solution.
</p
><hr /><p>To be in the registry, your extension package needs <code>package.scm</code
>.
I scanned through the existing list on wiki
(<a href="http://practical-scheme.net/wiliki/wiliki.cgi?Gauche%3APackages">WiLiKi:Gauche:Packages</a
>) and added packages that I could find
the public repository with <code>package.scm</code
>.
</p
><p>If your extension is old enough not to have <code>package.scm</code
>,
a convenient way is to run <code>gauche-package populate</code
> in
the top directory of the source tree.  It gives you a template
<code>package.scm</code
> with some fields filled by whatever information
it can find.
</p
><p><span class="tag-anchor">Tag: <a href="gauche?p=Tag:Extensions">Extensions</a
></span
>
</p
><p>
</p
>]]></content:encoded><dc:date>2026-01-13T10:24:59+00:00</dc:date>
</item>
<item rdf:about="http://blog.practical-scheme.net/gauche/20251211-alternative-array-complex-format"><title>Alternative external formats for arrays and complex numbers</title>
<link>http://blog.practical-scheme.net/gauche/20251211-alternative-array-complex-format</link>
<content:encoded><![CDATA[<h2 id="H-15w81v">Alternative external formats for arrays and complex numbers
</h2
><p>Gauche now recognizes a few different external format for arrays and complex numbers.  For writing, it can be selected by <code>&lt;write-controls&gt;</code
> object, or <code>print-mode</code
> in REPL.
</p
><h3 id="H-1wsx8ja">Array literals
</h3
><p>Gauche has been supporting <a href="http://srfi.schemers.org/srfi-25">srfi:25</a
> arrays, but it does not define external representations.
Gauche uses <a href="http://srfi.schemers.org/srfi-10">srfi:10</a
> <code>#,</code
> mechanism to allow to write
arrays that can be read back, but it is not very user-frendly.
</p
><pre>gosh$ (tabulate-array (shape 0 4 0 4) *)
#,(&lt;array&gt; (0 4 0 4) 0 0 0 0 0 1 2 3 0 2 4 6 0 3 6 9)
</pre
><p>Now we have this:
</p
><pre>gosh$ (tabulate-array (shape 0 4 0 4) *)
#2a((0 0 0 0)
    (0 1 2 3)
    (0 2 4 6)
    (0 3 6 9))
</pre
><p>The <code>#2a(...)</code
> notation is defined in <a href="http://srfi.schemers.org/srfi-163">srfi:163</a
> Enhanced Array Literals, and in its
simplest form, it is also compatible to Common Lisp's array literals.  From 0.9.16, it is
the default output format of the array.
</p
><p>You can also make Gauche reports the lengthes of each dimension:
</p
><pre>gosh$ ,pm array dimensions
Current print mode:
       length :  50         pretty :  #t     bytestring :  #f
        level :  10           base :  10          array : dimensions
        width :  80   radix-prefix :  #f        complex : rectangular
string-length : 256  exact-decimal :  #f
gosh$ (tabulate-array (shape 0 4 0 4) *)
#2a:4:4((0 0 0 0)
        (0 1 2 3)
        (0 2 4 6)
        (0 3 6 9))
</pre
><p>The reader recognizes all of those formats.
</p
><h3 id="H-190wwrb">Complex literals
</h3
><p>There was an asymmetry in input/output of complex literals. For reading, both the rectangular notation
<code>1.4142135623730951+1.4142135623730951i</code
> and the polar notation <code>2@0.7853981633974483</code
> are recognized, but
for printing, it is alyways in the rectangular notation.
Now you can choose the output format.
</p
><p>Gauche also extended the polar notation by adding suffix <code>pi</code
>, e.g. <code>2@0.25pi</code
> to specify
the phase by the multiple of pi.
</p
><p>The following session shows how a complex number is printed with different print-mode:
</p
><pre>gosh&gt; (expt -16 1/4)
1.4142135623730951+1.4142135623730951i
gosh&gt; ,pm polar
Current print mode:
       length :  50           base :  10  exact-decimal :  #f
        level :  10   radix-prefix :  #f          array : compact
       pretty :  #t  string-length : 256        complex : rectangular
        width :  79     bytestring :  #f
gosh&gt; (expt -16 1/4)
2.0@0.7853981633974483
gosh&gt; ,pm complex polar-pi
Current print mode:
       length :  50           base :  10  exact-decimal :  #f
        level :  10   radix-prefix :  #f          array : compact
       pretty :  #t  string-length : 256        complex : polar-pi
        width :  79     bytestring :  #f
gosh&gt; (expt -16 1/4)
2.0@0.25pi
</pre
><p>Furthermore, Gauche also supports Common-Lisp style
complex notation, <code>#c(...)</code
>.  This is particulary
useful to exchange data between Gauche and CL programs.
</p
><pre>gosh&gt; ,pm complex vector
Current print mode:
       length :  50           base :  10  exact-decimal :  #f
        level :  10   radix-prefix :  #f          array : compact
       pretty :  #t  string-length : 256        complex : vector
        width :  79     bytestring :  #f
gosh&gt; (expt -16 1/4)
#c(1.4142135623730951 1.4142135623730951)
</pre
><p>The reader can read all the complex formats.
</p
><p><span class="tag-anchor">Tags: <a href="gauche?p=Tag:0.9.16">0.9.16</a
>, <a href="gauche?p=Tag:REPL">REPL</a
>, <a href="gauche?p=Tag:printer">printer</a
>, <a href="gauche?p=Tag:array">array</a
>, <a href="gauche?p=Tag:complex">complex</a
></span
>
</p
><p>
</p
>]]></content:encoded><dc:date>2025-12-11T03:55:09+00:00</dc:date>
</item>
<item rdf:about="http://blog.practical-scheme.net/gauche/20250413-exact-repeating-decimals"><title>Exact and repeating decimals</title>
<link>http://blog.practical-scheme.net/gauche/20250413-exact-repeating-decimals</link>
<content:encoded><![CDATA[<h2 id="H-16ycqj8">Exact and repeating decimals
</h2
><p>Novice programmers are often perplexed by most programming languages
being not able to add 0.1 ten times ``correctly'':
</p
><pre>s = 0
for i in range(10):
   s += 0.1
print(s)

# prints: 0.9999999999999999
</pre
><p>&quot;Floating point numbers are inexact, that's why,&quot; tells a tutor.
&quot;You should expect some errors.&quot;
</p
><p>Gauche isn't an exception, for decimal notation is read as <em>inexact</em
>
numbers:
</p
><pre>gosh&gt; (apply + (make-list 10 0.1))
0.9999999999999999
</pre
><p>However, Scheme also has <em>exact</em
> numbers.  Numbers without a decimal
point or exponent, or rational numbers, are read as <em>exact</em
> numbers.
You can also prefix decimal numbers with <code>#e</code
> to make them exact.
Using exact numbers, you can have an exact result.
</p
><pre>gosh&gt; (apply + (make-list 10 #e0.1))
1
</pre
><p>The trick is that Gauche reads <code>#e0.1</code
> as an exact rational number
<code>1/10</code
>, and perform computation as exact rationals.
It is revealed when the result is not a whole number:
</p
><pre>gosh&gt; (+ #e0.1 #e0.1)
1/5
</pre
><p>It is incovenient, though, when you want to perform exact computation
with decimal numbers, i.e. adding prices with dollars and cents.
If you add $15.15 and $8.91, you want to see the result as
<code>24.06</code
> instead of <code>1203/50</code
>.
</p
><pre>;; Inexact
gosh&gt; (+ 15.15 8.91)
24.060000000000002

;; Exact
gosh&gt; (+ #e15.15 #e8.91)
1203/50
</pre
><p>So, we added a new REPL print mode, <em>exact-decimal</em
>.  If you set it
to <code>#t</code
>, Gauche tries to print exact non-integer result as
decimal notation whenever possible.
</p
><pre>gosh&gt; ,pm exact-decimal #t
Current print mode:
        length :  50
         level :  10
        pretty :  #t
         width :  79
          base :  10
         radix :  #f
 string-length : 256
    bytestring :  #f
 exact-decimal :  #t
</pre
><p>Let's see:
</p
><pre>gosh&gt; (+ #e15.15 #e8.91)
#e24.06
</pre
><p>We can always have exact decimal notation
of rational numbers whose denominator's factor
contains only 2 and 5.
</p
><pre>gosh&gt; 1/65536
#e0.0000152587890625
</pre
><hr /><p>As far as we use addition, subtraction,
and multiplication of exact decimal notated numbers, the result
is always representable with exact decimal notation.
</p
><p>But what if division is involved?  Isn't it a shame that we have
an exact value (as a rational number), but can't print it as a
decimal exactly?
</p
><p>Decimal notation of rational numbers whose denominator contains
factors other than 2 and 5 becomes repeating decimals.  Hence
if we have a notation of repeating decimals, we can cover such
cases.
</p
><p>So, here it is.  If a numeric literal contains <code>#</code
> followed
by one or more digits, we understand the digits after <code>#</code
>
repeating infinitely.
</p
><pre>gosh&gt; 0.#3
0.3333333333333333
gosh&gt; 0.0#123
0.012312312312312312
gosh&gt; 0.#5
0.5555555555555556
gosh&gt; 0.1#9
0.2
</pre
><p>(Note: If no digits follows <code>#</code
>, it is &quot;insignificant digit&quot;
notation in R5RS.)
</p
><p>The above examples have limited number of digits because
they're inexact numbers (note that we didn't
prefix them with <code>#e</code
>).  For exact numbers, we can represent
any rational numbers exactly with this notation:
</p
><pre>gosh&gt; 1/3
#e0.#3
gosh&gt; 1/7
#e0.#142857
gosh&gt; (* 1/7 2)
#e0.#285714
gosh&gt; (* #e0.#3 #e0.#142857)
#e0.#047619
</pre
><p>Note that the length of repetition can be arbitrarily long, so
there are numbers that can't practically be printed in this notation.
For the time being, we have a hard limit of 1024 for the length of
repetition.  If the result exceeds this limitation, we fall
back to rational notation.
</p
><pre>;; 1/2063 has repeating cycle of 1031 digits
gosh&gt; (/ 1 2063)
1/2063
</pre
><p><span class="tag-anchor">Tags: <a href="gauche?p=Tag:Numbers">Numbers</a
>, <a href="gauche?p=Tag:Syntax">Syntax</a
></span
>
</p
><p>
</p
>]]></content:encoded><dc:date>2025-04-13T18:53:28+00:00</dc:date>
</item>
<item rdf:about="http://blog.practical-scheme.net/gauche/20240628-setup-gauche"><title>Running prebuilt Gauche on GitHub workflow</title>
<link>http://blog.practical-scheme.net/gauche/20240628-setup-gauche</link>
<content:encoded><![CDATA[<h2 id="H-1akac51">Running prebuilt Gauche on GitHub workflow
</h2
><p>The <code>setup-gauche</code
> action installs Gauche on GitHub workflow runners for you
(<a href="https://blog.practical-scheme.net/gauche/20220617-setup-gauche">Using Gauche in GitHub Actions</a
>).  But it downloaded source tarball and compiled, which took time.
Especially if your repo is a small library, it feels waste of time
compiling Gauche every time you push to the repo.
</p
><p>Now, <code>setup-gauche</code
> can use a prebuilt binary on <code>ubuntu-latest</code
> and
<code>macos-latest</code
> platforms.  Just give <code>prebuilt-binary: true</code
> as the parameter:
</p
><pre>name: Build and test

on: [push, pull_request]

jobs:
  build-and-test:
    runs-on: ubuntu-latest
    timeout-minutes: 10
    steps:
    - uses: actions/checkout@v3
    - uses: practical-scheme/setup-gauche@v5
      with:
        prebuilt-binary: true
    - name: Install dependencies
      run: |
        sudo apt install -y gettext
    - name: Build and check
      run: |
        ./configure
        make
        make -s check
</pre
><p>Installing prebuilt binary takes around 10s or so; huge time saving.
</p
><p>Note that the prebuilt binary is provided with the latest Gauche release
only.  Other parameters of <code>setup-gauche</code
> are ignored if you use
the prebuilt binary.
</p
><p>(You may have noticed that the repository name is now under
<code>practical-scheme</code
> instead
of <code>shirok</code
>--I made <code>practical-scheme</code
> organization and am gradually
moving Gauche repositories to there, for easier maintenance.  The URL
is redirected from <code>shirok</code
> so you don't need to update immediately,
but just FYI.)
</p
><hr /><p>The following is for those who are curious about behind-the-scene.
</p
><p>Prebuilt binaries are prepared in a different repository:
<a href="https://github.com/practical-scheme/setup-gauche-binary">https://github.com/practical-scheme/setup-gauche-binary</a
>
</p
><p>It has GitHub actions that fetches the latest release tarball,
build in GitHub runner, and upload the result as the assets of the
repo's release.  That ensures the binary runs on GitHub runners.
</p
><p><span class="tag-anchor">Tags: <a href="gauche?p=Tag:github">github</a
>, <a href="gauche?p=Tag:CI">CI</a
></span
>
</p
><p>
</p
>]]></content:encoded><dc:date>2024-06-29T02:08:33+00:00</dc:date>
</item>
<item rdf:about="http://blog.practical-scheme.net/gauche/20240117-formatter-procedure"><title>Caching formatter procedure</title>
<link>http://blog.practical-scheme.net/gauche/20240117-formatter-procedure</link>
<content:encoded><![CDATA[<h2 id="H-1aedv9j">Caching formatter procedure
</h2
><p>Lisp's <code>format</code
> procedure is very un-Schemy.  Instead of having a set of
composable, orthogonal, do-one-thing-well procedures, <code>format</code
> introduces
a mini-language that's syntactically and semantically separate from the base
language. It is not extendable, loaded with obscure features from the past.
Yet it is handy for typical trivial tasks and that's why Gauche (and other Schemes,
plus a couple fo SRFIs) offer it.
(And to be honest, there's some pleasure to tinker such mini-language implementations.)
</p
><p>Aside from the non-composability, another glaring drawback of <code>format</code
> is
that it needs to interpret the mini language (format string) at runtime.
Most <code>format</code
> calls have a literal format string, and it is waste of time
to parse it every time <code>format</code
> is called.  An obvious optimization
is to recognize the literal format string and translates the call to <code>format</code
>
by simpler procedures at compile-time.  I believe most CL implemenations do so.
</p
><p>However,
Gauche, as well as some other Scheme implementations and SRFI-48, allows the
<em>port</em
> argument to be omitted.  It is convenient,
but it indeed makes compile-time transformation difficult.  If the first
argument of <code>format</code
> is a non-literal expression (it is the case
if you're passing a port), it is diffuclt for the compiler to recognize
if the format string is a constant, even the second argument is a literal
string that looks like a format string.  If the first expression yields
a string at runtime, <em>that</em
> is the format string and the literal
string is an argument to be shown.
</p
><p>Despite these difficulties, we can still take advantage of
literal format string, by caching the format string compilation result
at run-time.
</p
><p>It is not exactly the same as memoization.  It is difficult to control
amount of memoized results, and we only want to cache literal format
strings, which needs to be determined at compile time.
</p
><p>So, we implemented a hybrid solution.  The compiler macro attached
to <code>format</code
> checks if possible format string is a literal, and if so,
it transforms the call into an internal procedure that takes an extra
argument.  The extra argument contains the position of the possible literal
format string, and a mutable box.  The following is the core part of
the compile-time transformation:
</p
><pre>(define-syntax make-format-transformer
  (er-macro-transformer
   (^[f r c]
     (match f
       [(_ shared?)
        (quasirename r
          `(er-macro-transformer
            (^[f r c]
              (define (context-literal pos) `(,',shared? ,pos ,(box #f)))
              (match f
                [(_ (? string?) . _)
                 (quasirename r
                   `(format-internal ',(context-literal 0) (list ,@(cdr f))))]
                [(_ _ (? string?) . _)
                 (quasirename r
                   `(format-internal ',(context-literal 1) (list ,@(cdr f))))]
                [(_ _ _ (? string?) . _)
                 (quasirename r
                   `(format-internal ',(context-literal 2) (list ,@(cdr f))))]
                [_ f]))))]))))
</pre
><p>(NB: <code>shared?</code
> flag is used to share the routine with <code>format</code
> and
<code>format/ss</code
>.  We need to check the literal string in first, second and third
position, for Gauche's <code>format</code
> allows two optional arguments before the
format string.)
</p
><p>At run-time, the internal function can see if the literal string is
indeed a format string.  If so, it computes a formatter procedure
based on the format string, and stores it to the mutable box.  Subsequent
calls will use the computed formatter procedure, skipping parsing and
compiling the format string.  The caching occurs per-call-site, much like
the global variable lookup (we cache the <code>&lt;gloc&gt;</code
> object, the result
of lookup, in the code vector).
</p
><p>The <code>format-internal</code
> procedure checks optional arguments, and calls
<code>format-2</code
>.  Its first argument can be a mutable box introduced
by the above macro, if we do know the format string is literal.
</p
><pre>(define (format-2 formatter-cache shared? out control fmtstr args)
  (let1 formatter (if formatter-cache
                    (or (unbox formatter-cache)
                        (rlet1 f (formatter-compile fmtstr)
                          (set-box! formatter-cache f)))
                    (formatter-compile fmtstr))
    (case out
      [(#t)
       (call-formatter shared? #t formatter (current-output-port) control args)]
      [(#f) (let1 out (open-output-string)
              (call-formatter shared? #f formatter out control args)
              (get-output-string out))]
      [else (call-formatter shared? #t formatter out control args)])))
</pre
><p>A micro benchmark shows it's effective.  In real code, the effect
may not be so prominent, but it does remove worries that you're wasting
time for parsing format string.
</p
><pre>(define (run p)
  (dotimes [n 1000000]
    (format p &quot;n=~7d 1/n=~8,6f\n&quot; n (/. n))))

(define (main _)
  (time (call-with-output-file &quot;/dev/null&quot; run))
  0)
</pre
><p>With caching off:
</p
><pre>;(time (call-with-output-file &quot;/dev/null&quot; run))
; real  19.796
; user  19.790
; sys    0.000
</pre
><p>With caching on:
</p
><pre>;(time (call-with-output-file &quot;/dev/null&quot; run))
; real  10.313
; user  10.310
; sys    0.000
</pre
><p><span class="tag-anchor">Tag: <a href="gauche?p=Tag:format">format</a
></span
>
</p
><p>
</p
>]]></content:encoded><dc:date>2024-01-18T02:37:07+00:00</dc:date>
</item>
<item rdf:about="http://blog.practical-scheme.net/gauche/20230930-pipeworks"><title>Pipeworks</title>
<link>http://blog.practical-scheme.net/gauche/20230930-pipeworks</link>
<content:encoded><![CDATA[<h2 id="H-aljkrj">Pipeworks
</h2
><p>Ports are very handy abstraction of data source and sink.  In Gauche libraries, you can find many utitlies that reads from input port or writes to output port, and then
another utilities (e.g. convert from/to string) are built on top of them.
</p
><p>While they are useful, it becomes tricky when you want to compose those
utilities.  Suppose you have a procedure <code>f</code
> that writes to an output port,
and a procedure <code>g</code
> that read from an input port.  You want to feed the
output of <code>f</code
> to <code>g</code
> while make
<code>f</code
> and <code>g</code
> run concurrently, so some threading is involved.  You can write
such a <em>pipe</em
> using procedural ports but it is cumbersome to do so for
every occasion.  I want something that's as easy as Unix pipe.
</p
><p>So I initially started to writing a pipe utility using procedural ports.  Then
I realised I also want a device dual to it; while a pipe flows data from an output
port to an input port, the co-pipe, or <em>pump</em
>, pulls data from an input port
and push it to an output port.   An example is that you run a subprocess
and feed its error output to your current output port.  When you invoke
a subprocess (<a href="http://practical-scheme.net/gauche/man/?p=gauche.process">ref:gauche.process</a
>), you can get its error output from
an input port. So you need to read it <em>actively</em
> and feed the data to
your current output port.
</p
><p>Then you might want to peek the error output to find out a specific
error message appears.  So your contraption reads actively an input port,
and feed the data to an output port, <em>and</em
> you can read whatever data
flows through it from another input port to monitor.
</p
><p>There are many variations, and mulling over it for some time, I wrote
a library that abstracts any of such configurations.  I call the device
<em>plumbing</em
> (<a href="http://practical-scheme.net/gauche/man/?v=draft&amp;p=control.plumbing">draft:control.plumbing</a
>).
</p
><p>You can also create an output port that feeds the data to multiple outputs,
or gather multiple input port into one input port.  Refer to the manual
to see what you can do.
</p
><p><span class="tag-anchor">Tags: <a href="gauche?p=Tag:0.9.13">0.9.13</a
>, <a href="gauche?p=Tag:control.plumbing">control.plumbing</a
></span
>
</p
>]]></content:encoded><dc:date>2023-09-30T08:24:26+00:00</dc:date>
</item>
<item rdf:about="http://blog.practical-scheme.net/gauche/20230929-real-numerical-functions"><title>Real numerical functions</title>
<link>http://blog.practical-scheme.net/gauche/20230929-real-numerical-functions</link>
<content:encoded><![CDATA[<h2 id="H-1igzncg">Real numerical functions
</h2
><p>Scheme devines a set of elementary functions that can handle complex numbers.
In Gauche, complex elementary functions is built on top
of real domain functions.  Up to 0.9.12, we had real-only version
with the name such as <code>%sin</code
> or <code>%exp</code
>.   As the percent prefix
suggests, they are not meant to be used directly; <code>sin</code
> ro <code>exp</code
>
are built on top of them.
</p
><p>However, sometimes you want to use real-only versions to avoid overhead
of type testing and dispatching complex numbers.  <a href="http://srfi.schemers.org/srfi-94">srfi:94</a
> defines
real-domain functions, so we decided to adapt them.  Now you have <code>real-sin</code
>,
<code>real-exp</code
> etc. (<a href="http://practical-scheme.net/gauche/man/?v=draft&amp;p=real-exp">draft:real-exp</a
>) as built-in.
</p
><p>Note that <code>scheme.flonum</code
> also provides &quot;flonum-only&quot;
version of elementary functions, e.g. <code>flsin</code
> (<a href="http://practical-scheme.net/gauche/man/?p=scheme.flonum">ref:scheme.flonum</a
>).
They won't even accept exact numbers.  Since it is in R7RS-large,
you may want to use them for portable code.
</p
><p>Although the names <code>%sin</code
> etc. are undocumented and not meant to be
directly used, they were visible by default, so some existing code
are relying on it.  It needs some effort to rewrite all occurrences
of such functions with the new <code>real-sin</code
> etc, so we provide
a compatibility module, <code>compat.real-elementary-functions</code
>.  Just
using it in your code provides compatibility names.  If you want
to make your code work on both 0.9.12 and 0.9.13, you can use
<code>cond-expand</code
>:
</p
><pre>(cond-expand
  ((library compat.real-elementary-functions)
   (use compat.real-elementary-functions))
  (else))
</pre
><p><span class="tag-anchor">Tags: <a href="gauche?p=Tag:0.9.13">0.9.13</a
>, <a href="gauche?p=Tag:NumericFunctions">NumericFunctions</a
></span
>
</p
>]]></content:encoded><dc:date>2023-09-29T00:31:52+00:00</dc:date>
</item>
<item rdf:about="http://blog.practical-scheme.net/gauche/20230928-pretty-print-indent"><title>Pretty print indentation</title>
<link>http://blog.practical-scheme.net/gauche/20230928-pretty-print-indent</link>
<content:encoded><![CDATA[<h2 id="H-111xvo7">Pretty print indentation
</h2
><p>Yet another small thing good to have.  You can now specify base indentation
to the pretty printer (<a href="http://practical-scheme.net/gauche/man/?p=pprint">ref:pprint</a
>).  It is applied to the second line
and after.
</p
><pre>gosh&gt; (pprint (make-list 100 'abc) :indent 20)
(abc abc abc abc abc abc abc abc abc abc abc abc abc abc
                     abc abc abc abc abc abc abc abc abc abc abc abc abc abc
                     abc abc abc abc abc abc abc abc abc abc abc abc abc abc
                     abc abc abc abc abc abc abc abc abc abc abc abc abc abc
                     abc abc abc abc abc abc abc abc abc abc abc abc abc abc
                     abc abc abc abc abc abc abc abc abc abc abc abc abc abc
                     abc abc abc abc abc abc abc abc abc abc abc abc abc abc
                     abc abc)
#&lt;undef&gt;
</pre
><p>To say more precisely, when the pretty printer spills data to another line,
it inserts &quot;a newline + whitespace * indent&quot;, plus any indent computed
by the pretty printer.
</p
><p>The benefit easiest to see is when the pretty printer is used inside
<code>format</code
>.   When a pretty printing triggered by the <code>~:w</code
> directive,
it sets the base indentation at the column it starts printing.  Hence
the entire pretty print is indented to align nicely:
</p
><pre>gosh&gt; (format #t &quot;Long list: ~:w\n&quot; (make-list 100 'abc))
Long list: (abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc
            abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc
            abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc
            abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc
            abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc
            abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc
            abc abc)
#&lt;undef&gt;
</pre
><p>Since pretty printing is built-in to the core printer (<code>pprint</code
> is just
a simple interface to use), other output routines such as <code>write</code
> can
also use base indentation.  You can set <code>indent</code
> slot of a write-controls.
</p
><pre>gosh&gt; (write (make-list 100 'abc) (make-write-controls :pretty #t :indent 20 :width 79))
(abc abc abc abc abc abc abc abc abc abc abc abc abc abc
                     abc abc abc abc abc abc abc abc abc abc abc abc abc abc
                     abc abc abc abc abc abc abc abc abc abc abc abc abc abc
                     abc abc abc abc abc abc abc abc abc abc abc abc abc abc
                     abc abc abc abc abc abc abc abc abc abc abc abc abc abc
                     abc abc abc abc abc abc abc abc abc abc abc abc abc abc
                     abc abc abc abc abc abc abc abc abc abc abc abc abc abc
                     abc abc)#&lt;undef&gt;
</pre
><p><span class="tag-anchor">Tags: <a href="gauche?p=Tag:0.9.13">0.9.13</a
>, <a href="gauche?p=Tag:pretty-printing">pretty-printing</a
></span
>
</p
>]]></content:encoded><dc:date>2023-09-28T21:54:35+00:00</dc:date>
</item>
<item rdf:about="http://blog.practical-scheme.net/gauche/20230926a-segmented-completion"><title>Segmented completion</title>
<link>http://blog.practical-scheme.net/gauche/20230926a-segmented-completion</link>
<content:encoded><![CDATA[<h2 id="H-md6ykt">Segmented completion
</h2
><p>Another little something on REPL.  It can now complete symbols like <code>call-with-current-continuation</code
> from <code>c-w-c-c</code
>.  This is an old tradition of
Lisp environment.
</p
><p><img src="https://blog.practical-scheme.net/images/segmented-completion.gif" alt="[image]" />
</p
><p>We added a new module <code>text.segmented-match</code
> (<a href="http://practical-scheme.net/gauche/man/?v=draft&amp;p=text.segmented-match">draft:text.segmented-match</a
>)
to support this.
</p
><p><span class="tag-anchor">Tags: <a href="gauche?p=Tag:0.9.13">0.9.13</a
>, <a href="gauche?p=Tag:REPL">REPL</a
></span
>
</p
>]]></content:encoded><dc:date>2023-09-26T17:30:47+00:00</dc:date>
</item>
<item rdf:about="http://blog.practical-scheme.net/gauche/20230926-unbound-variable"><title>Hints for unbound variable error</title>
<link>http://blog.practical-scheme.net/gauche/20230926-unbound-variable</link>
<content:encoded><![CDATA[<h2 id="H-f5pvf1">Hints for unbound variable error
</h2
><p>While working on REPL, sometimes you accidentally try to evaluate a variable that isn't
visible from your current module.
It is a bit annoying if you <em>know</em
> the module is loaded, just you forget
to <code>use</code
> it in the current module.
</p
><p>So we added a little feature.  When REPL reports an unbound varriable error,
it also lists if there are variables of that name, exported from
modules that are loaded into the process but not visible from the evaluating
module:
</p
><pre>gosh$ (thread-start! (make-thread (^[] (print &quot;Hi&quot;))))
*** UNBOUND-VARIABLE-ERROR: unbound variable: make-thread
    NOTE: `make-thread' is exported from the following module:
     - gauche.threads
Stack Trace:
_______________________________________
  0  (report-error e)
  1  (make-thread (^ () (print &quot;Hi&quot;)))
        at &quot;(input string port)&quot;:1
</pre
><p>It may be nice to show modules that aren't even loaded, too, but that would
be too costly so we avoided it.  It also doesn't show non-exported variables,
which is debatable--sometimes you forgot to export one and that caused this
error.  Let's use this for a while and see if we need non-exported ones, too.
</p
><hr /><p>This is realized in a general mechanism in error reporting.   We haven't
documented it yet, for we may tweak the interface, but I'll show it
to give the general idea.
</p
><p>The error message in REPL, including the stack trace, is produced by
<code>report-error</code
> (<a href="http://practical-scheme.net/gauche/man/?p=report-error">ref:report-error</a
>).  It prints <code>*** ...</code
> line,
with the condition class name and error message, then calls
a generic function <code>report-additional-condition</code
> on the condition.
We have a specialized method for <code>&lt;unbound-variable-error&gt;</code
> which
searches the name in the loaded modules and prints the hint.
</p
><p>If the thrown condition is a compound condition, <code>report-additional-condition</code
>
is called over each component of the compound condition.  This allows
custom report for each component.  When you load a file that has a statically
detectable error, you get the additional information (<code>While compiling ...</code
>).
It is also realized by the same mechanism.  The compiler and the loader
adds the location information as a compound condition, and <code>report-error</code
>
calls <code>report-additional-condition</code
> on them, which shows those additional
messages.
</p
><pre>gosh&gt; ,l ./foo
*** ERROR: wrong number of arguments: cons requires 2, but got 1
    While compiling &quot;./foo.scm&quot; at line 1: (define (bar x) (cons x))
    While loading &quot;./foo.scm&quot; at line 2
Stack Trace:
_______________________________________
  0  (report-error e)
  1  (errorf &quot;wrong number of arguments: ~a requires ~a, but got ~&quot;...
  2  (pass1/expand-inliner program id gval cenv)

</pre
><p><span class="tag-anchor">Tags: <a href="gauche?p=Tag:0.9.13">0.9.13</a
>, <a href="gauche?p=Tag:REPL">REPL</a
>, <a href="gauche?p=Tag:report-error">report-error</a
></span
>
</p
>]]></content:encoded><dc:date>2023-09-26T07:21:18+00:00</dc:date>
</item>
<item rdf:about="http://blog.practical-scheme.net/gauche/20230413-init-once"><title>:immutable slot option</title>
<link>http://blog.practical-scheme.net/gauche/20230413-init-once</link>
<content:encoded><![CDATA[<h2 id="H-c557ly"><code>:immutable</code
> slot option
</h2
><p>Immutability is far more valued nowadays than when CLOS was designed.  Back then,
basically the system allows programmers to change almost everything, assuming
he knows what he was doing.
</p
><p>However, specifying something immutable is not only to protect users
from shooting their own foot; it communicates to the readers.  Here the
readers can be a programmer who reads and uses the code years later (who
can be the same programmer who wrote it bot has forgotten the details),
or a program-processing programs such as optimizer to take advantage of
immutablity.
</p
><p>CLOS (and Gauche's object system) have enough flexibility
to implement immutable slots, but it is somewhat awkward.  It's not
as simple as having a custom setter that throws an error; for, the slot
setter is also called in the instance initializer which runs at the
instance construction.  You have to distinguish whether the slot setter
is invoked during initialization or outside initialization, but such
dynamic introspection would be costly.
</p
><p>We came up an alternative mechanism which is effectively realizes immutable
slots in practical situations, but does not require to distinguish whether
it's in initialization or not.
</p
><p>If a slot has a true value for <code>:immutable</code
> slot option, the slot can only
be initialized once--that is, the setter sets the value if the slot is previously
unbound, but throws an error if not.   If you give the slot an initial value,
either with <code>:init-keyword</code
> or <code>:init-value</code
> etc., then that one chance
to set the value is used within initializer.  Uninitialized immutable slots
don't make much sense, so we expect almost always immutable slots are initialized
this way.
</p
><p>It is possible that the initializer leaves the slot unbound, and later
the user call <code>slot-set!</code
> to set it once.  It can be viewed as delayed
initialization.
</p
><p>(We first named this option <code>init-once</code
>, for the slot can be set once,
but changed our mind for it could be confusing.) 
</p
><p><span class="tag-anchor">Tag: <a href="gauche?p=Tag:ObjectSystem">ObjectSystem</a
></span
>
</p
><p>
</p
>]]></content:encoded><dc:date>2023-04-13T21:40:43+00:00</dc:date>
</item>
<item rdf:about="http://blog.practical-scheme.net/gauche/20221005-macro-source-info"><title>Source info propagation with macro expansion</title>
<link>http://blog.practical-scheme.net/gauche/20221005-macro-source-info</link>
<content:encoded><![CDATA[<h2 id="H-1q1rwb9">Source info propagation with macro expansion
</h2
><p>Gauche tracks source code location information and shows it in the
stack trace.  However, what if the source is generated by macros?
In 0.9.12, the macro expander re-attached the original source
info to the outermost form of the macro output.  However, if
a runtime error occurred in constructed code other than the outermost
one, stack trace couldn't find the info and had to show
&quot;[unknown location]&quot;.  It was annoying especially when the
code was the result of nested macro expansions, that you didn't
get a clue about where the error came from.
</p
><p>I was annoyed enough, so from 0.9.13, you can have better stack trace.
(Well, if you're familiar with other Scheme that employs syntax-case
macro expander, you're already familiar with such a feature.  Yes, Gauche
finally caught up.)
</p
><p>Let's show it with a somewhat contrived example.  The following <code>cxr</code
>
macro expands to <code>cxxx...xxr</code
> according to the given sequence
of <code>a</code
> or <code>d</code
>.
</p
><pre>;; (cxr a r obj) == (car obj)
;; (cxr a a r obj) == (caar obj)
;; (cxr a d a r obj) == (cadar obj)
;;etc.
(define-syntax cxr
  (syntax-rules (a d r)
    [(_ r obj) obj]
    [(_ a xs ...) (car (cxr xs ...))]
    [(_ d xs ...) (cdr (cxr xs ...))]
    [(_ . xs) (syntax-error &quot;Malformed cxr:&quot; (cxr . xs))]))
</pre
><p>In 0.9.12, if you pass something that causes a runtime error,
you get the annoying unknown location:
</p
><pre>gosh$ (cxr a a a a r '(1 2 3 4))
*** ERROR: pair required, but got 1
Stack Trace:
_______________________________________
  0  (car (cxr a r '(1 2 3 4)))
        [unknown location]
  1  (eval expr env)
        at &quot;/usr/share/gauche-0.98/0.9.12/lib/gauche/interactive.scm&quot;:336
</pre
><p>In 0.9.13, you'll get this:
</p
><pre>gosh$ (cxr a a a a r '(1 2 3 4))
*** ERROR: pair required, but got 1
Stack Trace:
_______________________________________
  0  (car (cxr a r '(1 2 3 4)))
        at &quot;/home/shiro/src/Gauche/test/macro-source-info.scm&quot;:15
        expanded from (cxr a a r '(1 2 3 4))
        at &quot;/home/shiro/src/Gauche/test/macro-source-info.scm&quot;:15
        expanded from (cxr a a a r '(1 2 3 4))
        at &quot;/home/shiro/src/Gauche/test/macro-source-info.scm&quot;:15
        expanded from (cxr a a a a r '(1 2 3 4))
        at &quot;(standard input)&quot;:34
  1  (eval expr env)
        at &quot;/home/shiro/src/Gauche/src/../lib/gauche/interactive.scm&quot;:354
</pre
><p>This works with ER-macro, too.  Suppose we have another macro,
<code>c*r</code
>, in which you can give <code>a</code
>'s and <code>d</code
>'s in a single symbol.
That is, <code>(c*r aada obj)</code
> is <code>(caadar obj)</code
>.
We also let the code print the given symbol, just for the sake of
making things complicated.
</p
><pre>;; (c*r aa obj) == print 'aa' and return (caar obj)
;; (c*r addar obj) == print 'addar' and return (caadr obj)
;; etc.
(define-syntax c*r
  (er-macro-transformer
   (^[form rename cmp]
     (match form
       [(_ xs obj)
        (let1 cs (map ($ string-&gt;symbol $ string $)
                      (string-&gt;list (symbol-&gt;string xs)))
          (quasirename rename
            `(begin
               (print ',xs)
               (cxr ,@cs r ,obj))))]))))
</pre
><p>Here's 0.9.12:
</p
><pre>gosh$ (c*r aad '(1 2 3 4))
aad
*** ERROR: pair required, but got 2
Stack Trace:
_______________________________________
  0  (car (cxr a d r '(1 2 3 4)))
        [unknown location]
  1  (eval expr env)
        at &quot;/usr/share/gauche-0.98/0.9.12/lib/gauche/interactive.scm&quot;:336
</pre
><p>And HEAD:
</p
><pre>gosh$ (c*r aad '(1 2 3 4))
aad
*** ERROR: pair required, but got 2
Stack Trace:
_______________________________________
  0  (car (cxr a d r '(1 2 3 4)))
        at &quot;/home/shiro/src/Gauche/test/macro-source-info.scm&quot;:15
        expanded from (cxr a a d r '(1 2 3 4))
        at &quot;/home/shiro/src/Gauche/test/macro-source-info.scm&quot;:60
        expanded from (quasirename rename `(begin (print ',xs) (cxr ,@cs r  ...
        at &quot;/home/shiro/src/Gauche/test/macro-source-info.scm&quot;:57
  1  (eval expr env)
        at &quot;/home/shiro/src/Gauche/src/../lib/gauche/interactive.scm&quot;:354
</pre
><hr /><p>Now, if you're user of <code>syntax-case</code
> or <code>syntax-rules</code
>, there's
no wonder how it can be done.  Macro output is constructed
as syntactic objects, which can carry any sideband information.
But with ER-macro, you construct the output as a simple S-expression, so
it's not obviouhs where those information comes from.
</p
><p>Gauche has an extended pair that can carry extra information other
than car and cdr.  Those sideband data isn't visible as far as
you're treating it as a pair, nor it affects <code>equal?</code
>-ity of
the pairs.  Source code information is stored there by <code>read</code
>
procedure and its families.
</p
><pre>gosh$ (read-from-string &quot;(a b c d)&quot;)
(a b c d)
gosh$ (pair-attributes *1)
((source-info &quot;(input string port)&quot; 1))
</pre
><p>If you consturct lists with <code>cons</code
> or <code>list</code
>, those information
won't be attached.  However, <code>quasirename</code
> does the trick.
It extracts the original source info from the input, and re-attaches
it to the constructed form.
</p
><p>Note that, for a macro expander, we need to consider
two kinds of source information: One is of the macro definition,
and another is of the macro input.  The source info of the
macro definition is available through the argument of <code>quasirename</code
>.
But how can it get the macro input information?  The macro input is
already deconstructed by the time <code>quasirename</code
> is called.
</p
><p>We use another sideband mechanism, procedure tags.  Srfi-229 defines
a general mechanism to attach an arbitrary Scheme object to a procedure.
Gauche has more general mechanism (although not documented yet) that
a procedure can have multiple tags, and the macro input is attached
to the <em>rename</em
> procedure as one of such tags.  Then <code>quasirename</code
>
extracts that information from the rename procedure and applies
it to the output.
</p
><p>In the definition of <code>quasirename</code
>, the output construction code
looks like this:
</p
><pre>;; in src/libmacro.scm

   (if-let1 si (pair-attribute-get objs 'source-info #f)
     (let1 orig (assoc-ref ((with-module gauche.internal %procedure-tags-alist) r)
                           'macro-input)
       `(,extended-cons. ,xx ,yys '((source-info ,@si)
                                    ,@(cond-list
                                       [orig `(original . ,orig)]))))
     `(,cons. ,xx ,yys)))))
</pre
><p>The pair attribute <code>source-info</code
> holds the source info of
macro definition, and <code>original</code
> holds the macro input form.
</p
><hr /><p>The disadvantage of having source info in the sideband data of
pairs is, obviously, that you can't attach source info to other objects
than pairs.  I find it not a big issue in practice, for most expressions that need
attention are function calls, macro calls or special forms.
</p
><p>On the other hand, it has an advantage that quoted literal lists can have
source code information.  It can't be done with syntax objects,
for <code>quote</code
> strips any syntax wrappings.  It is handy when you
put a literal nested structure as DSL and let its walker signals
an error with the location of the literal structure.
</p
><hr /><p>This is a desired feature and I'm happy to have it.  However,
I'm feeling a bit of ambivalence, too.
</p
><p>The reason I prefer ER-macro to syntax-case is that ER-macro is
explicit---input and output are raw S-expression which you can
direclty touch and rearrange.  With syntax-case, things are wrapped
in opaque syntax object, and even though you can unwrap and rewrap the
objects, that opaqueness bothers me.
</p
><p>However, with this <code>quasirename</code
> modification, I did introduce
an implicit operation; even though the output of <code>quasirename</code
>
can be treated as an ordinary S-expression, it does more to it than just
consing.
</p
><p>If I feel comfortable with this, maybe I can also feel comfortable
with <code>syntax-case</code
>, too.  I don't know yet.  Let's see.
</p
><p><span class="tag-anchor">Tags: <a href="gauche?p=Tag:Macro%2C">Macro,</a
>, <a href="gauche?p=Tag:0.9.13%2C">0.9.13,</a
>, <a href="gauche?p=Tag:quasirename">quasirename</a
></span
>
</p
><p>
</p
>]]></content:encoded><dc:date>2022-10-05T09:05:28+00:00</dc:date>
</item>
<item rdf:about="http://blog.practical-scheme.net/gauche/20220617-setup-gauche"><title>Using Gauche in GitHub Actions</title>
<link>http://blog.practical-scheme.net/gauche/20220617-setup-gauche</link>
<content:encoded><![CDATA[<h2 id="H-h57947">Using Gauche in GitHub Actions
</h2
><p>I created a GitHub action to install Gauche in the runner, so that you can use Gauche in subsequent steps: <a href="https://github.com/shirok/setup-gauche">setup-gauche</a
>.  Currently the action works on Linux and OSX.
</p
><p>To use the action, simply say <code>uses: shirok/setup-gauche@v3</code
> in your job steps (check the latest version number in the <a href="https://github.com/shirok/setup-gauche">setup-gauche page</a
>).  The following is an excerpt of <code>.github/workflow/main.yml</code
> of Gauche-mecab:
</p
><pre>jobs:
  build-and-test-linux:
    runs-on: ubuntu-latest
    timeout-minutes: 10
    steps:
    - uses: actions/checkout@v3
    - uses: shirok/setup-gauche@v3
    - name: Install dependencies
      run: |
        sudo apt install -y libmecab-dev mecab-ipadic-utf8
    - name: Build and check
      run: |
        ./configure
        make
        make -s check
        make -s check-dep
</pre
><p>Gauche is installed in standard path (<code>/usr</code
> on Linux, <code>/usr/local</code
> on OSX) so that you can build Gauche extensions or run Gauche applications without any extra settings.
</p
><p>By default, it installs the latest release.  You can choose a specific version of Gauche to install via <code>gauche-version</code
> input parameter; specifically, saying 'snapshot' installs the latest snapshot (prerelease) build, if there's any newer than the latest release.
</p
><p><span class="tag-anchor">Tags: <a href="gauche?p=Tag:github">github</a
>, <a href="gauche?p=Tag:CI">CI</a
></span
>
</p
><p>
</p
>]]></content:encoded><dc:date>2022-06-17T10:56:36+00:00</dc:date>
</item>
<item rdf:about="http://blog.practical-scheme.net/gauche/20220402-running-gosh-docker"><title>Running gosh without installing</title>
<link>http://blog.practical-scheme.net/gauche/20220402-running-gosh-docker</link>
<content:encoded><![CDATA[<h2 id="H-1dmdkm8">Running gosh without installing
</h2
><p>Recently I wrote some test scripts in Gauche for a project that didn't use Gauche in particular.  I could've kicked <a href="https://github.com/shirok/get-gauche">get-gauche</a
> script during <code>make check</code
> to install Gauche locally as needed, but that seemed a bit of overkill, especially it was just for small test scripts.
</p
><p>Then I thought, well, I already have a Docker image.  If I can feed a local
script to it...
</p
><p>So here it is.  I included in a Docker image a small script <code>gosh-script</code
>,
which chdirs into <code>/home/app</code
> and run gosh.  If you mount local cwd on
<code>/home/app</code
>, the scripts, libraries and data in it are all visible to
<code>gosh</code
> in the Docker:
</p
><pre>docker run --rm -ti -v `pwd`:/home/app practicalscheme/gauche gosh-script TEST-SCRIPT
</pre
><p>Or, you can use <a href="https://github.com/shirok/Gauche-docker-image/blob/master/samples/local-script/run-gosh-in-docker.sh">run-gosh-in-docker.sh</a
> script.
</p
><p>You can't acceses local resources other than the filesystem below the current
directory, and you can't use extra libraries.
But for the simple tasks this is enough.
</p
><p>See <a href="https://github.com/shirok/Gauche-docker-image#running-a-local-gauche-script-without-installing-gauche">README in Gauche-docker-image</a
> for the details.
</p
><p><span class="tag-anchor">Tag: <a href="gauche?p=Tag:Docker">Docker</a
></span
>
</p
><p>
</p
>]]></content:encoded><dc:date>2022-04-02T00:44:54+00:00</dc:date>
</item>
<item rdf:about="http://blog.practical-scheme.net/gauche/20211024-type-punning"><title>Is this an Undefined Behavior?</title>
<link>http://blog.practical-scheme.net/gauche/20211024-type-punning</link>
<content:encoded><![CDATA[<h2 id="H-1ftrseh">Is this an Undefined Behavior?
</h2
><p>Automated tests of Gauche HEAD on Windows platform started failing since
several days ago.  The log showed SHA1 digest result didn't match.
It's weird, for I haven't touched that part of code for long time.
</p
><p>I isolated the reproducible condition.  It happens with the fairly
new gcc (11.2.0) with <code>-O2</code
>.   It doesn't exhibit without optimization,
nor with gcc 10.2.0 or other previous versions of gcc I have.
</p
><p>The problematic code is Aaron D. Gifford's SHA implementation sha2.c
( <a href="http://www.aarongifford.com/computers/sha.html">http://www.aarongifford.com/computers/sha.html</a
> ).  It was last updated
in January 2004, so it's pretty old, but I think it's still widely
used.
</p
><p>I narrowed down the problem to around
<a href="https://github.com/shirok/Gauche/blob/7dc69a5caa29182095f1130b78c237d018298902/ext/digest/sha2.c#L733">here</a
>:
</p
><pre>        /* Set the bit count: */
#if BYTE_ORDER == LITTLE_ENDIAN
        /* Convert FROM host byte order */
        REVERSE64(context-&gt;s1.bitcount,context-&gt;s1.bitcount);
#endif
        void *buf56 = &amp;context-&gt;s1.buffer[56];
        *(sha_word64*)buf56 = context-&gt;s1.bitcount;

        /* Final transform: */
        SHA1_Internal_Transform(context, (sha_word32*)context-&gt;s1.buffer);
</pre
><p>In our case, <code>BYTE_ORDER</code
> is <code>LITTLE_ENDIAN</code
>.   <code>REVERSE64</code
>
is a macro to swap the byte order of a 64bit word.   <code>context-&gt;s1.bitcount</code
>
is <code>uint64_t</code
>, and <code>context-&gt;s1.buffer</code
> is an array of unsigned chars.
What it does is to store 64bit-word of <code>bitcount</code
> into the <code>buffer</code
>
from 56th octet in the network byte order, and calls
<code>SHA1_Internal_Transform</code
>.
</p
><p>It compiles to this code with optimization:
</p
><pre>   25ca75e9d:   48 8b 53 18             mov    0x18(%rbx),%rdx
   25ca75ea1:   48 0f ca                bswap  %rdx
   25ca75ea4:   48 89 53 18             mov    %rdx,0x18(%rbx)
   25ca75ea8:   48 89 d9                mov    %rbx,%rcx
   25ca75eab:   4c 89 e2                mov    %r12,%rdx
   25ca75eae:   e8 8d fa ff ff          call   25ca75940 &lt;SHA1_Internal_Transform&gt;
</pre
><p>Here, <code>%rbx</code
> contains the pointer to <code>context</code
>, and <code>%r12</code
> to
<code>context-&gt;s1.buffer</code
>.   The first three instructions swap the
64bit word. (By the way, <code>REVERSE64</code
> macro is written with shifts and
bitmasks.  Gcc cleverly figures out its intent and
replaces the whole expression by a <code>bswap</code
> instrucion.)
The next three instruction is the calling sequence of
<code>SHA1_Internal_Transform</code
>.
</p
><p>Wait.  There're no instructions emitted to store <code>bitcount</code
>
into <code>*buf56</code
>.   I checked the assembly after this but there're no
instructions for the store either.
</p
><p>If I insert a dummy external function call before
<code>SHA1_Internal_Transform</code
> like this:
</p
><pre>        /* Set the bit count: */
#if BYTE_ORDER == LITTLE_ENDIAN
        /* Convert FROM host byte order */
        REVERSE64(context-&gt;s1.bitcount,context-&gt;s1.bitcount);
#endif
        void *buf56 = &amp;context-&gt;s1.buffer[56];
        *(sha_word64*)buf56 = context-&gt;s1.bitcount;

        puts(&quot;foo&quot;);

        /* Final transform: */
        SHA1_Internal_Transform(context, (sha_word32*)context-&gt;s1.buffer);
</pre
><p>Then the storing to <code>*buf56</code
> appears (<code>mov %rdx, 0x58(%rbx)</code
>):
</p
><pre>   25ca75e9d:   48 8b 53 18             mov    0x18(%rbx),%rdx
   25ca75ea1:   48 0f ca                bswap  %rdx
   25ca75ea4:   48 89 53 18             mov    %rdx,0x18(%rbx)
   25ca75ea8:   48 8d 0d 8f c7 00 00    lea    0xc78f(%rip),%rcx        # 25ca8263e &lt;.rdata+0x9e&gt;
   25ca75eaf:   48 89 53 58             mov    %rdx,0x58(%rbx)
   25ca75eb3:   e8 60 2e 00 00          call   25ca78d18 &lt;puts&gt;
   25ca75eb8:   4c 89 e2                mov    %r12,%rdx
   25ca75ebb:   48 89 d9                mov    %rbx,%rcx
   25ca75ebe:   e8 7d fa ff ff          call   25ca75940 &lt;SHA1_Internal_Transform&gt;
</pre
><p>Now, accessing type punned pointer can break the strict aliasing rule.
The gcc might have figured the storing
into <code>*buf56</code
> had nothing to do with <code>SHA1_Internal_Transform</code
>.
But I feel there still needs to be
a leap that it completely eliminates the store instruction.
</p
><p>Does <code>*(sha_word64*)buf56 = context-&gt;s1.bitcount</code
> triggers
Undefined Behavior?  That's why gcc is entitled to remove that code?
</p
><p><span class="tag-anchor">Tags: <a href="gauche?p=Tag:gcc">gcc</a
>, <a href="gauche?p=Tag:Optimization">Optimization</a
>, <a href="gauche?p=Tag:UndefinedBehavior">UndefinedBehavior</a
></span
>
</p
><p>
</p
>]]></content:encoded><dc:date>2022-06-04T21:58:23+00:00</dc:date>
</item>
</rdf:RDF>
