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