Live data from Hacker News

Crunch – a Scheme compiler with a minimal runtime

more-magic.net

31–40 of 73 posts

Re: Crunch – a Scheme compiler with a minimal runtime

#31
post #29

This just made my day. I'm the author of Scheme for Max, an extension to the Max music programming environment that puts the s7 Scheme interpreter in it for scripting note-event level code (not dsp). It would be fantastic to also be able to use a subset of Scheme for generating DSP level code, where speed would be more important than dynamic typing or continuations/tco/etc. I will be watching this closely!

what do you think about pre-scheme?

I haven't tried it, but I'm curious and interested!

Re: Crunch – a Scheme compiler with a minimal runtime

#32
post #30
post #8

For Common Lispers such as myself, who are vaguely aware of developments in the Scheme space: the most important difference between CRUNCH and Chicken appears to be that, while both compile down to C/object code, CRUNCH is additionally targeting a statically-typed subset of Scheme. Opinion: this is great. The aversion of Lispers to static types is historical rather than intrinsic and reflects the relative difference…

What are the main differences between OcamML and a statically typed Lisp?

Type inference is probably the biggest thing. You would need explicit "phases" to expand macros, disallow macro expansion at runtime, and implement bi-directional type inference HM-style to get even close to what OCaml has.

To be honest, I'd kill for a Lisp that had the same type system as OCaml, but I suspect the closes we'll get is basically Rust (whose macro system is quite good).

Re: Crunch – a Scheme compiler with a minimal runtime

#33
post #20

The very first rudimentary standalone example: (define (main) (display "Hello world\n")) Doesn't compile, but instead gives error: Error: main: global variable `scheme#display' has unknown type

