src/HOL/Tools/Function/context_tree.ML
author blanchet
Thu, 13 Mar 2014 13:18:13 +0100
changeset 56085 3d11892ea537
parent 55085 0e8e4dc55866
permissions -rw-r--r--
killed a few 'metis' calls
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31775
2b04504fcb69 uniformly capitialized names for subdirectories
haftmann
parents: 30492
diff changeset
     1
(*  Title:      HOL/Tools/Function/context_tree.ML
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     2
    Author:     Alexander Krauss, TU Muenchen
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     3
41114
f9ae7c2abf7e tuned headers
krauss
parents: 40317
diff changeset
     4
Construction and traversal of trees of nested contexts along a term.
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     5
*)
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     6
33099
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents: 33049
diff changeset
     7
signature FUNCTION_CTXTREE =
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     8
sig
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
     9
  (* poor man's contexts: fixes + assumes *)
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
    10
  type ctxt = (string * typ) list * thm list
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
    11
  type ctx_tree
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    12
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
    13
  (* FIXME: This interface is a mess and needs to be cleaned up! *)
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
    14
  val get_function_congs : Proof.context -> thm list
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
    15
  val add_function_cong : thm -> Context.generic -> Context.generic
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
    16
  val map_function_congs : (thm list -> thm list) -> Context.generic -> Context.generic
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    17
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
    18
  val cong_add: attribute
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
    19
  val cong_del: attribute
24168
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
    20
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
    21
  val mk_tree: (string * typ) -> term -> Proof.context -> term -> ctx_tree
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20289
diff changeset
    22
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
    23
  val inst_tree: theory -> term -> term -> ctx_tree -> ctx_tree
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    24
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
    25
  val export_term : ctxt -> term -> term
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
    26
  val export_thm : theory -> ctxt -> thm -> thm
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
    27
  val import_thm : theory -> ctxt -> thm -> thm
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    28
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
    29
  val traverse_tree :
26115
3c38ef7cf54f removed dead code; some cleanup
krauss
parents: 25538
diff changeset
    30
   (ctxt -> term ->
3c38ef7cf54f removed dead code; some cleanup
krauss
parents: 25538
diff changeset
    31
   (ctxt * thm) list ->
3c38ef7cf54f removed dead code; some cleanup
krauss
parents: 25538
diff changeset
    32
   (ctxt * thm) list * 'b ->
3c38ef7cf54f removed dead code; some cleanup
krauss
parents: 25538
diff changeset
    33
   (ctxt * thm) list * 'b)
23819
2040846d1bbe some interface cleanup
krauss
parents: 21237
diff changeset
    34
   -> ctx_tree -> 'b -> 'b
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    35
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 44338
diff changeset
    36
  val rewrite_by_tree : Proof.context -> term -> thm -> (thm * thm) list ->
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
    37
    ctx_tree -> thm * (thm * thm) list
55085
0e8e4dc55866 moved 'fundef_cong' attribute (and other basic 'fun' stuff) up the dependency chain
blanchet
parents: 51717
diff changeset
    38
0e8e4dc55866 moved 'fundef_cong' attribute (and other basic 'fun' stuff) up the dependency chain
blanchet
parents: 51717
diff changeset
    39
  val setup : theory -> theory
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    40
end
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    41
33099
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents: 33049
diff changeset
    42
structure Function_Ctx_Tree : FUNCTION_CTXTREE =
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    43
struct
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    44
26115
3c38ef7cf54f removed dead code; some cleanup
krauss
parents: 25538
diff changeset
    45
type ctxt = (string * typ) list * thm list
3c38ef7cf54f removed dead code; some cleanup
krauss
parents: 25538
diff changeset
    46
33099
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents: 33049
diff changeset
    47
open Function_Common
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents: 33049
diff changeset
    48
open Function_Lib
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    49
33519
e31a85f92ce9 adapted Generic_Data, Proof_Data;
wenzelm
parents: 33099
diff changeset
    50
structure FunctionCongs = Generic_Data
24168
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
    51
(
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
    52
  type T = thm list
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
    53
  val empty = []
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
    54
  val extend = I
33519
e31a85f92ce9 adapted Generic_Data, Proof_Data;
wenzelm
parents: 33099
diff changeset
    55
  val merge = Thm.merge_thms
24168
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
    56
);
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
    57
33099
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents: 33049
diff changeset
    58
val get_function_congs = FunctionCongs.get o Context.Proof
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents: 33049
diff changeset
    59
val map_function_congs = FunctionCongs.map
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents: 33049
diff changeset
    60
val add_function_cong = FunctionCongs.map o Thm.add_thm
24168
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
    61
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
    62
(* congruence rules *)
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
    63
33099
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents: 33049
diff changeset
    64
val cong_add = Thm.declaration_attribute (map_function_congs o Thm.add_thm o safe_mk_meta_eq);
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents: 33049
diff changeset
    65
val cong_del = Thm.declaration_attribute (map_function_congs o Thm.del_thm o safe_mk_meta_eq);
24168
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
    66
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
    67
35403
25a67a606782 modernized structure Int_Graph;
wenzelm
parents: 34232
diff changeset
    68
type depgraph = int Int_Graph.T
23819
2040846d1bbe some interface cleanup
krauss
parents: 21237
diff changeset
    69
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
    70
datatype ctx_tree =
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
    71
  Leaf of term
26115
3c38ef7cf54f removed dead code; some cleanup
krauss
parents: 25538
diff changeset
    72
  | Cong of (thm * depgraph * (ctxt * ctx_tree) list)
23819
2040846d1bbe some interface cleanup
krauss
parents: 21237
diff changeset
    73
  | RCall of (term * ctx_tree)
2040846d1bbe some interface cleanup
krauss
parents: 21237
diff changeset
    74
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    75
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    76
(* Maps "Trueprop A = B" to "A" *)
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    77
val rhs_of = snd o HOLogic.dest_eq o HOLogic.dest_Trueprop
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    78
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    79
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    80
(*** Dependency analysis for congruence rules ***)
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    81
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
    82
fun branch_vars t =
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
    83
  let
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
    84
    val t' = snd (dest_all_all t)
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
    85
    val (assumes, concl) = Logic.strip_horn t'
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
    86
  in
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
    87
    (fold Term.add_vars assumes [], Term.add_vars concl [])
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
    88
  end
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    89
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    90
fun cong_deps crule =
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
    91
  let
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
    92
    val num_branches = map_index (apsnd branch_vars) (prems_of crule)
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
    93
  in
35403
25a67a606782 modernized structure Int_Graph;
wenzelm
parents: 34232
diff changeset
    94
    Int_Graph.empty
25a67a606782 modernized structure Int_Graph;
wenzelm
parents: 34232
diff changeset
    95
    |> fold (fn (i,_)=> Int_Graph.new_node (i,i)) num_branches
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
    96
    |> fold_product (fn (i, (c1, _)) => fn (j, (_, t2)) =>
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
    97
         if i = j orelse null (inter (op =) c1 t2)
35403
25a67a606782 modernized structure Int_Graph;
wenzelm
parents: 34232
diff changeset
    98
         then I else Int_Graph.add_edge_acyclic (i,j))
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
    99
       num_branches num_branches
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   100
    end
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   101
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   102
val default_congs =
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   103
  map (fn c => c RS eq_reflection) [@{thm "cong"}, @{thm "ext"}]
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   104
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   105
(* Called on the INSTANTIATED branches of the congruence rule *)
42362
5528970ac6f7 observe firm naming convention ctxt: Proof.context;
wenzelm
parents: 42361
diff changeset
   106
fun mk_branch ctxt t =
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   107
  let
42495
1af81b70cf09 clarified Variable.focus vs. Variable.focus_cterm -- eliminated clone;
wenzelm
parents: 42362
diff changeset
   108
    val ((params, impl), ctxt') = Variable.focus t ctxt
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   109
    val (assms, concl) = Logic.strip_horn impl
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   110
  in
42495
1af81b70cf09 clarified Variable.focus vs. Variable.focus_cterm -- eliminated clone;
wenzelm
parents: 42362
diff changeset
   111
    (ctxt', map #2 params, assms, rhs_of concl)
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   112
  end
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   113
42362
5528970ac6f7 observe firm naming convention ctxt: Proof.context;
wenzelm
parents: 42361
diff changeset
   114
fun find_cong_rule ctxt fvar h ((r,dep)::rs) t =
5528970ac6f7 observe firm naming convention ctxt: Proof.context;
wenzelm
parents: 42361
diff changeset
   115
     (let
5528970ac6f7 observe firm naming convention ctxt: Proof.context;
wenzelm
parents: 42361
diff changeset
   116
        val thy = Proof_Context.theory_of ctxt
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   117
42362
5528970ac6f7 observe firm naming convention ctxt: Proof.context;
wenzelm
parents: 42361
diff changeset
   118
        val tt' = Logic.mk_equals (Pattern.rewrite_term thy [(Free fvar, h)] [] t, t)
5528970ac6f7 observe firm naming convention ctxt: Proof.context;
wenzelm
parents: 42361
diff changeset
   119
        val (c, subs) = (concl_of r, prems_of r)
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   120
42362
5528970ac6f7 observe firm naming convention ctxt: Proof.context;
wenzelm
parents: 42361
diff changeset
   121
        val subst =
5528970ac6f7 observe firm naming convention ctxt: Proof.context;
wenzelm
parents: 42361
diff changeset
   122
          Pattern.match (Proof_Context.theory_of ctxt) (c, tt') (Vartab.empty, Vartab.empty)
5528970ac6f7 observe firm naming convention ctxt: Proof.context;
wenzelm
parents: 42361
diff changeset
   123
        val branches = map (mk_branch ctxt o Envir.beta_norm o Envir.subst_term subst) subs
5528970ac6f7 observe firm naming convention ctxt: Proof.context;
wenzelm
parents: 42361
diff changeset
   124
        val inst = map (fn v =>
5528970ac6f7 observe firm naming convention ctxt: Proof.context;
wenzelm
parents: 42361
diff changeset
   125
            (cterm_of thy (Var v), cterm_of thy (Envir.subst_term subst (Var v))))
5528970ac6f7 observe firm naming convention ctxt: Proof.context;
wenzelm
parents: 42361
diff changeset
   126
          (Term.add_vars c [])
5528970ac6f7 observe firm naming convention ctxt: Proof.context;
wenzelm
parents: 42361
diff changeset
   127
      in
5528970ac6f7 observe firm naming convention ctxt: Proof.context;
wenzelm
parents: 42361
diff changeset
   128
         (cterm_instantiate inst r, dep, branches)
5528970ac6f7 observe firm naming convention ctxt: Proof.context;
wenzelm
parents: 42361
diff changeset
   129
      end
5528970ac6f7 observe firm naming convention ctxt: Proof.context;
wenzelm
parents: 42361
diff changeset
   130
      handle Pattern.MATCH => find_cong_rule ctxt fvar h rs t)
40317
1eac228c52b3 replaced ancient sys_error by raise Fail, assuming that the latter is not handled specifically by the environment;
wenzelm
parents: 36945
diff changeset
   131
  | find_cong_rule _ _ _ [] _ = raise General.Fail "No cong rule found!"
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   132
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   133
24168
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
   134
fun mk_tree fvar h ctxt t =
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   135
  let
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   136
    val congs = get_function_congs ctxt
26115
3c38ef7cf54f removed dead code; some cleanup
krauss
parents: 25538
diff changeset
   137
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   138
    (* FIXME: Save in theory: *)
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   139
    val congs_deps = map (fn c => (c, cong_deps c)) (congs @ default_congs)
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   140
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   141
    fun matchcall (a $ b) = if a = Free fvar then SOME b else NONE
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   142
      | matchcall _ = NONE
24168
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
   143
42362
5528970ac6f7 observe firm naming convention ctxt: Proof.context;
wenzelm
parents: 42361
diff changeset
   144
    fun mk_tree' ctxt t =
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   145
      case matchcall t of
42362
5528970ac6f7 observe firm naming convention ctxt: Proof.context;
wenzelm
parents: 42361
diff changeset
   146
        SOME arg => RCall (t, mk_tree' ctxt arg)
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   147
      | NONE =>
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   148
        if not (exists_subterm (fn Free v => v = fvar | _ => false) t) then Leaf t
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   149
        else
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   150
          let
42362
5528970ac6f7 observe firm naming convention ctxt: Proof.context;
wenzelm
parents: 42361
diff changeset
   151
            val (r, dep, branches) = find_cong_rule ctxt fvar h congs_deps t
5528970ac6f7 observe firm naming convention ctxt: Proof.context;
wenzelm
parents: 42361
diff changeset
   152
            fun subtree (ctxt', fixes, assumes, st) =
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   153
              ((fixes,
42362
5528970ac6f7 observe firm naming convention ctxt: Proof.context;
wenzelm
parents: 42361
diff changeset
   154
                map (Thm.assume o cterm_of (Proof_Context.theory_of ctxt)) assumes),
5528970ac6f7 observe firm naming convention ctxt: Proof.context;
wenzelm
parents: 42361
diff changeset
   155
               mk_tree' ctxt' st)
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   156
          in
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   157
            Cong (r, dep, map subtree branches)
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   158
          end
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   159
  in
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   160
    mk_tree' ctxt t
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   161
  end
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   162
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20289
diff changeset
   163
fun inst_tree thy fvar f tr =
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   164
  let
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   165
    val cfvar = cterm_of thy fvar
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   166
    val cf = cterm_of thy f
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20289
diff changeset
   167
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   168
    fun inst_term t =
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   169
      subst_bound(f, abstract_over (fvar, t))
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   170
36945
9bec62c10714 less pervasive names from structure Thm;
wenzelm
parents: 35403
diff changeset
   171
    val inst_thm = Thm.forall_elim cf o Thm.forall_intr cfvar
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20289
diff changeset
   172
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   173
    fun inst_tree_aux (Leaf t) = Leaf t
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   174
      | inst_tree_aux (Cong (crule, deps, branches)) =
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   175
        Cong (inst_thm crule, deps, map inst_branch branches)
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   176
      | inst_tree_aux (RCall (t, str)) =
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   177
        RCall (inst_term t, inst_tree_aux str)
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   178
    and inst_branch ((fxs, assms), str) =
36945
9bec62c10714 less pervasive names from structure Thm;
wenzelm
parents: 35403
diff changeset
   179
      ((fxs, map (Thm.assume o cterm_of thy o inst_term o prop_of) assms),
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   180
       inst_tree_aux str)
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   181
  in
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   182
    inst_tree_aux tr
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   183
  end
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20289
diff changeset
   184
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20289
diff changeset
   185
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   186
(* Poor man's contexts: Only fixes and assumes *)
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   187
fun compose (fs1, as1) (fs2, as2) = (fs1 @ fs2, as1 @ as2)
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   188
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   189
fun export_term (fixes, assumes) =
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   190
 fold_rev (curry Logic.mk_implies o prop_of) assumes
27330
1af2598b5f7d Logic.all/mk_equals/mk_implies;
wenzelm
parents: 26529
diff changeset
   191
 #> fold_rev (Logic.all o Free) fixes
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   192
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   193
fun export_thm thy (fixes, assumes) =
36945
9bec62c10714 less pervasive names from structure Thm;
wenzelm
parents: 35403
diff changeset
   194
 fold_rev (Thm.implies_intr o cprop_of) assumes
9bec62c10714 less pervasive names from structure Thm;
wenzelm
parents: 35403
diff changeset
   195
 #> fold_rev (Thm.forall_intr o cterm_of thy o Free) fixes
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   196
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   197
fun import_thm thy (fixes, athms) =
36945
9bec62c10714 less pervasive names from structure Thm;
wenzelm
parents: 35403
diff changeset
   198
 fold (Thm.forall_elim o cterm_of thy o Free) fixes
24977
9f98751c9628 replaced (flip Thm.implies_elim) by Thm.elim_implies;
wenzelm
parents: 24168
diff changeset
   199
 #> fold Thm.elim_implies athms
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   200
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   201
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   202
(* folds in the order of the dependencies of a graph. *)
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   203
fun fold_deps G f x =
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   204
  let
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   205
    fun fill_table i (T, x) =
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   206
      case Inttab.lookup T i of
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   207
        SOME _ => (T, x)
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   208
      | NONE =>
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   209
        let
44338
700008399ee5 refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
wenzelm
parents: 42495
diff changeset
   210
          val (T', x') = Int_Graph.Keys.fold fill_table (Int_Graph.imm_succs G i) (T, x)
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   211
          val (v, x'') = f (the o Inttab.lookup T') i x'
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   212
        in
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   213
          (Inttab.update (i, v) T', x'')
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   214
        end
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   215
35403
25a67a606782 modernized structure Int_Graph;
wenzelm
parents: 34232
diff changeset
   216
    val (T, x) = fold fill_table (Int_Graph.keys G) (Inttab.empty, x)
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   217
  in
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   218
    (Inttab.fold (cons o snd) T [], x)
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   219
  end
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   220
26115
3c38ef7cf54f removed dead code; some cleanup
krauss
parents: 25538
diff changeset
   221
fun traverse_tree rcOp tr =
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   222
  let
42362
5528970ac6f7 observe firm naming convention ctxt: Proof.context;
wenzelm
parents: 42361
diff changeset
   223
    fun traverse_help ctxt (Leaf _) _ x = ([], x)
5528970ac6f7 observe firm naming convention ctxt: Proof.context;
wenzelm
parents: 42361
diff changeset
   224
      | traverse_help ctxt (RCall (t, st)) u x =
5528970ac6f7 observe firm naming convention ctxt: Proof.context;
wenzelm
parents: 42361
diff changeset
   225
          rcOp ctxt t u (traverse_help ctxt st u x)
5528970ac6f7 observe firm naming convention ctxt: Proof.context;
wenzelm
parents: 42361
diff changeset
   226
      | traverse_help ctxt (Cong (_, deps, branches)) u x =
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   227
          let
42362
5528970ac6f7 observe firm naming convention ctxt: Proof.context;
wenzelm
parents: 42361
diff changeset
   228
            fun sub_step lu i x =
5528970ac6f7 observe firm naming convention ctxt: Proof.context;
wenzelm
parents: 42361
diff changeset
   229
              let
5528970ac6f7 observe firm naming convention ctxt: Proof.context;
wenzelm
parents: 42361
diff changeset
   230
                val (ctxt', subtree) = nth branches i
44338
700008399ee5 refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
wenzelm
parents: 42495
diff changeset
   231
                val used = Int_Graph.Keys.fold_rev (append o lu) (Int_Graph.imm_succs deps i) u
42362
5528970ac6f7 observe firm naming convention ctxt: Proof.context;
wenzelm
parents: 42361
diff changeset
   232
                val (subs, x') = traverse_help (compose ctxt ctxt') subtree used x
5528970ac6f7 observe firm naming convention ctxt: Proof.context;
wenzelm
parents: 42361
diff changeset
   233
                val exported_subs = map (apfst (compose ctxt')) subs (* FIXME: Right order of composition? *)
5528970ac6f7 observe firm naming convention ctxt: Proof.context;
wenzelm
parents: 42361
diff changeset
   234
              in
5528970ac6f7 observe firm naming convention ctxt: Proof.context;
wenzelm
parents: 42361
diff changeset
   235
                (exported_subs, x')
5528970ac6f7 observe firm naming convention ctxt: Proof.context;
wenzelm
parents: 42361
diff changeset
   236
              end
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   237
          in
42362
5528970ac6f7 observe firm naming convention ctxt: Proof.context;
wenzelm
parents: 42361
diff changeset
   238
            fold_deps deps sub_step x
5528970ac6f7 observe firm naming convention ctxt: Proof.context;
wenzelm
parents: 42361
diff changeset
   239
            |> apfst flat
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   240
          end
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   241
  in
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   242
    snd o traverse_help ([], []) tr []
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   243
  end
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   244
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 44338
diff changeset
   245
fun rewrite_by_tree ctxt h ih x tr =
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   246
  let
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 44338
diff changeset
   247
    val thy = Proof_Context.theory_of ctxt
36945
9bec62c10714 less pervasive names from structure Thm;
wenzelm
parents: 35403
diff changeset
   248
    fun rewrite_help _ _ x (Leaf t) = (Thm.reflexive (cterm_of thy t), x)
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   249
      | rewrite_help fix h_as x (RCall (_ $ arg, st)) =
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   250
        let
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   251
          val (inner, (lRi,ha)::x') = rewrite_help fix h_as x st (* "a' = a" *)
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   252
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   253
          val iha = import_thm thy (fix, h_as) ha (* (a', h a') : G *)
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   254
            |> Conv.fconv_rule (Conv.arg_conv (Conv.comb_conv (Conv.arg_conv (K inner))))
26196
0a0c2752561e Use conversions instead of simplifier. tuned
krauss
parents: 26115
diff changeset
   255
                                                    (* (a, h a) : G   *)
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   256
          val inst_ih = instantiate' [] [SOME (cterm_of thy arg)] ih
36945
9bec62c10714 less pervasive names from structure Thm;
wenzelm
parents: 35403
diff changeset
   257
          val eq = Thm.implies_elim (Thm.implies_elim inst_ih lRi) iha (* h a = f a *)
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   258
36945
9bec62c10714 less pervasive names from structure Thm;
wenzelm
parents: 35403
diff changeset
   259
          val h_a'_eq_h_a = Thm.combination (Thm.reflexive (cterm_of thy h)) inner
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   260
          val h_a_eq_f_a = eq RS eq_reflection
36945
9bec62c10714 less pervasive names from structure Thm;
wenzelm
parents: 35403
diff changeset
   261
          val result = Thm.transitive h_a'_eq_h_a h_a_eq_f_a
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   262
        in
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   263
          (result, x')
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   264
        end
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   265
      | rewrite_help fix h_as x (Cong (crule, deps, branches)) =
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   266
        let
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   267
          fun sub_step lu i x =
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   268
            let
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   269
              val ((fixes, assumes), st) = nth branches i
44338
700008399ee5 refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
wenzelm
parents: 42495
diff changeset
   270
              val used = map lu (Int_Graph.immediate_succs deps i)
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   271
                |> map (fn u_eq => (u_eq RS sym) RS eq_reflection)
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   272
                |> filter_out Thm.is_reflexive
26196
0a0c2752561e Use conversions instead of simplifier. tuned
krauss
parents: 26115
diff changeset
   273
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 44338
diff changeset
   274
              val assumes' = map (simplify (put_simpset HOL_basic_ss  ctxt addsimps used)) assumes
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   275
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   276
              val (subeq, x') =
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   277
                rewrite_help (fix @ fixes) (h_as @ assumes') x st
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   278
              val subeq_exp =
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   279
                export_thm thy (fixes, assumes) (subeq RS meta_eq_to_obj_eq)
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   280
            in
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   281
              (subeq_exp, x')
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   282
            end
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   283
          val (subthms, x') = fold_deps deps sub_step x
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   284
        in
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   285
          (fold_rev (curry op COMP) subthms crule, x')
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   286
        end
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   287
  in
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   288
    rewrite_help [] [] x tr
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   289
  end
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33519
diff changeset
   290
55085
0e8e4dc55866 moved 'fundef_cong' attribute (and other basic 'fun' stuff) up the dependency chain
blanchet
parents: 51717
diff changeset
   291
val setup =
0e8e4dc55866 moved 'fundef_cong' attribute (and other basic 'fun' stuff) up the dependency chain
blanchet
parents: 51717
diff changeset
   292
  Attrib.setup @{binding fundef_cong} (Attrib.add_del cong_add cong_del)
0e8e4dc55866 moved 'fundef_cong' attribute (and other basic 'fun' stuff) up the dependency chain
blanchet
parents: 51717
diff changeset
   293
    "declaration of congruence rule for function definitions"
0e8e4dc55866 moved 'fundef_cong' attribute (and other basic 'fun' stuff) up the dependency chain
blanchet
parents: 51717
diff changeset
   294
19612
1e133047809a use IntGraph from Pure;
wenzelm
parents: 19564
diff changeset
   295
end