| 23449 |      1 | (*  Title:      HOL/MetisTest/BT.thy
 | 
|  |      2 |     ID:         $Id$
 | 
|  |      3 |     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
 | 
|  |      4 | 
 | 
|  |      5 | Testing the metis method
 | 
|  |      6 | *)
 | 
|  |      7 | 
 | 
|  |      8 | header {* Binary trees *}
 | 
|  |      9 | 
 | 
|  |     10 | theory BT imports Main begin
 | 
|  |     11 | 
 | 
|  |     12 | 
 | 
|  |     13 | datatype 'a bt =
 | 
|  |     14 |     Lf
 | 
|  |     15 |   | Br 'a  "'a bt"  "'a bt"
 | 
|  |     16 | 
 | 
|  |     17 | consts
 | 
|  |     18 |   n_nodes   :: "'a bt => nat"
 | 
|  |     19 |   n_leaves  :: "'a bt => nat"
 | 
|  |     20 |   depth     :: "'a bt => nat"
 | 
|  |     21 |   reflect   :: "'a bt => 'a bt"
 | 
|  |     22 |   bt_map    :: "('a => 'b) => ('a bt => 'b bt)"
 | 
|  |     23 |   preorder  :: "'a bt => 'a list"
 | 
|  |     24 |   inorder   :: "'a bt => 'a list"
 | 
|  |     25 |   postorder :: "'a bt => 'a list"
 | 
|  |     26 |   appnd    :: "'a bt => 'a bt => 'a bt"
 | 
|  |     27 | 
 | 
|  |     28 | primrec
 | 
|  |     29 |   "n_nodes Lf = 0"
 | 
|  |     30 |   "n_nodes (Br a t1 t2) = Suc (n_nodes t1 + n_nodes t2)"
 | 
|  |     31 | 
 | 
|  |     32 | primrec
 | 
|  |     33 |   "n_leaves Lf = Suc 0"
 | 
|  |     34 |   "n_leaves (Br a t1 t2) = n_leaves t1 + n_leaves t2"
 | 
|  |     35 | 
 | 
|  |     36 | primrec
 | 
|  |     37 |   "depth Lf = 0"
 | 
|  |     38 |   "depth (Br a t1 t2) = Suc (max (depth t1) (depth t2))"
 | 
|  |     39 | 
 | 
|  |     40 | primrec
 | 
|  |     41 |   "reflect Lf = Lf"
 | 
|  |     42 |   "reflect (Br a t1 t2) = Br a (reflect t2) (reflect t1)"
 | 
|  |     43 | 
 | 
|  |     44 | primrec
 | 
|  |     45 |   "bt_map f Lf = Lf"
 | 
|  |     46 |   "bt_map f (Br a t1 t2) = Br (f a) (bt_map f t1) (bt_map f t2)"
 | 
|  |     47 | 
 | 
|  |     48 | primrec
 | 
|  |     49 |   "preorder Lf = []"
 | 
|  |     50 |   "preorder (Br a t1 t2) = [a] @ (preorder t1) @ (preorder t2)"
 | 
|  |     51 | 
 | 
|  |     52 | primrec
 | 
|  |     53 |   "inorder Lf = []"
 | 
|  |     54 |   "inorder (Br a t1 t2) = (inorder t1) @ [a] @ (inorder t2)"
 | 
|  |     55 | 
 | 
|  |     56 | primrec
 | 
|  |     57 |   "postorder Lf = []"
 | 
|  |     58 |   "postorder (Br a t1 t2) = (postorder t1) @ (postorder t2) @ [a]"
 | 
|  |     59 | 
 | 
|  |     60 | primrec
 | 
|  |     61 |   "appnd Lf t = t"
 | 
|  |     62 |   "appnd (Br a t1 t2) t = Br a (appnd t1 t) (appnd t2 t)"
 | 
|  |     63 | 
 | 
|  |     64 | 
 | 
|  |     65 | text {* \medskip BT simplification *}
 | 
|  |     66 | 
 | 
|  |     67 | ML {*ResAtp.problem_name := "BT__n_leaves_reflect"*}
 | 
|  |     68 | lemma n_leaves_reflect: "n_leaves (reflect t) = n_leaves t"
 | 
