Received: (at 61214) by debbugs.gnu.org; 14 Feb 2023 19:26:41 +0000 From debbugs-submit-bounces <at> debbugs.gnu.org Tue Feb 14 14:26:40 2023 Received: from localhost ([127.0.0.1]:57035 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>) id 1pS0wq-0002XG-Mg for submit <at> debbugs.gnu.org; Tue, 14 Feb 2023 14:26:40 -0500 Received: from knopi.disroot.org ([178.21.23.139]:58660) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <paren@HIDDEN>) id 1pS0wn-0002X0-F6 for 61214 <at> debbugs.gnu.org; Tue, 14 Feb 2023 14:26:38 -0500 Received: from localhost (localhost [127.0.0.1]) by disroot.org (Postfix) with ESMTP id 2000241551; Tue, 14 Feb 2023 20:26:36 +0100 (CET) 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 I4Wu6_AHQ-wy; Tue, 14 Feb 2023 20:26:34 +0100 (CET) Content-Type: multipart/signed; boundary=77444312f7d082b9426ebb5839e1913bd3316fc37db693e997c07695ee1d; micalg=pgp-sha512; protocol="application/pgp-signature" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=disroot.org; s=mail; t=1676402794; bh=aBavourCBnf93UbWiJDe4wahMfsmBKvr09Yi5L1pAxQ=; h=Date:To:Cc:Subject:From:References:In-Reply-To; b=J7RHG05lCellU8k7DPHbn4tVuGXTjRO5xpJIlEidtXICtgJ1MzP9zHHqoFGRZS9ub QzEsNEvfXLj3xussyFisALVVyJJk5xbBIblGEO6V3WwhqyKnaaKbBEpufc5b811MCh 1HcTh7+6si9pzNH0dig1aKOPapepQsMF1S3DG3RHtDfBeIt+akylZNm4URAaeclqKd xnoPcL7FHZxlSnIdzGXSqlro7FyrhKv4x5FOyKgTZlsAPcO8sj4tV3IYEgam9zf2gT Nuj3+67s958dhoKFNfHmzejOaag04Uohxy88RdfoT7G1rWaTOmd7cIMe3/yvjXUCr8 tXfTq7l4vYeIQ== Date: Tue, 14 Feb 2023 19:26:30 +0000 Message-Id: <CQIJ6Y5TL6YD.1BFDOES1LT8EQ@guix-framework> To: "Simon Tournier" <zimon.toutoune@HIDDEN>, "( via Guix-patches via" <guix-patches@HIDDEN>, <61214 <at> debbugs.gnu.org> Subject: Re: [bug#61214] [PATCH guix-artwork v5] website: posts: Add Dissecting Guix, Part 2: The Store Monad. From: "(" <paren@HIDDEN> References: <20230201172821.3072-1-paren@HIDDEN> <20230214073314.2651-1-paren@HIDDEN> <87mt5gyvlh.fsf@HIDDEN> In-Reply-To: <87mt5gyvlh.fsf@HIDDEN> X-Spam-Score: 0.2 (/) X-Debbugs-Envelope-To: 61214 Cc: ludo@HIDDEN, mail@HIDDEN, zimoun.toutoune@HIDDEN X-BeenThere: debbugs-submit <at> debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: <debbugs-submit.debbugs.gnu.org> List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe> List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/> List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org> List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help> List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe> Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org> X-Spam-Score: -0.8 (/) --77444312f7d082b9426ebb5839e1913bd3316fc37db693e997c07695ee1d Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 On Tue Feb 14, 2023 at 6:01 PM GMT, Simon Tournier wrote: > --^ > Typo > > s/returing/returning aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa -- ( --77444312f7d082b9426ebb5839e1913bd3316fc37db693e997c07695ee1d Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQGzBAABCgAdFiEE6Vh10NblKE5doNlW7ImHg/nqI20FAmPr4GcACgkQ7ImHg/nq I21jowwApbtaNIpOWPJNaiyxyBNyI3qyFxSLJqElNrBSthtNSt/zK1BctTaG0J0u qJVZ8J1PgckFFwhPSGyuMf765dbXAkhkchQG6Fk5z6Hz2cNN/H8R0cnpOVXgIbiF EOPHSCHaWHaMFs9Y5rGp0BlBiL9cIym3CX5OS7MOvPTb9sLp9C/+E8aGLc3zLOJy U6O9r1kt7nOKPbaY6b6DnPDfSuKAEXYH9dwjwDWsDUncU51AeLrivy11tYbhJ/5Z H+RxnHYcBnhjNG17r2wcC35tG6fKTXv3l8KHUZkB/a3+qaXrxvOpg0ZS6vTm2e9H mkKnhdAks48a3FfHpZZ6kZshVfuh3pUWhNSZUzTv4xZhwXqgrrvVZAuI4V1GpebU NkdEyFxQYRj+tZ5mzuInbYdghBiLrD3Il/sXwTsd9tdWYXQhiTkcXWLnpgkV5sKy Ovc+f7bxUxNNdw6+l2O9bMecwusAzDYDoDmdUiL9JN8axk9clx/0hRwMB1zkyTw5 Ok7X4KH/ =TsFY -----END PGP SIGNATURE----- --77444312f7d082b9426ebb5839e1913bd3316fc37db693e997c07695ee1d--
guix-patches@HIDDEN
:bug#61214
; Package guix-patches
.
Full text available.Received: (at submit) by debbugs.gnu.org; 14 Feb 2023 19:26:43 +0000 From debbugs-submit-bounces <at> debbugs.gnu.org Tue Feb 14 14:26:43 2023 Received: from localhost ([127.0.0.1]:57037 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>) id 1pS0ws-0002XP-Tf for submit <at> debbugs.gnu.org; Tue, 14 Feb 2023 14:26:43 -0500 Received: from lists.gnu.org ([209.51.188.17]:38192) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <paren@HIDDEN>) id 1pS0wq-0002XA-3L for submit <at> debbugs.gnu.org; Tue, 14 Feb 2023 14:26:40 -0500 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 <paren@HIDDEN>) id 1pS0wp-0005IQ-S3 for guix-patches@HIDDEN; Tue, 14 Feb 2023 14:26:39 -0500 Received: from knopi.disroot.org ([178.21.23.139]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from <paren@HIDDEN>) id 1pS0wo-0005rI-EK; Tue, 14 Feb 2023 14:26:39 -0500 Received: from localhost (localhost [127.0.0.1]) by disroot.org (Postfix) with ESMTP id 2000241551; Tue, 14 Feb 2023 20:26:36 +0100 (CET) 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 I4Wu6_AHQ-wy; Tue, 14 Feb 2023 20:26:34 +0100 (CET) Content-Type: multipart/signed; boundary=77444312f7d082b9426ebb5839e1913bd3316fc37db693e997c07695ee1d; micalg=pgp-sha512; protocol="application/pgp-signature" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=disroot.org; s=mail; t=1676402794; bh=aBavourCBnf93UbWiJDe4wahMfsmBKvr09Yi5L1pAxQ=; h=Date:To:Cc:Subject:From:References:In-Reply-To; b=J7RHG05lCellU8k7DPHbn4tVuGXTjRO5xpJIlEidtXICtgJ1MzP9zHHqoFGRZS9ub QzEsNEvfXLj3xussyFisALVVyJJk5xbBIblGEO6V3WwhqyKnaaKbBEpufc5b811MCh 1HcTh7+6si9pzNH0dig1aKOPapepQsMF1S3DG3RHtDfBeIt+akylZNm4URAaeclqKd xnoPcL7FHZxlSnIdzGXSqlro7FyrhKv4x5FOyKgTZlsAPcO8sj4tV3IYEgam9zf2gT Nuj3+67s958dhoKFNfHmzejOaag04Uohxy88RdfoT7G1rWaTOmd7cIMe3/yvjXUCr8 tXfTq7l4vYeIQ== Date: Tue, 14 Feb 2023 19:26:30 +0000 Message-Id: <CQIJ6Y5TL6YD.1BFDOES1LT8EQ@guix-framework> To: "Simon Tournier" <zimon.toutoune@HIDDEN>, "( via Guix-patches via" <guix-patches@HIDDEN>, <61214 <at> debbugs.gnu.org> Subject: Re: [bug#61214] [PATCH guix-artwork v5] website: posts: Add Dissecting Guix, Part 2: The Store Monad. From: "(" <paren@HIDDEN> References: <20230201172821.3072-1-paren@HIDDEN> <20230214073314.2651-1-paren@HIDDEN> <87mt5gyvlh.fsf@HIDDEN> In-Reply-To: <87mt5gyvlh.fsf@HIDDEN> Received-SPF: pass client-ip=178.21.23.139; envelope-from=paren@HIDDEN; helo=knopi.disroot.org X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, MIME_HEADER_CTYPE_ONLY=0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, TRACKER_ID=0.1 autolearn=ham autolearn_force=no X-Spam_action: no action X-Spam-Score: -1.2 (-) X-Debbugs-Envelope-To: submit Cc: ludo@HIDDEN, mail@HIDDEN, zimoun.toutoune@HIDDEN X-BeenThere: debbugs-submit <at> debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: <debbugs-submit.debbugs.gnu.org> List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe> List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/> List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org> List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help> List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe> Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org> X-Spam-Score: -2.2 (--) --77444312f7d082b9426ebb5839e1913bd3316fc37db693e997c07695ee1d Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 On Tue Feb 14, 2023 at 6:01 PM GMT, Simon Tournier wrote: > --^ > Typo > > s/returing/returning aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa -- ( --77444312f7d082b9426ebb5839e1913bd3316fc37db693e997c07695ee1d Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQGzBAABCgAdFiEE6Vh10NblKE5doNlW7ImHg/nqI20FAmPr4GcACgkQ7ImHg/nq I21jowwApbtaNIpOWPJNaiyxyBNyI3qyFxSLJqElNrBSthtNSt/zK1BctTaG0J0u qJVZ8J1PgckFFwhPSGyuMf765dbXAkhkchQG6Fk5z6Hz2cNN/H8R0cnpOVXgIbiF EOPHSCHaWHaMFs9Y5rGp0BlBiL9cIym3CX5OS7MOvPTb9sLp9C/+E8aGLc3zLOJy U6O9r1kt7nOKPbaY6b6DnPDfSuKAEXYH9dwjwDWsDUncU51AeLrivy11tYbhJ/5Z H+RxnHYcBnhjNG17r2wcC35tG6fKTXv3l8KHUZkB/a3+qaXrxvOpg0ZS6vTm2e9H mkKnhdAks48a3FfHpZZ6kZshVfuh3pUWhNSZUzTv4xZhwXqgrrvVZAuI4V1GpebU NkdEyFxQYRj+tZ5mzuInbYdghBiLrD3Il/sXwTsd9tdWYXQhiTkcXWLnpgkV5sKy Ovc+f7bxUxNNdw6+l2O9bMecwusAzDYDoDmdUiL9JN8axk9clx/0hRwMB1zkyTw5 Ok7X4KH/ =TsFY -----END PGP SIGNATURE----- --77444312f7d082b9426ebb5839e1913bd3316fc37db693e997c07695ee1d--
guix-patches@HIDDEN
:bug#61214
; Package guix-patches
.
Full text available.Received: (at 61214) by debbugs.gnu.org; 14 Feb 2023 19:25:17 +0000 From debbugs-submit-bounces <at> debbugs.gnu.org Tue Feb 14 14:25:17 2023 Received: from localhost ([127.0.0.1]:57011 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>) id 1pS0vU-0002TG-Rv for submit <at> debbugs.gnu.org; Tue, 14 Feb 2023 14:25:17 -0500 Received: from mail-wm1-f41.google.com ([209.85.128.41]:44741) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <zimon.toutoune@HIDDEN>) id 1pS0vT-0002Ss-0g for 61214 <at> debbugs.gnu.org; Tue, 14 Feb 2023 14:25:15 -0500 Received: by mail-wm1-f41.google.com with SMTP id s13-20020a05600c45cd00b003ddca7a2bcbso7035936wmo.3 for <61214 <at> debbugs.gnu.org>; Tue, 14 Feb 2023 11:25:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:references :in-reply-to:subject:cc:to:from:from:to:cc:subject:date:message-id :reply-to; bh=iWPPOrB+O1DXLPYTjoyHqD8kpz87pfuqooE6g0WGsCY=; b=lhbJ7PoNEfpIHDLJMG6ChSkX+6lgM/J3ylTjohH2wAJ4JHRY2uQWPx9GkhseUgqzhZ 6PvgtwdghrtAJ6CM1Lkdt5F96YxxS049EM3uzL+CWwNiE0XZNawjgDovujAcPYl9f5eC 9ocqUJmuI5rXxeweIc1vn1KQGwfz6RhJM4k6RNCwzIXFUDN7StLchDSKTZqR2D1DkJfP YnSteGgd7sfayt9hPVX9IAQbvyAnEyYn0V7h8cC6BtjD3QK8//S+dsfXiHdL7C4j5S/2 4lmtj323uqSgYRc9NGa54BW+s5Eq4AYkvZAfctQMwODqUOHfAxl8L9MaudYqODK7u18x FB+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:references :in-reply-to:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=iWPPOrB+O1DXLPYTjoyHqD8kpz87pfuqooE6g0WGsCY=; b=D1GztSADoYHqBbCHPQjS8aljA7VqF4FJe3Y4f5M4oFbck2ICf88UeHu1zFc9I9rnV5 Rhml2o1Ag1Zwe/NP8rVJXk/2XUjIFBzu9bwVKZ1eqlHWrNgUAXsjeqOdMM4S5wnC/3/e +BT4vBSpwcJAk38n9MwUAW2CkMmXT3Wx38tVgRNgY0eUZOibqNw0xwQPzmfrKKXrIzKl 3BPw8yR9VQ7gO1HyKPlVooeDyY0a3j0eI8kQ4hTfAUdVRQUrSDnHTuKOM9OMrUwrNBYC sSTnwCAMY3O2wTRA1E4Z8BTkoptXfz63bCXWBVN6OvCumHPZRezBtORzwcG912NeW7OV 85Xw== X-Gm-Message-State: AO0yUKXGhzcJ9S1spq5HwquMvOJGnGGVwYUpiR+hq7+jUzpE8Bk6jicw udGPPdvMY7kirFS1HdsFTBc= X-Google-Smtp-Source: AK7set9bnnVTJX5bA7L/cJQ1qXbnB/5GYdwiEkeFw9TvavMnpDt+Xlj7TOCrxOejpg0NgnUI4a0QZQ== X-Received: by 2002:a05:600c:3caa:b0:3dc:5ae4:c13d with SMTP id bg42-20020a05600c3caa00b003dc5ae4c13dmr25058wmb.4.1676402709520; Tue, 14 Feb 2023 11:25:09 -0800 (PST) Received: from pfiuh07 ([193.48.40.241]) by smtp.gmail.com with ESMTPSA id t15-20020a05600c198f00b003ddc781b1d4sm19410611wmq.33.2023.02.14.11.25.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Feb 2023 11:25:09 -0800 (PST) From: Simon Tournier <zimon.toutoune@HIDDEN> To: "( via Guix-patches via" <guix-patches@HIDDEN>, 61214 <at> debbugs.gnu.org Subject: Re: [bug#61214] [PATCH guix-artwork v5] website: posts: Add Dissecting Guix, Part 2: The Store Monad. In-Reply-To: <20230214073314.2651-1-paren@HIDDEN> References: <20230201172821.3072-1-paren@HIDDEN> <20230214073314.2651-1-paren@HIDDEN> Date: Tue, 14 Feb 2023 19:01:46 +0100 Message-ID: <87mt5gyvlh.fsf@HIDDEN> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Score: 0.0 (/) X-Debbugs-Envelope-To: 61214 Cc: "\(" <paren@HIDDEN>, ludo@HIDDEN, mail@HIDDEN, zimoun.toutoune@HIDDEN X-BeenThere: debbugs-submit <at> debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: <debbugs-submit.debbugs.gnu.org> List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe> List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/> List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org> List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help> List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe> Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org> X-Spam-Score: -1.0 (-) Hi, On mar., 14 f=C3=A9vr. 2023 at 07:33, "\( via Guix-patches" via <guix-patch= es@HIDDEN> wrote: > +# Yes, No, Maybe So > + > +Let's instead implement another M of functional programming, _`maybe`_ v= alues, > +representing a value that may or may not exist. For instance, there cou= ld be a > +procedure that attempts to pop a stack, returing the result if there is = one, or --^ Typo s/returing/returning Cheers, simon
guix-patches@HIDDEN
:bug#61214
; Package guix-patches
.
Full text available.Received: (at submit) by debbugs.gnu.org; 14 Feb 2023 19:25:31 +0000 From debbugs-submit-bounces <at> debbugs.gnu.org Tue Feb 14 14:25:31 2023 Received: from localhost ([127.0.0.1]:57022 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>) id 1pS0vj-0002US-AF for submit <at> debbugs.gnu.org; Tue, 14 Feb 2023 14:25:31 -0500 Received: from lists.gnu.org ([209.51.188.17]:34448) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <zimon.toutoune@HIDDEN>) id 1pS0vh-0002UF-LM for submit <at> debbugs.gnu.org; Tue, 14 Feb 2023 14:25:29 -0500 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 <zimon.toutoune@HIDDEN>) id 1pS0vW-0007Pu-8z for guix-patches@HIDDEN; Tue, 14 Feb 2023 14:25:18 -0500 Received: from mail-wm1-x336.google.com ([2a00:1450:4864:20::336]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from <zimon.toutoune@HIDDEN>) id 1pS0vR-0005Qm-Vp; Tue, 14 Feb 2023 14:25:15 -0500 Received: by mail-wm1-x336.google.com with SMTP id f23-20020a05600c491700b003dff4480a17so1343211wmp.1; Tue, 14 Feb 2023 11:25:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:references :in-reply-to:subject:cc:to:from:from:to:cc:subject:date:message-id :reply-to; bh=iWPPOrB+O1DXLPYTjoyHqD8kpz87pfuqooE6g0WGsCY=; b=lhbJ7PoNEfpIHDLJMG6ChSkX+6lgM/J3ylTjohH2wAJ4JHRY2uQWPx9GkhseUgqzhZ 6PvgtwdghrtAJ6CM1Lkdt5F96YxxS049EM3uzL+CWwNiE0XZNawjgDovujAcPYl9f5eC 9ocqUJmuI5rXxeweIc1vn1KQGwfz6RhJM4k6RNCwzIXFUDN7StLchDSKTZqR2D1DkJfP YnSteGgd7sfayt9hPVX9IAQbvyAnEyYn0V7h8cC6BtjD3QK8//S+dsfXiHdL7C4j5S/2 4lmtj323uqSgYRc9NGa54BW+s5Eq4AYkvZAfctQMwODqUOHfAxl8L9MaudYqODK7u18x FB+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:references :in-reply-to:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=iWPPOrB+O1DXLPYTjoyHqD8kpz87pfuqooE6g0WGsCY=; b=rcMKypIBa9xIoV6jvBbX12m8aHM97oX3VuCTHcfSmAgQOtb/gS1TGSCnCAwY5XUgo+ +7avhXaUxVj+MLiT1ElgdR9ueNUC1EYLvjsAZl2Cro9PXGwvSwMUQhBwcMUwtAgzs1HE +VmEeBcSgxcZPLqgZFKSOV/md5npXmA992c5IZlS3jT5HVjiDPn7pTbuKDewX+QiPqGB VyGppwzY25M39B2NoZcRkMqDQUvU3Vvn7Z3dDoDMAs5k7bSsND9eUR0BKszIZuYhnONI zss1QwdUKTSfcwOrTrGcbZpb0UPcPNwwFhmil4isGwVhdFs4xcbpFUXQ1avDnj0Uaj9y vY9g== X-Gm-Message-State: AO0yUKXrYzpa94ZivaOraaJbapcZ6Z2RG/4XbNXI9AKdnDzG3TBwJLtc XWeO5o2t0c+YxdEJVhbXISU= X-Google-Smtp-Source: AK7set9bnnVTJX5bA7L/cJQ1qXbnB/5GYdwiEkeFw9TvavMnpDt+Xlj7TOCrxOejpg0NgnUI4a0QZQ== X-Received: by 2002:a05:600c:3caa:b0:3dc:5ae4:c13d with SMTP id bg42-20020a05600c3caa00b003dc5ae4c13dmr25058wmb.4.1676402709520; Tue, 14 Feb 2023 11:25:09 -0800 (PST) Received: from pfiuh07 ([193.48.40.241]) by smtp.gmail.com with ESMTPSA id t15-20020a05600c198f00b003ddc781b1d4sm19410611wmq.33.2023.02.14.11.25.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Feb 2023 11:25:09 -0800 (PST) From: Simon Tournier <zimon.toutoune@HIDDEN> To: "( via Guix-patches via" <guix-patches@HIDDEN>, 61214 <at> debbugs.gnu.org Subject: Re: [bug#61214] [PATCH guix-artwork v5] website: posts: Add Dissecting Guix, Part 2: The Store Monad. In-Reply-To: <20230214073314.2651-1-paren@HIDDEN> References: <20230201172821.3072-1-paren@HIDDEN> <20230214073314.2651-1-paren@HIDDEN> Date: Tue, 14 Feb 2023 19:01:46 +0100 Message-ID: <87mt5gyvlh.fsf@HIDDEN> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Received-SPF: pass client-ip=2a00:1450:4864:20::336; envelope-from=zimon.toutoune@HIDDEN; helo=mail-wm1-x336.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Spam-Score: -1.3 (-) X-Debbugs-Envelope-To: submit Cc: "\(" <paren@HIDDEN>, ludo@HIDDEN, mail@HIDDEN, zimoun.toutoune@HIDDEN X-BeenThere: debbugs-submit <at> debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: <debbugs-submit.debbugs.gnu.org> List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe> List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/> List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org> List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help> List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe> Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org> X-Spam-Score: -2.3 (--) Hi, On mar., 14 f=C3=A9vr. 2023 at 07:33, "\( via Guix-patches" via <guix-patch= es@HIDDEN> wrote: > +# Yes, No, Maybe So > + > +Let's instead implement another M of functional programming, _`maybe`_ v= alues, > +representing a value that may or may not exist. For instance, there cou= ld be a > +procedure that attempts to pop a stack, returing the result if there is = one, or --^ Typo s/returing/returning Cheers, simon
guix-patches@HIDDEN
:bug#61214
; Package guix-patches
.
Full text available.Received: (at 61214) by debbugs.gnu.org; 14 Feb 2023 07:33:22 +0000 From debbugs-submit-bounces <at> debbugs.gnu.org Tue Feb 14 02:33:22 2023 Received: from localhost ([127.0.0.1]:52383 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>) id 1pRpoX-000587-DW for submit <at> debbugs.gnu.org; Tue, 14 Feb 2023 02:33:22 -0500 Received: from knopi.disroot.org ([178.21.23.139]:41894) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <paren@HIDDEN>) id 1pRpoU-00057y-Lg for 61214 <at> debbugs.gnu.org; Tue, 14 Feb 2023 02:33:20 -0500 Received: from localhost (localhost [127.0.0.1]) by disroot.org (Postfix) with ESMTP id 07C6241418; Tue, 14 Feb 2023 08:33:18 +0100 (CET) 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 fCGDs_iP1WQp; Tue, 14 Feb 2023 08:33:15 +0100 (CET) From: "(" <paren@HIDDEN> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=disroot.org; s=mail; t=1676359995; bh=UuvrXN6TQHN26F0BxgbM5Kng7G/8TEItJb8hCtw2cu8=; h=From:To:Cc:Subject:Date; b=KPEmLXz81sQEz+kBqG3vASKetmimefbufkbpsoNF+UY4kn8QYIBXUwD9Yu8uUGiH+ YDoZXdVBualQE/hbSwxYtBX67Q0GlXTBTnklbEHXDeyLutseKOwE9y30JjEzBL10Ql +de/qsP9pvXtswvxLfYMEcLjj6IZ5LHcg+tFPENt5tZ/gdkziKK2YUfKk3u/8xf3o4 89AjgTDzNK/xiwRUg6MqMINgJsc7nuEvyLVJaIaOGe9zmP+VBAX6HrSnB8Mi4bxYDQ 8UMsJIp8kkZkcfwLcAVUdMwhsZMbvFacRlHS22OV19hSOMnpEQ6JC7YnemnuZm4elD 6eNeAXg2Lc4yw== To: 61214 <at> debbugs.gnu.org Subject: [PATCH guix-artwork v5] website: posts: Add Dissecting Guix, Part 2: The Store Monad. Date: Tue, 14 Feb 2023 07:33:14 +0000 Message-Id: <20230214073314.2651-1-paren@HIDDEN> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Score: 0.0 (/) X-Debbugs-Envelope-To: 61214 Cc: "\(" <paren@HIDDEN>, ludo@HIDDEN, mail@HIDDEN, zimoun.toutoune@HIDDEN X-BeenThere: debbugs-submit <at> debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: <debbugs-submit.debbugs.gnu.org> List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe> List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/> List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org> List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help> List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe> Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org> X-Spam-Score: -1.0 (-) * website/posts/dissecting-guix-2-store-monad.md: New blog post. --- Oops, forgot to ``git commit -a''... :/ See v4 for the changelog. .../posts/dissecting-guix-2-store-monad.md | 567 ++++++++++++++++++ 1 file changed, 567 insertions(+) create mode 100644 website/posts/dissecting-guix-2-store-monad.md diff --git a/website/posts/dissecting-guix-2-store-monad.md b/website/posts/dissecting-guix-2-store-monad.md new file mode 100644 index 0000000..13b9cbb --- /dev/null +++ b/website/posts/dissecting-guix-2-store-monad.md @@ -0,0 +1,567 @@ +title: Dissecting Guix, Part 2: The Store Monad +date: TBC +author: ( +tags: Dissecting Guix, Functional package management, Programming interfaces, Scheme API +--- +Hello again! + +In [the last post](https://guix.gnu.org/en/blog/2023/dissecting-guix-part-1-derivations/), +we briefly mentioned the `with-store` and `run-with-store` macros. Today, we'll +be looking at those in further detail, along with the related monad library and +the [`%store-monad`](https://guix.gnu.org/manual/devel/en/html_node/The-Store-Monad.html)! + +Typically, we use monads to chain operations together, and the `%store-monad` is +no different; it's used to combine operations that work on the Guix store (for +instance, creating derivations, building derivations, or adding data files to +the store). + +However, monads are a little hard to explain, and from a distance, they seem to +be quite incomprehensible. So, I want you to erase them from your mind for now. +We'll come back to them later. And be aware that if you can't seem to get your +head around them, it's okay; you can understand most of the architecture of Guix +without understanding monads. + +# Yes, No, Maybe So + +Let's instead implement another M of functional programming, _`maybe`_ values, +representing a value that may or may not exist. For instance, there could be a +procedure that attempts to pop a stack, returing the result if there is one, or +`nothing` if the stack has no elements. + +`maybe` is a very common feature of statically-typed functional languages, and +you'll see it all over the place in Haskell and OCaml code. However, Guile is +dynamically typed, so we usually use ad-hoc `#f` values as the "null value" +instead of a proper "nothing" or "none". + +Just for fun, though, we'll implement a proper `maybe` in Guile. Fire up that +REPL once again, and let's import a bunch of modules that we'll need: + +```scheme +(use-modules (ice-9 match) + (srfi srfi-9)) +``` + +We'll implement `maybe` as a record with two fields, `is?` and `value`. If the +value contains something, `is?` will be `#t` and `value` will contain the thing +in question, and if it's empty, `is?`'ll be `#f`. + +```scheme +(define-record-type <maybe> + (make-maybe is? value) + maybe? + (is? maybe-is?) + (value maybe-value)) +``` + +Now we'll define constructors for the two possible states: + +```scheme +(define (something value) + (make-maybe #t value)) + +(define (nothing) + (make-maybe #f #f)) ;the value here doesn't matter; we'll just use #f +``` + +And make some silly functions that return optional values: + +```scheme +(define (remove-a str) + (if (eq? (string-ref str 0) #\a) + (something (substring str 1)) + (nothing))) + +(define (remove-b str) + (if (eq? (string-ref str 0) #\b) + (something (substring str 1)) + (nothing))) + +(remove-a "ahh") +⇒ #<<maybe> is?: #t value: "hh"> + +(remove-a "ooh") +⇒ #<<maybe> is?: #f value: #f> + +(remove-b "bad") +⇒ #<<maybe> is?: #t value: "ad"> +``` + +But what if we want to compose the results of these functions? + +# Keeping Your Composure + +As you might have guessed, this is not fun. Cosplaying as a compiler backend +typically isn't. + +```scheme +(let ((t1 (remove-a "abcd"))) + (if (maybe-is? t1) + (remove-b (maybe-value t1)) + (nothing))) +⇒ #<<maybe> is?: #t value: "cd"> + +(let ((t1 (remove-a "bbcd"))) + (if (maybe-is? t1) + (remove-b (maybe-value t1)) + (nothing))) +⇒ #<<maybe> is?: #f value: #f> +``` + +I can almost hear the heckling. Even worse, composing three: + +```scheme +(let* ((t1 (remove-a "abad")) + (t2 (if (maybe-is? t1) + (remove-b (maybe-value t1)) + (nothing)))) + (if (maybe-is? t2) + (remove-a (maybe-value t2)) + (nothing))) +⇒ #<<maybe> is?: #t value: "d"> +``` + +So, how do we go about making this more bearable? Well, one way could be to +make `remove-a` and `remove-b` accept `maybe`s: + +```scheme +(define (remove-a ?str) + (match ?str + (($ <maybe> #t str) + (if (eq? (string-ref str 0) #\a) + (something (substring str 1)) + (nothing))) + (_ (nothing)))) + +(define (remove-b ?str) + (match ?str + (($ <maybe> #t str) + (if (eq? (string-ref str 0) #\b) + (something (substring str 1)) + (nothing))) + (_ (nothing)))) +``` + +Not at all pretty, but it works! + +```scheme +(remove-b (remove-a (something "abc"))) +⇒ #<<maybe> is?: #t value: "c"> +``` + +Still, our procedures now require quite a bit of boilerplate. Might there be a +better way? + +# The Ties That `>>=` Us + +First of all, we'll revert to our original definitions of `remove-a` and +`remove-b`, that is to say, the ones that take a regular value and return a +`maybe`. + +```scheme +(define (remove-a str) + (if (eq? (string-ref str 0) #\a) + (something (substring str 1)) + (nothing))) + +(define (remove-b str) + (if (eq? (string-ref str 0) #\b) + (something (substring str 1)) + (nothing))) +``` + +What if tried introducing higher-order procedures (procedures that accept other +procedures as arguments) into the equation? Because we're functional +programmers and we have an unhealthy obsession with that sort of thing. + +```scheme +(define (maybe-chain maybe proc) + (if (maybe-is? maybe) + (proc (maybe-value maybe)) + (nothing))) + +(maybe-chain (something "abc") + remove-a) +⇒ #<<maybe> is?: #t value: "bc"> + +(maybe-chain (nothing) + remove-a) +⇒ #<<maybe> is?: #f value: #f> +``` + +It lives! To make it easier to compose procedures like this, we'll define a +macro that allows us to perform any number of sequenced operations with only one +composition form: + +```scheme +(define-syntax maybe-chain* + (syntax-rules () + ((_ maybe proc) + (maybe-chain maybe proc)) + ((_ maybe proc rest ...) + (maybe-chain* (maybe-chain maybe proc) + rest ...)))) + +(maybe-chain* (something "abad") + remove-a + remove-b + remove-a) +⇒ #<<maybe> is?: #t value: "d"> +``` + +Congratulations, you've just implemented the `bind` operation, commonly written +as `>>=`, for our `maybe` type. And it turns out that a monad is just any +container-like value for which `>>=` (along with another procedure called +`return`, which wraps a given value in the simplest possible form of a monad) +has been implemented. + +A more formal definition would be that a monad is a mathematical object composed +of three parts: a type, a `bind` function, and a `return` function. So, how do +monads relate to Guix? + +# New Wheel, Old Wheel + +Now that we've reinvented the wheel, we'd better learn to use the original +wheel. Guix provides a generic, high-level monads library, along with the two +generic monads `%identity-monad` and `%state-monad`, and the Guix-specific +`%store-monad`. Since `maybe` is not one of them, let's integrate our version +into the Guix monad system! + +First we'll import the module that provides the aforementioned library: + +```scheme +(use-modules (guix monads)) +``` + +To define a monad's behaviour in Guix, we simply use the `define-monad` macro, +and provide two procedures: `bind`, and `return`. + +```scheme +(define-monad %maybe-monad + (bind maybe-chain) + (return something)) +``` + +`bind` is just the procedure that we use to compose monadic procedure calls +together, and `return` is the procedure that wraps values in the most basic form +of the monad. A properly implemented `bind` and `return` must follow the +so-called _monad laws_: + +1. `(bind (return x) proc)` must be equivalent to `(proc x)`. +2. `(bind monad return)` must be equivalent to just `monad`. +3. `(bind (bind monad proc-1) proc-2)` must be equivalent to + `(bind monad (lambda (x) (bind (proc-1 x) proc-2)))`. + +Let's verify that our `maybe-chain` and `something` procedures adhere to the +monad laws: + +```scheme +(define (mlaws-proc-1 x) + (something (+ x 1))) + +(define (mlaws-proc-2 x) + (something (+ x 2))) + +;; First law: the left identity. +(equal? (maybe-chain (something 0) + mlaws-proc-1) + (mlaws-proc-1 0)) +⇒ #t + +;; Second law: the right identity. +(equal? (maybe-chain (something 0) + something) + (something 0)) +⇒ #t + +;; Third law: associativity. +(equal? (maybe-chain (maybe-chain (something 0) + mlaws-proc-1) + mlaws-proc-2) + (maybe-chain (something 0) + (lambda (x) + (maybe-chain (mlaws-proc-1 x) + mlaws-proc-2)))) +⇒ #t +``` + +Now that we know they're valid, we can use the `with-monad` macro to tell Guix +to use these specific implementations of `bind` and `return`, and the `>>=` +macro to thread monads through procedure calls! + +```scheme +(with-monad %maybe-monad + (>>= (something "aabbc") + remove-a + remove-a + remove-b + remove-b)) +⇒ #<<maybe> is?: #t value: "c"> +``` + +We can also now use `return`: + +```scheme +(with-monad %maybe-monad + (return 32)) +⇒ #<<maybe> is?: #t value: 32> +``` + +But Guix provides many higher-level interfaces than `>>=` and `return`, as we +will see. There's `mbegin`, which evaluates monadic expressions without binding +them to symbols, returning the last one. This, however, isn't particularly +useful with our `%maybe-monad`, as it's only really usable if the monadic +operations within have side effects, just like the non-monadic `begin`. + +There's also `mlet` and `mlet*`, which _do_ bind the results of monadic +expressions to symbols, and are essentially equivalent to a chain of +`(>>= MEXPR (lambda (BINDING) ...))`: + +```scheme +;; This is equivalent... +(mlet* %maybe-monad ((str -> "abad") ;non-monadic binding uses the -> symbol + (str1 (remove-a str)) + (str2 (remove-b str))) + (remove-a str)) +⇒ #<<maybe> is?: #t value: "d"> + +;; ...to this: +(with-monad %maybe-monad + (>>= (return "abad") + (lambda (str) + (remove-a str)) + (lambda (str1) + (remove-b str)) + (lambda (str2) + (remove-a str)))) +``` + +Various abstractions over these two exist too, such as `mwhen` (a `when` plus an +`mbegin`), `munless` (an `unless` plus an `mbegin`), and `mparameterize` +(dynamically-scoped value rebinding, like `parameterize`, in a monadic context). +`lift` takes a procedure and a monad and creates a new procedure that returns +a monadic value. + +There are also interfaces for manipulating lists wrapped in monads; `listm` +creates such a list, `sequence` turns a list of monads into a list wrapped in a +monad, and the `anym`, `mapm`, and `foldm` procedures are like their non-monadic +equivalents, except that they return lists wrapped in monads. + +This is all well and good, you may be thinking, but why does Guix need a monad +library, anyway? The answer is technically that it doesn't. But building on +the monad API makes a lot of things much easier, and to learn why, we're going +to look at one of Guix's built-in monads. + +# In a State + +Guix implements a monad called `%state-monad`, and it works with single-argument +procedures returning two values. Behold: + +```scheme +(with-monad %state-monad + (return 33)) +⇒ #<procedure 21dc9a0 at <unknown port>:1106:22 (state)> +``` + +The `run-with-state` value turns this procedure into an actually useful value, +or, rather, two values: + +```scheme +(run-with-state (with-monad %state-monad (return 33)) + (list "foo" "bar" "baz")) +⇒ 33 +⇒ ("foo" "bar" "baz") +``` + +What can this actually do for us, though? Well, it gets interesting if we do +some `>>=`ing: + +```scheme +(define state-seq + (mlet* %state-monad ((number (return 33))) + (state-push number))) +result +⇒ #<procedure 7fcb6f466960 at <unknown port>:1484:24 (state)> + +(run-with-state state-seq (list 32)) +⇒ (32) +⇒ (33 32) + +(run-with-state state-seq (list 30 99)) +⇒ (30 99) +⇒ (33 30 99) +``` + +What is `state-push`? It's a monadic procedure for `%state-monad` that takes +whatever's currently in the first value (the primary value) and pushes it onto +the second value (the state value), which is assumed to be a list, returning the +old state value as the primary value and the new list as the state value. + +So, when we do `(run-with-state result (list 32))`, we're passing `(list 32)` as +the initial state value, and then the `>>=` form passes that and `33` to +`state-push`. What `%state-monad` allows us to do is thread together some +procedures that require some kind of state, while essentially pretending the +state value is stored globally, like you might do in, say, C, and then retrieve +both the final state and the result at the end! + +If you're a bit confused, don't worry. We'll write some of our own +`%state-monad`-based monadic procedures and hopefully all will become clear. +Consider, for instance, the +[Fibonacci sequence](https://en.wikipedia.org/wiki/Fibonacci_number), in which +each value is computed by adding the previous two. We could use the +`%state-monad` to compute Fibonacci numbers by storing the previous number as +the primary value and the number before that as the state value: + +```scheme +(define (fibonacci-thing value) + (lambda (state) + (values (+ value state) + value))) +``` + +Now we can feed our Fibonacci-generating procedure the first value using +`run-with-state` and the second using `return`: + +```scheme +(run-with-state + (mlet* %state-monad ((starting (return 1)) + (n1 (fibonacci-thing starting)) + (n2 (fibonacci-thing n1))) + (fibonacci-thing n2)) + 0) +⇒ 3 +⇒ 2 + +(run-with-state + (mlet* %state-monad ((starting (return 1)) + (n1 (fibonacci-thing starting)) + (n2 (fibonacci-thing n1)) + (n3 (fibonacci-thing n2)) + (n4 (fibonacci-thing n3)) + (n5 (fibonacci-thing n4))) + (fibonacci-thing n5)) + 0) +⇒ 13 +⇒ 8 +``` + +This is all very nifty, and possibly useful in general, but what does this have +to do with Guix? Well, many Guix store-based operations are meant to be used +in concert with yet another monad, called the `%store-monad`. But if we look at +`(guix store)`, where `%store-monad` is defined... + +```scheme +(define-alias %store-monad %state-monad) +(define-alias store-return state-return) +(define-alias store-bind state-bind) +``` + +It was all a shallow façade! All the "store monad" is is a special case of the +state monad, where a value representing the store is passed as the state value. + +# Lies, Damned Lies, and Abstractions + +We mentioned that, technically, we didn't need monads for Guix. Indeed, many +(now deprecated) procedures take a store value as the argument, such as +`build-expression->derivation`. However, monads are far more elegant and +simplify store code by quite a bit. + +`build-expression->derivation`, being deprecated, should never of course be +used. For one thing, it uses the "quoted build expression" style, rather than +G-expressions (we'll discuss gexps another time). The best way to create a +derivation from some basic build code is to use the new-fangled +`gexp->derivation` procedure: + +```scheme +(use-modules (guix gexp) + (gnu packages irc)) + +(define symlink-irssi + (gexp->derivation "link-to-irssi" + #~(symlink #$(file-append irssi "/bin/irssi") #$output))) +⇒ #<procedure 7fddcc7b81e0 at guix/gexp.scm:1180:2 (state)> +``` + +You don't have to understand the `#~(...)` form yet, only everything surrounding +it. We can see that this `gexp->derivation` returns a procedure taking the +initial state (store), just like our `%state-monad` procedures did, and like we +used `run-with-state` to pass the initial state to a `%state-monad` monadic +value, we use our old friend `run-with-store` when we have a `%store-monad` +monadic value! + +```scheme +(define symlink-irssi-drv + (with-store store + (run-with-store store + symlink-irssi))) +⇒ #<derivation /gnu/store/q7kwwl4z6psifnv4di1p1kpvlx06fmyq-link-to-irssi.drv => /gnu/store/6a94niigx4ii0ldjdy33wx9anhifr25x-link-to-irssi 7fddb7ef52d0> +``` + +Let's just check this derivation is as expected by reading the code from the +builder script. + +```scheme +(define symlink-irssi-builder + (list-ref (derivation-builder-arguments symlink-irssi-drv) 1)) + +(call-with-input-file symlink-irssi-builder + (lambda (port) + (read port))) + +⇒ (symlink + "/gnu/store/hrlmypx1lrdjlxpkqy88bfrzg5p0bn6d-irssi-1.4.3/bin/irssi" + ((@ (guile) getenv) "out")) +``` + +And indeed, it symlinks the `irssi` binary to the output path. Some other, +higher-level, monadic procedures include `interned-file`, which copies a file +from outside the store into it, and `text-file`, which copies some text into it. +Generally, these procedures aren't used, as there are higher-level procedures +that perform similar functions (which we will discuss later), but for the sake +of this blog post, here's an example: + +```scheme +(with-store store + (run-with-store store + (text-file "unmatched-paren" + "( <paren@HIDDEN>"))) +⇒ "/gnu/store/v6smacxvdk4yvaa3s3wmd54lixn1dp3y-unmatched-paren" +``` + +# Conclusion + +What have we learned about monads? The key points we can take away are: + +1. Monads are a way of composing together procedures and values that are wrapped + in containers that give them extra context, like `maybe` values. +2. Guix provides a high-level monad library that compensates for Guile's lack of + static typing or an interface-like system. +3. The `(guix monads)` module provides the state monad, which allows you to + thread state through procedures, allowing you to essentially pretend it's a + global variable that's modified by each procedure. +4. Guix uses the store monad frequently to thread a store connection through + procedures that need it. +5. The store monad is really just the state monad in disguise, where the state + value is used to thread the store object through monadic procedures. + +If you've read this post in its entirety but still don't yet quite get it, don't +worry. Try to modify and tinker about with the examples, and ask any questions +on the IRC channel `#guix:libera.chat` and mailing list at `help-guix@HIDDEN`, +and hopefully it will all click eventually! + +#### About GNU Guix + +[GNU Guix](https://guix.gnu.org) is a transactional package manager and +an advanced distribution of the GNU system that [respects user +freedom](https://www.gnu.org/distros/free-system-distribution-guidelines.html). +Guix can be used on top of any system running the Hurd or the Linux +kernel, or it can be used as a standalone operating system distribution +for i686, x86_64, ARMv7, AArch64 and POWER9 machines. + +In addition to standard package management features, Guix supports +transactional upgrades and roll-backs, unprivileged package management, +per-user profiles, and garbage collection. When used as a standalone +GNU/Linux distribution, Guix offers a declarative, stateless approach to +operating system configuration management. Guix is highly customizable +and hackable through [Guile](https://www.gnu.org/software/guile) +programming interfaces and extensions to the +[Scheme](http://schemers.org) language. base-commit: fe113595b6f7d8a1e1a0b814521f02783f9209c3 -- 2.39.1
guix-patches@HIDDEN
:bug#61214
; Package guix-patches
.
Full text available.Received: (at 61214) by debbugs.gnu.org; 14 Feb 2023 07:31:02 +0000 From debbugs-submit-bounces <at> debbugs.gnu.org Tue Feb 14 02:31:02 2023 Received: from localhost ([127.0.0.1]:52369 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>) id 1pRpmH-00053r-EB for submit <at> debbugs.gnu.org; Tue, 14 Feb 2023 02:31:02 -0500 Received: from knopi.disroot.org ([178.21.23.139]:47106) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <paren@HIDDEN>) id 1pRpmE-00053h-El for 61214 <at> debbugs.gnu.org; Tue, 14 Feb 2023 02:31:00 -0500 Received: from localhost (localhost [127.0.0.1]) by disroot.org (Postfix) with ESMTP id 1D8D641470; Tue, 14 Feb 2023 08:30:57 +0100 (CET) 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 VCD2-3CGn-Ye; Tue, 14 Feb 2023 08:30:54 +0100 (CET) From: "(" <paren@HIDDEN> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=disroot.org; s=mail; t=1676359854; bh=FYKkghAsH3rPLqo3oemu4/PTkA6QClfnfycowAt/A2Y=; h=From:To:Cc:Subject:Date; b=POO+vaUrDlRzcDX02Nyw2AdokEinED/sQb4YQjzVtwipCO3jVQvSlRe711gMLdmm0 FnSQnAYy0eaZdq8RHtGLNfQ17TjPDJGlko69Ev+U2HZ2LM4kdQN3wm3zSUdwGQf0Kt Es8LxZA6z0G+bGVsRdxLRwxeigwPGpa0HEJ5e30qkRvxKuTO0bZhoO7ycvyvHf5VWw T469G5BYqFgTSZlpMPTJC52apTuTDFC2nWglY4qUoGy1F9xnOqeytkPBerStQKeryl ptTGOT6BOU1VJk4LIfLAUXqwjdebCmzkwTcJTBPQT3SR61qcIlungb1EYI4mse8XQM Tlk5zQxWOIlMQ== To: 61214 <at> debbugs.gnu.org Subject: [PATCH guix-artwork v4] website: posts: Add Dissecting Guix, Part 2: The Store Monad. Date: Tue, 14 Feb 2023 07:30:49 +0000 Message-Id: <20230214073049.2126-1-paren@HIDDEN> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Score: 0.0 (/) X-Debbugs-Envelope-To: 61214 Cc: "\(" <paren@HIDDEN>, ludo@HIDDEN, mail@HIDDEN, zimoun.toutoune@HIDDEN X-BeenThere: debbugs-submit <at> debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: <debbugs-submit.debbugs.gnu.org> List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe> List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/> List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org> List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help> List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe> Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org> X-Spam-Score: -1.0 (-) * website/posts/dissecting-guix-2-store-monad.md: New blog post. --- Heya, Addressing criticism from Chris and Ludo: * Don't use 'API' ad nauseum. * Use ⇒ instead of ;; for return values. * Provide an example of where 'maybe' could be useful when introducing it. * Say 'statically-typed' rather than 'strongly-typed'. * Add high-level explanation of the purpose of monads to the introduction. * Don't say that we sometimes use '() as a 'nothing' value. * Talk about 'the so-called monads laws' rather than 'these laws'. * Link to the 'The Store Monad' section of the manual in the introduction. * Remove the MBEGIN example, as it's essentially pointless. Explain how it's only useful if the operations have side effects. * Don't say we ignore the state; instead, we basically pretend it's a global variable. * Just say that monads are more elegant rather than more pure. * Note that you can ask any questions on IRC or the mailing list :) .../posts/dissecting-guix-2-store-monad.md | 557 ++++++++++++++++++ 1 file changed, 557 insertions(+) create mode 100644 website/posts/dissecting-guix-2-store-monad.md diff --git a/website/posts/dissecting-guix-2-store-monad.md b/website/posts/dissecting-guix-2-store-monad.md new file mode 100644 index 0000000..a27a28b --- /dev/null +++ b/website/posts/dissecting-guix-2-store-monad.md @@ -0,0 +1,557 @@ +title: Dissecting Guix, Part 2: The Store Monad +date: TBC +author: ( +tags: Dissecting Guix, Functional package management, Programming interfaces, Scheme API +--- +Hello again! + +In [the last post](https://guix.gnu.org/en/blog/2023/dissecting-guix-part-1-derivations/), +we briefly mentioned the `with-store` and `run-with-store` APIs. Today, we'll +be looking at those in further detail, along with the related monad API and the +`%store-monad`! + +Monads are a little hard to explain, and from a distance, they seem more than a +bit confusing. So, I want you to erase monads from your mind for now. We'll +come back to them later. + +# Yes, No, Maybe So + +Let's instead implement another M of functional programming, _`maybe`_ values, +representing a value that may or may not exist. `maybe` is a very common +feature of strongly-typed functional languages, and you'll see it all over the +place in Haskell and OCaml code. However, Guile is dynamically typed, so we +usually use ad-hoc `#f`s and `'()`s for null values instead of a proper +"optional" value. + +Just for fun, though, we'll implement a proper `maybe` in Guile. Fire up that +REPL once again, and let's import a bunch of modules that we'll need: + +```scheme +(use-modules (ice-9 match) + (srfi srfi-9)) +``` + +We'll implement `maybe` as a record with two fields, `is?` and `value`. If the +value contains something, `is?` will be `#t` and `value` will contain the thing +in question, and if it's empty, `is?`'ll be `#f`. + +```scheme +(define-record-type <maybe> + (make-maybe is? value) + maybe? + (is? maybe-is?) + (value maybe-value)) +``` + +Now we'll define constructors for the two possible states: + +```scheme +(define (something value) + (make-maybe #t value)) + +(define (nothing) + (make-maybe #f #f)) ;the value here doesn't matter; we'll just use #f +``` + +And make some silly functions that return optional values: + +```scheme +(define (remove-a str) + (if (eq? (string-ref str 0) #\a) + (something (substring str 1)) + (nothing))) + +(define (remove-b str) + (if (eq? (string-ref str 0) #\b) + (something (substring str 1)) + (nothing))) + +(remove-a "ahh") +;; #<<maybe> is?: #t value: "hh"> + +(remove-a "ooh") +;; #<<maybe> is?: #f value: #f> + +(remove-b "bad") +;; #<<maybe> is?: #t value: "ad"> +``` + +But what if we want to compose the results of these functions? + +# Keeping Your Composure + +As you might have guessed, this is not fun. Cosplaying as a compiler backend +typically isn't. + +```scheme +(let ((t1 (remove-a "abcd"))) + (if (maybe-is? t1) + (remove-b (maybe-value t1)) + (nothing))) +;; #<<maybe> is?: #t value: "cd"> + +(let ((t1 (remove-a "bbcd"))) + (if (maybe-is? t1) + (remove-b (maybe-value t1)) + (nothing))) +;; #<<maybe> is?: #f value: #f> +``` + +I can almost hear the heckling. Even worse, composing three: + +```scheme +(let* ((t1 (remove-a "abad")) + (t2 (if (maybe-is? t1) + (remove-b (maybe-value t1)) + (nothing)))) + (if (maybe-is? t2) + (remove-a (maybe-value t2)) + (nothing))) +;; #<<maybe> is?: #t value: "d"> +``` + +So, how do we go about making this more bearable? Well, one way could be to +make `remove-a` and `remove-b` accept `maybe`s: + +```scheme +(define (remove-a ?str) + (match ?str + (($ <maybe> #t str) + (if (eq? (string-ref str 0) #\a) + (something (substring str 1)) + (nothing))) + (_ (nothing)))) + +(define (remove-b ?str) + (match ?str + (($ <maybe> #t str) + (if (eq? (string-ref str 0) #\b) + (something (substring str 1)) + (nothing))) + (_ (nothing)))) +``` + +Not at all pretty, but it works! + +``` +(remove-b (remove-a (something "abc"))) +;; #<<maybe> is?: #t value: "c"> +``` + +Still, our procedures now require quite a bit of boilerplate. Might there be a +better way? + +# The Ties That `>>=` Us + +First of all, we'll revert to our original definitions of `remove-a` and +`remove-b`, that is to say, the ones that take a regular value and return a +`maybe`. + +```scheme +(define (remove-a str) + (if (eq? (string-ref str 0) #\a) + (something (substring str 1)) + (nothing))) + +(define (remove-b str) + (if (eq? (string-ref str 0) #\b) + (something (substring str 1)) + (nothing))) +``` + +What if tried introducing higher-order procedures (procedures that accept other +procedures as arguments) into the equation? Because we're functional +programmers and we have an unhealthy obsession with that sort of thing. + +```scheme +(define (maybe-chain maybe proc) + (if (maybe-is? maybe) + (proc (maybe-value maybe)) + (nothing))) + +(maybe-chain (something "abc") + remove-a) +;; #<<maybe> is?: #t value: "bc"> + +(maybe-chain (nothing) + remove-a) +;; #<<maybe> is?: #f value: #f> +``` + +It lives! To make it easier to compose procedures like this, we'll define a +macro that allows us to perform any number of sequenced operations with only one +composition form: + +```scheme +(define-syntax maybe-chain* + (syntax-rules () + ((_ maybe proc) + (maybe-chain maybe proc)) + ((_ maybe proc rest ...) + (maybe-chain* (maybe-chain maybe proc) + rest ...)))) + +(maybe-chain* (something "abad") + remove-a + remove-b + remove-a) +;; #<<maybe> is?: #t value: "d"> +``` + +Congratulations, you've just implemented the `bind` operation, commonly written +as `>>=`, for our `maybe` type. And it turns out that a monad is just any +container-like value for which `>>=` (along with another procedure called +`return`, which wraps a given value in the simplest possible form of a monad) +has been implemented. + +A more formal definition would be that a monad is a mathematical object composed +of three parts: a type, a `bind` function, and a `return` function. So, how do +monads relate to Guix? + +# New Wheel, Old Wheel + +Now that we've reinvented the wheel, we'd better learn to use the original +wheel. Guix provides a generic, high-level monads API, along with the two +generic monads `%identity-monad` and `%state-monad`, and the Guix-specific +`%store-monad`. Since `maybe` is not one of them, let's integrate our version +into the Guix monad system! + +First we'll make the API available: + +```scheme +(use-modules (guix monads)) +``` + +To define a monad's API in Guix, we simply use the `define-monad` macro, and +provide two procedures: `bind`, and `return`. + +```scheme +(define-monad %maybe-monad + (bind maybe-chain) + (return something)) +``` + +`bind` is just the procedure that we use to compose monadic procedure calls +together, and `return` is the procedure that wraps values in the most basic form +of the monad. A properly implemented `bind` and `return` must follow these +laws: + +1. `(bind (return x) proc)` must be equivalent to `(proc x)`. +2. `(bind monad return)` must be equivalent to just `monad`. +3. `(bind (bind monad proc-1) proc-2)` must be equivalent to + `(bind monad (lambda (x) (bind (proc-1 x) proc-2)))`. + +Let's verify that our `maybe-chain` and `something` procedures adhere to the +monad laws: + +```scheme +(define (mlaws-proc-1 x) + (something (+ x 1))) + +(define (mlaws-proc-2 x) + (something (+ x 2))) + +;; First law: the left identity. +(equal? (maybe-chain (something 0) + mlaws-proc-1) + (mlaws-proc-1 0)) +;; #t + +;; Second law: the right identity. +(equal? (maybe-chain (something 0) + something) + (something 0)) +;; #t + +;; Third law: associativity. +(equal? (maybe-chain (maybe-chain (something 0) + mlaws-proc-1) + mlaws-proc-2) + (maybe-chain (something 0) + (lambda (x) + (maybe-chain (mlaws-proc-1 x) + mlaws-proc-2)))) +;; #t +``` + +Now that we know they're valid, we can use the `with-monad` macro to tell Guix +to use these specific implementations of `bind` and `return`, and the `>>=` +macro to thread monads through procedure calls! + +```scheme +(with-monad %maybe-monad + (>>= (something "aabbc") + remove-a + remove-a + remove-b + remove-b)) +;; #<<maybe> is?: #t value: "c"> +``` + +We can also now use `return`: + +```scheme +(with-monad %maybe-monad + (return 32)) +;; #<<maybe> is?: #t value: 32> +``` + +But Guix provides many higher-level APIs than `>>=` and `return`, as we will +see. There's `mbegin`, which evaluates monadic expressions without binding them +to symbols, returning the last one: + +```scheme +(mbegin %maybe-monad + (remove-a "abc")) +;; #<<maybe> is?: #t value: "bc"> +``` + +And there's `mlet` and `mlet*`, which can bind them, and are essentially +equivalent to a chain of `(>>= MEXPR (lambda (BINDING) ...))`: + +```scheme +;; This is equivalent... +(mlet* %maybe-monad ((str -> "abad") ;non-monadic binding uses the -> symbol + (str1 (remove-a str)) + (str2 (remove-b str))) + (remove-a str)) +;; #<<maybe> is?: #t value: "d"> + +;; ...to this: +(with-monad %maybe-monad + (>>= (return "abad") + (lambda (str) + (remove-a str)) + (lambda (str1) + (remove-b str)) + (lambda (str2) + (remove-a str)))) +``` + +Various abstractions over these two exist too, such as `mwhen` (a `when` plus an +`mbegin`), `munless` (an `unless` plus an `mbegin`), and `mparameterize` +(dynamically-scoped value rebinding, like `parameterize`, in a monadic context). +`lift` takes a procedure and a monad and creates a new procedure that returns +a monadic value. + +There are also APIs for manipulating lists wrapped in monads; `listm` creates +such a list, `sequence` turns a list of monads into a list wrapped in a monad, +and the `anym`, `mapm`, and `foldm` procedures are like their non-monadic +equivalents, except that they return lists wrapped in monads. + +This is all well and good, you may be thinking, but why does Guix need a monad +API? The answer is technically that it doesn't. But building on the monad API +makes a lot of things much easier, and to learn why, we're going to look at one +of Guix's built-in monads. + +# In a State + +Guix implements a monad called `%state-monad`, and it works with single-argument +procedures returning two values. Behold: + +```scheme +(with-monad %state-monad + (return 33)) +;; #<procedure 21dc9a0 at <unknown port>:1106:22 (state)> +``` + +The `run-with-state` value turns this procedure into an actually useful value, +or, rather, two values: + +```scheme +(run-with-state (with-monad %state-monad (return 33)) + (list "foo" "bar" "baz")) +;; 33 +;; ("foo" "bar" "baz") +``` + +What can this actually do for us, though? Well, it gets interesting if we do +some `>>=`ing: + +```scheme +(define state-seq + (mlet* %state-monad ((number (return 33))) + (state-push number))) +result +;; #<procedure 7fcb6f466960 at <unknown port>:1484:24 (state)> + +(run-with-state state-seq (list 32)) +;; (32) +;; (33 32) + +(run-with-state state-seq (list 30 99)) +;; (30 99) +;; (33 30 99) +``` + +What is `state-push`? It's a monadic procedure for `%state-monad` that takes +whatever's currently in the first value (the primary value) and pushes it onto +the second value (the state value), which is assumed to be a list, returning the +old state value as the primary value and the new list as the state value. + +So, when we do `(run-with-state result (list 32))`, we're passing `(list 32)` as +the initial state value, and then the `>>=` form passes that and `33` to +`state-push`. What `%state-monad` allows us to do is thread together some +procedures that require some kind of state, while pretending the state isn't +there, and then retrieve both the final state and the result at the end! + +If you're a bit confused, don't worry. We'll write some of our own +`%state-monad`-based monadic procedures and hopefully all will become clear. +Consider, for instance, the +[Fibonacci sequence](https://en.wikipedia.org/wiki/Fibonacci_number), in which +each value is computed by adding the previous two. We could use the +`%state-monad` to compute Fibonacci numbers by storing the previous number as +the primary value and the number before that as the state value: + +```scheme +(define (fibonacci-thing value) + (lambda (state) + (values (+ value state) + value))) +``` + +Now we can feed our Fibonacci-generating procedure the first value using +`run-with-state` and the second using `return`: + +```scheme +(run-with-state + (mlet* %state-monad ((starting (return 1)) + (n1 (fibonacci-thing starting)) + (n2 (fibonacci-thing n1))) + (fibonacci-thing n2)) + 0) +;; 3 +;; 2 + +(run-with-state + (mlet* %state-monad ((starting (return 1)) + (n1 (fibonacci-thing starting)) + (n2 (fibonacci-thing n1)) + (n3 (fibonacci-thing n2)) + (n4 (fibonacci-thing n3)) + (n5 (fibonacci-thing n4))) + (fibonacci-thing n5)) + 0) +;; 13 +;; 8 +``` + +This is all very nifty, and possibly useful in general, but what does this have +to do with Guix? Well, many Guix store-based operations are meant to be used +in concert with yet another monad, called the `%store-monad`. But if we look at +`(guix store)`, where `%store-monad` is defined... + +```scheme +(define-alias %store-monad %state-monad) +(define-alias store-return state-return) +(define-alias store-bind state-bind) +``` + +It was all a shallow façade! All the "store monad" is is a special case of the +state monad, where a value representing the store is passed as the state value. + +# Lies, Damned Lies, and Abstractions + +We mentioned that, technically, we didn't need monads for Guix. Indeed, many +(now deprecated) procedures take a store value as the argument, such as +`build-expression->derivation`. However, using monads both helps ensure purity +and simply looks nicer. + +`build-expression->derivation`, being deprecated, should never of course be +used. For one thing, it uses the "quoted build expression" style, rather than +G-expressions (we'll discuss gexps another time). The best way to create a +derivation from some basic build code is to use the new-fangled +`gexp->derivation` procedure: + +```scheme +(use-modules (guix gexp) + (gnu packages irc)) + +(define symlink-irssi + (gexp->derivation "link-to-irssi" + #~(symlink #$(file-append irssi "/bin/irssi") #$output))) +;; #<procedure 7fddcc7b81e0 at guix/gexp.scm:1180:2 (state)> +``` + +You don't have to understand the `#~(...)` form yet, only everything surrounding +it. We can see that this `gexp->derivation` returns a procedure taking the +initial state (store), just like our `%state-monad` procedures did, and like we +used `run-with-state` to pass the initial state to a `%state-monad` monadic +value, we use our old friend `run-with-store` when we have a `%store-monad` +monadic value! + +```scheme +(define symlink-irssi-drv + (with-store store + (run-with-store store + symlink-irssi))) +;; #<derivation /gnu/store/q7kwwl4z6psifnv4di1p1kpvlx06fmyq-link-to-irssi.drv => /gnu/store/6a94niigx4ii0ldjdy33wx9anhifr25x-link-to-irssi 7fddb7ef52d0> +``` + +Let's just check this derivation is as expected by reading the code from the +builder script. + +```scheme +(define symlink-irssi-builder + (list-ref (derivation-builder-arguments symlink-irssi-drv) 1)) + +(call-with-input-file symlink-irssi-builder + (lambda (port) + (read port))) + +;; (symlink +;; "/gnu/store/hrlmypx1lrdjlxpkqy88bfrzg5p0bn6d-irssi-1.4.3/bin/irssi" +;; ((@ (guile) getenv) "out")) +``` + +And indeed, it symlinks the `irssi` binary to the output path. Some other, +higher-level, monadic procedures include `interned-file`, which copies a file +from outside the store into it, and `text-file`, which copies some text into it. +Generally, these procedures aren't used, as there are higher-level procedures +that perform similar functions (which we will discuss later), but for the sake +of this blog post, here's an example: + +```scheme +(with-store store + (run-with-store store + (text-file "unmatched-paren" + "( <paren@HIDDEN>"))) +;; "/gnu/store/v6smacxvdk4yvaa3s3wmd54lixn1dp3y-unmatched-paren" +``` + +# Conclusion + +What have we learned about monads? The key points we can take away are: + +1. Monads are a way of composing together procedures and values that are wrapped + in containers that give them extra context, like `maybe` values. +2. Guix provides a high-level monad API that compensates for Guile's lack of + strong types or an interface-like system. +3. This API provides the state monad, which allows you to thread state through + procedures such that you can pretend it doesn't exist. +4. Guix uses the store monad frequently to thread a store connection through + procedures that need it. +5. The store monad is really just the state monad in disguise, where the state + value is used to thread the store object through monadic procedures. + +If you've read this post in its entirety but still don't yet quite get it, don't +worry. Try to modify and tinker about with the examples, and hopefully it will +all click eventually! + +#### About GNU Guix + +[GNU Guix](https://guix.gnu.org) is a transactional package manager and +an advanced distribution of the GNU system that [respects user +freedom](https://www.gnu.org/distros/free-system-distribution-guidelines.html). +Guix can be used on top of any system running the Hurd or the Linux +kernel, or it can be used as a standalone operating system distribution +for i686, x86_64, ARMv7, AArch64 and POWER9 machines. + +In addition to standard package management features, Guix supports +transactional upgrades and roll-backs, unprivileged package management, +per-user profiles, and garbage collection. When used as a standalone +GNU/Linux distribution, Guix offers a declarative, stateless approach to +operating system configuration management. Guix is highly customizable +and hackable through [Guile](https://www.gnu.org/software/guile) +programming interfaces and extensions to the +[Scheme](http://schemers.org) language. base-commit: fe113595b6f7d8a1e1a0b814521f02783f9209c3 -- 2.39.1
guix-patches@HIDDEN
:bug#61214
; Package guix-patches
.
Full text available.Received: (at 61214) by debbugs.gnu.org; 13 Feb 2023 12:10:39 +0000 From debbugs-submit-bounces <at> debbugs.gnu.org Mon Feb 13 07:10:39 2023 Received: from localhost ([127.0.0.1]:47880 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>) id 1pRXfL-0005Of-7P for submit <at> debbugs.gnu.org; Mon, 13 Feb 2023 07:10:39 -0500 Received: from mail-wm1-f48.google.com ([209.85.128.48]:45931) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <zimon.toutoune@HIDDEN>) id 1pRXfF-0005OB-IN for 61214 <at> debbugs.gnu.org; Mon, 13 Feb 2023 07:10:34 -0500 Received: by mail-wm1-f48.google.com with SMTP id m16-20020a05600c3b1000b003dc4050c94aso8837599wms.4 for <61214 <at> debbugs.gnu.org>; Mon, 13 Feb 2023 04:10:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1676290228; h=content-transfer-encoding:mime-version:message-id:date:references :in-reply-to:subject:cc:to:from:from:to:cc:subject:date:message-id :reply-to; bh=+clLkHaBS/Ija0ehT6LubIwU1Mt5edzrPKdKYdTVJGY=; b=LyTDVaw9I4NFkwQu+HEaftll5wmJ8VUS5EJ8AT/tLf3EEyIflxsabVvvSMtMVICeL6 uPrhyB1bPRntDEgvqsfL5XLh84rTOZnUMzRNmU+NQ1yvvNzE11G+W9nzjalmxpYkoDhE UN1msY6n2fxi2Ny96MoWwzvTB/4514vFP+kVwrn6y7O9lL/WRP+xFNxXeSAIZtGYiZJQ lL6LYx3+jKnR3tZykxbaDsmE/1PtnPNOuNXlLOc/s8wZU843s4ySA28CBBQdUxSHviRC XikOqHKPVyHygTyVoubziEiJo4NKeuAR0KsRwAn5laxcDXHgAoptQSKiM6P05Oyu+jQj hL/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1676290228; h=content-transfer-encoding:mime-version:message-id:date:references :in-reply-to:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=+clLkHaBS/Ija0ehT6LubIwU1Mt5edzrPKdKYdTVJGY=; b=DkIWGpsSY1k6QNEYoc7Jsq99enur8ZqGiT5DM3uslUHmYOTy7bifQDmr4iGt38R6T8 lKJnwTrBboPQbn9Ny60Ahqsvq8CcCfUeMrX102libaDnpTtBQgqNrrzFvIHbUe4grn89 PSwwXU0TKovURq7dJfucqdYyFZHlp1WpRrdosyE4L4fQ8TIWts7URBeae+dPJTmQT/3q TFjaeS+ka6XFUHRcXaqAOC89L9660x7MyNGSGduejHesZUtoPWhKGDLO/oR8kslZ4BnV P5eSiWGjfRWIgzt84BKk4Pww4+V3mtXwLzMVqPnjH8ihuk3Kfz3YiQrZbGdEJCcie/l5 60Rw== X-Gm-Message-State: AO0yUKUnUPGavGsml41OoBbxcsFDzuXueBrol33Sy5uJY3+3bKifc6u1 UjYe9qKB698HvSqVyOzXxnRI6W7NuSA= X-Google-Smtp-Source: AK7set9vzhYXkcJ2ORtczvPrziWp8XpXhbH8NBr26MI5tJNmLXl5eVZSqZyEN6CKcpTfM4UKIAx3Gw== X-Received: by 2002:a05:600c:1d06:b0:3db:2063:425e with SMTP id l6-20020a05600c1d0600b003db2063425emr22883762wms.1.1676290227837; Mon, 13 Feb 2023 04:10:27 -0800 (PST) Received: from lili ([2a01:e0a:59b:9120:65d2:2476:f637:db1e]) by smtp.gmail.com with ESMTPSA id p14-20020a1c544e000000b003db06493ee7sm17727281wmi.47.2023.02.13.04.10.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Feb 2023 04:10:27 -0800 (PST) From: zimoun <zimon.toutoune@HIDDEN> To: Christopher Baines <mail@HIDDEN>, "(" <paren@HIDDEN> Subject: Re: [bug#61214] [PATCH guix-artwork v3] website: posts: Add Dissecting Guix, Part 2: The Store Monad. In-Reply-To: <875yc73xgu.fsf@HIDDEN> References: <20230201172821.3072-1-paren@HIDDEN> <20230203073624.2338-1-paren@HIDDEN> <875yc73xgu.fsf@HIDDEN> Date: Mon, 13 Feb 2023 13:08:33 +0100 Message-ID: <86v8k56a4e.fsf@HIDDEN> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Score: 0.0 (/) X-Debbugs-Envelope-To: 61214 Cc: 61214 <at> debbugs.gnu.org X-BeenThere: debbugs-submit <at> debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: <debbugs-submit.debbugs.gnu.org> List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe> List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/> List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org> List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help> List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe> Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org> X-Spam-Score: -1.0 (-) Hi Chris, On Sun, 12 Feb 2023 at 10:47, Christopher Baines <mail@HIDDEN> wrote: > I'd say that Guile is a strongly typed language. I'm also not sure what > the point about compensating for something lacking in Guile means. From my understanding, =E2=80=9Cstrongly typed=E2=80=9D is poorly defined a= nd there is no strong consensus. The Guile compiler accepts this, (define (bang) (+ 1 "0")) and typed language folks say it should not be possible for a decent compiler supporting =E2=80=9Cstrongly typed=E2=80=9D language. From my point of view, it is better to speak about dynamically typed and statically typed where definition is clearer. About the remark about =E2=80=9Ccompensating=E2=80=9C, I guess the point is= that using language with a powerful type system, this monad story is somehow included in the type system machinery; for instance Haskell. Since Guile does not have such type system =E2=80=93 but instead it has macros ;-= ) =E2=80=93 then the monad story needs an implementation for its own. (Aside, is monad another way to see macro? [1] :-)) Cheers, simon 1: <http://kawagner.blogspot.com/2007/02/understanding-monads-for-real.html>
guix-patches@HIDDEN
:bug#61214
; Package guix-patches
.
Full text available.Received: (at 61214) by debbugs.gnu.org; 12 Feb 2023 20:38:54 +0000 From debbugs-submit-bounces <at> debbugs.gnu.org Sun Feb 12 15:38:54 2023 Received: from localhost ([127.0.0.1]:46952 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>) id 1pRJ7e-0004JV-Bp for submit <at> debbugs.gnu.org; Sun, 12 Feb 2023 15:38:54 -0500 Received: from knopi.disroot.org ([178.21.23.139]:45874) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <paren@HIDDEN>) id 1pRJ7b-0004JL-RR for 61214 <at> debbugs.gnu.org; Sun, 12 Feb 2023 15:38:52 -0500 Received: from localhost (localhost [127.0.0.1]) by disroot.org (Postfix) with ESMTP id 5D645412C9; Sun, 12 Feb 2023 21:38:50 +0100 (CET) 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 cB2khLFbuez4; Sun, 12 Feb 2023 21:38:49 +0100 (CET) Content-Type: multipart/signed; boundary=59a84b46d5ac90f8d7eaf947d4b723e355bf88aa01ba826b613fc309f172; micalg=pgp-sha512; protocol="application/pgp-signature" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=disroot.org; s=mail; t=1676234329; bh=3Dgx3EuKJyv6/hE8/cX/B/Wh+kD04EqmBx93N0j/6mM=; h=Date:Cc:Subject:From:To:References:In-Reply-To; b=Y75yYnOgk+DSrnQmjlgsYM6busuqMLw/rxaYdIq19Pw6Drl+yAJSfcapR2DbOmzsf zOeGWTVyzuKUWIHrpK3yukdhkR1TIjJT3q+F56dAoGjJHt7yD8wljm0qO3xp1O5C8G 4MyHfysqunaP2Mz/N20WeXxpOQQKMTYloOAlswrF6hx2z7KLrmBzkQtIcRVF9eNGz0 n2RVQFrSMo7o1Y3dUy2x6nZ6A1/lRtF7oBinLFauZFIdVFUx2qD/Yj85VjTYtuIux9 rTkgfkaUcP7sf53Gy1hNan+2sjHTpttHeCVQUfr+nPkuqFvzGmo2ZBo56VdS8P+2vK P6VXfTxWEoU9g== Date: Sun, 12 Feb 2023 20:38:45 +0000 Message-Id: <CQGVH6E9HODC.145G6QF07H4IL@guix-framework> Subject: Re: bug#61214: [PATCH guix-artwork] website: posts: Add Dissecting Guix, Part 2: The Store Monad. From: "(" <paren@HIDDEN> To: =?utf-8?q?Ludovic_Court=C3=A8s?= <ludo@HIDDEN> References: <20230201172821.3072-1-paren@HIDDEN> <20230203073624.2338-1-paren@HIDDEN> <87h6vqlpxk.fsf_-_@HIDDEN> In-Reply-To: <87h6vqlpxk.fsf_-_@HIDDEN> X-Spam-Score: 0.1 (/) X-Debbugs-Envelope-To: 61214 Cc: 61214 <at> debbugs.gnu.org X-BeenThere: debbugs-submit <at> debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: <debbugs-submit.debbugs.gnu.org> List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe> List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/> List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org> List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help> List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe> Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org> X-Spam-Score: -0.9 (/) --59a84b46d5ac90f8d7eaf947d4b723e355bf88aa01ba826b613fc309f172 Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Heya, On Sun Feb 12, 2023 at 6:05 PM GMT, Ludovic Court=C3=A8s wrote: > Like Chris, I=E2=80=99m wary of acronyms (they can easily make things > impenetrable) so I=E2=80=99d write: > > - the `with-store` macro and the `run-with-store` procedure > - the related monad interface Wow, I really did use 'API' quite a lot... Oops :) > I agree with Chris=E2=80=99s comment: a few sentences of a higher-level i= ntro > showing where this fits in the big picture would be great! Okay, I'll try to figure something out for that. > It would be nice to stress, also, that one doesn=E2=80=99t _need_ to know= about > monads to use the various programming interfaces of Guix; instead, it=E2= =80=99s > a thing for someone who=E2=80=99d like to get a deep dive into the intern= als of > Guix. Fair enough :) > Maybe add something like =E2=80=9CFor example, one might write a function= that > divides two integers such that it returns special value `nothing` when > the divisor is zero, and `maybe 5` when passed `15` and `3`.=E2=80=9D (I > couldn=E2=80=99t think of a better example but you get the idea. :-)) Okay. > > +feature of strongly-typed functional languages, and you'll see it all = over the > > s/strongly/statically/ :-) Ahh, that's why everyone was pointing out the wording :) > In Scheme we use #f, not '(), to denote =E2=80=9CNothing=E2=80=9D. Mhm, not sure why I added '() in retrospect. > Maybe =E2=80=9CFirst, let=E2=80=99s import that module:=E2=80=9D? Too much 'API'... :) > =E2=80=9CTo define a monad in Guix, we use `define-monad`=E2=80=9D > > (In general, I=E2=80=99d avoid =E2=80=9Csimply=E2=80=9D because whether i= t=E2=80=99s =E2=80=9Csimple=E2=80=9D depends on > one=E2=80=99s background.) Ah, of course. I should've remembered this from last time :) > s/these laws/the so-called _monad laws_/ Good idea. > Should it be =E2=80=9C=E2=87=92 #t=E2=80=9D instead of =E2=80=9C;; #t=E2= =80=9D, to follow the convention used in > the manual and in other places? That's much nicer, yeah. > s/monad API/monad module/ (or =E2=80=9Cmonad library=E2=80=9D) (insert thumbs up emoji here) > > + strong types or an interface-like system. > > =E2=80=9Cstatic types=E2=80=9D? Yep. > s/This API/The `(guix monads)` module/ Okay. > Maybe link to the =E2=80=9CThe Store Monad=E2=80=9D section of the manual= here or > earlier? Oh, did I not link to it before, in the "Lies, Damned Lies, and Abstraction= s" section!? * unmatched-paren checks ...oops. > I feel we=E2=80=99re asking a lot of work from you, but hopefully the res= ult > will be even more pleasant. I guess v4 will be ready to go! Hopefully! :) > Thanks for all the work, And thanks for all your own work! :) -- ( --59a84b46d5ac90f8d7eaf947d4b723e355bf88aa01ba826b613fc309f172 Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQGzBAABCgAdFiEE6Vh10NblKE5doNlW7ImHg/nqI20FAmPpTlYACgkQ7ImHg/nq I22C8Av9GT/keTBesJLk5OY/eP/WouZRamqWTXQgX4Dp0sylbwaFP0UnP2QRpz4o bk5GLfnFx/FFTib4NyCePVKLO4sivsgRTLlSw6PONxCjEawIY23ZFYt0b4+0uKvt gzxvDRehv6QRtWZVmzVTxdjcMCxVVd8pnUlpfwOG51JfyCmItmjPXcoGM4QcEOg4 gxhHUtMM7Pagnlil2lHkgWmLehk9DNuLbgXHydltIQXEOs5OSmJYHC7SjJDs6WsE Y4GQ7MXXzoD2/zr7/5eeTfxYjdU/AvmN6AMNzsxBJV7IyTDy2FeMNda+Ll1X7EYz ErQhNbeBtt+JB5dQfwZiLPxx+WBuVkScCH2eOlry7N31Sy+kXI+2wZ0gQKrIBajH PkrAruSFMAI/CLguNy09N/0HlbK4SWYKCYP1uNZ1pvu+uRXh+Y/Ib9VFNWDR5y2T jr4pWd8eIe0hhjFzX1Ogygciy4TbeBd271Nf6RUUM/zCZBbPcll+X621w5EUIxbV tAiESYSg =2Ard -----END PGP SIGNATURE----- --59a84b46d5ac90f8d7eaf947d4b723e355bf88aa01ba826b613fc309f172--
guix-patches@HIDDEN
:bug#61214
; Package guix-patches
.
Full text available.Received: (at 61214) by debbugs.gnu.org; 12 Feb 2023 18:06:00 +0000 From debbugs-submit-bounces <at> debbugs.gnu.org Sun Feb 12 13:06:00 2023 Received: from localhost ([127.0.0.1]:46677 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>) id 1pRGjf-0003b4-To for submit <at> debbugs.gnu.org; Sun, 12 Feb 2023 13:06:00 -0500 Received: from eggs.gnu.org ([209.51.188.92]:32982) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <ludo@HIDDEN>) id 1pRGjd-0003ar-V9 for 61214 <at> debbugs.gnu.org; Sun, 12 Feb 2023 13:05:58 -0500 Received: from fencepost.gnu.org ([2001:470:142:3::e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from <ludo@HIDDEN>) id 1pRGjY-0006GZ-OU; Sun, 12 Feb 2023 13:05:52 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=MIME-Version:In-Reply-To:Date:References:Subject:To: From; bh=Okaq730h8wYvHQOm2YVVYLOKKlrw6QuXnOZHEFM49p0=; b=lgq6UJi+IzZJ/V2u59/v sT2OxMhJUydi1OPpqRhtOTMiIfrL0kDdTGHVH4aSfn9WHmzyQPUgt3C9baW4y25zr5waD9Qg0PNkh uiYQZhWsrQLyQy5t3JgMyCqo7/zZTN/hA8PtQ1ZaDSHZ+KaG29454owcJ/90wGXmHAw1zTrkADiZg fhIGnozZXsMtE0iXZuSZ9SLtw62BYx1vPtVbH+3XyJdV2Uatvbxs/zLgmgzdJaEEtcXh9lVZvEbhh CGTb5xreBRqG8dCcleM5Re66u8vkDWMgImlQo0GEku5WFwlq1TV5/fDHcX/5ESTrIDuODK1RmSx1c o7XfhKZuY18P+Q==; Received: from [185.228.228.230] (helo=ribbon) by fencepost.gnu.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from <ludo@HIDDEN>) id 1pRGjX-00076f-SP; Sun, 12 Feb 2023 13:05:52 -0500 From: =?utf-8?Q?Ludovic_Court=C3=A8s?= <ludo@HIDDEN> To: "(" <paren@HIDDEN> Subject: Re: bug#61214: [PATCH guix-artwork] website: posts: Add Dissecting Guix, Part 2: The Store Monad. References: <20230201172821.3072-1-paren@HIDDEN> <20230203073624.2338-1-paren@HIDDEN> Date: Sun, 12 Feb 2023 19:05:43 +0100 In-Reply-To: <20230203073624.2338-1-paren@HIDDEN> (paren@HIDDEN's message of "Fri, 3 Feb 2023 07:36:24 +0000") Message-ID: <87h6vqlpxk.fsf_-_@HIDDEN> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Score: -2.3 (--) X-Debbugs-Envelope-To: 61214 Cc: 61214 <at> debbugs.gnu.org X-BeenThere: debbugs-submit <at> debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: <debbugs-submit.debbugs.gnu.org> List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe> List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/> List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org> List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help> List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe> Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org> X-Spam-Score: -3.3 (---) Hello! "(" <paren@HIDDEN> skribis: > * website/posts/dissecting-guix-2-store-monad.md: New blog post. Nice work again! Some comments below: > +In [the last post](https://guix.gnu.org/en/blog/2023/dissecting-guix-par= t-1-derivations/), > +we briefly mentioned the `with-store` and `run-with-store` APIs. Today,= we'll > +be looking at those in further detail, along with the related monad API = and the > +`%store-monad`! Like Chris, I=E2=80=99m wary of acronyms (they can easily make things impenetrable) so I=E2=80=99d write: - the `with-store` macro and the `run-with-store` procedure - the related monad interface > +Monads are a little hard to explain, and from a distance, they seem more= than a > +bit confusing. So, I want you to erase monads from your mind for now. = We'll > +come back to them later. I agree with Chris=E2=80=99s comment: a few sentences of a higher-level int= ro showing where this fits in the big picture would be great! It would be nice to stress, also, that one doesn=E2=80=99t _need_ to know a= bout monads to use the various programming interfaces of Guix; instead, it=E2=80= =99s a thing for someone who=E2=80=99d like to get a deep dive into the internal= s of Guix. I=E2=80=99m saying this because we FP people, especially in the Haskell cam= p, are sometimes very good at making things look fancy and complicated. The last thing we=E2=80=99d want is to make it sound like this whole thing targets an audience of people with a PhD in the field. :-) > +# Yes, No, Maybe So > + > +Let's instead implement another M of functional programming, _`maybe`_ v= alues, > +representing a value that may or may not exist. `maybe` is a very common ^ Maybe add something like =E2=80=9CFor example, one might write a function t= hat divides two integers such that it returns special value `nothing` when the divisor is zero, and `maybe 5` when passed `15` and `3`.=E2=80=9D (I couldn=E2=80=99t think of a better example but you get the idea. :-)) > +feature of strongly-typed functional languages, and you'll see it all ov= er the s/strongly/statically/ :-) > +place in Haskell and OCaml code. However, Guile is dynamically typed, so= we > +usually use ad-hoc `#f`s and `'()`s for null values instead of a proper > +"optional" value. In Scheme we use #f, not '(), to denote =E2=80=9CNothing=E2=80=9D. > +# New Wheel, Old Wheel > + > +Now that we've reinvented the wheel, we'd better learn to use the origin= al > +wheel. Guix provides a generic, high-level monads API, along with the t= wo > +generic monads `%identity-monad` and `%state-monad`, and the Guix-specif= ic > +`%store-monad`. Since `maybe` is not one of them, let's integrate our v= ersion > +into the Guix monad system! > + > +First we'll make the API available: Maybe =E2=80=9CFirst, let=E2=80=99s import that module:=E2=80=9D? > +```scheme > +(use-modules (guix monads)) > +``` > + > +To define a monad's API in Guix, we simply use the `define-monad` macro,= and =E2=80=9CTo define a monad in Guix, we use `define-monad`=E2=80=9D (In general, I=E2=80=99d avoid =E2=80=9Csimply=E2=80=9D because whether it= =E2=80=99s =E2=80=9Csimple=E2=80=9D depends on one=E2=80=99s background.) > +`bind` is just the procedure that we use to compose monadic procedure ca= lls > +together, and `return` is the procedure that wraps values in the most ba= sic form > +of the monad. A properly implemented `bind` and `return` must follow th= ese > +laws: s/these laws/the so-called _monad laws_/ (since you use that term below) > +;; First law: the left identity. > +(equal? (maybe-chain (something 0) > + mlaws-proc-1) > + (mlaws-proc-1 0)) > +;; #t Should it be =E2=80=9C=E2=87=92 #t=E2=80=9D instead of =E2=80=9C;; #t=E2=80= =9D, to follow the convention used in the manual and in other places? > +What have we learned about monads? The key points we can take away are: > + > +1. Monads are a way of composing together procedures and values that are= wrapped > + in containers that give them extra context, like `maybe` values. > +2. Guix provides a high-level monad API that compensates for Guile's lac= k of s/monad API/monad module/ (or =E2=80=9Cmonad library=E2=80=9D) > + strong types or an interface-like system. =E2=80=9Cstatic types=E2=80=9D? > +3. This API provides the state monad, which allows you to thread state t= hrough s/This API/The `(guix monads)` module/ > + procedures such that you can pretend it doesn't exist. > +4. Guix uses the store monad frequently to thread a store connection thr= ough > + procedures that need it. > +5. The store monad is really just the state monad in disguise, where the= state > + value is used to thread the store object through monadic procedures. > + > +If you've read this post in its entirety but still don't yet quite get i= t, don't > +worry. Try to modify and tinker about with the examples, and hopefully = it will > +all click eventually! Maybe link to the =E2=80=9CThe Store Monad=E2=80=9D section of the manual h= ere or earlier? I feel we=E2=80=99re asking a lot of work from you, but hopefully the result will be even more pleasant. I guess v4 will be ready to go! Thanks for all the work, Ludo=E2=80=99.
guix-patches@HIDDEN
:bug#61214
; Package guix-patches
.
Full text available.Received: (at submit) by debbugs.gnu.org; 12 Feb 2023 14:18:05 +0000 From debbugs-submit-bounces <at> debbugs.gnu.org Sun Feb 12 09:18:05 2023 Received: from localhost ([127.0.0.1]:44422 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>) id 1pRDB6-0005YQ-UO for submit <at> debbugs.gnu.org; Sun, 12 Feb 2023 09:18:05 -0500 Received: from lists.gnu.org ([209.51.188.17]:42652) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <paren@HIDDEN>) id 1pRDB4-0005YI-Mk for submit <at> debbugs.gnu.org; Sun, 12 Feb 2023 09:18:03 -0500 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 <paren@HIDDEN>) id 1pRDB4-0005Gi-Ce for guix-patches@HIDDEN; Sun, 12 Feb 2023 09:18:02 -0500 Received: from knopi.disroot.org ([178.21.23.139]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from <paren@HIDDEN>) id 1pRDAy-0004hs-Mu for guix-patches@HIDDEN; Sun, 12 Feb 2023 09:17:58 -0500 Received: from localhost (localhost [127.0.0.1]) by disroot.org (Postfix) with ESMTP id 42BCF41327; Sun, 12 Feb 2023 15:17:53 +0100 (CET) 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 f7ZqqsuFBiPI; Sun, 12 Feb 2023 15:17:52 +0100 (CET) Content-Type: multipart/signed; boundary=5cc3f916ba10d364804d18b296b0a5302a012e9f7cd33632812b60b82ee4; micalg=pgp-sha512; protocol="application/pgp-signature" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=disroot.org; s=mail; t=1676211471; bh=E28h6ps1mZAzT5JXFkJnADrEGRugC/ZLuHObznvjNk8=; h=Date:Cc:Subject:From:To:References:In-Reply-To; b=XrhAnoVtLV/bvfpAHKJHD8VzA/fcMH4YF92L6oOsIscKdkzRipLbMz+VZCqeTIlEO 3xYYhfOpp9P+OzesTZeavFtUdgiyZ3Ny8j9OLYoRLJmmlIVW2ROSxgGb9yoC3OgV2d XtGE0HfD08NToVJ2Kgut/MI/KNZMesrMWOFU1TqUzZkRYDGurKCUmCPUJaH+FHEH9Z nz+i4sGaxr97HZMx7brje7t2HXILLJEyYdbUSyXbijjzZZBHKQJ3lbVMAmRgfJNyN5 JX+usfAO2whf+DPeZkvRLaC6MWvFlBqk69F9KPQEUuOu42hEPXQ/NIZivTZfGsQEPQ GuqngqnSEm9jg== Date: Sun, 12 Feb 2023 14:17:47 +0000 Message-Id: <CQGNDHR7IWV6.2MF3DI6YO66EG@guix-framework> Subject: Re: [bug#61214] [PATCH guix-artwork v3] website: posts: Add Dissecting Guix, Part 2: The Store Monad. From: "(" <paren@HIDDEN> To: "Christopher Baines" <mail@HIDDEN> References: <20230201172821.3072-1-paren@HIDDEN> <20230203073624.2338-1-paren@HIDDEN> <875yc73xgu.fsf@HIDDEN> In-Reply-To: <875yc73xgu.fsf@HIDDEN> Received-SPF: pass client-ip=178.21.23.139; envelope-from=paren@HIDDEN; helo=knopi.disroot.org X-Spam_score_int: -19 X-Spam_score: -2.0 X-Spam_bar: -- X-Spam_report: (-2.0 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, MIME_HEADER_CTYPE_ONLY=0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Spam-Score: -1.3 (-) X-Debbugs-Envelope-To: submit Cc: 61214 <at> debbugs.gnu.org, guix-patches@HIDDEN X-BeenThere: debbugs-submit <at> debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: <debbugs-submit.debbugs.gnu.org> List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe> List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/> List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org> List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help> List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe> Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org> X-Spam-Score: -2.3 (--) --5cc3f916ba10d364804d18b296b0a5302a012e9f7cd33632812b60b82ee4 Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Hi, On Sun Feb 12, 2023 at 10:47 AM GMT, Christopher Baines wrote: > I think there's some room to improve the introduction here. Linking to > the previous post in the series is fine, but what I think is missing is > some context around the topic and setting some expectations for the > reader. > I'm not sure who you're pitching this post at People who have used Guix and know basic Scheme but haven't delved into Gui= x's interiors yet :) > I think the s's after the `#f` and `'()` here don't aid > readability. Something like: Fair. > I think it would be clearer to say "To define the maybe monad, we use > the define-monad macro.", then there's no need to keep track of what API > is being discussed. I'm also not sure it's useful to talk about things > within Guix as APIs unless you're talking about a specific case of using > Guix from some external program/software. Good point. Maybe I could say something like: "To define the maybe monad's behaviour, we use the define-monad macro." using "behaviour" to describe the specifics of a monad. > I think this would be confusing for someone who's encountering monads > for the first time. I think it's good to try and avoid going to deep, > but if there's mention of the "laws", I think it's important to say that > these laws come from category theory. Yeah, okay. > > + > > +```scheme > > +(mbegin %maybe-monad > > + (remove-a "abc")) > > +;; #<<maybe> is?: #t value: "bc"> > > +``` > > This is stretching my understanding of monads here, but would this > example be better if the (mbegin bit included two expressions rather > than one? I might just remove the MBEGIN example entirely. I have no idea why MBEGIN= exists, or what advantages it confers, so I just included it for the sake of comple= teness -.o.- If someone could elaborate on what MBEGIN is for I would very much a= ppreciate it. > I think the point is still good here, but maybe it's simpler to say "but > why does Guix use monads?". Okay. > > +So, when we do `(run-with-state result (list 32))`, we're passing `(li= st 32)` as > > +the initial state value, and then the `>>=3D` form passes that and `33= ` to > > +`state-push`. What `%state-monad` allows us to do is thread together = some > > +procedures that require some kind of state, while pretending the state= isn't > > +there, and then retrieve both the final state and the result at the en= d! > > I'm not sure the "pretending the state isn't there" but is helpful here, > if you're pretending the state doesn't exist, why is writing monadic > code helpful? Yeah, this doesn't really get across the point I'm trying to make. I'm not= sure how else to word it, though... > > +We mentioned that, technically, we didn't need monads for Guix. Indee= d, many > > +(now deprecated) procedures take a store value as the argument, such a= s > > +`build-expression->derivation`. However, using monads both helps ensu= re purity > > +and simply looks nicer. > > I'm not sure what you mean by purity here? Me neither :P Simon mentioned something about monads ensuring purity in th= eir review, which I didn't quite understand, so I just wrote something vague ab= out it (which I shouldn't have done). > > +And indeed, it symlinks the `irssi` binary to the output path. Some o= ther, > > +higher-level, monadic procedures include `interned-file`, which copies= a file > > +from outside the store into it, and `text-file`, which copies some tex= t into it. > > +Generally, these procedures aren't used, as there are higher-level pro= cedures > > +that perform similar functions (which we will discuss later), but for = the sake > > +of this blog post, here's an example: > > + > > +```scheme > > +(with-store store > > + (run-with-store store > > + (text-file "unmatched-paren" > > + "( <paren@HIDDEN>"))) > > +;; "/gnu/store/v6smacxvdk4yvaa3s3wmd54lixn1dp3y-unmatched-paren" > > +``` > > I think the build up to this section is pretty good, but then I'm not > sure what this last section is trying to explain. It's just showing an example of the TEXT-FILE procedure, that's all :) > Maybe at this point it would be good to leave the REPL and give some > concrete examples of non-trivial monadic code in Guix, and discuss what > that would look like if implemented without using monads. Good idea! :) > > +# Conclusion > > + > > +What have we learned about monads? The key points we can take away ar= e: > > + > > +1. Monads are a way of composing together procedures and values that a= re wrapped > > + in containers that give them extra context, like `maybe` values. > > +2. Guix provides a high-level monad API that compensates for Guile's l= ack of > > + strong types or an interface-like system. > > I'd say that Guile is a strongly typed language. I'm also not sure what > the point about compensating for something lacking in Guile means. Guile doesn't have type definitions and it can't "fix" values to types. I'= d consider that to be weak typing, personally :) Regarding the point: it's supposed to say something like > > +4. Guix uses the store monad frequently to thread a store connection t= hrough > > + procedures that need it. > > +5. The store monad is really just the state monad in disguise, where t= he state > > + value is used to thread the store object through monadic procedures= . > > 4 and 5 here are observations, but not very useful conclusions. I think > the more interesting question to ask is why are things implemented this > way? > Ideally the closing points would be well made in the previous section, > and this final bit would be a summary. They're supposed to be a short summary of the main lessons the blog post attempts to teach, but I'll consider removing them. > > +If you've read this post in its entirety but still don't yet quite get= it, don't > > +worry. Try to modify and tinker about with the examples, and hopefull= y it will > > +all click eventually! > > Maybe this could be a call to get involved in the community (talk on IRC > or the mailing list? Yeah, good idea :) -- ( --5cc3f916ba10d364804d18b296b0a5302a012e9f7cd33632812b60b82ee4 Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQGzBAABCgAdFiEE6Vh10NblKE5doNlW7ImHg/nqI20FAmPo9QwACgkQ7ImHg/nq I2026wv+PeZLwwcKTJu38NOTCOQSyw46ZgvZFAOUzD3L/I+i1KE4GEHepxUZBDt1 +IuBvaVKiK5XvwqFwLl3aIbfs0Mf3HGDUKxbhvt4l43/T2EPjpU5eIUG6t69LTSZ jc9sC+jb77rF1sVIOKxe3SNb7+IX642HiVXtHgu8T6o+N2xndZNMyrnQG4pUDnyd dRzwYry2Ke7Hz2cm7wdnnJkG4/ziYOjWliVoniVJhwC7fPjdT0CwqYlvnwNyNjTL B9ItN59aGgOLnraFTVDKD1XJKnBSEyTlIkpi/P4ZK/mX7IBaX4R2MfNQO4/oy8lS r1SkWuDiybx+lE4xv55t+JInCq3GY6F6h6Yq8QkwRsaennSyzxCubRWOHoAeXupj 1Zb1TdQK5Z3asomtMbjsI7clyDDF7jTnPIhN6G0ISH2PCPRs0cdcevrrPX80poPW 6SLeVYRkT3Tns0H50LMXhEApEFwXvUTDfe5M2tkaBQGG1oAVn5Xmxw4osoGt1Osk LYubJIyd =KxYx -----END PGP SIGNATURE----- --5cc3f916ba10d364804d18b296b0a5302a012e9f7cd33632812b60b82ee4--
guix-patches@HIDDEN
:bug#61214
; Package guix-patches
.
Full text available.Received: (at 61214) by debbugs.gnu.org; 12 Feb 2023 14:17:57 +0000 From debbugs-submit-bounces <at> debbugs.gnu.org Sun Feb 12 09:17:57 2023 Received: from localhost ([127.0.0.1]:44418 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>) id 1pRDAz-0005Xh-CH for submit <at> debbugs.gnu.org; Sun, 12 Feb 2023 09:17:57 -0500 Received: from knopi.disroot.org ([178.21.23.139]:52922) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <paren@HIDDEN>) id 1pRDAx-0005XV-0a for 61214 <at> debbugs.gnu.org; Sun, 12 Feb 2023 09:17:56 -0500 Received: from localhost (localhost [127.0.0.1]) by disroot.org (Postfix) with ESMTP id 42BCF41327; Sun, 12 Feb 2023 15:17:53 +0100 (CET) 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 f7ZqqsuFBiPI; Sun, 12 Feb 2023 15:17:52 +0100 (CET) Content-Type: multipart/signed; boundary=5cc3f916ba10d364804d18b296b0a5302a012e9f7cd33632812b60b82ee4; micalg=pgp-sha512; protocol="application/pgp-signature" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=disroot.org; s=mail; t=1676211471; bh=E28h6ps1mZAzT5JXFkJnADrEGRugC/ZLuHObznvjNk8=; h=Date:Cc:Subject:From:To:References:In-Reply-To; b=XrhAnoVtLV/bvfpAHKJHD8VzA/fcMH4YF92L6oOsIscKdkzRipLbMz+VZCqeTIlEO 3xYYhfOpp9P+OzesTZeavFtUdgiyZ3Ny8j9OLYoRLJmmlIVW2ROSxgGb9yoC3OgV2d XtGE0HfD08NToVJ2Kgut/MI/KNZMesrMWOFU1TqUzZkRYDGurKCUmCPUJaH+FHEH9Z nz+i4sGaxr97HZMx7brje7t2HXILLJEyYdbUSyXbijjzZZBHKQJ3lbVMAmRgfJNyN5 JX+usfAO2whf+DPeZkvRLaC6MWvFlBqk69F9KPQEUuOu42hEPXQ/NIZivTZfGsQEPQ GuqngqnSEm9jg== Date: Sun, 12 Feb 2023 14:17:47 +0000 Message-Id: <CQGNDHR7IWV6.2MF3DI6YO66EG@guix-framework> Subject: Re: [bug#61214] [PATCH guix-artwork v3] website: posts: Add Dissecting Guix, Part 2: The Store Monad. From: "(" <paren@HIDDEN> To: "Christopher Baines" <mail@HIDDEN> References: <20230201172821.3072-1-paren@HIDDEN> <20230203073624.2338-1-paren@HIDDEN> <875yc73xgu.fsf@HIDDEN> In-Reply-To: <875yc73xgu.fsf@HIDDEN> X-Spam-Score: 0.1 (/) X-Debbugs-Envelope-To: 61214 Cc: 61214 <at> debbugs.gnu.org, guix-patches@HIDDEN X-BeenThere: debbugs-submit <at> debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: <debbugs-submit.debbugs.gnu.org> List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe> List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/> List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org> List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help> List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe> Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org> X-Spam-Score: -0.9 (/) --5cc3f916ba10d364804d18b296b0a5302a012e9f7cd33632812b60b82ee4 Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Hi, On Sun Feb 12, 2023 at 10:47 AM GMT, Christopher Baines wrote: > I think there's some room to improve the introduction here. Linking to > the previous post in the series is fine, but what I think is missing is > some context around the topic and setting some expectations for the > reader. > I'm not sure who you're pitching this post at People who have used Guix and know basic Scheme but haven't delved into Gui= x's interiors yet :) > I think the s's after the `#f` and `'()` here don't aid > readability. Something like: Fair. > I think it would be clearer to say "To define the maybe monad, we use > the define-monad macro.", then there's no need to keep track of what API > is being discussed. I'm also not sure it's useful to talk about things > within Guix as APIs unless you're talking about a specific case of using > Guix from some external program/software. Good point. Maybe I could say something like: "To define the maybe monad's behaviour, we use the define-monad macro." using "behaviour" to describe the specifics of a monad. > I think this would be confusing for someone who's encountering monads > for the first time. I think it's good to try and avoid going to deep, > but if there's mention of the "laws", I think it's important to say that > these laws come from category theory. Yeah, okay. > > + > > +```scheme > > +(mbegin %maybe-monad > > + (remove-a "abc")) > > +;; #<<maybe> is?: #t value: "bc"> > > +``` > > This is stretching my understanding of monads here, but would this > example be better if the (mbegin bit included two expressions rather > than one? I might just remove the MBEGIN example entirely. I have no idea why MBEGIN= exists, or what advantages it confers, so I just included it for the sake of comple= teness -.o.- If someone could elaborate on what MBEGIN is for I would very much a= ppreciate it. > I think the point is still good here, but maybe it's simpler to say "but > why does Guix use monads?". Okay. > > +So, when we do `(run-with-state result (list 32))`, we're passing `(li= st 32)` as > > +the initial state value, and then the `>>=3D` form passes that and `33= ` to > > +`state-push`. What `%state-monad` allows us to do is thread together = some > > +procedures that require some kind of state, while pretending the state= isn't > > +there, and then retrieve both the final state and the result at the en= d! > > I'm not sure the "pretending the state isn't there" but is helpful here, > if you're pretending the state doesn't exist, why is writing monadic > code helpful? Yeah, this doesn't really get across the point I'm trying to make. I'm not= sure how else to word it, though... > > +We mentioned that, technically, we didn't need monads for Guix. Indee= d, many > > +(now deprecated) procedures take a store value as the argument, such a= s > > +`build-expression->derivation`. However, using monads both helps ensu= re purity > > +and simply looks nicer. > > I'm not sure what you mean by purity here? Me neither :P Simon mentioned something about monads ensuring purity in th= eir review, which I didn't quite understand, so I just wrote something vague ab= out it (which I shouldn't have done). > > +And indeed, it symlinks the `irssi` binary to the output path. Some o= ther, > > +higher-level, monadic procedures include `interned-file`, which copies= a file > > +from outside the store into it, and `text-file`, which copies some tex= t into it. > > +Generally, these procedures aren't used, as there are higher-level pro= cedures > > +that perform similar functions (which we will discuss later), but for = the sake > > +of this blog post, here's an example: > > + > > +```scheme > > +(with-store store > > + (run-with-store store > > + (text-file "unmatched-paren" > > + "( <paren@HIDDEN>"))) > > +;; "/gnu/store/v6smacxvdk4yvaa3s3wmd54lixn1dp3y-unmatched-paren" > > +``` > > I think the build up to this section is pretty good, but then I'm not > sure what this last section is trying to explain. It's just showing an example of the TEXT-FILE procedure, that's all :) > Maybe at this point it would be good to leave the REPL and give some > concrete examples of non-trivial monadic code in Guix, and discuss what > that would look like if implemented without using monads. Good idea! :) > > +# Conclusion > > + > > +What have we learned about monads? The key points we can take away ar= e: > > + > > +1. Monads are a way of composing together procedures and values that a= re wrapped > > + in containers that give them extra context, like `maybe` values. > > +2. Guix provides a high-level monad API that compensates for Guile's l= ack of > > + strong types or an interface-like system. > > I'd say that Guile is a strongly typed language. I'm also not sure what > the point about compensating for something lacking in Guile means. Guile doesn't have type definitions and it can't "fix" values to types. I'= d consider that to be weak typing, personally :) Regarding the point: it's supposed to say something like > > +4. Guix uses the store monad frequently to thread a store connection t= hrough > > + procedures that need it. > > +5. The store monad is really just the state monad in disguise, where t= he state > > + value is used to thread the store object through monadic procedures= . > > 4 and 5 here are observations, but not very useful conclusions. I think > the more interesting question to ask is why are things implemented this > way? > Ideally the closing points would be well made in the previous section, > and this final bit would be a summary. They're supposed to be a short summary of the main lessons the blog post attempts to teach, but I'll consider removing them. > > +If you've read this post in its entirety but still don't yet quite get= it, don't > > +worry. Try to modify and tinker about with the examples, and hopefull= y it will > > +all click eventually! > > Maybe this could be a call to get involved in the community (talk on IRC > or the mailing list? Yeah, good idea :) -- ( --5cc3f916ba10d364804d18b296b0a5302a012e9f7cd33632812b60b82ee4 Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQGzBAABCgAdFiEE6Vh10NblKE5doNlW7ImHg/nqI20FAmPo9QwACgkQ7ImHg/nq I2026wv+PeZLwwcKTJu38NOTCOQSyw46ZgvZFAOUzD3L/I+i1KE4GEHepxUZBDt1 +IuBvaVKiK5XvwqFwLl3aIbfs0Mf3HGDUKxbhvt4l43/T2EPjpU5eIUG6t69LTSZ jc9sC+jb77rF1sVIOKxe3SNb7+IX642HiVXtHgu8T6o+N2xndZNMyrnQG4pUDnyd dRzwYry2Ke7Hz2cm7wdnnJkG4/ziYOjWliVoniVJhwC7fPjdT0CwqYlvnwNyNjTL B9ItN59aGgOLnraFTVDKD1XJKnBSEyTlIkpi/P4ZK/mX7IBaX4R2MfNQO4/oy8lS r1SkWuDiybx+lE4xv55t+JInCq3GY6F6h6Yq8QkwRsaennSyzxCubRWOHoAeXupj 1Zb1TdQK5Z3asomtMbjsI7clyDDF7jTnPIhN6G0ISH2PCPRs0cdcevrrPX80poPW 6SLeVYRkT3Tns0H50LMXhEApEFwXvUTDfe5M2tkaBQGG1oAVn5Xmxw4osoGt1Osk LYubJIyd =KxYx -----END PGP SIGNATURE----- --5cc3f916ba10d364804d18b296b0a5302a012e9f7cd33632812b60b82ee4--
guix-patches@HIDDEN
:bug#61214
; Package guix-patches
.
Full text available.Received: (at 61214) by debbugs.gnu.org; 12 Feb 2023 12:00:25 +0000 From debbugs-submit-bounces <at> debbugs.gnu.org Sun Feb 12 07:00:25 2023 Received: from localhost ([127.0.0.1]:44218 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>) id 1pRB1s-0007op-G8 for submit <at> debbugs.gnu.org; Sun, 12 Feb 2023 07:00:25 -0500 Received: from mira.cbaines.net ([212.71.252.8]:42210) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <mail@HIDDEN>) id 1pRB1q-0007og-0k for 61214 <at> debbugs.gnu.org; Sun, 12 Feb 2023 07:00:23 -0500 Received: from localhost (unknown [IPv6:2a02:8010:68c1:0:54d1:d5d4:280e:f699]) by mira.cbaines.net (Postfix) with ESMTPSA id 266B916620; Sun, 12 Feb 2023 12:00:21 +0000 (GMT) Received: from felis (localhost [127.0.0.1]) by localhost (OpenSMTPD) with ESMTP id 4cf79a71; Sun, 12 Feb 2023 12:00:20 +0000 (UTC) References: <20230201172821.3072-1-paren@HIDDEN> <20230203073624.2338-1-paren@HIDDEN> User-agent: mu4e 1.8.11; emacs 28.2 From: Christopher Baines <mail@HIDDEN> To: "(" <paren@HIDDEN> Subject: Re: [bug#61214] [PATCH guix-artwork v3] website: posts: Add Dissecting Guix, Part 2: The Store Monad. Date: Sun, 12 Feb 2023 10:47:11 +0000 In-reply-to: <20230203073624.2338-1-paren@HIDDEN> Message-ID: <875yc73xgu.fsf@HIDDEN> MIME-Version: 1.0 Content-Type: multipart/signed; boundary="=-=-="; micalg=pgp-sha512; protocol="application/pgp-signature" X-Spam-Score: -0.0 (/) X-Debbugs-Envelope-To: 61214 Cc: 61214 <at> debbugs.gnu.org, guix-patches@HIDDEN X-BeenThere: debbugs-submit <at> debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: <debbugs-submit.debbugs.gnu.org> List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe> List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/> List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org> List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help> List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe> Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org> X-Spam-Score: -1.0 (-) --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable "( via Guix-patches" via <guix-patches@HIDDEN> writes: > +Hello again! > + > +In [the last post](https://guix.gnu.org/en/blog/2023/dissecting-guix-par= t-1-derivations/), > +we briefly mentioned the `with-store` and `run-with-store` APIs. Today,= we'll > +be looking at those in further detail, along with the related monad API = and the > +`%store-monad`! > + > +Monads are a little hard to explain, and from a distance, they seem more= than a > +bit confusing. So, I want you to erase monads from your mind for now. = We'll > +come back to them later. I think there's some room to improve the introduction here. Linking to the previous post in the series is fine, but what I think is missing is some context around the topic and setting some expectations for the reader. I'm not sure who you're pitching this post at, but I'll assume that you want it to be accessible and interesting to people who don't know anything about Guix, but maybe have some programing experience. I think this introduction here [1] is a really good one. It's not too long, but it puts the topic in some context, sets expectations, and does all of that in a way that I think would be understood by someone who doesn't know about Guix. 1: https://guix.gnu.org/en/blog/2021/the-big-change/ > +# Yes, No, Maybe So > + > +Let's instead implement another M of functional programming, _`maybe`_ v= alues, > +representing a value that may or may not exist. `maybe` is a very common > +feature of strongly-typed functional languages, and you'll see it all ov= er the > +place in Haskell and OCaml code. However, Guile is dynamically typed, so= we > +usually use ad-hoc `#f`s and `'()`s for null values instead of a proper > +"optional" value. I think the s's after the `#f` and `'()` here don't aid readability. Something like: usually use ad-hoc `#false` and `'()` (empty list) values instead > +Just for fun, though, we'll implement a proper `maybe` in Guile. Fire u= p that > +REPL once again, and let's import a bunch of modules that we'll need: ... > +A more formal definition would be that a monad is a mathematical object = composed > +of three parts: a type, a `bind` function, and a `return` function. So,= how do > +monads relate to Guix? > + > +# New Wheel, Old Wheel > + > +Now that we've reinvented the wheel, we'd better learn to use the origin= al > +wheel. Guix provides a generic, high-level monads API, along with the t= wo > +generic monads `%identity-monad` and `%state-monad`, and the Guix-specif= ic > +`%store-monad`. Since `maybe` is not one of them, let's integrate our v= ersion > +into the Guix monad system! > + > +First we'll make the API available: > + > +```scheme > +(use-modules (guix monads)) > +``` > + > +To define a monad's API in Guix, we simply use the `define-monad` macro,= and > +provide two procedures: `bind`, and `return`. At least when I read this, I'm drawn to the use of "API" numerous times and keeping track of what's being talked about. =2D Guix provides a generic, high-level monads API Maybe "Guix includes a generic monads module providing syntax and types, along with the two generic monads ..." would be more informative here. =2D we'll make the API available I'm not too fussed about this. =2D To define a monad's API in Guix, we Maybe API here refers to the same API as just mentioned previously, but I guess you're now talking about a different API, but this is confusing. I think it would be clearer to say "To define the maybe monad, we use the define-monad macro.", then there's no need to keep track of what API is being discussed. I'm also not sure it's useful to talk about things within Guix as APIs unless you're talking about a specific case of using Guix from some external program/software. > +```scheme > +(define-monad %maybe-monad > + (bind maybe-chain) > + (return something)) > +``` > + > +`bind` is just the procedure that we use to compose monadic procedure ca= lls > +together, and `return` is the procedure that wraps values in the most ba= sic form > +of the monad. A properly implemented `bind` and `return` must follow th= ese > +laws: I think this would be confusing for someone who's encountering monads for the first time. I think it's good to try and avoid going to deep, but if there's mention of the "laws", I think it's important to say that these laws come from category theory. ... > +But Guix provides many higher-level APIs than `>>=3D` and `return`, as w= e will > +see. There's `mbegin`, which evaluates monadic expressions without bind= ing them > +to symbols, returning the last one: > + > +```scheme > +(mbegin %maybe-monad > + (remove-a "abc")) > +;; #<<maybe> is?: #t value: "bc"> > +``` This is stretching my understanding of monads here, but would this example be better if the (mbegin bit included two expressions rather than one? > +And there's `mlet` and `mlet*`, which can bind them, and are essentially > +equivalent to a chain of `(>>=3D MEXPR (lambda (BINDING) ...))`: ... > +This is all well and good, you may be thinking, but why does Guix need a= monad > +API? The answer is technically that it doesn't. But building on the mo= nad API > +makes a lot of things much easier, and to learn why, we're going to look= at one > +of Guix's built-in monads. The "API" returns. At least when I think of an "API" in the context of Guix, I'm thinking of that interface providing a way to use Guix, from an external prospective. Obviously that doesn't really match up with what's going on here. I think the point is still good here, but maybe it's simpler to say "but why does Guix use monads?". > +# In a State > + > +Guix implements a monad called `%state-monad`, and it works with single-= argument > +procedures returning two values. Behold: > + > +```scheme > +(with-monad %state-monad > + (return 33)) > +;; #<procedure 21dc9a0 at <unknown port>:1106:22 (state)> > +``` > + > +The `run-with-state` value turns this procedure into an actually useful = value, > +or, rather, two values: > + > +```scheme > +(run-with-state (with-monad %state-monad (return 33)) > + (list "foo" "bar" "baz")) > +;; 33 > +;; ("foo" "bar" "baz") > +``` > + > +What can this actually do for us, though? Well, it gets interesting if w= e do > +some `>>=3D`ing: > + > +```scheme > +(define state-seq > + (mlet* %state-monad ((number (return 33))) > + (state-push number))) > +result > +;; #<procedure 7fcb6f466960 at <unknown port>:1484:24 (state)> > + > +(run-with-state state-seq (list 32)) > +;; (32) > +;; (33 32) > + > +(run-with-state state-seq (list 30 99)) > +;; (30 99) > +;; (33 30 99) > +``` > + > +What is `state-push`? It's a monadic procedure for `%state-monad` that = takes > +whatever's currently in the first value (the primary value) and pushes i= t onto > +the second value (the state value), which is assumed to be a list, retur= ning the > +old state value as the primary value and the new list as the state value. > + > +So, when we do `(run-with-state result (list 32))`, we're passing `(list= 32)` as > +the initial state value, and then the `>>=3D` form passes that and `33` = to > +`state-push`. What `%state-monad` allows us to do is thread together so= me > +procedures that require some kind of state, while pretending the state i= sn't > +there, and then retrieve both the final state and the result at the end! I'm not sure the "pretending the state isn't there" but is helpful here, if you're pretending the state doesn't exist, why is writing monadic code helpful? > +If you're a bit confused, don't worry. We'll write some of our own > +`%state-monad`-based monadic procedures and hopefully all will become cl= ear. > +Consider, for instance, the > +[Fibonacci sequence](https://en.wikipedia.org/wiki/Fibonacci_number), in= which > +each value is computed by adding the previous two. We could use the > +`%state-monad` to compute Fibonacci numbers by storing the previous numb= er as > +the primary value and the number before that as the state value: ... > +This is all very nifty, and possibly useful in general, but what does th= is have > +to do with Guix? Well, many Guix store-based operations are meant to be= used > +in concert with yet another monad, called the `%store-monad`. But if we= look at > +`(guix store)`, where `%store-monad` is defined... > + > +```scheme > +(define-alias %store-monad %state-monad) > +(define-alias store-return state-return) > +(define-alias store-bind state-bind) > +``` > + > +It was all a shallow fa=C3=A7ade! All the "store monad" is is a special= case of the > +state monad, where a value representing the store is passed as the state= value. > + > +# Lies, Damned Lies, and Abstractions > + > +We mentioned that, technically, we didn't need monads for Guix. Indeed,= many > +(now deprecated) procedures take a store value as the argument, such as > +`build-expression->derivation`. However, using monads both helps ensure= purity > +and simply looks nicer. I'm not sure what you mean by purity here? > +`build-expression->derivation`, being deprecated, should never of course= be > +used. For one thing, it uses the "quoted build expression" style, rathe= r than > +G-expressions (we'll discuss gexps another time). The best way to creat= e a > +derivation from some basic build code is to use the new-fangled > +`gexp->derivation` procedure: > + > +```scheme > +(use-modules (guix gexp) > + (gnu packages irc)) > + > +(define symlink-irssi > + (gexp->derivation "link-to-irssi" > + #~(symlink #$(file-append irssi "/bin/irssi") #$output))) > +;; #<procedure 7fddcc7b81e0 at guix/gexp.scm:1180:2 (state)> > +``` > + > +You don't have to understand the `#~(...)` form yet, only everything sur= rounding > +it. We can see that this `gexp->derivation` returns a procedure taking = the > +initial state (store), just like our `%state-monad` procedures did, and = like we > +used `run-with-state` to pass the initial state to a `%state-monad` mona= dic > +value, we use our old friend `run-with-store` when we have a `%store-mon= ad` > +monadic value! > + > +```scheme > +(define symlink-irssi-drv > + (with-store store > + (run-with-store store > + symlink-irssi))) > +;; #<derivation /gnu/store/q7kwwl4z6psifnv4di1p1kpvlx06fmyq-link-to-irss= i.drv =3D> /gnu/store/6a94niigx4ii0ldjdy33wx9anhifr25x-link-to-irssi 7fddb7= ef52d0> > +``` > + > +Let's just check this derivation is as expected by reading the code from= the > +builder script. > + > +```scheme > +(define symlink-irssi-builder > + (list-ref (derivation-builder-arguments symlink-irssi-drv) 1)) > + > +(call-with-input-file symlink-irssi-builder > + (lambda (port) > + (read port))) > +=20=20=20=20 > +;; (symlink > +;; "/gnu/store/hrlmypx1lrdjlxpkqy88bfrzg5p0bn6d-irssi-1.4.3/bin/irssi" > +;; ((@ (guile) getenv) "out")) > +``` > + > +And indeed, it symlinks the `irssi` binary to the output path. Some oth= er, > +higher-level, monadic procedures include `interned-file`, which copies a= file > +from outside the store into it, and `text-file`, which copies some text = into it. > +Generally, these procedures aren't used, as there are higher-level proce= dures > +that perform similar functions (which we will discuss later), but for th= e sake > +of this blog post, here's an example: > + > +```scheme > +(with-store store > + (run-with-store store > + (text-file "unmatched-paren" > + "( <paren@HIDDEN>"))) > +;; "/gnu/store/v6smacxvdk4yvaa3s3wmd54lixn1dp3y-unmatched-paren" > +``` I think the build up to this section is pretty good, but then I'm not sure what this last section is trying to explain. Maybe at this point it would be good to leave the REPL and give some concrete examples of non-trivial monadic code in Guix, and discuss what that would look like if implemented without using monads. > +# Conclusion > + > +What have we learned about monads? The key points we can take away are: > + > +1. Monads are a way of composing together procedures and values that are= wrapped > + in containers that give them extra context, like `maybe` values. > +2. Guix provides a high-level monad API that compensates for Guile's lac= k of > + strong types or an interface-like system. I'd say that Guile is a strongly typed language. I'm also not sure what the point about compensating for something lacking in Guile means. > +3. This API provides the state monad, which allows you to thread state t= hrough > + procedures such that you can pretend it doesn't exist. > +4. Guix uses the store monad frequently to thread a store connection thr= ough > + procedures that need it. > +5. The store monad is really just the state monad in disguise, where the= state > + value is used to thread the store object through monadic procedures. 4 and 5 here are observations, but not very useful conclusions. I think the more interesting question to ask is why are things implemented this way? Ideally the closing points would be well made in the previous section, and this final bit would be a summary. > +If you've read this post in its entirety but still don't yet quite get i= t, don't > +worry. Try to modify and tinker about with the examples, and hopefully = it will > +all click eventually! Maybe this could be a call to get involved in the community (talk on IRC or the mailing list? --=-=-= Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQKlBAEBCgCPFiEEPonu50WOcg2XVOCyXiijOwuE9XcFAmPo1NFfFIAAAAAALgAo aXNzdWVyLWZwckBub3RhdGlvbnMub3BlbnBncC5maWZ0aGhvcnNlbWFuLm5ldDNF ODlFRUU3NDU4RTcyMEQ5NzU0RTBCMjVFMjhBMzNCMEI4NEY1NzcRHG1haWxAY2Jh aW5lcy5uZXQACgkQXiijOwuE9XcikQ/+OGv7ppCF0xh/PeowuGkmL83L/GvK3Wl2 Ouk9R7MMJ4a55XkI0gZ9PGIrS4RQ/Y40I8wNjag/5SPd9KWyKzdSKI+Jp/9ASOI2 eyt5dWMgl2h0eNwFWQ5qYQBWlZmbJ28ChwKYlMoOh1eM0xcK7s1yWCQ7QzHx8aID 96a4eBqKeC1KKbpwlmfq1Oa2uqEU4eS2Met/hb7yldapnWuRIxvCXc/IxwQbdRaZ HeOsc929N2aBaiXQUuLWqhvtwq/s6t9tZZJsUokgmqCEIxgBE28EFN0o0ipF9Ez7 QEjQxgaPt3ZMcWhp7ntmDRjlmzun4JVG0GdXr4w9tygPPV0lZl25T8qXphKzlAob FMry4RYypFBK+W7q8F1T+bMFm/z8hgQcXJV2w8iY40IpYvQ/7U31r6En/z8P5W0K KZNR84yUegHTV1wVHM248kBzczMZlUOSv0RPNTeH/ZPKbnCrhWDDX6JnRmGUvZwX kRQFQVPCipdISimk2PSHkbpm1XhIs7vv3+JFkYwkSkravB2kUYEw4MR0XgvXhQUa HBEuZdEklWwK2hzMYCDtqdfdJKj2QBEcZ/TnFtEaDQ/o+UZeiA+rj9iShzZMQUIG 1kX3U2PQq463fig+5q5jNW9+uEC6S97z1C/KfGWZJBsotGhYaV65/YTY9NjxUkhD 05VoOrfT4b4= =SJ4y -----END PGP SIGNATURE----- --=-=-=--
guix-patches@HIDDEN
:bug#61214
; Package guix-patches
.
Full text available.Received: (at submit) by debbugs.gnu.org; 12 Feb 2023 12:00:32 +0000 From debbugs-submit-bounces <at> debbugs.gnu.org Sun Feb 12 07:00:32 2023 Received: from localhost ([127.0.0.1]:44221 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>) id 1pRB1z-0007pD-FF for submit <at> debbugs.gnu.org; Sun, 12 Feb 2023 07:00:32 -0500 Received: from lists.gnu.org ([209.51.188.17]:33048) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <mail@HIDDEN>) id 1pRB1x-0007p2-K8 for submit <at> debbugs.gnu.org; Sun, 12 Feb 2023 07:00:30 -0500 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 <mail@HIDDEN>) id 1pRB1w-0007j3-DM for guix-patches@HIDDEN; Sun, 12 Feb 2023 07:00:28 -0500 Received: from mira.cbaines.net ([212.71.252.8]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from <mail@HIDDEN>) id 1pRB1t-0001Ba-3B for guix-patches@HIDDEN; Sun, 12 Feb 2023 07:00:28 -0500 Received: from localhost (unknown [IPv6:2a02:8010:68c1:0:54d1:d5d4:280e:f699]) by mira.cbaines.net (Postfix) with ESMTPSA id 266B916620; Sun, 12 Feb 2023 12:00:21 +0000 (GMT) Received: from felis (localhost [127.0.0.1]) by localhost (OpenSMTPD) with ESMTP id 4cf79a71; Sun, 12 Feb 2023 12:00:20 +0000 (UTC) References: <20230201172821.3072-1-paren@HIDDEN> <20230203073624.2338-1-paren@HIDDEN> User-agent: mu4e 1.8.11; emacs 28.2 From: Christopher Baines <mail@HIDDEN> To: "(" <paren@HIDDEN> Subject: Re: [bug#61214] [PATCH guix-artwork v3] website: posts: Add Dissecting Guix, Part 2: The Store Monad. Date: Sun, 12 Feb 2023 10:47:11 +0000 In-reply-to: <20230203073624.2338-1-paren@HIDDEN> Message-ID: <875yc73xgu.fsf@HIDDEN> MIME-Version: 1.0 Content-Type: multipart/signed; boundary="=-=-="; micalg=pgp-sha512; protocol="application/pgp-signature" Received-SPF: pass client-ip=212.71.252.8; envelope-from=mail@HIDDEN; helo=mira.cbaines.net X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Spam-Score: -1.4 (-) X-Debbugs-Envelope-To: submit Cc: 61214 <at> debbugs.gnu.org, guix-patches@HIDDEN X-BeenThere: debbugs-submit <at> debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: <debbugs-submit.debbugs.gnu.org> List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe> List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/> List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org> List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help> List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe> Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org> X-Spam-Score: -2.4 (--) --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable "( via Guix-patches" via <guix-patches@HIDDEN> writes: > +Hello again! > + > +In [the last post](https://guix.gnu.org/en/blog/2023/dissecting-guix-par= t-1-derivations/), > +we briefly mentioned the `with-store` and `run-with-store` APIs. Today,= we'll > +be looking at those in further detail, along with the related monad API = and the > +`%store-monad`! > + > +Monads are a little hard to explain, and from a distance, they seem more= than a > +bit confusing. So, I want you to erase monads from your mind for now. = We'll > +come back to them later. I think there's some room to improve the introduction here. Linking to the previous post in the series is fine, but what I think is missing is some context around the topic and setting some expectations for the reader. I'm not sure who you're pitching this post at, but I'll assume that you want it to be accessible and interesting to people who don't know anything about Guix, but maybe have some programing experience. I think this introduction here [1] is a really good one. It's not too long, but it puts the topic in some context, sets expectations, and does all of that in a way that I think would be understood by someone who doesn't know about Guix. 1: https://guix.gnu.org/en/blog/2021/the-big-change/ > +# Yes, No, Maybe So > + > +Let's instead implement another M of functional programming, _`maybe`_ v= alues, > +representing a value that may or may not exist. `maybe` is a very common > +feature of strongly-typed functional languages, and you'll see it all ov= er the > +place in Haskell and OCaml code. However, Guile is dynamically typed, so= we > +usually use ad-hoc `#f`s and `'()`s for null values instead of a proper > +"optional" value. I think the s's after the `#f` and `'()` here don't aid readability. Something like: usually use ad-hoc `#false` and `'()` (empty list) values instead > +Just for fun, though, we'll implement a proper `maybe` in Guile. Fire u= p that > +REPL once again, and let's import a bunch of modules that we'll need: ... > +A more formal definition would be that a monad is a mathematical object = composed > +of three parts: a type, a `bind` function, and a `return` function. So,= how do > +monads relate to Guix? > + > +# New Wheel, Old Wheel > + > +Now that we've reinvented the wheel, we'd better learn to use the origin= al > +wheel. Guix provides a generic, high-level monads API, along with the t= wo > +generic monads `%identity-monad` and `%state-monad`, and the Guix-specif= ic > +`%store-monad`. Since `maybe` is not one of them, let's integrate our v= ersion > +into the Guix monad system! > + > +First we'll make the API available: > + > +```scheme > +(use-modules (guix monads)) > +``` > + > +To define a monad's API in Guix, we simply use the `define-monad` macro,= and > +provide two procedures: `bind`, and `return`. At least when I read this, I'm drawn to the use of "API" numerous times and keeping track of what's being talked about. =2D Guix provides a generic, high-level monads API Maybe "Guix includes a generic monads module providing syntax and types, along with the two generic monads ..." would be more informative here. =2D we'll make the API available I'm not too fussed about this. =2D To define a monad's API in Guix, we Maybe API here refers to the same API as just mentioned previously, but I guess you're now talking about a different API, but this is confusing. I think it would be clearer to say "To define the maybe monad, we use the define-monad macro.", then there's no need to keep track of what API is being discussed. I'm also not sure it's useful to talk about things within Guix as APIs unless you're talking about a specific case of using Guix from some external program/software. > +```scheme > +(define-monad %maybe-monad > + (bind maybe-chain) > + (return something)) > +``` > + > +`bind` is just the procedure that we use to compose monadic procedure ca= lls > +together, and `return` is the procedure that wraps values in the most ba= sic form > +of the monad. A properly implemented `bind` and `return` must follow th= ese > +laws: I think this would be confusing for someone who's encountering monads for the first time. I think it's good to try and avoid going to deep, but if there's mention of the "laws", I think it's important to say that these laws come from category theory. ... > +But Guix provides many higher-level APIs than `>>=3D` and `return`, as w= e will > +see. There's `mbegin`, which evaluates monadic expressions without bind= ing them > +to symbols, returning the last one: > + > +```scheme > +(mbegin %maybe-monad > + (remove-a "abc")) > +;; #<<maybe> is?: #t value: "bc"> > +``` This is stretching my understanding of monads here, but would this example be better if the (mbegin bit included two expressions rather than one? > +And there's `mlet` and `mlet*`, which can bind them, and are essentially > +equivalent to a chain of `(>>=3D MEXPR (lambda (BINDING) ...))`: ... > +This is all well and good, you may be thinking, but why does Guix need a= monad > +API? The answer is technically that it doesn't. But building on the mo= nad API > +makes a lot of things much easier, and to learn why, we're going to look= at one > +of Guix's built-in monads. The "API" returns. At least when I think of an "API" in the context of Guix, I'm thinking of that interface providing a way to use Guix, from an external prospective. Obviously that doesn't really match up with what's going on here. I think the point is still good here, but maybe it's simpler to say "but why does Guix use monads?". > +# In a State > + > +Guix implements a monad called `%state-monad`, and it works with single-= argument > +procedures returning two values. Behold: > + > +```scheme > +(with-monad %state-monad > + (return 33)) > +;; #<procedure 21dc9a0 at <unknown port>:1106:22 (state)> > +``` > + > +The `run-with-state` value turns this procedure into an actually useful = value, > +or, rather, two values: > + > +```scheme > +(run-with-state (with-monad %state-monad (return 33)) > + (list "foo" "bar" "baz")) > +;; 33 > +;; ("foo" "bar" "baz") > +``` > + > +What can this actually do for us, though? Well, it gets interesting if w= e do > +some `>>=3D`ing: > + > +```scheme > +(define state-seq > + (mlet* %state-monad ((number (return 33))) > + (state-push number))) > +result > +;; #<procedure 7fcb6f466960 at <unknown port>:1484:24 (state)> > + > +(run-with-state state-seq (list 32)) > +;; (32) > +;; (33 32) > + > +(run-with-state state-seq (list 30 99)) > +;; (30 99) > +;; (33 30 99) > +``` > + > +What is `state-push`? It's a monadic procedure for `%state-monad` that = takes > +whatever's currently in the first value (the primary value) and pushes i= t onto > +the second value (the state value), which is assumed to be a list, retur= ning the > +old state value as the primary value and the new list as the state value. > + > +So, when we do `(run-with-state result (list 32))`, we're passing `(list= 32)` as > +the initial state value, and then the `>>=3D` form passes that and `33` = to > +`state-push`. What `%state-monad` allows us to do is thread together so= me > +procedures that require some kind of state, while pretending the state i= sn't > +there, and then retrieve both the final state and the result at the end! I'm not sure the "pretending the state isn't there" but is helpful here, if you're pretending the state doesn't exist, why is writing monadic code helpful? > +If you're a bit confused, don't worry. We'll write some of our own > +`%state-monad`-based monadic procedures and hopefully all will become cl= ear. > +Consider, for instance, the > +[Fibonacci sequence](https://en.wikipedia.org/wiki/Fibonacci_number), in= which > +each value is computed by adding the previous two. We could use the > +`%state-monad` to compute Fibonacci numbers by storing the previous numb= er as > +the primary value and the number before that as the state value: ... > +This is all very nifty, and possibly useful in general, but what does th= is have > +to do with Guix? Well, many Guix store-based operations are meant to be= used > +in concert with yet another monad, called the `%store-monad`. But if we= look at > +`(guix store)`, where `%store-monad` is defined... > + > +```scheme > +(define-alias %store-monad %state-monad) > +(define-alias store-return state-return) > +(define-alias store-bind state-bind) > +``` > + > +It was all a shallow fa=C3=A7ade! All the "store monad" is is a special= case of the > +state monad, where a value representing the store is passed as the state= value. > + > +# Lies, Damned Lies, and Abstractions > + > +We mentioned that, technically, we didn't need monads for Guix. Indeed,= many > +(now deprecated) procedures take a store value as the argument, such as > +`build-expression->derivation`. However, using monads both helps ensure= purity > +and simply looks nicer. I'm not sure what you mean by purity here? > +`build-expression->derivation`, being deprecated, should never of course= be > +used. For one thing, it uses the "quoted build expression" style, rathe= r than > +G-expressions (we'll discuss gexps another time). The best way to creat= e a > +derivation from some basic build code is to use the new-fangled > +`gexp->derivation` procedure: > + > +```scheme > +(use-modules (guix gexp) > + (gnu packages irc)) > + > +(define symlink-irssi > + (gexp->derivation "link-to-irssi" > + #~(symlink #$(file-append irssi "/bin/irssi") #$output))) > +;; #<procedure 7fddcc7b81e0 at guix/gexp.scm:1180:2 (state)> > +``` > + > +You don't have to understand the `#~(...)` form yet, only everything sur= rounding > +it. We can see that this `gexp->derivation` returns a procedure taking = the > +initial state (store), just like our `%state-monad` procedures did, and = like we > +used `run-with-state` to pass the initial state to a `%state-monad` mona= dic > +value, we use our old friend `run-with-store` when we have a `%store-mon= ad` > +monadic value! > + > +```scheme > +(define symlink-irssi-drv > + (with-store store > + (run-with-store store > + symlink-irssi))) > +;; #<derivation /gnu/store/q7kwwl4z6psifnv4di1p1kpvlx06fmyq-link-to-irss= i.drv =3D> /gnu/store/6a94niigx4ii0ldjdy33wx9anhifr25x-link-to-irssi 7fddb7= ef52d0> > +``` > + > +Let's just check this derivation is as expected by reading the code from= the > +builder script. > + > +```scheme > +(define symlink-irssi-builder > + (list-ref (derivation-builder-arguments symlink-irssi-drv) 1)) > + > +(call-with-input-file symlink-irssi-builder > + (lambda (port) > + (read port))) > +=20=20=20=20 > +;; (symlink > +;; "/gnu/store/hrlmypx1lrdjlxpkqy88bfrzg5p0bn6d-irssi-1.4.3/bin/irssi" > +;; ((@ (guile) getenv) "out")) > +``` > + > +And indeed, it symlinks the `irssi` binary to the output path. Some oth= er, > +higher-level, monadic procedures include `interned-file`, which copies a= file > +from outside the store into it, and `text-file`, which copies some text = into it. > +Generally, these procedures aren't used, as there are higher-level proce= dures > +that perform similar functions (which we will discuss later), but for th= e sake > +of this blog post, here's an example: > + > +```scheme > +(with-store store > + (run-with-store store > + (text-file "unmatched-paren" > + "( <paren@HIDDEN>"))) > +;; "/gnu/store/v6smacxvdk4yvaa3s3wmd54lixn1dp3y-unmatched-paren" > +``` I think the build up to this section is pretty good, but then I'm not sure what this last section is trying to explain. Maybe at this point it would be good to leave the REPL and give some concrete examples of non-trivial monadic code in Guix, and discuss what that would look like if implemented without using monads. > +# Conclusion > + > +What have we learned about monads? The key points we can take away are: > + > +1. Monads are a way of composing together procedures and values that are= wrapped > + in containers that give them extra context, like `maybe` values. > +2. Guix provides a high-level monad API that compensates for Guile's lac= k of > + strong types or an interface-like system. I'd say that Guile is a strongly typed language. I'm also not sure what the point about compensating for something lacking in Guile means. > +3. This API provides the state monad, which allows you to thread state t= hrough > + procedures such that you can pretend it doesn't exist. > +4. Guix uses the store monad frequently to thread a store connection thr= ough > + procedures that need it. > +5. The store monad is really just the state monad in disguise, where the= state > + value is used to thread the store object through monadic procedures. 4 and 5 here are observations, but not very useful conclusions. I think the more interesting question to ask is why are things implemented this way? Ideally the closing points would be well made in the previous section, and this final bit would be a summary. > +If you've read this post in its entirety but still don't yet quite get i= t, don't > +worry. Try to modify and tinker about with the examples, and hopefully = it will > +all click eventually! Maybe this could be a call to get involved in the community (talk on IRC or the mailing list? --=-=-= Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQKlBAEBCgCPFiEEPonu50WOcg2XVOCyXiijOwuE9XcFAmPo1NFfFIAAAAAALgAo aXNzdWVyLWZwckBub3RhdGlvbnMub3BlbnBncC5maWZ0aGhvcnNlbWFuLm5ldDNF ODlFRUU3NDU4RTcyMEQ5NzU0RTBCMjVFMjhBMzNCMEI4NEY1NzcRHG1haWxAY2Jh aW5lcy5uZXQACgkQXiijOwuE9XcikQ/+OGv7ppCF0xh/PeowuGkmL83L/GvK3Wl2 Ouk9R7MMJ4a55XkI0gZ9PGIrS4RQ/Y40I8wNjag/5SPd9KWyKzdSKI+Jp/9ASOI2 eyt5dWMgl2h0eNwFWQ5qYQBWlZmbJ28ChwKYlMoOh1eM0xcK7s1yWCQ7QzHx8aID 96a4eBqKeC1KKbpwlmfq1Oa2uqEU4eS2Met/hb7yldapnWuRIxvCXc/IxwQbdRaZ HeOsc929N2aBaiXQUuLWqhvtwq/s6t9tZZJsUokgmqCEIxgBE28EFN0o0ipF9Ez7 QEjQxgaPt3ZMcWhp7ntmDRjlmzun4JVG0GdXr4w9tygPPV0lZl25T8qXphKzlAob FMry4RYypFBK+W7q8F1T+bMFm/z8hgQcXJV2w8iY40IpYvQ/7U31r6En/z8P5W0K KZNR84yUegHTV1wVHM248kBzczMZlUOSv0RPNTeH/ZPKbnCrhWDDX6JnRmGUvZwX kRQFQVPCipdISimk2PSHkbpm1XhIs7vv3+JFkYwkSkravB2kUYEw4MR0XgvXhQUa HBEuZdEklWwK2hzMYCDtqdfdJKj2QBEcZ/TnFtEaDQ/o+UZeiA+rj9iShzZMQUIG 1kX3U2PQq463fig+5q5jNW9+uEC6S97z1C/KfGWZJBsotGhYaV65/YTY9NjxUkhD 05VoOrfT4b4= =SJ4y -----END PGP SIGNATURE----- --=-=-=--
guix-patches@HIDDEN
:bug#61214
; Package guix-patches
.
Full text available.Received: (at 61214) by debbugs.gnu.org; 6 Feb 2023 18:20:23 +0000 From debbugs-submit-bounces <at> debbugs.gnu.org Mon Feb 06 13:20:23 2023 Received: from localhost ([127.0.0.1]:49876 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>) id 1pP66I-0008WM-NP for submit <at> debbugs.gnu.org; Mon, 06 Feb 2023 13:20:22 -0500 Received: from mail-wm1-f49.google.com ([209.85.128.49]:42663) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <zimon.toutoune@HIDDEN>) id 1pP66H-0008Vm-AP for 61214 <at> debbugs.gnu.org; Mon, 06 Feb 2023 13:20:21 -0500 Received: by mail-wm1-f49.google.com with SMTP id j29-20020a05600c1c1d00b003dc52fed235so9557607wms.1 for <61214 <at> debbugs.gnu.org>; Mon, 06 Feb 2023 10:20:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:references :in-reply-to:subject:cc:to:from:from:to:cc:subject:date:message-id :reply-to; bh=tNGe2Lr5/BfoySdT19IhMIpqYXVYRttBm5BAPbbkUJw=; b=FZv2yIaNwE5KILFbHou82UhiSiYqs7S4IH6vNqBhw6LlLGngZZtHDJnTL4r7Wrn0iv KdKaoe7r+My8i45aSLUKNnLcBJsKy+RugdvNaXenBRajNx3uWtwho0GodL1ZIZv5G0ds ObYRSTUG8hJ0iksDnuW5jlyK+m0YmQLuAhDRu8tzKOSm1mhULmjQZunV46WKsvJAzwgf 69jMMI6K1gheWSTMFaQYj4MgILw8oY9kcIxxKZnRlBB4wZEezfKfmJdh56AXJh122yWE dHKa6abeQFRhRqpQ/R5doTpb2pnR4O5ELiaUco87TZ/V/P/wbJ1Nv+dPLQQke/gpNhWi xAJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:references :in-reply-to:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=tNGe2Lr5/BfoySdT19IhMIpqYXVYRttBm5BAPbbkUJw=; b=60aG8x9MuPywpCEzxManrvwkjW2nLoabiTiFxW/RgL/M51xOS35O0CVDL7jZfKonTb RTmvNttRQVNZnpGOzm3Id5rgh5JFjewml3HQXsXZnL66TgxeSIqKm7VWtFVTJDbjLwzI m+FqnYmARZlrtAmKdCm/UNUCKE5MrBWDmJE6Yhoq60QgnvuyCz58Z1zTbKYSg3OuJ5Xm zXjFXo+45ip/PALmk7rtiWv/lvEpUK55nUMK3aXU/E3UxWhf1bjK3mkeoyNJ6k81mO77 H9UAsnszyZDPEfg70j7Kb9ZM7+5KCehkOKl0MGbbEFNX0St4E62ah3R2Mwqonnm0Fi09 Phxw== X-Gm-Message-State: AO0yUKWaaCc3IXAkiY9YApVLVHsO4S08133oEi5BITBwCzKahLsE9Jsg sjUrwY1yBWX7SeELOBw83Ac= X-Google-Smtp-Source: AK7set/2Ruu0pQoZo37zRzyYbass54AreLToEgQHgnOfHH5ZPPfMXV1bRsbT9hsuS+TSuHHNHIb1kQ== X-Received: by 2002:a05:600c:3b28:b0:3df:fc67:cfe4 with SMTP id m40-20020a05600c3b2800b003dffc67cfe4mr716192wms.3.1675707615188; Mon, 06 Feb 2023 10:20:15 -0800 (PST) Received: from pfiuh07 ([193.48.40.241]) by smtp.gmail.com with ESMTPSA id q14-20020a05600c46ce00b003dc47d458cdsm12240172wmo.15.2023.02.06.10.20.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Feb 2023 10:20:14 -0800 (PST) From: Simon Tournier <zimon.toutoune@HIDDEN> To: "( via Guix-patches via" <guix-patches@HIDDEN>, 61214 <at> debbugs.gnu.org Subject: Re: [bug#61214] [PATCH guix-artwork v2] website: posts: Add Dissecting Guix, Part 2: The Store Monad. In-Reply-To: <20230202150028.3005-1-paren@HIDDEN> References: <20230201172821.3072-1-paren@HIDDEN> <20230202150028.3005-1-paren@HIDDEN> Date: Mon, 06 Feb 2023 16:38:18 +0100 Message-ID: <87wn4ukdn9.fsf@HIDDEN> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Score: 0.0 (/) X-Debbugs-Envelope-To: 61214 Cc: "\(" <paren@HIDDEN> X-BeenThere: debbugs-submit <at> debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: <debbugs-submit.debbugs.gnu.org> List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe> List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/> List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org> List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help> List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe> Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org> X-Spam-Score: -1.0 (-) Hi, On jeu., 02 f=C3=A9vr. 2023 at 15:00, "\( via Guix-patches" via <guix-patch= es@HIDDEN> wrote: > .../posts/dissecting-guix-2-store-monad.md | 555 ++++++++++++++++++ > 1 file changed, 555 insertions(+) > create mode 100644 website/posts/dissecting-guix-2-store-monad.md Well done! Nice reading, I like it. :-) My English is not enough good to evaluate if there is no grammar mistakes. Other said, LGTM! Cheers, simon
guix-patches@HIDDEN
:bug#61214
; Package guix-patches
.
Full text available.Received: (at submit) by debbugs.gnu.org; 6 Feb 2023 18:20:20 +0000 From debbugs-submit-bounces <at> debbugs.gnu.org Mon Feb 06 13:20:20 2023 Received: from localhost ([127.0.0.1]:49872 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>) id 1pP66G-0008W2-Df for submit <at> debbugs.gnu.org; Mon, 06 Feb 2023 13:20:20 -0500 Received: from lists.gnu.org ([209.51.188.17]:59504) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <zimon.toutoune@HIDDEN>) id 1pP66E-0008Vt-R8 for submit <at> debbugs.gnu.org; Mon, 06 Feb 2023 13:20:19 -0500 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 <zimon.toutoune@HIDDEN>) id 1pP66E-0007bR-FM for guix-patches@HIDDEN; Mon, 06 Feb 2023 13:20:18 -0500 Received: from mail-wm1-x32d.google.com ([2a00:1450:4864:20::32d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from <zimon.toutoune@HIDDEN>) id 1pP66C-0007vd-VK for guix-patches@HIDDEN; Mon, 06 Feb 2023 13:20:18 -0500 Received: by mail-wm1-x32d.google.com with SMTP id q8so9311676wmo.5 for <guix-patches@HIDDEN>; Mon, 06 Feb 2023 10:20:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:references :in-reply-to:subject:cc:to:from:from:to:cc:subject:date:message-id :reply-to; bh=tNGe2Lr5/BfoySdT19IhMIpqYXVYRttBm5BAPbbkUJw=; b=FZv2yIaNwE5KILFbHou82UhiSiYqs7S4IH6vNqBhw6LlLGngZZtHDJnTL4r7Wrn0iv KdKaoe7r+My8i45aSLUKNnLcBJsKy+RugdvNaXenBRajNx3uWtwho0GodL1ZIZv5G0ds ObYRSTUG8hJ0iksDnuW5jlyK+m0YmQLuAhDRu8tzKOSm1mhULmjQZunV46WKsvJAzwgf 69jMMI6K1gheWSTMFaQYj4MgILw8oY9kcIxxKZnRlBB4wZEezfKfmJdh56AXJh122yWE dHKa6abeQFRhRqpQ/R5doTpb2pnR4O5ELiaUco87TZ/V/P/wbJ1Nv+dPLQQke/gpNhWi xAJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:references :in-reply-to:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=tNGe2Lr5/BfoySdT19IhMIpqYXVYRttBm5BAPbbkUJw=; b=fDBGbXpERTFJdSL1fhf8VUCmMsDYaraOEUMBjjeX40c145HmjObQYNPBHoGlBG7PLX 08DXyV6gaf3mP+2GvHJ6F/p4SBuZQglbICyk670fADFPNgSEiYojvqxX1WzKRnKxEQ0I nCJAotXFVPCotugLf6HJ0CZw99STADl/aegaHzR4AY7B1olF18bsI37Lk4Gl1zdlHoaE JlcsxdnhKhjHMSh6SMR6gIg7VSK7t+p5kyK06gAQQL2cFcXeYRtF+mV5dmTSkRjBbDRF eZJ8L36uCKAgtGnTqRYVQX1KmGnDsoeNpLnAlt1zmitqi3TCiyrbCCk1WJMBdNKYlVJP 1DeA== X-Gm-Message-State: AO0yUKWyaCeAkrTV7eBFa7IDKlQAcbTzV8coQJejOBhWdnyxRW5Iym2p TSFHO+cxbFUVrOK8wYHNSQo= X-Google-Smtp-Source: AK7set/2Ruu0pQoZo37zRzyYbass54AreLToEgQHgnOfHH5ZPPfMXV1bRsbT9hsuS+TSuHHNHIb1kQ== X-Received: by 2002:a05:600c:3b28:b0:3df:fc67:cfe4 with SMTP id m40-20020a05600c3b2800b003dffc67cfe4mr716192wms.3.1675707615188; Mon, 06 Feb 2023 10:20:15 -0800 (PST) Received: from pfiuh07 ([193.48.40.241]) by smtp.gmail.com with ESMTPSA id q14-20020a05600c46ce00b003dc47d458cdsm12240172wmo.15.2023.02.06.10.20.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Feb 2023 10:20:14 -0800 (PST) From: Simon Tournier <zimon.toutoune@HIDDEN> To: "( via Guix-patches via" <guix-patches@HIDDEN>, 61214 <at> debbugs.gnu.org Subject: Re: [bug#61214] [PATCH guix-artwork v2] website: posts: Add Dissecting Guix, Part 2: The Store Monad. In-Reply-To: <20230202150028.3005-1-paren@HIDDEN> References: <20230201172821.3072-1-paren@HIDDEN> <20230202150028.3005-1-paren@HIDDEN> Date: Mon, 06 Feb 2023 16:38:18 +0100 Message-ID: <87wn4ukdn9.fsf@HIDDEN> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Received-SPF: pass client-ip=2a00:1450:4864:20::32d; envelope-from=zimon.toutoune@HIDDEN; helo=mail-wm1-x32d.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Spam-Score: -1.3 (-) X-Debbugs-Envelope-To: submit Cc: "\(" <paren@HIDDEN> X-BeenThere: debbugs-submit <at> debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: <debbugs-submit.debbugs.gnu.org> List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe> List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/> List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org> List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help> List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe> Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org> X-Spam-Score: -2.3 (--) Hi, On jeu., 02 f=C3=A9vr. 2023 at 15:00, "\( via Guix-patches" via <guix-patch= es@HIDDEN> wrote: > .../posts/dissecting-guix-2-store-monad.md | 555 ++++++++++++++++++ > 1 file changed, 555 insertions(+) > create mode 100644 website/posts/dissecting-guix-2-store-monad.md Well done! Nice reading, I like it. :-) My English is not enough good to evaluate if there is no grammar mistakes. Other said, LGTM! Cheers, simon
guix-patches@HIDDEN
:bug#61214
; Package guix-patches
.
Full text available.Received: (at 61214) by debbugs.gnu.org; 3 Feb 2023 07:36:44 +0000 From debbugs-submit-bounces <at> debbugs.gnu.org Fri Feb 03 02:36:44 2023 Received: from localhost ([127.0.0.1]:36408 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>) id 1pNqcl-000691-GF for submit <at> debbugs.gnu.org; Fri, 03 Feb 2023 02:36:44 -0500 Received: from knopi.disroot.org ([178.21.23.139]:38720) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <paren@HIDDEN>) id 1pNqci-00068p-HT for 61214 <at> debbugs.gnu.org; Fri, 03 Feb 2023 02:36:42 -0500 Received: from localhost (localhost [127.0.0.1]) by disroot.org (Postfix) with ESMTP id B40324139B; Fri, 3 Feb 2023 08:36:38 +0100 (CET) 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 HwaEDWMG_LRH; Fri, 3 Feb 2023 08:36:35 +0100 (CET) From: "(" <paren@HIDDEN> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=disroot.org; s=mail; t=1675409795; bh=sx2xKo4J5jfNKcG0WKMCL9ewVBZjdrVmsXyaEAiQUH0=; h=From:To:Cc:Subject:Date; b=A8HedlByP95pUYE9hQt8mPInHDMPG9cOSDXEa7DWTgBoMve/U2pjHVN6bMGTi84YN i3+2W7sOYzZGYxWlr8H2xyzrImjuZTw9X/hkla9Hib8iAg1lRB/z1rl/pQ2ygGpB7A UYEIMSNUpq6LdkK1JiJpVZg+vas5ESCwn0DY+7ARBvNu+IZ7TsmP+KU/fgii7jV7Nd yaZbAY/YIlGLQ6wSBUOat43w2yi2upUPUPkVeUMe1S2ixeVdtNXaDPXe3mtWwvVC77 l9OkzrPVfkJbb1Qnyu/FoyfIB5ezFFyU2KnEGA2JQM2PVGn2kc4ibhxKVT4s5evBFI xx3dHIg3fG49g== To: 61214 <at> debbugs.gnu.org Subject: [PATCH guix-artwork v3] website: posts: Add Dissecting Guix, Part 2: The Store Monad. Date: Fri, 3 Feb 2023 07:36:24 +0000 Message-Id: <20230203073624.2338-1-paren@HIDDEN> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Score: 0.0 (/) X-Debbugs-Envelope-To: 61214 Cc: "\(" <paren@HIDDEN> X-BeenThere: debbugs-submit <at> debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: <debbugs-submit.debbugs.gnu.org> List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe> List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/> List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org> List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help> List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe> Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org> X-Spam-Score: -1.0 (-) * website/posts/dissecting-guix-2-store-monad.md: New blog post. --- Heya, Here's a v3. * Make purpose of `run-with-store` versus `run-with-state` clearer. -- ( .../posts/dissecting-guix-2-store-monad.md | 557 ++++++++++++++++++ 1 file changed, 557 insertions(+) create mode 100644 website/posts/dissecting-guix-2-store-monad.md diff --git a/website/posts/dissecting-guix-2-store-monad.md b/website/posts/dissecting-guix-2-store-monad.md new file mode 100644 index 0000000..a27a28b --- /dev/null +++ b/website/posts/dissecting-guix-2-store-monad.md @@ -0,0 +1,557 @@ +title: Dissecting Guix, Part 2: The Store Monad +date: TBC +author: ( +tags: Dissecting Guix, Functional package management, Programming interfaces, Scheme API +--- +Hello again! + +In [the last post](https://guix.gnu.org/en/blog/2023/dissecting-guix-part-1-derivations/), +we briefly mentioned the `with-store` and `run-with-store` APIs. Today, we'll +be looking at those in further detail, along with the related monad API and the +`%store-monad`! + +Monads are a little hard to explain, and from a distance, they seem more than a +bit confusing. So, I want you to erase monads from your mind for now. We'll +come back to them later. + +# Yes, No, Maybe So + +Let's instead implement another M of functional programming, _`maybe`_ values, +representing a value that may or may not exist. `maybe` is a very common +feature of strongly-typed functional languages, and you'll see it all over the +place in Haskell and OCaml code. However, Guile is dynamically typed, so we +usually use ad-hoc `#f`s and `'()`s for null values instead of a proper +"optional" value. + +Just for fun, though, we'll implement a proper `maybe` in Guile. Fire up that +REPL once again, and let's import a bunch of modules that we'll need: + +```scheme +(use-modules (ice-9 match) + (srfi srfi-9)) +``` + +We'll implement `maybe` as a record with two fields, `is?` and `value`. If the +value contains something, `is?` will be `#t` and `value` will contain the thing +in question, and if it's empty, `is?`'ll be `#f`. + +```scheme +(define-record-type <maybe> + (make-maybe is? value) + maybe? + (is? maybe-is?) + (value maybe-value)) +``` + +Now we'll define constructors for the two possible states: + +```scheme +(define (something value) + (make-maybe #t value)) + +(define (nothing) + (make-maybe #f #f)) ;the value here doesn't matter; we'll just use #f +``` + +And make some silly functions that return optional values: + +```scheme +(define (remove-a str) + (if (eq? (string-ref str 0) #\a) + (something (substring str 1)) + (nothing))) + +(define (remove-b str) + (if (eq? (string-ref str 0) #\b) + (something (substring str 1)) + (nothing))) + +(remove-a "ahh") +;; #<<maybe> is?: #t value: "hh"> + +(remove-a "ooh") +;; #<<maybe> is?: #f value: #f> + +(remove-b "bad") +;; #<<maybe> is?: #t value: "ad"> +``` + +But what if we want to compose the results of these functions? + +# Keeping Your Composure + +As you might have guessed, this is not fun. Cosplaying as a compiler backend +typically isn't. + +```scheme +(let ((t1 (remove-a "abcd"))) + (if (maybe-is? t1) + (remove-b (maybe-value t1)) + (nothing))) +;; #<<maybe> is?: #t value: "cd"> + +(let ((t1 (remove-a "bbcd"))) + (if (maybe-is? t1) + (remove-b (maybe-value t1)) + (nothing))) +;; #<<maybe> is?: #f value: #f> +``` + +I can almost hear the heckling. Even worse, composing three: + +```scheme +(let* ((t1 (remove-a "abad")) + (t2 (if (maybe-is? t1) + (remove-b (maybe-value t1)) + (nothing)))) + (if (maybe-is? t2) + (remove-a (maybe-value t2)) + (nothing))) +;; #<<maybe> is?: #t value: "d"> +``` + +So, how do we go about making this more bearable? Well, one way could be to +make `remove-a` and `remove-b` accept `maybe`s: + +```scheme +(define (remove-a ?str) + (match ?str + (($ <maybe> #t str) + (if (eq? (string-ref str 0) #\a) + (something (substring str 1)) + (nothing))) + (_ (nothing)))) + +(define (remove-b ?str) + (match ?str + (($ <maybe> #t str) + (if (eq? (string-ref str 0) #\b) + (something (substring str 1)) + (nothing))) + (_ (nothing)))) +``` + +Not at all pretty, but it works! + +``` +(remove-b (remove-a (something "abc"))) +;; #<<maybe> is?: #t value: "c"> +``` + +Still, our procedures now require quite a bit of boilerplate. Might there be a +better way? + +# The Ties That `>>=` Us + +First of all, we'll revert to our original definitions of `remove-a` and +`remove-b`, that is to say, the ones that take a regular value and return a +`maybe`. + +```scheme +(define (remove-a str) + (if (eq? (string-ref str 0) #\a) + (something (substring str 1)) + (nothing))) + +(define (remove-b str) + (if (eq? (string-ref str 0) #\b) + (something (substring str 1)) + (nothing))) +``` + +What if tried introducing higher-order procedures (procedures that accept other +procedures as arguments) into the equation? Because we're functional +programmers and we have an unhealthy obsession with that sort of thing. + +```scheme +(define (maybe-chain maybe proc) + (if (maybe-is? maybe) + (proc (maybe-value maybe)) + (nothing))) + +(maybe-chain (something "abc") + remove-a) +;; #<<maybe> is?: #t value: "bc"> + +(maybe-chain (nothing) + remove-a) +;; #<<maybe> is?: #f value: #f> +``` + +It lives! To make it easier to compose procedures like this, we'll define a +macro that allows us to perform any number of sequenced operations with only one +composition form: + +```scheme +(define-syntax maybe-chain* + (syntax-rules () + ((_ maybe proc) + (maybe-chain maybe proc)) + ((_ maybe proc rest ...) + (maybe-chain* (maybe-chain maybe proc) + rest ...)))) + +(maybe-chain* (something "abad") + remove-a + remove-b + remove-a) +;; #<<maybe> is?: #t value: "d"> +``` + +Congratulations, you've just implemented the `bind` operation, commonly written +as `>>=`, for our `maybe` type. And it turns out that a monad is just any +container-like value for which `>>=` (along with another procedure called +`return`, which wraps a given value in the simplest possible form of a monad) +has been implemented. + +A more formal definition would be that a monad is a mathematical object composed +of three parts: a type, a `bind` function, and a `return` function. So, how do +monads relate to Guix? + +# New Wheel, Old Wheel + +Now that we've reinvented the wheel, we'd better learn to use the original +wheel. Guix provides a generic, high-level monads API, along with the two +generic monads `%identity-monad` and `%state-monad`, and the Guix-specific +`%store-monad`. Since `maybe` is not one of them, let's integrate our version +into the Guix monad system! + +First we'll make the API available: + +```scheme +(use-modules (guix monads)) +``` + +To define a monad's API in Guix, we simply use the `define-monad` macro, and +provide two procedures: `bind`, and `return`. + +```scheme +(define-monad %maybe-monad + (bind maybe-chain) + (return something)) +``` + +`bind` is just the procedure that we use to compose monadic procedure calls +together, and `return` is the procedure that wraps values in the most basic form +of the monad. A properly implemented `bind` and `return` must follow these +laws: + +1. `(bind (return x) proc)` must be equivalent to `(proc x)`. +2. `(bind monad return)` must be equivalent to just `monad`. +3. `(bind (bind monad proc-1) proc-2)` must be equivalent to + `(bind monad (lambda (x) (bind (proc-1 x) proc-2)))`. + +Let's verify that our `maybe-chain` and `something` procedures adhere to the +monad laws: + +```scheme +(define (mlaws-proc-1 x) + (something (+ x 1))) + +(define (mlaws-proc-2 x) + (something (+ x 2))) + +;; First law: the left identity. +(equal? (maybe-chain (something 0) + mlaws-proc-1) + (mlaws-proc-1 0)) +;; #t + +;; Second law: the right identity. +(equal? (maybe-chain (something 0) + something) + (something 0)) +;; #t + +;; Third law: associativity. +(equal? (maybe-chain (maybe-chain (something 0) + mlaws-proc-1) + mlaws-proc-2) + (maybe-chain (something 0) + (lambda (x) + (maybe-chain (mlaws-proc-1 x) + mlaws-proc-2)))) +;; #t +``` + +Now that we know they're valid, we can use the `with-monad` macro to tell Guix +to use these specific implementations of `bind` and `return`, and the `>>=` +macro to thread monads through procedure calls! + +```scheme +(with-monad %maybe-monad + (>>= (something "aabbc") + remove-a + remove-a + remove-b + remove-b)) +;; #<<maybe> is?: #t value: "c"> +``` + +We can also now use `return`: + +```scheme +(with-monad %maybe-monad + (return 32)) +;; #<<maybe> is?: #t value: 32> +``` + +But Guix provides many higher-level APIs than `>>=` and `return`, as we will +see. There's `mbegin`, which evaluates monadic expressions without binding them +to symbols, returning the last one: + +```scheme +(mbegin %maybe-monad + (remove-a "abc")) +;; #<<maybe> is?: #t value: "bc"> +``` + +And there's `mlet` and `mlet*`, which can bind them, and are essentially +equivalent to a chain of `(>>= MEXPR (lambda (BINDING) ...))`: + +```scheme +;; This is equivalent... +(mlet* %maybe-monad ((str -> "abad") ;non-monadic binding uses the -> symbol + (str1 (remove-a str)) + (str2 (remove-b str))) + (remove-a str)) +;; #<<maybe> is?: #t value: "d"> + +;; ...to this: +(with-monad %maybe-monad + (>>= (return "abad") + (lambda (str) + (remove-a str)) + (lambda (str1) + (remove-b str)) + (lambda (str2) + (remove-a str)))) +``` + +Various abstractions over these two exist too, such as `mwhen` (a `when` plus an +`mbegin`), `munless` (an `unless` plus an `mbegin`), and `mparameterize` +(dynamically-scoped value rebinding, like `parameterize`, in a monadic context). +`lift` takes a procedure and a monad and creates a new procedure that returns +a monadic value. + +There are also APIs for manipulating lists wrapped in monads; `listm` creates +such a list, `sequence` turns a list of monads into a list wrapped in a monad, +and the `anym`, `mapm`, and `foldm` procedures are like their non-monadic +equivalents, except that they return lists wrapped in monads. + +This is all well and good, you may be thinking, but why does Guix need a monad +API? The answer is technically that it doesn't. But building on the monad API +makes a lot of things much easier, and to learn why, we're going to look at one +of Guix's built-in monads. + +# In a State + +Guix implements a monad called `%state-monad`, and it works with single-argument +procedures returning two values. Behold: + +```scheme +(with-monad %state-monad + (return 33)) +;; #<procedure 21dc9a0 at <unknown port>:1106:22 (state)> +``` + +The `run-with-state` value turns this procedure into an actually useful value, +or, rather, two values: + +```scheme +(run-with-state (with-monad %state-monad (return 33)) + (list "foo" "bar" "baz")) +;; 33 +;; ("foo" "bar" "baz") +``` + +What can this actually do for us, though? Well, it gets interesting if we do +some `>>=`ing: + +```scheme +(define state-seq + (mlet* %state-monad ((number (return 33))) + (state-push number))) +result +;; #<procedure 7fcb6f466960 at <unknown port>:1484:24 (state)> + +(run-with-state state-seq (list 32)) +;; (32) +;; (33 32) + +(run-with-state state-seq (list 30 99)) +;; (30 99) +;; (33 30 99) +``` + +What is `state-push`? It's a monadic procedure for `%state-monad` that takes +whatever's currently in the first value (the primary value) and pushes it onto +the second value (the state value), which is assumed to be a list, returning the +old state value as the primary value and the new list as the state value. + +So, when we do `(run-with-state result (list 32))`, we're passing `(list 32)` as +the initial state value, and then the `>>=` form passes that and `33` to +`state-push`. What `%state-monad` allows us to do is thread together some +procedures that require some kind of state, while pretending the state isn't +there, and then retrieve both the final state and the result at the end! + +If you're a bit confused, don't worry. We'll write some of our own +`%state-monad`-based monadic procedures and hopefully all will become clear. +Consider, for instance, the +[Fibonacci sequence](https://en.wikipedia.org/wiki/Fibonacci_number), in which +each value is computed by adding the previous two. We could use the +`%state-monad` to compute Fibonacci numbers by storing the previous number as +the primary value and the number before that as the state value: + +```scheme +(define (fibonacci-thing value) + (lambda (state) + (values (+ value state) + value))) +``` + +Now we can feed our Fibonacci-generating procedure the first value using +`run-with-state` and the second using `return`: + +```scheme +(run-with-state + (mlet* %state-monad ((starting (return 1)) + (n1 (fibonacci-thing starting)) + (n2 (fibonacci-thing n1))) + (fibonacci-thing n2)) + 0) +;; 3 +;; 2 + +(run-with-state + (mlet* %state-monad ((starting (return 1)) + (n1 (fibonacci-thing starting)) + (n2 (fibonacci-thing n1)) + (n3 (fibonacci-thing n2)) + (n4 (fibonacci-thing n3)) + (n5 (fibonacci-thing n4))) + (fibonacci-thing n5)) + 0) +;; 13 +;; 8 +``` + +This is all very nifty, and possibly useful in general, but what does this have +to do with Guix? Well, many Guix store-based operations are meant to be used +in concert with yet another monad, called the `%store-monad`. But if we look at +`(guix store)`, where `%store-monad` is defined... + +```scheme +(define-alias %store-monad %state-monad) +(define-alias store-return state-return) +(define-alias store-bind state-bind) +``` + +It was all a shallow façade! All the "store monad" is is a special case of the +state monad, where a value representing the store is passed as the state value. + +# Lies, Damned Lies, and Abstractions + +We mentioned that, technically, we didn't need monads for Guix. Indeed, many +(now deprecated) procedures take a store value as the argument, such as +`build-expression->derivation`. However, using monads both helps ensure purity +and simply looks nicer. + +`build-expression->derivation`, being deprecated, should never of course be +used. For one thing, it uses the "quoted build expression" style, rather than +G-expressions (we'll discuss gexps another time). The best way to create a +derivation from some basic build code is to use the new-fangled +`gexp->derivation` procedure: + +```scheme +(use-modules (guix gexp) + (gnu packages irc)) + +(define symlink-irssi + (gexp->derivation "link-to-irssi" + #~(symlink #$(file-append irssi "/bin/irssi") #$output))) +;; #<procedure 7fddcc7b81e0 at guix/gexp.scm:1180:2 (state)> +``` + +You don't have to understand the `#~(...)` form yet, only everything surrounding +it. We can see that this `gexp->derivation` returns a procedure taking the +initial state (store), just like our `%state-monad` procedures did, and like we +used `run-with-state` to pass the initial state to a `%state-monad` monadic +value, we use our old friend `run-with-store` when we have a `%store-monad` +monadic value! + +```scheme +(define symlink-irssi-drv + (with-store store + (run-with-store store + symlink-irssi))) +;; #<derivation /gnu/store/q7kwwl4z6psifnv4di1p1kpvlx06fmyq-link-to-irssi.drv => /gnu/store/6a94niigx4ii0ldjdy33wx9anhifr25x-link-to-irssi 7fddb7ef52d0> +``` + +Let's just check this derivation is as expected by reading the code from the +builder script. + +```scheme +(define symlink-irssi-builder + (list-ref (derivation-builder-arguments symlink-irssi-drv) 1)) + +(call-with-input-file symlink-irssi-builder + (lambda (port) + (read port))) + +;; (symlink +;; "/gnu/store/hrlmypx1lrdjlxpkqy88bfrzg5p0bn6d-irssi-1.4.3/bin/irssi" +;; ((@ (guile) getenv) "out")) +``` + +And indeed, it symlinks the `irssi` binary to the output path. Some other, +higher-level, monadic procedures include `interned-file`, which copies a file +from outside the store into it, and `text-file`, which copies some text into it. +Generally, these procedures aren't used, as there are higher-level procedures +that perform similar functions (which we will discuss later), but for the sake +of this blog post, here's an example: + +```scheme +(with-store store + (run-with-store store + (text-file "unmatched-paren" + "( <paren@HIDDEN>"))) +;; "/gnu/store/v6smacxvdk4yvaa3s3wmd54lixn1dp3y-unmatched-paren" +``` + +# Conclusion + +What have we learned about monads? The key points we can take away are: + +1. Monads are a way of composing together procedures and values that are wrapped + in containers that give them extra context, like `maybe` values. +2. Guix provides a high-level monad API that compensates for Guile's lack of + strong types or an interface-like system. +3. This API provides the state monad, which allows you to thread state through + procedures such that you can pretend it doesn't exist. +4. Guix uses the store monad frequently to thread a store connection through + procedures that need it. +5. The store monad is really just the state monad in disguise, where the state + value is used to thread the store object through monadic procedures. + +If you've read this post in its entirety but still don't yet quite get it, don't +worry. Try to modify and tinker about with the examples, and hopefully it will +all click eventually! + +#### About GNU Guix + +[GNU Guix](https://guix.gnu.org) is a transactional package manager and +an advanced distribution of the GNU system that [respects user +freedom](https://www.gnu.org/distros/free-system-distribution-guidelines.html). +Guix can be used on top of any system running the Hurd or the Linux +kernel, or it can be used as a standalone operating system distribution +for i686, x86_64, ARMv7, AArch64 and POWER9 machines. + +In addition to standard package management features, Guix supports +transactional upgrades and roll-backs, unprivileged package management, +per-user profiles, and garbage collection. When used as a standalone +GNU/Linux distribution, Guix offers a declarative, stateless approach to +operating system configuration management. Guix is highly customizable +and hackable through [Guile](https://www.gnu.org/software/guile) +programming interfaces and extensions to the +[Scheme](http://schemers.org) language. base-commit: fe113595b6f7d8a1e1a0b814521f02783f9209c3 -- 2.39.1
guix-patches@HIDDEN
:bug#61214
; Package guix-patches
.
Full text available.Received: (at 61214) by debbugs.gnu.org; 3 Feb 2023 06:35:31 +0000 From debbugs-submit-bounces <at> debbugs.gnu.org Fri Feb 03 01:35:31 2023 Received: from localhost ([127.0.0.1]:36356 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>) id 1pNpfX-0004VY-0c for submit <at> debbugs.gnu.org; Fri, 03 Feb 2023 01:35:31 -0500 Received: from knopi.disroot.org ([178.21.23.139]:58286) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <paren@HIDDEN>) id 1pNpfV-0004VL-FT for 61214 <at> debbugs.gnu.org; Fri, 03 Feb 2023 01:35:30 -0500 Received: from localhost (localhost [127.0.0.1]) by disroot.org (Postfix) with ESMTP id 8EBDD41302; Fri, 3 Feb 2023 07:35:27 +0100 (CET) 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 3XK_nlS4eOuQ; Fri, 3 Feb 2023 07:35:26 +0100 (CET) Content-Type: multipart/signed; boundary=5b0889fd4b0853fbf520e065780abe13f3f409ed67b721a5f979577b24c7; micalg=pgp-sha512; protocol="application/pgp-signature" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=disroot.org; s=mail; t=1675406126; bh=Rt7nr0vC0dCBj2ImQSttnu6dBUrNm0APA5a4bcoMJAA=; h=Date:Subject:From:To:References:In-Reply-To; b=mHWhFcB0hBHkcRsjkfaFKi2KUN2UZUjiNoYqKbMce4iH3KjDexB+P5iaoU1ikMxWF smsa/Jm2LExY5LNSZJmhnTyXdOODUzZtOF3g/N0bEV/+Tk8Tdq/iW+Nhy9kCarrZnf xO18Xn/0X/tWIY0+SV1ELJJaXzWBA+j734O7zUOsXS4sbVjv/AarvsJrc2g/OGgJ55 cqDEMnTYg6rqpSMzj9U0PK54DDjXdnmhZndY9xzM6IzcQdku/dJEZ4/y2e6C4mbaYs 9GsBxe00OBON1PDFPKnVdN7Mv2xoDlekGhaw66xGuOpSltgoCsRxg0S1PeT41bkFUX v923y0c54fUlA== Date: Fri, 03 Feb 2023 06:35:07 +0000 Message-Id: <CQ8PWCCHN8SF.16CVVASV7A5MN@guix-framework> Subject: Re: [bug#61214] run-with-state and run-with-store From: "(" <paren@HIDDEN> To: "Feng Shu" <tumashu@HIDDEN>, <61214 <at> debbugs.gnu.org> References: <20230201172821.3072-1-paren@HIDDEN> <87a61vtsuw.fsf@HIDDEN> In-Reply-To: <87a61vtsuw.fsf@HIDDEN> X-Spam-Score: 0.1 (/) X-Debbugs-Envelope-To: 61214 X-BeenThere: debbugs-submit <at> debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: <debbugs-submit.debbugs.gnu.org> List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe> List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/> List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org> List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help> List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe> Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org> X-Spam-Score: -0.9 (/) --5b0889fd4b0853fbf520e065780abe13f3f409ed67b721a5f979577b24c7 Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Heya, On Fri Feb 3, 2023 at 1:55 AM GMT, Feng Shu wrote: > For my poor English, I do not understand this well, does this mean > "run-with-store will call run-with-state, we just use run-with-store > generally." or "run-with-store is similer with run-with-state, they can > replace each other"? Oops :) It's supposed to mean something like: > Just like we use `run-with-state` to pass an initial state to a monadic > value that uses `%state-monad`, we use our old friend `run-with-store` > when the value uses the `%store-monad`. Since it's unclear at present, I'll change it in a moment. -- ( --5b0889fd4b0853fbf520e065780abe13f3f409ed67b721a5f979577b24c7 Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQGzBAABCgAdFiEE6Vh10NblKE5doNlW7ImHg/nqI20FAmPcqxwACgkQ7ImHg/nq I21+bwwAje7bfOgah+dx8y0SHZRz5W+KnOWcuczRDjAmFb6XiFiNbsthcYGc6tdJ v+VkZ1vXk0eCjyADn+/s/Sgy+MVqt+tmaZoYSDRi5ExJcRU9CPecdf05LQSveViH nDJw/2I5MgJ8fkCFtH0jwTLG4XdmCEslUfNyiwEIyuQpCR65p9W3f4kHKrJb6qVp 3c+4khCTcM9S/wtdQiE+P7LcbczQlWZ0HG0iSYXnhGrBY08Hh36mlf1gF0x1g0cT d8QNmPlUM1ka70NqggT6i7d9jHy3EkhFg97iJx1FkbfA2FyP55YSLnAWg25qpepf va5DIMW//PRkagEl2LnGTezgdDnzInXriDOHP2oUoWZZk7l2vyQ7WeETFRMaV/2r k7M9BeSHjnuMGEJnm/oRsWZj8mE4yWME2EKrgHev/RodVcfzdBIzIAKG11cZLIOW zAChRBdGpT6wJ+VztmrcBe8kuxZuDCDFyJb5Jic2j22EyqCnLSob9qcI1lywr1oZ XjmuPhLE =/Kq2 -----END PGP SIGNATURE----- --5b0889fd4b0853fbf520e065780abe13f3f409ed67b721a5f979577b24c7--
guix-patches@HIDDEN
:bug#61214
; Package guix-patches
.
Full text available.Received: (at 61214) by debbugs.gnu.org; 3 Feb 2023 05:31:23 +0000 From debbugs-submit-bounces <at> debbugs.gnu.org Fri Feb 03 00:31:23 2023 Received: from localhost ([127.0.0.1]:36297 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>) id 1pNofS-0002NL-E5 for submit <at> debbugs.gnu.org; Fri, 03 Feb 2023 00:31:22 -0500 Received: from mail.envs.net ([5.199.136.28]:39188) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <iyzsong@HIDDEN>) id 1pNofQ-0002N2-8H for 61214 <at> debbugs.gnu.org; Fri, 03 Feb 2023 00:31:21 -0500 Received: from localhost (mail.envs.net [127.0.0.1]) by mail.envs.net (Postfix) with ESMTP id 5B35E38A01E9; Fri, 3 Feb 2023 05:31:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=envs.net; s=modoboa; t=1675402278; bh=zm3SyPWiwEzgHRlnZ5ZjqKoBDeSmYhZvd2nenacFv8w=; h=From:To:Cc:Subject:References:Date:In-Reply-To:From; b=lI/VJqFrY3J7/uB2MTSC3pc8HmLIR7plX9lHpnL9lV5chCJcGh7HNgvuvKItBIww/ 5O3+MRRzKrLkSNJHIW7NSpSFVTHbhTVAZzbUdW+fQCqy284/Gox2A6iy+RdZPtp23q wwP5Of1F4QmDEO4dCwxrrwv9g3o5JxPa3VMcVPJs+X4xEkxQ4LT+2aDSoogcsblazk NbiqcfplUtBtZ9k/cVTU6qakV1NIIC/2ZTYsnDxcTyY1RVQQC/dhp4ugjgxQEZFlJE GU9EE/2F6a5u6fduwiGS02QHOTBNMNSlSE759vUpHnzRFdcIGdzf6ysMph8q/5K5FY OjHIl9GXA0JReFQw4u+hsNM6n4gIw9m9ugRonyBeEzTRTGne4E2NE6MQjvL3mKin1b tCUMqFhp7J+IWVBCUk4GKco27V04zRTltDw+knSZAfD+bikUe12UdMfnpTyxUuOhwL kHAtzLWWPsGgRe++4wgdmQyTaSMmJfS9cIoQYPCxfej27s1P/IXfZbm/rFRPuoWKF5 BZG24946liTPlqetfS3grz1LpFgTxbhlWmQ02Acd1tb76ze9PljsrgJdlJs1qp7bGU u4ny6PLBcwL04Rhqqc486dKpmwyCiLKYBUF72lDrCHLuio951vbbBkIgNLCPVAyXBT W0s29OdOKW0iXV9x4rxJItn0= X-Virus-Scanned: Debian amavisd-new at mail.envs.net Received: from mail.envs.net ([127.0.0.1]) by localhost (mail.envs.net [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id E55mVxCe4Vdc; Fri, 3 Feb 2023 05:31:15 +0000 (UTC) Received: from localhost (unknown [182.150.116.135]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mail.envs.net (Postfix) with ESMTPSA; Fri, 3 Feb 2023 05:31:14 +0000 (UTC) Received: from localhost (localhost [local]) by localhost (OpenSMTPD) with ESMTPA id 49ed4355; Fri, 3 Feb 2023 05:31:28 +0000 (UTC) From: =?utf-8?B?5a6L5paH5q2m?= <iyzsong@HIDDEN> To: Feng Shu <tumashu@HIDDEN> Subject: Re: bug#61214: [PATCH guix-artwork] website: posts: Add Dissecting Guix, Part 2: The Store Monad. References: <20230201172821.3072-1-paren@HIDDEN> <87a61vtsuw.fsf@HIDDEN> Date: Fri, 03 Feb 2023 13:31:28 +0800 In-Reply-To: <87a61vtsuw.fsf@HIDDEN> (Feng Shu's message of "Fri, 03 Feb 2023 09:55:51 +0800") Message-ID: <87wn4zmi1b.fsf_-_@HIDDEN> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain X-Spam-Score: 0.0 (/) X-Debbugs-Envelope-To: 61214 Cc: 61214 <at> debbugs.gnu.org X-BeenThere: debbugs-submit <at> debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: <debbugs-submit.debbugs.gnu.org> List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe> List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/> List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org> List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help> List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe> Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org> X-Spam-Score: -1.0 (-) Feng Shu <tumashu@HIDDEN> writes: >> And to pass this initial state, we used `run-with-state`. The equivalent for working with >> the store is our old friend `run-with-store`! > > For my poor English, I do not understand this well, does this mean > "run-with-store will call run-with-state, we just use run-with-store > generally." or "run-with-store is similer with run-with-state, they can > replace each other"? Well, "The equivalent for working with the store ..." should read as "And to pass this initial store, we should use `run-with-store`". > We can see that this `gexp->derivation` returns a procedure taking the > initial state (store), just like our `%state-monad` procedures did. > And to pass this initial state, we used `run-with-state`. Maybe "And to pass the initial state for `%state-monad`, we used `run-with-state`. So to pass the initial for `%store-monad`, we will use `run-with-store`!" is better?
guix-patches@HIDDEN
:bug#61214
; Package guix-patches
.
Full text available.Received: (at 61214) by debbugs.gnu.org; 3 Feb 2023 01:56:12 +0000 From debbugs-submit-bounces <at> debbugs.gnu.org Thu Feb 02 20:56:12 2023 Received: from localhost ([127.0.0.1]:36197 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>) id 1pNlJD-0004xC-R1 for submit <at> debbugs.gnu.org; Thu, 02 Feb 2023 20:56:12 -0500 Received: from m12.mail.163.com ([220.181.12.196]:40512) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <tumashu@HIDDEN>) id 1pNlJA-0004wd-VI for 61214 <at> debbugs.gnu.org; Thu, 02 Feb 2023 20:56:10 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:Subject:Date:Message-ID:MIME-Version: Content-Type; bh=brGRygr6GbQPbMXXRIrX1nBrSk//6+1ueCkOCmhXkxw=; b=nDyPymtR2b7Sg5Hun8V/6QWMkIJJPZ2neg8LjB9FAfuoDAyK/EmVJKH1iQM2ZA ACYRuvxwHK0818m40i4Pq5RkrXGsFsri6gnXOjDiqzR6cozMZT5jB5w5/DRg4Zei lK+GHjhZXZVbc7XNYZcREMGyr9UmXPEbQyAZfcOdAFsso= Received: from Tumashu (unknown [218.92.14.78]) by zwqz-smtp-mta-g0-0 (Coremail) with SMTP id _____wBXXA6nadxj7W6RCg--.52027S2; Fri, 03 Feb 2023 09:55:53 +0800 (CST) From: Feng Shu <tumashu@HIDDEN> To: 61214 <at> debbugs.gnu.org Subject: run-with-state and run-with-store Date: Fri, 03 Feb 2023 09:55:51 +0800 Message-ID: <87a61vtsuw.fsf@HIDDEN> User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 Content-Type: text/plain X-CM-TRANSID: _____wBXXA6nadxj7W6RCg--.52027S2 X-Coremail-Antispam: 1Uf129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7v73 VFW2AGmfu7bjvjm3AaLaJ3UbIYCTnIWIevJa73UjIFyTuYvjxUOhFIUUUUU X-Originating-IP: [218.92.14.78] X-CM-SenderInfo: 5wxpt2lkx6il2tof0z/xtbCghUL1GD-y-JuIQAAsh X-Spam-Score: 0.0 (/) X-Debbugs-Envelope-To: 61214 X-BeenThere: debbugs-submit <at> debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: <debbugs-submit.debbugs.gnu.org> List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe> List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/> List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org> List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help> List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe> Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org> X-Spam-Score: -1.0 (-) > And to pass this initial state, we used `run-with-state`. The equivalent for working with > the store is our old friend `run-with-store`! For my poor English, I do not understand this well, does this mean "run-with-store will call run-with-state, we just use run-with-store generally." or "run-with-store is similer with run-with-state, they can replace each other"? --
guix-patches@HIDDEN
:bug#61214
; Package guix-patches
.
Full text available.Received: (at 61214) by debbugs.gnu.org; 2 Feb 2023 15:04:24 +0000 From debbugs-submit-bounces <at> debbugs.gnu.org Thu Feb 02 10:04:24 2023 Received: from localhost ([127.0.0.1]:35503 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>) id 1pNb8S-0005SG-1k for submit <at> debbugs.gnu.org; Thu, 02 Feb 2023 10:04:24 -0500 Received: from knopi.disroot.org ([178.21.23.139]:42318) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <paren@HIDDEN>) id 1pNb8Q-0005S7-A1 for 61214 <at> debbugs.gnu.org; Thu, 02 Feb 2023 10:04:22 -0500 Received: from localhost (localhost [127.0.0.1]) by disroot.org (Postfix) with ESMTP id 933B6414B6; Thu, 2 Feb 2023 16:04:20 +0100 (CET) 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 UTF8SMTP id Xf8ucfV0h_zz; Thu, 2 Feb 2023 16:04:19 +0100 (CET) Content-Type: multipart/signed; boundary=ce8736b403e3a99cacd543fcade23abccfd3e52faac0f45b306219803a57; micalg=pgp-sha512; protocol="application/pgp-signature" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=disroot.org; s=mail; t=1675350259; bh=B5G041SvwAB8llCMXmsUVvhoSvypA1q/2c+MTvyZEec=; h=Date:Cc:Subject:From:To:References:In-Reply-To; b=luiUtOMeY1hzHdz9fmi6/HM0EecVTtbzVC9zBrq0KUdDEhWFq6KSISkycL/1Oubi8 KkEldsBMJfjnDTa/fOuVlvzZKHbCGL9B6HWrvFrEaMZssQe0fM28hf0B8gZOq9Rczd 873+eDUNEGhQaUFaGyTdoPZM/RRGNiVA8TbySXaEk+078RKMGLuF/00c1/XvxhXA69 cFLV63PJPTniEFFZI1y7QP/TJYK7IrSqdcVeewWKMRrfnnvnvbU03mwGL1idfO2dyY BFD/GlmiLCLK1Z6giK59ULaRUvEBTKV6PIGTT2/FkjAjwawKWE3sYNpPN9/c1g/2ui AIH3q9KhtFOuw== Date: Thu, 02 Feb 2023 15:04:17 +0000 Message-Id: <CQ863NCSSW8H.2DPB4PHPV6ZFO@guix-framework> Subject: Re: [PATCH guix-artwork v2] website: posts: Add Dissecting Guix, Part 2: The Store Monad. From: "(" <paren@HIDDEN> To: "(" <paren@HIDDEN>, <61214 <at> debbugs.gnu.org> References: <20230202150028.3005-1-paren@HIDDEN> In-Reply-To: <20230202150028.3005-1-paren@HIDDEN> X-Spam-Score: 0.1 (/) X-Debbugs-Envelope-To: 61214 Cc: zimoun <zimon.toutoune@HIDDEN> X-BeenThere: debbugs-submit <at> debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: <debbugs-submit.debbugs.gnu.org> List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe> List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/> List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org> List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help> List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe> Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org> X-Spam-Score: -0.9 (/) --ce8736b403e3a99cacd543fcade23abccfd3e52faac0f45b306219803a57 Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 CC zimoun. -- ( --ce8736b403e3a99cacd543fcade23abccfd3e52faac0f45b306219803a57 Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQGzBAABCgAdFiEE6Vh10NblKE5doNlW7ImHg/nqI20FAmPb0PMACgkQ7ImHg/nq I21cBAwAou1pv49PUTDzkuNtq/UjX6ytQUPGZANzyFvQGf0GLL9HO6mkBUnDVLut NO+puzMmq31tvA+s9/FslSsYUy5vWnhPXk4r/TLmhWlBjJyHbzymANy6xiwyHzyf lpeyBpYtatgxkoRDr9IqRm6cOUiPfShjiJD4RAdBPfcy956vlHffCgSe7mTyiSqK PriSCXDH/iCPdAKJIYjv+kOk9xoTxryftpwXr3OY1hnocbGD5J9N2eyQVsWknDow OqSOD+8Tjocaefv8E2zd9rKLFP5XrRNSazYauotMCj0nyKoX+lm/XPmjQ+GwNanu 0dCLVF5Mm2XOvQ9Ml2lejAgEhzUmVLWY5JEqmxT2Cjz4za6ELJfdjxf49AQJB60c B22zXjx7SEPWrzlhnjfqTVdePNDSgBj3nBp4SriuMcMGqTOC7FwtjJpZencN9yHj wVb5Ou4y6GGl4P3wUszr3xjTZV5g7kYmUaiY2UMCSPrZlGmWb5/4M4LHr37QaUrr pONUITHn =24cR -----END PGP SIGNATURE----- --ce8736b403e3a99cacd543fcade23abccfd3e52faac0f45b306219803a57--
guix-patches@HIDDEN
:bug#61214
; Package guix-patches
.
Full text available.Received: (at 61214) by debbugs.gnu.org; 2 Feb 2023 15:00:48 +0000 From debbugs-submit-bounces <at> debbugs.gnu.org Thu Feb 02 10:00:48 2023 Received: from localhost ([127.0.0.1]:35499 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>) id 1pNb4x-0005Mo-Fb for submit <at> debbugs.gnu.org; Thu, 02 Feb 2023 10:00:48 -0500 Received: from knopi.disroot.org ([178.21.23.139]:39800) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <paren@HIDDEN>) id 1pNb4t-0005Mb-GX for 61214 <at> debbugs.gnu.org; Thu, 02 Feb 2023 10:00:45 -0500 Received: from localhost (localhost [127.0.0.1]) by disroot.org (Postfix) with ESMTP id 59584414C9; Thu, 2 Feb 2023 16:00:40 +0100 (CET) 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 UTF8SMTP id cmmoRC_tipWE; Thu, 2 Feb 2023 16:00:36 +0100 (CET) From: "(" <paren@HIDDEN> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=disroot.org; s=mail; t=1675350036; bh=md49FmF5dJcrt0c+od+Om1hpIO3h4xwJj6RmNBYPjuM=; h=From:To:Cc:Subject:Date; b=dT8jKdaaQqbfNcHRIorjKdnKrKs0SEaonXl4p82tiDGPdOjP49MptPYaxKAGLjPyf JB8cg0ocSkY+uAcID/EPsFxdnbbNdwgOecB9+/2nPY1XQTb0H2zlzVCymag5j36XyW 3Zdeq+L89QU5PaRhHOXqoA4PU7yUy2ho/8JYgwnOa6fhudLuOXeSUeb6efowkbrYXt lpflYvsdw1OaNI/jiicIQyWkvmvGcDeVvCWscWiDSPBg4XH7+/YmI4kn70bXaU+spv QEwvQsB6Ao6L21mdmzPg6F8NHXPq54ki5FKp8NMUR5yveLOrUr4wqi6LDiBBWia0ct YhIUWw+uRa7Qw== To: 61214 <at> debbugs.gnu.org Subject: [PATCH guix-artwork v2] website: posts: Add Dissecting Guix, Part 2: The Store Monad. Date: Thu, 2 Feb 2023 15:00:28 +0000 Message-Id: <20230202150028.3005-1-paren@HIDDEN> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Score: 0.0 (/) X-Debbugs-Envelope-To: 61214 Cc: "\(" <paren@HIDDEN> X-BeenThere: debbugs-submit <at> debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: <debbugs-submit.debbugs.gnu.org> List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe> List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/> List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org> List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help> List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe> Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org> X-Spam-Score: -1.0 (-) * website/posts/dissecting-guix-2-store-monad.md: New blog post. --- Heya, Taking into account zimoun's critiques, here's a v2. * Say 'dynamically typed' rather than 'not statically typed'. * Say 'compose' rather than 'chain'. * Mention that the first #F in the definition of NOTHING could be any other value. * Use MATCH in the second implementations of REMOVE-A and REMOVE-B. * Note that we've only implemented >>= for <MAYBE>. * Add a more formal description of a monad to 'The Ties That `>>=` Us'. * Mention %IDENTITY-MONAD, %STATE-MONAD, and %STORE-MONAD at the start of 'New Wheel, Old Wheel'. * Take the reader through the monad laws in 'New Wheel, Old Wheel'. * Show what the first MLET* example expands to. * Fix typo by changing MLIST to LISTM. * Go into slightly more detail about what the monadic higher-order list procedure variants do. * Add missing %STATE-MONAD to the MLET* in STATE-SEQ. * Move explanation of the Fibonacci example before the code snippet, and clarify it. * Remove example for BUILD-EXPRESSION->DERIVATION. .../posts/dissecting-guix-2-store-monad.md | 555 ++++++++++++++++++ 1 file changed, 555 insertions(+) create mode 100644 website/posts/dissecting-guix-2-store-monad.md diff --git a/website/posts/dissecting-guix-2-store-monad.md b/website/posts/dissecting-guix-2-store-monad.md new file mode 100644 index 0000000..7c7c074 --- /dev/null +++ b/website/posts/dissecting-guix-2-store-monad.md @@ -0,0 +1,556 @@ +title: Dissecting Guix, Part 2: The Store Monad +date: TBC +author: ( +tags: Dissecting Guix, Functional package management, Programming interfaces, Scheme API +--- +Hello again! + +In [the last post](https://guix.gnu.org/en/blog/2023/dissecting-guix-part-1-derivations/), +we briefly mentioned the `with-store` and `run-with-store` APIs. Today, we'll +be looking at those in further detail, along with the related monad API and the +`%store-monad`! + +Monads are a little hard to explain, and from a distance, they seem more than a +bit confusing. So, I want you to erase monads from your mind for now. We'll +come back to them later. + +# Yes, No, Maybe So + +Let's instead implement another M of functional programming, _`maybe`_ values, +representing a value that may or may not exist. `maybe` is a very common +feature of strongly-typed functional languages, and you'll see it all over the +place in Haskell and OCaml code. However, Guile is dynamically typed, so we +usually use ad-hoc `#f`s and `'()`s for null values instead of a proper +"optional" value. + +Just for fun, though, we'll implement a proper `maybe` in Guile. Fire up that +REPL once again, and let's import a bunch of modules that we'll need: + +```scheme +(use-modules (ice-9 match) + (srfi srfi-9)) +``` + +We'll implement `maybe` as a record with two fields, `is?` and `value`. If the +value contains something, `is?` will be `#t` and `value` will contain the thing +in question, and if it's empty, `is?`'ll be `#f`. + +```scheme +(define-record-type <maybe> + (make-maybe is? value) + maybe? + (is? maybe-is?) + (value maybe-value)) +``` + +Now we'll define constructors for the two possible states: + +```scheme +(define (something value) + (make-maybe #t value)) + +(define (nothing) + (make-maybe #f #f)) ;the value here doesn't matter; we'll just use #f +``` + +And make some silly functions that return optional values: + +```scheme +(define (remove-a str) + (if (eq? (string-ref str 0) #\a) + (something (substring str 1)) + (nothing))) + +(define (remove-b str) + (if (eq? (string-ref str 0) #\b) + (something (substring str 1)) + (nothing))) + +(remove-a "ahh") +;; #<<maybe> is?: #t value: "hh"> + +(remove-a "ooh") +;; #<<maybe> is?: #f value: #f> + +(remove-b "bad") +;; #<<maybe> is?: #t value: "ad"> +``` + +But what if we want to compose the results of these functions? + +# Keeping Your Composure + +As you might have guessed, this is not fun. Cosplaying as a compiler backend +typically isn't. + +```scheme +(let ((t1 (remove-a "abcd"))) + (if (maybe-is? t1) + (remove-b (maybe-value t1)) + (nothing))) +;; #<<maybe> is?: #t value: "cd"> + +(let ((t1 (remove-a "bbcd"))) + (if (maybe-is? t1) + (remove-b (maybe-value t1)) + (nothing))) +;; #<<maybe> is?: #f value: #f> +``` + +I can almost hear the heckling. Even worse, composing three: + +```scheme +(let* ((t1 (remove-a "abad")) + (t2 (if (maybe-is? t1) + (remove-b (maybe-value t1)) + (nothing)))) + (if (maybe-is? t2) + (remove-a (maybe-value t2)) + (nothing))) +;; #<<maybe> is?: #t value: "d"> +``` + +So, how do we go about making this more bearable? Well, one way could be to +make `remove-a` and `remove-b` accept `maybe`s: + +```scheme +(define (remove-a ?str) + (match ?str + (($ <maybe> #t str) + (if (eq? (string-ref str 0) #\a) + (something (substring str 1)) + (nothing))) + (_ (nothing)))) + +(define (remove-b ?str) + (match ?str + (($ <maybe> #t str) + (if (eq? (string-ref str 0) #\b) + (something (substring str 1)) + (nothing))) + (_ (nothing)))) +``` + +Not at all pretty, but it works! + +``` +(remove-b (remove-a (something "abc"))) +;; #<<maybe> is?: #t value: "c"> +``` + +Still, our procedures now require quite a bit of boilerplate. Might there be a +better way? + +# The Ties That `>>=` Us + +First of all, we'll revert to our original definitions of `remove-a` and +`remove-b`, that is to say, the ones that take a regular value and return a +`maybe`. + +```scheme +(define (remove-a str) + (if (eq? (string-ref str 0) #\a) + (something (substring str 1)) + (nothing))) + +(define (remove-b str) + (if (eq? (string-ref str 0) #\b) + (something (substring str 1)) + (nothing))) +``` + +What if tried introducing higher-order procedures (procedures that accept other +procedures as arguments) into the equation? Because we're functional +programmers and we have an unhealthy obsession with that sort of thing. + +```scheme +(define (maybe-chain maybe proc) + (if (maybe-is? maybe) + (proc (maybe-value maybe)) + (nothing))) + +(maybe-chain (something "abc") + remove-a) +;; #<<maybe> is?: #t value: "bc"> + +(maybe-chain (nothing) + remove-a) +;; #<<maybe> is?: #f value: #f> +``` + +It lives! To make it easier to compose procedures like this, we'll define a +macro that allows us to perform any number of sequenced operations with only one +composition form: + +```scheme +(define-syntax maybe-chain* + (syntax-rules () + ((_ maybe proc) + (maybe-chain maybe proc)) + ((_ maybe proc rest ...) + (maybe-chain* (maybe-chain maybe proc) + rest ...)))) + +(maybe-chain* (something "abad") + remove-a + remove-b + remove-a) +;; #<<maybe> is?: #t value: "d"> +``` + +Congratulations, you've just implemented the `bind` operation, commonly written +as `>>=`, for our `maybe` type. And it turns out that a monad is just any +container-like value for which `>>=` (along with another procedure called +`return`, which wraps a given value in the simplest possible form of a monad) +has been implemented. + +A more formal definition would be that a monad is a mathematical object composed +of three parts: a type, a `bind` function, and a `return` function. So, how do +monads relate to Guix? + +# New Wheel, Old Wheel + +Now that we've reinvented the wheel, we'd better learn to use the original +wheel. Guix provides a generic, high-level monads API, along with the two +generic monads `%identity-monad` and `%state-monad`, and the Guix-specific +`%store-monad`. Since `maybe` is not one of them, let's integrate our version +into the Guix monad system! + +First we'll make the API available: + +```scheme +(use-modules (guix monads)) +``` + +To define a monad's API in Guix, we simply use the `define-monad` macro, and +provide two procedures: `bind`, and `return`. + +```scheme +(define-monad %maybe-monad + (bind maybe-chain) + (return something)) +``` + +`bind` is just the procedure that we use to compose monadic procedure calls +together, and `return` is the procedure that wraps values in the most basic form +of the monad. A properly implemented `bind` and `return` must follow these +laws: + +1. `(bind (return x) proc)` must be equivalent to `(proc x)`. +2. `(bind monad return)` must be equivalent to just `monad`. +3. `(bind (bind monad proc-1) proc-2)` must be equivalent to + `(bind monad (lambda (x) (bind (proc-1 x) proc-2)))`. + +Let's verify that our `maybe-chain` and `something` procedures adhere to the +monad laws: + +```scheme +(define (mlaws-proc-1 x) + (something (+ x 1))) + +(define (mlaws-proc-2 x) + (something (+ x 2))) + +;; First law: the left identity. +(equal? (maybe-chain (something 0) + mlaws-proc-1) + (mlaws-proc-1 0)) +;; #t + +;; Second law: the right identity. +(equal? (maybe-chain (something 0) + something) + (something 0)) +;; #t + +;; Third law: associativity. +(equal? (maybe-chain (maybe-chain (something 0) + mlaws-proc-1) + mlaws-proc-2) + (maybe-chain (something 0) + (lambda (x) + (maybe-chain (mlaws-proc-1 x) + mlaws-proc-2)))) +;; #t +``` + +Now that we know they're valid, we can use the `with-monad` macro to tell Guix +to use these specific implementations of `bind` and `return`, and the `>>=` +macro to thread monads through procedure calls! + +```scheme +(with-monad %maybe-monad + (>>= (something "aabbc") + remove-a + remove-a + remove-b + remove-b)) +;; #<<maybe> is?: #t value: "c"> +``` + +We can also now use `return`: + +```scheme +(with-monad %maybe-monad + (return 32)) +;; #<<maybe> is?: #t value: 32> +``` + +But Guix provides many higher-level APIs than `>>=` and `return`, as we will +see. There's `mbegin`, which evaluates monadic expressions without binding them +to symbols, returning the last one: + +```scheme +(mbegin %maybe-monad + (remove-a "abc")) +;; #<<maybe> is?: #t value: "bc"> +``` + +And there's `mlet` and `mlet*`, which can bind them, and are essentially +equivalent to a chain of `(>>= MEXPR (lambda (BINDING) ...))`: + +```scheme +;; This is equivalent... +(mlet* %maybe-monad ((str -> "abad") ;non-monadic binding uses the -> symbol + (str1 (remove-a str)) + (str2 (remove-b str))) + (remove-a str)) +;; #<<maybe> is?: #t value: "d"> + +;; ...to this: +(with-monad %maybe-monad + (>>= (return "abad") + (lambda (str) + (remove-a str)) + (lambda (str1) + (remove-b str)) + (lambda (str2) + (remove-a str)))) +``` + +Various abstractions over these two exist too, such as `mwhen` (a `when` plus an +`mbegin`), `munless` (an `unless` plus an `mbegin`), and `mparameterize` +(dynamically-scoped value rebinding, like `parameterize`, in a monadic context). +`lift` takes a procedure and a monad and creates a new procedure that returns +a monadic value. + +There are also APIs for manipulating lists wrapped in monads; `listm` creates +such a list, `sequence` turns a list of monads into a list wrapped in a monad, +and the `anym`, `mapm`, and `foldm` procedures are like their non-monadic +equivalents, except that they return lists wrapped in monads. + +This is all well and good, you may be thinking, but why does Guix need a monad +API? The answer is technically that it doesn't. But building on the monad API +makes a lot of things much easier, and to learn why, we're going to look at one +of Guix's built-in monads. + +# In a State + +Guix implements a monad called `%state-monad`, and it works with single-argument +procedures returning two values. Behold: + +```scheme +(with-monad %state-monad + (return 33)) +;; #<procedure 21dc9a0 at <unknown port>:1106:22 (state)> +``` + +The `run-with-state` value turns this procedure into an actually useful value, +or, rather, two values: + +```scheme +(run-with-state (with-monad %state-monad (return 33)) + (list "foo" "bar" "baz")) +;; 33 +;; ("foo" "bar" "baz") +``` + +What can this actually do for us, though? Well, it gets interesting if we do +some `>>=`ing: + +```scheme +(define state-seq + (mlet* %state-monad ((number (return 33))) + (state-push number))) +result +;; #<procedure 7fcb6f466960 at <unknown port>:1484:24 (state)> + +(run-with-state state-seq (list 32)) +;; (32) +;; (33 32) + +(run-with-state state-seq (list 30 99)) +;; (30 99) +;; (33 30 99) +``` + +What is `state-push`? It's a monadic procedure for `%state-monad` that takes +whatever's currently in the first value (the primary value) and pushes it onto +the second value (the state value), which is assumed to be a list, returning the +old state value as the primary value and the new list as the state value. + +So, when we do `(run-with-state result (list 32))`, we're passing `(list 32)` as +the initial state value, and then the `>>=` form passes that and `33` to +`state-push`. What `%state-monad` allows us to do is thread together some +procedures that require some kind of state, while pretending the state isn't +there, and then retrieve both the final state and the result at the end! + +If you're a bit confused, don't worry. We'll write some of our own +`%state-monad`-based monadic procedures and hopefully all will become clear. +Consider, for instance, the +[Fibonacci sequence](https://en.wikipedia.org/wiki/Fibonacci_number), in which +each value is computed by adding the previous two. We could use the +`%state-monad` to compute Fibonacci numbers by storing the previous number as +the primary value and the number before that as the state value: + +```scheme +(define (fibonacci-thing value) + (lambda (state) + (values (+ value state) + value))) +``` + +Now we can feed our Fibonacci-generating procedure the first value using +`run-with-state` and the second using `return`: + +```scheme +(run-with-state + (mlet* %state-monad ((starting (return 1)) + (n1 (fibonacci-thing starting)) + (n2 (fibonacci-thing n1))) + (fibonacci-thing n2)) + 0) +;; 3 +;; 2 + +(run-with-state + (mlet* %state-monad ((starting (return 1)) + (n1 (fibonacci-thing starting)) + (n2 (fibonacci-thing n1)) + (n3 (fibonacci-thing n2)) + (n4 (fibonacci-thing n3)) + (n5 (fibonacci-thing n4))) + (fibonacci-thing n5)) + 0) +;; 13 +;; 8 +``` + +This is all very nifty, and possibly useful in general, but what does this have +to do with Guix? Well, many Guix store-based operations are meant to be used +in concert with yet another monad, called the `%store-monad`. But if we look at +`(guix store)`, where `%store-monad` is defined... + +```scheme +(define-alias %store-monad %state-monad) +(define-alias store-return state-return) +(define-alias store-bind state-bind) +``` + +It was all a shallow façade! All the "store monad" is is a special case of the +state monad, where a value representing the store is passed as the state value. + +# Lies, Damned Lies, and Abstractions + +We mentioned that, technically, we didn't need monads for Guix. Indeed, many +(now deprecated) procedures take a store value as the argument, such as +`build-expression->derivation`. However, using monads both helps ensure purity +and simply looks nicer. + +`build-expression->derivation`, being deprecated, should never of course be +used. For one thing, it uses the "quoted build expression" style, rather than +G-expressions (we'll discuss gexps another time). The best way to create a +derivation from some basic build code is to use the new-fangled +`gexp->derivation` procedure: + +```scheme +(use-modules (guix gexp) + (gnu packages irc)) + +(define symlink-irssi + (gexp->derivation "link-to-irssi" + #~(symlink #$(file-append irssi "/bin/irssi") #$output))) +;; #<procedure 7fddcc7b81e0 at guix/gexp.scm:1180:2 (state)> +``` + +You don't have to understand the `#~(...)` form yet, only everything surrounding +it. We can see that this `gexp->derivation` returns a procedure taking the +initial state (store), just like our `%state-monad` procedures did. And to pass +this initial state, we used `run-with-state`. The equivalent for working with +the store is our old friend `run-with-store`! + +```scheme +(define symlink-irssi-drv + (with-store store + (run-with-store store + symlink-irssi))) +;; #<derivation /gnu/store/q7kwwl4z6psifnv4di1p1kpvlx06fmyq-link-to-irssi.drv => /gnu/store/6a94niigx4ii0ldjdy33wx9anhifr25x-link-to-irssi 7fddb7ef52d0> +``` + +Let's just check this derivation is as expected by reading the code from the +builder script. + +```scheme +(define symlink-irssi-builder + (list-ref (derivation-builder-arguments symlink-irssi-drv) 1)) + +(call-with-input-file symlink-irssi-builder + (lambda (port) + (read port))) + +;; (symlink +;; "/gnu/store/hrlmypx1lrdjlxpkqy88bfrzg5p0bn6d-irssi-1.4.3/bin/irssi" +;; ((@ (guile) getenv) "out")) +``` + +And indeed, it symlinks the `irssi` binary to the output path. Some other, +higher-level, monadic procedures include `interned-file`, which copies a file +from outside the store into it, and `text-file`, which copies some text into it. +Generally, these procedures aren't used, as there are higher-level procedures +that perform similar functions (which we will discuss later), but for the sake +of this blog post, here's an example: + +```scheme +(with-store store + (run-with-store store + (text-file "unmatched-paren" + "( <paren@HIDDEN>"))) +;; "/gnu/store/v6smacxvdk4yvaa3s3wmd54lixn1dp3y-unmatched-paren" +``` + +# Conclusion + +What have we learned about monads? The key points we can take away are: + +1. Monads are a way of composing together procedures and values that are wrapped + in containers that give them extra context, like `maybe` values. +2. Guix provides a high-level monad API that compensates for Guile's lack of + strong types or an interface-like system. +3. This API provides the state monad, which allows you to thread state through + procedures such that you can pretend it doesn't exist. +4. Guix uses the store monad frequently to thread a store connection through + procedures that need it. +5. The store monad is really just the state monad in disguise, where the state + value is used to thread the store object through monadic procedures. + +If you've read this post in its entirety but still don't yet quite get it, don't +worry. Try to modify and tinker about with the examples, and hopefully it will +all click eventually! + +#### About GNU Guix + +[GNU Guix](https://guix.gnu.org) is a transactional package manager and +an advanced distribution of the GNU system that [respects user +freedom](https://www.gnu.org/distros/free-system-distribution-guidelines.html). +Guix can be used on top of any system running the Hurd or the Linux +kernel, or it can be used as a standalone operating system distribution +for i686, x86_64, ARMv7, AArch64 and POWER9 machines. + +In addition to standard package management features, Guix supports +transactional upgrades and roll-backs, unprivileged package management, +per-user profiles, and garbage collection. When used as a standalone +GNU/Linux distribution, Guix offers a declarative, stateless approach to +operating system configuration management. Guix is highly customizable +and hackable through [Guile](https://www.gnu.org/software/guile) +programming interfaces and extensions to the +[Scheme](http://schemers.org) language. base-commit: fe113595b6f7d8a1e1a0b814521f02783f9209c3 -- 2.39.1
guix-patches@HIDDEN
:bug#61214
; Package guix-patches
.
Full text available.Received: (at submit) by debbugs.gnu.org; 2 Feb 2023 13:07:07 +0000 From debbugs-submit-bounces <at> debbugs.gnu.org Thu Feb 02 08:07:07 2023 Received: from localhost ([127.0.0.1]:32870 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>) id 1pNZIx-0001fx-HI for submit <at> debbugs.gnu.org; Thu, 02 Feb 2023 08:07:07 -0500 Received: from lists.gnu.org ([209.51.188.17]:59560) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <paren@HIDDEN>) id 1pNZIv-0001fn-W4 for submit <at> debbugs.gnu.org; Thu, 02 Feb 2023 08:07:06 -0500 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 <paren@HIDDEN>) id 1pNZIr-0001D5-TP for guix-patches@HIDDEN; Thu, 02 Feb 2023 08:07:01 -0500 Received: from knopi.disroot.org ([178.21.23.139]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from <paren@HIDDEN>) id 1pNZIq-0003na-AH for guix-patches@HIDDEN; Thu, 02 Feb 2023 08:07:01 -0500 Received: from localhost (localhost [127.0.0.1]) by disroot.org (Postfix) with ESMTP id EA23B414D0; Thu, 2 Feb 2023 14:06:54 +0100 (CET) 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 l6PU4CliUzsP; Thu, 2 Feb 2023 14:06:53 +0100 (CET) Content-Type: multipart/signed; boundary=5aad2bbd81c2ef20b5411e4f5bd014d79a981568edc23e961230cc436c1a; micalg=pgp-sha512; protocol="application/pgp-signature" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=disroot.org; s=mail; t=1675343213; bh=AjEHUpi5LMU3r/0wNL2b/FHK390scEAZm1zVjTA74jo=; h=Date:Subject:From:To:References:In-Reply-To; b=eK3lfDjTDPn5Zc5FFww+A6cdYczk3J5nlBNy7ccq5I67/cwuc3DlIS7bpaQDsFqVZ qEeWxo90ywrjKdzmFO/qp5hsQFKvKqj6a3RMQ+zv1rxj3kEgbY+8glncr2luiDO0nU bJXR44oLoBypJouVpvc2frUy2ZQHPhHNUlzhHZMvskWN0vBfoT/euL0mrVy6c86b5c 7JpdmquyyVrytITeooCNqlYJf0KUVEj9NSC491bZF9RYfnEVnwFB115i6gRx2F07Q6 PHUdFmt+lQDrM4sByJWZSTmyWT3YXxauvzXkVM61XJ22ufhOy0m17/NKk+R+wB8lh7 5Tix5rCMOjmlA== Date: Thu, 02 Feb 2023 13:06:48 +0000 Message-Id: <CQ83LP3KVPB2.31N7LK9MI3TA@guix-framework> Subject: Re: [bug#61214] [PATCH guix-artwork] website: posts: Add Dissecting Guix, Part 2: The Store Monad. From: "(" <paren@HIDDEN> To: "Simon Tournier" <zimon.toutoune@HIDDEN>, "( via Guix-patches via" <guix-patches@HIDDEN>, <61214 <at> debbugs.gnu.org> References: <20230201172821.3072-1-paren@HIDDEN> <86wn50tr9p.fsf@HIDDEN> In-Reply-To: <86wn50tr9p.fsf@HIDDEN> Received-SPF: pass client-ip=178.21.23.139; envelope-from=paren@HIDDEN; helo=knopi.disroot.org X-Spam_score_int: -19 X-Spam_score: -2.0 X-Spam_bar: -- X-Spam_report: (-2.0 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, MIME_HEADER_CTYPE_ONLY=0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Spam-Score: -1.3 (-) X-Debbugs-Envelope-To: submit X-BeenThere: debbugs-submit <at> debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: <debbugs-submit.debbugs.gnu.org> List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe> List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/> List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org> List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help> List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe> Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org> X-Spam-Score: -2.3 (--) --5aad2bbd81c2ef20b5411e4f5bd014d79a981568edc23e961230cc436c1a Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Heya, On Thu Feb 2, 2023 at 8:17 AM GMT, Simon Tournier wrote: > Nice! Some minor comments which can be trashed if they are not > helpful. :-) You raise some good points; I'll send a v2 in a moment. > Nice read of an hard topic. Well done! :-) Thank you! :) -- ( --5aad2bbd81c2ef20b5411e4f5bd014d79a981568edc23e961230cc436c1a Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQGzBAABCgAdFiEE6Vh10NblKE5doNlW7ImHg/nqI20FAmPbtWoACgkQ7ImHg/nq I238qwwAlV29WkBntaWZVrAvu5o64lhDmamoce4bAcrK0KFtyAMxJMwvrjnGjc5P FGqno9mxg1leqwhve7euLVfaKeXaFMibHAe0YTPpLAUTSO2b6YcF2bo/FLQUgCQ6 +FHzZK7EJ6muYaU9i7j4dEo0CT8LT8CndtL+AOcQZcCV0ONE6V+sP/uJs947c/xk NkgFLQR6AEfp4TYs2knf2+qsnJMbHCJaakmKks7OheMw+Sd5T1FYOGkB7l8J5iEO hmpfxUH6Emjw0Ydtosn5xMoA/8sRiMtmVaKmv3tmYe9Shox/V4gA6LpPtfnnjw4I AHKWGNv90X28Ghj9P1BMn4HSR8CH9b1PDMeSCedCEOPASN3xCDcOmMWR828tyzPv F7HJFdqC1tixT916LqbxZsyR1JYu+DX7nkX5MAD8fAjFfnR3TdGOWHsFXHG5Z0cc rZmcRMetedxve011sVNdRuLru1vwD7kiOUsmV4N+njcu0BRBoOSj/1rx68unVrQG qdRc9Prz =lfCl -----END PGP SIGNATURE----- --5aad2bbd81c2ef20b5411e4f5bd014d79a981568edc23e961230cc436c1a--
guix-patches@HIDDEN
:bug#61214
; Package guix-patches
.
Full text available.Received: (at 61214) by debbugs.gnu.org; 2 Feb 2023 13:06:58 +0000 From debbugs-submit-bounces <at> debbugs.gnu.org Thu Feb 02 08:06:58 2023 Received: from localhost ([127.0.0.1]:32866 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>) id 1pNZIo-0001fC-8V for submit <at> debbugs.gnu.org; Thu, 02 Feb 2023 08:06:58 -0500 Received: from knopi.disroot.org ([178.21.23.139]:51486) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <paren@HIDDEN>) id 1pNZIm-0001f2-6b for 61214 <at> debbugs.gnu.org; Thu, 02 Feb 2023 08:06:56 -0500 Received: from localhost (localhost [127.0.0.1]) by disroot.org (Postfix) with ESMTP id EA23B414D0; Thu, 2 Feb 2023 14:06:54 +0100 (CET) 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 l6PU4CliUzsP; Thu, 2 Feb 2023 14:06:53 +0100 (CET) Content-Type: multipart/signed; boundary=5aad2bbd81c2ef20b5411e4f5bd014d79a981568edc23e961230cc436c1a; micalg=pgp-sha512; protocol="application/pgp-signature" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=disroot.org; s=mail; t=1675343213; bh=AjEHUpi5LMU3r/0wNL2b/FHK390scEAZm1zVjTA74jo=; h=Date:Subject:From:To:References:In-Reply-To; b=eK3lfDjTDPn5Zc5FFww+A6cdYczk3J5nlBNy7ccq5I67/cwuc3DlIS7bpaQDsFqVZ qEeWxo90ywrjKdzmFO/qp5hsQFKvKqj6a3RMQ+zv1rxj3kEgbY+8glncr2luiDO0nU bJXR44oLoBypJouVpvc2frUy2ZQHPhHNUlzhHZMvskWN0vBfoT/euL0mrVy6c86b5c 7JpdmquyyVrytITeooCNqlYJf0KUVEj9NSC491bZF9RYfnEVnwFB115i6gRx2F07Q6 PHUdFmt+lQDrM4sByJWZSTmyWT3YXxauvzXkVM61XJ22ufhOy0m17/NKk+R+wB8lh7 5Tix5rCMOjmlA== Date: Thu, 02 Feb 2023 13:06:48 +0000 Message-Id: <CQ83LP3KVPB2.31N7LK9MI3TA@guix-framework> Subject: Re: [bug#61214] [PATCH guix-artwork] website: posts: Add Dissecting Guix, Part 2: The Store Monad. From: "(" <paren@HIDDEN> To: "Simon Tournier" <zimon.toutoune@HIDDEN>, "( via Guix-patches via" <guix-patches@HIDDEN>, <61214 <at> debbugs.gnu.org> References: <20230201172821.3072-1-paren@HIDDEN> <86wn50tr9p.fsf@HIDDEN> In-Reply-To: <86wn50tr9p.fsf@HIDDEN> X-Spam-Score: 0.1 (/) X-Debbugs-Envelope-To: 61214 X-BeenThere: debbugs-submit <at> debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: <debbugs-submit.debbugs.gnu.org> List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe> List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/> List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org> List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help> List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe> Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org> X-Spam-Score: -0.9 (/) --5aad2bbd81c2ef20b5411e4f5bd014d79a981568edc23e961230cc436c1a Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Heya, On Thu Feb 2, 2023 at 8:17 AM GMT, Simon Tournier wrote: > Nice! Some minor comments which can be trashed if they are not > helpful. :-) You raise some good points; I'll send a v2 in a moment. > Nice read of an hard topic. Well done! :-) Thank you! :) -- ( --5aad2bbd81c2ef20b5411e4f5bd014d79a981568edc23e961230cc436c1a Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQGzBAABCgAdFiEE6Vh10NblKE5doNlW7ImHg/nqI20FAmPbtWoACgkQ7ImHg/nq I238qwwAlV29WkBntaWZVrAvu5o64lhDmamoce4bAcrK0KFtyAMxJMwvrjnGjc5P FGqno9mxg1leqwhve7euLVfaKeXaFMibHAe0YTPpLAUTSO2b6YcF2bo/FLQUgCQ6 +FHzZK7EJ6muYaU9i7j4dEo0CT8LT8CndtL+AOcQZcCV0ONE6V+sP/uJs947c/xk NkgFLQR6AEfp4TYs2knf2+qsnJMbHCJaakmKks7OheMw+Sd5T1FYOGkB7l8J5iEO hmpfxUH6Emjw0Ydtosn5xMoA/8sRiMtmVaKmv3tmYe9Shox/V4gA6LpPtfnnjw4I AHKWGNv90X28Ghj9P1BMn4HSR8CH9b1PDMeSCedCEOPASN3xCDcOmMWR828tyzPv F7HJFdqC1tixT916LqbxZsyR1JYu+DX7nkX5MAD8fAjFfnR3TdGOWHsFXHG5Z0cc rZmcRMetedxve011sVNdRuLru1vwD7kiOUsmV4N+njcu0BRBoOSj/1rx68unVrQG qdRc9Prz =lfCl -----END PGP SIGNATURE----- --5aad2bbd81c2ef20b5411e4f5bd014d79a981568edc23e961230cc436c1a--
guix-patches@HIDDEN
:bug#61214
; Package guix-patches
.
Full text available.Received: (at 61214) by debbugs.gnu.org; 2 Feb 2023 08:18:19 +0000 From debbugs-submit-bounces <at> debbugs.gnu.org Thu Feb 02 03:18:19 2023 Received: from localhost ([127.0.0.1]:60631 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>) id 1pNUnR-0003Ou-VE for submit <at> debbugs.gnu.org; Thu, 02 Feb 2023 03:18:19 -0500 Received: from mail-wm1-f51.google.com ([209.85.128.51]:53219) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <zimon.toutoune@HIDDEN>) id 1pNUnP-0003OT-8Q for 61214 <at> debbugs.gnu.org; Thu, 02 Feb 2023 03:18:16 -0500 Received: by mail-wm1-f51.google.com with SMTP id k16so740050wms.2 for <61214 <at> debbugs.gnu.org>; Thu, 02 Feb 2023 00:18:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:in-reply-to :subject:cc:to:from:from:to:cc:subject:date:message-id:reply-to; bh=48jKcPMndQI0D7ZVGsI7FNZtsvOQgZDPcxxbIAvEUGc=; b=M7LXLu4MKrle4hSJ+ZahxSw+9gSqjZis4aZ9WIcRJG0qoH/7EuaY/erIsVi7i0uHYh gcT2QzHXShmCJiDvPHq/BYYHwiPFddL7eolGQavHato4Y+bpJQUtVouZSLOUaRyQemTB fviNgdsOwsEt6A+i71YbSYN0i7iBtY/I5l3RDgEzVE9RSG4c2A8KyRlRW8QII1Msk2OL pmBeLyL5XCdj89LYI8LH273dDaIiVLTHINQUX9eOIvSMmQB3V4/Np8gzavgM7FCC03x2 9wWKyimGcZ0BkyUSTc/ZEjRwyzGgKdROeJ4wz81IWrmL6bshi+MjRAdG11P7muBa7NZJ RzNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:in-reply-to :subject:cc:to:from:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=48jKcPMndQI0D7ZVGsI7FNZtsvOQgZDPcxxbIAvEUGc=; b=oHsfiHCi5cS3HVEWe2c+IQdBJeziZ7H+8tsYqaedi+l0y1qsNr3FTIopbnzCyQJAYt 25D08QUmHb6dM19cdA6WNHDfwBE2ULOBR0phJfb+RyL7u4k2QRg4g/ZA1Dig1dkhsn6h hKs6HyiZfL8yVCyFrAN8yszQZ7y4szwWA5nge35aCVphEdjYoErYYnYABCybLWe/+mRu r1mf+2oPhM5PAv9CvWIPLia0BEgeLu02vbQ7kzmZcBhH4Y/3TurhvIMuSlMU/XrXnzux g91Hdts2jAhwtyBUZCsnIkmuqr9e4JvsTc4N88wzc3885Qgy8yvutbiBPe9dDjSZ7rak CyKA== X-Gm-Message-State: AO0yUKWhh1RY9vcvNbcjR+peBEyiWDezyasoaTDigfOYJOXgrybPLfRu fdyneP0ljHETw5VmZg4Ax24i4LyUZMAMGQ== X-Google-Smtp-Source: AK7set+3no+LVUJQ2NTBu+ydZfk0zI9ZpC6JeXcx1TfhDSWyWrLb8zX4jUpvhwOY2ZEpaivPluNVaA== X-Received: by 2002:a05:600c:43c4:b0:3dc:de85:4fed with SMTP id f4-20020a05600c43c400b003dcde854fedmr4454385wmn.0.1675325889504; Thu, 02 Feb 2023 00:18:09 -0800 (PST) Received: from lili ([188.44.71.158]) by smtp.gmail.com with ESMTPSA id i40-20020a05600c4b2800b003dc42d48defsm3734547wmp.6.2023.02.02.00.18.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Feb 2023 00:18:09 -0800 (PST) From: Simon Tournier <zimon.toutoune@HIDDEN> To: "( via Guix-patches via" <guix-patches@HIDDEN>, 61214 <at> debbugs.gnu.org Subject: Re: [bug#61214] [PATCH guix-artwork] website: posts: Add Dissecting Guix, Part 2: The Store Monad. In-Reply-To: <20230201172821.3072-1-paren@HIDDEN> Date: Thu, 02 Feb 2023 09:17:54 +0100 Message-ID: <86wn50tr9p.fsf@HIDDEN> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Score: 0.0 (/) X-Debbugs-Envelope-To: 61214 Cc: "\(" <paren@HIDDEN> X-BeenThere: debbugs-submit <at> debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: <debbugs-submit.debbugs.gnu.org> List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe> List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/> List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org> List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help> List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe> Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org> X-Spam-Score: -1.0 (-) Hi, Nice! Some minor comments which can be trashed if they are not helpful. :-) On Wed, 01 Feb 2023 at 17:28, "\( via Guix-patches" via <guix-patches@HIDDEN= rg> wrote: > +Let's instead implement another M of functional programming, _`maybe`_ v= alues, > +representing a value that may or may not exist. `maybe` is a very common > +feature of strongly-typed functional languages, and you'll see it all ov= er the > +place in Haskell and OCaml code. However, Guile is not strongly typed, s= o we I would say =E2=80=9CGuile is dynamically typed=E2=80=9C or =E2=80=9Cis not= statically typed=E2=80=9D instead of =E2=80=9Cis not strongly typed=E2=80=9D. Because the terminolog= y =E2=80=9Cstrongly typed=E2=80=9D is probably confusing, for instance, quoting Wikipedia [1]: Smalltalk, Ruby, Python, and Self are all "strongly typed" in the sense that =E2=80=A6 The Lisp family of languages are all "strongly typed" in the sense that =E2=80=A6 Standard ML, F#, OCaml, Haskell, Go and Rust are statically type-checked, =E2=80=A6 1: <https://en.wikipedia.org/wiki/Strong_and_weak_typing> > +(define (nothing) > + (make-maybe #f #f)) > +``` Here, I would mention that the value =E2=80=98#f=E2=80=98 is picked but any= thing else would also work as 'nothing. Well, to make the distinction with the previous part about #f or () for null values, I would use the symbol 'no-value or something like that. In all cases, I would mention that this #f is just useless. > +(define (remove-a ?str) > + (if (maybe-is? ?str) > + (let ((str (maybe-value ?str))) > + (if (eq? (string-ref str 0) #\a) > + (something (substring str 1)) > + (nothing))) > + (nothing))) Well, my personal preference would be a =E2=80=99match=E2=80=99 instead of = =E2=80=99let=E2=80=99. :-) Maybe, it is for smoothly introducing mlet? :-) > +Congratulations, you've reinvented `bind`, commonly written as the `>>= =3D` > +operator. And it turns out that a monadic type is just a container type= that > +can be used with `>>=3D`! From my experience, what is often confusing for newcomer with =E2=80=9Cmona= dic notation=E2=80=9C is that =E2=80=99bind=E2=80=99 (>>=3D) appears magic sinc= e it is the same symbol for all the types. When it is attached to one type; here =E2=80=98maybe=E2= =80=98. Well, for what it is worth, it is the same kind of issue when one presents well-known arithmetic operators. At first, it is confusing that the symbol + in 1+2 and 1.2+3.4 does not have the same meaning and e.g. 1+2.3 is a third meaning. Well, I would write: =E2=80=9DCongratulations, you've implemented `bind` fo= r the record `maybe`. It is commonly denoted as the `>>=3D` operator. And it turns out that a monadic type is just a container type that can be used with this `>>=3D` operator defined for such type!=E2=80=9C > +# New Wheel, Old Wheel > + > +Now that we've reinvented the wheel, we'd better learn to use the origin= al > +wheel. Guix provides a generic, high-level monads API, along with three= monads, > +though `maybe` is not one of them, so let's integrate it into the Guix m= onad > +system! I think it is confusing because from my point of view, a piece is missing. Well, for what it is worth, I would end the previous section with a rough definition of monad: 1. one type constructor 2. one =E2=80=98bind=E2=80=98 operator 3. one =E2=80=99return=E2=80=99 function and that =E2=80=99bind=E2=80=99 and =E2=80=99return=E2=80=99 must satisfy t= he 3 laws of monad. Using =E2=80=99maybe=E2=80=99 as example, all is explicit and more or less concre= te. Maybe, the type constructor could be omitted; without loss in generality. Therefore, =E2=80=9Calong with three monads=E2=80=9D means that the API alr= eady provides a type constructor, =E2=80=99bind=E2=80=99 and =E2=80=99return=E2=80=99 for= each. I would write =E2=80=9Calong with the three monads commonly named identity and state monads=E2=80=9D. What is the third? :-) > +First we'll make the API available: > + > +```scheme > +(use-modules (guix monads)) > +``` > + > +To define a monad's API in Guix, we simply use the `define-monad` macro,= and > +provide two procedures: `bind`, and `return`. > + > +```scheme > +(define-monad %maybe-monad > + (bind maybe-chain) > + (return something)) > +``` IMHO, ending the previous section with a short paragraph explaining that a monad is a mathematical object defined with three/two components (type, bind and return) makes this define-monad API more meaningful; again IMHO. > +`bind` is just the procedure that we use to chain monadic procedure calls Instead of chain, I would write compose. Elsewhere too. > +There are also APIs for manipulating lists wrapped in monads; `mlist` cr= eates Do you mean =E2=80=99listm=E2=80=99? > +such a list, `anym` is a monadic `any`, `sequence` turns a list of monad= s into a > +monadic list, `mapm` is a monadic `map`, and `foldm` is a monadic `fold`. Well, here we are touching the limit of what Scheme can express. :-) Because for instance, reading, (mapm %state-monad (lift1 1+ %state-monad) '(0 1 2)) it does not appear clear to me what monad provides compared to just pass arguments around. ;-) > +# In a State > + > +Guix implements a monad called `%state-monad`, and it works with single-= argument > +procedures returning two values. Behold: > + > +```scheme > +(with-monad %state-monad > + (return 33)) > +;; #<procedure 21dc9a0 at <unknown port>:1106:22 (state)> > +``` > > +The `run-with-state` value turns this procedure into an actually useful = value, > +or, rather, two values: > + > +```scheme > +(run-with-state (with-monad %state-monad (return 33)) > + (list "foo" "bar" "baz")) > +;; 33 > +;; ("foo" "bar" "baz") > +``` > + > +What can this actually do for us, though? Well, it gets interesting if w= e do > +some `>>=3D`ing: What appears to me hard to follow is that >>=3D is not explicitly used in the following. See below. > + > +```scheme > +(define state-seq > + (mlet* ((number (return 33))) > + (state-push number))) I guess %state-monad is missing in mlet*. > +result > +;; #<procedure 7fcb6f466960 at <unknown port>:1484:24 (state)> > + > +(run-with-state state-seq (list 32)) > +;; (32) > +;; (33 32) > + > +(run-with-state state-seq (list 30 99)) > +;; (30 99) > +;; (33 30 99) > +``` > + > +What is `state-push`? It's a monadic procedure for `%state-monad` that = takes > +whatever's currently in the first value (the primary value) and pushes i= t onto > +the second value (the state value), which is assumed to be a list, retur= ning the > +old state value as the primary value and the new list as the state value. > + > +So, when we do `(run-with-state result (list 32))`, we're passing `(list= 32)` as > +the initial state value, and then the `>>=3D` form passes that and `33` = to Well, maybe instead of mlet*, it would ease the reading if state-seq explicitly uses >>=3D and mention again that instead it could be written using mlet*. It appears to me clearer with the text to have: (define state-seq* (with-monad %state-monad (>>=3D (return 33) state-push))) Or instead, maybe earlier, something like: --8<---------------cut here---------------start------------->8--- And there's `mlet` and `mlet*`, which can bind them, and is essentially equivalent to a chain of `(>>=3D MEXPR (lambda (BINDING) ...))`. Other said, (with-monad %maybe-monad (>>=3D (return "abad") remove-a remove-b remove-a)) is equivalent to: (mlet* %maybe-monad ((str -> "abad") ;non-monadic binding uses the -> symbol (str1 (remove-a str)) (str2 (remove-b str))) (remove-a str)) --8<---------------cut here---------------end--------------->8--- Maybe instead of some unrelated example earlier, I would write: --8<---------------cut here---------------start------------->8--- Now we can use the `with-monad` macro to tell Guix to use this specific `bi= nd` and `return`, and the `>>=3D` macro to thread monads through procedure call= s! ```scheme (with-monad %maybe-monad (>>=3D (something "aabbc") remove-a remove-a remove-b remove-b)) ;; #<<maybe> is?: #t value: "c"> ``` We can also now use `return`: ```scheme (with-monad %maybe-monad (return 32)) ;; #<<maybe> is?: #t value: 32> ``` But Guix provides many higher-level APIs than `>>=3D` and `return`, as we w= ill see. There's `mbegin`, which evaluates monadic expressions without binding= them to symbols, returning the last one: ```scheme (mbegin %maybe-monad (remove-a "abc")) ;; #<<maybe> is?: #t value: "bc"> ``` And there's `mlet` and `mlet*`, which can bind them, and is essentially equivalent to a chain of `(>>=3D MEXPR (lambda (BINDING) ...))`, rewriting the previous example using the operator `>>=3D`: ```scheme (mlet* %maybe-monad ((str -> "aabbc") ;non-monadic binding uses the -> symb= ol (str1 (remove-a str)) (str2 (remove-a str1)) (str3 (remove-b str2)))=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20 (remove-b str3)) ;; #<<maybe> is?: #t value: "c"> ``` --8<---------------cut here---------------end--------------->8--- > +`state-push`. What `%state-monad` allows us to do is thread together so= me > +procedures that require some kind of state, while pretending the state i= sn't > +there, and then retrieve both the final state and the result at the end! > + > +If you're a bit confused, don't worry. We'll write some of our own > +`%state-monad`-based monadic procedures and hopefully all will become cl= ear. I would add something along these lines for some context: --8<---------------cut here---------------start------------->8--- Consider the (Fibonacci sequence)[https://en.wikipedia.org/wiki/Fibonacci_number] where the next value is computed using the previous values. Let consider one `state` that stores the previous computed value, for instance, ```scheme (define (fibonacci-thing value) (lambda (state) (values (+ value state) value))) ``` Now, let=E2=80=99s evaluate the three first values of the sequence, starting with 1 and 0. (run-with-state (with-monad %state-monad (>>=3D (return 1) fibonacci-thing fibonacci-thing fibonacci-thing)) 0) ;; 3 ;; 2 Similarly, it is possible to compute the 6th and 7th term of the sequence (remind the 0th and first are given by 0 and 1), using equivalently mlet*. (run-with-state (mlet* %state-monad ((starting (return 1)) (n1 (fibonacci-thing starting)) (n2 (fibonacci-thing n1)) (n3 (fibonacci-thing n2)) (n4 (fibonacci-thing n3)) (n5 (fibonacci-thing n4))) (fibonacci-thing n5)) 0) ;; 13 ;; 8 ``` The `fibonacci-thing` monadic procedure takes the number passed, makes it t= he current state, and outputs the sum of the state and the number passed. This gives us a sort of Fibonacci-sequence-like behaviour, where the next number= in the sequence is given by the sum of the two before. --8<---------------cut here---------------end--------------->8--- > +This is all very nifty, and possibly useful in general, but what does th= is have > +to do with Guix? Well, many Guix store-based operations are meant to be= used > +in concert with yet another monad, called the `%store-monad`. But if we= look at > +`(guix store)`, where `%store-monad` is defined... > + > +```scheme > +(define-alias %store-monad %state-monad) > +(define-alias store-return state-return) > +(define-alias store-bind state-bind) > +``` > + > +It was all a shallow fa=C3=A7ade! All the "store monad" is is a special= case of the > +state monad, where a value representing the store is passed as the state= value. > + > +# Lies, Damned Lies, and Abstractions > + > +We mentioned that, technically, we didn't need monads for Guix. Indeed,= many > +(now deprecated) procedures take a store value as the argument: I think it is worth to mention that monad provides the correct framework to compose in a pure world some impure functions. The pure world is an isolated environment but that alone is useless. What make things useful is to have inputs from the outside impure world. All the question is thus to keep the control of impurity when composing. Well, this short video [1] is about Haskell but all the arguments apply, IMHO. I mean the kind of diagram seems to also make sense here. 1: <https://youtu.be/iSmkqocn0oQ> > +```scheme > +(use-modules (guix derivations) > + (guix store)) > + > +(with-store store ;remember this? > + (build-expression->derivation store NAME EXPRESSION ...)) > +``` > + > +This procedure, being deprecated, should never of course be used. For o= ne > +thing, it uses the "quoted build expression" style, rather than gexps, w= hich we > +will discuss another time. The best way to create a derivation from som= e basic > +build code is to use the new-fangled `gexp->derivation` procedure, which= happens > +to be monadic! This paragraph appears to me confusing because it is not clear what is deprecated; with-store or build-expression->derivation? Well, I would drop build-expression->derivation. Instead, I would use this pattern: --8<---------------cut here---------------start------------->8--- Consider that we would like to build something and have it in the store, for instance some text file. Well, the procedure `text-file`, (define* (text-file name text ;string #:optional (references '())) "Return as a monadic value the absolute file name in the store of the file containing TEXT, a string. seems what we want. Therefore, we want to evaluate this monadic value in the context of the store, similarly as the previous run-with-state. `guix repl` provides a nice interface with the command `,run-in-store`: scheme@(guix-user)> ,run-in-store (text-file "unmatched-paren" "( <paren@HIDDEN>") $1 =3D "/gnu/store/v6smacxvdk4yvaa3s3wmd54lixn1dp3y-unmatched-paren" Unwrapping the machinery, it would reads, (with-store store (run-with-store store (text-file "unmatched-paren" "( <paren@HIDDEN>"))) ;; "/gnu/store/v6smacxvdk4yvaa3s3wmd54lixn1dp3y-unmatched-paren" where `with-store` acts similarly as `with-monad` and `run-with-store` similarly as `run-with-state`. Note the order is reversed because the store is somehow unique. Now, consider we want to build a derivation. Let define one: ```scheme (use-modules (guix gexp) (gnu packages irc)) (define symlink-irssi (gexp->derivation "link-to-irssi" #~(symlink #$(file-append irssi "/bin/irssi") #$output))) ;; #<procedure 7fddcc7b81e0 at guix/gexp.scm:1180:2 (state)> ``` Remember that store and state are related. You don't have to understand the `#~(...)` form yet, only everything surrou= nding it. We can see that this `gexp->derivation` returns a procedure taking the initial state (store), just like our `%state-monad` procedures did. And to= pass this initial state, we used `run-with-state`. The equivalent for working w= ith the store is our old friend `run-with-store`! (with-store store (run-with-store store symlink-irssi))) ;; #<derivation /gnu/store/q7kwwl4z6psifnv4di1p1kpvlx06fmyq-link-to-irssi.d= rv =3D> /gnu/store/6a94niigx4ii0ldjdy33wx9anhifr25x-link-to-irssi 7fddb7ef5= 2d0> --8<---------------cut here---------------end--------------->8--- My 2 cents. Nice read of an hard topic. Well done! :-) Cheers, simon
guix-patches@HIDDEN
:bug#61214
; Package guix-patches
.
Full text available.Received: (at submit) by debbugs.gnu.org; 2 Feb 2023 08:18:22 +0000 From debbugs-submit-bounces <at> debbugs.gnu.org Thu Feb 02 03:18:22 2023 Received: from localhost ([127.0.0.1]:60634 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>) id 1pNUnV-0003PD-2T for submit <at> debbugs.gnu.org; Thu, 02 Feb 2023 03:18:22 -0500 Received: from lists.gnu.org ([209.51.188.17]:39206) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <zimon.toutoune@HIDDEN>) id 1pNUnS-0003Ow-Gg for submit <at> debbugs.gnu.org; Thu, 02 Feb 2023 03:18:19 -0500 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 <zimon.toutoune@HIDDEN>) id 1pNUnP-0002OP-Ss for guix-patches@HIDDEN; Thu, 02 Feb 2023 03:18:15 -0500 Received: from mail-wm1-x32b.google.com ([2a00:1450:4864:20::32b]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from <zimon.toutoune@HIDDEN>) id 1pNUnL-0008Fx-8W for guix-patches@HIDDEN; Thu, 02 Feb 2023 03:18:14 -0500 Received: by mail-wm1-x32b.google.com with SMTP id j29-20020a05600c1c1d00b003dc52fed235so712737wms.1 for <guix-patches@HIDDEN>; Thu, 02 Feb 2023 00:18:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:in-reply-to :subject:cc:to:from:from:to:cc:subject:date:message-id:reply-to; bh=48jKcPMndQI0D7ZVGsI7FNZtsvOQgZDPcxxbIAvEUGc=; b=M7LXLu4MKrle4hSJ+ZahxSw+9gSqjZis4aZ9WIcRJG0qoH/7EuaY/erIsVi7i0uHYh gcT2QzHXShmCJiDvPHq/BYYHwiPFddL7eolGQavHato4Y+bpJQUtVouZSLOUaRyQemTB fviNgdsOwsEt6A+i71YbSYN0i7iBtY/I5l3RDgEzVE9RSG4c2A8KyRlRW8QII1Msk2OL pmBeLyL5XCdj89LYI8LH273dDaIiVLTHINQUX9eOIvSMmQB3V4/Np8gzavgM7FCC03x2 9wWKyimGcZ0BkyUSTc/ZEjRwyzGgKdROeJ4wz81IWrmL6bshi+MjRAdG11P7muBa7NZJ RzNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:in-reply-to :subject:cc:to:from:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=48jKcPMndQI0D7ZVGsI7FNZtsvOQgZDPcxxbIAvEUGc=; b=i1/ZWMhLG+1hSBKTzlBPGXWfOqMduepCQr4xjSi+FuD7A92rNvH6i/wlXCqXRJ9xth DBxrSzGuwa4SCDWWC4/4YPrtBuzqPkoqeWOJDXfbfrjntQfrlhcPAOEhp7MW7122/c4h RbDuXq4ZBb1qdqtEAs/UV5h2BhsJLGJ8TgXX2UWTO8V0EIKhv/K5VjuOvhv4ulBCGIpD /+aGYLsJ6E+KBmvsfxkdA+Pi+Qf6PRjRZwWNYSBTkvBZvOkaHcdCN88q/l6NNq1kal8V T4QTUwnVLZtmEzK08nT8y/NRzpzotARS+oN1zHjvIltFfIKShb3Hvo2Psp2llJJ2wnh5 YizA== X-Gm-Message-State: AO0yUKUWAiupR+GwdwY6zT/MNRjL2NzcLTK+ZryMa3Boqmxuvkp6bj/U aygTE9eh7e6O5//VhLBc62A= X-Google-Smtp-Source: AK7set+3no+LVUJQ2NTBu+ydZfk0zI9ZpC6JeXcx1TfhDSWyWrLb8zX4jUpvhwOY2ZEpaivPluNVaA== X-Received: by 2002:a05:600c:43c4:b0:3dc:de85:4fed with SMTP id f4-20020a05600c43c400b003dcde854fedmr4454385wmn.0.1675325889504; Thu, 02 Feb 2023 00:18:09 -0800 (PST) Received: from lili ([188.44.71.158]) by smtp.gmail.com with ESMTPSA id i40-20020a05600c4b2800b003dc42d48defsm3734547wmp.6.2023.02.02.00.18.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Feb 2023 00:18:09 -0800 (PST) From: Simon Tournier <zimon.toutoune@HIDDEN> To: "( via Guix-patches via" <guix-patches@HIDDEN>, 61214 <at> debbugs.gnu.org Subject: Re: [bug#61214] [PATCH guix-artwork] website: posts: Add Dissecting Guix, Part 2: The Store Monad. In-Reply-To: <20230201172821.3072-1-paren@HIDDEN> Date: Thu, 02 Feb 2023 09:17:54 +0100 Message-ID: <86wn50tr9p.fsf@HIDDEN> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Received-SPF: pass client-ip=2a00:1450:4864:20::32b; envelope-from=zimon.toutoune@HIDDEN; helo=mail-wm1-x32b.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_PDS_SHORTFWD_URISHRT_QP=0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-Spam-Score: -1.3 (-) X-Debbugs-Envelope-To: submit Cc: "\(" <paren@HIDDEN> X-BeenThere: debbugs-submit <at> debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: <debbugs-submit.debbugs.gnu.org> List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe> List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/> List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org> List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help> List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe> Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org> X-Spam-Score: -2.3 (--) Hi, Nice! Some minor comments which can be trashed if they are not helpful. :-) On Wed, 01 Feb 2023 at 17:28, "\( via Guix-patches" via <guix-patches@HIDDEN= rg> wrote: > +Let's instead implement another M of functional programming, _`maybe`_ v= alues, > +representing a value that may or may not exist. `maybe` is a very common > +feature of strongly-typed functional languages, and you'll see it all ov= er the > +place in Haskell and OCaml code. However, Guile is not strongly typed, s= o we I would say =E2=80=9CGuile is dynamically typed=E2=80=9C or =E2=80=9Cis not= statically typed=E2=80=9D instead of =E2=80=9Cis not strongly typed=E2=80=9D. Because the terminolog= y =E2=80=9Cstrongly typed=E2=80=9D is probably confusing, for instance, quoting Wikipedia [1]: Smalltalk, Ruby, Python, and Self are all "strongly typed" in the sense that =E2=80=A6 The Lisp family of languages are all "strongly typed" in the sense that =E2=80=A6 Standard ML, F#, OCaml, Haskell, Go and Rust are statically type-checked, =E2=80=A6 1: <https://en.wikipedia.org/wiki/Strong_and_weak_typing> > +(define (nothing) > + (make-maybe #f #f)) > +``` Here, I would mention that the value =E2=80=98#f=E2=80=98 is picked but any= thing else would also work as 'nothing. Well, to make the distinction with the previous part about #f or () for null values, I would use the symbol 'no-value or something like that. In all cases, I would mention that this #f is just useless. > +(define (remove-a ?str) > + (if (maybe-is? ?str) > + (let ((str (maybe-value ?str))) > + (if (eq? (string-ref str 0) #\a) > + (something (substring str 1)) > + (nothing))) > + (nothing))) Well, my personal preference would be a =E2=80=99match=E2=80=99 instead of = =E2=80=99let=E2=80=99. :-) Maybe, it is for smoothly introducing mlet? :-) > +Congratulations, you've reinvented `bind`, commonly written as the `>>= =3D` > +operator. And it turns out that a monadic type is just a container type= that > +can be used with `>>=3D`! From my experience, what is often confusing for newcomer with =E2=80=9Cmona= dic notation=E2=80=9C is that =E2=80=99bind=E2=80=99 (>>=3D) appears magic sinc= e it is the same symbol for all the types. When it is attached to one type; here =E2=80=98maybe=E2= =80=98. Well, for what it is worth, it is the same kind of issue when one presents well-known arithmetic operators. At first, it is confusing that the symbol + in 1+2 and 1.2+3.4 does not have the same meaning and e.g. 1+2.3 is a third meaning. Well, I would write: =E2=80=9DCongratulations, you've implemented `bind` fo= r the record `maybe`. It is commonly denoted as the `>>=3D` operator. And it turns out that a monadic type is just a container type that can be used with this `>>=3D` operator defined for such type!=E2=80=9C > +# New Wheel, Old Wheel > + > +Now that we've reinvented the wheel, we'd better learn to use the origin= al > +wheel. Guix provides a generic, high-level monads API, along with three= monads, > +though `maybe` is not one of them, so let's integrate it into the Guix m= onad > +system! I think it is confusing because from my point of view, a piece is missing. Well, for what it is worth, I would end the previous section with a rough definition of monad: 1. one type constructor 2. one =E2=80=98bind=E2=80=98 operator 3. one =E2=80=99return=E2=80=99 function and that =E2=80=99bind=E2=80=99 and =E2=80=99return=E2=80=99 must satisfy t= he 3 laws of monad. Using =E2=80=99maybe=E2=80=99 as example, all is explicit and more or less concre= te. Maybe, the type constructor could be omitted; without loss in generality. Therefore, =E2=80=9Calong with three monads=E2=80=9D means that the API alr= eady provides a type constructor, =E2=80=99bind=E2=80=99 and =E2=80=99return=E2=80=99 for= each. I would write =E2=80=9Calong with the three monads commonly named identity and state monads=E2=80=9D. What is the third? :-) > +First we'll make the API available: > + > +```scheme > +(use-modules (guix monads)) > +``` > + > +To define a monad's API in Guix, we simply use the `define-monad` macro,= and > +provide two procedures: `bind`, and `return`. > + > +```scheme > +(define-monad %maybe-monad > + (bind maybe-chain) > + (return something)) > +``` IMHO, ending the previous section with a short paragraph explaining that a monad is a mathematical object defined with three/two components (type, bind and return) makes this define-monad API more meaningful; again IMHO. > +`bind` is just the procedure that we use to chain monadic procedure calls Instead of chain, I would write compose. Elsewhere too. > +There are also APIs for manipulating lists wrapped in monads; `mlist` cr= eates Do you mean =E2=80=99listm=E2=80=99? > +such a list, `anym` is a monadic `any`, `sequence` turns a list of monad= s into a > +monadic list, `mapm` is a monadic `map`, and `foldm` is a monadic `fold`. Well, here we are touching the limit of what Scheme can express. :-) Because for instance, reading, (mapm %state-monad (lift1 1+ %state-monad) '(0 1 2)) it does not appear clear to me what monad provides compared to just pass arguments around. ;-) > +# In a State > + > +Guix implements a monad called `%state-monad`, and it works with single-= argument > +procedures returning two values. Behold: > + > +```scheme > +(with-monad %state-monad > + (return 33)) > +;; #<procedure 21dc9a0 at <unknown port>:1106:22 (state)> > +``` > > +The `run-with-state` value turns this procedure into an actually useful = value, > +or, rather, two values: > + > +```scheme > +(run-with-state (with-monad %state-monad (return 33)) > + (list "foo" "bar" "baz")) > +;; 33 > +;; ("foo" "bar" "baz") > +``` > + > +What can this actually do for us, though? Well, it gets interesting if w= e do > +some `>>=3D`ing: What appears to me hard to follow is that >>=3D is not explicitly used in the following. See below. > + > +```scheme > +(define state-seq > + (mlet* ((number (return 33))) > + (state-push number))) I guess %state-monad is missing in mlet*. > +result > +;; #<procedure 7fcb6f466960 at <unknown port>:1484:24 (state)> > + > +(run-with-state state-seq (list 32)) > +;; (32) > +;; (33 32) > + > +(run-with-state state-seq (list 30 99)) > +;; (30 99) > +;; (33 30 99) > +``` > + > +What is `state-push`? It's a monadic procedure for `%state-monad` that = takes > +whatever's currently in the first value (the primary value) and pushes i= t onto > +the second value (the state value), which is assumed to be a list, retur= ning the > +old state value as the primary value and the new list as the state value. > + > +So, when we do `(run-with-state result (list 32))`, we're passing `(list= 32)` as > +the initial state value, and then the `>>=3D` form passes that and `33` = to Well, maybe instead of mlet*, it would ease the reading if state-seq explicitly uses >>=3D and mention again that instead it could be written using mlet*. It appears to me clearer with the text to have: (define state-seq* (with-monad %state-monad (>>=3D (return 33) state-push))) Or instead, maybe earlier, something like: --8<---------------cut here---------------start------------->8--- And there's `mlet` and `mlet*`, which can bind them, and is essentially equivalent to a chain of `(>>=3D MEXPR (lambda (BINDING) ...))`. Other said, (with-monad %maybe-monad (>>=3D (return "abad") remove-a remove-b remove-a)) is equivalent to: (mlet* %maybe-monad ((str -> "abad") ;non-monadic binding uses the -> symbol (str1 (remove-a str)) (str2 (remove-b str))) (remove-a str)) --8<---------------cut here---------------end--------------->8--- Maybe instead of some unrelated example earlier, I would write: --8<---------------cut here---------------start------------->8--- Now we can use the `with-monad` macro to tell Guix to use this specific `bi= nd` and `return`, and the `>>=3D` macro to thread monads through procedure call= s! ```scheme (with-monad %maybe-monad (>>=3D (something "aabbc") remove-a remove-a remove-b remove-b)) ;; #<<maybe> is?: #t value: "c"> ``` We can also now use `return`: ```scheme (with-monad %maybe-monad (return 32)) ;; #<<maybe> is?: #t value: 32> ``` But Guix provides many higher-level APIs than `>>=3D` and `return`, as we w= ill see. There's `mbegin`, which evaluates monadic expressions without binding= them to symbols, returning the last one: ```scheme (mbegin %maybe-monad (remove-a "abc")) ;; #<<maybe> is?: #t value: "bc"> ``` And there's `mlet` and `mlet*`, which can bind them, and is essentially equivalent to a chain of `(>>=3D MEXPR (lambda (BINDING) ...))`, rewriting the previous example using the operator `>>=3D`: ```scheme (mlet* %maybe-monad ((str -> "aabbc") ;non-monadic binding uses the -> symb= ol (str1 (remove-a str)) (str2 (remove-a str1)) (str3 (remove-b str2)))=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20 (remove-b str3)) ;; #<<maybe> is?: #t value: "c"> ``` --8<---------------cut here---------------end--------------->8--- > +`state-push`. What `%state-monad` allows us to do is thread together so= me > +procedures that require some kind of state, while pretending the state i= sn't > +there, and then retrieve both the final state and the result at the end! > + > +If you're a bit confused, don't worry. We'll write some of our own > +`%state-monad`-based monadic procedures and hopefully all will become cl= ear. I would add something along these lines for some context: --8<---------------cut here---------------start------------->8--- Consider the (Fibonacci sequence)[https://en.wikipedia.org/wiki/Fibonacci_number] where the next value is computed using the previous values. Let consider one `state` that stores the previous computed value, for instance, ```scheme (define (fibonacci-thing value) (lambda (state) (values (+ value state) value))) ``` Now, let=E2=80=99s evaluate the three first values of the sequence, starting with 1 and 0. (run-with-state (with-monad %state-monad (>>=3D (return 1) fibonacci-thing fibonacci-thing fibonacci-thing)) 0) ;; 3 ;; 2 Similarly, it is possible to compute the 6th and 7th term of the sequence (remind the 0th and first are given by 0 and 1), using equivalently mlet*. (run-with-state (mlet* %state-monad ((starting (return 1)) (n1 (fibonacci-thing starting)) (n2 (fibonacci-thing n1)) (n3 (fibonacci-thing n2)) (n4 (fibonacci-thing n3)) (n5 (fibonacci-thing n4))) (fibonacci-thing n5)) 0) ;; 13 ;; 8 ``` The `fibonacci-thing` monadic procedure takes the number passed, makes it t= he current state, and outputs the sum of the state and the number passed. This gives us a sort of Fibonacci-sequence-like behaviour, where the next number= in the sequence is given by the sum of the two before. --8<---------------cut here---------------end--------------->8--- > +This is all very nifty, and possibly useful in general, but what does th= is have > +to do with Guix? Well, many Guix store-based operations are meant to be= used > +in concert with yet another monad, called the `%store-monad`. But if we= look at > +`(guix store)`, where `%store-monad` is defined... > + > +```scheme > +(define-alias %store-monad %state-monad) > +(define-alias store-return state-return) > +(define-alias store-bind state-bind) > +``` > + > +It was all a shallow fa=C3=A7ade! All the "store monad" is is a special= case of the > +state monad, where a value representing the store is passed as the state= value. > + > +# Lies, Damned Lies, and Abstractions > + > +We mentioned that, technically, we didn't need monads for Guix. Indeed,= many > +(now deprecated) procedures take a store value as the argument: I think it is worth to mention that monad provides the correct framework to compose in a pure world some impure functions. The pure world is an isolated environment but that alone is useless. What make things useful is to have inputs from the outside impure world. All the question is thus to keep the control of impurity when composing. Well, this short video [1] is about Haskell but all the arguments apply, IMHO. I mean the kind of diagram seems to also make sense here. 1: <https://youtu.be/iSmkqocn0oQ> > +```scheme > +(use-modules (guix derivations) > + (guix store)) > + > +(with-store store ;remember this? > + (build-expression->derivation store NAME EXPRESSION ...)) > +``` > + > +This procedure, being deprecated, should never of course be used. For o= ne > +thing, it uses the "quoted build expression" style, rather than gexps, w= hich we > +will discuss another time. The best way to create a derivation from som= e basic > +build code is to use the new-fangled `gexp->derivation` procedure, which= happens > +to be monadic! This paragraph appears to me confusing because it is not clear what is deprecated; with-store or build-expression->derivation? Well, I would drop build-expression->derivation. Instead, I would use this pattern: --8<---------------cut here---------------start------------->8--- Consider that we would like to build something and have it in the store, for instance some text file. Well, the procedure `text-file`, (define* (text-file name text ;string #:optional (references '())) "Return as a monadic value the absolute file name in the store of the file containing TEXT, a string. seems what we want. Therefore, we want to evaluate this monadic value in the context of the store, similarly as the previous run-with-state. `guix repl` provides a nice interface with the command `,run-in-store`: scheme@(guix-user)> ,run-in-store (text-file "unmatched-paren" "( <paren@HIDDEN>") $1 =3D "/gnu/store/v6smacxvdk4yvaa3s3wmd54lixn1dp3y-unmatched-paren" Unwrapping the machinery, it would reads, (with-store store (run-with-store store (text-file "unmatched-paren" "( <paren@HIDDEN>"))) ;; "/gnu/store/v6smacxvdk4yvaa3s3wmd54lixn1dp3y-unmatched-paren" where `with-store` acts similarly as `with-monad` and `run-with-store` similarly as `run-with-state`. Note the order is reversed because the store is somehow unique. Now, consider we want to build a derivation. Let define one: ```scheme (use-modules (guix gexp) (gnu packages irc)) (define symlink-irssi (gexp->derivation "link-to-irssi" #~(symlink #$(file-append irssi "/bin/irssi") #$output))) ;; #<procedure 7fddcc7b81e0 at guix/gexp.scm:1180:2 (state)> ``` Remember that store and state are related. You don't have to understand the `#~(...)` form yet, only everything surrou= nding it. We can see that this `gexp->derivation` returns a procedure taking the initial state (store), just like our `%state-monad` procedures did. And to= pass this initial state, we used `run-with-state`. The equivalent for working w= ith the store is our old friend `run-with-store`! (with-store store (run-with-store store symlink-irssi))) ;; #<derivation /gnu/store/q7kwwl4z6psifnv4di1p1kpvlx06fmyq-link-to-irssi.d= rv =3D> /gnu/store/6a94niigx4ii0ldjdy33wx9anhifr25x-link-to-irssi 7fddb7ef5= 2d0> --8<---------------cut here---------------end--------------->8--- My 2 cents. Nice read of an hard topic. Well done! :-) Cheers, simon
guix-patches@HIDDEN
:bug#61214
; Package guix-patches
.
Full text available.Received: (at submit) by debbugs.gnu.org; 1 Feb 2023 17:28:47 +0000 From debbugs-submit-bounces <at> debbugs.gnu.org Wed Feb 01 12:28:47 2023 Received: from localhost ([127.0.0.1]:59760 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>) id 1pNGub-0004w5-SO for submit <at> debbugs.gnu.org; Wed, 01 Feb 2023 12:28:47 -0500 Received: from lists.gnu.org ([209.51.188.17]:59116) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <paren@HIDDEN>) id 1pNGua-0004vx-25 for submit <at> debbugs.gnu.org; Wed, 01 Feb 2023 12:28:45 -0500 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 <paren@HIDDEN>) id 1pNGuY-0004nz-QJ for guix-patches@HIDDEN; Wed, 01 Feb 2023 12:28:43 -0500 Received: from knopi.disroot.org ([178.21.23.139]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from <paren@HIDDEN>) id 1pNGuV-0007iX-SV for guix-patches@HIDDEN; Wed, 01 Feb 2023 12:28:42 -0500 Received: from localhost (localhost [127.0.0.1]) by disroot.org (Postfix) with ESMTP id 54717413CE; Wed, 1 Feb 2023 18:28:36 +0100 (CET) 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 HirxCjrzdAcB; Wed, 1 Feb 2023 18:28:34 +0100 (CET) From: "(" <paren@HIDDEN> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=disroot.org; s=mail; t=1675272513; bh=NEGOKZEiygbQ/2t8/MiFYICWh39O7w1D6zmyq9c24Vo=; h=From:To:Cc:Subject:Date; b=fcy6dtY62P4N8W39I4QPXOPwTOu1fLJrQyEswf8DV59qrOBjNInEodxpHZv983man YRU2ipOk+js6sOG2Q/4LQZI0aBuWHTnXVEVRDk47crx5GVAgr3+z+f7PP3XMWJzUsc szgHASoBpeHFDYHvhnV03AA735gszKk7B0XtpKnTaUnkfwQwI6ZbIT3PNIpDebJGbK MSMeXx6wrsgQFNxJCh6JvqKfIZpL9v0VG9FdQAFqKFQjs1HQ0KJn0DNJ5fz+A3JTl9 jwWGTnrZv9xuIPisiXZoV0b3forbBR0MEpLk1nNM8Da496H+LFB+BU//TmCGARW3Jj nAax5RJRBkXQA== To: guix-patches@HIDDEN Subject: [PATCH guix-artwork] website: posts: Add Dissecting Guix, Part 2: The Store Monad. Date: Wed, 1 Feb 2023 17:28:21 +0000 Message-Id: <20230201172821.3072-1-paren@HIDDEN> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=178.21.23.139; envelope-from=paren@HIDDEN; helo=knopi.disroot.org X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Spam-Score: -1.4 (-) X-Debbugs-Envelope-To: submit Cc: "\(" <paren@HIDDEN> X-BeenThere: debbugs-submit <at> debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: <debbugs-submit.debbugs.gnu.org> List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe> List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/> List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org> List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help> List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe> Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org> X-Spam-Score: -2.4 (--) * website/posts/dissecting-guix-2-store-monad.md: New blog post. --- Heya Guix! At long last, the second Dissecting Guix is complete :) This one is about monads, the Guix monad API, and the %STORE-MONAD. Hopefully it's not too confusing, but if you find it hard to follow, please let me know! -- ( .../posts/dissecting-guix-2-store-monad.md | 498 ++++++++++++++++++ 1 file changed, 498 insertions(+) create mode 100644 website/posts/dissecting-guix-2-store-monad.md diff --git a/website/posts/dissecting-guix-2-store-monad.md b/website/posts/dissecting-guix-2-store-monad.md new file mode 100644 index 0000000..83f2e69 --- /dev/null +++ b/website/posts/dissecting-guix-2-store-monad.md @@ -0,0 +1,498 @@ +title: Dissecting Guix, Part 2: The Store Monad +date: TBC +author: ( +tags: Dissecting Guix, Functional package management, Programming interfaces, Scheme API +--- +Hello again! + +In [the last post](https://guix.gnu.org/en/blog/2023/dissecting-guix-part-1-derivations/), +we briefly mentioned the `with-store` and `run-with-store` APIs. Today, we'll +be looking at those in further detail, along with the related monad API and the +`%store-monad`! + +Monads are a little hard to explain, and from a distance, they seem more than a +bit confusing. So, I want you to erase monads from your mind for now. We'll +come back to them later. + +# Yes, No, Maybe So + +Let's instead implement another M of functional programming, _`maybe`_ values, +representing a value that may or may not exist. `maybe` is a very common +feature of strongly-typed functional languages, and you'll see it all over the +place in Haskell and OCaml code. However, Guile is not strongly typed, so we +usually use ad-hoc `#f`s and `'()`s for null values instead of a proper +"optional" value. + +Just for fun, though, we'll implement a proper `maybe` in Guile. Fire up that +REPL once again, and let's import a bunch of modules that we'll need: + +```scheme +(use-modules (ice-9 match) + (srfi srfi-9)) +``` + +We'll implement `maybe` as a record with two fields, `is?` and `value`. If the +value contains something, `is?` will be `#t` and `value` will contain the thing +in question, and if it's empty, `is?`'ll be `#f`. + +```scheme +(define-record-type <maybe> + (make-maybe is? value) + maybe? + (is? maybe-is?) + (value maybe-value)) +``` + +Now we'll define constructors for the two possible states: + +```scheme +(define (something value) + (make-maybe #t value)) + +(define (nothing) + (make-maybe #f #f)) +``` + +And make some silly functions that return optional values: + +```scheme +(define (remove-a str) + (if (eq? (string-ref str 0) #\a) + (something (substring str 1)) + (nothing))) + +(define (remove-b str) + (if (eq? (string-ref str 0) #\b) + (something (substring str 1)) + (nothing))) + +(remove-a "ahh") +;; #<<maybe> is?: #t value: "hh"> + +(remove-a "ooh") +;; #<<maybe> is?: #f value: #f> + +(remove-b "bad") +;; #<<maybe> is?: #t value: "ad"> +``` + +But what if we want to compose the results of these functions? + +# Keeping Your Composure + +As you might have guessed, this is not fun. Cosplaying as a compiler backend +typically isn't. + +```scheme +(let ((t1 (remove-a "abcd"))) + (if (maybe-is? t1) + (remove-b (maybe-value t1)) + (nothing))) +;; #<<maybe> is?: #t value: "cd"> + +(let ((t1 (remove-a "bbcd"))) + (if (maybe-is? t1) + (remove-b (maybe-value t1)) + (nothing))) +;; #<<maybe> is?: #f value: #f> +``` + +I can almost hear the heckling. Even worse, chaining three: + +```scheme +(let* ((t1 (remove-a "abad")) + (t2 (if (maybe-is? t1) + (remove-b (maybe-value t1)) + (nothing)))) + (if (maybe-is? t2) + (remove-a (maybe-value t2)) + (nothing))) +;; #<<maybe> is?: #t value: "d"> +``` + +So, how do we go about making this more bearable? Well, one way could be to +make `remove-a` and `remove-b` accept `maybe`s: + +```scheme +(define (remove-a ?str) + (if (maybe-is? ?str) + (let ((str (maybe-value ?str))) + (if (eq? (string-ref str 0) #\a) + (something (substring str 1)) + (nothing))) + (nothing))) + +(define (remove-b ?str) + (if (maybe-is? ?str) + (let ((str (maybe-value ?str))) + (if (eq? (string-ref str 0) #\b) + (something (substring str 1)) + (nothing))) + (nothing))) +``` + +Not at all pretty, but it works! + +``` +(remove-b (remove-a (something "abc"))) +;; #<<maybe> is?: #t value: "c"> +``` + +Still, our procedures now require quite a bit of boilerplate. Might there be a +better way? + +# The Ties That `>>=` Us + +First of all, we'll revert to our original definitions of `remove-a` and +`remove-b`, that is to say, the ones that take a regular value and return a +`maybe`. + +```scheme +(define (remove-a str) + (if (eq? (string-ref str 0) #\a) + (something (substring str 1)) + (nothing))) + +(define (remove-b str) + (if (eq? (string-ref str 0) #\b) + (something (substring str 1)) + (nothing))) +``` + +What if tried introducing higher-order procedures (procedures that accept other +procedures as arguments) into the equation? Because we're functional +programmers and we're somewhat obsessed with that kind of thing. + +```scheme +(define (maybe-chain maybe proc) + (if (maybe-is? maybe) + (proc (maybe-value maybe)) + (nothing))) + +(maybe-chain (something "abc") + remove-a) +;; #<<maybe> is?: #t value: "bc"> + +(maybe-chain (nothing) + remove-a) +;; #<<maybe> is?: #f value: #f> +``` + +It lives! To make it easier to chain procedures like this, we'll define a macro +that allows us to perform any number of sequenced operations with only one +chaining form: + +```scheme +(define-syntax maybe-chain* + (syntax-rules () + ((_ maybe proc) + (maybe-chain maybe proc)) + ((_ maybe proc rest ...) + (maybe-chain* (maybe-chain maybe proc) + rest ...)))) + +(maybe-chain* (something "abad") + remove-a + remove-b + remove-a) +;; #<<maybe> is?: #t value: "d"> +``` + +Congratulations, you've reinvented `bind`, commonly written as the `>>=` +operator. And it turns out that a monadic type is just a container type that +can be used with `>>=`! + +# New Wheel, Old Wheel + +Now that we've reinvented the wheel, we'd better learn to use the original +wheel. Guix provides a generic, high-level monads API, along with three monads, +though `maybe` is not one of them, so let's integrate it into the Guix monad +system! + +First we'll make the API available: + +```scheme +(use-modules (guix monads)) +``` + +To define a monad's API in Guix, we simply use the `define-monad` macro, and +provide two procedures: `bind`, and `return`. + +```scheme +(define-monad %maybe-monad + (bind maybe-chain) + (return something)) +``` + +`bind` is just the procedure that we use to chain monadic procedure calls +together, and `return` is a procedure that takes a non-monadic value and wraps +it up in the most basic form possible of the monad. + +Now we can use the `with-monad` macro to tell Guix to use this specific `bind` +and `return`, and the `>>=` macro to thread monads through procedure calls! + +```scheme +(with-monad %maybe-monad + (>>= (something "aabbc") + remove-a + remove-a + remove-b + remove-b)) +;; #<<maybe> is?: #t value: "c"> +``` + +We can also now use `return`: + +```scheme +(with-monad %maybe-monad + (return 32)) +;; #<<maybe> is?: #t value: 32> +``` + +But Guix provides many higher-level APIs than `>>=` and `return`, as we will +see. There's `mbegin`, which evaluates monadic expressions without binding them +to symbols, returning the last one: + +```scheme +(mbegin %maybe-monad + (remove-a "abc")) +;; #<<maybe> is?: #t value: "bc"> +``` + +And there's `mlet` and `mlet*`, which can bind them, and is essentially +equivalent to a chain of `(>>= MEXPR (lambda (BINDING) ...))`: + +```scheme +(mlet* %maybe-monad ((str -> "abad") ;non-monadic binding uses the -> symbol + (str1 (remove-a str)) + (str2 (remove-b str))) + (remove-a str)) +;; #<<maybe> is?: #t value: "d"> +``` + +Various abstractions over these two exist too, such as `mwhen` (a `when` plus an +`mbegin`), `munless` (an `unless` plus an `mbegin`), and `mparameterize` +(dynamically-scoped value rebinding, like `parameterize`, in a monadic context). +`lift` takes a procedure and a monad and creates a new procedure that returns +a monadic value. + +There are also APIs for manipulating lists wrapped in monads; `mlist` creates +such a list, `anym` is a monadic `any`, `sequence` turns a list of monads into a +monadic list, `mapm` is a monadic `map`, and `foldm` is a monadic `fold`. + +This is all well and good, you may be thinking, but why does Guix need a monad +API? The answer is technically that it doesn't. But building on the monad API +makes a lot of things much easier, and to learn why, we're going to look at one +of Guix's built-in monads. + +# In a State + +Guix implements a monad called `%state-monad`, and it works with single-argument +procedures returning two values. Behold: + +```scheme +(with-monad %state-monad + (return 33)) +;; #<procedure 21dc9a0 at <unknown port>:1106:22 (state)> +``` + +The `run-with-state` value turns this procedure into an actually useful value, +or, rather, two values: + +```scheme +(run-with-state (with-monad %state-monad (return 33)) + (list "foo" "bar" "baz")) +;; 33 +;; ("foo" "bar" "baz") +``` + +What can this actually do for us, though? Well, it gets interesting if we do +some `>>=`ing: + +```scheme +(define state-seq + (mlet* ((number (return 33))) + (state-push number))) +result +;; #<procedure 7fcb6f466960 at <unknown port>:1484:24 (state)> + +(run-with-state state-seq (list 32)) +;; (32) +;; (33 32) + +(run-with-state state-seq (list 30 99)) +;; (30 99) +;; (33 30 99) +``` + +What is `state-push`? It's a monadic procedure for `%state-monad` that takes +whatever's currently in the first value (the primary value) and pushes it onto +the second value (the state value), which is assumed to be a list, returning the +old state value as the primary value and the new list as the state value. + +So, when we do `(run-with-state result (list 32))`, we're passing `(list 32)` as +the initial state value, and then the `>>=` form passes that and `33` to +`state-push`. What `%state-monad` allows us to do is thread together some +procedures that require some kind of state, while pretending the state isn't +there, and then retrieve both the final state and the result at the end! + +If you're a bit confused, don't worry. We'll write some of our own +`%state-monad`-based monadic procedures and hopefully all will become clear. + +```scheme +(define (fibonacci-thing value) + (lambda (state) + (values (+ value state) + value))) + +(run-with-state + (mlet* %state-monad ((starting (return 1)) + (n1 (fibonacci-thing starting)) + (n2 (fibonacci-thing n1))) + (fibonacci-thing n2)) + 0) +;; 3 +;; 2 + +(run-with-state + (mlet* %state-monad ((starting (return 1)) + (n1 (fibonacci-thing starting)) + (n2 (fibonacci-thing n1)) + (n3 (fibonacci-thing n2)) + (n4 (fibonacci-thing n3)) + (n5 (fibonacci-thing n4))) + (fibonacci-thing n5)) + 0) +;; 13 +;; 8 +``` + +The `fibonacci-thing` monadic procedure takes the number passed, makes it the +current state, and outputs the sum of the state and the number passed. This +gives us a sort of Fibonacci-sequence-like behaviour, where the next number in +the sequence is given by the sum of the two before. + +This is all very nifty, and possibly useful in general, but what does this have +to do with Guix? Well, many Guix store-based operations are meant to be used +in concert with yet another monad, called the `%store-monad`. But if we look at +`(guix store)`, where `%store-monad` is defined... + +```scheme +(define-alias %store-monad %state-monad) +(define-alias store-return state-return) +(define-alias store-bind state-bind) +``` + +It was all a shallow façade! All the "store monad" is is a special case of the +state monad, where a value representing the store is passed as the state value. + +# Lies, Damned Lies, and Abstractions + +We mentioned that, technically, we didn't need monads for Guix. Indeed, many +(now deprecated) procedures take a store value as the argument: + +```scheme +(use-modules (guix derivations) + (guix store)) + +(with-store store ;remember this? + (build-expression->derivation store NAME EXPRESSION ...)) +``` + +This procedure, being deprecated, should never of course be used. For one +thing, it uses the "quoted build expression" style, rather than gexps, which we +will discuss another time. The best way to create a derivation from some basic +build code is to use the new-fangled `gexp->derivation` procedure, which happens +to be monadic! + +```scheme +(use-modules (guix gexp) + (gnu packages irc)) + +(define symlink-irssi + (gexp->derivation "link-to-irssi" + #~(symlink #$(file-append irssi "/bin/irssi") #$output))) +;; #<procedure 7fddcc7b81e0 at guix/gexp.scm:1180:2 (state)> +``` + +You don't have to understand the `#~(...)` form yet, only everything surrounding +it. We can see that this `gexp->derivation` returns a procedure taking the +initial state (store), just like our `%state-monad` procedures did. And to pass +this initial state, we used `run-with-state`. The equivalent for working with +the store is our old friend `run-with-store`! + +```scheme +(define symlink-irssi-drv + (with-store store + (run-with-store store + symlink-irssi))) +;; #<derivation /gnu/store/q7kwwl4z6psifnv4di1p1kpvlx06fmyq-link-to-irssi.drv => /gnu/store/6a94niigx4ii0ldjdy33wx9anhifr25x-link-to-irssi 7fddb7ef52d0> +``` + +Let's just check this derivation is as expected by reading the code from the +builder script. + +```scheme +(define symlink-irssi-builder + (list-ref (derivation-builder-arguments symlink-irssi-drv) 1)) + +(call-with-input-file symlink-irssi-builder + (lambda (port) + (read port))) + +;; (symlink +;; "/gnu/store/hrlmypx1lrdjlxpkqy88bfrzg5p0bn6d-irssi-1.4.3/bin/irssi" +;; ((@ (guile) getenv) "out")) +``` + +And indeed, it symlinks the `irssi` binary to the output path. Some other, +higher-level, monadic procedures include `interned-file`, which copies a file +from outside the store into it, and `text-file`, which copies some text into it. +Generally, these procedures aren't used, as there are higher-level procedures +that perform similar functions (which we will discuss later), but for the sake +of this blog post, here's an example: + +```scheme +(with-store store + (run-with-store store + (text-file "unmatched-paren" + "( <paren@HIDDEN>"))) +;; "/gnu/store/v6smacxvdk4yvaa3s3wmd54lixn1dp3y-unmatched-paren" +``` + +# Conclusion + +What have we learned about monads? The key points we can take away are: + +1. Monads are a way of chaining together procedures and values that are wrapped + in containers that give them extra context, like `maybe` values. +2. Guix provides a high-level monad API that compensates for Guile's lack of + strong types or an interface-like system. +3. This API provides the state monad, which allows you to thread state through + procedures such that you can pretend it doesn't exist. +4. Guix uses the store monad frequently to thread a store connection through + procedures that need it. +5. The store monad is really just the state monad in disguise, where the state + value is used to thread the store object through monadic procedures. + +If you've read this post in its entirety but still don't yet quite get it, don't +worry. Try to modify and tinker about with the examples, and hopefully it will +all click eventually! + +#### About GNU Guix + +[GNU Guix](https://guix.gnu.org) is a transactional package manager and +an advanced distribution of the GNU system that [respects user +freedom](https://www.gnu.org/distros/free-system-distribution-guidelines.html). +Guix can be used on top of any system running the Hurd or the Linux +kernel, or it can be used as a standalone operating system distribution +for i686, x86_64, ARMv7, AArch64 and POWER9 machines. + +In addition to standard package management features, Guix supports +transactional upgrades and roll-backs, unprivileged package management, +per-user profiles, and garbage collection. When used as a standalone +GNU/Linux distribution, Guix offers a declarative, stateless approach to +operating system configuration management. Guix is highly customizable +and hackable through [Guile](https://www.gnu.org/software/guile) +programming interfaces and extensions to the +[Scheme](http://schemers.org) language. base-commit: fe113595b6f7d8a1e1a0b814521f02783f9209c3 -- 2.39.1
"(" <paren@HIDDEN>
:guix-patches@HIDDEN
.
Full text available.guix-patches@HIDDEN
:bug#61214
; Package guix-patches
.
Full text available.
GNU bug tracking system
Copyright (C) 1999 Darren O. Benham,
1997 nCipher Corporation Ltd,
1994-97 Ian Jackson.