Derivative Flipping

(blog.sigfpe.com)

91 points | by dmit 2 days ago

5 comments

  • mjfisher 16 hours ago
    This looks really interesting but I'm falling down on the first piece of maths (and the linked pdf that attempts to clarify in the article).

    Until this point, a derivative for me has been a derivative of an continuous function.

    Is this arbitrarily defining the derivative of a particular set? (e.g. "The derivative of a set of strings S with respect to a symbol a is the set of strings gener- ated by stripping the leading a from the strings in S that start with a")

    Is that analogous to regular differential calculus, or a completely different species of derivatives and it just shares the same name?

    • ky3 14 hours ago
      You could search for "brzozowski left inverse" as those links might contain better explanations for you.

      The gist of the adjunction < R, aS > = < a\R, S >, where R is a regex and S is a string, is that: R matches aS (that is, the string S but prepended with 'a') iff the left inverse of R wrt 'a', matches S.

      The left inverse here is the unconcatenation of R on the left with 'a'.

      (Recall that the algebra of regexes builds them up from unions, concatenations, and kleene stars on top of constants: the empty set, the null string, and the alphabets of your language. The novelty is that you can invert concatenation—on both ends!)

      • mjfisher 7 hours ago
        Thanks. That helped find some links - looks like the concept I was missing was Formal Language. There is learning to be done!
      • andai 13 hours ago
        Thanks, but

        >Recall...

        Where was I supposed to have learned all that stuff?

        Edit: In undergrad, apparently:

        Introduction to Automata Theory, Languages, and Computation by Hopcroft, Motwani and Ullman

        • hamonrxe 10 hours ago
          A cyberciligistic font in Lisp.
    • ssivark 8 hours ago
      The best analogy to hold in mind is that the derivative is the generator of infinitesimal translations i.e. the operator that moves the function over a bit: given f(x) and the derivative tells you f(x+dx).

      In the context of strings, moving by dx is like a discrete shift -- knocking off a piece of the string and continuing to match the regex against what's left (if the match still survives).

      The non-trivial nature of this comes when the template R is not just a simple string, but a more complicated program with branches (in a DSL corresponding to an algebra), and the idea is that the Leibniz rule of calculus derivatives correctly models how the matching decisions ought to be recursively delegated to simpler templates. This feels natural if we think of a string / template as concatenating operators and then apply the product rule of derivatives.

      • mjfisher 7 hours ago
        That's really helpful actually (although I'll still need some more googling). I think the insight that concatenation for strings is a bit like a multiplier here helps
    • yorwba 16 hours ago
      It is analogous to regular calculus in some ways but not others. For example, the derivative of xyz with respect to x is yz in either case. The derivative of yz with respect to x is 0 or the empty set, respectively. But the analogy stops working when you have the symbol appear somewhere other than the beginning, because multiplication of numbers is order-independent and string concatenation is not.
    • abe27342 15 hours ago
      There are some other good answers pointing to concrete leads. There is the formal notion of a derivation (see https://en.wikipedia.org/wiki/Derivation_(differential_algeb... ) which generalizes the derivatives in differential calculus you are familiar with to other more exotic objects. For example, this allows defining derivatives on combinatorial species.

      The Brzozowski derivative is not strictly one of these, due to its odd product rule expression for concatenation. https://math.stackexchange.com/questions/5024248/are-brzozow... has some good explanations if you have any algebra background.

      edit: The comments on the math stack exchange link seem to point out that while the Brzozowski derivative is not a derivation over some algebra, it is over a certain (f,g)-bimodule, so works in some even more general sense. Interesting!

      • dapperdrake 15 hours ago
        Or Radon-Nikodym for measurable functions.
    • nagaiaida 7 hours ago
      you might be interested in these papers if you want to keep digging at this. regular degular power rule derivatives fall out of reasonable operations on algebraic data types (zippering in particular)

      http://strictlypositive.org/diff.pdf

      http://strictlypositive.org/dfordata.pdf

      https://conal.net/papers/language-derivatives/

      edit: oh i see i'm not the first to mention this lol

    • andai 13 hours ago
      Found Brzozowski's paper,

      Derivatives of Regular Expressions (1964)

      https://dl.acm.org/doi/epdf/10.1145/321239.321249

      See also his excellent

      Survey of Regular Expressions and Their Applications (1962)

      https://sci-hub.ru/storage/dace/2342/6f7bde55a02bda6a6c955be...

    • ufo 14 hours ago
      The name _derivative_ is sometimes used to describe operators that resemble calculus derivatives: here, the operator is linear and there are analogues of the chain and product rule. You might also see the name _formal derivative_ when they want to emphasize that the similarity is mainly algebraic and about physical rates of change.
    • convolvatron 16 hours ago
      you missed the reference to the Brzozowski derivative, which has a wiki page
  • hamonrxe 10 hours ago
    [dead]
  • zergrush 12 hours ago
    not into math so I had to ask what Gemini Hydrogen thought and I'm still confused as to what the implications are

    if it doesn't help me earn more money or improve my life I generally find these academic tidbits rather boring and not worth paying attention to.

    I mean if you get it and you find its meaning significant thats certainly valid too

  • gatane 10 hours ago
    Interesting how no one has pointed this is a lot like zippers from functional programming, which themselves are derivatives of algebraic data types.

    "The derivative of a (regular) type is its type of one-hole context (McBride)." Yup. Grab any data structure, make a hole; that new type is its derivative.

    Yes. You can take the derivative of a """struct""" in lambda land. Have fun. Use google scholar for the pdf.

    https://journals.sagepub.com/doi/abs/10.3233/FUN-2005-651-20...

    Oh, and if you want to formalize this idea, you get containers; think of the taylor expansion but for data types. What is x made of? Where does it live?

    :)