|  |     69 |   apply (induct t)
 | 
|  |     70 |   apply (metis add_right_cancel n_leaves.simps(1) reflect.simps(1))
 | 
|  |     71 |   apply (metis add_commute n_leaves.simps(2) reflect.simps(2))
 | 
|  |     72 |   done
 | 
|  |     73 | 
 | 
|  |     74 | ML {*ResAtp.problem_name := "BT__n_nodes_reflect"*}
 | 
|  |     75 | lemma n_nodes_reflect: "n_nodes (reflect t) = n_nodes t"
 | 
|  |     76 |   apply (induct t)
 | 
|  |     77 |   apply (metis reflect.simps(1))
 | 
|  |     78 |   apply (metis n_nodes.simps(2) nat_add_commute reflect.simps(2))
 | 
|  |     79 |   done
 | 
|  |     80 | 
 | 
|  |     81 | ML {*ResAtp.problem_name := "BT__depth_reflect"*}
 | 
|  |     82 | lemma depth_reflect: "depth (reflect t) = depth t"
 | 
|  |     83 |   apply (induct t)
 | 
|  |     84 |   apply (metis depth.simps(1) reflect.simps(1))
 | 
|  |     85 |   apply (metis depth.simps(2) min_max.less_eq_less_sup.sup_commute reflect.simps(2))
 | 
|  |     86 |   done
 | 
|  |     87 | 
 | 
|  |     88 | text {*
 | 
|  |     89 |   The famous relationship between the numbers of leaves and nodes.
 | 
|  |     90 | *}
 | 
|  |     91 | 
 | 
|  |     92 | ML {*ResAtp.problem_name := "BT__n_leaves_nodes"*}
 | 
|  |     93 | lemma n_leaves_nodes: "n_leaves t = Suc (n_nodes t)"
 | 
|  |     94 |   apply (induct t)
 | 
|  |     95 |   apply (metis n_leaves.simps(1) n_nodes.simps(1))
 | 
|  |     96 |   apply auto
 | 
|  |     97 |   done
 | 
|  |     98 | 
 | 
|  |     99 | ML {*ResAtp.problem_name := "BT__reflect_reflect_ident"*}
 | 
|  |    100 | lemma reflect_reflect_ident: "reflect (reflect t) = t"
 | 
|  |    101 |   apply (induct t)
 | 
|  |    102 |   apply (metis add_right_cancel reflect.simps(1));
 | 
|  |    103 |   apply (metis Suc_Suc_eq reflect.simps(2))
 | 
|  |    104 |   done
 | 
|  |    105 | 
 | 
|  |    106 | ML {*ResAtp.problem_name := "BT__bt_map_ident"*}
 | 
|  |    107 | lemma bt_map_ident: "bt_map (%x. x) = (%y. y)"
 | 
|  |    108 | apply (rule ext) 
 | 
|  |    109 | apply (induct_tac y)
 | 
|  |    110 |   apply (metis bt_map.simps(1))
 | 
|  |    111 | txt{*BUG involving flex-flex pairs*}
 | 
|  |    112 | (*  apply (metis bt_map.simps(2)) *)
 | 
|  |    113 | apply auto
 | 
|  |    114 | done
 | 
|  |    115 | 
 | 
|  |    116 | 
 | 
|  |    117 | ML {*ResAtp.problem_name := "BT__bt_map_appnd"*}
 | 
|  |    118 | lemma bt_map_appnd: "bt_map f (appnd t u) = appnd (bt_map f t) (bt_map f u)"
 | 
|  |    119 | apply (induct t)
 | 
|  |    120 |   apply (metis appnd.simps(1) bt_map.simps(1))
 | 
|  |    121 |   apply (metis appnd.simps(2) bt_map.simps(2))  (*slow!!*)
 | 
|  |    122 | done
 | 
|  |    123 | 
 | 
|  |    124 | 
 | 
|  |    125 | ML {*ResAtp.problem_name := "BT__bt_map_compose"*}
 | 
|  |    126 | lemma bt_map_compose: "bt_map (f o g) t = bt_map f (bt_map g t)"
 | 
|  |    127 | apply (induct t) 
 | 
|  |    128 |   apply (metis bt_map.simps(1))
 | 
