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