src/HOL/Tools/Nitpick/nitpick_mono.ML
author blanchet
Wed Feb 17 20:46:50 2010 +0100 (2010-02-17)
changeset 35190 ce653cc27a94
parent 35079 592edca1dfb3
child 35219 15a5f213ef5b
permissions -rw-r--r--
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
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@35070
   192
fun fresh_ctype_for_type ({hol_ctxt as {thy, ...}, 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@35070
   221
              val xs = datatype_constrs hol_ctxt 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@34982
   273
    | NONE => if T = alpha_T then
blanchet@34982
   274
                let val C = fresh_ctype_for_type cdata T in
blanchet@34982
   275
                  (Unsynchronized.change constr_cache (cons (x, C)); C)
blanchet@34982
   276
                end
blanchet@34982
   277
              else
blanchet@34982
   278
                (fresh_ctype_for_type cdata (body_type T);
blanchet@34982
   279
                 AList.lookup (op =) (!constr_cache) x |> the)
blanchet@33192
   280
  else
blanchet@33192
   281
    fresh_ctype_for_type cdata T
blanchet@35190
   282
fun ctype_for_sel (cdata as {hol_ctxt, binarize, ...}) (x as (s, _)) =
blanchet@35190
   283
  x |> binarized_and_boxed_constr_for_sel hol_ctxt binarize
blanchet@35190
   284
    |> ctype_for_constr cdata |> sel_ctype_from_constr_ctype s
blanchet@33192
   285
blanchet@33192
   286
(* literal list -> ctype -> ctype *)
blanchet@33192
   287
fun instantiate_ctype lits =
blanchet@33192
   288
  let
blanchet@33192
   289
    (* ctype -> ctype *)
blanchet@33192
   290
    fun aux CAlpha = CAlpha
blanchet@33192
   291
      | aux (CFun (C1, V x, C2)) =
blanchet@33192
   292
        let
blanchet@33192
   293
          val a = case AList.lookup (op =) lits x of
blanchet@33192
   294
                    SOME sn => S sn
blanchet@33192
   295
                  | NONE => V x
blanchet@33192
   296
        in CFun (aux C1, a, aux C2) end
blanchet@33192
   297
      | aux (CFun (C1, a, C2)) = CFun (aux C1, a, aux C2)
blanchet@33192
   298
      | aux (CPair Cp) = CPair (pairself aux Cp)
blanchet@33192
   299
      | aux (CType (s, Cs)) = CType (s, map aux Cs)
blanchet@33192
   300
      | aux (CRec z) = CRec z
blanchet@33192
   301
  in aux end
blanchet@33192
   302
blanchet@33192
   303
datatype comp_op = Eq | Leq
blanchet@33192
   304
blanchet@33192
   305
type comp = sign_atom * sign_atom * comp_op * var list
blanchet@33192
   306
type sign_expr = literal list
blanchet@33192
   307
blanchet@33192
   308
datatype constraint_set =
blanchet@33192
   309
  UnsolvableCSet |
blanchet@33192
   310
  CSet of literal list * comp list * sign_expr list
blanchet@33192
   311
blanchet@33192
   312
(* comp_op -> string *)
blanchet@33192
   313
fun string_for_comp_op Eq = "="
blanchet@33192
   314
  | string_for_comp_op Leq = "\<le>"
blanchet@33192
   315
blanchet@33192
   316
(* sign_expr -> string *)
blanchet@33192
   317
fun string_for_sign_expr [] = "\<bot>"
blanchet@33192
   318
  | string_for_sign_expr lits =
blanchet@33192
   319
    space_implode " \<or> " (map string_for_literal lits)
blanchet@33192
   320
blanchet@33192
   321
(* constraint_set *)
blanchet@33192
   322
val slack = CSet ([], [], [])
blanchet@33192
   323
blanchet@33192
   324
(* literal -> literal list option -> literal list option *)
blanchet@33192
   325
fun do_literal _ NONE = NONE
blanchet@33192
   326
  | do_literal (x, sn) (SOME lits) =
blanchet@33192
   327
    case AList.lookup (op =) lits x of
blanchet@33192
   328
      SOME sn' => if sn = sn' then SOME lits else NONE
blanchet@33192
   329
    | NONE => SOME ((x, sn) :: lits)
blanchet@33192
   330
blanchet@33192
   331
(* comp_op -> var list -> sign_atom -> sign_atom -> literal list * comp list
blanchet@33192
   332
   -> (literal list * comp list) option *)
blanchet@33192
   333
fun do_sign_atom_comp Eq [] a1 a2 (accum as (lits, comps)) =
blanchet@33192
   334
    (case (a1, a2) of
blanchet@33192
   335
       (S sn1, S sn2) => if sn1 = sn2 then SOME accum else NONE
blanchet@33192
   336
     | (V x1, S sn2) =>
blanchet@33192
   337
       Option.map (rpair comps) (do_literal (x1, sn2) (SOME lits))
blanchet@33192
   338
     | (V _, V _) => SOME (lits, insert (op =) (a1, a2, Eq, []) comps)
blanchet@33192
   339
     | _ => do_sign_atom_comp Eq [] a2 a1 accum)
blanchet@33192
   340
  | do_sign_atom_comp Leq [] a1 a2 (accum as (lits, comps)) =
blanchet@33192
   341
    (case (a1, a2) of
blanchet@34982
   342
       (_, S Minus) => SOME accum
blanchet@34982
   343
     | (S Plus, _) => SOME accum
blanchet@34982
   344
     | (S Minus, S Plus) => NONE
blanchet@33192
   345
     | (V _, V _) => SOME (lits, insert (op =) (a1, a2, Leq, []) comps)
blanchet@33192
   346
     | _ => do_sign_atom_comp Eq [] a1 a2 accum)
blanchet@33192
   347
  | do_sign_atom_comp cmp xs a1 a2 (accum as (lits, comps)) =
blanchet@33192
   348
    SOME (lits, insert (op =) (a1, a2, cmp, xs) comps)
blanchet@33192
   349
blanchet@33192
   350
(* comp -> var list -> ctype -> ctype -> (literal list * comp list) option
blanchet@33192
   351
   -> (literal list * comp list) option *)
blanchet@33192
   352
fun do_ctype_comp _ _ _ _ NONE = NONE
blanchet@33192
   353
  | do_ctype_comp _ _ CAlpha CAlpha accum = accum
blanchet@33192
   354
  | do_ctype_comp Eq xs (CFun (C11, a1, C12)) (CFun (C21, a2, C22))
blanchet@33192
   355
                  (SOME accum) =
blanchet@33192
   356
     accum |> do_sign_atom_comp Eq xs a1 a2 |> do_ctype_comp Eq xs C11 C21
blanchet@33192
   357
           |> do_ctype_comp Eq xs C12 C22
blanchet@33192
   358
  | do_ctype_comp Leq xs (CFun (C11, a1, C12)) (CFun (C21, a2, C22))
blanchet@33192
   359
                  (SOME accum) =
blanchet@33192
   360
    (if exists_alpha_sub_ctype C11 then
blanchet@33192
   361
       accum |> do_sign_atom_comp Leq xs a1 a2
blanchet@33192
   362
             |> do_ctype_comp Leq xs C21 C11
blanchet@33192
   363
             |> (case a2 of
blanchet@34982
   364
                   S Minus => I
blanchet@34982
   365
                 | S Plus => do_ctype_comp Leq xs C11 C21
blanchet@33192
   366
                 | V x => do_ctype_comp Leq (x :: xs) C11 C21)
blanchet@33192
   367
     else
blanchet@33192
   368
       SOME accum)
blanchet@33192
   369
    |> do_ctype_comp Leq xs C12 C22
blanchet@33192
   370
  | do_ctype_comp cmp xs (C1 as CPair (C11, C12)) (C2 as CPair (C21, C22))
blanchet@33192
   371
                  accum =
blanchet@33192
   372
    (accum |> fold (uncurry (do_ctype_comp cmp xs)) [(C11, C21), (C12, C22)]
blanchet@33192
   373
     handle Library.UnequalLengths =>
blanchet@33232
   374
            raise CTYPE ("Nitpick_Mono.do_ctype_comp", [C1, C2]))
blanchet@33192
   375
  | do_ctype_comp cmp xs (CType _) (CType _) accum =
blanchet@33192
   376
    accum (* no need to compare them thanks to the cache *)
blanchet@33192
   377
  | do_ctype_comp _ _ C1 C2 _ =
blanchet@33232
   378
    raise CTYPE ("Nitpick_Mono.do_ctype_comp", [C1, C2])
blanchet@33192
   379
blanchet@33192
   380
(* comp_op -> ctype -> ctype -> constraint_set -> constraint_set *)
blanchet@33192
   381
fun add_ctype_comp _ _ _ UnsolvableCSet = UnsolvableCSet
blanchet@33192
   382
  | add_ctype_comp cmp C1 C2 (CSet (lits, comps, sexps)) =
blanchet@33192
   383
    (print_g ("*** Add " ^ string_for_ctype C1 ^ " " ^ string_for_comp_op cmp ^
blanchet@33192
   384
              " " ^ string_for_ctype C2);
blanchet@33192
   385
     case do_ctype_comp cmp [] C1 C2 (SOME (lits, comps)) of
blanchet@33192
   386
       NONE => (print_g "**** Unsolvable"; UnsolvableCSet)
blanchet@33192
   387
     | SOME (lits, comps) => CSet (lits, comps, sexps))
blanchet@33192
   388
blanchet@33192
   389
(* ctype -> ctype -> constraint_set -> constraint_set *)
blanchet@33192
   390
val add_ctypes_equal = add_ctype_comp Eq
blanchet@33192
   391
val add_is_sub_ctype = add_ctype_comp Leq
blanchet@33192
   392
blanchet@33192
   393
(* sign -> sign_expr -> ctype -> (literal list * sign_expr list) option
blanchet@33192
   394
   -> (literal list * sign_expr list) option *)
blanchet@33192
   395
fun do_notin_ctype_fv _ _ _ NONE = NONE
blanchet@34982
   396
  | do_notin_ctype_fv Minus _ CAlpha accum = accum
blanchet@34982
   397
  | do_notin_ctype_fv Plus [] CAlpha _ = NONE
blanchet@34982
   398
  | do_notin_ctype_fv Plus [(x, sn)] CAlpha (SOME (lits, sexps)) =
blanchet@33192
   399
    SOME lits |> do_literal (x, sn) |> Option.map (rpair sexps)
blanchet@34982
   400
  | do_notin_ctype_fv Plus sexp CAlpha (SOME (lits, sexps)) =
blanchet@33192
   401
    SOME (lits, insert (op =) sexp sexps)
blanchet@33192
   402
  | do_notin_ctype_fv sn sexp (CFun (C1, S sn', C2)) accum =
blanchet@34982
   403
    accum |> (if sn' = Plus andalso sn = Plus then
blanchet@34982
   404
                do_notin_ctype_fv Plus sexp C1
blanchet@34982
   405
              else
blanchet@34982
   406
                I)
blanchet@34982
   407
          |> (if sn' = Minus orelse sn = Plus then
blanchet@34982
   408
                do_notin_ctype_fv Minus sexp C1
blanchet@34982
   409
              else
blanchet@34982
   410
                I)
blanchet@33192
   411
          |> do_notin_ctype_fv sn sexp C2
blanchet@34982
   412
  | do_notin_ctype_fv Plus sexp (CFun (C1, V x, C2)) accum =
blanchet@34982
   413
    accum |> (case do_literal (x, Minus) (SOME sexp) of
blanchet@33192
   414
                NONE => I
blanchet@34982
   415
              | SOME sexp' => do_notin_ctype_fv Plus sexp' C1)
blanchet@34982
   416
          |> do_notin_ctype_fv Minus sexp C1
blanchet@34982
   417
          |> do_notin_ctype_fv Plus sexp C2
blanchet@34982
   418
  | do_notin_ctype_fv Minus sexp (CFun (C1, V x, C2)) accum =
blanchet@34982
   419
    accum |> (case do_literal (x, Plus) (SOME sexp) of
blanchet@33192
   420
                NONE => I
blanchet@34982
   421
              | SOME sexp' => do_notin_ctype_fv Plus sexp' C1)
blanchet@34982
   422
          |> do_notin_ctype_fv Minus sexp C2
blanchet@33192
   423
  | do_notin_ctype_fv sn sexp (CPair (C1, C2)) accum =
blanchet@33192
   424
    accum |> fold (do_notin_ctype_fv sn sexp) [C1, C2]
blanchet@33192
   425
  | do_notin_ctype_fv sn sexp (CType (_, Cs)) accum =
blanchet@33192
   426
    accum |> fold (do_notin_ctype_fv sn sexp) Cs
blanchet@33192
   427
  | do_notin_ctype_fv _ _ C _ =
blanchet@33232
   428
    raise CTYPE ("Nitpick_Mono.do_notin_ctype_fv", [C])
blanchet@33192
   429
blanchet@33192
   430
(* sign -> ctype -> constraint_set -> constraint_set *)
blanchet@33192
   431
fun add_notin_ctype_fv _ _ UnsolvableCSet = UnsolvableCSet
blanchet@33192
   432
  | add_notin_ctype_fv sn C (CSet (lits, comps, sexps)) =
blanchet@33192
   433
    (print_g ("*** Add " ^ string_for_ctype C ^ " is right-" ^
blanchet@34982
   434
              (case sn of Minus => "unique" | Plus => "total") ^ ".");
blanchet@33192
   435
     case do_notin_ctype_fv sn [] C (SOME (lits, sexps)) of
blanchet@33192
   436
       NONE => (print_g "**** Unsolvable"; UnsolvableCSet)
blanchet@33192
   437
     | SOME (lits, sexps) => CSet (lits, comps, sexps))
blanchet@33192
   438
blanchet@33192
   439
(* ctype -> constraint_set -> constraint_set *)
blanchet@34982
   440
val add_ctype_is_right_unique = add_notin_ctype_fv Minus
blanchet@34982
   441
val add_ctype_is_right_total = add_notin_ctype_fv Plus
blanchet@33192
   442
blanchet@33192
   443
(* constraint_set -> constraint_set -> constraint_set *)
blanchet@33192
   444
fun unite (CSet (lits1, comps1, sexps1)) (CSet (lits2, comps2, sexps2)) =
blanchet@33192
   445
    (case SOME lits1 |> fold do_literal lits2 of
blanchet@33192
   446
       NONE => (print_g "**** Unsolvable"; UnsolvableCSet)
blanchet@33192
   447
     | SOME lits => CSet (lits, comps1 @ comps2, sexps1 @ sexps2))
blanchet@33192
   448
  | unite _ _ = UnsolvableCSet
blanchet@33192
   449
blanchet@33192
   450
(* sign -> bool *)
blanchet@34982
   451
fun bool_from_sign Plus = false
blanchet@34982
   452
  | bool_from_sign Minus = true
blanchet@33192
   453
(* bool -> sign *)
blanchet@34982
   454
fun sign_from_bool false = Plus
blanchet@34982
   455
  | sign_from_bool true = Minus
blanchet@33192
   456
blanchet@33192
   457
(* literal -> PropLogic.prop_formula *)
blanchet@33192
   458
fun prop_for_literal (x, sn) =
blanchet@33192
   459
  (not (bool_from_sign sn) ? PropLogic.Not) (PropLogic.BoolVar x)
blanchet@33192
   460
(* sign_atom -> PropLogic.prop_formula *)
blanchet@33192
   461
fun prop_for_sign_atom_eq (S sn', sn) =
blanchet@33192
   462
    if sn = sn' then PropLogic.True else PropLogic.False
blanchet@33192
   463
  | prop_for_sign_atom_eq (V x, sn) = prop_for_literal (x, sn)
blanchet@33192
   464
(* sign_expr -> PropLogic.prop_formula *)
blanchet@33192
   465
fun prop_for_sign_expr xs = PropLogic.exists (map prop_for_literal xs)
blanchet@33192
   466
(* var list -> sign -> PropLogic.prop_formula *)
blanchet@33192
   467
fun prop_for_exists_eq xs sn =
blanchet@33192
   468
  PropLogic.exists (map (fn x => prop_for_literal (x, sn)) xs)
blanchet@33192
   469
(* comp -> PropLogic.prop_formula *)
blanchet@33192
   470
fun prop_for_comp (a1, a2, Eq, []) =
blanchet@33192
   471
    PropLogic.SAnd (prop_for_comp (a1, a2, Leq, []),
blanchet@33192
   472
                    prop_for_comp (a2, a1, Leq, []))
blanchet@33192
   473
  | prop_for_comp (a1, a2, Leq, []) =
blanchet@34982
   474
    PropLogic.SOr (prop_for_sign_atom_eq (a1, Plus),
blanchet@34982
   475
                   prop_for_sign_atom_eq (a2, Minus))
blanchet@33192
   476
  | prop_for_comp (a1, a2, cmp, xs) =
blanchet@34982
   477
    PropLogic.SOr (prop_for_exists_eq xs Minus, prop_for_comp (a1, a2, cmp, []))
blanchet@33192
   478
blanchet@33192
   479
(* var -> (int -> bool option) -> literal list -> literal list *)
blanchet@34123
   480
fun literals_from_assignments max_var assigns lits =
blanchet@33192
   481
  fold (fn x => fn accum =>
blanchet@33192
   482
           if AList.defined (op =) lits x then
blanchet@33192
   483
             accum
blanchet@34123
   484
           else case assigns x of
blanchet@33192
   485
             SOME b => (x, sign_from_bool b) :: accum
blanchet@33192
   486
           | NONE => accum) (max_var downto 1) lits
blanchet@33192
   487
blanchet@33192
   488
(* literal list -> sign_atom -> sign option *)
blanchet@33192
   489
fun lookup_sign_atom _ (S sn) = SOME sn
blanchet@33192
   490
  | lookup_sign_atom lit (V x) = AList.lookup (op =) lit x
blanchet@33192
   491
blanchet@33192
   492
(* comp -> string *)
blanchet@33192
   493
fun string_for_comp (a1, a2, cmp, xs) =
blanchet@33192
   494
  string_for_sign_atom a1 ^ " " ^ string_for_comp_op cmp ^
blanchet@33192
   495
  subscript_string_for_vars " \<and> " xs ^ " " ^ string_for_sign_atom a2
blanchet@33192
   496
blanchet@33192
   497
(* literal list -> comp list -> sign_expr list -> unit *)
blanchet@33192
   498
fun print_problem lits comps sexps =
blanchet@33192
   499
  print_g ("*** Problem:\n" ^ cat_lines (map string_for_literal lits @
blanchet@33192
   500
                                         map string_for_comp comps @
blanchet@33192
   501
                                         map string_for_sign_expr sexps))
blanchet@33192
   502
blanchet@33192
   503
(* literal list -> unit *)
blanchet@33192
   504
fun print_solution lits =
blanchet@34982
   505
  let val (pos, neg) = List.partition (curry (op =) Plus o snd) lits in
blanchet@33192
   506
    print_g ("*** Solution:\n" ^
blanchet@33192
   507
             "+: " ^ commas (map (string_for_var o fst) pos) ^ "\n" ^
blanchet@33192
   508
             "-: " ^ commas (map (string_for_var o fst) neg))
blanchet@33192
   509
  end
blanchet@33192
   510
blanchet@33192
   511
(* var -> constraint_set -> literal list list option *)
blanchet@33192
   512
fun solve _ UnsolvableCSet = (print_g "*** Problem: Unsolvable"; NONE)
blanchet@33192
   513
  | solve max_var (CSet (lits, comps, sexps)) =
blanchet@33192
   514
    let
blanchet@33192
   515
      val _ = print_problem lits comps sexps
blanchet@33192
   516
      val prop = PropLogic.all (map prop_for_literal lits @
blanchet@33192
   517
                                map prop_for_comp comps @
blanchet@33192
   518
                                map prop_for_sign_expr sexps)
blanchet@34123
   519
      (* use the first ML solver (to avoid startup overhead) *)
blanchet@34123
   520
      val solvers = !SatSolver.solvers
blanchet@34123
   521
                    |> filter (member (op =) ["dptsat", "dpll"] o fst)
blanchet@33192
   522
    in
blanchet@34123
   523
      case snd (hd solvers) prop of
blanchet@34123
   524
        SatSolver.SATISFIABLE assigns =>
blanchet@34123
   525
        SOME (literals_from_assignments max_var assigns lits
blanchet@33192
   526
              |> tap print_solution)
blanchet@33192
   527
      | _ => NONE
blanchet@33192
   528
    end
blanchet@33192
   529
blanchet@33192
   530
(* var -> constraint_set -> bool *)
blanchet@33192
   531
val is_solvable = is_some oo solve
blanchet@33192
   532
blanchet@33192
   533
type ctype_schema = ctype * constraint_set
blanchet@33192
   534
type ctype_context =
blanchet@33192
   535
  {bounds: ctype list,
blanchet@33192
   536
   frees: (styp * ctype) list,
blanchet@34982
   537
   consts: (styp * ctype) list}
blanchet@33192
   538
blanchet@33192
   539
type accumulator = ctype_context * constraint_set
blanchet@33192
   540
blanchet@33192
   541
val initial_gamma = {bounds = [], frees = [], consts = []}
blanchet@33192
   542
val unsolvable_accum = (initial_gamma, UnsolvableCSet)
blanchet@33192
   543
blanchet@33192
   544
(* ctype -> ctype_context -> ctype_context *)
blanchet@33192
   545
fun push_bound C {bounds, frees, consts} =
blanchet@33192
   546
  {bounds = C :: bounds, frees = frees, consts = consts}
blanchet@33192
   547
(* ctype_context -> ctype_context *)
blanchet@33192
   548
fun pop_bound {bounds, frees, consts} =
blanchet@33192
   549
  {bounds = tl bounds, frees = frees, consts = consts}
blanchet@33192
   550
  handle List.Empty => initial_gamma
blanchet@33192
   551
blanchet@33192
   552
(* cdata -> term -> accumulator -> ctype * accumulator *)
blanchet@35070
   553
fun consider_term (cdata as {hol_ctxt as {ctxt, thy, def_table, ...}, alpha_T,
blanchet@33192
   554
                             max_fresh, ...}) =
blanchet@33192
   555
  let
blanchet@33192
   556
    (* typ -> ctype *)
blanchet@33192
   557
    val ctype_for = fresh_ctype_for_type cdata
blanchet@33192
   558
    (* ctype -> ctype *)
blanchet@33192
   559
    fun pos_set_ctype_for_dom C =
blanchet@34982
   560
      CFun (C, S (if exists_alpha_sub_ctype C then Plus else Minus), bool_C)
blanchet@33192
   561
    (* typ -> accumulator -> ctype * accumulator *)
blanchet@33192
   562
    fun do_quantifier T (gamma, cset) =
blanchet@33192
   563
      let
blanchet@33192
   564
        val abs_C = ctype_for (domain_type (domain_type T))
blanchet@33192
   565
        val body_C = ctype_for (range_type T)
blanchet@33192
   566
      in
blanchet@34982
   567
        (CFun (CFun (abs_C, S Minus, body_C), S Minus, body_C),
blanchet@33192
   568
         (gamma, cset |> add_ctype_is_right_total abs_C))
blanchet@33192
   569
      end
blanchet@33192
   570
    fun do_equals T (gamma, cset) =
blanchet@33192
   571
      let val C = ctype_for (domain_type T) in
blanchet@34982
   572
        (CFun (C, S Minus, CFun (C, V (Unsynchronized.inc max_fresh),
blanchet@34982
   573
                                 ctype_for (nth_range_type 2 T))),
blanchet@33192
   574
         (gamma, cset |> add_ctype_is_right_unique C))
blanchet@33192
   575
      end
blanchet@33192
   576
    fun do_robust_set_operation T (gamma, cset) =
blanchet@33192
   577
      let
blanchet@33192
   578
        val set_T = domain_type T
blanchet@33192
   579
        val C1 = ctype_for set_T
blanchet@33192
   580
        val C2 = ctype_for set_T
blanchet@33192
   581
        val C3 = ctype_for set_T
blanchet@33192
   582
      in
blanchet@34982
   583
        (CFun (C1, S Minus, CFun (C2, S Minus, C3)),
blanchet@33192
   584
         (gamma, cset |> add_is_sub_ctype C1 C3 |> add_is_sub_ctype C2 C3))
blanchet@33192
   585
      end
blanchet@33192
   586
    fun do_fragile_set_operation T (gamma, cset) =
blanchet@33192
   587
      let
blanchet@33192
   588
        val set_T = domain_type T
blanchet@33192
   589
        val set_C = ctype_for set_T
blanchet@33192
   590
        (* typ -> ctype *)
blanchet@33192
   591
        fun custom_ctype_for (T as Type ("fun", [T1, T2])) =
blanchet@33192
   592
            if T = set_T then set_C
blanchet@34982
   593
            else CFun (custom_ctype_for T1, S Minus, custom_ctype_for T2)
blanchet@33192
   594
          | custom_ctype_for T = ctype_for T
blanchet@33192
   595
      in
blanchet@33192
   596
        (custom_ctype_for T, (gamma, cset |> add_ctype_is_right_unique set_C))
blanchet@33192
   597
      end
blanchet@33192
   598
    (* typ -> accumulator -> ctype * accumulator *)
blanchet@33192
   599
    fun do_pair_constr T accum =
blanchet@33192
   600
      case ctype_for (nth_range_type 2 T) of
blanchet@33192
   601
        C as CPair (a_C, b_C) =>
blanchet@34982
   602
        (CFun (a_C, S Minus, CFun (b_C, S Minus, C)), accum)
blanchet@33232
   603
      | C => raise CTYPE ("Nitpick_Mono.consider_term.do_pair_constr", [C])
blanchet@33192
   604
    (* int -> typ -> accumulator -> ctype * accumulator *)
blanchet@33192
   605
    fun do_nth_pair_sel n T =
blanchet@33192
   606
      case ctype_for (domain_type T) of
blanchet@33192
   607
        C as CPair (a_C, b_C) =>
blanchet@34982
   608
        pair (CFun (C, S Minus, if n = 0 then a_C else b_C))
blanchet@33232
   609
      | C => raise CTYPE ("Nitpick_Mono.consider_term.do_nth_pair_sel", [C])
blanchet@33192
   610
    val unsolvable = (CType ("unsolvable", []), unsolvable_accum)
blanchet@33192
   611
    (* typ -> term -> accumulator -> ctype * accumulator *)
blanchet@33192
   612
    fun do_bounded_quantifier abs_T bound_t body_t accum =
blanchet@33192
   613
      let
blanchet@33192
   614
        val abs_C = ctype_for abs_T
blanchet@33192
   615
        val (bound_C, accum) = accum |>> push_bound abs_C |> do_term bound_t
blanchet@33192
   616
        val expected_bound_C = pos_set_ctype_for_dom abs_C
blanchet@33192
   617
      in
blanchet@33192
   618
        accum ||> add_ctypes_equal expected_bound_C bound_C |> do_term body_t
blanchet@33192
   619
              ||> apfst pop_bound
blanchet@33192
   620
      end
blanchet@33192
   621
    (* term -> accumulator -> ctype * accumulator *)
blanchet@33192
   622
    and do_term _ (_, UnsolvableCSet) = unsolvable
blanchet@33192
   623
      | do_term t (accum as (gamma as {bounds, frees, consts}, cset)) =
blanchet@33192
   624
        (case t of
blanchet@33192
   625
           Const (x as (s, T)) =>
blanchet@33192
   626
           (case AList.lookup (op =) consts x of
blanchet@34982
   627
              SOME C => (C, accum)
blanchet@33192
   628
            | NONE =>
blanchet@33192
   629
              if not (could_exist_alpha_subtype alpha_T T) then
blanchet@33192
   630
                (ctype_for T, accum)
blanchet@33192
   631
              else case s of
blanchet@33192
   632
                @{const_name all} => do_quantifier T accum
blanchet@33192
   633
              | @{const_name "=="} => do_equals T accum
blanchet@33192
   634
              | @{const_name All} => do_quantifier T accum
blanchet@33192
   635
              | @{const_name Ex} => do_quantifier T accum
blanchet@33192
   636
              | @{const_name "op ="} => do_equals T accum
blanchet@33192
   637
              | @{const_name The} => (print_g "*** The"; unsolvable)
blanchet@33192
   638
              | @{const_name Eps} => (print_g "*** Eps"; unsolvable)
blanchet@33192
   639
              | @{const_name If} =>
blanchet@33192
   640
                do_robust_set_operation (range_type T) accum
blanchet@34982
   641
                |>> curry3 CFun bool_C (S Minus)
blanchet@33192
   642
              | @{const_name Pair} => do_pair_constr T accum
blanchet@33192
   643
              | @{const_name fst} => do_nth_pair_sel 0 T accum
blanchet@33192
   644
              | @{const_name snd} => do_nth_pair_sel 1 T accum 
blanchet@33192
   645
              | @{const_name Id} =>
blanchet@34982
   646
                (CFun (ctype_for (domain_type T), S Minus, bool_C), accum)
blanchet@33192
   647
              | @{const_name insert} =>
blanchet@33192
   648
                let
blanchet@33192
   649
                  val set_T = domain_type (range_type T)
blanchet@33192
   650
                  val C1 = ctype_for (domain_type set_T)
blanchet@33192
   651
                  val C1' = pos_set_ctype_for_dom C1
blanchet@33192
   652
                  val C2 = ctype_for set_T
blanchet@33192
   653
                  val C3 = ctype_for set_T
blanchet@33192
   654
                in
blanchet@34982
   655
                  (CFun (C1, S Minus, CFun (C2, S Minus, C3)),
blanchet@33192
   656
                   (gamma, cset |> add_ctype_is_right_unique C1
blanchet@33192
   657
                                |> add_is_sub_ctype C1' C3
blanchet@33192
   658
                                |> add_is_sub_ctype C2 C3))
blanchet@33192
   659
                end
blanchet@33192
   660
              | @{const_name converse} =>
blanchet@33192
   661
                let
blanchet@33192
   662
                  val x = Unsynchronized.inc max_fresh
blanchet@33192
   663
                  (* typ -> ctype *)
blanchet@33192
   664
                  fun ctype_for_set T =
blanchet@33192
   665
                    CFun (ctype_for (domain_type T), V x, bool_C)
blanchet@33192
   666
                  val ab_set_C = domain_type T |> ctype_for_set
blanchet@33192
   667
                  val ba_set_C = range_type T |> ctype_for_set
blanchet@34982
   668
                in (CFun (ab_set_C, S Minus, ba_set_C), accum) end
blanchet@33192
   669
              | @{const_name trancl} => do_fragile_set_operation T accum
blanchet@33192
   670
              | @{const_name rtrancl} => (print_g "*** rtrancl"; unsolvable)
haftmann@35028
   671
              | @{const_name semilattice_inf_fun_inst.inf_fun} =>
blanchet@33192
   672
                do_robust_set_operation T accum
haftmann@35028
   673
              | @{const_name semilattice_sup_fun_inst.sup_fun} =>
blanchet@33192
   674
                do_robust_set_operation T accum
blanchet@33192
   675
              | @{const_name finite} =>
blanchet@33192
   676
                let val C1 = ctype_for (domain_type (domain_type T)) in
blanchet@34982
   677
                  (CFun (pos_set_ctype_for_dom C1, S Minus, bool_C), accum)
blanchet@33192
   678
                end
blanchet@33192
   679
              | @{const_name rel_comp} =>
blanchet@33192
   680
                let
blanchet@33192
   681
                  val x = Unsynchronized.inc max_fresh
blanchet@33192
   682
                  (* typ -> ctype *)
blanchet@33192
   683
                  fun ctype_for_set T =
blanchet@33192
   684
                    CFun (ctype_for (domain_type T), V x, bool_C)
blanchet@33192
   685
                  val bc_set_C = domain_type T |> ctype_for_set
blanchet@33192
   686
                  val ab_set_C = domain_type (range_type T) |> ctype_for_set
blanchet@33192
   687
                  val ac_set_C = nth_range_type 2 T |> ctype_for_set
blanchet@33192
   688
                in
blanchet@34982
   689
                  (CFun (bc_set_C, S Minus, CFun (ab_set_C, S Minus, ac_set_C)),
blanchet@33192
   690
                   accum)
blanchet@33192
   691
                end
blanchet@33192
   692
              | @{const_name image} =>
blanchet@33192
   693
                let
blanchet@33192
   694
                  val a_C = ctype_for (domain_type (domain_type T))
blanchet@33192
   695
                  val b_C = ctype_for (range_type (domain_type T))
blanchet@33192
   696
                in
blanchet@34982
   697
                  (CFun (CFun (a_C, S Minus, b_C), S Minus,
blanchet@34982
   698
                         CFun (pos_set_ctype_for_dom a_C, S Minus,
blanchet@33192
   699
                               pos_set_ctype_for_dom b_C)), accum)
blanchet@33192
   700
                end
blanchet@33192
   701
              | @{const_name Sigma} =>
blanchet@33192
   702
                let
blanchet@33192
   703
                  val x = Unsynchronized.inc max_fresh
blanchet@33192
   704
                  (* typ -> ctype *)
blanchet@33192
   705
                  fun ctype_for_set T =
blanchet@33192
   706
                    CFun (ctype_for (domain_type T), V x, bool_C)
blanchet@33192
   707
                  val a_set_T = domain_type T
blanchet@33192
   708
                  val a_C = ctype_for (domain_type a_set_T)
blanchet@33192
   709
                  val b_set_C = ctype_for_set (range_type (domain_type
blanchet@33192
   710
                                                               (range_type T)))
blanchet@33192
   711
                  val a_set_C = ctype_for_set a_set_T
blanchet@34982
   712
                  val a_to_b_set_C = CFun (a_C, S Minus, b_set_C)
blanchet@33192
   713
                  val ab_set_C = ctype_for_set (nth_range_type 2 T)
blanchet@33192
   714
                in
blanchet@34982
   715
                  (CFun (a_set_C, S Minus,
blanchet@34982
   716
                         CFun (a_to_b_set_C, S Minus, ab_set_C)), accum)
blanchet@33192
   717
                end
blanchet@33192
   718
              | @{const_name minus_fun_inst.minus_fun} =>
blanchet@33192
   719
                let
blanchet@33192
   720
                  val set_T = domain_type T
blanchet@33192
   721
                  val left_set_C = ctype_for set_T
blanchet@33192
   722
                  val right_set_C = ctype_for set_T
blanchet@33192
   723
                in
blanchet@34982
   724
                  (CFun (left_set_C, S Minus,
blanchet@34982
   725
                         CFun (right_set_C, S Minus, left_set_C)),
blanchet@33192
   726
                   (gamma, cset |> add_ctype_is_right_unique right_set_C
blanchet@33574
   727
                                |> add_is_sub_ctype right_set_C left_set_C))
blanchet@33192
   728
                end
blanchet@33192
   729
              | @{const_name ord_fun_inst.less_eq_fun} =>
blanchet@33192
   730
                do_fragile_set_operation T accum
blanchet@33192
   731
              | @{const_name Tha} =>
blanchet@33192
   732
                let
blanchet@33192
   733
                  val a_C = ctype_for (domain_type (domain_type T))
blanchet@33192
   734
                  val a_set_C = pos_set_ctype_for_dom a_C
blanchet@34982
   735
                in (CFun (a_set_C, S Minus, a_C), accum) end
blanchet@33192
   736
              | @{const_name FunBox} =>
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
              | _ => if is_sel s then
blanchet@33192
   741
                       if constr_name_for_sel_like s = @{const_name FunBox} then
blanchet@33192
   742
                         let val dom_C = ctype_for (domain_type T) in
blanchet@34982
   743
                           (CFun (dom_C, S Minus, dom_C), accum)
blanchet@33192
   744
                         end
blanchet@33192
   745
                       else
blanchet@33192
   746
                         (ctype_for_sel cdata x, accum)
blanchet@33192
   747
                     else if is_constr thy x then
blanchet@33192
   748
                       (ctype_for_constr cdata x, accum)
blanchet@33192
   749
                     else if is_built_in_const true x then
blanchet@33192
   750
                       case def_of_const thy def_table x of
blanchet@33192
   751
                         SOME t' => do_term t' accum
blanchet@33192
   752
                       | NONE => (print_g ("*** built-in " ^ s); unsolvable)
blanchet@33192
   753
                     else
blanchet@34982
   754
                       let val C = ctype_for T in
blanchet@34982
   755
                         (C, ({bounds = bounds, frees = frees,
blanchet@34982
   756
                               consts = (x, C) :: consts}, cset))
blanchet@34982
   757
                       end)
blanchet@33192
   758
         | Free (x as (_, T)) =>
blanchet@33192
   759
           (case AList.lookup (op =) frees x of
blanchet@33192
   760
              SOME C => (C, accum)
blanchet@33192
   761
            | NONE =>
blanchet@33192
   762
              let val C = ctype_for T in
blanchet@33192
   763
                (C, ({bounds = bounds, frees = (x, C) :: frees,
blanchet@33192
   764
                      consts = consts}, cset))
blanchet@33192
   765
              end)
blanchet@33192
   766
         | Var _ => (print_g "*** Var"; unsolvable)
blanchet@33192
   767
         | Bound j => (nth bounds j, accum)
blanchet@33192
   768
         | Abs (_, T, @{const False}) => (ctype_for (T --> bool_T), accum)
blanchet@33192
   769
         | Abs (s, T, t') =>
blanchet@34998
   770
           ((case t' of
blanchet@34998
   771
               t1' $ Bound 0 =>
blanchet@34998
   772
               if not (loose_bvar1 (t1', 0)) then
blanchet@34998
   773
                 do_term (incr_boundvars ~1 t1') accum
blanchet@34998
   774
               else
blanchet@34998
   775
                 raise SAME ()
blanchet@34998
   776
             | _ => raise SAME ())
blanchet@34998
   777
            handle SAME () =>
blanchet@34998
   778
                   let
blanchet@34998
   779
                     val C = ctype_for T
blanchet@34998
   780
                     val (C', accum) = do_term t' (accum |>> push_bound C)
blanchet@34998
   781
                   in (CFun (C, S Minus, C'), accum |>> pop_bound) end)
blanchet@33192
   782
         | Const (@{const_name All}, _)
blanchet@33192
   783
           $ Abs (_, T', @{const "op -->"} $ (t1 $ Bound 0) $ t2) =>
blanchet@33192
   784
           do_bounded_quantifier T' t1 t2 accum
blanchet@33192
   785
         | Const (@{const_name Ex}, _)
blanchet@33192
   786
           $ Abs (_, T', @{const "op &"} $ (t1 $ Bound 0) $ t2) =>
blanchet@33192
   787
           do_bounded_quantifier T' t1 t2 accum
blanchet@33192
   788
         | Const (@{const_name Let}, _) $ t1 $ t2 =>
blanchet@33192
   789
           do_term (betapply (t2, t1)) accum
blanchet@33192
   790
         | t1 $ t2 =>
blanchet@33192
   791
           let
blanchet@33192
   792
             val (C1, accum) = do_term t1 accum
blanchet@33192
   793
             val (C2, accum) = do_term t2 accum
blanchet@33192
   794
           in
blanchet@33192
   795
             case accum of
blanchet@33192
   796
               (_, UnsolvableCSet) => unsolvable
blanchet@33192
   797
             | _ => case C1 of
blanchet@33192
   798
                      CFun (C11, _, C12) =>
blanchet@33192
   799
                      (C12, accum ||> add_is_sub_ctype C2 C11)
blanchet@33232
   800
                    | _ => raise CTYPE ("Nitpick_Mono.consider_term.do_term \
blanchet@33192
   801
                                        \(op $)", [C1])
blanchet@33192
   802
           end)
blanchet@33192
   803
        |> tap (fn (C, _) =>
blanchet@33192
   804
                   print_g ("  \<Gamma> \<turnstile> " ^
blanchet@33192
   805
                            Syntax.string_of_term ctxt t ^ " : " ^
blanchet@33192
   806
                            string_for_ctype C))
blanchet@33192
   807
  in do_term end
blanchet@33192
   808
blanchet@33192
   809
(* cdata -> sign -> term -> accumulator -> accumulator *)
blanchet@35070
   810
fun consider_general_formula (cdata as {hol_ctxt as {ctxt, ...}, ...}) =
blanchet@33192
   811
  let
blanchet@33192
   812
    (* typ -> ctype *)
blanchet@33192
   813
    val ctype_for = fresh_ctype_for_type cdata
blanchet@33732
   814
    (* term -> accumulator -> ctype * accumulator *)
blanchet@33732
   815
    val do_term = consider_term cdata
blanchet@33192
   816
    (* sign -> term -> accumulator -> accumulator *)
blanchet@33192
   817
    fun do_formula _ _ (_, UnsolvableCSet) = unsolvable_accum
blanchet@33192
   818
      | do_formula sn t (accum as (gamma as {bounds, frees, consts}, cset)) =
blanchet@33192
   819
        let
blanchet@33192
   820
          (* term -> accumulator -> accumulator *)
blanchet@33192
   821
          val do_co_formula = do_formula sn
blanchet@33192
   822
          val do_contra_formula = do_formula (negate sn)
blanchet@33192
   823
          (* string -> typ -> term -> accumulator *)
blanchet@33192
   824
          fun do_quantifier quant_s abs_T body_t =
blanchet@33192
   825
            let
blanchet@33192
   826
              val abs_C = ctype_for abs_T
blanchet@34982
   827
              val side_cond = ((sn = Minus) = (quant_s = @{const_name Ex}))
blanchet@33192
   828
              val cset = cset |> side_cond ? add_ctype_is_right_total abs_C
blanchet@33192
   829
            in
blanchet@33192
   830
              (gamma |> push_bound abs_C, cset) |> do_co_formula body_t
blanchet@33192
   831
                                                |>> pop_bound
blanchet@33192
   832
            end
blanchet@33192
   833
          (* typ -> term -> accumulator *)
blanchet@33192
   834
          fun do_bounded_quantifier abs_T body_t =
blanchet@33192
   835
            accum |>> push_bound (ctype_for abs_T) |> do_co_formula body_t
blanchet@33192
   836
                  |>> pop_bound
blanchet@33192
   837
          (* term -> term -> accumulator *)
blanchet@33192
   838
          fun do_equals t1 t2 =
blanchet@33192
   839
            case sn of
blanchet@34982
   840
              Plus => do_term t accum |> snd
blanchet@34982
   841
            | Minus => let
blanchet@34982
   842
                         val (C1, accum) = do_term t1 accum
blanchet@34982
   843
                         val (C2, accum) = do_term t2 accum
blanchet@34982
   844
                       in accum ||> add_ctypes_equal C1 C2 end
blanchet@33192
   845
        in
blanchet@33192
   846
          case t of
blanchet@33192
   847
            Const (s0 as @{const_name all}, _) $ Abs (_, T1, t1) =>
blanchet@33192
   848
            do_quantifier s0 T1 t1
blanchet@33192
   849
          | Const (@{const_name "=="}, _) $ t1 $ t2 => do_equals t1 t2
blanchet@33192
   850
          | @{const "==>"} $ t1 $ t2 =>
blanchet@33192
   851
            accum |> do_contra_formula t1 |> do_co_formula t2
blanchet@33192
   852
          | @{const Trueprop} $ t1 => do_co_formula t1 accum
blanchet@33192
   853
          | @{const Not} $ t1 => do_contra_formula t1 accum
blanchet@33192
   854
          | Const (@{const_name All}, _)
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 All}, _) $ Abs (_, T1, t1) =>
blanchet@33192
   858
            do_quantifier s0 T1 t1
blanchet@33192
   859
          | Const (@{const_name Ex}, _)
blanchet@33192
   860
            $ Abs (_, T1, t1 as @{const "op &"} $ (_ $ Bound 0) $ _) =>
blanchet@33192
   861
            do_bounded_quantifier T1 t1
blanchet@33192
   862
          | Const (s0 as @{const_name Ex}, _) $ Abs (_, T1, t1) =>
blanchet@33192
   863
            do_quantifier s0 T1 t1
blanchet@33192
   864
          | Const (@{const_name "op ="}, _) $ t1 $ t2 => do_equals t1 t2
blanchet@33192
   865
          | @{const "op &"} $ t1 $ t2 =>
blanchet@33192
   866
            accum |> do_co_formula t1 |> do_co_formula t2
blanchet@33192
   867
          | @{const "op |"} $ t1 $ t2 =>
blanchet@33192
   868
            accum |> do_co_formula t1 |> do_co_formula t2
blanchet@33192
   869
          | @{const "op -->"} $ t1 $ t2 =>
blanchet@33192
   870
            accum |> do_contra_formula t1 |> do_co_formula t2
blanchet@33192
   871
          | Const (@{const_name If}, _) $ t1 $ t2 $ t3 =>
blanchet@33732
   872
            accum |> do_term t1 |> snd |> fold do_co_formula [t2, t3]
blanchet@33192
   873
          | Const (@{const_name Let}, _) $ t1 $ t2 =>
blanchet@33192
   874
            do_co_formula (betapply (t2, t1)) accum
blanchet@33732
   875
          | _ => do_term t accum |> snd
blanchet@33192
   876
        end
blanchet@33192
   877
        |> tap (fn _ => print_g ("\<Gamma> \<turnstile> " ^
blanchet@33192
   878
                                 Syntax.string_of_term ctxt t ^
blanchet@33192
   879
                                 " : o\<^sup>" ^ string_for_sign sn))
blanchet@33192
   880
  in do_formula end
blanchet@33192
   881
blanchet@33192
   882
(* The harmless axiom optimization below is somewhat too aggressive in the face
blanchet@33192
   883
   of (rather peculiar) user-defined axioms. *)
blanchet@33192
   884
val harmless_consts =
blanchet@33192
   885
  [@{const_name ord_class.less}, @{const_name ord_class.less_eq}]
blanchet@33192
   886
val bounteous_consts = [@{const_name bisim}]
blanchet@33192
   887
blanchet@33192
   888
(* term -> bool *)
blanchet@33192
   889
fun is_harmless_axiom t =
blanchet@33192
   890
  Term.add_consts t [] |> filter_out (is_built_in_const true)
blanchet@33192
   891
  |> (forall (member (op =) harmless_consts o original_name o fst)
blanchet@33192
   892
      orf exists (member (op =) bounteous_consts o fst))
blanchet@33192
   893
blanchet@33192
   894
(* cdata -> sign -> term -> accumulator -> accumulator *)
blanchet@33192
   895
fun consider_nondefinitional_axiom cdata sn t =
blanchet@33192
   896
  not (is_harmless_axiom t) ? consider_general_formula cdata sn t
blanchet@33192
   897
blanchet@33192
   898
(* cdata -> term -> accumulator -> accumulator *)
blanchet@35070
   899
fun consider_definitional_axiom (cdata as {hol_ctxt as {thy, ...}, ...}) t =
blanchet@33192
   900
  if not (is_constr_pattern_formula thy t) then
blanchet@34982
   901
    consider_nondefinitional_axiom cdata Plus t
blanchet@33192
   902
  else if is_harmless_axiom t then
blanchet@33192
   903
    I
blanchet@33192
   904
  else
blanchet@33192
   905
    let
blanchet@33732
   906
      (* term -> accumulator -> ctype * accumulator *)
blanchet@33192
   907
      val do_term = consider_term cdata
blanchet@33192
   908
      (* typ -> term -> accumulator -> accumulator *)
blanchet@33192
   909
      fun do_all abs_T body_t accum =
blanchet@33192
   910
        let val abs_C = fresh_ctype_for_type cdata abs_T in
blanchet@33192
   911
          accum |>> push_bound abs_C |> do_formula body_t |>> pop_bound
blanchet@33192
   912
        end
blanchet@33192
   913
      (* term -> term -> accumulator -> accumulator *)
blanchet@33192
   914
      and do_implies t1 t2 = do_term t1 #> snd #> do_formula t2
blanchet@33192
   915
      and do_equals t1 t2 accum =
blanchet@33192
   916
        let
blanchet@33192
   917
          val (C1, accum) = do_term t1 accum
blanchet@33192
   918
          val (C2, accum) = do_term t2 accum
blanchet@33192
   919
        in accum ||> add_ctypes_equal C1 C2 end
blanchet@33192
   920
      (* term -> accumulator -> accumulator *)
blanchet@33192
   921
      and do_formula _ (_, UnsolvableCSet) = unsolvable_accum
blanchet@33192
   922
        | do_formula t accum =
blanchet@33192
   923
          case t of
blanchet@33192
   924
            Const (@{const_name all}, _) $ Abs (_, T1, t1) => do_all T1 t1 accum
blanchet@33192
   925
          | @{const Trueprop} $ t1 => do_formula t1 accum
blanchet@33192
   926
          | Const (@{const_name "=="}, _) $ t1 $ t2 => do_equals t1 t2 accum
blanchet@33192
   927
          | @{const "==>"} $ t1 $ t2 => do_implies t1 t2 accum
blanchet@33192
   928
          | @{const Pure.conjunction} $ t1 $ t2 =>
blanchet@33192
   929
            accum |> do_formula t1 |> do_formula t2
blanchet@33192
   930
          | Const (@{const_name All}, _) $ Abs (_, T1, t1) => do_all T1 t1 accum
blanchet@33192
   931
          | Const (@{const_name "op ="}, _) $ t1 $ t2 => do_equals t1 t2 accum
blanchet@33192
   932
          | @{const "op &"} $ t1 $ t2 => accum |> do_formula t1 |> do_formula t2
blanchet@33192
   933
          | @{const "op -->"} $ t1 $ t2 => do_implies t1 t2 accum
blanchet@33232
   934
          | _ => raise TERM ("Nitpick_Mono.consider_definitional_axiom.\
blanchet@33192
   935
                             \do_formula", [t])
blanchet@33192
   936
    in do_formula t end
blanchet@33192
   937
blanchet@33192
   938
(* Proof.context -> literal list -> term -> ctype -> string *)
blanchet@33192
   939
fun string_for_ctype_of_term ctxt lits t C =
blanchet@33192
   940
  Syntax.string_of_term ctxt t ^ " : " ^
blanchet@33192
   941
  string_for_ctype (instantiate_ctype lits C)
blanchet@33192
   942
blanchet@33192
   943
(* theory -> literal list -> ctype_context -> unit *)
blanchet@33192
   944
fun print_ctype_context ctxt lits ({frees, consts, ...} : ctype_context) =
blanchet@33192
   945
  map (fn (x, C) => string_for_ctype_of_term ctxt lits (Free x) C) frees @
blanchet@34982
   946
  map (fn (x, C) => string_for_ctype_of_term ctxt lits (Const x) C) consts
blanchet@33192
   947
  |> cat_lines |> print_g
blanchet@33192
   948
blanchet@35190
   949
(* hol_context -> bool -> typ -> sign -> term list -> term list -> term
blanchet@35190
   950
   -> bool *)
blanchet@35190
   951
fun formulas_monotonic (hol_ctxt as {ctxt, ...}) binarize alpha_T sn def_ts
blanchet@35190
   952
                       nondef_ts core_t =
blanchet@33192
   953
  let
blanchet@33192
   954
    val _ = print_g ("****** " ^ string_for_ctype CAlpha ^ " is " ^
blanchet@33192
   955
                     Syntax.string_of_typ ctxt alpha_T)
blanchet@35190
   956
    val cdata as {max_fresh, ...} = initial_cdata hol_ctxt binarize alpha_T
blanchet@33192
   957
    val (gamma, cset) =
blanchet@33192
   958
      (initial_gamma, slack)
blanchet@33192
   959
      |> fold (consider_definitional_axiom cdata) def_ts
blanchet@34982
   960
      |> fold (consider_nondefinitional_axiom cdata Plus) nondef_ts
blanchet@34982
   961
      |> consider_general_formula cdata sn core_t
blanchet@33192
   962
  in
blanchet@33192
   963
    case solve (!max_fresh) cset of
blanchet@33192
   964
      SOME lits => (print_ctype_context ctxt lits gamma; true)
blanchet@33192
   965
    | _ => false
blanchet@33192
   966
  end
blanchet@33192
   967
  handle CTYPE (loc, Cs) => raise BAD (loc, commas (map string_for_ctype Cs))
blanchet@33192
   968
blanchet@33192
   969
end;