src/HOL/Tools/Nitpick/nitpick_hol.ML
changeset 38215 1c7d7eaebdf2
parent 38212 a7e92239922f
child 38240 a44d108a8d39
equal deleted inserted replaced
38214:8164c91039ea 38215:1c7d7eaebdf2
   111   val curried_binder_types : typ -> typ list
   111   val curried_binder_types : typ -> typ list
   112   val mk_flat_tuple : typ -> term list -> term
   112   val mk_flat_tuple : typ -> term list -> term
   113   val dest_n_tuple : int -> term -> term list
   113   val dest_n_tuple : int -> term -> term list
   114   val is_real_datatype : theory -> string -> bool
   114   val is_real_datatype : theory -> string -> bool
   115   val is_standard_datatype : theory -> (typ option * bool) list -> typ -> bool
   115   val is_standard_datatype : theory -> (typ option * bool) list -> typ -> bool
       
   116   val is_codatatype : theory -> typ -> bool
   116   val is_quot_type : theory -> typ -> bool
   117   val is_quot_type : theory -> typ -> bool
   117   val is_codatatype : theory -> typ -> bool
       
   118   val is_pure_typedef : Proof.context -> typ -> bool
   118   val is_pure_typedef : Proof.context -> typ -> bool
   119   val is_univ_typedef : Proof.context -> typ -> bool
   119   val is_univ_typedef : Proof.context -> typ -> bool
   120   val is_datatype : Proof.context -> (typ option * bool) list -> typ -> bool
   120   val is_datatype : Proof.context -> (typ option * bool) list -> typ -> bool
   121   val is_record_constr : styp -> bool
   121   val is_record_constr : styp -> bool
   122   val is_record_get : theory -> styp -> bool
   122   val is_record_get : theory -> styp -> bool
   638     val codatatypes = AList.update (op =) (co_s, (case_name, constr_xs))
   638     val codatatypes = AList.update (op =) (co_s, (case_name, constr_xs))
   639                                    codatatypes
   639                                    codatatypes
   640   in Data.put {frac_types = frac_types, codatatypes = codatatypes} thy end
   640   in Data.put {frac_types = frac_types, codatatypes = codatatypes} thy end
   641 fun unregister_codatatype co_T = register_codatatype co_T "" []
   641 fun unregister_codatatype co_T = register_codatatype co_T "" []
   642 
   642 
   643 fun is_quot_type thy (Type (s, _)) =
       
   644     is_some (Quotient_Info.quotdata_lookup_raw thy s)
       
   645   | is_quot_type _ _ = false
       
   646 fun is_codatatype thy (Type (s, _)) =
   643 fun is_codatatype thy (Type (s, _)) =
   647     not (null (AList.lookup (op =) (#codatatypes (Data.get thy)) s
   644     not (null (AList.lookup (op =) (#codatatypes (Data.get thy)) s
   648                |> Option.map snd |> these))
   645                |> Option.map snd |> these))
   649   | is_codatatype _ _ = false
   646   | is_codatatype _ _ = false
       
   647 fun is_real_quot_type thy (Type (s, _)) =
       
   648     is_some (Quotient_Info.quotdata_lookup_raw thy s)
       
   649   | is_real_quot_type _ _ = false
       
   650 fun is_quot_type thy T =
       
   651   is_real_quot_type thy T andalso not (is_codatatype thy T)
   650 fun is_pure_typedef ctxt (T as Type (s, _)) =
   652 fun is_pure_typedef ctxt (T as Type (s, _)) =
   651     let val thy = ProofContext.theory_of ctxt in
   653     let val thy = ProofContext.theory_of ctxt in
   652       is_typedef ctxt s andalso
   654       is_typedef ctxt s andalso
   653       not (is_real_datatype thy s orelse is_quot_type thy T orelse
   655       not (is_real_datatype thy s orelse is_real_quot_type thy T orelse
   654            is_codatatype thy T orelse is_record_type T orelse
   656            is_codatatype thy T orelse is_record_type T orelse
   655            is_integer_like_type T)
   657            is_integer_like_type T)
   656     end
   658     end
   657   | is_pure_typedef _ _ = false
   659   | is_pure_typedef _ _ = false
   658 fun is_univ_typedef ctxt (Type (s, _)) =
   660 fun is_univ_typedef ctxt (Type (s, _)) =
   679      | NONE => false)
   681      | NONE => false)
   680   | is_univ_typedef _ _ = false
   682   | is_univ_typedef _ _ = false
   681 fun is_datatype ctxt stds (T as Type (s, _)) =
   683 fun is_datatype ctxt stds (T as Type (s, _)) =
   682     let val thy = ProofContext.theory_of ctxt in
   684     let val thy = ProofContext.theory_of ctxt in
   683       (is_typedef ctxt s orelse is_codatatype thy T orelse T = @{typ ind} orelse
   685       (is_typedef ctxt s orelse is_codatatype thy T orelse T = @{typ ind} orelse
   684        is_quot_type thy T) andalso not (is_basic_datatype thy stds s)
   686        is_real_quot_type thy T) andalso not (is_basic_datatype thy stds s)
   685     end
   687     end
   686   | is_datatype _ _ _ = false
   688   | is_datatype _ _ _ = false
   687 
   689 
   688 fun all_record_fields thy T =
   690 fun all_record_fields thy T =
   689   let val (recs, more) = Record.get_extT_fields thy T in
   691   let val (recs, more) = Record.get_extT_fields thy T in
   716 fun is_rep_fun ctxt (s, Type (@{type_name fun}, [Type (s', _), _])) =
   718 fun is_rep_fun ctxt (s, Type (@{type_name fun}, [Type (s', _), _])) =
   717     (case typedef_info ctxt s' of
   719     (case typedef_info ctxt s' of
   718        SOME {Rep_name, ...} => s = Rep_name
   720        SOME {Rep_name, ...} => s = Rep_name
   719      | NONE => false)
   721      | NONE => false)
   720   | is_rep_fun _ _ = false
   722   | is_rep_fun _ _ = false
   721 fun is_quot_abs_fun ctxt
   723 fun is_quot_abs_fun ctxt (x as (_, Type (@{type_name fun},
   722                     (x as (_, Type (@{type_name fun}, [_, Type (s', _)]))) =
   724                                          [_, abs_T as Type (s', _)]))) =
   723     (try (Quotient_Term.absrep_const_chk Quotient_Term.AbsF ctxt) s'
   725     let val thy = ProofContext.theory_of ctxt in
   724      = SOME (Const x))
   726       (try (Quotient_Term.absrep_const_chk Quotient_Term.AbsF ctxt) s'
       
   727        = SOME (Const x)) andalso not (is_codatatype thy abs_T)
       
   728     end
   725   | is_quot_abs_fun _ _ = false
   729   | is_quot_abs_fun _ _ = false
   726 fun is_quot_rep_fun ctxt
   730 fun is_quot_rep_fun ctxt (x as (_, Type (@{type_name fun},
   727                     (x as (_, Type (@{type_name fun}, [Type (s', _), _]))) =
   731                                          [abs_T as Type (s', _), _]))) =
   728     (try (Quotient_Term.absrep_const_chk Quotient_Term.RepF ctxt) s'
   732     let val thy = ProofContext.theory_of ctxt in
   729      = SOME (Const x))
   733       (try (Quotient_Term.absrep_const_chk Quotient_Term.RepF ctxt) s'
       
   734        = SOME (Const x)) andalso not (is_codatatype thy abs_T)
       
   735     end
   730   | is_quot_rep_fun _ _ = false
   736   | is_quot_rep_fun _ _ = false
   731 
   737 
   732 fun mate_of_rep_fun ctxt (x as (_, Type (@{type_name fun},
   738 fun mate_of_rep_fun ctxt (x as (_, Type (@{type_name fun},
   733                                          [T1 as Type (s', _), T2]))) =
   739                                          [T1 as Type (s', _), T2]))) =
   734     (case typedef_info ctxt s' of
   740     (case typedef_info ctxt s' of
   911              let
   917              let
   912                val s' = unsuffix Record.ext_typeN s ^ Record.extN
   918                val s' = unsuffix Record.ext_typeN s ^ Record.extN
   913                val T' = (Record.get_extT_fields thy T
   919                val T' = (Record.get_extT_fields thy T
   914                         |> apsnd single |> uncurry append |> map snd) ---> T
   920                         |> apsnd single |> uncurry append |> map snd) ---> T
   915              in [(s', T')] end
   921              in [(s', T')] end
   916            else if is_quot_type thy T then
   922            else if is_real_quot_type thy T then
   917              [(@{const_name Quot}, rep_type_for_quot_type thy T --> T)]
   923              [(@{const_name Quot}, rep_type_for_quot_type thy T --> T)]
   918            else case typedef_info ctxt s of
   924            else case typedef_info ctxt s of
   919              SOME {abs_type, rep_type, Abs_name, ...} =>
   925              SOME {abs_type, rep_type, Abs_name, ...} =>
   920              [(Abs_name,
   926              [(Abs_name,
   921                varify_and_instantiate_type thy abs_type T rep_type --> T)]
   927                varify_and_instantiate_type thy abs_type T rep_type --> T)]