From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Max Brieiev Newsgroups: gmane.emacs.devel Subject: Issues with named-led and pcase inside iter-defun. Date: Mon, 11 Sep 2023 11:47:17 +0300 Message-ID: <87leddnlka.fsf@gmail.com> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="34958"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Mon Sep 11 10:48:00 2023 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1qfcaO-0008uH-1t for ged-emacs-devel@m.gmane-mx.org; Mon, 11 Sep 2023 10:48:00 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qfcZq-00076A-Ii; Mon, 11 Sep 2023 04:47:26 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qfcZp-00075o-8H for emacs-devel@gnu.org; Mon, 11 Sep 2023 04:47:25 -0400 Original-Received: from mail-ej1-x62f.google.com ([2a00:1450:4864:20::62f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qfcZm-0002LS-RI for emacs-devel@gnu.org; Mon, 11 Sep 2023 04:47:24 -0400 Original-Received: by mail-ej1-x62f.google.com with SMTP id a640c23a62f3a-99c93638322so891600466b.1 for ; Mon, 11 Sep 2023 01:47:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1694422040; x=1695026840; darn=gnu.org; h=mime-version:message-id:date:mail-followup-to:user-agent:subject:to :from:from:to:cc:subject:date:message-id:reply-to; bh=u99TwgMGFEkNioGmtzn2TageyWHpO6Fmuh1bQtMyKiY=; b=MorVCGvY4MrSWbrG86DBikHpG3PBgDzuZu6ng3PN9nWXz++2RBNxsu9gyz69bCQ62V EeKNRTOXzQdhm6yTIXlg6tNJX0kIHqqYmzhO672o8EWGaBNDMqwkeMPoP4iN1pSrgSh8 XFedJdJGiQwy2NDnny4EiM9O0gFoSvgQ3R9joCrBkFYoSr7TQ1o47jYYUMIDIGr9NTx6 9PRdk1pdOEo1EwexcAvIHuxE4xFua3JbU9pirQFG/Wl5Hn7k6AAPwVBomgrPi6kej4vh dO/a+cViKWlRpAJ/Alf4FHK6UyvmCvA2WbS0NWyuSD2G4Qy6YFJ76WoeiwtRvpA9te+6 CDLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694422040; x=1695026840; h=mime-version:message-id:date:mail-followup-to:user-agent:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=u99TwgMGFEkNioGmtzn2TageyWHpO6Fmuh1bQtMyKiY=; b=Kl738u7IoV6jDvIYjiHRBnVnXoH3X64qfP/4jtbJyB80d3TvD9/80lrPnFpJBtxY5n +J5x7dYzcQQyZWq67MZsMVAEljweCQu88LeyKvPWfxzlYONGDArR0FMt/TYYRhok6pfe aZmnoXRlb3O+GZOcpdUHROmiuBMwBAP354Vh73ksyiZoNKJV/6Vx1Nn0tLBzQLBcu4nJ jIKWwPGR3yD2ahv7DZHulV1xE9h1dN9rwRRaDZeUbspt6t4afAXydlZ9KtGT8mlvztc6 MPq7va+NGEmPC/1kAsZFTHjjN5cLJyyratb5//n7zHqk9RHseTkKABEkP+FzJHdK9JPX MIJA== X-Gm-Message-State: AOJu0YxkUO61Kbos3LgmILoU+yqXndIaiR3FZuig0nyvhZ18nMm6Kj37 GgQjuB4vHh0/kI6OBbke8/xPwhKe9Lg= X-Google-Smtp-Source: AGHT+IH1kp97kIkve7sRcHNQYRcvpEIN3JfoKIegTiPWjx1HcKCx05pk6YUxNr5Qot04Ab4ulO4yFA== X-Received: by 2002:a17:906:5a63:b0:9a5:f038:a4c1 with SMTP id my35-20020a1709065a6300b009a5f038a4c1mr11167777ejc.26.1694422039739; Mon, 11 Sep 2023 01:47:19 -0700 (PDT) Original-Received: from vmax ([109.251.233.9]) by smtp.gmail.com with ESMTPSA id pk5-20020a170906d7a500b0098e2969ed44sm4937532ejb.45.2023.09.11.01.47.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Sep 2023 01:47:19 -0700 (PDT) Mail-Followup-To: emacs-devel@gnu.org Received-SPF: pass client-ip=2a00:1450:4864:20::62f; envelope-from=max.brieiev@gmail.com; helo=mail-ej1-x62f.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-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:310456 Archived-At: Hello, I've been trying to build some of my code on top of generator.el library. I've found that functions defined with iter-defun have some issues with named-let and pcase. This is a macro I use to wrap iter-defun and advance the iterator to the first yield expression: (defmacro co-defun (name args &rest body) "Define a coroutine. Automatically advance it to the first yield expression." (declare (indent defun)) (let ((co-name (gensym (concat (symbol-name name) "-co-")))) `(progn (iter-defun ,co-name ,args ,@body) (defun ,name (&rest passthrough-args) (let ((co (apply #',co-name passthrough-args))) (iter-next co) co))))) Now, here is a superficial example that involves both named-let and pcase: (co-defun lazy-sum () (named-let sum ((running-sum 0)) (pcase running-sum ((pred (< 10)) running-sum) (_ (sum (+ running-sum (iter-yield nil))))))) (setq it (lazy-sum)) (iter-next it 3) (iter-next it 5) (iter-next it 7) ; raises (iter-end-of-sequence . 15) So far so good. But let's make the example a bit more practical: (co-defun curling-parser/headers (chunk) "Parse HTTP headers. Returns alist of headers and the remaining data." (named-let parse-chunk ((chunk chunk) (aheaders (list))) (pcase chunk ;; header line ((rx line-start (let name (+ (not ":"))) ":" (* blank) (let value (+ (not "\r"))) "\r\n" (let tail (* anything))) (parse-chunk tail (cons (cons name value) aheaders))) ;; end of headers section ((rx line-start "\r\n" (let tail (* anything))) (list (nreverse aheaders) tail)) ;; suspend. Resumes parsing after the next chunk comes in (_ (parse-chunk (concat chunk (iter-yield nil)) aheaders))))) ;; Send chunks into the coroutine (setq parser (curling-parser/headers "Content-Ty")) (iter-next parser "pe: text/plain\r\nCache-Control: no-ca") (iter-next parser "che\r\n\r\nHello World!") The above has essentially the same structure as lazy-sum function, however this code fails with the error: (void-function cps-internal-yield) Is this a bug related to iterators in Elisp? After some trial and error, I found that iter-yield sometimes doesn't work inside pcase, so I moved it outside of pcase like this: (named-let parse-chunk ((chunk chunk) (aheaders (list))) (let ((result (pcase ...))) (if result result (parse-chunk (concat chunk (iter-yield nil)) aheaders)))) But now it started to fail with the same error outside pcase, but inside named-let! So my question is how do I work around this? There is no obvious way to tell whether iter-yield will succeed or fail inside pcase/named-let construct. Is it a fundamental limitation of iterators or just a bug that needs to be fixed? I have rewritten my code with a plain while loop, and it works without issues, but I like named-let/pcase approach much more.