|  |    129 | txt{*Metis runs forever*}
 | 
|  |    130 | (*  apply (metis bt_map.simps(2) o_apply)*)
 | 
|  |    131 | apply auto
 | 
|  |    132 | done
 | 
|  |    133 | 
 | 
|  |    134 | 
 | 
|  |    135 | ML {*ResAtp.problem_name := "BT__bt_map_reflect"*}
 | 
|  |    136 | lemma bt_map_reflect: "bt_map f (reflect t) = reflect (bt_map f t)"
 | 
|  |    137 |   apply (induct t)
 | 
|  |    138 |   apply (metis add_right_cancel bt_map.simps(1) reflect.simps(1))
 | 
|  |    139 |   apply (metis add_right_cancel bt_map.simps(2) reflect.simps(2))
 | 
|  |    140 |   done
 | 
|  |    141 | 
 | 
|  |    142 | ML {*ResAtp.problem_name := "BT__preorder_bt_map"*}
 | 
|  |    143 | lemma preorder_bt_map: "preorder (bt_map f t) = map f (preorder t)"
 | 
|  |    144 |   apply (induct t)
 | 
|  |    145 |   apply (metis bt_map.simps(1) map.simps(1) preorder.simps(1))
 | 
|  |    146 |    apply simp
 | 
|  |    147 |   done
 | 
|  |    148 | 
 | 
|  |    149 | ML {*ResAtp.problem_name := "BT__inorder_bt_map"*}
 | 
|  |    150 | lemma inorder_bt_map: "inorder (bt_map f t) = map f (inorder t)"
 | 
|  |    151 |   apply (induct t)
 | 
|  |    152 |   apply (metis bt_map.simps(1) inorder.simps(1) map.simps(1))
 | 
|  |    153 |   apply simp
 | 
|  |    154 |   done
 | 
|  |    155 | 
 | 
|  |    156 | ML {*ResAtp.problem_name := "BT__postorder_bt_map"*}
 | 
|  |    157 | lemma postorder_bt_map: "postorder (bt_map f t) = map f (postorder t)"
 | 
|  |    158 |   apply (induct t)
 | 
|  |    159 |   apply (metis bt_map.simps(1) map.simps(1) postorder.simps(1))
 | 
|  |    160 |    apply simp
 | 
|  |    161 |   done
 | 
|  |    162 | 
 | 
|  |    163 | ML {*ResAtp.problem_name := "BT__depth_bt_map"*}
 | 
|  |    164 | lemma depth_bt_map [simp]: "depth (bt_map f t) = depth t"
 | 
|  |    165 |   apply (induct t)
 | 
|  |    166 |   apply (metis bt_map.simps(1) depth.simps(1))
 | 
|  |    167 |    apply simp
 | 
|  |    168 |   done
 | 
|  |    169 | 
 | 
|  |    170 | ML {*ResAtp.problem_name := "BT__n_leaves_bt_map"*}
 | 
|  |    171 | lemma n_leaves_bt_map [simp]: "n_leaves (bt_map f t) = n_leaves t"
 | 
|  |    172 |   apply (induct t)
 | 
|  |    173 |   apply (metis One_nat_def Suc_eq_add_numeral_1 bt_map.simps(1) less_add_one less_antisym linorder_neq_iff n_leaves.simps(1))
 | 
|  |    174 |   apply (metis add_commute bt_map.simps(2) n_leaves.simps(2))
 | 
|  |    175 |   done
 | 
|  |    176 | 
 | 
|  |    177 | 
 | 
|  |    178 | ML {*ResAtp.problem_name := "BT__preorder_reflect"*}
 | 
|  |    179 | lemma preorder_reflect: "preorder (reflect t) = rev (postorder t)"
 | 
|  |    180 |   apply (induct t)
 | 
|  |    181 |   apply (metis postorder.simps(1) preorder.simps(1) reflect.simps(1) rev_is_Nil_conv)
 | 
|  |    182 |   apply (metis append_eq_append_conv2 inorder.simps(1) postorder.simps(2) preorder.simps(2) reflect.simps(2) rev_append rev_is_rev_conv rev_singleton_conv rev_swap rotate_simps)
 | 
|  |    183 |   done
 | 
