views:

157

answers:

3

Hi there.

I am a final year engineering student. Me and my friends have decided that our final year project would be "Simulation of Turing Machine using Template Metaprogramming".

I understand what "Turing Machine" and "Template Metaprogramming" are but my question is why the simulation would be tedious if we design the Turing Machine without TMP? What advantages can we get if we us TMP and what would we miss/gain if we don't using TMP but use a conventional approach?

Any suggestions as to how we shall proceed?

+7  A: 

The primary reason why one would implement Turing machines using template metaprogramming is not because it's easier than in "ordinary" C++ (it isn't), but to demonstrate that C++ templates are Turing complete.

sepp2k
Yes I think you got it right. Our team leader is crazy about C++ and it was his idea that we should use template metaprogramming to implement a Turing Machine.
Mukesh Kumar
Thanks accepted your answer.
Mukesh Kumar
C++ templates are turing complete? Really?
Pavel Shved
@Pavel: Yes. See [this paper](http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.14.3670) for a "sketch" of a proof.
sepp2k
@sepp2k, thanks for the link, I'll check it, but really... compiler can't work infinitely, while Turing machine can.
Pavel Shved
@Pavel: If you find a compiler that doesn't limit the maximum depth of templates, you can very easily get it to run forever (or more likely, until it runs out of space).
sepp2k
@Pavel: that's not what "Turing complete" means in reference to real systems. By convention it's taken to mean "barring resource limits". My PC can't work infinitely, either, but one doesn't normally say that for this reason Pentium assembly is "not Turing complete", although of course from a theoretical point of view it isn't Turing complete. In the case of TMP, the resource limits are imposed semi-arbitrarily by the compiler implementation, but the principle is the same.
Steve Jessop
@Steve, no, I just meant that an inherent property of any Turing complete language is a *theoretical* possibility of executing an infinite loop. I thought that standard-compliant template instantiation algorithm can't, but now it seems I have to revamp this...
Pavel Shved
@Pavel: I suspect (but I don't know for sure) that's an optimization problem. Functional languages also express loops as recursion (following lambda calculus), but they have a lot more incentive to prevent recursion from consuming resources. If a meta-program requires arbitrarily many instantiations of some template, does a compiler theoretically need to limit the number, or even to have representations of them in memory simultaneously?
Steve Jessop
+2  A: 

I don't think there are advantages to designing a Turing machine simulation using template metaprogramming. It's actually rather more like fencing with both hands tied behind your back, holding your foil between your teeth.

The reason you'd do this is to familiarize yourself with the power of the C++ template system, and to prove that C++ templates (and therefore the C++ compiler) are Turing complete.

Ken Bloom
+1 for the analogy.
larsmans
+1  A: 

To show its TM-completeness, it is sufficient to implement the Lambda calculus.

Anyway, it might be easy to implement a restricted TM with bits as symbols and a max band length of 64bit, where the blanks are 0. An alternative could be to disallow to write the blancs and count the relative position of the left and right terminator; then it will be 65bit wide. An uint64_t will hold all bits to the right in BigEndian, and to the left in LittleEndian; the active bit has to be in an own template parameter. Either one bit on the band has to stay 0, or there are counters to mark the end of the band.

comonad