view hoareBinaryTree.agda @ 623:753353a41da5

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 08 Nov 2021 21:07:41 +0900
parents a1849f24fa66
children bf27e2c7c6c5
line wrap: on
line source

module hoareBinaryTree where

open import Level renaming (zero to Z ; suc to succ)

open import Data.Nat hiding (compare)
open import Data.Nat.Properties as NatProp
open import Data.Maybe
-- open import Data.Maybe.Properties
open import Data.Empty
open import Data.List
open import Data.Product

open import Function as F hiding (const)

open import Relation.Binary
open import Relation.Binary.PropositionalEquality
open import Relation.Nullary
open import logic


_iso_ : {n : Level} {a : Set n} → ℕ → ℕ → Set
d iso d' = (¬ (suc d ≤ d')) ∧ (¬ (suc d' ≤ d))

iso-intro : {n : Level} {a : Set n} {x y : ℕ} → ¬ (suc x ≤ y) → ¬ (suc y ≤ x) → _iso_ {n} {a} x y
iso-intro = λ z z₁ → record { proj1 = z ; proj2 = z₁ }

--
--
--  no children , having left node , having right node , having both
--
data bt {n : Level} (A : Set n) : Set n where
  leaf : bt A
  node :  (key : ℕ) → (value : A) →
    (left : bt A ) → (right : bt A ) → bt A

node-key : {n : Level} {A : Set n} → bt A → Maybe ℕ
node-key (node key _ _ _) = just key
node-key _ = nothing

node-value : {n : Level} {A : Set n} → bt A → Maybe A
node-value (node _ value _ _) = just value
node-value _ = nothing

bt-depth : {n : Level} {A : Set n} → (tree : bt A ) → ℕ
bt-depth leaf = 0
bt-depth (node key value t t₁) = suc (Data.Nat._⊔_ (bt-depth t ) (bt-depth t₁ ))

find : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → (tree : bt A ) → List (bt A)
           → (next : bt A → List (bt A) → t ) → (exit : bt A → List (bt A) → t ) → t
find key leaf st _ exit = exit leaf st
find key (node key₁ v tree tree₁) st next exit with <-cmp key key₁
find key n st _ exit | tri≈ ¬a b ¬c = exit n st
find key n@(node key₁ v tree tree₁) st next _ | tri< a ¬b ¬c = next tree (n ∷ st)
find key n@(node key₁ v tree tree₁) st next _ | tri> ¬a ¬b c = next tree₁ (n ∷ st)

{-# TERMINATING #-}
find-loop : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → bt A → List (bt A)  → (exit : bt A → List (bt A) → t) → t
find-loop {n} {m} {A} {t} key tree st exit = find-loop1 tree st where
    find-loop1 : bt A → List (bt A) → t
    find-loop1 tree st = find key tree st find-loop1  exit

replaceNode : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → (value : A) → bt A → (bt A → t) → t
replaceNode k v leaf next = next (node k v leaf leaf)
replaceNode k v (node key value t t₁) next = next (node k v t t₁)

replace : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → (value : A) → bt A → List (bt A) → (next : ℕ → A → bt A → List (bt A) → t ) → (exit : bt A → t) → t
replace key value tree [] next exit = exit tree
replace key value tree (leaf ∷ st) next exit = next key value tree st 
replace key value tree (node key₁ value₁ left right ∷ st) next exit with <-cmp key key₁
... | tri< a ¬b ¬c = next key value (node key₁ value₁ tree right ) st
... | tri≈ ¬a b ¬c = next key value (node key₁ value  left right ) st
... | tri> ¬a ¬b c = next key value (node key₁ value₁ left tree ) st

{-# TERMINATING #-}
replace-loop : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → (value : A) → bt A → List (bt A)  → (exit : bt A → t) → t
replace-loop {_} {_} {A} {t} key value tree st exit = replace-loop1 key value tree st where
    replace-loop1 : (key : ℕ) → (value : A) → bt A → List (bt A) → t
    replace-loop1 key value tree st = replace key value tree st replace-loop1  exit

insertTree : {n m : Level} {A : Set n} {t : Set m} → (tree : bt A) → (key : ℕ) → (value : A) → (next : bt A → t ) → t
insertTree tree key value exit = find-loop key tree [] $ λ t st → replaceNode key value t $ λ t1 → replace-loop key value t1 st exit 

insertTest1 = insertTree leaf 1 1 (λ x → x )
insertTest2 = insertTree insertTest1 2 1 (λ x → x )

open import Data.Unit hiding ( _≟_ ;  _≤?_ ; _≤_)

data treeInvariant {n : Level} {A : Set n} : (tree : bt A) → Set n where
    t-leaf : treeInvariant leaf 
    t-single : {key : ℕ} → {value : A} →  treeInvariant (node key value leaf leaf) 
    t-right : {key key₁ : ℕ} → {value value₁ : A} → {t₁ t₂ : bt A} → (key < key₁) → treeInvariant (node key₁ value₁ t₁ t₂)  → treeInvariant (node key value leaf (node key₁ value₁ t₁ t₂)) 
    t-left  : {key key₁ : ℕ} → {value value₁ : A} → {t₁ t₂ : bt A} → (key₁ < key) → treeInvariant (node key value₁ t₁ t₂)  → treeInvariant (node key₁ value₁ (node key value₁ t₁ t₂) leaf ) 
    t-node  : {key key₁ key₂ : ℕ} → {value value₁ value₂ : A} → {t₁ t₂ t₃ t₄ : bt A} → (key < key₁) → (key₁ < key₂)
       → treeInvariant (node key value t₁ t₂) 
       → treeInvariant (node key₂ value₂ t₃ t₄)
       → treeInvariant (node key₁ value₁ (node key value t₁ t₂) (node key₂ value₂ t₃ t₄)) 

treeInvariantTest1  : treeInvariant (node 3 0 leaf (node 1 1 leaf (node 3 5 leaf leaf)))
treeInvariantTest1  = {!!}

data stackInvariant {n : Level} {A : Set n} : (tree tree0 : bt A) → (stack  : List (bt A)) → Set n where
    s-nil : stackInvariant  leaf leaf [] 
    s-single : (tree : bt A) → stackInvariant tree tree (tree ∷ [] ) 
    s-<      : (tree0 tree : bt A) → {key : ℕ } → {value : A } { left  : bt A} → {st : List (bt A)}
         → stackInvariant (node key value left tree ) tree0 (node key value left tree ∷ st )  → stackInvariant tree tree0 (tree  ∷ node key value left tree ∷ st ) 
    s->      : (tree0 tree : bt A) → {key : ℕ } → {value : A } { right  : bt A} → {st : List (bt A)}
         → stackInvariant (node key value tree right ) tree0 (node key value tree right ∷ st )  → stackInvariant tree tree0 (tree  ∷ node key value tree right ∷ st ) 

data replacedTree  {n : Level} {A : Set n} (key : ℕ) (value : A)  : (tree tree1 : bt A ) → Set n where
    r-leaf : replacedTree key value leaf (node key value leaf leaf)
    r-node : {value₁ : A} → {t t₁ : bt A} → replacedTree key value (node key value₁ t t₁) (node key value t t₁) 
    r-right : {k : ℕ } {v : A} → {t t1 t2 : bt A}
          → k > key → ( replacedTree key value t1 t2 →  replacedTree key value (node k v t t1) (node k v t t2) )
    r-left : {k : ℕ } {v : A} → {t t1 t2 : bt A}
          → k < key → ( replacedTree key value t1 t2 →  replacedTree key value (node k v t1 t) (node k v t2 t) )

findP : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → (tree tree0 : bt A ) → (stack : List (bt A))
           →  treeInvariant tree ∧ stackInvariant tree tree0 stack  
           → (next : (tree1 tree0 : bt A) → (stack : List (bt A)) → treeInvariant tree1 ∧ stackInvariant tree1 tree0 stack → bt-depth tree1 < bt-depth tree   → t )
           → (exit : (tree1 tree0 : bt A) → (stack : List (bt A)) → treeInvariant tree1 ∧ stackInvariant tree1 tree0 stack → t ) → t
findP key leaf tree0 st Pre _ exit = exit leaf tree0 st {!!}
findP key (node key₁ v tree tree₁) tree0 st Pre next exit with <-cmp key key₁
findP key n tree0 st Pre _ exit | tri≈ ¬a b ¬c = exit n tree0 st {!!}
findP key n@(node key₁ v tree tree₁) tree0 st Pre next _ | tri< a ¬b ¬c = next tree tree0 (n ∷ st) {!!} {!!}
findP key n@(node key₁ v tree tree₁) tree0 st Pre next _ | tri> ¬a ¬b c = next tree₁ tree0 (n ∷ st) {!!} {!!}

replaceNodeP : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → (value : A) → (tree : bt A) → (treeInvariant tree )
    → ((tree1 : bt A) → treeInvariant tree1 →  replacedTree key value tree tree1 → t) → t
replaceNodeP k v leaf P next = next (node k v leaf leaf) {!!} {!!} 
replaceNodeP k v (node key value t t₁) P next = next (node k v t t₁) {!!} {!!}

replaceP : {n m : Level} {A : Set n} {t : Set m}
     → (key : ℕ) → (value : A) → (tree repl : bt A) → (stack : List (bt A)) → treeInvariant tree ∧ stackInvariant repl tree stack ∧ replacedTree key value tree repl
     → (next : ℕ → A → (tree1 repl : bt A) → (stack : List (bt A)) → treeInvariant tree1 ∧ stackInvariant repl tree1 stack ∧ replacedTree key value tree1 repl → bt-depth tree1 < bt-depth tree   → t )
     → (exit : (tree1 repl : bt A) → treeInvariant tree1 ∧ replacedTree key value tree1 repl → t) → t
replaceP key value tree repl [] Pre next exit = exit tree repl {!!} 
replaceP key value tree repl (leaf ∷ st) Pre next exit = next key value tree {!!} st {!!} {!!}
replaceP key value tree repl (node key₁ value₁ left right ∷ st) Pre next exit with <-cmp key key₁
... | tri< a ¬b ¬c = next key value (node key₁ value₁ tree right ) {!!} st {!!}  {!!}
... | tri≈ ¬a b ¬c = next key value (node key₁ value  left right ) {!!} st {!!}  {!!}
... | tri> ¬a ¬b c = next key value (node key₁ value₁ left tree ) {!!} st {!!}  {!!}

open import Relation.Binary.Definitions

nat-≤> : { x y : ℕ } → x ≤ y → y < x → ⊥
nat-≤>  (s≤s x<y) (s≤s y<x) = nat-≤> x<y y<x
lemma3 : {i j : ℕ} → 0 ≡ i → j < i → ⊥
lemma3 refl ()
lemma5 : {i j : ℕ} → i < 1 → j < i → ⊥
lemma5 (s≤s z≤n) ()

TerminatingLoopS : {l m : Level} {t : Set l} (Index : Set m ) → {Invraiant : Index → Set m } → ( reduce : Index → ℕ)
   → (r : Index) → (p : Invraiant r)  
   → (loop : (r : Index)  → Invraiant r → (next : (r1 : Index)  → Invraiant r1 → reduce r1 < reduce r  → t ) → t) → t
TerminatingLoopS {_} {_} {t} Index {Invraiant} reduce r p loop with <-cmp 0 (reduce r)
... | tri≈ ¬a b ¬c = loop r p (λ r1 p1 lt → ⊥-elim (lemma3 b lt) ) 
... | tri< a ¬b ¬c = loop r p (λ r1 p1 lt1 → TerminatingLoop1 (reduce r) r r1 (≤-step lt1) p1 lt1 ) where 
    TerminatingLoop1 : (j : ℕ) → (r r1 : Index) → reduce r1 < suc j  → Invraiant r1 →  reduce r1 < reduce r → t
    TerminatingLoop1 zero r r1 n≤j p1 lt = loop r1 p1 (λ r2 p1 lt1 → ⊥-elim (lemma5 n≤j lt1)) 
    TerminatingLoop1 (suc j) r r1  n≤j p1 lt with <-cmp (reduce r1) (suc j)
    ... | tri< a ¬b ¬c = TerminatingLoop1 j r r1 a p1 lt 
    ... | tri≈ ¬a b ¬c = loop r1 p1 (λ r2 p2 lt1 → TerminatingLoop1 j r1 r2 (subst (λ k → reduce r2 < k ) b lt1 ) p2 lt1 )
    ... | tri> ¬a ¬b c =  ⊥-elim ( nat-≤> c n≤j )   

open _∧_

RTtoTI0  : {n : Level} {A : Set n}  → (tree repl : bt A) → (key : ℕ) → (value : A) → treeInvariant tree
     → replacedTree key value tree repl → treeInvariant repl
RTtoTI0  = {!!}

RTtoTI1  : {n : Level} {A : Set n}  → (tree repl : bt A) → (key : ℕ) → (value : A) → treeInvariant repl
     → replacedTree key value tree repl → treeInvariant tree
RTtoTI1  = {!!}

insertTreeP : {n m : Level} {A : Set n} {t : Set m} → (tree : bt A) → (key : ℕ) → (value : A) → treeInvariant tree
     → (exit : (tree repl : bt A) → treeInvariant tree ∧ replacedTree key value tree repl → t ) → t
insertTreeP {n} {m} {A} {t} tree key value P exit =
   TerminatingLoopS (bt A ∧ List (bt A) ) {λ p → treeInvariant (proj1 p) ∧ stackInvariant (proj1 p) tree (proj2 p) } (λ p → bt-depth (proj1 p)) ⟪ tree , [] ⟫  ⟪ P , {!!}  ⟫
       $ λ p P loop → findP key (proj1 p)  tree (proj2 p) {!!} (λ t _ s P1 lt → loop ⟪ t ,  s  ⟫ {!!} lt )
       $ λ t _ s P → replaceNodeP key value t (proj1 P)
       $ λ t1 P1 R → TerminatingLoopS (List (bt A) ∧ (bt A ∧ bt A ))
            {λ p → treeInvariant (proj1 (proj2 p)) ∧ stackInvariant (proj1 (proj2 p)) tree (proj1 p)  ∧ replacedTree key value (proj1 (proj2 p)) (proj2 (proj2 p)) }
               (λ p → bt-depth (proj1 (proj2 p))) ⟪ s , ⟪ t , t1 ⟫ ⟫ ⟪ proj1 P , ⟪ {!!}  , R ⟫ ⟫
       $  λ p P1 loop → replaceP key value (proj1 (proj2 p)) (proj2 (proj2 p)) (proj1 p) {!!}
            (λ key value tree1 repl1 stack P2 lt → loop ⟪ stack , ⟪ tree1  , repl1  ⟫ ⟫ {!!} lt )  exit 

top-value : {n : Level} {A : Set n} → (tree : bt A) →  Maybe A 
top-value leaf = nothing
top-value (node key value tree tree₁) = just value

insertTreeSpec0 : {n : Level} {A : Set n} → (tree : bt A) → (value : A) → top-value tree ≡ just value → ⊤
insertTreeSpec0 _ _ _ = tt

record findPR {n : Level} {A : Set n} (tree : bt A ) (stack : List (bt A)) (C : bt A → List (bt A) → Set n) : Set n where
   field
     tree0 : bt A
     ti : treeInvariant tree0
     si : stackInvariant tree tree0 stack
     ci : C tree stack
   
findPP : {n m : Level} {A : Set n} {t : Set m}
           → (key : ℕ) → (tree : bt A ) → (stack : List (bt A))
           → (C : bt A → List (bt A) → Set n ) (Pre :  findPR tree stack {!!} )
           → (next : (tree1 : bt A) → (stack1 : List (bt A)) → findPR tree1 stack1 {!!} →  bt-depth tree1 < bt-depth tree   → t )
           → (exit : (tree1 : bt A) → (stack1 : List (bt A)) → findPR tree1 stack1 {!!} → t) → t
findPP key leaf st C Pre next exit = exit leaf st Pre  
findPP key (node key₁ v tree tree₁) st C Pre next exit with <-cmp key key₁
findPP key n st C P next exit | tri≈ ¬a b ¬c = exit n st P 
findPP {_} {_} {A} key n@(node key₁ v tree tree₁) st C Pre next exit | tri< a ¬b ¬c =
          next tree (n ∷ st) (record {ti = findPR.ti Pre  ; si = findPP2 st (findPR.si Pre) ; ci = ?} ) findPP1 where 
    tree0 =  findPR.tree0 Pre 
    findPP2 : (st : List (bt A)) → stackInvariant {!!} tree0 st →  stackInvariant {!!} tree0 (node key₁ v tree tree₁ ∷ st)
    findPP2 = {!!}
    findPP1 : suc ( bt-depth tree ) ≤ suc (bt-depth tree Data.Nat.⊔ bt-depth tree₁)
    findPP1 =  {!!}
findPP key n@(node key₁ v tree tree₁) st C Pre next exit | tri> ¬a ¬b c = next tree₁ (n ∷ st) {!!} findPP2 where -- Cond n st → Cond tree₁ (n ∷ st)
    findPP2 : suc (bt-depth tree₁) ≤ suc (bt-depth tree Data.Nat.⊔ bt-depth tree₁)
    findPP2 = {!!}

insertTreePP : {n m : Level} {A : Set n} {t : Set m} → (tree : bt A) → (key : ℕ) → (value : A) → treeInvariant tree
     → (exit : (tree repl : bt A) → treeInvariant tree ∧ replacedTree key value tree repl → t ) → t
insertTreePP {n} {m} {A} {t} tree key value P exit =
   TerminatingLoopS (bt A ∧ List (bt A) ) {λ p → findPR (proj1 p) (proj2 p) {!!} } (λ p → bt-depth (proj1 p)) ⟪ tree , [] ⟫  {!!}
       $ λ p P loop → findPP key (proj1 p) (proj2 p) {!!} (λ t s P1 lt → loop ⟪ t ,  s  ⟫ {!!} lt )
       $ λ t s P → replaceNodeP key value t {!!}
       $ λ t1 P1 R → TerminatingLoopS (List (bt A) ∧ (bt A ∧ bt A ))
            {λ p → treeInvariant (proj1 (proj2 p)) ∧ stackInvariant (proj1 (proj2 p)) tree (proj1 p)  ∧ replacedTree key value (proj1 (proj2 p)) (proj2 (proj2 p)) }
               (λ p → bt-depth (proj1 (proj2 p))) ⟪ s , ⟪ t , t1 ⟫ ⟫ ⟪ {!!} , ⟪ {!!}  , R ⟫ ⟫
       $  λ p P1 loop → replaceP key value (proj1 (proj2 p)) (proj2 (proj2 p)) (proj1 p) {!!}
            (λ key value tree1 repl1 stack P2 lt → loop ⟪ stack , ⟪ tree1  , repl1  ⟫ ⟫ {!!} lt )  exit 

-- findP key tree stack = findPP key tree stack {findPR} → record { ti = tree-invariant tree ; si stack-invariant tree stack } → 

record findP-contains {n : Level} {A : Set n} (tree : bt A ) (stack : List (bt A)) : Set n where
   field
     key1 : ℕ
     value1 : A
     tree1 : bt A
     ci : replacedTree key1 value1 tree tree1
   
containsTree : {n m : Level} {A : Set n} {t : Set m} → (tree tree1 : bt A) → (key : ℕ) → (value : A) → treeInvariant tree1 → replacedTree key value tree1 tree  → ⊤
containsTree {n} {m} {A} {t} tree tree1 key value P RT =
   TerminatingLoopS (bt A ∧ List (bt A) )
     {λ p → findPR (proj1 p) (proj2 p) {!!} ∧ findP-contains (proj1 p) (proj2 p)} (λ p → bt-depth (proj1 p))
              ⟪ tree1 , []  ⟫ ⟪ {!!} , record { key1 = key ; value1 = value ; tree1 = tree ; ci = RT ; R = record { tree0 = {!!} ; ti = P ; si = lift tt } } ⟫
       $ λ p P loop → findPP key (proj1 p) (proj2 p) (proj1 P) (λ t s P1 lt → loop ⟪ t , s ⟫ ⟪ P1 , {!!} ⟫ lt ) 
       $ λ t1 s1 P2 → insertTreeSpec0 t1 value {!!}