Live data from Hacker News

Reader Macros in Common Lisp (2014)

lisper.in

31–37 of 37 posts

Re: Reader Macros in Common Lisp (2014)

#31
post #20

so I started learning common lisp lately. It's insane how one can combine very high level language, with down-to-metal compilation. I think that's the only language allowing it? Absolutely impressive. Feels like writing python but compiling to native

Julia has similar capabilities, probably because it was heavily inspired by lisps. You can even modify Julia's compiler from within Julia. I often write code at a python level of abstraction and then use julia introspection to check the machine code that was generated.

So if I understand correctly, in Julia you programmatically look at generated machine code? Is there a way to modify it, or is just for making sure some optimizations were applied?

Re: Reader Macros in Common Lisp (2014)

#32

Earlier quoted context omitted.

Julia has similar capabilities, probably because it was heavily inspired by lisps. You can even modify Julia's compiler from within Julia. I often write code at a python level of abstraction and then use julia introspection to check the machine code that was generated.

So if I understand correctly, in Julia you programmatically look at generated machine code? Is there a way to modify it, or is just for making sure some optimizations were applied?

@code_native just lets you look at generated code, but Julia also uses macros frequently to give the compiler hints about how to compile your code. Some examples are @inbounds which disables bounds checks, @fastmath which is the local version of C/Fortran's --math-mode=fast, @simd which lets the compiler assume it can re-order loops (it will do so anyway if it can prove you won't notice). If you need more fine grained control (which is very rare) you can also emit LLVM bytecode (or direct assembly) directly.

Re: Reader Macros in Common Lisp (2014)

#33

Earlier quoted context omitted.

Julia has similar capabilities, probably because it was heavily inspired by lisps. You can even modify Julia's compiler from within Julia. I often write code at a python level of abstraction and then use julia introspection to check the machine code that was generated.

So if I understand correctly, in Julia you programmatically look at generated machine code? Is there a way to modify it, or is just for making sure some optimizations were applied?

We can modify the code at a few different levels. The easiest level is our untyped intermediate representation. The next easiest level is to modify things at the level of the LLVM code which is basically one step above assembler, and almost always better to work on than direct machine code (also machine code can be embedded in LLVM code if you need to). You can also use https://github.com/YingboMa/AsmMacro.jl if you like.

We are also working out interfaces to make it easier to programatically work on our typed IR through a technique and set of interfaces known as "abstract interpretation".

Re: Reader Macros in Common Lisp (2014)

#34
post #26

Okay let me get this straight... Are you saying that I don't need to use parenthesis as much in lisp? Instead I could define a few "reader macros" and then immediately have a different syntax? Say if I wanted to change (if (test-clause) (action1) (action2)) To if (test-clause) do (action1) else (action2) Is this actually possible inline in lisp, meaning I just need to import a library that implements the if-reader-ma…

Yes; for isntance see the CMU infix.cl file from the 1990's:

https://www.cs.cmu.edu/afs/cs/project/ai-repository/ai/lang/...

It has test cases at the bottom. like checking that f(a)*=g(b) expands to (setf (f a) (* (f a) (g b))))

The module installs the syntax under the #i dispatch characters; you wrap the infix expression with #I( ... )

Re: Reader Macros in Common Lisp (2014)

#35

Earlier quoted context omitted.

Julia has similar capabilities, probably because it was heavily inspired by lisps. You can even modify Julia's compiler from within Julia. I often write code at a python level of abstraction and then use julia introspection to check the machine code that was generated.

So if I understand correctly, in Julia you programmatically look at generated machine code? Is there a way to modify it, or is just for making sure some optimizations were applied?

Some abstractions are costly if the compiler doesn't optimize them away, so one use is to check if that happens. So one iterates changing the Julia code, not the machine code mostly.

Re: Reader Macros in Common Lisp (2014)

#36
post #26

Okay let me get this straight... Are you saying that I don't need to use parenthesis as much in lisp? Instead I could define a few "reader macros" and then immediately have a different syntax? Say if I wanted to change (if (test-clause) (action1) (action2)) To if (test-clause) do (action1) else (action2) Is this actually possible inline in lisp, meaning I just need to import a library that implements the if-reader-ma…

This works in many Common Lisp implementations: https://readable.sourceforge.io

Cool stuff, thanks.

Re: Reader Macros in Common Lisp (2014)

#37
post #28

Earlier quoted context omitted.

Out of curiosity, why not write a Scheme or Common Lisp compiler? Or just a subset?

I want an operating system from first principles. To write the smallest OS with the smallest language and the least amount of restriction. The goal is being able to replace every single part of the runtime/interpreter if you wish, but the OS itself starts with the fewest functions/features possible. cons, lambda, and the other usual suspects, a way of running actual native machine code, the homoiconicity of LISP, a r…

> You could build yourself a Scheme or a Common Lisp on top of it if you wish.

No offense, but if it were Common Lisp I would take a look, but I don't have much interest in learning a one off language.

Sounds like you're having so fun, though, so good luck!

Post reply on HN