# HG changeset patch # User traytel # Date 1582582661 -3600 # Node ID b94053ca8d7795cf5f882f64bc4b0ccc9b3ab0ca # Parent d7ef73df3d15735dc2abc22bac441bfb9814f4c7# Parent 53fcbede7bf74db505f9bf9a12dc92ca8e5311fc merged diff -r d7ef73df3d15 -r b94053ca8d77 src/HOL/Data_Structures/AVL_Set.thy --- a/src/HOL/Data_Structures/AVL_Set.thy Mon Feb 24 21:46:45 2020 +0100 +++ b/src/HOL/Data_Structures/AVL_Set.thy Mon Feb 24 23:17:41 2020 +0100 @@ -21,13 +21,13 @@ fun avl :: "'a avl_tree \ bool" where "avl Leaf = True" | -"avl (Node l (a,h) r) = +"avl (Node l (a,n) r) = ((height l = height r \ height l = height r + 1 \ height r = height l + 1) \ - h = max (height l) (height r) + 1 \ avl l \ avl r)" + n = max (height l) (height r) + 1 \ avl l \ avl r)" fun ht :: "'a avl_tree \ nat" where "ht Leaf = 0" | -"ht (Node l (a,h) r) = h" +"ht (Node l (a,n) r) = n" definition node :: "'a avl_tree \ 'a \ 'a avl_tree \ 'a avl_tree" where "node l a r = Node l (a, max (ht l) (ht r) + 1) r" @@ -56,8 +56,8 @@ fun insert :: "'a::linorder \ 'a avl_tree \ 'a avl_tree" where "insert x Leaf = Node Leaf (x, 1) Leaf" | -"insert x (Node l (a, h) r) = (case cmp x a of - EQ \ Node l (a, h) r | +"insert x (Node l (a, n) r) = (case cmp x a of + EQ \ Node l (a, n) r | LT \ balL (insert x l) a r | GT \ balR l a (insert x r))" @@ -68,17 +68,17 @@ lemmas split_max_induct = split_max.induct[case_names Node Leaf] fun del_root :: "'a avl_tree \ 'a avl_tree" where -"del_root (Node Leaf (a,h) r) = r" | -"del_root (Node l (a,h) Leaf) = l" | -"del_root (Node l (a,h) r) = (let (l', a') = split_max l in balR l' a' r)" +"del_root (Node Leaf (a,_) r) = r" | +"del_root (Node l (a,_) Leaf) = l" | +"del_root (Node l (a,_) r) = (let (l', a') = split_max l in balR l' a' r)" lemmas del_root_cases = del_root.cases[split_format(complete), case_names Leaf_t Node_Leaf Node_Node] fun delete :: "'a::linorder \ 'a avl_tree \ 'a avl_tree" where "delete _ Leaf = Leaf" | -"delete x (Node l (a, h) r) = +"delete x (Node l (a, n) r) = (case cmp x a of - EQ \ del_root (Node l (a, h) r) | + EQ \ del_root (Node l (a, n) r) | LT \ balR (delete x l) a r | GT \ balL l a (delete x r))" @@ -217,7 +217,7 @@ "(height (insert x t) = height t \ height (insert x t) = height t + 1)" using assms proof (induction t rule: tree2_induct) - case (Node l a h r) + case (Node l a _ r) case 1 show ?case proof(cases "x = a") @@ -285,12 +285,12 @@ height x = height(fst (split_max x)) + 1" using assms proof (induct x rule: split_max_induct) - case (Node l a h r) + case Node case 1 thus ?case using Node by (auto simp: height_balL height_balL2 avl_balL split:prod.split) next - case (Node l a h r) + case (Node l a _ r) case 2 let ?r' = "fst (split_max r)" from \avl x\ Node 2 have "avl l" and "avl r" by simp_all @@ -323,9 +323,9 @@ shows "height t = height(del_root t) \ height t = height(del_root t) + 1" using assms proof (cases t rule: del_root_cases) - case (Node_Node ll ln lh lr n h rl rn rh rr) - let ?l = "Node ll (ln, lh) lr" - let ?r = "Node rl (rn, rh) rr" + case (Node_Node ll lx lh lr x h rl rx rh rr) + let ?l = "Node ll (lx, lh) lr" + let ?r = "Node rl (rx, rh) rr" let ?l' = "fst (split_max ?l)" let ?t' = "balR ?l' (snd(split_max ?l)) ?r" from \avl t\ and Node_Node have "avl ?r" by simp @@ -357,63 +357,63 @@ shows "avl(delete x t)" and "height t = (height (delete x t)) \ height t = height (delete x t) + 1" using assms proof (induct t rule: tree2_induct) - case (Node l n h r) + case (Node l a n r) case 1 show ?case - proof(cases "x = n") + proof(cases "x = a") case True with Node 1 show ?thesis by (auto simp:avl_del_root) next case False show ?thesis - proof(cases "xx\n\ show ?thesis by (auto simp add:avl_balL) + case False with Node 1 \x\a\ show ?thesis by (auto simp add:avl_balL) qed qed case 2 show ?case - proof(cases "x = n") + proof(cases "x = a") case True - with 1 have "height (Node l (n,h) r) = height(del_root (Node l (n,h) r)) - \ height (Node l (n,h) r) = height(del_root (Node l (n,h) r)) + 1" + with 1 have "height (Node l (a,n) r) = height(del_root (Node l (a,n) r)) + \ height (Node l (a,n) r) = height(del_root (Node l (a,n) r)) + 1" by (subst height_del_root,simp_all) with True show ?thesis by simp next case False show ?thesis - proof(cases "xx < n\ show ?thesis by(auto simp: balR_def) + case False with Node 1 \x < a\ show ?thesis by(auto simp: balR_def) next case True - hence "(height (balR (delete x l) n r) = height (delete x l) + 2) \ - height (balR (delete x l) n r) = height (delete x l) + 3" (is "?A \ ?B") + hence "(height (balR (delete x l) a r) = height (delete x l) + 2) \ + height (balR (delete x l) a r) = height (delete x l) + 3" (is "?A \ ?B") using Node 2 by (intro height_balR) auto thus ?thesis proof - assume ?A with \x < n\ Node 2 show ?thesis by(auto simp: balR_def) + assume ?A with \x < a\ Node 2 show ?thesis by(auto simp: balR_def) next - assume ?B with \x < n\ Node 2 show ?thesis by(auto simp: balR_def) + assume ?B with \x < a\ Node 2 show ?thesis by(auto simp: balR_def) qed qed next case False show ?thesis proof(cases "height l = height (delete x r) + 2") - case False with Node 1 \\x < n\ \x \ n\ show ?thesis by(auto simp: balL_def) + case False with Node 1 \\x < a\ \x \ a\ show ?thesis by(auto simp: balL_def) next case True - hence "(height (balL l n (delete x r)) = height (delete x r) + 2) \ - height (balL l n (delete x r)) = height (delete x r) + 3" (is "?A \ ?B") + hence "(height (balL l a (delete x r)) = height (delete x r) + 2) \ + height (balL l a (delete x r)) = height (delete x r) + 3" (is "?A \ ?B") using Node 2 by (intro height_balL) auto thus ?thesis proof - assume ?A with \\x < n\ \x \ n\ Node 2 show ?thesis by(auto simp: balL_def) + assume ?A with \\x < a\ \x \ a\ Node 2 show ?thesis by(auto simp: balL_def) next - assume ?B with \\x < n\ \x \ n\ Node 2 show ?thesis by(auto simp: balL_def) + assume ?B with \\x < a\ \x \ a\ Node 2 show ?thesis by(auto simp: balL_def) qed qed qed @@ -449,13 +449,13 @@ lemma height_invers: "(height t = 0) = (t = Leaf)" - "avl t \ (height t = Suc h) = (\ l a r . t = Node l (a,Suc h) r)" + "avl t \ (height t = Suc n) = (\ l a r . t = Node l (a,Suc n) r)" by (induction t) auto -text \Any AVL tree of height \h\ has at least \fib (h+2)\ leaves:\ +text \Any AVL tree of height \n\ has at least \fib (n+2)\ leaves:\ -lemma avl_fib_bound: "avl t \ height t = h \ fib (h+2) \ size1 t" -proof (induction h arbitrary: t rule: fib.induct) +lemma avl_fib_bound: "avl t \ height t = n \ fib (n+2) \ size1 t" +proof (induction n arbitrary: t rule: fib.induct) case 1 thus ?case by (simp add: height_invers) next case 2 thus ?case by (cases t) (auto simp: height_invers) diff -r d7ef73df3d15 -r b94053ca8d77 src/Pure/Syntax/parser.ML --- a/src/Pure/Syntax/parser.ML Mon Feb 24 21:46:45 2020 +0100 +++ b/src/Pure/Syntax/parser.ML Mon Feb 24 23:17:41 2020 +0100 @@ -128,7 +128,12 @@ if chains_member chains (from, lhs) then (SOME from, false, chains) else (SOME from, true, chains_insert (from, lhs) chains) - | _ => (NONE, false, chains_declare lhs chains)); + | _ => + let + val chains' = chains + |> chains_declare lhs + |> fold (fn Nonterminal (nt, _) => chains_declare nt | _ => I) rhs; + in (NONE, false, chains') end); (*propagate new chain in lookahead and lambdas; added_starts is used later to associate existing