Documentation

Examples.Ram.Merge

A complete linear merge on the actual RAM machine #

The shared declaration in Array.Merge.Function uses the same fixed merge loop as the reusable contract. Its input consists of two preloaded source arrays and a disjoint, already allocated destination. The sources may overlap each other. The block theorem explicitly starts at PC 1 and includes halt, not an implicit loader or allocator. The bitwise-OR guard does not add the two counts in a word.

The result theorem observes standard List.merge in the halted target heap, with both sources, destination-external heap words, I/O, and higher user registers preserved by that same run. Sortedness is needed only by the final sorted-merge theorem, not by the general machine execution theorem.

Function.merge executes the typed three-array function and returns its actual shared state. Its mathematical contents theorem uses ordinary List.merge; the separate count includes both the inner core call and the outer typed call.

Observe the shared source loop as a standalone block. The callable interface below uses the same declaration, not another copy of its source.

Equations
Instances For
    theorem Ram.Examples.Merge.valid {control : ℕ} (hcontrol : 5 ≤ control) :
    theorem Ram.Examples.Merge.checked {control : ℕ} (hcontrol : 5 ≤ control) :
    theorem Ram.Examples.Merge.code_length (control : ℕ) :
    List.length (code control) = 84
    structure Ram.Examples.Merge.Result {w : ℕ} (control heapLimit : ℕ) (left right destination : Word w) (xs ys : List (Word w)) (entry finish : State w) :

    All output observations are attached to one completed target execution. Heap preservation is stated inside the source-visible heap; compiler-private stack/control storage is not misidentified as user array storage.

    Instances For
      theorem Ram.Examples.Merge.block_runs {w control heapLimit : ℕ} {left right destination : Word w} {xs ys scratch : List (Word w)} {s : Source.State w} {start : State w} (hw : 0 < w) (hcontrol : 5 ≤ control) (hlen : scratch.length = xs.length + ys.length) (hdl : ArraysDisjoint destination (xs.length + ys.length) left xs.length) (hdr : ArraysDisjoint destination (xs.length + ys.length) right ys.length) (hpre : Source.Array.Merge.Pre heapLimit left right destination xs ys scratch s) (hmatch : Source.State.Matches heapLimit control s start) (hpc : start.pc = 1) (hheap : heapLimit ≤ BitVec.toNat (start.regs (ABI.sp control))) (hcodefit : List.length (code control) < 2 ^ w) :
      ∃ (sourceFinal : Source.State w) (finish : State w), TerminatesWithin (code control) (34 * (xs.length + ys.length) + 5) start finish ∧ Source.Array.Merge.Post heapLimit left right destination xs ys s sourceFinal ∧ Source.State.Observes heapLimit control sourceFinal finish ∧ FramePreserved control heapLimit start finish

      The source contract and existing checked compiler supply the complete target execution. The extra one is the actual halt; PC 1 excludes the unused input-header prologue from both the execution and its claimed budget.

      theorem Ram.Examples.Merge.merge_on_machine {w control heapLimit : ℕ} {left right destination : Word w} {xs ys scratch : List (Word w)} {s : Source.State w} {start : State w} (hw : 0 < w) (hcontrol : 5 ≤ control) (hlen : scratch.length = xs.length + ys.length) (hdl : ArraysDisjoint destination (xs.length + ys.length) left xs.length) (hdr : ArraysDisjoint destination (xs.length + ys.length) right ys.length) (hpre : Source.Array.Merge.Pre heapLimit left right destination xs ys scratch s) (hmatch : Source.State.Matches heapLimit control s start) (hpc : start.pc = 1) (hheap : heapLimit ≤ BitVec.toNat (start.regs (ABI.sp control))) (hcodefit : List.length (code control) < 2 ^ w) :
      ∃ (finish : State w), TerminatesWithin (code control) (34 * (xs.length + ys.length) + 5) start finish ∧ Result control heapLimit left right destination xs ys start finish

      General merge works even for unsorted source arrays and permits overlap between them. Its actual result is standard List.merge, with unchanged sources, a destination frame, final pointers/counts, and retained caller I/O.

      theorem Ram.Examples.Merge.merge_sorted_on_machine {w control heapLimit : ℕ} {left right destination : Word w} {xs ys scratch : List (Word w)} {s : Source.State w} {start : State w} (hw : 0 < w) (hcontrol : 5 ≤ control) (hlen : scratch.length = xs.length + ys.length) (hdl : ArraysDisjoint destination (xs.length + ys.length) left xs.length) (hdr : ArraysDisjoint destination (xs.length + ys.length) right ys.length) (hxs : List.Pairwise Source.Array.unsignedLE xs) (hys : List.Pairwise Source.Array.unsignedLE ys) (hpre : Source.Array.Merge.Pre heapLimit left right destination xs ys scratch s) (hmatch : Source.State.Matches heapLimit control s start) (hpc : start.pc = 1) (hheap : heapLimit ≤ BitVec.toNat (start.regs (ABI.sp control))) (hcodefit : List.length (code control) < 2 ^ w) :
      ∃ (finish : State w) (output : List (Word w)), TerminatesWithin (code control) (34 * (xs.length + ys.length) + 5) start finish ∧ Result control heapLimit left right destination xs ys start finish ∧ ArrayRep finish.mem destination output ∧ Source.Array.SortedPerm (xs ++ ys) output

      When both source lists are sorted, the very same halted heap contains a sorted permutation of their concatenation. The mathematical bridge is the existing standard merge theorem, not an added sortedness assumption on the general machine execution rule.

      def Ram.Examples.Merge.Function.Safe (left right destination : ArrayRef 32) (heapLimit : ℕ) (entry : Source.State 32) :

      Three existing represented arrays admit a merge into the independent destination. The two sources need not be disjoint from each other.

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

        Termination of the typed wrapper and its core follows from budget-free correctness. Stack capacity pays for both live function frames.

        def Ram.Examples.Merge.Function.merge (left right destination : ArrayRef 32) (heapLimit : ℕ) (entry : Source.State 32) (safe : Safe left right destination heapLimit entry) (hstack : heapLimit + 2 * ABI.frameSize Source.Array.Merge.mergeFunctions.registers < 2 ^ 32) :

        Execute the three-array function and return its actual shared state. The lists and safety proofs are erased; no result or time budget is an input.

        Equations
        Instances For
          theorem Ram.Examples.Merge.Function.merge_spec {left right destination : ArrayRef 32} {heapLimit : ℕ} {entry : Source.State 32} (safe : Safe left right destination heapLimit entry) (hstack : heapLimit + 2 * ABI.frameSize Source.Array.Merge.mergeFunctions.registers < 2 ^ 32) {xs ys scratch : List (Word 32)} (length : scratch.length = xs.length + ys.length) (leftArray : left.Rep heapLimit xs entry) (rightArray : right.Rep heapLimit ys entry) (destinationArray : destination.Rep heapLimit scratch entry) (hdl : ArraysDisjoint destination.base (xs.length + ys.length) left.base xs.length) (hdr : ArraysDisjoint destination.base (xs.length + ys.length) right.base ys.length) :
          have finish := merge left right destination heapLimit entry safe hstack; left.Rep heapLimit xs finish ∧ right.Rep heapLimit ys finish ∧ destination.Rep heapLimit (Source.Array.unsignedMerge xs ys) finish ∧ ArrayFrame destination.base (xs.length + ys.length) entry.mem finish.mem ∧ finish.input = entry.input ∧ finish.outputRev = entry.outputRev

          Ordinary array representations specify the state produced by the actual typed application. Only the destination may change; both sources and I/O survive.

          theorem Ram.Examples.Merge.Function.merge_contents {left right destination : ArrayRef 32} {heapLimit : ℕ} {entry : Source.State 32} (safe : Safe left right destination heapLimit entry) (hstack : heapLimit + 2 * ABI.frameSize Source.Array.Merge.mergeFunctions.registers < 2 ^ 32) {xs ys scratch : List (Word 32)} (length : scratch.length = xs.length + ys.length) (leftArray : left.Rep heapLimit xs entry) (rightArray : right.Rep heapLimit ys entry) (destinationArray : destination.Rep heapLimit scratch entry) (hdl : ArraysDisjoint destination.base (xs.length + ys.length) left.base xs.length) (hdr : ArraysDisjoint destination.base (xs.length + ys.length) right.base ys.length) :
          arrayContents (merge left right destination heapLimit entry safe hstack).mem destination.base (BitVec.toNat destination.length) = Source.Array.unsignedMerge xs ys

          The actual destination contents equal standard list merge. Sortedness is not required for this equation about the implementation.

          theorem Ram.Examples.Merge.Function.merge_sorted {left right destination : ArrayRef 32} {heapLimit : ℕ} {entry : Source.State 32} (safe : Safe left right destination heapLimit entry) (hstack : heapLimit + 2 * ABI.frameSize Source.Array.Merge.mergeFunctions.registers < 2 ^ 32) {xs ys scratch : List (Word 32)} (length : scratch.length = xs.length + ys.length) (leftArray : left.Rep heapLimit xs entry) (rightArray : right.Rep heapLimit ys entry) (destinationArray : destination.Rep heapLimit scratch entry) (hdl : ArraysDisjoint destination.base (xs.length + ys.length) left.base xs.length) (hdr : ArraysDisjoint destination.base (xs.length + ys.length) right.base ys.length) (hxs : List.Pairwise Source.Array.unsignedLE xs) (hys : List.Pairwise Source.Array.unsignedLE ys) :
          Source.Array.SortedPerm (xs ++ ys) (arrayContents (merge left right destination heapLimit entry safe hstack).mem destination.base (BitVec.toNat destination.length))

          Upstream merge properties give a sorted permutation of both inputs, without reopening the implementation's loop, bindings or calling convention.

          theorem Ram.Examples.Merge.Function.runTotal_steps_le {left right destination : ArrayRef 32} {heapLimit : ℕ} {entry : Source.State 32} (safe : Safe left right destination heapLimit entry) (hstack : heapLimit + 2 * ABI.frameSize Source.Array.Merge.mergeFunctions.registers < 2 ^ 32) {xs ys scratch : List (Word 32)} (length : scratch.length = xs.length + ys.length) (leftArray : left.Rep heapLimit xs entry) (rightArray : right.Rep heapLimit ys entry) (destinationArray : destination.Rep heapLimit scratch entry) (hdl : ArraysDisjoint destination.base (xs.length + ys.length) left.base xs.length) (hdr : ArraysDisjoint destination.base (xs.length + ys.length) right.base ys.length) :
          (Source.Array.Merge.mergeFunctions.runTotal.merge left right destination heapLimit entry ⋯).steps ≤ 34 * (xs.length + ys.length) + 122

          The complete typed invocation includes both core and wrapper calls, their returns, and the final halt. Preloading remains outside this count.