Search code examples
lispmonadsquotecategory-theory

What's the Lisp `quote` special form in category-theoretical terms?


How should I think of quote in the context of Category Theory? Is quote a monad? What kind of things is it?


Solution

  • I do not think it plays any role in category theory since it does not have anything to do with computation and everything to do with parsing and syntax. It is not a monad.

    Imagine you want the string 5 + 1, what do you do? Well you enclose it in double quotes like "5 + 1" in the code and suddenly the result isn't 6 anymore but the string "5 + 1". Is "" something special in category theory? Is it a monad? Don't think so since it tells the compiler to create such a data structure that results in that string. In Haskell "hello" is just fancy syntax sugar for ['H', 'e', 'l', 'l', 'o']. In most languages a string is just a series of consecutive bytes, often an array.

    The quote special form performs the same operation syck that '(+ 1 2) isn't an expression anymore, but data. The compiler does (cons '+ (cons '1 (cons '2 '()))) and store the pointer to that for everywhere you have some literal ending with (+ 1 2). Because of that (eq '(1 2) (cdr '(+ 1 2)) might be #t but #f is just as reasonable outcome since the compiler might not optimize for shared structure.

    Moving forward you could imagine a fancy language that can dictate how the parser and compiler interpret literals. Almost all languages I know has string literals but if you made code to model complex numbers it would have been cool to say in the code that 3+5i should become tmp1 = make_complex 3 5 and that tmp1 is to be put everywhere the literal 3+5i exists in code. Why should numbers, strings, chars and perhaps regular expressions have special treatment?