src/HOL/Tools/function_package/context_tree.ML
author wenzelm
Thu, 11 Oct 2007 19:10:19 +0200
changeset 24977 9f98751c9628
parent 24168 86a03a092062
child 25538 58e8ba3b792b
permissions -rw-r--r--
replaced (flip Thm.implies_elim) by Thm.elim_implies;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     1
(*  Title:      HOL/Tools/function_package/context_tree.ML
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     2
    ID:         $Id$
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     3
    Author:     Alexander Krauss, TU Muenchen
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     4
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     5
A package for general recursive function definitions. 
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     6
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
     7
*)
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     8
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     9
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    10
signature FUNDEF_CTXTREE =
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    11
sig
23819
2040846d1bbe some interface cleanup
krauss
parents: 21237
diff changeset
    12
    type depgraph
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    13
    type ctx_tree
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    14
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    15
    (* FIXME: This interface is a mess and needs to be cleaned up! *)
24168
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
    16
    val get_fundef_congs : Context.generic -> thm list
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
    17
    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
    18
    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
    19
24168
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
    20
    val cong_add: attribute
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
    21
    val cong_del: attribute
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
    22
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
    23
    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
    24
23819
2040846d1bbe some interface cleanup
krauss
parents: 21237
diff changeset
    25
    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
    26
