src/ZF/ex/TF.ML
changeset 515 abcc438e7c27
parent 486 6b58082796f6
child 529 f0d16216e394
equal deleted inserted replaced
514:ab2c867829ec 515:abcc438e7c27
     2     ID:         $Id$
     2     ID:         $Id$
     3     Author: 	Lawrence C Paulson, Cambridge University Computer Laboratory
     3     Author: 	Lawrence C Paulson, Cambridge University Computer Laboratory
     4     Copyright   1993  University of Cambridge
     4     Copyright   1993  University of Cambridge
     5 
     5 
     6 Trees & forests, a mutually recursive type definition.
     6 Trees & forests, a mutually recursive type definition.
       
     7 
       
     8 Still needs
       
     9 
       
    10 "TF_reflect == (%z. TF_rec(z, %x ts r. Tcons(x,r), 0, 
       
    11                %t ts r1 r2. TF_of_list(list_of_TF(r2) @ <r1,0>)))"
     7 *)
    12 *)
     8 
    13 
     9 structure TF = Datatype_Fun
    14 open TF;
    10  (val thy        = Univ.thy
    15 
    11   val thy_name   = "TF"
    16 val [TconsI, FnilI, FconsI] = tree_forest.intrs;
    12   val rec_specs  = [("tree", "univ(A)",
       
    13                        [(["Tcons"],  "[i,i]=>i",  NoSyn)]),
       
    14                     ("forest", "univ(A)",
       
    15                        [(["Fnil"],   "i",         NoSyn),
       
    16                         (["Fcons"],  "[i,i]=>i",  NoSyn)])]
       
    17   val rec_styp   = "i=>i"
       
    18   val sintrs     = 
       
    19           ["[| a:A;  f: forest(A) |] ==> Tcons(a,f) : tree(A)",
       
    20            "Fnil : forest(A)",
       
    21            "[| t: tree(A);  f: forest(A) |] ==> Fcons(t,f) : forest(A)"]
       
    22   val monos      = []
       
    23   val type_intrs = datatype_intrs
       
    24   val type_elims = datatype_elims);
       
    25 
       
    26 val [TconsI, FnilI, FconsI] = TF.intrs;
       
    27 
    17 
    28 (** tree_forest(A) as the union of tree(A) and forest(A) **)
    18 (** tree_forest(A) as the union of tree(A) and forest(A) **)
    29 
    19 
    30 goalw TF.thy (tl TF.defs) "tree(A) <= tree_forest(A)";
    20 goalw TF.thy (tl tree_forest.defs) "tree(A) <= tree_forest(A)";
    31 by (rtac Part_subset 1);
    21 by (rtac Part_subset 1);
    32 val tree_subset_TF = result();
    22 val tree_subset_TF = result();
    33 
    23 
    34 goalw TF.thy (tl TF.defs) "forest(A) <= tree_forest(A)";
    24 goalw TF.thy (tl tree_forest.defs) "forest(A) <= tree_forest(A)";
    35 by (rtac Part_subset 1);
    25 by (rtac Part_subset 1);
    36 val forest_subset_TF = result();
    26 val forest_subset_TF = result();
    37 
    27 
    38 goalw TF.thy (tl TF.defs) "tree(A) Un forest(A) = tree_forest(A)";
    28 goalw TF.thy (tl tree_forest.defs) "tree(A) Un forest(A) = tree_forest(A)";
    39 by (rtac (TF.dom_subset RS Part_sum_equality) 1);
    29 by (rtac (tree_forest.dom_subset RS Part_sum_equality) 1);
    40 val TF_equals_Un = result();
    30 val TF_equals_Un = result();
    41 
    31 
    42 (** NOT useful, but interesting... **)
    32 (** NOT useful, but interesting... **)
    43 
    33 
    44 (*The (refl RS conjI RS exI RS exI) avoids considerable search!*)
    34 (*The (refl RS conjI RS exI RS exI) avoids considerable search!*)
    45 val unfold_cs = sum_cs addSIs [PartI, refl RS conjI RS exI RS exI]
    35 val unfold_cs = sum_cs addSIs [PartI, refl RS conjI RS exI RS exI]
    46                     addIs datatype_intrs
    36                     addIs datatype_intrs
    47                     addDs [TF.dom_subset RS subsetD]
    37                     addDs [tree_forest.dom_subset RS subsetD]
    48 	            addSEs ([PartE] @ datatype_elims @ TF.free_SEs);
    38 	            addSEs ([PartE] @ datatype_elims @ tree_forest.free_SEs);
    49 
    39 
    50 goalw TF.thy (tl TF.defs)
    40 goalw TF.thy (tl tree_forest.defs)
    51     "tree_forest(A) = (A*forest(A)) + ({0} + tree(A)*forest(A))";
    41     "tree_forest(A) = (A*forest(A)) + ({0} + tree(A)*forest(A))";
    52 by (rtac (TF.unfold RS trans) 1);
    42 by (rtac (tree_forest.unfold RS trans) 1);
    53 by (rewrite_goals_tac TF.con_defs);
    43 by (rewrite_goals_tac tree_forest.con_defs);
    54 by (rtac equalityI 1);
    44 by (rtac equalityI 1);
    55 by (fast_tac unfold_cs 1);
    45 by (fast_tac unfold_cs 1);
    56 by (fast_tac unfold_cs 1);
    46 by (fast_tac unfold_cs 1);
    57 val tree_forest_unfold = result();
    47 val tree_forest_unfold = result();
    58 
    48 
    59 val tree_forest_unfold' = rewrite_rule (tl TF.defs) tree_forest_unfold;
    49 val tree_forest_unfold' = rewrite_rule (tl tree_forest.defs) tree_forest_unfold;
    60 
    50 
    61 
    51 
    62 goalw TF.thy (tl TF.defs) "tree(A) = {Inl(x). x: A*forest(A)}";
    52 goalw TF.thy (tl tree_forest.defs)
       
    53     "tree(A) = {Inl(x). x: A*forest(A)}";
    63 by (rtac (Part_Inl RS subst) 1);
    54 by (rtac (Part_Inl RS subst) 1);
    64 by (rtac (tree_forest_unfold' RS subst_context) 1);
    55 by (rtac (tree_forest_unfold' RS subst_context) 1);
    65 val tree_unfold = result();
    56 val tree_unfold = result();
    66 
    57 
    67 goalw TF.thy (tl TF.defs) "forest(A) = {Inr(x). x: {0} + tree(A)*forest(A)}";
    58 goalw TF.thy (tl tree_forest.defs)
       
    59     "forest(A) = {Inr(x). x: {0} + tree(A)*forest(A)}";
    68 by (rtac (Part_Inr RS subst) 1);
    60 by (rtac (Part_Inr RS subst) 1);
    69 by (rtac (tree_forest_unfold' RS subst_context) 1);
    61 by (rtac (tree_forest_unfold' RS subst_context) 1);
    70 val forest_unfold = result();
    62 val forest_unfold = result();
    71 
    63 
    72 
    64 
    73 
    65 (*** TF_rec -- by Vset recursion ***)
       
    66 
       
    67 (** conversion rules **)
       
    68 
       
    69 goal TF.thy "TF_rec(Tcons(a,f), b, c, d) = b(a, f, TF_rec(f,b,c,d))";
       
    70 by (rtac (TF_rec_def RS def_Vrec RS trans) 1);
       
    71 by (rewrite_goals_tac tree_forest.con_defs);
       
    72 by (simp_tac rank_ss 1);
       
    73 val TF_rec_Tcons = result();
       
    74 
       
    75 goal TF.thy "TF_rec(Fnil, b, c, d) = c";
       
    76 by (rtac (TF_rec_def RS def_Vrec RS trans) 1);
       
    77 by (rewrite_goals_tac tree_forest.con_defs);
       
    78 by (simp_tac rank_ss 1);
       
    79 val TF_rec_Fnil = result();
       
    80 
       
    81 goal TF.thy "TF_rec(Fcons(t,f), b, c, d) = \
       
    82 \      d(t, f, TF_rec(t, b, c, d), TF_rec(f, b, c, d))";
       
    83 by (rtac (TF_rec_def RS def_Vrec RS trans) 1);
       
    84 by (rewrite_goals_tac tree_forest.con_defs);
       
    85 by (simp_tac rank_ss 1);
       
    86 val TF_rec_Fcons = result();
       
    87 
       
    88 (*list_ss includes list operations as well as arith_ss*)
       
    89 val TF_rec_ss = list_ss addsimps
       
    90   [TF_rec_Tcons, TF_rec_Fnil, TF_rec_Fcons, TconsI, FnilI, FconsI];
       
    91 
       
    92 (** Type checking **)
       
    93 
       
    94 val major::prems = goal TF.thy
       
    95     "[| z: tree_forest(A);  \
       
    96 \       !!x f r. [| x: A;  f: forest(A);  r: C(f) 		\
       
    97 \                 |] ==> b(x,f,r): C(Tcons(x,f));     	\
       
    98 \	c : C(Fnil);        					\
       
    99 \       !!t f r1 r2. [| t: tree(A);  f: forest(A);  r1: C(t); r2: C(f) \
       
   100 \                     |] ==> d(t,f,r1,r2): C(Fcons(t,f))    	\
       
   101 \    |] ==> TF_rec(z,b,c,d) : C(z)";
       
   102 by (rtac (major RS tree_forest.induct) 1);
       
   103 by (ALLGOALS (asm_simp_tac (TF_rec_ss addsimps prems)));
       
   104 val TF_rec_type = result();
       
   105 
       
   106 (*Mutually recursive version*)
       
   107 val prems = goal TF.thy
       
   108     "[| !!x f r. [| x: A;  f: forest(A);  r: D(f) 		\
       
   109 \                 |] ==> b(x,f,r): C(Tcons(x,f));    	 	\
       
   110 \	c : D(Fnil);        					\
       
   111 \       !!t f r1 r2. [| t: tree(A);  f: forest(A);  r1: C(t); r2: D(f) \
       
   112 \                     |] ==> d(t,f,r1,r2): D(Fcons(t,f))    	\
       
   113 \    |] ==> (ALL t:tree(A).    TF_rec(t,b,c,d)  : C(t)) &  	\
       
   114 \           (ALL f: forest(A). TF_rec(f,b,c,d) : D(f))";
       
   115 by (rewtac Ball_def);
       
   116 by (rtac tree_forest.mutual_induct 1);
       
   117 by (ALLGOALS (asm_simp_tac (TF_rec_ss addsimps prems)));
       
   118 val tree_forest_rec_type = result();
       
   119 
       
   120 
       
   121 (** Versions for use with definitions **)
       
   122 
       
   123 val [rew] = goal TF.thy
       
   124     "[| !!z. j(z)==TF_rec(z,b,c,d) |] ==> j(Tcons(a,f)) = b(a,f,j(f))";
       
   125 by (rewtac rew);
       
   126 by (rtac TF_rec_Tcons 1);
       
   127 val def_TF_rec_Tcons = result();
       
   128 
       
   129 val [rew] = goal TF.thy
       
   130     "[| !!z. j(z)==TF_rec(z,b,c,d) |] ==> j(Fnil) = c";
       
   131 by (rewtac rew);
       
   132 by (rtac TF_rec_Fnil 1);
       
   133 val def_TF_rec_Fnil = result();
       
   134 
       
   135 val [rew] = goal TF.thy
       
   136     "[| !!z. j(z)==TF_rec(z,b,c,d) |] ==> j(Fcons(t,f)) = d(t,f,j(t),j(f))";
       
   137 by (rewtac rew);
       
   138 by (rtac TF_rec_Fcons 1);
       
   139 val def_TF_rec_Fcons = result();
       
   140 
       
   141 fun TF_recs def = map standard 
       
   142     	([def] RL [def_TF_rec_Tcons, def_TF_rec_Fnil, def_TF_rec_Fcons]);
       
   143 
       
   144 
       
   145 (** list_of_TF and TF_of_list **)
       
   146 
       
   147 val [list_of_TF_Tcons, list_of_TF_Fnil, list_of_TF_Fcons] =
       
   148 	TF_recs list_of_TF_def;
       
   149 
       
   150 goalw TF.thy [list_of_TF_def]
       
   151     "!!z A. z: tree_forest(A) ==> list_of_TF(z) : list(tree(A))";
       
   152 by (REPEAT (ares_tac ([TF_rec_type,TconsI] @ list_typechecks) 1));
       
   153 val list_of_TF_type = result();
       
   154 
       
   155 val [TF_of_list_Nil,TF_of_list_Cons] = list_recs TF_of_list_def;
       
   156 
       
   157 goalw TF.thy [TF_of_list_def] 
       
   158     "!!l A. l: list(tree(A)) ==> TF_of_list(l) : forest(A)";
       
   159 by (REPEAT (ares_tac [list_rec_type, FnilI, FconsI] 1));
       
   160 val TF_of_list_type = result();
       
   161 
       
   162 
       
   163 (** TF_map **)
       
   164 
       
   165 val [TF_map_Tcons, TF_map_Fnil, TF_map_Fcons] = TF_recs TF_map_def;
       
   166 
       
   167 val prems = goalw TF.thy [TF_map_def]
       
   168     "[| !!x. x: A ==> h(x): B |] ==> \
       
   169 \      (ALL t:tree(A). TF_map(h,t) : tree(B)) &  \
       
   170 \      (ALL f: forest(A). TF_map(h,f) : forest(B))";
       
   171 by (REPEAT
       
   172     (ares_tac ([tree_forest_rec_type, TconsI, FnilI, FconsI] @ prems) 1));
       
   173 val TF_map_type = result();
       
   174 
       
   175 
       
   176 (** TF_size **)
       
   177 
       
   178 val [TF_size_Tcons, TF_size_Fnil, TF_size_Fcons] = TF_recs TF_size_def;
       
   179 
       
   180 goalw TF.thy [TF_size_def]
       
   181     "!!z A. z: tree_forest(A) ==> TF_size(z) : nat";
       
   182 by (REPEAT (ares_tac [TF_rec_type, add_type, nat_0I, nat_succI] 1));
       
   183 val TF_size_type = result();
       
   184 
       
   185 
       
   186 (** TF_preorder **)
       
   187 
       
   188 val [TF_preorder_Tcons, TF_preorder_Fnil, TF_preorder_Fcons] =
       
   189 	TF_recs TF_preorder_def;
       
   190 
       
   191 goalw TF.thy [TF_preorder_def]
       
   192     "!!z A. z: tree_forest(A) ==> TF_preorder(z) : list(A)";
       
   193 by (REPEAT (ares_tac ([TF_rec_type, app_type] @ list.intrs) 1));
       
   194 val TF_preorder_type = result();
       
   195 
       
   196 
       
   197 (** Term simplification **)
       
   198 
       
   199 val treeI = tree_subset_TF RS subsetD
       
   200 and forestI = forest_subset_TF RS subsetD;
       
   201 
       
   202 val TF_typechecks =
       
   203     [TconsI, FnilI, FconsI, treeI, forestI,
       
   204      list_of_TF_type, TF_map_type, TF_size_type, TF_preorder_type];
       
   205 
       
   206 val TF_rewrites =
       
   207    [TF_rec_Tcons, TF_rec_Fnil, TF_rec_Fcons,
       
   208     list_of_TF_Tcons, list_of_TF_Fnil, list_of_TF_Fcons,
       
   209     TF_of_list_Nil,TF_of_list_Cons,
       
   210     TF_map_Tcons, TF_map_Fnil, TF_map_Fcons,
       
   211     TF_size_Tcons, TF_size_Fnil, TF_size_Fcons,
       
   212     TF_preorder_Tcons, TF_preorder_Fnil, TF_preorder_Fcons];
       
   213 
       
   214 val TF_ss = list_ss addsimps TF_rewrites
       
   215                     setsolver type_auto_tac (list_typechecks@TF_typechecks);
       
   216 
       
   217 (** theorems about list_of_TF and TF_of_list **)
       
   218 
       
   219 (*essentially the same as list induction*)
       
   220 val major::prems = goal TF.thy 
       
   221     "[| f: forest(A);  	\
       
   222 \       R(Fnil);        \
       
   223 \       !!t f. [| t: tree(A);  f: forest(A);  R(f) |] ==> R(Fcons(t,f))  \
       
   224 \    |] ==> R(f)";
       
   225 by (rtac (major RS (tree_forest.mutual_induct RS conjunct2 RS spec RSN (2,rev_mp))) 1);
       
   226 by (REPEAT (ares_tac (TrueI::prems) 1));
       
   227 val forest_induct = result();
       
   228 
       
   229 goal TF.thy "!!f A. f: forest(A) ==> TF_of_list(list_of_TF(f)) = f";
       
   230 by (etac forest_induct 1);
       
   231 by (ALLGOALS (asm_simp_tac TF_ss));
       
   232 val forest_iso = result();
       
   233 
       
   234 goal TF.thy
       
   235     "!!ts. ts: list(tree(A)) ==> list_of_TF(TF_of_list(ts)) = ts";
       
   236 by (etac list.induct 1);
       
   237 by (ALLGOALS (asm_simp_tac TF_ss));
       
   238 val tree_list_iso = result();
       
   239 
       
   240 (** theorems about TF_map **)
       
   241 
       
   242 goal TF.thy "!!z A. z: tree_forest(A) ==> TF_map(%u.u, z) = z";
       
   243 by (etac tree_forest.induct 1);
       
   244 by (ALLGOALS (asm_simp_tac TF_ss));
       
   245 val TF_map_ident = result();
       
   246 
       
   247 goal TF.thy
       
   248  "!!z A. z: tree_forest(A) ==> TF_map(h, TF_map(j,z)) = TF_map(%u.h(j(u)), z)";
       
   249 by (etac tree_forest.induct 1);
       
   250 by (ALLGOALS (asm_simp_tac TF_ss));
       
   251 val TF_map_compose = result();
       
   252 
       
   253 (** theorems about TF_size **)
       
   254 
       
   255 goal TF.thy
       
   256     "!!z A. z: tree_forest(A) ==> TF_size(TF_map(h,z)) = TF_size(z)";
       
   257 by (etac tree_forest.induct 1);
       
   258 by (ALLGOALS (asm_simp_tac TF_ss));
       
   259 val TF_size_TF_map = result();
       
   260 
       
   261 goal TF.thy
       
   262     "!!z A. z: tree_forest(A) ==> TF_size(z) = length(TF_preorder(z))";
       
   263 by (etac tree_forest.induct 1);
       
   264 by (ALLGOALS (asm_simp_tac (TF_ss addsimps [length_app])));
       
   265 val TF_size_length = result();
       
   266 
       
   267 (** theorems about TF_preorder **)
       
   268 
       
   269 goal TF.thy "!!z A. z: tree_forest(A) ==> \
       
   270 \                      TF_preorder(TF_map(h,z)) = map(h, TF_preorder(z))";
       
   271 by (etac tree_forest.induct 1);
       
   272 by (ALLGOALS (asm_simp_tac (TF_ss addsimps [map_app_distrib])));
       
   273 val TF_preorder_TF_map = result();