src/HOL/Tools/Nitpick/nitpick_mono.ML
author blanchet
Thu Feb 18 10:38:37 2010 +0100 (2010-02-18)
changeset 35219 15a5f213ef5b
parent 35190 ce653cc27a94
child 35220 2bcdae5f4fdb
permissions -rw-r--r--
fix bug in Nitpick's monotonicity code w.r.t. binary integers
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@33192
     5
Monotonicity predicate for higher-order logic.
blanchet@33192
     6
*)
blanchet@33192
     7
blanchet@33192
     8
signature NITPICK_MONO =
blanchet@33192
     9
sig
blanchet@34982
    10
  datatype sign = Plus | Minus
blanchet@35070
    11
  type hol_context = Nitpick_HOL.hol_context
blanchet@33192
    12
blanchet@33192
    13
  val formulas_monotonic :
blanchet@35190
    14
    hol_context -> bool -> typ -> sign -> term list -> term list -> term -> bool
blanchet@33192
    15
end;
blanchet@33192
    16
blanchet@33232
    17
structure Nitpick_Mono : NITPICK_MONO =
blanchet@33192
    18
struct
blanchet@33192
    19
blanchet@33232
    20
open Nitpick_Util
blanchet@33232
    21
open Nitpick_HOL
blanchet@33192
    22
blanchet@33192
    23
type var = int
blanchet@33192
    24
blanchet@34982
    25
datatype sign = Plus | Minus
blanchet@33192
    26
datatype sign_atom = S of sign | V of var
blanchet@33192
    27
blanchet@33192
    28
type literal = var * sign
blanchet@33192
    29
blanchet@33192
    30
datatype ctype =
blanchet@33192
    31
  CAlpha |
blanchet@33192
    32
  CFun of ctype * sign_atom * ctype |
blanchet@33192
    33
  CPair of ctype * ctype |
blanchet@33192
    34
  CType of string * ctype list |
blanchet@33192
    35
  CRec of string * typ list
blanchet@33192
    36
blanchet@33192
    37
type cdata =
blanchet@35070
    38
  {hol_ctxt: hol_context,
blanchet@35190
    39
   binarize: bool,
blanchet@33192
    40
   alpha_T: typ,
blanchet@33192
    41
   max_fresh: int Unsynchronized.ref,
blanchet@33192
    42
   datatype_cache: ((string * typ list) * ctype) list Unsynchronized.ref,
blanchet@33192
    43
   constr_cache: (styp * ctype) list Unsynchronized.ref}
blanchet@33192
    44
blanchet@33192
    45
exception CTYPE of string * ctype list
blanchet@33192
    46
blanchet@33192
    47
(* string -> unit *)
blanchet@33192
    48
fun print_g (s : string) = ()
blanchet@33192
    49
blanchet@33192
    50
(* var -> string *)
blanchet@33192
    51
val string_for_var = signed_string_of_int
blanchet@33192
    52
(* string -> var list -> string *)
blanchet@33192
    53
fun string_for_vars sep [] = "0\<^bsub>" ^ sep ^ "\<^esub>"
blanchet@33192
    54
  | string_for_vars sep xs = space_implode sep (map string_for_var xs)
blanchet@33192
    55
fun subscript_string_for_vars sep xs =
blanchet@33192
    56
  if null xs then "" else "\<^bsub>" ^ string_for_vars sep xs ^ "\<^esub>"
blanchet@33192
    57
blanchet@33192
    58
(* sign -> string *)
blanchet@34982
    59
fun string_for_sign Plus = "+"
blanchet@34982
    60
  | string_for_sign Minus = "-"
blanchet@33192
    61
blanchet@33192
    62
(* sign -> sign -> sign *)
blanchet@34982
    63
fun xor sn1 sn2 = if sn1 = sn2 then Plus else Minus
blanchet@33192
    64
(* sign -> sign *)
blanchet@34982
    65
val negate = xor Minus
blanchet@33192
    66
blanchet@33192
    67
(* sign_atom -> string *)
blanchet@33192
    68
fun string_for_sign_atom (S sn) = string_for_sign sn
blanchet@33192
    69
  | string_for_sign_atom (V j) = string_for_var j
blanchet@33192
    70
blanchet@33192
    71
(* literal -> string *)
blanchet@33192
    72
fun string_for_literal (x, sn) = string_for_var x ^ " = " ^ string_for_sign sn
blanchet@33192
    73
blanchet@33192
    74
val bool_C = CType (@{type_name bool}, [])
blanchet@33192
    75
blanchet@33192
    76
(* ctype -> bool *)
blanchet@33192
    77
fun is_CRec (CRec _) = true
blanchet@33192
    78
  | is_CRec _ = false
blanchet@33192
    79
blanchet@33192
    80
val no_prec = 100
blanchet@33192
    81
val prec_CFun = 1
blanchet@33192
    82
val prec_CPair = 2
blanchet@33192
    83
blanchet@33192
    84
(* tuple_set -> int *)
blanchet@33192
    85
fun precedence_of_ctype (CFun _) = prec_CFun
blanchet@33192
    86
  | precedence_of_ctype (CPair _) = prec_CPair
blanchet@33192
    87
  | precedence_of_ctype _ = no_prec
blanchet@33192
    88
blanchet@33192
    89
(* ctype -> string *)
blanchet@33192
    90
val string_for_ctype =
blanchet@33192
    91
  let
blanchet@33192
    92
    (* int -> ctype -> string *)
blanchet@33192
    93
    fun aux outer_prec C =
blanchet@33192
    94
      let
blanchet@33192
    95
        val prec = precedence_of_ctype C
blanchet@33192
    96
        val need_parens = (prec < outer_prec)
blanchet@33192
    97
      in
blanchet@33192
    98
        (if need_parens then "(" else "") ^
blanchet@33192
    99
        (case C of
blanchet@33192
   100
           CAlpha => "\<alpha>"
blanchet@33192
   101
         | CFun (C1, a, C2) =>
blanchet@33192
   102
           aux (prec + 1) C1 ^ " \<Rightarrow>\<^bsup>" ^
blanchet@33192
   103
           string_for_sign_atom a ^ "\<^esup> " ^ aux prec C2
blanchet@33192
   104
         | CPair (C1, C2) => aux (prec + 1) C1 ^ " \<times> " ^ aux prec C2
blanchet@33192
   105
         | CType (s, []) =>
blanchet@34121
   106
           if s = @{type_name prop} orelse s = @{type_name bool} then "o" else s
blanchet@33192
   107
         | CType (s, Cs) => "(" ^ commas (map (aux 0) Cs) ^ ") " ^ s
blanchet@33192
   108
         | CRec (s, _) => "[" ^ s ^ "]") ^
blanchet@33192
   109
        (if need_parens then ")" else "")
blanchet@33192
   110
      end
blanchet@33192
   111
  in aux 0 end
blanchet@33192
   112
blanchet@33192
   113
(* ctype -> ctype list *)
blanchet@33192
   114
fun flatten_ctype (CPair (C1, C2)) = maps flatten_ctype [C1, C2]
blanchet@33192
   115
  | flatten_ctype (CType (_, Cs)) = maps flatten_ctype Cs
blanchet@33192
   116
  | flatten_ctype C = [C]
blanchet@33192
   117
blanchet@35190
   118
(* hol_context -> bool -> typ -> cdata *)
blanchet@35190
   119
fun initial_cdata hol_ctxt binarize alpha_T =
blanchet@35190
   120
  ({hol_ctxt = hol_ctxt, binarize = binarize, alpha_T = alpha_T,
blanchet@35190
   121
    max_fresh = Unsynchronized.ref 0, datatype_cache = Unsynchronized.ref [],
blanchet@33192
   122
    constr_cache = Unsynchronized.ref []} : cdata)
blanchet@33192
   123
blanchet@33192
   124
(* typ -> typ -> bool *)
blanchet@33192
   125
fun could_exist_alpha_subtype alpha_T (T as Type (_, Ts)) =
blanchet@34936
   126
    T = alpha_T orelse (not (is_fp_iterator_type T) andalso
blanchet@34936
   127
                        exists (could_exist_alpha_subtype alpha_T) Ts)
blanchet@33192
   128
  | could_exist_alpha_subtype alpha_T T = (T = alpha_T)
blanchet@33192
   129
(* theory -> typ -> typ -> bool *)
blanchet@34121
   130
fun could_exist_alpha_sub_ctype _ (alpha_T as TFree _) T =
blanchet@34121
   131
    could_exist_alpha_subtype alpha_T T
blanchet@34121
   132
  | could_exist_alpha_sub_ctype thy alpha_T T =
blanchet@34121
   133
    (T = alpha_T orelse is_datatype thy T)
blanchet@33192
   134
blanchet@33192
   135
(* ctype -> bool *)
blanchet@33192
   136
fun exists_alpha_sub_ctype CAlpha = true
blanchet@33192
   137
  | exists_alpha_sub_ctype (CFun (C1, _, C2)) =
blanchet@33192
   138
    exists exists_alpha_sub_ctype [C1, C2]
