src/HOL/Tools/Nitpick/nitpick_hol.ML
changeset 35665 ff2bf50505ab
parent 35625 9c818cab0dd0
child 35671 ed2c3830d881
equal deleted inserted replaced
35664:fee01e85605f 35665:ff2bf50505ab
    63   val s_conj : term * term -> term
    63   val s_conj : term * term -> term
    64   val s_disj : term * term -> term
    64   val s_disj : term * term -> term
    65   val strip_any_connective : term -> term list * term
    65   val strip_any_connective : term -> term list * term
    66   val conjuncts_of : term -> term list
    66   val conjuncts_of : term -> term list
    67   val disjuncts_of : term -> term list
    67   val disjuncts_of : term -> term list
    68   val unarize_and_unbox_type : typ -> typ
    68   val unarize_unbox_etc_type : typ -> typ
    69   val unarize_unbox_and_uniterize_type : typ -> typ
    69   val uniterize_unarize_unbox_etc_type : typ -> typ
    70   val string_for_type : Proof.context -> typ -> string
    70   val string_for_type : Proof.context -> typ -> string
    71   val prefix_name : string -> string -> string
    71   val prefix_name : string -> string -> string
    72   val shortest_name : string -> string
    72   val shortest_name : string -> string
    73   val short_name : string -> string
    73   val short_name : string -> string
    74   val shorten_names_in_term : term -> term
    74   val shorten_names_in_term : term -> term
   146   val discriminate_value : hol_context -> styp -> term -> term
   146   val discriminate_value : hol_context -> styp -> term -> term
   147   val select_nth_constr_arg :
   147   val select_nth_constr_arg :
   148     theory -> (typ option * bool) list -> styp -> term -> int -> typ -> term
   148     theory -> (typ option * bool) list -> styp -> term -> int -> typ -> term
   149   val construct_value :
   149   val construct_value :
   150     theory -> (typ option * bool) list -> styp -> term list -> term
   150     theory -> (typ option * bool) list -> styp -> term list -> term
       
   151   val coerce_term : hol_context -> typ list -> typ -> typ -> term -> term
   151   val card_of_type : (typ * int) list -> typ -> int
   152   val card_of_type : (typ * int) list -> typ -> int
   152   val bounded_card_of_type : int -> int -> (typ * int) list -> typ -> int
   153   val bounded_card_of_type : int -> int -> (typ * int) list -> typ -> int
   153   val bounded_exact_card_of_type :
   154   val bounded_exact_card_of_type :
   154     hol_context -> typ list -> int -> int -> (typ * int) list -> typ -> int
   155     hol_context -> typ list -> int -> int -> (typ * int) list -> typ -> int
   155   val is_finite_type : hol_context -> typ -> bool
   156   val is_finite_type : hol_context -> typ -> bool
       
   157   val is_small_finite_type : hol_context -> typ -> bool
   156   val special_bounds : term list -> (indexname * typ) list
   158   val special_bounds : term list -> (indexname * typ) list
   157   val abs_var : indexname * typ -> term -> term
   159   val abs_var : indexname * typ -> term -> term
   158   val is_funky_typedef : theory -> typ -> bool
   160   val is_funky_typedef : theory -> typ -> bool
   159   val all_axioms_of :
   161   val all_axioms_of :
   160     theory -> (term * term) list -> term list * term list * term list
   162     theory -> (term * term) list -> term list * term list * term list
   399 (* typ -> typ *)
   401 (* typ -> typ *)
   400 fun unarize_type @{typ "unsigned_bit word"} = nat_T
   402 fun unarize_type @{typ "unsigned_bit word"} = nat_T
   401   | unarize_type @{typ "signed_bit word"} = int_T
   403   | unarize_type @{typ "signed_bit word"} = int_T
   402   | unarize_type (Type (s, Ts as _ :: _)) = Type (s, map unarize_type Ts)
   404   | unarize_type (Type (s, Ts as _ :: _)) = Type (s, map unarize_type Ts)
   403   | unarize_type T = T
   405   | unarize_type T = T
   404 fun unarize_and_unbox_type (Type (@{type_name fun_box}, Ts)) =
   406 fun unarize_unbox_etc_type (Type (@{type_name fin_fun}, Ts)) =
   405     unarize_and_unbox_type (Type ("fun", Ts))
   407     unarize_unbox_etc_type (Type (@{type_name fun}, Ts))
   406   | unarize_and_unbox_type (Type (@{type_name pair_box}, Ts)) =
   408   | unarize_unbox_etc_type (Type (@{type_name fun_box}, Ts)) =
   407     Type ("*", map unarize_and_unbox_type Ts)
   409     unarize_unbox_etc_type (Type (@{type_name fun}, Ts))
   408   | unarize_and_unbox_type @{typ "unsigned_bit word"} = nat_T
   410   | unarize_unbox_etc_type (Type (@{type_name pair_box}, Ts)) =
   409   | unarize_and_unbox_type @{typ "signed_bit word"} = int_T
   411     Type (@{type_name "*"}, map unarize_unbox_etc_type Ts)
   410   | unarize_and_unbox_type (Type (s, Ts as _ :: _)) =
   412   | unarize_unbox_etc_type @{typ "unsigned_bit word"} = nat_T
   411     Type (s, map unarize_and_unbox_type Ts)
   413   | unarize_unbox_etc_type @{typ "signed_bit word"} = int_T
   412   | unarize_and_unbox_type T = T
   414   | unarize_unbox_etc_type (Type (s, Ts as _ :: _)) =
       
   415     Type (s, map unarize_unbox_etc_type Ts)
       
   416   | unarize_unbox_etc_type T = T
   413 fun uniterize_type (Type (s, Ts as _ :: _)) = Type (s, map uniterize_type Ts)
   417 fun uniterize_type (Type (s, Ts as _ :: _)) = Type (s, map uniterize_type Ts)
   414   | uniterize_type @{typ bisim_iterator} = nat_T
   418   | uniterize_type @{typ bisim_iterator} = nat_T
   415   | uniterize_type T = T
   419   | uniterize_type T = T
   416 val unarize_unbox_and_uniterize_type = uniterize_type o unarize_and_unbox_type
   420 val uniterize_unarize_unbox_etc_type = uniterize_type o unarize_unbox_etc_type
   417 
   421 
   418 (* Proof.context -> typ -> string *)
   422 (* Proof.context -> typ -> string *)
   419 fun string_for_type ctxt = Syntax.string_of_typ ctxt o unarize_and_unbox_type
   423 fun string_for_type ctxt = Syntax.string_of_typ ctxt o unarize_unbox_etc_type
   420 
   424 
   421 (* string -> string -> string *)
   425 (* string -> string -> string *)
   422 val prefix_name = Long_Name.qualify o Long_Name.base_name
   426 val prefix_name = Long_Name.qualify o Long_Name.base_name
   423 (* string -> string *)
   427 (* string -> string *)
   424 fun shortest_name s = List.last (space_explode "." s) handle List.Empty => ""
   428 fun shortest_name s = List.last (space_explode "." s) handle List.Empty => ""
   437 val shorten_names_in_term =
   441 val shorten_names_in_term =
   438   map_aterms (fn Const (s, T) => Const (short_name s, T) | t => t)
   442   map_aterms (fn Const (s, T) => Const (short_name s, T) | t => t)
   439   #> map_types shorten_names_in_type
   443   #> map_types shorten_names_in_type
   440 
   444 
   441 (* theory -> typ * typ -> bool *)
   445 (* theory -> typ * typ -> bool *)
   442 fun type_match thy (T1, T2) =
   446 fun strict_type_match thy (T1, T2) =
   443   (Sign.typ_match thy (T2, T1) Vartab.empty; true)
   447   (Sign.typ_match thy (T2, T1) Vartab.empty; true)
   444   handle Type.TYPE_MATCH => false
   448   handle Type.TYPE_MATCH => false
       
   449 fun type_match thy = strict_type_match thy o pairself unarize_unbox_etc_type
   445 (* theory -> styp * styp -> bool *)
   450 (* theory -> styp * styp -> bool *)
   446 fun const_match thy ((s1, T1), (s2, T2)) =
   451 fun const_match thy ((s1, T1), (s2, T2)) =
   447   s1 = s2 andalso type_match thy (T1, T2)
   452   s1 = s2 andalso type_match thy (T1, T2)
   448 (* theory -> term * term -> bool *)
   453 (* theory -> term * term -> bool *)
   449 fun term_match thy (Const x1, Const x2) = const_match thy (x1, x2)
   454 fun term_match thy (Const x1, Const x2) = const_match thy (x1, x2)
   452   | term_match _ (t1, t2) = t1 aconv t2
   457   | term_match _ (t1, t2) = t1 aconv t2
   453 
   458 
   454 (* typ -> bool *)
   459 (* typ -> bool *)
   455 fun is_TFree (TFree _) = true
   460 fun is_TFree (TFree _) = true
   456   | is_TFree _ = false
   461   | is_TFree _ = false
   457 fun is_higher_order_type (Type ("fun", _)) = true
   462 fun is_higher_order_type (Type (@{type_name fun}, _)) = true
   458   | is_higher_order_type (Type (_, Ts)) = exists is_higher_order_type Ts
   463   | is_higher_order_type (Type (_, Ts)) = exists is_higher_order_type Ts
   459   | is_higher_order_type _ = false
   464   | is_higher_order_type _ = false
   460 fun is_fun_type (Type ("fun", _)) = true
   465 fun is_fun_type (Type (@{type_name fun}, _)) = true
   461   | is_fun_type _ = false
   466   | is_fun_type _ = false
   462 fun is_set_type (Type ("fun", [_, @{typ bool}])) = true
   467 fun is_set_type (Type (@{type_name fun}, [_, @{typ bool}])) = true
   463   | is_set_type _ = false
   468   | is_set_type _ = false
   464 fun is_pair_type (Type ("*", _)) = true
   469 fun is_pair_type (Type (@{type_name "*"}, _)) = true
   465   | is_pair_type _ = false
   470   | is_pair_type _ = false
   466 fun is_lfp_iterator_type (Type (s, _)) = String.isPrefix lfp_iterator_prefix s
   471 fun is_lfp_iterator_type (Type (s, _)) = String.isPrefix lfp_iterator_prefix s
   467   | is_lfp_iterator_type _ = false
   472   | is_lfp_iterator_type _ = false
   468 fun is_gfp_iterator_type (Type (s, _)) = String.isPrefix gfp_iterator_prefix s
   473 fun is_gfp_iterator_type (Type (s, _)) = String.isPrefix gfp_iterator_prefix s
   469   | is_gfp_iterator_type _ = false
   474   | is_gfp_iterator_type _ = false
   492   | const_for_iterator_type T =
   497   | const_for_iterator_type T =
   493     raise TYPE ("Nitpick_HOL.const_for_iterator_type", [T], [])
   498     raise TYPE ("Nitpick_HOL.const_for_iterator_type", [T], [])
   494 
   499 
   495 (* int -> typ -> typ list * typ *)
   500 (* int -> typ -> typ list * typ *)
   496 fun strip_n_binders 0 T = ([], T)
   501 fun strip_n_binders 0 T = ([], T)
   497   | strip_n_binders n (Type ("fun", [T1, T2])) =
   502   | strip_n_binders n (Type (@{type_name fun}, [T1, T2])) =
   498     strip_n_binders (n - 1) T2 |>> cons T1
   503     strip_n_binders (n - 1) T2 |>> cons T1
   499   | strip_n_binders n (Type (@{type_name fun_box}, Ts)) =
   504   | strip_n_binders n (Type (@{type_name fun_box}, Ts)) =
   500     strip_n_binders n (Type ("fun", Ts))
   505     strip_n_binders n (Type (@{type_name fun}, Ts))
   501   | strip_n_binders _ T = raise TYPE ("Nitpick_HOL.strip_n_binders", [T], [])
   506   | strip_n_binders _ T = raise TYPE ("Nitpick_HOL.strip_n_binders", [T], [])
   502 (* typ -> typ *)
   507 (* typ -> typ *)
   503 val nth_range_type = snd oo strip_n_binders
   508 val nth_range_type = snd oo strip_n_binders
   504 
   509 
   505 (* typ -> int *)
   510 (* typ -> int *)
   506 fun num_factors_in_type (Type ("*", [T1, T2])) =
   511 fun num_factors_in_type (Type (@{type_name "*"}, [T1, T2])) =
   507     fold (Integer.add o num_factors_in_type) [T1, T2] 0
   512     fold (Integer.add o num_factors_in_type) [T1, T2] 0
   508   | num_factors_in_type _ = 1
   513   | num_factors_in_type _ = 1
   509 fun num_binder_types (Type ("fun", [_, T2])) = 1 + num_binder_types T2
   514 fun num_binder_types (Type (@{type_name fun}, [_, T2])) =
       
   515     1 + num_binder_types T2
   510   | num_binder_types _ = 0
   516   | num_binder_types _ = 0
   511 (* typ -> typ list *)
   517 (* typ -> typ list *)
   512 val curried_binder_types = maps HOLogic.flatten_tupleT o binder_types
   518 val curried_binder_types = maps HOLogic.flatten_tupleT o binder_types
   513 fun maybe_curried_binder_types T =
   519 fun maybe_curried_binder_types T =
   514   (if is_pair_type (body_type T) then binder_types else curried_binder_types) T
   520   (if is_pair_type (body_type T) then binder_types else curried_binder_types) T
   515 
   521 
   516 (* typ -> term list -> term *)
   522 (* typ -> term list -> term *)
   517 fun mk_flat_tuple _ [t] = t
   523 fun mk_flat_tuple _ [t] = t
   518   | mk_flat_tuple (Type ("*", [T1, T2])) (t :: ts) =
   524   | mk_flat_tuple (Type (@{type_name "*"}, [T1, T2])) (t :: ts) =
   519     HOLogic.pair_const T1 T2 $ t $ (mk_flat_tuple T2 ts)
   525     HOLogic.pair_const T1 T2 $ t $ (mk_flat_tuple T2 ts)
   520   | mk_flat_tuple T ts = raise TYPE ("Nitpick_HOL.mk_flat_tuple", [T], ts)
   526   | mk_flat_tuple T ts = raise TYPE ("Nitpick_HOL.mk_flat_tuple", [T], ts)
   521 (* int -> term -> term list *)
   527 (* int -> term -> term list *)
   522 fun dest_n_tuple 1 t = [t]
   528 fun dest_n_tuple 1 t = [t]
   523   | dest_n_tuple n t = HOLogic.dest_prod t ||> dest_n_tuple (n - 1) |> op ::
   529   | dest_n_tuple n t = HOLogic.dest_prod t ||> dest_n_tuple (n - 1) |> op ::
   593     val {frac_types, codatatypes} = Data.get thy
   599     val {frac_types, codatatypes} = Data.get thy
   594     val constr_xs = map (apsnd (repair_constr_type thy co_T)) constr_xs
   600     val constr_xs = map (apsnd (repair_constr_type thy co_T)) constr_xs
   595     val (co_s, co_Ts) = dest_Type co_T
   601     val (co_s, co_Ts) = dest_Type co_T
   596     val _ =
   602     val _ =
   597       if forall is_TFree co_Ts andalso not (has_duplicates (op =) co_Ts) andalso
   603       if forall is_TFree co_Ts andalso not (has_duplicates (op =) co_Ts) andalso
   598          co_s <> "fun" andalso
   604          co_s <> @{type_name fun} andalso
   599          not (is_basic_datatype thy [(NONE, true)] co_s) then
   605          not (is_basic_datatype thy [(NONE, true)] co_s) then
   600         ()
   606         ()
   601       else
   607       else
   602         raise TYPE ("Nitpick_HOL.register_codatatype", [co_T], [])
   608         raise TYPE ("Nitpick_HOL.register_codatatype", [co_T], [])
   603     val codatatypes = AList.update (op =) (co_s, (case_name, constr_xs))
   609     val codatatypes = AList.update (op =) (co_s, (case_name, constr_xs))
   667 (* theory -> string -> typ -> int *)
   673 (* theory -> string -> typ -> int *)
   668 fun no_of_record_field thy s T1 =
   674 fun no_of_record_field thy s T1 =
   669   find_index (curry (op =) s o fst)
   675   find_index (curry (op =) s o fst)
   670              (Record.get_extT_fields thy T1 ||> single |> op @)
   676              (Record.get_extT_fields thy T1 ||> single |> op @)
   671 (* theory -> styp -> bool *)
   677 (* theory -> styp -> bool *)
   672 fun is_record_get thy (s, Type ("fun", [T1, _])) =
   678 fun is_record_get thy (s, Type (@{type_name fun}, [T1, _])) =
   673     exists (curry (op =) s o fst) (all_record_fields thy T1)
   679     exists (curry (op =) s o fst) (all_record_fields thy T1)
   674   | is_record_get _ _ = false
   680   | is_record_get _ _ = false
   675 fun is_record_update thy (s, T) =
   681 fun is_record_update thy (s, T) =
   676   String.isSuffix Record.updateN s andalso
   682   String.isSuffix Record.updateN s andalso
   677   exists (curry (op =) (unsuffix Record.updateN s) o fst)
   683   exists (curry (op =) (unsuffix Record.updateN s) o fst)
   678          (all_record_fields thy (body_type T))
   684          (all_record_fields thy (body_type T))
   679   handle TYPE _ => false
   685   handle TYPE _ => false
   680 fun is_abs_fun thy (s, Type ("fun", [_, Type (s', _)])) =
   686 fun is_abs_fun thy (s, Type (@{type_name fun}, [_, Type (s', _)])) =
   681     (case typedef_info thy s' of
   687     (case typedef_info thy s' of
   682        SOME {Abs_name, ...} => s = Abs_name
   688        SOME {Abs_name, ...} => s = Abs_name
   683      | NONE => false)
   689      | NONE => false)
   684   | is_abs_fun _ _ = false
   690   | is_abs_fun _ _ = false
   685 fun is_rep_fun thy (s, Type ("fun", [Type (s', _), _])) =
   691 fun is_rep_fun thy (s, Type (@{type_name fun}, [Type (s', _), _])) =
   686     (case typedef_info thy s' of
   692     (case typedef_info thy s' of
   687        SOME {Rep_name, ...} => s = Rep_name
   693        SOME {Rep_name, ...} => s = Rep_name
   688      | NONE => false)
   694      | NONE => false)
   689   | is_rep_fun _ _ = false
   695   | is_rep_fun _ _ = false
   690 (* Proof.context -> styp -> bool *)
   696 (* Proof.context -> styp -> bool *)
   691 fun is_quot_abs_fun ctxt (x as (_, Type ("fun", [_, Type (s', _)]))) =
   697 fun is_quot_abs_fun ctxt
       
   698                     (x as (_, Type (@{type_name fun}, [_, Type (s', _)]))) =
   692     (try (Quotient_Term.absrep_const_chk Quotient_Term.AbsF ctxt) s'
   699     (try (Quotient_Term.absrep_const_chk Quotient_Term.AbsF ctxt) s'
   693      = SOME (Const x))
   700      = SOME (Const x))
   694   | is_quot_abs_fun _ _ = false
   701   | is_quot_abs_fun _ _ = false
   695 fun is_quot_rep_fun ctxt (x as (_, Type ("fun", [Type (s', _), _]))) =
   702 fun is_quot_rep_fun ctxt
       
   703                     (x as (_, Type (@{type_name fun}, [Type (s', _), _]))) =
   696     (try (Quotient_Term.absrep_const_chk Quotient_Term.RepF ctxt) s'
   704     (try (Quotient_Term.absrep_const_chk Quotient_Term.RepF ctxt) s'
   697      = SOME (Const x))
   705      = SOME (Const x))
   698   | is_quot_rep_fun _ _ = false
   706   | is_quot_rep_fun _ _ = false
   699 
   707 
   700 (* theory -> styp -> styp *)
   708 (* theory -> styp -> styp *)
   701 fun mate_of_rep_fun thy (x as (_, Type ("fun", [T1 as Type (s', _), T2]))) =
   709 fun mate_of_rep_fun thy (x as (_, Type (@{type_name fun},
       
   710                                         [T1 as Type (s', _), T2]))) =
   702     (case typedef_info thy s' of
   711     (case typedef_info thy s' of
   703        SOME {Abs_name, ...} => (Abs_name, Type ("fun", [T2, T1]))
   712        SOME {Abs_name, ...} => (Abs_name, Type (@{type_name fun}, [T2, T1]))
   704      | NONE => raise TERM ("Nitpick_HOL.mate_of_rep_fun", [Const x]))
   713      | NONE => raise TERM ("Nitpick_HOL.mate_of_rep_fun", [Const x]))
   705   | mate_of_rep_fun _ x = raise TERM ("Nitpick_HOL.mate_of_rep_fun", [Const x])
   714   | mate_of_rep_fun _ x = raise TERM ("Nitpick_HOL.mate_of_rep_fun", [Const x])
   706 (* theory -> typ -> typ *)
   715 (* theory -> typ -> typ *)
   707 fun rep_type_for_quot_type thy (T as Type (s, _)) =
   716 fun rep_type_for_quot_type thy (T as Type (s, _)) =
   708   let val {qtyp, rtyp, ...} = Quotient_Info.quotdata_lookup thy s in
   717   let val {qtyp, rtyp, ...} = Quotient_Info.quotdata_lookup thy s in
   727     exists (fn (s', T') => s = s' andalso repair_constr_type thy co_T T' = T)
   736     exists (fn (s', T') => s = s' andalso repair_constr_type thy co_T T' = T)
   728            (AList.lookup (op =) codatatypes co_s |> Option.map snd |> these)
   737            (AList.lookup (op =) codatatypes co_s |> Option.map snd |> these)
   729   end
   738   end
   730   handle TYPE ("dest_Type", _, _) => false
   739   handle TYPE ("dest_Type", _, _) => false
   731 fun is_constr_like thy (s, T) =
   740 fun is_constr_like thy (s, T) =
   732   member (op =) [@{const_name FunBox}, @{const_name PairBox},
   741   member (op =) [@{const_name FinFun}, @{const_name FunBox},
   733                  @{const_name Quot}, @{const_name Zero_Rep},
   742                  @{const_name PairBox}, @{const_name Quot},
   734                  @{const_name Suc_Rep}] s orelse
   743                  @{const_name Zero_Rep}, @{const_name Suc_Rep}] s orelse
   735   let val (x as (_, T)) = (s, unarize_and_unbox_type T) in
   744   let val (x as (_, T)) = (s, unarize_unbox_etc_type T) in
   736     Refute.is_IDT_constructor thy x orelse is_record_constr x orelse
   745     Refute.is_IDT_constructor thy x orelse is_record_constr x orelse
   737     (is_abs_fun thy x andalso is_pure_typedef thy (range_type T)) orelse
   746     (is_abs_fun thy x andalso is_pure_typedef thy (range_type T)) orelse
   738     is_coconstr thy x
   747     is_coconstr thy x
   739   end
   748   end
   740 fun is_stale_constr thy (x as (_, T)) =
   749 fun is_stale_constr thy (x as (_, T)) =
   747                          (fst (dest_Type (unarize_type (body_type T))))) andalso
   756                          (fst (dest_Type (unarize_type (body_type T))))) andalso
   748   not (is_stale_constr thy x)
   757   not (is_stale_constr thy x)
   749 (* string -> bool *)
   758 (* string -> bool *)
   750 val is_sel = String.isPrefix discr_prefix orf String.isPrefix sel_prefix
   759 val is_sel = String.isPrefix discr_prefix orf String.isPrefix sel_prefix
   751 val is_sel_like_and_no_discr =
   760 val is_sel_like_and_no_discr =
   752   String.isPrefix sel_prefix
   761   String.isPrefix sel_prefix orf
   753   orf (member (op =) [@{const_name fst}, @{const_name snd}])
   762   (member (op =) [@{const_name fst}, @{const_name snd}])
   754 
   763 
   755 (* boxability -> boxability *)
   764 (* boxability -> boxability *)
   756 fun in_fun_lhs_for InConstr = InSel
   765 fun in_fun_lhs_for InConstr = InSel
   757   | in_fun_lhs_for _ = InFunLHS
   766   | in_fun_lhs_for _ = InFunLHS
   758 fun in_fun_rhs_for InConstr = InConstr
   767 fun in_fun_rhs_for InConstr = InConstr
   761   | in_fun_rhs_for _ = InFunRHS1
   770   | in_fun_rhs_for _ = InFunRHS1
   762 
   771 
   763 (* hol_context -> boxability -> typ -> bool *)
   772 (* hol_context -> boxability -> typ -> bool *)
   764 fun is_boxing_worth_it (hol_ctxt : hol_context) boxy T =
   773 fun is_boxing_worth_it (hol_ctxt : hol_context) boxy T =
   765   case T of
   774   case T of
   766     Type ("fun", _) =>
   775     Type (@{type_name fun}, _) =>
   767     (boxy = InPair orelse boxy = InFunLHS) andalso
   776     (boxy = InPair orelse boxy = InFunLHS) andalso
   768     not (is_boolean_type (body_type T))
   777     not (is_boolean_type (body_type T))
   769   | Type ("*", Ts) =>
   778   | Type (@{type_name "*"}, Ts) =>
   770     boxy = InPair orelse boxy = InFunRHS1 orelse boxy = InFunRHS2 orelse
   779     boxy = InPair orelse boxy = InFunRHS1 orelse boxy = InFunRHS2 orelse
   771     ((boxy = InExpr orelse boxy = InFunLHS) andalso
   780     ((boxy = InExpr orelse boxy = InFunLHS) andalso
   772      exists (is_boxing_worth_it hol_ctxt InPair)
   781      exists (is_boxing_worth_it hol_ctxt InPair)
   773             (map (box_type hol_ctxt InPair) Ts))
   782             (map (box_type hol_ctxt InPair) Ts))
   774   | _ => false
   783   | _ => false
   778     SOME (SOME box_me) => box_me
   787     SOME (SOME box_me) => box_me
   779   | _ => is_boxing_worth_it hol_ctxt boxy (Type z)
   788   | _ => is_boxing_worth_it hol_ctxt boxy (Type z)
   780 (* hol_context -> boxability -> typ -> typ *)
   789 (* hol_context -> boxability -> typ -> typ *)
   781 and box_type hol_ctxt boxy T =
   790 and box_type hol_ctxt boxy T =
   782   case T of
   791   case T of
   783     Type (z as ("fun", [T1, T2])) =>
   792     Type (z as (@{type_name fun}, [T1, T2])) =>
   784     if boxy <> InConstr andalso boxy <> InSel andalso
   793     if boxy <> InConstr andalso boxy <> InSel andalso
   785        should_box_type hol_ctxt boxy z then
   794        should_box_type hol_ctxt boxy z then
   786       Type (@{type_name fun_box},
   795       Type (@{type_name fun_box},
   787             [box_type hol_ctxt InFunLHS T1, box_type hol_ctxt InFunRHS1 T2])
   796             [box_type hol_ctxt InFunLHS T1, box_type hol_ctxt InFunRHS1 T2])
   788     else
   797     else
   789       box_type hol_ctxt (in_fun_lhs_for boxy) T1
   798       box_type hol_ctxt (in_fun_lhs_for boxy) T1
   790       --> box_type hol_ctxt (in_fun_rhs_for boxy) T2
   799       --> box_type hol_ctxt (in_fun_rhs_for boxy) T2
   791   | Type (z as ("*", Ts)) =>
   800   | Type (z as (@{type_name "*"}, Ts)) =>
   792     if boxy <> InConstr andalso boxy <> InSel
   801     if boxy <> InConstr andalso boxy <> InSel
   793        andalso should_box_type hol_ctxt boxy z then
   802        andalso should_box_type hol_ctxt boxy z then
   794       Type (@{type_name pair_box}, map (box_type hol_ctxt InSel) Ts)
   803       Type (@{type_name pair_box}, map (box_type hol_ctxt InSel) Ts)
   795     else
   804     else
   796       Type ("*", map (box_type hol_ctxt
   805       Type (@{type_name "*"},
       
   806             map (box_type hol_ctxt
   797                           (if boxy = InConstr orelse boxy = InSel then boxy
   807                           (if boxy = InConstr orelse boxy = InSel then boxy
   798                            else InPair)) Ts)
   808                            else InPair)) Ts)
   799   | _ => T
   809   | _ => T
   800 
   810 
   801 (* typ -> typ *)
   811 (* typ -> typ *)
   977     else if is_pair_type dataT then
   987     else if is_pair_type dataT then
   978       Const (nth_sel_for_constr x n)
   988       Const (nth_sel_for_constr x n)
   979     else
   989     else
   980       let
   990       let
   981         (* int -> typ -> int * term *)
   991         (* int -> typ -> int * term *)
   982         fun aux m (Type ("*", [T1, T2])) =
   992         fun aux m (Type (@{type_name "*"}, [T1, T2])) =
   983             let
   993             let
   984               val (m, t1) = aux m T1
   994               val (m, t1) = aux m T1
   985               val (m, t2) = aux m T2
   995               val (m, t2) = aux m T2
   986             in (m, HOLogic.mk_prod (t1, t2)) end
   996             in (m, HOLogic.mk_prod (t1, t2)) end
   987           | aux m T =
   997           | aux m T =
  1016         else
  1026         else
  1017           list_comb (Const x, args)
  1027           list_comb (Const x, args)
  1018       | _ => list_comb (Const x, args)
  1028       | _ => list_comb (Const x, args)
  1019     end
  1029     end
  1020 
  1030 
       
  1031 (* hol_context -> typ -> term -> term *)
       
  1032 fun constr_expand (hol_ctxt as {thy, stds, ...}) T t =
       
  1033   (case head_of t of
       
  1034      Const x => if is_constr_like thy x then t else raise SAME ()
       
  1035    | _ => raise SAME ())
       
  1036   handle SAME () =>
       
  1037          let
       
  1038            val x' as (_, T') =
       
  1039              if is_pair_type T then
       
  1040                let val (T1, T2) = HOLogic.dest_prodT T in
       
  1041                  (@{const_name Pair}, T1 --> T2 --> T)
       
  1042                end
       
  1043              else
       
  1044                datatype_constrs hol_ctxt T |> hd
       
  1045            val arg_Ts = binder_types T'
       
  1046          in
       
  1047            list_comb (Const x', map2 (select_nth_constr_arg thy stds x' t)
       
  1048                                      (index_seq 0 (length arg_Ts)) arg_Ts)
       
  1049          end
       
  1050 
       
  1051 (* (term -> term) -> int -> term -> term *)
       
  1052 fun coerce_bound_no f j t =
       
  1053   case t of
       
  1054     t1 $ t2 => coerce_bound_no f j t1 $ coerce_bound_no f j t2
       
  1055   | Abs (s, T, t') => Abs (s, T, coerce_bound_no f (j + 1) t')
       
  1056   | Bound j' => if j' = j then f t else t
       
  1057   | _ => t
       
  1058 (* hol_context -> typ -> typ -> term -> term *)
       
  1059 fun coerce_bound_0_in_term hol_ctxt new_T old_T =
       
  1060   old_T <> new_T ? coerce_bound_no (coerce_term hol_ctxt [new_T] old_T new_T) 0
       
  1061 (* hol_context -> typ list -> typ -> typ -> term -> term *)
       
  1062 and coerce_term (hol_ctxt as {thy, stds, fast_descrs, ...}) Ts new_T old_T t =
       
  1063   if old_T = new_T then
       
  1064     t
       
  1065   else
       
  1066     case (new_T, old_T) of
       
  1067       (Type (new_s, new_Ts as [new_T1, new_T2]),
       
  1068        Type (@{type_name fun}, [old_T1, old_T2])) =>
       
  1069       (case eta_expand Ts t 1 of
       
  1070          Abs (s, _, t') =>
       
  1071          Abs (s, new_T1,
       
  1072               t' |> coerce_bound_0_in_term hol_ctxt new_T1 old_T1
       
  1073                  |> coerce_term hol_ctxt (new_T1 :: Ts) new_T2 old_T2)
       
  1074          |> Envir.eta_contract
       
  1075          |> new_s <> @{type_name fun}
       
  1076             ? construct_value thy stds
       
  1077                   (if new_s = @{type_name fin_fun} then @{const_name FinFun}
       
  1078                    else @{const_name FunBox},
       
  1079                    Type (@{type_name fun}, new_Ts) --> new_T)
       
  1080               o single
       
  1081        | t' => raise TERM ("Nitpick_HOL.coerce_term", [t']))
       
  1082     | (Type (new_s, new_Ts as [new_T1, new_T2]),
       
  1083        Type (old_s, old_Ts as [old_T1, old_T2])) =>
       
  1084       if old_s = @{type_name fin_fun} orelse old_s = @{type_name fun_box} orelse
       
  1085          old_s = @{type_name pair_box} orelse old_s = @{type_name "*"} then
       
  1086         case constr_expand hol_ctxt old_T t of
       
  1087           Const (old_s, _) $ t1 =>
       
  1088           if new_s = @{type_name fun} then
       
  1089             coerce_term hol_ctxt Ts new_T (Type (@{type_name fun}, old_Ts)) t1
       
  1090           else
       
  1091             construct_value thy stds
       
  1092                 (old_s, Type (@{type_name fun}, new_Ts) --> new_T)
       
  1093                 [coerce_term hol_ctxt Ts (Type (@{type_name fun}, new_Ts))
       
  1094                              (Type (@{type_name fun}, old_Ts)) t1]
       
  1095         | Const _ $ t1 $ t2 =>
       
  1096           construct_value thy stds
       
  1097               (if new_s = @{type_name "*"} then @{const_name Pair}
       
  1098                else @{const_name PairBox}, new_Ts ---> new_T)
       
  1099               (map3 (coerce_term hol_ctxt Ts) [new_T1, new_T2] [old_T1, old_T2]
       
  1100                     [t1, t2])
       
  1101         | t' => raise TERM ("Nitpick_HOL.coerce_term", [t'])
       
  1102       else
       
  1103         raise TYPE ("Nitpick_HOL.coerce_term", [new_T, old_T], [t])
       
  1104     | _ => raise TYPE ("Nitpick_HOL.coerce_term", [new_T, old_T], [t])
       
  1105 
  1021 (* (typ * int) list -> typ -> int *)
  1106 (* (typ * int) list -> typ -> int *)
  1022 fun card_of_type assigns (Type ("fun", [T1, T2])) =
  1107 fun card_of_type assigns (Type (@{type_name fun}, [T1, T2])) =
  1023     reasonable_power (card_of_type assigns T2) (card_of_type assigns T1)
  1108     reasonable_power (card_of_type assigns T2) (card_of_type assigns T1)
  1024   | card_of_type assigns (Type ("*", [T1, T2])) =
  1109   | card_of_type assigns (Type (@{type_name "*"}, [T1, T2])) =
  1025     card_of_type assigns T1 * card_of_type assigns T2
  1110     card_of_type assigns T1 * card_of_type assigns T2
  1026   | card_of_type _ (Type (@{type_name itself}, _)) = 1
  1111   | card_of_type _ (Type (@{type_name itself}, _)) = 1
  1027   | card_of_type _ @{typ prop} = 2
  1112   | card_of_type _ @{typ prop} = 2
  1028   | card_of_type _ @{typ bool} = 2
  1113   | card_of_type _ @{typ bool} = 2
  1029   | card_of_type _ @{typ unit} = 1
  1114   | card_of_type _ @{typ unit} = 1
  1031     case AList.lookup (op =) assigns T of
  1116     case AList.lookup (op =) assigns T of
  1032       SOME k => k
  1117       SOME k => k
  1033     | NONE => if T = @{typ bisim_iterator} then 0
  1118     | NONE => if T = @{typ bisim_iterator} then 0
  1034               else raise TYPE ("Nitpick_HOL.card_of_type", [T], [])
  1119               else raise TYPE ("Nitpick_HOL.card_of_type", [T], [])
  1035 (* int -> (typ * int) list -> typ -> int *)
  1120 (* int -> (typ * int) list -> typ -> int *)
  1036 fun bounded_card_of_type max default_card assigns (Type ("fun", [T1, T2])) =
  1121 fun bounded_card_of_type max default_card assigns
       
  1122                          (Type (@{type_name fun}, [T1, T2])) =
  1037     let
  1123     let
  1038       val k1 = bounded_card_of_type max default_card assigns T1
  1124       val k1 = bounded_card_of_type max default_card assigns T1
  1039       val k2 = bounded_card_of_type max default_card assigns T2
  1125       val k2 = bounded_card_of_type max default_card assigns T2
  1040     in
  1126     in
  1041       if k1 = max orelse k2 = max then max
  1127       if k1 = max orelse k2 = max then max
  1042       else Int.min (max, reasonable_power k2 k1)
  1128       else Int.min (max, reasonable_power k2 k1)
  1043     end
  1129     end
  1044   | bounded_card_of_type max default_card assigns (Type ("*", [T1, T2])) =
  1130   | bounded_card_of_type max default_card assigns
       
  1131                          (Type (@{type_name "*"}, [T1, T2])) =
  1045     let
  1132     let
  1046       val k1 = bounded_card_of_type max default_card assigns T1
  1133       val k1 = bounded_card_of_type max default_card assigns T1
  1047       val k2 = bounded_card_of_type max default_card assigns T2
  1134       val k2 = bounded_card_of_type max default_card assigns T2
  1048     in if k1 = max orelse k2 = max then max else Int.min (max, k1 * k2) end
  1135     in if k1 = max orelse k2 = max then max else Int.min (max, k1 * k2) end
  1049   | bounded_card_of_type max default_card assigns T =
  1136   | bounded_card_of_type max default_card assigns T =
  1062       (if member (op =) avoid T then
  1149       (if member (op =) avoid T then
  1063          0
  1150          0
  1064        else if member (op =) finitizable_dataTs T then
  1151        else if member (op =) finitizable_dataTs T then
  1065          raise SAME ()
  1152          raise SAME ()
  1066        else case T of
  1153        else case T of
  1067          Type ("fun", [T1, T2]) =>
  1154          Type (@{type_name fun}, [T1, T2]) =>
  1068          let
  1155          let
  1069            val k1 = aux avoid T1
  1156            val k1 = aux avoid T1
  1070            val k2 = aux avoid T2
  1157            val k2 = aux avoid T2
  1071          in
  1158          in
  1072            if k1 = 0 orelse k2 = 0 then 0
  1159            if k1 = 0 orelse k2 = 0 then 0
  1073            else if k1 >= max orelse k2 >= max then max
  1160            else if k1 >= max orelse k2 >= max then max
  1074            else Int.min (max, reasonable_power k2 k1)
  1161            else Int.min (max, reasonable_power k2 k1)
  1075          end
  1162          end
  1076        | Type ("*", [T1, T2]) =>
  1163        | Type (@{type_name "*"}, [T1, T2]) =>
  1077          let
  1164          let
  1078            val k1 = aux avoid T1
  1165            val k1 = aux avoid T1
  1079            val k2 = aux avoid T2
  1166            val k2 = aux avoid T2
  1080          in
  1167          in
  1081            if k1 = 0 orelse k2 = 0 then 0
  1168            if k1 = 0 orelse k2 = 0 then 0
  1102        | _ => raise SAME ())
  1189        | _ => raise SAME ())
  1103       handle SAME () =>
  1190       handle SAME () =>
  1104              AList.lookup (op =) assigns T |> the_default default_card
  1191              AList.lookup (op =) assigns T |> the_default default_card
  1105   in Int.min (max, aux [] T) end
  1192   in Int.min (max, aux [] T) end
  1106 
  1193 
       
  1194 val small_type_max_card = 5
       
  1195 
  1107 (* hol_context -> typ -> bool *)
  1196 (* hol_context -> typ -> bool *)
  1108 fun is_finite_type hol_ctxt T =
  1197 fun is_finite_type hol_ctxt T =
  1109   bounded_exact_card_of_type hol_ctxt [] 1 2 [] T <> 0
  1198   bounded_exact_card_of_type hol_ctxt [] 1 2 [] T > 0
       
  1199 (* hol_context -> typ -> bool *)
       
  1200 fun is_small_finite_type hol_ctxt T =
       
  1201   let val n = bounded_exact_card_of_type hol_ctxt [] 1 2 [] T in
       
  1202     n > 0 andalso n <= small_type_max_card
       
  1203   end
  1110 
  1204 
  1111 (* term -> bool *)
  1205 (* term -> bool *)
  1112 fun is_ground_term (t1 $ t2) = is_ground_term t1 andalso is_ground_term t2
  1206 fun is_ground_term (t1 $ t2) = is_ground_term t1 andalso is_ground_term t2
  1113   | is_ground_term (Const _) = true
  1207   | is_ground_term (Const _) = true
  1114   | is_ground_term _ = false
  1208   | is_ground_term _ = false
  1142 (* theory -> bool -> term -> bool *)
  1236 (* theory -> bool -> term -> bool *)
  1143 fun is_typedef_axiom thy boring (@{const "==>"} $ _ $ t2) =
  1237 fun is_typedef_axiom thy boring (@{const "==>"} $ _ $ t2) =
  1144     is_typedef_axiom thy boring t2
  1238     is_typedef_axiom thy boring t2
  1145   | is_typedef_axiom thy boring
  1239   | is_typedef_axiom thy boring
  1146         (@{const Trueprop} $ (Const (@{const_name Typedef.type_definition}, _)
  1240         (@{const Trueprop} $ (Const (@{const_name Typedef.type_definition}, _)
  1147          $ Const (_, Type ("fun", [Type (s, _), _])) $ Const _ $ _)) =
  1241          $ Const (_, Type (@{type_name fun}, [Type (s, _), _]))
       
  1242          $ Const _ $ _)) =
  1148     boring <> is_funky_typedef_name thy s andalso is_typedef thy s
  1243     boring <> is_funky_typedef_name thy s andalso is_typedef thy s
  1149   | is_typedef_axiom _ _ _ = false
  1244   | is_typedef_axiom _ _ _ = false
  1150 
  1245 
  1151 (* Distinguishes between (1) constant definition axioms, (2) type arity and
  1246 (* Distinguishes between (1) constant definition axioms, (2) type arity and
  1152    typedef axioms, and (3) other axioms, and returns the pair ((1), (3)).
  1247    typedef axioms, and (3) other axioms, and returns the pair ((1), (3)).
  1536                                                      x)))
  1631                                                      x)))
  1537          [!simp_table, psimp_table]
  1632          [!simp_table, psimp_table]
  1538 fun is_inductive_pred hol_ctxt =
  1633 fun is_inductive_pred hol_ctxt =
  1539   is_real_inductive_pred hol_ctxt andf (not o is_real_equational_fun hol_ctxt)
  1634   is_real_inductive_pred hol_ctxt andf (not o is_real_equational_fun hol_ctxt)
  1540 fun is_equational_fun hol_ctxt =
  1635 fun is_equational_fun hol_ctxt =
  1541   (is_real_equational_fun hol_ctxt orf is_real_inductive_pred hol_ctxt
  1636   (is_real_equational_fun hol_ctxt orf is_real_inductive_pred hol_ctxt orf
  1542    orf (String.isPrefix ubfp_prefix orf String.isPrefix lbfp_prefix) o fst)
  1637    (String.isPrefix ubfp_prefix orf String.isPrefix lbfp_prefix) o fst)
  1543 
  1638 
  1544 (* term * term -> term *)
  1639 (* term * term -> term *)
  1545 fun s_betapply (Const (@{const_name If}, _) $ @{const True} $ t, _) = t
  1640 fun s_betapply (Const (@{const_name If}, _) $ @{const True} $ t, _) = t
  1546   | s_betapply (Const (@{const_name If}, _) $ @{const False} $ _, t) = t
  1641   | s_betapply (Const (@{const_name If}, _) $ @{const False} $ _, t) = t
  1547   | s_betapply p = betapply p
  1642   | s_betapply p = betapply p
  1584   let
  1679   let
  1585     (* int -> typ list -> term -> term *)
  1680     (* int -> typ list -> term -> term *)
  1586     fun do_term depth Ts t =
  1681     fun do_term depth Ts t =
  1587       case t of
  1682       case t of
  1588         (t0 as Const (@{const_name Int.number_class.number_of},
  1683         (t0 as Const (@{const_name Int.number_class.number_of},
  1589                       Type ("fun", [_, ran_T]))) $ t1 =>
  1684                       Type (@{type_name fun}, [_, ran_T]))) $ t1 =>
  1590         ((if is_number_type thy ran_T then
  1685         ((if is_number_type thy ran_T then
  1591             let
  1686             let
  1592               val j = t1 |> HOLogic.dest_numeral
  1687               val j = t1 |> HOLogic.dest_numeral
  1593                          |> ran_T = nat_T ? Integer.max 0
  1688                          |> ran_T = nat_T ? Integer.max 0
  1594               val s = numeral_prefix ^ signed_string_of_int j
  1689               val s = numeral_prefix ^ signed_string_of_int j
  1610         do_const depth Ts t (@{const_name refl'}, range_type T) [t2]
  1705         do_const depth Ts t (@{const_name refl'}, range_type T) [t2]
  1611       | (t0 as Const (@{const_name Sigma}, _)) $ t1 $ (t2 as Abs (_, _, t2')) =>
  1706       | (t0 as Const (@{const_name Sigma}, _)) $ t1 $ (t2 as Abs (_, _, t2')) =>
  1612         betapplys (t0 |> loose_bvar1 (t2', 0) ? do_term depth Ts,
  1707         betapplys (t0 |> loose_bvar1 (t2', 0) ? do_term depth Ts,
  1613                    map (do_term depth Ts) [t1, t2])
  1708                    map (do_term depth Ts) [t1, t2])
  1614       | Const (x as (@{const_name distinct},
  1709       | Const (x as (@{const_name distinct},
  1615                Type ("fun", [Type (@{type_name list}, [T']), _])))
  1710                Type (@{type_name fun}, [Type (@{type_name list}, [T']), _])))
  1616         $ (t1 as _ $ _) =>
  1711         $ (t1 as _ $ _) =>
  1617         (t1 |> HOLogic.dest_list |> distinctness_formula T'
  1712         (t1 |> HOLogic.dest_list |> distinctness_formula T'
  1618          handle TERM _ => do_const depth Ts t x [t1])
  1713          handle TERM _ => do_const depth Ts t x [t1])
  1619       | Const (x as (@{const_name If}, _)) $ t1 $ t2 $ t3 =>
  1714       | Const (x as (@{const_name If}, _)) $ t1 $ t2 $ t3 =>
  1620         if is_ground_term t1 andalso
  1715         if is_ground_term t1 andalso
  1967     val (outer_Ts, rest_T) = strip_n_binders (length outer) T
  2062     val (outer_Ts, rest_T) = strip_n_binders (length outer) T
  1968     val tuple_arg_Ts = strip_type rest_T |> fst
  2063     val tuple_arg_Ts = strip_type rest_T |> fst
  1969     val tuple_T = HOLogic.mk_tupleT tuple_arg_Ts
  2064     val tuple_T = HOLogic.mk_tupleT tuple_arg_Ts
  1970     val set_T = tuple_T --> bool_T
  2065     val set_T = tuple_T --> bool_T
  1971     val curried_T = tuple_T --> set_T
  2066     val curried_T = tuple_T --> set_T
  1972     val uncurried_T = Type ("*", [tuple_T, tuple_T]) --> bool_T
  2067     val uncurried_T = Type (@{type_name "*"}, [tuple_T, tuple_T]) --> bool_T
  1973     val (base_rhs, step_rhs) = linear_pred_base_and_step_rhss fp_app
  2068     val (base_rhs, step_rhs) = linear_pred_base_and_step_rhss fp_app
  1974     val base_x as (base_s, _) = (base_prefix ^ s, outer_Ts ---> set_T)
  2069     val base_x as (base_s, _) = (base_prefix ^ s, outer_Ts ---> set_T)
  1975     val base_eq = HOLogic.mk_eq (list_comb (Const base_x, outer_vars), base_rhs)
  2070     val base_eq = HOLogic.mk_eq (list_comb (Const base_x, outer_vars), base_rhs)
  1976                   |> HOLogic.mk_Trueprop
  2071                   |> HOLogic.mk_Trueprop
  1977     val _ = add_simps simp_table base_s [base_eq]
  2072     val _ = add_simps simp_table base_s [base_eq]
  2090 (* hol_context -> bool -> typ -> typ list -> typ list *)
  2185 (* hol_context -> bool -> typ -> typ list -> typ list *)
  2091 fun add_ground_types hol_ctxt binarize =
  2186 fun add_ground_types hol_ctxt binarize =
  2092   let
  2187   let
  2093     fun aux T accum =
  2188     fun aux T accum =
  2094       case T of
  2189       case T of
  2095         Type ("fun", Ts) => fold aux Ts accum
  2190         Type (@{type_name fun}, Ts) => fold aux Ts accum
  2096       | Type ("*", Ts) => fold aux Ts accum
  2191       | Type (@{type_name "*"}, Ts) => fold aux Ts accum
  2097       | Type (@{type_name itself}, [T1]) => aux T1 accum
  2192       | Type (@{type_name itself}, [T1]) => aux T1 accum
  2098       | Type (_, Ts) =>
  2193       | Type (_, Ts) =>
  2099         if member (op =) (@{typ prop} :: @{typ bool} :: @{typ unit} :: accum)
  2194         if member (op =) (@{typ prop} :: @{typ bool} :: @{typ unit} :: accum)
  2100                   T then
  2195                   T then
  2101           accum
  2196           accum
  2159             end
  2254             end
  2160 
  2255 
  2161 (* int list -> int list -> typ -> typ *)
  2256 (* int list -> int list -> typ -> typ *)
  2162 fun format_type default_format format T =
  2257 fun format_type default_format format T =
  2163   let
  2258   let
  2164     val T = unarize_unbox_and_uniterize_type T
  2259     val T = uniterize_unarize_unbox_etc_type T
  2165     val format = format |> filter (curry (op <) 0)
  2260     val format = format |> filter (curry (op <) 0)
  2166   in
  2261   in
  2167     if forall (curry (op =) 1) format then
  2262     if forall (curry (op =) 1) format then
  2168       T
  2263       T
  2169     else
  2264     else
  2198       (if String.isPrefix special_prefix s then
  2293       (if String.isPrefix special_prefix s then
  2199          let
  2294          let
  2200            (* term -> term *)
  2295            (* term -> term *)
  2201            val do_term = map_aterms (fn Const x => fst (do_const x) | t' => t')
  2296            val do_term = map_aterms (fn Const x => fst (do_const x) | t' => t')
  2202            val (x' as (_, T'), js, ts) =
  2297            val (x' as (_, T'), js, ts) =
  2203              AList.find (op =) (!special_funs) (s, unarize_and_unbox_type T)
  2298              AList.find (op =) (!special_funs) (s, unarize_unbox_etc_type T)
  2204              |> the_single
  2299              |> the_single
  2205            val max_j = List.last js
  2300            val max_j = List.last js
  2206            val Ts = List.take (binder_types T', max_j + 1)
  2301            val Ts = List.take (binder_types T', max_j + 1)
  2207            val missing_js = filter_out (member (op =) js) (0 upto max_j)
  2302            val missing_js = filter_out (member (op =) js) (0 upto max_j)
  2208            val missing_Ts = filter_indices missing_js Ts
  2303            val missing_Ts = filter_indices missing_js Ts
  2292                            (Const (@{const_name Eps}, (T --> bool_T) --> T))) T)
  2387                            (Const (@{const_name Eps}, (T --> bool_T) --> T))) T)
  2293        else
  2388        else
  2294          let val t = Const (original_name s, T) in
  2389          let val t = Const (original_name s, T) in
  2295            (t, format_term_type thy def_table formats t)
  2390            (t, format_term_type thy def_table formats t)
  2296          end)
  2391          end)
  2297       |>> map_types unarize_unbox_and_uniterize_type
  2392       |>> map_types uniterize_unarize_unbox_etc_type
  2298       |>> shorten_names_in_term |>> Term.map_abs_vars shortest_name
  2393       |>> shorten_names_in_term |>> Term.map_abs_vars shortest_name
  2299   in do_const end
  2394   in do_const end
  2300 
  2395 
  2301 (* styp -> string *)
  2396 (* styp -> string *)
  2302 fun assign_operator_for_const (s, T) =
  2397 fun assign_operator_for_const (s, T) =