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