blanchet@33192
   139
  | exists_alpha_sub_ctype (CPair (C1, C2)) =
blanchet@33192
   140
    exists exists_alpha_sub_ctype [C1, C2]
blanchet@33192
   141
  | exists_alpha_sub_ctype (CType (_, Cs)) = exists exists_alpha_sub_ctype Cs
blanchet@33192
   142
  | exists_alpha_sub_ctype (CRec _) = true
blanchet@33192
   143
blanchet@33192
   144
(* ctype -> bool *)
blanchet@33192
   145
fun exists_alpha_sub_ctype_fresh CAlpha = true
blanchet@33192
   146
  | exists_alpha_sub_ctype_fresh (CFun (_, V _, _)) = true
blanchet@33192
   147
  | exists_alpha_sub_ctype_fresh (CFun (_, _, C2)) =
blanchet@33192
   148
    exists_alpha_sub_ctype_fresh C2
blanchet@33192
   149
  | exists_alpha_sub_ctype_fresh (CPair (C1, C2)) =
blanchet@33192
   150
    exists exists_alpha_sub_ctype_fresh [C1, C2]
blanchet@33192
   151
  | exists_alpha_sub_ctype_fresh (CType (_, Cs)) =
blanchet@33192
   152
    exists exists_alpha_sub_ctype_fresh Cs
blanchet@33192
   153
  | exists_alpha_sub_ctype_fresh (CRec _) = true
blanchet@33192
   154
blanchet@33192
   155
(* string * typ list -> ctype list -> ctype *)
blanchet@33192
   156
fun constr_ctype_for_binders z Cs =
blanchet@34982
   157
  fold_rev (fn C => curry3 CFun C (S Minus)) Cs (CRec z)
blanchet@33192
   158
blanchet@33192
   159
(* ((string * typ list) * ctype) list -> ctype list -> ctype -> ctype *)
blanchet@33192
   160
fun repair_ctype _ _ CAlpha = CAlpha
blanchet@33192
   161
  | repair_ctype cache seen (CFun (C1, a, C2)) =
blanchet@33192
   162
    CFun (repair_ctype cache seen C1, a, repair_ctype cache seen C2)
blanchet@33192
   163
  | repair_ctype cache seen (CPair Cp) =
blanchet@33192
   164
    CPair (pairself (repair_ctype cache seen) Cp)
blanchet@33192
   165
  | repair_ctype cache seen (CType (s, Cs)) =
blanchet@33192
   166
    CType (s, maps (flatten_ctype o repair_ctype cache seen) Cs)
blanchet@33192
   167
  | repair_ctype cache seen (CRec (z as (s, _))) =
blanchet@33192
   168
    case AList.lookup (op =) cache z |> the of
blanchet@33192
   169
      CRec _ => CType (s, [])
blanchet@34121
   170
    | C => if member (op =) seen C then CType (s, [])
blanchet@33192
   171
           else repair_ctype cache (C :: seen) C
blanchet@33192
   172
blanchet@33192
   173
(* ((string * typ list) * ctype) list Unsynchronized.ref -> unit *)
blanchet@33192
   174
fun repair_datatype_cache cache =
blanchet@33192
   175
  let
blanchet@33192
   176
    (* (string * typ list) * ctype -> unit *)
blanchet@33192
   177
    fun repair_one (z, C) =
blanchet@33192
   178
      Unsynchronized.change cache
blanchet@33192
   179
          (AList.update (op =) (z, repair_ctype (!cache) [] C))
blanchet@33192
   180
  in List.app repair_one (rev (!cache)) end
blanchet@33192
   181
blanchet@33192
   182
(* (typ * ctype) list -> (styp * ctype) list Unsynchronized.ref -> unit *)
blanchet@33192
   183
fun repair_constr_cache dtype_cache constr_cache =
blanchet@33192
   184
  let
blanchet@33192
   185
    (* styp * ctype -> unit *)
blanchet@33192
   186
    fun repair_one (x, C) =
blanchet@33192
   187
      Unsynchronized.change constr_cache
blanchet@33192
   188
          (AList.update (op =) (x, repair_ctype dtype_cache [] C))
blanchet@33192
   189
  in List.app repair_one (!constr_cache) end
blanchet@33192
   190
blanchet@33192
   191
(* cdata -> typ -> ctype *)
blanchet@35219
   192
fun fresh_ctype_for_type ({hol_ctxt as {thy, ...}, binarize, alpha_T, max_fresh,
blanchet@33192
   193
                           datatype_cache, constr_cache, ...} : cdata) =
blanchet@33192
   194
  let
blanchet@33192
   195
    (* typ -> typ -> ctype *)
blanchet@33192
   196
    fun do_fun T1 T2 =
blanchet@33192
   197
      let
blanchet@33192
   198
        val C1 = do_type T1
blanchet@33192
   199
        val C2 = do_type T2
blanchet@34936
   200
        val a = if is_boolean_type (body_type T2) andalso
blanchet@34936
   201
                   exists_alpha_sub_ctype_fresh C1 then
blanchet@33192
   202
                  V (Unsynchronized.inc max_fresh)
blanchet@33192
   203
                else
blanchet@34982
   204
                  S Minus
blanchet@33192
   205
      in CFun (C1, a, C2) end
blanchet@33192
   206
    (* typ -> ctype *)
blanchet@33192
   207
    and do_type T =
blanchet@33192
   208
      if T = alpha_T then
blanchet@33192
   209
        CAlpha
blanchet@33192
   210
      else case T of
blanchet@33192
   211
        Type ("fun", [T1, T2]) => do_fun T1 T2
blanchet@33192
   212
      | Type (@{type_name fun_box}, [T1, T2]) => do_fun T1 T2
blanchet@33192
   213
      | Type ("*", [T1, T2]) => CPair (pairself do_type (T1, T2))
blanchet@33192
   214
      | Type (z as (s, _)) =>
blanchet@33192
   215
        if could_exist_alpha_sub_ctype thy alpha_T T then
blanchet@33192
   216
          case AList.lookup (op =) (!datatype_cache) z of
blanchet@33192
   217
            SOME C => C
blanchet@33192
   218
          | NONE =>
blanchet@33192
   219
            let
blanchet@33192
   220
              val _ = Unsynchronized.change datatype_cache (cons (z, CRec z))
blanchet@35219
   221
              val xs = binarized_and_boxed_datatype_constrs hol_ctxt binarize T
blanchet@33192
   222
              val (all_Cs, constr_Cs) =
