From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp11.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms9.migadu.com with LMTPS id MFi6CPUjTGTpEAAASxT56A (envelope-from ) for ; Fri, 28 Apr 2023 21:52:21 +0200 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp11.migadu.com with LMTPS id mGrCCPUjTGT5jQAA9RJhRA (envelope-from ) for ; Fri, 28 Apr 2023 21:52:21 +0200 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id C1D17E0E for ; Fri, 28 Apr 2023 21:52:19 +0200 (CEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1psTdm-0002Wy-EK; Fri, 28 Apr 2023 15:20:22 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1psTdU-0002PX-9C for guix-patches@gnu.org; Fri, 28 Apr 2023 15:20:05 -0400 Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1psTdT-0004Nt-Ti for guix-patches@gnu.org; Fri, 28 Apr 2023 15:20:04 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1psTdT-00063Y-Oh for guix-patches@gnu.org; Fri, 28 Apr 2023 15:20:03 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#63135] [PATCH v2 3/5] records: match-record: Support thunked and delayed fields. Resent-From: "(" Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Fri, 28 Apr 2023 19:20:03 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 63135 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 63135@debbugs.gnu.org Cc: "\(" , Josselin Poiret Received: via spool by 63135-submit@debbugs.gnu.org id=B63135.168270956223145 (code B ref 63135); Fri, 28 Apr 2023 19:20:03 +0000 Received: (at 63135) by debbugs.gnu.org; 28 Apr 2023 19:19:22 +0000 Received: from localhost ([127.0.0.1]:34540 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1psTcn-000619-AO for submit@debbugs.gnu.org; Fri, 28 Apr 2023 15:19:21 -0400 Received: from knopi.disroot.org ([178.21.23.139]:41632) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1psTck-00060A-B2 for 63135@debbugs.gnu.org; Fri, 28 Apr 2023 15:19:19 -0400 Received: from localhost (localhost [127.0.0.1]) by disroot.org (Postfix) with ESMTP id B663240316; Fri, 28 Apr 2023 21:19:17 +0200 (CEST) X-Virus-Scanned: SPAM Filter at disroot.org Received: from knopi.disroot.org ([127.0.0.1]) by localhost (disroot.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 1Dl51W6z95T8; Fri, 28 Apr 2023 21:19:16 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=disroot.org; s=mail; t=1682709556; bh=VR5KpZOjanz7iRoDHHzMvUH88/n7hp56BE3Gxtae7XM=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=kPcU7yCT+hatnu3N85nZNDXeAD6i5uLg0pBy398tvQ/6iER3r2X6IyGeBMiixqu7o Sz0H2PURAMtzhiH6ezZ0CD4NfTmsiUsw7UCT8zr+xD3rzAXl3h7kiuBup2wA9TELGD In+p0fCD68ofN6bHshT/VFIv/3A9gNBr4E50POVaOC8fCDOTnvQlY6bk/ahwBhLLHL jk+rTbPiWjiLGpeXgq5XECZxM5DKZStL+zXKZJDie+n6zbPHidIpYsM8A3vuXOl7ec lhM031MWW8XKwxx37NbyWbFOFKE20lsWiTtyFLlVM3HQgc3xnWP7bpWuU+UAPO18gt 4ZX/3ObuldBtA== Date: Fri, 28 Apr 2023 20:19:03 +0100 Message-Id: <20230428191905.13860-4-paren@disroot.org> In-Reply-To: <20230428191905.13860-1-paren@disroot.org> References: <20230428191905.13860-1-paren@disroot.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: "\(" X-ACL-Warn: , "\( via Guix-patches" From: "\( via Guix-patches" via Errors-To: guix-patches-bounces+larch=yhetil.org@gnu.org Sender: guix-patches-bounces+larch=yhetil.org@gnu.org X-Migadu-Country: US X-Migadu-Flow: FLOW_IN ARC-Seal: i=1; s=key1; d=yhetil.org; t=1682711540; a=rsa-sha256; cv=none; b=nrQhGMI4O6c4u2+KhTQ1zhhX4SZ1IPyYAXiFcpgGQx/pAKw91xpfcxxoRcQv6m3eMyWVRY wZRbm/a5v1cSxBkZWvBrQ12ZHoHeobNR6Avb98Ic39UI+iBEy1Kqibjs+sbyWZbVLrVSSD YvPJpSgp7Dw9cdiQsszVBNWGEcTicf+k7tmJ9id+j93e0FCdWAN7ZaACvESXeMUJY7WmPW xvId5dd4CfV9mfREmrPaQqpB5nA56L119t8r1wDr3fWXS1dLEx97s14cMrobn0odQoq7jI rcdAchQpUGDdLMOnwA3TgVPJoRz4bwEIsZHwqP5ZDNSFaO07wSqU7/mmrlc5Dw== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=disroot.org header.s=mail header.b=kPcU7yCT; dmarc=pass (policy=none) header.from=gnu.org; spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org" ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1682711540; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding:resent-cc: resent-from:resent-sender:resent-message-id:in-reply-to:in-reply-to: references:references:list-id:list-help:list-unsubscribe: list-subscribe:list-post:dkim-signature; bh=hpzUvk47KkQsHTydjNjlHi0SS3qugV1PTO5vVJqwtZ8=; b=b9YjlR30akuE2J5POdEnalXP3u84wEpl2zHklL7ZvmUtUgIUgmHkUGqoSZIw79G0RGw9JO XWjXq/jYk5fyP8A0SxoQMrUXHGIN6kihIUQcEKeW8W3Bah7hda+wiy8txg35ayYuao9d9P 11RuF4TELS4cYAEC1xBZxjPQo2Z1/NSWPvVdlojgXc0UYkcOycCTeMwDopz8/jCiBzhkH9 HTqpWTBPd104qXbzh9iJC1dQexepZZo4wu+2VKADHCLnEFg381Lu7Ih7KmFgXl59mDdxTr CCGSWiYiL0GeF9qdxQqV6TwS3rXaqsfoTV2ma5cPM0hIjv5ltazTPwloBAQD7Q== X-Migadu-Spam-Score: -1.91 X-Spam-Score: -1.91 X-Migadu-Queue-Id: C1D17E0E X-Migadu-Scanner: scn0.migadu.com Authentication-Results: aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=disroot.org header.s=mail header.b=kPcU7yCT; dmarc=pass (policy=none) header.from=gnu.org; spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org" X-TUID: wXc8m4ScvxV9 * guix/records.scm (match-record): Unwrap matched thunked and delayed fields. * tests/records.scm ("match-record, thunked field", "match-record, delayed field"): New tests. --- guix/records.scm | 60 ++++++++++++++++++++++++++++++----------------- tests/records.scm | 29 +++++++++++++++++++++++ 2 files changed, 68 insertions(+), 21 deletions(-) diff --git a/guix/records.scm b/guix/records.scm index 4bee9d0aac..041eb2f297 100644 --- a/guix/records.scm +++ b/guix/records.scm @@ -21,6 +21,7 @@ (define-module (guix records) #:use-module (srfi srfi-1) #:use-module (srfi srfi-9) #:use-module (srfi srfi-26) + #:use-module (srfi srfi-71) #:use-module (ice-9 match) #:use-module (ice-9 rdelim) #:autoload (system base target) (target-most-positive-fixnum) @@ -428,10 +429,19 @@ (define (compute-abi-cookie field-specs) (defaults (filter-map field-default-value #'((field properties ...) ...))) (sanitizers (filter-map field-sanitizer - #'((field properties ...) ...))) + #'((field properties ...) ...))) (cookie (compute-abi-cookie field-spec))) (with-syntax (((field-spec* ...) (map field-spec->srfi-9 field-spec)) + ((field-type ...) + (map (match-lambda + ((? thunked-field?) + (datum->syntax s 'thunked)) + ((? delayed-field?) + (datum->syntax s 'delayed)) + (else + (datum->syntax s 'normal))) + field-spec)) ((thunked-field-accessor ...) (filter-map (lambda (field) (and (thunked-field? field) @@ -465,7 +475,7 @@ (define-syntax type macro-expansion time." (syntax-case s (map-fields) ((_ (map-fields _ _) macro) - #'(macro (field ...))) + #'(macro ((field field-type) ...))) (id (identifier? #'id) #'#,(rtd-identifier #'type))))) @@ -578,31 +588,42 @@ (define (recutils->alist port) ;;; Pattern matching. ;;; -(define-syntax lookup-field +(define-syntax lookup-field+wrapper (lambda (s) - "Look up FIELD in the given list and return an expression that represents -its offset in the record. Raise a syntax violation when the field is not -found, displaying it as originating in form S*." - (syntax-case s () - ((_ s* field offset ()) + "Look up FIELD in the given list and return both an expression that represents +its offset in the record and a procedure that wraps it to return its \"true\" value +(for instance, FORCE is returned in the case of a delayed field). RECORD is passed +to thunked values. Raise a syntax violation when the field is not found, displaying +it as originating in form S*." + (syntax-case s (normal delayed thunked) + ((_ s* record field offset ()) (syntax-violation 'match-record "unknown record type field" #'s* #'field)) - ((_ s* field offset (head tail ...)) + ((_ s* record field offset ((head normal) tail ...)) + (free-identifier=? #'field #'head) + #'(values offset identity)) + ((_ s* record field offset ((head delayed) tail ...)) (free-identifier=? #'field #'head) - #'offset) - ((_ s* field offset (_ tail ...)) - #'(lookup-field s* field (+ 1 offset) (tail ...)))))) + #'(values offset force)) + ((_ s* record field offset ((head thunked) tail ...)) + (free-identifier=? #'field #'head) + #'(values offset (cut <> record))) + ((_ s* record field offset (_ tail ...)) + #'(lookup-field+wrapper s* record field + (+ 1 offset) (tail ...)))))) (define-syntax match-record-inner (lambda (s) (syntax-case s () ((_ s* record type ((field variable) rest ...) body ...) - #'(let-syntax ((field-offset (syntax-rules () - ((_ f) - (lookup-field s* field 0 f))))) - (let* ((offset (type (map-fields type match-record) field-offset)) - (variable (struct-ref record offset))) + #'(let-syntax ((field-offset+wrapper + (syntax-rules () + ((_ f) + (lookup-field+wrapper s* record field 0 f))))) + (let* ((offset wrap (type (map-fields type match-record) + field-offset+wrapper)) + (variable (wrap (struct-ref record offset)))) (match-record-inner s* record type (rest ...) body ...)))) ((_ s* record type (field rest ...) body ...) ;; Redirect to the canonical form above. @@ -614,10 +635,7 @@ (define-syntax match-record (lambda (s) "Bind each FIELD of a RECORD of the given TYPE to it's FIELD name. The order in which fields appear does not matter. A syntax error is raised if -an unknown field is queried. - -The current implementation does not support thunked and delayed fields." - ;; TODO support thunked and delayed fields +an unknown field is queried." (syntax-case s () ((_ record type (fields ...) body ...) #`(if (eq? (struct-vtable record) type) diff --git a/tests/records.scm b/tests/records.scm index b1203dfeb7..4f0aeb3903 100644 --- a/tests/records.scm +++ b/tests/records.scm @@ -561,4 +561,33 @@ (define-record-type* foo make-foo (make-fresh-user-module))) (lambda (key . args) key))) +(test-equal "match-record, delayed field" + "foo bar bar foo" + (begin + (define-record-type* with-delayed make-with-delayed + with-delayed? + (delayed with-delayed-delayed + (delayed))) + + (let ((rec (with-delayed + (delayed "foo bar bar foo")))) + (match-record rec (delayed) + delayed)))) + +(test-equal "match-record, thunked field" + '("foo" "foobar") + (begin + (define-record-type* with-thunked make-with-thunked + with-thunked? + (normal with-thunked-normal) + (thunked with-thunked-thunked + (thunked))) + + (let ((rec (with-thunked + (normal "foo") + (thunked (string-append (with-thunked-normal this-record) + "bar"))))) + (match-record rec (normal thunked) + (list normal thunked))))) + (test-end) -- 2.39.2