src/HOL/Tools/record_package.ML
changeset 17510 5e3ce025e1a5
parent 17485 c39871c52977
child 17600 9ae09014730c
equal deleted inserted replaced
17509:054cd8972095 17510:5e3ce025e1a5
   313 
   313 
   314 (* access 'sel_upd' *)
   314 (* access 'sel_upd' *)
   315 
   315 
   316 val get_sel_upd = #sel_upd o RecordsData.get;
   316 val get_sel_upd = #sel_upd o RecordsData.get;
   317 
   317 
   318 val get_selectors = Symtab.lookup o #selectors o get_sel_upd;
   318 val is_selector = Symtab.defined o #selectors o get_sel_upd;
   319 fun is_selector sg name = is_some (get_selectors sg (Sign.intern_const sg name));
       
   320 
       
   321 val get_updates = Symtab.lookup o #updates o get_sel_upd;
   319 val get_updates = Symtab.lookup o #updates o get_sel_upd;
   322 val get_simpset = #simpset o get_sel_upd;
   320 val get_simpset = #simpset o get_sel_upd;
   323 
   321 
   324 fun put_sel_upd names simps thy =
   322 fun put_sel_upd names simps thy =
   325   let
   323   let
   849  *   X does not affect the selected subrecord.
   847  *   X does not affect the selected subrecord.
   850  * - If X is a more-selector we have to make sure that S is not in the updated
   848  * - If X is a more-selector we have to make sure that S is not in the updated
   851  *   subrecord.
   849  *   subrecord.
   852  *)
   850  *)
   853 val record_simproc =
   851 val record_simproc =
   854   Simplifier.simproc (Theory.sign_of HOL.thy) "record_simp" ["s (u k r)"]
   852   Simplifier.simproc HOL.thy "record_simp" ["s (u k r)"]   (* FIXME pattern!? *)
   855     (fn sg => fn ss => fn t =>
   853     (fn sg => fn ss => fn t =>
   856       (case t of (sel as Const (s, Type (_,[domS,rangeS])))$
   854       (case t of (sel as Const (s, Type (_,[domS,rangeS])))$
   857                    ((upd as Const (u,Type(_,[_,Type (_,[rT,_])]))) $ k $ r)=>
   855                    ((upd as Const (u,Type(_,[_,Type (_,[rT,_])]))) $ k $ r)=>
   858         (case get_selectors sg s of SOME () =>
   856         if is_selector sg s then
   859           (case get_updates sg u of SOME u_name =>
   857           (case get_updates sg u of SOME u_name =>
   860             let
   858             let
   861               val {sel_upd={updates,...},extfields,...} = RecordsData.get sg;
   859               val {sel_upd={updates,...},extfields,...} = RecordsData.get sg;
   862 
   860 
   863               fun mk_eq_terms ((upd as Const (u,Type(_,[kT,_]))) $ k $ r) =
   861               fun mk_eq_terms ((upd as Const (u,Type(_,[kT,_]))) $ k $ r) =
   897                     else SOME (prove_split_simp sg ss domS
   895                     else SOME (prove_split_simp sg ss domS
   898                                  (list_all(vars,(equals rangeS$(sel$trm)$(sel$trm')))))
   896                                  (list_all(vars,(equals rangeS$(sel$trm)$(sel$trm')))))
   899                | NONE => NONE)
   897                | NONE => NONE)
   900             end
   898             end
   901           | NONE => NONE)
   899           | NONE => NONE)
   902         | NONE => NONE)
   900         else NONE
   903       | _ => NONE));
   901       | _ => NONE));
   904 
   902 
   905 (* record_upd_simproc *)
   903 (* record_upd_simproc *)
   906 (* simplify multiple updates:
   904 (* simplify multiple updates:
   907  *  (1)  "r(|M:=3,N:=1,M:=2,N:=4|) == r(|M:=2,N:=4|)"
   905  *  (1)  "r(|M:=3,N:=1,M:=2,N:=4|) == r(|M:=2,N:=4|)"
   910  *    r(|more := m; A := A r|)
   908  *    r(|more := m; A := A r|)
   911  * If A is contained in the fields of m we cannot remove the update A := A r!
   909  * If A is contained in the fields of m we cannot remove the update A := A r!
   912  * (But r(|more := r; A := A (r(|more := r|))|) = r(|more := r|)
   910  * (But r(|more := r; A := A (r(|more := r|))|) = r(|more := r|)
   913 *)
   911 *)
   914 val record_upd_simproc =
   912 val record_upd_simproc =
   915   Simplifier.simproc (Theory.sign_of HOL.thy) "record_upd_simp" ["(u k r)"]
   913   Simplifier.simproc HOL.thy "record_upd_simp" ["u k r"]    (* FIXME pattern *)
   916     (fn sg => fn ss => fn t =>
   914     (fn sg => fn ss => fn t =>
   917       (case t of ((upd as Const (u, Type(_,[_,Type(_,[rT,_])]))) $ k $ r) =>
   915       (case t of ((upd as Const (u, Type(_,[_,Type(_,[rT,_])]))) $ k $ r) =>
   918          let datatype ('a,'b) calc = Init of 'b | Inter of 'a
   916          let datatype ('a,'b) calc = Init of 'b | Inter of 'a
   919              val {sel_upd={selectors,updates,...},extfields,...} = RecordsData.get sg;
   917              val {sel_upd={selectors,updates,...},extfields,...} = RecordsData.get sg;
   920 
   918 
  1023  *               record_eq_simproc       record_split_simp_tac
  1021  *               record_eq_simproc       record_split_simp_tac
  1024  * Complexity: #components * #updates     #updates
  1022  * Complexity: #components * #updates     #updates
  1025  *
  1023  *
  1026  *)
  1024  *)
  1027 val record_eq_simproc =
  1025 val record_eq_simproc =
  1028   Simplifier.simproc (Theory.sign_of HOL.thy) "record_eq_simp" ["r = s"]
  1026   Simplifier.simproc HOL.thy "record_eq_simp" ["r = s"]
  1029     (fn sg => fn _ => fn t =>
  1027     (fn sg => fn _ => fn t =>
  1030       (case t of Const ("op =", Type (_, [T, _])) $ _ $ _ =>
  1028       (case t of Const ("op =", Type (_, [T, _])) $ _ $ _ =>
  1031         (case rec_id (~1) T of
  1029         (case rec_id (~1) T of
  1032            "" => NONE
  1030            "" => NONE
  1033          | name => (case get_equalities sg name of
  1031          | name => (case get_equalities sg name of
  1041  * P t = 0: do not split
  1039  * P t = 0: do not split
  1042  * P t = ~1: completely split
  1040  * P t = ~1: completely split
  1043  * P t > 0: split up to given bound of record extensions
  1041  * P t > 0: split up to given bound of record extensions
  1044  *)
  1042  *)
  1045 fun record_split_simproc P =
  1043 fun record_split_simproc P =
  1046   Simplifier.simproc (Theory.sign_of HOL.thy) "record_split_simp" ["(a t)"]
  1044   Simplifier.simproc HOL.thy "record_split_simp" ["a t"]
  1047     (fn sg => fn _ => fn t =>
  1045     (fn sg => fn _ => fn t =>
  1048       (case t of (Const (quantifier, Type (_, [Type (_, [T, _]), _])))$trm =>
  1046       (case t of (Const (quantifier, Type (_, [Type (_, [T, _]), _])))$trm =>
  1049          if quantifier = "All" orelse quantifier = "all" orelse quantifier = "Ex"
  1047          if quantifier = "All" orelse quantifier = "all" orelse quantifier = "Ex"
  1050          then (case rec_id (~1) T of
  1048          then (case rec_id (~1) T of
  1051                  "" => NONE
  1049                  "" => NONE
  1064                       end)
  1062                       end)
  1065          else NONE
  1063          else NONE
  1066        | _ => NONE))
  1064        | _ => NONE))
  1067 
  1065 
  1068 val record_ex_sel_eq_simproc =
  1066 val record_ex_sel_eq_simproc =
  1069   Simplifier.simproc (Theory.sign_of HOL.thy) "record_ex_sel_eq_simproc" ["Ex t"]
  1067   Simplifier.simproc HOL.thy "record_ex_sel_eq_simproc" ["Ex t"]
  1070     (fn sg => fn ss => fn t =>
  1068     (fn sg => fn ss => fn t =>
  1071        let
  1069        let
  1072          fun prove prop =
  1070          fun prove prop =
  1073            quick_and_dirty_prove true sg [] prop
  1071            quick_and_dirty_prove true sg [] prop
  1074              (fn _ => simp_tac (Simplifier.inherit_bounds ss (get_simpset sg)
  1072              (fn _ => simp_tac (Simplifier.inherit_bounds ss (get_simpset sg)
  1075                addsimps simp_thms addsimprocs [record_split_simproc (K ~1)]) 1);
  1073                addsimps simp_thms addsimprocs [record_split_simproc (K ~1)]) 1);
  1076 
  1074 
  1077          fun mkeq (lr,Teq,(sel,Tsel),x) i =
  1075          fun mkeq (lr,Teq,(sel,Tsel),x) i =
  1078               (case get_selectors sg sel of SOME () =>
  1076               if is_selector sg sel then
  1079                  let val x' = if not (loose_bvar1 (x,0))
  1077                  let val x' = if not (loose_bvar1 (x,0))
  1080                               then Free ("x" ^ string_of_int i, range_type Tsel)
  1078                               then Free ("x" ^ string_of_int i, range_type Tsel)
  1081                               else raise TERM ("",[x]);
  1079                               else raise TERM ("",[x]);
  1082                      val sel' = Const (sel,Tsel)$Bound 0;
  1080                      val sel' = Const (sel,Tsel)$Bound 0;
  1083                      val (l,r) = if lr then (sel',x') else (x',sel');
  1081                      val (l,r) = if lr then (sel',x') else (x',sel');
  1084                   in Const ("op =",Teq)$l$r end
  1082                   in Const ("op =",Teq)$l$r end
  1085                | NONE => raise TERM ("",[Const (sel,Tsel)]));
  1083               else raise TERM ("",[Const (sel,Tsel)]);
  1086 
  1084 
  1087          fun dest_sel_eq (Const ("op =",Teq)$(Const (sel,Tsel)$Bound 0)$X) =
  1085          fun dest_sel_eq (Const ("op =",Teq)$(Const (sel,Tsel)$Bound 0)$X) =
  1088                            (true,Teq,(sel,Tsel),X)
  1086                            (true,Teq,(sel,Tsel),X)
  1089            | dest_sel_eq (Const ("op =",Teq)$X$(Const (sel,Tsel)$Bound 0)) =
  1087            | dest_sel_eq (Const ("op =",Teq)$X$(Const (sel,Tsel)$Bound 0)) =
  1090                            (false,Teq,(sel,Tsel),X)
  1088                            (false,Teq,(sel,Tsel),X)
  1232  * avoid problems with higher order unification.
  1230  * avoid problems with higher order unification.
  1233  *)
  1231  *)
  1234 
  1232 
  1235 fun try_param_tac s rule i st =
  1233 fun try_param_tac s rule i st =
  1236   let
  1234   let
  1237     val cert = cterm_of (Thm.sign_of_thm st);
  1235     val cert = cterm_of (Thm.theory_of_thm st);
  1238     val g = List.nth (prems_of st, i - 1);
  1236     val g = List.nth (prems_of st, i - 1);
  1239     val params = Logic.strip_params g;
  1237     val params = Logic.strip_params g;
  1240     val concl = HOLogic.dest_Trueprop (Logic.strip_assums_concl g);
  1238     val concl = HOLogic.dest_Trueprop (Logic.strip_assums_concl g);
  1241     val rule' = Thm.lift_rule (st, i) rule;
  1239     val rule' = Thm.lift_rule (st, i) rule;
  1242     val (P, ys) = strip_comb (HOLogic.dest_Trueprop
  1240     val (P, ys) = strip_comb (HOLogic.dest_Trueprop
  1408       let val P = Free (variant variants "P", extT-->Term.propT) in
  1406       let val P = Free (variant variants "P", extT-->Term.propT) in
  1409         Logic.mk_equals
  1407         Logic.mk_equals
  1410          (All [dest_Free s] (P $ s), All (map dest_Free vars_more) (P $ ext))
  1408          (All [dest_Free s] (P $ s), All (map dest_Free vars_more) (P $ ext))
  1411       end;
  1409       end;
  1412 
  1410 
  1413     fun prove stndrd = quick_and_dirty_prove stndrd (Theory.sign_of defs_thy);
  1411     fun prove stndrd = quick_and_dirty_prove stndrd defs_thy;
  1414     val prove_standard = quick_and_dirty_prove true (Theory.sign_of defs_thy);
  1412     val prove_standard = quick_and_dirty_prove true defs_thy;
  1415     fun prove_simp stndrd simps =
  1413     fun prove_simp stndrd simps =
  1416       let val tac = simp_all_tac HOL_ss simps
  1414       let val tac = simp_all_tac HOL_ss simps
  1417       in fn prop => prove stndrd [] prop (K tac) end;
  1415       in fn prop => prove stndrd [] prop (K tac) end;
  1418 
  1416 
  1419     fun inject_prf () = (prove_simp true [ext_def,abs_inject,Pair_eq] inject_prop);
  1417     fun inject_prf () = (prove_simp true [ext_def,abs_inject,Pair_eq] inject_prop);
  1829         val seleqs = map mk_sel_eq all_named_vars_more
  1827         val seleqs = map mk_sel_eq all_named_vars_more
  1830       in All (map dest_Free [r0,s']) (Logic.list_implies (seleqs,r0 === s')) end;
  1828       in All (map dest_Free [r0,s']) (Logic.list_implies (seleqs,r0 === s')) end;
  1831 
  1829 
  1832     (* 3rd stage: thms_thy *)
  1830     (* 3rd stage: thms_thy *)
  1833 
  1831 
  1834     fun prove stndrd = quick_and_dirty_prove stndrd (Theory.sign_of defs_thy);
  1832     fun prove stndrd = quick_and_dirty_prove stndrd defs_thy;
  1835     val prove_standard = quick_and_dirty_prove true (Theory.sign_of defs_thy);
  1833     val prove_standard = quick_and_dirty_prove true defs_thy;
  1836 
  1834 
  1837     fun prove_simp stndrd ss simps =
  1835     fun prove_simp stndrd ss simps =
  1838       let val tac = simp_all_tac ss simps
  1836       let val tac = simp_all_tac ss simps
  1839       in fn prop => prove stndrd [] prop (K tac) end;
  1837       in fn prop => prove stndrd [] prop (K tac) end;
  1840 
  1838 
  1841     val ss = get_simpset (sign_of defs_thy);
  1839     val ss = get_simpset defs_thy;
  1842 
  1840 
  1843     fun sel_convs_prf () = map (prove_simp false ss
  1841     fun sel_convs_prf () = map (prove_simp false ss
  1844                            (sel_defs@ext_dest_convs)) sel_conv_props;
  1842                            (sel_defs@ext_dest_convs)) sel_conv_props;
  1845     val sel_convs = timeit_msg "record sel_convs proof:" sel_convs_prf;
  1843     val sel_convs = timeit_msg "record sel_convs proof:" sel_convs_prf;
  1846     fun sel_convs_standard_prf () = map standard sel_convs
  1844     fun sel_convs_standard_prf () = map standard sel_convs