Documentation

Examples.Language.Splay.Program

An in-place splay access #

The three borrowed arrays store immutable keys and mutable left/right child indices. Zero denotes an empty subtree; nonzero indices name stable nodes. splay returns the new root after an ordinary key search, including unsuccessful searches. Its recursive calls descend by two edges and its rotations implement zig, zig-zig and zig-zag, not the different move-to-root algorithm.

The declaration is the sole executable implementation. No mathematical tree is passed to it, and no host tree operation replaces any of its reads or writes. The source contracts establish valid indices and successful termination; separate backend contracts account for finite words, storage and execution cost.

theorem Complexity.Language.Examples.Splay.Implementation.splay_spec {pre✝ : Env [Ty.buffer CellTy.nat, Ty.buffer CellTy.nat, Ty.buffer CellTy.nat, Ty.nat, Ty.nat] → Heap → Prop} {post✝ : Env [Ty.buffer CellTy.nat, Ty.buffer CellTy.nat, Ty.buffer CellTy.nat, Ty.nat, Ty.nat] → Heap → ℕ → Heap → Prop} :
FunctionTotal program splayId pre✝ post✝ → ∀ (keys left right : Buffer CellTy.nat) (root key : ℕ) (continuation : Std.Do.PostCond ℕ (Std.Do.PostShape.except Fault (Std.Do.PostShape.arg Heap Std.Do.PostShape.pure))), ⦃fun (initialHeap : Heap) => { down := pre✝ (Env.cons keys (Env.cons left (Env.cons right (Env.cons root (Env.cons key Env.empty))))) initialHeap ∧ ∀ (value : ℕ) (finalHeap : Heap), post✝ (Env.cons keys (Env.cons left (Env.cons right (Env.cons root (Env.cons key Env.empty))))) initialHeap value finalHeap → (continuation.1 value finalHeap).down }⦄ splay keys left right root key ⦃continuation⦄

Apply a supplied source contract with ordinary named arguments and its actual returned value and final heap. No callee body or new contract is inferred.

@[reducible, inline]

The actual declared argument environment, constructed from ordinary source parameters. This is proof-side parameter transport, not an additional runtime operation.

Equations
  • One or more equations did not get rendered due to their size.
