src/HOL/Tools/Nitpick/nitpick_preproc.ML
author nipkow
Thu, 26 Jun 2025 17:30:33 +0200
changeset 82772 59b937edcff8
parent 80634 a90ab1ea6458
permissions -rw-r--r--
merged
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
     1
(*  Title:      HOL/Tools/Nitpick/nitpick_preproc.ML
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
     2
    Author:     Jasmin Blanchette, TU Muenchen
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
     3
    Copyright   2008, 2009, 2010
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
     4
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
     5
Nitpick's HOL preprocessor.
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
     6
*)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
     7
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
     8
signature NITPICK_PREPROC =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
     9
sig
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
    10
  type hol_context = Nitpick_HOL.hol_context
38169
b51784515471 optimize local "def"s by treating them as definitions
blanchet
parents: 38166
diff changeset
    11
  val preprocess_formulas :
41052
3db267a01c1d remove the "fin_fun" optimization in Nitpick -- it was always a hack and didn't help much
blanchet
parents: 41001
diff changeset
    12
    hol_context -> term list -> term
41803
ef13e3b7cbaf more work on "fix_datatype_vals" optimization (renamed "preconstruct")
blanchet
parents: 41793
diff changeset
    13
    -> term list * term list * term list * bool * bool * bool
35866
513074557e06 move the Sledgehammer Isar commands together into one file;
blanchet
parents: 35807
diff changeset
    14
end;
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
    15
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
    16
structure Nitpick_Preproc : NITPICK_PREPROC =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
    17
struct
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
    18
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
    19
open Nitpick_Util
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
    20
open Nitpick_HOL
35665
ff2bf50505ab added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents: 35408
diff changeset
    21
open Nitpick_Mono
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
    22
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
    23
fun is_positive_existential polar quant_s =
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
    24
  (polar = Pos andalso quant_s = \<^const_name>\<open>Ex\<close>) orelse
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
    25
  (polar = Neg andalso quant_s <> \<^const_name>\<open>Ex\<close>)
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
    26
37271
694aebcd602b fix Nitpick soundness bug regarding The and Eps
blanchet
parents: 37260
diff changeset
    27
val is_descr =
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
    28
  member (op =) [\<^const_name>\<open>The\<close>, \<^const_name>\<open>Eps\<close>, \<^const_name>\<open>safe_The\<close>]
37271
694aebcd602b fix Nitpick soundness bug regarding The and Eps
blanchet
parents: 37260
diff changeset
    29
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
    30
(** Binary coding of integers **)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
    31
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
    32
(* If a formula contains a numeral whose absolute value is more than this
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
    33
   threshold, the unary coding is likely not to work well and we prefer the
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
    34
   binary coding. *)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
    35
val binary_int_threshold = 3
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
    36
35718
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35672
diff changeset
    37
val may_use_binary_ints =
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35672
diff changeset
    38
  let
74380
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
    39
    fun aux def \<^Const_>\<open>Pure.eq _ for t1 t2\<close> = aux def t1 andalso aux false t2
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
    40
      | aux def \<^Const_>\<open>Pure.imp for t1 t2\<close> = aux false t1 andalso aux def t2
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
    41
      | aux def \<^Const_>\<open>HOL.eq _ for t1 t2\<close> = aux def t1 andalso aux false t2
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
    42
      | aux def \<^Const_>\<open>implies for t1 t2\<close> = aux false t1 andalso aux def t2
35718
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35672
diff changeset
    43
      | aux def (t1 $ t2) = aux def t1 andalso aux def t2
74380
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
    44
      | aux def (t as Const (s, _)) = (not def orelse t <> \<^Const>\<open>Suc\<close>) andalso
47909
5f1afeebafbc fixed "real" after they were redefined as a 'quotient_type'
blanchet
parents: 47754
diff changeset
    45
        not (member (op =)
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
    46
               [\<^const_name>\<open>Abs_Frac\<close>, \<^const_name>\<open>Rep_Frac\<close>,
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
    47
                \<^const_name>\<open>nat_gcd\<close>, \<^const_name>\<open>nat_lcm\<close>,
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
    48
                \<^const_name>\<open>Frac\<close>, \<^const_name>\<open>norm_frac\<close>] s)
