help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: inline function expansion


From: Stefan Monnier
Subject: Re: inline function expansion
Date: Sun, 28 May 2023 10:57:19 -0400
User-agent: Gnus/5.13 (Gnus v5.13)

> 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




reply via email to

[Prev in Thread] Current Thread [Next in Thread]