From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Andrew Hyatt Newsgroups: gmane.emacs.devel Subject: Re: pcase defuns Date: Sun, 19 Dec 2021 10:33:12 -0500 Message-ID: References: <871r299e5j.fsf@gnu.org> Mime-Version: 1.0 Content-Type: text/plain; format=flowed Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="11489"; mail-complaints-to="usenet@ciao.gmane.io" Cc: emacs-devel@gnu.org To: Tassilo Horn Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Sun Dec 19 16:34:32 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 1myyCm-0002lC-K0 for ged-emacs-devel@m.gmane-mx.org; Sun, 19 Dec 2021 16:34:32 +0100 Original-Received: from localhost ([::1]:52520 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1myyCk-0007sm-QO for ged-emacs-devel@m.gmane-mx.org; Sun, 19 Dec 2021 10:34:30 -0500 Original-Received: from eggs.gnu.org ([209.51.188.92]:45658) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1myyBm-0007D2-An for emacs-devel@gnu.org; Sun, 19 Dec 2021 10:33:30 -0500 Original-Received: from [2607:f8b0:4864:20::f31] (port=38708 helo=mail-qv1-xf31.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1myyBj-00019W-VN; Sun, 19 Dec 2021 10:33:30 -0500 Original-Received: by mail-qv1-xf31.google.com with SMTP id o10so7212119qvc.5; Sun, 19 Dec 2021 07:33:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:in-reply-to:references:date:message-id :mime-version; bh=eZVmyEnX4UXnQMtwvpUia3fQ8aVhdVNaOVkbFVHeD6M=; b=SB48eFdCal30frD7/yDqyT8r9mFvdrpHzHb7gigQtTjJfW1WuXxu/wtGMQaR1tcicF bPqOdAqKrFRJzgjkn8d0U7NQsxALYSi/ZLRGJozpxMLIoPVGBmD6MspISH2wRUDlrXMA f4yAvRJojK3FghhY0Tt8nstfIy/qkJMzfrKKDKlSIJkVpH+4uBWwLoCvQNiF3GkLeb5F 04/Z7FZ4owELAG1H6RLwKjuJ+995LSc7be7mORIhjTW3VtkNMrxVa58EWoKg/GwKSIH4 EyV4Ll34AmM/T+bhGab/OqJ0AqHOxL45G0lc4NHedd7fUmq+HbdOnoDcgE8FEQx8vuO0 sE8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:in-reply-to:references:date :message-id:mime-version; bh=eZVmyEnX4UXnQMtwvpUia3fQ8aVhdVNaOVkbFVHeD6M=; b=mKU8I5CLSTi4DCiUmI85KWUIFjFAGvntLCHdiKWT6qtzz2vzMLCAWVXHrroitvfFyf s8r7Uny/VE5bTkZaFqrhgqhII8vfdJC3vuUdw1vJdSEsIuYrlUPidW2vvlwOu96o/Oy+ jI8oButX4ikL1seUQiwVa1FLfF6y9w2CC6BgAF2bAd4RIXvtrDwZjf9gpH9fsh44P5Zd DfmYt+JIz+za+0/wI6MeM1VHSTZr0lf4hNjc99Z9c3jnIfuSB3WLK8MEKDBJsfuPrmAN fAwQPDmymFJNaMcF4SwnnolIUGy+MdZW2rfijtJePFZgw+T+KH/GuM9hboLZsNDUKmGR Li/g== X-Gm-Message-State: AOAM531HwgzXV2Vhn9jRjWX9vsSxGPCDoEoA2+ZMwpQVNp8qLTLAGNXj 4/2AF/S089p04pXedQ/8rp862qKaDTU= X-Google-Smtp-Source: ABdhPJzARJdsTTAC0hWvvLrBFWCjBK9ExbTtbPvNmoL7YRXTXLeYiAF4/DJBEfGhb6fx/ZetMKAPCQ== X-Received: by 2002:ad4:4ea6:: with SMTP id ed6mr5191909qvb.54.1639927993971; Sun, 19 Dec 2021 07:33:13 -0800 (PST) Original-Received: from andrews-mbp.lan (pool-108-14-78-80.nycmny.fios.verizon.net. [108.14.78.80]) by smtp.gmail.com with ESMTPSA id x21sm8952916qkf.77.2021.12.19.07.33.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Dec 2021 07:33:13 -0800 (PST) In-Reply-To: <871r299e5j.fsf@gnu.org> X-Host-Lookup-Failed: Reverse DNS lookup failed for 2607:f8b0:4864:20::f31 (failed) Received-SPF: pass client-ip=2607:f8b0:4864:20::f31; envelope-from=ahyatt@gmail.com; helo=mail-qv1-xf31.google.com X-Spam_score_int: -12 X-Spam_score: -1.3 X-Spam_bar: - X-Spam_report: (-1.3 / 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, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no 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" Xref: news.gmane.io gmane.emacs.devel:282446 Archived-At: On Sun, Dec 19, 2021 at 09:34 AM Tassilo Horn wrote: > Andrew Hyatt writes: > > Hi Andrew, > >> As a part of a personal project, I wrote a way to define >> functions in an equivalent way to pcases. For example: >> >> (pcase-defun mytest (a b _) >> "Match on 'a 'b with the third argument a wildcard" "a b >> match") >> >> (pcase-defun mytest (c ,var _) >> "Match on 'c binding VAR, with the third argument a wildcard" >> (format "c %s match" var) ) >> >> (mytest 'a 'b 'c) -> "a b match" (mytest 'c 100 'c) -> "c 100 >> match" > > So that's basically similar to cl-defgeneric / cl-defmethod just > with pcase pattern matching, right? > > In that case, I think I like the cl approach better, i.e., > having a separate definition for the signature + several > implementations for variants. > > But since the order of patterns is very significant, I'm not > sure if it's a good idea to specify them separately. E.g., what > if there are pcase-defun's for mytest in several files? Then > you might get different behavior depending on the order how > files are loaded. And even in the "all patterns in mytest.el" > case: what if I re-evaluate some pcase-defuns after the file has > been loaded? I have a solution for this: a crude one: I just sort all the rules according to their flattened length. So the most complicated ones should get tried first, which I think is something that can communicated to the user. There's other ways the system could behave, such as offering to the user to define a priority for the rule. But definitely it's easy to make sure new rules can be added without crazy things happening. You've hit upon the issue with these types of systems. Powerful, easy to use, but dispatch rules are always vague. The same thing can happen with cl-defmethod, IIRC, where you can have two rules that both seem like they should take precedence. I think offering this functionality gives the user that choice - if they want very specific ordering, they can just use pcase in a function, either in a solution as you propose, or just use pcase itself, which is pretty straighforward. > > So probably I'd favor a pcase-defun which specifies all patterns and > bodies in a single definition, e.g., like: > > (pase-defun mytest > "Docstring" > ((a b _) > "a b match") > ((c ,var _) > (format "c %s match" var))) > > Bye, > Tassilo