src/HOL/Tools/Function/context_tree.ML
author haftmann
Wed, 21 Oct 2009 12:09:37 +0200
changeset 33049 c38f02fdf35d
parent 33038 8f9594c31de4
child 33099 b8cdd3d73022
permissions -rw-r--r--
curried inter as canonical list operation (beware of argument order)
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
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     4
A package for general recursive function definitions. 
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     5
Builds and traverses trees of nested contexts along a term.
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     6
*)
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     7
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     8
signature FUNDEF_CTXTREE =
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     9
sig
26115
3c38ef7cf54f removed dead code; some cleanup
krauss
parents: 25538
diff changeset
    10
    type ctxt = (string * typ) list * thm list (* poor man's contexts: fixes + assumes *)
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    11
    type ctx_tree
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    12
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    13
    (* FIXME: This interface is a mess and needs to be cleaned up! *)
30492
cb7e886e4b10 get data from plain Proof.context;
wenzelm
parents: 29329
diff changeset
    14
    val get_fundef_congs : Proof.context -> thm list
24168
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
    15
    val add_fundef_cong : thm -> Context.generic -> Context.generic
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
    16
    val map_fundef_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
24168
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
    18
    val cong_add: attribute
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
    19
    val cong_del: attribute
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
    20
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
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
23819
2040846d1bbe some interface cleanup
krauss
parents: 21237
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
26115
3c38ef7cf54f removed dead code; some cleanup
krauss
parents: 25538
diff changeset
    25
    val export_term : ctxt -> term -> term
3c38ef7cf54f removed dead code; some cleanup
krauss
parents: 25538
diff changeset
    26
    val export_thm : theory -> ctxt -> thm -> thm
3c38ef7cf54f removed dead code; some cleanup
krauss
parents: 25538
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
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
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
23819
2040846d1bbe some interface cleanup
krauss
parents: 21237
diff changeset
    36
    val rewrite_by_tree : theory -> term -> thm -> (thm * thm) list -> ctx_tree -> thm * (thm * thm) list
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    37
end
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    38
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    39
structure FundefCtxTree : FUNDEF_CTXTREE =
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    40
struct
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    41
26115
3c38ef7cf54f removed dead code; some cleanup
krauss
parents: 25538
diff changeset
    42
type ctxt = (string * typ) list * thm list
3c38ef7cf54f removed dead code; some cleanup
krauss
parents: 25538
diff changeset
    43
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    44
open FundefCommon
21051
c49467a9c1e1 Switched function package to use the new package for inductive predicates.
krauss
parents: 20854
diff changeset
    45
open FundefLib
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    46
24168
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
    47
structure FundefCongs = GenericDataFun
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
    48
(
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
    49
  type T = thm list
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
    50
  val empty = []
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
    51
  val extend = I
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
    52
  fun merge _ = Thm.merge_thms
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
    53
);
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
    54
30492
cb7e886e4b10 get data from plain Proof.context;
wenzelm
parents: 29329
diff changeset
    55
val get_fundef_congs = FundefCongs.get o Context.Proof
cb7e886e4b10 get data from plain Proof.context;
wenzelm
parents: 29329
diff changeset
    56
val map_fundef_congs = FundefCongs.map
24168
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
    57
val add_fundef_cong = FundefCongs.map o Thm.add_thm
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
    58
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
    59
(* congruence rules *)
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
    60
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
    61
val cong_add = Thm.declaration_attribute (map_fundef_congs o Thm.add_thm o safe_mk_meta_eq);
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
    62
val cong_del = Thm.declaration_attribute (map_fundef_congs o Thm.del_thm o safe_mk_meta_eq);
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
    63
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
    64
23819
2040846d1bbe some interface cleanup
krauss
parents: 21237
diff changeset
    65
type depgraph = int IntGraph.T
2040846d1bbe some interface cleanup
krauss
parents: 21237
diff changeset
    66
2040846d1bbe some interface cleanup
krauss
parents: 21237
diff changeset
    67
datatype ctx_tree 
2040846d1bbe some interface cleanup
krauss
parents: 21237
diff changeset
    68
  = Leaf of term
26115
3c38ef7cf54f removed dead code; some cleanup
krauss
parents: 25538
diff changeset
    69
  | Cong of (thm * depgraph * (ctxt * ctx_tree) list)
23819
2040846d1bbe some interface cleanup
krauss
parents: 21237
diff changeset
    70
  | RCall of (term * ctx_tree)
2040846d1bbe some interface cleanup
krauss
parents: 21237
diff changeset
    71
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    72
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    73
(* Maps "Trueprop A = B" to "A" *)
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    74
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
    75
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    76
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    77
(*** Dependency analysis for congruence rules ***)
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
fun branch_vars t = 
21237
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
    80
    let 
21188
2aa15b663cd4 minor cleanup
krauss
parents: 21105
diff changeset
    81
      val t' = snd (dest_all_all t)
26529
03ad378ed5f0 Function package no longer overwrites theorems.
krauss
parents: 26196
diff changeset
    82
      val (assumes, concl) = Logic.strip_horn t'
29329
e02b3a32f34f removed references to OldTerm.*
krauss
parents: 29265
diff changeset
    83
    in (fold Term.add_vars assumes [], Term.add_vars concl [])
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    84
    end
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    85
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    86
fun cong_deps crule =
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    87
    let
26115
3c38ef7cf54f removed dead code; some cleanup
krauss
parents: 25538
diff changeset
    88
      val num_branches = map_index (apsnd branch_vars) (prems_of crule)
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    89
    in
24168
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
    90
      IntGraph.empty
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
    91
        |> fold (fn (i,_)=> IntGraph.new_node (i,i)) num_branches
29329
e02b3a32f34f removed references to OldTerm.*
krauss
parents: 29265
diff changeset
    92
        |> fold_product (fn (i, (c1, _)) => fn (j, (_, t2)) => 
33049
c38f02fdf35d curried inter as canonical list operation (beware of argument order)
haftmann
parents: 33038
diff changeset
    93
               if i = j orelse null (inter (op =) c1 t2)
29329
e02b3a32f34f removed references to OldTerm.*
krauss
parents: 29265
diff changeset
    94
               then I else IntGraph.add_edge_acyclic (i,j))
25538
58e8ba3b792b map_product and fold_product
haftmann
parents: 24977
diff changeset
    95
             num_branches num_branches
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    96
    end
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    97
    
26115
3c38ef7cf54f removed dead code; some cleanup
krauss
parents: 25538
diff changeset
    98
val default_congs = 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
    99
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   100
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   101
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   102
(* Called on the INSTANTIATED branches of the congruence rule *)
19922
984ae977f7aa Fixed name clash.
krauss
parents: 19781
diff changeset
   103
fun mk_branch ctx t = 
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   104
    let
24168
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
   105
      val (ctx', fixes, impl) = dest_all_all_ctx ctx t
26529
03ad378ed5f0 Function package no longer overwrites theorems.
krauss
parents: 26196
diff changeset
   106
      val (assms, concl) = Logic.strip_horn impl
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   107
    in
26529
03ad378ed5f0 Function package no longer overwrites theorems.
krauss
parents: 26196
diff changeset
   108
      (ctx', fixes, assms, rhs_of concl)
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   109
    end
24168
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
   110
    
21100
cda93bbf35db Fixed bug in the handling of congruence rules
krauss
parents: 21051
diff changeset
   111
fun find_cong_rule ctx fvar h ((r,dep)::rs) t =
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   112
    (let
21100
cda93bbf35db Fixed bug in the handling of congruence rules
krauss
parents: 21051
diff changeset
   113
       val thy = ProofContext.theory_of ctx
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   114
21100
cda93bbf35db Fixed bug in the handling of congruence rules
krauss
parents: 21051
diff changeset
   115
       val tt' = Logic.mk_equals (Pattern.rewrite_term thy [(Free fvar, h)] [] t, t)
cda93bbf35db Fixed bug in the handling of congruence rules
krauss
parents: 21051
diff changeset
   116
       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
   117
21100
cda93bbf35db Fixed bug in the handling of congruence rules
krauss
parents: 21051
diff changeset
   118
       val subst = Pattern.match (ProofContext.theory_of ctx) (c, tt') (Vartab.empty, Vartab.empty)
32035
8e77b6a250d5 tuned/modernized Envir.subst_XXX;
wenzelm
parents: 31775
diff changeset
   119
       val branches = map (mk_branch ctx o Envir.beta_norm o Envir.subst_term subst) subs
8e77b6a250d5 tuned/modernized Envir.subst_XXX;
wenzelm
parents: 31775
diff changeset
   120
       val inst = map (fn v =>
8e77b6a250d5 tuned/modernized Envir.subst_XXX;
wenzelm
parents: 31775
diff changeset
   121
        (cterm_of thy (Var v), cterm_of thy (Envir.subst_term subst (Var v)))) (Term.add_vars c [])
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   122
     in
21237
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   123
   (cterm_instantiate inst r, dep, branches)
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   124
     end
21100
cda93bbf35db Fixed bug in the handling of congruence rules
krauss
parents: 21051
diff changeset
   125
    handle Pattern.MATCH => find_cong_rule ctx fvar h rs t)
31775
2b04504fcb69 uniformly capitialized names for subdirectories
haftmann
parents: 30492
diff changeset
   126
  | find_cong_rule _ _ _ [] _ = sys_error "Function/context_tree.ML: No cong rule found!"
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   127
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   128
24168
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
   129
fun mk_tree fvar h ctxt t =
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
   130
    let 
30492
cb7e886e4b10 get data from plain Proof.context;
wenzelm
parents: 29329
diff changeset
   131
      val congs = get_fundef_congs ctxt
26115
3c38ef7cf54f removed dead code; some cleanup
krauss
parents: 25538
diff changeset
   132
      val congs_deps = map (fn c => (c, cong_deps c)) (congs @ default_congs) (* FIXME: Save in theory *)
3c38ef7cf54f removed dead code; some cleanup
krauss
parents: 25538
diff changeset
   133
3c38ef7cf54f removed dead code; some cleanup
krauss
parents: 25538
diff changeset
   134
      fun matchcall (a $ b) = if a = Free fvar then SOME b else NONE
3c38ef7cf54f removed dead code; some cleanup
krauss
parents: 25538
diff changeset
   135
        | matchcall _ = NONE
24168
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
   136
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
   137
      fun mk_tree' ctx t =
26115
3c38ef7cf54f removed dead code; some cleanup
krauss
parents: 25538
diff changeset
   138
          case matchcall t of
24168
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
   139
            SOME arg => RCall (t, mk_tree' ctx arg)
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
   140
          | NONE => 
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
   141
            if not (exists_subterm (fn Free v => v = fvar | _ => false) t) then Leaf t
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
   142
            else 
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
   143
              let val (r, dep, branches) = find_cong_rule ctx fvar h congs_deps t in
26115
3c38ef7cf54f removed dead code; some cleanup
krauss
parents: 25538
diff changeset
   144
                Cong (r, dep, 
24168
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
   145
                      map (fn (ctx', fixes, assumes, st) => 
26115
3c38ef7cf54f removed dead code; some cleanup
krauss
parents: 25538
diff changeset
   146
                              ((fixes, map (assume o cterm_of (ProofContext.theory_of ctx)) assumes), 
24168
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
   147
                               mk_tree' ctx' st)) branches)
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
   148
              end
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
   149
    in
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
   150
      mk_tree' ctxt t
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
   151
    end
21237
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   152
    
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   153
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20289
diff changeset
   154
fun inst_tree thy fvar f tr =
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20289
diff changeset
   155
    let
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20289
diff changeset
   156
      val cfvar = cterm_of thy fvar
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20289
diff changeset
   157
      val cf = cterm_of thy f
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20289
diff changeset
   158
               
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20289
diff changeset
   159
      fun inst_term t = 
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20289
diff changeset
   160
          subst_bound(f, abstract_over (fvar, t))
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20289
diff changeset
   161
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20289
diff changeset
   162
      val inst_thm = forall_elim cf o forall_intr cfvar 
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20289
diff changeset
   163
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20289
diff changeset
   164
      fun inst_tree_aux (Leaf t) = Leaf t
26115
3c38ef7cf54f removed dead code; some cleanup
krauss
parents: 25538
diff changeset
   165
        | inst_tree_aux (Cong (crule, deps, branches)) =
3c38ef7cf54f removed dead code; some cleanup
krauss
parents: 25538
diff changeset
   166
          Cong (inst_thm crule, deps, map inst_branch branches)
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20289
diff changeset
   167
        | inst_tree_aux (RCall (t, str)) =
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20289
diff changeset
   168
          RCall (inst_term t, inst_tree_aux str)
26115
3c38ef7cf54f removed dead code; some cleanup
krauss
parents: 25538
diff changeset
   169
      and inst_branch ((fxs, assms), str) = 
3c38ef7cf54f removed dead code; some cleanup
krauss
parents: 25538
diff changeset
   170
          ((fxs, map (assume o cterm_of thy o inst_term o prop_of) assms), inst_tree_aux str)
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20289
diff changeset
   171
    in
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20289
diff changeset
   172
      inst_tree_aux tr
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20289
diff changeset
   173
    end
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20289
diff changeset
   174
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20289
diff changeset
   175
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   176
(* 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
   177
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
   178
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   179
fun export_term (fixes, assumes) =
26115
3c38ef7cf54f removed dead code; some cleanup
krauss
parents: 25538
diff changeset
   180
    fold_rev (curry Logic.mk_implies o prop_of) assumes 
27330
1af2598b5f7d Logic.all/mk_equals/mk_implies;
wenzelm
parents: 26529
diff changeset
   181
 #> 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
   182
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   183
fun export_thm thy (fixes, assumes) =
26115
3c38ef7cf54f removed dead code; some cleanup
krauss
parents: 25538
diff changeset
   184
    fold_rev (implies_intr o cprop_of) assumes
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   185
 #> fold_rev (forall_intr o cterm_of thy o Free) fixes
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   186
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   187
fun import_thm thy (fixes, athms) =
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   188
    fold (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
   189
 #> fold Thm.elim_implies athms
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   190
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   191
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   192
(* 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
   193
fun fold_deps G f x =
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   194
    let
26115
3c38ef7cf54f removed dead code; some cleanup
krauss
parents: 25538
diff changeset
   195
      fun fill_table i (T, x) =
3c38ef7cf54f removed dead code; some cleanup
krauss
parents: 25538
diff changeset
   196
          case Inttab.lookup T i of
3c38ef7cf54f removed dead code; some cleanup
krauss
parents: 25538
diff changeset
   197
            SOME _ => (T, x)
3c38ef7cf54f removed dead code; some cleanup
krauss
parents: 25538
diff changeset
   198
          | NONE => 
3c38ef7cf54f removed dead code; some cleanup
krauss
parents: 25538
diff changeset
   199
            let
3c38ef7cf54f removed dead code; some cleanup
krauss
parents: 25538
diff changeset
   200
              val (T', x') = fold fill_table (IntGraph.imm_succs G i) (T, x)
3c38ef7cf54f removed dead code; some cleanup
krauss
parents: 25538
diff changeset
   201
              val (v, x'') = f (the o Inttab.lookup T') i x'
3c38ef7cf54f removed dead code; some cleanup
krauss
parents: 25538
diff changeset
   202
            in
3c38ef7cf54f removed dead code; some cleanup
krauss
parents: 25538
diff changeset
   203
              (Inttab.update (i, v) T', x'')
3c38ef7cf54f removed dead code; some cleanup
krauss
parents: 25538
diff changeset
   204
            end
3c38ef7cf54f removed dead code; some cleanup
krauss
parents: 25538
diff changeset
   205
            
3c38ef7cf54f removed dead code; some cleanup
krauss
parents: 25538
diff changeset
   206
      val (T, x) = fold fill_table (IntGraph.keys G) (Inttab.empty, x)
21237
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   207
    in
26115
3c38ef7cf54f removed dead code; some cleanup
krauss
parents: 25538
diff changeset
   208
      (Inttab.fold (cons o snd) T [], x)
21237
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   209
    end
26115
3c38ef7cf54f removed dead code; some cleanup
krauss
parents: 25538
diff changeset
   210
    
3c38ef7cf54f removed dead code; some cleanup
krauss
parents: 25538
diff changeset
   211
fun traverse_tree rcOp tr =
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   212
    let 
26115
3c38ef7cf54f removed dead code; some cleanup
krauss
parents: 25538
diff changeset
   213
  fun traverse_help ctx (Leaf _) _ x = ([], x)
21237
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   214
    | traverse_help ctx (RCall (t, st)) u x =
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   215
      rcOp ctx t u (traverse_help ctx st u x)
26115
3c38ef7cf54f removed dead code; some cleanup
krauss
parents: 25538
diff changeset
   216
    | traverse_help ctx (Cong (_, deps, branches)) u x =
21237
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   217
      let
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   218
    fun sub_step lu i x =
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   219
        let
26115
3c38ef7cf54f removed dead code; some cleanup
krauss
parents: 25538
diff changeset
   220
          val (ctx', subtree) = nth branches i
3c38ef7cf54f removed dead code; some cleanup
krauss
parents: 25538
diff changeset
   221
          val used = fold_rev (append o lu) (IntGraph.imm_succs deps i) u
3c38ef7cf54f removed dead code; some cleanup
krauss
parents: 25538
diff changeset
   222
          val (subs, x') = traverse_help (compose ctx ctx') subtree used x
3c38ef7cf54f removed dead code; some cleanup
krauss
parents: 25538
diff changeset
   223
          val exported_subs = map (apfst (compose ctx')) subs (* FIXME: Right order of composition? *)
21237
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   224
        in
26115
3c38ef7cf54f removed dead code; some cleanup
krauss
parents: 25538
diff changeset
   225
          (exported_subs, x')
21237
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   226
        end
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   227
      in
26115
3c38ef7cf54f removed dead code; some cleanup
krauss
parents: 25538
diff changeset
   228
        fold_deps deps sub_step x
3c38ef7cf54f removed dead code; some cleanup
krauss
parents: 25538
diff changeset
   229
          |> apfst flat
21237
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   230
      end
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   231
    in
26115
3c38ef7cf54f removed dead code; some cleanup
krauss
parents: 25538
diff changeset
   232
      snd o traverse_help ([], []) tr []
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   233
    end
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   234
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20289
diff changeset
   235
fun rewrite_by_tree thy h ih x tr =
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   236
    let
26196
0a0c2752561e Use conversions instead of simplifier. tuned
krauss
parents: 26115
diff changeset
   237
      fun rewrite_help _ _ x (Leaf t) = (reflexive (cterm_of thy t), x)
0a0c2752561e Use conversions instead of simplifier. tuned
krauss
parents: 26115
diff changeset
   238
        | rewrite_help fix h_as x (RCall (_ $ arg, st)) =
21237
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   239
          let
26196
0a0c2752561e Use conversions instead of simplifier. tuned
krauss
parents: 26115
diff changeset
   240
            val (inner, (lRi,ha)::x') = rewrite_help fix h_as x st (* "a' = a" *)
21237
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   241
                                                     
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   242
            val iha = import_thm thy (fix, h_as) ha (* (a', h a') : G *)
26196
0a0c2752561e Use conversions instead of simplifier. tuned
krauss
parents: 26115
diff changeset
   243
                 |> Conv.fconv_rule (Conv.arg_conv (Conv.comb_conv (Conv.arg_conv (K inner))))
0a0c2752561e Use conversions instead of simplifier. tuned
krauss
parents: 26115
diff changeset
   244
                                                    (* (a, h a) : G   *)
21237
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   245
            val inst_ih = instantiate' [] [SOME (cterm_of thy arg)] ih
26196
0a0c2752561e Use conversions instead of simplifier. tuned
krauss
parents: 26115
diff changeset
   246
            val eq = implies_elim (implies_elim inst_ih lRi) iha (* h a = f a *)
21237
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   247
                     
26196
0a0c2752561e Use conversions instead of simplifier. tuned
krauss
parents: 26115
diff changeset
   248
            val h_a'_eq_h_a = combination (reflexive (cterm_of thy h)) inner
0a0c2752561e Use conversions instead of simplifier. tuned
krauss
parents: 26115
diff changeset
   249
            val h_a_eq_f_a = eq RS eq_reflection
0a0c2752561e Use conversions instead of simplifier. tuned
krauss
parents: 26115
diff changeset
   250
            val result = transitive h_a'_eq_h_a h_a_eq_f_a
21237
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   251
          in
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   252
            (result, x')
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   253
          end
26196
0a0c2752561e Use conversions instead of simplifier. tuned
krauss
parents: 26115
diff changeset
   254
        | rewrite_help fix h_as x (Cong (crule, deps, branches)) =
21237
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   255
          let
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   256
            fun sub_step lu i x =
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   257
                let
26115
3c38ef7cf54f removed dead code; some cleanup
krauss
parents: 25538
diff changeset
   258
                  val ((fixes, assumes), st) = nth branches i
26196
0a0c2752561e Use conversions instead of simplifier. tuned
krauss
parents: 26115
diff changeset
   259
                  val used = map lu (IntGraph.imm_succs deps i)
0a0c2752561e Use conversions instead of simplifier. tuned
krauss
parents: 26115
diff changeset
   260
                             |> map (fn u_eq => (u_eq RS sym) RS eq_reflection)
0a0c2752561e Use conversions instead of simplifier. tuned
krauss
parents: 26115
diff changeset
   261
                             |> filter_out Thm.is_reflexive
0a0c2752561e Use conversions instead of simplifier. tuned
krauss
parents: 26115
diff changeset
   262
0a0c2752561e Use conversions instead of simplifier. tuned
krauss
parents: 26115
diff changeset
   263
                  val assumes' = map (simplify (HOL_basic_ss addsimps used)) assumes
21237
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   264
                                 
26196
0a0c2752561e Use conversions instead of simplifier. tuned
krauss
parents: 26115
diff changeset
   265
                  val (subeq, x') = rewrite_help (fix @ fixes) (h_as @ assumes') x st
26115
3c38ef7cf54f removed dead code; some cleanup
krauss
parents: 25538
diff changeset
   266
                  val subeq_exp = export_thm thy (fixes, assumes) (subeq RS meta_eq_to_obj_eq)
21237
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   267
                in
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   268
                  (subeq_exp, x')
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   269
                end
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   270
                
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   271
            val (subthms, x') = fold_deps deps sub_step x
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   272
          in
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   273
            (fold_rev (curry op COMP) subthms crule, x')
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   274
          end
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   275
    in
26196
0a0c2752561e Use conversions instead of simplifier. tuned
krauss
parents: 26115
diff changeset
   276
      rewrite_help [] [] x tr
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   277
    end
21237
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   278
    
19612
1e133047809a use IntGraph from Pure;
wenzelm
parents: 19564
diff changeset
   279
end