From: Stefan Monnier <monnier@iro.umontreal.ca>
To: Lynn Winebarger <owinebar@gmail.com>
Cc: Philip Kaludercic <philipk@posteo.net>, help-gnu-emacs@gnu.org
Subject: Re: inline function expansion
Date: Sun, 28 May 2023 10:57:19 -0400 [thread overview]
Message-ID: <jwvh6rwjzhf.fsf-monnier+emacs@gnu.org> (raw)
In-Reply-To: <CAM=F=bD=k13KyhH3YFG=-Kj+nq5gCThEwoOM+aOAwG_nPmrBKw@mail.gmail.com> (Lynn Winebarger's message of "Sat, 27 May 2023 10:34:23 -0400")
> For my purposes, interfaces and realizations of interfaces are just a
> way to specify bindings of symbols in a more structured and
> encapsulated way than raw macros.
> I'm still spit-balling here, but I'm thinking a generic
> compile-time-dispatched (inlineable) "max" might be defined along
> these lines:
>
> ;; ordering interface has two parameters
> (define-interface ordering (bool-T elt-T)
> ;; return value of `lt' method satisfies bool-T interface
> (method (bool-T lt) (elt-T a) (elt-T b)))
>
> (define-realized-interface integer-ordering (ordering boolean integer)
> ;; bind lt method to built-in `<'
> (method lt <))
>
> (defgeneric max (< a b)
> "Determine the larger of a and b according to <")
>
> (define-inline-method max ((ordering <) &opaque a b)
> "Determine the larger of a and b according to ordering <"
> ;; < is treated syntactically as a dispatcher
> (if (< lt a b) a b))
>
> ;; because elisp does not allow applications in the operator position
> (define-inlined-method integer-max (max integer-ordering))
> ;; Alternatively,
> ;; (integer-ordering lt) reduces at compile time to something like
> ;; #s(interface-method
> ;; #s(interface-realization
> ;; #s(interface ordering boolean integer)
> ;; <)
> ;; lt
> ;; <)
> ;; which `max' is specialized for by define-inline-method above, so
> (defun integer-max (a b)
> ;; inlined by compile-time dispatch of the max generic method
> (max (integer-ordering lt) a b))
>
> ;; These should produce t
> (= (max integer-ordering 5 6) (integer-max 5 6))
> (= (max (integer-ordering lt) 5 6) (integer-max 5 6))
> (macroexp-const-p (macroexpand-all '(max (integer-ordering lt) 5 6)))
OK, that helps me understand a bit what you're talking about, thanks.
This example seems rather uninteresting (lots of extra code for very little
gain), so I strongly suspect this is not your prime-motivator.
What's the over-arching goal?
I think `define-inline` is definitely not a good starting point for
the above. OTOH you might be able to layer your `define-inlined-method`
on top of the current `cl-generic.el`. AFAICT the main thing you need is
some way to write a "call with enough «type» annotations" such that the
dispatch can be performed at compile time.
>> >> I'm still not sure why you're not using a `compiler-macro` which seems
>> >> to be exactly what you're after.
>> >
>> > I'm very finicky I suppose. I want to get constant expression
>> > evaluation as automatically as possible, to enable the compile-time
>> > dispatch cleanly. Or are you saying that generic methods can be
>> > directly made into compiler macros?
>>
>> And here I'm lost as well. AFAICT there's just as little "directly made
>> into" for define-inline as for compiler-macros (`define-inline` is just
>> a way to define a function and its `compiler-macro` in one go).
>
> Hopefully the example above clarified what I'm talking about
Somewhat. Still doesn't explain why you're focusing on `define-inline`
rather than `compiler-macro`. A compiler macro is just like a macro
except:
- It shares its name with a function.
- The returned expansion should behave identically to the
way the function call would behave.
- It's unspecified when or even *if* it's expanded (if it's not
expanded, the function is called at runtime instead).
- It receives an additional argument (the whole sexp), which it can use
to say "I don't want to expand anything this time, just call the
function at runtime instead".
IOW it's a mechanism to implement compile-time (well,
macroexpansion-time in our case) optimizations.
> Although I would change "inline-const-p" to test for function purity
> (exclude stateful closures or otherwise impure functions).
I think that would be a mistake. "const-p" doesn't mean that the return
value is pure but that the expression itself always returns the same value.
Stefan
next prev parent reply other threads:[~2023-05-28 14:57 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-05-07 14:32 inline function expansion Lynn Winebarger
2023-05-07 17:51 ` Basile Starynkevitch
2023-05-07 19:48 ` Philip Kaludercic
2023-05-07 20:16 ` Lynn Winebarger
2023-05-08 0:21 ` Emanuel Berg
2023-05-08 11:12 ` Lynn Winebarger
2023-05-08 2:03 ` Lynn Winebarger
2023-05-11 7:11 ` Lynn Winebarger
2023-05-12 6:25 ` Emanuel Berg
2023-05-18 14:56 ` Lynn Winebarger
2023-05-19 13:31 ` Stefan Monnier
2023-05-20 14:18 ` Lynn Winebarger
2023-05-20 15:32 ` Stefan Monnier
2023-05-21 12:47 ` Lynn Winebarger
2023-05-18 18:29 ` Stefan Monnier
2023-05-19 0:22 ` Lynn Winebarger
2023-05-19 13:07 ` Stefan Monnier
2023-05-20 15:01 ` Lynn Winebarger
2023-05-20 15:48 ` Stefan Monnier
2023-05-27 14:34 ` Lynn Winebarger
2023-05-28 14:12 ` Lynn Winebarger
2023-05-28 14:57 ` Stefan Monnier [this message]
2023-05-28 22:42 ` Lynn Winebarger
2023-05-29 2:59 ` Stefan Monnier
2023-06-06 22:38 ` Lynn Winebarger
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=jwvh6rwjzhf.fsf-monnier+emacs@gnu.org \
--to=monnier@iro.umontreal.ca \
--cc=help-gnu-emacs@gnu.org \
--cc=owinebar@gmail.com \
--cc=philipk@posteo.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).