HNHacker News
TopNewBestAskShowJobs

ICWiener

70 karma · joined February 1, 2013

submissionscomments
ICWiener··on Feature Macros, an Alternative to Feature Expressions in Clojure
I am looking at one of the examples:

https://github.com/feature-macros/clojurescript/blob/feature...

But if I run Clojure on the JVM, the following fails:

     (def host nil)
     (defn foo [] (when (= :cljs host)
                    (gstring/format "something")))
... the error being that there is no "gstring" namespace.

Since the proposed solution with macros would produce such code (I am right?), how does it solve the issue of namespaces that only exist in a specific platform?

ICWiener··on Lisp Macro
I can understand that debugging expanded code is not fun.

On the other hand, LOOP allows to naturally express iteration patterns that can be error-prone with other languages: you hardly produce off-by-one bugs contrary to hand-crafted loops.

See the Appendix of the Iterate manual for a comparison of looping constructs in CL: http://common-lisp.net/project/iterate/doc/Don_0027t-Loop-It....

ICWiener··on Why Lisp macros are cool, a Perl perspective (2005)
The common part is the special operator UNWIND-PROTECT. You can use it to define a function (not a macro). This is in fact a recommended way to design your program: define a CALL-WITH-X function and write a simple WITH-X macro that expand in a funcall to that function, with a more convenient syntax (see http://random-state.net/log/3390120648.html).

Scala resource management techniques are built upon try/finally blocks, aren't they? If you want, you can use Scala ARM, which is a non-standardized library. Then, you can use a monadic approach, an imperative one, etc. This boils down to convention and idioms, which are not standardized either.

You seem to think that there should be an autorithy which acts as the gatekeeper between the good things and the bad things that are added to a language, and that the ability to "mess" with the abstract syntax tree leads to chaos (I guess you alignment is Lawful Good). We disagree. CL is designed to evolve smoothly over time, and macros are one of the features that enable this.

ICWiener··on Why Lisp macros are cool, a Perl perspective (2005)
The regex example is more about partial evaluation than constant inlining. So instead of commenting on "random one-liner snippets" and "structure", which I find boring, I'd prefer to share an example of partial evaluation techniques applied to Haskell:

http://repository.readscheme.org/ftp/papers/coutts_transfer_...

The whole essay is interesting. Then, at section 4, we discover that the author relies on Template Haskell:

Using the Template Haskell infrastructure confers a number of advantages. There are the straight-forward obvious advantages that TH provides an abstract syntax tree, a parser, pretty printer and a monad providing unique name supply and error reporting. Because it is part of a compiler, the compiler also does the other ordinary semantic checks including typechecking. This is obviously useful since our partial evaluator can only be expected to work for well formed and typed Haskell programs and so we are spared from performing these checks. A less obvious advantage is that we may be able to do binding time checking in terms of a slightly modified Haskell type system and implement it using the existing compiler’s type checker rather than having to write one from scratch.

ICWiener··on Why Lisp macros are cool, a Perl perspective (2005)
Sure, macros can help define otherwise lacking features: you don't have promises in CL? (ql:quickload :cl-async-future). Here, you have promises.

But that is not my point. What I tried to say is that macros are orthogonal to high-level constructs because they are used at the compiler level, for meta-programming purposes.

For example, take "cl-ppcre", a.k.a. perl-like regexes.

When you do (ppcre:replace-regex "<my regex>" ...) with a constant string, there is a compiler-macro that calls create-scanner at compile-time to avoid parsing and building the scanner at runtime.

Maybe you think that regexes should be built-in and that a sufficiently smart compiler should be able to do the same job. But how is this different than what we have in CL? the compiler is sufficiently smart because we allow the programmer to interact with the compilation process.

Macros are adequate high-level constructs when you want to manipulate programs as data.

ICWiener··on Why Lisp macros are cool, a Perl perspective (2005)
I forgot about lenses, thanks.

About non-locality of macros vs. functions:

Macros, being functions, provide this kind of assurance, too at the syntax tree level: you only operate on the local, current subtree. They can indeed modify the meaning of symbols in that subtree, but I think you are taking things to the extreme. Off course, an arbitrarly nasty macro "far up in the tree" (how deep are the typical functions, anyway?) could do horrible things. Also, we must ban sugar because if someone eats too much sugar, that someone dies.

However, the average usage is more subtle. For example, when you DEFUN a function, you are registering a function in your current environment (side-effects) through a macro, which does not dramatically change the semantics of the body of the function, compared to, say, an anonymous LAMBDA (the only change is, I think, the ability to RETURN-FROM a named function).

CL is not a functional language: you can loop over lists or across vectors, you can setf special variables and mutate strings. You have static and dynamic non-local exits (return, throw/catch, signal) and even goto's (tagbody). That does not mean that you should use them all and go against best practices and common sense. Just because a feature could be misused does not make it irrelevant.

For the record, I have nothing against functional languages (or any other language, really (except of course PHP ;-)). It just seem unfair to attack macros. They should be avoided whenever possible, but they have some uses: C (macros), C++ (templates), and even Haskell and Scala try to have them:

http://scalamacros.org/usecases/index.html

https://downloads.haskell.org/~ghc/7.6.1/docs/html/users_gui...

http://neilmitchell.blogspot.fr/2007/01/does-haskell-need-ma...

Finally: macros are not a hack used to fix a supposedly missing feature of the language (e.g. "I don't need macros because I have lazy evaluation", etc.). Macros allow programmers to interact with the compiler (reader macros, compiler-macros): they are the primitive provided to perform meta-programming tasks.

ICWiener··on Why Lisp macros are cool, a Perl perspective (2005)
Don't forget ONCE-ONLY
ICWiener··on Why Lisp macros are cool, a Perl perspective (2005)
It is true that macros are regarded as a last resort mechanism: whenever you can use functions, or generic functions, you are encouraged to do so.

Also, macros are nothing more than functions: they operate on the syntax tree, at compile-time, but they are still functions. All language implementations (compilers, interpreters), will have somehow a need to manipulate syntax trees: in CL, this facility is exposed to the programmer (hence, the "programmable programming language" quote from John Foderaro).

> If they need more things, add those things as first-class features,

That's not the philosophy behind Lisp. You don't wait for a new relase of the standard (C++11, C++14, ...) and for compiler support: if you need to do crazy things with syntax trees, you are welcome. Then you may pulbish it and other people will use your libraries, like it is done for any other libraries in any languages.

> they still lead to unreadable code because you can't tell what a given piece of code does until you understand which parts are macros and understand all of them

So, in order to understand a pice of code, I must: (1) know what is, or isn't, a macro, and (2) if it is a macro, know what it does. The same applies for any function: don't apply a function if you don't know what it does.

You can rely on docstrings, macroexpand, as well as live coding techniques (describe, inspect) to help you understand complex operations. But saying that macros make code inherently "unreadable" is not justified: what about monads? do they simplify code or complexify it? like macros, they introduce an abstraction (loop) and can help with consistency (with-open): some people dislike it, because they feel they loose sight of what is "really" happening, but this is not a bad idea by itself.

> But don't throw up your hands and let every library rewrite code willy-nilly

Strawman: people don't really do that.

Regarding your example, I really would like to know how typeclasses can be used to solve `(setf (car list) x)`. If you have a concrete example of what you mean, for example in Haskell, please explain.

ICWiener··on Reproducible SBCL builds – a month ahead of schedule
This is a great achievement: bitwise-identical output of the compiler when bootstrapped with different CL implementations.

It is inspiring to see people like Christophe Rhodes work on SBCL and make tremendous progress.

He is sufficiently honest to write how he missed an obvious typo (guess what, this happens with every language, no matter how hard you try to avoid them). Unfortunately, this is now the most discussed part of the article, which is sad.

ICWiener··on Shill: A Secure Shell Scripting Language
SCSH: http://scsh.net/
ICWiener··on Hermit: a font for programmers, by a programmer
Thanks, it is always nice to see a monospaced font available. I especially like the light version. I also like the fact that symbols that look similar (e.g. "1lI") are very distinct from each others.
ICWiener··on Build Tools – Make, no more
See ASDF, for example (http://common-lisp.net/project/asdf/).
ICWiener··on Build Tools – Make, no more
If you want to use Make, read "Recursive Make Considered Harmful" first (http://aegis.sourceforge.net/auug97.pdf).
ICWiener··on A Haskell Programmer Tries to Learn Racket
> ... recognized the program was useless and boiled it down to a 3 instruction loop

The loop itself is quite useless, why wasn't it also removed ? (Just kidding)

I don't have anything agains Lua or hash tables in principle. And of course tables are used in practice in CL code, but they aren't the primary data-structure.

> __index can be another table to searched if the lookup on the child fails (which in turn can have its own parent and so on)

> the first time a function like "GL.CreateShader" is called, the lookup fails, and the __index metamethod mangles the index name a bit and in turn calls (on Windows) the C function wglGetProcAddress to look up its address, which it then stores in the original table. Using this library, you can write code that uses GL functions willy-nilly, and their addresses will automatically be looked up at runtime the first time they are used.

So, it is an association list implemented using tables, where links are given by the __index property, using a metatable.

So maybe it is convenient after all to have a very simple data-structure like cons in a language and let more complex data be implemented with it, instead of the opposite.

ICWiener··on A Haskell Programmer Tries to Learn Racket
> I strongly doubt your compiler will transform all the literal alists in your program into hash tables,

Time for experiment. SBCL doesn't, as far as I know. But you seem to imply that it is always better to use hash-tables, and I don't think so (even though hash-table can be implemented in a smart way, like in Lua).

Under roughly 10 elements, alist result in shorter code.

     (defun my-fun (x)
       (declare (optimize (speed 3) (debug 3) (safety 0))
                (type (member a b c) x))
       (let ((a '((a . 10) (b . 21) (c . 31))))
         (cdr (assoc  x a :test #'eq))))

     (disassemble #'my-fun)
     ; disassembly for MY-FUN
     ; Size: 53 bytes
     ; 04A7FF2F:       483B1592FFFFFF   CMP RDX, [RIP-110]         ; 'A
                                                                   ; no-arg-parsing entry point
     ;       36:       7511             JNE L1
     ;       38:       488B0D91FFFFFF   MOV RCX, [RIP-111]         ; '(A . 10)
     ;       3F: L0:   488B5101         MOV RDX, [RCX+1]
     ;       43:       488BE5           MOV RSP, RBP
     ;       46:       F8               CLC
     ;       47:       5D               POP RBP
     ;       48:       C3               RET
     ;       49: L1:   488B1D98FFFFFF   MOV RBX, [RIP-104]         ; '(B . 21)
     ;       50:       488B0D89FFFFFF   MOV RCX, [RIP-119]         ; '(C . 31)
     ;       57:       483B157AFFFFFF   CMP RDX, [RIP-134]         ; 'B
     ;       5E:       480F44CB         CMOVEQ RCX, RBX
     ;       62:       EBDB             JMP L0
This is roughly equivalent to a "case" construct (not shown here). Then, this is what I have with a hash-table:

     (let ((table (make-hash-table :test #'eq)))
       (declare (optimize (speed 3) (debug 3) (safety 0)))
       (setf (gethash 'a table) 10)
       (setf (gethash 'b table) 21)
       (setf (gethash 'c table) 31)
       
       (defun my-fun-hash (x)
         (declare (optimize (speed 3) (debug 3) (safety 0))
                  (type (member a b c) x))
         (gethash x table)))

     (disassemble #'my-fun-hash)
     ; disassembly for MY-FUN-HASH
     ; Size: 115 bytes
     ; 05467EB0:       .ENTRY MY-FUN-HASH(X)                       ; (FUNCTION (#)
                                                                   ;  (VALUES T # ..))
     ;      EE8:       8F4508           POP QWORD PTR [RBP+8]
     ;      EEB:       488D65E8         LEA RSP, [RBP-24]
     ;      EEF:       488B7805         MOV RDI, [RAX+5]
     ;      EF3:       4C8BC7           MOV R8, RDI
     ;      EF6:       498BF8           MOV RDI, R8
     ;      EF9:       BE17001020       MOV ESI, 537919511
     ;      EFE:       488B05FBFDFFFF   MOV RAX, [RIP-517]         ; #<FDEFINITION object for SB-IMPL::GETHASH3>
     ;      F05:       B906000000       MOV ECX, 6
     ;      F0A:       FF7508           PUSH QWORD PTR [RBP+8]
     ;      F0D:       FF6009           JMP QWORD PTR [RAX+9]
     ;      F10:       6A20             PUSH 32
     ;      F12:       B9604F4200       MOV ECX, 4345696           ; alloc_tramp
     ;      F17:       FFD1             CALL RCX
     ;      F19:       59               POP RCX
     ;      F1A:       488D490B         LEA RCX, [RCX+11]
     ;      F1E:       E917FFFFFF       JMP #x1005467E3A
The function with an hash-table has a constant size relatively to the number of elements in the table, which is fully known at compile-time. The size of the code with a case/alist grows linearly with the number of elements.

Now, let's compare speed.

In the following versions, I have added more elements in the alist, just to be sure the code with an hash-table is the shortest (from 'a to 'l).

     (time
      (dotimes (i 10000000)
        (dolist (x '(a b c d e f g h i j k l))
          (my-fun-hash x))))

     (time
      (dotimes (i 10000000)
        (dolist (x '(a b c d e f g h i j k l))
          (my-fun-case x))))

     (time
      (dotimes (i 10000000)
        (dolist (x '(a b c d e f g h i j k l))
          (my-fun x))))
Results:

   HASH:
    2.491 seconds of real time
    33,072 bytes consed
  
   ALIST:
    0.852 seconds of real time
    0 bytes consed
  
   CASE:
    0.778 seconds of real time
    0 bytes consed  
 
Even though in terms of footprint, the code tend to be larger with alist quite rapidly (10 elements), the resulting code is faster and does not allocate memory.

Also, just to clarify, alist and property lists have a different behavior than hash-tables, namely that the sequential access allows you to shadow values from another list: if you write (cons (cons 'a b) older-alist), you have a new list where the value for key 'a is b, and where values for other keys are those found in older-list (even though older-list also contains key 'a).

I don't quite remember what is my point anyway ;-) but it was fun to test the different behaviors.

ICWiener··on A Haskell Programmer Tries to Learn Racket
> but a quick search didn't reveal a syntax for hash table literals, which I would consider to be a prerequisite for calling them first class citizens

http://en.wikipedia.org/wiki/First-class_data_type

Hash-tables are part of the standard, and available in conforming implementations out-of-the-box. (make-hash-table) produces an instance of a table which can be passed as argument to function, be assigned to variables, ... I don't think syntax defines what feature is or isn't first-class, even though it has an impact on it's usage.

Adding support for litteral hash-tables is possible, as it was already said: define a reader macro; that macro could even simply use the utility function "plist-hash-table", from Alexandria, which is used like this:

     (alexandria:plist-hash-table '(:a 1 :b 2))
So that you could write #H{:a 1 :b 2} and get what you want.

On the first hand, people say that CL is bloated, but on the other hand, they want to have everything available by default. Do you expect Python or C++ implementations to come with regular expression built-in, or is it okay if you need to add an 'import' statement or link with Boost?

CL allows you to define libraries that can change even the basic surface syntax, for your convenience. Considering that there is more than enough defined in the specification, what benefits would be in having an "official" hash litteral syntax? consistency accross different projects? many people use the "cl-ppcre" regex library without problem.

Yes, regular expressions come in a library even though they could easily be labelled as a fundamental feature of a language, and hence be expected to be "first-class", or "built-in"; but again, that is not the case in CL, which is neither Perl nor Lua.

And about keyword parameters, this is just the surface syntax, which is on purpose based on a simple, basic representation of the syntax tree as lists of expressions.

Then, the compiler will work with the argument list in your function definition and take one or another approach to represent them in the object code. And I doubt that they are stored in a hash-table, which is unlikely an appropriate way to handle a couple of keyword arguments.

Similarly, when you define a class, everything is declared by a simple list of slots, where slots are lists of parameters: but in fact, the class might store the actual instance slots in a hash-table, an array, a list, a database or a memory mapped file.

The fact that you are mainly manipulating lists does not mean that everything is eventually represented as supposedly inefficient linked-list at runtime.

ICWiener··on C++ – From goto to std::transform
Honestly, I understand; I am really bothered too when I see people applying techniques without even thinking about performance implications (and btw, this is also true for security, correctness, portability ... implications).

C++ is viewed as the ultimate language when it comes to performance, for good reasons; of course, if I ever need a fast program, I will consider it.

But as an exercise, why not try to optimize the CL code, first?

By using type declarations and avoiding allocating memory, here is an optimized CL version that runs under 0.01s, beating the C++ code (note that the original C++ code could be optimized too; e.g. why check the command-line argument every time?).

     (defpackage :test (:use :cl))
     (in-package :test)

     (defconstant +vector+
       (map '(simple-array fixnum (10000))
            (lambda (x) (declare (ignorable x)) (random 32767))
            (make-array 10000 :element-type 'fixnum)))

     (let ((result (make-array 10000 :element-type 'fixnum)))
       (declare (type (simple-array fixnum (10000)) result))
       (flet ((square (x)
                (declare (optimize (speed 3) (safety 0) (debug 0))
                         (fixnum x))
                (the fixnum (* x x))))
         (defun square-vect ()
           (declare (optimize (speed 3) (safety 0) (debug 0))           
                    (type (simple-array fixnum (10000)) +vector+))
           (loop for i from 0 to 9999
              do (setf (aref result i) (square (aref +vector+ i))))
           result)))

     (time
      (dotimes (i 1000)
        (square-vect)))
       
     Evaluation took:
       0.009 seconds of real time
       0.008000 seconds of total run time (0.008000 user, 0.000000 system)
       88.89% CPU
       22,650,364 processor cycles
       0 bytes consed
       
     Evaluation took:
       0.009 seconds of real time
       0.008000 seconds of total run time (0.008000 user, 0.000000 system)
       88.89% CPU
       22,789,708 processor cycles
       0 bytes consed
       
     Evaluation took:
       0.009 seconds of real time
       0.008001 seconds of total run time (0.008001 user, 0.000000 system)
       88.89% CPU
       22,418,566 processor cycles
       0 bytes consed
       
     Evaluation took:
       0.009 seconds of real time
       0.012001 seconds of total run time (0.012001 user, 0.000000 system)
       133.33% CPU
       22,498,018 processor cycles
       0 bytes consed
EDIT: of course, now, you can say that the programs is unsafe under certain conditions; you may have an issue with the "defconstant" declaration (replacing it with a variable leads to 0.012s results).

But after all, there is no specification. In practice, one must deal with actual requirements and sensible measurments.

ICWiener··on C++ – From goto to std::transform
Does speed really matter? I still prefer the readable Lisp version (SBCL, no optimization):

  (defun square-vect (vect)
    (map 'vector (lambda (x) (* x x)) vect))

  (setq *vector*
        (map 'vector (lambda (x) (random 32767))
             (make-array 10000 :element-type 'integer)))

  (time
   (dotimes (i 1000)
     (square-vect *vector*)))
Results :

  0.084 seconds of real time
  0.084005 seconds of total run time (0.084005 user, 0.000000 system)
  100.00% CPU
  218,686,836 processor cycles
  79,876,912 bytes consed
On my system, the C++ version perform like this:

   ./main 1
   1: 270756377 - elapsed time: 0.022324s
   ~$ ./main 2
   2: 270756377 - elapsed time: 0.018883s
   ~$ ./main 3
   3: 270756377 - elapsed time: 0.016929s
   ~$ ./main 4
   4: 270756377 - elapsed time: 0.014268s
   ~$ ./main 5
   5: 270756377 - elapsed time: 0.016093s
   ~$ ./main 6
   6: 270756377 - elapsed time: 0.018651s
   ~$ ./main 7 # :-)
   7: -1455582645 - elapsed time: 5.3e-05s
ICWiener··on Soundlab: Lazy signal synthesis in Common Lisp
Also, have a look at http://faust.grame.fr/
ICWiener··on Poll: How do you protect your privacy?
This is nice, but there is however a little display problem with long labels.
ICWiener··on Draft. Version control for writing
I spend a lot of time writing technical documentation, in Org-Mode, LaTeX, and so on. In order to collaborate efficiently with others, we need to have a strict line wrap policy (one line = one sentence), so that diffs are easier to understand (e.g. under emacs, auto-fill breaks paragraphs formatting). Since Draft is designed for writing, how does it handle diffs?
← PreviousPage 2 of 2