Since most posts here are basically about what people don't like (me included) 
and questions about things that don't work, I would like to ask, what you like 
the most about Nim. What is the reasons you believe in Nim. Or simply what part 
of the language or the standard library do you think is the most useful to you, 
and why.

So I would like to start:

Combination of typed and untyped macros, because the untyped macros give a lot 
of freedom to design a DSL (domain specific language). The untyped macro can 
transform the dsl into an expression that is compatible with the type checker, 
so that a typed macro can process it.

My example is Tensor Contractions in Einstein notation. For those who don't 
know what a Tensor is, it is Basically a Matrix, just that it can have 
arbitrary amount of dimensions, meaning it can be 2×3 or 2×3×4 or even 
1000×1000×1000×1000. A Tensor contraction is like a Matrix product, best if you 
just look it up on Wikipedia, instead of me explaining it.

So when I want nim to understand Tensor contractions, I could write a macro 
that can transform tensor contraction language into loops that then execute the 
contraction. To do so there are some macro expansion stages.

Stage 1, the the DSL written by the user.
    
    
    untypedMacroCall:
      C[i,l] = A[i,j,k] * B[j,k,l]
    

the identifiers `i j k l` were never introduced, this is no problem when 
untypedMacroCall is an untyped macro.

Stage 2: the intermediate typed macro

the first macro creates this expression:
    
    
    typedMacroCall:
      var C = contract(A,B, [(1,0), (2,1)])
    

this expression is now valid nim code, where the type checker in infer the 
types of A and B. A small side node here is, that contract does not really do 
anything, it is just an empty procedure to make the type checker happy, and 
pass the expressions to `typedMacroCall`. This inner macro call can then 
generate for example loops to do the tensor contraction
    
    
    var C : Tensor2[A.high(0), B.high(2)]
    for i in 0 .. A.high(0):
      for l in 0 .. B.high(2):
        for j in 0 .. A.high(1):
          for k in 0 .. A.high(2):
            C[i,j] = A[i,j,k] * B[j,k,l]
    

The macro could now do some optimizations, for example the loop order. Or when 
the input is a bit more complex, the macro can detect multiple occurences of 
the same expression.

Overall I think the macros are a very great language feature, that I would 
never want to miss anymore in any new programming language that I am going to 
learn. I don't know any other programming language, where this would be 
possible.

So what are youre experiences? 

Reply via email to