src/HOL/Tools/Nitpick/nitpick_model.ML
author blanchet
Tue Jan 03 18:33:17 2012 +0100 (2012-01-03)
changeset 46083 efeaa79f021b
parent 45479 3387d482e0a9
child 46097 0ed9365fa9d2
permissions -rw-r--r--
port part of Nitpick to "set" type constructor
blanchet@33982
     1
(*  Title:      HOL/Tools/Nitpick/nitpick_model.ML
blanchet@33192
     2
    Author:     Jasmin Blanchette, TU Muenchen
blanchet@34982
     3
    Copyright   2009, 2010
blanchet@33192
     4
blanchet@33192
     5
Model reconstruction for Nitpick.
blanchet@33192
     6
*)
blanchet@33192
     7
blanchet@33192
     8
signature NITPICK_MODEL =
blanchet@33192
     9
sig
blanchet@33705
    10
  type styp = Nitpick_Util.styp
blanchet@33232
    11
  type scope = Nitpick_Scope.scope
blanchet@33232
    12
  type rep = Nitpick_Rep.rep
blanchet@33232
    13
  type nut = Nitpick_Nut.nut
blanchet@33192
    14
blanchet@36390
    15
  type params =
blanchet@36390
    16
    {show_datatypes: bool,
blanchet@41993
    17
     show_skolems: bool,
blanchet@36390
    18
     show_consts: bool}
blanchet@36390
    19
blanchet@35712
    20
  type term_postprocessor =
blanchet@35712
    21
    Proof.context -> string -> (typ -> term list) -> typ -> term -> term
blanchet@33192
    22
blanchet@33192
    23
  structure NameTable : TABLE
blanchet@33192
    24
blanchet@35712
    25
  val irrelevant : string
blanchet@35712
    26
  val unknown : string
blanchet@37261
    27
  val unrep : unit -> string
blanchet@35711
    28
  val register_term_postprocessor :
blanchet@38284
    29
    typ -> term_postprocessor -> morphism -> Context.generic -> Context.generic
blanchet@38240
    30
  val register_term_postprocessor_global :
blanchet@35711
    31
    typ -> term_postprocessor -> theory -> theory
blanchet@38284
    32
  val unregister_term_postprocessor :
blanchet@38284
    33
    typ -> morphism -> Context.generic -> Context.generic
blanchet@38240
    34
  val unregister_term_postprocessor_global : typ -> theory -> theory
blanchet@33192
    35
  val tuple_list_for_name :
blanchet@33192
    36
    nut NameTable.table -> Kodkod.raw_bound list -> nut -> int list list
blanchet@35712
    37
  val dest_plain_fun : term -> bool * (term list * term list)
blanchet@33192
    38
  val reconstruct_hol_model :
blanchet@37260
    39
    params -> scope -> (term option * int list) list
blanchet@38170
    40
    -> (typ option * string list) list -> styp list -> styp list -> nut list
blanchet@38170
    41
    -> nut list -> nut list -> nut NameTable.table -> Kodkod.raw_bound list
blanchet@33192
    42
    -> Pretty.T * bool
blanchet@33192
    43
  val prove_hol_model :
blanchet@33192
    44
    scope -> Time.time option -> nut list -> nut list -> nut NameTable.table
blanchet@33192
    45
    -> Kodkod.raw_bound list -> term -> bool option
blanchet@33192
    46
end;
blanchet@33192
    47
blanchet@33232
    48
structure Nitpick_Model : NITPICK_MODEL =
blanchet@33192
    49
struct
blanchet@33192
    50
blanchet@33232
    51
open Nitpick_Util
blanchet@33232
    52
open Nitpick_HOL
blanchet@33232
    53
open Nitpick_Scope
blanchet@33232
    54
open Nitpick_Peephole
blanchet@33232
    55
open Nitpick_Rep
blanchet@33232
    56
open Nitpick_Nut
blanchet@33192
    57
blanchet@34126
    58
structure KK = Kodkod
blanchet@34126
    59
blanchet@36390
    60
type params =
blanchet@36390
    61
  {show_datatypes: bool,
blanchet@41993
    62
   show_skolems: bool,
blanchet@36390
    63
   show_consts: bool}
blanchet@33192
    64
blanchet@35712
    65
type term_postprocessor =
blanchet@35712
    66
  Proof.context -> string -> (typ -> term list) -> typ -> term -> term
blanchet@35711
    67
wenzelm@41472
    68
structure Data = Generic_Data
wenzelm@41472
    69
(
blanchet@35711
    70
  type T = (typ * term_postprocessor) list
blanchet@35711
    71
  val empty = []
blanchet@35711
    72
  val extend = I
wenzelm@41472
    73
  fun merge data = AList.merge (op =) (K true) data
wenzelm@41472
    74
)
blanchet@35711
    75
blanchet@37261
    76
fun xsym s s' () = if print_mode_active Symbol.xsymbolsN then s else s'
blanchet@37261
    77
blanchet@35712
    78
val irrelevant = "_"
blanchet@33192
    79
val unknown = "?"
blanchet@37261
    80
val unrep = xsym "\<dots>" "..."
blanchet@37261
    81
val maybe_mixfix = xsym "_\<^sup>?" "_?"
blanchet@37261
    82
val base_mixfix = xsym "_\<^bsub>base\<^esub>" "_.base"
blanchet@37261
    83
val step_mixfix = xsym "_\<^bsub>step\<^esub>" "_.step"
blanchet@37261
    84
val abs_mixfix = xsym "\<guillemotleft>_\<guillemotright>" "\"_\""
blanchet@35718
    85
val arg_var_prefix = "x"
blanchet@37261
    86
val cyclic_co_val_name = xsym "\<omega>" "w"
blanchet@37261
    87
val cyclic_const_prefix = xsym "\<xi>" "X"
blanchet@37261
    88
fun cyclic_type_name () = nitpick_prefix ^ cyclic_const_prefix ()
blanchet@34982
    89
val opt_flag = nitpick_prefix ^ "opt"
blanchet@34982
    90
val non_opt_flag = nitpick_prefix ^ "non_opt"
blanchet@33192
    91
blanchet@35076
    92
type atom_pool = ((string * int) * int list) list
blanchet@35076
    93
blanchet@35718
    94
fun add_wacky_syntax ctxt =
blanchet@35718
    95
  let
blanchet@35718
    96
    val name_of = fst o dest_Const
wenzelm@42361
    97
    val thy = Proof_Context.theory_of ctxt |> Context.reject_draft
blanchet@35718
    98
    val (maybe_t, thy) =
wenzelm@42375
    99
      Sign.declare_const_global ((@{binding nitpick_maybe}, @{typ "'a => 'a"}),
blanchet@37261
   100
                          Mixfix (maybe_mixfix (), [1000], 1000)) thy
blanchet@35718
   101
    val (abs_t, thy) =
wenzelm@42375
   102
      Sign.declare_const_global ((@{binding nitpick_abs}, @{typ "'a => 'b"}),
blanchet@37261
   103
                          Mixfix (abs_mixfix (), [40], 40)) thy
blanchet@35718
   104
    val (base_t, thy) =
wenzelm@42375
   105
      Sign.declare_const_global ((@{binding nitpick_base}, @{typ "'a => 'a"}),
blanchet@37261
   106
                          Mixfix (base_mixfix (), [1000], 1000)) thy
blanchet@35718
   107
    val (step_t, thy) =
wenzelm@42375
   108
      Sign.declare_const_global ((@{binding nitpick_step}, @{typ "'a => 'a"}),
blanchet@37261
   109
                          Mixfix (step_mixfix (), [1000], 1000)) thy
blanchet@35718
   110
  in
blanchet@35718
   111
    (pairself (pairself name_of) ((maybe_t, abs_t), (base_t, step_t)),
wenzelm@42361
   112
     Proof_Context.transfer_syntax thy ctxt)
blanchet@35718
   113
  end
blanchet@35718
   114
blanchet@35718
   115
(** Term reconstruction **)
blanchet@35718
   116
blanchet@37260
   117
fun nth_atom_number pool T j =
blanchet@37260
   118
  case AList.lookup (op =) (!pool) T of
blanchet@37260
   119
    SOME js =>
blanchet@37260
   120
    (case find_index (curry (op =) j) js of
blanchet@37260
   121
       ~1 => (Unsynchronized.change pool (cons (T, j :: js));
blanchet@37260
   122
              length js + 1)
blanchet@37260
   123
     | n => length js - n)
blanchet@37260
   124
  | NONE => (Unsynchronized.change pool (cons (T, [j])); 1)
blanchet@37260
   125
fun atom_suffix s =
blanchet@37260
   126
  nat_subscript
wenzelm@40627
   127
  #> (s <> "" andalso Symbol.is_ascii_digit (List.last (raw_explode s)))  (* FIXME Symbol.explode (?) *)
blanchet@33884
   128
     ? prefix "\<^isub>,"
blanchet@37260
   129
fun nth_atom_name thy atomss pool prefix T j =
blanchet@37260
   130
  let
blanchet@37260
   131
    val ss = these (triple_lookup (type_match thy) atomss T)
blanchet@37260
   132
    val m = nth_atom_number pool T j
blanchet@37260
   133
  in
blanchet@37260
   134
    if m <= length ss then
blanchet@37260
   135
      nth ss (m - 1)
blanchet@37260
   136
    else case T of
blanchet@37260
   137
      Type (s, _) =>
blanchet@37260
   138
      let val s' = shortest_name s in
blanchet@37260
   139
        prefix ^
