src/HOL/Tools/Predicate_Compile/predicate_compile_data.ML
changeset 55437 3fd63b92ea3b
parent 51717 9e7d1c139569
child 55440 721b4561007a
equal deleted inserted replaced
55436:9781e17dcc23 55437:3fd63b92ea3b
     9   val ignore_consts : string list -> theory -> theory
     9   val ignore_consts : string list -> theory -> theory
    10   val keep_functions : string list -> theory -> theory
    10   val keep_functions : string list -> theory -> theory
    11   val keep_function : theory -> string -> bool
    11   val keep_function : theory -> string -> bool
    12   val processed_specs : theory -> string -> (string * thm list) list option
    12   val processed_specs : theory -> string -> (string * thm list) list option
    13   val store_processed_specs : (string * (string * thm list) list) -> theory -> theory
    13   val store_processed_specs : (string * (string * thm list) list) -> theory -> theory
    14   
    14 
    15   val get_specification : Predicate_Compile_Aux.options -> theory -> term -> thm list
    15   val get_specification : Predicate_Compile_Aux.options -> theory -> term -> thm list
    16   val obtain_specification_graph :
    16   val obtain_specification_graph :
    17     Predicate_Compile_Aux.options -> theory -> term -> thm list Term_Graph.T
    17     Predicate_Compile_Aux.options -> theory -> term -> thm list Term_Graph.T
    18     
    18 
    19   val present_graph : thm list Term_Graph.T -> unit
    19   val present_graph : thm list Term_Graph.T -> unit
    20   val normalize_equation : theory -> thm -> thm
    20   val normalize_equation : theory -> thm -> thm
    21 end;
    21 end;
    22 
    22 
    23 structure Predicate_Compile_Data : PREDICATE_COMPILE_DATA =
    23 structure Predicate_Compile_Data : PREDICATE_COMPILE_DATA =
    64 
    64 
    65 fun defining_term_of_introrule_term t =
    65 fun defining_term_of_introrule_term t =
    66   let
    66   let
    67     val _ $ u = Logic.strip_imp_concl t
    67     val _ $ u = Logic.strip_imp_concl t
    68   in fst (strip_comb u) end
    68   in fst (strip_comb u) end
    69 (*  
    69 (*
    70   in case pred of
    70   in case pred of
    71     Const (c, T) => c
    71     Const (c, T) => c
    72     | _ => raise TERM ("defining_const_of_introrule_term failed: Not a constant", [t])
    72     | _ => raise TERM ("defining_const_of_introrule_term failed: Not a constant", [t])
    73   end
    73   end
    74 *)
    74 *)
    75 val defining_term_of_introrule = defining_term_of_introrule_term o prop_of
    75 val defining_term_of_introrule = defining_term_of_introrule_term o prop_of
    76 
    76 
    77 fun defining_const_of_introrule th =
    77 fun defining_const_of_introrule th =
    78   case defining_term_of_introrule th
    78   (case defining_term_of_introrule th of
    79    of Const (c, _) => c
    79     Const (c, _) => c
    80     | _ => raise TERM ("defining_const_of_introrule failed: Not a constant", [prop_of th])
    80   | _ => raise TERM ("defining_const_of_introrule failed: Not a constant", [prop_of th]))
    81 
    81 
    82 (*TODO*)
    82 (*TODO*)
    83 fun is_introlike_term _ = true
    83 fun is_introlike_term _ = true
    84 
    84 
    85 val is_introlike = is_introlike_term o prop_of
    85 val is_introlike = is_introlike_term o prop_of
    86 
    86 
    87 fun check_equation_format_term (t as (Const ("==", _) $ u $ _)) =
    87 fun check_equation_format_term (t as (Const ("==", _) $ u $ _)) =
    88   (case strip_comb u of
    88       (case strip_comb u of
    89     (Const (_, T), args) =>
    89         (Const (_, T), args) =>
    90       if (length (binder_types T) = length args) then
    90           if (length (binder_types T) = length args) then
    91         true
    91             true
    92       else
    92           else
    93         raise TERM ("check_equation_format_term failed: Number of arguments mismatch", [t])
    93             raise TERM ("check_equation_format_term failed: Number of arguments mismatch", [t])
    94   | _ => raise TERM ("check_equation_format_term failed: Not a constant", [t]))
    94       | _ => raise TERM ("check_equation_format_term failed: Not a constant", [t]))
    95   | check_equation_format_term t =
    95   | check_equation_format_term t =
    96     raise TERM ("check_equation_format_term failed: Not an equation", [t])
    96       raise TERM ("check_equation_format_term failed: Not an equation", [t])
    97 
    97 
    98 val check_equation_format = check_equation_format_term o prop_of
    98 val check_equation_format = check_equation_format_term o prop_of
    99 
    99 
   100 
   100 
   101 fun defining_term_of_equation_term (Const ("==", _) $ u $ _) = fst (strip_comb u)
   101 fun defining_term_of_equation_term (Const ("==", _) $ u $ _) = fst (strip_comb u)
   102   | defining_term_of_equation_term t =
   102   | defining_term_of_equation_term t =
   103     raise TERM ("defining_const_of_equation_term failed: Not an equation", [t])
   103       raise TERM ("defining_const_of_equation_term failed: Not an equation", [t])
   104 
   104 
   105 val defining_term_of_equation = defining_term_of_equation_term o prop_of
   105 val defining_term_of_equation = defining_term_of_equation_term o prop_of
   106 
   106 
   107 fun defining_const_of_equation th =
   107 fun defining_const_of_equation th =
   108   case defining_term_of_equation th
   108   (case defining_term_of_equation th of
   109    of Const (c, _) => c
   109     Const (c, _) => c
   110     | _ => raise TERM ("defining_const_of_equation failed: Not a constant", [prop_of th])
   110   | _ => raise TERM ("defining_const_of_equation failed: Not a constant", [prop_of th]))
   111 
   111 
   112 
   112 
   113 
   113 
   114 
   114 
   115 (* Normalizing equations *)
   115 (* Normalizing equations *)
   116 
   116 
   117 fun mk_meta_equation th =
   117 fun mk_meta_equation th =
   118   case prop_of th of
   118   (case prop_of th of
   119     Const (@{const_name Trueprop}, _) $ (Const (@{const_name HOL.eq}, _) $ _ $ _) => th RS @{thm eq_reflection}
   119     Const (@{const_name Trueprop}, _) $ (Const (@{const_name HOL.eq}, _) $ _ $ _) =>
   120   | _ => th
   120       th RS @{thm eq_reflection}
       
   121   | _ => th)
   121 
   122 
   122 val meta_fun_cong = @{lemma "f == g ==> f x == g x" by simp}
   123 val meta_fun_cong = @{lemma "f == g ==> f x == g x" by simp}
   123 
   124 
   124 fun full_fun_cong_expand th =
   125 fun full_fun_cong_expand th =
   125   let
   126   let
   129 
   130 
   130 fun declare_names s xs ctxt =
   131 fun declare_names s xs ctxt =
   131   let
   132   let
   132     val res = Name.invent_names ctxt s xs
   133     val res = Name.invent_names ctxt s xs
   133   in (res, fold Name.declare (map fst res) ctxt) end
   134   in (res, fold Name.declare (map fst res) ctxt) end
   134   
   135 
   135 fun split_all_pairs thy th =
   136 fun split_all_pairs thy th =
   136   let
   137   let
   137     val ctxt = Proof_Context.init_global thy  (* FIXME proper context!? *)
   138     val ctxt = Proof_Context.init_global thy  (* FIXME proper context!? *)
   138     val ((_, [th']), _) = Variable.import true [th] ctxt
   139     val ((_, [th']), _) = Variable.import true [th] ctxt
   139     val t = prop_of th'
   140     val t = prop_of th'
   140     val frees = Term.add_frees t [] 
   141     val frees = Term.add_frees t []
   141     val freenames = Term.add_free_names t []
   142     val freenames = Term.add_free_names t []
   142     val nctxt = Name.make_context freenames
   143     val nctxt = Name.make_context freenames
   143     fun mk_tuple_rewrites (x, T) nctxt =
   144     fun mk_tuple_rewrites (x, T) nctxt =
   144       let
   145       let
   145         val Ts = HOLogic.flatten_tupleT T
   146         val Ts = HOLogic.flatten_tupleT T
   146         val (xTs, nctxt') = declare_names x Ts nctxt
   147         val (xTs, nctxt') = declare_names x Ts nctxt
   147         val paths = HOLogic.flat_tupleT_paths T
   148         val paths = HOLogic.flat_tupleT_paths T
   148       in ((Free (x, T), HOLogic.mk_ptuple paths T (map Free xTs)), nctxt') end
   149       in ((Free (x, T), HOLogic.mk_ptuple paths T (map Free xTs)), nctxt') end
   149     val (rewr, _) = fold_map mk_tuple_rewrites frees nctxt 
   150     val (rewr, _) = fold_map mk_tuple_rewrites frees nctxt
   150     val t' = Pattern.rewrite_term thy rewr [] t
   151     val t' = Pattern.rewrite_term thy rewr [] t
   151     val th'' =
   152     val th'' =
   152       Goal.prove ctxt (Term.add_free_names t' []) [] t'
   153       Goal.prove ctxt (Term.add_free_names t' []) [] t'
   153         (fn _ => ALLGOALS Skip_Proof.cheat_tac)
   154         (fn _ => ALLGOALS Skip_Proof.cheat_tac)
   154     val th''' = Local_Defs.unfold ctxt [@{thm split_conv}, @{thm fst_conv}, @{thm snd_conv}] th''
   155     val th''' = Local_Defs.unfold ctxt [@{thm split_conv}, @{thm fst_conv}, @{thm snd_conv}] th''
   160 fun inline_equations thy th =
   161 fun inline_equations thy th =
   161   let
   162   let
   162     val ctxt = Proof_Context.init_global thy
   163     val ctxt = Proof_Context.init_global thy
   163     val inline_defs = Predicate_Compile_Inline_Defs.get ctxt
   164     val inline_defs = Predicate_Compile_Inline_Defs.get ctxt
   164     val th' = (Simplifier.full_simplify (put_simpset HOL_basic_ss ctxt addsimps inline_defs)) th
   165     val th' = (Simplifier.full_simplify (put_simpset HOL_basic_ss ctxt addsimps inline_defs)) th
   165     (*val _ = print_step options 
   166     (*val _ = print_step options
   166       ("Inlining " ^ (Syntax.string_of_term_global thy (prop_of th))
   167       ("Inlining " ^ (Syntax.string_of_term_global thy (prop_of th))
   167        ^ "with " ^ (commas (map ((Syntax.string_of_term_global thy) o prop_of) inline_defs))
   168        ^ "with " ^ (commas (map ((Syntax.string_of_term_global thy) o prop_of) inline_defs))
   168        ^" to " ^ (Syntax.string_of_term_global thy (prop_of th')))*)
   169        ^" to " ^ (Syntax.string_of_term_global thy (prop_of th')))*)
   169   in
   170   in
   170     th'
   171     th'
   204         if is_introlike th andalso defining_const_of_introrule th = fst (dest_Const t) then
   205         if is_introlike th andalso defining_const_of_introrule th = fst (dest_Const t) then
   205           SOME th
   206           SOME th
   206         else
   207         else
   207           NONE
   208           NONE
   208     fun filter_defs ths = map_filter filtering (map (normalize thy o Thm.transfer thy) ths)
   209     fun filter_defs ths = map_filter filtering (map (normalize thy o Thm.transfer thy) ths)
   209     val spec = case filter_defs (Predicate_Compile_Alternative_Defs.get ctxt) of
   210     val spec =
   210       [] => (case Spec_Rules.retrieve ctxt t of
   211       (case filter_defs (Predicate_Compile_Alternative_Defs.get ctxt) of
   211           [] => error ("No specification for " ^ (Syntax.string_of_term_global thy t))
   212         [] =>
   212         | ((_, (_, ths)) :: _) => filter_defs ths)
   213           (case Spec_Rules.retrieve ctxt t of
   213     | ths => rev ths
   214             [] => error ("No specification for " ^ Syntax.string_of_term_global thy t)
       
   215           | ((_, (_, ths)) :: _) => filter_defs ths)
       
   216       | ths => rev ths)
   214     val _ =
   217     val _ =
   215       if show_intermediate_results options then
   218       if show_intermediate_results options then
   216         tracing ("Specification for " ^ (Syntax.string_of_term_global thy t) ^ ":\n" ^
   219         tracing ("Specification for " ^ (Syntax.string_of_term_global thy t) ^ ":\n" ^
   217           commas (map (Display.string_of_thm_global thy) spec))
   220           commas (map (Display.string_of_thm_global thy) spec))
   218       else ()
   221       else ()
   219   in
   222   in
   220     spec
   223     spec
   221   end
   224   end
   222 
   225 
   223 val logic_operator_names =
   226 val logic_operator_names =
   224   [@{const_name "=="}, 
   227   [@{const_name "=="},
   225    @{const_name "==>"},
   228    @{const_name "==>"},
   226    @{const_name Trueprop},
   229    @{const_name Trueprop},
   227    @{const_name Not},
   230    @{const_name Not},
   228    @{const_name HOL.eq},
   231    @{const_name HOL.eq},
   229    @{const_name HOL.implies},
   232    @{const_name HOL.implies},
   230    @{const_name All},
   233    @{const_name All},
   231    @{const_name Ex}, 
   234    @{const_name Ex},
   232    @{const_name HOL.conj},
   235    @{const_name HOL.conj},
   233    @{const_name HOL.disj}]
   236    @{const_name HOL.disj}]
   234 
   237 
   235 fun special_cases (c, _) = member (op =) [
   238 fun special_cases (c, _) =
   236   @{const_name Product_Type.Unity},
   239   member (op =)
   237   @{const_name False},
   240    [@{const_name Product_Type.Unity},
   238   @{const_name Suc}, @{const_name Nat.zero_nat_inst.zero_nat},
   241     @{const_name False},
   239   @{const_name Nat.one_nat_inst.one_nat},
   242     @{const_name Suc}, @{const_name Nat.zero_nat_inst.zero_nat},
   240   @{const_name Orderings.less}, @{const_name Orderings.less_eq},
   243     @{const_name Nat.one_nat_inst.one_nat},
   241   @{const_name Groups.zero},
   244     @{const_name Orderings.less}, @{const_name Orderings.less_eq},
   242   @{const_name Groups.one},  @{const_name Groups.plus},
   245     @{const_name Groups.zero},
   243   @{const_name Nat.ord_nat_inst.less_eq_nat},
   246     @{const_name Groups.one},  @{const_name Groups.plus},
   244   @{const_name Nat.ord_nat_inst.less_nat},
   247     @{const_name Nat.ord_nat_inst.less_eq_nat},
   245 (* FIXME
   248     @{const_name Nat.ord_nat_inst.less_nat},
   246   @{const_name number_nat_inst.number_of_nat},
   249   (* FIXME
   247 *)
   250     @{const_name number_nat_inst.number_of_nat},
   248   @{const_name Num.Bit0},
   251   *)
   249   @{const_name Num.Bit1},
   252     @{const_name Num.Bit0},
   250   @{const_name Num.One},
   253     @{const_name Num.Bit1},
   251   @{const_name Int.zero_int_inst.zero_int},
   254     @{const_name Num.One},
   252   @{const_name List.filter},
   255     @{const_name Int.zero_int_inst.zero_int},
   253   @{const_name HOL.If},
   256     @{const_name List.filter},
   254   @{const_name Groups.minus}
   257     @{const_name HOL.If},
   255   ] c
   258     @{const_name Groups.minus}] c
   256 
   259 
   257 
   260 
   258 fun obtain_specification_graph options thy t =
   261 fun obtain_specification_graph options thy t =
   259   let
   262   let
   260     val ctxt = Proof_Context.init_global thy
   263     val ctxt = Proof_Context.init_global thy
   304       |> maps (Term_Graph.immediate_succs gr)
   307       |> maps (Term_Graph.immediate_succs gr)
   305       |> subtract eq_cname consts
   308       |> subtract eq_cname consts
   306       |> map (the o Termtab.lookup mapping)
   309       |> map (the o Termtab.lookup mapping)
   307       |> distinct (eq_list eq_cname);
   310       |> distinct (eq_list eq_cname);
   308     val conn = [] |> fold (fn consts => cons (consts, succs consts)) constss;
   311     val conn = [] |> fold (fn consts => cons (consts, succs consts)) constss;
   309     
   312 
   310     fun namify consts = map string_of_const consts
   313     fun namify consts = map string_of_const consts
   311       |> commas;
   314       |> commas;
   312     val prgr = map (fn (consts, constss) =>
   315     val prgr = map (fn (consts, constss) =>
   313       { name = namify consts, ID = namify consts, dir = "", unfold = true,
   316       {name = namify consts, ID = namify consts, dir = "", unfold = true,
   314         path = "", parents = map namify constss, content = [] }) conn;
   317        path = "", parents = map namify constss, content = [] }) conn
   315   in Graph_Display.display_graph prgr end;
   318   in Graph_Display.display_graph prgr end
   316 
   319 
   317 
   320 end
   318 end;