src/HOL/Tools/record.ML
changeset 46047 6170af176fbb
parent 46046 05392bdd2286
child 46049 963af563a132
equal deleted inserted replaced
46046:05392bdd2286 46047:6170af176fbb
   377 type data =
   377 type data =
   378  {records: info Symtab.table,
   378  {records: info Symtab.table,
   379   sel_upd:
   379   sel_upd:
   380    {selectors: (int * bool) Symtab.table,
   380    {selectors: (int * bool) Symtab.table,
   381     updates: string Symtab.table,
   381     updates: string Symtab.table,
   382     simpset: Simplifier.simpset,
   382     simpset: simpset,
   383     defset: Simplifier.simpset,
   383     defset: simpset,
   384     foldcong: Simplifier.simpset,
   384     foldcong: simpset,
   385     unfoldcong: Simplifier.simpset},
   385     unfoldcong: simpset},
   386   equalities: thm Symtab.table,
   386   equalities: thm Symtab.table,
   387   extinjects: thm list,
   387   extinjects: thm list,
   388   extsplit: thm Symtab.table,  (*maps extension name to split rule*)
   388   extsplit: thm Symtab.table,  (*maps extension name to split rule*)
   389   splits: (thm * thm * thm * thm) Symtab.table,  (*!!, ALL, EX - split-equalities, induct rule*)
   389   splits: (thm * thm * thm * thm) Symtab.table,  (*!!, ALL, EX - split-equalities, induct rule*)
   390   extfields: (string * typ) list Symtab.table,  (*maps extension to its fields*)
   390   extfields: (string * typ) list Symtab.table,  (*maps extension to its fields*)
   492     val {records, sel_upd = {selectors, updates, simpset, defset, foldcong, unfoldcong},
   492     val {records, sel_upd = {selectors, updates, simpset, defset, foldcong, unfoldcong},
   493       equalities, extinjects, extsplit, splits, extfields, fieldext} = Data.get thy;
   493       equalities, extinjects, extsplit, splits, extfields, fieldext} = Data.get thy;
   494     val data = make_data records
   494     val data = make_data records
   495       {selectors = fold Symtab.update_new sels selectors,
   495       {selectors = fold Symtab.update_new sels selectors,
   496         updates = fold Symtab.update_new upds updates,
   496         updates = fold Symtab.update_new upds updates,
   497         simpset = Simplifier.addsimps (simpset, simps),
   497         simpset = simpset addsimps simps,
   498         defset = Simplifier.addsimps (defset, defs),
   498         defset = defset addsimps defs,
   499         foldcong = foldcong |> fold Simplifier.add_cong folds,
   499         foldcong = foldcong |> fold Simplifier.add_cong folds,
   500         unfoldcong = unfoldcong |> fold Simplifier.add_cong unfolds}
   500         unfoldcong = unfoldcong |> fold Simplifier.add_cong unfolds}
   501        equalities extinjects extsplit splits extfields fieldext;
   501        equalities extinjects extsplit splits extfields fieldext;
   502   in Data.put data thy end;
   502   in Data.put data thy end;
   503 
   503 
  1421 
  1421 
  1422     val simprocs = if has_rec goal then [split_simproc P] else [];
  1422     val simprocs = if has_rec goal then [split_simproc P] else [];
  1423     val thms' = @{thms o_apply K_record_comp} @ thms;
  1423     val thms' = @{thms o_apply K_record_comp} @ thms;
  1424   in
  1424   in
  1425     EVERY split_frees_tacs THEN
  1425     EVERY split_frees_tacs THEN
  1426     Simplifier.full_simp_tac (get_simpset thy addsimps thms' addsimprocs simprocs) i
  1426     full_simp_tac (get_simpset thy addsimps thms' addsimprocs simprocs) i
  1427   end);
  1427   end);
  1428 
  1428 
  1429 
  1429 
  1430 (* split_tac *)
  1430 (* split_tac *)
  1431 
  1431 
  1442     fun is_all (Const (@{const_name all}, _) $ _) = ~1
  1442     fun is_all (Const (@{const_name all}, _) $ _) = ~1
  1443       | is_all (Const (@{const_name All}, _) $ _) = ~1
  1443       | is_all (Const (@{const_name All}, _) $ _) = ~1
  1444       | is_all _ = 0;
  1444       | is_all _ = 0;
  1445   in
  1445   in
  1446     if has_rec goal then
  1446     if has_rec goal then
  1447       Simplifier.full_simp_tac (HOL_basic_ss addsimprocs [split_simproc is_all]) i
  1447       full_simp_tac (HOL_basic_ss addsimprocs [split_simproc is_all]) i
  1448     else no_tac
  1448     else no_tac
  1449   end);
  1449   end);
  1450 
  1450 
  1451 
  1451 
  1452 (* wrapper *)
  1452 (* wrapper *)
  1464 fun induct_type_global name = [case_names_fields, Induct.induct_type name];
  1464 fun induct_type_global name = [case_names_fields, Induct.induct_type name];
  1465 fun cases_type_global name = [case_names_fields, Induct.cases_type name];
  1465 fun cases_type_global name = [case_names_fields, Induct.cases_type name];
  1466 
  1466 
  1467 
  1467 
  1468 (* tactics *)
  1468 (* tactics *)
  1469 
       
  1470 fun simp_all_tac ss simps = ALLGOALS (Simplifier.asm_full_simp_tac (ss addsimps simps));
       
  1471 
  1469 
  1472 (*Do case analysis / induction according to rule on last parameter of ith subgoal
  1470 (*Do case analysis / induction according to rule on last parameter of ith subgoal
  1473   (or on s if there are no parameters).
  1471   (or on s if there are no parameters).
  1474   Instatiation of record variable (and predicate) in rule is calculated to
  1472   Instatiation of record variable (and predicate) in rule is calculated to
  1475   avoid problems with higher order unification.*)
  1473   avoid problems with higher order unification.*)
  1791       (HOLogic.mk_Trueprop o HOLogic.mk_eq)
  1789       (HOLogic.mk_Trueprop o HOLogic.mk_eq)
  1792         (Const (@{const_name HOL.equal}, extT --> extT --> HOLogic.boolT),
  1790         (Const (@{const_name HOL.equal}, extT --> extT --> HOLogic.boolT),
  1793          Const (@{const_name HOL.eq}, extT --> extT --> HOLogic.boolT));
  1791          Const (@{const_name HOL.eq}, extT --> extT --> HOLogic.boolT));
  1794     fun tac eq_def =
  1792     fun tac eq_def =
  1795       Class.intro_classes_tac []
  1793       Class.intro_classes_tac []
  1796       THEN (Simplifier.rewrite_goals_tac [Simpdata.mk_eq eq_def])
  1794       THEN rewrite_goals_tac [Simpdata.mk_eq eq_def]
  1797       THEN ALLGOALS (rtac @{thm refl});
  1795       THEN ALLGOALS (rtac @{thm refl});
  1798     fun mk_eq thy eq_def = Simplifier.rewrite_rule
  1796     fun mk_eq thy eq_def =
  1799       [(AxClass.unoverload thy o Thm.symmetric o Simpdata.mk_eq) eq_def] inject;
  1797       Simplifier.rewrite_rule
       
  1798         [(AxClass.unoverload thy o Thm.symmetric o Simpdata.mk_eq) eq_def] inject;
  1800     fun mk_eq_refl thy =
  1799     fun mk_eq_refl thy =
  1801       @{thm equal_refl}
  1800       @{thm equal_refl}
  1802       |> Thm.instantiate
  1801       |> Thm.instantiate
  1803         ([pairself (ctyp_of thy) (TVar (("'a", 0), @{sort equal}), Logic.varifyT_global extT)], [])
  1802         ([pairself (ctyp_of thy) (TVar (("'a", 0), @{sort equal}), Logic.varifyT_global extT)], [])
  1804       |> AxClass.unoverload thy;
  1803       |> AxClass.unoverload thy;
  2127 
  2126 
  2128     (* 3rd stage: thms_thy *)
  2127     (* 3rd stage: thms_thy *)
  2129 
  2128 
  2130     val prove = Skip_Proof.prove_global defs_thy;
  2129     val prove = Skip_Proof.prove_global defs_thy;
  2131 
  2130 
  2132     fun prove_simp ss simps =
       
  2133       let val tac = simp_all_tac ss simps
       
  2134       in fn prop => prove [] [] prop (K tac) end;
       
  2135 
       
  2136     val ss = get_simpset defs_thy;
  2131     val ss = get_simpset defs_thy;
       
  2132     val simp_defs_tac =
       
  2133       asm_full_simp_tac (ss addsimps (sel_defs @ accessor_thms @ upd_defs @ updator_thms));
  2137 
  2134 
  2138     val sel_convs = timeit_msg ctxt "record sel_convs proof:" (fn () =>
  2135     val sel_convs = timeit_msg ctxt "record sel_convs proof:" (fn () =>
  2139       map (prove_simp ss (sel_defs @ accessor_thms)) sel_conv_props);
  2136       map (fn prop => prove [] [] prop (K (ALLGOALS simp_defs_tac))) sel_conv_props);
  2140 
  2137 
  2141     val upd_convs = timeit_msg ctxt "record upd_convs proof:" (fn () =>
  2138     val upd_convs = timeit_msg ctxt "record upd_convs proof:" (fn () =>
  2142       map (prove_simp ss (upd_defs @ updator_thms)) upd_conv_props);
  2139       map (fn prop => prove [] [] prop (K (ALLGOALS simp_defs_tac))) upd_conv_props);
  2143 
  2140 
  2144     val (fold_congs, unfold_congs) = timeit_msg ctxt "record upd fold/unfold congs:" (fn () =>
  2141     val (fold_congs, unfold_congs) = timeit_msg ctxt "record upd fold/unfold congs:" (fn () =>
  2145       let
  2142       let
  2146         val symdefs = map Thm.symmetric (sel_defs @ upd_defs);
  2143         val symdefs = map Thm.symmetric (sel_defs @ upd_defs);
  2147         val fold_ss = HOL_basic_ss addsimps symdefs;
  2144         val fold_ss = HOL_basic_ss addsimps symdefs;
  2181 
  2178 
  2182     val cases = timeit_msg ctxt "record cases proof:" (fn () =>
  2179     val cases = timeit_msg ctxt "record cases proof:" (fn () =>
  2183       prove [] [] cases_prop
  2180       prove [] [] cases_prop
  2184         (fn _ =>
  2181         (fn _ =>
  2185           try_param_tac rN cases_scheme 1 THEN
  2182           try_param_tac rN cases_scheme 1 THEN
  2186           simp_all_tac HOL_basic_ss [@{thm unit_all_eq1}]));
  2183           ALLGOALS (asm_full_simp_tac (HOL_basic_ss addsimps [@{thm unit_all_eq1}]))));
  2187 
  2184 
  2188     val surjective = timeit_msg ctxt "record surjective proof:" (fn () =>
  2185     val surjective = timeit_msg ctxt "record surjective proof:" (fn () =>
  2189       let
  2186       let
  2190         val leaf_ss =
  2187         val leaf_ss =
  2191           get_sel_upd_defs defs_thy addsimps (sel_defs @ @{thms o_assoc id_apply id_o o_id});
  2188           get_sel_upd_defs defs_thy addsimps (sel_defs @ @{thms o_assoc id_apply id_o o_id});
  2257         fn st =>
  2254         fn st =>
  2258           let val [s, s'] = map #1 (rev (Tactic.innermost_params 1 st)) in
  2255           let val [s, s'] = map #1 (rev (Tactic.innermost_params 1 st)) in
  2259             st |> (res_inst_tac context [((rN, 0), s)] cases_scheme 1 THEN
  2256             st |> (res_inst_tac context [((rN, 0), s)] cases_scheme 1 THEN
  2260               res_inst_tac context [((rN, 0), s')] cases_scheme 1 THEN
  2257               res_inst_tac context [((rN, 0), s')] cases_scheme 1 THEN
  2261               Subgoal.FOCUS (fn {prems, ...} => equality_tac prems) context 1)
  2258               Subgoal.FOCUS (fn {prems, ...} => equality_tac prems) context 1)
  2262              (*simp_all_tac ss (sel_convs) would also work but is less efficient*)
  2259              (*simp_defs_tac would also work but is less efficient*)
  2263           end));
  2260           end));
  2264 
  2261 
  2265     val ((([sel_convs', upd_convs', sel_defs', upd_defs',
  2262     val ((([sel_convs', upd_convs', sel_defs', upd_defs',
  2266             fold_congs', unfold_congs',
  2263             fold_congs', unfold_congs',
  2267           splits' as [split_meta', split_object', split_ex'], derived_defs'],
  2264           splits' as [split_meta', split_object', split_ex'], derived_defs'],