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