blanchet@33192
   223
                fold_rev (fn (_, T') => fn (all_Cs, constr_Cs) =>
blanchet@33192
   224
                             let
blanchet@33192
   225
                               val binder_Cs = map do_type (binder_types T')
blanchet@33192
   226
                               val new_Cs = filter exists_alpha_sub_ctype_fresh
blanchet@33192
   227
                                                   binder_Cs
blanchet@33192
   228
                               val constr_C = constr_ctype_for_binders z
blanchet@33192
   229
                                                                       binder_Cs
blanchet@33192
   230
                             in
blanchet@33192
   231
                               (union (op =) new_Cs all_Cs,
blanchet@33192
   232
                                constr_C :: constr_Cs)
blanchet@33192
   233
                             end)
blanchet@33192
   234
                         xs ([], [])
blanchet@33192
   235
              val C = CType (s, all_Cs)
blanchet@33192
   236
              val _ = Unsynchronized.change datatype_cache
blanchet@33192
   237
                          (AList.update (op =) (z, C))
blanchet@33192
   238
              val _ = Unsynchronized.change constr_cache
blanchet@33192
   239
                          (append (xs ~~ constr_Cs))
blanchet@33192
   240
            in
blanchet@33192
   241
              if forall (not o is_CRec o snd) (!datatype_cache) then
blanchet@33192
   242
                (repair_datatype_cache datatype_cache;
blanchet@33192
   243
                 repair_constr_cache (!datatype_cache) constr_cache;
blanchet@33192
   244
                 AList.lookup (op =) (!datatype_cache) z |> the)
blanchet@33192
   245
              else
blanchet@33192
   246
                C
blanchet@33192
   247
            end
blanchet@33192
   248
        else
blanchet@33192
   249
          CType (s, [])
blanchet@33192
   250
      | _ => CType (Refute.string_of_typ T, [])
blanchet@33192
   251
  in do_type end
blanchet@33192
   252
blanchet@33192
   253
(* ctype -> ctype list *)
blanchet@33192
   254
fun prodC_factors (CPair (C1, C2)) = maps prodC_factors [C1, C2]
blanchet@33192
   255
  | prodC_factors C = [C]
blanchet@33192
   256
(* ctype -> ctype list * ctype *)
blanchet@34982
   257
fun curried_strip_ctype (CFun (C1, S Minus, C2)) =
blanchet@33192
   258
    curried_strip_ctype C2 |>> append (prodC_factors C1)
blanchet@33192
   259
  | curried_strip_ctype C = ([], C)
blanchet@33192
   260
(* string -> ctype -> ctype *)
blanchet@33192
   261
fun sel_ctype_from_constr_ctype s C =
blanchet@33192
   262
  let val (arg_Cs, dataC) = curried_strip_ctype C in
blanchet@34982
   263
    CFun (dataC, S Minus,
blanchet@33192
   264
          case sel_no_from_name s of ~1 => bool_C | n => nth arg_Cs n)
blanchet@33192
   265
  end
blanchet@33192
   266
blanchet@33192
   267
(* cdata -> styp -> ctype *)
blanchet@35070
   268
fun ctype_for_constr (cdata as {hol_ctxt as {thy, ...}, alpha_T, constr_cache,
blanchet@33192
   269
                                ...}) (x as (_, T)) =
blanchet@33192
   270
  if could_exist_alpha_sub_ctype thy alpha_T T then
blanchet@33192
   271
    case AList.lookup (op =) (!constr_cache) x of
blanchet@33192
   272
      SOME C => C
blanchet@35219
   273
    | NONE => (fresh_ctype_for_type cdata (body_type T);
blanchet@35219
   274
               AList.lookup (op =) (!constr_cache) x |> the)
blanchet@33192
   275
  else
blanchet@33192
   276
    fresh_ctype_for_type cdata T
blanchet@35190
   277
fun ctype_for_sel (cdata as {hol_ctxt, binarize, ...}) (x as (s, _)) =
blanchet@35190
   278
  x |> binarized_and_boxed_constr_for_sel hol_ctxt binarize
blanchet@35190
   279
    |> ctype_for_constr cdata |> sel_ctype_from_constr_ctype s
blanchet@33192
   280
blanchet@33192
   281
(* literal list -> ctype -> ctype *)
blanchet@33192
   282
fun instantiate_ctype lits =
blanchet@33192
   283
  let
blanchet@33192
   284
    (* ctype -> ctype *)
blanchet@33192
   285
    fun aux CAlpha = CAlpha
blanchet@33192
   286
      | aux (CFun (C1, V x, C2)) =
blanchet@33192
   287
        let
blanchet@33192
   288
          val a = case AList.lookup (op =) lits x of
blanchet@33192
   289
                    SOME sn => S sn
blanchet@33192
   290
                  | NONE => V x
blanchet@33192
   291
        in CFun (aux C1, a, aux C2) end
blanchet@33192
   292
      | aux (CFun (C1, a, C2)) = CFun (aux C1, a, aux C2)
blanchet@33192
   293
      | aux (CPair Cp) = CPair (pairself aux Cp)
blanchet@33192
   294
      | aux (CType (s, Cs)) = CType (s, map aux Cs)
blanchet@33192
   295
      | aux (CRec z) = CRec z
blanchet@33192
   296
  in aux end
blanchet@33192
   297
blanchet@33192
   298
datatype comp_op = Eq | Leq
blanchet@33192
   299
blanchet@33192
   300
type comp = sign_atom * sign_atom * comp_op * var list
blanchet@33192
   301
type sign_expr = literal list
blanchet@33192
   302
blanchet@33192
   303
datatype constraint_set =
blanchet@33192
   304
  UnsolvableCSet |
blanchet@33192
   305
  CSet of literal list * comp list * sign_expr list
blanchet@33192
   306
blanchet@33192
   307
(* comp_op -> string *)
blanchet@33192
   308
fun string_for_comp_op Eq = "="
blanchet@33192
   309
  | string_for_comp_op Leq = "\<le>"
blanchet@33192
   310
blanchet@33192
   311
(* sign_expr -> string *)
blanchet@33192
   312
fun string_for_sign_expr [] = "\<bot>"
blanchet@33192
   313
  | string_for_sign_expr lits =
blanchet@33192
   314
    space_implode " \<or> " (map string_for_literal lits)
blanchet@33192
   315
blanchet@33192
   316
(* constraint_set *)
blanchet@33192
   317
val slack = CSet ([], [], [])
blanchet@33192
   318
blanchet@33192
   319
(* literal -> literal list option -> literal list option *)
blanchet@33192
   320
fun do_literal _ NONE = NONE
blanchet@33192
   321
  | do_literal (x, sn) (SOME lits) =
blanchet@33192
   322
    case AList.lookup (op =) lits x of
blanchet@33192
   323
      SOME sn' => if sn = sn' then SOME lits else NONE
blanchet@33192
   324
    | NONE => SOME ((x, sn) :: lits)
blanchet@33192
   325
blanchet@33192
   326
(* comp_op -> var list -> sign_atom -> sign_atom -> literal list * comp list
blanchet@33192
   327
   -> (literal list * comp list) option *)
blanchet@33192
   328
fun do_sign_atom_comp Eq [] a1 a2 (accum as (lits, comps)) =
blanchet@33192
   329
    (case (a1, a2) of
blanchet@33192
   330
       (S sn1, S sn2) => if sn1 = sn2 then SOME accum else NONE
blanchet@33192
   331
     | (V x1, S sn2) =>
blanchet@33192
   332
       Option.map (rpair comps) (do_literal (x1, sn2) (SOME lits))
blanchet@33192
   333
     | (V _, V _) => SOME (lits, insert (op =) (a1, a2, Eq, []) comps)
blanchet@33192
   334
     | _ => do_sign_atom_comp Eq [] a2 a1 accum)
blanchet@33192
   335
  | do_sign_atom_comp Leq [] a1 a2 (accum as (lits, comps)) =
blanchet@33192
   336
    (case (a1, a2) of
blanchet@34982
   337
       (_, S Minus) => SOME accum
blanchet@34982
   338
     | (S Plus, _) => SOME accum
blanchet@34982
   339
     | (S Minus, S Plus) => NONE
blanchet@33192
   340
     | (V _, V _) => SOME (lits, insert (op =) (a1, a2, Leq, []) comps)
blanchet@33192
   341
     | _ => do_sign_atom_comp Eq [] a1 a2 accum)
blanchet@33192
   342
  | do_sign_atom_comp cmp xs a1 a2 (accum as (lits, comps)) =
blanchet@33192
   343
    SOME (lits, insert (op =) (a1, a2, cmp, xs) comps)
blanchet@33192
   344
blanchet@33192
   345
(* comp -> var list -> ctype -> ctype -> (literal list * comp list) option
blanchet@33192
   346
   -> (literal list * comp list) option *)
blanchet@33192
   347
fun do_ctype_comp _ _ _ _ NONE = NONE
blanchet@33192
   348
  | do_ctype_comp _ _ CAlpha CAlpha accum = accum
blanchet@33192
   349
  | do_ctype_comp Eq xs (CFun (C11, a1, C12)) (CFun (C21, a2, C22))
blanchet@33192
   350
                  (SOME accum) =
blanchet@33192
   351
     accum |> do_sign_atom_comp Eq xs a1 a2 |> do_ctype_comp Eq xs C11 C21
blanchet@33192
   352
           |> do_ctype_comp Eq xs C12 C22
blanchet@33192
   353
  | do_ctype_comp Leq xs (CFun (C11, a1, C12)) (CFun (C21, a2, C22))
blanchet@33192
   354
                  (SOME accum) =
blanchet@33192
   355
    (if exists_alpha_sub_ctype C11 then
blanchet@33192
   356
       accum |> do_sign_atom_comp Leq xs a1 a2
blanchet@33192
   357
             |> do_ctype_comp Leq xs C21 C11
blanchet@33192
   358
             |> (case a2 of
blanchet@34982
   359
                   S Minus => I
blanchet@34982
   360
                 | S Plus => do_ctype_comp Leq xs C11 C21
blanchet@33192
   361
                 | V x => do_ctype_comp Leq (x :: xs) C11 C21)
blanchet@33192
   362
     else
blanchet@33192
   363
       SOME accum)
blanchet@33192
   364
    |> do_ctype_comp Leq xs C12 C22
blanchet@33192
   365
  | do_ctype_comp cmp xs (C1 as CPair (C11, C12)) (C2 as CPair (C21, C22))
blanchet@33192
   366
                  accum =
blanchet@33192
   367
    (accum |> fold (uncurry (do_ctype_comp cmp xs)) [(C11, C21), (C12, C22)]
blanchet@33192
   368
     handle Library.UnequalLengths =>
blanchet@33232
   369
            raise CTYPE ("Nitpick_Mono.do_ctype_comp", [C1, C2]))
blanchet@33192
   370
  | do_ctype_comp cmp xs (CType _) (CType _) accum =
blanchet@33192
   371
    accum (* no need to compare them thanks to the cache *)
blanchet@33192
   372
  | do_ctype_comp _ _ C1 C2 _ =
blanchet@33232
   373
    raise CTYPE ("Nitpick_Mono.do_ctype_comp", [C1, C2])
blanchet@33192
   374
blanchet@33192
   375
(* comp_op -> ctype -> ctype -> constraint_set -> constraint_set *)
blanchet@33192
   376
fun add_ctype_comp _ _ _ UnsolvableCSet = UnsolvableCSet
blanchet@33192
   377
  | add_ctype_comp cmp C1 C2 (CSet (lits, comps, sexps)) =
blanchet@33192
   378
    (print_g ("*** Add " ^ string_for_ctype C1 ^ " " ^ string_for_comp_op cmp ^
blanchet@33192
   379
              " " ^ string_for_ctype C2);
blanchet@33192
   380
     case do_ctype_comp cmp [] C1 C2 (SOME (lits, comps)) of
blanchet@33192
   381
       NONE => (print_g "**** Unsolvable"; UnsolvableCSet)
blanchet@33192
   382
     | SOME (lits, comps) => CSet (lits, comps, sexps))
blanchet@33192
   383
blanchet@33192
   384
(* ctype -> ctype -> constraint_set -> constraint_set *)
blanchet@33192
   385
val add_ctypes_equal = add_ctype_comp Eq
blanchet@33192
   386
val add_is_sub_ctype = add_ctype_comp Leq
blanchet@33192
   387
blanchet@33192
   388
(* sign -> sign_expr -> ctype -> (literal list * sign_expr list) option
blanchet@33192
   389
   -> (literal list * sign_expr list) option *)
blanchet@33192
   390
fun do_notin_ctype_fv _ _ _ NONE = NONE
blanchet@34982
   391
  | do_notin_ctype_fv Minus _ CAlpha accum = accum
blanchet@34982
   392
  | do_notin_ctype_fv Plus [] CAlpha _ = NONE
blanchet@34982
   393
  | do_notin_ctype_fv Plus [(x, sn)] CAlpha (SOME (lits, sexps)) =
blanchet@33192
   394
    SOME lits |> do_literal (x, sn) |> Option.map (rpair sexps)
blanchet@34982
   395
  | do_notin_ctype_fv Plus sexp CAlpha (SOME (lits, sexps)) =
blanchet@33192
   396
    SOME (lits, insert (op =) sexp sexps)
blanchet@33192
   397
  | do_notin_ctype_fv sn sexp (CFun (C1, S sn', C2)) accum =
blanchet@34982
   398
    accum |> (if sn' = Plus andalso sn = Plus then
blanchet@34982
   399
                do_notin_ctype_fv Plus sexp C1
blanchet@34982
   400
              else
blanchet@34982
   401
                I)
blanchet@34982
   402
          |> (if sn' = Minus orelse sn = Plus then
blanchet@34982
   403
                do_notin_ctype_fv Minus sexp C1
blanchet@34982
   404
              else
blanchet@34982
   405
                I)
blanchet@33192
   406
          |> do_notin_ctype_fv sn sexp C2
blanchet@34982
   407
  | do_notin_ctype_fv Plus sexp (CFun (C1, V x, C2)) accum =
blanchet@34982
   408
    accum |> (case do_literal (x, Minus) (SOME sexp) of
blanchet@33192
   409
                NONE => I
blanchet@34982
   410
              | SOME sexp' => do_notin_ctype_fv Plus sexp' C1)
blanchet@34982
   411
          |> do_notin_ctype_fv Minus sexp C1
blanchet@34982
   412
          |> do_notin_ctype_fv Plus sexp C2
blanchet@34982
   413
  | do_notin_ctype_fv Minus sexp (CFun (C1, V x, C2)) accum =
blanchet@34982
   414
    accum |> (case do_literal (x, Plus) (SOME sexp) of
blanchet@33192
   415
                NONE => I
blanchet@34982
   416
              | SOME sexp' => do_notin_ctype_fv Plus sexp' C1)
blanchet@34982
   417
          |> do_notin_ctype_fv Minus sexp C2
blanchet@33192
   418
  | do_notin_ctype_fv sn sexp (CPair (C1, C2)) accum =
blanchet@33192
   419
    accum |> fold (do_notin_ctype_fv sn sexp) [C1, C2]
blanchet@33192
   420
  | do_notin_ctype_fv sn sexp (CType (_, Cs)) accum =
blanchet@33192
   421
    accum |> fold (do_notin_ctype_fv sn sexp) Cs
blanchet@33192
   422
  | do_notin_ctype_fv _ _ C _ =
blanchet@33232
   423
    raise CTYPE ("Nitpick_Mono.do_notin_ctype_fv", [C])
blanchet@33192
   424
blanchet@33192
   425
(* sign -> ctype -> constraint_set -> constraint_set *)
blanchet@33192
   426
fun add_notin_ctype_fv _ _ UnsolvableCSet = UnsolvableCSet
blanchet@33192
   427
  | add_notin_ctype_fv sn C (CSet (lits, comps, sexps)) =
blanchet@33192
   428
    (print_g ("*** Add " ^ string_for_ctype C ^ " is right-" ^
blanchet@34982
   429
              (case sn of Minus => "unique" | Plus => "total") ^ ".");
blanchet@33192
   430
     case do_notin_ctype_fv sn [] C (SOME (lits, sexps)) of
blanchet@33192
   431
       NONE => (print_g "**** Unsolvable"; UnsolvableCSet)
blanchet@33192
   432
     | SOME (lits, sexps) => CSet (lits, comps, sexps))
blanchet@33192
   433
blanchet@33192
   434
(* ctype -> constraint_set -> constraint_set *)
blanchet@34982
   435
val add_ctype_is_right_unique = add_notin_ctype_fv Minus
blanchet@34982
   436
val add_ctype_is_right_total = add_notin_ctype_fv Plus
blanchet@33192
   437
blanchet@33192
   438
(* constraint_set -> constraint_set -> constraint_set *)
blanchet@33192
   439
fun unite (CSet (lits1, comps1, sexps1)) (CSet (lits2, comps2, sexps2)) =
blanchet@33192
   440
    (case SOME lits1 |> fold do_literal lits2 of
blanchet@33192
   441
       NONE => (print_g "**** Unsolvable"; UnsolvableCSet)
blanchet@33192
   442
     | SOME lits => CSet (lits, comps1 @ comps2, sexps1 @ sexps2))
blanchet@33192
   443
  | unite _ _ = UnsolvableCSet
blanchet@33192
   444
blanchet@33192
   445
(* sign -> bool *)
blanchet@34982
   446
fun bool_from_sign Plus = false
blanchet@34982
   447
  | bool_from_sign Minus = true
blanchet@33192
   448
(* bool -> sign *)
blanchet@34982
   449
fun sign_from_bool false = Plus
blanchet@34982
   450
  | sign_from_bool true = Minus
blanchet@33192
   451
blanchet@33192
   452
(* literal -> PropLogic.prop_formula *)
blanchet@33192
   453
fun prop_for_literal (x, sn) =
blanchet@33192
   454
  (not (bool_from_sign sn) ? PropLogic.Not) (PropLogic.BoolVar x)
blanchet@33192
   455
(* sign_atom -> PropLogic.prop_formula *)
blanchet@33192
   456
fun prop_for_sign_atom_eq (S sn', sn) =
blanchet@33192
   457
    if sn = sn' then PropLogic.True else PropLogic.False
blanchet@33192
   458
  | prop_for_sign_atom_eq (V x, sn) = prop_for_literal (x, sn)
blanchet@33192
   459
(* sign_expr -> PropLogic.prop_formula *)
blanchet@33192
   460
fun prop_for_sign_expr xs = PropLogic.exists (map prop_for_literal xs)
blanchet@33192
   461
(* var list -> sign -> PropLogic.prop_formula *)
blanchet@33192
   462
fun prop_for_exists_eq xs sn =
blanchet@33192
   463
  PropLogic.exists (map (fn x => prop_for_literal (x, sn)) xs)
blanchet@33192
   464
(* comp -> PropLogic.prop_formula *)
blanchet@33192
   465
fun prop_for_comp (a1, a2, Eq, []) =
blanchet@33192
   466
    PropLogic.SAnd (prop_for_comp (a1, a2, Leq, []),
blanchet@33192
   467
                    prop_for_comp (a2, a1, Leq, []))
blanchet@33192
   468
  | prop_for_comp (a1, a2, Leq, []) =
blanchet@34982
   469
    PropLogic.SOr (prop_for_sign_atom_eq (a1, Plus),
blanchet@34982
   470
                   prop_for_sign_atom_eq (a2, Minus))
blanchet@33192
   471
  | prop_for_comp (a1, a2, cmp, xs) =
blanchet@34982
   472
    PropLogic.SOr (prop_for_exists_eq xs Minus, prop_for_comp (a1, a2, cmp, []))
blanchet@33192
   473
blanchet@33192
   474
(* var -> (int -> bool option) -> literal list -> literal list *)
blanchet@34123
   475
fun literals_from_assignments max_var assigns lits =
blanchet@33192
   476
  fold (fn x => fn accum =>
blanchet@33192
   477
           if AList.defined (op =) lits x then
blanchet@33192
   478
             accum
blanchet@34123
   479
           else case assigns x of
blanchet@33192
   480
             SOME b => (x, sign_from_bool b) :: accum
blanchet@33192
   481
           | NONE => accum) (max_var downto 1) lits
blanchet@33192
   482
blanchet@33192
   483
(* literal list -> sign_atom -> sign option *)
blanchet@33192
   484
fun lookup_sign_atom _ (S sn) = SOME sn
blanchet@33192
   485
  | lookup_sign_atom lit (V x) = AList.lookup (op =) lit x
blanchet@33192
   486
blanchet@33192
   487
(* comp -> string *)
blanchet@33192
   488
fun string_for_comp (a1, a2, cmp, xs) =
blanchet@33192
   489
  string_for_sign_atom a1 ^ " " ^ string_for_comp_op cmp ^
blanchet@33192
   490
  subscript_string_for_vars " \<and> " xs ^ " " ^ string_for_sign_atom a2
blanchet@33192
   491
blanchet@33192
   492
(* literal list -> comp list -> sign_expr list -> unit *)
blanchet@33192
   493
fun print_problem lits comps sexps =
blanchet@33192
   494
  print_g ("*** Problem:\n" ^ cat_lines (map string_for_literal lits @
blanchet@33192
   495
                                         map string_for_comp comps @
blanchet@33192
   496
                                         map string_for_sign_expr sexps))
blanchet@33192
   497
blanchet@33192
   498
(* literal list -> unit *)
blanchet@33192
   499
fun print_solution lits =
blanchet@34982
   500
  let val (pos, neg) = List.partition (curry (op =) Plus o snd) lits in
blanchet@33192
   501
    print_g ("*** Solution:\n" ^
blanchet@33192
   502
             "+: " ^ commas (map (string_for_var o fst) pos) ^ "\n" ^
blanchet@33192
   503
             "-: " ^ commas (map (string_for_var o fst) neg))
blanchet@33192
   504
  end
blanchet@33192
   505
blanchet@33192
   506
(* var -> constraint_set -> literal list list option *)
blanchet@33192
   507
fun solve _ UnsolvableCSet = (print_g "*** Problem: Unsolvable"; NONE)
blanchet@33192
   508
  | solve max_var (CSet (lits, comps, sexps)) =
blanchet@33192
   509
    let
blanchet@33192
   510
      val _ = print_problem lits comps sexps
blanchet@33192
   511
      val prop = PropLogic.all (map prop_for_literal lits @
blanchet@33192
   512
                                map prop_for_comp comps @
blanchet@33192
   513
                                map prop_for_sign_expr sexps)
blanchet@34123
   514
      (* use the first ML solver (to avoid startup overhead) *)
blanchet@34123
   515
      val solvers = !SatSolver.solvers
blanchet@34123
   516
                    |> filter (member (op =) ["dptsat", "dpll"] o fst)
blanchet@33192
   517
    in
blanchet@34123
   518
      case snd (hd solvers) prop of
blanchet@34123
   519
        SatSolver.SATISFIABLE assigns =>
blanchet@34123
   520
        SOME (literals_from_assignments max_var assigns lits
blanchet@33192
   521
              |> tap print_solution)
blanchet@33192
   522
      | _ => NONE
blanchet@33192
   523
    end
blanchet@33192
   524
blanchet@33192
   525
(* var -> constraint_set -> bool *)
blanchet@33192
   526
val is_solvable = is_some oo solve
blanchet@33192
   527
blanchet@33192
   528
type ctype_schema = ctype * constraint_set
blanchet@33192
   529
type ctype_context =
blanchet@33192
   530
  {bounds: ctype list,
blanchet@33192
   531
   frees: (styp * ctype) list,
blanchet@34982
   532
   consts: (styp * ctype) list}
blanchet@33192
   533
blanchet@33192
   534
type accumulator = ctype_context * constraint_set
blanchet@33192
   535
blanchet@33192
   536
val initial_gamma = {bounds = [], frees = [], consts = []}
blanchet@33192
   537
val unsolvable_accum = (initial_gamma, UnsolvableCSet)
blanchet@33192
   538
blanchet@33192
   539
(* ctype -> ctype_context -> ctype_context *)
blanchet@33192
   540
fun push_bound C {bounds, frees, consts} =
blanchet@33192
   541
  {bounds = C :: bounds, frees = frees, consts = consts}
blanchet@33192
   542
(* ctype_context -> ctype_context *)
blanchet@33192
   543
fun pop_bound {bounds, frees, consts} =
blanchet@33192
   544
  {bounds = tl bounds, frees = frees, consts = consts}
blanchet@33192
   545
  handle List.Empty => initial_gamma
blanchet@33192
   546
blanchet@33192
   547
(* cdata -> term -> accumulator -> ctype * accumulator *)
blanchet@35070
   548
fun consider_term (cdata as {hol_ctxt as {ctxt, thy, def_table, ...}, alpha_T,
blanchet@33192
   549
                             max_fresh, ...}) =
blanchet@33192
   550
  let
blanchet@33192
   551
    (* typ -> ctype *)
blanchet@33192
   552
    val ctype_for = fresh_ctype_for_type cdata
blanchet@33192
   553
    (* ctype -> ctype *)
blanchet@33192
   554
    fun pos_set_ctype_for_dom C =
blanchet@34982
   555
      CFun (C, S (if exists_alpha_sub_ctype C then Plus else Minus), bool_C)
blanchet@33192
   556
    (* typ -> accumulator -> ctype * accumulator *)
blanchet@33192
   557
    fun do_quantifier T (gamma, cset) =
blanchet@33192
   558
      let
blanchet@33192
   559
        val abs_C = ctype_for (domain_type (domain_type T))
blanchet@33192
   560
        val body_C = ctype_for (range_type T)
blanchet@33192
   561
      in
blanchet@34982
   562
        (CFun (CFun (abs_C, S Minus, body_C), S Minus, body_C),
blanchet@33192
   563
         (gamma, cset |> add_ctype_is_right_total abs_C))
blanchet@33192
   564
      end
blanchet@33192
   565
    fun do_equals T (gamma, cset) =
blanchet@33192
   566
      let val C = ctype_for (domain_type T) in
blanchet@34982
   567
        (CFun (C, S Minus, CFun (C, V (Unsynchronized.inc max_fresh),
blanchet@34982
   568
                                 ctype_for (nth_range_type 2 T))),
blanchet@33192
   569
         (gamma, cset |> add_ctype_is_right_unique C))
blanchet@33192
   570
      end
blanchet@33192
   571
    fun do_robust_set_operation T (gamma, cset) =
blanchet@33192
   572
      let
blanchet@33192
   573
        val set_T = domain_type T
blanchet@33192
   574
        val C1 = ctype_for set_T
blanchet@33192
   575
        val C2 = ctype_for set_T
blanchet@33192
   576
        val C3 = ctype_for set_T
blanchet@33192
   577
      in
blanchet@34982
   578
        (CFun (C1, S Minus, CFun (C2, S Minus, C3)),
blanchet@33192
   579
         (gamma, cset |> add_is_sub_ctype C1 C3 |> add_is_sub_ctype C2 C3))
blanchet@33192
   580
      end
blanchet@33192
   581
    fun do_fragile_set_operation T (gamma, cset) =
blanchet@33192
   582
      let
blanchet@33192
   583
        val set_T = domain_type T
blanchet@33192
   584
        val set_C = ctype_for set_T
blanchet@33192
   585
        (* typ -> ctype *)
blanchet@33192
   586
        fun custom_ctype_for (T as Type ("fun", [T1, T2])) =
blanchet@33192
   587
            if T = set_T then set_C
blanchet@34982
   588
            else CFun (custom_ctype_for T1, S Minus, custom_ctype_for T2)
blanchet@33192
   589
          | custom_ctype_for T = ctype_for T
blanchet@33192
   590
      in
blanchet@33192
   591
        (custom_ctype_for T, (gamma, cset |> add_ctype_is_right_unique set_C))
blanchet@33192
   592
      end
blanchet@33192
   593
    (* typ -> accumulator -> ctype * accumulator *)
blanchet@33192
   594
    fun do_pair_constr T accum =
blanchet@33192
   595
      case ctype_for (nth_range_type 2 T) of
blanchet@33192
   596
        C as CPair (a_C, b_C) =>
blanchet@34982
   597
        (CFun (a_C, S Minus, CFun (b_C, S Minus, C)), accum)
blanchet@33232
   598
      | C => raise CTYPE ("Nitpick_Mono.consider_term.do_pair_constr", [C])
blanchet@33192
   599
    (* int -> typ -> accumulator -> ctype * accumulator *)
blanchet@33192
   600
    fun do_nth_pair_sel n T =
blanchet@33192
   601
      case ctype_for (domain_type T) of
blanchet@33192
   602
        C as CPair (a_C, b_C) =>
blanchet@34982
   603
        pair (CFun (C, S Minus, if n = 0 then a_C else b_C))
blanchet@33232
   604
      | C => raise CTYPE ("Nitpick_Mono.consider_term.do_nth_pair_sel", [C])
blanchet@33192
   605
    val unsolvable = (CType ("unsolvable", []), unsolvable_accum)
blanchet@33192
   606
    (* typ -> term -> accumulator -> ctype * accumulator *)
blanchet@33192
   607
    fun do_bounded_quantifier abs_T bound_t body_t accum =
blanchet@33192
   608
      let
blanchet@33192
   609
        val abs_C = ctype_for abs_T
blanchet@33192
   610
        val (bound_C, accum) = accum |>> push_bound abs_C |> do_term bound_t
blanchet@33192
   611
        val expected_bound_C = pos_set_ctype_for_dom abs_C
blanchet@33192
   612
      in
blanchet@33192
   613
        accum ||> add_ctypes_equal expected_bound_C bound_C |> do_term body_t
blanchet@33192
   614
              ||> apfst pop_bound
blanchet@33192
   615
      end
blanchet@33192
   616
    (* term -> accumulator -> ctype * accumulator *)
blanchet@33192
   617
    and do_term _ (_, UnsolvableCSet) = unsolvable
blanchet@33192
   618
      | do_term t (accum as (gamma as {bounds, frees, consts}, cset)) =
blanchet@33192
   619
        (case t of
blanchet@33192
   620
           Const (x as (s, T)) =>
blanchet@33192
   621
           (case AList.lookup (op =) consts x of
blanchet@34982
   622
              SOME C => (C, accum)
blanchet@33192
   623
            | NONE =>
blanchet@33192
   624
              if not (could_exist_alpha_subtype alpha_T T) then
blanchet@33192
   625
                (ctype_for T, accum)
blanchet@33192
   626
              else case s of
blanchet@33192
   627
                @{const_name all} => do_quantifier T accum
blanchet@33192
   628
              | @{const_name "=="} => do_equals T accum
blanchet@33192
   629
              | @{const_name All} => do_quantifier T accum
blanchet@33192
   630
              | @{const_name Ex} => do_quantifier T accum
blanchet@33192
   631
              | @{const_name "op ="} => do_equals T accum
blanchet@33192
   632
              | @{const_name The} => (print_g "*** The"; unsolvable)
blanchet@33192
   633
              | @{const_name Eps} => (print_g "*** Eps"; unsolvable)
blanchet@33192
   634
              | @{const_name If} =>
blanchet@33192
   635
                do_robust_set_operation (range_type T) accum
blanchet@34982
   636
                |>> curry3 CFun bool_C (S Minus)
blanchet@33192
   637
              | @{const_name Pair} => do_pair_constr T accum
blanchet@33192
   638
              | @{const_name fst} => do_nth_pair_sel 0 T accum
blanchet@33192
   639
              | @{const_name snd} => do_nth_pair_sel 1 T accum 
blanchet@33192
   640
              | @{const_name Id} =>
blanchet@34982
   641
                (CFun (ctype_for (domain_type T), S Minus, bool_C), accum)
blanchet@33192
   642
              | @{const_name insert} =>
blanchet@33192
   643
                let
blanchet@33192
   644
                  val set_T = domain_type (range_type T)
blanchet@33192
   645
                  val C1 = ctype_for (domain_type set_T)
blanchet@33192
   646
                  val C1' = pos_set_ctype_for_dom C1
blanchet@33192
   647
                  val C2 = ctype_for set_T
blanchet@33192
   648
                  val C3 = ctype_for set_T
blanchet@33192
   649
                in
blanchet@34982
   650
                  (CFun (C1, S Minus, CFun (C2, S Minus, C3)),
blanchet@33192
   651
                   (gamma, cset |> add_ctype_is_right_unique C1
blanchet@33192
   652
                                |> add_is_sub_ctype C1' C3
blanchet@33192
   653
                                |> add_is_sub_ctype C2 C3))
blanchet@33192
   654
                end
blanchet@33192
   655
              | @{const_name converse} =>
blanchet@33192
   656
                let
blanchet@33192
   657
                  val x = Unsynchronized.inc max_fresh
blanchet@33192
   658
                  (* typ -> ctype *)
blanchet@33192
   659
                  fun ctype_for_set T =
blanchet@33192
   660
                    CFun (ctype_for (domain_type T), V x, bool_C)
blanchet@33192
   661
                  val ab_set_C = domain_type T |> ctype_for_set
blanchet@33192
   662
                  val ba_set_C = range_type T |> ctype_for_set
blanchet@34982
   663
                in (CFun (ab_set_C, S Minus, ba_set_C), accum) end
blanchet@33192
   664
              | @{const_name trancl} => do_fragile_set_operation T accum
blanchet@33192
   665
              | @{const_name rtrancl} => (print_g "*** rtrancl"; unsolvable)
haftmann@35028
   666
              | @{const_name semilattice_inf_fun_inst.inf_fun} =>
blanchet@33192
   667
                do_robust_set_operation T accum
haftmann@35028
   668
              | @{const_name semilattice_sup_fun_inst.sup_fun} =>
blanchet@33192
   669
                do_robust_set_operation T accum
blanchet@33192
   670
              | @{const_name finite} =>
blanchet@33192
   671
                let val C1 = ctype_for (domain_type (domain_type T)) in
blanchet@34982
   672
                  (CFun (pos_set_ctype_for_dom C1, S Minus, bool_C), accum)
blanchet@33192
   673
                end
blanchet@33192
   674
              | @{const_name rel_comp} =>
blanchet@33192
   675
                let
blanchet@33192
   676
                  val x = Unsynchronized.inc max_fresh
blanchet@33192
   677
                  (* typ -> ctype *)
blanchet@33192
   678
                  fun ctype_for_set T =
blanchet@33192
   679
                    CFun (ctype_for (domain_type T), V x, bool_C)
blanchet@33192
   680
                  val bc_set_C = domain_type T |> ctype_for_set
blanchet@33192
   681
                  val ab_set_C = domain_type (range_type T) |> ctype_for_set
blanchet@33192
   682
                  val ac_set_C = nth_range_type 2 T |> ctype_for_set
blanchet@33192
   683
                in
blanchet@34982
   684
                  (CFun (bc_set_C, S Minus, CFun (ab_set_C, S Minus, ac_set_C)),
blanchet@33192
   685
                   accum)
blanchet@33192
   686
                end
blanchet@33192
   687
              | @{const_name image} =>
blanchet@33192
   688
                let
blanchet@33192
   689
                  val a_C = ctype_for (domain_type (domain_type T))
blanchet@33192
   690
                  val b_C = ctype_for (range_type (domain_type T))
blanchet@33192
   691
                in
blanchet@34982
   692
                  (CFun (CFun (a_C, S Minus, b_C), S Minus,
blanchet@34982
   693
                         CFun (pos_set_ctype_for_dom a_C, S Minus,
blanchet@33192
   694
                               pos_set_ctype_for_dom b_C)), accum)
blanchet@33192
   695
                end
blanchet@33192
   696
              | @{const_name Sigma} =>
blanchet@33192
   697
                let
blanchet@33192
   698
                  val x = Unsynchronized.inc max_fresh
blanchet@33192
   699
                  (* typ -> ctype *)
blanchet@33192
   700
                  fun ctype_for_set T =
blanchet@33192
   701
                    CFun (ctype_for (domain_type T), V x, bool_C)
blanchet@33192
   702
                  val a_set_T = domain_type T
blanchet@33192
   703
                  val a_C = ctype_for (domain_type a_set_T)
blanchet@33192
   704
                  val b_set_C = ctype_for_set (range_type (domain_type
blanchet@33192
   705
                                                               (range_type T)))
blanchet@33192
   706
                  val a_set_C = ctype_for_set a_set_T
blanchet@34982
   707
                  val a_to_b_set_C = CFun (a_C, S Minus, b_set_C)
blanchet@33192
   708
                  val ab_set_C = ctype_for_set (nth_range_type 2 T)
blanchet@33192
   709
                in
blanchet@34982
   710
                  (CFun (a_set_C, S Minus,
blanchet@34982
   711
                         CFun (a_to_b_set_C, S Minus, ab_set_C)), accum)
blanchet@33192
   712
                end
blanchet@33192
   713
              | @{const_name minus_fun_inst.minus_fun} =>
blanchet@33192
   714
                let
blanchet@33192
   715
                  val set_T = domain_type T
blanchet@33192
   716
                  val left_set_C = ctype_for set_T
blanchet@33192
   717
                  val right_set_C = ctype_for set_T
blanchet@33192
   718
                in
blanchet@34982
   719
                  (CFun (left_set_C, S Minus,
blanchet@34982
   720
                         CFun (right_set_C, S Minus, left_set_C)),
blanchet@33192
   721
                   (gamma, cset |> add_ctype_is_right_unique right_set_C
blanchet@33574
   722
                                |> add_is_sub_ctype right_set_C left_set_C))
blanchet@33192
   723
                end
blanchet@33192
   724
              | @{const_name ord_fun_inst.less_eq_fun} =>
blanchet@33192
   725
                do_fragile_set_operation T accum
blanchet@33192
   726
              | @{const_name Tha} =>
blanchet@33192
   727
                let
blanchet@33192
   728
                  val a_C = ctype_for (domain_type (domain_type T))
blanchet@33192
   729
                  val a_set_C = pos_set_ctype_for_dom a_C
blanchet@34982
   730
                in (CFun (a_set_C, S Minus, a_C), accum) end
blanchet@33192
   731
              | @{const_name FunBox} =>
blanchet@33192
   732
                let val dom_C = ctype_for (domain_type T) in
blanchet@34982
   733
                  (CFun (dom_C, S Minus, dom_C), accum)
blanchet@33192
   734
                end
blanchet@33192
   735
              | _ => if is_sel s then
blanchet@33192
   736
                       if constr_name_for_sel_like s = @{const_name FunBox} then
blanchet@33192
   737
                         let val dom_C = ctype_for (domain_type T) in
blanchet@34982
   738
                           (CFun (dom_C, S Minus, dom_C), accum)
blanchet@33192
   739
                         end
blanchet@33192
   740
                       else
blanchet@33192
   741
                         (ctype_for_sel cdata x, accum)
blanchet@33192
   742
                     else if is_constr thy x then
blanchet@33192
   743
                       (ctype_for_constr cdata x, accum)
blanchet@33192
   744
                     else if is_built_in_const true x then
blanchet@33192
   745
                       case def_of_const thy def_table x of
blanchet@33192
   746
                         SOME t' => do_term t' accum
blanchet@33192
   747
                       | NONE => (print_g ("*** built-in " ^ s); unsolvable)
blanchet@33192
   748
                     else
blanchet@34982
   749
                       let val C = ctype_for T in
blanchet@34982
   750
                         (C, ({bounds = bounds, frees = frees,
blanchet@34982
   751
                               consts = (x, C) :: consts}, cset))
blanchet@34982
   752
                       end)
blanchet@33192
   753
         | Free (x as (_, T)) =>
blanchet@33192
   754
           (case AList.lookup (op =) frees x of
blanchet@33192
   755
              SOME C => (C, accum)
blanchet@33192
   756
            | NONE =>
blanchet@33192
   757
              let val C = ctype_for T in
blanchet@33192
   758
                (C, ({bounds = bounds, frees = (x, C) :: frees,
blanchet@33192
   759
                      consts = consts}, cset))
blanchet@33192
   760
              end)
blanchet@33192
   761
         | Var _ => (print_g "*** Var"; unsolvable)
blanchet@33192
   762
         | Bound j => (nth bounds j, accum)
blanchet@33192
   763
         | Abs (_, T, @{const False}) => (ctype_for (T --> bool_T), accum)
blanchet@33192
   764
         | Abs (s, T, t') =>
blanchet@34998
   765
           ((case t' of
blanchet@34998
   766
               t1' $ Bound 0 =>
blanchet@34998
   767
               if not (loose_bvar1 (t1', 0)) then
blanchet@34998
   768
                 do_term (incr_boundvars ~1 t1') accum
blanchet@34998
   769
               else
blanchet@34998
   770
                 raise SAME ()
blanchet@34998
   771
             | _ => raise SAME ())
blanchet@34998
   772
            handle SAME () =>
blanchet@34998
   773
                   let
blanchet@34998
   774
                     val C = ctype_for T
blanchet@34998
   775
                     val (C', accum) = do_term t' (accum |>> push_bound C)
blanchet@34998
   776
                   in (CFun (C, S Minus, C'), accum |>> pop_bound) end)
blanchet@33192
   777
         | Const (@{const_name All}, _)
blanchet@33192
   778
           $ Abs (_, T', @{const "op -->"} $ (t1 $ Bound 0) $ t2) =>
blanchet@33192
   779
           do_bounded_quantifier T' t1 t2 accum
blanchet@33192
   780
         | Const (@{const_name Ex}, _)
blanchet@33192
   781
           $ Abs (_, T', @{const "op &"} $ (t1 $ Bound 0) $ t2) =>
blanchet@33192
   782
           do_bounded_quantifier T' t1 t2 accum
blanchet@33192
   783
         | Const (@{const_name Let}, _) $ t1 $ t2 =>
blanchet@33192
   784
           do_term (betapply (t2, t1)) accum
blanchet@33192
   785
         | t1 $ t2 =>
blanchet@33192
   786
           let
blanchet@33192
   787
             val (C1, accum) = do_term t1 accum
blanchet@33192
   788
             val (C2, accum) = do_term t2 accum
blanchet@33192
   789
           in
blanchet@33192
   790
             case accum of
blanchet@33192
   791
               (_, UnsolvableCSet) => unsolvable
blanchet@33192
   792
             | _ => case C1 of
blanchet@33192
   793
                      CFun (C11, _, C12) =>
blanchet@33192
   794
                      (C12, accum ||> add_is_sub_ctype C2 C11)
blanchet@33232
   795
                    | _ => raise CTYPE ("Nitpick_Mono.consider_term.do_term \
blanchet@33192
   796
                                        \(op $)", [C1])
blanchet@33192
   797
           end)
blanchet@33192
   798
        |> tap (fn (C, _) =>
blanchet@33192
   799
                   print_g ("  \<Gamma> \<turnstile> " ^
blanchet@33192
   800
                            Syntax.string_of_term ctxt t ^ " : " ^
blanchet@33192
   801
                            string_for_ctype C))
blanchet@33192
   802
  in do_term end
blanchet@33192
   803
blanchet@33192
   804
(* cdata -> sign -> term -> accumulator -> accumulator *)
blanchet@35070
   805
fun consider_general_formula (cdata as {hol_ctxt as {ctxt, ...}, ...}) =
blanchet@33192
   806
  let
blanchet@33192
   807
    (* typ -> ctype *)
blanchet@33192
   808
    val ctype_for = fresh_ctype_for_type cdata
blanchet@33732
   809
    (* term -> accumulator -> ctype * accumulator *)
blanchet@33732
   810
    val do_term = consider_term cdata
blanchet@33192
   811
    (* sign -> term -> accumulator -> accumulator *)
blanchet@33192
   812
    fun do_formula _ _ (_, UnsolvableCSet) = unsolvable_accum
blanchet@33192
   813
      | do_formula sn t (accum as (gamma as {bounds, frees, consts}, cset)) =
blanchet@33192
   814
        let
blanchet@33192
   815
          (* term -> accumulator -> accumulator *)
blanchet@33192
   816
          val do_co_formula = do_formula sn
blanchet@33192
   817
          val do_contra_formula = do_formula (negate sn)
blanchet@33192
   818
          (* string -> typ -> term -> accumulator *)
blanchet@33192
   819
          fun do_quantifier quant_s abs_T body_t =
blanchet@33192
   820
            let
blanchet@33192
   821
              val abs_C = ctype_for abs_T
blanchet@34982
   822
              val side_cond = ((sn = Minus) = (quant_s = @{const_name Ex}))
blanchet@33192
   823
              val cset = cset |> side_cond ? add_ctype_is_right_total abs_C
blanchet@33192
   824
            in
blanchet@33192
   825
              (gamma |> push_bound abs_C, cset) |> do_co_formula body_t
blanchet@33192
   826
                                                |>> pop_bound
blanchet@33192
   827
            end
blanchet@33192
   828
          (* typ -> term -> accumulator *)
blanchet@33192
   829
          fun do_bounded_quantifier abs_T body_t =
blanchet@33192
   830
            accum |>> push_bound (ctype_for abs_T) |> do_co_formula body_t
blanchet@33192
   831
                  |>> pop_bound
blanchet@33192
   832
          (* term -> term -> accumulator *)
blanchet@33192
   833
          fun do_equals t1 t2 =
blanchet@33192
   834
            case sn of
blanchet@34982
   835
              Plus => do_term t accum |> snd
blanchet@34982
   836
            | Minus => let
blanchet@34982
   837
                         val (C1, accum) = do_term t1 accum
blanchet@34982
   838
                         val (C2, accum) = do_term t2 accum
blanchet@34982
   839
                       in accum ||> add_ctypes_equal C1 C2 end
blanchet@33192
   840
        in
blanchet@33192
   841
          case t of
blanchet@33192
   842
            Const (s0 as @{const_name all}, _) $ Abs (_, T1, t1) =>
blanchet@33192
   843
            do_quantifier s0 T1 t1
blanchet@33192
   844
          | Const (@{const_name "=="}, _) $ t1 $ t2 => do_equals t1 t2
blanchet@33192
   845
          | @{const "==>"} $ t1 $ t2 =>
blanchet@33192
   846
            accum |> do_contra_formula t1 |> do_co_formula t2
blanchet@33192
   847
          | @{const Trueprop} $ t1 => do_co_formula t1 accum
blanchet@33192
   848
          | @{const Not} $ t1 => do_contra_formula t1 accum
blanchet@33192
   849
          | Const (@{const_name All}, _)
blanchet@33192
   850
            $ Abs (_, T1, t1 as @{const "op -->"} $ (_ $ Bound 0) $ _) =>
blanchet@33192
   851
            do_bounded_quantifier T1 t1
blanchet@33192
   852
          | Const (s0 as @{const_name All}, _) $ Abs (_, T1, t1) =>
blanchet@33192
   853
            do_quantifier s0 T1 t1
blanchet@33192
   854
          | Const (@{const_name Ex}, _)
blanchet@33192
   855
            $ Abs (_, T1, t1 as @{const "op &"} $ (_ $ Bound 0) $ _) =>
blanchet@33192
   856
            do_bounded_quantifier T1 t1
blanchet@33192
   857
          | Const (s0 as @{const_name Ex}, _) $ Abs (_, T1, t1) =>
blanchet@33192
   858
            do_quantifier s0 T1 t1
blanchet@33192
   859
          | Const (@{const_name "op ="}, _) $ t1 $ t2 => do_equals t1 t2
blanchet@33192
   860
          | @{const "op &"} $ t1 $ t2 =>
blanchet@33192
   861
            accum |> do_co_formula t1 |> do_co_formula t2
blanchet@33192
   862
          | @{const "op |"} $ t1 $ t2 =>
blanchet@33192
   863
            accum |> do_co_formula t1 |> do_co_formula t2
blanchet@33192
   864
          | @{const "op -->"} $ t1 $ t2 =>
blanchet@33192
   865
            accum |> do_contra_formula t1 |> do_co_formula t2
blanchet@33192
   866
          | Const (@{const_name If}, _) $ t1 $ t2 $ t3 =>
blanchet@33732
   867
            accum |> do_term t1 |> snd |> fold do_co_formula [t2, t3]
blanchet@33192
   868
          | Const (@{const_name Let}, _) $ t1 $ t2 =>
blanchet@33192
   869
            do_co_formula (betapply (t2, t1)) accum
blanchet@33732
   870
          | _ => do_term t accum |> snd
blanchet@33192
   871
        end
blanchet@33192
   872
        |> tap (fn _ => print_g ("\<Gamma> \<turnstile> " ^
blanchet@33192
   873
                                 Syntax.string_of_term ctxt t ^
blanchet@33192
   874
                                 " : o\<^sup>" ^ string_for_sign sn))
blanchet@33192
   875
  in do_formula end
blanchet@33192
   876
blanchet@33192
   877
(* The harmless axiom optimization below is somewhat too aggressive in the face
blanchet@33192
   878
   of (rather peculiar) user-defined axioms. *)
blanchet@33192
   879
val harmless_consts =
blanchet@33192
   880
  [@{const_name ord_class.less}, @{const_name ord_class.less_eq}]
blanchet@33192
   881
val bounteous_consts = [@{const_name bisim}]
blanchet@33192
   882
blanchet@33192
   883
(* term -> bool *)
blanchet@33192
   884
fun is_harmless_axiom t =
blanchet@33192
   885
  Term.add_consts t [] |> filter_out (is_built_in_const true)
blanchet@33192
   886
  |> (forall (member (op =) harmless_consts o original_name o fst)
blanchet@33192
   887
      orf exists (member (op =) bounteous_consts o fst))
blanchet@33192
   888
blanchet@33192
   889
(* cdata -> sign -> term -> accumulator -> accumulator *)
blanchet@33192
   890
fun consider_nondefinitional_axiom cdata sn t =
blanchet@33192
   891
  not (is_harmless_axiom t) ? consider_general_formula cdata sn t
blanchet@33192
   892
blanchet@33192
   893
(* cdata -> term -> accumulator -> accumulator *)
blanchet@35070
   894
fun consider_definitional_axiom (cdata as {hol_ctxt as {thy, ...}, ...}) t =
blanchet@33192
   895
  if not (is_constr_pattern_formula thy t) then
blanchet@34982
   896
    consider_nondefinitional_axiom cdata Plus t
blanchet@33192
   897
  else if is_harmless_axiom t then
blanchet@33192
   898
    I
blanchet@33192
   899
  else
blanchet@33192
   900
    let
blanchet@33732
   901
      (* term -> accumulator -> ctype * accumulator *)
blanchet@33192
   902
      val do_term = consider_term cdata
blanchet@33192
   903
      (* typ -> term -> accumulator -> accumulator *)
blanchet@33192
   904
      fun do_all abs_T body_t accum =
blanchet@33192
   905
        let val abs_C = fresh_ctype_for_type cdata abs_T in
blanchet@33192
   906
          accum |>> push_bound abs_C |> do_formula body_t |>> pop_bound
blanchet@33192
   907
        end
blanchet@33192
   908
      (* term -> term -> accumulator -> accumulator *)
blanchet@33192
   909
      and do_implies t1 t2 = do_term t1 #> snd #> do_formula t2
blanchet@33192
   910
      and do_equals t1 t2 accum =
blanchet@33192
   911
        let
blanchet@33192
   912
          val (C1, accum) = do_term t1 accum
blanchet@33192
   913
          val (C2, accum) = do_term t2 accum
blanchet@33192
   914
        in accum ||> add_ctypes_equal C1 C2 end
blanchet@33192
   915
      (* term -> accumulator -> accumulator *)
blanchet@33192
   916
      and do_formula _ (_, UnsolvableCSet) = unsolvable_accum
blanchet@33192
   917
        | do_formula t accum =
blanchet@33192
   918
          case t of
blanchet@33192
   919
            Const (@{const_name all}, _) $ Abs (_, T1, t1) => do_all T1 t1 accum
blanchet@33192
   920
          | @{const Trueprop} $ t1 => do_formula t1 accum
blanchet@33192
   921
          | Const (@{const_name "=="}, _) $ t1 $ t2 => do_equals t1 t2 accum
blanchet@33192
   922
          | @{const "==>"} $ t1 $ t2 => do_implies t1 t2 accum
blanchet@33192
   923
          | @{const Pure.conjunction} $ t1 $ t2 =>
blanchet@33192
   924
            accum |> do_formula t1 |> do_formula t2
blanchet@33192
   925
          | Const (@{const_name All}, _) $ Abs (_, T1, t1) => do_all T1 t1 accum
blanchet@33192
   926
          | Const (@{const_name "op ="}, _) $ t1 $ t2 => do_equals t1 t2 accum
blanchet@33192
   927
          | @{const "op &"} $ t1 $ t2 => accum |> do_formula t1 |> do_formula t2
blanchet@33192
   928
          | @{const "op -->"} $ t1 $ t2 => do_implies t1 t2 accum
blanchet@33232
   929
          | _ => raise TERM ("Nitpick_Mono.consider_definitional_axiom.\
blanchet@33192
   930
                             \do_formula", [t])
blanchet@33192
   931
    in do_formula t end
blanchet@33192
   932
blanchet@33192
   933
(* Proof.context -> literal list -> term -> ctype -> string *)
blanchet@33192
   934
fun string_for_ctype_of_term ctxt lits t C =
blanchet@33192
   935
  Syntax.string_of_term ctxt t ^ " : " ^
blanchet@33192
   936
  string_for_ctype (instantiate_ctype lits C)
blanchet@33192
   937
blanchet@33192
   938
(* theory -> literal list -> ctype_context -> unit *)
blanchet@33192
   939
fun print_ctype_context ctxt lits ({frees, consts, ...} : ctype_context) =
blanchet@33192
   940
  map (fn (x, C) => string_for_ctype_of_term ctxt lits (Free x) C) frees @
blanchet@34982
   941
  map (fn (x, C) => string_for_ctype_of_term ctxt lits (Const x) C) consts
blanchet@33192
   942
  |> cat_lines |> print_g
blanchet@33192
   943
blanchet@35190
   944
(* hol_context -> bool -> typ -> sign -> term list -> term list -> term
blanchet@35190
   945
   -> bool *)
blanchet@35190
   946
fun formulas_monotonic (hol_ctxt as {ctxt, ...}) binarize alpha_T sn def_ts
blanchet@35190
   947
                       nondef_ts core_t =
blanchet@33192
   948
  let
blanchet@33192
   949
    val _ = print_g ("****** " ^ string_for_ctype CAlpha ^ " is " ^
blanchet@33192
   950
                     Syntax.string_of_typ ctxt alpha_T)
blanchet@35190
   951
    val cdata as {max_fresh, ...} = initial_cdata hol_ctxt binarize alpha_T
blanchet@33192
   952
    val (gamma, cset) =
blanchet@33192
   953
      (initial_gamma, slack)
blanchet@33192
   954
      |> fold (consider_definitional_axiom cdata) def_ts
blanchet@34982
   955
      |> fold (consider_nondefinitional_axiom cdata Plus) nondef_ts
blanchet@34982
   956
      |> consider_general_formula cdata sn core_t
blanchet@33192
   957
  in
blanchet@33192
   958
    case solve (!max_fresh) cset of
blanchet@33192
   959
      SOME lits => (print_ctype_context ctxt lits gamma; true)
blanchet@33192
   960
    | _ => false
blanchet@33192
   961
  end
blanchet@33192
   962
  handle CTYPE (loc, Cs) => raise BAD (loc, commas (map string_for_ctype Cs))
blanchet@33192
   963
blanchet@33192
   964
end;