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