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