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).

cprint {
  sclass VStack implements Steppable {
    new L<Computable> stack;
    
    void subCompute(Computable computation) {
      computation.stack = this;
      stack.add(computation);
    }
    
    public bool step() {
      if (empty(stack)) false;
      if (last(stack).returned)
        popLast(stack);
      else
        last(stack).run();
      true;
    }
  }
  
  asclass Computable implements Runnable {
    VStack stack;
    bool returned;
    Runnable continuation;
  }
  
  sclass f_ackermann extends Computable {
    BigInt a, b;
    BigInt returnValue;
    
    *(BigInt *a, BigInt *b) {}
    
    record noeq JustReturn(f_ackermann subComputation) implements Runnable {
      run {
        returnValue = subComputation.returnValue;
        set returned;
      }
    }
    
    record noeq Step2(f_ackermann subComputation) implements Runnable {
      run {
        f_ackermann subComputation2 = new(minus(a, BigInt.ONE), 
          subComputation.returnValue);
        continuation = new JustReturn(subComputation2);
        stack.subCompute(subComputation2);
      }
    }
    
    run {
      if (continuation != null)
        ret with continuation.run();
        
      if (a.equals(BigInt.ZERO)) {
        returnValue = plus(b, BigInt.ONE);
        ret with set returned;
      }
      
      if (b.equals(BigInt.ZERO)) {
        f_ackermann subComputation = new(a.subtract(BigInt.ONE), BigInt.ONE);
        continuation = new JustReturn(subComputation);
        stack.subCompute(subComputation);
        ret;
      }
      
      f_ackermann subComputation = new(a, minus(b, BigInt.ONE));
      continuation = new Step2(subComputation);
      stack.subCompute(subComputation);
    }
  }
  
  start-thread {
    new VStack stack;
    f_ackermann ack = new(bigInt(2), bigInt(2));
    stack.subCompute(ack);
    stepAllWithStats(stack);
    print("Result: " + ack.returnValue);
  }
}

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: 203 / 321
Version history: 6 change(s)
Referenced in: [show references]