src/HOL/Tools/Nitpick/nitpick_mono.ML
author blanchet
Mon Dec 06 13:18:25 2010 +0100 (2010-12-06)
changeset 40993 52ee2a187cdb
parent 40991 902ad76994d5
child 40994 3bdb8df0daf0
permissions -rw-r--r--
support 3 monotonicity calculi in one and fix soundness bug
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@40993
    14
    hol_context -> bool -> int -> typ -> term list * term list -> bool
blanchet@35665
    15
  val finitize_funs :
blanchet@40993
    16
    hol_context -> bool -> (typ option * bool option) list -> int -> 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@40993
   376
fun do_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@40993
   382
     | _ => do_annotation_atom_comp Eq [] aa2 aa1 accum)
blanchet@40993
   383
  | do_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@40993
   389
  | do_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@40993
   392
fun add_annotation_atom_comp cmp xs aa1 aa2
blanchet@40993
   393
                             ((asgs, comps, clauses) : constraint_set) =
blanchet@40993
   394
  (trace_msg (fn () => "*** Add " ^ string_for_annotation_atom aa1 ^ " " ^
blanchet@40993
   395
                       string_for_comp_op cmp ^ " " ^
blanchet@40993
   396
                       string_for_annotation_atom aa2);
blanchet@40993
   397
   case do_annotation_atom_comp cmp xs aa1 aa2 (asgs, comps) of
blanchet@40993
   398
     NONE => (trace_msg (K "**** Unsolvable"); raise UNSOLVABLE ())
blanchet@40993
   399
   | SOME (asgs, comps) => (asgs, comps, clauses))
blanchet@40993
   400
blanchet@35385
   401
fun do_mtype_comp _ _ _ _ NONE = NONE
blanchet@35385
   402
  | do_mtype_comp _ _ MAlpha MAlpha accum = accum
blanchet@40988
   403
  | do_mtype_comp Eq xs (MFun (M11, aa1, M12)) (MFun (M21, aa2, M22))
blanchet@33192
   404
                  (SOME accum) =
blanchet@40993
   405
     accum |> do_annotation_atom_comp Eq xs aa1 aa2
blanchet@40988
   406
           |> do_mtype_comp Eq xs M11 M21 |> do_mtype_comp Eq xs M12 M22
blanchet@40988
   407
  | do_mtype_comp Leq xs (MFun (M11, aa1, M12)) (MFun (M21, aa2, M22))
blanchet@33192
   408
                  (SOME accum) =
blanchet@35385
   409
    (if exists_alpha_sub_mtype M11 then
blanchet@40993
   410
       accum |> do_annotation_atom_comp Leq xs aa1 aa2
blanchet@35385
   411
             |> do_mtype_comp Leq xs M21 M11
blanchet@40988
   412
             |> (case aa2 of
blanchet@40985
   413
                   A Gen => I
blanchet@40989
   414
                 | A _ => do_mtype_comp Leq xs M11 M21
blanchet@35385
   415
                 | V x => do_mtype_comp Leq (x :: xs) M11 M21)
blanchet@33192
   416
     else
blanchet@33192
   417
       SOME accum)
blanchet@35385
   418
    |> do_mtype_comp Leq xs M12 M22
blanchet@35385
   419
  | do_mtype_comp cmp xs (M1 as MPair (M11, M12)) (M2 as MPair (M21, M22))
blanchet@33192
   420
                  accum =
blanchet@35385
   421
    (accum |> fold (uncurry (do_mtype_comp cmp xs)) [(M11, M21), (M12, M22)]
wenzelm@40722
   422
     handle ListPair.UnequalLengths =>
blanchet@35665
   423
            raise MTYPE ("Nitpick_Mono.do_mtype_comp", [M1, M2], []))
blanchet@35385
   424
  | do_mtype_comp _ _ (MType _) (MType _) accum =
blanchet@33192
   425
    accum (* no need to compare them thanks to the cache *)
blanchet@35665
   426
  | do_mtype_comp cmp _ M1 M2 _ =
blanchet@35665
   427
    raise MTYPE ("Nitpick_Mono.do_mtype_comp (" ^ string_for_comp_op cmp ^ ")",
blanchet@35665
   428
                 [M1, M2], [])
blanchet@33192
   429
blanchet@40990
   430
fun add_mtype_comp cmp M1 M2 ((asgs, comps, clauses) : constraint_set) =
blanchet@40991
   431
  (trace_msg (fn () => "*** Add " ^ string_for_mtype M1 ^ " " ^
blanchet@40991
   432
                       string_for_comp_op cmp ^ " " ^ string_for_mtype M2);
blanchet@40991
   433
   case do_mtype_comp cmp [] M1 M2 (SOME (asgs, comps)) of
blanchet@40991
   434
     NONE => (trace_msg (K "**** Unsolvable"); raise UNSOLVABLE ())
blanchet@40991
   435
   | SOME (asgs, comps) => (asgs, comps, clauses))
blanchet@33192
   436
blanchet@35385
   437
val add_mtypes_equal = add_mtype_comp Eq
blanchet@35385
   438
val add_is_sub_mtype = add_mtype_comp Leq
blanchet@33192
   439
blanchet@35385
   440
fun do_notin_mtype_fv _ _ _ NONE = NONE
blanchet@35385
   441
  | do_notin_mtype_fv Minus _ MAlpha accum = accum
blanchet@35385
   442
  | do_notin_mtype_fv Plus [] MAlpha _ = NONE
blanchet@40990
   443
  | do_notin_mtype_fv Plus [(x, a)] MAlpha (SOME (asgs, clauses)) =
blanchet@40991
   444
    SOME asgs |> add_assign_conjunct (x, a) |> Option.map (rpair clauses)
blanchet@40990
   445
  | do_notin_mtype_fv Plus clause MAlpha (SOME (asgs, clauses)) =
blanchet@40990
   446
    SOME (asgs, insert (op =) clause clauses)
blanchet@40993
   447
  | do_notin_mtype_fv sn clause (MFun (M1, A a, M2)) accum =
blanchet@40993
   448
    accum |> (if a <> Gen andalso sn = Plus then
blanchet@40990
   449
                do_notin_mtype_fv Plus clause M1
blanchet@40990
   450
              else
blanchet@40990
   451
                I)
blanchet@40993
   452
          |> (if a = Gen orelse sn = Plus then
blanchet@40990
   453
                do_notin_mtype_fv Minus clause M1
blanchet@40990
   454
              else
blanchet@40990
   455
                I)
blanchet@40990
   456
          |> do_notin_mtype_fv sn clause M2
blanchet@40990
   457
  | do_notin_mtype_fv Plus clause (MFun (M1, V x, M2)) accum =
blanchet@40991
   458
    accum |> (case add_assign_disjunct (x, Gen) (SOME clause) of
blanchet@33192
   459
                NONE => I
blanchet@40990
   460
              | SOME clause' => do_notin_mtype_fv Plus clause' M1)
blanchet@40990
   461
          |> do_notin_mtype_fv Minus clause M1
blanchet@40990
   462
          |> do_notin_mtype_fv Plus clause M2
blanchet@40990
   463
  | do_notin_mtype_fv Minus clause (MFun (M1, V x, M2)) accum =
blanchet@40993
   464
    accum |> (case fold (fn a => add_assign_disjunct (x, a)) [Fls, Tru]
blanchet@40990
   465
                        (SOME clause) of
blanchet@33192
   466
                NONE => I
blanchet@40990
   467
              | SOME clause' => do_notin_mtype_fv Plus clause' M1)
blanchet@40990
   468
          |> do_notin_mtype_fv Minus clause M2
blanchet@40990
   469
  | do_notin_mtype_fv sn clause (MPair (M1, M2)) accum =
