src/HOL/Tools/ATP/atp_util.ML
changeset 54554 b8d0d8407c3b
parent 53800 ac1ec5065316
child 54757 4960647932ec
equal deleted inserted replaced
54553:2b0da4c1dd40 54554:b8d0d8407c3b
    27   val type_intersect : theory -> typ -> typ -> bool
    27   val type_intersect : theory -> typ -> typ -> bool
    28   val type_equiv : theory -> typ * typ -> bool
    28   val type_equiv : theory -> typ * typ -> bool
    29   val varify_type : Proof.context -> typ -> typ
    29   val varify_type : Proof.context -> typ -> typ
    30   val instantiate_type : theory -> typ -> typ -> typ -> typ
    30   val instantiate_type : theory -> typ -> typ -> typ -> typ
    31   val varify_and_instantiate_type : Proof.context -> typ -> typ -> typ -> typ
    31   val varify_and_instantiate_type : Proof.context -> typ -> typ -> typ -> typ
    32   val typ_of_dtyp : Datatype.descr -> (Datatype.dtyp * typ) list -> Datatype.dtyp -> typ
       
    33   val is_type_surely_finite : Proof.context -> typ -> bool
    32   val is_type_surely_finite : Proof.context -> typ -> bool
    34   val is_type_surely_infinite : Proof.context -> bool -> typ list -> typ -> bool
    33   val is_type_surely_infinite : Proof.context -> bool -> typ list -> typ -> bool
    35   val s_not : term -> term
    34   val s_not : term -> term
    36   val s_conj : term * term -> term
    35   val s_conj : term * term -> term
    37   val s_disj : term * term -> term
    36   val s_disj : term * term -> term
   189 fun varify_and_instantiate_type ctxt T1 T1' T2 =
   188 fun varify_and_instantiate_type ctxt T1 T1' T2 =
   190   let val thy = Proof_Context.theory_of ctxt in
   189   let val thy = Proof_Context.theory_of ctxt in
   191     instantiate_type thy (varify_type ctxt T1) T1' (varify_type ctxt T2)
   190     instantiate_type thy (varify_type ctxt T1) T1' (varify_type ctxt T2)
   192   end
   191   end
   193 
   192 
   194 fun typ_of_dtyp _ typ_assoc (Datatype.DtTFree a) =
   193 fun free_constructors_of ctxt (Type (s, Ts)) =
   195     the (AList.lookup (op =) typ_assoc (Datatype.DtTFree a))
   194     (case Ctr_Sugar.ctr_sugar_of ctxt s of
   196   | typ_of_dtyp descr typ_assoc (Datatype.DtType (s, Us)) =
   195       SOME {ctrs, ...} => map_filter (try dest_Const o Ctr_Sugar.mk_ctr Ts) ctrs
   197     Type (s, map (typ_of_dtyp descr typ_assoc) Us)
   196     | NONE => [])
   198   | typ_of_dtyp descr typ_assoc (Datatype.DtRec i) =
   197   | free_constructors_of _ _ = []
   199     let val (s, ds, _) = the (AList.lookup (op =) descr i) in
       
   200       Type (s, map (typ_of_dtyp descr typ_assoc) ds)
       
   201     end
       
   202 
       
   203 fun datatype_constrs thy (T as Type (s, Ts)) =
       
   204     (case Datatype.get_info thy s of
       
   205        SOME {index, descr, ...} =>
       
   206        let val (_, dtyps, constrs) = AList.lookup (op =) descr index |> the in
       
   207          map (apsnd (fn Us => map (typ_of_dtyp descr (dtyps ~~ Ts)) Us ---> T))
       
   208              constrs
       
   209        end
       
   210      | NONE => [])
       
   211   | datatype_constrs _ _ = []
       
   212 
   198 
   213 (* Similar to "Nitpick_HOL.bounded_exact_card_of_type".
   199 (* Similar to "Nitpick_HOL.bounded_exact_card_of_type".
   214    0 means infinite type, 1 means singleton type (e.g., "unit"), and 2 means
   200    0 means infinite type, 1 means singleton type (e.g., "unit"), and 2 means
   215    cardinality 2 or more. The specified default cardinality is returned if the
   201    cardinality 2 or more. The specified default cardinality is returned if the
   216    cardinality of the type can't be determined. *)
   202    cardinality of the type can't be determined. *)
   237         | @{typ prop} => 2
   223         | @{typ prop} => 2
   238         | @{typ bool} => 2 (* optimization *)
   224         | @{typ bool} => 2 (* optimization *)
   239         | @{typ nat} => 0 (* optimization *)
   225         | @{typ nat} => 0 (* optimization *)
   240         | Type ("Int.int", []) => 0 (* optimization *)
   226         | Type ("Int.int", []) => 0 (* optimization *)
   241         | Type (s, _) =>
   227         | Type (s, _) =>
   242           (case datatype_constrs thy T of
   228           (case free_constructors_of ctxt T of
   243              constrs as _ :: _ =>
   229              constrs as _ :: _ =>
   244              let
   230              let
   245                val constr_cards =
   231                val constr_cards =
   246                  map (Integer.prod o map (aux slack (T :: avoid)) o binder_types
   232                  map (Integer.prod o map (aux slack (T :: avoid)) o binder_types o snd) constrs
   247                       o snd) constrs
       
   248              in
   233              in
   249                if exists (curry (op =) 0) constr_cards then 0
   234                if exists (curry (op =) 0) constr_cards then 0
   250                else Int.min (max, Integer.sum constr_cards)
   235                else Int.min (max, Integer.sum constr_cards)
   251              end
   236              end
   252            | [] =>
   237            | [] =>
   268                   | 1 => 1
   253                   | 1 => 1
   269                   | _ => default_card)
   254                   | _ => default_card)
   270                else
   255                else
   271                  default_card
   256                  default_card
   272              | [] => default_card)
   257              | [] => default_card)
       
   258         | TFree _ =>
   273           (* Very slightly unsound: Type variables are assumed not to be
   259           (* Very slightly unsound: Type variables are assumed not to be
   274              constrained to cardinality 1. (In practice, the user would most
   260              constrained to cardinality 1. (In practice, the user would most
   275              likely have used "unit" directly anyway.) *)
   261              likely have used "unit" directly anyway.) *)
   276         | TFree _ =>
       
   277           if not sound andalso default_card = 1 then 2 else default_card
   262           if not sound andalso default_card = 1 then 2 else default_card
   278         | TVar _ => default_card
   263         | TVar _ => default_card
   279   in Int.min (max, aux false [] T) end
   264   in Int.min (max, aux false [] T) end
   280 
   265 
   281 fun is_type_surely_finite ctxt T = tiny_card_of_type ctxt true [] 0 T <> 0
   266 fun is_type_surely_finite ctxt T = tiny_card_of_type ctxt true [] 0 T <> 0