Instances For

    The finite table of actual named source bodies.

    Equations
    • One or more equations did not get rendered due to their size.
    Instances For

      Fold an actual source invocation back to its named ordinary-argument observation.

      theorem Complexity.Language.Examples.Splay.Implementation.rotateLeft_total_iff (pre✝ : Buffer CellTy.nat → Buffer CellTy.nat → ℕ → Heap → Prop) (post✝ : Buffer CellTy.nat → Buffer CellTy.nat → ℕ → Heap → ℕ → Heap → Prop) :
      rotateLeft_contract pre✝ post✝ ↔ ∀ (left right : Buffer CellTy.nat) (root : ℕ) (initialHeap : Heap), pre✝ left right root initialHeap → ∃ (value : ℕ) (finalHeap : Heap), rotateLeft left right root initialHeap = Part.some (Except.ok value, finalHeap) ∧ post✝ left right root initialHeap value finalHeap

      The source contract is equivalent to ordinary curried preconditions and successful result/heap postconditions, including termination and absence of faults.

      Fold an actual source invocation back to its named ordinary-argument observation.

      Ordinary arguments and the actual result observed in their real source heaps. This interface does not assert the existence of a total mathematical model.

      Equations
      • One or more equations did not get rendered due to their size.
      Instances For

        Fold an actual source invocation back to its named ordinary-argument observation.

        The named function's actual independently interpreted source body.

        Equations
        • One or more equations did not get rendered due to their size.
        Instances For
          theorem Complexity.Language.Examples.Splay.Implementation.rotateLeft_spec {pre✝ : Env [Ty.buffer CellTy.nat, Ty.buffer CellTy.nat, Ty.nat] → Heap → Prop} {post✝ : Env [Ty.buffer CellTy.nat, Ty.buffer CellTy.nat, Ty.nat] → Heap → ℕ → Heap → Prop} :
          FunctionTotal program rotateLeftId pre✝ post✝ → ∀ (left right : Buffer CellTy.nat) (root : ℕ) (continuation : Std.Do.PostCond ℕ (Std.Do.PostShape.except Fault (Std.Do.PostShape.arg Heap Std.Do.PostShape.pure))), ⦃fun (initialHeap : Heap) => { down := pre✝ (Env.cons left (Env.cons right (Env.cons root Env.empty))) initialHeap ∧ ∀ (value : ℕ) (finalHeap : Heap), post✝ (Env.cons left (Env.cons right (Env.cons root Env.empty))) initialHeap value finalHeap → (continuation.1 value finalHeap).down }⦄ rotateLeft left right root ⦃continuation⦄

          Apply a supplied source contract with ordinary named arguments and its actual returned value and final heap. No callee body or new contract is inferred.

          theorem Complexity.Language.Examples.Splay.Implementation.rotateRight_spec {pre✝ : Env [Ty.buffer CellTy.nat, Ty.buffer CellTy.nat, Ty.nat] → Heap → Prop} {post✝ : Env [Ty.buffer CellTy.nat, Ty.buffer CellTy.nat, Ty.nat] → Heap → ℕ → Heap → Prop} :
          FunctionTotal program rotateRightId pre✝ post✝ → ∀ (left right : Buffer CellTy.nat) (root : ℕ) (continuation : Std.Do.PostCond ℕ (Std.Do.PostShape.except Fault (Std.Do.PostShape.arg Heap Std.Do.PostShape.pure))), ⦃fun (initialHeap : Heap) => { down := pre✝ (Env.cons left (Env.cons right (Env.cons root Env.empty))) initialHeap ∧ ∀ (value : ℕ) (finalHeap : Heap), post✝ (Env.cons left (Env.cons right (Env.cons root Env.empty))) initialHeap value finalHeap → (continuation.1 value finalHeap).down }⦄ rotateRight left right root ⦃continuation⦄

          Apply a supplied source contract with ordinary named arguments and its actual returned value and final heap. No callee body or new contract is inferred.

          theorem Complexity.Language.Examples.Splay.Implementation.rotateLeft_eq (left right : Buffer CellTy.nat) (root : ℕ) :
          rotateLeft left right root = do let child ← right.readM root let middle ← left.readM child right.writeM root middle left.writeM child root pure child

          One source-body equation in ordinary monadic notation; named callees remain opaque.

          The named function's actual independently interpreted source body.

          Equations
          • One or more equations did not get rendered due to their size.
          Instances For
            theorem Complexity.Language.Examples.Splay.Implementation.splay_contract_iff_triple (pre✝ : Buffer CellTy.nat → Buffer CellTy.nat → Buffer CellTy.nat → ℕ → ℕ → Heap → Prop) (post✝ : Buffer CellTy.nat → Buffer CellTy.nat → Buffer CellTy.nat → ℕ → ℕ → Heap → ℕ → Heap → Prop) :
            splay_contract pre✝ post✝ ↔ ∀ (keys left right : Buffer CellTy.nat) (root key : ℕ) (initialHeap : Heap), ⦃fun (currentHeap : Heap) => { down := currentHeap = initialHeap ∧ pre✝ keys left right root key initialHeap }⦄ splay keys left right root key ⦃(fun (value : ℕ) (finalHeap : Heap) => { down := post✝ keys left right root key initialHeap value finalHeap }, fun (x : Fault) (x_1 : Heap) => { down := False }, PUnit.unit)⦄

            Prove the ordinary-parameter source contract with a total-correctness triple for the same action. The ghost initial heap equals the actual entry heap; successful results use the actual final heap, and faults are excluded.

            @[reducible, inline]

            Apply a predicate, postcondition or bound with ordinary source parameters to the actual argument environment. No new contract or execution is introduced.

            Equations
            Instances For

              The named function's actual partial source action, with ordinary typed arguments and an explicitly supplied shared heap.

              Equations
              • One or more equations did not get rendered due to their size.
              Instances For
                @[reducible, inline]

                Budget-free total correctness with ordinary source parameters and relational initial/final heap postconditions. This is the existing source function contract.

                Equations
                • One or more equations did not get rendered due to their size.
                Instances For
                  theorem Complexity.Language.Examples.Splay.Implementation.rotateLeft_contract_iff_triple (pre✝ : Buffer CellTy.nat → Buffer CellTy.nat → ℕ → Heap → Prop) (post✝ : Buffer CellTy.nat → Buffer CellTy.nat → ℕ → Heap → ℕ → Heap → Prop) :
                  rotateLeft_contract pre✝ post✝ ↔ ∀ (left right : Buffer CellTy.nat) (root : ℕ) (initialHeap : Heap), ⦃fun (currentHeap : Heap) => { down := currentHeap = initialHeap ∧ pre✝ left right root initialHeap }⦄ rotateLeft left right root ⦃(fun (value : ℕ) (finalHeap : Heap) => { down := post✝ left right root initialHeap value finalHeap }, fun (x : Fault) (x_1 : Heap) => { down := False }, PUnit.unit)⦄

                  Prove the ordinary-parameter source contract with a total-correctness triple for the same action. The ghost initial heap equals the actual entry heap; successful results use the actual final heap, and faults are excluded.

                  The named function's actual partial source action, with ordinary typed arguments and an explicitly supplied shared heap.

                  Equations
                  • One or more equations did not get rendered due to their size.
                  Instances For
                    theorem Complexity.Language.Examples.Splay.Implementation.rotateRight_contract_iff_triple (pre✝ : Buffer CellTy.nat → Buffer CellTy.nat → ℕ → Heap → Prop) (post✝ : Buffer CellTy.nat → Buffer CellTy.nat → ℕ → Heap → ℕ → Heap → Prop) :
                    rotateRight_contract pre✝ post✝ ↔ ∀ (left right : Buffer CellTy.nat) (root : ℕ) (initialHeap : Heap), ⦃fun (currentHeap : Heap) => { down := currentHeap = initialHeap ∧ pre✝ left right root initialHeap }⦄ rotateRight left right root ⦃(fun (value : ℕ) (finalHeap : Heap) => { down := post✝ left right root initialHeap value finalHeap }, fun (x : Fault) (x_1 : Heap) => { down := False }, PUnit.unit)⦄

                    Prove the ordinary-parameter source contract with a total-correctness triple for the same action. The ghost initial heap equals the actual entry heap; successful results use the actual final heap, and faults are excluded.

                    The named function's actual partial source action, with ordinary typed arguments and an explicitly supplied shared heap.

                    Equations
                    • One or more equations did not get rendered due to their size.
                    Instances For
                      theorem Complexity.Language.Examples.Splay.Implementation.rotateRight_eq (left right : Buffer CellTy.nat) (root : ℕ) :
                      rotateRight left right root = do let child ← left.readM root let middle ← right.readM child left.writeM root middle right.writeM child root pure child

                      One source-body equation in ordinary monadic notation; named callees remain opaque.

                      theorem Complexity.Language.Examples.Splay.Implementation.splay_total_iff (pre✝ : Buffer CellTy.nat → Buffer CellTy.nat → Buffer CellTy.nat → ℕ → ℕ → Heap → Prop) (post✝ : Buffer CellTy.nat → Buffer CellTy.nat → Buffer CellTy.nat → ℕ → ℕ → Heap → ℕ → Heap → Prop) :
                      splay_contract pre✝ post✝ ↔ ∀ (keys left right : Buffer CellTy.nat) (root key : ℕ) (initialHeap : Heap), pre✝ keys left right root key initialHeap → ∃ (value : ℕ) (finalHeap : Heap), splay keys left right root key initialHeap = Part.some (Except.ok value, finalHeap) ∧ post✝ keys left right root key initialHeap value finalHeap

                      The source contract is equivalent to ordinary curried preconditions and successful result/heap postconditions, including termination and absence of faults.

                      The named function's actual independently interpreted source body.

                      Equations
                      • One or more equations did not get rendered due to their size.
                      Instances For
                        @[reducible, inline]

                        The source program's declared first-order signatures.

                        Equations
                        • One or more equations did not get rendered due to their size.
                        Instances For

                          Ordinary arguments and the actual result observed in their real source heaps. This interface does not assert the existence of a total mathematical model.

                          Equations
                          • One or more equations did not get rendered due to their size.
                          Instances For
                            @[reducible, inline]

                            Budget-free total correctness with ordinary source parameters and relational initial/final heap postconditions. This is the existing source function contract.

                            Equations
                            • One or more equations did not get rendered due to their size.
                            Instances For
                              @[reducible, inline]

                              Apply a predicate, postcondition or bound with ordinary source parameters to the actual argument environment. No new contract or execution is introduced.

                              Equations
                              Instances For
                                @[reducible, inline]

                                Apply a predicate, postcondition or bound with ordinary source parameters to the actual argument environment. No new contract or execution is introduced.

                                Equations
                                Instances For
                                  @[reducible, inline]

                                  Budget-free total correctness with ordinary source parameters and relational initial/final heap postconditions. This is the existing source function contract.

                                  Equations
                                  • One or more equations did not get rendered due to their size.
                                  Instances For
                                    theorem Complexity.Language.Examples.Splay.Implementation.rotateRight_total_iff (pre✝ : Buffer CellTy.nat → Buffer CellTy.nat → ℕ → Heap → Prop) (post✝ : Buffer CellTy.nat → Buffer CellTy.nat → ℕ → Heap → ℕ → Heap → Prop) :
                                    rotateRight_contract pre✝ post✝ ↔ ∀ (left right : Buffer CellTy.nat) (root : ℕ) (initialHeap : Heap), pre✝ left right root initialHeap → ∃ (value : ℕ) (finalHeap : Heap), rotateRight left right root initialHeap = Part.some (Except.ok value, finalHeap) ∧ post✝ left right root initialHeap value finalHeap

                                    The source contract is equivalent to ordinary curried preconditions and successful result/heap postconditions, including termination and absence of faults.

                                    theorem Complexity.Language.Examples.Splay.Implementation.splay_eq (keys left right : Buffer CellTy.nat) (root key : ℕ) :
                                    splay keys left right root key = have condition := decide (root = 0); if condition = true then pure 0 else do let rootKey ← keys.readM root have condition : Bool := decide (key < rootKey) if condition = true then do let child ← left.readM root have condition : Bool := decide (child = 0) if condition = true then pure root else do let childKey ← keys.readM child have condition : Bool := decide (key < childKey) if condition = true then do let grandchild ← left.readM child let newGrandchild ← splay keys left right grandchild key left.writeM child newGrandchild let moved ← rotateRight left right root let remaining ← left.readM moved have condition : Bool := decide (remaining = 0) if condition = true then pure moved else do let result ← rotateRight left right moved pure result else have condition := decide (childKey < key); if condition = true then do let grandchild ← right.readM child let newGrandchild ← splay keys left right grandchild key right.writeM child newGrandchild have condition : Bool := decide (newGrandchild = 0) if condition = true then do let result ← rotateRight left right root pure result else do let moved ← rotateLeft left right child left.writeM root moved let result ← rotateRight left right root pure result else do let result ← rotateRight left right root pure result else have condition := decide (rootKey < key); if condition = true then do let child ← right.readM root have condition : Bool := decide (child = 0) if condition = true then pure root else do let childKey ← keys.readM child have condition : Bool := decide (childKey < key) if condition = true then do let grandchild ← right.readM child let newGrandchild ← splay keys left right grandchild key right.writeM child newGrandchild let moved ← rotateLeft left right root let remaining ← right.readM moved have condition : Bool := decide (remaining = 0) if condition = true then pure moved else do let result ← rotateLeft left right moved pure result else have condition := decide (key < childKey); if condition = true then do let grandchild ← left.readM child let newGrandchild ← splay keys left right grandchild key left.writeM child newGrandchild have condition : Bool := decide (newGrandchild = 0) if condition = true then do let result ← rotateLeft left right root pure result else do let moved ← rotateRight left right child right.writeM root moved let result ← rotateLeft left right root pure result else do let result ← rotateLeft left right root pure result else pure root

                                    One source-body equation in ordinary monadic notation; named callees remain opaque.

                                    @[reducible, inline]

                                    The actual declared argument environment, constructed from ordinary source parameters. This is proof-side parameter transport, not an additional runtime operation.

                                    Equations
                                    • One or more equations did not get rendered due to their size.
                                    Instances For
                                      @[reducible, inline]

                                      The actual declared argument environment, constructed from ordinary source parameters. This is proof-side parameter transport, not an additional runtime operation.

                                      Equations
                                      • One or more equations did not get rendered due to their size.
                                      Instances For

                                        Ordinary arguments and the actual result observed in their real source heaps. This interface does not assert the existence of a total mathematical model.

                                        Equations
                                        • One or more equations did not get rendered due to their size.
                                        Instances For