blanchet@40990
   470
    accum |> fold (do_notin_mtype_fv sn clause) [M1, M2]
blanchet@40990
   471
  | do_notin_mtype_fv sn clause (MType (_, Ms)) accum =
blanchet@40990
   472
    accum |> fold (do_notin_mtype_fv sn clause) Ms
blanchet@35385
   473
  | do_notin_mtype_fv _ _ M _ =
blanchet@35665
   474
    raise MTYPE ("Nitpick_Mono.do_notin_mtype_fv", [M], [])
blanchet@33192
   475
blanchet@40990
   476
fun add_notin_mtype_fv sn M ((asgs, comps, clauses) : constraint_set) =
blanchet@40985
   477
  (trace_msg (fn () => "*** Add " ^ string_for_mtype M ^ " is " ^
blanchet@40985
   478
                       (case sn of Minus => "concrete" | Plus => "complete"));
blanchet@40990
   479
   case do_notin_mtype_fv sn [] M (SOME (asgs, clauses)) of
blanchet@40985
   480
     NONE => (trace_msg (K "**** Unsolvable"); raise UNSOLVABLE ())
blanchet@40990
   481
   | SOME (asgs, clauses) => (asgs, comps, clauses))
blanchet@33192
   482
blanchet@35665
   483
val add_mtype_is_concrete = add_notin_mtype_fv Minus
blanchet@35665
   484
val add_mtype_is_complete = add_notin_mtype_fv Plus
blanchet@33192
   485
blanchet@40986
   486
val bool_table =
blanchet@40986
   487
  [(Gen, (false, false)),
blanchet@40986
   488
   (New, (false, true)),
blanchet@40986
   489
   (Fls, (true, false)),
blanchet@40986
   490
   (Tru, (true, true))]
blanchet@40986
   491
blanchet@40993
   492
fun fst_var n = 2 * n
blanchet@40993
   493
fun snd_var n = 2 * n + 1
blanchet@40993
   494
blanchet@40986
   495
val bools_from_annotation = AList.lookup (op =) bool_table #> the
blanchet@40986
   496
val annotation_from_bools = AList.find (op =) bool_table #> the_single
blanchet@33192
   497
blanchet@40990
   498
fun prop_for_bool b = if b then PL.True else PL.False
blanchet@40990
   499
fun prop_for_bool_var_equality (v1, v2) =
blanchet@40990
   500
  PL.And (PL.Or (PL.BoolVar v1, PL.Not (PL.BoolVar v2)),
blanchet@40990
   501
          PL.Or (PL.Not (PL.BoolVar v1), PL.BoolVar v2))
blanchet@40990
   502
fun prop_for_assign (x, a) =
blanchet@40986
   503
  let val (b1, b2) = bools_from_annotation a in
blanchet@40990
   504
    PL.And (PL.BoolVar (fst_var x) |> not b1 ? PL.Not,
blanchet@40990
   505
            PL.BoolVar (snd_var x) |> not b2 ? PL.Not)
blanchet@40986
   506
  end
blanchet@40990
   507
fun prop_for_atom_assign (A a', a) = prop_for_bool (a = a')
blanchet@40990
   508
  | prop_for_atom_assign (V x, a) = prop_for_assign (x, a)
blanchet@40990
   509
fun prop_for_atom_equality (aa1, A a2) = prop_for_atom_assign (aa1, a2)
blanchet@40990
   510
  | prop_for_atom_equality (A a1, aa2) = prop_for_atom_assign (aa2, a1)
blanchet@40990
   511
  | prop_for_atom_equality (V x1, V x2) =
blanchet@40990
   512
    PL.And (prop_for_bool_var_equality (pairself fst_var (x1, x2)),
blanchet@40990
   513
            prop_for_bool_var_equality (pairself snd_var (x1, x2)))
blanchet@40990
   514
val prop_for_assign_clause = PL.exists o map prop_for_assign
blanchet@40990
   515
fun prop_for_exists_var_assign xs a =
blanchet@40990
   516
  PL.exists (map (fn x => prop_for_assign (x, a)) xs)
blanchet@40988
   517
fun prop_for_comp (aa1, aa2, Eq, []) =
blanchet@40990
   518
    PL.SAnd (prop_for_comp (aa1, aa2, Leq, []),
blanchet@40990
   519
             prop_for_comp (aa2, aa1, Leq, []))
blanchet@40988
   520
  | prop_for_comp (aa1, aa2, Leq, []) =
blanchet@40990
   521
    PL.SOr (prop_for_atom_equality (aa1, aa2), prop_for_atom_assign (aa2, Gen))
blanchet@40988
   522
  | prop_for_comp (aa1, aa2, cmp, xs) =
blanchet@40990
   523
    PL.SOr (prop_for_exists_var_assign xs Gen,
blanchet@40990
   524
            prop_for_comp (aa1, aa2, cmp, []))
blanchet@33192
   525
blanchet@40993
   526
(* The "calculus" parameter may be 1, 2, or 3, corresponding approximately to
blanchet@40993
   527
   the M1, M2, and M3 calculi from Blanchette & Krauss 2011. *)
blanchet@40993
   528
fun variable_domain calculus =
blanchet@40993
   529
  [Gen] @ (if calculus > 1 then [Fls] else [])
blanchet@40993
   530
  @ (if calculus > 2 then [New, Tru] else [])
blanchet@40993
   531
blanchet@40993
   532
fun prop_for_variable_domain calculus x =
blanchet@40993
   533
  PL.exists (map (curry prop_for_assign x) (variable_domain calculus))
blanchet@40986
   534
blanchet@40990
   535
fun extract_assigns max_var assigns asgs =
blanchet@33192
   536
  fold (fn x => fn accum =>
blanchet@40989
   537
           if AList.defined (op =) asgs x then
blanchet@33192
   538
             accum
blanchet@40986
   539
           else case (fst_var x, snd_var x) |> pairself assigns of
blanchet@40986
   540
             (NONE, NONE) => accum
blanchet@40993
   541
           | bp => (x, annotation_from_bools (pairself (the_default false) bp))
blanchet@40993
   542
                   :: accum)
blanchet@40989
   543
       (max_var downto 1) asgs
blanchet@33192
   544
blanchet@40990
   545
fun print_problem asgs comps clauses =
blanchet@38179
   546
  trace_msg (fn () => "*** Problem:\n" ^
blanchet@40990
   547
                      cat_lines (map string_for_assign asgs @
blanchet@38179
   548
                                 map string_for_comp comps @
blanchet@40990
   549
                                 map string_for_assign_clause clauses))
blanchet@33192
   550
blanchet@40989
   551
fun print_solution asgs =
blanchet@40989
   552
  trace_msg (fn () => "*** Solution:\n" ^
blanchet@40989
   553
      (asgs
blanchet@40989
   554
       |> map swap
blanchet@40989
   555
       |> AList.group (op =)
blanchet@40989
   556
       |> map (fn (a, xs) => string_for_annotation a ^ ": " ^
blanchet@40989
   557
                             string_for_vars ", " xs)
blanchet@40989
   558
       |> space_implode "\n"))
blanchet@33192
   559
blanchet@40993
   560
fun solve calculus max_var (asgs, comps, clauses) =
blanchet@40146
   561
  let
blanchet@40146
   562
    fun do_assigns assigns =
blanchet@40990
   563
      SOME (extract_assigns max_var assigns asgs |> tap print_solution)
blanchet@40990
   564
    val _ = print_problem asgs comps clauses
blanchet@40993
   565
    val prop =
blanchet@40993
   566
      map prop_for_assign asgs @
blanchet@40993
   567
      map prop_for_comp comps @
blanchet@40993
   568
      map prop_for_assign_clause clauses @
