From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Michael Heerdegen Newsgroups: gmane.emacs.devel Subject: Re: pcase docs Date: Sat, 23 Jan 2016 15:22:08 +0100 Message-ID: <87vb6k8jfz.fsf@web.de> References: <83a8o8gcya.fsf@gnu.org> <87zivx5ree.fsf@web.de> <87r3h95qs3.fsf@web.de> <83wpr15mk1.fsf@gnu.org> <8737toiota.fsf@web.de> <83fuxo5xo4.fsf@gnu.org> <87mvrwzekd.fsf@web.de> <8360yk5sgm.fsf@gnu.org> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1453558951 23969 80.91.229.3 (23 Jan 2016 14:22:31 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 23 Jan 2016 14:22:31 +0000 (UTC) Cc: jwiegley@gmail.com, emacs-devel@gnu.org To: Eli Zaretskii Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Sat Jan 23 15:22:22 2016 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 1aMz53-0007Ik-Hd for ged-emacs-devel@m.gmane.org; Sat, 23 Jan 2016 15:22:21 +0100 Original-Received: from localhost ([::1]:57643 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aMz52-0005yZ-P9 for ged-emacs-devel@m.gmane.org; Sat, 23 Jan 2016 09:22:20 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:45481) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aMz4y-0005yS-Lp for emacs-devel@gnu.org; Sat, 23 Jan 2016 09:22:17 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aMz4x-0005AU-JP for emacs-devel@gnu.org; Sat, 23 Jan 2016 09:22:16 -0500 Original-Received: from mout.web.de ([212.227.17.12]:61871) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aMz4t-000578-Js; Sat, 23 Jan 2016 09:22:11 -0500 Original-Received: from drachen.dragon ([92.77.162.209]) by smtp.web.de (mrweb101) with ESMTPSA (Nemesis) id 0M1X0v-1a7EXx46if-00tXZm; Sat, 23 Jan 2016 15:22:09 +0100 In-Reply-To: <8360yk5sgm.fsf@gnu.org> (Eli Zaretskii's message of "Sat, 23 Jan 2016 15:35:37 +0200") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.0.50 (gnu/linux) X-Provags-ID: V03:K0:vk32EmsV1ySLmVTMfocTLG2WeWOBjQDmTA5KOj+boHBST8vRbaE wvhcSkaADPaqEFy2v3LQ6igZKpInm5GmBocSwL76o3KOC9VIq9PY4ork2hHxSsvqhPfKnRD VnekBKdci1z5pgMNRnU52u20rOz6nuKtXYC1FX0SYq928vaPPVr0X4GJ15peSZuW0mWlw8q oAR8Je4LIJJ7vcBH41CZQ== X-UI-Out-Filterresults: notjunk:1;V01:K0:eD3vcosaYMo=:+t1BLzCL6zzfqpkhEDoM2e oz3aSj3/lsQUlaKEEADDmgImuhx/X3765TLt4gz0XECGnW7mXDEu/ZE/gNZX/GdU0+OKMQYos GS4kfaIqg4jpehzJYswhvrDLpQcvTY70lxf/7sEMeSvioWZnVwpnqmy7wzn8PeplYw05mRxLD dNL0K4ZxOpYdr+2UpmjKBiLeVGAOP7WBxD7YiZJf1X2AV+YaOCuuyNYW6XvAYVyab0HW/xPVz YV4gcn0k5bxrMCrfQqe5Jzjxgqxols8O9tV7p//5rma050jAHRSc77nS4vMsYzu0dFPARFOm1 Il1BSO1Cn9iS5YVIBxxqby2VezebevahHaQjbHeLwDCXxJMIHyhARKIQLiMeCebrAinMH19fa y77IYYK0WlK9+T5k+9fZYr3mAMRyjoVMG64rxpNO5jmpd3ZvXAmBcSGdlT9S83N1LU/QpMsJO /SBzbxomeh4cjlhy+TncF3TJ+2U5sRbfaqp18BTuEpnwfppCoEnyIqYTK0W3lCTpw37auY5WJ X137HzIYsT97J7hWqL29wy6aQ5mk3SaaaDGVnJgSL0pghDj9sTLZWSVt6VVB6wZED7p2hGXRE CkG3aTq3+H2pfWkEwiohMAZEYAP2HNaEU2PjPfSQ2KJdrjOdMUbDVHWmOgs2g0MUsVAnPAA5M auEHZcl7BQMve/xGIm/7V6JmOhpl5/jacWTVpj2zcWEaAlU7PPuJPf96JGby/DeAh10yABXhg DWgy4+wUJLCjeqKdlCKnO3fW/ZZx/zRMAm9ySrRhhKvFiNELXrSmEkpH/vQJZAcogyCB9gAI X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 212.227.17.12 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:198642 Archived-At: Eli Zaretskii writes: > > Surely not! I just had the time to read the code, carefully (I had to, > > to be able to write my el-search.el thing). Most others don't care > > because they just don't care, I think. > > But this stuff is widely used, by several different individuals. How > do they know what to put in the code, if they, like me, are confused > about the purpose and the usage? Dunno. If it helps: I had spent really a lot of time into understanding pcase. More time than you can probably invest, doing all the work you do. And I made lots of errors over weeks after had finally internalized everything. > > Actually `pcase-defmacro' is easy to understand if you understood > > `defmacro': You define a pattern by its name and by the arguments it > > will accept. > > But 'pcase' patterns don't accept any arguments, they are matched > against a value of an expression. So how to understand "arguments" in > this context? pcase-defmacro can only define patterns that are used like function calls, (pattern-name pattern-args), like (pred fun) (guard expr) (or pattern...) In the sense of pcase-defmacro, FUN, EXPR or the PATTERNs play the role of arguments of the pattern. > > When the pcase "compiler" finds a pattern that is defined as a > > macro, it substitutes the "call" with the "code" returned by > > evaluating the macro definition with the given arguments. > > What do you mean by "call" here? A usage, or appearance, of the pattern. > And how to explain that a pattern that starts with "`(list" will > somehow end up using the pattern defined with "(pcase-defmacro map"? No, `map' doesn't define `, it defines a pattern named `map' (that currently is rarely used, or nowhere). Mmh, maybe you need just a simple example. Let's define a pcase pattern "greater-than" that matches any number greater than a specified one, so that e.g. (pcase 9 ((greater-than 2) t)) ==> t but (pcase (+ 0 1) ((greater-than (* 2 5)) t)) ==> nil. or (pcase "Hallo" ((greater-than 2) t)) ==> nil. You could define it like this: (pcase-defmacro greater-than (number) `(and (pred numberp) (pred (lambda (x) (< ,number x))))) Then any occurrence of (greater-than N) in any pcase pattern is simply substituted with (and (pred numberp) (pred (lambda (x) (< N x)))) That's it. Of course, the variable name "x" in the above definition could lead to name clashes (as with "conventional" macros), so you would better use an uninterned symbol. But with the shortcut version of `pred', we instead can just write (pcase-defmacro greater-than (number) `(and (pred numberp) (pred (< ,number)))) and avoid that problem this way. A different simple example: here is how you could define `guard' via `pred': (pcase-defmacro guard (expr) `(pred ,(lambda () expr))) HTH, Michael.