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