3

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?

sbi
  • 219,715
  • 46
  • 258
  • 445
  • Can you eloberate in which way "template metaprogramming" is useful for implementing a turing machine simulation? Maybe only your professor can. – codymanix Oct 19 '10 at 13:12
  • 6
    Um... **you** don't know why you have decided on this specific project? Personally, I'd say Turing machine implementations are tedious whatever implementation technique you choose: they're much more interesting as gedankenexperiments to reason about computability. Unless you really can implement a full Turing machine with infinite memory, of course: that'd be cool! :-) – Pontus Gagge Oct 19 '10 at 13:15
  • 2
    @Pontus Gagge - Most likely they picked it from a professor-provided list of possible projects. – T.E.D. Oct 19 '10 at 13:23
  • @T.E.D : I am afraid but your guess is incorrect. – Mukesh Kumar Oct 19 '10 at 13:24
  • 1
    So he's right that you came up with the idea yourself, but don't know why? *(boggles)* – T.E.D. Oct 19 '10 at 13:26
  • @T.E.D : No it was not my idea. Our team leader is C++ crazy and this project was his idea. – Mukesh Kumar Oct 19 '10 at 13:28
  • 1
    OK. I'm +1ing the question just for the correct use of "crazy" in the comments. :-) – T.E.D. Oct 19 '10 at 13:30
  • You're from India, aren't you?.. – P Shved Oct 19 '10 at 13:30
  • 3
    Template meta code implement loops as recursion and because of the limit on recursive depth that compilers impose on template meta code I think this is a bad idea (unless you have a very small program to run through the touring machine). – Martin York Oct 19 '10 at 13:41

3 Answers3

10

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
  • 363,768
  • 54
  • 674
  • 675
  • 1
    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 Oct 19 '10 at 13:23
  • C++ templates are turing complete? Really? – P Shved Oct 19 '10 at 13:38
  • @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 Oct 19 '10 at 13:48
  • @sepp2k, thanks for the link, I'll check it, but really... compiler can't work infinitely, while Turing machine can. – P Shved Oct 19 '10 at 14:02
  • @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 Oct 19 '10 at 14:09
  • 1
    @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 Oct 19 '10 at 14:15
  • @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... – P Shved Oct 19 '10 at 21:00
  • @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 Oct 19 '10 at 21:48
5

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
  • 57,498
  • 14
  • 111
  • 168
1

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
  • 5,134
  • 2
  • 33
  • 31