src/HOL/Tools/Nitpick/nitpick_mono.ML
author blanchet
Thu Mar 18 14:52:11 2010 +0100 (2010-03-18)
changeset 35832 1dac16f00cd2
parent 35812 394fe2b064cd
child 36385 ff5f88702590
permissions -rw-r--r--
added type constraints to make SML/NJ happy
blanchet@33982
     1
(*  Title:      HOL/Tools/Nitpick/nitpick_mono.ML
blanchet@33192
     2
    Author:     Jasmin Blanchette, TU Muenchen
blanchet@34982
     3
    Copyright   2009, 2010
blanchet@33192
     4
blanchet@35384
     5
Monotonicity inference for higher-order logic.
blanchet@33192
     6
*)
blanchet@33192
     7
blanchet@33192
     8
signature NITPICK_MONO =
blanchet@33192
     9
sig
blanchet@35070
    10
  type hol_context = Nitpick_HOL.hol_context
blanchet@33192
    11
blanchet@33192
    12
  val formulas_monotonic :
blanchet@35386
    13
    hol_context -> bool -> typ -> term list * term list -> bool
blanchet@35665
    14
  val finitize_funs :
blanchet@35665
    15
    hol_context -> bool -> (typ option * bool option) list -> typ
blanchet@35665
    16
    -> term list * term list -> term list * term list
blanchet@33192
    17
end;
blanchet@33192
    18
blanchet@33232
    19
structure Nitpick_Mono : NITPICK_MONO =
blanchet@33192
    20
struct
blanchet@33192
    21
blanchet@33232
    22
open Nitpick_Util
blanchet@33232
    23
open Nitpick_HOL
blanchet@33192
    24
blanchet@33192
    25
type var = int
blanchet@33192
    26
blanchet@34982
    27
datatype sign = Plus | Minus
blanchet@33192
    28
datatype sign_atom = S of sign | V of var
blanchet@33192
    29
blanchet@33192
    30
type literal = var * sign
blanchet@33192
    31
blanchet@35385
    32
datatype mtyp =
blanchet@35385
    33
  MAlpha |
blanchet@35385
    34
  MFun of mtyp * sign_atom * mtyp |
blanchet@35385
    35
  MPair of mtyp * mtyp |
blanchet@35385
    36
  MType of string * mtyp list |
blanchet@35385
    37
  MRec of string * typ list
blanchet@33192
    38
blanchet@35385
    39
datatype mterm =
blanchet@35386
    40
  MRaw of term * mtyp |
blanchet@35385
    41
  MAbs of string * typ * mtyp * sign_atom * mterm |
blanchet@35385
    42
  MApp of mterm * mterm
blanchet@35385
    43
blanchet@35385
    44
type mdata =
blanchet@35070
    45
  {hol_ctxt: hol_context,
blanchet@35190
    46
   binarize: bool,
blanchet@33192
    47
   alpha_T: typ,
blanchet@35665
    48
   no_harmless: bool,
blanchet@33192
    49
   max_fresh: int Unsynchronized.ref,
blanchet@35665
    50
   datatype_mcache: ((string * typ list) * mtyp) list Unsynchronized.ref,
blanchet@35665
    51
   constr_mcache: (styp * mtyp) list Unsynchronized.ref}
blanchet@33192
    52
blanchet@35812
    53
exception UNSOLVABLE of unit
blanchet@35665
    54
exception MTYPE of string * mtyp list * typ list
blanchet@35665
    55
exception MTERM of string * mterm list
blanchet@33192
    56
blanchet@33192
    57
(* string -> unit *)
blanchet@35280
    58
fun print_g (_ : string) = ()
blanchet@35665
    59
(* val print_g = tracing *)
blanchet@33192
    60
blanchet@33192
    61
(* var -> string *)
blanchet@33192
    62
val string_for_var = signed_string_of_int
blanchet@33192
    63
(* string -> var list -> string *)
blanchet@33192
    64
fun string_for_vars sep [] = "0\<^bsub>" ^ sep ^ "\<^esub>"
blanchet@33192
    65
  | string_for_vars sep xs = space_implode sep (map string_for_var xs)
blanchet@33192
    66
fun subscript_string_for_vars sep xs =
blanchet@33192
    67
  if null xs then "" else "\<^bsub>" ^ string_for_vars sep xs ^ "\<^esub>"
blanchet@33192
    68
blanchet@33192
    69
(* sign -> string *)
blanchet@34982
    70
fun string_for_sign Plus = "+"
blanchet@34982
    71
  | string_for_sign Minus = "-"
blanchet@33192
    72
blanchet@33192
    73
(* sign -> sign -> sign *)
blanchet@34982
    74
fun xor sn1 sn2 = if sn1 = sn2 then Plus else Minus
blanchet@33192
    75
(* sign -> sign *)
blanchet@34982
    76
val negate = xor Minus
blanchet@33192
    77
blanchet@33192
    78
(* sign_atom -> string *)
blanchet@33192
    79
fun string_for_sign_atom (S sn) = string_for_sign sn
blanchet@35665
    80
  | string_for_sign_atom (V x) = string_for_var x
blanchet@33192
    81
blanchet@33192
    82
(* literal -> string *)
blanchet@33192
    83
fun string_for_literal (x, sn) = string_for_var x ^ " = " ^ string_for_sign sn
blanchet@33192
    84
blanchet@35385
    85
val bool_M = MType (@{type_name bool}, [])
blanchet@35386
    86
val dummy_M = MType (nitpick_prefix ^ "dummy", [])
blanchet@33192
    87
blanchet@35385
    88
(* mtyp -> bool *)
blanchet@35385
    89
fun is_MRec (MRec _) = true
blanchet@35385
    90
  | is_MRec _ = false
blanchet@35385
    91
(* mtyp -> mtyp * sign_atom * mtyp *)
blanchet@35385
    92
fun dest_MFun (MFun z) = z
blanchet@35665
    93
  | dest_MFun M = raise MTYPE ("Nitpick_Mono.dest_MFun", [M], [])
blanchet@33192
    94
blanchet@33192
    95
val no_prec = 100
blanchet@33192
    96
blanchet@35385
    97
(* mtyp -> int *)
blanchet@35385
    98
fun precedence_of_mtype (MFun _) = 1
blanchet@35385
    99
  | precedence_of_mtype (MPair _) = 2
blanchet@35385
   100
  | precedence_of_mtype _ = no_prec
blanchet@33192
   101
blanchet@35385
   102
(* mtyp -> string *)
blanchet@35385
   103
val string_for_mtype =
blanchet@33192
   104
  let
blanchet@35385
   105
    (* int -> mtyp -> string *)
blanchet@35385
   106
    fun aux outer_prec M =
blanchet@33192
   107
      let
blanchet@35385
   108
        val prec = precedence_of_mtype M
blanchet@33192
   109
        val need_parens = (prec < outer_prec)
blanchet@33192
   110
      in
blanchet@33192
   111
        (if need_parens then "(" else "") ^
blanchet@35386
   112
        (if M = dummy_M then
blanchet@35386
   113
           "_"
blanchet@35386
   114
         else case M of
blanchet@35386
   115
             MAlpha => "\<alpha>"
blanchet@35386
   116
           | MFun (M1, a, M2) =>
blanchet@35386
   117
             aux (prec + 1) M1 ^ " \<Rightarrow>\<^bsup>" ^
blanchet@35386
   118
             string_for_sign_atom a ^ "\<^esup> " ^ aux prec M2
blanchet@35386
   119
           | MPair (M1, M2) => aux (prec + 1) M1 ^ " \<times> " ^ aux prec M2
blanchet@35386
   120
           | MType (s, []) =>
blanchet@35386
   121
             if s = @{type_name prop} orelse s = @{type_name bool} then "o"
blanchet@35386
   122
             else s
blanchet@35386
   123
           | MType (s, Ms) => "(" ^ commas (map (aux 0) Ms) ^ ") " ^ s
blanchet@35386
   124
           | MRec (s, _) => "[" ^ s ^ "]") ^
blanchet@33192
   125
        (if need_parens then ")" else "")
blanchet@33192
   126
      end
blanchet@33192
   127
  in aux 0 end
blanchet@33192
   128
blanchet@35385
   129
(* mtyp -> mtyp list *)
blanchet@35385
   130
fun flatten_mtype (MPair (M1, M2)) = maps flatten_mtype [M1, M2]
blanchet@35385
   131
  | flatten_mtype (MType (_, Ms)) = maps flatten_mtype Ms
blanchet@35385
   132
  | flatten_mtype M = [M]
blanchet@35385
   133
blanchet@35385
   134
(* mterm -> bool *)
blanchet@35386
   135
fun precedence_of_mterm (MRaw _) = no_prec
blanchet@35385
   136
  | precedence_of_mterm (MAbs _) = 1
blanchet@35385
   137
  | precedence_of_mterm (MApp _) = 2
blanchet@33192
   138
blanchet@35385
   139
(* Proof.context -> mterm -> string *)
blanchet@35385
   140
fun string_for_mterm ctxt =
blanchet@35385
   141
  let
blanchet@35385
   142
    (* mtype -> string *)
blanchet@35385
   143
    fun mtype_annotation M = "\<^bsup>" ^ string_for_mtype M ^ "\<^esup>"
blanchet@35385
   144
    (* int -> mterm -> string *)
blanchet@35385
   145
    fun aux outer_prec m =
blanchet@35385
   146
      let
blanchet@35385
   147
        val prec = precedence_of_mterm m
blanchet@35385
   148
        val need_parens = (prec < outer_prec)
blanchet@35385
   149
      in
blanchet@35385
   150
        (if need_parens then "(" else "") ^
blanchet@35385
   151
        (case m of
blanchet@35386
   152
           MRaw (t, M) => Syntax.string_of_term ctxt t ^ mtype_annotation M
blanchet@35385
   153
         | MAbs (s, _, M, a, m) =>
blanchet@35385
   154
           "\<lambda>" ^ s ^ mtype_annotation M ^ ".\<^bsup>" ^
blanchet@35385
   155
           string_for_sign_atom a ^ "\<^esup> " ^ aux prec m
blanchet@35385
   156
         | MApp (m1, m2) => aux prec m1 ^ " " ^ aux (prec + 1) m2) ^
blanchet@35385
   157
        (if need_parens then ")" else "")
blanchet@35385
   158
      end
blanchet@35385
   159
  in aux 0 end
blanchet@35385
   160
blanchet@35385
   161
(* mterm -> mtyp *)
blanchet@35386
   162
fun mtype_of_mterm (MRaw (_, M)) = M
blanchet@35385
   163
  | mtype_of_mterm (MAbs (_, _, M, a, m)) = MFun (M, a, mtype_of_mterm m)
blanchet@35385
   164
  | mtype_of_mterm (MApp (m1, _)) =
blanchet@35385
   165
    case mtype_of_mterm m1 of
blanchet@35385
   166
      MFun (_, _, M12) => M12
blanchet@35665
   167
    | M1 => raise MTYPE ("Nitpick_Mono.mtype_of_mterm", [M1], [])
blanchet@35665
   168
blanchet@35665
   169
(* mterm -> mterm * mterm list *)
blanchet@35665
   170
fun strip_mcomb (MApp (m1, m2)) = strip_mcomb m1 ||> (fn ms => append ms [m2])
blanchet@35665
   171
  | strip_mcomb m = (m, [])
blanchet@35385
   172
blanchet@35665
   173
(* hol_context -> bool -> bool -> typ -> mdata *)
blanchet@35665
   174
