News for hackers. Stuff that matters.
Re:Porting a NES Emulator from Go to Nim by warmwaffles on Friday May 01, 2015 @03:23PM (#9473907)
Well this is new. I've never seen Nim before. What does it offer that Rust, Haskell, Erlang, etc... do not?
Re:Porting a NES Emulator from Go to Nim by def- on Friday May 01, 2015 @03:26PM (#9473931)
Two of my old posts may answer that question:

- http://hookrace.net/blog/what-is-special-about-nim/

- http://hookrace.net/blog/what-makes-nim-practical/

Summary by benhoyt: https://news.ycombinator.com/item?id=8822918

  * Run regular code at compile time
  * Extend the language (AST templates and macros);
    this can be used to add a form of list comprehensions to the language!
  * Add your own optimizations to the compiler!
  * Bind (easily) to your favorite C functions and libraries
  * Control when and for how long the garbage collector runs
  * Type safe sets and arrays of enums; this was cool:
    "Internally the set works as an efficient bitvector."
  * Unified Call Syntax, so mystr.len() is equivalent to len(mystr)
  * Good performance -- not placing too much emphasis on this,
    but it's faster than C++ in his benchmark
  * Compile to JavaScript
Re:Porting a NES Emulator from Go to Nim by jedi_stannis on Friday May 01, 2015 @03:58PM (#9474105)
Thanks, that first post is really interesting. Two things stand out to me:

* Run regular code at run time - does this just error if you try to use const on something that depends on a runtime value?

* Add your own optimizations to the compiler - isn't this very dangerous, especially if optimizations from various sources of code get combined? Even if your optimizations are valid (which there seems to be no guarantee of), computer math is notorious for being different from real math. People unaware of the nuances of integers rolling over or floating point seem like they could easily shoot themselves in the foot here.

Re:Porting a NES Emulator from Go to Nim by dom96 on Friday May 01, 2015 @04:06PM (#9474155)
> * Run regular code at run time - does this just error if you try to use const on something that depends on a runtime value?

Yep. For example `const foo = stdin.readLine()` would result in "Error: cannot evaluate at compile time: stdin".

As for your second point. I haven't used this feature personally but I have heard that the compiler will tell you exactly where these optimisations are applied. You can also disable these optimisations very easily during compilation.

Re:Porting a NES Emulator from Go to Nim by TJSomething on Friday May 01, 2015 @08:19PM (#9475327)
For reference, compared with Rust:

    * Yes, with compiler plugins
    * Yes, with macros
    * Maybe. I think that you could use a compiler plugin
      to do arbitrary transformations on all the code in a
      module, but I haven't seen an implementation.
    * Yes
    * What garbage collector?
    * Yep
    * Kind of. Any method can be invoked as a function with
      the <type>::func(val, args...) syntax, but the 
      opposite's not true.
    * Check
    * Yep, via Emscripten
Re:Porting a NES Emulator from Go to Nim by adrusi on Friday May 01, 2015 @09:45PM (#9475543)
Rust isn't working with Emscripten yet.
Re:Porting a NES Emulator from Go to Nim by TJSomething on Saturday May 02, 2015 @05:48AM (#9476488)
Re:Porting a NES Emulator from Go to Nim by Peaker on Friday May 01, 2015 @05:45PM (#9474726)
That's a great list, but the question was about stuff that's not in Haskell (among others) so a few nitpicks:

* Haskell can run regular code at compile-time (But it is slow, using ghci/TH)

* Haskell can add optimizations (rewrite rules and plugins). I think Haskell innovated rewrite rules and Nim probably was inspired by that?

* Binding to C easily is also a Haskell feature

* Type safe sets and arrays of enums: Is this not just a library? Haskell sets and arrays of enums are type-safe by default?

* Unified call syntax is true of Haskell too

* Compile to Javascript -- Haskell can do that too

Re:Porting a NES Emulator from Go to Nim by def- on Friday May 01, 2015 @05:54PM (#9474770)
I'm a big Haskell fan (it was my favorite language before discovering Nim), but here's my personal list of Nim advantages over Haskell:

    - Predictable performance (lazy evaluation is problematic)
    - Easy to achieve high performance
    - Easier to read, even for non-specialists
    - I'm much more productive in it
    - Multi-paradigm: Nim is mostly imperative, but OO and functional can be mixed as well.
http://hookrace.net/blog/conclusion-on-nim/
Re:Porting a NES Emulator from Go to Nim by progman on Saturday May 02, 2015 @03:22AM (#9476215)
> I'm much more productive in it

That's the point! I know many languages and Nim is by far the most productive one. It's like coding in Python with the assurance that the compiler will catch many errors that would cause runtime errors in Python. That assurance makes coding in Nim faster than in Python because I don't have to think so much about avoiding runtime errors.

Re:Porting a NES Emulator from Go to Nim by notsony on Friday May 01, 2015 @07:13PM (#9475098)
Thanks.

Does Nim compile to C99 ?

If someone were to ask what's wrong with Nim, what would you say are the things that need to be fixed?

Re:Porting a NES Emulator from Go to Nim by def- on Friday May 01, 2015 @07:40PM (#9475204)
C89, so it's also compatible to Microsoft's vcc.

There are quite some compiler bugs remaining in Nim and the standard library could be improved.

Re:Porting a NES Emulator from Go to Nim by jug on Friday May 01, 2015 @04:00PM (#9474119)
I find the easiest way to think of Nim is "A Python-style language that compiles to C". Of course, the truth isn't as simple as that. But I like it, especially how pragmatic the language is. It doesn't try to "prove" a philosophy like Rust, but I think it gains other advantages in return.
Re:Porting a NES Emulator from Go to Nim by madez on Friday May 01, 2015 @03:25PM (#9473922)
I'd say that it feels more like a scripting language in comparison to Rust and is more approachable than Haskell or Erlang while being fast since it's compiled to C.
Re:Porting a NES Emulator from Go to Nim by mreiland on Friday May 01, 2015 @03:58PM (#9474106)
being compiled to C doesn't imply fast :)
Re:Porting a NES Emulator from Go to Nim by def- on Friday May 01, 2015 @05:18PM (#9474592)
It does have other advantages though, like inheriting C's excellent portability.
Re:Porting a NES Emulator from Go to Nim by coldtea on Friday May 01, 2015 @06:51PM (#9475005)
Unless done badly, it does.

Transpiling to C gives you the opportunity to emit code that is straightforward clean and fast C for most operations, which avoid an abstraction overhead.

Re:Porting a NES Emulator from Go to Nim by jerf on Friday May 01, 2015 @07:11PM (#9475087)
No, it doesn't. You could easily compile Python into C (basically, "just" inline the core interpreter loop), but it will still be slow, because the Python semantics still require a lot of dictionary resolutions on every operation. Getting efficient C still requires you to think about getting efficient code. You can not get it "for free" just by waving a C compiler over your language. Otherwise, everyone would just do that and there would be no languages slower than C.

Before you reply to disagree, make sure to very, very carefully think through the implications of the last sentence there. It is not as if compiling to C is particularly hard; it is literally "homework assignment for a normal college senior in a compilers course"-difficult. Many languages have gone through a phase where they compile to C. Usually they deliberately leave C at some point, another thing to carefully ponder the implications of before replying in disagreement.

Re:Porting a NES Emulator from Go to Nim by coldtea on Friday May 01, 2015 @07:25PM (#9475155)
>No, it doesn't. You could easily compile Python into C (basically, "just" inline the core interpreter loop), but it will still be slow, because the Python semantics still require a lot of dictionary resolutions on every operation.

Yeah, and you could do this too at the start of your emitted code from the transpiler:

sleep(UINT_MAX)

which would also make it slower than an interpreted language. But I specified I wasn't talking about that, or about "just inlining the core interpreter loop", but about transpiling letting you also emit straightforward and clean code without extra abstractions for most operations.

A new language like Nim doesn't come with all the baggage of Python's runtime pre-existing, and doesn't need to recreate it.

Re:Porting a NES Emulator from Go to Nim by mreiland on Friday May 01, 2015 @11:16PM (#9475728)
HipHop doesn't support the entirety of the PHP runtime for exactly the reasons that have been laid out for you.

That's reality from a company with a shitton of money and a vested interest in doing it successfully.

They can't do it in the general case because reality disagrees with you.

Re:Porting a NES Emulator from Go to Nim by coldtea on Saturday May 02, 2015 @07:49AM (#9476726)
Not sure what are you getting at. What I didn't say: transpiling is magic fairy dust for speed, and you don't have to care about language semantics at all.

What I said: "unless done badly" (my words on the original comment), it's fast, as it gives you a chance to emit straightfoward C code for most operations. Now, regarding your example:

First, HipHop is not transpiled to C. It's a runtime with a JIT.

Second, what does "HipHop not supporting the entirety of the PHP runtime" has to do with anything?

I take it you mean that the fact they had to skip some parts of the runtime for increased speed, proves something related to what I wrote. But (besides the JIT thing) PHP is not greenfield like Nim. They have a runtime, and it has to work in a certain way. PHP wasn't designed with transpiling in mind, and HipHop had to follow it closely as a design goal. I never said runtime decisions you have to mimic to be 100% compatible can't slow you down.

Re:Porting a NES Emulator from Go to Nim by mreiland on Saturday May 02, 2015 @08:56AM (#9476877)
http://en.wikipedia.org/wiki/HipHop_for_PHP

> HipHop for PHP (HPHPc) is a PHP transpiler created by Facebook. By using HPHPc as a source-to-source compiler, PHP code is translated into C++, compiled into a binary and run as an executable, as opposed to the PHP's usual execution path of PHP code being transformed into opcodes and interpreted.

Re:Porting a NES Emulator from Go to Nim by coldtea on Saturday May 02, 2015 @10:33AM (#9477198)
OK, I had in mind the JITed HipHop VM that they tout now (later development it seems).

Still, how does the original version of HipHop as a transpiling compiler invalidate what I said?

I said (check my original comment): unless you do it badly, transpiling to C gets you to run faster, because it lets you translate most operations to straightforward and fast C code.

And that's exactly the logic they followed and what they achieved: they transpiled PHP to C, as opposed from running it with PHPs runtime interpreter, in order to make it faster, and it worked.

That they skipped some parts of PHP runtime behavior/semantics, which if they didn't it would slow things down, doesn't clash with what I said. In fact it's already covered in my comment: "it let's you translate MOST" (not all) operations to fast C code.

Re:Porting a NES Emulator from Go to Nim by mreiland on Saturday May 02, 2015 @06:28PM (#9478812)
> transpiling to C gets you to run faster

You're changing the context.

> while being fast since it's compiled to C. - them

> being compiled to C doesn't imply fast :) - me

> Unless done badly, it does. - you

You've switched to "faster" because it's a more defensible position, but that isn't the idea that was originally being responded to. That's why jerf said the following, emphasis mine:

> No, it doesn't. You could easily compile Python into C (basically, "just" inline the core interpreter loop), __but it will still be slow__

Re:Porting a NES Emulator from Go to Nim by jerf on Sunday May 03, 2015 @09:48PM (#9483915)
You're really providing further evidence for my pet theory that anybody who seriously uses the word "transpiler" has no clue how compilers work, or what they can and can not do. (This is on the theory that anybody who does have that understanding also understands that "transpiler" is a etymological solution to a problem that didn't exist... the word "compiler" already covered the bases completely and totally... if you actually understand them.)
Re:Porting a NES Emulator from Go to Nim by Dewie3 on Friday May 01, 2015 @04:00PM (#9474117)
> while being fast since it's compiled to C.

Which is fast compared to what? Being compiled to JS?

It's not like being compiled to a "fast language" automatically makes that compiled program fast. You also have to consider how complex the runtime is, and other variables which are probably a direct result of the semantics of the language.

Re:Porting a NES Emulator from Go to Nim by guelo on Friday May 01, 2015 @04:29PM (#9474307)
It's closer to Go than Rust since it is garbage collected. It's statically typed, has OO features and first-class functions. It compiles to C making it pretty portable.
Re:Porting a NES Emulator from Go to Nim by rdtsc on Friday May 01, 2015 @04:42PM (#9474380)
Has generics, meta-programming and GC can be disabled (or tuned to run up to some max # of milliseconds).

So it is Go++ if you wish.

But I am not sure about its concurrency. Go does have channels which are nice abstractions for doing concurrency right.

Re:Porting a NES Emulator from Go to Nim by dom96 on Friday May 01, 2015 @05:04PM (#9474513)
Nim has channels too (although I think they need replacing). There is also spawn which is similar to Goroutines in some ways: http://nim-lang.org/0.11.0/manual.html#parallel-spawn-spawn-...
Re:Porting a NES Emulator from Go to Nim by andolanra on Friday May 01, 2015 @04:39PM (#9474367)
You can have manual control over memory if you want, though. This worked the last time I looked at the language, at least:

    foo = cast[ptr Foo](alloc(sizeof(Foo)))
    # do stuff with foo
    dealloc(foo)
EDIT: Here's the documentation on the site itself. Most things are 'traced' by the GC, but by using alloc and dealloc you can work with 'untraced' (i.e. manually allocated) memory: http://nim-lang.org/0.11.0/manual.html#types-reference-and-p...
Re:Porting a NES Emulator from Go to Nim by pcwalton on Friday May 01, 2015 @06:39PM (#9474966)
Go has unsafe.Pointer too.