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