fun initial_mdata hol_ctxt binarize no_harmless alpha_T =
blanchet@35190
   175
  ({hol_ctxt = hol_ctxt, binarize = binarize, alpha_T = alpha_T,
blanchet@35665
   176
    no_harmless = no_harmless, max_fresh = Unsynchronized.ref 0,
blanchet@35665
   177
    datatype_mcache = Unsynchronized.ref [],
blanchet@35665
   178
    constr_mcache = Unsynchronized.ref []} : mdata)
blanchet@33192
   179
blanchet@33192
   180
(* typ -> typ -> bool *)
blanchet@33192
   181
fun could_exist_alpha_subtype alpha_T (T as Type (_, Ts)) =
blanchet@34936
   182
    T = alpha_T orelse (not (is_fp_iterator_type T) andalso
blanchet@34936
   183
                        exists (could_exist_alpha_subtype alpha_T) Ts)
blanchet@33192
   184
  | could_exist_alpha_subtype alpha_T T = (T = alpha_T)
blanchet@33192
   185
(* theory -> typ -> typ -> bool *)
blanchet@35385
   186
fun could_exist_alpha_sub_mtype _ (alpha_T as TFree _) T =
blanchet@34121
   187
    could_exist_alpha_subtype alpha_T T
blanchet@35385
   188
  | could_exist_alpha_sub_mtype thy alpha_T T =
blanchet@35220
   189
    (T = alpha_T orelse is_datatype thy [(NONE, true)] T)
blanchet@33192
   190
blanchet@35385
   191
(* mtyp -> bool *)
blanchet@35385
   192
fun exists_alpha_sub_mtype MAlpha = true
blanchet@35385
   193
  | exists_alpha_sub_mtype (MFun (M1, _, M2)) =
blanchet@35385
   194
    exists exists_alpha_sub_mtype [M1, M2]
blanchet@35385
   195
  | exists_alpha_sub_mtype (MPair (M1, M2)) =
blanchet@35385
   196
    exists exists_alpha_sub_mtype [M1, M2]
blanchet@35385
   197
  | exists_alpha_sub_mtype (MType (_, Ms)) = exists exists_alpha_sub_mtype Ms
blanchet@35385
   198
  | exists_alpha_sub_mtype (MRec _) = true
blanchet@33192
   199
blanchet@35385
   200
(* mtyp -> bool *)
blanchet@35385
   201
fun exists_alpha_sub_mtype_fresh MAlpha = true
blanchet@35385
   202
  | exists_alpha_sub_mtype_fresh (MFun (_, V _, _)) = true
blanchet@35385
   203
  | exists_alpha_sub_mtype_fresh (MFun (_, _, M2)) =
blanchet@35385
   204
    exists_alpha_sub_mtype_fresh M2
blanchet@35385
   205
  | exists_alpha_sub_mtype_fresh (MPair (M1, M2)) =
blanchet@35385
   206
    exists exists_alpha_sub_mtype_fresh [M1, M2]
blanchet@35385
   207
  | exists_alpha_sub_mtype_fresh (MType (_, Ms)) =
blanchet@35385
   208
    exists exists_alpha_sub_mtype_fresh Ms
blanchet@35385
   209
  | exists_alpha_sub_mtype_fresh (MRec _) = true
blanchet@33192
   210
blanchet@35385
   211
(* string * typ list -> mtyp list -> mtyp *)
blanchet@35385
   212
fun constr_mtype_for_binders z Ms =
blanchet@35385
   213
  fold_rev (fn M => curry3 MFun M (S Minus)) Ms (MRec z)
blanchet@33192
   214
blanchet@35385
   215
(* ((string * typ list) * mtyp) list -> mtyp list -> mtyp -> mtyp *)
blanchet@35385
   216
fun repair_mtype _ _ MAlpha = MAlpha
blanchet@35385
   217
  | repair_mtype cache seen (MFun (M1, a, M2)) =
blanchet@35385
   218
    MFun (repair_mtype cache seen M1, a, repair_mtype cache seen M2)
blanchet@35385
   219
  | repair_mtype cache seen (MPair Mp) =
blanchet@35385
   220
    MPair (pairself (repair_mtype cache seen) Mp)
blanchet@35385
   221
  | repair_mtype cache seen (MType (s, Ms)) =
blanchet@35385
   222
    MType (s, maps (flatten_mtype o repair_mtype cache seen) Ms)
blanchet@35385
   223
  | repair_mtype cache seen (MRec (z as (s, _))) =
blanchet@33192
   224
    case AList.lookup (op =) cache z |> the of
blanchet@35385
   225
      MRec _ => MType (s, [])
blanchet@35385
   226
    | M => if member (op =) seen M then MType (s, [])
blanchet@35385
   227
           else repair_mtype cache (M :: seen) M
blanchet@33192
   228
blanchet@35385
   229
(* ((string * typ list) * mtyp) list Unsynchronized.ref -> unit *)
blanchet@35665
   230
fun repair_datatype_mcache cache =
blanchet@33192
   231
  let
blanchet@35385
   232
    (* (string * typ list) * mtyp -> unit *)
blanchet@35385
   233
    fun repair_one (z, M) =
blanchet@33192
   234
      Unsynchronized.change cache
blanchet@35385
   235
          (AList.update (op =) (z, repair_mtype (!cache) [] M))
blanchet@33192
   236
  in List.app repair_one (rev (!cache)) end
blanchet@33192
   237
blanchet@35385
   238
(* (typ * mtyp) list -> (styp * mtyp) list Unsynchronized.ref -> unit *)
blanchet@35665
   239
fun repair_constr_mcache dtype_cache constr_mcache =
blanchet@33192
   240
  let
blanchet@35385
   241
    (* styp * mtyp -> unit *)
blanchet@35385
   242
    fun repair_one (x, M) =
blanchet@35665
   243
      Unsynchronized.change constr_mcache
blanchet@35385
   244
          (AList.update (op =) (x, repair_mtype dtype_cache [] M))
blanchet@35665
   245
  in List.app repair_one (!constr_mcache) end
blanchet@35665
   246
blanchet@35665
   247
(* typ -> bool *)
blanchet@35665
   248
fun is_fin_fun_supported_type @{typ prop} = true
blanchet@35665
   249
  | is_fin_fun_supported_type @{typ bool} = true
blanchet@35665
   250
  | is_fin_fun_supported_type (Type (@{type_name option}, _)) = true
blanchet@35665
   251
  | is_fin_fun_supported_type _ = false
blanchet@35665
   252
(* typ -> typ -> term -> term option *)
blanchet@35665
   253
fun fin_fun_body _ _ (t as @{term False}) = SOME t
blanchet@35665
   254
  | fin_fun_body _ _ (t as Const (@{const_name None}, _)) = SOME t
blanchet@35665
   255
  | fin_fun_body dom_T ran_T
