src/HOL/Tools/recdef.ML
author Philipp Meyer
Wed Sep 30 11:33:59 2009 +0200 (2009-09-30)
changeset 32866 1238cbb7c08f
parent 32863 5e8cef567042
child 32952 aeb1e44fbc19
permissions -rw-r--r--
atp_minimal using chain_ths again
     1 (*  Title:      HOL/Tools/recdef.ML
     2     Author:     Markus Wenzel, TU Muenchen
     3 
     4 Wrapper module for Konrad Slind's TFL package.
     5 *)
     6 
     7 signature RECDEF =
     8 sig
     9   val get_recdef: theory -> string
    10     -> {simps: thm list, rules: thm list list, induct: thm, tcs: term list} option
    11   val get_hints: Proof.context -> {simps: thm list, congs: (string * thm) list, wfs: thm list}
    12   val simp_add: attribute
    13   val simp_del: attribute
    14   val cong_add: attribute
    15   val cong_del: attribute
    16   val wf_add: attribute
    17   val wf_del: attribute
    18   val add_recdef: bool -> xstring -> string -> ((binding * string) * Attrib.src list) list ->
    19     Attrib.src option -> theory -> theory
    20       * {simps: thm list, rules: thm list list, induct: thm, tcs: term list}
    21   val add_recdef_i: bool -> xstring -> term -> ((binding * term) * attribute list) list ->
    22     theory -> theory * {simps: thm list, rules: thm list list, induct: thm, tcs: term list}
    23   val defer_recdef: xstring -> string list -> (Facts.ref * Attrib.src list) list
    24     -> theory -> theory * {induct_rules: thm}
    25   val defer_recdef_i: xstring -> term list -> thm list -> theory -> theory * {induct_rules: thm}
    26   val recdef_tc: bstring * Attrib.src list -> xstring -> int option -> bool ->
    27     local_theory -> Proof.state
    28   val recdef_tc_i: bstring * Attrib.src list -> string -> int option -> bool ->
    29     local_theory -> Proof.state
    30   val setup: theory -> theory
    31 end;
    32 
    33 structure Recdef: RECDEF =
    34 struct
    35 
    36 
    37 (** recdef hints **)
    38 
    39 (* type hints *)
    40 
    41 type hints = {simps: thm list, congs: (string * thm) list, wfs: thm list};
    42 
    43 fun mk_hints (simps, congs, wfs) = {simps = simps, congs = congs, wfs = wfs}: hints;
    44 fun map_hints f ({simps, congs, wfs}: hints) = mk_hints (f (simps, congs, wfs));
    45 
    46 fun map_simps f = map_hints (fn (simps, congs, wfs) => (f simps, congs, wfs));
    47 fun map_congs f = map_hints (fn (simps, congs, wfs) => (simps, f congs, wfs));
    48 fun map_wfs f = map_hints (fn (simps, congs, wfs) => (simps, congs, f wfs));
    49 
    50 
    51 (* congruence rules *)
    52 
    53 local
    54 
    55 val cong_head =
    56   fst o Term.dest_Const o Term.head_of o fst o Logic.dest_equals o Thm.concl_of;
    57 
    58 fun prep_cong raw_thm =
    59   let val thm = safe_mk_meta_eq raw_thm in (cong_head thm, thm) end;
    60 
    61 in
    62 
    63 fun add_cong raw_thm congs =
    64   let
    65     val (c, thm) = prep_cong raw_thm;
    66     val _ = if AList.defined (op =) congs c
    67       then warning ("Overwriting recdef congruence rule for " ^ quote c)
    68       else ();
    69   in AList.update (op =) (c, thm) congs end;
    70 
    71 fun del_cong raw_thm congs =
    72   let
    73     val (c, thm) = prep_cong raw_thm;
    74     val _ = if AList.defined (op =) congs c
    75       then ()
    76       else warning ("No recdef congruence rule for " ^ quote c);
    77   in AList.delete (op =) c congs end;
    78 
    79 end;
    80 
    81 
    82 
    83 (** global and local recdef data **)
    84 
    85 (* theory data *)
    86 
    87 type recdef_info = {simps: thm list, rules: thm list list, induct: thm, tcs: term list};
    88 
    89 structure GlobalRecdefData = TheoryDataFun
    90 (
    91   type T = recdef_info Symtab.table * hints;
    92   val empty = (Symtab.empty, mk_hints ([], [], [])): T;
    93   val copy = I;
    94   val extend = I;
    95   fun merge _
    96    ((tab1, {simps = simps1, congs = congs1, wfs = wfs1}),
    97     (tab2, {simps = simps2, congs = congs2, wfs = wfs2})) : T =
    98       (Symtab.merge (K true) (tab1, tab2),
    99         mk_hints (Thm.merge_thms (simps1, simps2),
   100           AList.merge (op =) Thm.eq_thm (congs1, congs2),
   101           Thm.merge_thms (wfs1, wfs2)));
   102 );
   103 
   104 val get_recdef = Symtab.lookup o #1 o GlobalRecdefData.get;
   105 
   106 fun put_recdef name info thy =
   107   let
   108     val (tab, hints) = GlobalRecdefData.get thy;
   109     val tab' = Symtab.update_new (name, info) tab
   110       handle Symtab.DUP _ => error ("Duplicate recursive function definition " ^ quote name);
   111   in GlobalRecdefData.put (tab', hints) thy end;
   112 
   113 val get_global_hints = #2 o GlobalRecdefData.get;
   114 
   115 
   116 (* proof data *)
   117 
   118 structure LocalRecdefData = ProofDataFun
   119 (
   120   type T = hints;
   121   val init = get_global_hints;
   122 );
   123 
   124 val get_hints = LocalRecdefData.get;
   125 fun map_hints f = Context.mapping (GlobalRecdefData.map (apsnd f)) (LocalRecdefData.map f);
   126 
   127 
   128 (* attributes *)
   129 
   130 fun attrib f = Thm.declaration_attribute (map_hints o f);
   131 
   132 val simp_add = attrib (map_simps o Thm.add_thm);
   133 val simp_del = attrib (map_simps o Thm.del_thm);
   134 val cong_add = attrib (map_congs o add_cong);
   135 val cong_del = attrib (map_congs o del_cong);
   136 val wf_add = attrib (map_wfs o Thm.add_thm);
   137 val wf_del = attrib (map_wfs o Thm.del_thm);
   138 
   139 
   140 (* modifiers *)
   141 
   142 val recdef_simpN = "recdef_simp";
   143 val recdef_congN = "recdef_cong";
   144 val recdef_wfN = "recdef_wf";
   145 
   146 val recdef_modifiers =
   147  [Args.$$$ recdef_simpN -- Args.colon >> K ((I, simp_add): Method.modifier),
   148   Args.$$$ recdef_simpN -- Args.add -- Args.colon >> K (I, simp_add),
   149   Args.$$$ recdef_simpN -- Args.del -- Args.colon >> K (I, simp_del),
   150   Args.$$$ recdef_congN -- Args.colon >> K (I, cong_add),
   151   Args.$$$ recdef_congN -- Args.add -- Args.colon >> K (I, cong_add),
   152   Args.$$$ recdef_congN -- Args.del -- Args.colon >> K (I, cong_del),
   153   Args.$$$ recdef_wfN -- Args.colon >> K (I, wf_add),
   154   Args.$$$ recdef_wfN -- Args.add -- Args.colon >> K (I, wf_add),
   155   Args.$$$ recdef_wfN -- Args.del -- Args.colon >> K (I, wf_del)] @
   156   Clasimp.clasimp_modifiers;
   157 
   158 
   159 
   160 (** prepare_hints(_i) **)
   161 
   162 fun prepare_hints thy opt_src =
   163   let
   164     val ctxt0 = ProofContext.init thy;
   165     val ctxt =
   166       (case opt_src of
   167         NONE => ctxt0
   168       | SOME src => #2 (Method.syntax (Method.sections recdef_modifiers) src ctxt0));
   169     val {simps, congs, wfs} = get_hints ctxt;
   170     val cs = claset_of ctxt;
   171     val ss = simpset_of ctxt addsimps simps;
   172   in (cs, ss, rev (map snd congs), wfs) end;
   173 
   174 fun prepare_hints_i thy () =
   175   let
   176     val ctxt0 = ProofContext.init thy;
   177     val {simps, congs, wfs} = get_global_hints thy;
   178   in (claset_of ctxt0, simpset_of ctxt0 addsimps simps, rev (map snd congs), wfs) end;
   179 
   180 
   181 
   182 (** add_recdef(_i) **)
   183 
   184 fun requires_recdef thy = Theory.requires thy "Recdef" "recursive functions";
   185 
   186 fun gen_add_recdef tfl_fn prep_att prep_hints not_permissive raw_name R eq_srcs hints thy =
   187   let
   188     val _ = requires_recdef thy;
   189 
   190     val name = Sign.intern_const thy raw_name;
   191     val bname = Long_Name.base_name name;
   192     val _ = writeln ("Defining recursive function " ^ quote name ^ " ...");
   193 
   194     val ((eq_names, eqs), raw_eq_atts) = apfst split_list (split_list eq_srcs);
   195     val eq_atts = map (map (prep_att thy)) raw_eq_atts;
   196 
   197     val (cs, ss, congs, wfs) = prep_hints thy hints;
   198     (*We must remove imp_cong to prevent looping when the induction rule
   199       is simplified. Many induction rules have nested implications that would
   200       give rise to looping conditional rewriting.*)
   201     val (thy, {rules = rules_idx, induct, tcs}) =
   202         tfl_fn not_permissive thy cs (ss delcongs [imp_cong])
   203                congs wfs name R eqs;
   204     val rules = (map o map) fst (partition_eq (eq_snd (op = : int * int -> bool)) rules_idx);
   205     val simp_att = if null tcs then [Simplifier.simp_add, Nitpick_Const_Simps.add,
   206       Code.add_default_eqn_attribute, Quickcheck_RecFun_Simps.add] else [];
   207 
   208     val ((simps' :: rules', [induct']), thy) =
   209       thy
   210       |> Sign.add_path bname
   211       |> PureThy.add_thmss
   212         (((Binding.name "simps", List.concat rules), simp_att) :: ((eq_names ~~ rules) ~~ eq_atts))
   213       ||>> PureThy.add_thms [((Binding.name "induct", induct), [])];
   214     val result = {simps = simps', rules = rules', induct = induct', tcs = tcs};
   215     val thy =
   216       thy
   217       |> put_recdef name result
   218       |> Sign.parent_path;
   219   in (thy, result) end;
   220 
   221 val add_recdef = gen_add_recdef Tfl.define Attrib.attribute prepare_hints;
   222 fun add_recdef_i x y z w = gen_add_recdef Tfl.define_i (K I) prepare_hints_i x y z w ();
   223 
   224 
   225 
   226 (** defer_recdef(_i) **)
   227 
   228 fun gen_defer_recdef tfl_fn eval_thms raw_name eqs raw_congs thy =
   229   let
   230     val name = Sign.intern_const thy raw_name;
   231     val bname = Long_Name.base_name name;
   232 
   233     val _ = requires_recdef thy;
   234     val _ = writeln ("Deferred recursive function " ^ quote name ^ " ...");
   235 
   236     val congs = eval_thms (ProofContext.init thy) raw_congs;
   237     val (thy2, induct_rules) = tfl_fn thy congs name eqs;
   238     val ([induct_rules'], thy3) =
   239       thy2
   240       |> Sign.add_path bname
   241       |> PureThy.add_thms [((Binding.name "induct_rules", induct_rules), [])]
   242       ||> Sign.parent_path;
   243   in (thy3, {induct_rules = induct_rules'}) end;
   244 
   245 val defer_recdef = gen_defer_recdef Tfl.defer Attrib.eval_thms;
   246 val defer_recdef_i = gen_defer_recdef Tfl.defer_i (K I);
   247 
   248 
   249 
   250 (** recdef_tc(_i) **)
   251 
   252 fun gen_recdef_tc prep_att prep_name (bname, raw_atts) raw_name opt_i int lthy =
   253   let
   254     val thy = ProofContext.theory_of lthy;
   255     val name = prep_name thy raw_name;
   256     val atts = map (prep_att thy) raw_atts;
   257     val tcs =
   258       (case get_recdef thy name of
   259         NONE => error ("No recdef definition of constant: " ^ quote name)
   260       | SOME {tcs, ...} => tcs);
   261     val i = the_default 1 opt_i;
   262     val tc = nth tcs (i - 1) handle Subscript =>
   263       error ("No termination condition #" ^ string_of_int i ^
   264         " in recdef definition of " ^ quote name);
   265   in
   266     Specification.theorem Thm.internalK NONE (K I) (Binding.name bname, atts)
   267       [] (Element.Shows [(Attrib.empty_binding, [(HOLogic.mk_Trueprop tc, [])])]) int lthy
   268   end;
   269 
   270 val recdef_tc = gen_recdef_tc Attrib.intern_src Sign.intern_const;
   271 val recdef_tc_i = gen_recdef_tc (K I) (K I);
   272 
   273 
   274 
   275 (** package setup **)
   276 
   277 (* setup theory *)
   278 
   279 val setup =
   280   Attrib.setup @{binding recdef_simp} (Attrib.add_del simp_add simp_del)
   281     "declaration of recdef simp rule" #>
   282   Attrib.setup @{binding recdef_cong} (Attrib.add_del cong_add cong_del)
   283     "declaration of recdef cong rule" #>
   284   Attrib.setup @{binding recdef_wf} (Attrib.add_del wf_add wf_del)
   285     "declaration of recdef wf rule";
   286 
   287 
   288 (* outer syntax *)
   289 
   290 local structure P = OuterParse and K = OuterKeyword in
   291 
   292 val _ = List.app OuterKeyword.keyword ["permissive", "congs", "hints"];
   293 
   294 val hints =
   295   P.$$$ "(" |-- P.!!! (P.position (P.$$$ "hints" -- Args.parse) --| P.$$$ ")") >> Args.src;
   296 
   297 val recdef_decl =
   298   Scan.optional (P.$$$ "(" -- P.!!! (P.$$$ "permissive" -- P.$$$ ")") >> K false) true --
   299   P.name -- P.term -- Scan.repeat1 (SpecParse.opt_thm_name ":" -- P.prop)
   300     -- Scan.option hints
   301   >> (fn ((((p, f), R), eqs), src) => #1 o add_recdef p f R (map P.triple_swap eqs) src);
   302 
   303 val _ =
   304   OuterSyntax.command "recdef" "define general recursive functions (TFL)" K.thy_decl
   305     (recdef_decl >> Toplevel.theory);
   306 
   307 
   308 val defer_recdef_decl =
   309   P.name -- Scan.repeat1 P.prop --
   310   Scan.optional (P.$$$ "(" |-- P.$$$ "congs" |-- P.!!! (SpecParse.xthms1 --| P.$$$ ")")) []
   311   >> (fn ((f, eqs), congs) => #1 o defer_recdef f eqs congs);
   312 
   313 val _ =
   314   OuterSyntax.command "defer_recdef" "defer general recursive functions (TFL)" K.thy_decl
   315     (defer_recdef_decl >> Toplevel.theory);
   316 
   317 val _ =
   318   OuterSyntax.local_theory_to_proof' "recdef_tc" "recommence proof of termination condition (TFL)"
   319     K.thy_goal
   320     ((SpecParse.opt_thm_name ":" >> apfst Binding.name_of) -- P.xname --
   321         Scan.option (P.$$$ "(" |-- P.nat --| P.$$$ ")")
   322       >> (fn ((thm_name, name), i) => recdef_tc thm_name name i));
   323 
   324 end;
   325 
   326 end;