Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

64
LINES

< > BotCompany Repo | #1030375 // Turn Ackermann function into a virtual stack [memory-compact version plus tail calls]

JavaX source code (Dynamic Module) [tags: use-pretranspiled] - run with: Stefan's OS

Uses 911K of libraries. Click here for Pure Java version (2453L/13K).

1  
cprint {
2  
  sclass VStack implements Steppable {
3  
    new L<Computable> stack;
4  
    O latestResult;
5  
    
6  
    void push(Computable computation) {
7  
      stack.add(computation);
8  
    }
9  
    
10  
    public bool step() {
11  
      if (empty(stack)) false;
12  
      O latestResult = this.latestResult;
13  
      this.latestResult = null;
14  
      last(stack).step(this, latestResult);
15  
      true;
16  
    }
17  
    
18  
    // called from computation to return a value
19  
    void _return(O value default null) {
20  
      latestResult = value;
21  
      removeLast(stack);
22  
    }
23  
    
24  
    // called from computation to tail-call another routine
25  
    void tailCall(Computable computation) {
26  
      removeLast(stack);
27  
      stack.add(computation);
28  
    }
29  
  }
30  
  
31  
  // A is what the function returns
32  
  asclass Computable<A> {
33  
    abstract void step(VStack stack, O subComputationResult);
34  
  }
35  
  
36  
  sclass f_ackermann extends Computable<BigInt> {
37  
    BigInt a, b;
38  
    int currentStep; // 0 in the beginning, goes up to 1
39  
40  
    *(BigInt *a, BigInt *b) {}
41  
    
42  
    void step(VStack stack, O subComputationResult) {
43  
      if (currentStep == 0) {
44  
        if (a.equals(BigInt.ZERO))
45  
          stack._return(plus(b, BigInt.ONE));
46  
        else if (b.equals(BigInt.ZERO))
47  
          stack.tailCall(new f_ackermann(a.subtract(BigInt.ONE), BigInt.ONE);
48  
        else {
49  
          stack.push(new f_ackermann(a, minus(b, BigInt.ONE)));
50  
          currentStep = 1;
51  
        }
52  
      } else if (currentStep == 1)
53  
        stack.tailCall(new f_ackermann(minus(a, BigInt.ONE), subComputationResult/BigInt));
54  
      else fail();
55  
    }
56  
  }
57  
  
58  
  start-thread {
59  
    new VStack stack;
60  
    stack.push(new f_ackermann(bigInt(2), bigInt(2)));
61  
    stepAllWithStats(stack);
62  
    print("Result: " + stack.latestResult);
63  
  }
64  
}

Author comment

Began life as a copy of #1030374

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt

No comments. add comment

Snippet ID: #1030375
Snippet name: Turn Ackermann function into a virtual stack [memory-compact version plus tail calls]
Eternal ID of this version: #1030375/7
Text MD5: 4af1f55538e845610d786af005ed6259
Transpilation MD5: 28d79d42d49e8e99bed6bed589579e50
Author: stefan
Category: javax / maths
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-12-10 14:09:01
Source code size: 1791 bytes / 64 lines
Pitched / IR pitched: No / No
Views / Downloads: 231 / 356
Version history: 6 change(s)
Referenced in: [show references]