blanchet@35665
   256
                 ((t0 as Const (@{const_name If}, _))
blanchet@35665
   257
                  $ (t1 as Const (@{const_name "op ="}, _) $ Bound 0 $ t1')
blanchet@35665
   258
                  $ t2 $ t3) =
blanchet@35665
   259
    (if loose_bvar1 (t1', 0) then
blanchet@35665
   260
       NONE
blanchet@35665
   261
     else case fin_fun_body dom_T ran_T t3 of
blanchet@35665
   262
       NONE => NONE
blanchet@35665
   263
     | SOME t3 =>
blanchet@35665
   264
       SOME (t0 $ (Const (@{const_name is_unknown}, dom_T --> bool_T) $ t1')
blanchet@35665
   265
                $ (Const (@{const_name unknown}, ran_T)) $ (t0 $ t1 $ t2 $ t3)))
blanchet@35665
   266
  | fin_fun_body _ _ _ = NONE
blanchet@33192
   267
blanchet@35672
   268
(* mdata -> bool -> typ -> typ -> mtyp * sign_atom * mtyp *)
blanchet@35672
   269
fun fresh_mfun_for_fun_type (mdata as {max_fresh, ...} : mdata) all_minus
blanchet@35672
   270
                            T1 T2 =
blanchet@33192
   271
  let
blanchet@35672
   272
    val M1 = fresh_mtype_for_type mdata all_minus T1
blanchet@35672
   273
    val M2 = fresh_mtype_for_type mdata all_minus T2
blanchet@35672
   274
    val a = if not all_minus andalso exists_alpha_sub_mtype_fresh M1 andalso
blanchet@35672
   275
               is_fin_fun_supported_type (body_type T2) then
blanchet@35385
   276
              V (Unsynchronized.inc max_fresh)
blanchet@35385
   277
            else
blanchet@35385
   278
              S Minus
blanchet@35385
   279
  in (M1, a, M2) end
blanchet@35672
   280
(* mdata -> bool -> typ -> mtyp *)
blanchet@35385
   281
and fresh_mtype_for_type (mdata as {hol_ctxt as {thy, ...}, binarize, alpha_T,
blanchet@35672
   282
                                    datatype_mcache, constr_mcache, ...})
blanchet@35672
   283
                         all_minus =
blanchet@35385
   284
  let
blanchet@35385
   285
    (* typ -> mtyp *)
blanchet@35385
   286
    fun do_type T =
blanchet@33192
   287
      if T = alpha_T then
blanchet@35385
   288
        MAlpha
blanchet@33192
   289
      else case T of
blanchet@35665
   290
        Type (@{type_name fun}, [T1, T2]) =>
blanchet@35672
   291
        MFun (fresh_mfun_for_fun_type mdata false T1 T2)
blanchet@35665
   292
      | Type (@{type_name "*"}, [T1, T2]) => MPair (pairself do_type (T1, T2))
blanchet@33192
   293
      | Type (z as (s, _)) =>
blanchet@35385
   294
        if could_exist_alpha_sub_mtype thy alpha_T T then
blanchet@35665
   295
          case AList.lookup (op =) (!datatype_mcache) z of
blanchet@35385
   296
            SOME M => M
blanchet@33192
   297
          | NONE =>
blanchet@33192
   298
            let
blanchet@35665
   299
              val _ = Unsynchronized.change datatype_mcache (cons (z, MRec z))
blanchet@35219
   300
              val xs = binarized_and_boxed_datatype_constrs hol_ctxt binarize T
blanchet@35385
   301
              val (all_Ms, constr_Ms) =
blanchet@35385
   302
                fold_rev (fn (_, T') => fn (all_Ms, constr_Ms) =>
blanchet@33192
   303
                             let
blanchet@35385
   304
                               val binder_Ms = map do_type (binder_types T')
blanchet@35385
   305
                               val new_Ms = filter exists_alpha_sub_mtype_fresh
blanchet@35385
   306
                                                   binder_Ms
blanchet@35385
   307
                               val constr_M = constr_mtype_for_binders z
blanchet@35385
   308
                                                                       binder_Ms
blanchet@33192
   309
                             in
blanchet@35385
   310
                               (union (op =) new_Ms all_Ms,
blanchet@35385
   311
                                constr_M :: constr_Ms)
blanchet@33192
   312
                             end)
blanchet@33192
   313
                         xs ([], [])
blanchet@35385
   314
              val M = MType (s, all_Ms)
blanchet@35665
   315
              val _ = Unsynchronized.change datatype_mcache
blanchet@35385
   316
                          (AList.update (op =) (z, M))
blanchet@35665
   317
              val _ = Unsynchronized.change constr_mcache
blanchet@35385
   318
                          (append (xs ~~ constr_Ms))
blanchet@33192
   319
            in
blanchet@35665
   320
              if forall (not o is_MRec o snd) (!datatype_mcache) then
blanchet@35665
   321
                (repair_datatype_mcache datatype_mcache;
blanchet@35665
   322
                 repair_constr_mcache (!datatype_mcache) constr_mcache;
blanchet@35665
   323
                 AList.lookup (op =) (!datatype_mcache) z |> the)
blanchet@33192
   324
              else
blanchet@35385
   325
                M
blanchet@33192
   326
            end
blanchet@33192
   327
        else
blanchet@35385
   328
          MType (s, [])
blanchet@35385
   329
      | _ => MType (Refute.string_of_typ T, [])
blanchet@33192
   330
  in do_type end
blanchet@33192
   331
blanchet@35385
   332
(* mtyp -> mtyp list *)
blanchet@35385
   333
fun prodM_factors (MPair (M1, M2)) = maps prodM_factors [M1, M2]
blanchet@35385
   334
  | prodM_factors M = [M]
blanchet@35385
   335
(* mtyp -> mtyp list * mtyp *)
blanchet@35665
   336
fun curried_strip_mtype (MFun (M1, _, M2)) =
blanchet@35385
   337
    curried_strip_mtype M2 |>> append (prodM_factors M1)
blanchet@35385
   338
  | curried_strip_mtype M = ([], M)
blanchet@35385
   339
(* string -> mtyp -> mtyp *)
blanchet@35385
   340
fun sel_mtype_from_constr_mtype s M =
blanchet@35385
   341
  let val (arg_Ms, dataM) = curried_strip_mtype M in
blanchet@35385
   342
    MFun (dataM, S Minus,
blanchet@35385
   343
          case sel_no_from_name s of ~1 => bool_M | n => nth arg_Ms n)
blanchet@33192
   344
  end
blanchet@33192
   345
blanchet@35385
   346
(* mdata -> styp -> mtyp *)
blanchet@35665
   347
fun mtype_for_constr (mdata as {hol_ctxt = {thy, ...}, alpha_T, constr_mcache,
blanchet@33192
   348
                                ...}) (x as (_, T)) =
blanchet@35385
   349
  if could_exist_alpha_sub_mtype thy alpha_T T then
blanchet@35665
   350
    case AList.lookup (op =) (!constr_mcache) x of
blanchet@35385
   351
      SOME M => M
blanchet@35384
   352
    | NONE => if T = alpha_T then
blanchet@35672
   353
                let val M = fresh_mtype_for_type mdata false T in
blanchet@35665
   354
                  (Unsynchronized.change constr_mcache (cons (x, M)); M)
blanchet@35384
   355
                end
blanchet@35384
   356
              else
blanchet@35672
   357
                (fresh_mtype_for_type mdata false (body_type T);
blanchet@35665
   358
                 AList.lookup (op =) (!constr_mcache) x |> the)
blanchet@33192
   359
  else
blanchet@35672
   360
    fresh_mtype_for_type mdata false T
blanchet@35385
   361
fun mtype_for_sel (mdata as {hol_ctxt, binarize, ...}) (x as (s, _)) =
blanchet@35190
   362
  x |> binarized_and_boxed_constr_for_sel hol_ctxt binarize
blanchet@35385
   363
    |> mtype_for_constr mdata |> sel_mtype_from_constr_mtype s
blanchet@33192
   364
blanchet@35665
   365
(* literal list -> sign_atom -> sign_atom *)
blanchet@35665
   366
fun resolve_sign_atom lits (V x) =
blanchet@35665
   367
    x |> AList.lookup (op =) lits |> Option.map S |> the_default (V x)
blanchet@35665
   368
  | resolve_sign_atom _ a = a
blanchet@35385
   369
(* literal list -> mtyp -> mtyp *)
blanchet@35665
   370
fun resolve_mtype lits =
blanchet@33192
   371
  let
blanchet@35385
   372
    (* mtyp -> mtyp *)
blanchet@35385
   373
    fun aux MAlpha = MAlpha
blanchet@35665
   374
      | aux (MFun (M1, a, M2)) = MFun (aux M1, resolve_sign_atom lits a, aux M2)
blanchet@35385
   375
      | aux (MPair Mp) = MPair (pairself aux Mp)
blanchet@35385
   376
      | aux (MType (s, Ms)) = MType (s, map aux Ms)
blanchet@35385
   377
      | aux (MRec z) = MRec z
blanchet@33192
   378
  in aux end
blanchet@33192
   379
blanchet@33192
   380
datatype comp_op = Eq | Leq
blanchet@33192
   381
blanchet@33192
   382
type comp = sign_atom * sign_atom * comp_op * var list
blanchet@33192
   383
type sign_expr = literal list
blanchet@33192
   384
blanchet@35812
   385
type constraint_set = literal list * comp list * sign_expr list
blanchet@33192
   386
blanchet@33192
   387
(* comp_op -> string *)
blanchet@33192
   388
fun string_for_comp_op Eq = "="
blanchet@33192
   389
  | string_for_comp_op Leq = "\<le>"
blanchet@33192
   390
blanchet@33192
   391
(* sign_expr -> string *)
blanchet@33192
   392
fun string_for_sign_expr [] = "\<bot>"
blanchet@33192
   393
  | string_for_sign_expr lits =
blanchet@33192
   394
    space_implode " \<or> " (map string_for_literal lits)
blanchet@33192
   395
blanchet@33192
   396
(* literal -> literal list option -> literal list option *)
blanchet@33192
   397
fun do_literal _ NONE = NONE
blanchet@33192
   398
  | do_literal (x, sn) (SOME lits) =
blanchet@33192
   399
    case AList.lookup (op =) lits x of
blanchet@33192
   400
      SOME sn' => if sn = sn' then SOME lits else NONE
blanchet@33192
   401
    | NONE => SOME ((x, sn) :: lits)
blanchet@33192
   402
blanchet@33192
   403
(* comp_op -> var list -> sign_atom -> sign_atom -> literal list * comp list
blanchet@33192
   404
   -> (literal list * comp list) option *)
blanchet@33192
   405
fun do_sign_atom_comp Eq [] a1 a2 (accum as (lits, comps)) =
blanchet@33192
   406
    (case (a1, a2) of
blanchet@33192
   407
       (S sn1, S sn2) => if sn1 = sn2 then SOME accum else NONE
blanchet@33192
   408
     | (V x1, S sn2) =>
blanchet@33192
   409
       Option.map (rpair comps) (do_literal (x1, sn2) (SOME lits))
blanchet@33192
   410
     | (V _, V _) => SOME (lits, insert (op =) (a1, a2, Eq, []) comps)
blanchet@33192
   411
     | _ => do_sign_atom_comp Eq [] a2 a1 accum)
blanchet@33192
   412
  | do_sign_atom_comp Leq [] a1 a2 (accum as (lits, comps)) =
blanchet@33192
   413
    (case (a1, a2) of
blanchet@34982
   414
       (_, S Minus) => SOME accum
blanchet@34982
   415
     | (S Plus, _) => SOME accum
blanchet@34982
   416
     | (S Minus, S Plus) => NONE
blanchet@33192
   417
     | (V _, V _) => SOME (lits, insert (op =) (a1, a2, Leq, []) comps)
blanchet@33192
   418
     | _ => do_sign_atom_comp Eq [] a1 a2 accum)
blanchet@35280
   419
  | do_sign_atom_comp cmp xs a1 a2 (lits, comps) =
blanchet@33192
   420
    SOME (lits, insert (op =) (a1, a2, cmp, xs) comps)
blanchet@33192
   421
blanchet@35385
   422
(* comp -> var list -> mtyp -> mtyp -> (literal list * comp list) option
blanchet@33192
   423
   -> (literal list * comp list) option *)
blanchet@35385
   424
fun do_mtype_comp _ _ _ _ NONE = NONE
blanchet@35385
   425
  | do_mtype_comp _ _ MAlpha MAlpha accum = accum
blanchet@35385
   426
  | do_mtype_comp Eq xs (MFun (M11, a1, M12)) (MFun (M21, a2, M22))
blanchet@33192
   427
                  (SOME accum) =
blanchet@35385
   428
     accum |> do_sign_atom_comp Eq xs a1 a2 |> do_mtype_comp Eq xs M11 M21
blanchet@35385
   429
           |> do_mtype_comp Eq xs M12 M22
blanchet@35385
   430
  | do_mtype_comp Leq xs (MFun (M11, a1, M12)) (MFun (M21, a2, M22))
blanchet@33192
   431
                  (SOME accum) =
blanchet@35385
   432
    (if exists_alpha_sub_mtype M11 then
blanchet@33192
   433
       accum |> do_sign_atom_comp Leq xs a1 a2
blanchet@35385
   434
             |> do_mtype_comp Leq xs M21 M11
blanchet@33192
   435
             |> (case a2 of
blanchet@34982
   436
                   S Minus => I
blanchet@35385
   437
                 | S Plus => do_mtype_comp Leq xs M11 M21
blanchet@35385
   438
                 | V x => do_mtype_comp Leq (x :: xs) M11 M21)
blanchet@33192
   439
     else
blanchet@33192
   440
       SOME accum)
blanchet@35385
   441
    |> do_mtype_comp Leq xs M12 M22
blanchet@35385
   442
  | do_mtype_comp cmp xs (M1 as MPair (M11, M12)) (M2 as MPair (M21, M22))
blanchet@33192
   443
                  accum =
blanchet@35385
   444
    (accum |> fold (uncurry (do_mtype_comp cmp xs)) [(M11, M21), (M12, M22)]
blanchet@33192
   445
     handle Library.UnequalLengths =>
blanchet@35665
   446
            raise MTYPE ("Nitpick_Mono.do_mtype_comp", [M1, M2], []))
blanchet@35385
   447
  | do_mtype_comp _ _ (MType _) (MType _) accum =
blanchet@33192
   448
    accum (* no need to compare them thanks to the cache *)
blanchet@35665
   449
  | do_mtype_comp cmp _ M1 M2 _ =
blanchet@35665
   450
    raise MTYPE ("Nitpick_Mono.do_mtype_comp (" ^ string_for_comp_op cmp ^ ")",
blanchet@35665
   451
                 [M1, M2], [])
blanchet@33192
   452
blanchet@35385
   453
(* comp_op -> mtyp -> mtyp -> constraint_set -> constraint_set *)
blanchet@35832
   454
fun add_mtype_comp cmp M1 M2 ((lits, comps, sexps) : constraint_set) =
blanchet@35385
   455
    (print_g ("*** Add " ^ string_for_mtype M1 ^ " " ^ string_for_comp_op cmp ^
blanchet@35385
   456
              " " ^ string_for_mtype M2);
blanchet@35385
   457
     case do_mtype_comp cmp [] M1 M2 (SOME (lits, comps)) of
blanchet@35812
   458
       NONE => (print_g "**** Unsolvable"; raise UNSOLVABLE ())
blanchet@35812
   459
     | SOME (lits, comps) => (lits, comps, sexps))
blanchet@33192
   460
blanchet@35385
   461
(* mtyp -> mtyp -> constraint_set -> constraint_set *)
blanchet@35385
   462
val add_mtypes_equal = add_mtype_comp Eq
blanchet@35385
   463
val add_is_sub_mtype = add_mtype_comp Leq
blanchet@33192
   464
blanchet@35385
   465
(* sign -> sign_expr -> mtyp -> (literal list * sign_expr list) option
blanchet@33192
   466
   -> (literal list * sign_expr list) option *)
blanchet@35385
   467
fun do_notin_mtype_fv _ _ _ NONE = NONE
blanchet@35385
   468
  | do_notin_mtype_fv Minus _ MAlpha accum = accum
blanchet@35385
   469
  | do_notin_mtype_fv Plus [] MAlpha _ = NONE
blanchet@35385
   470
  | do_notin_mtype_fv Plus [(x, sn)] MAlpha (SOME (lits, sexps)) =
blanchet@33192
   471
    SOME lits |> do_literal (x, sn) |> Option.map (rpair sexps)
blanchet@35385
   472
  | do_notin_mtype_fv Plus sexp MAlpha (SOME (lits, sexps)) =
blanchet@33192
   473
    SOME (lits, insert (op =) sexp sexps)
blanchet@35385
   474
  | do_notin_mtype_fv sn sexp (MFun (M1, S sn', M2)) accum =
blanchet@34982
   475
    accum |> (if sn' = Plus andalso sn = Plus then
blanchet@35385
   476
                do_notin_mtype_fv Plus sexp M1
blanchet@34982
   477
              else
blanchet@34982
   478
                I)
blanchet@34982
   479
          |> (if sn' = Minus orelse sn = Plus then
blanchet@35385
   480
                do_notin_mtype_fv Minus sexp M1
blanchet@34982
   481
              else
blanchet@34982
   482
                I)
blanchet@35385
   483
          |> do_notin_mtype_fv sn sexp M2
blanchet@35385
   484
  | do_notin_mtype_fv Plus sexp (MFun (M1, V x, M2)) accum =
blanchet@34982
   485
    accum |> (case do_literal (x, Minus) (SOME sexp) of
blanchet@33192
   486
                NONE => I
blanchet@35385
   487
              | SOME sexp' => do_notin_mtype_fv Plus sexp' M1)
blanchet@35385
   488
          |> do_notin_mtype_fv Minus sexp M1
blanchet@35385
   489
          |> do_notin_mtype_fv Plus sexp M2
blanchet@35385
   490
  | do_notin_mtype_fv Minus sexp (MFun (M1, V x, M2)) accum =
blanchet@34982
   491
    accum |> (case do_literal (x, Plus) (SOME sexp) of
blanchet@33192
   492
                NONE => I
blanchet@35385
   493
              | SOME sexp' => do_notin_mtype_fv Plus sexp' M1)
blanchet@35385
   494
          |> do_notin_mtype_fv Minus sexp M2
blanchet@35385
   495
  | do_notin_mtype_fv sn sexp (MPair (M1, M2)) accum =
blanchet@35385
   496
    accum |> fold (do_notin_mtype_fv sn sexp) [M1, M2]
blanchet@35385
   497
  | do_notin_mtype_fv sn sexp (MType (_, Ms)) accum =
blanchet@35385
   498
    accum |> fold (do_notin_mtype_fv sn sexp) Ms
blanchet@35385
   499
  | do_notin_mtype_fv _ _ M _ =
blanchet@35665
   500
    raise MTYPE ("Nitpick_Mono.do_notin_mtype_fv", [M], [])
blanchet@33192
   501
blanchet@35385
   502
(* sign -> mtyp -> constraint_set -> constraint_set *)
blanchet@35832
   503
fun add_notin_mtype_fv sn M ((lits, comps, sexps) : constraint_set) =
blanchet@35665
   504
    (print_g ("*** Add " ^ string_for_mtype M ^ " is " ^
blanchet@35665
   505
              (case sn of Minus => "concrete" | Plus => "complete") ^ ".");
blanchet@35385
   506
     case do_notin_mtype_fv sn [] M (SOME (lits, sexps)) of
blanchet@35812
   507
       NONE => (print_g "**** Unsolvable"; raise UNSOLVABLE ())
blanchet@35812
   508
     | SOME (lits, sexps) => (lits, comps, sexps))
blanchet@33192
   509
blanchet@35385
   510
(* mtyp -> constraint_set -> constraint_set *)
blanchet@35665
   511
val add_mtype_is_concrete = add_notin_mtype_fv Minus
blanchet@35665
   512
val add_mtype_is_complete = add_notin_mtype_fv Plus
blanchet@33192
   513
blanchet@35384
   514
val bool_from_minus = true
blanchet@35384
   515
blanchet@33192
   516
(* sign -> bool *)
blanchet@35384
   517
fun bool_from_sign Plus = not bool_from_minus
blanchet@35384
   518
  | bool_from_sign Minus = bool_from_minus
blanchet@33192
   519
(* bool -> sign *)
blanchet@35384
   520
fun sign_from_bool b = if b = bool_from_minus then Minus else Plus
blanchet@33192
   521
blanchet@33192
   522
(* literal -> PropLogic.prop_formula *)
blanchet@33192
   523
fun prop_for_literal (x, sn) =
blanchet@33192
   524
  (not (bool_from_sign sn) ? PropLogic.Not) (PropLogic.BoolVar x)
blanchet@33192
   525
(* sign_atom -> PropLogic.prop_formula *)
blanchet@33192
   526
fun prop_for_sign_atom_eq (S sn', sn) =
blanchet@33192
   527
    if sn = sn' then PropLogic.True else PropLogic.False
blanchet@33192
   528
  | prop_for_sign_atom_eq (V x, sn) = prop_for_literal (x, sn)
blanchet@33192
   529
(* sign_expr -> PropLogic.prop_formula *)
blanchet@33192
   530
fun prop_for_sign_expr xs = PropLogic.exists (map prop_for_literal xs)
blanchet@33192
   531
(* var list -> sign -> PropLogic.prop_formula *)
blanchet@33192
   532
fun prop_for_exists_eq xs sn =
blanchet@33192
   533
  PropLogic.exists (map (fn x => prop_for_literal (x, sn)) xs)
blanchet@33192
   534
(* comp -> PropLogic.prop_formula *)
blanchet@33192
   535
fun prop_for_comp (a1, a2, Eq, []) =
blanchet@33192
   536
    PropLogic.SAnd (prop_for_comp (a1, a2, Leq, []),
blanchet@33192
   537
                    prop_for_comp (a2, a1, Leq, []))
blanchet@33192
   538
  | prop_for_comp (a1, a2, Leq, []) =
blanchet@34982
   539
    PropLogic.SOr (prop_for_sign_atom_eq (a1, Plus),
blanchet@34982
   540
                   prop_for_sign_atom_eq (a2, Minus))
blanchet@33192
   541
  | prop_for_comp (a1, a2, cmp, xs) =
blanchet@34982
   542
    PropLogic.SOr (prop_for_exists_eq xs Minus, prop_for_comp (a1, a2, cmp, []))
blanchet@33192
   543
blanchet@33192
   544
(* var -> (int -> bool option) -> literal list -> literal list *)
blanchet@34123
   545
fun literals_from_assignments max_var assigns lits =
blanchet@33192
   546
  fold (fn x => fn accum =>
blanchet@33192
   547
           if AList.defined (op =) lits x then
blanchet@33192
   548
             accum
blanchet@34123
   549
           else case assigns x of
blanchet@33192
   550
             SOME b => (x, sign_from_bool b) :: accum
blanchet@33192
   551
           | NONE => accum) (max_var downto 1) lits
blanchet@33192
   552
blanchet@33192
   553
(* comp -> string *)
blanchet@33192
   554
fun string_for_comp (a1, a2, cmp, xs) =
blanchet@33192
   555
  string_for_sign_atom a1 ^ " " ^ string_for_comp_op cmp ^
blanchet@33192
   556
  subscript_string_for_vars " \<and> " xs ^ " " ^ string_for_sign_atom a2
blanchet@33192
   557
blanchet@33192
   558
(* literal list -> comp list -> sign_expr list -> unit *)
blanchet@33192
   559
fun print_problem lits comps sexps =
blanchet@33192
   560
  print_g ("*** Problem:\n" ^ cat_lines (map string_for_literal lits @
blanchet@33192
   561
                                         map string_for_comp comps @
blanchet@33192
   562
                                         map string_for_sign_expr sexps))
blanchet@33192
   563
blanchet@33192
   564
(* literal list -> unit *)
blanchet@33192
   565
fun print_solution lits =
blanchet@34982
   566
  let val (pos, neg) = List.partition (curry (op =) Plus o snd) lits in
blanchet@33192
   567
    print_g ("*** Solution:\n" ^
blanchet@33192
   568
             "+: " ^ commas (map (string_for_var o fst) pos) ^ "\n" ^
blanchet@33192
   569
             "-: " ^ commas (map (string_for_var o fst) neg))
blanchet@33192
   570
  end
blanchet@33192
   571
blanchet@35384
   572
(* var -> constraint_set -> literal list option *)
blanchet@35812
   573
fun solve max_var (lits, comps, sexps) =
blanchet@33192
   574
    let
blanchet@35386
   575
      (* (int -> bool option) -> literal list option *)
blanchet@35386
   576
      fun do_assigns assigns =
blanchet@35386
   577
        SOME (literals_from_assignments max_var assigns lits
blanchet@35386
   578
              |> tap print_solution)
blanchet@33192
   579
      val _ = print_problem lits comps sexps
blanchet@33192
   580
      val prop = PropLogic.all (map prop_for_literal lits @
blanchet@33192
   581
                                map prop_for_comp comps @
blanchet@33192
   582
                                map prop_for_sign_expr sexps)
blanchet@35386
   583
      val default_val = bool_from_sign Minus
blanchet@33192
   584
    in
blanchet@35386
   585
      if PropLogic.eval (K default_val) prop then
blanchet@35386
   586
        do_assigns (K (SOME default_val))
blanchet@35386
   587
      else
blanchet@35386
   588
        let
blanchet@35386
   589
          (* use the first ML solver (to avoid startup overhead) *)
blanchet@35386
   590
          val solvers = !SatSolver.solvers
blanchet@35386
   591
                        |> filter (member (op =) ["dptsat", "dpll"] o fst)
blanchet@35386
   592
        in
blanchet@35386
   593
          case snd (hd solvers) prop of
blanchet@35386
   594
            SatSolver.SATISFIABLE assigns => do_assigns assigns
blanchet@35386
   595
          | _ => NONE
blanchet@35386
   596
        end
blanchet@33192
   597
    end
blanchet@33192
   598
blanchet@35385
   599
type mtype_schema = mtyp * constraint_set
blanchet@35385
   600
type mtype_context =
blanchet@35665
   601
  {bound_Ts: typ list,
blanchet@35665
   602
   bound_Ms: mtyp list,
blanchet@35385
   603
   frees: (styp * mtyp) list,
blanchet@35385
   604
   consts: (styp * mtyp) list}
blanchet@33192
   605
blanchet@35385
   606
type accumulator = mtype_context * constraint_set
blanchet@33192
   607
blanchet@35665
   608
val initial_gamma = {bound_Ts = [], bound_Ms = [], frees = [], consts = []}
blanchet@33192
   609
blanchet@35665
   610
(* typ -> mtyp -> mtype_context -> mtype_context *)
blanchet@35665
   611
fun push_bound T M {bound_Ts, bound_Ms, frees, consts} =
blanchet@35665
   612
  {bound_Ts = T :: bound_Ts, bound_Ms = M :: bound_Ms, frees = frees,
blanchet@35665
   613
   consts = consts}
blanchet@35385
   614
(* mtype_context -> mtype_context *)
blanchet@35665
   615
fun pop_bound {bound_Ts, bound_Ms, frees, consts} =
blanchet@35665
   616
  {bound_Ts = tl bound_Ts, bound_Ms = tl bound_Ms, frees = frees,
blanchet@35665
   617
   consts = consts}
blanchet@35665
   618
  handle List.Empty => initial_gamma (* FIXME: needed? *)
blanchet@33192
   619
blanchet@35385
   620
(* mdata -> term -> accumulator -> mterm * accumulator *)
blanchet@35386
   621
fun consider_term (mdata as {hol_ctxt as {thy, ctxt, stds, fast_descrs,
blanchet@35665
   622
                                          def_table, ...},
blanchet@35220
   623
                             alpha_T, max_fresh, ...}) =
blanchet@33192
   624
  let
blanchet@35385
   625
    (* typ -> mtyp *)
blanchet@35672
   626
    val mtype_for = fresh_mtype_for_type mdata false
blanchet@35385
   627
    (* mtyp -> mtyp *)
blanchet@35665
   628
    fun plus_set_mtype_for_dom M =
blanchet@35385
   629
      MFun (M, S (if exists_alpha_sub_mtype M then Plus else Minus), bool_M)
blanchet@35385
   630
    (* typ -> accumulator -> mterm * accumulator *)
blanchet@35385
   631
    fun do_all T (gamma, cset) =
blanchet@33192
   632
      let
blanchet@35385
   633
        val abs_M = mtype_for (domain_type (domain_type T))
blanchet@35386
   634
        val body_M = mtype_for (body_type T)
blanchet@33192
   635
      in
blanchet@35385
   636
        (MFun (MFun (abs_M, S Minus, body_M), S Minus, body_M),
blanchet@35665
   637
         (gamma, cset |> add_mtype_is_complete abs_M))
blanchet@33192
   638
      end
blanchet@33192
   639
    fun do_equals T (gamma, cset) =
blanchet@35385
   640
      let val M = mtype_for (domain_type T) in
blanchet@35385
   641
        (MFun (M, S Minus, MFun (M, V (Unsynchronized.inc max_fresh),
blanchet@35385
   642
                                 mtype_for (nth_range_type 2 T))),
blanchet@35665
   643
         (gamma, cset |> add_mtype_is_concrete M))
blanchet@33192
   644
      end
blanchet@33192
   645
    fun do_robust_set_operation T (gamma, cset) =
blanchet@33192
   646
      let
blanchet@33192
   647
        val set_T = domain_type T
blanchet@35385
   648
        val M1 = mtype_for set_T
blanchet@35385
   649
        val M2 = mtype_for set_T
blanchet@35385
   650
        val M3 = mtype_for set_T
blanchet@33192
   651
      in
blanchet@35385
   652
        (MFun (M1, S Minus, MFun (M2, S Minus, M3)),
blanchet@35385
   653
         (gamma, cset |> add_is_sub_mtype M1 M3 |> add_is_sub_mtype M2 M3))
blanchet@33192
   654
      end
blanchet@33192
   655
    fun do_fragile_set_operation T (gamma, cset) =
blanchet@33192
   656
      let
blanchet@33192
   657
        val set_T = domain_type T
blanchet@35385
   658
        val set_M = mtype_for set_T
blanchet@35385
   659
        (* typ -> mtyp *)
blanchet@35665
   660
        fun custom_mtype_for (T as Type (@{type_name fun}, [T1, T2])) =
blanchet@35385
   661
            if T = set_T then set_M
blanchet@35385
   662
            else MFun (custom_mtype_for T1, S Minus, custom_mtype_for T2)
blanchet@35385
   663
          | custom_mtype_for T = mtype_for T
blanchet@33192
   664
      in
blanchet@35665
   665
        (custom_mtype_for T, (gamma, cset |> add_mtype_is_concrete set_M))
blanchet@33192
   666
      end
blanchet@35385
   667
    (* typ -> accumulator -> mtyp * accumulator *)
blanchet@33192
   668
    fun do_pair_constr T accum =
blanchet@35385
   669
      case mtype_for (nth_range_type 2 T) of
blanchet@35385
   670
        M as MPair (a_M, b_M) =>
blanchet@35385
   671
        (MFun (a_M, S Minus, MFun (b_M, S Minus, M)), accum)
blanchet@35665
   672
      | M => raise MTYPE ("Nitpick_Mono.consider_term.do_pair_constr", [M], [])
blanchet@35385
   673
    (* int -> typ -> accumulator -> mtyp * accumulator *)
blanchet@33192
   674
    fun do_nth_pair_sel n T =
blanchet@35385
   675
      case mtype_for (domain_type T) of
blanchet@35385
   676
        M as MPair (a_M, b_M) =>
blanchet@35385
   677
        pair (MFun (M, S Minus, if n = 0 then a_M else b_M))
blanchet@35665
   678
      | M => raise MTYPE ("Nitpick_Mono.consider_term.do_nth_pair_sel", [M], [])
blanchet@35385
   679
    (* term -> string -> typ -> term -> term -> term -> accumulator
blanchet@35385
   680
       -> mterm * accumulator *)
blanchet@35385
   681
    fun do_bounded_quantifier t0 abs_s abs_T connective_t bound_t body_t accum =
blanchet@33192
   682
      let
blanchet@35385
   683
        val abs_M = mtype_for abs_T
blanchet@35665
   684
        val (bound_m, accum) =
blanchet@35665
   685
          accum |>> push_bound abs_T abs_M |> do_term bound_t
blanchet@35665
   686
        val expected_bound_M = plus_set_mtype_for_dom abs_M
blanchet@35385
   687
        val (body_m, accum) =
blanchet@35385
   688
          accum ||> add_mtypes_equal expected_bound_M (mtype_of_mterm bound_m)
blanchet@35385
   689
                |> do_term body_t ||> apfst pop_bound
blanchet@35385
   690
        val bound_M = mtype_of_mterm bound_m
blanchet@35385
   691
        val (M1, a, M2) = dest_MFun bound_M
blanchet@33192
   692
      in
blanchet@35386
   693
        (MApp (MRaw (t0, MFun (bound_M, S Minus, bool_M)),
blanchet@35385
   694
               MAbs (abs_s, abs_T, M1, a,
blanchet@35386
   695
                     MApp (MApp (MRaw (connective_t,
blanchet@35386
   696
                                       mtype_for (fastype_of connective_t)),
blanchet@35386
   697
                                 MApp (bound_m, MRaw (Bound 0, M1))),
blanchet@35385
   698
                           body_m))), accum)
blanchet@33192
   699
      end
blanchet@35385
   700
    (* term -> accumulator -> mterm * accumulator *)
blanchet@35812
   701
    and do_term t (accum as (gamma as {bound_Ts, bound_Ms, frees, consts},
blanchet@35665
   702
                             cset)) =
blanchet@33192
   703
        (case t of
blanchet@33192
   704
           Const (x as (s, T)) =>
blanchet@33192
   705
           (case AList.lookup (op =) consts x of
blanchet@35385
   706
              SOME M => (M, accum)
blanchet@33192
   707
            | NONE =>
blanchet@33192
   708
              if not (could_exist_alpha_subtype alpha_T T) then
blanchet@35385
   709
                (mtype_for T, accum)
blanchet@33192
   710
              else case s of
blanchet@35385
   711
                @{const_name all} => do_all T accum
blanchet@33192
   712
              | @{const_name "=="} => do_equals T accum
blanchet@35385
   713
              | @{const_name All} => do_all T accum
blanchet@35385
   714
              | @{const_name Ex} =>
blanchet@35386
   715
                let val set_T = domain_type T in
blanchet@35386
   716
                  do_term (Abs (Name.uu, set_T,
blanchet@35386
   717
                                @{const Not} $ (HOLogic.mk_eq
blanchet@35386
   718
                                    (Abs (Name.uu, domain_type set_T,
blanchet@35386
   719
                                          @{const False}),
blanchet@35386
   720
                                     Bound 0)))) accum
blanchet@35386
   721
                  |>> mtype_of_mterm
blanchet@35386
   722
                end
blanchet@33192
   723
              | @{const_name "op ="} => do_equals T accum
blanchet@35812
   724
              | @{const_name The} => (print_g "*** The"; raise UNSOLVABLE ())
blanchet@35812
   725
              | @{const_name Eps} => (print_g "*** Eps"; raise UNSOLVABLE ())
blanchet@33192
   726
              | @{const_name If} =>
blanchet@33192
   727
                do_robust_set_operation (range_type T) accum
blanchet@35385
   728
                |>> curry3 MFun bool_M (S Minus)
blanchet@33192
   729
              | @{const_name Pair} => do_pair_constr T accum
blanchet@33192
   730
              | @{const_name fst} => do_nth_pair_sel 0 T accum
blanchet@33192
   731
              | @{const_name snd} => do_nth_pair_sel 1 T accum 
blanchet@33192
   732
              | @{const_name Id} =>
blanchet@35385
   733
                (MFun (mtype_for (domain_type T), S Minus, bool_M), accum)
blanchet@33192
   734
              | @{const_name insert} =>
blanchet@33192
   735
                let
blanchet@33192
   736
                  val set_T = domain_type (range_type T)
blanchet@35385
   737
                  val M1 = mtype_for (domain_type set_T)
blanchet@35665
   738
                  val M1' = plus_set_mtype_for_dom M1
blanchet@35385
   739
                  val M2 = mtype_for set_T
blanchet@35385
   740
                  val M3 = mtype_for set_T
blanchet@33192
   741
                in
blanchet@35385
   742
                  (MFun (M1, S Minus, MFun (M2, S Minus, M3)),
blanchet@35665
   743
                   (gamma, cset |> add_mtype_is_concrete M1
blanchet@35385
   744
                                |> add_is_sub_mtype M1' M3
blanchet@35385
   745
                                |> add_is_sub_mtype M2 M3))
blanchet@33192
   746
                end
blanchet@33192
   747
              | @{const_name converse} =>
blanchet@33192
   748
                let
blanchet@33192
   749
                  val x = Unsynchronized.inc max_fresh
blanchet@35385
   750
                  (* typ -> mtyp *)
blanchet@35385
   751
                  fun mtype_for_set T =
blanchet@35385
   752
                    MFun (mtype_for (domain_type T), V x, bool_M)
blanchet@35385
   753
                  val ab_set_M = domain_type T |> mtype_for_set
blanchet@35385
   754
                  val ba_set_M = range_type T |> mtype_for_set
blanchet@35385
   755
                in (MFun (ab_set_M, S Minus, ba_set_M), accum) end
blanchet@33192
   756
              | @{const_name trancl} => do_fragile_set_operation T accum
blanchet@33192
   757
              | @{const_name rel_comp} =>
blanchet@33192
   758
                let
blanchet@33192
   759
                  val x = Unsynchronized.inc max_fresh
blanchet@35385
   760
                  (* typ -> mtyp *)
blanchet@35385
   761
                  fun mtype_for_set T =
blanchet@35385
   762
                    MFun (mtype_for (domain_type T), V x, bool_M)
blanchet@35385
   763
                  val bc_set_M = domain_type T |> mtype_for_set
blanchet@35385
   764
                  val ab_set_M = domain_type (range_type T) |> mtype_for_set
blanchet@35385
   765
                  val ac_set_M = nth_range_type 2 T |> mtype_for_set
blanchet@33192
   766
                in
blanchet@35385
   767
                  (MFun (bc_set_M, S Minus, MFun (ab_set_M, S Minus, ac_set_M)),
blanchet@33192
   768
                   accum)
blanchet@33192
   769
                end
blanchet@33192
   770
              | @{const_name image} =>
blanchet@33192
   771
                let
blanchet@35385
   772
                  val a_M = mtype_for (domain_type (domain_type T))
blanchet@35385
   773
                  val b_M = mtype_for (range_type (domain_type T))
blanchet@33192
   774
                in
blanchet@35385
   775
                  (MFun (MFun (a_M, S Minus, b_M), S Minus,
blanchet@35665
   776
                         MFun (plus_set_mtype_for_dom a_M, S Minus,
blanchet@35665
   777
                               plus_set_mtype_for_dom b_M)), accum)
blanchet@33192
   778
                end
blanchet@35672
   779
              | @{const_name finite} =>
blanchet@35672
   780
                let val M1 = mtype_for (domain_type (domain_type T)) in
blanchet@35672
   781
                  (MFun (plus_set_mtype_for_dom M1, S Minus, bool_M), accum)
blanchet@35672
   782
                end
blanchet@33192
   783
              | @{const_name Sigma} =>
blanchet@33192
   784
                let
blanchet@33192
   785
                  val x = Unsynchronized.inc max_fresh
blanchet@35385
   786
                  (* typ -> mtyp *)
blanchet@35385
   787
                  fun mtype_for_set T =
blanchet@35385
   788
                    MFun (mtype_for (domain_type T), V x, bool_M)
blanchet@33192
   789
                  val a_set_T = domain_type T
blanchet@35385
   790
                  val a_M = mtype_for (domain_type a_set_T)
blanchet@35385
   791
                  val b_set_M = mtype_for_set (range_type (domain_type
blanchet@33192
   792
                                                               (range_type T)))
blanchet@35385
   793
                  val a_set_M = mtype_for_set a_set_T
blanchet@35385
   794
                  val a_to_b_set_M = MFun (a_M, S Minus, b_set_M)
blanchet@35385
   795
                  val ab_set_M = mtype_for_set (nth_range_type 2 T)
blanchet@33192
   796
                in
blanchet@35385
   797
                  (MFun (a_set_M, S Minus,
blanchet@35385
   798
                         MFun (a_to_b_set_M, S Minus, ab_set_M)), accum)
blanchet@33192
   799
                end
blanchet@35220
   800
              | _ =>
blanchet@35671
   801
                if s = @{const_name safe_The} orelse
blanchet@35671
   802
                   s = @{const_name safe_Eps} then
blanchet@35671
   803
                  let
blanchet@35671
   804
                    val a_set_M = mtype_for (domain_type T)
blanchet@35671
   805
                    val a_M = dest_MFun a_set_M |> #1
blanchet@35671
   806
                  in (MFun (a_set_M, S Minus, a_M), accum) end
blanchet@35671
   807
                else if s = @{const_name minus_class.minus} andalso
blanchet@35671
   808
                        is_set_type (domain_type T) then
blanchet@35220
   809
                  let
blanchet@35220
   810
                    val set_T = domain_type T
blanchet@35385
   811
                    val left_set_M = mtype_for set_T
blanchet@35385
   812
                    val right_set_M = mtype_for set_T
blanchet@35220
   813
                  in
blanchet@35385
   814
                    (MFun (left_set_M, S Minus,
blanchet@35385
   815
                           MFun (right_set_M, S Minus, left_set_M)),
blanchet@35665
   816
                     (gamma, cset |> add_mtype_is_concrete right_set_M
blanchet@35385
   817
                                  |> add_is_sub_mtype right_set_M left_set_M))
blanchet@35220
   818
                  end
blanchet@35220
   819
                else if s = @{const_name ord_class.less_eq} andalso
blanchet@35220
   820
                        is_set_type (domain_type T) then
blanchet@35220
   821
                  do_fragile_set_operation T accum
blanchet@35220
   822
                else if (s = @{const_name semilattice_inf_class.inf} orelse
blanchet@35220
   823
                         s = @{const_name semilattice_sup_class.sup}) andalso
blanchet@35220
   824
                        is_set_type (domain_type T) then
blanchet@35220
   825
                  do_robust_set_operation T accum
blanchet@35220
   826
                else if is_sel s then
blanchet@35665
   827
                  (mtype_for_sel mdata x, accum)
blanchet@35220
   828
                else if is_constr thy stds x then
blanchet@35385
   829
                  (mtype_for_constr mdata x, accum)
blanchet@35672
   830
                else if is_built_in_const thy stds fast_descrs x then
blanchet@35672
   831
                  (fresh_mtype_for_type mdata true T, accum)
blanchet@35220
   832
                else
blanchet@35385
   833
                  let val M = mtype_for T in
blanchet@35665
   834
                    (M, ({bound_Ts = bound_Ts, bound_Ms = bound_Ms,
blanchet@35665
   835
                          frees = frees, consts = (x, M) :: consts}, cset))
blanchet@35386
   836
                  end) |>> curry MRaw t
blanchet@33192
   837
         | Free (x as (_, T)) =>
blanchet@33192
   838
           (case AList.lookup (op =) frees x of
blanchet@35385
   839
              SOME M => (M, accum)
blanchet@33192
   840
            | NONE =>
blanchet@35385
   841
              let val M = mtype_for T in
blanchet@35665
   842
                (M, ({bound_Ts = bound_Ts, bound_Ms = bound_Ms,
blanchet@35665
   843
                      frees = (x, M) :: frees, consts = consts}, cset))
blanchet@35386
   844
              end) |>> curry MRaw t
blanchet@35812
   845
         | Var _ => (print_g "*** Var"; raise UNSOLVABLE ())
blanchet@35665
   846
         | Bound j => (MRaw (t, nth bound_Ms j), accum)
blanchet@35385
   847
         | Abs (s, T, t') =>
blanchet@35665
   848
           (case fin_fun_body T (fastype_of1 (T :: bound_Ts, t')) t' of
blanchet@35665
   849
              SOME t' =>
blanchet@35665
   850
              let
blanchet@35665
   851
                val M = mtype_for T
blanchet@35665
   852
                val a = V (Unsynchronized.inc max_fresh)
blanchet@35665
   853
                val (m', accum) = do_term t' (accum |>> push_bound T M)
blanchet@35665
   854
              in (MAbs (s, T, M, a, m'), accum |>> pop_bound) end
blanchet@35665
   855
            | NONE =>
blanchet@35665
   856
              ((case t' of
blanchet@35665
   857
                  t1' $ Bound 0 =>
blanchet@35665
   858
                  if not (loose_bvar1 (t1', 0)) then
blanchet@35665
   859
                    do_term (incr_boundvars ~1 t1') accum
blanchet@35665
   860
                  else
blanchet@35665
   861
                    raise SAME ()
blanchet@35665
   862
                | _ => raise SAME ())
blanchet@35665
   863
               handle SAME () =>
blanchet@35665
   864
                      let
blanchet@35665
   865
                        val M = mtype_for T
blanchet@35665
   866
                        val (m', accum) = do_term t' (accum |>> push_bound T M)
blanchet@35665
   867
                      in
blanchet@35665
   868
                        (MAbs (s, T, M, S Minus, m'), accum |>> pop_bound)
blanchet@35665
   869
                      end))
blanchet@35385
   870
         | (t0 as Const (@{const_name All}, _))
blanchet@35385
   871
           $ Abs (s', T', (t10 as @{const "op -->"}) $ (t11 $ Bound 0) $ t12) =>
blanchet@35385
   872
           do_bounded_quantifier t0 s' T' t10 t11 t12 accum
blanchet@35385
   873
         | (t0 as Const (@{const_name Ex}, _))
blanchet@35385
   874
           $ Abs (s', T', (t10 as @{const "op &"}) $ (t11 $ Bound 0) $ t12) =>
blanchet@35385
   875
           do_bounded_quantifier t0 s' T' t10 t11 t12 accum
blanchet@33192
   876
         | Const (@{const_name Let}, _) $ t1 $ t2 =>
blanchet@33192
   877
           do_term (betapply (t2, t1)) accum
blanchet@33192
   878
         | t1 $ t2 =>
blanchet@33192
   879
           let
blanchet@35385
   880
             val (m1, accum) = do_term t1 accum
blanchet@35385
   881
             val (m2, accum) = do_term t2 accum
blanchet@33192
   882
           in
blanchet@35812
   883
             let
blanchet@35812
   884
               val T11 = domain_type (fastype_of1 (bound_Ts, t1))
blanchet@35812
   885
               val T2 = fastype_of1 (bound_Ts, t2)
blanchet@35812
   886
               val M11 = mtype_of_mterm m1 |> dest_MFun |> #1
blanchet@35812
   887
               val M2 = mtype_of_mterm m2
blanchet@35812
   888
             in (MApp (m1, m2), accum ||> add_is_sub_mtype M2 M11) end
blanchet@33192
   889
           end)
blanchet@35385
   890
        |> tap (fn (m, _) => print_g ("  \<Gamma> \<turnstile> " ^
blanchet@35385
   891
                                      string_for_mterm ctxt m))
blanchet@33192
   892
  in do_term end
blanchet@33192
   893
blanchet@35665
   894
(* int -> mtyp -> accumulator -> accumulator *)
blanchet@35665
   895
fun force_minus_funs 0 _ = I
blanchet@35665
   896
  | force_minus_funs n (M as MFun (M1, _, M2)) =
blanchet@35665
   897
    add_mtypes_equal M (MFun (M1, S Minus, M2))
blanchet@35665
   898
    #> force_minus_funs (n - 1) M2
blanchet@35665
   899
  | force_minus_funs _ M =
blanchet@35665
   900
    raise MTYPE ("Nitpick_Mono.force_minus_funs", [M], [])
blanchet@35665
   901
(* mdata -> bool -> styp -> term -> term -> mterm * accumulator *)
blanchet@35665
   902
fun consider_general_equals mdata def (x as (_, T)) t1 t2 accum =
blanchet@35386
   903
  let
blanchet@35386
   904
    val (m1, accum) = consider_term mdata t1 accum
blanchet@35386
   905
    val (m2, accum) = consider_term mdata t2 accum
blanchet@35386
   906
    val M1 = mtype_of_mterm m1
blanchet@35386
   907
    val M2 = mtype_of_mterm m2
blanchet@35665
   908
    val accum = accum ||> add_mtypes_equal M1 M2
blanchet@35672
   909
    val body_M = fresh_mtype_for_type mdata false (nth_range_type 2 T)
blanchet@35665
   910
    val m = MApp (MApp (MRaw (Const x,
blanchet@35665
   911
                MFun (M1, S Minus, MFun (M2, S Minus, body_M))), m1), m2)
blanchet@35386
   912
  in
blanchet@35665
   913
    (m, if def then
blanchet@35665
   914
          let val (head_m, arg_ms) = strip_mcomb m1 in
blanchet@35665
   915
            accum ||> force_minus_funs (length arg_ms) (mtype_of_mterm head_m)
blanchet@35665
   916
          end
blanchet@35665
   917
        else
blanchet@35665
   918
          accum)
blanchet@35386
   919
  end
blanchet@35386
   920
blanchet@35386
   921
(* mdata -> sign -> term -> accumulator -> mterm * accumulator *)
blanchet@35385
   922
fun consider_general_formula (mdata as {hol_ctxt = {ctxt, ...}, ...}) =
blanchet@33192
   923
  let
blanchet@35385
   924
    (* typ -> mtyp *)
blanchet@35672
   925
    val mtype_for = fresh_mtype_for_type mdata false
blanchet@35386
   926
    (* term -> accumulator -> mterm * accumulator *)
blanchet@35386
   927
    val do_term = consider_term mdata
blanchet@35386
   928
    (* sign -> term -> accumulator -> mterm * accumulator *)
blanchet@35812
   929
    fun do_formula sn t accum =
blanchet@33192
   930
        let
blanchet@35386
   931
          (* styp -> string -> typ -> term -> mterm * accumulator *)
blanchet@35386
   932
          fun do_quantifier (quant_x as (quant_s, _)) abs_s abs_T body_t =
blanchet@33192
   933
            let
blanchet@35385
   934
              val abs_M = mtype_for abs_T
blanchet@34982
   935
              val side_cond = ((sn = Minus) = (quant_s = @{const_name Ex}))
blanchet@35386
   936
              val (body_m, accum) =
blanchet@35665
   937
                accum ||> side_cond ? add_mtype_is_complete abs_M
blanchet@35665
   938
                      |>> push_bound abs_T abs_M |> do_formula sn body_t
blanchet@35386
   939
              val body_M = mtype_of_mterm body_m
blanchet@33192
   940
            in
blanchet@35665
   941
              (MApp (MRaw (Const quant_x,
blanchet@35665
   942
                           MFun (MFun (abs_M, S Minus, body_M), S Minus,
blanchet@35665
   943
                                 body_M)),
blanchet@35386
   944
                     MAbs (abs_s, abs_T, abs_M, S Minus, body_m)),
blanchet@35386
   945
               accum |>> pop_bound)
blanchet@33192
   946
            end
blanchet@35386
   947
          (* styp -> term -> term -> mterm * accumulator *)
blanchet@35386
   948
          fun do_equals x t1 t2 =
blanchet@33192
   949
            case sn of
blanchet@35386
   950
              Plus => do_term t accum
blanchet@35665
   951
            | Minus => consider_general_equals mdata false x t1 t2 accum
blanchet@33192
   952
        in
blanchet@33192
   953
          case t of
blanchet@35386
   954
            Const (x as (@{const_name all}, _)) $ Abs (s1, T1, t1) =>
blanchet@35386
   955
            do_quantifier x s1 T1 t1
blanchet@35386
   956
          | Const (x as (@{const_name "=="}, _)) $ t1 $ t2 => do_equals x t1 t2
blanchet@35386
   957
          | @{const Trueprop} $ t1 =>
blanchet@35386
   958
            let val (m1, accum) = do_formula sn t1 accum in
blanchet@35386
   959
              (MApp (MRaw (@{const Trueprop}, mtype_for (bool_T --> prop_T)),
blanchet@35386
   960
                     m1), accum)
blanchet@35386
   961
            end
blanchet@35386
   962
          | @{const Not} $ t1 =>
blanchet@35386
   963
            let val (m1, accum) = do_formula (negate sn) t1 accum in
blanchet@35386
   964
              (MApp (MRaw (@{const Not}, mtype_for (bool_T --> bool_T)), m1),
blanchet@35386
   965
               accum)
blanchet@35386
   966
            end
blanchet@35386
   967
          | Const (x as (@{const_name All}, _)) $ Abs (s1, T1, t1) =>
blanchet@35386
   968
            do_quantifier x s1 T1 t1
blanchet@35386
   969
          | Const (x0 as (s0 as @{const_name Ex}, T0))
blanchet@35386
   970
            $ (t1 as Abs (s1, T1, t1')) =>
blanchet@35385
   971
            (case sn of
blanchet@35386
   972
               Plus => do_quantifier x0 s1 T1 t1'
blanchet@35385
   973
             | Minus =>
blanchet@35665
   974
               (* FIXME: Move elsewhere *)
blanchet@35385
   975
               do_term (@{const Not}
blanchet@35385
   976
                        $ (HOLogic.eq_const (domain_type T0) $ t1
blanchet@35386
   977
                           $ Abs (Name.uu, T1, @{const False}))) accum)
blanchet@35386
   978
          | Const (x as (@{const_name "op ="}, _)) $ t1 $ t2 =>
blanchet@35386
   979
            do_equals x t1 t2
blanchet@35386
   980
          | (t0 as Const (s0, _)) $ t1 $ t2 =>
blanchet@35386
   981
            if s0 = @{const_name "==>"} orelse s0 = @{const_name "op &"} orelse
blanchet@35386
   982
               s0 = @{const_name "op |"} orelse s0 = @{const_name "op -->"} then
blanchet@35386
   983
              let
blanchet@35386
   984
                val impl = (s0 = @{const_name "==>"} orelse
blanchet@35386
   985
                           s0 = @{const_name "op -->"})
blanchet@35386
   986
                val (m1, accum) = do_formula (sn |> impl ? negate) t1 accum
blanchet@35386
   987
                val (m2, accum) = do_formula sn t2 accum
blanchet@35386
   988
              in
blanchet@35386
   989
                (MApp (MApp (MRaw (t0, mtype_for (fastype_of t0)), m1), m2),
blanchet@35386
   990
                 accum)
blanchet@35386
   991
              end 
blanchet@35386
   992
            else
blanchet@35386
   993
              do_term t accum
blanchet@35386
   994
          | _ => do_term t accum
blanchet@33192
   995
        end
blanchet@35386
   996
        |> tap (fn (m, _) =>
blanchet@35386
   997
                   print_g ("\<Gamma> \<turnstile> " ^
blanchet@35386
   998
                            string_for_mterm ctxt m ^ " : o\<^sup>" ^
blanchet@35386
   999
                            string_for_sign sn))
blanchet@33192
  1000
  in do_formula end
blanchet@33192
  1001
blanchet@33192
  1002
(* The harmless axiom optimization below is somewhat too aggressive in the face
blanchet@33192
  1003
   of (rather peculiar) user-defined axioms. *)
blanchet@33192
  1004
val harmless_consts =
blanchet@33192
  1005
  [@{const_name ord_class.less}, @{const_name ord_class.less_eq}]
blanchet@33192
  1006
val bounteous_consts = [@{const_name bisim}]
blanchet@33192
  1007
blanchet@35665
  1008
(* mdata -> term -> bool *)
blanchet@35665
  1009
fun is_harmless_axiom ({no_harmless = true, ...} : mdata) _ = false
blanchet@35665
  1010
  | is_harmless_axiom {hol_ctxt = {thy, stds, fast_descrs, ...}, ...} t =
blanchet@35665
  1011
    Term.add_consts t []
blanchet@35665
  1012
    |> filter_out (is_built_in_const thy stds fast_descrs)
blanchet@35665
  1013
    |> (forall (member (op =) harmless_consts o original_name o fst) orf
blanchet@35665
  1014
        exists (member (op =) bounteous_consts o fst))
blanchet@33192
  1015
blanchet@35386
  1016
(* mdata -> term -> accumulator -> mterm * accumulator *)
blanchet@35665
  1017
fun consider_nondefinitional_axiom mdata t =
blanchet@35665
  1018
  if is_harmless_axiom mdata t then pair (MRaw (t, dummy_M))
blanchet@35386
  1019
  else consider_general_formula mdata Plus t
blanchet@33192
  1020
blanchet@35386
  1021
(* mdata -> term -> accumulator -> mterm * accumulator *)
blanchet@35665
  1022
fun consider_definitional_axiom (mdata as {hol_ctxt = {thy, ...}, ...}) t =
blanchet@33192
  1023
  if not (is_constr_pattern_formula thy t) then
blanchet@35386
  1024
    consider_nondefinitional_axiom mdata t
blanchet@35665
  1025
  else if is_harmless_axiom mdata t then
blanchet@35386
  1026
    pair (MRaw (t, dummy_M))
blanchet@33192
  1027
  else
blanchet@33192
  1028
    let
blanchet@35386
  1029
      (* typ -> mtyp *)
blanchet@35672
  1030
      val mtype_for = fresh_mtype_for_type mdata false
blanchet@35386
  1031
      (* term -> accumulator -> mterm * accumulator *)
blanchet@35386
  1032
      val do_term = consider_term mdata
blanchet@35386
  1033
      (* term -> string -> typ -> term -> accumulator -> mterm * accumulator *)
blanchet@35386
  1034
      fun do_all quant_t abs_s abs_T body_t accum =
blanchet@35386
  1035
        let
blanchet@35386
  1036
          val abs_M = mtype_for abs_T
blanchet@35386
  1037
          val (body_m, accum) =
blanchet@35665
  1038
            accum |>> push_bound abs_T abs_M |> do_formula body_t
blanchet@35386
  1039
          val body_M = mtype_of_mterm body_m
blanchet@35386
  1040
        in
blanchet@35665
  1041
          (MApp (MRaw (quant_t,
blanchet@35665
  1042
                       MFun (MFun (abs_M, S Minus, body_M), S Minus, body_M)),
blanchet@35386
  1043
                 MAbs (abs_s, abs_T, abs_M, S Minus, body_m)),
blanchet@35386
  1044
           accum |>> pop_bound)
blanchet@33192
  1045
        end
blanchet@35386
  1046
      (* term -> term -> term -> accumulator -> mterm * accumulator *)
blanchet@35386
  1047
      and do_conjunction t0 t1 t2 accum =
blanchet@33192
  1048
        let
blanchet@35386
  1049
          val (m1, accum) = do_formula t1 accum
blanchet@35386
  1050
          val (m2, accum) = do_formula t2 accum
blanchet@35386
  1051
        in
blanchet@35386
  1052
          (MApp (MApp (MRaw (t0, mtype_for (fastype_of t0)), m1), m2), accum)
blanchet@35386
  1053
        end
blanchet@35386
  1054
      and do_implies t0 t1 t2 accum =
blanchet@35386
  1055
        let
blanchet@35386
  1056
          val (m1, accum) = do_term t1 accum
blanchet@35386
  1057
          val (m2, accum) = do_formula t2 accum
blanchet@35386
  1058
        in
blanchet@35386
  1059
          (MApp (MApp (MRaw (t0, mtype_for (fastype_of t0)), m1), m2), accum)
blanchet@35386
  1060
        end
blanchet@33192
  1061
      (* term -> accumulator -> accumulator *)
blanchet@35812
  1062
      and do_formula t accum =
blanchet@33192
  1063
          case t of
blanchet@35386
  1064
            (t0 as Const (@{const_name all}, _)) $ Abs (s1, T1, t1) =>
blanchet@35386
  1065
            do_all t0 s1 T1 t1 accum
blanchet@35665
  1066
          | @{const Trueprop} $ t1 =>
blanchet@35665
  1067
            let val (m1, accum) = do_formula t1 accum in
blanchet@35665
  1068
              (MApp (MRaw (@{const Trueprop}, mtype_for (bool_T --> prop_T)),
blanchet@35665
  1069
                     m1), accum)
blanchet@35665
  1070
            end
blanchet@35386
  1071
          | Const (x as (@{const_name "=="}, _)) $ t1 $ t2 =>
blanchet@35665
  1072
            consider_general_equals mdata true x t1 t2 accum
blanchet@35386
  1073
          | (t0 as @{const "==>"}) $ t1 $ t2 => do_implies t0 t1 t2 accum
blanchet@35386
  1074
          | (t0 as @{const Pure.conjunction}) $ t1 $ t2 =>
blanchet@35386
  1075
            do_conjunction t0 t1 t2 accum
blanchet@35386
  1076
          | (t0 as Const (@{const_name All}, _)) $ Abs (s0, T1, t1) =>
blanchet@35386
  1077
            do_all t0 s0 T1 t1 accum
blanchet@35386
  1078
          | Const (x as (@{const_name "op ="}, _)) $ t1 $ t2 =>
blanchet@35665
  1079
            consider_general_equals mdata true x t1 t2 accum
blanchet@35386
  1080
          | (t0 as @{const "op &"}) $ t1 $ t2 => do_conjunction t0 t1 t2 accum
blanchet@35386
  1081
          | (t0 as @{const "op -->"}) $ t1 $ t2 => do_implies t0 t1 t2 accum
blanchet@33232
  1082
          | _ => raise TERM ("Nitpick_Mono.consider_definitional_axiom.\
blanchet@33192
  1083
                             \do_formula", [t])
blanchet@33192
  1084
    in do_formula t end
blanchet@33192
  1085
blanchet@35385
  1086
(* Proof.context -> literal list -> term -> mtyp -> string *)
blanchet@35385
  1087
fun string_for_mtype_of_term ctxt lits t M =
blanchet@35665
  1088
  Syntax.string_of_term ctxt t ^ " : " ^ string_for_mtype (resolve_mtype lits M)
blanchet@33192
  1089
blanchet@35385
  1090
(* theory -> literal list -> mtype_context -> unit *)
blanchet@35385
  1091
fun print_mtype_context ctxt lits ({frees, consts, ...} : mtype_context) =
blanchet@35385
  1092
  map (fn (x, M) => string_for_mtype_of_term ctxt lits (Free x) M) frees @
blanchet@35385
  1093
  map (fn (x, M) => string_for_mtype_of_term ctxt lits (Const x) M) consts
blanchet@33192
  1094
  |> cat_lines |> print_g
blanchet@33192
  1095
blanchet@35386
  1096
(* ('a -> 'b -> 'c * 'd) -> 'a -> 'c list * 'b -> 'c list * 'd *)
blanchet@35665
  1097
fun amass f t (ms, accum) =
blanchet@35386
  1098
  let val (m, accum) = f t accum in (m :: ms, accum) end
blanchet@35386
  1099
blanchet@35665
  1100
(* string -> bool -> hol_context -> bool -> typ -> term list * term list
blanchet@35665
  1101
   -> (literal list * (mterm list * mterm list) * (styp * mtyp) list) option *)
blanchet@35665
  1102
fun infer which no_harmless (hol_ctxt as {ctxt, ...}) binarize alpha_T
blanchet@35665
  1103
          (nondef_ts, def_ts) =
blanchet@33192
  1104
  let
blanchet@35665
  1105
    val _ = print_g ("****** " ^ which ^ " analysis: " ^
blanchet@35385
  1106
                     string_for_mtype MAlpha ^ " is " ^
blanchet@33192
  1107
                     Syntax.string_of_typ ctxt alpha_T)
blanchet@35665
  1108
    val mdata as {max_fresh, constr_mcache, ...} =
blanchet@35665
  1109
      initial_mdata hol_ctxt binarize no_harmless alpha_T
blanchet@35812
  1110
    val accum = (initial_gamma, ([], [], []))
blanchet@35386
  1111
    val (nondef_ms, accum) =
blanchet@35665
  1112
      ([], accum) |> amass (consider_general_formula mdata Plus) (hd nondef_ts)
blanchet@35665
  1113
                  |> fold (amass (consider_nondefinitional_axiom mdata))
blanchet@35386
  1114
                          (tl nondef_ts)
blanchet@35386
  1115
    val (def_ms, (gamma, cset)) =
blanchet@35665
  1116
      ([], accum) |> fold (amass (consider_definitional_axiom mdata)) def_ts
blanchet@33192
  1117
  in
blanchet@33192
  1118
    case solve (!max_fresh) cset of
blanchet@35665
  1119
      SOME lits => (print_mtype_context ctxt lits gamma;
blanchet@35665
  1120
                    SOME (lits, (nondef_ms, def_ms), !constr_mcache))
blanchet@35665
  1121
    | _ => NONE
blanchet@33192
  1122
  end
blanchet@35812
  1123
  handle UNSOLVABLE () => NONE
blanchet@35812
  1124
       | MTYPE (loc, Ms, Ts) =>
blanchet@35665
  1125
         raise BAD (loc, commas (map string_for_mtype Ms @
blanchet@35665
  1126
                                 map (Syntax.string_of_typ ctxt) Ts))
blanchet@35665
  1127
       | MTERM (loc, ms) =>
blanchet@35665
  1128
         raise BAD (loc, commas (map (string_for_mterm ctxt) ms))
blanchet@35665
  1129
blanchet@35665
  1130
(* hol_context -> bool -> typ -> term list * term list -> bool *)
blanchet@35665
  1131
val formulas_monotonic = is_some oooo infer "Monotonicity" false
blanchet@35665
  1132
blanchet@35665
  1133
(* typ -> typ -> styp *)
blanchet@35665
  1134
fun fin_fun_constr T1 T2 =
blanchet@35665
  1135
  (@{const_name FinFun}, (T1 --> T2) --> Type (@{type_name fin_fun}, [T1, T2]))
blanchet@35665
  1136
blanchet@35665
  1137
(* hol_context -> bool -> (typ option * bool option) list -> typ
blanchet@35665
  1138
   -> term list * term list -> term list * term list *)
blanchet@35665
  1139
fun finitize_funs (hol_ctxt as {thy, stds, fast_descrs, constr_cache, ...})
blanchet@35665
  1140
                  binarize finitizes alpha_T tsp =
blanchet@35665
  1141
  case infer "Finiteness" true hol_ctxt binarize alpha_T tsp of
blanchet@35665
  1142
    SOME (lits, msp, constr_mtypes) =>
blanchet@35812
  1143
    if forall (curry (op =) Minus o snd) lits then
blanchet@35812
  1144
      tsp
blanchet@35812
  1145
    else
blanchet@35812
  1146
      let
blanchet@35812
  1147
        (* typ -> sign_atom -> bool *)
blanchet@35812
  1148
        fun should_finitize T a =
blanchet@35812
  1149
          case triple_lookup (type_match thy) finitizes T of
blanchet@35812
  1150
            SOME (SOME false) => false
blanchet@35812
  1151
          | _ => resolve_sign_atom lits a = S Plus
blanchet@35812
  1152
        (* typ -> mtyp -> typ *)
blanchet@35812
  1153
        fun type_from_mtype T M =
blanchet@35812
  1154
          case (M, T) of
blanchet@35812
  1155
            (MAlpha, _) => T
blanchet@35812
  1156
          | (MFun (M1, a, M2), Type (@{type_name fun}, Ts)) =>
blanchet@35812
  1157
            Type (if should_finitize T a then @{type_name fin_fun}
blanchet@35812
  1158
                  else @{type_name fun}, map2 type_from_mtype Ts [M1, M2])
blanchet@35812
  1159
          | (MPair (M1, M2), Type (@{type_name "*"}, Ts)) =>
blanchet@35812
  1160
            Type (@{type_name "*"}, map2 type_from_mtype Ts [M1, M2])
blanchet@35812
  1161
          | (MType _, _) => T
blanchet@35812
  1162
          | _ => raise MTYPE ("Nitpick_Mono.finitize_funs.type_from_mtype",
blanchet@35812
  1163
                              [M], [T])
blanchet@35812
  1164
        (* styp -> styp *)
blanchet@35812
  1165
        fun finitize_constr (x as (s, T)) =
blanchet@35812
  1166
          (s, case AList.lookup (op =) constr_mtypes x of
blanchet@35812
  1167
                SOME M => type_from_mtype T M
blanchet@35812
  1168
              | NONE => T)
blanchet@35812
  1169
        (* typ list -> mterm -> term *)
blanchet@35812
  1170
        fun term_from_mterm Ts m =
blanchet@35812
  1171
          case m of
blanchet@35812
  1172
            MRaw (t, M) =>
blanchet@35812
  1173
            let
blanchet@35812
  1174
              val T = fastype_of1 (Ts, t)
blanchet@35812
  1175
              val T' = type_from_mtype T M
blanchet@35812
  1176
            in
blanchet@35812
  1177
              case t of
blanchet@35812
  1178
                Const (x as (s, _)) =>
blanchet@35812
  1179
                if s = @{const_name insert} then
blanchet@35812
  1180
                  case nth_range_type 2 T' of
blanchet@35812
  1181
                    set_T' as Type (@{type_name fin_fun}, [elem_T', _]) =>
blanchet@35812
  1182
                      Abs (Name.uu, elem_T', Abs (Name.uu, set_T',
blanchet@35812
  1183
                          Const (@{const_name If},
blanchet@35812
  1184
                                 bool_T --> set_T' --> set_T' --> set_T')
blanchet@35812
  1185
                          $ (Const (@{const_name is_unknown},
blanchet@35812
  1186
                                    elem_T' --> bool_T) $ Bound 1)
blanchet@35812
  1187
                          $ (Const (@{const_name unknown}, set_T'))
blanchet@35812
  1188
                          $ (coerce_term hol_ctxt Ts T' T (Const x)
blanchet@35812
  1189
                             $ Bound 1 $ Bound 0)))
blanchet@35812
  1190
                  | _ => Const (s, T')
blanchet@35812
  1191
                else if s = @{const_name finite} then
blanchet@35812
  1192
                  case domain_type T' of
blanchet@35812
  1193
                    set_T' as Type (@{type_name fin_fun}, _) =>
blanchet@35812
  1194
                    Abs (Name.uu, set_T', @{const True})
blanchet@35812
  1195
                  | _ => Const (s, T')
blanchet@35812
  1196
                else if s = @{const_name "=="} orelse
blanchet@35812
  1197
                        s = @{const_name "op ="} then
blanchet@35812
  1198
                  Const (s, T')
blanchet@35812
  1199
                else if is_built_in_const thy stds fast_descrs x then
blanchet@35812
  1200
                  coerce_term hol_ctxt Ts T' T t
blanchet@35812
  1201
                else if is_constr thy stds x then
blanchet@35812
  1202
                  Const (finitize_constr x)
blanchet@35812
  1203
                else if is_sel s then
blanchet@35812
  1204
                  let
blanchet@35812
  1205
                    val n = sel_no_from_name s
blanchet@35812
  1206
                    val x' =
blanchet@35812
  1207
                      x |> binarized_and_boxed_constr_for_sel hol_ctxt binarize
blanchet@35812
  1208
                        |> finitize_constr
blanchet@35812
  1209
                    val x'' =
blanchet@35812
  1210
                      binarized_and_boxed_nth_sel_for_constr hol_ctxt binarize
blanchet@35812
  1211
                                                             x' n
blanchet@35812
  1212
                  in Const x'' end
blanchet@35812
  1213
                else
blanchet@35812
  1214
                  Const (s, T')
blanchet@35812
  1215
              | Free (s, T) => Free (s, type_from_mtype T M)
blanchet@35812
  1216
              | Bound _ => t
blanchet@35812
  1217
              | _ => raise MTERM ("Nitpick_Mono.finitize_funs.term_from_mterm",
blanchet@35812
  1218
                                  [m])
blanchet@35812
  1219
            end
blanchet@35812
  1220
          | MApp (m1, m2) =>
blanchet@35812
  1221
            let
blanchet@35812
  1222
              val (t1, t2) = pairself (term_from_mterm Ts) (m1, m2)
blanchet@35812
  1223
              val (T1, T2) = pairself (curry fastype_of1 Ts) (t1, t2)
blanchet@35812
  1224
              val (t1', T2') =
blanchet@35812
  1225
                case T1 of
blanchet@35812
  1226
                  Type (s, [T11, T12]) => 
blanchet@35812
  1227
                  (if s = @{type_name fin_fun} then
blanchet@35812
  1228
                     select_nth_constr_arg thy stds (fin_fun_constr T11 T12) t1
blanchet@35812
  1229
                                           0 (T11 --> T12)
blanchet@35812
  1230
                   else
blanchet@35812
  1231
                     t1, T11)
blanchet@35812
  1232
                | _ => raise TYPE ("Nitpick_Mono.finitize_funs.term_from_mterm",
blanchet@35812
  1233
                                   [T1], [])
blanchet@35812
  1234
            in betapply (t1', coerce_term hol_ctxt Ts T2' T2 t2) end
blanchet@35812
  1235
          | MAbs (s, T, M, a, m') =>
blanchet@35812
  1236
            let
blanchet@35812
  1237
              val T = type_from_mtype T M
blanchet@35812
  1238
              val t' = term_from_mterm (T :: Ts) m'
blanchet@35812
  1239
              val T' = fastype_of1 (T :: Ts, t')
blanchet@35812
  1240
            in
blanchet@35812
  1241
              Abs (s, T, t')
blanchet@35812
  1242
              |> should_finitize (T --> T') a
blanchet@35812
  1243
                 ? construct_value thy stds (fin_fun_constr T T') o single
blanchet@35812
  1244
            end
blanchet@35812
  1245
      in
blanchet@35812
  1246
        Unsynchronized.change constr_cache (map (apsnd (map finitize_constr)));
blanchet@35812
  1247
        pairself (map (term_from_mterm [])) msp
blanchet@35812
  1248
      end
blanchet@35665
  1249
  | NONE => tsp
blanchet@33192
  1250
blanchet@33192
  1251
end;