summaryrefslogtreecommitdiff
path: root/Verification/Find.lean
blob: 9ff7f3da21a650e25a6889e5722234334c4b9a0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import Verification.Tree
import Verification.BinarySearchTree
import Verification.Specifications
import AvlVerification

namespace Implementation

open Primitives
open avl_verification
open Tree (AVLTree AVLTree.set)
open Specifications (OrdSpecLinearOrderEq infallible ltOfRustOrder gtOfRustOrder)

variable (T: Type) (H: avl_verification.Ord T) [DecidableEq T] [LinearOrder T] (Ospec: OrdSpecLinearOrderEq H)

@[pspec]
def AVLTreeSet.find_loop_spec
  (a: T) (t: Option (AVLNode T)):
  BST.Invariant t ->  b, AVLTreeSet.find_loop _ H a t = Result.ok b  (b  a  AVLTree.set t) := fun Hbst => by
  rewrite [AVLTreeSet.find_loop]
  match t with 
  | none => use false; simp [AVLTree.set]; tauto
  | some (AVLNode.mk b left right) =>
    dsimp only
    have :  a b,  o, H.cmp a b = .ok o := infallible H
    progress keep Hordering as  ordering 
    cases ordering
    all_goals dsimp only
    . convert (AVLTreeSet.find_loop_spec a right (BST.right Hbst)) using 4
      apply Iff.intro
      -- We apply a localization theorem here.
      . intro Hmem; exact (BST.right_pos Hbst Hmem (ltOfRustOrder _ _ _ Hordering))
      . intro Hmem; simp [AVLTree.set_some]; right; assumption
    . simp [Ospec.equivalence _ _ Hordering]
    . convert (AVLTreeSet.find_loop_spec a left (BST.left Hbst)) using 4
      apply Iff.intro
      -- We apply a localization theorem here.
      . intro Hmem; exact (BST.left_pos Hbst Hmem (gtOfRustOrder _ _ _ Hordering))
      . intro Hmem; simp [AVLTree.set_some]; left; right; assumption


def AVLTreeSet.find_spec
  (a: T) (t: AVLTreeSet T):
  BST.Invariant t.root ->  b, t.find _ H a = Result.ok b  (b  a  AVLTree.set t.root) := fun Hbst => by
    rw [AVLTreeSet.find]
    progress; simp only [Result.ok.injEq, exists_eq_left']; assumption

end Implementation