Thread overview | |||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
November 27, 2006 Thesis on metaprogramming in D | ||||
---|---|---|---|---|
| ||||
Hello. My name is Leandro Lucarella, I'm finishing my grade on Computer Engineering in the UBA (University of Buenos Aires, Argentina) and I'm planning to do my thesis on meta-programming and I'd like to do it using D (because is the language that is closer to my ideal language, if it just had Python-like syntax... ;). The center of my thesis would be adding meta-classes[1] to D, and maybe some other meta-programming features D could lack of (maybe some kind of access to the AST like XLR[2]). I'd like to know if you think this is: 1) Doable. 2) Useful. 3) A feature that can be included in the official D specification. In case it's not clear, I'd like to add support for meta-classes to the D language itself, not provide some nasty library to ease the job using the existing tools. If there is any D guru that is willing to help me out and give me some guidance, that would be great too. TIA [1] Like Python meta-classes but, of course, resolved at compile time. http://www.python.org/doc/essays/metaclasses/ [2] Extensible Language and Runtime http://xlr.sourceforge.net/ -- Leandro Lucarella Integratech S.A. 4571-5252 |
November 28, 2006 Re: Thesis on metaprogramming in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Leandro Lucarella | On Mon, 27 Nov 2006 15:23:33 -0300, Leandro Lucarella <llucarella@integratech.com.ar> wrote: >The center of my thesis would be adding meta-classes[1] to D, and maybe some other meta-programming features D could lack of (maybe some kind of access to the AST like XLR[2]). I can't really offer any useful support, but if I understand what you are saying, I can offer encouragement. My view of the ideal handling of metaprogramming is that it should have access to all the features that are available at runtime. The only languages that I know that genuinely achieve that are the Lisp-alikes such as Scheme. My ideal is being able to define 'quoted' blocks that are parsed and translated to ASTs, with those ASTs being processed by other code to derive new ASTs which are then submitted for the final back-end optimisation and code generation. Which is not anti-templates as such. I like templates. It just means that, in principle, you could define an alternative template mechanism as a library. The nearest you get to this at the moment (outside Lisp-alikes), at least that I know of, are languages like Python where you can access the AST, including the AST of generated code, at run-time. Thanks for the XLR link, by the way - very interesting, and possibly capable of exactly what I described above, though I've not read it properly yet. -- Remove 'wants' and 'nospam' from e-mail. |
November 28, 2006 Re: Thesis on metaprogramming in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steve Horne | Steve Horne wrote: > My view of the ideal handling of metaprogramming is that it should have access to all the features that are available at runtime. The only languages that I know that genuinely achieve that are the Lisp-alikes such as Scheme. > > My ideal is being able to define 'quoted' blocks that are parsed and translated to ASTs, with those ASTs being processed by other code to derive new ASTs which are then submitted for the final back-end optimisation and code generation. Nemerle does exactly that way! -- AKhropov |
November 28, 2006 Re: Thesis on metaprogramming in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrey Khropov | Andrey Khropov wrote:
> Steve Horne wrote:
>
>> My view of the ideal handling of metaprogramming is that it should have access to all the features that are available at runtime. The only languages that I know that genuinely achieve that are the Lisp-alikes such as Scheme.
>>
>> My ideal is being able to define 'quoted' blocks that are parsed and translated to ASTs, with those ASTs being processed by other code to derive new ASTs which are then submitted for the final back-end optimisation and code generation.
>
> Nemerle does exactly that way!
>
Maybe this is a little of topic in a D news group but since nemerle came up, What is the speed of nemerle related to other languages such as C D C++ , Java or python?
|
November 28, 2006 Re: Thesis on metaprogramming in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Johan Granberg | Johan Granberg wrote: > Andrey Khropov wrote: > > Maybe this is a little of topic in a D news group but since nemerle came up, > What is the speed of nemerle related to other languages such as C D C++ , > Java or python? It runs on Microsoft's .NET CLR, so it's probably about the same as C#. --bb |
November 28, 2006 Re: Thesis on metaprogramming in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bill Baxter | Bill Baxter wrote: > It runs on Microsoft's .NET CLR, so it's probably about the same as C#. Yes, my experiments have shown that this is generally the case. No surprise, because it's a statically typed language unlike Scheme/Lisp. -- AKhropov |
November 28, 2006 Re: Thesis on metaprogramming in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrey Khropov | Andrey Khropov wrote:
> Steve Horne wrote:
>
>> My view of the ideal handling of metaprogramming is that it should
>> have access to all the features that are available at runtime. The
>> only languages that I know that genuinely achieve that are the
>> Lisp-alikes such as Scheme.
>>
>> My ideal is being able to define 'quoted' blocks that are parsed and
>> translated to ASTs, with those ASTs being processed by other code to
>> derive new ASTs which are then submitted for the final back-end
>> optimisation and code generation.
>
> Nemerle does exactly that way!
When I looked at the Nemerle website, I didn't see much that couldn't be done easily with D templates. It would be interesting to find something it can do, that D can't.
|
November 28, 2006 Re: Thesis on metaprogramming in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Don Clugston | Don Clugston wrote:
> Andrey Khropov wrote:
>
>> Steve Horne wrote:
>>
>>> My view of the ideal handling of metaprogramming is that it should
>>> have access to all the features that are available at runtime. The
>>> only languages that I know that genuinely achieve that are the
>>> Lisp-alikes such as Scheme.
>>>
>>> My ideal is being able to define 'quoted' blocks that are parsed and
>>> translated to ASTs, with those ASTs being processed by other code to
>>> derive new ASTs which are then submitted for the final back-end
>>> optimisation and code generation.
>>
>>
>> Nemerle does exactly that way!
>
>
> When I looked at the Nemerle website, I didn't see much that couldn't be done easily with D templates. It would be interesting to find something it can do, that D can't.
>
> def sr = System.IO.StreamReader ("SomeFile.txt");
Immutable run-time variables. You can assign anything to them, and then it can't change. AFAIK, D's const doesn't let you do that.
> someFunction(if (cond) expr_1 else expr_2);
No statements--everything is an expression. And yes, it is VERY handy :)
> import some.namespace.SomeClass;
Import contents of static classes as well as modules.
> match (control) {
> | button is Button => ...
> | listv is ListView => ...
> | _ => ... // null case
> }
switch eat your heart out. You can use literal, pattern matching... just about anything.
> def res2 = frobnicate (7, do_qux = true,
> do_baz = false,
> do_bar = true);
Named parameters. D doesn't even have hashtable literals, so faking it would be a complete pain.
> ()
That's the void literal. Basically, makes void act like any other type. Try writing generic code where functions can have void return values, and you'll end up writing everything twice since you can't have void variables.
> variant RgbColor {
> | Red
> | Yellow
> | Green
> | Different {
> red : float;
> green : float;
> blue : float;
> }
> }
Different to a union since it remembers what kind of thing is being stored. Fun trick with trees is to use a variant to store either a branch or a leaf.
> macro while_macro (cond, body)
> syntax ("while", "(", cond, ")", body) {
> <[
> def loop () {
> when ($cond) {
> $body;
> loop ()
> }
> }
> loop ()
> ]>
> }
Do THAT with templates :3
>> def x = 3;
>> System.Console.WriteLine ($"My value of x is $x and I'm happy");
>
> expands to
>
>> def x = 3;
>> System.Console.WriteLine ({
>> def sb = System.Text.StringBuilder ("My value of x is ");
>> sb.Append (x.ToString ());
>> sb.Append (" and I'm happy");
>> sb.ToString ()
>> });
Even if you could fake the one just above that, there's no way you can do that in D.
It would be foolish to think that Nemerle isn't an amazingly powerful language. D is good, and its' templates are very powerful, but they're not THAT powerful.
There is always more to learn :)
-- Daniel
|
November 28, 2006 Re: Thesis on metaprogramming in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Keep | Daniel Keep wrote: > Don Clugston wrote: >> Andrey Khropov wrote: >> >>> Steve Horne wrote: >>> >>>> My view of the ideal handling of metaprogramming is that it should >>>> have access to all the features that are available at runtime. The >>>> only languages that I know that genuinely achieve that are the >>>> Lisp-alikes such as Scheme. >>>> >>>> My ideal is being able to define 'quoted' blocks that are parsed and >>>> translated to ASTs, with those ASTs being processed by other code to >>>> derive new ASTs which are then submitted for the final back-end >>>> optimisation and code generation. >>> >>> >>> Nemerle does exactly that way! >> >> >> When I looked at the Nemerle website, I didn't see much that couldn't be done easily with D templates. It would be interesting to find something it can do, that D can't. >> > > > def sr = System.IO.StreamReader ("SomeFile.txt"); > > Immutable run-time variables. You can assign anything to them, and then it can't change. AFAIK, D's const doesn't let you do that. > > > someFunction(if (cond) expr_1 else expr_2); > > No statements--everything is an expression. And yes, it is VERY handy :) > > > import some.namespace.SomeClass; > > Import contents of static classes as well as modules. > > > match (control) { > > | button is Button => ... > > | listv is ListView => ... > > | _ => ... // null case > > } > > switch eat your heart out. You can use literal, pattern matching... just about anything. > > > def res2 = frobnicate (7, do_qux = true, > > do_baz = false, > > do_bar = true); > > Named parameters. D doesn't even have hashtable literals, so faking it would be a complete pain. > > > () > > That's the void literal. Basically, makes void act like any other type. Try writing generic code where functions can have void return values, and you'll end up writing everything twice since you can't have void variables. > > > variant RgbColor { > > | Red > > | Yellow > > | Green > > | Different { > > red : float; > > green : float; > > blue : float; > > } > > } > > Different to a union since it remembers what kind of thing is being stored. Fun trick with trees is to use a variant to store either a branch or a leaf. > > > macro while_macro (cond, body) > > syntax ("while", "(", cond, ")", body) { > > <[ > > def loop () { > > when ($cond) { > > $body; > > loop () > > } > > } > > loop () > > ]> > > } > > Do THAT with templates :3 The 'lazy evaluation' page has a similar example. :-) > >> def x = 3; > >> System.Console.WriteLine ($"My value of x is $x and I'm happy"); > > > > expands to > > > >> def x = 3; > >> System.Console.WriteLine ({ > >> def sb = System.Text.StringBuilder ("My value of x is "); > >> sb.Append (x.ToString ()); > >> sb.Append (" and I'm happy"); > >> sb.ToString () > >> }); > > Even if you could fake the one just above that, there's no way you can do that in D. You could if we got the identifier() keyword which I've previously proposed. Admittedly, it still wouldn't be pretty. But there's really because there's no support for embedded variables. But doing the same thing for System.Console.WriteLine("My value of x is ", x, " and I'm happy"); is trivial with the new tuples. > It would be foolish to think that Nemerle isn't an amazingly powerful language. D is good, and its' templates are very powerful, but they're not THAT powerful. I still have the impression that the functionality isn't much different. > There is always more to learn :) > > -- Daniel |
November 29, 2006 Re: Thesis on metaprogramming in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Don Clugston | On Tue, 28 Nov 2006 13:59:20 +0100, Don Clugston <dac@nospam.com.au> wrote: >Andrey Khropov wrote: >> Steve Horne wrote: >> >>> My view of the ideal handling of metaprogramming is that it should have access to all the features that are available at runtime. The only languages that I know that genuinely achieve that are the Lisp-alikes such as Scheme. >>> >>> My ideal is being able to define 'quoted' blocks that are parsed and translated to ASTs, with those ASTs being processed by other code to derive new ASTs which are then submitted for the final back-end optimisation and code generation. >> >> Nemerle does exactly that way! Cool! >When I looked at the Nemerle website, I didn't see much that couldn't be done easily with D templates. It would be interesting to find something it can do, that D can't. That doesn't worry me much, to be honest. I don't want another Scheme (or at least my perception of Scheme) where you start each journey not by getting into your car, but by re-inventing the wheel. The point in my view is to be able to opt of the standard toolkit when in those special cases where it doesn't quite fit, without having to opt out of the language completely. And so I imagine most generic programming tasks in Nemerle would use the .NET 2 generics. These aren't as flexible as C++ templates, let alone D ones, but they have the advantage that they are closure based and not prone to bloat. Nevertheless, there are useful things that you can't easily do with them. It sounds like Nemerle lets you work around that limitation when you need to. Being a .NET thing, it's presumably more of an apps-level language than systems-level, but maybe it has some transferrable ideas? -- Remove 'wants' and 'nospam' from e-mail. |
Copyright © 1999-2021 by the D Language Foundation