src/HOL/Tools/Nitpick/nitpick_model.ML
author blanchet
Mon Dec 14 12:30:26 2009 +0100 (2009-12-14)
changeset 34121 5e831d805118
parent 33982 1ae222745c4a
child 34123 c4988215a691
permissions -rw-r--r--
get rid of polymorphic equality in Nitpick's code + a few minor cleanups
blanchet@33982
     1
(*  Title:      HOL/Tools/Nitpick/nitpick_model.ML
blanchet@33192
     2
    Author:     Jasmin Blanchette, TU Muenchen
blanchet@33192
     3
    Copyright   2009
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@33192
    15
  type params = {
blanchet@33192
    16
    show_skolems: bool,
blanchet@33192
    17
    show_datatypes: bool,
blanchet@33192
    18
    show_consts: bool}
blanchet@33192
    19
blanchet@33192
    20
  structure NameTable : TABLE
blanchet@33192
    21
blanchet@33192
    22
  val tuple_list_for_name :
blanchet@33192
    23
    nut NameTable.table -> Kodkod.raw_bound list -> nut -> int list list
blanchet@33192
    24
  val reconstruct_hol_model :
blanchet@33192
    25
    params -> scope -> (term option * int list) list -> styp list -> nut list
blanchet@33192
    26
    -> nut list -> nut list -> nut NameTable.table -> Kodkod.raw_bound list
blanchet@33192
    27
    -> Pretty.T * bool
blanchet@33192
    28
  val prove_hol_model :
blanchet@33192
    29
    scope -> Time.time option -> nut list -> nut list -> nut NameTable.table
blanchet@33192
    30
    -> Kodkod.raw_bound list -> term -> bool option
blanchet@33192
    31
end;
blanchet@33192
    32
blanchet@33232
    33
structure Nitpick_Model : NITPICK_MODEL =
blanchet@33192
    34
struct
blanchet@33192
    35
blanchet@33232
    36
open Nitpick_Util
blanchet@33232
    37
open Nitpick_HOL
blanchet@33232
    38
open Nitpick_Scope
blanchet@33232
    39
open Nitpick_Peephole
blanchet@33232
    40
open Nitpick_Rep
blanchet@33232
    41
open Nitpick_Nut
blanchet@33192
    42
blanchet@33192
    43
type params = {
blanchet@33192
    44
  show_skolems: bool,
blanchet@33192
    45
  show_datatypes: bool,
blanchet@33192
    46
  show_consts: bool}
blanchet@33192
    47
blanchet@33192
    48
val unknown = "?"
blanchet@33192
    49
val unrep = "\<dots>"
blanchet@33192
    50
val maybe_mixfix = "_\<^sup>?"
blanchet@33192
    51
val base_mixfix = "_\<^bsub>base\<^esub>"
blanchet@33192
    52
val step_mixfix = "_\<^bsub>step\<^esub>"
blanchet@33192
    53
val abs_mixfix = "\<guillemotleft>_\<guillemotright>"
blanchet@33192
    54
val non_opt_name = nitpick_prefix ^ "non_opt"
blanchet@33192
    55
blanchet@33884
    56
(* string -> int -> string *)
blanchet@33884
    57
fun atom_suffix s j =
blanchet@33884
    58
  nat_subscript (j + 1)
blanchet@33884
    59
  |> (s <> "" andalso Symbol.is_ascii_digit (List.last (explode s)))
blanchet@33884
    60
     ? prefix "\<^isub>,"
blanchet@33192
    61
(* string -> typ -> int -> string *)
blanchet@33192
    62
fun atom_name prefix (T as Type (s, _)) j =
blanchet@34121
    63
    prefix ^ substring (shortest_name s, 0, 1) ^ atom_suffix s j
blanchet@33192
    64
  | atom_name prefix (T as TFree (s, _)) j =
blanchet@33884
    65
    prefix ^ perhaps (try (unprefix "'")) s ^ atom_suffix s j
blanchet@33232
    66
  | atom_name _ T _ = raise TYPE ("Nitpick_Model.atom_name", [T], [])
blanchet@33192
    67
(* bool -> typ -> int -> term *)
blanchet@33192
    68
fun atom for_auto T j =
blanchet@33192
    69
  if for_auto then
blanchet@33192
    70
    Free (atom_name (hd (space_explode "." nitpick_prefix)) T j, T)
blanchet@33192
    71
  else
blanchet@33192
    72
    Const (atom_name "" T j, T)
blanchet@33192
    73
blanchet@33192
    74
(* nut NameTable.table -> Kodkod.raw_bound list -> nut -> int list list *)
blanchet@33192
    75
fun tuple_list_for_name rel_table bounds name =
blanchet@33192
    76
  the (AList.lookup (op =) bounds (the_rel rel_table name)) handle NUT _ => [[]]
blanchet@33192
    77
blanchet@33192
    78
(* term -> term *)
blanchet@33192
    79
fun unbox_term (Const (@{const_name FunBox}, _) $ t1) = unbox_term t1
blanchet@33192
    80
  | unbox_term (Const (@{const_name PairBox},
blanchet@33192
    81
                       Type ("fun", [T1, Type ("fun", [T2, T3])])) $ t1 $ t2) =
blanchet@33192
    82
    let val Ts = map unbox_type [T1, T2] in
blanchet@33192
    83
      Const (@{const_name Pair}, Ts ---> Type ("*", Ts))
blanchet@33192
    84
      $ unbox_term t1 $ unbox_term t2
blanchet@33192
    85
    end
blanchet@33192
    86
  | unbox_term (Const (s, T)) = Const (s, unbox_type T)
blanchet@33192
    87
  | unbox_term (t1 $ t2) = unbox_term t1 $ unbox_term t2
blanchet@33192
    88
  | unbox_term (Free (s, T)) = Free (s, unbox_type T)
blanchet@33192
    89
  | unbox_term (Var (x, T)) = Var (x, unbox_type T)
blanchet@33192
    90
  | unbox_term (Bound j) = Bound j
blanchet@33192
    91
  | unbox_term (Abs (s, T, t')) = Abs (s, unbox_type T, unbox_term t')
blanchet@33192
    92
blanchet@33192
    93
(* typ -> typ -> (typ * typ) * (typ * typ) *)
blanchet@33192
    94
fun factor_out_types (T1 as Type ("*", [T11, T12]))
blanchet@33192
    95
                     (T2 as Type ("*", [T21, T22])) =
blanchet@33192
    96
    let val (n1, n2) = pairself num_factors_in_type (T11, T21) in
blanchet@33192
    97
      if n1 = n2 then
blanchet@33192
    98
        let
blanchet@33192
    99
          val ((T11', opt_T12'), (T21', opt_T22')) = factor_out_types T12 T22
blanchet@33192
   100
        in
blanchet@33192
   101
          ((Type ("*", [T11, T11']), opt_T12'),
blanchet@33192
   102
           (Type ("*", [T21, T21']), opt_T22'))
blanchet@33192
   103
        end
blanchet@33192
   104
      else if n1 < n2 then
blanchet@33192
   105
        case factor_out_types T1 T21 of
blanchet@33192
   106
          (p1, (T21', NONE)) => (p1, (T21', SOME T22))
blanchet@33192
   107
        | (p1, (T21', SOME T22')) =>
blanchet@33192
   108
          (p1, (T21', SOME (Type ("*", [T22', T22]))))
blanchet@33192
   109
      else
blanchet@33192
   110
        swap (factor_out_types T2 T1)
blanchet@33192
   111
    end
blanchet@33192
   112
  | factor_out_types (Type ("*", [T11, T12])) T2 = ((T11, SOME T12), (T2, NONE))
blanchet@33192
   113
  | factor_out_types T1 (Type ("*", [T21, T22])) = ((T1, NONE), (T21, SOME T22))
blanchet@33192
   114
  | factor_out_types T1 T2 = ((T1, NONE), (T2, NONE))
blanchet@33192
   115
blanchet@33192
   116
(* bool -> typ -> typ -> (term * term) list -> term *)
blanchet@33192
   117
fun make_plain_fun maybe_opt T1 T2 =
blanchet@33192
   118
  let
blanchet@33192
   119
    (* typ -> typ -> (term * term) list -> term *)
blanchet@33192
   120
    fun aux T1 T2 [] =
blanchet@33192
   121
        Const (if maybe_opt orelse T2 <> bool_T then @{const_name undefined}
blanchet@33192
   122
               else non_opt_name, T1 --> T2)
blanchet@33192
   123
      | aux T1 T2 ((t1, t2) :: ps) =
blanchet@33192
   124
        Const (@{const_name fun_upd}, [T1 --> T2, T1, T2] ---> T1 --> T2)
blanchet@33192
   125
        $ aux T1 T2 ps $ t1 $ t2
blanchet@33192
   126
  in aux T1 T2 o rev end
blanchet@33192
   127
(* term -> bool *)
blanchet@34121
   128
fun is_plain_fun (Const (s, _)) =
blanchet@34121
   129
    (s = @{const_name undefined} orelse s = non_opt_name)
blanchet@33192
   130
  | is_plain_fun (Const (@{const_name fun_upd}, _) $ t0 $ _ $ _) =
blanchet@33192
   131
    is_plain_fun t0
blanchet@33192
   132
  | is_plain_fun _ = false
blanchet@33192
   133
(* term -> bool * (term list * term list) *)
blanchet@33192
   134
val dest_plain_fun =
blanchet@33192
   135
  let
blanchet@33192
   136
    (* term -> term list * term list *)
blanchet@33192
   137
    fun aux (Const (s, _)) = (s <> non_opt_name, ([], []))
blanchet@33192
   138
      | aux (Const (@{const_name fun_upd}, _) $ t0 $ t1 $ t2) =
blanchet@33192
   139
        let val (s, (ts1, ts2)) = aux t0 in (s, (t1 :: ts1, t2 :: ts2)) end
blanchet@33232
   140
      | aux t = raise TERM ("Nitpick_Model.dest_plain_fun", [t])
blanchet@33192
   141
  in apsnd (pairself rev) o aux end
blanchet@33192
   142
blanchet@33565
   143
(* typ -> typ -> typ -> term -> term * term *)
blanchet@33565
   144
fun break_in_two T T1 T2 t =
blanchet@33565
   145
  let
blanchet@33565
   146
    val ps = HOLogic.flat_tupleT_paths T
blanchet@33565
   147
    val cut = length (HOLogic.strip_tupleT T1)
blanchet@33565
   148
    val (ps1, ps2) = pairself HOLogic.flat_tupleT_paths (T1, T2)
blanchet@33565
   149
    val (ts1, ts2) = t |> HOLogic.strip_ptuple ps |> chop cut
blanchet@33565
   150
  in (HOLogic.mk_ptuple ps1 T1 ts1, HOLogic.mk_ptuple ps2 T2 ts2) end
blanchet@33192
   151
(* typ -> term -> term -> term *)
blanchet@33192
   152
fun pair_up (Type ("*", [T1', T2']))
blanchet@33192
   153
            (t1 as Const (@{const_name Pair},
blanchet@33192
   154
                          Type ("fun", [_, Type ("fun", [_, T1])])) $ t11 $ t12)
blanchet@33192
   155
            t2 =
blanchet@33192
   156
    if T1 = T1' then HOLogic.mk_prod (t1, t2)
blanchet@33192
   157
    else HOLogic.mk_prod (t11, pair_up T2' t12 t2)
blanchet@33192
   158
  | pair_up _ t1 t2 = HOLogic.mk_prod (t1, t2)
blanchet@33192
   159
(* typ -> term -> term list * term list -> (term * term) list*)
blanchet@33192
   160
fun multi_pair_up T1 t1 (ts2, ts3) = map2 (pair o pair_up T1 t1) ts2 ts3
blanchet@33192
   161
blanchet@33192
   162
(* typ -> typ -> typ -> term -> term *)
blanchet@33192
   163
fun typecast_fun (Type ("fun", [T1', T2'])) T1 T2 t =
blanchet@33192
   164
    let
blanchet@33565
   165
      (* typ -> typ -> typ -> typ -> term -> term *)
blanchet@33565
   166
      fun do_curry T1 T1a T1b T2 t =
blanchet@33192
   167
        let
blanchet@33192
   168
          val (maybe_opt, ps) = dest_plain_fun t
blanchet@33192
   169
          val ps =
blanchet@33565
   170
            ps |>> map (break_in_two T1 T1a T1b)
blanchet@33192
   171
               |> uncurry (map2 (fn (t1a, t1b) => fn t2 => (t1a, (t1b, t2))))
blanchet@33192
   172
               |> AList.coalesce (op =)
blanchet@33192
   173
               |> map (apsnd (make_plain_fun maybe_opt T1b T2))
blanchet@33192
   174
        in make_plain_fun maybe_opt T1a (T1b --> T2) ps end
blanchet@33192
   175
      (* typ -> typ -> term -> term *)
blanchet@33192
   176
      and do_uncurry T1 T2 t =
blanchet@33192
   177
        let
blanchet@33192
   178
          val (maybe_opt, tsp) = dest_plain_fun t
blanchet@33192
   179
          val ps =
blanchet@33192
   180
            tsp |> op ~~
blanchet@33192
   181
                |> maps (fn (t1, t2) =>
blanchet@33192
   182
                            multi_pair_up T1 t1 (snd (dest_plain_fun t2)))
blanchet@33192
   183
        in make_plain_fun maybe_opt T1 T2 ps end
blanchet@33192
   184
      (* typ -> typ -> typ -> typ -> term -> term *)
blanchet@33192
   185
      and do_arrow T1' T2' _ _ (Const (s, _)) = Const (s, T1' --> T2')
blanchet@33192
   186
        | do_arrow T1' T2' T1 T2
blanchet@33192
   187
                   (Const (@{const_name fun_upd}, _) $ t0 $ t1 $ t2) =
blanchet@33192
   188
          Const (@{const_name fun_upd},
blanchet@33192
   189
                 [T1' --> T2', T1', T2'] ---> T1' --> T2')
blanchet@33192
   190
          $ do_arrow T1' T2' T1 T2 t0 $ do_term T1' T1 t1 $ do_term T2' T2 t2
blanchet@33192
   191
        | do_arrow _ _ _ _ t =
blanchet@33232
   192
          raise TERM ("Nitpick_Model.typecast_fun.do_arrow", [t])
blanchet@33192
   193
      and do_fun T1' T2' T1 T2 t =
blanchet@33192
   194
        case factor_out_types T1' T1 of
blanchet@33192
   195
          ((_, NONE), (_, NONE)) => t |> do_arrow T1' T2' T1 T2
blanchet@33192
   196
        | ((_, NONE), (T1a, SOME T1b)) =>
blanchet@33565
   197
          t |> do_curry T1 T1a T1b T2 |> do_arrow T1' T2' T1a (T1b --> T2)
blanchet@33192
   198
        | ((T1a', SOME T1b'), (_, NONE)) =>
blanchet@33192
   199
          t |> do_arrow T1a' (T1b' --> T2') T1 T2 |> do_uncurry T1' T2'
blanchet@33232
   200
        | _ => raise TYPE ("Nitpick_Model.typecast_fun.do_fun", [T1, T1'], [])
blanchet@33192
   201
      (* typ -> typ -> term -> term *)
blanchet@33192
   202
      and do_term (Type ("fun", [T1', T2'])) (Type ("fun", [T1, T2])) t =
blanchet@33192
   203
          do_fun T1' T2' T1 T2 t
blanchet@33192
   204
        | do_term (T' as Type ("*", Ts' as [T1', T2'])) (Type ("*", [T1, T2]))
blanchet@33192
   205
                  (Const (@{const_name Pair}, _) $ t1 $ t2) =
blanchet@33192
   206
          Const (@{const_name Pair}, Ts' ---> T')
blanchet@33192
   207
          $ do_term T1' T1 t1 $ do_term T2' T2 t2
blanchet@33192
   208
        | do_term T' T t =
blanchet@33192
   209
          if T = T' then t
blanchet@33232
   210
          else raise TYPE ("Nitpick_Model.typecast_fun.do_term", [T, T'], [])
blanchet@33192
   211
    in if T1' = T1 andalso T2' = T2 then t else do_fun T1' T2' T1 T2 t end
blanchet@33232
   212
  | typecast_fun T' _ _ _ = raise TYPE ("Nitpick_Model.typecast_fun", [T'], [])
blanchet@33192
   213
blanchet@33192
   214
(* term -> string *)
blanchet@33192
   215
fun truth_const_sort_key @{const True} = "0"
blanchet@33192
   216
  | truth_const_sort_key @{const False} = "2"
blanchet@33192
   217
  | truth_const_sort_key _ = "1"
blanchet@33192
   218
blanchet@33192
   219
(* typ -> term list -> term *)
blanchet@33192
   220
fun mk_tuple (Type ("*", [T1, T2])) ts =
blanchet@33192
   221
    HOLogic.mk_prod (mk_tuple T1 ts,
blanchet@33192
   222
        mk_tuple T2 (List.drop (ts, length (HOLogic.flatten_tupleT T1))))
blanchet@33192
   223
  | mk_tuple _ (t :: _) = t
blanchet@33192
   224
blanchet@33192
   225
(* string * string * string * string -> scope -> nut list -> nut list
blanchet@33192
   226
   -> nut list -> nut NameTable.table -> Kodkod.raw_bound list -> typ -> typ
blanchet@33192
   227
   -> rep -> int list list -> term *)
blanchet@33192
   228
fun reconstruct_term (maybe_name, base_name, step_name, abs_name)
blanchet@33192
   229
        ({ext_ctxt as {thy, ctxt, ...}, card_assigns, datatypes, ofs, ...}
blanchet@33192
   230
         : scope) sel_names rel_table bounds =
blanchet@33192
   231
  let
blanchet@33192
   232
    val for_auto = (maybe_name = "")
blanchet@33192
   233
    (* bool -> typ -> typ -> (term * term) list -> term *)
blanchet@33192
   234
    fun make_set maybe_opt T1 T2 =
blanchet@33192
   235
      let
blanchet@33192
   236
        val empty_const = Const (@{const_name Set.empty}, T1 --> T2)
blanchet@33192
   237
        val insert_const = Const (@{const_name insert},
blanchet@33192
   238
                                  [T1, T1 --> T2] ---> T1 --> T2)
blanchet@33192
   239
        (* (term * term) list -> term *)
blanchet@33192
   240
        fun aux [] =
blanchet@33192
   241
            if maybe_opt andalso not (is_precise_type datatypes T1) then
blanchet@33192
   242
              insert_const $ Const (unrep, T1) $ empty_const
blanchet@33192
   243
            else
blanchet@33192
   244
              empty_const
blanchet@33192
   245
          | aux ((t1, t2) :: zs) =
blanchet@33192
   246
            aux zs |> t2 <> @{const False}
blanchet@33192
   247
                      ? curry (op $) (insert_const
blanchet@33192
   248
                                      $ (t1 |> t2 <> @{const True}
blanchet@33192
   249
                                               ? curry (op $)
blanchet@33192
   250
                                                       (Const (maybe_name,
blanchet@33192
   251
                                                               T1 --> T1))))
blanchet@33192
   252
      in aux end
blanchet@33192
   253
    (* typ -> typ -> typ -> (term * term) list -> term *)
blanchet@33192
   254
    fun make_map T1 T2 T2' =
blanchet@33192
   255
      let
blanchet@33192
   256
        val update_const = Const (@{const_name fun_upd},
blanchet@33192
   257
                                  [T1 --> T2, T1, T2] ---> T1 --> T2)
blanchet@33192
   258
        (* (term * term) list -> term *)
blanchet@33192
   259
        fun aux' [] = Const (@{const_name Map.empty}, T1 --> T2)
blanchet@33192
   260
          | aux' ((t1, t2) :: ps) =
blanchet@33192
   261
            (case t2 of
blanchet@33192
   262
               Const (@{const_name None}, _) => aux' ps
blanchet@33192
   263
             | _ => update_const $ aux' ps $ t1 $ t2)
blanchet@33192
   264
        fun aux ps =
blanchet@33192
   265
          if not (is_precise_type datatypes T1) then
blanchet@33192
   266
            update_const $ aux' ps $ Const (unrep, T1)
blanchet@33192
   267
            $ (Const (@{const_name Some}, T2' --> T2) $ Const (unknown, T2'))
blanchet@33192
   268
          else
blanchet@33192
   269
            aux' ps
blanchet@33192
   270
      in aux end
blanchet@33192
   271
    (* typ list -> term -> term *)
blanchet@33192
   272
    fun setify_mapify_funs Ts t =
blanchet@33192
   273
      (case fastype_of1 (Ts, t) of
blanchet@33192
   274
         Type ("fun", [T1, T2]) =>
blanchet@33192
   275
         if is_plain_fun t then
blanchet@33192
   276
           case T2 of
blanchet@33192
   277
             @{typ bool} =>
blanchet@33192
   278
             let
blanchet@33192
   279
               val (maybe_opt, ts_pair) =
blanchet@33192
   280
                 dest_plain_fun t ||> pairself (map (setify_mapify_funs Ts))
blanchet@33192
   281
             in
blanchet@33192
   282
               make_set maybe_opt T1 T2
blanchet@33192
   283
                        (sort_wrt (truth_const_sort_key o snd) (op ~~ ts_pair))
blanchet@33192
   284
             end
blanchet@33192
   285
           | Type (@{type_name option}, [T2']) =>
blanchet@33192
   286
             let
blanchet@33192
   287
               val ts_pair = snd (dest_plain_fun t)
blanchet@33192
   288
                             |> pairself (map (setify_mapify_funs Ts))
blanchet@33192
   289
             in make_map T1 T2 T2' (rev (op ~~ ts_pair)) end
blanchet@33192
   290
           | _ => raise SAME ()
blanchet@33192
   291
         else
blanchet@33192
   292
           raise SAME ()
blanchet@33192
   293
       | _ => raise SAME ())
blanchet@33192
   294
      handle SAME () =>
blanchet@33192
   295
             case t of
blanchet@33192
   296
               t1 $ t2 => setify_mapify_funs Ts t1 $ setify_mapify_funs Ts t2
blanchet@33192
   297
             | Abs (s, T, t') => Abs (s, T, setify_mapify_funs (T :: Ts) t')
blanchet@33192
   298
             | _ => t
blanchet@33192
   299
    (* bool -> typ -> typ -> typ -> term list -> term list -> term *)
blanchet@33192
   300
    fun make_fun maybe_opt T1 T2 T' ts1 ts2 =
blanchet@33192
   301
      ts1 ~~ ts2 |> T1 = @{typ bisim_iterator} ? rev
blanchet@33192
   302
                 |> make_plain_fun (maybe_opt andalso not for_auto) T1 T2
blanchet@33192
   303
                 |> unbox_term
blanchet@33192
   304
                 |> typecast_fun (unbox_type T') (unbox_type T1) (unbox_type T2)
blanchet@33192
   305
    (* (typ * int) list -> typ -> typ -> int -> term *)
blanchet@33192
   306
    fun term_for_atom seen (T as Type ("fun", [T1, T2])) T' j =
blanchet@33192
   307
        let
blanchet@33192
   308
          val k1 = card_of_type card_assigns T1
blanchet@33192
   309
          val k2 = card_of_type card_assigns T2
blanchet@33192
   310
        in
blanchet@33192
   311
          term_for_rep seen T T' (Vect (k1, Atom (k2, 0)))
blanchet@33192
   312
                       [nth_combination (replicate k1 (k2, 0)) j]
blanchet@33192
   313
          handle General.Subscript =>
blanchet@33232
   314
                 raise ARG ("Nitpick_Model.reconstruct_term.term_for_atom",
blanchet@33192
   315
                            signed_string_of_int j ^ " for " ^
blanchet@33192
   316
                            string_for_rep (Vect (k1, Atom (k2, 0))))
blanchet@33192
   317
        end
blanchet@33192
   318
      | term_for_atom seen (Type ("*", [T1, T2])) _ j =
blanchet@33192
   319
        let val k1 = card_of_type card_assigns T1 in
blanchet@33192
   320
          list_comb (HOLogic.pair_const T1 T2,
blanchet@33192
   321
                     map2 (fn T => term_for_atom seen T T) [T1, T2]
blanchet@33192
   322
                          [j div k1, j mod k1])
blanchet@33192
   323
        end
blanchet@33192
   324
      | term_for_atom seen @{typ prop} _ j =
blanchet@33192
   325
        HOLogic.mk_Trueprop (term_for_atom seen bool_T bool_T j)
blanchet@33192
   326
      | term_for_atom _ @{typ bool} _ j =
blanchet@33192
   327
        if j = 0 then @{const False} else @{const True}
blanchet@33192
   328
      | term_for_atom _ @{typ unit} _ _ = @{const Unity}
blanchet@33192
   329
      | term_for_atom seen T _ j =
blanchet@33192
   330
        if T = nat_T then
blanchet@33192
   331
          HOLogic.mk_number nat_T j
blanchet@33192
   332
        else if T = int_T then
blanchet@33192
   333
          HOLogic.mk_number int_T
blanchet@33192
   334
              (int_for_atom (card_of_type card_assigns int_T, 0) j)
blanchet@33192
   335
        else if is_fp_iterator_type T then
blanchet@33192
   336
          HOLogic.mk_number nat_T (card_of_type card_assigns T - j - 1)
blanchet@33192
   337
        else if T = @{typ bisim_iterator} then
blanchet@33192
   338
          HOLogic.mk_number nat_T j
blanchet@33192
   339
        else case datatype_spec datatypes T of
blanchet@33192
   340
          NONE => atom for_auto T j
blanchet@33558
   341
        | SOME {shallow = true, ...} => atom for_auto T j
blanchet@33558
   342
        | SOME {co, constrs, ...} =>
blanchet@33192
   343
          let
blanchet@33192
   344
            (* styp -> int list *)
blanchet@33192
   345
            fun tuples_for_const (s, T) =
blanchet@33192
   346
              tuple_list_for_name rel_table bounds (ConstName (s, T, Any))
blanchet@33192
   347
            (* unit -> indexname * typ *)
blanchet@33192
   348
            fun var () = ((atom_name "" T j, 0), T)
blanchet@33192
   349
            val discr_jsss = map (tuples_for_const o discr_for_constr o #const)
blanchet@33192
   350
                                 constrs
blanchet@33192
   351
            val real_j = j + offset_of_type ofs T
blanchet@33192
   352
            val constr_x as (constr_s, constr_T) =
blanchet@33192
   353
              get_first (fn (jss, {const, ...}) =>
blanchet@34121
   354
                            if member (op =) jss [real_j] then SOME const
blanchet@34121
   355
                            else NONE)
blanchet@33192
   356
                        (discr_jsss ~~ constrs) |> the
blanchet@33192
   357
            val arg_Ts = curried_binder_types constr_T
blanchet@33192
   358
            val sel_xs = map (boxed_nth_sel_for_constr ext_ctxt constr_x)
blanchet@33192
   359
                             (index_seq 0 (length arg_Ts))
blanchet@33192
   360
            val sel_Rs =
blanchet@33192
   361
              map (fn x => get_first
blanchet@33192
   362
                               (fn ConstName (s', T', R) =>
blanchet@33192
   363
                                   if (s', T') = x then SOME R else NONE
blanchet@33232
   364
                                 | u => raise NUT ("Nitpick_Model.reconstruct_\
blanchet@33192
   365
                                                   \term.term_for_atom", [u]))
blanchet@33192
   366
                               sel_names |> the) sel_xs
blanchet@33192
   367
            val arg_Rs = map (snd o dest_Func) sel_Rs
blanchet@33192
   368
            val sel_jsss = map tuples_for_const sel_xs
blanchet@33192
   369
            val arg_jsss =
blanchet@33192
   370
              map (map_filter (fn js => if hd js = real_j then SOME (tl js)
blanchet@33192
   371
                                        else NONE)) sel_jsss
blanchet@33192
   372
            val uncur_arg_Ts = binder_types constr_T
blanchet@33192
   373
          in
blanchet@34121
   374
            if co andalso member (op =) seen (T, j) then
blanchet@33192
   375
              Var (var ())
blanchet@33192
   376
            else
blanchet@33192
   377
              let
blanchet@33192
   378
                val seen = seen |> co ? cons (T, j)
blanchet@33192
   379
                val ts =
blanchet@33192
   380
                  if length arg_Ts = 0 then
blanchet@33192
   381
                    []
blanchet@33192
   382
                  else
blanchet@33192
   383
                    map3 (fn Ts => term_for_rep seen Ts Ts) arg_Ts arg_Rs
blanchet@33192
   384
                         arg_jsss
blanchet@33192
   385
                    |> mk_tuple (HOLogic.mk_tupleT uncur_arg_Ts)
blanchet@33192
   386
                    |> dest_n_tuple (length uncur_arg_Ts)
blanchet@33192
   387
                val t =
blanchet@33192
   388
                  if constr_s = @{const_name Abs_Frac} then
blanchet@33192
   389
                    let
blanchet@33192
   390
                      val num_T = body_type T
blanchet@33192
   391
                      (* int -> term *)
blanchet@33192
   392
                      val mk_num = HOLogic.mk_number num_T
blanchet@33192
   393
                    in
blanchet@33192
   394
                      case ts of
blanchet@33192
   395
                        [Const (@{const_name Pair}, _) $ t1 $ t2] =>
blanchet@33192
   396
                        (case snd (HOLogic.dest_number t1) of
blanchet@33192
   397
                           0 => mk_num 0
blanchet@33192
   398
                         | n1 => case HOLogic.dest_number t2 |> snd of
blanchet@33192
   399
                                   1 => mk_num n1
blanchet@33192
   400
                                 | n2 => Const (@{const_name HOL.divide},
blanchet@33192
   401
                                                [num_T, num_T] ---> num_T)
blanchet@33192
   402
                                         $ mk_num n1 $ mk_num n2)
blanchet@33232
   403
                      | _ => raise TERM ("Nitpick_Model.reconstruct_term.term_\
blanchet@33192
   404
                                         \for_atom (Abs_Frac)", ts)
blanchet@33192
   405
                    end
blanchet@33192
   406
                  else if not for_auto andalso is_abs_fun thy constr_x then
blanchet@33192
   407
                    Const (abs_name, constr_T) $ the_single ts
blanchet@33192
   408
                  else
blanchet@33192
   409
                    list_comb (Const constr_x, ts)
blanchet@33192
   410
              in
blanchet@33192
   411
                if co then
blanchet@33192
   412
                  let val var = var () in
blanchet@34121
   413
                    if exists_subterm (curry (op =) (Var var)) t then
blanchet@33192
   414
                      Const (@{const_name The}, (T --> bool_T) --> T)
blanchet@33192
   415
                      $ Abs ("\<omega>", T,
blanchet@33192
   416
                             Const (@{const_name "op ="}, [T, T] ---> bool_T)
blanchet@33192
   417
                             $ Bound 0 $ abstract_over (Var var, t))
blanchet@33192
   418
                    else
blanchet@33192
   419
                      t
blanchet@33192
   420
                  end
blanchet@33192
   421
                else
blanchet@33192
   422
                  t
blanchet@33192
   423
              end
blanchet@33192
   424
          end
blanchet@33192
   425
    (* (typ * int) list -> int -> rep -> typ -> typ -> typ -> int list
blanchet@33192
   426
       -> term *)
blanchet@33192
   427
    and term_for_vect seen k R T1 T2 T' js =
blanchet@33192
   428
      make_fun true T1 T2 T' (map (term_for_atom seen T1 T1) (index_seq 0 k))
blanchet@33192
   429
               (map (term_for_rep seen T2 T2 R o single)
blanchet@33192
   430
                    (batch_list (arity_of_rep R) js))
blanchet@33192
   431
    (* (typ * int) list -> typ -> typ -> rep -> int list list -> term *)
blanchet@33192
   432
    and term_for_rep seen T T' Unit [[]] = term_for_atom seen T T' 0
blanchet@33192
   433
      | term_for_rep seen T T' (R as Atom (k, j0)) [[j]] =
blanchet@33192
   434
        if j >= j0 andalso j < j0 + k then term_for_atom seen T T' (j - j0)
blanchet@33232
   435
        else raise REP ("Nitpick_Model.reconstruct_term.term_for_rep", [R])
blanchet@33192
   436
      | term_for_rep seen (Type ("*", [T1, T2])) _ (Struct [R1, R2]) [js] =
blanchet@33192
   437
        let
blanchet@33192
   438
          val arity1 = arity_of_rep R1
blanchet@33192
   439
          val (js1, js2) = chop arity1 js
blanchet@33192
   440
        in
blanchet@33192
   441
          list_comb (HOLogic.pair_const T1 T2,
blanchet@33192
   442
                     map3 (fn T => term_for_rep seen T T) [T1, T2] [R1, R2]
blanchet@33192
   443
                          [[js1], [js2]])
blanchet@33192
   444
        end
blanchet@33192
   445
      | term_for_rep seen (Type ("fun", [T1, T2])) T' (R as Vect (k, R')) [js] =
blanchet@33192
   446
        term_for_vect seen k R' T1 T2 T' js
blanchet@33192
   447
      | term_for_rep seen (Type ("fun", [T1, T2])) T' (Func (R1, Formula Neut))
blanchet@33192
   448
                     jss =
blanchet@33192
   449
        let
blanchet@33192
   450
          val jss1 = all_combinations_for_rep R1
blanchet@33192
   451
          val ts1 = map (term_for_rep seen T1 T1 R1 o single) jss1
blanchet@33192
   452
          val ts2 =
blanchet@33192
   453
            map (fn js => term_for_rep seen T2 T2 (Atom (2, 0))
blanchet@34121
   454
                                       [[int_for_bool (member (op =) jss js)]])
blanchet@34121
   455
                jss1
blanchet@33192
   456
        in make_fun false T1 T2 T' ts1 ts2 end
blanchet@33192
   457
      | term_for_rep seen (Type ("fun", [T1, T2])) T' (Func (R1, R2)) jss =
blanchet@33192
   458
        let
blanchet@33192
   459
          val arity1 = arity_of_rep R1
blanchet@33192
   460
          val jss1 = all_combinations_for_rep R1
blanchet@33192
   461
          val ts1 = map (term_for_rep seen T1 T1 R1 o single) jss1
blanchet@33192
   462
          val grouped_jss2 = AList.group (op =) (map (chop arity1) jss)
blanchet@33192
   463
          val ts2 = map (term_for_rep seen T2 T2 R2 o the_default []
blanchet@33192
   464
                         o AList.lookup (op =) grouped_jss2) jss1
blanchet@33192
   465
        in make_fun true T1 T2 T' ts1 ts2 end
blanchet@33192
   466
      | term_for_rep seen T T' (Opt R) jss =
blanchet@33192
   467
        if null jss then Const (unknown, T) else term_for_rep seen T T' R jss
blanchet@33192
   468
      | term_for_rep seen T _ R jss =
blanchet@33232
   469
        raise ARG ("Nitpick_Model.reconstruct_term.term_for_rep",
blanchet@33192
   470
                   Refute.string_of_typ T ^ " " ^ string_for_rep R ^ " " ^
blanchet@33192
   471
                   string_of_int (length jss))
blanchet@33192
   472
  in
blanchet@33192
   473
    (not for_auto ? setify_mapify_funs []) o unbox_term oooo term_for_rep []
blanchet@33192
   474
  end
blanchet@33192
   475
blanchet@33192
   476
(* scope -> nut list -> nut NameTable.table -> Kodkod.raw_bound list -> nut
blanchet@33192
   477
   -> term *)
blanchet@33192
   478
fun term_for_name scope sel_names rel_table bounds name =
blanchet@33192
   479
  let val T = type_of name in
blanchet@33192
   480
    tuple_list_for_name rel_table bounds name
blanchet@33192
   481
    |> reconstruct_term ("", "", "", "") scope sel_names rel_table bounds T T
blanchet@33192
   482
                        (rep_of name)
blanchet@33192
   483
  end
blanchet@33192
   484
blanchet@33192
   485
(* Proof.context
blanchet@33192
   486
   -> (string * string * string * string * string) * Proof.context *)
blanchet@33192
   487
fun add_wacky_syntax ctxt =
blanchet@33192
   488
  let
blanchet@33192
   489
    (* term -> string *)
blanchet@33192
   490
    val name_of = fst o dest_Const
blanchet@33192
   491
    val thy = ProofContext.theory_of ctxt |> Context.reject_draft
blanchet@33192
   492
    val (maybe_t, thy) =
blanchet@33202
   493
      Sign.declare_const ((@{binding nitpick_maybe}, @{typ "'a => 'a"}),
blanchet@33202
   494
                          Mixfix (maybe_mixfix, [1000], 1000)) thy
blanchet@33192
   495
    val (base_t, thy) =
blanchet@33202
   496
      Sign.declare_const ((@{binding nitpick_base}, @{typ "'a => 'a"}),
blanchet@33202
   497
                          Mixfix (base_mixfix, [1000], 1000)) thy
blanchet@33192
   498
    val (step_t, thy) =
blanchet@33202
   499
      Sign.declare_const ((@{binding nitpick_step}, @{typ "'a => 'a"}),
blanchet@33202
   500
                          Mixfix (step_mixfix, [1000], 1000)) thy
blanchet@33192
   501
    val (abs_t, thy) =
blanchet@33202
   502
      Sign.declare_const ((@{binding nitpick_abs}, @{typ "'a => 'b"}),
blanchet@33202
   503
                          Mixfix (abs_mixfix, [40], 40)) thy
blanchet@33192
   504
  in
blanchet@33192
   505
    ((name_of maybe_t, name_of base_t, name_of step_t, name_of abs_t),
blanchet@33192
   506
     ProofContext.transfer_syntax thy ctxt)
blanchet@33192
   507
  end
blanchet@33192
   508
blanchet@33192
   509
(* term -> term *)
blanchet@33192
   510
fun unfold_outer_the_binders (t as Const (@{const_name The}, _)
blanchet@33192
   511
                                   $ Abs (s, T, Const (@{const_name "op ="}, _)
blanchet@33192
   512
                                                $ Bound 0 $ t')) =
blanchet@33192
   513
    betapply (Abs (s, T, t'), t) |> unfold_outer_the_binders
blanchet@33192
   514
  | unfold_outer_the_binders t = t
blanchet@33192
   515
(* typ list -> int -> term * term -> bool *)
blanchet@33192
   516
fun bisimilar_values _ 0 _ = true
blanchet@33192
   517
  | bisimilar_values coTs max_depth (t1, t2) =
blanchet@33192
   518
    let val T = fastype_of t1 in
blanchet@33192
   519
      if exists_subtype (member (op =) coTs) T then
blanchet@33192
   520
        let
blanchet@33192
   521
          val ((head1, args1), (head2, args2)) =
blanchet@33192
   522
            pairself (strip_comb o unfold_outer_the_binders) (t1, t2)
blanchet@34121
   523
          val max_depth = max_depth - (if member (op =) coTs T then 1 else 0)
blanchet@33192
   524
        in
blanchet@33192
   525
          head1 = head2
blanchet@33192
   526
          andalso forall (bisimilar_values coTs max_depth) (args1 ~~ args2)
blanchet@33192
   527
        end
blanchet@33192
   528
      else
blanchet@33192
   529
        t1 = t2
blanchet@33192
   530
    end
blanchet@33192
   531
blanchet@33192
   532
(* params -> scope -> (term option * int list) list -> styp list -> nut list
blanchet@33192
   533
  -> nut list -> nut list -> nut NameTable.table -> Kodkod.raw_bound list
blanchet@33192
   534
  -> Pretty.T * bool *)
blanchet@33192
   535
fun reconstruct_hol_model {show_skolems, show_datatypes, show_consts}
blanchet@33192
   536
        ({ext_ctxt as {thy, ctxt, max_bisim_depth, boxes, user_axioms, debug,
blanchet@33192
   537
                       wfs, destroy_constrs, specialize, skolemize,
blanchet@33192
   538
                       star_linear_preds, uncurry, fast_descrs, tac_timeout,
blanchet@33192
   539
                       evals, case_names, def_table, nondef_table, user_nondefs,
blanchet@33192
   540
                       simp_table, psimp_table, intro_table, ground_thm_table,
blanchet@33192
   541
                       ersatz_table, skolems, special_funs, unrolled_preds,
blanchet@33580
   542
                       wf_cache, constr_cache},
blanchet@33192
   543
         card_assigns, bisim_depth, datatypes, ofs} : scope) formats all_frees
blanchet@33192
   544
        free_names sel_names nonsel_names rel_table bounds =
blanchet@33192
   545
  let
blanchet@33192
   546
    val (wacky_names as (_, base_name, step_name, _), ctxt) =
blanchet@33192
   547
      add_wacky_syntax ctxt
blanchet@33192
   548
    val ext_ctxt =
blanchet@33192
   549
      {thy = thy, ctxt = ctxt, max_bisim_depth = max_bisim_depth, boxes = boxes,
blanchet@33192
   550
       wfs = wfs, user_axioms = user_axioms, debug = debug,
blanchet@33192
   551
       destroy_constrs = destroy_constrs, specialize = specialize,
blanchet@33192
   552
       skolemize = skolemize, star_linear_preds = star_linear_preds,
blanchet@33192
   553
       uncurry = uncurry, fast_descrs = fast_descrs, tac_timeout = tac_timeout,
blanchet@33192
   554
       evals = evals, case_names = case_names, def_table = def_table,
blanchet@33192
   555
       nondef_table = nondef_table, user_nondefs = user_nondefs,
blanchet@33192
   556
       simp_table = simp_table, psimp_table = psimp_table,
blanchet@33192
   557
       intro_table = intro_table, ground_thm_table = ground_thm_table,
blanchet@33192
   558
       ersatz_table = ersatz_table, skolems = skolems,
blanchet@33192
   559
       special_funs = special_funs, unrolled_preds = unrolled_preds,
blanchet@33580
   560
       wf_cache = wf_cache, constr_cache = constr_cache}
blanchet@33192
   561
    val scope = {ext_ctxt = ext_ctxt, card_assigns = card_assigns,
blanchet@33192
   562
                 bisim_depth = bisim_depth, datatypes = datatypes, ofs = ofs}
blanchet@33192
   563
    (* typ -> typ -> rep -> int list list -> term *)
blanchet@33192
   564
    val term_for_rep = reconstruct_term wacky_names scope sel_names rel_table
blanchet@33192
   565
                                        bounds
blanchet@33192
   566
    (* typ -> typ -> typ *)
blanchet@33192
   567
    fun nth_value_of_type T card n = term_for_rep T T (Atom (card, 0)) [[n]]
blanchet@33192
   568
    (* dtype_spec list -> dtype_spec -> bool *)
blanchet@33192
   569
    fun is_codatatype_wellformed (cos : dtype_spec list)
blanchet@33192
   570
                                 ({typ, card, ...} : dtype_spec) =
blanchet@33192
   571
      let
blanchet@33192
   572
        val ts = map (nth_value_of_type typ card) (index_seq 0 card)
blanchet@33192
   573
        val max_depth = Integer.sum (map #card cos)
blanchet@33192
   574
      in
blanchet@33192
   575
        forall (not o bisimilar_values (map #typ cos) max_depth)
blanchet@33192
   576
               (all_distinct_unordered_pairs_of ts)
blanchet@33192
   577
      end
blanchet@33192
   578
    (* string -> Pretty.T *)
blanchet@33192
   579
    fun pretty_for_assign name =
blanchet@33192
   580
      let
blanchet@33192
   581
        val (oper, (t1, T'), T) =
blanchet@33192
   582
          case name of
blanchet@33192
   583
            FreeName (s, T, _) =>
blanchet@33192
   584
            let val t = Free (s, unbox_type T) in
blanchet@33192
   585
              ("=", (t, format_term_type thy def_table formats t), T)
blanchet@33192
   586
            end
blanchet@33192
   587
          | ConstName (s, T, _) =>
blanchet@33192
   588
            (assign_operator_for_const (s, T),
blanchet@33192
   589
             user_friendly_const ext_ctxt (base_name, step_name) formats (s, T),
blanchet@33192
   590
             T)
blanchet@33232
   591
          | _ => raise NUT ("Nitpick_Model.reconstruct_hol_model.\
blanchet@33192
   592
                            \pretty_for_assign", [name])
blanchet@33192
   593
        val t2 = if rep_of name = Any then
blanchet@33192
   594
                   Const (@{const_name undefined}, T')
blanchet@33192
   595
                 else
blanchet@33192
   596
                   tuple_list_for_name rel_table bounds name
blanchet@33192
   597
                   |> term_for_rep T T' (rep_of name)
blanchet@33192
   598
      in
blanchet@33192
   599
        Pretty.block (Pretty.breaks
blanchet@33571
   600
            [setmp_show_all_types (Syntax.pretty_term ctxt) t1,
blanchet@33192
   601
             Pretty.str oper, Syntax.pretty_term ctxt t2])
blanchet@33192
   602
      end
blanchet@33192
   603
    (* dtype_spec -> Pretty.T *)
blanchet@33192
   604
    fun pretty_for_datatype ({typ, card, precise, ...} : dtype_spec) =
blanchet@33192
   605
      Pretty.block (Pretty.breaks
blanchet@33192
   606
          [Syntax.pretty_typ ctxt (unbox_type typ), Pretty.str "=",
blanchet@33192
   607
           Pretty.enum "," "{" "}"
blanchet@33192
   608
               (map (Syntax.pretty_term ctxt o nth_value_of_type typ card)
blanchet@33192
   609
                    (index_seq 0 card) @
blanchet@33192
   610
                (if precise then [] else [Pretty.str unrep]))])
blanchet@33192
   611
    (* typ -> dtype_spec list *)
blanchet@33192
   612
    fun integer_datatype T =
blanchet@33192
   613
      [{typ = T, card = card_of_type card_assigns T, co = false,
blanchet@33558
   614
        precise = false, shallow = false, constrs = []}]
blanchet@33232
   615
      handle TYPE ("Nitpick_HOL.card_of_type", _, _) => []
blanchet@33192
   616
    val (codatatypes, datatypes) =
blanchet@33558
   617
      datatypes |> filter_out #shallow
blanchet@33558
   618
                |> List.partition #co
blanchet@33558
   619
                ||> append (integer_datatype nat_T @ integer_datatype int_T)
blanchet@33192
   620
    val block_of_datatypes =
blanchet@33192
   621
      if show_datatypes andalso not (null datatypes) then
blanchet@33192
   622
        [Pretty.big_list ("Datatype" ^ plural_s_for_list datatypes ^ ":")
blanchet@33192
   623
                         (map pretty_for_datatype datatypes)]
blanchet@33192
   624
      else
blanchet@33192
   625
        []
blanchet@33192
   626
    val block_of_codatatypes =
blanchet@33192
   627
      if show_datatypes andalso not (null codatatypes) then
blanchet@33192
   628
        [Pretty.big_list ("Codatatype" ^ plural_s_for_list codatatypes ^ ":")
blanchet@33192
   629
                         (map pretty_for_datatype codatatypes)]
blanchet@33192
   630
      else
blanchet@33192
   631
        []
blanchet@33192
   632
    (* bool -> string -> nut list -> Pretty.T list *)
blanchet@33192
   633
    fun block_of_names show title names =
blanchet@33192
   634
      if show andalso not (null names) then
blanchet@33192
   635
        Pretty.str (title ^ plural_s_for_list names ^ ":")
blanchet@33192
   636
        :: map (Pretty.indent indent_size o pretty_for_assign)
blanchet@33192
   637
               (sort_wrt (original_name o nickname_of) names)
blanchet@33192
   638
      else
blanchet@33192
   639
        []
blanchet@33192
   640
    val (skolem_names, nonskolem_nonsel_names) =
blanchet@33192
   641
      List.partition is_skolem_name nonsel_names
blanchet@33192
   642
    val (eval_names, noneval_nonskolem_nonsel_names) =
blanchet@33192
   643
      List.partition (String.isPrefix eval_prefix o nickname_of)
blanchet@33192
   644
                     nonskolem_nonsel_names
blanchet@34121
   645
      ||> filter_out (curry (op =) @{const_name bisim_iterator_max}
blanchet@34121
   646
                      o nickname_of)
blanchet@33192
   647
    val free_names =
blanchet@33192
   648
      map (fn x as (s, T) =>
blanchet@34121
   649
              case filter (curry (op =) x
blanchet@34121
   650
                           o pairf nickname_of (unbox_type o type_of))
blanchet@33192
   651
                          free_names of
blanchet@33192
   652
                [name] => name
blanchet@33192
   653
              | [] => FreeName (s, T, Any)
blanchet@33232
   654
              | _ => raise TERM ("Nitpick_Model.reconstruct_hol_model",
blanchet@33192
   655
                                 [Const x])) all_frees
blanchet@33192
   656
    val chunks = block_of_names true "Free variable" free_names @
blanchet@33192
   657
                 block_of_names show_skolems "Skolem constant" skolem_names @
blanchet@33192
   658
                 block_of_names true "Evaluated term" eval_names @
blanchet@33192
   659
                 block_of_datatypes @ block_of_codatatypes @
blanchet@33192
   660
                 block_of_names show_consts "Constant"
blanchet@33192
   661
                                noneval_nonskolem_nonsel_names
blanchet@33192
   662
  in
blanchet@33192
   663
    (Pretty.chunks (if null chunks then [Pretty.str "Empty assignment"]
blanchet@33192
   664
                    else chunks),
blanchet@33192
   665
     bisim_depth >= 0
blanchet@33192
   666
     orelse forall (is_codatatype_wellformed codatatypes) codatatypes)
blanchet@33192
   667
  end
blanchet@33192
   668
blanchet@33192
   669
(* scope -> Time.time option -> nut list -> nut list -> nut NameTable.table
blanchet@33192
   670
   -> Kodkod.raw_bound list -> term -> bool option *)
blanchet@33192
   671
fun prove_hol_model (scope as {ext_ctxt as {thy, ctxt, ...}, card_assigns, ...})
blanchet@33192
   672
                    auto_timeout free_names sel_names rel_table bounds prop =
blanchet@33192
   673
  let
blanchet@33192
   674
    (* typ * int -> term *)
blanchet@33192
   675
    fun free_type_assm (T, k) =
blanchet@33192
   676
      let
blanchet@33192
   677
        (* int -> term *)
blanchet@33192
   678
        val atom = atom true T
blanchet@33192
   679
        fun equation_for_atom j = HOLogic.eq_const T $ Bound 0 $ atom j
blanchet@33192
   680
        val eqs = map equation_for_atom (index_seq 0 k)
blanchet@33192
   681
        val compreh_assm =
blanchet@33192
   682
          Const (@{const_name All}, (T --> bool_T) --> bool_T)
blanchet@33192
   683
              $ Abs ("x", T, foldl1 HOLogic.mk_disj eqs)
blanchet@33192
   684
        val distinct_assm = distinctness_formula T (map atom (index_seq 0 k))
blanchet@33192
   685
      in HOLogic.mk_conj (compreh_assm, distinct_assm) end
blanchet@33192
   686
    (* nut -> term *)
blanchet@33192
   687
    fun free_name_assm name =
blanchet@33192
   688
      HOLogic.mk_eq (Free (nickname_of name, type_of name),
blanchet@33192
   689
                     term_for_name scope sel_names rel_table bounds name)
blanchet@33192
   690
    val freeT_assms = map free_type_assm (filter (is_TFree o fst) card_assigns)
blanchet@33192
   691
    val model_assms = map free_name_assm free_names
blanchet@33192
   692
    val assm = List.foldr HOLogic.mk_conj @{const True}
blanchet@33192
   693
                          (freeT_assms @ model_assms)
blanchet@33192
   694
    (* bool -> bool *)
blanchet@33192
   695
    fun try_out negate =
blanchet@33192
   696
      let
blanchet@33192
   697
        val concl = (negate ? curry (op $) @{const Not})
blanchet@33192
   698
                    (ObjectLogic.atomize_term thy prop)
blanchet@33192
   699
        val goal = HOLogic.mk_Trueprop (HOLogic.mk_imp (assm, concl))
blanchet@33192
   700
                   |> map_types (map_type_tfree
blanchet@33192
   701
                          (fn (s, []) => TFree (s, HOLogic.typeS)
blanchet@33192
   702
                            | x => TFree x))
blanchet@33192
   703
                   |> cterm_of thy |> Goal.init
blanchet@33192
   704
      in
blanchet@33192
   705
        (goal |> SINGLE (DETERM_TIMEOUT auto_timeout
blanchet@33192
   706
                                        (auto_tac (clasimpset_of ctxt)))
blanchet@33192
   707
              |> the |> Goal.finish ctxt; true)
blanchet@33192
   708
        handle THM _ => false
blanchet@33192
   709
             | TimeLimit.TimeOut => false
blanchet@33192
   710
      end
blanchet@33192
   711
  in
blanchet@33705
   712
    if try_out false then SOME true
blanchet@33705
   713
    else if try_out true then SOME false
blanchet@33192
   714
    else NONE
blanchet@33192
   715
  end
blanchet@33192
   716
blanchet@33192
   717
end;