blanchet@41039
   140
        (if T = @{typ string} then "s"
blanchet@41039
   141
         else if String.isPrefix "\\" s' then s'
blanchet@41039
   142
         else substring (s', 0, 1)) ^ atom_suffix s m
blanchet@37260
   143
      end
blanchet@37260
   144
    | TFree (s, _) => prefix ^ perhaps (try (unprefix "'")) s ^ atom_suffix s m
blanchet@37260
   145
    | _ => raise TYPE ("Nitpick_Model.nth_atom_name", [T], [])
blanchet@37260
   146
  end
blanchet@37260
   147
fun nth_atom thy atomss pool for_auto T j =
blanchet@33192
   148
  if for_auto then
blanchet@37260
   149
    Free (nth_atom_name thy atomss pool (hd (space_explode "." nitpick_prefix))
blanchet@37260
   150
                        T j, T)
blanchet@33192
   151
  else
blanchet@37260
   152
    Const (nth_atom_name thy atomss pool "" T j, T)
blanchet@33192
   153
blanchet@35177
   154
fun extract_real_number (Const (@{const_name divide}, _) $ t1 $ t2) =
blanchet@34126
   155
    real (snd (HOLogic.dest_number t1)) / real (snd (HOLogic.dest_number t2))
blanchet@34126
   156
  | extract_real_number t = real (snd (HOLogic.dest_number t))
blanchet@34124
   157
fun nice_term_ord (Abs (_, _, t1), Abs (_, _, t2)) = nice_term_ord (t1, t2)
blanchet@34126
   158
  | nice_term_ord tp = Real.compare (pairself extract_real_number tp)
blanchet@34124
   159
    handle TERM ("dest_number", _) =>
blanchet@34126
   160
           case tp of
blanchet@34124
   161
             (t11 $ t12, t21 $ t22) =>
blanchet@34124
   162
             (case nice_term_ord (t11, t21) of
blanchet@34124
   163
                EQUAL => nice_term_ord (t12, t22)
blanchet@34124
   164
              | ord => ord)
wenzelm@35408
   165
           | _ => Term_Ord.fast_term_ord tp
blanchet@34124
   166
blanchet@38284
   167
fun register_term_postprocessor_generic T postproc =
blanchet@38284
   168
  Data.map (cons (T, postproc))
blanchet@38284
   169
(* TODO: Consider morphism. *)
blanchet@38284
   170
fun register_term_postprocessor T postproc (_ : morphism) =
blanchet@38284
   171
  register_term_postprocessor_generic T postproc
blanchet@38240
   172
val register_term_postprocessor_global =
blanchet@38240
   173
  Context.theory_map oo register_term_postprocessor_generic
blanchet@38240
   174
blanchet@38240
   175
fun unregister_term_postprocessor_generic T = Data.map (AList.delete (op =) T)
blanchet@38284
   176
(* TODO: Consider morphism. *)
blanchet@38284
   177
fun unregister_term_postprocessor T (_ : morphism) =
blanchet@38284
   178
  unregister_term_postprocessor_generic T
blanchet@38240
   179
val unregister_term_postprocessor_global =
blanchet@38240
   180
  Context.theory_map o unregister_term_postprocessor_generic
blanchet@35711
   181
blanchet@33192
   182
fun tuple_list_for_name rel_table bounds name =
blanchet@33192
   183
  the (AList.lookup (op =) bounds (the_rel rel_table name)) handle NUT _ => [[]]
blanchet@33192
   184
blanchet@41052
   185
fun unarize_unbox_etc_term (Const (@{const_name FunBox}, _) $ t1) =
blanchet@35665
   186
    unarize_unbox_etc_term t1
blanchet@35665
   187
  | unarize_unbox_etc_term
blanchet@35665
   188
        (Const (@{const_name PairBox},
blanchet@35665
   189
                Type (@{type_name fun}, [T1, Type (@{type_name fun}, [T2, _])]))
blanchet@35665
   190
         $ t1 $ t2) =
blanchet@35665
   191
    let val Ts = map uniterize_unarize_unbox_etc_type [T1, T2] in
blanchet@38190
   192
      Const (@{const_name Pair}, Ts ---> Type (@{type_name prod}, Ts))
blanchet@35665
   193
      $ unarize_unbox_etc_term t1 $ unarize_unbox_etc_term t2
blanchet@33192
   194
    end
blanchet@35665
   195
  | unarize_unbox_etc_term (Const (s, T)) =
blanchet@35665
   196
    Const (s, uniterize_unarize_unbox_etc_type T)
blanchet@35665
   197
  | unarize_unbox_etc_term (t1 $ t2) =
blanchet@35665
   198
    unarize_unbox_etc_term t1 $ unarize_unbox_etc_term t2
blanchet@35665
   199
  | unarize_unbox_etc_term (Free (s, T)) =
blanchet@35665
   200
    Free (s, uniterize_unarize_unbox_etc_type T)
blanchet@35665
   201
  | unarize_unbox_etc_term (Var (x, T)) =
blanchet@35665
   202
    Var (x, uniterize_unarize_unbox_etc_type T)
blanchet@35665
   203
  | unarize_unbox_etc_term (Bound j) = Bound j
blanchet@35665
   204
  | unarize_unbox_etc_term (Abs (s, T, t')) =
blanchet@35665
   205
    Abs (s, uniterize_unarize_unbox_etc_type T, unarize_unbox_etc_term t')
blanchet@33192
   206
blanchet@38190
   207
fun factor_out_types (T1 as Type (@{type_name prod}, [T11, T12]))
blanchet@38190
   208
                     (T2 as Type (@{type_name prod}, [T21, T22])) =
blanchet@33192
   209
    let val (n1, n2) = pairself num_factors_in_type (T11, T21) in
blanchet@33192
   210
      if n1 = n2 then
blanchet@33192
   211
        let
blanchet@33192
   212
          val ((T11', opt_T12'), (T21', opt_T22')) = factor_out_types T12 T22
blanchet@33192
   213
        in
blanchet@38190
   214
          ((Type (@{type_name prod}, [T11, T11']), opt_T12'),
blanchet@38190
   215
           (Type (@{type_name prod}, [T21, T21']), opt_T22'))
blanchet@33192
   216
        end
blanchet@33192
   217
      else if n1 < n2 then
blanchet@33192
   218
        case factor_out_types T1 T21 of
blanchet@33192
   219
          (p1, (T21', NONE)) => (p1, (T21', SOME T22))
blanchet@33192
   220
        | (p1, (T21', SOME T22')) =>
blanchet@38190
   221
          (p1, (T21', SOME (Type (@{type_name prod}, [T22', T22]))))
blanchet@33192
   222
      else
blanchet@33192
   223
        swap (factor_out_types T2 T1)
blanchet@33192
   224
    end
blanchet@38190
   225
  | factor_out_types (Type (@{type_name prod}, [T11, T12])) T2 =
blanchet@35665
   226
    ((T11, SOME T12), (T2, NONE))
blanchet@38190
   227
  | factor_out_types T1 (Type (@{type_name prod}, [T21, T22])) =
blanchet@35665
   228
    ((T1, NONE), (T21, SOME T22))
blanchet@33192
   229
  | factor_out_types T1 T2 = ((T1, NONE), (T2, NONE))
blanchet@33192
   230
blanchet@46083
   231
(* Term-encoded data structure for holding key-value pairs as well as an "opt"
blanchet@46083
   232
   flag indicating whether the function is approximated. *)
blanchet@33192
   233
fun make_plain_fun maybe_opt T1 T2 =
blanchet@33192
   234
  let
blanchet@33192
   235
    fun aux T1 T2 [] =
blanchet@34982
   236
        Const (if maybe_opt then opt_flag else non_opt_flag, T1 --> T2)
blanchet@35712
   237
      | aux T1 T2 ((t1, t2) :: tps) =
blanchet@34124
   238
        Const (@{const_name fun_upd}, (T1 --> T2) --> T1 --> T2 --> T1 --> T2)
blanchet@35712
   239
        $ aux T1 T2 tps $ t1 $ t2
blanchet@33192
   240
  in aux T1 T2 o rev end
blanchet@34982
   241
fun is_plain_fun (Const (s, _)) = (s = opt_flag orelse s = non_opt_flag)
blanchet@33192
   242
  | is_plain_fun (Const (@{const_name fun_upd}, _) $ t0 $ _ $ _) =
blanchet@33192
   243
    is_plain_fun t0
blanchet@33192
   244
  | is_plain_fun _ = false
blanchet@33192
   245
val dest_plain_fun =
blanchet@33192
   246
  let
blanchet@35712
   247
    fun aux (Abs (_, _, Const (s, _))) = (s <> irrelevant, ([], []))
blanchet@35712
   248
      | aux (Const (s, _)) = (s <> non_opt_flag, ([], []))
blanchet@33192
   249
      | aux (Const (@{const_name fun_upd}, _) $ t0 $ t1 $ t2) =
blanchet@35712
   250
        let val (maybe_opt, (ts1, ts2)) = aux t0 in
blanchet@35712
   251
          (maybe_opt, (t1 :: ts1, t2 :: ts2))
blanchet@35712
   252
        end
blanchet@33232
   253
      | aux t = raise TERM ("Nitpick_Model.dest_plain_fun", [t])
blanchet@33192
   254
  in apsnd (pairself rev) o aux end
blanchet@33192
   255
blanchet@33565
   256
fun break_in_two T T1 T2 t =
blanchet@33565
   257
  let
blanchet@33565
   258
    val ps = HOLogic.flat_tupleT_paths T
blanchet@33565
   259
    val cut = length (HOLogic.strip_tupleT T1)
blanchet@33565
   260
    val (ps1, ps2) = pairself HOLogic.flat_tupleT_paths (T1, T2)
blanchet@33565
   261
    val (ts1, ts2) = t |> HOLogic.strip_ptuple ps |> chop cut
blanchet@33565
   262
  in (HOLogic.mk_ptuple ps1 T1 ts1, HOLogic.mk_ptuple ps2 T2 ts2) end
blanchet@38190
   263
fun pair_up (Type (@{type_name prod}, [T1', T2']))
blanchet@33192
   264
            (t1 as Const (@{const_name Pair},
blanchet@35665
   265
                          Type (@{type_name fun},
blanchet@35665
   266
                                [_, Type (@{type_name fun}, [_, T1])]))
blanchet@35665
   267
             $ t11 $ t12) t2 =
blanchet@33192
   268
    if T1 = T1' then HOLogic.mk_prod (t1, t2)
blanchet@33192
   269
    else HOLogic.mk_prod (t11, pair_up T2' t12 t2)
blanchet@33192
   270
  | pair_up _ t1 t2 = HOLogic.mk_prod (t1, t2)
blanchet@33192
   271
fun multi_pair_up T1 t1 (ts2, ts3) = map2 (pair o pair_up T1 t1) ts2 ts3
blanchet@33192
   272
blanchet@46083
   273
fun format_fun T' T1 T2 t =
blanchet@46083
   274
  let
blanchet@46083
   275
    val T1' = pseudo_domain_type T'
blanchet@46083
   276
    val T2' = pseudo_range_type T'
blanchet@46083
   277
    fun do_curry T1 T1a T1b T2 t =
blanchet@46083
   278
      let
blanchet@46083
   279
        val (maybe_opt, tsp) = dest_plain_fun t
blanchet@46083
   280
        val tps =
blanchet@46083
   281
          tsp |>> map (break_in_two T1 T1a T1b)
blanchet@46083
   282
              |> uncurry (map2 (fn (t1a, t1b) => fn t2 => (t1a, (t1b, t2))))
blanchet@46083
   283
              |> AList.coalesce (op =)
blanchet@46083
   284
              |> map (apsnd (make_plain_fun maybe_opt T1b T2))
blanchet@46083
   285
      in make_plain_fun maybe_opt T1a (T1b --> T2) tps end
blanchet@46083
   286
    and do_uncurry T1 T2 t =
blanchet@46083
   287
      let
blanchet@46083
   288
        val (maybe_opt, tsp) = dest_plain_fun t
blanchet@46083
   289
        val tps =
blanchet@46083
   290
          tsp |> op ~~
blanchet@46083
   291
              |> maps (fn (t1, t2) =>
blanchet@46083
   292
                          multi_pair_up T1 t1 (snd (dest_plain_fun t2)))
blanchet@46083
   293
      in make_plain_fun maybe_opt T1 T2 tps end
blanchet@46083
   294
    and do_arrow T1' T2' _ _ (Const (s, _)) = Const (s, T1' --> T2')
blanchet@46083
   295
      | do_arrow T1' T2' T1 T2
blanchet@46083
   296
                 (Const (@{const_name fun_upd}, _) $ t0 $ t1 $ t2) =
blanchet@46083
   297
        Const (@{const_name fun_upd},
blanchet@46083
   298
               (T1' --> T2') --> T1' --> T2' --> T1' --> T2')
blanchet@46083
   299
        $ do_arrow T1' T2' T1 T2 t0 $ do_term T1' T1 t1 $ do_term T2' T2 t2
blanchet@46083
   300
      | do_arrow _ _ _ _ t =
blanchet@46083
   301
        raise TERM ("Nitpick_Model.format_fun.do_arrow", [t])
blanchet@46083
   302
    and do_fun T1' T2' T1 T2 t =
blanchet@46083
   303
      case factor_out_types T1' T1 of
blanchet@46083
   304
        ((_, NONE), (_, NONE)) => t |> do_arrow T1' T2' T1 T2
blanchet@46083
   305
      | ((_, NONE), (T1a, SOME T1b)) =>
blanchet@46083
   306
        t |> do_curry T1 T1a T1b T2 |> do_arrow T1' T2' T1a (T1b --> T2)
blanchet@46083
   307
      | ((T1a', SOME T1b'), (_, NONE)) =>
blanchet@46083
   308
        t |> do_arrow T1a' (T1b' --> T2') T1 T2 |> do_uncurry T1' T2'
blanchet@46083
   309
      | _ => raise TYPE ("Nitpick_Model.format_fun.do_fun", [T1, T1'], [])
blanchet@46083
   310
    and do_term (Type (@{type_name fun}, [T1', T2']))
blanchet@46083
   311
                (Type (@{type_name fun}, [T1, T2])) t =
blanchet@46083
   312
        do_fun T1' T2' T1 T2 t
blanchet@46083
   313
      | do_term (T' as Type (@{type_name prod}, Ts' as [T1', T2']))
blanchet@46083
   314
                (Type (@{type_name prod}, [T1, T2]))
blanchet@46083
   315
                (Const (@{const_name Pair}, _) $ t1 $ t2) =
blanchet@46083
   316
        Const (@{const_name Pair}, Ts' ---> T')
blanchet@46083
   317
        $ do_term T1' T1 t1 $ do_term T2' T2 t2
blanchet@46083
   318
      | do_term T' T t =
blanchet@46083
   319
        if T = T' then t
blanchet@46083
   320
        else raise TYPE ("Nitpick_Model.format_fun.do_term", [T, T'], [])
blanchet@46083
   321
  in if T1' = T1 andalso T2' = T2 then t else do_fun T1' T2' T1 T2 t end
blanchet@33192
   322
blanchet@33192
   323
fun truth_const_sort_key @{const True} = "0"
blanchet@33192
   324
  | truth_const_sort_key @{const False} = "2"
blanchet@33192
   325
  | truth_const_sort_key _ = "1"
blanchet@33192
   326
blanchet@38190
   327
fun mk_tuple (Type (@{type_name prod}, [T1, T2])) ts =
blanchet@33192
   328
    HOLogic.mk_prod (mk_tuple T1 ts,
blanchet@33192
   329
        mk_tuple T2 (List.drop (ts, length (HOLogic.flatten_tupleT T1))))
blanchet@33192
   330
  | mk_tuple _ (t :: _) = t
blanchet@34124
   331
  | mk_tuple T [] = raise TYPE ("Nitpick_Model.mk_tuple", [T], [])
blanchet@33192
   332
blanchet@38240
   333
fun varified_type_match ctxt (candid_T, pat_T) =
wenzelm@42361
   334
  let val thy = Proof_Context.theory_of ctxt in
blanchet@38240
   335
    strict_type_match thy (candid_T, varify_type ctxt pat_T)
blanchet@38240
   336
  end
blanchet@35711
   337
blanchet@35712
   338
fun all_values_of_type pool wacky_names (scope as {card_assigns, ...} : scope)
blanchet@37260
   339
                       atomss sel_names rel_table bounds card T =
blanchet@35712
   340
  let
blanchet@35712
   341
    val card = if card = 0 then card_of_type card_assigns T else card
blanchet@35712
   342
    fun nth_value_of_type n =
blanchet@35712
   343
      let
blanchet@35712
   344
        fun term unfold =
blanchet@37262
   345
          reconstruct_term true unfold pool wacky_names scope atomss sel_names
blanchet@37260
   346
                           rel_table bounds T T (Atom (card, 0)) [[n]]
blanchet@35712
   347
      in
blanchet@35712
   348
        case term false of
blanchet@35712
   349
          t as Const (s, _) =>
blanchet@37261
   350
          if String.isPrefix (cyclic_const_prefix ()) s then
blanchet@35712
   351
            HOLogic.mk_eq (t, term true)
blanchet@35712
   352
          else
blanchet@35712
   353
            t
blanchet@35712
   354
        | t => t
blanchet@35712
   355
      end
blanchet@35712
   356
  in index_seq 0 card |> map nth_value_of_type |> sort nice_term_ord end
blanchet@37262
   357
and reconstruct_term maybe_opt unfold pool
blanchet@37262
   358
        (wacky_names as ((maybe_name, abs_name), _))
blanchet@37256
   359
        (scope as {hol_ctxt as {thy, ctxt, stds, ...}, binarize, card_assigns,
blanchet@37260
   360
                   bits, datatypes, ofs, ...})
blanchet@37260
   361
        atomss sel_names rel_table bounds =
blanchet@33192
   362
  let
blanchet@33192
   363
    val for_auto = (maybe_name = "")
blanchet@34124
   364
    fun value_of_bits jss =
blanchet@34124
   365
      let
blanchet@34124
   366
        val j0 = offset_of_type ofs @{typ unsigned_bit}
blanchet@34124
   367
        val js = map (Integer.add (~ j0) o the_single) jss
blanchet@34124
   368
      in
blanchet@34124
   369
        fold (fn j => Integer.add (reasonable_power 2 j |> j = bits ? op ~))
blanchet@34124
   370
             js 0
blanchet@34124
   371
      end
blanchet@35712
   372
    val all_values =
blanchet@37260
   373
      all_values_of_type pool wacky_names scope atomss sel_names rel_table
blanchet@37260
   374
                         bounds 0
blanchet@35712
   375
    fun postprocess_term (Type (@{type_name fun}, _)) = I
blanchet@35712
   376
      | postprocess_term T =
blanchet@38240
   377
        case Data.get (Context.Proof ctxt) of
blanchet@38240
   378
          [] => I
blanchet@38240
   379
        | postprocs =>
blanchet@38240
   380
          case AList.lookup (varified_type_match ctxt) postprocs T of
blanchet@38240
   381
            SOME postproc => postproc ctxt maybe_name all_values T
blanchet@38240
   382
          | NONE => I
blanchet@35712
   383
    fun postprocess_subterms Ts (t1 $ t2) =
blanchet@35712
   384
        let val t = postprocess_subterms Ts t1 $ postprocess_subterms Ts t2 in
blanchet@35712
   385
          postprocess_term (fastype_of1 (Ts, t)) t
blanchet@35712
   386
        end
blanchet@35712
   387
      | postprocess_subterms Ts (Abs (s, T, t')) =
blanchet@35712
   388
        Abs (s, T, postprocess_subterms (T :: Ts) t')
blanchet@35712
   389
      | postprocess_subterms Ts t = postprocess_term (fastype_of1 (Ts, t)) t
blanchet@46083
   390
    fun make_set maybe_opt T tps =
blanchet@33192
   391
      let
blanchet@46083
   392
        val empty_const = Const (@{const_abbrev Set.empty}, T --> bool_T)
blanchet@46083
   393
        val insert_const =
blanchet@46083
   394
          Const (@{const_name insert}, T --> (T --> bool_T) --> T --> bool_T)
blanchet@33192
   395
        fun aux [] =
blanchet@46083
   396
            if maybe_opt andalso not (is_complete_type datatypes false T) then
blanchet@46083
   397
              insert_const $ Const (unrep (), T) $ empty_const
blanchet@33192
   398
            else
blanchet@33192
   399
              empty_const
blanchet@33192
   400
          | aux ((t1, t2) :: zs) =
blanchet@35388
   401
            aux zs
blanchet@35388
   402
            |> t2 <> @{const False}
blanchet@35388
   403
               ? curry (op $)
blanchet@35388
   404
                       (insert_const
blanchet@35388
   405
                        $ (t1 |> t2 <> @{const True}
blanchet@35388
   406
                                 ? curry (op $)
blanchet@46083
   407
                                         (Const (maybe_name, T --> T))))
blanchet@35388
   408
      in
blanchet@35388
   409
        if forall (fn (_, t) => t <> @{const True} andalso t <> @{const False})
blanchet@35388
   410
                  tps then
blanchet@46083
   411
          Const (unknown, T --> bool_T)
blanchet@35388
   412
        else
blanchet@35388
   413
          aux tps
blanchet@35388
   414
      end
blanchet@35665
   415
    fun make_map maybe_opt T1 T2 T2' =
blanchet@33192
   416
      let
blanchet@33192
   417
        val update_const = Const (@{const_name fun_upd},
blanchet@34124
   418
                                  (T1 --> T2) --> T1 --> T2 --> T1 --> T2)
wenzelm@35402
   419
        fun aux' [] = Const (@{const_abbrev Map.empty}, T1 --> T2)
blanchet@35712
   420
          | aux' ((t1, t2) :: tps) =
blanchet@33192
   421
            (case t2 of
blanchet@35712
   422
               Const (@{const_name None}, _) => aux' tps
blanchet@35712
   423
             | _ => update_const $ aux' tps $ t1 $ t2)
blanchet@35712
   424
        fun aux tps =
blanchet@35665
   425
          if maybe_opt andalso not (is_complete_type datatypes false T1) then
blanchet@37261
   426
            update_const $ aux' tps $ Const (unrep (), T1)
blanchet@33192
   427
            $ (Const (@{const_name Some}, T2' --> T2) $ Const (unknown, T2'))
blanchet@33192
   428
          else
blanchet@35712
   429
            aux' tps
blanchet@33192
   430
      in aux end
blanchet@34982
   431
    fun polish_funs Ts t =
blanchet@33192
   432
      (case fastype_of1 (Ts, t) of
blanchet@35665
   433
         Type (@{type_name fun}, [T1, T2]) =>
blanchet@33192
   434
         if is_plain_fun t then
blanchet@33192
   435
           case T2 of
blanchet@46083
   436
             Type (@{type_name option}, [T2']) =>
blanchet@33192
   437
             let
blanchet@35665
   438
               val (maybe_opt, ts_pair) =
blanchet@35665
   439
                 dest_plain_fun t ||> pairself (map (polish_funs Ts))
blanchet@35665
   440
             in make_map maybe_opt T1 T2 T2' (rev (op ~~ ts_pair)) end
blanchet@33192
   441
           | _ => raise SAME ()
blanchet@33192
   442
         else
blanchet@33192
   443
           raise SAME ()
blanchet@33192
   444
       | _ => raise SAME ())
blanchet@33192
   445
      handle SAME () =>
blanchet@33192
   446
             case t of
blanchet@34982
   447
               (t1 as Const (@{const_name fun_upd}, _) $ t11 $ _)
blanchet@34982
   448
               $ (t2 as Const (s, _)) =>
blanchet@34982
   449
               if s = unknown then polish_funs Ts t11
blanchet@34982
   450
               else polish_funs Ts t1 $ polish_funs Ts t2
blanchet@34982
   451
             | t1 $ t2 => polish_funs Ts t1 $ polish_funs Ts t2
blanchet@34982
   452
             | Abs (s, T, t') => Abs (s, T, polish_funs (T :: Ts) t')
blanchet@35665
   453
             | Const (s, Type (@{type_name fun}, [T1, T2])) =>
blanchet@34982
   454
               if s = opt_flag orelse s = non_opt_flag then
blanchet@35712
   455
                 Abs ("x", T1,
blanchet@35712
   456
                      Const (if is_complete_type datatypes false T1 then
blanchet@35712
   457
                               irrelevant
blanchet@35712
   458
                             else
blanchet@35712
   459
                               unknown, T2))
blanchet@34982
   460
               else
blanchet@34982
   461
                 t
blanchet@34982
   462
             | t => t
blanchet@46083
   463
    fun make_fun_or_set maybe_opt T T1 T2 T' ts1 ts2 =
blanchet@46083
   464
      ts1 ~~ ts2
blanchet@46083
   465
      |> sort (nice_term_ord o pairself fst)
blanchet@46083
   466
      |> (case T of
blanchet@46083
   467
            Type (@{type_name set}, _) =>
blanchet@46083
   468
            sort_wrt (truth_const_sort_key o snd)
blanchet@46083
   469
            #> make_set maybe_opt T'
blanchet@46083
   470
          | _ =>
blanchet@46083
   471
            make_plain_fun maybe_opt T1 T2
blanchet@46083
   472
            #> unarize_unbox_etc_term
blanchet@46083
   473
            #> format_fun (uniterize_unarize_unbox_etc_type T')
blanchet@46083
   474
                          (uniterize_unarize_unbox_etc_type T1)
blanchet@46083
   475
                          (uniterize_unarize_unbox_etc_type T2))
blanchet@35665
   476
    fun term_for_atom seen (T as Type (@{type_name fun}, [T1, T2])) T' j _ =
blanchet@33192
   477
        let
blanchet@33192
   478
          val k1 = card_of_type card_assigns T1
blanchet@33192
   479
          val k2 = card_of_type card_assigns T2
blanchet@33192
   480
        in
blanchet@35665
   481
          term_for_rep true seen T T' (Vect (k1, Atom (k2, 0)))
blanchet@33192
   482
                       [nth_combination (replicate k1 (k2, 0)) j]
blanchet@33192
   483
          handle General.Subscript =>
blanchet@33232
   484
                 raise ARG ("Nitpick_Model.reconstruct_term.term_for_atom",
blanchet@33192
   485
                            signed_string_of_int j ^ " for " ^
blanchet@33192
   486
                            string_for_rep (Vect (k1, Atom (k2, 0))))
blanchet@33192
   487
        end
blanchet@38190
   488
      | term_for_atom seen (Type (@{type_name prod}, [T1, T2])) _ j k =
blanchet@35075
   489
        let
blanchet@35075
   490
          val k1 = card_of_type card_assigns T1
blanchet@35075
   491
          val k2 = k div k1
blanchet@35075
   492
        in
blanchet@33192
   493
          list_comb (HOLogic.pair_const T1 T2,
blanchet@35075
   494
                     map3 (fn T => term_for_atom seen T T) [T1, T2]
blanchet@35075
   495
                          [j div k2, j mod k2] [k1, k2]) (* ### k2 or k1? FIXME *)
blanchet@33192
   496
        end
blanchet@35075
   497
      | term_for_atom seen @{typ prop} _ j k =
blanchet@35075
   498
        HOLogic.mk_Trueprop (term_for_atom seen bool_T bool_T j k)
blanchet@35075
   499
      | term_for_atom _ @{typ bool} _ j _ =
blanchet@33192
   500
        if j = 0 then @{const False} else @{const True}
blanchet@35075
   501
      | term_for_atom seen T _ j k =
blanchet@35220
   502
        if T = nat_T andalso is_standard_datatype thy stds nat_T then
blanchet@33192
   503
          HOLogic.mk_number nat_T j
blanchet@33192
   504
        else if T = int_T then
blanchet@35075
   505
          HOLogic.mk_number int_T (int_for_atom (k, 0) j)
blanchet@33192
   506
        else if is_fp_iterator_type T then
blanchet@35075
   507
          HOLogic.mk_number nat_T (k - j - 1)
blanchet@33192
   508
        else if T = @{typ bisim_iterator} then
blanchet@33192
   509
          HOLogic.mk_number nat_T j
blanchet@33192
   510
        else case datatype_spec datatypes T of
blanchet@37260
   511
          NONE => nth_atom thy atomss pool for_auto T j
blanchet@37260
   512
        | SOME {deep = false, ...} => nth_atom thy atomss pool for_auto T j
blanchet@35179
   513
        | SOME {co, standard, constrs, ...} =>
blanchet@33192
   514
          let
blanchet@33192
   515
            fun tuples_for_const (s, T) =
blanchet@33192
   516
              tuple_list_for_name rel_table bounds (ConstName (s, T, Any))
blanchet@35180
   517
            fun cyclic_atom () =
blanchet@37261
   518
              nth_atom thy atomss pool for_auto (Type (cyclic_type_name (), []))
blanchet@37261
   519
                       j
blanchet@37260
   520
            fun cyclic_var () =
blanchet@37260
   521
              Var ((nth_atom_name thy atomss pool "" T j, 0), T)
blanchet@33192
   522
            val discr_jsss = map (tuples_for_const o discr_for_constr o #const)
blanchet@33192
   523
                                 constrs
blanchet@33192
   524
            val real_j = j + offset_of_type ofs T
blanchet@33192
   525
            val constr_x as (constr_s, constr_T) =
blanchet@33192
   526
              get_first (fn (jss, {const, ...}) =>
blanchet@34121
   527
                            if member (op =) jss [real_j] then SOME const
blanchet@34121
   528
                            else NONE)
blanchet@33192
   529
                        (discr_jsss ~~ constrs) |> the
blanchet@33192
   530
            val arg_Ts = curried_binder_types constr_T
blanchet@35190
   531
            val sel_xs =
blanchet@35190
   532
              map (binarized_and_boxed_nth_sel_for_constr hol_ctxt binarize
blanchet@35190
   533
                                                          constr_x)
blanchet@35190
   534
                  (index_seq 0 (length arg_Ts))
blanchet@33192
   535
            val sel_Rs =
blanchet@33192
   536
              map (fn x => get_first
blanchet@33192
   537
                               (fn ConstName (s', T', R) =>
blanchet@33192
   538
                                   if (s', T') = x then SOME R else NONE
blanchet@33232
   539
                                 | u => raise NUT ("Nitpick_Model.reconstruct_\
blanchet@33192
   540
                                                   \term.term_for_atom", [u]))
blanchet@33192
   541
                               sel_names |> the) sel_xs
blanchet@33192
   542
            val arg_Rs = map (snd o dest_Func) sel_Rs
blanchet@33192
   543
            val sel_jsss = map tuples_for_const sel_xs
blanchet@33192
   544
            val arg_jsss =
blanchet@33192
   545
              map (map_filter (fn js => if hd js = real_j then SOME (tl js)
blanchet@33192
   546
                                        else NONE)) sel_jsss
blanchet@33192
   547
            val uncur_arg_Ts = binder_types constr_T
blanchet@35179
   548
            val maybe_cyclic = co orelse not standard
blanchet@33192
   549
          in
blanchet@35180
   550
            if maybe_cyclic andalso not (null seen) andalso
blanchet@35188
   551
               member (op =) (seen |> unfold ? (fst o split_last)) (T, j) then
blanchet@35180
   552
              cyclic_var ()
blanchet@34124
   553
            else if constr_s = @{const_name Word} then
blanchet@34124
   554
              HOLogic.mk_number
blanchet@34124
   555
                  (if T = @{typ "unsigned_bit word"} then nat_T else int_T)
blanchet@34124
   556
                  (value_of_bits (the_single arg_jsss))
blanchet@33192
   557
            else
blanchet@33192
   558
              let
blanchet@35179
   559
                val seen = seen |> maybe_cyclic ? cons (T, j)
blanchet@33192
   560
                val ts =
blanchet@33192
   561
                  if length arg_Ts = 0 then
blanchet@33192
   562
                    []
blanchet@33192
   563
                  else
blanchet@41052
   564
                    map3 (fn Ts => term_for_rep true seen Ts Ts) arg_Ts arg_Rs
blanchet@41052
   565
                         arg_jsss
blanchet@33192
   566
                    |> mk_tuple (HOLogic.mk_tupleT uncur_arg_Ts)
blanchet@33192
   567
                    |> dest_n_tuple (length uncur_arg_Ts)
blanchet@33192
   568
                val t =
blanchet@33192
   569
                  if constr_s = @{const_name Abs_Frac} then
blanchet@35711
   570
                    case ts of
blanchet@35711
   571
                      [Const (@{const_name Pair}, _) $ t1 $ t2] =>
blanchet@35711
   572
                      frac_from_term_pair (body_type T) t1 t2
blanchet@35711
   573
                    | _ => raise TERM ("Nitpick_Model.reconstruct_term.\
blanchet@35711
   574
                                       \term_for_atom (Abs_Frac)", ts)
blanchet@34936
   575
                  else if not for_auto andalso
blanchet@37256
   576
                          (is_abs_fun ctxt constr_x orelse
blanchet@34936
   577
                           constr_s = @{const_name Quot}) then
blanchet@33192
   578
                    Const (abs_name, constr_T) $ the_single ts
blanchet@33192
   579
                  else
blanchet@33192
   580
                    list_comb (Const constr_x, ts)
blanchet@33192
   581
              in
blanchet@35179
   582
                if maybe_cyclic then
blanchet@35180
   583
                  let val var = cyclic_var () in
blanchet@35188
   584
                    if unfold andalso not standard andalso
blanchet@35180
   585
                       length seen = 1 andalso
blanchet@37261
   586
                       exists_subterm
blanchet@37261
   587
                           (fn Const (s, _) =>
blanchet@37261
   588
                               String.isPrefix (cyclic_const_prefix ()) s
blanchet@37261
   589
                             | t' => t' = var) t then
blanchet@35188
   590
                      subst_atomic [(var, cyclic_atom ())] t
blanchet@35180
   591
                    else if exists_subterm (curry (op =) var) t then
blanchet@35179
   592
                      if co then
blanchet@35179
   593
                        Const (@{const_name The}, (T --> bool_T) --> T)
blanchet@37261
   594
                        $ Abs (cyclic_co_val_name (), T,
haftmann@38864
   595
                               Const (@{const_name HOL.eq}, T --> T --> bool_T)
blanchet@35180
   596
                               $ Bound 0 $ abstract_over (var, t))
blanchet@35179
   597
                      else
blanchet@35180
   598
                        cyclic_atom ()
blanchet@33192
   599
                    else
blanchet@33192
   600
                      t
blanchet@33192
   601
                  end
blanchet@33192
   602
                else
blanchet@33192
   603
                  t
blanchet@33192
   604
              end
blanchet@33192
   605
          end
blanchet@46083
   606
    and term_for_vect seen k R T T' js =
blanchet@46083
   607
      let
blanchet@46083
   608
        val T1 = pseudo_domain_type T
blanchet@46083
   609
        val T2 = pseudo_range_type T
blanchet@46083
   610
      in
blanchet@46083
   611
        make_fun_or_set true T T1 T2 T'
blanchet@46083
   612
            (map (fn j => term_for_atom seen T1 T1 j k) (index_seq 0 k))
blanchet@46083
   613
            (map (term_for_rep true seen T2 T2 R o single)
blanchet@46083
   614
                 (batch_list (arity_of_rep R) js))
blanchet@46083
   615
      end
blanchet@38190
   616
    and term_for_rep _ seen T T' (R as Atom (k, j0)) [[j]] =
blanchet@35075
   617
        if j >= j0 andalso j < j0 + k then term_for_atom seen T T' (j - j0) k
blanchet@33232
   618
        else raise REP ("Nitpick_Model.reconstruct_term.term_for_rep", [R])
blanchet@38190
   619
      | term_for_rep _ seen (Type (@{type_name prod}, [T1, T2])) _
blanchet@35665
   620
                     (Struct [R1, R2]) [js] =
blanchet@33192
   621
        let
blanchet@33192
   622
          val arity1 = arity_of_rep R1
blanchet@33192
   623
          val (js1, js2) = chop arity1 js
blanchet@33192
   624
        in
blanchet@33192
   625
          list_comb (HOLogic.pair_const T1 T2,
blanchet@35665
   626
                     map3 (fn T => term_for_rep true seen T T) [T1, T2] [R1, R2]
blanchet@33192
   627
                          [[js1], [js2]])
blanchet@33192
   628
        end
blanchet@46083
   629
      | term_for_rep _ seen T T' (Vect (k, R')) [js] =
blanchet@46083
   630
        term_for_vect seen k R' T T' js
blanchet@46083
   631
      | term_for_rep maybe_opt seen T T' (Func (R1, Formula Neut)) jss =
blanchet@33192
   632
        let
blanchet@46083
   633
          val T1 = pseudo_domain_type T
blanchet@46083
   634
          val T2 = pseudo_range_type T
blanchet@33192
   635
          val jss1 = all_combinations_for_rep R1
blanchet@35665
   636
          val ts1 = map (term_for_rep true seen T1 T1 R1 o single) jss1
blanchet@33192
   637
          val ts2 =
blanchet@35665
   638
            map (fn js => term_for_rep true seen T2 T2 (Atom (2, 0))
blanchet@35385
   639
                                       [[int_from_bool (member (op =) jss js)]])
blanchet@34121
   640
                jss1
blanchet@46083
   641
        in make_fun_or_set maybe_opt T T1 T2 T' ts1 ts2 end
blanchet@46083
   642
      | term_for_rep maybe_opt seen T T' (Func (R1, R2)) jss =
blanchet@33192
   643
        let
blanchet@46083
   644
          val T1 = pseudo_domain_type T
blanchet@46083
   645
          val T2 = pseudo_range_type T
blanchet@33192
   646
          val arity1 = arity_of_rep R1
blanchet@33192
   647
          val jss1 = all_combinations_for_rep R1
blanchet@35665
   648
          val ts1 = map (term_for_rep false seen T1 T1 R1 o single) jss1
blanchet@33192
   649
          val grouped_jss2 = AList.group (op =) (map (chop arity1) jss)
blanchet@35665
   650
          val ts2 = map (term_for_rep false seen T2 T2 R2 o the_default []
blanchet@33192
   651
                         o AList.lookup (op =) grouped_jss2) jss1
blanchet@46083
   652
        in make_fun_or_set maybe_opt T T1 T2 T' ts1 ts2 end
blanchet@35665
   653
      | term_for_rep _ seen T T' (Opt R) jss =
blanchet@35665
   654
        if null jss then Const (unknown, T)
blanchet@35665
   655
        else term_for_rep true seen T T' R jss
blanchet@35665
   656
      | term_for_rep _ _ T _ R jss =
blanchet@33232
   657
        raise ARG ("Nitpick_Model.reconstruct_term.term_for_rep",
blanchet@37260
   658
                   Syntax.string_of_typ ctxt T ^ " " ^ string_for_rep R ^ " " ^
blanchet@33192
   659
                   string_of_int (length jss))
blanchet@35712
   660
  in
blanchet@35712
   661
    postprocess_subterms [] o polish_funs [] o unarize_unbox_etc_term
blanchet@37262
   662
    oooo term_for_rep maybe_opt []
blanchet@35712
   663
  end
blanchet@33192
   664
blanchet@35718
   665
(** Constant postprocessing **)
blanchet@35718
   666
blanchet@35718
   667
fun dest_n_tuple_type 1 T = [T]
blanchet@35718
   668
  | dest_n_tuple_type n (Type (_, [T1, T2])) =
blanchet@35718
   669
    T1 :: dest_n_tuple_type (n - 1) T2
blanchet@35718
   670
  | dest_n_tuple_type _ T =
blanchet@35718
   671
    raise TYPE ("Nitpick_Model.dest_n_tuple_type", [T], [])
blanchet@35718
   672
blanchet@41791
   673
fun const_format thy def_tables (x as (s, T)) =
blanchet@35718
   674
  if String.isPrefix unrolled_prefix s then
blanchet@41791
   675
    const_format thy def_tables (original_name s, range_type T)
blanchet@35718
   676
  else if String.isPrefix skolem_prefix s then
blanchet@35718
   677
    let
blanchet@35718
   678
      val k = unprefix skolem_prefix s
blanchet@35718
   679
              |> strip_first_name_sep |> fst |> space_explode "@"
blanchet@35718
   680
              |> hd |> Int.fromString |> the
blanchet@35718
   681
    in [k, num_binder_types T - k] end
blanchet@35718
   682
  else if original_name s <> s then
blanchet@35718
   683
    [num_binder_types T]
blanchet@41791
   684
  else case def_of_const thy def_tables x of
blanchet@35718
   685
    SOME t' => if fixpoint_kind_of_rhs t' <> NoFp then
blanchet@35718
   686
                 let val k = length (strip_abs_vars t') in
blanchet@35718
   687
                   [k, num_binder_types T - k]
blanchet@35718
   688
                 end
blanchet@35718
   689
               else
blanchet@35718
   690
                 [num_binder_types T]
blanchet@35718
   691
  | NONE => [num_binder_types T]
blanchet@35718
   692
fun intersect_formats _ [] = []
blanchet@35718
   693
  | intersect_formats [] _ = []
blanchet@35718
   694
  | intersect_formats ks1 ks2 =
blanchet@35718
   695
    let val ((ks1', k1), (ks2', k2)) = pairself split_last (ks1, ks2) in
blanchet@35718
   696
      intersect_formats (ks1' @ (if k1 > k2 then [k1 - k2] else []))
blanchet@35718
   697
                        (ks2' @ (if k2 > k1 then [k2 - k1] else [])) @
blanchet@35718
   698
      [Int.min (k1, k2)]
blanchet@35718
   699
    end
blanchet@35718
   700
blanchet@41791
   701
fun lookup_format thy def_tables formats t =
blanchet@35718
   702
  case AList.lookup (fn (SOME x, SOME y) =>
blanchet@35718
   703
                        (term_match thy) (x, y) | _ => false)
blanchet@35718
   704
                    formats (SOME t) of
blanchet@35718
   705
    SOME format => format
blanchet@35718
   706
  | NONE => let val format = the (AList.lookup (op =) formats NONE) in
blanchet@35718
   707
              case t of
blanchet@35718
   708
                Const x => intersect_formats format
blanchet@41791
   709
                                             (const_format thy def_tables x)
blanchet@35718
   710
              | _ => format
blanchet@35718
   711
            end
blanchet@35718
   712
blanchet@35718
   713
fun format_type default_format format T =
blanchet@35718
   714
  let
blanchet@35718
   715
    val T = uniterize_unarize_unbox_etc_type T
blanchet@35718
   716
    val format = format |> filter (curry (op <) 0)
blanchet@35718
   717
  in
blanchet@35718
   718
    if forall (curry (op =) 1) format then
blanchet@35718
   719
      T
blanchet@35718
   720
    else
blanchet@35718
   721
      let
blanchet@35718
   722
        val (binder_Ts, body_T) = strip_type T
blanchet@35718
   723
        val batched =
blanchet@35718
   724
          binder_Ts
blanchet@35718
   725
          |> map (format_type default_format default_format)
blanchet@35718
   726
          |> rev |> chunk_list_unevenly (rev format)
blanchet@35718
   727
          |> map (HOLogic.mk_tupleT o rev)
blanchet@35718
   728
      in List.foldl (op -->) body_T batched end
blanchet@35718
   729
  end
blanchet@41791
   730
fun format_term_type thy def_tables formats t =
blanchet@35718
   731
  format_type (the (AList.lookup (op =) formats NONE))
blanchet@41791
   732
              (lookup_format thy def_tables formats t) (fastype_of t)
blanchet@35718
   733
blanchet@35718
   734
fun repair_special_format js m format =
blanchet@35718
   735
  m - 1 downto 0 |> chunk_list_unevenly (rev format)
blanchet@35718
   736
                 |> map (rev o filter_out (member (op =) js))
blanchet@35718
   737
                 |> filter_out null |> map length |> rev
blanchet@35718
   738
blanchet@41791
   739
fun user_friendly_const ({thy, evals, def_tables, skolems, special_funs, ...}
blanchet@35718
   740
                         : hol_context) (base_name, step_name) formats =
blanchet@35718
   741
  let
blanchet@35718
   742
    val default_format = the (AList.lookup (op =) formats NONE)
blanchet@35718
   743
    fun do_const (x as (s, T)) =
blanchet@35718
   744
      (if String.isPrefix special_prefix s then
blanchet@35718
   745
         let
blanchet@35718
   746
           val do_term = map_aterms (fn Const x => fst (do_const x) | t' => t')
blanchet@35718
   747
           val (x' as (_, T'), js, ts) =
blanchet@35718
   748
             AList.find (op =) (!special_funs) (s, unarize_unbox_etc_type T)
blanchet@35718
   749
             |> the_single
blanchet@35718
   750
           val max_j = List.last js
blanchet@35718
   751
           val Ts = List.take (binder_types T', max_j + 1)
blanchet@35718
   752
           val missing_js = filter_out (member (op =) js) (0 upto max_j)
blanchet@35718
   753
           val missing_Ts = filter_indices missing_js Ts
blanchet@35718
   754
           fun nth_missing_var n =
blanchet@35718
   755
             ((arg_var_prefix ^ nat_subscript (n + 1), 0), nth missing_Ts n)
blanchet@35718
   756
           val missing_vars = map nth_missing_var (0 upto length missing_js - 1)
blanchet@35718
   757
           val vars = special_bounds ts @ missing_vars
blanchet@35718
   758
           val ts' =
blanchet@35718
   759
             map (fn j =>
blanchet@35718
   760
                     case AList.lookup (op =) (js ~~ ts) j of
blanchet@35718
   761
                       SOME t => do_term t
blanchet@35718
   762
                     | NONE =>
blanchet@35718
   763
                       Var (nth missing_vars
blanchet@35718
   764
                                (find_index (curry (op =) j) missing_js)))
blanchet@35718
   765
                 (0 upto max_j)
blanchet@35718
   766
           val t = do_const x' |> fst
blanchet@35718
   767
           val format =
blanchet@35718
   768
             case AList.lookup (fn (SOME t1, SOME t2) => term_match thy (t1, t2)
blanchet@35718
   769
                                 | _ => false) formats (SOME t) of
blanchet@35718
   770
               SOME format =>
blanchet@35718
   771
               repair_special_format js (num_binder_types T') format
blanchet@35718
   772
             | NONE =>
blanchet@41791
   773
               const_format thy def_tables x'
blanchet@35718
   774
               |> repair_special_format js (num_binder_types T')
blanchet@35718
   775
               |> intersect_formats default_format
blanchet@35718
   776
         in
blanchet@35718
   777
           (list_comb (t, ts') |> fold_rev abs_var vars,
blanchet@35718
   778
            format_type default_format format T)
blanchet@35718
   779
         end
blanchet@35718
   780
       else if String.isPrefix uncurry_prefix s then
blanchet@35718
   781
         let
blanchet@35718
   782
           val (ss, s') = unprefix uncurry_prefix s
blanchet@35718
   783
                          |> strip_first_name_sep |>> space_explode "@"
blanchet@35718
   784
         in
blanchet@35718
   785
           if String.isPrefix step_prefix s' then
blanchet@35718
   786
             do_const (s', T)
blanchet@35718
   787
           else
blanchet@35718
   788
             let
blanchet@35718
   789
               val k = the (Int.fromString (hd ss))
blanchet@35718
   790
               val j = the (Int.fromString (List.last ss))
blanchet@35718
   791
               val (before_Ts, (tuple_T, rest_T)) =
blanchet@35718
   792
                 strip_n_binders j T ||> (strip_n_binders 1 #>> hd)
blanchet@35718
   793
               val T' = before_Ts ---> dest_n_tuple_type k tuple_T ---> rest_T
blanchet@35718
   794
             in do_const (s', T') end
blanchet@35718
   795
         end
blanchet@35718
   796
       else if String.isPrefix unrolled_prefix s then
blanchet@35718
   797
         let val t = Const (original_name s, range_type T) in
blanchet@35718
   798
           (lambda (Free (iter_var_prefix, nat_T)) t,
blanchet@35718
   799
            format_type default_format
blanchet@41791
   800
                        (lookup_format thy def_tables formats t) T)
blanchet@35718
   801
         end
blanchet@35718
   802
       else if String.isPrefix base_prefix s then
blanchet@35718
   803
         (Const (base_name, T --> T) $ Const (unprefix base_prefix s, T),
blanchet@35718
   804
          format_type default_format default_format T)
blanchet@35718
   805
       else if String.isPrefix step_prefix s then
blanchet@35718
   806
         (Const (step_name, T --> T) $ Const (unprefix step_prefix s, T),
blanchet@35718
   807
          format_type default_format default_format T)
blanchet@35718
   808
       else if String.isPrefix quot_normal_prefix s then
blanchet@38207
   809
         let val t = Const (nitpick_prefix ^ "quotient normal form", T) in
blanchet@41791
   810
           (t, format_term_type thy def_tables formats t)
blanchet@35718
   811
         end
blanchet@35718
   812
       else if String.isPrefix skolem_prefix s then
blanchet@35718
   813
         let
blanchet@35718
   814
           val ss = the (AList.lookup (op =) (!skolems) s)
blanchet@35718
   815
           val (Ts, Ts') = chop (length ss) (binder_types T)
blanchet@35718
   816
           val frees = map Free (ss ~~ Ts)
blanchet@35718
   817
           val s' = original_name s
blanchet@35718
   818
         in
blanchet@35718
   819
           (fold lambda frees (Const (s', Ts' ---> T)),
blanchet@35718
   820
            format_type default_format
blanchet@41791
   821
                        (lookup_format thy def_tables formats (Const x)) T)
blanchet@35718
   822
         end
blanchet@35718
   823
       else if String.isPrefix eval_prefix s then
blanchet@35718
   824
         let
blanchet@35718
   825
           val t = nth evals (the (Int.fromString (unprefix eval_prefix s)))
blanchet@41791
   826
         in (t, format_term_type thy def_tables formats t) end
blanchet@35718
   827
       else
blanchet@45479
   828
         (* The selector case can occur in conjunction with fractional types.
blanchet@45479
   829
            It's not pretty. *)
blanchet@45479
   830
         let val t = Const (s |> not (is_sel s) ? original_name, T) in
blanchet@41791
   831
           (t, format_term_type thy def_tables formats t)
blanchet@35718
   832
         end)
blanchet@35718
   833
      |>> map_types uniterize_unarize_unbox_etc_type
blanchet@35718
   834
      |>> shorten_names_in_term |>> Term.map_abs_vars shortest_name
blanchet@35718
   835
  in do_const end
blanchet@35718
   836
blanchet@35718
   837
fun assign_operator_for_const (s, T) =
blanchet@35718
   838
  if String.isPrefix ubfp_prefix s then
blanchet@37261
   839
    if is_fun_type T then xsym "\<subseteq>" "<=" ()
blanchet@37261
   840
    else xsym "\<le>" "<=" ()
blanchet@35718
   841
  else if String.isPrefix lbfp_prefix s then
blanchet@37261
   842
    if is_fun_type T then xsym "\<supseteq>" ">=" ()
blanchet@37261
   843
    else xsym "\<ge>" ">=" ()
blanchet@35718
   844
  else if original_name s <> s then
blanchet@35718
   845
    assign_operator_for_const (strip_first_name_sep s |> snd, T)
blanchet@35718
   846
  else
blanchet@35718
   847
    "="
blanchet@35718
   848
blanchet@35718
   849
(** Model reconstruction **)
blanchet@35718
   850
blanchet@33192
   851
fun unfold_outer_the_binders (t as Const (@{const_name The}, _)
haftmann@38864
   852
                                   $ Abs (s, T, Const (@{const_name HOL.eq}, _)
blanchet@33192
   853
                                                $ Bound 0 $ t')) =
blanchet@33192
   854
    betapply (Abs (s, T, t'), t) |> unfold_outer_the_binders
blanchet@33192
   855
  | unfold_outer_the_binders t = t
blanchet@33192
   856
fun bisimilar_values _ 0 _ = true
blanchet@33192
   857
  | bisimilar_values coTs max_depth (t1, t2) =
blanchet@33192
   858
    let val T = fastype_of t1 in
blanchet@33192
   859
      if exists_subtype (member (op =) coTs) T then
blanchet@33192
   860
        let
blanchet@33192
   861
          val ((head1, args1), (head2, args2)) =
blanchet@33192
   862
            pairself (strip_comb o unfold_outer_the_binders) (t1, t2)
blanchet@34121
   863
          val max_depth = max_depth - (if member (op =) coTs T then 1 else 0)
blanchet@33192
   864
        in
blanchet@34936
   865
          head1 = head2 andalso
blanchet@34936
   866
          forall (bisimilar_values coTs max_depth) (args1 ~~ args2)
blanchet@33192
   867
        end
blanchet@33192
   868
      else
blanchet@33192
   869
        t1 = t2
blanchet@33192
   870
    end
blanchet@33192
   871
blanchet@41993
   872
fun reconstruct_hol_model {show_datatypes, show_skolems, show_consts}
blanchet@35280
   873
        ({hol_ctxt = {thy, ctxt, max_bisim_depth, boxes, stds, wfs, user_axioms,
blanchet@38209
   874
                      debug, whacks, binary_ints, destroy_constrs, specialize,
blanchet@41875
   875
                      star_linear_preds, total_consts, needs, tac_timeout,
blanchet@42415
   876
                      evals, case_names, def_tables, nondef_table, nondefs,
blanchet@41803
   877
                      simp_table, psimp_table, choice_spec_table, intro_table,
blanchet@36388
   878
                      ground_thm_table, ersatz_table, skolems, special_funs,
blanchet@41803
   879
                      unrolled_preds, wf_cache, constr_cache}, binarize,
blanchet@41803
   880
                      card_assigns, bits, bisim_depth, datatypes, ofs} : scope)
blanchet@38170
   881
        formats atomss real_frees pseudo_frees free_names sel_names nonsel_names
blanchet@38170
   882
        rel_table bounds =
blanchet@33192
   883
  let
blanchet@35180
   884
    val pool = Unsynchronized.ref []
blanchet@35280
   885
    val (wacky_names as (_, base_step_names), ctxt) =
blanchet@33192
   886
      add_wacky_syntax ctxt
blanchet@35070
   887
    val hol_ctxt =
blanchet@33192
   888
      {thy = thy, ctxt = ctxt, max_bisim_depth = max_bisim_depth, boxes = boxes,
blanchet@34982
   889
       stds = stds, wfs = wfs, user_axioms = user_axioms, debug = debug,
blanchet@38209
   890
       whacks = whacks, binary_ints = binary_ints,
blanchet@38209
   891
       destroy_constrs = destroy_constrs, specialize = specialize,
blanchet@41871
   892
       star_linear_preds = star_linear_preds, total_consts = total_consts,
blanchet@41875
   893
       needs = needs, tac_timeout = tac_timeout, evals = evals,
blanchet@41871
   894
       case_names = case_names, def_tables = def_tables,
blanchet@42415
   895
       nondef_table = nondef_table, nondefs = nondefs, simp_table = simp_table,
blanchet@42415
   896
       psimp_table = psimp_table, choice_spec_table = choice_spec_table,
blanchet@42415
   897
       intro_table = intro_table, ground_thm_table = ground_thm_table,
blanchet@42415
   898
       ersatz_table = ersatz_table, skolems = skolems,
blanchet@42415
   899
       special_funs = special_funs, unrolled_preds = unrolled_preds,
blanchet@42415
   900
       wf_cache = wf_cache, constr_cache = constr_cache}
blanchet@36388
   901
    val scope =
blanchet@36388
   902
      {hol_ctxt = hol_ctxt, binarize = binarize, card_assigns = card_assigns,
blanchet@36388
   903
       bits = bits, bisim_depth = bisim_depth, datatypes = datatypes, ofs = ofs}
blanchet@37262
   904
    fun term_for_rep maybe_opt unfold =
blanchet@37262
   905
      reconstruct_term maybe_opt unfold pool wacky_names scope atomss
blanchet@37262
   906
                       sel_names rel_table bounds
blanchet@35712
   907
    val all_values =
blanchet@37260
   908
      all_values_of_type pool wacky_names scope atomss sel_names rel_table
blanchet@37260
   909
                         bounds
blanchet@38126
   910
    fun is_codatatype_wellformed (cos : datatype_spec list)
blanchet@38126
   911
                                 ({typ, card, ...} : datatype_spec) =
blanchet@33192
   912
      let
blanchet@35712
   913
        val ts = all_values card typ
blanchet@33192
   914
        val max_depth = Integer.sum (map #card cos)
blanchet@33192
   915
      in
blanchet@33192
   916
        forall (not o bisimilar_values (map #typ cos) max_depth)
blanchet@33192
   917
               (all_distinct_unordered_pairs_of ts)
blanchet@33192
   918
      end
blanchet@33192
   919
    fun pretty_for_assign name =
blanchet@33192
   920
      let
blanchet@33192
   921
        val (oper, (t1, T'), T) =
blanchet@33192
   922
          case name of
blanchet@33192
   923
            FreeName (s, T, _) =>
blanchet@35665
   924
            let val t = Free (s, uniterize_unarize_unbox_etc_type T) in
blanchet@41791
   925
              ("=", (t, format_term_type thy def_tables formats t), T)
blanchet@33192
   926
            end
blanchet@33192
   927
          | ConstName (s, T, _) =>
blanchet@33192
   928
            (assign_operator_for_const (s, T),
blanchet@35280
   929
             user_friendly_const hol_ctxt base_step_names formats (s, T), T)
blanchet@33232
   930
          | _ => raise NUT ("Nitpick_Model.reconstruct_hol_model.\
blanchet@33192
   931
                            \pretty_for_assign", [name])
blanchet@33192
   932
        val t2 = if rep_of name = Any then
blanchet@33192
   933
                   Const (@{const_name undefined}, T')
blanchet@33192
   934
                 else
blanchet@33192
   935
                   tuple_list_for_name rel_table bounds name
blanchet@37262
   936
                   |> term_for_rep (not (is_fully_representable_set name)) false
blanchet@37262
   937
                                   T T' (rep_of name)
blanchet@33192
   938
      in
blanchet@33192
   939
        Pretty.block (Pretty.breaks
wenzelm@39118
   940
            [Syntax.pretty_term (set_show_all_types ctxt) t1,
blanchet@33192
   941
             Pretty.str oper, Syntax.pretty_term ctxt t2])
blanchet@33192
   942
      end
blanchet@38126
   943
    fun pretty_for_datatype ({typ, card, complete, ...} : datatype_spec) =
blanchet@33192
   944
      Pretty.block (Pretty.breaks
blanchet@38189
   945
          (pretty_for_type ctxt typ ::
blanchet@35665
   946
           (case typ of
blanchet@41052
   947
              Type (@{type_name fun_box}, _) => [Pretty.str "[boxed]"]
blanchet@35665
   948
            | Type (@{type_name pair_box}, _) => [Pretty.str "[boxed]"]
blanchet@35665
   949
            | _ => []) @
blanchet@35665
   950
           [Pretty.str "=",
blanchet@35665
   951
            Pretty.enum "," "{" "}"
blanchet@35712
   952
                (map (Syntax.pretty_term ctxt) (all_values card typ) @
blanchet@35665
   953
                 (if fun_from_pair complete false then []
blanchet@37261
   954
                  else [Pretty.str (unrep ())]))]))
blanchet@33192
   955
    fun integer_datatype T =
blanchet@33192
   956
      [{typ = T, card = card_of_type card_assigns T, co = false,
blanchet@38126
   957
        standard = true, self_rec = true, complete = (false, false),
blanchet@38126
   958
        concrete = (true, true), deep = true, constrs = []}]
blanchet@33232
   959
      handle TYPE ("Nitpick_HOL.card_of_type", _, _) => []
blanchet@33192
   960
    val (codatatypes, datatypes) =
blanchet@34982
   961
      datatypes |> filter #deep |> List.partition #co
blanchet@35220
   962
                ||> append (integer_datatype int_T
blanchet@35220
   963
                            |> is_standard_datatype thy stds nat_T
blanchet@35220
   964
                               ? append (integer_datatype nat_T))
blanchet@33192
   965
    val block_of_datatypes =
blanchet@33192
   966
      if show_datatypes andalso not (null datatypes) then
blanchet@33192
   967
        [Pretty.big_list ("Datatype" ^ plural_s_for_list datatypes ^ ":")
blanchet@33192
   968
                         (map pretty_for_datatype datatypes)]
blanchet@33192
   969
      else
blanchet@33192
   970
        []
blanchet@33192
   971
    val block_of_codatatypes =
blanchet@33192
   972
      if show_datatypes andalso not (null codatatypes) then
blanchet@33192
   973
        [Pretty.big_list ("Codatatype" ^ plural_s_for_list codatatypes ^ ":")
blanchet@33192
   974
                         (map pretty_for_datatype codatatypes)]
blanchet@33192
   975
      else
blanchet@33192
   976
        []
blanchet@33192
   977
    fun block_of_names show title names =
blanchet@33192
   978
      if show andalso not (null names) then
blanchet@33192
   979
        Pretty.str (title ^ plural_s_for_list names ^ ":")
blanchet@33192
   980
        :: map (Pretty.indent indent_size o pretty_for_assign)
blanchet@33192
   981
               (sort_wrt (original_name o nickname_of) names)
blanchet@33192
   982
      else
blanchet@33192
   983
        []
blanchet@38170
   984
    fun free_name_for_term keep_all (x as (s, T)) =
blanchet@38170
   985
      case filter (curry (op =) x
blanchet@38170
   986
                   o pairf nickname_of (uniterize_unarize_unbox_etc_type
blanchet@38170
   987
                                        o type_of)) free_names of
blanchet@38170
   988
        [name] => SOME name
blanchet@38170
   989
      | [] => if keep_all then SOME (FreeName (s, T, Any)) else NONE
blanchet@38170
   990
      | _ => raise TERM ("Nitpick_Model.reconstruct_hol_model.\
blanchet@38170
   991
                         \free_name_for_term", [Const x])
blanchet@33192
   992
    val (skolem_names, nonskolem_nonsel_names) =
blanchet@33192
   993
      List.partition is_skolem_name nonsel_names
blanchet@33192
   994
    val (eval_names, noneval_nonskolem_nonsel_names) =
blanchet@33192
   995
      List.partition (String.isPrefix eval_prefix o nickname_of)
blanchet@33192
   996
                     nonskolem_nonsel_names
blanchet@35665
   997
      ||> filter_out (member (op =) [@{const_name bisim},
blanchet@35665
   998
                                     @{const_name bisim_iterator_max}]
blanchet@34121
   999
                      o nickname_of)
blanchet@38170
  1000
      ||> append (map_filter (free_name_for_term false) pseudo_frees)
blanchet@38170
  1001
    val real_free_names = map_filter (free_name_for_term true) real_frees
blanchet@38170
  1002
    val chunks = block_of_names true "Free variable" real_free_names @
blanchet@41993
  1003
                 block_of_names show_skolems "Skolem constant" skolem_names @
blanchet@33192
  1004
                 block_of_names true "Evaluated term" eval_names @
blanchet@33192
  1005
                 block_of_datatypes @ block_of_codatatypes @
blanchet@33192
  1006
                 block_of_names show_consts "Constant"
blanchet@33192
  1007
                                noneval_nonskolem_nonsel_names
blanchet@33192
  1008
  in
blanchet@33192
  1009
    (Pretty.chunks (if null chunks then [Pretty.str "Empty assignment"]
blanchet@33192
  1010
                    else chunks),
blanchet@34936
  1011
     bisim_depth >= 0 orelse
blanchet@34936
  1012
     forall (is_codatatype_wellformed codatatypes) codatatypes)
blanchet@33192
  1013
  end
blanchet@33192
  1014
blanchet@37262
  1015
fun term_for_name pool scope atomss sel_names rel_table bounds name =
blanchet@37262
  1016
  let val T = type_of name in
blanchet@37262
  1017
    tuple_list_for_name rel_table bounds name
blanchet@37262
  1018
    |> reconstruct_term (not (is_fully_representable_set name)) false pool
blanchet@37262
  1019
                        (("", ""), ("", "")) scope atomss sel_names rel_table
blanchet@37262
  1020
                        bounds T T (rep_of name)
blanchet@37262
  1021
  end
blanchet@37262
  1022
blanchet@35280
  1023
fun prove_hol_model (scope as {hol_ctxt = {thy, ctxt, debug, ...},
blanchet@34998
  1024
                               card_assigns, ...})
blanchet@33192
  1025
                    auto_timeout free_names sel_names rel_table bounds prop =
blanchet@33192
  1026
  let
blanchet@35076
  1027
    val pool = Unsynchronized.ref []
blanchet@37260
  1028
    val atomss = [(NONE, [])]
blanchet@33192
  1029
    fun free_type_assm (T, k) =
blanchet@33192
  1030
      let
blanchet@37260
  1031
        fun atom j = nth_atom thy atomss pool true T j
blanchet@33192
  1032
        fun equation_for_atom j = HOLogic.eq_const T $ Bound 0 $ atom j
blanchet@33192
  1033
        val eqs = map equation_for_atom (index_seq 0 k)
blanchet@33192
  1034
        val compreh_assm =
blanchet@33192
  1035
          Const (@{const_name All}, (T --> bool_T) --> bool_T)
blanchet@33192
  1036
              $ Abs ("x", T, foldl1 HOLogic.mk_disj eqs)
blanchet@33192
  1037
        val distinct_assm = distinctness_formula T (map atom (index_seq 0 k))
blanchet@34998
  1038
      in s_conj (compreh_assm, distinct_assm) end
blanchet@33192
  1039
    fun free_name_assm name =
blanchet@33192
  1040
      HOLogic.mk_eq (Free (nickname_of name, type_of name),
blanchet@37260
  1041
                     term_for_name pool scope atomss sel_names rel_table bounds
blanchet@37260
  1042
                                   name)
blanchet@33192
  1043
    val freeT_assms = map free_type_assm (filter (is_TFree o fst) card_assigns)
blanchet@33192
  1044
    val model_assms = map free_name_assm free_names
blanchet@34998
  1045
    val assm = foldr1 s_conj (freeT_assms @ model_assms)
blanchet@33192
  1046
    fun try_out negate =
blanchet@33192
  1047
      let
blanchet@33192
  1048
        val concl = (negate ? curry (op $) @{const Not})
wenzelm@35625
  1049
                    (Object_Logic.atomize_term thy prop)
blanchet@34998
  1050
        val prop = HOLogic.mk_Trueprop (HOLogic.mk_imp (assm, concl))
blanchet@33192
  1051
                   |> map_types (map_type_tfree
blanchet@34998
  1052
                                     (fn (s, []) => TFree (s, HOLogic.typeS)
blanchet@34998
  1053
                                       | x => TFree x))
blanchet@46083
  1054
        val _ =
blanchet@46083
  1055
          if debug then
blanchet@46083
  1056
            (if negate then "Genuineness" else "Spuriousness") ^ " goal: " ^
blanchet@46083
  1057
            Syntax.string_of_term ctxt prop ^ "."
blanchet@46083
  1058
            |> Output.urgent_message
blanchet@46083
  1059
          else
blanchet@46083
  1060
            ()
blanchet@34998
  1061
        val goal = prop |> cterm_of thy |> Goal.init
blanchet@33192
  1062
      in
wenzelm@42793
  1063
        (goal |> SINGLE (DETERM_TIMEOUT auto_timeout (auto_tac ctxt))
blanchet@33192
  1064
              |> the |> Goal.finish ctxt; true)
blanchet@33192
  1065
        handle THM _ => false
blanchet@33192
  1066
             | TimeLimit.TimeOut => false
blanchet@33192
  1067
      end
blanchet@33192
  1068
  in
blanchet@33705
  1069
    if try_out false then SOME true
blanchet@33705
  1070
    else if try_out true then SOME false
blanchet@33192
  1071
    else NONE
blanchet@33192
  1072
  end
blanchet@33192
  1073
blanchet@33192
  1074
end;