src/HOL/Tools/Nitpick/nitpick_rep.ML
changeset 69593 3dda49e08b9d
parent 59058 a78612c67ec0
equal deleted inserted replaced
69592:a80d8ec6c998 69593:3dda49e08b9d
   146 
   146 
   147 fun lazy_range_rep _ _ _ (Vect (_, R)) = R
   147 fun lazy_range_rep _ _ _ (Vect (_, R)) = R
   148   | lazy_range_rep _ _ _ (Func (_, R2)) = R2
   148   | lazy_range_rep _ _ _ (Func (_, R2)) = R2
   149   | lazy_range_rep ofs T ran_card (Opt R) =
   149   | lazy_range_rep ofs T ran_card (Opt R) =
   150     Opt (lazy_range_rep ofs T ran_card R)
   150     Opt (lazy_range_rep ofs T ran_card R)
   151   | lazy_range_rep ofs (Type (@{type_name fun}, [_, T2])) _ (Atom (1, _)) =
   151   | lazy_range_rep ofs (Type (\<^type_name>\<open>fun\<close>, [_, T2])) _ (Atom (1, _)) =
   152     Atom (1, offset_of_type ofs T2)
   152     Atom (1, offset_of_type ofs T2)
   153   | lazy_range_rep ofs (Type (@{type_name fun}, [_, T2])) ran_card (Atom _) =
   153   | lazy_range_rep ofs (Type (\<^type_name>\<open>fun\<close>, [_, T2])) ran_card (Atom _) =
   154     Atom (ran_card (), offset_of_type ofs T2)
   154     Atom (ran_card (), offset_of_type ofs T2)
   155   | lazy_range_rep _ _ _ R = raise REP ("Nitpick_Rep.lazy_range_rep", [R])
   155   | lazy_range_rep _ _ _ R = raise REP ("Nitpick_Rep.lazy_range_rep", [R])
   156 
   156 
   157 fun binder_reps (Func (R1, R2)) = R1 :: binder_reps R2
   157 fun binder_reps (Func (R1, R2)) = R1 :: binder_reps R2
   158   | binder_reps _ = []
   158   | binder_reps _ = []
   169   | one_rep _ _ (Struct Rs) = Struct Rs
   169   | one_rep _ _ (Struct Rs) = Struct Rs
   170   | one_rep _ _ (Vect z) = Vect z
   170   | one_rep _ _ (Vect z) = Vect z
   171   | one_rep ofs T (Opt R) = one_rep ofs T R
   171   | one_rep ofs T (Opt R) = one_rep ofs T R
   172   | one_rep ofs T R = Atom (card_of_rep R, offset_of_type ofs T)
   172   | one_rep ofs T R = Atom (card_of_rep R, offset_of_type ofs T)
   173 
   173 
   174 fun optable_rep ofs (Type (@{type_name fun}, [_, T2])) (Func (R1, R2)) =
   174 fun optable_rep ofs (Type (\<^type_name>\<open>fun\<close>, [_, T2])) (Func (R1, R2)) =
   175     Func (R1, optable_rep ofs T2 R2)
   175     Func (R1, optable_rep ofs T2 R2)
   176   | optable_rep ofs (Type (@{type_name set}, [T'])) R =
   176   | optable_rep ofs (Type (\<^type_name>\<open>set\<close>, [T'])) R =
   177     optable_rep ofs (T' --> bool_T) R
   177     optable_rep ofs (T' --> bool_T) R
   178   | optable_rep ofs T R = one_rep ofs T R
   178   | optable_rep ofs T R = one_rep ofs T R
   179 
   179 
   180 fun opt_rep ofs (Type (@{type_name fun}, [_, T2])) (Func (R1, R2)) =
   180 fun opt_rep ofs (Type (\<^type_name>\<open>fun\<close>, [_, T2])) (Func (R1, R2)) =
   181     Func (R1, opt_rep ofs T2 R2)
   181     Func (R1, opt_rep ofs T2 R2)
   182   | opt_rep ofs (Type (@{type_name set}, [T'])) R =
   182   | opt_rep ofs (Type (\<^type_name>\<open>set\<close>, [T'])) R =
   183     opt_rep ofs (T' --> bool_T) R
   183     opt_rep ofs (T' --> bool_T) R
   184   | opt_rep ofs T R = Opt (optable_rep ofs T R)
   184   | opt_rep ofs T R = Opt (optable_rep ofs T R)
   185 
   185 
   186 fun unopt_rep (Func (R1, R2)) = Func (R1, unopt_rep R2)
   186 fun unopt_rep (Func (R1, R2)) = Func (R1, unopt_rep R2)
   187   | unopt_rep (Opt R) = R
   187   | unopt_rep (Opt R) = R
   247     val j0 =
   247     val j0 =
   248       offset_of_type ofs (fst (HOLogic.dest_prodT (pseudo_domain_type T)))
   248       offset_of_type ofs (fst (HOLogic.dest_prodT (pseudo_domain_type T)))
   249   in Func (Struct [Atom (k, j0), Atom (k, j0)], Formula Neut) end
   249   in Func (Struct [Atom (k, j0), Atom (k, j0)], Formula Neut) end
   250 
   250 
   251 fun best_one_rep_for_type (scope as {card_assigns, ...} : scope)
   251 fun best_one_rep_for_type (scope as {card_assigns, ...} : scope)
   252                           (Type (@{type_name fun}, [T1, T2])) =
   252                           (Type (\<^type_name>\<open>fun\<close>, [T1, T2])) =
   253     Vect (card_of_type card_assigns T1, (best_one_rep_for_type scope T2))
   253     Vect (card_of_type card_assigns T1, (best_one_rep_for_type scope T2))
   254   | best_one_rep_for_type scope (Type (@{type_name set}, [T'])) =
   254   | best_one_rep_for_type scope (Type (\<^type_name>\<open>set\<close>, [T'])) =
   255     best_one_rep_for_type scope (T' --> bool_T)
   255     best_one_rep_for_type scope (T' --> bool_T)
   256   | best_one_rep_for_type scope (Type (@{type_name prod}, Ts)) =
   256   | best_one_rep_for_type scope (Type (\<^type_name>\<open>prod\<close>, Ts)) =
   257     Struct (map (best_one_rep_for_type scope) Ts)
   257     Struct (map (best_one_rep_for_type scope) Ts)
   258   | best_one_rep_for_type {card_assigns, ofs, ...} T =
   258   | best_one_rep_for_type {card_assigns, ofs, ...} T =
   259     Atom (card_of_type card_assigns T, offset_of_type ofs T)
   259     Atom (card_of_type card_assigns T, offset_of_type ofs T)
   260 
   260 
   261 fun best_opt_set_rep_for_type scope (Type (@{type_name fun}, [T1, T2])) =
   261 fun best_opt_set_rep_for_type scope (Type (\<^type_name>\<open>fun\<close>, [T1, T2])) =
   262     Func (best_one_rep_for_type scope T1, best_opt_set_rep_for_type scope T2)
   262     Func (best_one_rep_for_type scope T1, best_opt_set_rep_for_type scope T2)
   263   | best_opt_set_rep_for_type scope (Type (@{type_name set}, [T'])) =
   263   | best_opt_set_rep_for_type scope (Type (\<^type_name>\<open>set\<close>, [T'])) =
   264     best_opt_set_rep_for_type scope (T' --> bool_T)
   264     best_opt_set_rep_for_type scope (T' --> bool_T)
   265   | best_opt_set_rep_for_type (scope as {ofs, ...}) T =
   265   | best_opt_set_rep_for_type (scope as {ofs, ...}) T =
   266     opt_rep ofs T (best_one_rep_for_type scope T)
   266     opt_rep ofs T (best_one_rep_for_type scope T)
   267 
   267 
   268 fun best_non_opt_set_rep_for_type scope (Type (@{type_name fun}, [T1, T2])) =
   268 fun best_non_opt_set_rep_for_type scope (Type (\<^type_name>\<open>fun\<close>, [T1, T2])) =
   269     (case (best_one_rep_for_type scope T1,
   269     (case (best_one_rep_for_type scope T1,
   270            best_non_opt_set_rep_for_type scope T2) of
   270            best_non_opt_set_rep_for_type scope T2) of
   271        (R1, Atom (2, _)) => Func (R1, Formula Neut)
   271        (R1, Atom (2, _)) => Func (R1, Formula Neut)
   272      | z => Func z)
   272      | z => Func z)
   273   | best_non_opt_set_rep_for_type scope (Type (@{type_name set}, [T'])) =
   273   | best_non_opt_set_rep_for_type scope (Type (\<^type_name>\<open>set\<close>, [T'])) =
   274     best_non_opt_set_rep_for_type scope (T' --> bool_T)
   274     best_non_opt_set_rep_for_type scope (T' --> bool_T)
   275   | best_non_opt_set_rep_for_type scope T = best_one_rep_for_type scope T
   275   | best_non_opt_set_rep_for_type scope T = best_one_rep_for_type scope T
   276 
   276 
   277 fun best_set_rep_for_type (scope as {data_types, ...}) T =
   277 fun best_set_rep_for_type (scope as {data_types, ...}) T =
   278   (if is_exact_type data_types true T then best_non_opt_set_rep_for_type
   278   (if is_exact_type data_types true T then best_non_opt_set_rep_for_type
   279    else best_opt_set_rep_for_type) scope T
   279    else best_opt_set_rep_for_type) scope T
   280 
   280 
   281 fun best_non_opt_symmetric_reps_for_fun_type (scope as {ofs, ...})
   281 fun best_non_opt_symmetric_reps_for_fun_type (scope as {ofs, ...})
   282                                            (Type (@{type_name fun}, [T1, T2])) =
   282                                            (Type (\<^type_name>\<open>fun\<close>, [T1, T2])) =
   283     (optable_rep ofs T1 (best_one_rep_for_type scope T1),
   283     (optable_rep ofs T1 (best_one_rep_for_type scope T1),
   284      optable_rep ofs T2 (best_one_rep_for_type scope T2))
   284      optable_rep ofs T2 (best_one_rep_for_type scope T2))
   285   | best_non_opt_symmetric_reps_for_fun_type _ T =
   285   | best_non_opt_symmetric_reps_for_fun_type _ T =
   286     raise TYPE ("Nitpick_Rep.best_non_opt_symmetric_reps_for_fun_type", [T], [])
   286     raise TYPE ("Nitpick_Rep.best_non_opt_symmetric_reps_for_fun_type", [T], [])
   287 
   287 
   295   | atom_schema_of_rep (Opt R) = atom_schema_of_rep R
   295   | atom_schema_of_rep (Opt R) = atom_schema_of_rep R
   296 and atom_schema_of_reps Rs = maps atom_schema_of_rep Rs
   296 and atom_schema_of_reps Rs = maps atom_schema_of_rep Rs
   297 
   297 
   298 fun type_schema_of_rep _ (Formula _) = []
   298 fun type_schema_of_rep _ (Formula _) = []
   299   | type_schema_of_rep T (Atom _) = [T]
   299   | type_schema_of_rep T (Atom _) = [T]
   300   | type_schema_of_rep (Type (@{type_name prod}, [T1, T2])) (Struct [R1, R2]) =
   300   | type_schema_of_rep (Type (\<^type_name>\<open>prod\<close>, [T1, T2])) (Struct [R1, R2]) =
   301     type_schema_of_reps [T1, T2] [R1, R2]
   301     type_schema_of_reps [T1, T2] [R1, R2]
   302   | type_schema_of_rep (Type (@{type_name fun}, [_, T2])) (Vect (k, R)) =
   302   | type_schema_of_rep (Type (\<^type_name>\<open>fun\<close>, [_, T2])) (Vect (k, R)) =
   303     replicate_list k (type_schema_of_rep T2 R)
   303     replicate_list k (type_schema_of_rep T2 R)
   304   | type_schema_of_rep (Type (@{type_name fun}, [T1, T2])) (Func (R1, R2)) =
   304   | type_schema_of_rep (Type (\<^type_name>\<open>fun\<close>, [T1, T2])) (Func (R1, R2)) =
   305     type_schema_of_rep T1 R1 @ type_schema_of_rep T2 R2
   305     type_schema_of_rep T1 R1 @ type_schema_of_rep T2 R2
   306   | type_schema_of_rep (Type (@{type_name set}, [T'])) R =
   306   | type_schema_of_rep (Type (\<^type_name>\<open>set\<close>, [T'])) R =
   307     type_schema_of_rep (T' --> bool_T) R
   307     type_schema_of_rep (T' --> bool_T) R
   308   | type_schema_of_rep T (Opt R) = type_schema_of_rep T R
   308   | type_schema_of_rep T (Opt R) = type_schema_of_rep T R
   309   | type_schema_of_rep _ R = raise REP ("Nitpick_Rep.type_schema_of_rep", [R])
   309   | type_schema_of_rep _ R = raise REP ("Nitpick_Rep.type_schema_of_rep", [R])
   310 and type_schema_of_reps Ts Rs = flat (map2 type_schema_of_rep Ts Rs)
   310 and type_schema_of_reps Ts Rs = flat (map2 type_schema_of_rep Ts Rs)
   311 
   311