blanchet@40993
   569
      (if calculus < 3 then
blanchet@40993
   570
         map (prop_for_variable_domain calculus) (1 upto max_var)
blanchet@40993
   571
       else
blanchet@40993
   572
         [])
blanchet@40993
   573
      |> PL.all
blanchet@40146
   574
  in
blanchet@40990
   575
    if PL.eval (K false) prop then
blanchet@40990
   576
      do_assigns (K (SOME false))
blanchet@40990
   577
    else if PL.eval (K true) prop then
blanchet@40990
   578
      do_assigns (K (SOME true))
blanchet@40146
   579
    else
blanchet@40146
   580
      let
blanchet@40146
   581
        (* use the first ML solver (to avoid startup overhead) *)
blanchet@40146
   582
        val solvers = !SatSolver.solvers
blanchet@40146
   583
                      |> filter (member (op =) ["dptsat", "dpll"] o fst)
blanchet@40146
   584
      in
blanchet@40146
   585
        case snd (hd solvers) prop of
blanchet@40146
   586
          SatSolver.SATISFIABLE assigns => do_assigns assigns
blanchet@40146
   587
        | _ => NONE
blanchet@40146
   588
      end
blanchet@40146
   589
  end
blanchet@33192
   590
blanchet@35385
   591
type mtype_schema = mtyp * constraint_set
blanchet@35385
   592
type mtype_context =
blanchet@35665
   593
  {bound_Ts: typ list,
blanchet@35665
   594
   bound_Ms: mtyp list,
blanchet@40987
   595
   bound_frame: (int * annotation_atom) list,
blanchet@35385
   596
   frees: (styp * mtyp) list,
blanchet@35385
   597
   consts: (styp * mtyp) list}
blanchet@33192
   598
blanchet@35385
   599
type accumulator = mtype_context * constraint_set
blanchet@33192
   600
blanchet@40987
   601
val initial_gamma =
blanchet@40987
   602
  {bound_Ts = [], bound_Ms = [], bound_frame = [], frees = [], consts = []}
blanchet@33192
   603
blanchet@40993
   604
fun push_bound aa T M {bound_Ts, bound_Ms, bound_frame, frees, consts} =
blanchet@40987
   605
  {bound_Ts = T :: bound_Ts, bound_Ms = M :: bound_Ms,
blanchet@40993
   606
   bound_frame = (length bound_Ts, aa) :: bound_frame, frees = frees,
blanchet@40993
   607
   consts = consts}
blanchet@40987
   608
fun pop_bound {bound_Ts, bound_Ms, bound_frame, frees, consts} =
blanchet@40993
   609
  {bound_Ts = tl bound_Ts, bound_Ms = tl bound_Ms,
blanchet@40993
   610
   bound_frame = bound_frame
blanchet@40993
   611
                 |> filter_out (fn (depth, _) => depth = length bound_Ts - 1),
blanchet@40987
   612
   frees = frees, consts = consts}
blanchet@35665
   613
  handle List.Empty => initial_gamma (* FIXME: needed? *)
blanchet@33192
   614
blanchet@39359
   615
fun consider_term (mdata as {hol_ctxt = {thy, ctxt, stds, ...}, alpha_T,
blanchet@39359
   616
                             max_fresh, ...}) =
blanchet@33192
   617
  let
blanchet@37476
   618
    fun is_enough_eta_expanded t =
blanchet@37476
   619
      case strip_comb t of
blanchet@39359
   620
        (Const x, ts) => the_default 0 (arity_of_built_in_const thy stds x)
blanchet@37476
   621
        <= length ts
blanchet@37476
   622
      | _ => true
blanchet@35672
   623
    val mtype_for = fresh_mtype_for_type mdata false
blanchet@35665
   624
    fun plus_set_mtype_for_dom M =
blanchet@40985
   625
      MFun (M, A (if exists_alpha_sub_mtype M then Fls else Gen), bool_M)
blanchet@35385
   626
    fun do_all T (gamma, cset) =
blanchet@33192
   627
      let
blanchet@35385
   628
        val abs_M = mtype_for (domain_type (domain_type T))
blanchet@35386
   629
        val body_M = mtype_for (body_type T)
blanchet@33192
   630
      in
blanchet@40985
   631
        (MFun (MFun (abs_M, A Gen, body_M), A Gen, body_M),
blanchet@35665
   632
         (gamma, cset |> add_mtype_is_complete abs_M))
blanchet@33192
   633
      end
blanchet@33192
   634
    fun do_equals T (gamma, cset) =
blanchet@35385
   635
      let val M = mtype_for (domain_type T) in
blanchet@40985
   636
        (MFun (M, A Gen, MFun (M, V (Unsynchronized.inc max_fresh),
blanchet@40985
   637
                               mtype_for (nth_range_type 2 T))),
blanchet@35665
   638
         (gamma, cset |> add_mtype_is_concrete M))
blanchet@33192
   639
      end
blanchet@33192
   640
    fun do_robust_set_operation T (gamma, cset) =
blanchet@33192
   641
      let
blanchet@33192
   642
        val set_T = domain_type T
blanchet@35385
   643
        val M1 = mtype_for set_T
blanchet@35385
   644
        val M2 = mtype_for set_T
blanchet@35385
   645
        val M3 = mtype_for set_T
blanchet@33192
   646
      in
blanchet@40985
   647
        (MFun (M1, A Gen, MFun (M2, A Gen, M3)),
blanchet@35385
   648
         (gamma, cset |> add_is_sub_mtype M1 M3 |> add_is_sub_mtype M2 M3))
blanchet@33192
   649
      end
blanchet@33192
   650
    fun do_fragile_set_operation T (gamma, cset) =
blanchet@33192
   651
      let
blanchet@33192
   652
        val set_T = domain_type T
blanchet@35385
   653
        val set_M = mtype_for set_T
blanchet@35665
   654
        fun custom_mtype_for (T as Type (@{type_name fun}, [T1, T2])) =
blanchet@35385
   655
            if T = set_T then set_M
blanchet@40985
   656
            else MFun (custom_mtype_for T1, A Gen, custom_mtype_for T2)
blanchet@35385
   657
          | custom_mtype_for T = mtype_for T
blanchet@33192
   658
      in
blanchet@35665
   659
        (custom_mtype_for T, (gamma, cset |> add_mtype_is_concrete set_M))
blanchet@33192
   660
      end
blanchet@33192
   661
    fun do_pair_constr T accum =
blanchet@35385
   662
      case mtype_for (nth_range_type 2 T) of
blanchet@35385
   663
        M as MPair (a_M, b_M) =>
blanchet@40985
   664
        (MFun (a_M, A Gen, MFun (b_M, A Gen, M)), accum)
blanchet@35665
   665
      | M => raise MTYPE ("Nitpick_Mono.consider_term.do_pair_constr", [M], [])
blanchet@33192
   666
    fun do_nth_pair_sel n T =
blanchet@35385
   667
      case mtype_for (domain_type T) of
blanchet@35385
   668
        M as MPair (a_M, b_M) =>
blanchet@40985
   669
        pair (MFun (M, A Gen, if n = 0 then a_M else b_M))
blanchet@35665
   670
      | M => raise MTYPE ("Nitpick_Mono.consider_term.do_nth_pair_sel", [M], [])
blanchet@35385
   671
    fun do_bounded_quantifier t0 abs_s abs_T connective_t bound_t body_t accum =
blanchet@33192
   672
      let
blanchet@35385
   673
        val abs_M = mtype_for abs_T
blanchet@40993
   674
        val aa = V (Unsynchronized.inc max_fresh)
blanchet@35665
   675
        val (bound_m, accum) =
blanchet@40993
   676
          accum |>> push_bound aa abs_T abs_M |> do_term bound_t
