www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - About the template system

reply Georg Wrede <georg.wrede nospam.org> writes:
Existing efforts to create a template system have been
based on the mind-set of the C preprosessor.

Did some thinking...  :-)

What is the template system?
I think it is a language for transforming things. Not
entirely unlike the regex thing. The Template System (TS)
takes, what is essentially, "a multi line string",
from possibly several separate places in the source.
It also takes function signatures, and outputs "strings
that look like customized function definitions", which
it inserts into the source input stream. (In spirit, at
least.)

So, it is a language. A year ago I read that somebody had
discovered that the C++ TS is actually a Turing Complete
(TC) language. (Googling around I found e.g.:
http://homepage.mac.com/sigfpe/Computing/sk.html
where it is discussed quite well.)

So?

In hindsight, can it be, that TC is a required property
of a well functioning TS ?? If this is really true, then
we should consider TC from the outset, and just build
a language from that point of view.

----------

If this is the case, then two origins come to mind, off
hand. Either base it on Lambda Calculus, or make just
another Procedural Language (PL).

While I personally would do it with PL, my reasons would
only be that it is easier to grasp.   :-(

OTOH, a TS based on Lambda Calculus might bring truly
stunning power, flexibility and expandability.

Problem is, LC might not be too familiar to all, and
even worse, such a system might (?) result in a template
language that is also "unfamiliar looking" to the
"C language family" kind of programmer.

Actually, developing and prototyping this language would
be quite fast with Lisp or Scheme. But it might be also
reasonably easy and fast with D itself? (Both LC and PL
development can be done with all three.)

Another tack could be using Yacc, or some such?

----------

Any thoughts?

georg

PS: (( A not-so-serious idea, what if we cavalierly just
took an existing language and decided to use it as our
template language? Say, a subset of Lua or Euphoria??
Or DMD-Script? ))   ;-)
Feb 18 2005
parent reply Patrick Down <Patrick_member pathlink.com> writes:
In article <4215B2C8.7030706 nospam.org>, Georg Wrede says...
Existing efforts to create a template system have been
based on the mind-set of the C preprosessor.

Did some thinking...  :-)

What is the template system?
I think it is a language for transforming things. Not
entirely unlike the regex thing. The Template System (TS)
takes, what is essentially, "a multi line string",
from possibly several separate places in the source.
It also takes function signatures, and outputs "strings
that look like customized function definitions", which
it inserts into the source input stream. (In spirit, at
least.)

So, it is a language. 
Yes, I was looking at the C++ template system a while back and I came to the realization that it reminded me of a functional language. Ok, the resemblance is slight but the template system has two things in common with functional languages. 1. Functional languages often use pattern matching for conditionals. This is similar to using template specialization. 2. They use recursion for there looping constructs. I think the grand revelation behind all this is that a program is just data like every thing else. The lisp people had this figured out a long time ago. With lisp that programming language and data representation is one in the same so you can write programs to manipulate programs. For C++ and D it always seem like a little bit of a disjoint that what is basically a procedural language needs a functional like language for templates. One of the things I've been looking at lately is a .NET language called Boo (http://boo.codehaus.org/). It's interesting because it has an extensible compiler pipeline. It has a module that will render the code into an AST and then modules that will render the AST into .NET IL. You can write code in Boo ( or any other .NET language ) to rewrite the AST tree and insert it into the compiler pipeline. When you combine this with the ability to tie custom attributes to any function or data member as well as add you own keywords you get a pretty powerful language extension system. THe nice thing is the extensions can be written in the same language and the rest of the code. This is all well and good for .NET. What the solution is for more a more static language like D I don't have an answer for.
Feb 18 2005
parent Georg Wrede <georg.wrede nospam.org> writes:
(OT:
Damn, there are still a few rough edges in the Thunderbird
mail client Composer. Sometimes it has a hard time knowing
what is quoted or not.)

Patrick Down wrote:
 In article <4215B2C8.7030706 nospam.org>, Georg Wrede says...
 
Existing efforts to create a template system have been
based on the mind-set of the C preprosessor.
<snip>
 Yes, I was looking at the C++ template system a while back
 and I came to the realization that it reminded me of a functional
 language.  
Right. Seems that we really should give this some thought, and not just go on the old way.
 For C++ and D it always seem like a little bit of a disjoint that
 what is basically a procedural language needs a functional like
 language for templates.
Yes. OTOH, I think D should remain a procedural language. In fact, IMHO it's about as good as can be imagined, already! From this point of view, the disjointness between the language "itself" and its template system may be a good thing. People who come to D expect a procedural language, and changing that might slow down the inflow of new talent, or the adoption of D. Folks can get up to speed quickly with D. And because of this disjointness, the line between "mundane" and "esotheric" stays visible. Compare that to C++, where _everyone_ feel incompetent because there is no "visible" distinction between the different areas in the language. At the end of the day a professional C++ programmer feels guilty for still not having learnt it all. Well, that distinction wasn't all that important. But what is, is that the two-language-in-one paradigm, where you essentially have a compile time language and a runtime language, is quite powerful. And seems "natural" to many, at least in the markets we're out to get. (The audience screams "Get Stroustrup! Haddaboy! Now get Gosling! Go, go, go! Get Wall, get Hejlsberg! Kernighan!")
 One of the things I've been looking at lately is a .NET language called 
 Boo (http://boo.codehaus.org/).  It's interesting because it has an 
 extensible compiler pipeline.  It has a module that will render the 
 code into an AST and then modules that will render the AST into .NET IL.  
 You can write code in Boo ( or any other .NET language ) to rewrite the 
 AST tree and insert it into the compiler pipeline.  When you combine 
 this with the ability to tie custom attributes to any function or data 
 member as well as add you own keywords you get a pretty powerful language 
 extension system.  THe nice thing is the extensions can be written in the 
 same language and the rest of the code.
 
 This is all well and good for .NET.  What the solution is for 
 more a more static language like D I don't have an answer for.
My gut feeling (so far nothing more) says this is an incredibly important issue. I wonder, the preprocessor needs of D and C++ are different. But what actually makes this differece?
Feb 19 2005