views:

100

answers:

3

My programming language has no arrays, no lists, no pointers, no eval and no variable variables. All it has:

  • Ordinary variables like you know them from most programming languages: They all have an exact name and a value.

  • One stack. Functions provided are: push (add element to top), pop (remove element from top, get value) and empty (check if stack is empty)

My language is turing-complete. (Basic arithmetics, conditional jumps, etc implemented) That means, it must be possible to implement some sort of list or array, right?

But I have no idea how...

What I want to achieve: Create a function which can retrieve and/or change an element x of the stack.

I could easily add this function in the implementation of my language, in the interpreter, but I want to do it in my programming language.

  • "Solution" one (Accessing an element x, counting from the stack top)

Create a loop. Pop off the element from the stack top x times. The last element popped of is element number x. I end up with a destroyed stack.

  • Solution two:

Do the same as above, but store all popped off values in a second stack. Then you could move all elements back after you are done. But you know what? I don't have a second stack!

+1  A: 

Do you have procedure calls and recursion? Then you do have a second stack, the call stack. If not, are you sure it's Turing complete, and not just a PDA?

Logan Capaldo
I have a call stack, yes - but it is not accessible from the within language. It is an internal part of the interpreter.
Zack
Doesn't need to be directly accessible. If your procedures can have local variables and support recursion, you can use them to restore the stack.
Logan Capaldo
Sure, just recurse over the stack popping as you recurse and pushing as you return. However, stating in your question that you have only one stack is a bit misleading.
WhirlWind
No recursion, no local variables :)
Zack
+2  A: 

Sounds like a homework question, as it flexing random bits of Computer Science...

I think you would want to use recursion to do this. Say I have something like this..

Queue globalQueue = new Queue();

Then I could have code that got element X like this

public Object findElement(stepsToTake s) {

    if (queue.empty()) {
        throw new EmptyQueueYouFailException();
    }

    Object o = queue.pop();


   if (s == 0) {
        queue.push(o);
        return o;
    }

    Object actualResult = findElement( s - 1 );
    //restore this element to the stack
    queue.push(o);
    //return actual result
    return actualResult;
}

So more likely than not I made some bug... have not thought through it super well. Especially worried that I will reorder the stack because of the order of my calls..

Hopefully this can get you thinking along the right lines to get a solution?

bwawok
Then how do you store o?
WhirlWind
Well I grab o off the top.. then either base case or recursive case, o gets put back on. I don't think I lose any o with my algo, but I haven't thought through what order the os end up in before and after
bwawok
Thanks for your code example, I think I have some idea of what to do now. And btw: Why do you think that this is homework? I mean... I did neither mention a specific programming language nor ask for code! Your code example won't work for my language, though: It doesn't have scope and so the variable o will get overwritten by the next function call.
Zack
Since the language has no variables, this is a bit of a conundrum, eh?
WhirlWind
@WhirlWind: Please read the first two lines of my question
Zack
Well, very few people use a language with such a limited set of functions other than in a Computer Science class where they are trying to teach you to think like a computer :) Looking at item X in a queue is rather simple in any "real" language.
bwawok
Yeah, I think the OP has two stacks, not just one...
WhirlWind
Yeah I thought this was homework too. I can't see what else can be, other than a logic puzzle (and if it was a logic puzzle I see no reason not to say it explicitly).
Lo'oris
+2  A: 

If you have only one stack, this is equivalent to a pushdown automaton, which can recognize context-free languages, and is not Turing-complete. Your proof of Turing completeness should inform how you can implement freeform memory access.

In general, to prove Turing-completeness, you must be able to show how your language can move left to right over a tape (or indirectly simulate this process), which corresponds roughly to a single higher-level array.

WhirlWind
Strange: http://stackoverflow.com/questions/1389981/can-a-language-be-turing-complete-without-any-support-for-arrays
Zack
This is a matter of understanding what "any support for arrays" means. The lambda calculus doesn't have arrays as a primitive, but you can certainly create arrays (well lists anyway, array is usually understood to imply certain things about the implementation and ultimately certain things about the hardware) out of the primitives it does give you.
Logan Capaldo
@Zack "You can, however, simulate a linear data structure like a List by chaining functions together." -- from that very question. How would you do that with this language if your claim about a single stack is true?
WhirlWind