blanchet@35665
   677
        val expected_bound_M = plus_set_mtype_for_dom abs_M
blanchet@35385
   678
        val (body_m, accum) =
blanchet@35385
   679
          accum ||> add_mtypes_equal expected_bound_M (mtype_of_mterm bound_m)
blanchet@35385
   680
                |> do_term body_t ||> apfst pop_bound
blanchet@35385
   681
        val bound_M = mtype_of_mterm bound_m
blanchet@40993
   682
        val (M1, aa', _) = dest_MFun bound_M
blanchet@33192
   683
      in
blanchet@40993
   684
        (MApp (MRaw (t0, MFun (bound_M, aa, bool_M)),
blanchet@40993
   685
               MAbs (abs_s, abs_T, M1, aa',
blanchet@35386
   686
                     MApp (MApp (MRaw (connective_t,
blanchet@35386
   687
                                       mtype_for (fastype_of connective_t)),
blanchet@35386
   688
                                 MApp (bound_m, MRaw (Bound 0, M1))),
blanchet@35385
   689
                           body_m))), accum)
blanchet@33192
   690
      end
blanchet@40987
   691
    and do_term t (accum as ({bound_Ts, bound_Ms, bound_frame, frees, consts},
blanchet@40987
   692
                             cset)) =
blanchet@40993
   693
      (trace_msg (fn () => "  \<Gamma> \<turnstile> " ^
blanchet@40993
   694
                           Syntax.string_of_term ctxt t ^ " : _?");
blanchet@40993
   695
       case t of
blanchet@40993
   696
         Const (x as (s, T)) =>
blanchet@40993
   697
         (case AList.lookup (op =) consts x of
blanchet@40993
   698
            SOME M => (M, accum)
blanchet@40993
   699
          | NONE =>
blanchet@40993
   700
            case s of
blanchet@40993
   701
              @{const_name False} =>
blanchet@40993
   702
              (bool_M, accum ||> fold (add_annotation_atom_comp Leq [] (A Fls))
blanchet@40993
   703
                                      (map snd bound_frame))
blanchet@40993
   704
            | @{const_name True} =>
blanchet@40993
   705
              (bool_M, accum ||> fold (add_annotation_atom_comp Leq [] (A Tru))
blanchet@40993
   706
                                      (map snd bound_frame))
blanchet@40993
   707
            | _ =>
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
haftmann@38864
   723
              | @{const_name HOL.eq} => do_equals T accum
blanchet@37476
   724
              | @{const_name The} =>
blanchet@38179
   725
                (trace_msg (K "*** The"); raise UNSOLVABLE ())
blanchet@37476
   726
              | @{const_name Eps} =>
blanchet@38179
   727
                (trace_msg (K "*** Eps"); raise UNSOLVABLE ())
blanchet@33192
   728
              | @{const_name If} =>
blanchet@33192
   729
                do_robust_set_operation (range_type T) accum
blanchet@40985
   730
                |>> curry3 MFun bool_M (A Gen)
blanchet@33192
   731
              | @{const_name Pair} => do_pair_constr T accum
blanchet@33192
   732
              | @{const_name fst} => do_nth_pair_sel 0 T accum
blanchet@39316
   733
              | @{const_name snd} => do_nth_pair_sel 1 T accum
blanchet@33192
   734
              | @{const_name Id} =>
blanchet@40985
   735
                (MFun (mtype_for (domain_type T), A Gen, bool_M), accum)
blanchet@33192
   736
              | @{const_name converse} =>
blanchet@33192
   737
                let
blanchet@33192
   738
                  val x = Unsynchronized.inc max_fresh
blanchet@35385
   739
                  fun mtype_for_set T =
blanchet@35385
   740
                    MFun (mtype_for (domain_type T), V x, bool_M)
blanchet@35385
   741
                  val ab_set_M = domain_type T |> mtype_for_set
blanchet@35385
   742
                  val ba_set_M = range_type T |> mtype_for_set
blanchet@40985
   743
                in (MFun (ab_set_M, A Gen, ba_set_M), accum) end
blanchet@33192
   744
              | @{const_name trancl} => do_fragile_set_operation T accum
blanchet@33192
   745
              | @{const_name rel_comp} =>
blanchet@33192
   746
                let
blanchet@33192
   747
                  val x = Unsynchronized.inc max_fresh
blanchet@35385
   748
                  fun mtype_for_set T =
blanchet@35385
   749
                    MFun (mtype_for (domain_type T), V x, bool_M)
blanchet@35385
   750
                  val bc_set_M = domain_type T |> mtype_for_set
blanchet@35385
   751
                  val ab_set_M = domain_type (range_type T) |> mtype_for_set
blanchet@35385
   752
                  val ac_set_M = nth_range_type 2 T |> mtype_for_set
blanchet@33192
   753
                in
blanchet@40985
   754
                  (MFun (bc_set_M, A Gen, MFun (ab_set_M, A Gen, ac_set_M)),
blanchet@33192
   755
                   accum)
blanchet@33192
   756
                end
blanchet@33192
   757
              | @{const_name image} =>
blanchet@33192
   758
                let
blanchet@35385
   759
                  val a_M = mtype_for (domain_type (domain_type T))
blanchet@35385
   760
                  val b_M = mtype_for (range_type (domain_type T))
blanchet@33192
   761
                in
blanchet@40985
   762
                  (MFun (MFun (a_M, A Gen, b_M), A Gen,
blanchet@40985
   763
                         MFun (plus_set_mtype_for_dom a_M, A Gen,
blanchet@35665
   764
                               plus_set_mtype_for_dom b_M)), accum)
blanchet@33192
   765
                end
blanchet@35672
   766
              | @{const_name finite} =>
blanchet@35672
   767
                let val M1 = mtype_for (domain_type (domain_type T)) in
blanchet@40985
   768
                  (MFun (plus_set_mtype_for_dom M1, A Gen, bool_M), accum)
blanchet@35672
   769
                end
blanchet@33192
   770
              | @{const_name Sigma} =>
blanchet@33192
   771
                let
blanchet@33192
   772
                  val x = Unsynchronized.inc max_fresh
blanchet@35385
   773
                  fun mtype_for_set T =
blanchet@35385
   774
                    MFun (mtype_for (domain_type T), V x, bool_M)
blanchet@33192
   775
                  val a_set_T = domain_type T
blanchet@35385
   776
                  val a_M = mtype_for (domain_type a_set_T)
blanchet@40993
   777
                  val b_set_M =
blanchet@40993
   778
                    mtype_for_set (range_type (domain_type (range_type T)))
blanchet@35385
   779
                  val a_set_M = mtype_for_set a_set_T
blanchet@40985
   780
                  val a_to_b_set_M = MFun (a_M, A Gen, b_set_M)
blanchet@35385
   781
                  val ab_set_M = mtype_for_set (nth_range_type 2 T)
blanchet@33192
   782
                in
blanchet@40985
   783
                  (MFun (a_set_M, A Gen, MFun (a_to_b_set_M, A Gen, ab_set_M)),
blanchet@40985
   784
                   accum)
blanchet@33192
   785
                end
blanchet@35220
   786
              | _ =>
blanchet@39360
   787
                if s = @{const_name safe_The} then
blanchet@35671
   788
                  let
blanchet@35671
   789
                    val a_set_M = mtype_for (domain_type T)
blanchet@35671
   790
                    val a_M = dest_MFun a_set_M |> #1
blanchet@40985
   791
                  in (MFun (a_set_M, A Gen, a_M), accum) end
blanchet@35220
   792
                else if s = @{const_name ord_class.less_eq} andalso
blanchet@35220
   793
                        is_set_type (domain_type T) then
blanchet@35220
   794
                  do_fragile_set_operation T accum
blanchet@35220
   795
                else if is_sel s then
blanchet@35665
   796
                  (mtype_for_sel mdata x, accum)
blanchet@37256
   797
                else if is_constr ctxt stds x then
blanchet@35385
   798
                  (mtype_for_constr mdata x, accum)
blanchet@39359
   799
                else if is_built_in_const thy stds x then
blanchet@35672
   800
                  (fresh_mtype_for_type mdata true T, accum)
blanchet@35220
   801
                else
blanchet@35385
   802
                  let val M = mtype_for T in
blanchet@35665
   803
                    (M, ({bound_Ts = bound_Ts, bound_Ms = bound_Ms,
blanchet@40987
   804
                          bound_frame = bound_frame, frees = frees,
blanchet@40987
   805
                          consts = (x, M) :: consts}, cset))
blanchet@35386
   806
                  end) |>> curry MRaw t
blanchet@33192
   807
         | Free (x as (_, T)) =>
blanchet@33192
   808
           (case AList.lookup (op =) frees x of
blanchet@35385
   809
              SOME M => (M, accum)
blanchet@33192
   810
            | NONE =>
blanchet@35385
   811
              let val M = mtype_for T in
blanchet@35665
   812
                (M, ({bound_Ts = bound_Ts, bound_Ms = bound_Ms,
blanchet@40987
   813
                      bound_frame = bound_frame, frees = (x, M) :: frees,
blanchet@40987
   814
                      consts = consts}, cset))
blanchet@35386
   815
              end) |>> curry MRaw t
blanchet@38179
   816
         | Var _ => (trace_msg (K "*** Var"); raise UNSOLVABLE ())
blanchet@35665
   817
         | Bound j => (MRaw (t, nth bound_Ms j), accum)
blanchet@35385
   818
         | Abs (s, T, t') =>
blanchet@35665
   819
           (case fin_fun_body T (fastype_of1 (T :: bound_Ts, t')) t' of
blanchet@35665
   820
              SOME t' =>
blanchet@35665
   821
              let
blanchet@35665
   822
                val M = mtype_for T
blanchet@40993
   823
                val (m', accum) = do_term t' (accum |>> push_bound (A Fls) T M)
blanchet@40993
   824
              in (MAbs (s, T, M, A Fls, m'), accum |>> pop_bound) end
blanchet@35665
   825
            | NONE =>
blanchet@35665
   826
              ((case t' of
blanchet@35665
   827
                  t1' $ Bound 0 =>
blanchet@37476
   828
                  if not (loose_bvar1 (t1', 0)) andalso
blanchet@37476
   829
                     is_enough_eta_expanded t1' then
blanchet@35665
   830
                    do_term (incr_boundvars ~1 t1') accum
blanchet@35665
   831
                  else
blanchet@35665
   832
                    raise SAME ()
haftmann@38864
   833
                | (t11 as Const (@{const_name HOL.eq}, _)) $ Bound 0 $ t13 =>
blanchet@37476
   834
                  if not (loose_bvar1 (t13, 0)) then
blanchet@37476
   835
                    do_term (incr_boundvars ~1 (t11 $ t13)) accum
blanchet@37476
   836
                  else
blanchet@37476
   837
                    raise SAME ()
blanchet@35665
   838
                | _ => raise SAME ())
blanchet@35665
   839
               handle SAME () =>
blanchet@35665
   840
                      let
blanchet@35665
   841
                        val M = mtype_for T
blanchet@40993
   842
                        val aa = V (Unsynchronized.inc max_fresh)
blanchet@40993
   843
                        val (m', accum) =
blanchet@40993
   844
                          do_term t' (accum |>> push_bound aa T M)
blanchet@40993
   845
                      in (MAbs (s, T, M, aa, m'), accum |>> pop_bound) end))
blanchet@35385
   846
         | (t0 as Const (@{const_name All}, _))
blanchet@40993
   847
           $ Abs (s', T', (t10 as @{const HOL.implies})
blanchet@40993
   848
                          $ (t11 $ Bound 0) $ t12) =>
blanchet@35385
   849
           do_bounded_quantifier t0 s' T' t10 t11 t12 accum
blanchet@35385
   850
         | (t0 as Const (@{const_name Ex}, _))
blanchet@40993
   851
           $ Abs (s', T', (t10 as @{const HOL.conj})
blanchet@40993
   852
                          $ (t11 $ Bound 0) $ t12) =>
blanchet@35385
   853
           do_bounded_quantifier t0 s' T' t10 t11 t12 accum
blanchet@33192
   854
         | Const (@{const_name Let}, _) $ t1 $ t2 =>
blanchet@33192
   855
           do_term (betapply (t2, t1)) accum
blanchet@33192
   856
         | t1 $ t2 =>
blanchet@33192
   857
           let
blanchet@35385
   858
             val (m1, accum) = do_term t1 accum
blanchet@35385
   859
             val (m2, accum) = do_term t2 accum
blanchet@33192
   860
           in
blanchet@35812
   861
             let
blanchet@35812
   862
               val M11 = mtype_of_mterm m1 |> dest_MFun |> #1
blanchet@35812
   863
               val M2 = mtype_of_mterm m2
blanchet@35812
   864
             in (MApp (m1, m2), accum ||> add_is_sub_mtype M2 M11) end
blanchet@33192
   865
           end)
blanchet@38179
   866
        |> tap (fn (m, _) => trace_msg (fn () => "  \<Gamma> \<turnstile> " ^
blanchet@38179
   867
                                                 string_for_mterm ctxt m))
blanchet@33192
   868
  in do_term end
blanchet@33192
   869
blanchet@35665
   870
fun force_minus_funs 0 _ = I
blanchet@35665
   871
  | force_minus_funs n (M as MFun (M1, _, M2)) =
blanchet@40985
   872
    add_mtypes_equal M (MFun (M1, A Gen, M2)) #> force_minus_funs (n - 1) M2
blanchet@35665
   873
  | force_minus_funs _ M =
blanchet@35665
   874
    raise MTYPE ("Nitpick_Mono.force_minus_funs", [M], [])
blanchet@35665
   875
fun consider_general_equals mdata def (x as (_, T)) t1 t2 accum =
blanchet@35386
   876
  let
blanchet@35386
   877
    val (m1, accum) = consider_term mdata t1 accum
blanchet@35386
   878
    val (m2, accum) = consider_term mdata t2 accum
blanchet@35386
   879
    val M1 = mtype_of_mterm m1
blanchet@35386
   880
    val M2 = mtype_of_mterm m2
blanchet@35665
   881
    val accum = accum ||> add_mtypes_equal M1 M2
blanchet@35672
   882
    val body_M = fresh_mtype_for_type mdata false (nth_range_type 2 T)
blanchet@35665
   883
    val m = MApp (MApp (MRaw (Const x,
blanchet@40985
   884
                           MFun (M1, A Gen, MFun (M2, A Gen, body_M))), m1), m2)
blanchet@35386
   885
  in
blanchet@35665
   886
    (m, if def then
blanchet@35665
   887
          let val (head_m, arg_ms) = strip_mcomb m1 in
blanchet@35665
   888
            accum ||> force_minus_funs (length arg_ms) (mtype_of_mterm head_m)
blanchet@35665
   889
          end
blanchet@35665
   890
        else
blanchet@35665
   891
          accum)
blanchet@35386
   892
  end
blanchet@35386
   893
blanchet@35385
   894
fun consider_general_formula (mdata as {hol_ctxt = {ctxt, ...}, ...}) =
blanchet@33192
   895
  let
blanchet@35672
   896
    val mtype_for = fresh_mtype_for_type mdata false
blanchet@35386
   897
    val do_term = consider_term mdata
blanchet@35812
   898
    fun do_formula sn t accum =
blanchet@33192
   899
        let
blanchet@35386
   900
          fun do_quantifier (quant_x as (quant_s, _)) abs_s abs_T body_t =
blanchet@33192
   901
            let
blanchet@35385
   902
              val abs_M = mtype_for abs_T
blanchet@40993
   903
              val a = Gen (* FIXME: strengthen *)
blanchet@34982
   904
              val side_cond = ((sn = Minus) = (quant_s = @{const_name Ex}))
blanchet@35386
   905
              val (body_m, accum) =
blanchet@35665
   906
                accum ||> side_cond ? add_mtype_is_complete abs_M
blanchet@40993
   907
                      |>> push_bound (A a) abs_T abs_M |> do_formula sn body_t
blanchet@35386
   908
              val body_M = mtype_of_mterm body_m
blanchet@33192
   909
            in
blanchet@35665
   910
              (MApp (MRaw (Const quant_x,
blanchet@40985
   911
                           MFun (MFun (abs_M, A Gen, body_M), A Gen, body_M)),
blanchet@40985
   912
                     MAbs (abs_s, abs_T, abs_M, A Gen, body_m)),
blanchet@35386
   913
               accum |>> pop_bound)
blanchet@33192
   914
            end
blanchet@35386
   915
          fun do_equals x t1 t2 =
blanchet@33192
   916
            case sn of
blanchet@35386
   917
              Plus => do_term t accum
blanchet@35665
   918
            | Minus => consider_general_equals mdata false x t1 t2 accum
blanchet@33192
   919
        in
blanchet@38179
   920
          (trace_msg (fn () => "  \<Gamma> \<turnstile> " ^
blanchet@38179
   921
                               Syntax.string_of_term ctxt t ^ " : o\<^sup>" ^
blanchet@38179
   922
                               string_for_sign sn ^ "?");
blanchet@37704
   923
           case t of
blanchet@37704
   924
             Const (x as (@{const_name all}, _)) $ Abs (s1, T1, t1) =>
blanchet@37704
   925
             do_quantifier x s1 T1 t1
blanchet@37704
   926
           | Const (x as (@{const_name "=="}, _)) $ t1 $ t2 => do_equals x t1 t2
blanchet@37704
   927
           | @{const Trueprop} $ t1 =>
blanchet@37704
   928
             let val (m1, accum) = do_formula sn t1 accum in
blanchet@37704
   929
               (MApp (MRaw (@{const Trueprop}, mtype_for (bool_T --> prop_T)),
blanchet@37704
   930
                      m1), accum)
blanchet@37704
   931
             end
blanchet@37704
   932
           | @{const Not} $ t1 =>
blanchet@40985
   933
             let val (m1, accum) = do_formula (negate_sign sn) t1 accum in
blanchet@37704
   934
               (MApp (MRaw (@{const Not}, mtype_for (bool_T --> bool_T)), m1),
blanchet@37704
   935
                accum)
blanchet@37704
   936
             end
blanchet@37704
   937
           | Const (x as (@{const_name All}, _)) $ Abs (s1, T1, t1) =>
blanchet@37704
   938
             do_quantifier x s1 T1 t1
blanchet@39345
   939
           | Const (x0 as (@{const_name Ex}, T0))
blanchet@37704
   940
             $ (t1 as Abs (s1, T1, t1')) =>
blanchet@37704
   941
             (case sn of
blanchet@37704
   942
                Plus => do_quantifier x0 s1 T1 t1'
blanchet@37704
   943
              | Minus =>
blanchet@37704
   944
                (* FIXME: Move elsewhere *)
blanchet@37704
   945
                do_term (@{const Not}
blanchet@37704
   946
                         $ (HOLogic.eq_const (domain_type T0) $ t1
blanchet@37704
   947
                            $ Abs (Name.uu, T1, @{const False}))) accum)
haftmann@38864
   948
           | Const (x as (@{const_name HOL.eq}, _)) $ t1 $ t2 =>
blanchet@37704
   949
             do_equals x t1 t2
blanchet@37704
   950
           | Const (@{const_name Let}, _) $ t1 $ t2 =>
blanchet@37704
   951
             do_formula sn (betapply (t2, t1)) accum
blanchet@37704
   952
           | (t0 as Const (s0, _)) $ t1 $ t2 =>
blanchet@38199
   953
             if s0 = @{const_name "==>"} orelse
blanchet@38199
   954
                s0 = @{const_name Pure.conjunction} orelse
haftmann@38795
   955
                s0 = @{const_name HOL.conj} orelse
haftmann@38795
   956
                s0 = @{const_name HOL.disj} orelse
haftmann@38786
   957
                s0 = @{const_name HOL.implies} then
blanchet@37704
   958
               let
blanchet@37704
   959
                 val impl = (s0 = @{const_name "==>"} orelse
haftmann@38786
   960
                             s0 = @{const_name HOL.implies})
blanchet@40985
   961
                 val (m1, accum) =
blanchet@40985
   962
                   do_formula (sn |> impl ? negate_sign) t1 accum
blanchet@37704
   963
                 val (m2, accum) = do_formula sn t2 accum
blanchet@37704
   964
               in
blanchet@37704
   965
                 (MApp (MApp (MRaw (t0, mtype_for (fastype_of t0)), m1), m2),
blanchet@37704
   966
                  accum)
blanchet@39316
   967
               end
blanchet@37704
   968
             else
blanchet@37704
   969
               do_term t accum
blanchet@37704
   970
           | _ => do_term t accum)
blanchet@33192
   971
        end
blanchet@35386
   972
        |> tap (fn (m, _) =>
blanchet@38179
   973
                   trace_msg (fn () => "\<Gamma> \<turnstile> " ^
blanchet@38179
   974
                                       string_for_mterm ctxt m ^ " : o\<^sup>" ^
blanchet@38179
   975
                                       string_for_sign sn))
blanchet@33192
   976
  in do_formula end
blanchet@33192
   977
blanchet@33192
   978
(* The harmless axiom optimization below is somewhat too aggressive in the face
blanchet@33192
   979
   of (rather peculiar) user-defined axioms. *)
blanchet@33192
   980
val harmless_consts =
blanchet@33192
   981
  [@{const_name ord_class.less}, @{const_name ord_class.less_eq}]
blanchet@33192
   982
val bounteous_consts = [@{const_name bisim}]
blanchet@33192
   983
blanchet@35665
   984
fun is_harmless_axiom ({no_harmless = true, ...} : mdata) _ = false
blanchet@39359
   985
  | is_harmless_axiom {hol_ctxt = {thy, stds, ...}, ...} t =
blanchet@35665
   986
    Term.add_consts t []
blanchet@39359
   987
    |> filter_out (is_built_in_const thy stds)
blanchet@35665
   988
    |> (forall (member (op =) harmless_consts o original_name o fst) orf
blanchet@35665
   989
        exists (member (op =) bounteous_consts o fst))
blanchet@33192
   990
blanchet@35665
   991
fun consider_nondefinitional_axiom mdata t =
blanchet@35665
   992
  if is_harmless_axiom mdata t then pair (MRaw (t, dummy_M))
blanchet@35386
   993
  else consider_general_formula mdata Plus t
blanchet@33192
   994
blanchet@37256
   995
fun consider_definitional_axiom (mdata as {hol_ctxt = {ctxt, ...}, ...}) t =
blanchet@37256
   996
  if not (is_constr_pattern_formula ctxt t) then
blanchet@35386
   997
    consider_nondefinitional_axiom mdata t
blanchet@35665
   998
  else if is_harmless_axiom mdata t then
blanchet@35386
   999
    pair (MRaw (t, dummy_M))
blanchet@33192
  1000
  else
blanchet@33192
  1001
    let
blanchet@35672
  1002
      val mtype_for = fresh_mtype_for_type mdata false
blanchet@35386
  1003
      val do_term = consider_term mdata
blanchet@35386
  1004
      fun do_all quant_t abs_s abs_T body_t accum =
blanchet@35386
  1005
        let
blanchet@35386
  1006
          val abs_M = mtype_for abs_T
blanchet@35386
  1007
          val (body_m, accum) =
blanchet@40993
  1008
            accum |>> push_bound (A Gen) abs_T abs_M |> do_formula body_t
blanchet@35386
  1009
          val body_M = mtype_of_mterm body_m
blanchet@35386
  1010
        in
blanchet@40985
  1011
          (MApp (MRaw (quant_t, MFun (MFun (abs_M, A Gen, body_M), A Gen,
blanchet@40985
  1012
                       body_M)),
blanchet@40985
  1013
                 MAbs (abs_s, abs_T, abs_M, A Gen, body_m)),
blanchet@35386
  1014
           accum |>> pop_bound)
blanchet@33192
  1015
        end
blanchet@35386
  1016
      and do_conjunction t0 t1 t2 accum =
blanchet@33192
  1017
        let
blanchet@35386
  1018
          val (m1, accum) = do_formula t1 accum
blanchet@35386
  1019
          val (m2, accum) = do_formula t2 accum
blanchet@35386
  1020
        in
blanchet@35386
  1021
          (MApp (MApp (MRaw (t0, mtype_for (fastype_of t0)), m1), m2), accum)
blanchet@35386
  1022
        end
blanchet@35386
  1023
      and do_implies t0 t1 t2 accum =
blanchet@35386
  1024
        let
blanchet@35386
  1025
          val (m1, accum) = do_term t1 accum
blanchet@35386
  1026
          val (m2, accum) = do_formula t2 accum
blanchet@35386
  1027
        in
blanchet@35386
  1028
          (MApp (MApp (MRaw (t0, mtype_for (fastype_of t0)), m1), m2), accum)
blanchet@35386
  1029
        end
blanchet@35812
  1030
      and do_formula t accum =
blanchet@33192
  1031
          case t of
blanchet@35386
  1032
            (t0 as Const (@{const_name all}, _)) $ Abs (s1, T1, t1) =>
blanchet@35386
  1033
            do_all t0 s1 T1 t1 accum
blanchet@35665
  1034
          | @{const Trueprop} $ t1 =>
blanchet@35665
  1035
            let val (m1, accum) = do_formula t1 accum in
blanchet@35665
  1036
              (MApp (MRaw (@{const Trueprop}, mtype_for (bool_T --> prop_T)),
blanchet@35665
  1037
                     m1), accum)
blanchet@35665
  1038
            end
blanchet@35386
  1039
          | Const (x as (@{const_name "=="}, _)) $ t1 $ t2 =>
blanchet@35665
  1040
            consider_general_equals mdata true x t1 t2 accum
blanchet@35386
  1041
          | (t0 as @{const "==>"}) $ t1 $ t2 => do_implies t0 t1 t2 accum
blanchet@35386
  1042
          | (t0 as @{const Pure.conjunction}) $ t1 $ t2 =>
blanchet@35386
  1043
            do_conjunction t0 t1 t2 accum
blanchet@35386
  1044
          | (t0 as Const (@{const_name All}, _)) $ Abs (s0, T1, t1) =>
blanchet@35386
  1045
            do_all t0 s0 T1 t1 accum
haftmann@38864
  1046
          | Const (x as (@{const_name HOL.eq}, _)) $ t1 $ t2 =>
blanchet@35665
  1047
            consider_general_equals mdata true x t1 t2 accum
haftmann@38795
  1048
          | (t0 as @{const HOL.conj}) $ t1 $ t2 => do_conjunction t0 t1 t2 accum
haftmann@38786
  1049
          | (t0 as @{const HOL.implies}) $ t1 $ t2 => do_implies t0 t1 t2 accum
blanchet@33232
  1050
          | _ => raise TERM ("Nitpick_Mono.consider_definitional_axiom.\
blanchet@33192
  1051
                             \do_formula", [t])
blanchet@33192
  1052
    in do_formula t end
blanchet@33192
  1053
blanchet@40989
  1054
fun string_for_mtype_of_term ctxt asgs t M =
blanchet@40989
  1055
  Syntax.string_of_term ctxt t ^ " : " ^ string_for_mtype (resolve_mtype asgs M)
blanchet@33192
  1056
blanchet@40989
  1057
fun print_mtype_context ctxt asgs ({frees, consts, ...} : mtype_context) =
blanchet@38179
  1058
  trace_msg (fn () =>
blanchet@40989
  1059
      map (fn (x, M) => string_for_mtype_of_term ctxt asgs (Free x) M) frees @
blanchet@40989
  1060
      map (fn (x, M) => string_for_mtype_of_term ctxt asgs (Const x) M) consts
blanchet@37476
  1061
      |> cat_lines)
blanchet@33192
  1062
blanchet@35665
  1063
fun amass f t (ms, accum) =
blanchet@35386
  1064
  let val (m, accum) = f t accum in (m :: ms, accum) end
blanchet@35386
  1065
blanchet@40993
  1066
fun infer which no_harmless (hol_ctxt as {ctxt, ...}) binarize calculus alpha_T
blanchet@35665
  1067
          (nondef_ts, def_ts) =
blanchet@33192
  1068
  let
blanchet@38179
  1069
    val _ = trace_msg (fn () => "****** " ^ which ^ " analysis: " ^
blanchet@38179
  1070
                                string_for_mtype MAlpha ^ " is " ^
blanchet@38179
  1071
                                Syntax.string_of_typ ctxt alpha_T)
blanchet@35665
  1072
    val mdata as {max_fresh, constr_mcache, ...} =
blanchet@35665
  1073
      initial_mdata hol_ctxt binarize no_harmless alpha_T
blanchet@35812
  1074
    val accum = (initial_gamma, ([], [], []))
blanchet@35386
  1075
    val (nondef_ms, accum) =
blanchet@35665
  1076
      ([], accum) |> amass (consider_general_formula mdata Plus) (hd nondef_ts)
blanchet@35665
  1077
                  |> fold (amass (consider_nondefinitional_axiom mdata))
blanchet@35386
  1078
                          (tl nondef_ts)
blanchet@35386
  1079
    val (def_ms, (gamma, cset)) =
blanchet@35665
  1080
      ([], accum) |> fold (amass (consider_definitional_axiom mdata)) def_ts
blanchet@33192
  1081
  in
blanchet@40993
  1082
    case solve calculus (!max_fresh) cset of
blanchet@40989
  1083
      SOME asgs => (print_mtype_context ctxt asgs gamma;
blanchet@40989
  1084
                    SOME (asgs, (nondef_ms, def_ms), !constr_mcache))
blanchet@35665
  1085
    | _ => NONE
blanchet@33192
  1086
  end
blanchet@35812
  1087
  handle UNSOLVABLE () => NONE
blanchet@35812
  1088
       | MTYPE (loc, Ms, Ts) =>
blanchet@35665
  1089
         raise BAD (loc, commas (map string_for_mtype Ms @
blanchet@35665
  1090
                                 map (Syntax.string_of_typ ctxt) Ts))
blanchet@35665
  1091
       | MTERM (loc, ms) =>
blanchet@35665
  1092
         raise BAD (loc, commas (map (string_for_mterm ctxt) ms))
blanchet@35665
  1093
blanchet@40993
  1094
fun formulas_monotonic hol_ctxt =
blanchet@40993
  1095
  is_some oooo infer "Monotonicity" false hol_ctxt
blanchet@35665
  1096
blanchet@35665
  1097
fun fin_fun_constr T1 T2 =
blanchet@35665
  1098
  (@{const_name FinFun}, (T1 --> T2) --> Type (@{type_name fin_fun}, [T1, T2]))
blanchet@35665
  1099
blanchet@40993
  1100
fun finitize_funs (hol_ctxt as {thy, ctxt, stds, constr_cache, ...}) binarize
blanchet@40993
  1101
                  finitizes calculus alpha_T tsp =
blanchet@40993
  1102
  case infer "Finiteness" true hol_ctxt binarize calculus alpha_T tsp of
blanchet@40989
  1103
    SOME (asgs, msp, constr_mtypes) =>
blanchet@40989
  1104
    if forall (curry (op =) Gen o snd) asgs then
blanchet@35812
  1105
      tsp
blanchet@35812
  1106
    else
blanchet@35812
  1107
      let
blanchet@40988
  1108
        fun should_finitize T aa =
blanchet@35812
  1109
          case triple_lookup (type_match thy) finitizes T of
blanchet@35812
  1110
            SOME (SOME false) => false
blanchet@40989
  1111
          | _ => resolve_annotation_atom asgs aa = A Fls
blanchet@35812
  1112
        fun type_from_mtype T M =
blanchet@35812
  1113
          case (M, T) of
blanchet@35812
  1114
            (MAlpha, _) => T
blanchet@40988
  1115
          | (MFun (M1, aa, M2), Type (@{type_name fun}, Ts)) =>
blanchet@40988
  1116
            Type (if should_finitize T aa then @{type_name fin_fun}
blanchet@35812
  1117
                  else @{type_name fun}, map2 type_from_mtype Ts [M1, M2])
blanchet@38190
  1118
          | (MPair (M1, M2), Type (@{type_name prod}, Ts)) =>
blanchet@38190
  1119
            Type (@{type_name prod}, map2 type_from_mtype Ts [M1, M2])
blanchet@35812
  1120
          | (MType _, _) => T
blanchet@35812
  1121
          | _ => raise MTYPE ("Nitpick_Mono.finitize_funs.type_from_mtype",
blanchet@35812
  1122
                              [M], [T])
blanchet@35812
  1123
        fun finitize_constr (x as (s, T)) =
blanchet@35812
  1124
          (s, case AList.lookup (op =) constr_mtypes x of
blanchet@35812
  1125
                SOME M => type_from_mtype T M
blanchet@35812
  1126
              | NONE => T)
blanchet@37267
  1127
        fun term_from_mterm new_Ts old_Ts m =
blanchet@35812
  1128
          case m of
blanchet@35812
  1129
            MRaw (t, M) =>
blanchet@35812
  1130
            let
blanchet@37267
  1131
              val T = fastype_of1 (old_Ts, t)
blanchet@35812
  1132
              val T' = type_from_mtype T M
blanchet@35812
  1133
            in
blanchet@35812
  1134
              case t of
blanchet@35812
  1135
                Const (x as (s, _)) =>
blanchet@37476
  1136
                if s = @{const_name finite} then
blanchet@35812
  1137
                  case domain_type T' of
blanchet@35812
  1138
                    set_T' as Type (@{type_name fin_fun}, _) =>
blanchet@35812
  1139
                    Abs (Name.uu, set_T', @{const True})
blanchet@35812
  1140
                  | _ => Const (s, T')
blanchet@35812
  1141
                else if s = @{const_name "=="} orelse
haftmann@38864
  1142
                        s = @{const_name HOL.eq} then
blanchet@37476
  1143
                  let
blanchet@37476
  1144
                    val T =
blanchet@37476
  1145
                      case T' of
blanchet@37476
  1146
                        Type (_, [T1, Type (_, [T2, T3])]) =>
blanchet@37476
  1147
                        T1 --> T2 --> T3
blanchet@37476
  1148
                      | _ => raise TYPE ("Nitpick_Mono.finitize_funs.\
blanchet@37476
  1149
                                         \term_from_mterm", [T, T'], [])
blanchet@37476
  1150
                  in coerce_term hol_ctxt new_Ts T' T (Const (s, T)) end
blanchet@39359
  1151
                else if is_built_in_const thy stds x then
blanchet@37267
  1152
                  coerce_term hol_ctxt new_Ts T' T t
blanchet@37256
  1153
                else if is_constr ctxt stds x then
blanchet@35812
  1154
                  Const (finitize_constr x)
blanchet@35812
  1155
                else if is_sel s then
blanchet@35812
  1156
                  let
blanchet@35812
  1157
                    val n = sel_no_from_name s
blanchet@35812
  1158
                    val x' =
blanchet@35812
  1159
                      x |> binarized_and_boxed_constr_for_sel hol_ctxt binarize
blanchet@35812
  1160
                        |> finitize_constr
blanchet@35812
  1161
                    val x'' =
blanchet@35812
  1162
                      binarized_and_boxed_nth_sel_for_constr hol_ctxt binarize
blanchet@35812
  1163
                                                             x' n
blanchet@35812
  1164
                  in Const x'' end
blanchet@35812
  1165
                else
blanchet@35812
  1166
                  Const (s, T')
blanchet@35812
  1167
              | Free (s, T) => Free (s, type_from_mtype T M)
blanchet@35812
  1168
              | Bound _ => t
blanchet@35812
  1169
              | _ => raise MTERM ("Nitpick_Mono.finitize_funs.term_from_mterm",
blanchet@35812
  1170
                                  [m])
blanchet@35812
  1171
            end
blanchet@35812
  1172
          | MApp (m1, m2) =>
blanchet@35812
  1173
            let
blanchet@37267
  1174
              val (t1, t2) = pairself (term_from_mterm new_Ts old_Ts) (m1, m2)
blanchet@37267
  1175
              val (T1, T2) = pairself (curry fastype_of1 new_Ts) (t1, t2)
blanchet@35812
  1176
              val (t1', T2') =
blanchet@35812
  1177
                case T1 of
blanchet@39316
  1178
                  Type (s, [T11, T12]) =>
blanchet@35812
  1179
                  (if s = @{type_name fin_fun} then
blanchet@37256
  1180
                     select_nth_constr_arg ctxt stds (fin_fun_constr T11 T12) t1
blanchet@35812
  1181
                                           0 (T11 --> T12)
blanchet@35812
  1182
                   else
blanchet@35812
  1183
                     t1, T11)
blanchet@35812
  1184
                | _ => raise TYPE ("Nitpick_Mono.finitize_funs.term_from_mterm",
blanchet@35812
  1185
                                   [T1], [])
blanchet@37267
  1186
            in betapply (t1', coerce_term hol_ctxt new_Ts T2' T2 t2) end
blanchet@40988
  1187
          | MAbs (s, old_T, M, aa, m') =>
blanchet@35812
  1188
            let
blanchet@37267
  1189
              val new_T = type_from_mtype old_T M
blanchet@37267
  1190
              val t' = term_from_mterm (new_T :: new_Ts) (old_T :: old_Ts) m'
blanchet@37267
  1191
              val T' = fastype_of1 (new_T :: new_Ts, t')
blanchet@35812
  1192
            in
blanchet@37267
  1193
              Abs (s, new_T, t')
blanchet@40988
  1194
              |> should_finitize (new_T --> T') aa
blanchet@37267
  1195
                 ? construct_value ctxt stds (fin_fun_constr new_T T') o single
blanchet@35812
  1196
            end
blanchet@35812
  1197
      in
blanchet@35812
  1198
        Unsynchronized.change constr_cache (map (apsnd (map finitize_constr)));
blanchet@37267
  1199
        pairself (map (term_from_mterm [] [])) msp
blanchet@35812
  1200
      end
blanchet@35665
  1201
  | NONE => tsp
blanchet@33192
  1202
blanchet@33192
  1203
end;