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