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

77
LINES

< > BotCompany Repo | #1030373 // Turn Ackermann function into a virtual stack [OK]

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

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

1  
cprint {
2  
  sclass VStack implements Steppable {
3  
    new L<Computable> stack;
4  
    
5  
    void subCompute(Computable computation) {
6  
      computation.stack = this;
7  
      stack.add(computation);
8  
    }
9  
    
10  
    public bool step() {
11  
      if (empty(stack)) false;
12  
      if (last(stack).returned)
13  
        popLast(stack);
14  
      else
15  
        last(stack).run();
16  
      true;
17  
    }
18  
  }
19  
  
20  
  asclass Computable implements Runnable {
21  
    VStack stack;
22  
    bool returned;
23  
    Runnable continuation;
24  
  }
25  
  
26  
  sclass f_ackermann extends Computable {
27  
    BigInt a, b;
28  
    BigInt returnValue;
29  
    
30  
    *(BigInt *a, BigInt *b) {}
31  
    
32  
    record noeq JustReturn(f_ackermann subComputation) implements Runnable {
33  
      run {
34  
        returnValue = subComputation.returnValue;
35  
        set returned;
36  
      }
37  
    }
38  
    
39  
    record noeq Step2(f_ackermann subComputation) implements Runnable {
40  
      run {
41  
        f_ackermann subComputation2 = new(minus(a, BigInt.ONE), 
42  
          subComputation.returnValue);
43  
        continuation = new JustReturn(subComputation2);
44  
        stack.subCompute(subComputation2);
45  
      }
46  
    }
47  
    
48  
    run {
49  
      if (continuation != null)
50  
        ret with continuation.run();
51  
        
52  
      if (a.equals(BigInt.ZERO)) {
53  
        returnValue = plus(b, BigInt.ONE);
54  
        ret with set returned;
55  
      }
56  
      
57  
      if (b.equals(BigInt.ZERO)) {
58  
        f_ackermann subComputation = new(a.subtract(BigInt.ONE), BigInt.ONE);
59  
        continuation = new JustReturn(subComputation);
60  
        stack.subCompute(subComputation);
61  
        ret;
62  
      }
63  
      
64  
      f_ackermann subComputation = new(a, minus(b, BigInt.ONE));
65  
      continuation = new Step2(subComputation);
66  
      stack.subCompute(subComputation);
67  
    }
68  
  }
69  
  
70  
  start-thread {
71  
    new VStack stack;
72  
    f_ackermann ack = new(bigInt(2), bigInt(2));
73  
    stack.subCompute(ack);
74  
    stepAllWithStats(stack);
75  
    print("Result: " + ack.returnValue);
76  
  }
77  
}

Author comment

Began life as a copy of #1030372

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1030373
Snippet name: Turn Ackermann function into a virtual stack [OK]
Eternal ID of this version: #1030373/7
Text MD5: 227afedfc71512ffd21b5bf32f7161fe
Transpilation MD5: c226f63006e47eb32dacb44bd1716c3b
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 13:58:37
Source code size: 1982 bytes / 77 lines
Pitched / IR pitched: No / No
Views / Downloads: 129 / 222
Version history: 6 change(s)
Referenced in: [show references]