src/HOL/Tools/Nitpick/nitpick_mono.ML
author blanchet
Tue Dec 07 11:56:53 2010 +0100 (2010-12-07)
changeset 41052 3db267a01c1d
parent 41050 effbaa323cf0
child 41054 e58d1cdda832
permissions -rw-r--r--
remove the "fin_fun" optimization in Nitpick -- it was always a hack and didn't help much
     1 (*  Title:      HOL/Tools/Nitpick/nitpick_mono.ML
     2     Author:     Jasmin Blanchette, TU Muenchen
     3     Copyright   2009, 2010
     4 
     5 Monotonicity inference for higher-order logic.
     6 *)
     7 
     8 signature NITPICK_MONO =
     9 sig
    10   type hol_context = Nitpick_HOL.hol_context
    11 
    12   val trace : bool Unsynchronized.ref
    13   val formulas_monotonic :
    14     hol_context -> bool -> typ -> term list * term list -> bool
    15 end;
    16 
    17 structure Nitpick_Mono : NITPICK_MONO =
    18 struct
    19 
    20 open Nitpick_Util
    21 open Nitpick_HOL
    22 
    23 structure PL = PropLogic
    24 
    25 datatype sign = Plus | Minus
    26 
    27 type var = int
    28 
    29 datatype annotation = Gen | New | Fls | Tru
    30 datatype annotation_atom = A of annotation | V of var
    31 
    32 type assign_literal = var * (sign * annotation)
    33 
    34 datatype mtyp =
    35   MAlpha |
    36   MFun of mtyp * annotation_atom * mtyp |
    37   MPair of mtyp * mtyp |
    38   MType of string * mtyp list |
    39   MRec of string * typ list
    40 
    41 datatype mterm =
    42   MRaw of term * mtyp |
    43   MAbs of string * typ * mtyp * annotation_atom * mterm |
    44   MApp of mterm * mterm
    45 
    46 type mdata =
    47   {hol_ctxt: hol_context,
    48    binarize: bool,
    49    alpha_T: typ,
    50    no_harmless: bool,
    51    max_fresh: int Unsynchronized.ref,
    52    datatype_mcache: ((string * typ list) * mtyp) list Unsynchronized.ref,
    53    constr_mcache: (styp * mtyp) list Unsynchronized.ref}
    54 
    55 exception UNSOLVABLE of unit
    56 exception MTYPE of string * mtyp list * typ list
    57 exception MTERM of string * mterm list
    58 
    59 val trace = Unsynchronized.ref false
    60 fun trace_msg msg = if !trace then tracing (msg ()) else ()
    61 
    62 fun string_for_sign Plus = "+"
    63   | string_for_sign Minus = "-"
    64 
    65 fun negate_sign Plus = Minus
    66   | negate_sign Minus = Plus
    67 
    68 val string_for_var = signed_string_of_int
    69 fun string_for_vars sep [] = "0\<^bsub>" ^ sep ^ "\<^esub>"
    70   | string_for_vars sep xs = space_implode sep (map string_for_var xs)
    71 fun subscript_string_for_vars sep xs =
    72   if null xs then "" else "\<^bsub>" ^ string_for_vars sep xs ^ "\<^esub>"
    73 
    74 fun string_for_annotation Gen = "G"
    75   | string_for_annotation New = "N"
    76   | string_for_annotation Fls = "F"
    77   | string_for_annotation Tru = "T"
    78 
    79 fun string_for_annotation_atom (A a) = string_for_annotation a
    80   | string_for_annotation_atom (V x) = string_for_var x
    81 
    82 fun string_for_assign_literal (x, (sn, a)) =
    83   string_for_var x ^ (case sn of Plus => " = " | Minus => " \<noteq> ") ^
    84   string_for_annotation a
    85 
    86 val bool_M = MType (@{type_name bool}, [])
    87 val dummy_M = MType (nitpick_prefix ^ "dummy", [])
    88 
    89 fun is_MRec (MRec _) = true
    90   | is_MRec _ = false
    91 fun dest_MFun (MFun z) = z
    92   | dest_MFun M = raise MTYPE ("Nitpick_Mono.dest_MFun", [M], [])
    93 
    94 val no_prec = 100
    95 
    96 fun precedence_of_mtype (MFun _) = 1
    97   | precedence_of_mtype (MPair _) = 2
    98   | precedence_of_mtype _ = no_prec
    99 
   100 val string_for_mtype =
   101   let
   102     fun aux outer_prec M =
   103       let
   104         val prec = precedence_of_mtype M
   105         val need_parens = (prec < outer_prec)
   106       in
   107         (if need_parens then "(" else "") ^
   108         (if M = dummy_M then
   109            "_"
   110          else case M of
   111              MAlpha => "\<alpha>"
   112            | MFun (M1, aa, M2) =>
   113              aux (prec + 1) M1 ^ " \<Rightarrow>\<^bsup>" ^
   114              string_for_annotation_atom aa ^ "\<^esup> " ^ aux prec M2
   115            | MPair (M1, M2) => aux (prec + 1) M1 ^ " \<times> " ^ aux prec M2
   116            | MType (s, []) =>
   117              if s = @{type_name prop} orelse s = @{type_name bool} then "o"
   118              else s
   119            | MType (s, Ms) => "(" ^ commas (map (aux 0) Ms) ^ ") " ^ s
   120            | MRec (s, _) => "[" ^ s ^ "]") ^
   121         (if need_parens then ")" else "")
   122       end
   123   in aux 0 end
   124 
   125 fun flatten_mtype (MPair (M1, M2)) = maps flatten_mtype [M1, M2]
   126   | flatten_mtype (MType (_, Ms)) = maps flatten_mtype Ms
   127   | flatten_mtype M = [M]
   128 
   129 fun precedence_of_mterm (MRaw _) = no_prec
   130   | precedence_of_mterm (MAbs _) = 1
   131   | precedence_of_mterm (MApp _) = 2
   132 
   133 fun string_for_mterm ctxt =
   134   let
   135     fun mtype_annotation M = "\<^bsup>" ^ string_for_mtype M ^ "\<^esup>"
   136     fun aux outer_prec m =
   137       let
   138         val prec = precedence_of_mterm m
   139         val need_parens = (prec < outer_prec)
   140       in
   141         (if need_parens then "(" else "") ^
   142         (case m of
   143            MRaw (t, M) => Syntax.string_of_term ctxt t ^ mtype_annotation M
   144          | MAbs (s, _, M, aa, m) =>
   145            "\<lambda>" ^ s ^ mtype_annotation M ^ ".\<^bsup>" ^
   146            string_for_annotation_atom aa ^ "\<^esup> " ^ aux prec m
   147          | MApp (m1, m2) => aux prec m1 ^ " " ^ aux (prec + 1) m2) ^
   148         (if need_parens then ")" else "")
   149       end
   150   in aux 0 end
   151 
   152 fun mtype_of_mterm (MRaw (_, M)) = M
   153   | mtype_of_mterm (MAbs (_, _, M, aa, m)) = MFun (M, aa, mtype_of_mterm m)
   154   | mtype_of_mterm (MApp (m1, _)) =
   155     case mtype_of_mterm m1 of
   156       MFun (_, _, M12) => M12
   157     | M1 => raise MTYPE ("Nitpick_Mono.mtype_of_mterm", [M1], [])
   158 
   159 fun strip_mcomb (MApp (m1, m2)) = strip_mcomb m1 ||> (fn ms => append ms [m2])
   160   | strip_mcomb m = (m, [])
   161 
   162 fun initial_mdata hol_ctxt binarize no_harmless alpha_T =
   163   ({hol_ctxt = hol_ctxt, binarize = binarize, alpha_T = alpha_T,
   164     no_harmless = no_harmless, max_fresh = Unsynchronized.ref 0,
   165     datatype_mcache = Unsynchronized.ref [],
   166     constr_mcache = Unsynchronized.ref []} : mdata)
   167 
   168 fun could_exist_alpha_subtype alpha_T (T as Type (_, Ts)) =
   169     T = alpha_T orelse (not (is_fp_iterator_type T) andalso
   170                         exists (could_exist_alpha_subtype alpha_T) Ts)
   171   | could_exist_alpha_subtype alpha_T T = (T = alpha_T)
   172 fun could_exist_alpha_sub_mtype _ (alpha_T as TFree _) T =
   173     could_exist_alpha_subtype alpha_T T
   174   | could_exist_alpha_sub_mtype ctxt alpha_T T =
   175     (T = alpha_T orelse is_datatype ctxt [(NONE, true)] T)
   176 
   177 fun exists_alpha_sub_mtype MAlpha = true
   178   | exists_alpha_sub_mtype (MFun (M1, _, M2)) =
   179     exists exists_alpha_sub_mtype [M1, M2]
   180   | exists_alpha_sub_mtype (MPair (M1, M2)) =
   181     exists exists_alpha_sub_mtype [M1, M2]
   182   | exists_alpha_sub_mtype (MType (_, Ms)) = exists exists_alpha_sub_mtype Ms
   183   | exists_alpha_sub_mtype (MRec _) = true
   184 
   185 fun exists_alpha_sub_mtype_fresh MAlpha = true
   186   | exists_alpha_sub_mtype_fresh (MFun (_, V _, _)) = true
   187   | exists_alpha_sub_mtype_fresh (MFun (_, _, M2)) =
   188     exists_alpha_sub_mtype_fresh M2
   189   | exists_alpha_sub_mtype_fresh (MPair (M1, M2)) =
   190     exists exists_alpha_sub_mtype_fresh [M1, M2]
   191   | exists_alpha_sub_mtype_fresh (MType (_, Ms)) =
   192     exists exists_alpha_sub_mtype_fresh Ms
   193   | exists_alpha_sub_mtype_fresh (MRec _) = true
   194 
   195 fun constr_mtype_for_binders z Ms =
   196   fold_rev (fn M => curry3 MFun M (A Gen)) Ms (MRec z)
   197 
   198 fun repair_mtype _ _ MAlpha = MAlpha
   199   | repair_mtype cache seen (MFun (M1, aa, M2)) =
   200     MFun (repair_mtype cache seen M1, aa, repair_mtype cache seen M2)
   201   | repair_mtype cache seen (MPair Mp) =
   202     MPair (pairself (repair_mtype cache seen) Mp)
   203   | repair_mtype cache seen (MType (s, Ms)) =
   204     MType (s, maps (flatten_mtype o repair_mtype cache seen) Ms)
   205   | repair_mtype cache seen (MRec (z as (s, _))) =
   206     case AList.lookup (op =) cache z |> the of
   207       MRec _ => MType (s, [])
   208     | M => if member (op =) seen M then MType (s, [])
   209            else repair_mtype cache (M :: seen) M
   210 
   211 fun repair_datatype_mcache cache =
   212   let
   213     fun repair_one (z, M) =
   214       Unsynchronized.change cache
   215           (AList.update (op =) (z, repair_mtype (!cache) [] M))
   216   in List.app repair_one (rev (!cache)) end
   217 
   218 fun repair_constr_mcache dtype_cache constr_mcache =
   219   let
   220     fun repair_one (x, M) =
   221       Unsynchronized.change constr_mcache
   222           (AList.update (op =) (x, repair_mtype dtype_cache [] M))
   223   in List.app repair_one (!constr_mcache) end
   224 
   225 fun is_fin_fun_supported_type @{typ prop} = true
   226   | is_fin_fun_supported_type @{typ bool} = true
   227   | is_fin_fun_supported_type (Type (@{type_name option}, _)) = true
   228   | is_fin_fun_supported_type _ = false
   229 
   230 (* TODO: clean this up *)
   231 fun fin_fun_body _ _ (t as @{term False}) = SOME t
   232   | fin_fun_body _ _ (t as Const (@{const_name None}, _)) = SOME t
   233   | fin_fun_body dom_T ran_T
   234                  ((t0 as Const (@{const_name If}, _))
   235                   $ (t1 as Const (@{const_name HOL.eq}, _) $ Bound 0 $ t1')
   236                   $ t2 $ t3) =
   237     (if loose_bvar1 (t1', 0) then
   238        NONE
   239      else case fin_fun_body dom_T ran_T t3 of
   240        NONE => NONE
   241      | SOME t3 =>
   242        SOME (t0 $ (Const (@{const_name is_unknown}, dom_T --> bool_T) $ t1')
   243                 $ (Const (@{const_name unknown}, ran_T)) $ (t0 $ t1 $ t2 $ t3)))
   244   | fin_fun_body _ _ _ = NONE
   245 
   246 (* ### FIXME: make sure well-annotated! *)
   247 
   248 fun fresh_mfun_for_fun_type (mdata as {max_fresh, ...} : mdata) all_minus
   249                             T1 T2 =
   250   let
   251     val M1 = fresh_mtype_for_type mdata all_minus T1
   252     val M2 = fresh_mtype_for_type mdata all_minus T2
   253     val aa = if not all_minus andalso exists_alpha_sub_mtype_fresh M1 andalso
   254                 is_fin_fun_supported_type (body_type T2) then
   255                V (Unsynchronized.inc max_fresh)
   256              else
   257                A Gen
   258   in (M1, aa, M2) end
   259 and fresh_mtype_for_type (mdata as {hol_ctxt as {ctxt, ...}, binarize, alpha_T,
   260                                     datatype_mcache, constr_mcache, ...})
   261                          all_minus =
   262   let
   263     fun do_type T =
   264       if T = alpha_T then
   265         MAlpha
   266       else case T of
   267         Type (@{type_name fun}, [T1, T2]) =>
   268         MFun (fresh_mfun_for_fun_type mdata all_minus T1 T2)
   269       | Type (@{type_name prod}, [T1, T2]) => MPair (pairself do_type (T1, T2))
   270       | Type (z as (s, _)) =>
   271         if could_exist_alpha_sub_mtype ctxt alpha_T T then
   272           case AList.lookup (op =) (!datatype_mcache) z of
   273             SOME M => M
   274           | NONE =>
   275             let
   276               val _ = Unsynchronized.change datatype_mcache (cons (z, MRec z))
   277               val xs = binarized_and_boxed_datatype_constrs hol_ctxt binarize T
   278               val (all_Ms, constr_Ms) =
   279                 fold_rev (fn (_, T') => fn (all_Ms, constr_Ms) =>
   280                              let
   281                                val binder_Ms = map do_type (binder_types T')
   282                                val new_Ms = filter exists_alpha_sub_mtype_fresh
   283                                                    binder_Ms
   284                                val constr_M = constr_mtype_for_binders z
   285                                                                        binder_Ms
   286                              in
   287                                (union (op =) new_Ms all_Ms,
   288                                 constr_M :: constr_Ms)
   289                              end)
   290                          xs ([], [])
   291               val M = MType (s, all_Ms)
   292               val _ = Unsynchronized.change datatype_mcache
   293                           (AList.update (op =) (z, M))
   294               val _ = Unsynchronized.change constr_mcache
   295                           (append (xs ~~ constr_Ms))
   296             in
   297               if forall (not o is_MRec o snd) (!datatype_mcache) then
   298                 (repair_datatype_mcache datatype_mcache;
   299                  repair_constr_mcache (!datatype_mcache) constr_mcache;
   300                  AList.lookup (op =) (!datatype_mcache) z |> the)
   301               else
   302                 M
   303             end
   304         else
   305           MType (s, [])
   306       | _ => MType (simple_string_of_typ T, [])
   307   in do_type end
   308 
   309 val ground_and_sole_base_constrs = [] (* FIXME: [@{const_name Nil}, @{const_name None}], cf. lists_empty *)
   310 
   311 fun prodM_factors (MPair (M1, M2)) = maps prodM_factors [M1, M2]
   312   | prodM_factors M = [M]
   313 fun curried_strip_mtype (MFun (M1, _, M2)) =
   314     curried_strip_mtype M2 |>> append (prodM_factors M1)
   315   | curried_strip_mtype M = ([], M)
   316 fun sel_mtype_from_constr_mtype s M =
   317   let
   318     val (arg_Ms, dataM) = curried_strip_mtype M
   319     val a = if member (op =) ground_and_sole_base_constrs
   320                              (constr_name_for_sel_like s) then
   321               Fls
   322             else
   323               Gen
   324   in
   325     MFun (dataM, A a,
   326           case sel_no_from_name s of ~1 => bool_M | n => nth arg_Ms n)
   327   end
   328 
   329 fun mtype_for_constr (mdata as {hol_ctxt = {ctxt, ...}, alpha_T, constr_mcache,
   330                                 ...}) (x as (_, T)) =
   331   if could_exist_alpha_sub_mtype ctxt alpha_T T then
   332     case AList.lookup (op =) (!constr_mcache) x of
   333       SOME M => M
   334     | NONE => if T = alpha_T then
   335                 let val M = fresh_mtype_for_type mdata false T in
   336                   (Unsynchronized.change constr_mcache (cons (x, M)); M)
   337                 end
   338               else
   339                 (fresh_mtype_for_type mdata false (body_type T);
   340                  AList.lookup (op =) (!constr_mcache) x |> the)
   341   else
   342     fresh_mtype_for_type mdata false T
   343 fun mtype_for_sel (mdata as {hol_ctxt, binarize, ...}) (x as (s, _)) =
   344   x |> binarized_and_boxed_constr_for_sel hol_ctxt binarize
   345     |> mtype_for_constr mdata |> sel_mtype_from_constr_mtype s
   346 
   347 fun resolve_annotation_atom asgs (V x) =
   348     x |> AList.lookup (op =) asgs |> Option.map A |> the_default (V x)
   349   | resolve_annotation_atom _ aa = aa
   350 fun resolve_mtype asgs =
   351   let
   352     fun aux MAlpha = MAlpha
   353       | aux (MFun (M1, aa, M2)) =
   354         MFun (aux M1, resolve_annotation_atom asgs aa, aux M2)
   355       | aux (MPair Mp) = MPair (pairself aux Mp)
   356       | aux (MType (s, Ms)) = MType (s, map aux Ms)
   357       | aux (MRec z) = MRec z
   358   in aux end
   359 
   360 datatype comp_op = Eq | Neq | Leq
   361 
   362 type comp = annotation_atom * annotation_atom * comp_op * var list
   363 type assign_clause = assign_literal list
   364 
   365 type constraint_set = comp list * assign_clause list
   366 
   367 fun string_for_comp_op Eq = "="
   368   | string_for_comp_op Neq = "\<noteq>"
   369   | string_for_comp_op Leq = "\<le>"
   370 
   371 fun string_for_comp (aa1, aa2, cmp, xs) =
   372   string_for_annotation_atom aa1 ^ " " ^ string_for_comp_op cmp ^
   373   subscript_string_for_vars " \<and> " xs ^ " " ^ string_for_annotation_atom aa2
   374 
   375 fun string_for_assign_clause NONE = "\<top>"
   376   | string_for_assign_clause (SOME []) = "\<bot>"
   377   | string_for_assign_clause (SOME asgs) =
   378     space_implode " \<or> " (map string_for_assign_literal asgs)
   379 
   380 fun add_assign_literal (x, (sn, a)) clauses =
   381   if exists (fn [(x', (sn', a'))] =>
   382                 x = x' andalso ((sn = sn' andalso a <> a') orelse
   383                                 (sn <> sn' andalso a = a'))
   384               | _ => false) clauses then
   385     NONE
   386   else
   387     SOME ([(x, (sn, a))] :: clauses)
   388 
   389 fun add_assign_disjunct _ NONE = NONE
   390   | add_assign_disjunct asg (SOME asgs) = SOME (insert (op =) asg asgs)
   391 
   392 fun add_assign_clause NONE = I
   393   | add_assign_clause (SOME clause) = insert (op =) clause
   394 
   395 fun annotation_comp Eq a1 a2 = (a1 = a2)
   396   | annotation_comp Neq a1 a2 = (a1 <> a2)
   397   | annotation_comp Leq a1 a2 = (a1 = a2 orelse a2 = Gen)
   398 
   399 fun sign_for_comp_op Eq = Plus
   400   | sign_for_comp_op Neq = Minus
   401   | sign_for_comp_op Leq = raise BAD ("sign_for_comp_op", "unexpected \"Leq\"")
   402 
   403 fun do_annotation_atom_comp Leq [] aa1 aa2 (cset as (comps, clauses)) =
   404     (case (aa1, aa2) of
   405        (A a1, A a2) => if annotation_comp Leq a1 a2 then SOME cset else NONE
   406      | _ => SOME (insert (op =) (aa1, aa2, Leq, []) comps, clauses))
   407   | do_annotation_atom_comp cmp [] aa1 aa2 (cset as (comps, clauses)) =
   408     (case (aa1, aa2) of
   409        (A a1, A a2) => if annotation_comp cmp a1 a2 then SOME cset else NONE
   410      | (V x1, A a2) =>
   411        clauses |> add_assign_literal (x1, (sign_for_comp_op cmp, a2))
   412                |> Option.map (pair comps)
   413      | (A _, V _) => do_annotation_atom_comp cmp [] aa2 aa1 cset
   414      | (V _, V _) => SOME (insert (op =) (aa1, aa2, cmp, []) comps, clauses))
   415   | do_annotation_atom_comp cmp xs aa1 aa2 (comps, clauses) =
   416     SOME (insert (op =) (aa1, aa2, cmp, xs) comps, clauses)
   417 
   418 fun add_annotation_atom_comp cmp xs aa1 aa2 (comps, clauses) =
   419   (trace_msg (fn () => "*** Add " ^ string_for_comp (aa1, aa2, cmp, xs));
   420    case do_annotation_atom_comp cmp xs aa1 aa2 (comps, clauses) of
   421      NONE => (trace_msg (K "**** Unsolvable"); raise UNSOLVABLE ())
   422    | SOME cset => cset)
   423 
   424 fun do_mtype_comp _ _ _ _ NONE = NONE
   425   | do_mtype_comp _ _ MAlpha MAlpha cset = cset
   426   | do_mtype_comp Eq xs (MFun (M11, aa1, M12)) (MFun (M21, aa2, M22))
   427                   (SOME cset) =
   428     cset |> do_annotation_atom_comp Eq xs aa1 aa2
   429          |> do_mtype_comp Eq xs M11 M21 |> do_mtype_comp Eq xs M12 M22
   430   | do_mtype_comp Leq xs (MFun (M11, aa1, M12)) (MFun (M21, aa2, M22))
   431                   (SOME cset) =
   432     (if exists_alpha_sub_mtype M11 then
   433        cset |> do_annotation_atom_comp Leq xs aa1 aa2
   434             |> do_mtype_comp Leq xs M21 M11
   435             |> (case aa2 of
   436                   A Gen => I
   437                 | A _ => do_mtype_comp Leq xs M11 M21
   438                 | V x => do_mtype_comp Leq (x :: xs) M11 M21)
   439      else
   440        SOME cset)
   441     |> do_mtype_comp Leq xs M12 M22
   442   | do_mtype_comp cmp xs (M1 as MPair (M11, M12)) (M2 as MPair (M21, M22))
   443                   cset =
   444     (cset |> fold (uncurry (do_mtype_comp cmp xs)) [(M11, M21), (M12, M22)]
   445      handle ListPair.UnequalLengths =>
   446             raise MTYPE ("Nitpick_Mono.do_mtype_comp", [M1, M2], []))
   447   | do_mtype_comp _ _ (MType _) (MType _) cset =
   448     cset (* no need to compare them thanks to the cache *)
   449   | do_mtype_comp cmp _ M1 M2 _ =
   450     raise MTYPE ("Nitpick_Mono.do_mtype_comp (" ^ string_for_comp_op cmp ^ ")",
   451                  [M1, M2], [])
   452 
   453 fun add_mtype_comp cmp M1 M2 cset =
   454   (trace_msg (fn () => "*** Add " ^ string_for_mtype M1 ^ " " ^
   455                        string_for_comp_op cmp ^ " " ^ string_for_mtype M2);
   456    case SOME cset |> do_mtype_comp cmp [] M1 M2 of
   457      NONE => (trace_msg (K "**** Unsolvable"); raise UNSOLVABLE ())
   458    | SOME cset => cset)
   459 
   460 val add_mtypes_equal = add_mtype_comp Eq
   461 val add_is_sub_mtype = add_mtype_comp Leq
   462 
   463 fun do_notin_mtype_fv _ _ _ NONE = NONE
   464   | do_notin_mtype_fv Minus _ MAlpha cset = cset
   465   | do_notin_mtype_fv Plus [] MAlpha _ = NONE
   466   | do_notin_mtype_fv Plus [asg] MAlpha (SOME clauses) =
   467     clauses |> add_assign_literal asg
   468   | do_notin_mtype_fv Plus unless MAlpha (SOME clauses) =
   469     SOME (insert (op =) unless clauses)
   470   | do_notin_mtype_fv sn unless (MFun (M1, A a, M2)) cset =
   471     cset |> (if a <> Gen andalso sn = Plus then do_notin_mtype_fv Plus unless M1
   472              else I)
   473          |> (if a = Gen orelse sn = Plus then do_notin_mtype_fv Minus unless M1
   474              else I)
   475          |> do_notin_mtype_fv sn unless M2
   476   | do_notin_mtype_fv Plus unless (MFun (M1, V x, M2)) cset =
   477     cset |> (case add_assign_disjunct (x, (Plus, Gen)) (SOME unless) of
   478                NONE => I
   479              | SOME unless' => do_notin_mtype_fv Plus unless' M1)
   480          |> do_notin_mtype_fv Minus unless M1
   481          |> do_notin_mtype_fv Plus unless M2
   482   | do_notin_mtype_fv Minus unless (MFun (M1, V x, M2)) cset =
   483     cset |> (case fold (fn a => add_assign_disjunct (x, (Plus, a))) [Fls, Tru]
   484                        (SOME unless) of
   485                NONE => I
   486              | SOME unless' => do_notin_mtype_fv Plus unless' M1)
   487          |> do_notin_mtype_fv Minus unless M2
   488   | do_notin_mtype_fv sn unless (MPair (M1, M2)) cset =
   489     cset |> fold (do_notin_mtype_fv sn unless) [M1, M2]
   490   | do_notin_mtype_fv sn unless (MType (_, Ms)) cset =
   491     cset |> fold (do_notin_mtype_fv sn unless) Ms
   492  | do_notin_mtype_fv _ _ M _ =
   493    raise MTYPE ("Nitpick_Mono.do_notin_mtype_fv", [M], [])
   494 
   495 fun add_notin_mtype_fv sn unless M (comps, clauses) =
   496   (trace_msg (fn () => "*** Add " ^ string_for_mtype M ^ " is " ^
   497                        (case sn of Minus => "concrete" | Plus => "complete"));
   498    case SOME clauses |> do_notin_mtype_fv sn unless M of
   499      NONE => (trace_msg (K "**** Unsolvable"); raise UNSOLVABLE ())
   500    | SOME clauses => (comps, clauses))
   501 
   502 val add_mtype_is_concrete = add_notin_mtype_fv Minus
   503 val add_mtype_is_complete = add_notin_mtype_fv Plus
   504 
   505 val bool_table =
   506   [(Gen, (false, false)),
   507    (New, (false, true)),
   508    (Fls, (true, false)),
   509    (Tru, (true, true))]
   510 
   511 fun fst_var n = 2 * n
   512 fun snd_var n = 2 * n + 1
   513 
   514 val bools_from_annotation = AList.lookup (op =) bool_table #> the
   515 val annotation_from_bools = AList.find (op =) bool_table #> the_single
   516 
   517 fun prop_for_bool b = if b then PL.True else PL.False
   518 fun prop_for_bool_var_equality (v1, v2) =
   519   PL.SAnd (PL.SOr (PL.BoolVar v1, PL.SNot (PL.BoolVar v2)),
   520            PL.SOr (PL.SNot (PL.BoolVar v1), PL.BoolVar v2))
   521 fun prop_for_assign (x, a) =
   522   let val (b1, b2) = bools_from_annotation a in
   523     PL.SAnd (PL.BoolVar (fst_var x) |> not b1 ? PL.SNot,
   524              PL.BoolVar (snd_var x) |> not b2 ? PL.SNot)
   525   end
   526 fun prop_for_assign_literal (x, (Plus, a)) = prop_for_assign (x, a)
   527   | prop_for_assign_literal (x, (Minus, a)) = PL.SNot (prop_for_assign (x, a))
   528 fun prop_for_atom_assign (A a', a) = prop_for_bool (a = a')
   529   | prop_for_atom_assign (V x, a) = prop_for_assign_literal (x, (Plus, a))
   530 fun prop_for_atom_equality (aa1, A a2) = prop_for_atom_assign (aa1, a2)
   531   | prop_for_atom_equality (A a1, aa2) = prop_for_atom_assign (aa2, a1)
   532   | prop_for_atom_equality (V x1, V x2) =
   533     PL.SAnd (prop_for_bool_var_equality (pairself fst_var (x1, x2)),
   534              prop_for_bool_var_equality (pairself snd_var (x1, x2)))
   535 val prop_for_assign_clause = PL.exists o map prop_for_assign_literal
   536 fun prop_for_exists_var_assign_literal xs a =
   537   PL.exists (map (fn x => prop_for_assign_literal (x, (Plus, a))) xs)
   538 fun prop_for_comp (aa1, aa2, Eq, []) =
   539     PL.SAnd (prop_for_comp (aa1, aa2, Leq, []),
   540              prop_for_comp (aa2, aa1, Leq, []))
   541   | prop_for_comp (aa1, aa2, Neq, []) =
   542     PL.SNot (prop_for_comp (aa1, aa2, Eq, []))
   543   | prop_for_comp (aa1, aa2, Leq, []) =
   544     PL.SOr (prop_for_atom_equality (aa1, aa2), prop_for_atom_assign (aa2, Gen))
   545   | prop_for_comp (aa1, aa2, cmp, xs) =
   546     PL.SOr (prop_for_exists_var_assign_literal xs Gen,
   547             prop_for_comp (aa1, aa2, cmp, []))
   548 
   549 fun extract_assigns max_var assigns asgs =
   550   fold (fn x => fn accum =>
   551            if AList.defined (op =) asgs x then
   552              accum
   553            else case (fst_var x, snd_var x) |> pairself assigns of
   554              (NONE, NONE) => accum
   555            | bp => (x, annotation_from_bools (pairself (the_default false) bp))
   556                    :: accum)
   557        (max_var downto 1) asgs
   558 
   559 fun print_problem comps clauses =
   560   trace_msg (fn () => "*** Problem:\n" ^
   561                       cat_lines (map string_for_comp comps @
   562                                  map (string_for_assign_clause o SOME) clauses))
   563 
   564 fun print_solution asgs =
   565   trace_msg (fn () => "*** Solution:\n" ^
   566       (asgs
   567        |> map swap
   568        |> AList.group (op =)
   569        |> map (fn (a, xs) => string_for_annotation a ^ ": " ^
   570                              string_for_vars ", " (sort int_ord xs))
   571        |> space_implode "\n"))
   572 
   573 (* The ML solver timeout should correspond more or less to the overhead of
   574    invoking an external prover. *)
   575 val ml_solver_timeout = SOME (seconds 0.02)
   576 
   577 fun solve tac_timeout max_var (comps, clauses) =
   578   let
   579     val asgs =
   580       map_filter (fn [(x, (Plus, a))] => SOME (x, a) | _ => NONE) clauses
   581     fun do_assigns assigns =
   582       SOME (extract_assigns max_var assigns asgs |> tap print_solution)
   583     val _ = print_problem comps clauses
   584     val prop =
   585       PL.all (map prop_for_comp comps @ map prop_for_assign_clause clauses)
   586   in
   587     if PL.eval (K false) prop then
   588       do_assigns (K (SOME false))
   589     else if PL.eval (K true) prop then
   590       do_assigns (K (SOME true))
   591     else
   592       let
   593         (* use the first ML solver (to avoid startup overhead) *)
   594         val (ml_solvers, nonml_solvers) =
   595           !SatSolver.solvers
   596           |> List.partition (member (op =) ["dptsat", "dpll"] o fst)
   597         val res =
   598           if null nonml_solvers then
   599             time_limit tac_timeout (snd (hd ml_solvers)) prop
   600           else
   601             time_limit ml_solver_timeout (snd (hd ml_solvers)) prop
   602             handle TimeLimit.TimeOut =>
   603                    time_limit tac_timeout (SatSolver.invoke_solver "auto") prop
   604       in
   605         case res of
   606           SatSolver.SATISFIABLE assigns => do_assigns assigns
   607         | _ => (trace_msg (K "*** Unsolvable"); NONE)
   608       end
   609       handle TimeLimit.TimeOut => (trace_msg (K "*** Timed out"); NONE)
   610   end
   611 
   612 type mcontext =
   613   {bound_Ts: typ list,
   614    bound_Ms: mtyp list,
   615    frame: (int * annotation_atom) list,
   616    frees: (styp * mtyp) list,
   617    consts: (styp * mtyp) list}
   618 
   619 fun string_for_bound ctxt Ms (j, aa) =
   620   Syntax.string_of_term ctxt (Bound (length Ms - j - 1)) ^ " :\<^bsup>" ^
   621   string_for_annotation_atom aa ^ "\<^esup> " ^
   622   string_for_mtype (nth Ms (length Ms - j - 1))
   623 fun string_for_free relevant_frees ((s, _), M) =
   624   if member (op =) relevant_frees s then SOME (s ^ " : " ^ string_for_mtype M)
   625   else NONE
   626 fun string_for_mcontext ctxt t {bound_Ms, frame, frees, ...} =
   627   (map_filter (string_for_free (Term.add_free_names t [])) frees @
   628    map (string_for_bound ctxt bound_Ms) frame)
   629   |> commas |> enclose "[" "]"
   630 
   631 val initial_gamma =
   632   {bound_Ts = [], bound_Ms = [], frame = [], frees = [], consts = []}
   633 
   634 fun push_bound aa T M {bound_Ts, bound_Ms, frame, frees, consts} =
   635   {bound_Ts = T :: bound_Ts, bound_Ms = M :: bound_Ms,
   636    frame = frame @ [(length bound_Ts, aa)], frees = frees, consts = consts}
   637 fun pop_bound {bound_Ts, bound_Ms, frame, frees, consts} =
   638   {bound_Ts = tl bound_Ts, bound_Ms = tl bound_Ms,
   639    frame = frame |> filter_out (fn (j, _) => j = length bound_Ts - 1),
   640    frees = frees, consts = consts}
   641   handle List.Empty => initial_gamma (* FIXME: needed? *)
   642 
   643 fun set_frame frame ({bound_Ts, bound_Ms, frees, consts, ...} : mcontext) =
   644   {bound_Ts = bound_Ts, bound_Ms = bound_Ms, frame = frame, frees = frees,
   645    consts = consts}
   646 
   647 (* FIXME: make sure tracing messages are complete *)
   648 
   649 fun add_comp_frame aa cmp = fold (add_annotation_atom_comp cmp [] aa o snd)
   650 
   651 fun add_bound_frame j frame =
   652   let
   653     val (new_frame, gen_frame) = List.partition (curry (op =) j o fst) frame
   654   in
   655     add_comp_frame (A New) Leq new_frame
   656     #> add_comp_frame (A Gen) Eq gen_frame
   657   end
   658 
   659 fun fresh_frame ({max_fresh, ...} : mdata) fls tru =
   660   map (apsnd (fn aa =>
   661                  case (aa, fls, tru) of
   662                    (A Fls, SOME a, _) => A a
   663                  | (A Tru, _, SOME a) => A a
   664                  | (A Gen, _, _) => A Gen
   665                  | _ => V (Unsynchronized.inc max_fresh)))
   666 
   667 fun conj_clauses res_aa aa1 aa2 =
   668   [[(aa1, (Neq, Tru)), (aa2, (Neq, Tru)), (res_aa, (Eq, Tru))],
   669    [(aa1, (Neq, Fls)), (res_aa, (Eq, Fls))],
   670    [(aa2, (Neq, Fls)), (res_aa, (Eq, Fls))],
   671    [(aa1, (Neq, Gen)), (aa2, (Eq, Fls)), (res_aa, (Eq, Gen))],
   672    [(aa1, (Neq, New)), (aa2, (Eq, Fls)), (res_aa, (Eq, Gen))],
   673    [(aa1, (Eq, Fls)), (aa2, (Neq, Gen)), (res_aa, (Eq, Gen))],
   674    [(aa1, (Eq, Fls)), (aa2, (Neq, New)), (res_aa, (Eq, Gen))]]
   675 
   676 fun disj_clauses res_aa aa1 aa2 =
   677   [[(aa1, (Neq, Tru)), (res_aa, (Eq, Tru))],
   678    [(aa2, (Neq, Tru)), (res_aa, (Eq, Tru))],
   679    [(aa1, (Neq, Fls)), (aa2, (Neq, Fls)), (res_aa, (Eq, Fls))],
   680    [(aa1, (Neq, Gen)), (aa2, (Eq, Tru)), (res_aa, (Eq, Gen))],
   681    [(aa1, (Neq, New)), (aa2, (Eq, Tru)), (res_aa, (Eq, Gen))],
   682    [(aa1, (Eq, Tru)), (aa2, (Neq, Gen)), (res_aa, (Eq, Gen))],
   683    [(aa1, (Eq, Tru)), (aa2, (Neq, New)), (res_aa, (Eq, Gen))]]
   684 
   685 fun imp_clauses res_aa aa1 aa2 =
   686   [[(aa1, (Neq, Fls)), (res_aa, (Eq, Tru))],
   687    [(aa2, (Neq, Tru)), (res_aa, (Eq, Tru))],
   688    [(aa1, (Neq, Tru)), (aa2, (Neq, Fls)), (res_aa, (Eq, Fls))],
   689    [(aa1, (Neq, Gen)), (aa2, (Eq, Tru)), (res_aa, (Eq, Gen))],
   690    [(aa1, (Neq, New)), (aa2, (Eq, Tru)), (res_aa, (Eq, Gen))],
   691    [(aa1, (Eq, Fls)), (aa2, (Neq, Gen)), (res_aa, (Eq, Gen))],
   692    [(aa1, (Eq, Fls)), (aa2, (Neq, New)), (res_aa, (Eq, Gen))]]
   693 
   694 val meta_conj_triple = ("\<and>", conj_clauses, @{const Pure.conjunction})
   695 val meta_imp_triple = ("\<implies>", imp_clauses, @{const "==>"})
   696 val conj_triple = ("\<and>", conj_clauses, @{const conj})
   697 val disj_triple = ("\<or>", disj_clauses, @{const disj})
   698 val imp_triple = ("\<implies>", imp_clauses, @{const implies})
   699 
   700 fun add_annotation_clause_from_quasi_clause _ NONE = NONE
   701   | add_annotation_clause_from_quasi_clause [] accum = accum
   702   | add_annotation_clause_from_quasi_clause ((aa, (cmp, a)) :: rest) accum =
   703     case aa of
   704       A a' => if annotation_comp cmp a' a then NONE
   705               else add_annotation_clause_from_quasi_clause rest accum
   706     | V x => add_annotation_clause_from_quasi_clause rest accum
   707              |> Option.map (cons (x, (sign_for_comp_op cmp, a)))
   708 
   709 fun assign_clause_from_quasi_clause unless =
   710   add_annotation_clause_from_quasi_clause unless (SOME [])
   711 
   712 fun add_connective_var conn mk_quasi_clauses res_aa aa1 aa2 =
   713   (trace_msg (fn () => "*** Add " ^ string_for_annotation_atom res_aa ^ " = " ^
   714                        string_for_annotation_atom aa1 ^ " " ^ conn ^ " " ^
   715                        string_for_annotation_atom aa2);
   716    fold (add_assign_clause o assign_clause_from_quasi_clause)
   717         (mk_quasi_clauses res_aa aa1 aa2))
   718 fun add_connective_frames conn mk_quasi_clauses res_frame frame1 frame2 =
   719   fold I (map3 (fn (_, res_aa) => fn (_, aa1) => fn (_, aa2) =>
   720                    add_connective_var conn mk_quasi_clauses res_aa aa1 aa2)
   721                res_frame frame1 frame2)
   722 
   723 fun kill_unused_in_frame is_in (accum as ({frame, ...}, _)) =
   724   let val (used_frame, unused_frame) = List.partition is_in frame in
   725     accum |>> set_frame used_frame
   726           ||> add_comp_frame (A Gen) Eq unused_frame
   727   end
   728 
   729 fun split_frame is_in_fun (gamma as {frame, ...}, cset) =
   730   let
   731     fun bubble fun_frame arg_frame [] cset =
   732         ((rev fun_frame, rev arg_frame), cset)
   733       | bubble fun_frame arg_frame ((bound as (_, aa)) :: rest) cset =
   734         if is_in_fun bound then
   735           bubble (bound :: fun_frame) arg_frame rest
   736                  (cset |> add_comp_frame aa Leq arg_frame)
   737         else
   738           bubble fun_frame (bound :: arg_frame) rest cset
   739   in cset |> bubble [] [] frame ||> pair gamma end
   740 
   741 fun add_annotation_atom_comp_alt _ (A Gen) _ _ = I
   742   | add_annotation_atom_comp_alt _ (A _) _ _ =
   743     (trace_msg (K "*** Expected G"); raise UNSOLVABLE ())
   744   | add_annotation_atom_comp_alt cmp (V x) aa1 aa2 =
   745     add_annotation_atom_comp cmp [x] aa1 aa2
   746 
   747 fun add_arg_order1 ((_, aa), (_, prev_aa)) = I
   748   add_annotation_atom_comp_alt Neq prev_aa (A Gen) aa
   749 fun add_app1 fun_aa ((_, res_aa), (_, arg_aa)) = I
   750   let
   751     val clause = [(arg_aa, (Eq, New)), (res_aa, (Eq, Gen))]
   752                  |> assign_clause_from_quasi_clause
   753   in
   754     trace_msg (fn () => "*** Add " ^ string_for_assign_clause clause);
   755     apsnd (add_assign_clause clause)
   756     #> add_annotation_atom_comp_alt Leq arg_aa fun_aa res_aa
   757   end
   758 fun add_app _ [] [] = I
   759   | add_app fun_aa res_frame arg_frame =
   760     add_comp_frame (A New) Leq arg_frame
   761     #> fold add_arg_order1 (tl arg_frame ~~ (fst (split_last arg_frame)))
   762     #> fold (add_app1 fun_aa) (res_frame ~~ arg_frame)
   763 
   764 fun consider_connective mdata (conn, mk_quasi_clauses, t0) do_t1 do_t2
   765                         (accum as ({frame, ...}, _)) =
   766   let
   767     val mtype_for = fresh_mtype_for_type mdata false
   768     val frame1 = fresh_frame mdata (SOME Tru) NONE frame
   769     val frame2 = fresh_frame mdata (SOME Fls) NONE frame
   770     val (m1, accum) = accum |>> set_frame frame1 |> do_t1
   771     val (m2, accum) = accum |>> set_frame frame2 |> do_t2
   772   in
   773     (MApp (MApp (MRaw (t0, mtype_for (fastype_of t0)), m1), m2),
   774      accum |>> set_frame frame
   775            ||> apsnd (add_connective_frames conn mk_quasi_clauses frame frame1
   776                                             frame2))
   777   end
   778 
   779 fun consider_term (mdata as {hol_ctxt = {thy, ctxt, stds, ...}, alpha_T,
   780                              max_fresh, ...}) =
   781   let
   782     fun is_enough_eta_expanded t =
   783       case strip_comb t of
   784         (Const x, ts) => the_default 0 (arity_of_built_in_const thy stds x)
   785         <= length ts
   786       | _ => true
   787     val mtype_for = fresh_mtype_for_type mdata false
   788     fun do_all T (gamma, cset) =
   789       let
   790         val abs_M = mtype_for (domain_type (domain_type T))
   791         val x = Unsynchronized.inc max_fresh
   792         val body_M = mtype_for (body_type T)
   793       in
   794         (MFun (MFun (abs_M, V x, body_M), A Gen, body_M),
   795          (gamma, cset |> add_mtype_is_complete [(x, (Plus, Tru))] abs_M))
   796       end
   797     fun do_equals T (gamma, cset) =
   798       let
   799         val M = mtype_for (domain_type T)
   800         val x = Unsynchronized.inc max_fresh
   801       in
   802         (MFun (M, A Gen, MFun (M, V x, mtype_for (nth_range_type 2 T))),
   803          (gamma, cset |> add_mtype_is_concrete [] M
   804                       |> add_annotation_atom_comp Leq [] (A Fls) (V x)))
   805       end
   806     fun do_robust_set_operation T (gamma, cset) =
   807       let
   808         val set_T = domain_type T
   809         val M1 = mtype_for set_T
   810         val M2 = mtype_for set_T
   811         val M3 = mtype_for set_T
   812       in
   813         (MFun (M1, A Gen, MFun (M2, A Gen, M3)),
   814          (gamma, cset |> add_is_sub_mtype M1 M3 |> add_is_sub_mtype M2 M3))
   815       end
   816     fun do_fragile_set_operation T (gamma, cset) =
   817       let
   818         val set_T = domain_type T
   819         val set_M = mtype_for set_T
   820         fun custom_mtype_for (T as Type (@{type_name fun}, [T1, T2])) =
   821             if T = set_T then set_M
   822             else MFun (custom_mtype_for T1, A Gen, custom_mtype_for T2)
   823           | custom_mtype_for T = mtype_for T
   824       in
   825         (custom_mtype_for T, (gamma, cset |> add_mtype_is_concrete [] set_M))
   826       end
   827     fun do_pair_constr T accum =
   828       case mtype_for (nth_range_type 2 T) of
   829         M as MPair (a_M, b_M) =>
   830         (MFun (a_M, A Gen, MFun (b_M, A Gen, M)), accum)
   831       | M => raise MTYPE ("Nitpick_Mono.consider_term.do_pair_constr", [M], [])
   832     fun do_nth_pair_sel n T =
   833       case mtype_for (domain_type T) of
   834         M as MPair (a_M, b_M) =>
   835         pair (MFun (M, A Gen, if n = 0 then a_M else b_M))
   836       | M => raise MTYPE ("Nitpick_Mono.consider_term.do_nth_pair_sel", [M], [])
   837     and do_connect triple t1 t2 =
   838       consider_connective mdata triple (do_term t1) (do_term t2)
   839     and do_term t
   840             (accum as (gamma as {bound_Ts, bound_Ms, frame, frees, consts},
   841                        cset)) =
   842       (trace_msg (fn () => "  " ^ string_for_mcontext ctxt t gamma ^
   843                            " \<turnstile> " ^ Syntax.string_of_term ctxt t ^
   844                            " : _?");
   845        case t of
   846          @{const False} =>
   847          (MRaw (t, bool_M), accum ||> add_comp_frame (A Fls) Leq frame)
   848        | Const (@{const_name None}, T) =>
   849          (MRaw (t, mtype_for T), accum ||> add_comp_frame (A Fls) Leq frame)
   850        | @{const True} =>
   851          (MRaw (t, bool_M), accum ||> add_comp_frame (A Tru) Leq frame)
   852        | (t0 as Const (@{const_name HOL.eq}, _)) $ Bound 0 $ t2 =>
   853          (* hack to exploit symmetry of equality when typing "insert" *)
   854          (if t2 = Bound 0 then do_term @{term True}
   855           else do_term (t0 $ t2 $ Bound 0)) accum
   856        | Const (x as (s, T)) =>
   857          (case AList.lookup (op =) consts x of
   858             SOME M => (M, accum)
   859           | NONE =>
   860             if not (could_exist_alpha_subtype alpha_T T) then
   861               (mtype_for T, accum)
   862             else case s of
   863               @{const_name all} => do_all T accum
   864             | @{const_name "=="} => do_equals T accum
   865             | @{const_name All} => do_all T accum
   866             | @{const_name Ex} =>
   867               let val set_T = domain_type T in
   868                 do_term (Abs (Name.uu, set_T,
   869                               @{const Not} $ (HOLogic.mk_eq
   870                                   (Abs (Name.uu, domain_type set_T,
   871                                         @{const False}),
   872                                    Bound 0)))) accum
   873                 |>> mtype_of_mterm
   874               end
   875             | @{const_name HOL.eq} => do_equals T accum
   876             | @{const_name The} =>
   877               (trace_msg (K "*** The"); raise UNSOLVABLE ())
   878             | @{const_name Eps} =>
   879               (trace_msg (K "*** Eps"); raise UNSOLVABLE ())
   880             | @{const_name If} =>
   881               do_robust_set_operation (range_type T) accum
   882               |>> curry3 MFun bool_M (A Gen)
   883             | @{const_name Pair} => do_pair_constr T accum
   884             | @{const_name fst} => do_nth_pair_sel 0 T accum
   885             | @{const_name snd} => do_nth_pair_sel 1 T accum
   886             | @{const_name Id} =>
   887               (MFun (mtype_for (domain_type T), A Gen, bool_M), accum)
   888             | @{const_name converse} =>
   889               let
   890                 val x = Unsynchronized.inc max_fresh
   891                 fun mtype_for_set T =
   892                   MFun (mtype_for (domain_type T), V x, bool_M)
   893                 val ab_set_M = domain_type T |> mtype_for_set
   894                 val ba_set_M = range_type T |> mtype_for_set
   895               in
   896                 (MFun (ab_set_M, A Gen, ba_set_M),
   897                  accum ||> add_annotation_atom_comp Neq [] (V x) (A New))
   898               end
   899             | @{const_name trancl} => do_fragile_set_operation T accum
   900             | @{const_name rel_comp} =>
   901               let
   902                 val x = Unsynchronized.inc max_fresh
   903                 fun mtype_for_set T =
   904                   MFun (mtype_for (domain_type T), V x, bool_M)
   905                 val bc_set_M = domain_type T |> mtype_for_set
   906                 val ab_set_M = domain_type (range_type T) |> mtype_for_set
   907                 val ac_set_M = nth_range_type 2 T |> mtype_for_set
   908               in
   909                 (MFun (bc_set_M, A Gen, MFun (ab_set_M, A Gen, ac_set_M)),
   910                  accum ||> add_annotation_atom_comp Neq [] (V x) (A New))
   911               end
   912             | @{const_name finite} =>
   913               let
   914                 val M1 = mtype_for (domain_type (domain_type T))
   915                 val a = if exists_alpha_sub_mtype M1 then Fls else Gen
   916               in (MFun (MFun (M1, A a, bool_M), A Gen, bool_M), accum) end
   917             | @{const_name prod} =>
   918               let
   919                 val x = Unsynchronized.inc max_fresh
   920                 fun mtype_for_set T =
   921                   MFun (mtype_for (domain_type T), V x, bool_M)
   922                 val a_set_M = mtype_for_set (domain_type T)
   923                 val b_set_M =
   924                   mtype_for_set (range_type (domain_type (range_type T)))
   925                 val ab_set_M = mtype_for_set (nth_range_type 2 T)
   926               in
   927                 (MFun (a_set_M, A Gen, MFun (b_set_M, A Gen, ab_set_M)),
   928                  accum ||> add_annotation_atom_comp Neq [] (V x) (A New))
   929               end
   930             | _ =>
   931               if s = @{const_name safe_The} then
   932                 let
   933                   val a_set_M = mtype_for (domain_type T)
   934                   val a_M = dest_MFun a_set_M |> #1
   935                 in (MFun (a_set_M, A Gen, a_M), accum) end
   936               else if s = @{const_name ord_class.less_eq} andalso
   937                       is_set_type (domain_type T) then
   938                 do_fragile_set_operation T accum
   939               else if is_sel s then
   940                 (mtype_for_sel mdata x, accum)
   941               else if is_constr ctxt stds x then
   942                 (mtype_for_constr mdata x, accum)
   943               else if is_built_in_const thy stds x then
   944                 (fresh_mtype_for_type mdata true T, accum)
   945               else
   946                 let val M = mtype_for T in
   947                   (M, ({bound_Ts = bound_Ts, bound_Ms = bound_Ms, frame = frame,
   948                         frees = frees, consts = (x, M) :: consts}, cset))
   949                 end)
   950            |>> curry MRaw t
   951            ||> apsnd (add_comp_frame (A Gen) Eq frame)
   952          | Free (x as (_, T)) =>
   953            (case AList.lookup (op =) frees x of
   954               SOME M => (M, accum)
   955             | NONE =>
   956               let val M = mtype_for T in
   957                 (M, ({bound_Ts = bound_Ts, bound_Ms = bound_Ms, frame = frame,
   958                       frees = (x, M) :: frees, consts = consts}, cset))
   959               end)
   960            |>> curry MRaw t ||> apsnd (add_comp_frame (A Gen) Eq frame)
   961          | Var _ => (trace_msg (K "*** Var"); raise UNSOLVABLE ())
   962          | Bound j =>
   963            (MRaw (t, nth bound_Ms j),
   964             accum ||> add_bound_frame (length bound_Ts - j - 1) frame)
   965          | Abs (s, T, t') =>
   966            (case fin_fun_body T (fastype_of1 (T :: bound_Ts, t')) t' of
   967               SOME t' =>
   968               let
   969                 val M = mtype_for T
   970                 val x = Unsynchronized.inc max_fresh
   971                 val (m', accum) = do_term t' (accum |>> push_bound (V x) T M)
   972               in
   973                 (MAbs (s, T, M, V x, m'),
   974                  accum |>> pop_bound
   975                        ||> add_annotation_atom_comp Leq [] (A Fls) (V x))
   976               end
   977             | NONE =>
   978               ((case t' of
   979                   t1' $ Bound 0 =>
   980                   if not (loose_bvar1 (t1', 0)) andalso
   981                      is_enough_eta_expanded t1' then
   982                     do_term (incr_boundvars ~1 t1') accum
   983                   else
   984                     raise SAME ()
   985                 | (t11 as Const (@{const_name HOL.eq}, _)) $ Bound 0 $ t13 =>
   986                   if not (loose_bvar1 (t13, 0)) then
   987                     do_term (incr_boundvars ~1 (t11 $ t13)) accum
   988                   else
   989                     raise SAME ()
   990                 | _ => raise SAME ())
   991                handle SAME () =>
   992                       let
   993                         val M = mtype_for T
   994                         val x = Unsynchronized.inc max_fresh
   995                         val (m', accum) =
   996                           do_term t' (accum |>> push_bound (V x) T M)
   997                       in (MAbs (s, T, M, V x, m'), accum |>> pop_bound) end))
   998          | @{const Not} $ t1 => do_connect imp_triple t1 @{const False} accum
   999          | @{const conj} $ t1 $ t2 => do_connect conj_triple t1 t2 accum
  1000          | @{const disj} $ t1 $ t2 => do_connect disj_triple t1 t2 accum
  1001          | @{const implies} $ t1 $ t2 => do_connect imp_triple t1 t2 accum
  1002          | Const (@{const_name Let}, _) $ t1 $ t2 =>
  1003            do_term (betapply (t2, t1)) accum
  1004          | t1 $ t2 =>
  1005            let
  1006              fun is_in t (j, _) = loose_bvar1 (t, length bound_Ts - j - 1)
  1007              val accum as ({frame, ...}, _) =
  1008                accum |> kill_unused_in_frame (is_in t)
  1009              val ((frame1a, frame1b), accum) = accum |> split_frame (is_in t1)
  1010              val frame2a = frame1a |> map (apsnd (K (A Gen)))
  1011              val frame2b =
  1012                frame1b |> map (apsnd (fn _ => V (Unsynchronized.inc max_fresh)))
  1013              val frame2 = frame2a @ frame2b
  1014              val (m1, accum) = accum |>> set_frame frame1a |> do_term t1
  1015              val (m2, accum) = accum |>> set_frame frame2 |> do_term t2
  1016            in
  1017              let
  1018                val (M11, aa, _) = mtype_of_mterm m1 |> dest_MFun
  1019                val M2 = mtype_of_mterm m2
  1020              in
  1021                (MApp (m1, m2),
  1022                 accum |>> set_frame frame
  1023                       ||> add_is_sub_mtype M2 M11
  1024                       ||> add_app aa frame1b frame2b)
  1025              end
  1026            end)
  1027         |> tap (fn (m, (gamma, _)) =>
  1028                    trace_msg (fn () => "  " ^ string_for_mcontext ctxt t gamma ^
  1029                                        " \<turnstile> " ^
  1030                                        string_for_mterm ctxt m))
  1031   in do_term end
  1032 
  1033 fun force_gen_funs 0 _ = I
  1034   | force_gen_funs n (M as MFun (M1, _, M2)) =
  1035     add_mtypes_equal M (MFun (M1, A Gen, M2)) #> force_gen_funs (n - 1) M2
  1036   | force_gen_funs _ M = raise MTYPE ("Nitpick_Mono.force_gen_funs", [M], [])
  1037 fun consider_general_equals mdata def (x as (_, T)) t1 t2 accum =
  1038   let
  1039     val (m1, accum) = consider_term mdata t1 accum
  1040     val (m2, accum) = consider_term mdata t2 accum
  1041     val M1 = mtype_of_mterm m1
  1042     val M2 = mtype_of_mterm m2
  1043     val accum = accum ||> add_mtypes_equal M1 M2
  1044     val body_M = fresh_mtype_for_type mdata false (nth_range_type 2 T)
  1045     val m = MApp (MApp (MRaw (Const x,
  1046                            MFun (M1, A Gen, MFun (M2, A Gen, body_M))), m1), m2)
  1047   in
  1048     (m, (if def then
  1049            let val (head_m, arg_ms) = strip_mcomb m1 in
  1050              accum ||> force_gen_funs (length arg_ms) (mtype_of_mterm head_m)
  1051            end
  1052          else
  1053            accum))
  1054   end
  1055 
  1056 fun consider_general_formula (mdata as {hol_ctxt = {ctxt, ...}, max_fresh,
  1057                                         ...}) =
  1058   let
  1059     val mtype_for = fresh_mtype_for_type mdata false
  1060     val do_term = consider_term mdata
  1061     fun do_formula sn t (accum as (gamma, _)) =
  1062       let
  1063         fun do_quantifier (quant_x as (quant_s, _)) abs_s abs_T body_t =
  1064           let
  1065             val abs_M = mtype_for abs_T
  1066             val x = Unsynchronized.inc max_fresh
  1067             val side_cond = ((sn = Minus) = (quant_s = @{const_name Ex}))
  1068             fun ann () = if quant_s = @{const_name Ex} then Fls else Tru
  1069             val (body_m, accum) =
  1070               accum ||> side_cond
  1071                         ? add_mtype_is_complete [(x, (Plus, ann ()))] abs_M
  1072                     |>> push_bound (V x) abs_T abs_M |> do_formula sn body_t
  1073             val body_M = mtype_of_mterm body_m
  1074           in
  1075             (MApp (MRaw (Const quant_x,
  1076                          MFun (MFun (abs_M, A Gen, body_M), A Gen, body_M)),
  1077                    MAbs (abs_s, abs_T, abs_M, A Gen, body_m)),
  1078              accum |>> pop_bound)
  1079           end
  1080         fun do_connect triple neg1 t1 t2 =
  1081           consider_connective mdata triple
  1082               (do_formula (sn |> neg1 ? negate_sign) t1) (do_formula sn t2)
  1083         fun do_equals x t1 t2 =
  1084           case sn of
  1085             Plus => do_term t accum
  1086           | Minus => consider_general_equals mdata false x t1 t2 accum
  1087       in
  1088         trace_msg (fn () => "  " ^ string_for_mcontext ctxt t gamma ^
  1089                             " \<turnstile> " ^ Syntax.string_of_term ctxt t ^
  1090                             " : o\<^sup>" ^ string_for_sign sn ^ "?");
  1091         case t of
  1092           Const (x as (@{const_name all}, _)) $ Abs (s1, T1, t1) =>
  1093           do_quantifier x s1 T1 t1
  1094         | Const (x as (@{const_name "=="}, _)) $ t1 $ t2 => do_equals x t1 t2
  1095         | @{const Trueprop} $ t1 =>
  1096           let val (m1, accum) = do_formula sn t1 accum in
  1097             (MApp (MRaw (@{const Trueprop}, mtype_for (bool_T --> prop_T)), m1),
  1098              accum)
  1099           end
  1100         | Const (x as (@{const_name All}, _)) $ Abs (s1, T1, t1) =>
  1101           do_quantifier x s1 T1 t1
  1102         | Const (x0 as (@{const_name Ex}, T0)) $ (t1 as Abs (s1, T1, t1')) =>
  1103           (case sn of
  1104              Plus => do_quantifier x0 s1 T1 t1'
  1105            | Minus =>
  1106              (* FIXME: Move elsewhere *)
  1107              do_term (@{const Not}
  1108                       $ (HOLogic.eq_const (domain_type T0) $ t1
  1109                          $ Abs (Name.uu, T1, @{const False}))) accum)
  1110         | Const (x as (@{const_name HOL.eq}, _)) $ t1 $ t2 => do_equals x t1 t2
  1111         | Const (@{const_name Let}, _) $ t1 $ t2 =>
  1112           do_formula sn (betapply (t2, t1)) accum
  1113         | @{const Pure.conjunction} $ t1 $ t2 =>
  1114           do_connect meta_conj_triple false t1 t2 accum
  1115         | @{const "==>"} $ t1 $ t2 =>
  1116           do_connect meta_imp_triple true t1 t2 accum
  1117         | @{const Not} $ t1 =>
  1118           do_connect imp_triple true t1 @{const False} accum
  1119         | @{const conj} $ t1 $ t2 => do_connect conj_triple false t1 t2 accum
  1120         | @{const disj} $ t1 $ t2 => do_connect disj_triple false t1 t2 accum
  1121         | @{const implies} $ t1 $ t2 => do_connect imp_triple true t1 t2 accum
  1122         | _ => do_term t accum
  1123       end
  1124       |> tap (fn (m, (gamma, _)) =>
  1125                  trace_msg (fn () => string_for_mcontext ctxt t gamma ^
  1126                                      " \<turnstile> " ^
  1127                                      string_for_mterm ctxt m ^ " : o\<^sup>" ^
  1128                                      string_for_sign sn))
  1129   in do_formula end
  1130 
  1131 (* The harmless axiom optimization below is somewhat too aggressive in the face
  1132    of (rather peculiar) user-defined axioms. *)
  1133 val harmless_consts =
  1134   [@{const_name ord_class.less}, @{const_name ord_class.less_eq}]
  1135 val bounteous_consts = [@{const_name bisim}]
  1136 
  1137 fun is_harmless_axiom ({no_harmless = true, ...} : mdata) _ = false
  1138   | is_harmless_axiom {hol_ctxt = {thy, stds, ...}, ...} t =
  1139     Term.add_consts t []
  1140     |> filter_out (is_built_in_const thy stds)
  1141     |> (forall (member (op =) harmless_consts o original_name o fst) orf
  1142         exists (member (op =) bounteous_consts o fst))
  1143 
  1144 fun consider_nondefinitional_axiom mdata t =
  1145   if is_harmless_axiom mdata t then pair (MRaw (t, dummy_M))
  1146   else consider_general_formula mdata Plus t
  1147 
  1148 fun consider_definitional_axiom (mdata as {hol_ctxt = {ctxt, ...}, ...}) t =
  1149   if not (is_constr_pattern_formula ctxt t) then
  1150     consider_nondefinitional_axiom mdata t
  1151   else if is_harmless_axiom mdata t then
  1152     pair (MRaw (t, dummy_M))
  1153   else
  1154     let
  1155       val mtype_for = fresh_mtype_for_type mdata false
  1156       val do_term = consider_term mdata
  1157       fun do_all quant_t abs_s abs_T body_t accum =
  1158         let
  1159           val abs_M = mtype_for abs_T
  1160           val (body_m, accum) =
  1161             accum |>> push_bound (A Gen) abs_T abs_M |> do_formula body_t
  1162           val body_M = mtype_of_mterm body_m
  1163         in
  1164           (MApp (MRaw (quant_t, MFun (MFun (abs_M, A Gen, body_M), A Gen,
  1165                        body_M)),
  1166                  MAbs (abs_s, abs_T, abs_M, A Gen, body_m)),
  1167            accum |>> pop_bound)
  1168         end
  1169       and do_conjunction t0 t1 t2 accum =
  1170         let
  1171           val (m1, accum) = do_formula t1 accum
  1172           val (m2, accum) = do_formula t2 accum
  1173         in
  1174           (MApp (MApp (MRaw (t0, mtype_for (fastype_of t0)), m1), m2), accum)
  1175         end
  1176       and do_implies t0 t1 t2 accum =
  1177         let
  1178           val (m1, accum) = do_term t1 accum
  1179           val (m2, accum) = do_formula t2 accum
  1180         in
  1181           (MApp (MApp (MRaw (t0, mtype_for (fastype_of t0)), m1), m2), accum)
  1182         end
  1183       and do_formula t accum =
  1184         case t of
  1185           (t0 as Const (@{const_name all}, _)) $ Abs (s1, T1, t1) =>
  1186           do_all t0 s1 T1 t1 accum
  1187         | @{const Trueprop} $ t1 =>
  1188           let val (m1, accum) = do_formula t1 accum in
  1189             (MApp (MRaw (@{const Trueprop}, mtype_for (bool_T --> prop_T)), m1),
  1190              accum)
  1191           end
  1192         | Const (x as (@{const_name "=="}, _)) $ t1 $ t2 =>
  1193           consider_general_equals mdata true x t1 t2 accum
  1194         | (t0 as @{const "==>"}) $ t1 $ t2 => do_implies t0 t1 t2 accum
  1195         | (t0 as @{const Pure.conjunction}) $ t1 $ t2 =>
  1196           do_conjunction t0 t1 t2 accum
  1197         | (t0 as Const (@{const_name All}, _)) $ Abs (s0, T1, t1) =>
  1198           do_all t0 s0 T1 t1 accum
  1199         | Const (x as (@{const_name HOL.eq}, _)) $ t1 $ t2 =>
  1200           consider_general_equals mdata true x t1 t2 accum
  1201         | (t0 as @{const conj}) $ t1 $ t2 => do_conjunction t0 t1 t2 accum
  1202         | (t0 as @{const implies}) $ t1 $ t2 => do_implies t0 t1 t2 accum
  1203         | _ => raise TERM ("Nitpick_Mono.consider_definitional_axiom.\
  1204                            \do_formula", [t])
  1205     in do_formula t end
  1206 
  1207 fun string_for_mtype_of_term ctxt asgs t M =
  1208   Syntax.string_of_term ctxt t ^ " : " ^ string_for_mtype (resolve_mtype asgs M)
  1209 
  1210 fun print_mcontext ctxt asgs ({frees, consts, ...} : mcontext) =
  1211   trace_msg (fn () =>
  1212       map (fn (x, M) => string_for_mtype_of_term ctxt asgs (Free x) M) frees @
  1213       map (fn (x, M) => string_for_mtype_of_term ctxt asgs (Const x) M) consts
  1214       |> cat_lines)
  1215 
  1216 fun amass f t (ms, accum) =
  1217   let val (m, accum) = f t accum in (m :: ms, accum) end
  1218 
  1219 fun infer which no_harmless (hol_ctxt as {ctxt, tac_timeout, ...}) binarize
  1220           alpha_T (nondef_ts, def_ts) =
  1221   let
  1222     val _ = trace_msg (fn () => "****** " ^ which ^ " analysis: " ^
  1223                                 string_for_mtype MAlpha ^ " is " ^
  1224                                 Syntax.string_of_typ ctxt alpha_T)
  1225     val mdata as {max_fresh, constr_mcache, ...} =
  1226       initial_mdata hol_ctxt binarize no_harmless alpha_T
  1227     val accum = (initial_gamma, ([], []))
  1228     val (nondef_ms, accum) =
  1229       ([], accum) |> amass (consider_general_formula mdata Plus) (hd nondef_ts)
  1230                   |> fold (amass (consider_nondefinitional_axiom mdata))
  1231                           (tl nondef_ts)
  1232     val (def_ms, (gamma, cset)) =
  1233       ([], accum) |> fold (amass (consider_definitional_axiom mdata)) def_ts
  1234   in
  1235     case solve tac_timeout (!max_fresh) cset of
  1236       SOME asgs => (print_mcontext ctxt asgs gamma;
  1237                     SOME (asgs, (nondef_ms, def_ms), !constr_mcache))
  1238     | _ => NONE
  1239   end
  1240   handle UNSOLVABLE () => NONE
  1241        | MTYPE (loc, Ms, Ts) =>
  1242          raise BAD (loc, commas (map string_for_mtype Ms @
  1243                                  map (Syntax.string_of_typ ctxt) Ts))
  1244        | MTERM (loc, ms) =>
  1245          raise BAD (loc, commas (map (string_for_mterm ctxt) ms))
  1246 
  1247 val formulas_monotonic = is_some oooo infer "Monotonicity" false
  1248 
  1249 end;