From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Oleh Krehel Newsgroups: gmane.emacs.devel Subject: Re: Question on pcase Date: Mon, 26 Oct 2015 16:55:15 +0100 Message-ID: <87io5tzkt8.fsf@gmail.com> References: <871tcngdv2.fsf@gmail.com> <87k2qe1u09.fsf@web.de> <83r3kmrtat.fsf@gnu.org> <87r3kl22zk.fsf@web.de> <837fmdzpf2.fsf@gnu.org> <87oafp659p.fsf@web.de> <831tclzly9.fsf@gnu.org> <87fv115t20.fsf@web.de> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1445874914 18510 80.91.229.3 (26 Oct 2015 15:55:14 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Mon, 26 Oct 2015 15:55:14 +0000 (UTC) Cc: Eli Zaretskii , Emacs Development To: Michael Heerdegen Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Mon Oct 26 16:55:08 2015 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1Zqk6z-0004AD-SN for ged-emacs-devel@m.gmane.org; Mon, 26 Oct 2015 16:55:06 +0100 Original-Received: from localhost ([::1]:53648 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zqk6z-0007iP-4G for ged-emacs-devel@m.gmane.org; Mon, 26 Oct 2015 11:55:05 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:38617) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zqk6i-0007i2-QM for emacs-devel@gnu.org; Mon, 26 Oct 2015 11:54:51 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Zqk6d-00006n-AN for emacs-devel@gnu.org; Mon, 26 Oct 2015 11:54:48 -0400 Original-Received: from mail-wi0-x235.google.com ([2a00:1450:400c:c05::235]:35495) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zqk6d-00006O-3x; Mon, 26 Oct 2015 11:54:43 -0400 Original-Received: by wicll6 with SMTP id ll6so120778188wic.0; Mon, 26 Oct 2015 08:54:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:references:date:in-reply-to:message-id :user-agent:mime-version:content-type; bh=qQPG+JnuR5lY2n9+bONhOnqcWc/hulomhER2vXjFUbQ=; b=eedicahd8QIw/76GAkm7Zo7L24npJMAeAOg4Q7f/ABF3hRw43+x8pooJrvsz1K2JgV akxkF9DqA31WcAzD4+EMxdfsNdn7xazfCVyIkyZeS73ZOUe9d7d2KU8EnQHsdIzoUQVs Ak0ZSTq0GQze7nV/w14gRxirZtuw7/lxnQF3vWJMKL8Aa/+iYUKGgVcbcJArEDFErNgM lINOS4MOPoin9Z/B4sEbne4MaX71ws5k/pbuX8XG4T4nve1kBZBoFh4uKEALyJmk062r YMrUjVFM8VcRLYgIvbuX7J9hmW0FlNQ1NrhZjtezxjB24xTQpfbzXGruQMKI5QnOdypn 1KZg== X-Received: by 10.194.92.138 with SMTP id cm10mr15554951wjb.6.1445874882363; Mon, 26 Oct 2015 08:54:42 -0700 (PDT) Original-Received: from firefly (dyn069045.nbw.tue.nl. [131.155.69.45]) by smtp.gmail.com with ESMTPSA id l5sm8398592wjf.11.2015.10.26.08.54.41 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Mon, 26 Oct 2015 08:54:41 -0700 (PDT) In-Reply-To: <87fv115t20.fsf@web.de> (Michael Heerdegen's message of "Fri, 23 Oct 2015 20:38:31 +0200") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.0.50 (gnu/linux) X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2a00:1450:400c:c05::235 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.14 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.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:192659 Archived-At: Hi Micheal, Thanks for your work, it could be useful to many people. Michael Heerdegen writes: > Someone who wants to try to learn pcase (Oleh? Oleh!) can help by > reading it and telling me if it is understandable, and send corrections > - or write a better introduction ;-) - and format it nicely for > inclusion into Elpa or Emacs when it turns out to help people. The reason I dislike `pcase' is not because I don't know how to use it (the basic rules are actually pretty simple), it's because I think it leads to code that's hard to understand, maintain and transform. I dislike the trivial `if-let' and `when-let' for the same reasons. I generally dislike any custom macro that includes `if' or binds variables. This is because I can't reason about the code that uses these macros unless I know exactly what they do in terms of binding variables and selecting branches. These macros don't follow the substitution model for procedure application (SMPA) [1], which is a valuable debugging technique for me. However, I'm willing to implement some tooling that will allow `pcase' to follow SMPA. Take this code for example, with "|" being the point: (pcase which (`all t) |(`safe (member fun completion--capf-safe-funs)) (`optimist (not (member fun completion--capf-misbehave-funs)))) Here, the (`all t) branch is selected, and the `which' symbol can be extracted from the context with `up-list'. I've implemented a function that prints "pcase: nil" if the selected branch doesn't match, and "pcase: t" when it matches. This way, when I'm debugging a code with `pcase' I can see which branch is the correct one without evaluating the code inside the branch. After this, I can step into the correct branch and use SMPA. Of course, this function would also need to bind the same variables that a `pcase' branch would bind. Taking your example: (pcase x ('a 1) ("Hallo" 2) |(thing (message "%s is neither equal to 'a nor to \"Hallo\"." thing))) This is what I would like to have: (equal (macroexpand '(eval-pcase-branch x (thing (message "%s is neither equal to 'a nor to \"Hallo\"." thing)))) '(progn (setq thing x) (message "pcase: t"))) After this, the inner body of the branch can be properly evaluated, since `thing' is bound to `x' now. So far, I've implemented some code that can check if each branch will be followed, see https://github.com/abo-abo/lispy/commit/d3ed4e4fee435a2a448ddc0722d07cd997ee59d3. But the code that binds the variables, e.g. (setq thing x) will likely be hard to implement. For instance, look at this example with macroexpand: (setq test '(1 . 2)) (pcase test (`(,foo . ,baz) (cons baz foo))) ;; => ;; (2 . 1) (macroexpand '(pcase test (`(,foo . ,baz) (cons baz foo)))) ;; => ;; (if (consp test) ;; (let* ((x (car test)) ;; (x (cdr test))) ;; (let ((baz x) ;; (foo x)) ;; (cons baz foo))) ;; nil) The macroexpanded code returns (2 . 2) when evaluated. This I don't understand. Although, it still works fine with `eval': (eval (macroexpand '(pcase test (`(,foo . ,baz) (cons baz foo))))) ;; => ;; (2 . 1) Maybe someone could explain the above, and also suggest the best way the create variable bindings from a pcase branch. thanks again, Oleh [1]: https://mitpress.mit.edu/sicp/full-text/sicp/book/node10.html