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