23819
2040846d1bbe some interface cleanup
krauss
parents: 21237
diff changeset
    27
    val add_context_varnames : ctx_tree -> string list -> string list
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 export_term : (string * typ) list * term list -> term -> term
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    30
    val export_thm : theory -> (string * typ) list * term list -> thm -> thm
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    31
    val import_thm : theory -> (string * typ) list * thm list -> thm -> thm
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    32
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    33
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    34
    val traverse_tree : 
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    35
   ((string * typ) list * thm list -> term ->
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    36
   (((string * typ) list * thm list) * thm) list ->
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    37
   (((string * typ) list * thm list) * thm) list * 'b ->
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    38
   (((string * typ) list * thm list) * thm) list * 'b)
23819
2040846d1bbe some interface cleanup
krauss
parents: 21237
diff changeset
    39
   -> ctx_tree -> 'b -> 'b
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    40
23819
2040846d1bbe some interface cleanup
krauss
parents: 21237
diff changeset
    41
    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
    42
end
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    43
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    44
structure FundefCtxTree : FUNDEF_CTXTREE =
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    45
struct
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    46
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    47
open FundefCommon
21051
c49467a9c1e1 Switched function package to use the new package for inductive predicates.
krauss
parents: 20854
diff changeset
    48
open FundefLib
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    49
24168
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
    50
structure FundefCongs = GenericDataFun
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
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
    55
  fun merge _ = Thm.merge_thms
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
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
    58
val map_fundef_congs = FundefCongs.map 
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
    59
val get_fundef_congs = FundefCongs.get
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
    60
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
    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
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
    64
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
    65
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
    66
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
    67
23819
2040846d1bbe some interface cleanup
krauss
parents: 21237
diff changeset
    68
type depgraph = int IntGraph.T
2040846d1bbe some interface cleanup
krauss
parents: 21237
diff changeset
    69
2040846d1bbe some interface cleanup
krauss
parents: 21237
diff changeset
    70
datatype ctx_tree 
2040846d1bbe some interface cleanup
krauss
parents: 21237
diff changeset
    71
  = Leaf of term
2040846d1bbe some interface cleanup
krauss
parents: 21237
diff changeset
    72
  | Cong of (term * thm * depgraph * ((string * typ) list * thm list * ctx_tree) list)
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
(* Maps "A == B" to "B" *)
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    79
val meta_rhs_of = snd o Logic.dest_equals
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    80
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    81
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    82
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    83
(*** Dependency analysis for congruence rules ***)
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    84
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    85
fun branch_vars t = 
21237
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
    86
    let 
21188
2aa15b663cd4 minor cleanup
krauss
parents: 21105
diff changeset
    87
      val t' = snd (dest_all_all t)
2aa15b663cd4 minor cleanup
krauss
parents: 21105
diff changeset
    88
      val assumes = Logic.strip_imp_prems t'
2aa15b663cd4 minor cleanup
krauss
parents: 21105
diff changeset
    89
      val concl = Logic.strip_imp_concl t'
2aa15b663cd4 minor cleanup
krauss
parents: 21105
diff changeset
    90
    in (fold (curry add_term_vars) assumes [], term_vars concl)
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    91
    end
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    92
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    93
fun cong_deps crule =
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    94
    let
24168
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
    95
      val branches = map branch_vars (prems_of crule)
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
    96
      val num_branches = (1 upto (length branches)) ~~ branches
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    97
    in
24168
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
    98
      IntGraph.empty
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
    99
        |> fold (fn (i,_)=> IntGraph.new_node (i,i)) num_branches
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
   100
        |> fold (fn ((i,(c1,_)),(j,(_, t2))) => if i = j orelse null (c1 inter t2) then I else IntGraph.add_edge_acyclic (i,j))
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
   101
        (product num_branches num_branches)
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   102
    end
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   103
    
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   104
val add_congs = map (fn c => c RS eq_reflection) [cong, ext] 
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   105
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   106
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   107
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   108
(* Called on the INSTANTIATED branches of the congruence rule *)
19922
984ae977f7aa Fixed name clash.
krauss
parents: 19781
diff changeset
   109
fun mk_branch ctx t = 
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   110
    let
24168
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
   111
      val (ctx', fixes, impl) = dest_all_all_ctx ctx t
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   112
    in
21188
2aa15b663cd4 minor cleanup
krauss
parents: 21105
diff changeset
   113
      (ctx', fixes, Logic.strip_imp_prems impl, rhs_of (Logic.strip_imp_concl impl))
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   114
    end
24168
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
   115
    
21100
cda93bbf35db Fixed bug in the handling of congruence rules
krauss
parents: 21051
diff changeset
   116
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
   117
    (let
21100
cda93bbf35db Fixed bug in the handling of congruence rules
krauss
parents: 21051
diff changeset
   118
       val thy = ProofContext.theory_of ctx
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   119
21100
cda93bbf35db Fixed bug in the handling of congruence rules
krauss
parents: 21051
diff changeset
   120
       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
   121
       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
   122
21100
cda93bbf35db Fixed bug in the handling of congruence rules
krauss
parents: 21051
diff changeset
   123
       val subst = Pattern.match (ProofContext.theory_of ctx) (c, tt') (Vartab.empty, Vartab.empty)
cda93bbf35db Fixed bug in the handling of congruence rules
krauss
parents: 21051
diff changeset
   124
       val branches = map (mk_branch ctx o Envir.beta_norm o Envir.subst_vars subst) subs
cda93bbf35db Fixed bug in the handling of congruence rules
krauss
parents: 21051
diff changeset
   125
       val inst = map (fn v => (cterm_of thy (Var v), cterm_of thy (Envir.subst_vars 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
   126
     in
21237
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   127
   (cterm_instantiate inst r, dep, branches)
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   128
     end
21100
cda93bbf35db Fixed bug in the handling of congruence rules
krauss
parents: 21051
diff changeset
   129
    handle Pattern.MATCH => find_cong_rule ctx fvar h rs t)
cda93bbf35db Fixed bug in the handling of congruence rules
krauss
parents: 21051
diff changeset
   130
  | find_cong_rule _ _ _ [] _ = sys_error "function_package/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
   131
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   132
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20289
diff changeset
   133
fun matchcall fvar (a $ b) = if a = Free fvar then SOME b else NONE
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20289
diff changeset
   134
  | matchcall fvar _ = NONE
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20289
diff changeset
   135
24168
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
   136
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
   137
    let 
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
   138
      val congs = get_fundef_congs (Context.Proof ctxt)
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
   139
      val congs_deps = map (fn c => (c, cong_deps c)) (congs @ add_congs) (* FIXME: Save in theory *)
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
   140
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
   141
      fun mk_tree' ctx t =
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
   142
          case matchcall fvar t of
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
   143
            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
   144
          | NONE => 
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
   145
            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
   146
            else 
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
   147
              let val (r, dep, branches) = find_cong_rule ctx fvar h congs_deps t in
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
   148
                Cong (t, r, dep, 
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
   149
                      map (fn (ctx', fixes, assumes, st) => 
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
   150
                              (fixes, map (assume o cterm_of (ProofContext.theory_of ctx)) assumes, 
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
   151
                               mk_tree' ctx' st)) branches)
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
   152
              end
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
   153
    in
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
   154
      mk_tree' ctxt t
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 23819
diff changeset
   155
    end
21237
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   156
    
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   157
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20289
diff changeset
   158
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
   159
    let
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20289
diff changeset
   160
      val cfvar = cterm_of thy fvar
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20289
diff changeset
   161
      val cf = cterm_of thy f
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20289
diff changeset
   162
               
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20289
diff changeset
   163
      fun inst_term t = 
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20289
diff changeset
   164
          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
   165
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20289
diff changeset
   166
      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
   167
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20289
diff changeset
   168
      fun inst_tree_aux (Leaf t) = Leaf t
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20289
diff changeset
   169
        | inst_tree_aux (Cong (t, crule, deps, branches)) =
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20289
diff changeset
   170
          Cong (inst_term t, inst_thm crule, deps, map inst_branch branches)
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20289
diff changeset
   171
        | inst_tree_aux (RCall (t, str)) =
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20289
diff changeset
   172
          RCall (inst_term t, inst_tree_aux str)
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20289
diff changeset
   173
      and inst_branch (fxs, assms, str) = 
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20289
diff changeset
   174
          (fxs, map (assume o cterm_of thy o inst_term o prop_of) assms, inst_tree_aux str)
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20289
diff changeset
   175
    in
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20289
diff changeset
   176
      inst_tree_aux tr
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20289
diff changeset
   177
    end
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20289
diff changeset
   178
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20289
diff changeset
   179
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20289
diff changeset
   180
21237
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   181
(* FIXME: remove *)   
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   182
fun add_context_varnames (Leaf _) = I
20854
f9cf9e62d11c insert replacing ins ins_int ins_string
haftmann
parents: 20523
diff changeset
   183
  | add_context_varnames (Cong (_, _, _, sub)) = fold (fn (fs, _, st) => fold (insert (op =) o fst) fs o add_context_varnames st) sub
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   184
  | add_context_varnames (RCall (_,st)) = add_context_varnames st
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   185
    
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
(* 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
   188
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
   189
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   190
fun export_term (fixes, assumes) =
19922
984ae977f7aa Fixed name clash.
krauss
parents: 19781
diff changeset
   191
    fold_rev (curry Logic.mk_implies) assumes #> fold_rev (mk_forall 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) =
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   194
    fold_rev (implies_intr o cterm_of thy) assumes
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   195
 #> 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
   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) =
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   198
    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
   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
fun assume_in_ctxt thy (fixes, athms) prop =
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   202
    let
21237
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   203
  val global_assum = export_term (fixes, map prop_of athms) prop
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   204
    in
21237
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   205
  (global_assum,
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   206
   assume (cterm_of thy global_assum) |> import_thm thy (fixes, athms))
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   207
    end
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   208
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   209
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   210
(* 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
   211
fun fold_deps G f x =
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   212
    let
21237
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   213
  fun fill_table i (T, x) =
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   214
      case Inttab.lookup T i of
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   215
    SOME _ => (T, x)
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   216
        | NONE => 
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   217
    let
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   218
        val (T', x') = fold fill_table (IntGraph.imm_succs G i) (T, x)
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   219
        val (v, x'') = f (the o Inttab.lookup T') i x
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   220
    in
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   221
        (Inttab.update (i, v) T', x'')
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   222
    end
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   223
21237
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   224
  val (T, x) = fold fill_table (IntGraph.keys G) (Inttab.empty, x)
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   225
    in
21237
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   226
  (Inttab.fold (cons o snd) T [], x)
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   227
    end
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   228
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   229
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   230
fun flatten xss = fold_rev append xss []
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   231
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   232
fun traverse_tree rcOp tr x =
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   233
    let 
21237
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   234
  fun traverse_help ctx (Leaf _) u x = ([], x)
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   235
    | traverse_help ctx (RCall (t, st)) u x =
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   236
      rcOp ctx t u (traverse_help ctx st u x)
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   237
    | traverse_help ctx (Cong (t, crule, deps, branches)) u x =
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   238
      let
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   239
    fun sub_step lu i x =
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   240
        let
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   241
      val (fixes, assumes, subtree) = nth branches (i - 1)
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   242
      val used = fold_rev (append o lu) (IntGraph.imm_succs deps i) u
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   243
      val (subs, x') = traverse_help (compose ctx (fixes, assumes)) subtree used x
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   244
      val exported_subs = map (apfst (compose (fixes, assumes))) subs
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   245
        in
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   246
      (exported_subs, x')
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   247
        end
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   248
      in
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   249
    fold_deps deps sub_step x
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   250
        |> apfst flatten
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   251
      end
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   252
    in
21237
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   253
  snd (traverse_help ([], []) tr [] x)
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   254
    end
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   255
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   256
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   257
fun is_refl thm = let val (l,r) = Logic.dest_equals (prop_of thm) in l = r end
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   258
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20289
diff changeset
   259
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
   260
    let
21237
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   261
      fun rewrite_help fix f_as h_as x (Leaf t) = (reflexive (cterm_of thy t), x)
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   262
        | rewrite_help fix f_as h_as x (RCall (_ $ arg, st)) =
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   263
          let
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   264
            val (inner, (lRi,ha)::x') = rewrite_help fix f_as h_as x st
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   265
                                                     
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   266
             (* Need not use the simplifier here. Can use primitive steps! *)
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   267
            val rew_ha = if is_refl inner then I else simplify (HOL_basic_ss addsimps [inner])
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   268
           
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   269
            val h_a_eq_h_a' = combination (reflexive (cterm_of thy h)) inner
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   270
            val iha = import_thm thy (fix, h_as) ha (* (a', h a') : G *)
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   271
                                 |> rew_ha
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   272
                      
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   273
            val inst_ih = instantiate' [] [SOME (cterm_of thy arg)] ih
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   274
            val eq = implies_elim (implies_elim inst_ih lRi) iha
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   275
                     
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   276
            val h_a'_eq_f_a' = eq RS eq_reflection
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   277
            val result = transitive h_a_eq_h_a' h_a'_eq_f_a'
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   278
          in
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   279
            (result, x')
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   280
          end
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   281
        | rewrite_help fix f_as h_as x (Cong (t, crule, deps, branches)) =
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   282
          let
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   283
            fun sub_step lu i x =
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   284
                let
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   285
                  val (fixes, assumes, st) = nth branches (i - 1)
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   286
                  val used = fold_rev (cons o lu) (IntGraph.imm_succs deps i) []
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   287
                  val used_rev = map (fn u_eq => (u_eq RS sym) RS eq_reflection) used
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   288
                  val assumes' = map (simplify (HOL_basic_ss addsimps (filter_out is_refl used_rev))) assumes
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   289
                                 
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   290
                  val (subeq, x') = rewrite_help (fix @ fixes) (f_as @ assumes) (h_as @ assumes') x st
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   291
                  val subeq_exp = export_thm thy (fixes, map prop_of assumes) (subeq RS meta_eq_to_obj_eq)
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   292
                in
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   293
                  (subeq_exp, x')
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   294
                end
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   295
                
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   296
            val (subthms, x') = fold_deps deps sub_step x
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   297
          in
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   298
            (fold_rev (curry op COMP) subthms crule, x')
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   299
          end
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   300
    in
21237
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   301
      rewrite_help [] [] [] x tr
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   302
    end
21237
b803f9870e97 untabified
krauss
parents: 21188
diff changeset
   303
    
19612
1e133047809a use IntGraph from Pure;
wenzelm
parents: 19564
diff changeset
   304
end