The popcount example does compile, if you drop the "assume" form,

    (import (chicken bitwise) (chicken fixnum))
    (define (popcount32 x) ; From "Hacker's Delight"                                                                                                                                       
    ;  (assume                                                                                                                                                                             
       (let ((x (fx- x (bitwise-and (arithmetic-shift x -1) #x55555555))))
         (let ((x (fx+ (bitwise-and x #x33333333)
                       (bitwise-and (arithmetic-shift x -2) #x33333333))))
           (let ((x (bitwise-and (fx+ x (arithmetic-shift x -4)) #x0F0F0F0F)))
             (let ((x (fx+ x (arithmetic-shift x -8))))
               (let ((x (fx+ x (arithmetic-shift x -16))))
                 (bitwise-and x #x0000003F)))))))
Turns into somewhat redundant C, looks much like transliterated bytecode, e.g.

    long v17 = 0;
    // ...
    // popcnt.scm:7                                                                                                                                                                            
    v17 = 0;
    // ...
    // popcnt.scm:7                                                                                                                                                                            
    v17 = (t37 & 252645135);
    // popcount32                                                                                                                                                                              
    // popcnt.scm:8                                                                                                                                                                            
    t40 = crunch_arithmetic_shift(v17, -8);
Interesting find in the (small) crunch.h is

    #ifdef CRUNCH_FREESTANDING
    #include 
    #include 
    #include 
    #include "crunch-environment.h"
    #else
    // loads of stuff
though I haven't found crunch-environment.h in the source yet

Re: Crunch – a Scheme compiler with a minimal runtime

#34
> Other targets are possible, like GPUs. I don't know anything about that, so if you are interested and think you can contribute, please don't hesitate to contact me.

The freestanding macro suggests most of the heavy lifting is done. Stuff the GPU targets will struggle with in no particular order:

- setjmp / longjmp

- signals (if used?)

- threads

- fast alloc/free

- stack will be smaller than chicken expects

I don't know how to do signals. The rest are merely difficult.

Re: Crunch – a Scheme compiler with a minimal runtime

#35
post #22

Earlier quoted context omitted.

From the article: > What is needed is a small, portable compiler that generates more or less "natural" C code with minimal dependencies and runtime system that supports at least the basic constructs of the language and that puts an emphasis on producing efficient code, even if some of the more powerful features of Scheme are not available. Since C doesn't support continuations, it is not possible to have continuation…

> Since C doesn't support continuations, it is not possible to have continuations and at the same time generate "natural" C code. Since CHICKEN actually does this, I'll add the proviso of "without a GC or costly runtime".

I would call the code CHICKEN generates for "direct style" not "natural". (I know - it is a cop-out to use "natural" in quotes)

Regardless of what we call the style, it isn't simple any more.

For people interested in the internals of Chicken Scheme (I suspect sctb already knows):

https://www.more-magic.net/posts/internals-gc.html

Re: Crunch – a Scheme compiler with a minimal runtime

#36
post #30

Earlier quoted context omitted.

What are the main differences between OcamML and a statically typed Lisp?

Type inference is probably the biggest thing. You would need explicit "phases" to expand macros, disallow macro expansion at runtime, and implement bi-directional type inference HM-style to get even close to what OCaml has. To be honest, I'd kill for a Lisp that had the same type system as OCaml, but I suspect the closes we'll get is basically Rust (whose macro system is quite good).

Racket has Typed Racket, which while not Hindley-Miller can do some type inference.

There's also the plait language which says its type system is similar to ML: https://docs.racket-lang.org/plait/index.html

And Hackett, inspired by Haskell: https://lexi-lambda.github.io/hackett/

And Common Lisp has coalton: https://coalton-lang.github.io/

Re: Crunch – a Scheme compiler with a minimal runtime

#37
post #20

The very first rudimentary standalone example: (define (main) (display "Hello world\n")) Doesn't compile, but instead gives error: Error: main: global variable `scheme#display' has unknown type

The popcount example does compile, if you drop the "assume" form, (import (chicken bitwise) (chicken fixnum)) (define (popcount32 x) ; From "Hacker's Delight" ; (assume (let ((x (fx- x (bitwise-and (arithmetic-shift x -1) #x55555555)))) (let ((x (fx+ (bitwise-and x #x33333333) (bitwise-and (arithmetic-shift x -2) #x33333333)))) (let ((x (bitwise-and (fx+ x (arithmetic-shift x -4)) #x0F0F0F0F))) (let ((x (fx+ x (arith…

The nice thing is that you can emit pretty sloppy C and the C compiler will optimize it pretty well.

Re: Crunch – a Scheme compiler with a minimal runtime

#38
post #30

Earlier quoted context omitted.

What are the main differences between OcamML and a statically typed Lisp?

Type inference is probably the biggest thing. You would need explicit "phases" to expand macros, disallow macro expansion at runtime, and implement bi-directional type inference HM-style to get even close to what OCaml has. To be honest, I'd kill for a Lisp that had the same type system as OCaml, but I suspect the closes we'll get is basically Rust (whose macro system is quite good).

It's also pretty straightforward to use multiple coding styles in the lisps in question, regardless of the typing being static or not.

Re: Crunch – a Scheme compiler with a minimal runtime

#39
post #30
post #8

For Common Lispers such as myself, who are vaguely aware of developments in the Scheme space: the most important difference between CRUNCH and Chicken appears to be that, while both compile down to C/object code, CRUNCH is additionally targeting a statically-typed subset of Scheme. Opinion: this is great. The aversion of Lispers to static types is historical rather than intrinsic and reflects the relative difference…

What are the main differences between OcamML and a statically typed Lisp?

The module and functor system. Also macros are much more a Lisp family thing.

Re: Crunch – a Scheme compiler with a minimal runtime

#40
post #36

Earlier quoted context omitted.

Type inference is probably the biggest thing. You would need explicit "phases" to expand macros, disallow macro expansion at runtime, and implement bi-directional type inference HM-style to get even close to what OCaml has. To be honest, I'd kill for a Lisp that had the same type system as OCaml, but I suspect the closes we'll get is basically Rust (whose macro system is quite good).

Racket has Typed Racket, which while not Hindley-Miller can do some type inference. There's also the plait language which says its type system is similar to ML: https://docs.racket-lang.org/plait/index.html And Hackett, inspired by Haskell: https://lexi-lambda.github.io/hackett/ And Common Lisp has coalton: https://coalton-lang.github.io/

Most of those aren't really ready for production use except maybe Typed Racket, which I consider to be too "weak" and took a route with annotations that I'm not a fan of. Coalton is very interesting, I've been following it for a bit. Carp [0] is another one that I've been following.

[0]: https://github.com/carp-lang/Carp

Post reply on HN