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