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