|  |    184 | 
 | 
|  |    185 | ML {*ResAtp.problem_name := "BT__inorder_reflect"*}
 | 
|  |    186 | lemma inorder_reflect: "inorder (reflect t) = rev (inorder t)"
 | 
|  |    187 |   apply (induct t)
 | 
|  |    188 |   apply (metis inorder.simps(1) reflect.simps(1) rev.simps(1))
 | 
|  |    189 |   apply simp
 | 
|  |    190 |   done
 | 
|  |    191 | 
 | 
|  |    192 | ML {*ResAtp.problem_name := "BT__postorder_reflect"*}
 | 
|  |    193 | lemma postorder_reflect: "postorder (reflect t) = rev (preorder t)"
 | 
|  |    194 |   apply (induct t)
 | 
|  |    195 |   apply (metis postorder.simps(1) preorder.simps(1) reflect.simps(1) rev.simps(1))
 | 
|  |    196 |   apply (metis Cons_eq_appendI postorder.simps(2) preorder.simps(2) reflect.simps(2) rev.simps(2) rev_append rotate1_def self_append_conv2)
 | 
|  |    197 |   done
 | 
|  |    198 | 
 | 
|  |    199 | text {*
 | 
|  |    200 |  Analogues of the standard properties of the append function for lists.
 | 
|  |    201 | *}
 | 
|  |    202 | 
 | 
|  |    203 | ML {*ResAtp.problem_name := "BT__appnd_assoc"*}
 | 
|  |    204 | lemma appnd_assoc [simp]:
 | 
|  |    205 |      "appnd (appnd t1 t2) t3 = appnd t1 (appnd t2 t3)"
 | 
|  |    206 |   apply (induct t1)
 | 
|  |    207 |   apply (metis appnd.simps(1))
 | 
|  |    208 |   apply (metis appnd.simps(2))
 | 
|  |    209 |   done
 | 
|  |    210 | 
 | 
|  |    211 | ML {*ResAtp.problem_name := "BT__appnd_Lf2"*}
 | 
|  |    212 | lemma appnd_Lf2 [simp]: "appnd t Lf = t"
 | 
|  |    213 |   apply (induct t)
 | 
|  |    214 |   apply (metis appnd.simps(1))
 | 
|  |    215 |   apply (metis appnd.simps(2))
 | 
|  |    216 |   done
 | 
|  |    217 | 
 | 
|  |    218 | ML {*ResAtp.problem_name := "BT__depth_appnd"*}
 | 
|  |    219 |   declare max_add_distrib_left [simp]
 | 
|  |    220 | lemma depth_appnd [simp]: "depth (appnd t1 t2) = depth t1 + depth t2"
 | 
|  |    221 |   apply (induct t1)
 | 
|  |    222 |   apply (metis add_0 appnd.simps(1) depth.simps(1))
 | 
|  |    223 | apply (simp add: ); 
 | 
|  |    224 |   done
 | 
|  |    225 | 
 | 
|  |    226 | ML {*ResAtp.problem_name := "BT__n_leaves_appnd"*}
 | 
|  |    227 | lemma n_leaves_appnd [simp]:
 | 
|  |    228 |      "n_leaves (appnd t1 t2) = n_leaves t1 * n_leaves t2"
 | 
|  |    229 |   apply (induct t1)
 | 
|  |    230 |   apply (metis One_nat_def appnd.simps(1) less_irrefl less_linear n_leaves.simps(1) nat_mult_1) 
 | 
|  |    231 |   apply (simp add: left_distrib)
 | 
|  |    232 |   done
 | 
|  |    233 | 
 | 
|  |    234 | ML {*ResAtp.problem_name := "BT__bt_map_appnd"*}
 | 
|  |    235 | lemma bt_map_appnd:
 | 
|  |    236 |      "bt_map f (appnd t1 t2) = appnd (bt_map f t1) (bt_map f t2)"
 | 
|  |    237 |   apply (induct t1)
 | 
|  |    238 |   apply (metis appnd.simps(1) bt_map_appnd)
 | 
|  |    239 |   apply (metis bt_map_appnd)
 | 
|  |    240 |   done
 | 
|  |    241 | 
 | 
|  |    242 | end
 |