src/HOL/Tools/Nitpick/nitpick_model.ML
changeset 41791 01d722707a36
parent 41472 f6ab14e61604
child 41803 ef13e3b7cbaf
equal deleted inserted replaced
41790:56dcd46ddf7a 41791:01d722707a36
   660   | dest_n_tuple_type n (Type (_, [T1, T2])) =
   660   | dest_n_tuple_type n (Type (_, [T1, T2])) =
   661     T1 :: dest_n_tuple_type (n - 1) T2
   661     T1 :: dest_n_tuple_type (n - 1) T2
   662   | dest_n_tuple_type _ T =
   662   | dest_n_tuple_type _ T =
   663     raise TYPE ("Nitpick_Model.dest_n_tuple_type", [T], [])
   663     raise TYPE ("Nitpick_Model.dest_n_tuple_type", [T], [])
   664 
   664 
   665 fun const_format thy def_table (x as (s, T)) =
   665 fun const_format thy def_tables (x as (s, T)) =
   666   if String.isPrefix unrolled_prefix s then
   666   if String.isPrefix unrolled_prefix s then
   667     const_format thy def_table (original_name s, range_type T)
   667     const_format thy def_tables (original_name s, range_type T)
   668   else if String.isPrefix skolem_prefix s then
   668   else if String.isPrefix skolem_prefix s then
   669     let
   669     let
   670       val k = unprefix skolem_prefix s
   670       val k = unprefix skolem_prefix s
   671               |> strip_first_name_sep |> fst |> space_explode "@"
   671               |> strip_first_name_sep |> fst |> space_explode "@"
   672               |> hd |> Int.fromString |> the
   672               |> hd |> Int.fromString |> the
   673     in [k, num_binder_types T - k] end
   673     in [k, num_binder_types T - k] end
   674   else if original_name s <> s then
   674   else if original_name s <> s then
   675     [num_binder_types T]
   675     [num_binder_types T]
   676   else case def_of_const thy def_table x of
   676   else case def_of_const thy def_tables x of
   677     SOME t' => if fixpoint_kind_of_rhs t' <> NoFp then
   677     SOME t' => if fixpoint_kind_of_rhs t' <> NoFp then
   678                  let val k = length (strip_abs_vars t') in
   678                  let val k = length (strip_abs_vars t') in
   679                    [k, num_binder_types T - k]
   679                    [k, num_binder_types T - k]
   680                  end
   680                  end
   681                else
   681                else
   688       intersect_formats (ks1' @ (if k1 > k2 then [k1 - k2] else []))
   688       intersect_formats (ks1' @ (if k1 > k2 then [k1 - k2] else []))
   689                         (ks2' @ (if k2 > k1 then [k2 - k1] else [])) @
   689                         (ks2' @ (if k2 > k1 then [k2 - k1] else [])) @
   690       [Int.min (k1, k2)]
   690       [Int.min (k1, k2)]
   691     end
   691     end
   692 
   692 
   693 fun lookup_format thy def_table formats t =
   693 fun lookup_format thy def_tables formats t =
   694   case AList.lookup (fn (SOME x, SOME y) =>
   694   case AList.lookup (fn (SOME x, SOME y) =>
   695                         (term_match thy) (x, y) | _ => false)
   695                         (term_match thy) (x, y) | _ => false)
   696                     formats (SOME t) of
   696                     formats (SOME t) of
   697     SOME format => format
   697     SOME format => format
   698   | NONE => let val format = the (AList.lookup (op =) formats NONE) in
   698   | NONE => let val format = the (AList.lookup (op =) formats NONE) in
   699               case t of
   699               case t of
   700                 Const x => intersect_formats format
   700                 Const x => intersect_formats format
   701                                              (const_format thy def_table x)
   701                                              (const_format thy def_tables x)
   702               | _ => format
   702               | _ => format
   703             end
   703             end
   704 
   704 
   705 fun format_type default_format format T =
   705 fun format_type default_format format T =
   706   let
   706   let
   717           |> map (format_type default_format default_format)
   717           |> map (format_type default_format default_format)
   718           |> rev |> chunk_list_unevenly (rev format)
   718           |> rev |> chunk_list_unevenly (rev format)
   719           |> map (HOLogic.mk_tupleT o rev)
   719           |> map (HOLogic.mk_tupleT o rev)
   720       in List.foldl (op -->) body_T batched end
   720       in List.foldl (op -->) body_T batched end
   721   end
   721   end
   722 fun format_term_type thy def_table formats t =
   722 fun format_term_type thy def_tables formats t =
   723   format_type (the (AList.lookup (op =) formats NONE))
   723   format_type (the (AList.lookup (op =) formats NONE))
   724               (lookup_format thy def_table formats t) (fastype_of t)
   724               (lookup_format thy def_tables formats t) (fastype_of t)
   725 
   725 
   726 fun repair_special_format js m format =
   726 fun repair_special_format js m format =
   727   m - 1 downto 0 |> chunk_list_unevenly (rev format)
   727   m - 1 downto 0 |> chunk_list_unevenly (rev format)
   728                  |> map (rev o filter_out (member (op =) js))
   728                  |> map (rev o filter_out (member (op =) js))
   729                  |> filter_out null |> map length |> rev
   729                  |> filter_out null |> map length |> rev
   730 
   730 
   731 fun user_friendly_const ({thy, evals, def_table, skolems, special_funs, ...}
   731 fun user_friendly_const ({thy, evals, def_tables, skolems, special_funs, ...}
   732                          : hol_context) (base_name, step_name) formats =
   732                          : hol_context) (base_name, step_name) formats =
   733   let
   733   let
   734     val default_format = the (AList.lookup (op =) formats NONE)
   734     val default_format = the (AList.lookup (op =) formats NONE)
   735     fun do_const (x as (s, T)) =
   735     fun do_const (x as (s, T)) =
   736       (if String.isPrefix special_prefix s then
   736       (if String.isPrefix special_prefix s then
   760              case AList.lookup (fn (SOME t1, SOME t2) => term_match thy (t1, t2)
   760              case AList.lookup (fn (SOME t1, SOME t2) => term_match thy (t1, t2)
   761                                  | _ => false) formats (SOME t) of
   761                                  | _ => false) formats (SOME t) of
   762                SOME format =>
   762                SOME format =>
   763                repair_special_format js (num_binder_types T') format
   763                repair_special_format js (num_binder_types T') format
   764              | NONE =>
   764              | NONE =>
   765                const_format thy def_table x'
   765                const_format thy def_tables x'
   766                |> repair_special_format js (num_binder_types T')
   766                |> repair_special_format js (num_binder_types T')
   767                |> intersect_formats default_format
   767                |> intersect_formats default_format
   768          in
   768          in
   769            (list_comb (t, ts') |> fold_rev abs_var vars,
   769            (list_comb (t, ts') |> fold_rev abs_var vars,
   770             format_type default_format format T)
   770             format_type default_format format T)
   787          end
   787          end
   788        else if String.isPrefix unrolled_prefix s then
   788        else if String.isPrefix unrolled_prefix s then
   789          let val t = Const (original_name s, range_type T) in
   789          let val t = Const (original_name s, range_type T) in
   790            (lambda (Free (iter_var_prefix, nat_T)) t,
   790            (lambda (Free (iter_var_prefix, nat_T)) t,
   791             format_type default_format
   791             format_type default_format
   792                         (lookup_format thy def_table formats t) T)
   792                         (lookup_format thy def_tables formats t) T)
   793          end
   793          end
   794        else if String.isPrefix base_prefix s then
   794        else if String.isPrefix base_prefix s then
   795          (Const (base_name, T --> T) $ Const (unprefix base_prefix s, T),
   795          (Const (base_name, T --> T) $ Const (unprefix base_prefix s, T),
   796           format_type default_format default_format T)
   796           format_type default_format default_format T)
   797        else if String.isPrefix step_prefix s then
   797        else if String.isPrefix step_prefix s then
   798          (Const (step_name, T --> T) $ Const (unprefix step_prefix s, T),
   798          (Const (step_name, T --> T) $ Const (unprefix step_prefix s, T),
   799           format_type default_format default_format T)
   799           format_type default_format default_format T)
   800        else if String.isPrefix quot_normal_prefix s then
   800        else if String.isPrefix quot_normal_prefix s then
   801          let val t = Const (nitpick_prefix ^ "quotient normal form", T) in
   801          let val t = Const (nitpick_prefix ^ "quotient normal form", T) in
   802            (t, format_term_type thy def_table formats t)
   802            (t, format_term_type thy def_tables formats t)
   803          end
   803          end
   804        else if String.isPrefix skolem_prefix s then
   804        else if String.isPrefix skolem_prefix s then
   805          let
   805          let
   806            val ss = the (AList.lookup (op =) (!skolems) s)
   806            val ss = the (AList.lookup (op =) (!skolems) s)
   807            val (Ts, Ts') = chop (length ss) (binder_types T)
   807            val (Ts, Ts') = chop (length ss) (binder_types T)
   808            val frees = map Free (ss ~~ Ts)
   808            val frees = map Free (ss ~~ Ts)
   809            val s' = original_name s
   809            val s' = original_name s
   810          in
   810          in
   811            (fold lambda frees (Const (s', Ts' ---> T)),
   811            (fold lambda frees (Const (s', Ts' ---> T)),
   812             format_type default_format
   812             format_type default_format
   813                         (lookup_format thy def_table formats (Const x)) T)
   813                         (lookup_format thy def_tables formats (Const x)) T)
   814          end
   814          end
   815        else if String.isPrefix eval_prefix s then
   815        else if String.isPrefix eval_prefix s then
   816          let
   816          let
   817            val t = nth evals (the (Int.fromString (unprefix eval_prefix s)))
   817            val t = nth evals (the (Int.fromString (unprefix eval_prefix s)))
   818          in (t, format_term_type thy def_table formats t) end
   818          in (t, format_term_type thy def_tables formats t) end
   819        else
   819        else
   820          let val t = Const (original_name s, T) in
   820          let val t = Const (original_name s, T) in
   821            (t, format_term_type thy def_table formats t)
   821            (t, format_term_type thy def_tables formats t)
   822          end)
   822          end)
   823       |>> map_types uniterize_unarize_unbox_etc_type
   823       |>> map_types uniterize_unarize_unbox_etc_type
   824       |>> shorten_names_in_term |>> Term.map_abs_vars shortest_name
   824       |>> shorten_names_in_term |>> Term.map_abs_vars shortest_name
   825   in do_const end
   825   in do_const end
   826 
   826 
   861 
   861 
   862 fun reconstruct_hol_model {show_datatypes, show_consts}
   862 fun reconstruct_hol_model {show_datatypes, show_consts}
   863         ({hol_ctxt = {thy, ctxt, max_bisim_depth, boxes, stds, wfs, user_axioms,
   863         ({hol_ctxt = {thy, ctxt, max_bisim_depth, boxes, stds, wfs, user_axioms,
   864                       debug, whacks, binary_ints, destroy_constrs, specialize,
   864                       debug, whacks, binary_ints, destroy_constrs, specialize,
   865                       star_linear_preds, tac_timeout, evals, case_names,
   865                       star_linear_preds, tac_timeout, evals, case_names,
   866                       def_table, nondef_table, user_nondefs, simp_table,
   866                       def_tables, nondef_table, user_nondefs, simp_table,
   867                       psimp_table, choice_spec_table, intro_table,
   867                       psimp_table, choice_spec_table, intro_table,
   868                       ground_thm_table, ersatz_table, skolems, special_funs,
   868                       ground_thm_table, ersatz_table, skolems, special_funs,
   869                       unrolled_preds, wf_cache, constr_cache},
   869                       unrolled_preds, wf_cache, constr_cache},
   870          binarize, card_assigns, bits, bisim_depth, datatypes, ofs} : scope)
   870          binarize, card_assigns, bits, bisim_depth, datatypes, ofs} : scope)
   871         formats atomss real_frees pseudo_frees free_names sel_names nonsel_names
   871         formats atomss real_frees pseudo_frees free_names sel_names nonsel_names
   878       {thy = thy, ctxt = ctxt, max_bisim_depth = max_bisim_depth, boxes = boxes,
   878       {thy = thy, ctxt = ctxt, max_bisim_depth = max_bisim_depth, boxes = boxes,
   879        stds = stds, wfs = wfs, user_axioms = user_axioms, debug = debug,
   879        stds = stds, wfs = wfs, user_axioms = user_axioms, debug = debug,
   880        whacks = whacks, binary_ints = binary_ints,
   880        whacks = whacks, binary_ints = binary_ints,
   881        destroy_constrs = destroy_constrs, specialize = specialize,
   881        destroy_constrs = destroy_constrs, specialize = specialize,
   882        star_linear_preds = star_linear_preds, tac_timeout = tac_timeout,
   882        star_linear_preds = star_linear_preds, tac_timeout = tac_timeout,
   883        evals = evals, case_names = case_names, def_table = def_table,
   883        evals = evals, case_names = case_names, def_tables = def_tables,
   884        nondef_table = nondef_table, user_nondefs = user_nondefs,
   884        nondef_table = nondef_table, user_nondefs = user_nondefs,
   885        simp_table = simp_table, psimp_table = psimp_table,
   885        simp_table = simp_table, psimp_table = psimp_table,
   886        choice_spec_table = choice_spec_table, intro_table = intro_table,
   886        choice_spec_table = choice_spec_table, intro_table = intro_table,
   887        ground_thm_table = ground_thm_table, ersatz_table = ersatz_table,
   887        ground_thm_table = ground_thm_table, ersatz_table = ersatz_table,
   888        skolems = skolems, special_funs = special_funs,
   888        skolems = skolems, special_funs = special_funs,
   910       let
   910       let
   911         val (oper, (t1, T'), T) =
   911         val (oper, (t1, T'), T) =
   912           case name of
   912           case name of
   913             FreeName (s, T, _) =>
   913             FreeName (s, T, _) =>
   914             let val t = Free (s, uniterize_unarize_unbox_etc_type T) in
   914             let val t = Free (s, uniterize_unarize_unbox_etc_type T) in
   915               ("=", (t, format_term_type thy def_table formats t), T)
   915               ("=", (t, format_term_type thy def_tables formats t), T)
   916             end
   916             end
   917           | ConstName (s, T, _) =>
   917           | ConstName (s, T, _) =>
   918             (assign_operator_for_const (s, T),
   918             (assign_operator_for_const (s, T),
   919              user_friendly_const hol_ctxt base_step_names formats (s, T), T)
   919              user_friendly_const hol_ctxt base_step_names formats (s, T), T)
   920           | _ => raise NUT ("Nitpick_Model.reconstruct_hol_model.\
   920           | _ => raise NUT ("Nitpick_Model.reconstruct_hol_model.\