src/HOL/Tools/Nitpick/nitpick_hol.ML
changeset 35284 9edc2bd6d2bd
parent 35283 7ae51d5ea05d
child 35309 997aa3a3e4bb
equal deleted inserted replaced
35283:7ae51d5ea05d 35284:9edc2bd6d2bd
   595   in Data.put {frac_types = frac_types, codatatypes = codatatypes} thy end
   595   in Data.put {frac_types = frac_types, codatatypes = codatatypes} thy end
   596 (* typ -> theory -> theory *)
   596 (* typ -> theory -> theory *)
   597 fun unregister_codatatype co_T = register_codatatype co_T "" []
   597 fun unregister_codatatype co_T = register_codatatype co_T "" []
   598 
   598 
   599 (* theory -> typ -> bool *)
   599 (* theory -> typ -> bool *)
   600 fun is_quot_type _ (Type ("IntEx.my_int", _)) = true (* FIXME *)
   600 fun is_quot_type thy (Type (s, _)) =
   601   | is_quot_type _ (Type ("FSet.fset", _)) = true
   601     is_some (Quotient_Info.quotdata_lookup_raw thy s)
   602   | is_quot_type _ _ = false
   602   | is_quot_type _ _ = false
   603 fun is_codatatype thy (Type (s, _)) =
   603 fun is_codatatype thy (Type (s, _)) =
   604     not (null (AList.lookup (op =) (#codatatypes (Data.get thy)) s
   604     not (null (AList.lookup (op =) (#codatatypes (Data.get thy)) s
   605                |> Option.map snd |> these))
   605                |> Option.map snd |> these))
   606   | is_codatatype _ _ = false
   606   | is_codatatype _ _ = false
   664     (case typedef_info thy s' of
   664     (case typedef_info thy s' of
   665        SOME {Rep_name, ...} => s = Rep_name
   665        SOME {Rep_name, ...} => s = Rep_name
   666      | NONE => false)
   666      | NONE => false)
   667   | is_rep_fun _ _ = false
   667   | is_rep_fun _ _ = false
   668 (* Proof.context -> styp -> bool *)
   668 (* Proof.context -> styp -> bool *)
   669 fun is_quot_abs_fun _ ("IntEx.abs_my_int", _) = true
   669 fun is_quot_abs_fun ctxt (x as (_, Type ("fun", [_, Type (s', _)]))) =
   670   | is_quot_abs_fun _ ("FSet.abs_fset", _) = true
   670     (try (Quotient_Term.absrep_const_chk Quotient_Term.AbsF ctxt) s'
       
   671      = SOME (Const x))
   671   | is_quot_abs_fun _ _ = false
   672   | is_quot_abs_fun _ _ = false
   672 fun is_quot_rep_fun _ ("IntEx.rep_my_int", _) = true
   673 fun is_quot_rep_fun ctxt (x as (_, Type ("fun", [Type (s', _), _]))) =
   673   | is_quot_rep_fun _ ("FSet.rep_fset", _) = true
   674     (try (Quotient_Term.absrep_const_chk Quotient_Term.RepF ctxt) s'
       
   675      = SOME (Const x))
   674   | is_quot_rep_fun _ _ = false
   676   | is_quot_rep_fun _ _ = false
   675 
   677 
   676 (* theory -> styp -> styp *)
   678 (* theory -> styp -> styp *)
   677 fun mate_of_rep_fun thy (x as (_, Type ("fun", [T1 as Type (s', _), T2]))) =
   679 fun mate_of_rep_fun thy (x as (_, Type ("fun", [T1 as Type (s', _), T2]))) =
   678     (case typedef_info thy s' of
   680     (case typedef_info thy s' of
   679        SOME {Abs_name, ...} => (Abs_name, Type ("fun", [T2, T1]))
   681        SOME {Abs_name, ...} => (Abs_name, Type ("fun", [T2, T1]))
   680      | NONE => raise TERM ("Nitpick_HOL.mate_of_rep_fun", [Const x]))
   682      | NONE => raise TERM ("Nitpick_HOL.mate_of_rep_fun", [Const x]))
   681   | mate_of_rep_fun _ x = raise TERM ("Nitpick_HOL.mate_of_rep_fun", [Const x])
   683   | mate_of_rep_fun _ x = raise TERM ("Nitpick_HOL.mate_of_rep_fun", [Const x])
   682 (* theory -> typ -> typ *)
   684 (* theory -> typ -> typ *)
   683 fun rep_type_for_quot_type _ (Type ("IntEx.my_int", [])) = @{typ "nat * nat"}
   685 fun rep_type_for_quot_type thy (T as Type (s, _)) =
   684   | rep_type_for_quot_type _ (Type ("FSet.fset", [T])) =
   686   let val {qtyp, rtyp, ...} = Quotient_Info.quotdata_lookup thy s in
   685     Type (@{type_name list}, [T])
   687     instantiate_type thy qtyp T rtyp
   686   | rep_type_for_quot_type _ T =
   688   end
   687     raise TYPE ("Nitpick_HOL.rep_type_for_quot_type", [T], [])
       
   688 (* theory -> typ -> term *)
   689 (* theory -> typ -> term *)
   689 fun equiv_relation_for_quot_type _ (Type ("IntEx.my_int", [])) =
   690 fun equiv_relation_for_quot_type thy (Type (s, Ts)) =
   690     Const ("IntEx.intrel", @{typ "(nat * nat) => (nat * nat) => bool"})
   691     let
   691   | equiv_relation_for_quot_type _ (Type ("FSet.fset", [T])) =
   692       val {qtyp, equiv_rel, ...} = Quotient_Info.quotdata_lookup thy s
   692     Const ("FSet.list_eq",
   693       val Ts' = qtyp |> dest_Type |> snd
   693            Type (@{type_name list}, [T]) --> Type (@{type_name list}, [T])
   694     in subst_atomic_types (Ts' ~~ Ts) equiv_rel end
   694            --> bool_T)
       
   695   | equiv_relation_for_quot_type _ T =
   695   | equiv_relation_for_quot_type _ T =
   696     raise TYPE ("Nitpick_HOL.equiv_relation_for_quot_type", [T], [])
   696     raise TYPE ("Nitpick_HOL.equiv_relation_for_quot_type", [T], [])
   697 
   697 
   698 (* theory -> styp -> bool *)
   698 (* theory -> styp -> bool *)
   699 fun is_coconstr thy (s, T) =
   699 fun is_coconstr thy (s, T) =
  1545 
  1545 
  1546 (* Prevents divergence in case of cyclic or infinite definition dependencies. *)
  1546 (* Prevents divergence in case of cyclic or infinite definition dependencies. *)
  1547 val unfold_max_depth = 255
  1547 val unfold_max_depth = 255
  1548 
  1548 
  1549 (* hol_context -> term -> term *)
  1549 (* hol_context -> term -> term *)
  1550 fun unfold_defs_in_term (hol_ctxt as {thy, stds, fast_descrs, case_names,
  1550 fun unfold_defs_in_term (hol_ctxt as {thy, ctxt, stds, fast_descrs, case_names,
  1551                                       def_table, ground_thm_table, ersatz_table,
  1551                                       def_table, ground_thm_table, ersatz_table,
  1552                                       ...}) =
  1552                                       ...}) =
  1553   let
  1553   let
  1554     (* int -> typ list -> term -> term *)
  1554     (* int -> typ list -> term -> term *)
  1555     fun do_term depth Ts t =
  1555     fun do_term depth Ts t =
  1630               if is_constr thy stds x then
  1630               if is_constr thy stds x then
  1631                 (Const x, ts)
  1631                 (Const x, ts)
  1632               else if is_stale_constr thy x then
  1632               else if is_stale_constr thy x then
  1633                 raise NOT_SUPPORTED ("(non-co)constructors of codatatypes \
  1633                 raise NOT_SUPPORTED ("(non-co)constructors of codatatypes \
  1634                                      \(\"" ^ s ^ "\")")
  1634                                      \(\"" ^ s ^ "\")")
  1635               else if is_quot_abs_fun thy x then
  1635               else if is_quot_abs_fun ctxt x then
  1636                 let
  1636                 let
  1637                   val rep_T = domain_type T
  1637                   val rep_T = domain_type T
  1638                   val abs_T = range_type T
  1638                   val abs_T = range_type T
  1639                 in
  1639                 in
  1640                   (Abs (Name.uu, rep_T,
  1640                   (Abs (Name.uu, rep_T,
  1641                         Const (@{const_name Quot}, rep_T --> abs_T)
  1641                         Const (@{const_name Quot}, rep_T --> abs_T)
  1642                                $ (Const (@{const_name quot_normal},
  1642                                $ (Const (@{const_name quot_normal},
  1643                                          rep_T --> rep_T) $ Bound 0)), ts)
  1643                                          rep_T --> rep_T) $ Bound 0)), ts)
  1644                 end
  1644                 end
  1645               else if is_quot_rep_fun thy x then
  1645               else if is_quot_rep_fun ctxt x then
  1646                 let
  1646                 let
  1647                   val abs_T = domain_type T
  1647                   val abs_T = domain_type T
  1648                   val rep_T = range_type T
  1648                   val rep_T = range_type T
  1649                   val x' = (@{const_name Quot}, rep_T --> abs_T)
  1649                   val x' = (@{const_name Quot}, rep_T --> abs_T)
  1650                 in select_nth_constr_arg_with_args depth Ts x' ts 0 rep_T end
  1650                 in select_nth_constr_arg_with_args depth Ts x' ts 0 rep_T end