35718
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35672
diff changeset
    49
      | aux def (Abs (_, _, t')) = aux def t'
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35672
diff changeset
    50
      | aux _ _ = true
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35672
diff changeset
    51
  in aux end
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35672
diff changeset
    52
val should_use_binary_ints =
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35672
diff changeset
    53
  let
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35672
diff changeset
    54
    fun aux (t1 $ t2) = aux t1 orelse aux t2
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35672
diff changeset
    55
      | aux (Const (s, T)) =
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
    56
        ((s = \<^const_name>\<open>times\<close> orelse s = \<^const_name>\<open>Rings.divide\<close>) andalso
35718
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35672
diff changeset
    57
         is_integer_type (body_type T)) orelse
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35672
diff changeset
    58
        (String.isPrefix numeral_prefix s andalso
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35672
diff changeset
    59
         let val n = the (Int.fromString (unprefix numeral_prefix s)) in
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35672
diff changeset
    60
           n < ~ binary_int_threshold orelse n > binary_int_threshold
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35672
diff changeset
    61
         end)
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35672
diff changeset
    62
      | aux (Abs (_, _, t')) = aux t'
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35672
diff changeset
    63
      | aux _ = false
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35672
diff changeset
    64
  in aux end
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
    65
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
    66
(** Uncurrying **)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
    67
37256
0dca1ec52999 thread along context instead of theory for typedef lookup
blanchet
parents: 37253
diff changeset
    68
fun add_to_uncurry_table ctxt t =
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
    69
  let
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
    70
    fun aux (t1 $ t2) args table =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
    71
        let val table = aux t2 [] table in aux t1 (t2 :: args) table end
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
    72
      | aux (Abs (_, _, t')) _ table = aux t' [] table
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
    73
      | aux (t as Const (x as (s, _))) args table =
55889
6bfbec3dff62 tuned code
blanchet
parents: 55888
diff changeset
    74
        if is_built_in_const x orelse is_nonfree_constr ctxt x orelse
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
    75
           is_sel s orelse s = \<^const_name>\<open>Sigma\<close> then
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
    76
          table
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
    77
        else
36384
76d5fd5a45fb cosmetics
blanchet
parents: 35866
diff changeset
    78
          Termtab.map_default (t, 65536) (Integer.min (length args)) table
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
    79
      | aux _ _ table = table
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
    80
  in aux t [] end
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
    81
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
    82
fun uncurry_prefix_for k j =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
    83
  uncurry_prefix ^ string_of_int k ^ "@" ^ string_of_int j ^ name_sep
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
    84
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
    85
fun uncurry_term table t =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
    86
  let
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
    87
    fun aux (t1 $ t2) args = aux t1 (aux t2 [] :: args)
37476
0681e46b4022 optimized code generated for datatype cases + more;
blanchet
parents: 37271
diff changeset
    88
      | aux (Abs (s, T, t')) args = s_betapplys [] (Abs (s, T, aux t' []), args)
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
    89
      | aux (t as Const (s, T)) args =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
    90
        (case Termtab.lookup table t of
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
    91
           SOME n =>
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
    92
           if n >= 2 then
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
    93
             let
35280
54ab4921f826 fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents: 35220
diff changeset
    94
               val arg_Ts = strip_n_binders n T |> fst
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
    95
               val j =
35280
54ab4921f826 fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents: 35220
diff changeset
    96
                 if is_iterator_type (hd arg_Ts) then
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
    97
                   1
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
    98
                 else case find_index (not_equal bool_T) arg_Ts of
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
    99
                   ~1 => n
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   100
                 | j => j
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   101
               val ((before_args, tuple_args), after_args) =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   102
                 args |> chop n |>> chop j
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   103
               val ((before_arg_Ts, tuple_arg_Ts), rest_T) =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   104
                 T |> strip_n_binders n |>> chop j
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   105
               val tuple_T = HOLogic.mk_tupleT tuple_arg_Ts
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   106
             in
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   107
               if n - j < 2 then
37476
0681e46b4022 optimized code generated for datatype cases + more;
blanchet
parents: 37271
diff changeset
   108
                 s_betapplys [] (t, args)
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   109
               else
37476
0681e46b4022 optimized code generated for datatype cases + more;
blanchet
parents: 37271
diff changeset
   110
                 s_betapplys []
0681e46b4022 optimized code generated for datatype cases + more;
blanchet
parents: 37271
diff changeset
   111
                     (Const (uncurry_prefix_for (n - j) j ^ s,
0681e46b4022 optimized code generated for datatype cases + more;
blanchet
parents: 37271
diff changeset
   112
                             before_arg_Ts ---> tuple_T --> rest_T),
0681e46b4022 optimized code generated for datatype cases + more;
blanchet
parents: 37271
diff changeset
   113
                      before_args @ [mk_flat_tuple tuple_T tuple_args] @
0681e46b4022 optimized code generated for datatype cases + more;
blanchet
parents: 37271
diff changeset
   114
                      after_args)
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   115
             end
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   116
           else
37476
0681e46b4022 optimized code generated for datatype cases + more;
blanchet
parents: 37271
diff changeset
   117
             s_betapplys [] (t, args)
0681e46b4022 optimized code generated for datatype cases + more;
blanchet
parents: 37271
diff changeset
   118
         | NONE => s_betapplys [] (t, args))
0681e46b4022 optimized code generated for datatype cases + more;
blanchet
parents: 37271
diff changeset
   119
      | aux t args = s_betapplys [] (t, args)
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   120
  in aux t [] end
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   121
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   122
(** Boxing **)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   123
55888
cac1add157e8 removed nonstandard models from Nitpick
blanchet
parents: 51706
diff changeset
   124
fun box_fun_and_pair_in_term (hol_ctxt as {ctxt, ...}) def orig_t =
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   125
  let
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
   126
    fun box_relational_operator_type (Type (\<^type_name>\<open>fun\<close>, Ts)) =
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
   127
        Type (\<^type_name>\<open>fun\<close>, map box_relational_operator_type Ts)
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
   128
      | box_relational_operator_type (Type (\<^type_name>\<open>prod\<close>, Ts)) =
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
   129
        Type (\<^type_name>\<open>prod\<close>, map (box_type hol_ctxt InPair) Ts)
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   130
      | box_relational_operator_type T = T
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   131
    fun add_boxed_types_for_var (z as (_, T)) (T', t') =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   132
      case t' of
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   133
        Var z' => z' = z ? insert (op =) T'
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
   134
      | Const (\<^const_name>\<open>Pair\<close>, _) $ t1 $ t2 =>
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   135
        (case T' of
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   136
           Type (_, [T1, T2]) =>
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   137
           fold (add_boxed_types_for_var z) [(T1, t1), (T2, t2)]
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   138
         | _ => raise TYPE ("Nitpick_Preproc.box_fun_and_pair_in_term.\
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   139
                            \add_boxed_types_for_var", [T'], []))
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   140
      | _ => exists_subterm (curry (op =) (Var z)) t' ? insert (op =) T
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   141
    fun box_var_in_def new_Ts old_Ts t (z as (_, T)) =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   142
      case t of
74380
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
   143
        \<^Const_>\<open>Trueprop for t1\<close> => box_var_in_def new_Ts old_Ts t1 z
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   144
      | Const (s0, _) $ t1 $ _ =>
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
   145
        if s0 = \<^const_name>\<open>Pure.eq\<close> orelse s0 = \<^const_name>\<open>HOL.eq\<close> then
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   146
          let
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   147
            val (t', args) = strip_comb t1
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   148
            val T' = fastype_of1 (new_Ts, do_term new_Ts old_Ts Neut t')
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   149
          in
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   150
            case fold (add_boxed_types_for_var z)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   151
                      (fst (strip_n_binders (length args) T') ~~ args) [] of
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   152
              [T''] => T''
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   153
            | _ => T
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   154
          end
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   155
        else
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   156
          T
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   157
      | _ => T
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   158
    and do_quantifier new_Ts old_Ts polar quant_s quant_T abs_s abs_T t =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   159
      let
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   160
        val abs_T' =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   161
          if polar = Neut orelse is_positive_existential polar quant_s then
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   162
            box_type hol_ctxt InFunLHS abs_T
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   163
          else
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   164
            abs_T
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   165
        val body_T = body_type quant_T
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   166
      in
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   167
        Const (quant_s, (abs_T' --> body_T) --> body_T)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   168
        $ Abs (abs_s, abs_T',
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   169
               t |> do_term (abs_T' :: new_Ts) (abs_T :: old_Ts) polar)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   170
      end
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   171
    and do_equals new_Ts old_Ts s0 T0 t1 t2 =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   172
      let
59058
a78612c67ec0 renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents: 58634
diff changeset
   173
        val (t1, t2) = apply2 (do_term new_Ts old_Ts Neut) (t1, t2)
a78612c67ec0 renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents: 58634
diff changeset
   174
        val (T1, T2) = apply2 (curry fastype_of1 new_Ts) (t1, t2)
a78612c67ec0 renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents: 58634
diff changeset
   175
        val T = if def then T1 else [T1, T2] |> sort (int_ord o apply2 size_of_typ) |> hd
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   176
      in
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   177
        list_comb (Const (s0, T --> T --> body_type T0),
35384
88dbcfe75c45 cosmetics
blanchet
parents: 35312
diff changeset
   178
                   map2 (coerce_term hol_ctxt new_Ts T) [T1, T2] [t1, t2])
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   179
      end
37271
694aebcd602b fix Nitpick soundness bug regarding The and Eps
blanchet
parents: 37260
diff changeset
   180
    and do_descr s T =
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   181
      let val T1 = box_type hol_ctxt InFunLHS (range_type T) in
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   182
        Const (s, (T1 --> bool_T) --> T1)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   183
      end
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   184
    and do_term new_Ts old_Ts polar t =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   185
      case t of
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
   186
        Const (s0 as \<^const_name>\<open>Pure.all\<close>, T0) $ Abs (s1, T1, t1) =>
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   187
        do_quantifier new_Ts old_Ts polar s0 T0 s1 T1 t1
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
   188
      | Const (s0 as \<^const_name>\<open>Pure.eq\<close>, T0) $ t1 $ t2 =>
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   189
        do_equals new_Ts old_Ts s0 T0 t1 t2
74380
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
   190
      | \<^Const_>\<open>Pure.imp for t1 t2\<close> =>
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
   191
        \<^Const>\<open>Pure.imp for \<open>do_term new_Ts old_Ts (flip_polarity polar) t1\<close>
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
   192
          \<open>do_term new_Ts old_Ts polar t2\<close>\<close>
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
   193
      | \<^Const_>\<open>Pure.conjunction for t1 t2\<close> =>
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
   194
        \<^Const>\<open>Pure.conjunction for \<open>do_term new_Ts old_Ts polar t1\<close>
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
   195
          \<open>do_term new_Ts old_Ts polar t2\<close>\<close>
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
   196
      | \<^Const_>\<open>Trueprop for t1\<close> => \<^Const>\<open>Trueprop for \<open>do_term new_Ts old_Ts polar t1\<close>\<close>
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
   197
      | \<^Const_>\<open>Not for t1\<close> => \<^Const>\<open>Not for \<open>do_term new_Ts old_Ts (flip_polarity polar) t1\<close>\<close>
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
   198
      | Const (s0 as \<^const_name>\<open>All\<close>, T0) $ Abs (s1, T1, t1) =>
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   199
        do_quantifier new_Ts old_Ts polar s0 T0 s1 T1 t1
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
   200
      | Const (s0 as \<^const_name>\<open>Ex\<close>, T0) $ Abs (s1, T1, t1) =>
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   201
        do_quantifier new_Ts old_Ts polar s0 T0 s1 T1 t1
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
   202
      | Const (s0 as \<^const_name>\<open>HOL.eq\<close>, T0) $ t1 $ t2 =>
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   203
        do_equals new_Ts old_Ts s0 T0 t1 t2
74380
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
   204
      | \<^Const_>\<open>conj for t1 t2\<close> =>
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
   205
        \<^Const>\<open>conj for \<open>do_term new_Ts old_Ts polar t1\<close>
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
   206
          \<open>do_term new_Ts old_Ts polar t2\<close>\<close>
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
   207
      | \<^Const_>\<open>disj for t1 t2\<close> =>
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
   208
        \<^Const>\<open>disj for \<open>do_term new_Ts old_Ts polar t1\<close>
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
   209
          \<open>do_term new_Ts old_Ts polar t2\<close>\<close>
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
   210
      | \<^Const_>\<open>implies for t1 t2\<close> =>
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
   211
        \<^Const>\<open>implies for \<open>do_term new_Ts old_Ts (flip_polarity polar) t1\<close>
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
   212
          \<open>do_term new_Ts old_Ts polar t2\<close>\<close>
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   213
      | Const (x as (s, T)) =>
37271
694aebcd602b fix Nitpick soundness bug regarding The and Eps
blanchet
parents: 37260
diff changeset
   214
        if is_descr s then
694aebcd602b fix Nitpick soundness bug regarding The and Eps
blanchet
parents: 37260
diff changeset
   215
          do_descr s T
694aebcd602b fix Nitpick soundness bug regarding The and Eps
blanchet
parents: 37260
diff changeset
   216
        else
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
   217
          Const (s, if s = \<^const_name>\<open>converse\<close> orelse
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
   218
                       s = \<^const_name>\<open>trancl\<close> then
37271
694aebcd602b fix Nitpick soundness bug regarding The and Eps
blanchet
parents: 37260
diff changeset
   219
                      box_relational_operator_type T
694aebcd602b fix Nitpick soundness bug regarding The and Eps
blanchet
parents: 37260
diff changeset
   220
                    else if String.isPrefix quot_normal_prefix s then
694aebcd602b fix Nitpick soundness bug regarding The and Eps
blanchet
parents: 37260
diff changeset
   221
                      let val T' = box_type hol_ctxt InFunLHS (domain_type T) in
694aebcd602b fix Nitpick soundness bug regarding The and Eps
blanchet
parents: 37260
diff changeset
   222
                        T' --> T'
694aebcd602b fix Nitpick soundness bug regarding The and Eps
blanchet
parents: 37260
diff changeset
   223
                      end
55888
cac1add157e8 removed nonstandard models from Nitpick
blanchet
parents: 51706
diff changeset
   224
                    else if is_built_in_const x orelse
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
   225
                            s = \<^const_name>\<open>Sigma\<close> then
37271
694aebcd602b fix Nitpick soundness bug regarding The and Eps
blanchet
parents: 37260
diff changeset
   226
                      T
55889
6bfbec3dff62 tuned code
blanchet
parents: 55888
diff changeset
   227
                    else if is_nonfree_constr ctxt x then
37271
694aebcd602b fix Nitpick soundness bug regarding The and Eps
blanchet
parents: 37260
diff changeset
   228
                      box_type hol_ctxt InConstr T
694aebcd602b fix Nitpick soundness bug regarding The and Eps
blanchet
parents: 37260
diff changeset
   229
                    else if is_sel s orelse is_rep_fun ctxt x then
694aebcd602b fix Nitpick soundness bug regarding The and Eps
blanchet
parents: 37260
diff changeset
   230
                      box_type hol_ctxt InSel T
694aebcd602b fix Nitpick soundness bug regarding The and Eps
blanchet
parents: 37260
diff changeset
   231
                    else
694aebcd602b fix Nitpick soundness bug regarding The and Eps
blanchet
parents: 37260
diff changeset
   232
                      box_type hol_ctxt InExpr T)
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   233
      | t1 $ Abs (s, T, t2') =>
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   234
        let
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   235
          val t1 = do_term new_Ts old_Ts Neut t1
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   236
          val T1 = fastype_of1 (new_Ts, t1)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   237
          val (s1, Ts1) = dest_Type T1
80634
a90ab1ea6458 tuned: more explicit dest_Type_name and dest_Type_args;
wenzelm
parents: 79170
diff changeset
   238
          val T' = hd (dest_Type_args (hd Ts1))
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   239
          val t2 = Abs (s, T', do_term (T' :: new_Ts) (T :: old_Ts) Neut t2')
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   240
          val T2 = fastype_of1 (new_Ts, t2)
35384
88dbcfe75c45 cosmetics
blanchet
parents: 35312
diff changeset
   241
          val t2 = coerce_term hol_ctxt new_Ts (hd Ts1) T2 t2
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   242
        in
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
   243
          s_betapply new_Ts (if s1 = \<^type_name>\<open>fun\<close> then
37476
0681e46b4022 optimized code generated for datatype cases + more;
blanchet
parents: 37271
diff changeset
   244
                               t1
0681e46b4022 optimized code generated for datatype cases + more;
blanchet
parents: 37271
diff changeset
   245
                             else
55888
cac1add157e8 removed nonstandard models from Nitpick
blanchet
parents: 51706
diff changeset
   246
                               select_nth_constr_arg ctxt
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
   247
                                   (\<^const_name>\<open>FunBox\<close>,
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
   248
                                    Type (\<^type_name>\<open>fun\<close>, Ts1) --> T1) t1 0
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
   249
                                   (Type (\<^type_name>\<open>fun\<close>, Ts1)), t2)
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   250
        end
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   251
      | t1 $ t2 =>
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   252
        let
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   253
          val t1 = do_term new_Ts old_Ts Neut t1
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   254
          val T1 = fastype_of1 (new_Ts, t1)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   255
          val (s1, Ts1) = dest_Type T1
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   256
          val t2 = do_term new_Ts old_Ts Neut t2
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   257
          val T2 = fastype_of1 (new_Ts, t2)
35384
88dbcfe75c45 cosmetics
blanchet
parents: 35312
diff changeset
   258
          val t2 = coerce_term hol_ctxt new_Ts (hd Ts1) T2 t2
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   259
        in
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
   260
          s_betapply new_Ts (if s1 = \<^type_name>\<open>fun\<close> then
37476
0681e46b4022 optimized code generated for datatype cases + more;
blanchet
parents: 37271
diff changeset
   261
                               t1
0681e46b4022 optimized code generated for datatype cases + more;
blanchet
parents: 37271
diff changeset
   262
                             else
55888
cac1add157e8 removed nonstandard models from Nitpick
blanchet
parents: 51706
diff changeset
   263
                               select_nth_constr_arg ctxt
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
   264
                                   (\<^const_name>\<open>FunBox\<close>,
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
   265
                                    Type (\<^type_name>\<open>fun\<close>, Ts1) --> T1) t1 0
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
   266
                                   (Type (\<^type_name>\<open>fun\<close>, Ts1)), t2)
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   267
        end
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   268
      | Free (s, T) => Free (s, box_type hol_ctxt InExpr T)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   269
      | Var (z as (x, T)) =>
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   270
        Var (x, if def then box_var_in_def new_Ts old_Ts orig_t z
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   271
                else box_type hol_ctxt InExpr T)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   272
      | Bound _ => t
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   273
      | Abs (s, T, t') =>
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   274
        Abs (s, T, do_term (T :: new_Ts) (T :: old_Ts) Neut t')
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   275
  in do_term [] [] Pos orig_t end
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   276
46102
b669437de253 more robust destruction of "set Collect" idiom
blanchet
parents: 44241
diff changeset
   277
(** Destruction of set membership and comprehensions **)
b669437de253 more robust destruction of "set Collect" idiom
blanchet
parents: 44241
diff changeset
   278
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
   279
fun destroy_set_Collect (Const (\<^const_name>\<open>Set.member\<close>, _) $ t1
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
   280
                         $ (Const (\<^const_name>\<open>Collect\<close>, _) $ t2)) =
46102
b669437de253 more robust destruction of "set Collect" idiom
blanchet
parents: 44241
diff changeset
   281
    destroy_set_Collect (t2 $ t1)
b669437de253 more robust destruction of "set Collect" idiom
blanchet
parents: 44241
diff changeset
   282
  | destroy_set_Collect (t1 $ t2) =
b669437de253 more robust destruction of "set Collect" idiom
blanchet
parents: 44241
diff changeset
   283
    destroy_set_Collect t1 $ destroy_set_Collect t2
b669437de253 more robust destruction of "set Collect" idiom
blanchet
parents: 44241
diff changeset
   284
  | destroy_set_Collect (Abs (s, T, t')) = Abs (s, T, destroy_set_Collect t')
b669437de253 more robust destruction of "set Collect" idiom
blanchet
parents: 44241
diff changeset
   285
  | destroy_set_Collect t = t
b669437de253 more robust destruction of "set Collect" idiom
blanchet
parents: 44241
diff changeset
   286
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   287
(** Destruction of constructors **)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   288
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   289
val val_var_prefix = nitpick_prefix ^ "v"
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   290
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   291
fun fresh_value_var Ts k n j t =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   292
  Var ((val_var_prefix ^ nat_subscript (n - j), k), fastype_of1 (Ts, t))
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   293
35280
54ab4921f826 fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents: 35220
diff changeset
   294
fun has_heavy_bounds_or_vars Ts t =
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   295
  let
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   296
    fun aux [] = false
46115
ecab67f5a5c2 improved "set" support by code inspection
blanchet
parents: 46102
diff changeset
   297
      | aux [T] = is_fun_or_set_type T orelse is_pair_type T
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   298
      | aux _ = true
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   299
  in aux (map snd (Term.add_vars t []) @ map (nth Ts) (loose_bnos t)) end
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   300
55888
cac1add157e8 removed nonstandard models from Nitpick
blanchet
parents: 51706
diff changeset
   301
fun pull_out_constr_comb ({ctxt, ...} : hol_context) Ts relax k level t args
cac1add157e8 removed nonstandard models from Nitpick
blanchet
parents: 51706
diff changeset
   302
                         seen =
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   303
  let val t_comb = list_comb (t, args) in
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   304
    case t of
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   305
      Const x =>
55888
cac1add157e8 removed nonstandard models from Nitpick
blanchet
parents: 51706
diff changeset
   306
      if not relax andalso is_constr ctxt x andalso
46115
ecab67f5a5c2 improved "set" support by code inspection
blanchet
parents: 46102
diff changeset
   307
         not (is_fun_or_set_type (fastype_of1 (Ts, t_comb))) andalso
35280
54ab4921f826 fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents: 35220
diff changeset
   308
         has_heavy_bounds_or_vars Ts t_comb andalso
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   309
         not (loose_bvar (t_comb, level)) then
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   310
        let
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   311
          val (j, seen) = case find_index (curry (op =) t_comb) seen of
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   312
                            ~1 => (0, t_comb :: seen)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   313
                          | j => (j, seen)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   314
        in (fresh_value_var Ts k (length seen) j t_comb, seen) end
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   315
      else
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   316
        (t_comb, seen)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   317
    | _ => (t_comb, seen)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   318
  end
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   319
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   320
fun equations_for_pulled_out_constrs mk_eq Ts k seen =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   321
  let val n = length seen in
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   322
    map2 (fn j => fn t => mk_eq (fresh_value_var Ts k n j t, t))
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   323
         (index_seq 0 n) seen
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   324
  end
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   325
35220
2bcdae5f4fdb added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents: 35190
diff changeset
   326
fun pull_out_universal_constrs hol_ctxt def t =
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   327
  let
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   328
    val k = maxidx_of_term t + 1
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   329
    fun do_term Ts def t args seen =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   330
      case t of
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
   331
        (t0 as Const (\<^const_name>\<open>Pure.eq\<close>, _)) $ t1 $ t2 =>
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   332
        do_eq_or_imp Ts true def t0 t1 t2 seen
74380
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
   333
      | (t0 as \<^Const_>\<open>Pure.imp\<close>) $ t1 $ t2 =>
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   334
        if def then (t, []) else do_eq_or_imp Ts false def t0 t1 t2 seen
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
   335
      | (t0 as Const (\<^const_name>\<open>HOL.eq\<close>, _)) $ t1 $ t2 =>
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   336
        do_eq_or_imp Ts true def t0 t1 t2 seen
74380
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
   337
      | (t0 as \<^Const_>\<open>implies\<close>) $ t1 $ t2 =>
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   338
        do_eq_or_imp Ts false def t0 t1 t2 seen
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   339
      | Abs (s, T, t') =>
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   340
        let val (t', seen) = do_term (T :: Ts) def t' [] seen in
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   341
          (list_comb (Abs (s, T, t'), args), seen)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   342
        end
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   343
      | t1 $ t2 =>
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   344
        let val (t2, seen) = do_term Ts def t2 [] seen in
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   345
          do_term Ts def t1 (t2 :: args) seen
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   346
        end
35220
2bcdae5f4fdb added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents: 35190
diff changeset
   347
      | _ => pull_out_constr_comb hol_ctxt Ts def k 0 t args seen
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   348
    and do_eq_or_imp Ts eq def t0 t1 t2 seen =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   349
      let
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   350
        val (t2, seen) = if eq andalso def then (t2, seen)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   351
                         else do_term Ts false t2 [] seen
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   352
        val (t1, seen) = do_term Ts false t1 [] seen
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   353
      in (t0 $ t1 $ t2, seen) end
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   354
    val (concl, seen) = do_term [] def t [] []
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   355
  in
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   356
    Logic.list_implies (equations_for_pulled_out_constrs Logic.mk_equals [] k
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   357
                                                         seen, concl)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   358
  end
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   359
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   360
fun mk_exists v t =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   361
  HOLogic.exists_const (fastype_of v) $ lambda v (incr_boundvars 1 t)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   362
35220
2bcdae5f4fdb added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents: 35190
diff changeset
   363
fun pull_out_existential_constrs hol_ctxt t =
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   364
  let
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   365
    val k = maxidx_of_term t + 1
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   366
    fun aux Ts num_exists t args seen =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   367
      case t of
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
   368
        (t0 as Const (\<^const_name>\<open>Ex\<close>, _)) $ Abs (s1, T1, t1) =>
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   369
        let
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   370
          val (t1, seen') = aux (T1 :: Ts) (num_exists + 1) t1 [] []
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   371
          val n = length seen'
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   372
          fun vars () = map2 (fresh_value_var Ts k n) (index_seq 0 n) seen'
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   373
        in
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   374
          (equations_for_pulled_out_constrs HOLogic.mk_eq Ts k seen'
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   375
           |> List.foldl s_conj t1 |> fold mk_exists (vars ())
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   376
           |> curry3 Abs s1 T1 |> curry (op $) t0, seen)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   377
        end
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   378
      | t1 $ t2 =>
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   379
        let val (t2, seen) = aux Ts num_exists t2 [] seen in
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   380
          aux Ts num_exists t1 (t2 :: args) seen
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   381
        end
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   382
      | Abs (s, T, t') =>
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   383
        let
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   384
          val (t', seen) = aux (T :: Ts) 0 t' [] (map (incr_boundvars 1) seen)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   385
        in (list_comb (Abs (s, T, t'), args), map (incr_boundvars ~1) seen) end
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   386
      | _ =>
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   387
        if num_exists > 0 then
35220
2bcdae5f4fdb added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents: 35190
diff changeset
   388
          pull_out_constr_comb hol_ctxt Ts false k num_exists t args seen
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   389
        else
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   390
          (list_comb (t, args), seen)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   391
  in aux [] 0 t [] [] |> fst end
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   392
55888
cac1add157e8 removed nonstandard models from Nitpick
blanchet
parents: 51706
diff changeset
   393
fun destroy_pulled_out_constrs (hol_ctxt as {ctxt, ...}) axiom strong t =
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   394
  let
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   395
    val num_occs_of_var =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   396
      fold_aterms (fn Var z => (fn f => fn z' => f z' |> z = z' ? Integer.add 1)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   397
                    | _ => I) t (K 0)
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
   398
    fun aux Ts careful ((t0 as Const (\<^const_name>\<open>Pure.eq\<close>, _)) $ t1 $ t2) =
41793
c7a2669ae75d tweaked Nitpick based on C++ memory model example
blanchet
parents: 41791
diff changeset
   399
        aux_eq Ts careful true t0 t1 t2
74380
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
   400
      | aux Ts careful ((t0 as \<^Const_>\<open>Pure.imp\<close>) $ t1 $ t2) =
41793
c7a2669ae75d tweaked Nitpick based on C++ memory model example
blanchet
parents: 41791
diff changeset
   401
        t0 $ aux Ts false t1 $ aux Ts careful t2
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
   402
      | aux Ts careful ((t0 as Const (\<^const_name>\<open>HOL.eq\<close>, _)) $ t1 $ t2) =
41793
c7a2669ae75d tweaked Nitpick based on C++ memory model example
blanchet
parents: 41791
diff changeset
   403
        aux_eq Ts careful true t0 t1 t2
74380
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
   404
      | aux Ts careful ((t0 as \<^Const_>\<open>implies\<close>) $ t1 $ t2) =
41793
c7a2669ae75d tweaked Nitpick based on C++ memory model example
blanchet
parents: 41791
diff changeset
   405
        t0 $ aux Ts false t1 $ aux Ts careful t2
c7a2669ae75d tweaked Nitpick based on C++ memory model example
blanchet
parents: 41791
diff changeset
   406
      | aux Ts careful (Abs (s, T, t')) = Abs (s, T, aux (T :: Ts) careful t')
c7a2669ae75d tweaked Nitpick based on C++ memory model example
blanchet
parents: 41791
diff changeset
   407
      | aux Ts careful (t1 $ t2) = aux Ts careful t1 $ aux Ts careful t2
c7a2669ae75d tweaked Nitpick based on C++ memory model example
blanchet
parents: 41791
diff changeset
   408
      | aux _ _ t = t
c7a2669ae75d tweaked Nitpick based on C++ memory model example
blanchet
parents: 41791
diff changeset
   409
    and aux_eq Ts careful pass1 t0 t1 t2 =
41994
c567c860caf6 always destroy constructor patterns, since this seems to be always useful
blanchet
parents: 41876
diff changeset
   410
      ((if careful orelse
c567c860caf6 always destroy constructor patterns, since this seems to be always useful
blanchet
parents: 41876
diff changeset
   411
           not (strong orelse forall (is_constr_pattern ctxt) [t1, t2]) then
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   412
          raise SAME ()
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   413
        else if axiom andalso is_Var t2 andalso
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   414
                num_occs_of_var (dest_Var t2) = 1 then
74380
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
   415
          \<^Const>\<open>True\<close>
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   416
        else case strip_comb t2 of
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   417
          (* The first case is not as general as it could be. *)
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
   418
          (Const (\<^const_name>\<open>PairBox\<close>, _),
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
   419
                  [Const (\<^const_name>\<open>fst\<close>, _) $ Var z1,
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
   420
                   Const (\<^const_name>\<open>snd\<close>, _) $ Var z2]) =>
74380
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
   421
          if z1 = z2 andalso num_occs_of_var z1 = 2 then \<^Const>\<open>True\<close>
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   422
          else raise SAME ()
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   423
        | (Const (x as (s, T)), args) =>
35386
45a4e19d3ebd more work on the new monotonicity stuff in Nitpick
blanchet
parents: 35385
diff changeset
   424
          let
45a4e19d3ebd more work on the new monotonicity stuff in Nitpick
blanchet
parents: 35385
diff changeset
   425
            val (arg_Ts, dataT) = strip_type T
45a4e19d3ebd more work on the new monotonicity stuff in Nitpick
blanchet
parents: 35385
diff changeset
   426
            val n = length arg_Ts
45a4e19d3ebd more work on the new monotonicity stuff in Nitpick
blanchet
parents: 35385
diff changeset
   427
          in
45a4e19d3ebd more work on the new monotonicity stuff in Nitpick
blanchet
parents: 35385
diff changeset
   428
            if length args = n andalso
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
   429
               (is_constr ctxt x orelse s = \<^const_name>\<open>Pair\<close> orelse
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
   430
                x = (\<^const_name>\<open>Suc\<close>, nat_T --> nat_T)) andalso
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   431
               (not careful orelse not (is_Var t1) orelse
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   432
                String.isPrefix val_var_prefix (fst (fst (dest_Var t1)))) then
41793
c7a2669ae75d tweaked Nitpick based on C++ memory model example
blanchet
parents: 41791
diff changeset
   433
                s_let Ts "l" (n + 1) dataT bool_T
c7a2669ae75d tweaked Nitpick based on C++ memory model example
blanchet
parents: 41791
diff changeset
   434
                      (fn t1 =>
c7a2669ae75d tweaked Nitpick based on C++ memory model example
blanchet
parents: 41791
diff changeset
   435
                          discriminate_value hol_ctxt x t1 ::
58634
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 56245
diff changeset
   436
                          @{map 3} (sel_eq Ts x t1) (index_seq 0 n) arg_Ts args
41793
c7a2669ae75d tweaked Nitpick based on C++ memory model example
blanchet
parents: 41791
diff changeset
   437
                          |> foldr1 s_conj) t1
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   438
            else
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   439
              raise SAME ()
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   440
          end
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   441
        | _ => raise SAME ())
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   442
       |> body_type (type_of t0) = prop_T ? HOLogic.mk_Trueprop)
41793
c7a2669ae75d tweaked Nitpick based on C++ memory model example
blanchet
parents: 41791
diff changeset
   443
      handle SAME () => if pass1 then aux_eq Ts careful false t0 t2 t1
c7a2669ae75d tweaked Nitpick based on C++ memory model example
blanchet
parents: 41791
diff changeset
   444
                        else t0 $ aux Ts false t2 $ aux Ts false t1
c7a2669ae75d tweaked Nitpick based on C++ memory model example
blanchet
parents: 41791
diff changeset
   445
    and sel_eq Ts x t n nth_T nth_t =
55888
cac1add157e8 removed nonstandard models from Nitpick
blanchet
parents: 51706
diff changeset
   446
      HOLogic.eq_const nth_T $ nth_t $ select_nth_constr_arg ctxt x t n nth_T
41793
c7a2669ae75d tweaked Nitpick based on C++ memory model example
blanchet
parents: 41791
diff changeset
   447
      |> aux Ts false
c7a2669ae75d tweaked Nitpick based on C++ memory model example
blanchet
parents: 41791
diff changeset
   448
  in aux [] axiom t end
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   449
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   450
(** Destruction of universal and existential equalities **)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   451
74380
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
   452
fun curry_assms \<^Const_>\<open>Pure.imp for \<^Const>\<open>Trueprop for \<^Const_>\<open>conj for t1 t2\<close>\<close> t3\<close> =
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   453
    curry_assms (Logic.list_implies ([t1, t2] |> map HOLogic.mk_Trueprop, t3))
74380
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
   454
  | curry_assms \<^Const_>\<open>Pure.imp for t1 t2\<close> = \<^Const>\<open>Pure.imp for \<open>curry_assms t1\<close> \<open>curry_assms t2\<close>\<close>
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   455
  | curry_assms t = t
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   456
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   457
val destroy_universal_equalities =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   458
  let
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   459
    fun aux prems zs t =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   460
      case t of
74380
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
   461
        \<^Const_>\<open>Pure.imp for t1 t2\<close> => aux_implies prems zs t1 t2
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   462
      | _ => Logic.list_implies (rev prems, t)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   463
    and aux_implies prems zs t1 t2 =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   464
      case t1 of
74380
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
   465
        \<^Const_>\<open>Pure.eq _ for \<open>Var z\<close> t'\<close> => aux_eq prems zs z t' t1 t2
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
   466
      | \<^Const_>\<open>Trueprop for \<^Const_>\<open>HOL.eq _ for \<open>Var z\<close> t'\<close>\<close> => aux_eq prems zs z t' t1 t2
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
   467
      | \<^Const_>\<open>Trueprop for \<^Const_>\<open>HOL.eq _ for t' \<open>Var z\<close>\<close>\<close> => aux_eq prems zs z t' t1 t2
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   468
      | _ => aux (t1 :: prems) (Term.add_vars t1 zs) t2
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   469
    and aux_eq prems zs z t' t1 t2 =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   470
      if not (member (op =) zs z) andalso
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   471
         not (exists_subterm (curry (op =) (Var z)) t') then
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   472
        aux prems zs (subst_free [(Var z, t')] t2)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   473
      else
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   474
        aux (t1 :: prems) (Term.add_vars t1 zs) t2
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   475
  in aux [] [] end
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   476
55888
cac1add157e8 removed nonstandard models from Nitpick
blanchet
parents: 51706
diff changeset
   477
fun find_bound_assign ctxt j =
35220
2bcdae5f4fdb added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents: 35190
diff changeset
   478
  let
2bcdae5f4fdb added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents: 35190
diff changeset
   479
    fun do_term _ [] = NONE
2bcdae5f4fdb added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents: 35190
diff changeset
   480
      | do_term seen (t :: ts) =
2bcdae5f4fdb added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents: 35190
diff changeset
   481
        let
2bcdae5f4fdb added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents: 35190
diff changeset
   482
          fun do_eq pass1 t1 t2 =
2bcdae5f4fdb added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents: 35190
diff changeset
   483
            (if loose_bvar1 (t2, j) then
2bcdae5f4fdb added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents: 35190
diff changeset
   484
               if pass1 then do_eq false t2 t1 else raise SAME ()
2bcdae5f4fdb added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents: 35190
diff changeset
   485
             else case t1 of
2bcdae5f4fdb added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents: 35190
diff changeset
   486
               Bound j' => if j' = j then SOME (t2, ts @ seen) else raise SAME ()
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
   487
             | Const (s, Type (\<^type_name>\<open>fun\<close>, [T1, T2])) $ Bound j' =>
35220
2bcdae5f4fdb added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents: 35190
diff changeset
   488
               if j' = j andalso
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
   489
                  s = nth_sel_name_for_constr_name \<^const_name>\<open>FunBox\<close> 0 then
55888
cac1add157e8 removed nonstandard models from Nitpick
blanchet
parents: 51706
diff changeset
   490
                 SOME (construct_value ctxt
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
   491
                                       (\<^const_name>\<open>FunBox\<close>, T2 --> T1) [t2],
37256
0dca1ec52999 thread along context instead of theory for typedef lookup
blanchet
parents: 37253
diff changeset
   492
                       ts @ seen)
35220
2bcdae5f4fdb added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents: 35190
diff changeset
   493
               else
2bcdae5f4fdb added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents: 35190
diff changeset
   494
                 raise SAME ()
2bcdae5f4fdb added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents: 35190
diff changeset
   495
             | _ => raise SAME ())
2bcdae5f4fdb added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents: 35190
diff changeset
   496
            handle SAME () => do_term (t :: seen) ts
2bcdae5f4fdb added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents: 35190
diff changeset
   497
        in
2bcdae5f4fdb added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents: 35190
diff changeset
   498
          case t of
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
   499
            Const (\<^const_name>\<open>HOL.eq\<close>, _) $ t1 $ t2 => do_eq true t1 t2
35220
2bcdae5f4fdb added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents: 35190
diff changeset
   500
          | _ => do_term (t :: seen) ts
2bcdae5f4fdb added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents: 35190
diff changeset
   501
        end
2bcdae5f4fdb added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents: 35190
diff changeset
   502
  in do_term end
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   503
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   504
fun subst_one_bound j arg t =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   505
  let
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   506
    fun aux (Bound i, lev) =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   507
        if i < lev then raise SAME ()
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   508
        else if i = lev then incr_boundvars (lev - j) arg
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   509
        else Bound (i - 1)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   510
      | aux (Abs (a, T, body), lev) = Abs (a, T, aux (body, lev + 1))
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   511
      | aux (f $ t, lev) =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   512
        (aux (f, lev) $ (aux (t, lev) handle SAME () => t)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   513
         handle SAME () => f $ aux (t, lev))
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   514
      | aux _ = raise SAME ()
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   515
  in aux (t, j) handle SAME () => t end
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   516
55888
cac1add157e8 removed nonstandard models from Nitpick
blanchet
parents: 51706
diff changeset
   517
fun destroy_existential_equalities ({ctxt, ...} : hol_context) =
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   518
  let
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   519
    fun kill [] [] ts = foldr1 s_conj ts
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   520
      | kill (s :: ss) (T :: Ts) ts =
55888
cac1add157e8 removed nonstandard models from Nitpick
blanchet
parents: 51706
diff changeset
   521
        (case find_bound_assign ctxt (length ss) [] ts of
74380
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
   522
           SOME (_, []) => \<^Const>\<open>True\<close>
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   523
         | SOME (arg_t, ts) =>
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   524
           kill ss Ts (map (subst_one_bound (length ss)
79170
4affbdbeefd4 clarified signature: more standard argument order;
wenzelm
parents: 74380
diff changeset
   525
                                (incr_bv ~1 (length ss + 1) arg_t)) ts)
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   526
         | NONE =>
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
   527
           Const (\<^const_name>\<open>Ex\<close>, (T --> bool_T) --> bool_T)
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   528
           $ Abs (s, T, kill ss Ts ts))
40722
441260986b63 make two copies (!) of Library.UnequalLengths coincide with ListPair.UnequalLengths;
wenzelm
parents: 39687
diff changeset
   529
      | kill _ _ _ = raise ListPair.UnequalLengths
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
   530
    fun gather ss Ts (Const (\<^const_name>\<open>Ex\<close>, _) $ Abs (s1, T1, t1)) =
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   531
        gather (ss @ [s1]) (Ts @ [T1]) t1
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   532
      | gather [] [] (Abs (s, T, t1)) = Abs (s, T, gather [] [] t1)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   533
      | gather [] [] (t1 $ t2) = gather [] [] t1 $ gather [] [] t2
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   534
      | gather [] [] t = t
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   535
      | gather ss Ts t = kill ss Ts (conjuncts_of (gather [] [] t))
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   536
  in gather [] [] end
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   537
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   538
(** Skolemization **)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   539
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   540
fun skolem_prefix_for k j =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   541
  skolem_prefix ^ string_of_int k ^ "@" ^ string_of_int j ^ name_sep
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   542
41791
01d722707a36 always unfold constant defitions marked with "nitpick_def" -- to prevent unfolding, there's already "nitpick_simp"
blanchet
parents: 41052
diff changeset
   543
fun skolemize_term_and_more (hol_ctxt as {thy, def_tables, skolems, ...})
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   544
                            skolem_depth =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   545
  let
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   546
    val incrs = map (Integer.add 1)
37928
24785fa2416c do a better job at Skolemizing in Nitpick, for TPTP FOF
blanchet
parents: 37704
diff changeset
   547
    fun aux ss Ts js skolemizable polar t =
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   548
      let
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   549
        fun do_quantifier quant_s quant_T abs_s abs_T t =
37928
24785fa2416c do a better job at Skolemizing in Nitpick, for TPTP FOF
blanchet
parents: 37704
diff changeset
   550
          (if not (loose_bvar1 (t, 0)) then
24785fa2416c do a better job at Skolemizing in Nitpick, for TPTP FOF
blanchet
parents: 37704
diff changeset
   551
             aux ss Ts js skolemizable polar (incr_boundvars ~1 t)
24785fa2416c do a better job at Skolemizing in Nitpick, for TPTP FOF
blanchet
parents: 37704
diff changeset
   552
           else if is_positive_existential polar quant_s then
24785fa2416c do a better job at Skolemizing in Nitpick, for TPTP FOF
blanchet
parents: 37704
diff changeset
   553
             let
24785fa2416c do a better job at Skolemizing in Nitpick, for TPTP FOF
blanchet
parents: 37704
diff changeset
   554
               val j = length (!skolems) + 1
24785fa2416c do a better job at Skolemizing in Nitpick, for TPTP FOF
blanchet
parents: 37704
diff changeset
   555
             in
47754
11b4395aaf0c remove too aggressive skolemization optimization (prevented discovery of a model in SYN994^1)
blanchet
parents: 47669
diff changeset
   556
               if skolemizable andalso length js <= skolem_depth then
37928
24785fa2416c do a better job at Skolemizing in Nitpick, for TPTP FOF
blanchet
parents: 37704
diff changeset
   557
                 let
47754
11b4395aaf0c remove too aggressive skolemization optimization (prevented discovery of a model in SYN994^1)
blanchet
parents: 47669
diff changeset
   558
                   val sko_s = skolem_prefix_for (length js) j ^ abs_s
11b4395aaf0c remove too aggressive skolemization optimization (prevented discovery of a model in SYN994^1)
blanchet
parents: 47669
diff changeset
   559
                   val _ = Unsynchronized.change skolems (cons (sko_s, ss))
11b4395aaf0c remove too aggressive skolemization optimization (prevented discovery of a model in SYN994^1)
blanchet
parents: 47669
diff changeset
   560
                   val sko_t = list_comb (Const (sko_s, rev Ts ---> abs_T),
11b4395aaf0c remove too aggressive skolemization optimization (prevented discovery of a model in SYN994^1)
blanchet
parents: 47669
diff changeset
   561
                                          map Bound (rev js))
37928
24785fa2416c do a better job at Skolemizing in Nitpick, for TPTP FOF
blanchet
parents: 37704
diff changeset
   562
                   val abs_t = Abs (abs_s, abs_T,
24785fa2416c do a better job at Skolemizing in Nitpick, for TPTP FOF
blanchet
parents: 37704
diff changeset
   563
                                    aux ss Ts (incrs js) skolemizable polar t)
24785fa2416c do a better job at Skolemizing in Nitpick, for TPTP FOF
blanchet
parents: 37704
diff changeset
   564
                 in
47754
11b4395aaf0c remove too aggressive skolemization optimization (prevented discovery of a model in SYN994^1)
blanchet
parents: 47669
diff changeset
   565
                   if null js then
37928
24785fa2416c do a better job at Skolemizing in Nitpick, for TPTP FOF
blanchet
parents: 37704
diff changeset
   566
                     s_betapply Ts (abs_t, sko_t)
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   567
                   else
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
   568
                     Const (\<^const_name>\<open>Let\<close>, abs_T --> quant_T) $ sko_t
37928
24785fa2416c do a better job at Skolemizing in Nitpick, for TPTP FOF
blanchet
parents: 37704
diff changeset
   569
                     $ abs_t
24785fa2416c do a better job at Skolemizing in Nitpick, for TPTP FOF
blanchet
parents: 37704
diff changeset
   570
                 end
24785fa2416c do a better job at Skolemizing in Nitpick, for TPTP FOF
blanchet
parents: 37704
diff changeset
   571
               else
24785fa2416c do a better job at Skolemizing in Nitpick, for TPTP FOF
blanchet
parents: 37704
diff changeset
   572
                 raise SAME ()
24785fa2416c do a better job at Skolemizing in Nitpick, for TPTP FOF
blanchet
parents: 37704
diff changeset
   573
             end
24785fa2416c do a better job at Skolemizing in Nitpick, for TPTP FOF
blanchet
parents: 37704
diff changeset
   574
           else
24785fa2416c do a better job at Skolemizing in Nitpick, for TPTP FOF
blanchet
parents: 37704
diff changeset
   575
             raise SAME ())
24785fa2416c do a better job at Skolemizing in Nitpick, for TPTP FOF
blanchet
parents: 37704
diff changeset
   576
          handle SAME () =>
24785fa2416c do a better job at Skolemizing in Nitpick, for TPTP FOF
blanchet
parents: 37704
diff changeset
   577
                 Const (quant_s, quant_T)
24785fa2416c do a better job at Skolemizing in Nitpick, for TPTP FOF
blanchet
parents: 37704
diff changeset
   578
                 $ Abs (abs_s, abs_T,
38166
28bb89672cc7 fix Skolemizer -- last week's optimizations resulted in UnequalLengths errors
blanchet
parents: 38165
diff changeset
   579
                        aux (abs_s :: ss) (abs_T :: Ts) (0 :: incrs js)
28bb89672cc7 fix Skolemizer -- last week's optimizations resulted in UnequalLengths errors
blanchet
parents: 38165
diff changeset
   580
                            (skolemizable andalso
28bb89672cc7 fix Skolemizer -- last week's optimizations resulted in UnequalLengths errors
blanchet
parents: 38165
diff changeset
   581
                             not (is_higher_order_type abs_T)) polar t)
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   582
      in
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   583
        case t of
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
   584
          Const (s0 as \<^const_name>\<open>Pure.all\<close>, T0) $ Abs (s1, T1, t1) =>
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   585
          do_quantifier s0 T0 s1 T1 t1
74380
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
   586
        | \<^Const_>\<open>Pure.imp for t1 t2\<close> =>
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
   587
          \<^Const>\<open>Pure.imp for \<open>aux ss Ts js skolemizable (flip_polarity polar) t1\<close>
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
   588
            \<open>aux ss Ts js skolemizable polar t2\<close>\<close>
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
   589
        | \<^Const_>\<open>Pure.conjunction for t1 t2\<close> =>
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
   590
          \<^Const>\<open>Pure.conjunction for \<open>aux ss Ts js skolemizable polar t1\<close>
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
   591
            \<open>aux ss Ts js skolemizable polar t2\<close>\<close>
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
   592
        | \<^Const_>\<open>Trueprop for t1\<close> =>
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
   593
          \<^Const>\<open>Trueprop for \<open>aux ss Ts js skolemizable polar t1\<close>\<close>
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
   594
        | \<^Const_>\<open>Not for t1\<close> =>
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
   595
          \<^Const>\<open>Not for \<open>aux ss Ts js skolemizable (flip_polarity polar) t1\<close>\<close>
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
   596
        | Const (s0 as \<^const_name>\<open>All\<close>, T0) $ Abs (s1, T1, t1) =>
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   597
          do_quantifier s0 T0 s1 T1 t1
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
   598
        | Const (s0 as \<^const_name>\<open>Ex\<close>, T0) $ Abs (s1, T1, t1) =>
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   599
          do_quantifier s0 T0 s1 T1 t1
74380
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
   600
        | \<^Const_>\<open>conj for t1 t2\<close> =>
59058
a78612c67ec0 renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents: 58634
diff changeset
   601
          s_conj (apply2 (aux ss Ts js skolemizable polar) (t1, t2))
74380
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
   602
        | \<^Const_>\<open>disj for t1 t2\<close> =>
59058
a78612c67ec0 renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents: 58634
diff changeset
   603
          s_disj (apply2 (aux ss Ts js skolemizable polar) (t1, t2))
74380
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
   604
        | \<^Const_>\<open>implies for t1 t2\<close> =>
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
   605
          \<^Const>\<open>implies for \<open>aux ss Ts js skolemizable (flip_polarity polar) t1\<close>
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
   606
            \<open>aux ss Ts js skolemizable polar t2\<close>\<close>
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
   607
        | (t0 as Const (\<^const_name>\<open>Let\<close>, _)) $ t1 $ t2 =>
37928
24785fa2416c do a better job at Skolemizing in Nitpick, for TPTP FOF
blanchet
parents: 37704
diff changeset
   608
          t0 $ t1 $ aux ss Ts js skolemizable polar t2
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   609
        | Const (x as (s, T)) =>
55889
6bfbec3dff62 tuned code
blanchet
parents: 55888
diff changeset
   610
          if is_raw_inductive_pred hol_ctxt x andalso
6bfbec3dff62 tuned code
blanchet
parents: 55888
diff changeset
   611
             not (is_raw_equational_fun hol_ctxt x) andalso
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   612
             not (is_well_founded_inductive_pred hol_ctxt x) then
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   613
            let
41791
01d722707a36 always unfold constant defitions marked with "nitpick_def" -- to prevent unfolding, there's already "nitpick_simp"
blanchet
parents: 41052
diff changeset
   614
              val gfp = (fixpoint_kind_of_const thy def_tables x = Gfp)
37476
0681e46b4022 optimized code generated for datatype cases + more;
blanchet
parents: 37271
diff changeset
   615
              val (pref, connective) =
74380
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
   616
                if gfp then (lbfp_prefix, \<^Const>\<open>disj\<close>)
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
   617
                else (ubfp_prefix, \<^Const>\<open>conj\<close>)
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   618
              fun pos () = unrolled_inductive_pred_const hol_ctxt gfp x
37928
24785fa2416c do a better job at Skolemizing in Nitpick, for TPTP FOF
blanchet
parents: 37704
diff changeset
   619
                           |> aux ss Ts js skolemizable polar
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   620
              fun neg () = Const (pref ^ s, T)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   621
            in
37476
0681e46b4022 optimized code generated for datatype cases + more;
blanchet
parents: 37271
diff changeset
   622
              case polar |> gfp ? flip_polarity of
0681e46b4022 optimized code generated for datatype cases + more;
blanchet
parents: 37271
diff changeset
   623
                Pos => pos ()
0681e46b4022 optimized code generated for datatype cases + more;
blanchet
parents: 37271
diff changeset
   624
              | Neg => neg ()
0681e46b4022 optimized code generated for datatype cases + more;
blanchet
parents: 37271
diff changeset
   625
              | Neut =>
0681e46b4022 optimized code generated for datatype cases + more;
blanchet
parents: 37271
diff changeset
   626
                let
0681e46b4022 optimized code generated for datatype cases + more;
blanchet
parents: 37271
diff changeset
   627
                  val arg_Ts = binder_types T
0681e46b4022 optimized code generated for datatype cases + more;
blanchet
parents: 37271
diff changeset
   628
                  fun app f =
0681e46b4022 optimized code generated for datatype cases + more;
blanchet
parents: 37271
diff changeset
   629
                    list_comb (f (), map Bound (length arg_Ts - 1 downto 0))
0681e46b4022 optimized code generated for datatype cases + more;
blanchet
parents: 37271
diff changeset
   630
                in
44241
7943b69f0188 modernized signature of Term.absfree/absdummy;
wenzelm
parents: 42415
diff changeset
   631
                  fold_rev absdummy arg_Ts (connective $ app pos $ app neg)
37476
0681e46b4022 optimized code generated for datatype cases + more;
blanchet
parents: 37271
diff changeset
   632
                end
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   633
            end
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   634
          else
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   635
            Const x
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   636
        | t1 $ t2 =>
38166
28bb89672cc7 fix Skolemizer -- last week's optimizations resulted in UnequalLengths errors
blanchet
parents: 38165
diff changeset
   637
          s_betapply Ts (aux ss Ts js false polar t1,
28bb89672cc7 fix Skolemizer -- last week's optimizations resulted in UnequalLengths errors
blanchet
parents: 38165
diff changeset
   638
                         aux ss Ts js false Neut t2)
37928
24785fa2416c do a better job at Skolemizing in Nitpick, for TPTP FOF
blanchet
parents: 37704
diff changeset
   639
        | Abs (s, T, t1) =>
24785fa2416c do a better job at Skolemizing in Nitpick, for TPTP FOF
blanchet
parents: 37704
diff changeset
   640
          Abs (s, T, aux ss Ts (incrs js) skolemizable polar t1)
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   641
        | _ => t
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   642
      end
37928
24785fa2416c do a better job at Skolemizing in Nitpick, for TPTP FOF
blanchet
parents: 37704
diff changeset
   643
  in aux [] [] [] true Pos end
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   644
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   645
(** Function specialization **)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   646
74380
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
   647
fun params_in_equation \<^Const_>\<open>Pure.imp for _ t2\<close> = params_in_equation t2
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
   648
  | params_in_equation \<^Const_>\<open>Trueprop for t1\<close> = params_in_equation t1
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
   649
  | params_in_equation \<^Const_>\<open>HOL.eq _ for t1 _\<close> = snd (strip_comb t1)
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   650
  | params_in_equation _ = []
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   651
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   652
fun specialize_fun_axiom x x' fixed_js fixed_args extra_args t =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   653
  let
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   654
    val k = fold Integer.max (map maxidx_of_term (fixed_args @ extra_args)) 0
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   655
            + 1
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   656
    val t = map_aterms (fn Var ((s, i), T) => Var ((s, k + i), T) | t' => t') t
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   657
    val fixed_params = filter_indices fixed_js (params_in_equation t)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   658
    fun aux args (Abs (s, T, t)) = list_comb (Abs (s, T, aux [] t), args)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   659
      | aux args (t1 $ t2) = aux (aux [] t2 :: args) t1
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   660
      | aux args t =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   661
        if t = Const x then
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   662
          list_comb (Const x', extra_args @ filter_out_indices fixed_js args)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   663
        else
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   664
          let val j = find_index (curry (op =) t) fixed_params in
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   665
            list_comb (if j >= 0 then nth fixed_args j else t, args)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   666
          end
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   667
  in aux [] t end
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   668
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   669
fun static_args_in_term ({ersatz_table, ...} : hol_context) x t =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   670
  let
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   671
    fun fun_calls (Abs (_, _, t)) _ = fun_calls t []
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   672
      | fun_calls (t1 $ t2) args = fun_calls t2 [] #> fun_calls t1 (t2 :: args)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   673
      | fun_calls t args =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   674
        (case t of
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   675
           Const (x' as (s', T')) =>
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   676
           x = x' orelse (case AList.lookup (op =) ersatz_table s' of
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   677
                            SOME s'' => x = (s'', T')
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   678
                          | NONE => false)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   679
         | _ => false) ? cons args
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   680
    fun call_sets [] [] vs = [vs]
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   681
      | call_sets [] uss vs = vs :: call_sets uss [] []
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   682
      | call_sets ([] :: _) _ _ = []
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   683
      | call_sets ((t :: ts) :: tss) uss vs =
39687
4e9b6ada3a21 modernized structure Ord_List;
wenzelm
parents: 39360
diff changeset
   684
        Ord_List.insert Term_Ord.term_ord t vs |> call_sets tss (ts :: uss)
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   685
    val sets = call_sets (fun_calls t [] []) [] []
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   686
    val indexed_sets = sets ~~ (index_seq 0 (length sets))
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   687
  in
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   688
    fold_rev (fn (set, j) =>
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   689
                 case set of
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   690
                   [Var _] => AList.lookup (op =) indexed_sets set = SOME j
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   691
                              ? cons (j, NONE)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   692
                 | [t as Const _] => cons (j, SOME t)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   693
                 | [t as Free _] => cons (j, SOME t)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   694
                 | _ => I) indexed_sets []
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   695
  end
55889
6bfbec3dff62 tuned code
blanchet
parents: 55888
diff changeset
   696
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   697
fun static_args_in_terms hol_ctxt x =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   698
  map (static_args_in_term hol_ctxt x)
39687
4e9b6ada3a21 modernized structure Ord_List;
wenzelm
parents: 39360
diff changeset
   699
  #> fold1 (Ord_List.inter (prod_ord int_ord (option_ord Term_Ord.term_ord)))
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   700
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   701
fun overlapping_indices [] _ = []
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   702
  | overlapping_indices _ [] = []
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   703
  | overlapping_indices (ps1 as (j1, t1) :: ps1') (ps2 as (j2, t2) :: ps2') =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   704
    if j1 < j2 then overlapping_indices ps1' ps2
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   705
    else if j1 > j2 then overlapping_indices ps1 ps2'
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   706
    else overlapping_indices ps1' ps2' |> the_default t2 t1 = t2 ? cons j1
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   707
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   708
fun special_prefix_for j = special_prefix ^ string_of_int j ^ name_sep
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   709
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   710
(* If a constant's definition is picked up deeper than this threshold, we
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   711
   prevent excessive specialization by not specializing it. *)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   712
val special_max_depth = 20
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   713
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   714
val bound_var_prefix = "b"
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   715
38165
9797be44df23 prevent generation of needless specialized constants etc.
blanchet
parents: 37928
diff changeset
   716
fun special_fun_aconv ((x1, js1, ts1), (x2, js2, ts2)) =
9797be44df23 prevent generation of needless specialized constants etc.
blanchet
parents: 37928
diff changeset
   717
  x1 = x2 andalso js1 = js2 andalso length ts1 = length ts2 andalso
9797be44df23 prevent generation of needless specialized constants etc.
blanchet
parents: 37928
diff changeset
   718
  forall (op aconv) (ts1 ~~ ts2)
9797be44df23 prevent generation of needless specialized constants etc.
blanchet
parents: 37928
diff changeset
   719
38204
38339c055869 don't specialize built-ins or constructors
blanchet
parents: 38202
diff changeset
   720
fun specialize_consts_in_term
55888
cac1add157e8 removed nonstandard models from Nitpick
blanchet
parents: 51706
diff changeset
   721
        (hol_ctxt as {ctxt, thy, specialize, def_tables, simp_table,
39359
6f49c7fbb1b1 remove "fast_descs" option from Nitpick;
blanchet
parents: 39345
diff changeset
   722
                      special_funs, ...}) def depth t =
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   723
  if not specialize orelse depth > special_max_depth then
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   724
    t
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   725
  else
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   726
    let
38206
78403fcd0ec5 fiddle with specialization etc.
blanchet
parents: 38205
diff changeset
   727
      val blacklist =
78403fcd0ec5 fiddle with specialization etc.
blanchet
parents: 38205
diff changeset
   728
        if def then case term_under_def t of Const x => [x] | _ => [] else []
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   729
      fun aux args Ts (Const (x as (s, T))) =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   730
          ((if not (member (op =) blacklist x) andalso not (null args) andalso
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   731
               not (String.isPrefix special_prefix s) andalso
55888
cac1add157e8 removed nonstandard models from Nitpick
blanchet
parents: 51706
diff changeset
   732
               not (is_built_in_const x) andalso
55889
6bfbec3dff62 tuned code
blanchet
parents: 55888
diff changeset
   733
               (is_equational_fun hol_ctxt x orelse
41791
01d722707a36 always unfold constant defitions marked with "nitpick_def" -- to prevent unfolding, there's already "nitpick_simp"
blanchet
parents: 41052
diff changeset
   734
                (is_some (def_of_const thy def_tables x) andalso
38204
38339c055869 don't specialize built-ins or constructors
blanchet
parents: 38202
diff changeset
   735
                 not (is_of_class_const thy x) andalso
55888
cac1add157e8 removed nonstandard models from Nitpick
blanchet
parents: 51706
diff changeset
   736
                 not (is_constr ctxt x) andalso
38204
38339c055869 don't specialize built-ins or constructors
blanchet
parents: 38202
diff changeset
   737
                 not (is_choice_spec_fun hol_ctxt x))) then
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   738
              let
41793
c7a2669ae75d tweaked Nitpick based on C++ memory model example
blanchet
parents: 41791
diff changeset
   739
                val eligible_args =
c7a2669ae75d tweaked Nitpick based on C++ memory model example
blanchet
parents: 41791
diff changeset
   740
                  filter (is_special_eligible_arg true Ts o snd)
c7a2669ae75d tweaked Nitpick based on C++ memory model example
blanchet
parents: 41791
diff changeset
   741
                         (index_seq 0 (length args) ~~ args)
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   742
                val _ = not (null eligible_args) orelse raise SAME ()
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   743
                val old_axs = equational_fun_axioms hol_ctxt x
35220
2bcdae5f4fdb added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents: 35190
diff changeset
   744
                              |> map (destroy_existential_equalities hol_ctxt)
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   745
                val static_params = static_args_in_terms hol_ctxt x old_axs
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   746
                val fixed_js = overlapping_indices static_params eligible_args
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   747
                val _ = not (null fixed_js) orelse raise SAME ()
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   748
                val fixed_args = filter_indices fixed_js args
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   749
                val vars = fold Term.add_vars fixed_args []
59058
a78612c67ec0 renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents: 58634
diff changeset
   750
                           |> sort (Term_Ord.fast_indexname_ord o apply2 fst)
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   751
                val bound_js = fold (fn t => fn js => add_loose_bnos (t, 0, js))
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   752
                                    fixed_args []
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   753
                               |> sort int_ord
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   754
                val live_args = filter_out_indices fixed_js args
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   755
                val extra_args = map Var vars @ map Bound bound_js @ live_args
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   756
                val extra_Ts = map snd vars @ filter_indices bound_js Ts
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   757
                val k = maxidx_of_term t + 1
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   758
                fun var_for_bound_no j =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   759
                  Var ((bound_var_prefix ^
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   760
                        nat_subscript (find_index (curry (op =) j) bound_js
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   761
                                       + 1), k),
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   762
                       nth Ts j)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   763
                val fixed_args_in_axiom =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   764
                  map (curry subst_bounds
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   765
                             (map var_for_bound_no (index_seq 0 (length Ts))))
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   766
                      fixed_args
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   767
              in
38165
9797be44df23 prevent generation of needless specialized constants etc.
blanchet
parents: 37928
diff changeset
   768
                case AList.lookup special_fun_aconv (!special_funs)
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   769
                                  (x, fixed_js, fixed_args_in_axiom) of
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   770
                  SOME x' => list_comb (Const x', extra_args)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   771
                | NONE =>
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   772
                  let
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   773
                    val extra_args_in_axiom =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   774
                      map Var vars @ map var_for_bound_no bound_js
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   775
                    val x' as (s', _) =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   776
                      (special_prefix_for (length (!special_funs) + 1) ^ s,
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   777
                       extra_Ts @ filter_out_indices fixed_js (binder_types T)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   778
                       ---> body_type T)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   779
                    val new_axs =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   780
                      map (specialize_fun_axiom x x' fixed_js
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   781
                               fixed_args_in_axiom extra_args_in_axiom) old_axs
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   782
                    val _ =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   783
                      Unsynchronized.change special_funs
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   784
                          (cons ((x, fixed_js, fixed_args_in_axiom), x'))
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   785
                    val _ = add_simps simp_table s' new_axs
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   786
                  in list_comb (Const x', extra_args) end
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   787
              end
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   788
            else
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   789
              raise SAME ())
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   790
           handle SAME () => list_comb (Const x, args))
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   791
        | aux args Ts (Abs (s, T, t)) =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   792
          list_comb (Abs (s, T, aux [] (T :: Ts) t), args)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   793
        | aux args Ts (t1 $ t2) = aux (aux [] Ts t2 :: args) Ts t1
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   794
        | aux args _ t = list_comb (t, args)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   795
    in aux [] [] t end
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   796
55889
6bfbec3dff62 tuned code
blanchet
parents: 55888
diff changeset
   797
type special_triple = int list * term list * (string * typ)
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   798
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   799
val cong_var_prefix = "c"
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   800
35280
54ab4921f826 fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents: 35220
diff changeset
   801
fun special_congruence_axiom T (js1, ts1, x1) (js2, ts2, x2) =
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   802
  let
59058
a78612c67ec0 renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents: 58634
diff changeset
   803
    val (bounds1, bounds2) = apply2 (map Var o special_bounds) (ts1, ts2)
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   804
    val Ts = binder_types T
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   805
    val max_j = fold (fold Integer.max) [js1, js2] ~1
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   806
    val (eqs, (args1, args2)) =
59058
a78612c67ec0 renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents: 58634
diff changeset
   807
      fold (fn j => case apply2 (fn ps => AList.lookup (op =) ps j)
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   808
                                  (js1 ~~ ts1, js2 ~~ ts2) of
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   809
                      (SOME t1, SOME t2) => apfst (cons (t1, t2))
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   810
                    | (SOME t1, NONE) => apsnd (apsnd (cons t1))
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   811
                    | (NONE, SOME t2) => apsnd (apfst (cons t2))
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   812
                    | (NONE, NONE) =>
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   813
                      let val v = Var ((cong_var_prefix ^ nat_subscript j, 0),
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   814
                                       nth Ts j) in
59058
a78612c67ec0 renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents: 58634
diff changeset
   815
                        apsnd (apply2 (cons v))
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   816
                      end) (max_j downto 0) ([], ([], []))
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   817
  in
38165
9797be44df23 prevent generation of needless specialized constants etc.
blanchet
parents: 37928
diff changeset
   818
    Logic.list_implies (eqs |> filter_out (op aconv) |> distinct (op =)
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   819
                            |> map Logic.mk_equals,
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   820
                        Logic.mk_equals (list_comb (Const x1, bounds1 @ args1),
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   821
                                         list_comb (Const x2, bounds2 @ args2)))
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   822
  end
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   823
38170
d74b66ec02ce handle free variables even more gracefully;
blanchet
parents: 38169
diff changeset
   824
fun special_congruence_axioms (hol_ctxt as {special_funs, ...}) ts =
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   825
  let
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   826
    val groups =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   827
      !special_funs
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   828
      |> map (fn ((x, js, ts), x') => (x, (js, ts, x')))
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   829
      |> AList.group (op =)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   830
      |> filter_out (is_equational_fun_surely_complete hol_ctxt o fst)
38170
d74b66ec02ce handle free variables even more gracefully;
blanchet
parents: 38169
diff changeset
   831
      |> map (fn (x, zs) =>
d74b66ec02ce handle free variables even more gracefully;
blanchet
parents: 38169
diff changeset
   832
                 (x, zs |> member (op =) ts (Const x) ? cons ([], [], x)))
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   833
    fun generality (js, _, _) = ~(length js)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   834
    fun is_more_specific (j1, t1, x1) (j2, t2, x2) =
38165
9797be44df23 prevent generation of needless specialized constants etc.
blanchet
parents: 37928
diff changeset
   835
      x1 <> x2 andalso length j2 < length j1 andalso
39687
4e9b6ada3a21 modernized structure Ord_List;
wenzelm
parents: 39360
diff changeset
   836
      Ord_List.subset (prod_ord int_ord Term_Ord.term_ord) (j2 ~~ t2, j1 ~~ t1)
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   837
    fun do_pass_1 _ [] [_] [_] = I
35280
54ab4921f826 fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents: 35220
diff changeset
   838
      | do_pass_1 T skipped _ [] = do_pass_2 T skipped
54ab4921f826 fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents: 35220
diff changeset
   839
      | do_pass_1 T skipped all (z :: zs) =
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   840
        case filter (is_more_specific z) all
59058
a78612c67ec0 renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents: 58634
diff changeset
   841
             |> sort (int_ord o apply2 generality) of
35280
54ab4921f826 fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents: 35220
diff changeset
   842
          [] => do_pass_1 T (z :: skipped) all zs
54ab4921f826 fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents: 35220
diff changeset
   843
        | (z' :: _) => cons (special_congruence_axiom T z z')
54ab4921f826 fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents: 35220
diff changeset
   844
                       #> do_pass_1 T skipped all zs
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   845
    and do_pass_2 _ [] = I
35280
54ab4921f826 fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents: 35220
diff changeset
   846
      | do_pass_2 T (z :: zs) =
54ab4921f826 fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents: 35220
diff changeset
   847
        fold (cons o special_congruence_axiom T z) zs #> do_pass_2 T zs
54ab4921f826 fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents: 35220
diff changeset
   848
  in fold (fn ((_, T), zs) => do_pass_1 T [] zs zs) groups [] end
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   849
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   850
(** Axiom selection **)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   851
38169
b51784515471 optimize local "def"s by treating them as definitions
blanchet
parents: 38166
diff changeset
   852
fun defined_free_by_assumption t =
b51784515471 optimize local "def"s by treating them as definitions
blanchet
parents: 38166
diff changeset
   853
  let
47669
f3896a53043f fix bug where "==" was used instead of "HOL.eq"
blanchet
parents: 46115
diff changeset
   854
    fun do_equals u def =
f3896a53043f fix bug where "==" was used instead of "HOL.eq"
blanchet
parents: 46115
diff changeset
   855
      if exists_subterm (curry (op aconv) u) def then NONE else SOME u
38169
b51784515471 optimize local "def"s by treating them as definitions
blanchet
parents: 38166
diff changeset
   856
  in
b51784515471 optimize local "def"s by treating them as definitions
blanchet
parents: 38166
diff changeset
   857
    case t of
74380
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
   858
      \<^Const_>\<open>Pure.eq _ for \<open>u as Free _\<close> def\<close> => do_equals u def
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
   859
    | \<^Const_>\<open>Trueprop\<close> $ \<^Const_>\<open>HOL.eq _ for \<open>u as Free _\<close> def\<close> => do_equals u def
38169
b51784515471 optimize local "def"s by treating them as definitions
blanchet
parents: 38166
diff changeset
   860
    | _ => NONE
b51784515471 optimize local "def"s by treating them as definitions
blanchet
parents: 38166
diff changeset
   861
  end
b51784515471 optimize local "def"s by treating them as definitions
blanchet
parents: 38166
diff changeset
   862
b51784515471 optimize local "def"s by treating them as definitions
blanchet
parents: 38166
diff changeset
   863
fun assumption_exclusively_defines_free assm_ts t =
b51784515471 optimize local "def"s by treating them as definitions
blanchet
parents: 38166
diff changeset
   864
  case defined_free_by_assumption t of
47669
f3896a53043f fix bug where "==" was used instead of "HOL.eq"
blanchet
parents: 46115
diff changeset
   865
    SOME u =>
f3896a53043f fix bug where "==" was used instead of "HOL.eq"
blanchet
parents: 46115
diff changeset
   866
    length (filter ((fn SOME u' => u aconv u' | NONE => false)
38169
b51784515471 optimize local "def"s by treating them as definitions
blanchet
parents: 38166
diff changeset
   867
                     o defined_free_by_assumption) assm_ts) = 1
b51784515471 optimize local "def"s by treating them as definitions
blanchet
parents: 38166
diff changeset
   868
  | NONE => false
b51784515471 optimize local "def"s by treating them as definitions
blanchet
parents: 38166
diff changeset
   869
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   870
fun all_table_entries table = Symtab.fold (append o snd) table []
55889
6bfbec3dff62 tuned code
blanchet
parents: 55888
diff changeset
   871
41791
01d722707a36 always unfold constant defitions marked with "nitpick_def" -- to prevent unfolding, there's already "nitpick_simp"
blanchet
parents: 41052
diff changeset
   872
fun extra_table tables s =
59058
a78612c67ec0 renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents: 58634
diff changeset
   873
  Symtab.make [(s, apply2 all_table_entries tables |> op @)]
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   874
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   875
fun eval_axiom_for_term j t =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   876
  Logic.mk_equals (Const (eval_prefix ^ string_of_int j, fastype_of t), t)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   877
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   878
val is_trivial_equation = the_default false o try (op aconv o Logic.dest_equals)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   879
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   880
(* Prevents divergence in case of cyclic or infinite axiom dependencies. *)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   881
val axioms_max_depth = 255
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   882
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   883
fun axioms_for_term
55888
cac1add157e8 removed nonstandard models from Nitpick
blanchet
parents: 51706
diff changeset
   884
        (hol_ctxt as {thy, ctxt, max_bisim_depth, user_axioms, evals,
cac1add157e8 removed nonstandard models from Nitpick
blanchet
parents: 51706
diff changeset
   885
                      def_tables, nondef_table, choice_spec_table, nondefs,
cac1add157e8 removed nonstandard models from Nitpick
blanchet
parents: 51706
diff changeset
   886
                      ...}) assm_ts neg_t =
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   887
  let
38170
d74b66ec02ce handle free variables even more gracefully;
blanchet
parents: 38169
diff changeset
   888
    val (def_assm_ts, nondef_assm_ts) =
d74b66ec02ce handle free variables even more gracefully;
blanchet
parents: 38169
diff changeset
   889
      List.partition (assumption_exclusively_defines_free assm_ts) assm_ts
d74b66ec02ce handle free variables even more gracefully;
blanchet
parents: 38169
diff changeset
   890
    val def_assm_table = map (`(the o defined_free_by_assumption)) def_assm_ts
55889
6bfbec3dff62 tuned code
blanchet
parents: 55888
diff changeset
   891
    type accumulator = (string * typ) list * (term list * term list)
38206
78403fcd0ec5 fiddle with specialization etc.
blanchet
parents: 38205
diff changeset
   892
    fun add_axiom get app def depth t (accum as (seen, axs)) =
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   893
      let
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   894
        val t = t |> unfold_defs_in_term hol_ctxt
38206
78403fcd0ec5 fiddle with specialization etc.
blanchet
parents: 38205
diff changeset
   895
                  |> skolemize_term_and_more hol_ctxt ~1 (* FIXME: why ~1? *)
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   896
      in
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   897
        if is_trivial_equation t then
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   898
          accum
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   899
        else
38206
78403fcd0ec5 fiddle with specialization etc.
blanchet
parents: 38205
diff changeset
   900
          let val t' = t |> specialize_consts_in_term hol_ctxt def depth in
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   901
            if exists (member (op aconv) (get axs)) [t, t'] then accum
38170
d74b66ec02ce handle free variables even more gracefully;
blanchet
parents: 38169
diff changeset
   902
            else add_axioms_for_term (depth + 1) t' (seen, app (cons t') axs)
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   903
          end
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   904
      end
38206
78403fcd0ec5 fiddle with specialization etc.
blanchet
parents: 38205
diff changeset
   905
    and add_def_axiom depth = add_axiom fst apfst true depth
78403fcd0ec5 fiddle with specialization etc.
blanchet
parents: 38205
diff changeset
   906
    and add_nondef_axiom depth = add_axiom snd apsnd false depth
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   907
    and add_maybe_def_axiom depth t =
74380
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
   908
      (if head_of t <> \<^Const>\<open>Pure.imp\<close> then add_def_axiom
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   909
       else add_nondef_axiom) depth t
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   910
    and add_eq_axiom depth t =
37256
0dca1ec52999 thread along context instead of theory for typedef lookup
blanchet
parents: 37253
diff changeset
   911
      (if is_constr_pattern_formula ctxt t then add_def_axiom
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   912
       else add_nondef_axiom) depth t
38170
d74b66ec02ce handle free variables even more gracefully;
blanchet
parents: 38169
diff changeset
   913
    and add_axioms_for_term depth t (accum as (seen, axs)) =
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   914
      case t of
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   915
        t1 $ t2 => accum |> fold (add_axioms_for_term depth) [t1, t2]
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   916
      | Const (x as (s, T)) =>
55888
cac1add157e8 removed nonstandard models from Nitpick
blanchet
parents: 51706
diff changeset
   917
        (if member (op aconv) seen t orelse is_built_in_const x then
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   918
           accum
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   919
         else
38170
d74b66ec02ce handle free variables even more gracefully;
blanchet
parents: 38169
diff changeset
   920
           let val accum = (t :: seen, axs) in
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   921
             if depth > axioms_max_depth then
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   922
               raise TOO_LARGE ("Nitpick_Preproc.axioms_for_term.\
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   923
                                \add_axioms_for_term",
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   924
                                "too many nested axioms (" ^
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   925
                                string_of_int depth ^ ")")
37260
dde817e6dfb1 added "atoms" option to Nitpick (request from Karlsruhe) + wrap Refute. functions to "nitpick_util.ML"
blanchet
parents: 37256
diff changeset
   926
             else if is_of_class_const thy x then
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   927
               let
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   928
                 val class = Logic.class_of_const s
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   929
                 val of_class = Logic.mk_of_class (TVar (("'a", 0), [class]),
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   930
                                                   class)
36555
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36389
diff changeset
   931
                 val ax1 = try (specialize_type thy x) of_class
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36389
diff changeset
   932
                 val ax2 = Option.map (specialize_type thy x o snd)
37260
dde817e6dfb1 added "atoms" option to Nitpick (request from Karlsruhe) + wrap Refute. functions to "nitpick_util.ML"
blanchet
parents: 37256
diff changeset
   933
                                      (get_class_def thy class)
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   934
               in
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   935
                 fold (add_maybe_def_axiom depth) (map_filter I [ax1, ax2])
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   936
                      accum
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   937
               end
55888
cac1add157e8 removed nonstandard models from Nitpick
blanchet
parents: 51706
diff changeset
   938
             else if is_constr ctxt x then
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   939
               accum
37271
694aebcd602b fix Nitpick soundness bug regarding The and Eps
blanchet
parents: 37260
diff changeset
   940
             else if is_descr (original_name s) then
694aebcd602b fix Nitpick soundness bug regarding The and Eps
blanchet
parents: 37260
diff changeset
   941
               fold (add_nondef_axiom depth) (equational_fun_axioms hol_ctxt x)
694aebcd602b fix Nitpick soundness bug regarding The and Eps
blanchet
parents: 37260
diff changeset
   942
                    accum
55889
6bfbec3dff62 tuned code
blanchet
parents: 55888
diff changeset
   943
             else if is_equational_fun hol_ctxt x then
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   944
               fold (add_eq_axiom depth) (equational_fun_axioms hol_ctxt x)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   945
                    accum
35807
e4d1b5cbd429 added support for "specification" and "ax_specification" constructs to Nitpick
blanchet
parents: 35718
diff changeset
   946
             else if is_choice_spec_fun hol_ctxt x then
e4d1b5cbd429 added support for "specification" and "ax_specification" constructs to Nitpick
blanchet
parents: 35718
diff changeset
   947
               fold (add_nondef_axiom depth)
e4d1b5cbd429 added support for "specification" and "ax_specification" constructs to Nitpick
blanchet
parents: 35718
diff changeset
   948
                    (nondef_props_for_const thy true choice_spec_table x) accum
37256
0dca1ec52999 thread along context instead of theory for typedef lookup
blanchet
parents: 37253
diff changeset
   949
             else if is_abs_fun ctxt x then
38215
1c7d7eaebdf2 quotient types registered as codatatypes are no longer quotient types
blanchet
parents: 38207
diff changeset
   950
               accum |> fold (add_nondef_axiom depth)
1c7d7eaebdf2 quotient types registered as codatatypes are no longer quotient types
blanchet
parents: 38207
diff changeset
   951
                             (nondef_props_for_const thy false nondef_table x)
38240
a44d108a8d39 local versions of Nitpick.register_xxx functions
blanchet
parents: 38215
diff changeset
   952
                     |> (is_funky_typedef ctxt (range_type T) orelse
38215
1c7d7eaebdf2 quotient types registered as codatatypes are no longer quotient types
blanchet
parents: 38207
diff changeset
   953
                         range_type T = nat_T)
1c7d7eaebdf2 quotient types registered as codatatypes are no longer quotient types
blanchet
parents: 38207
diff changeset
   954
                        ? fold (add_maybe_def_axiom depth)
1c7d7eaebdf2 quotient types registered as codatatypes are no longer quotient types
blanchet
parents: 38207
diff changeset
   955
                               (nondef_props_for_const thy true
41791
01d722707a36 always unfold constant defitions marked with "nitpick_def" -- to prevent unfolding, there's already "nitpick_simp"
blanchet
parents: 41052
diff changeset
   956
                                    (extra_table def_tables s) x)
37256
0dca1ec52999 thread along context instead of theory for typedef lookup
blanchet
parents: 37253
diff changeset
   957
             else if is_rep_fun ctxt x then
38215
1c7d7eaebdf2 quotient types registered as codatatypes are no longer quotient types
blanchet
parents: 38207
diff changeset
   958
               accum |> fold (add_nondef_axiom depth)
1c7d7eaebdf2 quotient types registered as codatatypes are no longer quotient types
blanchet
parents: 38207
diff changeset
   959
                             (nondef_props_for_const thy false nondef_table x)
38240
a44d108a8d39 local versions of Nitpick.register_xxx functions
blanchet
parents: 38215
diff changeset
   960
                     |> (is_funky_typedef ctxt (range_type T) orelse
38215
1c7d7eaebdf2 quotient types registered as codatatypes are no longer quotient types
blanchet
parents: 38207
diff changeset
   961
                         range_type T = nat_T)
1c7d7eaebdf2 quotient types registered as codatatypes are no longer quotient types
blanchet
parents: 38207
diff changeset
   962
                        ? fold (add_maybe_def_axiom depth)
1c7d7eaebdf2 quotient types registered as codatatypes are no longer quotient types
blanchet
parents: 38207
diff changeset
   963
                               (nondef_props_for_const thy true
41791
01d722707a36 always unfold constant defitions marked with "nitpick_def" -- to prevent unfolding, there's already "nitpick_simp"
blanchet
parents: 41052
diff changeset
   964
                                    (extra_table def_tables s) x)
38215
1c7d7eaebdf2 quotient types registered as codatatypes are no longer quotient types
blanchet
parents: 38207
diff changeset
   965
                     |> add_axioms_for_term depth
1c7d7eaebdf2 quotient types registered as codatatypes are no longer quotient types
blanchet
parents: 38207
diff changeset
   966
                                            (Const (mate_of_rep_fun ctxt x))
1c7d7eaebdf2 quotient types registered as codatatypes are no longer quotient types
blanchet
parents: 38207
diff changeset
   967
                     |> fold (add_def_axiom depth)
1c7d7eaebdf2 quotient types registered as codatatypes are no longer quotient types
blanchet
parents: 38207
diff changeset
   968
                             (inverse_axioms_for_rep_fun ctxt x)
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
   969
             else if s = \<^const_name>\<open>Pure.type\<close> then
37253
e01c1fe245cd don't include any axioms for "TYPE" in Nitpick
blanchet
parents: 36555
diff changeset
   970
               accum
41791
01d722707a36 always unfold constant defitions marked with "nitpick_def" -- to prevent unfolding, there's already "nitpick_simp"
blanchet
parents: 41052
diff changeset
   971
             else case def_of_const thy def_tables x of
39345
062c10ff848c remove unreferenced identifiers
blanchet
parents: 38864
diff changeset
   972
               SOME _ =>
38202
379fb08da97b prevent the expansion of too large definitions -- use equations for these instead
blanchet
parents: 38190
diff changeset
   973
               fold (add_eq_axiom depth) (equational_fun_axioms hol_ctxt x)
379fb08da97b prevent the expansion of too large definitions -- use equations for these instead
blanchet
parents: 38190
diff changeset
   974
                    accum
379fb08da97b prevent the expansion of too large definitions -- use equations for these instead
blanchet
parents: 38190
diff changeset
   975
             | NONE =>
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   976
               accum |> user_axioms <> SOME false
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   977
                        ? fold (add_nondef_axiom depth)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   978
                               (nondef_props_for_const thy false nondef_table x)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   979
           end)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   980
        |> add_axioms_for_type depth T
47669
f3896a53043f fix bug where "==" was used instead of "HOL.eq"
blanchet
parents: 46115
diff changeset
   981
      | Free (_, T) =>
38170
d74b66ec02ce handle free variables even more gracefully;
blanchet
parents: 38169
diff changeset
   982
        (if member (op aconv) seen t then
d74b66ec02ce handle free variables even more gracefully;
blanchet
parents: 38169
diff changeset
   983
           accum
47669
f3896a53043f fix bug where "==" was used instead of "HOL.eq"
blanchet
parents: 46115
diff changeset
   984
         else case AList.lookup (op =) def_assm_table t of
38170
d74b66ec02ce handle free variables even more gracefully;
blanchet
parents: 38169
diff changeset
   985
           SOME t => add_def_axiom depth t accum
d74b66ec02ce handle free variables even more gracefully;
blanchet
parents: 38169
diff changeset
   986
         | NONE => accum)
d74b66ec02ce handle free variables even more gracefully;
blanchet
parents: 38169
diff changeset
   987
        |> add_axioms_for_type depth T
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   988
      | Var (_, T) => add_axioms_for_type depth T accum
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   989
      | Bound _ => accum
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   990
      | Abs (_, T, t) => accum |> add_axioms_for_term depth t
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   991
                               |> add_axioms_for_type depth T
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   992
    and add_axioms_for_type depth T =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   993
      case T of
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
   994
        Type (\<^type_name>\<open>fun\<close>, Ts) => fold (add_axioms_for_type depth) Ts
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
   995
      | Type (\<^type_name>\<open>prod\<close>, Ts) => fold (add_axioms_for_type depth) Ts
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
   996
      | Type (\<^type_name>\<open>set\<close>, Ts) => fold (add_axioms_for_type depth) Ts
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
   997
      | \<^typ>\<open>prop\<close> => I
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
   998
      | \<^typ>\<open>bool\<close> => I
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
   999
      | TFree (_, S) => add_axioms_for_sort depth T S
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1000
      | TVar (_, S) => add_axioms_for_sort depth T S
35280
54ab4921f826 fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents: 35220
diff changeset
  1001
      | Type (z as (_, Ts)) =>
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1002
        fold (add_axioms_for_type depth) Ts
37256
0dca1ec52999 thread along context instead of theory for typedef lookup
blanchet
parents: 37253
diff changeset
  1003
        #> (if is_pure_typedef ctxt T then
0dca1ec52999 thread along context instead of theory for typedef lookup
blanchet
parents: 37253
diff changeset
  1004
              fold (add_maybe_def_axiom depth) (optimized_typedef_axioms ctxt z)
38240
a44d108a8d39 local versions of Nitpick.register_xxx functions
blanchet
parents: 38215
diff changeset
  1005
            else if is_quot_type ctxt T then
55888
cac1add157e8 removed nonstandard models from Nitpick
blanchet
parents: 51706
diff changeset
  1006
              fold (add_def_axiom depth) (optimized_quot_type_axioms ctxt z)
38240
a44d108a8d39 local versions of Nitpick.register_xxx functions
blanchet
parents: 38215
diff changeset
  1007
            else if max_bisim_depth >= 0 andalso is_codatatype ctxt T then
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1008
              fold (add_maybe_def_axiom depth)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1009
                   (codatatype_bisim_axioms hol_ctxt T)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1010
            else
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1011
              I)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1012
    and add_axioms_for_sort depth T S =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1013
      let
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1014
        val supers = Sign.complete_sort thy S
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1015
        val class_axioms =
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59058
diff changeset
  1016
          maps (fn class => map Thm.prop_of (Axclass.get_info thy class |> #axioms
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1017
                                         handle ERROR _ => [])) supers
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1018
        val monomorphic_class_axioms =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1019
          map (fn t => case Term.add_tvars t [] of
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1020
                         [] => t
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1021
                       | [(x, S)] =>
61770
a20048c78891 removed needless ML function
blanchet
parents: 61324
diff changeset
  1022
                         Envir.subst_term_types (Vartab.make [(x, (S, T))]) t
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1023
                       | _ => raise TERM ("Nitpick_Preproc.axioms_for_term.\
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1024
                                          \add_axioms_for_sort", [t]))
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1025
              class_axioms
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1026
      in fold (add_nondef_axiom depth) monomorphic_class_axioms end
42415
10accf397ab6 use "Spec_Rules" for finding axioms -- more reliable and cleaner
blanchet
parents: 42361
diff changeset
  1027
    val (mono_nondefs, poly_nondefs) =
10accf397ab6 use "Spec_Rules" for finding axioms -- more reliable and cleaner
blanchet
parents: 42361
diff changeset
  1028
      List.partition (null o Term.hidden_polymorphism) nondefs
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1029
    val eval_axioms = map2 eval_axiom_for_term (index_seq 0 (length evals))
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1030
                           evals
38170
d74b66ec02ce handle free variables even more gracefully;
blanchet
parents: 38169
diff changeset
  1031
    val (seen, (defs, nondefs)) =
38169
b51784515471 optimize local "def"s by treating them as definitions
blanchet
parents: 38166
diff changeset
  1032
      ([], ([], []))
b51784515471 optimize local "def"s by treating them as definitions
blanchet
parents: 38166
diff changeset
  1033
      |> add_axioms_for_term 1 neg_t
b51784515471 optimize local "def"s by treating them as definitions
blanchet
parents: 38166
diff changeset
  1034
      |> fold_rev (add_nondef_axiom 1) nondef_assm_ts
b51784515471 optimize local "def"s by treating them as definitions
blanchet
parents: 38166
diff changeset
  1035
      |> fold_rev (add_def_axiom 1) eval_axioms
42415
10accf397ab6 use "Spec_Rules" for finding axioms -- more reliable and cleaner
blanchet
parents: 42361
diff changeset
  1036
      |> user_axioms = SOME true ? fold (add_nondef_axiom 1) mono_nondefs
38170
d74b66ec02ce handle free variables even more gracefully;
blanchet
parents: 38169
diff changeset
  1037
    val defs = defs @ special_congruence_axioms hol_ctxt seen
35386
45a4e19d3ebd more work on the new monotonicity stuff in Nitpick
blanchet
parents: 35385
diff changeset
  1038
    val got_all_mono_user_axioms =
42415
10accf397ab6 use "Spec_Rules" for finding axioms -- more reliable and cleaner
blanchet
parents: 42361
diff changeset
  1039
      (user_axioms = SOME true orelse null mono_nondefs)
10accf397ab6 use "Spec_Rules" for finding axioms -- more reliable and cleaner
blanchet
parents: 42361
diff changeset
  1040
  in (neg_t :: nondefs, defs, got_all_mono_user_axioms, null poly_nondefs) end
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1041
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1042
(** Simplification of constructor/selector terms **)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1043
37256
0dca1ec52999 thread along context instead of theory for typedef lookup
blanchet
parents: 37253
diff changeset
  1044
fun simplify_constrs_and_sels ctxt t =
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1045
  let
61324
d4ec7594f558 avoid unsound simplification of (C (s x)) when s is a selector but not C's
blanchet
parents: 60352
diff changeset
  1046
    fun is_nth_sel_on constr_s t' n (Const (s, _) $ t) =
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1047
        (t = t' andalso is_sel_like_and_no_discr s andalso
61324
d4ec7594f558 avoid unsound simplification of (C (s x)) when s is a selector but not C's
blanchet
parents: 60352
diff changeset
  1048
         constr_name_for_sel_like s = constr_s andalso sel_no_from_name s = n)
d4ec7594f558 avoid unsound simplification of (C (s x)) when s is a selector but not C's
blanchet
parents: 60352
diff changeset
  1049
      | is_nth_sel_on _ _ _ _ = false
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
  1050
    fun do_term (Const (\<^const_name>\<open>Rep_Frac\<close>, _)
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
  1051
                 $ (Const (\<^const_name>\<open>Abs_Frac\<close>, _) $ t1)) [] =
47909
5f1afeebafbc fixed "real" after they were redefined as a 'quotient_type'
blanchet
parents: 47754
diff changeset
  1052
        do_term t1 []
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
  1053
      | do_term (Const (\<^const_name>\<open>Abs_Frac\<close>, _)
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
  1054
                 $ (Const (\<^const_name>\<open>Rep_Frac\<close>, _) $ t1)) [] =
47909
5f1afeebafbc fixed "real" after they were redefined as a 'quotient_type'
blanchet
parents: 47754
diff changeset
  1055
        do_term t1 []
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1056
      | do_term (t1 $ t2) args = do_term t1 (do_term t2 [] :: args)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1057
      | do_term (t as Const (x as (s, T))) (args as _ :: _) =
55889
6bfbec3dff62 tuned code
blanchet
parents: 55888
diff changeset
  1058
        ((if is_nonfree_constr ctxt x then
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1059
            if length args = num_binder_types T then
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1060
              case hd args of
35280
54ab4921f826 fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents: 35220
diff changeset
  1061
                Const (_, T') $ t' =>
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1062
                if domain_type T' = body_type T andalso
61324
d4ec7594f558 avoid unsound simplification of (C (s x)) when s is a selector but not C's
blanchet
parents: 60352
diff changeset
  1063
                   forall (uncurry (is_nth_sel_on s t'))
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1064
                          (index_seq 0 (length args) ~~ args) then
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1065
                  t'
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1066
                else
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1067
                  raise SAME ()
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1068
              | _ => raise SAME ()
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1069
            else
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1070
              raise SAME ()
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1071
          else if is_sel_like_and_no_discr s then
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1072
            case strip_comb (hd args) of
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1073
              (Const (x' as (s', T')), ts') =>
55889
6bfbec3dff62 tuned code
blanchet
parents: 55888
diff changeset
  1074
              if is_free_constr ctxt x' andalso
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1075
                 constr_name_for_sel_like s = s' andalso
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1076
                 not (exists is_pair_type (binder_types T')) then
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1077
                list_comb (nth ts' (sel_no_from_name s), tl args)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1078
              else
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1079
                raise SAME ()
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1080
            | _ => raise SAME ()
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1081
          else
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1082
            raise SAME ())
37476
0681e46b4022 optimized code generated for datatype cases + more;
blanchet
parents: 37271
diff changeset
  1083
         handle SAME () => s_betapplys [] (t, args))
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1084
      | do_term (Abs (s, T, t')) args =
37476
0681e46b4022 optimized code generated for datatype cases + more;
blanchet
parents: 37271
diff changeset
  1085
        s_betapplys [] (Abs (s, T, do_term t' []), args)
0681e46b4022 optimized code generated for datatype cases + more;
blanchet
parents: 37271
diff changeset
  1086
      | do_term t args = s_betapplys [] (t, args)
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1087
  in do_term t [] end
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1088
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1089
(** Quantifier massaging: Distributing quantifiers **)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1090
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1091
fun distribute_quantifiers t =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1092
  case t of
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
  1093
    (t0 as Const (\<^const_name>\<open>All\<close>, T0)) $ Abs (s, T1, t1) =>
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1094
    (case t1 of
74380
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
  1095
       (t10 as \<^Const_>\<open>conj\<close>) $ t11 $ t12 =>
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1096
       t10 $ distribute_quantifiers (t0 $ Abs (s, T1, t11))
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1097
           $ distribute_quantifiers (t0 $ Abs (s, T1, t12))
74380
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
  1098
     | (t10 as \<^Const_>\<open>Not\<close>) $ t11 =>
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
  1099
       t10 $ distribute_quantifiers (Const (\<^const_name>\<open>Ex\<close>, T0)
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1100
                                     $ Abs (s, T1, t11))
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1101
     | t1 =>
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1102
       if not (loose_bvar1 (t1, 0)) then
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1103
         distribute_quantifiers (incr_boundvars ~1 t1)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1104
       else
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1105
         t0 $ Abs (s, T1, distribute_quantifiers t1))
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
  1106
  | (t0 as Const (\<^const_name>\<open>Ex\<close>, T0)) $ Abs (s, T1, t1) =>
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1107
    (case distribute_quantifiers t1 of
74380
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
  1108
       (t10 as \<^Const_>\<open>disj\<close>) $ t11 $ t12 =>
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1109
       t10 $ distribute_quantifiers (t0 $ Abs (s, T1, t11))
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1110
           $ distribute_quantifiers (t0 $ Abs (s, T1, t12))
74380
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
  1111
     | (t10 as \<^Const_>\<open>implies\<close>) $ t11 $ t12 =>
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
  1112
       t10 $ distribute_quantifiers (Const (\<^const_name>\<open>All\<close>, T0)
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1113
                                     $ Abs (s, T1, t11))
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1114
           $ distribute_quantifiers (t0 $ Abs (s, T1, t12))
74380
79ac28db185c clarified antiquotations;
wenzelm
parents: 69597
diff changeset
  1115
     | (t10 as \<^Const_>\<open>Not\<close>) $ t11 =>
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
  1116
       t10 $ distribute_quantifiers (Const (\<^const_name>\<open>All\<close>, T0)
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1117
                                     $ Abs (s, T1, t11))
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1118
     | t1 =>
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1119
       if not (loose_bvar1 (t1, 0)) then
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1120
         distribute_quantifiers (incr_boundvars ~1 t1)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1121
       else
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1122
         t0 $ Abs (s, T1, distribute_quantifiers t1))
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1123
  | t1 $ t2 => distribute_quantifiers t1 $ distribute_quantifiers t2
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1124
  | Abs (s, T, t') => Abs (s, T, distribute_quantifiers t')
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1125
  | _ => t
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1126
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1127
(** Quantifier massaging: Pushing quantifiers inward **)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1128
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1129
fun renumber_bounds j n f t =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1130
  case t of
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1131
    t1 $ t2 => renumber_bounds j n f t1 $ renumber_bounds j n f t2
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1132
  | Abs (s, T, t') => Abs (s, T, renumber_bounds (j + 1) n f t')
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1133
  | Bound j' =>
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1134
    Bound (if j' >= j andalso j' < j + n then f (j' - j) + j else j')
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1135
  | _ => t
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1136
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1137
(* Maximum number of quantifiers in a cluster for which the exponential
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1138
   algorithm is used. Larger clusters use a heuristic inspired by Claessen &
35386
45a4e19d3ebd more work on the new monotonicity stuff in Nitpick
blanchet
parents: 35385
diff changeset
  1139
   Soerensson's polynomial binary splitting procedure (p. 5 of their MODEL 2003
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1140
   paper). *)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1141
val quantifier_cluster_threshold = 7
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1142
35280
54ab4921f826 fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents: 35220
diff changeset
  1143
val push_quantifiers_inward =
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1144
  let
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1145
    fun aux quant_s ss Ts t =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1146
      (case t of
35280
54ab4921f826 fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents: 35220
diff changeset
  1147
         Const (s0, _) $ Abs (s1, T1, t1 as _ $ _) =>
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1148
         if s0 = quant_s then
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1149
           aux s0 (s1 :: ss) (T1 :: Ts) t1
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1150
         else if quant_s = "" andalso
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 61770
diff changeset
  1151
                 (s0 = \<^const_name>\<open>All\<close> orelse s0 = \<^const_name>\<open>Ex\<close>) then
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1152
           aux s0 [s1] [T1] t1
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1153
         else
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1154
           raise SAME ()
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1155
       | _ => raise SAME ())
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1156
      handle SAME () =>
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1157
             case t of
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1158
               t1 $ t2 =>
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1159
               if quant_s = "" then
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1160
                 aux "" [] [] t1 $ aux "" [] [] t2
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1161
               else
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1162
                 let
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1163
                   fun big_union proj ps =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1164
                     fold (fold (insert (op =)) o proj) ps []
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1165
                   val (ts, connective) = strip_any_connective t
41860
49d0fc8c418c improved "is_special_eligible_arg" further, by approximating the cardinality of the argument (as is done quite successfully elsewhere)
blanchet
parents: 41803
diff changeset
  1166
                   val T_costs = map typical_card_of_type Ts
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1167
                   val t_costs = map size_of_term ts
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1168
                   val num_Ts = length Ts
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1169
                   val flip = curry (op -) (num_Ts - 1)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1170
                   val t_boundss = map (map flip o loose_bnos) ts
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1171
                   fun merge costly_boundss [] = costly_boundss
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1172
                     | merge costly_boundss (j :: js) =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1173
                       let
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1174
                         val (yeas, nays) =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1175
                           List.partition (fn (bounds, _) =>
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1176
                                              member (op =) bounds j)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1177
                                          costly_boundss
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1178
                         val yeas_bounds = big_union fst yeas
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1179
                         val yeas_cost = Integer.sum (map snd yeas)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1180
                                         * nth T_costs j
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1181
                       in merge ((yeas_bounds, yeas_cost) :: nays) js end
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1182
                   val cost = Integer.sum o map snd oo merge
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1183
                   fun heuristically_best_permutation _ [] = []
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1184
                     | heuristically_best_permutation costly_boundss js =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1185
                       let
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1186
                         val (costly_boundss, (j, js)) =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1187
                           js |> map (`(merge costly_boundss o single))
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1188
                              |> sort (int_ord
59058
a78612c67ec0 renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents: 58634
diff changeset
  1189
                                       o apply2 (Integer.sum o map snd o fst))
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1190
                              |> split_list |>> hd ||> pairf hd tl
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1191
                       in
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1192
                         j :: heuristically_best_permutation costly_boundss js
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1193
                       end
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1194
                   val js =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1195
                     if length Ts <= quantifier_cluster_threshold then
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1196
                       all_permutations (index_seq 0 num_Ts)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1197
                       |> map (`(cost (t_boundss ~~ t_costs)))
59058
a78612c67ec0 renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents: 58634
diff changeset
  1198
                       |> sort (int_ord o apply2 fst) |> hd |> snd
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1199
                     else
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1200
                       heuristically_best_permutation (t_boundss ~~ t_costs)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1201
                                                      (index_seq 0 num_Ts)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1202
                   val back_js = map (fn j => find_index (curry (op =) j) js)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1203
                                     (index_seq 0 num_Ts)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1204
                   val ts = map (renumber_bounds 0 num_Ts (nth back_js o flip))
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1205
                                ts
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1206
                   fun mk_connection [] =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1207
                       raise ARG ("Nitpick_Preproc.push_quantifiers_inward.aux.\
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1208
                                  \mk_connection", "")
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1209
                     | mk_connection ts_cum_bounds =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1210
                       ts_cum_bounds |> map fst
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1211
                       |> foldr1 (fn (t1, t2) => connective $ t1 $ t2)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1212
                   fun build ts_cum_bounds [] = ts_cum_bounds |> mk_connection
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1213
                     | build ts_cum_bounds (j :: js) =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1214
                       let
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1215
                         val (yeas, nays) =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1216
                           List.partition (fn (_, bounds) =>
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1217
                                              member (op =) bounds j)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1218
                                          ts_cum_bounds
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1219
                           ||> map (apfst (incr_boundvars ~1))
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1220
                       in
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1221
                         if null yeas then
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1222
                           build nays js
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1223
                         else
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1224
                           let val T = nth Ts (flip j) in
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1225
                             build ((Const (quant_s, (T --> bool_T) --> bool_T)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1226
                                     $ Abs (nth ss (flip j), T,
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1227
                                            mk_connection yeas),
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1228
                                      big_union snd yeas) :: nays) js
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1229
                           end
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1230
                       end
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1231
                 in build (ts ~~ t_boundss) js end
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1232
             | Abs (s, T, t') => Abs (s, T, aux "" [] [] t')
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1233
             | _ => t
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1234
  in aux "" [] [] end
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1235
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1236
(** Preprocessor entry point **)
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1237
37928
24785fa2416c do a better job at Skolemizing in Nitpick, for TPTP FOF
blanchet
parents: 37704
diff changeset
  1238
val max_skolem_depth = 3
36389
8228b3a4a2ba remove "skolemize" option from Nitpick, since Skolemization is always useful
blanchet
parents: 36388
diff changeset
  1239
38169
b51784515471 optimize local "def"s by treating them as definitions
blanchet
parents: 38166
diff changeset
  1240
fun preprocess_formulas
55888
cac1add157e8 removed nonstandard models from Nitpick
blanchet
parents: 51706
diff changeset
  1241
        (hol_ctxt as {ctxt, binary_ints, destroy_constrs, boxes, needs, ...})
cac1add157e8 removed nonstandard models from Nitpick
blanchet
parents: 51706
diff changeset
  1242
        assm_ts neg_t =
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1243
  let
35386
45a4e19d3ebd more work on the new monotonicity stuff in Nitpick
blanchet
parents: 35385
diff changeset
  1244
    val (nondef_ts, def_ts, got_all_mono_user_axioms, no_poly_user_axioms) =
38169
b51784515471 optimize local "def"s by treating them as definitions
blanchet
parents: 38166
diff changeset
  1245
      neg_t |> unfold_defs_in_term hol_ctxt
b51784515471 optimize local "def"s by treating them as definitions
blanchet
parents: 38166
diff changeset
  1246
            |> close_form
b51784515471 optimize local "def"s by treating them as definitions
blanchet
parents: 38166
diff changeset
  1247
            |> skolemize_term_and_more hol_ctxt max_skolem_depth
38206
78403fcd0ec5 fiddle with specialization etc.
blanchet
parents: 38205
diff changeset
  1248
            |> specialize_consts_in_term hol_ctxt false 0
38169
b51784515471 optimize local "def"s by treating them as definitions
blanchet
parents: 38166
diff changeset
  1249
            |> axioms_for_term hol_ctxt assm_ts
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1250
    val binarize =
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1251
      case binary_ints of
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1252
        SOME false => false
35718
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35672
diff changeset
  1253
      | _ => forall (may_use_binary_ints false) nondef_ts andalso
eee1a5e0d334 moved some Nitpick code around
blanchet
parents: 35672
diff changeset
  1254
             forall (may_use_binary_ints true) def_ts andalso
35220
2bcdae5f4fdb added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents: 35190
diff changeset
  1255
             (binary_ints = SOME true orelse
35386
45a4e19d3ebd more work on the new monotonicity stuff in Nitpick
blanchet
parents: 35385
diff changeset
  1256
              exists should_use_binary_ints (nondef_ts @ def_ts))
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1257
    val box = exists (not_equal (SOME false) o snd) boxes
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1258
    val table =
35386
45a4e19d3ebd more work on the new monotonicity stuff in Nitpick
blanchet
parents: 35385
diff changeset
  1259
      Termtab.empty
37256
0dca1ec52999 thread along context instead of theory for typedef lookup
blanchet
parents: 37253
diff changeset
  1260
      |> box ? fold (add_to_uncurry_table ctxt) (nondef_ts @ def_ts)
41803
ef13e3b7cbaf more work on "fix_datatype_vals" optimization (renamed "preconstruct")
blanchet
parents: 41793
diff changeset
  1261
    fun do_middle def =
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1262
      binarize ? binarize_nat_and_int_in_term
36388
30f7ce76712d removed Nitpick's "uncurry" option
blanchet
parents: 36385
diff changeset
  1263
      #> box ? uncurry_term table
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1264
      #> box ? box_fun_and_pair_in_term hol_ctxt def
41803
ef13e3b7cbaf more work on "fix_datatype_vals" optimization (renamed "preconstruct")
blanchet
parents: 41793
diff changeset
  1265
    fun do_tail def =
46102
b669437de253 more robust destruction of "set Collect" idiom
blanchet
parents: 44241
diff changeset
  1266
      destroy_set_Collect
b669437de253 more robust destruction of "set Collect" idiom
blanchet
parents: 44241
diff changeset
  1267
      #> destroy_constrs ? (pull_out_universal_constrs hol_ctxt def
b669437de253 more robust destruction of "set Collect" idiom
blanchet
parents: 44241
diff changeset
  1268
                            #> pull_out_existential_constrs hol_ctxt)
41994
c567c860caf6 always destroy constructor patterns, since this seems to be always useful
blanchet
parents: 41876
diff changeset
  1269
      #> destroy_pulled_out_constrs hol_ctxt def destroy_constrs
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1270
      #> curry_assms
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1271
      #> destroy_universal_equalities
35220
2bcdae5f4fdb added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents: 35190
diff changeset
  1272
      #> destroy_existential_equalities hol_ctxt
37256
0dca1ec52999 thread along context instead of theory for typedef lookup
blanchet
parents: 37253
diff changeset
  1273
      #> simplify_constrs_and_sels ctxt
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1274
      #> distribute_quantifiers
35280
54ab4921f826 fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents: 35220
diff changeset
  1275
      #> push_quantifiers_inward
35078
6fd1052fe463 optimization to quantifiers in Nitpick's handling of simp rules + renamed some SAT solvers
blanchet
parents: 35070
diff changeset
  1276
      #> close_form
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1277
      #> Term.map_abs_vars shortest_name
41803
ef13e3b7cbaf more work on "fix_datatype_vals" optimization (renamed "preconstruct")
blanchet
parents: 41793
diff changeset
  1278
    val nondef_ts = nondef_ts |> map (do_middle false)
41875
e3cd0dce9b1a renamed "preconstr" option "need"
blanchet
parents: 41870
diff changeset
  1279
    val need_ts =
41876
03f699556955 simplify "need" option's syntax
blanchet
parents: 41875
diff changeset
  1280
      case needs of
03f699556955 simplify "need" option's syntax
blanchet
parents: 41875
diff changeset
  1281
        SOME needs =>
03f699556955 simplify "need" option's syntax
blanchet
parents: 41875
diff changeset
  1282
        needs |> map (unfold_defs_in_term hol_ctxt #> do_middle false)
03f699556955 simplify "need" option's syntax
blanchet
parents: 41875
diff changeset
  1283
      | NONE => [] (* FIXME: Implement inference. *)
41803
ef13e3b7cbaf more work on "fix_datatype_vals" optimization (renamed "preconstruct")
blanchet
parents: 41793
diff changeset
  1284
    val nondef_ts = nondef_ts |> map (do_tail false)
ef13e3b7cbaf more work on "fix_datatype_vals" optimization (renamed "preconstruct")
blanchet
parents: 41793
diff changeset
  1285
    val def_ts = def_ts |> map (do_middle true #> do_tail true)
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1286
  in
41875
e3cd0dce9b1a renamed "preconstr" option "need"
blanchet
parents: 41870
diff changeset
  1287
    (nondef_ts, def_ts, need_ts, got_all_mono_user_axioms, no_poly_user_axioms,
e3cd0dce9b1a renamed "preconstr" option "need"
blanchet
parents: 41870
diff changeset
  1288
     binarize)
35070
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1289
  end
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1290
96136eb6218f split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
diff changeset
  1291
end;