Nemerle
Nemerle is a general-purpose, high-level, statically typed programming language designed for platforms using the Common Language Infrastructure (.NET/Mono). It supports multiple paradigms, including functional, object-oriented, aspect-oriented, reflective, and imperative programming. The language features a simple C#-like syntax and a powerful metaprogramming system. In June 2012, the core Nemerle developers were hired by the Czech software company JetBrains. The team focused on developing Nitra, a framework for implementing existing and new programming languages. Both Nemerle and Nitra appear to have since been abandoned or discontinued by JetBrains; Nitra has not received updates from its... Source: e01 Encyclopedia. Licence: CC BY-SA 4.0.
Nemerle is a general-purpose, high-level, statically typed programming language designed for platforms using the Common Language Infrastructure (.NET/Mono). It supports multiple paradigms, including functional, object-oriented, aspect-oriented, reflective, and imperative programming. The language features a simple C#-like syntax and a powerful metaprogramming system. In June 2012, the core Nemerle developers were hired by the Czech software company JetBrains. The team focused on developing Nitra, a framework for implementing existing and new programming languages. Both Nemerle and Nitra appear to have since been abandoned or discontinued by JetBrains; Nitra has not received updates from its original creators since 2017, and Nemerle is now maintained independently by the Russian Software Development Network. However, no major releases have occurred, and development progress remains slow. JetBrains has not referenced Nemerle or Nitra for several years. The language is named after the Archmage Nemmerle, a character in the fantasy novel A Wizard of Earthsea by Ursula K. Le Guin. ==Features== Nemerle's most notable feature is the ability to mix styles of programming that are object-oriented and functional. Programs may be structured using object-oriented concepts such as classes and namespaces, while methods can (optionally) be written in a functional style. Other notable features include: strong type inference a flexible metaprogramming subsystem (using macros) full support for object-oriented programming (OOP), in the style of C#, Java, and C++ full support for functional programming, in the style of ML, OCaml, and Haskell, with these features: higher-order functions pattern matching algebraic types local functions tuples and anonymous types partial application of functions The metaprogramming system allows for great compiler extensibility, embedding domain-specific languages, partial evaluation, and aspect-oriented programming, taking a high-level approach to lift as much of the burden as possible from programmers. The language combines all Common Language Infrastructure (CLI) standard features, including parametric polymorphism, lambdas, extension methods etc. Accessing the libraries included in the .NET or Mono platforms is as easy as in C#. ===Type inference=== def x = 1; // int def myList = List(); // generic List[T], type T is deduced from the usage in the next line myList.Add(x); // compiler deduces type of T as int making myList type of List[int] ===Everything is an expression=== def x = { // similar to x = 3 def y = 1; def z = 2; y + z // this last statement is a block return value }; def x = if (DateTime.Now.DayOfWeek == DayOfWeek.Monday) // if, using, try are also expressions "Monday" else "other day"; def x = try int.Parse(someString) catch { | FormatException() => 0 }; def x = returnBlock : { foreach (i in [1, 2, 3]) when (i > 2) returnBlock(true); // exit block (x = true) false // x = false }; ===Tuples=== def k = (1, "one"); // k : (int * string) def (a, b) = k; // a = 1, b = "one" ===Pattern matching=== def result = match (number) { | 0 => "zero" | 1 => "one" | x when x "negative" | _ => "more than one" } {{hidden|Other pattern matching examples|style = border:1px dashed gray;| Type matching with variable binding: def check (o : object) { match (o) { | i is int => $"An int: $i" | s is string => $"A string: $(s.ToUpper())" | _ => "Object of another type" } } Tuple pattern matching: match (tuple) { | ( 42, _ ) => "42 on first position" | ( _, 42 ) => "42 on second position" | ( x, y ) => $"( $x, $y )" } Regexp matching: using Nemerle.Text; regexp match (str) { | "a+.*" => printf("a\n"); | @"(? \d+)-\w+" => printf("%d\n", num + 3); | "(? (Ala|Kasia))? ma kota" => match (name) { | Some (n) => printf("%s\n", n) | None => printf("noname?\n") } | _ => printf("default\n"); } }} ===Functional types and local functions=== using System.Console; // classes and modules (static classes) can be put in namespaces def next(x) { x + 1 }; // the type of x argument and other function arguments can be deduced from usage def mult(x, y) { x * y }; def fibonacci(i) { | 0 => 0 | 1 => 1 | other => fibonacci(i - 1) + fibonacci(i - 2) }; WriteLine(next(9)); // 10 similar to "Console.WriteLine(next(9));" WriteLine(mult(2, 2)); // 4 WriteLine(fibonacci(10)); // 55 variant RgbColor { | Red | Yellow | Green | Different { red : float; green : float; blue : float; } } ===Metaprogramming=== Nemerle's macro system allows for creating, analysing, and modifying program code during compiling. Macros can be used in the form of a method call or as a new language construct. Many constructs within the language are implemented using macros (if, for, foreach, while, using etc.). "if" macro example: macro @if (cond, e1, e2) syntax ("if", "(", cond, ")", e1, Optional (";"), "else", e2) { /* defines an area of quasi-quotation, the Nemerle compiler transforms the code in it to an AST, such transformations are somewhat similar to an Expression compiling in C# */ $e1 | _ => $e2 } ]> } // using this macro in code: def max = if (a > b) a else b; // during a compile time the upper line will be transformed to this: def max = match (a > b) { | true => a | _ => b } using System.Console; [Qux] class FooBar { public static Main(): void { WriteLine("Hello") } static Foo (x: int): void { if (x == 3) { def y = x * 42; Foo (x) } else { [x].Map (fun (x) { x * 2 }) } } static Bar(): int { def foo = 2 + 7 * 13; foo } } could be rewritten as: using System.Console; [Qux] \ class FooBar public static Main(): void WriteLine("Hello") static Foo (x: int): void if (x == 3) def y = x * 42; Foo (x) else [x].Map (fun (x) { x * 2 }) static Bar(): int def foo = 2 + 7 * 13 foo Notably, it is not possible to break expressions or alternative clauses in matches over multiple lines without using a backslash \ : // This will not compile ... static Bar(): int def foo = 2 + 7 * 13 foo match (s) | "a" | "aa" => 1 | "b" | "bb" => 2 | _ => 0 // But this will: static Bar(): int def foo = 2 \ + 7 \ * 13 foo match (s) | "a" \ | "aa" => 1 | "b" \ | "bb" => 2 | _ => 0 In order to activate this syntax, the user must add #pragma indent to the top of the file or use the compiler option -i . ==Examples== ===Hello, World!=== The traditional Hello World! can be implemented in a more C#-like fashion: class Hello { static Main () : void { System.Console.WriteLine ("Hello, world!"); } } or more simply: System.Console.WriteLine("Hello, world!"); ===Examples of macros=== Macros allow generating boilerplate code with added static checks performed by the compiler. They reduce the amount of code that must be written by hand, make code generation safer, and allow programs to generate code with compiler checks, while keeping source code relatively small and readable. def s = $"The number is $i"; //insert the value of the variable i where $i is placed def s = $"$x + $y = $(x+y)"; // $(...) can be used to make calculations or access members ====Declarative code generation==== StructuralEquality, Memoize, json, and with are macros which generate code in compile time. Though some of them (StructuralEquality, Memoize) can look like C# attributes, during compiling, they will be examined by the compiler and transformed to appropriate code using logic predefined by their macros. [StructuralEquality] // Implement IEquatable[Sample] .Net interface using by element comparison equality. class Sample { [Memoize] // remember first evaluation result public static SomeLongEvaluations() : int { MathLib.CalculateNthPrime(10000000) } [DependencyProperty] // WPF dependency property public DependencyPropertySample { get; set; } public static Main() : void { /* syntax macro "json" generates code: JObject.Object([("a", JValue.Number(SomeLongEvaluations())), ("b", JValue.Number(SomeLongEvaluations() + 1))]) / def jObject = json { a: SomeLongEvaluations(); b: (SomeLongEvaluations() + 1)} // object initialization macro " WriteLine("Calc done"); // events and delegates } ReadLine(); } } ====Database accessibility==== Using Nemerle macros for SQL you can write: ExecuteReaderLoop("SELECT firstname, lastname FROM employee WHERE firstname = $myparm", dbcon, { WriteLine ($"Name: $firstname $lastname") }); instead of string sql = "SELECT firstname, lastname FROM employee WHERE firstname = :a"; using (NpgsqlCommand dbcmd = new NpgsqlCommand (sql, dbcon, dbtran)) { dbcmd.Parameters.Add("a", myparm); using (NpgsqlReader reader = dbcmd.ExecuteReader()) { while(reader.Read()) { var firstname = reader.GetString (0); var lastname = reader.GetString (1); Console.WriteLine ("Name: {0} {1}", firstname, lastname) } } } and this is not just hiding some operations in a library, but additional work performed by the compiler to understand the query string, the variables used there, and the columns returned from the database. The ExecuteReaderLoop macro will generate code roughly equivalent to what you would have to type manually. Moreover, it connects to the database at compilation time to check that your SQL query really makes sense. ====New language constructs==== With Nemerle macros you can also introduce some new syntax into the language: macro ReverseFor (i, begin, body) syntax("ford", "(", i, ";", begin, ")", body) { = 0; $i--) $body ]> } defines a macro introducing the syntax and can be used like ford (i ; n) print (i); ===Nemerle with ASP.NET=== Nemerle can be either embedded directly into ASP.NET: Page_Load(_ : object, _ : EventArgs) : void { Message.Text = $"You last accessed this page at: $(DateTime.Now)"; } EnterBtn_Click(_ : object, _ : EventArgs) : void { Message.Text = $"Hi $(Name.Text), welcome to ASP.NET!"; } Please enter your name: ...Or stored in a separate file and entered with a single line: ===PInvoke=== Nemerle can take advantage of native platform libraries. The syntax is very similar to C#'s and other .NET languages. Here is the simplest example: using System; using System.Runtime.InteropServices; class PlatformInvokeTest { [DllImport("msvcrt.dll")] public extern static puts(c : string) : int; [DllImport("msvcrt.dll")] internal extern static _flushall() : int; public static Main() : void { _ = puts("Test"); _ = _flushall(); } } ==References== ==Further reading== Publications about Nemerle in RSDN Magazine, Russian official science magazine Presentation "Nemerle is notable" by Denis Rystsov Article "Unconventional languages for unconventional supercomputers" by Andrey Adinetz ==External links== GitHub project and repository (new development) Google Code project and repository (old development) Nemerle Forum Nemerle presentation on Microsoft Research SSCLI RFP II Capstone 2005 workshop Nemerle at 99 Bottles of Beer
Cite this
"Nemerle". 1784348233. e01 Encyclopedia. CC BY-SA 4.0. https://en.wikipedia.org/w/index.php?oldid=1364738168 JSON record · API collection · BibTeX · RIS · CSL-JSON · citation policy