text‹Invariant: tries are fully shrunk:› fun invar where "invar Lf = True" | "invar (Nd b (l,r)) = (invar l ∧ invar r ∧ (l = Lf ∧ r = Lf ⟶ b))"
lemma insert_Lf: "insert xs t ≠ Lf" using insert.elims by blast
lemma invar_insert: "invar t ==> invar(insert xs t)" proof(induction xs t rule: insert.induct) case 1 thus ?caseby simp next case (2 b lr) thus ?caseby(cases lr; simp) next case (3 k ks) thus ?caseby(simp; cases ks; auto) next case (4 k ks b lr) thenshow ?caseby(cases lr; auto simp: insert_Lf) qed
lemma invar_delete: "invar t ==> invar(delete xs t)" proof(induction t arbitrary: xs) case Lf thus ?caseby simp next case (Nd b lr) thus ?caseby(cases lr)(auto split: list.split) qed
interpretation S: Set where empty = empty and isin = isin and insert = insert and delete = delete and set = set_trie and invar = invar unfolding Set_def by (smt (verit, best) Tries_Binary.empty_def invar.simps(1) invar_delete invar_insert set_trie_delete set_trie_empty set_trie_insert set_trie_isin)
text‹Fully shrunk:› fun invarP where "invarP LfP = True" | "invarP (NdP ps b (l,r)) = (invarP l ∧ invarP r ∧ (l = LfP ∨ r = LfP ⟶ b))"
fun isinP :: "trieP ==> bool list ==> bool"where "isinP LfP ks = False" | "isinP (NdP ps b lr) ks = (let n = length ps in if ps = take n ks then case drop n ks of [] ==> b | k#ks' ==> isinP (sel2 k lr) ks' else False)"
definition emptyP :: trieP where
[simp]: "emptyP = LfP"
fun lcp :: "'a list ==> 'a list ==> 'a list × 'a list × 'a list"where "lcp [] ys = ([],[],ys)" | "lcp xs [] = ([],xs,[])" | "lcp (x#xs) (y#ys) = (if x≠y then ([],x#xs,y#ys) else let (ps,xs',ys') = lcp xs ys in (x#ps,xs',ys'))"
lemma mod2_cong[fundef_cong]: "[ lr = lr'; k = k'; ∧a b. lr'=(a,b) ==> f (a) = f' (a) ; ∧a b. lr'=(a,b) ==> f (b) = f' (b) ] ==> mod2 f k lr= mod2 f' k' lr'" by(cases lr, cases lr', auto)
fun insertP :: "bool list ==> trieP ==> trieP"where "insertP ks LfP = NdP ks True (LfP,LfP)" | "insertP ks (NdP ps b lr) = (case lcp ks ps of (qs, k#ks', p#ps') ==> let tp = NdP ps' b lr; tk = NdP ks' True (LfP,LfP) in NdP qs False (if k then (tp,tk) else (tk,tp)) | (qs, k#ks', []) ==> NdP ps b (mod2 (insertP ks') k lr) | (qs, [], p#ps') ==> let t = NdP ps' b lr in NdP qs True (if p then (LfP,t) else (t,LfP)) | (qs,[],[]) ==> NdP ps True lr)"
text‹Smart constructor that shrinks:› definition nodeP :: "bool list ==> bool ==> trieP * trieP ==> trieP"where "nodeP ps b lr = (if b then NdP ps b lr else case lr of (LfP,LfP) ==> LfP | (LfP, NdP ks b lr) ==> NdP (ps @ True # ks) b lr | (NdP ks b lr, LfP) ==> NdP (ps @ False # ks) b lr | _ ==> NdP ps b lr)"
fun deleteP :: "bool list ==> trieP ==> trieP"where "deleteP ks LfP = LfP" | "deleteP ks (NdP ps b lr) = (case lcp ks ps of (_, _, _#_) ==> NdP ps b lr | (_, k#ks', []) ==> nodeP ps b (mod2 (deleteP ks') k lr) | (_, [], []) ==> nodeP ps False lr)"
subsubsection ‹Functional Correctness›
text‹First step: @{typ trieP} implements @{typ trie} via the abstraction function ‹abs_trieP›:\<close>
fun prefix_trie :: "bool list ==> trie ==> trie"where "prefix_trie [] t = t" | "prefix_trie (k#ks) t = (let t' = prefix_trie ks t in Nd False (if k then (Lf,t') else (t',Lf)))"
fun abs_trieP :: "trieP ==> trie"where "abs_trieP LfP = Lf" | "abs_trieP (NdP ps b (l,r)) = prefix_trie ps (Nd b (abs_trieP l, abs_trieP r))"
text‹Correctness of @{const isinP}:›
lemma isin_prefix_trie: "isin (prefix_trie ps t) ks = (ps = take (length ps) ks ∧ isin t (drop (length ps) ks))" by (induction ps arbitrary: ks) (auto split: list.split)
lemma abs_trieP_isinP: "isinP t ks = isin (abs_trieP t) ks" proof (induction t arbitrary: ks rule: abs_trieP.induct) qed (auto simp: isin_prefix_trie split: list.split)
text‹Correctness of @{const insertP}:›
lemma prefix_trie_Lfs: "prefix_trie ks (Nd True (Lf,Lf)) = insert ks Lf" by (induction ks) auto
lemma insert_prefix_trie_same: "insert ps (prefix_trie ps (Nd b lr)) = prefix_trie ps (Nd True lr)" by (induction ps) auto
lemma insert_append: "insert (ks @ ks') (prefix_trie ks t) = prefix_trie ks (insert ks' t)" by (induction ks) auto
lemma prefix_trie_append: "prefix_trie (ps @ qs) t = prefix_trie ps (prefix_trie qs t)" by (induction ps) auto
lemma invarP_insertP: "invarP t ==> invarP(insertP xs t)" proof(induction t arbitrary: xs) case LfP thus ?caseby simp next case (NdP bs b lr) thenshow ?case by(cases lr)(auto simp: insertP_LfP split: prod.split list.split) qed
(* Inlining this proof leads to nontermination *) lemma invarP_nodeP: "[ invarP t1; invarP t2]==> invarP (nodeP xs b (t1, t2))" by (auto simp add: nodeP_def split: trieP.split)
lemma invarP_deleteP: "invarP t ==> invarP(deleteP xs t)" proof(induction t arbitrary: xs) case LfP thus ?caseby simp next case (NdP ks b lr) thus ?caseby(cases lr)(auto simp: invarP_nodeP split: prod.split list.split) qed
interpretation SP: Set where empty = emptyP and isin = isinP and insert = insertP and delete = deleteP and set = set_trieP and invar = invarP proof (standard, goal_cases) case 1 show ?caseby (simp add: set_trieP_def set_trie_def) next case 2 show ?caseby(rule isinP_set_trieP) next case 3 thus ?caseby (auto simp: set_trieP_insertP) next case 4 thus ?caseby(auto simp: set_trieP_deleteP) next case 5 thus ?caseby(simp) next case 6 thus ?caseby(rule invarP_insertP) next case 7 thus ?caseby(rule invarP_deleteP) qed
end
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.16 Sekunden
(vorverarbeitet am 2026-04-30)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.