src/HOL/Tools/Nitpick/nitpick_hol.ML
changeset 33556 cba22e2999d5
parent 33232 f93390060bbe
child 33557 107f3df799f6
equal deleted inserted replaced
33233:f9ff11344ec4 33556:cba22e2999d5
   123   val ersatz_table : theory -> (string * string) list
   123   val ersatz_table : theory -> (string * string) list
   124   val def_of_const : theory -> const_table -> styp -> term option
   124   val def_of_const : theory -> const_table -> styp -> term option
   125   val is_inductive_pred : extended_context -> styp -> bool
   125   val is_inductive_pred : extended_context -> styp -> bool
   126   val is_constr_pattern_lhs : theory -> term -> bool
   126   val is_constr_pattern_lhs : theory -> term -> bool
   127   val is_constr_pattern_formula : theory -> term -> bool
   127   val is_constr_pattern_formula : theory -> term -> bool
   128   val coalesce_type_vars_in_terms : term list -> term list
   128   val merge_type_vars_in_terms : term list -> term list
   129   val ground_types_in_type : extended_context -> typ -> typ list
   129   val ground_types_in_type : extended_context -> typ -> typ list
   130   val ground_types_in_terms : extended_context -> term list -> typ list
   130   val ground_types_in_terms : extended_context -> term list -> typ list
   131   val format_type : int list -> int list -> typ -> typ
   131   val format_type : int list -> int list -> typ -> typ
   132   val format_term_type :
   132   val format_term_type :
   133     theory -> const_table -> (term option * int list) list -> term -> typ
   133     theory -> const_table -> (term option * int list) list -> term -> typ
  1014       | do_eq (@{const Trueprop} $ (Const (@{const_name "op ="}, _) $ t1 $ _)) =
  1014       | do_eq (@{const Trueprop} $ (Const (@{const_name "op ="}, _) $ t1 $ _)) =
  1015         do_lhs t1
  1015         do_lhs t1
  1016       | do_eq _ = false
  1016       | do_eq _ = false
  1017   in do_eq end
  1017   in do_eq end
  1018 
  1018 
  1019 (* This table is not pretty. A better approach would be to avoid expanding the
       
  1020    operators to their low-level definitions, but this would require dealing with
       
  1021    overloading. *)
       
  1022 val built_in_built_in_defs =
       
  1023   [@{thm div_int_inst.div_int}, @{thm div_int_inst.mod_int},
       
  1024    @{thm div_nat_inst.div_nat}, @{thm div_nat_inst.mod_nat},
       
  1025    @{thm lower_semilattice_fun_inst.inf_fun}, @{thm minus_fun_inst.minus_fun},
       
  1026    @{thm minus_int_inst.minus_int}, @{thm minus_nat_inst.minus_nat},
       
  1027    @{thm one_int_inst.one_int}, @{thm one_nat_inst.one_nat},
       
  1028    @{thm ord_fun_inst.less_eq_fun}, @{thm ord_int_inst.less_eq_int},
       
  1029    @{thm ord_int_inst.less_int}, @{thm ord_nat_inst.less_eq_nat},
       
  1030    @{thm ord_nat_inst.less_nat}, @{thm plus_int_inst.plus_int},
       
  1031    @{thm plus_nat_inst.plus_nat}, @{thm times_int_inst.times_int},
       
  1032    @{thm times_nat_inst.times_nat}, @{thm uminus_int_inst.uminus_int},
       
  1033    @{thm upper_semilattice_fun_inst.sup_fun}, @{thm zero_int_inst.zero_int},
       
  1034    @{thm zero_nat_inst.zero_nat}]
       
  1035   |> map prop_of
       
  1036 
       
  1037 (* theory -> term list * term list * term list *)
  1019 (* theory -> term list * term list * term list *)
  1038 fun all_axioms_of thy =
  1020 fun all_axioms_of thy =
  1039   let
  1021   let
  1040     (* theory list -> term list *)
  1022     (* theory list -> term list *)
  1041     val axioms_of_thys = maps Thm.axioms_of #> map (apsnd prop_of)
  1023     val axioms_of_thys = maps Thm.axioms_of #> map (apsnd prop_of)
  1052     val (user_defs, user_nondefs) =
  1034     val (user_defs, user_nondefs) =
  1053       partition_axioms_by_definitionality thy user_axioms def_names
  1035       partition_axioms_by_definitionality thy user_axioms def_names
  1054     val (built_in_nondefs, user_nondefs) =
  1036     val (built_in_nondefs, user_nondefs) =
  1055       List.partition (is_typedef_axiom thy false) user_nondefs
  1037       List.partition (is_typedef_axiom thy false) user_nondefs
  1056       |>> append built_in_nondefs
  1038       |>> append built_in_nondefs
  1057     val defs = built_in_built_in_defs @
  1039     val defs = (thy |> PureThy.all_thms_of
  1058                (thy |> PureThy.all_thms_of
       
  1059                     |> filter (equal Thm.definitionK o Thm.get_kind o snd)
  1040                     |> filter (equal Thm.definitionK o Thm.get_kind o snd)
  1060                     |> map (prop_of o snd) |> filter is_plain_definition) @
  1041                     |> map (prop_of o snd) |> filter is_plain_definition) @
  1061                user_defs @ built_in_defs
  1042                user_defs @ built_in_defs
  1062   in (defs, built_in_nondefs, user_nondefs) end
  1043   in (defs, built_in_nondefs, user_nondefs) end
  1063 
  1044 
  1307     constr_case_body thy (1, x)
  1288     constr_case_body thy (1, x)
  1308     |> fold_rev (add_constr_case thy res_T) (length xs downto 2 ~~ xs')
  1289     |> fold_rev (add_constr_case thy res_T) (length xs downto 2 ~~ xs')
  1309     |> fold_rev (curry absdummy) (func_Ts @ [dataT])
  1290     |> fold_rev (curry absdummy) (func_Ts @ [dataT])
  1310   end
  1291   end
  1311 
  1292 
  1312 val redefined_in_Nitpick_thy =
       
  1313   [@{const_name option_case}, @{const_name nat_case}, @{const_name list_case},
       
  1314    @{const_name list_size}]
       
  1315 
       
  1316 (* theory -> string -> typ -> typ -> term -> term *)
  1293 (* theory -> string -> typ -> typ -> term -> term *)
  1317 fun optimized_record_get thy s rec_T res_T t =
  1294 fun optimized_record_get thy s rec_T res_T t =
  1318   let val constr_x = the_single (datatype_constrs thy rec_T) in
  1295   let val constr_x = the_single (datatype_constrs thy rec_T) in
  1319     case no_of_record_field thy s rec_T of
  1296     case no_of_record_field thy s rec_T of
  1320       ~1 => (case rec_T of
  1297       ~1 => (case rec_T of
  1380   is_real_inductive_pred ext_ctxt andf (not o is_real_equational_fun ext_ctxt)
  1357   is_real_inductive_pred ext_ctxt andf (not o is_real_equational_fun ext_ctxt)
  1381 fun is_equational_fun (ext_ctxt as {thy, def_table, ...}) =
  1358 fun is_equational_fun (ext_ctxt as {thy, def_table, ...}) =
  1382   (is_real_equational_fun ext_ctxt orf is_real_inductive_pred ext_ctxt
  1359   (is_real_equational_fun ext_ctxt orf is_real_inductive_pred ext_ctxt
  1383    orf (String.isPrefix ubfp_prefix orf String.isPrefix lbfp_prefix) o fst)
  1360    orf (String.isPrefix ubfp_prefix orf String.isPrefix lbfp_prefix) o fst)
  1384   andf (not o has_trivial_definition thy def_table)
  1361   andf (not o has_trivial_definition thy def_table)
  1385   andf (not o member (op =) redefined_in_Nitpick_thy o fst)
       
  1386 
  1362 
  1387 (* term * term -> term *)
  1363 (* term * term -> term *)
  1388 fun s_betapply (Const (@{const_name If}, _) $ @{const True} $ t, _) = t
  1364 fun s_betapply (Const (@{const_name If}, _) $ @{const True} $ t, _) = t
  1389   | s_betapply (Const (@{const_name If}, _) $ @{const False} $ _, t) = t
  1365   | s_betapply (Const (@{const_name If}, _) $ @{const False} $ _, t) = t
  1390   | s_betapply p = betapply p
  1366   | s_betapply p = betapply p
  1877     raw_inductive_pred_axiom ext_ctxt x
  1853     raw_inductive_pred_axiom ext_ctxt x
  1878 
  1854 
  1879 (* extended_context -> styp -> term list *)
  1855 (* extended_context -> styp -> term list *)
  1880 fun raw_equational_fun_axioms (ext_ctxt as {thy, fast_descrs, simp_table,
  1856 fun raw_equational_fun_axioms (ext_ctxt as {thy, fast_descrs, simp_table,
  1881                                             psimp_table, ...}) (x as (s, _)) =
  1857                                             psimp_table, ...}) (x as (s, _)) =
  1882   if s mem redefined_in_Nitpick_thy then
  1858   case def_props_for_const thy fast_descrs (!simp_table) x of
  1883     []
       
  1884   else case def_props_for_const thy fast_descrs (!simp_table) x of
       
  1885     [] => (case def_props_for_const thy fast_descrs psimp_table x of
  1859     [] => (case def_props_for_const thy fast_descrs psimp_table x of
  1886              [] => [inductive_pred_axiom ext_ctxt x]
  1860              [] => [inductive_pred_axiom ext_ctxt x]
  1887            | psimps => psimps)
  1861            | psimps => psimps)
  1888   | simps => simps
  1862   | simps => simps
  1889 
  1863 
  1890 val equational_fun_axioms = map extensionalize oo raw_equational_fun_axioms
  1864 val equational_fun_axioms = map extensionalize oo raw_equational_fun_axioms
  1891 
  1865 
  1892 (* term list -> term list *)
  1866 (* term list -> term list *)
  1893 fun coalesce_type_vars_in_terms ts =
  1867 fun merge_type_vars_in_terms ts =
  1894   let
  1868   let
  1895     (* typ -> (sort * string) list -> (sort * string) list *)
  1869     (* typ -> (sort * string) list -> (sort * string) list *)
  1896     fun add_type (TFree (s, S)) table =
  1870     fun add_type (TFree (s, S)) table =
  1897         (case AList.lookup (op =) table S of
  1871         (case AList.lookup (op =) table S of
  1898            SOME s' =>
  1872            SOME s' =>