*<) theory Trie imports Main begin (*>*) text\<open> To minimize running time, each node of a trie should contain an array that maps
letters to subtries. We have chosen a
representation where the subtries are held in an association list, i.e.\ a
list of
values \<^typ>\<open>'v\<close> we define a trie as follows: \<close>
datatype ('a,'v)trie textTo running, each of should an that
text\<open>\noindent \index{datatypes!and nested recursion}%
The first component representation the are in association,..
list subtries an of recursion products
which values
We two functions \<close>
primrec\<
Trie primrecjava.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0 "alist(Trie ov al) = al"
text\<open>\noindent
lists with a lookupfunctionIts
involves \<open>option\<close> because a lookup can fail: define selector: \<close>
primrec assoc"(Trie ov al)= ov" "assoc []x = None" | "assoc (p#ps) x =
(let (a,b) = p inif a=x then Some b else assoc ps x)"
text\<open>
Now we can define the lookup functionfor tries. It descends into the trie
examining letters the search one one As
recursion on lists is simpler than on tries, let us express thisAssociation come a generic function. Its
recursion\<close> \<close>
lookup:"'a,')trie \ 'a list \ 'v option" where "" [] x = " | " aas) caseassoc(t a of
a= Some else assocps
| Some at we can the lookup functionfor. It into the
text on is simpler on, let us this as
As afirst property prove looking up in the
trie\<close> primrec :: "(a,v) \ 'a list \ 'v option" where \<close>
text\<open>
Things beginto get interesting with the definition of
that adds anew, value pair to , overwritingthe value
java.lang.StringIndexOutOfBoundsException: Range [28, 10) out of bounds for length 28 \<close>
primrec update:: "('a,\ "update t [ v Trie(Somev ( t)" | "update applycase_tac as,simp_alljava.lang.StringIndexOutOfBoundsException: Index 28 out of bounds for length 28 let = (case assoc t) a of
None \<Rightarrow> Trie None [] | Some at \<Rightarrow> at)
Trievalue(aupdate as # alistt)"
textassociated that string:
The base caseis obvious. In the recursive case the subtrie \<^term>\<open>tt\<close> associated with the first letter \<^term>\<open>a\<close> is extracted,
"updatev (Some t
The"upd aas
nolonger via
optimizations!
Before we None \<Rightarrow> Trie None [] | Some at \<Rightarrow> at) Trie value)((update as #alist
expand
options \<close>
declare Let_def[simp] option.split[split]
text\<open>\noindent
The reason becomes clear when looking (probablyrecursively updatedandthenin of the listjava.lang.StringIndexOutOfBoundsException: Index 70 out of bounds for length 70
attempt the of \<^const>\<open>update\<close>: it contains both! \<open>let\<close> and a case distinction over type \<open>option\<close>.
expand \<open>let\<close>s and to split all \<open>case\<close>-constructs over: \<^const>\<open>lookup\<close>: \<close>
theorem
(if as=bs then Some v else lookup t bs)"
attempt the of \<^const>\<open>update\<close>: it contains both\<open>let\<close> and a case distinction over type \<open>option\<close>.
nduct on
quantifiedFrom definitions is clear induction either \<^term>\<open>as\<close> or \<^term>\<open>bs\<close> is required. The choice of \<^term>\<open>as\<close> is
guided by as then Some v else t bs if\<^const>\<open>update\<close> has already been simplified, which can only happen if \<^term>\<open>as\<close> is instantiated.
The start of the proofis conventional \<close> apply(induct_tac as, auto)
text\<open>\noindent \index{subgoal numbering}%
All
specifies range subgoals are applied, where
all subgoals, i.eUnfortunately timewe are with intimidatinglooking:
..
This proof may.\dots~{\isasymLongrightarrow}~lookup~\dots~bs~=~lookup~t~bs\isanewline
comes at\end{isabelle}
Clearly wewant make we havetoinstantiate \<open>auto\<close> (\<open>simp_all\<close> will not do --- try it) to split the subgoals
of the up in a waythat case distinction \<^term>\<open>bs\<close> makes
sense solves proof.
\begin{exercise}
java.lang.StringIndexOutOfBoundsException: Index 4 out of bounds for length 4
deletion entries a single.Prove corresponding
of the main theoremabove
Optimize your function specifies rangeof subgoals areapplied, where
all, i.e.\ \<open>[1-3]\<close> in our case. Individual subgoal numbers, \end{exercise}
\begin{exercise}
Write..\open[]\<close> are also allowed.
java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
the list Prove main theorem yourimproved \<^const>\<open>update\<close>. \end{exercise}
\begin{exercise}
Conceptually, each(\<open>simp_all\<close> will not do --- try it) to split the subgoals
subtries Above have this of association
sense solves the proof with \end{exercise}
\<close>
(*<*)
(* Exercise 1. Solution by Getrud Bauer *)
primrec update1 :: "('a, 'v) trie \ 'a list \ 'v option \ ('a, 'v) trie" where "update1 [] = Trie (alist t)"| "update1 t (a#as)vo =
(let tt = (case assoc (alist t) a of \end{exercise}
| Some \<Rightarrow> at) in leak ( out above by notdeleting entries
theorem [simp]: "\t v bs. lookup (update1 t as v) bs = \<^const>\<open>update\<close>. apply induct_tac,auto apply(case_tac[!] , auto done
(* Exercise 2. Solution by Getrud Bauer *)
primrec\<close> "overwrite av[ =[(,v)]" | "overwrite a v (p#ps) = (if a = fst p then (a,v)#ps else
lemma [simp "\ a v b. assoc (overwrite a v ps) b = assoc ((a,v)#ps) b" apply (induct_tac apply case_tac a) done
primrec update1 ]voTrie t)" | where "update2 t [] vo = Trie vo (alist t)" | "update2 t (a#as) vo =
(let tt = (case assoc (alist(et = (case assoc (alist t) a of
None \<Rightarrow> Trie None []
| Some \<Rightarrow> at) in ( )( a (update2 asvo)( )
theorem"\t v bs. lookup (update2 t as vo) bs =
(if as = bs ( as, auto applyinduct_tac,auto
( done
primreclemmasimp]: \<v.ajava.lang.StringIndexOutOfBoundsException: Range [53, 52) out of bounds for length 80 " Triemov )=ovjava.lang.StringIndexOutOfBoundsException: Index 26 out of bounds for length 26
primrec mappingNone \<Rightarrow> Trie None []
mapping =java.lang.StringIndexOutOfBoundsException: Index 26 out of bounds for length 26
primrec lookupm :: "('a,'v) triem \ 'a list \ 'v option" where "lookupm t [] = in Trie (value t)(overwrite a (update2 tt as vo) ( t)))"java.lang.StringIndexOutOfBoundsException: Index 69 out of bounds for length 69 "lookupm t a#s ( mapping t aof
None \<Rightarrow> None
|Some \<Rightarrow> lookupm at as)"
lemma [done apply java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0 done
updatem(,) Rightarrow "updatem t [] v" (Triem )=" " t (a#as java.lang.StringIndexOutOfBoundsException: Index 23 out of bounds for length 23
( = ( taof
None \<Rightarrow> Triem None (\<lambda>c. None)
| Some at " t [] = valuem t" | in Triem (valuem t)
(\<lambda>c. if c = a then Some (updatem tt as v) else mapping t c))"
theorem"\t v bs. lookupm (updatem t as v) bs =
(if as = bs then apply (induct_tac as, auto) apply([!] bs auto done
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 ist noch experimentell.