src/ZF/ex/TF.ML
changeset 6046 2c8a8be36c94
parent 5147 825877190618
child 6153 bff90585cce5
equal deleted inserted replaced
6045:6a9dc67d48f5 6046:2c8a8be36c94
     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>)))"
       
    12 *)
     7 *)
    13 
     8 
    14 open TF;
     9 Addsimps tree_forest.intrs;
    15 
       
    16 val [TconsI, FnilI, FconsI] = tree_forest.intrs;
       
    17 
    10 
    18 (** tree_forest(A) as the union of tree(A) and forest(A) **)
    11 (** tree_forest(A) as the union of tree(A) and forest(A) **)
    19 
    12 
    20 val [_, tree_def, forest_def] = tree_forest.defs;
    13 val [_, tree_def, forest_def] = tree_forest.defs;
    21 
    14 
    56 by (rtac (Part_Inr RS subst) 1);
    49 by (rtac (Part_Inr RS subst) 1);
    57 by (rtac (tree_forest_unfold' RS subst_context) 1);
    50 by (rtac (tree_forest_unfold' RS subst_context) 1);
    58 qed "forest_unfold";
    51 qed "forest_unfold";
    59 
    52 
    60 
    53 
    61 (*** TF_rec -- by Vset recursion ***)
    54 (** Type checking for recursor: Not needed; possibly interesting (??) **)
    62 
       
    63 (** conversion rules **)
       
    64 
       
    65 Goal "TF_rec(Tcons(a,f), b, c, d) = b(a, f, TF_rec(f,b,c,d))";
       
    66 by (rtac (TF_rec_def RS def_Vrec RS trans) 1);
       
    67 by (rewrite_goals_tac tree_forest.con_defs);
       
    68 by (simp_tac rank_ss 1);
       
    69 qed "TF_rec_Tcons";
       
    70 
       
    71 Goal "TF_rec(Fnil, b, c, d) = c";
       
    72 by (rtac (TF_rec_def RS def_Vrec RS trans) 1);
       
    73 by (rewrite_goals_tac tree_forest.con_defs);
       
    74 by (Simp_tac 1);
       
    75 qed "TF_rec_Fnil";
       
    76 
       
    77 Goal "TF_rec(Fcons(t,f), b, c, d) = \
       
    78 \      d(t, f, TF_rec(t, b, c, d), TF_rec(f, b, c, d))";
       
    79 by (rtac (TF_rec_def RS def_Vrec RS trans) 1);
       
    80 by (rewrite_goals_tac tree_forest.con_defs);
       
    81 by (simp_tac rank_ss 1);
       
    82 qed "TF_rec_Fcons";
       
    83 
       
    84 Addsimps [TF_rec_Tcons, TF_rec_Fnil, TF_rec_Fcons, TconsI, FnilI, FconsI];
       
    85 
       
    86 (** Type checking **)
       
    87 
    55 
    88 val major::prems = goal TF.thy
    56 val major::prems = goal TF.thy
    89     "[| z: tree_forest(A);  \
    57     "[| z: tree_forest(A);  \
    90 \       !!x f r. [| x: A;  f: forest(A);  r: C(f)               \
    58 \       !!x f r. [| x: A;  f: forest(A);  r: C(f)               \
    91 \                 |] ==> b(x,f,r): C(Tcons(x,f));       \
    59 \                 |] ==> b(x,f,r): C(Tcons(x,f));       \
    92 \       c : C(Fnil);                                            \
    60 \       c : C(Fnil);                                            \
    93 \       !!t f r1 r2. [| t: tree(A);  f: forest(A);  r1: C(t); r2: C(f) \
    61 \       !!t f r1 r2. [| t: tree(A);  f: forest(A);  r1: C(t); r2: C(f) \
    94 \                     |] ==> d(t,f,r1,r2): C(Fcons(t,f))        \
    62 \                     |] ==> d(t,f,r1,r2): C(Fcons(t,f))        \
    95 \    |] ==> TF_rec(z,b,c,d) : C(z)";
    63 \    |] ==> tree_forest_rec(b,c,d,z) : C(z)";
    96 by (rtac (major RS tree_forest.induct) 1);
    64 by (rtac (major RS tree_forest.induct) 1);
    97 by (ALLGOALS (asm_simp_tac (simpset() addsimps prems)));
    65 by (ALLGOALS (asm_simp_tac (simpset() addsimps prems)));
    98 qed "TF_rec_type";
    66 qed "TF_rec_type";
    99 
    67 
   100 (*Mutually recursive version*)
    68 (*Mutually recursive version*)
   102     "[| !!x f r. [| x: A;  f: forest(A);  r: D(f)               \
    70     "[| !!x f r. [| x: A;  f: forest(A);  r: D(f)               \
   103 \                 |] ==> b(x,f,r): C(Tcons(x,f));               \
    71 \                 |] ==> b(x,f,r): C(Tcons(x,f));               \
   104 \       c : D(Fnil);                                            \
    72 \       c : D(Fnil);                                            \
   105 \       !!t f r1 r2. [| t: tree(A);  f: forest(A);  r1: C(t); r2: D(f) \
    73 \       !!t f r1 r2. [| t: tree(A);  f: forest(A);  r1: C(t); r2: D(f) \
   106 \                     |] ==> d(t,f,r1,r2): D(Fcons(t,f))        \
    74 \                     |] ==> d(t,f,r1,r2): D(Fcons(t,f))        \
   107 \    |] ==> (ALL t:tree(A).    TF_rec(t,b,c,d)  : C(t)) &       \
    75 \    |] ==> (ALL t:tree(A).    tree_forest_rec(b,c,d,t)  : C(t)) &       \
   108 \           (ALL f: forest(A). TF_rec(f,b,c,d) : D(f))";
    76 \           (ALL f: forest(A). tree_forest_rec(b,c,d,f) : D(f))";
   109 by (rewtac Ball_def);
    77 by (rewtac Ball_def);
   110 by (rtac tree_forest.mutual_induct 1);
    78 by (rtac tree_forest.mutual_induct 1);
   111 by (ALLGOALS (asm_simp_tac (simpset() addsimps prems)));
    79 by (ALLGOALS (asm_simp_tac (simpset() addsimps prems)));
   112 qed "tree_forest_rec_type";
    80 qed "tree_forest_rec_type";
   113 
    81 
   114 
    82 
   115 (** Versions for use with definitions **)
    83 (** list_of_TF and of_list **)
   116 
    84 
   117 val [rew] = goal TF.thy
    85 Goal "z: tree_forest(A) ==> list_of_TF(z) : list(tree(A))";
   118     "[| !!z. j(z)==TF_rec(z,b,c,d) |] ==> j(Tcons(a,f)) = b(a,f,j(f))";
    86 by (etac tree_forest.induct 1);
   119 by (rewtac rew);
    87 by (ALLGOALS Asm_simp_tac);
   120 by (rtac TF_rec_Tcons 1);
    88 qed "list_of_TF_type";
   121 qed "def_TF_rec_Tcons";
       
   122 
    89 
   123 val [rew] = goal TF.thy
    90 Goal "l: list(tree(A)) ==> of_list(l) : forest(A)";
   124     "[| !!z. j(z)==TF_rec(z,b,c,d) |] ==> j(Fnil) = c";
    91 by (etac list.induct 1);
   125 by (rewtac rew);
    92 by (ALLGOALS Asm_simp_tac);
   126 by (rtac TF_rec_Fnil 1);
    93 qed "of_list_type";
   127 qed "def_TF_rec_Fnil";
       
   128 
       
   129 val [rew] = goal TF.thy
       
   130     "[| !!z. j(z)==TF_rec(z,b,c,d) |] ==> j(Fcons(t,f)) = d(t,f,j(t),j(f))";
       
   131 by (rewtac rew);
       
   132 by (rtac TF_rec_Fcons 1);
       
   133 qed "def_TF_rec_Fcons";
       
   134 
       
   135 fun TF_recs def = map standard 
       
   136         ([def] RL [def_TF_rec_Tcons, def_TF_rec_Fnil, def_TF_rec_Fcons]);
       
   137 
    94 
   138 
    95 
   139 (** list_of_TF and TF_of_list **)
    96 (** map **)
   140 
    97 
   141 val [list_of_TF_Tcons, list_of_TF_Fnil, list_of_TF_Fcons] =
    98 val prems = Goal
   142         TF_recs list_of_TF_def;
    99     "[| !!x. x: A ==> h(x): B |] ==> \
   143 Addsimps [list_of_TF_Tcons, list_of_TF_Fnil, list_of_TF_Fcons];
   100 \      (ALL t:tree(A). map(h,t) : tree(B)) &  \
   144 
   101 \      (ALL f: forest(A). map(h,f) : forest(B))";
   145 Goalw [list_of_TF_def]
   102 by (rewtac Ball_def);
   146     "z: tree_forest(A) ==> list_of_TF(z) : list(tree(A))";
   103 by (rtac tree_forest.mutual_induct 1);
   147 by (REPEAT (ares_tac ([TF_rec_type,TconsI] @ list_typechecks) 1));
   104 by (ALLGOALS (asm_simp_tac (simpset() addsimps prems)));
   148 qed "list_of_TF_type";
   105 qed "map_type";
   149 
       
   150 val [TF_of_list_Nil,TF_of_list_Cons] = list_recs TF_of_list_def;
       
   151 Addsimps [TF_of_list_Nil,TF_of_list_Cons];
       
   152 
       
   153 Goalw [TF_of_list_def] 
       
   154     "l: list(tree(A)) ==> TF_of_list(l) : forest(A)";
       
   155 by (REPEAT (ares_tac [list_rec_type, FnilI, FconsI] 1));
       
   156 qed "TF_of_list_type";
       
   157 
   106 
   158 
   107 
   159 (** TF_map **)
   108 (** size **)
   160 
   109 
   161 val [TF_map_Tcons, TF_map_Fnil, TF_map_Fcons] = TF_recs TF_map_def;
   110 Goal "z: tree_forest(A) ==> size(z) : nat";
   162 Addsimps [TF_map_Tcons, TF_map_Fnil, TF_map_Fcons];
   111 by (etac tree_forest.induct 1);
   163 
   112 by (ALLGOALS Asm_simp_tac);
   164 val prems = goalw TF.thy [TF_map_def]
   113 qed "size_type";
   165     "[| !!x. x: A ==> h(x): B |] ==> \
       
   166 \      (ALL t:tree(A). TF_map(h,t) : tree(B)) &  \
       
   167 \      (ALL f: forest(A). TF_map(h,f) : forest(B))";
       
   168 by (REPEAT
       
   169     (ares_tac ([tree_forest_rec_type, TconsI, FnilI, FconsI] @ prems) 1));
       
   170 qed "TF_map_type";
       
   171 
   114 
   172 
   115 
   173 (** TF_size **)
   116 (** preorder **)
   174 
   117 
   175 val [TF_size_Tcons, TF_size_Fnil, TF_size_Fcons] = TF_recs TF_size_def;
   118 Goal "z: tree_forest(A) ==> preorder(z) : list(A)";
   176 Addsimps [TF_size_Tcons, TF_size_Fnil, TF_size_Fcons];
   119 by (etac tree_forest.induct 1);
   177 
   120 by (ALLGOALS Asm_simp_tac);
   178 Goalw [TF_size_def]
   121 qed "preorder_type";
   179     "z: tree_forest(A) ==> TF_size(z) : nat";
       
   180 by (REPEAT (ares_tac [TF_rec_type, add_type, nat_0I, nat_succI] 1));
       
   181 qed "TF_size_type";
       
   182 
       
   183 
       
   184 (** TF_preorder **)
       
   185 
       
   186 val [TF_preorder_Tcons, TF_preorder_Fnil, TF_preorder_Fcons] =
       
   187         TF_recs TF_preorder_def;
       
   188 Addsimps [TF_preorder_Tcons, TF_preorder_Fnil, TF_preorder_Fcons];
       
   189 
       
   190 Goalw [TF_preorder_def]
       
   191     "z: tree_forest(A) ==> TF_preorder(z) : list(A)";
       
   192 by (REPEAT (ares_tac ([TF_rec_type, app_type] @ list.intrs) 1));
       
   193 qed "TF_preorder_type";
       
   194 
   122 
   195 
   123 
   196 (** Term simplification **)
   124 (** Term simplification **)
   197 
   125 
   198 val treeI = tree_subset_TF RS subsetD
   126 val treeI = tree_subset_TF RS subsetD
   199 and forestI = forest_subset_TF RS subsetD;
   127 and forestI = forest_subset_TF RS subsetD;
   200 
   128 
   201 val TF_typechecks =
   129 val typechecks =
   202     [TconsI, FnilI, FconsI, treeI, forestI,
   130     [treeI, forestI,
   203      list_of_TF_type, TF_map_type, TF_size_type, TF_preorder_type];
   131      list_of_TF_type, map_type, size_type, preorder_type];
   204 
   132 
   205 simpset_ref() := simpset() setSolver type_auto_tac (list_typechecks@TF_typechecks);
   133 Addsimps typechecks;
   206 
   134 
   207 (** theorems about list_of_TF and TF_of_list **)
   135 (** theorems about list_of_TF and of_list **)
   208 
   136 
   209 (*essentially the same as list induction*)
   137 (*essentially the same as list induction*)
   210 val major::prems = goal TF.thy 
   138 val major::prems = Goal
   211     "[| f: forest(A);   \
   139     "[| f: forest(A);   \
   212 \       R(Fnil);        \
   140 \       R(Fnil);        \
   213 \       !!t f. [| t: tree(A);  f: forest(A);  R(f) |] ==> R(Fcons(t,f))  \
   141 \       !!t f. [| t: tree(A);  f: forest(A);  R(f) |] ==> R(Fcons(t,f))  \
   214 \    |] ==> R(f)";
   142 \    |] ==> R(f)";
   215 by (rtac (major RS (tree_forest.mutual_induct RS conjunct2 RS spec RSN (2,rev_mp))) 1);
   143 by (rtac (major RS (tree_forest.mutual_induct RS conjunct2 RS spec RSN (2,rev_mp))) 1);
   216 by (REPEAT (ares_tac (TrueI::prems) 1));
   144 by (REPEAT (ares_tac (TrueI::prems) 1));
   217 qed "forest_induct";
   145 qed "forest_induct";
   218 
   146 
   219 Goal "f: forest(A) ==> TF_of_list(list_of_TF(f)) = f";
   147 Goal "f: forest(A) ==> of_list(list_of_TF(f)) = f";
   220 by (etac forest_induct 1);
   148 by (etac forest_induct 1);
   221 by (ALLGOALS Asm_simp_tac);
   149 by (ALLGOALS Asm_simp_tac);
   222 qed "forest_iso";
   150 qed "forest_iso";
   223 
   151 
   224 Goal "ts: list(tree(A)) ==> list_of_TF(TF_of_list(ts)) = ts";
   152 Goal "ts: list(tree(A)) ==> list_of_TF(of_list(ts)) = ts";
   225 by (etac list.induct 1);
   153 by (etac list.induct 1);
   226 by (ALLGOALS Asm_simp_tac);
   154 by (ALLGOALS Asm_simp_tac);
   227 qed "tree_list_iso";
   155 qed "tree_list_iso";
   228 
   156 
   229 (** theorems about TF_map **)
   157 (** theorems about map **)
   230 
   158 
   231 Goal "z: tree_forest(A) ==> TF_map(%u. u, z) = z";
   159 Goal "z: tree_forest(A) ==> map(%u. u, z) = z";
   232 by (etac tree_forest.induct 1);
   160 by (etac tree_forest.induct 1);
   233 by (ALLGOALS Asm_simp_tac);
   161 by (ALLGOALS Asm_simp_tac);
   234 qed "TF_map_ident";
   162 qed "map_ident";
   235 
   163 
   236 Goal "z: tree_forest(A) ==> TF_map(h, TF_map(j,z)) = TF_map(%u. h(j(u)), z)";
   164 Goal "z: tree_forest(A) ==> map(h, map(j,z)) = map(%u. h(j(u)), z)";
   237 by (etac tree_forest.induct 1);
   165 by (etac tree_forest.induct 1);
   238 by (ALLGOALS Asm_simp_tac);
   166 by (ALLGOALS Asm_simp_tac);
   239 qed "TF_map_compose";
   167 qed "map_compose";
   240 
   168 
   241 (** theorems about TF_size **)
   169 (** theorems about size **)
   242 
   170 
   243 Goal "z: tree_forest(A) ==> TF_size(TF_map(h,z)) = TF_size(z)";
   171 Goal "z: tree_forest(A) ==> size(map(h,z)) = size(z)";
   244 by (etac tree_forest.induct 1);
   172 by (etac tree_forest.induct 1);
   245 by (ALLGOALS Asm_simp_tac);
   173 by (ALLGOALS Asm_simp_tac);
   246 qed "TF_size_TF_map";
   174 qed "size_map";
   247 
   175 
   248 Goal "z: tree_forest(A) ==> TF_size(z) = length(TF_preorder(z))";
   176 Goal "z: tree_forest(A) ==> size(z) = length(preorder(z))";
   249 by (etac tree_forest.induct 1);
   177 by (etac tree_forest.induct 1);
   250 by (ALLGOALS (asm_simp_tac (simpset() addsimps [length_app])));
   178 by (ALLGOALS   (*TYPECHECKING: why so explicit?*)
   251 qed "TF_size_length";
   179     (asm_simp_tac
       
   180      (simpset() addsimps [treeI RS preorder_type RS length_app])));
       
   181 qed "size_length";
   252 
   182 
   253 (** theorems about TF_preorder **)
   183 (** theorems about preorder **)
   254 
   184 
   255 Goal "z: tree_forest(A) ==> \
   185 Goal "z: tree_forest(A) ==> preorder(TF.map(h,z)) = List.map(h, preorder(z))";
   256 \                      TF_preorder(TF_map(h,z)) = map(h, TF_preorder(z))";
       
   257 by (etac tree_forest.induct 1);
   186 by (etac tree_forest.induct 1);
   258 by (ALLGOALS (asm_simp_tac (simpset() addsimps [map_app_distrib])));
   187 by (ALLGOALS  (*TYPECHECKING: why so explicit?*)
   259 qed "TF_preorder_TF_map";
   188     (asm_simp_tac
       
   189      (simpset() addsimps [treeI RS preorder_type RS map_app_distrib])));
       
   190 qed "preorder_map";