From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Stefan Monnier Newsgroups: gmane.emacs.devel Subject: Re: [PATCH] Some improvements for cl-flet Date: Thu, 07 Oct 2021 14:23:36 -0400 Message-ID: References: <87bl4zqnqn.fsf@gmail.com> <87mto2gbpu.fsf@gmail.com> <87k0j6gbjg.fsf@gmail.com> <87pmshqvfk.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="37016"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux) Cc: emacs-devel@gnu.org To: akater Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Thu Oct 07 20:28:01 2021 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 1mYY7c-0009PE-Pt for ged-emacs-devel@m.gmane-mx.org; Thu, 07 Oct 2021 20:28:01 +0200 Original-Received: from localhost ([::1]:43234 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mYY7b-0005nd-6d for ged-emacs-devel@m.gmane-mx.org; Thu, 07 Oct 2021 14:27:59 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:44710) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mYY4J-0002kr-TF for emacs-devel@gnu.org; Thu, 07 Oct 2021 14:24:35 -0400 Original-Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]:52022) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mYY4G-0007bU-7N for emacs-devel@gnu.org; Thu, 07 Oct 2021 14:24:34 -0400 Original-Received: from pmg3.iro.umontreal.ca (localhost [127.0.0.1]) by pmg3.iro.umontreal.ca (Proxmox) with ESMTP id 2E4C64407D9; Thu, 7 Oct 2021 14:24:29 -0400 (EDT) Original-Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1]) by pmg3.iro.umontreal.ca (Proxmox) with ESMTP id 38E074407B6; Thu, 7 Oct 2021 14:24:27 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca; s=mail; t=1633631067; bh=coPGQhsfjN6kOiqqu+e//VtdQ//04vBkY1f6lch4B3M=; h=From:To:Cc:Subject:References:Date:In-Reply-To:From; b=TsUY8idgAnYY2FquAhjz13q74MafNZy9+YjCjuxaTvoYsyJPyT7fpoXcbbCRMHmM4 bKrq5IVxilupIogVy+RMV/izuCf93o38JWc41SbGLHnBuOPsgXYAStNPR3DVbb84dQ 6LuftX90od/fibk4bK44hB1umF0X7KNVfSMSwSGuE7Kx+v56tb0pQKAIlc3HQt1l4K 7PsgH+reO24ki4gmlEcb1sDZaV/E3ST3yaWk0qG1a2Q3Mmr6+rUL3Vld82TLFUTOyj fdhu2SJ224sI0VxhXis4Iy7MmxS4gMw2/PxvcSJiVKNLQZZjDcrQaiGjwwfGa1FmP9 y/85pj9ZGjskQ== Original-Received: from alfajor (lechon.iro.umontreal.ca [132.204.27.242]) by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id 21EDA12037A; Thu, 7 Oct 2021 14:24:27 -0400 (EDT) In-Reply-To: <87pmshqvfk.fsf@gmail.com> (akater's message of "Thu, 07 Oct 2021 05:02:23 +0000") Received-SPF: pass client-ip=132.204.25.50; envelope-from=monnier@iro.umontreal.ca; helo=mailscanner.iro.umontreal.ca X-Spam_score_int: -42 X-Spam_score: -4.3 X-Spam_bar: ---- X-Spam_report: (-4.3 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, RCVD_IN_DNSWL_MED=-2.3, 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.23 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" Xref: news.gmane.io gmane.emacs.devel:276511 Archived-At: >> Maybe it might be worthwhile splitting it into 2 or 3 patches so as to >> better see how we got to the level of complexity. >> See more comments below. > > Local setfs certainly can be added later (or simply dropped, until > better times). It will simplify the patch. I'm fine adding it now, but I'd like to see the intermediate steps to better understand what is due to what. >> Why do we have/need this? Does it work with other things that use >> gv-places, like `push`, `pop`, `cl-callf`, ...? If so, how? >> If not, then we need another approach which does. > > I added support for local setfs mostly I understand why we want support for local setf functions. My question was why do we need the code I referred to in order to implement that feature. The patch below seems to work for me. >> I thought handling `cl-flet` of (setf foo) would amount to calling >> `gv-setter` to get the symbol corresponding to `(setf foo)` and then >> c-flet-binding that symbol instead of `(setf foo). > At the very least, gv-setter will fail for local (setf car) and so on. > I don't know how cl-flet should treat such things (in Common Lisp it's > UB) but I saw that cl-flet performs local redefinition of car just fine > so I followed suit. I'd also consider such a (setf car) as UB, but as for how does it behave in practice: basically `car` has a `gv-expander` and those take precedence over a (setf car) function. >>> +(defun cl--expand-flet (env body &rest flet-expanders-plist) >>> + "Return a form equivalent to `(cl-flet ,bindings BODY) >>> +where bindings correspond to FLET-EXPANDERS-PLIST as described below. >>> + >>> +ENV should be macroexpansion environment >>> +to be augmented with some definitions from FLET-EXPANDERS-PLIST >>> +to then expand forms in BODY with. >>> + >>> +FLET-EXPANDERS-PLIST should be a plist >>> +where keys are function names >>> +and values are 0-argument lambdas >>> +to be called if the corresponding function name is encountered >>> +in BODY and then only (that is, at most once). >> >> Why "at most once"? > > We don't want to end up calling different functions in different > instances of local function calls which share the same name within a > single cl-flet form, for whatever unforseeable reason this might happen > (like poorly written flet-expander or poorly written exp in (func exp)). The question is why enforce this here rather than elsewhere. It restricts the possible uses of `cl--expand-flet` and I get the impression that it doesn't save us any significant complexity in the callers (it saves us less complexity there than it costs us here). E.g. in: > (cl--expand-flet macroenv (cdr parsed-body) > 'cl-call-next-method (lambda () (push cnm uses-cnm) > (list nil cnm)) > 'cl-next-method-p (lambda () (push nmp uses-cnm) > (list nil nmp))) If the functions where called multiple times rather than "at most once", we'd just replace the `push` with `cl-pushnew`. But that's a very minor cosmetic detail. Either way works, I was just curious why you did it this way. Stefan diff --git a/lisp/emacs-lisp/cl-macs.el b/lisp/emacs-lisp/cl-macs.el index 1852471bcbb..ad0477e3b68 100644 --- a/lisp/emacs-lisp/cl-macs.el +++ b/lisp/emacs-lisp/cl-macs.el @@ -2030,7 +2030,10 @@ cl-flet (let ((binds ()) (newenv macroexpand-all-environment)) (dolist (binding bindings) (let ((var (make-symbol (format "--cl-%s--" (car binding)))) + (fname (car binding)) (args-and-body (cdr binding))) + (if (eq (car-safe fname) 'setf) + (setq fname (gv-setter (cadr fname)))) (if (and (= (length args-and-body) 1) (symbolp (car args-and-body))) ;; Optimize (cl-flet ((fun var)) body). (setq var (car args-and-body)) @@ -2038,7 +2041,7 @@ cl-flet (car args-and-body) `(cl-function (lambda . ,args-and-body)))) binds)) - (push (cons (car binding) + (push (cons fname (lambda (&rest args) (if (eq (car args) cl--labels-magic) (list cl--labels-magic var)