From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: ludo@gnu.org (Ludovic =?utf-8?Q?Court=C3=A8s?=) Newsgroups: gmane.lisp.guile.user Subject: Re: cannot compile: srfi-10 define-reader-ctor 'hash '#,( Date: Sat, 05 Jul 2014 15:40:47 +0200 Message-ID: <874myvudnk.fsf@gnu.org> References: <87y4w9jog8.fsf@drakenvlieg.flower> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Trace: ger.gmane.org 1404567681 7218 80.91.229.3 (5 Jul 2014 13:41:21 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 5 Jul 2014 13:41:21 +0000 (UTC) Cc: guile-user@gnu.org To: Jan Nieuwenhuizen Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Sat Jul 05 15:41:12 2014 Return-path: Envelope-to: guile-user@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1X3QDH-0002tI-BS for guile-user@m.gmane.org; Sat, 05 Jul 2014 15:41:11 +0200 Original-Received: from localhost ([::1]:42303 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1X3QDH-0005cK-1a for guile-user@m.gmane.org; Sat, 05 Jul 2014 09:41:11 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:39342) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1X3QD2-0005bC-5s for guile-user@gnu.org; Sat, 05 Jul 2014 09:41:02 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1X3QCw-0005nP-8x for guile-user@gnu.org; Sat, 05 Jul 2014 09:40:56 -0400 Original-Received: from hera.aquilenet.fr ([2a01:474::1]:60442) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1X3QCv-0005md-R0; Sat, 05 Jul 2014 09:40:50 -0400 Original-Received: from localhost (localhost [127.0.0.1]) by hera.aquilenet.fr (Postfix) with ESMTP id 5952C30EC; Sat, 5 Jul 2014 15:40:48 +0200 (CEST) Original-Received: from hera.aquilenet.fr ([127.0.0.1]) by localhost (hera.aquilenet.fr [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id v13SiybFkAoy; Sat, 5 Jul 2014 15:40:48 +0200 (CEST) Original-Received: from pluto (reverse-83.fdn.fr [80.67.176.83]) by hera.aquilenet.fr (Postfix) with ESMTPSA id D94AC21AF; Sat, 5 Jul 2014 15:40:47 +0200 (CEST) X-URL: http://www.fdn.fr/~lcourtes/ X-Revolutionary-Date: 17 Messidor an 222 de la =?utf-8?Q?R=C3=A9volution?= X-PGP-Key-ID: 0xEA52ECF4 X-PGP-Key: http://www.fdn.fr/~lcourtes/ludovic.asc X-PGP-Fingerprint: 83C4 F8E5 10A3 3B4C 5BEA D15D 77DD 95E2 EA52 ECF4 X-OS: x86_64-unknown-linux-gnu In-Reply-To: <87y4w9jog8.fsf@drakenvlieg.flower> (Jan Nieuwenhuizen's message of "Fri, 04 Jul 2014 14:30:47 +0200") User-Agent: Gnus/5.130009 (Ma Gnus v0.9) Emacs/24.3 (gnu/linux) X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2a01:474::1 X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane.org@gnu.org Original-Sender: guile-user-bounces+guile-user=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.user:11325 Archived-At: Jan Nieuwenhuizen skribis: > (use-modules (srfi srfi-10)) > > (define-reader-ctor 'hash > (lambda elems > (let ((table (make-hash-table))) > (for-each (lambda (elem) > (apply hash-set! table elem)) > elems) > table))) In this example, you want the reader extension to be available at compile time, and not necessarily at run time. However, by writing the code as is, the reader extension is available only at run time, hence the error. To require evaluation of the =E2=80=98define-reader-ctor=E2=80=99 form at c= ompile time, change the code to (info "(guile) Eval When"): --8<---------------cut here---------------start------------->8--- (eval-when (expand) (define-reader-ctor 'hash (lambda elems (let ((table (make-hash-table))) (for-each (lambda (elem) (apply hash-set! table elem)) elems) table)))) --8<---------------cut here---------------end--------------->8--- Now, you=E2=80=99ll get this error: --8<---------------cut here---------------start------------->8--- $ guile ~/tmp/hash.scm ;;; note: auto-compilation is enabled, set GUILE_AUTO_COMPILE=3D0 ;;; or pass the --no-auto-compile argument to disable. ;;; compiling /home/ludo/tmp/hash.scm ;;; WARNING: compilation of /home/ludo/tmp/hash.scm failed: ;;; ERROR: build-constant-store: unrecognized object # cat --8<---------------cut here---------------end--------------->8--- The problem here is that the reader extension above returns (at compile time) a hash table. However, a hash table as such cannot appear in source code text, hence the error. Instead, you=E2=80=99d want the reader extension to return source code that constructs the hash table. First, a macro: --8<---------------cut here---------------start------------->8--- (define-syntax build-hash-table (syntax-rules () ((_ table (key value) rest ...) (begin (hash-set! table key value) (build-hash-table table rest ...))) ((_ table) table))) (define-syntax-rule (hash-table (key value) ...) (let ((table (make-hash-table))) (build-hash-table table (key value) ...))) --8<---------------cut here---------------end--------------->8--- With that macro, we get: --8<---------------cut here---------------start------------->8--- scheme@(guile-user)> ,expand (hash-table ("a" 1) ("b" 2) ("c" 3)) $2 =3D (let ((table (make-hash-table))) (hash-set! table "a" 1) (begin (hash-set! table "b" 2) (begin (hash-set! table "c" 3) table))) --8<---------------cut here---------------end--------------->8--- Now, if in addition you want #, syntax for that, you can write: --8<---------------cut here---------------start------------->8--- (eval-when (expand) (define-reader-ctor 'hash (lambda elems `(hash-table ,@elems)))) (define (animal->family animal) (hash-ref #,(hash ("tiger" "cat") ("lion" "cat") ("wolf" "dog")) animal)) (display (animal->family "lion")) (newline) --8<---------------cut here---------------end--------------->8--- But I=E2=80=99m not sure the #, extension is worthwhile here. HTH, Ludo=E2=80=99.