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